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
r denote the step sizes of Euler, Midpoint and Runge-Kutta method respectively.
In the Euler method the value
y at the point
t_n+1 = t_n + e is is given by the first two of the taylor expansion of
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 + m and
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 + r and
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
Thus the Runge-Kutta needs four steps of calculation to get the next value
When comparing the three methods, one should therefore choose the stepsizes accordingly, that is in such a way that
4*e = 2*m = r
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:
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
e = 0.125, thus fullfilling our requirement at them for the methods to be comparable.
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:
y'(t) = y(t), which of course has the exact solution
y(t) = exp(t), the following step sizes where chosen:
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.