%% Update Controller Model with Tuned Values
% Propagate the values of tuned parameters to other Control Design Blocks.
% You can use tuning commands such as systune, looptune, or the Robust Control Toolbox™ command hinfstruct to tune blocks in a closed-loop model of a control system. If you do so, the tuned controller parameters are embedded in a generalized model. You can use setBlockValue to propagate those parameters to a controller model.
% Create a tunable model of the closed-loop response of a control system, and tune the parameters using systune.

s = tf('s');
num = 33000*(s^2 - 200*s + 90000);
den = (s + 12.5)*(s^2 + 25*s + 63000);
G = num/den;

C0 = tunablePID('C0','pi');
a = realp('a',1);
F0 = tf(a,[1 a]);
T0 = feedback(G*C0,F0);
T0.InputName = 'r';
T0.OutputName = 'y';

% T0 is a generalized model of the closed-loop control system and contains two tunable blocks:
% C0 - Tunable PID controller
% a - Real tunable parameter
%
% Create a tuning requirement for the output y to track the input r, and tune the system to meet that requirement.

Req = TuningGoal.Tracking('r','y',0.05);
[T,fSoft,~] = systune(T0,Req);

% The generalized model T contains the tuned values of C0 and a.
%
% Propagate the tuned values of the controller in T to the controller model C0.

C = setBlockValue(C0,T)

% C is still a tunablePID controller. The current PID gains in C are set to the values of the controller in T.
%
% Obtain a numeric LTI model of the tuned controller using getValue.

 CVal = getValue(C,T);

% This command returns a numerical state-space model of the tuned controller.
% Copyright 2012 The MathWorks, Inc.