在MATLAB中生成一个可以绘制二阶系统动态响应的GUI程序
时间: 2024-02-20 20:01:40 浏览: 23
以下是一个MATLAB的GUI程序,用于绘制二阶系统的动态响应:
```matlab
function varargout = SecondOrderSystemGUI(varargin)
% SECONDORDERSYSTEMGUI MATLAB code for SecondOrderSystemGUI.fig
% SECONDORDERSYSTEMGUI, by itself, creates a new SECONDORDERSYSTEMGUI or raises the existing
% singleton*.
%
% H = SECONDORDERSYSTEMGUI returns the handle to a new SECONDORDERSYSTEMGUI or the handle to
% the existing singleton*.
%
% SECONDORDERSYSTEMGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SECONDORDERSYSTEMGUI.M with the given input arguments.
%
% SECONDORDERSYSTEMGUI('Property','Value',...) creates a new SECONDORDERSYSTEMGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before SecondOrderSystemGUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to SecondOrderSystemGUI_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 SecondOrderSystemGUI
% Last Modified by GUIDE v2.5 12-Oct-2021 17:31:59
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @SecondOrderSystemGUI_OpeningFcn, ...
'gui_OutputFcn', @SecondOrderSystemGUI_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 SecondOrderSystemGUI is made visible.
function SecondOrderSystemGUI_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 SecondOrderSystemGUI (see VARARGIN)
% Choose default command line output for SecondOrderSystemGUI
handles.output = hObject;
% 初始化图形
axes(handles.plot1);
handles.plot1 = plot(0,0);
xlabel('时间(s)');
ylabel('响应');
title('系统响应');
grid on;
% 初始化控制参数
handles.wn = 1;
handles.zeta = 0.1;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes SecondOrderSystemGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = SecondOrderSystemGUI_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;
% --- 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)
% 获取滑动条的值
handles.wn = get(hObject, 'Value');
% 更新系统响应图
t = 0:0.01:10;
y = step(handles.wn^2*[1 2*handles.zeta handles.zeta^2], t);
axes(handles.plot1);
handles.plot1 = plot(t, y);
xlabel('时间(s)');
ylabel('响应');
title('系统响应');
grid on;
% Update handles structure
guidata(hObject, handles);
% --- 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
% 设置滑动条范围和初始值
set(hObject, 'Min', 0.1, 'Max', 10, 'Value', 1);
% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject handle to slider2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 获取滑动条的值
handles.zeta = get(hObject, 'Value');
% 更新系统响应图
t = 0:0.01:10;
y = step(handles.wn^2*[1 2*handles.zeta handles.zeta^2], t);
axes(handles.plot1);
handles.plot1 = plot(t, y);
xlabel('时间(s)');
ylabel('响应');
title('系统响应');
grid on;
% Update handles structure
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% 设置滑动条范围和初始值
set(hObject, 'Min', 0, 'Max', 1, 'Value', 0.1);
```
该GUI程序包含两个滑动条,分别用于控制系统的自然频率和阻尼比。当滑动条的值发生变化时,程序会重新计算系统的响应,并更新图形显示。用户可以通过调整滑动条的值来观察系统响应随自然频率和阻尼比的变化情况。