# Issue with Coefficient command

I'm trying to used the Coefficient command to extract the numerical values in front of a Chebyshev polynomial. I know that there is a numerical way to do this, presented in numerical recipes, which I have already done. But I think using the Coefficient function would be easier and possibly less prone to numerical issues. For example when I use the method from numerical recipes Mathematica thinks that some of my Chebyshev coefficients have an imaginary part, which I know they certainly do not.

This is what I have so far:

test = {6.6358882978163365, 2.12043877554564, 7.221785806754298, 8.556762565842575,
0.8777943247026911}

test2[y_] = Sum[test[[i + 1]]*ChebyshevT[i, y], {i, 0, 4}]

6.63589 + 2.12044 y + 7.22179 (-1 + 2 y^2) + 8.55676 (-3 y + 4 y^3) +
0.877794 (1 - 8 y^2 + 8 y^4)


Then I try

Table[Coefficient[test2[y], ChebyshevT[i, y]], {i, 0, 4}]


which resulted in an error, so I just trying getting the coefficients one by one to see if I could figure out the issue. Please bear with me,

In[38]:= Coefficient[test2[y], ChebyshevT[0, y]]

During evaluation of In[38]:= Coefficient::ivar: 1 is not a valid variable. >>

Out[38]= Coefficient[
6.63589 + 2.12044 y + 7.22179 (-1 + 2 y^2) +  8.55676 (-3 y + 4 y^3)
+ 0.877794 (1 - 8 y^2 + 8 y^4), 1]

In[39]:= Coefficient[test2[y], ChebyshevT[1, y]]

Out[39]= -23.5498

In[40]:= Coefficient[test2[y], ChebyshevT[2, y]]

Out[40]= 7.22179

In[41]:= Coefficient[test2[y], ChebyshevT[3, y]]

Out[41]= 8.55676

In[42]:= Coefficient[test2[y], ChebyshevT[4, y]]

Out[42]= 0.877794


So, the Coefficient command is not working for the zeroth order and first order Chebyshev polynomial. I think I understand why it won't work for the zeroth order. The Coefficient function probably needs a symbolic argument and not a number like "1". Is there anyway to fix this?

More confusing to me is why it fails for the first order Chebyshev polynomial. I can't figure that part out at all.

The code

Table[Coefficient[test2[y], z = ChebyshevT[i, y]; If[z === 1, Sequence @@ {y, 0}, z]], {i,
0, 4}]


provide by Nasser, seems to work, it gives

{0.291897, -23.5498, 7.22179, 8.55676, 0.877794}


But I'm starting to understand that there is a fundamental problem with what I'm trying to do. When I give the command

Coefficient[test2[y], ChebyshevT[1, y]]


Mathematica just interprets that as

Coefficient[test2[y], y]


and the adds together all of the numerical coefficients in front of anything that has a "y" in the term. Ideally I want Mathematica to output 2.12044, but it is adding up all of the terms that have a "y" in front of them.

I guess I will just have to stick with the method found in numerical recipes.

-
oh, you are starting the table from i=0. If you start from i=1 then Table[Coefficient[test2[y], ChebyshevT[i, y]], {i, 1, 4}] gives {-23.5498, 7.22179, 8.55676, 0.877794}. When i=0, then ChebyshevT[0, y]=1 and then it becomes as if you wrote Coefficient[test2[y], 1] which is not valid. Look at help of Coefficient more carefully. –  Nasser Sep 2 '13 at 21:14
To answer your last question above, try: Table[Coefficient[test2[y], z = ChebyshevT[i, y]; If[z === 1, Sequence @@ {y, 0}, z]], {i, 0, 4}] –  Nasser Sep 2 '13 at 21:29
I converted your "answer" to an addendum because I believe it is one. –  Mr.Wizard Sep 2 '13 at 23:58
I am baffled but what you are saying above. You do not like that Coefficient adds together all of the numerical coefficients in front of anything that has a "y" in the term.`? But this is the definition of Coefficient of a term. You are asking M to give you the Coefficient of $y$ in the polynomial. Here it is: !Mathematica graphics and it is clear that the Coefficient of $y$ is -23.5498. But instead you are you want M to return 2.12044? But that would be the wrong Coefficient. Are you saying Numerical recipes returns 2.12044 here? –  Nasser Sep 3 '13 at 2:20
By the way, if one expands the expression test2[y] // Expand, one finds: 0.291897 - 23.5498 y + 7.42122 y^2 + 34.2271 y^3 + 7.02235 y^4. Now, look at the factor at y^4. It is not equal to 0.877794 as expected, is it? –  Alexei Boulbitch Sep 3 '13 at 7:34