📄 bodeplotgui.m
字号:
% 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 beginning of divStr. dStr=[' ' blanks(floor((LnStr-LdStr)/2)) dStr]; %pad denominator. else %the demoninator is longer than numerator, pad numerator. n=LdStr; nStr=[' ' blanks(floor((LdStr-LnStr)/2)) nStr]; dStr=[' ' dStr]; end divStr=[]; for i=1:n, divStr=[divStr '-']; end divStr=['H(s) = ' divStr]; set(handles.TransferFunctionText,'String',strvcat(nStr,divStr,dStr)); %Change type font and size. set(handles.TransferFunctionText,'FontName','Courier New') %set(handles.TransferFunctionText,'FontSize',10) guidata(handles.AsymBodePlot, handles); %save changes to handles.end% ------------------End of function BodePlotDispTF -------------------% --------------------------------------------------------------------function BodePlotLegend(handles) %This function creates the legends for the Bode plot being displayed. %It also makes four changes to the "handles" structure. % 1) It updates the array "IncElem" that holds the indices that determine % which elements are included in the Bode plot. % 2) It updates the sring array "IncStr" that hold the description of % each included elements. % 3) Updates ExcElem that holds indices of excluded elements. % 4) Updates ExcStr that hold descriptions of excluded elements. %Load the terms and the plotting strings into local variables for convenience. Terms=handles.Terms; axes(handles.LegendPlot); %Set axes to the legend widow, cla; % and clear it. Xleg=[0 0.2]; %Xleg holds start and end of line segment for legend. XlegText=0.25; %XlegText is location of text. FntSz=8; %Font Size of text. y=1-1/(length(Terms)+6); %Vertical location of first text item plot(Xleg,[y y],... 'Color',handles.exactColor,... 'LineWidth',handles.LnWdth/2); %Plot line for legend. text(XlegText,y,'Exact Bode Plot','FontSize',FntSz); %Place text hold on; if (get(handles.ShowAsymptoticCheckBox,'Value')~=0), y=1-2/(length(Terms)+6); %Vertical location of second item plot(Xleg,[y y],... 'Color',handles.exactColor,... 'LineStyle',':',... 'LineWidth',handles.LnWdth); %Plot line for legend. text(XlegText,y,'Asymptotic Plot','FontSize',FntSz); %Place text end y=1-3/(length(Terms)+6); %Vertical location of third item. plot(Xleg,[y y],... %Line. 'Color',handles.zrefColor,... 'LineWidth',2); text(XlegText,y,'Zero Value (for reference only)','FontSize',FntSz); %Text. IncElem=[]; %The indices of elements to be included in plot. ExcElem=[]; %The indices of elements to be excluded from plot. IncStr=''; %An array of strings describing included elements. ExcStr=''; %An array of strings describing excluded elements. %These variables are used as local counters later. Here they are initialized. i1=1; i2=1; for i=1:length(Terms), %For each term, %Tv is a local variable representing the pole location. It is used solely % for convenience. Tv=Terms(i).value; %Tm is a local variable representing the pole multiplicity. It is used solely % for convenience. Tm=Terms(i).multiplicity; %S2 is a blank string to be added to later in the loop. S2=''; %The next section of code ("switch" statement) plus a few lines, creates %a string that describes the pole or zero, its location, muliplicity... %The variable "DescStr" hold a Descriptive String for the pole or zero. The %string "S2" is a Second String that holds additional information (if needed) switch Terms(i).type, case 'Constant', %If the term is a consant, print its value in a string. DescStr=sprintf('Constant = %0.2g (%0.2g dB)',Tv,20*log10(Tv)); case 'RealPole', %If the term is a real pole, print its value in string. DescStr=sprintf('Real Pole at %0.2g',Tv); case 'RealZero', %If the term is a real zero, print its value in string. DescStr=sprintf('Real Zero at %0.2g',Tv); if real(Tv)>0, DescStr=[DescStr ' RHP (Non-min phase)']; end; case 'ComplexPole', %If the term is a complex pole, print its value in string. %However, do this in terms of natural frequency and damping, as %well as the actual location of the pole (in S2). wn=abs(Tv); theta=atan(abs(imag(Tv)/real(Tv))); zeta=cos(theta); DescStr=sprintf('Complex Pole at wn=%0.2g, zeta=%0.2g',wn,zeta); if (zeta < (1/sqrt(2))), %peaking only if zeta<0.707 S2=sprintf('(%0.2g +/- %0.2gj) Circle shows peak height.',real(Tv),imag(Tv)); else S2=sprintf('(%0.2g +/- %0.2gj) (no peaking, zeta<0.707)',real(Tv),imag(Tv)); end case 'ComplexZero', %If the term is a complex zero, print its value in string. %However, do this in terms of natural frequency and damping, as %well as the actual location of the zero (in S2). %Also note if it is a non-minimum phase zero. wn=abs(Tv); theta=atan(abs(imag(Tv)/real(Tv))); zeta=cos(theta); DescStr=sprintf('Complex Zero at wn=%0.2g, zeta=%0.2g',wn,zeta); if real(Tv)>0, DescStr=[DescStr ' (RHP, Non-min phase)']; end; if (zeta < (1/sqrt(2))), %peaking only if zeta<0.707 S2=sprintf('(%0.2g +/- %0.2gj) Circle shows peak height.',real(Tv),imag(Tv)); else S2=sprintf('(%0.2g +/- %0.2gj) (no peaking, zeta<0.707)',real(Tv),imag(Tv)); end case 'OriginPole', %If pole is at origin, not this. DescStr=sprintf('Pole at origin'); case 'OriginZero', %If zero is at origin, not this. DescStr=sprintf('Zero at origin'); end %If multiplicity is greater than one, not this as well. if Tm>1, DescStr=[DescStr sprintf(', mult=%d',Tm)]; end %At this point we have a string (in "DescStr" and "S2"). if Terms(i).display, %If the term is to be included in plot.... IncStr=strvcat(IncStr,DescStr); %Add the Desriptive String to IncStr IncElem(i1)=i; %Add the appropriate index to the Included Elements list. i1=i1+1; %Increment the index counter y=1-(i1+2)/(length(Terms)+6); %Calculate the vertical position. plot(Xleg,[y y],... %Plot the line. 'LineWidth',handles.LnWdth,... 'LineStyle',GetLineStyle(i,handles),... 'Color',GetLineColor(i,handles)); text(XlegText,y,strvcat(DescStr,S2),'FontSize',FntSz); %Add the text. else %The term is *not* to be included in plot, so... ExcStr=strvcat(ExcStr,DescStr); %Add its Desriptive String to ExcStr ExcElem(i2)=i; %Add the appropriate index to the Excluded Elements list. i2=i2+1; %Increment the index counter. end end hold off; %Get rid of ticks around plot. axis([0 1 0 1]); set(gca,'Xtick',[]); set(gca,'Ytick',[]); %At this point the legend is completed. Next we will make up the strings %for the boxes that separately list included and excluded elements. IncStr=strvcat(IncStr,' '); %Add a blank line to IncStr. IncStr=strvcat(IncStr,'-------'); %Add a series of dashes. %If there are any included elements, we can click on box to exclude it. if i1~=1 IncStr=strvcat(IncStr,'Select element to exclude from plot'); end ExcStr=strvcat(ExcStr,' '); %Add a blank line to ExcStr ExcStr=strvcat(ExcStr,'-------'); %Add a series of dashes. %If there are any excluded elements, we can click on box to include it. if i2~=1 ExcStr=strvcat(ExcStr,'Select element to include in plot'); end %Set the strings for included and excluded elements. set(handles.IncludedElements,'String',IncStr); set(handles.ExcludedElements,'String',ExcStr); %Change the arrays holding included and excluded elements in the handles array. handles.IncElem=IncElem; handles.ExcElem=ExcElem; guidata(handles.AsymBodePlot, handles); %save changes to handles.end% ------------------End of function BodePlotLegend --------% --------------------------------------------------------------------% --- Executes during object creation, after setting all properties.function LineWidth_CreateFcn(hObject, eventdata, handles) % hObject handle to LineWidth (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called if ispc set(hObject,'BackgroundColor','white'); else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); endend% ------------------End of function BodePlotLegend --------% --------------------------------------------------------------------% Set the width of the lines used in plots.function LineWidth_Callback(hObject, eventdata, handles) % hObject handle to LineWidth (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) handles.LnWdth=str2num(get(handles.LineWidth,'String')); guidata(handles.AsymBodePlot, handles); %save changes to handles. BodePlotter(handles); %Plot the Transfer function. handles=guidata(handles.AsymBodePlot); %Reload handles after function call.end% ------------------End of function BodePlotLegend --------% --------------------------------------------------------------------% Determines the line color of the ith plot.function linecolor=GetLineColor(i,handles); numColors=size(handles.colors,1); %Cycle through colors by using mod operator. linecolor=handles.colors(mod(i-1,numColors)+1,:);end% ------------------End of function GetLineColor --------% --------------------------------------------------------------------% Determines the line style of the ith plot.function linestyle=GetLineStyle(i,handles); numColors=size(handles.colors,1); numLnStl=size(handles.linestyle,2); %Cycle through line styles, incrementing after all colors are used. linestyle=handles.linestyle{mod(ceil(i/numColors)-1,numLnStl)+1};end% ------------------End of function GetLineStyle --------% --------------------------------------------------------------------% --- Executes on button press in GrayCheckBox.% This function sets the sequence of colors used in plotting to gray scales.function GrayCheckBox_Callback(hObject, eventdata, handles) if get(handles.GrayCheckBox,'Value')==1, %If button is not set, handles.colors=handles.Gray; %and set colors to gray scale handles.zrefColor=handles.GrayZero; else handles.colors=handles.Color; %and set colors to RGB handles.zrefColor=handles.ColorZero; end guidata(handles.AsymBodePlot, handles); %save changes to handles. BodePlotter(handles); %Plot the Transfer function. handles=guidata(handles.AsymBodePlot); %Reload handles after function call.end% ------------------End of function BodePlotLegend --------% --- Executes on button press in GridCheckBox.function GridCheckBox_Callback(hObject, eventdata, handles) BodePlotter(handles); %Plot the Transfer function.end% --- Executes on button press in RadianCheckBox.function RadianCheckBox_Callback(hObject, eventdata, handles) BodePlotter(handles); %Plot the Transfer function.end% --- Executes on button press in WebButton.function WebButton_Callback(hObject, eventdata, handles) web('http://www.swarthmore.edu/NatSci/echeeve1/Ref/Bode/Bode.html','-browser')end% --- Executes on button press in ShowAsymptoticCheckBox.function ShowAsymptoticCheckBox_Callback(hObject, eventdata, handles) BodePlotter(handles); %Plot the Transfer function.end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -