1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Matlab: Solving two second order differential equations

  1. Dec 17, 2008 #1
    1. The problem statement, all variables and given/known data
    Write a function and solver for the chaotic motion of stars in a galaxy

    Initial conditions:

    x at time zero = 0
    velocity of x at time zero = 0.3
    y at time zero = 0
    velocity of y at time zero = 0

    Time period 2 to 200 with an interval od 0.02

    2. Relevant equations

    This gives rise to the equations:

    d^2x/dt^2= -x-2xy;

    d^2y/dt^2= -y-x^2+y^2.

    And this gives rise to the four first-order equations

    dx/dt=velocity of x

    dvelocity of y/dt = -x -2xy

    dy/dt = velocity of y

    dvelocity of y/dt = -y=x^2+y^2

    3. The attempt at a solution

    This is my first time learning matlab and all the examples we were shown until now dealt with first-ODEs. I've made lots and lots of attempts at the solution, all coming up with all kinds of errors like undefined variables on Matlab. Basically my problem is that I can't figure out how to structure the function and solver for so many different terms.

    One hopeless attempt was:

    velocity of x to be Vx
    x to be x
    velocity of y to be Vy
    y to be y

    Is it supposed to look like:

    function q=func(t,x,y)
    q=[Vx; -x-2*x*y; Vy; -x^2+y^2]

    And the solver is


    I'm so confused on so many points. I don't think [x,y]=ode45(...) is right, but I don't know if it should be [t,x], [t.y] or whatever. Likewise, I know I'm meant to plot (x,y) for different time intervals, so do write plot(t,x,y)? I don't even know what [] is supposed to be for, only that it's in the notes and all the other examples I can find.

    I tried so many different variations, tried to look it up in books and on the web for 7 hours straight yesterday and 2 more today and I'm feeling quite miserable about matlab. Our notes aren't helpful at all, using different variations of names for x and y for different problems which was so confusing. I'd be so appreciative for any general help on how to structure function and solver, or even direct me to a book or website that deals with two second-ODEs.

    Is it just meant to look like:

    function name = another function name(time, variable 1, variable 2)
    name=[RHS equation 1;RHS equation 2;RHS equation 3;RHS equation 4];

    And the solver:

    initial conditions 1 = [x];
    initial conditions 2 = [y];
    initial conditions 3 = [z];
    constant = [a];
    t = [...;...;...]
    [?]=ode45(@function name,t, initial conditions 1, initial conditions2, initial conditions 3, [], constant);
    plot (?)

    Many thanks
    Last edited: Dec 17, 2008
  2. jcsd
  3. Dec 18, 2008 #2
    Never mind, figured out where I went wrong.

    Learning physics by trial and error...hmrph.
  4. Dec 19, 2008 #3
    thats pretty much how you learn matlab =\
  5. Dec 19, 2008 #4
    Feels like how I've learned the whole of my classical mechanics module - it's not very efficient!
  6. May 17, 2011 #5
    I'm also trying to solve two second ODE's for a different physical quantity. Could you please let me know the right ode structure.
    Thanks a lot in advance.

    Many thanks,
  7. May 18, 2011 #6
    Got it...
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook