# -*- coding: UTF-8 -*- from math import * import numpy as np ############################################ # 数値積分(シンプソン則) # x1 : 下限 # x2 : 上限 # n : 分割数 # fun : 関数名(f) # return : 積分値 # coded by Y.Suganuma ############################################ def simpson(x1, x2, n, fun) : s1 = 0.0 s2 = 0.0 h = (x2 - x1) / n h2 = 2.0 * h x = x1 + h while x < x2 : s1 += fun(x) x += h2 x = x1 + h2 while x < x2 - h : s2 += fun(x) x += h2 s = h * (fun(x1) + fun(x2) + 4.0 * s1 + 2.0 * s2) / 3.0 return s; ---------------------------------- # -*- coding: UTF-8 -*- import numpy as np from math import * from function import simpson ############################################ # シンプソン則 # coded by Y.Suganuma ############################################ # 関数値の計算 def snx(x) : return sin(x) # 計算 pi2 = 0.5 * pi y = simpson(0.0, pi2, 100, snx) print("result " + str(y))