UNPKG

newton-raphson

Version:

JavaScript implementation of the Newton-Raphson method for approximating roots of a real-valued function

36 lines (31 loc) 993 B
(function (root, factory) { if (typeof define === 'function' && define.amd) { define([], factory); } else if (typeof exports === 'object') { module.exports = factory(); } else { root.newtonRaphson = factory(); } }(this, function () { return function findRoot(f, fprime, guess, options) { options = options || {}; var tolerance = options.tolerance || 0.00000001; var epsilon = options.epsilon || 0.0000000000001; var maxIterations = options.maxIterations || 20; var haveWeFoundSolution = false; var newtonX; for (var i = 0; i < maxIterations; ++i) { var denominator = fprime(guess); if (Math.abs(denominator) < epsilon) { return false } result = guess - (f(guess) / denominator); var resultWithinTolerance = Math.abs(result - guess) < tolerance; if (resultWithinTolerance) { return result } guess = result; } return false; } }));