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

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

    本版积分规则

    招聘斑竹

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

    GMT+8, 2026-4-23 21:33

    Powered by Discuz! X3.5 Licensed

    © 2001-2026 Discuz! Team.

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