二分法解方程

原文链接: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

上一篇:OSPF虚链路配置实验


下一篇:动态路由协议之RIP路由协议