-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprogram26.py
52 lines (46 loc) · 1.19 KB
/
program26.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
Spectral methods in MATLAB. Lloyd
Program 26
"""
# Eigenvalues of 2nd-order Chebyshev diff.matrix
from numpy import *
from scipy.linalg import eig
from matplotlib import pyplot as plt
from cheb import cheb
from numpy.linalg import matrix_power
from numpy.polynomial import chebyshev as n_cheb
N = 60
[D,x] = cheb(N)
D2 = matrix_power(D, 2)
D2 = D2[1:N,1:N]
Lam, V = eig(D2)
ii = argsort(-Lam)
e = Lam[ii]
V = V[:,ii]
# Plot eigenvalues
plt.subplot(3,1,1)
plt.hold('on')
plt.loglog(-real(e),'o')
plt.ylabel('eigenvalue')
plt.title('N = '+str(N)+', max|lambda| = '+str(max(-e)/N**4)+' N^4')
plt.semilogy([2*N/pi,2*N/pi],[1,1e6],'--r')
plt.text(2.1*N/pi, 24, '2*Pi/N')
# Plot eigenmodes N/4 (physical) and N (nonfysical):
vN4 = hstack([0,V[:,int(N/4.0)-2],0])
xx = arange(-1,1.01,0.01)
vh = n_cheb.chebfit(x, vN4, N)
vv = n_cheb.chebval(xx, vh)
plt.subplot(3,1,2)
plt.hold('on')
plt.plot(xx,vv)
plt.plot(x,vN4,'o')
plt.title('eigenmode N/4')
plt.axis([-1,1,-0.2,0.2])
vN = hstack([0,V[:,N-2],0])
plt.subplot(3,1,3)
plt.hold('on')
plt.semilogy(x,abs(vN))
plt.axis([-1,1,-1,1])
plt.plot(x,vN,'o')
plt.title('modulus of eigenmode N (log scale)')
plt.show()