2016-04-07 14 views
0

xグリッドとxベクトルと時間グリッドのメッシュグリッドを数値的に設定していますが、もう一度x(position )は0から20の間でなければならず、したがってt(時間)は0から1000までであるため、Heat方程式を解くことができます。しかし、私は、例えばのためにするたびに、私はステップ10の数を作る、私はエラーを取得する:ここでIndexError:インデックス10は、軸10のサイズが0の場合です。

"Traceback (most recent call last): 
File "/home/universe/Desktop/Python/Heat_1.py", line 33, in <module> 
x[i] = a + i*h 
IndexError: index 10 is out of bounds for axis 0 with size 10" 

は私のコードです:

from math import sin,pi 
import numpy 
import numpy as np 

#Constant variables 
N = int(input("Number of intervals in x (<=20):")) 
M = int(input("Number of time steps (<=1000):")) 

#Some initialised varibles 
a = 0.0 
b = 1.0 
t_min = 0.0 
t_max = 0.5 

# Array Variables 
x = np.linspace(a,b, M) 
t = np.linspace(t_min, t_max, M) 


#Some scalar variables 
n = []       # the number of x-steps 
i, s = [], []     # The position and time 

# Get the number of x-steps to use 
for n in range(0,N): 
    if n > 0 or n <= N: 
     continue 

# Get the number of time steps to use 
for m in range(0,M): 
    if m > 0 or n <= M: 
     continue 

# Set up x-grid and x-vector 
h =(b-a)/n 
for i in range(0,N+1): 
    x[i] = a + i*h 

# Set up time-grid 
k = (t_max - t_min)/m 
for s in range(0, M+1): 
    t[s] = t_min + k*s 

print(x,t) 
+0

配列は0から9までインデックスされています。 –

答えて

0

あなたが範囲外のインデックスを試してみてください。

for s in range(0, M+1): 
    t[s] = t_min + k*s 

変更するには:

for s in range(M): 
    t[s] = t_min + k*s 

それは動作します。

あなたはMの長さとtを作成します。

t = np.linspace(t_min, t_max, M) 

をですから、tM要素にのみアクセスすることができます。

Pythonは常にゼロでインデックスを開始します。したがって:

for s in range(M): 

ながら、Mループを行います。

for s in range(0, M+1): 

M+1ループを行います。

+0

これは実際には機能していますが、xの値はa = 0.0からb = 1.0まで等間隔に分割されているはずです。私に値が1以上の値を与えるので、私に必要な値を与えているようです。 – Tonikami04

+0

ありがとうございました。私は今、正しく動作しています。@MikeMuller – Tonikami04

+0

それは助けになりました。あなたの問題が解決されたら、回答を受け入れることができます(http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)。 –

関連する問題