-
MMULT problem
Hi there,
The last line wil not work below why is this?
I have tried it on an excel worksheet and it works there?
Also how can I return an array from a function. Is is possibel to retuern more than one array from a function?
Code:
Function bSpline(y As Range, xIn As Range, steps As Integer, Optional aIn As Variant) As Double
Dim V() As Double
Dim n As Integer, i As Integer, j As Integer, l As Integer
Dim a As Variant
Dim x() As Double
Dim B1() As Double
Dim B2() As Double
Dim B3() As Double
Dim B4() As Double
ReDim B1(steps)
ReDim B2(steps)
ReDim B3(steps)
ReDim B4(steps)
Dim f As Variant
f = y.Value
Dim k As Variant
k = xIn.Value
n = UBound(k) - LBound(k) - 4
ReDim V(n, n)
If IsMissing(aIn) Then
V(0, 0) = 1
V(0, 1) = -2
V(0, 2) = 1
For i = 1 To n - 1
V(i, i - 1) = 1 / 6
V(i, i) = 2 / 3
V(i, i + 1) = 1 / 6
Next i
V(n, n - 2) = 1
V(n, n - 1) = 2
V(n, n) = 1
a = Application.WorksheetFunction.MMult(Application.WorksheetFunction.MInverse(V), f)
Else
a = aIn.Value
End If
For j = 1 To 4
ReDim x(steps)
x(0) = k(j + 3, 1)
For l = 1 To (steps - 1)
x(l) = ((k(j + 4, 1) - k(j + 3, 1)) / steps) * l + k(j + 3, 1)
Next l
x(steps) = k(j + 4, 1)
For i = 1 To steps
B1(i) = Basis(x(i), j, 4, xIn)
B2(i) = Basis(x(i), j + 1, 4, xIn)
B3(i) = Basis(x(i), j + 2, 4, xIn)
B4(i) = Basis(x(i), j + 3, 4, xIn)
Next i
bSpline = Application.WorksheetFunction.MMult(B1, a(j, 1))
Next j
End Function
-
Re: MMULT problem
I believe MMULT requires two dimensional arrays.
-
Re: MMULT problem
But it works in excel?
+ from A level maths (4,4)*(4,1) =(4,1) should be no problem?
-
Re: MMULT problem
Code:
The last line wil not work below why is this?
By last line, do you mean the following?
bSpline = Application.WorksheetFunction.MMult(B1, a(j, 1))
If so I can see a couple of issues. First of all a(j,1) is a scalar and MMULT requires arrays. Also, I think there is a logic error in the function. The function return value bSpline is calculated within the loop for j. Only the last value would be returned, so there are unnecessary calcs. Lastly, what error do you get?
Code:
I have tried it on an excel worksheet and it works there?
Don't know what you mean by this. Tried what on an excel worksheet? I can't believe the posted function will work as a worksheet function.
Code:
Also how can I return an array from a function. Is is possibel to retuern more than one array from a function?
Looks like you did it here: http://www.vbforums.com/showthread.php?t=576973
A function can return more than one array, but what you'll get is a matrix.
As an overall comment, I suggest trying simple subs and functions using MMULT until you get a thorough understanding of what works. Then you can incorporate into a more complicated function. For example, the following is a simple sub that utilizes MMULT. Start playing around with different types of input, such as one dimensional arrays, and you'll quick learn what syntax is needed.:)
Code:
Sub works()
Dim B1(1 To 1, 1 To 3) As Double
Dim B2(1 To 3, 1 To 1) As Double
Dim res As Variant
B1(1, 1) = 1#
B1(1, 2) = 2#
B1(1, 3) = 3#
B2(1, 1) = -5#
B2(2, 1) = 0#
B2(3, 1) = 11#
res = Application.WorksheetFunction.MMult(B1, B2)
Debug.Print res(1)
End Sub
-
Re: MMULT problem
That's great thanks for your help
Baz