function [sys, x0, str, ts] = rlspps(t,x,u,flag,name,order,char_poly,dt)
%RLSPPS S-function which performs pole placement.
%   This M-file is designed to be used in a Simulink S-function block.
%   It computes the gains for a filter placed in the feed forward path
%   of a control system such that the closed loop has the desired
%   characteristic polynomial.  The gains are returned as a single
%   vector with P as the first half, and L as the second half.
%   
%   The input arguments are
%   sys_name:   the name of the block diagram system
%   blk_name:   the name of the discrete controller in the block diagram
%   order:      the order of the model
%   ic:         the initial condition for the model parameters
%   char_poly:  the desired characteristic polynomial
%   dt:         how often to sample points (secs)
%
%   See also SFUNTMPL.

%   Copyright (c) 1990-1998 by The MathWorks, Inc. All Rights Reserved.
%   $Revision: 1.15 $
%   Rick Spada 6-17-92

if abs(flag) == 2

  % sample hit, return the next discrete states
  num = [0,u(1:order)'];
  den = [1,u(order+1:2*order)'];
  [l,p] = rlsppd(num,den,char_poly);
  if ~isempty(l) & ~isempty(p)
    sys = [l,p];
    num=['[' sprintf('%.16g ',p) ']'];
    den=['[' sprintf('%.16g ',l) ']'];
    set_param(name,'Numerator',num,'Denominator',den);
  end
  
elseif flag == 3

  % return the block outputs
  sys = x;
  sys = sys(:);
 
elseif flag == 4
  
  % Return next sample hit.  Where the sample time for this block
  % has been returned in the flag=0 call, it is unecessary for the
  % next sample hit to be computed.  However, if it is desired that
  % the sample time be changed on the fly, then it is necessary to
  % return the next sample hit based on the current value of the
  % dt parameter.
  
  sys = [];

  % To dynamically change the sample time on the fly, uncomment the
  % following lines of code.
  %
  % ns = t/dt;
  % sys = (1 + floor(ns + 1e-13*(1+ns)))*dt;
  %

elseif flag == 0
  
  % Return sizes of parameters and initial conditions
  sys(1) = 0;                           % 0 continuous states
  sys(2) = order*2;                     % 2*order discrete states (num and den)
  sys(3) = order*2;                     % 2*order outputs (num and den)
  sys(4) = order*2;                     % 2*order inputs (num and den)
  sys(5) = 0;                           % no roots
  sys(6) = 0;                           % no direct feedthrough
  sys(7) = 1;                           % 1 sample time
  
  x0 = zeros(order*2,1);
  ts = [dt, 0];
  
else

  % all other flags, return an empty matrix
  sys = [];
  
end
