Chinaunix首页 | 论坛 | 博客
  • 博客访问: 683217
  • 博文数量: 90
  • 博客积分: 1631
  • 博客等级: 上尉
  • 技术积分: 1413
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-15 22:43
文章分类
文章存档

2017年(8)

2016年(9)

2015年(11)

2014年(10)

2013年(9)

2012年(9)

2010年(2)

2009年(10)

2008年(22)

我的朋友

分类: C/C++

2013-07-30 00:25:25

本文同步发于matlabsky论坛:

构建Simulink模型



使用一个简单的二阶“质量-弹簧-阻尼”系统作为示例。
4.gif 

1.建立二阶系统的数学模型如下(Simulink模型)

3.jpg 

但是为了我们方便调节每个环节的参数,我们把此界面改成如下:
1.jpg 
注意模型中出现的未知量c,k,m应该在workspace中先做定义。
将文件保存为mass_spring.mdl。

2.在GUI中做程序的图形界面

5.jpg 

3.编写按钮simulate_pushbutton的回调函数

点击(此处)折叠或打开

  1. function simulate_pushbutton_Callback(hObject, eventdata, handles)

  2. % hObject handle to simulate_pushbutton (see GCBO)

  3. % eventdata reserved - to be defined in a future version of MATLAB

  4. % handles structure with handles and user data (see GUIDATA)

  5. axes(handles.axes1)

  6. m1=str2num(get(handles.mass_editText,'string')); % 将axes1设置为当前坐标系,便于绘制仿真结果

  7. m=1/m1;

  8. c=str2num(get(handles.damping_editText,'string'));

  9. k=str2num(get(handles.spring_editText,'string'));



  10. set_param('mass_spring/Gain','Gain','m');

  11. set_param('mass_spring/Gain1','Gain','m');

  12. set_param('mass_spring/Gain2','Gain','m');

  13. set_param('mass_spring/Gain2','Gain','c');

  14. set_param('mass_spring/Gain2','Gain','k');



  15. % 配置Simulink仿真选项,使其使用当前工作空间

  16. % 设置Simulink从当前工作空间运行

  17. options = simset('SrcWorkspace','current');

  18. % 使用sim()函数对模型进行仿真

  19. % 第一参数是模型名称

  20. % 第二个参数是向量,指定仿真开始和结束时间

  21. % 如果设置为[],则使用在Simulink中设置的始末时间

  22. % 第三个参数设置Simulink仿真选项

  23. sim('mass_spring',[],options);

  24. % 绘制图形% 模型中有一个OUT模块,使用它我们可以设置输出变量为tout和yout

  25. % 然后就可以直接调用Simulink的获取仿真结果tout和yout进行绘图了

  26. plot(tout,yout)

  27. xlabel('Time')

  28. ylabel('Displacement')

  29. title('2nd Order Mass Spring System')

  30. grid on
 
注:看到网上很多网友在问为什么如果把上面调节参数的代码写成下面的样子不可以

点击(此处)折叠或打开

  1. set_param('mass_spring/Gain','m','m');
原因是mass_spring 系统的Gain环节就没有名为'm'的参数,我们可以试一下。

查看'mass_spring/Gain'的所有参数:

点击(此处)折叠或打开

  1. get_param('mass_spring/Gain','ObjectParameters')
显示的参数为:


点击(此处)折叠或打开

  1. ans =



  2.                                         Name: [1x1 struct]

  3.                                          Tag: [1x1 struct]

  4.                                  Description: [1x1 struct]

  5.                                         Type: [1x1 struct]

  6.                                       Parent: [1x1 struct]

  7.                                       Handle: [1x1 struct]

  8.                              HiliteAncestors: [1x1 struct]

  9.                              RequirementInfo: [1x1 struct]

  10.                                        Ports: [1x1 struct]

  11.                                     Position: [1x1 struct]

  12.                                  Orientation: [1x1 struct]

  13.                             PortRotationType: [1x1 struct]

  14.                              ForegroundColor: [1x1 struct]

  15.                              BackgroundColor: [1x1 struct]

  16.                                   DropShadow: [1x1 struct]

  17.                                       IOType: [1x1 struct]

  18.                                NamePlacement: [1x1 struct]

  19.                                     ShowName: [1x1 struct]

  20.                                     Priority: [1x1 struct]

  21.                       AttributesFormatString: [1x1 struct]

  22.                            InstantiateOnLoad: [1x1 struct]

  23.                          PolySpaceEndComment: [1x1 struct]

  24.                        PolySpaceStartComment: [1x1 struct]

  25.                                AncestorBlock: [1x1 struct]

  26.                               ReferenceBlock: [1x1 struct]

  27.                               LibraryVersion: [1x1 struct]

  28.                           UserDataPersistent: [1x1 struct]

  29.                                     UserData: [1x1 struct]

  30.                                      RTWdata: [1x1 struct]

  31.                                      HDLData: [1x1 struct]

  32.                                  Diagnostics: [1x1 struct]

  33.                             DialogParameters: [1x1 struct]

  34.                           CompiledSampleTime: [1x1 struct]

  35.                             InputSignalNames: [1x1 struct]

  36.                            OutputSignalNames: [1x1 struct]

  37.                          ModelParamTableInfo: [1x1 struct]

  38.                 StatePerturbationForJacobian: [1x1 struct]

  39.     SCDEnableBlockLinearizationSpecification: [1x1 struct]

  40.           SCDBlockLinearizationSpecification: [1x1 struct]

  41.                                      CopyFcn: [1x1 struct]

  42.                                    DeleteFcn: [1x1 struct]

  43.                                UndoDeleteFcn: [1x1 struct]

  44.                                      LoadFcn: [1x1 struct]

  45.                                ModelCloseFcn: [1x1 struct]

  46.                                   PreSaveFcn: [1x1 struct]

  47.                                  PostSaveFcn: [1x1 struct]

  48.                                      InitFcn: [1x1 struct]

  49.                                     StartFcn: [1x1 struct]

  50.                                     PauseFcn: [1x1 struct]

  51.                                  ContinueFcn: [1x1 struct]

  52.                                      StopFcn: [1x1 struct]

  53.                                NameChangeFcn: [1x1 struct]

  54.                                 ClipboardFcn: [1x1 struct]

  55.                                   DestroyFcn: [1x1 struct]

  56.                                   PreCopyFcn: [1x1 struct]

  57.                                      OpenFcn: [1x1 struct]

  58.                                     CloseFcn: [1x1 struct]

  59.                                 PreDeleteFcn: [1x1 struct]

  60.                               ParentCloseFcn: [1x1 struct]

  61.                                      MoveFcn: [1x1 struct]

  62.                                    BlockType: [1x1 struct]

  63.                             BlockDescription: [1x1 struct]

  64.                                   LinkStatus: [1x1 struct]

  65.                             StaticLinkStatus: [1x1 struct]

  66.                             PortConnectivity: [1x1 struct]

  67.                                  PortHandles: [1x1 struct]

  68.                                  LineHandles: [1x1 struct]

  69.                           CompiledPortWidths: [1x1 struct]

  70.                       CompiledPortDimensions: [1x1 struct]

  71.                        CompiledPortDataTypes: [1x1 struct]

  72.                   CompiledPortComplexSignals: [1x1 struct]

  73.                        CompiledPortFrameData: [1x1 struct]

  74.                    DataTypeOverride_Compiled: [1x1 struct]

  75.               MinMaxOverflowLogging_Compiled: [1x1 struct]

  76.                                 Capabilities: [1x1 struct]

  77.                              IOSignalStrings: [1x1 struct]

  78.                                RuntimeObject: [1x1 struct]

  79.                          ExtModeUploadOption: [1x1 struct]

  80.                      ExtModeLoggingSupported: [1x1 struct]

  81.                           ExtModeLoggingTrig: [1x1 struct]

  82.                                     FontName: [1x1 struct]

  83.                                     FontSize: [1x1 struct]

  84.                                   FontWeight: [1x1 struct]

  85.                                    FontAngle: [1x1 struct]

  86.                                     Selected: [1x1 struct]

  87.                                         Gain: [1x1 struct]

  88.                               Multiplication: [1x1 struct]

  89.                                     ParamMin: [1x1 struct]

  90.                                     ParamMax: [1x1 struct]

  91.                             ParamDataTypeStr: [1x1 struct]

  92.                                       OutMin: [1x1 struct]

  93.                                       OutMax: [1x1 struct]

  94.                               OutDataTypeStr: [1x1 struct]

  95.                                    LockScale: [1x1 struct]

  96.                                      RndMeth: [1x1 struct]

  97.                    SaturateOnIntegerOverflow: [1x1 struct]

  98.                                   SampleTime: [1x1 struct]

  99.                                     MaskType: [1x1 struct]

  100.                              MaskDescription: [1x1 struct]

  101.                                     MaskHelp: [1x1 struct]

  102.                             MaskPromptString: [1x1 struct]

  103.                              MaskStyleString: [1x1 struct]

  104.                                MaskVariables: [1x1 struct]

  105.                       MaskTunableValueString: [1x1 struct]

  106.                           MaskCallbackString: [1x1 struct]

  107.                             MaskEnableString: [1x1 struct]

  108.                         MaskVisibilityString: [1x1 struct]

  109.                            MaskToolTipString: [1x1 struct]

  110.                           MaskVarAliasString: [1x1 struct]

  111.                           MaskInitialization: [1x1 struct]

  112.                           MaskSelfModifiable: [1x1 struct]

  113.                                  MaskDisplay: [1x1 struct]

  114.                                MaskIconFrame: [1x1 struct]

  115.                               MaskIconOpaque: [1x1 struct]

  116.                               MaskIconRotate: [1x1 struct]

  117.                               MaskPortRotate: [1x1 struct]

  118.                                MaskIconUnits: [1x1 struct]

  119.                              MaskValueString: [1x1 struct]

  120.                     MaskRunInitForIconRedraw: [1x1 struct]

  121.                            MaskTabNameString: [1x1 struct]

  122.                                         Mask: [1x1 struct]

  123.                                MaskCallbacks: [1x1 struct]

  124.                                  MaskEnables: [1x1 struct]

  125.                                    MaskNames: [1x1 struct]

  126.                       MaskPropertyNameString: [1x1 struct]

  127.                                  MaskPrompts: [1x1 struct]

  128.                                   MaskStyles: [1x1 struct]

  129.                            MaskTunableValues: [1x1 struct]

  130.                                   MaskValues: [1x1 struct]

  131.                          MaskToolTipsDisplay: [1x1 struct]

  132.                             MaskVisibilities: [1x1 struct]

  133.                               MaskVarAliases: [1x1 struct]

  134.                              MaskWSVariables: [1x1 struct]

  135.                                 MaskTabNames: [1x1 struct

可以发现增益环节中有一个参数为Gain,这就是我们要传递数值的参数,并没有参数m或c或k。

4.GUI界面运行
2.jpg 
小结

set_param函数介绍
set_param函数的功能很多,这里只介绍如何用set_param函数设置Simulink仿真参数以及如何开始、暂停、终止仿真进程或者更新显示一个仿真模型。

调用格式为:
set_param(modname,property,value,…)
其中modname为设置的模型名,property为要设置的参数,value是设置值。这里设置的参数可以有很多种,而且和用simset设置的内容不尽相同,相关参数的设置可以参考有关资料。

阅读(15401) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~