#include #include using namespace std; long double fuzhu_1(double x,int t) { if(t==1) return 1.0/x; else return 1.0/(1+x*x); } long double fuzhu(double a,double b,int n,int t) { double h=(b-a)/n; double sum=0.0; for(int i=0;i<=n-1;i++) { sum=sum+fuzhu_1(a+i*h+h/2,t); } sum=sum*(h/2); return sum; } long double simpson(double a,double b,int n,int t) { double h=(b-a)/double(n); double sum; sum=fuzhu_1(a,t)+fuzhu_1(b,t)+4*fuzhu_1(a+h/2,t);//注意下标不同,不要搞错了 for(int i=1;i<=n-1;i++) { sum=sum+4*fuzhu_1(a+i*h+h/2,t)+2*fuzhu_1(a+i*h,t); } sum=sum*(h/6); return sum; }//求取ln2,pai long double romberg(double a,double b,int t) { double temp1[5000]; double temp2[5000]; double h=b-a; int i=2; temp1[0]=(fuzhu_1(a,t)+fuzhu_1(b,t))*h/2; temp2[0]=(temp1[0]/2)+fuzhu(a,b,1,t); temp2[1]=pow(4.0,1)/(pow(4.0,1)-1)*temp2[0]-1.0/(pow(4.0,1)-1)*temp1[0]; while(abs(temp2[i-1]-temp1[i-2])>pow(10.0,-9)) { //cout<