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

Write a user-defined MATLAB function that solves the system of linear equations

ID: 3283647 • Letter: W

Question

Write a user-defined MATLAB function that solves the system of linear equations of Ax = b using the iterative Jacobi method. For function name and arguments, use xJacobi(A, b, xO) The input arguments are the coefficient matrix (A), right hand side vector (b), and initial guess (x0). The function should first check if A is a square matrix and A and b and xO have the same number of rows. If not, an appropriate error message should appear on the screen and the program is terminated. The function should stop and return the answer when any the of following criteria is met 1. estimated relative error for all unknowns becomes less than 0.00001 2. number of iterations reaches 100

Explanation / Answer


function x1 = jacobi1(a,b,x0,tol)
[c d] =size(a);
if ( c == d && length(b) == length(x0))
n = length(b);
x = zeros(n,1);
for j = 1 : n
x(j) = ((b(j) - a(j,[1:j-1,j+1:n]) * x0([1:j-1,j+1:n])) / a(j,j)); % the first iteration
end
x1 = x';
k = 1;
x_ny = zeros(n,1);
while norm(x1-x0,1) > tol
for j = 1 : n
x_ny(j) = ((b(j) - a(j,[1:j-1,j+1:n]) * x1([1:j-1,j+1:n])) / a(j,j));
end
x0 = x1;
x1 = x_ny';
k = k + 1;
end
k % number of iteration
x = x1';
else
fprintf('Dimension does not math');
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