Varying step size for RK Method?
Show older comments
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?
Accepted Answer
More Answers (0)
Categories
Find more on Runge Kutta Methods in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!