This is the explanation of our Monte Carlo simulation program to compute the value of pi by find the area of a unit circle lying within a 2 by 2 square. The simulation is done only for the first quadrant. We did the simulation in two slightly different ways. In the first way, the simulation was done only once each time the program is run. A sequence of n0 random points whose x-coordinates and y-coodinates are contained in ROW vectors Xvec and Yvec. A logical (ROW) vector was computed (but it was not stored in a variable). It has a value of 1 at points that lie within the circle, otherwise it has a value of 0. We need to sum over this vector to find n, the total number of points that lie within the circle. Thus we need to sum over the second index of the logical ROW vector. But in the case of summing over a vector (ROW or COLUMN), we can simply use sum(LogicalVector) without a second argument to specify which dimension to perform the sum (since it is obvious for vectors, simply sum over the dimension that is different from 1). We then did the simulation a second way. This time the simulation was done a certain number of times, specified by the variable repeat. In other words there are a number of different copies of the simulation. Data for different copies of the simulation occupy a different ROW in the MATRICES XX, YY, as well as in the resulting logical MATRIX that specifies whether or not a given point lies within the circle. The first index of these matrices (labeling the rows) specifies the individual copy of the simulation. The second index (labeling the columns) specifies the points (first point, second point, etc.). The sum that computes the total number of points within the circle must add up the elements of this logical MATRIX, one for each ROW (i.e. for each copy of the simulation). That is why the sum function must have a second argument indicating that the sum is done over the second index, otherwise the default is to sum over the first index, which would be incorrect here! Thus the resulting n (now renamed NumberPtsInCircle) is a column vector, and so is MyPi and RelativeErrors. Each element of these vectors are the results for each copies of the simulation. The final answer is obtained by taking the mean of all the computed values for pi.