%
%  This program plays with a ZOH in Simulink
%
% Set the sample time
%
  Ts = 0.05;
%
%  set the final time
%
  Tf = 10;
%
%  set the continuous-time transfer function
%
  Gp = tf(1,[1 2 0.1])
  [num_s, den_s] = tfdata(Gp,'v');
%
%  convert to a discrete-time transfer function using ZOH
%
  Gp_z = c2d(Gp,Ts,'zoh')
  [num_Gp_z,den_Gp_z] = tfdata(Gp_z,'v');
%
%  set the discrete-time controller
%
  Gc_z = 5.9*tf([1 -0.982],[1 -1], Ts);
  [num_Gc_z, den_Gc_z] = tfdata(Gc_z,'v');
%
%  set up the continuous-time state model
%
  A = [0 1 0; 0 0 1; -2 -3 -4];
  B = [0; 0; 1];
  C = [1 0 0];
  D = 0;
%
%  make the discrete-time model
%
 sys_c = ss(A,B,C,D);
 sys_d = c2d(sys_c, Ts, 'ZOH');
 [G,H,Cd,Dd] = ssdata(sys_d);
%
%  place the closed loop poles
%
  p = [0.1 0.2 0.3];
  K = place(G,H,p);
%
%  run the simulation
%
  sim('zoh_files');
%
%  plot the transfer function results
%
  figure;
  [tz_s,yz_s]  = stairs(t_z, y_z);
  [tzoh_s,yzoh_s] = stairs(t_z,ys);
  plot(tz_s, yz_s,'r.-', tzoh_s, yzoh_s,'b:'); grid;
%
%  plot the state variable results
%
  figure;
  [tz_s,yz_s]  = stairs(t_z, ystate_z);
  [tzoh_s,yzoh_s] = stairs(t_z,ystate_s);
  plot(tz_s, yz_s,'r.-', tzoh_s, yzoh_s,'b:'); grid;
  