Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON - please help with PART II in this LAB Background Part of this lab requires a basic understanding of infinite series. An infinite series

PYTHON - please help with PART II in this LAB

image text in transcribed

image text in transcribed

image text in transcribedimage text in transcribed

Background Part of this lab requires a basic understanding of infinite series. An infinite series is a mathematical formula that can never be fully calculated, but as more and more iterations are performed the result converges on a value. In this lab we are going to be using two well-known infinite series to estimate the value of pi (T), which is roughly 3.14159265. Leibniz Series The first series we will look at is the Leibniz series (named after Gottfried Leibniz) which has alternating additions and subtractions of fractions to approximate pi. 4 4 4 4 4 4 4 3 5 7 :+ 13 9 + 11 It should be relatively simple to guess the next few terms. The numerator is always 4 and the denominator is the next odd number in the series. The terms alternate between being added to the result or subtracted. This is one of the simplest series to implement, but it takes many millions of iterations before the value starts looking like pi. After 10,000,000 iterations, the value is about: 3.1415925535891397 (the red being where the value diverges from pi) Nilakantha Series Like the Leibniz series, the Nilakantha series (named after Kelallur Nilakantha Somayaji) also calculates pi by using a repeating pattern of adding and subtracting fractions where the numerator is also always 4. 4 11 = 3+ 2x3 x4 4x5 x6*6x7x8 8x9 x 10 * 10 x 11 x 12 This series is also easy to calculate but has the advantage of converging on pi much quicker. After 10,000 iterations, the value is: 3.14159265358956 (again, red indicates where it diverges). As you can see it gets closer to pi in FAR less time than Leibniz. Part I To make this project easier, you can utilize the Fraction module which will have a much higher resolution then using floating point types. So, use the following import command: from Fraction import * Additionally, we will use the Decimal data type which has more precision than floats: from decimal import * Next, we will need some module-level variables for calculations: Pi out to 50 digits: pi50 = Decimal("3.14159265358979323846264338327950288419716939937510") Number of iterations to perform: iterations = 1000000 Create an iterator class to calculate the value of pi using the Leibniz series: class LeibnizPiIterator: _init__() signature: def _init__(self): For this method, simply use a pass command. All of the setup will occur in __iter__ Liter_() signature: def _iter_(self): This method initializes the values we will need for the iterator o Create an instance variable called self.fraction and assign to it a Fraction object with a numerator of O and denominator of 1. This will represent the running total for the series. o Create an instance variable called self.n and assign 1 to it. This will represent the denominator to be used in the next iteration (see documentation above). o Create an instance variable called self.add_next and assign to it True. This is a Boolean value indicating if the next iteration will be an add or subtract. Return self _next_() signature: def __next__(self): o This method is where the work is done for each iteration. o If self.add_next is True then the next value is to be added to self.fraction. Otherwise, subtract it. The next value is a new Fraction object with the value 4 / self.n o After updating self.fraction do the following: Change the value of self.add_next to its opposite. Add 2 to self.n Return self.fraction.value . . Test the Iterator: Set iterations to 100,000 (don't use the thousands separator) Create a new LeibnizPiIterator object and loop through it "iterations" times. 0 You can implement using a for loop or while loop. Report the result of the final value to the terminal Then calculate the difference between the final value from the iterator and pi50 and report that value to the terminal as well. Change the iterations variable to 10,000,000 and re-run the test it will take much more time, maybe get a coffee!) Sample Output: pi after 100000 iterations: 3.14158265358985315979006807645440766789826367641700 Difference: 0.00000999999994007867257530682509500000000000000000 after 10000000 iterations: 3.14159255358913974898979182536369204750639121844740 Difference: 0.00000010000065348947285155791581080000000000000000 i 1 Part II Now you are going to create a generator that calculates pi using Nilakantha's Series. def NilakanthaPiGenerator(): In the function, create the following variables: o fraction: set it to a new Fraction object with the numerator set to 3 and the denominator to 1. This represents the current value of pi after each iteration. num: set it to 2. This represents the first factor in the denominator calculation at each iteration. o add_next: set it to True. This represents whether or not to add or subtract the next value to fraction. Create an infinite loop. Inside the loop: o Calculate the next Fraction object to add/subtraction to fraction. Store it in a variable called operand. 0 Add or subtract operand to fraction based on the value of add_next o Flip the value of add_next Add 2 to num o yield fraction.value . 0 Next, test this generator in much the same way as the iterator created in the first section. You must test it at 100,000 and 10,000,000 iterations. The output should show how much closer to pi this series gets

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

More Books

Students also viewed these Databases questions

Question

Claim: d Answered: 1 week ago

Answered: 1 week ago

Question

Draw and explain the operation of LVDT for pressure measurement

Answered: 1 week ago