📄 cnf_update_model.m
字号:
% check the validity of the model based on the Entry
% *************************************************************************
function cnfdb = cnf_update_model(cdb)
cnfdb = cdb;
%
cnfdb.sys_ok = 0;
% A
the_str = deblank(cnfdb.Entry.A);
if isempty(the_str)
the_str='[]';
end
IA=evalin('base',the_str,'[]');
[nx,na]=size(IA);
if nx<1 | nx~=na
warndlg('The maxtrix A is not appropriate !', 'Warning ...', 'modal');
uiwait;
return;
end
% B
the_str = deblank(cnfdb.Entry.B);
if isempty(the_str)
the_str='[]';
end
IB=evalin('base',the_str,'[]');
if ~isequal(size(IB),[nx 1])
warndlg('The maxtrix B is not appropriate !', 'Warning ...', 'modal');
uiwait;
return;
end
% C1
the_str = deblank(cnfdb.Entry.C1);
if isempty(the_str)
the_str='[]';
end
IC1=evalin('base',the_str,'[]');
[nm nc1]=size(IC1);
if nc1~=nx
warndlg('The maxtrix C1 is not appropriate !', 'Warning ...', 'modal');
uiwait;
return;
end
if nm~=rank(IC1)
warndlg('The maxtrix C1 is not of full row rank !', 'Warning ...', 'modal');
uiwait;
return;
end
% C2
the_str = deblank(cnfdb.Entry.C2);
if isempty(the_str)
the_str='[]';
end
IC2=evalin('base',the_str,'[]');
if ~isequal(size(IC2),[1 nx])
warndlg('The maxtrix C2 is not appropriate !', 'Warning ...', 'modal');
uiwait;
return;
end
% D2
the_str = deblank(cnfdb.Entry.D2);
if isempty(the_str)
the_str='[0]';
end
ID2=evalin('base',the_str,'[]');
if ~isequal(size(ID2),[1 1])
warndlg('The maxtrix D2 is not appropriate !', 'Warning ...', 'modal');
uiwait;
return;
end
% Check controllability and observability
% check for stabilizability
% RB=IB;
% RAB=RB;
% for n=1:nx-1,
% RB=IA*RB;
% RAB=[RAB RB];
% end
RAB=ctrb(IA,IB);
if rank(RAB)~=nx
poles=eig(IA);
nn=find(real(poles)>=0);
for n=1:length(nn),
XA=[IA-poles(nn(n))*eye(nx) IB];
if rank(XA)~=nx
warndlg('The nominal model is not stabilizable !', 'Warning ...', 'modal');
uiwait;
return;
end
end
end
% % check for detectability
% % RC=IC1;
% % RAC=RC;
% % for n=1:nx-1,
% % RC=RC*IA;
% % RAC=[RAC; RC];
% % end
RAC=obsv(IA,IC1);
if rank(RAC)~=nx
poles=eig(IA);
nn=find(real(poles) >=0);
for n=1:length(nn),
XA=[IA-poles(nn(n))*eye(nx) ; IC1];
if rank(XA)~=nx
% warndlg('The nominal model is not detectable !', 'Warning ...', 'modal');
% uiwait;
% break;
%% return;
end
end
end
% check for invertibility at s=0
if rank([-IA -IB; IC2 ID2])<nx+1
warndlg('The nominal model has an invariant zero at s=0 !', 'Warning ...', 'modal');
uiwait;
return;
end
% initial condition x0
the_str = deblank(cnfdb.Entry.x0);
if isempty(the_str)
x0=zeros(nx,1);
else
M = evalin('base',the_str,'[]');
[nr,nc]=size(M);
if nr==1
xx=[M';zeros(nx,1)];
else
if nr>1
xx=[M(:,1);zeros(nx,1)];
else
xx=zeros(nx,1);
end
end
x0=xx(1:nx);
% warndlg('The initial state you just entered is of wrong dimension.','Warning ','modal');
end
cnfdb.System.x0=x0;
% for disturbance
% E
the_str = deblank(cnfdb.Entry.E);
if isempty(the_str)
the_str='[]';
end
IE=evalin('base',the_str,'[]');
[ne nw]=size(IE);
if ne~=nx & cnfdb.System.DT_in ==1
warndlg('The maxtrix E is not appropriate !', 'Warning ...', 'modal');
uiwait;
return;
end
cnfdb.System.DT_E=IE;
% w
the_str = deblank(cnfdb.System.DT_w);
if cnfdb.System.DT_in
if isempty(the_str)
the_str=matrix2str(zeros(nw,1));
else
t = 0.1;
if ~isequal(size(eval(the_str, '[]')), [nw 1])
warndlg('The time function for disturbance is invalid !', 'Warning ...', 'modal');
uiwait;
return;
end
end
end
cnfdb.System.DT_w = the_str;
% for control saturation
the_str = deblank(cnfdb.Entry.Um);
if isempty(the_str)
the_str='[]';
end
Umax = evalin('base',the_str,'[]');
if cnfdb.System.CS_in & ( isempty(Umax) | Umax<=0)
warndlg('The control saturation level is not appropriate !', 'Warning ...', 'modal');
uiwait;
return;
end
cnfdb.System.CS_Umax=Umax;
% for resonance modes
the_str = deblank(cnfdb.Entry.RM);
if isempty(the_str)
the_str='zpk(1)';
end
cnfdb.System.RM=evalin('base',the_str,'[]');
if cnfdb.System.RM_in & ~isa(cnfdb.System.RM, 'lti')
% cnfdb.System.RM_in = 0;
warndlg('The resonant modes should be formulated as an LTI model !', 'Warning ...', 'modal');
uiwait;
return;
end
% for input nonlinearity
NL_Fcn = deblank(cnfdb.System.NL_Fcn);
if isempty(NL_Fcn)
if cnfdb.System.NL_in
NL_Fcn='0';
end
else
clear t; % This to to aviod the confusion with disturbance w(t)
y=ones(nm,1);
if ~isequal(size(eval(NL_Fcn, '[]')), [1 1])
warndlg({'The nonlinearity function you entered is invalid !', 'Please note that it must be a scalar one.'} , 'Warning ...', 'modal');
uiwait;
return;
end
end
cnfdb.System.NL_Fcn = NL_Fcn;
% nonlinear zero dynamics
if cnfdb.System.QD_in==1
QD_Fcn = cnfdb.System.QD_Fcn;
Q0 = cnfdb.System.Q0;
if isempty(QD_Fcn)| isempty(Q0)
uiwait(msgbox('Please speicify the nonlinear zero dynamics and initial value.', get(gcf, 'Name'), 'help', 'modal'));
return;
end
% nq=length(strfind(QD_Fcn,';'))+1;
% nh=size(IC2,1);
% h=zeros(nh,1);
% x=zeros(nx,1);
x=cnfdb.System.x0;
h=cnfdb.System.C2*x0;
q=Q0;
qdot=eval(QD_Fcn,'[]');
if ~isequal(size(qdot),size(Q0)) | ( size(Q0,2) ~=1 )
uiwait(msgbox('The nonlinear zero dynamics or the initial value are not appropriate.', get(gcf, 'Name'), 'help', 'modal'));
return;
end
% % test the stability of the nonlinear zero dynamics
% tstep=cnfdb.StepSize;
% tstop=cnfdb.StopTime;
% tt=0;
% nn=0;
% QT=Q0;
% while tt<tstop & nn<50
% [t1,qt] = ode45(@qdotfun,[0,tstep], QT, [], QD_Fcn);
% pt=length(t1);
% QT=qt(pt,:)';
% tt=tt+tstep;
% nn=nn+1;
% end
% if norm(QT)> norm(Q0)
% selection = questdlg({'It looks like the nonlinear zero dynamics is not stable..',...
% 'Do you still wish to continue ?'}, get(gcf, 'Name') , 'Yes','No','Yes');
% if strcmp(selection,'No')
% return;
% end
% % uiwait(warndlg('It looks like the nonlinear zero dynamics is not stable.' , 'Warning ...', 'modal'));
% % return;
% end
end
cnfdb.sys_ok=1;
% check whether there are any changes to the nominal model
if ~isequal(cnfdb.System.A, IA) | ~isequal(cnfdb.System.B, IB) | ~isequal(cnfdb.System.C1, IC1) | ...
~isequal(cnfdb.System.C2, IC2) | ~isequal(cnfdb.System.D2, ID2)
cnfdb.sys_ok = 2;
end
cnfdb.System.A=IA;
cnfdb.System.B=IB;
cnfdb.System.C1=IC1;
cnfdb.System.C2=IC2;
cnfdb.System.D2=ID2;
% % Differential Equations of the nonlinear zero dynamics
% function qdot=qdotfun(t,qt, qf)
% q=qt;
% h=0;
% qdot = eval(qf);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -