简体   繁体   English

用Java计算Tarloy系列

[英]Calculating Tarloy series in Java

I want to calculate the sum of this function ((-1)^n)*(x^2n)/(2.n!) but my program isn't working.I need your help.Here's what i tried: 我想计算此函数的总和((-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;
      }


}

From what I can tell, your difficulties are arising in your facto method. 据我所知,您的困难来自您的facto方法。 First of all, result was never properly declared (at least in the code provided). 首先,从未正确声明result (至少在提供的代码中)。 Furthermore, when 0 is passed for the first term, i=1 and therefore i<=0 evaluates false and the loop never executes. 此外,当第一项传递0时, i=1 ,因此i<=0 false ,并且循环永远不会执行。 So what value would result have? 那么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;
 }

This is How I Will Make This Exercise : 这就是我做这个练习的方式:

public class TaylorSeries { 公共类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));

}

} }

This is How You Make It I Just Fix Some Errors On Your Program : 这是您的方法,我只是在程序上修复了一些错误:
public class 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;
}

public class 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));
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM