我正在尝试最小化在Excel 2010中使用Solver工具
ABS(expectedPayout(H3,H6,H5,H7,H8,H4)-H21)
其中expectedPayout是我在VBA中创建的函数。求解器会运行,但它给了我一个无意义的答案。由于expectedPayout是一个创建的函数,求解器能不能真正使用它吗?
谢谢
编辑:添加更多信息
我的数据采用+ bi的形式,其中a和b是可以操作的常量,而且我是双精度数。
我的目标是尝试优化a和b,以使expectedPayout和H21之间的差异尽可能接近零
以下是expectedPayout的代码
Function expectedPayout(age As Double, payment As Double, rate As Double, coL As Double, guaranteedTime As Integer, gender As String)
Dim discountFactor As Double
Dim expectedValue As Double
Dim prob As Double
discountFactor = 1 / (1 + rate)
expectedValue = 0
For i = (age + guaranteedTime + 1) To 115 'stochastic payments
prob = 1
For Each cell In Range("B" & age + 3 & ":B" & i + 3)
prob = prob * (1 - cell.Value)
Next
expectedValue = expectedValue + payment * ((1 + coL) ^ (i - (age + 1))) * prob * (discountFactor ^ (i - age))
Next
For i = 1 To guaranteedTime 'deterministic payments
expectedValue = expectedValue + payment * ((1 + coL) ^ (i - 1)) * (discountFactor ^ i)
Next
expectedPayout = expectedValue
End Function
答案 0 :(得分:0)
Solver并不真正适用于用户定义的函数。我建议您创建一个与expectedPayout
计算的函数相反的新函数,并使用它而不是求解器。