Varying step size for RK Method?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Bakican Ayna
el 1 de Abr. de 2020
Editada: James Tursa
el 1 de Abr. de 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?
0 comentarios
Respuesta aceptada
James Tursa
el 1 de Abr. de 2020
Editada: James Tursa
el 1 de Abr. de 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.
0 comentarios
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!