1
$\begingroup$

I have two functions, $f(x)=\sin x$ and $g(x)=\sin\frac{pi}{4}x$, and I want to calculate the average Euclidean metric as an expected value to score for the average distance between the two functions on $l^2$.

Using the metric:

$d(f,g)=|f(x)-g(x)|=\sqrt{|x(t)|^2+|y(t)|^2}$

would be the best option. So I looked up at Wolfram Reference and found these two options,

EuclideanDistance[{a, b, c}, {x, y, z}]

NormalizedSquaredEuclideanDistance[{a, b}, {x, y}]

However, both are for vectors, and not for real-valued functions.

Any ideas appreciated.

Thanks

$\endgroup$
5
  • 1
    $\begingroup$ Use L2 Norm for two functions. Integrate may help. $\endgroup$
    – cvgmt
    Dec 7, 2022 at 9:16
  • $\begingroup$ Ok, I correct from $\mathbb{R}^2$ to $l^2$. But would you give an example? $\endgroup$
    – Vangsnes
    Dec 7, 2022 at 9:17
  • $\begingroup$ @cvgmt the integral converges if the function is first converted to a series. Thanks $\endgroup$
    – Vangsnes
    Dec 7, 2022 at 9:35
  • $\begingroup$ @cvgmt I tried also on the metric $d(x,y)=\sqrt{|x(t)|^2-|y(t)|^2}$ on $l_2$ space, using the functions converted to series. But the result, where the functions are converted to a series on a specific interval, gives only a complex function. Since I got magnitudes with the metric on $C[a,b]$, I think that is the only metric I can use, which you mention, integration. That means your suggestion works with the metric $d(x,y)=\int_a^b|x(t)-y(t)|$ , and gives ultimately a real value magnitude. This I cannot get with $l_2$ space, with the first norm. Does that sound correct to you? $\endgroup$
    – Vangsnes
    Dec 7, 2022 at 13:57
  • 3
    $\begingroup$ f[x_] = Sin[x]; g[x_] = Sin[π/4 x]; L2[a_, b_] = Integrate[(f[x] - g[x])^2, {x, a, b}, Assumptions -> a < b]^(1/2); l2[a_, b_] := NIntegrate[(f[x] - g[x])^2, {x, a, b}]^( 1/2); {L2[-2, 2] // N, l2[-2, 2]} $\endgroup$
    – cvgmt
    Dec 7, 2022 at 14:31

1 Answer 1

2
$\begingroup$

You can simply write out the integral:

f[x_] = Sin[x];
g[x_] = Sin[π/4 x];

dist[a_, b_] = Integrate[Abs[f[x] - g[x]], {x, a, b}, Assumptions -> a < b]
(*    ((π Cos[a] - 4 Cos[a π/4])/Sign[Sin[a] - Sin[a π/4]] +
       (-π Cos[b] + 4 Cos[b π/4])/Sign[Sin[b] - Sin[b π/4]])/π    *)

but the answer is wrong:

dist[-2, 2] // N
(*    -0.832294    *)

NIntegrate[Abs[f[x] - g[x]], {x, -2, 2}]
(*    0.32786    *)
$\endgroup$
1
  • $\begingroup$ Integrate[RealAbs[Sin[x] - Sin[Pi/4*x]], {x, -2, 2}] performs the correct answer (2 (-4 + \[Pi] Cos[2] - \[Pi] Cos[(4 \[Pi])/(4 + \[Pi])] + 8 Cos[\[Pi]^2/(4 + \[Pi])] + 2 \[Pi] Sin[(2 \[Pi])/(4 + \[Pi])]^2))/\[Pi]. $\endgroup$
    – user64494
    Dec 7, 2022 at 12:42

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