#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;
}
二分逼近/牛顿迭代——一元高次非线性方程求解
最新推荐文章于 2021-05-22 21:11:11 发布