Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find whole distance between two points in a curved line in R?

Tags:

r

distance

I have a similar line graph plotted using R plot function (plot(df))

enter image description here

I want to get distance of the whole line between two points in the graph (e.g., between x(1) and x(3)). How can I do this?

like image 291
Eka Avatar asked Oct 26 '25 12:10

Eka


1 Answers

  1. If your function is defined over a fine grid of points, you can compute the length of the line segment between each pair of points and add them. Pythagoras is your friend here:

    drawing of arc length of a small segment with the arc-segment as hypotenuse of a triangle

    To the extent that the points are not close enough together that the function is essentially linear between the points, it will tend to (generally only slightly) underestimate the arc length.

    Note that if your x-values are stored in increasing order, these ẟx and ẟy values can be obtained directly by differencing (in R that's diff)

  2. If you have a functional form for y as a function of x you can apply the integral for the arc length -- i.e. integrate

    ∫ √[1+(dy/dx)²] dx

between a and b. This is essentially just the calculation in 1 taken to the limit.

  1. If both x and y are parametric functions of another variable (t, say) you can simplify the parametric form of the above integral (if we don't forget the Jacobian) to integrating

    ∫ √[(dx/dt)²+(dy/dt)²] dt

    between a and b

    (Note the direct parallel to 1.)

  2. if you don't have a convenient-to-integrate functional form in 2. or 3. you can use numerical quadrature; this can be quite efficient (which can be handy when the derivative function is expensive to evaluate).

like image 69
Glen_b Avatar answered Oct 29 '25 03:10

Glen_b



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!