Построение полиномиальных моделей¶
В данном примере с помощью специализированной библиотеки Polynomials будет продемонстрировано построение полиномов различных порядков и их визуализация.
Установка библиотеки Polynomials, необходимых для построения полиномов:
In [ ]:
Pkg.add("Polynomials")
Запуск библиотеки Polynomials:
In [ ]:
using Polynomials
Формирование набора данных, по которым будут строиться полиномы:
In [ ]:
xs = collect(0:20); ys = @. exp(-xs) + sin(xs);
Построение полиномиальных моделей:
In [ ]:
pol1 = fit(xs,ys,1)
pol5 = fit(xs,ys,5)
pol9 = fit(xs,ys,9)
pol20 = fit(xs,ys,20)
println(pol1)
0.411552 - 0.0288685*x
Запуск библиотеки для построения графиков:
In [ ]:
using Plots
Построение графиков получившихся полиномов:
In [ ]:
scatter(xs,ys,label="исходные данные")
plot!(pol1, extrema(xs)..., label="полином 1-й степени")
plot!(pol5, extrema(xs)..., label="полином 5-й степени")
plot!(pol9, extrema(xs)..., label="полином 9-й степени")
plot!(pol20, extrema(xs)..., label="полином 20-й степени")
Out[0]:
Вывод:¶
В данном примере было продемонстрировано построение полиномов и их визуализация. По графику можно оценить насколько сильно степень полинома влияет на его подгонку к исходному набору данных.
{"id": "f4426d6f-9f83-401d-ab23-a57fd8843716", "data": [{"showlegend": true, "mode": "markers", "xaxis": "x", "colorbar": {"title": {"text": ""}}, "name": "исходные данные", "zmin": null, "yaxis": "y", "legendgroup": "исходные данные", "marker": {"symbol": "circle", "color": "rgba(0, 154, 250, 1.000)", "line": {"color": "rgba(0, 0, 0, 1)", "width": 1}, "size": 8}, "zmax": null, "y": [1, 1.2093504259793388, 1.0446327100622943, 0.19090707642773116, -0.738486856419194, -0.952186327664053, -0.2769367460222595, 0.6578984806843435, 0.9896937092512843, 0.4122418950458433, -0.5439757109596073, -0.9999735048499132, -0.5365667737880816, 0.4201692971560479, 0.9906081872235895, 0.6502881460594373, -0.28790320412989057, -0.9613974504801797, -0.7509872315416964, 0.1498772152657488, 0.9129452527887812], "type": "scatter", "x": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 21, "currentCount": 21}}}, {"showlegend": true, "mode": "lines", "xaxis": "x", "colorbar": {"title": {"text": ""}}, "name": "полином 1-й степени", "zmin": null, "yaxis": "y", "legendgroup": "полином 1-й степени", "zmax": null, "line": {"color": "rgba(227, 111, 71, 1.000)", "shape": "linear", "dash": "solid", "width": 1}, "y": [0.4115518916182629, 0.40577818577645913, 0.4000044799346554, 0.39423077409285157, 0.3884570682510478, 0.38268336240924405, 0.3769096565674403, 0.37113595072563654, 0.36536224488383273, 0.359588539042029, 0.3538148332002252, 0.34804112735842146, 0.3422674215166177, 0.3364937156748139, 0.33072000983301014, 0.3249463039912064, 0.31917259814940263, 0.3133988923075989, 0.30762518646579506, 0.3018514806239913, 0.29607777478218755, 0.2903040689403838, 0.28453036309858, 0.2787566572567763, 0.2729829514149725, 0.2672092455731687, 0.26143553973136496, 0.25566183388956115, 0.24988812804775742, 0.24411442220595367, 0.23834071636414988, 0.2325670105223461, 0.22679330468054235, 0.2210195988387386, 0.21524589299693483, 0.20947218715513105, 0.20369848131332727, 0.1979247754715235, 0.19215106962971976, 0.186377363787916, 0.18060365794611222, 0.17482995210430846, 0.16905624626250468, 0.16328254042070092, 0.15750883457889714, 0.15173512873709338, 0.14596142289528963, 0.14018771705348584, 0.1344140112116821, 0.1286403053698783, 0.12286659952807455, 0.11709289368627081, 0.11131918784446701, 0.10554548200266325, 0.09977177616085946, 0.09399807031905572, 0.08822436447725197, 0.08245065863544818, 0.07667695279364442, 0.07090324695184062, 0.06512954111003688, 0.05935583526823313, 0.053582129426429335, 0.047808423584625594, 0.042034717742821796, 0.03626101190101805, 0.030487306059214302, 0.024713600217410505, 0.018939894375606756, 0.01316618853380296, 0.007392482691999214, 0.0016187768501954674, -0.00415492899160833, -0.009928634833412076, -0.015702340675215875, -0.02147604651701962, -0.027249752358823365, -0.033023458200627166, -0.03879716404243091, -0.044570869884234705, -0.050344575726038454, -0.0561182815678422, -0.061891987409645945, -0.06766569325144979, -0.07343939909325355, -0.07921310493505729, -0.08498681077686103, -0.09076051661866479, -0.09653422246046862, -0.10230792830227238, -0.10808163414407612, -0.11385533998587986, -0.11962904582768362, -0.12540275166948747, -0.1311764575112912, -0.13695016335309496, -0.1427238691948987, -0.14849757503670244, -0.1542712808785063, -0.16004498672031003, -0.1658186925621138], "type": "scatter", "x": [0, 0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2, 2.2, 2.4, 2.6, 2.8, 3, 3.2, 3.4, 3.6, 3.8, 4, 4.2, 4.4, 4.6, 4.8, 5, 5.2, 5.4, 5.6, 5.8, 6, 6.2, 6.4, 6.6, 6.8, 7, 7.2, 7.4, 7.6, 7.8, 8, 8.2, 8.4, 8.6, 8.8, 9, 9.2, 9.4, 9.6, 9.8, 10, 10.2, 10.4, 10.6, 10.8, 11, 11.2, 11.4, 11.6, 11.8, 12, 12.2, 12.4, 12.6, 12.8, 13, 13.2, 13.4, 13.6, 13.8, 14, 14.2, 14.4, 14.6, 14.8, 15, 15.2, 15.4, 15.6, 15.8, 16, 16.2, 16.4, 16.6, 16.8, 17, 17.2, 17.4, 17.6, 17.8, 18, 18.2, 18.4, 18.6, 18.8, 19, 19.2, 19.4, 19.6, 19.8, 20], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 101, "currentCount": 101}}}, {"showlegend": true, "mode": "lines", "xaxis": "x", "colorbar": {"title": {"text": ""}}, "name": "полином 5-й степени", "zmin": null, "yaxis": "y", "legendgroup": "полином 5-й степени", "zmax": null, "line": {"color": "rgba(62, 164, 78, 1.000)", "shape": "linear", "dash": "solid", "width": 1}, "y": [1.2269146744967887, 1.1773809962538015, 1.1218990499397394, 1.061464475173689, 0.9970121870752386, 0.9294178522034211, 0.8594993644956553, 0.7880183212066888, 0.7156814988475398, 0.6431423291244394, 0.5710023748777736, 0.4998128060210255, 0.4300758754797182, 0.3622463951303554, 0.2967332117393653, 0.23390068290204152, 0.17407015298148606, 0.1175214290475514, 0.06449425681578136, 0.01518979658635613, -0.030227900816969244, -0.0716304181079189, -0.10892349559875748, -0.14204555526134527, -0.1709662247881975, -0.1956848616535415, -0.21622907717437523, -0.23265326057152363, -0.24503710303069895, -0.2534841217635556, -0.25812018406875037, -0.2590920313929987, -0.25656580339213425, -0.2507255619921645, -0.24177181545032944, -0.22992004241616093, -0.21539921599253864, -0.19845032779674768, -0.17932491202153839, -0.15828356949618314, -0.13559449174753224, -0.11153198506107528, -0.0863749945419955, -0.06040562817623201, -0.033907680891532586, -0.00716515861851591, 0.019539197648277877, 0.04592538778931567, 0.0717176284990313, 0.09664582922474943, 0.12044706810564598, 0.14286706791166928, 0.16366167198250856, 0.18259832016651265, 0.19945752475964004, 0.21403434644440594, 0.2261398702288186, 0.2356026813853267, 0.24227034138975687, 0.24601086386025367, 0.24671419049622945, 0.24429366701730676, 0.2386875191022414, 0.2298603283278938, 0.21780450810815324, 0.2025417796328863, 0.18412464780686957, 0.1626378771887383, 0.13819996792993178, 0.1109646317136424, 0.08112226769373757, 0.04890143843371439, 0.014570345845632918, -0.02156169287092127, -0.059142769289922355, -0.0977769078199412, -0.1370225897651926, -0.17639127738664456, -0.21534593796301216, -0.25329956785187197, -0.2896137165506685, -0.3235970107578258, -0.3545036784337293, -0.3815320728618842, -0.4038231967098785, -0.42045922609050024, -0.4304620346228005, -0.43279171749306006, -0.42634511551599574, -0.4099543391956692, -0.3823852927866561, -0.34233619835505713, -0.2884361198395468, -0.21924348711242891, -0.13324462004073476, -0.02885225254727089, 0.09559594332837604, 0.24183883336874198, 0.4116932971173908, 0.6070557038179439, 0.8299033883529169], "type": "scatter", "x": [0, 0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2, 2.2, 2.4, 2.6, 2.8, 3, 3.2, 3.4, 3.6, 3.8, 4, 4.2, 4.4, 4.6, 4.8, 5, 5.2, 5.4, 5.6, 5.8, 6, 6.2, 6.4, 6.6, 6.8, 7, 7.2, 7.4, 7.6, 7.8, 8, 8.2, 8.4, 8.6, 8.8, 9, 9.2, 9.4, 9.6, 9.8, 10, 10.2, 10.4, 10.6, 10.8, 11, 11.2, 11.4, 11.6, 11.8, 12, 12.2, 12.4, 12.6, 12.8, 13, 13.2, 13.4, 13.6, 13.8, 14, 14.2, 14.4, 14.6, 14.8, 15, 15.2, 15.4, 15.6, 15.8, 16, 16.2, 16.4, 16.6, 16.8, 17, 17.2, 17.4, 17.6, 17.8, 18, 18.2, 18.4, 18.6, 18.8, 19, 19.2, 19.4, 19.6, 19.8, 20], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 101, "currentCount": 101}}}, {"showlegend": true, "mode": "lines", "xaxis": "x", "colorbar": {"title": {"text": ""}}, "name": "полином 9-й степени", "zmin": null, "yaxis": "y", "legendgroup": "полином 9-й степени", "zmax": null, "line": {"color": "rgba(195, 113, 210, 1.000)", "shape": "linear", "dash": "solid", "width": 1}, "y": [0.9769855934687885, 0.9339014059004563, 0.989794401475228, 1.0905515034501319, 1.1970097862516258, 1.2823279684304718, 1.3296702824044722, 1.3301779540583492, 1.2812047258858732, 1.1847940307459792, 1.0463765694620464, 0.8736681644218139, 0.6757488530344159, 0.46230524937096307, 0.2430192395557738, 0.027087086486823786, -0.17714599775362824, -0.36244979788416903, -0.5228846175671624, -0.6539005343766779, -0.7523716703931187, -0.816575914589797, -0.8461297137696999, -0.8418867566323051, -0.805808610601888, -0.7408146333291048, -0.650617770286602, -0.5395521666192833, -0.41239786537637524, -0.27420723545106507, -0.13013717097843136, 0.014709470401362112, 0.1554302701225148, 0.2874550917112628, 0.4066614564927547, 0.5094720096662155, 0.5929330726265475, 0.6547736883801102, 0.6934449506408953, 0.7081397637071633, 0.698793509495564, 0.6660664001710566, 0.6113085696224672, 0.536509204640284, 0.44423123700526596, 0.3375333108498197, 0.21988090553742853, 0.09504863300289126, -0.032984160054512844, -0.1601422705473096, -0.28236572631841783, -0.39572078107931136, -0.4965085130604204, -0.5813687482011499, -0.6473770806095183, -0.6921328421887539, -0.7138359797103679, -0.7113509312373641, -0.6842557546230065, -0.6328749489020226, -0.5582946246899165, -0.46235892222334585, -0.3476468454754132, -0.21742897768849528, -0.07560386799799029, 0.07338576983350237, 0.22465652685213167, 0.3730065107621834, 0.5130549416137024, 0.6393973119547399, 0.7467738048846391, 0.8302481483178741, 0.8853935420293024, 0.9084817243114386, 0.8966706490899038, 0.848185620260669, 0.7624880790923887, 0.6404255624112973, 0.4843556438593055, 0.2982359378900057, 0.08767148588046747, -0.14008994240536868, -0.37622491444592054, -0.6104748904638131, -0.8313722612861605, -1.0265647563297442, -1.1832520335410461, -1.288749212862575, -1.3311930930359355, -1.3004077962057348, -1.1889476169845155, -0.9933359125903762, -0.715519955975078, -0.3645627897091315, 0.04140574275744418, 0.47295544144146434, 0.8868151289062458, 1.2229991990587217, 1.4015743216999668, 1.3190380108131365, 0.844279460931123], "type": "scatter", "x": [0, 0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2, 2.2, 2.4, 2.6, 2.8, 3, 3.2, 3.4, 3.6, 3.8, 4, 4.2, 4.4, 4.6, 4.8, 5, 5.2, 5.4, 5.6, 5.8, 6, 6.2, 6.4, 6.6, 6.8, 7, 7.2, 7.4, 7.6, 7.8, 8, 8.2, 8.4, 8.6, 8.8, 9, 9.2, 9.4, 9.6, 9.8, 10, 10.2, 10.4, 10.6, 10.8, 11, 11.2, 11.4, 11.6, 11.8, 12, 12.2, 12.4, 12.6, 12.8, 13, 13.2, 13.4, 13.6, 13.8, 14, 14.2, 14.4, 14.6, 14.8, 15, 15.2, 15.4, 15.6, 15.8, 16, 16.2, 16.4, 16.6, 16.8, 17, 17.2, 17.4, 17.6, 17.8, 18, 18.2, 18.4, 18.6, 18.8, 19, 19.2, 19.4, 19.6, 19.8, 20], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 101, "currentCount": 101}}}, {"showlegend": true, "mode": "lines", "xaxis": "x", "colorbar": {"title": {"text": ""}}, "name": "полином 20-й степени", "zmin": null, "yaxis": "y", "legendgroup": "полином 20-й степени", "zmax": null, "line": {"color": "rgba(172, 142, 24, 1.000)", "shape": "linear", "dash": "solid", "width": 1}, "y": [1, 1.0192808682401158, 1.0613545312103745, 1.1143502124260691, 1.1670035037434807, 1.2093504259793388, 1.2331180129964383, 1.2319300214552995, 1.2013954783851735, 1.1391163882943303, 1.0446327100622943, 0.919313157829248, 0.7661962939793706, 0.5897855632715883, 0.3958028736449083, 0.19090707642773164, -0.017614393094392843, -0.22217076971950017, -0.41519892162709277, -0.5894881502718873, -0.738486856419202, -0.8565795823503509, -0.9393239562156069, -0.9836385518351725, -0.9879345571319711, -0.9521863276641044, -0.8779382921832163, -0.7682481743398157, -0.6275689967207242, -0.4615747400558993, -0.27693674602198215, -0.0810598888086844, 0.11821087828216856, 0.312901832418411, 0.49522718087212353, 0.6578984806849757, 0.7944144067732759, 0.8993192863235148, 0.9684200671726856, 0.9989530486435014, 0.9896937092443179, 0.9410052371899369, 0.8548238160666458, 0.7345812418609632, 0.5850679483184618, 0.4122418950300027, 0.22299093298273623, 0.024858117351534627, -0.17425908392320705, -0.3664236968088514, -0.5439757110321537, -0.6998374985275417, -0.8277960061788029, -0.9227504747351274, -0.980915811430443, -0.9999735053595774, -0.9791640761268797, -0.9193173654745554, -0.822819466592791, -0.6935176052638725, -0.5365667750503331, -0.35822422591200026, -0.16560001030174415, 0.033626470444986144, 0.2315126202957504, 0.42016929381197443, 0.5920753161967062, 0.740377316926289, 0.859162961208196, 0.9436966132248126, 0.9906081814767369, 0.9980274172572937, 0.9656584895507627, 0.8947918154044385, 0.7882525672290885, 0.6502881282657393, 0.48639870264863183, 0.30311810215844276, 0.10775323593297334, -0.09190717120386423, -0.28790322835646176, -0.47242125010432706, -0.6381052383098104, -0.7783502011680687, -0.8875654724685458, -0.9613975953242249, -0.996903074319747, -0.9926660633397865, -0.9488538069501306, -0.8672104211899394, -0.7509874115028412, -0.6048154044861944, -0.43452277301030096, -0.24690766432158848, -0.04947157844240433, 0.14987697197963334, 0.3431408751791365, 0.5225838234317167, 0.6811070853050879, 0.8126919956209007, 0.9129442474119067], "type": "scatter", "x": [0, 0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2, 2.2, 2.4, 2.6, 2.8, 3, 3.2, 3.4, 3.6, 3.8, 4, 4.2, 4.4, 4.6, 4.8, 5, 5.2, 5.4, 5.6, 5.8, 6, 6.2, 6.4, 6.6, 6.8, 7, 7.2, 7.4, 7.6, 7.8, 8, 8.2, 8.4, 8.6, 8.8, 9, 9.2, 9.4, 9.6, 9.8, 10, 10.2, 10.4, 10.6, 10.8, 11, 11.2, 11.4, 11.6, 11.8, 12, 12.2, 12.4, 12.6, 12.8, 13, 13.2, 13.4, 13.6, 13.8, 14, 14.2, 14.4, 14.6, 14.8, 15, 15.2, 15.4, 15.6, 15.8, 16, 16.2, 16.4, 16.6, 16.8, 17, 17.2, 17.4, 17.6, 17.8, 18, 18.2, 18.4, 18.6, 18.8, 19, 19.2, 19.4, 19.6, 19.8, 20], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 101, "currentCount": 101}}}], "config": {"showlegend": true, "xaxis": {"showticklabels": true, "gridwidth": 0.5, "range": [-0.6000000000000014, 20.6], "domain": [0.0658209390492855, 0.9934383202099738], "mirror": false, "tickangle": 0, "showline": true, "zeroline": false, "tickfont": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "zerolinecolor": "rgba(0, 0, 0, 1)", "anchor": "y", "visible": true, "ticks": "inside", "tickmode": "array", "linecolor": "rgba(0, 0, 0, 1)", "showgrid": true, "title": {"text": "", "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}}, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickcolor": "rgb(0, 0, 0)", "type": "linear"}, "paper_bgcolor": "rgba(255, 255, 255, 1.000)", "annotations": [], "height": 400, "margin": {"l": 0, "b": 20, "r": 0, "t": 20}, "plot_bgcolor": "rgba(255, 255, 255, 1.000)", "yaxis": {"showticklabels": true, "gridwidth": 0.5, "range": [-1.413176115478013, 1.483557344142044], "domain": [0.03762029746281716, 0.9901574803149606], "mirror": false, "tickangle": 0, "showline": true, "zeroline": false, "tickfont": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "zerolinecolor": "rgba(0, 0, 0, 1)", "anchor": "x", "visible": true, "ticks": "inside", "tickmode": "array", "linecolor": "rgba(0, 0, 0, 1)", "showgrid": true, "title": {"text": "", "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}}, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickcolor": "rgb(0, 0, 0)", "type": "linear"}, "legend": {"yanchor": "auto", "xanchor": "auto", "bordercolor": "rgba(0, 0, 0, 1)", "bgcolor": "rgba(255, 255, 255, 1.000)", "borderwidth": 1, "tracegroupgap": 0, "y": 1, "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "title": {"font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}, "text": ""}, "traceorder": "normal", "x": 1}, "width": 875.484375}}