为验证上一篇《C++ 函数怎样作形参?实例:微分法求定积分》里的函数,在网上找了四道定积分例题:
验证代码及结果如下:
#include <iostream> #include <cmath> using namespace std; #define N 100000 #define E 2.7182818 #define Pi 3.1415926 class CIFun { public: double f1(double x){ return (sin(x)*tan(x)*tan(x))/(3+cos(x))+log(2-x); } double f2(double x){ return asin(sqrt(x))/sqrt(x-x*x); } double f3(double x){ return sqrt(1-sin(2*x)); } double f4(double x){ return sin(x)/(1+sin(x)+cos(x)); } }; double CIntegral(double a, double b, double (CIFun::*pf)(double)) { CIFun fn; double r = 0, d = (b-a)/N; for(int i=0; i<N; i++) r+=d * (fn.*pf)(a+i*d+d/2); return r; } int main(void) { double result=0; cout << "f1(x) = (sin(x)*tan(x)*tan(x))/(3+cos(x))+log(2-x);" << endl; cout << "CInterral函数 计算的值 = "; result = CIntegral(-1, 1, &CIFun::f1); cout << result << endl; cout << "题目自带的答案:-2-Ln3 = " << -2-log(3) << endl << endl; cout << "f2(x) = asin(sqrt(x))/sqrt(x-x*x);" << endl; cout << "CInterral函数 计算的值 = "; result = CIntegral(0.25, 0.75, &CIFun::f2); cout << result << endl; cout << "题目自带的答案:Pi*Pi/12 = " << Pi*Pi/12 << endl << endl; cout << "f3(x) = sqrt(1-sin(2*x));" << endl; cout << "CInterral函数 计算的值 = "; result = CIntegral(0, Pi/2, &CIFun::f3); cout << result << endl; cout << "题目自带的答案:2*Sqrt(2)-2 = " << 2*sqrt(2)-2 << endl << endl; cout << "f4(x) = sin(x)/(1+sin(x)+cos(x));" << endl; cout << "CInterral函数 计算的值 = "; result = CIntegral(0, Pi/2, &CIFun::f4); cout << result << endl; cout << "题目自带的答案:[Pi-2*Ln(2)]/4 = " << 0.25*Pi-0.5*log(2) << endl; return 0; }
运算结果:
f1(x) = (sin(x)*tan(x)*tan(x))/(3+cos(x))+log(2-x);
CInterral函数 计算的值 = 1.29584
题目自带的答案:-2-Ln3 = -3.09861
f2(x) = asin(sqrt(x))/sqrt(x-x*x);
CInterral函数 计算的值 = 0.822467
题目自带的答案:Pi*Pi/12 = 0.822467
f3(x) = sqrt(1-sin(2*x));
CInterral函数 计算的值 = 0.828427
题目自带的答案:2*Sqrt(2)-2 = 0.828427
f4(x) = sin(x)/(1+sin(x)+cos(x));
CInterral函数 计算的值 = 0.438825
题目自带的答案:[Pi-2*Ln(2)]/4 = 0.438825
--------------------------------
Process exited after 0.4932 seconds with return value 0
请按任意键继续. . .
明显后三题是对的,第1题提供解题的人粗心做错了,看了一下解题过程他有一步忘了 d(2-x)=-1 ,所以少了一个负号:
重新计算正确答案 -2+3*Ln3,与函数值对比正确:
#include <iostream> #include <cmath> using namespace std; #define N 100000 #define E 2.7182818 double func(double x) { return (sin(x)*tan(x)*tan(x))/(3+cos(x))+log(2-x); } double Integral(double a, double b, double f(double)) { double r = 0, d = (b-a)/N; for(int i=0; i<N; i++) r+=d * f(a+i*d+d/2); return r; } int main(void) { cout << "函数的解:" << Integral(-1, 1, func) << endl; cout << "正确答案:-2+3*Ln3 = " << -2+3*log(3) << endl; return 0; } /* 函数的解:1.29584 正确答案:-2+3*Ln3 = 1.29584 -------------------------------- Process exited after 0.7949 seconds with return value 0 请按任意键继续. . . */
无意中发现了一个做错的题目,是不是很意外,有点小开森啊 ^_^ ^_^ ^_^
最后我们把精度提高到小数点后16位,切割分数增至1亿,再看测试结果:
#include <iostream> #include <iomanip> #include <cmath> using namespace std; #define N 100000000 #define E 2.71828182845904523536 #define Pi 3.14159265358979323846 class CIFun { public: double f1(double x){ return (sin(x)*tan(x)*tan(x))/(3+cos(x))+log(2-x); } double f2(double x){ return asin(sqrt(x))/sqrt(x-x*x); } double f3(double x){ return sqrt(1-sin(2*x)); } double f4(double x){ return sin(x)/(1+sin(x)+cos(x)); } }; double CIntegral(double a, double b, double (CIFun::*pf)(double)) { CIFun fn; double r = 0, d = (b-a)/N; for(int i=0; i<N; i++) r+=d * (fn.*pf)(a+i*d+d/2); return r; } int main(void) { double result=0; result = CIntegral(-1, 1, &CIFun::f1); cout << setprecision(16) << result << "<=函数的返回值" <<endl; cout << 3*log(3)-2 << "<=答案计算结果:3*ln3-2" << endl << endl; result = CIntegral(0.25, 0.75, &CIFun::f2); cout << result << "<=函数的返回值" <<endl; cout << Pi*Pi/12 << "<=答案计算结果:Pi*Pi/12" << endl << endl; result = CIntegral(0, Pi/2, &CIFun::f3); cout << result << "<=函数的返回值" << endl; cout << 2*sqrt(2)-2 << "<=答案计算结果:2*sqrt(2)-2" << endl << endl; result = CIntegral(0, Pi/2, &CIFun::f4); cout << result << "<=函数的返回值" << endl; cout << 0.25*Pi-0.5*log(2) << "<=答案计算结果:[Pi-2*ln(2)]/4" << endl; return 0; }
测试结果精度能保证到小数点后11位,却以牺牲时间为代价得来的,共计用时约51秒。
/*
1.295836866004461<=函数的返回值
1.295836866004329<=答案计算结果:3*ln3-2
0.822467033424098<=函数的返回值
0.8224670334241132<=答案计算结果:Pi*Pi/12
0.8284271247458824<=函数的返回值
0.8284271247461903<=答案计算结果:2*sqrt(2)-2
0.438824573117462<=函数的返回值
0.4388245731174756<=答案计算结果:[Pi-2*ln(2)]/4
--------------------------------
Process exited after 50.96 seconds with return value 0
请按任意键继续. . .
*/