1 #include<stdio.h> 2 #include<math.h> 3 #define maxlen 100 4 #define eps 0.5*1e-5 5 double a=0; 6 double b=1; 7 double f(double x){ 8 return 4/(1+x*x); 9 } 10 double t(int n){ 11 int i; 12 double sum, h = (b - a) / n; 13 for (i = 1; i < n; i++) 14 sum += f(a + i * h); 15 sum += (f(a) + f(b)) / 2; 16 return (h * sum); 17 } 18 int main() 19 { 20 printf("k\t T[k]\t\t S[k]\t\t C[k]\t\t R[k]\n"); 21 double T[maxlen]; 22 double S[maxlen]; 23 double C[maxlen]; 24 double R[maxlen]; 25 int k=-1; 26 while(1){ 27 k++; 28 int z=pow(2,k); 29 T[k]=t(z); 30 if(k>0){ 31 S[k]=T[k]*4/3-T[k-1]/3; 32 } 33 if(k>1){ 34 C[k]=S[k]*16/15-S[k-1]/15; 35 } 36 if(k>2){ 37 R[k]=C[k]*64/63-C[k-1]/63; 38 } 39 printf("%d\t %lf\t %lf\t %lf\t %lf\n",k+1,T[k],S[k],C[k],R[k]); 40 if(k>3){ 41 if (abs(R[k]-R[k-1])<eps){ 42 break; 43 } 44 } 45 } 46 printf("a:0,b:1 f(x):4/(1+x*x) eps:0.5*1e-5 the result:%lf\n",R[k]); 47 return 0; 48 }
原文地址:https://www.cnblogs.com/DixinFan/p/10117175.html
时间: 2024-11-08 09:54:43