📜  如何用Python做微积分?

📅  最后修改于: 2022-05-13 01:54:28.315000             🧑  作者: Mango

如何用Python做微积分?

微积分是数学的一个分支,专注于极限、函数、导数、积分和无穷级数。我们将使用SymPy库与Python进行微积分。 SymPy 是一个用于符号数学的Python库。它旨在成为一个功能齐全的计算机代数系统(CAS),同时保持代码尽可能简单,以便于理解和易于扩展。 SymPy 完全用Python编写。

安装:

pip install sympy

如果我们想写任何 sympy 表达式,首先我们必须声明它的符号变量。为此,我们可以使用以下两个函数:

  • sympy.Symbol():用于通过将变量作为字符串传递给其参数来声明单个变量。
  • sympy.symbols():用于通过将变量作为字符串传递给其参数来声明多变量。所有变量必须由一个空格分隔,形成一个字符串。

差异化

我们可以使用diff(func, var)方法区分任何 sympy 表达式。参数func表示要微分的 sympy 表达式, var表示我们必须对其进行微分的变量。

示例 1:



Python3
# Importing library
import sympy as sym
 
# Declaring variables
x, y, z = sym.symbols('x y z')
 
# expression of which we have to find derivative
exp = x**3 * y + y**3 + z
 
# Differentiating exp with respect to x
derivative1_x = sym.diff(exp, x)
print('derivative w.r.t x: ',
      derivative1_x)
 
# Differentiating exp with respect to y
derivative1_y = sym.diff(exp, y)
print('derivative w.r.t y: ',
      derivative1_y)


Python3
# Finding second derivative
# of exp with respect to x
derivative2_x = sym.diff(exp, x, 2)
print('second derivative w.r.t. x: ',
      derivative2_x)
 
# Finding second derivative
# of exp with respect to y
derivative2_y = sym.diff(exp, y, 2)
print('second derivative w.r.t. y: ',
      derivative2_y)


Python3
# Indefinite integration of cos(x) w.r.t. dx
integral1 = sym.integrate(sym.cos(x), x)
print('indefinite integral of cos(x): ',
      integral1)
 
# definite integration of cos(x) w.r.t. dx between -1 to 1
integral2 = sym.integrate(sym.cos(x), (x, -1, 1))
print('definite integral of cos(x) between -1 to 1: ',
      integral2)
 
# definite integration of exp(-x) w.r.t. dx between 0 to ∞
integral3 = sym.integrate(sym.exp(-x), (x, 0, sym.oo))
print('definite integral of exp(-x) between 0 to ∞: ',
      integral3)


Python3
# Calculating limit of f(x) = x as x->∞
limit1 = sym.limit(x, x, sym.oo)
print(limit1)
 
# Calculating limit of f(x) = 1/x as x->∞
limit2 = sym.limit(1/x, x, sym.oo)
print(limit2)
 
# Calculating limit of f(x) = sin(x)/x as x->0
limit3 = sym.limit(sym.sin(x)/x, x, 0)
print(limit3)


Python3
# assign series
series1 = sym.series(sym.cos(x), x)
print(series1)
 
# assign series
series2 = sym.series(1/sym.cos(x), x, 0, 4)
print(series2)


输出:

derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2

我们还可以使用diff(func, var, n)方法找到更高的导数。这里,参数n表示要找到的第 n 个导数。

示例 2:

蟒蛇3

# Finding second derivative
# of exp with respect to x
derivative2_x = sym.diff(exp, x, 2)
print('second derivative w.r.t. x: ',
      derivative2_x)
 
# Finding second derivative
# of exp with respect to y
derivative2_y = sym.diff(exp, y, 2)
print('second derivative w.r.t. y: ',
      derivative2_y)

输出:

second derivative w.r.t. x:  6*x*y
second derivative w.r.t. y:  6*y

一体化

您可以通过集成()函数对超越基本函数和特殊函数进行不定和定积分。

参数FUNC表示sympy表达加以区别,VAR表示变量相对于我们具有分化,LOWER_LIMIT表示给定积分的下限和UPPER_LIMIT表示定积分的上限。

注意: SymPy 中的∞ 是 oo。

示例 1:

蟒蛇3

# Indefinite integration of cos(x) w.r.t. dx
integral1 = sym.integrate(sym.cos(x), x)
print('indefinite integral of cos(x): ',
      integral1)
 
# definite integration of cos(x) w.r.t. dx between -1 to 1
integral2 = sym.integrate(sym.cos(x), (x, -1, 1))
print('definite integral of cos(x) between -1 to 1: ',
      integral2)
 
# definite integration of exp(-x) w.r.t. dx between 0 to ∞
integral3 = sym.integrate(sym.exp(-x), (x, 0, sym.oo))
print('definite integral of exp(-x) between 0 to ∞: ',
      integral3)

输出:

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1

限制

您可以使用limit( 函数 , variable, point)计算函数的极限。因此,如果您想将 f(x) 的极限计算为 x->0,您将发出 limit(f, x, 0)。

例子:

蟒蛇3

# Calculating limit of f(x) = x as x->∞
limit1 = sym.limit(x, x, sym.oo)
print(limit1)
 
# Calculating limit of f(x) = 1/x as x->∞
limit2 = sym.limit(1/x, x, sym.oo)
print(limit2)
 
# Calculating limit of f(x) = sin(x)/x as x->0
limit3 = sym.limit(sym.sin(x)/x, x, 0)
print(limit3)

输出:



oo
0
1

系列扩展

我们还可以计算一个点周围函数的泰勒级数展开。要计算 f(x) 围绕 x n阶 x=x 0项的展开,请使用sympy.series(f, x, x 0 , n) 。 x 0和 n 可以省略,在这种情况下,将使用默认值 x 0 =0 和 n=6。

例子:

蟒蛇3

# assign series
series1 = sym.series(sym.cos(x), x)
print(series1)
 
# assign series
series2 = sym.series(1/sym.cos(x), x, 0, 4)
print(series2)

输出:

1 - x**2/2 + x**4/24 + O(x**6)
1 + x**2/2 + O(x**4)

末尾的 O(x 4 ) 或 O(x 6 ) 项表示忽略所有幂大于或等于 x 4或 x 6的 x 项。