简体   繁体   中英

Python numpy interpolation gives wrong output

I have an input and output array. I have given below the plot. I want to interpolate value at x=0. I was expecting something like around 16.7 but instead giving 17.4881, peak value. What could be wrong.

Data :

在此处输入图片说明

My code:

xdata = [0.101,-0.008,-0.111,-0.209,-0.303]
ydata = [16.5241,16.7987,17.0499,17.2793,17.4885]
xp = np.interp(0,xdata,ydata)
print(xp)

Present output:

  17.4885

Expected output:

16.7 # around from plot

If you look at interp function documentation , it says that

The x-coordinates of the data points, must be increasing if argument period is not specified. Otherwise, xp is internally sorted after normalizing the periodic boundaries with xp = xp % period.

But your xdata is in descending order, so you need to reverse order in xdata and ydata

import numpy as np
xdata = [0.101,-0.008,-0.111,-0.209,-0.303][::-1]
ydata = [16.5241,16.7987,17.0499,17.2793,17.4885][::-1]
xp = np.interp(0,xdata,ydata)
print(xp)
# 16.778545871559633

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