Spline Interpolation with Python

前端 未结 3 1988
醉酒成梦
醉酒成梦 2020-12-15 08:37

I wrote the following code to perform a spline interpolation:

import numpy as np
import scipy as sp

x1 = [1., 0.88,  0.67,  0.50,  0.35,  0.27, 0.18,  0.11,         


        
3条回答
  •  一整个雨季
    2020-12-15 08:53

    You can get this in the following way:

    import numpy as np
    import scipy as sp
    from scipy.interpolate import interp1d
    
    x1 = [1., 0.88,  0.67,  0.50,  0.35,  0.27, 0.18,  0.11,  0.08,  0.04,  0.04,  0.02]
    y1 = [0., 13.99, 27.99, 41.98, 55.98, 69.97, 83.97, 97.97, 111.96, 125.96, 139.95, 153.95]
    
    # Combine lists into list of tuples
    points = zip(x1, y1)
    
    # Sort list of tuples by x-value
    points = sorted(points, key=lambda point: point[0])
    
    # Split list of tuples into two list of x values any y values
    x1, y1 = zip(*points)
    
    new_length = 25
    new_x = np.linspace(min(x1), max(x1), new_length)
    new_y = sp.interpolate.interp1d(x1, y1, kind='cubic')(new_x)
    

提交回复
热议问题