Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

System of two second order ODE's

  1. Jan 4, 2015 #1
    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);
    xprime=[x(1); k*(1-x(1).^2)./(1+k*x(2)); x(3) ; -(k*x(1)*x(3))./(1+k*x(2))];

    x0=[1 1 1 1];

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

    Attached Files:

    • eq1.JPG
      File size:
      3.9 KB
    • eq2.JPG
      File size:
      5 KB
  2. jcsd
  3. Jan 9, 2015 #2
    You've got the order of the vector elements wrong. Matlab assumes the function [itex]\vec y' = \vec f(t, \vec y)[/itex]. In other words, [tex]
    \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]
    [/tex] 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.
  4. Jan 9, 2015 #3
    Hi da_nang thanks for your answer.
    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.
  5. Jan 9, 2015 #4
    It's easier to show if you number your functions. [tex]\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. [tex] \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.
  6. Jan 9, 2015 #5
    I tried your seggestion and it's still doesn't seem to work out for me..
    is it working for you?
  7. Jan 10, 2015 #6
    Code (Text):
    xprime=[x(1); k*(1-x(1).^2)./(1+k*x(2)); x(3) ; -(k*x(1)*x(3))./(1+k*x(2))];
    Code (Text):
    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 [itex]y[/itex] and [itex]z[/itex] are stored in
    Code (Text):
    Code (Text):
    respectively after you've run the program. It should work.
  8. Jan 11, 2015 #7
    It's working , Thank you very much :)
    appreciate your help !
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook