The first thing to do is to create a class containing the equations. The Lorenz system consists of the following three equations:
Doing this using the Apache Commons Math library means that we have to create a class that implements the FirstOrderDifferentialEquations interface. This interface requires two methods to be implemented, which are computeDerivatives and getDimension:
01 import org.apache.commons.math3.exception.DimensionMismatchException; DimensionMismatchException {
|
| Java2html |
The next step is to write the code that will call the integrator. There are various integrators. For this application we'll use the ClassicalRungeKuttaIntegrator. You will also need a step handler to capture each step of the integration:
01 import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; new ClassicalRungeKuttaIntegrator(0.01); WriteToFileStepHandler("lorenz.csv");
|
| Java2html |
The step handler has to implement the StepHandler class. For the step handler below I created a constructor that takes a filename so that the results can be written to a csv file. Obviously you can do whatever you want with this data:
01 import java.io.File;30 new PrintWriter(new File(filename), "UTF-8");
|
| Java2html |
Last but not the least I read the file with R and created the graph:


