Skip to content

Instantly share code, notes, and snippets.

@tieubinhco
Created December 3, 2020 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tieubinhco/a0e97b7fdcef3a119a87f672359b658a to your computer and use it in GitHub Desktop.
Save tieubinhco/a0e97b7fdcef3a119a87f672359b658a to your computer and use it in GitHub Desktop.
Gauss - Seidel MATLAB
function x=gauss(A,b)
%This function solves system of algebraic equations using Gaussian method
%Ax=b
%Input: matrix A, column vector b
%Output vector x
n=size(A,1);
b=b(:); %make sure b is a column vector.
nb=n+1;
Ab=[A b]; %extended matrix
%Forward process
for i=1:n-1
for j=i+1:n
Ab(j,i:nb)=Ab(j,i:nb)-Ab(j,i)*Ab(i,i:nb)/Ab(i,i);
end
end
%Inverse process
x=zeros(n,1);
x(n)=Ab(n,nb)/Ab(n,n);
for i=n-1:-1:1
x(i)=(Ab(i,nb)-Ab(i,i+1:n)*x(i+1:n))/Ab(i,i);
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment