# Runge-Kutta method for a system, using MATLAB

• Dec 10th 2011, 12:32 AM
alexmahone
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 $\displaystyle h=0.1$ and $\displaystyle h=0.05$ to approximate the values $\displaystyle x(1)$ and $\displaystyle y(1)$. Compare the approximations with the actual values.

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

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

$\displaystyle x(t)=e^{-t}(\sin t-t)$, $\displaystyle 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?
• Dec 10th 2011, 01:13 AM
alexmahone
Re: Runge-Kutta method for a system, using MATLAB
There's probably a mistake in the question: the 1st DE should be $\displaystyle x'=-x+y-(1+t^3)e^{-t}$. After making that change, I get the correct answer.