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

📄 rlocusgui.m

📁 在Matlab中可用的根轨迹图例子源码
💻 M
📖 第 1 页 / 共 2 页
字号:
function varargout = RLocusGui(varargin)
% RLocusGui M-file for RLocusGui.fig
%
% Proper syntax for calling the function is RLocusGui(sys), where "sys"
% is a transfer function object (this requires the control-systems
% toolbox.)
%
% The function take the transfer function and and plots the root locus
% (using Matlab's "rlocus" command.  It then describes and illustrates
% all of the "textbook" rules for plotting the root locus.  It also creates
% a web page that demonstrates all of the rules.
%
% It was created using "GUIDE," Matlab's GUI creation tool.
%
%Written by Erik Cheever (Copyright 2007)
%Contact: erik_cheever@swarthmore.edu
% Erik Cheever
% Dept. of Engineering
% Swarthmore College
% 500 College Avenue
% Swarthmore, PA 19081  USA

% Last Modified by GUIDE v2.5 12-Feb-2007 12:43:57

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
   'gui_Singleton',  gui_Singleton, ...
   'gui_OpeningFcn', @RLocusGui_OpeningFcn, ...
   'gui_OutputFcn',  @RLocusGui_OutputFcn, ...
   'gui_LayoutFcn',  [] , ...
   'gui_Callback',   []);
if nargin && ischar(varargin{1})
   gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
   [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
   gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


%% The code below is split into several "cells."  (See MatLab docs).------
% The next cell contains all of the code that Matlab's GUIDE created that
% was not modified, and is empty.
%
% This is followed by initialization code.
%
% This is followed by utility functions that are called by several
% other functions.  This section also includes code that runs GUI.
%
% This is followed by the code that generates the web page html.
%
% The last cell includes all of the code that describes the "Root Locus
% Rules"
%-------------------------------------------------------------------------


%% Empty Matlab code (no comments) ---------------------------------------
% --- Outputs from this function are returned to the command line.
function varargout = RLocusGui_OutputFcn(hObject, eventdata, handles)
%Empty function (no output arguments)

% --- Executes on selection change in lbRuleDescr.
function lbRuleDescr_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function lbRuleDescr_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),...
      get(0,'defaultUicontrolBackgroundColor'))
   set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function sldKIndex_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), ...
      get(0,'defaultUicontrolBackgroundColor'))
   set(hObject,'BackgroundColor',[.9 .9 .9]);
end
%-------------------------------------------------------------------------


%% Initialization code
% --- Executes just before RLocusGui is made visible. --------------------
% This function makes sure inputs are appropriate, and
% also determines fundamental information about transfer
% function (order of numerator and denominator polynomials...).
function RLocusGui_OpeningFcn(hObject, eventdata, handles, varargin)
%The first time the code is called, it is slow to start up, so display
% a "waitbar" dialog.
hWait=waitbar(0.1,'Please wait while GUI initializes.');
nargin=length(varargin);
%Make sure we have one argument, and that it is a 'tf'
if (nargin~=1) || (~isa(varargin{1},'tf'))
   disp(' ');
   disp('Root Locus Plotter - proper usage is ''RLocusGui(Sys)'',')
   disp('  where ''Sys'' is a transfer function object.');
   disp(' '); disp(' ');
   close(handles.RLocusGuiFig);
   close(hWait);
   return
end
disp(' '); disp(' ');

Sys=minreal(varargin{1});   %Get minimum realization.
% Get numerator and denominator of two realizations.  If their
% lengths are unequal, it means that there were poles and zeros that
% cancelled.
[n1,d1]=tfdata(Sys,'v');
[n2,d2]=tfdata(varargin{1},'v');
if (length(n1)~=length(n2)),
   disp('***************************Warning*****************************');
   disp('Original transfer function was:');
   varargin{1}
   disp('Some poles and zeros were equal.  After cancellation:');
   Sys
   disp('The simplified transfer function is the one that will be used.');
   disp('**************************************************************');
   disp(' ');
   beep;
   s{1}='System has poles and zeros that cancel.';
   s{2}='See command window for details.';
   waitfor(warndlg(s));
end
handles.Sys=Sys;    %The variable Sys is the transfer function.

% Choose default command line output for RLocusGui.
% This was generated by Matlab, but is never used
handles.output = hObject;

handles.ColorOrder=get(gca,'ColorOrder');  %Save color order;
handles.HighlightColor=[1 0.75 0.75];      %Color for highlights (pink).
waitbar(0.25);
guidata(hObject, handles);      % Save changes to handle.
getSysInfo(hObject, handles);   % Get useful information about Xfer func.
handles=guidata(hObject);       % Reload handles (changed in getTFInfor)
waitbar(0.5);
InitRlocus(handles);
waitbar(0.75);
set(handles.rbInfo,'Value',1);   %Choose first radio button.
%Display it as the "SelectedObject"
set(handles.panelChooseRule,'SelectedObject',handles.rbInfo)
set(handles.lbRuleDescr,'String',RuleInfo(handles));
set(handles.axRules,'Visible','off');  %Hide second plot.
set(handles.txtKval,'Visible','off');  %Hide text on plot.
set(handles.sldKIndex,'visible','off');%Hide slider.
set(handles.txtKeq0,'visible','off');  %Hide "K=0" text for slider.
set(handles.txtKeqInf,'visible','off');%Hide "K=Inf" text for slider.
set(handles.cbInteract,'visible','off');%Hide Checkbox.
% This next variable is a kludge.  For the last two "rules," the user can
% interact with the GUI.  This is distinct from the others, and I needed
% a way to keep track of this.  If interaction is ongoing, the variable is
% set to 1.  Normally it will be 0.
handles.interactive=0;
handles.kInd=0;   %Index into array of gain (K) values.

RLocusDispTF(handles);      % Disp Xfer function

guidata(hObject, handles);  % Update handles structure
waitbar(1.0);
close(hWait);
% ------------------------------------------------------------------------


% ------------------------------------------------------------------------
function RLocusDispTF(handles)
% This function displays a tranfer function that is a helper function.
% It takes the transfer function of the and splits it
% into three lines so that it can be displayed nicely.  For example:
% "            s + 1"
% "H(s) = ---------------"
% "        s^2 + 2 s + 1"
% The numerator string is in the variable nStr,
% the second line is in divStr,
% and the denominator string is in dStr.

% Get numerator and denominator.
[n,d]=tfdata(handles.Sys,'v');
% Get string representations of numerator and denominator
nStr=poly2str(n,'s');  dStr=poly2str(d,'s');
% Find length of strings.
LnStr=length(nStr);  LdStr=length(dStr);

if LnStr>LdStr,
   %the numerator is longer than denominator string, so pad denominator.
   n=LnStr;                  %n is the length of the longer string.
   nStr=['        ' nStr];   %add spaces for characters at start of divStr.
   dStr=['        ' blanks(floor((LnStr-LdStr)/n)) dStr]; %pad denominator.
else
   %the demoninator is longer than numerator, pad numerator.
   n=LdStr;
   nStr=['        ' blanks(floor((LdStr-LnStr)/n)) nStr];
   dStr=['        ' dStr];
end

divStr='G(s)H(s)= ';
for i=1:n,  divStr=[divStr '-']; end
set(handles.txtXfer,'String',{nStr,divStr,dStr});
%Change type font and size.
set(handles.txtXfer,'FontName','Courier New')
set(handles.txtXfer,'FontSize',8)

guidata(handles.RLocusGuiFig, handles);  %save changes to handles.
% ------------------End of function RLocusDispTF -------------------------


% ------This function gets all of the information from transfer function.-
function getSysInfo(hObject, handles)
sys=handles.Sys;
[num,den]=tfdata(sys,'v'); %Get (and save) numerator and denominator.
handles.Num=num; handles.Den=den;
[z,p]=zpkdata(sys,'v');  %Get zeros and poles (to accuracy of 0.01)
z=round(real(z)*100)/100+j*round(imag(z)*100)/100;
p=round(real(p)*100)/100+j*round(imag(p)*100)/100;
realZIndex=find(abs(imag(z))<1E-3); %Determine which are real (i.e., on
realPIndex=find(abs(imag(p))<1E-3); % the real axis)...
z(realZIndex)=real(z(realZIndex));  % and set imag part to zerp.
p(realPIndex)=real(p(realPIndex));
handles.Z=z; handles.P=p;  %Store zeros and poles.

m=length(z); n=length(p);  %Length of numerator and denominator.
q=n-m;                     %Number of zeros at infinity.
handles.M=m; handles.N=n; handles.Q=q; %Store values.

[r,k1]=rlocus(sys);  % Let Matlab calculate appropriate range for k
for i=1:(length(k1)-1), %Generate intermediate points (smoother plots)
   k(2*(i-1)+1)=k1(i);  %Take value of k, but also...
   k(2*i)=(k1(i)+k1(i+1))/2;   %generate new point between consecutive k's
end
[r,k]=rlocus(sys,k);       %Recalculate with finer sampling of k.
handles.R=r; handles.K=k;  %Save.

% Slider for "k" has stops at each value of "k."
set(handles.sldKIndex,'Max',length(k));
set(handles.sldKIndex,'Min',1);
set(handles.sldKIndex,'SliderStep',[1/length(k) 2/length(k)]);
set(handles.sldKIndex,'Value',1);

% Get information for autoscaling.  This is largely a kludge.  Determine
% the min and max value of the axes as a multiple ("scale") of the min and
% max values of the zeros and poles of the transfer function.
scale=1.5;
if ~isempty(z),
   rlPzMin=min(min(real(p)),min(real(z)))*scale;

⌨️ 快捷键说明

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