Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compute distance between two lines (sequence of 2-coordinate points) in Julia

Tags:

distance

julia

I have two functions, y = f(x) and y = g(x), defined through Array{Float64,2} -- a sequence of (x, y) points.

I want to find such f'(x) = f(x+dx) + dy that the distance between the functions on the chart is minimal. For example, here, the distance is already quite small, but it could be even smaller.

enter image description here

In my particular case, the number of points in two arrays is identical, so I tried to minimise the following function (assuming p = [dx, dy]):

loss(p) = sum(abs2, mapslices(x -> x .- p, f .- g, dims=2))

But, apparently, this doesn't work well. Also, it would be nice to be able to work with arrays of different sizes.

like image 389
leventov Avatar asked Jan 24 '23 13:01

leventov


1 Answers

So the steps you would need to take is:

  1. find the range of dx that will ensure at least 50% overlap of the curves
  2. define both curves, call them f and g, using interpolate (without extrapolate) from Interpolations.jl using the points you have
  3. define a function that will calculate the aggregate distance between them; the easiest would be to use QuadGK.jl with the range of integration based on dx and the range of curves (so that we integrate without extrapolating, call the ends of this range xmin and xmax): quadgk(x -> (f(x)-g(x+dy)+dy)^2, xmin, xmax)); make it a function of dx and dy
  4. then use e.g. Optim.jl to find optimal dx and dy (with the constraint on dx)
like image 82
Bogumił Kamiński Avatar answered May 01 '23 01:05

Bogumił Kamiński