第三章

kkk12367發表於2024-10-15

學號3020
3.2

點選檢視程式碼
def X(n):  
    # 差分方程的解  
    return 2 * (-1)**(n + 1)  
  
 
n_values = [0, 1, 2, 3, 4, 5]  
for n in n_values:  
    print(f"X({n}) = {X(n)}")
 
print("學號:3020")

3.3

點選檢視程式碼
import numpy as np
 
from scipy.sparse.linalg import eigs
 
import pylab as plt
 
  
 
w = np.array([[0, 1, 0, 1, 1, 1],
              [0, 0, 0, 1, 1, 1],
              [1, 1, 0, 1, 0, 0],
              [0, 0, 0, 0, 1, 1],
              [0, 0, 1, 0, 0, 1],
              [0, 0, 1, 0, 0, 0]])
 
r = np.sum(w,axis=1,keepdims=True)
 
n = w.shape[0]
 
d = 0.85
 
P = (1-d)/n+d*w/r #利用矩陣廣播
 
w,v = eigs(P.T,1) #求最大特徵值及對應的特徵向量
 
v = v/sum(v)
 
v = v.real
 
print("最大特徵值為:",w.real)
 
print("歸一化特徵向量為:\n",np.round(v,4))
 
plt.bar(range(1,n+1),v.flatten(),width=0.6)
 
plt.show()
 
 
print("學號:3020")

相關文章