简介
Newton-Cotes 公式是指在等距节点下使用 Lagrange 插值多项式建立的数值求积公式。
设将求积区间 [a, b] 划分为 n 等分,选取等分点
x
i
=
a
+
i
h
,
h
=
b
−
a
n
,
i
=
0
,
1
,
2
,
⋅
⋅
⋅
,
n
x_i = a + ih , h=\frac{b-a}{n}, i = 0, 1, 2, · · · , n
xi=a+ih,h=nb−a,i=0,1,2,⋅⋅⋅,n
作为求积节点构造求积公式
∫
a
b
f
(
x
)
d
x
≈
(
b
−
a
)
∑
i
=
0
n
λ
i
f
(
x
i
)
\ \int_a^b{f\left( x \right) dx\approx \left( b-a \right) \sum_{i=0}^n{\lambda _if\left( x_i \right)}}
∫abf(x)dx≈(b−a)i=0∑nλif(xi)
这种求积公式称为 Newton-Cotes 公式
其公式求积系数如下:
实例
代码
#include<iostream>
using namespace std;
#define MAXSIZE 7
double a,b,h,core[100],f[100];
int n;
double c[MAXSIZE][MAXSIZE+2 ] = {{2, 1,1}, {6, 1, 4,1}, {8, 1, 3,3,1}, {90, 7,
32, 12,32,7}, {288, 19, 75, 50,50,75,19}, {840, 41, 216, 27, 272,27,216,41},
{17280, 751, 3577,1323, 2989,2989,1323,3577,751}};
void input()
{
cout<<"输入积分区间:"<<endl;
cin>>a>>b;
cout<<"输入想要将区间几等分:"<<endl;
cin>>n;
cout<<"输入横坐标对应的纵坐标的值:"<<endl;
h = (b-a)/n;
for(int i=0;i<=n;i++)
{
cout<<"x["<<i<<"] = " << a + i * h <<" y["<<i<<"] = ";
cin>>f[i];
}
}
double cau()
{
double sum=0;
for(int i=0;i<=n;i++)
{
double x = c[n-1][i+1] ;
sum += x * f[i];
}
sum = sum / c[n-1][0] * (b-a) ;
return sum;
}
int main()
{
input();
double re = cau() ;
cout<<"输出结果:"<<re<<endl;
return 0;
}
结果: