%% Constrain Closed-Loop Dynamics of Specified Loop of System to Tune
% Create a requirement that constrains the inner loop of the following control system to be stable and free of fast dynamics. Specify that the constraint is evaluated with the outer loop open.
%
% Create a model of the system. To do so, specify and connect the numeric plant models, G1 and G2, and the tunable controllers C1 and C2. Also, create and connect the AnalysisPoint blocks, AP1 and AP2, which mark points of interest for analysis and tuning.

G1 = tf(10,[1 10]);
G2 = tf([1 2],[1 0.2 10]);
C1 = tunablePID('C','pi');
C2 = tunableGain('G',1);
AP1 = AnalysisPoint('AP1');
AP2 = AnalysisPoint('AP2');
T = feedback(G1*feedback(G2*C2,AP2)*C1,AP1);

%
% Create a tuning requirement that constrains the dynamics of the closed-loop poles. Restrict the poles of the inner loop to the region \mathrm{Re}(s) < -0.1, |s| < 30.

Req = TuningGoal.Poles(0.1,0,30);

% Setting the minimum damping to zero imposes no constraint on the damping constants for the poles.
%
% Specify that the constraint on the tuned system poles is applied with the outer loop open.

Req.Openings = 'AP1';

% When you tune T using this requirement, the constraint applies to the poles of the entire control system evaluated with the loop open at 'AP1'. In other words, the poles of the inner loop plus the poles of C1 and G1 are all considered.
% After you tune T, you can use viewGoal to validate the tuned control system against the requirement.
% Copyright 2012 The MathWorks, Inc.