Ordinary Differential Equations

Download Runge-Kutta-Fehlberg codes (single and double precision). This code is unsuitable for stiff problems.
Download Adams-Bashforth code for stiff problems.

4th Order Runge-Kutta

  • We wish to solve the differential equation



  • In a marching process, we calculate the solution at subsequent stations by using the equations



    where, the step size



Pseudo-code for Runge-Kutta for a System of Equations

  • We wish to solve the system of differential equations



  • The pseudo-code is:

    x - scalar; y, k1, k2, k3, k4, slope are vectors; n number of equations; h is step size.

    On exit, both x and y are updated for the next station in marching.

    SUB RK4 ( x , y , n , h )
    CALL Derivs ( x , y , k1 )
    DO i = 1 , n
         ym ( i ) = y ( i ) + k1 ( i ) * h / 2
    END DO
    xm = x + h / 2
    CALL Derivs ( xm , ym , k2 )
    DO i = 1 , n
         ym ( i ) = y ( i ) + k2 ( i ) * h / 2
    END DO
    CALL Derivs ( xm , ym , k3 )
    DO i = 1 , n
         ym ( i ) = y ( i ) + k3 ( i ) * h
    END DO
    xm = x + h
    CALL Derivs ( xm , ym , k4 )
    DO i = 1 , n
         slope ( i ) = ( k1 ( i ) + 2 * ( k2 ( i) + k3 ( i ) ) + k4 ( i ) ) / 6
         y ( i ) = y ( i ) + slope ( i ) * h
    END DO
    x = xm
    END


    SUB Derivs ( x , y , f )
    f ( 1 ) = function-1 ( x , y )
    f ( 2 ) = function-2 ( x , y )
    ...
    f ( n ) = function-n ( x , y )
    END



Excel Sheet for demonstrating Runge-Kutta Calculations


SIMULINK Models for ODE

Problem #1

Problem #2

Runge-Kutta-Fehlberg Variable-Step Method (RKF45)

Begin with the step size



Calculate



By using



Update step-size using following algorithm