Cubic Regression Calculator
Fit a cubic curve to your data using least squares and get all four coefficients, R², and predictions instantly.
📖 What is Cubic Regression?
Cubic regression fits a third-degree polynomial - y = ax³ + bx² + cx + d - to a set of data points using the method of least squares. It extends quadratic regression by adding a cubic term, allowing the fitted curve to have up to two turning points and an inflection point where the curvature changes sign.
Cubic polynomials are remarkably versatile. They can model S-curves (slow start, rapid middle growth, then levelling off), data with a local peak followed by a trough, and non-symmetric bell-shaped relationships. In engineering, stress-strain curves for ductile materials follow a roughly cubic shape: linear elastic region, yield point, plastic hardening, then failure. In environmental science, pollutant concentration in a river over distance sometimes follows a cubic pattern. In economics, total cost functions are often modelled as cubic polynomials to capture economies of scale and eventually rising marginal costs.
The four coefficients a, b, c, d are determined by solving a 4×4 system of normal equations derived from the least-squares criterion. This calculator uses Gaussian elimination with partial pivoting - a numerically stable direct solver - to find the solution without requiring matrix inversion.
Compared to higher-degree polynomials, the cubic is often a practical sweet spot: complex enough to capture non-linear patterns with one or two turning points, but simple enough to avoid excessive oscillation and overfitting that plague degree-5 or degree-6 models.
📐 Formulas
The normal equations (4×4 system) are derived by setting ∂SS/∂d = ∂SS/∂c = ∂SS/∂b = ∂SS/∂a = 0:
d·n + c·Σx + b·Σx² + a·Σx³ = Σy
d·Σx + c·Σx² + b·Σx³ + a·Σx⁴ = Σxy
d·Σx² + c·Σx³ + b·Σx⁴ + a·Σx⁵ = Σx²y
d·Σx³ + c·Σx⁴ + b·Σx⁵ + a·Σx⁶ = Σx³y
Solved by Gaussian elimination with partial pivoting for [d, c, b, a].
R²: R² = 1 − Σ(yᵢ − ŷᵢ)² / Σ(yᵢ − ȳ)²
Inflection point: x_inf = −b / (3a) where the curve changes concavity.
Turning points: solve dy/dx = 3ax² + 2bx + c = 0 using the quadratic formula. Real solutions exist when discriminant (2b)² − 4(3a)(c) ≥ 0.
📖 How to Use This Calculator
📝 Example Calculations
Example 1 - Temperature Variation Over 24 Hours
Hour (X): 0, 4, 8, 12, 16, 20, 24. Temperature °C (Y): 14, 11, 15, 25, 28, 22, 15.
Fit yields a ≈ 0.0062, b ≈ −0.210, c ≈ 1.882, d ≈ 12.60. R² ≈ 0.982.
Equation: ŷ = 0.0062x³ − 0.210x² + 1.882x + 12.60.
Prediction at hour 14 (early afternoon): ŷ = 0.0062(2744) − 0.210(196) + 1.882(14) + 12.60 ≈ 27.1°C.
Example 2 - Stress-Strain Curve
Strain ε (×10⁻³): 0, 1, 2, 3, 4, 5, 6. Stress σ (MPa): 0, 200, 370, 480, 520, 490, 420.
Fit: a ≈ −4.286, b ≈ 40.357, c ≈ 148.71, d ≈ 1.43. R² ≈ 0.994.
Inflection at ε = −40.357/(3×−4.286) ≈ 3.14 × 10⁻³ - the approximate yield strain.
Peak stress predicted at ε ≈ 4.2 × 10⁻³: ŷ ≈ 524 MPa, close to the observed maximum.
Example 3 - S-Curve Technology Adoption
Year (X, 0=2015): 0, 1, 2, 3, 4, 5, 6, 7. Adoption % (Y): 2, 5, 12, 28, 52, 72, 84, 90.
Fit: a ≈ −0.637, b ≈ 7.310, c ≈ −7.167, d ≈ 2.095. R² ≈ 0.993.
Inflection at x = −7.310/(3×−0.637) ≈ 3.83 years after 2015 (mid-2018) - the point of fastest adoption growth.
At year 8 (2023): ŷ = −0.637(512) + 7.310(64) − 7.167(8) + 2.095 ≈ 92% adoption predicted.
Example 4 - Total Cost Function in Economics
Output Q (units): 1, 2, 3, 4, 5, 6, 7. Total cost TC (£000): 12, 16, 18, 22, 32, 50, 80.
Fit: a ≈ 1.286, b ≈ −9.143, c ≈ 24.71, d ≈ −4.43. R² ≈ 0.997.
Marginal cost dTC/dQ = 3(1.286)Q² − 2(9.143)Q + 24.71 = 3.857Q² − 18.286Q + 24.71.
Minimum marginal cost at Q = 18.286/(2×3.857) ≈ 2.37 units - the most efficient production scale.
Example 5 - River Pollutant Concentration by Distance
Distance from source (km): 0, 5, 10, 15, 20, 25, 30. Concentration (mg/L): 0.5, 2.8, 6.1, 7.4, 5.9, 2.6, 0.8.
Fit: a ≈ 0.00040, b ≈ −0.01952, c ≈ 0.2892, d ≈ 0.501. R² ≈ 0.997.
Peak concentration predicted at x ≈ 14.2 km downstream - close to the observed maximum at 15 km.
At 35 km: ŷ ≈ −0.27 mg/L (slightly negative due to cubic extrapolation - shows the limits of polynomial extrapolation outside the data range).