/* @flow */ import sign from "./sign"; /** * [Bisection method](https://en.wikipedia.org/wiki/Bisection_method) is a root-finding * method that repeatedly bisects an interval to find the root. * * This function returns a numerical approximation to the exact value. * * @param {Function} func input function * @param {number} start - start of interval * @param {number} end - end of interval * @param {number} maxIterations - the maximum number of iterations * @param {number} errorTolerance - the error tolerance * @returns {number} estimated root value * @throws {TypeError} Argument func must be a function * * @example * bisect(Math.cos,0,4,100,0.003); // => 1.572265625 */ function bisect( func /*: (x: any) => number */, start /*: number */, end /*: number */, maxIterations /*: number */, errorTolerance /*: number */ ) /*:number*/ { if (typeof func !== "function") throw new TypeError("func must be a function"); for (let i = 0; i < maxIterations; i++) { const output = (start + end) / 2; if ( func(output) === 0 || Math.abs((end - start) / 2) < errorTolerance ) { return output; } if (sign(func(output)) === sign(func(start))) { start = output; } else { end = output; } } throw new Error("maximum number of iterations exceeded"); } export default bisect;