CompSci 100e2.1 1 N-Body Simulation l Applications to astrophysics.  Orbits of solar system bodies.  Stellar dynamics at the galactic center.  Stellar.

Slides:



Advertisements
Similar presentations
Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
Advertisements

Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops.
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
CSC 221: Computer Programming I Fall 2001  conditional repetition  repetition for: simulations, input verification, input sequences, …  while loops.
Case study 1: Calculate the approximation of Pi
3.4 N-body Simulation Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright.
1 Powers of Two: Trace Ex. Print powers of 2 that are  2 N. Increment i from 0 to N. Double v each time. int i = 0; int v = 1; while (i
Module on Computational Astrophysics Professor Jim Stone Department of Astrophysical Sciences and PACM.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Professor John Peterson
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
CS 280 Data Structures Professor John Peterson. Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm.
Chapter 14 Simulation. Monte Carlo Process Statistical Analysis of Simulation Results Verification of the Simulation Model Computer Simulation with Excel.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 Fall 2008ACS-1903 Ch 4 Loops and Files while loop do-while loop Other topics later on …
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
Fundamentals of Python: From First Programs Through Data Structures
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Fundamentals of Python: First Programs
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
1.3 Conditionals and Loops Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · October.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Recap of Functions. Functions in Java f x y z f (x, y, z) Input Output Algorithm Allows you to clearly separate the tasks in a program. Enables reuse.
2.2 Libraries and Clients Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Control Structures RepetitionorIterationorLooping Part I.
Overview Go over parts of quiz? Another iteration structure for loop.
Control Structures RepetitionorIterationorLooping Part I.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Advanced Arithmetic, Conditionals, and Loops INFSY 535.
Counting Loops.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
1.3 Conditionals and Loops Copyright 2004, FoxTrot by Bill Amend
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Exercise 2 : Using for loop Repetition (loop) (1)control variable initialization (2)Test Conditon (3)Modification of control variable value order : (1)
Chapter 4 Repetition Statements (loops)
while Repetition Structure
Looping and Repetition
Iteration with While You can say that again.
Outline Altering flow of control Boolean expressions
Lecture Notes – Week 3 Lecture-2
Repetition Control Structure
Data types, assignment statements, and math functions allow you to compute You write a sequence of statements that tell the computer what needs to be done.
Loop Strategies Repetition Playbook.
Repetition Statements (Loops) - 2
Repetition Statements
Introduction to Computer Science I.
Looping Structures.
REPETITION Why Repetition?
Presentation transcript:

CompSci 100e2.1 1 N-Body Simulation l Applications to astrophysics.  Orbits of solar system bodies.  Stellar dynamics at the galactic center.  Stellar dynamics in a globular cluster.  Stellar dynamics during the collision of two galaxies.  Formation of structure in the universe.  Dynamics of galaxies during cluster formation. © Sedgewick & Wayne

CompSci 100e2.2 2 N-Body Simulation l 1. Setup initial distribution of particles.  Need accurate data and model of mass distribution. l 2. Compute forces between particles.  Direct sum: N 2.  Appel / Barnes-Hut" N log N. l 3. Evolve particles using ODE solver.  Leapfrog method balances efficiency and accuracy. l 4. Display and analyze results.  = softening parameter eliminates binary stars with r <  hard binaries can be important source of energy © Sedgewick & Wayne

CompSci 100e2.3 3 If Statement Examples © Sedgewick & Wayne

CompSci 100e2.4 While Loop The while loop. A common repetition structure.  Check a boolean expression.  Execute a sequence of statements.  Repeat. while ( boolean expression ) { statement 1 ; statement 2 ; } statement 1 true false boolean expression statement 2 loop body loop continuation condition © Sedgewick & Wayne

CompSci 100e2.5 5 For Loops The for loop. Another common repetition structure.  Execute initialization statement.  Check boolean expression.  Execute sequence of statements.  Execute increment statement.  Repeat. for ( init ; boolean expression ; increment ) { statement 1 ; statement 2 ; } statement 1 true false boolean expression statement 2 init increment body loop continuation condition © Sedgewick & Wayne

CompSci 100e2.6 6 Loop Examples © Sedgewick & Wayne

CompSci 100e2.7 Problem 1 l Given n, calculate 2 n  What if you wanted to print all from 2 0 to 2 n ?  What if you wanted to return the value?

CompSci 100e2.8 8 While Loops: Powers of Two Ex. Print first n powers of 2.  Increment i from 1 to n.  Double v each time. int i = 0; int v = 1; while (i <= N) { System.out.println(v); i = i + 1; v = 2 * v; } Console : iv true i <= N true true false n = 6 © Sedgewick & Wayne

CompSci 100e2.9 Problem 2 l Square Root:  Given a real number c and some error tolerance epsilon  Estimate t, the square root of c

CompSci 100e While Loops: Square Root Q. How might we implement Math.sqrt() ? l A. To compute the square root of c:  Initialize t 0 = c.  Repeat until t i = c / t i, up to desired precision: set t i+1 to be the average of t i and c / t i. computing the square root of 2 © Sedgewick & Wayne

CompSci 100e2.11 Problem 3 l Gambler's ruin. Gambler starts with $stake and places $1 fair bets until going broke or reaching $goal.  What are the chances of winning?  How many bets will it take?

CompSci 100e Gambler's Ruin l One approach. Monte Carlo simulation.  Flip digital coins and see what happens. Pseudorandom number generation java.util.Random  Repeat and compute statistics. © Sedgewick & Wayne