选用三种数值积分方法计算圆周率pi .
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/27 15:36:53
选用三种数值积分方法计算圆周率pi .
选用三种数值积分方法计算圆周率pi .
选用三种数值积分方法计算圆周率pi .
用4了四种方法,另外还加了个龙格贝.
大人大量给分吧~
#include
using namespace std;
double getPI0(int h){
double l = 1.0/h;
int i,j;
double s = 0;
for(i = 0; i < h; i++){
s += l*(4/(1+((2*i+1)*l/2)*((2*i+1)*l/2)));
}
return s;
}
double getPI1(double h){
double l = 1.0/h;
int i,j;
double s = 0;
for(i = 0; i < h; i++){
s += l*(4/((1+i*l*i*l)))/2+l*4/(1+((i+1)*l*(i+1)*l))/2;
}
return s;
}
double getPI2(double h){
double l = 1.0/h;
int i,j;
double a,b;
double s = 0;
for(i = 0; i < h; i++){
a = i*l;
b = i*l+l;
double fa = 4/(1+a*a);
double fb = 4/(1+b*b);
double fab = 4/(1+(a+b)*(a+b)/4);
s += l*(fa+4*fab+fb)/6;
}
return s;
}
double fj(double a,int j){
int i;
double r = 1;
for(i = 0; i < j; i++){
r *= a;
}
return r;
}
double longBeiGe(double r){//r为给定的误差限
double * tm;
double * t;
tm = new double[1];
t = new double [2];
tm[0] = getPI1(1);
t[0] = getPI1(2);
t[1] = (4*t[0]-tm[0])/3;
int i = 1,j = 0;
while(t[i]-tm[i-1]>r){
tm = t;i++;
t = new double [i+1];
t[0] = getPI1(fj(2,i+1));
for(j = 1; j