필터 지우기
필터 지우기

Changing the Jacobi Method into Gauss-Seidel method

조회 수: 3 (최근 30일)
jeff417
jeff417 2016년 10월 15일
댓글: Geoff Hayes 2016년 10월 15일
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
Geoff Hayes
Geoff Hayes 2016년 10월 15일
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.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Waveform Generation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by