Skip to content

Instantly share code, notes, and snippets.

@ivankahl
Last active April 3, 2024 20:55
Show Gist options
  • Save ivankahl/ead8caaaab073fedd006270cab7128d0 to your computer and use it in GitHub Desktop.
Save ivankahl/ead8caaaab073fedd006270cab7128d0 to your computer and use it in GitHub Desktop.
The Jacobi method implemented in Octave.
function xnew = jacobi(A, b, xold)
% This is a sample implementation of the Jacobi method in
% Octave.
% We first need to determine how many equations there are
% that we need to solve
n = size(A, 1);
% We create a blank xnew vector to store the final results
xnew = zeros(n, 1);
for k = 1:n
% We first get the diagonal value so that we can set it
% to null in the matrix
diagonal_value = A(k,k);
% Set the diagonal value to 0 so it doesn't affect our
% future calculations
A(k,k) = 0;
% We then calculate a new xnew value using the xold vector
xnew(k) = (b(k) - A(k,:)*xold) / diagonal_value;
endfor
endfunction
@LuchoQuiru
Copy link

thanksX!

@Tinovillar
Copy link

genius!

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