One other method for solving Ordinary Differential Equations (ODEs)
is the Runge-Kutta method. This method is a family of iterative methods
that can be used to solve initial-value problems. The most common Runge-Kutta methods
are the 2nd and 4th order.
The way to solve these methods are very similar to the Euler Method,
but with more precision. The 2nd order Runge-Kutta method is also known
as the midpoint method, and the 4th order Runge-Kutta method is the most
commonly used.
The final result is yrk(2)=5.3053 . If you recall the Euler Method result is
ye(2)=4.8657 .
It’s clear that the Runge-Kutta Method is more precise than Euler’s method. That’s because
of its higher order of accuracy, which means it can provide a more accurate approximation of the solution
over a given interval. It is designed to minimize the local truncation error, which is
the error introduced at each step of the calculation. Also, higher order methods like this reduce the error
more rapidly as the step size decreases.
When we compare to the exact values, the approximations we found on the Runge Kutta method have a very small error:
As we can see, the exact solution is overshadowed by the Runge Kutta method, showing it’s
extremely small error.
Implementation
Below we have the implementation of 4th Order Runge Kutta method using Python.
import numpy as npdef runge_kutta(function, a, b, h, y0): t = np.linspace(a, b, int((b - a) / h) + 1) n = np.zeros(len(t)) n[0] = y0 for i in range(0, len(t) - 1): k1 = h * function(t[i], n[i]) k2 = h * function(t[i] + h / 2, n[i] + k1 / 2) k3 = h * function(t[i] + h / 2, n[i] + k2 / 2) k4 = h * function(t[i + 1], n[i] + k3) n[i + 1] = n[i] + (k1 + 2 * k2 + 2 * k3 + k4) / 6 return t, n
André Albano @onablaerdna
I'm a Geophysicist and Coder. I work with seismic inversion algorithms,
software development, well log analysis and so on.