n = 5; I = eye(n); % Diension of the state vector of RNN
N = 15; % Number of neurons in the hidden layer
p = n; C0 = I; C1 = eye(N); C2 = C1; % Whole state is measured
A0 = -diag(1:.25:1+(n-1)*.25); A1 = rand(n,N); H1 = rand(N,n); A2 = rand(n,N); H2 = rand(N,n); % Random matrices for RNN dynamics
L0 = n*C0'; L1 = N*H1'*C1'; L2 = N*H2'*C2'; % Observer gains
dm = 0; dM = 5; d = dM*max(rand(n,1),dm+0.1*dM*ones(n,1)); % Minimum and maximum values of disturbances
disp(' ');

disp(' ');
disp('***************************************************************************'); % Verification of LMIs
disp('Check stability')

% Formulation of LMIs
epsilon1 = 1e-10; epsilon2 = epsilon1/1e2; 
Gamma = [I -I]; D0 = blkdiag(A0-L0*C0,A0-L0*C0); D1 = blkdiag(A1-L1*C1,A1-L1*C1); D2 = blkdiag(A2-L2*C2,A2-L2*C2); Hh1 = blkdiag(H1,H1); Hh2 = blkdiag(H2,H2);
P1 = sdpvar(2*n); P2 = sdpvar(n); Lam1 = sdpvar(2*N,2*N,'diagonal'); Lam2 = sdpvar(2*N,2*N,'diagonal'); Phi = sdpvar(2*n); 
Psi = sdpvar(2*n,2*n,'full'); Om0 = sdpvar(2*n,2*n,'full'); Om1 = sdpvar(2*n,2*N,'full'); Om2 = sdpvar(2*n,2*N,'full');
Ups00 = sdpvar(2*n,2*n,'diagonal'); Ups11 = sdpvar(2*n,2*n,'diagonal'); Ups22 = sdpvar(2*n,2*n,'diagonal');
Ups01 = sdpvar(2*N,2*N,'diagonal'); Ups02 = sdpvar(2*N,2*N,'diagonal'); Ups12 = sdpvar(2*n,2*n,'diagonal');
LMI1 = [Phi >= epsilon1*eye(2*n), P2 >= epsilon1*I, P1 >= 0, Lam1 >= 0, Lam2 >= 0];
LMI2 = [Ups00 >= 0, Ups11 >= 0, Ups22 >= 0, Ups01 >= 0, Ups02 >= 0, Ups12 >= 0]; P = P1+Gamma'*P2*Gamma;
Z = [-Psi'-Psi Psi'*D0-Om0 Psi'*D1-Om1 Psi'*D2-Om2 Psi'; D0'*Psi-Om0' (P+Om0)'*D0+D0'*(P+Om0)+Ups00 (P+Om0)'*D1+D0'*(Om1+Hh1'*Lam1)+Hh1'*Ups01 (P+Om0)'*D2+D0'*(Om2+Hh2'*Lam2)+Hh2'*Ups02 P+Om0';...
    D1'*Psi-Om1' D1'*(P+Om0)+(Om1+Hh1'*Lam1)'*D0+Ups01*Hh1 (Om1+Hh1'*Lam1)'*D1+D1'*(Om1+Hh1'*Lam1)+Hh1*Ups11*Hh1' (Om1+Hh1'*Lam1)'*D2+D1'*(Om2+Hh2'*Lam2)+Hh1*Ups12*Hh2' Lam1*Hh1+Om1';...
    D2'*Psi-Om2' D2'*(P+Om0)+(Om2+Hh2'*Lam2)'*D0+Ups02*Hh2 D2'*(Om1+Hh1'*Lam1)+(Om2+Hh2'*Lam2)'*D1+Hh2*Ups12*Hh1' (Om2+Hh2'*Lam2)'*D2+D2'*(Om2+Hh2'*Lam2)+Hh2*Ups22*Hh2' Lam2*Hh2+Om2';...
    Psi P+Om0 Hh1'*Lam1+Om1 Hh2'*Lam2+Om2 -Phi];
