我遇到了最小值未正确设置的问题。最大值设置完美,但我知道最小值应该小于 0。运行此代码片段,似乎最小值从未被设置。有什么想法吗?
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。
关于java - 确定函数的极值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36560386/