create a function that executes gauss-seidel iteration in matlab

main.m
function x = gaussSeidel(A, b, x0, tol, max_iter)
    % Initializing variables
    n = length(b);
    x = x0;
    iter = 0;
    err = tol + 1;
    
    % Gauss-Seidel iteration
    while err > tol && iter < max_iter
        x_old = x;
        
        for i = 1:n
            x(i) = (b(i) - A(i,1:i-1)*x(1:i-1) - A(i,i+1:n)*x_old(i+1:n)) / A(i,i);
        end
        
        err = norm(x - x_old, inf);
        iter = iter + 1;
    end
    
    if iter == max_iter
        disp('Maximum number of iterations reached');
    else
        disp(['Converged in ', num2str(iter), ' iterations']);
    end
end
605 chars
26 lines

gistlibby LogSnag