Created
November 18, 2016 20:14
-
-
Save samstewart/2403891df44745d3c49bc463ccc38fca to your computer and use it in GitHub Desktop.
Jacobi method in matlab
This file contains hidden or 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
% 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the diagonal\ is just ./diagonal, which imo is cleaner, more understandable, and more efficient.