Help looping scalars in Euler Method function

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)

回答 (1 件)

Geoff Hayes
Geoff Hayes 2018 年 2 月 11 日

0 投票

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).

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

質問済み:

JR
2018 年 2 月 11 日

回答済み:

2018 年 2 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by