Solving Equations

In this lesson, we will learn about solving different types of equations numerically and symbolically.

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

Press + to interact
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

Press + to interact
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 ...