Solve linear equation system by given LU decomposition and vector of constants
Asked Answered
A

2

6

Given L and U LU decomposition and vector of constants b such that LU*x=b , is there any built in function which find the x ? Mean something like -

X = functionName(L,U,b) 

Note that in both L and U we are dealing with triangular matrices which can be solved directly by forward and backward substitution without using the Gaussian elimination process.

Edit :

Solving this linear equation system should be according to the following steps -

1. define y - s.t Ux=y
2. solve Ly=b by forward substitution
3. solve Ux=y by backward substitution
4. return y

Edit 2 :

I found linalg::matlinsolveLU but I didn't try it cause I have too old version (R2010a) . Is it working for anyone ?

Agitato answered 28/5, 2013 at 23:20 Comment(5)
Are your vectors and matrices symbolic? If so, you need to detail this in your question.Fragile
Also, mldivide is overloaded for symbolic systems, but won't work exactly the same.Fragile
What is mean "matrices symbolic" ?Agitato
linalg::matlinsolveLU is in the Symbolic Toolbox. if your L and U are floating point values, as opposed to having been creates via sym or syms then linalg::matlinsolveLU wouldn't work well for you anyways.Fragile
@URL87: I think you are confusing numeric solutions to symbolic ones...Mace
M
6

If you have:

A = rand(3);
b = rand(3,1);

then the solution to the system can be simply computed as:

x = A\b

Or if you already have an LU decomposition of A, then:

[L,U] = lu(A);
xx = U\(L\b)

the mldivide function is smart enough to detect that the matrix is triangular and chose an algorithm accordingly (forward/backward substitution)

Mace answered 29/5, 2013 at 0:17 Comment(5)
this kind of thing is useful if you have a fixed coefficient matrix A but many different right hand sides b. So by precomputing the decomposition, you might speed things upMace
How you know that it "smart enough to detect ..." ?Agitato
I gave a link to the docs that explains how mldivide implements different algorithms and chooses the appropriate one based on the characteristics of the input matrix..Mace
here are other questions about mldivide that you might find interesting: How does the MATLAB backslash operator solve Ax=b for square matrices?, in matlab, what differences are between linsolve and mldivide?Mace
Also worth mentioning: How does the backslash operator work when A is full?Mace
F
3

I think this is what you're looking for:

A = rand(3,3); % Random 3-by-3 matrix
b = rand(3,1); % Random 3-by-1 vector
[L,U] = lu(A); % LU decomposition
x = U\(L\b)    % Solve system of equations via mldivide (same as x = A\b or x = (L*U)\b)
err = L*U*x-b  % Numerical error

The system of equations is solved using mldivide. You might also look at qr which implements QR decomposition instead of using LU decomposition. qr can directly solve A*x = b type problems and is more efficient. Also look at linsolve. For symbolic systems you may still be able to use mldivide, or try linalg::matlinsolveLU in MuPAD.

Fragile answered 28/5, 2013 at 23:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.