I would like to compute coefficient in mathematica. for example I wrote this code to find the coefficients of cos(kx) in (a+b*cos(x))^4 where "a" and "b" are parameters.
f[x_] := (a + b Cos[x])^4
f1[x_] := TrigReduce[f[x]]
g[x_, k_] := Coefficient[f1[x], Cos[k x]]
it works for the coefficients of cos(k*x),
for example the coefficient of cos(2x) is
g[x,2]= 1/8 (24 a^2 b^2 + 4 b^4)
but it does not work for constant(here constant means independent of cos(kx). In other words, just the terms with numbers and the parameters "a" and "b").
I would like to write the code to find the constant in the above meaning.
thanks.
Plugging
Coefficient[TrigReduce[(a + b*Cos[x])^4],Cos[2*x]]
into Wolfram|Alpha produced the output you wanted it to. This leads me to suggest that your problem might have to do with how the expression is being evaluated as opposed to a problem with how you are mathematically thinking about it.I do not have access to a copy of Mathematica so can not test this, but I would try changing
:=
to=
in the second line of code.I would also try putting it all in one line as
If that works, it is definitely something wrong with how/when Mathematica is assigning stuff.
See this link for more information.