# InterpolatingPolynomial defining x

So InterpolatingPolynomial use integer x values? How do i manipulate them? InterpolatingPolynomial[FUNCTION of x, x]

Guess i quite don't get the function definition cause my code is not working properly

I want to use the function for defined x range

xk[k_, n_] := (-1 + (k)*(2/(n)))
f[x_] := 1/(1 + 25 x^2)
fk[n_] := Table[f[xk[i, n]], {i, 0, n}]
PlotPoint[x_] :=
ListPlot[Table[{xk[i, x], Part[fk[x], i + 1]}, {i, 0, x}]]
PlotLine[x_] := Plot[f[i], {i, xk[0, x], xk[x, x]}]
Show[PlotLine, PlotPoint]

getMatrix[N_] :=
Table[If[j == 1, 1, xk[i - 1, N - 1]^(j - 1)], {i, N}, {j, N}]
Lin := LinearSolve[getMatrix, Table[fk[k, 3], {k, 0, 3}]]
c[n_] := LinearSolve[getMatrix[n + 1], fk[n]]
p1[x_, n_] := Sum[Part[c[n], i + 1]*x^i, {i, 0, n}]
PlotPoly := Plot[p1[i, 4], {i, xk[0, 4], xk[4, 4]}]
PlotPoly
Show[PlotPoly, PlotPoint, PlotLine]

l[x_, i_, n_] :=
Product[If[j != i, (x - xk[j, n])/(xk[i, n] - xk[j, n]), 1], {j, 0,
n}]
l[xk[3, 10], 4, 10]
p2[x_, n_] := Sum[f[xk[k, n]]*l[x, k, n], {k, 0, n}]
Plot[p2[i, 4], {i, xk[0, 4], xk[4, 4]}]

Plot[InterpolatingPolynomial[f[x], x], {x, xk[0, 4], xk[4, 4]}]



Edit1:

Plot3 := Plot[
InterpolatingPolynomial[{f[x]}, x], {x, xk[0, 4], xk[4, 4]}]


Any ideas how to set this up such Plot3 equals p2 and p1?

New contributor
Rapiz is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
• What is it that you want InterpolatingPolynomial to do? The way you have it set up in Plot3, all that is happening is you're plotting your f[x] and InterpolatingPolynomial isn't actually doing anything. For InterpolatingPolynomial to work, you need to send it some data points. Do you want to feed it the data points created by your initial PlotPoint function? – MassDefect Dec 2 at 21:40
• Yes my script tells me that the plot with InterpolatingPolynomial (xk and f) should be the same plot as my Plot[p2[i, 4], {i, xk[0, 4], xk[4, 4]}] and p1 – Rapiz Dec 2 at 21:45
• @Rapiz You will need to describe what you need quite a bit better. You will also want to remove any code in your question that does not pertain to the question directly. Right now I think this is more confusing than it needs to be. – MarcoB Dec 2 at 22:08
• So my function for supporting values are defined by f[x_] and my supporting points are defined by xk[k_,n_]  I calculated the interpolation polynom with p1 and p2 Now i want to calculate my interpolation polymom with InterpolationPolynomial[] and plot it. My script tells me that it should look the same as p1 and p2. So the plot should be equal to PlotPoly and Plot[p2[i, 4], {i, xk[0, 4], xk[4, 4]}] – Rapiz Dec 2 at 22:11

In Mathematica, there is a difference between Set (=) and SetDelayed (:=). SetDelayed is often used when you're defining a function because you want to define it now but not actually evaluate it until you have a number to plug into it, and you will probably evaluate it repeatedly. Set means that the code will be executed immediately and the result stored in your variable. Your code isn't too computationally intensive, but defining something like Plot3 := Plot[...] means that the plotting algorithm must be called everytime you write Plot3. In these cases, usually you just want plot3 = Plot[...] so that it is evaluated just once and then stored. Also, I recommend against using uppercase variable/function names to avoid collisions with built-in functions.

As for using InterpolatingPolynomial, it needs to have a set of data points. One possibility is:

points[n_] := Transpose[{#, f[#]} &@xk[Range[0, n], n]]
intplt[n_] := Plot[InterpolatingPolynomial[points[n], x], {x, -1, 1}, PlotStyle -> Red]
Show[
PlotLine[x],
PlotPoint,
intplt,
PlotRange -> Full
] If you're new to Mathematica, the Slot (#) notation is probably a bit confusing. They are essentially placeholders and are just waiting for something to be slotted into them. So my {#, f[#]} &@ xk[Range[0, n], n] is equivalent to {xk[Range[0, n], n], f[xk[Range[0, n], n]}. The & character is the signal that the slots should be filled with whatever comes after it, and the @ symbol is the same as using square brackets [ ] around the code, but I already had a lot of square brackets so I felt it looked cleaner with @.

My xk[Range[0, n], n] might also look strange, but many built-in function as well as custom functions have the property Listable. This means that if you give it a list of values, it will provide an output for each of the inputs. In the case of $$n=4$$, Range[0, 4] produces the list {0, 1, 2, 3, 4} so we have xk[{0, 1, 2, 3, 4}, 4]. It will then perform the calculation {xk[0, 4], xk[1, 4], xk[2, 4], xk[3, 4], xk[4, 4]} and return the result. The same thing can be achieved using Table but Listable functions can be many times faster.

I'm also using Transpose to turn 2 lists into a series of $$(x, y)$$ data points, which is what InterpolatingPolynomial is expecting. Thus, points generates the list {{-1, 1/26}, {-(1/2), 4/29}, {0, 1}, {1/2, 4/29}, {1, 1/26}}.

• I'm way more confused about InterpolatingPolynomial, isn't the x in the documentation the list that gives the f it's x values ? – Rapiz Dec 2 at 23:08
• Tried to use InterpolatingPolynomial[fk,Table[xk[i,4],{i,0,4}]] but that one failed as well – Rapiz Dec 2 at 23:11
• @Rapiz No, it's not. The x can be any variable name you want. For example, you could have InterpolatingPolynomial[{{0, 1}, {1, 2}, {2, 5}}, myvariable] and it will return myvariable^2 + 1. The x should not contain any data itself, it's just a convenient way to specify which variable you'd like to use to write the polynomial. If you try InterpolatingPolynomial[{1, 2, 5}, x], it assumes the values you give are y-values and assumes the x-values are {1, 2, 3} (many functions in Mathematica make this assumption if you only give them y-data). – MassDefect Dec 2 at 23:20