MATLAB: I'm having trouble calculating the probability to estimate pi. Any help is appreciated.
Show older comments
The MATLAB command rand produces a uniformly-distributed random number between 0 and 1. For example, the MATLAB commands x = rand; and y = rand; will produce a point randomly located in the unit square: (0<x<1) and (0<y<1). The probability that the randomly-generated point (x, y) will lie within the "unit quarter-circle" is equal to the ratio between the area of the unit quarter-circle and that of a unit square; i.e., π/4. Note that (x, y) lies within the unit quarter circle if x^2+y^2<1 Write a MATLAB program that will achieve the following tasks. a. Generate a minimum of 10,000 points (x,y). b. Test each point and increment a counter if x^2+y^2<1. c. Produce an estimate of π. d. Calculate the relative percent true error, E sub t.
n=10000;
for counter=0:n
x= rand;
y=rand;
z=hypot(x,y);
if (z<1)
Est_pi= Area*4;
counter= counter+1;
Error= (((abs(pi-(Est_pi)))/(pi)).*100);
end
end
1 Comment
Star Strider
on 10 Sep 2015
Original Question copied here:
The MATLAB command rand produces a uniformly-distributed random number between 0 and 1. For example, the MATLAB commands x = rand; and y = rand; will produce a point randomly located in the unit square: (0<x<1) and (0<y<1). The probability that the randomly-generated point (x, y) will lie within the "unit quarter-circle" is equal to the ratio between the area of the unit quarter-circle and that of a unit square; i.e., π/4. Note that (x, y) lies within the unit quarter circle if x^2+y^2<1 Write a MATLAB program that will achieve the following tasks. a. Generate a minimum of 10,000 points (x,y). b. Test each point and increment a counter if x^2+y^2<1. c. Produce an estimate of π. d. Calculate the relative percent true error, E sub t.
n=10000;
for counter=0:n
x= rand;
y=rand;
z=hypot(x,y);
if (z<1)
Est_pi= Area*4;
counter= counter+1;
Error= (((abs(pi-(Est_pi)))/(pi)).*100);
end
end
Accepted Answer
More Answers (1)
jimmymarsh
on 9 Sep 2015
Categories
Find more on Conway's Game of Life 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!