# INVSID 1.0: An Inverse System Identification Toolbox for MATLAB
## 2 Creation of INVSID 1.0
### MATLAB Codes
Then, derive the gain of the steady-state Kalman filter for the model (11) by using the following equation [25]:  
\[\mathbf{L}_{\mathrm{a},m}=\mathbf{A}_{\mathrm{a},m}\mathbf{P}_{m}\mathbf{C}_{\mathrm{a},m}^{ \mathrm{T}}(\mathbf{C}_{\mathrm{a},m}\mathbf{P}_{m}\mathbf{C}_{\mathrm{a},m}^{\mathrm{T}}+ R)^{-1}, \tag{28}\]  
where the value of \(\mathbf{P}_{m}\) can be derived as the unique solution of the following algebraic Riccati equation:  
\[\mathbf{P}_{m}=\mathbf{A}_{\mathrm{a},m}[\mathbf{P}_{m}-\mathbf{P}_{m}\mathbf{C}_{\mathrm{a},m}^{ \mathrm{T}}(\mathbf{C}_{\mathrm{a},m}\mathbf{P}_{m}\mathbf{C}_{\mathrm{a},m}^{\mathrm{T}}+ R)^{-1}\mathbf{C}_{\mathrm{a},m}\mathbf{P}_{m}]\mathbf{A}_{\mathrm{a},m}^{\mathrm{T}}+\mathbf{Q}, \tag{29}\]  
under the following conditions:  
1. \((\mathbf{A}_{\mathrm{a},m},\mathbf{C}_{\mathrm{a},m})\) is detectable. (A system is detectable if all the unobservable states are stable)
2. \((\mathbf{A}_{\mathrm{a},m},\mathbf{Q})\) is controllable. Then, stack all the calculated observer gains \(\mathbf{L}_{\mathrm{a},m}\) for \(m=1,2,\ldots,N\) into the matrix \(\mathbf{L}_{N}\), i.e.,  
\[\mathbf{L}_{N}= \begin{pmatrix}\mathbf{L}_{\mathrm{a},1}&\mathbf{L}_{\mathrm{a},2}&\ldots& \mathbf{L}_{\mathrm{a},N}\end{pmatrix} . \tag{30}\]After calculating the reconstructor transfer functions \(G_{\text{inv},m}(z)\) for \(m=1,2,\ldots,N\) based on (27), stack all the obtained transfer functions into the vector function \(\mathbf{E}_{N}\), which can be denoted as  
\[\mathbf{E}_{N}= \begin{pmatrix}G_{\text{inv},1}(z)\\ G_{\text{inv},2}(z)\\ \vdots\\ G_{\text{inv},N}(z)\end{pmatrix} . \tag{31}\]  
Obtain the frequency response function values \(G_{\text{inv},m}(e^{-j\Omega_{m}T_{\text{s}}})\) for \(m=1,2,\ldots,N\) by replacing \(z\) in Equation (27) with \(e^{-j\Omega_{m}T_{\text{s}}}\) for \(m=1,2,\ldots,N\), then stack all the frequency response function values into the vector \(\mathbf{G}_{N}\), i.e.,  
\[\mathbf{G}_{N}= \begin{pmatrix}G_{\text{inv},1}(e^{-j\Omega_{1}T_{\text{s}}})\\ G_{\text{inv},2}(e^{-j\Omega_{2}T_{\text{s}}})\\ \vdots\\ G_{\text{inv},N}(e^{-j\Omega_{N}T_{\text{s}}})\end{pmatrix} . \tag{32}\]  
The above process can be realized by the following MATLAB codes. > r1=size(sysss.a,1)+2;  
> AaN=zeros(r1*dim,r1);  
> for m=1:dim  r2=1+(m-1)*r1;  AaN(r2:r1*m,:)=[sysss.a sysss.b*CN(m,:)  zeros(2,size(sysss.a,1)) AN(((2*m-1):2*m),:)];  end  
> CaN=zeros(dim,r1);  
> for m=1:dim  CaN(m,:)=[sysss.c sysss.d*CN(m,:)];  end  
> CrN=zeros(dim,r1);  
> for m=1:dim  r2=1+(m-1)*r1;  LN(:,m)=dlqr(aN(r2:r1*m,:)',CaN(m,:)',Q,R)';  end  
> g=cell(dim,1);  
> GN=zeros(dim,1);  
> for m=1:dim  r2=1+(m-1)*r1;  sysr=ss(aN(r2:r1*m,:)-LN(:,m)*CaN(m,:),LN(:,m),CrN(m,:),0,Ts);  if isstable(sysr)==1  g{m,:}=sysr;  GN(m,:)=frd(g{m,:},FN(m,:),'Hz').ResponseData;  else  break  end  
**Remark 3**. : The values of \(Q\) and \(R\) can be tuned by changing the values of pc and mc. * With the calculated frequency response function values \(G_{\text{inv},m}(e^{-i\Omega_{m}T_{\text{s}}})\), \(m=1,2,\ldots,N\), the inverse model \(G_{\text{inv}}\) in state-space representation can be identified by using the MATLAB function n4 sid, then the Bode plot of the identified inverse model can be obtained. **Remark 4**. : The values of nx denote the inverse model order which can be specified. * The following MATLAB command can be used for the series connection of the models \(G_{\text{d}}\) and \(\hat{G}_{\text{inv}}\). * Gs=series(sysss,Ginv); Then, the Bode plot of the combined model can be displayed using the following codes.