#include<stdio.h>
#include<math.h>
double Resolve(double a,double b,double c,double d);
double f(double x,double a,double b,double c,double d);
double f1(double x,double a,double b,double c);
int main()
{
double a,b,c,d=0;
double y;
printf("input a b c d:");
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
y=Resolve(a,b,c,d);
printf("output:\n%.4lf\n",y);
return 0;
}
double Resolve(double a,double b,double c,double d)
{
double x0=1,x1,x2,x3;
x1=x0-f(x0,a,b,c,d)/f1(x0,a,b,c);
x2=x1-f(x1,a,b,c,d)/f1(x1,a,b,c);
x3=x2-f(x2,a,b,c,d)/f1(x2,a,b,c);
if(f(x1,a,b,c,d)<(double)1/(10*10*10*10*10))
return x1;
else
{
if(f(x2,a,b,c,d)<(double)1/(10*10*10*10*10))
return x2;
else
return x3;
}
}
double f(double x,double a,double b,double c,double d)
{
double f;
f=((a*x+b)*x+c)*x+d;
return f;
}
double f1(double x,double a,double b,double c)
{
double f1;
f1=(3*a*x+2*b)*x+c;
return f1;
}
牛顿迭代法求方程的根
最新推荐文章于 2022-05-10 00:27:53 发布