package nine;
public class Nine {
private double a;
private double b;
private double e;
private int n;
public Nine(){
a = 0;
b = 0;
n = 0;
e = 0;
}
public Nine(double a,double b,int n,double e){
this.a = a;
this.b = b;
this.n = n;
this.e = e;
}
public void Tn(int i){
double sum = 0;
double h = (b-a)/n;
for(int k= 1;k<n;k++){
double x = a + k*h;
sum += fx(x,i);
}
sum = sum*h + (fx(a,i)+fx(b,i))*h/2;
System.out.println("复化梯形公式求积分:"+sum+" \tn:"+n);
}
public void Simpson(int i){
double sum = 0;
double sum1 = 0;
double sum2 = 0;
double h = (b-a)/n;
for(int k = 1;k<=n;k++){
double x = a + (k-0.5)*h;
sum1+= fx(x,i);
}
for(int k=1;k<n;k++){
double x = a +k*h;
sum2+=fx(x,i);
}
sum+= sum1*h*2/3 + sum2*h/3 + (fx(a,i)+fx(b,i))*h/6;
System.out.println("复化Simpson公式求积分:"+sum+"\tn:"+n);
}
public void Romberg(int i){
double sum = (64*C(2*n,i)-C(n,i))/63;
System.out.println("Romberg公式求积分:"+sum+"\tn:"+n);
}
public void GetN(int i){
int number = 1;
double x1 = T(number,i);
double x2 = T(2*number,i);
for( number = 1;Math.abs(x2-x1)>=3*e;number*=2){
x1 = x2;
x2 = T(4*number,i);
}
System.out.println("复化梯形公式(逐次分半)在达到精度为"+e+"时,n:"+number+"步长:"+(b-a)/number);
number = 1;
x1 = S(number,i);
x2 = S(2*number,i);
for( number = 1;Math.abs(x2-x1)>15*e;number*=2){
x1 = x2;
x2 = S(4*number,i);
}
System.out.println("复化Simpon公式在达到精度为"+e+"时,n:"+number+"步长:"+(b-a)/number);
number = 1;
x1 = C(number,i);
x2 = C(2*number,i);
for( number = 1;Math.abs(x2-x1)>63*e;number*=2){
x1 = x2;
x2 = C(4*number,i);
}
System.out.println("Romberg公式在达到精度为"+e+"时,n:"+number+"步长:"+(b-a)/number);
}
public double T(int n,int i){
if(n==1){
return (b-a)*(fx(a,i)+fx(b,i))/2;
}
else{
double h = (b-a)*2/n;
double sum=0;
for(int k = 0;k<n/2;k++){
double x = a + (k+0.5)*h;
sum+= fx(x,i);
}
return T(n/2,i)/2+sum*h/2;
}
}
public double S(int n,int i){
return (4*T(2*n,i)-T(n,i))/3;
}
public double C(int n,int i){
return (16*S(2*n,i)-S(n,i))/15;
}
public double fx(double x,int i){
if(i==1){
return Math.pow(4-Math.sin(x)*Math.sin(x), 0.5);
}
if(i==2){
if(x==0){
return 1;
}
return Math.sin(x)/x;
}
if(i==3){
return Math.pow(Math.E, x)/(4+x*x);
}
if(i==4){
return Math.log(1+x)/(1+x*x);
}
return 0;
}
}