ESTIMATING PI – with Python

(c) 2021 George Gadanidis

1. TASK
Get the Task as a PDF: https://learnx.ca/wp-content/uploads/2021/04/estimating-pi-python-task.pdf

 Estimate the value of Pi using the number of random (x, y) points (or “darts”) that fall in the biggest circle that fits in a 200 x 200 square in the centre of the coordinate system.

Start by coding these components:

  1. Generate 1000 (x, y) points with x and y being random numbers from -100 to 100.
  2. Count the number of points that fall in the circle and the number of points that fall outside of the circle.
  3. For each point calculate use this information to estimate the value of Pi
  4. Plot all of the values of Pi, as shown below

Create a poster presentation of what you did, what you learned, and what else you’d like to know.


2. SCAFFOLDING
NOTE: A possible solution: https://colab.research.google.com/drive/1LlDFv6YYknVZTnpX6alsG1UMbaIb6dXN#scrollTo=uyaOjPbZmxuq

How can we write code to dynamically model the square-circle relationship and use the relationship to estimate Pi?

1.  In search of Pi

Before we start coding, we need to understand the math relationship that gives us Pi.  Here is one way:

We have a pink circle, with radius r, that just fits inside a blue square. That is, the pink circle is the biggest circle that can fit inside the square.

A. What is the area of the circle? ___________

B. What is the area of the square? ___________

C. Complete the fraction below, and simply expressions as much as possible:

2.  Connecting the dots

Discuss in your team:

Suppose we have these 3 variables we can use to count the number of pink dots and the number of blue dots, and then estimate the value of Pi:

  1. pinkCounter
  2. blueCounter
  3. piEstimate

A.  How may we use these variables, and the result from 1.C, to estimate the value of Pi?

B.  Write an algebraic expression using these variables to estimate the value of Pi.

 1.  100 random (x, y) points

The code on the right plots 100 random (x, y) points, with x & y from -100 to 100. 

  • Enter and run the code
  • Change the value of darts to 10
  • What is the effect?
  • For the code below, change 200 to 20 and 100 to 10
  • What is the effect?
  • For the code below, change ‘b.’ to ‘ro’

  • What is the effect?

2.  Which points are outside the circle? 

We have a 200 x 200 pixel square, with (0,0) in its centre.

What is the radius of the largest circle the fits in the square?

If a point is on the circumference of the circle then, using the Pythagorean Theorem, we know the x2 + y2 = 1002.

What does the Python code below do? 

Use the variables dartsInCircle and dartsInSquare to write an expression that calculates an estimate of Pi. See the Mindset section for help.