按照题目要求用二分法求方程的实根的C语言程序如下
#include <stdio.h>
#include <math.h>
double f(double x) {return 2*x*x*x - 4*x*x + x - 2;}
int main(){
double root, x1 = 0, x2 = 10,y;
root = x1+(x2-x1)/2;
y = f(root);
while(fabs(y) > 1e-6) {
if(y > 0) x2 = root;
else x1 = root;
root = x1+(x2 - x1)/2;
y = f(root);
}
printf("%.2f\n",root);
return 0;
}
追问请问大神,为什么根的区间是0-10呢?
追答区间定0到10是因为0代入方程小于0,10代入方程大于0,在这个区间才会有方程的根.
追问哦哦,这呀,谢谢大神啦!