A = B ^ n - C ^ n(Excel VBA:数值方法)

时间:2018-03-07 13:03:34

标签: vba excel-vba excel-formula numerical-methods numerical-computing

我有多次试验的日期 A B C 。我得到了等式:

A = B ^ {n} - C ^ {n}

A,B,C => 0

B&以及c

A B C 的值,我想计算 n

我试图创建一个使用数值方法计算n的VBA函数。

注意:我被告知 0.5< = n< = 1

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

是否有任何情况下此代码无效或者是否需要进行任何更改。

任何改进或其他解决方案都会得到满足! 谢谢, Ĵ

0 个答案:

没有答案