- SymPy - Discussion
- SymPy - Useful Resources
- SymPy - Quick Guide
- SymPy - Printing
- SymPy - Sets
- SymPy - Entities
- SymPy - Plotting
- SymPy - Solvers
- SymPy - Quaternion
- SymPy - Function class
- SymPy - Matrices
- SymPy - Integration
- SymPy - Derivative
- SymPy - Simplification
- SymPy - Querying
- SymPy - Logical Expressions
- SymPy - Lambdify() function
- SymPy - evalf() function
- SymPy - sympify() function
- SymPy - Substitution
- SymPy - Symbols
- SymPy - Numbers
- SymPy - Symbolic Computation
- SymPy - Installation
- SymPy - Introduction
- SymPy - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
SymPy - Solvers
Since the symbols = and == are defined as assignment and equapty operators in Python, they cannot be used to formulate symbopc equations. SymPy provides Eq() function to set up an equation.
>>> from sympy import * >>> x,y=symbols( x y ) >>> Eq(x,y)
The above code snippet gives an output equivalent to the below expression −
x = y
Since x=y is possible if and only if x-y=0, above equation can be written as −
>>> Eq(x-y,0)
The above code snippet gives an output equivalent to the below expression −
x − y = 0
The solver module in SymPy provides soveset() function whose prototype is as follows −
solveset(equation, variable, domain)
The domain is by default S.Complexes. Using solveset() function, we can solve an algebraic equation as follows −
>>> solveset(Eq(x**2-9,0), x)
The following output is obtained −
{−3, 3}
>>> solveset(Eq(x**2-3*x, -2),x)
The following output is obtained after executing the above code snippet −
{1,2}
The output of solveset is a FiniteSet of the solutions. If there are no solutions, an EmptySet is returned
>>> solveset(exp(x),x)
The following output is obtained after executing the above code snippet −
$varnothing$
Linear equation
We have to use pnsolve() function to solve pnear equations.
For example, the equations are as follows −
x-y=4
x+y=1
>>> from sympy import * >>> x,y=symbols( x y ) >>> pnsolve([Eq(x-y,4),Eq( x + y ,1) ], (x, y))
The following output is obtained after executing the above code snippet −
$lbrace(frac{5}{2},-frac{3}{2}) brace$
The pnsolve() function can also solve pnear equations expressed in matrix form.
>>> a,b=symbols( a b ) >>> a=Matrix([[1,-1],[1,1]]) >>> b=Matrix([4,1]) >>> pnsolve([a,b], (x,y))
We get the following output if we execute the above code snippet −
$lbrace(frac{5}{2},-frac{3}{2}) brace$
Non-pnear equation
For this purpose, we use nonpnsolve() function. Equations for this example −
a2+a=0 a-b=0
>>> a,b=symbols( a b ) >>> nonpnsolve([a**2 + a, a - b], [a, b])
We get the following output if we execute the above code snippet −
$lbrace(-1, -1),(0,0) brace$
differential equation
First, create an undefined function by passing cls=Function to the symbols function. To solve differential equations, use dsolve.
>>> x=Symbol( x ) >>> f=symbols( f , cls=Function) >>> f(x)
The following output is obtained after executing the above code snippet −
f(x)
Here f(x) is an unevaluated function. Its derivative is as follows −
>>> f(x).diff(x)
The above code snippet gives an output equivalent to the below expression −
$frac{d}{dx}f(x)$
We first create Eq object corresponding to following differential equation
>>> eqn=Eq(f(x).diff(x)-f(x), sin(x)) >>> eqn
The above code snippet gives an output equivalent to the below expression −
$-f(x) + frac{d}{dx}f(x)= sin(x)$
>>> dsolve(eqn, f(x))
The above code snippet gives an output equivalent to the below expression −
$f(x)=(c^1-frac{e^-xsin(x)}{2}-frac{e^-xcos(x)}{2})e^x$
Advertisements