Skip to content

Instantly share code, notes, and snippets.

@dimdenGD
Last active April 28, 2023 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimdenGD/808ba1a59a23e8bb198285c4fbef9502 to your computer and use it in GitHub Desktop.
Save dimdenGD/808ba1a59a23e8bb198285c4fbef9502 to your computer and use it in GitHub Desktop.
Lagrange Interpolation in JavaScript
function lagrangeInterpolation(xs, ys, x) {
let y = 0;
for(let i = 0; i < xs.length; i++) {
let t = 1;
for(let j = 0; j < xs.length; j++) {
if(j !== i) {
t *= (x - xs[j]) / (xs[i] - xs[j]);
}
}
y += t * ys[i];
}
return y;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment