%% Fit Uncertain Model to Array of LTI Responses
% Fit an uncertain model to an array of LTI responses. The responses might be, for example, the results of multiple runs of acquisition of frequency response data from a physical system.
% For the purpose of this example, generate the frequency response data by creating an array of LTI models and sampling the frequency response of those models.

Pnom = tf(2,[1 -2]);
p1 = Pnom*tf(1,[.06 1]); 
p2 = Pnom*tf([-.02 1],[.02 1]); 
p3 = Pnom*tf(50^2,[1 2*.1*50 50^2]); 
array = stack(1,p1,p2,p3);
Parray = frd(array,logspace(-1,3,60));

%
% The frequency response data in Parray represents three separate data acquisition experiments on the system.
% Plot relative errors between the nominal plant response and the three models in the LTI array.

relerr = (Pnom-Parray)/Pnom;
bodemag(relerr)

%
% If you use a multiplicative uncertainty model structure, the magnitude of the shaping filter should fit the maximum relative errors at each frequency.
% Try a 1st-order shaping filter to fit the maximum relative errors.

[P,Info] = ucover(Parray,Pnom,1);

%
% P is an uncertain state-space (uss) model that captures the uncertainty as a ultidyn uncertain dynamics block.

P.Uncertainty

%
% The Info structure contains other information about the fit, including the shaping filter. Plot the response to see how well the shaping filter fits the relative errors.

bodemag(relerr,'b--',Info.W1,'r',{0.1,1000});

%
% The plot shows that the filter W1 is too conservative and exceeds the maximum relative error at most frequencies. To obtain a tighter fit, rerun the function using a 4th-order filter.

[P,Info] = ucover(Parray,Pnom,4);

%
% Evaluate the fit by plotting the Bode magnitude plot.

bodemag(relerr,'b--',Info.W1,'r',{0.1,1000});

% This plot shows that for the 4th-order filter, the magnitude of W1 closely matches the minimum uncertainty amount.
% Copyright 2012 The MathWorks, Inc.