Python 实现用于SciPy'的强制功能;s求解ivp函数
我试图用SciPy的solve_ivp函数来解一个线性单自由度弹簧质量振子的运动,这个振子有一个力函数。但是,我不知道如何将强制函数合并到代码中 一些计算机信息Python 实现用于SciPy'的强制功能;s求解ivp函数,python,scipy,numerical-methods,differential-equations,Python,Scipy,Numerical Methods,Differential Equations,我试图用SciPy的solve_ivp函数来解一个线性单自由度弹簧质量振子的运动,这个振子有一个力函数。但是,我不知道如何将强制函数合并到代码中 一些计算机信息 操作系统:Windows 10 IDE:Spyder 4.1.4 Python:Python3.7.3 64位| qt5.9.6 | PyQt5 5.9.2 | Windows 10 我的微分方程系统实现代码如下: def deriv( self, t, x, wn ): """ The
def deriv( self, t, x, wn ):
"""
The system of first order equations that is used to represent the system
Original equation: x'' + (wn)**2*x = ff
The 2nd order equation is then reduced to two first order equations
x[1] = x''
x[0] = ff - (wn)**2*x
"""
ff = cos(2*pi*t)
dxdt = [ x[1], ff - wn**2*x[0] ]
return dxdt
我的解算器实现代码如下所示:
def time_vector( self, t0, tf, dt ):
if ( t0 >= tf ):
print("ERROR....START TIME GREATER THAN OR EQUAL TO END TIME\n")
self.time_error = -1
if ( t0 < tf ):
self.t0 = float( t0 )
self.tf = float( tf )
self.dt = float( dt )
self.t = arange( start = t0, stop = tf, step = dt )
def solve( self ):
time_span = [ self.t0, self.tf ]
ic = [ self.x0, self.xdot0 ]
self.soln = solve_ivp(lambda t, x: self.deriv(self.t, x, self.wn),
t_span = time_span, y0 = ic, dense_output = True,
t_eval = self.t)
self.disp = self.soln.y[0, :]
self.vel = self.soln.y[1, :]
self.soln_time = self.soln.t
def时间向量(self,t0,tf,dt):
如果(t0>=tf):
打印(“错误…开始时间大于或等于结束时间\n”)
self.time\u错误=-1
如果(t0
每当运行此代码时,我都会通过Spyder IDE收到以下错误:
“ValueError:使用序列设置数组元素。”
如果我将deriv函数中的变量“ff”修改为常数,则solve_ivp函数能够成功求解系统。因此,我认为我的强制函数在脚本中的建模是错误的。我认为它可能与时间向量传递到“ff”变量的方式有关
我希望这些信息有帮助;如果你需要更多的信息,请告诉我
谢谢您需要传递给定的
t
值,而不是self.t
。无需将self.wn
作为参数,因为该类字段也可在class函数deriv
中使用。然后您也不需要lambda
构造。请注意,在scipy的最新版本中,solve\u ivp
中还有一个args
可选参数。