guozi6

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float fact(float a,float b,float c,float d);//定义
float a,b,c,d,x;
cout<<"请输入方程的系数:"<<endl;
cin>>a;//输入系数
cin>>b;
cin>>c;
cin>>d;
x=fact(a,b,c,d);//运用函数
cout<<"所求方程的根为x="<<x<<endl;

return 0;
}
float fact(float a,float b,float c,float d)//构造函数
{
float f,fd,x=0,h,x0=1.5;
do
{
x0=x;
f=a*x0*x0*x0+b*x0*x0+c*x0+d;
fd=3*a*x0*x0+2*b*x0+c;//求导数
h=f/fd;
x=x0-h;//运用牛顿迭代式
}while(fabs(x-x0)>=1e-5);//循环结束
return x;

}

 

posted on 2023-04-18 20:35  汀幻  阅读(21)  评论(0编辑  收藏  举报