Please use Matlab. I need the code and the output. This is question 1. The Gauss
ID: 2966731 • Letter: P
Question
Please use Matlab. I need the code and the output. This is question 1.
The Gauss-Jordan Method is similar to the Gaussian Elimination method described in class and in the text. At the ith stage we use the ith equation to eliminate the Xi term from the equations Ek = i + 1 , n (below the pivot as in Gaussian Elimination) and also from Ek, k = 1,... ,i - 1 (above the pivot). The augmented matrix will be reduced to the following form: Write a program that solves an n x n system of equations using Gauss-Jordan Elimination. The program should accept the augmented matrix as input and output either the solution or a message that the system has no unique solution. Apply the program to the system from question 1. 4x1 - x2 + X3 = 8 2x1 + 5x2 + 2x3 = 3 2x1 + 2x2 + 4x3 = 11 X1 + 2x2 + 4x3 =11 4x1 - x2 + X3 = 8 2x1 + 5x2 + 2x3 = 3Explanation / Answer
The following is the MATLAB code for solving the system of equations using Gauss-Jordan method.
% solves the system of equations
n=input('Enter the number of equations to solve:');
a=zeros(n,n);c=zeros(n,1);
a=input('Enter the coefficients of the equations in a matrix form:');
c=input('Enter the constants of the equations as a column vector:');
A=[a c]; % forms the augmented matrix
if rank(a)<n
fprintf('There is no unique solution ');
end
k=1;
for j=1:n-1
k=k+1;
for i=k:n
A(i,:)=A(i,:)-A(i,j)*A(j,:)/A(j,j);
end
end
k=n;
for j=n:-1:2
k=k-1;
for i=k:-1:1
A(i,:)=A(i,:)-A(i,j)*A(j,:)/A(j,j);
end
end
% creates the loops to make the diagonal coefficient matrix
for i=1:n
d(i)=A(i,n+1)/A(i,i);
end
% finds the solution of the system of equations
if rank(a)==n
fprintf('The solution of the system of equations is ')
d'
end
The following is the MATLAB output when the above MATLAB code is run:
Enter the number of equations to solve:3
Enter the coefficients of the equations in a matrix form:[4 -1 1;2 5 2;1 2 4]
Enter the constants of the equations as a column vector:[8 3 11]'
The solution of the system of equations is
ans =
1
-1
3