MATLAB习题及答案.docx
《MATLAB习题及答案.docx》由会员分享,可在线阅读,更多相关《MATLAB习题及答案.docx(14页珍藏版)》请在冰豆网上搜索。
MATLAB习题及答案
1.令
,
,
(1)求
和
的点积
(2)求
和
的叉乘积
(3)求
、
、
的混和积
参考答案:
>>A=[1,5,3];
>>B=[2,1,4];
>>C=[9,-1,5];
(1)
>>dot(A,B)
ans=
19
(2)
>>cross(B,C)
ans=
926-11
(3)
>>dot(A,cross(B,C))
ans=
106
2.令
,
,
,
。
求解下列问题:
(1)
(2)
(3)
(4)
(5)
(6)
(7)
参考答案:
>>A=[2,-1;-2,-2];
>>B=[2,-3;0,-4];
>>C=[1;2];
>>D=[1,0;0,1];
(1)
>>2*A
ans=
4-2
-4-4
(2)
>>A+B
ans=
4-4
-2-6
(3)
>>A*B
ans=
4-2
-414
(4)
>>A.*B
ans=
43
08
(5)
>>B*C
ans=
-4
-8
(6)
>>A/B
ans=
1.0000-0.5000
-1.00001.2500
(7)
>>A\B
ans=
0.6667-0.3333
-0.66672.3333
3.
求该矩阵的特征值和特征向量
参考答案:
>>X=[9,8,8,9;2,7,4,7;6,4,6,1;4,0,7,4]
X=
9889
2747
6461
4074
>>[V,D]=eig(X)
V=
-0.75430.2437+0.1991i0.2437-0.1991i-0.5547
-0.38210.2241+0.4505i0.2241-0.4505i0.5493
-0.41320.1132-0.5091i0.1132+0.5091i0.4280
-0.3382-0.6131-0.6131-0.4554
D=
21.4707000
01.1178+4.5139i00
001.1178-4.5139i0
0002.2938
4.生成5阶魔术矩阵,记为
,对其进行如下操作
(1)求
的逆
(2)计算
的行列式
(3)求
的条件数
(4)求矩阵
的秩
(5)求矩阵
的迹
参考答案:
>>A=magic(5)
A=
17241815
23571416
46132022
101219213
11182529
(1)
>>inv(A)
ans=
-0.00490.0512-0.03540.00120.0034
0.0431-0.0373-0.00460.01270.0015
-0.03030.00310.00310.00310.0364
0.0047-0.00650.01080.0435-0.0370
0.00280.00500.0415-0.04500.0111
(2)
>>det(A)
ans=
5070000
(3)
>>cond(A)
ans=
5.4618
(4)
>>rank(A)
ans=
5
(5)
>>trace(A)
ans=
65
5.
,对
进行如下操作:
(1)求
的LU分解
(2)求
的正交分解
(3)求
的特征值分解
(4)求
的奇异值分解
>>X=[9,0,1,2;9,3,2,1;4,8,1,0;8,0,6,7]
X=
9012
9321
4810
8067
(1)
>>[L,U]=lu(X)
L=
1.0000000
1.00000.37500.15491.0000
0.44441.000000
0.888901.00000
U=
9.000001.00002.0000
08.00000.5556-0.8889
005.11115.2222
000-1.4755
(2)
>>[Q,R]=qr(X)
Q=
-0.57850.28660.5381-0.5418
-0.5785-0.10520.21820.7788
-0.2571-0.9175-0.0818-0.2921
-0.51430.2548-0.8100-0.1206
R=
-15.5563-3.7927-5.0783-5.3354
0-7.65610.68712.2512
00-3.9672-4.3754
000-1.1492
(3)
>>[V,D]=eig(X)
V=
0.3650-0.1238-0.1819i-0.1238+0.1819i-0.0337
0.4317-0.2224+0.2205i-0.2224-0.2205i0.2755
0.3764-0.2020+0.5806i-0.2020-0.5806i-0.7751
0.73400.68970.68970.5676
D=
14.0539000
03.8077+2.9407i00
003.8077-2.9407i0
000-1.6693
(4)
>>[U,S,V]=svd(X)
U=
-0.4891-0.1120-0.6156-0.6077
-0.52680.2260-0.35270.7396
-0.31360.81790.3860-0.2893
-0.6204-0.51710.5896-0.0027
S=
17.8325000
08.309200
004.83570
0000.7578
V=
-0.86140.0194-0.50750.0109
-0.22930.86910.4198-0.1262
-0.3128-0.23410.53820.7468
-0.3279-0.43540.5259-0.6529
6.比较稀疏矩阵与满矩阵的异同之处,如eye(10)与speye(10)生成矩阵的异同之处
略。
7.将10阶随机矩阵转换为稀疏矩阵
参考答案:
>>A=rand(10)
>>sparse(A)
8.将10阶稀疏正态随机矩阵转换为满矩阵
参考答案:
>>A=sprand(10,10,0.2)
>>B=full(A)
9.计算下列积分
(1)
(2)
(3)
(4)
(5)
参考答案:
(1)
>>f=@(x)x+x.^3+x.^5;
>>q=quad(f,-1,1)
q=
2.2204e-016
(2)
>>f2=@(x)sin(x)+cos(x);
>>q=quad(f2,-1,10)
q=
1.6768
(3)
>>f3=@(x)exp(x/2);
>>q=quad(f3,2,6)
q=
34.7345
(4)
>>f4=@(x)x./(x.^4+4);
>>q=quad(f4,1,10)
q=
0.2718
(5)
>>f5=@(x,y)sin(y).*(x+y)./(x.^2+4);
>>q=dblquad(f5,1,10,1,10)
q=
5.5254
10.求下列函数的极值
(1)
(2)
参考答案:
(1)
>>z=@(x)x
(1)^2-(x
(2)-1)^2;
>>[x,fvalue,flag,output]=fminsearch(z,[0,0])
Exiting:
Maximumnumberoffunctionevaluationshasbeenexceeded
-increaseMaxFunEvalsoption.
Currentfunctionvalue:
-359212266339440800000000000000000000000000000000000000000000000000000000000000000000.000000
x=
1.0e+041*
0.9523-6.0686
fvalue=
-3.5921e+083
flag=
0
output=
iterations:
200
funcCount:
401
algorithm:
'Nelder-Meadsimplexdirectsearch'
message:
[1x233char]
(2)
>>z=@(x)(x
(1)-x
(2)+1)^2;
>>[x,fvalue,flag,output]=fminsearch(z,[0,0])
x=
-0.54250.4575
fvalue=
2.4109e-011
flag=
1
output=
iterations:
40
funcCount:
74
algorithm:
'Nelder-Meadsimplexdirectsearch'
message:
[1x196char]
11.求下列函数的解,并绘制图形
(1)
,初始点为
(2)
参考答案:
(1)
>>y=@(x)exp(x)-x^5;
>>x=fzero(y,8)
x=
12.7132
>>fplot(y,[x-1,x+1]);
>>holdon
>>plot(x,y(x),'r*');
(2)
>>y=@(x)x*sin(x);
x=
0
12.有如下数据:
x
1
1.1
1.2
1.3
1.4
y
1.00000
1.23368
1.55271
1.99372
2.61170
利用本章介绍的几种插值方法对其进行插值,得到每隔0.05的结果
参考答案:
编写脚本文件,文件内容为:
%Interpolationusingthefourmethods
x=[11.11.21.31.4];
y=[1.000001.233681.552711.993722.61170];
length_of_x=length(x);
scalar_x=x
(1):
0.05:
x(length_of_x);
length_of_sx=length(scalar_x);
y_nearest=zeros(length(scalar_x),1);
y_linear=zeros(length(scalar_x),1);
y_spline=zeros(length(scalar_x),1);
y_cubic=zeros(length(scalar_x),1);
fori=1:
length_of_sx
y_nearest(i)=interp1(x,y,scalar_x(i),'nearest');
y_linear(i)=interp1(x,y,scalar_x(i),'linear');
y_spline(i)=interp1(x,y,scalar_x(i),'spline');
y_cubic(i)=interp1(x,y,scalar_x(i),'cubic');
end
subplot(2,2,1),plot(x,y,'*'),holdon,plot(scalar_x,y_nearest),title('method=nearest');
subplot(2,2,2),plot(x,y,'*'),holdon,plot(scalar_x,y_linear),title('method=linear');
subplot(2,2,3),plot(x,y,'*'),holdon,plot(scalar_x,y_spline),title('method=spline');
subplot(2,2,4),plot(x,y,'*'),holdon,plot(scalar_x,y_cubic),title('method=cubic');
得到结果为: