Please solve the following in MATLAB Please add the comments
Please solve the following in MATLAB. Please add the comments in each line and provide the coding window and the command window. Try to make the codes as universal as possible. Write a function file that utilizes the Jacobi method to solve for the following system of linear equations. Your inputs should be the coefficient matrix, the vector of y values, and the initial guesses. x1 - 2x3 + x4 = 7 x2 - 7x3 - 2x4 = 12 x1 + x2 + x3 + x4 = 5 x1 - 10x4 = 10
Solution
Program:
A=[1 0 -2 1;0 1 -7 -2;1 1 -1 1;1 0 0 -10] % A matrix
B=[7;12;5;10] % Bmatrix
P=[0;0;0;0] % initial guess
delta=0.001 % tolerance
max1=15 % maximum number of iterations
X=jacobi(A,B,P,delta, max1)
Function Program:
function X=jacobi(A,B,P,delta, max1)
% Input - A is an N x N nonsingular matrix
% - B is an N x 1 matrix
% - P is an N x 1 matrix; the initial guess
% - delta is the tolerance for P
% - max1 is the maximum number of iterations
% Output - X is an N x 1 matrix: the jacobi approximation to
% the solution of AX = B
N = length(B);
for k=1:max1
for j=1:N
X(j)=(B(j)-A(j,[1:j-1,j+1:N])*P([1:j-1,j+1:N]))/A(j,j);
end
err=abs(norm(X\'-P));
relerr=err/(norm(X)+eps);
P=X\';
if (err<delta)|(relerr<delta)
break
end
end
X=X\';
