用弦截法求方程的解

来源:互联网 发布:淘宝如何快速升钻 编辑:程序博客网 时间:2024/04/29 22:17

#include <iostream>
#include <iomanip>
#include <cmath>
double f(double x);
using namespace std;
int main()
{
 double x0=0.5,x1=1,x2;
 cout<<x0<<endl;
 cout<<x1<<endl;
 for(;fabs(x1-x0)>=0.000001;)
 {
  x2=x1-f(x1)*(x1-x0)/(f(x1)-f(x0));
     x0=x1;
  x1=x2;
        cout<<x2<<endl;
 }
 return 0;
}

double f(double x)
{
 double y;
 y=x*x*x*exp(x)-2;
 return(y);
}

原创粉丝点击