0

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.

Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193

2 Answers2

2

Check if this solve your equation:

import numpy as np
from scipy.optimize import fsolve

def eqs(X, Y):
    A, B = X[:3], X[3]
    C, D = Y[:3], Y[3]
    eq1 = A - B * np.sin(C)
    eq2 = C[0] * B + D * np.sum(A * np.cos(C))
    return np.append(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 = np.append(A0, B0), args = np.append(C, D))
sol

Output:

array([ 0.e+000, -1.e-323,  5.e-324, -1.e-323])
Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
Zaraki Kenpachi
  • 5,510
  • 2
  • 15
  • 38
  • but then how do you pass D and C? I want them to be passed because they are not necessarily constants as shown in the arbitrary code. – Foad S. Farimani Jun 27 '19 at 11:05
1

These scipy.optimize functions require a function with a signature like

f(x, *args)

x is a array (often 1d) that the solver will vary; args is a tuple of arguments that are just passed through from the outside.

Change your eqs to fit this pattern

In [11]: def eqs(X, C, D): 
    ...:     A, B  = X[:-1], X[-1] 
    ...:     eq1 = (A - B * np.sin(C)).tolist() 
    ...:     eq2 = [5 * B + D * np.sum(A * np.cos(C))] 
    ...:     return eq1 + eq2 

    ...: n = 3 
    ...: A0 = np.random.rand(n) 
    ...: B0 = np.random.rand(1) 
    ...:  
    ...: C = np.random.rand(n) 
    ...: D =  np.random.rand(1)  

Make a test call to eqs:

In [12]: eqs(np.concatenate((A0,B0)),C,D)                                                            
Out[12]: 
[-0.28460532658572657,
 -0.03649115738682615,
 0.7625781482352719,
 array([5.46430853])]

Now try it in the fsolve:

In [13]: fsolve(eqs, np.concatenate((A0,B0)), args=(C,D))                                            
Out[13]: array([0., 0., 0., 0.])
hpaulj
  • 221,503
  • 14
  • 230
  • 353