首页 技术 正文
技术 2022年11月9日
0 收藏 997 点赞 4,670 浏览 2703 个字

Simpson(辛普森)公式和梯形公式是求数值积分中很重要的两个公式,可以帮助我们使用计算机求解数值积分,而在使用过程中也有多种方式,比如复合公式和变步长公式。这里分别给出其简单实现(C++版):

1、复合公式:

 #include<iostream>
#include<iomanip>
#include <cmath>
using namespace std; double Simpson(double a,double b,double n);
double Compound_Trapezoid(double a,double b,double n); int main()
{
int n;
double a, b;
cout << "区间数n:";
cin >> n;
cout << "区间端点a:";
cin >> a;
cout<<"区间端点b:";
cin >> b;
cout<<setprecision()<<Simpson(a,b,n)<<endl;
cout<<setprecision()<<Compound_Trapezoid(a,b,n)<<endl;
getchar();
getchar();
return ;
} /*
* Simpson算法
*/
double Simpson(double a,double b,double n)
{
double h=(b-a)/n;
double Sn=exp(a)-exp(b);
for (double x=a+h;x<=b;x+=h)
{
Sn+=*exp(x-h/)+*exp(x);
}
Sn *= h/;
return Sn;
} /*
* 复合梯形算法
*/
double Compound_Trapezoid(double a,double b,double n)
{
double h=(b-a)/n;
double Sn=exp(a)+exp(b);
for(double x=a+h;x<b;x+=h)
{
Sn += * exp(x);
}
Sn *= h/;
return Sn;
}

2、变步长公式

 /*
* e^x,1/x求1到3的积分
* 精确到1E-5
*/
#include<iostream>
#include<iomanip>
#include<cmath> using namespace std; //变步长梯形法
double ex_Variable_step_size_trape(double ,double ,double);
double x_Variable_step_size_trape(double ,double ,double);
//变步长Simpson法
double ex_Variable_step_size_Simpson(double ,double ,double);
double x_Variable_step_size_Simpson(double ,double ,double);
//Romberg法
//double Romberg(); int main()
{
//左端点a,右端点b,允许误差E
double a,b,E;
cout << "请输入左端点a:";
cin >> a;
cout << "请输右端点b:";
cin >> b;
cout << "请输入允许误差E:";
cin >> E;
cout << "变步长梯形(e^x):" << setiosflags(ios::fixed) << setprecision() << ex_Variable_step_size_trape(a,b,E) << endl;
cout << "变步长Simpson(e^x):" << setiosflags(ios::fixed) << setprecision() << ex_Variable_step_size_Simpson(a,b,E) << endl;
cout << "变步长梯形(1/x):" << setiosflags(ios::fixed) << setprecision() << x_Variable_step_size_trape(a,b,E) << endl;
cout << "变步长Simpson(1/x):" << setiosflags(ios::fixed) << setprecision() << x_Variable_step_size_Simpson(a,b,E) << endl;
getchar();
getchar();
return ;
} double ex_Variable_step_size_trape(double a,double b,double E)
{
double h = b - a, e = ,T2 = ;
double T1 = h/ * (exp(a) + exp(b));
do
{
double S = , x = a + h/;
do
{
S += exp(x);
x += h;
}while(x < b);
T2 = T1/ + h/ * S;
e = (T2 > T1)?(T2 - T1):(T1 - T2);
h = h/;
T1 = T2;
}while(e > E);
return T2;
} double x_Variable_step_size_trape(double a,double b,double E)
{
double h = b - a, e = ,T2 = ;
double T1 = h/ * (/a + /b);
do
{
double S = , x = a + h/;
do
{
S += /x;
x += h;
}while(x < b);
T2 = T1/ + h/ * S;
e = (T2 > T1)?(T2 - T1):(T1 - T2);
h = h/;
T1 = T2;
}while(e > E);
return T2;
} double ex_Variable_step_size_Simpson(double a,double b,double E)
{
double h = b - a, e = ,T2 = ;
double T1 = h/ * (exp(a) - exp(b));
do
{
double S = , x = a + h/;
do
{
S += * exp(x);
x += h/;
S += * exp(x);
x += h/;
}while(x <= b);
T2 = T1/ + h/ * S;
e = (T2 > T1)?(T2 - T1):(T1 - T2);
h = h/;
T1 = T2;
}while(e > E);
return T2;
} double x_Variable_step_size_Simpson(double a,double b,double E)
{
double h = b - a, e = ,T2 = ;
double T1 = h/ * (/a - /b);
do
{
double S = , x = a + h/;
do
{
S += * /x;
x += h/;
S += * /x;
x += h/;
}while(x <= b);
T2 = T1/ + h/ * S;
e = (T2 > T1)?(T2 - T1):(T1 - T2);
h = h/;
T1 = T2;
}while(e > E);
return T2;
}

作者:耑新新,发布于  博客园

转载请注明出处,欢迎邮件交流:zhuanxinxin@aliyun.com

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905