NYOJ 503(牛顿迭代)

/*关键是迭代公式要收敛*/
#include<stdio.h>
#include<math.h>
double f(double x,double Y)
{
return 8*pow(x,4)-7*pow(x,3)+2*x*x+3*x+6-Y;
}
double f_(double x)
{
return 32*pow(x,3)-21*x*x+4*x+3;
}
int main()
{  
int T;double x1,x2,Y;
scanf("%d",&T);
while(T--)
{   
    scanf("%lf",&Y);
    if(f(0,Y)>0||f(100,Y)<0)
	{
		printf("No solution!\n");
		continue;
	}
    x1=0;x2=100;
    while(fabs(x1-x2)>1e-10)
    {
    x1=x2;
    x2=x1-f(x1,Y)/f_(x1); 
    }
    printf("%.4lf\n",x2);   
}
}  
        

  

posted @ 2012-05-24 21:05  加拿大小哥哥  阅读(237)  评论(0)    收藏  举报