Programming is becoming more important in today’s society and computer simulation has become a fundamental tool in research and industry. It is therefore natural to include programming as part of the scientific education. The CSE-project (Computing in Science Education) at the Faculty of Mathematics and Natural Sciences at the University of Oslo, has the goal to include computing as a natural tool for all science and engineering students from the first semester of their undergraduate studies. We will in this course, as one of the undergraduate courses at the University of Oslo, use computing as a tool to gain a better understanding of various phenomena in electromagnetism. The numerical part of FYS1120 includes visualization of electric and magnetic fields, motion of charged particles in field and simulation of circuits.
One of the tools we are going to use this year is the IPython Notebook, which is a web-based interactive computational environment designed for writing and running Python code. For that you can use the full power of Python and its many libraries. In addition to that the notebook allows you to make shareable documents, that combine that code with text, equations, visualizations, images and video. IPython Notebook is widely used in scientific computing and is a great environment to be working at.
Before you get started, you need to follow the instructions on how to install IPython Notebook.
Running IPython Notebook
In order to run IPython Notebook open up your terminal and use cd command in terminal to navigate to the desirable directory (e.g.: cd /Users/Milad/Documents/notebooks). Enter the following command:
ipython notebook --pylab inline
The “–pylab inline” argument is important, as it enables plots inside notebook documents.
It may take a minute or two to set itself up, but eventually IPython Notebook will open in your default web browser and should look something like this:
This is the IPython Notebook dashboard. If there are any notebooks (.ipynb files) in the directory you are inn, they will be listed here. Click on New Notebook to create a new notebook. A new window will pop up and should look something like this:
As you can see you have menus with several options. You should play around with these options gradually. The name of this notebook is by default untitled0, but you rename it by clicking on it.
As you can see you have a cell where you can execute Python code. In order to evaluate a cell click on the cell and press shift + Enter. Run this:
print "Electromagnetism is fun!"
This should give you:
Note the change from In[ ] to In. A number in brackets indicates that this specific cell is evaluated and the value of the number tells you the number of cells you have evaluated (you can also evaluate the same cell several times). You can go on now and write more commands in the same cell or you can insert a new cell from the menu: Insert > Insert Cell Above/Below or you can click on the arrow-icons with underlines.
Define two constants in one cell:
a = 2.0 b = 3.0 print a , b
Add a new cell and define c:
c = a+b
So add yet another cell and run:
Now go back to the cell where you defined b and redefine it to be 4.0 instead. Evaluate this cell and then jump to the last cell and print the value of c again:
As you can see the value of c is still 5 and not 6, although we redefined b to be 4. The reason for this is because we have not recalculated c after that we redefined the value of b. To get it right you must evaluate the cell where you calculate c, as well.
- You can evaluate all the cells in your notebook from top to bottom from the menu: Cell > Run All.
- You can clear the outputs from the menu: Cell > All Output > Clear.
- You can restart the kernel from the menu: Kernel > Restart. This will clear the memory for saved variables etc.
If you want to write plain text insert a new cell and change the cell type from code to Markdown. Within Markdown cells you can also include mathematics in a straightforward way, using standard LaTeX notation with $.
Press shift+ Enter to evaluate the cell:
- Markdown is a text-to-HTML conversion tool. More about the syntax here.
- You can write html code in Markdown cells.
Here is a short demo of the notebook’s basic features:
Check out this tutorial as well, but be aware that the IPython Notebook version in this toturial is a older version than the one we are using. The layout is different but the features are the same.
If you are interested to explore IPython Notebook further, check out these sites: