I have a non-linear system of equations built with sympy with more variables than equations.
Which is the best package to solve this kind of system using Python?
I’m not sure how to manage the presence of free variables.
I don’t show my system because I think it’s useless, I only need to understand the general path.
I tried to use sympy.nonlinsolve
but it gives me as a result my symbolic variables (for example the first variable is a1 and the result is ‘a1’)
Nico Loreti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
You can use scipy.optimize.minimize, your variables will be p as a numpy 1D array.
Your equations will be in form ax+by+… = cst
And the return of the score function will be the sum of all equations (it may not be zero if overconstrained and will be zero if underconstrained)
As you have less variables than equations you have infinite solutions possibles.
Here is the documentation: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html
let says you have a python variable A symbolizing all equations for rows and variables for columns:
def fun(x, A, B):
return numpy.sum(numpy.abs(numpy.dot(A, x) - B))
A = numpy.array([
[1, 2, 3], # 1*x + 2*y + 3*z = B[0]
[0, 2, 1] # 0*x + 2*y + 1*z = B[1]
])
B = numpy.array([5, 10])
x0 = numpy.array([var1_init, var2_init, ...])
variables = scipy.optimize.minimize(fun, x0, args=(A)).x
The result will depend of the minimize method you are using and the x0 initialisation. If you just want one solution, you don t fill method field and initialize at 0 each variables of x0.