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

 找回密码
 立即加入
搜索
查看: 8831|回复: 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
    9 l$ k+ [- S6 r, _! j, A' N7 N6 r# F' v' g
    + ~+ V4 C6 d$ V9 n% u8 `
        谢谢大家的关注~~
    % ^5 V9 a! `+ ?4 V# A8 tfunction [yhat,xi,thpred]=predict(varargin)% ~8 |/ p) ?( h/ e2 ~+ ^
    %PREDICT Computes the k-step ahead prediction.; V3 ?- Y5 y' H% B$ V
    %   YP = PREDICT(MODEL,DATA,K)
    . B. s) n# C' z7 z4 l%
    4 L8 T  y0 b! ^%   DATA: The output - input data as an IDDATA object, for which the
    + z$ a! X1 f& r1 F; R1 H" \1 C) n%   prediction is computed.! c+ q8 d5 f# d& ^6 S3 l: d
    %8 k  [' U9 ~3 `
    %   MODEL: The model as any IDMODEL object, IDPOLY, IDSS, IDARX or IDGREY." [  J/ y$ L9 B4 k
    %
    1 L+ K; P. f" S) `. x5 |! F%   K: The prediction horizon. Old outputs up to time t-K are used to8 u, E, Z; N: p* ?* T  S, }& n# v4 J
    %       predict the output at time t. All relevant inputs are used.
    : \6 r8 b8 P4 F%       K = Inf gives a pure simulation of the system.(Default K=1).
    + l1 e6 a( o( A( ^) X7 R. w%   YP: The resulting predicted output as an IDDATA object. If DATA
    8 d; {8 L6 E: g9 B2 I# D%       contains multiple experiments, so will YP.
    ( F- W) G+ `& t3 W7 p%
    6 f  F! r6 D! G, L! j- ^& `9 }$ P%   YP = PREDICT(MODEL,DATA,K,INIT)  or
    % o( }& i! s* V% M" U: S( z) K* I) T%   YP = PREDICT(MODEL,DATA,K,'InitialState',INIT) allows the choice of3 k4 z2 v4 h* k) B" `
    %   initial state vector:
    + F: o# K5 L! Q6 v%   INIT: The initialization strategy: one of
    6 `' ^( J+ M, i0 |6 }8 J%      'e': Estimate initial state so that the norm of the
    " m* V, j" I9 c%           prediction errors is minimized.
    . p1 [  d# K; m%           This state is returned as X0e, see below. For multiexperiment
    6 i: O) L( U& N# X  M& x# o%           DATA, X0e is a matrix whose columns contain the initial states0 z/ p3 B: M: W" ~( {- P9 ?
    %           for each experiment.: v1 C2 W/ E6 I$ r4 B+ ^
    %      'z': Take the initial state as zero# Q; E' ]. K9 I
    %      'm': Use the model's internal initial state.) o) b. W0 e7 l) ]6 B4 t' P
    %      'd': Same as 'e', but if Model.InputDelay is non-zero, these delays1 @% k' X/ v* N5 j% T
    %            are first converted to explicit model delays, so that the are
    6 z! O$ l/ Q# C2 Q, x8 Z, T* F8 Z%            contained in X0 for the calculation of YP.
    9 s+ v$ D5 a, G8 s$ S0 i" b%       X0: a column vector of appropriate length to be used as initial value./ G# v5 q  `: d, @, c6 _
    %           For multiexperiment DATA, X0 may be a matrix whose columns give1 T0 V$ [! |! T% ?3 u
    %           different initial states for each experiment.
    , D* `" }5 N( A9 ^  N%   If INIT is not specified, Model.InitialState is used, so that! b$ B' N0 L6 V+ E5 t
    %      'Estimate', 'Backcast' and 'Auto' gives an estimated initial state,' Z0 `( p( a1 d& O* Z
    %      while 'Zero' gives 'z' and 'Fixed' gives 'm'. If Model is an IDARX
    ; R! C/ b* q# Y7 ?2 F3 h( \. q: F5 o%      model the default initial state is 'z'.
    - a/ U3 Z  Z% i2 D: h5 y4 v%. j, h% t' l6 E7 t2 z( K( z
    %   With [YP,X0e,MPRED] = PREDICT(MODEL,DATA,K) the initial state(s) and the
    $ z  j( `0 R( Y! s, g4 V%   predictor MPRED are returned.  Note that if MODEL is continuous time, X0e  z0 B- n4 R$ O* z( b1 E$ i
    %   is returned as the states of this model. These may differ from the initial
    9 z3 {2 X) ^  o. d; X9 Q3 y. J%   states of the discrete time model that has the sampling interval(s) of% n# z0 K& L  n) Y' K5 I
    %   DATA. Also when INIT = 'd' only the states of MODEL are returned in
    ) U" ?) s+ W; L; _! [%   X0e. (To obtain the full X0, apply first INPD2NK to a discrete time9 T, A6 H# |) E2 V. V% T
    %   version of the model.)& K. o, @4 b6 |4 X( a( w0 h
    %   MPRED is a cell array of IDPOLY objects,
    $ A) u! z5 B! ^. u& j, E%   such that MPRED{ky} is the predictor for the ky:th output. The matching2 l. O( r7 v! f  O9 t0 h
    %   the channels of MPRED with data follows from its InputNames.7 x  c7 I/ G9 Z+ r8 [
    %   See also COMPARE and IDMODEL/SIM., p& r' O0 l) |: s1 u* o5 W
    %   L. Ljung 10-1-89,9-9-94
    : A# Q2 I, F5 _9 Y%   Copyright 1986-2008 The MathWorks, Inc.: z6 i: n7 R6 h
    %   $Revision: 1.1.8.9.2.1 $  $Date: 2008/08/01 15:32:39 $! X/ W8 b' l5 [  K2 e4 O+ ~

    - Z* N2 m7 M% w- t8 w1 a: ]% First find if there is any pair 'InitialState', init in the argument9 }2 r' K* @1 D9 U- ]. b. [
    % list:$ ~: C5 {  R) \  C* k0 z( O
    nr = find(strncmpi(varargin,'in',2));# v3 G/ ]. E8 K$ e9 n5 Y( a6 z: |
    init =[]; xi = [];$ m$ q% b! Y& t5 C# _
    if ~isempty(nr)" o8 i% K" z8 j9 H9 m& U
        if length(varargin)<nr+1  p/ s1 F% j0 r( X* Y
            error('Ident:general:optionsValuePair',.../ Q* B! n; A( X5 J+ L4 R- x8 e! X% R
                    'The "%s" option of the "%s" command must be specified using a name/value pair. Type "help %s" for more information.',...
    # V* X8 G! l0 H" J. u3 U: A                'InitialState','predict','predict')5 I3 }( d+ f+ j2 B' G  }# r
        end
    " d2 u* E; e  L6 i! ?5 Y    init = varargin{nr+1};: G1 Q4 x* d' ^% p) T# F
        if ~isa(init,'char') && ~isa(init,'double')% n$ t3 H! `9 X; ?( x$ |4 D2 q" n/ H# L
            error('Ident:analysis:IniSpec2',..." P- I/ n9 {$ z% Z. G( W2 m: E7 Q
                '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')
    ( G" W2 t* ^2 o/ z+ H4 D    end6 U+ z* K  r( ?# z7 r0 r
        varargin(nr:nr+1)=[];%
    / Q0 o) U4 u: q( \1 }, tend
    - N5 i9 `. ~+ w& mif length(varargin)<29 I1 l) ^2 f. [0 o7 r& H4 {7 i
        disp('Usage: YP = PREDICT(MODEL,DATA)')& e2 e) M" t, o0 N. b% J
        disp('       YP = PREDICT(MODEL,DATA,M)')
    ! R; v) M; u0 O- E    yhat = []; xi=[];) ~$ x& I6 g4 s0 w9 b
        return2 x; x8 X$ E% L9 W( ?7 _& N
    end
    4 D% _$ [4 `& C3 p0 F2 u" Ddata = varargin{1};
    : f7 l, c( B2 ~  ptheta = varargin{2};
    8 }3 m/ @0 L* t  ?5 Z$ n: oif length(varargin)>2
    0 _# o7 Q  c$ ~( j+ C    m = varargin{3};- Y8 Y2 R- ]) a' n
        if ~isnumeric(m) || ~isscalar(m) || ~isreal(m) || round(m)~=m || m<1
    $ V% u9 J9 O! j8 z; D5 G2 \        error('Ident:analysis:predictInvalidHorizon',...  o. `& _* D7 |
                'In the "predict(MODEL,DATA,K)" command, the prediction horizon K must be a positive integer.')5 w  q) p+ Q! K4 @2 K* H
        end    3 |* A& _, Z5 d% }! p8 {
    else
    ) `  [4 z6 L# I% [! `) o  c    m = 1;) J7 J- [+ k# J" j3 S, s3 t$ P  u
    end+ S" A* S5 P8 B5 K+ y6 T
    if length(varargin)>3 && isempty(init)3 V. o, ?( I. S" E5 h
        init = varargin{4};/ y7 A9 m. W4 p0 j( A9 J3 f9 N
    end8 b5 p1 F% ]1 H
    thpred = [];" M2 Z8 L7 @5 t
    yhat = [];7 Z* G+ G% c+ w! Q. Z
    if isa(data,'idmodel') % Forgive order' o5 X$ o7 R0 O
        data1 = theta;( u$ o- y1 @- D
        theta = data;
    ( U- P, w3 \9 K    data = data1;
    ; G( B$ G& h9 f2 `end
    3 ?. K, X6 }& r, c3 o4 n: }nxorig = length(ssdata(theta));6 v4 O$ w  |5 \) k
    [nym,num] = size(theta);
    / d( l5 ~  z2 C1 Q3 o; }/ pif isa(data,'frd') || isa(data,'idfrd')
    * y8 l0 C- p1 ]+ @4 w$ |0 f    error('Ident:analysis:predictIDFRD',...
    / N! P" d, D0 C, ?        'The "predict" command is not applicable to frequency response data (IDFRD object).')
    # P- G, p. x/ u: d, S" U$ lend
    7 |: J- {1 b7 h$ c! nif isa(data,'iddata')
    * [1 Q" P  A; c' t    [Ncap,ny,nu] = size(data);$ P/ {- Z# T( N: |! n
        if ny~=nym || nu~=num: Q7 S6 f  T; e
            error('Ident:general:modelDataDimMismatch',...1 n0 O+ Y7 A, K( e- q% x; u
                'The number of inputs and outputs of the model must match that of the data.')
    * q; G, p; p8 E; x) c# B( s    end) i# {! i7 V" i: z+ ]
    end
    / m6 g9 O( c6 Zif isempty(m), m=1; end- m7 D7 N( E9 d3 P' s
    if ~isinf(m),
    0 S7 t2 \$ F4 j' t9 j1 i    if m<1 || m~=floor(m)+ r& p# j  J# @$ R
            error('Ident:analysis:predictInvalidHorizon',...1 V' ?9 F  C3 T( u* Q" y6 T
                'In the "predict(MODEL,DATA,K)" command, the prediction horizon K must be a positive integer.')
    5 q; v2 ?  \! H4 d    end
    ' D, w' ?( S6 W& rend
    ' k' [8 @' S9 H1 U/ a8 |% First deal with the special case of continuous time data and continuous
    - H9 T1 D) ~; C/ j' x7 x  K! }% model:
    4 Z2 ]  u! A0 F' @( |# }8 ]if isa(data,'iddata') && any(cell2mat(pvget(data,'Ts'))==0), G5 Q  q2 _4 ~7 k6 }" K0 ]" h' |
        if ~isinf(m)# t" F7 N3 N# b% S8 c$ z
            error('Ident:analysis:predictCTData',...
    " \9 ~* m2 P2 I4 A            'The "predict" command cannot be used with continuous time data.')+ `0 _( |8 W( c! E, _3 ]
        end
    2 u+ e1 \$ S9 w- E/ k8 p    yhat = sim(data,theta);%没懂
      l, j4 g' Q; F0 t8 }    if nargout == 0% S: }7 d7 @! {$ v5 C
            utidplot(theta,yhat,'Simulated')* {0 f* l; [: `1 k9 F5 h
            clear yhat* T) W/ ^- o- v! l' C
        end4 S4 w3 f! A0 ], M" F
        return
    % ~$ I5 d# D! V. v& aend! g7 V! ^. @: N, H" t
    if isempty(init)" W+ M* }2 v0 c- t$ O/ A. |  h. ]
        if isa(theta,'idarx')
    3 ^' e; _. {$ P, G        init='z';
    1 V+ T( h6 Q1 m% D* k1 F    else
    ! i( z$ Z2 V7 a: h9 K. E        init=pvget(theta,'InitialState');
    0 ]  D: r7 R5 v8 Y0 g, ]    end7 o) p2 _. H, E. Y8 s9 r: T
        init=lower(init(1));
    5 K  k% n! k; n! _4 C; y# M3 [    if init=='d'5 ~( j) A# z, e$ r: ^8 t- @9 H
            inpd= pvget(theta,'InputDelay');  ]* m9 o7 I1 r2 v1 v3 t
            if norm(inpd)==0
    ' ?" c6 I- P0 K* u, {0 M& Z5 g            init = 'e';& A& N3 e( \2 ?
            end
    5 c% ]' @" [4 w    end
    8 W. P0 M. B5 O    if init=='a' || init=='b'
    ' k; @, P% n, U8 y6 [' n        init = 'e';# ~# G0 n# M  n4 i/ o
        elseif init=='f'/ k& J! n6 B( J+ a! o0 n
            init = 'm';
    & i4 }! {: R. X/ k6 E    end- ~7 {. o  u* Q$ x2 v, |: L
    elseif ischar(init)" C4 Z; G9 V7 i& k
        init=lower(init(1));6 x  ?3 K% R& x, Z. ~8 c' ?: Z; ]
        if ~any(strcmp(init,{'m','z','e','d'}))
    $ L2 c, y) q1 X2 y5 @2 X  N" x: k        error('Ident:analysis:predictIni3',.... ~# x0 z" k/ f% v
                'The "InitialState" option of the "idmodel/predict" command must be set to ''E(stimate)'', ''Z(ero)'', ''M(odel)'', ''D(elayconvert)'', or a vector.')
    . c# F+ m6 m& q+ C" w5 b    end
    5 H4 b" L4 G+ M0 x; h7 j    if init=='m' && isa(theta,'idpoly')" S' u: c9 t6 K( r; r- G
            warning('Ident:analysis:predictIni4',...
    - d! c2 `. C  {' O  U9 l& q! m            'Setting the "InitialState" option to ''m'' in the "predict" command is same as setting it to ''zero'' for IDPOLY models.')
    " W2 g7 L; E' x4 f/ _. p8 C    end$ S; E7 b  S2 W
    end
    ' J& t+ Y, k" c# Z& ]%% Now init is either a vector or the values 'e', 'm', 'z' 'd'
    ' V) A7 O- ~# U$ yif isa(data,'iddata');
    ) x' h; q" s6 W2 ^: ?% R( h6 w1 x    if strcmp(pvget(data,'Domain'),'Frequency')
    / G5 X- B7 y' \; ~: K( M/ n  ^" G        if m<inf
    / K- o- [" U; v            error('Ident:analysis:predictFreqDataFiniteK',...
    # b8 D  Q& p6 m1 p0 i% A% Q                'Prediction with finite horizon is not applicable to frequency domain data.')
    ( I! o3 t1 D) h# W- w        end0 B, j: [$ q2 B9 y! X0 |/ i6 \
        end+ n1 h' v. @0 \: k* r" E: q
    end
    % P2 e/ }! `1 c0 ~& y, lif isa(data,'iddata')
    9 |2 l* L8 E: ^& k9 |    [uni,Tsd,inters] = dunique(data);
    7 }' B' R2 m+ R% T% r" h    iddflag = 1;
    ! a4 K# r  b* C; T/ k  V) xelse
    % n: n) n' L: E5 ^- C    iddflag = 0;6 i+ D) u- A* ^$ z
        uni = 1;
    ) J5 D* x5 L- s3 `    Tsd = 1;
    ) W$ P8 g/ x8 h  g3 S    inters = 'z';
    , c& d; G7 X+ W$ Fend
    ; x  Q. P  Y/ F, \3 b% v%% Do away with continuous time right away:
    $ E5 f+ T* F. e0 Y& K& C- HTs = pvget(theta,'Ts');
    : z0 K4 z& z2 ~  Lif Ts == 0" D# o  a' U" z
        theta = pvset(theta,'CovarianceMatrix',[]); % Less calculations7 u; `% ]7 ^, j6 i1 q
        ms = idss(theta);
    . O4 a8 c2 j; x. ~# [5 x    nc = size(ms,'nx');
    , m4 P1 o( R5 l9 i+ L- X- Z4 L3 l    if ischar(init)1 u6 x5 S; T# s; y9 @
            if init=='m';9 l) d% y0 k5 i% \/ R$ ^* i# M- b* k
                init=pvget(ms,'X0');
    : G# C) ]$ o% ^+ o' q! a, ^1 @/ L        end$ C5 w) V7 I& Y) E+ i$ j) y
            if init=='z'! J6 M* O$ W# A1 {
                init=zeros(nc,1);1 C+ X: y6 l; G
            end
    ) [, P' N: h1 p& `* A    end
    5 j6 b+ z* W. u& _2 F    if ~isa(data,'iddata')* V* H; `$ T8 w; ?$ @
            error('Ident:analysis:predictDataFormat',...( Y3 d) J- T; |0 K! u! T- _
                ['For a continuous time model, data must be specified using an IDDATA object.',...9 k* V" X! k* E
                'Type "help iddata" for information on how to create the IDDATA object.'])) a; g6 W: x; Q: _* ]" Z
        end3 c* O5 i/ [/ V7 P
        Nex = size(data,'Ne');" m" `, J2 c& Z/ j" E( F& W9 r
        u = pvget(data,'InputData');+ _! P9 l3 v: f' G
        u1 = [];. t1 R, K2 I8 b1 v" {: G; U1 {
        for kexp = 1:length(u);
    ! L6 B& V) z" q& W% ^1 ~3 R        u1 = [u1,u{kexp}(1,:).'];
    8 I5 e* M8 {# c1 v- j) a# O    end
    9 M1 ?$ U: }. ?. Y' D    nu = size(data,'nu');7 y( ^3 i, E: M% i( h, [$ }8 l4 N: e
        if uni  a0 I4 b0 [4 m; S7 R) w
            nc = size(ms,'Nx');
    8 C. |9 [% B2 v        [md,Gll] = c2d(ms,Tsd,inters);
    ' n" V2 T% U( E: b! ^  D        if init=='d', md = inpd2nk(md); init='e';end
    - c) w, n( P1 d4 v        % First work on init
    6 o7 i) C# l% m# N: c( L/ j        if isa(init,'double')5 Z& T  Q" [( U
                [nxi,mexp]=size(init);
    % G& j) z$ t2 A, w6 {, j/ c            if nxi~=nc, ]1 k& M/ O' O% z
                    error('Ident:analysis:IniRows',...
    / S+ l1 R, N) J+ V3 z7 x2 P8 f; [) [# {                    'The value of the "InitialState" option should have %d row(s).',nc); I' a$ M" X# n( q) X
                end7 y9 [3 C2 w0 x2 J' q% K
                if mexp==1 && Nex>1
      }" h: @! J8 e6 K& W                init = init*ones(1,Nex);+ n' t3 C- f, d6 A" V) k
                    mexp = Nex;9 {) c# X  P* E4 Y! F+ |/ C7 U# e
                end
    + s, s( `( g4 }4 T! _  M            if mexp~=Nex& J- Z5 r  l) Y  s0 h# B
                    error('Ident:analysis:IniSize',...4 _7 e9 L# C; o, g. b6 J7 Q
                        'The "InitialState" option value must have either 1 or Ne columns, where Ne = no. of data experiments.');
    7 V. d9 D' X0 S  T6 d% M            end' N% E0 H! U0 r5 H
                if ~isempty(Gll)
    ; P! s3 D' E* O( @! P. M  \                init = Gll*[init;u1];
    - ~  Q  }- `) y4 H/ I            end
    5 Y5 p0 L+ \9 s0 Z/ r, V        end" o* o4 M3 u) d5 I
            if init == 'e'
    " u5 m2 P" ^4 h            inite = x0iniest(md,data);
    % ~+ b3 L4 g3 V- B2 H$ \        else  O: a/ `3 ~& n9 l  y# s
                inite = init;
    , _" I1 O9 f- H4 g$ {$ |# S        end
    , x3 C1 F: h/ u5 @# a: q9 F% x! o        yhat = predict(md,data,m,inite); %Check XIC!
    3 R( c4 K* z4 Y2 t7 d; b: g/ U6 |        if ~isempty(Gll)
    % i. v! W3 ?, a+ w- D            xi = inite(1:nc,:)-Gll(1:nc,nc+1:nc+nu)*u1;# Y! U& K6 \' e
            else) o" {1 J7 ?" e6 p
                xi = inite(1:nc,:);# N& D, T' n6 _, l7 p
            end
    * I0 y8 M1 c+ q$ J    else % Different sampling intervals
    . Q6 x& U0 z# n9 ]1 L0 f        Tsd = pvget(data,'Ts');
    : O+ b' s$ i1 @' g: a0 U        ints = pvget(data,'InterSample');
    ! j# A8 j8 Y1 G5 g2 Q; n" B        %xic=zeros(nc,Nex);
    4 w3 p4 o/ G4 @" O        for kexp = 1:Nex: U: @. J& a, f6 |
                [md,Gll] = c2d(theta,Tsd{kexp},ints{1,kexp});6 H* `, d% \/ p& U2 f
                if init == 'd', md = inpd2nk(md); init = 'e';end
    0 A( c& t' ^9 L* e            if isa(init,'double')
    ; K( l* m, u9 j0 h% [) y                if ~isempty(Gll)
    ( w; [/ c" X! D9 d/ `" w/ j                    if size(init,2)==1
    - V! d+ G- n& ]' o0 r                        initk = Gll*[init;u1(:,kexp)];
    / k8 P2 f# q) k0 }$ N- f0 \; f                    else
    - D6 B# A& s' w5 ^" Q                        if size(init,2)~=Nex3 n* b  \6 B" e1 M$ H# V
                                error('Ident:analysis:IniSize',...  {+ f: }) i5 I$ `
                                    'The "InitialState" option value must have either 1 or Ne columns, where Ne = no. of data experiments.');
    " V9 ]) o% r3 n* ?  P' j6 L& T                        end
    $ N5 E. q9 Y2 \/ ], j' n7 v                        initk = Gll*[init(:,kexp);u1(:,kexp)];; `/ N  G# ~0 j; Q
                        end
    # F  q5 D; E$ O0 n1 n* q+ L& s7 _                end
    ) {7 n% p4 @6 K/ r6 d3 t            else
    . Q9 T2 y7 K; k# j8 V                initk = init;
    & B4 D# e, }) X' w" E8 T9 v. v% o            end
    4 q! }. G3 d& C- A, y            if init == 'e'
    " Q5 f3 Q+ w; \+ \% Y& N1 w                initk = x0iniest(md,getexp(data,kexp));
    ) w4 P: d& v" ]6 j4 J            end
    # i" s3 `4 O$ Z5 s/ L* h            if ~isempty(Gll)% M( h9 H! N) F: e$ s
                    xi(:,kexp) = initk(1:nc)-Gll(1:nc,nc+1:nc+nu)*u1(:,kexp);7 j% K; [4 l- ]: [; H8 l1 o* Z
                else
    " l: Q6 K/ C; q3 o: b  G; t' t                xi(:,kexp) = initk(1:nc);6 |7 U5 e4 B- x( X( Y/ H
                end
    4 ~0 Y7 H/ D4 X  K+ b! {  R            yhatk = predict(md,getexp(data,kexp),m,initk);
    * Z* V( b# r. S2 S' w            if isempty(yhat)/ K$ v7 o, m$ ]. A
                    yhat = yhatk;8 D  O* Q" Q, g
                else
    % Z+ Z( K( `( A: M7 r6 @! W5 T' r                yhat=merge(yhat,yhatk);5 v+ [, H+ n. Y& _2 W$ z  l
                end8 v) Z  N$ a2 s( b& B
            end  s  Z# f8 {0 ~5 n8 [$ ~
        end
    % p/ F# m- k3 \* ~    if nargout == 08 b) t) G7 U/ C: l. N
            utidplot(md,yhat,'Predicted')
    7 b9 i: w5 F- J3 b' N1 R! A        clear yhat
    6 B# g: C, |1 |    end: T5 m3 t- j  f4 J  M9 L
        return* M7 _+ ^' P$ q0 T- i0 B" L9 Q& p6 Q: s
    else %Ts>0
    % G& s2 E3 x3 I7 f8 C5 O/ ?    if init=='d'
    % K0 x' [# _7 M' }        if any(pvget(theta,'InputDelay')>0)+ X6 k$ ]+ `) [$ V' O
                theta = inpd2nk(theta);
    ; W" A: ~7 L* h& l2 |$ w2 U        end
    . d' E5 c9 G  m        init = 'e';
    4 e* G: m- N! Y2 B; b  N    end
    9 d" M  h+ E# H  Tend2 w6 \6 Z* q2 O/ ^. K. Q  _
    if isinf(m)/ D1 K: z: [/ D, {- H! `" b
        if ischar(init) && init=='e'
    2 n( }/ T" v/ c6 u        X0 = x0iniest(theta,data);
    3 N3 \1 L, a' {# e7 k        init = X0;  % Note the difference with COMPARE, which fits% `# C+ b) x2 P6 ^
            % X0 with K = 0;
    ) R" C. r8 w+ B4 X) A    end
    7 G8 Q; r. y( R' P5 z    if ~isa(data,'iddata')
    4 N* Q6 [  B7 o        data = data(:,nym+1:end);
    " R' ~+ L$ [5 }0 C# q    end
    7 `; f* \* |6 b# n2 ?3 ]    [lw0,id0] = lastwarn; was = warning('off','Ident:analysis:unstableSim');6 A6 j1 v/ B# g* U& P
        try$ p- h8 }# V# w. ^2 X% R2 G
            yhat = sim(theta,data,init);$ L, m- q0 H1 k
        catch E
    8 {; O. U6 G+ s7 t6 y        warning(was)$ n" V# |% R6 ?0 M
            lastwarn(lw0,id0);
    - \/ {" _/ ]: q/ H; s# |        throw(E)# C+ U6 m! f1 T$ E( x  `2 ]
        end6 Q6 {: V9 N$ d: D& c( }7 b3 N
        warning(was)
    4 p6 h2 h7 U. D, [    lastwarn(lw0,id0);
    2 a* @0 h" @5 |: k   
    3 L; K) {) v( K1 U" z2 T    thpred = theta;
    , d9 I0 e. W- T4 ?9 H' J    if nargout == 00 M2 ]7 Z. \/ S, [
            utidplot(theta,yhat,'Predicted')
    9 u7 x( Y- F$ ?, H        clear yhat3 K5 M3 q! d" G0 L9 u, ]4 d
        elseif nargout>1
    6 ~- a+ c" M: N& _9 G6 S        xi = init;4 P( x8 F* `( n  p7 U
        end
    . ~" }) D0 W& v7 y    return
    $ L" A( M: \+ v" `end
    % _3 V7 H) p/ g: D7 d%Inpd = pvget(theta,'InputDelay');5 p/ l: ~5 W' v& c
    nu = size(theta,'nu');
    8 z  b, m: n% R6 M, z3 z, v) uif ~uni
    7 H' C3 T. m5 i. w- K    Tsd = pvget(data,'Ts'); Tsd = Tsd{1};
    5 t! _* B( |. r& y# j. \2 j    warning('Ident:estimation:nonUniqueDataTs',...
    : L! `4 Z# i/ ]1 w) z        ['The data set contains experiments with different sampling intervals.\n',...
    " t1 |+ q6 \% R        'Sampling interval from the first experiment (=%g) will be used.'],Tsd)8 y% @$ X3 z/ Y2 I
    end  e, b6 B& q9 z1 v8 }5 N
    if iddflag && any(abs(Ts-Tsd)>1e4*eps)
    ) h. v- F: M/ }7 t# {    warning('Ident:analysis:dataModelTsMismatch',...
    0 {, j# g9 {$ o% v, b            'The data and model sampling intervals are different. The data sampling interval will be used.')
    ) n4 K4 C3 N8 l' R) c+ c( qend
    + H7 W/ j7 @  @9 AInpd = pvget(theta,'InputDelay');$ |, `" L0 F* n1 S* D* W- c3 P
    if init=='d'. y3 J2 \* a5 Z; o4 }
        init = 'e';  V" K# h5 k% n- J7 z: b
        theta = inpd2nk(theta);3 n6 n' @: d! `# z
        Inpd = zeros(size(Inpd));
    ( N' P( m. s9 O  E' iend! g, f( M4 u) r3 U# h
    if isa(data,'iddata')- C. j9 k' Q3 `# _* L, z7 ]' V6 Y
        if isnan(data)
    & }8 ~- G9 g) c7 ]6 x1 C        error('Ident:general:idprepMissingData',...) y/ s0 j' M! |- {# F3 C
                'Data contains NaNs which represent missing values. Use the "misdata" command to fill in the missing values before using.');
    $ Y2 }4 ]+ {1 ?+ \& A    end9 B1 ~% A0 F2 ^% m/ c7 T
        data = nkshift(data,Inpd,'append');8 i1 b! k" G7 b/ ?2 V2 V
        theta = pvset(theta,'InputDelay',zeros(nu,1));5 A- E9 s" @7 K
        [ze,Ne,ny,nu,Ts,Name,Ncaps,errflag] = idprep(data,0,'dummy');8 e( Z5 A6 H; I: r/ y& U! @) u' l  v
        %if ~isempty(errflag.message), error(errflag), end; i- ~& p. {" g, E0 {
        if ~isempty(Name), data.Name = Name; end
    5 b) f3 D0 }4 r. K    iddatflag = 1;
    9 V1 Q* @% P  o; Velse* M# \( k: b" k* N5 O6 I
        if norm(Inpd)>eps: r- }( l$ i! y% E. O# R- F
            if iscell(data)  1 ^) f/ |% f& ?/ F9 B3 [7 t
                data = data{1};
    . s2 K! Q9 x- T7 \; I        end
    - C" Q' Z' ?, A' }6 b        [Ncap,nudum] = size(data);/ e1 r7 y$ g$ Z1 p0 k. E
            nk1 = Inpd;6 q& r5 J9 g- i
            ny = nudum - length(nk1);& s# T& P3 p1 ^$ K9 ?
            Ncc = min([Ncap,Ncap+min(nk1)]);
    : y8 |) ]0 v: C2 l" o        for ku = 1:length(nk1)
    ) ?7 T+ a9 f& l, s            u1 = data(max([nk1(:);0])-nk1(ku)+1:Ncc-nk1(ku),ny+ku);
    1 i+ I3 g. a# G" @0 i7 O# [            newsamp = Ncap-length(u1);
    / T. [% s5 ^# ?            if nk1(ku)>0
    4 c) D" ], f: A( w                u1= [zeros(newsamp,1);u1];3 B# h! U: H  p9 Q5 g
                else
    0 F( L6 S: H+ j& n3 J2 Y- c, L: Z                u1 = [u1;zeros(newsamp,1)];
    ! q7 h+ [) s6 s" p. a( O            end- Y. ]7 ?: O# Q9 P9 n
                data(:,ny+ku) = u1;5 ^" y* A8 e& M5 t( u8 s8 h
            end
    * `# {' f: M2 ]    end
    / ?& q5 Q- |# t    iddatflag= 0;
    * K% B. f' `! d0 t3 d+ K    if ~iscell(data), ze ={data};else ze = data;end
    * b2 N# p: C( W2 M7 ~2 ?    Ne = length(ze);
    , A) L, u% S8 Y% ~8 o    nz = size(ze{1},2);
    ! N- \  S0 v/ j4 \! H    for kexp = 1:Ne5 N! y% }. j# I/ z
            Ncaps(kexp) = size(ze{kexp},1);; [# _+ |( R% I& x
        end! `( B) k+ [3 B7 ]& w
        %Ts =[];
    7 @' s0 u! \; T* s7 Qend
    ( O4 K! P& A- V# ^! w+ Eif m>=min(Ncaps)
      }- @3 X/ n7 l) n# K9 G    error('Ident:analysis:predictLargeK',...: E* b  ]7 h" x; }3 F0 ~
            'Prediction horizon must be smaller than the number of data samples.')4 C7 _% [7 F% Q! e
    end) t4 U# D! Q2 y  w1 v8 g" F* d
    [A,B,C,D,K,X0]=ssdata(theta);1 i) a6 A! }: l, f
    [nyt,nx]=size(C);nut=size(B,2);, O( l9 S) W- x
    if iddatflag
    3 b% T& o* G8 D    if ny~=nyt || nu~=nut
    ; p+ B' t7 [' P        error('Ident:general:modelDataDimMismatch',...
    5 N3 M( E3 n* A' n* y9 |: |- f            'The number of inputs and outputs of the model must match that of the data.')
    7 {$ G, X& W9 h; P/ ^) N; K3 M    end
    9 d- o( {% U! _5 p! Eelse
    8 j" ^5 M% H, q9 Z9 ?( K    if nz~=nyt+nut
    5 _, C6 Z6 e8 I6 e, \8 j( |        error('Ident:general:modelDataDimMismatch',...
    ; m/ P9 n& l% Q/ h4 k% s            'The number of inputs and outputs of the model must match that of the data.')
    $ O: o% @( H* }) X    end& k+ `1 s/ _; `% `9 a( m  |3 F: Q' F
        ny = nyt; nu = nut;
    / Z0 O; z: A( V* ?0 Q1 C0 Send2 L1 n3 e( I( M% l( R& w, f( o
    if strcmp(init,'e'), l3 H  [2 \5 l7 U0 s" l
        X0 = x0iniest(theta,ze);6 X7 g/ Q9 ?2 E
    elseif strcmp(init,'z')3 c  |5 X4 ^4 o  H
        X0 = zeros(size(X0));! D& D1 |8 J+ m
    end+ x; Y1 F1 I1 J: j
    if ~ischar(init)& ^/ O2 P2 i9 K5 z0 b
        X0 = init;
    4 {( n& @$ D# \1 ]+ c: z" `. Gend
    5 W6 D7 I# E1 Iif nargout>1" ~) O$ X- t  N8 ?2 _
        xi = X0;
    2 {5 [4 t. N' X6 y7 ?  Send
    ; T# N; r& F, ]! i  K/ ^[xnr,xnc]= size(X0);9 ]8 c2 [6 n! {) ~
    if xnc~=1 && xnc~=Ne
    7 b% j* W. @/ Z    error('Ident:analysis:IniSize',...
    ' n9 G8 X2 m  H- x        'The "InitialState" option value must have either 1 or Ne columns, where Ne = no. of data experiments.');
    ' {( U6 C. P" w) _' Nend2 m  N+ J+ K" o5 N7 F3 e( c
    if xnr~=nx
    ) \/ A1 n7 F9 p9 S$ s3 A# N9 Y+ m: M    error('Ident:analysis:IniRows',...$ s7 A5 c% D% Z. D) t. v, a
            'The value of the "InitialState" option should have %d row(s).',nx)
    * C, x) V5 m% w% L( A, Eend; W8 n9 e) f7 Q* p' o& ]
    if xnc==1 && Ne>1
    1 O3 K9 a) R. F/ @# X    X0= X0*ones(1,Ne);
    2 e8 V% z; N9 L6 z5 R5 C+ Iend
    7 n' h+ I- k, B' m7 X* A: p/ Y8 zfor kexp = 1:Ne
    7 C4 \" ?* X0 x* e/ z$ u. L/ u    z = ze{kexp};. K4 |. d4 G# I& R" V$ q
        u = z(:,1+ny:end);
    6 F2 f! P0 p: |* k2 Z    Ncap = Ncaps(kexp);
    9 q- E9 o; _; u9 F! `    if m==inf,
    ) l) k9 ~! S/ M5 R        yhat=sim(theta,u,X0(:,kexp));
    ( o6 z6 U( p8 d, V* o; i    else
    + J( T$ B: J2 l8 R- D        x=ltitr(A-K*C,[K B-K*D], z, X0(:,kexp));2 C+ j9 r5 X  M$ J
            if m==1,  O" y6 }2 l% d5 q  _
                yhat=(C*x.').';* N9 G- `# b! Q3 w0 z: n- M( m
                if ~isempty(D),yhat=yhat + (D*u.').';end+ p! ~, v% a! j" M- S$ V! q
            else
      E0 Q" V) P- l4 w5 R            F=D;Mm=eye(length(A));6 ]+ m; u# @+ e8 o+ p6 Y
                for km=1:m-1
    9 `2 p9 f) [) W! q' K                F=[F C*Mm*B];
    ! Z4 H5 m: V+ q' S2 t1 V                Mm=A*Mm;3 r9 d5 W4 Z$ L# U5 {+ i2 [
                end
    ) S3 ], A/ y7 b            yhat=zeros(Ncap,ny);%corr 9111112 P  X. s8 W+ Z+ n) j( P
                for ky=1:ny, _" g, o  t- i0 o% Y/ @
                    for ku=1:nu# v, n4 f0 d$ }+ t$ q
                        yhat(:,ky)=yhat(:,ky)+filter(F(ky,ku:nu:m*nu),1,u(:,ku));: G( G8 e5 J6 S
                    end
    , M: S6 t- {3 J! e* r8 M* v            end
    # a  V$ P7 ^$ _7 T  q) T! A7 b* h            if isempty(yhat),yhat=zeros(Ncap,ny);end
    # \/ v; o5 L4 A( e            yhat(m:Ncap,:)=yhat(m:Ncap,:)+(C*Mm*x(1:Ncap-m+1,:).').';
    3 Q9 \0 S" V* q            if nu>0
    9 \2 V3 Y1 h; [' g+ b                x=ltitr(A,B,u(1:m,:),X0(:,kexp));
    2 Y2 }+ L- k: e1 Y* M. _+ s                yhat(1:m,:)=(C*x.').';
    7 S: V# l. j% a" @# M            end
      ^& T( M# j% x% u6 p            if ~isempty(D),yhat(1:m,:)=yhat(1:m,:) + (D*u(1:m,:).').';end
    9 E, Y# Y/ p5 C- r- ]        end" D* s( A& q6 m) D
        end: [9 C4 X* }6 f) b" Z+ k# |
        yhatc{kexp} = yhat;$ `5 w* S( E/ Q# F
    end  T5 B( M$ I! i3 h$ Z0 n/ j
    if iddatflag9 C  e1 [2 D. T' w. K
        yhat = data;
    4 p7 v  a( q+ @    yhat = pvset(yhat,'OutputData',yhatc,'InputData',[]);
    , g7 P' U9 W- X, _else# Z) [6 C7 J& i8 ]4 P
        yhat = yhatc;
    5 {6 {! m5 y: J4 Rend
    0 c9 f' b8 s+ A/ V  `if nargout >2$ ]- d8 `5 C; g
        thpred = polypred(theta,m);8 d! J6 j2 n- G. m) |
    end5 P9 N- ~5 b% \8 h+ |
    if nargout > 1* o5 ]  z* S$ e  Q! o) h
        try2 J2 G8 r3 U" x
            xi = xi(1:nxorig,:);1 u- K. F9 A' y. X& X: x1 n; v
        end# D  m7 n# v! O+ K6 m$ L, v3 Y
    end
    $ |% C+ A4 v: y+ K  k5 K+ Oif (nargout == 0)5 a8 Z' @; R& O+ T( l/ W; e
        % Plot y and yhat.
    " Y  {! D# A+ G; s4 {) }: O; |    utidplot(theta,yhat,'Predicted');7 g. {  }5 l& x+ G' f: c. I; l: Z
        clear yhat x0;2 l  ^; u5 U8 r+ Y: r) ~3 A
    end4 m0 l6 K/ v2 _) G9 V% Q& s0 g
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    ! q' ^4 m" P, D$ I/ c) v# }. u8 N%%%%% LOCAL FUNCTIONS. W7 X1 N$ a8 ]; m/ a
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % {8 h8 F9 G1 _5 u* W8 X; }4 S- Hfunction xi = x0iniest(m,data)
    ' N3 g2 L6 g' r. o* ?" ]if isa(data,'iddata') && strcmpi(pvget(data,'Domain'),'frequency'): R% h2 X  r1 s* `: Q' O, s
        [dum,xi] = pe_f(m,data);) \0 r1 e( q8 D
        return
    ! I. s: m/ o- n0 S+ b  ?2 n: uend9 x+ g' H  u- n
    Inpd = pvget(m,'InputDelay');
    . y0 h8 a" Y1 a" ?3 L4 uif isa(data,'iddata'); U4 P' ~0 U3 X) D6 r, V! ]
        data = nkshift(data,Inpd,'append');" S+ F- y. |* w: l2 L  N
        [ze,Ne,ny,nu,Ts,Name,Ncaps,errflag] = idprep(data,0,'dummy');
    4 _: u( k0 x: }2 s% V; m8 O    %if ~isempty(errflag.message), error(errflag), end1 G' N- A) c* `
        if ~isempty(Name)$ J6 \1 s3 a) B9 d" N
            data.Name = Name;
    7 u9 p6 m9 r; B( x1 _    end
    + _, [1 J- l2 ~; Yelse
    8 Y! p5 k6 k- I) Q6 S    if iscell(data)
      D* j- D# ?/ w, P* m2 h" X" y( d        ze = data;$ }* I3 Z6 [' G9 s. d5 N$ {
        else
    & j/ u* R4 r% P; ~$ z        ze = {data};
    ! Y8 r' D( o" D- s. J, b1 S/ {. a    end( _6 N: B% v* V) |* l
    end
    $ d! M+ s6 z1 w: \$ j/ ?3 falg=pvget(m,'Algorithm');
    9 ~7 O) t; C# j6 [9 E- N6 s6 a3 ^maxsize=alg.MaxSize;( ~9 E1 v- `  N! g* ~5 s
    [A,B,C,D,K,X0]=ssdata(m);) u+ L- l/ Q; f' {% t0 C# N" i
    nx=length(A);
    $ e- D: ?4 ?( u9 b/ @" Tif ischar(maxsize): R1 J( R2 K9 `6 n" @4 V  s
        maxsize = idmsize(length(ze{1}),nx);
    4 j$ d/ \+ a$ P/ Q5 [3 `end
    # I! f/ P9 l8 N7 t" ?/ R( }AKC=A-K*C;
    % N* j. w2 M! M4 V1 R& v4 g[ny,nx]=size(C);
    5 S4 ^: z& z* y% q$ B& P& [nu=size(B,2);
    - |" E2 C. k% d! Z9 M+ T% zel=zeros(0,ny);( }0 ^/ `0 O2 V& a: u# g
    xic = zeros(nx,0);
    9 U# B0 u0 p& ?5 U! `Ne = length(ze);
    0 c9 a0 ~7 T' L( N4 Z$ b$ ^for kexp = 1:Ne+ z$ Q+ l) U7 l5 ]5 B  T# U3 m
        z = ze{kexp};
    4 F, j% D9 H7 u0 T6 u    [Ncap,nz]=size(z);
    ( e6 l6 T8 p% i- g. ~# ~    if nu+ny~=nz
    * K8 }$ R5 V9 Z+ {1 X) B        error('Ident:general:modelDataDimMismatch',...
    0 ~3 d: ]* T( q. _6 j            'The number of inputs and outputs of the model must match that of the data.')
    ( }3 e$ u8 k7 V" F8 k    end
    7 l  A: J3 T9 G- N3 q. W3 u3 s    nz=ny+nu; [Ncap,dum] = size(z); n = nx;
    : u4 W  f* O- B6 N" x! P: }    rowmax = nx+nz; X0 = zeros(nx,1);
    8 L! D3 ^/ g% T4 A. `    M=floor(maxsize/rowmax);
    3 G- Z0 W6 x) u5 Z/ U/ V    if ny>1 || M<Ncap
    # o& t- h+ R' d# D9 O1 X) q        R=zeros(n,n);Fcap=zeros(n,1);R1=[];' i. ~7 S7 O  m/ I+ S9 q
            for kc=1:M:Ncap0 v% X5 ~5 L) z/ x3 R7 S! u
                jj=(kc:min(Ncap,kc-1+M));# I" H0 Y% ?1 t& ?4 U
                if jj(length(jj))<Ncap1 X) o0 E2 e. }5 b7 S2 @' l6 {
                    jjz = [jj,jj(length(jj))+1];) `: q: x/ Y6 e) ]& b
                else9 E/ H- h: C$ x, h8 W
                    jjz=jj;( `- ^; S6 G: t4 @. V% v  J
                end- G/ o( F8 Q3 F# `/ c
                psitemp=zeros(length(jj),ny);# J8 _6 K# j- F4 `
                psi=zeros(ny*length(jj),n);
    ( q9 S& e. p, R2 }+ B' l9 w2 D            x=ltitr(AKC,[K B-K*D],z(jjz,:),X0);* ?* h9 j- @  R5 @7 I
                yh=(C*x(1:length(jj),:).').';/ _9 H5 r+ G; a1 v( j
                nm=pvget(m,'NoiseVariance');
    % ?3 j$ s3 h' a& y# @            if isempty(nm) || norm(nm)==0 % To handle models without noise8 O1 c# _* u( Z/ Z$ c( _. Y
                    nm = eye(ny);* F# C1 n7 j5 D
                end0 V" |* \3 T$ h& j! {
                sqrlam=pinv(sqrtm(nm));
    : C; ]0 h' b7 o! h! H! \0 V8 P' P5 P            if ~isempty(D),yh=yh+(D*z(jj,ny+1:ny+nu).').';end4 d" c! a) Y/ n& J
                e=(z(jj,1:ny)-yh)*sqrlam;
    9 R% U* k9 h# D4 H" f' u            [nxr,nxc]=size(x);X0=x(nxr,:).';
    $ |  E8 Z4 Z6 a3 ~8 `            evec=e(:);7 b& C! C; R( k9 ~6 g
                kl=1;
      ~6 w7 B* w" U0 S- ]3 B            for kx=1:nx
    2 c: r2 ^2 Q' C" v2 m                if kc==19 a  b% V9 \% M2 `+ g7 h
                        x0dum=zeros(nx,1);x0dum(kx,1)=1;
    * B* f7 E6 Q+ Z6 E! C6 A( j                else* F0 j2 Z! x+ T8 i
                        x0dum=X00(:,kl);
    1 {8 [, z# ~# B: Q                end0 _$ |6 i5 h5 u2 Z
                    psix=ltitr(AKC,zeros(nx,1),zeros(length(jjz),1),x0dum);; j% }, O2 i" Z
                    [rp,cp]=size(psix);
    , U& W. f. a0 [$ Q                X00(:,kl)=psix(rp,:).';
    7 c" v* l1 h9 v: E1 T& V( }                psitemp=(C*psix(1:length(jj),:).').'*sqrlam;! P6 U8 h( e5 o% M4 P$ t
                    psi(:,kl)=psitemp(:);kl=kl+1;
    * J* f4 h, {0 n, B9 i' `            end' m% i1 I- D! B: c
                if ~isempty(R1)
    ) k/ v! |* [' j3 M' g! [$ L                if size(R1,1)<n+1/ f+ l( R0 P$ v6 |
                        error('Ident:estimation:predictSmallMaxSize',...* Q# |2 n% |" W2 Q  u
                            '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''.')6 a/ i1 X$ b" v  U1 b
                    end+ r0 U9 D, D- |0 @% b. ?
                    R1=R1(1:n+1,:);. ], k0 |% f; f! v. ~7 q6 `5 F
                end
      x3 E% m1 u9 i8 N9 L            H1 = [R1;[psi,evec] ];R1 = triu(qr(H1));
    1 l, e$ U. E6 @# c) m4 a        end
    ( |: ^/ f! A6 w( Y        try9 |& K" h/ m) l; b6 p8 @1 E3 u& k3 A
                xi(:,kexp) = pinv(R1(1:n,1:n))*R1(1:n,n+1);$ b9 k) W" p, Q" F! E2 g. Q
            catch2 ^1 y- D. {0 k& a  o
                warning('Ident:estimation:X0EstFailed',...
    ( m  g( x4 k: N" b" |                'Failed to estimate initial conditions. Check model stability. \nThe "InitialState" option has been set to zero.')
    8 m/ m  T7 j& x* z! R            xi(:,kexp) = zeros(n,1);
    6 R! N9 A( h% j! Z6 Z7 d        end
    - F9 t; F- Q8 l) x    else0 X6 ^% A0 o+ [9 u$ D
            %% First estimate new value of xi
    0 ?. E- F. v9 m) d4 t        x=ltitr(AKC,[K B-K*D],z);  K% m* ?6 I! ~
            y0=x*C';
      G7 m9 g8 g% l) Y        if ~isempty(D),: B" }* X* ?$ o* E3 ?, p5 S' x. s
                y0=y0+(D*z(:,ny+1:ny+nu).').';+ J/ Z' a. V# i
            end5 }  A0 p, N$ z# A2 U. `5 d' L' {
            psix0=ltitr(AKC.',C.',[1;zeros(Ncap,1)]);; U6 E; g# Y$ V5 L. M) {
            psix0=psix0(2:end,:);
    7 V4 G8 m  }: Y        try
      x: }9 F9 k. Z1 ?3 M3 k            xi(:,kexp) = pinv(psix0)*(z(:,1)-y0);
    5 g* E0 l7 D: L+ d$ K        catch5 L  c% x4 h0 w1 R, R4 r
                warning('Ident:estimation:X0EstFailed',...0 l9 l! Q% o+ }+ h& I. ?
                    'Failed to estimate initial conditions. Check model stability. \nThe "InitialState" option has been set to zero.')% H6 W# h8 M5 \6 j. n, k' l) Q( O
                xi(:,kexp) = zeros(n,1);
    % P4 Z+ B  V: C0 T  u4 q        end' B: ~) w  L  j+ y: ]! J3 `  q
        end
    + ]! H5 M5 I7 }7 G$ Gend' G0 l. U6 q% J  n* Z0 @; W4 K
    %%*************************************************************************$ o# ~+ ]2 V) ]7 s& h* z
    function thpred = polypred(theta,m): j* ]; d8 O7 f
    % Note that cross couplings between output5 z, F8 p( r# w7 J
    % channels are ignored in these calculations.) c3 ~" S. h- |! u: E6 U# w" q. s
    [ny,nu]= size(theta);
      d0 c" H2 V5 T. o" `yna = pvget(theta,'OutputName');
    9 {( T) r: z4 ^8 M% }+ Nuna = pvget(theta,'InputName');
    ) A, Z& r: V/ Z. L: j" dyu = pvget(theta,'OutputUnit');
    5 U) j3 F0 C3 E* r3 {2 Huu = pvget(theta,'InputUnit');6 z* F' {# ]! N6 p2 U9 {
    for ky = 1:ny
    : t5 B6 m" |8 d* }2 \    [a,b,c,d,f] = polydata(theta(ky,:));8 r& }& |( t5 I/ P
        if nu>03 r( f' |- _% x  q; R; D* Z
            ff = 1;% C* g- F( I* J2 \) W
            for ku = 1:nu,
    4 ]1 f# i# x- a# e" a7 M/ U! a            bt = b(ku,:);7 f# G% ?, F* V6 T7 T2 ~
                for kku = 1:nu, if kku~=ku, bt = conv(bt,f(kku,:)); end, end& j8 C( d% F" Y
                bb(ku,:) = bt;' M9 w, ~) E" s. p% k( J
                ff = conv(ff,f(ku,:));
    # |1 `5 r& b; j0 Z4 ^        end& z, I8 X* q4 v: [
            a = conv(conv(a,ff),d);c=conv(c,ff);, [7 ^( @4 w6 l. E
        else
    3 N7 {) s& z) z& P- ~        a = conv(a,d);- j1 v$ d- j( X8 v) O# ]
        end
    ) G# c) s! P- h# C. D    na = length(a); nc = length(c); nn = max(na,nc);
    * x1 {9 D" P2 ?$ d5 A1 @( d7 ~    a = [a,zeros(1,nn-na)]; c = [c,zeros(1,nn-nc)];, c/ d' Y! J7 R3 R
        [f,g] = deconv(conv([1 zeros(1,m-1)],c),a);$ g1 p3 y$ Z% Q' @- ]6 }- O
        ng=length(g);
    7 h0 w! J- U- M, _! `    if nu>0,
    . Z" N3 I% m! K$ M        df=conv(d,f);8 K! K/ {2 c3 S+ s4 w/ o
            for ku=1:nu8 I& [$ ~( B( X/ }% n( U9 R' U
                bf(ku,:)=conv(bb(ku,:),df);
    % u% E1 @! G! J  w        end
    ( U4 j3 l- X! |$ `        nbf=length(bf(1,:));nn=max(ng,nbf);6 `7 A- g  j& E% E
            gg=[[g,zeros(1,nn-ng)];[bf,zeros(nu,nn-nbf)]];7 k) r; |3 n. v; T3 V% y: w
        else
    1 ^# p+ ~% R- z. r        gg=g;
    4 s: w5 h) Y% @$ m( t. C    end! b& ^( ^' m# \" S5 x3 F; x/ S+ i
        th1 = idpoly(c,gg);$ i8 ]) w% i6 ^- W6 N
        th1 = pvset(th1,'InputName',[yna(ky);una],'OutputName',[yna{ky},'p'],...+ ~5 ~9 {# ]& d/ }: c" z8 @- W/ ?; T
            'InputUnit',[yu(ky);uu],'OutputUnit',yu(ky),'Ts',pvget(theta,'Ts'),...6 J3 g) F5 m" N: b, b$ y
            'TimeUnit',pvget(theta,'TimeUnit'),'EstimationInfo',pvget(theta,'EstimationInfo'));
    ( T2 e: ^% a3 s    thpred{ky} = th1;9 I: z# y0 M$ W3 l' G4 @
    end
    / b- [+ h) ]7 e1 ~# k- j%%%************************************************************************
    "真诚赞赏,手留余香"
    还没有人打赏,支持一下
    帖文化:【文明发帖 和谐互动】 社区精神:【创新、交流、互助、共享】
    您需要登录后才可以回帖 登录 | 立即加入

    本版积分规则

    招聘斑竹

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

    GMT+8, 2026-9-21 07:02

    Powered by Discuz! X3.5 Licensed

    © 2001-2026 Discuz! Team.

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