Improve precision of Numerical.findRoot()

This commit is contained in:
Jürg Lehni 2013-12-30 14:18:33 +01:00
parent 8395541298
commit 6654dca6bf

View file

@ -119,13 +119,13 @@ var Numerical = new function() {
findRoot: function(f, df, x, a, b, n, tolerance) {
for (var i = 0; i < n; i++) {
var fx = f(x),
dx = fx / df(x);
// Calculate a new candidate with the Newton-Raphson method.
dx = fx / df(x),
nx = x - dx;
// See if we can trust the Newton-Raphson result. If not we use
// bisection to find another candiate for Newton's method.
if (abs(dx) < tolerance)
return x;
// Generate a candidate for Newton's method.
var nx = x - dx;
return nx;
// Update the root-bounding interval and test for containment of
// the candidate. If candidate is outside the root-bounding
// interval, use bisection instead.
@ -140,6 +140,9 @@ var Numerical = new function() {
x = nx >= b ? 0.5 * (a + b) : nx;
}
}
// Return the best result even though we haven't gotten close
// enough to the root... (In paper.js this never seems to happen).
return x;
},
/**