# 二分逼近/牛顿迭代——一元高次非线性方程求解

#include"iostream"
#include"cstdio"
#include"cstring"
#include"cstdlib"
#include"cmath"
#define precision 0.00000001

using namespace std;

int count=0;

//一元多次方程求解
double function(double x)
{
return 2*x*x+3.2*x-1.8;
}

double binary(double x,double y)
{
double mid=(x+y)/2.0;
while(fabs(x-y)>precision)
{
if(function(mid)==0) break;
if(function(x)*function(mid)<0) y=mid;
else x=mid;
mid=(x+y)/2;
count++;
}
return mid;
}

double function2(double x)   //返回导数，高精度代倒数
{
return (function(x+0.000005)-function(x-0.000005))/0.00001;
}

double newtown(double x)
{
double x1=x-function(x)/function2(x);
while(fabs(x1-x)>precision)
{
if(function(x1)==0) break;
x=x1;
x1=x-function(x)/function2(x);
count++;
}
return x1;
}

int main()
{
cout<<binary(-0.8,8.0)<<endl;
cout<<count<<endl;
count=0;
cout<<newtown(8)<<endl;
cout<<count<<endl;
count=0;
cout<<newtown(-8)<<endl;
cout<<count<<endl;
return 0;
} 

• 本文已收录于以下专栏：

举报原因： 您举报文章：二分逼近/牛顿迭代——一元高次非线性方程求解 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)