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 电子设计自动化的主要应用领域,EDA的主要应用领域包括:集成电路设计模拟电路仿真动态系统的仿真E
6、DA主要软件及特点:Protel、Pspice、Saber、Matlab,1.6 计算机仿真的基本步骤,计算机仿真过程包括分析、建模、计算和验证;模型的调试和计算主要依靠仿真软件的设计和使用;验证过程非常重要;人为的干预和控制对仿真的效果有很大的影响;,系统和任务分析,建立系统的仿真模型,模型的调试与计算,仿真结果的验证,1.7 计算机仿真设计的原则,“分级仿真”原则:对系统的分析应根据任务的不同采用不同的仿真模型或算法,合理简化系统,突出问题的关键;“准确度与速度合理兼顾”原则:在仿真的不同阶段合理调整对准确度和速度的要求,提高仿真效率;“可信验证”原则:所有仿真结果应有可信的验证方法或依据
7、;,第二章 Matlab和Simulink简介,Matlab和Simulink简介MATLAB InstallationMATLAB DesktopArrayScripts and FunctionsSimulink交互式仿真环境SolverS-Function,2.0 Matlab和Simulink简介,Matlab是集数值计算、符号运算和图形处理功能于一身的超级科学计算语言;与其它计算语言相比,Matlab在功能、开放性和易学性等方面独占鳌头;Matlab的核心是数值计算,Simulink是建立系统框图和仿真的环境;,* Battery(s)V0 43 0 DC 15V; V1 39 0
8、DC 15V; *DISCHARGEV_0 41 0 PULSE(-15 15 196ms 10us 10us 20ms 200ms); *OFFSETV_1 5 0 PULSE(5 -5.7 150ms 10us 10us 50ms 200ms); *PIBIASV_2 1 0 PULSE(10 -10 190ms 2us 2us 10ms 200ms); * Misc.TRAN 10us 210ms 0s 10ms UIC,语句方式,框图方式,2.1 MATLAB Installation,The main Products of Matlab for modeling and simul
9、ating dynamical systems:MatlabSymbolic MathSimulinkControl System,2.2 MATLAB Desktop,Command Window(指令窗)Command History(历史指令窗)Launch Pad(交互界面分类目录窗)Help Browser(帮助浏览器)Current Directory Browser(当前目录浏览器)Workspace Browser(工作空间浏览器)Array Editor(内存数组浏览器)Editor/Debugger(M文件编辑/调试器),Launch Pad,Current Directo
10、ry 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 objectthe array(数组)。数组:一组数组成的长方阵列。Its typeIts dimensionsIts contentThe data asso
11、ciated with this arrayIf numeric,whether the variable is real or complexIf a structure,the number of fields and field names,数组与矩阵的区别,矩阵:作为一种变换或映射算子的体现,矩阵有着明确而严格的数学规则;数组:Matlab设置数组的目的主要是为了简化数据的管理和操作、使指令的形式更加自然、程序的运行更加有效;Matlab中对数组的计算实际上是对数组中每个元素平等地施加同样地操作;,2.3.2 数组在Matlab仿真计算中的应用,Matlab中数据的存放和处理均采用数组
12、;Matlab中的数组主要分为:Numeric Array,Character Array,Cell Array, Structure ArrayCharacter Array可用于仿真结果和过程的可视化;Structure Array可用于与Simulink交换信息,包括仿真参数的设定、结果的获取和处理等;,2.3.3 Numeric Array(数值数组),数值数组是Matlab中最基本地数组形式;数值数组分实数数组和复数数组;, A=1 2 3 4,5 6 7 8A = 1 2 3 4 5 6 7 8 A=1 2 3 4;5 6 7 8A = 1 2 3 4 5 6 7 8 A*4ans
13、 = 4 8 12 16 20 24 28 32,2.3.4 数组元素的标识,“全下标”标识“单下标”标识, A(4)ans = 6 A(1,4)ans = 4 A(2,3)=0; AA = 1 2 3 4 5 6 0 8,A = 1 2 3 4 5 6 7 8,单下标,全下标,冒号的使用,j:kis the same as j,j+1,.,kj:i:kis the same as j,j+i,j+2i, .,kA(:,j)is the j-th column of AA(i,:)is the i-th row of A,2.3.5 Character Array(字符串数组),字符串数组基本
14、元素是字符串。字符串数组可用于计算过程和结果的可视化。字符串数组的直接创建。利用串操作函数创建串数组。,A = The exmaple of the Character Arrays, A=1234 ;12345A =1234 12345, A=char(1234,12345)A =1234 12345,转换函数产生数码字符串,Int2str(整数数组转换为串数组);Num2str(非整数数组转换为串数组);,A=rand(2,4)A =0.9501 0.6068 0.8913 0.4565 0.2311 0.4860 0.7621 0.0185B=num2str(A,2),C=int2str
15、(A)B =0.95 0.61 0.89 0.46 0.23 0.49 0.76 0.019C =1 1 1 0 0 0 1 0class(B),class(C)ans =charans =char,clear%清除内存中地所有变a=2,w=3;%t=0:0.01:10;%取自变量采样数组y=exp(-a*t).*sin(w*t);%计算函数值,产生函数数组y_max,i_max=max(y);%找最大值的位置t_text=t=,num2str(t(i_max);%生成最大值点的横坐标字符串y_text=y=,num2str(y_max);%生成最大值点的众坐标字符串max_text=char
16、(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);%标注最大值点text(t(i_max)+0.3,y_max+0.05,max_text);%标注最大值点的字符串title(tit),xlabel(t),ylabel(y),hold off%标注图名、横坐标名、纵坐标名,字符串数组用于
17、计算结果的可视化实例,字符串数组用于计算结果的可视化实例,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 indexing(元胞外标识):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
18、 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;A1,2 = Anne Smith;A2,1 = 3+7i;A2,2 = -pi:pi/10:pi;,2.3.6.2 Obtaining Data from Cell Arrays,调取元胞数据分:元胞的调取和元胞内容的调取;元胞地标识分:元胞外标识和元胞内标识;,A1,1
19、ans =1 4 3 0 5 8 7 2 9A1,1(1,2)ans = 4,A(1,1)=1 2 3;4 5 6;A(1,1)=2x3 doubleA1,1=1 2 3 4 5 6,2.3.7 Structure Array(构架数组),The elements of a Structure Array are Structures.Structures are MATLAB arrays with data containers called fields. The fields of a structure can contain any kind of data. Structure
20、Array的基本组分是架构( Structure) ,在Structure以下划分了域(Field),域可用于存放任何形式的数据。,2.3.7.1 Structure Array的基本结构,patient.name = John Doe;patient.billing = 127.00;patient.test = 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;pati
21、ent(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; 118 118 119; 172 170 169)patient = 1x2 struct array with fields:namebillingtest,2.3.7.3 Accessing Data in Structure Arrays,架构数组的存取:架构域内容,mypatients
22、= patient(1:2)1x2 struct array with fields: name billing test,mypatients(1)ans = name: John Doe billing: 127 test: 3x3 double,str = patient(2).namestr =Ann Lane,patient(2).testans = 68 70 68 118 118 119 172 170 169,2.4 Scripts and Functions,Scripts(脚本文件), which do not accept input arguments or retur
23、n output arguments. They operate on data in the workspace.Functions(函数文件), which can accept input arguments and return output arguments. Internal variables 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
24、 existing data in the workspace, or they can create new data on which to operate. Although scripts do not return output arguments, any variables that they create remain in the workspace, to be used in subsequent computations.Scripts在使用Simulink时的主要作用:作为调用Simulink模型的外部环境。,2.4.2 Flow Control,Scripts中可以
25、使用各种流程控制,用于控制Simulink的调用或参数的设置。主要的流程控制包括:if statementsswitch statementsfor loopswhile loops,2.4.2.1 if statements,判断结构,if rem(n,2) = 0 M = odd_magic(n)elseif rem(n,4) = 0 M = single_even_magic(n)else M = double_even_magic(n)end,2.4.2.2 switch statements,选择结构,switch (rem(n,4)=0) + (rem(n,2)=0) case 0
26、 M = odd_magic(n) case 1 M = single_even_magic(n) case 2 M = double_even_magic(n) otherwise error(This is impossible) end,2.4.2.3 for loops,For循环,for n = 3:32 r(n) = rank(magic(n);end,2.4.2.4 while loops,While(条件)循环,while b-a eps*b x = (a+b)/2; fx = x3-2*x-5; if sign(fx) = sign(fa) a = x; fa = fx; e
27、lse b = x; fb = fx; endend,2.4.3 Functions(函数),Create a Function,Call a Function,function area=circle(r,s)%CIRCUIT this is keyword of the function% next is comment linesclf; %clear current figure windowsita=0:pi/100:2*pi;y=r*exp(i*sita);plot(y,s);area=pi*r2;,circle(2,r)ans = 12.5664 x=circle(2,b)x=
28、12.5664,Function最典型的应用S-Function,2.5 Simulink交互式仿真环境,Simulink,the most widely used software package in academia and industry for modeling and simulating dynamical systems.Interactive,you can change parameters and see what happens immediately.Simulink provides a graphical user interface (GUI).Simulin
29、k is practical,it is being used by thousands of engineers around the world.,Simulink的基本操作Modeling Dynamic SystemsSimulating Dynamic SystemsModeling and Simulating Discrete Systems,2.5.1 Modeling Dynamic Systems,Blocks(模块)States(状态)System Functions(仿真运行功能)Continuous and Discrete Blocks(连续和离散系统)Sample
30、 Time(采样时间),2.5.1.1 Blocks(模块),BlocksEach block represents an elementary dynamic system that produces an output.Simulink本身具有比较丰富的Block资源,应首先予以考虑。,为方便设计,最好遵照下述命名规则: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.Examp
31、les of the blocks that have STATES:Integrator,Unit DelayThe Simulink Gain block is an example of a stateless block.,2.5.1.3 System Functions(仿真运行功能),所谓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(离散状
32、态刷新): 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 function(连续状态刷新):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 B
33、locks,Continuous blocks respond continuously to continuously changing input.Discrete blocks respond to changes in input only at the blocks sample time. Discrete blocks hold their output constant between successive sample time hits.Some blocks can be either continuous or discrete,depending on whether
34、 they are driven by continuous or discrete blocks.,2.5.2 Simulating Dynamic Systems,Model Initialization PhaseModel Execution PhaseProcessing 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
35、 their values.2、Flattens the model hierarchy by replacing virtual subsystems with the blocks that they contain.3、Sorts the blocks 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 memor
36、y used to store the current values of each blocks states and outputs.,2.5.2.2 Model Execution Phase,Simulink successively computes 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 a
37、t which the states and outputs are computed are called time steps. The length of time between steps is called the step size.Simulink决定计算步长依据:用户的设定、离散模块或系统的采样时间、数值计算的误差。,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、Update
38、s the states of the models blocks in sorted order.3、Optionally checks for discontinuities in the continuous states of blocks.4、Computes the time for the next time step.,2.5.3 Modeling and Simulating Discrete Systems,Discrete BlocksSample TimePurely Discrete SystemsMultirate SystemsDetermining Step S
39、ize for Discrete SystemsSample Time PropagationInvariant ConstantsMixed Continuous and Discrete Systems,2.5.3.1 Discrete 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
40、discrete blocks states are updated.Specify an offset time (or skew) by specifying a two-element vector in this field.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 Syste
41、ms:系统中的模块采用不同的采样率进行工作.,2.5.3.4 Determining Step Size for Discrete Systems,The fundamental sample time of a discrete 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 sys
42、tem.,2.5.3.5 Mixed Continuous and Discrete Systems,Mixed continuous and discrete systems are composed of both sampled 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
43、 and accuracy. Due to discontinuities associated with the sample and hold of the discrete blocks, the ode15s and 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 s
44、pecify its parametersSelect output options,2.7 Data Exchange Within Simulink and Workspace,Workspace I/O Pane-You 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?-A
45、n S-function is a computer language description of a Simulink block.When to Use an S-Function?-Adding new general 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-Usi
46、ng graphical animations (see the inverted pendulum demo, penddemo),Mathematics of Simulink Blocks,S-Function:实际上是利用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 blocks inputsflag-indicates a task to be performedp1, p2, -the blocks parameters.,