How to solve a pair of nonlinear equations using Python?

What is the best way to solve a pair of nonlinear equations using Python. (Numpy, Scipy or Sympy)

eg:

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

The code snippet that solves the above pair will be great

+60
python numpy scipy sympy
Jan 05 2018-12-12T00:
source share
7 answers

for a 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)) 
+66
Jan 05 '12 at 8:22
source share

If you prefer sympy, you can use nsolve .

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

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

+27
Jan 21 '12 at 17:21
source share

Try this, I assure you that it will work perfectly.

  import scipy.optimize as opt from numpy import exp import timeit st1 = timeit.default_timer() def f(variables) : (x,y) = variables first_eq = x + y**2 -4 second_eq = exp(x) + x*y - 3 return [first_eq, second_eq] solution = opt.fsolve(f, (0.1,1) ) print(solution) st2 = timeit.default_timer() print("RUN TIME : {0}".format(st2-st1)) -> [ 0.62034452 1.83838393] RUN TIME : 0.0009331008900937708 

Fyi. as mentioned above, you can also use the "Broyden approximation" by replacing "fsolve" with "broyden1". It is working. I have done it.

I don’t know exactly how the Bruiden approximation works, but it took 0.02 s.

And I recommend that you do not use the Sympy <functions - conveniently, but in terms of speed it is rather slow. You'll see.

+3
Nov 04 '17 at 4:01
source share
 from scipy.optimize import fsolve def double_solve(f1,f2,x0,y0): func = lambda x: [f1(x[0], x[1]), f2(x[0], x[1])] return fsolve(func,[x0,y0]) def n_solve(functions,variables): func = lambda x: [ f(*x) for f in functions] return fsolve(func, variables) f1 = lambda x,y : x**2+y**2-1 f2 = lambda x,y : xy res = double_solve(f1,f2,1,0) res = n_solve([f1,f2],[1.0,0.0]) 
+2
Feb 19 '18 at 16:02
source share

You can use the openopt package and its NLP method. It has many dynamic programming algorithms for solving nonlinear algebraic equations, consisting of:
goldenSection, scipy_fminbound, scipy_bfgs, scipy_cg, scipy_ncg, amsg2p, scipy_lbfgsb, scipy_tnc, bobyqa, ralg, ipopt, scipy_slsqp, scipy_cobyla, lincher, you can choose.
Some of the latest algorithms can solve the problem of limited nonlinear programming. So, you can enter your system of equations in openopt.NLP () with this function:

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

+1
Feb 08 '14 at 20:36
source share

I got the Bruden method for working with related nonlinear equations (usually involving polynomials and exponentials) in the IDL, but I haven't tried it in Python:

http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.broyden1.html#scipy.optimize.broyden1

scipy.optimize.broyden1

 scipy.optimize.broyden1(F, xin, iter=None, alpha=None, reduction_method='restart', max_rank=None, verbose=False, maxiter=None, f_tol=None, f_rtol=None, x_tol=None, x_rtol=None, tol_norm=None, line_search='armijo', callback=None, **kw)[source] 

Find the root of the function using the first approximation of J. Ya. Brides.

This method is also known as the "good Broydens method."

+1
Jul 03 '15 at 7:20
source share

An alternative to fsolve is root :

 import numpy as np from scipy.optimize import root def your_funcs(X): x, y = X # all RHS have to be 0 f = [x + y**2 - 4, np.exp(x) + x * y - 3] return f sol = root(your_funcs, [1.0, 1.0]) print(sol.x) 

It will print

 [0.62034452 1.83838393] 

If you then check

 print(your_funcs(sol.x)) 

You get

 [4.4508396968012676e-11, -1.0512035686360832e-11] 

confirming the correctness of the decision.

0
Sep 23 '18 at 14:25
source share



All Articles