#***************************/
# シンプソン則 */
# coded by Y.Suganuma */
#***************************/
#***************/
# 関数値の計算 */
#***************/
snx = Proc.new { |x|
Math.sin(x)
}
#******************************************************/
# 数値積分(シンプソン則) */
# x1 : 下限 */
# x2 : 上限 */
# n : 分割数 */
# fun : 関数名(f) */
# return : 積分値 */
#******************************************************/
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.call(x)
x += h2
end
x = x1 + h2
while x < x2-h
s2 += fun.call(x)
x += h2
end
s = h * (fun.call(x1) + fun.call(x2) + 4.0 * s1 + 2.0 * s2) / 3.0
return s
end
pi2 = 2.0 * Math.atan(1.0)
y = simpson(0.0, pi2, 100, &snx)
printf("result %f\n", y)