# CE 691 Homework assignment # 2

```CS 151L – Spring 2015
Programming Assignment 2
Due: Wednesday, February 4, 2015 at 11:59 PM
(Email in two separate MatLab scripts called “cs151sp15assn2a.m” and “cs151sp15assn2b.m”.
1. Compute an approximation of  to arbitrary precision by dividing the perimeter of an n-sided regular
polygon inscribed in a unit circle by the radius, r = 1.
s

r=1
Write a MatLab script that does the following:
a) Create a 1 by 6 matrix, or row vector, representing the number of sides of a polygon where n is
equal to 4, 8, 16, 32, 64 and 128.
b) Compute another row vector, theta (), whose elements correspond to those of the array, n. You
will need to calculate the value of theta based on the number of sides of the polygon and knowing
that a complete circle encompasses 360 degrees. (Do not do the calculations using a calculator;
you should be using MATLAB operations to determine these values.)
c) Compute a row vector, s, representing the length of one side of the polygon, whose elements
correspond to those of the array, n. Note that you now have calculated the angle within each
triangle and that two of the sides of the triangle are equal to 1.
d) Compute the row vector, p, representing the perimeter of each polygon.
e) For a circle, the ratio of the circumference to the diameter is   c  d and   c
d
). Therefore,
 is approximately perimeter/(2*r). Using this information, compute pi_approx, for each polygon.
1
f)
Compute the row vector, percent_error, whose elements represent the percent error in pi_approx
for each polygon. The percent error is defined as (note that this value should always be positive):
 approximate  exact 
100  

exact


g) Plot the percent error (vertical axis) versus n (horizontal axis) using the plot command plot(a, b)
where a is the horizontal value and b is the vertical value.
2. Compute an approximation of  by using random numbers. Assume a circle of radius ½ unit is
inscribed within a square with sides of length a = 1 unit. The area of the circle is given by:
Acircle  r 2   0.5 
2
Therefore,

, while the area of the unit square is given by Asquare  a   1  1 .
2
4
2
 A

Acircle  / 4 

 , and   4 circle  . We can approximate  probabilistically by
A

Asquare
1
4
 square 
recognizing that if we randomly throw darts at the unit square region, the number of “hits” within the
circle to “hits” within the square is approximately in the ratio of
 A

n
ncircle
A
 circle . Therefore
nsquare Asquare

  4 circle   4 circle  .
 Asquare 
 nsquare 
y
a=1
y=1
.
.
r = 1/2
.
.
a=1
.
y=0
.
x
x=0
x=1
Step-by-Step Solution
a) Create the variable n as a 1 by 1 matrix, or scalar, where the user must input the value using the
input command.
b) Create two 1 by n row vectors x and y, each containing n random numbers between 0 and 1. (Use
the MATLAB function rand().)
2
c) Compute a 1 by n matrix, dist, containing the distance of each point (xi, yi) from the center of the
circle (Note that the center of the circle is located at (0.5, 0.5) on the coordinate system in the
diagram). You will need to use the distance formula ( x  0.5   y  0.5 ) to create this
variable (you can use the sqrt() function or raise the values to the ½ power.)
d) Compute the row vector, in_out, which contains a “1” if each point is within the circle, and a “0”
if each point is not within the circle. (Hint: you can use the MATLAB function floor() and
multiply the distance by 2 – the radius of the circle is only ½; therefore, if the distance of one of
the points is 0.44, then it will be in the circle and if the distance of one of the points is 0.51, then
it is outside the circle. There are always other methods to perform this calculation.)
2
2
 Acircle 


  4 ncircle  .
A

n

 square 
 square 
e) Determine an approximation of  , pi_approx, by the formula   4
(Hint: you can use the MATLAB function sum() and remember that the value in the denominator
is the number of hits in the square, not the number of hits outside the circle!)
f) Determine the percent error, percent_error, in the approximation of  (this value should always
be positive).
g) Now, re-run the script using n = 1e2, 1e3, etc. (i.e. 100, 1000, etc.) In your m-file, answer the
following question: what must be the order of magnitude of n to routinely get  to within 1
percent accuracy?
3
```