%% Getting Started with LTI Models -- Creating Models


%   Copyright 1986-2004 The MathWorks, Inc.
%   $Revision: 1.1.6.1 $  $Date: 2004/08/17 21:33:06 $

 
%% LTI Model Types
% The Control System Toolbox provides commands for creating 4 basic types
% of linear time-invariant (LTI) models:
%
% * transfer function models (*TF*)
% * zero-pole-gain models (*ZPK*)
% * state-space models (*SS*)
% * frequency response data models (*FRD*) 
%
% These functions take model data as input and return objects which embody
% this data in a single MATLAB variable.


%% Transfer Function Models
%
% $$ H(s) = \frac{p_{1} s^{n} + p_{2} s^{n-1} + \ldots + p_{n+1}}{q_{1}
% s^{m} + q_{2} s^{m-1} + \ldots + q_{m+1}} $$
%
% where:
%
% $$ p_{1} \ldots p_{n+1} \mbox{ are the numerator coefficients, and} $$
%
% $$ q_{1} \ldots q_{m+1} \mbox{ are the denominator coefficients} $$
%

%%
% The numerator and denominator polynomials define a transfer function 
% model.
%
% A vector of coefficients specifies the polynomials. For example
%
% [ 1, 2, 10 ]
%
% would specify the polynomial
%        
% $$  s^{2} + 2 s + 10 .$$

%% Example: Transfer Function Models
% $$ H(s) = \frac{s}{s^2+2s+10} $$

%%
% You can create a SISO transfer function model by specifying its numerator
% and denominator polynomials as inputs to the TF command:
num = [ 1  0 ];       % Numerator: s
den = [ 1  2  10 ];   % Denominator: s^2 + 2 s + 10
H = tf(num,den);

%%
% You can also specify this model as a rational expression of s:
%
s = tf('s');        % Create Laplace variable
H = s / (s^2 + 2*s + 10);

%% Zero-Pole-Gain Models
% $$ H(s) = k\frac{( s - z_{1} ) \ldots ( s - z_{n} )}{( s - p_{1} ) \ldots
% ( s - p_{m} )} $$
%
% where
%
% $$ k \mbox{ is the gain} $$
%
% $$ z_{1} \ldots z_{n} \mbox{ are the zeros of H(s)} $$
%
% $$ p_{1} \ldots p_{m} \mbox{ are the poles of H(s)}. $$
%

%%
% Zero-pole-gain models are the factored form of transfer function models.
%
% In this format, the gain k, zeros z
% (numerator roots), and poles p (denominator roots) characterize the model.


%% Example: Zero-Pole-Gain Models
% $$ H(s) = -2\frac{s}{( s - 2 ) ( s^2 - 2 s + 2 )} $$

%%
% You can specify a SISO zero-pole-gain model using the ZPK command:
z = 0;                   % Zeros
p = [ 2  1+i  1-i ];     % Poles
k = -2;                  % Gain
H = zpk(z,p,k);

%%
% You can also specify this model as a rational expression of s:
s = zpk('s');
H = -2*s / (s - 2) / (s^2 - 2*s + 2);
 

%% State-Space Models
% $$ \frac{dx}{dt} = Ax + Bu $$
%
% $$ y = Cx + Du $$
%
% where
%
% $$ x \mbox{ is the state vector} $$
%
% $$ u \mbox{ and } y \mbox{ are the input and output vectors} $$
% 
% $$ A,~B,~C,~D \mbox{ are the state-space matrices} $$

%%
% State-space models are constructed from the linear differential or
% difference equations describing the system dynamics.
%
% The state-space matrices A, B, C, and D characterize these
% models.

%% Example: State-Space Models
% The following describes a simple electric motor.
%
% $$ \frac{d^2\theta}{dt^2} + 2\frac{d\theta}{dt} + 5\theta = 3I $$
%
% where
%
% $$ I \mbox{ is the driving current (input,u)} $$
% 
% $$ \theta \mbox{ is the angular displacement of the rotor (ouput,y).} $$

%%
% The relations below describe the relationship between the driving 
% current (input) and the angular displacement of the rotor (output)
% in state-space form.

%%
% $$ \frac{dx}{dt} = Ax + BI~~~~~~~A = \left[\matrix{0 & 1 \cr
% -5 & -2 }\right] ~~~~~~~  B=\left[\matrix{0 \cr 3 }\right]~~~~~~~
% x=\left[\matrix{\theta \cr \frac{d\theta}{dt} }\right] $$
%
% $$ \theta = Cx + DI~~~~~~~C=[1~~0]~~~~~~~D = [0] $$

%%

%
% You can use the SS command to create the state-space model of this system
% in MATLAB:

A = [ 0  1 ; -5  -2 ];
B = [ 0 ; 3 ];
C = [ 1  0 ];
D = 0;
H = ss(A,B,C,D);
 
%% Frequency Response Data Models

GSCreatingModels_aux % Draw diagram

%%
% Frequency response data, *FRD*, models allow you to store the measured
% or simulated complex frequency response of a system in an LTI object. You
% can then analyze the model using the Control System Toolbox.

%% Example: Frequency Response Data Models
% Given a vector of frequencies and a vector of system responses to
% excitation at these frequencies,
%
% From input 1 to:                
%                                        
%          Frequency(Hz)      output 1   
%          -------------      --------   
%               1000      -0.8126-0.0003i
%               2000      -0.1751-0.0016i
%               3000      -0.0926-0.4630i
%
% you can construct an *FRD* model with this data using the FRD command:

freq = [1000 ; 2000 ; 3000];  % measured in Hz
resp = [-0.8126-0.0003i ; -0.1751-0.0016i ; -0.0926-0.4630i];
H = frd(resp,freq,'Units','Hz');
      
%%
% The last 2 arguments in this example are used to indicate that the
% frequency units are in Hertz.  The MATLAB output is shown above.
 