I want to get the coefficients of the polynomial
1+2sin(2x)+3sin(3x)+4sin(4x)
as
{1,2,3,4}.
But I use this code
Coefficient[1+2sin(2x)+3sin(3x)+4sin(4x), Table[f[i*x], {i, 3}]]
Only get the {2,3,4}, can not get the coefficient of constant term.
How to get what I want?
nma
(Nasser M. Abbasi)
December 7, 2015, 9:13am
2
I want to get the coefficients of the belw polynomial
1+2sin(2x)+3sin(3x)+4sin(4x) as {1,2,3,4}.
But the above is not a polynomial? If you want to get {1,2,3,4} you can try
Clear[x];
f = 1 + 2 Sin[2 x] + 3 Sin[3 x] + 4 Sin[4 x];
f = List @@ f;
f /. Sin[_] -> Sequence @@ {}
(*{1, 2, 3, 4}*)
Thank you very much for your help!
But some bugs appears:
If the input series is “0 + 0 Sin[2 x] + 0 Sin[3 x] + 4 Sin[4 x];”
Only get the result {4}. But I want to get {0,0,0,4}
Could you please give more advice?
nma
(Nasser M. Abbasi)
December 7, 2015, 9:37am
4
If the input series is “0 + 0 Sin[2 x] + 0 Sin[3 x] + 4 Sin[4 x];”
Only get the result {4}
There is no bug. Mathematica input itself simplifies 0 Sin[2 x] + 0 Sin[3 x] to zero before even the evaluator gets hold of the expression;
So if you type
f = 0 + 0 Sin[2 x] + 0 Sin[3 x] + 4 Sin[4 x]
and look at f you’ll see it is 4 Sin[4 x]
There is nothing you can do about this really, unless you use Hold and friends when you actually type the expression itself.
f = Hold[0 + 0 Sin[2 x] + 0 Sin[3 x] + 4 Sin[4 x]]
But once you release the Hold, we are back to square one. May be you can try using Strings. But I am not sure what you are trying to do there.
Sorry for not clarifying the problem.
I want extract the coefficients of sine/cosine series and transform them into matrix, so I need coefficients of all terms (including the 0 terms).
“String” does not work, but thank you all the same.
The following works on your example:
sinList = {Sin[2 x], Sin[3 x], Sin[4 x]};
f = 1 + 2 Sin[2 x] + 3 Sin[3 x] + 4 Sin[4 x];
Prepend[Map[Coefficient[f, #] &, sinList], f /. Thread[sinList -> 0]]
It does work! Many thanks for your helpful.