\$35.00

Category:

Description

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 − 4 + 4 − 4 + 4 − 4 + 4 − 4 … 1 3 5 7 9 11 13 15

• Nilakantha series is a somewhat more complicated series to calculate the value of . Here is the formula to apply:

 = 3 + 4 − 4 + 4 − 4 + 4 … 2 ∗ 3 ∗ 4 4 ∗ 5 ∗ 6 6 ∗ 7 ∗ 8 8 ∗ 9 ∗ 10 10 ∗ 11 ∗ 12

1

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 − 4 + 4 − 4 = 2.8952 1 3 5 7

• Nilakantha series (where 3 in the following equation is an offset, but not the first term)

 = 3 + 4 − 4 + 4 − 4 = 3.1397 2 ∗ 3 ∗ 4 4 ∗ 5 ∗ 6 6 ∗ 7 ∗ 8 8 ∗ 9 ∗ 10

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 − 4 + 4 − 4 = 2.8952 … 1 3 5 7

nd 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 − 4 = 3.1333 … 2 ∗ 3 ∗ 4 4 ∗ 5 ∗ 6 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.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

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