选用三种数值积分方法计算圆周率pi .

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/25 12:37:32
选用三种数值积分方法计算圆周率pi .
xTn@+Nl"6y, v,NrvE Z$ڒ"Ub DL&NU uϹw3R?^OLJ ۿ#7;1z2~g}^t(lLlPE:ҦtcL'vu:ty?t2(狰3Iݣގۭ[ n@xF :ƎAX/J +|8BSK3.AΓf/2h<%#vGT@MSd2TtLUM.%}+i,-ZCI?ts\%U'qܿoރ378"N4@u33"Gg`CR2y2V2ToI!Zzij\䃿vЪkᅓ4#}ɨqk]p.`Y_-Z=Bx"n<:>w8[\gBLTZg`KU8{-4̠  X[V!S'Ixsnn Tf-VҰȝ[:1|f8rooW&1

选用三种数值积分方法计算圆周率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