Results 1 to 5 of 5

Math Help - MATLAB

  1. #1
    Member Maccaman's Avatar
    Joined
    Sep 2008
    Posts
    85

    MATLAB

    Hello,

    I need to solve a second order differential equation in Matlab.....which sucks because I HATE using Matlab.

    Anyway, I cannot use the built-in matlab functions ode45, ode23s, ect, I have to employ the use of the fourth order Runge Kutta method......which I can do for a first order differential equation, but I cant think of how to use it for a 2nd order DE. I think that I first have to transform the 2nd order DE into a system of 1st order DE's.

    Here is my matlab code for an IVP (1st order) using fourth order Runge Kutta method.



    function y = rk4step(f,t,x,h)
    % f - Matlab inline function f(t,y)
    % t - initial time
    % x - initial condition
    % h - stepsize

    h2 = h / 2;
    k1 = f(t, x);
    k2 = f(t + h2, x + h2 * k1);
    k3 = f(t + h2, x + h2 * k2);
    k4 = f(t + h, x + h * k3);
    y = x + (k1 + 2 * k2 + 2 * k3 + k4) * h / 6

    Should I transform the system of first order DE's into a vector and go from there?
    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 Maccaman View Post
    Hello,

    I need to solve a second order differential equation in Matlab.....which sucks because I HATE using Matlab.

    Anyway, I cannot use the built-in matlab functions ode45, ode23s, ect, I have to employ the use of the fourth order Runge Kutta method......which I can do for a first order differential equation, but I cant think of how to use it for a 2nd order DE. I think that I first have to transform the 2nd order DE into a system of 1st order DE's.

    Here is my matlab code for an IVP (1st order) using fourth order Runge Kutta method.



    function y = rk4step(f,t,x,h)
    % f - Matlab inline function f(t,y)
    % t - initial time
    % x - initial condition
    % h - stepsize

    h2 = h / 2;
    k1 = f(t, x);
    k2 = f(t + h2, x + h2 * k1);
    k3 = f(t + h2, x + h2 * k2);
    k4 = f(t + h, x + h * k3);
    y = x + (k1 + 2 * k2 + 2 * k3 + k4) * h / 6

    Should I transform the system of first order DE's into a vector and go from there?
    You convert your 2-nd order ODE into a first order system. So if you have an ODE for x(t) , introduce the state vector:

    X(t)= {x(t) \brack x'(t)}

    Then:

    X'(t)={X_2(t) \brack f(t,x(t),x'(t))}

    where the ODE has been rewritten in the form:

    x''(t)=f(t,x(t),x'(t))

    RonL
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Member Maccaman's Avatar
    Joined
    Sep 2008
    Posts
    85
    So if my 2nd order ode is

     x'' = \mu (1-x^2)x' -x

    then how is that in MATLAB?

    Is it something like

    function z = xdot (t,x);
    mu = 0.1;
    z(1) = x(2);
    z(2) = x(3);
    z(3) = mu*(1-z(1)^2)*z(2) - z(1);
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Member Maccaman's Avatar
    Joined
    Sep 2008
    Posts
    85
    I may as well just post this question and see if anyone can help. Im just so tired the information im getting for this question is just going in one eyeball and out the other.

    The equation above is the Van de Pol equation. I need to have mu = 0.1 and use the fourth order runge kutta method with a step size of h=0.1 in the interval 0 < t< 200 using the initial conditions x(0) = 1, and x'(0) =0.
    Then the initial conditions x(0) = 5, x'(0) = 0.
    Then i have to plot x(t) vs. time as well as the trajectories in the phase space (x(t), x'(t) of all solns. (which by the way i am unsure of what is meant by phase space).

    for the Van de pol eqn., here is my mfile in matlab

    function z = xdot (t,x);
    mu = 0.1;

    z(1) = x(2);
    z(2) = x(3);
    z(3) = mu*(1-z(1)^2)*z(2) - z(1);


    Here is my mfile for the 4th order runge kutta

    function[t,y] = runkut4(t,x,h)
    s1=xdot(t,x);
    s2=xdot(t+h/2,x+h*s1/2);
    s3=xdot(t+h/2,x+h*s2/2);
    s4=xdot(t+h,x+h*s3);
    x=x+h*(s1+2*s2+2*s3+s4)/6;

    I dont know how to go on from here.


    Follow Math Help Forum on Facebook and Google+

  5. #5
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by Maccaman View Post
    So if my 2nd order ode is

     x'' = \mu (1-x^2)x' -x

    then how is that in MATLAB?

    Is it something like



    function z = xdot (t,x);
    mu = 0.1;
    z(1) = x(2);
    z(2) = x(3);
    z(3) = mu*(1-z(1)^2)*z(2) - z(1);
    A second order ODE converts to a first order ODE with a 2-D state, so no.

    X={x \brack x'}

    so for your ODE:

     <br />
X'={x' \brack x''}={X_2 \brack \mu(1-X_1^2)X_2 - X_1}<br />

    So:

    Code:
    function z = xdot (t,x);
    mu = 0.1;
    z(1) = x(2);
    z(2) = mu*(1-x(1)^2)*x(2) - x(1);
    RonL
    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 Please help me
    Posted in the Math Software Forum
    Replies: 8
    Last Post: November 10th 2008, 05:40 PM
  5. Matlab
    Posted in the Calculus Forum
    Replies: 3
    Last Post: August 20th 2008, 11:39 PM

Search Tags


/mathhelpforum @mathhelpforum