0% found this document useful (0 votes)
13 views

GaussJordonMethod

Matlab Code with Explaination and output of Gauss Jordon Method
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)
13 views

GaussJordonMethod

Matlab Code with Explaination and output of Gauss Jordon Method
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/ 3

MATLAB Code for Gauss-Jordan Method

The Gauss-Jordan method is an algorithm for solving systems of linear equations by converting
the matrix to its reduced row echelon form (RREF). Here’s the MATLAB implementation with
detailed explanations:

Code:
% Gauss-Jordan Method in MATLAB
% Define the augmented matrix [A|b]
augmented_matrix = [2, 1, -1, 8;
-3, -1, 2, -11;
-2, 1, 2, -3]; % Example system of equations
% Get the number of rows
[n, ~] = size(augmented_matrix);
% Perform Gauss-Jordan elimination
for i = 1:n
% Make the diagonal element 1
augmented_matrix(i, :) = augmented_matrix(i, :) / augmented_matrix(i, i);
% Make the other elements in the column 0
for j = 1:n
if i ~= j
factor = augmented_matrix(j, i);
augmented_matrix(j, :) = augmented_matrix(j, :) - factor * augmented_matrix(i, :);
end
end
end
% Extract the solution
solution = augmented_matrix(:, end);
% Display the results
disp('The solution of the system is:');
disp(solution);
Wokplace of the Code: Output of the Code:

Explanation of the Code


Define the Augmented Matrix:
augmented_matrix = [ ... ];

The augmented matrix [A|b] includes the coefficient matrix A and the right-hand side vector 𝑏
Size of the Matrix:
[n, ~] = size(augmented_matrix);
n is the number of rows (equations) in the system.
Gauss-Jordan Elimination:
for i = 1:n
augmented_matrix(i, :) = augmented_matrix(i, :) / augmented_matrix(i, i);
Divides each row by its diagonal element to make the diagonal element equal to 1.
for j = 1:n
if i ~= j
factor = augmented_matrix(j, i);
augmented_matrix(j, :) = augmented_matrix(j, :) - factor * augmented_matrix(i, :);
end
end
Eliminates all other elements in the column to make them 0.
Extract the Solution:
solution = augmented_matrix(:, end);
The last column of the reduced matrix contains the solution to the system.
Display the Results:
disp('The solution of the system is:');
disp(solution);
Outputs the solution vector.
Example Input
Equations:
2x + y - z = 8
-3x - y + 2z = -11
-2x + y + 2z = -3
Augmented Matrix:
[2, 1, -1, 8;
-3, -1, 2, -11;
-2, 1, 2, -3];

Output:

Steps in the elimination process:

1. Normalize the first row to make a11=1.


2. Eliminate the first column elements below and above a11.
3. Repeat the process for the second and third rows.

Final Matrix:

[1 0 0 2]
[0 1 0 3]
[0 0 1 -1]
Solution:
The solution of the system is:
2
3
-1

You might also like