simulink教程前言 (3)
MATLAB-SIMULINK讲解完整版

第3章 SIMULINK应用基础
(1) 文件管理类:包括 4 个按键,分别是按键 、按键 和按键 。
(2) 对象管理类:包括 3 个按键,分别是按键 和按键 。
、按键 、按键
(3) 命令管理类:包括 2 个按键,分别是按键 和按键 。
(4) 仿真控制类:包括 6 个按键、1 个文本框、1 个列表
框,分别是按键 、按键 如图3-6所示,在模型中加入注释文字,使模型更具可 读性。
图3-6 添加注释文字示例 (a) 未加注释文字;(b) 加入注释文字
第3章 SIMULINK应用基础
3.2.3 子系统的建立与封装 1. 子系统的建立 一般而言,电力系统仿真模型都比较复杂,规模很大,
包含了数量可观的各种模块。如果这些模块都直接显示在 SIMULINK仿真平台窗口中,将显得拥挤、杂乱,不利于用 户建模和分析。可以把实现同一种功能或几种功能的多个模 块组合成一个子系统,从而简化模型,其效果如同其它高级 语言中的子程序和函数功能。
、列 表框
、按键 、按键 和按键 。 (5) 窗口切换类:包括 6 个按键,分别是按键 、按键 、按键 、按键 和按键 。
、按键
工具栏中各个工具图标及其功能说明见附录 B。
第3章 SIMULINK应用基础
3.2 SIMULINK的基本操作
3.2.1 模块及信号线的基本操作 1. 模块的基本操作 模块是系统模型中最基本的元素,不同模块代表了不同
增强模型的可读性
签内容,在标签编辑框外的窗口中单击鼠标退出
第3章 SIMULINK应用基础
3.2.2 系统模型的基本操作 除了熟悉模块和信号线的基本操作方法,用户还需熟悉
SIMULINK系统模型本身的基本操作,包括模型文件的创建、 打开、保存以及模型的注释等。
Simulink入门指南

3 Creating a Simulink Model•“Overview of a Simple Model”on page3-2•“Creating the Simple Model”on page3-3•“Connecting Blocks in the Simple Model”on page3-9•“Simulating the Simple Model”on page3-143Creating a Simulink®ModelOverview of a Simple ModelYou can use Simulink software to model dynamic systems and simulate thebehavior of the models.The basic techniques you use to create a simple modelare the same techniques you will use for more complex models.To create this simple model,you need four blocks:•Sine Wave—Generates an input signal for the model.•Integrator—Processes the input signal.•Mux—Multiplexes the input signal and processed signal into a singlesignal.•Scope—Visualizes the signals in the model.After connecting the blocks,they model a system that integrates a sine wavesignal and displays the result along with the original signal.You can build this simple model yourself,starting with“Creating a NewModel”on page3-3.3-2Creating the Simple ModelCreating the Simple ModelIn this section...“Creating a New Model”on page3-3“Adding Blocks to a Model”on page3-4“Moving Blocks in the Model”on page3-8Creating a New ModelBefore creating a model,you need to start Simulink,and then open an emptymodel window.1If Simulink is not running,in the MATLAB Command Window,entersimulinkThe Simulink Library Browser opens.2From the Simulink Library Browser menu,select File>New>Model.A Simulink editor window opens with an empty model in the right pane.3-33Creating a Simulink®Model3Select File>Save as.The Save As dialog box opens.4In the File name box,enter a name for your model,and then click Save.For example,enter simple_model.The software saves your model with the filename simple_model.mdl.Adding Blocks to a ModelTo create a model,you begin by copying blocks from the Simulink LibraryBrowser to the Simulink editor window.For a description of the blocks in thisexample,see“Overview of a Simple Model”on page3-2.1In the Simulink Library Browser,select the Sources library.The Simulink Library Browser displays blocks from the Sources library inthe right pane.3-4Creating the Simple Model3-53Creating a Simulink®Model2Select the Sine Wave block,and then drag it to the editor window.A copy of the Sine Wave block appears in your model.3Add the following blocks to your model in the same way you added theSign Wave block.Library BlockSinks ScopeContinuous IntegratorSignal Routing MuxYour model now has the blocks you need for the simple model.3-6Creating the Simple Model3-73Creating a Simulink®ModelMoving Blocks in the ModelBefore you connect the blocks in your model,you should arrange themlogically to make the signal connections as straightforward as possible.Tomove a block in a model,you can either•Click and drag the block•Select the block,and then press the arrow keys on the keyboard1Move the Scope block after the Mux block output.2Move the Sine Wave and Integrator blocks before the Mux block Inputs.Your model should look similar to the following figure.Your next task is to connect the blocks together with signal lines.See“Connecting Blocks in the Simple Model”on page3-9.3-8Connecting Blocks in the Simple ModelConnecting Blocks in the Simple ModelIn this section...“Block Connections in a Model”on page3-9“Drawing Lines Between Blocks”on page3-9“Drawing a Branch Line”on page3-12Block Connections in a ModelAfter you add blocks to your model,you need to connect them.The connectinglines represent the signals within a model.Most blocks have angle brackets on one or both sides.These angle bracketsrepresent input and output ports:•The>symbol pointing into a block is an input port.•The>symbol pointing out of a block is an output port.Input port Output portDrawing Lines Between BlocksConnect the blocks by drawing lines between output ports and input ports.For how to add blocks to the model in this example,see“Adding Blocks toa Model”on page3-4.1Position your mouse pointer over the output port on the right side of theSine Wave block.3-93Creating a Simulink®Model2Drag a line from the output port to the top input port of the Mux block.While holding the mouse button down,the connecting line is shown as alight colored arrow.3Release the mouse button over the output port.Simulink connects the blocks with an arrow indicating the direction ofsignal flow.4Drag a line from the output port of the Integrator block to the bottom inputport on the Mux block.The Integrator block connects to the Mux block with a signal line.3-10Connecting Blocks in the Simple Model5Select the Mux block,hold down the Shift key,and then select the Scopeblock.A line is drawn between the blocks to connect them.Note The Shift+click shortcut is useful when you are connecting widelyseparated blocks,or when working with complex models.Your model should now look similar to the following figure.3-113Creating a Simulink®ModelDrawing a Branch LineThe simple model is almost complete,but one connection is missing.To finishthe model,you need to connect the Sine Wave block to the Integrator block.This final connection is somewhat different from the other three connections,which all connect output ports to input ports.Because the output port of theSine Wave block already has a connection,you must connect this existing lineto the input port of the Integrator block.The new line,called a branch line,carries the same signal that passes from the Sine Wave block to the Mux block.1Position the mouse pointer on the line between the Sine Wave and theMux block.2Hold down the Ctrl key,and then drag a line to the input port of theIntegrator block input port.This step adds a connection to the existing line and draws a line betweenthe connection and the input port of the Integrator block.3-12Connecting Blocks in the Simple Model3From the File menu,click Save.Your model is now complete.It should look similar to the following figure.After your model is complete,you can simulate the model.See“Simulatingthe Simple Model”on page3-14.3-133Creating a Simulink®ModelSimulating the Simple ModelIn this section...“Setting Simulation Options”on page3-14“Running a Simulation and Observing Results”on page3-15Setting Simulation OptionsBefore you simulate a model,you have to set simulation options.You specifyoptions,such as the stop time and solver,using the Model ConfigurationParameters dialog box.For how to build the model in this example,see“Creating the Simple Model”on page3-3.1In the Simulink editor window,select Simulation>ModelConfiguration Parameters.The Configuration Parameters dialog boxopens to the Solver pane.2In the Stop time field,enter20,and in the Max step size field,enter0.2.3Click OK.The software updates the parameter values with your changes and closesthe Configuration Parameters dialog box.For more information about Simulink configuration parameters,see“Configuration Parameters Dialog Box”.3-14Simulating the Simple Model Running a Simulation and Observing ResultsAfter entering your configuration parameter changes,you are ready tosimulate the simple model and visualize the simulation results.1In the Simulink editor window and from the menu,selectSimulation>Start.The simulation runs,and then stops when it reaches the stop time specifiedin the Model Configuration Parameters dialog box.Tip Alternatively,you can control a simulation by clicking the Startsimulation button and Stop simulation button on the editorwindow toolbar.2Double-click the Scope block.The Scope window opens and displays the simulation results.The plotshows a sine wave signal with the resulting cosine wave signal from theIntegrator block.3-153Creating a Simulink®Model3From the toolbar,click the Parameters button,and then the Graphicstab.The Scope Parameters dialog opens with figure editing commands.4Make changes to the appearance of the figure.For example,select whitefor the Figure and Axes background color,and change the signal line colorsto blue and green.Click the Apply button to see your changes.3-16Simulating the Simple Model5Select File>Close.The Simulink editor window closes with changes to your model and the configuration parameters.3-173Creating a Simulink®Model 3-184 Modeling a Dynamic Control System•“Understanding a Demo Model”on page4-2•“Simulating the Demo Model”on page4-11•“Moving Data Between MATLAB and the Demo Model”on page4-194Modeling a Dynamic Control SystemUnderstanding a Demo ModelIn this section...“Overview of the Demo Model”on page4-2“Opening the Demo Model”on page4-3“Anatomy of the Demo Model”on page4-4“Subsystems in the Demo Model”on page4-5“Subsystems and Masks”on page4-9“Creating a Subsystem”on page4-9“Creating a Subsystem Mask”on page4-10Overview of the Demo ModelThis demo model illustrates how you can use Simulink software to modela dynamic control system.The model defines a heating system and thethermodynamics of a house.It included the outdoor environment,the thermalcharacteristics of a house,and the house heating system.Use this model to explore common Simulink modeling tasks,such as•Grouping multiple blocks into a single subsystem block to simplify a blockdiagram.See“Subsystems in the Demo Model”on page4-5•Customizing the appearance of blocks using the masking feature.See“Creating a Subsystem Mask”on page4-10•Simulating a model and observing the results using a Scope block.See“Running the Simulation”on page4-11•Changing the input parameters of the model to investigate how the systemresponds.See“Changing the Thermostat Setting”on page4-12.•Importing data from the MATLAB workspace into a model beforesimulation.See“Importing Data from the MATLAB Workspace”on page4-19.•Exporting simulation data from the model back to the MATLAB workspace.See“Exporting Simulation Data to the MATLAB Workspace”on page4-23. 4-2Understanding a Demo Model Opening the Demo ModelThe demo model for this example is called sldemo_househeat.It models the heating system and thermodynamics of a house.1Start MATLAB,and then In the MATLAB Command Window,enter sldemo_househeatThe Simulink editor opens with the demo model.4Modeling a Dynamic Control SystemAnatomy of the Demo ModelThe demo model defines the dynamics of the outdoor environment,thethermal characteristics of the house,and the house heating system.It allowsyou to simulate how the thermostat setting and outdoor environment affectthe indoor temperature and cumulative heating costs.The demo model includes many of the same blocks you used to create thesimple model in Chapter3,“Creating a Simulink Model”.These include:•A Scope block(labeled PlotResults)on the far right,displays thesimulation results.•A Mux block at the bottom right,combines the indoor and outdoortemperature signals for the Scope.•A Sine Wave block(labeled Daily Temp Variation)at the bottom left,provides one of three data sources for the model.In the demo model,the thermostat is set to70degrees Fahrenheit.Thesystem models fluctuations in outdoor temperature by applying a sine wavewith amplitude of15degrees to a base temperature of50degrees.The three data inputs(sources)are provided by two Constant blocks(labeledSet Point and Avg Outdoor Temp),and the Sine Wave block(labeled DailyTemp Variation).The Scope block labeled PlotResults is the one output(sink).Understanding a Demo Model Subsystems in the Demo ModelThe sldemo_househeat demo model uses subsystems to simplify theappearance of the block diagram,create reusable components,and customizethe appearance of blocks.A subsystem is a hierarchical grouping of blocks encapsulated by a single Subsystem block.The demo model uses the following subsystems:Thermostat,Heater,House, Fahrenheit to Celsius,and Celsius to Fahrenheit1In the MATLAB Command Window,entersldemo_househeatThe demo model opens in the Simulink editor window.4Modeling a Dynamic Control System2Subsystems can be complex and contain many blocks that might otherwiseclutter a diagram.For example,double-click the House subsystem block toopen it.Contents of House subsystemThe subsystem receives heat flow and external temperature as inputs,which it uses to compute the current room temperature.You could leaveeach of these blocks in the main model window,but combining them as asubsystem helps simplify the block diagram.Understanding a Demo Model3A subsystems can also be simple and contain only a few blocks.Forexample,double-click the Thermostat subsystem block to open it.Contents of Thermostat subsystemThis subsystem models the operation of a thermostat,determining whenthe heating system is on or off.It contains only one Relay block,butlogically represents the thermostat in the block diagram..4Modeling a Dynamic Control SystemUnderstanding a Demo Model Subsystems and MasksSubsystems allows you to group related blocks into one block.They are also reusable,enabling you to implement an algorithm once and use it multiple times.For example,the model contains two instances of identical subsystems named Fahrenheit to Celsius.These subsystems convert the inside and outside temperatures from degrees Fahrenheit to degrees CelsiusYou can customize the appearance of a subsystem by using a process knownas masking.Masking a subsystem allows you to specify a unique icon anddialog box for the Subsystem block.For example,the House and Thermostat subsystems display custom icons that depict physical objects,while the conversion subsystems display custom dialog boxes when you double-clickthem.1Double-click the Fahrenheit to Celsius block.The custom dialog box for the F2C block opens.2To view the underlying blocks in the conversion subsystem,right-click thesubsystem block,point to Mask,and then select Look Under Mask.The editor displays the blocks behind the mask.Creating a SubsystemTo create a subsystem:1In the demo model window,select the set point and Fahrenheit to Celsiusblocks.4Modeling a Dynamic Control System2From the menu,select Diagram>Subsystem&Modeling Reference>Create Subsystem from Selection.The blocks are combined into one subsystem block.For more information about working with subsystems,see“CreatingSubsystems”in the Simulink User’s Guide.Creating a Subsystem MaskTo mask a subsystem:1In the demo model window,right-click the new subsystem block,and thenselect Mask>Add/Edit Mask.The Mask Editor dialog box opens.2From the Command list,select disp(show text in center of block).The dialog box displays the syntax for this command below the list.3In the Icon Drawing commands field,enter disp('SelectTemperature').4Click OK.The software masks the subsystem block with the text you entered.For more information about masking subsystems,see“Working with BlockMasks”in the Simulink User’s Guide.Simulating the Demo ModelSimulating the Demo ModelIn this section...“Running the Simulation”on page4-11“Changing the Thermostat Setting”on page4-12“Changing the Average outdoor Temperature”on page4-14“Changing the Daily Temperature Variation”on page4-16Running the SimulationSimulating the model allows you to observe how the thermostat setting andoutdoor environment affect the indoor temperature and the cumulativeheating cost.1In the demo model window,double-click the Scope block namedPlotResults.The software opens a Scope window that contains two axes with the labelsHeatCost and Temperatures.2From the menu,select Simulation>Start.The software simulates the model.As the simulation runs,the cumulativeheating cost appears on the HeatCost graph at the top of the Scope window.The indoor and outdoor temperatures appear on the Temperatures graphas yellow(top)and magenta(bottom)signals,respectively.4Modeling a Dynamic Control SystemChanging the Thermostat SettingOne of the most powerful benefits of modeling a system with Simulink isthe ability to interactively define the system inputs and observe changes inthe behavior of your model.This allows you to quickly evaluate your modeland validate the simulation results.Change the thermostat setting to68degrees Fahrenheit and observe howthe model responds.1In the Simulink editor window,double-click the Set Point block.TheSource Block Parameters dialog box opens.2In the Constant value field,enter68.Simulating the Demo Model3Click OK.The software applies your changes.4To rerun the simulation,select Simulation>Start.The software simulates the model.In the Scope window,notice that a lower thermostat setting reduces the cumulative heating cost.4Modeling a Dynamic Control SystemChanging the Average outdoor TemperatureChange the average outdoor temperature to45degrees Fahrenheit andobserve how the model responds.1In the Simulink editor window,double-click the Avg Outdoor Temp block.The Source Block Parameters dialog box opens.2In the Constant value field,enter45.Simulating the Demo Model3Click OK.The software applies your changes and closes the dialog box.4To rerun the simulation,select Simulation>Start.The software simulates the model dynamics.In the Scope window,noticethat a colder outdoor temperature increases the cumulative heating cost.4Modeling a Dynamic Control SystemChanging the Daily Temperature VariationDecrease the temperature variation to see how the model responds.1In the Simulink editor window,double-click the Daily Temp Variationblock.The Source Block Parameters dialog box opens.2In the Amplitude field,enter5.3Click OK.The software applies your changes and closes the dialog box.4To rerun the simulation,select Simulation>Start.The software simulates the model.In the Scope window,notice that amore stable outdoor temperature alters the frequency with which theheater operates.Simulating the Demo Model4Modeling a Dynamic Control SystemMoving Data Between MATLAB and the Demo ModelMoving Data Between MATLAB and the Demo ModelIn this section...“Importing Data from the MATLAB Workspace”on page4-19“Exporting Simulation Data to the MATLAB Workspace”on page4-23Importing Data from the MATLAB WorkspaceSimulink also allows you to import data from the MATLAB workspace to themodel input ports.This allows you to import actual physical data into yourmodel.For information about other data import capabilities,see“Importingand Exporting Simulation Data”in the Simulink User’s Guide.Note In this example,you will create a vector of temperature data inMATLAB,and use that data as an input to the Simulink model.To import data from the MATLAB workspace:1In the MATLAB Command Window,create time and temperature data byentering the following commands:x=(0:0.01:4*pi)';y=32+(5*sin(x));z=linspace(0,48,1257)';2In the Simulink editor window,select the Avg Outdoor Temp block,andthen press the Delete key to delete it.3Delete the following items from the model in the same way:•Daily Temp Variation block•Two input signal lines to the Sum block•Sum block4Modeling a Dynamic Control SystemThe model should now look similar to the following figure.Notice that theoutput signal from the Sum block changes to a red,dotted line,indicatingthat it is not connected to a block.4In the demo model window,select View>Library Browser.The Simulink Library Browser window opens.5In the Simulink Library Browser,select the Sources library.6From the Sources library right pane,select the In1block,and then drag itto the model window.Moving Data Between MATLAB and the Demo ModelAn In1block appears in the model window.7Connect the dotted line(originally connected to the Sum block)to the In1 block.8In the Simulink editor window,select Simulation>Configuration Parameters.The Configuration Parameters dialog box opens.9In the menu on the left side of the dialog,select Data Import/Export.The Data Import/Export pane opens.4Modeling a Dynamic Control System10Select the Input check box.11In the Input field,enter[z,y].12Click OK.The software applies your changes and closes the dialog box.13To rerun the simulation,select Simulation>Start.The software simulates the model.In the Scope window,notice that themodel ran using the imported data,showing colder temperatures andhigher heat use.Exporting Simulation Data to the MATLAB Workspace Once you have completed a model,you may want to export your simulation results to MATLAB workspace for further data analysis or visualization.For information about additional data export capabilities,see“Exporting Simulation Data”.To export the HeatCost data from the model to the MATLAB workspace:1In the Simulink Library Browser window,select the Sinks library.2From the Sinks library,select the Out1block,and then drag it to the top right of the demo model window.An Out1block appears in the model window.3Draw a branch line from the HeatCost signal line to the Out1block.For more information,see“Drawing a Branch Line”on page3-12.4Select Simulation>Configuration Parameters.The Configuration Parameters dialog box opens.5From the menu on the left side of the dialog box,select DataImport/Export.The Data Import/Export pane opens.6Select the Time and Output check boxes.7Click OK.The software applies your changes and closes the dialog box.8To rerun the simulation,select Simulation>Start.The software simulates the model and saves the time and HeatCost data to the MATLAB workspace.Notice that the tout and yout variables nowappear in the MATLAB workspace.。
simulink教程

2.2.3 指定方块பைடு நூலகம்颜色
Simulink 允许用户在方块图中指定任何模块或标注的前景色和背景色,也可以指定方
如果要 Simulink 自动连接模块,先用鼠标选择模块,然后按下 Ctrl 键,再用鼠标单击 目标模块,Simulink 会自动把源模块的输出端口与目标模块的输入端口相连,如果需要, Simulink 还会绕过某些干扰连接的模块,如图 2-1 所示。
图 2-1 如果连接两个模块时,两个模块上有多个输出端口和输入端口,Simulink 会尽可能地 连接这些端口,如图 2-2 所示。
设置模块参数
如何在模块参数对话框和 MATLAB 工作区内设
置模块参数
标注模型图
如何在模型图中利用 TeX 命令编辑带有数学符
号、希腊字母和其字符的标注
模块属性对话框
如何设置模块属性,包括模块优先级和编辑指定
模块标注
模块优先级
如何控制和显示模型中模块的优先级
查表编辑器
如何利用查表编辑器编辑和显示查表模块和自定
图 2-4
2.1.3 手动连接模块
如果要手动连接模块,先把鼠标光标放置在源模块的输出端口,不必精确地定位光标 位置,光标的形状会改变为十字型,然后按下鼠标按钮,拖动光标指针到目标模块的输入 端口,如图 2-5 所示。当释放鼠标时,Simulink 会用带键头的连线替代端口符号,箭头的 方向表示了信号流的方向。
图 2-2 如果要把一组源模块与一个目标模块连接,可以先选择这组源模块,然后按下 Ctrl 键, 再用鼠标单击目标模块,如图 2-3 所示。
第三章 Simulink基础

同一模型中复制模块 (1)按下ctrl键,选中要复制的模块 (2)拖到喜欢的位置 删除模块 选中后,点击右健中的clear 或按键盘的delete 断开模块连接 按下shitf键,选中模块并从模型中的原始位置 拖支模块即可。 改变模块的方向 Format菜单中选择flip block命令旋转180度,或 者rotate block顺时针旋转90度。
数学运算模块(math)
求绝对值或求模 建立逻辑真值表 求复数的幅值与相角 求复数的实部和虚部 求点乘(内积) 增益(对输入信号乘上一个常数) 逻辑操作符 由幅值与相角求复数 数学运算函数
求极大与极小值 对输入信号求积或商 比较操作符 取整操作 取输入的正负符号 以滑动形式改变增益 对输入信号求代数和 三角函数
输入源模块(source)
带限白噪声 产生一个频率不断变化的正弦波 显示当前仿真时间 生成一个常值 在规定的采样间隔显示当前仿真时间 从工作空间中定义的矩阵读取数据 从文件中读取数据 接地线模块 整个系统的输入子端
固定时间间隔的脉冲发生器 斜坡信号 正态分布的随机信号 产生规律重复的线性信号 产生各种不同的波型 产生一个正弦波 产生一个阶跃函数 均匀分布随机数
4、选择对象(包括模块和连线) 选择一个对象 单击鼠标左键即可选中 选择一个以上对象
逐个选择法:按住shift键 用方框选择
选择整个模型: Ctrl+A 或edit菜单下的select all
5、两个窗口之间模块的复制和移动 用鼠标复制模块 (1)打开相应的库或模型窗口 (2)把要复制的模块拖到自己的目标窗口 中。 用菜单中的命令复制、粘贴模块 模块的命名 选中模块的名称,然后输入新的字符 在同一模块中移动模块 (1)选中需要移动的模块和连线 (2)拖动到适当的地方,然后释放鼠标
Simulink基础学习教程

Simulink基础学习Simulink是面向框图的仿真软件。
7.1一个Simulink的简单程序【例7.1】创建一个正弦信号的仿真模型。
步骤如下:(1) 在MATLAB的命令窗口运行simulink命令,或单击工具栏中的图标,就可以打开Simulink模块库浏览器(Simulink Library Browser) 窗口,如图7.1所示。
(2)单击工具栏上的图标或选择菜单“File ”——“New ”——“Model ”,新建一个名为“untitled ”的空白模型窗口。
(3) 在上图的右侧子模块窗口中,单击“Source ”子模块库前的“+”(或双击Source),或者直接在左侧模块和工具箱栏单击Simulink 下的Source 子模块库,便可看到各种输入源模块。
(4) 用鼠标单击所需要的输入信号源模块“Sine Wave ”(正弦信号),将其拖放到的空白模型窗口“untitled ”,则“Sine Wave ”模块就被添加到untitled 窗口;也可以用鼠标选中“Sine Wave ”模块,单击鼠标右键,在快捷菜单中选择“add to 'untitled'”命令,就可以将“Sine Wave ”模块添加到untitled 窗口,如图7.2所示。
图7.1 Simulink 界面(5) 用同样的方法打开接收模块库“Sinks”,选择其中的“Scope”模块(示波器)拖放到“untitled”窗口中。
(6) 在“untitled”窗口中,用鼠标指向“Sine Wave”右侧的输出端,当光标变为十字符时,按住鼠标拖向“Scope”模块的输入端,松开鼠标按键,就完成了两个模块间的信号线连接,一个简单模型已经建成。
如图7.3所示。
(7) 开始仿真,单击“untitled”模型窗口中“开始仿真”图标,或者选择菜单“Simulink”——“Start”,则仿真开始。
双击“Scope”模块出现示波器显示屏,可以看到黄色的正弦波形。
Simulink3运行仿真Simulink教学课件

SIMULINK 3SIMULINK 3运行仿真运行仿真介绍两种Simulink运行仿真的方法3.1使用窗口运行仿真3.2 使用MATLAB 命令运行仿真本章内容和学习目的掌握以上两种运行仿真的方法3.1 使用窗口运行仿真1. 设置仿真参数优点人机交互性强不必记住繁琐的命令语句即可进行操作。
使用窗口运行仿真主要可以完成以下一些操作。
3. 启动仿真4. 停止仿真5. 中断仿真6. 仿真诊断2. 应用仿真参数仿真参数和算法选择的设置仿真参数和算法设置后使之生效选择命令运行仿真选择命令停止仿真可以在中断点继续启动仿真而停止仿真则不能在仿真中若出现错误Simulink将会终止仿真并在仿真诊断对话框中显示错误信息1. 设置仿真参数选择菜单选项【SimulationParameters】可以对仿真参数及算法进行设置共有五个∠羁▉6?解法设置Solver??工作间I/OWorkspace I/O??诊断页Diagnostics??高级设置Advanced??实时工具对话框Real-Time Workshop??解法设置Solver讲??工作间I/OWorkspace I/O讲??诊断页Diagnostics不讲自学??高级设置Advanced不讲自学??实时工具对话框Real-Time Workshop不讲自学设置起始和终止时间选择积分分解法指定求解参数和选择输出选项管理MATLAB工作间的输入输出项选择在仿真中警告信息的等级对仿真的一些高级配置进行设置对实时工具中若干参数进行设置。
若没有安装实时工具不出现此框。
1解法设置页当选中菜单选项【SimulationParameters】后出现参数及算法等设置页。
再点击【Solver】则出现解法设置页。
解法设置页包括三项内容设置仿真的启动时间和终止时间选择算法并指定参数选择输出项仿真时间仿真解法各种解法说明见下页默认解法ode45变步长解法ode45ode23ode113ode15discrete定步长解法ode5ode4ode3ode2ode1discrete最大步长初始步长输出选项用户用来控制仿真输出个数的对话框共有三个菜单选项定义输出产生附加输出产生指定输出。
simulink教程

第七章
Simulink建模和仿真
7.1 Simulink的概述和基本操作
近几年来,在学术界和工业领域,Simulink已经成为动态系 统建模和仿真领域中应用最为广泛的软件之一。Simulink可 以很方便地创建和维护一个完整地模块,评估不同地算法和 结构,并验证系统的性能。由于Simulink是采用模块组合方 式来建模,从而可以使得用户能够快速、准确地创建动态系 统的计算机仿真模型,特别是对复杂的不确定非线性系统, 更为方便。 Simulink模型可以用来模拟线性和非线性、连续和离散或者 两者的混合系统,也就是说它可以用来模拟几乎所有可能遇 到动态系统。另外Simulink还提供一套图形动画的处理方法, 使用户可以方便的观察到仿真的整个过程。 Simulink没有单独的语言,但是它提供了S函数规则。所谓 的S函数可以是一个M函数文件、FORTRAN程序、C或C++语言 程序等,通过特殊的语法规则使之能够被Simulink模型或模 块调用。S函数使Simulink更加充实、完备,具有更强的处 理能力。
第七章
Simulink建模和仿真
7.2
基本模块
由于大多数物理系统都可以用微分方程组和代数方程组来描 述,Simulink也采用的是本教材第四章介绍的面向结构图的 数字仿真原理。但其功能块的类型、数值解法、功能快的描 述、以及建模方式和方法远远超出CSS仿真程序包,其界面 也更加友好。 Simulink 4把功能块分成9类,分别放臵在9个库中,如图 7.2.1所示:源模块库(Sources)、输出显示库(Sinks)、离 散模块库(Discrete)、连续模块库(Continuous)、非线性模 块库(Nonlinear)、数学函数库(Math)、通用函数及列表库 (Functions and Tables)、信号处理及系统类模块库 (Signal and Systems)和子系统模块库(Subsystems)。 表7.2.1到表7.2.8列出了个库包含的主要模块及简单说明。 图7.2.2到图7.2.10列出了各库包含的主要模块种类图形。
Simulink入门教程

Simulink 快速入门要构建模型, 可以使用Simulink® Editor 和Library Browser。
启动 MATLAB 软件启动 Simulink 之前, 请先启动MATLAB®。
请参阅启动和关闭(MATLAB)。
配置 MATLAB 以启动 Simulink您在 MATLAB 会话中打开第一个模型时需要的时间比打开后续模型长, 因为默认情况下, MATLAB 会在打开第一个模型时启动 Simulink。
这种即时启动Simulink 的方法可以缩短 MATLAB 启动时间, 避免不必要的系统内存占用。
•要快速打开第一个模型, 您可以配置 MATLAB, 在它启动时同时启动Simulink。
要启动 Simulink 而不打开模型或 Library Browser, 请使用start_simulink。
•根据 MATLAB 的启动方式, 恰当使用此命令:•在 MATLAB startup.m文件中在操作系统命令行中, 使用matlab 命令和-r 开关例如, 要在运行Microsoft®Windows®操作系统的计算机上启动 MATLAB 时启动 Simulink, 请创建具有以下目标的桌面快捷方式:matlabroot\bin\win64\matlab.exe -r start_simulink在 Macintosh 和Linux®计算机上, 可在启动 MATLAB 时使用以下命令启动Simulink 软件:matlab -r start_simulink打开 Simulink Editor•要打开 Simulink Editor, 您可以:•创建一个模型。
在 MATLAB 的Home 选项卡上, 点击Simulink 并选择一个模型模板。
或者, 如果您已经打开了 Library Browser, 请点击New Model 按钮/。
有关创建模型的其他方法, 请参阅创建模型。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
号组进行仿真等
38
第 3 章 Snk 信号的概念,并给出范例说明 Simulink 中信号的种类,以及如何指 定、显示和验证信号连接的有效性。
3.1.1 信号属性及分类
信号是模型仿真时出现在 Simulink 模块输出端的数值流。理解模型图中连接模块之间 沿着示意线传输的信号是非常有用的,但需要注意的是,Simulink 模型中用来连接模块的 线只具有逻辑意义,而没有任何物理含义。因此,把 Simulink 中的信号类比成电子信号也 是不完全正确的。例如,电子信号在电缆中传输时是需要时间的,相比之下,Simulink 模 块的输出是同时出现在它所连接模块的输入端的。
信号维数 Simulink 模块可以输出一维或二维信号。一维(1-D)信号是由一维数组输出流组成,这 个数组流在每个仿真时间步上以一个数组(向量)的频率进行输出;二维(2-D)信号是由一个 二维数组流组成,这个二维数组在每个模块采样时间内以一个二维数组(矩阵)的频率产生。 Simulink 的用户接口和文档通常把一维信号描述为向量(vectors),把二维信号描述为 矩阵(matrices),而一元素数组常常是指标量(scalar),行向量(row vector)是只有一行的二维 数组,列向量(column vector)是只有一列的二维数组。 在仿真过程中,对 Simulink 中的各个模块来说,每个模块可接受或可输出的信号维数 是各不相同的,有些模块可以接受或输出任意维数的信号,而有些模块则只能接受或输出 标量信号或向量信号。本章 3.1.2 节中的内容“确定输出信号的维数”讨论了对于可输出 非标量信号的模块,如何确定这些模块的输出信号维数。 信号数据类型 数据类型是指用来在 Simulink 内部表示信号值的格式,缺省时,Simulink 信号的数据 类型是 double(双精度),但是,用户也可以创建其它数据类型的信号。Simulink 支持与 MATLAB 相同的数据类型。 复信号 缺省时,Simulink 的信号值是实数,但 Simulink 模型也可以创建和管理复信号,信号 值为复数的信号称为复信号。用户可以用下面的方法把复信号引入到 Simulink 模型中: -- 从 MATLAB 工作区将复值信号数据通过模型最顶层的输入端口(即 Inport 端口)装载
42
第 3 章 Simulink 信号操作
M×N 矩阵 M×N 矩阵
off
M×N 矩阵
on
M×N 矩阵
Simulink 中 Source 模块库中的模块允许用户指定这些模块输出的信号的维数,因此可 以利用这些模块将不同维数的信号引入到用户模型中。
确定非 Sources 模块的输出维数 如果一个模块有输入,那么该模块的输出在经过标量扩展之后与其输入有相同的维数 (所有的输入也必须有相同的维数)。
图 3-1 例如,以图 3-1 所示模型为例。模型中用 Bus Creator 模块和 Bus Selector 模块传递纯 虚信号,这两个模块均是纯虚模块,驱动 Gain 模块 G1 和 G2 的信号是分别对应于 s2 和 s1 的纯虚信号,Bus Selector 模块对话框可以设置从输入总线(这里是标签为 s3 的信号)中传递 过来的信号,用户可以选择信号排列的顺序。当更新或仿真模型时,Simulink 会自动确定 模型中的信号。首先选中模型中标识为 s3 的纯虚信号线,然后选择 Edit 菜单下的 Signal properties 命令,打开信号属性对话框,将对话框中的 Show Propagated Signals 选项设置 为 on,此时模型中的纯虚信号标签显示了由纯虚信号表示的非纯虚信号,如图 3-2 所示。
40
第 3 章 Simulink 信号操作
去,直到 Simulink 确定出纯虚信号的所有非纯虚分量。
控制信号
控制信号(Control Signal)也是 Simulink 中的一种信号,当仿真执行某一模块时,另一 模块利用控制信号对这个模块进行初始化,例如,函数调用或动作子系统模块。当用户更 新系统方块图的仿真条件或者开始仿真时,Simulink 会使用点划线重新绘制用来表示方块 图控制信号的线,如图 3-3 所示。
维数和信号属性
信号属性
如何利用信号属性对话框设置信号属性,包括如
何显示纯虚信号
检验信号连接
如何检验模型中的模块是否能接受用户所创建的
信号,也就是判断信号是否被正确传递
显示信号
如何在模型方块图中设置信号标签,以及如何显
示信号特性
信号组操作
如何创建和使用可互换的信号组,包括如何编辑
信号组,如何编辑信号和波形,以及如何利用信
第 3 章 Simulink 信号操作
信号操作是 Simulink 模型中的一个重要内容,正确处理模型信号对于仿真结果的准确
性和模型的可读性具有重要的意义,本章的内容向读者介绍如何创建和使用 Simulink 信号。
本章的主要内容包括:
信号基础
介绍关键的信号概念,包括 Simulink 中的信号分
类、信号数据类型、信号总线、纯虚信号、信号
3.1.2 确定输出信号的维数
如果一个模块可以产生非标量信号,那么模块输出的信号的维数取决于模块参数;如 果模块是 Sources 库中的模块,那么模块输出的信号的维数取决于模块输入和模块参数的 维数。
确定 Sources 模块的输出维数 Sources 库中的模块是没有输入的模块,如 Constant 模块和 Sine Wave 模块等。如果用 户在模块的参数对话框内没有选择 Interpret Vector Parameters as 1-D 参数项,那么一个 Sources 模块输出的维数与其输出值参数的维数是相同的;如果选择了模块参数对话框内的 Interpret Vector Parameters as 1-D 参数项,那么在输出参数值的维数不是 N×1 或 1×N 的 情况下,模块输出的维数才等于输出值参数的维数,若输出参数值的维数是 N×1 或 1×N, 模块输出一个宽度为 N 的向量信号。 以 Sources 模块库中的 Constant 模块为例,这个模块输出一个等于其 Constant value 参 数 值 的 常 值 信 号 , 表 3-2 说 明 了 Constant value 参 数 的 维 数 和 Interpret Vector Parameters as 1-D 参数的设置值如何确定了 Constant 模块输出的维数。
第 3 章 Simulink 信号操作
43
3.1.4 输入和参数的标量扩展
标量扩展(Scalar expansion)是将标量值转换为相同维数的非标量数组的过程。许多 Simulink 模块都支持输入和参数的标量扩展。
控制信号
图 3-3 信号总线 信号总线是用来表示一组信号的纯虚信号,它是用来模拟捆绑在一起的电缆信号,没 有实际的数学或物理含义,Simulink 使用特定的线型来表示信号总线。如果用户在 Format 菜单下选择 Signal Dimensions 命令,Simulink 会显示总线中信号分量的数目,如图 3-4 所 示。
41
二维信号数组 信号值是实数(对应于复数)的信号 含有一个元素的数组,也就是,有一个元素的一维或二维数组 由 Mux 模块或 Demux 模块创建的信号 Simulink 用来确定信号和模块属性的过程,这些属性包括数据类型、信号 标签、采样时间、信号维数等。 信号所包含的元素个数,二维信号的大小通常表示为 M×N,M 是组成信 号的列数,N 是组成信号的行数。 一维信号数组 表示其它信号或信号组的信号 向量信号的大小
第 3 章 Simulink 信号操作
39
就是一组信号示意图,它没有任何数学或物理意义,当对模型进行仿真时,Simulink 会忽 略这些信号。
Simulink 中的纯虚模块,如 Bus Creator 模块或 Subsystem 模块可以产生纯虚信号。同 纯虚模块一样,纯虚信号也允许用户以图示方式简化模型。例如,利用 Bus Creator 模块, 用户可以将大量的非纯虚信号(也就是由非纯虚模块产生的信号)减化为单个的纯虚信号, 因此也就使整个用户模型更简洁,更便于理解,在这里,用户可以将纯虚信号看成是捆绑 在一起的一组信号。
3.1.3 确定信号及参数维数准则
当创建一个 Simulink 模型时,用户必须遵守 Simulink 中信号和参数维数的确定准则。 输入信号维数准则
一个模块的所有非标量输入必须有相同的维数。 一个模块可以混合有标量输入和非标量输入,但所有的非标量输入都必须有相同的维 数,Simulink 会扩展标量输入,以使其与非标量输入具有相同的维数,这样就遵守了上述 规则。
图 3-2
注意:纯虚信号可以用来表示纯虚信号及非纯虚信号。例如,用户可以用 Bus Creator 模块将多个纯虚信号和非纯虚信号组合成一个纯虚信号。在信号传 递过程中,如果 Simulink 发现纯虚信号的一个分量是它自身的纯虚,那么 Simulink 会利用信号传递来确定它的非纯虚分量,这个过程会一直继续下
信号总线
图 3-4 信号术语汇编 表 3-1 概述了 Simulink 用户接口和文档中用来描述信号的术语。
表 3-1 信号术语
术语
含义
复信号 数据类型
信号值是复数的信号 用来在 Simulink 内部表示信号值的格式
矩阵 实信号 标量 信号总线 信号传递
信号尺寸
向量 纯虚信号 宽度
第 3 章 Simulink 信号操作
向量或矩阵输入转换规则
在下列情况下,Simulink 会将向量转换为行矩阵或列矩阵,或者将行矩阵或列矩阵转 换为向量:
a. 如果一个向量信号被连接到要求矩阵的输入上,Simulink 会将向量转换为一行矩 阵或一列矩阵;
b. 如果一列矩阵或一行矩阵被连接到要求向量的输入上,Simulink 会将矩阵转换为 向量;