%% A program to find the value of pi using random numbers clc clear all format long % Variable count counts the number of dots inside the circle of radius 1 % drawn with the center at the origin of a x-y coordinate system! %% INPUTS % Choose total number of dots! n=8E6 %% CODE count=0; for i=1:1:n % random choice of x value x=randab(-1,1); %randomn choice of y value y=randab(-1,1); % checking of the point is inside or outside the circle if x^2+y^2<=1 count=count+1; end end % The area of the square is 4. % The ratio of the number of dots inside the circle/number of dots in the % square is the ratio of the area of the circle/area of the square. pival=count/n*4; % actual value of pi %% OUTPUTS picalc=pival piactual=pi % Percentage true error absea=abs((piactual-picalc)/piactual)*100