ThisCalculator

Everyday

Polynomial Regression Calculator: Fit Curves to Data

Updated · Standard formula, shown below

The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.

This polynomial regression calculator fits a polynomial curve of a chosen degree to your paired x-y data, showing the coefficients, the fitted equation, and a goodness-of-fit measure (R²). You enter your data points, pick the polynomial degree (1 for a line, 2 for a parabola, etc.), and the calculator returns the best-fit equation plus how well it explains the variation in your data.

Formula

Polynomial regression finds the coefficients a0, a1, …, ak that minimize the sum of squared residuals between the observed y-values and the predicted values from the polynomial:

ŷ = a0 + a1x + a2x² + … + akxk

where k is the degree, x and y are your data pairs, and ŷ is the predicted y. The coefficients are found by solving the normal equations (or using a QR decomposition for numerical stability). The R² value is:

R² = 1 − (SSres / SStot)

where SSres is the sum of squared residuals (yi − ŷi)² and SStot is the total sum of squares (yi − ȳ)². R² ranges from 0 to 1, with 1 meaning a perfect fit.

Worked Example

Using the default data: x = 1,2,3,4,5 and y = 2.1,3.9,8.2,15.1,26.0, with degree 2.

Step 1: Compute the necessary sums: n = 5, Σx = 15, Σx² = 55, Σx³ = 225, Σx⁴ = 979, Σy = 55.3, Σxy = 211.0, Σx²y = 869.4.

Step 2: Set up the normal equations for a quadratic (y = a + bx + cx²):

  • 5a + 15b + 55c = 55.3
  • 15a + 55b + 225c = 211.0
  • 55a + 225b + 979c = 869.4

Step 3: Solve (using elimination or matrix methods). The solution is approximately a = 1.05, b = −0.55, c = 1.02.

Step 4: The fitted equation is ŷ = 1.05 − 0.55x + 1.02x². For x = 1, ŷ = 1.05 − 0.55 + 1.02 = 1.52; residual = 2.1 − 1.52 = 0.58. Squared residuals sum to about 0.97. The total sum of squares (around mean ȳ = 11.06) is about 335.2. So R² = 1 − (0.97/335.2) = 0.9971.

The calculator shows the equation as 1.05 − 0.55x + 1.02x² and R² = 0.9971, matching our manual calculation.

When to Use Polynomial Regression

Use polynomial regression when your scatter plot shows a clear curve that a straight line cannot capture. Common applications include:

  • Modeling growth rates (e.g., bacterial growth, plant height over time)
  • Physics experiments (e.g., projectile motion, pendulum period vs. length)
  • Economics (e.g., cost curves, diminishing returns)
  • Engineering calibration curves (e.g., sensor output vs. measured value)

Degree 1 is simple linear regression. Degree 2 (quadratic) fits a parabola, degree 3 (cubic) fits an S-shaped curve, and so on. Choose the lowest degree that adequately models the trend without overfitting.

How to Choose the Right Degree

The degree is the highest power of x in the equation. Here’s how to decide:

  • Start with a scatter plot. If the points appear to follow a straight line, use degree 1.
  • If the points show one bend (like a U or an inverted U), try degree 2.
  • If there are two bends (like an S-curve), try degree 3.
  • Increase the degree gradually, but watch R². A higher degree always increases R² on the training data, but a degree that is too high (e.g., 7 with 10 points) will fit noise, not the underlying pattern.

A practical rule: keep the degree well below the number of data points. For n points, a polynomial of degree n−1 can pass through every point exactly, but that is rarely meaningful. Use cross-validation or a simpler model if the coefficients become unstable.

Common Mistakes and Pitfalls

  • Overfitting: Using too high a degree makes the model fit the noise. The curve may wiggle wildly between points.
  • Underfitting: Using degree 1 when the relationship is clearly curved gives a poor fit and low R².
  • Extrapolating: Polynomials can behave erratically outside the range of your data. A quadratic that fits well from x=0 to 10 may shoot up or down dramatically at x=20.
  • Ignoring units: If x is in meters and y in seconds, the coefficients have mixed units. That’s fine mathematically, but be careful when interpreting them physically.
  • Rounding too early: Keep full precision during calculations; round only the final coefficients to avoid large errors in predictions.

Limitations of Polynomial Regression

Polynomial regression is not always the best tool. It is sensitive to outliers, especially at the edges of the data range. High-degree polynomials can be numerically unstable, leading to large rounding errors. Also, the model assumes that the relationship is smooth and continuous; if your data has discontinuities or periodic patterns, other models (like splines or Fourier series) may be more appropriate. Finally, R² alone does not tell you if the model is correct—always plot the fitted curve against the data to visually check the fit.

Alternatives to Polynomial Regression

  • Linear regression: When the relationship is straight, it’s simpler and more interpretable.
  • Logarithmic or exponential regression: For data that grows or decays rapidly, transforming the variables often works better.
  • Power law regression: Fits y = a·xb, common in physics and biology.
  • Spline interpolation: Fits piecewise polynomials that join smoothly, useful for complex curves without a single global formula.
  • Moving average or smoothing: When you just want to see the trend without a parametric equation.

Each method has its strengths; polynomial regression is a good starting point for curved data, but always compare with other models if the fit is not satisfactory.

Frequently Asked Questions

What is the difference between polynomial regression and linear regression?

Linear regression fits a straight line (degree 1). Polynomial regression fits a curve of degree 2 or higher. Both are “linear” in the sense that they use linear algebra to find coefficients; the term “linear” in linear regression refers to the model being linear in the parameters, not the shape of the curve.

Can I use polynomial regression for time series data?

Yes, you can use time as the x-variable. However, time series often have autocorrelation and trends that may not be well captured by a simple polynomial. Consider specialized time-series methods if your data has seasonality or irregular patterns.

What does R² mean in polynomial regression?

R² (coefficient of determination) measures the proportion of variance in the y-variable that is explained by the model. An R² of 0.9 means 90% of the variation is explained. But a high R² does not guarantee a good model—it can be artificially high if you overfit.

How many data points do I need for polynomial regression?

At a minimum, you need at least (degree + 1) points to get a unique fit. For a quadratic, that’s 3 points. But for a reliable model, you should have many more points than parameters—typically at least 5–10 times the number of coefficients.

Why does my calculator show a different equation than Excel?

Different software may use different algorithms (e.g., QR decomposition vs. normal equations) and different rounding. Also, Excel’s trendline may display fewer digits. The underlying fit should be the same if the data and degree match.

Can I predict y for an x outside my data range?

Technically yes, but it’s risky. Polynomials can diverge quickly outside the observed range. Use extrapolation only with caution and clearly state that the prediction is outside the data’s support.

Related

Formula used
in x | X values | - | 1,2,3,4,5 | 0
in y | Y values | - | 2.1,3.9,8.2,15.1,26.0 | 0
in deg | Polynomial degree | - | 2 | 1
out eq | Fitted equation | - | polyfit(x,y,deg) | 3
out r2 | R-squared | - | polyfit_r2(x,y,deg) | 4

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.