File indexing completed on 2026-04-09 07:48:51
0001
0002 """
0003 https://en.wikipedia.org/wiki/Trapezoidal_rule
0004
0005 Integral ( f(x) ) dx ~ (b - a) 0.5(f(a) + f(b))
0006 a->b
0007
0008
0009 https://numpy.org/doc/stable/reference/generated/numpy.trapz.html
0010
0011 """
0012 import numpy as np
0013 import matplotlib.pyplot as plt
0014
0015 if __name__ == '__main__':
0016
0017 x = np.arange(10, dtype=np.float64)
0018 y = x*x
0019 i_ = lambda _:np.power(_, 3)/3.
0020
0021 area_approx = np.trapz( y, x )
0022 area = i_( x[-1] ) - i_(x[0])
0023
0024 fig, ax = plt.subplots()
0025 fig.suptitle( " np.trapz(y,x) area_approx : %10.4f area : %10.4f " % (area_approx, area) )
0026 ax.plot( x, y )
0027 ax.scatter( x, y )
0028
0029 fig.show()
0030
0031
0032