Calculates different formulas that converge to the number pi

Assignment Help C/C++ Programming
Reference no: EM13163420

Write a C program (Care should be taken not to use extensions of C + + language in your work ) that calculates different formulas that converge to the number pi, to compare the speed of convergence of these formulas. Each form will be calculated by a different function. Each function will have the same parameter that indicates the number of terms to be used in the formula to approximate the value of the number pi. Here are the formulas that your program must calculate, in order of the speed convergence from the slowest to the fastest:

A) The first formula, very simple but the convergence is very slow, was discovered by Leonhard Euler:   

pi =    \(\sqrt{6\sum_{k=1}^{\infty} (1/k^{2})}\)   

Each partial sum Ek of this series can be expressed in terms of the first k terms, as follows:

Partial sum # 1 (first term): E1= sqrt(6)

Partial sum # 2 (first two terms): E2=sqrt(6)* sqrt(1+(1/4))

Partial sum # 3 (first three terms): E3= sqrt(6) * sqrt(1+(1/4)+(1/9))

Partial sum # 4 (first four terms): E4= sqrt(6)* sqrt(1+(1/4)+(1/9)+(1/16))

Etc.

B) The second formula, very elegant and discovered by William Brouncker is expressed as a continued fraction, but it is also very slow convergent

 

(4/pi) = 1 + (12 / 2 + (32 / 2 + (52 / 2 +( 72 / 2+ (92 / )

Each partial sum Fk of this series can be expressed in terms of the first k terms, as follows:

Partial sum # 1 (first term): F1= 1 + (12 /2 )

Partial sum # 2 (first two terms): F2= 1 + (12 /(2 + (32 / 2 )))

Partial sum # 3 (first three terms): F3= 1 + (12 /(2 + (32 / (2 +(52 /2) ) ) ) )

Partial sum # 4 (first four terms): F4= 1 + (12 /(2 + (32 / (2 +(52 /(2 + (72 / 2 ) ) ) ) ) ) )

Etc.

Note: Small suggestion to calculate the continued fraction, start with the last term, then continue with the penultimate term, and so on.

C) The third formula is still expressed as a continued fraction, with a slow convergence

 

(Pi/2) = 1 + (2 / (3+( (1*3)/ (4+ ((3*5)/ 4 +((5*7)/ 4+ ((7*9)/ )

 

Each partial sum Fk of this series can be expressed in terms of the first k terms, as follows:

Partial sum # 1 (first term): F1= 1 + (2 /3)

Partial sum # 2 (first two terms): F2= 1 + (2 /(3+ ((1*3)/4 )))

Partial sum # 3 (first three terms): F3= 1 + (2 /(3+ ((1*3)/(4+((3*5)/4 ) ) ) ) )

Partial sum # 4 (first four terms): F4= 1 + (2 /(3+ ((1*3)/(4+((3*5)/(4 +((5*7)/4) ) ) ) ) ) )

Etc.

Note: Small suggestion to calculate the continued fraction, start with the last term, then continue with the penultimate term, and so on.

D) The last formula, deduced from the arctan function and having a much faster convergence, was also discovered by Leonhard Euler:

pi =   \(\sum_{k=0}^{\infty} (2^{k} (k!)^{2}) / ((2k+1)! ) \)= 2 * (1+(1/3)+((1*2)/(3*5)) + ((1*2*3)/(3*5*7)) + ...)

Each partial sum Ek of this series can be expressed in terms of the first k terms, as follows:

Partial sum # 1 (first term): E0= 2

Partial sum # 2 (first two terms): E1= 2 * (1+(1/3))

Partial sum # 3 (first three terms): E2= 2 * (1+(1/3)+((1*2)/(3*5)))

Partial sum # 4 (first four terms): E3= 2 * (1+(1/3)+((1*2)/(3*5)) + ((1*2*3)/(3*5*7)))

Etc.

Here are some partial results expected by your program outputs:

Convergence of the PI calculation with the first formula of Euler

Value with         1 terms = 2.4494897, error = 0.6921029

Value with         3 terms = 2.8577380, error = 0.2838546

Value with         7 terms = 3.0117739, error = 0.1298187

Value with         15 terms = 3.0793898, error = 0.0622028

Value with         23 terms = 3.1006973, error = 0.0408954

Value with         39 terms = 3.1173248, error = 0.0242679

etc.

 

Convergence of the PI calculation with the formula of Brouncker

Value with         1 terms = 2.6666667, error = 0.4749260

Value with         3 terms = 2.8952381, error = 0.2463546

Value with         7 terms = 3.0170718, error = 0.1245208

Value with         15 terms = 3.0791534, error = 0.0624393

Value with         23 terms = 3.0999440, error = 0.0416486

Value with         39 terms = 3.1165966, error = 0.0249961

etc.

 

Convergence of the PI calculation with the formula as continued fraction

Value with         1 terms = 3.33333333333333, error = 0.19174067974354

Value with         3 terms = 3.18095238095238, error = 0.03935972736259

Value with         5 terms = 3.15786435786436, error = 0.01627170427457

Value with         9 terms = 3.14710277682414, error = 0.00551012323435

Value with        13 terms = 3.14432869185326, error = 0.00273603826347

Value with        21 terms = 3.14267315437062, error = 0.00108050078083

etc.

 

Convergence of the PI calculation with the second formula of Euler

Value with        1 terms = 2.00000000000000, error = 1.14159265358979

Value with        3 terms = 2.93333333333333, error = 0.20825932025646

Value with        7 terms = 3.13215673215673, error = 0.00943592143306

Value with        11 terms = 3.14110602160138, error = 0.00048663198841

Value with        15 terms = 3.14156615934495, error = 0.00002649424484

Value with        19 terms = 3.14159116699150, error = 0.00000148659829

etc.

 

 

You will find that the values calculated by the program using a number of terms that are not consecutive ( for example, the first Euler's formula , one term , then 3 terms, and 7 terms used , etc. . ) . This phenomenon is explained by the irregularity algorithm used to determine the number of terms to compute. We start by calculating one term and is defined increments ( an integer variable ) set to 1 (as if you wanted to use two terms to the second iteration, then 3 terms in the third iteration, etc.). Calculating the absolute error of the current iteration and compared with the absolute error of the previous iteration : If this comparison reveals no significant new figure appeared, you double the increment . This iterative process continues until you get the desired number of significant digits .

For example, if the initial results of the first analysis Euler formula , we find that the second value uses three terms : indeed , the first value was not able to give a single significant digit of the number pi, we doubled the increment that increases from 1 to 2, and so the next iteration uses three terms (1 + increment value ) . According to the same principle as the second value has not brought a single new significant figure, we also doubled the increment which now goes from 2 to 4, and therefore the next iteration request under 7 (3 + value increment ) . And so on.

How does one decide whether a value provides a significant new number? Simply by comparing the integral part of the base 10 logarithm of the absolute error of the current iteration with the integer portion of the base 10 logarithm of the absolute error from the previous iteration . If these two quantities are identical, double- increment. Obviously, in the first iteration, there is no absolute error of the previous iteration , which is why we should initialize this amount to the value pi (which is quite logical since it is somewhat the maximum error we can have ) .

 

Note: To calculate the logarithm to the base 10, use the log10 function in the C library to get the integer part , you simply make an explicit conversion like this: (int) log10 ( yourvariable ) .

Now how do we decide the number of significant figures to calculate ? Simply by comparing the absolute error from the previous iteration with a precision chosen initially . Two cases are distinguished:

- For the first two formulas ( Euler and Bouncker ) which converge very slowly , a first precision 1e-7 should be used ( # define EPSILON1 1e- 7 )

- For the last two formulas , an accuracy of 1e- 14 is used ( # define EPSILON2 1e- 14)

Note: For the release of the first two options , use the conversion specification % 9d to print the number of terms used and the conversion specification % .7lf to print the value and error. For the release of the last two formulas , use the conversion specification % 7d to print the number of terms used and the conversion specification % .14lf to print the value and error.

Note : Use the following value for the number pi : # define PI 3.14159265358979

Note: Use the double type for your calculations because you need a fairly high accuracy.

In summary, your main function main() will consist of four almost identical loops to produce the results according to different formulas ( precision used, the function call to use and printing formats change, but the method is the same for all the loops ) . Each function must not call other 

 

 

Reference no: EM13163420

Questions Cloud

State the atmospheric pressure in the room : The atmospheric pressure in the room when the measurement was made was 628 mmHg and the temperature was 36.0 degrees C. What is the density of the gas?
Floating point values into an array : Write a program that allows a user to enter 10 floating point values into an array. Then, in a loop, the program should prompt the user for a desired precision and subsequently display each value to the correct precision.
Write a recursive and iterative versions of binary search : in C++ write a recursive and iterative versions of binary search and compare their run times using the array a[i]=i, i=0,..., n-1
Class named realestate that has data members : Create a class named RealEstate that has data members to hold the price of a house, the number of bedrooms, and the number of baths. Member functions include overloaded insertion and extraction operations
Calculates different formulas that converge to the number pi : Write a C program (Care should be taken not to use extensions of C + + language in your work ) that calculates different formulas that converge to the number  pi,  to compare the speed of convergence of these formulas. Each form will be calculated by..
What is the root-mean-square molecular speed of a gas : what is the root-mean-square molecular speed of a gas molecule of this smelly gas molecule at 25C?
What are the subscripts in the empirical formula : What are the subscripts in the empirical formula of this compound?
What is the residual income of the new project : A new project being considered would require an investment of $15,000 and would generate annual net operating income of $3,000. What is the residual income of the new project?
What are the perceived benefits vs costs : When is an ABC approach most advantageous and what are the perceived benefits vs. costs of an ABC system (you are not expected to quantify the costs benefits)

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Prepare a businesspartner

Prepare a BusinessPartner class that contains a company name, first name and a telephone number.

  Write a program using vectors and iterators

Create a program that uses at least two functions that will be called from your main. This program is a number game program that asks for parts of your phone and after manipulating it mathematically, eventually outputs your entire phone number. The d..

  Implement circular linked list in c program

implement circular linked list in c program in the best possible easiest way.

  Develop a program that when you pipe in a file

develop a program that when you pipe in a file it will demonstrate on the left had side the line number  work by having a program piped in and when a \n occurs a new line and number is indicated.

  C++ class that has a dynamic array of a string

show  an example of a C++ Class that has a dynamic array of a string that takes in names by user input??

  Create class has three pieces of information as data members

Create a class called Date in C++ that includes three pieces of information as data members: month (type int), day (type int) and yaer (type int).

  The rpn calculator program

The RPN calculator program should read the RPN expression as an entire line from stdin.Input will consist of a single line. After completing the evaluation of the expression, the program should print the contents of the entire stack, starting with th..

  Write main function to compute stress-strain in steel rod

Write a main function and the following functions to compute the stress and strain in a steel rod of diameter D (inches) and length L (inches) subject to the compression loads.

  Write c++ programs

Write a C++ program to accept distance in kilometers, coverts it to meters and then displays the result. Write a C++ program to find area and circumference of a circle.

  Create the appropriate constructor, getters and setters

Create the appropriate constructor, getters and setters for the class. Create an instance of Student for each of the students listed above from array. Construct the instance with lastname, firstname, and job.

  Design a simple game of blackjack

Prepare a simple game of blackjack using object oriented programming.

  Write a loop that fills a vector

Write a loop that fills a vector V with ten  different  random numbers between 1 and 100. output must actually display the random numbers to get credit.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd