Implement Jacobi iteration as a MATLAB function with the format: function [x err
ID: 668188 • 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 Örst 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 niter flag] = jacobi( A,x,b,tol,maxiter )
%jacobi iterations
% Guess vector
x=zeros(size(b));
%matrix os system Ax=b
[n,m]=size(A);
%iterations
i=1;
%loading max iterations
niter=maxiter;
for i=1:maxiter
for j=1:n
x(j)=(b(j)-A(j,1:j-1)*x(1:j-1)-A(j,j+1:n)*x(j+1:n))/A(j,j)
end
if max(abs(A*x-b))<tol
niter=i;
break;
end
x=y';
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.