Question

In: Advanced Math

in Matlab, Use the Monte Carlo analysis to compute the area of a circle with radius...

in Matlab, Use the Monte Carlo analysis to compute the area of a circle with radius 1. print out your code, at least one figure on which the circle and ‘dart hits’ are shown, and numerical results for N=10, 100,1000. For each N, repeat the calculation at least 5times.

Solutions

Expert Solution

%%Matlab code for Monte Carlo simulation for finding area of circle
clear all
close all
%Radius of the circle
r=1;
% number of points generated
N = 10;
a = -r;
b = r;
x = a + (b-a).*rand(N,1);
y = a + (b-a).*rand(N,1);
radii = sqrt(x.^2+y.^2);
hits = sum(radii<=1);
th=linspace(0,2*pi);
x_r=cos(th);
y_r=sin(th);

figure(1)
plot(x_r,y_r,'Linewidth',2)
%Plotting data
k=0;
hold on
for i=1:N
   
     radd=sqrt((x(i)).^2+(y(i)).^2);
     if radd<=1
         k=k+1;
         xx(k)=x(i);
         yy(k)=y(i);
         plot(x(i),y(i),'r*')
     else
         plot(x(i),y(i),'b*')
     end
   
end
xlim([-1 1])
ylim([-1 1])
title(sprintf('Monte Carlo plot for N=%d',N))
xlabel('x')
ylabel('y')
ar=4*hits/N;
fprintf('\n\tFor N=%d area of Circle using Monte Carlo method is %f.\n',N,ar)

            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          
clear all

%Radius of the circle
r=1;
% number of points generated
N = 100;
a = -r;
b = r;
x = a + (b-a).*rand(N,1);
y = a + (b-a).*rand(N,1);
radii = sqrt(x.^2+y.^2);
hits = sum(radii<=1);
th=linspace(0,2*pi);
x_r=cos(th);
y_r=sin(th);

figure(2)
plot(x_r,y_r,'Linewidth',2)
%Plotting data
k=0;
hold on
for i=1:N
   
     radd=sqrt((x(i)).^2+(y(i)).^2);
     if radd<=1
         k=k+1;
         xx(k)=x(i);
         yy(k)=y(i);
         plot(x(i),y(i),'r*')
     else
         plot(x(i),y(i),'b*')
     end
   
end
xlim([-1 1])
ylim([-1 1])
title(sprintf('Monte Carlo plot for N=%d',N))
xlabel('x')
ylabel('y')
ar=4*hits/N;
fprintf('\n\tFor N=%d area of Circle using Monte Carlo method is %f.\n',N,ar)

            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
          
clear all
%Radius of the circle
r=1;
% number of points generated
N = 1000;
a = -r;
b = r;
x = a + (b-a).*rand(N,1);
y = a + (b-a).*rand(N,1);
radii = sqrt(x.^2+y.^2);
hits = sum(radii<=1);
th=linspace(0,2*pi);
x_r=cos(th);
y_r=sin(th);

figure(3)
plot(x_r,y_r,'Linewidth',2)
%Plotting data
k=0;
hold on
for i=1:N
   
     radd=sqrt((x(i)).^2+(y(i)).^2);
     if radd<=1
         k=k+1;
         xx(k)=x(i);
         yy(k)=y(i);
         plot(x(i),y(i),'r*')
     else
         plot(x(i),y(i),'b*')
     end
   
end
xlim([-1 1])
ylim([-1 1])
title(sprintf('Monte Carlo plot for N=%d',N))
xlabel('x')
ylabel('y')
ar=4*hits/N;
fprintf('\n\tFor N=%d area of Circle using Monte Carlo method is %f.\n',N,ar)

            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


Related Solutions

Under what conditions is it necessary to use Monte Carlo analysis in the study of a...
Under what conditions is it necessary to use Monte Carlo analysis in the study of a queuing system?
A Monte Carlo simulation is a method for finding a value that is difficult to compute...
A Monte Carlo simulation is a method for finding a value that is difficult to compute by performing many random experiments. For example, suppose we wanted to estimate π to within a certain accuracy. We could do so by randomly (and independently) sampling n points from the unit square and counting how many of them are inside the unit circle (assuming that the probability of selecting a point in a given region is proportional to the area of the region)....
How and when do you use the Monte Carlo analysis? Does it have anything to do...
How and when do you use the Monte Carlo analysis? Does it have anything to do with understanding the probability of a risk occurring as well?
Write a matlab program that determines the value of pi using the monte carlo technique. do...
Write a matlab program that determines the value of pi using the monte carlo technique. do this for a loop of multiple fixed points. (i.e 100-10000) Plot the computed value of pi and the difference from the true value as this number increases. Time the execution of your code for various numbers of points, and plot the precision vs the computational cost.
using matlab can you show me an example of a monte carlo that has two things...
using matlab can you show me an example of a monte carlo that has two things randomizing?
Make a program in C++ and write explanations. Monte-Carlo methods Calculating pi with Monte-Carlo method is...
Make a program in C++ and write explanations. Monte-Carlo methods Calculating pi with Monte-Carlo method is NOT allowed.
A circle of radius r has area A = πr2. If a random circle has a...
A circle of radius r has area A = πr2. If a random circle has a radius that is uniformly distributed on the interval (0, 1), what are the mean and variance of the area of the circle? Change the distribution of the radius to an exponential distribution with paramter β = 2. Also find the probability that the area of the circle exceeds 3, if it is known that the area exceeds 2.
explain the 5 steps of conducting a Monte Carlo simulation? Thank You Quantitative Analysis
explain the 5 steps of conducting a Monte Carlo simulation? Thank You Quantitative Analysis
problem is also a Monte Carlo simulation, but this time in the continuous domain: must use...
problem is also a Monte Carlo simulation, but this time in the continuous domain: must use the following fact: a circle inscribed in a unit square has as radius of 0.5 and an area of ?∗(0.52)=?4.π∗(0.52)=π4. Therefore, if you generate num_trials random points in the unit square, and count how many land inside the circle, you can calculate an approximation of ? For this problem, you must create code in python (A) Draw the diagram of the unit square with...
1) A circle of radius r has area A = π r2. If a random circle...
1) A circle of radius r has area A = π r2. If a random circle has a radius that is evenly distributed in the interval (0, 1), what are the mean and variance of the area of ​​the circle? choose the correct answer A) 1/3 and 1/12 B) Pi/3 and 1/12 C) Pi/3 and 1/5 D) Pi/3 and (4/45)*Pi^2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT