function [r,s,t]=ppp(a,b,p,k,iint)
%[r,s,t]=ppp(a,b,p,k,iint)
%pole placement with polynomials
%System     Ay(t)=q**(-k)Bu(t)  k=>0
%A,B coprime
%Regulator  Ru(t)=-Sy(t)+Tr(t)
%R monic, T constant
%R,S  minimal degreee solution to   AR+q**(-k)BS=P
%degP<=degB+k+iint+max(degA-1,0)
%p will be padded with zeros to get the correct size
%T chosen to give unit static gain from r to y
%Leading 1's of monic polynomials are included in the vectors
%if iint=0 no integration in the regulator
%if iint=1 integrating regulator
%TS 861004 revised 861015, 880131
% Revised slightly for Matlab 5.2, MS980420

%1. Degrees and possible conversion of vectors
if a(1)==1,else b=b/a(1);a=a/a(1);end
if p(1)==1,else p=p/p(1);end
[n1,n2]=size(a);if n2>n1,a=a';na=n2;else,na=n1;end
[n1,n2]=size(b);if n2>n1,b=b';nb=n2;else,nb=n1;end
[n1,n2]=size(p);if n2>n1,p=p';np=n2;else,np=n1;end
nr=nb+k-2;ns=max([na+iint-1,1]);
a
%2. Modify a
if iint==1,a=conv(a,[1 -1]); a=a';na=length(a);end

%3. Fill matrices
for i=1:nr,aa(i:na-1+i,i)=a;end
for i=1:ns,aa(k-1+i:k+nb+i-2,nr+i)=b;end
bb=zeros(nr+ns+1,1);
bb(1:np)=p;
bb(1:na)=bb(1:na)-a;
bb=bb(2:nr+ns+1);

%4. Solve equations
tt=aa\bb;

%5. Modify R for integrating case
if nr==0,r=1;else,r=[1,tt(1:nr)'];end
if iint==1;r=conv(r,[1 -1]);end
s=(tt(nr+1:nr+ns))';

%6. Compute constant T
t=sum(p)/sum(b);