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

  1. function [e,r]=resid(z,th,M,maxsize)
  2. %RESID  Computes and tests the residuals associated with a model
  3. %
  4. %    E = resid(Z,TH)
  5. %
  6. %    Z : The output-input data Z=[y u], with y and u being column vectors.
  7. %    For multi-variable systems Z=[y1 y2 .. yp u1 u2 ... un]. 
  8. %    For time-series Z=y only.
  9. %    TH: The model to be evaluated on the given data set. (Format as
  10. %    described by HELP THETA)
  11. %    E : The residuals associated with TH and Z. [resid(Z,TH); just performs
  12. %    and displays the tests, without returning any data.]
  13. %
  14. %    The autocorrelation function of E and the cross correlation between
  15. %    E and the input(s) is computed and displayed. 99 % confidence limits 
  16. %    for these values are also given (based on the hypothesis
  17. %    that the residuals are white and independent of the inputs). These 
  18. %    functions are given up to lag 25, which can be changed to M by
  19. %    E = resid(Z,TH,M). The correlation information can be saved and re-
  20. %    plotted by
  21. %    [E,R] = resid(Z,TH).         resid(R);
  22. %
  23. %    E = resid(Z,TH,M,MAXSIZE) changes the memory variable MAXSIZE from
  24. %    its default value. See HELP AUXVAR.
  25.  
  26. %    L. Ljung 10-1-86,1-25-92
  27. %    Copyright (c) 1986-92 by the MathWorks, Inc.
  28. %    All Rights Reserved.
  29.  
  30. % ** Set up the default values **
  31. clf reset
  32. [N,nz]=size(z);
  33. maxsdef=idmsize(N);
  34.  
  35. if nargin<4, maxsize=maxsdef;end
  36. if nargin<3, M=25;end
  37. if maxsize<0,maxsize=maxsdef;end,if M<0, M=25;end
  38. if nargin==1,[nz2,M1]=size(z);nz=sqrt(nz2);M=M1-2;N=z(1,M1-1);
  39. ny=z(1,M1);nu=nz-ny;r=z(:,1:M);end
  40. if nargin>1,
  41. [N,nz]=size(z); nu=th(1,3);ny=nz-nu;
  42. % *** Compute the residuals and the covariance functions ***
  43.  
  44. e=pe(z,th);
  45.  
  46. if nz>1, e1=[e z(:,ny+1:nz)];else e1=e;end
  47. r=covf(e1,M,maxsize);
  48. end
  49. nr=0:M-1;plotind=0;
  50. for ky=1:ny
  51. ind=ky+(ky-1)*nz;
  52. % ** Confidence interval for the autocovariance function **
  53. sdre=2.58*(r(ind,1))/sqrt(N)*ones(M,1);
  54. if nz==1,spin=111;else spin=210+plotind+1;end,subplot(spin)
  55. plot(nr,r(ind,:)'/r(ind,1),nr,[ sdre -sdre]/r(ind,1),':r')
  56. title(['Correlation function of residuals. Output # ',int2str(ky)])
  57. xlabel('lag')
  58. plotind=rem(plotind+1,2);
  59. if plotind==0,pause,clf reset,end
  60. end
  61.  
  62.  
  63. nr=-M+1:M-1;
  64. % *** Compute confidence lines for the cross-covariance functions **
  65. for ky=1:ny
  66. for ku=1:nu
  67. ind1=ky+(ny+ku-1)*nz;ind2=ny+ku+(ky-1)*nz;indy=ky+(ky-1)*nz;
  68. indu=(ny+ku)+(ny+ku-1)*nz;
  69.    %corr 891007
  70.    sdreu=2.58*sqrt(r(indy,1)*r(indu,1)+2*(r(indy,2:M)*r(indu,2:M)'))/sqrt(N)*ones(2*M-1,1); % corr 890927
  71.    spin=210+plotind+1;subplot(spin)
  72.    plot(nr,[r(ind1,M:-1:1) r(ind2,2:M) ]'/(sqrt(r(indy,1)*r(indu,1))),...
  73.     nr,[ sdreu -sdreu]/(sqrt(r(indy,1)*r(indu,1))),':r' )
  74.  
  75.    title(['Cross corr. function between input ' int2str(ku)...
  76.      ' and residuals from output ',int2str(ky)])
  77.    xlabel('lag')
  78.    plotind=rem(plotind+1,2);if ky+ku<nz & plotind==0,pause,clf,end
  79. end,end
  80. r(1,M+1:M+2)=[N,ny];
  81. set(gcf,'NextPlot','replace');
  82.  
  83.