CS 190C, Spring 2009: Problem Set 2

Posted: Friday, January 23, 2009

Due: Friday, January 30, 2009, 10pm (electronic submission via Blackboard)

Each program should be in a separate file. Name your files problem1.py and problem2.py. Please use the following template for your programs.

# CS190C: Spring 2009
# FirstName LastName
# Problem Set 2, Problem 1
def main():
   #This is where your code should go
   print "Hello world."

Problem 1

Write a Python program to approximate the value of sin(x) using the following Taylor series expansion:

Your program should prompt the user for the value of x (in radians) and the number of desired terms to be used in the approximation. It should output the approximate value computed, along with the value from math.sin(x), as well as the relative error (see lab 2 for details).


  • Your program should check that the value of x satisfies 0< = x < = 2*pi. If x is outside this range, print a message and do not compute an approximation.
  • Your program should check that no division by zero will be made.
  • Avoid unnecessary computations.

Problem 2

Monte Carlo methods are used to simulate complex physical and mathematical systems by repeated random sampling. In simple terms, given a probability, p, that an event will occur in certain conditions, a program generates those conditions repeatedly. The number of times the event occurs divided by the number of times the conditions are generated should be approximately equal to p.

The Monte Carlo method can be used to generate an approximate value of pi. The figure below shows a unit square with a quarter of a circle inscribed. The area of the square is 1 and the area of the quarter circle is pi/4. Using a random number generator, imagine “throwing” random points at the square. The ratio between the number of points that fall inside the circle (red points) and the total number of points thrown (red and green points) gives an approximation to the value of pi/4. This process is a Monte Carlo simulation approximating pi.

Write a Python program that runs this simulation. Your program will run a simulation for a specified number of iterations. let n be the variable representing the number of iterations. For a given n, executes n iteration, with each iteration generating a random point (x,y) and determining whether the point lies inside the circle or not. Assume that (O,O) is the lower left corner of the square. After the n iterations are completed, the program outputs the approximation of pi using the ratio of the points inside the circle and outside the circle (need to multiply the ratio by 4). Also output the number of iterations executed and the value of math.pi.

Your program should run for n = 100, 1000, 10000, 100000, and 1000000. Do not paste in the code five times, but use a for-loop around your simulation code that executes the simulation five times.


  • A random point is generated by generating two random numbers, each between 0 and 1 (including 0, but not 1). See Lab 2 how to generate random numbers using library random.
  • A point (x,y) lies inside the circle if sqrt(x^2 + y^2) <1.
  • When testing your code, run it only once (not 5 times).
  • Note that for the same value of n, you will see slightly different answers for pi as each execution uses a new set of random points.
cs190c/problemset2_09.txt · Last modified: 2009/01/26 08:57 (external edit)
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki