# Maximizing trig functions in Mathematica

Just to give some context, my motivation for this programming question is to understand the derivation of the CSHS inequality and basically entails maximizing the following function:

Abs[c1 Cos[2(a1-b1)]+ c2 Cos[2(a1-b2)] + c3 Cos[2(a2-b1)] + c4 Cos[2(a2-b2)]]


where a1,b1,b2,and a2 are arbitrary angles and c1,c2,c3,c4 = $\pm 1$ ONLY. I want to be able to determine the maximum value of this function along with the combination of angles that lead to this maximum. Also NO angles can be equal to each other and ONLY an odd number of terms can have negative coefficients.

Eventually, I also want to repeat the calculation for a1,a2,a3,b1,b2,b3 (which will have a total of nine cosine terms)

When I tried putting the following code in Mathematica, it simply spat the input back at me and did not perform any computation, can someone help me out? (note my code didn't include the c1,c2,c3,c4 parameters, I wasn't quite sure how to incorporate them)

Maximize[{Abs[Cos[2 (a1 - b1)] - Cos[2 (a1 - b2)] + Cos[2 (a2 - b1)] + Cos[2 (a2 - b2)]],
0 <= a1 <= 2 Pi , 0 <= b1 <= 2 Pi,
0 <= a2 <= 2 Pi, 0 <= b2   <= 2 Pi}, {a1, b2, a2, b1}]

-
It won't parse the way you think. Either use Pi or [Pi] but not [Pi]. –  Daniel Lichtblau Jun 12 '13 at 4:41
Also this numerical optimization indicates it is probably 2 sqrt(2). For slight simplifications I got rid of the Abs[] and removed the factors of 2 in the angles. This changing from abs to a square makes it a bit faster but the outcome is the same either way. NMaximize[{(Cos[a1 - b1] - Cos[a1 - b2] + Cos[a2 - b1] + Cos[a2 - b2])^2, 0 <= a1 <= 2*Pi, 0 <= b1 <= 2*Pi, 0 <= a2 <= 2*Pi, 0 <= b2 <= 2*Pi}, {a1, b2, a2, b1}] Out[108]= {8., {a1 -> 4.1531005061, b2 -> 1.79690602065, a2 -> 2.58230418181, b1 -> 3.36770234824}} –  Daniel Lichtblau Jun 12 '13 at 4:47
thanks, using NMaximize seems like the way to go. But if I want to have coefficients c1,c2,c3,c4 in front of my cosine terms and I want to maximize my function while allowing these coefficients to be only 1 or -1, how would I implement the constraints? –  user2476576 Jun 12 '13 at 7:45
also I want to ensure that no angles can be the same, along with the condition I mentioned aboved –  user2476576 Jun 12 '13 at 8:22

The original question didn't have the condition about none of the angles being equal, in which case the answer is 4. This is because each Cos can be made to equal 1. You have 4 variables a1, a2, b1 and b2, and four cosines, so there are going to be several ways of making the combinations 2(a1-b1), 2(a1-b2), 2(a2-b1) and 2(a2-b2) equal 0 (hence choosing the corresponding c1/c2/c3/c4 to be +1), or equal to pi (hence choosing the corresponding c1/c2/c3/c4 to be -1).