【问题标题】:Solving nonlinear overdetermined system using python使用python解决非线性超定系统
【发布时间】:2015-04-16 21:56:57
【问题描述】:

我正在尝试找到一种使用 python 解决非线性超定系统的好方法。我在这里查看了优化工具http://docs.scipy.org/doc/scipy/reference/optimize.nonlin.html,但我不知道如何使用它们。到目前为止我所拥有的是

#overdetermined nonlinear system that I'll be using
'''
a = cos(x)*cos(y)                           
b = cos(x)*sin(y)                           
c = -sin(y)                                   
d = sin(z)*sin(y)*sin(x) + cos(z)*cos(y)    
e = cos(x)*sin(z)                           
f = cos(z)*sin(x)*cos(z) + sin(z)*sin(x)    
g = cos(z)*sin(x)*sin(y) - sin(z)*cos(y)    
h = cos(x)*cos(z)
a-h will be random int values in the range 0-10 inclusive
'''
import math
from random import randint
import scipy.optimize

def system(p):
    x, y, z = p
    return(math.cos(x)*math.cos(y)-randint(0,10),
           math.cos(x)*math.sin(y)-randint(0,10),
           -math.sin(y)-randint(0,10),
           math.sin(z)*math.sin(y)*math.sin(x)+math.cos(z)*math.cos(y)-randint(0,10),
           math.cos(x)*math.sin(z)-randint(0,10),
           math.cos(z)*math.sin(x)*math.cos(z)+math.sin(z)*math.sin(x)-randint(0,10),
           math.cos(z)*math.sin(x)*math.sin(y)-math.sin(z)*math.cos(y)-randint(0,10),
           math.cos(x)*math.cos(z)-randint(0,10))

x = scipy.optimize.broyden1(system, [1,1,1], f_tol=1e-14)

你能帮我一下吗?

【问题讨论】:

    标签: python scipy


    【解决方案1】:

    如果我的理解正确,您想找到非线性方程组 f(x) = b 的近似解,其中 b 是包含随机值 b=[a,...,h] 的向量。

    为此,您首先需要从system 函数中删除随机值,否则在每次迭代中,求解器将尝试求解不同的方程组。此外,我认为基本的 Broyden 方法仅适用于具有与方程一样多的未知数的系统。或者,您可以使用scipy.optimize.leastsq。一个可能的解决方案如下所示:

    # I am using numpy because it's more convenient for the generation of
    # random numbers.
    import numpy as np
    from numpy.random import randint
    import scipy.optimize
    
    # Removed random right-hand side values and changed nomenclature a bit.
    def f(x):
        x1, x2, x3 = x
        return np.asarray((math.cos(x1)*math.cos(x2),
                           math.cos(x1)*math.sin(x2),
                           -math.sin(x2),
                           math.sin(x3)*math.sin(x2)*math.sin(x1)+math.cos(x3)*math.cos(x2),
                           math.cos(x1)*math.sin(x3),
                           math.cos(x3)*math.sin(x1)*math.cos(x3)+math.sin(x3)*math.sin(x1),
                           math.cos(x3)*math.sin(x1)*math.sin(x2)-math.sin(x3)*math.cos(x2),
                           math.cos(x1)*math.cos(x3)))
    
    # The second parameter is used to set the solution vector using the args
    # argument of leastsq.
    def system(x,b):
        return (f(x)-b)
    
    b = randint(0, 10, size=8)
    x = scipy.optimize.leastsq(system, np.asarray((1,1,1)), args=b)[0]
    

    我希望这对您有所帮助。但是请注意,您极不可能找到解决方案,尤其是当您在区间 [0,10] 内生成随机整数而 f 的范围限制为 [-2,2] 时

    【讨论】:

      猜你喜欢
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 2017-03-26
      • 2019-01-15
      • 2019-10-05
      • 1970-01-01
      • 2014-05-30
      相关资源
      最近更新 更多