Script of Code
Script of Code
1. We start by declaring arrays A, L, U, and P to hold the input matrix, lower triangular
matrix, upper triangular matrix, and permutation vector, respectively.
3. We initialize matrices L and U as all zeros and the permutation vector P as a simple
identity permutation.
6. Rows are swapped in matrix A and permutation vector P to perform partial pivoting.
' Swap rows in A and P
Dim temp As Double
temp = P(k)
P(k) = P(maxIndex)
P(maxIndex) = temp
For j = 1 To 6
temp = A(k, j)
A(k, j) = A(maxIndex, j)
A(maxIndex, j) = temp
Next j
7. The code calculates the elements of matrices L and U based on Doolittle's method. The
sum1 and sum2 variables are used to accumulate the sum of products in the formulas.
' Calculate L and U matrices
For i = k To 6
sum1 = 0
For j = 1 To k - 1
sum1 = sum1 + L(i, j) * U(j, k)
Next j
L(i, k) = A(i, k) - sum1
Next i
For i = k + 1 To 6
sum2 = 0
For j = 1 To k - 1
sum2 = sum2 + L(k, j) * U(j, i)
Next j
U(k, i) = (A(k, i) - sum2) / L(k, k)
Next i
Next k
8. Finally, the code displays the resulting L, U, and P matrices in the Excel worksheet
starting from cell A1.
' Display L, U, and P matrices in Excel (assuming starting cell A1)
For i = 1 To 6
For j = 1 To 6
Cells(i, j).Value = L(i, j)
Cells(i, j + 7).Value = U(i, j)
Next j
Cells(i, 14).Value = P(i)
Next i
End Sub
You can replace the example matrix A with your own 6x6 matrix for decomposition. The
code performs LU decomposition, taking care of partial pivoting and produces the lower
and upper triangular matrices L and U, as well as the permutation vector P.