ans =
-0.0562 -0.6902 -0.0436 -0.1051 -0.3282 -0.4311 -0.8011 -0.9350 -0.3763 ans =
1.0e-15 *
0 -0.1110 -0.0278 0 0 0 0 0.1110 0 ans =
-80.2971 65.0383 107.2212 -8.0299 91.2626 70.5679 -66.2535 153.4898 66.4342
不相同。第二个更接近0。具体原理需要参考线性代数书……有点忘了。
A\\eye(3) eye(3)/A
ans =
0.1472 -0.1444 0.0639 -0.0611 0.0222 0.1056 -0.0194 0.1889 -0.1028 ans =
0.1472 -0.1444 0.0639 -0.0611 0.0222 0.1056 -0.0194 0.1889 -0.1028
相同。因为对于对角阵,,二者均可化为同一形式。 6.结果不同
A=[1 2; 3 4]; B1=A.^(0.5) B2=0.5.^A B3=A^(0.5) B4=0.5^A
B1 =
1.0000 1.4142 1.7321 2.0000 B2 =
0.5000 0.2500 0.1250 0.0625 B3 =
0.5537 + 0.4644i 0.8070 - 0.2124i 1.2104 - 0.3186i 1.7641 + 0.1458i B4 =
0.9910 -0.4422 -0.6634 0.3276 A1=B1.*B1 A3=B3*B3
norm(A1-A3,'fro')
A1 =
1.0000 2.0000
3.0000 4.0000 A3 =
1.0000 + 0.0000i 2.0000 + 0.0000i 3.0000 - 0.0000i 4.0000 + 0.0000i ans =
1.2831e-15
可见误差在eps量级,可以认为相等。
7.绘出图形
x=-3*pi:pi/15:3*pi; y=x;
[X,Y]=meshgrid(x,y); warning off;
Z=sin(X).*sin(Y)./X./Y;
共有10个非数数据。
surf(X,Y,Z)
shading interp
x=-3*pi:pi/15:3*pi; Lx=(x==0);
xx=x+Lx*realmin; y=xx;
[X,Y]=meshgrid(xx,y); warning off;
Z=sin(X).*sin(Y)./X./Y; surf(X,Y,Z)
shading interp
即消除零点处的断点即可
8.两种思路 %第二种思路
function z=zpoly_z(x,y) if x+y<=-1
z=0.546*exp(-0.75*y.^2-3.75*x.^2+1.5*x); elseif x+y>-1 & x+y<=1
z=0.758*exp(-y.^2-6*x.^2); else
z=0.546*exp(-0.75*y.^2-3.75*x.^2-1.5*x); end
x=-1.5:0.1:1.5; y=-3:0.1:3;
[X,Y]=meshgrid(x,y); Z=zpoly_z(X,Y); surf(X,Y,Z);
%第一种思路
x=-1.5:0.1:1.5; y=-3:0.2:3; LX=length(x); LY=length(y); for ii=1:LX for jj=1:LY
if x(ii)+y(jj)<=-1
z=0.546*exp(-0.75*y.^2-3.75*x.^2+1.5*x); elseif x(ii)+y(jj)>-1 & x(ii)+y(jj)<=1 z=0.758*exp(-y.^2-6*x.^2); else
z=0.546*exp(-0.75*y.^2-3.75*x.^2-1.5*x); end end end
[X,Y]=meshgrid(x,y); Z=zpoly_z(X,Y); surf(X,Y,Z);
%其实for循环完全无意义……
9.矩阵计算
%第一问老师取消
rng default
A=randn(50,70)+1i*randn(50,70); B=randn(70,60)+1i*randn(70,60); C=randn(50,60)+1i*randn(50,60); D=randn(60,1)+1i*randn(60,1); G=(A*B-C)*D
Gr=real(G),70,70 Gi=imag(G) Gn=norm(G,2)
G =
1.0e+02 *
-0.1776 + 1.9914i 0.6088 + 0.3316i -0.1340 - 0.8615i 0.0752 - 0.0759i -0.1171 - 1.8169i 0.2005 - 1.4540i -1.4501 + 0.1897i 0.6445 + 0.1657i -1.0651 + 0.1191i 0.3301 - 0.0450i -1.4338 + 0.8707i -0.9491 + 1.4840i 1.1314 + 1.2751i -0.5158 - 0.0725i -0.2746 + 0.2518i -1.0279 - 0.8409i