/********************************/ /* シンプソン則 */ /* coded by Y.Suganuma */ /********************************/ import java.io.*; public class Simpson { public static void main(String args[]) throws IOException { double y; Kansu kn = new Kansu(0); y = App.simpson(0.0, 0.5*Math.PI, 100, kn); System.out.println("result " + y); } } ---------------------------------------------- /********************/ /* 関数値の計算 */ /********************/ class Kansu { private int sw; // コンストラクタ Kansu (int s) {sw = s;} // double型関数 double snx(double x) { double y = 0.0; switch (sw) { // 関数値(f(x))の計算 case 0: y = Math.sin(x); break; } return y; } } ------------------------------------------ /****************************/ /* 科学技術系算用の手法 */ /****************************/ class App { /***********************************************************/ /* 数値積分(シンプソン則) */ /* x1 : 下限 */ /* x2 : 上限 */ /* n : 分割数 */ /* kn : 関数値を計算するクラスのオブジェクト */ /* return : 積分値 */ /***********************************************************/ static double simpson(double x1, double x2, int n, Kansu kn) { double s1 = 0.0; double s2 = 0.0; double s, x, h, h2; h = (x2 - x1) / n; h2 = 2.0 * h; for (x = x1+h; x < x2; x += h2) s1 += kn.snx(x); for (x = x1+h2; x < x2-h; x += h2) s2 += kn.snx(x); s = h * (kn.snx(x1) + kn.snx(x2) + 4.0 * s1 + 2.0 * s2) / 3.0; return s; } }