# System of two second order ODE's

Hi ,
I have tried solving the following system of ODE's (eq1 attached) using Matlab.
first i reduced it into a system of four 1st order ODE's (eq2 attached).
thani tried to solve it using ode45() in the following manner:

function xprime = Mirage(t,x);
k=2;
xprime=[x(1); k*(1-x(1).^2)./(1+k*x(2)); x(3) ; -(k*x(1)*x(3))./(1+k*x(2))];
end

x0=[1 1 1 1];
tspan=[0,20];
[t,x]=ode45(@Mirage,tspan,x0)

the program seem to be in infinite loop(stays Busy).
where did i wrong?

#### Attachments

• 3.9 KB Views: 353
• 5 KB Views: 421

You've got the order of the vector elements wrong. Matlab assumes the function $\vec y' = \vec f(t, \vec y)$. In other words, $$\left[ \begin{array}{ccc} y_1' \\ y_2' \\ y_3' \end{array} \right] = \left[ \begin{array}{ccc} f_1(t, \vec y) \\ f_2(t, \vec y) \\ f_3(t, \vec y) \end{array} \right], \vec y = \left[ \begin{array}{ccc} y_1 \\ y_2 \\ y_3 \end{array} \right]$$ and so on. The slowdown in your case is due to xprime's current setup wrecking havoc on ode45's adaptive step feature i.e. it's making the time step smaller and smaller.

I think i didn't fully understand. In the Mirage function i set two inputs: the first one is the time interval for solving the problem.
the second will be a vector of initial conditions.
the line where " xprime=.." i must have mixed values of x(1) with x(2) and so on because the four equations are semi dependent.
how should the elements should be set? i'll be happy if you can post a fixed code.
thanks

It's easier to show if you number your functions. $$\vec y = \left[ \begin{array}{ccc} y_1 \\ y_2 \\ y_3 \\ y_4 \end{array} \right] = \left[ \begin{array}{ccc} y \\ y' \\ z \\ z' \end{array} \right]$$
This gives you the following system of ODEs. $$\vec y' = \vec f(t, \vec y) \iff \left[ \begin{array}{ccc} y_1' \\ y_2' \\ y_3' \\ y_4' \end{array} \right] = \left[ \begin{array}{ccc} y_2 \\ \frac{k(1 - y_2^2)}{1 + k y_1} \\ y_4 \\ \frac{k y_2 y_4}{1 + k y_1}\end{array} \right]$$

From this you can fix the code by swapping the indices 1 with 2 and 3 with 4.

I tried your seggestion and it's still doesn't seem to work out for me..
is it working for you?

Replace
Code:
xprime=[x(1); k*(1-x(1).^2)./(1+k*x(2)); x(3) ; -(k*x(1)*x(3))./(1+k*x(2))];
with
Code:
xprime=[x(2); k*(1-x(2).^2)./(1+k*x(1)); x(4) ; -(k*x(2)*x(4))./(1+k*x(1))];
.

The functions $y$ and $z$ are stored in
Code:
x(:,1)
and
Code:
x(:,3)
respectively after you've run the program. It should work.

It's working , Thank you very much :)