130 likes | 332 Views
Simulation Time-stepping and Monte Carlo Methods Random Number Generation. Shirley Moore CS 1401 Spring 2013 March 26, 2013. Agenda. Announcements Time-stepping simulation Lab 5 Wrapup Using random number methods Monte Carlo simulation Assignments for Tuesday, April 2
E N D
SimulationTime-stepping and Monte Carlo MethodsRandom Number Generation Shirley Moore CS 1401 Spring 2013 March 26, 2013
Agenda • Announcements • Time-stepping simulation • Lab 5 Wrapup • Using random number methods • Monte Carlo simulation • Assignments for Tuesday, April 2 • Review and practice for Exam 2 • Fun and review! Google Blockly Maze
Announcements • Two talks this Thursday at 12:00 and 1:00 (see course website for details) • Exam 2 on Thursday, practice and review in class and lab today • Unit 3 plan on course website
What is Computer Simulation? • Modeling real-world phenomena with computational algorithms • Normally requires some assumptions and approximations/simplifications • Aside: Are You Living In a Computer Simulation? ORIGINAL Nick Bostrom. Philosophical Quarterly, 2003, Vol. 53, No. 211, pp. 243-255, www.simulation-argument.com • Types of computer simulations • Time-stepping simulations • Monte Carlo simulations
Time-Stepping Simulations • Simulate continuous time with a loop • Each iteration of the loop is called a time-step and models an interval of time (e.g., 1 second, 0.01 second, 1 hour, 1 day, etc.) • Relevant physical quantities are updated each time stamp according to physical laws • Examples • Lab 5 Project Motion Simulation • Climate Change Simulation
2D Projectile Motion Simulation • In each time step (i.e., loop iteration), the statements in the loop body calculate approximately what happens to the projectile: • The projectile MOVES (changes position where position is given by x,y coordinates) because its velocity (with components in the horizontal x and vertical y directions) is not zero. • The projectile’s vertical velocity CHANGES because of the effect of gravity.
How does position change during one time step? • Change in a given direction is velocity in that direction multiplied by the time step. • e.g., If you are driving a car north at 40 miles an hour (velocity), then in 15 minutes = 0.25 hour (time step), your have gone 40 * 0.25 = 10 miles, so position position + 40 * 0.25 • For the projectile motion simulation xx + vx * dt yy + vy * dt
How does the velocity change during one time step? • Horizontal velocity doesn’t change • Newton’s First Law of Motion • Our model simplifies away air friction • Vertical velocity is reduced by effect of gravity • Newton’s Second Law of Motion • Again simplifying away air friction • Near Earth’s surface, gravity makes free falling objects accelerate at a rate of -9.8 (meters per second) per second • i.e., -9.8 is the change in velocity that occurs each second • So, vyvy + (-.9.8) * dt
Simulation Errors • What are sources of error in our projectile motion simulation? • Model simplifications • Computational approximations
Climate Change Simulation • Climate change as simulated by the NCAR CCSM: http://www.youtube.com/watch?v=d8sHvhLvfBo • Community Earth System Model (CESM) (formerly CCSM):http://www.cesm.ucar.edu • Very, Very Simple Climate Model: http://www.windows2universe.org/earth/climate/cli_model.html
Projectile Motion Calculations • Calculations inside the loop body t = t + dt
Random Number Generation • Math.random() is a Java method that returns a (pseudo)-random double value d such that 0.0 <= d < 1.0 • How can you use this method to generate a random integer in a given range – e.g., 1 to 6? • Write a loop that randomly tosses a 6-sided die.
Monte Carlo Simulation Example // Estimate π using Monte Carlo simulation public class MonteCarloSimulation { public static void main(String[] args) { final int NTRIALS = 1000000; intnHits = 0; double x, y; // Randomly generate NTRIALS points in 2x2 square // centered at origin and count how many // land inside unit circle. Ratio of circle area to // square area is π/4. for (inti = 0; i < NTRIALS; i++) { x = Math.random() * 2.0 – 1; y = Math.random() * 2.0 – 1; if (x*x + y*y <= 1.0) nHits++; } double pi = 4.0 * (nHits/NTRIALS); System.out.println(“PI is “ + pi); } } Actually, the above program has a bug and will always output 0.0. Can you find the bug and fix it?