Results 1 to 2 of 2

Math Help - Runge-Kutta method for a system, using MATLAB

  1. #1
    MHF Contributor alexmahone's Avatar
    Joined
    Oct 2008
    Posts
    1,074
    Thanks
    7

    Runge-Kutta method for a system, using MATLAB

    An initial value problem and its exact solution are given. Use the Runge-Kutta method with step sizes h=0.1 and h=0.05 to approximate the values x(1) and y(1). Compare the approximations with the actual values.

    x'=-x-y-(1+t^3)e^{-t}, x(0)=0,

    y'=-x-y-(t-3t^2)e^{-t}, y(0)=1;

    x(t)=e^{-t}(\sin t-t), y(t)=e^{-t}(\cos t+t^3)

    Code:
    A=[-1 -1;-1 -1];
    f=inline('-exp(-t)*[1+t^3;t-3*t^2]');
    t=0;
    t1=1;
    x=[0;1];
    h=0.1;
    n=(t1-t)/h;
    for i=1:n
        k1=A*x+f(t);
        k2=A*(x+h*k1/2)+f(t+1/2*h);
        k3=A*(x+h*k2/2)+f(t+1/2*h);
        k4=A*(x+h*k3)+f(t+h);
        k=(k1+2*k2+2*k3+k4)/6;
        t=t+h;
        x=x+h*k;
    end
    h
    x
    
    t=0;
    x=[0;1];
    h=0.05;
    n=(t1-t)/h;
    for i=1:n
        k1=A*x+f(t);
        k2=A*(x+h*k1/2)+f(t+1/2*h);
        k3=A*(x+h*k2/2)+f(t+1/2*h);
        k4=A*(x+h*k3)+f(t+h);
        k=(k1+2*k2+2*k3+k4)/6;
        t=t+h;
        x=x+h*k;
    end
    h
    x
    
    disp('Actual values:')
    x=[exp(-1)*(sin(1)-1);exp(-1)*(cos(1)+1)]
    The output is:

    Code:
    h =
    
        0.1000
    
    
    x =
    
       -0.9904
        0.9732
    
    
    h =
    
        0.0500
    
    
    x =
    
       -0.9904
        0.9732
    
    Actual values:
    
    x =
    
       -0.0583
        0.5666
    My Runge-Kutta approximations don't tally with the actual values. Where have I gone wrong?
    Last edited by alexmahone; December 10th 2011 at 12:53 AM.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor alexmahone's Avatar
    Joined
    Oct 2008
    Posts
    1,074
    Thanks
    7

    Re: Runge-Kutta method for a system, using MATLAB

    There's probably a mistake in the question: the 1st DE should be x'=-x+y-(1+t^3)e^{-t}. After making that change, I get the correct answer.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Runge kutta matlab code (method)
    Posted in the Differential Equations Forum
    Replies: 2
    Last Post: November 25th 2010, 10:19 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