Sympy符号计算(使用python求导,解方程组) 您所在的位置:网站首页 带绝对值符号的函数求导 Sympy符号计算(使用python求导,解方程组)

Sympy符号计算(使用python求导,解方程组)

#Sympy符号计算(使用python求导,解方程组)| 来源: 网络整理| 查看: 265

Sympy符号计算 什么是符号计算?

符号计算以符号方式处理数学对象的计算。这意味着数学对象被精确地表示,而不是近似地表示,并且具有未评估变量的数学表达式被保留为符号形式。

>>> import math >>> math.sqrt(9) 3.0 >>> math.sqrt(8) 2.82842712475 >>> import sympy >>> sympy.sqrt(3) sqrt(3) # 符号结果可以象征性地简化 >>> sympy.sqrt(8) 2*sqrt(2) >>> from sympy import * >>> x = symbols('x') >>> a = Integral(cos(x)*exp(x), x) >>> Eq(a, a.doit()) Eq(Integral(exp(x)*cos(x), x), exp(x)*sin(x)/2 + exp(x)*cos(x)/2)

运行结果: ∫ e x cos ⁡ ( x ) d x = e x sin ⁡ ( x ) 2 + e x cos ⁡ ( x ) 2 \int e^{x} \cos (x) d x=\frac{e^{x} \sin (x)}{2}+\frac{e^{x} \cos (x)}{2} ∫excos(x)dx=2exsin(x)​+2excos(x)​

在使用前在SymPy变量必须定义

通常,最好的做法是将Symbols分配给同名的Python变量,尽管有例外:符号名称可以包含Python变量名称中不允许的字符,或者可能只是想通过赋值长的符号来避免键入长名称名称为单字母Python变量。

>>> from __future__ import division >>> from sympy import * >>> x, y, z, t = symbols('x y z t') >>> k, m, n = symbols('k m n', integer=True) >>> f, g, h = symbols('f g h', cls=Function) # 定义函数符号变量 >>> from sympy import symbols >>> x, y = symbols('x y') # 定义变量 >>> expr = x + 2*y >>> expr x + 2*y >>> expr + 1 x + 2*y + 1 >>> expr - x 2*y >>> x*expr x*(x + 2*y) # 符号形式转换 >>> from sympy import expand, factor >>> expanded_expr = expand(x*expr) >>> expanded_expr x**2 + 2*x*y >>> factor(expanded_expr) x*(x + 2*y) import math from sympy import * init_printing() math.sqrt(2)

1.4142135623730951 \displaystyle 1.4142135623730951 1.4142135623730951

sqrt(2)

2 \displaystyle \sqrt{2} 2 ​

pi

π \displaystyle \pi π

x,y,z,a,b,c,n,r = symbols('x,y,z,a,b,c,n,r') alpha,beta,gamma,theta = symbols('alpha,beta,gamma,theta') log(alpha**beta)+gamma

γ + log ⁡ ( α β ) \displaystyle \gamma + \log{\left(\alpha^{\beta} \right)} γ+log(αβ)

sin(x)**2+cos(y)**2

sin ⁡ 2 ( x ) + cos ⁡ 2 ( y ) \displaystyle \sin^{2}{\left(x \right)} + \cos^{2}{\left(y \right)} sin2(x)+cos2(y)

mu,sigma = symbols('mu,sigma') mu,sigma

( μ ,   σ ) \displaystyle \left( \mu, \ \sigma\right) (μ, σ)

b = exp(-(x-mu)**2/(2*sigma)**2) b

e − ( − μ + x ) 2 4 σ 2 \displaystyle e^{- \frac{\left(- \mu + x\right)^{2}}{4 \sigma^{2}}} e−4σ2(−μ+x)2​

求导 (x**2).diff()

2 x \displaystyle 2 x 2x

sin(x).diff()

cos ⁡ ( x ) \displaystyle \cos{\left(x \right)} cos(x)

(x**2+x*y+y**2).diff(x)

2 x + y \displaystyle 2 x + y 2x+y

diff(x**2+x*y+y**2,y)

x + 2 y \displaystyle x + 2 y x+2y

diff(x**3+x**2+2*x+x*y+y**2,x,2)

2 ( 3 x + 1 ) \displaystyle 2 \left(3 x + 1\right) 2(3x+1)

(x**3+x**2+2*x+x*y+y**2).diff(x,2)

2 ( 3 x + 1 ) \displaystyle 2 \left(3 x + 1\right) 2(3x+1)

(x**3+x**2+2*x+x*y+y**2).diff(x,2).expand() # expand()展开式子

6 x + 2 \displaystyle 6 x + 2 6x+2

b

e − ( − μ + x ) 2 4 σ 2 \displaystyle e^{- \frac{\left(- \mu + x\right)^{2}}{4 \sigma^{2}}} e−4σ2(−μ+x)2​

b.diff(x)

− ( − 2 μ + 2 x ) e − ( − μ + x ) 2 4 σ 2 4 σ 2 \displaystyle - \frac{\left(- 2 \mu + 2 x\right) e^{- \frac{\left(- \mu + x\right)^{2}}{4 \sigma^{2}}}}{4 \sigma^{2}} −4σ2(−2μ+2x)e−4σ2(−μ+x)2​​

b.diff(x,2)

( − 2 + ( μ − x ) 2 σ 2 ) e − ( μ − x ) 2 4 σ 2 4 σ 2 \displaystyle \frac{\left(-2 + \frac{\left(\mu - x\right)^{2}}{\sigma^{2}}\right) e^{- \frac{\left(\mu - x\right)^{2}}{4 \sigma^{2}}}}{4 \sigma^{2}} 4σ2(−2+σ2(μ−x)2​)e−4σ2(μ−x)2​​

b.diff(x).diff(x)

− e − ( − μ + x ) 2 4 σ 2 2 σ 2 + ( − 2 μ + 2 x ) 2 e − ( − μ + x ) 2 4 σ 2 16 σ 4 \displaystyle - \frac{e^{- \frac{\left(- \mu + x\right)^{2}}{4 \sigma^{2}}}}{2 \sigma^{2}} + \frac{\left(- 2 \mu + 2 x\right)^{2} e^{- \frac{\left(- \mu + x\right)^{2}}{4 \sigma^{2}}}}{16 \sigma^{4}} −2σ2e−4σ2(−μ+x)2​​+16σ4(−2μ+2x)2e−4σ2(−μ+x)2​​

b.diff(x,2).expand() # 二阶导

μ 2 e − μ 2 4 σ 2 e − x 2 4 σ 2 e μ x 2 σ 2 4 σ 4 − μ x e − μ 2 4 σ 2 e − x 2 4 σ 2 e μ x 2 σ 2 2 σ 4 − e − μ 2 4 σ 2 e − x 2 4 σ 2 e μ x 2 σ 2 2 σ 2 + x 2 e − μ 2 4 σ 2 e − x 2 4 σ 2 e μ x 2 σ 2 4 σ 4 \displaystyle \frac{\mu^{2} e^{- \frac{\mu^{2}}{4 \sigma^{2}}} e^{- \frac{x^{2}}{4 \sigma^{2}}} e^{\frac{\mu x}{2 \sigma^{2}}}}{4 \sigma^{4}} - \frac{\mu x e^{- \frac{\mu^{2}}{4 \sigma^{2}}} e^{- \frac{x^{2}}{4 \sigma^{2}}} e^{\frac{\mu x}{2 \sigma^{2}}}}{2 \sigma^{4}} - \frac{e^{- \frac{\mu^{2}}{4 \sigma^{2}}} e^{- \frac{x^{2}}{4 \sigma^{2}}} e^{\frac{\mu x}{2 \sigma^{2}}}}{2 \sigma^{2}} + \frac{x^{2} e^{- \frac{\mu^{2}}{4 \sigma^{2}}} e^{- \frac{x^{2}}{4 \sigma^{2}}} e^{\frac{\mu x}{2 \sigma^{2}}}}{4 \sigma^{4}} 4σ4μ2e−4σ2μ2​e−4σ2x2​e2σ2μx​​−2σ4μxe−4σ2μ2​e−4σ2x2​e2σ2μx​​−2σ2e−4σ2μ2​e−4σ2x2​e2σ2μx​​+4σ4x2e−4σ2μ2​e−4σ2x2​e2σ2μx​​

方程 expr = sin(x)**2+cos(x)**2 expr

sin ⁡ 2 ( x ) + cos ⁡ 2 ( x ) \displaystyle \sin^{2}{\left(x \right)} + \cos^{2}{\left(x \right)} sin2(x)+cos2(x)

simplify(expr) # 简化式子

1 \displaystyle 1 1

simplify(b.diff(x).diff(x).diff(x))

