fun函数设置循环,当x0-x1的绝对值小于0.000001循环结束。
#include
<stdio.h>
#include
<math.h>
float
fun()
{float
x,n=0.0,root
while(root>=0.000001||root<=-0.000001)
{
x=n
n=cos(x)
root=x-n
}
root=n
return
root
}
void
main()
{
float
f=fun()
printf("root=%f\n",f)
}
while 和do while是不同地第二个改成
#include <math.h>
#include <stdio.h>
main()
{float x,t,c
int a
scanf("%d",&a)
x=1
t=xx=(1.0/2)*(x+a/x)c=x-t
while(fabs(c)/x>1e-5)
{
t=xx=(1.0/2)*(x+a/x)c=x-t
}
printf("%f",x)
getch()
}
才会等价于第一个程序
迭代法就是让方程的解不断去逼近真实的解。这是一种数值计算方法。思路就是按上面的步骤,只设置两个x0,x1开始x0代表第一个值,x1代表第二值第一次迭代之后,让x0=x1,x1=新的值,这样x0代表第二个值,x1代表第三值以此类推。。。直到误差满足要求