830 likes | 951 Views
在抗击“非典”期间,学校课程安排受到很大的影响,因此对授课的方式和内容作了相应的修改:. 1 、取消第四章,将仿真实例的学习排入前三章,要求同学通过自己的上机实践学习 Matlab 和 Simulink 的基本操作。 2 、原上机实践部分的内容安排为几个范例的学习。同学们应充分利用范例对所涉及的关键内容进行实践。. 计 算 机 仿 真 技 术. 童 亦 斌 2003.4. 系统的分析方法. 系统:客观世界中相互作用的实体所构成的具有特定功能的整体。 为了定量分析系统,可将其本身的特点和内部的相互关系抽象出来,构造出系统的模型。对系统的分析变成对系统模型的分析。
E N D
在抗击“非典”期间,学校课程安排受到很大的影响,因此对授课的方式和内容作了相应的修改:在抗击“非典”期间,学校课程安排受到很大的影响,因此对授课的方式和内容作了相应的修改: • 1、取消第四章,将仿真实例的学习排入前三章,要求同学通过自己的上机实践学习Matlab和Simulink的基本操作。 • 2、原上机实践部分的内容安排为几个范例的学习。同学们应充分利用范例对所涉及的关键内容进行实践。
计 算 机 仿 真 技 术 童 亦 斌 2003.4
系统的分析方法 • 系统:客观世界中相互作用的实体所构成的具有特定功能的整体。 • 为了定量分析系统,可将其本身的特点和内部的相互关系抽象出来,构造出系统的模型。对系统的分析变成对系统模型的分析。 • 系统模型:分物理模型和数学模型,由于计算机技术的发展,数学模型的应用越来越广泛。 • 系统的数学模型:描述系统动态特性的数学表达式。 • 根据系统的性质和数学分析工具可分为:连续系统、离散时间系统、离散事件系统、混合系统等。
系统的仿真分析方法 • 仿真技术:用模拟的方法模拟实际系统的表现。 • 仿真技术的基本方法:数学模型仿真、物理模型仿真、硬件仿真、操作仿真、虚拟现实。 • 系统仿真:根据系统数学模型研究系统性能。 • 计算机仿真:利用计算机完成对系统的仿真。本课程特指利用计算机、根据系统的数学模型对电路系统进行仿真。
计算机仿真技术讲授内容 • 计算机仿真技术概述 • Matlab和Simulink简介 • 计算机仿真的建模 • 计算机仿真实例 • 计算机仿真上机实践
本课程的学习方法、作业和考试 • 学习方法:特别强调直接在计算机进行实践,强调学习利用Matlab各种资源解决问题; • 作业:分别关于Matlab的基本使用、Simulink的建模、S函数和Simulink结果的调用; • 考试:建立一个交流异步电机变频启动的计算机仿真模型,对交流异步电机的变频启动过程进行观察; 交流电机变频系统框图
考试的基本要求和条件 • 基本要求-利用搭建的模型研究变频启动过程中电机电流的变化情况; • 条件:电源电压直流600V、电机为Simulink默认的交流异步电机、电机空载、采用理想IGBT逆变器桥、开关频率固定为2500Hz、输出频率从5Hz变化到50Hz、启动时间可调;
第一章 计算机仿真技术概述 • 为什么要进行计算机仿真? • 计算机仿真能完成什么任务? • 计算机仿真技术的关键 • 仿真软件的发展状况 • 电子设计自动化的主要应用领域 • 计算机仿真的基本步骤 • 计算机仿真设计的原则
§1.1 为什么要进行计算机仿真? • 实际系统的理论分析往往非常复杂,传统的分析方法几乎不可能完成; • 很多时候几乎不可能或很难进行真实实验的分析和验证; • 实验的成本高、效率低、周期长;
通过计算机仿真我们能够: • 提高设计效率、缩短设计周期; • 准确、全面地模拟和分析系统; • 提高产品和技术的设计质量; • 降低设计成本; • 充分发挥设计人员的创造性; • 降低干扰因素的影响,集中注意于关键问题; • 优化系统和参数的设计
§1.2 计算机仿真能完成什么任务? • 动态系统的仿真分析; • 参数的优化; • 极限参数的设计; • 环境参数对系统性能的影响; • 突发或偶发事件的模拟; • 电子设计自动化(Electronic Design Automation)
§1.3 计算机仿真技术的关键 • 数值计算算法; • 电路数学模型建立的模块化; • 不同应用软件间的数据共享; • 仿真计算的效率和准确性; • 混合系统的建模和仿真; • 仿真结果的可视化;
§1.4 仿真软件的发展状况 早期仿真语言和程序 早期数学软件包 • 现代仿真软件的特点:功能强大的数学软件包作为计算引擎、交互式的编程方式、友好的图形界面、丰富的模型资源和良好的程序接口等; 现代的仿真软件
§1.5 电子设计自动化的主要应用领域 • EDA的主要应用领域包括: -集成电路设计 -模拟电路仿真 -动态系统的仿真 • EDA主要软件及特点: Protel、Pspice、Saber、Matlab
§1.6 计算机仿真的基本步骤 系统和任务分析 • 计算机仿真过程包括分析、建模、计算和验证; • 模型的调试和计算主要依靠仿真软件的设计和使用; • 验证过程非常重要; • 人为的干预和控制对仿真的效果有很大的影响; 建立系统的仿真模型 模型的调试与计算 仿真结果的验证
§1.7 计算机仿真设计的原则 • “分级仿真”原则:对系统的分析应根据任务的不同采用不同的仿真模型或算法,合理简化系统,突出问题的关键; • “准确度与速度合理兼顾”原则:在仿真的不同阶段合理调整对准确度和速度的要求,提高仿真效率; • “可信验证”原则:所有仿真结果应有可信的验证方法或依据;
第二章 Matlab和Simulink简介 • Matlab和Simulink简介 • MATLAB Installation • MATLAB Desktop • Array • Scripts and Functions • Simulink交互式仿真环境 • Solver • S-Function
§2.0 Matlab和Simulink简介 * 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 语句方式 • Matlab是集数值计算、符号运算和图形处理功能于一身的超级科学计算语言; • 与其它计算语言相比,Matlab在功能、开放性和易学性等方面独占鳌头; • Matlab的核心是数值计算,Simulink是建立系统框图和仿真的环境; 框图方式
§2.1 MATLAB Installation • The main Products of Matlab for modeling and simulating dynamical systems: -Matlab -Symbolic Math -Simulink -Control System
§2.2 MATLAB Desktop • Command Window(指令窗) • Command History(历史指令窗) • Launch Pad(交互界面分类目录窗) • Help Browser(帮助浏览器) • Current Directory Browser(当前目录浏览器) • Workspace Browser(工作空间浏览器) • Array Editor(内存数组浏览器) • Editor/Debugger(M文件编辑/调试器)
Current Directory Browser Command Window Launch Pad
§2.3 Array • Array(数组)的基本概念 • 数组在Matlab仿真计算中的应用 • Numeric Array(数值数组) • 数值元素的标识 • Character Array(字符串数组) • Cell Array(元胞数组) • Structure Array(架构数组)
§2.3.1 Array(数组)的基本概念 • MATLAB works with only a single object-the array(数组)。 • 数组:一组数组成的长方阵列。 Its type Its dimensions Its content The data associated with this array If numeric,whether the variable is real or complex If a structure,the number of fields and field names
数组与矩阵的区别 • 矩阵:作为一种变换或映射算子的体现,矩阵有着明确而严格的数学规则; • 数组:Matlab设置数组的目的主要是为了简化数据的管理和操作、使指令的形式更加自然、程序的运行更加有效; • Matlab中对数组的计算实际上是对数组中每个元素平等地施加同样地操作;
§2.3.2 数组在Matlab仿真计算中的应用 • Matlab中数据的存放和处理均采用数组; • Matlab中的数组主要分为: Numeric Array,Character Array,Cell Array, Structure Array • Character Array可用于仿真结果和过程的可视化; • Structure Array可用于与Simulink交换信息,包括仿真参数的设定、结果的获取和处理等;
§2.3.3 Numeric Array(数值数组) >> 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 4 5 6 7 8 >> A*4 ans = 4 8 12 16 20 24 28 32 • 数值数组是Matlab中最基本地数组形式; • 数值数组分实数数组和复数数组;
§2.3.4 数组元素的标识 • “全下标”标识 • “单下标”标识 >> A(4) ans = 6 >> A(1,4) ans = 4 >> A(2,3)=0; >> A A = 1 2 3 4 5 6 0 8 单下标 全下标 A = 1 2 3 4 5 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 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
转换函数产生数码字符串 A=rand(2,4) A =0.9501 0.6068 0.8913 0.4565 0.2311 0.4860 0.7621 0.0185 B=num2str(A,2),C=int2str(A) B =0.95 0.61 0.89 0.46 0.23 0.49 0.76 0.019 C =1 1 1 0 0 0 1 0 class(B),class(C) ans =char ans =char • Int2str(整数数组转换为串数组); • Num2str(非整数数组转换为串数组);
字符串数组用于计算结果的可视化实例 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('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 %标注图名、横坐标名、纵坐标名
§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}; Content indexin(元胞内标识): 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]
§2.3.6.2 Obtaining Data from Cell Arrays A{1,1} ans =1 4 3 0 5 8 7 2 9 A{1,1}(1,2) ans = 4 • 调取元胞数据分:元胞的调取和元胞内容的调取; • 元胞地标识分:元胞外标识和元胞内标识; A(1,1)={[1 2 3;4 5 6]}; A(1,1)=[2x3 double] A{1,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 Array的基本组分是架构( Structure) ,在Structure以下划分了域(Field),域可用于存放任何形式的数据。
§2.3.7.1 Structure Array的基本结构 A Structure Array A Structure patient.name = 'John Doe'; patient.billing = 127.00; patient.test = [79 75 73; 180 178 177.5; 220 210 205];
§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; 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: name billing test mypatients(1) ans = name: 'John Doe' billing: 127 test: [3x3 double] str = patient(2).name str = Ann Lane patient(2).test ans = 68 70 68 118 118 119 172 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 variables are local to the function.
§2.4.1 Scripts • When you invoke a script, Matlabsimply 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 that they create remain in the workspace, to be used in subsequent computations. • Scripts在使用Simulink时的主要作用:作为调用Simulink模型的外部环境。
§2.4.2 Flow Control • Scripts中可以使用各种流程控制,用于控制Simulink的调用或参数的设置。 • 主要的流程控制包括: -if statements -switch statements -for loops -while 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 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 b-a > eps*b x = (a+b)/2; fx = x^3-2*x-5; if sign(fx) == sign(fa) a = x; fa = fx; else b = x; fb = fx; end end • While(条件)循环
Create a Function Call a Function §2.4.3 Functions(函数) function area=circle(r,s) %CIRCUIT this is keyword of the function % next is comment lines clf; %clear current figure window sita=0:pi/100:2*pi; y=r*exp(i*sita); plot(y,s); area=pi*r^2; circle(2,'r') ans = 12.5664 x=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 dynamical 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 and Simulating Discrete Systems