C 中的黄金分割搜索
Golden-section search in C
我正在尝试编写一个程序,该程序使用黄金分割搜索方法来查找由函数 (x^2 / 4) + (y^2 / 9) = 1 但运气不好。设法让程序编译,但我得到的输出是“-nan”
这是我尝试过的:
#include <stdio.h>
#include <math.h>
// As the ellipse is given by the function (x^2 / 4) + (y^2 / 9) = 1 which can be rewritten as y = sqrt(9(1 - (x^2 / 4)))
// Thus, it can deduced that max(x) = 2, min(x) = -2, max(y) = 3 and min(y) = -3
double triangleArea(double base, double height) { // Calculates the area of one of the triangles that make up a half of the full triangle. The result will later be multiplied with 2 in order to get the area of the full triangle.
double area = (base * height) / 2;
return area;
}
double goldenSearch_Max (double xLower, double xUpper) {
double goldenRatio = 0.5 * (sqrt(5) - 1.0); // Initialising Golden Ratio.
double x1 = xUpper - (xUpper - xLower) / goldenRatio;
double x2 = xLower + (xUpper - xLower) / goldenRatio;
while (fabs(x2 - x1) > 0.00001) {
if ((triangleArea(x1, sqrt(9 * (1 - (x1 * x1 / 4))))) > (triangleArea(x2, sqrt(9 * (1 - (x2 * x2 / 4)))))) {
xUpper = x2;
}
else {
xLower = x1;
}
x1 = xUpper - (xUpper - xLower) / goldenRatio;
x2 = xLower + (xUpper - xLower) / goldenRatio;
}
return (xUpper + xLower);
}
int main() {
double goldenRatio = 0.5 * (sqrt(5) - 1.0); // Initialising Golden Ratio.
//double triangleBase = 2 + x; // The elliptical equation's max(x) was 2 and the base of the two halves of the triangle is located on the x axis. So the base of one of the halves is 2 + x, x being the extra length after the 2 units of the x axis.
//double triangleHeight = sqrt(1 - (x * x) / 4) * 3; // As the elliptical equation can also be written as y = sqrt(9(1 - (x^2 / 4))) after changing the places of the y terms and the result.
double xNew = goldenSearch_Max(0, 2);
printf("%f", (triangleArea(xNew, sqrt(9 * (1 - (xNew * xNew / 4))))));
return 0;
}
如有任何帮助,我们将不胜感激。
我可以发现几个问题:
double goldenRatio = 0.5 * (sqrt(5) - 1.0);
这就是黄金比例的倒数。这很好,只要它被相应地使用:
double const inv_golden_ratio = 0.5 * (sqrt(5) - 1.0);
double x1 = xUpper - (xUpper - xLower) * inv_golden_ratio;
// ^
// ...
return (xUpper + xLower);
不应该比return实际最大值(或最小值)对应的值更好吗?
点的平均值也可以,但简单的总和是错误的。
return (x1 + x2) * 0.5;
我正在尝试编写一个程序,该程序使用黄金分割搜索方法来查找由函数 (x^2 / 4) + (y^2 / 9) = 1 但运气不好。设法让程序编译,但我得到的输出是“-nan”
这是我尝试过的:
#include <stdio.h>
#include <math.h>
// As the ellipse is given by the function (x^2 / 4) + (y^2 / 9) = 1 which can be rewritten as y = sqrt(9(1 - (x^2 / 4)))
// Thus, it can deduced that max(x) = 2, min(x) = -2, max(y) = 3 and min(y) = -3
double triangleArea(double base, double height) { // Calculates the area of one of the triangles that make up a half of the full triangle. The result will later be multiplied with 2 in order to get the area of the full triangle.
double area = (base * height) / 2;
return area;
}
double goldenSearch_Max (double xLower, double xUpper) {
double goldenRatio = 0.5 * (sqrt(5) - 1.0); // Initialising Golden Ratio.
double x1 = xUpper - (xUpper - xLower) / goldenRatio;
double x2 = xLower + (xUpper - xLower) / goldenRatio;
while (fabs(x2 - x1) > 0.00001) {
if ((triangleArea(x1, sqrt(9 * (1 - (x1 * x1 / 4))))) > (triangleArea(x2, sqrt(9 * (1 - (x2 * x2 / 4)))))) {
xUpper = x2;
}
else {
xLower = x1;
}
x1 = xUpper - (xUpper - xLower) / goldenRatio;
x2 = xLower + (xUpper - xLower) / goldenRatio;
}
return (xUpper + xLower);
}
int main() {
double goldenRatio = 0.5 * (sqrt(5) - 1.0); // Initialising Golden Ratio.
//double triangleBase = 2 + x; // The elliptical equation's max(x) was 2 and the base of the two halves of the triangle is located on the x axis. So the base of one of the halves is 2 + x, x being the extra length after the 2 units of the x axis.
//double triangleHeight = sqrt(1 - (x * x) / 4) * 3; // As the elliptical equation can also be written as y = sqrt(9(1 - (x^2 / 4))) after changing the places of the y terms and the result.
double xNew = goldenSearch_Max(0, 2);
printf("%f", (triangleArea(xNew, sqrt(9 * (1 - (xNew * xNew / 4))))));
return 0;
}
如有任何帮助,我们将不胜感激。
我可以发现几个问题:
double goldenRatio = 0.5 * (sqrt(5) - 1.0);
这就是黄金比例的倒数。这很好,只要它被相应地使用:
double const inv_golden_ratio = 0.5 * (sqrt(5) - 1.0);
double x1 = xUpper - (xUpper - xLower) * inv_golden_ratio;
// ^
// ...
return (xUpper + xLower);
不应该比return实际最大值(或最小值)对应的值更好吗?
点的平均值也可以,但简单的总和是错误的。
return (x1 + x2) * 0.5;