1 / 109

MATLAB  简介 Introduction to MATLAB

MATLAB  简介 Introduction to MATLAB. 门爱东教授. 主题概述. 1 Matlab 开始 历史、起源、功能 MATLAB 窗 a) 命令窗 b) 图形窗 c) 演示窗 d) 编辑窗 2 Matlab 基础 3 Matlab 程序 4 Matlab 扩展 5 Matlab 进阶. Matlab 开始. MATLAB 开始: 历史和起源. 最初用 Fortran 编写: Cleve Moler and others 几年的开发

hop
Télécharger la présentation

MATLAB  简介 Introduction to MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MATLAB简介 Introduction to MATLAB 门爱东教授

  2. 主题概述 1 Matlab 开始 • 历史、起源、功能 • MATLAB 窗 a) 命令窗 b) 图形窗 c) 演示窗 d) 编辑窗 2 Matlab 基础 3 Matlab 程序 4 Matlab 扩展 5 Matlab 进阶

  3. Matlab 开始

  4. MATLAB 开始:历史和起源 • 最初用 Fortran 编写: • Cleve Moler and others 几年的开发 • 对LINPACK/EISPACK 库的交互访问:可信赖的数值和矩阵计算技术 • 命名 MATrix LABoratory = MATLAB • 早期发行 • 限制的脚本 • 行打印图形 • 最初针对大型机,随后发行 Dos 和 MAC 版本 • 现在的商业版本 The MathWorks Inc 公司开发. • Core 采用 C 语言编写,出色的可扩展性 • 运行于多个平台,优化的接口和计算 • 专业版和学生版 • 扩展包括 “SIMULINK” 和各种 “TOOLBOXES”

  5. MATLAB 开始:功能 • Matlab 是什么? • “技术计算环境,用于高性能数值计算和可视化”(官方定义) • Matlab 能做什么? • 为线性代数和科学计算提供对计算工具的“无编译”访问。 • 利用复数数据的阵列和矩阵作为基本的变量类型(也有结构和多维性能、字符数据、目标对象) • 对于计算的可视化解释,无缝隙集成了 2D 和 3D 图形。 • 对于特定的应用和学科需要,允许和鼓励用户扩展(M-files 和 Toolboxs)

  6. MatLab 开始:窗 (Windows) 命令(提示符处键入) Matlab 输出 Matlab提示符(>>)和光标 ( | ) • 命令窗 Command Window • 当首次进入 Matlab 时,命令窗是激活的。 • 在提示符 >> 处输入交互命令或表达式,例 help, sqrt(9) • 结果自动显示 (ans = )

  7. MatLab 开始:窗 (Windows) • 命令窗 Command Window • MATLAB 解释器,即时计算/反馈/评价 » 3.5*6.2 ans = 21.7000 » » exp(1j*3*pi/4) ans = -0.7071 + 0.7071i »

  8. MatLab 开始:窗 (Windows) • 工作空间窗 Workspace Window • 用于显示目前变量的尺寸、类型(一般是双精度的),和所占内存大小。

  9. MatLab 开始:窗 (Windows) • 图形窗 Graphics Window • 用于显示绘图、图形和图象 • 例如,键入命令 >> plot([1,2,4,9,16],[1,2,3,4,5]) • Matlab 画出结果如下: • 可以编辑图形、坐标轴的特性

  10. MatLab 开始:窗 (Windows) • 演示窗 Demo Window • 在命令窗中键入 Demo 激活演示窗 • 从左边窗口所列的主题中选择所需的演示

  11. MatLab 开始:窗 (Windows) • 编辑窗 Edit Window • 用于生成和修改 M-files (Matlab 程序脚本) • 命令窗提示符处键入 edit • 命令窗 File --> New or Open 新的或旧的 M 文件. • 工具栏

  12. MatLab 开始:窗 (Windows) • Help 窗 • 通过下拉 help 菜单访问 -- 选择 Matlab help or 工具栏中选择 “?” • 选择查询的主题 • 在命令窗中键入 help 激活“帮助”的非交互版本 • 在下拉 help 菜单中选择 help 的 HTML 版本(安装时需选择)

  13. 主题概述 1 Matlab 开始 2 Matlab 基础 • 基本的数据结构——矩阵 • 工作空间管理 • 特殊字符 • 获得帮助 3 Matlab 程序 4 Matlab 扩展 5 Matlab 进阶

  14. Matlab 基础:矩阵数据结构 • 矩阵是 MATLAB 的基础 • 矩阵数据结构 • Matlab 中所有的变量都表示为矩阵 • 标量:1×1 矩阵 • 向量:n×1 或 1×n 矩阵 • 矩阵剖析 • 元素按行列排列 • 特定的元素可以通过它们的行列位置来引用, 如 a(4,2) = 7 a=[2 0.5;-4 1;3 2;1 7] Row 行 Column 列 • 方阵:行列数相等的矩阵

  15. Matlab 基础:矩阵数据结构 % 矩阵输入 » A=[1 3 5; 6 9 2; 4 8 7] A = 1 3 5 6 9 2 4 8 7 » det(A) %求矩阵 A 的行列式 ans = 5 » A^2+3*A %矩阵运算 ans = 42 79 61 86 142 68 92 164 106 MATrix LABoratory -- 矩阵数据 --线性代数规则

  16. Matlab 基础:矩阵数据结构 • 工作空间 (Workspace) clear 函数从工作空间中清除变量(函数) clear t=0:5; T=0:0.5:5; who » Your variables are: T t » whos Name Size Bytes Class T 1x11 88 double array t 1x6 48 double array Grand total is 17 elements using 136 bytes • 非常快速的键入; » a=exp(1j*t); whos

  17. MatLab 基础:工作空间管理 • 管理命令:在命令窗中键入下列命令 (有些任务也可以通过 File 下拉菜单访问): P.S. 这些命令都有许多选项。例如matlab.mat只是缺省文件名。

  18. MatLab 基础:命令行编辑器 • Single-Stroke Editing Capabilities • Up-Arrow, Ctrl-P Recall previous line • Down-Arrow, Ctrl-N Recall next line • Left-Arrow, Ctrl-B Move back (left) one character • Right-Arrow, Ctrl-F Move forward (right) one character • Ctrl-Left-Arrow, Ctrl-L Move left one word • Ctrl-Right-Arrow, Ctrl-R Move right one word • Home, Ctrl-A Move to beginning of command line • End, Ctrl-E Move to end of command line • Esc, Ctrl-U Clear command line • Del, Ctrl-D Delete character at cursor • Backspace Delete character left of cursor • Ctrl-K Delete to end of line • “Smart Recall” also available • Can modify editor behavior via the command cedit

  19. MatLab 基础:特殊字符 • 分号 ; • 禁止回应 echo t=[0:5]; t; t » t = 0 1 2 3 4 5 • 矩阵行分隔 » B=[1 4 3; 2 9 -5] B = 1 4 3 2 9 -5

  20. S 第 4 行的所 有元素 MatLab 基础:特殊字符 • 冒号 : • 例举中项目的分隔:用于产生包含递增或递减数字序列的向量 » x=[0:2:12] x = 0 2 4 6 8 10 12 • 代表矩阵某行或某列的所有元素

  21. MatLab 基础:特殊字符 • 冒号: 从先前定义的矩阵中选择一个子矩阵,假设 命令 >> C1=C(:, 2:3) >> C2=C(3:4,1:2) 结果如下:

  22. MatLab 基础:特殊字符 • 单引号 ' • 转置运算(注意: 共轭): 矩阵 A=A’ 的转置表示行列互换,得到新的矩阵 A’ » m=[1+j; 2-3*j]' m = 1.0000 - 1.0000i 2.0000 + 3.0000i • 字符串声明 » wierd=['hello' 'there'] wierd = hellothere

  23. MatLab 基础:特殊字符 • 周期“ .” • 修改一元或二进制操作(注意:下例中取消共轭) » n=[1+j; 2-3*j].' n = 1.0000 + 1.0000i 2.0000 - 3.0000i • 阵列运算:乘 .* 除 ./ .^ 等,具体的后面再讲。 • 空矩阵:没有包含任何元素的矩阵,例如

  24. MatLab 基础:特殊字符 • 逗号 , • 在一行中分隔多个命令,没有输出抑制。 » a=3; b=1 b = 1 » a=3 b=1 ??? a=3 b | Missing operator, comma, or semi-colon. » a=3, b=1 a = 3 b = 1 • 百分比符号 % • 此行中其后的所有的命令都忽略(用于注释) » a=3 % b=1 a = 3

  25. MatLab 基础:获得帮助 • 几种方法 • 命令行 » help HELP topics: matlab\general - General purpose commands. : matlab\funfun - Function functions and ODE solvers. : toolbox\signal - Signal Processing Toolbox. For more help on directory/topic, type "help topic". • 下拉菜单Help • 注意: 多种方式,包括 HTML and PDF

  26. MatLab 基础:获得帮助 • lookfor命令将在大多数 help 主题中进行关键词搜索 (用 Ctrl-C终止搜索,可能需要一定时间) • 注意在命令行 help 描述中命令 都采用大写字母 » help eps EPS Floating point relative accuracy. EPS returns the distance from 1.0 to the next largest floating point number. EPS is used as a default tolerance by PINV and RANK, as well as several other MATLAB functions. See also REALMAX, REALMIN. »EPS ??? Undefined variable or capitalized internal function EPS; Caps Lock may be on. » eps eps = 2.2204e-016

  27. Matlab 基础:举例 • MATrix LABoratory » a=[1 2 3]; a*a' ans = 14 » a=[1 2 3]; a*a ??? Error using ==> * Inner matrix dimensions must agree. 基于矩阵的有效的计算 » a=[1 2 3]; a.*a ans = 1 4 9 » sum(a) ans = 6 » b=a.*exp(a.^2) b = 1.0e+004 * 0.0003 0.0109 2.4309 » b(1) ans = 2.7183

  28. Matlab 基础:练习 • 命令t=[0:0.001:10];在 0 到 10 秒之间产生毫秒级的时间索引。阵列 t 占用多大存储 ? • 一些特殊的 MATLAB 变量是永久的,例如ans, eps, flops,i, Inf, j, NaN, pi。通过 help,练习它们的使用方法。 • MATLAB 具有强大的矩阵元素索引能力,运行下述命令: x=[0:0.1:1], y=x(11:-1:1) z=x(1:2:length(x)), w=x(1:3:end) A=[1 0 0; 0 2 0; 0 0 3], B=A(:,3:-1:1)

  29. 主题概述 1 Matlab 开始 2 Matlab 基础 3 Matlab 程序 4 Matlab 扩展 5 Matlab 进阶 • 变量:变量名称、赋值和分配 • 函数: • 基本计算、结果显示 • 阵列运算 vs.矩阵运算 • 基本数学函数和数据分析函数 • 多项式定义和处理 • 绘图:2D和3D • 程序流程控制: • 关系和逻辑运算 • 块结构化的编程(If、For、While) • 程序文本: • 脚本文件 M-Files • 函数文件 Function Files

  30. MATLAB 程序:变量命名 • MATLAB 区分大小写! (注意在线 Help 中都采用大写字母)。因此,A 和 a 表示不同的变量。 • 变量名字必须以字母开头 A-Z a-z • 每个名字由 31个字母、数字和下划线等唯一组成 • 使用whoor whos检测当前工作空间中变量 • 使用which命令诊断是否存在名字冲突 » which det det is a built-in function. » det=125.2; » which det det is a variable. » clear det

  31. MATLAB 程序:变量赋值 • 变量赋值:直接的显式列出 • 数值放在方括号里 >> A=[3,5]; • 数值通常按行输入,行之间用分号隔开 >> C=[-1,0,0;1,-1,0;0,0,2]; • 省略最后的分号,将使 Matlab 自动显示矩阵值 自动输出 • 每行可分别列出

  32. MATLAB 程序:变量赋值 • 长的行数据可以在逗号后加省略号(3个点),得以在下一行继续。 >> F=[1, 52, 64, 197, 42, -42, … 55, 82, 22, 109] • 通过引用特定的位置可以单独改变某个矩阵元素 • 如果 S=[5, 6, 4] • 用命令 S(2)=8 把矩阵 S 的第二个元素的数值由 6 改为 8. • 可以引用先前定义的矩阵,重新定义一个新的矩阵。例如,如果 S=[5,6,4],我们定义一个新的矩阵: S

  33. MATLAB 程序:变量赋值 • 用户输入 User Input • Input 命令显示一个字符串,并等待用户响应 • 输入的数值存为特定的变量 • 矩阵必须使用正确的语法从键盘输入。 • 注意这个命令在运行 Matlab 脚本时非常有用 user response MATLAB response

  34. MATLAB 程序:变量的保存和调入 • .mat 文件是 save命令的缺省格式 • 紧凑格式,节省磁盘空间 • 不能输出给其它应用软件 • save命令的通用形式 • save <fname> <vlist> -option1 -option2…, etc.例如: • ASCII(text)文件可以用字处理软件(wordpad or NotePad)浏览、修改或准备。 • ASCII 文件中矩阵的每一行包含一个隔离 line。

  35. MATLAB 程序:简单的变量分配 • 标量Scalars » clear; a=1; b=2.0; c=1+j*2; d='Hello'; whos Name Size Bytes Class a 1x1 8 double array b 1x1 8 double array c 1x1 16 double array (complex) d 1x5 10 char array Grand total is 8 elements using 42 bytes

  36. MATLAB 程序:简单的变量分配 • 阵列Arrays (实数、复数、字符)==>(double, complex, char) » clear; a=0:0.1:1; b=[0:0.1:1]; c=[0:0.1:1]'; » d=[0 j 1]; e=[1 'abc' 3]; whos Name Size Bytes Class a 1x11 88 double array b 1x11 88 double array c 11x1 88 double array d 1x3 48 double array (complex) e 1x5 10 char array Grand total is 41 elements using 322 bytes What is the content of variable e ?

  37. MATLAB 程序:简单的变量分配 • 矩阵 (实数、复数) » clear; a=[0 1; 5 9]; b=[j 1; 0 2]; » c=[a b]; d=[a; b]; e=[]; whos Name Size Bytes Class a 2x2 32 double array b 2x2 64 double array (complex) c 2x4 128 double array (complex) d 4x2 128 double array (complex) e 0x0 0 double array Grand total is 24 elements using 352 bytes 利用 “空矩阵 [ ]” 分配,例如上述的 e 变量,用于各种初始化任务,例如预先分配内存空间。

  38. MATLAB 程序:简单的变量分配 • 矩阵 (稀疏和字符) » clear; a=[1 0; 0 1]; b=sparse(a); » c=['who' 'is'; 'that?']; whos Name Size Bytes Class a 2x2 32 double array b 2x2 36 sparse array c 2x5 20 char array Grand total is 16 elements using 88 bytes » c c = whois that? » c=['who' 'is'; 'there?'] ??? All rows in the bracketed expression must have the same number of columns.

  39. MATLAB 程序:变量分配 • 多维阵列 • 范例:“same-size 矩阵的集合” » clear; A=zeros(2,2,3); whos Name Size Bytes Class A 2x2x3 96 double array Grand total is 12 elements using 96 bytes » A(:,:,2)=eye(2) A(:,:,1) = 0 0 0 0 A(:,:,2) = 1 0 0 1 A(:,:,3) = 0 0 0 0

  40. MATLAB 程序:变量分配 • 单元阵列 Cell Arrays • 范例: “various elements 各种元素的集合” » clear; A={'stuff' [1 2;3 4] [0:4]}; whos Name Size Bytes Class A 1x3 358 cell array Grand total is 17 elements using 358 bytes » A A = 'stuff' [2x2 double] [1x5 double] » A(2) % The parentheses provide a cell ans = [2x2 double] » A{2} % Curly brackets extract what’s underneath A = 1 2 3 4 The use of curly brackets { } is new in Version 5.

  41. MATLAB 程序:变量分配 • 结构 Structures • 范例: “结构化的数据集合” » mystuff.name='Jerry'; mystuff.age=35; » mystuff.family={'Chris' 'Margo' 'Max'} mystuff = name: 'Jerry' age: 34 family: {1x3 cell} » whos Name Size Bytes Class mystuff 1x1 692 struct array Grand total is 25 elements using 692 bytes » help struct STRUCT Create or convert to structure array. ... See also CLASS, CELL, GETFIELD, SETFIELD, RMFIELD, FIELDNAMES. 也能产生结构“阵列” 注意: MATLAB Objects are structures with methods!

  42. MATLAB 函数:标量运算和特殊的标量 • Matlab 标量计算服从标准的代数优先顺序(运算顺序) • 两个标量 a 和 b 之间的算术运算: • 特殊的标量值: Matlab 预定义的一些数值。 • 在 Matlab 中重新定义这些数值将引起不可预测的结果。

  43. MATLAB 函数:阵列运算 vs 矩阵运算 • 当计算涉及非标量变量时,必须注意维数和说明 • 有两个基本说明: • Array Operations 阵列运算 (元素对元素) • Matrix/Vector 矩阵运算 • 两者都有用,用户必须仔细编写计算,并从 Matlab 中选择合适的函数。

  44. MATLAB 函数:阵列运算 • 阵列运算:两个相同大小的矩阵的元素对元素运算 • 特别注意:阵列(Array)运算和矩阵(Matrix)运算不相同! a = 3 4 2 2 1 5 a.*b = 15 24 14 16 9 50 b = 5 6 7 8 9 10 » x=0:1:5;pofx=x^2+2*x+1 ??? Error using ==> ^ Matrix must be square. » pofx=x.^2+2*x+1 pofx = 1 4 9 16 25 36

  45. MATLAB 函数:阵列运算 • 关键是“.” 和非矩阵函数 » t=linspace(0,2*pi,5) t = 0 1.5708 3.1416 4.712 6.2832 » cos(t).^2+sin(t).^2 ans = 1 1 1 1 1

  46. MATLAB 函数:矩阵运算 • 不同于前面讨论的阵列 (arry) 运算 • 某些矩阵运算只能在某些条件下方能执行:如果矩阵是方阵,或如果两个输入矩阵是一致的。

  47. MATLAB 函数:矩阵运算 • 行/列 内部维数必须一致 »a=[2 3; 5 1]; b=[1 2]; c=a*b ??? Error using ==> * Inner matrix dimensions must agree. »c=a*b' c = 8 7 • 选择合适的 MATLAB 函数! (“m” suffix) » a=[2 0; 0 3]; exp(a), expm(a) ans = 7.38905609893065 1.00000000000000 1.00000000000000 20.08553692318767 ans = 7.38905609893065 0 0 20.08553692318770

  48. MATLAB 函数:特殊矩阵 • 特殊矩阵:内置函数用于标准处理 • and many more...

  49. MATLAB 函数:基本数学运算 • 指定阵列元素上的运算

  50. MATLAB 函数:基本数学运算 • 三角函数 • Matlab 中的三角函数把角度表示为弧度 • 可用的函数包括 sin(x), cos(x), tan(x), asin(x), acos(x), atan(x)。 • 其余的三角函数可以通过相应的关系计算,例如 sec(x)=1/cos(x)

More Related