1. Naive Gauss Elimination Starting from the back-substitution function written
ID: 1721101 • Letter: 1
Question
1. Naive Gauss Elimination Starting from the back-substitution function written in class, complete a naive Gauss Elimination solver by adding the forward-elimination component. Use this to solve the matrix system A x = b presented in class, namely,
(2 3 -1) (x1) ( 3 ) (1 -2 1) (x2) = ( 1 ) (1 -1 1) (x3) ( 1 )
Have your code return the solution x and show this solution in a command prompt printout. Note that you can, of course, check your answer easily with the Matlab backslash command, A. 2. Naive LU Decomposition Now, starting with your functioning Gauss Elimination code, modify it to keep the factors in the same matrix that it is passed. You of course will not modify the b-vector. Have this function return the L and U matrices in a single matrix. You do not need to create separate L and U matrices. Note that Matlab's lu() will also do this, but may perform row swapping (so you need to be careful with direct comparisons). Decompose the given 3x3 A matrix (above), and confirm your correct decomposition by computing LU = A. Provide a command prompt printout. Then write a solver function and use the LU solution technique to solve the (above) 3x3 system. Again, show this work with a printout of the command prompt window. Specifically, the technique first solves L d = b for d by back substitution, then U x = d for x by forward substitution.
Explanation / Answer
code for naive guassion elimination
% Naive Gauss Elimination
clear
A=input('Enter Matrix A > ')
Ao=A; % Save Original Coefficient Matrix
B=input('Enter Solution Vector B > ')
Bo=B; % Save Original Solution Vector
n=size(A,1);
% Forward Elimination
for k=1:n-1
for i=k+1:n
factor=A(i,k)/A(k,k);
for j=k+1:n
A(i,j)=A(i,j)-factor*A(k,j);
end
B(i)=B(i)-factor*B(k);
end
end
% Back Substitution
X(n)=B(n)/A(n,n);
for i=n-1:-1:1
sum=0;
for j=i+1:n
sum=sum+A(i,j)*X(j);
end
X(i)=(B(i)-sum)/A(i,i);
end
S=X.' % The .' prevents the elements from also being converted to complex conjugates if they are complex!
% Check Solution Using MATLAB Gauss Elimination
%Sc=AoBo % Solves Equation Set using MATLAB Gauss Elimination
%Diff=S-Sc
% Naive Gauss Elimination and LU Decomposition
clear
A=input('Enter Matrix A > ')
Ao=A; % Save Original Coefficient Matrix
B=input('Enter Solution Vector B > ')
Bo=B; % Save Original Solution Vector
n=size(A,1);
L=eye(n,n); % Initialize L Matrix to Ones along the Diagonal
% Forward Elimination
for k=1:n-1
for i=k+1:n
factor=A(i,k)/A(k,k);
L(i,k)=factor; % Save the Pivot Factors as L Matrix Elements
A(i,k)=0; % Set the Lower Triangle Elements to Zero
for j=k+1:n
A(i,j)=A(i,j)-factor*A(k,j);
end
B(i)=B(i)-factor*B(k);
end
end
% Back Substitution
X(n)=B(n)/A(n,n);
for i=n-1:-1:1
sum=0;
for j=i+1:n
sum=sum+A(i,j)*X(j);
end
X(i)=(B(i)-sum)/A(i,i);
end
S=X.' % The .' prevents the elements from also being converted to complex conjugates if they are complex!
% Check Solution
Bc=Ao*S
% Identification of L and U Matrices
L=L
U=A
% Check Matrix Product
Ac=L*U
% Check LU Decomposition
[Lc,Uc]=lu(Ao)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.