MATLAB块雅克比迭代法求线性方程组Ax=b的解块高斯-赛德尔迭代法求线性方程组Ax=b的解function [x,N]= BJ(A,b,x0,d,eps,M) %块雅克比迭代法求线性方程组Ax=b的解if nargin==4eps= 1.0e-6;M = 10000;elseif nargin<4errorreturnelseif nargin ==5M = 10000; %参数的默认值endNS = size(A);n = NS(1,1);if(sum(d) ~= n)disp('分块错误!');return;endbnum = length(d);bs = ones(bnum,1);for i=1:(bnum-1)bs(i+1,1)=sum(d(1:i))+1;%获得对角线上每个分块矩阵元素索引的起始值endDB = zeros(n,n);for i=1:bnumendb = bs(i,1)+d(i,1)-1;DB(bs(i,1):endb,bs(i,1):endb)=A(bs(i,1):endb,bs(i,1):endb );%求A的对角分块矩阵endfor i=1:bnumendb = bs(i,1)+d(i,1)-1;invDB(bs(i,1):endb,bs(i,1):endb)=inv(DB(bs(i,1):endb,bs(i ,1): endb));%求A的对角分块矩阵的逆矩阵endN = 0;tol = 1;while tol>=epsx = invDB*(DB-A)*x0+invDB*b; %由于LB+DB=DB-AN = N+1; %迭代步数tol = norm(x-x0); %前后两步迭代结果的误差x0 = x;if(N>=M)disp('Warning: 迭代次数太多,可能不收敛!');return;endendfunction [x,N]= BGS(A,b,x0,d,eps,M) %块高斯-赛德尔迭代法求线性方程组Ax=b的解if nargin==4eps= 1.0e-6;M = 10000;elseif nargin<4errorreturnelseif nargin ==5M = 10000;endNS = size(A);n = NS(1,1);bnum = length(d);bs = ones(bnum,1);for i=1:(bnum-1)bs(i+1,1)=sum(d(1:i))+1;%获得对角线上每个分块矩阵元素索引的起始值endDB = zeros(n,n);for i=1:bnumendb = bs(i,1)+d(i,1)-1;DB(bs(i,1):endb,bs(i,1):endb)=A(bs(i,1):endb,bs(i,1):endb ); %求A的对角分块矩阵endLB = -tril(A-DB); %求A的下三角分块阵UB = -triu(A-DB); %求A的上三角分块阵N = 0;tol = 1;while tol>=epsinvDL = inv(DB-LB);x = invDL*UB*x0+invDL*b; %块迭代公式N = N+1;tol = norm(x-x0);x0 = x;if(N>=M)disp('Warning: 迭代次数太多,可能不收敛!');return;endend类别:matlab 编程 | | 添加到搜藏 | 分享到i贴吧 | 浏览(168) | 评论 (0)上一篇:MATLAB 共轭梯度法求线性方程组A...。