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}
Plot
with optionAspectRatio -> Automatic
? $\endgroup$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. $\endgroup$Ei
andsol2
? $\endgroup$