coding in matlab 3. Write a function to solve Ax = b using the Jacobi iterative
ID: 3112023 • Letter: C
Question
coding in matlab
3. Write a function to solve Ax = b using the Jacobi iterative method. The function should take in the following as inputs: a matrix A of sizen x n . a vector b of size n × 1 a number et that specifies the total relative squared error tolerance the maximum number of iterations to attempt kmaz The outputs of the function should be: a status variable that indicates whether the solution did or did not converge within kmaz iterations the solution vector x ] The function should iterate until the total estimated squared error summed across all elements is less hantheoln ereotl clidor i denExplanation / Answer
n=input('dimension of the matrix)
function x1 = jacobi2(a,b,x0,tol)
for(i=1;i<=n;i++)
x(i)=0;
for(itr=1:maxit)
big=0;
for(i=1:n)
sum=0;
for(j=1:n)
if(!i=j)
sum=sum+a(i,j)*x(j);
end
end
temp=(a(i,n+1)-sum)/a(i,i)
relerror=norm((x(i)-temp)*(x(i)-temp)/temp);
if(relerror>big)
big=relerror;
end
x(i)=temp;
end
end
if(big<=e)
fprintf("Converges to a solution in %d iterations ");
for(i=1:n)
x(i)
end
else
fprintf("does not converge in %d iteration ",maxit);
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.