三次样条插值(Python实现)⽬录
1、原理
2、案例
3、代码
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import interpolate
import matplotlib as mpl
# Import data
file = pd.read_excel('Three moment method.xlsx',header=None,names=['x', 'value'])
data = pd.DataFrame(file)
# 数组切⽚
x = data['x']  # Take the first column of data
y = data['value']  # Take the second column of data
# Spline interpolation of correlation functions in SciPy Library
tck = interpolate.splrep(x, y)    #(t,c,k)包含节点向量、B样条曲线系数和样条曲线阶数的元组。xx = np.linspace(min(x), max(x), 200)
yy = interpolate.splev(xx, tck, der=0)
x1,x2=-0.02,2.56
y1=interpolate.splev(x1,tck,der=0)
y2=interpolate.splev(x2,tck,der=0)
print('When x = -0.02, the value of Y is:',y1)
print('When x = 2.56, the value of Y is:',y2)
print(yy)
plt.plot(x, y,'ro', xx, yy ,'b')
plt.legend(['true', 'cubic spline'])
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Three moment method of cubic spline interpolation')
# Save picture
plt.savefig('out2.png', dpi=600)
# Set the resolution at which you want to save the picture
plt.show()
[注] ;(插值)
4、结果
When x = -0.02, the value of Y is: -2.7618446933498895
When x = 2.56, the value of Y is: 4.73015581306341
[-2.46000000e+00 -2.78641445e+00 -3.08977770e+00 -3.37066746e+00 -3.62966147e+00 -3.86733746e+00 -4.08427316e+00 -4.28104630e+00 -4.45823461e+00 -4.61641582e+00 -4.7561
6766e+00 -4.87806786e+00 -4.98269414e+00 -5.07062425e+00 -5.14243590e+00 -5.19870684e+00 -5.24001478e+00 -5.26693746e+00 -5.28005262e+00 -5.27993797e+00 -5.26717125e+00 -5.24233019e+00 -5.20599252e+00 -5.15873597e+00 -5.10113827e+00 -5.03377715e+00 -4.95723034e+00 -4.87207557e+00 -4.77889057e+00 -4.67825307e+00 -4.57074080e+00 -4.45693149e+00 -4.33740287e+00 -4.21273266e+00 -4.08349861e+00 -3.95027844e+00 -3.81364988e+00 -3.67419066e+00 -3.53247850e+00 -3.38909115e+00 -3.24460633e+00 -3.09960177e+00 -2.95465520e+00 -2.81034434e+00 -2.66724694e+00 -2.52594072e+00 -2.38700341e+00 -2.25101274e+00 -2.11854644e+00 -1.99018224e+00 -1.86649786e+00 -1.74794500e+00 -1.63451294e+00 -1.52608502e+00 -1.42254454e+00 -1.32377483e+00 -1.22965918e+00 -1.14008093e+00 -1.05492338e+00 -9.74069856e-01 -8.97403663e-01 -8.24808121e-01 -7.56166544e-01 -6.91362247e-01 -6.30278546e-01 -5.72798756e-01 -5.18806191e-01 -4.68184167e-01 -4.20815999e-01 -3.76585002e-01 -3.35374491e-01 -2.97067781e-01 -2.61548188e-01 -2.28699025e-01 -1.98403609e-01 -1.70545255e-01 -1.45007277e-01 -1.21672990e-01 -1.00425711e-01 -8.11487529e-02 -6.37254319e-02 -4.80390629e-02 -3.39729610e-02 -2.14104413e-02 -1.02348187e-02 -3.29408529e-04  8.42247428e-03  1.61375146e-02  2.29323974e-02  2.89238074e-02  3.42284298e-02  3.89629493e-02
4.32440509e-02  4.71884195e-02
5.09130460e-02  5.45723558e-02
5.83936999e-02
6.26128392e-02  6.74655344e-02
7.31875464e-02
8.00146360e-02  8.81825640e-02
9.79270913e-02  1.09483979e-01
1.23088987e-01  1.38977877e-01  1.57386409e-01  1.78550345e-01
2.02705445e-01  2.30087470e-01  2.60932180e-01  2.95475338e-01
3.33952703e-01  3.76600036e-01
4.23653098e-01  4.75347650e-01
5.31919452e-01  5.93604267e-01
6.60637853e-01
7.33255973e-01
8.11694387e-01  8.96188856e-01
9.86975140e-01  1.08428900e+00  1.18836620e+00  1.29944250e+00  1.41775365e+00  1.54353542e+00
1.67701955e+00  1.81755953e+00  1.96253713e+00
2.10906542e+00
2.25425749e+00  2.39522641e+00  2.52908525e+00  2.65294709e+00
2.76416075e+00  2.86268703e+00  2.95011282e+00
3.02804956e+00
3.09810872e+00  3.16190174e+00  3.22104008e+00  3.27713520e+00  3.33179854e+00  3.38664156e+00  3.44327571e+00  3.50331246e+00  3.56836324e+00  3.64003951e+00  3.71995274e+00  3.80971437e+00
3.91093585e+00
4.02522864e+00  4.15420419e+00  4.29947396e+00
4.46264940e+00  4.64529207e+00  4.84751920e+00
5.06781661e+00
5.30458260e+00  5.55621550e+00  5.82111361e+00
6.09767525e+00
6.38429875e+00  6.67938240e+00  6.98132454e+00
7.28852348e+00
7.59937753e+00  7.91228501e+00  8.22564423e+00  8.53785351e+00
8.84731116e+00  9.15241551e+00  9.45156487e+00  9.74315755e+00  1.00255919e+01  1.02972661e+01  1.05565787e+01  1.08019278e+01  1.10317118e+01  1.12443291e+01  1.14381779e+01  1.16116565e+01  1.17631633e+01  1.18910966e+01  1.19938547e+01  1.20698359e+01  1.21174385e+01  1.21350609e+01  1.21211013e+01  1.20739581e+01  1.19920296e+01  1.18737141e+01  1.17174099e+01  1.15215154e+01  1.12844288e+01  1.10045485e+01  1.06802728e+01  1.03100000e+01]linspace numpy

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。