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

 找回密码
 立即加入
搜索
查看: 8863|回复: 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 5 p: `/ c3 H* h

    4 ~1 ?6 ?* o$ h; f$ u. C& d" k2 ]* m) z$ j, E
        谢谢大家的关注~~
    + x! u& k4 z8 T3 cfunction [yhat,xi,thpred]=predict(varargin)# V2 X+ v) Z( X! ^9 ~! }
    %PREDICT Computes the k-step ahead prediction.# _( A/ t* a! x4 o$ t6 ^- V! g
    %   YP = PREDICT(MODEL,DATA,K)1 X$ [* t' `" N# H
    %
    6 d) T" v& f5 f, S: O%   DATA: The output - input data as an IDDATA object, for which the0 l% F$ i) G: p
    %   prediction is computed.
    5 E1 L4 x: d+ k: H: G+ W3 `" J%
    " M% z  i+ a( n* e# @9 s2 d%   MODEL: The model as any IDMODEL object, IDPOLY, IDSS, IDARX or IDGREY.& Y; K! o% ]8 b# z' H) L
    %# ~% p0 D' c3 I/ V5 e6 o2 V# U
    %   K: The prediction horizon. Old outputs up to time t-K are used to/ `5 R' o9 ]( V2 P! ~
    %       predict the output at time t. All relevant inputs are used.
    + D* a3 y% A$ D( C1 K9 M' _  b" `, ]%       K = Inf gives a pure simulation of the system.(Default K=1).
    * w* `) s* E4 n+ p  D1 ?* a%   YP: The resulting predicted output as an IDDATA object. If DATA, m6 W% S. s  S" R& k5 R& ~1 k; t
    %       contains multiple experiments, so will YP.
    + X+ d3 q* N6 D& F& P# |0 T%* p* X6 g. L  V2 \2 q( {. P
    %   YP = PREDICT(MODEL,DATA,K,INIT)  or
    - r; a  T9 n8 t* ]9 }%   YP = PREDICT(MODEL,DATA,K,'InitialState',INIT) allows the choice of4 M1 D' G- X. q3 ?4 y
    %   initial state vector:
    - [# U) T4 ?+ G* K0 j% R! K; h%   INIT: The initialization strategy: one of. R7 [2 X) ~* m- e: Z' _$ x0 x
    %      'e': Estimate initial state so that the norm of the; R8 f1 ^; c" R. l6 g
    %           prediction errors is minimized.+ ~, ]2 G2 E3 _$ O+ O9 l3 ^6 W
    %           This state is returned as X0e, see below. For multiexperiment
    1 f4 i( E6 Y* P6 a& `) E%           DATA, X0e is a matrix whose columns contain the initial states
    0 u! I9 I2 F# a, d%           for each experiment.
    0 f6 c( r, [$ h; Y%      'z': Take the initial state as zero3 v; _/ R0 O8 \& h9 P) C( z) j
    %      'm': Use the model's internal initial state.
    & O" u/ V7 E) y& f" S: y' j  l' |%      'd': Same as 'e', but if Model.InputDelay is non-zero, these delays
    " K* W1 C6 D8 ^% ^8 Z7 `) O7 k8 R%            are first converted to explicit model delays, so that the are4 @' d. h% V# z- U4 U
    %            contained in X0 for the calculation of YP.
    7 J8 Q% ^2 A# V% I9 L0 x; |9 A%       X0: a column vector of appropriate length to be used as initial value.0 r: M6 D! y# K) n
    %           For multiexperiment DATA, X0 may be a matrix whose columns give
    6 i0 Z3 S; `7 d8 @: Y% t9 m( C) H%           different initial states for each experiment.
    , m  n7 _- m! X! r' g' p9 F%   If INIT is not specified, Model.InitialState is used, so that
    ( V0 m. Q$ h# W7 t7 M/ z2 ^, a- c%      'Estimate', 'Backcast' and 'Auto' gives an estimated initial state,
    2 V8 R  n. |( W" c* h& d$ R%      while 'Zero' gives 'z' and 'Fixed' gives 'm'. If Model is an IDARX6 E* }2 l$ V/ ]' `* x- Z
    %      model the default initial state is 'z'.
    + I' K, l$ O2 P( I%
    ! T% a/ v) O5 F8 Y, g! M%   With [YP,X0e,MPRED] = PREDICT(MODEL,DATA,K) the initial state(s) and the
    ( b! s$ p, `8 N4 _%   predictor MPRED are returned.  Note that if MODEL is continuous time, X0e
    7 H# N/ C/ H& U% t# e%   is returned as the states of this model. These may differ from the initial
    ; r( v; d* J$ W5 N) Q" k8 Q%   states of the discrete time model that has the sampling interval(s) of# ]- L) T: Q  X' E1 y2 N) F; X$ U
    %   DATA. Also when INIT = 'd' only the states of MODEL are returned in
    ; j6 j( u& B0 h+ s+ z: @%   X0e. (To obtain the full X0, apply first INPD2NK to a discrete time
    3 F+ w8 v% k; U0 \: O* p%   version of the model.)2 d7 j  [  p" Q4 \9 }
    %   MPRED is a cell array of IDPOLY objects,4 {" k) B, u4 q" b9 y
    %   such that MPRED{ky} is the predictor for the ky:th output. The matching: \# `/ q7 q2 W+ @: M5 I
    %   the channels of MPRED with data follows from its InputNames.
    7 Y5 k. n, |7 O$ S0 q! K%   See also COMPARE and IDMODEL/SIM.
    , ^' {' W+ O: d/ w4 \%   L. Ljung 10-1-89,9-9-94
    + ~' y7 {; ?& y' w%   Copyright 1986-2008 The MathWorks, Inc.6 {+ G1 B# O: e: Z: H9 H
    %   $Revision: 1.1.8.9.2.1 $  $Date: 2008/08/01 15:32:39 $& R4 {' \0 t1 s# e- Z8 z( V

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

    本版积分规则

    招聘斑竹

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

    GMT+8, 2026-11-1 17:07

    Powered by Discuz! X3.5 Licensed

    © 2001-2026 Discuz! Team.

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