确定函数的极值

Determine extrema of function

我 运行 遇到了一个未正确设置最小值的问题。最大值设置完美,但我知道最小值应该小于 0。运行 这个片段,似乎从未设置过最小值。有什么想法吗?

编辑:曲线点应该是从-1到3。这是一张图片:

public class FindingExtrema {
    public static void main(String[] args) {
        double lowestPoint = 0;
        double highestPoint = 0;
        double y;

        double x = -1;
        int timesCalculated = 0;

        while (x <= 3) {
            y = run(x);

            if (y < lowestPoint) {
                lowestPoint = y;
                System.out.printf("y: %1$.5f", y);
            }

            if (y > highestPoint) {
                highestPoint = y;
            }

            x += .00001;
            timesCalculated++;
        }

        System.out.println("Done!");
        System.out.printf("Lowest: %1$.5f, Highest: %2$.5f; Calculated %3$d times\n", lowestPoint, highestPoint, timesCalculated);
    }

    private static double run(double x) {
        return Math.cbrt(2 * x) - Math.sqrt(8 * x) + x + 16;
    }
}

but I know that the minima should be less than 0.

它不是,如果你绘制它。我将你的函数插入到 Google 中,范围为 0 到 3,最小值类似于 15.431。

按如下方式设置你的最低点。

    double lowestPoint = 1000;

你能把原方程式写出来吗?你找不到负值的平方根。

平方 public 静态双 sqrt(双 a)

Returns 双精度值的正确四舍五入的正平方根。特殊情况: ◦如果参数为 NaN 或小于零,则结果为 NaN。 ◦如果参数为正无穷大,则结果为正无穷大。 ◦如果参数为正零或负零,则结果与参数相同。 否则,结果是最接近参数 value.Parameters:a - a value.Returns:a 的正平方根的真实数学平方根的双精度值。如果参数为 NaN 或小于零,则结果为 NaN。

表达式

Math.cbrt(2 * x) - Math.sqrt(8 * x) + x + 16;

不等同于图表方程式的右侧 - 您将立方根与立方混淆,将平方根与平方混淆。

正确的表达方式是

(2 * x * x * x) - (8 * x * x) + x + 16