我遇到了一个最小值设置不正确的问题。最大值设置完美,但我知道最小值应小于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;
}
}
最佳答案
表达方式
Math.cbrt(2 * x) - Math.sqrt(8 * x) + x + 16;
不等于图形方程的右侧-您将立方根与立方混淆,将平方根与平方混淆。
正确的表达是
(2 * x * x * x) - (8 * x * x) + x + 16
关于java - 确定功能极值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36560386/