n =100 , a=-5 ,b=5
/* ex4-3.java based on Trapezoidol Rule is
* used for computing definite integral with
* domain [a,b] with n even-grids.
例題4-3 利用梯形積分法 成常態分布f(x)的面積
*/
import java.util.Scanner;
public class Main {
double F(double x1) {
return (1.0/Math.exp(x1*x1/2));
}
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
Main fun = new Main();
int n,i;
double a,b,x,tn,h,sum=0.0;
n=scanner.nextInt();
a=scanner.nextFloat();
b=scanner.nextFloat();
h=(b-a)/n;
x=a;
for(i=1;i<=n-1;i++) {
x=x+h;
sum=sum+fun.F(Math.abs(x));
}
tn=(1.0/Math.sqrt(2*Math.PI))*(h/2.0)*(fun.F(Math.abs(a))+fun.F(Math.abs(b))+2.0*sum);
System.out.printf("T%d=%10.6f\n",n,tn);
}
}
STDIN輸入資料
100 -5 5
輸出畫面
T100= 0.999999
沒有留言:
張貼留言