Solving LU factorization's backward and forward Solve with for loops
13 views (last 30 days)
I am learning LU factorization using MATLAB. I am following up this strategy:
- LU factorization of a matrix A means for a lower triangular matrix L and an upper triangular matrix U, A = LU
- I got the L and U by following method:
[L,U,P] = lu(A);
A_Check = P'*L*U;
3. Then I used the backslash command to solve the system LUx = b.
b_Pb = P*b; % Permuation (move rows of b)
c_Lb = L \ b_Pb; % Forward Solve
x_PLU = U \ c_Lb; % Backward Solve
4. Using for loops, I have complete the forward solve to find c_Lb:
% ~ FORWARD SOLVE ~
% Here I have used b_Pb instead of b.
c(1) = b_Pb(1)/L(1,1);
for i = 2:length(c)
known_values = 0; %Firstly I sum all the known values in a row.
for j = 1:i-1
known_values = known_values + L(i,j)*c(j); % For the first loop, this is simply c(1) multiplied by L(2,1).
end % subtract the known values from the right hand side.
% then divide the the coefficient of the ith component of c to find % c(i).
c(i) = (b_Pb(i) - known_values)/L(i,i);
% ~ END FORWARD SOLVE ~
Now I have to write the backwards solve involving U, c, and solving for the x value in which I need help.
(((Nota Bene: c = [3.0000; -1.2500; -1.5000]
U =[ 4.0000 8.0000 1.0000; 0 -1.0000 -0.7500; 0 0 -2.5000]
And the final value of x is expected to be: x = [-1.0000; 0.8000; 0.6000] )))))
Any help will be really appreciated. Thanks :)