Starting from:

$19.99

Lab01: Calculating 𝝅

Lab01: Calculating 𝝅 
1. Introduction In this exercise, we will complete several pieces of Python programs that calculate the value of πœ‹ and compare their performance. 2. Objectives The purpose of this assignment is to give you experience in: β€’ Using mathematical formulas in Python. β€’ Converting mathematical expressions into Python expressions. β€’ Refreshing knowledge on function and loops in Python. β€’ Doing a little bit study on series based πœ‹ approximation. Note : Before you start, if you are not familiar with if/else statement, for loop, while loop or function in Python, you are recommended to review the sample codes we covered in lecture first.
3. Background 3.1. Calculating 𝝅 using series πœ‹ is the ratio of a circle's circumference to its diameter. From the early old days, mathematicians have been trying different methods to calculate the value of πœ‹. Calculating πœ‹ by using an infinite series is one popular category for πœ‹ approximation. In this lab assignment, we will be implementing two types of series for πœ‹ calculation, namely, the Gregory-Leibniz series and the Nilakantha series. β€’ Gregory-Leibniz series is one of the simplest series to calculate the value of πœ‹. Here is the formula to apply: πœ‹=4 1βˆ’4 3+4 5βˆ’4 7+4 9βˆ’4 11+4 13βˆ’4 15… β€’ Nilakantha series is a somewhat more complicated series to calculate the value of πœ‹. Here is the formula to apply: πœ‹=3+ 4 2βˆ—3βˆ—4βˆ’ 4 4βˆ—5βˆ—6+ 4 6βˆ—7βˆ—8βˆ’ 4 8βˆ—9βˆ—10+ 4 10βˆ—11βˆ—12…
2
These series, with every iteration, will get closer and closer to πœ‹. In other words, the more terms being considered in the series, the closer we can get πœ‹. However, the speeds at which these series approach to πœ‹ are different. Formally, we call this type of speed, rate of convergence. By including the same number of terms in these series, the approximation accuracy can be quite different. Let’s have an example of including 4 terms, where we use πœ‹ to denote the approximation for πœ‹, β€’ Gregory-Leibniz series πœ‹=4 1βˆ’4 3+4 5βˆ’4 7=2.8952 β€’ Nilakantha series (where 3 in the following equation is an offset, but not the first term) πœ‹=3+ 4 2βˆ—3βˆ—4βˆ’ 4 4βˆ—5βˆ—6+ 4 6βˆ—7βˆ—8βˆ’ 4 8βˆ—9βˆ—10=3.1397 As we can see above, with both four terms, Nilakantha series provides a better estimation of πœ‹ than Gregory -Leibniz series, since 3.1397 is much closer to πœ‹ (3.1415927) than 2.8952. 4. Assignment When you are reading this lab assignment, you must have already logged in the Mimir Classroom Website, joined the CSE2050-001 course and downloaded the skeleton zip file. In the zip file, together with this lab assignment, you can find a skeleton code. All you need to do is to complete the skeleton code based on the instructions and submit it to the Mimir system. 4.1. 𝝅 calculation function Now, open the skeleton code. You can find, at the very beginning of the skeleton code, two functions that you need to complete. These functions use the Gregory Leibniz series and Nilakantha series, respectively, to calculate πœ‹ and return the result. The input parameter variable n denotes the number of terms that should be included to calculate πœ‹. β€’ For example, if we call function GregoryLeibniz as the following,
GregoryLeibniz(4) Then, the function should calculate πœ‹ with the first 4 terms of Gregory Leibniz series like πœ‹=4 1βˆ’4 3+4 5βˆ’4 7=2.8952…
3
And return value as 2.8952…
β€’ For another example, if we call function Nilakantha like,
Nilakantha(2) Then, the function should calculate πœ‹ with the first 2 terms of Nilakantha series, πœ‹=3 + 4 2βˆ—3βˆ—4 βˆ’ 4 4βˆ—5βˆ—6=3.1333… Offset 1st Term 2nd Term
and return value as 3.1333… (Please note that the offset 3 in the equation above is not a term in the series)
After implementing the two functions, we can run the python script in Python Shell. The following piece of code in the skeleton code will call the two newly implemented functions, and print the number of terms followed by the estimated values of πœ‹. n = 100 for i in range(n): print(i+1, GregoryLeibniz(i+1), Nilakantha(i+1))
In the Python Shell, you should see something show up similar to the following. If your output is different, please double check your code.
…(lines of output omitted)
4
4.2. Rate of convergence comparison The second part of our lab is to further compare the rate of convergence of the two series. We should add numbers of lines of code into the following corresponding positions.
Go back to the skeleton code. The following piece of code will compute and print the minimum number of terms required in order to approximate πœ‹ within an accuracy of 0.001 using Gregory Leibniz series (in other words, the approximated πœ‹ value should be within 3.1415927-0.001 to 3.1415927+0.001), together with its corresponding approximated value of πœ‹. The value of the minimum number of terms should be stored in variable i, and its corresponding approximated value of πœ‹ should be stored in variable result. pi = 3.1415927 accuracy = 0.001 i = 0 diff = 1 result = 0 ### add your code here print("GregoryLeibniz", i, result)
Do the same calculation to Nilakantha series and complete the last piece of code. It should print the minimum number of terms and the approximated πœ‹ value with the same accuracy with Nilakantha series. i = 0 diff = 1 ### add your code here print("Nilakantha", i, result)
In this subsection, we only need to print, in total, two lines of information. Do not modify any exiting statement or add any extra print statement

More products