1 / 194

MATLAB and Linear Algebra ( Application of Matlab in Linear Algebra)

MATLAB and Linear Algebra ( Application of Matlab in Linear Algebra). 王正盛 WANG Zhengsheng wangzhengsheng@nuaa.edu.cn 025-52075617 ( O ) ; 13952091572 Department of Mathematics NUAA. 课程的性质、目的和任务.

marty
Download Presentation

MATLAB and Linear Algebra ( Application of Matlab in Linear Algebra)

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 and Linear Algebra(Application of Matlab in Linear Algebra) 王正盛 WANG Zhengsheng wangzhengsheng@nuaa.edu.cn 025-52075617(O); 13952091572 Department of Mathematics NUAA Dr WangZhengsheng - Lecture Notes

  2. 课程的性质、目的和任务 • Matlab 是被广泛地应用在科学与工程计算领域里。它具有功能强大、使用简单等特点,内容包括:数值计算、符号计算、数据拟合、图形图像处理、系统模拟和仿真分析等功能。此外,用Matlab还可以进行动画设计、有限元分析等。 • 本课程的目的是使学生能够运用Matlab进行一般的工程计算,掌握Matlab的基本技术(基本运算、矩阵计算、符号运算、图形可视化技术和Matlab程序设计等),为将来从事工程技术方面的产品开发、科学研究、工程计算和管理打下一定的基础。 Dr WangZhengsheng - Lecture Notes

  3. 教学基本要求 1.了解Matlab语言特点、熟悉Matlab视窗环境、掌握Matlab帮助功能的基本使用方法。 2.掌握Matlab中变量和函数的有关概念、以及矩阵和多项式的构造方法。掌握数组运算和矩阵运算,并能够使用常用的几种常用的函数进行简单问题的求解。 3.搞清符号变量和符号表达式的定义,并能用符号运算解决一般的微积分和方程求解问题。掌握Matlab程序设计的几种基本控制转移语句,学会使用简单M文件的编程。 4.掌握二维平面图形和三维立体图形的绘制方法,进行数据可视化处理 Dr WangZhengsheng - Lecture Notes

  4. Outline of my Lectures 1、Introduciton to Matlab 2、Matlab数值运算 3、Matlab图形与可视化 4、Matlab符号运算 5、Matlab程序设计 6、Linear Algebra with MATLAB Dr WangZhengsheng - Lecture Notes

  5. Lecture1 Introduction to MATLAB I、Matlab简介 II、Matlab的工作环境 III、Matlab帮助系统 Dr WangZhengsheng - Lecture Notes

  6. I Matlab简介 • MATLAB语言是一种广泛应用于工程计算及数值分析领域的新型高级语言,自1984年由美国 MathWorks 公司推向市场以来,历经十多年的发展与竞争,现已成为国际公认的最优秀的工程应用开发环境。MATLAB功能强大、简单易学、编程效率高,深受广大科技工作者的欢迎。 • 在欧美各高等院校,MATLAB已经成为线性代数、自动控制理论、数字信号处理、时间序列分析、动态系统仿真、图像处理等课程的基本教学工具,成为大学生、硕士生以及博士生必须掌握的基本技能。 Dr WangZhengsheng - Lecture Notes

  7. I Matlab简介 1、MATLAB 的起源与发展 • MATLAB 是“ 矩阵实验室”( MATrix LABoratory)的缩写,最初版本是由 Cleve Moler 博士用 FORTRAN 语言开发的矩阵分析软件。 • 80 年代初期,由 Cleve Moler 和 John Little采用 C 语言改写了 MATLAB 的内核。成立了 Mathworks 软件开发公司,并于1984 年将 MATLAB 正式推向市场。 • 1992 年初推出了应用于 Windows 操作系统的 MATLAB 4.x 版本。 • 1997 年推出 5.1 版本。 • 1998 年推出 5.2 版本。 • 1999 年推出 MATLAB 5.3 版本,MATLAB 5.3对应于 Release 11 。 • 2000 年又推出了更为简便易学的 MATLAB 6.0 版本,MATLAB 6.0 对应于 Release 12。 Dr WangZhengsheng - Lecture Notes

  8. 2、MATLAB 的主要特点 • (1)数值计算和符号计算功能 • MATLAB 的基本单位为矩阵. • MATLAB的数值计算功能包括:矩阵运算、多项式和有理分式运算、数据统计分析、数值积分、优化处理等。符号计算将得到问题的解析解。 (2).MATLAB语言 MATLAB除了命令行的交互式操作以外,还可以程序方式工作。使用MATLAB可以很容易地实现C或FORTRAN语言的几乎全部功能,包括Windows图形用户界面的设计。 (3)图形功能 • MATLAB提供了两个层次的图形命令:一种是对图形句柄进行的低级图形命令,另一种是建立在低级图形命令之上的高级图形命令。利用MATLAB的高级图形命令可以轻而易举地绘制二维、三维乃至四维图形,并可进行图形和坐标的标识、视角和光照设计、色彩精细控制等等。 Dr WangZhengsheng - Lecture Notes

  9. (4 )应用工具箱 基本部分和各种可选的工具箱。 基本部分中有数百个内部函数。 • 其工具箱分为两大类:功能性工具箱和学科性工具箱。功能性工具箱主要用来扩充其符号计算功能、可视建模仿真功能及文字处理功能等。学科性工具箱专业性比较强,如控制系统工具箱、信号处理工具箱、神经网络工具箱、最优化工具箱、金融工具箱等,用户可以直接利用这些工具箱进行相关领域的科学研究。 Dr WangZhengsheng - Lecture Notes

  10. 3、MATLAB 的运行环境 • 硬件: 一般要求486以上的处理器、16MB以上内存、足够的的硬盘可用空间(随安装组件的多少而定)、CD-ROM驱动器、鼠标等。 • 下载: ftp.nuaa.edu.cn • 软件: Windows 9x ,2000,xp,NT … • Note that: Vista 需要安装高版本的MATLAB Dr WangZhengsheng - Lecture Notes

  11. II Matlab工作环境界面 1. MATLAB 的启动 桌面快捷方式启动 开始菜单方式启动 2. MATLAB和退出 Matlab菜单命令/关闭按钮 在Matlab命令窗口输入exit或quit Dr WangZhengsheng - Lecture Notes

  12. II Matlab工作环境界面 1、MATLAB菜单项 File( 文件)菜单 Edit( 编辑)菜单 View( 视图)菜单 Window( 窗口)菜单 Web( 连网信息) Help( 帮助)菜单 Dr WangZhengsheng - Lecture Notes

  13. II Matlab工作环境界面 例 1-1 绘制函数 y=2sin(1+x)的图像,并计算当 x=0.5 时的函数值。 x=(1:0.1:10); %给出自变量 x 的定义域 y=2*sin(1+x); %写出函数形式 plot(x,y) %绘出函数图形 y=2*sin(1+0.5) %求当 x=0.5 时的 y 值,其后不加分号,直接在窗口中给出结果 y =1.9950 %输出 y 的计算结果 Dr WangZhengsheng - Lecture Notes

  14. II Matlab工作环境界面 MATLAB 通用命令 Dr WangZhengsheng - Lecture Notes

  15. III Matlab帮助系统 Matlab常用的窗口帮助命令 Dr WangZhengsheng - Lecture Notes

  16. III Matlab帮助系统 help 命令 • 直接输入 help,MATLAB 将列出所有的帮助主题,每个帮助主题对应于 MATLAB搜索路径中的一个目录; • help 后加帮助主题,可获得指定帮助主题的帮助信息; • help 后加函数名; • help 后加命令名,将得到指定命令的用法; Dr WangZhengsheng - Lecture Notes

  17. 上机练习 ( 1)以两种方式打开 MATLAB 工作窗口,进入MATLAB 6.0 的工作环境,并尝试用不同的方式退出。 ( 2)尝试、熟悉 MATLAB 6.0 的各栏菜单以及各个工具栏的功能。 ( 3)重新启动 MATLAB 6.0,进入 MATLAB 工作窗口,用 who 命令查看当前工作空间内有无变量及其值。 ( 4)绘制函数y=cos(5x+2)/sin(3x+1) 的图像,并求解当 x=2 时的函数值。 ( 5)此时再次用 who 命令查看工作空间内的变量名及其值,与( 3)比较,同时用whos 命令查看变量,比较与 who 命令的不同。 ( 6)熟练掌握 MATLAB 的通用命令。 ( 7)练习并熟练掌握 MATLAB 的帮助命令,学会利用 MATLAB的帮助信息。 (8)用 lookfor 命令查找函数 cos 的信息,并与 help 命令查找的结果相比较,注意采用两种命令之间的差别。 Dr WangZhengsheng - Lecture Notes

  18. Lecture 2 MATLAB数值运算 Dr WangZhengsheng - Lecture Notes

  19. Lecture 2 MATLAB数值运算 基本要求: (1) 了解Matlab的各种数据类型; (2) 掌握Matlab的运算符与操作符的含义和用法; (3) 掌握Matlab基本表达式并了解其常用函数; Dr WangZhengsheng - Lecture Notes

  20. Lecture 2 MATLAB数值运算 • 1、 Matlab数据类型 • 变量和常量 • 数字变量的格式 • 矩阵、向量 • 多项式 Dr WangZhengsheng - Lecture Notes

  21. Lecture 2 MATLAB数值运算 a) 变量和常量 变量(包括函数)的命名规则:由英文字母、数字和下划线混合组成,不得包含空格和标点 ;第一个字符必须是英文字母,最多包括31个字符; 对字母的大小写敏感。 局部变量和全局变量 永久变量(常量) 只能在某一函数体内使用,而不能从其他函数和 Matlab工作空间访问的变量,就是局部变量。 在几个函数及M atlab函数中都能使用的变量就是全局变量。 (全局变量名应尽可能大写,并由global声明) Dr WangZhengsheng - Lecture Notes

  22. Lecture 2 MATLAB数值运算 Matlab永久变量 Dr WangZhengsheng - Lecture Notes

  23. Lecture 2 MATLAB数值运算 • b) 数字变量 • Matlab内部的每一个数据元素都是用双精度数(double)来表示和存储的。 • 变量输出时用户可以用format命令设置或改变输出格式。 • 短格式(Short):1.3333 0.0000 • 短格式e方式(Short e):1.3333e+00 1.2345e-06 • 短格式g方式(Short g):1.3333 0.0 • 长格式(Long):1.33333333333333 0.00000123450000 • 长格式e方式(Long e):1.33333333333333e+00 1.2345000000000e-06 • 长格式g方式(Long g):1.33333333333333 0.0000012345 • 银行格式(Bank):1.33 0.00 • 十六进制格式(Hex):3ff555555555 3eb46231abfd71 • +格式(+):++ • 有理数(Rational):1/3 2469/2000000000 Dr WangZhengsheng - Lecture Notes

  24. Lecture2 MATLAB数值运算 c) 矩阵、向量 Matlab是以矩阵为基本运算单元的,数组作为独立的计算单元实体是不存在的,它的建立、存储完全等同于矩阵,只是计算时在符号上做了不同的约定。 向量是组成矩阵的基本元素之一。可以把行向量看成1n 阶矩阵,把列向量看成n1阶矩阵。向量的基本运算采用数组运算法则。 Dr WangZhengsheng - Lecture Notes

  25. Lecture2 MATLAB数值运算 d) 多项式 多项式是以向量的形式表达的。可以是列向量也可以是行向量。 >> p=[1,2,3] >> poly2sym(p) ans = x^2+2*x+3 >> m=[3;4;5] >> poly2sym(m) ans = 3*x^2+4*x+5 Dr WangZhengsheng - Lecture Notes

  26. Lecture2 MATLAB数值运算 2、运算符与操作符 数学运算符 操作符 Dr WangZhengsheng - Lecture Notes

  27. Lecture2 MATLAB数值运算 • 冒号“ :” • 可以用来产生向量; • 用作矩阵的下标,部分地选择矩阵元素; • 进行行循环操作。 • 续号“ …” 表示一行未完,而在下一行继续; • 分号“ ;” • 在方括号中,表示矩阵中行的结尾; • 用在每行的结尾,则不显示该行运算的结果。 Dr WangZhengsheng - Lecture Notes

  28. Lecture2 MATLAB数值运算 关于逻辑真假的规定: • 在所有的关系表达式和逻辑表达式中,输入的任何非 0 数都被看作是“ 逻辑真”,而只有 0 才被认为是“ 逻辑假”; • 所有关系表达式和逻辑表达式的计算结果是一个由 0 和 1 组成的“ 逻辑矩阵( Logical Array)”。矩阵中的 1 表示“ 真”,0 表示“ 假”; Dr WangZhengsheng - Lecture Notes

  29. Lecture2 MATLAB数值运算 关系操作符 逻辑操作符 Dr WangZhengsheng - Lecture Notes

  30. Lecture2 MATLAB数值运算 关系运算举例 >> NE=(A~=B) NE = 1 0 1 1 1 1 0 1 0 >> A0=(A>5) A0 = 0 0 1 1 0 0 0 0 1 >> B0=(B<=6) B0 = 1 1 1 0 0 1 1 1 0 >> A=[3,4,8;9,0,2;5,3,7] A = 3 4 8 9 0 2 5 3 7 >> B=[4,4,1;7,8,4;5,1,7] B = 4 4 1 7 8 4 5 1 7 >> E=(A>B) E = 0 0 1 1 0 0 0 1 0 Dr WangZhengsheng - Lecture Notes

  31. Lecture2 MATLAB数值运算 逻辑运算举例 >> A=[3,4,8;9,0,2;5,3,7] A = 3 4 8 9 0 2 5 3 7 >> B=[4,4,1;7,8,4;5,1,7] B = 4 4 1 7 8 4 5 1 7 >> AB=A&B AB = 1 1 1 1 0 1 1 1 1 >> A_B=A|B A_B = 1 1 1 1 1 1 1 1 1 >> C=~A C = 0 0 0 0 1 0 0 0 0 >> cc=(A>3)&(B<6) cc = 0 1 1 0 0 0 1 0 0 Dr WangZhengsheng - Lecture Notes

  32. Lecture2 MATLAB数值运算 3、表达式及函数 Matlab采用的是表达式语言,用户输入的语句由 Matlab系统解释运行。用户可以在 Matlab的命令窗口中键入命令,也可以在编辑器内编写应用程序。 Matlab语句由表达式和变量组成,有两种最常见的语句形式: 表达式 变量=表达式 >> t=6 t = 6 >> sin(5*t+6) ans = -0.9918 >> y=sin(5*t+6) y = -0.9918 Dr WangZhengsheng - Lecture Notes

  33. Leture2 MATLAB数值运算 • 表达式由变量名、运算符、数字和函数名组成。 • 表达式将按常规的优先纽从左至右执行运算; • 优先级的规定是指数运算级别最高,乘除运算次之; • 括号可以改变运算顺序; • 书写表达式时,赋值符“=”和运算符两侧允许有空格,以增加可读性。但在复数或符号表达式中要尽量避免空格,以防出错; • 表达式的末尾加 “;” 时,Matlab系统只把数值赋给变量, 不显示计算结果;不加“;”时, Matlab 系统将会在该条语句的下面直接显示运算结果。 Dr WangZhengsheng - Lecture Notes

  34. Leture2 MATLAB数值运算 • Matlab 有十分丰富的函数资源,总共分为24类。 • 常用有一下几类: • 一般函数命令(GENERAL); • 操作符与操作(OPERATOR); • 数据类型和结构(DATETYPE); • 基本矩阵和矩阵操作(ELMAT); • 基本数学函数(ELFUN); • 二维图(PLOTXY); • 图形句柄(GENGRAPH); • 三维图(3DGRAPH); • 语言和程序设计(LANGUAGE); • 文件输入输出函数(IOFUN); • 符号工具箱(SYNBOLIC)。 Dr WangZhengsheng - Lecture Notes

  35. MATLAB矩阵运算 • 矩阵是MATLAB最基本的数据对象,MATLAB的大部分运算或命令都是在矩阵运算的意义下执行的。在MATLAB中,不需对矩阵的维数和类型进行说明,MATLAB会根据用户所输入的内容自动进行配置。 • 1.建立矩阵 • 建立矩阵可以用:直接输入法、利用函数建立矩阵和利用M文件建立矩阵。 • 直接输入法:将矩阵的元素用方括号括起来,按矩阵行的顺序输入各元素,同一行的各元素之间用空格或逗号分隔,不同行的元素之间用分号分隔。(也可以用回车键代替分号) • 例如,键入命令: A=[1 2 3;4 5 6;7 8 9] • 输出结果是: A = 1 2 3 • 4 5 6 • 7 8 9 Dr WangZhengsheng - Lecture Notes

  36. 1.建立矩阵 • 利用函数建立数值矩阵:MATLAB提供了许多生成和操作矩阵的函数,可以利用它们去建立矩阵。 • 例如: reshape函数和diag函数等。 • reshape函数用于建立数值矩阵。 • diag函数用于产生对角阵。 • 利用M文件建立矩阵:对于比较大且比较复杂的矩阵,可以为它专门建立一个M文件。其步骤为: • 第一步:使用编辑程序输入文件内容。 • 第二步:把输入的内容以纯文本方式存盘(设文件名为 mymatrix.m)。 • 第三步:在MATLAB命令窗口中输入mymatrix,就会自动建立一个名为AM的矩阵,可供以后显示和调用。 Dr WangZhengsheng - Lecture Notes

  37. 1.建立矩阵 • 利用M文件建立矩阵: 对于比较大且比较复杂的矩阵,可以为它专门建立一个M文件。其步骤为: • 第一步:使用编辑程序输入文件内容。 • 第二步:把输入的内容以纯文本方式存盘(设文件名为mymatrix.m)。 • 第三步:在MATLAB命令窗口中输入mymatrix,就会自动建立一个名为AM的矩阵,可供以后显示和调用。 • 子矩阵操作 Dr WangZhengsheng - Lecture Notes

  38. 2.矩阵的基本运算 • (1)矩阵转置 • (2)矩阵加和减 • (3)矩阵乘法 • (4)矩阵除法 A\b=inv(A)*b • (5)矩阵的乘方 a^2 Dr WangZhengsheng - Lecture Notes

  39. 3.矩阵的函数 • >> help matfun • Matrix functions - numerical linear algebra. • Matrix analysis. • norm - Matrix or vector norm. • normest - Estimate the matrix 2-norm. • rank - Matrix rank. • det - Determinant. • trace - Sum of diagonal elements. • null - Null space. • orth - Orthogonalization. • rref - Reduced row echelon form. • subspace - Angle between two subspaces. Dr WangZhengsheng - Lecture Notes

  40. 3.矩阵的函数 • Linear equations. • \ and / - Linear equation solution; use "help slash". • inv - Matrix inverse. • rcond - LAPACK reciprocal condition estimator • cond - Condition number with respect to inversion. • condest - 1-norm condition number estimate. • normest1 - 1-norm estimate. • chol - Cholesky factorization. • cholinc - Incomplete Cholesky factorization. • lu - LU factorization. • luinc - Incomplete LU factorization. • qr - Orthogonal-triangular decomposition. • lsqnonneg - Linear least squares with nonnegativity constraints. • pinv - Pseudoinverse. • lscov - Least squares with known covariance. Dr WangZhengsheng - Lecture Notes

  41. 3.矩阵的函数 • Eigenvalues and singular values. • eig - Eigenvalues and eigenvectors. • svd - Singular value decomposition. • gsvd - Generalized singular value decomposition. • eigs - A few eigenvalues. • svds - A few singular values. • poly - Characteristic polynomial. • polyeig - Polynomial eigenvalue problem. • condeig - Condition number with respect to eigenvalues. • hess - Hessenberg form. • qz - QZ factorization for generalized eigenvalues. • schur - Schur decomposition. Dr WangZhengsheng - Lecture Notes

  42. 3.矩阵的函数 • Matrix functions. • expm - Matrix exponential. • logm - Matrix logarithm. • sqrtm - Matrix square root. • funm - Evaluate general matrix function. • Factorization utilities • qrdelete - Delete a column or row from QR factorization. • qrinsert - Insert a column or row into QR factorization. • rsf2csf - Real block diagonal form to complex diagonal form. • cdf2rdf - Complex diagonal form to real block diagonal form. • balance - Diagonal scaling to improve eigenvalue accuracy. • planerot - Givens plane rotation. • cholupdate - rank 1 update to Cholesky factorization. • qrupdate - rank 1 update to QR factorization. Dr WangZhengsheng - Lecture Notes

  43. 4.建立矩阵的函数 • 常用函数有: • eye(size(A)) 产生与A矩阵同阶的单位矩阵 • zeros(m,n) 产生0矩阵 • ones(m,n) 产生幺矩阵 • rand (m,n) 产生随机元素的矩阵 • Size(a) 返回包含两个元素的向量。 • Length(a) 返回向量的长度。 • 5.数组运算 • (1) 数组的加和减 • (2) 数组的乘和除 • (3) 数组的乘方 Dr WangZhengsheng - Lecture Notes

  44. 6 应用举例 • 1、行列式计算; • 2、求解线性方程组; • 2、解特征值问题 Dr WangZhengsheng - Lecture Notes

  45. Lecture 3 MATLAB图形与可视化(Graphic) Dr WangZhengsheng - Lecture Notes

  46. Lecture 3 Matlab图形可视化 基本要求 (1)掌握图形窗口的创建与控制,以及图形窗口的基本操作; (2)熟练掌握二维和三维绘图基本的命令、线型控制; (3)初步掌握用特殊的图形来表现特殊数据的性质,如面积图、直方 图、饼图等。 (4)掌握坐标轴的控制和图形标注命令及其用法。 Dr WangZhengsheng - Lecture Notes

  47. Lecture 3 Matlab图形可视化 • A、二维平面图形与坐标系 • 1. 几个基本的绘图命令 • a. 线性坐标曲线plot • 函数命令plot 是MATLAB 二维曲线绘图中最简单、最重要、使用最广泛的一个线性绘图函数。它可以生成线段、曲线和参数方程曲线的函数图形。 • 命令格式: • plot(X,Y) • plot(x1,y1,x2,y2,…):综合调用方式 Dr WangZhengsheng - Lecture Notes

  48. Lecture 3 Matlab图形可视化 用命令 plot(x,y)绘制函数y=cos(x)在两个周期内的图形。 x=0:0.01:2*pi; y=cos(x); plot(x,y) 在同一图形窗口中用命令 plot(x,y)绘出正弦余弦函数的图形。 x=0:0.01:2*pi; y=[sin(x);cos(x)]; plot(x,y) Dr WangZhengsheng - Lecture Notes

  49. Lecture 3 Matlab图形可视化 • 二维函数曲线专用命令 fplot 用plot绘图在确定自变量的取值间隔时,一般采用平均间隔,有时会因某处 间距太大,而不能反映出函数的变化情况。fplot是绘制函数 y=f(x)图形的专用命令,它的数据点是自适应产生的,对那些导数变化较大的函数,用 fplot 函数绘出的曲线比等分取点所画出的曲线更加接近真实。 • fplot函数命令的调用格式为: • [X,Y]=fplot(‘fun’,lims) • fun:函数名字符串; • lims:定义 x 的取值区间,lims=[xmin,xmax]; • 二维函数曲线专用命令ezplot Dr WangZhengsheng - Lecture Notes

  50. Lecture 3 Matlab图形可视化 2.线型和颜色 plot 函数可以设置曲线的线段类型、定点标记和线段颜色。 常用的线段、颜色与定点标记参数 Dr WangZhengsheng - Lecture Notes

More Related