公式pai=4*∫10dx/(1+x2)
把[0.1]分成10等份,用两中方法分别计算
=======================================================
class work2 {
public static void main (String args[]){
double h = 0.1;//参数h
double m1 =0 ;//复化梯形求和求和
double n =10;
double I=0;//最后结果
double H_2 =0.2;
double m =5 ;//由于n=2m.n=10,所以m=5
double s1=0;//复化抛物线第一个数组求和
double s2=0;//复化抛物线第二个数组求和
double ss[] = new double[10];//复化梯形公式计算中的求和单元
double ss1[]=new double[5];//复化抛物线中用到的第一个数组
double ss2[]=new double[4];//复化抛物线中用到的第二个数组
double k[]={0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9};//10等分[0,1]
for(int i=0;i<10;i++){
ss[i] = 1/(1+k[i]*k[i]);
}
//teat pass
for(int i =0;i<10;i++){
m1 = m1 +ss[i];
}
//test pass
I = H_2*(0.5+2*m1);//0.5是f(b)
// --------------------test code
System.out.println("===复化梯形公式计算(包含测试及结果)===");
System.out.println("复化梯形公式单项结果");
for(int i =0;i<10;i++){
System.out.println(ss[i]);
}
System.out.println("结果求和 m is "+ m1);
System.out.println("结果是 "+ I);
System.out.println("==================================");
// ------------------------------------------------
//==开始复化抛物线公式计算======
System.out.println("复化抛物线公式计算");
//由于抛物线运算对数组的操作比较复杂,所以我重新定义数组k[]
double k_2[] = {0,0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9};
//这样数组k就和实际中的对应起来了,哦哈哈
for(int i =1;i<=m;i++){
int n1 = 2*i-1;
ss1[i-1] = 1/(1+k_2[n1]*k_2[n1]);
System.out.println(ss1[i-1]);//测试计算是否正确,
}
System.out.println("运算正确");
System.out.println("第二个数组计算");
for(int i = 1;i<=(m-1);i++){
int n1 = 2*i;
ss2[i-1]=1/(1+k_2[n1]*k_2[n1]);
System.out.println(ss2[i-1]);//测试计算是否正确,
}
System.out.println("运算正确");
//对2个数组求和
for(int i =0;i<5;i++){
s1 = s1 +ss1[i];
}
System.out.println("ss1求和是"+ s1);
for(int i =0;i<4;i++){
s2 = s2 +ss2[i];
}
System.out.println("ss2求和是"+ s2);
System.out.println("运算正确");
I = (h/3)*(1+0.5+4*s1+2*s2);
System.out.println("结果是 "+ I);
//抛物线法不知道正不正确,书上没有答案
}
}
===========================================