449,081 Members | 861 Online Need help? Post your question and get tips & solutions from a community of 449,081 IT Pros & Developers. It's quick & easy.

# finding curve length

 P: n/a Hey guys, I'm trying to optimize a program that measures the length of a curve. Suppose I define a function f and I have two bounds [a,b] and am trying to find the arc length. The familiar calculus equation / | sqrt(1+f'(x)) dx provides us the arc length. / I'm trying to use C to numerically find the arc length. float metric(float x2, float x1){ return sqrt(pow(f((x2-x1),2))+pow(f(x2)-f(x1),2));} float lengthofcurve(float a, float b){ float delta=.000001; float sum=0; for(a;a
4 Replies

 P: n/a nm****@gmail.com wrote: Hey guys, I'm trying to optimize a program that measures the length of a curve. Suppose I define a function f and I have two bounds [a,b] and am trying to find the arc length. The familiar calculus equation / | sqrt(1+f'(x)) dx provides us the arc length. / I'm trying to use C to numerically find the arc length. float metric(float x2, float x1){ return sqrt(pow(f((x2-x1),2))+pow(f(x2)-f(x1),2));} float lengthofcurve(float a, float b){ float delta=.000001; float sum=0; for(a;a

 P: n/a wrote: I'm trying to optimize a program that measures the length of a curve. Suppose I define a function f and I have two bounds [a,b] and am trying to find the arc length. The familiar calculus equation / | sqrt(1+f'(x)) dx provides us the arc length. / Why are you telling us this? Is it just interesting background or does it have something to do with your code, per se? I'm trying to use C to numerically find the arc length. float metric(float x2, float x1){ return sqrt(pow(f((x2-x1),2))+pow(f(x2)-f(x1),2));} I may be totally missing the point. But, ignoring your calculus comment, ISTM that this should be: return sqrt( pow( (x2-x1), 2.0.) + pow ( (f(x2) - f(x1) ), ..0) ); But you indicate what you have is working - albeit with error. To me, it just looks like noise. You mention error, how much? 1%, 10%, 50%? As has already been pointed out, you should be using double instead of float. Float is rarely used in C, except for long term (disk) storage - if at all. float lengthofcurve(float a, float b){ float delta=.000001; float sum=0; for(a;a

 P: n/a Michael, Without getting bogged down with mathematical details, this is a simple elementary calculus application of curve length restricted to cartesian coordinates. No need to take into account parametrization. My problems were unfamiliarity with how C allocates memory for different variable types. I took your recommendations and did the following: 1. Changed variable type from float to double. 2. Decreased the value for delta, and got more accurate curve lengths but this resulted in much longer computation time. My question is whether there is a way to optimize speed and accuracy of the answer? Or is this question relying too heavily on numerical analysis and I should stay away from the C forums for this? Nov 15 '05 #4

 P: n/a Please quote enough context -- not everyone may yet have received my answer or even your original request. Without context, you take from yourself the chance for another person answering. nm****@gmail.com wrote: Michael, Without getting bogged down with mathematical details, this is a simple elementary calculus application of curve length restricted to cartesian coordinates. No need to take into account parametrization. My problems were unfamiliarity with how C allocates memory for different variable types. I took your recommendations and did the following: 1. Changed variable type from float to double. 2. Decreased the value for delta, and got more accurate curve lengths but this resulted in much longer computation time. Yep. Thus the hint to have a look at quadrature methods: There are methods with step length adaption which cover regions where f/f' changes quickly with small steps and other regions with large steps. The main thing, though, is going away from a naive floating point loop iteration. Rather use long i; for (i=0; i

### This discussion thread is closed

Replies have been disabled for this discussion. 