1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 //计算f(x)=1/x 在[1,3]上的积分 5 6 double f(double x){ 7 return 1.0/(x+2); 8 } 9 10 double f1(double x){ 11 return 1.0/(x+5); 12 } 13 double f2(double x){ 14 return 1.0/(x+7); 15 } 16 17 double f3(double x){ 18 return 1.0/(x+9); 19 } 20 double f4(double x){ 21 return 1.0/(x+11); 22 } 23 24 int main(){ 25 //运用三点高斯公式积分值 26 printf("%2.8f\n", (0.5555556*f( -0.7745967)+0.8888889*f( 0 )+0.5555556*f( 0.7745967) )); 27 28 //运用五点高斯公式求积分值 29 printf("%2.8f\n", 0.2369269*f(-0.9061799)+ 0.2369269*f(0.9061799)+0.4786287*f(-0.5384693)+0.4786287*f(0.5384693) + 30 0.5688889*f(0) ); 31 printf("\n"); 32 33 //将区间4等分,分别利用二点高斯公式,然后累加得积分值 34 printf("%2.8f\n",f1( -1.0/sqrt(3.0))+f1(1.0/sqrt(3.0)) ); 35 printf("%2.8f\n",f2( -1.0/sqrt(3.0))+f2(1.0/sqrt(3.0)) ); 36 printf("%2.8f\n",f3( -1.0/sqrt(3.0))+f3(1.0/sqrt(3.0)) ); 37 printf("%2.8f\n",f4( -1.0/sqrt(3.0))+f4(1.0/sqrt(3.0)) ); 38 39 printf("sum=%2.8f\n",f1( -1.0/sqrt(3.0))+f1(1.0/sqrt(3.0)) +f2( -1.0/sqrt(3.0))+f2(1.0/sqrt(3.0)) +f3( -1.0/sqrt(3.0))+f3(1.0/sqrt(3.0)) +f4( -1.0/sqrt(3.0))+f4(1.0/sqrt(3.0)) ); 40 41 }
时间: 2024-10-23 19:19:38