简体   繁体   中英

Linear interpolation of given data set

I need to interpolate and find the values of a bunch of numbers x=np.linspace(0,1.1,23) y=[1.00,0,0.90,0,0.82,0,0.74,0,0.67,0,0.61,0,0.55,0,0.50,0,0.44,0,0.41,0,0.37,0,0.33] So the values of points x=0,1.1,1.2 etc are known and for points 0.05,0.15,0.25 etc are unknown and i have put them in the list as 0.

Here is my further code:

i=1
for i in t1:
   m=(x[i+1]-x[i-1])/(t1[i+1]-t1[i-1])
   y=x[i-1]+m*(t1[i]-t1[i-1])
   x1[i]=y
   i=i+2
print(x1) 

But i am getting this error: TypeError: list indices must be integers or slices, not numpy.float64

How do i fix this?

Try this:

import numpy as np

x = np.linspace(0, 1.1, 23)
y = [1.00, 0, 0.90, 0, 0.82, 0, 0.74, 0, 0.67, 0, 0.61,
     0, 0.55, 0, 0.50, 0, 0.44, 0, 0.41, 0, 0.37, 0, 0.33]

for i in range(1, len(y), 2):
    m = (y[i + 1] - y[i - 1]) / (x[i + 1] - x[i - 1])
    b = y[i + 1] - (m * x[i + 1])
    z = m * x[i] + b
    y[i] = round(z, 3)

print(y)

Output:

[1.0, 0.95, 0.9, 0.86, 0.82, 0.78, 0.74, 0.705, 0.67, 0.64, 0.61, 0.58, 0.55, 0.525, 0.5, 0.47, 0.44, 0.425, 0.41, 0.39, 0.37, 0.35, 0.33]

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