MATLAB最全答案Word文档下载推荐.docx
《MATLAB最全答案Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《MATLAB最全答案Word文档下载推荐.docx(69页珍藏版)》请在冰豆网上搜索。
%删去绝对值最小的点以求函数绝对值次小的点
[f2,x2_index]=min(abs(f))%求另一近似根--函数绝对值次小的点
f2=
0.0630
x2_index=
65
x(x2_index)
1.2500
%Page20,ex5
z=magic(10)
z=
929918156774515840
9880714167355576441
4818820225456637047
8587192136062697128
869325296168755234
17247683904249263365
2358289914830323966
7961395972931384572
10129496783537444653
111810077843643502759
sum(z)
505505505505505505505505505505
sum(diag(z))
505
z(:
2)/sqrt(3)
57.1577
46.1880
46.7654
50.2295
53.6936
13.8564
2.8868
3.4641
6.9282
10.3923
z(8,:
)=z(8,:
)+z(3,:
)
83871011151198387101115119
111810077843643502759
%Page40ex1
先在编辑器窗口写下列M函数,保存为eg2_1.m
function[xbar,s]=ex2_1(x)
n=length(x);
xbar=sum(x)/n;
s=sqrt((sum(x.^2)-n*xbar^2)/(n-1));
例如
x=[81706551766690876177];
[xbar,s]=ex2_1(x)
xbar=
72.4000
s=
12.1124
%Page40ex2
s=log
(1);
n=0;
whiles<
=100
n=n+1;
s=s+log(1+n);
end
m=n
计算结果m=37
%Page40ex3
clear;
F
(1)=1;
F
(2)=1;
k=2;
x=0;
e=1e-8;
a=(1+sqrt(5))/2;
whileabs(x-a)>
e
k=k+1;
F(k)=F(k-1)+F(k-2);
x=F(k)/F(k-1);
end
a,x,k
计算至k=21可满足精度
%Page40ex4
tic;
s=0;
fori=1:
1000000
s=s+sqrt(3)/2^i;
s,toc
i=1;
whilei<
=1000000
i=i+1;
i=1:
1000000;
s=sqrt(3)*sum(1./2.^i);
%Page40ex5
t=0:
24;
c=[15141414141516182022232528...
313231292725242220181716];
plot(t,c)
%Page40ex6
%
(1)
x=-2:
0.1:
y=x.^2.*sin(x.^2-x-2);
plot(x,y)
y=inline('
x^2*sin(x^2-x-2)'
);
fplot(y,[-22])
%
(2)参数方法
t=linspace(0,2*pi,100);
x=2*cos(t);
y=3*sin(t);
plot(x,y)
%(3)
x=-3:
3;
y=x;
[x,y]=meshgrid(x,y);
z=x.^2+y.^2;
surf(x,y,z)
%(4)
y=-3:
13;
z=x.^4+3*x.^2+y.^2-2*x-2*y-2*x.^2.*y+6;
%(5)
0.01:
2*pi;
x=sin(t);
y=cos(t);
z=cos(2*t);
plot3(x,y,z)
%(6)
theta=linspace(0,2*pi,50);
fai=linspace(0,pi/2,20);
[theta,fai]=meshgrid(theta,fai);
x=2*sin(fai).*cos(theta);
y=2*sin(fai).*sin(theta);
z=2*cos(fai);
%(7)
x=linspace(0,pi,100);
y1=sin(x);
y2=sin(x).*sin(10*x);
y3=-sin(x);
plot(x,y1,x,y2,x,y3)
%page41,ex7
x=-1.5:
1.5;
y=1.1*(x>
1.1)+x.*(x<
=1.1).*(x>
=-1.1)-1.1*(x<
-1.1);
%page41,ex8
分别使用whichtrapz,typetrapz,dirC:
\MATLAB7\toolbox\matlab\datafun\
%page41,ex9
close;
a=0.5457;
b=0.7575;
p=a*exp(-0.75*y.^2-3.75*x.^2-1.5*x).*(x+y>
1);
p=p+b*exp(-y.^2-6*x.^2).*(x+y>
-1).*(x+y<
=1);
p=p+a*exp(-0.75*y.^2-3.75*x.^2+1.5*x).*(x+y<
=-1);
mesh(x,y,p)
%page41,ex10
lookforlyapunov
helplyap
A=[123;
456;
780];
C=[2-5-22;
-5-24-56;
-22-56-16];
X=lyap(A,C)
X=
1.0000-1.0000-0.0000
-1.00002.00001.0000
-0.00001.00007.0000
%Chapter3
%Exercise1
a=[1,2,3];
b=[2,4,3];
a./b,a.\b,a/b,a\b
0.50000.50001.0000
221
0.6552%一元方程组x[2,4,3]=[1,2,3]的近似解
000
0.66671.33331.0000
%矩阵方程[1,2,3][x11,x12,x13;
x21,x22,x23;
x31,x32,x33]=[2,4,3]的特解
%Exercise2
(1)
A=[41-1;
32-6;
1-53];
b=[9;
-2;
1];
rank(A),rank([A,b])%[A,b]为增广矩阵
3
3%可见方程组唯一解
x=A\b
x=
2.3830
1.4894
2.0213
%Exercise2
(2)
A=[4-33;
b=[-1;
rank(A),rank([A,b])
-0.4706
-0.2941
0
%Exercise2(3)
A=[41;
32;
1-5];
b=[1;
1;
2
3%可见方程组无解
0.3311
-0.1219%最小二乘近似解
%Exercise2(4)
a=[2,1,-1,1;
1,2,1,-1;
1,1,2,1];
b=[123]'
;
%注意b的写法
rank(a),rank([a,b])
3%rank(a)==rank([a,b])<
4说明有无穷多解
a\b
1
0%一个特解
%Exercise3
b=[1,2,3]'
x=null(a),x0=a\b
-0.6255
0.6255
-0.2085
0.4170
x0=
%通解kx+x0
%Exercise4
x0=[0.20.8]'
a=[0.990.05;
0.010.95];
x1=a*x,x2=a^2*x,x10=a^10*x
x=x0;
1000,x=a*x;
end,x
0.8333
0.1667
x0=[0.80.2]'
[v,e]=eig(a)
v=
0.9806-0.7071
0.19610.7071
e=
1.00000
00.9400
v(:
1)./x
1.1767
1.1767%成比例,说明x是最大特征值对应的特征向量
%Exercise5
%用到公式(3.11)(3.12)
B=[6,2,1;
2.25,1,0.2;
3,0.2,1.8];
x=[25520]'
C=B/diag(x)
C=
0.24000.40000.0500
0.09000.20000.0100
0.12000.04000.0900
A=eye(3,3)-C
A=
0.7600-0.4000-0.0500
-0.09000.8000-0.0100
-0.1200-0.04000.9100
D=[171717]'
x=A\D
37.5696
25.7862
24.7690
%Exercise6
(1)
a=[41-1;
det(a),inv(a),[v,d]=eig(a)
-94
0.2553-0.02130.0426
0.1596-0.1383-0.2234
0.1809-0.2234-0.0532
0.0185-0.9009-0.3066
-0.7693-0.1240-0.7248
-0.6386-0.41580.6170
d=
-3.052700
03.67600
008.3766
%Exercise6
(2)
a=[11-1;
02-1;
-120];
2.0000-2.00001.0000
1.0000-1.00001.0000
2.0000-3.00002.0000
-0.57730.5774+0.0000i0.5774-0.0000i
-0.57730.57740.5774
-0.57740.5773-0.0000i0.5773+0.0000i
1.000000
01.0000+0.0000i0
001.0000-0.0000i
%Exercise6(3)
A=[5765;
71087;
68109;
57910]
5765
71087
68109
57910
det(A),inv(A),[v,d]=eig(A)
68.0000-41.0000-17.000010.0000
-41.000025.000010.0000-6.0000
-17.000010.00005.0000-3.0000
10.0000-6.0000-3.00002.0000
0.83040.09330.39630.3803
-0.5016-0.30170.61490.5286
-0.20860.7603-0.27160.5520
0.1237-0.5676-0.62540.5209
0.0102000
00.843100
003.85810
00030.2887
%Exercise6(4)(以n=5为例)
%关键是矩阵的定义
%方法一(三个for)
n=5;
n,a(i,i)=5;
(n-1),a(i,i+1)=6;
(n-1),a(i+1,i)=1;
a
%方法二(一个for)
a=zeros(n,n);
a(1,1:
2)=[56];
fori=2:
(n-1),a(i,[i-1,i,i+1])=[156];
a(n,[n-1n])=[15];
%方法三(不用for)
a=diag(5*ones(n,1));
b=diag(6*ones(n-1,1));
c=diag(ones(n-1,1));
a=a+[zeros(n-1,1),b;
zeros(1,n)]+[zeros(1,n);
c,zeros(n-1,1)]
%下列计算
det(a)
665
inv(a)
0.3173-0.58651.0286-1.62411.9489
-0.09770.4887-0.85711.3534-1.6241
0.0286-0.14290.5429-0.85711.0286
-0.00750.0376-0.14290.4887-0.5865
0.0015-0.00750.0286-0.09770.3173
[v,d]=eig(a)
-0.7843-0.7843-0.92370.9860-0.9237
0.5546-0.5546-0.3771-0.00000.3771
-0.2614-0.26140.0000-0.16430.0000
0.0924-0.09240.0628-0.0000-0.0628
-0.0218-0.02180.02570.02740.0257
0.75740000
09.2426000
007.449500
0005.00000
00002.5505
%Exercise7
(1)
[v,d]=eig(a)
det(v)
-0.9255%v行列式正常,特征向量线性相关,可对角化
inv(v)*a*v%验算
-3.05270.0000-0.0000
0.00003.6760-0.0000
-0.0000-0.00008.3766
[v2,d2]=jordan(a)%也可用jordan
v2=
0.07980.00760.9127
0.1886-0.31410.1256
-0.1605-0.26070.4213%特征向量不同
d2=
8.376600
0-3.0527-0.0000i0
003.6760+0.0000i
v2\a*v2
8.376600.0000
0.0000-3.05270.0000
0.00000.00003.6760
1)./v2(:
2)%对应相同特征值的特征向量成比例
2.4491
%Exercise7
(2)
-5.0566e-028-5.1918e-017i%v的行列式接近0,特征向量线性相关,不可对角化
[v,d]=jordan(a)
101
100
1-10
110
011
001%jordan标准形不是对角的,所以不可对角化
%Exercise7(3)
[v,d]=eig(A)
inv(v)*A*v
0.01020.0000-0.00000.0000
0.00000.8431-0.0000-0.0000
-0.00000.00003.8581-0.0000
-0.0000-0.0000030.2887
%本题用jordan不行,原因未知
%Exercise7(4)参考6(4)和7
(1),略
%Exercise8只有(3)对称,且特征值全部大于零,所以是正定矩阵.
%Exercise9
(1)
a=[4-313;
2-135;
1-1-1-1;
3-234;
7-6-70]
rank(a)
rank(a(1:
3,:
))
rank(a([124],:
))%1,2,4行为最大无关组
b=a([124],:
)'
c=a([35],:
b\c%线性表示的系数
0.50005.0000
-0.50001.0000
0-5.0000
%Exercise10
a=[1-22;
-2-24;
24-2]
0.33330.9339-0.1293
0.6667-0.3304-0.6681
-0.66670.1365-0.7327
-7.000000
02.00000
002.0000
v'
*v
1.00000.00000.0000
0.00001.00000
0.000001.000