This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the content is also available at Berkeley Python Numerical Methods.
The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!
< 22.4 Numerical Error and Instability | Contents | 22.6 Python ODE Solvers >
Predictor-Corrector and Runge Kutta Methods¶
Predictor-Corrector Methods¶
Given any time and state value, the function, \(F(t, S(t))\), returns the change of state \(\frac{dS(t)}{dt}\). Predictor-corrector methods of solving initial value problems improve the approximation accuracy of non-predictor-corrector methods by querying the \(F\) function several times at different locations (predictions), and then using a weighted average of the results (corrections) to update the state. Essentially, it uses two formulas: the predictor and corrector. The predictor is an explicit formula and first estimates the solution at \(t_{j+1}\), i.e. we can use Euler method or some other methods to finish this step. After we obtain the solution \(S(t_{j+1})\), we can apply the corrector to improve the accuracy. Using the found \(S(t_{j+1})\) on the right-hand side of an otherwise implicit formula, the corrector can calculate a new, more accurate solution.
The midpoint method has a predictor step:
which is the prediction of the solution value halfway between \(t_j\) and \(t_{j+1}\).
It then computes the corrector step:
which computes the solution at \(S(t_{j+1})\) from \(S(t_j)\) but using the derivative from \(S\left(t_{j} + \frac{h}{2}\right)\).
Runge Kutta Methods¶
Runge Kutta (RK) methods are one of the most widely used methods for solving ODEs. Recall that the Euler method uses the first two terms in Taylor series to approximate the numerical integration, which is linear: \(S(t_{j+1}) = S(t_j + h) = S(t_j) + h \cdot S'(t_j)\).
We can greatly improve the accuracy of numerical integration if we keep more terms of the series in
In order to get this more accurate solution, we need to derive the expressions of \(S''(t_j), S'''(t_j), \cdots, S^{(n)}(t_j)\). This extra work can be avoided using the RK methods, which is based on truncated Taylor series, but not require computation of these higher derivatives.
Second order Runge Kutta method¶
Let us first derive the second order RK method. Let \(\frac{dS(t)}{dt} = F(t,S(t))\), then we can assume an integration formula the form of
We can attempt to find these parameters \(c_1, c_2, p, q\) by matching the above equation to the second-order Taylor series, which gives us
Noting that $\(F'(t, s(t)) = \frac{\partial F}{\partial t} + \frac{\partial F}{\partial S}\frac{\partial S}{\partial t} = \frac{\partial F}{\partial t} + \frac{\partial F}{\partial S}F\)$
Therefore, equation \(\eqref{eq:matching}\) can be written as:
In equation \(\eqref{eq:integration}\), we can rewrite the last term by applying Taylor series in several variables, which gives us:
thus equation \(\eqref{eq:integration}\) becomes:
Comparing equation \(\eqref{eq:rewrite-matching}\) and \(\eqref{eq:rewrite-integration}\), we can easily obtain:
Because \(\eqref{eq:result}\) has four unknowns and only three equations, we can assign any value to one of the parameters and get the rest of the parameters. One popular choice is:
We can also define: $$
Fourth-order Runge Kutta method¶
A classical method for integrating ODEs with a high order of accuracy is the Fourth Order Runge Kutta (RK4) method. It is obtained from the Taylor series using similar approach we just discussed in the second-order method. This method uses four points \(k_1, k_2, k_3\), and \(k_4\). A weighted average of these is used to produce the approximation of the solution. The formula is as follows.
Therefore, we will have:
As indicated by its name, the RK4 method is fourth-order accurate, or \(O(h^4)\).
< 22.4 Numerical Error and Instability | Contents | 22.6 Python ODE Solvers >