home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 9.ddi / IDENT.DI$ / ROE.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  3.2 KB  |  88 lines

  1. function [thm,yhat,p,phi,psi] = roe(z,nn,adm,adg,th0,p0,phi,psi)
  2. %ROE    Computes estimates recursively for an output error model using the
  3. %    Recursive Prediction Error Method
  4. %
  5. %    [THM,YHAT] = roe(Z,NN,adm,adg)
  6. %
  7. %    Z: The output-input data z=[y u] (single input only!)
  8. %    NN : NN=[nb nf nk], The orders and delay of an output error
  9. %         input-output model (see HELP OE)
  10. %    adm: Adaptation mechanism. adg: Adaptation gain
  11. %     adm='ff', adg=lam:  Forgetting factor algorithm, with forg factor lam
  12. %     adm='kf', adg=R1: The Kalman filter algorithm with R1 as covariance 
  13. %        matrix of the parameter changes per time step
  14. %     adm='ng', adg=gam: A normalized gradient algorithm, with gain gam
  15. %     adm='ug', adg=gam: An Unnormalized gradient algorithm with gain gam
  16. %    THM: The resulting estimates. Row k contains the estimates "in alpha-
  17. %         betic order" corresponding to data up to time k (row k in Z)
  18. %    YHAT: The predicted values of the outputs. Row k corresponds to time k.
  19. %    Initial value of parameters(TH0) and of "P-matrix" (P0) can be given by
  20. %    [THM,YHAT,P] = roe(Z,NN,adm,adg,TH0,P0)
  21. %    Initial and last values of auxiliary data vectors phi and psi are 
  22. %    obtained by [THM,YHAT,P,phi,psi]=roe(Z,NN,adm,adg,TH0,P0,phi0,psi0). 
  23. %    
  24. %    See also RARX, RARMAX, RBJ, RPEM, and RPLR.
  25.  
  26. %    L. Ljung 10-1-89
  27. %    Copyright (c) 1989-90 by the MathWorks, Inc.
  28. %    All Rights Reserved.
  29.  
  30. [nz,ns]=size(z);
  31. if ns<=1,error('This routine requires an input. For a time series, use RARMAX or RARX instead!'),end
  32. if ns>2,error('This  routine is for single input only. Use RPEM instead!'),end
  33. if length(nn)~=3,error('Incorrect number of orders specified! nn = [nb nf nk]'),end
  34. nb=nn(1);nf=nn(2);nk=nn(3);nu=1;
  35. if nk<1,error('Sorry, this routine requires nk>0; Shift input sequence if necessary!'),end
  36. d=sum(nn(1:2));
  37. if ns>2,error('Sorry, this routine is for single input only!'),end
  38.  
  39. nbm=max([nb+nk-1,nf]);
  40.  
  41. tif=nb+1:d;
  42.  
  43. ib=nk:nb+nk-1;ibf=1:nf;
  44. iff=nbm+1:nbm+nf;
  45. iib=1:nbm-1;
  46. iif=nbm+1:nbm+nf-1;
  47. dm=nbm+nf;
  48. ii=[iib iif];i=[ib iff];
  49.  
  50. if nargin<8, psi=zeros(dm,1);end
  51. if nargin<7, phi=zeros(dm,1);end
  52. if nargin<6, p0=10000*eye(d);end
  53. if nargin<5, th0=eps*ones(d,1);end
  54. if isempty(psi),psi=zeros(dm,1);end
  55. if isempty(phi),phi=zeros(dm,1);end
  56. if isempty(p0),p0=10000*eye(d);end
  57. if isempty(th0),th0=eps*ones(d,1);end
  58. if length(th0)~=d, error('The length of th0 must equal the number of estimated parameters!'),end
  59. [th0nr,th0nc]=size(th0);if th0nr<th0nc, th0=th0';end
  60. p=p0;th=th0;
  61. if adm(1)=='f', R1=zeros(d,d);lam=adg;end
  62. if adm(1)=='k', [sR1,SR1]=size(adg);
  63.      if sR1~=d | SR1~=d,error('The R1 matrix should be a square matrix with dimension equal to number of parameters!'),end
  64.      R1=adg;lam=1;
  65. end
  66. if adm(2)=='g', grad=1;else grad=0;end
  67.  
  68. for kcou=1:nz
  69. yh=phi(i)'*th;
  70. epsi=z(kcou,1)-yh;
  71. if ~grad,K=p*psi(i)/(lam + psi(i)'*p*psi(i));
  72.          p=(p-K*psi(i)'*p)/lam+R1;
  73. else K=adg*psi(i);end
  74. if adm(1)=='n', K=K/(eps+psi(i)'*psi(i));end
  75. th=th+K*epsi;
  76. f=fstab([1;th(tif)])';
  77. th(tif)=f(2:nf+1);
  78. w=phi(i)'*th;
  79. if nk==1,up=z(kcou,2);else up=phi(nk-1);end
  80. ztil=[[up,-psi(ibf)'];[w,psi(iff)']]*f;
  81.  
  82. phi(ii+1)=phi(ii);psi(ii+1)=psi(ii);
  83. if nb>0,phi(1)=z(kcou,2);psi(nk)=ztil(1);end
  84. if nf>0,phi(nbm+1)=-w;psi(nbm+1)=-ztil(2);end
  85.  
  86. thm(kcou,:)=th';yhat(kcou)=yh;
  87. end
  88. yhat=yhat';