📄 2fenfa.c
字号:
/*用对分法求x*x*x-2*x*x-4*x-7=0在3到4的根*/
#define EPS 5e-3
#define DELTA 1e-6
#include"conio.h"
#include"stdio.h"
#include"math.h"
float Bisection(float,float,float(*f)(float));
float f(float) ;
main()
{ float a=3,b=4;
float x;
x=Bisection(a,b,f);
printf("\nThe root of the equation is %f",x);
getch();
}
float Bisection(float a,float b,float(*f)(float))
{ float c,fc,fa=f(a),fb=f(b) ;
int n=1;
printf("c\t\tf(c)\n") ;
while(1)
{ if(fa*fb>0) exit();
c=(a+b)/2,fc=f(c);
if(fabs(fc)<DELTA) break ;
else if(fa*fc<0) {b=c;fb=fc;}
else {a=c;fa=fc;}
if(b-a<EPS) break;
printf("%d\t\t%f\n",n++,c,fc) ; }
return c;
}
float f(float x)
{
return x*x*x-2*x*x-4*x-7;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -