Home:ALL Converter>Solving an underdetermined nonlinear system of equations in python

Solving an underdetermined nonlinear system of equations in python

Ask Time:2021-10-14T03:54:10         Author:David_D

Json Formatter

I am trying to solve a underdetermined system of nonlinear equations in python. At the moment my plan is to proceed as follows but unfortunately it does not work out. I'll give a small example.

I have this input data.

Lins = np.array([[[1, 2, 3], [4, 5, 6], [1, 5, 2], [5, 2, 6]], 
                 [[7, 2, 3], [4, 5, 6], [5, 8, 7], [2, 1, 4]]], np.int32)

Each of the lines representing polynoms. Meaning [1, 2, 3] represents 1x^2+2x+3, and [5, 8, 7] 5x^2+8x+7 and so on.

To "create" the equations for my system I do the following:

def f(x):
    X = np.array([x**2, x, np.ones_like(x)]).T
    return np.sum(Lins * X[:,None], axis=(1, 2))

So my plan is to get this two equations with the four unknowns x1-x4:

1x1^2+2x1+3 + 4x2^2+5x2+6 + 1x3^2+5x3+6 + 5x4^2+2x4+6

7x1^2+2x1+3 + 4x2^2+5x2+6 + 5x3^2+8x3+7 + 2x4^2+1x4+4

After that I import my solver, and define the solution vector b.

from scipy.optimize import least_squares
b = np.array([52, 62])

Finally, I try to solve the system:

x = least_squares(lambda x: f(x) - b, np.asarray((1,1,1,1,1)), bounds=(0, 1)).x

I am expecting four values representing the solutions for the 4 unknowns x1-x4. Unfortunately I get this error:

ValueError: operands could not be broadcast together with shapes (2,4,3) (4,1,3)

For me it seems as if there is a mistake in the way I put my data into the least_squares solver. But I can`t figure out the problem. Or is least_squares not appropriate to solve underdetermined systems?

Thank you in advance for all of your help :)

Author:David_D,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69561581/solving-an-underdetermined-nonlinear-system-of-equations-in-python
yy