Results 1 to 6 of 6

Math Help - MATLAB ode 45 help

  1. #1
    Newbie
    Joined
    Apr 2010
    Posts
    3

    MATLAB ode 45 help

    I have been given two second order ode's:

    d^2x/dt^2 = dx/dt * sqrt( (dx/dt)^2 + (dy/dt)^2 )
    d^2y/dt^2 = dy/dt * sqrt( (dx/dt)^2 + (dy/dt)^2 )

    with initial conditions x(0)=0; y(0)=0; dx/dt(t=0) = 134; dy/dt(t=0) = 0

    Now, my homework problem asks me to reduce the two second-order ode's to a system of first order ode's then write a script to solve the trajectory (i.e. x and y) for the first 5 seconds using the Matlab built-in function ode45.

    To reduce the two 2nd order odes i set for the first ode x=x1 and dx/dt=x2 and for second ode y=y1 and dy/dt=y2 to get the system of first order ode's:

    dx/dt = x2 ; x1(0) = 0
    dy/dt = y2 ; y1(0) = 0
    dx2/dt = x2 sqrt( (x2)^2 + (y2)^2 ) ; x2(0)= 134
    dy2/dt = y2 sqrt( (x2)^2 + (y2)^2 ); y2(0)= 0

    I have no idea however how to set this up in matlab using the ode45 function. Could anyone help please?
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Troktr View Post
    I have been given two second order ode's:

    d^2x/dt^2 = dx/dt * sqrt( (dx/dt)^2 + (dy/dt)^2 )
    d^2y/dt^2 = dy/dt * sqrt( (dx/dt)^2 + (dy/dt)^2 )

    with initial conditions x(0)=0; y(0)=0; dx/dt(t=0) = 134; dy/dt(t=0) = 0

    Now, my homework problem asks me to reduce the two second-order ode's to a system of first order ode's then write a script to solve the trajectory (i.e. x and y) for the first 5 seconds using the Matlab built-in function ode45.

    To reduce the two 2nd order odes i set for the first ode x=x1 and dx/dt=x2 and for second ode y=y1 and dy/dt=y2 to get the system of first order ode's:

    dx/dt = x2 ; x1(0) = 0
    dy/dt = y2 ; y1(0) = 0
    dx2/dt = x2 sqrt( (x2)^2 + (y2)^2 ) ; x2(0)= 134
    dy2/dt = y2 sqrt( (x2)^2 + (y2)^2 ); y2(0)= 0

    I have no idea however how to set this up in matlab using the ode45 function. Could anyone help please?
    You have a state vector:

    \bold{x}=\left[\begin{array}{c}x\\y\\x'\\y'\end{array} \right]

    then:

    \bold{x}'=\left[\begin{array}{c}x'\\y'\\x''\\y''\end{array} \right]= <br />
\left[\begin{array}{c}\bold{x}_3 \\ \bold{x}_4 \\ \bold{x}_3\sqrt{\bold{x}_3^2+\bold{x}_4^2}\\ \bold{x}_4\sqrt{\bold{x}_3^2+\bold{x}_4^2}\end{arr  ay} \right]

    Now you write a derivative function to calculate this last with arguments as required by ode45.

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Apr 2010
    Posts
    3
    I ended up with this:

    function dxy = trajxy(t, xy)
    j = 5.38; m = 80; g = 9.81;
    x = xy(1);
    xdot = xy(2);
    y = xy(3);
    ydot = xy(4);
    xdot = xdot;
    ydot = ydot;
    xdoubledot = -(j/m)*xdot*sqrt((xdot)^2+(ydot)^2);
    ydoubledot = -g - (j/m)*ydot*sqrt((xdot)^2 + (ydot)^2);
    dxy = [xdot; xdoubledot; ydot; ydoubledot];
    [t xy] = ode45('trajxy',0,5,[0 134 0 0])

    I get the error:

    ??? Input argument "xy" is undefined.

    Error in ==> trajxy at 3
    x = xy(1);

    Although it still outputs a matrix. The values seem to be off though.
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Troktr View Post
    I ended up with this:

    function dxy = trajxy(t, xy)
    j = 5.38; m = 80; g = 9.81;
    x = xy(1);
    xdot = xy(2);
    y = xy(3);
    ydot = xy(4);
    xdot = xdot;
    ydot = ydot;
    xdoubledot = -(j/m)*xdot*sqrt((xdot)^2+(ydot)^2);
    ydoubledot = -g - (j/m)*ydot*sqrt((xdot)^2 + (ydot)^2);
    dxy = [xdot; xdoubledot; ydot; ydoubledot];
    [t xy] = ode45('trajxy',0,5,[0 134 0 0])

    I get the error:

    ??? Input argument "xy" is undefined.

    Error in ==> trajxy at 3
    x = xy(1);

    Although it still outputs a matrix. The values seem to be off though.
    [t xy] = ode45('trajxy',0,5,[0 134 0 0])

    should be types at the command prompt, maybe it is but here it looks like its in the derivative file.

    CB
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Newbie
    Joined
    Apr 2010
    Posts
    3
    I just copied [t xy] = ode45('trajxy',0,5,[0 134 0 0]) in there right after it but yeah it isn't in the derivative file. After i run the derivative file i get the error. Then when i put [t xy] = ode45('trajxy',0,5,[0 134 0 0]) in the command window it still works but I dont seem to get right values.
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Troktr View Post
    I just copied [t xy] = ode45('trajxy',0,5,[0 134 0 0]) in there right after it but yeah it isn't in the derivative file. After i run the derivative file i get the error. Then when i put [t xy] = ode45('trajxy',0,5,[0 134 0 0]) in the command window it still works but I dont seem to get right values.
    Try making the initial state a column vector (I don't think this matters but we might as well be consistent).

    You also might be better off using a function handle rather than the function name in quotes (again I don't think this will make any difference IIRC quoted function names still work):

    [t, xy] = ode45(@trajxy,0,5,[0;134;0;0])

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Matlab help
    Posted in the Math Software Forum
    Replies: 1
    Last Post: February 11th 2010, 03:59 AM
  2. Matlab FFT Help
    Posted in the Math Software Forum
    Replies: 0
    Last Post: June 3rd 2009, 01:48 PM
  3. help about ODE in matlab
    Posted in the Math Software Forum
    Replies: 1
    Last Post: May 31st 2009, 04:10 AM
  4. Matlab
    Posted in the Math Software Forum
    Replies: 0
    Last Post: November 6th 2008, 09:46 AM
  5. Matlab
    Posted in the Calculus Forum
    Replies: 3
    Last Post: August 20th 2008, 11:39 PM

Search Tags


/mathhelpforum @mathhelpforum