%% Accessing and Modifying the Model Data
% This example shows how to access or edit parameter values and metadata in LTI objects.
%% Accessing Data
% The tf, zpk, ss, and frd commands create LTI objects that store model data in a single MATLAB® variable. This data includes model-specific parameters (e.g., A,B,C,D matrices for state-space models) as well as generic metadata such as input and output names. The data is arranged into a fixed set of data fields called properties.
% You can access model data in the following ways:
% The get command
% Structure-like dot notation
% Data retrieval commands
% For illustration purposes, create the SISO transfer function (TF):

G = tf([1 2],[1 3 10],'inputdelay',3)

%
% To see all properties of the TF object G, type

get(G)

%
% The first four properties Numerator, Denominator, IODelay, and Variable are specific to the TF representation. The remaining properties are common to all LTI representations. You can use help tf.Numerator to get more information on the Numerator property and similarly for the other properties.
% To retrieve the value of a particular property, use

G.InputDelay    % get input delay value

%
% You can use abbreviations for property names as long as they are unambiguous, for example:

G.iod    % get transport delay value

%

G.var    % get variable

%
%% Quick Data Retrieval
% You can also retrieve all model parameters at once using tfdata, zpkdata, ssdata, or frdata. For example:

[Numerator,Denominator,Ts] = tfdata(G)

%
% Note that the numerator and denominator are returned as cell arrays. This is consistent with the MIMO case where Numerator and Denominator contain cell arrays of numerator and denominator polynomials (with one entry per I/O pair). For SISO transfer functions, you can return the numerator and denominator data as vectors by using a flag, for example:

[Numerator,Denominator] = tfdata(G,'v')

%
%% Editing Data
% You can modify the data stored in LTI objects by editing the corresponding property values with set or dot notation. For example, for the transfer function G created above,

G.Ts = 1;

%
% changes the sample time from 0 to 1, which redefines the model as discrete:

G

%
% The set command is equivalent to dot assignment, but also lets you set multiple properties at once:

G.Ts = 0.1;
G.Variable = 'q';
G

%
%% Sensitivity Analysis Example
% Using model editing together with LTI array support, you can easily investigate sensitivity to parameter variations. For example, consider the second-order transfer function
%  H(s) = { s+5 \over s^2 + 2 \zeta s + 5 }
% You can investigate the effect of the damping parameter zeta on the frequency response by creating three models with different zeta values and comparing their Bode responses:

s = tf('s');

% Create 3 transfer functions with Numerator = s+5 and Denominator = 1
H = repsys(s+5,[1 1 3]);  

% Specify denominators using 3 different zeta values
zeta = [1 .5 .2];
for k = 1:3
  H(:,:,k).Denominator = [1 2*zeta(k) 5];  % zeta(k) -> k-th model
end

% Plot Bode response
bode(H)
grid

% Copyright 1986-2014 The MathWorks, Inc.