Changing the Jacobi Method into Gauss-Seidel method
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
For my numerical methods class, we are tasked with changing the provided Jacobi function into a Gauss-Seidel function. We have to modify the given code so that it is similar. This is my problem; I can do the Gauss-Seidel method, but I'm not sure how to do it by modifying this code. Can anyone help?
function [x, er, N] = Jacobi(A,b,x1,maxtol,maxitr)
% Inputs/Outputs:
% A = the coefficient matrix
% b = right-hand-side column vector in Ax=b
% x1 = vector of initial estimate of the solution
% maxtol = maximum error tolerance
% maxitr = maximum number of iterations allowed
% Check if input coefficient matrix is square
[m, n] = size(A);
if m~=n
error('The input matrix is not square')
end
% Check if the input initial estimate solution vector is column or not
if isrow(x1), x1=x1.'; end % or use x=x(:);
% Initializations
x = [x1, zeros(n,maxitr)];
k = 0;
er = 1;
while er >= maxtol && k < maxitr
k = k+1;
fprintf('\n Iteration = %3i\n', k)
for i = 1:n
j = [1:i-1,i+1:n];
x(i,k+1) = (b(i)-sum(A(i,j)*x(j,k)))/A(i,i);
fprintf('x%i = %f,\t',i,x(i,k+1))
end
er = norm(x(:,k+1)-x(:,k))/norm(x(:,k+1));
fprintf('\nerror = %e\n',er)
end
N = k;
disp(' ')
disp('x =')
disp(x(:,1:N+1).')
end
1 Kommentar
Geoff Hayes
am 15 Okt. 2016
jeff - what is the algorithm for Gauss-Seidel? What are the similarities between it and the Jacobi? The function signature would be the same for both algorithms (with a name change only) and the bodies would be similar too: both have an outer while loop to check for convergence, both are trying to find the x, and both have inner for loops. I suspect that the main differences then would be how the x and the er are determined. Since you can do Gauss-Seidel (as stated above), then all you "should" need to do is to replace how x and er are calculated.
Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!