【问题标题】:error in solving acoupled,first order differential equations in ipython在 ipython 中求解耦合的一阶微分方程时出错
【发布时间】:2023-03-24 19:18:02
【问题描述】:

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

  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.

我的代码有什么问题?

【问题讨论】:

    标签: scipy ipython


    【解决方案1】:
    • 您有三个一阶方程,而不是二阶。 (或者您的意思是在方程式 1、2 和 3 中写出二阶导数?)
    • deriv 应该返回三个值 dA/dt、dB/dt 和 dJ/dt,但目前它返回一个长度为 5 的数组。
    • zinit 应包含三个值:A(0)、B(0) 和 J(0)。

    【讨论】:

      【解决方案2】:

      我想我已经解决了。

          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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-28
        • 1970-01-01
        • 2012-10-14
        • 2012-02-28
        • 2021-06-14
        相关资源
        最近更新 更多