-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
68 lines (55 loc) · 1.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
module.exports = newtonRaphson;
function newtonRaphson (f, fp, x0, options) {
var x1, y, yp, tol, maxIter, iter, yph, ymh, yp2h, ym2h, h, hr, verbose, eps;
// Iterpret variadic forms:
if (typeof fp !== 'function') {
options = x0;
x0 = fp;
fp = null;
}
options = options || {};
tol = options.tolerance === undefined ? 1e-7 : options.tolerance;
eps = options.epsilon === undefined ? 2.220446049250313e-16 : options.epsilon;
maxIter = options.maxIterations === undefined ? 20 : options.maxIterations;
h = options.h === undefined ? 1e-4 : options.h;
verbose = options.verbose === undefined ? false : options.verbose;
hr = 1 / h;
iter = 0;
while (iter++ < maxIter) {
// Compute the value of the function:
y = f(x0);
if (fp) {
yp = fp(x0);
} else {
// Needs numerical derivatives:
yph = f(x0 + h);
ymh = f(x0 - h);
yp2h = f(x0 + 2 * h);
ym2h = f(x0 - 2 * h);
yp = ((ym2h - yp2h) + 8 * (yph - ymh)) * hr / 12;
}
// Check for badly conditioned update (extremely small first deriv relative to function):
if (Math.abs(yp) <= eps * Math.abs(y)) {
if (verbose) {
console.log('Newton-Raphson: failed to converged due to nearly zero first derivative');
}
return false;
}
// Update the guess:
x1 = x0 - y / yp;
// Check for convergence:
if (Math.abs(x1 - x0) <= tol * Math.abs(x1)) {
if (verbose) {
console.log('Newton-Raphson: converged to x = ' + x1 + ' after ' + iter + ' iterations');
}
return x1;
}
// Transfer update to the new guess:
x0 = x1;
}
if (verbose) {
console.log('Newton-Raphson: Maximum iterations reached (' + maxIter + ')');
}
return false;
}