# 12. Runge-Kutta (RK4) numerical solution for Differential Equations

In the last section, Euler's Method gave us one possible approach for solving differential equations numerically.

The problem with Euler's Method is that you have to use a small interval size to get a reasonably accurate result. That is, it's not very efficient.

The **Runge-Kutta Method** produces a better result in fewer steps.

## Runge-Kutta Method Order 4 Formula

### Applications of RK4

#### Mechanics

- Springs and dampeners on cars (This spring applet uses RK4.)

#### Biology

- Predator-prey models
- Fisheries collapses
- Drug delivery
- Epidemic prediction

#### Physics

- Climate change models
- Ozone protection

#### Aviation

- On-board computers
- Aerodynamics

`y(x+h)` `=y(x)+` `1/6(F_1+2F_2+2F_3+F_4)`

where

`F_1=hf(x,y)`

`F_2=hf(x+h/2,y+F_1/2)`

`F_3=hf(x+h/2,y+F_2/2)`

`F_4=hf(x+h,y+F_3)`

### Where does this formula come from?

Here's a brief background to the formula.

Let's look at an example to see how it works.

## Example

Use Runge-Kutta Method of Order 4 to solve the following, using a step size of `h=0.1` for `0<=x<=1`.

`dy/dx=(5x^2-y)/e^(x+y)`

`y(0) = 1`

### Step 1

**Note:** The following looks tedious, and it is. We'll use a computer (not calculator) to do most of the work for us. The following is here so you can see how the formula is applied.

We start with `x=0` and `y=1`. We'll find the `F` values first:

`F_1=hf(x,y)` ` = 0.1(5(0)^2-1)/e^(0+1)` ` = -0.03678794411`

For `F_2`, we need to know:

`x+h/2 = 0+0.1/2 = 0.05`, and

`y+F_1/2` ` = 1 + (-0.03678794411)/2` ` = 0.98160602794`

We substitute these into the `F_2` expression:

`F_2=hf(x+h/2,y+F_1/2)` ` = 0.1((5(0.05)^2-0.98160602794)/e^(0.05+0.98160602794))` `=-0.03454223937`

For `F_3`, we need to know:

`y+F_2/2` ` = 1 + (-0.03454223937)/2` ` = 0.98272888031`

So

`F_3=hf(x+h/2,y+F_2/2)` ` = 0.1((5(0.05)^2-0.98272888031)/e^(0.05+0.98272888031))` `=-0.03454345267`

For `F_4`, we need to know:

`y+F_3` ` = 1 -0.03454345267` ` = 0.96545654732`

So

`F_4=hf(x+h,y+F_3)` ` = 0.1((5(0.1)^2-0.96545654732)/e^(0.1+0.96545654732))` `= -0.03154393258`

### Step 2

Next, we take those 4 values and substitute them into the Runge-Kutta RK4 formula:

`y(x+h)` `=y(x)+1/6(F_1+2F_2+2F_3+F_4)`

`=1+1/6( -0.03678794411` ` -\ 2xx0.03454223937` `-\ 2 xx0.03454345267` `{:-\ 0.03154393258)`

`=0.9655827899`

Using this new `y`-value, we would start again, finding the new `F_1`, `F_2`, `F_3` and `F_4`, and substitute into the Runge-Kutta formula.

We continue with this process, and construct the following table. (I used a spreadsheet to obtain the table. Using calculator is very tedious, and error-prone.)

The table is super wide, so I have put it in the following link:

Here is the graph of the solutions we found, from `x=0` to `x=1`.

For interest, I extended the result up to `x=10`, and here is the resulting graph:

## Exercise

Solve the following using RK4 (Runge-Kutta Method of Order 4) for `0<=x<=2`. Use a step size of `h=0.2`:

`dy/dx=(x+y)sin xy`

`y(0) = 5`

Here's the table of values we get. Once again, it's a very wide table so I've put it separately here:

Here is the graph of the solutions we found, from `x=0` to `x=2`.

For interest, I extended the result up to `x=6`, and here is the resulting graph:

We observe the graph is not very smooth. If we use a smaller `h` value, the curve will generally be smoother.

In fact, this curve is asymptotic to the `x`-axis (it smoothly gets closer to the axis as `x` gets larger). The above graph shows what happens when our intervals are too coarse. (Those wiggles after around `x>4.5` should not be there.)

Here is the solution graph again, but this time I've used `h=0.1`. It's better, but still has a "wiggle" near `x=5` that should not be there.

If I took even smaller values of `h`, I'd get a smoother curve. However, that's only up to a point, because rounding errors become significant eventually. Also, computing time goes up for little added benefit.

## Improvements to Runge-Kutta

As you can see from the above example, there are points in the curve where the `y` values change relatively quickly (between `0` and `1`) and other places where the curve is more nearly linear (from `1` to `2`). Also, we have strange behavior near `x=5`.

In practice, when writing a computer program to perform Runge-Kutta, we allow for variable `h` values - quite small when the curve is changing quickly, and larger `h` values when the curve is relatively smooth.

Mathematics computer algebra systems (like Mathcad, Mathematica and Maple) include routines that calculate RK4 for us, so we just need to provide the function and the interval of interest.

## Caution

Always be wary of your answers! Numerical solutions are only ever approximations, and under certain conditions, you can get chaotic results.

### Search IntMath, blog and Forum

### Online Algebra Solver

This algebra solver can solve a wide range of math problems.

Go to: Online algebra solver

### Calculus Lessons on DVD

Math videos by MathTutorDVD.com

Easy to understand calculus lessons on DVD. See samples before you commit.

More info: Calculus videos

### The IntMath Newsletter

Sign up for the free **IntMath Newsletter**. Get math study tips, information, news and updates each fortnight. Join thousands of satisfied students, teachers and parents!