write a code in matlab to calculate the value of Pi by estim

write a code in matlab to calculate the value of Pi by estimating the area of a 2x2 square and the inscribing circle of radius 1. (e.g. randomly throwing pebbles into a 2x2 square, and calcuate the pebble counts inside and outside the circle)

Solution

countC = 0
totalCount = 1000
for i = 1:totalCount
x = 2*rand() - 1
y = 2*rand() - 1
r = sqrt(x*x + y*y)
if r <= 1
countC = countC + 1
end
end

% area of circle pi * r * r when r is 1 area is pi
% area of square is side*side = 2*2 = 4
% probability of point in circle is = area of circle / area of square
% p = pi/4
% pi = p*4
% p = points inside circle / total points
calc_pie1 = 4*(countC/totalCount)

write a code in matlab to calculate the value of Pi by estimating the area of a 2x2 square and the inscribing circle of radius 1. (e.g. randomly throwing pebble

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site