LMI3 = [Z <= 0];
% Solution of LMIs
solvesdp(LMI1 + LMI2 + LMI3);
% Verification of LMIs
P1 = double(P1); P2 = double(P2); Lam1 = double(Lam1); Lam2 = double(Lam2); Phi = double(Phi); Psi = double(Psi); Om0 = double(Om0); Om1 = double(Om1); Om2 = double(Om2);
Ups00 = double(Ups00); Ups01 = double(Ups01); Ups02 = double(Ups02); Ups12 = double(Ups12); Ups11 = double(Ups11); Ups22 = double(Ups22); 
P = P1+Gamma'*P2*Gamma;
Z = [-Psi'-Psi Psi'*D0-Om0 Psi'*D1-Om1 Psi'*D2-Om2 Psi'; D0'*Psi-Om0' (P+Om0)'*D0+D0'*(P+Om0)+Ups00 (P+Om0)'*D1+D0'*(Om1+Hh1'*Lam1)+Hh1'*Ups01 (P+Om0)'*D2+D0'*(Om2+Hh2'*Lam2)+Hh2'*Ups02 P+Om0';...
    D1'*Psi-Om1' D1'*(P+Om0)+(Om1+Hh1'*Lam1)'*D0+Ups01*Hh1 (Om1+Hh1'*Lam1)'*D1+D1'*(Om1+Hh1'*Lam1)+Hh1*Ups11*Hh1' (Om1+Hh1'*Lam1)'*D2+D1'*(Om2+Hh2'*Lam2)+Hh1*Ups12*Hh2' Lam1*Hh1+Om1';...
    D2'*Psi-Om2' D2'*(P+Om0)+(Om2+Hh2'*Lam2)'*D0+Ups02*Hh2 D2'*(Om1+Hh1'*Lam1)+(Om2+Hh2'*Lam2)'*D1+Hh2*Ups12*Hh1' (Om2+Hh2'*Lam2)'*D2+D2'*(Om2+Hh2'*Lam2)+Hh2*Ups22*Hh2' Lam2*Hh2+Om2';...
    Psi P+Om0 Hh1'*Lam1+Om1 Hh2'*Lam2+Om2 -Phi];
disp(['The min eigenvalues of P = ' num2str(min(eig(P)))]);
disp(['The min eigenvalues of Ups00+Ups11+Ups22 = ' num2str(min(eig(Ups00+Ups11+Ups22)))]);
disp(['The max eigenvalues of Z = ' num2str(max(eig(Z)))]);
disp(' ');

disp(' ');
disp('***************************************************************************'); % Simulation of RNN and the interval observer
disp('Simulation')
close all;
Nn = 1; INT = rand(n,Nn); Xm = 0.9*min(INT,[],2); XM = 1.1*max(INT,[],2);
M = 10e3; h = 1e-3; t = (1:M)'*h;
x = zeros(n,M); xm = zeros(n,M); xM = zeros(n,M);
figure('Name','Results of simulation');
for i = 1:Nn
    x(:,1) = INT(:,i); xm(:,1) = Xm; xM(:,1) = XM;
    for j = 2:M
        u = 5*cos((1:n)'*t(j)/4).^2;
        x(:,j) = x(:,j-1)+h*(u+d.*sin((1:n)'*t(j)/2).^2+A0*x(:,j-1)+A1*sign(H1*x(:,j-1))+A2*tansig(H2*x(:,j-1)));
        y = C0*x(:,j);
        xm(:,j) = xm(:,j-1)+h*(u+dm+(A0-L0*C0)*xm(:,j-1)+(A1-L1*C1)*sign(H1*xm(:,j-1))+(A2-L2*C2)*tansig(H2*xm(:,j-1))+L0*y+L1*sign(H1*y)+L2*tansig(H2*y));
        xM(:,j) = xM(:,j-1)+h*(u+dM+(A0-L0*C0)*xM(:,j-1)+(A1-L1*C1)*sign(H1*xM(:,j-1))+(A2-L2*C2)*tansig(H2*xM(:,j-1))+L0*y+L1*sign(H1*y)+L2*tansig(H2*y));
    end
    for k = 1:n
        pl = plot(t,x(k,:),t,xm(k,:),':',t,xM(k,:),'--'); hold on
        pl(1).Color = [k/n 0 k/n]; pl(2).Color = [k/n 0 k/n]; pl(3).Color = [k/n 0 k/n];
    end
end
disp(' ');
