Skip to content

Latest commit

 

History

History
117 lines (75 loc) · 2.63 KB

File metadata and controls

117 lines (75 loc) · 2.63 KB

brentq

Find a zero of a continuous function using Brent's method.

Finds a zero of a continuous function $f$ on the interval $\lbrack a, b \rbrack$ where the sign of $f(a)$ and $f(b)$ must be opposite.

Usage

var brentq = require( '@stdlib/optimize/base/brentq' );

brentq( f, a, b, maxIter, xtol, rtol, args )

Finds a zero of a continuous function f on the interval [a, b].

function f( x ) {
    return ( x * x ) - 1.0;
}

var out = brentq( f, 0.0, 2.0, 100, 2.0e-12, 8.88e-16, [] );
var root = out.root;
// returns 1.0

The function returns a solution object with the following properties:

  • root: the root.
  • iterations: number of iterations.
  • function_calls: number of function calls.
  • converged: boolean indicating if convergence was reached.
  • flag: convergence condition message.
  • method: method name.

The function has the following parameters:

  • f: objective function f(x, ...args).
  • a: lower bound.
  • b: upper bound.
  • maxIter: maximum number of iterations.
  • xtol: absolute tolerance.
  • rtol: relative tolerance.
  • args: array of arguments to be passed to f.

Examples

var sin = require( '@stdlib/math/base/special/sin' );
var brentq = require( '@stdlib/optimize/base/brentq' );

function f( x ) {
    return sin( x );
}

var out = brentq( f, 3.0, 3.2, 100, 2e-12, 8.88e-16, [] );
var root = out.root;

console.log( root );
// => 3.141592653589793