%% Set Up for Gain Scheduling by Linearizing at Design Points
% This example shows how to linearize a plant model at a set of design
% points for tuning of a gain-scheduled controller.  The example then uses
% the resulting linearized models to configure an |slTuner| interface for
% tuning the gain schedule.
%%
% Open the |rct_CSTR| model. 
mdl = 'rct_CSTR';
open_system(mdl)

%%
% In this model, the |Concentration controller| and |Temperature
% controller| both depend on the output concentration |Cr|. To set up this
% gain-scheduled system for tuning, you linearize the plant at a set of steady-state
% operating points that correspond to different values of the scheduling
% parameter |Cr|. Sometimes, it is convenient to use a separate model of the plant
% for trimming and linearization under various operating conditions.  For
% example, in this case, the most straightforward way to obtain these
% linearizations is to use a separate open-loop model of the plant,
% |rct_CSTR_OL|.
mdl_OL = 'rct_CSTR_OL';
open_system(mdl_OL)

%% Trim Plant at Design Points
% Suppose that you want to control this plant at a range of |Cr| values
% from |4| to |8|. Trim the model to find steady-state operating points
% for a set of values in this range. These values are the design points for
% tuning.
Cr = (4:8)';        % concentrations
for k=1:length(Cr)
   opspec = operspec(mdl_OL);
   % Set desired residual concentration
   opspec.Outputs(1).y = Cr(k);
   opspec.Outputs(1).Known = true;
   % Compute equilibrium condition
   [op(k),report(k)] = findop(mdl_OL,opspec,findopOptions('DisplayReport','off'));
end

%%
% |op| is an array of steady-state operating points. For more information
% about steady-state operating points, see <docid:slcontrol_ug.buvw_gr About Operating Points>. 

%% Linearize at Design Points 
% Linearizing the plant model
% using |op| returns an array of LTI models, each
% linearized at the corresponding design point.
G = linearize(mdl_OL,'rct_CSTR_OL/CSTR',op);

%% Create |slTuner| Interface with Block Substitution
% To tune the control system |rct_CSTR|, create an |slTuner| interface
% that linearizes the system at those design points. Use block substitution
% to replace the plant in |rct_CSTR| with the linearized plant-model
% array |G|. 
blocksub.Name = 'rct_CSTR/CSTR';
blocksub.Value = G;
tunedblocks = {'Kp','Ki'}; 
ST0 = slTuner(mdl,tunedblocks,blocksub);

%%
% For this example, only the PI coefficients in the |Concentration
% controller| are designated as tuned blocks. In general, however,
% |tunedblocks| lists all the blocks to tune.
%
% For more information about using block substitution to configure an
% |slTuner| interface for gain-scheduled controller tuning, see
% <docid:control_ug.mw_ea1e7c85-3136-48ff-9e9d-4e9ac0ed023e Multiple Design Points in slTuner Interface>.
%
% For another example that illustrates using trimming and linearization
% to generate a family of linear models for gain-scheduled controller
% tuning, see <docid:control_ug.mw_a33ce821-9c3c-4c4c-a514-d1c51c766a3f Trimming and Linearization of the HL-20 Airframe>.



%% 
% Copyright 2012 The MathWorks, Inc.