批处理最小二乘法 python
参考:系统辨识与自适应控制MATLAB仿真(修订版) 庞中华 崔红
仿真实例2.5
import numpy as np import matplotlib.pyplot as plt from mxulie import M_sequences plt.rcParams['font.sans-serif'] = ['Yahei consolas hybrid'] #plt.rcParams['axes.unicode_minus']=False if __name__ == '__main__': L = 100 #序列长度 Y = np.zeros(L) phi = np.zeros((L,4)) [M,IM]=M_sequences(L) xi = np.sqrt(0.01) * np.random.randn(L,1) y1 = y2 =0 u4 = u3 = u2 =u1 =0 theta = np.array([1.5,-0.7,1,0.5]) for i in np.arange(L): phi[i,:] = ((y1 , y2 ,u3 ,u4)) Y[i] = np.dot(theta,phi[i,:]) + xi[i] #Y[i] = 1.5*y1 -0.7*y2 + u3 + 0.5*u4 + xi[i] y2 = y1 y1 = Y[i] u4 = u3 u3 = u2 u2 = u1 u1 = IM[i] theta1 = np.dot(np.dot(np.linalg.inv(np.dot(phi.T,phi)),phi.T),Y) print(theta1) plt.subplot(2,1,1) plt.title('输入-逆M序列') plt.step(np.arange(L),IM) plt.subplot(2,1,2) plt.title('输出-Y') plt.plot(np.arange(L),Y) plt.subplots_adjust(hspace = 0.35) plt.show()