Using MATLAB, develop an M-file to determine matrix inverse based on the LU fact
ID: 1996315 • 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 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 [A] [A]^-1 = [I] and by using the MATLAB built-in function inv. x_1 + x_2 + 5x_3 = -21.5 -3x_1 - 6x_2 + 2x_3 = -61.5 10x_1 + 2x_2 - x_3 = 27Explanation / Answer
MATLAB Code given below
function Ainv=myinv(A)
[L,U,P] = Lu(A); % LU Facterization
% Solve for Identity matrix
I=eye(size(A));
s=size(A,1);
Ainv=zeros(size(A));
for i=1:s
b=I(:,i);
Ainv(:,i)=SubtractionB(U,SubtractionF(L,P*b));
end
end
function C=SubtractionB(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
function C=SubtractionF(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
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.