Arrays 索引器:索引10超出大小为10的轴0的界限

Arrays 索引器:索引10超出大小为10的轴0的界限,arrays,python-2.7,python-3.x,numpy,differential-equations,Arrays,Python 2.7,Python 3.x,Numpy,Differential Equations,我正在为x轴网和x矢量以及时间轴网数值设置网格,但我再次为x(位置)设置了一个数组,该数组应仅在0和20之间,并且t(时间)将从0到1000,以便解算热方程。但每次我想要,例如,我将步骤数设为10,我都会得到一个错误: "Traceback (most recent call last): File "/home/universe/Desktop/Python/Heat_1.py", line 33, in <module> x[i] = a + i*h IndexError: in

我正在为x轴网和x矢量以及时间轴网数值设置网格,但我再次为
x
(位置)设置了一个数组,该数组应仅在0和20之间,并且
t
(时间)将从0到1000,以便解算热方程。但每次我想要,例如,我将步骤数设为10,我都会得到一个错误:

"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"
“回溯(最近一次呼叫最后一次):
文件“/home/universe/Desktop/Python/Heat_1.py”,第33行,在
x[i]=a+i*h
索引器:索引10超出大小为10”的轴0的界限
这是我的密码:

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)
从数学导入sin,pi
进口numpy
将numpy作为np导入
#常量变量

N=int(输入(“尝试索引范围外的x(中的间隔数:

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) 
因此,您只能访问
t
中的
M
元素

Python总是以零开始索引。因此:

for s in range(M):
将执行
M
循环,同时:

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

将执行
M+1
循环。

数组是零索引的,即0-9Ya,它现在确实在工作,但x的值应该被等距分割,从:a=0.0到b=1.0,它似乎没有给我想要的值,因为它给出的值超过了1次,因为我现在让它正常工作并运行@MikeMullerGreat说这很有帮助。顺便说一句,如果它解决了你的问题,你可以给出一个答案。