Strict Standards: Declaration of Doku_Renderer_metadata::table_open() should be compatible with Doku_Renderer::table_open($maxcols = NULL, $numrows = NULL, $pos = NULL) in /home2/cp-wiki/htdocs/inc/parser/metadata.php on line 24

Strict Standards: Declaration of Doku_Renderer_metadata::table_close() should be compatible with Doku_Renderer::table_close($pos = NULL) in /home2/cp-wiki/htdocs/inc/parser/metadata.php on line 24

Warning: Cannot modify header information - headers already sent by (output started at /home2/cp-wiki/htdocs/inc/parser/metadata.php:24) in /home2/cp-wiki/htdocs/inc/actions.php on line 581

Warning: Cannot modify header information - headers already sent by (output started at /home2/cp-wiki/htdocs/inc/parser/metadata.php:24) in /home2/cp-wiki/htdocs/inc/actions.php on line 581
cs190c:problemset2_09
Table of Contents

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."
 
main()

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).

Comment:

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.

Comments: