0% found this document useful (0 votes)
2 views4 pages

NM-Lab 06

The document explains the Gauss elimination method for solving systems of linear equations using MATLAB. It outlines the procedure for implementing the method, including forming an augmented matrix, performing forward elimination, and back substitution. A final MATLAB function is provided for users to apply the method easily.

Uploaded by

Naeem Hanif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

NM-Lab 06

The document explains the Gauss elimination method for solving systems of linear equations using MATLAB. It outlines the procedure for implementing the method, including forming an augmented matrix, performing forward elimination, and back substitution. A final MATLAB function is provided for users to apply the method easily.

Uploaded by

Naeem Hanif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab# 06

Guass Elimination Method on


MATLAB
Introduction
Gauss elimination method is used to solve a system of linear equations. Let’s recall the definition of
these systems of equations. A system of linear equations is a group of linear equations with various
unknown factors. As we know, unknown factors exist in multiple equations. Solving a system involves
finding the value for the unknown factors to verify all the equations that make up the system.

In mathematics, the Gaussian elimination method is known as the row reduction algorithm for solving
linear equations systems. It consists of a sequence of operations performed on the corresponding matrix
of coefficients. We can also use this method to estimate either of the following:

 The rank of the given matrix


 The determinant of a square matrix
 The inverse of an invertible matrix

Procedure
Procedure to Write Gaussian Elimination Method in MATLAB

Step 1: Start a New Script or Function

You can either write a script or define a function. Let’s write a function so you can reuse it.

function x = gauss_elimination(A, b)

Step 2: Form the Augmented Matrix

Combine matrix A and vector b.

n = length(b);

Ab = [A b]; % Augmented matrix

Step 3: Forward Elimination (Convert to Upper Triangular Form)

Loop through rows and eliminate variables below the pivot.

for i = 1:n-1

% Partial pivoting (optional but recommended)

[~, maxRow] = max(abs(Ab(i:n,i)));

maxRow = maxRow + i - 1;
if maxRow ~= i

Ab([i, maxRow], :) = Ab([maxRow, i], :); % Swap rows

end

for j = i+1:n

factor = Ab(j,i) / Ab(i,i);

Ab(j,:) = Ab(j,:) - factor * Ab(i,:);

end

end

Step 4: Back Substitution

Solve the upper triangular system.

x = zeros(n,1);

x(n) = Ab(n,end) / Ab(n,n);

for i = n-1:-1:1

x(i) = (Ab(i,end) - Ab(i,i+1:n) * x(i+1:n)) / Ab(i,i);

end

Step 5: End the Function

end

Final MATLAB Function: gauss_elimination.m


How to Use It

In the command window or another script:

A = [2, -1, 1; 3, 3, 9; 3, 3, 5];

b = [2; -1; 4];

You might also like