Matlab Code Help Starting from the GaussE forward elimination function written i
ID: 1720967 • Letter: M
Question
Matlab Code Help
Starting from the GaussE forward elimination function written in class, complete a naive Gauss Elimination solver by adding the back-substitution 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) Please forgive this HTML editor's inability to well-represent a matrix system. 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. 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 code return the L and U matrices in a single matrix. 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 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. Insure proper functioning of both your codes by solving the following system (again, you can easily find the correct answer using Matlab's A command): Provide a command prompt printout of this solution.Explanation / Answer
matlab code
function x = naiv_gauss(A,b);
n = length(b); x = zeros(n,1);
for k=1:n-1 % forward elimination
for i=k+1:n
xmult = A(i,k)/A(k,k);
for j=k+1:n
A(i,j) = A(i,j)-xmult*A(k,j);
end
b(i) = b(i)-xmult*b(k);
end
end
% back substitution
x(n) = b(n)/A(n,n);
for i=n-1:-1:1
sum = b(i);
for j=i+1:n
sum = sum-A(i,j)*x(j);
end
x(i) = sum/A(i,i);
end