Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement Jacobi iteration as a MATLAB function with the format: function [x err

ID: 669317 • Letter: I

Question

Implement Jacobi iteration as a MATLAB function with the format:

function [x error niter flag] = my_jacobi(A,x,b,maxiter,tol)

where:

INPUT

A: the matrix of the system Ax=b

x: the First guess vector Ax=b

b: The vector in the system

maxiter: the maximum number of iteration to perform

tol: the tolerance

OUTPUT

x: the solution vector

niter: the number of iterations it took

flag: Indicates whether a solution was found within the specified

number of iterations. 0 means a solution was found, 1 means no

solution was found.

Explanation / Answer

Comments added

function [x, error, iter, flag] = my_jacobi(A, x, b, maxiter, tol)
%variables declaered
iter = 0;
flag = 0;

bnrm2 = norm(b);
if (bnrm2 == 0)
bnrm2 = 1.0;
end
%calculating error
r = b - x*A;
error = norm(r) / bnrm2;
if (error<tol)
return;
end
%size of matrix
[m,n] = size(A);
%calculating diagonal
M = diag(diag(A));
N = diag(diag(A)) - A;
%for loop iteration
for iter = 1:maxiter,
oldx = x;
x = M (N*x + b);

error = norm(x - oldx) / norm(x);
if (error <= tol)
break;
end
end
%if error greater than tolerance
if (error > tol)
flag = 1;
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote