1 //此方法用来求方程x*(e^x)-1=0在一个[x1,x2]反外内的根,x1,x2的值在运行时输入//
2 #include<math.h>
3 #include<iostream>
4 using namespace std;
5 double f(double x)
6 {
7 double y;
8 y=exp(x)*x-1;
9 return y;
10 }
11
12 double point(double x1,double x2)
13 {
14 double y;
15 y=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
16 return y;
17 }
18
19 double root(double x1,double x2)
20 {
21 double x,y,y1;
22 y1=f(x1);
23
24 do{
25 x=point(x1,x2);
26 y=f(x);
27 if(y*y1>0)
28 {x1=x; y1=y;}
29 else
30 x2=x;
31 }while(fabs(y)>=0.0001);
32 return x;
33 }
34
35 void main()
36 {
37 double x1,x2,f1,f2,x;
38 do{
39 cin>>x1>>x2;
40 f1=f(x1);
41 f2=f(x2);
42 }while((f1*f2)>=0);
43 x=root(x1,x2);
44 cout<<x<<endl;
45 }