【问题标题】:Interpretable code using Newton-Raphson Method使用 Newton-Raphson 方法的可解释代码
【发布时间】:2019-08-18 07:53:31
【问题描述】:

我在使用必须计算给定参数的平方根的 Java 代码时遇到问题。 但是,经过一番研究,我发现了一个我不知道如何正确实现的代码。

 // read in the command-line argument
    double c = Double.parseDouble(args[0]);
    double epsilon = 1.0e-15;  // relative error tolerance
    double t = c;              // estimate of the square root of c

    // repeatedly apply Newton update step until desired precision is achieved
    while (Math.abs(t - c/t) > epsilon *t) {
        t = (c/t + t) / 2.0;
    }

    // print out the estimate of the square root of c
    System.out.println(t);
  1. 我不完全理解的第一件事是为什么它们在第 8 行除以二。

    t = (c/t + t) / 2.0;

  2. 我不明白的第二件事是 while 循环中的条件,更准确地说:

    while(Math.abs(t - c/t) > epsilon*t)

  3. 不需要只有:

    while(Math.abs(t - c/t) > epsilon)

【问题讨论】:

  • 你从哪里得到epsilon*t?这篇文档/文章对这个表达式写了什么以及为什么使用因子*t

标签: java newtons-method


【解决方案1】:
t = (c/t + t) / 2.0;

这用于计算c/tt 两个值的平均值/平均值。该值存储在变量t 中,用于下一次循环迭代。在while 循环内使用System.out.println() 调用来检查此行之前的tc/t 的值以及此行之后的值。

【讨论】:

    猜你喜欢
    • 2015-12-06
    • 1970-01-01
    • 2014-01-30
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多