2017-12-21 9 views
0

温度(te)と時間(t)のプロットをいくつか作成しようとしています。それぞれのプロットで異なるタイムステップ(dt)温度。 original plot。最初は私のコードはうまく走り、私はプロットを作成しました(プロットはいくつかのプロットで構成され、ズームインするとより目立ちます)。しかし、dt_valuesの値を調整した後、コードを実行することができず、「xとyには同じ第1次元が必要です」というエラーメッセージが表示されますが、その理由はわかりません。xとyの配列のためにプロットすることができません

import numpy as np 
import matplotlib.pyplot as plt 


r = 0.024 #cooling rate per minute 
te_init = 90.0 

te_surr = 17 

dt_values = [0.05, 0.025, 0.01, 0.005, 0.001] 

for j in dt_values: 
    t = np.arange(0,100,j) 
    te = np.zeros(len(t)) 
    te[0] = te_init 

    def f(te): 
     y = -r*(te - te_surr) # y is the derivative 
     return y 
    for i in range(1,len(t)): 
     te[i] = te[i-1] + f(te[i-1])*j 
     plt.plot(t, te[i], label = j) 
     plt.xlabel('Time, [min]') 
     plt.ylabel('Temperature, [C]') 
     plt.title('Adaptive Time Steps') 
     plt.legend(loc=4) 
+1

'TE [i]が'単一数でありながら、 'T'は、長さ 'j'を有しています。 'j!= 1'(ほとんどの場合)では、' j'値のリストを単一の数字に対してプロットするのは意味がありません。これがエラーの内容です。 – ImportanceOfBeingErnest

+0

問題は、私がverus teをプロットしようとすると、「応答しない」ということになります。だから私はおそらく私はtとte [i]をプロットする必要があると思った – Louise

+0

あなたの質問の問題は少しです、それはあなたが実際に達成したいことは明確ではない。 – ImportanceOfBeingErnest

答えて

1

ループの外側にプロットする必要があります。ループの外側にプロットする必要があります。その後、plt.plot(t, te)が正常に動作しています。

import numpy as np 
import matplotlib.pyplot as plt 


r = 0.024 #cooling rate per minute 
te_init = 90.0 
te_surr = 17 

def f(te): 
    return -r*(te - te_surr) # the derivative 

dt_values = [20, 5, 1, 0.1, 0.05] 

for dt in dt_values: 
    t = np.arange(0,100,dt) 
    te = np.zeros(len(t)) 
    te[0] = te_init 

    for i in range(1,len(t)): 
     te[i] = te[i-1] + f(te[i-1])*dt 

    plt.plot(t, te, label = dt) 

plt.xlabel('Time, [min]') 
plt.ylabel('Temperature, [C]') 
plt.title('Adaptive Time Steps') 
plt.legend(loc=4, title="Timestep [min]") 

plt.show() 

enter image description here

関連する問題