c程序设计第四版谭浩强
用牛顿迭代法求根。方程为ax3+bx2+cx+d=0 系数a,b,c,d的值依次为1,2,3,4, 由主函数输入。求x在1附近的一个实根。求出根后由主函数输出。
【c源程序】
#include <stdio.h>
#include <math.h>
int main()
{float solut(float a,float b,float c,float d);
float a,b,c,d;
printf("input a,b,c,d:");
scanf("%f,%f,%f,%f",&a,&b,&c,&d);
printf("x=%10.7f\n",solut(a,b,c,d));
return 0;
}
float solut(float a,float b,float c,float d)
{float x=1,x0,f,f1;
do
{x0=x;
f=((a*x0+b)*x0+c)*x0+d;
f1=(3*a*x0+2*b)*x0+c;
x=x0-f/f1;
}
while(fabs(x-x0)>=1e-3);
return(x);
}
本文链接:http://www.wb98.com/c/post/tanhaoqiang_7.12.html
本站文章搜索: