Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask C/C++ Expert


Home >> C/C++

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 

 

 

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M9453540

Have any Question?


Related Questions in C/C++

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As