%SYSIDDM Identify "hairdryer" system characteristics.

%       This case study concerns data collected from a laboratory scale
%       "hairdryer". (Feedback's Process Trainer PT326; See also page
%       440 in Ljung,1987). 

%	This Expo demo adapted from ...
%	IDDEMO1 by Lennart Ljung
%	Adapted for Expo by Ned Gulley, 6-21-93
%	Copyright (c) 1984-94 by The MathWorks, Inc.

% Demo initialization ====================
if ~exist('SlideShowGUIFlag'), figNumber=0; end;

load dryer2
u=u2; y=y2;
z=[y u];
T=0.08;
int=1:1000;
plot(T*int,z(int,1))
title('OUTPUT #1')

if ssinit(figNumber),
  str = str2mat( ...
    ' Press the "Start" button to see a demonstration of', ...
    ' the system identification process.', ...
    ' ', ...
    ' >> load dryer2', ...
    ' >> u=u2; y=y2;', ...
    ' >> z=[y u];', ...
    ' >> T=0.08;', ...
    ' >> int=1:1000;', ...
    ' >> plot(T*int,z(int,1))', ...
    ' >> title(''OUTPUT #1'')' ...
  );
  ssdisp(figNumber,str);
  if figNumber, return; end
end

% Beginning of the demo ==================


str = str2mat( ...                                                           
  ' This case study concerns data collected from a', ...
  ' laboratory scale "hairdryer".  The process works like', ...
  ' this:  air is fanned through a tube and heated', ...
  ' by an electric current (input u) while the air temperature', ...
  ' (output y) is measured by a thermocouple at the outlet.' ...
);
ssdisp(figNumber,str);                                         

if sspause(figNumber), return; end;


z2=dtrend([y(1:300) u(1:300)]);
int=1:300;
plot(T*int,z2(int,1))
title('OUTPUT #1')

str = str2mat( ...
  ' Here is a closer look at the same data, only detrended', ...
  ' (or given a zero mean) so that the identification process', ...
  ' can work properly.  This data will now be used to create a ', ...
  ' model using the ARX command.', ...
  ' ', ...
  ' ', ...
  ' >> z2=dtrend([y(1:300) u(1:300)]);', ...
  ' >> int=1:300;', ...
  ' >> plot(T*int,z2(int,1))', ...
  ' >> title(''OUTPUT #1'')' ...
);
ssdisp(figNumber,str);                                                     

if sspause(figNumber), return; end;


th=sett(arx(z2,[2 2 3]),T);
yh=idsim(dtrend(u(700:900)),th);
t=(700:900)*T;
plot(t,[dtrend(y(700:900)) yh]);

str = str2mat( ...
  ' How good is this model? One way to find out is to', ...
  ' simulate it and compare the model output with', ...
  ' measured output. We then select a portion of the', ...
  ' original data that was not used to build the model.', ...
  ' Here is the original data (yellow) and the model (magenta).', ...
  ' ', ...
  ' >> th=sett(arx(z2,[2 2 3]),T);', ...
  ' >> yh=idsim(dtrend(u(700:900)),th);', ...
  ' >> t=(700:900)*T;', ...
  ' >> plot(t,[dtrend(y(700:900)) yh]);' ...
);
ssdisp(figNumber,str);                                            

if sspause(figNumber), return; end;


gth=th2ff(th);
gs=sett(spa(z2),T);
G=[gs gth];
G=G(2:size(G,1),:);
loglog(G(:,[1 6]),G(:,[2 7]));
xlabel('frequency (rad/sec)')
title('AMPLITUDE PLOT, input #1 output #1')

str = str2mat( ...
  ' Another measure of the quality of the model is a frequency', ...
  ' response comparison of the original system (yellow) with', ...
  ' the modeled system (magenta).', ...
  ' ', ...
  ' >> gth=th2ff(th);', ...
  ' >> gs=sett(spa(z2),T);', ...
  ' >> G=[gs gth];', ...
  ' >> G=G(2:size(G,1),:);', ...
  ' >> loglog(G(:,[1 6]),G(:,[2 7]));', ...
  ' >> xlabel(''frequency (rad/sec)'')' ...
);
ssdisp(figNumber,str);                                              

if sspause(figNumber), return; end;


step=ones(30,1);
idsimsd(step,th);
set(gcf,'NextPlot','add');

str = str2mat( ...
  ' Finally we can plot the uncertainty of the step response', ...
  ' of the model.  The model comes with an estimate of its', ...
  ' own uncertainty.  In this case 10 different step', ...
  ' responses are plotted, corresponding to models', ...
  ' drawn from the distribution of the true system.', ...
  ' ', ...
  ' ', ...
  ' >> step=ones(30,1);', ...
  ' >> idsimsd(step,th);', ...
  ' >> set(gcf,''NextPlot'',''add'');' ...
);
ssdisp(figNumber,str);                                        


% End of the demo ========================
