%% Tunable Gain With One Scheduling Variable
% Create a scalar gain K that varies as a quadratic function of a single scheduling variable, t:
% K(t) = K_{0} + K_{1}t + K_{2}t^{2}.
% This gain surface can represent a gain that varies with time. The coefficients K_{0}, K_{1}, and K_{2} are the tunable parameters of this time-varying gain.
% To represent the tunable gain surface K(t) in MATLAB®, first choose a vector of t values in the range of interest for your problem. Then, obtain the values of each basis function in the expansion of K(t), at those t values. For this example, suppose that t varies from 0 to 40.

t = 0:5:40;
F1 = t;
F2 = t.^2;

%
% Create a tunable model of the gain surface K(t), sampled at the t values.

K = gainsurf('K',1,F1,F2)

% K is an array of generalized matrices. Each element in K describes K(t) for a particular value of t, and depends on the tunable coefficients K_0, K_1, and K_2. For example, the first element, K(:,:,1), is K(0) = K_{0} + K_{1}*0 + K_{2}*0^{2} = K_{0}. The second element, K(:,:,2), is K(5) = K_{0} + K_{1}*5 + K_{2}*5^{2}, and so on.
%
% Associate the independent variable values with the corresponding values of K.

K.SamplingGrid = struct('time',t);

% The SamplingGrid property keeps track of the scheduling variable values associated with each entry in K. This association is convenient for tracing results back to independent variable values. For instance, you can use view(K) to inspect the tuned values of the gain surface after tuning. When you do so, view takes the axis range and labels from the entries in SamplingGrid. For this example, instead of tuning, manually set the values of the tunable blocks to non-zero values. View the resulting gain as a function of time.
%

values = struct('K_0',1,'K_1',-1,'K_2',0.1);
view(setBlockValue(K,values))

% You can use K as a tunable gain to design a gain-scheduled controller. Use systune to tune the coefficients K_{0}, K_{1}, and K_{2} at the sample times t = 0,5,...,40.
% Copyright 2015 The MathWorks, Inc.