create a program that estimates pi by simulation
2 views (last 30 days)
Show older comments
Create a program that estimates pi (𝜋) by simulation. This is accomplished by drawing random points in a unit square (1x1), and counting which ones fall inside a quarter of a unit circle (radius=1). The value of pi is then estimated using the ratio of areas of the quarter circle to the unit square. Calculate estimates of pi and relative error for 100, 1000, 10000, 100000 and 1000000 samples.
Answers (1)
weikang zhao
on 1 Feb 2021
This seems to be your homework and not a technical problem. Normally, I don’t do homework for others, but this question is simple enough.
N=100000;
pi_est=4*sum((unifrnd(0,1,N,1).^2+unifrnd(0,1,N,1).^2)<=1)/N;
change N to get all the estimates you want.
0 Comments
See Also
Categories
Find more on Monte-Carlo in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!