# Maximize a variable in NSolve Function

I want to solve a set of trigometric coupled equations with the NSolve funcion while at the same time trying to maximize a variable which has to be real and between $0$ to $1$.

NSolve[ Cos[x1]Cos[x3]-a==0 && Sin[x2]Cos[x1]-a==0 && Sin[x3]Cos[x1]+a==0, {x1,x2,x3,a}]


The variable that I want to maximize is a.

The input equations are a simplified version. There is a solution to my actual equations with a = 0.5.

The real problems is,

NSolve[ Cos[x1] Cos[x2] Cos[x3] - Sin[x1] Sin[x3] == a &&
Cos[x1]^2 Cos[x2] Cos[2 x3] - Cos[x2] Cos[2 x3] Sin[x1]^2 - 2 Cos[x1] (1 + Cos[x2]^2) Cos[x3] Sin[x1] Sin[x3] == a && 1/2 (1/2  Cos[x1] (-1 + 3 Cos[2 x1]) Cos[x2] Cos[x3] (-1 + 3 Cos[2 x3]) - 6 Cos[x1]^2 Cos[x3]^2 Sin[x1] Sin[x3] - 1/2  (1 + 3 Cos[2 x1]) Cos[x2]^2 (1 + 3 Cos[2 x3]) Sin[x1] Sin[x3] + 6  Cos[x1] Cos[x2]^3 Cos[x3] Sin[x1]^2 Sin[x3]^2) == -a, {x1, x2, x3, a}]

• What do you want to maximize, what are underlying conditions? Apr 16, 2016 at 15:38
• I want to maximise a Apr 16, 2016 at 15:40
• And what about x1, x2, x3 ? And what is \[Eta]2? Apr 16, 2016 at 15:50
• sorry [Eta]2 is x2. Ill change that now. x1, x2, x3 have to be real Apr 16, 2016 at 15:55

The original problem is an interesting example where a direct usage of Solve and NSolve fails. However Reduce can succeed solving it if we set unknowns in an appropriate order, thus there is no need to play with numerical functionality if symbolic one can resolve the given system. We can observe that there is a solution under the given condition with a == 1/2. We can do even more by demonstrating that there are only solutions in the real domain with a <= 1/2 thus a == 1/2 maximizes possible values of a.

Let's write down the underlying expressions

expr1 = Cos[x1] Cos[x2] Cos[x3] - Sin[x1] Sin[x3];
expr2 = Cos[x1]^2 Cos[x2] Cos[2 x3] - Cos[x2] Cos[2 x3] Sin[x1]^2 -
2 Cos[x1] (1 + Cos[x2]^2) Cos[x3] Sin[x1] Sin[x3];
expr3 = 1/2 (1/2 Cos[x1] (-1 + 3 Cos[2 x1]) Cos[x2] Cos[x3] (-1 + 3Cos[2 x3])
- 6 Cos[x1]^2 Cos[x3]^2 Sin[x1] Sin[x3] -
1/2 (1 + 3 Cos[2 x1]) Cos[x2]^2 (1 + 3 Cos[2 x3]) Sin[x1] Sin[x3]
+ 6 Cos[x1] Cos[x2]^3 Cos[x3] Sin[x1]^2 Sin[x3]^2);


and the system is

system = expr1 == a && expr2 == a && expr3 == -a;


Since expr1, expr2, expr3 are periodic functions with respect to x1, x2, x3 with the 2Pi period (we can easily check it with FunctionPeriod ) we restrict our seeking to the range 0 <= x1, x2, x3 <= 2Pi, by supplementing system with appropriate conditions. Moreover since there is a solution for a == 1/2 we restrict the searching by adding the following condition 1/2 <= a <=1. The overall conditions are:

conditions = 1/2 <= a <= 1 &&
0 <= x1 <= 2 Pi && 0 <= x2 <= 2 Pi && 0 <= x3 <= 2 Pi;


This yields a symbolic solution to the given problem.

sols = Reduce[ system && conditions,
{x1, x2, x3, a},
Backsubstitution -> True];


Symbolic solutions can be very useful in various tasks however we need not simplify them since we can just find their actual values:

N @ sols

(x1 == 4.17595 && x2 == 3.55883 && x3 == 2.10724 && a == 0.5) ||
(x1 == 4.17595 && x2 == 2.72436 && x3 == 2.10724 && a == 0.5) ||
(x1 == 5.24883 && x2 == 3.55883 && x3 == 1.03435 && a == 0.5) ||
(x1 == 5.24883 && x2 == 2.72436 && x3 == 1.03435 && a == 0.5) ||
(x1 == 1.03435 && x2 == 3.55883 && x3 == 5.24883 && a == 0.5) ||
(x1 == 1.03435 && x2 == 2.72436 && x3 == 5.24883 && a == 0.5) ||
(x1 == 2.10724 && x2 == 3.55883 && x3 == 4.17595 && a == 0.5) ||
(x1 == 2.10724 && x2 == 2.72436 && x3 == 4.17595 && a == 0.5)


In fact this demonstrates that the maximal value of a is 1/2.

A bit more straightforward way way of calculating the maximum value of a is an approriate use of NMaximize, now we need not use the parameter a and the condition 1/2 <= a <=1, thus we can get the value this way :

NMaximize[{ expr1, expr1 == expr2, expr1 + expr3 == 0,
Sequence @@ Rest@conditions}, {x1, x2, x3}]

 {0.5, {x1 -> 4.1759, x2 -> 2.72436, x3 -> 2.10719}}


Recalling that with NMaximize we get the global maximum, it was sufficient to get only one instance of arguments x1, x2, x3. For more information on this issue one can read these posts How do I determine the maximum value for a polynomial, given a range of x values? and How can I implement the method of Lagrange multipliers to find constrained extrema?. The method with Maximize has the advantage that we don't need any conditions on a. Analogically we can get the minimal value with NMinimize.

• Thank you for the help and sorry about the poorly worked question to begin with! Apr 16, 2016 at 16:28