simple math: distance from a point to a curve

 

Simple math question:


In a plane there is a curve: y = (100-x)/x

and an arbitrarily chosen point, let's say (50,50)


How would you calculate (in mql4 or not) the -shortest- distance from that point to the curve?

You'd have to use orthogonal projection of that point onto that curve, but how to do that in code?

 

Pick a step on the x axis. Generate y with the curve equation, then apply distance formula. If your point is (x0,y0) and the plotted point on curve is (x1,y1), then distance is MathSqrt( (x0-x1)*(x0-x1) + (y0-y1)*(y0-y1) ). The minim of these distances will approximate what you seek.

 
TheEconomist:

Pick a step on the x axis. Generate y with the curve equation, then apply distance formula. If your point is (x0,y0) and the plotted point on curve is (x1,y1), then distance is MathSqrt( (x0-x1)*(x0-x1) + (y0-y1)*(y0-y1) ). The minim of these distances will approximate what you seek.


What you say is incorrect, the orthogonal projection onto a curve is not simply trying to find the minimum of its horizontal and vertical projection.

Say my point is (10,40), then its corresponding value on the curve is: (10,9) (vertical/down projection) and (100/41,40) (horizontal/to the left projection).

If you fill that into the distance formula, according to your explanation you would have to choose the shortest distance (between point-horizontal projection and point-vertical projection).

There should be a different fomula, and that is what I'm looking for:

the orthogonal projection onto a curve.


Maybe the distance is found by setting the first derivative of the function with respect to that point equal to 0? And taking the minimum of all those distances will be the shortest distance to the curve.

but the Math & Trig  functions don't allow derivation...

 
The point where derivative is 0 is a relative maximum/minimum of the curve, according to Fermat's theorem. Perhaps, forcing the things, you could input the distance formula into the function, and get the derivate, and solve it. Did it with Mathcad and the solve extends on a page...
 
Try this:
double x,y,s,sfin=100;
for(int i=1;i<100;i++)
{
x=i;y=(100-x)/x;
s=MathSqrt(MathPow(x-50,2)+MathPow(y-50,2));
if(s<sfin)sfin=s:
}
Print("shortest distance - ",sfin);
 
Yeah, that's what I told him to do ; however, this approach may jump large segments of the curve (for example the y for the first x in the series, where function decays rapidly). Another approach would be that after you have the distance function of the point to the curve, to solve it with a solve algorithm (e.g. division method). However, even this method has limits, function must be continuous and have only one root on the target interval where the root lies in.
 
TheEconomist:
Yeah, that's what I told him to do ; however, this approach may jump large segments of the curve (for example the y for the first x in the series, where function decays rapidly). Another approach would be that after you have the distance function of the point to the curve, to solve it with a solve algorithm (e.g. division method). However, even this method has limits, function must be continuous and have only one root on the target interval where the root lies in.

ok thx to you both, for a similar problem & answer:

http://answers.yahoo.com/question/index?qid=20070109172252AAP34wx

 
MrH:

ok thx to you both, for a similar problem & answer:

http://answers.yahoo.com/question/index?qid=20070109172252AAP34wx

1 more thing, would you pls upload the MathCad file?

I'm only beginning to use that program and would like to see how you solved it.

 
MrH:

1 more thing, would you pls upload the MathCad file?

I'm only beginning to use that program and would like to see how you solved it.

Sorry, I didn't save it. First you write the distance formula from within the curve function, similar to that yahoo answer. Can't say for sure, I don't have it installed on laptop. After you write the function, select it with the mouse, then should be in the Symbolics, menu, on Variable submenu.... better take the guide from here

Reason: