I want to numerically solve a system of nonlinear equations and pass numpy ndarrays as inputs. Consider the arbitrary code below:
import numpy as np
from scipy.optimize import fsolve
def eqs(A, B, C, D):
eq1 = (A - B * np.sin(C)).tolist()
eq2 = [5 * B + D * np.sum(A * np.cos(C))]
return eq1 + eq2
n = 3
A = np.zeros((n))
A0 = np.random.rand(n)
B = 0.0
B0 = np.random.rand(1)[0]
C = np.random.rand(n)
D = np.random.rand(1)[0]
sol = fsolve(func = eqs, x0 = [A0, B0], args = [C, D])
which leads to
missing required positional arguments
error and changing the function to:
def eqs(A, B, C, D):
eq1 = A - B * np.sin(C)
eq2 = C[0] * B + D * np.sum(A * np.cos(C))
return [eq1, eq2]
also doesn't help. However, I highly doubt that the error has anything to do with passing ndarrays. One approach could be to change all the ndarrays to python lists back and forth. But then I would not be able to use numpy's vectorized functions like np.sin()
...
I would appreciate if you could help me know how this should be done.
P.S. Equations above are just arbitrary and they may not have solutions at all.