c语言 迭代法

如题所述

迭代法,是一种不断用变量的旧值递推新值的过程。

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);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-01-02
#include
#include
double
diedai(double
x,double
a);
double
sqrt(double
a);
double
sqrt(double
a)
{
double
x;
x=a/2;
return(diedai(x,a));
}
double
diedai(double
x,double
a)
{
double
y;
y=(x+a/x)/2;
/*迭代公式*/
if((fabs(x-y))>10e-6)
/*在不符合条件时进入迭代*/
{
y=diedai(y,a);
}
return(y);
}
void
main(void)
{
double
a,x;
printf("Input
a:\n");
/*输入要求平方根的数*/
scanf("%lf",&a);
x=sqrt(a);
printf("Result
%lf:\n",x);/*显示结果*/
}本回答被提问者采纳
相似回答