Example: Adams-Gear ordinary differential equation solver
A mildly stiff ordinary differential equation problem is solved using a solver which implements the Adams-Gear method. The solution at time t=240 is printed.
using System;
using Imsl.Math;
public class OdeAdamsGearEx1
{
private class MyFunction : OdeAdamsGear.IFunction
{
public MyFunction(double k1, double k2, double k3)
{
this.k1 = k1;
this.k2 = k2;
this.k3 = k3;
}
private double k1;
private double k2;
private double k3;
public virtual double[] F(double t, double[] y)
{
double[] yprime = new double[y.Length];
yprime[0] = - y[0] - y[0] * y[1] + k1 * y[1];
yprime[1] = (- k2) * y[1] + k3 * (1.0 - y[1]) * y[0];
return yprime;
}
}
public static void Main(String[] args)
{
double k1 = 294.0;
double k2 = 3.0;
double k3 = 0.01020408;
OdeAdamsGear.IFunction f = new MyFunction(k1, k2, k3);
double t = 0.0;
double tend = 240.0;
double[] y = {1.0, 0.0};
OdeAdamsGear q = new OdeAdamsGear(f);
q.NormMethod = OdeAdamsGear.ErrorNormOptions.Abs;
q.SolveMethod =
OdeAdamsGear.SolveOption.ChordComputedJacobian;
q.Tolerance = 1e-3;
q.Solve(t, tend, y);
// Print Results
Console.Out.WriteLine("Result = {{{0,5:0.####}, {1,5:0.####}}}",
y[0], y[1]);
}
}
Output
Result = {0.3924, 0.0013}
Link to C# source.