Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using MATLAB, develop an M-file to determine matrix inverse based on the LU fact

ID: 3184717 • Letter: U

Question

Using MATLAB, develop an M-file to determine matrix inverse based on the LU factorization method above. That is, develop a function called myinv that is passed the square matrix [A] and utilizing codes of part 1 above to return the inversed matrix. You are not to use MATLAB built-in function inv or left-division I in your codes. Test your function by using it to solve a system of equations listed below in part 3. Confirm that your function is working properly by verifying that [AJIA] 1-1 and by using the MATLAB built-in function inv.

Explanation / Answer

Here I'm providing you with 4 scripts. Script 1 calls script 2,3 and 4 in order to calculate the inverse Script 2 performs LU decomposition Script 3 performs backward substitution on U Script 4 performs forward substitution on L You can save the 4 scripts with their names and execute them to get the result SCRIPT 1: function Ainv=myinv(A) % We are getting triangular matrices from Lu decompositon(Script 2) [L,U,P] = LUDecomposition(A); % We are solving the linear system for Identity matrix I=eye(size(A)); s=size(A,1); Ainv=zeros(size(A)); for i=1:s b=I(:,i); Ainv(:,i)=BackSub(U,ForSub(L,P*b)); end SCRIPT 2: %function for calculating L and U matrices function [L,U,P] = LUDecomposition(A) function [L,U,P] = LUDecomposition(A) s=length(A); U=A; L=zeros(s,s); PV=(0:s-1)'; for j=1:s, % Pivoting (Max value in this column first) [~,index]=max(abs(U(j:s,j))); index=index+(j-1); t=PV(j); PV(j)=PV(index); PV(index)=t; t=L(j,1:j-1); L(j,1:j-1)=L(index,1:j-1); L(index,1:j-1)=t; t=U(j,j:end); U(j,j:end)=U(index,j:end); U(index,j:end)=t; % calculating LU L(j,j)=1; for i=(1+j):size(U,1) c= U(i,j)/U(j,j); U(i,j:s)=U(i,j:s)-U(j,j:s)*c; L(i,j)=c; end end P=zeros(s,s); P(PV(:)*s+(1:s)')=1; SCRIPT 3: function C=BackSub(U,b) % Solving C = U B; s=length(b); C=zeros(s,1); C(s)=b(s)/U(s,s); for j=(s-1):-1:1 C(j)=(b(j) -sum(U(j,j+1:end)'.*C(j+1:end)))/U(j,j); end SCRIPT 4: function C=ForSub(L,b) % Solving C = L b s=length(b); C=zeros(s,1); C(1)=b(1)/L(1,1); for j=2:s C(j)=(b(j) -sum(L(j,1:j-1)'.*C(1:j-1)))/L(j,j); end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote