I need some help with numerical , specifically VBA writing code . I posted a pic
ID: 3184117 • Letter: I
Question
I need some help with numerical , specifically VBA writing code . I posted a picture of the excel cells and numbers . Please help ! in ci Secure https://canvas.ouedu/courses/64376/files/folder/HW/HW7?preview.7386479 1: Solve SOE using Inverse Matrix A Write a user-defined VBA subroutine Solve1() to solve the system of equations X+Y+Z=1 2X + Y + 2Z = 2 X+2Y = 2 by performing the following tasks in Worksheet "Solve11": 1. Generate [A] using Gauss-Jordan Elimination 2. Write [A1] 3. Generate the column vector(x) = [A-11(b) 4. Write x) Q2: Solve SOE using Gauss EliminationExplanation / Answer
Option Explicit
Function Gauss_Jordan(A As Variant) As Variant
Dim i As Long, j As Long, k As Long, Atemp
Dim cols As Long, rows As Long, MaxVal As Double
Dim Max_Ind As Double, temp As Double, hold()
'******************************************************
'** Function performs Gauss-Jordan decomposition on **
'** a pxp input matrix and returns the augmented **
'** px2p matrix with the pxp identity matrix on the **
'** left hand side and the pxp matrix inverse on **
'** the right hand side. **
'******************************************************
'Determine if function is used on worksheet
If IsObject(A) = True Then
cols = A.Columns.Count
rows = A.rows.Count
Else
cols = UBound(A, 2)
rows = UBound(A, 1)
End If
'Increase columns to add identity matrix
cols = cols + cols
ReDim hold(1 To rows, 1 To cols), Atemp(1 To rows, 1 To cols)
'Augment matrix with identity matrix
For i = 1 To rows
For j = 1 To rows
Atemp(i, j) = A(i, j)
Atemp(i, j + rows) = 0#
Next j
Atemp(i, i + rows) = 1#
Next i
For i = 1 To rows
MaxVal = Atemp(i, i)
Max_Ind = i
' Sort procedure finds the greatest leading value
For j = i + 1 To rows
If Abs(Atemp(j, i)) > Abs(MaxVal) Then
MaxVal = Atemp(j, i)
Max_Ind = j
End If
Next j
If MaxVal = 0 Then
MsgBox Prompt:="Matrix is singular!", Title:="Error"
Exit Function
End If
'Set pivot equal to 1 and swap rows if necessary
For j = i To cols
'row i into temporary storage if swap is needed
temp = Atemp(i, j)
'data from row max_ind into row i and divide by maxval
Atemp(i, j) = Atemp(Max_Ind, j) / MaxVal
'Swap the max_ind row with row i (if necessary)
If Max_Ind <> i Then Atemp(Max_Ind, j) = temp
Next j
'Perform row operations to produce reduced row-echelon form
For k = 1 To rows
If k <> i Then 'Not the pivot
'store multiple of row
For j = 1 To cols
hold(k, j) = -Atemp(k, i) * Atemp(i, j)
Next j
'add together and replace the row
For j = 1 To cols
Atemp(k, j) = Atemp(k, j) + hold(k, j)
Next j
End If
Next k
Next i
Gauss_Jordan = Atemp
End Function