fixed_quad#

sunkit_spex.integrate.fixed_quad(func, a, b, n=5, args=(), func_kwargs={})[source]#

Compute a definite integral using fixed-order Gaussian quadrature.

Integrate func from a to b using Gaussian quadrature of order n.

This is a modified version of scipy.integrate.fixed_qaud

Parameters:
  • func (callable) – A Python function or method to integrate (must accept vector inputs). If integrating a vector-valued function, the returned array must have shape (..., len(x)).

  • a (float or np.array) – Lower limit of integration.

  • b (float or np.array) – Upper limit of integration.

  • n (int, optional) – Order of quadrature integration. Default is 5.

  • args (tuple, optional) – Extra arguments to pass to function, if any.

  • func_kwargs (dict, optional) – Keyword arguments to the function to be integrated

Returns:

val – Gaussian quadrature approximation to the integral

Return type:

float

Examples

>>> from sunkit_spex.integrate  import fixed_quad
>>> f = lambda x: x**8
>>> fixed_quad(f,0.0,1.0,n=4)
array(0.11108844)
>>> fixed_quad(f,0.0,1.0,n=5)
array(0.11111111)
>>> print(1/9.0)  # analytical result
0.1111111111111111
>>> fixed_quad(f, [0, 1, 2], [1, 2, 3], n=5)
array([1.11111111e-01, 5.67777778e+01, 2.13011111e+03])
>>> 1/9, (2**9 - 1**9)/9, (3**9 - 2**9)/9 # analytical result
(0.1111111111111111, 56.77777777777778, 2130.1111111111113)
>>> fixed_quad(np.cos,0.0,np.pi/2,n=4)
array(0.99999998)
>>> fixed_quad(np.cos,0.0,np.pi/2,n=5)
 array(1.)
>>> np.sin(np.pi/2)-np.sin(0)  # analytical result
1.0