Routines (alphabetical) > Routines: R > RK4

# RK4

The RK4 function uses the fourth-order Runge-Kutta method to advance a solution to a system of ordinary differential equations one time-step H, given values for the variables Y and their derivatives Dydx known at X.

RK4 is based on the routine `rk4` described in section 16.1 of Numerical Recipes in C: The Art of Scientific Computing (Second Edition), published by Cambridge University Press, and is used by permission.

## Examples

To integrate the example system of differential equations for one time step, H:

; Define the step size:
H = 0.5
; Define an initial X value:
X = 0.0
; Define initial Y values:
Y = [4.0, 6.0]
; Calculate the initial derivative values:
dydx = DIFFERENTIAL(X,Y)
; Integrate over the interval (0, 0.5):
result = RK4(Y, dydx, X, H, 'differential')
; Print the result:
PRINT, result

IDL prints:

3.11523 6.85767

This is the exact solution vector to five-decimal precision.

## Syntax

Result = RK4( Y, Dydx, X, H, Derivs [, /DOUBLE] )

## Return Value

Returns the integrations of the ordinary differential equations.

## Arguments

### Y

A vector of values for Y at X

Note: If RK4 is complex then only the real part is used for the computation.

### Dydx

A vector of derivatives for Y at X.

### X

A scalar value for the initial condition.

### H

A scalar value giving interval length or step size.

### Derivs

A scalar string specifying the name of a user-supplied IDL function that calculates the values of the derivatives Dydx at X. This function must accept two arguments: A scalar floating value X, and one n-element vector Y. It must return an n-element vector result.

For example, suppose the values of the derivatives are defined by the following relations:

dy0 / dx = –0.5y0, dy1 / dx = 4.0 – 0.3y1 – 0.1y0

We can write a function DIFFERENTIAL to express these relationships in the IDL language:

FUNCTION differential, X, Y

RETURN, [-0.5 * Y[0], 4.0 - 0.3 * Y[1] - 0.1 * Y[0]]

END

## Keywords

### DOUBLE

Set this keyword to force the computation to be done in double-precision arithmetic.

## Version History

4.0 | Introduced

## See Also

BROYDEN, NEWTON