牛顿迭代法C语言(C++牛顿迭代法的迭代公式原理例题)
2022-11-10 19:02:43
(一)问题:用牛顿迭代法求方程的近似根。
(二)要求:1、牛顿迭代法公式:
2、求在x=1附近的近似根
3、当a=4,b=3,c=2,d=1的近似根
(三)程序代码:
#include<iostream>
#include<cmath>
using namespace std;
double f(double a,double b,double c,double d,double x)
{
return a*pow(x,3)+b*pow(x,2)+c*x+d;
}
double df(double a,double b,double c,double x)
{
return a*pow(x,2)+b*x+c;
}
int main()
{
double a,b,c,d,x0,x=1;
cout<<"Enter a,b,c,d:";
cin>>a>>b>>c>>d;
do
{
x0=x;
x=x0-f(a,b,c,d,x0)/df(a,b,c,x0);
cout<<"x0="<<x0<<ends<<ends<<"x="<<x<<endl;
}
while(fabs(x-x0)>=1e-6);
cout<<a<<"x*x*x+"<<b<<"x*x+"<<c<<"x+"<<d<<"=0的近似根为x="<<x<<endl;
return 0;
}
免责声明:本文由用户上传,如有侵权请联系删除!