Profile picture

Professor of Computer Science @ Olin College

Modeling and Simulation in Python

Last updated: December 13th, 20182018-12-13Project preview

Welcome to Modeling and Simulation, welcome to Python, and welcome to Jupyter. This is a Jupyter notebook, which is a development environment where you can write and run Python code. There's a complete tutorial on how to use these notebooks below (Quick notebook tutorial).

How this code is structured

There's one directory per chapter (Chapter 1, Chapter 2, etc). Inside each directory you'll find the working notebook along with a copy that includes the solutions to the exercises. Here's a summary:

Along with each chapter's code, we include a directory with code utilities under code_utils. That code is available under each Chapter.

Quick notebook tutorial

Welcome to Modeling and Simulation, welcome to Python, and welcome to Jupyter.

This is a Jupyter notebook, which is a development environment where you can write and run Python code. Each notebook is divided into cells. Each cell contains either text (like this cell) or Python code.

Selecting and running cells

To select a cell, click in the left margin next to the cell. You should see a blue frame surrounding the selected cell.

To edit a code cell, click inside the cell. You should see a green frame around the selected cell, and you should see a cursor inside the cell.

To edit a text cell, double-click inside the cell. Again, you should see a green frame around the selected cell, and you should see a cursor inside the cell.

To run a cell, hold down SHIFT and press ENTER.

  • If you run a text cell, Jupyter typesets the text and displays the result.

  • If you run a code cell, it runs the Python code in the cell and displays the result, if any.

To try it out, edit this cell, change some of the text, and then press SHIFT-ENTER to run it.

Adding and removing cells

You can add and remove cells from a notebook using the buttons in the toolbar and the items in the menu, both of which you should see at the top of this notebook.

Try the following exercises:

  1. From the Insert menu select "Insert cell below" to add a cell below this one. By default, you get a code cell, as you can see in the pulldown menu that says "Code".

  2. In the new cell, add a print statement like print('Hello'), and run it.

  3. Add another cell, select the new cell, and then click on the pulldown menu that says "Code" and select "Markdown". This makes the new cell a text cell.

  4. In the new cell, type some text, and then run it.

  5. Use the arrow buttons in the toolbar to move cells up and down.

  6. Use the cut, copy, and paste buttons to delete, add, and move cells.

  7. As you make changes, Jupyter saves your notebook automatically, but if you want to make sure, you can press the save button, which looks like a floppy disk from the 1990s.

  8. Finally, when you are done with a notebook, select "Close and Halt" from the File menu.

Using the notebooks

The notebooks for each chapter contain the code from the chapter along with addition examples, explanatory text, and exercises. I recommend you

  1. Read the chapter first to understand the concepts and vocabulary,
  2. Run the notebook to review what you learned and see it in action, and then
  3. Attempt the exercises.

If you try to work through the notebooks without reading the book, you're gonna have a bad time. The notebooks contain some explanatory text, but it is probably not enough to make sense if you have not read the book. If you are working through a notebook and you get stuck, you might want to re-read (or read!) the corresponding section of the book.

Importing modsim

The following cell imports modsim, which is a collection of functions we will use throughout the book. Whenever you start the notebook, you will have to run the following cell. It does three things:

  1. It uses a Jupyter "magic command" to specify whether figures should appear in the notebook, or pop up in a new window.

  2. It configures Jupyter to display some values that would otherwise be invisible.

  3. It imports everything defined in modsim.

Select the following cell and press SHIFT-ENTER to run it.

In [1]:
# Configure Jupyter so figures appear in the notebook
%matplotlib inline

# Configure Jupyter to display the assigned value after an assignment
%config InteractiveShell.ast_node_interactivity='last_expr_or_assign'

# import functions from the modsim library
from modsim import *

print('If this cell runs successfully, it produces no output other than this message.')

The first time you run this on a new installation of Python, it might produce a warning message in pink. That's probably ok, but if you get a message that says modsim.py depends on Python 3.7 features, that means you have an older version of Python, and some features in modsim.py won't work correctly.

If you need a newer version of Python, I recommend installing Anaconda. You'll find more information in the preface of the book.

Restart and run all

When you change the contents of a cell, you have to run it again for those changes to have an effect. If you forget to do that, the results can be confusing, because the code you are looking at is not the code you ran.

If you ever lose track of which cells have run, and in what order, you should go to the Kernel menu and select "Restart & Run All". Restarting the kernel means that all of your variables get deleted, and running all the cells means all of your code will run again, in the right order.

Exercise: Select "Restart & Run All" now and confirm that it does what you want.

Notebooks AI
Notebooks AI Profile20060