I use
Clear["Global`*"];
(*=== 数据点 ===*)
bmiData = {17.53231562, 15.53238528, 17.26137457, 15.6088015,
15.20589021, 14.51292182, 14.75645275, 13.83305466, 15.69560048,
16.86902697, 18.0721242, 15.14403292, 17.44614778, 13.850415510,
18.32280354, 20.1631851, 18.59012493, 17.45527841, 18.58163511,
14.55731214, 15.40417624, 15.18792708, 15.62461325, 15.22882797,
15.02163115, 21.45492911, 20.89541272, 18.38647653, 29.34474265,
21.08535689, 22.02071132, 16.52876307, 18.38228254, 22.14403633,
19.2399927, 22.82261329, 26.10290031, 20.6759324, 22.34966855,
24.02533886};
fhlData = {1750, 2000, 2750, 2500, 2750, 2000, 2750, 1500, 2500, 2250,
3000, 1250, 2750, 1750, 2250, 1750, 2000, 2250, 2750, 2000, 1750,
2250, 2750, 2500, 2000, 2772, 3458, 2065, 4208, 4579, 5300, 2811,
2563, 4075, 3762, 4781, 3919, 4357, 3839, 3770};
(*=== 基函数 ===*)
basisfun[n_] := Table[x^i, {i, 0, n}]
(*=== 数据点点图 ===*)
bmiListPlt =
ListPlot[bmiData, PlotStyle -> {Red}, PlotMarkers -> "OpenMarkers"];
fhlListPlt =
ListPlot[fhlData, PlotStyle -> {Red}, PlotMarkers -> "OpenMarkers"];
(*=== 数据点拟合 ===*)
bmifit = Fit[bmiData, basisfun@7, x];
fhlfit = Fit[fhlData, basisfun@7, x];
(*=== 拟合图像 ===*)
bmiFitPlt = Plot[
bmifit,
{x, 0, Length@bmiData + 1},
PlotRange -> All
];
fhlFitPlt = Plot[
fhlfit,
{x, 0, Length@fhlData + 1},
PlotRange -> All
];
(*=== 多项式拟合拟合和点图 ===*)
bmishow = Show[bmiFitPlt, bmiListPlt]
fhlshow = Show[fhlFitPlt, fhlListPlt]
as you can see ,I had to go through the following steps
- get point data
- Constructing polynomial function
- Point plotted on a graph
- Do the fitting and draw the fitting curve
- Plot the fitting curve and data points on a graph
As you can see, I have two sets of data here, so I copy and paste a lot, in fact, I have many sets of data, so copying and pasting over and over again is obviously not a good idea.
Is there any way to package all of this into a function?
For example, if I input the data of the points and the number of fits I can draw the fit curve and the point graph directly
()
and;
. $\endgroup$