System of two second order ODE's

  • Context: Graduate 
  • Thread starter Thread starter chenrim
  • Start date Start date
  • Tags Tags
    Second order System
Click For Summary

Discussion Overview

The discussion revolves around solving a system of two second-order ordinary differential equations (ODEs) using Matlab. Participants explore the conversion of these equations into a system of first-order ODEs and troubleshoot issues related to the implementation in Matlab, particularly with the ode45 function.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their approach to solving the system by reducing it to four first-order ODEs and implementing it in Matlab using the ode45 function.
  • Another participant points out a potential error in the order of vector elements in the function, suggesting that it could affect the adaptive step feature of ode45.
  • A participant expresses confusion about the function inputs and the dependencies among the equations, seeking clarification on how to correctly set the elements in the code.
  • One participant proposes numbering the functions to clarify the system of ODEs and suggests specific index swaps to correct the code.
  • Another participant reports that the suggested changes did not resolve their issue, indicating ongoing difficulties.
  • A later reply provides a specific correction to the code, suggesting a different arrangement of the elements in the xprime vector.
  • Finally, one participant confirms that the revised code works successfully for them.

Areas of Agreement / Disagreement

There is no consensus on the initial implementation, as participants express differing views on the source of the problem and propose various corrections. The discussion includes both successful and unsuccessful attempts to resolve the issue.

Contextual Notes

Participants' suggestions depend on the specific structure of the ODEs and the implementation details in Matlab, which may not be fully resolved in the discussion.

chenrim
Messages
14
Reaction score
0
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?
i will appreciate your help.
 

Attachments

  • eq1.JPG
    eq1.JPG
    3.6 KB · Views: 473
  • eq2.JPG
    eq2.JPG
    4.8 KB · Views: 538
Physics news on Phys.org
You've got the order of the vector elements wrong. Matlab assumes the function \vec y&#039; = \vec f(t, \vec y). In other words, <br /> \left[ \begin{array}{ccc}<br /> y_1&#039; \\<br /> y_2&#039; \\<br /> y_3&#039; \end{array} \right] =<br /> \left[ \begin{array}{ccc}<br /> f_1(t, \vec y) \\<br /> f_2(t, \vec y) \\<br /> f_3(t, \vec y) \end{array} \right], \vec y =<br /> \left[ \begin{array}{ccc}<br /> y_1 \\<br /> y_2 \\<br /> y_3 \end{array} \right]<br /> 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.
 
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.
thanks
 
It's easier to show if you number your functions. \vec y =<br /> \left[ \begin{array}{ccc}<br /> y_1 \\<br /> y_2 \\<br /> y_3 \\<br /> y_4 \end{array} \right] =<br /> \left[ \begin{array}{ccc}<br /> y \\<br /> y&#039; \\<br /> z \\<br /> z&#039; \end{array} \right]<br />
This gives you the following system of ODEs. \vec y&#039; = \vec f(t, \vec y) \iff<br /> \left[ \begin{array}{ccc}<br /> y_1&#039; \\<br /> y_2&#039; \\<br /> y_3&#039; \\<br /> y_4&#039; \end{array} \right] =<br /> \left[ \begin{array}{ccc}<br /> y_2 \\<br /> \frac{k(1 - y_2^2)}{1 + k y_1} \\<br /> y_4 \\<br /> \frac{k y_2 y_4}{1 + k y_1}\end{array} \right]<br />

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 :)
appreciate your help !
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
687
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 24 ·
Replies
24
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
0
Views
2K