Aim: To solve a system of linear equations using the Gauss Elimination Method, and display the graph of the solution.
Objective: The Gauss Elimination Method is an iterative numerical method used for solving systems of linear equations. The objective of this method is to reduce the system of linear equations to an upper triangular matrix which can be easily solved by back substitution. The objective of displaying the graph of the solution is to visualize the intersection of the planes represented by the equations in the system.
Algorithm:
- Construct the coefficient matrix A and the constant vector b of the system of linear equations Ax = b.
- Augment the coefficient matrix A with the vector b.
- For each column of augmented matrix:
a. Select the pivot element as the absolute maximum value in the column.
b. Swap the rows to move the pivot element to the diagonal position.
c. Subtract the multiples of the pivot row from rows below it to eliminate the elements below the pivot element.
- The augmented matrix is now transformed to upper triangular form. Solve the system using back substitution starting from the last row.
- Create a grid of x, y, and z values in the desired range.
- Substitute the x, y, and z values into the equations of the system and plot the resulting planes.
- Plot the intersection of the planes to display the solution.
Matlab Code:
Let us consider a system of linear equations with three variables x, y, and z as follows:
2x + y + z = 5
4x - 6y + 0z = -2
-2x + 7y + 2z = 9
% Coefficient Matrix (A)
A = [2 1 1; 4 -6 0; -2 7 2];
% Constant Vector (b)
b = [5; -2; 9];
% Augmented Matrix (Ab)
Ab = [A b];
% Gaussian Elimination
n = size(Ab, 1); % Size of matrix
for i = 1:n-1
[~, j] = max(abs(Ab(i:n, i))); % Find pivot element
j = j + i - 1; % Adjust index
if j ~= i
Ab([i j], :) = Ab([j i], :); % Swap rows
end
for k = i+1:n
Ab(k, :) = Ab(k, :) - Ab(i, :)*(Ab(k, i)/Ab(i, i)); % Eliminate elements below pivot
end
end
% Solution
x = zeros(n, 1);
for i = n:-1:1
x(i) = (Ab(i, end) - Ab(i, i+1:n)*x(i+1:n))/Ab(i, i); % Solve using back substitution
end
% Display results
fprintf('Gauss Elimination Method\n')
fprintf('Solution: x = %f, y = %f, z = %f\n\n', x(1), x(2), x(3))
% Plotting the graph
[X,Y] = meshgrid(-5:0.1:5,-5:0.1:5); % Create grid for x, y, and z
Z1 = (5 - 2X - Y); % Calculate plane for first equation
Z2 = (-2 - 4X + 6Y)/0; % Calculate plane for second equation
Z3 = (9 + 2X - 7*Y)/2; % Calculate plane for third equation
figure
hold on
surf(X,Y,Z1,'FaceAlpha',0.5) % Plot first plane
surf(X,Y,Z2,'FaceAlpha',0.5) % Plot second plane
surf(X,Y,Z3,'FaceAlpha',0.5) % Plot third plane
view([45 30]) % Set view angle
plot3(x(1),x(2),x(3),'r.','MarkerSize',20) % Plot solution point
xlabel('X') % Label X axis
ylabel('Y') % Label Y axis
zlabel('Z') % Label Z axis
title('Intersection of Planes') % Set plot title
legend('Equation 1','Equation 2','Equation 3','Solution Point') % Set legend
Interpretation:
The Gauss Elimination Method gave the solution x = 2, y = 1, and z = 3 for the system of linear equations. The graph displays the planes represented by the equations, intersecting at the solution point, represented by the red dot. This visualization helps to understand the geometrical interpretation of the solution to the system of linear equations.
Thus, the Gauss Elimination Method is a powerful numerical method for solving systems of linear equations, and visualizing the solution helps in understanding the geometrical interpretation of the solution.