# How to return a function with variable different than variable used in Module

I have a module function where I am calculating an interpolated polynomial using Newton's method. The end of this module is what is actually forming the function. Currently, in the module, I have the last line having a function which is what I want returned from the module function. Instead, I am getting x$as the variable. Here is the code: NewtonInterpolation[xList_, yList_] := Module[{n, aList, NewtonInterPoly, x}, n = Length[xList]; aList = Table["", n]; aList[[1]] = yList[[1]]; Do[aList[[k]] = (yList[[k]] - Sum[aList[[i]]* Product[xList[[k]] - xList[[j]], {j, 1, i - 1}], {i, 1, k - 1}])/(Product[ xList[[k]] - xList[[j]], {j, 1, k - 1}]), {k, 2, n}]; NewtonInterPoly[x_] = Sum[aList[[i]]*Product[x - xList[[j]], {j, 1, i - 1}], {i, 1, n}]] xList = {1, 2, 3}; yList = {1, 4, 9}; Poly[y] = NewtonInterpolation[xList, yList]; Print[Expand[Poly[y]]]  which returns x$^2, but I want it to return y^2. What am I doing wrong?

• I ran your code and got a bunch of errors. Can you check to see if you have a typo or have included all the correct information? EDIT: Nevermind, I know what the issue is. Pre-V10.1, Table["", n] needs to be Table["", {n}]. – march Oct 17 '16 at 18:47
• If you remove x from your list of local Module variables, it will work, but then you will always need to use x. Perhaps you should redefine things this way: NewtonInterpolation[xList_, yList_, x_] := ... and remove x from your list of Module variables. Then run Poly[y_] = NewtonInterpolation[xList, yList, y]. Also, you don't need NewtonInterPoly[x_] =  in your code. – march Oct 17 '16 at 18:50

There are few small mistakes in the code you posted:

1) The table iterator should be in brackets.

 aList = Table["", n];


Should be

 aList = Table["", {n}];


2) I'm not sure what this line was intended to do:

  NewtonInterPoly[x_] =


but the way you are using it, it does nothing; you are using the result returned by the assignment, which is just the value from right hand side.

To answer your question: x should not be declared by the module, it should be passed in. There are two ways you could do this: add to the pattern, like so:

NewtonInterpolation[xList_, yList_, x_] :=


or (I would prefer):

NewtonInterpolation[xList_, yList_][x_] :=


The other option is to have NewtonInterpolation create a function, like so:

NewtonInterpolation[xList_, yList_] :=
Function[{x},
Module[ ...


And, if you change the pattern defining NewtonInterpolation, you should be sure to call

  ClearAll[NewtonInterpolation]

• note the Table syntax is correct with newer versions. – george2079 Oct 17 '16 at 19:12
• In my experience table with brackets is always more robust (Do also got a similar quality-of-life update, but it seems to give errors as well). I would agree with recommendation 1, even though it isn't supposed to matter. – user6014 Oct 17 '16 at 19:14

one approach is to have your module return a pure function. Replace the last line with :

Evaluate[Sum[
aList[[i]]*Product[# - xList[[j]], {j, 1, i - 1}], {i, 1, n}]] &


aside, for this example you might want to do :

Simplify@Evaluate ...


(Then you wont need to Expand the result )

the usage is:

Poly[y_] = NewtonInterpolation[xList, yList]@y;


or simply:

Poly=NewtonInterpolation[xList, yList]


(Note starting your own symbols with caps is not good practice)