Processing math: 100%

CPSC150A
Scientific Computing

Activity 16

Conditionals

Min 2

Write a function called min_2(number_1: float, number_2: float) -> float that returns the minimum of the two parameters, number_1 and number_2. The function should not use the built-in min function.

Test Cases

import test

def min_2(number_1: float, number_2: float) -> float:
    # Put your code here

def main() -> None:
    test.equal(min_2(?, ?), ?)
    # Put more test cases here
    return None

main()

Min 3

Write a function called min_3(number_1: float, number_2: float, number_3: float) -> float that returns the minimum of the 3 parameters to the function, number_1, number_2, and number_3. The function should not use the built-in min function.

Test Cases

import test

def min_3(number_1: float, number_2: float, number_3: float) -> float:
    # Put your code here

def main() -> None:
    test.equal(min_3(?, ?, ?), ?)
    # Put more test cases here
    return None

main()

Point in Circle

Earlier this semester we estimated π using the Leibniz approximation. Another way of estimating the value of pi is to use Monte Carlo Estimation. The technique is named after the casino because it depends on random chance and probability. A program that estimates π this way must test whether a point is inside of a circle.

Details

Create a Python program that uses the turtle to draw a large circle centered in the window. The program should generate random points in the window and draw them as a small x. The color of the x should be determined by whether or not the point is inside the large circle. If the point is inside the circle, the x should be green, if the point is outside the circle, the x should be red. Assume a point exactly on the edge of the large circle is inside the circle.

Example

  • Reusing functions makes programs easier to wrtie! You created a function in a previous activity to draw an x, copy that function to this program.
  • A point is inside of a circle if the distance between the point and the center of the circle is less than the radius of the circle.
  • Finding the distance between two points is another opportunity to reuse functions. You probably wrote a function to compute the distance between two points in a previous assignment. Copy and paste that function into this program.

Challenge

To perform a Monte Carlo estimation of π, we need to estimate the area of the circle. Create a program that draws a large circle circumscribed by a square in the center of the window. That is, the width and height of the square is the same as the diameter of the circle. The program should generate a large number of random points. It should be possible for a random point to be located anywhere inside the square but nowhere outside of the square. The fraction of points that are inside the circle is an estimate of the area of the circle as a fraction of the area of the square. Since the area of a circle is πr2, the program can use the estimated area to solve for π