Skip to content

Commit

Permalink
backtrack if overshooting
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Jan 2, 2020
1 parent 10833cc commit 559e3c4
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/newton.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@ export function solve(f, y, x) {
// Solve f(a,b) = [x,y]
export function solve2d(f, MAX_ITERATIONS = 40, eps = epsilon2) {
return function(x, y, a = 0, b = 0) {
var tx, ty;
var err2, da, db;
for (var i = 0; i < MAX_ITERATIONS; i++) {
var p = f(a, b);
// diffs
tx = p[0] - x;
ty = p[1] - y;
var p = f(a, b),
// diffs
tx = p[0] - x,
ty = p[1] - y;
if (abs(tx) < eps && abs(ty) < eps) break; // we're there!

// backtrack if we overshot
var h = tx * tx + ty * ty;
if (h > err2) {
a -= da /= 2;
b -= db /= 2;
continue;
}
err2 = h;

// partial derivatives
var ea = (a > 0 ? -1 : 1) * eps,
eb = (b > 0 ? -1 : 1) * eps,
Expand All @@ -39,9 +48,9 @@ export function solve2d(f, MAX_ITERATIONS = 40, eps = epsilon2) {
// determinant
D = dyb * dxa - dya * dxb,
// newton step — or half-step for small D
l = (abs(D) < 0.5 ? 0.5 : 1) / D,
da = (ty * dxb - tx * dyb) * l,
db = (tx * dya - ty * dxa) * l;
l = (abs(D) < 0.5 ? 0.5 : 1) / D;
da = (ty * dxb - tx * dyb) * l;
db = (tx * dya - ty * dxa) * l;
a += da;
b += db;
if (abs(da) < eps && abs(db) < eps) break; // we're crawling
Expand Down

0 comments on commit 559e3c4

Please sign in to comment.