Michael Thomas Flanagan's Java Scientific LibraryRungeKutta Class:      Numerical Solution of Differential Equations

Last update details: 28 February 2015
Main Page of Michael Thomas Flanagan's Java Scientific Library

This class contains the methods for performing the solving a single ordinary differential equation (ODE) and for solving a set of ordinary differential equations:
• Fourth Order Runge-Kutta (fixed step size);
A single ODE is supplied by means of the interface, DerivFunction.
A set of ODEs is supplied by means of the interface, DerivnFunction.
 import statement/s: import flanagan.integration.RungeKutta; import flanagan.integration.DerivFunction; [single ODE] import flanagan.integration.DerivnFunction; [set of ODEs]
Related classes

### SUMMARY OF METHODS

 Constructor public RungeKutta() Set initial value of x public void setInitialValueOfX(double x0) Set final value of x public void setFinalValueOfX(double xn) Set initial value/s of y public void setInitialValueOfY(double y0) public void setInitialValuesOfY(double[] y0) Set step size / initial step size public void setStepSize(double step) Set tolerance (accuracy) public void setToleranceScalingFactor(double tolSF) public void setToleranceAdditionFactor(double tolAddn) Number of iterations public void setMaximumIterations(int maxIter) public int getNumberOfIterations() public static void resetNstepsMultiplier(int multiplier) 4th order Runge-Kutta Single ODE, single x value public double fourthOrder(DerivFunction g) public static double fourthOrder(DerivFunction g, double x0, double y0, double xn, double h) Single ODE, several x values public double[][] fourthOrder(DerivFunction g, int nPoints) public double[][] fourthOrder(DerivFunction g, double[] points) public static double [][] fourthOrder(DerivFunction g, int nPoints, double x0, double y0, double xn, double h) public static double [][] fourthOrder(DerivFunction g, double[] points, double x0, double y0, double xn, double h) Several ODEs, single x value public double[] fourthOrder(DerivnFunction g) public static double[ ] fourthOrder(DerivnFunction g, double x0, double[ ] y0, double xn, double h) Several ODEs, several x values public double[][] fourthOrder(DerivnFunction g, int nPoints) public double[][] fourthOrder(DerivnFunction g, double[] points) public static double [][] fourthOrder(DerivnFunction g, int nPoints, double x0, double y0, double xn, double h) public static double [][] fourthOrder(DerivnFunction g, double[] points, double x0, double y0, double xn, double h) Runge-Kutta-Cash-Karp Single ODE, single x value public double cashKarp(DerivFunction g) public static double cashKarp(DerivFunction g, double x0, double y0, double xn, double h, double abstol, double reltol, int maxiter) public static double cashKarp(DerivFunction g, double x0, double y0, double xn, double h, double abstol, double reltol) Single ODE, several x values public double[][] cashKarp(DerivFunction g, int nPoints) public double[][] cashKarp(DerivFunction g, double[] points) public static double[][] cashKarp(DerivFunction g, int nPoints, double x0, double y0, double xn, double h, double abstol, double reltol, int maxiter) public static double[][] cashKarp(DerivFunction g, double points, double x0, double y0, double xn, double h, double abstol, double reltol, int maxiter) public static double[][] cashKarp(DerivFunction g, int nPoints, double x0, double y0, double xn, double h, double abstol, double reltol) public static double[][] cashKarp(DerivFunction g, double points, double x0, double y0, double xn, double h, double abstol, double reltol) Several ODEs, single x value public double[] cashKarp(DerivnFunction g) public static double[] cashKarp(DerivnFunction g, double x0, double[ ] y0, double xn, double h, double abstol, double reltol, int maxiter) public static double[] cashKarp(DerivnFunction g, double x0, double[ ] y0, double xn, double h, double abstol, double reltol) Several ODEs, several x values public double[][] cashKarp(DerivnFunction g, int nPoints) public double[][] cashKarp(DerivnFunction g, double[] points) public static double[][] cashKarp(DerivnFunction g, int nPoints, double x0, double y0, double xn, double h, double abstol, double reltol, int maxiter) public static double[][] cashKarp(DerivnFunction g, double points, double x0, double y0, double xn, double h, double abstol, double reltol, int maxiter) public static double[][] cashKarp(DerivnFunction g, int nPoints, double x0, double y0, double xn, double h, double abstol, double reltol) public static double[][] cashKarp(DerivnFunction g, double points, double x0, double y0, double xn, double h, double abstol, double reltol) Runge-Kutta-Fehlberg Single ODE, single x value public double fehlberg(DerivFunction g) public static double fehlberg(DerivFunction g, double x0, double y0, double xn, double h, double abstol, double reltol, int maxiter) public static double fehlberg(DerivFunction g, double x0, double y0, double xn, double h, double abstol, double reltol) Single ODE, several x values public double[][] fehlberg(DerivFunction g, int nPoints) public double[][] fehlberg(DerivFunction g, double[] points) public static double[][] fehlberg(DerivFunction g, int nPoints, double x0, double y0, double xn, double h, double abstol, double reltol, int maxiter) public static double[][] fehlberg(DerivFunction g, double points, double x0, double y0, double xn, double h, double abstol, double reltol, int maxiter) public static double[][] fehlberg(DerivFunction g, int nPoints, double x0, double y0, double xn, double h, double abstol, double reltol) public static double[][] fehlberg(DerivFunction g, double points, double x0, double y0, double xn, double h, double abstol, double reltol) Several ODEs, single x value public double[] fehlberg(DerivnFunction g) public static double[ ] fehlberg(DerivnFunction g, double x0, double[ ] y0, double xn, double h, double abstol, double reltol, int maxiter) public static double[ ] fehlberg(DerivnFunction g, double x0, double[ ] y0, double xn, double h, double abstol, double reltol) Several ODEs, several x values public double[][] fehlberg(DerivnFunction g, int nPoints) public double[][] fehlberg(DerivnFunction g, double[] points) public static double[][] fehlberg(DerivnFunction g, int nPoints, double x0, double y0, double xn, double h, double abstol, double reltol, int maxiter) public static double[][] fehlberg(DerivnFunction g, double points, double x0, double y0, double xn, double h, double abstol, double reltol, int maxiter) public static double[][] fehlberg(DerivnFunction g, int nPoints, double x0, double y0, double xn, double h, double abstol, double reltol) public static double[][] fehlberg(DerivnFunction g, double points, double x0, double y0, double xn, double h, double abstol, double reltol) Return the x and y values for all steps between the initial and final points public double[][] getAllValues() Plots Plot a multipoint output public void plot() Plot all step values public void plotSteps()

### CONSTRUCTOR AND METHODS

CONSTRUCTOR
public double RungeKutta()

Usage:                      RungeKutta rk = RungeKutta();
Creates an instance of RungeKutta.

SET THE INITIAL VALUE OF X
public void setInitialValueOfX(double x0)
Usage:                      rk.setInitialValueOfX(x0);
Sets the intial value of x, as argument x0, for the single (n=1) or n differential equations (ODEs):

SET THE FINAL VALUE OF X
public void setFinalValueOfX(double xn)
Usage:                      rk.setFinalValueOfX(xn);
Sets the final value of x, as argument xn, for the single (n=1) or n differential equations (ODEs):

SET THE INITIAL VALUE OR VALUES OF Y
Set initial value of y (single ODE)
public void setInitialValueOfY(double y0)
Usage:                      rk.setInitialValueOfY(y0);
Sets the intial value of y, as argument y0, for a single differential equations (ODE):

Set initial values of y (several ODEs)
public void setInitialValuesOfY(double[] yy0)
Usage:                      rk.setInitialValuesOfY(yy0);
Sets the intial values of y, as argument yy0, for n differential equations (ODEs):

SET THE STEP SIZE OR INITIAL STEP SIZE
public void setStepSize(double step)
Usage:                      rk.setStepSize(step);
Sets the step size, as argument step, used by the Runge-Kutta procedure. In the case of the adaptive step procedures the entered value is the initial step size.

SET THE TOLERANCE (ACCURACY) REQUIRED BY THE ADAPTIVE STEP METHODS
The step size h is adjusted on each iteration by the Fehlberg and by the Cash-Karp procedures. After each iteration the ratio, R, is calculated where

and y is the predicted value of y after the last step, err is the difference in the prediction of y using a 4th and 5th order Runge-Kutta procedure. If R is greater than one the step is rejected, the value of h is decreased as

and the step repeated. If R is less than one the step is accepted and the value of h is increased by

for the next step. The scaling factor, reltol, is a measure of the required accuracy and a trial value approximating to the reqiuired fractional accuracy in y is suggested. The factor, abstol, is a small number to accommodate the possibility of a zero value in one of the y values precluding its scaling.

public void setToleranceScalingFactor(double relTol)
Usage:                      rk.setToleranceScalingFactor(relTol);
Sets the value of scaling tolerance factor, reltol, as argument relTol, as described above.

Sets the value of addition tolerance factor, abstol, as argument absTol, as described above.

NUMBER OF ITERATIONS
Set the maximum number of iterations allowed in the adaptive step methods
public void setMaximumIterations(int maxIter)
Usage:                      rk.setMaximumIterations(maxIter);
Sets the value of maximum number of iterations, as argument maxIter, allowed in the adaptive step methods. See immediately below for the default value.

public static void resetNstepsMultiplier(int multiplier)
Usage:                      RungeKutta.resetNstepsMultiplier(multiplier);
If the method setting the maximum number of allowed iterations (see immediately above) is not called this number is calculated as multiplier(xn - x0)/h where x0 is the intial value of x, xn is the final value, h is the initial step size and the multiplier as a default value of 1000. This method allows the value of the multiplier factor to be reset.

Get the number of iterations performed
public int getNumberOfIterations()
Usage:                      nIter = rk.getNumberOfIterations();
Returns the number of iterations performed by any of the methods, fixed step or adaptive step.

FOURTH ORDER RUNGE-KUTTA METHOD
Set of n Ordinary Differential Equations (See below for n = 1)
Instance methods
public double[ ] fourthOrder(DerivnFunction g)
Usage:                      y = rk.fourthOrder(func);
Returns the n values of yi, as y[i], at the final value of x for the initial value of x and initial values of y, entered by the relevant setting methods described above, for a set of n ordinary differential equation (ODEs):

The method uses a classical fourth order Runge-Kutta procedure hence the step size is fixed. If (final value of x - initial value of x)/(step size) is not an integer the step size is first adjusted so that it is.
The argument, func, is a reference to the instance of the class that codes the function, f(x, y)reference (pointer) to the instance of the class that codes the function, f(x, y).
The mathematical functions to which the derivatives are equal, i.e. the n fi(x, yi), must be coded as a method
public double[ ] derivn(double x, double[ ] y)
The method, derivn, must have the above signature, i.e. name, type and argument list, and be a method in a class which must implement the interface, DerivnFunction. The name of this class is the user's choice. The arguments to the mathematical version of the functions, fi(x, yi, a, b, c . . .) other than x and the n yi, i.e. a, b, c . . . may not be included in the method deriv(x, y) argument list. They should be declared as members of the class of which derivn(x, y) is a member. The function is passed to the RungeKutta.fourthOrder(. . .) method as an instance of the class containing the method derivn(x, y) [func in the above example but this instance may be named at the user's choice]. This instance should be created in the method that calls RungeKutta.fourthOrder(. . .). This procedure can be seen more clearly in example four in Example programs.

public double[][] fourthOrder(DerivnFunction g, int nPoints)
Usage:                      y = rk.fourthOrder(func, nPoints);
Returns the values of the n yi, as y at nPoints equispaced points between, and including, the initial value of x and the final value of x for the initial value of x and initial value of y, entered by the relevant setting methods described above, for n ordinary differential equation (ODE).
The first row of the output array, y[][], y[0] contains the values of x for the nPoints points.
The subsequent rows of the output array, y[i] where i = 1 to n+1, contains the corresponding values of y for the (i-1)th ODE for the nPoints points.
Otherwise this method is the same as fourthOrder(DerivnFunction g) described above.
See plotting method for obtaining a quick plot of the values calculated by this method.

public double[][] fourthOrder(DerivnFunction g, double[] points)
Usage:                      y = rk.fourthOrder(func, points);
Returns the values of n yi, as y at each of the x points entered via the argument array points for the initial value of x and initial value of y, entered by the relevant setting methods described above, for a set of n ordinary differential equations (ODEs). These points must lie between the initial value of and final value of x entered by the relevant setting methods described above. They may include these initial and final values. If they do not these values will be added to the array of points.
The first row of the output array, y[][], y[0] contains the values of x for the nPoints points.
The second row of the output array y[1] contains the corresponding values of y for the nPoints points.
Otherwise this method is the same as fourthOrder(DerivnFunction g) described above.
See plotting method for obtaining a quick plot of the values calculated by this method.

Static method
public static double[ ] fourthOrder(DerivnFunction g, double x0, double[ ] y0, double xn, double h)
Usage:                      y = RungeKutta.fourthOrder(func, x0, y0, xn, h);
Returns the n values of y[i] at a given value of x (xn) for a set of n ordinary differential equation (ODEs):

given initial values of the n y(i), y0[i], at an initial value of x, x0, the final value of x, xn, the step size h and a reference (pointer) to the instance of the class that codes the function, f(x, y), func.
The method uses a classical fourth order Runge-Kutta procedure hence the step size is fixed. If (xn - x0)/h is not an integer the step size h is first adjusted so that it is.
The mathematical functions to which the derivatives are equal, i.e. the n fi(x, yi), must be coded as a method
public double[ ] derivn(double x, double[ ] y)
The method, derivn, must have the above signature, i.e. name, type and argument list, and be a method in a class which must implement the interface, DerivnFunction. The name of this class is the user's choice. The arguments to the mathematical version of the functions, fi(x, yi, a, b, c . . .) other than x and the n yi, i.e. a, b, c . . . may not be included in the method deriv(x, y) argument list. They should be declared as members of the class of which derivn(x, y) is a member. The function is passed to the RungeKutta.fourthOrder(. . .) method as an instance of the class containing the method derivn(x, y) [func in the above example but this instance may be named at the user's choice]. This instance should be created in the method that calls RungeKutta.fourthOrder(. . .). This procedure can be seen more clearly in example two in Example programs.

public static double[][] fourthOrder(DerivnFunction g, int nPoints, double x0, double y0, double xn, double h)
Usage:                      y = RungeKutta.fourthOrder(func, nPoints, x0, y0, xn, h);
This is the static version of the instance method, rk.fourthOrder(func, nPoints), described above, for obtaining the values of the n y at nPoints equidistant values of x.
See RungeKutta.fourthOrder(func, x0, y0, xn, h) and rk.fourthOrder(func, nPoints) (above) for details of the arguments and returned arrays.

public static double[][] fourthOrder(DerivnFunction g, double[] points, double x0, double y0, double xn, double h)
Usage:                      y = RungeKutta.fourthOrder(func, points, x0, y0, xn, h);
This is the static version of the instance method, rk.fourthOrder(func, points), described above, for obtaining the values of the n y at a user supplied array of values of x.
See RungeKutta.fourthOrder(func, x0, y0, xn, h) and rk.fourthOrder(func, points) (above) for details of the arguments and returned arrays.

FOURTH ORDER RUNGE-KUTTA METHOD
Single Ordinary Differential Equation
Instance methods
public double fourthOrder(DerivFunction g)
Usage:                      y = rk.fourthOrder(func);
Returns the value of yi, as y, at the final value of x for the initial value of x and initial value of y, entered by the relevant setting methods described above, for a single ordinary differential equation (ODE):

The method uses a classical fourth order Runge-Kutta procedure hence the step size is fixed. If (final value of x - initial value of x)/(step size) is not an integer the step size is first adjusted so that it is.
The argument, func, is a reference to the instance of the class that codes the function, f(x, y)reference (pointer) to the instance of the class that codes the function, f(x, y).
The mathematical function to which the derivative is equal, i.e. fi(x, yi), must be coded as a method
public double deriv(double x, double y)
The method, deriv, must have the above signature, i.e. name, type and argument list, and be a method in a class which must implement the interface, DerivnFunction. The name of this class is the user's choice. The arguments to the mathematical version of the functions, fi(x, y, a, b, c . . .) other than x and y, i.e. a, b, c . . . may not be included in the method deriv(x, y) argument list. They should be declared as public members of the class of which deriv(x, y) is a member. The function is passed to the RungeKutta.fourthOrder(. . .) method as an instance of the class containing the method derivn(x, y) [func in the above example but this instance may be named at the user's choice]. This instance should be created in the method that calls RungeKutta.fourthOrder(. . .). This procedure can be seen more clearly in example three in Example programs.

public double[][] fourthOrder(DerivFunction g, int nPoints)
Usage:                      y = rk.fourthOrder(func, nPoints);
Returns the values of yi, as y at nPoints equispaced points between, and including, the initial value of x and the final value of x for the initial value of x and initial value of y, entered by the relevant setting methods described above, for a single ordinary differential equation (ODE).
The first row of the output array, y[][], y[0] contains the values of x for the nPoints points.
The second row of the output array y[1] contains the corresponding values of y for the nPoints points.
Otherwise this method is the same as fourthOrder(DerivFunction g) described above.
See plotting method for obtaining a quick plot of the values calculated by this method.

public double[][] fourthOrder(DerivFunction g, double[] points)
Usage:                      y = rk.fourthOrder(func, points);
Returns the values of yi, as y at each of the x points entered via the argument array points for the initial value of x and initial value of y, entered by the relevant setting methods described above, for a single ordinary differential equation (ODE). These points must lie between the initial value of and final value of x entered by the relevant setting methods described above. They may include these initial and final values. If they do not these values will be added to the array of points.
The first row of the output array, y[][], y[0] contains the values of x for the nPoints points.
The second row of the output array y[1] contains the corresponding values of y for the nPoints points.
Otherwise this method is the same as fourthOrder(DerivFunction g) described above.
See plotting method for obtaining a quick plot of the values calculated by this method.

Static methods
public static double fourthOrder(DerivFunction g, double x0, double y0, double xn, double h)
Usage:                      y = RungeKutta.fourthOrder(func, x0, y0, xn, h);
Returns the value of y at a given value of x (xn) for a single ordinary differential equation (ODE):

given an initial value of y, y0, at an initial value of x, x0, the final value of x, xn, the initial step size h and a reference to the instance of the class that codes the function, f(x, y), func.
The method uses a classical fourth order Runge-Kutta procedure hence the step size is fixed. If (xn - x0)/h is not an integer the step size h is first adjusted so that it is.
The mathematical function to which the derivative is equal, i.e. f(x, y), must be coded as a method
public double deriv(double x, double y)
The method, deriv, must have the above signature, i.e. name, type and argument list, and be a method in a non-public class which must implement the interface, DerivFunction. The name of this class is the user's choice. The arguments to the mathematical version of the function, f(x, y, a, b, c . . .) other than x and y, i.e. a, b, c . . . may not be included in the method deriv(x, y) argument list. They should be declared as members of the class of which deriv(x, y) is a member. The function is passed to the RungeKutta.fehlberg(. . .) method as an instance of the class containing the method deriv(x, y) [func in the above example but this instance may be named at the user's choice]. This instance should be created in the method that calls RungeKutta.fehlberg(. . .). This procedure can be seen more clearly in example one in Example programs .

public static double[][] fourthOrder(DerivFunction g, int nPoints, double x0, double y0, double xn, double h)
Usage:                      y = RungeKutta.fourthOrder(func, nPoints, x0, y0, xn, h);
This is the static version of the instance method, rk.fourthOrder(func, nPoints), described above, for obtaining the values of y at nPoints equidistant values of x.
See RungeKutta.fourthOrder(func, x0, y0, xn, h) and rk.fourthOrder(func, nPoints) (above) for details of the arguments and returned arrays.

public static double[][] fourthOrder(DerivFunction g, double[] points, double x0, double y0, double xn, double h)
Usage:                      y = RungeKutta.fourthOrder(func, points, x0, y0, xn, h);
This is the static version of the instance method, rk.fourthOrder(func, points), described above, for obtaining the values of y at a user supplied array of values of x.
See RungeKutta.fourthOrder(func, x0, y0, xn, h) and rk.fourthOrder(func, points) (above) for details of the arguments and returned arrays.

RUNGE-KUTTA-FEHLBERG METHOD
Set of n Ordinary Differential Equations (See below for n = 1)
Instance method
public double[ ] fehlberg(DerivnFunction g)
Usage:                      y = rk.fehlberg(func);
Returns the n values of yi, as y[i], at the final value of x for the initial value of x and initial values of y, entered by the relevant setting methods described above, for a set of n ordinary differential equation (ODEs):

The method uses Fehlberg's modification of the Runge-Kutta procedure incorporating an adaptive step size.
The argument, func, is a reference to the instance of the class that codes the function, f(x, y)reference (pointer) to the instance of the class that codes the function, f(x, y).
The mathematical functions to which the derivatives are equal, i.e. the n fi(x, yi), must be coded as a method
public double[ ] derivn(double x, double[ ] y)
The method, derivn, must have the above signature, i.e. name, type and argument list, and be a method in a class which must implement the interface, DerivnFunction. The name of this class is the user's choice. The arguments to the mathematical version of the functions, fi(x, yi, a, b, c . . .) other than x and the n yi, i.e. a, b, c . . . may not be included in the method deriv(x, y) argument list. They should be declared as members of the class of which derivn(x, y) is a member. The function is passed to the RungeKutta.fourthOrder(. . .) method as an instance of the class containing the method derivn(x, y) [func in the above example but this instance may be named at the user's choice]. This instance should be created in the method that calls RungeKutta.fourthOrder(. . .). This procedure can be seen more clearly in example four in Example programs.

public double[][] fehlberg(DerivnFunction g, int nPoints)
Usage:                      y = rk.fehlberg(func, nPoints);
Returns the values of yi, as y at nPoints equispaced points between, and including, the initial value of x and the final value of x for the initial value of x and initial value of y, entered by the relevant setting methods described above, for a set of n ordinary differential equation (ODEs).
The first row of the output array, y[][], y[0] contains the values of x for the nPoints points.
The second row of the output array y[1] contains the corresponding values of y for the nPoints points.
Otherwise this method is the same as fehlberg(DerivFunction g) described above.
See plotting method for obtaining a quick plot of the values calculated by this method.

public double[][] fehlberg(DerivnFunction g, double[] points)
Usage:                      y = rk.fehlberg(func, points);
Returns the values of yi, as y at each of the x points entered via the argument array points for the initial value of x and initial value of y, entered by the relevant setting methods described above, for a set of n ordinary differential equation (ODEs). These points must lie between the initial value of and final value of x entered by the relevant setting methods described above. They may include these initial and final values. If they do not these values will be added to the array of points.
The first row of the output array, y[][], y[0] contains the values of x for the nPoints points.
The second row of the output array y[1] contains the corresponding values of y for the nPoints points.
Otherwise this method is the same as fehlberg(DerivFunction g) described above.
See plotting method for obtaining a quick plot of the values calculated by this method.

Static methods
public static double[ ] fehlberg(DerivnFunction g, double x0, double[ ] y0, double xn, double h, double abstol, double reltol, double maxiter)
public static double[ ] fehlberg(DerivnFunction g, double x0, double[ ] y0, double xn, double h, double abstol, double reltol)
Usage:
y = RungeKutta.fehlberg(func, x0, y0, xn, h, abstol, reltol, maxiter);
y = RungeKutta.fehlberg(func, x0, y0, xn, h, abstol, reltol);

Returns the n values of y[i] at a given value of x (xn) for a set of n ordinary differential equation (ODEs):

given initial values of the n y(i), y0[i], at an initial value of x, x0, the final value of x, xn, the initial step size h, an absolute tolerance abstol, a relative tolerance abstol, a maximum number of iterations maxiter and a reference (pointer) to the instance of the class that codes the functions, f(x, y), func.
The step size h is adjusted on each iteration by Fehlberg's procedure. After each iteration the ratio, R, is calculated, where

and y(i) are the n predicted values of the n y(i) after the last step, err(i) are the differences in the n predictions of the n y(i) using a 4th and 5th order Runge-Kutta procedure. If R is greater than one the step is rejected, the value of h is decreased as

and the step repeated. If R is less than one the step is accepted and the value of h is increased by

for the next step. The scaling factor, reltol, is a measure of the required accuracy and a trial value approximating to the reqiuired fractional accuracy in y is suggested. The factor, abstol, is a small number to accommodate the possibility of a zero value in one of the y values precluding its scaling.
If the maximum number of iterations, maxiter, is reached a ArthmeticException is thrown and the program is terminated. If maxiter is not included in the argument it is calculated as multiplier(xn - x0)/h. The multiplier has a default value of 1000 and maybe reset (see method above).
The mathematical functions to which the derivatives are equal, i.e. the n fi(x, yi), must be coded as a method
public double[ ] derivn(double x, double[ ] y)
The method, derivn, must have the above signature, i.e. name, type and argument list, and be a method in a non-public class which must implement the interface, DerivnFunction. The name of this class is the user's choice. The arguments to the mathematical version of the functions, fi(x, yi, a, b, c . . .) other than x and the n yi, i.e. a, b, c . . . may not be included in the method deriv(x, y) argument list. They should be declared as members of the class of which derivn(x, y) is a member. The function is passed to the RungeKutta.fehlberg(. . .) method as an instance of the class containing the method derivn(x, y) [func in the above example but this instance may be named at the user's choice]. This instance should be created in the method that calls RungeKutta.fehlberg(. . .). This procedure can be seen more clearly in example two in Example programs. This example considers the fourth order Runge-Kutta but the programming scheme is equally illustrative of the Fehlberg method. Only the extra parameters, abstol, reltol, (maxiter), need adding and the call statement amending.

public static double[][] fehlberg(DerivnFunction g, int nPoints, double x0, double y0, double xn, double h, double abstol, double reltol, double maxiter)
Usage:                      y = RungeKutta.fehlberg(func, nPoints, x0, y0, xn, h, abstol, reltol, maxiter);
This is a static version of the instance method, rk.fehlberg(func, nPoints), described above, for obtaining the values of y at nPoints equidistant values of x.
See RungeKutta.fehlberg(func, x0, y0, xn, h, abstol, reltol, maxiter) and rk.fehlberg(func, nPoints) (above) for details of the arguments and returned arrays.

public static double[][] fehlberg(DerivnFunction g, double[] points, double x0, double y0, double xn, double h, double abstol, double reltol, double maxiter)
Usage:                      y = RungeKutta.fehlberg(func, points, x0, y0, xn, h, abstol, reltol, maxiter);
This is a static version of the instance method, rk.fehlberg(func, points), described above, for obtaining the values of y at a user supplied array of values of x.
See RungeKutta.fehlberg(func, x0, y0, xn, h, abstol, reltol, maxiter) and rk.fehlberg(func, points) (above) for details of the arguments and returned arrays.

public static double[][] fehlberg(DerivnFunction g, int nPoints, double x0, double y0, double xn, double h, double abstol, double reltol)
Usage:                      y = RungeKutta.fehlberg(func, nPoints, x0, y0, xn, h, abstol, reltol);
This is a static version of the instance method, rk.fehlberg(func, nPoints), described above, for obtaining the values of y at nPoints equidistant values of x.
See RungeKutta.fehlberg(func, x0, y0, xn, h, abstol, reltol) and rk.fehlberg(func, nPoints) (above) for details of the arguments and returned arrays.

public static double[][] fehlberg(DerivFunction g, double[] points, double x0, double y0, double xn, double h, double abstol, double reltol)
Usage:                      y = RungeKutta.fehlberg(func, points, x0, y0, xn, h, abstol, reltol);
This is a static version of the instance method, rk.fehlberg(func, points), described above, for obtaining the values of y at a user supplied array of values of x.
See RungeKutta.fehlberg(func, x0, y0, xn, h, abstol, reltol) and rk.fehlberg(func, points) (above) for details of the arguments and returned arrays.

RUNGE-KUTTA-FEHLBERG METHOD
Single Ordinary Differential Equation
Instance method
public double[ ] fehlberg(DerivFunction g)
Usage:                      y = rk.fehlberg(func);
Returns the value of yi, as y, at the final value of x for the initial value of x and initial value of y, entered by the relevant setting methods described above, for the ordinary differential equation (ODE):

The method uses Fehlberg's modification of the Runge-Kutta procedure incorporating an adaptive step size.
The argument, func, is a reference to the instance of the class that codes the function, f(x, y)reference (pointer) to the instance of the class that codes the function, f(x, y).
The mathematical functions to which the derivative is equal, i.e. fi(x, yi), must be coded as a method
public double deriv(double x, double y)
The method, deriv, must have the above signature, i.e. name, type and argument list, and be a method in a class which must implement the interface, DerivnFunction. The name of this class is the user's choice. The arguments to the mathematical version of the functions, fi(x, y, a, b, c . . .) other than x and the y, i.e. a, b, c . . . may not be included in the method deriv(x, y) argument list. They should be declared as public members of the class of which derivn(x, y) is a member. The function is passed to the RungeKutta.fehlberg(. . .) method as an instance of the class containing the method deriv(x, y) [func in the above example but this instance may be named at the user's choice]. This instance should be created in the method that calls RungeKutta.fehlberg(. . .). This procedure can be seen more clearly in example three in Example programs.

public double[][] fehlberg(DerivFunction g, int nPoints)
Usage:                      y = rk.fehlberg(func, nPoints);
Returns the values of yi, as y at nPoints equispaced points between, and including, the initial value of x and the final value of x for the initial value of x and initial value of y, entered by the relevant setting methods described above, for a single ordinary differential equation (ODE).
The first row of the output array, y[][], y[0] contains the values of x for the nPoints points.
The second row of the output array y[1] contains the corresponding values of y for the nPoints points.
Otherwise this method is the same as fehlberg(DerivFunction g) described above.
See plotting method for obtaining a quick plot of the values calculated by this method.

public double[][] fehlberg(DerivFunction g, double[] points)
Usage:                      y = rk.fehlberg(func, points);
Returns the values of yi, as y at each of the x points entered via the argument array points for the initial value of x and initial value of y, entered by the relevant setting methods described above, for a single ordinary differential equation (ODE). These points must lie between the initial value of and final value of x entered by the relevant setting methods described above. They may include these initial and final values. If they do not these values will be added to the array of points.
The first row of the output array, y[][], y[0] contains the values of x for the nPoints points.
The second row of the output array y[1] contains the corresponding values of y for the nPoints points.
Otherwise this method is the same as fehlbergcashKarp(DerivFunction g) described above.
See plotting method for obtaining a quick plot of the values calculated by this method.

Static methods
public static double fehlberg(DerivFunction g, double x0, double y0, double xn, double h, double abstol, double reltol, double maxiter)
public static double fehlberg(DerivFunction g, double x0, double y0, double xn, double h, double abstol, double reltol)
Usage:
y = RungeKutta.fehlberg(func, x0, y0, xn, h, abstol, reltol, maxiter);
y = RungeKutta.fehlberg(func, x0, y0, xn, h, abstol, reltol);

Returns the value of y at a given value of x (xn) for a single ordinary differential equation (ODE):

given an initial value of y, y0, at an initial value of x, x0, the final value of x, xn, the initial step size h, an absolute tolerance abstol, a relative tolerance abstol, a maximum number of iterations maxiter and a reference (pointer) to the instance of the class that codes the function, f(x, y), func.
The step size h is adjusted on each iteration by Fehlberg's procedure. After each iteration the ratio, R, is calculated where

and y is the predicted value of y after the last step, err is the difference in the prediction of y using a 4th and 5th order Runge-Kutta procedure. If R is greater than one the step is rejected, the value of h is decreased as

and the step repeated. If R is less than one the step is accepted and the value of h is increased by

for the next step. The scaling factor, reltol, is a measure of the required accuracy and a trial value approximating to the reqiuired fractional accuracy in y is suggested. The factor, abstol, is a small number to accommodate the possibility of a zero value in one of the y values precluding its scaling.
If the maximum number of iterations, maxiter, is reached a ArthmeticException is thrown and the program is terminated. If maxiter is not included in the argument it is calculated as multiplier(xn - x0)/h. The multiplier has a default value of 1000 and maybe reset (see method above).
The mathematical function to which the derivative is equal, i.e. f(x, y), must be coded as a method
public double deriv(double x, double y)
The method, deriv, must have the above signature, i.e. name, type and argument list, and be a method in a non-public class which must implement the interface, DerivFunction. The name of this class is the user's choice. The arguments to the mathematical version of the function, f(x, y, a, b, c . . .) other than x and y, i.e. a, b, c . . . may not be included in the method deriv(x, y) argument list. They should be declared as members of the class of which deriv(x, y) is a member. The function is passed to the RungeKutta.fehlberg(. . .) method as an instance of the class containing the method deriv(x, y) [func in the above example but this instance may be named at the user's choice]. This instance should be created in the method that calls RungeKutta.fehlberg(. . .). This instance should be created in the method that calls RungeKutta.fehlberg(. . .). This procedure can be seen more clearly in example one in Example programs. This example considers the fourth order Runge-Kutta but the programming scheme is equally illustrative of the Fehlberg method. Only the extra parameters, abstol, reltol, (maxiter), need adding and the call statement amending.

public static double[][] fehlberg(DerivFunction g, int nPoints, double x0, double y0, double xn, double h, double abstol, double reltol, double maxiter)
Usage:                      y = RungeKutta.fehlberg(func, nPoints, x0, y0, xn, h, abstol, reltol, maxiter);
This is a static version of the instance method, rk.fehlberg(func, nPoints), described above, for obtaining the values of y at nPoints equidistant values of x.
See RungeKutta.fehlberg(func, x0, y0, xn, h, abstol, reltol, maxiter) and rk.fehlberg(func, nPoints) (above) for details of the arguments and returned arrays.

public static double[][] fehlberg(DerivFunction g, double[] points, double x0, double y0, double xn, double h, double abstol, double reltol, double maxiter)
Usage:                      y = RungeKutta.fehlberg(func, points, x0, y0, xn, h, abstol, reltol, maxiter);
This is a static version of the instance method, rk.fehlberg(func, points), described above, for obtaining the values of y at a user supplied array of values of x.
See RungeKutta.fehlberg(func, x0, y0, xn, h, abstol, reltol, maxiter) and rk.fehlberg(func, points) (above) for details of the arguments and returned arrays.

public static double[][] fehlberg(DerivFunction g, int nPoints, double x0, double y0, double xn, double h, double abstol, double reltol)
Usage:                      y = RungeKutta.fehlberg(func, nPoints, x0, y0, xn, h, abstol, reltol);
This is a static version of the instance method, rk.fehlberg(func, nPoints), described above, for obtaining the values of y at nPoints equidistant values of x.
See RungeKutta.fehlberg(func, x0, y0, xn, h, abstol, reltol) and rk.fehlberg(func, nPoints) (above) for details of the arguments and returned arrays.

public static double[][] fehlberg(DerivFunction g, double[] points, double x0, double y0, double xn, double h, double abstol, double reltol)
Usage:                      y = RungeKutta.fehlberg(func, points, x0, y0, xn, h, abstol, reltol);
This is a static version of the instance method, rk.fehlberg(func, points), described above, for obtaining the values of y at a user supplied array of values of x.
See RungeKutta.fehlberg(func, x0, y0, xn, h, abstol, reltol) and rk.fehlberg(func, points) (above) for details of the arguments and returned arrays.

RUNGE-KUTTA-CASH-KARP METHOD
Set of n Ordinary Differential Equations (See below for n = 1)
Instance method
public double[] cashKarp(DerivnFunction g)
Usage:                      y = rk.cashKarp(func);
Returns the n values of yi, as y[i], at the final value of x for the initial value of x and initial values of y, entered by the relevant setting methods described above, for a set of n ordinary differential equation (ODEs):

The method uses Cash and Karp's modification of the Runge-Kutta procedure incorporating an adaptive step size.
The argument, func, is a reference to the instance of the class that codes the function, f(x, y)reference (pointer) to the instance of the class that codes the function, f(x, y).
The mathematical functions to which the derivatives are equal, i.e. the n fi(x, yi), must be coded as a method
public double[ ] derivn(double x, double[ ] y)
The method, derivn, must have the above signature, i.e. name, type and argument list, and be a method in a class which must implement the interface, DerivnFunction. The name of this class is the user's choice. The arguments to the mathematical version of the functions, fi(x, yi, a, b, c . . .) other than x and the n yi, i.e. a, b, c . . . may not be included in the method deriv(x, y) argument list. They should be declared as members of the class of which derivn(x, y) is a member. The function is passed to the RungeKutta.cashKarp(. . .) method as an instance of the class containing the method derivn(x, y) [func in the above example but this instance may be named at the user's choice]. This instance should be created in the method that calls RungeKutta.cashKarp(. . .). This procedure can be seen more clearly in example four in Example programs.

public double[][] cashKarp(DerivnFunction g, int nPoints)
Usage:                      y = rk.cashKarp(func, nPoints);
Returns the values of yi, as y at nPoints equispaced points between, and including, the initial value of x and the final value of x for the initial value of x and initial value of y, entered by the relevant setting methods described above, for a set of n ordinary differential equation (ODEs).
The first row of the output array, y[][], y[0] contains the values of x for the nPoints points.
The second row of the output array y[1] contains the corresponding values of y for the nPoints points.
Otherwise this method is the same as cashKarp(DerivFunction g) described above.
See plotting method for obtaining a quick plot of the values calculated by this method.

public double[][] cashKarp(DerivnFunction g, double[] points)
Usage:                      y = rk.cashKarp(func, points);
Returns the values of yi, as y at each of the x points entered via the argument array points for the initial value of x and initial value of y, entered by the relevant setting methods described above, for a set of n ordinary differential equation (ODEs). These points must lie between the initial value of and final value of x entered by the relevant setting methods described above. They may include these initial and final values. If they do not these values will be added to the array of points.
The first row of the output array, y[][], y[0] contains the values of x for the nPoints points.
The second row of the output array y[1] contains the corresponding values of y for the nPoints points.
Otherwise this method is the same as cashKarp(DerivFunction g) described above.
See plotting method for obtaining a quick plot of the values calculated by this method.

Static methods
public static double[] cashKarp(DerivnFunction g, double x0, double y0, double xn, double h, double abstol, double reltol, double maxiter)
public static double[] cashKarp(DerivnFunction g, double x0, double y0, double xn, double h, double abstol, double reltol)
Usage:
y = RungeKutta.cashKarp(func, x0, y0, xn, h, abstol, reltol, maxiter);
y = RungeKutta.cashKarp(func, x0, y0, xn, h, abstol, reltol);

Returns the values of y at a given value of x (xn) for a set of n ordinary differential equation (ODEs):

This method is essentially the same as the Fehlberg method (see above). The coefficients derived by Cash and Karp differ from those of Fehlberg and are said to give a more efficient method. The programming instructions are identical to those listed under the Fehlberg method above.

public static double[][] cashKarp(DerivnFunction g, int nPoints, double x0, double y0, double xn, double h, double abstol, double reltol, double maxiter)
Usage:                      y = RungeKutta.cashKarp(func, nPoints, x0, y0, xn, h, abstol, reltol, maxiter);
This is a static version of the instance method, rk.cashKarp(func, nPoints), described above, for obtaining the values of y at nPoints equidistant values of x.
See RungeKutta.cashKarp(func, x0, y0, xn, h, abstol, reltol, maxiter) and rk.cashKarp(func, nPoints) (above) for details of the arguments and returned arrays.

public static double[][] cashKarp(DerivnFunction g, double[] points, double x0, double y0, double xn, double h, double abstol, double reltol, double maxiter)
Usage:                      y = RungeKutta.cashKarp(func, points, x0, y0, xn, h, abstol, reltol, maxiter);
This is a static version of the instance method, rk.cashKarp(func, points), described above, for obtaining the values of y at a user supplied array of values of x.
See RungeKutta.cashKarp(func, x0, y0, xn, h, abstol, reltol, maxiter) and rk.cashKarp(func, points) (above) for details of the arguments and returned arrays.

public static double[][] cashKarp(DerivnFunction g, int nPoints, double x0, double y0, double xn, double h, double abstol, double reltol)
Usage:                      y = RungeKutta.cashKarp(func, nPoints, x0, y0, xn, h, abstol, reltol);
This is a static version of the instance method, rk.cashKarp(func, nPoints), described above, for obtaining the values of y at nPoints equidistant values of x.
See RungeKutta.cashKarp(func, x0, y0, xn, h, abstol, reltol) and rk.cashKarp(func, nPoints) (above) for details of the arguments and returned arrays.

public static double[][] cashKarp(DerivFunction g, double[] points, double x0, double y0, double xn, double h, double abstol, double reltol)
Usage:                      y = RungeKutta.cashKarp(func, points, x0, y0, xn, h, abstol, reltol);
This is a static version of the instance method, rk.cashKarp(func, points), described above, for obtaining the values of y at a user supplied array of values of x.
See RungeKutta.cashKarp(func, x0, y0, xn, h, abstol, reltol) and rk.cashKarp(func, points) (above) for details of the arguments and returned arrays.

RUNGE-KUTTA-CASH-KARP METHOD
Single Ordinary Differential Equation
Instance method
public double cashKarp(DerivFunction g)
Usage:                      y = rk.cashKarp(func);
Returns the value of yi, as y, at the final value of x for the initial value of x and initial value of y, entered by the relevant setting methods described above, for the ordinary differential equation (ODE):

The method uses Cash and Karp's modification of the Runge-Kutta procedure incorporating an adaptive step size.
The argument, func, is a reference to the instance of the class that codes the function, f(x, y)reference (pointer) to the instance of the class that codes the function, f(x, y).
The mathematical functions to which the derivative is equal, i.e. fi(x, yi), must be coded as a method
public double deriv(double x, double y)
The method, deriv, must have the above signature, i.e. name, type and argument list, and be a method in a class which must implement the interface, DerivnFunction. The name of this class is the user's choice. The arguments to the mathematical version of the functions, fi(x, y, a, b, c . . .) other than x and the y, i.e. a, b, c . . . may not be included in the method deriv(x, y) argument list. They should be declared as public members of the class of which derivn(x, y) is a member. The function is passed to the RungeKutta.cashKarp(. . .) method as an instance of the class containing the method deriv(x, y) [func in the above example but this instance may be named at the user's choice]. This instance should be created in the method that calls RungeKutta.cashKarp(. . .). This procedure can be seen more clearly in the example four in Example programs.

public double[][] cashKarp(DerivFunction g, int nPoints)
Usage:                      y = rk.cashKarp(func, nPoints);
Returns the values of yi, as y at nPoints equispaced points between, and including, the initial value of x and the final value of x for the initial value of x and initial value of y, entered by the relevant setting methods described above, for a single ordinary differential equation (ODE).
The first row of the output array, y[][], y[0] contains the values of x for the nPoints points.
The second row of the output array y[1] contains the corresponding values of y for the nPoints points.
Otherwise this method is the same as cashKarp(DerivFunction g) described above.
See plotting method for obtaining a quick plot of the values calculated by this method.

public double[][] cashKarp(DerivFunction g, double[] points)
Usage:                      y = rk.cashKarp(func, points);
Returns the values of yi, as y at each of the x points entered via the argument array points for the initial value of x and initial value of y, entered by the relevant setting methods described above, for a single ordinary differential equation (ODE). These points must lie between the initial value of and final value of x entered by the relevant setting methods described above. They may include these initial and final values. If they do not these values will be added to the array of points.
The first row of the output array, y[][], y[0] contains the values of x for the nPoints points.
The second row of the output array y[1] contains the corresponding values of y for the nPoints points.
Otherwise this method is the same as cashKarp(DerivFunction g) described above.
See plotting method for obtaining a quick plot of the values calculated by this method.

Static methods
public static double cashKarp(DerivFunction g, double x0, double y0, double xn, double h, double abstol, double reltol, double maxiter)
public static double cashKarp(DerivFunction g, double x0, double y0, double xn, double h, double abstol, double reltol)
Usage:
y = RungeKutta.cashKarp(func, x0, y0, xn, h, abstol, reltol, maxiter);
y = RungeKutta.cashKarp(func, x0, y0, xn, h, abstol, reltol);

Returns the value of y at a given value of x (xn) for a single ordinary differential equation (ODE):

This method is essentially the same as the Fehlberg method (see above). The coefficients derived by Cash and Karp differ from those of Fehlberg and are said to give a more efficient method. The programming instructions are identical to those listed under the Fehlberg method above.

public static double[][] cashKarp(DerivFunction g, int nPoints, double x0, double y0, double xn, double h, double abstol, double reltol, double maxiter)
Usage:                      y = RungeKutta.cashKarp(func, nPoints, x0, y0, xn, h, abstol, reltol, maxiter);
This is a static version of the instance method, rk.cashKarp(func, nPoints), described above, for obtaining the values of y at nPoints equidistant values of x.
See RungeKutta.cashKarp(func, x0, y0, xn, h, abstol, reltol, maxiter) and rk.cashKarp(func, nPoints) (above) for details of the arguments and returned arrays.

public static double[][] cashKarp(DerivFunction g, double[] points, double x0, double y0, double xn, double h, double abstol, double reltol, double maxiter)
Usage:                      y = RungeKutta.cashKarp(func, points, x0, y0, xn, h, abstol, reltol, maxiter);
This is a static version of the instance method, rk.cashKarp(func, points), described above, for obtaining the values of y at a user supplied array of values of x.
See RungeKutta.cashKarp(func, x0, y0, xn, h, abstol, reltol, maxiter) and rk.cashKarp(func, points) (above) for details of the arguments and returned arrays.

public static double[][] cashKarp(DerivFunction g, int nPoints, double x0, double y0, double xn, double h, double abstol, double reltol)
Usage:                      y = RungeKutta.cashKarp(func, nPoints, x0, y0, xn, h, abstol, reltol);
This is a static version of the instance method, rk.cashKarp(func, nPoints), described above, for obtaining the values of y at nPoints equidistant values of x.
See RungeKutta.cashKarp(func, x0, y0, xn, h, abstol, reltol) and rk.cashKarp(func, nPoints) (above) for details of the arguments and returned arrays.

public static double[][] cashKarp(DerivFunction g, double[] points, double x0, double y0, double xn, double h, double abstol, double reltol)
Usage:                      y = RungeKutta.cashKarp(func, points, x0, y0, xn, h, abstol, reltol);
This is a static version of the instance method, rk.cashKarp(func, points), described above, for obtaining the values of y at a user supplied array of values of x.
See RungeKutta.cashKarp(func, x0, y0, xn, h, abstol, reltol) and rk.cashKarp(func, points) (above) for details of the arguments and returned arrays.

### RETURN ALL x AND y VALUES

public double[][] getAllValues()
Usage:                      all = rk.getAllValues();
This method returns all the x and y values between, and including, the initial and final values as calculated for all steps in the last call to any of the Runge-Kutta non-static methods. The values are returned in an array, e.g. all[][], in which the first row, e.g. all[0], contains the x values for all the steps. This array is useful in examining the step sizes used in the adaptive step procedures, Cash-Karp-Runge-Kutta and Fehlberg-Runge-Kutta. The subsequent rows contain the y values calculated at each step in the Runge-Kutta procedures; each row for a separate ODE, e.g. all[1] contains the y values calculated for a single ODE or the first ODE in a method handling several ODEs.

### PLOT A MULTIPOINT OUTPUT

public void plot()
Usage:                      y = rk.plot();
This method displays a plot of the calculated y values against the x values for the last call to a non-static method providing an output of calculated y values for more than one point. An exception is thrown if this method is called and there has been no previous call to a non-static method providing an output of calculated y values for more than one point.

### PLOT ALL STEP VALUES

public void plotSteps()
Usage:                      y = rk.plotSteps();
This method displays a plot of the calculated y values against the x values for all the steps in the Runge-Kutta procedure for the last call to a non-static method. This plot is useful in examining the step sizes used in the adaptive step procedures, Cash-Karp-Runge-Kutta and Fehlberg-Runge-Kutta.

### EXAMPLE PROGAMS

Four sample programs are given below:

Illustrating instance methods
Program RungeKuttaExampleThree
RungeKuttaExampleThree illustrates the use of a fourth order Runge-Kutta, Cash-Karp and Fehlberg procedures with a single ordinary differential equation
RungeKuttaExampleThree.java

Program RungeKuttaExampleFour
RungeKuttaExampleFour.java illustrates the use the fourth order Runge-Kutta, Cash-Karp and Fehlberg procedures with a set of three ordinary differential equation.
RungeKuttaExampleFour.java

Illustrating static methods
Program RungeKuttaExampleOne
RungeKuttaExampleOne illustrates the use of a fourth order Runge-Kutta, Cash-Karp and Fehlberg procedures with a single ordinary differential equation
RungeKuttaExampleOne.java

Program RungeKuttaExampleTwo
RungeKuttaExampleTwo.java illustrates the use the fourth order Runge-Kutta, Cash-Karp and Fehlberg procedures with a set of three ordinary differential equation.
RungeKuttaExampleTwo.java

### OTHER CLASSES USED BY THIS CLASS

This class uses the following classes in this library: