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

Part 3: Write a function that solves a system of linear equations. The function

ID: 2261561 • Letter: P

Question

Part 3: Write a function that solves a system of linear equations. The function should have 2 inputs namely, the coefficient matrix [C] and the constant vector b). Additionally, the function should have l output, ie, the solution vector The function should first verify the size of inputs are compatible for matrix multiplication, and then compute the solution vector . If the row nos. are not same then display the error message, Dimensions of the input quantities are inconsistent Please check the dimensions. Test your function for the set of equations mentioned below: 2x-y=-3 2x-3y 3 4x+2y+3- 2x-3y+5-14 -Str + 2n-p+49 12m+6m+3p+2q 10 2 13m+7" + 10p-29 = 3 In the command window create a 3 x 3 matrices using the matrices (al and b1).Name this matrix as [C and use it as an input to the function along with the vector (b). Generate the output The vectors (al, bl and (d are given as: 3 2 1 b-[00 s0 2s0 Hint: a will be a column vector and (bl) will be a row vector Name the function as: Team HW4AB P3 Copy the code in function file as well as the inputs created, and outputs generated in the command window in the Word document under the heading Part Clearly indicate the function and the inputs as well as the outputs from the command window.

Explanation / Answer

% Gauss-Seidel Method in MATLAB

function x = gauss_siedel( A ,B )

disp ( 'Enter the system of linear equations in the form of AX=B')

%Inputting matrix A

A = input ( 'Enter matrix A :   ')

% check if the entered matrix is a square matrix

[na , ma ] = size (A);

if na ~= ma

    disp('ERROR: Matrix A must be a square matrix')

    return

end

% Inputting matrix B

B = input ( 'Enter matrix B :   ')

% check if B is a column matrix

[nb , mb ] = size (B);

if nb ~= na || mb~=1

   disp( 'ERROR: Matrix B must be a column matrix')

   return

end

% Separation of matrix A into lower triangular and upper triangular matrices

% A = D + L + U

D = diag(diag(A));

L = tril(A)- D;

U = triu(A)- D

% check for convergence condition for Gauss-Seidel method

e= max(eig(-inv(D+L)*(U)));

if abs(e) >= 1

    disp ('Since the modulus of the largest Eigen value of iterative matrix is not less than 1')

    disp ('this process is not convergent.')

    return

end

% initial guess for X..?

% default guess is [ 1 1 .... 1]

r = input ( 'Any initial guess for X? (y/n):   ','s');

switch r

    case 'y'

        % asking for initial guess

    X0 = input('Enter initial guess for X : ')

        % check for initial guess

    [nx, mx] = size(X0);

        if nx ~= na || mx ~= 1

        disp( 'ERROR: Check input')

        return

    end

    otherwise

    X0 = ones(na,1);

end

% allowable error in final answer

t = input ( 'Enter the error allowed in final answer:  ');

tol = t*ones(na,1);

k= 1;

X( : , 1 ) = X0;

err= 1000000000*rand(na,1);% initial error assumption for looping

while sum(abs(err) >= tol) ~= zeros(na,1)

    X ( : ,k+ 1 ) = -inv(D+L)*(U)*X( : ,k) + inv(D+L)*B;% Gauss-Seidel formula

    err = X( :,k+1) - X( :, k);% finding error

    k = k + 1;

    

end

fprintf ('The final answer obtained after %g iterations is   ', k)

X( : ,k)