An initial value problem and its exact solution are given. Use the Runge-Kutta method with step sizes and to approximate the values and . Compare the approximations with the actual values.
, ,
, ;
,
Code:
A=[-1 -1;-1 -1];
f=inline('-exp(-t)*[1+t^3;t-3*t^2]');
t=0;
t1=1;
x=[0;1];
h=0.1;
n=(t1-t)/h;
for i=1:n
k1=A*x+f(t);
k2=A*(x+h*k1/2)+f(t+1/2*h);
k3=A*(x+h*k2/2)+f(t+1/2*h);
k4=A*(x+h*k3)+f(t+h);
k=(k1+2*k2+2*k3+k4)/6;
t=t+h;
x=x+h*k;
end
h
x
t=0;
x=[0;1];
h=0.05;
n=(t1-t)/h;
for i=1:n
k1=A*x+f(t);
k2=A*(x+h*k1/2)+f(t+1/2*h);
k3=A*(x+h*k2/2)+f(t+1/2*h);
k4=A*(x+h*k3)+f(t+h);
k=(k1+2*k2+2*k3+k4)/6;
t=t+h;
x=x+h*k;
end
h
x
disp('Actual values:')
x=[exp(-1)*(sin(1)-1);exp(-1)*(cos(1)+1)]
The output is:
Code:
h =
0.1000
x =
-0.9904
0.9732
h =
0.0500
x =
-0.9904
0.9732
Actual values:
x =
-0.0583
0.5666
My Runge-Kutta approximations don't tally with the actual values. Where have I gone wrong?