#include <stdio.h>
float func(float x) {
return x * x - 2;
}
float binSearch(float beg, float end, float acc) {
float mid;
while ( end - beg > acc) {
mid = (end + beg) / 2;
if (func(mid) > 0) {
end = mid;
} else {
beg = mid;
}
printf("beg,end,acc %f,%f,%f\n",beg,end,acc);
}
return mid;
}
int main () {
float a = binSearch(1, 2, 1e-5);
printf("x*x = 2 solution is %f\n", a);
printf("x*x = 2 solution is %f\n", binSearch(1, 2, 1e-6));
printf("x*x = 2 solution is %f\n", binSearch(1, 2, 1e-10));
return 0;
}
圆剖面,倒水之后水面下和总半圆面积比值r(0.6比如),问灌水高度多少;转为优化问题 灌水高度h介于[0,R]之间优化;
转载请注明原文地址:https://blackberry.8miu.com/read-14234.html