让我们假设我有一组与scipy odeint集成的微分方程。现在我的目标是找到稳态(我选择了这种状态存在的初始条件)。目前我实施了类似
的内容<vm-dns-name>.cloudapp.net:<public-port>
你有更有效的方法吗?
答案 0 :(得分:5)
如果您使用odeint
,那么您已经将微分方程编写为函数f(x, t)
(或可能f(x, t, *args)
)。如果您的系统是自主的(即f
实际上并不依赖于t
),您可以通过f(x, 0) == 0
解析x
找到均衡。例如,您可以使用scipy.optimize.fsolve
来解决均衡问题。
以下是一个例子。它使用"Coupled Spring Mass System" example from the scipy cookbook。 scipy.optimize.fsolve
用于查找均衡解x1 = 0.5
,y1 = 0
,x2 = 1.5
,y2 = 0
。
from scipy.optimize import fsolve
def vectorfield(w, t, p):
"""
Defines the differential equations for the coupled spring-mass system.
Arguments:
w : vector of the state variables:
w = [x1, y1, x2, y2]
t : time
p : vector of the parameters:
p = [m1, m2, k1, k2, L1, L2, b1, b2]
"""
x1, y1, x2, y2 = w
m1, m2, k1, k2, L1, L2, b1, b2 = p
# Create f = (x1', y1', x2', y2'):
f = [y1,
(-b1 * y1 - k1 * (x1 - L1) + k2 * (x2 - x1 - L2)) / m1,
y2,
(-b2 * y2 - k2 * (x2 - x1 - L2)) / m2]
return f
if __name__ == "__main__":
# Parameter values
# Masses:
m1 = 1.0
m2 = 1.5
# Spring constants
k1 = 8.0
k2 = 40.0
# Natural lengths
L1 = 0.5
L2 = 1.0
# Friction coefficients
b1 = 0.8
b2 = 0.5
# Pack up the parameters and initial conditions:
p = [m1, m2, k1, k2, L1, L2, b1, b2]
# Initial guess to pass to fsolve. The second and fourth components
# are the velocities of the masses, and we know they will be 0 at
# equilibrium. For the positions x1 and x2, we'll try 1 for both.
# A better guess could be obtained by solving the ODEs for some time
# interval, and using the last point of that solution.
w0 = [1.0, 0, 1.0, 0]
# Find the equilibrium
eq = fsolve(vectorfield, w0, args=(0, p))
print "Equilibrium: x1 = {0:.1f} y1 = {1:.1f} x2 = {2:.1f} y2 = {3:.1f}".format(*eq)
输出结果为:
Equilibrium: x1 = 0.5 y1 = 0.0 x2 = 1.5 y2 = 0.0
答案 1 :(得分:0)
参考你的评论,我认为没有更好的方法:
在这种情况下,您可以了解系统的定位。 (这在一些系统中很容易预测,比如摆锤或充电电容器),它会解决,我知道的最快方法是检查是否
FaceColor
困难在于确定epsilon的大小,参数p [0:n]来调整此检测。
显然,您已经在使用此窗口方法:
(p[0] * x[i] + p[1] * x[i-1] ... + p[n] * x[i-n] - mean(x[i-0:n]) )< epsilon)
并通过删除过滤器的参数并简单地使用方差来优化它。
对于许多系统(沉降看起来像一阶微分方程),滤波器参数看起来像这样:
epsilon = varmax
p[0:n] = 1
n = 22
意味着如果用这个简单的测试替换状态方差的计算,你可以让事情变得更快:
p[0] = n/2
p[n] = n/2
p[1:n-1] = 0
如果仍然存在小干扰,这将无法正确检测,因为我们不了解您的系统,这很难谈......