# 4.3 积分

使用Integrate()函数可以轻松完成各种积分的运算，Integrate()函数的使用方法如下：

Integrate(expressions, (integration\_variable\[, lower\_limit, upper\_limit])

其中第一个参数为待积分的表达式，第二个参数是以元组形式传入积分变量以及其上下界。

## 4.3.1 不定积分

当Integrate()函数第二个参数不传入积分上下界时，默认计算表达式的不定积分：

例：求不定积分 $$\int \frac{\arctan{\sqrt{x}}}{(1+x)\sqrt{x}}$$。

```python
[]:f=Lambda(x, atan(sqrt(x))/((1+x)\*sqrt(x)))

integrate(f(x), x)
```

\[]:![](/files/Ue1OaS8z4mJ1wF8fc02y)

## 4.3.2 定积分

当Integrate()函数第二个参数传入积分上下界时，默认计算表达式在给定范围内的定积分：

例：求定积分$$\int\_{0}^{log 2} e^{-x} dx$$

```python
[]:integrate(exp(-x), (x, 0, log(2)))
```

\[]:![](/files/WCL09BhPyGvMdaDpowlk)

除了使用Integrate()函数计算函数的定积分以外，还可以使用高斯-勒让德公式公式来求待积分函数在给定区间上的定积分。

在区间$$\[-1,1],\[-1,1]$$上，高斯-勒让德求积公式为： $$\int\_{-1}^{1} f(x) dx \approx \sum\_{k=0}^{n} A\_k f{x\_k}$$ Gass点系数都有表可以查询。

使用sympy.integrals.quadrature.gauss\_legendre()函数可以查询分布在区间上的高斯点以及对应的高斯系数。

gauss\_legendre()函数的使用方法如下：

```python
sympy.integrals.quadrature.**gauss_legendre**(*n*，*n_digits*)
```

其中n为高斯点的个数，n\_digits为返回的点数和权重的有效位数的个数，该函数返回一个二元组\*\*(x, w)\*\* : x和w是浮点数的点和权值列表。

例：使用高斯勒让德公式求定积分$$\int\_{-1}^{1} \sqrt{16+6x-x^2}$$的值

在本例中，我们先使用gass\_legendre生成10个高斯点以及对应的高斯系数，然后使用求和函数球的使用高斯-勒让德公式求出的积分值。

```python
[]:from sympy.integrals.quadrature import gauss_legendre

n_point=10

xi, wi = gauss\_legendre(n_point, 5)

print(xi, wi)

[]:[-0.97391, -0.86506, -0.67941, -0.43340, -0.14887, 0.14887, 0.43340, 0.67941,
0.86506, 0.97391] [0.066671, 0.14945, 0.21909, 0.26927, 0.29552, 0.29552,
0.26927, 0.21909, 0.14945, 0.066671]

[]:f=Lambda(x, sqrt(16+6\*x-x\*\*2))

gauss_sum=0

for i in range(0, n_point):

gauss_sum=wi[i]\*f(xi[i])+gauss_sum

gauss_sum
```

\[]:![](/files/W8tVUmXlw44NimEOvbBv)

gauss\_legendre()函数生成的高斯点分布在区间上，使用线性变换： $$x = \frac{b-a}{2} t + \frac{a+b}{2}$$ 可以将积分区间从变成，由定积分的换元法有： $$\int\_{a}^{b}f(x) dx = \frac{b-a}{2} \int\_{-1}^{1} f(\frac{b-a}{2} t + \frac{a+b}{2}) dt$$这样就可以使用高斯-勒让德公式计算一般区间的积分。

## 4.3.3 广义积分

求广义积分的方法与求定积分的方法是相同而，不同的是广义积分的计算结果有可能是无穷——$$\infty$$。

## 4.3.4 无穷积分

例：计算积分$$\int\_{0}^{+\infty} e^{-x^2} dx$$

```python
[]:integrate(exp(-x\*\*2), (x, 0, oo))
```

\[]:![](/files/IPHHCW4Xr12g2K4srGlM)

## 4.4.5 瑕积分

例：判断广义积分$$\int\_0^2 \frac{1}{(1-x)^2}$$的敛散性

```python
[]:integrate(1/(1-x)\*\*2, (x, 0, 2))
```

\[]:![](/files/934lizfaswjGUbxzIf2s)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://johannesliu.gitbook.io/learning-advanced-mathematics-with-python/4.0-calculus/4.3-integral.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
