Exercise 11.5#
Question 1#
Write a script that generates \(N\) random points inside a square region that contains a unit circle (radius of length 1). Plot these points using different marker styles and colors for points that are inside and outside the circle. Label these points in a legend. Take \(N\) as a user input.
Question 2#
By adapting the code to Question 1 above, you can estimate the value of \(\pi\). Consider the equation for the area of a circle:
For the points generated in the square region, we’d expect the ratio between the number of points inside the circle (\(N_\circ\)) and the total number of points (\(N\)) to be approximately equal to the ratio of the area of the circle and the area of the square (\(A_\square\)):
This can be re-arranged to find a formula for approximating \(\pi\):
Use this to approximate the value of \(\pi\), again taking \(N\) as a user input and printing out the result.
Question 3#
Consider the piecewise defined function:
for some integer \(n > 0\).
Write a program that takes a user input for \(n\) and plots the function for \(x\) in the range \([0, n\pi]\). Make use of a single array for \(x\) and \(y\) and a single call to the plot function. Use a constant number of points in the plots, rather than scaling it based on \(n\).