ipython 中的耦合一阶微分方程求解错误

error in solving acoupled,first order differential equations in ipython

我有以下耦合的一阶微分方程:

  1. dB(t)/dt=-a*A(t);
  2. dA(t)/dt=b*B(t);
  3. dJ(t)/dt=cA(t)-dB(t)

在 0 到 200 的时间间隔内使用您自己选择的参数 a = 0.05、b = 0.02、c = 0.03 和 d = 0.04 求解系统。 这是我的代码:

a=0.05
b=0.02
c=0.03
d=0.04

def function(x,t):
    x1, x2, x3 = x[0], x[1], x[2]
    #x1, x2, x3 = A, B, J
    dx1=b*x1
    dx2=-a*x0
    dx3=c*x0-d*x1
    return [dx1, dx2, dx3]
x0 = [100,100, 1]
t = linspace(0, 200, 200)
x = odeint(function, x0, t)

我得到的输出是:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-f8f398346307> in function(x, t)
      8     #x1, x2, x3 = A, B, J
      9     dx1=b*x1
---> 10     dx2=-a*x0
     11     dx3=c*x0-d*x1
     12     return [dx1, dx2, dx3]

TypeError: can't multiply sequence by non-int of type 'float'

Repeated convergence failures (perhaps bad Jacobian or tolerances).
Run with full_output = 1 to get quantitative information.

我的代码有什么问题?

  • 你有三个一阶方程,不是二阶。 (或者你的意思是在方程式 1、2、3 中写二阶导数?)
  • deriv应该return三个值dA/dt、dB/dt和dJ/dt,但目前return是一个长度数组5.
  • zinit 应包含三个值:A(0)、B(0) 和 J(0)。

我想我已经修好了。

    a=0.05
    b=0.02
    c=0.03
    d=0.04

    def function(x,t):
      x1, x2, x3 = x[0], x[1], x[2]
      #x1, x2, x3 = A, B, J
      dx1=b*x2 
      dx2=-a*x1
      dx3=c*x1-d*x2
      return [dx1, dx2, dx3]
   x0 = [100,100, 1]
   t = linspace(0, 200, 200)
   x = odeint(function, x0, t)