2014-09-24 15 views
0

我有一个函数,只有在某些情况发生时才调用样条函数..在这种情况下,当一个除数小于零时...函数的输入是与样条函数(称为CUBIC)相同,当我直接调用样条时,样条被测试并且运行良好!有人可以帮我吗?...遵循代码的一方当我调用样条函数时,我的函数的结果给出了错误的值

Function NDF6(T As Variant, dias As Variant, taxas As Variant) 
    If T <= dias(1) Then 
    NDF6 = taxas(1) 
    Exit Function 
    End If 
If T >= dias(tam) Then 
    NDF6 = taxas(tam) 
    Exit Function 
End If 
For i = 1 To tam 
    If T <= dias(i) Then 
     If taxas(i)/taxas(i - 1) < 0 Then 
      Call CUBIC(T, dias, taxas) 
     Else 
      i0 = ((taxas(i - 1) * dias(i - 1))/360) + 1 
      i1 = ((taxas(i - 1) * dias(i - 1))/360) + 1 
      irel = i1/i0 
      i2 = irel^((T - dias(i - 1))/(dias(i) - dias(i - 1))) 
      i2rel = i2 * i0 
      i2real = i2rel - 1 
      NDF6 = i2real * (360/T) 
     End If 
Public Function CUBIC(x As Variant, input_column As Variant, output_column As Variant) 
+0

这里没有足够的信息来真正地告诉问题可能是什么。您需要提供一些示例输入,“错误”输出以及预期输出应该是什么。以及CUBIC功能的完整代码。 – 2014-09-24 19:26:27

+0

@TimWilliams OK !!遵循立方体的完整代码 – 2014-09-25 12:49:40

回答

0

当我调用三次函数时,该函数返回一个零值。输入是与值相当于一天的值和两个阵列(DUONOFF和ONOFF)等效的小区中的天和价格,我称像功能:

NDF6(512,DUONOFF,ONOFF)

遵循CUBIC功能

Public Function CUBIC(x As Variant, input_column As Variant, output_column As Variant) 
    'Purpose: Given a data set consisting of a list of x values 
' and y values, this function will smoothly interpolate 
    ' a resulting output (y) value from a given input (x) value 

    ' This counts how many points are in "input" and "output" set of data 
    Dim input_count As Integer 
    Dim output_count As Integer 
    input_count = input_column.Rows.Count 
    output_count = output_column.Rows.Count 
    Next check to be sure that "input" # points = "output" # points 
    If input_count <> output_count Then 
    CUBIC = "Something's messed up! The number of indeces number of output_columnues don't match!" 
    GoTo out 
    End If 

    ReDim xin(input_count) As Single 
    ReDim yin(input_count) As Single 
    Dim c As Integer 
    For c = 1 To input_count 
    xin(c) = input_column(c) 
    yin(c) = output_column(c) 
    Next c 

     values are populated 

Dim N As Integer 'n=input_count 
Dim i, k As Integer 'these are loop counting integers 
Dim p, qn, sig, un As Single 
ReDim u(input_count - 1) As Single 
ReDim yt(input_count) As Single 'these are the 2nd deriv values 
N = input_count 
yt(1) = 0 
u(1) = 0 
For i = 2 To N - 1 
sig = (xin(i) - xin(i - 1))/(xin(i + 1) - xin(i - 1)) 
p = sig * yt(i - 1) + 2 
yt(i) = (sig - 1)/p 
u(i) = (yin(i + 1) - yin(i))/(xin(i + 1) - xin(i)) - (yin(i) - yin(i - 1))/(xin(i) - xin(i - _1)) 
    u(i) = (6 * u(i)/(xin(i + 1) - xin(i - 1)) - sig * u(i - 1))/p 

    Next i 

    qn = 0 
    un = 0 
    yt(N) = (un - qn * u(N - 1))/(qn * yt(N - 1) + 1) 
    For k = N - 1 To 1 Step -1 
     yt(k) = yt(k) * yt(k + 1) + u(k) 
     Next k 


     now eval spline at one point 

    Dim klo, khi As Integer 
    Dim h, b, a As Single 
     first find correct interval 
    klo = 1 
    khi = N 
    Do 
    k = khi - klo 
    If xin(k) > x Then 
    khi = k 
    Else 
    klo = k 
    End If 
    k = khi - klo 
    Loop While k > 1 
    h = xin(khi) - xin(klo) 
    a = (xin(khi) - x)/h 
    b = (x - xin(klo))/h 
    y = a * yin(klo) + b * yin(khi) + ((a^3 - a) * yt(klo) + (b^3 - b) * yt(khi)) * (h^2) _/ 6 

    CUBIC = y 
    out: 
    End Function