Antoine
Antoine

Reputation: 23

How to calculate the length of a curve of a math function?

I got curve C and I want to compute the curve length between its 2 points A,B:

f(x) = x² + 2x
C(   x,f(   x))
A(-2.0,f(-2.0))
B( 0.5,f( 0.5))

so x=<-2.0,0.5>

img

How to calculate the curve length between points A,B ? Of course I want to know how to calculate it on sheets :)

Thank you ;)

Upvotes: 2

Views: 4942

Answers (2)

Rory Daulton
Rory Daulton

Reputation: 22544

Here is Python 3 code that will approximate the length of an arc of a function graph. It is designed for continuous functions, though no computer program can do the infinitely many calculations needed to get the true result.

"""Compute the arc length of the curve defined by y = x**2 + 2*x for
-2 <= x <= 0.5 without using calculus.
"""
from math import hypot


def arclength(f, a, b, tol=1e-6):
    """Compute the arc length of function f(x) for a <= x <= b. Stop
    when two consecutive approximations are closer than the value of
    tol.
    """
    nsteps = 1  # number of steps to compute
    oldlength = 1.0e20
    length = 1.0e10
    while abs(oldlength - length) >= tol:
        nsteps *= 2
        fx1 = f(a)
        xdel = (b - a) / nsteps  # space between x-values
        oldlength = length
        length = 0
        for i in range(1, nsteps + 1):
            fx0 = fx1  # previous function value
            fx1 = f(a + i * (b - a) / nsteps)  # new function value
            length += hypot(xdel, fx1 - fx0)  # length of small line segment
    return length

def f(x):
    return x**2 + 2*x

print(arclength(f, -2.0, 0.5, 1e-10))

You can set the "tolerance" for the result. This routine basically follows the mathematical definition of the length of an arc. It approximates the curve with joined line segments and calculates the combined length of the segments. The number of segments is doubled until two consecutive length approximations are closer than the given tolerance. In the graphic below the blue segments are added together, then the red segments, and so on. Since a line is the shortest distance between two points, all the approximations and the final answer will be less than the true answer (unless round-off or other errors occur in the calculations).

The answer given by that code is

4.3052627174649505

The result from calculus, reduced to a decimal number, is

4.305262717478898

so my result is a little low, as expected, and is within the desired tolerance.

My routine does have some features to reduce computations and improve accuracy, but more could be done. Ask if you need more, such as the calculus closed form of the answer. Warning--that answer involves the inverse hyperbolic sine function.

enter image description here

Upvotes: 2

Spektre
Spektre

Reputation: 51845

You can simply compute many n points along the curve and add up the distances between them approximating your curve with many small line segments. That is actually how curve integration is done when the number of points goes to infinity. Without any higher math we can set n to some big enough value and add it in O(n) for loop. For example in C++ like this:

#include <math.h>
double f(double x){ return (x*x)+x+x; } // your function
double length(double x0,double x1,int n) // length of f(x) x=<x0,x1>
 {
 int e;
 double x,dx,y,dy,l;
 y=f(x0); dx=(x1-x0)/double(n-1); l=0.0; // start y and length
 for (e=1,x=x0+dx;e;x+=dx)  // loop through whole x range
  {
  if (x>=x1) { x=x1; e=0; } // end?
  dy=y; y=f(x); dy=y-dy;    // y=f(x) and dy is y-oldy
  l+=sqrt((dx*dx)+(dy*dy)); // add line length
  }
 return l;                  // return length
 }

use like this:

cout << length(-2.0,0.5,10) << endl;
cout << length(-2.0,0.5,100) << endl;
cout << length(-2.0,0.5,1000) << endl;
cout << length(-2.0,0.5,10000) << endl;
cout << length(-2.0,0.5,100000) << endl;
cout << length(-2.0,0.5,1000000) << endl;

when the result start saturating stop increasing n as you found your solution (with some error of coarse) Here results on my machine:

4.57118083390485
4.30516477250995
4.30776425810517
4.30551273287911
4.30528771762491
4.30526521739629

So we can round the answer to for example 4.305 ...

Of coarse if you compute the curve integral algebraically instead of this then you can obtain precise answer in O(1) if integrable of coarse...

Upvotes: 2

Related Questions