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