用Java计算Tarloy系列

时间:2016-03-05 17:17:01

标签: java function

我想计算这个函数的总和((-1)^ n)*(x ^ 2n)/(2.n!)但我的程序不工作。我需要你的帮助。这就是我试过的:

public double getCos()
     {
         double Cos = 0; 
         for(int i=0;i<=n;i++)
        {

             Cos+=(power(x,i)/facto(n));
        }
        return Cos;
     }
     private double facto(int n)
         {
             for (int i = 1; i <= n; i++) {
                   result = result * i;
                }
            return result*2;
         }
      private double power(int x,int n)
      {
          double power=Math.pow(-1,n)*Math.pow(x,2*n);
          return power;
      }


}

3 个答案:

答案 0 :(得分:0)

据我所知,您的result方法遇到了困难。首先,0从未正确声明(至少在提供的代码中)。此外,当第一个术语传递i=1时,i<=0因此false评估result并且循环永远不会执行。那么private double facto(int n) { if(n==0) return 1.0; double result = 0.0; for (int i = 1; i <= n; i++) { result = result * i; } return result*2; } 会有什么价值?

Double

答案 1 :(得分:0)

这是我将如何进行此练习:

公共课TaylorSeries {

static double power(double x,int n)
{
    double p = 1;
    for (int i = 1; i <= n ; i++) {
        p = p*x;
    }
    return p;
}

// this is the best way to make factorial function by recursion
static int factorial(int n)
{
    if(n == 1 || n == 0)
    {   
        return 1;   
    }
    else 
    {   
        return n*factorial(n - 1);
    }
}

// i = 0 to i = 5 the 5 is for the summation 
static double cos(double x) 
{
    double cos = 0;
    for (int i = 0; i <= 5 ; i++) {
        cos += ( power(-1, i) * power(x, 2*i) ) / ( factorial(2*i) );
    }
    return cos;
}

public static void main(String[] args) {

    System.out.println("2^3 : " + power(2, 3));
    System.out.println("5! : " + factorial(5));
    // 20 means summation from 0 to 20
    System.out.println("Cos(0.15) : " + cos(0.15));

}

}

答案 2 :(得分:0)

这就是你如何做到这一点我只是修复了你的程序中的一些错误:
公共课Cos {

public static double getCos(double x) {
    double Cos = 0;
    for (int i = 0; i <= 5; i++) {
        Cos += (power(-1, i) * power(x, 2*i)) / factorial(2*i) ;
    }
    return Cos;
}

公共课Cos {

public static double getCos(double x) {
    double Cos = 0;
    for (int i = 0; i <= 5; i++) {
        Cos += (power(-1, i) * power(x, 2*i)) / factorial(2*i) ;
    }
    return Cos;
}

private static double factorial(int n) {
    int result = 1;

    if( n == 0 || n == 1 )
        return 1;
    else {
    for (int i = 1; i <= n; i++) {
        result = result * i;
    }
    return result;
    }
}

private static double power(double x, int n) {
    return Math.pow(x, n);
}

public static void main(String[] args) {

    System.out.println("Test For The 3 Methods!");
    System.out.println("5^2 : " + power(5, 2));
    System.out.println("4! : " + factorial(4));
    System.out.println("Cos(0.2) : " + getCos(0.2));
}

}

private static double power(double x, int n) {
    return Math.pow(x, n);
}

public static void main(String[] args) {

    System.out.println("Test For The 3 Methods!");
    System.out.println("5^2 : " + power(5, 2));
    System.out.println("4! : " + factorial(4));
    System.out.println("Cos(0.2) : " + getCos(0.2));
}

}