我有一段未记录的代码,我必须理解它才能修复错误。调用以下方法
optimization
它应该找到一个非常复杂的函数的最大值
f
. 不幸的是,它在某些情况下失败了(即,它到达了“Max iteration reached”行)。
public static double optimization(double x1, double x2, double x3, Function<Double, Double> f, double epsilon) {
double y1 = f.apply(x1);
double y2 = f.apply(x2);
double y3 = f.apply(x3);
double a = ( x1*(y2-y3)+ x2*(y3-y1)+ x3*(y1-y2)) / ((x1-x2)*(x1-x3)*(x3-x2));
double b = (x1*x1*(y2-y3)+x2*x2*(y3-y1)+x3*x3*(y1-y2)) / ((x1-x2)*(x1-x3)*(x2-x3));
int i=0;
do {
i=i+1;
x3=x2;
x2=x1;
x1=-1.*b/(2*a);
y1=f.apply(x1);
y2=f.apply(x2);
y3=f.apply(x3);
a = ( x1*(y2-y3)+ x2*(y3-y1)+ x3*(y1-y2))/((x1-x2)*(x1-x3)*(x3-x2));
b = (x1*x1*(y2-y3)+x2*x2*(y3-y1)+x3*x3*(y1-y2))/((x1-x2)*(x1-x3)*(x2-x3));
} while((Math.abs(x1 - x2) > epsilon) && (i<1000));
if (i==1000){
Log.debug("Max iteration reached");
}
return x1;
}