《自动控制原理》(胥布工版)习题3-4(使用GUI)

来源:互联网 发布:被收购的中国品牌 知乎 编辑:程序博客网 时间:2024/04/30 11:54

刚接触GUI,打算设计一个GUI,带有一个滑动条,可以滑动选择不同的τ值,然后GUI根据不同的τ值画出系统的传递函数。

第一步,要编写一个function,求出系统的传递函数,该function的输出参数为s和τ,输出参数为系统传递函数Ts。


exe_3_4.m文件代码如下:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Exercise 3-4 of the <Principle of Automatic Control>, SCUT, Page 93    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function Ts = exe_3_4( s, taud )tau = taud;                                                                 % the independent variable       num1 = [0, 0, 8];                                                           % numerator of Gp(s)den1 = [1, 1, 0];                                                           % denominator of Gp(s)Gps = tf( num1, den1 );                                                     % open-loop transfer functionnum2 = [0, 1, 1];                                                           % numerator of Gc(s)den2 = [0, 1, 0];                                                           % denominator of Gc(s)Gcs = tf( num2, den2 );                                                     % open-loop transfer functionnum3 = [0, tau, 0];                                                         % numerator of H(s)den3 = [0, 0, 1];                                                           % denominator of H(s)Hs = tf( num3, den3 );                                                      % open-loop transfer functionGs = feedback( Gps, Hs, -1 );                                               % system transfer function, negative feedbackGls = series( Gcs, Gs );                                                    % connect 2 models in seriesGfs = feedback( Gls, 1, -1 );                                               % system transfer function, negative feedbacksys_num = cell2mat(Gfs.num);                                                % get the sys_num from cell(Gfs.num)sys_den = cell2mat(Gfs.den);                                                % get the sys_den from cell(Gfs.den)num = sys_num(1,1)*s.^3 + sys_num(1,2)*s.^2 + sys_num(1,3)*s + sys_num(1,4);% the real system numerator with independent variableden = sys_den(1,1)*s.^3 + sys_den(1,2)*s.^2 + sys_den(1,3)*s + sys_den(1,4);% the real system denominator with independent variableTs = num./den;                                                              % the transfer function of the whole systemend

然后使用MATLAB的GUIDE设计GUI界面入下图所示:


        

然后打开滑动条的回调函数,增加以下代码:

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 sliderval = get( hObject, 'value' );set( handles.text1, 'String', num2str(val,'%5.2f') );s = 0:0.1:100;                                                              Ts = exe_3_4( s, val );axes(handles.axes1);plot( s, Ts );

最终的效果如下图所示:


0 0
原创粉丝点击