Write aim, objective, algorithm, Matlab code along with the code to display the graph,…
- User
Write aim, objective, algorithm, Matlab code along with the code to display the graph, interpretation for Gauss Elimination Method
- Assistant
Write aim, objective, algorithm, Matlab code along with the code to display the graph, interpretation for Gauss Elimination Method
Aim: The aim of the Gauss Elimination Method is to solve a system of linear equations using a systematic approach consisting of forward elimination and back substitution.
Objective: The objective of implementing the Gauss Elimination Method in MATLAB is to develop a clear understanding of the method for solving the linear equation systems and validate its correctness by testing on different sets of linear equations.
Algorithm:
MATLAB Code:
function gauss_elimination(A, B)
n = length(B);
AugmentedMatrix = [A, B];
% Forward Elimination
for k = 1:(n - 1)
% Partial Pivoting
[~, p] = max(abs(AugmentedMatrix(k:end, k)));
p = p + k - 1;
if p ~= k
AugmentedMatrix([k, p], :) = AugmentedMatrix([p, k], :);
end
% Elimination
for i = (k + 1):n
factor = AugmentedMatrix(i, k) / AugmentedMatrix(k, k);
AugmentedMatrix(i, :) = AugmentedMatrix(i, :) - factor * AugmentedMatrix(k, :);
end
end
% Back Substitution
x = zeros(n, 1);
for i = n:-1:1
x(i) = (AugmentedMatrix(i, end) - AugmentedMatrix(i, 1:n) * x) / AugmentedMatrix(i, i);
end
% Display the solutions
disp('The solutions are: ')
disp(x)
end
MATLAB Code to display the graph:
% Test matrix A and vector B
A = [10 -7 0; -3 2.099 6; 5 -1 5];
B = [7; 3.901; 6];
% Solve the system using Gauss Elimination
gauss_elimination(A, B);
% Plot the equations on a 3D graph
x1 = linspace(-10, 10, 100);
x2 = linspace(-10, 10, 100);
[X1, X2] = meshgrid(x1, x2);
X3 = (B(1) - A(1,1)*X1 - A(1,2)*X2) / A(1,3);
Y3 = (B(2) - A(2,1)*X1 - A(2,2)*X2) / A(2,3);
Z3 = (B(3) - A(3,1)*X1 - A(3,2)*X2) / A(3,3);
figure
surf(X1, X2, X3, 'FaceAlpha', 0.5)
hold on
surf(X1, X2, Y3, 'FaceAlpha', 0.5)
hold on
surf(X1, X2, Z3, 'FaceAlpha', 0.5)
xlabel('x1')
ylabel('x2')
zlabel('x3')
title('3D Plot of the Equations')
hold off
Interpretation: Here we present a MATLAB implementation of Gauss Elimination Method to solve a system of linear equations. The method consists of two main stages: forward elimination and back substitution. It can be used to solve any n*n system of linear equations by modifying the input matrix A and vector B.
The graph will display the intersection of three planes, which represents the solution of the system of equations. The Gauss Elimination Method algorithm calculates this intersection point, and the graph visually confirms the correctness of the computation.
Not yet included in a published release.