Varying step size for RK Method?

조회 수: 2 (최근 30일)
Bakican Ayna
Bakican Ayna 2020년 4월 1일
편집: James Tursa 2020년 4월 1일
f =@(x,y) 150*(x-y*exp(100));
a = 0;
b = 2;
n = 150;
h = (b-a)/n; % Step Size
y(1) = 0; %Initial Condition
i= 0;
for x = a:h:b
i = i + 1;
k1 = f(x,y(i));
k2 = f(x+0.5*h,y(i)+0.5*h*k1);
k3 = f(x+0.5*h,y(i)+0.5*h*k2);
k4 = f(x+h,y(i)+h*k3);
y(i+1) = y(i) + (1/6)*h*(k1 + 2*k2 + 2*k3 + k4);
end
I'm asked to calculate y(2) using different step sizes for n=200, n=100 and n=50 respectively. However, how am i supposed to include all those step sizes without defining new a,b,n, and h?

채택된 답변

James Tursa
James Tursa 2020년 4월 1일
편집: James Tursa 2020년 4월 1일
The only variable that depends on a new n is h, so only recalculate h and then run your loop as is. Although if you want to make sure x gets to b exactly you should use something like linspace(a,b,n+1) instead of a:h:b. Also, you should clear y before each run.

추가 답변 (0개)

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by