Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ME3255S2017/lecture_13/GaussSeidel.m
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
35 lines (33 sloc)
875 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function x = GaussSeidel(A,b,es,maxit) | |
% GaussSeidel: Gauss Seidel method | |
% x = GaussSeidel(A,b): Gauss Seidel without relaxation | |
% input: | |
% A = coefficient matrix | |
% b = right hand side vector | |
% es = stop criterion (default = 0.00001%) | |
% maxit = max iterations (default = 50) | |
% output: | |
% x = solution vector | |
if nargin<2,error('at least 2 input arguments required'),end | |
if nargin<4|isempty(maxit),maxit=50;end | |
if nargin<3|isempty(es),es=0.00001;end | |
[m,n] = size(A); | |
if m~=n, error('Matrix A must be square'); end | |
C = A-diag(diag(A)); | |
x=zeros(n,1); | |
for i = 1:n | |
C(i,1:n) = C(i,1:n)/A(i,i); | |
end | |
d = b./diag(A); | |
iter = 0; | |
while (1) | |
xold = x; | |
for i = 1:n | |
x(i) = d(i)-C(i,:)*x; | |
if x(i) ~= 0 | |
ea(i) = abs((x(i) - xold(i))/x(i)) * 100; | |
end | |
end | |
iter = iter+1; | |
if max(ea)<=es | iter >= maxit, break, end | |
end |