1

I don't have a lot of experience with Python but I decided to give it a try in solving the following system of equations:

x = A * exp (x+y)

y = 4 * exp (x+y)

I want to solve this system and plot x and y as a function of A.

I saw some a similar question and give fsolve a try:

`from scipy.optimize import fsolve
 def f(p):
   x, y = p
   A = np.linspace(0,4)
   eq1= x -A* np.exp(x+y)
   eq2= y- 4* np.exp(x+y)
   return (eq1,eq2)
 x,y =  fsolve(f,(0, 0))
 print(x,y)
 plt.plot(x,A)
 plt.plot(y,A)
`

I'm getting these errors: setting an array element with a sequence. Result from function call is not a proper array of floats.

Community
  • 1
  • 1
ziulfer
  • 1,339
  • 5
  • 18
  • 30
  • You haven't defined `A` in the code that you show. It would be easier for someone to help you if you provide a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve). – Warren Weckesser Mar 12 '19 at 19:29
  • Just edited, as I said my experience with Python is minimal and maybe the way I'm defining A is not correct, but I want to have A varying from 0 to 4 – ziulfer Mar 12 '19 at 19:35

1 Answers1

3

Pass the value of A as argument to the function and run fsolve for each value of A separately. Following code works.

from scipy.optimize import fsolve
import matplotlib.pyplot as plt
import numpy as np
def f(p,*args):
  x, y = p
  A = args[0] 

  return (x -A* np.exp(x+y),y- 4* np.exp(x+y))
A = np.linspace(0,4,5)
X = []
Y =[]
for a in A:
  x,y =  fsolve(f,(0.0, 0.0) , args=(a))
  X.append(x)
  Y.append(y)
  print(x,y)

plt.plot(A,X)
plt.plot(A,Y)

4.458297786441408e-17 -1.3860676807976662
-1.100088440495758 -0.5021704548996653
-1.0668987418054918 -0.7236105952221454
-1.0405000943788385 -0.9052366768954621
-1.0393471472966025 -1.0393471472966027
/usr/local/lib/python3.6/dist-packages/scipy/optimize/minpack.py:163: RuntimeWarning: The iteration is not making good progress, as measured by the 
  improvement from the last ten iterations.
  warnings.warn(msg, RuntimeWarning)
/usr/local/lib/python3.6/dist-packages/scipy/optimize/minpack.py:163: RuntimeWarning: The iteration is not making good progress, as measured by the 
  improvement from the last five Jacobian evaluations.
  warnings.warn(msg, RuntimeWarning)
[<matplotlib.lines.Line2D at 0x7f4a2a83a4e0>]

enter image description here

Siva-Sg
  • 2,741
  • 19
  • 27
  • That worked perfectly! I read here https://stackoverflow.com/questions/6519380/find-roots-of-a-function-a-xn-bx-c-0-where-n-isnt-an-integer-with-numpy that `brenqt` is much faster than `fsolve`. I've tried then to use it but keep getting `f(a) and f(b) must have different signs`. I understand that `f must be continuous. f(a) and f(b) must have opposite signs.` But in the real case my functions are too complex to know their signs before hand. Is it possible to use `brentq` in this case? – ziulfer Apr 08 '19 at 15:57
  • No , you can't . `brentq` is meant to find the root of an equation , not solve a system of equations. `a` and `b` refer to intervals of the same root. They must be scalars. In your case , you would like to solve for both `x` and `y`. However, you may want to try `scipy.optimize.root` which is meant for multivariate case. – Siva-Sg Apr 09 '19 at 01:48