用python绘制颂歌

2024-04-29 08:10:07 发布

您现在位置:Python中文网/ 问答频道 /正文

当我编译下面的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

Tags: importplotasnpplteqdhiodeint
1条回答
网友
1楼 · 发布于 2024-04-29 08:10:07

在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()

相关问题 更多 >