Polynomial Regression Calculator
Fit a polynomial of any degree from 1 to 6 to your data using least squares and get all coefficients, R², and predictions.
📖 What is Polynomial Regression?
Polynomial regression is a powerful form of regression analysis that fits a polynomial of degree n - y = a₀ + a₁x + a₂x² + ··· + aₙxⁿ - to observed data. It is a generalisation of linear regression: degree 1 gives a straight line, degree 2 a parabola, degree 3 a cubic, and so on up to degree 6 in this calculator.
The key insight is that although the model is non-linear in x (it involves powers of x), it is still linear in the coefficients a₀, a₁, ..., aₙ. This means that the same ordinary least-squares principle applies: minimise the sum of squared residuals with respect to the coefficients, which yields a system of linear equations - the normal equations - that can be solved exactly using Gaussian elimination.
Polynomial regression underpins a huge variety of real-world applications. Analytical chemists use degree-3 to degree-5 polynomials for spectrophotometer calibration curves. Meteorologists fit degree-4 polynomials to hourly temperature profiles over 24 hours. Economists use cubic polynomials for cost functions. Signal processing engineers use high-degree polynomial baselines for background subtraction in spectroscopy. Epidemiologists use polynomial curves to model the shape of infection waves.
This calculator handles degrees 1 through 6, implementing the Vandermonde normal equations solved by Gaussian elimination with partial pivoting. Results include all coefficients, R², adjusted R² (which penalises for extra parameters), and a prediction field for evaluating the fitted polynomial at any X.
A critical principle: always prefer the simplest adequate model. Higher degree = more flexibility but higher risk of overfitting. Start with degree 1 or 2, check residuals, and increase degree only when there is clear systematic curvature remaining in the residuals.
📐 Formulas
Normal equations (Vandermonde system, (n+1) × (n+1)):
The matrix A has entries A[i][j] = Σₖ xₖ^(i+j), for i, j = 0, 1, ..., n.
The right-hand side vector b has entries b[i] = Σₖ yₖ · xₖⁱ.
Solve A·coeff = b for [a₀, a₁, ..., aₙ] using Gaussian elimination with partial pivoting.
R²: R² = 1 − SS_res / SS_tot, where SS_res = Σ(yᵢ − ŷᵢ)², SS_tot = Σ(yᵢ − ȳ)²
Adjusted R²: R²_adj = 1 − (1 − R²) · (n − 1) / (n − p − 1), where p = degree
Prediction: ŷ = a₀ + a₁x + a₂x² + ··· + aₙxⁿ evaluated at any chosen X using Horner's method for numerical efficiency.
📖 How to Use This Calculator
📝 Example Calculations
Example 1 - Degree 3: Economic Cubic Cost Function
Output Q: 1, 2, 3, 4, 5, 6, 7, 8. Total Cost TC (£000): 8, 11, 13, 17, 25, 40, 65, 104.
Degree 3 fit: a₀ ≈ 10.36, a₁ ≈ −4.14, a₂ ≈ 1.71, a₃ ≈ 0.286. R² ≈ 0.9993. Adj. R² ≈ 0.9988.
Equation: ŷ = 10.36 − 4.14x + 1.71x² + 0.286x³.
Prediction at Q = 9: ŷ = 10.36 − 4.14(9) + 1.71(81) + 0.286(729) ≈ £157,000.
Example 2 - Degree 4: Hourly Temperature Profile
Hour X: 0, 3, 6, 9, 12, 15, 18, 21, 24. Temp °C: 12, 10, 9, 16, 26, 29, 25, 18, 13.
Degree 4 fit: a₀ ≈ 11.55, a₁ ≈ −1.762, a₂ ≈ 0.607, a₃ ≈ −0.0556, a₄ ≈ 0.001538. R² ≈ 0.991. Adj. R² ≈ 0.982.
The degree-4 model captures the morning dip, afternoon peak, and evening decline accurately. Degree 2 (R² ≈ 0.852) misses the morning cooling - the extra terms are justified.
Prediction at hour 14: ŷ ≈ 28.4°C - matching the expected mid-afternoon maximum.
Example 3 - Degree 2 vs Degree 3: When Higher Degree is Not Needed
X: 0, 1, 2, 3, 4, 5, 6. Y: 1, 5, 11, 19, 29, 41, 55.
Degree 2: a₀ ≈ 1.00, a₁ ≈ 2.00, a₂ ≈ 2.00. R² = 1.000. Adj. R² = 1.000.
Degree 3: a₀ ≈ 1.00, a₁ ≈ 2.00, a₂ ≈ 2.00, a₃ ≈ ~0. R² = 1.000. Adj. R² stays near 1.000 but the cubic coefficient is essentially zero.
Conclusion: the data follows an exact quadratic (y = 2x² + 2x + 1). Adding a cubic term explains nothing extra. Prefer degree 2 for this data.
Example 4 - Degree 5: Calibration Curve in Analytical Chemistry
Concentration (μg/mL): 0, 2, 4, 6, 8, 10, 12, 14, 16. Absorbance: 0.000, 0.082, 0.197, 0.348, 0.496, 0.621, 0.718, 0.781, 0.820.
The curve flattens at high concentrations (detector saturation). Degree 1 R² ≈ 0.949 - poor. Degree 2 R² ≈ 0.9987. Degree 3 R² ≈ 0.9996. Degree 5 R² ≈ 0.99999 - excellent.
For a required absorbance of 0.650, solving the degree-3 equation gives concentration ≈ 10.8 μg/mL - more accurate than the linear calibration (would predict 10.3 μg/mL).
Example 5 - Demonstrating Overfitting (Degree 6 on 8 Points)
X: 1, 2, 3, 4, 5, 6, 7, 8. Y (noisy data): 3, 10, 18, 22, 21, 15, 8, 2.
Degree 2: R² ≈ 0.982, Adj. R² ≈ 0.975. Clean inverted-parabola fit.
Degree 3: R² ≈ 0.983, Adj. R² ≈ 0.970 - only marginal improvement; cubic coefficient near zero.
Degree 6: R² ≈ 0.9998, Adj. R² ≈ 0.9987 - curve passes nearly through all 8 points but oscillates wildly at x = 0 and x = 9. A prediction at x = 9 from degree 6 could be wildly inaccurate despite the near-perfect R².
Lesson: For this data, degree 2 is the correct model. Degree 6 is an overfit that memorises the noise in the 8 training points.