零点定理与二分法的结合求一个函数的根
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
#define EPS 1e-7
double fun(double x){
return x*x*x - 5*x*x + 10*x - 80; // 一个在0 - 100之间只有一个根的方程
}
int main(){
double L = 0;
double R = 100;
double root;
root = L + (R - L)/2 ;
double y = fun(root);
while(fabs(y) > EPS){
if(y < 0) // 根据零点定理可知,根在左部右部
L = root;
else
R = root;
root = L + (R - L)/2 ;
y = fun(root);
}
cout << root <<endl;
return 0;
}