%
%  driver for an IIR implementation of a simple feedback loop
%
  clear variables
%
  Ts = 0.01;
  Tf = 5.0;
  t = [0:Ts:Tf];
  N = length(t);
%
%  determine the input (reference) signal
%
  r = ones(1,length(t));
% 
% prepare the reference signal and the corresponding time for Simulink
%
  rt = [t' r'];
%
%  determine the continuous time transfer function for the plant
%
  Gp = tf([1 100],[1 2 200]);
%
%  get the discrete-time version of the tranfer function using a zero order
%  hold
%
  Gp_z = c2d(Gp,Ts,'zoh') 
  [Bp, Ap] = tfdata(Gp_z,'v');
  Nbp = length(Bp);
%
%  the closed loop system is the same as the open loop system for now
%
  Go_z = Gp_z; 
%
%  determine the discrete-time transfer function for the controller
%
%  Gc_z = tf([0.02 0],[1 -1],Ts);
%  [Bc, Ac] = tfdata(Gc_z,'v')
%  Nbc = length(Bc);
%
%  include the delay between reference signal and output
%
%  H = tf(1,[1 0],Ts);
%
%  determine the closed loop transfer function
%
%  Go_z = minreal(feedback(Gp_z*Gc_z,H))
%
%  simulate the step response in Matlab
%
  yfinal = lsim(Go_z, r(1:N), t(1:N));
%
%  run the simulation
%
  sim('openloop_DE');
%
%  Now plot the results
%
figure;
plot(t,yfinal,'+'); grid; 
%plot(ts,ys,'o',t,yfinal,'+'); grid; 
%legend('Simulink ','Matlab');
xlabel('Time'); ylabel('y value');
 