mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-19 14:10:14 -05:00
Improve precision of Numerical.findRoot()
This commit is contained in:
parent
8395541298
commit
6654dca6bf
1 changed files with 7 additions and 4 deletions
|
@ -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;
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue