ic = [1; 0; 0; 0; 1 ]; % ham f(x) la ham cua bien dau vao
lc = [0 0 0 1 0; 1 0 0 0 0 ; 0 1 0 0 1; 0 0 1 0 0; 0 0 0 1 0 ]; % O day la mang hoi quy
oc = [0 0 0 1 0 ]; CN = {bc,ic,lc,oc}; % 4.Delays
LD1 = [5 4 1 ]; % delays at LW(5,4) tuong ung voi tre ID1 cua pnet LD2 = [1 4 1 ]; % delays at LW(1,4) tuong ung voi tre ID2 cua pnet ID1 = [5 1 0 1]; % delays at IW(5,1)
LD = {LD1,LD2};ID = {ID1}; ID = {ID1}; ID={};
% 5.Transfer functions
TF = {'radbas','purelin','purelin','purelin','purelin'};% 6.Create NN for PLANT MODEL % 6.Create NN for PLANT MODEL
pnetc = newnet(PTR,ID,LD,LS,CN,TF);
pnetc.inputWeights{1,1}.weightFcn = 'fulldist'; %Ham mo hoa dau vao pnetc.layerWeights{1,4}.weightFcn = 'dist'; %Ham mo hoa dau ra
pnetc.layers{1}.netInputFcn = 'netprod'; %Tich cac thanh phan lien thuoc
voi khong gian con
pnetc.layerWeights{3,2}.learn = 0; pnetc.layerWeights{3,5}.learn = 0; %khoi tao lop 4- lop tong
pnetc.lw{4,3}=ones(1,nrule);
pnetc.layerWeights{4,3}.learn = 0; %copy tham so tu pnet1 sang pnetc pnetc.iw{1,1}=pnet1.iw{1,1}; pnetc.b{1} =pnet1.b{1};
pnetc.lw{1,4}=pnet1.iw{1,2}; % chuyen tu truyen thang sang hoi quy pnetc.lw{2,1}=pnet1.lw{2,1};
pnetc.iw{5,1}=pnet1.iw{5,1}; pnetc.b{5} =pnet1.b{5}; pnetc.lw{5,4}=pnet1.iw{5,2}; pnet_full=pnetc;
File chạy cho chương trình nhận dạng
[P1,T,t,Epoch]=taodulieu_training;
[pnet1,nrule,tr]=huanluyenmang3(P1,T,Epoch); [P1t,Tt,tt]=taodulieu_testing;
P1nt = con2seq(P1t); % Luu y tin hieu vao phai de dang noi tiep thi mang noron moi tinh toan dung
T1nt = con2seq(Tt); [pnet2,tr1]=train(pnet1,P1nt,T1nt); pause; [P2t,T2t,tt]=taodulieu_testing; P2nt = con2seq(P2t); T2nt = con2seq(T2t); [pnet3,tr2]=train(pnet2,P2nt,T2nt); pause; [P3t,T3t,tt]=taodulieu_testing; P3nt = con2seq(P3t); T3nt = con2seq(T3t); [pnet4,tr3]=train(pnet3,P3nt,T3nt); [P4t,T4t,tt]=taodulieu_testing; P4nt = con2seq(P4t); Y=sim(pnet4,P4nt); Y = cell2mat(Y); plot(t,T4t-Y,tt,T4t);
Vehamlienthuoc2(pnet1,nrule,tr); P1nt = con2seq(P1); [pnet_full]=chuyennoitiepsanghoiquy2(pnet1,nrule); Y=sim(pnet_full,P1nt); Y = cell2mat(Y); plot(t,T-Y,t,T); Tnt = con2seq(T); [pnet2,tr]=train(pnet_full,P1nt,Tnt); Mô hình đối tượng Hiển thị kết quả nhận dạng hàm liên thuộc các biến function Vehamlienthuoc2(pnet,nrule,tr) A=pnet.iw{1,1}; for i=1:nrule
z{i,1}=[A(i,1) A(i,3) A(i,5)]; z{i,2}=[A(i,2) A(i,4) A(i,6)]; end
m=[0:0.02:3];
% Su thay doi cua ham mu lk cua bien x1 f1ch=gmf1(m,z{1,1});
f2ch=gmf1(m,z{2,1}); f3ch=gmf1(m,z{3,1}); f4ch=gmf1(m,z{4,1});
m=[0:0.02:3];
% Su thay doi cua ham mu lk cua bien x2 f1ch=gmf1(m,z{1,2}); f2ch=gmf1(m,z{2,2}); f3ch=gmf1(m,z{3,2}); f4ch=gmf1(m,z{4,2}); f5ch=gmf1(m,z{5,2}); f6ch=gmf1(m,z{6,2}); figure(2); plot(m,[f1ch;f2ch;f3ch;f4ch;f5ch;f6ch]); plotperf(tr); end Hàm điều khiển GPC function [Pr,Nk,Dk]=GPC2bien(A,b,ny,nu,lamda)
%Ham cho nhieu bien dau vao
% Dieu khien du bao GPC dua tren model based predicted control page 54.55 % Dang mo hinh du bao la
% y(k+1)+A1.y(k)+..+An+1.y(k-n)=b1.Du(k)+b2.Du(k-1)+..+bn.Du(k-n+1) %A=[-1.637 0.4015];
%b=[3 2 1 3]; Ma tran b xay dung la b11.u1k+ b12.u2k+b21.u1k-1+b22.u2k-1 %ny=5; % Tam du bao %nu=3; % tam dieu khien %lamda=0.5; % trong so lamda % Xay dung ma tran CA, HA, Czb, Hzb.
[m,n]=size(A); [k,h]=size(b); if n<=ny
Am=[1 A zeros(1,ny-n)]; %Tao ma tran gia else Am=A; end if h<=2*ny bm=[b zeros(1,2*ny-h)]; else bm=b; end Am2=[A zeros(1,ny)]; bm2=[b zeros(1,2*ny)];
for i=1:ny % theo hang
for j=1:ny % theo cot
if j<=i CA(i,j)=Am(i-j+1); Czb(i,2*j-1)=bm(2*(i-j)+1); Czb(i,2*j) =bm(2*(i-j)+2); else CA(i,j)=0; Czb(i,2*j-1)=0;
for j=1:n % theo cot HA(i,j)=Am2(i+j-1);
end end
for i=1:ny % theo hang
for j=1:(h-2) % theo cot
Hzb(i,j)=bm2(2*i+j); end end % Tinh ma tran H, P, Q CA1=inv(CA); H= CA1*Czb; P= CA1*Hzb; Q= -CA1*HA;
% Chinh lai ma tran H cho phu hop voi tam dieu khien page 76. if nu<ny
H=H(:,1:2*nu); end
%Tinh ma tran Pr, Nk,Dk page 82 E1=[eye(2,2) zeros(2,2*(nu-1))]; M=E1*inv(H'*H+lamda*eye(2*nu)); Pr=M*H'; Nk=M*H'*Q; Dk=M*H'*P; Hàm chạy chương trình mô phỏng GPC
% Chuong trinh mo phong thanh cong
% luu y gia tri dat chi hieu qua tu 0.4 den 1.2
% De chay chuong trinh truoc tien phai chay ket qua tungGPC_ngon.mat hoac % ketqua8luatdep.mat
ny=5; nu=3; lamda=2; r1=1;
ts=0.05; %Thoi gian trich mau Time=10; % Thoi gian mo phong tung gia tri dat
Giatridat=[ 1 0.8 1 0.6 0.4]; [k,sogiatri]=size(Giatridat); %[P1,T,t,Epoch]=taodulieu_training; %[P1t,Tt,t]=taodulieu_testing;
%[pnet_full]=huanluyenmang(P1,T,Epoch); % Lay bo du lieu tu he mo-noron pnet
B=pnet_full.iw{5,1}; %chua tham so b*u 6-6-6 tham so dau la u(k),u(k-1),u(k-2)
a=pnet_full.lw{5,4}; %chua tham so a*y(k-1)
Y(k-1)
%Do tham so b chua 6 noron ma duoc nhan len 3 lan ve dang sai phan voi u [e,f]=size(B); b=[]; for i=0:(f/6-1) bi{i+1}=B(:,6*i+[1,2])+B(:,6*i+[3,4])+B(:,6*i+[5,6]); b=[b bi{i+1}]; end [so_kgcon,n]=size(a); am=[a zeros(so_kgcon,1)]; am1=[(-1)*ones(so_kgcon,1) a]; Am1=am-am1;
Am=-Am1; % Dua ve dang chuan CARIMA theo model predictive control
%Am chua so hang tuong ung voi so khong gian con % Lay bo tham so mo hinh con tu mo hinh khong gian con
% Luu y so phan tu qua khu cu delta u it nhat la 2 neu khong se k tinh duoc
for i=1:so_kgcon % tuong ung voi so noron
[Pr{i},Nk{i},Dk{i}]=GPC2bien(Am(i,:),b(i,:),ny,nu,lamda); end
% Dung dãy Ny vector tín hieu cho r tính tu thoi diem t den t+ Ny R=[];
for k=1:ny R=[R; 1] ; end
% Xay dung cac vecto trang thai ban dau
[m,n]=size(Am(1,:)); %Do coi moi khong gian co so he so A,b nhu nhau
[k,h]=size(b(1,:));
if 2*n<=h batdau=h/2; else batdau=n; end % do b co 2 tin hieu nen tham so gap doi
if batdau<3 batdau=3; end % batdau >=3 thi no moi khong ra nhieu he so
save_U_global=zeros(2,batdau); % vector tin hieu dieu khien u gom 2 hang u1 va u2 o khong gian con i
% Do neu tin hieu kich thich U=[0;0] thi he thong van co dau ra Y<>0 nen t=(0:ts:ts*(batdau-1))';
[t,x,Y]=sim('myplant_tung1',t,[],[t,save_U_global']);
save_y=Y'; % vector trang thai ban dau cua thoi diem y(k),y(k-1),...
for i=1:so_kgcon
U_local{i}=[0;0];
save_deltaU{i}=zeros(2,batdau-1); % vector tin hieu sai lech delta U cung gom 2 hang
end
hientai=batdau;
% Dua vao vong dieu khien GPC
y_pas=y_pas';
for i=1:so_kgcon % Tinh gia tri U_local
del_pas_U{i}=[]; % gia tri delta U o thoi diem qua khu tai k-1 va k-2,...
for j=2:(h/2)
% Dung vector delta U thanh cot de tinh tu 2 hang delta U del_pas_U{i}=[del_pas_U{i} save_deltaU{i}(1,hientai-j) save_deltaU{i}(2,hientai-j)];
end
del_pas_U1{i}=del_pas_U{i}';
delta_now_U{i}= Pr{i}*R1- Nk{i}*y_pas-Dk{i}*del_pas_U1{i}; % Tinh gia tri delta U hien tai
save_deltaU{i}=[save_deltaU{i} delta_now_U{i}]; %Luu giu day gia tri delta U
U_local{i}=U_local{i}+delta_now_U{i}; end
%Tinh gia tri U_global
x1=save_U_global(1,hientai-1); x2=save_U_global(2,hientai-1); y=save_y(hientai-1); Muy=[]; for i=1:so_kgcon Muy_x1=gmf1(x1,MX1(i,:)); Muy_x2=gmf1(x2,MX2(i,:)); Muy_y =gmf1(y,MY(i,:));
Muy=[Muy; Muy_x1* Muy_x2* Muy_y]; end
%Chuan hoa gia tri mo hoa nho bien vk TG=Muy.*v;
MUY=TG/sum(TG,1);
% Gia tri tin hieu dieu khien tong quat se la tong cua muy* Ucon U_global=[0;0]; for i=1:so_kgcon U_global=U_global+MUY(i)*U_local{i}; end save_U_global=[save_U_global U_global]; t=(0:ts:(ts*(hientai-1)))'; [t,x,Y]=sim('myplant_tung1',t,[],[t,save_U_global']); save_y=Y'; end
Ref=[ones(1,200) 0.8*ones(1,200) 0.6*ones(1,200) ones(1,200)]; plot(t,save_y);
grid on; end