Matlab程序设计教程(第二版)刘卫国课后参考答案解析
第06章_MATLAB数值计算_参考解答

%可参见教材第 157 页例 6.19
运行结果: dx =
000
%当 x=pi/2 时单独计算 x=pi/2; f=inline('sin(x).^2+cos(x).^2'); dx=diff(f([x, pi]))/(pi/2)
dx = 0
(2) 程序设计: clear all; close all; clc; x=1:3; f=inline('sqrt(x.^2+1)'); dx=diff(f([x, 4]))
运行结果:
U= 1.0e-004 * -0.0675
0.1715
fmin = 1.9920e-010
(2) 程序设计: clear all; close all; clc; f=inline('-sin(x)-cos(x.^2)'); fmax=fminbnd(f, 0, pi)
%用内联函数,求负的最小值 %注意函数名 f 不加单引号'
高教社刘卫国《MATLAB 程序设计与应用》(第二版)习题参考解答
第 6 章:MATLAB 数值计算
教材 P189 习题六
第 6 章 MATLAB 数值计算
1. 利用 MATLAB 提供的 randn 函数生成符合正态分布的 10×5 随机矩阵 A,进行如下操 作:
(1) A 各列元素的均值和标准方差。 (2) A 的最大元素和最小元素。 (3) 求 A 每行元素的和以及全部元素之和。 (4) 分别对 A 的每列元素按升序、每行元素按降序排列。 答:
运行结果: P=
0
15.0000
7.0000 -3.5000
0.5000 -2.0000 -2.0000
MATLAB程序设计与应用(刘卫国编)课后实验答案

