Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve a pair of nonlinear equations using Python?

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

eg:

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

A code snippet which solves the above pair will be great

like image 310
AIB Avatar asked Jan 05 '12 07:01

AIB


People also ask

Can Python solve a system of equations?

In Python, NumPy (Numerical Python), SciPy (Scientific Python) and SymPy (Symbolic Python) libraries can be used to solve systems of linear equations. These libraries use the concept of vectorization which allow them to do matrix computations efficiently by avoiding many for loops.


1 Answers

for numerical solution, you can use fsolve:

http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html#scipy.optimize.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)) 
like image 183
HYRY Avatar answered Sep 22 '22 02:09

HYRY