SciPy Points

chapter


Scipy The integratemodule provides a number of numerical integration, e.g., a multiple integral, double integral, triple integral, multiple integration, like Gaussian integration.

Here are some commonly integration function.

A re-integration

SciPy integral module, Quad function is an important function for seeking a double integral. For example, in a given range into the b, a function f (x) seeking a double integral.

$$\int_a^bf(x)dx$$

The general form of a quad scipy.integrate.quad(f, a, b), which fis the quadrature of the function name, aand brespectively lower and upper.

Examples

Let's look at an example of a Gaussian function, integrating the range of 0-5.

First need to define the function $ → f (x) = e ^ {- x2} $, which may be used to represent a lambda expression, and then use a quad double integral method to evaluate it.

import scipy.integrate
from numpy import exp
f = lambda x:exp(-x**2)
i = scipy.integrate.quad(f, 0, 5)
print(i)

Export

(0.8862269254513955, 2.3183115159980698e-14)

quad function returns two values, the first value is the integral value, the second value is the value of the integral absolute error estimates.

Examples

If the integral of the function f with coefficient parameters, namely:

$$I(a,b) = \int_0^1(ax^2+b)dx$$

So a and b can be passed through the quad function args:

from scipy.integrate import quad

def f(x, a, b):
    return a * (x ** 2) + b

ret = quad(f, 0, 1, args=(3, 1))
print (ret)

Export

(2.0, 2.220446049250313e-14)

Re-integration

To calculate the double integral, triple integral, multiple integrals, use dblquad, tplquad and nquad function.

Double integrals

dblquad general form is scipy.integrate.dblquad(func, a, b, gfun, hfun), where funcis the name to be integral function a, bis the lower limit of the x variable gfun, hfunthe upper limit of the variable y defined function name.

Examples

Seeking double integral:

$$\int_0^{\frac{1}{2}}dy\int_0^{\sqrt[]{1-4y^2}}19xydx$$

We use lambda expressions defined functions f, gand h. Note that, in many cases g, and hit may be constant, but even gand hconstant, must also be defined as a function.

import scipy.integrate
from numpy import exp
from math import sqrt
f = lambda x, y : 19*x*y
g = lambda x : 0
h = lambda y : sqrt(1-4*y**2)
i = scipy.integrate.dblquad(f, 0, 0.5, g, h)
print (i)

Export

(0.59375, 2.029716563995638e-14)

In addition to the methods described above, Scipy the integratemodule integration There are many other methods, e.g. nquad, seeking for multiple integration. However, most of the scenes quad and dblquad suffice.

Guess you like

Origin www.cnblogs.com/jinbuqi/p/11811145.html