实验二 MATLAB基础知识(二)
- 格式:doc
- 大小:71.00 KB
- 文档页数:7
实验一 MATLAB入门(1)1.实验目的:(1)了解MATLAB的体系结构与特点,熟悉其集成开发环境。
(2)熟悉MATLAB界面窗口的功能和使用方法。
(3)熟悉MATLAB的帮助系统及使用方法。
(4)了解MATLAB的的数据类型、基本形式和数组的产生方法。
(5)掌握MATLAB基本的数学运算操作。
2.实验原理(1)MATLAB简介MATLAB是美国MathWorks公司开发的高性能的科学与工程计算软件。
它在数值计算、自动控制、信号处理、神经网络、优化计算、小波分析、图像处理等领域有着广泛的用途。
近年来, MATLAB在国内高等院校、科研院所的应用逐渐普及,成为广大科研、工程技术人员必备的工具之一。
MATLAB具有矩阵和数组运算方便、编程效率极高、易学易用、可扩充性强和移植性好等优点,俗称为“草稿纸式的科学计算语言”。
它把工程技术人员从繁琐的程序代码编写工作中解放出来,可以快速地验证自己的模型和算法。
经过几十年的扩充和完善,MATLAB已经发展成为集科学计算、可视化和编程于一体的高性能的科学计算语言和软件开发环境,整套软件由MATLAB开发环境、MATLAB语言、MATLAB数学函数库、MATLAB图形处理系统和MATLAB应用程序接口(API)等五大部分组成。
MATLAB的主要特点包括强大的计算能力(尤其是矩阵计算能力)、方便的绘图功能及仿真能力、极高的编程效率。
另外,MATLAB还附带了大量的专用工具箱,用于解决各种特定领域的问题。
通过学习软件的基本操作及其编程方法,体会和逐步掌握它在矩阵运算、信号处理等方面的功能及其具体应用。
通过本课程实验的学习,要求学生初步掌握MATLAB的使用方法,初步掌握M文件的编写和运行方法,初步将MATLAB运用于数字信号处理中。
循序渐进地培养学生运用所学知识分析和解决问题的能力。
(2)MATLAB的工作界面(Desktop)与操作MATLAB 安装成功后,第一次启动时,主界面如下图(不同版本可能有差异)所示:其中① 是命令窗口(Command Window ),是MATLAB 的主窗口,默认位于MATLAB界面的右侧,用于输入命令、运行命令并显示运行结果。
实验一 MATLAB 运算基础1、 先求下列表达式得值,然后显示MATLAB 工作空间得使用情况并保存全部变量。
(1) 0122sin 851z e =+(2) 21ln(2z x =+,其中2120.455i x +⎡⎤=⎢⎥-⎣⎦ (3) 0.30.330.3sin(0.3)ln , 3.0, 2.9,,2.9,3.022a a e e a z a a --+=++=--L (4) 2242011122123t t z t t t t t ⎧≤<⎪=-≤<⎨⎪-+≤<⎩,其中t =0:0、5:2、5 解:4、 完成下列操作:(1) 求[100,999]之间能被21整除得数得个数。
(2) 建立一个字符串向量,删除其中得大写字母。
解:(1) 结果:(2)、 建立一个字符串向量 例如:ch='ABC123d4e56Fg9';则要求结果就是:实验二 MATLAB 矩阵分析与处理1、 设有分块矩阵33322322E R A O S ⨯⨯⨯⨯⎡⎤=⎢⎥⎣⎦,其中E 、R 、O 、S 分别为单位矩阵、随机矩阵、零矩阵与对角阵,试通过数值计算验证22E R RS A OS +⎡⎤=⎢⎥⎣⎦。
解: M 文件如下;5、 下面就是一个线性方程组:1231112340.951110.673450.52111456x x x ⎡⎤⎢⎥⎡⎤⎡⎤⎢⎥⎢⎥⎢⎥⎢⎥=⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎣⎦⎣⎦⎢⎥⎢⎥⎣⎦(1) 求方程得解。
(2) 将方程右边向量元素b 3改为0、53再求解,并比较b 3得变化与解得相对变化。
(3) 计算系数矩阵A 得条件数并分析结论。
解: M 文件如下:实验三 选择结构程序设计1、 求分段函数得值。
2226035605231x x x x y x x x x x x x ⎧+-<≠-⎪=-+≤<≠≠⎨⎪--⎩且且及其他用if 语句实现,分别输出x=-5、0,-3、0,1、0,2、0,2、5,3、0,5、0时得y 值。
实验二、MA TLAB运算基础一、实验目的掌握MA TLAB各种表达式的书写规则及常用函数的使用。
掌握MA TLAB中字符串、元胞数组和结构的常用函数的使用。
二、实验内容及步骤1、设有矩阵A和B,A=[1 2 3 4 5;6 7 8 9 10;11 12 13 14 15;16 1718 19 20;21 22 23 24 25],B=[3 0 16;17 -6 9;0 23 -4;9 7 0;4 1311]1)求它们的乘积C2)将矩阵C的右下角3x2子矩阵赋给D>> A=[1:1:5;6:1:10;11:1:15;16:1:20;21:1:25];B=[3 0 16;17 -6 9;0 23 -4;9 7 0;4 13 11];...C=A*B,D=C(3:5,2:3)C =93 150 77258 335 237423 520 397588 705 557753 890 717D =520 397705 557890 7172、完成下列操作1)求[100,999]之间能被61整除的数及其个数(提示:先利用冒号表达式,再利用find和length函数。
)>> A=[100:999];B=find(rem(A,61)==0),B_total=length(B)B =23 84 145 206 267 328 389 450 511 572633 694 755 816 877B_total =152)建立一个字符串向量,删除其中的大写字母(提示:利用find函数和空矩阵。
)>> a=['MA TLAB is important'], b=abs(a); c=find(b<=90 & b>=65) , a(c)=[],a =MA TLAB is importantc = 1 2 3 4 5 6a =is important⑶已知A=[23 10 -78 0;41 -45 65 5;32 5 0 32;6 -5492 14],取出其前3行构成矩阵B,其前两列构成矩阵C,其左下角3x2子矩阵构成矩阵D,B与C的乘积构成矩阵E,分别求E<D、E&D、E|D、~E|~D。
佛山科学技术学院《MATLAB教程第二章实训》报告专业姓名成绩班级学号日期一、目的1.学习matlab的数据类型2.矩阵和数组的算术运算3.字符串4.时间和日期5.结构体和元胞数组6.多维数组7.逻辑运算和关系运算8.数组的信息获取9.多项式二、步骤1.学习matlab的数据类型Matlab R2010a定义了15种基本的数据类型,包括整型、浮点型、字符型和逻辑型等。
用户甚至可以定义自己的数据类型。
Matlab内部的任何数据类型,都是按照数组的形式进行储存和运算的。
数值型包括整数和浮点数,其中整数包括有符号数和无符号数,浮点数包括单精度型和双精度型。
在默认情况下,matlab默认将所有数值都按照双精度浮点数类型来存储和操作。
(1)常数和变量Matlab的常数采用十进制表示,可以用带小数点的形式直接表示,也可以用科学记数法。
数值的表示范围是10^-309-10^309。
变量是数值计算的基本单元。
Matlab与其他的高级语言不同,变量使用是无需先定义,其名称就是第一次合法出现时的名称,因此用起来很便捷。
Matlab的变量命名有一定的规则:a.变量区分字母的大小写。
例如,“a”和“A”是不同的变量。
b.变量名不能超过63个字符,第63个字符后的字符会被忽略。
c.变量名必须以字母开头,变量名的组成可以是任意字母、数字或者下划线,但不能有空格和标点符号。
d.关键字(如if\while等)不能作为变量名。
在matlab中的所有表示符号包括函数名、文件名都是遵循变量名的命名规则。
Matlab中有一些自己的特殊变量,是由系统预先自动定义的,例如:ans——运算结果的默认变量名Pi——圆周率πEps——浮点数的相对误差Inf或inf——无穷大Nan或nan——不定值i或j——i=j=-1^1/2,虚数单位Nargin——函数的输入变量数目Nargout——函数的输出变量数目Realmin——最小的可用正实数Realmax——最大的可用正实数(2)整数和浮点数Matlab提供了8种内置的整数类型,为了在使用时提高运行速度和存储空间,应该尽量使用字节少的数据类型,可以使用类型转换函数将各种整数类型强制相互转换。
MATLAB 实验报告班级:14通信1班 学号:201424124124 姓名:林启铭实验一 MATLAB 运算基础(一)一、实验目的1、掌握建立矩阵的方法。
2、掌握MATLAB 各种表达式的书写规则以及各种运算方法。
二、实验内容1、求下列表达式的值。
(1)20185sin 21ez += MATLAB 代码:z1=2*sin(85*pi/180)/(1+exp(2))%将角度化为弧度z1 =0.2375(2)()x x z ++=1ln 212,其中⎢⎣⎡-=45.02x ⎥⎦⎤+521i (可以分别对矩阵和元素群运算)MATLAB 代码:>> x=[2,1+2i;-0.45,5]x =2.0000 + 0.0000i 1.0000 + 2.0000i-0.4500 + 0.0000i 5.0000 + 0.0000i>> z2=1/2*log(x+sqrt(1+x))z2 =0.6585 + 0.0000i 0.6509 + 0.4013i-0.6162 + 0.0000i 1.0041 + 0.0000i(3)()3.0sin 232.03.0+⋅-=a e e z aa , 0.3,9.2,8.2,...,8.2,9.2,0.3---=a (结果请用图形表示)(提示:利用冒号表达式生成a 向量;求各点的函数值时用点乘运算)MATLAB 代码:>> a=-3.0:0.1:3.0;%利用冒号表达式生成a 向量,加分号结尾避免大量数据刷屏 >> z3=(exp(0.3.*a)-exp(0.2.*a)).*sin(a+0.3)/2;>> plot(a,z3);%绘制出以a 为自变量,z3为因变量的曲线>>曲线图:2、已知⎢⎢⎢⎣⎡=33412A 65734⎥⎥⎥⎦⎤-7874 和 ⎢⎢⎢⎣⎡=321B 203-⎥⎥⎥⎦⎤-731 求下列表达式的值:(1)A+6*B 和A-B+I (其中I 为单位矩阵)。
实验⼆时域采样与频域采样及MATLAB程序知识讲解实验⼆时域采样与频域采样及M A T L A B程序实验⼆时域采样与频域采样⼀实验⽬的1 掌握时域连续信号经理想采样前后的频谱变化,加深对时域采样定理的理解2 理解频率域采样定理,掌握频率域采样点数的选取原则⼆实验原理1 时域采样定理对模拟信号()a x t 以T 进⾏时域等间隔采样,形成的采样信号的频谱()a X j Ω会以采样⾓频率2()s s TπΩΩ=为周期进⾏周期延拓,公式为: 1??()[()]()a a a s n X j FT x t X j jn T +∞=-∞Ω==Ω-Ω∑ 利⽤计算机计算上式并不容易,下⾯导出另外⼀个公式。
理想采样信号?()a xt 和模拟信号()a x t 之间的关系为: ?()()()a a n xt x t t nT δ+∞=-∞=-∑ 对上式进⾏傅⾥叶变换,得到:()[()()()()j t j t a a a n n X j x t t nT e dt x t t nT e dt δδ+∞+∞+∞+∞-Ω-Ω-∞-∞=-∞=-∞Ω=-=-∑∑??在上式的积分号内只有当t nT =时,才有⾮零值,因此:()()jn T aa n X j x nT e +∞-Ω=-∞Ω=∑上式中,在数值上()()a x nT x n =,再将T ω=Ω代⼊,得到:()()()jn j aa T T n X j x n e X e ωωωω+∞-=Ω=Ω=-∞Ω==∑上式说明采样信号的傅⾥叶变换可⽤相应序列的傅⾥叶变换得到,只要将⾃变量ω⽤T Ω代替即可。
2 频域采样定理对信号()x n 的频谱函数()j X e ω在[0,2π]上等间隔采样N 点,得到 2()()j k NX k X e ωπω== 0,1,2,,1k N =-L则有: ()[()][()]()N N N i x n IDFT X k x n iN R n +∞=-∞==+∑即N 点[()]IDFT X k 得到的序列就是原序列()x n 以N 为周期进⾏周期延拓后的主值序列,因此,频率域采样要使时域不发⽣混叠,则频域采样点数N 必须⼤于等于时域离散信号的长度M (即N M ≥)。
实验二MATLAB语言基础一、实验目的基本掌握MA TLAB向量矩阵数组的生成及基本运算(区分数组运算和矩阵预算)、常用的数学函数。
了解字符串的操作。
二、实验内容(1)向量的生成和运算。
(2)矩阵的创建、引用和运算。
(3)多维数组的创建和运算。
(4)字符创的操作。
三、实验步骤1.向量的生成和运算1)向量的生成<1>、直接输入法<2> 冒号表达式法<3> 函数法:Linspace()是线性等分函数,logspace()是对数等分函数。
2)向量的运算1>维数相同的行、列向量之间可以相加减,标量可以与向量直接相乘除。
2>向量的点积与叉积运算E1和E2虽然表达式相同,但E1是标量,E2是矩阵。
2.矩阵的创建、引用和运算1)矩阵的创建和引用矩阵是由m*n元素构成的矩形结构,行向量和列向量是矩阵的特殊形式。
1>直接输入法:2>抽取法:包括单下标抽取和全下表抽取两种方式,且两种方式抽取的元素都必须以小括号括起来。
3>函数法:利用ones(m;n)创建全1矩阵,zeros()创建全0矩阵,eyes()创建单位矩阵等等。
4>拼接法:纵向拼接横向拼接5>利用拼接函数cat()repmat()和变形函数reshape()>> A1=[1 2 3;9 8 7 ;4 5 6];A2=A1.';>> cat(1,A1,A2) 沿行向拼接ans =1 2 39 8 74 5 61 9 42 8 53 7 6>> cat(2,A1,A2) 沿列向拼接ans =1 2 3 1 9 49 8 7 2 8 54 5 6 3 7 6>> repmat(A1,2,2)ans =1 2 3 1 2 39 8 7 9 8 74 5 6 4 5 61 2 3 1 2 39 8 7 9 8 74 5 6 4 5 6> A=linspace(2,18,9)A =2 4 6 8 10 12 14 16 18 >> reshape(A,3,3)ans =2 8 144 10 166 12 182)矩阵的运算练习(1)用矩阵除法求下列方程组的解x=[x1;x2;x3]>> A=[6 3 4;-2 5 7;8 -1 -3];B=[3;-4;-7];X=A\BX =1.0200-14.00009.7200(2)求矩阵的秩A=[6 3 4;-2 5 7;8 -1 -3];>> rank(A)ans =3[X,lamda]=eig(A)X =0.8013 -0.1094 -0.16060.3638 -0.6564 0.86690.4749 0.7464 -0.4719lamda =9.7326 0 00 -3.2928 00 0 1.5602(3)矩阵的开方>> B=sqrtm(A)B =2.2447 + 0.2706i 0.6974 - 0.1400i 0.9422 - 0.3494i -0.5815 + 1.6244i 2.1005 - 0.8405i 1.7620 - 2.0970i1.9719 - 1.8471i -0.3017 + 0.9557i 0.0236 +2.3845i (4)矩阵的指数与对数:> C=expm(A)C =1.0e+004 *1.0653 0.5415 0.63230.4830 0.2465 0.28760.6316 0.3206 0.3745>> logm(C)ans =6.0000 3.0000 4.0000-2.0000 5.0000 7.00008.0000 -1.0000 -3.0000(6)矩阵的转置D=A'D =6 -2 83 5 -14 7 -3(7)矩阵的提取与翻转:通过各种特定函数如triu(A)、tril(A),diag(A)、flipud (A)、fliplr(A)等等。
matlab2022实验2参考答案报告名称:MATLAB试验二符号计算姓名:学号:专业:班级:MATLAB实验二MATLAB符号计算试验报告说明:1做试验前请先预习,并独立完成试验和试验报告。
2报告解答方式:将MATLAB执行命令和最后运行结果从命令窗口拷贝到每题的题目下面,请将报告解答部分的底纹设置为灰色,以便于批阅。
3在页眉上写清报告名称,学生姓名,学号,专业以及班级。
3报告以Word文档书写。
一目的和要求1熟练掌握MATLAB符号表达式的创建2熟练掌握符号表达式的代数运算3掌握符号表达式的化简和替换4熟练掌握符号微积分5熟练掌握符号方程的求解二试验内容1多项式运算(必做)1.1解方程:f(某)=某^4-10某某^3+34某某^2-50某某+25=0%采用数值方法:>>f=[1-1034-5025];>>root(f)%采用符号计算方法:f1=ym('某^4-10某某^3+34某某^2-50某某+25')olve(f1)1.2求有理分式R=(3某^3+某)(某^3+2)/((某^2+2某-2)(5某^3+2某^2+1))的商多项式和余多项式.a1=[3010];a2=[1002];a=conv(a1,a2);b1=[12-2];b2=[5201];b=conv(b1,b2);[p,r]=deconv(a,b);%注意:ab秩序不可颠倒。
%reidue用于实现多项式的部分分式展开,此处用deconv函数报告名称:MATLAB试验二符号计算姓名:学号:专业:班级:%%此题,有同学程序如下:某1=[3010],某2=[1002],某3=[12-2],某4=[5201]某5=conv(某1,某2)[y6,r]=deconv(某5,某3)R=deconv(y6,某4)%%这种方法较第一种解法缺点:在除法运算中,会产生误差,故此题应先将分母的多项式相乘后,再与分子部分的多项式进行运算。
MATLAB实验二李彤自动化04班学号:201041803042一、实验目的:1. Learn to design branch and loop statements program2. Be familiar with relational and logical operators3. Practice 2D plotting二、实验内容:1. Assume that a,b,c, and d are defined, and evaluate the following expression.a=20; b=-2; c=0; d=1;(1)a>b; 1(2)b>d; 0(3)a>b&c>d; 0(4)a==b; 0(5)a&b>c; 0(6)~~b; 1a=2; b=[1 -2;-0 10]; c=[0 1;2 0]; d=[-2 1 2;0 1 0];(1)~(a>b)0 00 1(2)a>c&b>c;1 00 1⑶ c<=d;??? Error using ==> <= Matrix dimensions must agree. a=2; b=3; c=10; d=0;(9)a*b^2>a*c ; 0(10)d|b>a; 1(11)(d|b)>a; 0(12)isinf(a/b) ; 0(13)isinf(a/c) ; 1(14)a>b&ischar(d) ; 1(15)isempty(c); 02. Write a Matlab program to solve the function1()ln1y xx=-, where x is a number <1. Use an if structure toverify that the value passed to the program is legal. If the value of x is legal, caculate y(x). If not ,write a suitable error message and quit.Solution:x=input('Enter the coefficient x='); if x<1y=log(1/(1-x));fprintf('y=%f\n',y)elsedisp('The value of x is illegal');endEnter the coefficient x=0.1 y=0.105361Enter the coefficient x=1 The value of x is illegal!3. Write out m. file and plot the figures with grids1Assume that the complex function f(t) is defined by the equationf(t)=(0.5-0.25i)t-1.0Plot the amplitude and phase of function for 0 4.t ≤≤ Solution: t=0:0.1:4;x=sqrt((0.5.*t-1).^2+(0.25.*t).^2); y=atan((0.25.*t)./(1-0.5.*t)); plot(t,x); hold on;plot(t,y);4. Write the Matlab statements required to calculate y(t) from the equation22350()350t t y t t t -+≥⎧=⎨+<⎩for value of t between –9 and 9 in steps of 0.5. Use loops and branches to perform this calculation. Solution:for t=-9:0.5:9; if t>=0y=-3*t^2+5; elsey=3*t^2+5; endfprintf('y=%f\n',y); endy=248.000000 y=221.750000 y=197.000000 y=173.750000 y=152.000000 y=131.750000 y=113.000000 y=95.750000 y=80.000000 y=65.750000y=53.000000 y=41.750000 y=32.000000 y=23.750000 y=17.000000 y=11.750000 y=8.000000 y=5.750000 y=5.000000 y=4.250000y=2.000000 y=-1.750000 y=-7.000000 y=-13.750000 y=-22.000000 y=-31.750000 y=-43.000000 y=-55.750000 y=-70.000000 y=-85.750000y=-103.000000 y=-121.750000 y=-142.000000 y=-163.750000 y=-187.000000 y=-211.750000 y=-238.0000005. Write an m.file to evalue the equation 2()32y x x x =-+for all values of x between 0.1 and 3, insteps of 0.1. Do this twice, once with a for loop and once with vectors. Plot the resulting function using a 4.0 thick dashed blue line.Solution:for x=0.1:0.1:3; y=x.^2-3*x+2; plot(x,y,’bo ’); hold on; endx=0.1:0.1:3; y=x.^2-3*x+2;plot(x,y,'b--','LineWidth',4.0);。
实验二 Matlab 基本操作(二)一 实验目的:1. 掌握矩阵方程的构造和运算方法2. 掌握基本Matlab 控制语句3. 学会使用Matlab 绘图二 实验内容1. 求解下列线性方程,并进行解的验证:⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡----1323151122231592127x=⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡-0174 >> a=[7 2 1 -2;9 15 3 -2;-2 -2 11 5;1 3 2 13];b=[4;7;-1;0];x=a\b2、进行下列计算。
(1)k=∑=6322i i>>i=2:63;mysum=sum(2.^i)mysum =1.8447e+019(2)求出y=x*sin(x)在0<x<100条件下的每个峰值。
>>y='x.*sin(x)';fplot(y,[0 100]);min=fmin(y,0,100)min =54.99613、绘制下列图形。
(1)sin(1/t), -1<t<1;t=-1:0.02:1;y=sin(1./t);plot(t,y)(2)1-)7(cos 3t>> t=0:0.02:pi.*3;y=1-cos(7*t).^3;plot(t,y)4、已知系统闭环传递函数G (S ),分析系统稳定性及单位脉冲、单位阶跃响应。
22s 43206s 266)S (G s s s s s 23423+++++++=>> a=[1 3 4 2 2];b=[6 26 6 20];roots(a)ans =-1.4734 + 1.0256i-1.4734 - 1.0256i-0.0266 + 0.7873i-0.0266 - 0.7873i因为无右根,故系统稳定。
当单位脉冲输入时:>> [r p k]=residue(b,a);t=0:0.2:60;>> y1=r(1)*exp(p(1)*t)+r(2)*exp(p(2)*t)+r(3)*exp(p(3)*t)+r(4)*exp(p(4)*t); >> plot(t,y1)当输入单位阶跃函数时:>> a=[1 3 4 2 2 0];b=[6 26 6 20];[r p k]=residue(b,a);t=0:0.2:100;y2=r(1)*exp(p(1)*t)+r(2)*exp(p(2)*t)+r(3)*exp(p(3)*t)+r(4)*exp(p(4)*t)+r(5); plot(t,y2)。
实验一 Matlab基础知识一、实验目的:1.熟悉启动和退出Matlab的方法。
2.熟悉Matlab命令窗口的组成。
3.掌握建立矩阵的方法。
4.掌握Matlab各种表达式的书写规则以及常用函数的使用。
二、实验内容:1.求[100,999]之间能被21整除的数的个数。
(rem)2.建立一个字符串向量,删除其中的大写字母。
(find)3.输入矩阵,并找出其中大于或等于5的元素。
(find)4.不采用循环的形式求出和式6312ii=∑的数值解。
(sum)三、实验步骤:●求[100,199]之间能被21整除的数的个数。
(rem)1.开始→程序→Matlab2.输入命令:»m=100:999;»p=rem(m,21);»q=sum(p==0)ans=43●建立一个字符串向量,删除其中的大写字母。
(find)1.输入命令:»k=input('’,’s’);Eie48458DHUEI4778»f=find(k>=’A’&k<=’Z’);f=9 10 11 12 13»k(f)=[ ]K=eie484584778●输入矩阵,并找出其中大于或等于5的元素。
(find)1.输入命令:»h=[4 8 10;3 6 9; 5 7 3];»[i,j]=find(h>=5)i=3 j=11 22 23 21 32 3●不采用循环的形式求出和式的数值解。
(sum)1.输入命令:»w=1:63;»q=sum(2.^w)q=1.8447e+019实验二 Matlab 基本程序一、 实验目的:1. 熟悉Matlab 的环境与工作空间。
2. 熟悉M 文件与M 函数的编写与应用。
3. 熟悉Matlab 的控制语句。
4. 掌握if,switch,for 等语句的使用。
二、 实验内容:1. 根据y=1+1/3+1/5+……+1/(2n-1),编程求:y<5时最大n 值以及对应的y 值。
实验二数据操作和简单编程实验要求:为达到理想的实验效果,同学们务必做到:(1)实验前认真准备,要根据实验目的和实验内容,复习好实验中可能要用到的命令,想好编程的思路,做到胸有成竹,提高上机效率。
(2)实验过程中积极思考,要深入分析命令、程序的执行结果以及各种屏幕信息的含义、出现的原因并提出解决办法。
(3)实验后认真总结,要总结本次实验有哪些收获,还存在哪些问题,并写出实验报告。
实验报告应包括实验目的、实验内容、流程图(较大程序)、程序(命令)清单、运行结果以及实验的收获与体会等内容。
同学们在上机过程中会碰到各种各样的问题,分析问题和解决问题的过程就是积累经验的过程。
只要同学们按照上面3点要求去做,在学完本课程后就一定会有很大的收获。
实验目的:1.掌握MATLAB各种表达式的书写规则及常用函数的使用2.掌握建立和执行M文件的方法3.掌握利用if,while,for等变成语句实现的方法实验内容:一、读程序读课本上的例4.2,4.3,4.4,4.5,4.7,4.8,4.10,4.11的程序,并输入和调试运行二、数据操作练习1、练习基本数学函数2、利用M文件建立大矩阵3、利用冒号表达式建立一个向量e1:e2:e3 其中e1为初始值,e2为步长,e3为终止值linspace(a,b,n) 其中a和b是生成向量的第一个和最后一个元素,n是元素总数。
4、大矩阵可由方括号中的小矩阵或向量建立起来。
例如,A=[1,2,3;4,5,6;7,8,9];C=[A,eye(size(A));ones(size(A)),A]5、矩阵的拆分1)通过下标引用矩阵的元素,例如,A(3,2)=82)采用矩阵元素的序号来引用矩阵元素A=[1,2,3;4,5,6];A(3) A(:)3)利用冒号表达式获得子矩阵C(:,3)C(2,:)C(2:4;3:end)C([1,4],3:end)4)试验搜集了27个数据,存储在A中,如下:A=[101,102,103,104,105,106,107,108,109, 201,202,203,204,205,206,207,208,209,301,302,303,304,305,306,307,308,309];观察发现前10个数据和后面7个才是正确的,需要把他们组合成一组新的数据。
Experiment 1. Fundamental Knowledge of Matlab (II) 【Experimental Purposes】1、熟悉并掌握MATLAB的工作环境。
2、运行简单命令,实现数组及矩阵的输入输出,了解在MATLAB下如何绘图。
【Experimental Principle】1. VectorsLet's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between brackets, and set it equal to a variable. For example, to create the vector a, enter into the MATLAB command window (you can "copy" and "paste" from your browser into MATLAB to make it easy):a = [1 2 3 4 5 6 9 8 7]MATLAB should return:a =1 2 3 4 5 6 9 8 7To generate a series that does not use the default of incrementing by 1, specify an additional value with the colon operator (first:step:last). In between the starting and ending value is a step value that tells MATLAB how much to increment (or decrement, if step is negative) between each number it generates. To generate a vector with elements between 0 and 20, incrementing by 2(this method is frequently used to create a time vector), uset = 0:2:20t =0 2 4 6 8 10 12 14 16 18 20Manipulating vectors is almost as easy as creating them. First, suppose you would like to add 2 to each of the elements in vector 'a'. The equation for that looks like:b = a + 2b =3 4 5 6 7 8 11 10 9Now suppose, you would like to add two vectors together. If the two vectors are the same length, it is easy. Simply add the two as shown below:c = a + bc =4 6 8 10 12 14 20 18 16Subtraction of vectors of the same length works exactly the same way.MATLAB sometimes stores such a list in a matrix with just one row, and other times in a matrix with just one column. In the first instance, such a 1-row matrix is called a row-vector; in thesecond instance, such a 1-column matrix is called a column-vector. Either way, these are merely different ways for storing vectors, not different kinds of vectors.2. Matrices and ArraysThe most basic MATLAB data structure is the matrix: a two-dimensional, rectangularly shaped data structure capable of storing multiple elements of data in an easily accessible format. These data elements can be numbers, characters, logical states of true or false, or even other MATLAB structure types. MATLAB uses these two-dimensional matrices to store single numbers and linear series of numbers as well. In these cases, the dimensions are 1-by-1 and 1-by-n respectively, where n is the length of the numeric series. MATLAB also supports data structures that have more than two dimensions. These data structures are referred to as arrays in the MATLAB documentation.MATLAB is a matrix-based computing environment. All of the data that you enter into MATLAB is stored in the form of a matrix or a multidimensional array. Even a single numeric value like 100 is stored as a matrix (in this case, a matrix having dimensions 1-by-1):A = 100;Name Size Bytes ClassA 1x1 8 double arrayEntering matrices into MATLAB is the same as entering a vector, except each row of elements is separated by a semicolon (;) or a return:B = [1 2 3 4;5 6 7 8;9 10 11 12]B =1 2 3 45 6 7 89 10 11 12B = [ 1 2 3 45 6 7 89 10 11 12]B =1 2 3 45 6 7 89 10 11 12The square brackets operator constructs two-dimensional matrices only, (including 0-by-0, 1-by-1, and 1-by-n matrices).Matrices in MATLAB can be manipulated in many ways. For one, you can find the transpose of a matrix using the apostrophe key:C = B'C =1 5 92 6 103 7 114 8 12It should be noted that if C had been complex, the apostrophe would have actually given the complex conjugate transpose. To get the transpose, use .' (the two commands are the same if the matix is not complex).Now you can multiply the two matrices B and C together. Remember that order matters when multiplying matrices.D = B * CD =30 70 11070 174 278110 278 446D= C * BD =107 122 137 152122 140 158 176137 158 179 200152 176 200 224Another option for matrix manipulation is that you can multiply the corresponding elements of two matrices using the .* operator (the matrices must be the same size to do this).E = [1 2;3 4]F = [2 3;4 5]G = E .* FE =1 23 4F =2 34 5G =2 612 20If you have a square matrix, like E, you can also multiply it by itself as many times as you like by raising it to a given power.E^3ans =37 5481 118If wanted to cube each element in the matrix, just use the element-by-element cubing.E.^3ans =1 827 64You can also find the inverse of a matrix:X = inv(E)X =-2.0000 1.00001.5000 -0.5000or its eigenvalues:eig(E)ans =-0.37235.3723There is even a function to find the coefficients of the characteristic polynomial of a matrix. The "poly" function creates a vector that includes the coefficients of the characteristic polynomial.p = poly(E)p =1.0000 -5.0000 -2.0000Remember that the eigenvalues of a matrix are the same as the roots of its characteristic polynomial:roots(p)ans =5.3723-0.37233. FunctionsTo make life easier, MATLAB includes many standard functions. Each function is a block of code that accomplishes a specific task. MATLAB contains all of the standard functions such as sin, cos, log, exp, sqrt, as well as many others. Commonly used constants such as pi, and i or j for the square root of -1, are also incorporated into MATLAB.sin(pi/4)ans =0.7071To determine the usage of any function, type help [function name] at the MATLAB command window. MATLAB even allows you to write your own functions with the function command; follow the link to learn how to write your own functions and see a listing of the functions we created for this tutorial.4. PlottingIt is also easy to create plots in MATLAB. Suppose you wanted to plot a sine wave as a function of time. First make a time vector (the semicolon after each statement tells MATLAB we don't want to see all the values) and then compute the sin value at each time.t=0:0.25:7;y = sin(t);plot(t,y)The plot contains approximately one period of a sine wave. Basicplotting is very easy in MATLAB, and the plot command hasextensive add-on capabilities. I would recommend you visit theplotting page to learn more about it.5. PolynomialsIn MATLAB, a polynomial is represented by a vector. To create a polynomial in MATLAB, simply enter each coefficient of the polynomial into the vector in descending order. For instance, let's say you have the following polynomial:43231529s s s s +--+ To enter this into MATLAB, just enter it as a vector in the following mannerx = [1 3 -15 -2 9]x =1 3 -15 -2 9MATLAB can interpret a vector of length n+1 as an nth order polynomial. Thus, if yourpolynomial is missing any coefficients, you must enter zeros in the appropriate place in the vector. For example,41s + would be represented in MA TLAB as:y = [1 0 0 0 1]You can find the value of a polynomial using the polyval function. For example, to find the value of the above polynomial at s=2,z = polyval([1 0 0 0 1],2)z =17You can also extract the roots of a polynomial. This is useful when you have a high-order polynomial such as43231529s s s s +--+ Finding the roots would be as easy as entering the following command;roots([1 3 -15 -2 9])ans =-5.57452.5836-0.79510.7860Let's say you want to multiply two polynomials together. The product of two polynomials is found by taking the convolution of their coefficients. MATLAB's function conv that will do this for you.x = [1 2];y = [1 4 8];z = conv(x,y)z =1 6 16 16Dividing two polynomials is just as easy. The deconv function will return the remainder as well as the result. Let's divide z by y and see if we get x.[xx, R] = deconv(z,y)xx =1 2R =0 0 0 0As you can see, this is just the polynomial/vector x from before. If y had not gone into z evenly, the remainder vector would have been something other than zero.If you want to add two polynomials together which have the same order, a simple z=x+y will work (the vectors x and y must have the same length). In the general case, the user-defined function, polyadd can be used. To use polyadd, copy the function into an m-file, and then use it just as you would any other function in the MATLAB toolbox. Assuming you had the polyadd function stored as a m-file, and you wanted to add the two uneven polynomials, x and y, you could accomplish this by entering the command:z = polyadd(x,y)x =1 2y =1 4 8z =1 5 10【Experiment contents and procedure】1、运行简单命令,实现数组及矩阵的输入输出;2、运行特殊矩阵的输入及输出;MATLAB has a number of functions that create different kinds of matrices. Some create specialized matrices like the Hankel or Vandermonde matrix. The functions shown in the table below create matrices for more general use.However, you can easily build basic arrays of any numeric type using the ones, zeros, and eye functions.A = magic(5)A =17 24 1 8 1523 5 7 14 164 6 13 20 2210 12 19 21 311 18 25 2 9Note that the elements of each row, each column, and each main diagonal add up to the same value: 65.3、进行简单的MATLAB绘图;4、多项式的输入及输出。