%% Scale All Uncertain Elements in a Model
% Consider the uncertain control system of the example "Robust Performance of Closed-Loop System" on the robgain reference page. That example examines the sensitivity of the closed-loop response at the plant output to disturbances at the plant input.

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);
S = feedback(1,G*C)

% Suppose that you do not want the peak gain of this sensitivity function to exceed 1.5. Use robgain to find out how much of the modeled uncertainty the system can tolerate while the peak gain remains below 1.5.

perfmarg = robgain(S,1.5)

%
% With that performance requirement, the system can only tolerate about 78% of the modeled uncertainty. Scale all the uncertain elements in S to create a model of the closed-loop system with the maximum level of uncertainty that meets the performance requirement.

factor = perfmarg.LowerBound;
S_scaled = uscale(S,factor)

% The display shows how the uncertain elements in S_scaled have changed: the peak gain of the ultidyn element delta is reduced from 1 to 0.78, and the range of variation of the uncertain real parameter k is reduced from ±40% to ±31.3%.
%
% Copyright 2019 The MathWorks, Inc.