编程题 编写函数fun,它的功能是:利用以下所示的简单迭代方法求方程式cos(x)-x=0的一个实根。
=cos( )
迭代步骤如下:
(1)取x1初值为0.0;
(2)x0=x1,把x1的值赋给x0;
(3)x1=cos(x0),求出一个新的x1;
(4)若x0-x1,的绝对值小于0.000001,则执行步骤(5),否则执行步骤(2);
(5)所求x1就是方程cos(x)-x=0的一个实根,作为函数值返回。
程序将输出结果Root=0.739085。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:#include <conio.h>
#include <math.h>
#include <stdio.h>
float fun()
{
}
main()
{
FILE *out;
float f = fun();
printf("Root=%f\n", f);
out = fopen("out.dat", "w");
fprintf(out, "%f", f);
fclose(out);
}
答案是: float fun()
{
float x1=0.0,x0;
do
{
x0=x1;
x1=cos(x0);
}
while(fabs(x0-x1)>=le-6);
return x1;
}