gpt4 book ai didi

python-3.x - 我应该如何使用单变量样条曲线来拟合特定形状的数据?

转载 作者:行者123 更新时间:2023-12-01 12:06:32 25 4
gpt4 key购买 nike

enter code here我正在尝试将样条拟合到两组数据点,以便沿公共(public) x 线性空间对它们进行归一化和比较。数据点代表一个辐射剖面,形状看起来像一座塔。

我不能在平滑度参数设置为零的情况下使用 UnivariateSpline,因为我正在寻找曲线中间部分的平滑线,而不是连接点,从而导致一条嘈杂的线。

如果我使用高平滑度参数,我会得到一个抛物线拟合,它不会捕获轮廓边缘的急剧上升和下降(这很重要)。

如果我让函数优化平滑度本身,我会在轮廓上升和下降前后得到立方线,这也不是最优的。

这种工作的一种方法是解决问题。我根据 np.dif(y) 最大值将配置文件 y 数据分成 3 组,以发现急剧上升和下降。然后我用零平滑度拟合第一组包括上升和最后一组包括下降并且用平滑线拟合中间组。这行得通,但它会在集合连接处造成丑陋的不连续性。

有什么方法可以将连续的平滑线拟合到整个数据?也许使用样条曲线或其他东西的加权? (我对权重不熟悉)

下面是一些示例数据集的代码:

#!/usr/bin/env python
# coding: utf-8

# In[4]:


import numpy as np
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
from scipy.interpolate import UnivariateSpline, InterpolatedUnivariateSpline


# In[11]:


#Example 1
x_1 = [-210.0, -208.0, -206.0, -204.0, -202.0, -200.0, -198.0, -196.0, -194.0, -192.0, -190.0, -188.0, -186.0, -184.0, -182.0, -180.0, -178.0, -176.0, -174.0, -172.0, -170.0, -168.0, -166.0, -164.0, -162.0, -160.0, -158.0, -156.0, -154.0, -152.0, -150.0, -148.0, -146.0, -144.0, -142.0, -140.0, -138.0, -136.0, -134.0, -132.0, -130.0, -128.0, -126.0, -124.0, -122.0, -120.0, -118.0, -116.0, -114.0, -112.0, -110.0, -108.0, -106.0, -104.0, -102.0, -100.0, -98.0, -96.0, -94.0, -92.0, -90.0, -88.0, -86.0, -84.0, -82.0, -80.0, -78.0, -76.0, -74.0, -72.0, -70.0, -68.0, -66.0, -64.0, -62.0, -60.0, -58.0, -56.0, -54.0, -52.0, -50.0, -48.0, -46.0, -44.0, -42.0, -40.0, -38.0, -36.0, -34.0, -32.0, -30.0, -28.0, -26.0, -24.0, -22.0, -20.0, -18.0, -16.0, -14.0, -12.0, -10.0, -8.0, -6.0, -4.0, -2.0, 0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0, 20.0, 22.0, 24.0, 26.0, 28.0, 30.0, 32.0, 34.0, 36.0, 38.0, 40.0, 42.0, 44.0, 46.0, 48.0, 50.0, 52.0, 54.0, 56.0, 58.0, 60.0, 62.0, 64.0, 66.0, 68.0, 70.0, 72.0, 74.0, 76.0, 78.0, 80.0, 82.0, 84.0, 86.0, 88.0, 90.0, 92.0, 94.0, 96.0, 98.0, 100.0, 102.0, 104.0, 106.0, 108.0, 110.0, 112.0, 114.0, 116.0, 118.0, 120.0, 122.0, 124.0, 126.0, 128.0, 130.0, 132.0, 134.0, 136.0, 138.0, 140.0, 142.0, 144.0, 146.0, 148.0, 150.0, 152.0, 154.0, 156.0, 158.0, 160.0, 162.0, 164.0, 166.0, 168.0, 170.0, 172.0, 174.0, 176.0, 178.0, 180.0, 182.0, 184.0, 186.0, 188.0, 190.0, 192.0, 194.0, 196.0, 198.0, 200.0, 202.0, 204.0, 206.0, 208.0, 210.0]

y_1 = [1.4295226140525178e-21, 1.6427103012883458e-21, 1.821549871710417e-21, 1.8600922737090837e-21, 1.952935493151029e-21, 2.0808512562831945e-21, 2.088480067210673e-21, 2.3252524971469115e-21, 2.3604087098101423e-21, 2.5777736716038026e-21, 2.6388544259730252e-21, 2.743766230009555e-21, 3.021176501652941e-21, 3.255022357012754e-21, 3.1825874279608568e-21, 3.3844734840462558e-21, 3.613537349804512e-21, 3.689479855589563e-21, 3.979344292112167e-21, 4.011284931665291e-21, 4.3331493445964974e-21, 4.42087387693927e-21, 4.61983100901474e-21, 4.903784307266063e-21, 5.296673588468055e-21, 5.268635414465153e-21, 5.534134900475608e-21, 5.861336704296021e-21, 6.211240190204385e-21, 6.413337671786028e-21, 6.736218974358413e-21, 6.933955303835514e-21, 7.245392516087334e-21, 7.616309507922374e-21, 8.172488917215604e-21, 8.169783959684506e-21, 8.880599809963712e-21, 9.199841185305185e-21, 9.707103772522977e-21, 1.0364862022889146e-20, 1.0811609909255555e-20, 1.1583389003976744e-20, 1.195989228313853e-20, 1.30126856493689e-20, 1.3376091484825573e-20, 1.4115440812380502e-20, 1.539229091776661e-20, 1.6059712561525453e-20, 1.6895967848631177e-20, 1.795689663143609e-20, 1.9536970071975345e-20, 2.0583008110924713e-20, 2.235540235635208e-20, 2.5947903879402268e-20, 6.180552377805634e-20, 8.355762167331923e-20, 8.496095568028067e-20, 8.765163907836433e-20, 8.771611279381496e-20, 8.996823318793393e-20, 9.101139069844862e-20, 9.206888878208697e-20, 9.272943402020724e-20, 9.435389469921155e-20, 9.500337591066436e-20, 9.452973019761442e-20, 9.648633430929047e-20, 9.700995580651313e-20, 9.911105200349805e-20, 9.842938696984599e-20, 9.786377648899059e-20, 9.901480010020134e-20, 1.0011040566633435e-19, 9.872422602941769e-20, 1.0048662803205042e-19, 1.0114903825570249e-19, 1.0059657242599848e-19, 1.0132771202896158e-19, 1.0154902286832572e-19, 1.0068825161789439e-19, 1.0146051065498602e-19, 1.0141934925868303e-19, 1.0216895105279509e-19, 1.0303946568701418e-19, 1.0374124925209062e-19, 1.0227728143897267e-19, 1.0262217511348233e-19, 1.0276500601751216e-19, 1.0221461450659016e-19, 1.0440281095195915e-19, 1.0573943858373224e-19, 1.0419443055088742e-19, 1.0403161868309251e-19, 1.0471634959640928e-19, 1.0457793287449229e-19, 1.0504213884925815e-19, 1.0415412360146518e-19, 1.0434049836638474e-19, 1.0391012318687714e-19, 1.0396020148265584e-19, 1.04951396652105e-19, 1.0344364925258479e-19, 1.0491096962922902e-19, 1.0415025435259349e-19, 1.046150383912583e-19, 1.0517031746260079e-19, 1.0432012386156581e-19, 1.0312319606274798e-19, 1.0456768903645697e-19, 1.0393270631189554e-19, 1.0412044375056791e-19, 1.0290060078672382e-19, 1.0261760590464094e-19, 1.0275738639552336e-19, 1.0404888376507175e-19, 1.0282264054940699e-19, 1.0311610076116371e-19, 1.0269367041009052e-19, 1.0223808065771127e-19, 1.0184789236933238e-19, 1.0097119873518135e-19, 1.0174979308736812e-19, 1.0251610302833564e-19, 1.000881431770874e-19, 1.009924373969923e-19, 1.011081374579119e-19, 9.992147913511413e-20, 1.0075329648516413e-19, 9.888206907990243e-20, 9.948660996811841e-20, 9.834779734967718e-20, 9.848154129024306e-20, 9.735079123737905e-20, 9.782276778837801e-20, 9.75924444064314e-20, 9.644272628509293e-20, 9.647677975528781e-20, 9.632505269173146e-20, 9.508677922926286e-20, 9.373387792792943e-20, 9.308928880664975e-20, 9.255456132058517e-20, 9.183996647052114e-20, 9.224537352857811e-20, 9.153095352090768e-20, 9.063345775506358e-20, 8.930568658721485e-20, 8.98206055956997e-20, 8.728312259974252e-20, 8.611547380813165e-20, 8.562279708334554e-20, 8.389801067327348e-20, 8.210582252046584e-20, 8.140069014074383e-20, 7.851960978690971e-20, 7.513436789426143e-20, 5.638352302680673e-20, 2.4859004101872068e-20, 2.1566799052306043e-20, 1.949059123294511e-20, 1.8582483735676543e-20, 1.7207284929588388e-20, 1.604838437714165e-20, 1.5023165841913053e-20, 1.4470032697073442e-20, 1.335443871139727e-20, 1.261864465821435e-20, 1.1696331799273179e-20, 1.12803378885813e-20, 1.0991595000466702e-20, 1.0065494691025012e-20, 9.715474570669913e-21, 9.356694627842102e-21, 9.034172189282595e-21, 8.50198334192219e-21, 8.241821794188562e-21, 7.777015748304419e-21, 7.424935256042654e-21, 7.055353404435796e-21, 6.5923927471651786e-21, 6.228610927711253e-21, 5.993195989560806e-21, 5.651661917630429e-21, 5.6913544921251365e-21, 5.205197713047647e-21, 5.191103961263008e-21, 4.957168426751453e-21, 4.846866967618264e-21, 4.466018769889993e-21, 4.3525640067408905e-21, 4.252492600315592e-21, 3.948750588250809e-21, 3.622136021452661e-21, 3.3592997229398316e-21, 3.294398935394057e-21, 3.2844230221832043e-21, 3.252908923193404e-21, 3.02291449964085e-21, 2.7542313899818323e-21, 2.7087762261072507e-21, 2.5415655501738826e-21, 2.414396761194755e-21, 2.303670400637897e-21, 2.0787727179258136e-21, 2.070699171984851e-21, 2.0715170116089346e-21, 1.8988826775374163e-21, 1.863615058015695e-21, 1.7050041100649195e-21, 1.5402602118223378e-21, 1.3249207166865905e-21]

plt.plot(x_1, y_1, 'r.')


# In[10]:


#Example 2
x_2 = [-210.0, -208.0, -206.0, -204.0, -202.0, -200.0, -198.0, -196.0, -194.0, -192.0, -190.0, -188.0, -186.0, -184.0, -182.0, -180.0, -178.0, -176.0, -174.0, -172.0, -170.0, -168.0, -166.0, -164.0, -162.0, -160.0, -158.0, -156.0, -154.0, -152.0, -150.0, -148.0, -146.0, -144.0, -142.0, -140.0, -138.0, -136.0, -134.0, -132.0, -130.0, -128.0, -126.0, -124.0, -122.0, -120.0, -118.0, -116.0, -114.0, -112.0, -110.0, -108.0, -106.0, -104.0, -102.0, -100.0, -98.0, -96.0, -94.0, -92.0, -90.0, -88.0, -86.0, -84.0, -82.0, -80.0, -78.0, -76.0, -74.0, -72.0, -70.0, -68.0, -66.0, -64.0, -62.0, -60.0, -58.0, -56.0, -54.0, -52.0, -50.0, -48.0, -46.0, -44.0, -42.0, -40.0, -38.0, -36.0, -34.0, -32.0, -30.0, -28.0, -26.0, -24.0, -22.0, -20.0, -18.0, -16.0, -14.0, -12.0, -10.0, -8.0, -6.0, -4.0, -2.0, 0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0, 20.0, 22.0, 24.0, 26.0, 28.0, 30.0, 32.0, 34.0, 36.0, 38.0, 40.0, 42.0, 44.0, 46.0, 48.0, 50.0, 52.0, 54.0, 56.0, 58.0, 60.0, 62.0, 64.0, 66.0, 68.0, 70.0, 72.0, 74.0, 76.0, 78.0, 80.0, 82.0, 84.0, 86.0, 88.0, 90.0, 92.0, 94.0, 96.0, 98.0, 100.0, 102.0, 104.0, 106.0, 108.0, 110.0, 112.0, 114.0, 116.0, 118.0, 120.0, 122.0, 124.0, 126.0, 128.0, 130.0, 132.0, 134.0, 136.0, 138.0, 140.0, 142.0, 144.0, 146.0, 148.0, 150.0, 152.0, 154.0, 156.0, 158.0, 160.0, 162.0, 164.0, 166.0, 168.0, 170.0, 172.0, 174.0, 176.0, 178.0, 180.0, 182.0, 184.0, 186.0, 188.0, 190.0, 192.0, 194.0, 196.0, 198.0, 200.0, 202.0, 204.0, 206.0, 208.0, 210.0]

y_2 = [2.2910963852194262e-21, 2.5036241886557506e-21, 2.790963416440546e-21, 3.0130680503797805e-21, 3.0890382932018132e-21, 3.3385161911918054e-21, 3.6272500006503804e-21, 3.8094258737066745e-21, 3.962013055350225e-21, 4.222200813155507e-21, 4.269168230198215e-21, 4.5440249211647395e-21, 4.839707707524846e-21, 5.089890727806043e-21, 5.261897111394708e-21, 5.5306822397458095e-21, 5.6212525353564914e-21, 5.71930438318742e-21, 6.068851329512762e-21, 6.659796990580275e-21, 6.640084163736441e-21, 6.974472401390285e-21, 7.235220636346861e-21, 7.42888543500852e-21, 7.68410692159234e-21, 8.227010684077543e-21, 8.57761716135209e-21, 8.78160449433916e-21, 9.256797398357753e-21, 9.512730905277979e-21, 9.877727948549268e-21, 1.0286120196936021e-20, 1.0482052604135322e-20, 1.1317892467506498e-20, 1.1592731486344916e-20, 1.1938454172660492e-20, 1.2319394821333983e-20, 1.3208666437940134e-20, 1.3518802244009191e-20, 1.4281239103721595e-20, 1.5202648684562753e-20, 1.5730166296746173e-20, 1.6311521296999433e-20, 1.715025757376222e-20, 1.7738115116057543e-20, 2.442033779983405e-20, 2.810474406566866e-20, 2.787991571316773e-20, 2.923155858873748e-20, 3.007170438827055e-20, 3.0671644428524155e-20, 3.17496370692417e-20, 3.23798758752675e-20, 3.223086568555706e-20, 3.3320214936033437e-20, 3.3950595655970576e-20, 3.4718915240703666e-20, 3.457656888533493e-20, 3.5805318976157914e-20, 3.612091890491987e-20, 3.6297957237821147e-20, 3.727826910548556e-20, 3.716976091294667e-20, 3.7562221066402467e-20, 3.7858481363637585e-20, 3.8169631548198136e-20, 3.873996286814575e-20, 3.8535760114503744e-20, 3.911870574532173e-20, 3.978948472248701e-20, 3.96812760936049e-20, 4.0541789016351394e-20, 4.070848278675065e-20, 4.056416274981832e-20, 4.0747527762245244e-20, 4.0954193756734306e-20, 4.130770742936087e-20, 4.1963476169210205e-20, 4.1521305058775856e-20, 4.188932600483549e-20, 4.268516523219416e-20, 4.239723152243848e-20, 4.2424575156845484e-20, 4.2905511786016784e-20, 4.3121436570454197e-20, 4.3165781560164535e-20, 4.322881049069666e-20, 4.367996270161183e-20, 4.3324053547429523e-20, 4.4267614360684866e-20, 4.4393013648774344e-20, 4.349637062329515e-20, 4.36255243601487e-20, 4.4019985094095945e-20, 4.351436598412846e-20, 4.3693225079146015e-20, 4.3665336553492754e-20, 4.4055290433622466e-20, 4.464115578635246e-20, 4.4710695264407615e-20, 4.424709528137636e-20, 4.4582033362040966e-20, 4.4968553458563157e-20, 4.470634400374162e-20, 4.464711346121424e-20, 4.4169959184487915e-20, 4.462973587354141e-20, 4.404362997145307e-20, 4.4055290213597814e-20, 4.4911926072772703e-20, 4.418742563717657e-20, 4.41555124186061e-20, 4.413829102672012e-20, 4.3289388131023605e-20, 4.330851180803315e-20, 4.419292440678085e-20, 4.3623379044113224e-20, 4.2806557394406624e-20, 4.347581323874764e-20, 4.391502021807816e-20, 4.3180431020601303e-20, 4.290755684518524e-20, 4.331591172047608e-20, 4.3035816554655354e-20, 4.273691534215363e-20, 4.2312799008872175e-20, 4.181875982238174e-20, 4.166858197549939e-20, 4.165421865580801e-20, 4.1703049280289345e-20, 4.1129291628166214e-20, 4.13653262257889e-20, 4.0652203867456746e-20, 4.0571583944027697e-20, 4.023335938266605e-20, 3.995646581756096e-20, 3.9764995976353826e-20, 3.96102451212362e-20, 3.908536617530974e-20, 3.876582937397666e-20, 3.904723620942827e-20, 3.8413154654431684e-20, 3.792866784624232e-20, 3.7655030433180885e-20, 3.687259026856779e-20, 3.6737276578765864e-20, 3.6388472378267036e-20, 3.5400555731661946e-20, 3.51994074782635e-20, 3.4878692529202436e-20, 3.370748372125426e-20, 3.3521834098837875e-20, 3.357088415078376e-20, 3.2471066490962824e-20, 3.231486356804195e-20, 3.196563266493261e-20, 3.140626135344336e-20, 3.1233583012208196e-20, 3.043676554049562e-20, 2.97148931841847e-20, 2.9304060278786886e-20, 2.843639163118055e-20, 2.7088118846749596e-20, 2.6668770232458674e-20, 2.5317741054419263e-20, 2.2632487112149384e-20, 1.7051273931925555e-20, 1.644957206106018e-20, 1.533328131415964e-20, 1.5157604276611147e-20, 1.4601258179229742e-20, 1.3513425688319074e-20, 1.310919483954386e-20, 1.2644773551080796e-20, 1.1985342732948615e-20, 1.1746220706392467e-20, 1.1253732080855462e-20, 1.0898698734619835e-20, 1.025911094900388e-20, 9.970046736129055e-21, 9.459348326524601e-21, 9.258043953253469e-21, 8.730233042606477e-21, 8.537354261437733e-21, 8.297105082912295e-21, 7.692390487585071e-21, 7.760522537399937e-21, 7.087951849965947e-21, 6.905473719198504e-21, 6.7262542747837674e-21, 6.711708677921627e-21, 6.202272210547485e-21, 6.150670065991061e-21, 5.8161968115262e-21, 5.457718874701264e-21, 5.1703565168072164e-21, 4.966152511183574e-21, 4.6329017649498e-21, 4.4378952742500075e-21, 4.2540638681771194e-21, 4.296285387480224e-21, 4.010729910010557e-21, 3.704022949326906e-21, 3.5569228488293754e-21, 3.4105640410143695e-21, 3.22512314359118e-21, 2.913199159446547e-21, 2.8695108269631298e-21, 2.6843795816009757e-21, 2.4529233716401215e-21, 2.1578424052250124e-21]

plt.plot(x_2, y_2, 'r.')


# In[13]:


#Example 4
x_4 = [-42.0, -39.9, -36.75, -33.6, -30.45, -27.3, -24.15, -21.0, -19.95, -18.9, -17.85, -16.8, -15.75, -14.7, -13.65, -12.6, -11.55, -10.5, -9.45, -8.4, -7.35, -6.3, -5.25, -4.2, -3.15, -2.1, -1.05, -0.0, 1.05, 2.1, 3.15, 4.2, 5.25, 6.3, 7.35, 8.4, 9.45, 10.5, 11.55, 12.6, 13.65, 14.7, 15.75, 16.8, 17.85, 18.9, 19.95, 21.0, 24.15, 27.3, 30.45, 33.6, 36.75, 39.9, 42.0]

y_4 = [0.0252, 0.0276, 0.0324, 0.0384, 0.0486, 0.0606, 0.0762, 0.1002, 0.1122, 0.126, 0.1392, 0.159, 0.1818, 0.2142, 0.2544, 0.3552, 0.774, 2.4708, 3.8052, 4.0284, 4.1526, 4.206, 4.245, 4.2798, 4.293, 4.326, 4.3218, 4.3134, 4.3176, 4.302, 4.2822, 4.2588, 4.2216, 4.176, 4.1178, 3.9888, 3.69, 2.2848, 0.678, 0.3348, 0.2484, 0.2082, 0.1806, 0.1578, 0.1392, 0.1248, 0.1098, 0.1014, 0.0762, 0.0576, 0.0438, 0.0336, 0.0264, 0.0192, 0.0156]

plt.plot(x_4, y_4, 'r.')


# In[14]:


#Example 5
x_5 = [-60.0, -57.0, -52.5, -48.0, -43.5, -39.0, -34.5, -30.0, -28.5, -27.0, -25.5, -24.0, -22.5, -21.0, -19.5, -18.0, -16.5, -15.0, -13.5, -12.0, -10.5, -9.0, -7.5, -6.0, -4.5, -3.0, -1.5, -0.0, 1.5, 3.0, 4.5, 6.0, 7.5, 9.0, 10.5, 12.0, 13.5, 15.0, 16.5, 18.0, 19.5, 21.0, 22.5, 24.0, 25.5, 27.0, 28.5, 30.0, 34.5, 39.0, 43.5, 48.0, 52.5, 57.0, 60.0]

y_5 = [0.0096, 0.0114, 0.0144, 0.0162, 0.0192, 0.0222, 0.027, 0.0324, 0.0348, 0.0366, 0.039, 0.042, 0.0468, 0.051, 0.0546, 0.0606, 0.078, 0.1488, 0.2394, 0.2634, 0.2754, 0.2838, 0.2874, 0.2898, 0.2892, 0.2916, 0.2952, 0.2958, 0.2958, 0.294, 0.2916, 0.2904, 0.2868, 0.2838, 0.273, 0.267, 0.2514, 0.201, 0.1092, 0.0684, 0.0594, 0.0516, 0.0486, 0.045, 0.042, 0.0378, 0.0354, 0.033, 0.027, 0.0222, 0.018, 0.0156, 0.0132, 0.0114, 0.0108]

plt.plot(x_5, y_5, 'r.')


# In[30]:


#Example 6
x_6 = [-210.0, -208.0, -206.0, -204.0, -202.0, -200.0, -198.0, -196.0, -194.0, -192.0, -190.0, -188.0, -186.0, -184.0, -182.0, -180.0, -178.0, -176.0, -174.0, -172.0, -170.0, -168.0, -166.0, -164.0, -162.0, -160.0, -158.0, -156.0, -154.0, -152.0, -150.0, -148.0, -146.0, -144.0, -142.0, -140.0, -138.0, -136.0, -134.0, -132.0, -130.0, -128.0, -126.0, -124.0, -122.0, -120.0, -118.0, -116.0, -114.0, -112.0, -110.0, -108.0, -106.0, -104.0, -102.0, -100.0, -98.0, -96.0, -94.0, -92.0, -90.0, -88.0, -86.0, -84.0, -82.0, -80.0, -78.0, -76.0, -74.0, -72.0, -70.0, -68.0, -66.0, -64.0, -62.0, -60.0, -58.0, -56.0, -54.0, -52.0, -50.0, -48.0, -46.0, -44.0, -42.0, -40.0, -38.0, -36.0, -34.0, -32.0, -30.0, -28.0, -26.0, -24.0, -22.0, -20.0, -18.0, -16.0, -14.0, -12.0, -10.0, -8.0, -6.0, -4.0, -2.0, 0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0, 20.0, 22.0, 24.0, 26.0, 28.0, 30.0, 32.0, 34.0, 36.0, 38.0, 40.0, 42.0, 44.0, 46.0, 48.0, 50.0, 52.0, 54.0, 56.0, 58.0, 60.0, 62.0, 64.0, 66.0, 68.0, 70.0, 72.0, 74.0, 76.0, 78.0, 80.0, 82.0, 84.0, 86.0, 88.0, 90.0, 92.0, 94.0, 96.0, 98.0, 100.0, 102.0, 104.0, 106.0, 108.0, 110.0, 112.0, 114.0, 116.0, 118.0, 120.0, 122.0, 124.0, 126.0, 128.0, 130.0, 132.0, 134.0, 136.0, 138.0, 140.0, 142.0, 144.0, 146.0, 148.0, 150.0, 152.0, 154.0, 156.0, 158.0, 160.0, 162.0, 164.0, 166.0, 168.0, 170.0, 172.0, 174.0, 176.0, 178.0, 180.0, 182.0, 184.0, 186.0, 188.0, 190.0, 192.0, 194.0, 196.0, 198.0, 200.0, 202.0, 204.0, 206.0, 208.0, 210.0]

y_6 = [6.28677714873522e-23, 6.015350936499715e-23, 5.050466820021878e-23, 3.0800495931900416e-23, 6.377920964215488e-23, 6.401156378294282e-23, 6.673973602410699e-23, 6.14772379985596e-23, 7.035504781437588e-23, 6.647813191209031e-23, 8.987125715883308e-23, 1.0962014891612314e-22, 1.2061338292031982e-22, 1.511960831568581e-22, 1.334065005307613e-22, 1.444621326901623e-22, 1.175777888537881e-22, 9.186349480778201e-23, 1.053193562536479e-22, 1.5742004713597796e-22, 1.9559524292954712e-22, 1.2975708925059726e-22, 1.277183209246732e-22, 1.2263955311283674e-22, 1.5353376089528623e-22, 1.2712004785515416e-22, 1.8175851330201075e-22, 1.9378582865299866e-22, 1.9575543612820894e-22, 2.0711651344023996e-22, 2.3265722893063886e-22, 2.3154693503059647e-22, 1.8855470922551297e-22, 2.184780483716518e-22, 2.952175821825184e-22, 2.8652448619341015e-22, 2.4568426732173105e-22, 1.912857526619866e-22, 3.008805002156601e-22, 3.6185995153711573e-22, 2.81322354449313e-22, 2.948153062878852e-22, 3.194805549219883e-22, 3.4751070504526084e-22, 3.4743114643604644e-22, 4.081638851317782e-22, 3.3001634072460708e-22, 3.5458861749286915e-22, 4.2090044335995083e-22, 3.691238498356027e-22, 4.5631945488501005e-22, 5.4156999315226965e-22, 5.224972938954798e-22, 5.734246095970507e-22, 5.526504769290482e-22, 6.441692509729275e-22, 7.829637930515487e-22, 7.43684923696153e-22, 6.8998937729163565e-22, 7.066820664503247e-22, 6.904192025560066e-22, 6.832403110417501e-22, 9.373992601352913e-22, 9.25444957842673e-22, 9.340785735574883e-22, 9.181511879268444e-22, 8.663690005178265e-22, 1.0434066030053434e-21, 1.2962108940029255e-21, 1.3355478013347152e-21, 1.4021910164883172e-21, 1.5690564732425149e-21, 1.566013245165699e-21, 1.6975675519659043e-21, 1.83282257433417e-21, 2.0290914559925582e-21, 2.05154769960568e-21, 2.1962070415808424e-21, 2.329555708123834e-21, 2.4536524673575805e-21, 2.744463218160622e-21, 3.11922700830001e-21, 3.204419854651303e-21, 3.631246305069154e-21, 4.15929182414834e-21, 4.451813287438604e-21, 4.792524469544286e-21, 5.108179013813069e-21, 5.9648825866908175e-21, 6.858022597842671e-21, 8.128974118153552e-21, 9.434115127406861e-21, 3.3594905999020805e-20, 6.668677918979607e-20, 6.684752587868652e-20, 6.948656686698981e-20, 6.982227274661475e-20, 7.347303754160397e-20, 7.24006298199293e-20, 7.355394942825563e-20, 7.051651940123586e-20, 7.335344406374832e-20, 7.329948957812673e-20, 7.382156328041935e-20, 7.310865459287941e-20, 7.312151666175396e-20, 7.221875772555963e-20, 7.226980355168221e-20, 7.43343220647108e-20, 7.291653407709913e-20, 7.269976202869558e-20, 6.959655129022066e-20, 7.074624914392494e-20, 6.890957193104233e-20, 6.934735254095059e-20, 6.8873685713126e-20, 6.657664789141802e-20, 6.200446594027494e-20, 3.2073387007519896e-20, 9.750459278176593e-21, 7.645458146495585e-21, 6.787444718324345e-21, 5.989525591765672e-21, 4.96958373446326e-21, 4.479484718167826e-21, 4.521796963732406e-21, 4.038886327568879e-21, 3.357461690860179e-21, 3.3659469103996706e-21, 2.7583433922652415e-21, 2.8182618063797066e-21, 2.6505968953974047e-21, 2.4896196021720153e-21, 2.2694950996011762e-21, 2.0996658174245134e-21, 1.8510599588510804e-21, 1.7916573569103085e-21, 1.5872865449561859e-21, 1.751203093313704e-21, 1.4766593500232548e-21, 1.573455010555304e-21, 1.3529208817208222e-21, 1.073563574489905e-21, 1.1771216837928365e-21, 1.0415174135986323e-21, 1.0628640008073597e-21, 1.039223533842794e-21, 1.028291229953572e-21, 9.412076460304985e-22, 8.320737929097531e-22, 7.686643986132835e-22, 6.331219138656264e-22, 6.908221899235576e-22, 6.638424584162705e-22, 6.060277046583144e-22, 5.997283211665274e-22, 5.911463640773349e-22, 5.738885172600806e-22, 5.406513165535457e-22, 3.953689656339179e-22, 5.446035679645354e-22, 4.526703023206118e-22, 4.806450349645417e-22, 4.35111455786463e-22, 4.519849592416851e-22, 3.8249676765525045e-22, 3.5917054406497582e-22, 3.77651013264907e-22, 3.782213868737279e-22, 3.4179914389331163e-22, 3.306944233647011e-22, 3.027612213871131e-22, 2.8858086718240066e-22, 2.5010949568332977e-22, 2.5380653094428096e-22, 2.8520695104677225e-22, 2.186350573535303e-22, 3.040252685689617e-22, 2.6216939080217406e-22, 2.656676371147944e-22, 2.0373980377734673e-22, 2.1958461984538776e-22, 1.7584870733621828e-22, 1.783862614438319e-22, 1.3604983722376723e-22, 1.7539702457750967e-22, 1.8965271580942392e-22, 1.238802000333676e-22, 1.40607795415966e-22, 1.3005998630756913e-22, 1.3467915105864815e-22, 1.0270370139029353e-22, 1.2923843677945801e-22, 1.1727521480398657e-22, 1.1019089518685079e-22, 1.187936868795503e-22, 1.0502860312792997e-22, 9.516709888018514e-23, 1.3596076183389932e-22, 7.851226221450051e-23, 1.1354290679287087e-22, 4.336086474940113e-23, 5.63621331607038e-23, 9.499586523760478e-23, 7.05979843530753e-23, 6.134282745798712e-23, 5.096827669218248e-23, 5.972448747054793e-23, 5.030720722916934e-23, 3.948478099745085e-23, 3.880251506265694e-23]

plt.plot(x_6, y_6, 'r.')


# ## Fitting attempt

# In[21]:


spl_1 = UnivariateSpline(x_1, y_1)

common_x = np.linspace(-220, 220, 1000)

fit_y_1 = spl_1(common_x)

plt.plot(common_x, fit_y_1, 'b-')
plt.plot(x_1, y_1, 'r.')


# In[24]:


spl_2 = UnivariateSpline(x_1, y_1, s=0)

common_x = np.linspace(-220, 220, 1000)

fit_y_2 = spl_2(common_x)

plt.plot(common_x, fit_y_2, 'b-')
plt.plot(x_1, y_1, 'r.')
plt.show()

plt.plot(common_x, fit_y_2, 'b-')
plt.plot(x_1, y_1, 'r.')
plt.xlim(-50, 50)
plt.show()


# In[34]:


spl_3 = UnivariateSpline(x_6, y_6, s=0.1)

common_x_3 = np.linspace(-80, 80, 1000)

fit_y_3 = spl_3(common_x_3)

plt.plot(common_x_3, fit_y_3, 'b-')
plt.plot(x_6, y_6, 'r.')
plt.show()


# In[33]:


spl_4 = UnivariateSpline(x_6, y_6, s=0)

common_x_3 = np.linspace(-80, 80, 1000)

fit_y_3 = spl_4(common_x_3)

plt.plot(common_x_3, fit_y_3, 'b-')
plt.plot(x_6, y_6, 'r.')
plt.xlim(-40,40)
plt.show()


# In[ ]:

最佳答案

虽然仍在考虑如何正确删除使用样条方法的跳跃,但这将是一种带有附加跳跃的简单多项式方法。在实践中,这也将两个解决方案以一种平滑的方式粘合在一起。

import matplotlib.pyplot as plt
import numpy as np
from numpy.polynomial.polynomial import polyval
from scipy.optimize import curve_fit


#Example 1
x_1 = [-210.0, -208.0, -206.0, -204.0, -202.0, -200.0, -198.0, -196.0, -194.0, -192.0, -190.0, -188.0, -186.0, -184.0, -182.0, -180.0, -178.0, -176.0, -174.0, -172.0, -170.0, -168.0, -166.0, -164.0, -162.0, -160.0, -158.0, -156.0, -154.0, -152.0, -150.0, -148.0, -146.0, -144.0, -142.0, -140.0, -138.0, -136.0, -134.0, -132.0, -130.0, -128.0, -126.0, -124.0, -122.0, -120.0, -118.0, -116.0, -114.0, -112.0, -110.0, -108.0, -106.0, -104.0, -102.0, -100.0, -98.0, -96.0, -94.0, -92.0, -90.0, -88.0, -86.0, -84.0, -82.0, -80.0, -78.0, -76.0, -74.0, -72.0, -70.0, -68.0, -66.0, -64.0, -62.0, -60.0, -58.0, -56.0, -54.0, -52.0, -50.0, -48.0, -46.0, -44.0, -42.0, -40.0, -38.0, -36.0, -34.0, -32.0, -30.0, -28.0, -26.0, -24.0, -22.0, -20.0, -18.0, -16.0, -14.0, -12.0, -10.0, -8.0, -6.0, -4.0, -2.0, 0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0, 20.0, 22.0, 24.0, 26.0, 28.0, 30.0, 32.0, 34.0, 36.0, 38.0, 40.0, 42.0, 44.0, 46.0, 48.0, 50.0, 52.0, 54.0, 56.0, 58.0, 60.0, 62.0, 64.0, 66.0, 68.0, 70.0, 72.0, 74.0, 76.0, 78.0, 80.0, 82.0, 84.0, 86.0, 88.0, 90.0, 92.0, 94.0, 96.0, 98.0, 100.0, 102.0, 104.0, 106.0, 108.0, 110.0, 112.0, 114.0, 116.0, 118.0, 120.0, 122.0, 124.0, 126.0, 128.0, 130.0, 132.0, 134.0, 136.0, 138.0, 140.0, 142.0, 144.0, 146.0, 148.0, 150.0, 152.0, 154.0, 156.0, 158.0, 160.0, 162.0, 164.0, 166.0, 168.0, 170.0, 172.0, 174.0, 176.0, 178.0, 180.0, 182.0, 184.0, 186.0, 188.0, 190.0, 192.0, 194.0, 196.0, 198.0, 200.0, 202.0, 204.0, 206.0, 208.0, 210.0]
y_1 = [1.4295226140525178e-21, 1.6427103012883458e-21, 1.821549871710417e-21, 1.8600922737090837e-21, 1.952935493151029e-21, 2.0808512562831945e-21, 2.088480067210673e-21, 2.3252524971469115e-21, 2.3604087098101423e-21, 2.5777736716038026e-21, 2.6388544259730252e-21, 2.743766230009555e-21, 3.021176501652941e-21, 3.255022357012754e-21, 3.1825874279608568e-21, 3.3844734840462558e-21, 3.613537349804512e-21, 3.689479855589563e-21, 3.979344292112167e-21, 4.011284931665291e-21, 4.3331493445964974e-21, 4.42087387693927e-21, 4.61983100901474e-21, 4.903784307266063e-21, 5.296673588468055e-21, 5.268635414465153e-21, 5.534134900475608e-21, 5.861336704296021e-21, 6.211240190204385e-21, 6.413337671786028e-21, 6.736218974358413e-21, 6.933955303835514e-21, 7.245392516087334e-21, 7.616309507922374e-21, 8.172488917215604e-21, 8.169783959684506e-21, 8.880599809963712e-21, 9.199841185305185e-21, 9.707103772522977e-21, 1.0364862022889146e-20, 1.0811609909255555e-20, 1.1583389003976744e-20, 1.195989228313853e-20, 1.30126856493689e-20, 1.3376091484825573e-20, 1.4115440812380502e-20, 1.539229091776661e-20, 1.6059712561525453e-20, 1.6895967848631177e-20, 1.795689663143609e-20, 1.9536970071975345e-20, 2.0583008110924713e-20, 2.235540235635208e-20, 2.5947903879402268e-20, 6.180552377805634e-20, 8.355762167331923e-20, 8.496095568028067e-20, 8.765163907836433e-20, 8.771611279381496e-20, 8.996823318793393e-20, 9.101139069844862e-20, 9.206888878208697e-20, 9.272943402020724e-20, 9.435389469921155e-20, 9.500337591066436e-20, 9.452973019761442e-20, 9.648633430929047e-20, 9.700995580651313e-20, 9.911105200349805e-20, 9.842938696984599e-20, 9.786377648899059e-20, 9.901480010020134e-20, 1.0011040566633435e-19, 9.872422602941769e-20, 1.0048662803205042e-19, 1.0114903825570249e-19, 1.0059657242599848e-19, 1.0132771202896158e-19, 1.0154902286832572e-19, 1.0068825161789439e-19, 1.0146051065498602e-19, 1.0141934925868303e-19, 1.0216895105279509e-19, 1.0303946568701418e-19, 1.0374124925209062e-19, 1.0227728143897267e-19, 1.0262217511348233e-19, 1.0276500601751216e-19, 1.0221461450659016e-19, 1.0440281095195915e-19, 1.0573943858373224e-19, 1.0419443055088742e-19, 1.0403161868309251e-19, 1.0471634959640928e-19, 1.0457793287449229e-19, 1.0504213884925815e-19, 1.0415412360146518e-19, 1.0434049836638474e-19, 1.0391012318687714e-19, 1.0396020148265584e-19, 1.04951396652105e-19, 1.0344364925258479e-19, 1.0491096962922902e-19, 1.0415025435259349e-19, 1.046150383912583e-19, 1.0517031746260079e-19, 1.0432012386156581e-19, 1.0312319606274798e-19, 1.0456768903645697e-19, 1.0393270631189554e-19, 1.0412044375056791e-19, 1.0290060078672382e-19, 1.0261760590464094e-19, 1.0275738639552336e-19, 1.0404888376507175e-19, 1.0282264054940699e-19, 1.0311610076116371e-19, 1.0269367041009052e-19, 1.0223808065771127e-19, 1.0184789236933238e-19, 1.0097119873518135e-19, 1.0174979308736812e-19, 1.0251610302833564e-19, 1.000881431770874e-19, 1.009924373969923e-19, 1.011081374579119e-19, 9.992147913511413e-20, 1.0075329648516413e-19, 9.888206907990243e-20, 9.948660996811841e-20, 9.834779734967718e-20, 9.848154129024306e-20, 9.735079123737905e-20, 9.782276778837801e-20, 9.75924444064314e-20, 9.644272628509293e-20, 9.647677975528781e-20, 9.632505269173146e-20, 9.508677922926286e-20, 9.373387792792943e-20, 9.308928880664975e-20, 9.255456132058517e-20, 9.183996647052114e-20, 9.224537352857811e-20, 9.153095352090768e-20, 9.063345775506358e-20, 8.930568658721485e-20, 8.98206055956997e-20, 8.728312259974252e-20, 8.611547380813165e-20, 8.562279708334554e-20, 8.389801067327348e-20, 8.210582252046584e-20, 8.140069014074383e-20, 7.851960978690971e-20, 7.513436789426143e-20, 5.638352302680673e-20, 2.4859004101872068e-20, 2.1566799052306043e-20, 1.949059123294511e-20, 1.8582483735676543e-20, 1.7207284929588388e-20, 1.604838437714165e-20, 1.5023165841913053e-20, 1.4470032697073442e-20, 1.335443871139727e-20, 1.261864465821435e-20, 1.1696331799273179e-20, 1.12803378885813e-20, 1.0991595000466702e-20, 1.0065494691025012e-20, 9.715474570669913e-21, 9.356694627842102e-21, 9.034172189282595e-21, 8.50198334192219e-21, 8.241821794188562e-21, 7.777015748304419e-21, 7.424935256042654e-21, 7.055353404435796e-21, 6.5923927471651786e-21, 6.228610927711253e-21, 5.993195989560806e-21, 5.651661917630429e-21, 5.6913544921251365e-21, 5.205197713047647e-21, 5.191103961263008e-21, 4.957168426751453e-21, 4.846866967618264e-21, 4.466018769889993e-21, 4.3525640067408905e-21, 4.252492600315592e-21, 3.948750588250809e-21, 3.622136021452661e-21, 3.3592997229398316e-21, 3.294398935394057e-21, 3.2844230221832043e-21, 3.252908923193404e-21, 3.02291449964085e-21, 2.7542313899818323e-21, 2.7087762261072507e-21, 2.5415655501738826e-21, 2.414396761194755e-21, 2.303670400637897e-21, 2.0787727179258136e-21, 2.070699171984851e-21, 2.0715170116089346e-21, 1.8988826775374163e-21, 1.863615058015695e-21, 1.7050041100649195e-21, 1.5402602118223378e-21, 1.3249207166865905e-21]
#Example 2
x_2 = [-210.0, -208.0, -206.0, -204.0, -202.0, -200.0, -198.0, -196.0, -194.0, -192.0, -190.0, -188.0, -186.0, -184.0, -182.0, -180.0, -178.0, -176.0, -174.0, -172.0, -170.0, -168.0, -166.0, -164.0, -162.0, -160.0, -158.0, -156.0, -154.0, -152.0, -150.0, -148.0, -146.0, -144.0, -142.0, -140.0, -138.0, -136.0, -134.0, -132.0, -130.0, -128.0, -126.0, -124.0, -122.0, -120.0, -118.0, -116.0, -114.0, -112.0, -110.0, -108.0, -106.0, -104.0, -102.0, -100.0, -98.0, -96.0, -94.0, -92.0, -90.0, -88.0, -86.0, -84.0, -82.0, -80.0, -78.0, -76.0, -74.0, -72.0, -70.0, -68.0, -66.0, -64.0, -62.0, -60.0, -58.0, -56.0, -54.0, -52.0, -50.0, -48.0, -46.0, -44.0, -42.0, -40.0, -38.0, -36.0, -34.0, -32.0, -30.0, -28.0, -26.0, -24.0, -22.0, -20.0, -18.0, -16.0, -14.0, -12.0, -10.0, -8.0, -6.0, -4.0, -2.0, 0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0, 20.0, 22.0, 24.0, 26.0, 28.0, 30.0, 32.0, 34.0, 36.0, 38.0, 40.0, 42.0, 44.0, 46.0, 48.0, 50.0, 52.0, 54.0, 56.0, 58.0, 60.0, 62.0, 64.0, 66.0, 68.0, 70.0, 72.0, 74.0, 76.0, 78.0, 80.0, 82.0, 84.0, 86.0, 88.0, 90.0, 92.0, 94.0, 96.0, 98.0, 100.0, 102.0, 104.0, 106.0, 108.0, 110.0, 112.0, 114.0, 116.0, 118.0, 120.0, 122.0, 124.0, 126.0, 128.0, 130.0, 132.0, 134.0, 136.0, 138.0, 140.0, 142.0, 144.0, 146.0, 148.0, 150.0, 152.0, 154.0, 156.0, 158.0, 160.0, 162.0, 164.0, 166.0, 168.0, 170.0, 172.0, 174.0, 176.0, 178.0, 180.0, 182.0, 184.0, 186.0, 188.0, 190.0, 192.0, 194.0, 196.0, 198.0, 200.0, 202.0, 204.0, 206.0, 208.0, 210.0]
y_2 = [2.2910963852194262e-21, 2.5036241886557506e-21, 2.790963416440546e-21, 3.0130680503797805e-21, 3.0890382932018132e-21, 3.3385161911918054e-21, 3.6272500006503804e-21, 3.8094258737066745e-21, 3.962013055350225e-21, 4.222200813155507e-21, 4.269168230198215e-21, 4.5440249211647395e-21, 4.839707707524846e-21, 5.089890727806043e-21, 5.261897111394708e-21, 5.5306822397458095e-21, 5.6212525353564914e-21, 5.71930438318742e-21, 6.068851329512762e-21, 6.659796990580275e-21, 6.640084163736441e-21, 6.974472401390285e-21, 7.235220636346861e-21, 7.42888543500852e-21, 7.68410692159234e-21, 8.227010684077543e-21, 8.57761716135209e-21, 8.78160449433916e-21, 9.256797398357753e-21, 9.512730905277979e-21, 9.877727948549268e-21, 1.0286120196936021e-20, 1.0482052604135322e-20, 1.1317892467506498e-20, 1.1592731486344916e-20, 1.1938454172660492e-20, 1.2319394821333983e-20, 1.3208666437940134e-20, 1.3518802244009191e-20, 1.4281239103721595e-20, 1.5202648684562753e-20, 1.5730166296746173e-20, 1.6311521296999433e-20, 1.715025757376222e-20, 1.7738115116057543e-20, 2.442033779983405e-20, 2.810474406566866e-20, 2.787991571316773e-20, 2.923155858873748e-20, 3.007170438827055e-20, 3.0671644428524155e-20, 3.17496370692417e-20, 3.23798758752675e-20, 3.223086568555706e-20, 3.3320214936033437e-20, 3.3950595655970576e-20, 3.4718915240703666e-20, 3.457656888533493e-20, 3.5805318976157914e-20, 3.612091890491987e-20, 3.6297957237821147e-20, 3.727826910548556e-20, 3.716976091294667e-20, 3.7562221066402467e-20, 3.7858481363637585e-20, 3.8169631548198136e-20, 3.873996286814575e-20, 3.8535760114503744e-20, 3.911870574532173e-20, 3.978948472248701e-20, 3.96812760936049e-20, 4.0541789016351394e-20, 4.070848278675065e-20, 4.056416274981832e-20, 4.0747527762245244e-20, 4.0954193756734306e-20, 4.130770742936087e-20, 4.1963476169210205e-20, 4.1521305058775856e-20, 4.188932600483549e-20, 4.268516523219416e-20, 4.239723152243848e-20, 4.2424575156845484e-20, 4.2905511786016784e-20, 4.3121436570454197e-20, 4.3165781560164535e-20, 4.322881049069666e-20, 4.367996270161183e-20, 4.3324053547429523e-20, 4.4267614360684866e-20, 4.4393013648774344e-20, 4.349637062329515e-20, 4.36255243601487e-20, 4.4019985094095945e-20, 4.351436598412846e-20, 4.3693225079146015e-20, 4.3665336553492754e-20, 4.4055290433622466e-20, 4.464115578635246e-20, 4.4710695264407615e-20, 4.424709528137636e-20, 4.4582033362040966e-20, 4.4968553458563157e-20, 4.470634400374162e-20, 4.464711346121424e-20, 4.4169959184487915e-20, 4.462973587354141e-20, 4.404362997145307e-20, 4.4055290213597814e-20, 4.4911926072772703e-20, 4.418742563717657e-20, 4.41555124186061e-20, 4.413829102672012e-20, 4.3289388131023605e-20, 4.330851180803315e-20, 4.419292440678085e-20, 4.3623379044113224e-20, 4.2806557394406624e-20, 4.347581323874764e-20, 4.391502021807816e-20, 4.3180431020601303e-20, 4.290755684518524e-20, 4.331591172047608e-20, 4.3035816554655354e-20, 4.273691534215363e-20, 4.2312799008872175e-20, 4.181875982238174e-20, 4.166858197549939e-20, 4.165421865580801e-20, 4.1703049280289345e-20, 4.1129291628166214e-20, 4.13653262257889e-20, 4.0652203867456746e-20, 4.0571583944027697e-20, 4.023335938266605e-20, 3.995646581756096e-20, 3.9764995976353826e-20, 3.96102451212362e-20, 3.908536617530974e-20, 3.876582937397666e-20, 3.904723620942827e-20, 3.8413154654431684e-20, 3.792866784624232e-20, 3.7655030433180885e-20, 3.687259026856779e-20, 3.6737276578765864e-20, 3.6388472378267036e-20, 3.5400555731661946e-20, 3.51994074782635e-20, 3.4878692529202436e-20, 3.370748372125426e-20, 3.3521834098837875e-20, 3.357088415078376e-20, 3.2471066490962824e-20, 3.231486356804195e-20, 3.196563266493261e-20, 3.140626135344336e-20, 3.1233583012208196e-20, 3.043676554049562e-20, 2.97148931841847e-20, 2.9304060278786886e-20, 2.843639163118055e-20, 2.7088118846749596e-20, 2.6668770232458674e-20, 2.5317741054419263e-20, 2.2632487112149384e-20, 1.7051273931925555e-20, 1.644957206106018e-20, 1.533328131415964e-20, 1.5157604276611147e-20, 1.4601258179229742e-20, 1.3513425688319074e-20, 1.310919483954386e-20, 1.2644773551080796e-20, 1.1985342732948615e-20, 1.1746220706392467e-20, 1.1253732080855462e-20, 1.0898698734619835e-20, 1.025911094900388e-20, 9.970046736129055e-21, 9.459348326524601e-21, 9.258043953253469e-21, 8.730233042606477e-21, 8.537354261437733e-21, 8.297105082912295e-21, 7.692390487585071e-21, 7.760522537399937e-21, 7.087951849965947e-21, 6.905473719198504e-21, 6.7262542747837674e-21, 6.711708677921627e-21, 6.202272210547485e-21, 6.150670065991061e-21, 5.8161968115262e-21, 5.457718874701264e-21, 5.1703565168072164e-21, 4.966152511183574e-21, 4.6329017649498e-21, 4.4378952742500075e-21, 4.2540638681771194e-21, 4.296285387480224e-21, 4.010729910010557e-21, 3.704022949326906e-21, 3.5569228488293754e-21, 3.4105640410143695e-21, 3.22512314359118e-21, 2.913199159446547e-21, 2.8695108269631298e-21, 2.6843795816009757e-21, 2.4529233716401215e-21, 2.1578424052250124e-21]

def jump( x, xs, a, s, off):
out = off + a * ( 0.5 * ( 1 + np.tanh( s * ( x + xs ) ) ) - 0.5 * ( 1 + np.tanh( s * ( x - xs ) ) ) )
return out

def poly_approach( x, a0, a1, a2, a3, a4, a5, a6, b0, b1, b2, b3, b4, b5, b6, xs, s ):
p1 = polyval( x, [ a0, a1, a2, a3, a4, a5, a6 ] ) * jump( x, xs, 1, s, 0 )
p2 = polyval( x, [ b0, b1, b2, b3, b4, b5, b6 ] ) * ( 1 - jump( x, xs, 1, s, 0 ) )
return p1 + p2

### I rescale the data to get resonable orders of magnitude
myX, myY = np.array( x_2 ) / 200., 1e20 * np.array( y_2 )
### simple double polynomial fit
sol, err = curve_fit( poly_approach, myX, myY, p0=[4, 0, -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, .6, 15] )


xList = np.linspace(-1.0, 1.0, 750)

fig = plt.figure()
ax = fig.add_subplot( 1, 1, 1)
ax.plot( myX, myY, 'r.')


ax.plot(xList, poly_approach( xList, *sol ), label='fit')
ax.plot(xList, poly_approach( xList, 4,0,-3,0,0,0,0, 0,0, 0,0,0,0,0, .6, 15 ) , ls='--', label='initial guess')
ax.legend( loc=0 )
plt.show()

提供 polynomial fit

关于python-3.x - 我应该如何使用单变量样条曲线来拟合特定形状的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56021295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com