Can someone please show me how to display an output with the following program?
ID: 3826273 • Letter: C
Question
Can someone please show me how to display an output with the following program? The output only has to display the value of f(x).
Ex. Of output: 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)
Following is program:
varl=newLagrange(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; j < k; ++j) { w = 1; for (var i = 0; i < k; ++i) { if (i != j) { w *= this.xs[j] - this.xs[i]; } } this.ws[j] = 1/w; } } // Calculate L(x) Lagrange.prototype.valueOf = function(x) { var a = 0; var b = 0; var c = 0; for (var j = 0; j < this.xs.length; ++j) { if (x != this.xs[j]) { a = this.ws[j] / (x - this.xs[j]); b += a * this.ys[j]; c += a; } else { return this.ys[j]; } } return b / c; }Explanation / Answer
output is 403520
in lagrange polynomial we can write this program different way
in 1st step we are assuming to index
in 2nd step we are adding or chaning the values the values to the previously points
and next we are re-calculating weights
and let last we are finding the value of L(x) by step by step process
using variables like a=0,b=0,c=0;
by default
in this if statement we are taking x value if x is equal to xs[j] then it goes to a statement
else it simply returns the b/c value.