Python 中的积分微积分

2024 年 8 月 29 日 | 阅读 17 分钟

本教程演示了如何利用 Python 中常用的科学计算库来计算实值函数在实值变量作用下,在给定区间内的连续积分。由于本网站专注于计算,这里使用的积分计算方法主要是数值方法;也提供了一些具体的解析方法。

本文使用了示例;每个段落都包含一个需要计算的积分示例以及一段使用相关库计算该积分的 Python 代码片段。

除了 NumPy 和 Python 3 版本之外,本教程中讨论的每个代码片段还需要额外的库,如 SciPy 和 SymPy。

通过 SciPy 进行积分

单变量函数积分(定积分)

定积分是微积分中的一个数学运算符,如果它作用于由实值变量构成且位于区间 [a,b](这是定义域的一个子集)内的实值函数,则计算给定函数在定义的区间 [a,b] 内的图形所围成区域的面积值。

SciPy 库提供了多种专门用于计算此类实值函数积分的数值方法。本节将通过一系列示例来说明如何应用这些数值方法。鼓励读者阅读官方 SciPy 文档以获取这些有用方法的完整列表。

使用 quad 函数计算积分

下面提供了使用 Scipy quad 函数计算积分值的 Python 代码片段。

代码

输出

Integration of a single variable function using the quad function
Finding the integral value of 2x^2e^-x bounded between x = 1 to x = 5
The integration of the given function is 
3.1801863337820993 having an error of 3.53071609036315e-14

使用 fixed_quad 函数计算积分

下面提供了使用 Scipy fixed_quad 函数计算积分值的 Python 代码片段。

代码

输出

Integration of a single variable function using the fixed_quad function
Finding the integral value of 2x^2e^-x bounded between x = 1 to x = 5
The integration of the given function is 
3.1801789114559855

使用 quadrature 函数计算积分

下面提供了使用 Scipy quadrature 函数计算积分值的 Python 代码片段。

代码

输出

Integration of a single variable function using the fixed_quad function
Finding the integral value of 2x^2e^-x bounded between x = 1 to x = 5
The integration of the given function is 
3.1801863337773124 having an error of 8.067120305099706e-10

使用 Romberg 函数计算积分

下面提供了使用 Scipy romberg 函数计算积分值的 Python 代码片段。

代码

输出

Integration of a single variable function using the Romberg function
Finding the integral value of 2x^2e^-x bounded between x = 1 to x = 5
The integration of the given function is 
3.1801863337821397

使用 trapezoid 函数计算积分

由于梯形法的积分技术是固定采样函数,因此算法首先均匀地划分积分区间,并计算区间内每个离散 x 值对应的函数值 y,然后再将离散值对 x 和 y 传递给积分算法。

下面提供了使用 Scipy trapezoid 函数计算积分值的 Python 代码片段。

代码

输出

Integration of a single variable function using the trapezoid function
Finding the integral value of 2x^2e^-x bounded between x = 1 to x = 5
The integration of the given function is 
3.180182964799505

使用 cumulative_trapezoid 函数计算积分

此函数也是一个固定采样函数。因此,我们在 trapezoid 函数中应用的原理也适用于此处。

下面提供了使用 Scipy cumulative trapezoid 函数计算积分值的 Python 代码片段。

代码

输出

Integration of a single variable function using the cumulative_trapezoid function
Finding the integral value of 2x^2e^-x bounded between x = 1 to x = 5
The integration of the given function is 
3.180182964799503

使用 Simpson 函数计算积分

此函数也是一个固定采样函数。因此,我们需要提供 x 和 y 数据点。

下面提供了使用 Scipy Simpson 函数计算积分值的 Python 代码片段。

代码

输出

Integration of a single variable function using the Simpson function
Finding the integral value of 2x^2e^-x bounded between x = 1 to x = 5
The integration of the given function is 
3.18018296480732

使用 quad 方法对单变量函数进行积分(积分上限为无穷大)

现在我们将计算一个单变量函数在无穷积分上限的积分。

下面提供了使用 Scipy cumulative trapezoid 函数计算积分值的 Python 代码片段。

代码

输出

Finding the integral value of 2x^2e^-x bounded between x = 1 to x = inf
The integration of the given function is 
4.203848996896752e-08

计算平面曲线弧的长度

众所周知,函数在给定边界之间形成的弧的长度,也称为平面曲线,可以使用给定单变量函数的积分来计算。

使用 quad 计算显式表示的平面曲线弧的长度。

下面是使用 SciPy 库的 quad 函数计算显式表示的平面曲线弧长度的 Python 代码示例。

代码

输出

Finding the length of a planar curve arc
Length of the arc of the curve y=e^(-x^3) from x = -2 to x = 2
The arclength of the given function is 2982.8283737550946 with an error equal to 8.847369866821285e-06

