数值分析实验报告6.docx
- 文档编号:7077913
- 上传时间:2023-01-17
- 格式:DOCX
- 页数:15
- 大小:44.95KB
数值分析实验报告6.docx
《数值分析实验报告6.docx》由会员分享,可在线阅读,更多相关《数值分析实验报告6.docx(15页珍藏版)》请在冰豆网上搜索。
数值分析实验报告6
实验名称:
线性方程组迭代解法
1)实验目的:
1.熟悉Matlab编程。
2.学习线性方程组迭代解法的程序设计算法。
2)实验题目:
第一题:
研究解线性方程组Ax=b迭代法收敛速度。
A为20阶五对角距阵
要求:
(1)选取不同的初始向量x0及右端向量b,给定迭代误差要求,用雅可比迭代和高斯-赛德尔迭代法求解,观察得到的序列是否收敛?
若收敛,记录迭代次数,分析计算结果并得出你的结论。
(2)用SOR迭代法求解上述方程组,松弛系数ω取1<ω<2的不同值,在
时停止迭代.记录迭代次数,分析计算结果并得出你的结论。
第二题:
给出线性方程组
其中系数矩阵
为希尔伯矩阵:
,
,i=1,2……n.
假设
,
,若取n=6,8,10,分别用雅克比迭代及SOR迭代(w=1,1.25,1.5)求解.比较计算结果
3)实验原理与理论基础:
第一题:
(一)雅克比(Jacobi)迭代法算法设计:
①输入矩阵a与右端向量b及初值x(1,i);
②按公式计算得
(二)高斯――赛得尔迭代法算法设计:
1.输入矩阵a与右端向量b及初值x(1,i).
2.
(i=1,2,…,n)
(三)超松驰法算法设计:
①输入矩阵a与右端向量b及初值x(1,i)。
②
4)实验内容:
第一题:
代码:
①雅克比(Jacobi)迭代法
function[]=yakebi(e)
%输入矩阵a与右端向量b。
fori=1:
20
a(i,i)=3;
end
fori=3:
20
forj=i-2
a(i,j)=-1/4;
a(j,i)=-1/4;
end
end
fori=2:
20
forj=i-1
a(i,j)=-1/2;
a(j,i)=-1/2;
end
end
b=[2.21.71.51.51.51.51.51.51.51.51.51.51.51.51.51.51.51.51.72.2];
k=1;
n=length(a);
fori=1:
n
x(1,i)=1;%数组中没有第0行。
end
whilek>=1
fori=1:
n
m=0;
%此步也可以用ifj~=i条件判定一下。
forj=1:
(i-1)
m=m+a(i,j)*x(k,j);
end
forj=(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;
%判定满足条件使循环停止迭代。
fori=1:
n
l=l+abs(x(k+1,i)-x(k,i));
end
ifl break end k=k+1; end %输出所有的x的值。 x(k+1,: ) k ②高斯――赛得尔迭代法 function[]=gaoshisaideer(e) fori=1: 20 a(i,i)=3; end fori=3: 20 forj=i-2 a(i,j)=-1/4; a(j,i)=-1/4; end end fori=2: 20 forj=i-1 a(i,j)=-1/2; a(j,i)=-1/2; end end b=[2.21.71.51.51.51.51.51.51.51.51.51.51.51.51.51.51.51.51.72.2]; k=1; n=length(a); fori=1: n x(1,i)=0;%数组中没有第0行。 end whilek>=1 fori=1: n p=0;q=0; forj=1: (i-1) p=p+a(i,j)*x(k+1,j); end forj=(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; %判定满足条件使循环停止迭代。 fori=1: n l=l+abs(x(k+1,i)-x(k,i)); end ifl break end k=k+1; end %输出所有的x的值。 x(k+1,: ) k ③超松驰法 function[]=caosongci(e,w) fori=1: 20 a(i,i)=3; end fori=3: 20 forj=i-2 a(i,j)=-1/4; a(j,i)=-1/4; end end fori=2: 20 forj=i-1 a(i,j)=-1/2; a(j,i)=-1/2; end end b=[2.21.71.51.51.51.51.51.51.51.51.51.51.51.51.51.51.51.51.72.2]; k=1; n=length(a); fori=1: n x(1,i)=0;%数组中没有第0行。 end whilek>=1 ifw>=2||w<=1 '请重新输入w的值,w在1与2之间'; break end fori=1: n p=0;q=0; forj=1: (i-1) p=p+a(i,j)*x(k+1,j); end forj=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; %判定满足条件使循环停止迭代。 fori=1: n l=l+abs(x(k+1,i)-x(k,i)); end ifl break end k=k+1; end %输出所有的x的值。 x(k+1,: ) k 第二题: 代码: 雅克比迭代法实现的函数 functionX=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('NowJacobimethod! '); disp('Startwiththevectorthat(0,0,0,...)^T'); fori=1: n fork=1: n X_old=X_new; temp=0; forj=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 SOR迭代法实现的函数 functionX=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('NowSuccessiveOverRelaxtionmethod! '); disp('Startwiththevectorthat(0,0,0,...)^T'); fori=1: n fork=1: n X_old=X_new; temp01=0; temp02=0; forj=1: n if(j 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)实验结果: 第一题①雅克比(Jacobi)迭代法 此时初值全取1; >>b=[2.21.71.51.51.51.51.51.51.51.51.51.51.51.51.51.51.51.51.72.2]; yakebi(0.00001) ans= Columns1through9 0.97930.97870.99410.99700.99890.99950.99980.99991.0000 Columns10through18 1.00001.00001.00000.99990.99980.99950.99890.99700.9941 Columns19through20 0.97870.9793 k= 12 此时初值全取1; >>b=[2.51.91.51.51.51.51.51.51.51.51.51.51.51.51.51.51.51.51.92.5]; yakebi(0.00001) ans= Columns1through12 1.09691.07071.02191.01031.00391.00161.00061.00031.00011.00011.00011.0001 Columns13through20 1.00031.00061.00161.00391.01031.02191.07071.0969 k= 14 ②高斯――赛得尔迭代法 此时初值全取1; >>b=[2.21.71.51.51.51.51.51.51.51.51.51.51.51.51.51.51.51.51.72.2]; >>gaoshisaideer(0.00001) ans= Columns1through12 0.97930.97870.99410.99700.99890.99950.99980.99991.00001.00001.00001.0000 Columns13through20 0.99990.99980.99950.99890.99700.99410.97870.9793 k= 14 此时初值全取1; >>b=[2.51.91.51.51.51.51.51.51.51.51.51.51.51.51.51.51.51.51.92.5]; gaoshisaideer(0.00001) ans= Columns1through12 1.09691.07071.02191.01031.00391.00161.00061.00031.00011.00011.00011.0001 Columns13through20 1.00031.00061.00161.00391.01031.02191.07071.0969 k= 14 ③超松驰法 >>caosongci(0.00001,1.5) ans= Columns1through12 0.97930.97870.99410.99700.99890.99950.99980.99991.00001.00001.00001.0000 Columns13through20 0.99990.99980.99950.99890.99700.99410.97870.9793 k= 25 >>caosongci(0.00001,1.4) ans= Columns1through12 0.97930.97870.99410.99700.99890.99950.99980.99991.00001.00001.00001.0000 Columns13through20 0.99990.99980.99950.99890.99700.99410.97870.9793 k= 19 >>caosongci(0.00001,1.3) ans= Columns1through12 0.97930.97870.99410.99700.99890.99950.99980.99991.00001.00001.00001.0000 Columns13through20 0.99990.99980.99950.99890.99700.99410.97870.9793 k= 15 >>caosongci(0.00001,1.6) ans= Columns1through12 0.97930.97870.99410.99700.99890.99950.99980.99991.00001.00001.00001.0000 Columns13through20 0.99990.99980.99950.99890.99700.99410.97870.9793 k= 34 >>caosongci(0.00001,1.7) ans= Columns1through12 0.97930.97870.99410.99700.99890.99950.99980.99991.00001.00001.00001.0000 Columns13through20 0.99990.99980.99950.99890.99700.99410.97870.9793 k= 47 >>caosongci(0.00001,1.9) ans= Columns1through12 0.97930.97870.99410.99700.99890.99950.99980.99991.00001.00001.00001.0000 Columns13through20 0.99990.99980.99950.99890.99700.99410.97870.9793 k= 150 第二题: 对于雅克比迭代法,通过执行以下代码 >>p211_1_JJ(6) >>p211_1_JJ(8) >>p211_1_JJ(10) 可以分别得到: NowJacobimethod! Startwiththevectorthat(0,0,0,...)^T ans= 2.45001.10360.62650.40600.28310.2071 NowJacobimethod! Startwiththevectorthat(0,0,0,...)^T ans= 2.71791.41010.85240.58090.42210.31980.24970.1995 NowJacobimethod! Startwiththevectorthat(0,0,0,...)^T ans= Columns1through9 2.92901.66621.05170.74230.55540.43150.34450.28070.2325 Column10 0.1951 对于SOR迭代法,执行相对应代码 n=6,ω=1,1.25,1.5的时候 >>p211_1_SOR(6,1) NowSuccessiveOverRelaxtionmethod! Startwiththevectorthat(0,0,0,...)^T ans= 2.45001.10360.62650.40600.28310.2071 >>p211_1_SOR(6,1.25) NowSuccessiveOverRelaxtionmethod! Startwiththevectorthat(0,0,0,...)^T ans= 3.06250.23100.87040.33890.31410.2097 >>p211_1_SOR(6,1.5) NowSuccessiveOverRelaxtionmethod! Startwiththevectorthat(0,0,0,...)^T ans= 3.6750-1.10092.0106-0.39940.7670-0.0384 与n=8,ω=1,1.25,1.5的时候 >>p211_1_SOR(8,1) NowSuccessiveOverRelaxtionmethod! Startwiththevectorthat(0,0,0,...)^T ans= 2.71791.41010.85240.58090.42210.31980.24970.1995 >>p211_1_SOR(8,1.25) NowSuccessiveOverRelaxtionmethod! Startwiththevectorthat(0,0,0,...)^T ans= 3.39730.48871.08980.50620.45010.32030.25730.2042 >>p211_1_SOR(8,1.5) NowSuccessiveOverRelaxtionmethod! Startwiththevectorthat(0,0,0,...)^T ans= 4.0768-0.94242.2923-0.27530.92520.05780.40710.1275 与n=10,ω=1,1.25,1.5的时候 >>p211_1_SOR(10,1) NowSuccessiveOverRelaxtionmethod! Startwiththevectorthat(0,0,0,...)^T ans= Columns1through9 2.92901.66621.05170.74230.55540.43150.34450.28070.2325 Column10 0.1951 >>p211_1_SOR(10,1.25) NowSuccessiveOverRelaxtionmethod! Startwiththevectorthat(0,0,0,...)^T ans= Columns1through9 3.66120.70981.28350.66170.58070.42990.35060.28440.2363 Column10 0.1984 >>p211_1_SOR(10,1.5) NowSuccessiveOverRelaxtionmethod! Startwiththevectorthat(0,0,0,...)^T ans= Columns1through9 4.3935-0.79582.5326-0.15231.07200.15650.50500.20410.2819 Column10 0.1766 6)实验结果分析与小结: 1.在实习中,进行学习MATLAB,有助于将两者都更加的熟悉并加以充分利用。 2.此次取的b对于雅克比迭代法、高斯――赛得尔迭代法都是收敛的,对于相同的初值与右端向量明显可以看出高斯――赛得尔迭代法比雅克比迭代法快,这与理论上的分析完全一致嘛。 3.对于SOR迭代方法选择不同的松弛因子收敛次数大大不同,当松弛因子为1.1时,在同等条件下迭代最快.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数值 分析 实验 报告
![提示](https://static.bdocx.com/images/bang_tan.gif)