Na Lab 4
Na Lab 4
TECHNOLOGY
ISLAMABAD
% Check for convergence based on the maximum norm of the difference between
old and new solution vectors
if norm(x_new - x, inf) < tol
break;
end
% Update the solution vector for the next iteration
x = x_new;
% Increment the iteration counter
iter = iter + 1;
end
% Display the final solution vector and the number of iterations performed
disp('Final Solution:');
disp(x_new);
disp(['Number of iterations:', num2str(iter)]);
Output:
n = length(b);
x = x0;
iter = 0;
while iter < max_iter
x_new = x;
for i = 1:n
sigma = 0;
for j = 1:i-1
sigma = sigma + A(i, j) * x_new(j);
end
for j = i+1:n
sigma = sigma + A(i, j) * x(j);
end
x_new(i) = (b(i) - sigma) / A(i, i);
end
if iter == max_iter
disp('Maximum iterations reached without convergence');
end
disp('Solution:');
disp(x);
disp(['Number of iterations:', num2str(iter)]);
output: