您的当前位置:首页线性方程组迭代解法

线性方程组迭代解法

2021-11-28 来源:乌哈旅游
实验六:线性方程组迭代解法

1)实验目的

• 熟悉Matlab编程;

• 学习线性方程组迭代解法的程序设计算法 2)实验题目

1.研究解线性方程组Ax=b迭代法收敛速度。A为20阶五对角距阵

31214A12312141412312141412312141412312 14123要求:

(1)选取不同的初始向量x0 及右端向量b,给定迭代误差要求,用雅可比迭代和高斯-赛德尔迭代法求解,观察得到的序列是否收敛?若收敛,记录迭代次数,分析计算结果并得出你的结论。

(2)用SOR迭代法求解上述方程组,松弛系数ω取1< ω <2的不同值,在 时停止迭代.记录迭代次数,分析计算结果并得出你的结论。

2.给出线性方程组Hnxb,其中系数矩阵Hn为希尔伯特矩阵:

Hnhijnn,hijTi,i,j1,2,,n.

ij1假设x*1,1,,1n,bHnx.若取n6,8,10,分别用雅可比迭代法及SOR迭代(1,1.25,1.5)求解,比较计算结果。 3)实验原理与理论基础

1.雅克比(Jacobi)迭代法算法设计:

①输入矩阵a与右端向量b及初值x(1,i); ②按公式计算得

xi(k1)1aiin(k)baxiijjj1ji(i1,2,,n)

2.高斯――赛得尔迭代法算法设计:

1. 输入矩阵a与右端向量b及初值x(1,i).

2.

x(k1)i1aiii1biaijx(jk1)j1(k)axijj (i = 1, 2,…, n)

ji1n 3.超松驰法算法设计:

①输入矩阵a与右端向量b及初值x(1,i)。

②x(k1)i(1)x(k)ibiaijxaiij1i1(k1)jji1anijx(k)j,02 4)实验内容

第一题实验程序: 1.雅克比迭代法: function []=yakebi(e) %输入矩阵a与右端向量b。 for i=1:20 a(i,i)=3; end

for i=3:20 for j=i-2

a(i,j)=-1/4; a(j,i)=-1/4; end end

for i=2:20 for j=i-1

a(i,j)=-1/2; a(j,i)=-1/2; end end

b=[2.2 1.7 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.7 2.2]; k=1;

n=length(a); for i=1:n

x(1,i)=1;%数组中没有第0行。 end

while k>=1 for i=1:n m=0;

%此步也可以用ifj~=i条件判定一下。 for j=1:(i-1)

m=m+a(i,j)*x(k,j); end

for j=(i+1):n

m=m+a(i,j)*x(k,j); end

x(k+1,i)=(b(i)-m)/a(i,i); end l=0;

%判定满足条件使循环停止迭代。 for i=1:n

l=l+abs(x(k+1,i)-x(k,i)); end if l%输出所有的x的值。 x(k+1,:)

k

2.高斯—赛德尔迭代法: function []=gaoshisaideer(e) for i=1:20 a(i,i)=3; end

for i=3:20 for j=i-2

a(i,j)=-1/4; a(j,i)=-1/4; end end

for i=2:20 for j=i-1

a(i,j)=-1/2; a(j,i)=-1/2; end end

b=[2.2 1.7 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.7 2.2]; k=1;

n=length(a); for i=1:n

x(1,i)=0;%数组中没有第0行。 end

while k>=1 for i=1:n p=0;q=0;

for j=1:(i-1)

p=p+a(i,j)*x(k+1,j); end

for j=(i+1):n

q=q+a(i,j)*x(k,j); end

x(k+1,i)=(b(i)-q-p)/a(i,i); end l=0;

%判定满足条件使循环停止迭代。 for i=1:n

l=l+abs(x(k+1,i)-x(k,i)); end if l%输出所有的x的值。 x(k+1,:) k

3.SOR迭代法程序:

function []=caosongci(e,w) for i=1:20 a(i,i)=3; end

for i=3:20 for j=i-2

a(i,j)=-1/4; a(j,i)=-1/4; end end

for i=2:20 for j=i-1

a(i,j)=-1/2; a(j,i)=-1/2; end end

b=[2.2 1.7 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.7 2.2]; k=1;

n=length(a); for i=1:n

x(1,i)=0;%数组中没有第0行。 end

while k>=1

if w>=2||w<=1

'请重新输入w的值,w在1与2之间';

break end for i=1:n p=0;q=0;

for j=1:(i-1)

p=p+a(i,j)*x(k+1,j); end

for j=i:n

q=q+a(i,j)*x(k,j); end

x(k+1,i)=x(k,i)+w*(b(i)-q-p)/a(i,i); end l=0;

%判定满足条件使循环停止迭代。 for i=1:n

l=l+abs(x(k+1,i)-x(k,i)); end if l%输出所有的x的值。 x(k+1,:) k

第二题实验程序: 1.雅克比迭代法:

function X = p211_1_JJ(n)

Hn = GET_Hn(n); b = GET_b(n); temp = 0;

X0 = zeros(1, n); X_old = zeros(1, n); X_new = zeros(1, n);

disp('Now Jacobi method!');

disp('Start with the vector that (0, 0, 0, ...)^T'); for i = 1:n

for k = 1:n

X_old = X_new; temp = 0; for j = 1:n if(j ~= i)

temp = temp + Hn(i, j) * X_old(j); end

end

X_new(i) = (b(i) - temp) / Hn(i, i); end end

X = X_new; end

2.SOR迭代法:

function X = p211_1_SOR(n, w) Hn = GET_Hn(n); b = GET_b(n); temp01 = 0; temp02 = 0;

X0 = zeros(1, n); X_old = zeros(1, n); X_new = zeros(1, n);

disp('Now Successive Over Relaxtion method!');

disp('Start with the vector that (0, 0, 0, ...)^T'); for i = 1:n for k = 1:n

X_old = X_new; temp01 = 0; temp02 = 0; for j = 1:n if(j < i)

temp01 = temp01 + Hn(i, j) * X_new(j); end

if(j > i)

temp02 = temp02 + Hn(i, j) * X_old(j); end end end

X_new(i) = w * (b(i) - temp01 - temp02) / Hn(i, i) + X_old(i); end

X = X_new; end

5)实验结果 第一题实验结果: 1.雅克比迭代法: 输入:

>> b=[2.2 1.7 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.7 2.2]; yakebi(0.00001) 结果: ans =

Columns 1 through 12

0.9793 0.9787 0.9941 0.9970 0.9989 0.9995 0.9998 0.9999 1.0000 1.0000 1.0000 1.0000

Columns 13 through 20

0.9999 0.9998 0.9995 0.9989 0.9970 0.9941 0.9787 0.9793

k = 12

2.高斯—赛德尔迭代法: 此时初值全取1; 输入:

>> b=[2.2 1.7 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.7 2.2]; >> gaoshisaideer(0.00001) 结果:ans =

Columns 1 through 12

0.9793 0.9787 0.9941 0.9970 0.9989 0.9995 0.9998 0.9999 1.0000 1.0000 1.0000 1.0000 Columns 13 through 20

0.9999 0.9998 0.9995 0.9989 0.9970 0.9941 0.9787 0.9793 k =

14

此时初值全取1;

输入:

>> b=[2.5 1.9 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.9 2.5]; gaoshisaideer(0.00001) 结果: ans =

Columns 1 through 12

1.0969 1.0707 1.0219 1.0103 1.0039 1.0016 1.0006 1.0003 1.0001 1.0001 1.0001 1.0001

Columns 13 through 20

1.0003 1.0006 1.0016 1.0039 1.0103 1.0219 1.0707 1.0969

k = 14

3.SOR迭代法:

>> caosongci(0.00001,1.1) ans =

Columns 1 through 12

0.9793 0.9787 0.9941 0.9970 0.9989 0.9995 0.9998 0.9999 1.0000 1.0000 1.0000 1.0000

Columns 13 through 20

0.9999 0.9998 0.9995 0.9989 0.9970 0.9941 0.9787

0.9793

k = 11

>> caosongci(0.00001,1.2) ans =

Columns 1 through 12

0.9793 0.9787 0.9941 0.9970 0.9989 0.9995 0.9998 0.9999 1.0000 1.0000 1.0000 1.0000

Columns 13 through 20

0.9999 0.9998 0.9995 0.9793

k = 12

>> caosongci(0.00001,1.3) ans =

Columns 1 through 12

0.9793 0.9787 0.9941 0.9999 1.0000 1.0000 1.0000 Columns 13 through 20

0.9999 0.9998 0.9995 0.9793

k = 15

>> caosongci(0.00001,1.4) ans =

Columns 1 through 12

0.9793 0.9787 0.9941 0.9999 1.0000 1.0000 1.0000 Columns 13 through 20

0.9999 0.9998 0.9995 0.9793

k = 19

>> caosongci(0.00001,1.5) ans =

Columns 1 through 12

0.9793 0.9787 0.9941 0.9999 1.0000 1.0000 1.0000 Columns 13 through 20

0.9999 0.9998 0.9995 0.9793

k = 25

>> caosongci(0.00001,1.6)

0.9989 0.9970 0.9970 0.9989 1.0000

0.9989 0.9970 0.9970 0.9989 1.0000

0.9989 0.9970 0.9970 0.9989 1.0000

0.9989 0.9970 0.9941 0.9787 0.9995 0.9998 0.9941 0.9787 0.9995 0.9998 0.9941 0.9787 0.9995 0.9998 0.9941 0.9787 ans =

Columns 1 through 12

0.9793 0.9787 0.9941 0.9970 0.9989 0.9995 0.9998 0.9999 1.0000 1.0000 1.0000 1.0000

Columns 13 through 20

0.9999 0.9998 0.9995 0.9989 0.9970 0.9941 0.9787 0.9793

k = 34

>> caosongci(0.00001,1.7) ans =

Columns 1 through 12

0.9793 0.9787 0.9941 0.9970 0.9989 0.9999 1.0000 1.0000 1.0000 1.0000

Columns 13 through 20

0.9999 0.9998 0.9995 0.9989 0.9970 0.9793

k = 47

>> caosongci(0.00001,1.8) ans =

Columns 1 through 12

0.9793 0.9787 0.9941 0.9970 0.9989 0.9999 1.0000 1.0000 1.0000 1.0000

Columns 13 through 20

0.9999 0.9998 0.9995 0.9989 0.9970 0.9793

k = 73

>> caosongci(0.00001,1.9) ans =

Columns 1 through 12

0.9793 0.9787 0.9941 0.9970 0.9989 0.9999 1.0000 1.0000 1.0000 1.0000

Columns 13 through 20

0.9999 0.9998 0.9995 0.9989 0.9970 0.9793

k = 150

第二题实验结果: 1.雅克比迭代法: >> p211_1_JJ(6)

Now Jacobi method!

Start with the vector that (0, 0, 0, ...)^T

0.9995 0.9998 0.9941 0.9787 0.9995 0.9998 0.9941 0.9787 0.9995 0.9998 0.9941 0.9787 ans =

2.4500 1.1036 0.6265 0.4060 0.2831 0.2071 >> p211_1_JJ(8) Now Jacobi method!

Start with the vector that (0, 0, 0, ...)^T ans =

2.7179 1.4101 0.8524 0.5809 0.4221 0.3198 0.2497 0.1995

>> p211_1_JJ(10)

Now Jacobi method!

Start with the vector that (0, 0, 0, ...)^T ans =

Columns 1 through 9

2.9290 1.6662 1.0517 0.7423 0.5554 0.4315 0.3445 0.2807 0.2325 Column 10

0.1951

2.SOR迭代法:

n=6, ω=1,1.25,1.5的时候 >> p211_1_SOR(6, 1)

Now Successive Over Relaxtion method!

Start with the vector that (0, 0, 0, ...)^T ans =

2.4500 1.1036 0.6265 0.4060 0.2831 0.2071 >> p211_1_SOR(6, 1.25)

Now Successive Over Relaxtion method!

Start with the vector that (0, 0, 0, ...)^T ans =

3.0625 0.2310 0.8704 0.3389 0.3141 0.2097 >> p211_1_SOR(6, 1.5)

Now Successive Over Relaxtion method!

Start with the vector that (0, 0, 0, ...)^T ans =

3.6750 -1.1009 2.0106 -0.3994 0.7670 -0.0384 与n=8, ω=1,1.25,1.5的时候 >> p211_1_SOR(8, 1)

Now Successive Over Relaxtion method!

Start with the vector that (0, 0, 0, ...)^T ans =

2.7179 1.4101 0.8524 0.5809 0.4221 0.3198 0.2497 0.1995

>> p211_1_SOR(8, 1.25)

Now Successive Over Relaxtion method!

Start with the vector that (0, 0, 0, ...)^T

ans =

3.3973 0.4887 1.0898 0.5062 0.4501 0.3203 0.2573 0.2042

>> p211_1_SOR(8, 1.5)

Now Successive Over Relaxtion method!

Start with the vector that (0, 0, 0, ...)^T ans =

4.0768 -0.9424 2.2923 -0.2753 0.9252 0.0578 0.4071 0.1275

与n=10, ω=1,1.25,1.5的时候 >> p211_1_SOR(10, 1)

Now Successive Over Relaxtion method!

Start with the vector that (0, 0, 0, ...)^T ans =

Columns 1 through 9

2.9290 1.6662 1.0517 0.7423 0.5554 0.4315 0.3445 0.2807 0.2325 Column 10 0.1951

>> p211_1_SOR(10, 1.25)

Now Successive Over Relaxtion method!

Start with the vector that (0, 0, 0, ...)^T ans =

Columns 1 through 9

3.6612 0.7098 1.2835 0.6617 0.5807 0.4299 0.3506 0.2844 0.2363 Column 10 0.1984

>> p211_1_SOR(10, 1.5)

Now Successive Over Relaxtion method!

Start with the vector that (0, 0, 0, ...)^T ans =

Columns 1 through 9

4.3935 -0.7958 2.5326 -0.1523 1.0720 0.1565 0.5050 0.2041 0.2819 Column 10 0.1766

6)实验结果分析与小结

本次实习主要是学会应用雅克比迭代法、高斯—赛德尔迭代法、SOR迭代法三种迭代法,并且了解三种迭代法的性质以及迭代精度等。

第一题中取的b对于雅克比迭代法、高斯――赛得尔迭代法都是收敛的,对于相同的初值与右端向量明显可以看出高斯――赛得尔迭代法比雅克比迭代法快。由第二题可得出对于SOR迭代方法选择不同的松弛因子,收敛次数大大不同,而当松弛因子为1.1时,在同等条件下迭代最快。

因篇幅问题不能全部显示,请点此查看更多更全内容