⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matx_proc.m

📁 很优良的PID控制器设计仿真程序与模型,经过严格检验
💻 M
📖 第 1 页 / 共 3 页
字号:

%-----------------------------------------------------------------------------
%disp_matrix is used to display a matrix in either TeX or MATLAB format in the 
%Information Display Window or in GUI
%
%   disp_matrix(key,arg1)
%if there is only 1 input argument, then it is 1 for TeX format, 2 for MATLAB 
%format.  If there are two input arguments, then it is displayed in the GUI.
%-----------------------------------------------------------------------------
function disp_matrix(key,arg1)
if nargin==1,
   uu=get(gcf,'UserData'); A=uu{1}{2};
   if ~isa(A,'double'), A=A.a; end
   [nrec,keyJus,err]=matdisp_pars;
   display_str; uu=get(gcf,'UserData'); [nr,nc]=size(A);
   if key==1, strMat='\left[\matrix{'; else, strMat='['; end
   for i=1:nr
      strMat1='   '; 
      for j=1:nc
         if key==1,
            strMat1=[strMat1,display_str(A(i,j),nrec,err)]; 
            if j<nc, strMat1=[strMat1,' & ']; end
         else
            %display the matrix in MATLAB strings
            ss1=display_str(A(i,j),nrec,err); ss1=ss1(find(ss1~='{' & ss1~='}'));
            n=findstr(ss1,'\times');
            if length(n)>0, ss2=ss1(n+9:end); ss1=ss1(1:n-1); end
            strMat1=[strMat1, ss1];
            if length(n)>0, strMat1=[strMat1,'e' ss2]; end
            if j<nc, strMat1=[strMat1,', ']; end
         end
      end
      if i<nr&key==1, strMat1=[strMat1 ' \cr']; end
      strMat=str2mat(strMat,strMat1);
   end
   if key==1, strMat=str2mat(strMat,'}\right]'); 
   else, strMat=str2mat(strMat,']'); end
   %set the display box on the Information Window visible
   set(uu(4),'Visible','on','Max',nr+2,'String',strMat);

else
   h_matmain=findobj('Tag','MatxProcMain');
   uu=get(h_matmain,'UserData'); A=uu{1}{1};  if isa(A,'ss'), A=A.a; end
   if ~issquare(A)& any(7:11==key) ;
      warndlg('Warning: Function of Non-square matrix cannot be evaluated!','Warning: Non-square matrix given!');
      return;   
   end

   switch key
   case 1, tmpA=A; strMatTitle='A'; %display matrix A
   case 2, tmpA=A'; strMatTitle='A^T'; %display the transpose of A
   case 3, %display the inverse matrix of A
      k_inv=0; 
      if ~issquare(A), k_inv=1; 
      elseif rank(A)<length(A), k_inv=1; end
      if k_inv==0, tmpA=inv(A); strMatTitle='A^{-1}';
      else, tmpA=pinv(A); strMatTitle='Pseudo Inverse A^+'; end   
   case 4, tmpA=fliplr(A); strMatTitle='FlipLr(A)'; %display the matrix flipped lift-right
   case 5, tmpA=flipud(A); strMatTitle='FlipUd(A)'; %display the matrix flipped up-down
   case 6, tmpA=rot90(A); strMatTitle='Rotate90(A)'; %display the matrix rotated by 90 degrees
   case 7, tmpA=mat_funs(A,1); strMatTitle='e^A'; %display the exponential matrix of A
   case 8, tmpA=mat_funs(A,2); strMatTitle='sin(A)'; %display the sine matrix of A 
   case 9, tmpA=mat_funs(A,3); strMatTitle='cos(A)'; %display the cosine matrix of A
   case 10, tmpA=funm(A,'log10'); strMatTitle='Log(A)'; %display the logarithmic matrix of A
   case 11, tmpA=sqrtm(A); strMatTitle='Sqrt(A)'; %display the square root matrix of A
   case 12, tmpA=orth(A); strMatTitle='Orthonormal Basis of A'; %display the orthonormal base of matrix A 
   case 13, %display the null space of matrix A
      tmpA=null(A);
      if length(A)==0, display_str; display_str(0.1,0.5,'Matrix A is Singular');
      else, strMatTitle='Null space of A'; end
   end
   uu{1}{2}=tmpA; set(h_matmain,'UserData',uu);
   shw_matrix(0,tmpA,0.05,0.75,strMatTitle);
end

%-----------------------------------------------------------------------
%matx_edit allows the user to edit the matrix elements in the interface.
%-----------------------------------------------------------------------
function matx_edit()
sys_disp;

uu=get(gcf,'UserData'); hA=uu{9}; hControls=uu{8};
key1=0;
if iscell(hA), hB=hA{2}; hC=hA{3}; hD=hA{4}; hA=hA{1}; key1=1; end

[nr,nc]=size(hA);
if key1==1, hA=[reshape(hA,1,prod(size(hA))),hB,hC,hD];
else, hA=reshape(hA,1,prod(size(hA))); end

while 1, %check whether the selected objects a handle of the matrix/state space model element
   [x,y,button]=ginput(1); key=0;
   if button~=1, break; 
   else   
      for i=1:length(hA), if (gco==hA(i)), key=1; break; end, end
   end
   if key==1, break; end
end   

if key==1,
   %if it is an element, then set the Edit mode
%   set(hControls(10),'UserData',hA(i)); 
   xx=get(hControls(9),'Position'); xx(1:2)=get(gcf,'CurrentPoint'); 
   set(hControls([9,11]),'Visible','on');
   set(hControls(9),'Position',xx,'String',flt_replace(get(hA(i),'String')),'UserData',hA(i)); 
   if key1==0, uu{10}{1}=1; set(gcf,'UserData',uu); end   
end

%-------------------------------------------------------------
%matx_update update the current matrix 
%-------------------------------------------------------------
function matx_update()

hA1=get(gco,'UserData'); uu=get(gcf,'UserData'); hControls=uu{8};
h_obj=get(hControls(9),'UserData'); set(hA1,'String',get(hControls(9),'String'));
set(hControls(9),'Visible','off');

hA=uu{9}; key=0; g_sys=uu{1}{1};
if iscell(hA), 
   A=g_sys.a; B=g_sys.b; C=g_sys.c; D=g_sys.d;
   key=1; hB=hA{2}; hC=hA{3}; hD=hA{4}; hA=hA{1};%key=1 for state space model
else, A=g_sys; end

[nr,nc]=size(hA);

for i=1:nr
   for j=1:nc, 
      if hA(i,j)==h_obj, A(i,j)=eval(get(hA(i,j),'String')); end
   end
   if key==1
      if hB(i)==h_obj, B(i,1)=eval(get(hB(i),'String')); end
      if hC(i)==h_obj, C(1,i)=eval(get(hC(i),'String')); end
end, end
if key==1, 
   if hD==h_obj, D=eval(get(hD,'String')); end 
   uu{1}{1}=ss(A,B,C,D); uu{1}{2}=uu{1}{1};
else, uu{1}={A,A}; end

set(gcf,'UserData',uu);

if key==1
   if ~isa(uu{1}{1},'double')
      %update the system plant model in CtrlLAB
      uu0=get(findobj('Tag','CtrlLABMain'),'UserData');
      g_sys=uu{1}{1};
      a_str=mat2str(g_sys.a); b_str=mat2str(g_sys.b);
      c_str=mat2str(g_sys.c); d_str=mat2str(g_sys.d);
      set(uu0{1}(1),'UserData',{2,uu{1}{1},a_str,b_str,c_str,d_str});
   end   
end

%-------------------------------------------------------------
%matdisp_pars obtains the display format
%
%   [nrec,njust,err]=matdisp_pars()
%where 
%  nrec -- the number of digit to display
%  njust -- the justification of display
%  err -- the zero truncation format.  err=0 for rational
%-------------------------------------------------------------
function [nrec,njust,err]=matdisp_pars();

aa=extra_funs(5,4,'Checked',2:4);
switch aa
case 1, nrec=8; 
case 2, nrec=4;
case 3, nrec=0;
end
njust=extra_funs(5,4,'Checked',5:7); aa=extra_funs(5,4,'Checked',10:13);
switch aa
case 1, err=0; 
case 2, err=1e-14;
case 3, err=1e-15;
case 4, err=1e-16;
end

%-------------------------------------------------------------
%shw_matpars shows the properties of the given matrix
%-------------------------------------------------------------
function shw_matpars(key)

switch key
case {1,2,3,6,7,8,9,10,11}, matx_pars(key,0.1,0.6);
case {4,5}, [xL,A]=matx_pars(key,0.1,0.6);
case 12
   xL=0.07; yL=0.45; [xL,A]=matx_pars(7,xL,yL);
   xL=xL+0.05; xL=matx_pars(8,xL,yL,A); xL=xL+0.05; xL=matx_pars(9,xL,yL,A);
   xL=matx_pars(10,xL,yL,A);    
case 13
   uu=get(gcf,'UserData'); A=uu{1}{1};
   if isa(A,'ss'), A=A.a; end   
   keyy=issquare(A);
   xL0=0.03; yL=0.63; [xL0,A]=matx_pars(5,xL0,yL);
   xL3=0.03; yL=0.92; if keyy, xL3=matx_pars(4,xL3,yL,A); end
   xL1=0.03; yL=0.35; if keyy, xL1=matx_pars(1,xL1,yL,A); end
   xL1=xL1+0.05; xL1=matx_pars(3,xL1,yL,A);
   xL1=xL1+0.05; xL1=matx_pars(6,xL1,yL,A);
   xL1=xL1+0.05; xL1=matx_pars(2,xL1,yL,A);
   xL2=0.03; yL=0.17; xL2=matx_pars(7,xL2,yL,A);
   xL2=xL2+0.05; xL2=matx_pars(8,xL2,yL,A);
   xL2=xL2+0.05; xL2=matx_pars(9,xL2,yL,A);
   xL2=xL2+0.05; xL2=matx_pars(10,xL2,yL,A);
   if ~keyy, 
      warndlg('Warning: some of the parameters not analyzable!','Warning: Non-square matrix given!');
   end   
end

%-------------------------------------------------------------
%matx_pars evaluates parameters of a given matrix
%-------------------------------------------------------------
function [xL,A]=matx_pars(key,xL,yL,A)

if nargin==3, 
   uu=get(gcf,'UserData'); A=uu{1}{1};
   if isa(A,'ss'), A=A.a; end   
end

if nargout~=1, display_str; end

%check whether the matrix is a square one for some analysis
if any([1,4,11]==key), 
   if ~issquare(A), 
      warndlg('Parameters cannot be evaluated!','Warning: Non-square matrix given!');
      return;
   end   
end

switch key
case {1,3,6}
   switch key
   case 1, atmp=det(A); str1='det(A)=';
   case 3, atmp=trace(A); str1='tr(A)=';
   case 6, atmp=cond(A); str1='cond(A)=';
   end
   xL=display_str(xL,yL,[str1,display_str(atmp)]);
case 2, xL=display_str(xL,yL,['rank(A)=' int2str(rank(A))]);
case {4,5}
   switch key
   case 4, atmp=eig(A); str1='Eigenvalues of Matrix';
   case 5, atmp=svd(A); str1='Singular Values of Matrix';
   end
   display_str(xL,yL,str1);
   xL=display_str(xL+0.05,yL-0.14,atmp,0);
