类 NewtonRaphson

java.lang.Object
cn.nkpro.elcube.utils.xirr.NewtonRaphson

public class NewtonRaphson extends Object
Simple implementation of the Newton-Raphson method for finding roots or inverses of a function.

The function and its derivative must be supplied as instances of DoubleUnaryOperator and the answers are computed as doubles.

For examples of usage, see the source of the test class or the Xirr class.

The iterations parameter is used as an upper bound on the number of iterations to run the method for.

The tolerance parameter is used to determine when the method has been successful. If the value of the function at the candidate input is within the tolerance of the desired target value, the method terminates.

  • 字段详细资料

    • TOLERANCE

      public static final double TOLERANCE
      Default tolerance.
      另请参阅:
      常量字段值
  • 构造器详细资料

    • NewtonRaphson

      public NewtonRaphson(DoubleUnaryOperator func, DoubleUnaryOperator derivative, double tolerance, long iterations)
      Construct an instance of the NewtonRaphson method for masochists who do not want to use builder().
      参数:
      func - the function
      derivative - the derivative of the function
      tolerance - the tolerance
      iterations - maximum number of iterations
  • 方法详细资料

    • builder

      public static NewtonRaphson.Builder builder()
      Convenience method for getting an instance of a NewtonRaphson.Builder.
      返回:
      new Builder
    • findRoot

      public double findRoot(double guess)
      Equivalent to inverse(0, guess).

      Find a root of the function starting at the given guess. Equivalent to invoking inverse(0, guess). Finds the input value x such that |f(x)| < tolerance.

      参数:
      guess - the value to start at
      返回:
      an input to the function which yields zero within the given tolerance
      另请参阅:
      inverse(double, double)
    • inverse

      public double inverse(double target, double guess)
      Find the input value to the function which yields the given target, starting at the guess. More precisely, finds an input value x such that |f(x) - target| < tolerance
      参数:
      target - the target value of the function
      guess - value to start the algorithm with
      返回:
      the inverse of the function at target within the given tolerance
      抛出:
      ZeroValuedDerivativeException - if the derivative is 0 while executing the Newton-Raphson method
      OverflowException - when a value involved is infinite or NaN
      NonconvergenceException - if the method fails to converge in the given number of iterations