## Batch least square method Python

Hua Xiaodian 2020-11-17 08:33:51
batch square method python

Reference resources ： System identification and adaptive control MATLAB Simulation （ Revised edition ） Pang Zhonghua Cui Hong

Simulation example 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 # Sequence length
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(' Input - The inverse M Sequence ')
plt.step(np.arange(L),IM)
plt.subplot(2,1,2)
plt.title(' Output -Y')
plt.plot(np.arange(L),Y)