Created
December 3, 2020 16:44
-
-
Save tieubinhco/a0e97b7fdcef3a119a87f672359b658a to your computer and use it in GitHub Desktop.
Gauss - Seidel MATLAB
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=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