MATLAB原理及应用.ppt

上传人:牧羊曲112 文档编号:5438966 上传时间:2023-07-07 格式:PPT 页数:68 大小:1.03MB
返回 下载 相关 举报
MATLAB原理及应用.ppt_第1页
第1页 / 共68页
MATLAB原理及应用.ppt_第2页
第2页 / 共68页
MATLAB原理及应用.ppt_第3页
第3页 / 共68页
MATLAB原理及应用.ppt_第4页
第4页 / 共68页
MATLAB原理及应用.ppt_第5页
第5页 / 共68页
点击查看更多>>
资源描述

《MATLAB原理及应用.ppt》由会员分享,可在线阅读,更多相关《MATLAB原理及应用.ppt(68页珍藏版)》请在三一办公上搜索。

1、MATLAB原理及应用,矩阵的操作,MATLAB的help,MATLAB的help,Help 的详细使用见MATLAB/Getting Started/Desktop Tools and Development Environment/Getting Help,MATLAB的help,MATLAB帮助函数,help funcname:Displays in the Command window a description of the specified function funcname.lookfor topic:Looks for the string topic in the firs

2、t comment line(the H1 line)of the HELP text of all M-files found on MATLABPATH(including private directories),and displays the H1 line for all files in which a match occurs.doc funcname:Opens the Help Browser to the reference page for the specified function funcname,providing a description,additiona

3、l remarks,and examples.,Entering Commands and Expressions,MATLAB retains your previous keystrokes.Use the up-arrow key to scroll back back through the commands.Press the key once to see the previous entry,and so on.Use the down-arrow key to scroll forward.Edit a line using the left-and right-arrow k

4、eys the Backspace key,and the Delete key.Press the Enter key to execute the command.,管理工作进程的命令,分号;抑制输出省略号三个点 如果输入语句太长,一行放不下,就用省略号,然后按回车表明下行接着写,如 s=1-1/2+1/3-1/4+1/5-1/6+1/7.-1/8+1/9-1/10+1/11-1/12;,控制命令窗口输入输出显示,格式函数format(见help说明)format 控制MATLAB显示的数值的格式,它只影响数的显示,不影响MATLAB如何计算它或保存它。,输入/输出命令,系统、目录、文件命

5、令,向量的产生矩阵的产生矩阵的基本操作矩阵中元素的获取改变矩阵形状矩阵信息的获取,变量(variable),不必事先声明变量名可包含最多63个字符变量名必须以字母开头,后面可跟字母,数字和下划线变量区分大小写,MATLAB如何识别变量,When you type problem1,1.MATLAB first checks to see if problem1 is a variable and if so,displays its value.2.If not,MATLAB then checks to see if problem1 is one of its own commands,a

6、nd executes it if it is.3.If not,MATLAB then looks in the current directory for a file named problem1.m and executes problem1 if it finds it.4.If not,MATLAB then searches the directories in its search path,in order,for problem1.m and then executes it if found.,MATLAB特殊变量(常数),ans 缺省变量名pi 3.1415926.ep

7、s 最小增量数 2(-52)Inf 无穷NaN 非数 如0/0realmin 最小浮点数 2.2251e-308realmax 最大浮点数 1.7977e+308i,j 虚数单位,标量(scalar),7,583.62,-3.51,5.46097e-14,83+4i,向量(vector),矩阵(matrix,array),53.2 87.39 4-12i 43.9,1 2 3 8 4 5 6 0 4 7 8 7,产生向量,向量:向量元素用逗号或空格隔开,再用方括号括起来产生行向量p=3,7,9p=3 7 9用()将行向量转置产生列向量.p=3,7,9 p=3 7 9g=3;7;9,数值序列也可

