PDA

View Full Version : [SLEEPER:] Urgent help re interpretation of code



Deltaeva
10-18-2010, 01:40 PM
Hello

I am relatively new to using vba. I have some code which I need to interpret re matrices.
1) explain the nature of the output matrix based on the procedure in the code
2) verify that all matrix multiplications are valid in terms of matrix size compatibility


Function Matrix_1(y As Variant, X As Variant) As Variant
Dim Xtrans As Variant
Dim temp As Variant
Dim output() As Variant, r() As Double, t() As Double
Xtrans = Application.Transpose(X)
XtX = Application.MInverse(Application.MMult(Xtrans, X))
temp = Application.Mult(XtX, Xtrans)
temp_out = Application.MMult(btemp, y)
Result = temp_out
End Function

Please help! thanks!

Frenchy646
10-20-2010, 01:29 AM
Hello,
Well, I may be a VBA newbie aswell, but here's the way I see what's happening in that code.

Function Matrix_1(y As Variant, X As Variant) As Variant

Here you're just defining the variables to be passed to the function (y and X ), and what the return value will be (in this case a variant, which I gather is the object used to contain matrices in VBA ).


Dim Xtrans As Variant
Dim temp As Variant
Dim output() As Variant, r() As Double, t() As Double

Codeefining all the variables to be stored in the RAM and used in the function. All of them will probably be matrices, except for r and t .
Note : being a newbie, I'm unsure as to why these variables seem to be methods due to the "()" at the end.


Xtrans = Application.Transpose(X)

Transposing the X matrix, and storing it in Xtrans.


XtX = Application.MInverse(Application.MMult(Xtrans, X))

Here, you are taking the inverse of the product of XTrans ( the x matrix you previously transposed ) and X, the original matrix used for Xtrans.


temp= Application.Mult(XtX, Xtrans)
Again, a multiplication. As it is not MMult, but in this case just Mult, I doubt very much that this is a matrix multiplication. Probably a scalar one ? Though don't see how that works with two matrices as arguments.
Value is being stored in variable named temp.


temp_out = Application.MMult(btemp, y)
Result = temp_out

Doing a Matrix multiplication between btemp (surely this must mean the temp variable ? ) and y, one of the arguments you passed the function.
I think it's not necessary to store it in temp_out then Result, but then again, I'm a VBA newbie, so there might be some silly reason why that has to be done.


As far as I can see
If the function works, then I am baffled, as I do not see much logic to it. If however, Result is the way that the result is returned from the function ( was pretty sure you would have had to store that in a variable named Matrix_1 ), and ASSUMING that btemp is suppose to be temp and that the code was meant to use MMult instead of Mult on the 7th line of your code, then this would be the matrix calculation being done :

Matrix arguments x and y
Result = ( ( ( x(T) * x )(^-1) ) * x(T) )* y

Where
x(T) means the transpose of the matrix named x
(^-1) means the inverse of the matrix in the brackets beforehand.

Hope this helps as a basis for understanding/discussion while you await an answer from someone far more versed in the mystery that is VBA

P.S : my original post was going to contain links to pages for you to understand what the different matrix calculations do, but not allowed to post links until I have done at least 5 posts

---------------------------------------------
Light travels faster than sound. That is why some people appear to be smart until you hear them speak

Bob Phillips
10-20-2010, 01:31 AM
This looks like a homework project, and it is not appropriate for us to do your homework.

Kenneth Hobs
10-20-2010, 05:26 AM
The best way to get help with your project is to use Excel's Help. Insert a module in the VBE and paste the code. Press F1 with the cursor in each keyword. This is the best way to learn about pre-built functions while in the VBE.