📄 matx_proc.m
字号:
[x,hh(12)]=display_str(0.45,0.66,'Columns',[0,0,0],'on',9);
hh(4)=uicontrol('Style','Edit','String',int2str(m),...
'Units','normalized','Position',[0.60,0.59,0.10,0.10],...
'HorizontalAlignment','left','BackgroundColor',[1,1,1]);
display_str(0.05,0.53,'Initial Form',[0,0,0],'on',9);
str=str2mat('Zero matrix','Identity matrix','Hilbert matrix',...
'Magic matrix','Random matrix','Diagonal matrix',...
'Companion matrix','Hankel matrix','Vandermonde matrix');
hh(5)=uicontrol('Style','Popupmenu','String',str,...
'Units','normalized','Position',[0.10,0.36,0.35,0.10],...
'BackgroundColor',[1,1,1],'Value',1,'CallBack','matx_proc(0,6);');
[x,hh(6)]=display_str(0.05,0.23,'Known Vector',[0,0,0],'off',9);
hh(7)=uicontrol('Style','Edit','String','[1,1,1]','Visible','off',...
'Units','normalized','Position',[0.10,0.08,0.8,0.10],...
'HorizontalAlignment','left','BackgroundColor',[1,1,1]);
hh(9)=1;
[x,hh(13)]=display_str(0.05,0.92,'Enter Entire Matrix Here',[0,0,0],'on',9);
hh(14)=uicontrol('Style','Edit','String','[]',...
'Units','normalized','Position',[0.10,0.75,0.65,0.11],...
'HorizontalAlignment','left','BackgroundColor',[1,1,1],'Visible','on');
set(gcf,'UserData',hh);
else, figure(h_win); end
case 2
if nargout==1, key=0; end
[filename,filepath]=uigetfile('*.mtx','Please Specify the File Name');
if ~isa(filename,'double')
if nargout==1, key=1; end
str=[filepath,filename]; uu{10}={0,str}; eval(['load(str, ''-mat'');']);
uu{1}{1}=A; uu{1}{2}=A; set(gcf,'UserData',uu); shw_matrix;
end
case 3
if nargout==1, key=0; end
if length(uu{10}{2})>=10
str=uu{10}{2}; A=uu{1}{1}; eval(['save(str,''A'')']);
uu{10}{1}=0; set(gcf,'UserData',uu);
else,
k_tmp=matx_file(4);
if nargin==1, key=k_tmp; end
end
case 4
key=0;
[filename,filepath]=uiputfile('*.mtx','Please Specify the File Name');
if ~isa(filename,'double')
str=[filepath,filename]; uu{10}{2}=str; set(gcf,'UserData',uu); matx_file(3); key=1;
end
case 5
if uu{10}{1}==1
ButtonName=questdlg('The matrix has not been save. Save it?', ...
'Quit Matx_Proc Confirm','Yes','No','Cancel','Cancel');
switch ButtonName,
case 'Yes', matx_file(9);
case 'No', matx_file(8);
end % switch
else, matx_file(8); end
case 6
h_win=findobj('Tag','MatxProcNew');
hh=get(h_win,'UserData'); hh(9)=get(hh(5),'Value');
if any([3,4]==hh(9)),
extra_funs(4,[],'Visible',hh([3,11]),hh([4,12,6,7]))
elseif hh(9)<=5,
extra_funs(4,[],'Visible',hh([3,4,11,12]),hh([6,7]))
else,
extra_funs(4,[],'Visible',hh([6,7]),hh([3,4,11,12]))
end
set(h_win,'UserData',hh);
case 7
h_win=findobj('Tag','MatxProcNew'); key=0;
hh=get(h_win,'UserData'); strMat=get(hh(14),'String');
if length(strMat)>2, A=eval(strMat);
else
kMatForm=hh(9);
if kMatForm<=5
n=eval(get(hh(3),'String'));
if ~any([3,4]==kMatForm), m=eval(get(hh(4),'String')); end
switch kMatForm
case 1, A=zeros(n,m);
case 2, A=eye(n,m);
case 3, A=hilb(n);
case 4, A=magic(n);
case 5, A=rand(n,m);
end
else
vecA=eval(get(hh(7),'String'));
switch kMatForm
case 6, A=diag(vecA);
case 7, A=compan([1,vecA]);
case 8, A=hankel(vecA);
case 9, A=vander(vecA);
end
end
end
close(h_win); figure(h_matmain); uu=get(gcf,'UserData');
uu{1}{1}=A; uu{1}{2}=A; uu{10}{1}=1; set(h_matmain,'UserData',uu); shw_matrix;
case 8
close(h_matmain); close(findobj('Tag','MatxProcNew')); close(findobj('Tag','MatxProcExtras'));
case 9
key0=matx_file(3);
if key0==1, close(h_matmain); close(findobj('Tag','MatxProcNew')); end
close(findobj('Tag','MatxProcExtras'));
end
%----------------------------------------------------
%mat_decomp perform matrix decomposition
%----------------------------------------------------
function matx_decomp(nTask)
uu=get(gcf,'UserData'); A=uu{1}{1}; hControls=uu{8};
if nargin==0, nTask=1; end
if isa(A,'ss'), A=A.a; end
if ~issquare(A)& any([1,3,4,5,6]==nTask) ;
warndlg('Warning: Non-Square matrix cannot be decomposited!','Warning: Non-square matrix given!');
return;
end
switch nTask
case 1, %LU decomposition
[A1,A2]=lu(A); extra_funs(4,8,'Visible',[3:4],5);
u_dat={A1,'L matrix in LU decomposition, A=LU'};
set(hControls(3),'UserData',u_dat,'String','Matrix L');
u_dat={A2,'U matrix in LU decomposition, A=LU'};
set(hControls(4),'UserData',u_dat,'String','Matrix U');
case 2, %Singular Value Decompistion (SVD)
[A1,A2,A3]=svd(A); set(hControls(3:5),'Visible','on');
u_dat={A1,'U matrix in SVD decomposition, A=UGV'};
set(hControls(3),'UserData',u_dat,'String','Matrix U');
u_dat={A2,'G matrix in SVD decomposition, A=UGV'};
set(hControls(4),'UserData',u_dat,'String','Matrix G');
u_dat={A3,'V matrix in SVD decomposition, A=UGV'};
set(hControls(5),'UserData',u_dat,'String','Matrix V');
case 3, %Schur decompistion
[A1,A2]=schur(A); extra_funs(4,8,'Visible',[3:4],5);
u_dat={A2,'T matrix in Schur decomposition, A=UTU^T'};
set(hControls(4),'UserData',u_dat,'String','Matrix T');
u_dat={A1,'U matrix in Schur decomposition, A=UTU^T'};
set(hControls(3),'UserData',u_dat,'String','Matrix U');
case 4, %QR decompistion
[A1,A2]=qr(A); extra_funs(4,8,'Visible',[3:4],5);
u_dat={A1,'Q matrix in QR decomposition, A=Q R'};
set(hControls(3),'UserData',u_dat,'String','Matrix Q');
u_dat={A2,'R matrix in QR decomposition, A=Q R'};
set(hControls(4),'UserData',u_dat,'String','Matrix R');
case 5, %Hess decomposition
[A1,A2]=hess(A); extra_funs(4,8,'Visible',[3:4],5);
u_dat={A1,'P matrix in Hess decomposition, A=PHP^T'};
set(hControls(3),'UserData',u_dat,'String','Matrix P');
u_dat={A2,'H matrix in Hess decomposition, A=PHP^T'};
set(hControls(4),'UserData',u_dat,'String','Matrix H');
case 6, %Balanced decomposition
[A1,A2]=balance(A); extra_funs(4,8,'Visible',[3:4],5);
u_dat={A1,'T matrix in balanced decomposition, A=T^{-1}BT'};
set(hControls(3),'UserData',u_dat,'String','Matrix T');
u_dat={A2,'B matrix in balanced decomposition, A=T^{-1}BT'};
set(hControls(4),'UserData',u_dat,'String','Matrix B');
otherwise
set(hControls(3:5),'Visible','off');
end
shw_decmat(3);
%----------------------------------------------------
%shw_decmat displays a matrix in decomposition
%----------------------------------------------------
function shw_decmat(nTask)
uu0=get(gcf,'UserData'); uu=get(uu0{8}(nTask),'UserData'); A=uu{1};
shw_matrix(0,A,0.05,0.75,uu{2});
if nTask<=2, extra_funs(4,8,'Enable',1:2,3:5);
else, extra_funs(4,8,'Enable',3:5,1:2); end
%---------------------------------------------
%shw_matrix displays the matrix
%---------------------------------------------
function [xL,yL,hA]=shw_matrix(nTask,A,xL,yL,strMatTitle)
h_matmain=findobj('Tag','MatxProcMain'); figure(h_matmain);
uu=get(gcf,'UserData'); hControls=uu{8}; hButRI=hControls(1:2);
%set defaults parameters in matrix display
if nargin==0, nTask=1; strMatTitle='Matrix A'; end
if nargin<=1, xL=0.05; yL=0.75; A=uu{1}{2};
elseif nargin==2, A=get(gco,'UserData'); end
[nrec,keyJus,err]=matdisp_pars; [nr,nc]=size(A);
%make display truncation
ii=find(abs(imag(A'))<err);
for i=1:length(ii), ix=ceil(ii(i)/nc); iy=ii(i)-(ix-1)*nc; A(ix,iy)=real(A(ix,iy)); end
if nargin<=1 | nr>1, frame_setting(hControls); end
if isreal(A), set(hButRI,'Enable','off');
elseif length(A)>0
set(hButRI(1),'Enable','on','UserData',{real(A),'Real part'});
set(hButRI(2),'Enable','on','UserData',{imag(A),'Imag part'});
strMatTitle='Real part'; A=real(A);
end
%set x- and y-axis displacement
vec=get(h_matmain,'Position'); XSpace=0.02/vec(3); YSpace=0.0417/vec(4);
xL0=display_str(xL,yL+YSpace,strMatTitle,[1,0,0]); hA=zeros(size(A));
%display each element in the matrix
for i=1:nc
for j=1:nr
[xL1,hA(j,i)]=display_str(xL,yL-(j-1)*YSpace,display_str(A(j,i),nrec,err),[0,0,0],'off');
xLEx1(j)=xL1-xL;
end
maxL=max(xLEx1);
if keyJus==2, %center justified
for j=1:nr, set(hA(j,i),'Position',[xL+(maxL-xLEx1(j))/2,yL-(j-1)*YSpace]); end
elseif keyJus==3, %right justified
for j=1:nr, set(hA(j,i),'Position',[xL+maxL-xLEx1(j),yL-(j-1)*YSpace]); end
end
set(hA(:,i),'Visible','on'); xL=xL+maxL+XSpace;
end
yL=yL-YSpace*nr;
uu{9}=hA; uu{1}{2}=A; set(h_matmain,'UserData',uu);
%setting horizontal and vertical scroll bars if necessary
if xL>1, set(hControls(6),'Visible','on','Min',0.5,'Max',xL-0.5,'Value',0.5);
else, set(hControls(6),'Visible','off'); end
if yL<0, set(hControls(7),'Visible','on','Min',yL+0.45,'Max',0.5,'Value',0.5);
else, set(hControls(7),'Visible','off'); end
%---------------------------------------------------
%mat_funs evaluates exp(A), sin(A), cos(A)
%---------------------------------------------------
function A_new=mat_funs(A,nTask)
A_new=zeros(size(A));
switch nTask
case 1, AA=A; F=eye(size(A)); ratio='1/(k+1)'; k=0; dk=1;%exp(A)
case 2, AA=-A^2; F=A; ratio='1/((k+2)*(k+1))'; k=1; dk=2;%sin(A)
case 3, AA=-A^2; F=eye(size(A)); ratio='1/((k+2)*(k+1))'; k=0; dk=2;%cos(A)
end
while norm(F+A_new-A_new,1)>0, A_new=A_new+F; F=AA*F*eval(ratio); k=k+dk; end
%-------------------------------------------------------
%frame_setting administrate the area for matrix display.
%-------------------------------------------------------
function frame_setting(hControls)
%set new axis
extra_funs(1,[0.05,0.07,0.70,0.83]);
%create a frame and save the handle to corresponding controls
hFrame=extra_funs(10,[0,0],[1,1]);
set(hFrame,'Color',[0,0,1]); set(hControls(6:7),'UserData',hFrame);
%------------------------------------------------------------------------------------
%flt_replace changes the TeX representation of a float value to MATLAB representation
%------------------------------------------------------------------------------------
function str=flt_replace(str)
i=findstr(str,'\times'); if length(i)>0, str=[str(1:i-1), 'e', str(i+10:end-1)]; end
%------------------------------------------------------------------------------------
%issquare detects whether a matrix given is square. If not square, an warning dialog
%box will be given.
%------------------------------------------------------------------------------------
function key=issquare(A)
key=size(A,1)==size(A,2);
%------------------------------------------------------------------------------------
%con_obsv detects a system given is controllable, observable, stabilizable and
%detectable.
%------------------------------------------------------------------------------------
function con_obsv(nTask)
h_win=findobj('Tag','MatxProcMain'); uu=get(h_win,'UserData'); G_Sys=uu{1}{1};
A=G_Sys.a; B=G_Sys.b; C=G_Sys.c; D=G_Sys.d; display_str;
switch nTask
case 11, %controllability judgement
[a,b,c,t,k]=ctrbf(A,B,C); n=size(A,1); n_c=n-sum(k);
if n_c==0
display_str(0.1,0.7,'System is Full Controllable');
else
display_str(0.1,0.7,['There are ' int2str(n_c) ' Uncontrollable Modes']);
A1=a(1:n_c,1:n_c); display_str(0.1,0.5,eig(A1),0)
end
case 12
[a,b,c,t,k]=obsvf(A,B,C); n=size(A,1); n_c=n-sum(k);
if n_c==0
display_str(0.1,0.7,'System is Full Observable');
else
display_str(0.1,0.7,['There are ' int2str(n_c) ' Unobservable Modes']);
A1=a(1:n_c,1:n_c); display_str(0.1,0.5,eig(A1),0)
end
case 13
[V,ss]=stab_detc(A,B);
if length(ss)==0,
display_str(0.1,0.7,'System is Full Controllable');
else
display_str(0.1,0.7,'System is not Full Stabilizable, with Unstabilizable Modes');
display_str(0.1,0.5,ss,0)
end
case 14
[V,ss]=stab_detc(A,C);
if length(ss)==0,
display_str(0.1,0.7,'System is Full Detectable');
else
display_str(0.1,0.7,'System is not Full Detectable, with Undetectable Modes');
display_str(0.1,0.5,ss,0)
end
end
%-----------------------------------------------------------------------
%[V,ss]=stab_detc(A,X) checks the stabilizability or detectability of a
%given system. The syntax is the same as in the lecture note.
%-----------------------------------------------------------------------
function [V,ss]=stab_detc(A,X)
n=length(A); [nc,nr]=size(X); ss=[];
if nc==n & nr==1, A0=A; B0=X;
elseif nc==1 & nr==n, A0=A'; B0=X';
else, errordlg('uncompetible (A,B)'); V=[]; return; end
C=ctrb(A0,B0); nB=rank(C);
if nB==n, V=1;
else
[Ac,Bc,Cc]=ctrbf(A0,B0,ones(1,n));
Anc=Ac(1:n-nB,1:n-nB); ee=eig(Anc);
for i=1:length(ee)
if real(ee(i))>=0, ss=[ss,ee(i)]; end
end
if length(ss)>0, V=0; else, V=1; end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -