How to solve symbolic equations
Description
Once we’ve expressed an equation or system of equations using the technique from how to write symbolic equations, we often want the software to solve the equation or system of equations for us.
Related tasks:
Using SymPy, in Python
This answer assumes you have imported SymPy as follows.
1
2
from sympy import * # load all math functions
init_printing( use_latex='mathjax' ) # use pretty math output
If your equation has just one variable, simply call solve
on it.
Note that you may get a list of more than one solution.
1
2
3
var( 'x' )
equation = Eq( x**2 + 3*x, -x + 9 )
solve( equation )
$\displaystyle \left[ -2 + \sqrt{13}, \ - \sqrt{13} - 2\right]$
Sometimes you get no solutions, which is shown as a Python empty list.
1
solve( Eq( x+1, x+2 ) )
$\displaystyle \left[ \right]$
Sometimes the answers include complex numbers.
1
solve( Eq( x**3, -1 ) )
$\displaystyle \left[ -1, \ \frac{1}{2} - \frac{\sqrt{3} i}{2}, \ \frac{1}{2} + \frac{\sqrt{3} i}{2}\right]$
To restrict the solution to the real numbers, use solveset
instead,
and specify the real numbers as the domain.
1
solveset( Eq( x**3, -1 ), domain=S.Reals )
$\displaystyle \left\{-1\right\}$
(If solveset
gives no solution, it shows it as the empty set symbol, $\emptyset$.)
You can solve systems of equations by calling solve
on them.
1
2
3
4
5
6
var( 'x y' )
system = [
Eq( x + 2*y, 1 ),
Eq( x - 9*y, 5 )
]
solve( system )
$\displaystyle \left\{ x : \frac{19}{11}, \ y : - \frac{4}{11}\right\}$
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Topics that include this task
Opportunities
This website does not yet contain a solution for this task in any of the following software packages.
- R
- Excel
- Julia
If you can contribute a solution using any of these pieces of software, see our Contributing page for how to help extend this website.