Class RungeKutta2<P>

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

public abstract class RungeKutta2<P> extends Object
Runge Kutta algorithm for solutions of second order differential equations. For an independent variable t (which will be called the "parameter" mainly so that a method with a short name getParam() can be used to obtain its current value), and a dependent variable y(t), the Runge Kutta algorithm is used to numerically solves the differential equation
y''(t) = f(t, y(t), y'(t))
The method getParam() returns the independent variable while the methods getValue(), getDeriv(), and getSecondDeriv() return* the dependent variable, its derivative, and its second derivative respectively. The independent variable is changed, and the dependent variable 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) or {setTolerance(double,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.

The static methods newInstance(RealValuedFunctionThree), newInstance(RealValuedFunctionThree,double,double,double), newInstance(RealValuedFunctThreeOps), and newInstance(RealValuedFunctThreeOps,double,double,double), can be used to create new instances of the RungeKutta2 class, but without parameters.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor.
    RungeKutta2(double t0, double y0, double yp0)
    Constructor with initial values.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    adaptiveUpdate(double tincr)
    Update the independent and dependent variables adaptively, increasing the independent variable by a specified amount.
    protected abstract double
    function(double t, double y, double yp)
    Apply a function to compute the derivatives given a parameter t and a variables y.
    final double
    Get the current value of the derivative of the dependent variable.
    final double
    Get the current value of the independent variable.
    Get a RungeKutta2's parameters.
    final double
    Get the current value of the second deriviative of the dependent variable
    double
    Get the current tolerance for the dependent variable.
    double
    Get the current tolerance for the derivative of the dependent variable.
    final double
    Get the current value of the dependent variable.
    double
    Get the minimum step size used by the Runge-Kutta-Fehlberg method since the last time the initial values were set, the tolerance was changed, or this method was called.
    Create a new instance of RungeKutta that uses an instance of RealValuedFunctionThree as its function.
    newInstance(RealValuedFunctionThree f, double t0, double y0, double yp0)
    Create a new instance of RungeKutta that uses an instance of RealValuedFunctionThree as its function, providing initial values.
    Create a new instance of RungeKutta that uses an instance of RealValuedFunctThreeOps as its function.
    newInstance(RealValuedFunctThreeOps f, double t0, double y0, double yp0)
    Create a new instance of RungeKutta2 that uses an instance of RealValuedFunctThreeOps as its function, providing initial values.
    void
    setInitialValues(double t0, double y0, double yp0)
    Set initial conditions.
    void
    setParameters(P parameters)
    Set a RungeKutta2's parameters.
    void
    setTolerance(double tol)
    Set the tolerances to the same values.
    void
    setTolerance(double tol1, double tol2)
    Set the tolerance.
    final void
    update(double h)
    Update the independent and dependent variables.
    final void
    update(double tincr, int n)
    Multi-step update of the independent and dependent variables.
    final void
    updateTo(double t)
    Update the independent and dependent variables so that the parameter will have a specified value.
    final void
    updateTo(double t, double h)
    Update the independent and dependent variables so that the parameter 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

    • RungeKutta2

      public RungeKutta2()
      Constructor.
    • RungeKutta2

      public RungeKutta2(double t0, double y0, double yp0)
      Constructor with initial values.
      Parameters:
      t0 - the initial value of the parameter with respect to which one differentiates
      y0 - the initial value of the variable for the specified initial value of the parameter
      yp0 - the initial value of the variable's first derivative for the specified initial value of the parameter
  • Method Details

    • setParameters

      public void setParameters(P parameters)
      Set a RungeKutta2'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 RungeKutta2'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 'function'.
      Returns:
      an instance of the class representing a Runge-Kutta class' parameters (this will be the same instance passed to setParameters)
    • function

      protected abstract double function(double t, double y, double yp)
      Apply a function to compute the derivatives given a parameter t and a variables y.
      Parameters:
      t - the parameter with respect to which one differentiates
      y - the value of the variable for the specified parameter t
      yp - the derivative of the variable for the specified parameter t
      Returns:
      the second derivative of y with respect to the parameter t
    • setInitialValues

      public void setInitialValues(double t0, double y0, double yp0)
      Set initial conditions. This is also done in the constructor.
      Parameters:
      t0 - the value of the parameter with respect to which one differentiates
      y0 - the value of the variable for the initial value of the parameter with respect to which one differentiates
      yp0 - the value of the derivative of the variable for the initial value of the parameter with respect to which one differentiates
    • getValue

      public final double getValue()
      Get the current value of the dependent variable.
      Returns:
      the value of the variable
    • getDeriv

      public final double getDeriv()
      Get the current value of the derivative of the dependent variable.
      Returns:
      the value of the variable's derivative
    • getSecondDeriv

      public final double getSecondDeriv()
      Get the current value of the second deriviative of the dependent variable
      Returns:
      the derivative of the dependent variable.
    • 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 and dependent variables.
      Parameters:
      h - the amount by which the independent variable changes
    • update

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

      public double minStepSize()
      Get the minimum step size used by the Runge-Kutta-Fehlberg method 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 parameter is updated, changing it by an amount t, the error is bounded by the absolute value of the change in the parameter 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 tol1, double tol2)
      Set the tolerance. When the parameter is updated, changing it by an amount t, the error is bounded by the absolute value of the change in the parameter 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:
      tol1 - the tolerance for the dependent variable
      tol2 - the tolerance for the first derivative of the dependent variable.
      Throws:
      IllegalArgumentException - the argument was less than or equal to zero
    • getTolerance1

      public double getTolerance1()
      Get the current tolerance for the dependent variable.
      Returns:
      the tolerance; zero if the tolerance has not been set
    • getTolerance2

      public double getTolerance2()
      Get the current tolerance for the derivative of the dependent variable.
      Returns:
      the tolerance; zero if the tolerance has not been set
    • adaptiveUpdate

      public void adaptiveUpdate(double tincr) throws IllegalStateException
      Update the independent and dependent variables 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 and dependent variables so that the parameter will have a specified value. The step size will be determined adaptively by this method.
      Parameters:
      t - the new value of the independent variable
      Throws:
      IllegalStateException - the method setTolerance(double) has not been called
    • updateTo

      public final void updateTo(double t, double h) throws IllegalArgumentException
      Update the independent and dependent variables so that the parameter 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 parameter
      h - the step size limit
      Throws:
      IllegalArgumentException - an argument was out of range (e.g, h was 0 or negative)
    • newInstance

      public static RungeKutta2 newInstance(RealValuedFunctionThree f)
      Create a new instance of RungeKutta that uses an instance of RealValuedFunctionThree as its function. This function's first argument is the Runge-Kutta algorithm's parameter (t) and its second argument is the current value of the Runge-Kutta algorithm's variable (y), and its third argument is the derivative of the variable with respect to the parameter (i.e,, dy/dt).
      Parameters:
      f - the function, which must take three arguments
      Returns:
      a new instance of RungeKutta2
    • newInstance

      public static RungeKutta2 newInstance(RealValuedFunctionThree f, double t0, double y0, double yp0)
      Create a new instance of RungeKutta that uses an instance of RealValuedFunctionThree as its function, providing initial values. This function's first argument is the Runge-Kutta algorithm's parameter (t) and its second argument is the current value of the Runge-Kutta algorithm's variable (y), and its third argument is the derivative of the variable with respect to the parmaeter (i.e,, dy/dt).
      Parameters:
      f - the function, which must take exactly three arguments
      t0 - the initial value of the parameter with respect to which one differentiates
      y0 - the initial value of the variable for the specified parameter
      yp0 - the initial value of the derivative of the variable for the specified parameter (i.e., dy/dt evaluated at t0)
      Returns:
      a new instance of RungeKutta2
      Throws:
      IllegalArgumentException - the function f cannot take two arguments
    • newInstance

      public static RungeKutta2 newInstance(RealValuedFunctThreeOps f)
      Create a new instance of RungeKutta that uses an instance of RealValuedFunctThreeOps as its function. This function's first and only argument is the Runge-Kutta algorithm's parameter (t) and its second argument is the initial value the Runge-Kutta algorithm's variable (y), and its third argument is the derivative of the variable with respect to the parmaeter (i.e,, dy/dt).

      The function may be a lambda expression.

      Parameters:
      f - the function, which must take three arguments
      Returns:
      a new instance of RungeKutta2
    • newInstance

      public static RungeKutta2 newInstance(RealValuedFunctThreeOps f, double t0, double y0, double yp0)
      Create a new instance of RungeKutta2 that uses an instance of RealValuedFunctThreeOps as its function, providing initial values. This function's first argument is the Runge-Kutta algorithm's parameter (t) and its second argument is the current value of the Runge-Kutta algorithm's variable (y), and its third argument is the derivative of the variable with respect to the parmaeter (i.e,, dy/dt).

      The function may be a lambda expression.

      Parameters:
      f - the function, which must take exactly three arguments
      t0 - the initial value of the parameter with respect to which one differentiates
      y0 - the initial value of the variable for the specified parameter
      yp0 - the initial value of the derivative of the variable for the specified parameter (i.e., dy/dt evaluated at t0)
      Returns:
      a new instance of RungeKutta2