Varying step size for RK Method?

3 visualizzazioni (ultimi 30 giorni)
Bakican Ayna
Bakican Ayna il 1 Apr 2020
Modificato: James Tursa il 1 Apr 2020
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?

Risposta accettata

James Tursa
James Tursa il 1 Apr 2020
Modificato: James Tursa il 1 Apr 2020
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.

Più risposte (0)

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by