Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to find x on curve $y(x)$, where curve $y(x)$ is perpendicular to point $(x_0,y_0)$. for this I minimize the distance between points $(x,y(x))$ and $(x_0,y_0)$, here's an example for point $x_0=4,y_0=3$.

y[x_] := 2 + 0.36 x
NMinimize[(4 - x)^2 + (3 - y[x])^2, x]

But somehow this does not give the right answer though Mathematica seems to be finding the right minimum.

{0.171388, {x -> 3.85977}}

enter image description here

Update:

In a more practical case, with Ei some data to be fitted, and sol2 the fitted coefficients of U[...] (refer to the end of the question for detail), this is what I tried:

Do[
 int = FindMinimum[{(Ei[1][[1, 2]] - U[x, 1] /. sol2[[2]])^2 + (i - x)^2}, x];
 AppendTo[perpendicular, int], 
 {i, 1, 30}]

cc = 
  ListPlot[{
    Evaluate @ Table[{i, Ei[1][[i, 2]]}, {i, 15, 23}], 
    Evaluate @
      Table[{x, U[x, 1] /. sol2[[2]]} /. perpendicular[[i, 2]], {i, 15, 23}]}, 
    Filling -> {1 -> {2}}, 
    PlotRange -> {0, 5}, 
    AspectRatio -> Automatic];

qq = 
  Plot[U[x, 1] /. sol2[[2]], {x, 15, 23}, 
    PlotRange -> {0, 5}, 
    AspectRatio -> Automatic];

enter image description here

This is the fitting function:

U[r_, o_] := 
  Sum[-ehh (1 - (1 - Exp[-Ahh (raa[i, j, o] - rshh)])^2), {i, 2, 
      5}, {j, 7, 10}] + 
    Sum[-ech (1 - (1 - Exp[-Ach (raa[i, j, o] - rsch)])^2), {i, 1, 
      1}, {j, 7, 10}] + 
    Sum[-ech (1 - (1 - Exp[-Ach (raa[i, j, o] - rsch)])^2), {i, 2, 
      5}, {j, 6, 6}] - 
    ecc (1 - (1 - Exp[-Acc (raa[1, 6, o] - rscc)])^2) /. x -> r;

And this the the fitted coefficients:

sol2 = {ehh -> -4.07603, Ahh -> 3.75309, rshh -> 1.44794, ech -> 0.223024, 
        Ach -> 1.51935, rsch -> 2.84276, ecc -> -4.52077, Acc -> 2.99396, 
        rscc -> 2.60361}
share|improve this question
1  
If your "curve" is a line, then you don't need optimization machinery; there's a nice geometric method for that purpose... –  J. M. Jun 10 '13 at 15:32
1  
I'm not following you. It looks to me the right answer indeed. Or do you mean the plot looks not perpendicular? Have you tried Plot with option AspectRatio -> Automatic? –  Silvia Jun 10 '13 at 16:11
4  
What @Silvia is saying is that the line through that point (probably) is perpendicular, and simply does not look so. This happens because aspect ratio !=1 will distort scale. –  Daniel Lichtblau Jun 10 '13 at 17:31
1  
I think you already found the actual perpendicular line. You just thought you haven't because a plot with aspect ratio $\neq 1$ misled you.. If you try Plot[y[x],{x,0,5},Prolog->{PointSize[.02],Through[{Point,Line}[{{4,3},{x,y[x]}/‌​.x->3.85977}]]},AspectRatio->Automatic], you'll see what I mean. –  Silvia Jun 10 '13 at 17:43
1  
What is Ei and sol2? –  Silvia Jun 10 '13 at 21:16
show 8 more comments

2 Answers 2

up vote 1 down vote accepted

If you make a line from blue and red points on the curve you will see they all are kinda pointing at one point.

There I found a mistake at your code, I think Ei[1][[1 , 2]] this is a static number, I guess it should be a various number changing by iterator i so you're finding minimum b=distance between 2 different points each time.

Do[
 int = FindMinimum[{(Ei[1][[i, 2]] - U[x, 1] /. sol2[[2]])^2 + (i - x)^2}, x];
 AppendTo[perpendicular, int], 
 {i, 1, 30}]
share|improve this answer
add comment
p = {x, y[x]} /. Last@NMinimize[(4 - x)^2 + (3 - y[x])^2, x]
(* {3.85977, 3.38952} *)

Dot[({4, 3} - p ), ({0, y[0]} - {5, y[5]})]

(* -3.9641*10^-8 *)

Numerically zero. As others said it doesnt look right on your plot because of the aspect ratio of the plot, try this:

Show[{ Plot[ y[x] , {x, 0, 5} ] ,  Graphics[Line[ {{4, 3}, p}]]}, 
       AspectRatio -> Automatic]

enter image description here

share|improve this answer
1  
Of course, since the base curve whose normal you're seeking is a line, you can do this: -(x - 4)/0.36 + 3. –  J. M. Jun 10 '13 at 18:07
    
I used you command AspectRatio -> Automatic but still in the curve i was working on actually it seems not perpendicular –  Raymond Ghaffarian Shirazi Jun 10 '13 at 19:27
    
Why not check mathematically if they are perpendicular, rather than by eye? Evaluate the slopes m1 and m2 at the intersection point and check that m1 == -1/m2 –  Corey Kelly Jun 11 '13 at 14:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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