Public
Edited
Jan 3, 2022
1 star
Insert cell
Insert cell
THRESHOLD = 1e-6;
Insert cell
function newton(f, guess) {
let df = derive(f);
let lambda = (x) => x - (f(x) / df(x));
return fixedPoint(lambda, guess);
}
Insert cell
function derive(f, dx = THRESHOLD){
return (x) => (f(x+dx) - f(x)) / dx;
}
Insert cell
function fixedPoint(f, current){
let next = f(current);
if (closeEnough(current, next)) return next;
else return fixedPoint(f, next);
}
Insert cell
function closeEnough(x, y, threshold = THRESHOLD){
return Math.abs(x - y) < threshold;
}
Insert cell
Insert cell
newton(x => x*x - 4, 1.0)
Insert cell
newton(x => x*x*x - 27, 1.0)
Insert cell
Insert cell
jazr = (y) => newton(x => y - x*x, 1.0);
Insert cell
jazr(4)
Insert cell
viewof x = Inputs.number({value: 4, label: "Enter the number:"})
Insert cell
jazr(x)
Insert cell
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