NBody Simulation l Applications to astrophysics Orbits of
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. Comp. Sci 100 e © Sedgewick & Wayne 1 2. 1
N-Body Simulation l l 1. Setup initial distribution of particles. Ø Need accurate data and model of mass distribution. 2. Compute forces between particles. Ø Direct sum: N 2. e = softening parameter Ø Appel / Barnes-Hut" N log N. eliminates binary stars with r < e hard binaries can be 3. Evolve particles using ODE solver. important source of energy Ø Leapfrog method balances efficiency and accuracy. 4. Display and analyze results. Comp. Sci 100 e © Sedgewick & Wayne 2 2. 2
If Statement Examples Comp. Sci 100 e © Sedgewick & Wayne 3 2. 3
While Loop l The while loop. A common repetition structure. Ø Check a boolean expression. Ø Execute a sequence of statements. Ø Repeat. loop continuation condition while (boolean expression) { statement 1; statement 2; loop body } statement 2 boolean expression true statement 1 false Comp. Sci 100 e © Sedgewick & Wayne 2. 4
For Loops l The for loop. Another common repetition structure. Ø Execute initialization statement. Ø Check boolean expression. Ø Execute sequence of statements. Ø Execute increment statement. loop continuation condition init Ø Repeat. for (init; boolean expression; increment) { statement 1; statement 2; body } increment statement 2 boolean expression true statement 1 false Comp. Sci 100 e © Sedgewick & Wayne 5 2. 5
Loop Examples Comp. Sci 100 e © Sedgewick & Wayne 6 2. 6
Problem 1 l Given n, calculate 2 n Ø What if you wanted to print all from 20 to 2 n? Ø What if you wanted to return the value? Comp. Sci 100 e 2. 7
While Loops: Powers of Two l 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; } i v i <= N 0 1 true 1 2 true 2 4 true 3 8 true 4 16 true 5 32 true 6 64 true 7 128 false Console: 1 2 4 8 16 32 64 n = 6 Comp. Sci 100 e © Sedgewick & Wayne 8 2. 8
Problem 2 l Square Root: Ø Given a real number c and some error tolerance epsilon Ø Estimate t, the square root of c Comp. Sci 100 e 2. 9
While Loops: Square Root l l Q. How might we implement Math. sqrt() ? A. To compute the square root of c: Ø Initialize t 0 = c. Ø Repeat until ti = c / ti, up to desired precision: set ti+1 to be the average of ti and c / ti. computing the square root of 2 Comp. Sci 100 e © Sedgewick & Wayne 10 2. 10
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? Comp. Sci 100 e 2. 11
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. Comp. Sci 100 e © Sedgewick & Wayne 12 2. 12
- Slides: 12