I'm trying to implement polynomial interpolation. My current implementation involves generating an expression that is the final equation that will be plotted. While plotting the function itself works fine, I also want to ListPlot the interpolation points (which will eventually be tuples stored in variable dots
).
To achieve this, I attempted to do polyFunc[x_]=polyFunc
which returns a Tag Plus in [expression] is Protected
.
As an attempt to fix this, I also tried to store the expression in an auxiliary variable, polyFuncAux
:
polyFuncAux[x_] = polyFunc
This returns no errors but yields the wrong answer. The x in the expression isn't substituted for the input value and is instead, appended to the end of the expression.
Ex:
Input: polyFuncAux[0]
Output: [expression][0]
polyInterp[func_, interval_, degree_] :=
Module[{xMatrix, yMatrix, polyFunc, polyFuncAux, newFunc, coeff,
line, dots},
xMatrix =
Table[If[i == 0 && j == 0, 1, i^j], {i, interval[[1]],
interval[[2]], Abs[interval[[2]] - interval[[1]]]/degree}, {j,
0, degree}];
xMatrix = Inverse[xMatrix];
yMatrix =
Table[func[i], {i, interval[[1]], interval[[2]],
Abs[interval[[2]] - interval[[1]]]/degree}];
coeff = xMatrix . yMatrix;
polyFunc = Table[x^i, {i, 0, degree}];
polyFunc = polyFunc*coeff;
polyFunc = Total[polyFunc];
polyFunc[x_] = polyFunc;
(*
line=Plot[{newFunc,func[x]},{x,interval[[1]],interval[[2]]},
PlotStyle->{{Red,Thickness[0.006]},{Blue,Dashed}}];
Show[line]
*)
];
```