Results 1 to 2 of 2

Math Help - MATLAB System of ODEs Runge Kutta Fourth order

  1. #1
    Junior Member
    Joined
    May 2010
    Posts
    74

    MATLAB System of ODEs Runge Kutta Fourth order

    I have to solve the ODE y'' + 4y' + 3y = 0 y(0) = 2 y'(0) = -4
    When tranformed into 2 first order ODE's it is dy1 = y2 and dy2 = -3y1 -4y2

    I have to write a Matlab code which solves this using the fourth order Runge Kutta scheme on the domain [0,1]

    This is what I have so far:

    Code:
    function [t,y] = System(f,n,h)
    M = [0,1;-3,-4];
    y(1, :) = [2, -4];
    f =@(t,y)(M*y')';
    t(1) = 0;
    
     
    for j = 1 : n
    k1 = h * f(t(j), y(j,:) );
    k2 = h * f( t(j) + h/2, y(j,:) + k1/2 );
    k3 = h * f( t(j) + h/2, y(j,:) + k2/2 );
    k4 = h * f( t(j) + h, y(j,:) + k3 );
    y(j+1,:) = y(j,:) + (k1 + 2*k2 + 2*k3 + k4) / 6;
    end
    I have tried so many different ways of writing this code and this one gives the least errors but does anyone have any idea where i can modify it so I don't get any errors and it solves properly?
    Last edited by CaptainBlack; October 6th 2010 at 12:17 AM.
    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 CookieC View Post
    I have to solve the ODE y'' + 4y' + 3y = 0 y(0) = 2 y'(0) = -4
    When tranformed into 2 first order ODE's it is dy1 = y2 and dy2 = -3y1 -4y2

    I have to write a Matlab code which solves this using the fourth order Runge Kutta scheme on the domain [0,1]

    This is what I have so far:

    Code:
    function [t,y] = System(f,n,h)
    M = [0,1;-3,-4];
    y(1, :) = [2, -4];
    f =@(t,y)(M*y')';
    t(1) = 0;
    
     
    for j = 1 : n
    k1 = h * f(t(j), y(j,:) );
    k2 = h * f( t(j) + h/2, y(j,:) + k1/2 );
    k3 = h * f( t(j) + h/2, y(j,:) + k2/2 );
    k4 = h * f( t(j) + h, y(j,:) + k3 );
    y(j+1,:) = y(j,:) + (k1 + 2*k2 + 2*k3 + k4) / 6;
    end
    I have tried so many different ways of writing this code and this one gives the least errors but does anyone have any idea where i can modify it so I don't get any errors and it solves properly?
    I haven't checked the answer but:

    Code:
    function [t,y] = System(n,h)
      M = [0,1;-3,-4];
      y(1, :) = [2, -4];
      f =@(t,y)(M*y')';
      t(1) = 0;
    
     
      for j = 1 : n
        k1 = h * f(t(j), y(j,:) );
        k2 = h * f( t(j) + h/2, y(j,:) + k1/2 );
        k3 = h * f( t(j) + h/2, y(j,:) + k2/2 );
        k4 = h * f( t(j) + h, y(j,:) + k3 );
        y(j+1,:) = y(j,:) + (k1 + 2*k2 + 2*k3 + k4) / 6;
        t(j+1)=t(j)+h;
      end
    runs

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Runge-Kutta method for a system, using MATLAB
    Posted in the Differential Equations Forum
    Replies: 1
    Last Post: December 10th 2011, 01:13 AM
  2. Replies: 0
    Last Post: July 20th 2011, 03:04 PM
  3. Fourth Order Runge-Kutta for two-simultaneous 1st ODE
    Posted in the Math Software Forum
    Replies: 1
    Last Post: November 2nd 2010, 03:43 PM
  4. Fourth-order Runge-Kutta for this equation.
    Posted in the Differential Equations Forum
    Replies: 1
    Last Post: October 18th 2009, 03:37 AM
  5. Fourth-Order Runge-Kutta Method
    Posted in the Differential Equations Forum
    Replies: 1
    Last Post: March 4th 2009, 05:32 AM

Search Tags


/mathhelpforum @mathhelpforum