用二分法求方程的根

总时间限制: 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 }
复制代码

 

posted on   华山青竹  阅读(2156)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示