﻿ 130213024王秋凉+作业7 - 计算数学达人 - 专，学者，数值代数，微分方程数值解

### 130213024王秋凉+作业7

1)估计5到20阶Hilbert矩阵的inf范数条件数
syms n j i;
for n=5:20
a=zeros(n,n);%构造一个n*n的矩阵
for i=1:n
for j=1:n
a(i,j)=1/(i+j-1);
end
end
c=cond(a);%计算a的条件数
f=norm(c,inf);%计算c的无穷范数
fprintf('n=%fn norm(c,inf)%en',n,f)%输出
end

n=5.000000
norm(c,inf)4.766073e+005
n=6.000000
norm(c,inf)1.495106e+007
n=7.000000
norm(c,inf)4.753674e+008
n=8.000000
norm(c,inf)1.525758e+010
n=9.000000
norm(c,inf)4.931542e+011
n=10.000000
norm(c,inf)1.602468e+013
n=11.000000
norm(c,inf)5.226017e+014
n=12.000000
norm(c,inf)1.699490e+016
n=13.000000
norm(c,inf)3.459085e+017
n=14.000000
norm(c,inf)4.692946e+017
n=15.000000
norm(c,inf)2.446873e+017
n=16.000000
norm(c,inf)4.376709e+017
n=17.000000
norm(c,inf)4.314406e+017
n=18.000000
norm(c,inf)2.384043e+018
n=19.000000
norm(c,inf)6.369075e+018
n=20.000000
norm(c,inf)1.845813e+018

（2）估算x^的精度
%用列主元guass消去法计算Ax=b的计算解x^
function x2=gauss1(A,b)
[m,n]=size(A);
tol=1e-14;
p=[];
if n==m
for k=1:n-1
[r,c]=find(A==max(max(abs(A(k:n,k:n)))));
p(k)=r(1);
A([k,p(k)],:)=A([p(k),k],:);
if   abs(A(k,k))>tol
A(k+1:n,k)=A(k+1:n,k)/A(k,k);
A(k+1:n,k+1:n)=A(k+1:n,k+1:n)-A(k+1:n,k)*A(k,k+1:n);

else
break;
end
end
L=eye(n)+tril(A,-1);
U=triu(A);
% the interchanges of b by P
for k=1:length(p)
b([k,p(k)])=b([p(k),k]);
end
%Ly=b
b(1)=b(1)/L(1,1);
for j=2:n
b(j)=(b(j)-L(j,1:j-1)*b(1:j-1))/L(j,j);
end
%Ux=y
x2=zeros(n,1);
x2(n)=b(n)/U(n,n);
for j=n-1:-1:1
x2(j)=(b(j)-U(j,j+1:n)*x2(j+1:n))/U(j,j);
end
else
'A is not a square'
end
%估计x^的精度

for n=5:20
A1=-1*ones(n,n);
A=tril(A1);
for i=1:n
A(i,i)=1;
end
A(:,n)=1;
x=rand(n,1)
b=A*x;
x2=gauss1(A,b)
r=b-A*x2;
k=cond(A);
r1=k*norm(r,inf)/norm(b,inf)
end

x =

0.8147
0.9058
0.1270
0.9134
0.6324

x2 =

0.8147
0.9058
0.1270
0.9134
0.6324

r1 =

9.2799e-016

x =

0.0975
0.2785
0.5469
0.9575
0.9649
0.1576

x2 =

0.0975
0.2785
0.5469
0.9575
0.9649
0.1576

r1 =

4.3727e-016

x =

0.9706
0.9572
0.4854
0.8003
0.1419
0.4218
0.9157

x2 =

0.9706
0.9572
0.4854
0.8003
0.1419
0.4218
0.9157

r1 =

2.8639e-015

x =

0.7922
0.9595
0.6557
0.0357
0.8491
0.9340
0.6787
0.7577

x2 =

0.7922
0.9595
0.6557
0.0357
0.8491
0.9340
0.6787
0.7577

r1 =

4.5089e-015

x =

0.7431
0.3922
0.6555
0.1712
0.7060
0.0318
0.2769
0.0462
0.0971

x2 =

0.7431
0.3922
0.6555
0.1712
0.7060
0.0318
0.2769
0.0462
0.0971

r1 =

2.3956e-015

x =

0.8235
0.6948
0.3171
0.9502
0.0344
0.4387
0.3816
0.7655
0.7952
0.1869

x2 =

0.8235
0.6948
0.3171
0.9502
0.0344
0.4387
0.3816
0.7655
0.7952
0.1869

r1 =

8.5439e-015

x =

0.4898
0.4456
0.6463
0.7094
0.7547
0.2760
0.6797
0.6551
0.1626
0.1190
0.4984

x2 =

0.4898
0.4456
0.6463
0.7094
0.7547
0.2760
0.6797
0.6551
0.1626
0.1190
0.4984

r1 =

3.7652e-014

x =

0.9597
0.3404
0.5853
0.2238
0.7513
0.2551
0.5060
0.6991
0.8909
0.9593
0.5472
0.1386

x2 =

0.9597
0.3404
0.5853
0.2238
0.7513
0.2551
0.5060
0.6991
0.8909
0.9593
0.5472
0.1386

r1 =

2.3470e-014

x =

0.1493
0.2575
0.8407
0.2543
0.8143
0.2435
0.9293
0.3500
0.1966
0.2511
0.6160
0.4733
0.3517

x2 =

0.1493
0.2575
0.8407
0.2543
0.8143
0.2435
0.9293
0.3500
0.1966
0.2511
0.6160
0.4733
0.3517

r1 =

1.6762e-013

x =

0.8308
0.5853
0.5497
0.9172
0.2858
0.7572
0.7537
0.3804
0.5678
0.0759
0.0540
0.5308
0.7792
0.9340

x2 =

0.8308
0.5853
0.5497
0.9172
0.2858
0.7572
0.7537
0.3804
0.5678
0.0759
0.0540
0.5308
0.7792
0.9340

r1 =

5.4734e-013

x =

0.1299
0.5688
0.4694
0.0119
0.3371
0.1622
0.7943
0.3112
0.5285
0.1656
0.6020
0.2630
0.6541
0.6892
0.7482

x2 =

0.1299
0.5688
0.4694
0.0119
0.3371
0.1622
0.7943
0.3112
0.5285
0.1656
0.6020
0.2630
0.6541
0.6892
0.7482

r1 =

6.8259e-013

x =

0.4505
0.0838
0.2290
0.9133
0.1524
0.8258
0.5383
0.9961
0.0782
0.4427
0.1067
0.9619
0.0046
0.7749
0.8173
0.8687

x2 =

0.4505
0.0838
0.2290
0.9133
0.1524
0.8258
0.5383
0.9961
0.0782
0.4427
0.1067
0.9619
0.0046
0.7749
0.8173
0.8687

r1 =

1.9065e-012

x =

0.0844
0.3998
0.2599
0.8001
0.4314
0.9106
0.1818
0.2638
0.1455
0.1361
0.8693
0.5797
0.5499
0.1450
0.8530
0.6221
0.3510

x2 =

0.0844
0.3998
0.2599
0.8001
0.4314
0.9106
0.1818
0.2638
0.1455
0.1361
0.8693
0.5797
0.5499
0.1450
0.8530
0.6221
0.3510

r1 =

9.4976e-013

x =

0.5132
0.4018
0.0760
0.2399
0.1233
0.1839
0.2400
0.4173
0.0497
0.9027
0.9448
0.4909
0.4893
0.3377
0.9001
0.3692
0.1112
0.7803

x2 =

0.5132
0.4018
0.0760
0.2399
0.1233
0.1839
0.2400
0.4173
0.0497
0.9027
0.9448
0.4909
0.4893
0.3377
0.9001
0.3692
0.1112
0.7803

r1 =

1.0258e-011

x =

0.3897
0.2417
0.4039
0.0965
0.1320
0.9421
0.9561
0.5752
0.0598
0.2348
0.3532
0.8212
0.0154
0.0430
0.1690
0.6491
0.7317
0.6477
0.4509

x2 =

0.3897
0.2417
0.4039
0.0965
0.1320
0.9421
0.9561
0.5752
0.0598
0.2348
0.3532
0.8212
0.0154
0.0430
0.1690
0.6491
0.7317
0.6477
0.4509

r1 =

1.1940e-011

x =

0.5470
0.2963
0.7447
0.1890
0.6868
0.1835
0.3685
0.6256
0.7802
0.0811
0.9294
0.7757
0.4868
0.4359
0.4468
0.3063
0.5085
0.5108
0.8176
0.7948

x2 =

0.5470
0.2963
0.7447
0.1890
0.6868
0.1835
0.3685
0.6256
0.7802
0.0811
0.9294
0.7757
0.4868
0.4359
0.4468
0.3063
0.5085
0.5108
0.8176
0.7948

r1 =

2.3413e-011