⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gauss_seidel_iterative.m

📁 利用高斯塞得尔迭代法解线性方程组
💻 M
字号:
% This is a program used to solve the linear equations 
% with the method of gauss_seidel_iterative.  The reference is 
% "数值分析与算法",on page 68.  It is accomplished by viponlyone
% on 3/11/2008.
% 此算法是根据书上65页的简单迭代法改进而来,需要在矩阵有对
% 角优势时才可以用,否则结果会发散。

%function y=gauss_seidel_iterative(A,B,eps);

%-------parameters used in this program-------------%
%    N    : the order of the linear equeations
%    A    : the coefficients matrix of the linear equeations
%    B    : the costant matrix of the linear equeations
%    eps  : the precision of the operation
%---------------------------------------------------%

A=[7   2   1  -2;
   9   15  3  -2;
   -2  -2  11  5;
   1   3   2  13];
B=[4   7  -1   0];
eps=0.000001;
[N,N]=size(A);
w=0;
q=zeros(N,1);
X=zeros(N,1);

for i=1:N
    p=0.0;
    for j=1:N
        if i~=j
            p=p+abs(A(i,j));
        end
    end 
    if p>abs(A(i,i))
        disp('It is not a diagnal dominate matrix');
        break
    end
end

for r=1:N.^2
    for i=1:N
        p=0.0;
        for j=1:N
            if i~=j
                p=p+A(i,j)*X(j);
            end
        end
        t=X(i);
        X(i)=(B(i)-p)/A(i,i);
        q(i)=abs(X(i)-t)/(1.0+abs(X(i)));
    end
    for h=1:N
        if q(i)<eps
            w=w+1;
        end
    end
    if w==N
        break
    end
end

disp('iteration time');disp(r);
disp('computation error');disp(q);
disp('final solution');disp(X);
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -