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 ?

URL87
  • 10,667
  • 35
  • 107
  • 174
  • Are your vectors and matrices symbolic? If so, you need to detail this in your question. – horchler May 29 '13 at 00:26
  • Also, `mldivide` is [overloaded for symbolic systems](http://www.mathworks.com/help/symbolic/arithmeticoperations.html), but won't work exactly the same. – horchler May 29 '13 at 00:30
  • What is mean "matrices symbolic" ? – URL87 May 29 '13 at 00:32
  • `linalg::matlinsolveLU` is in the [Symbolic Toolbox](http://www.mathworks.com/help/symbolic/index.html). 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. – horchler May 29 '13 at 00:37
  • @URL87: I think you are confusing numeric solutions to symbolic ones... – Amro May 29 '13 at 00:39

2 Answers2

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)

Amro
  • 123,847
  • 25
  • 243
  • 454
  • 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 up – Amro May 29 '13 at 00:25
  • How you know that it "smart enough to detect ..." ? – URL87 May 29 '13 at 00:29
  • 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.. – Amro May 29 '13 at 00:31
  • here are other questions about `mldivide` that you might find interesting: [How does the MATLAB backslash operator solve Ax=b for square matrices?](http://scicomp.stackexchange.com/q/1001/386), [in matlab, what differences are between linsolve and mldivide?](http://scicomp.stackexchange.com/q/5603/386) – Amro May 29 '13 at 00:38
  • Also worth mentioning: [How does the backslash operator work when A is full?](http://www.mathworks.com/support/solutions/en/data/1-172BD/index.html?product=ML&solution=1-172BD) – Amro May 29 '13 at 00:44
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.

horchler
  • 18,384
  • 4
  • 37
  • 73