Can you please right the matlab code for this question Finis
Can you please right the matlab code for this question?
Finish the function. Output should follow these rules (note it uses functions from above):
if diag_dom(A) = 1, use Jacobi iteration to solve the system (Ax = b) and output x (out = x)
else, if the iteration matrix, M, for the Jacobi method is such that will_converge(M)= 1, solve using Jacobi Method, output x
else, return an empty matrix (i. e. []). function out = iterative_solve(A,b,initial_x,tol)
Code:
%A is a square matrix, b a column vector with equal rows
%If possible solves Ax= b using Jacobi Method starting with
%initial_x, ending when norm(A*x_i -b,inf)
Solution
clear all;
if diag_dom(A) = 1
out = iterative_solve(A,b,intial_x,tol);
end
elseif will_converge(M)=1
out = iterative_solve (A,b,intial_x,tol);
end
else
out = 0;
end
%function for Jacobian Method
function out = iterative_solve(A,b,initial_x,tol)
%Matrix A is split into the three matrices L, U and D such that A=D-L-U
D = diag(diag(A));
L = tril(-A,-1);
U = triu(-A,1);
%transition matrix and constant vector used for iterations
% As Ax = b, or (D-L-U)x = b, or Dx = (L+U)x + b, or x = Inv(D)*(L+U)x + Inv(D)*b
% transition matrix, T = Inv (D)*(L+U) & constant matrix, c = Inv(D)*b
T = inv(D)*(L+U);
c = inv(D)*b;
x = intial_x;
while 1
x(:,k+1) = T*x(:,k) + c;
if norm(A*x(:,k+1)-b,inf)
out = x(:,k+1);
break
end
end
