C++ 用自定义函数验证高等数学的定积分例题

为验证上一篇《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
请按任意键继续. . .
*/ 

 

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hann Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值