Question

In: Computer Science

Write a MATLAB function function = myMatrixInveesion(....) to calculate matrix inversion by implementing LU decomposition, forward...

Write a MATLAB function function = myMatrixInveesion(....) to calculate matrix inversion by implementing LU decomposition, forward and backward substitution procedures. Do NOT use the built-in "lu" or "inv" commands in your code. You will need to employ Nested Loops.

Thank you!

function_____ = myMatrixInversion(_____)

Solutions

Expert Solution

1. First method with  employ nested loop:

function Ainv=MatrixInverse(A)
% A=rand(7,7); 
%
% Ainv1=inv(A);
% Ainv2=MatrixInverse(A);
%
% Ainv1-Ainv2
%
% Do Lu decompositon to obtain triangle matrices (can easily be inverted)
[L,U,P] = Lu(A);
% Solve 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)=TriangleBackwardSub(U,TriangleForwardSub(L,P*b));
end

2. Second Method with 'Lu' and 'inv' commond

function [L,U,P] = Lu(A)
%  LU factorization.
%
%   
% [L,U,P] = Lu(A) returns unit lower triangular matrix L, upper
% triangular matrix U, and permutation matrix P so that P*A = L*U.
%
% example,
%   A=rand(9,9);
%
%   [L,U,P] = Lu(A);
%   sum(sum(abs(P*A- L*U)))
%   
%   [L,U,P] = lu(A);
%   sum(sum(abs(P*A- L*U)))
%
%
s=length(A);
U=A;
L=zeros(s,s);
PV=(0:s-1)';
for j=1:s,
    % Pivot Voting (Max value in this column first)
    [~,ind]=max(abs(U(j:s,j)));
    ind=ind+(j-1);
    t=PV(j); PV(j)=PV(ind); PV(ind)=t;
    t=L(j,1:j-1); L(j,1:j-1)=L(ind,1:j-1); L(ind,1:j-1)=t;
    t=U(j,j:end); U(j,j:end)=U(ind,j:end); U(ind,j:end)=t;
    % 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;

as you wish!!


Related Solutions

Matlab You will write a function to calculate the determinant of a matrix. It should work...
Matlab You will write a function to calculate the determinant of a matrix. It should work for any size matrix. Remember that the determinant can be calculated by multiplying the diagonal elements of an upper right triangular matrix. Your function will take a matrix passed to it and put it in upper right triangular form. You will work down the diagonal beginning at row 1 column 1, then row 2 column 2, etc. Note that the row and column numbers...
Write a Matlab function for a matrix that takes in a matrix in echelon form and...
Write a Matlab function for a matrix that takes in a matrix in echelon form and will return the row canonical form. The function cannot use rref, or any other matlab built in functions.
IN MATLAB, Create a 8x8 magic matrix. Then use MATLAB built in function, sum, to calculate...
IN MATLAB, Create a 8x8 magic matrix. Then use MATLAB built in function, sum, to calculate the sum of each column. Use subscript notation to find the 6th element and the last element of each magic square. Find the maximum of all the elements in each magic square.
Given an n × n matrix A, write a function that carries out LU factoriza- tion...
Given an n × n matrix A, write a function that carries out LU factoriza- tion without row pivoting. Note that this will be different from the built-in Matlab function lu, which does do row pivoting. (Using MATLAB)
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or...
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or any vector and simply returns back the transpose of that input. You can always verify your answer by using the inbuilt transpose function in MATLAB, however, you cannot use the transpose function directly when writing your code. Here is the starter code that we are providing to help you get started %x is the input vector or matrix. You can find the %size that...
MATLAB Function: (Backward substitution) Write a function that takes an upper triangular matrix U, and a...
MATLAB Function: (Backward substitution) Write a function that takes an upper triangular matrix U, and a vector b as input and returns the solution of the linear system U x = b. Use this function in the case when U is made of the numbers [(3,2,1),(0,5,4),(0,0,6)], and b = [1,1,−6]^T.
B. Write a function in MATLAB called findInverseOf2x2Matrix that takes in as input any square matrix...
B. Write a function in MATLAB called findInverseOf2x2Matrix that takes in as input any square matrix of size 2 × 2 and returns back the inverse of that matrix if one exists. If no inverse exists, print back a suitable error message. You can safely assume that we will test your code on square matrices of size 2 × 2 only. You can always verify your answer by using the inbuilt inverse function in MATLAB, however, you cannot use the...
Using Matlab Write a function, called digits_function that is able to calculate the number of digits...
Using Matlab Write a function, called digits_function that is able to calculate the number of digits and the multiplication of the digits. The input of this function is N (entered number) and the outputs are number_digits and sum_digits.
Normal distribution and discriminant functions Matlab - Write a procedure to calculate the log discriminant function...
Normal distribution and discriminant functions Matlab - Write a procedure to calculate the log discriminant function for a given multi-variate Gaussian distribution and prior probability.
(MATLAB) Write a script that would create a 3x5 matrix of random integers and write this...
(MATLAB) Write a script that would create a 3x5 matrix of random integers and write this new matrix to a file called “Assignment3_Question5.dat”.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT