www.gusucode.com > ident_featured 案例代码 matlab源码程序 > ident_featured/iddemo6.m

    %% Data and Model Objects in System Identification Toolbox(TM)
% This example shows how to manage data and model objects available in the
% System Identification Toolbox(TM). System identification is about
% building models from data. A data set is characterized by several pieces
% of information: The input and output signals, the sample time, the
% variable names and units, etc. Similarly, the estimated models contain
% information of different kinds - estimated parameters, their covariance
% matrices, model structure and so on.
%
% This means that it is suitable and desirable to package relevant
% information around data and models into objects. System Identification
% Toolbox(TM) contains a number of such objects, and the basic features of
% these are described in this example. 

% Copyright 1986-2014 The MathWorks, Inc. 

%% The IDDATA Object
% First create some data:
u = sign(randn(200,2)); % 2 inputs
y = randn(200,1);       % 1 output
ts = 0.1;               % The sample time

%%
% To collect the input and the output in one object do
z = iddata(y,u,ts);

%%
% The data information is displayed by just typing its name:
z

%%
% The data is plotted as iddata by the |plot| command, as in |plot(z)|.
% Press a key to continue and advance between the subplots. Here, we plot
% the channels separately:
%
plot(z(:,1,1)) % Data subset with Input 1 and Output 1.
%%
plot(z(:,1,2)) % Data subset with Input 2 and Output 1.

%%
% To retrieve the outputs and inputs, use
u = z.u;   % or, equivalently u = get(z,'u');
y = z.y;   % or, equivalently y = get(z,'y');

%%
% To select a portion of the data:
zp = z(48:79);

%%
% To select the first output and the second input:
zs = z(:,1,2);  % The ':' refers to all the data time points.

%%
% The sub-selections can be combined:
plot(z(45:54,1,2)) % samples 45 to 54 of response from second input to the first output.

%%
% The channels are given default names 'y1', 'u2', etc. This can be changed
% to any values by
set(z,'InputName',{'Voltage';'Current'},'OutputName','Speed');

%%
% Equivalently we could write
z.inputn = {'Voltage';'Current'}; % Autofill is used for properties
z.outputn = 'Speed';    % Upper and lower cases are also ignored

%%
% For bookkeeping and plots, also units can be set:
z.InputUnit = {'Volt';'Ampere'};
z.OutputUnit = 'm/s';
z

%%
% All current properties are (as for any object) obtained by get:
get(z)

%%
% In addition to the properties discussed so far, we have
% 'Period' which denotes the period of the input if periodic
% Period = inf means a non-periodic input:
z.Period

%%
% The intersample behavior of the input may be given as 'zoh'
% (zero-order-hold, i.e. piecewise constant) or 'foh' (first-
% order-hold, i.e., piecewise linear). The identification routines
% use this information to adjust the algorithms.
z.InterSample

%%
% You can add channels (both input and output) by "horizontal
% concatenation", i.e. z = [z1 z2]:
z2 = iddata(rand(200,1),ones(200,1),0.1,'OutputName','New Output',...
    'InputName','New Input');
z3 = [z,z2]

%%
% Let us plot some of the channels of |z3|:
plot(z3(:,1,1)) % Data subset with Input 2 and Output 1.

%%
plot(z3(:,2,3)) % Data subset with Input 2 and Output 3.

%%
% *Generating Inputs*
%
% The command |idinput| generates typical input signals.
u = idinput([30 1 10],'sine'); % 10 periods of 30 samples
u = iddata([],u,1,'Period',30) % Making the input an IDDATA object.

%%
% SIM applied to an iddata input delivers an iddata output. Let us use
% |sim| to obtain the response of an estimated model |m| using the input
% |u|. We also add noise to the model response in accordance with the noise
% dynamics of the model. We do this by using the "AddNoise" simulation
% option:
m = idpoly([1 -1.5 0.7],[0 1 0.5]);  % This creates a model; see below.
options = simOptions;
options.AddNoise = true;
y = sim(m,u,options) % simulated response produced as an iddata object

