# Thread: Runge-Kutta method for a system, using MATLAB

1. ## Runge-Kutta method for a system, using MATLAB

An initial value problem and its exact solution are given. Use the Runge-Kutta method with step sizes $h=0.1$ and $h=0.05$ to approximate the values $x(1)$ and $y(1)$. Compare the approximations with the actual values.

$x'=-x-y-(1+t^3)e^{-t}$, $x(0)=0$,

$y'=-x-y-(t-3t^2)e^{-t}$, $y(0)=1$;

$x(t)=e^{-t}(\sin t-t)$, $y(t)=e^{-t}(\cos t+t^3)$

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?

2. ## Re: Runge-Kutta method for a system, using MATLAB

There's probably a mistake in the question: the 1st DE should be $x'=-x+y-(1+t^3)e^{-t}$. After making that change, I get the correct answer.