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.

import java.text.*;
import com.imsl.math.*;

public class OdeAdamsGearEx1 {

    public static void main(String args[]) throws com.imsl.IMSLException {
        final double k1 = 294.0;
        final double k2 = 3.0;
        final double k3 = 0.01020408;

        OdeAdamsGear.Function f = new OdeAdamsGear.Function() {
            public 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;
            }
        };

        double t = 0.0;
        double tend = 240.0;
        double y[] = {1.0, 0.0};
        OdeAdamsGear q = new OdeAdamsGear(f);
        q.setNorm(OdeAdamsGear.ERROR_NORM_ABS);
        q.setSolveMethod(OdeAdamsGear.SOLVE_CHORD_COMPUTED_JACOBIAN);
        q.setTolerance(1.e-3);
        q.solve(t, tend, y);

        // Print Results
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(4);
        nf.setMinimumFractionDigits(4);

        System.out.println("Result = {" + nf.format(y[0]) + ", "
                + nf.format(y[1]) + "}");
    }
}

Output

Result = {0.3924, 0.0013}
Link to Java source.