Class RungeKuttaMV<P>

java.lang.Object
org.bzdev.math.RungeKuttaMV<P>

public abstract class RungeKuttaMV<P> extends Object
Multi-variable implementation of the Runge Kutta algorithm. For an independent variable t, and a vector of dependent variables y(t), the Runge Kutta algorithm numerically solves the differential equation
y'(t) = f(t, y(t))
The method getParam() returns the independent variable while the method getValue(int) and getDeriv(int) return the dependent variable and its first derivative respectively. The independent variable is changed, and the dependent variables are updated, by using the methods update(double), update(double,int), adaptiveUpdate(double), updateTo(double), or updateTo(double,double). The methods update(double), update(double,int), and updateTo(double,double) use the 4th order Runge-Kutta method, while the methods adaptiveUpdate(double) and update(double) use the Runge-Kutta-Fehlberg method (RK45), which adaptively adjusts the step size given a specified tolerance. The method minStepSize() will report the minimum step size used by the Runge-Kutta-Fehlberg method. This is useful if one wants an estimate of the number of knots needed for a spline that will fit the solution to a differential equation. Before the Runge-Kutta-Fehlberg method is used, the method setTolerance(double) must be called.

When parameters are provided (via a generic type), the parameters are used to adjust the behavior of the class' function, typically by providing various constants that it needs. This can reduce the number of classes created by an application in some instances. The parameters are represented by a Java class typically used as a container to hold a set of values.

  • Constructor Summary

    Constructors
    Constructor
    Description
    RungeKuttaMV(int n)
    Constructor.
    RungeKuttaMV(int n, double t0, double[] y0)
    Constructor with initial values.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    adaptiveUpdate(double tincr)
    Update the independent variable and variable adaptively, increasing the independent variable by a specified amount.
    protected abstract void
    applyFunction(double t, double[] y, double[] results)
    Apply a function to compute the derivatives given a parameter t and a variables y.
    final double
    getDeriv(int index)
    Get the current value of the derivative of a dependent variable
    final void
    getDerivs(double[] derivs)
    Get the current value fo the derivatives of the dependent variables
    final double
    Get the current value of the independent variable
    Get a RungeKuttaMV's parameters.
    double
    Get the current tolerance for the dependent variable.
    void
    getTolerances(double[] array)
    Get an array of tolerance, indexed by the indices of the dependent variables.
    final double
    getValue(int index)
    Get the current value of a dependent variable.
    final void
    getValues(double[] values)
    Get the current values of the dependent variables.
    double
    Get the minimum step size used since the last time the initial values were set, the tolerance was changed, or this method was called.
    final void
    setInitialValues(double t0, double[] y0)
    Set initial conditions.
    void
    setParameters(P parameters)
    Set a RungeKuttaMV's parameters.
    void
    setTolerance(double tol)
    Set the tolerances to the same values.
    void
    setTolerance(double[] tol)
    Set the tolerances.
    void
    setTolerance(int i, double tol)
    Set the tolerance for a specific dependent variable.
    final void
    update(double h)
    Update the independent variable and the dependent variables.
    final void
    update(double tincr, int m)
    Multi-step update of the independent variable and the dependent variables.
    final void
    updateTo(double t)
    Update the independent variable and variable so that the independent variable will have a specified value.
    final void
    updateTo(double t, double h)
    Update the independent variable and dependent variables so that the independent variable will have a specified value and so that the step size is a specified value or lower.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RungeKuttaMV

      public RungeKuttaMV(int n)
      Constructor.
      Parameters:
      n - the number of variables
    • RungeKuttaMV

      public RungeKuttaMV(int n, double t0, double[] y0)
      Constructor with initial values.
      Parameters:
      n - the number of variables
      t0 - the initial value of the independent variable
      y0 - the initial values of the dependent variables
  • Method Details

    • setParameters

      public void setParameters(P parameters)
      Set a RungeKuttaMV's parameters. Parameters are used to provide values that will be constant while the Runge-Kutta algorithm is running and may be used by the method named 'applyFunction'.
      Parameters:
      parameters - the parameters
    • getParameters

      public P getParameters()
      Get a RungeKuttaMV's parameters. Parameters are used to provide values that will be constant while the Runge-Kutta algorithm is running and may be used by the method named 'applyFunction'.
      Returns:
      an instance of the class representing a Runge-Kutta class' parameters (this will be the same instance passed to setParameters)
    • applyFunction

      protected abstract void applyFunction(double t, double[] y, double[] results)
      Apply a function to compute the derivatives given a parameter t and a variables y.
      Parameters:
      t - the independent variable
      y - the values of the dependent variables.
      results - the derivatives for the dependent variables
    • setInitialValues

      public final void setInitialValues(double t0, double[] y0)
      Set initial conditions. This is also done in one of the constructors.
      Parameters:
      t0 - the initial value of the independent variable
      y0 - the initial values of the dependent variables
    • getValue

      public final double getValue(int index)
      Get the current value of a dependent variable.
      Parameters:
      index - the dependent variable's index
      Returns:
      the value of the dependent variable for the specified index
    • getValues

      public final void getValues(double[] values)
      Get the current values of the dependent variables.
      Parameters:
      values - an array to hold the values of the dependent variables
    • getDeriv

      public final double getDeriv(int index)
      Get the current value of the derivative of a dependent variable
      Parameters:
      index - the index of a dependent variable
      Returns:
      the current value of the derivative for a specified dependent variable
    • getDerivs

      public final void getDerivs(double[] derivs)
      Get the current value fo the derivatives of the dependent variables
      Parameters:
      derivs - an array in which to store the derivatives
    • getParam

      public final double getParam()
      Get the current value of the independent variable
      Returns:
      the value of the independent variable
    • update

      public final void update(double h)
      Update the independent variable and the dependent variables.
      Parameters:
      h - the amount by which the independent variable changes
    • update

      public final void update(double tincr, int m)
      Multi-step update of the independent variable and the dependent variables.
      Parameters:
      tincr - the amount by which the independent variable changes
      m - the number of steps to use in changing the independent variable
    • minStepSize

      public double minStepSize()
      Get the minimum step size used since the last time the initial values were set, the tolerance was changed, or this method was called.

      After this method is called, subsequent calls will return 0.0 unless either adaptiveUpdate(double) or updateTo(double) was called with an argument that would change the current value of the independent variable. Changing the initial value or the tolerance will also result in this method returning 0.0 until either adaptiveUpdate(double) or updateTo(double) is called with an argument that would change the current value of the independent variable.

      Returns:
      the minimum step size; 0.0 if the minimum cannot yet be determined
    • setTolerance

      public void setTolerance(double tol)
      Set the tolerances to the same values. When the independent variable is updated, changing it by an amount t, the error is bounded by the absolute value of the change in the independent variable multiplied by the tolerance. A tolerance applies to the methods adaptiveUpdate(double) and updateTo(double).

      The class SimObject had a public method named SimObject.update() that by default calls a protected method named SimObject.update(double,long). A simulation object whose behavior is determined by a differential equation may contain a field whose value is an instance of RungeKutta, and the implementation of these update methods may call adaptiveUpdate(double) or updateTo(double). When this is the case, the tolerance(s) must typically be set before the simulation object's update method is called. The exception is when the the simulation time matches the value of the independent variable so that adpativeUpdate will be called with an argument of 0.0.

      Parameters:
      tol - the tolerance
      Throws:
      IllegalArgumentException - the argument was less than or equal to zero
    • setTolerance

      public void setTolerance(double[] tol)
      Set the tolerances. When the independent variable is updated, changing it by an amount t, the error is bounded by the absolute value of the change in the independent variable multiplied by the tolerance. A tolerance applies to the methods adaptiveUpdate(double) and updateTo(double).

      The class SimObject had a public method named SimObject.update() that by default calls a protected method named SimObject.update(double,long). A simulation object whose behavior is determined by a differential equation may contain a field whose value is an instance of RungeKutta, and the implementation of these update methods may call adaptiveUpdate(double) or updateTo(double). When this is the case, the tolerance(s) must typically be set before the simulation object's update method is called. The exception is when the the simulation time matches the value of the independent variable so that adpativeUpdate will be called with an argument of 0.0.

      Parameters:
      tol - the tolerances
      Throws:
      IllegalArgumentException - the argument was less than or equal to zero
    • setTolerance

      public void setTolerance(int i, double tol)
      Set the tolerance for a specific dependent variable. When the independent variable is updated, changing it by an amount t, the error is bounded by the absolute value of the change in the independent variable multiplied by the tolerance. A tolerance applies to the methods adaptiveUpdate(double) and updateTo(double).

      The class SimObject had a public method named SimObject.update() that by default calls a protected method named SimObject.update(double,long). A simulation object whose behavior is determined by a differential equation may contain a field whose value is an instance of RungeKutta, and the implementation of these update methods may call adaptiveUpdate(double) or updateTo(double). When this is the case, the tolerance(s) must typically be set before the simulation object's update method is called. The exception is when the the simulation time matches the value of the independent variable so that adaptiveUpdate will be called with an argument of 0.0.

      Parameters:
      i - the index for the variable
      tol - the tolerance for the variable whose index is i
      Throws:
      IllegalArgumentException - the argument was less than or equal to zero
    • getTolerance1

      public double getTolerance1(int i)
      Get the current tolerance for the dependent variable.
      Parameters:
      i - the index of the dependent variable whose tolerance will be returned
      Returns:
      the tolerance; zero if the tolerance has not been set
    • getTolerances

      public void getTolerances(double[] array)
      Get an array of tolerance, indexed by the indices of the dependent variables.
      Parameters:
      array - the array in which to store the values
      Throws:
      IndexOutOfBoundsException - the argument array is too small to contain the tolerances
      NullPointerException - the argument was null
    • adaptiveUpdate

      public void adaptiveUpdate(double tincr) throws IllegalStateException
      Update the independent variable and variable adaptively, increasing the independent variable by a specified amount.
      Parameters:
      tincr - the increment for the independent variable.
      Throws:
      IllegalStateException - the method setTolerance(double) has not been called
    • updateTo

      public final void updateTo(double t) throws IllegalStateException
      Update the independent variable and variable so that the independent variable will have a specified value. The step size will be determined by this method.
      Parameters:
      t - the new value of the independent variable
      Throws:
      IllegalStateException - one of the methods named setTolerance has not been called.
    • updateTo

      public final void updateTo(double t, double h) throws IllegalArgumentException
      Update the independent variable and dependent variables so that the independent variable will have a specified value and so that the step size is a specified value or lower.

      Note, regardless of the value of h, the maximum number of steps used will be no greater than Integer.MAX_VALUE.

      Parameters:
      t - the new value of the independent variable
      h - the step size limit
      Throws:
      IllegalArgumentException - an argument was out of range (e.g, h was 0 or negative)