Beginner - stuck on Matlab
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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!
0 Commenti
Risposta accettata
Walter Roberson
il 25 Mar 2013
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
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!