%% Robust Stability Margin as a Function of Frequency
% Consider a model of a control system containing uncertain elements.

k = ureal('k',10,'Percent',40);
delta = ultidyn('delta',[1 1]); 
G = tf(18,[1 1.8 k]) * (1 + 0.5*delta);
C = pid(2.3,3,0.38,0.001);
CL = feedback(G*C,1);

%
% By default, robstab computes only the weakest stability margin over all frequencies. To see how the stability margin varies with frequency, use the 'VaryFrequency' option of robOptions. For example, compute the stability margin of the system at frequency points between 0.1 and 10 rad/s.

opts = robOptions('VaryFrequency','on');
[stabmarg,wcu,info] = robstab(CL,{0.1,10},opts);
info

%
% robstab returns the vector of frequencies in the info output, in the Frequencies field. info.Bounds contains the upper and lower bounds on the stability margin at each frequency. Use these values to plot the frequency dependence of the stability margin.

semilogx(info.Frequency,info.Bounds)
title('Stability Margin vs. Frequency')
ylabel('Margin')
xlabel('Frequency')
legend('Lower bound','Upper bound')

%
% When you use the 'VaryFrequency' option, robstab chooses frequency points automatically. The frequencies it selects are guaranteed to include the frequency at which the stability margin is weakest (within the specified range). Display the returned frequency values to confirm that they include the critical frequency.

info.Frequency
stabmarg.CriticalFrequency

%
% Alternatively, instead of using 'VaryFrequency', you can specify particular frequencies at which to compute the robust stability margins. info.Bounds contains the margins at all specified frequencies. However, these results are not guaranteed to include the weakest margin, which might fall between specified frequency points.

w = logspace(-1,1,25); 
[stabmarg,wcu,info] = robstab(CL,w);
semilogx(w,info.Bounds)
title('Stability Margin vs. Frequency')
ylabel('Margin')
xlabel('Frequency')
legend('Lower bound','Upper bound')

%
% Copyright 2012 The MathWorks, Inc.