简体   繁体   中英

How do you use Apache Commons Math 3.0+ to solve ODEs with Jacobian?

Regarding a particular Java library to solve ODEs with Jacobian approximation, a previous Apache Commons Math library 2.2 is intuitive and there are some clear examples too:

Ordinary Differential Equations Integration

But 2.2 is deprecated now, and the recent library 3.0+

Apache Commons Math 3.0

particularly the packages:

org.apache.commons.math3.ode

org.apache.commons.math3.ode.events

org.apache.commons.math3.ode.nonstiff

are the replacements but seem to be much more complex. I think the new ODE versions were motivated by generalising the data structures and methods, but it is difficult to understand how some of the classes are intended to be used together (eg MainStateJacobianProvider, ExpandableStatefulODE, ParameterizedODE) and there are no examples of it anywhere while Apache have not updated their information pages. The API documentation is not very informative and seems ambiguous too.

So instead of sounding like I am complaining about everything I will pose a specific question, How would you recode the same problem from one of the examples demonstrated using 2.2 above, using the 3.0 library?

Here is the code:

public class BasicCircleODE implements ParameterizedODE {

    private double[] c;
    private double omega;

    public BasicCircleODE(double[] c, double omega) {
        this.c = c;
        this.omega = omega;
    }

    public int getDimension() {
        return 2;
    }

    public void computeDerivatives(double t, double[] y, double[] yDot) {
        yDot[0] = omega * (c[1] - y[1]);
        yDot[1] = omega * (y[0] - c[0]);
    }   

    public int getParametersDimension() {
        // we are only interested in the omega parameter
        return 1;
    }

    public void setParameter(int i, double value) {
        omega = value;
    }

}

double[] hY = new double[] { 0.001, 0.001 };
double[] hP = new double[] { 1.0e-6 };
FirstOrderIntegratorWithJacobians integrator = new FirstOrderIntegratorWithJacobians(dp853, ode, hY, hP);
integrator.integrate(t0, y0, dy0dp, t, y, dydy0, dydp);

The BasicCircle example has been translated to the new API in JacobianMatricesTest.

But I would suggest that you ask the question on the user mailinglist of the project where the devs usually answer quickly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM