I am seeking a minimum of a function under an inequality constraint. How can I set stop condition? The problem is that $\nabla f_p$ never goes to zero. The function:
$$f(x_1, x_2)=\left(x_1 - 1\right)^2 + 2\left(x_2 - 2\right)^2$$
The constraint
$$g(x_1,x_2)=x_1^2 + x_2^2-1\leq 0$$
Objective function (with penalty method):
$$f_p(x_1,x_2)=\left(x_1 - 1\right)^2 + 2\left(x_2 - 2\right)^2+\mu\left(\max\left\{ x_1^2 + x_2^2-1, 0\right\}\right)^2$$
Its gradient (outside valid domain): $$\nabla f_p=\left(2\left(x_1 -1\right)+4\mu x_1^3\right)\widehat{x_1} + \left(4\left(x_2 - 2\right) + 4\mu x_2^3\right) \widehat{x_2}$$
Its gradient (inside valid domain): $$\nabla f_p=2\left(x_1 -1\right)\widehat{x_1} +4\left(x_2 - 2\right) \widehat{x_2}$$
It appears that $|\nabla f_p|$ oscillates around a value greater than one so testing $|\nabla f_p|$ does not work, or are the conditional derivatives wrong?