I have a project where I need to know the exact minimal distance between a point $(e, f)$ and a sine wave $y = a + b\cdot\sin(cx+d)$

Is there any way of calculating this? If not, is there a way to approximate this?

Thanks in advance!

Edit

Following Ross' and Ben's answers, The distance between a point $(e,f)$ and a sine wave $y = a + b\cdot\sin(cx+d)$ can be calculated by defining it as a distance between two points: $$D=\sqrt{(x-e)^2+(a+b\cdot\sin(cx+d)-f)^2}$$ The $x$ where the distance is minimal can then be calculated by $E=D^2$ and then solving $E'=0$ $$E'= 2(bc\cdot\cos(cx+d)(a+b\cdot\sin(cx+d)-f)-e+x)=0$$ $$x=e-bc\cdot\cos(cx+d)(a+b\cdot\sin(cx+d)-f)$$ This last formula can only be answered exactly in certain cases.

  • I meant to use them as different values, sorry to be unclear – Wouter Raateland Oct 4 '13 at 18:30
  • No problem, it happens to the best of us. – Dan Rust Oct 4 '13 at 18:34
up vote 0 down vote accepted

Following Ross' answer, I will use $(e,f)$ to denote your point.

First, some simplifications:

  • The minimal distance between $(e,f)$ and $y = a + b\sin(cx + d)$ is the same as the minimal distance between $(e,f - a)$ and $y = b\sin(cx + d)$. So if I can solve the problem with $a = 0$, I can easily solve it for other cases as well.
  • Similarly, the minimal distance between $(e,f)$ and $y = b\sin(cx + d)$ is the same as the minimal distance between $(e + d/c,f)$ and $y = b\sin cx$, as long as $c \not= 0$. If $c = 0$ then the problem is not very difficult.
  • Again, assuming $c \not= 0$, the minimal distance between $(e,f)$ and $y = b\sin cx$ is the same as $1/c$ times the minimal distance between $(ec,fc)$ and $y = bc \sin x$.

So it's enough to solve the problem in the form "find the minimal distance between $(e,f)$ and $y = a\sin x$".

The points on the sine curve are of the form $(x,a\sin x)$. The distance to such a point is \[D = \sqrt{(x - e)^2 + (a\sin x - f)^2}.\] When this reaches a minimum, its derivative will be zero. As Jack M points out in the comments, you can throw away the square root, if you like: $D$ will reach its minimum exactly when $D^2$ does.

I've no patience for algebra so I leave the rest to you (unless you really get stuck), but I suspect you'll not end up with a neat expression, because there'll be too many trig functions in there. There are various ways to proceed from there, but I don't currently have time to go into them.

  • Thanks, this will get me there! I will update my question when I finished this piece of algebra. – Wouter Raateland Oct 4 '13 at 18:57
  • 2
    Do you need to keep the square root on $D$? If you minimize $D^2$ you'll have minimized $D$. – Jack M Oct 4 '13 at 19:00
  • Having both $x$ and $\sin x$ will (almost certainly) prevent an algebraic solution-you are into numerics. The choice between minimization and root finding is not easy. Root finding may be more accurate in $x$ because it avoids an $\epsilon^2$ problem, but the distance should be equally accurate. – Ross Millikan Oct 4 '13 at 19:46

Let your point be $(e,f)$ so we don't reuse $x,y$. An approximate approach is as follows: First, find the limits of the half wave of interest. Let's say we are above the curve. You want the local minimum nearest $e$ and the local maximum on the other side of $e$. The perpendicular at a point has slope that is the inverse reciprocal of the derivative. The perpendicular from the maximum will be vertical and on one side of $(e,f)$, the perpendicular from the minimum will be on the other side of $(e,f)$ Call up your favorite one-dimensional root finder to find the $x$ value where the perpendicular goes through $(e,f)$. We have bracketed the root, so it should be easy to find. Now find the distance from $(x,y)$ to $(e,f)$

  • @Macavity: thanks. I have changed to $e,f$ – Ross Millikan Oct 4 '13 at 18:04
  • Thanks for the quick answer! So if I am right, you said to take one half of a sine wave, calculate its derivative and then check at which x the value of the derivative is equal to the peripendicular of the slope of an imaginary line between point (e, f) and the x and the value y at that x? – Wouter Raateland Oct 4 '13 at 18:34
  • That is correct. As the sign of the error changes, a root finder should get there very quickly. They are in many numeric packages and described in any numerical analysis text. – Ross Millikan Oct 4 '13 at 19:43

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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