In [29]: e, f = myfun(m.log, 0, 3, 0.1)

Traceback (most recent call last):

File "<ipython-input-29-a0bd72d32f6b>", line 1, in <module>

e, f = myfun(m.log, 0, 3, 0.1)

File "//filestore.soton.ac.uk/users/oh1m12/mydocuments/20172018/Lecture6/lecture6_high.py", line 95, in myfun


ValueError: math domain error

In [30]: e, f = myfun(m.log, 0.1, 3, 0.1)

In [31]: pylab.plot(a, b, 'or', c, d, 'gs', e, f, 'b*', ms=20)


[<matplotlib.lines.Line2D at 0x2626599ecf8>,

<matplotlib.lines.Line2D at 0x2626599ef98>,

<matplotlib.lines.Line2D at 0x2626599eeb8>]

In [32]: e, f = myfun(x2, 0.1, 3, 0.1)

In [32]:

In [33]: pylab.plot(a, b, 'or', c, d, 'gs', e, f, 'b*', ms=20)


[<matplotlib.lines.Line2D at 0x26265a0f198>,

<matplotlib.lines.Line2D at 0x262659a9160>,

<matplotlib.lines.Line2D at 0x26265a0fcf8>]

In [34]: e, f = myfun(x2, -3, 3, 0.1)

In [35]: pylab.plot(a, b, 'or', c, d, 'gs', e, f, 'b*', ms=20)


[<matplotlib.lines.Line2D at 0x26265a72b38>,

<matplotlib.lines.Line2D at 0x26265a155c0>,

<matplotlib.lines.Line2D at 0x26265a72cf8>]

In [36]: runfile('//filestore.soton.ac.uk/users/oh1m12/mydocuments/20172018/Lecture6/lecture6_high.py', wdir='//filestore.soton.ac.uk/users/oh1m12/mydocuments/20172018/Lecture6')

In [37]: x, y = myfun(x2, xi=-3, xf=3, dx=0.1)

In [38]: pylab.plot(x, y, '*', markersize=20)

Out[38]: [<matplotlib.lines.Line2D at 0x26265aecf60>]

In [39]: x, y = myfun(x2, -3, 3, 0.1)

In [40]: pylab.plot(x, y, '*', markersize=20)

Out[40]: [<matplotlib.lines.Line2D at 0x26265b574a8>]

In [41]: x, y = myfun(x2)

In [42]: pylab.plot(x, y, '*', markersize=20)

Out[42]: [<matplotlib.lines.Line2D at 0x26265bc7198>]

In [43]: x1, y1 = myfun(x2, -1, 1, 0.1)

In [44]: pylab.plot(x, y, '*', x1, y1, 'rd', markersize=20)


[<matplotlib.lines.Line2D at 0x26265c2f3c8>,

<matplotlib.lines.Line2D at 0x26265c2f588>]

In [45]: from scipy.integrate import quad

In [46]: help(quad)

Help on function quad in module scipy.integrate.quadpack:

quad(func, a, b, args=(), full_output=0, epsabs=1.49e-08, epsrel=1.49e-08, limit=50, points=None, weight=None, wvar=None, wopts=None, maxp1=50, limlst=50)

Compute a definite integral.

Integrate func from `a` to `b` (possibly infinite interval) using a

technique from the Fortran library QUADPACK.



func : function

A Python function or method to integrate. If `func` takes many

arguments, it is integrated along the axis corresponding to the

first argument.

If the user desires improved integration performance, then f may

instead be a ``ctypes`` function of the form:

f(int n, double args[n]),

where ``args`` is an array of function arguments and ``n`` is the

length of ``args``. ``f.argtypes`` should be set to

``(c_int, c_double)``, and ``f.restype`` should be ``(c_double,)``.

a : float

Lower limit of integration (use -numpy.inf for -infinity).

b : float

Upper limit of integration (use numpy.inf for +infinity).

args : tuple, optional

Extra arguments to pass to `func`.

full_output : int, optional

Non-zero to return a dictionary of integration information.

If non-zero, warning messages are also suppressed and the

message is appended to the output tuple.



y : float

The integral of func from `a` to `b`.

abserr : float

An estimate of the absolute error in the result.

infodict : dict

A dictionary containing additional information.

Run scipy.integrate.quad_explain() for more information.

message :

A convergence message.

explain :

Appended only with 'cos' or 'sin' weighting and infinite

integration limits, it contains an explanation of the codes in


Other Parameters


epsabs : float or int, optional

Absolute error tolerance.

epsrel : float or int, optional

Relative error tolerance.

limit : float or int, optional

An upper bound on the number of subintervals used in the adaptive


points : (sequence of floats,ints), optional

A sequence of break points in the bounded integration interval

where local difficulties of the integrand may occur (e.g.,

singularities, discontinuities). The sequence does not have

to be sorted.

weight : float or int, optional

String indicating weighting function. Full explanation for this

and the remaining arguments can be found below.

wvar : optional

Variables for use with weighting functions.

wopts : optional

Optional input for reusing Chebyshev moments.

maxp1 : float or int, optional

An upper bound on the number of Chebyshev moments.

limlst : int, optional

Upper bound on the number of cycles (>=3) for use with a sinusoidal

weighting and an infinite end-point.

See Also


dblquad : double integral

tplquad : triple integral

nquad : n-dimensional integrals (uses `quad` recursively)

fixed_quad : fixed-order Gaussian quadrature

quadrature : adaptive Gaussian quadrature

odeint : ODE integrator

ode : ODE integrator

simps : integrator for sampled data

romb : integrator for sampled data

scipy.special : for coefficients and roots of orthogonal polynomials



**Extra information for quad() inputs and outputs**

If full_output is non-zero, then the third output argument

(infodict) is a dictionary with entries as tabulated below. For

infinite limits, the range is transformed to (0,1) and the

optional outputs are given with respect to this transformed range.

Let M be the input argument limit and let K be infodict['last'].

The entries are:


The number of function evaluations.


The number, K, of subintervals produced in the subdivision process.


A rank-1 array of length M, the first K elements of which are the

left end points of the subintervals in the partition of the

integration range.


A rank-1 array of length M, the first K elements of which are the

right end points of the subintervals.


A rank-1 array of length M, the first K elements of which are the

integral approximations on the subintervals.


A rank-1 array of length M, the first K elements of which are the

moduli of the absolute error estimates on the subintervals.


A rank-1 integer array of length M, the first L elements of

which are pointers to the error estimates over the subintervals

with ``L=K`` if ``K<=M/2+2`` or ``L=M+1-K`` otherwise. Let I be the

sequence ``infodict['iord']`` and let E be the sequence

``infodict['elist']``. Then ``E[I[1]], ..., E[I[L]]`` forms a

decreasing sequence.

If the input argument points is provided (i.e. it is not None),

the following additional outputs are placed in the output

dictionary. Assume the points sequence is of length P.


A rank-1 array of length P+2 containing the integration limits

and the break points of the intervals in ascending order.

This is an array giving the subintervals over which integration

will occur.


A rank-1 integer array of length M (=limit), containing the

subdivision levels of the subintervals, i.e., if (aa,bb) is a

subinterval of ``(pts[1], pts[2])`` where ``pts[0]`` and ``pts[2]``

are adjacent elements of ``infodict['pts']``, then (aa,bb) has level l

if ``|bb-aa| = |pts[2]-pts[1]| * 2**(-l)``.


A rank-1 integer array of length P+2. After the first integration

over the intervals (pts[1], pts[2]), the error estimates over some

of the intervals may have been increased artificially in order to

put their subdivision forward. This array has ones in slots

corresponding to the subintervals for which this happens.

**Weighting the integrand**

The input variables, *weight* and *wvar*, are used to weight the

integrand by a select list of functions. Different integration

methods are used to compute the integral with these weighting

functions. The possible values of weight and the corresponding

weighting functions are.

========== =================================== =====================

``weight`` Weight function used ``wvar``

========== =================================== =====================

'cos' cos(w*x) wvar = w

'sin' sin(w*x) wvar = w

'alg' g(x) = ((x-a)**alpha)*((b-x)**beta) wvar = (alpha, beta)

'alg-loga' g(x)*log(x-a) wvar = (alpha, beta)

'alg-logb' g(x)*log(b-x) wvar = (alpha, beta)

'alg-log' g(x)*log(x-a)*log(b-x) wvar = (alpha, beta)

'cauchy' 1/(x-c) wvar = c

========== =================================== =====================

wvar holds the parameter w, (alpha, beta), or c depending on the weight

selected. In these expressions, a and b are the integration limits.

For the 'cos' and 'sin' weighting, additional inputs and outputs are


For finite integration limits, the integration is performed using a

Clenshaw-Curtis method which uses Chebyshev moments. For repeated

calculations, these moments are saved in the output dictionary:


The maximum level of Chebyshev moments that have been computed,

i.e., if ``M_c`` is ``infodict['momcom']`` then the moments have been

computed for intervals of length ``|b-a| * 2**(-l)``,



A rank-1 integer array of length M(=limit), containing the

subdivision levels of the subintervals, i.e., an element of this

array is equal to l if the corresponding subinterval is

``|b-a|* 2**(-l)``.


A rank-2 array of shape (25, maxp1) containing the computed

Chebyshev moments. These can be passed on to an integration

over the same interval by passing this array as the second

element of the sequence wopts and passing infodict['momcom'] as

the first element.

If one of the integration limits is infinite, then a Fourier integral is

computed (assuming w neq 0). If full_output is 1 and a numerical error

is encountered, besides the error message attached to the output tuple,

a dictionary is also appended to the output tuple which translates the

error codes in the array ``info['ierlst']`` to English messages. The

output information dictionary contains the following entries instead of

'last', 'alist', 'blist', 'rlist', and 'elist':


The number of subintervals needed for the integration (call it ``K_f``).


A rank-1 array of length M_f=limlst, whose first ``K_f`` elements

contain the integral contribution over the interval

``(a+(k-1)c, a+kc)`` where ``c = (2*floor(|w|) + 1) * pi / |w|``

and ``k=1,2,...,K_f``.


A rank-1 array of length ``M_f`` containing the error estimate

corresponding to the interval in the same position in



A rank-1 integer array of length ``M_f`` containing an error flag

corresponding to the interval in the same position in

``infodict['rslist']``. See the explanation dictionary (last entry

in the output tuple) for the meaning of the codes.



Calculate :math:`\int^4_0 x^2 dx` and compare with an analytic result

>>> from scipy import integrate

>>> x2 = lambda x: x**2

>>> integrate.quad(x2, 0, 4)

(21.333333333333332, 2.3684757858670003e-13)

>>> print(4**3 / 3.) # analytical result


Calculate :math:`\int^\infty_0 e^{-x} dx`

>>> invexp = lambda x: np.exp(-x)

>>> integrate.quad(invexp, 0, np.inf)

(1.0, 5.842605999138044e-11)

>>> f = lambda x,a : a*x

>>> y, err = integrate.quad(f, 0, 1, args=(1,))

>>> y


>>> y, err = integrate.quad(f, 0, 1, args=(3,))

>>> y


Calculate :math:`\int^1_0 x^2 + y^2 dx` with ctypes, holding

y parameter as 1::

testlib.c =>

double func(int n, double args[n]){

return args[0]*args[0] + args[1]*args[1];}

compile to library testlib.*


from scipy import integrate

import ctypes

lib = ctypes.CDLL('/home/.../testlib.*') #use absolute path

lib.func.restype = ctypes.c_double

lib.func.argtypes = (ctypes.c_int,ctypes.c_double)


#(1.3333333333333333, 1.4802973661668752e-14)

print((1.0**3/3.0 + 1.0) - (0.0**3/3.0 + 0.0)) #Analytic result

# 1.3333333333333333

In [47]: quad(x2, 0, 1)

Out[47]: (0.33333333333333337, 3.700743415417189e-15)

In [48]: quad(x2, 0, 1, full_output=1)




