simp0.c

来自「数值分析算法描述与习题解答」· C语言 代码 · 共 46 行

C
46
字号
#include "stdio.h"
   #include "math.h"
  main()
  {    int n;
	  double a,b, s,simp(),simpf();
     n=4;  a=0.0; b=1.0;  
   s=simp(a,b, simpf,n);
    printf("\n");
    printf("s=%e\n",s);
    printf("\n");
  }

 
  double simpf(x)
   double x;
  {double y;
    y=log(1.0+x)/(1.0+x*x);
    return(y);
  }


 
  double simp(a,b, f,n)
	  int n;
    double a,b, (*f)();
  { int  i;
    double h,t1,t2,t0,t,x;
     h=b-a/n;
    t0= (*f)(a)+(*f)(b) ;
    t1=t2=0;
    for(i=1;i<n;i++)
	 
          { 
		    x=a+i*h;
	      if(i%2==0)
            t2=t2+(*f)(x);
	     else
		t1=t1+(*f)(x);
          }
        t =(h*(t0+2*t2+4*t1))/3.0;
         
       
    return(t);
  }

  

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?