-
Notifications
You must be signed in to change notification settings - Fork 2
/
ch6ex12.py
43 lines (36 loc) · 1 KB
/
ch6ex12.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
# Example 6.12
# Calculation of Derivatives Using Chebyshev Derivative Matrix Operator
from pylab import*
# Initialization
clf()
N = 5
u = lambda x: 4.*multiply(np.power(x, 2), multiply(1. - np.power(x, 2), exp(-x/2.)))
j = r_[0:N+1]
X = cos(j*pi/N)
U = u(X)
D = asmatrix(zeros([N+1,N+1]))
# Creation of the matrix operator
for j in range(N+1):
for k in range(N+1):
if j == k:
if j == 0:
D[j,k] = (2.*N**2 + 1.)/6.
elif j == N:
D[j,k] = -(2.*N**2 + 1.)/6.
else:
D[j,k] = -X[j]/(2.*(1. - X[j]**2))
else:
if (j == 0 or j == N):
cj = 2
else:
cj = 1.
if (k == 0 or k == N):
ck = 2
else:
ck = 1.
D[j,k] = cj*(-1.)**((j + 1) + (k + 1))/(ck*(X[j]-X[k]))
# Display the results
print("X =\n", X)
print("U =\n", U)
print("D =\n", D)
print("D*U =\n", dot(D, U))