In this assignment, we are going to try a few Monte Carlo experiments.
The first question asks you to pick points in a square and count what
percentage land in the circle of radius 1. In the second question we
will use this method to estimate the area under the curve without taking
an integral.
You will need to use the function rand(). It has ways that it can be
used, rand() is a random 12 digit number and rand(a..b) is
a random number generator X where the output of X()
is a random number between a and b.
For example try the following commands and ensure that you understand what they do:
> rand();
> evalf(rand()/10^13);
> rand(1..6);
> rand(1..6)();
> X:=rand(1..6);[seq(X(),i=1..10)];
> count:=0;
> for n from 1 to 1000 do
> count:=count+`if`(X()=1,1,0):
> od:
> evalf(count/1000);
Question #1:
Calculate an estimate for Pi/4 by sampling points that are chosen
uniformly in the range of [-1..1] x [-1..1] and testing if they
lie in the circle of raduis 1 or not (explain why the procedure below will
be an estimate for Pi/4.
We will need to break this problem down into several steps so you should write
a function (possibly several) for each of these.
1. A function pick_val which picks a random number between -1 and 1.
2. A function pick_pointwhich picks a random point whose x and y values are between -1 and 1.
3. A function test_in_circle which accepts a point p = [x,y] and returns true
if x^2+y^2<=1
4. A function test_points which tests n
randomly generated points from your function
in step 2 and counts how many of them lie in the circle of radius 1. Have this
function return a floating point estimate of the
fraction of points which lie in the circle and multiplied by 4.
Explain what you see for $n = 10, 100, 1000, 10000$ and $100000$.
Question #2:
Calculate an estimate for the area under the
Folium of Descartes
curve that we looked at in
Assignment #5. Recall that if we rotated this
curve by 45 degrees the equation was
$$y = x \sqrt{\frac{3 \sqrt{2} - 2 x}{6x + 3 \sqrt{2}}}$$
(well there is a plus or minus, but I just want to concentrate on the positive
part of this curve). We found that the area under the positive part of the
curve, but above the x-axis was equal to 3/4 (because the area inclosed
by the loop should have been 3/2).
Sample points randomly distributed uniformly in the bounding box
shown in the graph below ($0 \leq x \leq 2.5$ and $0 \leq y \leq 0.5$).
Estimate what percentage of those points lie
below the curve and find an estimate for the area under the curve.
You should open up a new worksheet and start from scratch. You will have to save
your work in a file and upload that file on to the course
moodle. Your
solution should be a sequence of commands where it is easy to change the input
string and after you execute the sequence of commands you should have the
correct output string. Please add documentation to your worksheet to explain how it
works. Just a few sentences is sufficient, but imagine that you were opening up the
worksheet for the first time and wanted to know what it did. You will be marked down
if what you write is not clear and coherent.
You should finish your assignment before class Tuesday November 6 by 11:59pm.
Assignments submitted after this date will be assessed a penalty of 10% per day.