在python中绘制ode

时间:2017-10-19 18:42:35

标签: python plot ode

当我编译下面的Python代码时

   import numpy as np
    from scipy.integrate import odeint
    import matplotlib.pyplot as plt

    def f(s,t):
     a = 20
     b =  1
     c =  1
     d =  3
     E =  3
     f =  2
     g =  3
     h =  1
     eq0 = S[0]
     eq1 = s[1]
     eq2 = S[2]
     eq3 = s[3]
     dHs = a-(a+b*eq3)*eq0+c*eq2
     dHi = b*eq3*eq0-(a+d+g)*eq1
     dHr = d*eq1-(a+c)*eq2
     dAs = eq3*(E-E*eq3)-h*eq3

     return [dHs, dHi, dHr, dAs]

t= np.linspace(0,20)
eq0= [20,5]

s= odeint(f,s0,t)
plt.plot(t,eq[:,0],'r--',Linewidth=2.0)
plt.plot(t,eq[:,1],'r--',Linewidth=2.0)
plt.plot(t,eq[:,2],'r--',Linewidth=2.0)
plt.plot(t,eq[:,3],'r--',Linewidth=2.0)
plt show()

我收到了这个错误,但我没有'知道修复此错误

 File "tentativa.py", line 7
    b =  1
         ^
IndentationError: unindent does not match any outer indentation level

1 个答案:

答案 0 :(得分:0)

你应该小心python中的偏移量。它意味着使用相同的空格用于"相同级别的代码" (通常是4)。另外,您可能错过了plt show()中的点。

请检查以下代码:

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

def f(s,t):
     a = 20
     b =  1
     c =  1
     d =  3
     E =  3
     f =  2
     g =  3
     h =  1
     eq0 = S[0]
     eq1 = s[1]
     eq2 = S[2]
     eq3 = s[3]
     dHs = a-(a+b*eq3)*eq0+c*eq2
     dHi = b*eq3*eq0-(a+d+g)*eq1
     dHr = d*eq1-(a+c)*eq2
     dAs = eq3*(E-E*eq3)-h*eq3

     return [dHs, dHi, dHr, dAs]

t= np.linspace(0,20)
eq0= [20,5]

s=odeint(f,s0,t)
plt.plot(t,eq[:,0],'r--',Linewidth=2.0)
plt.plot(t,eq[:,1],'r--',Linewidth=2.0)
plt.plot(t,eq[:,2],'r--',Linewidth=2.0)
plt.plot(t,eq[:,3],'r--',Linewidth=2.0)
plt.show()