Skip to content

Instantly share code, notes, and snippets.

@exavolt
Created December 27, 2010 23:31
Show Gist options
  • Save exavolt/756701 to your computer and use it in GitHub Desktop.
Save exavolt/756701 to your computer and use it in GitHub Desktop.
A class to perform Lagrange polynomial interpolation from a set of points
var LagrangePolynomial = (function(points){
var cc = {};
cc.points = points;
cc.calc = (function(x){
var n = this.points.length;
var y = 0.0;
var j = 0;
var k = 0;
var Lx = 1.0;
for (j = 0; j < n; ++j) {
Lx = 1.0;
for (k = 0; k < n; ++k) {
if (k != j) {
Lx = Lx * (x - this.points[k].x) / (this.points[j].x - this.points[k].x);
}
}
y = y + this.points[j].y * Lx;
}
return y;
});
return cc;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment