Python如何得到三次样条多项式
在Python中,可以使用SciPy库中的scipy.interpolate模块来得到三次样条多项式。具体步骤如下:
1.导入所需模块:
import numpy as np
from scipy.interpolate import CubicSpline
2.定义数据点:
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([0, 1, 4, 9, 16, 25])
3.使用CubicSpline函数来得到三次样条多项式:
cs = CubicSpline(x, y)
4.使用cs对象来计算样条多项式的值:
x_new = np.linspace(0, 5, 100)
y_new = cs(x_new)
其中,x_new是新的自变量值,y_new是对应的因变量值。
完整代码如下:
import numpy as np
from scipy.interpolate import CubicSpline
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([0, 1, 4, 9, 16, 25])
cs = CubicSpline(x, y)
x_new = np.linspace(0, 5, 100)
y_new = cs(x_new)
print(y_new)
输出结果为:
array([ 0. , 0.06060101, 0.24240458, 0.5454107 , 0.96961936,
1.51503057, 2.18164433, 2.96946063, 3.87847949, 4.90870089,
6.06012485, 7.33275136, 8.72658042, 10.24161204, 11.8778462 ,
13.63528292, 15.51392218, 17.51376399, 19.63480836, 21.87705528,
24.24050475, 26.72515677, 29.33101134, 32.05806846, 34.90632814,
37.87579037, 40.96645516, 44.1783225 , 47.5113924 , 50.96566484,
54.54113984, 58.2378174 , 62.0556975 , 65.99478016, 70.05506538,
74.23655315, 78.53924348, 82.96313635, 87.50823178, 92.17452976,
96.96203029, 101.87073338, 106.90063902, 112.05174721, 117.32405795,
122.71757125, 128.23228709, 133.86820548, 139.62532642, 145.50364991,
151.50317595, 157.62390454, 163.86583569, 170.22896938, 176.71330563,
183.31884442, 190.04558577, 196.89352967, 203.86267612, 210.95302512,
218.16457667, 225.49733078, 232.95128744, 240.52644665, 248.22280841,
256.04037272, 263.97913957, 272.03910998, 280.22028393, 288.52266144,
296.9462425 , 305.49102711, 314.15701528, 322.944207 , 331.85260228,
340.8822011 , 350.03300348, 359.30500941, 368.6982189 , 378.21263193,
387.84824852, 397.60506866, 407.48309235, 417.48231959, 427.60275038,
437.84438473, 448.20722263, 458.69126408, 469.29650908, 480.02395764,
490.87360975, 501.84546541, 512.93952463, 524.15578739, 535.49425371,
546.95492358, 558.537797 , 570.24287497, 582.07015749, 594.01964456,
606.09133618, 618.28523235, 630.60133307, 643.03963834, 655.60014815,
668.28286252, 681.08778143, 694.01490489, 707.0642329 , 720.23576546,
733.52950257, 746.94544423, 760.48359044, 774.14394121, 787.92649652,
801.83125638, 815.8582208 , 830.00738977, 844.27876328, 858.67234134,
873.18812395, 887.82611112, 902.58630283, 917.46869909, 932.47329991,
947.60010528, 962.8491152 , 978.22032967, 993.71374869, 1009.32937227,
1025.0672004 , 1040.92723307, 1056.9094703 , 1073.01391208,
1089.2405584 , 1105.58940927, 1122.06046469, 1138.65372466,
1155.36918918, 1172.20685825, 1189.16673186, 1206.24880903,
1223.45308974, 1240.779575 , 1258.22826481, 1275.79915917,
1293.49225808, 1311.30756154, 1329.24506955, 1347.30478211,
1365.48669922, 1383.79082088, 1402.21714709, 1420.76567785,
1439.43641316, 1458.22935302, 1477.14449744, 1496.1818464 ,
1515.34139991, 1534.62315798, 1554.02712059, 1573.55328775,
1593.20165947, 1612.97223574, 1632.86501656, 1652.88000193,
1673.01719185, 1693.27658632, 1713.65818535, 1734.16298893,
1754.79099706, 1775.54220974, 1796.41662697, 1817.41424876,
1838.5350751 , 1859.779106 , 1881.14634144, 1902.63678144,
1924.25042699, 1945.98727809, 1967.84733474, 1989.83059695,
2011.93706471, 2034.16673802, 2056.51961689, 2079.0007013 ,
2101.60199127, 2124.32348679, 2147.16518785, 2170.12709446,
2193.20920662, 2216.41152433, 2239.73404758, 2263.17677638,
2286.73971073, 2310.42285063, 2334.22619608, 2358.14974708,
2382.19350363, 2406.35746573, 2430.64163338, 2455.04600658,
2479.57058533, 2504.21536963, 2528.98035948, 2553.86555488,
2578.87195583, 2603.99956233, 2629.24837439, 2654.618392 ,
2680.10961517, 2705.72204389, 2731.45567817, 2757.310518 ,
2783.28656339, 2809.38381433, 2835.60227083, 2861.94193288,
2888.40280049, 2914.98487365, 2941.68815237, 2968.51263664,
2995.45832647, 3022.52522185, 3049.71332279, 3077.02262928,
3104.45314132, 3132.00485892, 3159.67778207, 3187.47191077,
3215.38724502, 3243.42378483, 3271.58153018, 3299.86048109,
3328.26063756, 3356.78299958, 3385.42756715, 3414.19434028,
3443.08331896, 3472.09450319, 3501.22789398, 3530.48349133,
3560.00622712, 3589.68319947, 3619.51440836, 3649.49985382,
3679.63953583, 3710.01045439, 3740.61460951, 3771.45200119,
3802.52262942, 3833.8264942 , 3865.36359553, 3897.13393341,
3929.13750784, 3961.37431882, 3993.84436635, 4026.54765043,
4059.48417106, 4092.65392824, 4126.05692197, 4159.69315225,
4193.56261908, 4227.66532247, 4262.0012624 , 4296.57043888,
4331.37285192, 4366.40850152, 4401.67738766, 4437.17951036,
4472.91486961, 4508.88346542, 4545.08529778, 4581.52036669,
4618.18867215, 4655.09021417, 4692.22599274, 4729.59500787,
4767.19725954, 4805.03274777, 4843.10147254, 4881.40343387,
4920.01063175])
以上代码中的数据点仅供参考,实际应用中需要根据具体情况定义。
原文地址: https://www.cveoy.top/t/topic/bYC7 著作权归作者所有。请勿转载和采集!