2017-12-31 356 views
0

私はPythonでlokta volterraアルゴリズムを書いています。値がなくても関数が返りません。なし

私は、次のエラー

File "abeille.py", line 73, in affichage_Lotka_Volterra_TL 
    T,L,Y=Lotka_Volterra(l_0,y_0,tmin,tmax,h) 

TypeError: 'NoneType' object is not iterable 

を持って、ここでは、そのようなあなたのreturn行を分割することはできませんコード

import numpy as np 
import matplotlib.pyplot as plt 



a,b,c,d=1.5,0.05,0.48,0.05 

'''a=taux de reproduction des proies (constant, indépendant du nombre de prédateurs) 

b=taux de mortalité des proies dû aux prédateurs rencontrés 

c=taux de mortalité des prédateurs (constant, indépendant du nombre de proies) 

d=taux de reproduction des prédateurs en fonction des proies rencontrées et mangées''' 



def lapin(l,y): 
    return 
    a*l-b*l*y 



def lynx(l,y): 
    return 
    d*l*y-c*y 



def Lotka_Volterra(l_0,y_0,tmin,tmax,h): 
    liste_t=[0]  
    liste_l=[l_0] 
    liste_y=[y_0] 
    t=tmin 
    dy=y_0 
    dl=l_0 
    while t<=tmax: 
     t+=h 
     liste_t.append(t) 
     dl+=(a*dl-b*dl*dy)*h 
     dy+=(d*dl*dy-c*dy)*h 
     liste_l.append(dl) 
     liste_y.append(dy) 
    return 
    liste_t,liste_l,liste_y 




def affichage_Lotka_Volterra_TL(l_0,y_0,tmin,tmax,h): 
'''Population de lièvres en fonction du temps''' 
    T,L,Y=Lotka_Volterra(l_0,y_0,tmin,tmax,h) 
    plt.plot(T,L) 
    plt.title('Population de lièvres au cours du temps\nConditions initiales : 4 kilolièvres pour 10 lynx sur une durée de 50 ans') 
    plt.xlabel('Temps d-étude (en année)') 
    plt.ylabel('L = Population de lièvres (en kilo)') 
    plt.show() 
affichage_Lotka_Volterra_TL(4,10,0,50,0.0005) 



def affichage_Lotka_Volterra_TY(l_0,y_0,tmin,tmax,h): 
    '''Population de lynx en fonction du temps''' 
    T,L,Y=Lotka_Volterra(l_0,y_0,tmin,tmax,h) 
    plt.plot(T,Y) 
    plt.title('Population de lynx au cours du temps\nConditions initiales : 4 kilolièvres pour 10 lynx sur une durée de 50 ans') 
    plt.xlabel('T = Temps d-étude (en année)') 
    plt.ylabel('Y = Population de lynx (en unité)') 
    plt.show() 


def affichage_Lotka_Volterra_YL(l_0,y_0,tmin,tmax,h): 
    '''Population de lynx en fonction de la population de lièvres''' 
    T,L,Y=Lotka_Volterra(l_0-2,y_0-2,tmin,tmax,h) 
    plt.plot(L,Y, label="L0=2 et Y0=8") 
    T,L,Y=Lotka_Volterra(l_0,y_0,tmin,tmax,h) 
    plt.plot(L,Y, label="L0=4 et Y0=10") 
    T,L,Y=Lotka_Volterra(l_0+2,y_0+2,tmin,tmax,h) 
    plt.plot(L,Y, label="L0=6 et Y0=12") 
    T,L,Y=Lotka_Volterra(l_0+10,y_0+10,tmin,tmax,h) 
    plt.plot(L,Y, label="L0=14 et Y0=20") 
    T,L,Y=Lotka_Volterra(l_0+50,y_0+50,tmin,tmax,h) 
    plt.plot(L,Y, label="L0=54 et Y0=60") 
    T,L,Y=Lotka_Volterra(l_0+20,y_0+100,tmin,tmax,h) 
    plt.plot(L,Y, label="L0=24 et Y0=110") 
    T,L,Y=Lotka_Volterra(l_0+60,y_0+100,tmin,tmax,h) 
    plt.plot(L,Y, label="L0=64 et Y0=110") 
    T,L,Y=Lotka_Volterra(l_0+100,y_0+100,tmin,tmax,h) 
    plt.plot(L,Y, label="L0=104 et Y0=108") 
    plt.title('Population de lynx en fonction de la population de lièvres\nConditions initiales : L0 kilolièvres pour Y0 lynx sur une durée de 50 ans et des relevés tous les 0.0005 ans') 
    plt.xlabel('L = Population de lièvres (en kilo)') 
    plt.ylabel('Y = Population de lynx (en unité)') 
    plt.legend(loc="upper right")#localisation de l'affichage de la légende sur le graphique 
    plt.show() 
affichage_Lotka_Volterra_YL(4,10,0,50,0.0005) 
+1

は明らか 'Lotka_Volterra'は'複数の名前に割り当てる繰り返し処理することができないNONE'が、戻っています。何故デバッグをしたのですか? – jonrsharpe

+1

[タプルを返す関数は、TypeErrorを返す可能性があります。 'NoneType'オブジェクトは反復不可能です。](https://stackoverflow.com/questions/17974383/function-that-returns-a-tuple-gives-typeerror-nonetype-オブジェクトが非反復可能) – jonrsharpe

答えて

2

できました。

return # Implicitly returns None 

# Then you have a pure expression 
# in an impure context after the return 
liste_t,liste_l,liste_y 

次の行にこれら3カンマ区切り値はreturnの一部ではありません。あなたはそのためNoneを戻ってきている

return liste_t,liste_l,liste_y 
1

変更これらの行は、あなたがNoneを取得します。

return 

return None 

と同等であり、あなたのreturn文の後のコードは、単に到達不能コードです。

あなたはおそらくしたい:

return liste_t,liste_l,liste_y 
+0

OOOOOOOpsレッスン1をプログラミングしています。ありがとうございました – Lucos

+0

あなたに満足する答えを受け取った場合は、受け入れ済みとマークしてください。ありがとうございました、 –

関連する問題