<s>[INST] Minimize function $\sqrt{x^2-4x+5}+\sqrt{4+x^2}$ Suppose I want to find
$$
\min f(x) = \min(\sqrt{x^2-4x+5}+\sqrt{4+x^2})
$$
I'd start with computing derivative and set it to $0$
$$
f'(x) = \frac{x-2}{\sqrt{x^2-4x+5}} + \frac{x}{\sqrt{4+x^2}}
$$
Then
$$
\frac{x-2}{\sqrt{x^2-4x+5}} + \frac{x}{\sqrt{4+x^2}} = 0
$$
$$
(x-2)\sqrt{4+x^2} + x\sqrt{x^2-4x+5}=0
$$
My first instinct is to rewrite it as
$$
(2-x)\sqrt{4+x^2} = x\sqrt{x^2-4x+5}
$$
and square both sides. The thing is, I get
$$
3x^2 -16x +16=0
$$
with $x_1 = 4/3$ and $x_2 = 4$. So $f'(x_1^-) <0$ and $f'(x_1^+) > 0$ thus in $x = 4/3$ there is a minimum of our function.
Question: is it valid to square both sides of an equation as I did above? Is there a possibility that because of that I lose or introduce a solution that shouldn't exist and might mess everything up?
 [/INST] You can square, but only after ensuring that you're equating numbers that are either both positive or both negative.
In your case you have
$$\textstyle
(2-x)\sqrt{4+x^2}=x\sqrt{x^2-4x+5}
$$
The square roots give no problem: they are both defined anywhere and positive. The sign is determined by the other factors.
If $2-x\ge0$, we also need $x\ge0$, that is, $0\le x\le2$.
If $2-x\le 0$, we also need $x\le0$, but this cannot happen.
Thus we can square, but we must discard roots that are not in $[0,2]$.
The equation you get is indeed $3x^2-16x-16=0$, with roots $4/3$ and $4$. The latter must be discarded.

By the way, the problem can be solved in a different way. Your function is
\begin{align}
f(x)
&=\textstyle\sqrt{x^2+4}+\sqrt{(x-2)^2+1^2} \\[6px]
&=\textstyle\sqrt{(x-0)^2+(0-2)^2}+\sqrt{(x-2)^2+(0-(-1))^2}
\end{align}
which is the sum of the distances of a point on the line $x=0$ from the points $(0,2)$ and $(2,-1)$.
You don't even need to compute the point of minimum, because the minimum is the distance between the two points, hence
$$\textstyle
\sqrt{(0-2)^2+(2+1)^2}=\sqrt{4+9}=\sqrt{13}
$$
If you want to find the point of minimum, the line joining the two points has equation
$$
\frac{y+1}{2+1}=\frac{x-2}{0-2}
$$
The only point $(x,0)$ on the $x$-axis satisfies
$$
\dfrac{1}{3}=\dfrac{2-x}{2}
$$
hence $x=4/3$.

</s>