473,395 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

finding curve length

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?

Thanks

Nov 15 '05 #1
4 6119
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.
Nov 15 '05 #2
<nm****@gmail.com> 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<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?


So you want to compute the wrong answer faster? Or what?
Nov 15 '05 #3
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
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<NUM_STEPS; i++) {
h = (double)i/NUM_STEPS;
....
}
for simple iterations or iterate until the forward interval
boundary (i.e. a + h) exceeds your upper integration limit
and do the last part of the summation by hand.
Reason:
for (f=0.0F; f < 1.0F; f += 0.1F) {
....
}
does not necessarily give you ten loop iterations and almost
certainly leave the loop with f != 1.0F because 0.1 cannot
be represented exactly in float (the same applies to double
and long double, of course).

Then think about improving your approximation of f'.

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?


IMO, the latter. If you have finally a C program and have
problems with the _C_ aspects, then you will receive excellent
help here.
However, it makes much more sense to ask where the experts are.
Even though I studied maths, I may have forgotten enough to
lead you into the completely wrong direction.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: B Moor | last post by:
I have a database with 100,000's records, each with a unique reference, eg A123BNK456 I would like to generate a search facility whereby we can choose an exact match or partial match, where the...
0
by: James Dean | last post by:
This may not be related to this newsgroup but i thought i would ask anyway. I am doing a project in C#. I would like to know of any good sites or information about Line and curve fitting. I would...
2
by: Extremest | last post by:
Here is the code I have so far. It connects to a db and grabs headers. It then sorts them into groups and then puts all the complete ones into another table. Problem I am having is that for some...
4
by: babyinc | last post by:
Please help me. I am totally beginner of C++ and my tutor give us these huge program to solve within 20 days. As a MSc student I am really gonna cry. Please anyone can help me. sumon1in1@yahoo.com ...
1
by: Mike Heywood | last post by:
Hi, I am currently trying to automate a process that I have been studying the manual results from for a while. The process simply identifies events that meet certain criteria and at the moment...
3
by: BSDSteve | last post by:
Relatively new to Java and have a question. I need to be able to take screenshots of an entire web page. The issue that I'm having is finding the page length of a page displayed within the...
26
by: K.J.Williams | last post by:
Hello, A friend and I want to learn PHP but we have two totally different programming backgrounds. I have experience with procedural programming in C, and he has experience with Visual BASIC....
0
by: sa6113 | last post by:
Hello there, I have a problem on curve fitting , would you please help me ?! I want to to develop a application that reads a text file with 2 columns of floating point data (as x and y) and...
3
by: cmwb2000 | last post by:
Hi I am trying to test to see if there is a link between the curve of a mouses movement and the length from the ball to the wrist (pivet point). I am recording a series of points as the mouse...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.