📄 demo.m
字号:
% DEMO - Demo file for identification by structured total least squares.clear allclose allecho on% --- Zero lag corresponds to linear static model ---l = 0; % lag m = 2; % # inputsp = 3; % # outputsT = 20; % length of the data sequence% Generate datan = l*p; % ordersys0 = drss_(n,p,m); % true systemu0 = randn(T,m); % true input y0 = lsim(sys0,u0); % true outputw0 = [u0 y0]; % true data w = w0; % the given data is exact% Identify the system[sysh,info,wh,xini] = stlsident(w,m,l);info% Verify the resultserr_sys = norm(sys0 - sysh)err_w = norm(w0 - wh)pause% --- Exact system identification ---l = 2; % lagm = 2; % # inputsp = 2; % # outputsT = 100; % length of the data sequence% Generate datan = l*p; % ordersys0 = drss_(n,p,m); % true systemu0 = randn(T,m); % true input y0 = lsim(sys0,u0); % true outputw0 = [u0 y0]; % true data w = w0; % the given data is exact% Identify the system[sysh,info,wh,xini] = stlsident(w,m,l);info% Verify the resultserr_sys = norm(sys0 - sysh)err_w = norm(w0 - wh, 'fro')pause% --- Errors-in-variables identification --- % Perturb the "true" data w0 with noisew = w0 + 0.5 * randn(T,m+p);% Identify the system[sysh,info,wh,xini] = stlsident(w,m,l);info% Verify the resultserr_data = norm(w0 - w ,'fro') / norm(w0,'fro')err_appr = norm(w0 - wh,'fro') / norm(w0,'fro')% Plot "true", "noisy", and approximating input and outputecho offt = 1:20;for i = 1:m+p figure plot(t,w0(t,i),'-r','linewidth',2), hold on plot(t,w(t,i) ,':k','linewidth',2) plot(t,wh(t,i),'--b','linewidth',2) set(gca,'fontsize',15) xlabel('time'), ylabel(sprintf('w_%d',i)), title('EIV identification') legend('true','data','appr')endecho onpause% --- Output error identification ---% Perturb the "true" output y0 with noisew = w0 + 0.75 * [zeros(T,m) randn(T,p)];% Identify the systemopt.exct = 1:m; % take into account that the input is exact[sysh,info,wh,xini] = stlsident(w,m,l,opt);info% Verify the resultserror_data = norm(w0 - w ,'fro') / norm(w0,'fro')error_appr = norm(w0 - wh,'fro') / norm(w0,'fro')% Plot "true", "noisy", and approximating output echo offt = 1:20;for i = 1:p figure plot(t,w0(t,m+i),'-r','linewidth',2), hold on plot(t,w(t,m+i) ,':k','linewidth',2) plot(t,wh(t,m+i),'--b','linewidth',2) set(gca,'fontsize',15) xlabel('time'), ylabel(sprintf('y_%d',i)), title('OE identification') legend('true','data','appr')endecho onpause% --- #inputs = 0 corrsponds to autonomous system identification ---% Generate dataT = 20; % Length of the data sequencexini0 = randn(n,1);y0 = initial(sys0,xini0,T);w0 = y0;% Perturb the "true" data w0 with noisew = w0 + 0.25 * randn(T,p); % data for the identification% Approximate the given data by a system with complexity (0,l)[sysh,info,wh,xinih] = stlsident(w,0,l);info% Verify the resultserror_data = norm(w0 - w ,'fro') / norm(w0,'fro')error_appr = norm(w0 - wh,'fro') / norm(w0,'fro')% Plot "true", "noisy", and approximating sequencesecho offt = 1:20;for i = 1:p figure plot(t,w0(t,i),'-r','linewidth',2), hold on plot(t,w(t,i) ,':k','linewidth',2) plot(t,wh(t,i),'--b','linewidth',2) set(gca,'fontsize',15) xlabel('time'), ylabel(sprintf('y_%d',i)), title('Autonomous system identification') legend('true','data','appr.')endecho onpause% --- Identification from step response observations ---% Generate dataT = 150; % length of the data sequencey0 = step(sys0,T);% Perturb the "true" data y0 with noisey = y0 + 0.25 * randn(T,p,m);% Construct the inputsu0 = zeros(T,m,m);for i = 1:m u0(:,i,i) = ones(T,1);end% The input/output data isw = [u0 y];% Proceed it with l zeros for the zero initial conditionswext = [zeros(l,m+p,m); w];% Identify the system from the extended I/0 dataopt.exct = [1:m]; % take into account that the input is exact[sysh,info,whext,xini] = stlsident(wext,m,l,opt);info% Remove the trailing part, setting the initial conditionswh = whext(l+1:end,:,:);% Verify the resultsw0 = [u0 y0];error_data = norm(w0(:) - w(:) ) / norm(w0(:))error_appr = norm(w0(:) - wh(:)) / norm(w0(:))% Plot "true", given, and approximating data sequences echo offt = 1:20;for i = 1:m for j = 1:p figure plot(t,w0(t,m+j,i),'-r','linewidth',2), hold on plot(t,w(t,m+j,i) ,':k','linewidth',2) plot(t,wh(t,m+j,i),'--b','linewidth',2) set(gca,'fontsize',15) title('Identification from step response') xlabel('time'), ylabel(sprintf('s_{%d,%d}',i,j)) legend('true','data','appr') endendecho onpause% --- Identification from impulse response observations ---% --- ( a.k.a. approximate realization ) ---% Generate dataT = 50; % length of the data sequencey0 = impulse(sys0,T);% Perturb the "true" data y0 with noisey = y0 + 0.25 * randn(T,p,m);% Version 1: solution by input/output identification.% Construct the inputsu0 = zeros(T,m,m); u0(1,:,:) = eye(m);% The given input/output data isw = [u0 y];% Proceed it with l zeros for the zero initial conditionswext = [zeros(l,m+p,m); w];% Identify the system from the extended I/0 dataopt.exct = [1:m]; % take into account that the input is exact[sysh1,info1,whext,xini1] = stlsident(wext,m,l,opt);info1 = info1% Remove the trailing part, setting the initial conditionswh1 = whext(l+1:end,:,:);% Version 2: solution by output-only identification.% Identify an autonomous system[sysh2,info2,yh2,xini2] = stlsident(y(2:end,:,:),0,l);yh2 = [y(1,:,:); yh2];info2 = info2% Recover the I/O systemsysh2 = ss(sysh2.a,xini2,sysh2.c,reshape(yh2(1,:,:),p,m),-1);wh2 = [u0 yh2];% Verify the resultsw0 = [u0 y0];error_data = norm(w0(:)-w(:))/norm(w0(:))error_appr1 = norm(w0(:)-wh1(:))/norm(w0(:))error_appr2 = norm(w0(:)-wh2(:))/norm(w0(:))% Plot "true", given, and approximating data sequences echo offt = 1:20;for i = 1:m for j = 1:p figure plot(t,w0(t,m+j,i),'-r','linewidth',2), hold on plot(t,w(t,m+j,i) ,':k','linewidth',2) plot(t,wh1(t,m+j,i),'--b','linewidth',2) plot(t,wh2(t,m+j,i),'-.b','linewidth',2) set(gca,'fontsize',15) title('Identification from impulse response') xlabel('time') ylabel(sprintf('h_{%d,%d}',i,j)) legend('true','data','appr. 1','appr. 2') endendecho onpause% --- Finite-time l2 model reduction ---l = 10; % lag of the original (high order) systemlr = 1; % lag of the reduced systemm = 2; % # inputsp = 2; % # outputs% High order systemsys = drss_(p*l,p,m);% Simulate impulse responseh = impulse(sys); % determine automatically TT = size(h,1); % time horizon T% Find reduced model[sysr,info,hr,xini] = stlsident(h(2:end,:,:),0,lr);hr = [h(1,:,:); hr];sysr = ss(sysr.a,xini,sysr.c,reshape(hr(1,:,:),p,m),-1);info% Relative Hinf and H2 norms of the error systemh2_err = norm(sys-sysr,2) / norm(sys,2)hi_err = norm(sys-sysr,'inf') / norm(sys,'inf')% Plot the impulse responses of the original and appr. systemsecho offt = 1:T;for i = 1:m for j = 1:p figure plot(t,h(t,j,i),'-r','linewidth',2), hold on plot(t,hr(t,j,i),'--b','linewidth',2), hold on set(gca,'fontsize',15) title('Finite-time l2 model reduction') xlabel('time') ylabel(sprintf('h_{%d,%d}',i,j)) legend('given','appr.','Location','Best') endendecho onpause% --- The misfit is generically independent of the input/output partitioning ---l = 1; % lag m = 2; % # inputsp = 1; % # outputsT = 20; % time horizon% Generate datasys0 = drss(p*l,p,m);u0 = randn(T,m);y0 = lsim(sys0,u0);w0 = [u0 y0];% Identify the system from the original exact dataw1 = w0;[sysh1,info1,wh1,xini1] = stlsident(w1,m,l);info1% Identify the system from exact data with randomly permuted variablesperm = randperm(m+p)w2 = w1(:,perm);[sysh2,info2,wh2,xini2] = stlsident(w2,m,l);info2% Compare the resultsnorm(wh1(:,perm) - wh2)% --- End of demo ---
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -