How Long is a Piece of String? Clever Dood Needed!

This is a discussion on How Long is a Piece of String? Clever Dood Needed! within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I have a maths problem. My program is rendering a sine wave of the form: y = oy + (ay ...

  1. #1
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812

    Question How Long is a Piece of String? Clever Dood Needed!

    I have a maths problem.

    My program is rendering a sine wave of the form:

    y = oy + (ay * SIN(ax * x))

    So in ASCII art it looks like :

    Code:
    y   _
    |  / \
    |     \_/
    |______x
    Here's my problem. I want to calculate the path length of the wave. Furthermore, I want to calculate the path length traversed from starting point (0, 0) to some point on the x axis.

    In otherwords, if we draw the axes on the ground using inches as our dimensions. Then laid out a piece of string in the shape of the sine wave. How long does the string need to be to reach a distince x along the x-axis from the origin?

    I believe this problem is solvable with cantenary equations, but my maths is upto scratch with these.

    Any ideas?
    Last edited by Davros; 10-18-2002 at 11:11 AM.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    my calculus is not so hot anymore but im sure the length of a curve can be found with integration.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >im sure the length of a curve can be found with integration

    Ahh! I spent the last two weeks wrestling with this. Integration gives the area under the curve, not the length of the curve.

    But thanks for replying.

    The only way I know how to solve this is numerically. Inefficient, but I know it will work.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    The length of a curve y=f(x) from x=a to x=b is

    §[a,b] (1-f'(x)^2)^0.5 * dx

    where § is the integral sign.

    So, you need to know the derivative of f(x)

    It can be shown if you rewrite y=f(x) in parametric form.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Davros
    Integration gives the area under the curve, not the length of the curve.
    As you can see, integration can be used to perform lots of things.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    I am suitably impressed!

    But still need help. I haven't done any maths for 8 years.

    So if my:

    f(x) = ay + SIN(ax * X)

    then

    f'(x) = ay * ax * COS(ax * x) [I think]

    I plug this into:

    LEN = §[a,b] (1-f'(x)^2)^0.5 * dx

    and get :

    LEN = §[a,b] ( (1 - (ay * ax * COS(ax * x)))^2)^0.5 * dx

    which equals

    LEN = §[a,b] (1 - (2 * ay * ax * COS(ax * x)) + (ay^2* ax^2 * COS(ax * x)^2) )^0.5 * dx

    So how do I integrate that lot! Which I've probably got wrong anyhow!
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Davros

    So if my:

    f(x) = ay + SIN(ax * X)

    then

    f'(x) = ay * ax * COS(ax * x) [I think]
    No.
    f'(x) = ax*cos(ax*x)


    And I think you misinterpreted my formula:
    It should read:

    §[a,b] (1 - (f'(x))^2 ) ^ 0.5 * dx

    Arrgg!! I hate typing math using ascii!!
    I've attached an image of the expression, I've replaced "ax" with "a" to avoid ambiguousness (I hate spelling that word, btw).

    I don't think that integral can be solved using algebraic methods, though.
    Attached Images Attached Images  
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Thanks for that.

    If as you say, the integral [probably] cannot be solved with algebraic methods, am I correct in thinking that I need a numerical solution to my problem?

    But thanks for your help.

    (I'll go way now & do some maths revision.)

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Davros
    If as you say, the integral [probably] cannot be solved with algebraic methods, am I correct in thinking that I need a numerical solution to my problem?
    Yes.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    Actually, im almost certain it can be, when I get home Ill give it a whack (hint: 1 - cos^2 = sin^2)
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  11. #11
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Your arc length formula is wrong..

    §[a,b] (1 - (f'(x))^2 ) ^ 0.5 * dx

    should be

    §[a,b] (1 + (f'(x))^2 ) ^ 0.5 * dx

    I am trying to do the problem... not too easy though .
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  12. #12
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    darn, cant do it. I was thinking of some sort of substitution, but it just got uglier, or back to where I started. Anyways, there probably isnt an antiderivative of that since it wasnt even in the integral table in my calc book. Oh well, theres always Simpson's Rule
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  13. #13
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by SilentStrike
    §[a,b] (1 - (f'(x))^2 ) ^ 0.5 * dx

    should be

    §[a,b] (1 + (f'(x))^2 ) ^ 0.5 * dx
    Yes, thanks for the correction.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 12:29 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 06:59 AM
  4. need help
    By emperor in forum C Programming
    Replies: 1
    Last Post: 03-04-2002, 12:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21