1、同济大学matlab课程练习题答案关晓飞第一章 初识matlab题目1x1=input(x1=);x2=input(x2=);x3=input(x3=);x4=input(x4=);x5=input(x5=);x6=input(x6=);x7=input(x7=);a=x1/x5;b=x3/(x2-x1)0.85;c=1-0.36*(x4/x2)(-0.56);d=2.62*c1.5;e=(x4/x2)1.16;f=(1-d*e)/(x6*x7)0.5;y=174.42*a*b*f题目2略题目3 help lookfor lookfor Search all MATLAB files for
2、keyword. lookfor XYZ looks for the string XYZ in the first comment line (the H1 line) of the HELP text in all MATLAB files found on MATLABPATH (including private directories). For all files in which a match occurs, lookfor displays the H1 line. For example, lookfor inverse finds at least a dozen mat
3、ches, including the H1 lines containing inverse hyperbolic cosine two-dimensional inverse FFT, and pseudoinverse. Contrast this with which inverse or what inverse, which run more quickly, but which probably fail to find anything because MATLAB does not ordinarily have a function inverse. lookfor XYZ
4、 -all searches the entire first comment block of each MATLAB file. In summary, WHAT lists the functions in a given directory, WHICH finds the directory containing a given function or file, and lookfor finds all functions in all directories that might havesomething to do with a given key word.题目4n=in
5、put(n=);sum(1:n)prod(1:n)题目5 linspace(0,360,9) ans = 0 45 90 135 180 225 270 315 360题目7n=input(n=)format bankn第二章矩阵的建立题目1.spiral产生螺旋矩阵例:spiral(3)ans= 7 8 9 6 1 2 5 4 3题目2.(1)1 2;3 4+10-2ians = 11.0000 - 2.0000i 12.0000 - 2.0000i 13.0000 - 2.0000i 14.0000 - 2.0000i(2)1 2;3 4.*0.1 0.2;0.3 0.4ans = 0.1
6、000 0.4000 0.9000 1.6000(3)1 2;3 4.20 10;9 2ans = 20.0000 5.0000 3.0000 0.5000(4)exp(1 2;3 4) %自然对数的1、2、3、4乘方ans = 2.7183 7.3891 20.0855 54.5982(5)1 2;3 4.2ans = 1 4 9 16(6)sum(1 2;3 4) ans = 4 6(7)prod(1 2;3 4) %每一列的所有行相加ans = 3 8(8)a,b=min(10 20;30 40) %a代表每一列的最小值,b代表该值所在的行数a = 10 20b = 1 1(9)abs(
7、1 2;3 4-pi) %绝对值ans = 2.1416 1.1416 0.1416 0.8584(10)linspace(3,4,5) %生成3到4均匀的5个数ans = 3.0000 3.2500 3.5000 3.7500 4.0000(11)A=1 2;3 4;sort(A(:,2) %提取第2列ans = 2 4题目3.A=1,2,3,4;4,3,2,1;2,3,4,1;3,2,4,1A = 1 2 3 4 4 3 2 1 2 3 4 1 3 2 4 1A(5,6)=5A = 1 2 3 4 0 0 4 3 2 1 0 0 2 3 4 1 0 0 3 2 4 1 0 0 0 0 0
8、0 0 5题目4示例:A=3 -1;-1 3A = 3 -1 -1 3X,D=eig(A)X = -0.7071 -0.7071 -0.7071 0.7071D = 2 0 0 4题目5A = 3 -1 -1 32Aans = 10 -6 -6 102.Aans = 8.0000 0.5000 0.5000 8.0000题目6(1)A=magic(6)A = 35 1 6 26 19 24 3 32 7 21 23 25 31 9 2 22 27 20 8 28 33 17 10 15 30 5 34 12 14 16 4 36 29 13 18 11c=A(2,1)c = 3(2)t = 2
9、:2:6;B = A(t,:);BB = 3 32 7 21 23 25 8 28 33 17 10 15 4 36 29 13 18 11(3)A(: , end,2:end-1,1)ans = 24 1 6 26 19 35 25 32 7 21 23 3 20 9 2 22 27 31 15 28 33 17 10 8 16 5 34 12 14 30 11 36 29 13 18 4(4)A(:,1 3 4 5 6)ans = 35 6 26 19 24 3 7 21 23 25 31 2 22 27 20 8 33 17 10 15 30 34 12 14 16 4 29 13 18
10、 11题目7ones(4,5)ans = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1rand(3,4)ans = 0.8147 0.9134 0.2785 0.9649 0.9058 0.6324 0.5469 0.1576 0.1270 0.0975 0.9575 0.9706 eye(3)ans = 1 0 0 0 1 0 0 0 1题目8A=0:99;A=reshape(A,10,10)A = 0 10 20 30 40 50 60 70 80 90 1 11 21 31 41 51 61 71 81 91 2 12 22 32 42 52 62 72
11、 82 92 3 13 23 33 43 53 63 73 83 93 4 14 24 34 44 54 64 74 84 94 5 15 25 35 45 55 65 75 85 95 6 16 26 36 46 56 66 76 86 96 7 17 27 37 47 57 67 77 87 97 8 18 28 38 48 58 68 78 88 98 9 19 29 39 49 59 69 79 89 99A=magic(3)题目9A = 8 1 6 3 5 7 4 9 2A(2,:)=A(2,:)*2A = 8 1 6 6 10 14 4 9 2A(1,:)=A(1,:)+A(3,:
12、)*(-2)A = 0 -17 2 6 10 14 4 9 2题目10A=1,3,5;5,8,3;6,1,6;B=3,6;9,5;8,2;C=3,3,9;4,0,6;D=2:6;A,Bans = 1 3 5 3 6 5 8 3 9 5 6 1 6 8 2A;Cans = 1 3 5 5 8 3 6 1 6 3 3 9 4 0 6A,B;Dans = 1 3 5 3 6 5 8 3 9 5 6 1 6 8 2 2 3 4 5 6A=ones(6,7)A = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
13、1 1 1 1 1 1 1题目11for i=1:6for j=1:7A(i,j)=i+j;endendAA = 2 3 4 5 6 7 8 3 4 5 6 7 8 9 4 5 6 7 8 9 10 5 6 7 8 9 10 11 6 7 8 9 10 11 12 7 8 9 10 11 12 13%本题还有多种方法第三章符号计算题目1略题目2 sym(0,3) ans = 0.3 sym(0.3)%符号工具箱得出更精确的解 ans = 3/10题目3 syms x s f=x5+3*x4+4*x3+2*x2+3*x+6;g=(s-1)/(s+1); f(x)=compose(f,g,x,s)
14、 f(x) = (3*(s - 1)/(s + 1) + (2*(s - 1)2)/(s + 1)2 + (4*(s - 1)3)/(s + 1)3 + (3*(s - 1)4)/(s + 1)4 + (s - 1)5/(s + 1)5 + 6 simplify(f(x)ans = 19 - (72*s4 + 120*s3 + 136*s2 + 72*s + 16)/(s + 1)5题目4 sum(sym(2).1:64) ans = 36893488147419103230题目5f=sym(cos(4*a)-4*cos(2*a)+3);simplify(f) ans = 8*sin(a)4题
15、目6 f=sym(3*x+2/3)10);expand(f) ans = 59049*x10 + 131220*x9 + 131220*x8 + 77760*x7 + 30240*x6 + 8064*x5 + (4480*x4)/3 + (5120*x3)/27 + (1280*x2)/81 + (5120*x)/6561 + 1024/59049所以系数最大的项是131220*x9 和 131220*x8题目7 syms a x factor(a*(sin(x)2-(2*a2-a+1)*sin(x)+2*a-1) ans = (sin(x) - 2*a + 1)*(a*sin(x) - 1)
16、第四章极限与导数题目1(1) syms x;lim1=limit(cos(x)-exp(-x2/2)/x4,x,0)lim1 =-1/12(2) syms x t;lim2=limit(1+2*t/x)3*x,x,inf)lim2 =Inf + imag(t)*6*i(3) syms x;lim3=limit(1/x,x,0,right)Lim3 =Inf(4) syms x;lim4=limit(2x-log(2x)-1)/(1-cos(x),x,0)Lim4 =log(2)2题目2 syms x;lim=limit(exp(x)-1-x)/x2,x,0)lim =1/2题目3 syms x
17、;lim=limit(x0.5-2(-1/x),x,0,right)lim =0题目4 syms x;lim=limit(atan(x)/(1+x2),x,pi,left)lim =atan(pi)/(pi2 + 1)题目5 y=7*x3-2*x+3;z=diff(y,2);x=1;eval(z)ans =42题目6 syms x y;z=exp(x)*sin(2*y)+log(x)*cos(2*y);f=diff(z,y)f =2*cos(2*y)*exp(x) - 2*sin(2*y)*log(x)题目7 syms x y t;x=t2*sin(t);y=2*t*cos(t);f=diff
18、(y)/diff(x)f =(2*cos(t) - 2*t*sin(t)/(t2*cos(t) + 2*t*sin(t)题目8 syms x;lim=limit(3x+9x)(1/x),x,inf)lim =9题目9 syms x;lim=limit(1+x)(1/x),x,0)lim =exp(1)题目10 syms x;y=(x-1)*(x-2)/(x-3)/(x-4)0.5;f1=diff(y,1),f2=diff(y,2),f3=diff(y,3)f1 =(x - 1)/(x - 3)*(x - 4) + (x - 2)/(x - 3)*(x - 4) - (x - 1)*(x - 2
19、)/(x - 3)*(x - 4)2) - (x - 1)*(x - 2)/(x - 3)2*(x - 4)/(2*(x - 1)*(x - 2)/(x - 3)*(x - 4)(1/2)f2 =(2/(x - 3)*(x - 4) - (2*(x - 1)/(x - 3)*(x - 4)2) - (2*(x - 1)/(x - 3)2*(x - 4) - (2*(x - 2)/(x - 3)*(x - 4)2) - (2*(x - 2)/(x - 3)2*(x - 4) + (2*(x - 1)*(x - 2)/(x - 3)*(x - 4)3) + (2*(x - 1)*(x - 2)/(
20、x - 3)2*(x - 4)2) + (2*(x - 1)*(x - 2)/(x - 3)3*(x - 4)/(2*(x - 1)*(x - 2)/(x - 3)*(x - 4)(1/2) - (x - 1)/(x - 3)*(x - 4) + (x - 2)/(x - 3)*(x - 4) - (x - 1)*(x - 2)/(x - 3)*(x - 4)2) - (x - 1)*(x - 2)/(x - 3)2*(x - 4)2/(4*(x - 1)*(x - 2)/(x - 3)*(x - 4)(3/2)f3 =(3*(x - 1)/(x - 3)*(x - 4) + (x - 2)/
21、(x - 3)*(x - 4) - (x - 1)*(x - 2)/(x - 3)*(x - 4)2) - (x - 1)*(x - 2)/(x - 3)2*(x - 4)3)/(8*(x - 1)*(x - 2)/(x - 3)*(x - 4)(5/2) - (6/(x - 3)*(x - 4)2) + 6/(x - 3)2*(x - 4) - (6*(x - 1)/(x - 3)*(x - 4)3) - (6*(x - 1)/(x - 3)2*(x - 4)2) - (6*(x - 1)/(x - 3)3*(x - 4) - (6*(x - 2)/(x - 3)*(x - 4)3) - (
22、6*(x - 2)/(x - 3)2*(x - 4)2) - (6*(x - 2)/(x - 3)3*(x - 4) + (6*(x - 1)*(x - 2)/(x - 3)*(x - 4)4) + (6*(x - 1)*(x - 2)/(x - 3)2*(x - 4)3) + (6*(x - 1)*(x - 2)/(x - 3)3*(x - 4)2) + (6*(x - 1)*(x - 2)/(x - 3)4*(x - 4)/(2*(x - 1)*(x - 2)/(x - 3)*(x - 4)(1/2) - (3*(x - 1)/(x - 3)*(x - 4) + (x - 2)/(x -
23、3)*(x - 4) - (x - 1)*(x - 2)/(x - 3)*(x - 4)2) - (x - 1)*(x - 2)/(x - 3)2*(x - 4)*(2/(x - 3)*(x - 4) - (2*(x - 1)/(x - 3)*(x - 4)2) - (2*(x - 1)/(x - 3)2*(x - 4) - (2*(x - 2)/(x - 3)*(x - 4)2) - (2*(x - 2)/(x - 3)2*(x - 4) + (2*(x - 1)*(x - 2)/(x - 3)*(x - 4)3) + (2*(x - 1)*(x - 2)/(x - 3)2*(x - 4)2
24、) + (2*(x - 1)*(x - 2)/(x - 3)3*(x - 4)/(4*(x - 1)*(x - 2)/(x - 3)*(x - 4)(3/2)题目11 syms x;y=x*log(x);diff(y,10)ans =40320/x9题目12 syms x;y=exp(-2*x);taylor(y,x,0,order,6)ans =- (4*x5)/15 + (2*x4)/3 - (4*x3)/3 + 2*x2 - 2*x + 1题目13 syms x;y=x/sin(x);f=taylor(y,x,2,order,5),ezplot(f)f =(2*(cos(2)2/sin(
25、2)2 + 1/2)/sin(2) - cos(2)/sin(2)2)*(x - 2)2 - (cos(2)/(3*sin(2) + (cos(2)*(cos(2)2/sin(2)2 + 1/2)/sin(2)/sin(2) - (2*(cos(2)2/(3*sin(2)2) + (cos(2)*(cos(2)/(3*sin(2) + (cos(2)*(cos(2)2/sin(2)2 + 1/2)/sin(2)/sin(2) + 5/24)/sin(2)*(x - 2)4 - (2*cos(2)/sin(2)2 - 1/sin(2)*(x - 2) - (x - 2)3*(2*(cos(2)/
26、(3*sin(2) + (cos(2)*(cos(2)2/sin(2)2 + 1/2)/sin(2)/sin(2) - (cos(2)2/sin(2)2 + 1/2)/sin(2) + 2/sin(2)题目14 syms x1 x2 x3 x4 x5 x6 x7;a=x1/x5;b=x3/(x2-x1)0.85;c=1-0.36*(x4/x2)(-0.56);d=2.62*c1.5;e=(x4/x2)1.16;f=(1-d*e)/(x6*x7)0.5;y=174.42*a*b*f; f1=diff(y,x1);f2=diff(y,x2);f3=diff(y,x3);f4=diff(y,x4);
27、f5=diff(y,x5);f6=diff(y,x6);f7=diff(y,x7);x1=0.1;x2=0.3;x4=0.1;x5=1.5;x6=16;x7=0.75; eval(f1),eval(f2),eval(f3),eval(f4),eval(f5),eval(f6),eval(f7)ans =(9818671183655279594565911892649*x3)/(148552804714245632987894906880*(5*x3)(3/20) + (7003944643125492063*(5*x3)(17/20)/225179981368524800ans =(15769
28、23169262824*(5*x3)(17/20)/651563676165967 - (9818671183655279594565911892649*x3)/(148552804714245632987894906880*(5*x3)(3/20)ans =1090091481931763/(82463372083200*(5*x3)(3/20)ans =-(61500003601250128*(5*x3)(17/20)/8470327790157571ans =-(2334648214375164021*(5*x3)(17/20)/1125899906842624000ans =-(134
29、89104037805899137*(5*x3)(17/20)/138777850513941643264ans =-(13489104037805899137*(5*x3)(17/20)/6505211742841014272第五章 程序编制题目1略题目2function f=fib(n)if n=1|n=2 %“|”是或者 f=1;else f=fib(n-1)+fib(n-2);end题目3function a=countwords(s)d=length(s);n=1;x=0;while n0)&(isempty(ounce)=1)&(isempty(kerra)=1)ounce=kil
30、ogram*1000/28.3495,kerra=kilogram*1000/0.2,endif ounce0&(isempty(kilogram)=1)&(isempty(kerra)=1)kilogram=ounce*28.3495/1000,kerra=ounce*5*28.3495endif kerra0&(isempty(ounce)=1)&(isempty(kilogram)=1)ounce=kerra/(5*28.3495),kilogram=kerra/5000,end%每次输入其中一个,输出另两个的换算结果题目5y=input(enter a year:);m=input(e
31、nter a month:);if (mod(y,400)=0)|(mod(y,4)=0)&(mod(y,100)=0)switch m, case1,3,5,7,8,10,12 nday=31; case4,6,9,11 nday=30; otherwise nday=29;endelse switch m, case1,3,5,7,8,10,12 nday=31; case4,6,9,11 nday=30; otherwise nday=28;endendnday=nday题目6s=input(,s);a=s(1:4);y=str2num(a);b=s(6:7);M=str2num(b);
32、c=s(9:10);D=str2num(c);C=floor(y/100);%世纪数Y=mod(y,100);%年份后两位w=mod(floor(C/4)-2*C+Y+floor(Y/4)+floor(13*(M+1)/5)+D-1),7)% Zeller公式该年的1,2月按13,14计算。题目7m=input();if m1500)&(m4500)&(m9000)&(m35000)&(m80000 t=45+300+900+6000+15750+0.45*(m-80000)endt第六章 算法入门%习题一%查键盘字符对应的ASCII码clear,clc %清空变量和屏幕a=input(请输入
33、您的要加密的字符:n,s); %输入加密字符n=length(a); %计算字符长度for i=1:n, if (a(i)=65 & a(i)=97 & a(i)=87 & a(i)=118 & a(i)=122, %118和122是字符w与z对应的ASCII码 b(i)=a(i)-118+97; else b(i)=a(i); endendb=char(b); %转化为字符串fprintf(转换后的字符为:%sn,b)%习题二clc,clear alldisp(计算日期在当年的天数);month=input(请输入月份(1-12):);day=input(请输入日期:); year=input(请输入年份(yyyy):); %检查是否为闰年if mod(year,400)=0leap_day=1; %被400整除的是闰年elseif mod(year,100)=0leap_day=0; %被100整除的不是闰年elseif mod(year,4)=0leap_day=1; %每四年是闰年elseleap_day=0; %余下的不是闰年end%计算当前天并加到当年的上一个月day_of_year=day;for ii=1