// 台形則による数値積分 import java.util.Random; import java.io.*; public class Int_expb{ public static void main(String[] args) throws IOException { double x0=0.0, x, y, h, s=0.0, sreal, serror; String str; BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Input: Number of trials"); str = buf.readLine(); long ntrial = Long.parseLong(str); h=1.0/ntrial; x = x0; y = Math.exp(-x); s = s + 0.5*y; for(int j=1; j <= ntrial-1; j++){ x = x + h; y = Math.exp(-x); s = s + y; } x = x + h; y = Math.exp(-x); s = s + 0.5*y; s = s / ntrial; sreal = 1.0 - Math.exp(-1.0); serror = s - sreal; System.out.println(" ntrial="+ntrial); System.out.println(" s= "+s); System.out.println(" sreal= "+sreal); System.out.println(" serror= "+serror); } }