fsolve finds a solution of (a system of) nonlinear equations from a starting estimate. I can vectorize my function call to use fsolve on multiple starting points and potentially find multiple solutions, as explained here. In this question it is described how to solve multiple nonlinear equations with fsolve. However, I am having problems combining the two, i.e. solving multiple nonlinear equations from multiple starting values. I know that I can always loop through my starting values and use the second posts answer, but, having to do this for possibly more than a 100000 points, I'm really trying to find a more pythonic (and hopefully faster) solution.
I tried different ways, for example the following (and many others):
from scipy.optimize import fsolve
import numpy as np
def equations(x): # x+y^2-4, sin(x)+x*y-3
ret = np.array([x[:,0]+x[:,1]**2-4, np.sin(x[:,0]) + x[:,0]*x[:,1] - 3]).T
return ret
p1 = np.array([0,0]) # first initial value
p2 = np.array([1,1]) # second initial value
x0 = np.array([p1,p2])
print(x0[0,1])
print(equations(x0))
print(fsolve(equations, x0=x0))
The shapes and all work, but fsolve
throws: 'IndexError: too many indices for array'
I tried some different ways, but I can't work out any functioning code on this besides using a simple for loop. Any suggestions?