2015-06-14 22 views
2

我寫一個Java裏面無限的總和相匹配 - 到t=infinity寫一個無窮大數的公式,爲什麼這不是寫成的?

sqrt(t)*sech(t)^2 dtt=0(無限總和從t = 0開始,然後在t = infinity結束我引用Wolfram Alpha的(數學)比較我的結果。)。

從更數學的角度來說,這(本質上)是程序在做什麼。我注意到這是平方(雙曲)割線。雖然,最大真的infinity-

integrate sqrt(t)*sech(t)^2 dt from t=0 to t=1000 

爲了配合這無限的總和,我寫了以下的短節目。

public class TestSum { 
    public static void main(String[] args) { 
      newForm(0.5); 
    } 

    public static double newForm(double s) { 
    int n = 0; 
    double currentSum = 0; 

    while (n < 1000) { 
     double sech = 1/Math.cosh(n); 
     double squared = Math.pow(sech, 2); 
     currentSum = ((Math.pow(n, s))*squared) + currentSum; 
     if(n == 999) 
      System.out.println("The current sum is " + currentSum); 
     n++; 
    } 
    return currentSum; 
    } 
} 

當我插入到這個數學/鎢我得到 -

integrate sqrt(t)*sech(t)^2 dt from t=0 to t=1000 
integral_0^1000 sqrt(t) sech^2(t) dt = 0.758128 

結果從運行程序 -

run: 
The current sum is 0.5401365941579325 
BUILD SUCCESSFUL (total time: 0 seconds) 

我敢肯定數學是沒有錯的。我的程序有什麼問題?

+1

你不是應該採取開方的地方? –

+1

這是如何遞歸?我沒有在任何地方看到遞歸 –

+0

Math.pow(n,s)引用s = 0.5。我不確定是否應該從技術上將它歸類爲遞歸(它看起來與我相似,但我在數學上比在Java上好)。 – Axion004

回答

5

您的解決方案根本不夠準確。

的積分可以通過黎曼和

Riemann Summ

看到Riemann Sum在維基百科上近似。

結果變得越好,delta x越小(或者在你的情況下,delta t越小)。

在你的解決方案delta t = 1,所以近似不是很好。

的一個可能的解決方案來近似結果更好是使用:

public class TestSum { 
    public static void main(String[] args) { 
      double result= integrate(0, 1000); 
      System.out.print("result = " + result); 
    } 

    public static double integrate(double start, double end) { 
    double currentIntegralValue = 0; 
    double dt=0.01d; 
    double t = start; 

    while (Math.abs(end - t) >= dt && t-end < 0) { 
     currentIntegralValue += fn(t)*dt; 
     t += dt; 
    } 
    return currentIntegralValue; 
    } 

    private static double fn(double t) { 
     double sech = 1/Math.cosh(t); 
     double squared = Math.pow(sech, 2); 
     return ((Math.pow(t, 0.5))*squared); 
    } 
} 

結果= 0.7579201343666041

可以通過使用較小的dt進一步改善的結果。

dt=0.00001d 

結果= 0.7581278135568323

+0

謝謝,我看到了我的錯誤。我有些尷尬,並將繼續在我的計劃上工作。 – Axion004

相關問題