Saturday, April 3, 2010

GAUSS ELIMINATION

In linear algebra, Gaussian elimination is an algorithm for solving systems of linear equations, finding the rank of a matrix, and calculating the inverse of an invertible square matrix The following is a sample of VBA excel code created by the author: Private Sub Clear_Click() Application.ScreenUpdating = False Dim ans As String ans = MsgBox("Data will be cleared. Please confirm ", vbYesNo + vbExclamation) If ans = vbNo Then End Sheets(8).Activate 'clear content of matrix Sheets(8).Range("A10:IV1000").Select Selection.ClearContents Sheets(8).Cells(3, 2).Select End Sub Private Sub Compute_Click() Dim N, div, multi As Integer 'reading data N = Sheets(8).Cells(3, 2).Value ReDim c(N, N + 1) For i = 1 To N For j = 1 To N + 1 c(i, j) = Sheets(8).Cells(i + 10, j + 3).Value Next j Next i 'ensure c(1, 1) <> 0 If c(1, 1) = 0 Then Call msg0 Else 'forward elimination of pivot row For i = 1 To N div = c(i, i) For j = i To N + 1 c(i, j) = c(i, j) / div Next j For k = i + 1 To N multi = c(k, i) For j = i To N + 1 c(k, j) = c(k, j) - multi * c(i, j) Next j Next k Next i 'back substitution For i = N To 0 Step -1 For k = N To i + 1 Step -1 c(i, N + 1) = c(i, N + 1) - c(i, k) * c(k, N + 1) Next k Next i 'value of unknown For i = 1 To N Sheets(8).Cells(i + 10, 1) = c(i, N + 1) Next i End Sub Sub msg250() MsgBox ("n value is limit to 250 only!") End End Sub Sub msg0() MsgBox ("C(1,1) is equal to 0") End End Sub Private Sub GenerateGE_Click() Dim N As Integer Dim ans As String Application.ScreenUpdating = False Sheets(8).Activate 'N as number of row or column N = Sheets(8).Cells(3, 2).Value 'limit N to 250 If N > 250 Then Call msg250 Else For i = 1 To N Sheets(8).Cells(i + 10, 3) = i Sheets(8).Cells(10, i + 3) = i Next i Sheets(8).Cells(10, N + 4) = N + 1 Sheets(8).Cells(11, 2).Select End Sub