#include <iostream>
#include <cmath> using namespace std; float f(float); void main() { const double error=0.00001; float x1=0,x2=3,x; do { x=(x1+x2)/2; if(f(x)*f(x1)>0) x1=x; else x2=x; // cout<<x1<<" "<<x2<<endl; }while(fabs(x1-x2)>error); cout<<x<<endl; } float f(float x) { return exp(x)-x-2;//x*x-x-2 } |
|