modify the jacobi function file given in coding exercise 1 to create a new function file in matlab that executes gauss-seidel iteration in matlab

You can modify the Jacobi function file to create a new function file for Gauss-Seidel iteration in MATLAB. Here's the modified MATLAB code for creating a Gauss-Seidel function:

main.m
function [X, iter] = gauss_seidel(A, b, X0, tol, max_iter)
    n = length(b);
    iter = 0;
    X = X0;
    while iter < max_iter
        X_new = X;
        for i = 1:n
            sigma = A(i,1:i-1)*X(1:i-1) + A(i,i+1:n)*X(i+1:n);
            X_new(i) = (b(i) - sigma) / A(i,i);
        end
        if norm(X_new - X) < tol
            X = X_new;
            return;
        end
        X = X_new;
        iter = iter + 1;
    end
end
436 chars
19 lines

You can save this code in a file named gauss_seidel.m and then call it similar to how you would call the Jacobi function.

gistlibby LogSnag