%% Model Reduction With Frequency-Dependent Error Profile
% Use a weighting function to control the frequency dependence of the error between the original and reduced models.
% For this example, load a 48-state SISO model and reduce it to 6th order.

load('balancmrData.mat','bplant')
bplant6 = balancmr(bplant,6);
bode(bplant,bplant6,bplant-bplant6,logspace(0,2,200))
legend('Original','Reduced','Error')

%
% The error is fairly uniformly distributed in frequency. Create a weighting function that allows for a larger error at frequencies below 100 rad/s. In this case, use a biproper function that has unit gain at low frequency, but drops to -40 dB at higher frequencies.

W = tf(0.01*[1 1.4e2 1e4],[1 14 100]); 
bodemag(W,bplant)

%
% Reduce the model to 6th order again, using this weighting function. Because bplant is a SISO model, you can use the function as either input or output weight.

bplant6W = balancmr(bplant,6,'Weights',{W,1});
bode(bplant,bplant6W,bplant-bplant6W,logspace(0,2,200))
legend('Original','Reduced w/Weight','Error')

% The error is now larger at low frequencies, with a correspondingly better match at high frequencies between the original and reduced models.
% Copyright 2012 The MathWorks, Inc.