The Runge Kutta base class

by Trent Guidry 7. October 2009 05:38

In this post I develop the base class used by numerical algorithms that solve systems of ordinary differential equations, such as the Euler and Runge Kutta algorithms.

I will call this class RungeKuttaBase and the source code for it is given at the bottom of this post.

This class is an extremely simple class that consists of one virtual function called Integrate.  It accepts an array of Parameters for the dependent variables of the system of ordinary differential equations, a parameter for the independent variable of the system, an array of functions for the differential equations, a double specifying the integration end point, and a double specifying the integration step size.  The results of the integration are returned in an array of double arrays.

An example of using a class that derives from this class is given below.

Equations (from a lecture by Oleg Emanouvilov at Iowa State University) :

x1’ = x1 – 2x2

x2’ = 2x1 + x2

Where x1(0) = 0, x2(0) = 4

This system was analytically solved in that lecture to give the following solution.

x1 = -4etsin(2t)

x2 = 4etcos(2t)

Sample code for the integration from 0 to 3.3 with a step size of 0.1, where Runge Kutta 5 Butcher inherits from Runge Kutta Base is given below.

            Parameter T = new Parameter(0.0);
            Parameter X1 = new Parameter(0.0);
            Parameter X2 = new Parameter(4.0);
            Parameter[] parameters = new Parameter[] { X1, X2 };
            Func<double>[] functions = new Func<double>[]
            {
                () => X1 - 2.0 * X2,
                () => 2.0 * X1 + X2
            };

            RungeKuttaBase ode = new RungeKutta5Butcher();
            double[][] dRes = ode.Integrate(parameters, T, functions, 3.3, 0.1);

The source code for the Runge Kutta Base class is given below.

    public class RungeKuttaBase
    {
        public RungeKuttaBase()
        {
        }

        public virtual double[][] Integrate(Parameter[] rungeKuttaParameters, Parameter x, Func<double>[] rungeKuttaFunctions, double xEnd, double step)
        {
            throw new NotImplementedException();
        }
    }

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading