SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.analysis.differentiation
Class Ridders

java.lang.Object
  extended by com.numericalmethod.suanshu.analysis.differentiation.Ridders
All Implemented Interfaces:
Function, RealScalarFunction

public class Ridders
extends java.lang.Object
implements RealScalarFunction

This class computes the numerical derivative of a function using Ridders' method. This method in general gives a higher precision than the simple finite differencing method, c.f., FiniteDifference.

Ridders' method tries a sequence of decreasing h's to compute the derivatives, and then extrapolate to zero using Neville's algorithm.

The choice of the initial h is critical. If h is too big, the value computed could be inaccurate. If h is too small, due to rounding error, we might be computing the "same" value over and over again for different h's.

See Also:
"Ridders. C. J. F. "Accurate computation of F'(x) and F'(x)F''(x)," Adv. Eng. Sftw., 4, 75-76. 1982."

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.numericalmethod.suanshu.analysis.function.Function
Function.EvaluationException
 
Field Summary
 RealScalarFunction f
          the function to take the derivative of
static int N_POINTS_4_EXTRAPOLATION
          the default number of points for extrapolation
 int nPoints4Extrapolation
          the number of points for extrapolation
 int order
          the order of the derivative
 double rate
          the rate at which h decreases
 
Constructor Summary
Ridders(RealScalarFunction f, int[] varidx)
          Construct the derivative function of a real vector-valued function using Ridder's method.
Ridders(RealScalarFunction f, int[] varidx, double rate, int nPoints4Extrapolation)
          Construct the derivative function of a real vector-valued function using Ridder's method.
Ridders(UnivariateRealFunction f, int order)
          Construct the derivative function of a univariate function using Ridder's method.
Ridders(UnivariateRealFunction f, int order, double rate, int nPoints4Extrapolation)
          Construct the derivative function of a univariate function using Ridder's method.
 
Method Summary
 int dimension4Domain()
          Get the number of variables of the function.
 int dimension4Range()
          Get the dimension of the range space the function.
 double evaluate(double... x)
          A real function, f, takes a double array double[], {x0, x1, ..., xn} and maps it to a double, f(x0, x1, ..., xn)
 double evaluate(double[] x, double h)
          Numerically evaluate the derivative of f at point x with step size h.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

N_POINTS_4_EXTRAPOLATION

public static final int N_POINTS_4_EXTRAPOLATION
the default number of points for extrapolation

See Also:
Constant Field Values

f

public final RealScalarFunction f
the function to take the derivative of


order

public final int order
the order of the derivative


rate

public final double rate
the rate at which h decreases


nPoints4Extrapolation

public final int nPoints4Extrapolation
the number of points for extrapolation

Constructor Detail

Ridders

public Ridders(UnivariateRealFunction f,
               int order,
               double rate,
               int nPoints4Extrapolation)
Construct the derivative function of a univariate function using Ridder's method.

Parameters:
f - the UnivariateRealFunction to take derivative of
order - the order of differentiation
rate - the rate at which the increment h decreases; rate should be a simple number such as 0.75, not like 0.66666666666...
nPoints4Extrapolation - number of points used for extrapolation

Ridders

public Ridders(UnivariateRealFunction f,
               int order)
Construct the derivative function of a univariate function using Ridder's method.

Parameters:
f - the UnivariateRealFunction to take derivative of
order - the order of the derivative

Ridders

public Ridders(RealScalarFunction f,
               int[] varidx,
               double rate,
               int nPoints4Extrapolation)
Construct the derivative function of a real vector-valued function using Ridder's method.

For example, varidx = new int[]{1, 2} means

           ∂f
 fx1,x2 = -------
          ∂x2∂x1

    ∂    ∂
 = ---( ---- )
   ∂x2   ∂x1
 

The indices count from 1 up to the number of variables of f, i.e., the domain dimension of f.

Parameters:
f - the real multivariate function to take derivative of
varidx - specify the variable indices, numbering from 1 up to the domain dimension of f
rate - rate should be a simple number, not like 0.66666666666...
nPoints4Extrapolation - number of points used for extrapolation

Ridders

public Ridders(RealScalarFunction f,
               int[] varidx)
Construct the derivative function of a real vector-valued function using Ridder's method.

For example, varidx = new int[]{1, 2} means

           ∂f
 fx1,x2 = -------
          ∂x2∂x1

    ∂    ∂
 = ---( ---- )
   ∂x2   ∂x1
 

The indices count from 1 up to the number of variables of f, i.e., the domain dimension of f.

Parameters:
f - the real multivariate function to take derivative of
varidx - specify the variable indices, numbering from 1 up to the domain dimension of f
Method Detail

evaluate

public double evaluate(double... x)
A real function, f, takes a double array double[],
{x0, x1, ..., xn}
and maps it to a double,
f(x0, x1, ..., xn)

Make sure that h and x+h are representable in floating point precision so that the difference between x+h and x is exactly h, the step size.

Specified by:
evaluate in interface RealScalarFunction
Parameters:
x - the point to evaluate the derivative of f at
Returns:
the numerical derivative of f at point x using Ridders' method
See Also:
Wikipedia: Practical considerations

evaluate

public double evaluate(double[] x,
                       double h)
Numerically evaluate the derivative of f at point x with step size h.

Automatically determining the step size h could be challenging, esp. for when |x| is near 0. An analysis that involves f' and f'' may be needed to determine the step size. User may want to experiment with different hs by calling this function.

Parameters:
x - the point to evaluate f at
h - step size
Returns:
the numerical derivative of f at point x with step size h using Ridders' method

dimension4Domain

public int dimension4Domain()
Description copied from interface: Function
Get the number of variables of the function.

For example, for a univariate function, the domain dimension is 1. For a bivariate function, the domain dimension is 2.

Specified by:
dimension4Domain in interface Function
Returns:
the number of variables

dimension4Range

public int dimension4Range()
Description copied from interface: Function
Get the dimension of the range space the function.

For example, for a Rn->Rm function, the dimension of the range is m.

Specified by:
dimension4Range in interface Function
Returns:
the dimension of the range

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.