sums of sin or cosine to one sin using Mathematica

Nasser M. Abbasi 08/16/2013

Introduction

sums of cosine terms, all with same frequency but can have different amplitude or phases is converted to a single sin term. The first step is to use TrigExpand to break sin or cos with phase to pure sin or cos. Next step is to collect terms based on the pure sin and the pure cos terms. This can be done either use Collect (see appendix) or using CoefficientList.
The input to the function given below is the expression to reduce with the independent variable. Validation of expression is done using Mr.Wizard pattern test (see Appendix).

Table is given showing examples of results. The result is single sin which you can convert to cosine by adding Pi/2 to the phase.
Mathematica notebook
PDF file

Function

ClearAll["Global`*"]

sumsReduce[e_,x_]:=Module[{e1=e,p2,h,b,a,head},
p2=#|Verbatim[Plus][#]&[(_. (h:Sin|Cos)[_])..]; 
head=Replace[e1,{p2:>h,_->False}]; 

(*parsing is done, now check the result*)
If[head===False,
e1,

e1=TrigExpand[e1];
b=Last@CoefficientList[e1,Cos[x],2];
a=Last@CoefficientList[e1,Sin[x],2];
(*watch out for the atan2. Mathematica uses atan2(x,y) while Wikipedia uses atan2(y,x) *)
Sqrt[a^2+b^2] Sin[x+ArcTan[a,b]]
]
]

exampes

ClearAll[a,b,c,d,e,f,g,h,x];
sumsReduce[a Cos[x]+b Cos[x + b],x]//TraditionalForm

sumsReduce[a Cos[x+b]+c Cos[x+d]+e Cos[x-f],x]//TraditionalForm

sumsReduce[a Sin[x+b]+c Sin[x]+e Sin[x-f],x]//TraditionalForm

sumsReduce[a Sin[x+b]+c Sin[x]+e Sin[x-f]+Sin[x-2f]+3g Sin[x-2d],x]//TraditionalForm

Table

printout of table is here

parms={a->2./3,b->Pi/4.}; 
Grid[{
{
Grid[{
{expr=HoldForm[a Cos[x]+2a Cos[x +b]];Row[{Style["original ",Bold,14],ReleaseHold@expr/.parms}]},
{result=sumsReduce[ReleaseHold[expr],x]; Row[{Style["reduced ",Bold,14],result/.parms}]}
},Alignment->Left],
Plot[result/.parms,{x,-2 Pi,2 Pi},Frame->True,FrameLabel->{{None,None},{None,"result"}}],
Plot[ReleaseHold[expr]/.parms,{x,-2 Pi,2 Pi},Frame->True,FrameLabel->{{None,None},{None,"original"}}]
},
{
Grid[{
{expr=HoldForm[a Sin[x ]+2a Sin[x +b]];Row[{Style["original ",Bold,14],ReleaseHold@expr/.parms}]},
{result=sumsReduce[ReleaseHold[expr],x]; Row[{Style["reduced ",Bold,14],result/.parms}]}
},Alignment->Left],
Plot[result/.parms,{x,-2 Pi,2 Pi},Frame->True,FrameLabel->{{None,None},{None,"result"}}],
Plot[ReleaseHold[expr]/.parms,{x,-2 Pi,2 Pi},Frame->True,FrameLabel->{{None,None},{None,"original"}}]
},
{
Grid[{
{expr=HoldForm[a Cos[x ]+a Cos[x +b]+2a Cos[x+2b]+ Cos[x+3b]];Row[{Style["original ",Bold,14],ReleaseHold@expr/.parms}]},
{result=sumsReduce[ReleaseHold[expr],x]; Row[{Style["reduced ",Bold,14],result/.parms}]}
},Alignment->Left],
Plot[result/.parms,{x,-2 Pi,2 Pi},Frame->True,FrameLabel->{{None,None},{None,"result"}}],
Plot[ReleaseHold[expr]/.parms,{x,-2 Pi,2 Pi},Frame->True,FrameLabel->{{None,None},{None,"original"}}]
},
{
Grid[{
{expr=HoldForm[a Sin[x]+a Sin[x+b]+2a Sin[x+2b]+Sin[x+3b]];Row[{Style["original ",Bold,14],ReleaseHold@expr/.parms}]},
{result=sumsReduce[ReleaseHold[expr],x]; Row[{Style["reduced ",Bold,14],result/.parms}]}
},Alignment->Left],
Plot[result/.parms,{x,-2 Pi,2 Pi},Frame->True,FrameLabel->{{None,None},{None,"result"}}],
Plot[ReleaseHold[expr]/.parms,{x,-2 Pi,2 Pi},Frame->True,FrameLabel->{{None,None},{None,"original"}}]
},
{
Grid[{
{expr=HoldForm[a Sin[x]+2a Sin[x+2b]+3a Sin[x+3b]+Sin[x-4b]-Sin[x-b]+2 a Sin[x-5b]];Row[{Style["original ",Bold,14],ReleaseHold@expr/.parms}]},
{result=sumsReduce[ReleaseHold[expr],x]; Row[{Style["reduced ",Bold,14],result/.parms}]}
},Alignment->Left],
Plot[result/.parms,{x,-2 Pi,2 Pi},Frame->True,FrameLabel->{{None,None},{None,"result"}}],
Plot[ReleaseHold[expr]/.parms,{x,-2 Pi,2 Pi},Frame->True,FrameLabel->{{None,None},{None,"original"}}]
},
{
Grid[{
{expr=HoldForm[a Sin[x]-2a Sin[x-2b]+4a Sin[x-b/10]+Sin[x-4b]-Sin[x-b/2]+2 a Sin[x-5b]];Row[{Style["original ",Bold,14],ReleaseHold@expr/.parms}]},
{result=sumsReduce[ReleaseHold[expr],x]; Row[{Style["reduced ",Bold,14],result/.parms}]}
},Alignment->Left],
Plot[result/.parms,{x,-2 Pi,2 Pi},Frame->True,FrameLabel->{{None,None},{None,"result"}}],
Plot[ReleaseHold[expr]/.parms,{x,-2 Pi,2 Pi},Frame->True,FrameLabel->{{None,None},{None,"original"}}]
},
{
Grid[{
{expr=HoldForm[a Cos[x]-2a Cos[x-2b]+4a Cos[x-b/10]+Cos[x-4b]-Cos[x-b/2]+2 a Cos[x-5b]+Cos[x-6b]+3a Cos[x+20 b]];Row[{Style["original ",Bold,14],ReleaseHold@expr/.parms}]},
{result=sumsReduce[ReleaseHold[expr],x]; Row[{Style["reduced ",Bold,14],result/.parms}]}
},Alignment->Left],
Plot[result/.parms,{x,-2 Pi,2 Pi},Frame->True,FrameLabel->{{None,None},{None,"result"}}],
Plot[ReleaseHold[expr]/.parms,{x,-2 Pi,2 Pi},Frame->True,FrameLabel->{{None,None},{None,"original"}}]
}
},Frame->All]

References

1.  Mr Wizard’s pattern matcher used in the above function http://mathematica.stackexchange.com/questions/30588/using-matchq-or-other-means-to-parse-an-expression-using-sums-of-cos-or-sin-co
2. Wikepedia article http://en.wikipedia.org/wiki/List_of_trigonometric _identities
3. Ray Koopman implementation using Collect http://forums.wolfram.com/mathgroup/archive/2009/Jun/msg00479.html