计算给定参数化平面曲线的弧长

描述具有一维输入的多维输出的函数的一种方法是通过空间中的平面曲线。这些函数接收的输入是一个参数,输出是参数化函数。在多变量微积分中,找到产生特定曲线的多变量函数的参数形式是一个重要的话题。

下面是使用 Scipy quad 函数计算参数化形式表示的平面曲线弧长度的 Python 代码示例。

代码

输出

Finding the length of the arc of the parametric curve x(t) = cos^4(t) and y(t) = sin^4(t) from t = 0 to t = 2pi
The length of the arc is 6.492900960560925 with an error of 4.765521304124758e-13

双变量函数的二重积分

假设我们有一个双变量实值函数和一个定义域的子集。多元微积分中的定双积分运算符用于计算函数曲面与包含给定点对集合的平面或平面之间的固体体积值。

使用 dblquad 函数计算积分

下面是使用 Scipy dblquad 函数计算多变量函数积分值的 Python 代码示例。

代码

输出

Double integral computation
Finding the integral value of the function 2(x^2)ye^-xy from y = 0 to y = 10 and from x = y - 2 to x = y + 2
The value of the integration is 5.93984306144259 with an error of 7.325997504492067e-11

使用 nquad 函数计算积分

下面是使用 Scipy nquad 函数计算多变量函数积分值的 Python 代码示例。

代码

输出

Double integral using nquad
Finding the value of integration of the function 2(x^2)ye^-xy from y = 0 to y = 10 and from x = y - 2 to x = y + 2
The value of the integration is 5.93984306144259 with an error of 7.325997504492067e-11

使用 nquad 函数计算双变量函数的二重积分(积分上限为无穷大)

现在我们将看到如何计算双变量函数的二重积分,这次的积分上限为无穷大。

下面是使用 Scipy nquad 函数进行积分计算的 Python 代码示例。

代码

输出

Double integral using nquad
Finding the value of integration of the function 2(x^2)ye^-xy from y = 0 to y = inf and from x = 1 to x = inf
The value of the integration is 2.2072766470286456 with an error of 3.954831816447722e-08

三重变量函数的三重积分

现在让我们来看涉及三个以上变量的函数的积分微积分。这些积分称为三重积分。

使用 tplquad 计算三重积分

下面是使用 Scipy tplquad 函数计算积分值的 Python 代码示例。

代码

输出

Triple integration using tplquad
Find the integration value of the function x + y^2z^2 from z = 1 to z = 3, y = z to y = z + 3 and from x = y + z to x = 2(y + z)
R=The value of the integration is 3003.5 with an error of 5.1790448248981665e-11

使用 nquad 计算积分

下面是使用 Scipy nquad 函数进行积分计算的 Python 代码示例。

代码

输出

The value of the integration is 3003.5 with an error of 5.1790448248981665e-11

使用 Sympy 对单变量函数进行积分

SymPy 是一个用于处理数学的 Python 库,它的名字是 Symbolic Mathematics in Python 的缩写。SciPy 生态系统包含了 NumPy、Pandas 和 Matplotlib 等其他大型软件包,而 SymPy 也是其中的核心库。您可以使用 SymPy 处理数学表达式。它的目标是发展成为一个功能齐全的计算机代数系统(CAS),同时保持代码尽可能简单,以便于理解和扩展。SymPy 的所有代码都用 Python 编写。

SciPy 是一个用于数值数学的库,而 SymPy 是用于符号数学的。SciPy 只能处理浮点数等数值,而不理解 SymPy 的符号。

使用 integrate(f, (x, a, b)) 对单变量函数进行积分

下面是使用 SymPy 库的 integrate(f, (x, a, b)) 函数计算积分的 Python 代码示例。

代码

输出

Finding the integration value of the function 2x^2e^-x from x = 1 to x = 5
The value of integration of our function is 
3.18018633378210

使用 integrate(f, (x, a, b)) 通过不定积分对单变量函数进行积分

下面是使用 SymPy 库的 integrate(f, x) 函数计算积分的 Python 代码示例。

代码

输出

Finding integration of the function 2x^2e^-x from x = 1 to x = 5
The primitive value of integration is 
(-2*x**2 - 4*x - 4)*exp(-x)
The value of the integration of our function is 
3.180186333782099

程序首先计算不定积分,然后应用微积分基本定理估算积分值。

双变量函数的二重积分

使用 integrate(f, (x, xa, xb), (y, ya, yb)) 对双变量函数进行二重积分

下面是使用 SymPy 库的 integrate(f, (x, xa, xb), (y, ya, yb)) 函数计算积分的 Python 代码示例。

代码

输出

Finding the integration of the function xy^2e^-xy from y = 0 to y = 5 and from x = y - 2 to x = y + 2
The value of the integration is 0.748661161788309