Results 1 to 3 of 3

Math Help - Runge kutta matlab code (method)

  1. #1
    Banned
    Joined
    Nov 2010
    Posts
    8

    Smile Runge kutta matlab code (method)

    Hi guys,

    I am having trouble implementing Runge Kutta to solve the following
    Lotka-Volterra equations.

    dx/dt=x(1-2y) t0=0, x(t0)=1
    dy/dt=-y(1-2x) t0=0, x(t0)=2

    Below I given what code I have done so far but it only produces an axis with a dot.

    My first file has the code below:

    function z_out=lotka_volterra_rhs(z)
    %create a vector for storing the results;
    x=zeros(1,2);
    %z(1) is supposed to store the values of x
    %z(2) is supposed to store the values of y
    x(1)=z(1)*(1-2*z(2));
    x(2)=-z(2)*(1-2*z(1));
    z_out=x;

    my second file has the code below:

    t0=0;
    t1=40;
    Delta=0.005;
    t=t0: Delta:t1;
    i=1;
    y(i)=2;
    x(i)=1;
    while (x(i)<x)

    z_1=lotka_volterra_rhs(y(i),x(i));
    z_1x=lotka_volterra_rhs(y(i),x(i));
    z_2x=lotka_volterra_rhs((y(i)+Delta*(z_1/(2))),(x(i)+Delta*(z_1x/(2))));
    z_2=lotka_volterra_rhs((y(i)+Delta*(z_1/(2))),(x(i)+Delta*(z_1x/(2))));
    z_3x=lotka_volterra_rhs((y(i)+Delta*(z_2/(2))),(x(i)+Delta*(z_2x/(2))));
    z_3=lotka_volterra_rhs((y(i)+Delta*(z_2/(2))),(x(i)+Delta*(z_2x/(2))));
    z_4x=lotka_volterra_rhs((y(i)+Delta*z_3),(x(i)+Del ta*z_3x));
    z_4=lotka_volterra_rhs((y(i)+Delta*z_3),(x(i)+Delt a*z_3x));
    y(i+1)=(y(i)+Delta*((z_1+2*z_2+2*z_3+z_4)/(6)));
    x(i+1)=(x(i)+Delta*((z_1x+2*z_2x+2*z_3x+z_4x)/(6)));
    i=i+1;


    end;
    plot(x,y)

    Thank you guys... really appreciate it
    Last edited by Chris L T521; January 12th 2011 at 04:27 PM. Reason: Restored Original Post.
    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 MathsLion View Post
    Hi guys,

    I am having trouble implementing Runge Kutta to solve the following
    Lotka-Volterra equations.

    dx/dt=x(1-2y) t0=0, x(t0)=1
    dy/dt=-y(1-2x) t0=0, x(t0)=2

    Below I given what code I have done so far but it only produces an axis with a dot.

    My first file has the code below:

    function z_out=lotka_volterra_rhs(z)
    %create a vector for storing the results;
    x=zeros(1,2);
    %z(1) is supposed to store the values of x
    %z(2) is supposed to store the values of y
    x(1)=z(1)*(1-2*z(2));
    x(2)=-z(2)*(1-2*z(1));
    z_out=x;

    my second file has the code below:

    t0=0;
    t1=40;
    Delta=0.005;
    t=t0: Delta:t1;
    i=1;
    y(i)=2;
    x(i)=1;
    while (x(i)<x)

    z_1=lotka_volterra_rhs(y(i),x(i));
    z_1x=lotka_volterra_rhs(y(i),x(i));
    z_2x=lotka_volterra_rhs((y(i)+Delta*(z_1/(2))),(x(i)+Delta*(z_1x/(2))));
    z_2=lotka_volterra_rhs((y(i)+Delta*(z_1/(2))),(x(i)+Delta*(z_1x/(2))));
    z_3x=lotka_volterra_rhs((y(i)+Delta*(z_2/(2))),(x(i)+Delta*(z_2x/(2))));
    z_3=lotka_volterra_rhs((y(i)+Delta*(z_2/(2))),(x(i)+Delta*(z_2x/(2))));
    z_4x=lotka_volterra_rhs((y(i)+Delta*z_3),(x(i)+Del ta*z_3x));
    z_4=lotka_volterra_rhs((y(i)+Delta*z_3),(x(i)+Delt a*z_3x));
    y(i+1)=(y(i)+Delta*((z_1+2*z_2+2*z_3+z_4)/(6)));
    x(i+1)=(x(i)+Delta*((z_1x+2*z_2x+2*z_3x+z_4x)/(6)));
    i=i+1;


    end;
    plot(x,y)

    Thank you guys... really appreciate it
    In your while statement what exactly do you think (x(i)<x) does/means?

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Banned
    Joined
    Nov 2010
    Posts
    8
    this is wrong
    Last edited by MathsLion; November 26th 2010 at 01:24 AM.
    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. MATLAB System of ODEs Runge Kutta Fourth order
    Posted in the Differential Equations Forum
    Replies: 1
    Last Post: October 6th 2010, 12:33 AM
  3. Runge-Kutta method
    Posted in the Calculus Forum
    Replies: 0
    Last Post: April 23rd 2010, 02:38 AM
  4. Runge Kutta method
    Posted in the Differential Equations Forum
    Replies: 1
    Last Post: February 26th 2010, 01:54 PM
  5. MATLAB and 4th order Runge Kutta Method
    Posted in the Math Software Forum
    Replies: 0
    Last Post: April 1st 2009, 10:07 AM

Search Tags


/mathhelpforum @mathhelpforum