0

我正在使用 mclaurins 系列来计算 arccos(x^2-1) 但是当我将它与 math.acos 的结果进行比较时,它会有所不同。这是我的代码:

    public class Maclaurin {

    public static int factorial(int fact) {
        if(fact==0)
            return 1;
        return fact*factorial(fact-1);
    }

    public static void main(String[] args) {
    int i, j;
    double function = 0, x,result;

    x=0;

        for (int n = 0; n < 8; n++) {

            function=((factorial(2*n))/(Math.pow(4, n)*Math.pow(factorial(n),2)*(2*n+1)))*Math.pow((Math.pow(x, 2)-1),2*n+1);
            result=(Math.PI/2)-function;

            System.out.println("x= "+x+" y= "+result);
            System.out.println("Test "+Math.acos(Math.pow(x, 2)-1));
            x+=0.13;
        }




    }


    }

Programm output. test is a value calculated with Math.arccos and it differs from y calculated with mclaurins formula:

    x= 0.0 y= 2.5707963267948966
    Test 3.141592653589793
    x= 0.13 y= 1.7291549939933966
    Test 2.9574849820283498
    x= 0.26 y= 1.6236496851024964
    Test 2.771793621843802
    x= 0.39 y= 1.5848621264898726
    Test 2.5828078861333155
    x= 0.52 y= 1.5725761587226181
    Test 2.3885331918392687
    x= 0.65 y= 1.5708496332463704
    Test 2.1864594293995867
    x= 0.78 y= 1.570796415168701
    Test 1.9731661516473589
    x= 0.91 y= 1.5707963267948972
    Test 1.7435543826662978

编辑:计算在 maclaurin 函数中的新代码,我从主函数中调用它。对除前 3 个值外的所有值都有效:maclaurin 包;

public class Maclaurin {
    private static double x;

//public static int factorial(int fact) {
//    if(fact==0)
//        return 1;
//    return fact*factorial(fact-1);
//}
   public static double factorial(int fact) {
    if(fact==0)
        return 1;
    return fact*factorial(fact-1);
}

public static void main(String[] args)
    {
         x = 0;
        for (int i=0;i<8;i++)
            {
                maclaurin(x);
                x=x+0.14;
            }

    }

public static void maclaurin(double value){
    double function = 0, x, result;

    x =value;

    for (int n = 0; n < 20; n++) {
        function += ((factorial(2 * n)) / (Math.pow(4, n) * Math.pow(factorial(n), 2) * (2 * n + 1)))
                * Math.pow((Math.pow(x, 2) - 1), 2 * n + 1);
    }

    result = (Math.PI / 2) - function;
    System.out.println("x= " + x + " y= " + result);
    System.out.println("Test " + Math.acos(Math.pow(x, 2) - 1));
}




}
4

2 回答 2

2

我想你不明白,麦克劳林系列(泰勒系列)实际上是做什么的。它可以计算出一个近似值。您将仅获得 n -> ∞ 的准确值。由于我们无法计算,我们需要定义一些 n 并在那里停止计算。您必须添加总和的每个部分才能获得实际值。所以基本上你应该迭代 n 并将计算的值添加到function你可以计算的循环之后result

public static void main(String[] args){
    double x = 0;

    for(int i = 0;i < 8;i++){
        System.out.println("x: " + x + " maclaurin: " + maclaurin(x));
        System.out.println("Test: " + Math.acos(Math.pow(x, 2) - 1));

        x += 0.14;
    }
}

public static double maclaurin(double x){
    double function = 0;

    for (int n = 0; n < 10; n++) {
        function += ((factorial(2 * n)) / (Math.pow(4, n) * Math.pow(factorial(n), 2) * (2 * n + 1)))
                * Math.pow((Math.pow(x, 2) - 1), 2 * n + 1);
    }

    return (Math.PI / 2) - function;
}

这样我得到了非常接近的值:

x: 0.0 maclaurin: 2.962490972738185
Test: 3.141592653589793
x: 0.14 maclaurin: 2.8972172328920296
Test: 2.9432779368601296
x: 0.28 maclaurin: 2.7366715068800485
Test: 2.7429790581451043
x: 0.42000000000000004 maclaurin: 2.5381695201901326
Test: 2.5385256934250617
x: 0.56 maclaurin: 2.3273181153351756
Test: 2.327323409412957
x: 0.7000000000000001 maclaurin: 2.105981109221438
Test: 2.1059811170704963
x: 0.8400000000000001 maclaurin: 1.8696239609917407
Test: 1.8696239609918046
x: 0.9800000000000001 maclaurin: 1.6104066839613247
Test: 1.6104066839613247

看起来,对于接近 0 的 x,结果并不好。您可以增加n以获得更好的结果,但在某些时候您会得到NaN,因为factorial在某些时候会溢出。

不要忘记factorial像@Brick 建议的那样进行更改:

public static double factorial(int fact) {
    if(fact==0)
        return 1;
    return fact*factorial(fact-1);
}

如果您想知道如何以factorial迭代方式计算,这里有一个示例(在这种特殊情况下实际上并不重要,但是嘿,我们是来学习新事物的,对吧?):

public static double factorial(int fact) {
    double result = 1;
    while(fact > 1){
        result *= fact--;
    }
    return result;
}

编辑:将fact参数更改为int

EDIT2:包装maclaurin函数并添加更多值

EDIT3:添加了迭代factorial

于 2016-09-07T21:43:26.830 回答
2

出现在循环中的 16 的阶乘大于MAX_INT.

我得到:

 >> factorial(16)
 2.0923e+013
 >> 2^31
 2.1475e+009

在八度。您需要进行分析简化以将其保持在范围内或使用double而不是在int那里。

于 2016-09-07T21:10:46.857 回答