This demo illustrates how the JMSL Library can be used to easily build an interactive display application using Java, utilizing ordinary differential equations to describe and illustrate real-world situations. The predator-prey problem computes the population density of rabbits and foxes over time. The population density is governed by the following Ordinary Differential Equations:
dr/dt = 2r - 2rf
df/dt = -f + rf
where r is the density of rabbits, and f is the density of foxes. The chart shows the population density of foxes vs. rabbits. The curve is parameterized by time. The blue squares are computed values and the line is an interpolation between the computed values. The large red circle is the initial condition.
Click in the chart area to set a new initial condition for the problem. The solution is recomputed automatically. You can click and drag to update the solution continuously.
com.imsl.math.OdeRungeKutta
- this class
is used to compute the solution for this initial-value problem using the Runge-Kutta-Verner
fifth-order and sixth-order method.
com.imsl.IMSLException
- this class appears
because OdeRungeKutta
can throw a com.imsl.math.OdeRungeKutta.DidNotConvergeException
.
The exception is thrown by the solve()
method and caught in the
updateInitialPoint()
method where it is reported by printing text
in the chart area.
The chart in this demo contains the initial condition marked as a large red circle and the solution as a blue line with blue filled squares as markers. All of the charting attributes are configured in the constructor.
To update the chart, the class implements
MouseListener, MouseMotionListener
and the updateInitialPoint()
method is called on mouseClicked or mouseDragged events.
Note that the MouseEvent is passed to that method.
Reporting an exception by printing text in the chart
area is unique to this demo. There are two parts to accomplishing this. First,
a Data object must be created to hold the text. To hold the text, the setLabelType()
method is set to Data.LABEL_TYPE_TITLE
.
double
ex[] = {2.5};
double ey[] = {4.0};
dataError = new Data(axis, ex, ey);
dataError.setLabelType(Data.LABEL_TYPE_TITLE);
dataError.setTextColor(Color.red);
Initially the title attribute for this data object is not set; when there is nothing to report, or is set to an empty string. Once an exception is thrown and needs to be reported, the title attribute is set to the string to display.
}
catch (IMSLException exception) {
StringBuffer sb = new StringBuffer(exception.toString());
int nCharsPerLine = 30;
for (int k = nCharsPerLine; k < sb.length(); k += nCharsPerLine) {
for (int j = 0; j < nCharsPerLine; j++) {
if (sb.charAt(k-j) == ' ') {
sb.setCharAt(k-j, '\n'); k -= j;
break;
}
}
}
dataError.setTitle(sb.toString());
}
repaint();
PredatorPrey.java | This is the main class, and it extends JFrameChart. |
Two alternatives are available to run this demo:
1) Use the source code in your development environment as any other Java code. More information is available in the How To.
2) An executable jar file containing all of the demos
referenced in this guide is included in the jmsl/lib directory. On
Windows, you may double-click the file to run it if files with a ".jar"
extension are properly registered with javaw.exe. Alternatively,
for both Windows and UNIX environments, the jar file may be executed from the
command line using java -jar gallery.jar
.
As list of buttons, one for each demo, is created. Demos can be subsetted as they relate to specific areas (Math, Stat, Finance, Charting) by choosing the appropriate selection on the JComboBox. To run the Additional Demos, select Quick Start in the JComboBox.