算法原理:
C程序代码:
# include<stdio.h>
# include<math.h>
main(){
float f(float x0);
float f1(float x1);
float sishewuru(int m,float x2);
int i,n;
float x,y;
printf("\n the iterations and the inicial number is:");
scanf("%d,%f",&n,&x);
for(i=0;i<n;i++)
{
y=x-f(x)/f1(x);
x=y;
}
y=sishewuru(3,y);
printf("%f",y);
}
/* the function f(x)=x^3+7*x-1 */
float f(float x0){
float y;
y=pow(x0,3)+7*x0-1;
return y;
}
/* the function f(x)=3*pow(x,2)+7 */
float f1(float x1){
float y;
y=3*pow(x1,2)+7;
return y;
}
float sishewuru(int n,float x2){
int z1;
float y,z;
z1=(int)(x2*pow(10,n));
y=z1/(pow(10,n));
if(fabs(x2-y)>(5*pow(10,-n-1)))
y=(z1+1)/pow(10,n);
else
if (fabs(x2-y)==(5*pow(10,-n-1)))
if(z1%2!=0)
y=(z1+1)/pow(10,n);
return y;}