8、产生向量,中括号不要x=m:q:n如果m-n是q的整数倍,产生的向量的最后一个值是n如果x=0:2:8,则产生的向量 x=0,2,4,6,8如果m-n不是q的整数倍,产生的向量的最后一个值小于n如果x=0:2:7,则产生的向量x=0,2,4,6,数值序列(也可产生向量),缺省步长为整数1A=10:15 A=-2.5:2.5 A=1:6.3(即使结尾的值是小数,产生的序列都是整数)A=1 2 3 4 5 6设置任意步长值(步长分别为整数、小数、负数)A=10:5:50 A=3:0.2:3.8 A=9:-1:1,可作下标 A(1:100)可作循环变量取值 i=1:2:15可生成向量或数组 a=10

9、0:-1:50,linspace命令,命令linspace产生线性间隔行向量,只能定义元素个数不能定义增量(步长)linspace(x1,x2,n)where x1 and x2 are the lower and upper limits and n is the number of points.If n is omitted,the spacing is 1.linspace(5,8,31)is equivalent to 5:0.1:8.,Logspace对数间隔命令,Its syntax is logspace(a,b,n),where n is the number of poin

10、ts between 10a and 10b.For example,x=logspace(-1,1,4)produces the vector x=0.1000,0.4642,2.1544,10.000.If n is omitted,the number of points defaults to 50.,Magnitude,Length,and Absolute Value 的区别,注意help文档中Magnitude length absolute value的区别Length指的是向量中元素个数Magnitude指的是 向量中元素的开平方(x12+x22+xn2)与向量的几何长度一样

11、absolute value指的是向量中每一个元素的绝对值For exampleif x=2,-4,5,its length is 3;(computed from length(x)its magnitude is 22+(4)2+52=6.7082;(computed from sqrt(x*x)its absolute value is 2,4,5(computed from abs(x).,矩阵(matrix)的输入,输入明确的元素清单 元素间用空格或逗号隔开 行与行用分号或回车(enter)隔开 用方括号括起所有元素-1 2 3 4 5;4 5 6-7 8;1 2 3-9 3-1,2

12、,3,4,5;4,5,6,-7,8;1,2,3,-9,3用MATLAB内部函数产生矩阵 B=magic(4)D=1,3,5;7,9,11;,矩阵元素的获取,获取单个元素双下标表示A(i,j)代表矩阵A的第i行第j列的元素 单下标表示A(i)代表矩阵A的存储的第i个元素 A=2 6 9;4 2 8;3 5 1A=2 6 9 4 2 8 3 5 1矩阵中元素在内存中按列存储:2,4,3,6,2,5,9,8,1,矩阵元素的获取,获取多个元素获取连续元素A(1:m,n)获取A矩阵第n列1到m个元素A(:)获取A矩阵中所有的元素A=magic(4);A(1,4)+A(2,4)+A(3,4)+A(4,4)

13、等同 sum(A(1:4,4)获取不连续元素A(a:s:m,n)A(行向量,n),矩阵中不连续元素的获取,A=magic(4)B=A;B(1:3:16)=-10B=-10 2 3-10 5 11-10 8 9-10 6 12-10 14 15-10,A=5:5:50B=1 3 6 7 10;A(B)ans=5 15 30 35 50,关键字end,end代表数组或矩阵中最后一个元素。编程时,不知道矩阵的维数时,在获取最后一个元素时很有用。B(1:3:16)=-10 等同 B(1:3:end)=-10,冒号的使用,表示元素间隔单位 0:pi/4:pi 表示引用矩阵的元素A(1:k,j)矩阵A第j

14、列前k个元素 A(:,j)矩阵A第j列所有元素 A(:,end)矩阵A最后一列所有元素A(:)矩阵A所有元素,输出以列向量显示A=B(:,1 3 2 4)交换矩阵B的中间两列变成AA(1,:)=删除A矩阵的第一行,矩阵基本信息函数,矩阵基本信息函数,min 求数组里面元素的最小值 x=min(A)若A是向量,就求所有元素最小值,x是一标量;若A是矩阵,就按列求每列元素最小值,x是一行向量 x,k=min(A)x包含A矩阵的最小值,k包含最小值的位置编号max 求数组里面元素的最大值 x=max(A)x,k=max(A),矩阵的调整,减少矩阵元素 A=magic(4)A=16 2 3 13 5

15、11 10 8 9 7 6 12 4 14 15 1A(:,2)=删除A矩阵第2列A(2:2:10)=删除A矩阵第2到10个元素,矩阵的调整,矩阵的调整,调整矩阵形状 reshape 修改矩阵形状A=1 4 7 10;2 5 8 11;3 6 9 12A=1 4 7 10 2 5 8 11 3 6 9 12B=reshape(A,2,6)B=1 3 5 7 9 112 4 6 8 10 12,矩阵的调整,调整矩阵形状 fliplr 水平翻转矩阵A=1 4 7 10;2 5 8 11;3 6 9 12A=1 4 7 10 2 5 8 11 3 6 9 12B=fliplr(A)B=10 7 4

16、1 11 8 5 2 12 9 6 3,矩阵的调整,调整矩阵形状 transpose 矩阵转置(等效运算符.)A=1 4 7 10;2 5 8 11;3 6 9 12A=1 4 7 10 2 5 8 11 3 6 9 12B=A.B=1 2 3 4 5 6 7 8 9 10 11 12,矩阵的调整,调整矩阵形状 ctranspose 矩阵共轭转置(等效运算符)A=1+9i 2-8i 3+7i;4-6i 5+5i 6-4i A=1.0000+9.0000i 2.0000-8.0000i 3.0000+7.0000i 4.0000-6.0000i 5.0000+5.0000i 6.0000-4.0

17、000iB=AB=1.0000-9.0000i 4.0000+6.0000i 2.0000+8.0000i 5.0000-5.0000i 3.0000-7.0000i 6.0000+4.0000i,基本矩阵函数,eye 单位一矩阵 eye(n)eye(m,n)ones 全一矩阵 ones(n)ones(m,n)zeros 全零矩阵 zeros(n)zeros(m,n)rand 均匀分布的随机矩阵(0,1)之间randn 正态分布的随机矩阵 均值为零 方差为1diag 生成对角矩阵magic 每列,每行,每个主对角线上的元素之和 都相同。注意:全零矩阵zeros不是空矩阵,多项式的表示及运算,多

18、项式表示将多项式表示成行向量,其中元素是多项式系数,按降幂排列。P(x)=x3-2x-5 p=1 0-2-5;多项式求函数值 polyval polyval(p,5)ans=110,Example of Plotting a Polynomial画多项式函数f(x)=9x3 5x2+3x+7 for-2 x 5a=9,-5,3,7;x=-2:0.01:5;f=polyval(a,x);plot(x,f),xlabel(x),ylabel(f(x),多项式的表示及运算,多项式求根roots(poly 由根返回多项式系数)计算多项式系数poly poly(r),r中包含了多项式的根,计算结果是一行

19、向量,其中的多项式系数按降幂顺序排列。,r=roots(p)r=2.0946-1.0473+1.1359i-1.0473-1.1359i,p2=poly(r)p2=1 8.8818e-16-2-5,多项式乘、除,多项式相乘 conv(a,b)a,b为相乘的多项式的系数多项式相除 q,r=deconv(num,den)num中包含了分子多项式的系数dem中包含了分母多项式的系数q为商式的系数r为余式多项式的系数,逻辑函数,all 确定所有元素是否都是非零或逻辑1 B=all(A)any 确定是否有非零元素 B=any(A)and 求逻辑与 and(A,B)or 求逻辑或 or(A,B)not 求

20、逻辑非 not(A)xor 求逻辑异或 xor(A,B),Logical function,Logical functionDefinitionischar(A)Returns a 1 if A is a character array and 0 otherwise.isempty(A)Returns a 1 if A is an empty matrix and 0 otherwise.isinf(A)Returns an array of the same dimension as A,with ones whereA has inf and zeros elsewhere.isnan(

21、A)Returns an array of the same dimension as A with ones whereA has NaN and zeros elsewhere.(NaN stands for“not anumber,”which means an undefined result.),逻辑函数例子,若 A=0.53 0.67 0.01 0.38 0.07 0.42 0.69 那么 B=(A 0.5)返回 B=0 0 1 1 1 1 0 any(B)返回逻辑1用any函数可控制语句的条件 if any(A 0.5)语句 end,逻辑函数的使用 a=-2-1 0 1 2 is

22、inf(1./a)Warning:Divide by zero.ans=0 0 1 0 0 isnan(0./a)Warning:Divide by zero.ans=0 0 1 0 0,由逻辑运算和关系运算产生的逻辑数组可用于数组元素获取x=2.1 1.7 1.6 1.5 NaN 1.9 1.8 1.5 5.1 1.8 1.4 2.2 1.6 1.8;NaN问题数据,要去掉它x=x(isfinite(x)x=2.1 1.7 1.6 1.5 1.9 1.8 1.5 5.1 1.8 1.4 2.2 1.6 1.85.1是异常值,去掉它.The following statement remove

23、s outliers,in this case those elements more than three standard deviations from the mean:x=x(abs(x-mean(x)=3*std(x)x=2.1 1.7 1.6 1.5 1.9 1.8 1.5 1.8 1.4 2.2 1.6 1.8,数的表示,虚数与科学计算法虚数i或 j 6i-3.14159j10的幂用科学计算法 6.02252e23 运算符+加-减*乘(按矩阵运算规则).*点乘(元素对元素)/除./点除 幂.点幂 共轭转置,Complex Number Operations,The number

24、 c1=1 2i is entered as follows:c1=1-2i.An asterisk is not needed between i or j and a number,although it is required with a variable,such as c2=5 i*c1.Be careful.The expressions y=7/2*iand x=7/2i give two different results:y=(7/2)i=3.5iand x=7/(2i)=3.5i.,Operations with Complex Numbersx=-3+4i;y=6-8i

25、;mag_x=abs(x)mag_x=5.0000mag_y=abs(y)mag_y=10.0000mag_product=abs(x*y)50.0000,Operations with Complex Numbersangle_x=angle(x)angle_x=2.2143angle_y=angle(y)angle_y=-0.9273sum_angles=angle_x+angle_ysum_angles=1.2870angle_product=angle(x*y)angle_product=1.2870,Scalar arithmetic operations,矩阵乘运算,阵列乘标量阵列

26、中每个元素与标量相乘两阵列相乘(除、求幂)数组乘 元素对元素(点乘)矩阵乘 矩阵相乘的运算规则,A=2,9;5,-7;3*Aans=6 27 15-21,x.*y=x(1)y(1),x(2)y(2),.,x(n)y(n),若x是一向量,表达式 z=(ey sin x)cos2x,用MATLAB来表示:z=exp(y).*sin(x).*(cos(x).2.,Element-by-element operations,Symbol+-+-.*./.,Examples6,3+2=8,58,3-5=3,-26,5+4,8=10,136,5-4,8=2,-33,5.*4,8=12,402,5./4,8

27、=2/4,5/82,5.4,8=24,583,5.2=32,522.3,5=23,253,5.2,4=32,54,OperationScalar-array additionScalar-array subtractionArray additionArray subtractionArray multiplicationArray right divisionArray left divisionArray exponentiation,FormA+bA bA+BA BA.*BA./BA.BA.B,Matrix Left Division and Linear AlgebraicEquati

28、ons 6x+12y+4z=707x 2y+3z=52x+8y 9z=64 A=6,12,4;7,-2,3;2,8,-9;B=70;5;64;Solution=ABSolution=3 5-2The solution is x=3,y=5,and z=2.,算术运算的优先级,先括号,再幂运算,再乘除运算,最后是加减运算,The arithmetic operators+,-,*,/,and have precedence over the relational operators.Thus the statement z=5 2+7 is equivalent to z=5(2+7)and r

29、eturns the result z=0.We can use parentheses to change the order of precedence;for example,z=(5 2)+7 evaluates to z=8.,逻辑运算符,&、|、分别表示与、或、非元素对元素进行逻辑运算逻辑0代表“假”,逻辑1或非零元素代表“真”逻辑运算返回一个逻辑数组A&B 等同 and(A,B)A|B 等同 or(A,B)A 等同 not(A),Logical Operators and the find FunctionConsider the sessionx=5,-3,0,0,8;y=2,

30、4,0,5,7;z=find(x&y)z=1 2 5Note that the find function returns the indices,and not the values.,关系运算符,、=、=、=关系运算符完成两个数组之间的元素对元素的比较,返回一个同维的逻辑数组,关系为“真”相应的元素为1,否则为0。例如:X=5;X=1 2 3;4 5 6;7 8 10 ans=1 1 1 1 1 0 0 0 0,For example,suppose that x=6,3,9 and y=14,2,9.The following MATLAB session shows some exam

31、ples.z=(x z=(x=y)z=1 1 0z=(x 8)z=0 0 1,用生成的逻辑数组对数组进行寻址When a logical array is used to address another array,it extracts from that array the elements in the locations where the logical array has 1s.So typing A(B),where B is a logical array of the same size as A,returns the values of A at the indices

32、where B is 1.,Specifying array subscripts with logical arrays extracts the elements that correspond to the true(1)elements in the logical array.Given A=5,6,7;8,9,10;11,12,13 and B=logical(eye(3),we can extract the diagonal elements of A by typing C=A(B)to obtain C=5;9;13.,The relational operators ca

33、n be used for array addressing.For example,with x=6,3,9 and y=14,2,9,typing z=x(xy)finds all the elements in x that are less than the corresponding elements in y.The result is z=6.,关系运算符用于寻址,数学 Mathematics,线性代数 矩阵分析、线性方程、特征值、奇异值、矩阵的对数、矩阵的指数、矩阵分解 基本数学三角函数、指数函数、对数、复数、近似、求余数、离散数学(有理分式、最小公倍数、最大公约数)多项式多项

34、式相乘、相除、部分分式展开、求根、微分、积分傅里叶变换 fftguid fft见MATLAB/Users Guide/Mathematics/Fourier Tramsforms,常见数学函数,Exponentialexp(x)sqrt(x)Logarithmiclog(x)log10(x),常见数学函数,Complexabs(x)angle(x)conj(x)imag(x)real(x),Absolute valueAngle of a complex numberComplex conjugateImaginary part of a complex numberReal part of

35、a complex number,常见数学函数,Numericceil(x)fix(x)floor(x)round(x)sign(x),Round to(四舍五入)nearest integer toward Round to nearest integer toward zeroRound to nearest integer toward-Round toward nearest integer.Signum function:+1 if x 0;0 if x=0;-1 if x 0.,三角函数,cos(x)cot(x)csc(x)sec(x)sin(x)tan(x),Cosine;cos

36、 x.Cotangent;cot x.Cosecant;csc x.Secant;sec x.Sine;sin x.Tangent;tan x.,反三角函数,acos(x)acot(x)acsc(x)asec(x)asin(x)atan(x)atan2(y,x),Inverse cosine;arccos x.Inverse cotangent;arccot x.Inverse cosecant;arccsc x.Inverse secant;arcsec x.Inverse sine;arcsin x.Inverse tangent;arctan x.Four-quadrant inverse tangent.,Order of precedence for operator types,PrecedenceOperator typeFirstParentheses;evaluated starting with the innermost pair.SecondArithmetic operators and logical NOT();evaluated from left to right.ThirdRelational operators;evaluated from left to right.FourthLogical AND.FifthLogical OR.,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号