3
$\begingroup$

Is there a way to make mathematica automatically collect terms like $\mathrm{Cos}[n\ x]$ with $n=1,2,3...$? I have a long expression coming out of a Fourier series where I see a lot of $Cos[x],Cos[2 x],Cos[3 x]$ and so on, but Simplify is very inefficient. I would like mathematica to automatically collect all coefficients of the various Cos[n x] without having to do it manually for each one of them.

$\endgroup$
2
  • $\begingroup$ can you show your initial code, so we try to help! $\endgroup$
    – nufaie
    Apr 6, 2019 at 17:00
  • $\begingroup$ Try Collect[exp /. Cos[x n_.] -> x^n, x]/. x^n_. -> Cos[n x]. $\endgroup$
    – Somos
    Apr 7, 2019 at 2:29

1 Answer 1

6
$\begingroup$

is this what you are asking?

exp=3Cos[x]+5Cos[2x]+12Cos[3x];

Coefficient[exp,Cos[x #]]&/@Range@3

{3, 5, 12}

$\endgroup$
2
  • $\begingroup$ Yes, thanks this is it! $\endgroup$
    – Riccardo
    Apr 6, 2019 at 17:28
  • $\begingroup$ you are welcome! $\endgroup$
    – ZaMoC
    Apr 6, 2019 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.