




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、MATLAB作业第二章作业1 a=double(87); b=double(190); a+bans =277 a-bans =-103 a*bans = 16530、 a=uint8(87); b=uint8(190); a+bans = 255 a-bans = 0 a*bans = 2552、 sind(60)ans = 0.8660 exp(3)ans = 20.0855 cos(3*pi/4)ans = -0.70713、 u=2; v=3; 4*u*v/log10(v)ans = 50.3017 (exp(u)+v)2/(v2-u)ans = 15.4189 sqrt(u-3*v)
2、/(u*v)ans = 0 + 0.4410i4、(3-5i)*(4+2i)ans = 22.0000 -14.0000i sin(2-8i)ans = 1.3553e+003 +6.2026e+002i5、 4 4 4=20ans = 0 4=20ans = 1 b a=39,b=58,c=3,d=7a = 39b = 58c = 3d = 7 abans = 0 a ab&bcans = 0 a=dans = 0 a|bcans = 1 dans = 17、 a=60;s=sind(a);disp(The answer is:),disp(s);The answer is: 0.8660
3、a=3; s=exp(a); disp(The answer is:),disp(s);The answer is: 20.0855 a=3*pi/4; s=cos(a); disp(The answer is:),disp(s);The answer is: -0.70718、 a=39;b=58; c=3; d=7; s1=ab; s2=ab&bc; s4=a=d; s5=a|bc; s6=d; disp(s1 is:),disp(s1); disp(s2 is:),disp(s2);disp(s3 is:),disp(s3);disp(s4 is:),disp(s4);disp(s5 i
4、s:),disp(s5);disp(s6 is:),disp(s6);s1 is: 0s2 is: 0s3 is: 0s4 is: 0s5 is: 1s6 is: 1第三章1生成一个33随即矩阵,将其对角线元素加1 A=rand(3)A = 0.4660 0.5252 0.8381 0.4186 0.2026 0.0196 0.8462 0.6721 0.6813 diag(A+1)ans = 1.4660 1.20261.68132生成一个元素值在1和10之间的33随即矩阵,将其重新排序,使得:(1)每列按照降序排列(2)每行按照降序排列 A=round(rand(3)*10)A = 3 7
5、 9 5 4 6 2 9 5 A1=sort(A,descend)A1 = 5 9 9 3 7 6 2 4 5 A2=sort(A,2,descend)A2 = 9 7 3 6 5 4 9 5 23令啊3, X 2 1; 3 2 Y=4 5; 6 7进行幂运算,计算:(1)aX (2)Xa (3)XY a=3,X=2 1; 3 2, Y=4 5;6 7a = 3X = 2 1 3 2Y = 4 5 6 7 aXans = 30.8436 17.0326 51.0978 30.8436 Xaans = 26 15 45 26 X.Yans = 16 1 729 1284生成33随机矩阵,判断其元
6、素是否大于0.5 A=rand(3)A = 0.7271 0.5681 0.5466 0.3093 0.3704 0.4449 0.8385 0.7027 0.6946 L=abs(A)0.5L = 1 1 1 0 0 0 1 1 15.有a3,B4 3;7 5,C=8 9;7 6,D=4 7;12 8,进行如下操作。(1)(aC&BD (3)C a=3,B=4 3; 7 5,C=8 9;7 6,D=4 7;12 8a = 3B = 4 3 7 5C = 8 9 7 6D = 4 7 12 8 (a aC&B C A=1 5 3, B=2 1 4,C=9 -1 5A = 1 5 3B = 2
7、1 4C = 9 -1 5 dot(A,B)ans = 19 A*Bans = 19 cross(B,C)ans = 9 26 -11 dot(A,cross(B,C)ans = 1062.令A2 -1; -2 -2,B=2 -3;0 -4,C=1;2,;D=1 0;0 1.求解下列问题1)2A 2)AB 3)AB 4)A.*B 5)BC 6)A/B 7)AB A=2 -1; -2 -2,B=2 -3;0 -4,C=1;2,D=1 0;0 1A = 2 -1 -2 -2B = 2 -3 0 -4C = 1 2D = 1 0 0 1 2*Aans = 4 -2 -4 -4 A+Bans = 4
8、-4 -2 -6 A*Bans = 4 -2 -4 14 A.*Bans = 4 3 0 8 B*Cans = -4 -8 A/Bans = 1.0000 -0.5000 -1.0000 1.2500 ABans = 0.6667 -0.3333 -0.6667 2.33333.9 8 8 9;2 7 4 7;6 4 6 1;4 0 7 4求该矩阵的特征值和特征向量。A=9 8 8 9;2 7 4 7;6 4 6 1;4 0 7 4A = 9 8 8 9 2 7 4 7 6 4 6 1 4 0 7 4 lambda=eig(A)lambda = 21.4707 1.1178 + 4.5139i
9、 1.1178 - 4.5139i 2.2938 4, 生成 5 阶魔术矩阵,记为,对其进行如下操作(1)求的逆(2)计算的行列式(3)求的条件数(4)求矩阵的秩(5)求矩阵的迹 A=magic(5)A = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 inv(A)ans = -0.0049 0.0512 -0.0354 0.0012 0.0034 0.0431 -0.0373 -0.0046 0.0127 0.0015 -0.0303 0.0031 0.0031 0.0031 0.0364 0.0047 -
10、0.0065 0.0108 0.0435 -0.0370 0.0028 0.0050 0.0415 -0.0450 0.0111 det(A)ans = 5.0700e+006 rank(A)ans = 5 cond(A)ans = 5.4618 trace(A)ans = 655, X=9 0 1 2;9 3 2 1;4 8 1 0;8 0 6 7; L,U=lu(X)L = 1.0000 0 0 0 1.0000 0.3750 0.1549 1.0000 0.4444 1.0000 0 0 0.8889 0 1.0000 0U = 9.0000 0 1.0000 2.0000 0 8.00
11、00 0.5556 -0.8889 0 0 5.1111 5.2222 0 0 0 -1.4755 Q,R=qr(X)Q = -0.5785 0.2866 0.5381 -0.5418 -0.5785 -0.1052 0.2182 0.7788 -0.2571 -0.9175 -0.0818 -0.2921 -0.5143 0.2548 -0.8100 -0.1206R = -15.5563 -3.7927 -5.0783 -5.3354 0 -7.6561 0.6871 2.2512 0 0 -3.9672 -4.3754 0 0 0 -1.1492V,D=eig(X)V = Columns
12、 1 through 3 0.3650 -0.1238 - 0.1819i -0.1238 + 0.1819i 0.4317 -0.2224 + 0.2205i -0.2224 - 0.2205i 0.3764 -0.2020 + 0.5806i -0.2020 - 0.5806i 0.7340 0.6897 0.6897 Column 4 -0.0337 0.2755 -0.7751 0.5676 D = Columns 1 through 3 14.0539 0 0 0 3.8077 + 2.9407i 0 0 0 3.8077 - 2.9407i 0 0 0 Column 4 0 0 0
13、 -1.6693 U,S,V=svd(X)U = -0.4891 -0.1120 -0.6156 -0.6077 -0.5268 0.2260 -0.3527 0.7396 -0.3136 0.8179 0.3860 -0.2893 -0.6204 -0.5171 0.5896 -0.0027S = 17.8325 0 0 0 0 8.3092 0 0 0 0 4.8357 0 0 0 0 0.7578V = -0.8614 0.0194 -0.5075 0.0109 -0.2293 0.8691 0.4198 -0.1262 -0.3128 -0.2341 0.5382 0.7468 -0.
14、3279 -0.4354 0.5259 -0.65296比较稀疏矩阵与满矩阵的异同之处,如eye(10)与speye(10)生成矩阵的异同之处。 eye(10),speye(10)ans = 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1ans =
15、 (1,1) 1 (2,2) 1 (3,3) 1 (4,4) 1 (5,5) 1 (6,6) 1 (7,7) 1 (8,8) 1 (9,9) 1 (10,10) 1Eye(10)生成的矩阵除对角线元素全部为1外,其余元素都为0;而speye(10)生成矩阵只限制了对较元素全为1,并未规定其余元素的值。7将10阶随机矩阵转换为稀疏矩阵。 A=ceil(rand(10)*5)A = 4 3 5 5 5 1 1 1 4 4 1 1 2 3 5 1 3 2 2 1 3 3 2 2 2 5 5 1 1 5 3 4 1 2 1 3 3 4 3 3 2 5 1 2 5 5 5 4 4 4 1 2 4 2 2
16、 4 3 5 4 2 4 2 1 3 4 4 3 3 5 3 4 5 5 1 5 2 3 3 5 5 4 2 1 1 4 1 3 1 2 2 3 5 1 3 5 1 5 4 4 4 s=sparse(A)s = (1,1) 4 (2,1) 1 (3,1) 3 (4,1) 3 (5,1) 2 (6,1) 1 (7,1) 4 (8,1) 4 (9,1) 4 (10,1) 3 (1,2) 3 (2,2) 1 (3,2) 3 (4,2) 4 (5,2) 5 (6,2) 2 (7,2) 2 (8,2) 5 (9,2) 2 (10,2) 5 (1,3) 5 (2,3) 2 (3,3) 2 (4,3) 1
17、(5,3) 1 (6,3) 4 (7,3) 1 (8,3) 5 (9,3) 1 (10,3) 1 (1,4) 5 (2,4) 3 (3,4) 2 (4,4) 2 (5,4) 2 (6,4) 2 (7,4) 3 (8,4) 1 (9,4) 1 (10,4) 3 (1,5) 5 (2,5) 5 (3,5) 2 (4,5) 1 (5,5) 5 (6,5) 2 (7,5) 4 (8,5) 5 (9,5) 4 (10,5) 5 (1,6) 1 (2,6) 1 (3,6) 5 (4,6) 3 (5,6) 5 (6,6) 4 (7,6) 4 (8,6) 2 (9,6) 1 (10,6) 1 (1,7) 1
18、(2,7) 3 (3,7) 5 (4,7) 3 (5,7) 5 (6,7) 3 (7,7) 3 (8,7) 3 (9,7) 3 (10,7) 5 (1,8) 1 (2,8) 2 (3,8) 1 (4,8) 4 (5,8) 4 (6,8) 5 (7,8) 3 (8,8) 3 (9,8) 1 (10,8) 4 (1,9) 4 (2,9) 2 (3,9) 1 (4,9) 3 (5,9) 4 (6,9) 4 (7,9) 5 (8,9) 5 (9,9) 2 (10,9) 4 (1,10) 4 (2,10) 1 (3,10) 5 (4,10) 3 (5,10) 4 (6,10) 2 (7,10) 3 (8
19、,10) 5 (9,10) 2 (10,10) 48将10阶稀疏正态随机矩阵转换为满矩阵。 A=sprandn(eye(10)A = (1,1) -1.3362 (2,2) 0.7143 (3,3) 1.6236 (4,4) -0.6918 (5,5) 0.8580 (6,6) 1.2540 (7,7) -1.5937 (8,8) -1.4410 (9,9) 0.5711 (10,10) -0.3999 full(A)ans = Columns 1 through 7 -1.3362 0 0 0 0 0 0 0 0.7143 0 0 0 0 0 0 0 1.6236 0 0 0 0 0 0 0
20、 -0.6918 0 0 0 0 0 0 0 0.8580 0 0 0 0 0 0 0 1.2540 0 0 0 0 0 0 0 -1.5937 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 8 through 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1.4410 0 0 0 0.5711 0 0 0 -0.3999第五章作业1计算下列积分1)y=(x)(x+x.3+x.5); q=quad(y,-1,1)q = 2.2204e-0162)y=(x)(sin(x)+cos(x); q=qu
21、ad(y,1,10)q = -0.00613)y=(x)(exp(x/2); quad(y,2,6)ans = 34.73454)y=(x)(x./(x.4+4); quad(y,1,10)ans =0.27185)f=(x,y)sin(y)*(x+y)./(x.2+4); dblquad(f,1,10,1,10)ans =5.52546) f=(x,y)y*(x+y)./4; dblquad(f,1,y,1,10)ans = 1.0057e+0057 f=(x,y,z)y*(x+y)./4; triplequad(f,1,y,1,10,0,3)ans = 3.0172e+0052,求下列函数
22、极值1) z = (x)x(1)2-(x(2)-1)2; x,fvalue,flag,output=fminsearch(z,1,2) Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: -27150778911470467000000000000000000000000000000000000000000000000000000000000000000000000.000000 x = 1.0e+044
23、* -1.2137 2.0465fvalue = -2.7151e+088flag = 0output = iterations: 200 funcCount: 401 algorithm: Nelder-Mead simplex direct search message: 1x238 char2) z = (x)(x(1)-x(2)+1)2; x,fvalue,flag,output=fminsearch(z,5,10)x = 6.3273 7.3273fvalue = 4.4480e-023flag = 1output = iterations: 65 funcCount: 116 al
24、gorithm: Nelder-Mead simplex direct search message: 1x196 char3) z = (x)sin(x(1)+cos(x(2)+cos(x(1)-x(2); x,fvalue,flag,output=fminsearch(z,pi,pi/2)x = 5.2360 2.6180fvalue = -2.5981flag = 1output = iterations: 43 funcCount: 82 algorithm: Nelder-Mead simplex direct search message: 1x196 char3,求下列函数的解,
25、并会制图形1)y=(x)exp(x)-x.5; x1,y1=fzero(y,8)x1 = 12.7132y1 = 0 fplot(y,x1-3,x1+3) hold on plot(x1,y1,k*)2)y=(x)x*sin(x); x1,y1=fzero(y,0,pi)x1 = 0y1 = 0 fplot(y,x1-3,x1+3) hold on plot(x1,y1,k*)4,有如下数据X1.4Y1.000001.233681.552711.993722.61170x=1 1.1 1.2 1.3 1.4; y=1.00000 1.23368 1.55271 1.9937
26、2 2.61170;length_of_x=length(x); scalar_x=x(1):0.05:x(length_of_x); length_of_sx=length(scalar_x); y_nearest = zeros(length_of_sx,1); y_linear = zeros(length_of_sx,1); y_spline = zeros(length_of_sx,1); y_cubic = zeros(length_of_sx,1); for i=1:length_of_sx y_nearest(i)=interp1(x,y,scalar_x(i),nearest
27、); y_linear(i) =interp1(x,y,scalar_x(i),linear); y_spline(i) =interp1(x,y,scalar_x(i),spline); y_cubic(i) =interp1(x,y,scalar_x(i),cubic);end subplot(2,2,1),plot(x,y,r*),hold on,plot(scalar_x,y_nearest),title(method=nearest); subplot(2,2,2),plot(x,y,b*),hold on,plot(scalar_x,y_linear),title(method=l
28、inear); subplot(2,2,3),plot(x,y,g*),hold on,plot(scalar_x,y_spline),title(method=spline); subplot(2,2,4),plot(x,y,y*),hold on,plot(scalar_x,y_cubic),title(method=cubic); 第六章作业1,编制一个脚本,查找给定字符串中字符出现的次数和位置。% find the number and position of the letter in the stringstr=I am Guozhiqiang; % The string to b
29、e searchedx=i; % The letter to be searched fordouble(position);double(number);double position=findstr(str,x); %Find the position of the letterdouble number=length(position); % The total length of the letterdisp(position);disp(number); 11 13 22,编写一个脚本,判断输入字符串中每个单词的首字母是否为大写,若不是则将其修改为大写,其他字母为小写% To fin
30、d whether the first letter of each word of the str is capital % letter,if not correct itstr= i am guozhiqiang; % The string to be judgeddouble(length)double length=length(str);for i=1:length if (i=1|isspace(str(i-1)&(str(i)=a&str(i) A(1,2)=I study in Tianjin University; A(2,1)=5; A(2,2)=6.900 A = I
31、am Guozhiqiang I study in Tianjin University 5 6.9000 cellplot(A)4,创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级学生成绩进行管理,如计算总分、平均分,排列名次等。person=struct(name,number,mark,class,); person(1).name=Guozhiqiang; person(2).name=xudan; person(3).mark=95; person(4).number=3008204066; personperson = 1x4 str
32、uct array with fields: name number markclass第七章作业1,叙述脚本式M文件与函数式M文件的异同 脚本文件允许用户将Matlab指令输入到一个简单的文本文件中,再用Matlab打开该文件,执行其中的指令,这些指令执行的结果和用用户在Matlab提示符下输入指令执行的结果完全一样,这些文件就是文本文件。如将该M文件存入Matlab目录下,就可以在command窗口中直接输入脚本名执行命令,该M文件中的命令可以访问Matlab工作区中的所有变量,并且这个M文件生成的所有变量也成为了Matlab工作区中的一部分 函数M文件,类似于脚本文件,不是在命令窗口中输
33、入,而是用文本编辑器生成的一个外部文本文件。但函数M文件又区别于脚本文件,一个函数与Matlab的工作区的通信只能通过传递给函数的变量和这个函数生成的变量来进行。在这个函数中的内部变量不会显示,也不会与Matlab工作区产生相互影响。2,叙述Matlab中都有哪些变量类型及这些变量类型的特点。局部变量,全局变量,永久变量。局部变量:每个函数都有自己的局部变量,这些变量只能在定义它的函数内部使用。当函数运行时,局部变量保存在函数的工作空间中,一旦函数退出,这些局部变量将不复存在。全局变量:在函数或基本工作空间内,用global声明的变量为全局变量。永久变量:永久变量用persistent声明,只
34、能在M文件函数中定义和使用,只允许声明它的函数存取。当声明它的函数退出时,MATLAB不会从内存中清除它。3, 第一次循环完成,x=10;第二次循环完成,x=5050;第三次循环完成,x=196;第四次循环完成,x=5004, fid=fopen(magic.m,r); count=0; whilefeof(fid)line=fgetl(fid)if isempty(line)|strncmp(line,%,1)continueendcount=count+1;endline =function M = magic(n)line =%MAGIC Magic square.line =% MAGIC(N) is an N-by-N matrix constructed from the integersline =% 1 through N2 with equal row, column, and diagonal sums.line =% Produces valid magic squares for all N 0 except N = 2.line = line =% Copyright 1984-2010 The MathWorks, Inc. line =% $Revision: $ $Date: 201
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 IEC 63297:2025 EN-FR Sensing devices for non-intrusive load monitoring (NILM) systems
- 【正版授权】 IEC 63119-1:2025 EN Information exchange for electric vehicle charging roaming service - Part 1: General
- 华科版五年级信息技术课件
- 海南省小升初数学试卷
- 怀化市高三联考数学试卷
- 济南初三中考数学试卷
- 健康管理学概论课件
- 2024-2030年中国浙江省房地产行业市场调查研究及投资前景展望报告
- 中国香熏炉行业市场发展前景及发展趋势与投资战略研究报告(2024-2030)
- 中国榉木行业市场发展前景及发展趋势与投资战略研究报告(2024-2030)
- 2025年项目管理专业资格考试试题及答案
- 非公企业党建培训课件
- 2025区域型变电站智能巡视系统技术规范
- 汛期公交安全课件
- 环境影响管理方案(3篇)
- 财务报表编制与审核合同模板
- 上海闵行区教育系统招聘实验员考试真题2024
- 建设部建设工程重大质量安全事故应急预案
- 2025年中航油招聘笔试参考题库附带答案详解
- 2024年中国中高端电子铝箔行业市场调查报告
- DB54∕T 0275-2023 民用建筑节能技术标准
评论
0/150
提交评论