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<b;a+=delta){
sum+=metric(a,a+delta);
}
return sum;}
Metric computes the length of two points and returnes their distance.
My results so far have been with a high degree of error. Any
suggestions on making the algorithm more efficient?
For one thing, you do not take parametrization into account,
i.e. whether f "goes throught the curve" at "constant speed"
or not.
In addition, you seem not to know enough about floating point
numbers, otherwise you would know that your summation loop
based on floating point numbers is a bad idea at best and
that "float" is probably not precise enough for your purposes.
Get a good book about numerical mathematics and start from the
beginning and go on until you have covered "quadrature"/numerical
integration.
Then test independently:
- The quality of your approximation of f' to a function f
for a parametrization from 0 to 1 via x, x*x, sqrt(x)
- The quality of lengthofcurve for exact derivative known
with given quadrature formula
Your implementation lacks in both areas.
Note that only the issues about float vs. double and your
for-loop are vaguely C-specific. The rest is off-topic round
here.
You may get better answers in newsgroups about programming
in general and scientific newsgroups dealing with numerical
mathematics.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.