设为首页收藏本站|繁體中文 快速切换版块

 找回密码
 立即加入
搜索
查看: 8702|回复: 4

时间序列达人们来讨论一下matlab中predict函数

[复制链接]
  • TA的每日心情
    开心
    2016-2-29 16:07
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    累计签到:1 天
    连续签到:1 天
    发表于 2010-9-28 11:13:27 | 显示全部楼层 |阅读模式

    马上加入,结交更多好友,共享更多资料,让你轻松玩转电力研学社区!

    您需要 登录 才可以下载或查看,没有账号?立即加入

    ×
    打开程序也看不懂,600多行代码,请达人们帮助解读一下,先谢谢各位仁兄了~~
    "真诚赞赏,手留余香"
    还没有人打赏,支持一下
    楼主热帖
    帖文化:【文明发帖 和谐互动】 社区精神:【创新、交流、互助、共享】

    该用户从未签到

    尚未签到

    发表于 2010-9-28 12:40:43 | 显示全部楼层
    贴代码上来哇
    "真诚赞赏,手留余香"
    还没有人打赏,支持一下
    帖文化:【文明发帖 和谐互动】 社区精神:【创新、交流、互助、共享】

    该用户从未签到

    尚未签到

    发表于 2010-9-28 13:03:22 | 显示全部楼层
    你想自己编一个predict函数?
    "真诚赞赏,手留余香"
    还没有人打赏,支持一下
    帖文化:【文明发帖 和谐互动】 社区精神:【创新、交流、互助、共享】

    该用户从未签到

    尚未签到

    发表于 2010-9-28 17:53:25 | 显示全部楼层
    恩,贴代码上来大家看看
    "真诚赞赏,手留余香"
    还没有人打赏,支持一下
    帖文化:【文明发帖 和谐互动】 社区精神:【创新、交流、互助、共享】
  • TA的每日心情
    开心
    2016-2-29 16:07
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    累计签到:1 天
    连续签到:1 天
     楼主| 发表于 2010-10-1 20:42:36 | 显示全部楼层
    回复 4# climber
    6 {  T' T* [% ?8 l9 n" _
    " {7 U4 V" |" N! j* Z& ?3 }
    4 s  q8 e2 Z. q    谢谢大家的关注~~
    0 W( t! d2 i& r) Mfunction [yhat,xi,thpred]=predict(varargin)
    - \! a% {' O, Z) }8 C%PREDICT Computes the k-step ahead prediction.
    9 r+ ?0 ?- \# Z& n+ z. u%   YP = PREDICT(MODEL,DATA,K)
    ) f" s* H7 z7 F( M# F* x%
    7 B* U' P* l6 @. Z, N: G%   DATA: The output - input data as an IDDATA object, for which the
    0 F* B$ O2 ^! ]) p) v& w%   prediction is computed.5 s  w; p  @. E5 H; k
    %
    & p0 c( Q6 U/ [%   MODEL: The model as any IDMODEL object, IDPOLY, IDSS, IDARX or IDGREY.( B: |0 b( r8 E) |
    %, q1 t) `6 W% ~7 D( D
    %   K: The prediction horizon. Old outputs up to time t-K are used to" ?4 {% L& Z& i
    %       predict the output at time t. All relevant inputs are used.9 ]0 x+ y. Y  y
    %       K = Inf gives a pure simulation of the system.(Default K=1).
    ' _& c' I6 x# {  Y%   YP: The resulting predicted output as an IDDATA object. If DATA
    1 X6 r7 i2 G' C+ U3 n* \' y! g%       contains multiple experiments, so will YP.
    " R: ^4 c7 S' v  k( Y%
    $ l$ L+ ~' n5 U5 X& B%   YP = PREDICT(MODEL,DATA,K,INIT)  or
    3 u' X3 p3 N) {( e, y%   YP = PREDICT(MODEL,DATA,K,'InitialState',INIT) allows the choice of# t) ~1 ?3 b- p* f& Z4 p' v  X0 F
    %   initial state vector:
    , S+ C) s6 z& {! O' V- V%   INIT: The initialization strategy: one of" D2 k& {5 }7 h
    %      'e': Estimate initial state so that the norm of the
    . o! {+ I- b6 L2 p" @! Y4 `%           prediction errors is minimized.
    : S$ r4 s6 B7 E/ M& {7 [%           This state is returned as X0e, see below. For multiexperiment
    / e1 v3 t9 ?! l; y$ o, I2 ]%           DATA, X0e is a matrix whose columns contain the initial states& z1 O9 I. F0 `1 F" `
    %           for each experiment.
    * E% r! }& s- b) ]+ e%      'z': Take the initial state as zero" K: b  O  B$ Y4 B
    %      'm': Use the model's internal initial state.
    9 S& F: j( B8 M9 N, W$ r% J1 C- Z%      'd': Same as 'e', but if Model.InputDelay is non-zero, these delays
    ( a6 b% }' Z) R' |% G# l%            are first converted to explicit model delays, so that the are
    1 k! ~7 |. t; s% e%            contained in X0 for the calculation of YP.
    ( s4 y0 K2 k$ i%       X0: a column vector of appropriate length to be used as initial value.
    ; S, ]- b* @9 B. k%           For multiexperiment DATA, X0 may be a matrix whose columns give% d: y5 D2 ^, L5 k8 X/ K" g( c
    %           different initial states for each experiment.: o) d( x/ l5 a4 @
    %   If INIT is not specified, Model.InitialState is used, so that
    " ?- {- E  @, a2 n  ?; [' r! ~9 s9 S' u%      'Estimate', 'Backcast' and 'Auto' gives an estimated initial state,
    , G* m% T+ b$ X2 s%      while 'Zero' gives 'z' and 'Fixed' gives 'm'. If Model is an IDARX, W' c4 E7 Q) n( D) W
    %      model the default initial state is 'z'.
    + W# B- q: r, c  v) C%
    2 ?+ L' v; Z$ M9 y, H" M%   With [YP,X0e,MPRED] = PREDICT(MODEL,DATA,K) the initial state(s) and the- ?+ Z. P- H( u( A' b6 x# X
    %   predictor MPRED are returned.  Note that if MODEL is continuous time, X0e
    7 W) H3 g! X: }: I%   is returned as the states of this model. These may differ from the initial
    - |; G& M* v4 O" U$ g%   states of the discrete time model that has the sampling interval(s) of
    ' M0 L5 o: _; o%   DATA. Also when INIT = 'd' only the states of MODEL are returned in
    . B8 \! ]" f- x5 F; d%   X0e. (To obtain the full X0, apply first INPD2NK to a discrete time
    & u0 f) U# J. t6 F" N%   version of the model.)" }9 n* O/ A. H
    %   MPRED is a cell array of IDPOLY objects,/ G: `6 i& U+ X
    %   such that MPRED{ky} is the predictor for the ky:th output. The matching* |" N* g/ n* e1 S, ~
    %   the channels of MPRED with data follows from its InputNames.
    7 _6 v& e9 J9 ^. D1 r. n: o%   See also COMPARE and IDMODEL/SIM.$ t- t8 u5 u1 T' b; R$ w( v
    %   L. Ljung 10-1-89,9-9-94  L) _1 x7 W$ ?! ^% e' e
    %   Copyright 1986-2008 The MathWorks, Inc.: w: `- f* a3 g
    %   $Revision: 1.1.8.9.2.1 $  $Date: 2008/08/01 15:32:39 $
    5 L" i5 @. ]4 ]' v- l; k, E5 L( l# s" _/ Z
    % First find if there is any pair 'InitialState', init in the argument  I, `5 s, F5 r, O
    % list:
    ; s' R3 \5 W# Q- \. }3 onr = find(strncmpi(varargin,'in',2));  t( y6 T, V7 X8 P7 j! c
    init =[]; xi = [];
    : U; G1 Q% d/ m9 J1 l5 nif ~isempty(nr)
    ( U0 d6 V  B/ D4 E9 O$ k  Q" q% d    if length(varargin)<nr+1% k* k. u: U2 ]. m  _9 U% }' B
            error('Ident:general:optionsValuePair',...2 Q# m. ~( @* c
                    'The "%s" option of the "%s" command must be specified using a name/value pair. Type "help %s" for more information.',...
    2 Q( I' L0 u7 V% x- J/ R; Y                'InitialState','predict','predict'). a1 Z  w- Q* H+ j
        end
    % b8 W  s, ^+ g. c* o$ w. K    init = varargin{nr+1};; o  C8 W9 U5 _# w; u% C
        if ~isa(init,'char') && ~isa(init,'double')1 D/ F) e: O6 R5 Q5 X4 a; U
            error('Ident:analysis:IniSpec2',...
    - l: L6 ]) \& c9 O            'The "InitialState" option of the "%s" command must be set to a valid character or a vector/matrix. Type "help %s" for more information.','predict','predict')
    2 B% t. \# P9 f0 ?8 [/ T1 A    end5 M  V' I/ G/ H& F. \& h& N6 I7 |
        varargin(nr:nr+1)=[];%+ \  G8 R: Y3 u
    end) p7 ~+ X% i1 ~! b
    if length(varargin)<2
    " {$ p; o( O) }/ i( |' z    disp('Usage: YP = PREDICT(MODEL,DATA)')
    7 d" ^$ L) J, A6 o7 {; A! N    disp('       YP = PREDICT(MODEL,DATA,M)')% Y. H2 i/ p  h- X% H+ R' T; f
        yhat = []; xi=[];
    9 e1 M1 |. p6 r! F# _    return
    $ H7 E: b4 m* f$ ^9 ^end
    - @5 G1 |6 z6 I$ Kdata = varargin{1};, L+ Y1 R! n% f/ d
    theta = varargin{2};
    $ @% E* F$ @( B# Kif length(varargin)>2" w/ z8 m& b  t$ X4 \2 D1 L
        m = varargin{3};
    5 p% Z# T( K# {  q! P( I% Y0 W    if ~isnumeric(m) || ~isscalar(m) || ~isreal(m) || round(m)~=m || m<17 O" u: u9 v- g6 J+ O# D/ W
            error('Ident:analysis:predictInvalidHorizon',...
    : p: k) w9 @' k            'In the "predict(MODEL,DATA,K)" command, the prediction horizon K must be a positive integer.')
    , G# i8 \& I' G+ w) O    end   
    9 M' {# C$ z, C! O/ U+ `else3 c( a5 {5 j- a: {6 p: A
        m = 1;
    9 T' W' g' G* dend
    7 D7 ^4 W/ B9 Y5 v- L5 B3 Uif length(varargin)>3 && isempty(init)
    ! Z  _/ h0 c" h  t    init = varargin{4};; n9 p; H. N( b1 a6 c
    end4 O7 J  H5 A% g
    thpred = [];% j4 y! S9 k: ~4 q$ r- M$ V
    yhat = [];' ]; B3 v# Y5 |) l
    if isa(data,'idmodel') % Forgive order
    9 ~- a( t2 ?/ ~6 s, Y    data1 = theta;
    ( E" [$ w% U9 I$ i    theta = data;
    % W3 E& v7 L+ X3 t! ]$ X    data = data1;' b- y) o# W- Z/ _% G( h  A% t
    end- ~) H" C2 T) Y4 w4 ^
    nxorig = length(ssdata(theta));
    & [! Y  }* u5 s$ g- D* v[nym,num] = size(theta);0 t/ Y6 ~' t9 f; S
    if isa(data,'frd') || isa(data,'idfrd')
    $ ?# U" ~- [; d! _    error('Ident:analysis:predictIDFRD',...+ @  ?0 q- H; V7 s6 X2 x8 ?
            'The "predict" command is not applicable to frequency response data (IDFRD object).')
    - Z2 T1 h) e  f9 k/ }( [) L" m* send
    $ Z. l1 g# g8 E( p% E2 E' |if isa(data,'iddata')
    " B: e. P- T4 w1 z0 w/ X9 @: q9 s    [Ncap,ny,nu] = size(data);
    ' i# G. t: J3 F4 Q2 ?1 F; t9 E. |    if ny~=nym || nu~=num
    * E% K/ J6 ^+ T" S" e        error('Ident:general:modelDataDimMismatch',...; X6 m+ F- V9 r0 B& b5 v
                'The number of inputs and outputs of the model must match that of the data.')/ w. J6 _3 T2 g1 v6 B
        end4 E! U. L2 G) o- P4 e4 x$ ]) s& W
    end7 G# |8 @  o" S) Z8 X7 Y* e9 o
    if isempty(m), m=1; end5 w- M/ d8 r1 g1 v3 o+ m
    if ~isinf(m),! D  E8 e) c" y( w: `7 ]
        if m<1 || m~=floor(m)
    8 K# h" l8 Q# N, r" F        error('Ident:analysis:predictInvalidHorizon',...
    / o, Q. J* A+ k1 L            'In the "predict(MODEL,DATA,K)" command, the prediction horizon K must be a positive integer.')
    8 V# s0 x& I9 n5 r' z    end
    ! g, v, A# Y. b% k  eend4 O& d/ ~. w! T5 M, K% `! p
    % First deal with the special case of continuous time data and continuous5 o0 W* [8 P  `6 i& |1 P
    % model:3 i( U! V) r/ t5 b- I
    if isa(data,'iddata') && any(cell2mat(pvget(data,'Ts'))==0)
    9 h- g* ?! t2 t  n7 l    if ~isinf(m)
    ) m9 z& M/ p( \( [$ [* y0 |' f9 ^        error('Ident:analysis:predictCTData',...
    ( q( X, n! k* e% m, u            'The "predict" command cannot be used with continuous time data.'). _2 N# K: x2 J  F& J  K1 p% ?
        end
    , a( `8 |. G; M/ j6 J4 b    yhat = sim(data,theta);%没懂
    & N& ?# R2 Y1 T5 s9 b; n# T& e    if nargout == 0
    # V" w% p0 ~2 i6 d0 k9 n- L        utidplot(theta,yhat,'Simulated')4 `+ q7 T0 r  }6 ~6 U4 X
            clear yhat
    ; P, L1 X/ Q. R$ W& O. e0 a    end
    2 R! c$ z# }5 J# c* T    return/ {0 h8 M3 I+ h& j% a; k9 \$ y. F" S
    end4 F! j* B% v. ~
    if isempty(init)6 e% ]- R4 x& o* Z5 b
        if isa(theta,'idarx')" R4 R; ^. j) I  b4 J( s
            init='z';
    ) J7 _. X8 ~+ A    else
    ) r3 d8 y. Q: h        init=pvget(theta,'InitialState');
    ! d# }9 T6 q8 K7 E* e: m    end5 i. ~% v* I. t) w; u/ ?$ F: t
        init=lower(init(1));& J  r( \4 r, D0 ?" g0 g
        if init=='d'
    ; ^& D# i% Y6 H& Z  l+ L        inpd= pvget(theta,'InputDelay');. S& ?3 \5 C& ]- z
            if norm(inpd)==0
    * r+ S. K# z- P  V* y$ y            init = 'e';
    3 }2 s* k3 q2 z7 M        end0 A& a& N0 b% F% [8 X
        end+ `- J0 \% e# ]& P3 m7 s3 I
        if init=='a' || init=='b'
    9 f, V0 d+ t3 t) U3 d* S        init = 'e';( C9 d5 D! P& a6 k% w* M! Q8 [
        elseif init=='f'3 k  A. O+ V) {" N6 y0 M6 ]1 w
            init = 'm';: }) A' q! f1 e0 d, ^) L1 S
        end* t1 ?. P% A- b
    elseif ischar(init)' ^5 j2 b+ S. v5 W% S% i
        init=lower(init(1));# Z0 G+ y$ {5 h6 h* i
        if ~any(strcmp(init,{'m','z','e','d'}))
    ; k, Q+ d  l5 K        error('Ident:analysis:predictIni3',...
    ' F0 A& O2 a- W# \' I            'The "InitialState" option of the "idmodel/predict" command must be set to ''E(stimate)'', ''Z(ero)'', ''M(odel)'', ''D(elayconvert)'', or a vector.')
    3 F6 M5 ]3 y6 b" n: n) e: D    end
    9 _2 c! N. K$ j; ~% B. `9 ]    if init=='m' && isa(theta,'idpoly')
    : ]; P' s2 W$ v+ g. k; [        warning('Ident:analysis:predictIni4',...
    ) t  @$ J+ n$ m9 w- I1 C            'Setting the "InitialState" option to ''m'' in the "predict" command is same as setting it to ''zero'' for IDPOLY models.')
    $ `+ q: Y% N2 u/ N7 v- u. {9 l    end
    9 c) x) B% R$ k. w: t7 V( {  C4 T6 M) vend* c! Q8 i* G6 j1 [2 @& M
    %% Now init is either a vector or the values 'e', 'm', 'z' 'd'
    ) B+ i4 y1 M8 r) G0 Nif isa(data,'iddata');6 x% m9 u6 E* G; j
        if strcmp(pvget(data,'Domain'),'Frequency')
    ; z: v8 n2 O4 Z7 H' ]        if m<inf
    6 w/ x3 z0 o- |% p. P8 o5 c            error('Ident:analysis:predictFreqDataFiniteK',...& E+ r1 q+ G# v) E9 N4 @
                    'Prediction with finite horizon is not applicable to frequency domain data.')" M( U' Y# \8 {0 m
            end
      m" N1 K9 [( ~2 E+ A0 k: K2 x* }    end5 a/ F, y1 T  B: ]( {5 Z
    end! X* z" F6 o, b6 V0 x, `( m- T
    if isa(data,'iddata')
    $ A- R/ M; _8 B, N. C    [uni,Tsd,inters] = dunique(data);
    9 U& L; h1 ^0 ]1 r9 `6 j! H6 |7 p    iddflag = 1;
    1 S$ P1 P) s2 T' Qelse
    " M4 N) S7 `" x    iddflag = 0;0 e! `5 N& s( t$ L8 }
        uni = 1;
    3 R; a2 w+ a  F' s    Tsd = 1;
    ' h' Y' V& ]# ]' ~    inters = 'z';. a( G# V* C( V4 b8 \
    end  E: k! ~% l& l: K+ \3 X
    %% Do away with continuous time right away:) C4 M* E+ x8 _% F' z' E* z
    Ts = pvget(theta,'Ts');. ^" J9 f; I( u% L$ r
    if Ts == 0% w6 h2 j5 g$ b5 b
        theta = pvset(theta,'CovarianceMatrix',[]); % Less calculations5 {" \1 ^( T' R) T, l5 f! C' Q1 Y1 g
        ms = idss(theta);
    $ L' J+ l. K! l( j9 |% ^    nc = size(ms,'nx');* q: O) V) S6 B* j  M
        if ischar(init)
    1 k$ b* q; \& c! s. i# C, W3 O( J        if init=='m';
    7 v: h- c, A7 K7 x% j" k* Q            init=pvget(ms,'X0');, D; }7 Q2 Q- T1 I7 ?4 d3 Z
            end/ u/ c6 k7 I# E# {. e" G, N
            if init=='z'
    ! S3 q0 k' |& J( e  {1 H2 Z            init=zeros(nc,1);
    0 v4 L! _0 [( {; r        end
    & W2 L* x7 h1 y    end
    9 J7 N( h4 U' q' D2 g( ?2 g! I    if ~isa(data,'iddata')
    + w' R5 W4 f1 e        error('Ident:analysis:predictDataFormat',...& H5 a: `2 ~0 q% ^4 m) n  x
                ['For a continuous time model, data must be specified using an IDDATA object.',...
    ( m4 J( N3 x! U' _6 S4 P  }$ c            'Type "help iddata" for information on how to create the IDDATA object.'])
    " U* M; v2 k, O) x1 l    end
    1 c$ e' D1 K& D    Nex = size(data,'Ne');
    % s6 _1 n5 r2 X. t5 C1 J; r    u = pvget(data,'InputData');) }  c& z  y# h! e' j
        u1 = [];
    . ^! t- Z8 S8 y2 z+ o! u    for kexp = 1:length(u);
    ; {% b8 l# {# N( |+ X        u1 = [u1,u{kexp}(1,:).'];
    % T. J* T3 ^1 }* N3 i! N    end
    - t) M  l# R* k  A* X* H, H    nu = size(data,'nu');  e4 [3 Z/ L5 D' |- I8 \( ?. y
        if uni
    6 {' k3 D. J" V8 H/ e8 y. J; z        nc = size(ms,'Nx');  |7 E* ]" M* U9 @2 H) ]8 V# Z
            [md,Gll] = c2d(ms,Tsd,inters);& p9 U1 b4 b+ `" V, Q
            if init=='d', md = inpd2nk(md); init='e';end) h7 e1 m7 ^& V# V
            % First work on init
    ' s) F" ]' n5 S" i+ ^        if isa(init,'double')
      c& \  k5 H- ~! q7 l            [nxi,mexp]=size(init);
    2 \6 w7 @+ Z( f0 ^, S4 _            if nxi~=nc. f6 K6 c% a$ \
                    error('Ident:analysis:IniRows',...( v4 M1 ]( ?; c4 ]! u1 {* \6 e
                        'The value of the "InitialState" option should have %d row(s).',nc)
    : x( K  G* x9 ?            end" ]9 T/ v+ x  f+ }9 ~% z
                if mexp==1 && Nex>1
    ' `+ {: [# e3 f$ B; d8 y! X* z1 ]                init = init*ones(1,Nex);1 a" z! q) l% P( K9 d( y7 y8 c& S
                    mexp = Nex;
    ' A; L9 T8 \5 L7 C4 O            end) |" T: i1 T: x
                if mexp~=Nex
    $ c; W# K; E( }- o. m                error('Ident:analysis:IniSize',...
    6 H; s- O5 H* {- e                    'The "InitialState" option value must have either 1 or Ne columns, where Ne = no. of data experiments.');3 h* S/ f" f6 T. W
                end
      @0 G% C1 s. C9 P4 u            if ~isempty(Gll)
    : t( c. x( R1 Z$ b6 e+ A! ]7 A                init = Gll*[init;u1];) N; l5 R  M, A2 [
                end
    4 W) N) r% a* }  k) e8 v! Z        end
      T. E& r. C/ h- E        if init == 'e'
    % k, {) ?9 Z& L! d            inite = x0iniest(md,data);
    ) Y2 D1 Y; I$ z" r3 g4 |! |/ v& R9 a        else
    7 s+ A9 i" b7 K4 l" B2 `" H8 {& h            inite = init;
    5 r" \2 R, P( G% Y4 \. k        end
    3 B' T; b% B! e$ }) ~9 X2 a% Z        yhat = predict(md,data,m,inite); %Check XIC!( M% w" d1 Z2 Q* a
            if ~isempty(Gll)
    " l6 Z3 L; W2 A; b" A2 F, k            xi = inite(1:nc,:)-Gll(1:nc,nc+1:nc+nu)*u1;* R- b2 h! u# w3 i5 h! k# I
            else# B5 P: |/ n( c6 }- j& Z
                xi = inite(1:nc,:);
    : h/ T1 p/ K  O5 z        end
    , C* u: B" T- Q  |  t* h    else % Different sampling intervals8 g; B8 _0 L3 P* y1 [9 I' d
            Tsd = pvget(data,'Ts');
    6 ^* B' D- V$ x$ F9 x- Q        ints = pvget(data,'InterSample');
    2 v. _* `4 X3 C# ?7 d* U. _        %xic=zeros(nc,Nex);
    ' N# R  ?' B4 G0 H0 R* b        for kexp = 1:Nex
    - [( e# S7 h) \9 }            [md,Gll] = c2d(theta,Tsd{kexp},ints{1,kexp});2 F% i; ]: n; m* I$ _' ?
                if init == 'd', md = inpd2nk(md); init = 'e';end0 D; V3 T$ b% {% F2 w9 K
                if isa(init,'double'): o, K0 t5 n: H0 f
                    if ~isempty(Gll)
    6 g8 W0 |: e  U5 {' ~                    if size(init,2)==1
    1 [6 ]: F* T% ]& U- g* L# g; d6 o                        initk = Gll*[init;u1(:,kexp)];
    $ K# x2 a% Y+ `# E5 A                    else4 Q# X4 G$ Y2 b) {6 _: }4 e
                            if size(init,2)~=Nex4 f* A- R3 |. Y5 ?5 B: R$ j; }4 _( c
                                error('Ident:analysis:IniSize',...0 `- u  @% ]2 u/ @4 r6 O9 U
                                    'The "InitialState" option value must have either 1 or Ne columns, where Ne = no. of data experiments.');4 P  X0 B8 s0 R9 w
                            end
    3 m4 P7 f* i& A9 `8 u3 b" v8 x" b" W                        initk = Gll*[init(:,kexp);u1(:,kexp)];
    / e; D# z( S* T# {1 G                    end
    1 f0 N: g3 z- Y  L3 V3 g% h                end
    : Q1 ~, @$ v$ `( Y& D            else+ d) o( e8 P: S7 P4 a7 n  y
                    initk = init;
    ( u. n4 D5 g9 v& S4 h6 Z- n5 s; h            end
    ' h+ I& }8 J- C$ j0 k, W( M            if init == 'e'
      d. t2 y/ p9 G; U4 X                initk = x0iniest(md,getexp(data,kexp));0 o7 _% A- n& N1 B; y0 P
                end0 u0 d. C# ?8 T' c; e. \* `/ |: z
                if ~isempty(Gll)2 d; q/ M% s0 d6 b
                    xi(:,kexp) = initk(1:nc)-Gll(1:nc,nc+1:nc+nu)*u1(:,kexp);
    * K* H4 n1 r( F- m6 A# H            else
    # ?% i- _; F5 Y& Q                xi(:,kexp) = initk(1:nc);! S& b# F- n/ U
                end
    ( W" z' c" D/ _$ L' B: ~            yhatk = predict(md,getexp(data,kexp),m,initk);
    ! k+ D  \3 B# N            if isempty(yhat)9 B; m# q; r6 k' f: j" m
                    yhat = yhatk;
    5 P! I( Z+ R( j6 ^8 Z3 [9 N+ x+ Q& O! x            else
    ! @3 |$ y( f+ B                yhat=merge(yhat,yhatk);
    0 u+ s0 {( t, f2 G! Q            end$ r+ q" p* F2 X1 [
            end
    5 p' t8 P$ G* n; m( F) [    end
    7 a0 o9 Z1 z' _) _    if nargout == 09 ?1 Q$ {7 T, j/ C6 ]8 m
            utidplot(md,yhat,'Predicted')
    ' \* l8 n8 P4 z) d        clear yhat/ b3 V! ^" k$ M
        end! e( `# w7 }! z! r7 w1 f$ A2 a
        return
    . T( f; d0 F& b, S8 y- t7 Relse %Ts>0! F( }# `# {9 P+ z% i
        if init=='d'3 _2 q2 e' @" {4 M
            if any(pvget(theta,'InputDelay')>0)$ k& k+ f# }) V6 D4 }+ O6 I( p. Z8 r2 Q& ]
                theta = inpd2nk(theta);6 d/ L( f4 [) }. B, r% l; T# ~/ ^. J: }
            end
    8 x; x- b7 q8 W. H& E        init = 'e';! v. |0 i+ O& j& M
        end
    : J: Y5 _; t9 Y# Fend/ s" U- x; E" }& p% T8 J  C
    if isinf(m)* j" v. K- T* ?" W& z# d8 s
        if ischar(init) && init=='e'$ d; j. _3 N' L& b) R" p
            X0 = x0iniest(theta,data);
      e, Q8 p! @2 c- T- [; }9 u) \) j        init = X0;  % Note the difference with COMPARE, which fits
    : }1 {1 d; W: X8 c3 o        % X0 with K = 0;
    . |6 g% `! a3 w- n    end1 }: Z5 t1 S  Z! M% ?  |& ?
        if ~isa(data,'iddata')
    % A* j1 a2 {, _- A        data = data(:,nym+1:end);
    5 R2 ~% [; J+ j' l. k    end
    ( z+ L' L: c. }" G2 J# i4 J    [lw0,id0] = lastwarn; was = warning('off','Ident:analysis:unstableSim');$ I2 M% P( F* v
        try
    : }) [9 n2 m# a! k        yhat = sim(theta,data,init);- v; R- K" Q% {  X# U
        catch E/ @3 @  _; }( N$ e4 K  H
            warning(was)
    % ]5 f* D9 r6 S        lastwarn(lw0,id0);3 w! _3 l8 p3 M4 J0 }+ k
            throw(E)
    ) @0 G0 m* U% F9 {( b, t# g    end0 o" S' K9 L8 m) E* S
        warning(was)
    ( B) o' C1 S+ h6 ?    lastwarn(lw0,id0);
    9 n' F8 h9 S& H! T1 ^   
    1 h; P2 {. @$ y1 C. R" a# G    thpred = theta;
    ; r  ]! x5 |) P  U1 R0 b7 s& m0 a0 O    if nargout == 0. F% q5 K3 i$ o4 [5 L4 l
            utidplot(theta,yhat,'Predicted')! Q( N5 x/ J' s  y5 O
            clear yhat
    5 @3 ]' `/ f- t    elseif nargout>14 n0 u5 F. A; x8 J: _
            xi = init;. ]0 H8 I7 [7 Z4 n. [
        end
    ; m" a- F) o( l# }1 W    return9 Q; n0 Z$ m% X. C# s- M& F
    end
    3 @: t" F4 t* a4 o- ?* W2 n%Inpd = pvget(theta,'InputDelay');
    * {% R, \' h8 _1 F- ]9 cnu = size(theta,'nu');
    ; x( ]' Y% Y5 ]. ?if ~uni
    * Z' A& P0 C9 \2 n; f; k    Tsd = pvget(data,'Ts'); Tsd = Tsd{1};
    ( I4 {. v2 d! Z2 t2 b. U    warning('Ident:estimation:nonUniqueDataTs',...
    # k+ T! m1 n6 g        ['The data set contains experiments with different sampling intervals.\n',...
    3 b0 Q) _+ Y0 _3 U1 j  ^, Q        'Sampling interval from the first experiment (=%g) will be used.'],Tsd)
    & N( ~: \5 _! Qend3 E* k9 _$ i3 G5 M* r
    if iddflag && any(abs(Ts-Tsd)>1e4*eps); |8 O# _& }* V/ t+ b: S6 J! L& b
        warning('Ident:analysis:dataModelTsMismatch',...4 D$ y/ t, q: b
                'The data and model sampling intervals are different. The data sampling interval will be used.')/ L. U2 Z2 D9 H
    end/ K. _/ M7 v: a1 Y/ c8 s& W+ ]4 b2 r! ]
    Inpd = pvget(theta,'InputDelay');1 G- J! Y6 {0 R/ \% c
    if init=='d'
      j% h, V6 `- q9 t! t$ p' i    init = 'e';$ }0 M2 h- b9 j( S$ F
        theta = inpd2nk(theta);! d: Y  Y  d5 z* p& g3 O! ?' g
        Inpd = zeros(size(Inpd));
    8 z1 }# i) c# Q' _% E- f, lend
    " Y4 C% G6 d" S5 Yif isa(data,'iddata')
    ( ?* y1 t: Q. J    if isnan(data)- J# ^) v  [+ }# b8 Y, e5 M
            error('Ident:general:idprepMissingData',...; }# _$ r# g% p+ k  j/ \$ \
                'Data contains NaNs which represent missing values. Use the "misdata" command to fill in the missing values before using.');+ Y6 B0 k* X! F* q- I4 }
        end
    5 _+ Q6 J# B1 r. f/ a2 }    data = nkshift(data,Inpd,'append');" |1 D* g* o6 J3 G6 Z' I
        theta = pvset(theta,'InputDelay',zeros(nu,1));
    3 s' s# L- g6 r. {( G6 m) u, R    [ze,Ne,ny,nu,Ts,Name,Ncaps,errflag] = idprep(data,0,'dummy');9 _2 X1 A. e9 p) L: x- n. _
        %if ~isempty(errflag.message), error(errflag), end& k3 F8 t. t$ z5 Z
        if ~isempty(Name), data.Name = Name; end2 f! P' G; J; r
        iddatflag = 1;9 Y& q! x1 K* Q- T  ~3 W
    else- d1 n. N! p: C
        if norm(Inpd)>eps  I! R9 _7 w; G" t
            if iscell(data)  4 Y8 @8 _7 l9 ~, Y
                data = data{1};
    / _& P- H' u/ Z: n        end
    $ a$ N3 F, [( D4 x+ F( e        [Ncap,nudum] = size(data);
    * @7 H' i& l5 D        nk1 = Inpd;  L+ v2 Q9 v6 m9 r% v% j+ k0 q! k
            ny = nudum - length(nk1);8 i; ?# Q+ x: R" Q/ P7 b
            Ncc = min([Ncap,Ncap+min(nk1)]);
    6 o. b: \* y4 |' B7 y) F! b        for ku = 1:length(nk1)
    ' d% g" H, _+ v            u1 = data(max([nk1(:);0])-nk1(ku)+1:Ncc-nk1(ku),ny+ku);
    0 h% G3 [$ M! Q5 g/ s# k" A            newsamp = Ncap-length(u1);
    , W9 I2 e0 O) _% c% ^+ n            if nk1(ku)>09 w2 q+ d0 P: C) a8 t3 ?5 e
                    u1= [zeros(newsamp,1);u1];
    7 V* k' ?$ c( B' ~            else# a/ b. Q+ U2 T% `- }3 J
                    u1 = [u1;zeros(newsamp,1)];+ H. H; l' V# E: l
                end8 }  B7 K. T4 N6 x2 A* g! `
                data(:,ny+ku) = u1;
    8 ^. Z5 P) C/ u- u; @+ N% S        end& h! c0 A2 E8 _% l& w$ U3 c
        end
    4 Y8 \: u0 d- Q, @6 p9 g    iddatflag= 0;+ W$ s, P! {& I0 {. i. C/ P
        if ~iscell(data), ze ={data};else ze = data;end6 V/ \! Q- d3 r! }. Q7 H
        Ne = length(ze);4 @, d* F( z; e9 U  f7 K
        nz = size(ze{1},2);. o8 L2 T; O0 K4 ^* `& [9 S/ k" H
        for kexp = 1:Ne
    ( e0 B1 |) h& ~        Ncaps(kexp) = size(ze{kexp},1);8 E8 m- c- Q9 A7 `! r
        end2 P  @9 W- F, [& t. l" M, ^. h
        %Ts =[];
    * I) ?4 H' L& r' hend
    ) K) x* S+ K! ^if m>=min(Ncaps)
    $ w$ |' X. _) l- M" H    error('Ident:analysis:predictLargeK',...
    5 N7 {4 f! u) X9 X        'Prediction horizon must be smaller than the number of data samples.'). w. a$ \9 W6 o5 r+ E
    end3 e% O4 n5 f$ U7 X( \3 f5 }
    [A,B,C,D,K,X0]=ssdata(theta);: u2 |( Q4 ~- ~0 a; |0 `
    [nyt,nx]=size(C);nut=size(B,2);( z. R2 o: _# v
    if iddatflag
    + J6 ?& q) r5 Q% k    if ny~=nyt || nu~=nut/ a5 ^1 V& Q- \) e/ J9 `
            error('Ident:general:modelDataDimMismatch',...
      U6 z1 f4 X8 D2 [5 Q* p            'The number of inputs and outputs of the model must match that of the data.'), `3 p  e( V* v) W) n& |5 T
        end+ S( N8 v2 [# p/ e1 z
    else" b# s# M" `* `+ e* j
        if nz~=nyt+nut
    9 k% L% P. y! x- N0 n3 \        error('Ident:general:modelDataDimMismatch',...4 k0 j, m: v  U# ]+ m) [/ F( p
                'The number of inputs and outputs of the model must match that of the data.')# `$ B1 [6 V" D. i
        end/ X. \9 }, ]7 ?0 o% ~/ ]
        ny = nyt; nu = nut;
    / q, n7 c4 ]' [$ T! ?+ send5 |+ ]3 r9 w, k" j0 |- w
    if strcmp(init,'e')
    ! g* L# D1 B5 d  ?( z    X0 = x0iniest(theta,ze);
    ! ~$ l" j5 |; ^. relseif strcmp(init,'z')
    & v" k$ F& t+ R, {    X0 = zeros(size(X0));9 M; D7 _' V$ X" Z
    end4 v0 g5 I9 p6 n4 P( G
    if ~ischar(init)8 F/ a/ n$ \4 q3 T
        X0 = init;# _" f  ?+ n3 M
    end
    8 ^, V$ M. k) W9 C" Mif nargout>1
    8 N0 i/ M1 N7 a  X" t4 q' J4 u5 V    xi = X0;
    - e7 ~7 I5 O3 B* iend
    ; I/ d; O2 Z! U0 Y0 y[xnr,xnc]= size(X0);& \, C) z/ u& B. @1 q6 E
    if xnc~=1 && xnc~=Ne
    " g' l+ Y* {- w, ^1 F    error('Ident:analysis:IniSize',...; {( D+ i" W: A9 s9 s3 R
            'The "InitialState" option value must have either 1 or Ne columns, where Ne = no. of data experiments.');$ r( ^. k" e3 ~) Q
    end
    ; Q1 Z1 ~& K9 ]8 h# s* _if xnr~=nx
    $ S7 U. c6 U7 d5 T4 v    error('Ident:analysis:IniRows',...- T) U- t9 m6 A' Q$ s
            'The value of the "InitialState" option should have %d row(s).',nx)
    ' ^# t2 v1 o6 @end* z, E1 t- ~" `( P/ t# g8 U
    if xnc==1 && Ne>12 @6 `& |: u+ m- u
        X0= X0*ones(1,Ne);1 c, d7 x" u$ }: t3 A# h
    end3 ?& b) X( X( |% h  M& j7 ]
    for kexp = 1:Ne
    ( H; K6 r: w$ x) s% z, G- }    z = ze{kexp};
    2 y  z: C1 d1 g( g    u = z(:,1+ny:end);& ]5 b6 H( Q; W# \+ o6 Z
        Ncap = Ncaps(kexp);. N- C$ z4 Q0 z9 w) S
        if m==inf,( S5 b- V7 V9 P. D) _( w) x2 u
            yhat=sim(theta,u,X0(:,kexp));
    , G* l: a# w7 B0 a: J% c    else
    : @+ T1 S* S+ l. b  _+ Y        x=ltitr(A-K*C,[K B-K*D], z, X0(:,kexp));
    ; ~- K( y/ y/ g" J, @9 n; w# k        if m==1,
    , ?) w& ]9 y& d            yhat=(C*x.').';
    , ^3 N  Z  Z$ U) O5 x& P            if ~isempty(D),yhat=yhat + (D*u.').';end# R( \9 o; ~* [! s' L- T# t! [& f
            else2 y  a2 g! ?. w4 m" ?- n8 X0 P4 m" c
                F=D;Mm=eye(length(A));5 s! f  x0 C5 ?
                for km=1:m-1- M9 ^) ?+ e, B- k) s; {# h/ P0 H1 Z
                    F=[F C*Mm*B];" ~- H' T. K' Y1 @8 o2 ]* ~0 c+ |! z
                    Mm=A*Mm;4 W6 E+ D0 k2 e1 q6 i
                end1 }% p, _! i/ j/ v3 f* ?
                yhat=zeros(Ncap,ny);%corr 911111' G6 t2 U& i* c% }9 D0 V! `& Y
                for ky=1:ny
    : j: t% _& g  w% d" D. H                for ku=1:nu; c; r% {5 B. r! J% n
                        yhat(:,ky)=yhat(:,ky)+filter(F(ky,ku:nu:m*nu),1,u(:,ku));$ u- |1 r9 I2 Z( K; b# [2 a5 i
                    end
    % t3 q; _$ s- ^7 B0 d: ?            end
    4 x( z( \# o$ f- a9 ?- x" Z            if isempty(yhat),yhat=zeros(Ncap,ny);end
    1 n3 L  O$ c6 X1 _8 v            yhat(m:Ncap,:)=yhat(m:Ncap,:)+(C*Mm*x(1:Ncap-m+1,:).').';- ~1 Y/ x' l: m" N
                if nu>06 Q0 Q1 B6 [) d) X& u
                    x=ltitr(A,B,u(1:m,:),X0(:,kexp));& f1 n! a, e# W
                    yhat(1:m,:)=(C*x.').';) b- F# C/ H3 @+ T- L- J: H* H
                end$ x& p0 q2 p! h1 j6 ~0 E3 J
                if ~isempty(D),yhat(1:m,:)=yhat(1:m,:) + (D*u(1:m,:).').';end
    ( R: C- O: l* }% ~        end
    , e$ j8 B8 ]& E9 D    end- Z: s( v' B  p3 z& l
        yhatc{kexp} = yhat;
    % `8 ~- l. M1 P9 B* k4 d& R' Eend  H& j* K- \4 G. ~$ I7 ]2 x7 x
    if iddatflag4 \( }% g, e* @; H2 m% o: x/ A
        yhat = data;6 l& W  s4 d2 \! I% ~- R
        yhat = pvset(yhat,'OutputData',yhatc,'InputData',[]);
    ! j- F3 d$ }1 w# N/ W1 E7 yelse. @  o: e* y" W! Y0 O
        yhat = yhatc;6 {0 @4 |( `/ Q# Q  a
    end2 l3 w/ `0 w# s0 p2 i0 c
    if nargout >2
    $ q6 t3 t1 |& Z! u    thpred = polypred(theta,m);
    + U6 q( E0 u  D4 L. R) {1 pend
    6 D( h3 }& b  C) }9 N) eif nargout > 1  P+ E5 }2 P- z9 {2 t9 ?
        try
    ) Z+ q4 _3 ~% R        xi = xi(1:nxorig,:);$ V" b0 D) t. R) A7 |
        end
    / y! }- Y$ m1 A4 G; Pend
    9 q# [) |: U. \* [& ^if (nargout == 0)$ U% n: w( k8 y
        % Plot y and yhat.: C. e4 v# O# O) V- y: Y0 Q% e9 i; m7 m
        utidplot(theta,yhat,'Predicted');0 A( W" Y. b8 }1 P5 g; q
        clear yhat x0;# V+ ]$ u( E5 }0 y5 y0 F  V
    end
      _0 R6 G& z( ~, r3 i8 q%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%$ P# l, O+ C; H: e* U- Y
    %%%%% LOCAL FUNCTIONS+ j7 p" k! Y1 [: k# ?. ?4 A7 M/ t
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5 x' {' p6 g$ n8 q. M( c
    function xi = x0iniest(m,data)* l4 `  `: O9 `9 E9 q% ?2 T
    if isa(data,'iddata') && strcmpi(pvget(data,'Domain'),'frequency')0 B/ R8 s- g. |, z( d6 @5 b
        [dum,xi] = pe_f(m,data);
    % S5 E" W( `: S& J" z; G    return
    3 U4 _1 v/ r7 z& dend
    # a9 l1 q4 k& oInpd = pvget(m,'InputDelay');
    ! D$ m7 |5 c9 Q: }if isa(data,'iddata')3 a. S+ S' e; ?: V" ^, M8 L
        data = nkshift(data,Inpd,'append');
    , C9 f0 Q1 f+ F* I0 h- ^) I; ~    [ze,Ne,ny,nu,Ts,Name,Ncaps,errflag] = idprep(data,0,'dummy');
      j% O- U% e- C1 A( X( r4 M    %if ~isempty(errflag.message), error(errflag), end( H# l9 V# S2 _3 n9 _
        if ~isempty(Name)" f% y7 [6 p% a; V
            data.Name = Name;3 G; K8 T" {. B) a/ |  _& X3 O5 k
        end
    + m) L$ E- L* v  felse- t  i8 `4 g3 [4 D% |
        if iscell(data)
    8 C6 P2 o' ^' `! E) B1 Y        ze = data;
    ( r, T( x4 U0 |: `6 K% G    else: e! W; u/ G. B9 k) g) Z
            ze = {data};
    $ N  \. b+ Z8 Y8 T3 S    end, G/ B- n3 L* d! T0 o5 N) f/ `
    end
    3 \+ s3 D5 C, V3 ?& t5 xalg=pvget(m,'Algorithm');
    & r: f2 a9 S9 Y& S* z& F1 Rmaxsize=alg.MaxSize;
    8 P9 ]& y) `+ R* E: g+ n[A,B,C,D,K,X0]=ssdata(m);" i( l8 g% F* O( p, y+ M
    nx=length(A);$ |  I4 e' }. X
    if ischar(maxsize)
    9 _5 W& q* h0 F9 A1 G    maxsize = idmsize(length(ze{1}),nx);
    8 @* u8 g7 ?  D3 d5 ^end
    % k/ G1 A8 b' b5 `4 y, |; sAKC=A-K*C;" ~; g- R% I( I" L2 }
    [ny,nx]=size(C);( m' Z+ J$ B' z( g1 o
    nu=size(B,2);# b4 U8 e* U) d
    el=zeros(0,ny);# M: V' A. K& {. B
    xic = zeros(nx,0);& p' o) d2 W, r0 \3 ~0 u1 t8 g9 h
    Ne = length(ze);; [# a& c& u& d: Q4 ~1 f% L: ~( t' A
    for kexp = 1:Ne$ C* {( V8 ~# X; E8 Y. k+ O
        z = ze{kexp};
    5 ]1 R' h7 |) {* O    [Ncap,nz]=size(z);
    5 v3 N$ R! |2 [4 s" ?: R    if nu+ny~=nz5 a! g6 c& T( b; ~
            error('Ident:general:modelDataDimMismatch',...
    7 K, X# h( w/ N! d8 K2 r            'The number of inputs and outputs of the model must match that of the data.')4 F) d  J/ P9 K  s, o9 b2 H
        end
    ( L/ `) p2 `* M9 L! l    nz=ny+nu; [Ncap,dum] = size(z); n = nx;. F$ w) ?! E2 `/ i6 Y$ j" @
        rowmax = nx+nz; X0 = zeros(nx,1);
    ' f6 A7 s9 z1 V. b  B1 ]/ h0 q* \    M=floor(maxsize/rowmax);# ~' j5 `  I& K4 M6 u) D& ]- z9 d6 g. Z
        if ny>1 || M<Ncap
    ( L$ ^# q# {, V, P        R=zeros(n,n);Fcap=zeros(n,1);R1=[];
    % n# G% h8 S* ~2 p, p& R        for kc=1:M:Ncap
    5 j" e) g! t3 {$ e! b            jj=(kc:min(Ncap,kc-1+M));8 }! F) H* x6 j3 c- `) x
                if jj(length(jj))<Ncap
    ' b3 w: C) i* P5 M                jjz = [jj,jj(length(jj))+1];
    2 P& ~$ ]0 K, N! L9 U; p            else
    : A$ s; n" q5 q- I/ S                jjz=jj;5 h1 ~: L8 [, M) e! A+ l0 F
                end/ G' Y4 [2 h+ E* F
                psitemp=zeros(length(jj),ny);4 C) I, z  _& s# n! O
                psi=zeros(ny*length(jj),n);: l; w4 _  q& m5 n8 q' U
                x=ltitr(AKC,[K B-K*D],z(jjz,:),X0);/ q  b7 P4 |' t9 t8 {+ @4 R' M
                yh=(C*x(1:length(jj),:).').';
    " H( `& Q% y4 p7 \' }9 F- y            nm=pvget(m,'NoiseVariance');. A. [3 ?$ E. y! p7 a1 B/ Y. O
                if isempty(nm) || norm(nm)==0 % To handle models without noise
    ( _; g  s+ H, W7 }6 `% t8 P                nm = eye(ny);
    8 S# x4 `6 c5 R; W            end
    ! l8 R+ a8 `7 u) h3 G8 M, q0 r            sqrlam=pinv(sqrtm(nm));( y' O4 T2 I6 B! P/ c' Z
                if ~isempty(D),yh=yh+(D*z(jj,ny+1:ny+nu).').';end
    , `& {4 b4 w' q. ?2 U            e=(z(jj,1:ny)-yh)*sqrlam;" R& G  T+ b( q  o7 H/ @/ s
                [nxr,nxc]=size(x);X0=x(nxr,:).';. p* F) P4 S; U# ~* Z! Z" O
                evec=e(:);( _1 f9 p5 X4 H) y/ M3 a
                kl=1;
    9 O% [% X0 a/ G# ]9 _" Q            for kx=1:nx
    8 h4 V4 b# ?& `! d! g                if kc==16 x! w9 s" ^- K; `$ p4 e( W
                        x0dum=zeros(nx,1);x0dum(kx,1)=1;
    & M5 `2 e- Z# n1 q9 G7 X* ^                else" q) ~0 L% }/ E! b. z( I2 ], g
                        x0dum=X00(:,kl);; Y* J4 I' x/ F  k* h/ B/ Y
                    end
    ! i. |% H: A& D4 w* k& z                psix=ltitr(AKC,zeros(nx,1),zeros(length(jjz),1),x0dum);. I4 ]* H. C; J: a0 B4 \4 {
                    [rp,cp]=size(psix);
    % U& ~4 C' v' O7 o! J: \                X00(:,kl)=psix(rp,:).';0 B1 ?7 n  G' y1 l3 R% U( p
                    psitemp=(C*psix(1:length(jj),:).').'*sqrlam;
    ' |4 s/ h/ D( Y4 o* b! r) z                psi(:,kl)=psitemp(:);kl=kl+1;
    % P: E  A8 R9 U2 e8 ?            end+ {$ [6 I" N9 \: s2 L, z
                if ~isempty(R1)
    ( Q8 ?) G2 X" V' p! h                if size(R1,1)<n+1
    & A: F( ?# _9 s+ L                    error('Ident:estimation:predictSmallMaxSize',...
    $ m7 g+ C3 b; ^, W% x- D                        'The value of the algorithm property "MaxSize" is too small to estimate the initial states. Either increase MaxSize value in the model or set the "InitialState" option of the "predict" command to a value other than ''Estimate''.')& P* h% c* y- b
                    end
    , R- G2 Z; d3 c& a5 }9 }                R1=R1(1:n+1,:);
    - y/ T" L, z' X3 B/ N            end
    6 ]6 H  n3 G# P8 E3 O            H1 = [R1;[psi,evec] ];R1 = triu(qr(H1));
    6 i6 M3 _1 Q( v# J) m# C        end9 A! w* _" N3 r( O# ]. b- t
            try
    # u; w: U% A5 c: _0 q            xi(:,kexp) = pinv(R1(1:n,1:n))*R1(1:n,n+1);
    0 c& t) u+ W% _) w4 T        catch
    $ J" o) E- z/ u6 V8 J* c7 V4 t            warning('Ident:estimation:X0EstFailed',...
    & L! M5 x% ?2 A  R5 B                'Failed to estimate initial conditions. Check model stability. \nThe "InitialState" option has been set to zero.')
    ! K: K8 V4 |+ e# a- x- k! _! @: @            xi(:,kexp) = zeros(n,1);
    9 a, M: z8 ^. n2 O  i! W. v        end6 m1 ]- P! \( H4 M' b1 \/ |
        else
    % \; j- C" V3 [5 U& {& [1 F        %% First estimate new value of xi4 x9 H4 J6 y& v: H" P! Q" w7 g6 o
            x=ltitr(AKC,[K B-K*D],z);( D% @9 ~. I$ t+ n
            y0=x*C';
    9 A7 q: ~1 I. r  l        if ~isempty(D),
    ( w& j/ X# Z- V! E8 E3 \" k" R            y0=y0+(D*z(:,ny+1:ny+nu).').';/ j8 B# z. {& F& @- k" p
            end; E! Y" I, l7 k3 T4 Y0 F
            psix0=ltitr(AKC.',C.',[1;zeros(Ncap,1)]);, j0 l4 V3 o, o! [; f/ u
            psix0=psix0(2:end,:);. j8 n4 f, M) j3 e' z# {( P
            try- {( W. p2 Q$ W& [2 O/ P2 y2 H$ e
                xi(:,kexp) = pinv(psix0)*(z(:,1)-y0);9 K9 u2 C: m' |& Z+ U
            catch# q, y9 o- [! O( m  m" M
                warning('Ident:estimation:X0EstFailed',...- W- s9 b, y# k1 R( h8 s: _5 }7 F  P; b
                    'Failed to estimate initial conditions. Check model stability. \nThe "InitialState" option has been set to zero.')4 K, S) |( r9 Y  _) w) b  _; v/ D
                xi(:,kexp) = zeros(n,1);, H* I2 v" G- d) Q6 q# J. F9 ?9 T- h8 ~
            end. [4 |" P3 [, G, `5 S( a+ A
        end
    5 ?$ s- }; X- qend
    4 Q  S$ V1 C" O%%*************************************************************************3 ]4 t. A( t3 N: _
    function thpred = polypred(theta,m)
    + }! `- Y" e6 z% Note that cross couplings between output
    0 q" ~3 [4 i2 x/ b* V, e7 x% channels are ignored in these calculations.* r& f# ~: @7 \
    [ny,nu]= size(theta);
    " L* w: P; v% |8 Kyna = pvget(theta,'OutputName');
    ' E5 E' |) l! k! y3 cuna = pvget(theta,'InputName');
    0 n. Q& p( Y  S  e7 W) Vyu = pvget(theta,'OutputUnit');" @# k' E0 p! l* U/ q+ u; E
    uu = pvget(theta,'InputUnit');0 f/ R; ^# B, `1 T2 ^" ~
    for ky = 1:ny& O- @! X! f) z" A' ~& k$ x3 b
        [a,b,c,d,f] = polydata(theta(ky,:));' R3 n' w2 s2 ]! O, _) @
        if nu>0# S( g5 y$ [) ]& X4 a
            ff = 1;" Q. q# y4 M" D5 |
            for ku = 1:nu,1 w! i! F5 Y; x( u* z
                bt = b(ku,:);
    8 I5 L5 I0 ]7 v1 k7 u            for kku = 1:nu, if kku~=ku, bt = conv(bt,f(kku,:)); end, end; E  `, Q! Y3 H( T
                bb(ku,:) = bt;' l9 O- e1 T/ v- P/ d
                ff = conv(ff,f(ku,:));( I7 n) C7 T( P6 [
            end
    ! x- m2 s0 A; ]) P( [9 M# b1 P        a = conv(conv(a,ff),d);c=conv(c,ff);
    & X8 w' M) K" _9 y$ o% D    else2 D' V0 q7 E1 H$ q
            a = conv(a,d);4 h/ f# m" @* T
        end
    9 [; Z& ~4 [7 w# m6 A" x    na = length(a); nc = length(c); nn = max(na,nc);
    8 ?. O; w0 i. n4 P! C    a = [a,zeros(1,nn-na)]; c = [c,zeros(1,nn-nc)];( s- @. ~% t( Q  t5 T- @# t# b
        [f,g] = deconv(conv([1 zeros(1,m-1)],c),a);
    3 ~8 o# L, A+ j2 c3 i    ng=length(g);
    , l: h2 @  T# s, k    if nu>0,& w7 `# c& B6 y
            df=conv(d,f);) _1 s4 ?  J# p) ~; y
            for ku=1:nu
    ( w  N5 K$ u9 l0 r2 W            bf(ku,:)=conv(bb(ku,:),df);- C* Z2 ^$ _( P8 b6 [
            end
    5 X. t2 N1 n9 O. b6 B. s        nbf=length(bf(1,:));nn=max(ng,nbf);" Z& M5 B' r6 N3 U/ ~# G% P
            gg=[[g,zeros(1,nn-ng)];[bf,zeros(nu,nn-nbf)]];
    + W, O1 R0 T4 E    else
    1 d* C' a" T; N) I6 U+ L7 G        gg=g;3 [+ @4 @' u4 {- v
        end
    ' u" T1 U1 P+ B" k2 ]    th1 = idpoly(c,gg);( q1 }& U3 q' \  N
        th1 = pvset(th1,'InputName',[yna(ky);una],'OutputName',[yna{ky},'p'],...9 `, N& y, F2 t: b; u, n, @% B
            'InputUnit',[yu(ky);uu],'OutputUnit',yu(ky),'Ts',pvget(theta,'Ts'),...
    / K" }1 Y1 M" k* ]9 y2 D: d8 K        'TimeUnit',pvget(theta,'TimeUnit'),'EstimationInfo',pvget(theta,'EstimationInfo'));
    : q2 L, ^) K/ q: ?, q! P5 `    thpred{ky} = th1;2 }+ `7 }/ m# x% c: [
    end# Z& B; Y! n9 T8 V! q" [( Q9 N
    %%%************************************************************************
    "真诚赞赏,手留余香"
    还没有人打赏,支持一下
    帖文化:【文明发帖 和谐互动】 社区精神:【创新、交流、互助、共享】
    您需要登录后才可以回帖 登录 | 立即加入

    本版积分规则

    招聘斑竹

    小黑屋|手机版|APP下载(beta)|Archiver|电力研学网 ( 赣ICP备12000811号-1|赣公网安备36040302000210号 )|网站地图

    GMT+8, 2026-3-18 23:42

    Powered by Discuz! X3.5 Licensed

    © 2001-2025 Discuz! Team.

    快速回复 返回顶部 返回列表