%% Compute Gap Metric and Stability Margin
% Consider a plant and a stabilizing controller.

P1 = tf([1 2],[1 5 10]);
C = tf(4.4,[1 0]);

%
% Compute the stability margin for this plant and controller.

b1 = ncfmargin(P1,C)

%
% Next, compute the gap between P1 and the perturbed plant, P2.

P2 = tf([1 1],[1 3 10]);
[gap,nugap] = gapmetric(P1,P2)

%
% Because the stability margin b1 = b(P1,C) is greater than the gap between the two plants, C also stabilizes P2. As discussed in Gap Metrics and Stability Margins, the stability margin b2 = b(P2,C) satisfies the inequality asin(b(P2,C)) ≥ asin(b1)-asin(gap). Confirm this result.

b2 = ncfmargin(P2,C);
[asin(b2) asin(b1)-asin(gap)]

%
% Copyright 2018 The MathWorks, Inc.