Help looping scalars in Euler Method function
Mostra commenti meno recenti
I'd like to loop soln_Analytic (below) for different scalar values. As an example, it would run the function for h = .5, then for h = .6, to h = 3.0. The function below defines Euler's method where F is an anonymous function and t0, h, tfinal, and y0 are scalars.
function yout = ode1_WorkingCode(F,t0,h,tfinal,y0)
y = y0; % put y0 into y
yout = y; % put y into the output
for t = t0 : h : tfinal - h
s = F(t,y); y = y + h*s;
yout = [yout ; y];
Here I give the anonymous function, and define the scalar values. I think this is where I should put the for loop but I can't figure out how to execute it. Any help is appreciated.
k = 0.2;
F = @(t,y) -k*y
t0 = 0;
h = .5;
tfinal = 5;
y0 = 1;
soln_Analytic = ode1_WorkingCode(F,t0,h,tfinal,y0)
Risposte (1)
Geoff Hayes
il 11 Feb 2018
hr - if you want your h to change from 0.5, 0.6,0.7,..., 3.0, then you can simply do
for h=0.5:0.1:3.0
% do your calculation
end
Presumably you will want to save each result to an array (to avoid overwriting the value from the previous iteration).
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!