#include<stdio.h>
#include<math.h>
double f(double x)
{
double y;
y=0.5*log(4-x);
return y;
}
int main(void)
{
double x0,x1,eps,m,n,a;
printf("please input x0=\n");
scanf("%lf",&x0);
printf("please input eps=\n");
scanf("%lf",&eps);
printf("please input m=\n");
scanf("%lf",&m);
n=0;
do
{
x1=f(x0);
if(fabs(x1-x0)>m)
{
printf("迭代失败,程序终止\n");
return 0;
}
a=x0;
x0=x1;
n++;
}
while((x0-a)>eps);
printf("x0=%lf,n=%lf",x0,n);
return 0;
}
#include<math.h>
double f(double x)
{
double y;
y=0.5*log(4-x);
return y;
}
int main(void)
{
double x0,x1,eps,m,n,a;
printf("please input x0=\n");
scanf("%lf",&x0);
printf("please input eps=\n");
scanf("%lf",&eps);
printf("please input m=\n");
scanf("%lf",&m);
n=0;
do
{
x1=f(x0);
if(fabs(x1-x0)>m)
{
printf("迭代失败,程序终止\n");
return 0;
}
a=x0;
x0=x1;
n++;
}
while((x0-a)>eps);
printf("x0=%lf,n=%lf",x0,n);
return 0;
}