PIC 10A3 Homework 4 Due 5 PM, Friday, Nov 1
Problem 1: Monte Carlo Experiments
The Monte Carlo method is used in modeling a widerange of physical systems at the forefront of scientific research today. Lets consider the problem of estimating the area of the region x22y21 by utilizing the Monte Carlo method. This region is enclosed by a blue ellipse, which is inscribed in a 22 red square shown in the figure.
The experiment simply consists of throwing darts on this figure completely at random mean ing that every point in the red square has an equal chance of being hit by the dart. You keep throwing darts at random. All of the darts fall within the square, but not all of them fall within the ellipse. If you throw darts completely at random, this experiment estimates the ratio of the area of the ellipse to the area of the square, by counting the number of darts within each area.
Program this Monte Carlo method to compute the area of the blue ellipse using dierent numbers of darts. For each experiment, use N darts where N10, 102,, 106, and output the estimated area and the corresponding numerical error estimated areaexact area. The exact area for the ellipse is given by p2 2.22144146907918.
2
A successful run of your code may look like:
Instructions: This program is to give you practice using loops, the random number gen erator, math library, and output formatting. You may not use stdafx.h. Include header comments. Include iomanip to format your output. Name your file monteCarlo.cpp and submit it on CCLE.
estimated area2.40000
estimated area2.16000
estimated area2.18000
estimated area2.23680
estimated area2.21072
darts: 10
darts: 100
darts: 1000
darts: 10000
darts: 100000
darts: 1000000estimated area2.22037 error0.00107
error0.17856
error0.06144
error0.04144
error0.01536
error0.01072
Due 5 PM, Friday, Nov 1 Homework 4 PIC 10A3 Problem 2: Random Walk Robot
A robot is initially located at position 0, 0 in a grid 5, 55, 5. The robot can move randomly in any of the directions: up, down, left, right. The robot can only move one step at a time. For each move, print the direction of the move and the current position of the robot. If the robot makes a circle, which means it moves back to the original place, print Back to the origin! to the console and stop the program. If it reaches the boundary of the grid, print Hit the boundary! to the console and stop the program.
A successful run of your code may look like:
Down0,1
Down0,2
Up0,1
Left1,1
Left2,1
Up2,0
Left3,0
Left4,0
Left5,0
Hit the boundary!
or
Left1,0
Down1,1
Right 0,1
Up0,0
Back to the origin!
Instructions: This program is to give you practice using the control flow, the random number generator, and output formatting. You may not use stdafx.h. Include header comments. Include iomanip to format your output. Name your file randomWalk.cpp and submit it on CCLE.
Reviews
There are no reviews yet.