Take the 2-minute tour ×

What's the (best) way to solve a pair of non linear equations using Python. (Numpy, Scipy or Sympy)


  • x+y^2 = 4
  • e^x+ xy = 3

A code snippet which solves the above pair will be great

share|improve this question
sage can do this. –  Blender Jan 5 '12 at 7:51
yea I know that..I wish to do it in python, because I want to do it repetitively for different sets of equations –  AIB Jan 5 '12 at 7:53
You can import sage from any Python script. –  Blender Jan 5 '12 at 15:03
sage does it by being a wrapper for sympy and maxima, so you could just use those directly. –  endolith Jun 16 '14 at 4:11

3 Answers 3

up vote 17 down vote accepted

for numerical solution, you can use fsolve:


from scipy.optimize import fsolve
import math

def equations(p):
    x, y = p
    return (x+y**2-4, math.exp(x) + x*y - 3)

x, y =  fsolve(equations, (1, 1))

print equations((x, y))
share|improve this answer

If you prefer sympy you can use nsolve.

>>> nsolve([x+y**2-4, exp(x)+x*y-3], [x, y], [1, 1])

The first argument is a list of equations, the second is list of variables and the third is an initial guess.

share|improve this answer

You can use openopt package and its NLP method. It has many dynamic programming algorithms to solve nonlinear algebraic equations consisting:
goldenSection, scipy_fminbound, scipy_bfgs, scipy_cg, scipy_ncg, amsg2p, scipy_lbfgsb, scipy_tnc, bobyqa, ralg, ipopt, scipy_slsqp, scipy_cobyla, lincher, algencan, which you can choose from.
Some of the latter algorithms can solve constrained nonlinear programming problem. So, you can introduce your system of equations to openopt.NLP() with a function like this:

lambda x: x[0] + x[1]**2 - 4, np.exp(x[0]) + x[0]*x[1]

share|improve this answer

Your Answer


