This repository contains an algorithm for solving initial value problems (IVPs) using any explicit Runge—Kutta method of any order and for systems of arbitrary dimensionality. Unlike conventional implementations, this approach allows for a more compact and flexible code structure by eliminating the need to manually derive and write out lengthy expressions for each specific IVP. Instead, the user simply provides the Butcher tableau corresponding to the desired Runge—Kutta method, making the implementation both general and efficient.
- Explicit Runge—Kutta methods. Butcher tableau. Stability Function & Stability Region
- Description of the implemented algorithm
- Example
- Notes
- References
Let an initial value problem be specified as follows:
where
The
where
Method coefficients are conveniently set in the form of a Butcher tableau:
In the program implementation other elements of the matrix
In the context of stability analysis of explicit Runge—Kutta methods, the stability region is defined as
is stability function. For explicit Runge—Kutta methods in which the number of stages equals the order (i.e.
The bounds of stability regions for such methods are presented below:
For the method with
and the stability region is presented below:
Of course, you can implement the algorithm described in the previous section as well, and it will work the same way as the algorithm I will describe below.
So, the algorithm is based on the application of general matrix algebra:
To begin with, at each (also take a look here) iteration we need to initialize the matrix
and the matrix
Then the formulas for filling the matrix
The ExampleOfUse.mlx file shows the obtaining of the Tamari attractor
with initial conditions
using the 6th order Runge-Kutta-Butcher method.
[t, xsol] = odeExplicitGeneral(c_vector, A_matrix, b_vector, odefun, tspan, tau, incond)
-
c_vector
: vector of coefficients$\mathbf{c}$ of Butcher tableau for the selected method; -
A_matrix
: matrix of coefficients$\mathbf{A}$ of Butcher tableau for the selected method; -
b_vector
: vector of coefficients$\mathbf{b}$ of Butcher tableau for the selected method; -
odefun
: functions to solve, specified as a function handle that defines the functions to be integrated; -
tspan
: interval of integration, specified as a two-element vector; -
tau
: time discretization step; -
incond
: vector of initial conditions.
t
: vector of evaluation points used to perform the integration;xsol
: solution matrix in which each row corresponds to a solution at the value returned in the corresponding row oft
.
The code from the odeExplicitGeneral.m script shows a more illustrative integration procedure, for understanding from a theoretical point of view. The optimized version of this script odeExplicitGeneral_optimized.m looks as follows:
function [t, xsol] = odeExplicitGeneral_optimized(c_vector, A_matrix, b_vector, odefun, tspan, tau, incond)
s_stages = length(c_vector);
m = length(incond);
c_vector = reshape(c_vector, [s_stages 1]);
b_vector = reshape(b_vector, [s_stages 1]);
incond = reshape(incond, [m 1]);
t = (tspan(1):tau :tspan(2))';
xsol = zeros(length(incond), length(t));
xsol(:, 1) = incond(:);
K_matrix = zeros(m, s_stages);
for n = 1:length(t)-1
K_matrix(:, 1) = odefun(t(n), xsol(:, n));
for i = 2:s_stages
K_matrix(:, i) = odefun(t(n) + tau * c_vector(i), xsol(:, n) + tau * K_matrix(:, 1:i-1) * A_matrix(i, 1:i-1)');
end
xsol(:, n+1) = xsol(:, n) + tau * K_matrix * b_vector;
end
xsol = xsol';
end
With only 23 lines for such a powerful instrument, it looks awesome, doesn't it?
Here no unnecessary variables are created, and the K_matrix
is initialized as zero matrix only once, because the algorithm allows not to fill it with zeros at each iteration, but just to overwrite the columns at this iteration without using the columns with coeficients from the previous one:
K_matrix(:, i) = odefun(t(n) + tau * c_vector(i), xsol(:, n) + tau * K_matrix(:, 1:i-1) * A_matrix(i, 1:i-1)')
- Butcher, J. (2016). Numerical methods for ordinary differential equations. https://doi.org/10.1002/9781119121534
- Tamari, B. (1997). Conservation and symmetry laws and stabilization programs in economics. https://www.bentamari.com/PicturesEcometry/Book3-Conservation.pdf