To solve an equation or a system of equations means to find all values of the variables that satisifies all the equations.

Univariate Equations

To solve a univariate equation, means to find all values of the single variable that satisfies the equation. Here’s how you do it in sympy:

solveset(equation)

If you have a multivariate equation, and you’d like to rearrange it in terms of one of the variables (i.e. “solve” for that variable), do:

solveset(equation,variable_to_solve_for)

System of Equations

If you want to truely solve (and not simply rearrange) a multivariate equation, you need more than 1 equation for the system. The exact number of equations you need depends on the number of variables you have and the types of equations. Assuming you have a sufficient number of equations, here is how you solve it in sympy:

nonlinsolve(list_of_equations,list_of_symbols)

If you have a linear system of equations (i.e. all your equations are linear), you can use linsolve() instead, if you’d like. The general syntax for nonlinsolve() is:

nonlinsolve(system,variables)

The system parameter can be specified in 3 ways:

  1. list of equations
  2. augmented matrix
  3. matrix and vector as a tuple, i.e. (, )

The variables parameter doesn’t have to be specified unless you have an underdetermined system. If you have an underdetermined system, this parameter specified with respect to which variables the results should be displayed

Solve Numerically

Some systems (especially non linear system of equations) are impossible to solve analytically, but we can still solve them numerically. You just need a decent initial guess. Here’s how you do it in sympy:

nsolve(list_of_equations,variables_to_solve_for,initial_guess)

Summary

  • solveset(equation) to solve univariate equation
  • solveset(euqation,variable_to_solve_for) to rearrage an multivariate equation in terms of a particular variable
  • nonlinsolve(list_of_equations,list_of_variables) to solve any system of equations
  • linsolve(system,variables)
    • system parameter can be
      • list of equations
      • augmented matrix
      • matrix and vector as a tuple, i.e. (, )
    • variables parameter is optional, unless you have an underdetermined system, in which case you need to specify with respect to what variables you want the results

Playground

Try it yourself here! Below is a python interpreter with sympy installed.

import sympy
sympy.init_session()

# solve univariate equation
equation = Eq(x + 6,4) # x + 6 = 4
solved = solveset(equation)

# rearrange multivariate equation in terms of one variable
equation = Eq(y,z + 3*x) # y = z + 3x
solved = solveset(equation,x) # solve for x

# solve system of equations
equation1 = x**3 + 3*x**2 + x
equation2 = 3*x + 4# 
equations = [equation1,equation2]
variables = [x]
solved = nonlinsolve(equations,variables)

# solve system of linear equations
equation1 = 3*x+4*y
equation2 = 2*x+5*y
equations = [equation1,equation2]
variables = [x,y]
solved = linsolve(equations,variables)

# do your thang