Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the code to compute inverse of lower triangular matrix. How to compute inverse of upper triangular matrix from below code by changing it a little bit?

function L = L_inv(A)
            [n,n] = size(A);
            L = zeros(n); 
            for i=1:n
                L(i,i) = 1/A(i,i);
                for j=i+1:n
                    L(j,i)=-A(j, i:j-1)*L(i:j-1,i)/A(j,j);
                end
            end
        end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
758 views
Welcome To Ask or Share your Answers For Others

1 Answer

The inverse of the transpose of a matrix is equal to the transpose of its inverse. So, since an upper triangular matrix is just the transpose of a lower triangular matrix, to find the inverse of an upper triangular matrix you just need to apply your function to its transpose and then transpose again the result.

n = 10; % dimensions of your matrix
U = triu(rand(n, n)); % create a random upper triangular matrix
Uinv1 = L_inv(U')'; % using your function
Uinv2 = inv(U); % using the built-in function
Uinv1 - Uinv2

Printing the difference between the two results you can see that they are (almost) exactly the same. For example in my case norm(Uinv1 - Uinv2) = 5.642266075279820e-15.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...