%% Get Coefficient Values from Control System Model
% Extract current coefficient values from a generalized model of a control system that depends on a tunable gain surface.
% For this example, create a control system model and extract the initial values of its coefficients. Generally, gainsurfdata is useful for extracting tuned coefficient values after control system tuning with systune.
% Create an array of plant models in which each plant is sampled at a different value of a scheduling parameter, alpha.

alpha = (0:10:100)';
G = zpk(zeros(1,1,11));
for ii = 1:11
	G(:,:,ii) = zpk([],-1+0.01*alpha(ii),1);
end

%
% Create a tunable PI controller with gains that depend the same scheduling parameter.

F1 = alpha;
F2 = alpha.^2;
Kp = gainsurf('Kp',1,F1,F2);
Ki = gainsurf('Ki',0.1,F1,F2);
C = pid(Kp,Ki);

%
% Combine the plant model with the tunable controller to build a closed-loop control system model array.

M = feedback(G*C,1)

% This array of tunable closed-loop models, M, depends on the coefficients that parametrize the PI gains in terms of the scheduling variable, alpha.
%
% Extract the initial values of the tunable coefficients.

[Kp0,Kp1,Kp2] = gainsurfdata(M,'Kp');
[Ki0,Ki1,Ki2] = gainsurfdata(M,'Ki');

% M depends on the gain surfaces that are assigned the names Kp and Ki. Therefore, gainsurfdata finds and returns the initial values of the coefficients associated with those gain surfaces.
% Similarly, use gainsurfdata to extract the tuned values of the coefficients after using systune to tune M against a set of design requirements.
% Copyright 2015 The MathWorks, Inc.