Public
Edited
Nov 26, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Plot.plot({
grid: true,
marks: [
Plot.line(newtonPolynomCurve, {x: (d) => d[0], y: (d) => d[1]}),
Plot.dot(points, {x: (d) => d[0], y: (d) => d[1]})
]
})
Insert cell
coefficients = (points) => {
let coefficients = [points.map((p) => p[1])];
for (let i = 1; i < points.length; i++) {
coefficients[i] = [];
for (let j = 0; j < points.length-i; j++) {
coefficients[i][j] = (coefficients[i-1][j+1]-coefficients[i-1][j])/(points[j+i][0]-points[j][0])
}
}

return coefficients
}
Insert cell
polynomial = (points, x) => {
let poly = coefficientsMatrix[0][0];
for (let i=1; i<coefficientsMatrix.length; i++) {
let product = 1;
for (let j=0; j<i; j++) {
product = product * (x-points[j][0])
}
poly += coefficientsMatrix[i][0]*product
}
return poly
}
Insert cell
newtonPolynomCurve = {
const minX = d3.min(points.map((p) => p[0]));
const maxX = d3.max(points.map((p) => p[0]));
const domain = maxX-minX;
const curvePoints = [];
const startTime = new Date();
for (let x = minX; x <= maxX; x+=domain/fineness) {
curvePoints.push([x, polynomial(points, x)])
}
const endTime = new Date();
mutable calculatePoints = endTime-startTime;
return curvePoints
}
Insert cell
coefficientsMatrix = coefficients(points);
Insert cell
Insert cell
Insert cell
Insert cell
fineness = 300;
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