I have an expression that involves trig functions of t and y. I would like to find the coefficients of Cos[t], Sin[t], Cos[2 t], Sin[2 t] ... and the constant term independent of t. I can get the terms in t and y separated by using TrigExpand however this then leaves me with trig expressions like Cos[t]^2. To reduce these to terms in Cos[n t] etc. I have to apply TrigReduce to them but NOT to the terms involving y. TrigReduce does not have a second parameter to tell it to just reduce terms in say t. Can we make one?

My workaround is to use FourierTrigSeries guessing the highest trig multiple I need (here 5 but I don't want to guess) and then to use Coefficient to find the individual terms and the constant term. This is a bit long. Are there other ways?


Here is a simple example. I will need to do more complex examples.

e1 = -(Cos[t] - E^(-(y/Sqrt[2])) Cos[t - y/Sqrt[2]])^2 + 
   1/2 ((E^(-(y/Sqrt[2])) Cos[t - y/Sqrt[2]])/Sqrt[2] - (
      E^(-(y/Sqrt[2])) Sin[t - y/Sqrt[2]])/Sqrt[
      2]) (-(Sqrt[2] - 2 y) Cos[t] + 
      Sqrt[2] E^(-(y/Sqrt[
        2])) (Cos[t - y/Sqrt[2]] - E^(y/Sqrt[2]) Sin[t] + 
         Sin[t - y/Sqrt[2]])) ;

e2 = e1 // TrigExpand;
e3 = FourierTrigSeries[e2, t, 5];
trigs = Flatten@Table[{Cos[n t], Sin[n t]}, {n, 5}];
coffs = Coefficient[e3, #] & /@ trigs;
const = e3 - coffs.trigs;

The coefficients are

coffs // TableForm

Mathematica graphics

and the constant terms is


Mathematica graphics

A sanity check gives

Simplify[e1 == const + coffs.trigs]

(* True  *)

Try the following: These are the transformation rules:

rules = {Cos[t]^n_Integer :> TrigReduce[Cos[t]^n], 
  Sin[t]^n_Integer :> TrigReduce[Sin[t]^n], 
  Times[a_, Cos[t] , Sin[t], c_] :> a*c*TrigReduce[Cos[t] Sin[t]]};

With these rules

Collect[e2 /. rules, {Cos[2 t], Sin[2 t]}]


enter image description here

You might want to simplify the coefficients of the sin(2t) and cos(2t) as follows:

 Collect[e2 /. rules, {Cos[2 t], Sin[2 t]}], {{7, 2}, {8, 2}}]

giving the following

enter image description here

Have fun!

  • 1
    $\begingroup$ Good idea, write my own rules. Thanks. $\endgroup$ – Hugh Nov 9 '17 at 14:19
  • $\begingroup$ I know that this post was written long time ago, but I would like to add a correction to the rules defined above. It is better to write the last element of the them as Times[a___, Cos[t] , Sin[t]] :> Times[ a, TrigReduce[Cos[t] Sin[t]] ] For example, only in this form the rules will render the expressions like $2 \sin x \cos x$ into $\sin 2 x$, while the original rules will leave such input untouched $\endgroup$ – Sergei Ovchinnikov Dec 11 '20 at 19:24

One idea is to use FourierCoefficient to compute the general term:

f[n_] = FourierCoefficient[e1, t, n];
f[n] //TeXForm

$\begin{cases} \frac{1}{8} \left(-2+e^{-\frac{(1-i) y}{\sqrt{2}}} \left(2+(1-i) \sqrt{2} y\right)\right) & n=-2 \\ \frac{1}{8} \left(-2+e^{-\frac{(1+i) y}{\sqrt{2}}} \left(2+(1+i) \sqrt{2} y\right)\right) & n=2 \\ \frac{1}{8} \left(e^{-\frac{(1+i) y}{\sqrt{2}}} \left((1+i) \sqrt{2} y+(1-i) e^{i \sqrt{2} y} \left(\sqrt{2} y+(1+3 i)\right)+(4-2 i)\right)-4 e^{-\sqrt{2} y}-4\right) & n=0 \end{cases}$

We can then recover the Sin and Cos coefficients as follows:

sin = Simplify @* ComplexExpand /@ Simplify[I(f[n]-f[-n]), n>0];
sin //TeXForm

$\begin{cases} \frac{1}{4} e^{-\frac{y}{\sqrt{2}}} \left(\left(\sqrt{2} y+2\right) \sin \left(\frac{y}{\sqrt{2}}\right)-\sqrt{2} y \cos \left(\frac{y}{\sqrt{2}}\right)\right) & n=2 \\ 0 & \operatorname{True} \end{cases}$

cos = Simplify @* ComplexExpand /@ Simplify[f[n] + f[-n], n>0];
cos //TeXForm

$\begin{cases} \frac{1}{4} e^{-\frac{y}{\sqrt{2}}} \left(-2 e^{\frac{y}{\sqrt{2}}}+\sqrt{2} y \sin \left(\frac{y}{\sqrt{2}}\right)+\left(\sqrt{2} y+2\right) \cos \left(\frac{y}{\sqrt{2}}\right)\right) & n=2 \\ 0 & \operatorname{True} \end{cases}$

zero = Simplify @* ComplexExpand @ f[0];
zero //TeXForm

$\frac{1}{4} e^{-\sqrt{2} y} \left(-2 \left(e^{\sqrt{2} y}+1\right)+e^{\frac{y}{\sqrt{2}}} \left(\sqrt{2} y-2\right) \sin \left(\frac{y}{\sqrt{2}}\right)+e^{\frac{y}{\sqrt{2}}} \left(\sqrt{2} y+4\right) \cos \left(\frac{y}{\sqrt{2}}\right)\right)$

Using FourierSinCoefficient/FourierCosCoefficient to obtain the general term doesn't work quite as well, but we can use them to check the above results:

(sin /. n->2) == FourierSinCoefficient[e1, t, 2]
(cos /. n->2) == FourierCosCoefficient[e1, t, 2]



  • $\begingroup$ This is a good way of getting a general term. One problem is how to find how many terms there are and FourierCoefficient is certainly a good way of doing this. Although you have to recombine terms this may be the best method. I am not sure what happens if there is no general term. It seems a shame that one has to go to the effort of taking a Fourier transform when all that is needed is to do algebra to find the trig terms. Thanks for your help. $\endgroup$ – Hugh Nov 9 '17 at 19:12

You can try also with this:

e1 = -(Cos[t] - E^(-(y/Sqrt[2])) Cos[t - y/Sqrt[2]])^2 + 1/2 ((E^(-(y/Sqrt[2])) Cos[t - y/Sqrt[2]])/
  Sqrt[2] - (E^(-(y/Sqrt[2])) Sin[t - y/Sqrt[2]])/
  Sqrt[2]) (-(Sqrt[2] - 2 y) Cos[t] + 
 Sqrt[2] E^(-(y/Sqrt[2])) (Cos[t - y/Sqrt[2]] - 
    E^(y/Sqrt[2]) Sin[t] + Sin[t - y/Sqrt[2]])); 
CoefficientList[FourierTrigSeries[TrigExpand@e1, t, 5] /. {Cos[a_*t] :>  x^a, Sin[a_*t] :>  z^a}, {x, z}]

enter image description here

After obtaining the Fourier series you can transform the trig polynomial to a usual polynomials and then extract the coefficients.

I think you can save steps.

  • $\begingroup$ Interesting idea. Are there dangers in replacing a trig with an exponent? I will try this out. Thanks $\endgroup$ – Hugh Nov 9 '17 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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