Public
Edited
Sep 6, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// The function of interest written in Javascript
f = (x) => x ** 3 - x / 2 - 2
Insert cell
// The bisection method itself.
results = {
let desired_precisison = 0.00001;
let bail = 100;
let cnt = 0;
let a = 1;
let b = 2;
let pts = [a, b];
let c = (a + b) / 2;
pts.push(c);
while (b - a > desired_precisison && cnt < bail) {
cnt = cnt + 1;
if (f(c) < 0) {
a = c;
c = (a + b) / 2;
} else if (f(c) > 0) {
b = c;
c = (a + b) / 2;
} else if (f(c) == 0) {
a = c;
b = c;
}
pts.push(c);
}
return { root: c, value: f(c), pts };
}
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