Skip to content

Instantly share code, notes, and snippets.

@samstewart
Created November 18, 2016 20:14
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 samstewart/2403891df44745d3c49bc463ccc38fca to your computer and use it in GitHub Desktop.
Save samstewart/2403891df44745d3c49bc463ccc38fca to your computer and use it in GitHub Desktop.
Jacobi method in matlab
% Method to solve a linear system via jacobi iteration
% A: matrix in Ax = b
% b: column vector in Ax = b
% N: number of iterations
% returns: column vector solution after N iterations
function sol = jacobi_method(A, b, N)
diagonal = diag(diag(A)); % strip out the diagonal
diag_deleted = A - diagonal; % delete the diagonal
sol = zeros(size(b, 1), 1); % initial guess of zero
for i = 1:N
% computing the matrix inverse
sol = diagonal \ (b - diag_deleted * sol)
end
end
@abdul-ma
Copy link

please can you tell me Jacobi method general formula

@matin1099
Copy link

thanks for code!!!

@tcsmaster
Copy link

the diagonal\ is just ./diagonal, which imo is cleaner, more understandable, and more efficient.

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