How to Solve a Pair of Nonlinear Equations Using Python

How to solve a pair of nonlinear equations using Python?

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))

Solve a system of non-linear equations in Python (scipy.optimize.fsolve)

This works perfectly fine:

In [1]: %paste
from scipy.optimize import fsolve

def equations(p):
x, y = p
return (y - x**2 -7 + 5*x, 4*y - 8*x + 21)

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

print(equations((x, y)))

## -- End pasted text --
(0.0, 0.0)

In [2]: x
Out[2]: 3.5000000414181831

In [3]: y
Out[3]: 1.7500000828363667

equations(x, y) being (0, 0) means that both y - x**2 -7 + 5*x and 4*y - 8*x + 21 are 0.

Maybe you got confused and meant to print(x, y) instead of print(equations(x, y)) ?

Having issues solving pair of nonlinear equations using Python

It is very possible that there is no solution. x + y = 10, x + y = 20 has no solution, for example. This isn't an issue of non-linearity; this is an issue of math. Also, it might be possible, if this can't be solved algebraically, that the first equation has f(x,y) - g(x,y) is approximately zero. If f(x,y)-g(x,y)=0.0001, would you consider this close enough?

For completeness: Check out the math, as noted by @tstanisl. If you add the equations together, you solve f(x,y)=c/2 or g(x,y)=c/2, which is easier.

How to solve a non-linear system in Python

If you re-write the functions:

-0.006683 x**2 - 0.06893 x + 56.73- z = 0
0.002538 y**2 - 1.115 y + 56.73 - z = 0
(x-24.680)**2+(y-238.341)**2+(z+13.971)**2 - 12.580**2 = 0

this helps a bit.

You've got three equations, and three unknowns.

The brute force method is to loop through x, y, and z values (over some domain of x, y, and z), and see how close all equations get to zero.

fa = -0.006683 x**2 - 0.06893 x + 56.73- z
fb = 0.002538 y**2 - 1.115 y + 56.73 - z
fc = (x-24.680)**2+(y-238.341)**2+(z+13.971)**2 - 12.580**2

Use a 'cost function', like cost=fa * fa + fb * fb + fc * fc , and look for the minimum.

There are other methods, like the Nelder-Mead method, which can be used, and are more efficient. https://en.wikipedia.org/wiki/Nelder%E2%80%93Mead_method

Once you find a minimum, you can take your original search range, and make it finer, depending on what sort of accuracy you need.

@warped gives a nicer, more Pythonic solution, however, there is always the possibility there is no solution, or multiple solutions. BTW, is this a geometry problem? The last equation looks like the equation of a sphere.

from scipy.optimize import fsolve
import math

def equations(p):
x, y, z = p
return (-0.006683 * x*x - 0.06893 * x + 56.73- z, \
0.002538 * y*y - 1.115 * y + 56.73 - z, \
(x-24.680)**2+(y-238.341)**2+(z+13.971)**2-12.580**2)

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

print (equations((x,y,z)))

print(x,y,z)

Using the method noted in this question using SymPy (answered in nice detail by @Oscar Benjamin), How to solve a pair of nonlinear equations using Python?

you can find other solutions in another way. However, this method didn't find any solutions, at least with my first stab at it.

from sympy import *

x, y, z = symbols('x, y, z')
eq1 = Eq(-0.006683 * x * x - 0.06893 * x + 56.73- z, 0)
eq2 = Eq(0.002538 * y * y - 1.115 * y + 56.73 - z, 0)
eq3 = Eq((x-24.680)**2+(y-238.341)**2+(z+13.971)**2-12.580**2, 0)

sol = solve([eq1, eq2, eq3], [x, y,z])

print(sol)
print("")
if(len(sol)>1):
soln = [tuple(v.evalf() for v in s) for s in sol]
for idx, each in enumerate(soln):
print(idx,each)

How to solve nonlinear equations using a for loop in python?

According to this documentation, the output of solve is the solution. Nothing is assigned to x, that's still just the symbol.

x = symbols('x')
for N in range(0,2500,5):
result = solve(a2*x**2+a1*N*x+a0*N**2-s2*x**2-s1*x-s0-0)
answer.append(result)

Solving nonlinear systems of equations

You need to solve the nonlinear equation system F(x,y) = 0 subject to x, y >= 0, which is equivalent to minimize the euclidean norm ||F(x,y)|| s.t. x,y >= 0. To solve this constrained optimization problem, you can use scipy.optimize.minimize as follows:

import numpy as np
from scipy.optimize import minimize

def Funcion(z):
x = z[0]
y = z[1]

F = np.empty((2))
F[0] = 78 * x**0.3 * y**0.8 - 376
F[1] = 77 * x**0.5 * y - 770
return F

# initial point
zGuess = np.array([1.0, 1.0])

# bounds x, y >= 0
bounds = [(0, None), (0, None)]

# Solve the constrained optimization problem
z = minimize(lambda z: np.linalg.norm(Funcion(z)), x0=zGuess, bounds=bounds)

# Print the solution
print(z.x)



Related Topics



Leave a reply



Submit