MATLAB程序设计与应用第二版课后题答案 2

更新时间:2023-08-27 12:50:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

第二章

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 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 0 0 1 0 0 0 L2 =

1 1 1 1 1 0 0 0 L3 =

0 0 0 1 1 1 0 0 L4 =

4 5 6 5.(1)B=A(1:3,:)

C=A(:,1:2) D=A(2:4,3:4) E=B*C B =

23.0000 10.0000 -0.7780 0 41.0000 -45.0000 65.0000 5.0000 32.0000 5.0000 0 32.0000 C =

23.0000 10.0000 41.0000 -45.0000 32.0000 5.0000 6.0000 -9.5400 D =

65.0000 5.0000

45 21 0 0 0 75 53 11

0 32.0000 54.0000 3.1400 E =

1.0e+003 *

0.9141 -0.2239 1.2080 2.7123 1.1330 -0.2103 (2)E<D ans =

0 1 0 0 0 1 E&D ans =

1 1 0 1 1 1 E|D ans =

1 1 1 1 1 1 ~D|~E ans =

0 0 1 0 0 0

find(A>=10&A<25) ans = 1 5 6.

all(A)

ans = 0 any(A) ans = 1 isnan(A) ans =

0 1 0 0 isinf(A) ans =

0 0 0

0 0 1 1 0 0 0 isfinite(A) ans =

1 0 0 0 1 1 1

7.

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’;………. 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 2 ndims(B) ans =

2 (2) B(2) ans =

[3x3 double B(4) ans =

{3x3 cell} (3) B(3)=[] B =

[1] [3x3 double] {3x3 cell} B{3}=[] B =

[1] [3x3 double] []

第三章

1.

(1)A=eye(3)

(2) 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*B E=B*A

D=E 因此A与A-1是互逆的。 4.

A=[4 2 -1;3 -1 2;12 3 0]; b=[2;10;8]; x=inv(A)*b x =

-6.0000 26.6667 27.3333 5. (1)

diag(A) ;主对角线元素 ans = 1 1 5 9

triu(A) ;上三角阵 ans =

1 -1 2 3 0 1 -4 2 0 0 5 2 0 0 0 9 tril(A) ;下三角阵 ans =

1 0 0 0 5 1 0 0 3 0 5 0

11 15 0 9 rank(A) ;秩 ans =

4

norm(A) ;范数 ans =

21.3005

cond(A) ;条件数 ans =

11.1739

trace(A) ;迹

ans =

16 (2)略 6.

A=[1 1 0.5;1 1 0.25;0.5 0.25 2] A =

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 0.4443 0.5315 -0.6863 0.5621 0.4615 -0.0937 -0.6976 0.7103 D =

-0.0166 0 0 0 1.4801 0 0 0 2.5365

第四章

1.a=input('请输入一个4位数:'); while (a<1000|a>9999)

a=input('输入错误,请重新输入一个4位数:'); end

b=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); 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);

x2=(x>=1.5&x<3.5); x3=(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(['平均数是:',num2str(x1)]) m=(rem(x,2)==0&x<x1); n=find(m);

disp(['小于平均数的数是:',num2str(x(n))]); 4.A=input('请输入20个数:'); n=A; a=n; b=n; for n=A if a>=n a=n;

elseif b<=n b=n; end end

disp(['min:',num2str(a)]) disp(['max:',num2str(b)])

请输入20个数:[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] min:1 max:20

5.s=0; x=0;

for n=1:64 c=2^x; x=x+1; s=s+c; end

disp(['2的0次方到63次方的和是:',num2str(s)])

2的0次方到63次方的和是:18446744073709552000

6,(1)sum1=0; for n=1:100

x=(-1)^(n+1)*(1/n); sum1=sum1+x; end

disp(['当n取100时: sum=',num2str(sum1)]) 当n取100时: sum=0.68817

sum2=0;

for n=1:1000

x=(-1)^(n+1)*(1/n); sum2=sum2+x; end

disp(['当n取1000时: sum=',num2str(sum2)]) 当n取1000时: sum=0.69265

sum3=0;

for n=1:10000

x=(-1)^(n+1)*(1/n); sum3=sum3+x; end

disp(['当n取10000时:sum=',num2str(sum3)]) 当n取10000时:sum=0.6931 (2)

sum1=0;

for n=1:2:100

x=(-1)^(n+1)*(1/(n+1)); sum1=sum1+x; end

disp(['当n取100时: sum=',num2str(sum1)]) 当n取100时: sum=2.2496

sum1=0;

for n=1:2:1000

x=(-1)^(n+1)*(1/(n+1)); sum1=sum1+x; end

disp(['当n取1000时: sum=',num2str(sum1)]) 当n取1000时: sum=3.3964

sum1=0;

for n=1:2:10000

x=(-1)^(n+1)*(1/(n+1)); sum1=sum1+x; end

disp(['当n取10000时: sum=',num2str(sum1)]) 当n取10000时: sum=4.5473 (3)

sum1=0; for n=1:100 x=(1/4)^n; sum1=sum1+x; end

disp(['当n取100时: sum=',num2str(sum1)]) 当n取100时: sum=0.33333

sum1=0;

for n=1:1000 x=(1/4)^n; sum1=sum1+x; end

disp(['当n取1000时: sum=',num2str(sum1)]) 当n取1000时: sum=0.33333

sum1=0;

for n=1:10000 x=(1/4)^n; sum1=sum1+x; end

disp(['当n取10000时: sum=',num2str(sum1)]) 当n取1000时: sum=0.33333 (4)

sum1=0; for n=1:100

x=[(2*n)*(2*n)]/[(2*n-1)*(2*n+1)]; sum1=sum1+x; end

disp(['当n取100时: sum=',num2str(sum1)]) 当n取100时: sum=100.4975

sum1=0;

for n=1:1000

x=[(2*n)*(2*n)]/[(2*n-1)*(2*n+1)]; sum1=sum1+x; end

disp(['当n取1000时: sum=',num2str(sum1)]) 当n取1000时: sum=1000.4998

sum1=0;

for n=1:10000

x=[(2*n)*(2*n)]/[(2*n-1)*(2*n+1)]; sum1=sum1+x; end

disp(['当n取10000时: sum=',num2str(sum1)]) 当n取10000时: sum=10000.5 7,

function f=factor(n); f(1)=1;f(2)=1; for k=3:n

f(k)=f(k-1)+f(k-2); end

运行结果:

f=factor(10)

f=1 1 2 3 5 8 13 21 34 55

8. function [f1,f2]=factor(a,b); f1=a*b; f2=a.*b; 运行结果:

[f1,f2]=factor([1,2;1 2],[1 3;1 3]) f1 =

3 9 3 9 f2 =

1 6 1 6 9

function f=factor(n,m); y=0;

for k=1:n y=y+k^m; end

10.(1)S=108 (2)x=4 12 20 y=2 4 6

第五章

1. (1) x=-10:0.1:10; y=100./(1+x.^2);

plot(x,y)

(2) x=-10:0.1:10;

y=1/(2*pi)*exp(-x.^2/2);

plot(x,y)

(3) ezplot('x^2+y^2=1')

(4)

t=-10:0.1:10; x=t.^2; y=5*t.^3;

plot(x,y)

2. (1)

theta=0:0.01:2*pi; rho=5*cos(theta)+4; polar(theta,rho)

(2)

theta=0.001:0.1:2*pi; rho=12./sqrt(theta);

polar(theta,rho)

(3) theta=0.001:0.1:2*pi; rho=5./cos(theta)-7; polar(theta,rho)

(4)

theta=0.001:0.1:2*pi; rho=pi/3.*theta.^2;

polar(theta,rho)

3. (1)

t=0:pi/100:2*pi; x=cos(t); y=sin(t); z=t;

plot3(x,y,z)

(2)

u=0:pi/100:2*pi; v=0:pi/100:2*pi;

x=(1+cos(u)).*cos(v); y=(1+cos(u)).*sin(v); z=sin(u);

plot3(x,y,z)

(3) (4) 5.

plot函数:

>> x=linspace(-10,10,200); >> y=[]; >> for x0=x if x0>0

y=[y,x0.^2+(1+x0).^(1/4)+5]; elseif x0==0 y=[y,0]; elseif x0<0

y=[y,x0.^3+sqrt(1-x0)-5]; end end

>> plot(x,y)

fplot函数:

fplot('(x<0).*(x.^3+sqrt(1-x)-5)+(x==0).*0+(x>0).*(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 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.3x2—177.9x+8433 4.

(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)=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 94.1408 184.9056 332.5264 560.0000 5. (1)

建立函数文件: function f=fxy(u) x=u(1);y=u(2);

f=3.*x.^2+2*x.*y+y.^2

在命令窗口中输入以下命令: [U,fmin]=fminsearch('fxy',[1,1]) 结果: U =

1.0e-004 *

-0.0675 0.1715 fmin =

1.9920e-010 (2)

f=inline('-sin(x)-cos(x.^2)'); fmax=fminbnd(f,0,pi) fmax =

0.7310 6.

(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.19 dx =

0 0 0

42.1120

x=pi/2时单独计算: x=pi/2;

f=inline('sin(x).^2+cos(x).^2'); diff(f([x,pi]))/(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.9608 7.(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 =

6.2832 8.

N=64; %采样点数

T=5; %采样时间终点

t=linspace(0,T,N); %给出N个采样时间ti(i=1:N) y=exp(-t); %求各采样点样本值y dt=t(2)-t(1); %采样周期 f=1/dt; % 采样频率

Y=fft(y); %计算y的快速傅里叶变换Y F=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)*b x =

-1.8060 -0.5373

3.0448 矩阵除法法:

A=[2 3 5;3 7 4;1 -7 1]; b=[10;3;5]; x=A\b x =

-1.8060 -0.5373

3.0448 矩阵分解法:

A=[2 3 5;3 7 4;1 -7 1]; b=[10;3;5]; [L,U]=lu(A); x=U\(L\b) x =

-1.8060 -0.5373

3.0448

(2)方法同(1) 10.

本文来源:https://www.bwwdw.com/article/weji.html

Top