Presentation is loading. Please wait.

Presentation is loading. Please wait.

Simulation Time-stepping and Monte Carlo Methods Random Number Generation Shirley Moore CS 1401 Spring 2013 March 26, 2013.

Similar presentations


Presentation on theme: "Simulation Time-stepping and Monte Carlo Methods Random Number Generation Shirley Moore CS 1401 Spring 2013 March 26, 2013."— Presentation transcript:

1 Simulation Time-stepping and Monte Carlo Methods Random Number Generation Shirley Moore CS 1401 Spring 2013 March 26, 2013

2 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

3 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

4 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.comAre You Living In a Computer Simulation? ORIGINAL Nick Bostrom. Philosophical Quarterly, 2003, Vol. 53, No. 211, pp. 243-255 Types of computer simulations – Time-stepping simulations – Monte Carlo simulations

5 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

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

7 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 x  x + vx * dt y  y + vy * dt

8 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, vy  vy + (-.9.8) * dt

9 Simulation Errors What are sources of error in our projectile motion simulation? – Model simplifications – Computational approximations

10 Climate Change Simulation Climate change as simulated by the NCAR CCSM: http://www.youtube.com/watch?v=d8sHvhLvfBo http://www.youtube.com/watch?v=d8sHvhLvfBo Community Earth System Model (CESM) (formerly CCSM): http://www.cesm.ucar.eduhttp://www.cesm.ucar.edu Very, Very Simple Climate Model: http://www.windows2universe.org/earth/climate/cli_model.h tml http://www.windows2universe.org/earth/climate/cli_model.h tml

11 Projectile Motion Calculations Calculations inside the loop body t = t + dt

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

13 Monte Carlo Simulation Example // Estimate π using Monte Carlo simulation public class MonteCarloSimulation { public static void main(String[] args) { final int NTRIALS = 1000000; int nHits = 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 (int i = 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?


Download ppt "Simulation Time-stepping and Monte Carlo Methods Random Number Generation Shirley Moore CS 1401 Spring 2013 March 26, 2013."

Similar presentations


Ads by Google