Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}]
share|improve this question
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).

For one set of angles that give the max, the obvious answer is a1=a2=b1=b2=0. For the 9 cosine case, the max will be 9, and one possible answer is a1=a2=a3=b1=b2=b3=0. With the condition that none of the angles can be equal, then e.g. a1=0, a2=1E-10, b1=2E-10, b2=3E-10 will be just a little bit less than 4.

Regarding using Mathematica, I think the lesson is that it's always best to think about before the maths itself before using tools to help with the maths.

share|improve this answer
"'s always best to think about the maths itself before using tools to help with the maths." - well said. – J. M. Jun 12 '13 at 10:23
I forgot to tell you guys that the angles cannot all be equal, so actually the correct answer is 2 Sqrt (2), I will update my question. I'm still having trouble generalizing the problem to have optimizable coefficients as well. I want to be able to maximize my expression with c1,c2,c3,c4 equal to +/- 1 and no angles equal to each other. Basically, I know what the answer should be already, I just want to prove why a particular form of my expression yields a maximum – user2476576 Jun 12 '13 at 18:29
With the condition that none of the angles are equal, take e.g. a1=0, a2=1E-10, b1=2E-10, b2=3E-10 and you'll have something that's just a little bit less than 4. – Stochastically Jun 12 '13 at 18:40
Sorry to keep on tacking on conditions, but also only an odd number of terms can have negative coefficients – user2476576 Jun 12 '13 at 19:11
Does that mean that there has to be either 1 or 3 terms with a negative coefficient? – Stochastically Jun 12 '13 at 19:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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