( 6 σ 2 ( − μ + x ) + ( μ − x ) 3 ) e − ( μ − x ) 2 4 σ 2 8 σ 6 \displaystyle \frac{\left(6 \sigma^{2} \left(- \mu + x\right) + \left(\mu - x\right)^{3}\right) e^{- \frac{\left(\mu - x\right)^{2}}{4 \sigma^{2}}}}{8 \sigma^{6}} 8σ6(6σ2(−μ+x)+(μ−x)3)e−4σ2(μ−x)2​​

求解方程 solveset(x**2-4,x)

{ − 2 , 2 } \displaystyle \left\{-2, 2\right\} {−2,2}

solveset(sin(x),x)

{ 2 n π    ∣    n ∈ Z } ∪ { 2 n π + π    ∣    n ∈ Z } \displaystyle \left\{2 n \pi\; |\; n \in \mathbb{Z}\right\} \cup \left\{2 n \pi + \pi\; |\; n \in \mathbb{Z}\right\} {2nπ∣n∈Z}∪{2nπ+π∣n∈Z}

solveset((x-1)*(exp(x)+cos(x)+1),x,domain = S.Reals)

{ 1 } ∪ { x ∣ x ∈ R ∧ e x + cos ⁡ ( x ) + 1 = 0 } \displaystyle \left\{1\right\} \cup \left\{x \mid x \in \mathbb{R} \wedge e^{x} + \cos{\left(x \right)} + 1 = 0 \right\} {1}∪{x∣x∈R∧ex+cos(x)+1=0}

替换 Substitution (x**2+2*x+1).subs({x:sin(x)})

sin ⁡ 2 ( x ) + 2 sin ⁡ ( x ) + 1 \displaystyle \sin^{2}{\left(x \right)} + 2 \sin{\left(x \right)} + 1 sin2(x)+2sin(x)+1

(sin(x)**2+sin(x)-1+cos(x)+cos(x)**3).subs({(sin(x)**2+sin(x)):y})

y + cos ⁡ 3 ( x ) + cos ⁡ ( x ) − 1 \displaystyle y + \cos^{3}{\left(x \right)} + \cos{\left(x \right)} - 1 y+cos3(x)+cos(x)−1

(x**2+2*x+1).subs(x,sin(x))

sin ⁡ 2 ( x ) + 2 sin ⁡ ( x ) + 1 \displaystyle \sin^{2}{\left(x \right)} + 2 \sin{\left(x \right)} + 1 sin2(x)+2sin(x)+1

Plotting %matplotlib inline plot(x**2,(x,-100,100))

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xi9CxAGs-1582095533708)(output_36_0.png)]

textplot(x**2,-3,3) 9 | \ / | \ / | . . | | . . | \ / | \ / | \ / 4.76471 | -------\---------------------------------------/------- | .. .. | \ / | \ / | .. .. | \ / | .. .. | .. .. | .... .... 0 | ......... -3 0 3 不变性 a = x+y+1 a

x + y + 1 \displaystyle x + y + 1 x+y+1

x =3 a # 不发生改变

x + y + 1 \displaystyle x + y + 1 x+y+1

a.subs({x:3})

x + y + 1 \displaystyle x + y + 1 x+y+1

x = symbols('x') a.subs({x:3})

y + 4 \displaystyle y + 4 y+4

等式 eq = Eq(x**2,y) eq

x 2 = y \displaystyle x^{2} = y x2=y

eq.lhs

x 2 \displaystyle x^{2} x2

eq.rhs

y \displaystyle y y

solveset(eq,x)

{ − y , y } \displaystyle \left\{- \sqrt{y}, \sqrt{y}\right\} {−y ​,y ​}

分式 from __future__ import division 1/2

0.5 \displaystyle 0.5 0.5

acos(1/2)

1.0471975511966 \displaystyle 1.0471975511966 1.0471975511966

Rational(1,2)

1 2 \displaystyle \frac{1}{2} 21​

acos(Rational(1,2))

π 3 \displaystyle \frac{\pi}{3} 3π​

from fractions import Fraction Fraction(1,2) Fraction(1, 2) acos(Fraction(1,2))

π 3 \displaystyle \frac{\pi}{3} 3π​

S(1)/2

1 2 \displaystyle \frac{1}{2} 21​

type(S(1)) sympy.core.numbers.One 积分 integrate(x**2,x)+1

x 3 3 + 1 \displaystyle \frac{x^{3}}{3} + 1 3x3​+1

integrate(x**2,(x,0,3))

9 \displaystyle 9 9

integrate(x**n,(x,y,z))

{ − y n + 1 n + 1 + z n + 1 n + 1 for   n > − ∞ ∧ n < ∞ ∧ n ≠ − 1 − log ⁡ ( y ) + log ⁡ ( z ) otherwise \displaystyle \begin{cases} - \frac{y^{n + 1}}{n + 1} + \frac{z^{n + 1}}{n + 1} & \text{for}\: n > -\infty \wedge n < \infty \wedge n \neq -1 \\- \log{\left(y \right)} + \log{\left(z \right)} & \text{otherwise} \end{cases} {−n+1yn+1​+n+1zn+1​−log(y)+log(z)​forn>−∞∧n>> x = symbols('x') >>> expr = x + 1 >>> x = 2 >>> print(expr) x + 1 要更改表达式中符号的值,请使用==subs()==进行替换 >>> x = symbols('x') >>> expr = x + 1 >>> expr.subs(x, 2) 3 >>> expr = cos(x) + 1 >>> expr.subs(x, y) cos(y) + 1

替换通常是出于以下两个原因之一:

我们知道一个符号表达式,然后想知道它在某一点处的值,就将符号替换为值。例如,计算cos(x)+1在x=0的时候的值cos(0)+1,就要用subs(x,0)将替换为0

用另一个子表达式替换子表达式。

第一个是如果我们试图构建一个具有一些对称性的表达式

>>> expr = x**y >>> expr x**y >>> expr = expr.subs(y, x**y) >>> expr x**(x**y) >>> expr = expr.subs(y, x**x) >>> expr x**(x**(x**x))

​ 第二个是如果我们想要执行非常有控制的简化,或者可能是SymPy无法做到的简化。

>>> expr = sin(2*x) + cos(2*x) >>> expand_trig(expr) 2*sin(x)*cos(x) + 2*cos(x)**2 - 1 >>> expr.subs(sin(2*x), 2*sin(x)*cos(x)) 2*sin(x)*cos(x) + cos(2*x)

subs有两个重要的事项需要注意:

首先,它返回一个新表达式。SymPy对象是不可变的。 >>> expr = cos(x) >>> expr.subs(x, 0) 1 >>> expr cos(x) >>> x x 将它与列表理解相结合通常可以同时执行大量类似的替换。 >>> expr = x**3 + 4*x*y - z >>> expr.subs([(x, 2), (y, 4), (z, 0)]) 40 >>> expr = x**4 - 4*x**3 + 4*x**2 - 2*x + 3 >>> replacements = [(x**i, y**i) for i in range(5) if i % 2 == 0] >>> expr.subs(replacements) -4*x**3 - 2*x + y**4 + 4*y**2 + 3 等号

=它不代表SymPy中的相等性。相反,它是Python变量赋值。

==用于判断两个式子是否相等,返回逻辑运算符

>>> x + 1 == 4 False

还有一种方法称为equals测试两个表达式是否相等

>>> a = cos(x)**2 - sin(x)**2 >>> b = cos(2*x) >>> a.equals(b) True

建立符号相等可以使用Eq

>>> Eq(x + 1, 4) Eq(x + 1, 4) 使用simplify()函数简化 >>> from sympy import * >>> x, y, z = symbols('x y z') >>> init_printing(use_unicode=True) >>> simplify(sin(x)**2 + cos(x)**2) 1 >>> simplify((x**3 + x**2 - x - 1)/(x**2 + 2*x + 1)) x - 1 >>> simplify(gamma(x)/gamma(x - 2)) # gamma(x)是Γ(x),伽玛函数。 (x - 2)⋅(x - 1) >>> a = (x + 1)**2 >>> b = x**2 + 2*x + 1 >>> simplify(a - b) 0 >>> c = x**2 - 2*x + 1 >>> simplify(a - c) 4*x >>> Rational(1, 2) 1/2 >>> simplify(x**2 + 2*x + 1) ?2+2?+1 >>>factor(x**2 + 2*x + 1) (?+1)2 多项式/有理函数简化

expand()

给定多项式,expand()将其置于单项式和的规范形式中。

>>> expand((x + 1)**2) 2 x + 2⋅x + 1 >>> expand((x + 2)*(x - 3)) 2 x - x - 6 >>> expand((x + 1)*(x - 2) - (x - 1)*x) -2

factor()

factor()采用多项式并将其分解为有理数上的不可约因子。

>>> factor(x**3 - x**2 + x - 1) 2 (x - 1) ⋅(x + 1) >>> factor(x**2*z + 4*x*y*z + 4*y**2*z) 2 z⋅(x + 2⋅y) 将字符串转换为SymPy表达式

sympify函数(sympify不要混淆 simplify)可用于将字符串转换为SymPy表达式。

>>> str_expr = "x**2 + 3*x - 1/2" >>> expr = sympify(str_expr) >>> expr x**2 + 3*x - 1/2 >>> expr.subs(x, 2) 19/2 要将数值表达式计算为浮点数,请使用 evalf >>> expr = sqrt(8) >>> expr.evalf() 2.82842712474619 >>> pi.evalf(100) 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068 >>> expr = cos(2*x) >>> expr.evalf(subs={x: 2.4}) 0.0874989834394464 >>> one = cos(1)**2 + sin(1)**2 >>> (one - 1).evalf() -0.e-124 >>> (one - 1).evalf(chop=True) # chop=True 舍去误差 0 lambdify

将SymPy表达式转换为可以进行数值计算的表达式

>>> import numpy # doctest:+SKIP >>> a = numpy.arange(10) # doctest:+SKIP >>> expr = sin(x) >>> f = lambdify(x, expr, "numpy") # doctest:+SKIP >>> f(a) # doctest:+SKIP [ 0. 0.84147098 0.90929743 0.14112001 -0.7568025 -0.95892427 -0.2794155 0.6569866 0.98935825 0.41211849] >>> f = lambdify(x, expr, "math") >>> f(0.1) 0.0998334166468 # 要将lambdify与其自定义数值库一起使用,请传递对的字典sympy_name:numerical_function >>> def mysin(x): """ My sine. Note that this is only accurate for small x. """ return x >>> f = lambdify(x, expr, {"sin":mysin}) >>> f(0.1) 0.1 数学公式的2D漂亮打印输出,或LATEX >>> from sympy import init_printing >>> init_printing() # doctest: +SKIP >>> init_printing(use_unicode = True)

There are several printers available in SymPy.

The most common ones are:

strsreprASCII pretty printerUnicode pretty printerLaTeXMathMLDot 微积分 求导 >>> from sympy import * >>> x, y, z = symbols('x y z') >>> init_printing(use_unicode=True) >>> diff(cos(x), x) −sin(?) >>> diff(exp(x**2), x) # 求三阶导数 >>> diff(x**4, x, x, x) 24⋅x >>> diff(x**4, x, 3) 24⋅x # 同时获取多个变量的导数 >>> expr = exp(x*y*z) >>> diff(expr,x,y,y,z,z,z,z) >>> diff(expr,x,y,2,z,4) >>> expr.diff(x, y, y, z, 4) # 以上三种结果都是一样的 # 要创建未计算的导数,请使用Derivative类。它具有diff相同的语法。 >>> deriv = Derivative(expr, x, y, y, z, 4) >>> deriv >>> deriv.doit() #将未计算的导数计算 # 对x求n次导 >>> m, n, a, b = symbols('m n a b') >>> expr = (a*x + b)**m >>> expr.diff((x, n)) 积分 # 不定积分 >>> integrate(cos(x), x) sin(x) # 要计算定积分,请传递参数(integration_variable, lower_limit, upper_limit) >>> integrate(exp(-x), (x, 0, oo)) # 其中∞是由两个小写字母oo组成 1 # 计算多重积分 >>> integrate(exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo)) π # 如果integrate无法计算积分,则返回未评估的 Integral对象。 >>> expr = integrate(x**x, x) >>> print(expr) Integral(x**x, x) >>> expr ⌠ ⎮ x ⎮ x dx ⌡ >>> expr = Integral(log(x)**2, x) # 创建未计算的积分形式 >>> expr.doit() # 计算未计算的积分 2 x⋅log (x) - 2⋅x⋅log(x) + 2⋅x >>> integ = Integral((x**4 + x**2*exp(x) - x**2 - 2*x*exp(x) - 2*x - exp(x))*exp(x)/((x - 1)**2*(x + 1)**2*(exp(x) + 1)), x) 极限 >>> limit(sin(x)/x, x, 0) 1 >>> expr = x**2/exp(x) >>> expr.subs(x, oo) nan >>> limit(expr, x, oo) 0 >>> expr = Limit((cos(x) - 1)/x, x, 0) # 极限未计算形式 >>> expr.doit() # 计算极限 >>> limit(1/x, x, 0, '+') # x趋向于0+ ∞ >>> limit(1/x, x, 0, '-') # x趋向于0- -∞ 求解方程 关于方程的一个注释 >>> from sympy import * >>> x, y, z = symbols('x y z') >>> init_printing(use_unicode=True) >>> Eq(x, y) x = y >>> solveset(Eq(x**2, 1), x) {-1, 1} >>> solveset(Eq(x**2 - 1, 0), x) {-1, 1} >>> solveset(x**2 - 1, x) {-1, 1} # 如果您要求解的等式已经等于0,可以直接使用solveset(expr, x) # 而不是solveset(Eq(expr, 0), x) 以代数方式求解方程式

求解代数方程的主要函数是solveset(equation, variable=None, domain=S.Complexes)

还有一个求解函数solve(equations,variables) 求解方程组就用solve([EQ1,EQ2],[r1,r2])

推荐使用solveset(equation, variable=None, domain=S.Complexes)

>>> solveset(x**2 - x, x) {0, 1} >>> solveset(x - x, x, domain=S.Reals) ℝ >>> solveset(sin(x) - 1, x, domain=S.Reals) ⎧ π ⎫ ⎨2⋅n⋅π + ─ | n ∊ ℤ⎬ ⎩ 2 ⎭ >>> solveset(exp(x), x) # No solution exists ∅ >>> solveset(cos(x) - x, x) # Not able to find solution {x | x ∊ ℂ ∧ -x + cos(x) = 0}

求解线性方程组

方程列表形式: >>> linsolve([x + y + z - 1, x + y + 2*z - 3 ], (x, y, z)) {(-y - 1, y, 2)} 增强矩阵形式: >>> linsolve(Matrix(([1, 1, 1, 1], [1, 1, 2, 3])), (x, y, z)) {(-y - 1, y, 2)} A * x = b表格 >>> M = Matrix(((1, 1, 1, 1), (1, 1, 2, 3))) >>> system = A, b = M[:, :-1], M[:, -1] >>> linsolve(system, x, y, z) {(-y - 1, y, 2)}

求解非线性方程组:

当只有实数解时 >>> a, b, c, d = symbols('a, b, c, d', real=True) >>> nonlinsolve([a**2 + a, a - b], [a, b]) {(-1, -1), (0, 0)} >>> nonlinsolve([x*y - 1, x - 2], x, y) {(2, 1/2)} 当只有复数解时 >>> nonlinsolve([x**2 + 1, y**2 + 1], [x, y]) {(-ⅈ, -ⅈ), (-ⅈ, ⅈ), (ⅈ, -ⅈ), (ⅈ, ⅈ)} 既有实数解又有复数解时 >>> from sympy import sqrt >>> system = [x**2 - 2*y**2 -2, x*y - 2] >>> vars = [x, y] >>> nonlinsolve(system, vars) {(-2, -1), (2, 1), (-√2⋅ⅈ, √2⋅ⅈ), (√2⋅ⅈ, -√2⋅ⅈ)} 如果非线性方程组是正维系统(具有无限多个解的系统被认为是正维的): >>> nonlinsolve([x*y, x*y - x], [x, y]) {(0, y)} >>> system = [a**2 + a*c, a - b] >>> nonlinsolve(system, [a, b]) {(0, 0), (-c, -c)} 求解微分方程

要求解微分方程,请使用dsolve。通过传递cls=Function给symbols函数创建一个未定义的函数。

>>> f, g = symbols('f g', cls=Function) >>> f(x) f(x) >>> f(x).diff(x) d ──(f(x)) dx # 表示微分方程 f′′(x)−2f′(x)+f(x)=sin(x)f″(x)−2f′(x)+f(x)=sin⁡(x) >>> diffeq = Eq(f(x).diff(x, x) - 2*f(x).diff(x) + f(x), sin(x)) >>> diffeq 2 d d f(x) - 2⋅──(f(x)) + ───(f(x)) = sin(x) dx 2 dx >>> dsolve(diffeq, f(x)) x cos(x) f(x) = (C₁ + C₂⋅x)⋅ℯ + ────── 2 >>> dsolve(f(x).diff(x)*(1 - sin(f(x))) - 1, f(x)) -x + f(x) + cos(f(x)) = C₁


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有