AMath 585, Winter Quarter 2020 at the University of Washington. Developed by R.J. LeVeque and distributed under the BSD license. You are free to modify and use as you please, with attribution.
These notebooks are all available on Github.
This notebook shows a simple example.
%matplotlib inline
from pylab import *
from scipy.linalg import expm
The code below is supposed to solve $u'(t) = Iu(t)$ with $u(0) = \eta$ in the $5\times 5$ case where $I$ is the identity matrix and all elements of $\eta$ are equal to 1.
def utrue(t):
# Solve u'(t) = A u(t) with A = I, u(0) = eta
A = eye(5) # 5x5 identity matrix
eta = array([1.,1.,1.,1.])
u = dot(expm(A*t),eta) # matrix-vecor multiply
return u
utrue(1.)
The error message above some information about where the error occurred, and you might figure out from this that we tried to multiply a $5\times 5$ matrix by a vector with only 4 components, due to a typo in specifying eta
.
We can get more information by turning on the pdb debugger, using the Jupyter "magic" command pdb
. If you execute this a second time, it turns the debugger off.
pdb
utrue(1.)
Note that it now gives a ipdb>
prompt at the point where the exception occurred. You can query the state of variable, e.g. by typing an expresssion or a print statement and then hitting Enter. Type q
and Enter to quit (which you have to do before you can execute any other cell).
pdb
You can also put in a breakpoint to probe the state at some point, even if it is not giving an error. Here's a corrected version of the code with a breakpoint added.
from pdb import set_trace
def utrue(t):
# Solve u'(t) = A u(t) with A = I, u(0) = eta
A = eye(5) # 5x5 identity matrix
eta = array([1.,1.,1.,1.,1.])
u = dot(expm(A*t),eta)
print('This breakpoint is after setting A, eta, and u')
set_trace() # breakpoint here
return u
utrue(1.)
You can end the (Pdb)
prompt with q
to quit, or if things are working right, with c
to continue executing from this point.
If you happen to delete a cell when it's waiting for pdb input and the notebook hangs, remember that you can select Interrupt
from the Kernel
menu. Sometimes this doesn't work and you have to Restart
the kernel instead.