// Here we will find an approximate value //of pi by placing random points in a square //and looking at how many of those fall //inside an inscribed circle. // The followong lines will create // two vectors (list) of x and y // values each evenly distibuted between // -1 and 1. // rand(m,n) will give you m rows of // n random values between 0 and 1. // The time 2+1 is just to scale the // values to be between -1 and 1. x=rand(1,n)*2-1; y=rand(1,n)*2-1; scf(0) // Sets Current Frame (graph window) // to number 0. clf // CLears the current Frame. isoview // Sets the scale to be equal in // the x and y direction. scatter(x,y,1) // A scatter plot with small // dots (size 1). xarc(-1,1,2,2,0,360*64) // Draws a circle d2=x.^2+y.^2; // Finds the length squared // of the distance of the points to the // origin. xc=x(d2<1); // Selects the points closer yc=y(d2<1); // to the origin than 1. scatter(xc,yc,1,"red") // Make a scatter // plot of the selected ponts with point size // 1 and drawn in red. disp(4*length(xc)/n) // Displays the four // timed the ratio between the points in the // circle v.s. the total number of points. // Should be close to pi.