【发布时间】:2015-10-29 01:40:38
【问题描述】:
我正在寻找一种方法来计算 Java 中函数的最小值和最大值。我要创建的程序将看到围绕 x 轴振荡的函数的所有局部最小值和最大值(这不是学校作业,尽管我在下面的大纲中提到了 cos(x))。我在互联网上看到的方法都计算数组的最小值/最大值。我正在考虑编写一种方法,该方法将直接计算从 x = 0 到 x = infinity 的函数的该值。
例如,从 x = 0 到 x = 5000 的 cos(x)。有大量的局部最小值和最大值,
另外,从 x = 0 到 x = 5000 的 sin(x)。有大量的局部最大值和最小值。
函数也是从 x = 0 到 x = 无穷大的连续函数。
是否有首选的数值方法来执行此操作?
public class Test {
public static void main(String [] args) {
Function testFunction = new Function()
{
public double f(double x) {
return something;
}
}
findMax(testFunction, 1, 40000, 0.001);
findMin(testFunction, 1, 40000, 0.001);
}
public static interface Function {
public double f(double x);
}
public static double function(double x) {
return Math.cos(x);
}
public static void findMax(Function f, double lowerBound, double upperBound, double step) {
}
public static void findMin(Function f, double lowerBound, double upperBound, double step) {
}
}
这是一个寻找根的类似程序 -
// Finds the roots of the specified function passed in with a lower bound,
// upper bound, and step size.
public static void findRoots(Function f, double lowerBound,
double upperBound, double step) {
double x = lowerBound, next_x = x;
double y = f.f(x), next_y = y;
int s = sign(y), next_s = s;
for (x = lowerBound; x <= upperBound ; x += step) {
s = sign(y = f.f(x));
if (s == 0) {
System.out.println(x);
} else if (s != next_s) {
double dx = x - next_x;
double dy = y - next_y;
double cx = x - dx * (y / dy);
System.out.println(cx);
}
next_x = x; next_y = y; next_s = s;
}
}
【问题讨论】:
-
好点,我可能不应该在午夜写这篇文章。我会更新它....
-
推导二次函数并测试极值?
-
@Axion004,你知道如何在数学上找到任何二次方程的最小值吗?你曾经对一阶和二阶导数感到难过吗?
-
在像
f(x) = x^2 + x这样的函数的情况下,您如何知道何时达到最大值?要在封闭域上找到最大/最小值,您可以使用当前的方法,即在细网格上划分域并测试所有值。 -
您正在寻找数学优化方法 - en.m.wikipedia.org/wiki/Mathematical_optimization 这个问题太宽泛了,无法回答。你甚至没有说你的函数是否是连续的。