Sample Code for Monte Carlo estimation of pi

In summary, the conversation discusses using the ~ symbol in code to represent "not," and how this can be used to detect logicals on vectors that are not subsets of a matrix. The code also shows how to use logical arrays to access matrix elements. The purpose of the code is to plot rejection/accept regions using Monte Carlo sampling in order to estimate pi.
  • #1
FallenApple
566
61
Below is the partial code to plot the rejection/accept regions using monte carlo sampling. The context is to estimate pi and plotting is just one part of it.

I'm not sure how the ~ symbol can work.

Code:
scatter(samples(1,~reject),samples(2,~reject),'b.')
scatter(samples(1,reject),samples(2,reject),'rx')

I get the idea that ~ means "not". But reject is a different vector. How can the samples matrix detect logicals on vectors that aren't even subsets of the matrix?

The entire code and website I got it from is below.
Code:
% DISPLAY A CIRCLE INSCRIBED IN A SQUARE

figure;
a = 0:.01:2*pi;
x = cos(a); y = sin(a);
hold on
plot(x,y,'k','Linewidth',2)

t = text(0.5, 0.05,'r');
l = line([0 1],[0 0],'Linewidth',2);
axis equal
box on
xlim([-1 1])
ylim([-1 1])
title('Unit Circle Inscribed in a Square')

pause;
rand('seed',12345)
randn('seed',12345)
delete(l); delete(t);

% DRAW SAMPLES FROM PROPOSAL DISTRIBUTION
samples = 2*rand(2,100000) - 1;

% REJECTION
reject = sum(samples.^2) > 1;

% DISPLAY REJECTION CRITERION
scatter(samples(1,~reject),samples(2,~reject),'b.')
scatter(samples(1,reject),samples(2,reject),'rx')
hold off
xlim([-1 1])
ylim([-1 1])

piHat = mean(sum(samples.*samples)<1)*4;

title(['Estimate of \pi = ',num2str(piHat)]);

https://theclevermachine.wordpress.com/2012/09/10/rejection-sampling/
 
Physics news on Phys.org
  • #3
Orodruin said:
I suggest reading this: https://se.mathworks.com/help/matlab/math/matrix-indexing.html

If it is a tl;dr - scroll down directly to using logical arrays to access matrix elements.

Ok I see. How silly of me.

reject = sum(samples.^2) > 1;
is just a vector of logicals. True or False. So it can be used for conditioning.
 

What is Monte Carlo estimation of pi?

Monte Carlo estimation of pi is a method used to approximate the value of pi by generating random points on a square and counting the number of points that fall within a quarter of a circle inscribed within the square.

How does Monte Carlo estimation of pi work?

Monte Carlo estimation of pi works by using the ratio of the number of points that fall within the quarter circle to the total number of points generated to approximate the value of pi. This is based on the fact that the area of the quarter circle is equal to pi/4 and the area of the square is equal to 1.

Why is Monte Carlo estimation of pi useful?

Monte Carlo estimation of pi is useful because it provides a simple and intuitive way to approximate the value of pi without having to use complex mathematical equations. It is also easily scalable and can be used to calculate pi to a desired level of accuracy.

What are the limitations of Monte Carlo estimation of pi?

One limitation of Monte Carlo estimation of pi is that it is a probabilistic method, meaning that the accuracy of the approximation depends on the number of points generated. Additionally, this method can be computationally intensive and may not be the most efficient way to calculate pi compared to other mathematical methods.

How can I use Monte Carlo estimation of pi in my own research or projects?

Monte Carlo estimation of pi can be used in a variety of fields such as physics, engineering, and finance to simulate and model complex systems. It can also be used in computer graphics and animation to generate random patterns or textures. Additionally, it can be a fun and educational exercise to understand the concept of pi and its applications in real-world scenarios.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
505
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
12
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • Programming and Computer Science
Replies
22
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
819
Replies
0
Views
2K
  • Advanced Physics Homework Help
Replies
11
Views
2K
Back
Top