I am not sure if this is on-topic here, but I am trying.

Let $x,y$ be positive real numbers. I am trying to find $$ \min_{\sqrt{a}+\sqrt{b}=\sqrt{2}}(x-a)^2+(y-b)^2$$

I tried using Mathematica for this, by running

Minimize[{(x - a)^2 + (y - b)^2, Sqrt[a] + Sqrt[b] == Sqrt[2]}, {a, 

but the computation didn't terminate. I am not experienced in using computational software tools. What are good suggestions to handle this problem? (e.g. Will Python be a good choice here? what should I try?).

I guess approximations for the optimum will also be interesting for me.

It seems that to solving this problem analytically won't be feasible.


I am not sure if it matters, but in my case I also know that $x,y$ satisfy $xy \le \frac{1}{4}$. Since, $\sqrt{a}+\sqrt{b}=\sqrt{2}$, the arithmetic-geometric inequality implies that $ab \le \frac{1}{4}$. Maybe these facts can be used here somehow, I am not sure.

  • $\begingroup$ Are you actually looking for a generic approach, or just to come up with a function for this specific problem? In the latter case, couldn't you just substitute a=c^2, then cancel the b and proceed analytically? $\endgroup$
    – origimbo
    May 1, 2020 at 13:42
  • $\begingroup$ I am interested in the function for this specific problem. It seems that trying to solve this analytically (by hand) gets you to a cubic equation with rather messy formulas for the roots. (and it doesn't matter if you express $b$ in terms of $a$, or use Lagrang'es multipliers). $\endgroup$ May 1, 2020 at 13:58
  • 1
    $\begingroup$ As aside, symbolic math packages (e.g. sympy, since you mentioned Python, but Mathematica will do it) can handle solving cubic equations very readily. $\endgroup$
    – origimbo
    May 1, 2020 at 14:25
  • $\begingroup$ @AsafShachar The answer in Mathematics Stack Exchange that you linked to reduces the problem to solving a cubic. An analytic solution to a general cubic equation is possible, although the result will be a complicated expression. On the other hand, if you are satisfied with a numerical solution, then that should be straightforward. $\endgroup$
    – gandalf61
    May 1, 2020 at 15:02
  • 1
    $\begingroup$ @FedericoPoloni, would you mind summarizing your comments as an answer? $\endgroup$
    – nicoguaro
    May 1, 2020 at 18:06

2 Answers 2


From a comment: I suggest you to set $\sqrt{a}:=c$ and $\sqrt{b}:=d$ and then pass the problem in the variables c,d to whatever computational software you are using. I would avoid those non-smooth square roots in the constraints at all costs if it's possible.

The general idea (from a very philosophical standpoint; this feels more like a comment than an answer to me) is that in many contexts all that we really know how to solve, especially for large dimensions, are linear problems: solving linear systems of equations is an easy task, and an explicit procedure to minimize linear functions with linear constraints is taught in all undergraduate optimization courses. Probably Mathematica can make linear substitutions and solve linear problems easily.

Whenever you have a non-linear problem, often its numerical solution involves replacing it with a linear approximation: think about Newton's method, or the idea of derivatives itself. If the constraint is non-smooth, linearizing it is more difficult.

  • $\begingroup$ Thanks. One question that I have in mind about that it the following: Does it really matter that the constraints are non-differentiable? Doesn't the fact that the objective function we want to optimize is smooth in the parameters enough? I guess this might depend on the exact implementation/algorithm the software uses... $\endgroup$ May 3, 2020 at 9:21
  • $\begingroup$ It definitely depends on the algorithm. I am not expert enough to discuss algorithms in detail, sorry; also, I would guess that Mathematica uses some mixed symbolic/numeric method that is even more unusual. $\endgroup$ May 3, 2020 at 12:06

@federicopolini is right in his answer: Introduce $$ c= \sqrt{a}, d=\sqrt{b} $$ and your optimization problem will now read as follows: $$ \min (x-c^2)^2+(y-d^2)^2 $$ subject to the constraints $$ c+d = 2, \\ c\ge 0,\\ d\ge 0. $$ The inequality constraints are important to ensure that you get a solution that makes sense.

Now, you can eliminate $d=2-c$ and obtain the following problem instead: $$ \min (x-c^2)^2+(y-(2-c)^2)^2 $$ subject to the constraints $$ c\ge 0,\\ c\le 2. $$

You can solve this analytically: find all three values for $c$ for which $\frac{d}{dc}\left[(x-c^2)^2+(y-(2-c)^2)^2\right]=0$. Then you need to check which ones of these are actually minima by ensuring that $\frac{d^2}{dc^2}\left[(x-c^2)^2+(y-(2-c)^2)^2\right]>0$. And of those you then need to compare whether they are smaller or larger than the values of $(x-c^2)^2+(y-(2-c)^2)^2$ at $c=0$ and $c=2$ to cover the values at the end points of the feasible interval.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.