{'alist': array([ 0.00000000e+000, 1.29526084e-311, 1.29518723e-311,

1.29524507e-311, 1.29526061e-311, 1.29518723e-311,

1.29526100e-311, 1.29526100e-311, 1.29526041e-311,

1.29525950e-311, 1.29525950e-311, 1.29526041e-311,

1.29524507e-311, 1.29526061e-311, 1.29518723e-311,

1.29526084e-311, 1.29518723e-311, 1.29524507e-311,

1.29524507e-311, 1.29524507e-311, 1.29526100e-311,

1.29525950e-311, 1.29526100e-311, 1.29525950e-311,

1.29526033e-311, 1.29526074e-311, 1.29526098e-311,

1.29525950e-311, 1.29525950e-311, 1.29526061e-311,

1.29518723e-311, 1.29526100e-311, 1.29526100e-311,

1.29526100e-311, 1.29526074e-311, 1.29526041e-311,

1.29524507e-311, 1.29526041e-311, 1.29526061e-311,

1.29525950e-311, 1.29526100e-311, 1.29526098e-311,

1.29526100e-311, 1.29525950e-311, 1.29525950e-311,

1.29526098e-311, 1.29518723e-311, 1.29526041e-311,

1.29526041e-311, 1.29526084e-311]),

'blist': array([ 1.00000000e+000, 1.29524507e-311, 1.29526061e-311,

1.29524507e-311, 1.29524714e-311, 1.29518723e-311,

1.29524507e-311, 1.29524507e-311, 1.29524507e-311,

1.29526061e-311, 1.29518723e-311, 1.29525950e-311,

1.29526040e-311, 1.29518723e-311, 1.29524507e-311,

1.29524507e-311, 1.29526074e-311, 1.29525950e-311,

1.29526051e-311, 1.29518723e-311, 1.29524507e-311,

1.29525950e-311, 1.29518723e-311, 1.29526070e-311,

1.29518723e-311, 1.29526062e-311, 1.29518723e-311,

1.29525950e-311, 1.29524507e-311, 1.29526033e-311,

1.29518723e-311, 1.29524507e-311, 1.29525949e-311,

1.29518723e-311, 1.29526062e-311, 1.29526064e-311,

1.29526061e-311, 1.29526070e-311, 1.29526070e-311,

1.29526070e-311, 1.29526070e-311, 1.29526070e-311,

1.29526070e-311, 1.29526062e-311, 1.29526074e-311,

1.29526062e-311, 1.29526074e-311, 1.29526074e-311,

1.29526062e-311, 1.29526062e-311]),

'elist': array([ 3.70074342e-015, 1.29524507e-311, 1.29525848e-311,

1.29524507e-311, 1.29525832e-311, 1.29518723e-311,

1.29524507e-311, 1.29524507e-311, 1.29524507e-311,

1.29525848e-311, 1.29518723e-311, 1.29524507e-311,

1.29525828e-311, 1.29518723e-311, 1.29524507e-311,

1.29524507e-311, 1.29524518e-311, 1.29524507e-311,

1.29525877e-311, 1.29518723e-311, 1.29524507e-311,

1.29524507e-311, 1.29518723e-311, 1.29525877e-311,

1.29518723e-311, 1.29525848e-311, 1.29518723e-311,

1.29524507e-311, 1.29524507e-311, 1.29525901e-311,

1.29518723e-311, 1.29524507e-311, 1.29525902e-311,

1.29518723e-311, 1.29525848e-311, 1.29525902e-311,

1.29525848e-311, 1.29525900e-311, 1.29525900e-311,

1.29525900e-311, 1.29525900e-311, 1.29525900e-311,

1.29525900e-311, 1.29525900e-311, 1.29524518e-311,

1.29525900e-311, 1.29524518e-311, 1.29524518e-311,

1.29525900e-311, 1.29525900e-311]),

'iord': array([ 1, 610, 1707411760, 610, 1564470328,

610, 1707069000, 610, 1564890480, 610,

1706059400, 610, 1564470328, 610, 1706005888,

610, 1564890480, 610, 1707409648, 610,

1564470328, 610, 1707067240, 610, 1564890480,

610, 1675503280, 610, 1564470328, 610,

1707404400, 610, 1564890480, 610, 1707404272,

610, 1564470328, 610, 1707384592, 610,

1564890480, 610, 1707402608, 610, 1564470328,

610, 1707404528, 610, 1564602528, 610], dtype=int32),

'last': 1,

'neval': 21,

'rlist': array([ 3.33333333e-001, 1.29524507e-311, 1.29525950e-311,

1.29524507e-311, 1.29525817e-311, 1.29518723e-311,

1.29524507e-311, 1.29524507e-311, 1.29524507e-311,

1.29525950e-311, 1.29518723e-311, 1.29525950e-311,

1.29526040e-311, 1.29518723e-311, 1.29524507e-311,

1.29524507e-311, 1.29524518e-311, 1.29525950e-311,

1.29526008e-311, 1.29518723e-311, 1.29524507e-311,

1.29525950e-311, 1.29518723e-311, 1.29526031e-311,

1.29518723e-311, 1.29525950e-311, 1.29518723e-311,

1.29525950e-311, 1.29524507e-311, 1.29526033e-311,

1.29518723e-311, 1.29524507e-311, 1.29526022e-311,

1.29518723e-311, 1.29525950e-311, 1.29526022e-311,

1.29525950e-311, 1.29526031e-311, 1.29526031e-311,

1.29526031e-311, 1.29526031e-311, 1.29526031e-311,

1.29526031e-311, 1.29526031e-311, 1.29524518e-311,

1.29526031e-311, 1.29524518e-311, 1.29526041e-311,

1.29526028e-311, 1.29526028e-311])})

In [49]: x1, y1 = myfun(x2, -1, 1, 0.1)

In [50]: pylab.plot(x1, y1, 'ob', ms=20)

Out[50]: [<matplotlib.lines.Line2D at 0x26265c99d68>]

In [51]: x1, y1 = myfun(x2)

In [52]: pylab.plot(x1, y1, 'ob', ms=20)

Out[52]: [<matplotlib.lines.Line2D at 0x26265ceb1d0>]

In [53]: x1, y1 = myfun(x2, xi=-1)

In [54]: pylab.plot(x1, y1, 'ob', ms=20)

Out[54]: [<matplotlib.lines.Line2D at 0x26265d50908>]

In [55]: