# Comparing the Euler, Midpoint and Runge-Kutta method

**Posted:**January 25, 2012

**Filed under:**numerical analysis Leave a comment

To find an approximate solution to the initial value problem

`y'(t) = f(t,y(t)), y(t_0) = y_o`

we compare three different methods: The Euler method, the Midpoint method and Runge-Kutta method. The accuracy of the solutions we obtain through the different methods depend on the given step size. Let always **e**,**m** and

denote the step sizes of Euler, Midpoint and Runge-Kutta method respectively.**r**

In the **Euler method** the value `y_n+1`

of `y`

at the point `t_n+1 = t_n + `

is is given by the first two of the taylor expansion of **e**`y`

at `t_n`

, that is

`y_n+1 = y_n + `

**e***f(t_n,y(t_n)).

In the **Midpoint method** we have `t_n+1 = t_n + `

and **m**

`y_n+1 = y_n + `

**m***f[t_n+**m**/2,y_n + **m**/2*f(t_n,y_n)].

Note, that here we have to eveluate the function `f`

*twice* to obtain our next value `y_n+1`

, whereas when using Euler method we only needed to do this *once*.

Lastly the **Runge-Kutta method** (of fourth order) is a further refinement of the methods above, where we now have `t_n+1 = t_n + `

and **r**

`y_n+1 = y_n + 1/6*(k_1 + 2*k_2 + 2*k_3 + k_4)`

where the calculation of the coefficients `k_1, k_2, k_3, k_4`

requires an independent evaluation of `f`

each.

Thus the Runge-Kutta needs *four* steps of calculation to get the next value `y_n+1`

.

When comparing the three methods, one should therefore choose the stepsizes accordingly, that is in such a way that

`4*`

**e** = 2***m** = **r**

holds.

A justified question is to ask wether the midpoint method yields better results than the Euler method with half the step size at all. I wrote a script to investigate that question. Here are some examples, for which I compared the three methods:

Consider `y'(t) = -2*t*y(t) `

with initial value `y(0) = 1`

. We easily verify that `y(t) = exp(-t^2)`

is an exact solution to the problem. Calculating the solutions with three different methods I got the diagram

Here the graphs show the **exact solution** and solutions obtained with the **Runge-Kutta method**, the **midpoint method** and the **Euler method**

The step sizes chosen are

, **r**=0.5

and **m**=0.25

, thus fullfilling our requirement at them for the methods to be comparable. **e** = 0.125

We see, that while the Euler method does yield the smoothest curve it yields the worst result. Here, the way in which the next value y_n+1 is determined is inaccurate compared to the other two methods and since y_n+2 depends on y_n+1, this error progresses in the calculation to eventually yield only a very vague approximation.

Here is another example:

To solve `y'(t) = y(t)`

, which of course has the **exact solution** `y(t) = exp(t)`

, the following step sizes where chosen:

, **r**=1

and **m**=0.5**e** = 0.25

What I wanted to show are two examples in which the Runge-Kutta method yields better results than the Midpoint and Euler method, although for those step sizes are chosen accordingly smaller to have a comparable effort in computation.

In no way do I want to make a general statement however – this all was done for out of pure interest and came up in the Python course I took almost a year ago. You can download the Python script I used and try to integrate functions yourself. Be adivsed though: The script was written within the first month of me learning Python (which also my first programming language) so it’s not a beauty.