如何用已知的一些变量数值求解多变量联立方程?
How to solve multi-variable simultaneous equations numerically with some variables known?
How to solve a pair of nonlinear equations using Python?
在这道题中求解了一对非线性方程,每个方程都有两个参数。
现在我有两个以上的方程式,每个方程式都有多个参数。参数个数多于方程个数
通过问题中的方法。
例如:
'''
def func0(a,b,c,d,local1=α,local2=β):
#process
return function
# func0==0
def func1():
return
#etc
def multi_equ(p):
a,b,c,d,e = p
return (func0(a,b,c,d,local1,local2),func1(c,d,e),func2(a,b,c,d,e,local),etc)
'''
我的问题是:
如何编写 fsolve() 来解决此类函数,例如,有时参数 c 已知,我正在尝试解决其余问题。
要使用 fsolve
,您需要
A function that takes at least one (possibly vector) argument, and returns a value of the same length.
所以,方程的个数必须等于未知数的个数。如果你有一个接受类似输入的函数,除了其中一个输入变量是已知的,然后将它包装在另一个函数中,如下所示:
def f(a, b, c, d):
return [a-b, b-c, c-d]
def func(p):
a, b, d = p
c = 0.5
return f(a, b, c, d)
这里func
适合用在fsolve
,例如
fsolve(func, [0, 0, 0]) # returns [0.5, 0.5, 0.5]
与方程数不同的未知数数
如果数学问题的未知数比方程式多(或少),则不能使用fsolve
。我将使用的工具是 least_squares
,它可以最小化 func
的平方和。
def func(p):
a, b, c, d = p
return [a-b, b-c, c-d]
least_squares(func, [3, 2, 5, 1])
找到最小二乘问题的解 [2.75, 2.75, 2.75, 2.75]
。它还 returns 成本几乎为零,告诉我们 func
确实在找到的点处变为零。这个解决方案不是唯一的。
How to solve a pair of nonlinear equations using Python? 在这道题中求解了一对非线性方程,每个方程都有两个参数。
现在我有两个以上的方程式,每个方程式都有多个参数。参数个数多于方程个数
通过问题中的方法。 例如:
'''
def func0(a,b,c,d,local1=α,local2=β):
#process
return function
# func0==0
def func1():
return
#etc
def multi_equ(p):
a,b,c,d,e = p
return (func0(a,b,c,d,local1,local2),func1(c,d,e),func2(a,b,c,d,e,local),etc)
'''
我的问题是:
如何编写 fsolve() 来解决此类函数,例如,有时参数 c 已知,我正在尝试解决其余问题。
要使用 fsolve
,您需要
A function that takes at least one (possibly vector) argument, and returns a value of the same length.
所以,方程的个数必须等于未知数的个数。如果你有一个接受类似输入的函数,除了其中一个输入变量是已知的,然后将它包装在另一个函数中,如下所示:
def f(a, b, c, d):
return [a-b, b-c, c-d]
def func(p):
a, b, d = p
c = 0.5
return f(a, b, c, d)
这里func
适合用在fsolve
,例如
fsolve(func, [0, 0, 0]) # returns [0.5, 0.5, 0.5]
与方程数不同的未知数数
如果数学问题的未知数比方程式多(或少),则不能使用fsolve
。我将使用的工具是 least_squares
,它可以最小化 func
的平方和。
def func(p):
a, b, c, d = p
return [a-b, b-c, c-d]
least_squares(func, [3, 2, 5, 1])
找到最小二乘问题的解 [2.75, 2.75, 2.75, 2.75]
。它还 returns 成本几乎为零,告诉我们 func
确实在找到的点处变为零。这个解决方案不是唯一的。