# distance between two curves

I try to compute the distance between two curves. I use the EuclideanDistance to do that.

Here my code:

j = -1;
a32 = 3.9683436;
a43 = 4.2925064;
alfaF32 = 1.54553;
alfaF43 = 2.34472;
mu = 10^-3;
tot4 = alfaF32*mu;
tot5 = alfaF43*mu;

S = ((EuclideanDistance[{a43 (-Sqrt[(16 tot5*i)/
3 (1 + tot5/(27 i^3))] - (2 tot5)/(9 j*i)) +
a43}, {a32 (Sqrt[(16 tot4*i)/3 (1 + tot4/(27 i^3))] - (
2 tot4)/(9 j*i)) + a32}])/(3/2 - 4/3))^2;
n = ListPlot[Table[S, {i, 0.0001, .3, 0.0001}]]


But the x-axis is wrong, i try to explan. The x-axis must go from 0.0001 to 0.3 as in the S function. But the x-axis is completly wrong (the dimension is from 0.3/0.0001). Someone could help me?

• Could you please give us your definition of the "Euclidean" distance between curves? There are many possible distances based on Euclidean distances--Hausdorff distance is one, for instance, or perhaps you are looking for the $L^2$ distance between graphs of functions? – whuber Apr 25 '13 at 17:29
• It looks like you are measuring vertical differences at successive points on the horizontal axis. That's not what one generally means by "distance between" two curves. – Daniel Lichtblau Apr 25 '13 at 17:37
• If you don't provide x values in the ListPlot Mathematica assumes x values 1, 2, 3.... So change Table[S to Table[{i,S} and it should work. By the was, it's better not to start variables with uppercase characters, in order not to confuse them with built in ones. – Sjoerd C. de Vries Apr 25 '13 at 17:42

I'm not really sure if what you are doing makes any sense, but this code seems to implement that dubious thing:

f1[i_] := a43 (-Sqrt[(16 tot5*i)/3 (1 + tot5/(27 i^3))] - (2 tot5)/(9 j*i)) + a43;
f2[i_] := a32 (Sqrt[(16 tot4*i)/3 (1 + tot4/(27 i^3))] - (2 tot4)/(9 j*i)) +  a32;

s[i_] := (EuclideanDistance[f1[i], f2[i]]/(3/2 - 4/3))^2;

n = ListPlot[Table[{i, s[i]}, {i, 0.0001, .3, 0.0001}]]


I can't tell what you're doing either, but here's an idea using Nearest:

l1 = Table[{x, 1 + x^2}, {x, -2, 2, .005}];
l2 = Table[{x, 1/2 x^2}, {x, -2, 2, .005}];
d = {#[[1]], EuclideanDistance[#, First@Nearest[l2, #]]} & /@ l1;

ListLinePlot[{l1, l2, d}]


I don't know if there's a formal name for this nearest-point distance. In any case, the parameterization of this plot is questionable, so I think a better visualization would be something along the lines of this:

fD[f1_, f2_, iter_, opacity_: .5, colorF_: ColorData["TemperatureMap"]] :=
Module[{l1, l2, d, minmax},
l1 = Table[{First[iter], f1}, iter];
l2 = Table[{First[iter], f2}, iter];
d = Module[{nearest = First@Nearest[l2, #]},
{#, nearest, EuclideanDistance[#, nearest]}] & /@ l1;

minmax = {Min[Last /@ d], Max[Last /@ d]};
d = {#1, #2, Rescale[#3, minmax]} & @@@ d;

Show[ListLinePlot[{l1, l2}, AxesStyle -> Gray],
Graphics[{Opacity[opacity], {colorF[#3], Line[{#1, #2}]} & @@@ d}]]];

fD[1 + x^2, 1/2 x^2, {x, -2, 2, .001}]


And then, because deep down we're all some manner of deranged mad scientist hellbent on the destruction of this planet (or at least I am):

slides = ParallelTable[
Rasterize@fD[Sin[-offset + x^2], Cos[offset + 1/2 + x^2], {x, 0, 2 Pi, .001}],
{offset, 0, 2 Pi, 2 Pi/40}];


MUAHAHAHA. *goes mad with power* (Note for serious use the function needs HoldAll etc.)

Edit: Another issue I just noticed is that without restricting the plot range, you will get incorrect visuals at the ends of these kinds of plots. That's what accounts for the quirky banding at the ends of the parabolic plot.