Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this project, you will approximate Pi using several methods described in Chapter 2 of the textbook. You must submit your work by 1 1

In this project, you will approximate Pi using several methods described in Chapter 2 of the
textbook.
You must submit your work by 11:59 pm on the due date there will be no extensions.
By completing this project, you will gain
practice implementing three algorithms for approximating (Archimedes, Wallis, and
Monte Carlo)
practice following API requirements (function signatures)
experience using turtle graphics to visualize the Monte Carlo solution method
practice importing modules and functions from Python Standard Library (math, random)
and your code (different parts of this project)
some knowledge of deterministic vs. non-deterministic algorithms
practice Python functions with different return types, side effects, bool, selection
statements, loops, and strings
When writing Python code, follow the PEP8. Use whitespace between operators and
operands, use descriptive variable names, and add appropriate in-code comments (why, not
what). Do diligent parameter checking, e.g., do not assume that your function will receive a
positive int argument because you expect a positive int.
How to succeed:
Start early
Go to office hours
Ask questions on Discord or email.
All problems in this project are based on PPC Ch.2. Make sure
You may have other helper methods called from the required functions
Include docstring documentation for all the required functions
2. Instructions
2.1.[30 pts] The Archimedes Method
Requirements: Implement the Archimedes method (Sec.2.4 in PPC) using the following
function signature. In your function, return the computed approximation of (do not change
the name or type of argument and return value). Feel free to use the book version or come up
with your own.
def pi_arch(num_sides: int)-> float
Function signatures are informative; they contribute to documenting your code. Signatures
describe the input parameters' data types and the return value. Signatures do not perform any
data type validation.
What to submit: Upload arch.py on Coding Rooms.
2.2.[30 pts] The Wallis Method
Requirements: Implement the Wallis approximation method (Sec.2.5.3 in PPC) using the
following function signature. In your function, return the computed approximation of (do not
change the name or type of argument and return value). Feel free to use the book version or
come up with your own.
def pi_wallis(num_pairs: int)-> float
What to submit: Upload wallis.py on Coding Rooms.
2.3.[30 pts] Monte Carlo Simulation
Requirements: Implement the Monte Carlo approximation of (Sec 2.6 in PPC) using the
following function signature. In your function, return the computed approximation of (do not
change the name or type of argument and return value). Feel free to use the book version or
come up with your own.
Since you use pseudo-random numbers, you may get different answers each time you call the
function, so initialize random with a fixed seed (see example in Part 5). Use 42 as the random
seed in all experiments so all our results match (read https://tinyurl.com/UQLUE42).
def pi_mc(num_darts: int)-> float
What to submit: Upload mc.py on Coding Rooms.
2.4.[60 pts] Putting All Together
While the textbook provides solutions and simple implementations to the previous problems, in
this part, you get to design a method for evaluating the different approaches implemented in
Parts 1-3.
Your task is to implement a function that computes and prints the required input parameters
for each algorithm, given an error tolerance err_tol (a floating-point value between 0 and 1).
E.g., if err_tol is 0.001, how many sides does the Archimedes algorithm need to produce an
error of 0.001 or smaller?
Recall that an accurate value of is available through the math.pi constant, but remember that
the smaller the value for err_tol, the longer each method will take. Feel free to implement
any helper functions you need.
Requirements: Function signature input one float number and return a list of three values.
Ensure you import the Python modules you created for Parts 1,2, and 3, e.g., import arch.
def all_pi(err_tol: float)-> list:
Example. These are the results as printed on a proposed solution to this problem.
>>> print(all_pi(0.1))
Archimedes: num_sides =8(Differs by 0.08012519466907486)
Wallis: num_pairs =8(Differs by 0.0910026575342835)
Monte Carlo: num_darts =5(Differs by 0.05840734641020706)
[8,8,5]
When you run your code repeatedly with the same err_tol input, you get the same values for
Archimedes and Wallis (i.e., those functions are deterministic). Still, you may obtain different
answers for Monte Carlo (a non-deterministic function). The Monte Carlo produces different
results because it uses pseudo-random numbers for the darts' positions and gets different sets of
points with every run.
To ensure you get the same result every time you run, you can initialize the pseudo-random
number generator with a fixed seed first (use 42 as your seed) before calling your mc.pi_mc
function, e.g.,

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

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

10th Edition

0137916787, 978-0137916788

More Books

Students also viewed these Databases questions

Question

=+b) Create a time series plot of the ratio and describe the trend.

Answered: 1 week ago

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago