1、在抗击“非典”期间,学校课程安排受到很大的影响,因此对授课的方式和内容作了相应的修改:,1、取消第四章,将仿真实例的学习排入前三章,要求同学通过自己的上机实践学习Matlab和Simulink的基本操作。 2、原上机实践部分的内容安排为几个范例的学习。同学们应充分利用范例对所涉及的关键内容进行实践。,计 算 机 仿 真 技 术,童 亦 斌 2003.4,系统的分析方法,系统:客观世界中相互作用的实体所构成的具有特定功能的整体。 为了定量分析系统,可将其本身的特点和内部的相互关系抽象出来,构造出系统的模型。对系统的分析变成对系统模型的分析。 系统模型:分物理模型和数学模型,由于计算机技术的发展,
2、数学模型的应用越来越广泛。 系统的数学模型:描述系统动态特性的数学表达式。 根据系统的性质和数学分析工具可分为:连续系统、离散时间系统、离散事件系统、混合系统等。,系统的仿真分析方法,仿真技术:用模拟的方法模拟实际系统的表现。 仿真技术的基本方法:数学模型仿真、物理模型仿真、硬件仿真、操作仿真、虚拟现实。 系统仿真:根据系统数学模型研究系统性能。 计算机仿真:利用计算机完成对系统的仿真。本课程特指利用计算机、根据系统的数学模型对电路系统进行仿真。,计算机仿真技术讲授内容,计算机仿真技术概述 Matlab和Simulink简介 计算机仿真的建模 计算机仿真实例 计算机仿真上机实践,本课程的学习方
3、法、作业和考试,学习方法:特别强调直接在计算机进行实践,强调学习利用Matlab各种资源解决问题; 作业:分别关于Matlab的基本使用、Simulink的建模、S函数和Simulink结果的调用; 考试:建立一个交流异步电机变频启动的计算机仿真模型,对交流异步电机的变频启动过程进行观察;,交流电机变频系统框图,考试的基本要求和条件,基本要求利用搭建的模型研究变频启动过程中电机电流的变化情况; 条件:电源电压直流600V、电机为Simulink默认的交流异步电机、电机空载、采用理想IGBT逆变器桥、开关频率固定为2500Hz、输出频率从5Hz变化到50Hz、启动时间可调;,第一章 计算机仿真技
4、术概述,为什么要进行计算机仿真? 计算机仿真能完成什么任务? 计算机仿真技术的关键 仿真软件的发展状况 电子设计自动化的主要应用领域 计算机仿真的基本步骤 计算机仿真设计的原则,1.1 为什么要进行计算机仿真?,实际系统的理论分析往往非常复杂,传统的分析方法几乎不可能完成; 很多时候几乎不可能或很难进行真实实验的分析和验证; 实验的成本高、效率低、周期长;,通过计算机仿真我们能够:,提高设计效率、缩短设计周期; 准确、全面地模拟和分析系统; 提高产品和技术的设计质量; 降低设计成本; 充分发挥设计人员的创造性; 降低干扰因素的影响,集中注意于关键问题; 优化系统和参数的设计,1.2 计算机仿真
5、能完成什么任务?,动态系统的仿真分析; 参数的优化; 极限参数的设计; 环境参数对系统性能的影响; 突发或偶发事件的模拟; 电子设计自动化(Electronic Design Automation),1.3 计算机仿真技术的关键,数值计算算法; 电路数学模型建立的模块化; 不同应用软件间的数据共享; 仿真计算的效率和准确性; 混合系统的建模和仿真; 仿真结果的可视化;,1.4 仿真软件的发展状况,现代仿真软件的特点:功能强大的数学软件包作为计算引擎、交互式的编程方式、友好的图形界面、丰富的模型资源和良好的程序接口等;,早期仿真语言和程序,早期数学软件包,现代的仿真软件,1.5 电子设计自动化的
6、主要应用领域,EDA的主要应用领域包括:集成电路设计模拟电路仿真动态系统的仿真 EDA主要软件及特点:Protel、Pspice、Saber、Matlab,1.6 计算机仿真的基本步骤,计算机仿真过程包括分析、建模、计算和验证; 模型的调试和计算主要依靠仿真软件的设计和使用; 验证过程非常重要; 人为的干预和控制对仿真的效果有很大的影响;,系统和任务分析,建立系统的仿真模型,模型的调试与计算,仿真结果的验证,1.7 计算机仿真设计的原则,“分级仿真”原则:对系统的分析应根据任务的不同采用不同的仿真模型或算法,合理简化系统,突出问题的关键; “准确度与速度合理兼顾”原则:在仿真的不同阶段合理调整
7、对准确度和速度的要求,提高仿真效率; “可信验证”原则:所有仿真结果应有可信的验证方法或依据;,第二章 Matlab和Simulink简介,Matlab和Simulink简介 MATLAB Installation MATLAB Desktop Array Scripts and Functions Simulink交互式仿真环境 Solver S-Function,2.0 Matlab和Simulink简介,Matlab是集数值计算、符号运算和图形处理功能于一身的超级科学计算语言; 与其它计算语言相比,Matlab在功能、开放性和易学性等方面独占鳌头; Matlab的核心是数值计算,Simu
8、link是建立系统框图和仿真的环境;,* Battery(s) V0 43 0 DC 15V; V1 39 0 DC 15V; *DISCHARGE V_0 41 0 PULSE(-15 15 196ms 10us 10us 20ms 200ms); *OFFSET V_1 5 0 PULSE(5 -5.7 150ms 10us 10us 50ms 200ms); *PIBIAS V_2 1 0 PULSE(10 -10 190ms 2us 2us 10ms 200ms); * Misc .TRAN 10us 210ms 0s 10ms UIC,语句方式,框图方式,2.1 MATLAB Ins
9、tallation,The main Products of Matlab for modeling and simulating dynamical systems:MatlabSymbolic MathSimulinkControl System,2.2 MATLAB Desktop,Command Window(指令窗) Command History(历史指令窗) Launch Pad(交互界面分类目录窗) Help Browser(帮助浏览器) Current Directory Browser(当前目录浏览器) Workspace Browser(工作空间浏览器) Array Ed
10、itor(内存数组浏览器) Editor/Debugger(M文件编辑/调试器),Launch Pad,Current Directory Browser,Command Window,Matlab常用标点的功能,2.3 Array,Array(数组)的基本概念 数组在Matlab仿真计算中的应用 Numeric Array(数值数组) 数值元素的标识 Character Array(字符串数组) Cell Array(元胞数组) Structure Array(架构数组),2.3.1 Array(数组)的基本概念,MATLAB works with only a single objectt
11、he array(数组)。 数组:一组数组成的长方阵列。Its typeIts dimensionsIts contentThe data associated with this arrayIf numeric,whether the variable is real or complexIf a structure,the number of fields and field names,数组与矩阵的区别,矩阵:作为一种变换或映射算子的体现,矩阵有着明确而严格的数学规则; 数组:Matlab设置数组的目的主要是为了简化数据的管理和操作、使指令的形式更加自然、程序的运行更加有效; Matla
12、b中对数组的计算实际上是对数组中每个元素平等地施加同样地操作;,2.3.2 数组在Matlab仿真计算中的应用,Matlab中数据的存放和处理均采用数组; Matlab中的数组主要分为:Numeric Array,Character Array,Cell Array, Structure Array Character Array可用于仿真结果和过程的可视化; Structure Array可用于与Simulink交换信息,包括仿真参数的设定、结果的获取和处理等;,2.3.3 Numeric Array(数值数组),数值数组是Matlab中最基本地数组形式; 数值数组分实数数组和复数数组;,
13、A=1 2 3 4,5 6 7 8 A =1 2 3 4 5 6 7 8 A=1 2 3 4;5 6 7 8 A =1 2 3 45 6 7 8 A*4 ans =4 8 12 1620 24 28 32,2.3.4 数组元素的标识,“全下标”标识 “单下标”标识, A(4) ans = 6 A(1,4) ans = 4 A(2,3)=0; A A =1 2 3 45 6 0 8,A =1 2 3 45 6 7 8,单下标,全下标,冒号的使用,j:k is the same as j,j+1,.,k j:i:k is the same as j,j+i,j+2i, .,k A(:,j) is
14、the j-th column of A A(i,:) is the i-th row of A,2.3.5 Character Array(字符串数组),字符串数组基本元素是字符串。 字符串数组可用于计算过程和结果的可视化。 字符串数组的直接创建。 利用串操作函数创建串数组。,A = The exmaple of the Character Arrays, A=1234 ;12345 A = 1234 12345, A=char(1234,12345) A = 1234 12345,转换函数产生数码字符串,Int2str(整数数组转换为串数组); Num2str(非整数数组转换为串数组);,
15、A=rand(2,4) A =0.9501 0.6068 0.8913 0.45650.2311 0.4860 0.7621 0.0185 B=num2str(A,2),C=int2str(A) B =0.95 0.61 0.89 0.460.23 0.49 0.76 0.019 C =1 1 1 00 0 1 0 class(B),class(C) ans =char ans =char,clear %清除内存中地所有变 a=2,w=3; % t=0:0.01:10; %取自变量采样数组 y=exp(-a*t).*sin(w*t); %计算函数值,产生函数数组 y_max,i_max=max
16、(y); %找最大值的位置 t_text=t=,num2str(t(i_max); %生成最大值点的横坐标字符串 y_text=y=,num2str(y_max); %生成最大值点的众坐标字符串 max_text=char(maximum,t_text,y_text); %生成最大值点的字符串 tit=y=exp(-,num2str(a),t)*sin(,num2str(w),t); % plot(t,zeros(size(t),k); %画纵坐标为0的基准线 hold on % plot(t,y,b); %绘制函数曲线 plot(t(i_max),y_max,r.,MarkerSize,20
17、); %标注最大值点 text(t(i_max)+0.3,y_max+0.05,max_text); %标注最大值点的字符串 title(tit),xlabel(t),ylabel(y),hold off %标注图名、横坐标名、纵坐标名,字符串数组用于计算结果的可视化实例,字符串数组用于计算结果的可视化实例,2.3.6 Cell Array(元胞数组),The elements of a cell array are cells, containers that can hold other MATLAB arrays.,2.3.6.1 Creating Cell Arrays,Cell in
18、dexing(元胞外标识): A(1,1) = 1 4 3; 0 5 8; 7 2 9; A(1,2) = Anne Smith; A(2,1) = 3+7i; A(2,2) = -pi:pi/10:pi;,A(1,1) = 1 4 3; 0 5 8; 7 2 9; A(1,2) = Anne Smith; A(2,1) = 3+7i; A(2,2) = -pi:pi/10:pi; A = 3x3 double Anne Smith 3.0000+ 7.0000i 1x21 double,Content indexin(元胞内标识): A1,1 = 1 4 3; 0 5 8; 7 2 9; A
19、1,2 = Anne Smith; A2,1 = 3+7i; A2,2 = -pi:pi/10:pi;,2.3.6.2 Obtaining Data from Cell Arrays,调取元胞数据分:元胞的调取和元胞内容的调取; 元胞地标识分:元胞外标识和元胞内标识;,A1,1 ans =1 4 30 5 87 2 9 A1,1(1,2) ans = 4,A(1,1)=1 2 3;4 5 6; A(1,1)=2x3 double A1,1=1 2 34 5 6,2.3.7 Structure Array(构架数组),The elements of a Structure Array are S
20、tructures. Structures are MATLAB arrays with “data containers“ called fields. The fields of a structure can contain any kind of data. Structure Array的基本组分是架构( Structure) ,在Structure以下划分了域(Field),域可用于存放任何形式的数据。,2.3.7.1 Structure Array的基本结构,patient.name = John Doe; patient.billing = 127.00; patient.te
21、st = 79 75 73; 180 178 177.5; 220 210 205;,A Structure Array,A Structure,2.3.7.2 Building Structure Arrays,方法一:直接声明 patient(2).name = Ann Lane; patient(2).billing = 28.50; patient(2).test = 68 70 68; 118 118 119; 172 170 169;,方法二:利用struct指令 patient(2)=struct(name,Ann Lane,billing,28.50,test,68 70 68
22、; 118 118 119; 172 170 169) patient = 1x2 struct array with fields: name billing test,2.3.7.3 Accessing Data in Structure Arrays,架构数组的存取:架构域内容,mypatients = patient(1:2) 1x2 struct array with fields:namebillingtest,mypatients(1) ans = name: John Doebilling: 127test: 3x3 double,str = patient(2).name s
23、tr = Ann Lane,patient(2).test ans =68 70 68118 118 119172 170 169,2.4 Scripts and Functions,Scripts(脚本文件), which do not accept input arguments or return output arguments. They operate on data in the workspace. Functions(函数文件), which can accept input arguments and return output arguments. Internal va
24、riables are local to the function.,2.4.1 Scripts,When you invoke a script, Matlab simply executes the commands found in the file. Scripts can operate on existing data in the workspace, or they can create new data on which to operate. Although scripts do not return output arguments, any variables tha
25、t they create remain in the workspace, to be used in subsequent computations. Scripts在使用Simulink时的主要作用:作为调用Simulink模型的外部环境。,2.4.2 Flow Control,Scripts中可以使用各种流程控制,用于控制Simulink的调用或参数的设置。 主要的流程控制包括:if statementsswitch statementsfor loopswhile loops,2.4.2.1 if statements,判断结构,if rem(n,2) = 0M = odd_magi
26、c(n) elseif rem(n,4) = 0M = single_even_magic(n) elseM = double_even_magic(n) end,2.4.2.2 switch statements,选择结构,switch (rem(n,4)=0) + (rem(n,2)=0)case 0M = odd_magic(n)case 1M = single_even_magic(n)case 2M = double_even_magic(n)otherwiseerror(This is impossible)end,2.4.2.3 for loops,For循环,for n = 3
27、:32r(n) = rank(magic(n); end,2.4.2.4 while loops,While(条件)循环,while b-a eps*bx = (a+b)/2;fx = x3-2*x-5;if sign(fx) = sign(fa)a = x; fa = fx;elseb = x; fb = fx;end end,2.4.3 Functions(函数),Create a Function,Call a Function,function area=circle(r,s) %CIRCUIT this is keyword of the function % next is com
28、ment linesclf; %clear current figure window sita=0:pi/100:2*pi; y=r*exp(i*sita); plot(y,s); area=pi*r2;,circle(2,r) ans =12.5664x=circle(2,b) x=12.5664,Function最典型的应用S-Function,2.5 Simulink交互式仿真环境,Simulink,the most widely used software package in academia and industry for modeling and simulating dyn
29、amical systems. Interactive,you can change parameters and see what happens immediately. Simulink provides a graphical user interface (GUI). Simulink is practical,it is being used by thousands of engineers around the world.,Simulink的基本操作 Modeling Dynamic Systems Simulating Dynamic Systems Modeling an
30、d Simulating Discrete Systems,2.5.1 Modeling Dynamic Systems,Blocks(模块) States(状态) System Functions(仿真运行功能) Continuous and Discrete Blocks(连续和离散系统) Sample Time(采样时间),2.5.1.1 Blocks(模块),BlocksEach block represents an elementary dynamic system that produces an output. Simulink本身具有比较丰富的Block资源,应首先予以考虑。
31、,为方便设计,最好遵照下述命名规则: u:输入 x:(内部)状态 y:输出,2.5.1.2 States(内部状态),A block that has a state must store previous values of the state to compute its current state. Examples of the blocks that have STATES:Integrator,Unit Delay The Simulink Gain block is an example of a stateless block.,2.5.1.3 System Functions
32、(仿真运行功能),所谓System Functions是指仿真计算的三个循环步骤: 1、An output function(输出):Relates the systems outputs to its inputs, states, and time,y=f(t,x,u) 2、An update function(离散状态刷新): Relates the future values of the systems discrete states to the current time,inputs, and states,xdk+1= f(t,x,u) 3、A derivative funct
33、ion(连续状态刷新):Relates the derivatives of the continuous states to time and the present values of the states and inputs,xc= f(t,x,u),2.5.1.4 Continuous and Discrete Blocks,Continuous blocks respond continuously to continuously changing input. Discrete blocks respond to changes in input only at the bloc
34、ks sample time. Discrete blocks hold their output constant between successive sample time hits. Some blocks can be either continuous or discrete,depending on whether they are driven by continuous or discrete blocks.,2.5.2 Simulating Dynamic Systems,Model Initialization Phase Model Execution Phase Pr
35、ocessing at Each Time Step,2.5.2.1 Model Initialization Phase,During the initialization phase, Simulink: 1、Evaluates the models block parameter expressions to determine their values. 2、Flattens the model hierarchy by replacing virtual subsystems with the blocks that they contain. 3、Sorts the blocks
36、into the order in which they need to be executed during the execution phase. 4、Determines signal attributes. 5、Determines the sample times. 6、Allocates and initializes memory used to store the current values of each blocks states and outputs.,2.5.2.2 Model Execution Phase,Simulink successively compu
37、tes the states and outputs of the system at intervals from the simulation start time to the finish time, using information provided by the model. The successive time points at which the states and outputs are computed are called time steps. The length of time between steps is called the step size. S
38、imulink决定计算步长依据:用户的设定、离散模块或系统的采样时间、数值计算的误差。,2.5.2.3 Processing at Each Time Step,At each time step, Simulink1、Updates the outputs of the models blocks in sorted order.2、Updates the states of the models blocks in sorted order.3、Optionally checks for discontinuities in the continuous states of blocks.
39、4、Computes the time for the next time step.,2.5.3 Modeling and Simulating Discrete Systems,Discrete Blocks Sample Time Purely Discrete Systems Multirate Systems Determining Step Size for Discrete Systems Sample Time Propagation Invariant Constants Mixed Continuous and Discrete Systems,2.5.3.1 Discre
40、te Blocks,Each of the discrete blocks hasA built-in sampler at its inputA zero-order hold at its output,2.5.3.2 Sample Time,The Sample time parameter sets the sample time at which a discrete blocks states are updated. Specify an offset time (or skew) by specifying a two-element vector in this field.
41、 Cannot change the sample time of a block while a simulation is running.,2.5.3.3 Purely Discrete Systems & Multirate Systems,Purely Discrete Systems:系统中所有模块都按单一的采样率进行工作. Multirate Systems:系统中的模块采用不同的采样率进行工作.,2.5.3.4 Determining Step Size for Discrete Systems,The fundamental sample time of a discrete
42、 system is the greatest integral divisor of the systems actual sample times. You can direct Simulink to use either a fixed-step or a variable-step discrete solver to solve a discrete system.,2.5.3.5 Mixed Continuous and Discrete Systems,Mixed continuous and discrete systems are composed of both samp
43、led and continuous blocks. For most mixed continuous and discrete systems, the Runge-Kutta variable step methods, ode23 and ode45, are superior to the other methods in terms of efficiency and accuracy. Due to discontinuities associated with the sample and hold of the discrete blocks, the ode15s and
44、ode113 methods are not recommended for mixed continuous and discrete systems.,2.6 Solver(解算控制),The Solver pane allows you to:Set the simulation start and stop timesChoose the solver and specify its parametersSelect output options,2.7 Data Exchange Within Simulink and Workspace,Workspace I/O Pane-You
45、 can direct simulation output to workspace variables and get input and initial states from the workspace.,From Workspace Block & To Workspace Block,2.8 S-Function,What Is an S-Function?-An S-function is a computer language description of a Simulink block. When to Use an S-Function?-Adding new genera
46、l purpose blocks to Simulink-Adding blocks that represent hardware device drivers-Incorporating existing C code into a simulation-Describing a system as a mathematical set of equations-Using graphical animations (see the inverted pendulum demo, penddemo),Mathematics of Simulink Blocks,S-Function:实际上
47、是利用Matlab的编程语言直接设计模块的数学模型 S-Function的运行主要由三个环节组成:output,derivative,update,The stages of a simulation.,S-Function的运行实际上是一个循环调用的过程,Implementing S-Functions,An M-file S-function: sys,x0,str,ts=f(t,x,u,flag,p1,p2,.)f-S-functions namet-the current timex-the state vector of the S-function blocku-the block
48、s inputsflag-indicates a task to be performedp1, p2, -the blocks parameters.,S-Function的运行状态及标志,第三章 计算机仿真的建模,计算机仿真建模的含义和基本方法 Simulink模块库简介 Simulink基本模块的使用,3.1 计算机仿真建模的含义和基本方法,所谓建模,就是用数学表达式或更基本的物理模型单元的组合来模拟一个实际的系统。 数学表达式:更准确、直接; 物理模型单元的组合:十分直观,修改调试比较方便; Simulink仿真大多采用内部的模块库资源;,3.2 Simulink模块库简介,要求:初步了解Simulink模块的资源以及调用、连接和基本参数修改等操作。,3.3 Simulink基本模块的使用,具体要求:以下页示例电路熟悉所采用标准模块的参数及设置、仿真解算参数的设置、采样时间不同的设置方法。,第四章 计算机仿真实例,第五章 计算机仿真上机实践,范例实际1:S-Function的编写范例实践2:Simulink操作范例实践3: Simulink与Workspace的数据交换,范例实际1:S-Function的编写,学习要求:1、利用Timetwo学习S-Function的编写;2、利用sfcndemo_timestwo.mdl学习S-Function的调用及工作原理;,