Python

Weniger 412 Computer Lab

When you first get an account for the computer lab you will be assigned a password. Step 1 is changing your password.

  1. Log in
  2. Open a terminal (move the mouse to the upper left
  3. ssh into the server by entering “ssh physics-server” and then re-enter your password
  4. Then type the command “passwd”
  5. You will be prompted for your current password and your new password.
  6. Close the ssh connection by typing “exit”

Python editor and compiler

We will be using the editor called “geany” to edit python programs. You can start geany by moving the mouse to the upper-left corner of the screen and then typing “geany” (although actually, only the first few letters are needed). When you save a file with a .py extension, geany will understand that it is a python file, and allow you to run it (F5) directly from the editor.

Hello world

from __future__ import division
from pylab import *
print("hello world")

When you are ready to run, save the program with .py extension then press F5.

Making a matrix

from __future__ import division
from pylab import *
V = zeros((6,6),float)
print V

When you are ready to run, press F5.

For loop to modify the matrix

from __future__ import division
from pylab import *
V = zeros((6,6),float)
for i in arange(0,6):
  V[i,0]=1
print V

Note that arange defines an interval of numbers. In this case, the interval is bracketed by 0 and 6. So the interval is {0, 1, 2, 3, 4, 5}.

Project

Using the concepts outlined above, implement the relaxation algorithm to solve del^2 = 0 inside a matrix. Make sure you have an interesting boundary condition on the edges of the matrix. Once you have an interesting final matrix, you might want to visualize it:

from __future__ import division
from pylab import *
import matplotlib.pyplot as plt
V = zeros((6,6),float)
#your boundary conditions go here
#your relaxation algorithm goes here
plt.contourf(V) #alternatively you can also try plt.contour(V)
plt.colorbar()
plt.show()

The default settings for the contour plot might not give you enough contour lines. You can easily add more contour levels:

mylevels = [0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5]
plt.contourf(V,levels=mylevels)
plt.show()