简体   繁体   中英

Solve the sparse triangular linear system in Matlab

I implement the LU decomposition algorithm in Matlab for some large sparse Matrices to solve the linear system. When I got the L,U matrix, I used the backward substitution and forward substitution algorithm to solve the triangular linear system:

    %x = U\y;

for i = n : -1 : 1
    x(i,:) = (y(i,:)-U(i,:)*x)/U(i,i);
end

but I found this code is the bottleneck. Although I can use the A\\b to get the solution, but I want to know how can I implement a efficient algorithm to solve this problem in Matlab, For example, Can I write the matrix product to simulate the following action without for loop?

(I got some reference books and paper, but all of the code is not in Matlab, just for C++ or C code)

First off: correctness goes before speed ; the loop you posted produces results different from U\\y , so you might want to check that first :)

AFAIK, the backslash does some checks on the input matrix, and calls the fastest algorithm accordingly. When those checks indicate A is lower triangular, it will do exactly what you did (but then probably more efficient).

Anyway, to speed up your code: you should pre-allocate x , otherwise Matlab is forced to grow the vector at each iteration. Also, call your loop variable ii -- i is the imaginary unit, and the name resolution at each iteration takes some time. So, in summary:

x = zeros(size(y));
for ii = n : -1 : 1
    x(ii,:) = (y(ii,:)-U(ii,:)*x)/U(ii,ii);
end

Note that there is no 'vectorized' solution, as the next result depends on the previous one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM