{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Workbook for Chapter 11 of Explorations in Computing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Section 11.4: -Body Simulation of the Solar System" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Execute these cells to initialize the notebook:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from PythonLabs.SphereLab import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%matplotlib tk" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Exercises:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T53. Call `make_system` to create a list of body objects for the Sun and planets:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T54. There should be ten objects in the list:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T55. The first object represents the Sun:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T56. Call `view_system` to plot the positions of all the bodies:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T57. It will be easier to watch the orbits if you tell `view_system` to track only the first five bodies (Sun, Mercury, Venus, Earth, Mars):" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T58. Run the simulation for 10 time steps, using a step size of 1 day:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T59. Continue the simulation for another 355 days, so the total simulation runs for 365 time steps:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T60. Ask Python to print the mass of the Sun and the mass of Mars:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T61. Type this expression to multiply the mass of Mars by $1 \\times 10^6$ :" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T62. Continue the simulation for another 365 time steps:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "♦ Try some more experiments on your own. Start each experiment by making a fresh list of bodies and reinitializing the display." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `nbody` " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enter the definition of the `step_system` function (Figure 11.12) in the code cell below.\n", "If you want to copy and paste the code you'll find it at `nbody.py`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T64. Verify the function works by reading the solar system data file and simulating a few steps:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T65. Add a print statement to the inner `for` loop, right before the call to `Body.interaction`, and execute the code cell to update your function definition." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T66. Make a single call to the updated `step_system` function:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T67. Is every body being compared with every other body? When five bodies are passed to `step_system`, each one should be compared with all four of the others. Does each name show up exactly four times?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T68. How many lines are printed? Since we expect $n (n-1) / 2$ interactions, there should be ten lines for this set of five bodies." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T69. Another way to count the number of times `Body.interaction` is called is to initialize a variable named `count` to 0 before the outer `for` loop, update the variable inside the inner `for` loop, and print the value after the loop terminates. Add these statements to your code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T70. Re-execute the code cell with your function and run it again. Does it print 10 as the value of `count` when you pass it the five bodies in `b[0:5]`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T71. What is the value of `count` if you pass the entire solar system to `step_system`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }