Exercise 11.5

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:

\[ A_\circ = \pi r^2 \]

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\)):

\[ \frac{N_\circ}{N} \approx \frac{A_\circ}{A_\square} \]

This can be re-arranged to find a formula for approximating \(\pi\):

\[ \pi \approx \frac{A_\square}{r^2} \frac{N_\circ}{N} \]

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:

\[\begin{split} f(x) = \begin{cases} \sin(x), & 0 < x \leq \pi\\ \sin^2(x), & \pi < x \leq 2 \pi\\ \sin^3(x), & 2\pi < x \leq 3 \pi\\ \vdots &\\ \sin^n(x), & (n-1)\pi < x \leq n \pi \end{cases} \end{split}\]

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\).