用python求解非线性超定系统
我正试图找到一种用python解决非线性超定系统的好方法。我在这里研究了优化工具,但我不知道如何使用它们。到目前为止,我得到的是用python求解非线性超定系统,python,scipy,Python,Scipy,我正试图找到一种用python解决非线性超定系统的好方法。我在这里研究了优化工具,但我不知道如何使用它们。到目前为止,我得到的是 #overdetermined nonlinear system that I'll be using ''' a = cos(x)*cos(y) b = cos(x)*sin(y) c = -sin(y)
#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)
你能帮我一点忙吗?如果我没听错,你想找到非线性方程组的近似解
f(x)=b
,其中b是包含随机值的向量b=[a,…,h]
为此,首先需要从系统
函数中删除随机值,否则在每次迭代中,解算器将尝试解算不同的方程系统。此外,我认为基本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]如果我理解正确,您希望找到非线性方程组的近似解f(x)=b
其中b是包含随机值的向量b=[a,…,h]
为此,首先需要从系统
函数中删除随机值,否则在每次迭代中,解算器将尝试解算不同的方程系统。此外,我认为基本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]