# Minimizing cosine with constraints.

I want to solve the constrained minimization problem

\begin{align} \min_x \quad &\cos(x) \\ \text{s.t.} \quad & 0 \leq x \leq 2 \pi \end{align}

with Lagrange multipliers. Of course, the minimum is $$-1$$ at $$x = \pi$$, but I don't get that solution. My attempt:

\begin{align} \min_x \quad &f(x) \\ \text{s.t.} \quad & g_1(x) \leq 0 \\ & g_2(x) \leq 0 \end{align}

can be solved by taking the Lagrange function:

$$L(x, \lambda_1, \lambda_2) = f(x) + \lambda_1 g_1(x) + \lambda_2 g_2(x)$$

and setting its gradient to zero. In my example I have:

\begin{align} f(x) &= \cos(x) \\ g_1(x) &= -x \\ g_2(x) &= x - 2 \pi \end{align}

So the partial derivatives give me these equations:

\begin{align} \frac{\partial}{\partial x} L(x, \lambda_1, \lambda_2) &= -\sin(x) - \lambda_1 + \lambda_2 = 0 \\ \frac{\partial}{\partial \lambda_1} L(x, \lambda_1, \lambda_2) &= -x = 0 \\ \frac{\partial}{\partial \lambda_2} L(x, \lambda_1, \lambda_2) &= x - 2 \pi = 0 \end{align}

But this is bad, because from the second equation I get that $$x = 0$$ is a solution and from the third that $$x = 2 \pi$$ is a solution. But $$0 \neq 2 \pi$$ so there is something wrong because I should get $$x = \pi$$.

Where is the mistake here?

• the method of Lagrange multipliers is a strategy for finding the local maxima and minima of a function subject to equality constraints, whereas you have inequality constraints; just set $\dfrac{df}{dx}=0$ Commented Dec 22, 2019 at 1:45

In the present case, the optimum in the interior is $$-1$$ at $$x=0$$. The boundaries are single points, so there's nothing to optimize there; nevertheless, if you want you can apply the method of Lagrange multipliers roughly as you did (but separately in two separate applications of the method, once for each boundary) to recover the boundary points at $$x=0$$ and $$x=2\pi$$. Comparing the function values shows that the global optimum is the one in the interior.