matlab-simulink 初级教程
- 格式:doc
- 大小:688.00 KB
- 文档页数:43
Experiment 1Introduction to Simulink1.1 ObjectiveThe objective of Experiment #1 is to familiarize the students with simulation of power electronic circuits in Matlab/Simulink environment. Please follow the instructions in the laboratory manual.1.2 Simulink Basics TutorialSimulink is a graphical extension to MATLAB for the modeling and simulation of systems. In Simulink, systems are drawn on screen as block diagrams. Many elements of block diagrams are available (such as transfer functions, summing junctions, etc.), as well as virtual input devices (such as function generators) and output devices (such as oscilloscopes). Simulink is integrated with MATLAB and data can be easily transferred between the programs. In this tutorial, we will introduce the basics of using Simulink to model and simulate a system.1.2.1 Starting SimulinkSimulink is started from the MATLAB command prompt by entering the following command: >>simulinkAlternatively, you can click on the "Simulink Library Browser" button at the top of the M ATLAB command window as shown below:The Simulink Library Browser window should now appear on the screen. Most of the blocks needed for modeling basic systems can be found in the subfolders of the main "Simulink" folder (opened by clicking on the "+" in front of "Simulink"). Once the "Simulink" folder has been opened, the Library Browser window should look like:1.2.2 Basic ElementsThere are two major classes of elements in Simulink: blocks and lines. Blocks are used to generate, modify, combine, output, and display signals. Lines are used to transfer signals from one block to another. BlocksThe subfolders underneath the "Simulink" folder indicate the general classes of blocks available for us to use:•Continuous: Linear, continuous-time system elements (integrators, transfer functions, state-space models, etc.)•Discrete: Linear, discrete-time system elements (integrators, transfer functions, state-space models, etc.)•Functions & Tables: User-defined functions and tables for interpolating function values•Math: Mathematical operators (sum, gain, dot product, etc.)•Nonlinear: Nonlinear operators (coulomb/viscous friction, switches, relays, etc.)•Signals & Systems: Blocks for controlling/monitoring signal(s) and for creating subsystems•Sinks: Used to output or display signals (displays, scopes, graphs, etc.)•Sources: Used to generate various signals (step, ramp, sinusoidal, etc.)Blocks have zero to several input terminals and zero to several output terminals. Unused input terminals are indicated by a small open triangle. Unused output terminals are indicated by a small triangular point. The block shown below has an unused input terminal on the left and an unused output terminal on the right.LinesLines transmit signals in the direction indicated by the arrow. Lines must always transmit signals from the output terminal of one block to the input terminal of another block. One exception to this is that a line can tap off of another line. This sends the original signal to each of two (or more) destination blocks, as shown below:Lines can never inject a signal into another line; lines must be combined through the use of a block such as a summing junction.A signal can be either a scalar signal or a vector signal. For Single-Input, Single-Output systems, scalar signals are generally used. For Multi-Input, Multi-Output systems, vector signals are often used, consisting of two or more scalar signals. The lines used to transmit scalar and vector signals are identical. The type of signal carried by a line is determined by the blocks on either end of the line.1.2.3 Building a SystemTo demonstrate how a system is represented using Simulink, we will build the block diagram for a simple model consisting of a sinusoidal input multiplied by a constant gain, which is shown below:This model will consist of three blocks: Sine Wave, Gain, and Scope. The Sine Wave is a Source Block from which a sinusoidal input signal originates. This signal is transferred through a line in the direction indicated by the arrow to the Gain Math Block. The Gain block modifies its input signal (multiplies it by a constant value) and outputs a new signal through a line to the Scope block. The Scope is a Sink Block used to display a signal (much like an oscilloscope).We begin building our system by bringing up a new model window in which to create the block diagram. This is done by clicking on the "New Model" button in the toolbar of the Simulink Library Browser (looks like a blank page).Building the system model is then accomplished through a series of steps:1.The necessary blocks are gathered from the Library Browser and placed in the model window.2.The parameters of the blocks are then modified to correspond with the system we are modeling.3.Finally, the blocks are connected with lines to complete the model.Each of these steps will be explained in detail using our example system. Once a system is built, simulations are run to analyze its behavior.Gathering BlocksEach of the blocks we will use in our example model will be taken from the Simulink Library Browser. To place the Sine Wave block into the model window, follow these steps:1.Click on the "+" in front of "Sources" (this is a subfolder beneath the "Simulink" folder) todisplay the various source blocks available for us to use.2.Scroll down until you see the "Sine Wave" block. Clicking on this will display a shortexplanation of what that block does in the space below the folder list:3. To insert a Sine Wave block into your model window, click on it in the Library Browser and drag the block into your workspace.The same method can be used to place the Gain and Scope blocks in the model window. The "Gain" block can be found in the "Math" subfolder and the "Scope" block is located in the "Sink" subfolder. Arrange the three blocks in the workspace (done by selecting and dragging an individual block to a new location) so that they look similar to the following:Modifying the BlocksSimulink allows us to modify the blocks in our model so that they accurately reflect the characteristics of the system we are analyzing. For example, we can modify the Sine Wave block by double-clicking on it. Doing so will cause the following window to appear:This window allows us to adjust the amplitude, frequency, and phase shift of the sinusoidal input. The "Sample time" value indicates the time interval between successive readings of the signal. Setting this value to 0 indicates the signal is sampled continuously.Let us assume that our system's sinusoidal input has:•Amplitude = 2•Frequency = pi•Phase = pi/2Enter these values into the appropriate fields (leave the "Sample time" set to 0) and click "OK" to accept them and exit the window. Note that the frequency and phase for our system contain 'pi' (3.1415...). These values can be entered into Simulink just as they have been shown.Next, we modify the Gain block by double-clicking on it in the model window. The following window will then appear:Note that Simulink gives a brief explanation of the block's function in the top portion of this window. In the case of the Gain block, the signal input to the block (u) is multiplied by a constant (k) to create the block's output signal (y). Changing the "Gain" parameter in this window changes the value of k.For our system, set k = 5. Enter this value in the "Gain" field, and click "OK" to close the window.The Scope block simply plots its input signal as a function of time, and thus there are no system parameters that we can change for it. We will look at the Scope block in more detail after we have run our simulation.Connecting the BlocksFor a block diagram to accurately reflect the system we are modeling, the Simulink blocks must be properly connected. In our example system, the signal output by the Sine Wave block is transmitted to the Gain block. The Gain block amplifies this signal and outputs its new value to the Scope block, which graphs the signal as a function of time. Thus, we need to draw lines from the output of the Sine Wave block to the input of the Gain block, and from the output of the Gain block to the input of the Scope block.Lines are drawn by dragging the mouse from where a signal starts (output terminal of a block) to where it ends (input terminal of another block). When drawing lines, it is important to make sure that the signal reaches each of its intended terminals. Simulink will turn the mouse pointer into a crosshair when it is close enough to an output terminal to begin drawing a line, and the pointer will change into a double crosshair when it is close enough to snap to an input terminal. A signal is properly connected if its arrowhead is filled in. If the arrowhead is open, it means the signal is not connected to both blocks. To fix an open signal, you can treat the open arrowhead as an output terminal and continue drawing the line to an input terminal in the same manner as explained before.Properly Connected SignalWhen drawing lines, you do not need to worry about the path you follow. The lines will route themselves automatically. Once blocks are connected, they can be repositioned for a neater appearance. This is done by clicking on and dragging each block to its desired location (signals will stay properly connected and will re-route themselves).After drawing in the lines and repositioning the blocks, the example system model should look like:In some models, it will be necessary to branch a signal so that it is transmitted to two or more different input terminals. This is done by first placing the mouse cursor at the location where the signal is to branch. Then, using either the CTRL key in conjunction with the left mouse button or just the right mouse button, drag the new line to its intended destination. This method was used to construct the branch in the Sine Wave output signal shown below:The routing of lines and the location of branches can be changed by dragging them to their desired new position. To delete an incorrectly drawn line, simply click on it to select it, and hit the DELETE key.1.2.4. Running SimulationsNow that our model has been constructed, we are ready to simulate the system. Before starting simulation, we need to set the simulation parameters. To do this, go to the Simulation menu and click on Configuration Parameters. The Configuration Parameters dialog box opens on your desktopEnter desired stop time (e.g. 100 microseconds), and change the Solver Options from Variable-step to fix-step and the step size to 1e-4. The step size specifies the resolution of simulation. Click Apply and OK to close the Configuration Parameters window.Go to the Simulation menu and click on Start, or just click on the "Start/Pause Simulation" button in the model window toolbar (looks like the "Play" button on a VCR). Because our example is a relatively simple model, its simulation runs almost instantaneously. With more complicated systems, however, you will be able to see the progress of the simulation by observing its running time in the lower box of the model window. Double-click the Scope block to view the output of the Gain block for the simulation as a function of time. Once the Scope window appears, click the "Auto scale" button in its toolbar (looks like a pair of binoculars) to scale the graph to better fit the window. Having done this, you should see the following:。
matlab simulink模型搭建方法Matlab Simulink是一个强大的多领域仿真和模型搭建环境,广泛应用于控制系统、信号处理、通信系统等多个领域。
本文将详细介绍Matlab Simulink模型搭建的方法,帮助您快速掌握这一技能。
一、Simulink基础操作1.启动Simulink:在Matlab命令窗口输入“simulink”,然后按回车键,即可启动Simulink。
2.创建新模型:在Simulink开始页面,点击“新建模型”按钮,或在菜单栏中选择“文件”→“新建”→“模型”,创建一个空白模型。
3.添加模块:在Simulink库浏览器中,找到所需的模块,将其拖拽到模型窗口中。
4.连接模块:将鼠标光标放在一个模块的输出端口上,按住鼠标左键并拖拽到另一个模块的输入端口,松开鼠标左键,完成模块间的连接。
5.参数设置:双击模型窗口中的模块,可以设置模块的参数。
6.模型仿真:在模型窗口中,点击工具栏上的“开始仿真”按钮,或选择“仿真”→“开始仿真”进行模型仿真。
二、常见模块介绍1.源模块:用于生成信号,如Step、Ramp、Sine Wave等。
2.转换模块:用于信号转换和处理,如Gain、Sum、Product、Scope 等。
3.控制模块:用于实现控制算法,如PID Controller、State-Space等。
4.建模模块:用于构建物理系统的数学模型,如Transfer Fcn、State-Space等。
5.仿真模块:用于设置仿真参数,如Stop Time、Solver Options等。
三、模型搭建实例以下以一个简单的线性系统为例,介绍Simulink模型搭建过程。
1.打开Simulink,创建一个空白模型。
2.在库浏览器中找到以下模块,并将其添加到模型窗口中:- Sine Wave(正弦波信号源)- Transfer Fcn(传递函数模块)- Scope(示波器模块)3.连接模块:- 将Sine Wave的输出端口连接到Transfer Fcn的输入端口。
如何使用MATLABSimulink进行系统建模如何使用MATLAB Simulink进行系统建模第一章:MATLAB Simulink简介Matlab Simulink是一款基于MATLAB的工程工具软件,用于进行系统建模和仿真。
它提供了一种直观的图形化方法,使工程师能够轻松地建立和模拟复杂的系统。
Simulink支持各种工程学科,包括电气、机械、控制和通信等领域。
本章将简要介绍MATLAB Simulink的基本概念和主要功能。
1.1 Simulink的基本概念Simulink使用图形化的方式进行系统建模,系统模型由各种元件和信号线组成。
元件表示系统的各个组成部分,信号线表示元件之间的数据传输。
1.2 Simulink的主要功能Simulink具有以下主要功能:- 系统建模:通过拖拽和连接元件,可以快速搭建系统模型。
- 仿真和调试:使用仿真器可以对系统模型进行实时仿真,并进行调试和分析。
- 自动代码生成:Simulink可以自动生成C、C++、Verilog等编程语言的代码,可用于系统的实现和验证。
第二章:Simulink建模基础在本章中,我们将详细介绍如何使用Simulink进行系统建模的基础知识和技巧。
2.1 模型创建在Simulink中,可以通过选择“File -> New Model”来创建一个新的模型。
在模型中,可以使用工具栏上的元件库来选择需要的元件,然后将其拖拽到模型中。
2.2 连接元件在模型中,元件之间的连接通常使用信号线来表示。
可以通过鼠标点击元件输出端口和输入端口的方式来建立连接。
可以使用线段工具来绘制信号线,也可以使用Ctrl + 鼠标点击来删除信号线。
2.3 参数设置在建模过程中,可以通过双击元件来设置各个元件的参数。
每个元件都有各自的参数面板,可以根据具体需求进行设置。
第三章:Simulink高级建模技巧在本章中,我们将介绍一些进阶的Simulink建模技巧,如子系统的使用、模型的分层和复用等。
Simulink基本操作(2014.5 兰交大萌芽)Simulink是MATLAB软件所带的软件仿真工具。
其强大的功能几乎可以满足所有的系统仿真。
要掌握基本的仿真操作,必须认识仿真各个模块的英文名称。
其实记住图形是干嘛的即可。
说实话在运用外语软件时,英语真是个让人头疼的问题。
不过,得必须记住的是,在任务/现实面前,只能向前迈步。
下面我们介绍Simulink的基本操作,以截图为主。
一、1.打开MATLAB软件,点击Simulink按钮,进入Simulink仿真界面。
点击进入simulink2.新建*-mdl文件。
也可以打开已有的*-mdl文件进行编辑或仿真。
点击新建3.出现*-mdl界面。
现在进可以编辑机构框图了。
4.完成后,点击运行按钮,同时保存文件位置,注意文件地址要明确。
运行按钮Simulink给我们的基本模块结构很多,都在Simulink library browser窗口中。
其中有基本模块和专业模块。
常用基本模块。
二、下面,我们举例说明其简单的使用说明。
以下图为例。
该图为计算机控制系统的最少拍有纹波控制系统。
1.首先,添加各模块,同时修改其参数值,还可以边添加连线。
2.右键点击Add to …即可添加模块到*-mdl文件中。
点击右键点击我们可以改变其位置和大小。
3.再依次添加这两个。
发现在*-mdl文件中出现以下现象,不要着急!4.这时我们可以把他们移开(点击一个,拉开,再点击一个,拉开)同时放大合适的比例。
5.修改其属性的选中模块,双击打开。
比如Sum修改点击ok 得到6.双击Mux,在option中选择signals 点击ok,得连线鼠标放在箭头处,拉动,放入入口箭头处即可。
7.题目里出现两个相同模块的,可以进行复制、粘贴。
得到两个模块后在进行属性和参数的修改8.接着进行别的模块添加工作。
9.模块添加完成。
进行属性和参数设置,得到注;为了能让大家看的清楚些,我把布局缩小了,在自己实验仿真时,完全可以放开成大窗口,将模块放大,更清晰明了,不要小家子气。
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 按钮/。
有关创建模型的其他方法, 请参阅创建模型。
matlab之simulink最通俗教程Simulink是MATLAB的一个重要工具箱,用于建模和仿真控制系统。
Simulink提供了一种图形化建模环境,可以方便地构建复杂系统,并对其进行仿真和分析。
本文将详细介绍Simulink的基本原理和使用方法,以便初学者快速入门。
Simulink模型由各种模块组成,这些模块可以是系统组件、数学算法或信号处理函数。
用户可以使用Simulink库中的预定义模块,也可以自己编写MATLAB函数来创建自定义模块。
模块之间的连接通过信号线进行,可以传递各种类型的信号,如数值、布尔值和字符串。
使用Simulink建模的第一步是创建一个新模型。
在MATLAB命令窗口中输入“simulink”命令即可打开Simulink库浏览器。
然后,可以从左侧的“Simulink Library Browser”面板中拖动所需的模块到模型窗口中。
常用的模块包括输入输出模块、数学运算模块和逻辑控制模块。
在模型中添加模块后,可以使用鼠标将它们连接在一起。
要创建连接线,只需点击模块输出端口并将鼠标拖动到另一个模块的输入端口。
连接线将自动连接两个模块,形成信号传递路径。
连接线上可以添加箭头标记,用于指定信号的流动方向。
模型的参数和设置可以在模型窗口的右侧“Properties”面板中进行调整。
例如,可以设置模块的初始状态、仿真时间范围和采样时间。
还可以为模块添加注释、设置显示颜色和调整模块大小等。
Simulink提供了多种仿真和分析工具,用于评估模型的性能和行为。
可以使用“Simulate”按钮开始仿真并观察模型的实时响应。
仿真结果可以以图表或波形图的形式显示,并可以保存和导出到MATLAB工作空间中进行后续处理。
还可以使用模型验证和优化工具来检查模型的准确性和效率。
除了基本的建模和仿真功能外,Simulink还支持代码生成和硬件连接。
可以将Simulink模型转换为C代码,并嵌入到嵌入式系统中。
使用篇1.以管理员身份运行matlab2.登录后把当前文件夹改成C盘,找到TwinCAT→Functions→TE1400→SetupTwinCATTarget.p3.找到这个文件后右键选择Run,注意:这一步是为了选择matlabsimulink编译的module所需要的编译器种类,是第一次运行使用matlab+TE1400的时候必须执行的,以后就不必每次都操作这一步。
运行后在matlab主窗口提示让你选择是否用本地的编译器因为本地有VS2010的编译器,所以选择y后敲回车随后matlab找到本地有两种编译器,一个是matlab本体的lcc-win32 C2.4.1,另一个是VS2010,选择VS2010所代表的数字,输入2敲回车最后让matlab让你确认编译器的选择,输入y敲回车提示以下信息说明编译器选择完成4.点击工具栏中simulink图标5.弹出simulink编辑界面后,点击工具栏中的打开模型6.找到案例模型TempContrTest.mdl,点击打开7.本次案例模型是一个简单的温度控制External Setpoint是设定温度Feedback Temp是当前温度CoolerON是开关量输出8.打开simulation菜单栏,选择configuration parameters进行参数设定(1)进入参数设定后,选择右边的树形栏中的Solver,把其中的Type改成Fixed-step(2)之后选择树形栏中的Code Generation,把其中的System target file改成TwinCAT.tlc 点击Browse可以进行选择(3)继续选择树形栏中的Tc Module,在Publish module和Publish binaries for platform “TwinCAT RealTime(x86)”前打勾(4)最后选择树形栏中的Tc Advanced,把Task assignment改成Default在Add to cyclic caller,Variable cycle time,Export block digram以及Export block diagram debug information前打勾(5)以上操作完成后点击左下角的Apply(6)选择树形栏中的Code Generation,把Generate code only勾选后点击Generate code,随后matlab就开始把这个模型通过TE1400生成TC3所识别的Module了(7)回到matlab主窗口,等看到以下提示说明Module生成完成(8)我们来看下生成的Module会在什么位置可以发现在TwinCAT/3.1/CustomConfig/Modules路径下会生成名字和案例模型名字一样的文件夹TempContrTest打开可以发现里面其实主要是.tmc文件是TC3所需要的,其他都是一些描述文件,所以可以把.tmc文件拷贝出来,给一些没有Matlab的电脑上用9.打开TC3,并新建项目10.把名称改成英文,例如matlabsimulink,点击确认11.打开SYSTEM,右键TcCOM Objects添加新项12.TC3会自动找到之前生成的.tmc文件,选中后点击OK进行添加13.添加好后我们可以发现TcCOM Objects下出现matlab生成的Module,并且3个变量出现在IO位置,方便和PLC程序或者硬件IO进行变量连接14.右键Tasks添加新项名字可以改成matlab,点击OK添加新的Task15.因为我需要实施做温度计算,所以可以这个Task的优先级提高,修改成1,周期用默认的10ms即可16.双击TcCOM Objects下面的Object1(TempContrTest)Depend On改成Manual Config,并把Task分配成之前创建的名为“matlab”的Task17.右键PLC添加新项18.把名称修改为英文,例如test19.编辑一段模拟程序,模拟温度的升降20.程序写好后右键test Project,选择生成开始编译程序21.编译好后在test Instance自动生成3个变量22.接下来要做的就是把PLC中3个变量和matlab中三个变量链接起来Switch→CoolerONSP→External SetpointPV Feedback Temp23.变量链接完成后开始下传配置和程序,选择菜单栏TwinCAT,点击Activate Configuration弹出窗口点击确定提示切换到运行模式点击确定观察右下角图标是否编程绿色运行状态弹出窗口点击确定提示切换到运行模式点击确定观察右下角图标是否编程绿色运行状态24.打开PLC菜单,选择“登录到”把程序在线25.打开PLC菜单,选择“启动”把程序运行26.观察程序,看到成功利用matlab温度算法运行程序27.打开Object1(TempContrTest),选择Block Diagram也可以同时观察Matlab温度算法实时状态注:上图中可以看到由一个红色字提示说是非商业的,虽然TE1400插件装上了,但用的还是7天试用版,所以对于试用版有一些限制,查询information system可以看到如下:TC3中Scope View简单使用在之前的基础上来看下TC3下Scope View如何使用,装好TC3后Scope View会自动集成在TC3中1.首先右键“解决方案”选择添加,点击新建项目2.选择TwinCAT Measurement中的Measurement Scope Project,名称改成英文,例如tempcontrol,点击确定3.右键Axis,选择Target Browser4.打开小电脑图标下的Port_851(851),点击MAIN5.把MAIN程序中PV和SP分别添加到同一个坐标上6.保证程序在运行后,点击工具栏中的Record开始记录两个变量7.随后就可以观察到当前PV和SP的示波图下图中绿色是PV,蓝色是SP。
simulink操作流程Simulink操作流程。
一、啥是Simulink呢?Simulink呀,就像是一个超级好玩的积木搭建世界。
它是MATLAB里的一个超酷的工具,让咱们可以轻松地构建各种各样的系统模型呢。
比如说,你要是想模拟一个汽车的行驶过程,或者是一个电路的工作状态,Simulink就可以帮你搞定。
它就像一个万能的小助手,只要你有想法,就能在这个小天地里把想法变成模型。
二、打开Simulink的大门。
1. 先得打开MATLAB哦。
这就像是进入魔法世界得先找到入口一样。
打开MATLAB 之后呢,在它的主界面里,就能看到Simulink的图标啦,就像一个小宝藏的入口标志。
轻轻一点那个图标,就进入到Simulink的世界咯。
2. 刚进去的时候,可能会觉得有点眼花缭乱。
不过别担心,这就像你第一次走进一个超级大的游乐场,到处都是新鲜好玩的东西。
你会看到一个大大的空白区域,这就是咱们要大展身手的地方啦。
三、开始搭积木——构建模型。
1. 找模块。
- 在Simulink的左边有一个长长的库,这里面藏着各种各样的模块,就像一个个小零件。
比如说,如果你要构建一个数学计算的模型,就能在Math Operations这个库里找到加法、减法、乘法之类的模块。
这些模块长得都挺有趣的,就像一个个小方块,上面还有一些简单的标识,让你一眼就能看出来它是干啥的。
- 要是想做个信号处理的模型,Signal Processing库里的模块就派上用场啦。
你可以像在玩具箱里找玩具一样,在这些库里翻找你需要的模块。
2. 拖模块。
- 当你找到想要的模块之后呢,就可以用鼠标把它拖到中间的空白区域啦。
这感觉就像从玩具箱里拿出一个小积木,然后放到搭建台上。
比如说,你拖了一个正弦波模块到中间,它就静静地待在那儿,等着和其他模块交朋友呢。
3. 连接模块。
- 光有孤零零的模块可不行,还得把它们连接起来。
这就像把小积木用小棍子连接起来一样。
用鼠标点住一个模块的输出口(一般是小箭头),然后拖到另一个模块的输入口上,就把它们连接起来啦。
Simulink使用入门下面简要的介绍一下,如何使用Simulink进行建模和仿真实验:1.启动matlab之后,在命令窗口中输入命令“Simulink”或者单击工具栏上的Simulink图标,打开Simulink模块库窗口。
如图1所示。
图1 Simulink模块库窗口2.在Simulink模块库窗口中单击菜单项“File/New/Model”,就可以新建一个Simulink模型文件。
如图2所示。
图2 Simulink 模型文件3.在2中所建立的Simulink文件窗口中单击“File/Save as”,可以修改文件名,并把文件保存在自己所要保存的路径下。
4.双击Communications Blockset,该模块库包含了通信系统中常用的功能模块:Channels(传输信道),Comm Sources(信源),Comm Sink(信宿),Source Cording(信源编码),Modulation(调制),等等。
如图3所示。
图3 通信工具箱里的功能模块5.在Simulink基础库中找到自己需要的模块,选取该模块,直接拖动到新建模型窗口中的适当位置,或者选取该模块后,右击鼠标,“Add to…”加到所建模块窗口中。
图4中,把信号发生器放到了amn中。
图4 利用模块库建立仿真模型6.如果需要对模型模块进行参数设置和修改,只需选中模型文件中的相应模块,单击鼠标右键,选取相应的参数进行修改,或者双击鼠标左键,进行修改。
还可以在选中模块之后通过拖动鼠标来修改模块的位置、大小和形状。
7.通过快捷菜单的其它选项还可以对模型的颜色、旋转、字体、阴影等属性进行设置,也可以对模型进行接剪切、拷贝或删除。
8.模块外部的大于符号“>”分别表示信号的输入输出节点,为了连接两个模型的输入输出,可以将鼠标置于节点处,这时鼠标显示为十字,拖动鼠标到另一个模块的端口,然后释放鼠标的按钮,则形成了带箭头的连线,箭头的方向表示信号的流向。
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 子模块库,便可看到各种输入源模块。
图7.1 Simulink(4) 用鼠标单击所需要的输入信号源模块“Sine Wave”(正弦信号),将其拖放到的空白模型窗口“untitled”,则“Sine Wave”模块就被添加到untitled窗口;也可以用鼠标选中“Sine Wave”模块,单击鼠标右键,在快捷菜单中选择“add to 'untitled'”命令,就可以将“Sine Wave”模块添加到untitled窗口,如图7.2所示。
图7.2 Simulink(5) 用同样的方法打开接收模块库“Sinks”,选择其中的“Scope”模块(示波器)拖放到“untitled”窗口中。
(6)在“untitled ”窗口中,用鼠标指向“Sine Wave ”右侧的输出端,当光标变为十字符时,按住鼠标拖向“Scope ”模块的输入端,松开鼠标按键,就完成了两个模块间的信号线连接,一个简单模型已经建成。
如图7.3所示。
(7) 开始仿真,单击“untitled ”模型窗口中“开始仿真”图标,或者选择菜单“Simulink ”——“Start ”,则仿真开始。
双击“Scope ”模块出现示波器显示屏,可以看到黄色的正弦波形。
如图7.4所示。
(8) 保存模型,单击工具栏的图标,将该模型保存为“Ex0701.mdl ”文件。
图7.3 Simulink 模型图7.4 示波器窗7.2 Simulink的文件操作和模型窗口7.2.1 Simulink的文件操作1. 新建文件新建仿真模型文件有几种操作:在MATLAB的命令窗口选择菜单“File”“New”“Model”。
在图7.1的Simulink模块库浏览器窗口选择菜单“File”“New”“Model”,或者单击工具栏的图标。
在图7.3的Simulink模型窗口选择菜单“File”“New”“Model”,或者单击工具栏的图标。
2. 打开文件打开仿真模型文件有几种操作:在MATLAB的命令窗口输入不加扩展名的文件名,该文件必须在当前搜索路径中,例如输入“Ex0701”。
在MATLAB的命令窗口选择菜单“File”Open…”或者单击工具栏的图标打开文件。
在图7.1的Simulink 模块库浏览器窗口选择菜单“File ”“Open …”或者单击工具栏的图标打开“.mdl ”文件。
在图7.3的Simulink 模型窗口中选择菜单“File ”“Open …”或者单击工具栏的图标打开文件。
7.2.2 Simulink 的模型窗口模型窗口由菜单、工具栏、模型浏览器窗口、模型框图窗口以及状态栏组成。
1. 状态栏2. 工具栏模型窗口工具栏如图7.6所示。
状态菜单 模型模型图7.5 双窗口模工具3. 菜单Simulink 的模型窗口的常用菜单如表7.1所示。
表7.1模型窗口常用菜单表菜单名菜单项 功能 FileNew ——Model 新建模型 Model properties模型属性Preferences SIMULINK 界面的默认设置选项 Print … 打印模型Close 关闭当前Simulink 窗口 Exit MATLAB 退出MATLAB 系统 EditCreate subsystem创建子系统 Mask subsystem … 封装子系统Look under mask 查看封装子系统的内部结构 Update diagram 更新模型框图的外观 ViewGo to parent显示当前系统的父系统 Model browser options模型浏览器设置Block data tips options 鼠标位于模块上方时显示模块内部数据 Library browser 显示库浏览器Fit system to view 自动选择最合适的显示比例 Normal以正常比例(100%)显示模型开始仿真 打开库浏览器结束仿真打开调试器展示父系统单双窗口切换 图7.6 工具栏仿真模型 更新模块框图创建并编译生成exe 文件7.3 模型的创建7.3.1模块的操作1. 对象的选定选定单个对象选定对象只要在对象上单击鼠标,被选定的对象的四角处会出现小黑块编辑框。
选定多个对象如果选定多个对象,可以按下Shift键,然后再单击所需选定的模块;或者用鼠标拉出矩形虚线框,将所有待选模块框在其中,则矩形框中所有的对象均被选中,如图7.7所示。
选定所有对象如果要选定所有对象,可以选择菜单“Edit” “Select all”。
2. 模块的复制(1) 不同模型窗口(包括模型库窗口)之间的模块复制选定模块,用鼠标将其拖到另一模型窗口。
选定模块,使用菜单的“Copy”和“Paste”命令。
选定模块,使用工具栏的“Copy”和“Paste”按钮。
(2) 在同一模型窗口内的复制模块(如图7.8所示)选定模块,按下鼠标右键,拖动模块到合适的地方,释放鼠标。
选定模块,按住Ctrl键,再用鼠标拖动对象到合适的地方,释放鼠标。
图7.7 选定多个对象使用菜单和工具栏中的“Copy”和“Paste”按钮。
3. 模块的移动在同一模型窗口移动模块选定需要移动模块,用鼠标将模块拖到合适的地方。
在不同模型窗之间移动模块在不同模型窗之间移动模块,在用鼠标移动的同时按下Shift键。
当模块移动时,与之相连的连线也随之移动。
4. 模块的删除要删除模块,应选定待删除模块,按Delete键;或者用菜单“Edit”→“Clear”或“Cut”;或者用工具栏的“Cut”按钮。
5. 改变模块大小选定需要改变大小的模块,出现小黑块编辑框后,用鼠标拖动编辑框,可以实现放大或缩小。
6. 模块的翻转模块翻转180度选定模块,选择菜单“Format”→“Flip Block”可以将模块旋转180度,如同7.9中间为翻转180度示波器模块。
模块翻转90度选定模块,选择菜单“Format”→“Rotate Block”可以将模块旋转90度,如图7.9右边示波器所示。
如果一次翻转不能达到要求,可以多次翻转来实现。
图7.8 在同一模型窗口复制对象图7.9 翻转模块7. 模块名的编辑修改模块名单击模块下面或旁边的模块名,出现虚线编辑框就可对模块名进行修改。
模块名字体设置选定模块,选择菜单“Format”→“Font”,打开字体对话框设置字体。
模块名的显示和隐藏选定模块,选择菜单“Format”→“Hide /Show name”,可以隐藏或显示模块名。
模块名的翻转选定模块,选择菜单“Format”→“Flip name”,可以翻转模块名。
7.3.2信号线的操作1. 模块间连线先将光标指向一个模块的输出端,待光标变为十字符后,按下鼠标键并拖动,直到另一模块的输入端。
2. 信号线的分支和折曲(1) 分支的产生将光标指向信号线的分支点上,按鼠标右键,光标变为十字符,拖动鼠标直到分支线的终点,释放鼠标;或者按住Ctrl键,同时按下鼠标左键拖动鼠标到分支线的终点,如图7.10所示。
(2) 信号线的折线选中已存在的信号线,将光标指向折点处,按住Shift键,同时按下鼠标左键,当光标变成小圆圈时,用鼠标拖动小圆圈将折点拉至合适处,释放鼠标,如图7.11所示。
图7.10 信号线的分支图7.11 信号线的折线3. 信号线文本注释(label)添加文本注释双击需要添加文本注释的信号线,则出现一个空的文字填写框,在其中输入文本。
修改文本注释单击需要修改的文本注释,出现虚线编辑框即可修改文本。
移动文本注释单击标识,出现编辑框后,就可以移动编辑框。
复制文本注释单击需要复制的文本注释,按下Ctrl键同时移动文本注释,或者用菜单和工具栏的复制操作。
4. 在信号线中插入模块如果模块只有一个输入端口和一个输出端口,则该模块可以直接被插入到一条信号线中。
7.3.3给模型添加文本注释(1) 添加模型的文本注释在需要当作注释区的中心位置,双击鼠标左键,就会出现编辑框,在编辑框中就可以输入文字注释。
(2) 注释的移动在注释文字处单击鼠标左键,当出现文本编辑框后,用鼠标就可以拖动该文本编辑框。
7.4 Simulink的基本模块7.4.1基本模块Simulink的基本模块包括9个子模块库。
1.输入信号源模块库(Sources)输入信号源模块是用来向模型提供输入信号。
常用的输入信号源模块源如表7.2所示。
表7.2 常用的输入信号源模块表名称模块形状功能说明Constant 恒值常数,可设置数值Step 阶跃信号Ramp 线性增加或减小的信号Sine Wave 正弦波输出Signal Generator 信号发生器,可以产生正弦、方波、锯齿波和随机波信号From File 从文件获取数据From Workspace 从当前工作空间定义的矩阵读数据Clock 仿真时钟,输出每个仿真步点的时间In 输入模块2. 接收模块库(Sinks)接收模块是用来接收模块信号的,常用的接收模块如表7.3所示。
表7.3 常用的接收模块表名称模块形状功能说明Scope 示波器,显示实时信号Display 实时数值显示XY Graph 显示X-Y两个信号的关系图To File 把数据保存为文件To Workspace 把数据写成矩阵输出到工作空间Stop Simulation 输入不为零时终止仿真,常与关系模块配合使用Out 输出模块3. 连续系统模块库(Continuous)连续系统模块是构成连续系统的环节,常用的连续系统模块如表7.4所示。
表7.4 常用的连续系统模块表名称模块形状功能说明Integrator 积分环节Derivative 微分环节State-Space 状态方程模型Transfer Fcn 传递函数模型Zero-Pole 零—极点增益模型Transport Delay 把输入信号按给定的时间做延时4. 离散系统模块库(Discrete)离散系统模块是用来构成离散系统的环节,常用的离散系统模块如表7.5所示。