MATLAB编程练习含答案很好的.docx
《MATLAB编程练习含答案很好的.docx》由会员分享,可在线阅读,更多相关《MATLAB编程练习含答案很好的.docx(45页珍藏版)》请在冰豆网上搜索。
MATLAB编程练习含答案很好的
001双峰曲线图:
z=peaks(40);
mesh(z);
surf(z)
002解方程:
A=[3,4,-2;6,2,-3;45,5,4];
>>B=[14;4;23];
>>root=inv(A)*B
003傅里叶变换
loadmtlb;
subplot(2,1,1);
plot(mtlb);
>>title('原始语音信息');
>>y=fft(mtlb);
>>subplot(2,1,2);
>>yy=abs(y);
>>plot(yy);
>>title('傅里叶变换')
004输入函数:
a=input('Howmanyapples\n','s')
005输出函数
a=[1234;5678;12233445;344352334]
a=
1234
5678
12233445
344352334
disp(a)
a=
1234
5678
12233445
344352334
b=input('howmanypeople\n','s')
howmanypeople
twopeople
b=
twopeople
>>disp(b)
twopeople
>>
006求一元二次方程的根
a=1;b=2;c=3;
d=sqrt(b^2-4*a*c);
x1=(-b+d)/(2*a)
x1=
-1.0000+1.4142i
>>x2=(-b-d)/(2*a)
x2=
-1.0000-1.4142i
007求矩阵的相乘、转置、存盘、读入数据
A=[135;246;-10-2;-300];
>>B=[-13;-22;21];
>>C=A*B
C=
314
220
-3-5
3-9
>>C=C'
C=
32-33
1420-5-9
>>savemydatC
>>clear
>>loadmydatC
008编写数学计算公式:
A=2.1;B=-4.5;C=6;D=3.5;E=-5;
K=atan(((2*pi*A)+E/(2*pi*B*C))/D)
K=
1.3121
009
A=[10-1;241;-205];
>>B=[0-10;213;112];
>>H=2*A+B
H=
2-1-2
695
-3112
>>M=A^2-3*B
M=
33-6
213-2
-15-321
>>Y=A*B
Y=
-1-2-2
9314
5710
>>R=B*A
R=
-2-4-1
-2414
-1410
>>E=A.*B
E=
000
443
-2010
>>W=A\B
W=
0.3333-1.33330.6667
0.25001.00000.2500
0.3333-0.33330.6667
>>P=A/B
P=
-2.00003.0000-5.0000
-5.00003.0000-4.0000
7.0000-9.000016.0000
>>Z=A.\B
Warning:
Dividebyzero.
Z=
0-Inf0
1.00000.25003.0000
-0.5000Inf0.4000
>>D=A./B
Warning:
Dividebyzero.
D=
Inf0-Inf
1.00004.00000.3333
-2.000002.5000
010
a=4.96;b=8.11;
>>M=exp(a+b)/log10(a+b)
M=
4.2507e+005
011求三角形面积:
a=9.6;b=13.7;c=19.4;
>>s=(a+b+c)/2;
>>area=sqrt(s*(s-a)*(s-b)*(s-c))
area=
61.1739
012逻辑运算
A=[-10-68;-94012.3;00-5.1-2;0-230-7];
>>B=A(:
1:
2)
B=
-10
-94
00
0-23
>>C=A(1:
2,:
)
C=
-1.00000-6.00008.0000
-9.00004.0000012.3000
>>D=B'
D=
-1-900
040-23
>>A*B
ans=
1.0000-184.0000
-27.0000-266.9000
046.0000
207.000069.0000
>>Cans=
0010
1000
>>C&D
ans=
1000
0101
>>C|D
ans=
1111
1101
>>~C|~D
ans=
0111
1010
013矩阵运算练习:
A=[895;36-711;21-85]
A=
895
36-711
21-85
>>B
B=
-13-2
203
-319
>>RT=A*B
RT=
-52956
-831196
-5268-21
>>QW=A.*B
QW=
-827-10
72033
-63-845
>>ER=A^3
ER=
627233422944
15714-8565260
8142-19062390
>>BF=A.^3
BF=
512729125
46656-3431331
9261-512125
>>A/B
ans=
3.13414.9634-0.4024
-1.256112.5244-3.2317
-1.98786.4512-2.0366
>>EKV=B\A
EKV=
10.7195-1.26833.5244
9.47561.58543.7195
4.8537-1.48781.3171
>>KDK=[A,B]
KDK=
895-13-2
36-711203
21-85-319
>>ERI=[A;B]
ERI=
895
36-711
21-85
-13-2
203
-319
014一般函数的调用:
A=[23488390848939];
>>S=sum(A)
S=
2301
>>min(A)
ans=
2
>>EE=mean(A)
EE=
383.5000
>>QQ=std(A)
QQ=
419.3794
>>AO=sort(A)
AO=
23488390848939
>>yr=norm(A)
yr=
1.3273e+003
>>RT=prod(A)
RT=
1.8583e+012
>>gradient(A)
ans=
32.000043.0000178.0000380.0000274.500091.0000
>>max(A)
ans=
939
>>median(A)
ans=
239
>>diff(A)
ans=
325430245891
>>length(A)
ans=
6
>>sum(A)
ans=
2301
>>cov(A)
ans=
1.7588e+005
>>
015矩阵变换:
A=[344423;83423;34552]
A=
344423
83423
34552
>>tril(A)
ans=
3400
8340
34552
>>triu(A)
ans=
344423
03423
002
>>diag(A)
ans=
34
34
2
norm(A)
ans=
94.5106
>>rank(A)
ans=
3
>>det(A)
ans=
-23462
>>trace(A)
ans=
70
>>null(A)
ans=
Emptymatrix:
3-by-0
>>eig(A)
ans=
80.1587
12.7671
-22.9257
>>poly(A)
ans=
1.0e+004*
0.0001-0.0070-0.11072.3462
>>logm(A)
Warning:
PrincipalmatrixlogarithmisnotdefinedforAwith
nonpositiverealeigenvalues.Anon-principalmatrix
logarithmisreturned.
>Infunmat153
Inlogmat27
ans=
3.1909+0.1314i1.2707+0.1437i0.5011-0.2538i
0.4648+0.4974i3.3955+0.5438i0.1504-0.9608i
0.2935-1.2769i0.8069-1.3960i3.4768+2.4663i
>>fumn(A)
Undefinedcommand/function'fumn'.
>>inv(A)
ans=
0.0510-0.0502-0.0098
-0.03260.03040.0255
0.03050.0159-0.0343
>>cond(A)
ans=
8.5072
>>chol(A)
Errorusing==>chol
Matrixmustbepositivedefinite.
>>lu(A)
ans=
34.000044.000023.0000
0.235323.647117.5882
1.00000.4652-29.1816
>>pinv(A)
ans=
0.0510-0.0502-0.0098
-0.03260.03040.0255
0.03050.0159-0.0343
>>svd(A)
ans=
94.5106
22.3456
11.1095
>>expm(A)
ans=
1.0e+034*
2.18974.39681.9382
1.31542.64121.1643
1.87823.77121.6625
>>sqrtm(A)
ans=
5.2379+0.2003i3.4795+0.2190i1.8946-0.3869i
0.5241+0.7581i5.1429+0.8288i2.0575-1.4644i
3.0084-1.9461i4.7123-2.1276i2.1454+3.7589i
>>
016多项式的计算:
A=[344423;83423;34552]
A=
344423
83423
34552
>>P=poly(A)
P=
1.0e+004*
0.0001-0.0070-0.11072.3462
>>PPA=poly2str(P,'X')
PPA=
X^3-70X^2-1107X+23462
017多项式的运算:
p=[2683];w=[325602];
>>m=conv(p,w)
m=
64304592548180166
>>[q,r]=deconv(w,p)
q=
16
r=
0-40-128-46
>>dp=polyder(w)
dp=
961120
>>[num,den]=polyder(w,p)
num=
80512724312-16
den=
42468108100489
>>b=polyfit(p,w,4)
Warning:
Polynomialisnotunique;degree>=numberofdatapoints.
>Inpolyfitat74
b=
-0.67049.2037-32.2593098.1333
>>r=roots(p)
r=
-1.2119+1.0652i
-1.2119-1.0652i
-0.5761
018求多项式的商和余
p=conv([102],conv([14],[11]))
p=
156108
>>q=[1011]
q=
1011
>>[w,m]=deconv(p,q)
w=
15
m=
00543
>>cq=w;cr=m;
>>disp([cr,poly2str(m,'x')])
5x^2+4x+3
>>disp([cq,poly2str(w,'x')])
x+5
019将分式分解
a=[156];b=[1];
>>[r,p,k]=residue(b,a)
r=
-1.0000
1.0000
p=
-3.0000
-2.0000
k=
[]
020计算多项式:
a=[123;456;789];
>>p=[3023];
>>q=[23];
>>x=2;
>>r=roots(p)
r=
0.3911+1.0609i
0.3911-1.0609i
-0.7822
>>p1=conv(p,q)
p1=
694129
>>p2=poly(a)
p2=
1.0000-15.0000-18.0000-0.0000
>>p3=polyder(p)
p3=
902
>>p4=polyval(p,x)
p4=
31
021求除式和余项:
[q,r]=deconv(conv([102],[14]),[111])
022字符串的书写格式:
s='student'
s=
student
>>name='mary';
>>s1=[names]
s1=
marystudent
>>s3=[nameblanks(3);s]
s3=
mary
student
>>
023交换两个数:
clear
clc
a=[12345];
b=[678910];
c=a;
a=b;
b=c;
a
b
24If语句
n=input('enteranumber,n=');
ifn<10
n
end
025if双分支结构
a=input('enteranumber,a=');
b=input('enteranumber,b=');
ifa>b
max=a;
else
max=b;
end
max
026三个数按照由大到小的顺序排列:
A=15;B=24;C=45;
ifA
T=A;A=B;B=T;
elseifAT=A;A=C;C=T;
elseifBT=B;B=C;C=T;
end
A
B
C
027建立一个收费优惠系统:
price=input('pleasejinputtheprice:
price=')
switchfix(price/100)
case[0,1]
rate=0;
case[2,3,4]
rate=3/100;
casenum2cell(5:
9)
rate=5/100;
casenum2cell(10:
24)
rate=8/100;
casenum2cell(25:
49)
rate=10/100;
otherwise
rate=14/100;
end
price=price*(1-rate)
028:
while循环语句
i=0;s=0;
whilei<=1212
s=s+i;
i=i+1;
end
s
029,用for循环体语句:
sum=0;
fori=1:
1.5:
100;
sum=sum+i;
end
sum
030循环的嵌套
s=0;
fori=1:
1:
6;
forj=1:
1:
8;
s=s+i^j;
end;
end;
s
031continue语句的使用:
fori=100:
120;
ifrem(i,7)~=0;
continue;
end;
i
end
032
x=input('输入X的值x=')
ifx<1
y=x^2;
elseifx>1&x<2
y=x^2-1;
else
y=x^2-2*x+1;
end
y
033求阶乘的累加和
sum=0;
temp=1;
forn=1:
10;
temp=temp*n;
sum=sum+temp;
end
sum
034对角线元素之和
sum=0;
a=[1234;5678;9101112;13141516];
fori=1:
4;
sum=sum+a(i,i);
end
sum
035用拟合点绘图
A=[1215.3161825];
B=[5080118125150.8];
plot(A,B)
036绘制正玄曲线:
x=0:
0.05:
4*pi;
y=sin(x);
plot(x,y)
037绘制向量
x=[123456;789101112;131415161718]
plot(x)
x=[00.20.50.70.60.71.21.51.61.92.3]
plot(x)
x=0:
0.2:
2*pi
y=sin(x)
plot(x,y,'m:
p')
038在正弦函数上加标注:
t=0:
0.05:
2*pi;
plot(t,sin(t))
set(gca,'xtick',[01.43.1456.28])
xlabel('t(deg)')
ylabel('magnitude(v)')
title('thisisaexample()\rightarrow2\pi')
text(3.14,sin(3.14),'\leftarrowthiszerofor\pi')
039添加线条标注
x=0:
0.2:
12;
plot(x,sin(x),'-',x,1.5*cos(x),':
');
legend('First','Second',1)
040使用holdon函数
x=0:
0.2:
12;
plot(x,sin(x),'-');
holdon
plot(x,1.5*cos(x),':
');
041一界面多幅图
x=0:
0.05:
7;
y1=sin(x);
y2=1.5*cos(x);
y3=sin(2*x);
y4=5*cos(2*x);
subplot(221);plot(x,y1);title('sin(x)')
subplot(222);plot(x,y2);title('cos(x)')
subplot(223);plot(x,y3);title('sin(2x)')
subplot(224);plot(x,y4);title('cos(2x)')
042染色效果图
x=0:
0.05:
7;
y1=sin(x);
y2=1.5*cos(x);
y3=sin(2*x);
y4=5*cos(2*x);
subplot(221);plot(x,y1);title('sin(x)');fill(x,y1,'r')
subplot(222);plot(x,y2);title('cos(x)');fill(x,y2,'b')
subplot(223);plot(x,y3);title('sin(2x)');fill(x,y3,'k')
subplot(224);plot(x,y4);title('cos(2x)');fill(x,y4,'g')
043特殊坐标图
clc
y=[0,0.55,2.5,6.1,8.5,12.1,14.6,17,20,22,22.1]
subplot(221);plot(y);
title('线性坐标图');
subplot(222);semilogx(y);
title('x轴对数坐标图');
subplot(223);semilogx(y);
title('y轴对数坐标图');
subplot(224);loglog(y);
title('双对数坐标图')
t=0:
0.01:
2*pi;
r=2*cos(2*(t-pi/8));
polar(t,r)
044特殊函数绘图:
fplot('cos(tan(pi*x))',[-0.4,1.4])
fplot('sin(exp(pi*x))',[-0.4,1.4])
045饼形图与条形图:
x=[820362412];
subplot(221);pie(x,[10001]);
title('饼图');
subplot(222);bar(x,'group');
title('垂直条形图');
subplot(223);bar(x,'stack');
title('累加值为纵坐标的垂直条形图');
subplot(224);barh(x,'group');
title('水平条形图');
046梯形图与正弦函数
x=0:
0.1:
10;
y=sin(x);
subplot(121);stairs(x);
subplot(122);stairs(x,y);
047概率图
x=randn(1,1000);
y=-2:
0.1: