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

Prepare a MATLAB function that implements the Gauss elimination procedure for so

ID: 3551307 • Letter: P

Question

Prepare a MATLAB function that implements the Gauss elimination procedure for solving a system of linear equations with square matrix of coefficients n Time n. For forward sweep, use the code from the file GaussElimForwartSweep.m considered in the class. Modify the algorithm of forward sweep by adding the algorithm of swapping equations. For back substitution, use the code developed in homework 5. Using the developed code, solve the linear system AX = B, where Check the solution with the MATLAB calculating X = inv ( A ) * B.

Explanation / Answer

clc;
close all;
clear;

a=[1 -1 2 4;2 -2 0 -1;-2 0 -1 5;7 1 -1 2];
B=[0;1;-1;2];
X=inv(a)*B;

X1=naiv_gauss(a,B);

-----------------------------------------------------

%NOw Function(save it in another file named nav_gauss

function x = naiv_gauss(A,b);
    n = length(b); x = zeros(n,1);
    for k=1:n-1 % forward elimination
    for i=k+1:n
    xmult = A(i,k)/(0.0001+A(k,k));
    for j=k+1:n
    A(i,j) = A(i,j)-xmult*A(k,j);
    end
    b(i) = b(i)-xmult*b(k);
    end
    end
    % back substitution
    x(n) = b(n)/(0.0001+A(n,n));
    for i=n-1:-1:1
    sum = b(i);
    for j=i+1:n
    sum = sum-A(i,j)*x(j);
    end
    x(i) = sum/A(i,i);
end