Calculating arc length of function

Hi guys
This is more of a math question than a c++ question which is why im posting here under the lounge section. Hope thats okay.

I'm making a program that calculates the arc length of a function by basically splitting it up into several small pieces and using pythagoras to get an estimate of each of the small pieces then adding it all up.
(sorry im really bad at describing what i mean. Heres a link to what im talking about: https://www.khanacademy.org/math/multivariable-calculus/integrating-multivariable-functions/line-integrals-for-scalar-functions-articles/a/arc-length )

So basically i'm trying to write a program that calculates the following:
 
∫ √(dx^2 + dy^2)


The code looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
template <typename T>
T ComputeLen(T x1, T x2, T iters)
{
  T len{0};

  T diff = x2-x1;
  T incr;

  incr = diff / iters;

  for(T dx = 0; abs(dx) <= abs(diff); dx += incr)
  {

    //Pythagoras
    T a = incr;
    T b = f( x1+dx+incr) - f( x1+dx );

    //c = sqrt( a^2 + b^2 )
    T c = sqrt( (a*a) + (b*b) );

    len += c;
  }


  return len;
}

//Calculates the length of the function y=x^2 from 0 to 10
ComputeLen<double>(0.0f, 10.0f, 100000000);

("f" is just a function that takes a double and return its value squared, so: y=x^2)


Right now i have to pass it the amount of iterations i want between x1 and x2 and it will give me an estimate for the length. The problem is that i cant be sure how how accurate that estimate is - in terms of how many digits are accurate after the dot.

Does anyone know of a way to calculate the amount of iterations that is needed for a result with n-digit precision?
Last edited on
This is called the trapezium method, because a similar algorithm involving the areas of trapezoids is used to numerically approximate integrals.

Does anyone know of a way to calculate the amount of iterations that is needed for a result with n-digit precision?
That's not knowable in the general case. For example, no finite number of samples will let you get the arc length of sin(1/x) between -1 and 1.
You need to understand how that particular function behaves in that particular range to know how many samples to take.
For a general function you couldn't know in advance how many subintervals you would need. You would have to compute the arc length with N subintervals, then with 2N subintervals, and so on, seeing by how much your approximated arc lengths differed, and stopping when the difference got less than a certain tolerance.

If you take too many intervals then the truncation (i.e. theoretical approximation) error would be small, but you would start getting hit by rounding error instead.

There is a method known as Richardson extrapolation which allows you to both estimate the error and (less reliably) improve the solution when you have numerical approximations on N and 2N intervals.

However, if you want to estimate your error, then I don't think for a general function you could avoid doing the calculation at least twice.
Last edited on
Does anyone know of a way to calculate the amount of iterations that is needed for a result with n-digit precision?

Yes, simple (if you know how). lastchance describes it in other words I would have used (double intervals until arithmetical errors prevail, same as when you derive numerically.)
The method reminds me the title of a paper: "How Long Is the Coast of Britain?", Benoit Mandelbrot's first publish about fractional dimensions.
Topic archived. No new replies allowed.