Write a customized FUNCTION in VBA code to implement Cramer\'s rule. Then apply
ID: 3930432 • Letter: W
Question
Write a customized FUNCTION in VBA code to implement Cramer's rule. Then apply the customized function to solve below system of linear equations: [1 3 1 5 2 1 0 3 4 2 2 1 -3 1 3 2] {x_1 x_2 x_3 x_4} = {4 5 11 3} Specific requirements: 1) First, your customized function should check if the solution exists. In case the solution doesn't exist, prompt a warning message "Solution Doesn't Exist!" and exit the function. 2) Solution vector can be returned as either row or column vector, depending on how a user selects by dragging in worksheet. 3) Your customized function should be a generalized function, i.e. the code can be used to solve any system of linear equations. 4) Return and highlight your result vector in worksheet.Explanation / Answer
Dim x(1 To 3) As Single
Dim y(1 To 3) As Single
Dim z(1 To 3) As Single
Dim w(1 To 3) As Single
Private Sub RUN_Click()
x(1) = 1: x(2) = 2: x(3) = -1
y(1) = 1: y(2) = -1: y(3) = 3
z(1) = 1: z(2) = 1: z(3) = -1
w(1) = 0: w(2) = -1: w(3) = -8
'-------------------------------------------------------------------
detPart1 = x(1) * y(2) * z(3) + x(2) * y(3) * z(1) + x(3) * y(1) * z(2)
detPart2 = x(3) * y(2) * z(1) + x(1) * y(3) * z(2) + x(2) * y(1) * z(3)
determinant = detPart1 - detPart2
Print "determinant = "; determinant
If determinant = 0 Then
Print " determinant=0"
Exit Sub
End If
'----------------------------------------------------------------------
detPart1 = w(1) * y(2) * z(3) + w(2) * y(3) * z(1) + w(3) * y(1) * z(2)
detPart2 = w(3) * y(2) * z(1) + w(1) * y(3) * z(2) + w(2) * y(1) * z(3)
determinantx = detPart1 - detPart2
Print "determinant x = "; determinantx
'------------------------------------------------------------------------
detPart1 = x(1) * w(2) * z(3) + x(2) * w(3) * z(1) + x(3) * w(1) * z(2)
detPart2 = x(3) * w(2) * z(1) + x(1) * w(3) * z(2) + x(2) * w(1) * z(3)
determinanty = detPart1 - detPart2
Print "determinant y = "; determinanty
'-----------------------------------------------------------------------
detPart1 = x(1) * y(2) * w(3) + x(2) * y(3) * w(1) + x(3) * y(1) * w(2)
detPart2 = x(3) * y(2) * w(1) + x(1) * y(3) * w(2) + x(2) * y(1) * w(3)
determinantz = detPart1 - detPart2
Print "determinant z = "; determinantz
'-----------------------------------------------------------------------
x = determinantx / determinant
y = determinanty / determinant
z = determinantz / determinant
Print "x", "y", "z"
Print x, y, z
Print " x = "; x
Print " y = "; y
Print " z = "; z
End Sub
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.