在python中实现odeint有什么问题?

时间:2013-10-08 18:58:50

标签: python numpy scipy ode odeint

我正在使用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中的相同位置。

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

0 个答案:

没有答案