Greetings all,<br><br>I've recently begun using Python to do scientific computation, and I wrote the following script to find approximate eigenvalues for a semi-infinite matrix:<br><br><br><div style="margin-left: 40px;">
from pylab import *<br>from numpy import *<br>from scipy import *<br><br>def bandstructure(N,s):<br><br>    b = s/4.0<br><br>    jmax = 10 + N**2<br><br>    spectrum1 = [0]*2*N<br>    spectrum2 = [0]*2*N<br>    spectrum3 = [0]*2*N
<br><br><br>    for k in arange(1, 2*N+1, 1):<br><br>        A = zeros( (jmax,jmax) )<br><br>        i = 0<br>        while i <= jmax-1:<br>            if i <= jmax-2:<br>                A[i,i+1] = b<br>                A[i+1,i] = b
<br>                A[i,i] = ((k + 2.0*i*N)/N)**2<br>                i = i+1<br>            else:<br>                A[i,i] = ((k + 2.0*i*N)/N)**2<br>                i = i+1<br><br>        #This portion of the code builds a matrix twice as large to check against
<br><br>        B = zeros( (2*jmax,2*jmax) )<br><br>        i = 0<br>        while i <= 2*jmax-1:<br>            if i <= 2*jmax-2:<br>                B[i,i+1] = b<br>                B[i+1,i] = b<br>                B[i,i] = ((k + 
2.0*i*N)/N)**2<br>                i = i+1<br>            else:<br>                B[i,i] = ((k + 2.0*i*N)/N)**2<br>                i = i+1<br><br>        x = linalg.eigvals(A)<br>        y = linalg.eigvals(B)<br><br>        j = 1
<br>        while j<=3:<br>            if abs(y[j]-x[j]) <= 10.0**(-5):<br>                j = j + 1<br>            else:<br>                print 'jmax not large enough to obtain accurate results'<br><br>        spectrum1[k-1] = x[0] + 
0.5*s<br>        spectrum2[k-1] = x[1] + 0.5*s<br>        spectrum3[k-1] = x[2] + 0.5*s<br><br>    plot (k, spectrum1, k, spectrum2, k, spectrum3)<br><br>    xlabel('k (energy level)')<br>    ylabel('E/E_r')
<br>    title('Finite Size Band Structure, N = %d, s = %f' % (N, s))<br>    grid(true)<br></div>    <br><br>When I run this script, I get the following message, which I can't figure out:<br><br><div style="margin-left: 40px;">
Traceback (most recent call last):<br>  File "<stdin>", line 1, in <module><br>  File "bandstruc.py", line 61, in bandstructure<br>    plot (k, spectrum1, k, spectrum2, k, spectrum3)<br>  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/pylab.py", line 2028, in plot
<br>    ret =  gca().plot(*args, **kwargs)<br>  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/axes.py", line 2535, in plot<br>    for line in self._get_lines(*args, **kwargs):
<br>  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/axes.py", line 437, in _grab_next_args<br>    for seg in self._plot_2_args(remaining[:2], **kwargs):<br>  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/axes.py", line 337, in _plot_2_args
<br>    x, y, multicol = self._xy_from_xy(x, y)<br>  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/matplotlib/axes.py", line 266, in _xy_from_xy<br>    nrx, ncx = x.shape<br>
ValueError: need more than 0 values to unpack<br><br></div>Also, I've been having trouble with the plot function in matplotlib. For example, I enter the following in the terminal:<br><br><div style="margin-left: 40px;">
>>> from pylab import *<br>>>> plot([1,2,3])<br>[<matplotlib.lines.Line2D instance at 0x16adda30>]<br><br></div>I'm reasonably sure that the issue in the first big error message is just an indexing error on my part, but I have no idea what the second thing means. Every time I run the plot([1,2,3]) I get a different ending number that seems to vary randomly.
<br><br>Could anybody please help me out with these problems?<br><br>Thanks,<br><br>Stephen<br>