basic monte carlo question: area of circle

14 visualizzazioni (ultimi 30 giorni)
Eric
Eric il 1 Mag 2015
Modificato: hosein bashi il 30 Lug 2018
Question: Use monte carlo method to find area of circle radius 5 inside 7x7 square.
So this is the code I've put together so far to determine how many points land inside or on the circle (hits). My output for hits is 0 but I can't for the life of me figure out why, to me everything seems fine but clearly isn't.
clear;
N= 1000; % number of points generated
a = -7;
b = 7;
hits= 0;
for k = 1:N
x = a + (b-a).*rand(N,1);
y = a + (b-a).*rand(N,1);
% Count it if it is in/on the circle
radii = sqrt(x.^2+y.^2);
if radii <=5;
hits = hits + 1;
end
end
disp(hits)

Risposta accettata

pfb
pfb il 1 Mag 2015
Modificato: pfb il 1 Mag 2015
You are mixing things up. Your code is part vectorized, part scalar.
You have a loop over N, but then at each iteration you generate N points (so you're generating N^2 points overall). The variables x, y, and radii are vectors.
Note that
[6 7 4 2 8 5] <=5
gives
[0 0 1 1 0 1]
Therefore condition radii<=5 has a very low probability to be met ( all of the N radii should be less than 5).
As a result hits is (almost) never accumulated.
Your code is structured for a scalar radius. It would work if you substitute the lines
x = a + (b-a).*rand(N,1);
y = a + (b-a).*rand(N,1);
with
x = a + (b-a).*rand;
y = a + (b-a).*rand;
In this case hits will be a number between 0 and N, and hits/N is going to be proportional to the ratio of the areas of the circle and square, pi*(5/14)^2 (note that you can use simple power operator ^ instead of elementwise .^)
You can avoid the loop altogether using the vectorized variables.
N= 1000; % number of points generated
a = -7;
b = 7;
x = a + (b-a).*rand(N,1);
y = a + (b-a).*rand(N,1);
radii = sqrt(x.^2+y.^2);
hits = sum(radii<=5);

Più risposte (3)

Eric
Eric il 2 Mag 2015
Thanks both really helped a bunch.
How would I go about plotting the graph so that the hits are one colour and the rest are another?
I tried the following but I just get a linear line.
x = linspace(-7,7);
y = linspace(-7,7);
figure(1);
plot(x,y);
xlabel('x');
ylabel('y');
axis square
grid on;
title('Area of Circle');
  5 Commenti
Eric
Eric il 2 Mag 2015
Sorry I should have pointed out, I used loops because we unfortunately aren't allowed to use the sum function as we haven't been taught it yet.
Ive been trying to use a loops to add up the hits.
pfb
pfb il 2 Mag 2015
Modificato: pfb il 2 Mag 2015
ok, then you can write the function sum with a loop. After creating the vector i (which you need for the plotting)
hits = 0;
for j = 1:N
hits=hits+i(j);
end
misses= N-hits;
If you're not allowed to use logical indexing in the plot you can pretend you do not know it and do this
% vectors for the hits
xh = zeros(1,hits);
yh = xh;
% vector for the misses
xm = zeros(1,misses);
ym = xm;
% counters
mc=1;
hc=1;
for j=1:N
if(i(j))
% put the point in the hits vector
xh(hc)=x(j);
yh(hc)=y(j);
% increment the hit counter
hc=hc+1;
else
% put the point in the misses vector
xm(mc)=x(j);
ym(mc)=y(j);
% increment the miss counter
mc=mc+1;
end
end
plot(xh,yh,'g.');
hold
plot(xm,ym,'r.');
etcetera

Accedi per commentare.


Eric
Eric il 2 Mag 2015
Thanks a lot, appreciate it.
  1 Commento
pfb
pfb il 2 Mag 2015
you're welcome. Note that there was an error in my code above (in one instance I wrote hits instead of misses). Now it should be ok.

Accedi per commentare.


munesh pal
munesh pal il 27 Feb 2018
Calculate the area of the circle using Monte Carlo Simulation
clc
clear all
close all
%%input circle radius and coordinate
r=2;
c_x=7;
c_y=7;
%%position of the outer rectangle
p_x=c_x-r;
p_y=c_y-r;
pos=[p_x,p_y,r^2,r^2]
%%random number generation within the sqaure
N=1000;
a=p_x;
b=p_x+(2*r);
x = a + (b-a).*rand(N,1);
y = a + (b-a).*rand(N,1);
radii = sqrt((x-c_x).^2+(y-c_y).^2);
i = radii <= r;
hits = sum(i);
misses = N-hits;
plot(x(i),y(i),'.g');
hold;
plot(x(~i),y(~i),'.r');
rectangle('Position',pos,'Curvature',[1 1])
rectangle('Position',pos,'EdgeColor','r')
actual_a=22/7*r^2;
ttl = sprintf('Estimate Actual Area of circle: %1.3f, Area of the circle: %1.3f',actual_a,hits/N*(2*r)^2);
title(ttl);
axis equal
  1 Commento
hosein bashi
hosein bashi il 30 Lug 2018
Modificato: hosein bashi il 30 Lug 2018
can you explain please why we use random numbers? why we don't assume a grid and put our numbers in the middle of the cells? it would be more uniform in this way.

Accedi per commentare.

Categorie

Scopri di più su Just for fun in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by