How to perform cubic spline interpolation in python?

前端 未结 4 928
名媛妹妹
名媛妹妹 2021-01-30 11:44

I have two lists to describe the function y(x):

x = [0,1,2,3,4,5]
y = [12,14,22,39,58,77]

I would like to perform cubic spline interpolation so

4条回答
  •  我在风中等你
    2021-01-30 12:13

    Minimal python3 code:

    from scipy import interpolate
    
    if __name__ == '__main__':
        x = [ 0, 1, 2, 3, 4, 5]
        y = [12,14,22,39,58,77]
    
        # tck : tuple (t,c,k) a tuple containing the vector of knots,
        # the B-spline coefficients, and the degree of the spline.
        tck = interpolate.splrep(x, y)
    
        print(interpolate.splev(1.25, tck)) # Prints 15.203125000000002
        print(interpolate.splev(...other_value_here..., tck))
    

    Based on comment of cwhy and answer by youngmit

提交回复
热议问题