# How to find the best quadratic fit for a set of data?

I'm trying to find a degree-2 polynomial fit with x-intercepts of x=0 and x=1 for the below set of data:

  {{0., 3.6057449978954007*^-19}, {0.01, 1.1296765532927386},
{0.02, 1.1296765534056958}, {0.03, 1.1296765534339293},
{0.04, 1.1296765534464732}, {0.05, 1.1296765534535258},
{0.06, 1.1296765534580366}, {0.07, 1.1296765534611668},
{0.08, 1.1296765534634645}, {0.09, 1.1296765534652222},
{0.1, 1.1296765534666093}, {0.11, 1.1296765534677318},
{0.12, 1.1296765534686581}, {0.13, 1.1296765534694355},
{0.14, 1.129676553470097}, {0.15, 1.1296765534706663},
{0.16, 1.1296765534711617}, {0.17, 1.1296765534715962},
{0.18, 1.1296765534719804}, {0.19, 1.1296765534723223},
{0.2, 1.1296765534726287}, {0.21, 1.1296765534729047},
{0.22, 1.1296765534731543}, {0.23, 1.1296765534733813},
{0.24, 1.1296765534735882}, {0.25, 1.1296765534737778},
{0.26, 1.1296765534739521}, {0.27, 1.1296765534741127},
{0.28, 1.129676553474261}, {0.29, 1.1296765534743984},
{0.3, 1.129676553474526}, {0.31, 1.1296765534746442},
{0.32, 1.1296765534747546}, {0.33, 1.1296765534748576},
{0.34, 1.1296765534749535}, {0.35000000000000003, 1.1296765534750435},
{0.36, 1.1296765534751274}, {0.37, 1.129676553475206},
{0.38, 1.1296765534752793}, {0.39, 1.1296765534753481},
{0.4, 1.1296765534754123}, {0.41000000000000003, 1.1296765534754722},
{0.42, 1.1296765534755284}, {0.43, 1.1296765534755804},
{0.44, 1.1296765534756288}, {0.45, 1.1296765534756736},
{0.46, 1.1296765534757152}, {0.47000000000000003, 1.1296765534757531},
{0.48, 1.1296765534757878}, {0.49, 1.1296765534758193},
{0.5, 1.1296765534758475}, {0.51, 1.1296765534758726},
{0.52, 1.1296765534758941}, {0.53, 1.1296765534759126},
{0.54, 1.1296765534759279}, {0.55, 1.1296765534759394},
{0.56, 1.1296765534759476}, {0.5700000000000001, 1.1296765534759523},
{0.58, 1.1296765534759532}, {0.59, 1.12967655347595},
{0.6, 1.1296765534759432}, {0.61, 1.1296765534759319},
{0.62, 1.1296765534759161}, {0.63, 1.1296765534758957},
{0.64, 1.1296765534758701}, {0.65, 1.1296765534758395},
{0.66, 1.1296765534758029}, {0.67, 1.12967655347576},
{0.68, 1.1296765534757105}, {0.6900000000000001, 1.1296765534756539},
{0.7000000000000001, 1.1296765534755893}, {0.71, 1.129676553475516},
{0.72, 1.1296765534754334}, {0.73, 1.1296765534753404},
{0.74, 1.1296765534752358}, {0.75, 1.1296765534751185},
{0.76, 1.129676553474987}, {0.77, 1.1296765534748396},
{0.78, 1.129676553474674}, {0.79, 1.1296765534744877},
{0.8, 1.1296765534742783}, {0.81, 1.1296765534740418},
{0.8200000000000001, 1.1296765534737743},
{0.8300000000000001, 1.12967655347347}, {0.84, 1.1296765534731232},
{0.85, 1.129676553472725}, {0.86, 1.1296765534722655},
{0.87, 1.129676553471731}, {0.88, 1.1296765534711035},
{0.89, 1.1296765534703592}, {0.9, 1.129676553469465},
{0.91, 1.1296765534683741}, {0.92, 1.1296765534670172},
{0.93, 1.1296765534652888}, {0.9400000000000001, 1.1296765534630195},
{0.9500000000000001, 1.1296765534599165}, {0.96, 1.1296765534554318},
{0.97, 1.1296765534484046}, {0.98, 1.1296765534358846}}


I have tried using the Fit function, specifying a first-degree and second-degree term, but no constant term, as it would be desirable for the y-intercept to be zero (the function passes through the origin). I have also tried using BSplineFunction, but due to the nature of the data (which is essentially a straight horizontal line when plotted), I don't think that would very strongly resemble a degree-2 polynomial.

• So what is the problem with Fit[data, {x, x^2}, x]? Also, when you plot the data with ListPlot[data], it doesn't look like a quadratic dependency at all. There is basically no y dependency. Jul 16, 2019 at 17:20
• You can write: Fit[data, x (1 - x), x] Jul 16, 2019 at 20:27

I'm not sure if I understood your question correctly, but this may be what you want:

LinearModelFit[data, {x (1 - x)}, x, IncludeConstantBasis -> False]


FittedModel[5.64463(1-x)x]

This quadratic curve intersects the $$x$$-axis at $$x=0$$ and $$x=1$$, as desired. But it's a terrible fit.

• That's exactly what I was looking for, thank you. Yeah, the data absolutely does not cater to a degree-2 polynomial, but that's the form I was looking for. I also had no idea LinearModelFit had the option built-in to omit a constant term. I'll definitely be using that instead of manually specifying terms with Fit. Thanks!
– STDK
Jul 16, 2019 at 17:24

This an extended comment rather than an answer.

Why would you even attempt to fit a quadratic to your data? Dropping the first point (which is completely isolated from the rest of the data) results in the following:

data = data[[Range[2, Length[data]]]];
ListPlot[data, Frame -> True, PlotRangePadding -> 0.00000000005,
PlotRange -> {{0, 1}, {Min[data[[All, 2]]], Max[data[[All, 2]]]}}]


A first approximation might be the following:

data = Rationalize[data[[Range[2, Length[data]]]], 0];
{ymin, ymax} = MinMax[data[[All, 2]]];
nlm = NonlinearModelFit[data, (b x^c (1 - x)^d), {b, c, d}, x, MaxIterations -> 5000];

Show[ListPlot[data, PlotRangePadding -> 0.00000000005, Frame -> True,
PlotRange -> {{0, 1}, {ymin, ymax}}],
Plot[nlm[x], {x, 0, 1}, PlotRange -> All]]


This gives your a predictive function with nlm[0] and nlm[1] resulting in 0.