Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 733
URL87 Avatar asked May 28 '13 23:05

URL87


2 Answers

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)

like image 118
Amro Avatar answered Oct 24 '22 07:10

Amro


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.

like image 22
horchler Avatar answered Oct 24 '22 05:10

horchler