%%
% The simulation input |u| and the output |y| may be combined into a single
% |iddata| object as follows:
z5 = [y u] % The output-input iddata.

%%
% More about the |iddata| object is found under |help iddata|.

%% The Linear Model Objects
%
% All models are delivered as MATLAB(R) objects. There are a few different
% objects depending on the type of model used, but this is mostly
% transparent. 
load iddata1
m = armax(z1,[2 2 2 1]);  % This creates an ARMAX model, delivered as an IDPOLY object

%%
% All relevant properties of this model are packaged as one object (here,
% |idpoly|). To display it just type its name:
m

%%
%      Many of the model properties are directly accessible
m.a    % The A-polynomial

%%
% A list of properties is obtained by get:
get(m)

%%
% Use |present| to view the estimated parameter covariance as +/- 1
% standard deviation uncertainty values on individual parameters:
present(m)

%%
% Use |getpvec| to fetch a flat list of all the model parameters, or just
% the free ones, and their uncertainties. Use |getcov| to fetch the entire
% covariance matrix.
[par, dpar] = getpvec(m, 'free')
CovFree = getcov(m,'value')

%%
% nf = 0, nd = 0 denote orders of a general linear model, of which
% the ARMAX model is a special case.
%
% Report contains information about the estimation process:
m.Report
m.Report.DataUsed       % record of data used for estimation
m.Report.Fit            % quantitative measures of model quality
m.Report.Termination    % search termination conditions

%%
% To obtain on-line information about the minimization, use the 'Display'
% estimation option with possible values 'off', 'on', and 'full'. This
% launches a progress viewer that shows information on the model estimation
% progress.
Opt = armaxOptions('Display','on');
m1 = armax(z1,[2 2 2 1],Opt);

%% Variants of Linear Models - IDTF, IDPOLY, IDPROC, IDSS and IDGREY
% There are several types of linear models. The one above is an example of
% the |idpoly| version for polynomial type models. Different variants of
% polynomial-type models, such as Box-Jenkins models, Output Error models,
% ARMAX models etc are obtained using the corresponding estimators - |bj,
% oe, armax, arx| etc. All of these are presented as |idpoly| objects.
%
% Other variants are |idss| for state-space models; |idgrey| for
% user-defined structured state-space models; |idtf| for transfer function
% models, and |idproc| for process models (gain+delay+static gain).
%
%%
% The commands to evaluate the model: |bode, step, iopzmap, compare|, etc,
% all operate directly on the model objects, for example:
compare(z1,m1)

%%
% Transformations to state-space, transfer function and zeros/poles are
% obtained by |idssdata|, |tfdata| and |zpkdata|:
[num,den]  = tfdata(m1,'v')
%%
% The 'v' means that num and den are returned as vectors and not as cell
% arrays. The cell arrays are useful to handle multivariable systems. To
% also retrieve the 1 standard deviation uncertainties in the values of
% |num| and |den| use:
[num, den, ~, dnum, dden] = tfdata(m1,'v')

%% Transforming Identified Models into Numeric LTIs of Control System Toolbox(TM)
% The objects also connect directly to the Control System Toolbox(TM) model
% objects, like |tf|, |ss|, and |zpk|, and can be converted into these LTI
% objects, if Control System Toolbox is available. For example, |tf|
% converts an |idpoly| object into a |tf| object.
CSTBInstalled = exist('tf','class')==8;
if CSTBInstalled % check if Control System Toolbox is installed
    tfm = tf(m1) % convert IDPOLY model m1 into a TF object
end

%%
% When converting an IDLTI model to the Control Systems Toolbox's LTI
% models, noise component is not retained. To also include the noise
% channels as regular inputs of the LTI model, use the 'augmented' flag:
if CSTBInstalled
    tfm2 = tf(m1,'augmented') 
end

%%
% The noise channel is named |v@y1| in the model |tfm2|.

%% Additional Information
% For more information on identification of dynamic systems with System
% Identification Toolbox visit the
% <http://www.mathworks.com/products/sysid/ System Identification Toolbox> product
% information page.