实现一个强制函数以用于SciPy的resolve_ivp函数

问题描述

我正在尝试使用SciPy的 solve_ivp 函数解决具有强迫函数的线性单自由度弹簧质量振荡器的运动。但是,我不知道如何在代码中合并强制功能

一些计算机信息

  1. 操作系统:Windows 10
  2. IDE:Spyder 4.1.4
  3. Python:Python 3.7.3 64位| Qt 5.9.6 | PyQt5 5.9.2 | Windows 10

我用于执行微分方程组的代码如下:

 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,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

每当我运行此代码时,我的Spyder IDE都会出现以下错误: “ ValueError:设置具有序列的数组元素。”

如果将 deriv 函数中的变量“ ff”修改为常数,则 solve_ivp 函数可以成功求解系统。因此,我认为我的强制功能在脚本中的建模不正确。我认为时间向量可能会传递给“ ff”变量。

我希望这些信息能对您有所帮助;如果您需要更多信息,请告诉我。

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)