4
$\begingroup$

I'm facing the following problem: Given an implicit curve given by $f(x,y)=0$ and an initial guess $(x_0,y_0)$ which is hopefully close to the curve, I want to find the point $(x,y)$ on the curve which is closest to $(x_0,y_0)$.

My attempt:

I thought of using FindRoot with the equation repeated twice, i.e.

FindRoot[{f[x,y],f[x,y]},{{x,x0},{y,y0}}]

But this fails spectacularly with $f(x,y)=y-x^2$ and initial points $(0,y_0)$ with $y_0$ large.

Is there a good way to solve this problem?

Thanks!

$\endgroup$
6
  • 5
    $\begingroup$ NMinimize[{EuclideanDistance[{x, y}, {x0, y0}], f[x, y] == 0}, {x, y}]? $\endgroup$
    – user484
    Commented Jul 8, 2015 at 6:36
  • 1
    $\begingroup$ "closest" - that kind of screams for the use of Minimize[], no? $\endgroup$ Commented Jul 8, 2015 at 6:37
  • $\begingroup$ Thanks guys. You can close the question now. $\endgroup$
    – user1337
    Commented Jul 8, 2015 at 6:42
  • 2
    $\begingroup$ Better yet, why not answer your own question instead, so you can share what you've learned? $\endgroup$ Commented Jul 8, 2015 at 7:00
  • $\begingroup$ It might be better to minimize an EuclideanDistance square (a square of the euclidean norm instead of the norm.) $\endgroup$
    – Igor
    Commented Jul 8, 2015 at 11:55

1 Answer 1

12
$\begingroup$

RegionNearest will do this automatically for many cases, including $y-x^2=0$:

RegionNearest[ImplicitRegion[y - x^2 == 0, {x, y}], {0, y0}]

Mathematica graphics

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.