1

我正在使用 odeint 来求解一组耦合的一阶非线性微分方程。函数 dHs(t,v) 是一个返回耦合 DE 数组的函数。我正在尝试解决并绘制它们。我以前这样用过:

def f(y, t):
    """Returns the collections of first-order 
    decoupled differential equations with initial 
    values substituted in"""
    v11i = y[0]
    v22i = y[1]
    v12i = y[2]

    # the model equations
    f0 = dHs(tRel,vij)[0].subs(v12,v12i)
    f1 = dHs(tRel,vij)[3].subs(v12,v12i)
    f2 = dHs(tRel,vij)[1].expand().subs([(v11,v11i),(v22,v22i),(v12,v12i)])
    return [f0, f1, f2]

# Global
# Initial conditions for equations
v110 = 6               
v220 = 6                 
v120 = 4                  
y0 = [v110, v220, v120]       # initial condition vector
sMesh  = np.linspace(0, 1, 10e3)   # time grid

# Solve the DE
soln = odeint(f, y0, sMesh)

然后它在哪里被绘制并且工作得很好,但是现在我试图概括它并使用数组中的n个条目dHs(tRel,vij),它给了我一个错误

TypeError                                 Traceback (most recent call last)
C:\Users\Justin\AppData\Local\Enthought\Canopy\System\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
    174             else:
    175                 filename = fname
--> 176             exec compile(scripttext, filename, 'exec') in glob, loc
    177     else:
    178         def execfile(fname, *where):

C:\Users\Justin\Research\srg.py in <module>()
    129 
    130 if __name__ == "__main__":
--> 131     main()

C:\Users\Justin\Research\srg.py in main()
    124 
    125     sMesh  = np.linspace(0, 1, 10e3)   # time grid
--> 126     odeSolver(dHsFunction(t, V_s).expand(), sMesh, v, V_0)
    127 
    128     print "Success"

C:\Users\Justin\Research\srg.py in odeSolver(arrEq, timeMesh, v, v_i)
     69     grid returns array of solutions."""
     70 
---> 71     soln = odeint(f,v_i,timeMesh)
     72 
     73     # Create lambsa mesh to take s->infinity

C:\Users\Justin\AppData\Local\Enthought\Canopy\User\lib\site-packages\scipy\integrate\odepack.pyc in odeint(func, y0, t, args, Dfun, col_deriv,     full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn,     mxords, printmessg)
    141     output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
    142                              full_output, rtol, atol, tcrit, h0, hmax, hmin,
--> 143                              ixpr, mxstep, mxhnil, mxordn, mxords)
    144     if output[-1] < 0:
    145         print(_msgs[output[-1]])

TypeError: __array__() takes exactly 1 argument (2 given)

对于代码

def f(arrEq, v, v_i):
    """Returns the collections of first-order 
    coupled differential equations"""
    # Substitute values of v0-(n-1) for initial values
    for i in range (0,len(arrEq)):
        for j in range (0,len(v_i)):
            arrEq[i] = arrEq[i].subs(v[j],v_i[j])

    return arrEq

def odeSolver(arrEq, timeMesh, v, v_i):
    """Solves a set of coupled oridinary differential 
    equations. Takes in arrray of coupled ODE's and time 
    grid returns array of solutions."""

    soln = odeint(f,v_i,timeMesh) 

    # Create lambsa mesh to take s->infinity
    lambMesh = map(lambda x: 1./x**(1./4), timeMesh)

    # Plot functions with respect to lambda
    plotEvolution(soln, lambMesh, v)
    return 

# Within main
sMesh  = np.linspace(0, 1, 10e3)   # time grid
odeSolver(dHsFunction(t, V_s), sMesh, v, V_0) 

其中 v 是用于替换 arrEq 的符号数组,V_0 是对应于 v 中相同位置的符号的初始值数组。

我在做什么/叫错了导致第二种方法不起作用但第一种方法起作用。我使用完全相同的输入(方程相同),只是用不同的方法来解决它。

4

0 回答 0