[转载]【建模与辨识程序】增广的最小二乘辨识程序

%FLch3ELSeg4
clear
L=60;%

y1=1;y2=1;y3=1;y4=0;%

for i=1;
x1=xor(y3,y4);%

x2=y1;%

x3=y2;%

x4=y3;%

y(i)=y4;%

if y(i)>0.5,u(i)=-1;%M

else u(i)=1;%M

end
y1=x1;y2=x2;y3=x3;y4=x4;%

end
figure(1);%

subplot(2,1,1); %

stem(u),grid on%

v=randn(1,60); %

subplot(2,1,2); %

plot(v),grid on;%

R=corrcoef(u,v);%

r=R(1,2);%

u%

v%

z=zeros(7,60);zs=zeros(7,60);zm=zeros(7,60);zmd=zeros(7,60);%

z(2)=0;z(1)=0;zs(2)=0;zs(1)=0;zm(2)=0;zm(1)=0;zmd(2)=0;zmd(1)=0;%

%

c0=[0.001 0.001 0.001 0.001 0.001 0.001 0.001]';%

p0=10^6*eye(7,7);%

E=5.0e-15;%

c=[c0,zeros(7,59)];%

e=zeros(7,60);%

for k=3:60; %

z(k)=1.5*z(k-1)-0.7*z(k-2)+u(k-1)+0.5*u(k-2)+v(k)-v(k-1)+0.2*v(k-2);%

h1=[-z(k-1),-z(k-2),u(k-1),u(k-2),v(k),v(k-1),v(k-2)]';%

x=h1'*p0*h1+1; x1=inv(x); k1=p0*h1*x1; %K
d1=z(k)-h1'*c0; c1=c0+k1*d1;%

zs(k)=1.5*z(k-1)-0.7*z(k-2)+u(k-1)+0.5*u(k-2);%

zm(k)=[-z(k-1),-z(k-2),u(k-1),u(k-2)]*[c1(1);c1(2);c1(3);c1(4)];%

zmd(k)=h1'*c1;%

e1=c1-c0;
e2=e1./c0; %

e(:,k)=e2;
c0=c1;%

c(:,k)=c1;%

p1=p0-k1*k1'*[h1'*p0*h1+1];%find p(k)
p0=p1;%

if e2<=E break;%

end%

end%

c, e, %

z,zmd %

%

a1=c(1,; a2=c(2,; b1=c(3,; b2=c(4,;%

d1=c(5,; d2=c(6,; d3=c(7,; %

ea1=e(1,; ea2=e(2,; eb1=e(3,; eb2=e(4,; %

ed1=e(5,; ed2=e(6,; ed3=e(7,; %

figure(2);%

i=1:60;
plot(i,a1,'r',i,a2,'r:',i,b1,'b',i,b2,'b:',i,d1,'g',i,d2,'g:',i,d3,'g+')%

title('Parameter Identification with Recursive Least Squares Method')%

figure(3);%

i=1:60;
plot(i,ea1,'r',i,ea2,'r:',i,eb1,'b',i,eb2,'b:',i,ed1,'g',i,ed2,'g:',i,ed2,'r+')%

title('Identification Error')%

%response%

figure(4);%

subplot(4,1,1); %

i=1:60;
plot(i,zs(i),'r')%

subplot(4,1,2); %

i=1:60;
plot(i,z(i),'g')%

subplot(4,1,3); %

i=1:60;
plot(i,zmd(i),'b')%

subplot(4,1,4); %

i=1:60;
plot(i,zm(i),'b')%

http://blog.sciencenet.cn/blog-748007-1204781.html

全部精选博文导读

GMT+8, 2020-4-1 17:15