Skip to content

Instantly share code, notes, and snippets.

@ivankahl
Last active August 22, 2021 19:23
Show Gist options
  • Save ivankahl/e5894b0ba820287a3ba693932f9b2022 to your computer and use it in GitHub Desktop.
Save ivankahl/e5894b0ba820287a3ba693932f9b2022 to your computer and use it in GitHub Desktop.
The Gauss-Seidel method implemented in Octave.
function xnew = gauss_seidel(A, b, xold)
% This implementation of the Gauss-Seidel method in Octave uses
% a single loop. It is based on the solution provided in the
% APM1513 course work from UNISA.
% We first need to determine how many equations we need to solve
n = size(A, 1);
% We set the xnew to have the xold values
xnew = xold;
% Loop through each equation and determine a new x value for the
% xnew vector
for k = 1:n
% We first get the number in the diagonal for the current row in
% the matrix
diagonal_value = A(k,k);
% We then set that diagonal to 0 so that it will not affect our
% calculation of the x value in the next step
A(k,k) = 0;
% We then calculate the xnew value for the equation
xnew(k) = (b(k) - A(k,:)*xnew) / diagonal_value;
endfor
endfunction
@YungMills
Copy link

Hello there may i ask which version of Octave is the above code written in, i'm guessing it's the latest 6.2.0?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment