Like Tree1Likes

Approximating PI using the Taylor Series - Help!

This is a discussion on Approximating PI using the Taylor Series - Help! within the C++ Programming forums, part of the General Programming Boards category; Hey. I want to write a program that approximates the value of PI using the Taylor Series. This is the ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    1

    Question Approximating PI using the Taylor Series - Help!

    Hey. I want to write a program that approximates the value of PI using the Taylor Series. This is the program I've written so far but I'm not sure how to complete the FOR loop. Someone please help.

    Code:
    #include <cstdlib>
    #include <iostream>
    
    
    using namespace std;
    
    
    int main(int argc, char *argv[])
    {
        cout<<"Piapproximation\n"
        "Welcome to my version of Piapproximation\n";
        //declaring variables
        int terms;
        double pi = 0.0;
        //getting input from user
        cout<<"\nEnter the number of terms: ";
        cin>> terms;
        //loop to calculate the value of pi
        for (long int i = 1; i <= terms; i ++) 
        ...
        //output 
        cout<<"\nThe approximation of PI is: "<< pi <<
        "\n\nThank you!\n";
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Use a flowchart to complete your logic, then translate to code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I remember a π approximation we had done.We had used a do while loop,because the concept was to stop while the term was too small for us to be taken into account.We calculated the π which comes from the greek(of course) word περιφέρεια (=regional) by this series
    Code:
    π/6 = 1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + ...
    The tolerance is achieved by this
    Code:
     while (currentTerm > 1.0e-15);  /* Stop if current term is very small */
    You can change the tolerance for more or less precision

    Try writing your program and post back if needed
    Last edited by std10093; 08-05-2012 at 03:34 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Dude, do not hand out solutions!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Elysia View Post
    Dude, do not hand out solutions!
    Edited post.Sorry Elysia,this is the second time i make the same mistake! :/

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    We calculated the π which comes from the greek(of course) word περιφέρεια (=regional) by this series
    Given the definition of pi I find that odd. It has nothing to do with circumference and wikipedia says you're wrong. Are you really right?

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by whiteflags View Post
    Given the definition of pi I find that odd. It has nothing to do with circumference and wikipedia says you're wrong. Are you really right?
    Well i wasn't there when great greek mathematicians lived,so i will tell you what i was told .It is said that because if you select a random cycle and you calculate the fraction of its regional (=περιφέρεια) to its diameter (=διάμετρος) then you always get the number π.As a result π is said to be derived by the word περιφέρεια. It is also said that Epimarchos (=Επίμαρχος) invented the letter π in greek alphabet,but this is mythology.
    So my answer is i do not know 100%,but this is a very interesting question.I will search it and i will post it to the general discussions thread if i have something.For now let's stick to the NewP's code and do not get much off topic

    EDIt- another idea is that π could be derived by the greek word περίμετρος (=perimeter)
    Last edited by std10093; 08-05-2012 at 04:21 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Quote Originally Posted by std10093 View Post
    Edited post.Sorry Elysia,this is the second time i make the same mistake! :/
    Well, if you did it by mistake, then we're fine
    We all make mistakes
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Please have a go at writing the loop body and then post your attempt.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588
    the taylor series is a really lousy approximation for pi though.

    given the formula π/6 = 1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + ... + 1/n

    n must be a VERY large number to get useful results.

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Elkvis View Post
    the taylor series is a really lousy approximation for pi though.

    given the formula π/6 = 1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + ... + 1/n

    n must be a VERY large number to get useful results.
    That's not wrong.With the precision i stated before
    Code:
    while (currentTerm > 1.0e-15);
    i got this result.

    output
    Code:
    Summed 31622777 terms, pi is 3.14159
    However this a school project,rather than a scientific application so i thought it would be ok

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588
    I wrote a multithreaded version that does it 40 billion times, and got 3.141592645... - should be 3.141592653...

    given a sufficiently fast computer, you could calculate it within the precision of a double in a reasonable amount of time.

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I calculated the absolute error of that(where x' is the machine number)
    x' = 3.141592645

    x= 3.141592653 (actual value)
    Code:
    |ε| = |x - x'| = 0.000000008 = 0.08 * 10^(-7) < 0.5*10^(-7)
    which tell us that the machine value is going to be accurate at 7 decimal digits at the most.Here it is exact 7 digits.

    Then i calculated the absolute relative error
    Code:
    |ρ| = |ε| / x = 0,025464790899483937645941521750815 * 10^(-7) = 0,025464790899483937645941521750815 * 10^(-9) < 5 * 10^(-9)
    which says that at the most nine significant digits are going to be accurate.Here 8 digits are accurate.

    However i think that this accuracy is satisfactory for a school exercise,so the series i gave before i think is enough

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588
    either I'm missing something, or you're leaving out a lot of information about the formulas you used to calculate that absolute relative error.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    It says right there,
    Code:
    |ρ| = |ε| / x = |x - x'| / x
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Taylor series regarding E^x
    By rafacnsoccer in forum C++ Programming
    Replies: 8
    Last Post: 05-05-2010, 12:08 PM
  2. taylor series using only stdio.h
    By b0ss139 in forum C Programming
    Replies: 4
    Last Post: 07-25-2009, 11:32 PM
  3. help on c programming involving taylor series
    By harlow23 in forum C Programming
    Replies: 4
    Last Post: 05-04-2007, 11:03 PM
  4. taylor series using only stdio.h
    By faruque in forum C Programming
    Replies: 1
    Last Post: 02-13-2003, 12:50 PM
  5. taylor series expansion
    By noor_mirza in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2002, 11:02 PM

Tags for this Thread


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