Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

consider the system given by: $$x'_{1}=9x_{1}+24x_{2}+5\cos t-\dfrac{1}{3}\sin t$$ $$x'_{2}=-24x_{1}-51x_{2}-9\cos t+\dfrac{1}{3}\sin t$$ with initial values $$x_{1}(0)=\dfrac{4}{3}$$ and $$x_2(0)=\dfrac{2}{3}$$ Implement the RK4 algorithm in MATLAB or JAVA to solve the system from t=0 to t=20. Let h=1/1000,1/25, 1/20,1/15,4/55, compute $x_2(20)$ in each case. Thanks, i don't even try because i really don´t like numerical analysis nor applied math, so i don´t know how to do that in a programming language, i would sincerely appreciate your help on this, i don´t know how to proceed on that so i would appreciate even a short explanation of what i need to do or how to do that, if you consider this question do not belongs to this stack exchange please tell me and i would retry it from here and put in on a programming or CS forum.


share|cite|improve this question
As a warm up problem, you could implement Euler's method to solve this system. Euler's method is pretty simple. Do you know how you would do that? – littleO Nov 13 '12 at 6:29
Do you have access to Matlab? – littleO Nov 13 '12 at 6:38
Off course i know how to use MATLAB, but i don´t like numerical analysis, the Euler method i know it, and now how to implement it in MATLAB – Sebastian Griotberg Nov 13 '12 at 14:44
up vote 3 down vote accepted

To help you get started, here's a Matlab program that solves this system using Euler's method. You'll need to change the line where $f$ is defined. (This code is written to be clear, not to be as efficient as possible.)

%This code uses Euler's method to solve the system
% x'(t) = f(x(t),t).
% x(t) is a 2 x 1 column vector whose first component is
% x1(t) and whose second component is x2(t).
% The initial condition is x(0) = x0.

f = @(x,t) [0;0]; % DEFINE f CORRECTLY HERE.

x0 = [4/3 ; 2/3];

h = 1/1000;

t = 0:h:20;
numtVals = length(t);

x = zeros(2,numtVals);

x(:,1) = x0;

for i = 1:(numtVals - 1)

    ti = t(i);
    xi = x(:,i);

    xip1 = xi + h*f(xi,ti);
    x(:,i+1) = xip1;


figure('Name','x1 and x2')
hold on
hold off
share|cite|improve this answer
You can indent the code by 4 spaces to block the section as code. – Daryl Nov 13 '12 at 6:58
Thanks Daryl, I didn't know that! – littleO Nov 13 '12 at 7:09
How do i define x'(t)? – Sebastian Griotberg Nov 13 '12 at 14:49
I didn't get, @SebastianGriotberg you need Euler or RK4? – Kaster Nov 15 '12 at 1:11

Consider the vector $X=\binom {x_1}{x_2}$ such that $X'=\binom {x'_1}{x_2'}=\binom {9x_{1}+24x_{2}+5\cos t-\dfrac{1}{3}\sin t}{-24x_{1}-51x_{2}-9\cos t+\cfrac{1}{3}\sin t}=\phi(t, X)$.

Now consider two sequences $a_n\sim x_1$ and $b_n\sim x_2$ which implies that $X_n=\binom {a_n}{b_n}$

The $RK4$ method is given by $$\left \{\begin{array}{ll}K_1=\phi(t_n, X_n)\\ K_2=\phi(t_n+\cfrac h2,X_n+\cfrac h2 K_1)\\ K_3=\phi(t_n+\cfrac h2,X_n+\cfrac h2 K_2)\\K_4=\phi(t_n+h,X_n+hk_4)\\ X_{n+1}=X_n+\cfrac h6(K_1+2K_2+2K_3+K_4)\\ X_0=\binom {x_1(0)}{x_2(0)}\end{array}\right.$$

If you can turn this into a code on $MATLAB$ then you're good.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.