echo off
%   IDDEMO MULTIDATA shows how to deal with multiple experiments and
%   merging models.

%   Copyright 1986-2001 The MathWorks, Inc.
%   $Revision: 1.8 $ $Date: 2001/04/25 12:53:46 $
 
clf
help iddemo8
echo on
pause % Strike any key to continue.

% We start by looking at the dataset iddemo8.mat.

load iddemo8

% The name of the data object is dat, and let us look at it.

plot(dat)

pause % Strike any key to continue.

% We see that there are some problems with the output around
% sample 250-280 and around samples 600 to 650. Might have been
% sensor failures.
%
% Therefore split the data into three separate experiments and
% put then into a multi-experiment data object:

d1 = dat(1:250);
d2 = dat(281:600);
d3 = dat(651:1000);
d = merge(d1,d2,d3);

% To look at the new data set, just type its name:

pause % Strike any key to continue.

d

pause % Strike any key to continue.

% The different experiments can be given other names, e.g.,

d.exp = {'Period 1';'Day 2';'Phase 3'}

% To examine it, use plot:

pause % Strike any key to continue. 

plot(d),pause % Strike a key to advance the plots for one experiment to another.

% All model estimation routines accept multi-experiment data and
% take into account that they are recorded at different periods.
% Let us use the two first experiments for estimation and the third one
% for validation:

de = getexp(d,[1,2]); % subselection is done using  the command GETEXP using numbers
dv = getexp(d,'Phase 3'); % or names.
             
m1 = arx(de,[2 2 1]);
m2 = n4sid(de,2);
m3 = armax(de,[2 2 2 1]);
compare(dv,m1,m2,m3)

pause % Strike any key to continue. 

% compare also accepts multiple experiments:

compare(d,m1,m2,m3)

pause % Strike any key to continue. 

% Also spa, etfe, resid, predict, sim operate in the obvious way for
% multi-experiment data. 

% There is another way to deal with separate data sets: A model can be
% computed for each set, and then the models can  be merged:

m4 = armax(getexp(de,1),[2 2 2 1]);
m5 = armax(getexp(de,2),[2 2 2 1]);
m6 = merge(m4,m5);

pause % Strike any key to continue. 

% This is conceptually the same as computing m from the merged set DE, but
% it is not numerically the same. Working on DE assumes that the 
% signal-to-noise ratios are (about) the same in the different experiments,
% while merging separate models makes independet estimates of the noise levels.
% If the conditions are about the same for the different experiments, it is
% more efficient to estimate directly on the multi-experiment data.

pause % Strike any key to continue. 

% We can check the models m3 and m6 that are both ARMAX models obtained on the
% same data inthe two different ways:

[m3.a;m6.a]
[m3.b;m6.b]
[m3.c;m6.c]

pause % Strike any key to continue. 

compare(dv,m3,m6)

pause

% We now turn to another situation.
% Let us consider two data sets generated by the system m0.
% The system is given by

pause, m0

pause

% The data sets that have been collected are z1 and z2, obtained from m0
% with different inputs, noise and initial conditions.
% Strike a key to advance between the plots.

plot(z1)

pause

plot(z2)

% If we just concatenate the data we obtain

zzl = [z1;z2]

pause

plot(zzl)

pause

% A model is obtained by

ml = pem(zzl,3,'nk',[0 1]); % No delay from input # 1 and one from input # 2
bode(m0,ml)

% Strike a key to advance between plots
% This is not a very good model.

pause % press a key

% Now, instead treat the two data sets as different experiments:

zzm = merge(z1,z2)

% The model becomes

pause, mm = pem(zzm,3,'nk',[0 1],'trace','on'); pause

%  Let us compare the Bode plots of the true system (blue)
%  the model from concatenated data (green) and the model from the
%  merged data set (red):

bode(m0,'b',ml,'g',mm,'r')

%  The merged data give a much better model.
%
%  End of demo
echo off
