Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function x = gaussJacobi(A, B)
- [n, m] = size(A);
- if n ~= m
- error('Not a square matrix');
- end
- for i = 1 : n
- sum = 0;
- for j = 1 : n
- if i == j
- continue;
- end
- sum = sum + abs(A(i, j));
- end
- if abs(A(i, i)) <= sum
- error('Not diagonally dominant');
- end
- end
- for i = 1 : n
- val = A(i, i);
- for j = 1 : n
- A(i, j) = A(i, j) / val;
- end
- B(i, 1) = B(i, 1) / val;
- end
- x = zeros(1, n);
- while(1)
- y = zeros(1, n);
- for i = 1 : n
- y(1, i) = B(i, 1);
- for j = 1 : n
- if i == j
- continue;
- end
- y(1, i) = y(1, i) - A(i, j) * x(1, j);
- end
- end
- if x == y
- break;
- end
- x = y;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement