1、数学实验答案Chapter 1Page20,ex1(5) 等于exp(1),exp(2);exp(3),exp(4)(7) 3=1*3, 8=2*4(8) a 为各列最小值,b 为最小值所在的行号(10) 1=4,false, 2=3,false, 3=2, ture, 4=1,ture(11) 答案表明:编址第 2 元素满足不等式(30=20)和编址第 4 元素满足不等式(40=10)(12) 答案表明:编址第 2 行第 1 列元素满足不等式(30=20)和编址第 2 行第 2 列元素满足不等式(40=10)Page20, ex2(1) a, b, c 的值尽管都是 1, 但数据类型分别为数
2、值,字符, 逻辑, 注意 a 与 c 相等, 但他们不等于 b(2) double(fun)输出的分别是字符 a,b,s,(,x,)的 ASCII 码Page20,ex3 r=2;p=0.5;n=12; T=log(r)/n/log(1+0.01*p)Page20,ex4 x=-2:0.05:2;f=x.4-2.x; fmin,min_index=min(f)最小值最小值点编址 x(min_index) ans =0.6500最小值点 f1,x1_index=min(abs(f)求近似根-绝对值最小的点f1 = 0.0328x1_index = 24 x(x1_index) ans =-0.8
3、500 x(x1_index)=;f=x.4-2.x;删去绝对值最小的点以求函数绝对值次小的点 f2,x2_index=min(abs(f)求另一近似根-函数绝对值次小的点f2 = 0.0630x2_index = 65 x(x2_index) ans =1.2500Page20,ex5 z=magic(10) z =92 99 1 8 15 67 74 51 58 4098 80 7 14 16 73 55 57 64 414 81 88 20 22 54 56 63 70 4785 87 19 21 3 60 62 69 71 2886 93 25 2 9 61 68 75 52 3417
4、 24 76 83 90 42 49 26 33 6523 5 82 89 91 48 30 32 39 6679 6 13 95 97 29 31 38 45 7210 12 94 96 78 35 37 44 46 5311 18 100 77 84 36 43 50 27 59 sum(z) sum(diag(z) z(:,2)/sqrt(3) z(8,:)=z(8,:)+z(3,:)Chapter 2Page 45 ex1先在编辑器窗口写下列 M 函数,保存为 eg2_1.m function xbar,s=ex2_1(x)n=length(x); xbar=sum(x)/n;s=sq
5、rt(sum(x.2)-n*xbar2)/(n-1);例如x=81 70 65 51 76 66 90 87 61 77;xbar,s=ex2_1(x) Page 45 ex2 s=log(1);n=0;while sek=k+1;F(k)=F(k-1)+F(k-2); x=F(k)/F(k-1);end a,x,k计算至 k=21 可满足精度Page 45 ex4 clear;tic;s=0; for i=1:1000000s=s+sqrt(3)/2i; ends,toc tic;s=0;i=1;while i1.1)+x.*(x=-1.1)-1.1*(x1);p=p+b*exp(-y.2-
6、6*x.2).*(x+y-1).*(x+y=1); p=p+a*exp(-0.75*y.2-3.75*x.2+1.5*x).*(x+y A=1 2 3;4 5 6;7 8 0;C=2 -5 -22;-5 -24 -56;-22 -56 -16; X=lyap(A,C) X =1.0000 -1.0000-0.0000-1.0000 2.00001.0000-0.0000 1.00007.0000Chapter 3Page65Ex1 a=1,2,3;b=2,4,3;a./b,a.b,a/b,ab ans =0.5000 0.5000 1.0000ans = 2 2 1ans =0.6552 一元
7、方程组 x2,4,3=1,2的,3近 似解ans =0 0 00 0 00.6667 1.3333 1.0000矩阵方程1,2,3x11,x12,x13;x21,x22,x23;x31,x32,x33=的2特,4解,3Page65Ex 2(1) A=4 1 -1;3 2 -6;1 -5 3;b=9;-2;1; rank(A), rank(A,b)A,b为增广矩阵ans =3ans =3可见方程组唯一解 x=Ab x = 2.38301.48942.0213(2) A=4 -3 3;3 2 -6;1 -5 3;b=-1;-2;1; rank(A), rank(A,b) ans =3ans =3可
8、见方程组唯一解 x=Ab x =-0.4706-0.29410(3) A=4 1;3 2;1 -5;b=1;1;1; rank(A), rank(A,b) ans =2ans =3可见方程组无解 x=Ab x = 0.3311-0.1219最小二乘近似解(4) a=2,1,-1,1;1,2,1,-1;1,1,2,1;b=1 2 3;%注意 b 的写法 rank(a),rank(a,b) ans =3ans =3rank(a)=rank(a,b) abans = 1010一个特解Page65Ex3 a=2,1,-1,1;1,2,1,-1;1,1,2,1;b=1,2,3; x=null(a),x0
9、=ab x =-0.62550.6255-0.20850.4170x0 = 1010通解 kx+x0Page65Ex 4 x0=0.2 0.8;a=0.99 0.05;0.01 0.95; x1=a*x, x2=a2*x, x10=a10*x x=x0;for i=1:1000,x=a*x;end,x x =0.83330.1667 x0=0.8 0.2; x=x0;for i=1:1000,x=a*x;end,x x =0.83330.1667 v,e=eig(a) v =0.9806 -0.70710.1961 0.7071e = 1.0000 00 0.9400 v(:,1)./xans
10、 = 1.17671.1767成比例,说明 x 是最大特征值对应的特征向量Page65Ex5用到公式(3.11)(3.12) B=6,2,1;2.25,1,0.2;3,0.2,1.8;x=25 5 20; C=B/diag(x) C =0.2400 0.4000 0.05000.0900 0.2000 0.01000.1200 0.0400 0.0900 A=eye(3,3)-C A =0.7600 -0.4000 -0.0500-0.0900 0.8000 -0.0100-0.1200 -0.0400 0.9100 D=17 17 17;x=ADx = 37.569625.786224.76
11、90Page65Ex 6(1) a=4 1 -1;3 2 -6;1 -5 3;det(a),inv(a),v,d=eig(a) ans =-94ans =0.2553 -0.0213 0.04260.1596 -0.1383 -0.22340.1809 -0.2234 -0.0532v =0.0185 -0.9009 -0.3066-0.7693 -0.1240 -0.7248-0.6386 -0.4158 0.6170d =-3.0527 0 00 3.6760 00 0 8.3766(2) a=1 1 -1;0 2 -1;-1 2 0;det(a),inv(a),v,d=eig(a) an
12、s =1ans =2.0000 -2.0000 1.00001.0000 -1.0000 1.00002.0000 -3.0000 2.0000v =-0.5773 0.5774 + 0.0000i 0.5774 - 0.0000i-0.5773 0.5774 0.5774-0.5774 0.5773 - 0.0000i 0.5773 + 0.0000id = 1.0000 0 00 1.0000 + 0.0000i 00 0 1.0000 - 0.0000i(3) A=5 7 6 5;7 10 8 7;6 8 10 9;5 7 9 10 A =5 7 6 57 10 8 76 8 10 95
13、 7 9 10 det(A),inv(A), v,d=eig(A) ans =1ans =68.0000 -41.0000 -17.0000 10.0000-41.0000 25.0000 10.0000 -6.0000-17.0000 10.0000 5.0000 -3.000010.0000 -6.0000 -3.0000 2.0000v =0.8304 0.0933 0.3963 0.3803-0.5016 -0.3017 0.6149 0.5286-0.2086 0.7603 -0.2716 0.55200.1237 -0.5676 -0.6254 0.5209d =0.0102 0
14、0 00 0.8431 0 00 0 3.8581 00 0 0 30.2887(4)(以 n=5 为例) 方法一(三个 for) n=5;for i=1:n, a(i,i)=5;endfor i=1:(n-1),a(i,i+1)=6;endfor i=1:(n-1),a(i+1,i)=1;end a方法二(一个 for) n=5;a=zeros(n,n); a(1,1:2)=5 6;for i=2:(n-1),a(i,i-1,i,i+1)=1 5 6;end a(n,n-1 n)=1 5;a方法三(不用 for)n=5;a=diag(5*ones(n,1); b=diag(6*ones(n-
15、1,1);c=diag(ones(n-1,1);a=a+zeros(n-1,1),b;zeros(1,n)+zeros(1,n);c,zeros(n-1,1)下列计算 det(a) ans = 665 inv(a) ans =0.3173 -0.5865 1.0286 -1.6241 1.9489-0.0977 0.4887 -0.8571 1.3534 -1.62410.0286 -0.1429 0.5429 -0.8571 1.0286-0.0075 0.0376 -0.1429 0.4887 -0.58650.0015 -0.0075 0.0286 -0.0977 0.3173 v,d=
16、eig(a) v =-0.7843 -0.7843 -0.9237 0.9860 -0.92370.5546 -0.5546 -0.3771 -0.0000 0.3771-0.2614 -0.2614 0.0000 -0.1643 0.00000.0924 -0.0924 0.0628 -0.0000 -0.0628-0.0218 -0.0218 0.0257 0.0274 0.0257d =0.7574 0 0 0 00 9.2426 0 0 00 0 7.4495 0 00 0 0 5.0000 00 0 0 0 2.5505Page65Ex 7(1) a=4 1 -1;3 2 -6;1
17、-5 3;v,d=eig(a)v =0.0185 -0.9009 -0.3066-0.7693 -0.1240 -0.7248-0.6386 -0.4158 0.6170d =-3.0527 0 00 3.6760 00 0 8.3766 det(v) ans =-0.9255 %v 行列式正常, 特征向量线性相关,可对角化 inv(v)*a*v验算ans =-3.0527 0.0000 -0.00000.0000 3.6760 -0.0000-0.0000 -0.0000 8.3766 v2,d2=jordan(a)也可用 jordan v2 =0.0798 0.0076 0.91270.1
18、886 -0.3141 0.1256-0.1605 -0.2607 0.4213特征向量不同d2 = 8.3766 0 00 -3.0527 - 0.0000i 00 0 3.6760 + 0.0000i v2a*v2 ans =8.3766 0 0.00000.0000 -3.0527 0.00000.0000 0.0000 3.6760 v(:,1)./v2(:,2)对应相同特征值的特征向量成比例ans = 2.44912.44912.4491(2) a=1 1 -1;0 2 -1;-1 2 0;v,d=eig(a)v =-0.5773 0.5774 + 0.0000i 0.5774 -
19、0.0000i-0.5773 0.5774 0.5774-0.5774 0.5773 - 0.0000i 0.5773 + 0.0000id = 1.0000 0 00 1.0000 + 0.0000i 00 0 1.0000 - 0.0000i det(v) ans =-5.0566e-028 -5.1918e-017iv 的行列式接近 0, 特征向量线性相关,不可对角化 v,d=jordan(a) v =1 0 11 0 01 -1 0d = 1 1 00 1 10 0 1jordan 标准形不是对角的,所以不可对角化(3) A=5 7 6 5;7 10 8 7;6 8 10 9;5 7
20、9 10 A =5 7 6 57 10 8 76 8 10 95 7 9 10 v,d=eig(A) v =0.8304 0.0933 0.3963 0.3803-0.5016 -0.3017 0.6149 0.5286-0.2086 0.7603 -0.2716 0.55200.1237 -0.5676 -0.6254 0.5209d =0.0102 0 0 00 0.8431 0 00 0 3.8581 00 0 0 30.2887 inv(v)*A*v ans =0.0102 0.0000 -0.0000 0.00000.0000 0.8431 -0.0000 -0.0000-0.000
21、0 0.0000 3.8581 -0.0000-0.0000 -0.0000 0 30.2887本题用 jordan 不行, 原因未知(4)参考 6(4)和 7(1)Page65Exercise 8只有(3)对称, 且特征值全部大于零, 所以是正定矩阵.Page65Exercise 9(1) a=4 -3 1 3;2 -1 3 5;1 -1 -1 -1;3 -2 3 4;7 -6 -7 0 rank(a) ans =3 rank(a(1:3,:) ans =2 rank(a(1 2 4,:)1,2,4 行为最大无关组ans = 3 b=a(1 2 4,:);c=a(3 5,:); bc线性表示
22、的系数ans =0.5000 5.0000-0.5000 1.00000 -5.0000Page65Exercise 10 a=1 -2 2;-2 -2 4;2 4 -2 v,d=eig(a) v =0.3333 0.9339 -0.12930.6667 -0.3304 -0.6681-0.6667 0.1365 -0.7327d =-7.0000 0 00 2.0000 00 0 2.0000 v*v ans =1.0000 0.0000 0.00000.0000 1.0000 00.0000 0 1.0000v 确实是正交矩阵Page65Exercise 11设经过 6 个电阻的电流分别为
23、 i1, ., i6. 列方程组如下20-2i1=a; 5-3i2=c; a-3i3=c; a-4i4=b; c-5i5=b; b-3i6=0; i1=i3+i4;i5=i2+i3;i6=i4+i5;计算如下 A=1 0 0 2 0 0 0 0 0;0 0 1 0 3 0 0 0 0;1 0 -1 0 0 -3 0 0 0; 1 -1 0 0 0 0 -4 0 0;0 -1 1 0 0 0 0 -5 0;0 1 0 0 0 0 0 0 -3; 0 0 0 1 0 -1 -1 0 0;0 0 0 0 -1 -1 0 1 0;0 0 0 0 0 0 -1 -1 1;b=20 5 0 0 0 0 0
24、 0 0; Abans = 13.34536.44018.54203.3274-1.18071.60111.72630.42042.1467Page65Exercise 12 A=1 2 3;4 5 6;7 8 0; left=sum(eig(A), right=sum(trace(A) left =6.0000right =6 left=prod(eig(A), right=det(A)原题有错, (-1)n 应删去left = 27.0000right = 27 fA=(A-p(1)*eye(3,3)*(A-p(2)*eye(3,3)*(A-p(3)*eye(3,3) fA =1.0e-0
25、12 *0.0853 0.1421 0.02840.1421 0.1421 0-0.0568 -0.1137 0.1705 norm(fA)f(A)范数接近 0 ans =2.9536e-013Chapter 4Page84Exercise 1(1)roots(1 1 1)(2)roots(3 0 -4 0 2 -1)(3)p=zeros(1,24);p(1 17 18 22)=5 -6 8 -5;roots(p) (4)p1=2 3;p2=conv(p1, p1); p3=conv(p1, p2);p3(end)=p3(end)-4; %原 p3 最后一个分量-4 roots(p3)Page
26、84Exercise 2fun=inline(x*log(sqrt(x2-1)+x)-sqrt(x2-1)-0.5*x); fzero(fun,2)Page84Exercise 3fun=inline(x4-2x); fplot(fun,-2 2);grid on;fzero(fun,-1),fzero(fun,1),fminbnd(fun,0.5,1.5) Page84Exercise 4 fun=inline(x*sin(1/x),x);fplot(fun, -0.1 0.1);x=zeros(1,10);for i=1:10, x(i)=fzero(fun,(i-0.5)*0.01);e
27、nd; x=x,-xPage84Exercise 5fun=inline(9*x(1)2+36*x(2)2+4*x(3)2-36;x(1)2-2*x(2)2-20*x(3);16*x(1)-x(1)3- 2*x(2)2-16*x(3)2,x);a,b,c=fsolve(fun,0 0 0)Page84Exercise 6fun=(x)x(1)-0.7*sin(x(1)-0.2*cos(x(2),x(2)-0.7*cos(x(1)+0.2*sin(x(2); a,b,c=fsolve(fun,0.5 0.5)Page84Exercise 7clear; close; t=0:pi/100:2*p
28、i; x1=2+sqrt(5)*cos(t); y1=3-2*x1+sqrt(5)*sin(t); x2=3+sqrt(2)*cos(t); y2=6*sin(t);plot(x1,y1,x2,y2); grid on;作图发现 4 个解的大致位置,然后分别求解y1=fsolve(x(1)-2)2+(x(2)-3+2*x(1)2-5,2*(x(1)-3)2+(x(2)/3)2-4,1.5,2)y2=fsolve(x(1)-2)2+(x(2)-3+2*x(1)2-5,2*(x(1)-3)2+(x(2)/3)2-4,1.8,-2)y3=fsolve(x(1)-2)2+(x(2)-3+2*x(1)2
29、-5,2*(x(1)-3)2+(x(2)/3)2-4,3.5,-5)y4=fsolve(x(1)-2)2+(x(2)-3+2*x(1)2-5,2*(x(1)-3)2+(x(2)/3)2-4,4,-4)Page84Exercise 8(1)clear;fun=inline(x.2.*sin(x.2-x-2); fplot(fun,-2 2);grid on;作图观察x(1)=-2;x(3)=fminbnd(fun,-1,-0.5); x(5)=fminbnd(fun,1,2); fun2=inline(-x.2.*sin(x.2-x-2); x(2)=fminbnd(fun2,-2,-1);x(
30、4)=fminbnd(fun2,-0.5,0.5); x(6)=2feval(fun,x)答案: 以上 x(1)(3)(5)是局部极小, x(2)(4)(6)是局部极大,从最后一句知道 x(1)全局最小, x(2)最大。(2)clear;fun=inline(3*x.5-20*x.3+10); fplot(fun,-3 3);grid on;作图观察x(1)=-3;x(3)=fminsearch(fun,2.5); fun2=inline(-(3*x.5-20*x.3+10); x(2)=fminsearch(fun2,-2.5);x(4)=3;feval(fun,x) (3)fun=inli
31、ne(abs(x3-x2-x-2); fplot(fun,0 3);grid on;作图观察fminbnd(fun,1.5,2.5) fun2=inline(-abs(x3-x2-x-2); fminbnd(fun2,0.5,1.5)Page84Exercise 9close;x=-2:0.1:1;y=-7:0.1:1;x,y=meshgrid(x,y); z=y.3/9+3*x.2.*y+9*x.2+y.2+x.*y+9; mesh(x,y,z);grid on;作图观察fun=inline(x(2)3/9+3*x(1)2*x(2)+9*x(1)2+x(2)2+x(1)*x(2)+9);x=
32、fminsearch(fun,0 0)求极小值fun2=inline(-(x(2)3/9+3*x(1)2*x(2)+9*x(1)2+x(2)2+x(1)*x(2)+9); x=fminsearch(fun2,0 -5)求极大值Page84Exercise 10clear;t=0:24;c=15 14 14 14 14 15 16 18 20 22 23 25 28 .31 32 31 29 27 25 24 22 20 18 17 16;p2=polyfit(t,c,2) p3=polyfit(t,c,3)fun=inline(a(1)*exp(a(2)*(t-14).2),a,t); a=l
33、sqcurvefit(fun,0 0,t,c)初值可以试探f=feval(fun, a,t)norm(f-c)拟合效果plot(t,c,t,f)作图检验fun2=inline(b(1)*sin(pi/12*t+b(2)+20,b,t); b=lsqcurvefit(fun2,0 0,t,c)figure f2=feval(fun2, b,t)norm(f2-c)拟合效果plot(t,c,t,f2)作图检验Page84Exercise 11原题修改 f(x)+20fun=inline(1-x)*sqrt(10.52+x)-3.06*x*sqrt(1+x)*sqrt(5); x=fzero(fun
34、, 0, 1)Page84Exercise 12r=5.04/12/100;N=20*12;x=7500*180房屋总价格y=x*0.3首付款额x0=x-y贷款总额a=(1+r)N*r*x0/(1+r)N-1)月付还款额r1=4.05/12/100;x1=10*10000;公积金贷款a1=(1+r1)N*r1*x1/(1+r1)N-1)x2=x0-x1商业贷款a2=(1+r)N*r*x2/(1+r)N-1) a=a1+a2Page84Exercise 13列方程 th*R2+(pi-2*th)*r2-R*r*sin(th)=pi*r2/2化简得 sin(2*th)-2*th*cos(2*th)
35、=pi/2以下 Matlab 计算clear;fun= inline(sin(2*th)-2*th*cos(2*th)-pi/2,th) th=fsolve(fun,pi/4)R=20*cos(th)Page84Exercise 14先在 Editor 窗口写 M 函数保存function x=secant(fname,x0,x1,e) while abs(x0-x1)e,x=x1-(x1-x0)*feval(fname,x1)/(feval(fname,x1)-feval(fname,x0); x0=x1;x1=x;end再在指令窗口fun=inline(x*log(sqrt(x2-1)+x
36、)-sqrt(x2-1)-0.5*x); secant(fun,1,2,1e-8)Page84Exercise 15作系数为 a,初值为 xo,从第 m 步到第 n 步迭代过程的 M 函数: function f=ex4_15fun(a,x0,m,n)x(1)=x0; y(1)=a*x(1)+1;x(2)=y(1);if mm, plot(x(i),x(i),x(i+1),y(i-1),y(i),y(i); end endhold off;M 脚本文件subplot(2,2,1);ex4_15fun(0.9,1,1,20);subplot(2,2,2);ex4_15fun(-0.9,1,1,2
37、0);subplot(2,2,3);ex4_15fun(1.1,1,1,20);subplot(2,2,4);ex4_15fun(-1.1,1,1,20);Page84Exercise 16设夹角 t, 问题转化为 min f=5/sin(t)+10/cos(t) 取初始值 pi/4, 计算如下fun=(t)5/sin(t)+10/cos(t); t,f=fminsearch(fun, pi/4)t = 0.6709f = 20.8097Page84Exercise 17提示:x(k+2)=f(x(k)=a2*x(k)*(1-x(k)*(1-a*x(k)*(1-x(k) 计算平衡点 x|f(x
38、)| ex4_18(0.9,1,20) ex4_18(-0.9,1,20) ex4_18(1.1,1,20) ex4_18(-1.1,1,20)Page84Exercise 19 clear; close; x(1)=0; y(1)=0; for k=1:3000x(k+1)=1+y(k)-1.4*x(k)2; y(k+1)=0.3*x(k); end plot(x(1000:1500),y(1000:1500),+g);hold onplot(x(1501:2000),y(1501:2000),.b);plot(x(2001:2500),y(2001:2500),*y);plot(x(2501:3001),y(2501:3001),.r);Chapter 5Page101Exercise 1x=0 4 10 12 15 22 28 34 40;y=0 1 3 6 8 9 5 3 0;trapz(x,y)Page101Exercise 2x=0 4 10 12 15 22 28 34 40;y=0 1 3 6 8 9 5 3 0;diff(y)./