简体   繁体   English

在python中求解一个欠定非线性方程组

[英]Solving an underdetermined nonlinear system of equations in python

I am trying to solve a underdetermined system of nonlinear equations in python.我正在尝试在 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.含义[1, 2, 3]代表1 x^2+2 x+3 ,而[5, 8, 7] 5 x^2+8 x+7等等。

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:所以我的计划是用四个未知数 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 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 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.之后,我导入我的求解器,并定义解向量 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.我期待代表 4 个未知数 x1-x4 的解的四个值。 Unfortunately I get this error:不幸的是,我收到此错误:

ValueError: operands could not be broadcast together with shapes (2,4,3) (4,1,3) ValueError:操作数无法与形状一起广播 (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.对我来说,我将数据放入least_squares求解器的方式似乎存在错误。 But I can`t figure out the problem.但我无法弄清楚问题所在。 Or is least_squares not appropriate to solve underdetermined systems?或者least_squares不适合解决欠定系统?

Thank you in advance for all of your help :)预先感谢您的所有帮助:)

The Vandermonde Matrix is a very useful matrix for this problem. Vandermonde 矩阵是解决这个问题的一个非常有用的矩阵。 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.如果我们使用函数 f 参数 x (=[x1, x2, x3, x4]) 创建一个 'vander' 矩阵,那么我们可以创建您在计划中描述的方程。

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.我不太熟悉解决这些系统,所以我不能说程序的其余部分是否会产生正确的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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