1、线性方程组迭代解法实验六:线性方程组迭代解法 1)实验目的 熟悉Matlab编程; 学习线性方程组迭代解法的程序设计算法2)实验题目1.研究解线性方程组Ax=b迭代法收敛速度。A为20阶五对角距阵 要求:(1)选取不同的初始向量x0 及右端向量b,给定迭代误差要求,用雅可比迭代和高斯-赛德尔迭代法求解,观察得到的序列是否收敛?若收敛,记录迭代次数,分析计算结果并得出你的结论。(2)用SOR迭代法求解上述方程组,松弛系数取1 =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)
2、: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=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(
3、k,i); end if l=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 le break end k=k+1;end%输出所有的x的值。 x(k+1,:)k第二题实验程序
4、: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_o
5、ld(j); end end X_new(i) = (b(i) - temp) / Hn(i, i); endendX = X_new;end2.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
6、, .)T);for i = 1:n for k = 1:n X_old = X_new; temp01 = 0; temp02 = 0; for j = 1:n 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);endX = X_new;end5)实验结果 第一题实验结果: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
7、 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.9793k = 122.高斯赛德尔迭代法:此时初值全取1;输入: b=2.2 1.7 1.5 1.5 1.5 1.5 1.5 1.5 1.
8、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.9793k =14此时初值全取1;输入: b=2.5 1.9 1.5 1.5 1.5 1
9、.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.0969k = 143.SOR迭代法: caosongci(0.
10、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.9793k = 11 caosongci(0.00001,1.2)ans = Columns 1 through 12 0.9793 0.9787 0.9941 0.9970 0.9989 0.9995 0.99
11、98 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.9793k = 12 caosongci(0.00001,1.3)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.
12、9989 0.9970 0.9941 0.9787 0.9793k = 15 caosongci(0.00001,1.4)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.9793k = 19 caosongci(0.00001,1.5)ans = Columns 1 throug
13、h 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.9793k = 25 caosongci(0.00001,1.6)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
14、.0000 Columns 13 through 20 0.9999 0.9998 0.9995 0.9989 0.9970 0.9941 0.9787 0.9793k = 34 caosongci(0.00001,1.7)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
15、k = 47 caosongci(0.00001,1.8)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.9793k = 73 caosongci(0.00001,1.9)ans = Columns 1 through 12 0.9793 0.9787 0.9941 0.9970
16、 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.9793k = 150第二题实验结果:1.雅克比迭代法: p211_1_JJ(6) Now Jacobi method!Start with the vector that (0, 0, 0, .)Tans = 2.4500 1.1036 0.6265 0.4060 0.2831 0.2071 p211_1_JJ(8) Now Jacob
17、i method!Start with the vector that (0, 0, 0, .)Tans =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, .)Tans = 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.19512.SOR迭代法:n=6,
18、=1,1.25,1.5的时候 p211_1_SOR(6, 1)Now Successive Over Relaxtion method!Start with the vector that (0, 0, 0, .)Tans = 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, .)Tans = 3.0625 0.2310 0.8704 0.3389 0.3141 0.2097
19、p211_1_SOR(6, 1.5)Now Successive Over Relaxtion method!Start with the vector that (0, 0, 0, .)Tans = 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, .)Tans =2.7179 1.4101 0.8524 0.5809 0.4221 0
20、.3198 0.2497 0.1995 p211_1_SOR(8, 1.25)Now Successive Over Relaxtion method!Start with the vector that (0, 0, 0, .)Tans =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, .)Tans =4.0768 -0.9424 2.2923 -
21、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, .)Tans = 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 Rela
22、xtion method!Start with the vector that (0, 0, 0, .)Tans = 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, .)Tans = 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.17666)实验结果分析与小结本次实习主要是学会应用雅克比迭代法、高斯赛德尔迭代法、SOR迭代法三种迭代法,并且了解三种迭代法的性质以及迭代精度等。 第一题中取的b对于雅克比迭代法、高斯赛得尔迭代法都是收敛的,对于相同的初值与右端向量明显可以看出高斯赛得尔迭代法比雅克比迭代法快。由第二题可得出对于SOR迭代方法选择不同的松弛因子,收敛次数大大不同,而当松弛因子为1.1时,在同等条件下迭代最快。
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2