利用牛顿迭代法求方程根的函数c++

如图求个代码

第(1)问代码如下

#define fun1 5*p(x,9)+7*p(x,8)+10*p(x,7)+156*p(x,4)+89*p(x,3)+90*p(x,2)+101*x+50//函数
#define fun1_ 45*p(x,8)+56*p(x,7)+70*p(x,6)+156*4*p(x,3)+89*3*p(x,2)+180*p(x,1)+101//导数
#include<iostream>
#include<cmath>
using namespace std;
double p(double x,int n);
int main()
{
double x=0;
while(fabs(fun1)>0.0000001)//精度
            x=x-(fun1)/(fun1_);
cout<<x<<endl;
}
double p(double x,int n)
{
double s=1;
for(;n>0;n--)
s*=x;
return s;
}

 第(2)问

#define e 2.718281828459
#define fun2 x*pow(e,x)+p(x,5)*p(sin(x),3)
#define fun2_ pow(e,x)+x*pow(e,x)+4*p(x,4)*p(sin(x),3)+3*p(sin(x),2)*cos(x)*p(x,5)
#include<iostream>
#include<cmath>
using namespace std;
double p(double x,int n);
int main()
{
double x=10;
while(fabs(fun2)>0.0000001)//精度
x=x-(fun2)/(fun2_);
cout<<x<<endl;
}
double p(double x,int n)
{
double s=1;
for(;n>0;n--)
s*=x;
return s;
}

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答