# Issue with Coefficient command [closed]

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.

-

## closed as unclear what you're asking by Dr. belisarius, Sjoerd C. de Vries, m_goldberg, MarcoB, ÖskåAug 20 '15 at 17:36

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

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

Update notice: I added this answer, mutatis mutandis, to Series expansion in terms of Hermite polynomials, which effectively makes the present question a duplicate of the linked one. I decided to leave this one here, because, well, it was already written and would possibly help the OP. Also this question is likely to be closed for reasons other than it is a duplicate, and I wished to link them.

SolveAlways can do it. You set up an equation, setting the given polynomial equal to a linear combination of your basis polynomials. This approach will work generally with any polynomial that is a linear combination of a given set of (linearly independent) polynomials.

params = Table[C[i], {i, 0, 4}];
basis = Table[ChebyshevT[i, y], {i, 0, 4}];

coeff = params /. First@ SolveAlways[test2[y] == params.basis, y]
(*  {6.63589, 2.12044, 7.22179, 8.55676, 0.877794}  *)

coeff == test
(*  True  *)
`
-