Published
Edited
May 25, 2020
1 star
Insert cell
Insert cell
function cmod (a, b) {
var c
var aa = Math.abs(a)
var ab = Math.abs(b)
if (aa === 0 && ab === 0) {
return 0
} else if (aa >= ab) {
c = b / a
return aa * Math.sqrt(1 + c * c)
} else {
c = a / b
return ab * Math.sqrt(1 + c * c)
}
}
Insert cell
function cdiv (out, a, b, c, d ) {
var e,f
if (Math.abs(c) >= Math.abs(d) ) {
e = d/c
f = 1 / (c + d*e)
out[0] = (a + b*e) * f
out[1] = (b - a*e) * f
} else {
e = c/d
f = 1 / (c*e + d)
out[0] = (a*e + b) * f
out[1] = (b*e - a) * f
}
return out
}
Insert cell
complexNewtonRaphson = {
var tmp = [0, 0, 0, 0];
return function(out, f, z0, tolerance, maxIterations, verbose) {
maxIterations = maxIterations === undefined ? 50 : maxIterations;
tolerance = tolerance === undefined ? 1e-5 : tolerance;
out[0] = z0[0];
out[1] = z0[1];
var re, im;
re = z0[0];
im = z0[1];
var iteration = maxIterations;
var delta = 1;
var previousDelta = 0;
var error = Infinity;
while (error > tolerance && --iteration > 0) {
f(tmp, re, im);
cdiv(tmp, tmp[0], tmp[1], tmp[2], tmp[3]);
re -= tmp[0];
im -= tmp[1];
delta = cmod(tmp[0], tmp[1]);
error = Math.abs(delta / previousDelta);
previousDelta = delta;
}
if (iteration === 0 && error > tolerance) {
if (verbose)
console.warn(
'Newton-Raphson failed to converge after ' +
maxIterations +
' iterations'
);
out[0] = z0[0];
out[1] = z0[1];
} else {
out[0] = re;
out[1] = im;
}
return out;
};
}
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