Mr. Numbers
New Member
- Joined
- Nov 19, 2011
- Messages
- 2
Hi!
I have successfully written a macro to solve a set of simultaneous equations, as follows:
a + b + c = 24
a - b = 2
b - c = 2
My macro is fine:
Dim ValInv As Variant
Dim EqResults As Variant
Dim EqVal(1 To 3, 1 To 3) As Double
Dim EqGive(1 To 3, 1 To 3) As Double
EqVal(1, 1) = 1
EqVal(1, 2) = 1
EqVal(1, 3) = 1
EqGive(1, 1) = 24
EqVal(2, 1) = 1
EqVal(2, 2) = -1
EqVal(2, 3) = 0
EqGive(2, 1) = 2
EqVal(3, 1) = 0
EqVal(3, 2) = 1
EqVal(3, 3) = -1
EqGive(3, 1) = 2
Set WF = WorksheetFunction
ValInv = WF.MInverse(EqVal)
EqResults = WF.MMult(ValInv, EqGive)
Cells(1, 1) = EqResults(1, 1) 'This is a
Cells(2, 1) = EqResults(2, 1) 'This is b
Cells(3, 1) = EqResults(3, 1) 'This is c
I get a=10, b=8, c=6
"So what's the problem?" I here you ask. Well, suppose one of the calculations were multiplication, as shown below:
a + b + c = 22
a - b = 10
b * c = 16
The answer should be: a=12, b=2, c=8
However, I have NO idea how to accomplish this. Or am I just being so stupid and the answer is obvious? Running the matrices doesn't seem to be the answer.
Thanks for anyone who takes their precious time to read my query.
I have successfully written a macro to solve a set of simultaneous equations, as follows:
a + b + c = 24
a - b = 2
b - c = 2
My macro is fine:
Dim ValInv As Variant
Dim EqResults As Variant
Dim EqVal(1 To 3, 1 To 3) As Double
Dim EqGive(1 To 3, 1 To 3) As Double
EqVal(1, 1) = 1
EqVal(1, 2) = 1
EqVal(1, 3) = 1
EqGive(1, 1) = 24
EqVal(2, 1) = 1
EqVal(2, 2) = -1
EqVal(2, 3) = 0
EqGive(2, 1) = 2
EqVal(3, 1) = 0
EqVal(3, 2) = 1
EqVal(3, 3) = -1
EqGive(3, 1) = 2
Set WF = WorksheetFunction
ValInv = WF.MInverse(EqVal)
EqResults = WF.MMult(ValInv, EqGive)
Cells(1, 1) = EqResults(1, 1) 'This is a
Cells(2, 1) = EqResults(2, 1) 'This is b
Cells(3, 1) = EqResults(3, 1) 'This is c
I get a=10, b=8, c=6
"So what's the problem?" I here you ask. Well, suppose one of the calculations were multiplication, as shown below:
a + b + c = 22
a - b = 10
b * c = 16
The answer should be: a=12, b=2, c=8
However, I have NO idea how to accomplish this. Or am I just being so stupid and the answer is obvious? Running the matrices doesn't seem to be the answer.
Thanks for anyone who takes their precious time to read my query.