%% Compute Gap Metrics for Stable and Unstable Plant Models
% Create two plant models. One plant, P1, is an unstable first-order system with transfer function 1/(s–0.001). The other plant, P2, is stable, with transfer function 1/(s +0.001).

P1 = tf(1,[1 -0.001]); 
P2 = tf(1,[1 0.001]);

%
% Despite the fact that one plant is unstable and the other is stable, these plants are close as measured by the gap and nugap metrics.

[gap,nugap] = gapmetric(P1,P2)

%
% The gap is very small compared to 1. Thus a controller that yields a stable closed-loop system with P2 also tends to stabilize P1. For instance, the feedback controller C = 1 stabilizes both plants and renders nearly identical closed-loop gains. To see this, examine the sensitivity functions of the two closed-loop systems.

C = 1; 
H1 = loopsens(P1,C); 
H2 = loopsens(P2,C); 
subplot(2,2,1); bode(H1.Si,'-',H2.Si,'r--'); 
subplot(2,2,2); bode(H1.Ti,'-',H2.Ti,'r--'); 
subplot(2,2,3); bode(H1.PSi,'-',H2.PSi,'r--'); 
subplot(2,2,4); bode(H1.CSo,'-',H2.CSo,'r--');

%
% Next, consider two stable plant models that differ by a first-order system. One plant, P3, is the transfer function 50/(s+50), and the other plant, P4, is the transfer function [50/(s+50)]*8/(s+8).

P3 = tf(50,[1 50]); 
P4 = tf(8,[1 8])*P3;
figure
bode(P3,P4)

%
% Although the two systems have similar high-frequency dynamics and the same unity gain at low frequency, by the gap and nugap metrics, the plants are fairly far apart.

[gap,nugap] = gapmetric(P3,P4)

%
% Copyright 2015 The MathWorks, Inc.