习题二1.选择题(1)下列变量名中A是合法的。
A. char_1,i,jB. x*y,a.1C. x\y,a1234D. end,lbcx说明:end是关键字,变量名中不能有运算符、标点符号,可以有下划线。
内置常量可以作为变量名,但一般不提倡这样做。
(2)下列C是合法常量。
A. 3*e10B. 1e500C. -1.85e-56D. 10-2说明:10-2是表达式,1e500超过了realmax,3*e10也是表达式。
(3)x=uint8(2.3e10),则x所占的字节是D个。
A. 1B. 2C. 4D. 8(4)已知x=0:10,则x有B个元素。
A. 10B. 11C. 9D. 12(5)产生对角线上为全1其余为0的2行3列矩阵的命令是C。
A. ones(2,3)B. ones(3,2)C. eye(2,3)D. eye(3,2)(6)已知数组123456789a⎡⎤⎢⎥=⎢⎥⎢⎥⎣⎦,则a(:,end)是指C 。
A. 所有元素B. 第一行元素C. 第三列元素D. 第三行元素(7)已知数组123456789a⎡⎤⎢⎥=⎢⎥⎢⎥⎣⎦,则运行a(:,1)=[]命令后C。
A. a变成行向量B. a数组为2行2列C. a数组为3行2列D. a数组中没有元素3(8)已知数组123456789a⎡⎤⎢⎥=⎢⎥⎢⎥⎣⎦,则运行mean(a)命令是B。
A. 计算a每行的平均值B. 计算a每列的平均值C. a数组增加一行平均值D. a数组增加一列平均值(9)已知x为一个向量,计算ln(x)的MATLA B命令是计算B。
A. ln(x)B. log(x)C. Ln(x)D. lg10(x)(10)当a=2.4,使用取整函数计算得出3,则该取整函数名为C。
A. fixB. roundC. ceilD. floor(11)已知a=0:4,b=1:5,下面的运算表达式出错的为D。
A. a+bB. a./bC. a'*bD. a*b(12)命令day(now)是指C。
A. 按照日期字符串格式提取当前时间B. 提取当前时间C. 提取当前时间的日期D. 按照日期字符串格式提取当前日期(13)以下运算符中哪个的优先级最高B。
A. *B. ^C. ~=D. |(14)运行命令bitand(20,15)的结果是C。
A. 15B. 20C. 4D. 5bitand(20,15)ans =4(15)使用检测函数isinteger(15)的结果是B。
A. 1B. 0C. falseD. trueisinteger(15)ans =(16)计算三个多项式s1、s2和s3的乘积,则算式为C。
A. conv(s1,s2,s3)B. s1*s2*s3C. conv(conv(s1,s2),s3)D. conv(s1*s2*s3)2.复数变量a=2+3i,b=3-4i,计算a+b,a-b,c=a*b,d=a/b,并计算变量c的实部、虚部、模和相角。
a=2+3i;b=3-4i;a+ba-bc=a*bd=a/breal(c)imag(c)abs(c)angle(c)ans =5.0000 - 1.0000ians =-1.0000 + 7.0000ic =18.0000 + 1.0000id =-0.2400 + 0.6800ians =18ans =1ans =18.0278ans =0.05553.用“from:step:to”方式和linspace函数分别得到从0~4π步长为0.4π的变量x1和从0~4π分成10点的变量x2。
x1=0:0.4*pi:4*pix2=linspace(0,4*pi,10)x1 =Columns 1 through 100 1.2566 2.5133 3.7699 5.0265 6.2832 7.5398 8.7965 10.0531 11.3097Column 1112.5664x2 =0 1.3963 2.7925 4.1888 5.5851 6.9813 8.3776 9.7738 11.1701 12.56644.输入矩阵123456789a⎡⎤⎢⎥=⎢⎥⎢⎥⎣⎦,使用全下标方式取出元素“3”,使用单下标方式取出元素“8”,取出后两行子矩阵块,使用逻辑矩阵方式取出1379⎡⎤⎢⎥⎣⎦。
a=[1 2 3;4 5 6;7 8 9]a(1,3)a(6)a(2:3,:)a([1,3],[1,3])a =1 2 34 5 67 8 9ans =3ans =8ans =4 5 67 8 9ans =1 37 95.输入a为3×3的魔方阵,b为3×3的单位阵,并将a、b小矩阵组成3×6的大矩阵c和6×3的大矩阵d,将d矩阵的最后一行取出构成小矩阵e。
a=magic(3)b=eye(3)c=[a,b]d=[a;b]d(end,:)a =8 1 63 5 74 9 2b =1 0 00 1 00 0 1c =8 1 6 1 0 03 5 7 0 1 04 9 2 0 0 1d =8 1 63 5 74 9 21 0 00 1 00 0 1ans =0 0 16.将矩阵123456789a⎡⎤⎢⎥=⎢⎥⎢⎥⎣⎦用flipud、fliplr、rot90、diag、triu和tril函数进行操作。
a=reshape(1:9,3,3); a=a'flipud(a)fliplr(a)rot90(a)diag(a)triu(a)tril(a)a =1 2 3 4 5 6 7 8 9 ans =7 8 9 4 5 6 1 2 3 ans =3 2 1 6 54 9 8 7 ans =3 6 9 2 5 8 14 7 ans =159ans =1 2 3 0 5 6 0 0 9 ans =1 0 0 4 5 0 7 8 97.求矩阵1358⎡⎤⎢⎥⎣⎦的转置、秩、逆矩阵、矩阵的行列式值和矩阵的三次幂。
a=[1 3;5 8];a'rank(a)inv(a)det(a)a^3ans =1 53 8ans =2ans =-1.1429 0.4286 0.7143 -0.1429 ans =-7ans =151 264440 7678. 输入a=[1.6 -2.4 5.2 -0.2],分别使用数学函数ceil、fix、floor、round查看各种取整运算的结果。
a=[1.6 -2.4 5.2 -0.2];ceil(a)fix(a)floor(a)round(a)ans =2 -2 6 0ans =1 -2 5 0ans =1 -3 5 -1ans =2 -2 5 09.求解方程组12341241234123423283687 7225x x x xx x xx x x xx x x x-++=⎧⎪++=⎪⎨-++=⎪⎪+-+=⎩。
A=[2 -3 1 2;1 3 0 1;1 -1 1 8;7 1 -2 2] b=[8;6;7;5]A =2 -3 1 21 3 0 11 -1 1 87 1 -2 2b =8675解法一:x=A\bx =2.53531.20336.8299-0.1452解法二:x=inv(A)*bx =2.53531.20336.8299-0.1452解法三:Cram法则D=det(A);D1=det([b,A(:,2:4)]);D2=det([A(:,1),b,A(:,3:4)]);D3=det([A(:,1:2),b,A(:,4)]);D4=det([A(:,1:3),b]);x1=D1/D,x2=D2/D,x3=D3/D,x4=D4/Dx1 =2.5353x2 =1.2033x3 =6.8299x4 =-0.145210.计算数组123456789A⎡⎤⎢⎥=⎢⎥⎢⎥⎣⎦,111222333B⎡⎤⎢⎥=⎢⎥⎢⎥⎣⎦的左除、右除以及点乘和点除。
A=reshape(1:9,3,3);A=A' B=repmat([1;2;3],1,3)A/BA\BA.*BA./BA =1 2 34 5 67 8 9 B =1 1 12 2 23 3 3Warning: Matrix is singular to working precision. ans =NaN NaN Inf NaN NaN Inf NaN NaN InfWarning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.541976e-018. ans =-0.3333 -0.3333 -0.3333 0.6667 0.6667 0.6667 0 0 0 ans =1 2 3 8 10 12 21 24 27 ans =1.00002.00003.0000 2.0000 2.5000 3.0000 2.3333 2.6667 3.000011.计算函数2()10s i n (4)tf t e t =-的值,其中t 的范围从0~20步长取0.2;f1(t)为f(t)≥0的部分,计算f1(t)的值。
t=0:0.2:20;f=10*exp(2*t)-sin(4*t) f1=f(f>=0) f =1.0e+018 *Columns 1 through 100.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000Columns 11 through 200.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000Columns 21 through 300.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000Columns 31 through 400.0000 0.0000 0.0000 0.0000Columns 41 through 500.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000Columns 51 through 600.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000Columns 61 through 700.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000Columns 71 through 800.0000 0.0000 0.0000 0.0000 0.0001 0.0001 0.0002 0.0002 0.0004 0.0005Columns 81 through 900.0008 0.0012 0.0018 0.0026 0.0039 0.0058 0.0087 0.0130 0.0194 0.0289Columns 91 through 1000.0431 0.0643 0.0959 0.1431 0.2135 0.3186 0.4752 0.7090 1.0577 1.5778Column 1012.3539f1 =1.0e+018 *Columns 1 through 100.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000Columns 11 through 200.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000Columns 21 through 300.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000Columns 31 through 400.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000Columns 41 through 500.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000Columns 51 through 600.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000Columns 61 through 700.0000 0.0000 0.0000 0.0000Columns 71 through 800.0000 0.0000 0.0000 0.0000 0.0001 0.0001 0.0002 0.0002 0.0004 0.0005Columns 81 through 900.0008 0.0012 0.0018 0.0026 0.0039 0.0058 0.0087 0.0130 0.0194 0.0289Columns 91 through 1000.0431 0.0643 0.0959 0.1431 0.2135 0.3186 0.4752 0.7090 1.0577 1.5778Column 1012.353912.创建三维数组a,第一页为1234⎡⎤⎢⎥⎣⎦,第二页为1221⎡⎤⎢⎥⎣⎦,第三页为1222⎡⎤⎢⎥⎣⎦,重排生成数组b为3行、2列、2页。