1、第二章3.设矩阵A为A=24 23 9 21 6;65 74 24 11 21;34 5 98 75 21;8 42 42 53 121;43 21 45 64 21;(1) B=A(2:5,1:2:5)B = 65 24 21 34 98 21 8 42 121 43 45 21(2)A(7)=A =24 65 34 8 43 23 5 42 21 9 24 98 42 45 21 11 75 53 64 6 21 21 121 21(3)A+30(4)size(A);ndims(A)(5)题目有误(6)reshape(x,3,4)(7)abs(x)(8)char(x)4. L1 = 0 0
2、 0 0 1 0 0 0 0L2 = 1 1 1 1 1 0 0 0 0L3 = 0 0 0 1 1 1 0 0 0L4 = 4 5 65.(1)B=A(1:3,:)C=A(:,1:2)D=A(2:4,3:4)E=B*CB = 23.0000 10.0000 -0.7780 0 41.0000 -45.0000 65.0000 5.0000 32.0000 5.0000 0 32.0000C = 23.0000 10.0000 41.0000 -45.0000 32.0000 5.0000 6.0000 -9.5400D = 65.0000 5.0000 0 32.0000 54.0000 3
3、.1400E = 1.0e+003 * 0.9141 -0.2239 1.2080 2.7123 1.1330 -0.2103(2)E=10&A25)ans = 1 56. all(A)ans = 0any(A)ans = 1isnan(A) ans = 0 1 0 0 0 0 0isinf(A)ans = 0 0 1 1 0 0 0isfinite(A)ans = 1 0 0 0 1 1 17.A(1).x1=学号;A(1).x2=姓名;A(1).x3=专业;A(1).x4.x41=成绩1;.A(2).x1=学号;A(2).x2=姓名;A(2).x3=专业;A(2).x4.x41=成绩1;.
4、A(3).x1=学号;A(3).x2=姓名;A(3).x3=专业;A(3).x4.x41=成绩1;.A(4).x1=学号;A(4).x2=姓名;A(4).x3=专业;A(4).x4.x41=成绩1;.A(5).x1=学号;A(5).x2=姓名;A(5).x3=专业;A(5).x4.x41=成绩1;.8.(1)size(B)ans = 2 2ndims(B)ans = 2(2) B(2)ans =3x3 doubleB(4)ans = 3x3 cell(3)B(3)=B = 1 3x3 double 3x3 cellB3=B = 1 3x3 double 第三章1.(1)A=eye(3)(2)
5、A=100+100*rand(5,6)(3)A=1+sqrt(0.2)*randn(10,50)(4)B=ones(size(A)(5)A+30*eye(size(A)(6)B=diag(diag(A)2.B=rot90(A)C=rot90(A,-1)3.B=inv(A) ;A的逆矩阵C=det(A) ;A的行列式的值D=A*BE=B*AD=E 因此A与A-1是互逆的。4.A=4 2 -1;3 -1 2;12 3 0;b=2;10;8;x=inv(A)*bx = -6.0000 26.6667 27.33335.(1) diag(A) ;主对角线元素ans = 1 1 5 9triu(A) ;
6、上三角阵ans = 1 -1 2 3 0 1 -4 2 0 0 5 2 0 0 0 9tril(A) ;下三角阵ans = 1 0 0 0 5 1 0 0 3 0 5 011 15 0 9rank(A) ;秩ans = 4norm(A) ;范数ans = 21.3005cond(A) ;条件数ans = 11.1739trace(A) ;迹ans =16(2)略6.A=1 1 0.5;1 1 0.25;0.5 0.25 2A = 1.0000 1.0000 0.5000 1.0000 1.0000 0.2500 0.5000 0.2500 2.0000 V,D=eig(A)V = 0.7212
7、 0.4443 0.5315 -0.6863 0.5621 0.4615 -0.0937 -0.6976 0.7103D = -0.0166 0 0 0 1.4801 0 0 0 2.5365第四章1.a=input(请输入一个4位数:);while (a9999)a=input(输入错误,请重新输入一个4位数:);endb=fix(a/1000);c=rem(fix(a/100),10);d=rem(fix(a/10),10);e=rem(a,10);b=b+7;c=c+7;d=d+7;e=e+7;b=rem(b,10);c=rem(c,10);d=rem(c,10);e=rem(e,10)
8、;g=b;b=d;d=g;g=c;c=e;e=g;a=1000*d+100*e+10*b+c;disp(加密后:,num2str(a)2.a=input(请输入a: );b=input(请输入b: );c=input(请输入c: );x=0.5:1:5.5;x1=(x=0.5&x=1.5&x=3.5&x=5.5);y1=a.*(x.2)+b.*x+c;y2=a*(sin(b)c)+x;y3=log(abs(b+c./x);y=y1.*x1+y1.*x2+y3.*x3;disp(y)3.x=fix(rand(1,20)*89)+10;x1=fix(sum(x)/20);disp(平均数是:,nu
9、m2str(x1)m=(rem(x,2)=0&x=na=n;elseif b plot(x,y)fplot函数:fplot(x0).*(x.2+(1+x).(1/4)+5),-10,10)第六章1.A=randn(10,5)(1)mean(A) ;均值 std(A) ;标准方差(2)max(max(A) ;最大元素 min(min(A) ;最小元素(3)B=sum(A,2) ;A每行元素的和 sum(B) ;A全部元素之和(4)sort(A) ;A的每列元素按升序排列 sort(A,2,descend) ;A的每行元素按将序排列2.(1)(2)X=1 4 9 16 25 36 49 64 81
10、 100;Y=1:10;X1=1:100;Y1=interp1(X,Y,X1,cubic)3.x=165 123 150 123 141;y=187 126 172 125 148;P=polyfit(x,y,3)P = 1.0e+003 * -0.0000 0.0013 -0.1779 8.4330所以它的线性拟合曲线为:p(x)=1.3x2177.9x+84334.(1)P1=0 3 2;P2=5 -1 2;P3=1 0 -0.5;P=conv(conv(P1,P2),P3)P = 0 15.0000 7.0000 -3.5000 0.5000 -2.0000 -2.0000所以P(x)=
11、15x5+7x4-3.5x3+0.5x2-2x-2(2)roots(P)ans = 0.7071 0.1000 + 0.6245i 0.1000 - 0.6245i -0.7071 -0.6667 (3)i=0:10;xi=0.2*i;polyval(P,xi)ans = -2.0000 -2.3920 -2.6112 -1.7024 2.7104 15.0000 42.1120 94.1408 184.9056 332.5264 560.00005.(1)建立函数文件:function f=fxy(u)x=u(1);y=u(2);f=3.*x.2+2*x.*y+y.2在命令窗口中输入以下命令
12、:U,fmin=fminsearch(fxy,1,1)结果:U = 1.0e-004 * -0.0675 0.1715fmin = 1.9920e-010(2)f=inline(-sin(x)-cos(x.2);fmax=fminbnd(f,0,pi)fmax =0.73106.(1)x=pi/6 pi/4 pi/3;f=inline(sin(x).2+cos(x).2);dx=diff(f(x,5*pi/12)/(pi/12) 可参见第157页例题6.19dx = 0 0 0x=pi/2时单独计算:x=pi/2;f=inline(sin(x).2+cos(x).2);diff(f(x,pi)
13、/(pi/2)ans = 0(2)x=1:3;f=inline(sqrt(x.2+1);dx=diff(f(x,4)结果:dx =0.8219 0.9262 0.96087.(1)f=inline(sin(x).5.*sin(5*x);quad(f,0,pi)ans =0.0982(2)f=inline(1+x.2)./(1+x.4);quad(f,-1,1)ans =2.2214(3)f=inline(x.*sin(x)./(1+cos(x).2);quad(f,0,pi)ans =2.4674(4)f=inline(abs(cos(x+y);dblquad(f,0,pi,0,pi)ans
14、=6.28328.N=64; %采样点数T=5; %采样时间终点t=linspace(0,T,N); %给出N个采样时间ti(i=1:N)y=exp(-t); %求各采样点样本值ydt=t(2)-t(1); %采样周期f=1/dt; % 采样频率Y=fft(y); %计算y的快速傅里叶变换YF=Y(1:N/2+1); %F(k)=Y(k)f=f*(0:N/2)/N; %使频率轴f从0开始plot(f,abs(F) %绘制振幅-频率图9.(1)矩阵求逆法:A=2 3 5;3 7 4;1 -7 1;b=10;3;5;x=inv(A)*bx = -1.8060 -0.53733.0448矩阵除法法:
15、A=2 3 5;3 7 4;1 -7 1;b=10;3;5;x=Abx = -1.8060 -0.53733.0448矩阵分解法:A=2 3 5;3 7 4;1 -7 1;b=10;3;5;L,U=lu(A);x=U(Lb)x = -1.8060 -0.53733.0448(2)方法同(1)10.函数文件:line_solution(A,b)function x,y=line_solution(A,b)m,n=size(A);y=;if norm(b)0if rank(A)=rank(A,b)if rank(A)=ndisp(原方程组有唯一解x);x=Ab; elsedisp(原方程组有无穷个解,特解为x,齐次方程组的基础解系为y);x=Ab;y=null(A,r);enddisp(方程组无解);x=; endelsedisp(原方程组有零解x);x=zeros(n,1);if rank(A) In line_solution at 11方程组无解x = y = -0.5000 0.5000 1.0000 0 0