Need a basic javascript program to build the Lagrange polynomial that goes throu
ID: 3819905 • Letter: N
Question
Need a basic javascript program to build the Lagrange polynomial that goes through the points (p = [[0,3.8414709848078967],[0.6931471805599453,6.6818861490654635],[1.0986122886681098,7.5355691627323065],[1.3862943611198906,7.788374949171635],[1.6094379124341003,8.478827375073264],[1.791759469228055,9.887622378713294],[1.9459101490553132,11.440627194940042],[2.0794415416798357,12.307124413342725]]) and evaluate the function at x=1.5. Define the data points and the value of x in variables that can be easily replaced, like this:
p = [[0,3.8414709848078967],[0.6931471805599453,6.6818861490654635],[1.0986122886681098,7.5355691627323065],[1.3862943611198906,7.788374949171635],[1.6094379124341003,8.478827375073264],[1.791759469228055,9.887622378713294],[1.9459101490553132,11.440627194940042],[2.0794415416798357,12.307124413342725]]
x = 1.5
all I need you to display is the value of f(x).
With a Lagrange polynomial your answer would be
f(x) = 2.1271966695785522x^7 -28.013010025024414x^6 +127.76653289794922x^5 -276.1880187988281x^4 +308.3759117126465x^3 -173.41200065612793x^2 +42.868422985076904x +3.841470984610453
and you will be getting the original values that you had input: (0, 3.8415), (0.6931, 6.6819), (1.0986, 7.5356), (1.3863, 7.7884), (1.6094, 8.4788), (1.7918, 9.8876), (1.9459, 11.4406), (2.0794, 12.3071)
Again, the program need not to be extensive. A basic javascript program will suffice. Thanks.
Explanation / Answer
var l = new Lagrange(0, 0, 1, 1); var index = l.addPoint(0.5, 0.8); console.log(l.valueOf(0.1)); l.changePoint(index, 0.5, 0.1); console.log(l.valueOf(0.1)); var Lagrange = function(x1, y1, x2, y2) { this.xs = [x1, x2]; this.ys = [y1, y2]; this.ws = []; this._updateWeights(); } Lagrange.prototype.addPoint = function(x, y) { this.xs.push(x); this.ys.push(y); this._updateWeights(); return this.xs.length-1; } //changes a previously added point. Lagrange.prototype.changePoint = function(index, x, y) { this.xs[index] = x; this.ys[index] = y; this._updateWeights(); } // Recalculate barycentric weights. Lagrange.prototype._updateWeights = function() { var k = this.xs.length; var w; for (var j = 0; jRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.