#include <iostream>
#include <iomanip> //setprecision 设置输出精度
#include <cmath>
using namespace std;
//递增的函数
double fun(double x)
{
return (x + pow(x, 2) + pow(x, 3) + pow(x, 4) + pow(x, 5));
}
int main(int argc, char *argv[])
{
double a = 2.0f, b = 2.5f;
double c, f;
c= a + (b-a)/2;
f = fun(c);
while(fabs(f-100.0f)>1e-6)
{
if(f > 100.0f)
{
b = c;
c = a + (b - a) / 2;
}
else
{
a = c;
c = a + (b - a) / 2;
}
f = fun(c);
}
cout<<fixed<<setprecision(20)<<c<<endl;
return 0;
}
2.23964317142963410000
请按任意键继续. . .
这个题用心算,答案在2.0~2.5之间,因为2.5^5=97.62625... 当x=2时,加起来才62,
设定一个函数f(x)=x+x^2+x^3+x^4+x^5, x属于区间[2.0 2.5]
最优化。。。。