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:
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.
y_x = input ( 'Enter your function of x = ' )
% allowing the user to modify the function as required
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?
x Turn on thread page Beta
Stuck on MatLab Code watch
- Thread Starter
Last edited by Alpha-Omega; 25-03-2013 at 22:12.
- 25-03-2013 22:09
- 25-03-2013 22:13
- Thread Starter
- 25-03-2013 22:31
LOL no wonder I thought the reply was pretty quick
- 25-03-2013 23:50
I did a bit of googling and found this: http://www.mathworks.co.uk/help/matlab/ref/eval.html
x = 1 : 2 : 5; y_x = input ('Enter your function of x = ','s'); y_eval = eval(y_x); S = 2 .* sum(abs(y_eval)) plot( x , y_eval) title(['Numeric Integration for ',num2str(y_x),' with 2 slices'])