Finding normals to a curve through a given point

I want to find x on curve y(x)y(x)y(x), where curve y(x)y(x)y(x) is perpendicular to point (x0,y0)(x_0,y_0).
for this I minimize the distance between points (x,y(x))(x,y(x)) and (x0,y0)(x_0,y_0), here’s an example for point x0=4,y0=3x_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}}

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];

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}

=================

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 ≠1\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

=================

2 Answers
2

=================

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}]

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]

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