RK4 Solvers
Mathematics.NET provides a number of fourth-order Runge-Kutta (RK4) solvers, some of which can be used for tensor equations.
Second-Order Differential Equations
Suppose we want to solve the ordinary differential equation (ODE),
with the initial conditions and . We start by multiplying both sides by two and set so that we obtain
This lets us write equation (1) as
To solve this with Mathematics.NET, use RungeKutta4<Vector2<Real>, Real>
with X1
= and X2
=. We write the following:
using Mathematics.NET.Core;
using Mathematics.NET.LinearAlgebra;
using Mathematics.NET.Solvers;
using Plotly.NET.CSharp;
using Chart = Plotly.NET.CSharp.Chart;
State<Vector2<Real>, Real> state = new(new Vector2<Real>[] { new(2.34, 1.23) }, 0);
RungeKutta4<Vector2<Real>, Real> rk4 = new((t, x) => new(2 * Real.Sin(t) - 4 * x.X1 - x.X2, x.X1));
var count = 200;
Vector2<Real>[] points = new Vector2<Real>[count];
for (int i = 0; i < count; i++)
{
rk4.Integrate(state, 0.1);
points[i].X1 = state.Time;
points[i].X2 = state.System.Span[0].X2;
}
var chart = Chart.Line<double, double, string>(
x: points.Select(x => x.X1.AsDouble()),
y: points.Select(x => x.X2.AsDouble()),
Name: "X(t)",
ShowLegend: true);
chart.Show();
The state of the system is tracked by State<Vector2<Real>, Real>
, which contains information about the intial conditions. We have also chosen and the number of points to be 200. The solution is then plotted using Plotly.NET.