SymPy學習之Solvers

>>> from sympy import *
>>> x, y, z = symbols('x y z')
>>> init_printing(use_unicode=True)
A Note about Equations
#SymPy裏的符號等式用的是Eq
>>> Eq(x, y)
x = y
#方程求解函數默認右邊等於0
>>> solveset(Eq(x**2, 1), x)
{-1, 1}
>>> solveset(Eq(x**2 - 1, 0), x)
{-1, 1}
>>> solveset(x**2 - 1, x)
{-1, 1}
Solving Equations Algebraically
#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}
#root顯示出各個解的頻數
>>> solveset(x**3 - 6*x**2 + 9*x, x)
{0, 3}
>>> roots(x**3 - 6*x**2 + 9*x, x)
{0: 1, 3: 2}
#solve解方程組
>>> solve([x*y - 1, x - 2], x, y)
[(2, 1/2)]
Solving Differential Equations
#定義f,g類型爲function
>>> f, g = symbols('f g', cls=Function)
>>> f(x)
f(x)
>>> f(x).diff(x)
d
--(f(x))
dx
#求解微分方程
>>> dsolve(f(x).diff(x)*(1 - sin(f(x))), f(x))
f(x) + cos(f(x)) = C1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章