简体   繁体   中英

How does it work linear interpolation? It fails to predict the right value between points

My code does not work when I apply a linear interpolation between points.

Code

y= [0.1 ,0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.71]

x= [44.72, 43.4, 41.5, 39.9, 37.73, 36.1, 33.6, 31.5, 29.7, 26.4, 21.6, 16.8, 3.6, 0] 

x_new = 25

y_new = np.interp(x_new, x, y)
print(y_new)
plt.plot(x, y, "og-", x_new, y_new, "or");

Result

红点应该在绿线上。

Expected result在此处输入图像描述

Can someone help me?

The docs say that the x values must be monotonically increasing. Yours don't.

The x-coordinate sequence is expected to be increasing, but this is not explicitly enforced. However, if the sequence xp is non-increasing, interpolation results are meaningless.

Note that, since NaN is unsortable, xp also cannot contain NaNs.

A simple check for xp being strictly increasing is:

np.all(np.diff(xp) > 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM