将预先确定的参数传递给scipy.optimize.fsolve。

时间:2021-04-20 21:20:19

I'm trying to use python to solve a system of 6 nonlinear equations. There are 9 variables, and 3 of them are predetermined (leaving a system of 6 equations with 6 unknowns). The problem is, it could be any 3, I have no way of knowing beforehand.

我试着用python来解一个6个非线性方程组。有9个变量,其中3个是预先确定的(剩下6个方程,6个未知数)。问题是,它可能是任何3,我没有办法事先知道。

Here are the equations (if you're interested).

这是方程(如果你感兴趣的话)。

c11*c12 + c21*c22 + c31*c32 = 0

c11*c12 + c21*c22 + c31*c32 = 0。

c11*c13 + c21*c23 + c31*c33 = 0

c11*c13 + c21*c23 + c31*c33 = 0。

c12*c13 + c22*c23 + c32*c33 = 0

c12*c13 + c23 + c32*c33 = 0。

c11*c21 + c12*c22 + c13*c23 = 0

c11*c21 + c12*c22 + c13*c23 = 0。

c11*c31 + c12*c32 + c13*c33 = 0

c11*c31 + c12*c32 + c13*c33 = 0。

c21*c31 + c22*c32 + c23*c33 = 0

c21*c31 + c32 + c23*c33 = 0。


Note: This is the way I assumed would be quickest/easiest to solve. Another possible expression is:

注:这是我认为最快/最容易解决的方法。另一个可能的表达式是:

    |c11 c21 c31|
A = |c12 c22 c32|
    |c13 c23 c33|

    |c11 c12 c13|
B = |c21 c22 c23|
    |c31 c32 c33|

      |1 0 0|
A*B = |0 1 0|
      |0 0 1|

My question is: is there anyway to set 3 of these as fixed, and have scipy.optimize.fsolve (or a more appropriate module?) solve for the remaining parameters?

我的问题是:是否有固定的3个,并且有scipy。优化。fsolve(或者一个更合适的模块?)解决剩下的参数?

1 个解决方案

#1


0  

So, I found a valid solution myself. Not sure if it's the best solution, but it's functional.

所以,我自己找到了一个有效的解决方案。不确定它是否是最好的解决方案,但它是功能性的。

To answer my question, scipy.optimize.fsolve takes a parameter args = (extra arguments here). I put the predetermined parameters in here. When the function is called, the args are first parsed and the 3 predetermined values placed in the appropriate spot.

为了回答我的问题,scipy.优化。fsolve接受参数args =(这里的额外参数)。我把预先确定的参数放在这里。当调用该函数时,首先解析args,并将3个预先确定的值放置在适当的位置。

The remaining 6 variables are passed in a list, and are iterated over to fill the remaining gaps. Since the arguments are not changing, each variable is always placed in the same spot in the matrix.

剩下的6个变量被传递到一个列表中,并遍历以填充其余的空白。由于参数不变,每个变量总是放在矩阵中的同一个位置。

Using this method, any 3 matrix elements can be predetermined, and fsolve will attempt to determine the remainder.

使用此方法,任意3个矩阵元素都可以预先确定,fsolve将尝试确定其余的。

The calling statement for fsolve looks like this:

fsolve的调用语句如下:

paramSolve1, infodict, ier, mesg = scipy.optimize.fsolve(func,(i,i,i,i,i,i),args = (knownVals[0],knownVals[1],knownVals[2]), full_output = True, warning = False)

knwonVals is a list of predetermined parameters, and i is a starting guess (all 6 missing parameters get the same starting guess). full_output allows the optional outputs to be returned, and warning = False turns off the warning message present when a solution isn't found. For more information, check out http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html

knwonVals是一个预先确定的参数列表,我是一个开始猜测(所有6个丢失的参数都有相同的开始猜测)。full_output允许返回可选的输出,并警告= False在未找到解决方案时关闭警告消息。要了解更多信息,请查看http://docs.scipy.org/doc/scipy/reference/generated/scipy.html。

For those interested, the entire code for the problem is below.

对于那些感兴趣的人,下面是整个问题的代码。

import scipy
from scipy.optimize import fsolve

def func(params, *args):
    c = propMatrix(createMatrix(args), params)

    ans =(scipy.dot(c[:, 0],c[:, 1]), scipy.dot(c[:, 1],c[:, 2]), scipy.dot(c[:, 0],c[:, 2]),scipy.dot(c[:, 0],c[:, 0])-1,scipy.dot(c[:, 1],c[:, 1])-1,scipy.dot(c[:, 2],c[:, 2])-1)
    return ans

def createMatrix(knownVals):

    c = [['____', '____', '____'],['____', '____', '____'], ['____', '____', '____']]  

    for element in knownVals:
        x, y, val = element
        c[y][x] = float(val)
    return c

def propMatrix(c, params):
    for p in params:
        assign = True
        for x in range(3):
            for y in range(3):
                if c[x][y]=='____' and assign:
                    c[x][y] = float(p)
                    assign = False

    return scipy.array(c)

def test(c):
    v1 = c[:, 0]
    v2 = c[:, 1]
    v3 = c[:, 2]
    h1 = c[0, :]
    h2 = c[1, :]
    h3 = c[2, :]
    ans = (scipy.dot(v1,v1)-1, scipy.dot(v1,v2), scipy.dot(v1, v3), scipy.dot(v2, v2)-1, scipy.dot(v2, v3), scipy.dot(v3,v3)-1, scipy.dot(h1,h1)-1, scipy.dot(h1,h2), scipy.dot(h1, h3), scipy.dot(h2, h2)-1, scipy.dot(h2, h3), scipy.dot(h3,h3)-1)
    return ans

def getInput():
    knownVals = []
    print """\n\nThis module analytically solves for the rotation matrix\n
    First, enter 3 known values of the matrix:\n
                 x
            1    2    3   
       1 | c11  c12  c13 |
    y  2 | c21  c22  c23 |
       3 | c31  c32  c33 |\n\n"""

    for i in range(3):
        invalid = True
        print "Point Number %i:"%(i)
        while invalid:
            x = int(raw_input("\tx-coordinate:"))-1
            if x>2 or x<0:
                print "\tInvalid x-coordinate."
            else:
                invalid = False
        invalid = True
        while invalid:
            y = int(raw_input("\ty-coordinate:"))-1
            if y>2 or y<0:
                print "\tInvalid y-coordinate."
            else:
                invalid = False
        invalid = True
        while invalid:
            val = float(raw_input("\tValue:"))
            if val>1 or val<-1:
                print "\tInvalid value. Must be -1 <= value <= 1"
            else:
                invalid = False
        knownVals.append((x, y, val))
    c = createMatrix(knownVals)
    print "Input Matrix:\n\n", scipy.array(c)
    choice = raw_input("\nIs this correct (y/n)?  ")
    if choice == "y":
        return knownVals
    elif choice == "n":
        return getInput()

def Main():
    solution = False
    knownVals = getInput()
    for i in (-1,-.5,0,.5,1):
        paramSolve1, infodict, ier, mesg = scipy.optimize.fsolve(func,(i,i,i,i,i,i),args = (knownVals[0],knownVals[1],knownVals[2]), full_output = True, warning = False)
        if ier == 1:
            print "\nInitial value: %r"%(i) 
            print propMatrix(createMatrix(knownVals),paramSolve1)
            solution = True
    if not solution:
        print "Could not find a valid solution"

scipy.set_printoptions(precision = 4, suppress = True)
Main()

#1


0  

So, I found a valid solution myself. Not sure if it's the best solution, but it's functional.

所以,我自己找到了一个有效的解决方案。不确定它是否是最好的解决方案,但它是功能性的。

To answer my question, scipy.optimize.fsolve takes a parameter args = (extra arguments here). I put the predetermined parameters in here. When the function is called, the args are first parsed and the 3 predetermined values placed in the appropriate spot.

为了回答我的问题,scipy.优化。fsolve接受参数args =(这里的额外参数)。我把预先确定的参数放在这里。当调用该函数时,首先解析args,并将3个预先确定的值放置在适当的位置。

The remaining 6 variables are passed in a list, and are iterated over to fill the remaining gaps. Since the arguments are not changing, each variable is always placed in the same spot in the matrix.

剩下的6个变量被传递到一个列表中,并遍历以填充其余的空白。由于参数不变,每个变量总是放在矩阵中的同一个位置。

Using this method, any 3 matrix elements can be predetermined, and fsolve will attempt to determine the remainder.

使用此方法,任意3个矩阵元素都可以预先确定,fsolve将尝试确定其余的。

The calling statement for fsolve looks like this:

fsolve的调用语句如下:

paramSolve1, infodict, ier, mesg = scipy.optimize.fsolve(func,(i,i,i,i,i,i),args = (knownVals[0],knownVals[1],knownVals[2]), full_output = True, warning = False)

knwonVals is a list of predetermined parameters, and i is a starting guess (all 6 missing parameters get the same starting guess). full_output allows the optional outputs to be returned, and warning = False turns off the warning message present when a solution isn't found. For more information, check out http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html

knwonVals是一个预先确定的参数列表,我是一个开始猜测(所有6个丢失的参数都有相同的开始猜测)。full_output允许返回可选的输出,并警告= False在未找到解决方案时关闭警告消息。要了解更多信息,请查看http://docs.scipy.org/doc/scipy/reference/generated/scipy.html。

For those interested, the entire code for the problem is below.

对于那些感兴趣的人,下面是整个问题的代码。

import scipy
from scipy.optimize import fsolve

def func(params, *args):
    c = propMatrix(createMatrix(args), params)

    ans =(scipy.dot(c[:, 0],c[:, 1]), scipy.dot(c[:, 1],c[:, 2]), scipy.dot(c[:, 0],c[:, 2]),scipy.dot(c[:, 0],c[:, 0])-1,scipy.dot(c[:, 1],c[:, 1])-1,scipy.dot(c[:, 2],c[:, 2])-1)
    return ans

def createMatrix(knownVals):

    c = [['____', '____', '____'],['____', '____', '____'], ['____', '____', '____']]  

    for element in knownVals:
        x, y, val = element
        c[y][x] = float(val)
    return c

def propMatrix(c, params):
    for p in params:
        assign = True
        for x in range(3):
            for y in range(3):
                if c[x][y]=='____' and assign:
                    c[x][y] = float(p)
                    assign = False

    return scipy.array(c)

def test(c):
    v1 = c[:, 0]
    v2 = c[:, 1]
    v3 = c[:, 2]
    h1 = c[0, :]
    h2 = c[1, :]
    h3 = c[2, :]
    ans = (scipy.dot(v1,v1)-1, scipy.dot(v1,v2), scipy.dot(v1, v3), scipy.dot(v2, v2)-1, scipy.dot(v2, v3), scipy.dot(v3,v3)-1, scipy.dot(h1,h1)-1, scipy.dot(h1,h2), scipy.dot(h1, h3), scipy.dot(h2, h2)-1, scipy.dot(h2, h3), scipy.dot(h3,h3)-1)
    return ans

def getInput():
    knownVals = []
    print """\n\nThis module analytically solves for the rotation matrix\n
    First, enter 3 known values of the matrix:\n
                 x
            1    2    3   
       1 | c11  c12  c13 |
    y  2 | c21  c22  c23 |
       3 | c31  c32  c33 |\n\n"""

    for i in range(3):
        invalid = True
        print "Point Number %i:"%(i)
        while invalid:
            x = int(raw_input("\tx-coordinate:"))-1
            if x>2 or x<0:
                print "\tInvalid x-coordinate."
            else:
                invalid = False
        invalid = True
        while invalid:
            y = int(raw_input("\ty-coordinate:"))-1
            if y>2 or y<0:
                print "\tInvalid y-coordinate."
            else:
                invalid = False
        invalid = True
        while invalid:
            val = float(raw_input("\tValue:"))
            if val>1 or val<-1:
                print "\tInvalid value. Must be -1 <= value <= 1"
            else:
                invalid = False
        knownVals.append((x, y, val))
    c = createMatrix(knownVals)
    print "Input Matrix:\n\n", scipy.array(c)
    choice = raw_input("\nIs this correct (y/n)?  ")
    if choice == "y":
        return knownVals
    elif choice == "n":
        return getInput()

def Main():
    solution = False
    knownVals = getInput()
    for i in (-1,-.5,0,.5,1):
        paramSolve1, infodict, ier, mesg = scipy.optimize.fsolve(func,(i,i,i,i,i,i),args = (knownVals[0],knownVals[1],knownVals[2]), full_output = True, warning = False)
        if ier == 1:
            print "\nInitial value: %r"%(i) 
            print propMatrix(createMatrix(knownVals),paramSolve1)
            solution = True
    if not solution:
        print "Could not find a valid solution"

scipy.set_printoptions(precision = 4, suppress = True)
Main()