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
2018-2-11
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 Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!