clear variables
%
%  Program to model RLS for plant identification
%
%  determin the time vector
%
  Tf = 4.0;       % determine the final time
  Ts = 0.01;        % determine the sample interval
  t = [0:Ts:Tf];   % get a time vector
  N = length(t);
%
% enter the forgetting factor
%
  lambda = 0.95;
%
%  determine the input (reference) signal
%
  r = ones(1,length(t));
  uu = ones(1,length(t));
  r = uu.*(t<0.5)+ uu.*((t>1.0)& (t<1.5)) +uu.*((t>2.5)&(t<3.0))+uu.*(t>3.5);
  r = r+0.01*randn(1,length(t));
%  r = uu;
%  r = randn(1,N);
%  r = sin(2*pi*t);
% 
% prepare the reference signal and the corresponding time for Simulink
%
  rt = [t' r']; 
%
%  determine the size of the plant transfer function and controller 
%
  Nbp = 2;
  Nbc = 3;
%
%
% set the prefilter to 1 to start with
%
  Gpf = 1.0;
%
%  get the desired closed loop pole locations
%
  D = poly([ 0.1 0.2 0.3 0.4])
%
%  determine the saturation level
%
  saturation_level = 0.5;
%
%  Now simulate the system
%
  sim('adaptive_controller');
%
%  plot the results
%
  figure;
  subplot(2,1,1); stairs(ts,ys); grid; xlabel('Time'); ylabel('y value');
  subplot(2,1,2); stairs(ts,us); grid; xlabel('Time'); ylabel('u value');
 
%
% extract the parameters and plot them
%
%   N = length(ts)
%   for i = 1:N
%       theta1(i) = theta_est(1,1,i);
%       theta2(i) = theta_est(2,1,i);
%       theta3(i) = theta_est(3,1,i);
%   end;
%   figure;
%   subplot(4,1,1); plot(ts,-theta1,'r-'); grid; axis([ 0 max(ts) -2 2]); ylabel('a_1');
%   subplot(4,1,2); plot(ts,theta2,'r-');  grid; axis([ 0 max(ts) -2 2]); ylabel('b_0');
%   subplot(4,1,3); plot(ts,theta3,'r-');  grid; axis([ 0 max(ts) -2 2]); ylabel('b_1');
%   subplot(4,1,4); plot(ts,ys);
