A=B^n-C^n(Excel VBA:数值方法)
我从A、B和C的多个试验中获得了数据。我得到了一个等式: A=B^{n}-C^{n} A、B、C=>0 B>C 根据A、B和C的值,我希望计算n 我试图创建一个VBA函数,用数值方法计算nA=B^n-C^n(Excel VBA:数值方法),vba,excel,excel-formula,numerical-methods,numerical-computing,Vba,Excel,Excel Formula,Numerical Methods,Numerical Computing,我从A、B和C的多个试验中获得了数据。我得到了一个等式: A=B^{n}-C^{n} A、B、C=>0 B>C 根据A、B和C的值,我希望计算n 我试图创建一个VBA函数,用数值方法计算n 注意:我被告知0.5您是否尝试过数据类型decimal而不是double?@FunThomas,我认为decimal是VB.Net数据类型,而不是VBA@Job,我想你指的是数据,而不是多个试验的数据?当你说失败是什么意思?您能否描述并举例说明不起作用的输出和起作用的输出?更多的例子将有助于确定这是否是一个截
注意:我被告知0.5您是否尝试过数据类型
decimal
而不是double
?@FunThomas,我认为decimal
是VB.Net数据类型,而不是VBA@Job,我想你指的是数据,而不是多个试验的数据?当你说失败是什么意思?您能否描述并举例说明不起作用的输出和起作用的输出?更多的例子将有助于确定这是否是一个截断/精度问题
Option Explicit
Private Const mdblEPSILON As Double = 0.00000001
Public Function IsEqual(ByVal dblA As Double, ByVal dblB As Double) As Boolean
IsEqual = Abs(dblA - dblB) < mdblEPSILON
End Function
Public Function IsGreaterThan(ByVal dblA As Double, ByVal dblB As Double) As Boolean
IsGreaterThan = (dblA > dblB) And IsEqual(dblA, dblB) = False
End Function
Function nfinder(dblConstant As Double, dblPR As Double, dblPP As Double, dblDP As Double)
Dim i As Double
Dim j As Double
Dim k As Double
Dim n(1 To 11) As Double
Dim ratiodifference(1 To 11) As Double
Dim dblA As Double
Dim dblB As Double
Dim Temporary As Double
Dim ValueLB As Double
Dim ValueUB As Double
'This part calculates the ratiodifference for n= 0.5,0.6,...,1
'The ratio is:
' 1. A / (B^{n} - C^{n}) is calculated for each n
' 2. This difference between this value and 1 is calculated
' 3. I am assuming if A = B^{truevalue.n} - C^{truevaluen} then the ratiodifference will = 0 as the ratio should = 1
For i = 1 To 6
n(i) = 0.1 * i + 0.4
ratiodifference(i) = Abs(1 - dblConstant / (dblPR ^ n(i) - dblPP ^ n(i)))
Next i
'I could have used redim here but i was lazy
For i = 7 To 11
n(i) = 100
ratiodifference(i) = 999999999999999#
Next i
' This part orders using bubble sort in order of ratiodifference
For j = 1 To 6
For i = 1 To 5
dblA = ratiodifference(i)
dblB = ratiodifference(i + 1)
If IsGreaterThan(dblA, dblB) = True Then
Temporary = ratiodifference(i)
ratiodifference(i) = ratiodifference(i + 1)
ratiodifference(i + 1) = Temporary
Temporary = n(i)
n(i) = n(i + 1)
n(i + 1) = Temporary
End If
Next i
Next j
'This part selects the smaller n of the 2 smallest ratio difference and sets this as the LowerBound
If IsGreaterThan(n(1), n(2)) Then
Temporary = ratiodifference(1)
ratiodifference(1) = ratiodifference(2)
ratiodifference(2) = Temporary
Temporary = n(1)
n(1) = n(2)
n(2) = Temporary
End If
ValueLB = n(1)
'Using loops the above process is repeated up to a desired amount of decimal places
For k = 2 To dblDP + 1
'Starting at the lower bound go through the decimal incriments
For i = 1 To 10
n(i) = ValueLB + (i - 1) * 0.1 ^ k
ratiodifference(i) = Abs(1 - dblConstant / (dblPR ^ n(i) - dblPP ^ n(i)))
Next i
For j = 1 To 11
For i = 1 To 10
dblA = ratiodifference(i)
dblB = ratiodifference(i + 1)
If IsGreaterThan(dblA, dblB) = True Then
Temporary = ratiodifference(i)
ratiodifference(i) = ratiodifference(i + 1)
ratiodifference(i + 1) = Temporary
Temporary = n(i)
n(i) = n(i + 1)
n(i + 1) = Temporary
End If
Next i
Next j
If IsGreaterThan(n(1), n(2)) Then
Temporary = ratiodifference(1)
ratiodifference(1) = ratiodifference(2)
ratiodifference(2) = Temporary
Temporary = n(1)
n(1) = n(2)
n(2) = Temporary
End If
ValueLB = n(1)
Next k
nfinder = Round(ValueLB, dblDP)
End Function