case {7,8,9,10}
   switch key
   case 7, atmp=norm(A,1); str1='1';
   case 8, atmp=norm(A,2); str1='2';
   case 9, atmp=norm(A,Inf); str1='\infty';
   case 10, atmp=norm(A,'fro'); str1='F';
   end
   xL=display_str(xL,yL,['||A||_',str1 '=' display_str(atmp)]);
case 11
   display_str(xL,yL,'Characteristic Polynomial of Matrix');
   xL=display_str(xL+0.05,yL-0.18,display_str(poly(A)));
end

%-------------------------------------------------------------
%sys_disp displays the system state space format in the window
%-------------------------------------------------------------
function sys_disp(g_sys)

h_matmain=findobj('Tag','MatxProcMain'); uu=get(h_matmain,'UserData'); 
if nargin==0, g_sys=uu{1}{1}; end

hControls=uu{8}; frame_setting(hControls);

if ~isa(g_sys,'double')
   if ~isa(g_sys,'ss'), g_sys=ss(g_sys); end
   xL=0.05; yL=0.85; [xL_a,yL,hA]=shw_matrix(0,g_sys.a,xL,yL,'A matrix');
   vec=get(h_matmain,'Position'); YSpace=0.0417/vec(4); yL=yL-YSpace;
   [xL_b,yL,hB]=shw_matrix(0,g_sys.b',xL,yL,'B vector transpose'); yL=yL-YSpace;
   [xL_c,yL,hC]=shw_matrix(0,g_sys.c,xL,yL,'C vector'); yL=yL-YSpace;
   [xL_d,yL,hD]=shw_matrix(0,g_sys.d,xL,yL,'D constant');
   uu{9}={hA,hB,hC,hD}; uu{1}{2}=g_sys.a; set(h_matmain,'UserData',uu); xL=max([xL_a,xL_b,xL_c]);   
   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   
elseif ~isempty(g_sys), disp_matrix(1,[]); end   

%-----------------------------------------------------------------------------
%matx_file manipulates the File menu in the program
%
%   key=matx_file(nTask)
%where 
%  nTask=1 for New matrix creation
%        2 for Open
%        3, 4 for Save, Save As
%        5 for Exit
%the other values of nTask can be found in the comments of the function itself.
%------------------------------------------------------------------------------
function key=matx_file(nTask)
h_matmain=findobj('Tag','MatxProcMain'); uu=get(h_matmain,'UserData');
switch nTask
case 1
   h_win=findobj('Tag','MatxProcNew'); 
   if length(uu)>0
      A=uu{1}{1}; 
      if isa(uu{1}{1},'double'), [n,m]=size(A); else, [n,m]=size(A.a); end
   else, n=3; m=3; end
   if n*m==0, n=3; m=3; end
   if length(h_win)==0
      h_win=figure('Units','normalized','Position',[0.22125 0.232 0.425 0.3],...
         'MenuBar','none','Color',0.8*[1,1,1],'Interruptible','on','Tag','MatxProcNew',...
         'NumberTitle','off','Name','New Matrix Initialisation','Resize','off');
      extra_funs(1);
      hh(1)=uicontrol('Style','PushButton','String','Create',...
         'Units','normalized','Position',[0.80,0.80,0.16,0.13],'CallBack','matx_proc(0,7);');
      hh(2)=uicontrol('Style','PushButton','String','Cancel',...
         'Units','normalized','Position',[0.80,0.65,0.16,0.13],'CallBack','close(gcf);');
      hh(8)=uicontrol('Style','PushButton','String','Help',...
         'Units','normalized','Position',[0.80,0.50,0.16,0.13],'CallBack','clab_help(28);');
      [x,hh(11)]=display_str(0.05,0.66,'Number of Rows',[0,0,0],'on',9);
      hh(3)=uicontrol('Style','Edit','String',int2str(n),...
         'Units','normalized','Position',[0.32,0.59,0.10,0.10],...
         'HorizontalAlignment','left','BackgroundColor',[1,1,1]);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -