是这样的,关于三次样条我已经知道算法,就是卡在求逆矩阵这一块,也就是如下代码中红色字体的语句是唯一无法跑通的语句,求大神解答一下!!!
Public Function spline(x As Range, y As Range)
Dim l1 As Integer
l1 = x.Rows.Count
Dim l2 As Integer
l2 = y.Rows.Count
If l1 <> l2 Then
spline = "Error: Range count does not match"
GoTo endnow
End If
Dim a() As Double
Dim aInv() As Double
Dim i As Integer
Dim b() As Double
Dim k() As Double
Dim n As Integer
n = l1
ReDim a(1 To n, 1 To n)
ReDim aInv(1 To n, 1 To n)
ReDim b(1 To n)
'第1个点
a(1, 1) = 2 / (x(2) - x(1))
a(1, 2) = 1 / (x(2) - x(1))
b(1) = 3 * (y(2) - y(1)) / ((x(2) - x(1)) ^ 2)
'第n个点
a(n, n) = 2 / (x(n) - x(n - 1))
a(n, n - 1) = 1 / (x(n) - x(n - 1))
b(n) = 3 * (y(n) - y(n - 1)) / ((x(n) - x(n - 1)) ^ 2)
'第i个点
For i = 2 To n - 1
a(i, i - 1) = 1 / (x(i) - x(i - 1))
a(i, i + 1) = 1 / (x(i + 1) - x(i))
a(i, i) = 2 * a(i, i - 1) + 2 * a(i, i + 1)
b(i) = 3 * (y(i) - y(i - 1)) / ((x(i) - x(i - 1)) ^ 2) + 3 * (y(i + 1) - y(i)) / ((x(i + 1) - x(i)) ^ 2)
Next i
aInv = Application.WorksheetFunction.MInverse(a)
spline = aInv(1, 3)
endnow:
End Function