I am having some slight difficulty with the following code:
Lagrange[list_] :=
Module[{points = list, length, k, j, m, x, g},
length = Length[points];
k = length - 1;
f = Sum[points[[j + 1,2]]*Product[If[j != m, (x - points[[m + 1,1]])/
(points[[j + 1,1]] - points[[m + 1,1]]), 1], {m, 0, k}], {j, 0, k}];
g = FullSimplify[Expand[f]];
Return[f]]
The output I get is:
Out[101]= 0. -1.85698 (-1.5+x$26810) (-0.75+x$26810) (0. +x$26810) (0.75+x$26810)
+0.490717 (-1.5+x$26810) (-0.75+x$26810) (0. +x$26810) (1.5 +x$26810)
-0.490717 (-1.5+x$26810) (0. +x$26810) (0.75 +x$26810) (1.5 +x$26810)
+1.85698 (-0.75+x$26810) (0. +x$26810) (0.75 +x$26810) (1.5 +x$26810)
My concern is with these “$” symbols. I don’t know what they mean, I can’t find documentation on them, and they are preventing the plotting of this polynomial.
The
$in your output is from the unique variable generated by the lexical scoping ofModule(see the More Information part of mathematica/ref/Module). This is why I made myLagrangePolyfunction accept the symbol that the polynomial is meant to be in. I usedLagrangePoly[list_, var_:x], which defaults to the global symbolx.A simple example of the problem is
The number in the “local” variable
x$nnncomes from the global$ModuleNumber.If you don’t understand this, then you should probably read the tutorial Blocks Compared with Modules.