您的位置:首页 > 其它

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

2016-08-29 15:15 337 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: