%% Fit Frequency Response Data With Stable Minimum-Phase State-Space Model
% Create frequency response magnitude data from a fifth-order system.

sys = tf([1 2 2],[1 2.5 1.5])*tf(1,[1 0.1]); 
sys = sys*tf([1 3.75 3.5],[1 2.5 13]); 
omega = logspace(-1,1); 
sysg = abs(frd(sys,omega)); 
bodemag(sysg,'r');

%
% Fit the magnitude data with a minimum-phase, stable third-order system.

ord = 3; 
b1 = fitmagfrd(sysg,ord); 
b1g = frd(b1,omega); 
bodemag(sysg,'r',b1g,'k:');
legend('Data','3rd order fit');

%
% Fit the magnitude data with a third-order system constrained to lie below and above the given data.

C2.UpperBound = sysg;
C2.LowerBound = [];
b2 = fitmagfrd(sysg,ord,[],[],C2); 
b2g = frd(b2,omega); 
C3.UpperBound = [];
C3.LowerBound = sysg;
b3 = fitmagfrd(sysg,ord,[],[],C3); 
b3g = frd(b3,omega); 
bodemag(sysg,'r',b1g,'k:',b2g,'b-.',b3g,'m--') 
legend('Data','3rd order fit','3rd order fit, below data',...
       '3rd order fit, above data')

%
% Fit the magnitude data with a second-order system constrained to lie below and above the given data.

ord = 2;
C2.UpperBound = sysg;
C2.LowerBound = [];
b2 = fitmagfrd(sysg,ord,[],sysg,C2);
b2g = frd(b2,omega);
C3.UpperBound = [];
C3.LowerBound = sysg;
b3 = fitmagfrd(sysg,ord,[],sysg,C3);
b3g = frd(b3,omega);
bgp = fitfrd(genphase(sysg),ord);
bgpg = frd(bgp,omega);
bodemag(sysg,'r',b1g,'k:',b2g,'b-.',b3g,'m--',bgpg,'r--')
legend('Data','3rd order fit','2d order fit, below data',...
       '2nd order fit, above data','bgpg')

% Copyright 2015 The MathWorks, Inc.