牛顿迭代法求根
#include<stdio.h>#include<math.h>
void main(){
float f(float x);
float f1(float x);
float x2,x1=0.5;
x2=x1-f(x1)/f1(x1);
while(fabs(x2-x1)>1e-3)
{
x1=x2;
x2=x1-f(x1)/f1(x1);
}
printf("%f",x2);
}
float f(float x)
{
float a=(((3*x+4)*x+5)*x)*x-6;
return a;
}
float f1(float x)
{
float a=((12*x+12)*x+10)*x;
return a;
}
输出结果是表达式 3x^4+4x^3+5x^2+5x-6=0在0.5附近实根
调试结果是0.777557不知道对不对?
----------------解决方案--------------------------------------------------------