필터 지우기
필터 지우기

Beginner - stuck on Matlab

조회 수: 2 (최근 30일)
Dominic
Dominic 2013년 3월 25일
It's a question on MatLab Integration program, but have a look at this code first. It's a part of the Integration on Rectangle Method program:
[LINES 1-3]
x = 1 : 2 : 5; % x=lower limit: strip width: upper limit
% These are not supposed to be constants, but for this question's simplicity, let's keep it that way.
[LINE 4]
y_x = input ( 'Enter your function of x = ' )
% allowing the user to modify the function as required
[LINE 5]
S = 2 .* ( sum (abs (y_x)) )
Now, that works fine with the subsequent lines to calculate the approximation of the integration; but, I want what was input by the user for y_x to be also used for the plot TITLE, DISP, and others. The issue is y_x turns to an array instantly. If I turn the input into string [Ex: y_x = input ( ' Enter you function of x = ' , 's') ], it works with the DISP and TITLE, but it doesn't process the x array, so I don't get the integration approximation. Example, say x.^2 was input. Hence, y_x = x.^2
[LINE 6 - 7]
plot( x , y_x)
title(['Numeric Integration for' , num2str(y_x) , 'with 2 slices')
Instead of title being 'Numeric Integration for x.^2 with 2 slices', what shows is 'Numeric Integration for 1 9 25 with 2 slices.' [1 9 25] is the squares for x=1 : 2 : 5. That's because the input has turned to an array and info is lost. Anyone knows how to get around this problem?
Another problem is I also want the equation to be used on the FOR function:
for x = 1 : 2 : 5
y_x = ??? (something to go through the array and produce an array of y_x)
plot( [x x], [0 y_x])
end
That FOR function is to make vertical lines from the x-axis to the coordinate (x, y_x).
Thanks a lot!

채택된 답변

Walter Roberson
Walter Roberson 2013년 3월 25일
y_xs = input('Enter your function of x =', 's');
y_x = str2func( ['@(x) ', y_xs] );
Now, to calculate,
y_x(x)
and include y_xs in the title
  댓글 수: 1
Dominic
Dominic 2013년 3월 25일
You're awesome! It works now. Cheers

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by