** Coupled nonlinear equations
:PROPERTIES:
:categories: nonlinear algebra
:date: 2013/09/02 15:21:24
:updated: 2013/09/02 15:21:24
:END:
Suppose we seek the solution to this set of equations:
\begin{align}
y &=& x^2 \\
y &=& 8 - x^2
\end{align}
To solve this we need to setup a function that is equal to zero at the solution. We have two equations, so our function must return two values. There are two variables, so the argument to our function will be an array of values.
#+BEGIN_SRC python
from scipy.optimize import fsolve
def objective(X):
x, y = X # unpack the array in the argument
z1 = y - x**2 # first equation
z2 = y - 8 + x**2 # second equation
return [z1, z2] # list of zeros
x0, y0 = 1, 1 # initial guesses
guess = [x0, y0]
sol = fsolve(objective, guess)
print sol
# of course there may be more than one solution
x0, y0 = -1, -1 # initial guesses
guess = [x0, y0]
sol = fsolve(objective, guess)
print sol
#+END_SRC
#+RESULTS:
: [ 2. 4.]
: [-2. 4.]