【问题标题】:Java program gives incorrect Taylor series term for function e^xJava 程序为函数 e^x 给出了不正确的泰勒级数项
【发布时间】:2015-07-21 21:43:31
【问题描述】:
//java program that asks the user  to input a number that e^x=1+x+x^2/2! +x^3/3!... e is a mathematical constant equal to 2.718...

import java.util.Scanner;
public class taylor_2 {
  public static void main(String args[]) {
    Scanner input=new Scanner(System.in);
    double x; //input for x
    double factorial=1; //initializes factorial
    int counter=1; //initializes counter
    double result=1; //initializes result

    System.out.println("Enter non negative number"); //asks user to enter x
    x=input.nextInt();

 //output in while loop will continue to be generated if user doesn't entered a negative number

    while(x<1){
      System.out.println("I said entered a positive number");
      x=input.nextInt();
    }
    while(x>counter){
      factorial=factorial*counter;//factorial formula
      result=result+(Math.pow(x,counter))/factorial; //equation for e^x=1+x+x^2/2! +x^3/3!
      counter++;
    }
    System.out.println("Taylor series is " +result);//output for taylor equation e^x
  }
}

这是我的代码的输出:

输入非负数

2

泰勒级数是 4.0

当我输入 2 时,它应该输出 7.3890560983 而不是 4.0,因为 e=2.718... 和 e^2=7.3890560983。我做错了什么?

【问题讨论】:

  • 关于您的许多 // cmets 的旁注:它们几乎一文不值。注释应该在你想解释为什么做某事时才写;但是您的 cmets 只是重复已经存在且显而易见的内容。这样的 cmets 只会让阅读源代码变得更难。他们混淆而不是澄清。此外:类名应以大写字母开头;不是小写。
  • 为了帮助您解决问题:为什么要将 result 初始化为 1?为什么不添加更多 system.out.println 语句来打印循环中的各种值?换句话说:你期望别人调试你的代码;而......你已经有了代码;你让它运行;并且您可以轻松找出在计算过程中“发生”了什么。
  • 泰勒级数对从 0 到无穷大的 counter 的值求和,您仅对 x 求和,这不足以获得接近该级数收敛的值.显着增加它。将行 while(x&gt;counter){ 更改为 while(100&gt;counter){

标签: java algorithm taylor-series


【解决方案1】:

问题是泰勒级数与 e^x 不是同一个函数。 它将返回一个接近函数 e^x 的函数。

为了更好地理解它,我建议你看下一个链接的第二张图片:

https://en.wikipedia.org/wiki/Taylor_series

您可以在上图中看到,随着 n 越来越大,函数越来越准确。

您的代码的问题是您的 x 值是您的 n 值,这不是真的。

x: 必须是你现在想要 e^x 的值。

n:你的方程式是否准确。更大意味着更准确。

因此,您必须将 while(x&gt;counter) 更改为 while(n&gt;counter),其中 n 可以是具有用户选择精度的变量,也可以是具有您选择的精度的常数。

我认为在x=100 之前,n=150 应该可以工作。

希望对你有帮助! :)

【讨论】:

  • 别忘了credit me
  • 我怎么能相信你,我是那个网站的新手,我有点迷茫。感谢您的帮助。
【解决方案2】:

这里似乎有一个答案:EXP to Taylor series for c++,即使算法与你的略有不同。这是它的 Java 版本:

public class TaylorSeries {
public static void main(String args[]) {
    Scanner input = new Scanner(System.in);

    System.out.println("Enter x:");
    double x = input.nextDouble();

    double result = calcExp(x);
    System.out.println("calcExp(x) = " + result);
    System.out.println("       e^x = " + Math.pow(Math.E, x));
}

static double calcExp(double x) {
    double eps = 0.0000000000000000001;
    double elem = 1.0;
    double sum = 0.0;
    boolean negative = false;
    int i = 1;
    sum = 0.0;

    if (x < 0) {
        negative = true;
        x = -x;
    }

    do {
        sum += elem;
        elem *= x / i;
        i++;
        if (sum > Double.MAX_VALUE) {
            System.out.println("Too Large");
            break;
        }
    }
    while (elem >= eps);

    return negative ? 1.0 / sum : sum;
}
}

输出:

Enter x:
2
calcExp(x) = 7.389056098930649
       e^x = 7.3890560989306495

所有功劳都应该归于这里的答案:EXP to Taylor series。我只将 c++ 代码转换为 Java

【讨论】:

    猜你喜欢
    • 2016-01-24
    • 2019-04-11
    • 2021-12-26
    • 2013-01-21
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    相关资源
    最近更新 更多