Solving Equations
In this lesson, we will learn about solving different types of equations numerically and symbolically.
We'll cover the following...
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:
from sympy import *def f(x):return 2*x + 3x = 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:
from sympy import *def f(x):return x**2 - x - 6x = 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:
...