Thread: Help me with this series

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    38

    Help me with this series

    Alright I'm having lots of trouble estimating pi using this series. I must be missing something.

    My book says you can estimate pi using this series:

    Code:
    pi = 4( 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - ... (-1 / 2i - 1) + (1 / 2i + 1) )
    I'm sure a lot of you are familiar with this.

    This is my code:

    Code:
    double sum = 0;
    for(int i = 0; i <= data2; i++){
        sum +=  ((- (1.0 / (2.0 * i - 1.0)))  + (1.0 / (2.0 * i + 1.0)));
    }
    sum *= 4.0;
    My outcomes are always like 4.45264 ... I'm thinking it has to do with my parenthesis, but I've rearranged them so many times I can't figure it out. Also, data2 is just some arbitrary value that the user enters.

    Any help would be greatly appreciated. I'm pretty stuck on this.
    I like to play pocket pool.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since the series is
    Code:
    pi = 4( 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - ... (+1 / 2i - 1) - (1 / 2i + 1) )
    that may help. Also i starts at 1 and goes by twos.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use a debugger to watch how sum changes on each iteration of the loop

    What I suggest is that on each iteration compute the current term, i.e., 1, then -1/3, then 1/5, etc. It looks like you are trying to do two terms at a time, but when i = 0 you get 2, which definitely is not the result of 1 - 1/3.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Okay so like tabstop said, it looks like the book f-up the series by putting the negative on the wrong one. I also incremented by two and am getting better answers. here's the code:

    Code:
    double sum = 0;
    for(int i = 1; i <= data2; i+= 2){
        sum +=  (( (1.0 / (2.0 * i - 1.0)))  + (-(1.0 / (2.0 * i + 1.0))));
    }
    sum *= 4.0;
    answer when data2 = 10: 3.0418

    answer when data2 = 100: 3.1315

    answer when data2 = 1000: 3.14059

    answer when data2 = 20000: 3.14154

    Is this how the series is suppose to work? Does this look correct?

    I think it's right but I just want to be sure, since I changed the formula that the book gave.
    I like to play pocket pool.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by NoUse
    I also incremented by two and am getting better answers.
    Ah, but if you want to group pairs of terms together instead then incrementing by four makes more sense:
    1/1 - 1/3
    1/5 - 1/7
    1/9 - 1/11
    Notice the 1, 5, 9, ... progression. The denominator of the second term in the pair is easily obtained by adding 2 to the denominator of the first.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Quote Originally Posted by laserlight View Post
    Ah, but if you want to group pairs of terms together instead then incrementing by four makes more sense:
    1/1 - 1/3
    1/5 - 1/7
    1/9 - 1/11
    Notice the 1, 5, 9, ... progression. The denominator of the second term in the pair is easily obtained by adding 2 to the denominator of the first.
    Thanks for your help and quick responses.

    We're suppose to be going by the book. One of the questions we have to answer using this program is how many terms of the series until you get your first 3.14159. Incrementing by 4 would ultimately change the series thus changing the amount of terms it takes to get there, would it not?
    I like to play pocket pool.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by NoUse
    We're suppose to be going by the book. One of the questions we have to answer using this program is how many terms of the series until you get your first 3.14159. Incrementing by 4 would ultimately change the series thus changing the amount of terms it takes to get there, would it not?
    No, it won't. The series itself is the same, of course, but what concerns the number of iterations is the fact that you are grouping consecutive terms into pairs. If your book told you to do this, then the number of iterations would be the same whether you increment by 2 or 4. The difference is that if you increment by 2, you would compute the terms as 1/(2i-1) and -1/(2i+1), but if you increment by four you would compute the terms as 1/i and -1/(i+2) respectively.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sine series summation
    By kashya in forum C Programming
    Replies: 3
    Last Post: 12-17-2008, 08:00 PM
  2. Cosine - Maclaurin Series
    By carrotcake1029 in forum C Programming
    Replies: 12
    Last Post: 12-07-2008, 12:20 PM
  3. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  4. Problem with implementing Liebniz series
    By C_Necessity in forum C Programming
    Replies: 6
    Last Post: 06-15-2005, 12:39 PM
  5. array of series
    By ronenk in forum C Programming
    Replies: 11
    Last Post: 06-22-2004, 01:06 PM