integrals from numpy arrays: np.trapz

23 views
Skip to first unread message

Jay Wang

unread,
Oct 12, 2017, 2:38:34 PM10/12/17
to com...@googlegroups.com
One can find the crude integral from a pair of numpy arrays quickly. Numpy provides a function, trapz, to do this using the trapezoid rule, given a pair of x and y arrays. Example:
import numpy as np
x
= np.linspace(0., 1., 100)
y
= x*x
np
.trapz(y, x)
The results is 0.33335033840084344, close to the exact value 1/3. This may be useful for Exercise E4.12.
For more accurate calculation of integrals, consider using quad from the scipy.integrate library.

Reply all
Reply to author
Forward
0 new messages