作者在 2012-10-28 18:28:51 发布以下内容
//:牛顿迭代法
#include <stdio.h>
#include <math.h>
int main(){
float a, b, c, d;
float x1, x2, f1, f2;
printf("输入4个数:");
scanf("%f%f%f%f", &a, &b, &c, &d);
x1 = 0;
x2 = 1;
while(fabs(x1-x2)>1e-6){
x1 = x2;
f1 = a * x1* x1 * x1 + b * x1 * x1 + c * x1 + d;
f2 = 3 * a * x1 * x1 + 2 * b * x1 + c;
x2 = x1 - f1/f2;
}
printf("%fn", x2);
return 0;
}
#include <stdio.h>
#include <math.h>
int main(){
float a, b, c, d;
float x1, x2, f1, f2;
printf("输入4个数:");
scanf("%f%f%f%f", &a, &b, &c, &d);
x1 = 0;
x2 = 1;
while(fabs(x1-x2)>1e-6){
x1 = x2;
f1 = a * x1* x1 * x1 + b * x1 * x1 + c * x1 + d;
f2 = 3 * a * x1 * x1 + 2 * b * x1 + c;
x2 = x1 - f1/f2;
}
printf("%fn", x2);
return 0;
}