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!