Find a zero of a continuous function using Brent's method.
Finds a zero of a continuous function
var brentq = require( '@stdlib/optimize/base/brentq' );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.0The 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.
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