/************************
* 用二分法求方程
* f(x)=x^3-2x-5=0
* 在区间[2,3]内的根
*************************/
#include<stdio.h>
#include<math.h>
#include<conio.h>
float f(float x) {
float a;
a = x * x * x - 2 * x - 5;
return a;
}
int main() {
float a, b, e, x;
printf("\nplease input data a = ");
scanf("%f", &a);
printf("\nplease input data b = ");
scanf("%f", &b);
printf("\nplease input data eps = ");
scanf("%f", &e);
if (f(a) * f(b) < 0) {
while (f(x) != 0) {
x = (a + b) / 2;
if (f(a) * f(x) < 0) {
b = x;
if (abs(b - a) < e) {
break;
} else
continue;
} else {
a = x;
if (abs(b - a) < e) {
break;
} else
continue;
}
}
printf("\n");
x = (a + b) / 2;
printf("The root of f(x) = 0 is x =\t%f\n", x);
} else
printf("\n Not root ! Afresh input!\n");
return 0;
}
计算方法之二分法求方程根
最新推荐文章于 2022-07-01 17:33:48 发布