001    import org.apache.commons.math3.ode.*;
002    
003    /** Example of a system of first order differential equations (ODEs).
004      * <pre>  Lotka-Volterra equations for rabbits r(t) and foxes f(t)
005      *
006      *  dr/dt = r(t) * ( a - b * f(t) )
007      *  df/dt = f(t) * ( d * r(t) - c )
008      * </pre>
009      * @see <a target="_top" href="../0_CommonsMath_README.html#examples">CommonsMath_ODE_Lotka_Volterra~Demo.jxn</a>
010      * @see <a target="_blank" href="https://en.wikipedia.org/wiki/Lotka%2dVolterra_equations">&rarr; Wikipedia</a>
011      */
012    public class ODE_Lotka_Volterra implements FirstOrderDifferentialEquations
013    {
014    // global parameters:
015       double a, b, c, d;
016    
017       /** Stores the parameters of the equations. */
018       public ODE_Lotka_Volterra( double a, double b, double c, double d )
019       {
020          this.a = a;
021          this.b = b;
022          this.c = c;
023          this.d = d;
024       }
025       
026    // --- interface FirstOrderDifferentialEquations ---
027    
028       /** Returns the number of states(=2). */
029       public int getDimension()
030       {
031          return 2;
032       }
033    
034       /** Computes <code>dr/dt</code> and <code>df/dt</code>. */
035       public void computeDerivatives( double t, double [] y, double [] ydot )
036       {
037       // calculate derivatives
038          double rt = y[0];  // rabbits
039          double ft = y[1];  // foxes
040          ydot[0] = rt * ( a - b * ft );  // dr/dt
041          ydot[1] = ft * ( d * rt - c );  // df/dt
042       }
043    }