java - 在 Java 中求解三次方程时遇到问题 - IT工具网

我正在尝试遵循《程序员的数学和物理》第 3 章中的一些伪代码来求解三次方程,据我所知,我已经准确地遵循了它,但我似乎没有得到正确的输出.

例如:根据 Wolfram Alpha 5x^3 + 4x^2 + 3x + 2 = 0 应该给出 x≈-0.72932 的根,但我从脚本中返回 -1.8580943294965526。

有人能帮我弄清楚我到底在做什么吗?我按照脚本来更好地理解数学并将方程转换为代码。但这还处于我理解的边缘,所以我发现调试起来很麻烦。再加上这本书没有勘误表,而且许多在线评论都指出这本书有很多错误,我很难确定问题是出在我的代码、书籍解释还是两者兼而有之。

书中给出的方程是:

eq

如果判别式 > 0,则根的值为 r+s:

D1

如果判别式 == 0 则有两个根:

D2

如果判别式< 0,则可以找到三个根,如下所示:

D3

找到_t_后,您可以通过以下方式将其转换为_x_:

transform

package com.oacc.maths;

public class SolveCubic {

    public static double[] solveCubic(double a, double b, double c, double d) {
        double[] result;
        if (d != 1) {
            a = a / d;
            b = b / d;
            c = c / d;
        }

        double p = b / 3 - a * a / 9;
        double q = a * a * a / 27 - a * b / 6 + c / 2;
        double D = p * p * p + q * q;

        if (Double.compare(D, 0) >= 0) {
            if (Double.compare(D, 0) == 0) {
                double r = Math.cbrt(-q);
                result = new double[2];
                result[0] = 2 * r;
                result[1] = -r;
            } else {
                double r = Math.cbrt(-q + Math.sqrt(D));
                double s = Math.cbrt(-q - Math.sqrt(D));
                result = new double[1];
                result[0] = r + s;
            }
        } else {
            double ang = Math.acos(-q / Math.sqrt(-p * p * p));
            double r = 2 * Math.sqrt(-p);
            result = new double[3];
            for (int k = -1; k <= 1; k++) {
                double theta = (ang - 2 * Math.PI * k) / 3;
                result[k + 1] = r * Math.cos(theta);
            }

        }
        for (int i = 0; i < result.length; i++) {
            result[i] = result[i] - a / 3;
        }
        return result;
    }


    public static double[] solveCubic(double a, double b, double c) {
        double d = 1;
        return solveCubic(a, b, c, d);
    }

    public static void main(String args[]) {
        double[] result = solveCubic(5, 4, 3, 2);
        for (double aResult : result) {
            System.out.println(aResult);
        }
    }
}

我还从书籍网站找到了这个代码示例(注意,这不是书中的伪代码):http://www.delmarlearning.com/companions/content/1435457331/files/index.asp?isbn=1435457331

 on solveCubic(a,b,c,d)
 --! ARGUMENTS: 
a, b, c, d (all numbers). d is optional (default is 1)
 --! 
RETURNS: the list of solutions to dx^3+ax^2+bx+c=0


-- if d is defined then divide a, b and c by d 
 if not voidp(d) 
then
 if d=0 then return solveQuadratic(b,c,a)

set d to float(d)
 set a to a/d
 set b to b/d
 set 
c to c/d
 else
 set a to float(a)
 set b to float(b)

 set c to float(c)
 end if

 set p to b/3 - a*a/9

 set q to a*a*a/27 - a*b/6 + c/2
 set disc to p*p*p + q*q


 if abs(disc)<0.001 then 
 set r to cuberoot(-q)
 set ret to [2*r, -r]
 else if disc>0 then
 set r to cuberoot(-q+sqrt(disc))
 set s to cuberoot(-q-sqrt(disc))

set ret to [r+s]
 else

 set ang to acos(-q/sqrt(-p*p*p))
 set r to 2*sqrt(-p)

set ret to []
 repeat with k=-1 to 1
 set theta 
to (ang - 2*pi*k)/3
 ret.add(r*cos(theta))
 end repeat

end if
 set ret to ret-a/3 --NB: this adds the value to each 
element
 return ret
end 

最佳答案

错误似乎与您的 solveCubic 方法的参数名称有关。

您的书似乎正在解释如何求解方程 x3 + ax2 + bx + c = 0。您正在调用您的方法,认为参数 abcd 用于方程 ax3 + bx< support>2 + cx + d = 0。然而,事实证明,您的方法的主体实际上是在寻找方程 dx3 + ax2 + bx + c = 0。

除了这个命名错误之外,计算似乎是正确的。如果您不相信我,请尝试将您的值 ≈-1.858 代入 2x3 + 5x2 + 4x + 3。

如果您将 solveCubic 方法声明为

public static double[] solveCubic(double d, double a, double b, double c) {

参数对应于方程 dx3 + ax2 + bx + c。然后您应该会发现您的方法给出了您期望的答案。

关于java - 在 Java 中求解三次方程时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43559140/


原网址: 访问
创建于: 2023-12-05 18:21:48
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论