Math::Calculus::NewtonRaphson(3pm)

Math::Calculus::NewtonRaphson3pm

Algebraic newton raphson implementation

Usage

  use Math::Calculus::NewtonRaphson;
  # Create an object.
  my $exp = Math::Calculus::NewtonRaphson->new;
  
  # Set a variable and expression.
  $exp->addVariable('x');
  $exp->setExpression('x^2 - 5') or die $exp->getError;
  
  # Apply Newton Raphson.
  my $result = $exp->newtonRaphson(2) or die $exp->getError;
  print $result; # Prints 1.4142...

Overview

This module can take an algebraic expression, parses it and then uses the Newton Raphson method to solve the it. The Newton Raphson method relies on the fact that the expression you pass in evaluates to zero where there is a solution. That is, to solve:-

x^2 = 5

You would need to pass in:-

x^2 - 5

It understands expressions containing any of the operators +, -, *, / and ^ (raise to power), bracketed expressions to enable correct precedence and the functions ln, exp, sin, cos, tan, sec, cosec, cot, sinh, cosh, tanh, sech, cosech, coth, asin, acos, atan, asinh, acosh and atanh.

Export

None by default.

Methods

new
  $exp = Math::Calculus::NewtonRaphson->new;

Creates a new instance of the Newton Raphson object, which can hold an individual expression.

addVariable
  $exp->addVariable('x');

Sets a certain named value in the expression as being a variable. A named value must be an alphabetic character.

setExpression
  $exp->setExpression('x^2 + 5*x);

Takes an expression in human-readable form and stores it internally as a tree structure, checking it is a valid expression that the module can understand in the process. Note that the engine is strict about syntax. For example, note above that you must write 5*x and not just 5x. Whitespace is allowed in the expression, but does not have any effect on precedence. If you require control of precedence, use brackets; bracketed expressions will always be evaluated first, as you would normally expect. The module follows the BODMAS precedence convention. Returns undef on failure and a true value on success.

getExpression
  $expr = $exp->getExpression;

Returns a textaul, human readable representation of the expression that is being stored.

newtonRaphson
  $result = $exp->newtonRaphson($variable, $guess, %mappings);

Attempts to solve the expression for the given variable using the Newton Raphson method, using the passed value as the first guess. The mappings hash allows any other non-numeric constants to be mapped to numeric values - a pre-requisite for solving such equations.

getTraceback
  $exp->getTraceback;

When setExpression and newtonRaphson are called, a traceback is generated to describe what these functions did. If an error occurs, this traceback can be extremely useful in helping track down the source of the error.

getError
  $exp->getError;

When any method other than getTraceback is called, the error message stored is cleared, and then any errors that occur during the execution of the method are stored. If failure occurs, call this method to get a textual representation of the error.

Releases

9 releases since Debian 5.0 (Lenny); the text changed 3 times.

  1. 5.0–9
  2. 10
  3. 11
  4. 12–13

Author

Jonathan Worthington, <[email protected]>

From libmath-calculus-newtonraphson-perl 0.1-4 in Debian 13 (Trixie). Parsed by carta from the page's man source.