编写程序,用牛顿切线法求方程f(x)=x^3+2x+10=0的近似实根r,迭代初值为-1,精确到0.0001.
问题描述:
编写程序,用牛顿切线法求方程f(x)=x^3+2x+10=0的近似实根r,迭代初值为-1,精确到0.0001.
答
#include
#include
float f(float x)
{
float y;
y=x*x*x+2*x+10;
return(y);
}
float f1(float x)
{
float y;
y=3*x*x+2;
return(y);
}
void main()
{
float x0=-1.0,x1;
while(fabs(x1-x0) >=0.0001)
{
x1=x0-f(x0)/f1(x0);
x0=x1;
}
printf("%f",x1);
}