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

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

    本版积分规则

    招聘斑竹

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

    GMT+8, 2024-5-3 06:07

    Powered by Discuz! X3.5 Licensed

    © 2001-2024 Discuz! Team.

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