其他分享
首页 > 其他分享> > 二分法解方程

二分法解方程

作者:互联网

原文链接:http://www.cnblogs.com/GavinDai/archive/2011/11/13/2247621.html

#include <cmath>
#include <iostream>
using namespace std;
float f(float x)
{
 return x * x * x - 5 * x *x + 16 * x - 80;
}

void main(void)
{
 float x1, x2, x0, f0, f1, f2;
 do
 {
  cout<<"Input x1, x2\n";
  cin>>x1>>x2;
  f1 = f(x1);
  f2 = f(x2);
 } while (f1 * f2 > 0);
 do
 {
  x0 = ( x1 + x2) / 2;
  f0 = f(x0);
  if ((f0*f1)>0)
  {
   x1 = x0;
   f1 = f0;
  }else
  {
   x2 = x0;
   f2 = f0;
  }
 } while (fabs(f0)>=0.0001);
 cout<<"x = "<< x0 <<endl;
}

 

转载于:https://www.cnblogs.com/GavinDai/archive/2011/11/13/2247621.html

标签:f0,f1,f2,二分法,x2,解方程,x0,x1
来源: https://blog.csdn.net/weixin_30736301/article/details/99218607