The MonteCarlo method tries to estimate a numerical quantity

The Monte-Carlo method tries to estimate a numerical quantity by using random sampling. We will try to use this method to approximately find certain areas. Imagine a disk of radius r with center at (a, b) where 0 lessthanorequalto a, b lessthanorequalto 1. Consider the intersection of this disk with the unit square [0, 1| times [0, 1]. We want to find the area of this intersection. The Monte carlo approach would do the following: Generate many random points inside the unit square. Find which of these also lie within the disk. The fraction of points in the disk is the area of the intersection (approximately) since the area of the unit square is 1. Write a program to do this. Your program should take as input three floating point numbers a, r and its output will be the approximate area of the intersection of the unit square with the disk (x - a)^2 + (y - b)^2 lessthanorequalto r^2. Experiment with how many random points you should choose to make the area estimate quite accurate. We will assume that r greaterthanorequalto 0.5 - this will guarantee that the intersection area is not too small.

Solution

function res = monteCarlo(a,b,r)
   %reading number of points
   n = input(\'Number of points: \')
   figure(\'color\',\'white\')
   hold all
  
   axis square
   x = a-1;
   y = b-1;
  
   %calculating number of points inside square
   res = 0;
   for i=1:n
       if r(i)<=0.25
           res=res+1;
           plot(x(i),y(i),\'b.\');
       else

           plot(x(i),y(i),\'r.\');
       end
   end
   res/(0.25*n)
end

%calling function
monteCarlo(5,5,2);

 The Monte-Carlo method tries to estimate a numerical quantity by using random sampling. We will try to use this method to approximately find certain areas. Ima

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site