简体   繁体   中英

Solving an underdetermined nonlinear system of equations in python

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 1 x^2+2 x+3 , and [5, 8, 7] 5 x^2+8 x+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:

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

7 x1^2+2 x1+3 + 4 x2^2+5 x2+6 + 5 x3^2+8 x3+7 + 2 x4^2+1 x4+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 :)

The Vandermonde Matrix is a very useful matrix for this problem. If we create a 'vander' matrix using the function f argument x (=[x1, x2, x3, x4]), then we can create the equations you described in your plan.

def f(x): ## x = [x1, x2, x3, x4]
    X = np.vander(x, 3)  ## Vandermonde Matrix [[x^2 x 1] ...]
    return np.sum(Lins * X, axis=(1, 2))

I am less familiar with solving these systems and so I cannot say whether the rest of the program will produce the correct solutions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM