执行以下代码时,我得到IndexError: list assignment index out of range
:
def tcheb (a,b,n):
V=[]
for j in range(1,n) :
for k in range (1,n):
V[k]=((a+b)/2) + (((a-b)/2) * (np.cos((k-1)/(n-1)*np.pi)))
return V.append(k)
tcheb(-5,5,2)
答案 0 :(得分:0)
a = [0] * 10是初始化大小清单的方式
def tcheb (a,b,n):
V=[0] * 10
for j in range(1,n) :
for k in range (1,n):
V[k]=((a+b)/2) + (((a-b)/2) * (np.cos((k-1)/(n-1)*np.pi)))
return V.append(k)
tcheb(-5,5,2)