《MATLAB 程序设计与应用》刘卫国高等教育出版社前六个实验答案.docx
《《MATLAB 程序设计与应用》刘卫国高等教育出版社前六个实验答案.docx》由会员分享,可在线阅读,更多相关《《MATLAB 程序设计与应用》刘卫国高等教育出版社前六个实验答案.docx(26页珍藏版)》请在冰豆网上搜索。
《MATLAB程序设计与应用》刘卫国高等教育出版社前六个实验答案
实验一MATLAB运算基础
1.
(1)
z1=2*sin(85*pi/180)/(1+exp
(2))
z1=
0.2375
(2)
x=[21+2i;-0.455];
z2=log(x+sqrt(1+x.^2))/2
z2=
0.72180.7347+0.5317i
-0.21801.1562
(3)
a=-3:
0.1:
3
z3=(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=(0<=t&t<1).*t.*t+(1<=t&t<2).*(t.^2-1)+(2<=t&t<3).*(t.^2-2*t+1)
显示工作空间的变量和变量值
a1x61488doublearray
t1x648doublearray
x2x264doublearray(complex)
z11x18doublearray
z22x264doublearray(complex)
z31x61976doublearray(complex)
z41x648doublearray
保存全部变量
savemydata%将工作空间全部变量保存到mydata.mat文件中
2.
A=[1234-4;34787;3657]
B=[13-1;203;3-27]
(1)
A+6*B
ans=
1852-10
467105
215349
A-B+eye(size(A))
ans=
1231-3
32884
0671
A*B
ans=
684462
309-72596
154-5241
A.*B
ans=
121024
680261
9-13049
A^3
ans=
3722623382448604
247370149188600766
78688454142118820
A.^3
ans=
172839304-64
39304343658503
27274625343
A/B
ans=
16.4000-13.60007.6000
35.8000-76.200050.2000
67.0000-134.000068.0000
B\A
ans=
109.4000-131.2000322.8000
-53.000085.0000-171.0000
-61.600089.8000-186.2000
[A,B]
ans=
1234-413-1
34787203
36573-27
[A([1,3],:
);B^2]
ans=
1234-4
3657
451
11019
20-540
3.
A=[12345;678910;1112131415;1617181920;2122232425]
B=[3016;17-69;023-4;970;41311]
(1)C=A*B
C=
9315077
258335237
423520397
588705557
753890717
(2)
D=C(3:
5,2:
3)
D=
520397
705557
890717
4.
(1)
ind=find(rem(x,21)==0)
length(ind)
ans=
43
或者用一下程序
x=x(find(rem(x,21)==0))
x=
Columns1through21
105126147168189210231252273294315336357378399420441462483504525
Columns22through42
546567588609630651672693714735756777798819840861882903924945966
Column43
987
length(x)
ans=
43
(2)
str='adodfjoAWEREOTPET'
k=find(str>='A'&str<='Z')
str(k)=[]
实验二MATLAB矩阵分析与处理
1、
E=eye(3,3)
R=rand(3,2)
O=zeros(2,3)
S=diag([1,1])或者S=diag(1:
2)
A=[ER;OS]
A=[ER;OS]
A=
1.0000000.41030.3529
01.000000.89360.8132
001.00000.05790.0099
0001.00000
00001.0000
>>A^2
ans=
1.0000000.82050.7057
01.000001.78731.6263
001.00000.11580.0197
0001.00000
00001.0000
[ER+R*S;OS*S]
ans=
1.0000000.82050.7057
01.000001.78731.6263
001.00000.11580.0197
0001.00000
00001.0000
3、A=rand(5,5)
A=
0.13890.19880.46600.67210.8318
0.20280.01530.41860.83810.5028
0.19870.74680.84620.01960.7095
0.60380.44510.52520.68130.4289
0.27220.93180.20260.37950.3046
>>det(A)
ans=
-0.0627
>>trace(A)
ans=
1.9863
rank(A)
ans=
5
>>a1=norm(A,1)
a1=
2.7776
a2=norm(A,2)
a2=
2.3766
>>ainf=norm(A,inf)
ainf=
2.6842
4、
A=[-29618;20512;-885]
A=
-29618
20512
-885
>>[V,D]=eig(A)
V=
0.71300.28030.2733
-0.6084-0.78670.8725
0.34870.55010.4050
D=
-25.316900
0-10.51820
0016.8351
5.
A=[1/21/31/4;1/31/41/5;1/41/51/6]
b=[0.950.670.52]'
A=
0.50000.33330.2500
0.33330.25000.2000
0.25000.20000.1667
b=
0.9500
0.6700
0.5200
x=A\b
x=
1.2000
0.6000
0.6000
(2)
b(3)=0.53
b=
0.9500
0.6700
0.5300
>>x=inv(A)*b
x=
3.0000
-6.6000
6.6000
(3)
cond(A,1)
ans=
2.0150e+003
cond(A,2)
ans=
1.3533e+003
>>cond(A,inf)
ans=
2.0150e+003
分析:
矩阵A的条件数远远大于1,说明矩阵A是个病态矩阵,矩阵元素的轻微变化会引起解的很大变化
实验三
1.
forx=[-5,-3,1,2,2.5,3,5]
ifx<0&x~=-3
y=x.*x+x-6;
elseifx>=0&x<5&x~=2&x~=3
y=x.*x-5*x+6;
else
y=x.^2-x-1;
end
y
end
y=
14
y=
11
y=
2
y=
1
y=
-0.2500
y=
5
y=
19
2.
用if语句实现:
x=input('请输入一个成绩');
ifx<=100&x>=90
disp('成绩等级为:
A');
elseifx<90&x>=80
disp('成绩等级为:
B');
elseifx<80&x>=70
disp('成绩等级为:
C');
elseifx>=60&x<70
disp('成绩等级为:
D');
elseifx>=0&x<60
disp('不及格');
else
disp('输入的成绩不合理')
end
用switch语句实现:
x=input('请输入一个成绩');
switchfloor(x/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('不及格');
otherwise
disp('输入的成绩不合理')
end
3.
x=input('请输入工号');
y=input('请输入该员工工时数');
ify>120
gongzi=120*84+(y-120)*84*(1+0.15)
elseify<60
gongzi=y*84-700;
else
gongzi=y*84;
end
ifgongzi<0
gongzi=0;
end
disp('应发的工资是')
gongzhi
4.
x=floor(rand(2,1)*89)+10;
sign=input('请输入标点符号:
');
x=int2str(x);
expression=strcat(x
(1),sign,x
(2))
eval(expression)
5.
A=rand(5,6)
n=input('pleaseenternumbern')
if(n>size(A,1))
n=size(A,1)
disp('nlargerthantherowofmatrixA')
end
B=A(n,:
)
实验四:
循环结构程序设计
1.
(1)
forn=[100,1000,10000]
sum=0;
forx=1:
n
sum=sum+1/(x.^2);
end
value=sqrt(6*sum)
end
value=
3.1321
value=
3.1406
value=
3.1415
(2)
forn=[100,1000,10000]
x=1:
n;
value=sqrt(6*sum(1./x./x))
end
2.
y=0;
n=1;
whiley<3
y=y+1/(2*n-1);
n=n+1;
end
n=n-1;
y=y-1/(2*n-1)
n=n-1
y=
2.9944
n=
56
3.
(1)
a=2;
b=3;
x0=1;
x1=0;
n=1;
y=abs(x1-x0);
whilen<=500&y>10^(-5)
x1=a/(b+x0);
y=abs(x1-x0);
x0=x1;
n=n+1;
end
x1
x1=
0.5616
(2)
a=1;
b=1;
x0=1;
x1=0;
n=1;
y=abs(x1-x0);
whilen<=500&y>10^(-5)
x1=a/(b+x0);
y=abs(x1-x0);
x0=x1;
n=n+1;
end
x1
结果为:
x1=
0.6180
准确值为:
(-b+sqrt(b*b+4*a))/2
ans=
0.6180
当(a,b)的取值为(8,3)时
a=8;
b=3;
x0=1;
x1=0;
n=1;
y=abs(x1-x0);
whilen<=500&y>10^(-5)
x1=a/(b+x0);
y=abs(x1-x0);
x0=x1;
n=n+1;
end
x1
x1=
1.7016
准确值为;
(-b+sqrt(b*b+4*a))/2
ans=
1.7016
当(a,b)的取值为(10,0.1)时
a=10;
b=0.1;
x0=1;
x1=0;
n=1;
y=abs(x1-x0);
whilen<=500&y>10^(-5)
x1=a/(b+x0);
y=abs(x1-x0);
x0=x1;
n=n+1;
end
x1
x1=
3.1127
准确值为:
(-b+sqrt(b*b+4*a))/2
ans=
3.1127
4.
f1=1;
f2=0;
f3=1;
f=[f1,f2,f3];
forn=4:
100
f4=f3-2*f2+f1;
f=[ff4];
f1=f2;
f2=f3;
f3=f4;
end
f
maxvalue=max(f);
minvalue=min(f);
sumvalue=sum(f);
disp(['最大值是:
',num2str(maxvalue)])
disp(['最小值是:
',num2str(minvalue)])
disp(['各数和是:
',num2str(sumvalue)])
zhengshu=length(find(f>0))
fushu=length(find(f<0))
zeroshu=length(find(f==0))
5.
sushu=[];
forn=2:
49
x=n*(n+1)-1;
signx=0;
fork=2:
sqrt(x)
if(mod(x,k)==0)
signx=1;
end
end
ifsignx==0
sushu=[sushu;nn+1];
end
end
sushu
sushu=
23
34
45
56
67
89
910
1011
1112
1314
1516
1617
1920
2021
2122
2425
2627
2829
3031
3132
3536
3839
3940
4142
4445
4546
4647
4849
实验五:
1.函数文件如下:
function[expval,logval,sinval,cosval]=fushu(x)
%FUSHUcomputefushuvalue
expval=exp(x);
logval=log(x);
sinval=sin(x);
cosval=cos(x);
disp(['复数e的指数是:
',num2str(expval)])
disp(['复数e的对数是:
',num2str(logval)])
disp(['复数e的正弦是:
',num2str(sinval)])
disp(['复数e的余弦是:
',num2str(cosval)])
命令文件diaoyongfushu.m
x=1+2i
[expval,logval,sinval,cosval]=fushu(x)
2.
functionJ=jiefangcheng(m1,m2,m3)
H=[m1*cos(m3*pi/180)-m1-sin(m3*pi/180)0
m1*sin(m3*pi/180)0cos(m3*pi/180)0
0m2-sin(m3*pi/180)0
00-cos(m3*pi/180)1];
K=[0;m1*9.8;0;m2*9.8];
J=inv(H)*K;
disp(['方程组的解是:
',num2str(J')])
主程序:
m1=input('m1=');
m2=input('m2=');
m3=input('m3=');
J=jiefangcheng(m1,m2,m3);
a1=J
(1)
a2=J
(2)
N1=J(3)
N2=J(4)
3.
functiona=sushu(b)
%jueduisushu
x=fix(b/10);
y=rem(b,10);
a=10*y+x;
signab=0;
form=2:
sqrt(b)
ifrem(b,m)==0
signab=signab+1;
end
end
forn=2:
sqrt(a)
ifrem(a,n)==0
signab=signab+1;
end
end
ifsignab==0
a=b;
else
a=0;%这里赋值0,表示不是素数
end
命令文件
liangweisushu=[];
forn=10:
99
[a]=sushu(n);
ifa>0
liangweisushu=[liangweisushu,a];
end
end
disp(['所有的两位绝对素数是:
',num2str(liangweisushu)])
4.
functionf=fx(x)
%
f=1./((x-2).^2+0.1)+1./((x-3).^4+0.01);
命令文件:
x=[12;3+4i5]
f=fx(x)
5.
(1)
functionfn=f(n)
fn=n+10*log(n*n+5);
命令文件:
y=f(40)/(f(30)+f(20))
(2)
functionfn=f1(n)
fn=0;
fork=1:
n
fn=fn+k*(k+1);
end
命令文件:
y=f1(40)/(f1(30)+f1(20))
实验六
1.
x=linspace(0,2*pi,101)
y=(0.5+3*sin(x)./(1+x.*x)).*cos(x)
plot(x,y)
2.
(1)
clearall
x=linspace(-2*pi,2*pi,500);
y1=x.^2;
y2=cos(2*x);
y3=y1.*y2;
plot(x,y1,'r:
',x,y2,'b-',x,y3,'k.-')
(2)
clearall
x=linspace(-2*pi,2*pi,500);
y1=x.^2;
y2=cos(2*x);
y3=y1.*y2;
subplot(3,1,1);
plot(x,y1);
subplot(3,1,2);
plot(x,y2);
subplot(3,1,3);
plot(x,y3);
(3)
clearall
x=linspace(-2*pi,2*pi,500);
y1=x.^2;
y2=cos(2*x);
y3=y1.*y2;
subplot(3,3,1);
bar(x,y1);%条形图
subplot(3,3,2);
stairs(x,y1);%阶梯图
subplot(3,3,3);
stem(x,y1);%杆图
subplot(3,3,4);
bar(x,y2);
subplot(3,3,5);
stairs(x,y2);
subplot(3,3,6);
stem(x,y2);
subplot(3,3,7);
bar(x,y3);
subplot(3,3,8);
stairs(x,y3);
subplot(3,3,9);
stem(x,y3);
3.
x=-5:
0.01:
5
y=(x+sqrt(pi))/exp
(2).*(x<=0)+1/2*log(x+sqrt(1+x.^2)).*(x>0)
plot(x,y)
4.
clearall
a=input('请输入一个数a:
');
b=input('请输入一个数b:
');
n=input('请输入一个数n:
');
theta=linspace(0,2*pi,500);
rho=a*sin(b+n.*theta);
polar(theta,rho);
5.
clearall
x1=linspace(-5,5,21);
y1=linspace(0,10,31);
[x,y]=meshgrid(x1,y1);
z=cos(x).*cos(y).*exp(-0.25.*sqrt(x.^2+y.^2))
subplot(2,1,1);
surf(x,y,z);
subplot(2,1,2);
contour3(x,y,z,30);
6.
clearall
s=linspace(0,0.5*pi,10);
t=linspace(0,1.5*pi,30);
[S,T]=meshgrid(s,t);
x=cos(S).*cos(T);
y=cos(S).*sin(T);
z=sin(S);
colormap(prism);
figure
(1)
surf(x,y,z);
figure
(2)
surf(x,y,z);shadingflat;
figure(3)
surf(x,y,z);shadinginterp;