clear variables
%
%  Program to model RLS for plant identification
%
%  determin the time vector
%
  Tf = 4.0;       % determine the final time
  Ts = 0.005;        % determine the sample interval
  t = [0:Ts:Tf];   % get a time vector
  N = length(t);
%
% enter the forgetting factor
%
  lambda = 0.5;
%
%  determine the input (reference) signal
%
  r = ones(1,length(t));
  uu = ones(1,length(t));
  r =  uu.*((t>=0.25)&(t<0.5))+ 3*uu.*((t>0.75)&(t<1.25)) + 2*uu.*((t>1.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+j 0.2-j 0.3]*0.9);
%
%  determine the saturation level
%
  saturation_level = 2.0;
%
%  Now simulate the system
%
  sim('adaptive_controller');
%
%  plot the results
%
  figure;
  subplot(2,1,1); stairs(ts,ys,'Linewidth',2); grid; xlabel('Time'); ylabel('y value');
  subplot(2,1,2); stairs(ts,us(:,1),'Linewidth',2); grid; xlabel('Time'); ylabel('u value');
%
% extract the parameters and plot them when you get to the RLS part
%
%   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); grid; ylabel('y(n)');
