用二分法求方程的根

总时间限制: 1000ms      内存限制:  65536kB 题目链接:http://ica.openjudge.cn/function1/4/
描述

用二分法求下面方程在(-10, 10)之间的一个根。 
2x3- 4x2+ 3x- 6 = 0

输入
一个小于1的非负实数e,它的值表示所能允许的误差
输出
一个实数,其值为求得的一个根,要求精确到小数点后8位。
若该区间上没有根,则输出“No Solution”
样例输入
0
样例输出
2.00000000
提示对于一个连续函数f(x),若f(a)*f(b) <= 0,则f(x)在区间[a, b]内至少有一个根。
特别的,对于一个单调的连续函数,上述定理得逆定理也成立
若[a, b]上有根,则可进一步考察根是否在 [a, (a+b)/2]内,或者在[(a+b)/2, b]内。

若b-a <= e 则可终止迭代,并认为(a+b)/2是一个近似解,将它输出

请使用double类型!
 1 #include<stdio.h>
 2 #include<math.h>
 3 double f(double x);
 4 double fun(double a,double b,double e);
 5 int main()
 6 {
 7     double e,x1,x2,x;
 8     x1=-10;x2=10;
 9     scanf("%lf",&e);
10     x=fun(x1,x2,e);
11     printf("%.8lf\n",x);
12     return 0;
13 }
14 double f(double x)
15 {
16     double y;
17     y=2*x*x*x-4*x*x+3*x-6;
18     return y;
19 }
20 double fun(double a,double b,double e)
21 {
22     double fa,fb,fc,c;
23     fa=f(a);
24     fb=f(b);
25     c=(a+b)/2;
26     while(fabs(b-a)>e)
27     {
28         fc=f(c);
29         if(fc==0)
30         {
31             break;
32         }
33         else if(fc*fa<0)
34         {
35             b=c;
36             fb=fc;
37         }
38         else
39         {
40             a=c;
41             fa=fc;
42         }
43         c=(a+b)/2;
44     }
45     return c;
46 }

这个题目要用while语句实现才可以通过。下面的代码不能通过。(一直没懂什么原因……)下面这段是不行的。

 1 #include<stdio.h>
 2 #include<math.h>
 3 double f(double x);
 4 double fun(double a,double b,double e);
 5 int main()
 6 {
 7     double e,x1,x2,x;
 8     x1=-10;x2=10;
 9     scanf("%lf",&e);
10     x=fun(x1,x2,e);
11     printf("%.8lf\n",x);
12     return 0;
13 }
14 double f(double x)
15 {
16     double y;
17     y=2*x*x*x-4*x*x+3*x-6;
18     return y;
19 }
20 double fun(double a,double b,double e)
21 {
22     double fa,fb,fc,c;
23     fa=f(a);
24     fb=f(b);
25     c=(a+b)/2;
26     do
27     {
28         c=(a+b)/2;
29         fc=f(c);
30         if(fc==0)
31         {
32             break;
33         }
34         else if(fc*fa<0)
35         {
36             b=c;
37             fb=fc;
38         }
39         else
40         {
41             a=c;
42             fa=fc;
43         }
44     }while(fabs(b-a)>e);
45     return c;
46 }

 

下面的代码是通过了的。
 1 #include<iostream>
 2 #include<cmath>
 3 #include<iomanip>
 4 using namespace std;
 5 double f(double x);
 6 double fun(double a,double b,double e);
 7 int main()
 8 {
 9     double e,x1,x2,x;
10     x1=-10;x2=10;
11     cin>>e;    
12     x=fun(x1,x2,e);
13     cout<<setprecision(8)<<setiosflags(ios::fixed)<<x<<endl; 
14     return 0;
15 }
16 double f(double x) 
17 {
18     double y;
19     y=2*x*x*x-4*x*x+3*x-6;
20     return y;
21 }
22 double fun(double a,double b,double e)
23 {
24     double xm,y1,y2,ym;
25     y1=f(a);
26     y2=f(b);
27     xm=(a+b)/2;
28     while(fabs(a-b)>e)
29     {
30         ym=f(xm);
31         if (ym==0) return xm;
32         else  if(y1*ym<=0)
33         {
34                b=xm;
35                y2=ym;
36         }
37         else
38         {
39                a=xm;
40                y1=ym;
41         }
42         xm=(a+b)/2;                
43     }
44     return xm;
45 }

 

转载于:https://www.cnblogs.com/huashanqingzhu/p/3846553.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值