0% found this document useful (0 votes)
18 views3 pages

Script of Code

The document outlines a VBA subroutine for performing LU decomposition using Doolittle's method on a 6x6 matrix. It includes steps for initializing matrices, applying partial pivoting, and calculating the lower and upper triangular matrices, L and U, along with a permutation vector P. Finally, the results are displayed in an Excel worksheet.

Uploaded by

palero.nathanj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Script of Code

The document outlines a VBA subroutine for performing LU decomposition using Doolittle's method on a 6x6 matrix. It includes steps for initializing matrices, applying partial pivoting, and calculating the lower and upper triangular matrices, L and U, along with a permutation vector P. Finally, the results are displayed in an Excel worksheet.

Uploaded by

palero.nathanj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Sub LU_Doolittles ()

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.

Dim A(1 To 6, 1 To 6) As Double


Dim L(1 To 6, 1 To 6) As Double
Dim U(1 To 6, 1 To 6) As Double
Dim P(1 To 6) As Integer
Dim i As Integer, j As Integer, k As Integer
Dim sum1 As Double, sum2 As Double

2. We define our 6x6 matrix A by setting its values in the code.

' Define your 6x6 matrix A here


' For example, let's use a random matrix

A(1, 1) = 4: A(1, 2) = 2: A(1, 3) = 3: A(1, 4) = 1: A(1, 5) = 5: A(1, 6) = 2


A(2, 1) = 8: A(2, 2) = 7: A(2, 3) = 6: A(2, 4) = 4: A(2, 5) = 4: A(2, 6) = 2
A(3, 1) = 1: A(3, 2) = 5: A(3, 3) = 5: A(3, 4) = 2: A(3, 5) = 7: A(3, 6) = 1
A(4, 1) = 7: A(4, 2) = 8: A(4, 3) = 7: A(4, 4) = 1: A(4, 5) = 6: A(4, 6) = 6
A(5, 1) = 3: A(5, 2) = 2: A(5, 3) = 8: A(5, 4) = 9: A(5, 5) = 3: A(5, 6) = 4
A(6, 1) = 6: A(6, 2) = 1: A(6, 3) = 4: A(6, 4) = 5: A(6, 5) = 1: A(6, 6) = 9

3. We initialize matrices L and U as all zeros and the permutation vector P as a simple
identity permutation.

' Initialize L, U, and P matrices


For i = 1 To 6
P(i) = i
For j = 1 To 6
L(i, j) = 0
U(i, j) = 0
Next j
Next i
4. The code then enters a loop that performs LU decomposition using Doolittle's method.
The loop iterates over the rows/columns of the matrix.
' Perform LU decomposition using Doolittle's method
For k = 1 To 6
5. Within the loop, partial pivoting is used to find the maximum absolute value element
in the current column (maxVal and maxIndex).
' Partial pivoting
Dim maxVal As Double
Dim maxIndex As Integer
maxVal = 0
For i = k To 6
If Abs(A(i, k)) > maxVal Then
maxVal = Abs(A(i, k))
maxIndex = i
End If
Next i

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.

You might also like