Published
Edited
Nov 24, 2018
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function nicolosiInverse(x, y) {
const x2 = x * x;
const y2 = y * y;
const x2y2 = x2 + y2;
const pi = Math.PI;
const pi2 = pi * pi;
const { sin, cos } = Math;
const fn1 = function(phi) {
return x2y2 * (pi * sin(phi) - 2 * phi) * pi + 4 * phi * phi * (y - sin(phi)) + 2 * pi * phi - pi2 * y;
}

const fn2 = function(phi) {
return x2y2 * (pi * cos(phi) - 2) * pi + 8 * phi * (y - sin(phi)) - 4 * phi * phi * cos(phi) + 2 * pi;
}
return [
solveLambda(x, 1 - x2 - y2, -x) * Math.PI / 2 || 0,
solvePhi(fn1, fn2)
];
}
Insert cell
// quadratic formula
function solveLambda(a, b, c) {
return (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
}
Insert cell
// Newton-Raphson
function solvePhi(fn1, fn2, steps = 100) {

const tolerance = 1e-6;
let phi, phi0 = 0;

do {
phi = phi0 - fn1(phi0) / fn2(phi0);
if (Math.abs(phi - phi0) < tolerance) {
return phi;
}
phi0 = phi;
} while (--steps > 0);

return NaN;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more