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: 1843818 • Letter: W

Question

Write a user-defined MATLAB function that solves the system of linear equations of Ax = b using the iterative Guass - Seidel method. For function name and arguments, use x = GaussSeidel(A,b) The input arguments are the coefficient matrix (A) and right hand side vector (b). The function should first check if A is a square matrix and A and b 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:

estimated relative error for all unknowns becomes less than 0.001

number of iterations reaches 1000

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)

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