这会产生语法错误:
import numpy as np
import scipy.interpolate
import matplotlib.pyplot as plt
y = np.array([-0.23122875, -0.11375591, -0.09760733, -0.07401004, -0.03565704,-0.02449268,-0.01411058,0.0018971,-0.0145346,0.00156783,0.0048691,0.01990767,0.02048657,0.01691803,0.02228818,0.02578349,0.02707902,0.01996198])
x = np.array(range(len(y)))
stepsize = 0.0001
xvals = np.arange(0,13.0001,stepsize)
func = scipy.interpolate.interp1d(x, y, kind = "cubic")
yvals = func(xvals)
plt.plot(xvals,yvals)
plt.plot(x,y, "o")
dif = np.diff(yvals)
for i in range(len(y))
if i == 0:
print dif[int(i*1/stepsize)]
else:
print dif[int(i*1/stepsize)-1]
它在第5行中给出了语法错误,如此
y = np.array([ - 0.23122875,-0.11375591,-0.09760733,-0.07401004,-0.03565704,-0.02449268,-0.01411058,0.0018971,-0.0145346,0.00156783,0.0048691,0.01990767,0.02048657,0.01691803,0.02228818,0.02578349, 0.02707902,0.01996198]) ^ SyntaxError:语法无效
我只是不知道为什么。当我更改y中的某些值时,它会突然发挥作用。
提前致谢
答案 0 :(得分:0)
您似乎在此行中使用了soft hyphen:
y = np.array([-0.23122875, -0.11375591, -0.09760733, -0.07401004, -0.03565704,-0.02449268,-0.01411058,0.0018971,-0.0145346,0.00156783,0.0048691,0.01990767,0.02048657,0.01691803,0.02228818,0.02578349,0.02707902,0.01996198])
您需要将其切换为hyphen-minus,如下所示:
y = np.array([-0.23122875, -0.11375591, -0.09760733, -0.07401004, -0.03565704,-0.02449268,-0.01411058,0.0018971,-0.0145346,0.00156783,0.0048691,0.01990767,0.02048657,0.01691803,0.02228818,0.02578349,0.02707902,0.01996198])