数値積分(台形則)

# -*- coding: UTF-8 -*-
from math import *
import numpy as np

############################################
# 数値積分(台形則)
#      x1 : 下限
#      x2 : 上限
#      n : 分割数
#      fn : 関数名(f)
#      return : 積分値
#      coded by Y.Suganuma
############################################

def daikei(x1, x2, n, fn) :

	s = 0.0
	h = (x2 - x1) / n
	x = x1

	for i1 in range(0, n-1) :
		x += h
		s += fn(x)

	s = 0.5 * h * (fn(x1) + fn(x2) + 2.0 * s)

	return s

----------------------------------

# -*- coding: UTF-8 -*-
import numpy as np
from math import *
from function import daikei

############################################
# 台形則
#      coded by Y.Suganuma
############################################

			# 関数値の計算
def snx(x) :
	return sin(x)
			# 計算
pi2 = 0.5 * pi
y = daikei(0.0, pi2, 100, snx)
print("result " + str(y))