Consider the initialvalue problem y 6y 6 0 lessthanorequal
Consider the initial-value problem y\' = -6y + 6, 0 lessthanorequalto t lessthanorequalto 1,y(0) = 2 with exact solution y(t) = 1 + e^-6t Write a code to use Runge-Kutta method of order 4 with h = 0.1 to approximate the solution
Solution
h = 0.1; % step size
t = 0;
w = 2; % initial condition
fprintf(’Step 0: t = %12.8f, w = %12.8f\ ’, t, w);
for i=1:10 % step size is 0.1, From t=0 to t=1, it takes a step size of 0.1.It takes 10 steps So we have calculate 10 ietrations
k1 = h*f(t,w);
k2 = h*f(t+h/2, w+k1/2);
k3 = h*f(t+h/2, w+k2/2);
k4 = h*f(t+h, w+k3);
w = w + (k1+2*k2+2*k3+k4)/6;
t = t + h;
fprintf(’Step %d: t = %6.4f, w = %18.15f\ ’, i, t, w);
end
function z= f(t,y)
z = -6*y+1;
