Matrix manipulation in vba code

A

Andrew

Hello,
Let me try posting this again. I am trying to do some simple matrix
manipulation in vba. What I want to do is take a 4x4 matrix, invert
it, multiply a 4x1 column vector by the inverted matrix, and then put
the result into a column vector. I do not want to use ranges for the
inputs. I will put the matrix values into the matrix manually.

A is 4x4 matrix
B is 4x1 column
C is 4x1 column

Dim A(1 To 4, 1 To 4) As Double
Dim B(1 To 4) As Double
Dim C()

Here is how the matrices are established. The variables come from
worksheet cells (not shown)
A(1, 1) = x1 ^ 3
A(2, 1) = x2 ^ 3
A(3, 1) = 3 * x1 ^ 2
A(4, 1) = 3 * x2 ^ 2

A(1, 2) = x1 ^ 2
A(2, 2) = x2 ^ 2
A(3, 2) = x1 * 2
A(4, 2) = x2 * 2

A(1, 3) = x1
A(2, 3) = x2
A(3, 3) = 1
A(4, 3) = 1

A(1, 4) = 1
A(2, 4) = 1
A(3, 4) = 0
A(4, 4) = 0

B(1) = y1
B(2) = y2
B(3) = m1
B(4) = m2

What I want to do is invert A, then multiply B by A inverted, and put
the
results in C.

Here is my code:
C=MInverse(A)
C=MMult(C,B)

The code stops at the MMult(C,B) line, stating "Unable to get the
MMult property of the worksheetfunction class."

This seems to be a simple problem. Can anyone help me with this. An
example would be great.
thanks,
Andy
 
D

Dana DeLouis

Here is my code:
C=MInverse(A)
C=MMult(C,B)
The code stops at the MMult(C,B) line,

Hi. The functions are part of WorksheetFunction in Vba, as shown below.
The error, as my guess, is that your dimensions are off.
In Help, note the requirements for Rows and Columns between the two arrays.
Here's a demo. I wrote it like this to save space.

Sub Demo()
Dim A As Variant
Dim B As Variant
Dim C As Variant

A = [{2,2,1,3;1,3,4,4;5,4,1,5;5,4,3,4}]
B = [{5;4;3;2}] 'Make sure vertical x;y...
With WorksheetFunction
If Abs(.MDeterm(A)) < 0.0000000001 Then
MsgBox "A is Singular"
Exit Sub
End If

C = .MInverse(A)
C = .MMult(C, B)

'// iF Horizontal (x,y...
B = [{5,4,3,2}]
'// Then it would work with...
C = .MMult(B, C)

End With
End Sub


= = =
HTH :>)
Dana DeLouis
 
D

Dana DeLouis

I should have mentioned that if you want to do it similar to your code,
just adjust vector B like this...

Sub Demo()
Dim A As Variant
Dim B(1 To 4, 1 To 1)
Dim C As Variant

A = [{2,2,1,3;1,3,4,4;5,4,1,5;5,4,3,4}]

B(1, 1) = 5
B(2, 1) = 4
B(3, 1) = 3
B(4, 1) = 2

With WorksheetFunction
If Abs(.MDeterm(A)) < 0.0000000001 Then
MsgBox "A is Singular"
Exit Sub
End If

C = .MInverse(A)
C = .MMult(C, B)
End With
End Sub

= = =
HTH :>)
Dana DeLouis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top