Search⌘ K
AI Features

Solving Equations

Explore how to use SymPy's solve() function to find solutions to single and multivariable equations, including linear, non-linear, trigonometric, and interval problems. Understand how symbolic and numeric solutions differ and apply these methods to scientific computations.

SymPy has equation solvers that can handle linear and non-linear algebraic equations as well as linear and non-linear multivariate equations. Depending upon the input equation, the answer returned can be symbolic or numeric. The most commonly used one is solve(). It uses the following syntax:

solve(f(x), x)

The output type of solve() varies with input, sometimes it returns a Python list and sometimes a Python dictionary. Usually, for single variable equations, it returns a list and for multivariable equations, it returns a dictionary.

Let’s explore different cases in the examples below:

Solving for a single variable

Single solution

Let’s solve the following equation that has a single solution:

2x+3=02x+3=0

Python 3.5
from sympy import *
def f(x):
return 2*x + 3
x = Symbol('x')
sol = solve(f(x), x)
print(sol)

The solve() function returns a list with the only solution.

Multiple solutions

Let’s solve the following equation, which has multiple solutions:

x2x+6=0x^2-x+6=0

Python 3.5
from sympy import *
def f(x):
return x**2 - x - 6
x = Symbol('x')
sol = solve(f(x), x)
print(sol)

The solve() function returns a list with all the solutions. Let’s solve an equation with complex roots:

x2+4x+5=0x^2+4x+5=0 ...