简体   繁体   中英

Matlab: Solve for a single variable in a linear system of equations

I have a linear system of about 2000 sparse equations in Matlab. For my final result, I only really need the value of one of the variables: the other values are irrelevant. While there is no real problem in simply solving the equations and extracting the correct variable, I was wondering whether there was a faster way or Matlab command. For example, as soon as the required variable is calculated, the program could in principle stop running.

Is there anyone who knows whether this is at all possible, or if it would just be easier to keep solving the entire system?

Most of the computation time is spent inverting the matrix, if we can find a way to avoid completely inverting the matrix then we may be able to improve the computation time. Lets assume I'm only interested in the solution for the last variable x(N) . Using the standard method we compute

x = A\b;
res = x(N);

Assuming A is full rank, we can instead use LU decomposition of the augmented matrix [A b] to get x(N) which looks like this

[~,U] = lu([A b]);
res = U(end,end-1)/U(end,end);

This is essentially performing Gaussian elimination and then solving for x(N) using back-substitution.

We can extend this to find any value of x by swapping the columns of A before LU decomposition,

x_index = 123;    % the index of the solution we are interested in
A(:,[x_index,end]) = A(:,[end,x_index]);
[~,U] = lu([A b]);
res = U(end,end)/U(end,end-1);

Bench-marking performance in MATLAB2017a with 10,000 random 200 dimensional systems we get a slight speed-up

Total time direct method : 4.5401s
Total time LU method     : 3.9149s

Note that you may experience some precision issues if A isn't well conditioned.

Also, this approach doesn't take advantage of the sparsity of A . In my experiments even with 2000x2000 sparse matrices everything significantly slowed down and the LU method is significantly slower. That said full matrix representation only requires about 30MB which shouldn't be a problem on most computers.

If you have access to theory manuals on NASTRAN, I believe (from memory) there is coverage of partial solutions of linear systems. Also try looking for iterative or tri diagonal solvers for A*x = b. On this page , review the pqr solution answer by Shantachhani. Another reference .

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