实验一 MATLAB 运算基础1. 先求以下表达式的值,然后显示MATLAB 工作空间的利用情形并保留全数变量。
(1) 0122sin851z e=+(2) 21ln(2z x =+,其中2120.455i x +⎡⎤=⎢⎥-⎣⎦ (3) 0.30.330.3sin(0.3)ln , 3.0, 2.9,,2.9,3.022a a e e a z a a --+=++=-- (4) 2242011122123t t z t t t t t ⎧≤<⎪=-≤<⎨⎪-+≤<⎩,其中t =0:: 解:2. 已知:1234413134787,2033657327A B --⎡⎤⎡⎤⎢⎥⎢⎥==⎢⎥⎢⎥⎢⎥⎢⎥-⎣⎦⎣⎦求以下表达式的值:(1) A+6*B 和A-B+I (其中I 为单位矩阵)(2) A*B 和A.*B(3) A^3和A.^3(4) A/B 及B\A(5) [A,B]和[A([1,3],:);B^2]解:运算结果:3. 设有矩阵A 和B1234530166789101769,111213141502341617181920970212223242541311A B ⎡⎤⎡⎤⎢⎥⎢⎥-⎢⎥⎢⎥⎢⎥⎢⎥==-⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎣⎦⎣⎦ (1) 求它们的乘积C 。
(2) 将矩阵C的右下角3×2子矩阵赋给D。
(3) 查看MATLAB工作空间的利用情形。
解:. 运算结果:4. 完成以下操作:(1) 求[100,999]之间能被21整除的数的个数。
(2) 成立一个字符串向量,删除其中的大写字母。
解:(1) 结果:(2). 成立一个字符串向量例如:ch='ABC123d4e56Fg9';那么要求结果是:实验二 MATLAB 矩阵分析与处置1. 设有分块矩阵33322322E R A O S ⨯⨯⨯⨯⎡⎤=⎢⎥⎣⎦,其中E 、R 、O 、S 别离为单位矩阵、随机矩阵、零矩阵和对角阵,试通过数值计算验证22E R RS A O S +⎡⎤=⎢⎥⎣⎦。
MATLAB程序设计与应用(刘卫国编)课后实验答案

实验一 MATLAB 运算基础1. 先求下列表达式的值,然后显示MATLAB 工作空间的使用情况并保存全部变量。
(1) 0122sin851z e =+(2) 21ln(2z x =+,其中2120.455i x +⎡⎤=⎢⎥-⎣⎦ (3) 0.30.330.3sin(0.3)ln , 3.0, 2.9,,2.9,3.022a a e e a z a a --+=++=--(4) 2242011122123t t z t t t t t ⎧≤<⎪=-≤<⎨⎪-+≤<⎩,其中t =0:: 解:2. 已知:1234413134787,2033657327A B --⎡⎤⎡⎤⎢⎥⎢⎥==⎢⎥⎢⎥⎢⎥⎢⎥-⎣⎦⎣⎦求下列表达式的值:(1) A+6*B 和A-B+I (其中I 为单位矩阵)(2) A*B 和A.*B(3) A^3和A.^3(4) A/B 及B\A(5) [A,B]和[A([1,3],:);B^2]解:运算结果:3. 设有矩阵A 和B1234530166789101769,111213141502341617181920970212223242541311A B ⎡⎤⎡⎤⎢⎥⎢⎥-⎢⎥⎢⎥⎢⎥⎢⎥==-⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎣⎦⎣⎦ (1) 求它们的乘积C 。
(2) 将矩阵C 的右下角3×2子矩阵赋给D 。
(3) 查看MATLAB 工作空间的使用情况。
解:. 运算结果:4. 完成下列操作:(1) 求[100,999]之间能被21整除的数的个数。
(2) 建立一个字符串向量,删除其中的大写字母。
解:(1) 结果:(2). 建立一个字符串向量例如:ch='ABC123d4e56Fg9';则要求结果是:实验二 MATLAB 矩阵分析与处理1. 设有分块矩阵33322322E R A O S ⨯⨯⨯⨯⎡⎤=⎢⎥⎣⎦,其中E 、R 、O 、S 分别为单位矩阵、随机矩阵、零矩阵和对角阵,试通过数值计算验证22E R RS A O S +⎡⎤=⎢⎥⎣⎦。
MATLAB程序设计与应用(刘卫国编)课后实验答案

实验一 MATLAB 运算基础1. 先求下列表达式的值,然后显示MATLAB 工作空间的使用情况并保存全部变量。
(1) 0122sin851z e =+ (2) 221ln(1)2z x x =++,其中2120.455i x +⎡⎤=⎢⎥-⎣⎦ (3) 0.30.330.3sin(0.3)ln , 3.0, 2.9,,2.9,3.022a a e e a z a a --+=++=-- (4) 2242011122123t t z t t t t t ⎧≤<⎪=-≤<⎨⎪-+≤<⎩,其中t =0:0.5:2.5 解: M 文件:z1=2*sin(85*pi/180)/(1+exp(2))x=[2 1+2*i;-.45 5];z2=1/2*log(x+sqrt(1+x^2))a=-3.0:0.1:3.0;z3=(exp(0.3.*a)-exp(-0.3.*a))./2.*sin(a+0.3)+log((0.3+a)./2) t=0:0.5:2.5; z4=(t>=0&t<1).*(t.^2)+(t>=1&t<2).*(t.^2-1)+(t>=2&t<3) .*(t.^2-2*t+1)运算结果:z1=2*sin(85*pi/180)/(1+exp(2))x=[2 1+2*i;-.45 5];z2=1/2*log(x+sqrt(1+x^2))a=-3.0:0.1:3.0;z3=(exp(0.3.*a)-exp(-0.3.*a))./2.*sin(a+0.3)+log((0.3+a)./2)t=0:0.5:2.5;z4=(t>=0&t<1).*(t.^2)+(t>=1&t<2).*(t.^2-1)+(t>=2&t<3) .*(t.^2-2*t+1) z1 =0.2375z2 =0.7114 - 0.0253i 0.8968 + 0.3658i0.2139 + 0.9343i 1.1541 - 0.0044iz3 =Columns 1 through 40.7388 + 3.1416i 0.7696 + 3.1416i 0.7871 + 3.1416i 0.7913 + 3.1416iColumns 5 through 80.7822 + 3.1416i 0.7602 + 3.1416i 0.7254 + 3.1416i 0.6784 + 3.1416iColumns 9 through 120.6196 + 3.1416i 0.5496 + 3.1416i 0.4688 + 3.1416i 0.3780 + 3.1416iColumns 13 through 160.2775 + 3.1416i 0.1680 + 3.1416i 0.0497 + 3.1416i -0.0771 + 3.1416iColumns 17 through 20-0.2124 + 3.1416i -0.3566 + 3.1416i -0.5104 + 3.1416i -0.6752 + 3.1416iColumns 21 through 24-0.8536 + 3.1416i -1.0497 + 3.1416i -1.2701 + 3.1416i -1.5271 + 3.1416iColumns 25 through 28-1.8436 + 3.1416i -2.2727 + 3.1416i -2.9837 + 3.1416i -37.0245Columns 29 through 32-3.0017 -2.3085 -1.8971 -1.5978Columns 33 through 36-1.3575 -1.1531 -0.9723 -0.8083Columns 37 through 40-0.6567 -0.5151 -0.3819 -0.2561Columns 41 through 44-0.1374 -0.0255 0.0792 0.1766Columns 45 through 480.2663 0.3478 0.4206 0.4841Columns 49 through 520.5379 0.5815 0.6145 0.6366Columns 53 through 560.6474 0.6470 0.63510.6119Columns 57 through 600.5777 0.5327 0.4774 0.4126Column 610.3388z4 =0 0.2500 0 1.2500 1.0000 2.25002. 已知:1234413134787,2033657327A B --⎡⎤⎡⎤⎢⎥⎢⎥==⎢⎥⎢⎥⎢⎥⎢⎥-⎣⎦⎣⎦求下列表达式的值:(1) A+6*B 和A-B+I (其中I 为单位矩阵)(2) A*B 和A.*B(3) A^3和A.^3(4) A/B 及B\A(5) [A,B]和[A([1,3],:);B^2]解: M 文件:A=[12 34 -4;34 7 87;3 65 7];B=[1 3 -1;2 0 3;3 -2 7]; A+6.*BA-B+eye(3)A*BA.*BA^3A.^3A/BB\A[A,B][A([1,3],:);B^2]运算结果:A=[12 34 -4;34 7 87;3 65 7];B=[1 3 -1;2 0 3;3 -2 7]; A+6.*BA-B+eye(3)A*BA.*BA^3A.^3A/BB\A[A,B][A([1,3],:);B^2]ans =18 52 -1046 7 10521 53 49ans =12 31 -332 8 840 67 1ans =68 44 62309 -72 596154 -5 241ans =12 102 468 0 2619 -130 49ans =37226 233824 48604247370 149188 60076678688 454142 118820 ans =1728 39304 -6439304 343 65850327 274625 343 ans =16.4000 -13.6000 7.600035.8000 -76.2000 50.200067.0000 -134.0000 68.0000ans =109.4000 -131.2000 322.8000-53.0000 85.0000 -171.0000-61.6000 89.8000 -186.2000ans =12 34 -4 1 3 -134 7 87 2 0 33 65 7 3 -2 7ans =12 34 -43 65 74 5 111 0 1920 -5 403. 设有矩阵A 和B1234530166789101769,111213141502341617181920970212223242541311A B ⎡⎤⎡⎤⎢⎥⎢⎥-⎢⎥⎢⎥⎢⎥⎢⎥==-⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎣⎦⎣⎦(1) 求它们的乘积C。
Matlab_程序的设计教程(第二版)刘卫国_课后参考答案

第二章1 求下列表达式的值。
(1)w=sqrt(2)*(1+0.34245*10^(-6))(2)a=3.5;b=5;c=-9.8;x=(2*pi*a+(b+c)/(pi+a*b*c)-exp(2))/tan(b+c)+a (3)a=3.32;b=-7.9;y=2*pi*a^(2)*[(1-pi/4)*b-(0.8333-pi/4)*a](4)t=[2,1-3*i;5,-0.65];z=1/2*exp(2*t)*log(t+sqrt(1+t^(2)))2 求下列表达式A=[-1,5,-4;0,7,8;3,61,7];B=[8,3,-1;2,5,3;-3,2,0];(1)A+6*B A^2-B+eyeA*B A.*B B.*A(3)A/B B\A(4)[A,B] [A([1,3],:);B^2]3 根据已知,完成下列操作(1)A=[23,10,-0.778,0;41,-45,65,5;32,5,0,32;6,-9.54,54,3.14];K=find(A>10&A<25);A(K)(2)A=[23,10,-0.778,0;41,-45,65,5;32,5,0,32;6,-9.54,54,3.14];B=A(1:3,:) C=A(:,1:2) D=A(2:4,3:4) E=B*C(3)E<D E&D E|D ~E|~D第三章1 从键盘输入一个3位数,将它反向输出,如输入639,出数936。
f=input('输入一个数:','s');f(end :-1:1)2 用if语句score=input('请输入成绩:'); if score>=90&&score<=100 disp('A');elseif score>=80&&score<=89disp('B');elseif score>=70&&score<=79disp('C');elseif score>=60&&score<=69;disp('D'); elseif score<60&&score>=0;disp('E');elsedisp('出错');end用switch语句score=input('请输入成绩:'); switch fix(score/10)case {9,10}disp('A');case {8}disp('B');case {7}disp('C');case {6}disp('D');case {0,1,2,3,4,5}disp('E');otherwisedisp('出错');end第四章1题1) X=0:10;Y=x-x.^3/6;Plot(x,y)2) t=0:0.01:2*pi;x=8.*cos(t);y=4*sqrt(2).*sin(t);plot(x,y)2题M文件:t=-pi:pi/10:pi;y=1./(1+exp(-t));subplot(2,2,1);bar(t,y,'b');title('bar(t,y,''b'')');axis([-5,5,-3,3]); subplot(2,2,2);stairs(t,y,'k');title('stairs (t,y,''k'')');axis([-5,5,-3,3]); subplot(2,2,3);stem(t,y,'m');title('stem (t,y,''m'')');axis([-5,5,-3,3]); subplot(2,2,4);loglog(t,y,'g');title('loglog(t,y,''g'')');axis([-5,5,-3,3]);3题1)t=0:pi/100:2*pi;y=5*cos(t)+4;polar(t,y,'-*')2)t=-pi/3:pi/100:pi/3;r=5*sin(t).^2./cos(t);polar(t,r,'-*')4题1)t=0:pi/100:2*pi;x=exp(-t/20).*cos(t);y=exp(-t/20).*sin(t);z=t;plot3(x,y,z)第五章:1题A=randn(10,5)1) X=mean(A)Y=std(A,0,1)2) max(max(A))min(min(A))3) B=sum(A,2)sum(B)4) sort(A);sort(A,2,'descend')2题1)t=0:15:90;x1=[0,0.2588,0.5000,0.7071,0.8660,0.9659,1.0000];a1=0:1:90;y1=interp1(t,x1,a1,'spline')x2=[0,0.2679,0.5774,1.0000,1.7320,3.7320,NaN]; a2=0:1:75;y2= interp1(t,x2,a2,'spline')p1=polyfit(t,x1,5);z1=polyval(p1,a1)p2=polyfit(t,x2,5);z2=polyval(p2,a2)4题P=[2,-3,5,13];Q=[1,5,8];p=polyder(P)p1=polyder(P,Q)[p,q]= polyder(P,Q)5题P1=[1,2,4,0,5];P2=[1,0];P3=[1,2,3];1) P4=conv(P2,P3)P4=[0,1,2,3,0];P=P1+P42) x=roots(P)3) A=[-1,1.2,-1.4;0.75,2,3.5;0,5,2.5];Y=polyval(P,A)4) Z=polyvalm(P,A)第8章1.分解因式. (2)factor(sym(‘5135’))(1)syms A x y;A=x^4-y^4;factor(A)2.求函数的极限(1)syms x; (2)syms x;limit((x-2)/(x^2-4),x,2)f=(sqrt(pi)-sqrt(acos(x)))/sqrt(x+1);limit(f,x,-1,'right')3.求函数的符号导数。
MATLAB程序设计与应用(刘卫国编)课后实验答案

实验一 MATLAB 运算基础1. 先求下列表达式的值,然后显示MATLAB 工作空间的使用情况并保存全部变量。
(1) 0122sin851z e =+ (2) 221ln(1)2z x x =++,其中2120.455i x +⎡⎤=⎢⎥-⎣⎦ (3) 0.30.330.3sin(0.3)ln , 3.0, 2.9,,2.9,3.022a a e e a z a a --+=++=-- (4) 2242011122123t t z t t t t t ⎧≤<⎪=-≤<⎨⎪-+≤<⎩,其中t =0:0.5:2.5 解: M 文件:z1=2*sin(85*pi/180)/(1+exp(2))x=[2 1+2*i;-.45 5];z2=1/2*log(x+sqrt(1+x^2))a=-3.0:0.1:3.0;z3=(exp(0.3.*a)-exp(-0.3.*a))./2.*sin(a+0.3)+log((0.3+a)./2) t=0:0.5:2.5; z4=(t>=0&t<1).*(t.^2)+(t>=1&t<2).*(t.^2-1)+(t>=2&t<3) .*(t.^2-2*t+1)运算结果:z1=2*sin(85*pi/180)/(1+exp(2))x=[2 1+2*i;-.45 5];z2=1/2*log(x+sqrt(1+x^2))a=-3.0:0.1:3.0;z3=(exp(0.3.*a)-exp(-0.3.*a))./2.*sin(a+0.3)+log((0.3+a)./2)t=0:0.5:2.5;z4=(t>=0&t<1).*(t.^2)+(t>=1&t<2).*(t.^2-1)+(t>=2&t<3) .*(t.^2-2*t+1) z1 =0.2375z2 =0.7114 - 0.0253i 0.8968 + 0.3658i0.2139 + 0.9343i 1.1541 - 0.0044iz3 =Columns 1 through 40.7388 + 3.1416i 0.7696 + 3.1416i 0.7871 + 3.1416i 0.7913 + 3.1416iColumns 5 through 80.7822 + 3.1416i 0.7602 + 3.1416i 0.7254 + 3.1416i 0.6784 + 3.1416iColumns 9 through 120.6196 + 3.1416i 0.5496 + 3.1416i 0.4688 + 3.1416i 0.3780 + 3.1416iColumns 13 through 160.2775 + 3.1416i 0.1680 + 3.1416i 0.0497 + 3.1416i -0.0771 + 3.1416iColumns 17 through 20-0.2124 + 3.1416i -0.3566 + 3.1416i -0.5104 + 3.1416i -0.6752 + 3.1416iColumns 21 through 24-0.8536 + 3.1416i -1.0497 + 3.1416i -1.2701 + 3.1416i -1.5271 + 3.1416iColumns 25 through 28-1.8436 + 3.1416i -2.2727 + 3.1416i -2.9837 + 3.1416i -37.0245Columns 29 through 32-3.0017 -2.3085 -1.8971 -1.5978Columns 33 through 36-1.3575 -1.1531 -0.9723 -0.8083Columns 37 through 40-0.6567 -0.5151 -0.3819 -0.2561Columns 41 through 44-0.1374 -0.0255 0.0792 0.1766Columns 45 through 480.2663 0.3478 0.4206 0.4841Columns 49 through 520.5379 0.5815 0.6145 0.6366Columns 53 through 560.6474 0.6470 0.63510.6119Columns 57 through 600.5777 0.5327 0.4774 0.4126Column 610.3388z4 =0 0.2500 0 1.2500 1.0000 2.25002. 已知:1234413134787,2033657327A B --⎡⎤⎡⎤⎢⎥⎢⎥==⎢⎥⎢⎥⎢⎥⎢⎥-⎣⎦⎣⎦求下列表达式的值:(1) A+6*B 和A-B+I (其中I 为单位矩阵)(2) A*B 和A.*B(3) A^3和A.^3(4) A/B 及B\A(5) [A,B]和[A([1,3],:);B^2]解: M 文件:A=[12 34 -4;34 7 87;3 65 7];B=[1 3 -1;2 0 3;3 -2 7]; A+6.*BA-B+eye(3)A*BA.*BA^3A.^3A/BB\A[A,B][A([1,3],:);B^2]运算结果:A=[12 34 -4;34 7 87;3 65 7];B=[1 3 -1;2 0 3;3 -2 7]; A+6.*BA-B+eye(3)A*BA.*BA^3A.^3A/BB\A[A,B][A([1,3],:);B^2]ans =18 52 -1046 7 10521 53 49ans =12 31 -332 8 840 67 1ans =68 44 62309 -72 596154 -5 241ans =12 102 468 0 2619 -130 49ans =37226 233824 48604247370 149188 60076678688 454142 118820 ans =1728 39304 -6439304 343 65850327 274625 343 ans =16.4000 -13.6000 7.600035.8000 -76.2000 50.200067.0000 -134.0000 68.0000ans =109.4000 -131.2000 322.8000-53.0000 85.0000 -171.0000-61.6000 89.8000 -186.2000ans =12 34 -4 1 3 -134 7 87 2 0 33 65 7 3 -2 7ans =12 34 -43 65 74 5 111 0 1920 -5 403. 设有矩阵A 和B1234530166789101769,111213141502341617181920970212223242541311A B ⎡⎤⎡⎤⎢⎥⎢⎥-⎢⎥⎢⎥⎢⎥⎢⎥==-⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎣⎦⎣⎦(1) 求它们的乘积C。
Matlab程序设计教程[第二版]刘卫国课后参考答案及解析
![Matlab程序设计教程[第二版]刘卫国课后参考答案及解析](https://img.taocdn.com/s3/m/2b09c50ea5e9856a5612605a.png)
第二章1 求下列表达式的值。
(1)w=sqrt(2)*(1+0.34245*10^(-6))(2)a=3.5;b=5;c=-9.8;x=(2*pi*a+(b+c)/(pi+a*b*c)-exp(2))/tan(b+c)+a(3)a=3.32;b=-7.9;y=2*pi*a^(2)*[(1-pi/4)*b-(0.8333-pi/4)*a](4)t=[2,1-3*i;5,-0.65];z=1/2*exp(2*t)*log(t+sqrt(1+t^(2)))2 求下列表达式A=[-1,5,-4;0,7,8;3,61,7];B=[8,3,-1;2,5,3;-3,2,0];(1)A+6*B A^2-B+eye(2)A*B A.*B B.*A(3)A/B B\A(4)[A,B] [A([1,3],:);B^2]3 根据已知,完成下列操作(1)A=[23,10,-0.778,0;41,-45,65,5;32,5,0,32;6,-9.54,54,3.14]; K=find(A>10&A<25);A(K)(2)A=[23,10,-0.778,0;41,-45,65,5;32,5,0,32;6,-9.54,54,3.14]; B=A(1:3,:) C=A(:,1:2) D=A(2:4,3:4) E=B*C(3)E<D E&D E|D ~E|~D第三章1 从键盘输入一个3位数,将它反向输出,如输入639,出数936。
f=input('输入一个数:','s');f(end :-1:1)2 用if语句score=input('请输入成绩:');if score>=90&&score<=100disp('A');elseif score>=80&&score<=89disp('B');elseif score>=70&&score<=79disp('C');elseif score>=60&&score<=69;disp('D');elseif score<60&&score>=0;disp('E');elsedisp('出错');end用switch语句score=input('请输入成绩:');switch fix(score/10)case {9,10}disp('A');case {8}disp('B');case {7}disp('C');case {6}disp('D');case {0,1,2,3,4,5}disp('E');otherwisedisp('出错');end第四章1题1) X=0:10;Y=x-x.^3/6;Plot(x,y)2) t=0:0.01:2*pi;x=8.*cos(t);y=4*sqrt(2).*sin(t);plot(x,y)2题M文件:t=-pi:pi/10:pi;y=1./(1+exp(-t));subplot(2,2,1);bar(t,y,'b');title('bar(t,y,''b'')');axis([-5,5,-3,3]); subplot(2,2,2);stairs(t,y,'k');title('stairs (t,y,''k'')');axis([-5,5,-3,3]); subplot(2,2,3);stem(t,y,'m');title('stem (t,y,''m'')');axis([-5,5,-3,3]); subplot(2,2,4);loglog(t,y,'g');title('loglog(t,y,''g'')');axis([-5,5,-3,3]);3题1) t=0:pi/100:2*pi;y=5*cos(t)+4;polar(t,y,'-*')2) t=-pi/3:pi/100:pi/3;r=5*sin(t).^2./cos(t);polar(t,r,'-*')4题1)t=0:pi/100:2*pi;x=exp(-t/20).*cos(t);y=exp(-t/20).*sin(t);z=t;plot3(x,y,z)第五章:1题A=randn(10,5)1) X=mean(A)Y=std(A,0,1)2) max(max(A))min(min(A))3) B=sum(A,2)sum(B)4) sort(A);sort(A,2,'descend')2题1) t=0:15:90;x1=[0,0.2588,0.5000,0.7071,0.8660,0.9659,1.0000]; a1=0:1:90;y1=interp1(t,x1,a1,'spline')x2=[0,0.2679,0.5774,1.0000,1.7320,3.7320,NaN];a2=0:1:75;y2= interp1(t,x2,a2,'spline')p1=polyfit(t,x1,5);z1=polyval(p1,a1)p2=polyfit(t,x2,5);z2=polyval(p2,a2)4题P=[2,-3,5,13];Q=[1,5,8];p=polyder(P)p1=polyder(P,Q)[p,q]= polyder(P,Q)5题P1=[1,2,4,0,5];P2=[1,0];P3=[1,2,3];1) P4=conv(P2,P3)P4=[0,1,2,3,0];P=P1+P42) x=roots(P)3) A=[-1,1.2,-1.4;0.75,2,3.5;0,5,2.5];Y=polyval(P,A)4) Z=polyvalm(P,A)第8章1.分解因式. (2)factor(sym(‘5135’))(1)syms A x y;A=x^4-y^4;factor(A)2.求函数的极限(1)syms x; (2)syms x;limit((x-2)/(x^2-4),x,2)f=(sqrt(pi)-sqrt(acos(x)))/sqrt(x+1);limit(f,x,-1,'right')3.求函数的符号导数。
MATLAB程序设计与应用(刘卫国)》(第二版)-课后实验答案

《MATLAB程序设计与应用(刘卫国)》(第二版)实验一MATLAB运算基础1.(1) z1=2*sin(pi*85/180)/(1+exp(2))(2)x=[2,1+2i;-0.45,5];z2=log(x+sqrt(1+x.^2))/2(3)a=-3.0:0.1:3.0;z3=0.5*(exp(0.3*a)-exp(-0.3*a)).*sin(a+0.3)+log((0.3+a)/2)(4)t=0:0.5:2.5;z4=(t>=0&t<1).*t.^2+(t>=1&t<2).*(t.^2-1)+(t>=2&t<3).*(t.^2-2*t+1)2. A=[12,34,-4;34,7,87;3,65,7];B=[1,3,-1;2,0,3;3,-2,7];(1)a=A+6*Bb=A-B+eye(size(A)) %I=eye(size(A))(2)c=A*Bd=A.*B(3)e=A^3f=A.^3(4)g=A/Bh=B\A(5)m=[A,B]n=[A([1,3],:);B^2]3. A=[1,2,3,4,5;6,7,8,9,10;11,12,13,14,15;16,17,18,19,20;21,22,23,24,25];B=[3,0,16;17,-6,9;0,23,-4;9,7,0;4,13,11];(1)C=A*B(2)D=C(3:end,2:end)4.(1) a=100:999;b=rem(a,21)==0;c=find(b);d=length(c)(2)ch='Just as Bianhaiman said,Xiehong is ...';e=find(ch>='A'&ch<='Z');ch(e)=[]实验二 MATLAB矩阵分析与处理1.E=eye(3);R=rand(3,2);O=zeros(2,3);S=diag([1,2]);A=[E,R+R*S;O,S^2];a=A^22. H=hilb(5);P=pascal(5);Hh=det(H)Hp=det(P)Th=cond(H)Tp=cond(P)3. A=rand(5,5);a=det(A) %行列式的值b=trace(A) %行列式的迹c=rank(A) %行列式的秩d=norm(A) %行列式的范数4. A=[-29,6,18;20,5,12;-8,8,5];[V,D]=eig(A)5. A=[1/2,1/3,1/4;1/3,1/4,1/5;1/4,1/5,1/6];b=[0.95,0.67,0.52]';(1)x=inv(A)*b(2)b1=[0.95,0.67,0.53]';x=inv(A)*b1(3)c=cond(A)6. A=rand(3,3);B=sqrtm(A)C=sqrt(A)实验三选择结构程序设计1. x=input('请输入x的值:');if x<0&x~=-3y=x*x+x-6;elseif x>=0&x<5&x~=2&x~=3y=x*x-5*x+6;elsey=x*x-x-1;endy2.(if语句)score=input('请输入成绩:');if score>=0&score<60disp('E');elseif score>=60&score<70disp('D');elseif score>=70&score<80disp('C');elseif score>=80&score<90disp('B');elseif score>=90&score<=100disp('A');elsedisp('您输入的成绩有误,请重新输入!');end(switch语句)score=input('请输入成绩:');switch fix(score/10)case num2cell(0:5)disp('E');case {6}disp('D');case {7}disp('C');case {8}disp('B');case {9}disp('A');otherwisedisp('您输入的成绩有误,请重新输入!'); end3. a=input('请输入工号:','s');b=input('请输入该员工的工时数:');if b>120y=120*84+(b-120)*(1+15/100)*84;elseif b<60y=b*84-700;elsey=b*84;endy4.a=10+fix(89*rand());b=10+fix(89*rand());c=input('请输入一个运算符:','s'); if c=='+'disp(a+b);elseif c=='-'disp(a-b);elseif c=='*'disp(a*b);elsedisp(a/b);end5. A=rand(5,6);n=1:5;n=input('请输入n的值:');tryB=A(n,:);catchB=A(end,:);endBlasterr实验四循环结构程序设计1.(循环结构)n=input('请输入n的值:');y=0;for i=1:ny=y+1/i/i;pi=sqrt(6*y);endpi(向量运算)n=input('请输入n的值:');i=1:n;y=1./i.^2;f=sum(y);pi=sqrt(f*6)2. y=0;n=0;while 1n=n+1;y=y+1/(2*n-1);if y>=3breakendendny=y-1/(2*n-1)3. a=input('输入a的值:');b=input('输入b的值:');x0=1.0;n=1;x1=a/(b+x0);while abs(x1-x0)>=10^(-5)|n<=500 x1=a/(b+x0);x0=x1;n=n+1;endx14. f(1)=1;f(2)=0;f(3)=1;for n=4:100f(n)=f(n-1)-2*f(n-2)+f(n-3); enda=max(f) %最大值b=min(f) %最小值c=sum(f) %各数之和d=length(find(f>0)) %正数的个数e=length(find(f==0)) %零的个数f=length(find(f<0)) %负数的个数5. n=0;s=0;for k=2:49sn=k*(k+1)-1;m=fix(sqrt(sn));for i=2:mif rem(sn,i)==0breakelseendendif i==mn=n+1;s=s+sn;endendns实验五函数文件1.(函数文件)function [e,l,s,c]=f1(x)e=exp(x);l=log10(x);s=sin(x);c=cos(x);(命令文件)x=input('请输入一个复数:'); [e,l,s,c]=f1(x);elsc2.(函数文件)function [x]=f2(A,B)C=B';x=C*inv(A);(命令文件)m1=input('Enter m1:');m2=input('Enter m2:');theta=input('Enter theta:'); g=9.8;A=[m1*cos(theta),-m1,-sin(theta),0;m1*sin(theta),0,cos(theta),0;0,m2,-sin(theta),0;0,0,-cos(theta),1];B=[0,m1*g,0,m2*g]';[x]=f2(A,B);x3.(函数文件)function f=f3(x)f=1;for i=2:sqrt(x)if rem(x,i)==0f=0;breakendend(命令文件)for i=10:99j=10*rem(i,10)+fix(i/10);if f3(i)&f3(j)disp(i);endend4.(函数文件)function f=f4(x)f=1./((x-2).^2+0.1)+1./((x-3).^4+0.01);(命令文件)x=[1,2,3;4,5,6];y=f4(x)5.(1)(函数文件)function f=f5(n)f=n+10*log(n^2+5);(命令文件)y=f5(40)/(f5(30)+f5(20))(2)(函数文件)function f=f6(i)g=0;for i=1:nf=g+i.*(i+1);end(命令文件)y=f6(40)/(f6(30)+f6(20))实验六高层绘图操作1. x=linspace(0,2*pi,101);y=(0.5+3*sin(x)./(1+x.*x));plot(x,y);title('实验六第一题');xlabel('X');ylabel('Y');text(3,0.6,'y=0.5+3sin(x)/(1+x^2)');2.(1)x=linspace(0,pi,100);y1=x.^2;y2=cos(2*x);y3=y1.*y2;plot(x,y1,'b',x,y2,'g',x,y3,'r'); title('同一坐标系下得三条曲线');text(2,4,'y1=x^2');text(2,-0,8,'y2=cos(2x)');text(2,-2.2,'y=x^2*cos(2x)');xlabel('X');ylabel('Y');(2)x=linspace(0,pi,100); y1=x.^2;y2=cos(2*x);y3=y1.*y2;subplot(1,3,1);plot(x,y1);title('y1=x^2');xlabel('X');ylabel('Y'); subplot(1,3,2);plot(x,y2);title('y2=cos(x)');xlabel('X');ylabel('Y') subplot(1,3,3);plot(x,y3);title('y3=x^2*cos(x)'); xlabel('X');ylabel('Y');(3)x=linspace(0,2*pi,20); y1=x.^2;y2=cos(2*x);y3=y1.*y2;subplot(4,3,1);bar(x,y1);subplot(4,3,2);bar(x,y2);subplot(4,3,3);bar(x,y3);subplot(4,3,4);stairs(x,y1);subplot(4,3,5);stairs(x,y2);subplot(4,3,6);stairs(x,y3);subplot(4,3,7);stem(x,y1);subplot(4,3,8);stem(x,y2);subplot(4,3,9);stem(x,y3);subplot(4,3,10);fill(x,y1,'b');subplot(4,3,11);fill(x,y2,'r');subplot(4,3,12);fill(x,y3,'g');3. x=linspace(-5,5,100);y=[];for x0=xif x0>=-5&x<=0y=[y,(x0+sqrt(pi))/exp(2)];elsey=[y,log(x0+sqrt(1+x0.^2))/2];endendplot(x,y);title('实验六第三题');xlabel('X');ylabel('Y');4. a=input('请输入a:');b=input('请输入b:');n=input('请输入n:');theta=0:0.01:2*pi;rho=a.*sin(b+n.*theta);polar(theta,rho,'r');title('极坐标曲线');(如:当输入a=3,b=4,n=6时,图形为:)5. x=linspace(-5,5,21);y=linspace(0,10,31);[x,y]=meshgrid(x,y);z=cos(x).*cos(y).*exp(-sqrt(x.*x+y.*y)/4); subplot(2,1,1);surf(x,y,z);title('曲面图');xlabel('X');ylabel('Y');zlabel('Z'); subplot(2,1,2);surfc(x,y,z);title('等高线图');xlabel('X');ylabel('Y');zlabel('Z');6.(方法一)s=0:pi/100:pi/2;t=0:pi/100:3*pi/2;[s,t]=meshgrid(s,t);x=cos(s).*cos(t);y=cos(s).*sin(t);z=sin(s);surf(x,y,z);shading interp;title('实验六第六题');xlabel('X');ylabel('Y');zlabel('Z');(方法二)ezsurf('cos(s)*cos(t)','cos(s)*sin(t)','sin(s)',[0,0.5*pi,0,1.5*pi]); shading interp;实验七低层绘图操作1. hf=figure('MenuBar','figure','NumberTitle','off','Color','r',...'Name','图形窗口示例',...'WindowButtonDownFcn','disp(''Left Button Pressed.'')');2. x=-1:0.1:1;y=x.^2.*exp(2*x);h=line('XData',x,'YData',y);text(0.6,0.36*exp(1.2),'\leftarrow y=x^2*exp(2*x)');set(h,'Color','r','LineStyle','--','LineWidth',2);grid on;title('函数y=x^2*exp(2*x)');3. x=0:0.00001:0.001;[x,t]=meshgrid(x);v=10*exp(-0.01*x).*sin(2000*pi*t-0.2*x+pi);axes('view',[-37.5,30]);hf=surface(x,t,v);grid on;xlabel('X');ylabel('Y');zlabel('Z');title('曲面v=10*exp(-0.01*x)*sin(2000*pi*t-0.2*x+pi)');set(hf,'EdgeColor','interp','FaceColor','r','LineStyle',':','LineWidth',2);4. x=linspace(0,2*pi,20);y1=sin(x);y2=cos(x);y3=sin(x)./(cos(x)+eps);y4=cos(x)./(sin(x)+eps);axes('Position',[0.15,0.1,0.2,0.4]);plot(x,y1);title('y1=sin(x)');axes('Position',[0.05,0.6,0.2,0.3]);plot(x,y2);title('y2=cos(x)');axes('Position',[0.45,0.6,0.3,0.3]);plot(x,y3);title('y3=tan(x)');axes('Position',[0.5,0.2,0.3,0.2]);plot(x,y4);title('y4=cot(x)');5. cylinder(3);light('Position',[1,2,4]); shading interp;lighting gouraud;material shiny实验八数据处理与多项式计算1.A=rand(1,30000);a=mean(A) %均值b=std(A,0,2) %标准方差c=max(A) %最大元素d=min(A) %最小元素n=0;for i=1:30000if A(i)>0.5n=n+1; %大于0.5的随机数的个数endendm=n/30000 %百分比2. P=45+fix(50*rand(100,5));[zgf,xh]=max(P) %每门课的最高分及相应的学号[zdf,xh]=min(P) %每门课的最低分及相应的学号pjf=mean(P) %每门课的平均分fc=std(P) %标准方差A=sum(P,2); %总分[zfzg,xh]=max(A) %总分最高分及学号[zfzd,xh]=min(A) %总分最低分及学号[zcj,xsxh]=sort(A,1,'descend')3. h=6:2:18;x=6.5:2:17.5;t1=[18.0 20.0 22.0 25.0 30.0 28.0 24.0];t2=[15.0 19.0 24.0 28.0 34.0 32.0 30.0];a=interp1(h,t1,x,'spline')b=interp1(h,t2,x,'spline')4. x=linspace(1,101,10);y=log10(x);p=polyfit(x,y,5);y1=polyval(p,x);plot(x,y,'r:o',x,y1,'-*')5. p1=[1,2,4,0,5];p2=[1,2];p3=[1,2,3];p4=conv(p2,p3);p41=[0,p4];p=p1+p41 %P(x)=P1(x)+P2(x)*P3(x) x=roots(p) %根A=[-1,1.2,-1.4;0.75,2,3.5;0,5,2.5];y1=polyval(p,A)y2=polyvalm(p,A)。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第二章
1 求下列表达式的值。
(1)
w=sqrt(2)*(1+0.34245*10^(-6))
(2)
a=3.5;
b=5;
c=-9.8;
x=(2*pi*a+(b+c)/(pi+a*b*c)-exp(2))/tan(b+c)+a
(3)
a=3.32;
b=-7.9;
y=2*pi*a^(2)*[(1-pi/4)*b-(0.8333-pi/4)*a]
(4)
t=[2,1-3*i;5,-0.65];
z=1/2*exp(2*t)*log(t+sqrt(1+t^(2)))
2 求下列表达式
A=[-1,5,-4;0,7,8;3,61,7];
B=[8,3,-1;2,5,3;-3,2,0];
(1)
A+6*B A^2-B+eye
(2)
A*B A.*B B.*A
(3)
A/B B\A
(4)
[A,B] [A([1,3],:);B^2]
3 根据已知,完成下列操作
(1)
A=[23,10,-0.778,0;41,-45,65,5;32,5,0,32;6,-9.54,54,3.14];
K=find(A>10&A<25);
A(K)
(2)
A=[23,10,-0.778,0;41,-45,65,5;32,5,0,32;6,-9.54,54,3.14];
B=A(1:3,:) C=A(:,1:2) D=A(2:4,3:4) E=B*C
(3)
E<D E&D E|D ~E|~D
第三章
1 从键盘输入一个3位数,将它反向输出,如输入639,出数936。
f=input('输入一个数:','s');
f(end :-1:1)
2 用if语句
score=input('请输入成绩:');
if score>=90&&score<=100
disp('A');
elseif score>=80&&score<=89
disp('B');
elseif score>=70&&score<=79
disp('C');
elseif score>=60&&score<=69;
disp('D');
elseif score<60&&score>=0;
disp('E');
else
disp('出错');
end
用switch语句
score=input('请输入成绩:');
switch fix(score/10)
case {9,10}
disp('A');
case {8}
disp('B');
case {7}
disp('C');
case {6}
disp('D');
case {0,1,2,3,4,5}
disp('E');
otherwise
disp('出错');
end
第四章1题
1) X=0:10;
Y=x-x.^3/6;
Plot(x,y)
2) t=0:0.01:2*pi;
x=8.*cos(t);
y=4*sqrt(2).*sin(t);
plot(x,y)
2题
M文件:
t=-pi:pi/10:pi;
y=1./(1+exp(-t));
subplot(2,2,1);bar(t,y,'b');
title('bar(t,y,''b'')');axis([-5,5,-3,3]); subplot(2,2,2);stairs(t,y,'k');
title('stairs (t,y,''k'')');axis([-5,5,-3,3]); subplot(2,2,3);stem(t,y,'m');
title('stem (t,y,''m'')');axis([-5,5,-3,3]); subplot(2,2,4);loglog(t,y,'g');
title('loglog(t,y,''g'')');axis([-5,5,-3,3]);
3题
1) t=0:pi/100:2*pi;
y=5*cos(t)+4;
polar(t,y,'-*')
2) t=-pi/3:pi/100:pi/3;
r=5*sin(t).^2./cos(t);
polar(t,r,'-*')
4题
1)
t=0:pi/100:2*pi;
x=exp(-t/20).*cos(t);
y=exp(-t/20).*sin(t);
z=t;
plot3(x,y,z)
第五章:1题
A=randn(10,5)
1) X=mean(A)
Y=std(A,0,1)
2) max(max(A))
min(min(A))
3) B=sum(A,2)
sum(B)
4) sort(A);
sort(A,2,'descend')
2题
1) t=0:15:90;
x1=[0,0.2588,0.5000,0.7071,0.8660,0.9659,1.0000]; a1=0:1:90;
y1=interp1(t,x1,a1,'spline')
x2=[0,0.2679,0.5774,1.0000,1.7320,3.7320,NaN];
a2=0:1:75;
y2= interp1(t,x2,a2,'spline')
p1=polyfit(t,x1,5);
z1=polyval(p1,a1)
p2=polyfit(t,x2,5);
z2=polyval(p2,a2)
4题
P=[2,-3,5,13];
Q=[1,5,8];
p=polyder(P)
p1=polyder(P,Q)
[p,q]= polyder(P,Q)
5题
P1=[1,2,4,0,5];
P2=[1,0];
P3=[1,2,3];
1) P4=conv(P2,P3)
P4=[0,1,2,3,0];
P=P1+P4
2) x=roots(P)
3) A=[-1,1.2,-1.4;0.75,2,3.5;0,5,2.5];
Y=polyval(P,A)
4) Z=polyvalm(P,A)
第8章
1.分解因式. (2)factor(sym(‘5135’))
(1)syms A x y;
A=x^4-y^4;
factor(A)
2.求函数的极限
(1)syms x; (2)syms x;
limit((x-2)/(x^2-4),x,2)
f=(sqrt(pi)-sqrt(acos(x)))/sqrt(x+1);
limit(f,x,-1,'right')
3.求函数的符号导数。
(1)syms x y (2)sym x;
f=sin(1/x); f=(1-cos(2*x))/x;
diff(f) diff(f,x,2) diff(f) diff(f,x,2)
4.求积分
(1)x=sym(‘x’) (2)sym(‘y’)
f=sqrt(exp(x)+1) f=x/(x+y)
int(f) int(f)
(3) (4)
f=exp(x).*(1+exp(x))^2; f=x .*log(x)
int(f,0,log(2)) int(f,1,exp(1))
5.求下列级数之和
syms n;
s=symsum((-1)^(n+1)/n,1,inf)
x=sym(‘x');
syms n;
s=symsum(x^(2*n-1)/(2*n-1),x,inf)
6.求函数在x=x.处的泰勒展开式
(1)syms x (2) syms x
f1= (exp(x)+exp(-x))/2 f1=sqrt(x^3-2*x+1) taylor(f1,x,5) taylor(f1,x,6)
7.求非线性方程的符号解
(1)syms x a y
X=slove(‘x^3+a*x+1’,’x’)。