【问题标题】:Difficulty to Understand one Mathematics Formula难以理解一个数学公式
【发布时间】:2016-07-31 17:20:54
【问题描述】:

我有一个数学公式,很难理解并转换为 Java 代码。

[附图是我要解决的公式]

我已经编写了一些代码,但没有多大帮助。在公式中,我有 C 和 P 的值,需要找到 N。

下面是我用过的代码

public static Double sigmaSum(int c, int n, Double p){
    Double sum = 0.00;
    for(int i=0; i<c; i++){
    for(int j=i; j<n;j++){
    sum += ((Math.pow(p, j)) * (Math.pow((1-p), (n-j))));
   }

我有对应的excel公式但是不知道怎么转成java。

Option Explicit
Dim intLTPD, intCL, intC, intCalcF, intK, intComboNum, intComboDen1, intI, intJ, intComboDen2, intCombo, intL As Integer
Dim lngSampleSize As Long

Sub macBinSampPlan()
'
intLTPD = (Range("B1") / 100)
intCL = Range("B2") / 100
'intC = Int(Range("B3"))
Cells.Range("A6").Select

intCombo = 0
intCalcF = 0
intI = 0
intJ = 0

For intC = 0 To 10
    For intI = 1 To 10000
        For intJ = 0 To intC
            If intI >= intJ Then
                intCombo = Application.WorksheetFunction.Combin(intI, intJ)
                intCalcF = intCalcF + (intCombo * (Application.WorksheetFunction.Power(intLTPD, intJ)) * (Application.WorksheetFunction.Power((1 - intLTPD), (intI - intJ))))
            Else
                Exit For
            End If
        Next intJ
        If (intCalcF - (1 - intCL)) / intCalcF <= intLTPD Then
            lngSampleSize = intI
            Exit For
        Else
            intCombo = 0
            intCalcF = 0
        End If
    Next intI
    ActiveCell = intC
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell = lngSampleSize + 1
    ActiveCell.Offset(1, -1).Range("A1").Select
Next intC

End Sub

我正在努力解决这个问题大约一个星期,但无法解决。如果有人能解决这个问题,这应该是一个很大的帮助。提前致谢。

维韦克

【问题讨论】:

  • 不清楚你想要什么。您的 Java 模块代码采用 cnp,但没有返回任何内容。使用您的 Excel VBA 代码,完全不清楚不同的变量应该是什么。也许intJ 应该是i,所以intC 应该是c (10)。 intIn 从 1 循环到 10000,但为什么呢? intLTPD (B1) 可以是p。那么为什么不根据公式以有意义的方式命名变量呢?什么是intCL (B2),它与intCalcF 有什么关系,这是给定公式的结果? VBA 代码写入 10 行结果。 java代码到底应该做什么?
  • 因此,如果您想获得答案,请编辑您的问题并提供更多信息。顺便说一句:公式中的术语n choose iBinomial coefficient。这是 Excel 的 COMBIN 计算出来的。
  • 顺便说一句:BINOM.DIST - Java
  • Excel 也有=BINOM.DIST(c,n,p,TRUE)。从 VBA 你可以打电话给Application.WorksheetFunction.Binom_Dist(c,n,p,TRUE)

标签: java excel discrete-mathematics


【解决方案1】:

您实现二项式的方式似乎是错误的。根据你的公式,代码应该是:

public static Double sigmaSum(int c, int n, Double p){
    Double sum = 0.00;
    for(int i=0; i<c; i++){
    sum += ((CombinatoricsUtils.binomialCoefficientDouble(n,i)) * (Math.pow(p, i)) * (Math.pow((1-p), (n-i))));
   }
}

我还没有测试代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2016-04-26
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多