1 Write a function named namegetLUthat takes as input any si
1. Write a function named name_get_LU()that takes (as input) any size square matrix and returns (as outputs) the matrices L and U from its LU decomposition in the conventional format of Eq. (1). To do this, you must follow the same methodology used for Crout’s method shown are equivalent to equations (4.34–38) of Ref. [1], but using the conventional format for L and U shown in Eq. (1). You must clearly show your work in your paper copy of the project including the re-derivation of the general equations— starting from Eq. (1)—that will be used later in your function to obtain the L and U matrices. However, using Crout’s method (and also your method, which is derived from Crout’s method), the LU decomposition can only be obtained for square matrices, so if the input matrix is rectangular, your function must throw an error (see 4). Furthermore, Crout’s method (and yours) unfortunately cannot handle all singular matrices. Therefore, after testing for “rectangularity,” your function must test if the input matrix is singular using MATLAB’s built-in det() function. If the input matrix is singular, your function must throw an error using the built-in MATLAB function error() with the message \'Matrix is singular; the LU decomposition cannot be computed by this function.\' (Note: MATLAB’s lu() function is able to handle rectangular and singular matrices.)
2. Write a function name_get_det_LU() that takes (as input) any size square matrix and returns (as output) its determinant. Your function must find the determinant of the input matrix based on the LU factorization returned by your name_get_LU() function. Do not use MATLAB’s built-in det() function anywhere in this function.
3. Write a function named name_get_soln_Cramer()that finds the solution of a linear system of equations Ax = b using Cramer’s rule, which takes (as inputs) a square matrix A of size n and a column vector b of size n×1 and returns (as output) the solution vector x of size n×1. To do this, your function must call your function name_get_det_LU() to obtain all of the necessary determinants.
4. Each of your functions must throw an error if the matrix entered is rectangular (i.e. not square). You must use the built-in MATLAB function error() to throw the error and display an informative error message. Specifically, the third function name_get_soln_Cramer() must display the error message \'System has no solution or an infinite number of solutions; this function cannot be used to solve this system.\' if the input matrix is rectangular. Important: Once an error is thrown and an error message is displayed, your functions are required to halt execution and not to throw any further errors (this should happen automatically when you use the built-in MATLAB function error()).
5. You are required to test your first two functions name_get_LU() and name_get_det_LU() by comparing the results of your functions with those obtained using the MATLAB built in functions lu()and det()using the coefficient matrices of the five linear systems of equations shown below. You must also verify your results from name_get_soln_Cramer()using MATLAB’s “backslash” division
A procedure for determining the elements of the matrices [L] and [U] can be written by following the calculations. If [a] is an (n x n) matrix, the elements of [L] and [U] are given by:
Step 1: Calculating the first column of [L]:
for i= 1,2,...,n Li1= ai1 (4.34)
Step 2: Substituting 1s in the diagonal of [U]:
for i= 1,2,...,n Uii=1 (4.35)
Step 3: Calculating the elements in the first row of [U] (except U11 which was already calculated):
for j= 2,3,...,n U1j= a1j /L11 (4.36)
Step 4: Calculating the rest of the elements row after row (i is the row number and j is the column number). The elements of [L] are calculated first because they are used for calculating the elements of [U]:
for i= 2,3,...,n
11 12 13 14 a21 a22 a23 a24 a31 a32 a33 a34 a41 a42 a43 a44Solution
PART I:
function name_get_LU() = LUdecompCrout(X)
% This funct. decomposes the matrix X into a lower triangular matrix L
% and an upper triangular matrix U, using Crout\'s method such that X=LU.
% I/p variables:
% A The matrix of coefficients.
% O/p variable:
% L Lower triangular matrix.
% U Upper triangular matrix.
[R, C] = size(X);
for i = 1:R L(i,1) = X(i,1); U(i,i) = 1;
end
for j = 2:R U(1,j)= X(1,j)/L(1,1);
end
for i = 2:R for j = 2:i L(i,j)=X(i,j)-L(i,1:j-1)*U(1:j-1,j); end for j = i+1:R U(i,j)=(X(i,j)-L(i,1:i-1)*U(1:i-1,j))/L(i,i); end
end
PART II:
B = [ 1 2 3
4 5 6
7 8 0 ];
To calculate the LU factorization of the above matrix,we need to to call lu with two output arguments.
[L1,U] = lu(B)
L1 =
0.1429 1 0
0.5714 0.5 1
1 0 0
U =
7 8 0
0 0.8571 3
0 0 4.5
L1 is a permutation of a lower triangular matrix,To check now if the factorization is working, compute the product of:
L1*U
which returns the original B.
The inverse of the example matrix, X = inv(B), is actually computed from the inverses of the triangular factors
X = inv(U)*inv(L1)
Using three arguments on the left side to get the permutation matrix as well,
[L2,U,P] = lu(B)
returns a truly lower triangular L2, the same value of U, and the permutation matrix P.
L2 =
1 0 0
0.1429 1 0
0.5714 0.5 1
U =
7 8 0
0 0.8571 3
0 0 4.5
P =
0 0 1
1 0 0
0 1 0
Note that L2 = P*L1.
P*L1
answer =
1.0000 0 0
0.1429 1.0000 0
0.5714 0.5000 1.0000
To verify that L2*U is a permuted version of B, compute L2*U and subtract it from P*B:
P*B - L2*U
answer =
0 0 0
0 0 0
0 0 0
In this case, inv(U)*inv(L) results in the permutation of inv(B) given by inv(P)*inv(B).
The determinant of the example matrix is
d = det(B)
d = 27
It is computed from the determinants of the triangular factors
d = det(L)*det(U)
The solution to Bx = b is obtained with matrix division
x = B\\b
The solution is actually computed by solving two triangular systems
y = L\\b
x = U\\y
PART III:
function x = cramer(R,b)
% x = CRAMER(R,b) solves the square system R*x = b.
if det(R) == 0
error(\'Matrix is singular\')
end
[n,n] = size(R);
for j = 1:n
S = R;
S(:,j) = b;
x(j) = det(S)/det(R);
end
x = x\';


