Last active
April 3, 2024 20:55
-
-
Save ivankahl/ead8caaaab073fedd006270cab7128d0 to your computer and use it in GitHub Desktop.
The Jacobi method implemented in Octave.
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 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanksX!