//迭代法求平方根公式,這讓我想起研一的數值分析的計算實習題
//看來出來混總是要還的,估計將來要用到數值分析里面算法,然后將編程之
#include<stdio.h>
#include<math.h>
void main()
{//先得輸入a
float a,t,xn;//不能用xn1,還是采用一個中間變量t
printf("please input a:\n");
scanf("%f",&a);
//這次好像要編一個直到型的程序了,就是while,但感覺還是for用著順
for(xn=a;fabs(xn-t)>=0.00001;)
{t=xn;
xn=(xn+a/xn)/2;
//xn從什么開始選比較好呢,要不從a本身開始吧
}
printf("xn=%f\n",xn);
}
|