Created
December 27, 2010 23:31
-
-
Save exavolt/756701 to your computer and use it in GitHub Desktop.
A class to perform Lagrange polynomial interpolation from a set of points
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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