18.10.2014 Views

SIMSCRIPT II.5 Programming Language

SIMSCRIPT II.5 Programming Language

SIMSCRIPT II.5 Programming Language

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>SIMSCRIPT</strong> <strong>II.5</strong> <strong>Programming</strong> <strong>Language</strong><br />

j<br />

r<br />

r<br />

i<br />

r<br />

Figure 5-7. A Rectangular Coordinates System<br />

If we generate N points (i,j) within the square in a random fashion, some of the points will fall within<br />

the circle, and some will not. In fact, the proportion of those falling within the circle will be approximately<br />

π/4 of all the points. If M is the total number of those that fall within the circle, then<br />

M/N is approximately equal to π/4. We can estimate the value of π as 4M/N. The accuracy of this<br />

estimate improves as N increases, and is proportional to √N.<br />

The program shown below uses the function uniform.f to generate points (i,j) that are randomly<br />

distributed within a square of side R. It does this by generating random numbers between 0 and<br />

R and assigning them in pairs to i and j.<br />

Each such point (i,j) lies somewhere inside the square. If i 2 + j 2 ≤ r 2 , the point also lies within<br />

the circle, and 1 is added to M to record this fact. This procedure is repeated N times. Each time, a<br />

different i and j are generated and used to determine if the point (i,j) lies within the circle. At<br />

the end of N point generations, the approximation to π is printed.<br />

r<br />

main<br />

normally mode is real<br />

define HIT, I and NO.SAMPLES as integer variables<br />

read RADIUS and NO.SAMPLES<br />

let RADSQ = RADIUS**2<br />

let HIT = 0<br />

for I = 1 to NO.SAMPLES,<br />

do<br />

let XSAMPLE = uniform.f(0.0, RADIUS, 1)<br />

let YSAMPLE = uniform.f(0.0, RADIUS, 1)<br />

if XSAMPLE**2 + YSAMPLE**2 le RADSQ, '' within circle<br />

add 1 to HIT<br />

always<br />

loop<br />

let APPROX.PI = 4 * HIT/NO.SAMPLES<br />

print 1 line with NO.SAMPLES, APPROX.PI as follows<br />

THE ESTIMATED VALUE OF PI AFTER *** SAMPLES IS *.*****<br />

stop<br />

end<br />

r<br />

218

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!