# Finding normals to a curve through a given point

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


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}

-
If your "curve" is a line, then you don't need optimization machinery; there's a nice geometric method for that purpose... –  Ｊ. Ｍ. Jun 10 '13 at 15:32
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
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
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
What is Ei and sol2? –  Silvia Jun 10 '13 at 21:16

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]


-
Of course, since the base curve whose normal you're seeking is a line, you can do this: -(x - 4)/0.36 + 3. –  Ｊ. Ｍ. 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
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.3681016266345978, "perplexity": 4968.415979389733}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 20, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2015-18/segments/1429246636213.71/warc/CC-MAIN-20150417045716-00027-ip-10-235-10-82.ec2.internal.warc.gz"}