Varying step size for RK Method?

3 vues (au cours des 30 derniers jours)
Bakican Ayna
Bakican Ayna le 1 Avr 2020
Modifié(e) : James Tursa le 1 Avr 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?

Réponse acceptée

James Tursa
James Tursa le 1 Avr 2020
Modifié(e) : James Tursa le 1 Avr 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.

Plus de réponses (0)

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by