% ejemplo 5.1, aström, pag. 187
% "Adaptation of a feedforward gain" 
%   G(s)=1/(s+1)
% elaboro E.Quintero.M 22jul'98
%-------------------------------------------------
clear
% parametros
time=0; tf=20; gamma=1;
dt=0.05;    % integration step size
y=0; ym=0; k=1; k0=2; th=0;
while(time<(tf-dt))
%--------------------------------------------------------    
      uc=sin(time);
      u=th*uc;
      dy=-y+k*u;
      if(time==0) xx=[time uc y ym th]; end
%--------------------------------------------------------
      dym=-ym+k0*uc;
      e=y-ym;
      dth=-gamma*ym*e;

      y=y+dy*dt;
      ym=ym+dym*dt;
      th=th+dth*dt;

      time=time+dt;
      xx=[xx; time uc y ym th];
end
format short e
time,gamma,th
subplot(2,1,1),plot(xx(:,1),xx(:,3),xx(:,1),xx(:,4))
subplot(2,1,2),plot(xx(:,1),xx(:,5))
grid