1. 程式人生 > >歸一化二階系統的階躍響應之matlab GUI+程式打包實現

歸一化二階系統的階躍響應之matlab GUI+程式打包實現

歸一化系統的二階階躍響應之matlab GUI+程式打包實現

  1. 設計程式效果展示
    1.1.list選項:選擇第一項效果展示
    選項1
    1.2.list選項:選擇第二項效果展示
    選項2
    1.3.list選項:選擇第三項效果展示
    選項3
    1.4.list選項:選擇第四項效果展示
    選項4
  2. 設計思想
    為了研究歸一化二階系統G(s)中阻尼比對單位階躍響應的影響,於是做了上述1中的效果介面來做視覺化分析和展示。G(s)公式如下:其中s是時間,單位秒,另一個字母為阻尼比。
    二階階躍系統
    在matlab命令列中輸入"guide"命令,然後再彈出的介面中選擇"新建GUI",然後選擇Blank GUI(Default)即可。按照1中的效果圖在介面中依次拖選"軸Axes",“靜態文字”,“可編輯文字"等即可。詳細屬性設定如下:
    2.1.雙擊軸位框,在"屬性檢查器”,進行如下設定:
    FontUnits和Units均設定為normalized;XLimNode和 YLimNode均設定為normalized。
    2.2.靜態文字元件設定:
    FontUnits和Units均設定為normalized;String設定為"歸一化二階系統的階躍響應"
    2.3.面板設定:
    FontUnits為points和Units為normalized;Title為"阻尼比zeta";TitlePosition為lefttop。
    2.4.可編輯文字設定:
    FontUnits和Units均設定為normalized。
    2.5.滑動條Slider設定:
    FontUnits和Units均設定為normalized;Min為0,Max為2;SliderStep中x為0.01,y為0.1;Value為0。
    2.6.grid on設定:
    FontUnits和Units均設定為normalized;String為Grid on;Tag為gridon;Value為0。
    2.7.grid off設定:
    FontUnits和Units均設定為normalized;String為Grid off;Tag為gridoff;Value為1。
    2.8.ListBox設定:
    FontUnits和Units均設定為normalized;Min為0,Max為2;String設定時,單擊一下會彈出介面。分別輸入不標特徵點、上升時間點(0->0.95)、最大峰值點、鎮定時間點(0.95,1.05),各佔一行即可;Value為1.
    最後畫完圖及設定好屬性後,點選繪圖中的"儲存"按鈕將檔案儲存成exm080201.fig即可,同時也會生成對應的程式碼。然後參看下面展示的程式碼來完成回撥函式程式碼填充即可。
  3. 程式碼實現
    為了兼顧讀著學習和查閱程式碼,本文使用分割線#begin##和#end##標註出需要追加程式碼的部分方便讀著理解和學習。

3.1.exm080201.m檔案

%% exm080201.m
function varargout = exm080201(varargin)
% EXM080201 MATLAB code for exm080201.fig
%      EXM080201, by itself, creates a new EXM080201 or raises the existing
%      singleton*.
%
%      H = EXM080201 returns the handle to a new EXM080201 or the handle to
%      the existing singleton*.
%
%      EXM080201('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in EXM080201.M with the given input arguments.
%
%      EXM080201('Property','Value',...) creates a new EXM080201 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before exm080201_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to exm080201_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help exm080201

% Last Modified by GUIDE v2.5 02-Oct-2018 18:22:28

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @exm080201_OpeningFcn, ...
                   'gui_OutputFcn',  @exm080201_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

%% 下面兩個函式時介面啟動子函式和輸出子函式
% --- Executes just before exm080201 is made visible.
function exm080201_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to exm080201 (see VARARGIN)
% Choose default command line output for exm080201
%% ##############begin###############
zeta=0.3; %初始化阻尼比
set(handles.edit1,'String',num2str(zeta));
set(handles.slider1,'Value',zeta);
set(handles.gridon,'Value',0); %單選處於"非選"狀態
set(handles.gridoff,'Value',1); %單選處於"選中"狀態
set(handles.listbox1,'Value',1);
handles.t=0:0.05:15; %自定時間取樣陣列
handles.Color='Red'; %定義響應曲線的初始色彩
handles.zeta=zeta;
handles.flag=0; %初始繪圖標誌
handles=surfplot(handles);
handles.Color='Blue'; %定義響應曲線的非初始色彩
%% ##############end#################
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes exm080201 wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = exm080201_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;

%% 單選按鈕 Grid on回撥子函式
% --- Executes on button press in gridon.
function gridon_Callback(hObject, eventdata, handles)
% hObject    handle to gridon (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of gridon
%% ##############begin###############
set(handles.gridoff,'Value',0)
grid on %畫出網格線
%% ##############end#################

%% 單選按鈕 Grid off回撥子函式
% --- Executes on button press in gridoff.
function gridoff_Callback(hObject, eventdata, handles)
% hObject    handle to gridoff (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of gridoff
%% ##############begin###############
set(handles.gridon,'Value',0)
grid off %畫出網格線
%% ##############end#################

%% 列表框回撥子函式
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox1
%% ##############begin#################
listindex=get(hObject,'Value'); %獲得列表框所有選項的序號
if any(listindex==1) %使得第一項只能單獨備選
    set(handles.listbox1,'Value',1)
end
handles.flag=0; %列表框被觸發時,發出重畫曲面命令
handles=surfplot(handles);
handles.flag=1;
guidata(hObject, handles);
%% ##############end###################

% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


%%  可編輯文字框回撥子函式
function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double
%% ##############begin###############
sz=get(hObject,'String'); %從編輯文字框讀取輸入字元
zeta=str2double(sz);  %將字元轉成雙精度
%handles.slider1直接看各個控制元件物件的Tag標籤即可。
set(handles.slider1,'Value',zeta) %對話滑塊的油表定位
handles.zeta=zeta; %"GUI資料"形式,儲存資料以便共享
handles=surfplot(handles); %呼叫繪製子圖函式,繪製響應曲線
guidata(hObject, handles);
%% ##############end#################

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%% 滑塊回撥子函式組
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
%% ##############begin###############
zeta=get(hObject,'Value'); %獲得滑塊的遊標數值
set(handles.edit1,'String',num2str(zeta)) %將滑塊遊標數值填充到文字框中
handles.zeta=zeta; %更新阻尼係數
handles=surfplot(handles); %呼叫繪製子圖函式,繪製響應曲線
guidata(hObject, handles);

%% ##############end#################

% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

% --- Executes on key press with focus on listbox1 and none of its controls.
function listbox1_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  structure with the following fields (see MATLAB.UI.CONTROL.UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)

3.2.surfplot.m檔案

%% surfplot.m
function handles=surfplot(handles)
%handles=surfplot(handles) %供啟動子函式和個控制元件回撥子函式呼叫的繪圖函式
zeta=handles.zeta ;%僅為記述簡單
t=handles.t;
listindex=get(handles.listbox1,'Value'); %讀取列表框的選項序號陣列
Nt=length(t);
if handles.flag==0 %僅在介面啟動時執行
    cla %清空截面上次工作後可能殘留的軸上物件
    zmin=get(handles.slider1,'Min'); %讀取滑塊的min
    zmax=get(handles.slider1,'Max'); %讀取滑塊的max
    zt=zmin:0.05:zmax; %為3維座標"x軸"準備取樣點
    Nz=length(zt);
    [ZT,T]=meshgrid(zt,t); %3位曲線的"X","Y"取樣點陣
    Y=zeros(Nt,Nz);
    for k=1:Nz
        Y(:,k)=step(tf(1,[1,2*zt(k),1]),t);
    end
    surface(ZT,T,Y) %用底層繪圖命令,繪製曲線
    shading flat
else
    delete(handles.g1) %利用控制代碼,刪除上次繪製的殘留曲線
    delete(handles.rline) %利用控制代碼,刪除上次繪製的響應曲線
end
xz=ones(1,Nt)*zeta;
y1=ones(1,Nt)*1;
y=step(tf(1,[1,2*zeta,1]),t); %在指定的Zeta下,計算響應曲線
gz=[zeta,zeta,xz,zeta,zeta,xz]; %綠色封閉參考線的x座標
gt=[t(1),t(1),t,t(end),t(end),fliplr(t)] %綠色封閉參考線的y座標
gy=[0,1,y1,1,0,0*y1] %綠色封閉參考線的z座標
handles.g1=line(gz,gt,gy,'Color','g','LineWidth',1); %繪製綠色參照線,併產生控制代碼
handles.rline=line(xz,t,y,'Color',handles.Color,'LineWidth',2) %繪製紅色響應曲線,併產生控制代碼
K=length(get(handles.listbox1,'Value')); %列表框裡,被"選中"的項數
for jj=1:K
    switch listindex(jj)
        case 1
            ;
        case 2
            k95=min(find(y>0.95));
            k952=[(k95-1),k95];
            t95=interp1(y(k952),t(k952),0.95); %線性插值法確定0.95線的時間
            line(zeta,t95,0.95,'marker','+','markeredgecolor','r','markersize',15);
        case 3  %畫出最大峰值點
            [ym,km]=max(y); %找最大峰值
            if km<Nt&&(ym-1)>0
                line(zeta,t(km),ym,'marker','*','markeredgecolor','k','markersize',15);
            end
        case 4 %畫出鎮定時刻點
            ii=max(find(abs(y-1)>0.05));
            if ii<Nt
                line(zeta,t(ii+1),y(ii+1),'Color','r','Marker','o','MarkerSize',7)
            end
    end
end
xlabel('{\zeta}')
ylabel('t')
zlabel('y')
alpha(0.7)
view(75,44)

程式碼下載
百度網盤分享密碼:wq5e

  1. matlab程式打包小技巧
    進入到matlab主介面,點選"應用程式",選擇"運用程式打包",選擇主檔案
    “exm080201.m”,然後點選"重新整理依存關係",填寫自己的GUI軟體說明
    資訊,最後點選"打包"即可生成新的軟體程式。在當前檔案路徑下會產生兩個檔案:huawei.mlappinstall,huawei.prj。效果圖見如下:
    程式打包
    雙擊huawei.prj即可安裝到本地matlab APPs中。