Curve fitting calculator
Fit a polynomial of degree 1 to 10 to your data, over the whole range or split into pieces, and get SSE, R², adjusted R², RMSE, the largest error, the coefficients and a residual plot. It opens on Skill-Lync's specific-heat data, the dataset of the week 5 curve-fitting challenge in our MATLAB and Python courses. Paste your own x, y data instead, or check the numbers your MATLAB or Python code printed. Free, no sign-up, runs in your browser.
Least squares by QR on centred and scaled x · checked against numpy and scipy curve_fit · Updated 20 Nov 2026
Degree 3 means a·x³ + b·x² + c·x + d, the same as polyfit(T, cp, 3) in MATLAB or a four-coefficient function in curve_fit. A split-wise fit gives each piece its own polynomial; a point exactly on a split belongs to the piece on its right.
Set the data, degree and pieces your code used, then type the SSE, R² and RMSE it printed (any of the three). Matching is to the digits you typed. The boxes show the cubic fit to the course data.
(T in K, cp in J/(kg K), 3,200 rows)
| Degree | SSE | R² | RMSE |
|---|---|---|---|
| 1 | 2163049 | 0.924878 | 25.999 |
| 2 | 540107.6 | 0.981242 | 12.992 |
| 3 | 94272.02 | 0.996726 | 5.4277 |
| 4 | 11070.27 | 0.999616 | 1.86 |
| 5 | 10807.27 | 0.999625 | 1.8377 |
| 6 | 8332.233 | 0.999711 | 1.6136 |
| 7 | 3691.626 | 0.999872 | 1.0741 |
| 8 | 832.2498 | 0.999971 | 0.50998 |
- SST 2.87937e+7 = SSE + SSR; 3200 points, 4 coefficients.
- The largest error is at the ends of the range: polynomials bend away from the data where nothing holds them.
What is the curve fitting challenge?
You get a file of 3,200 temperatures and specific heats, cp against T from 300 K to 3,499 K, and three tasks: fit a linear and a cubic polynomial, say whether the fits are good, and find a way to make the fit "perfect". It is the curve-fitting challenge of Skill-Lync's MATLAB and Python courses for mechanical engineers (week 5, Curve Fitting and Regression, in MATLAB for Mechanical Engineers), and 1,148 public student reports carry "curve fit" in their title. Students fit with polyfit in MATLAB or scipy.optimize.curve_fit in Python, both of which minimise the sum of squared errors:
The calculator above does the same thing for any degree and any number of pieces, and shows where the errors are, not only how big they are.
What are the right answers for the linear and cubic fit?
For the course data: linear R² 0.924878 and RMSE 25.999 J/(kg K); cubic R² 0.996726 and RMSE 5.428. The cubic is 4.8 times better in RMSE, but it still misses by up to 13.440 at 3,499 K. Every degree, computed by Skill-Lync with numpy and checked against scipy's curve_fit:
| Degree | SSE | R² | Adjusted R² | RMSE √(SSE/n) | Largest error | cond(V), raw T |
|---|---|---|---|---|---|---|
| 1 | 2163049.09 | 0.9248776 | 0.9248541 | 25.9991 | 84.46 at 300 K | 4.8 × 10³ |
| 2 | 540107.56 | 0.9812421 | 0.9812304 | 12.9917 | 34.29 at 314 K | 2.4 × 10⁷ |
| 3 | 94272.02 | 0.9967259 | 0.9967229 | 5.4277 | 13.44 at 3499 K | 1.3 × 10¹¹ |
| 4 | 11070.27 | 0.9996155 | 0.9996150 | 1.8600 | 12.27 at 300 K | 7.2 × 10¹⁴ |
| 5 | 10807.27 | 0.9996247 | 0.9996241 | 1.8377 | 13.21 at 300 K | 4.1 × 10¹⁸ |
| 6 | 8332.23 | 0.9997106 | 0.9997101 | 1.6136 | 10.06 at 300 K | 2.4 × 10²² |
| 7 | 3691.63 | 0.9998718 | 0.9998715 | 1.0741 | 5.44 at 300 K | 1.4 × 10²⁶ |
| 8 | 832.25 | 0.9999711 | 0.9999710 | 0.5100 | 1.59 at 300 K | 8.3 × 10²⁹ |
| 9 | 95.83 | 0.9999967 | 0.9999967 | 0.1731 | 0.54 at 3499 K | 5.0 × 10³³ |
| 10 | 62.66 | 0.9999978 | 0.9999978 | 0.1399 | 0.93 at 300 K | 3.0 × 10³⁷ |
The cubic polynomial is cp = 1.9060827e-8·T³ − 0.00013812429·T² + 0.38789102·T + 838.89657 (p from polyfit(T, cp, 3), highest power first). The linear fit is cp = 0.098754·T + 995.202.
We know these are the course's numbers because published reports print them: linear SSE 2163049.0892, R² 0.9248776260524184, RMSE 25.999093068195325, quadratic SSE 540107.5629, cubic R² 0.9967259467146888, and an 8th-order RMSE of 0.5099784812413807. Our values agree to 9 or more significant figures (the last digits differ because curve_fit stops a hair short of the exact least-squares answer).
How do you make the curve fit perfect?
Split the data at 1,000 K and fit a quartic to each side. The errors then fall to round-off: SSE 1 × 10⁻²⁰, against 11,070.3 for a single quartic. That works because of where the data comes from. The file matches, to every printed digit, the NASA 7-coefficient polynomial for oxygen (O₂) from the GRI-Mech 3.0 thermodynamic database:
NASA polynomials use one set of coefficients from 200 to 1,000 K and another from 1,000 to 3,500 K. So the "data" is two quartics joined at 1,000 K. They meet in value (1116.193 J/(kg K) from both sides) but not in slope: dcp/dT drops from 0.1673 to 0.1354 J/(kg K²), 19% lower, at the seam. No single polynomial can bend that sharply, which is why going from degree 4 to degree 5 cuts the SSE by only 2.4%.
Most reports do find the split-wise idea (93 of the 119 we read mention it), but they split into equal parts. Equal halves put the break at about 1,900 K, which is nowhere special: a quartic on each half still leaves RMSE 1.404. Only 1 report in our sample split at 1,000 K. With the right break even a cubic does better (RMSE 1.054) than quartics on two equal halves.
| Degree per piece | Whole range | 2 equal halves | 4 equal quarters | Split at 1,000 K |
|---|---|---|---|---|
| 1 | 25.999 | 13.914 | 3.728 | 4.408 |
| 2 | 12.992 | 3.414 | 1.220 | 1.773 |
| 3 | 5.428 | 1.629 | 0.676 | 1.054 |
| 4 | 1.860 | 1.404 | 0.138 | 2 × 10⁻¹² |
Two notes for the report. A "perfect" fit is a statement about this file, not about oxygen: the file uses R/M = 266.048 J/(kg K) (8,314/31.25), while oxygen's molar mass of 31.999 g/mol gives 259.84, so the course values are 2.39% higher than oxygen's real cp. The shape is the same, so it changes nothing about the fitting. And beyond 3,500 K neither the NASA polynomial nor any of these fits should be trusted.
How do you check how good a curve fit is?
Look at the residual plot first, then the numbers. Three sums of squares measure the scatter about the fit, about the mean, and of the fit itself:
R² is the share of the variation in cp that the fit explains. It is a poor judge on smooth data like this: the linear fit already has R² = 0.925 while missing by up to 84 J/(kg K) at 300 K. RMSE is in the units of the data, so it says how far off a typical prediction is: 5.428 J/(kg K) for the cubic, 0.46% of the mean cp of 1,182.8. Adjusted R² penalises extra coefficients; with 3,200 points and at most 11 coefficients it is almost the same as R². The residual plot shows what the numbers hide. A good fit leaves residuals that look like noise. A wavy residual, like the ones here, means the polynomial has the wrong shape, and the largest errors sit at the ends of the range.
Why does MATLAB warn that the polynomial is badly conditioned?
Because T runs to 3,499 K, so the columns of the fitting matrix V = [T³ T² T 1] range from about 4 × 10¹⁰ down to 1. The condition number of V is 1.3 × 10¹¹ for a cubic and 4.1 × 10¹⁸ for degree 5, past the 10¹⁶ limit of double precision. MATLAB's polyfit checks for this and warns that the polynomial is badly conditioned, suggesting centring and scaling; 12 of the 119 reports we read quote the warning. Centring and scaling fits in a new variable:
That brings the condition number to 6.015 for the cubic and 37.255 for degree 5. On this data the fitted curve is the same either way (numpy's raw polyfit and the scaled fit agree to 7 digits in SSE even at degree 10), but at higher degrees or with less tidy data the raw coefficients lose accuracy. This calculator always fits in the scaled variable and converts the coefficients back to powers of x only for display.
What do popt and pcov mean in curve_fit?
popt holds the best-fit coefficients in the order your function lists them. pcov is their covariance matrix, estimated from the residuals:
For the course data, the linear fit gives a = 0.098754 ± 0.000498 and b = 995.20 ± 1.05. The correlation between a and b is -0.899: raise the slope and the intercept must drop to keep the line through the data. Read those standard errors with care here. They assume the residuals are random noise, and these are not: they are the systematic shape error of a straight line through a curve. The data has no noise at all.
In func(np.array(temperature), *popt), the * unpacks popt into separate arguments (func(T, a, b)), and np.array turns the list of temperatures into an array so that a*T + b is computed for all 3,200 points at once.
Which mistakes show up in published reports?
The "Check my numbers" tab recognises these. All of them come from the 151 public Skill-Lync reports of this challenge we read on 20 Nov 2026:
- RMSE = (SSE/1)^0.5 instead of (SSE/l)^0.5. The template names the length l (a lower-case L), which is easy to retype as 1. The result is √SSE: 1470.731 for the linear fit instead of 25.999. Seen in 3 reports.
- A loop over j that indexes with i. for j in range(l): e = cp[i] − fit[i] reuses the last i from the previous loop, so every term is the error at 3,499 K. One report prints a cubic "RMSE" of 13.440079591 that is exactly that error.
- R instead of R², from R = (SSR/SST)^0.5: 0.998362 for the cubic instead of 0.996726.
- A loop over range(1), which only adds the first point.
- Calling R² above 0.95 a good fit. The linear fit reaches 0.92 while its error at 300 K is 84 J/(kg K), 9% of the value there. Judge the fit by RMSE and the residual plot against what you need the fit for.
In our sample 56 reports used MATLAB, 60 Python and 3 both.
What this calculator does not cover
- Non-polynomial models (exponential, power law, Arrhenius, NASA-form fits with 1/T terms) and nonlinear least squares in general. Polynomials of degree 1 to 10 only.
- Weighted fits, fits through a fixed point, and smoothing or interpolating splines. Each piece of a split-wise fit is independent, so the pieces do not join smoothly at the splits.
- Prediction and confidence intervals. They need random, independent errors, which the course data does not have.
- Extrapolation. A polynomial fitted from 300 to 3,499 K says nothing reliable outside that range, and high degrees swing fastest just past the ends.
Curve fitting is one of the weekly challenges in Skill-Lync's MATLAB and Python courses for mechanical engineers, next to ODE solvers, engine cycles and data analysis. The same skills turn test data into the models that simulation and controls teams use every day.
Frequently asked questions
What are the R² and RMSE of the linear and cubic fit for the Cp data?
For Skill-Lync's 3,200-point Cp vs temperature file (300 to 3,499 K): linear fit SSE 21,63,049.1, R² 0.924878, RMSE 25.999 J/(kg K); cubic fit SSE 94,272.0, R² 0.996726, RMSE 5.428. RMSE here is √(SSE/n). These match the values in published student reports to 9 or more significant figures.
How do you make the curve fit perfect?
Split the data at 1,000 K and fit a fourth-order polynomial to each part. The file is built from the NASA 7-coefficient polynomial for oxygen, which uses one quartic below 1,000 K and another above. Fitted that way the error is round-off: SSE 1 × 10⁻²⁰. A single polynomial of degree 10 still leaves RMSE 0.140.
What do popt and pcov mean in scipy curve_fit?
popt is the array of best-fit coefficients, in the order your function takes them (for a·T + b, popt = [a, b]). pcov is their estimated covariance matrix: the square root of each diagonal entry is the standard error of that coefficient, and the off-diagonal entries show how strongly two coefficients trade off. For the linear Cp fit, a = 0.09875 ± 0.00050.
What does the * in *popt mean?
It unpacks the array into separate arguments. If popt = [a, b], then func(T, *popt) is the same call as func(T, a, b). np.array(temperature) turns the Python list into a NumPy array, so a*T + b works on all 3,200 values at once instead of failing on a list.
Why does MATLAB say the polynomial is badly conditioned?
Because the columns of the fitting matrix (1, T, T², T³ ...) differ by many orders of magnitude when T runs to 3,499 K. Its condition number is 1.3 × 10¹¹ for a cubic and 4.1 × 10¹⁸ for degree 5. Centring and scaling T, [p, S, mu] = polyfit(T, cp, n), brings that to 6.015 and 37.255. The fit itself barely changes.
Should RMSE divide by n or by n − p?
Both are in use. √(SSE/n) is the plain root-mean-square of the residuals, and most course codes use it. MATLAB's Curve Fitting Toolbox reports √(SSE/(n − p)), the standard error of the regression, where p is the number of coefficients. With 3,200 points the difference is tiny: 5.4277 against 5.4311 for the cubic.
Related on Skill-Lync
- 2D heat conduction solver (the mid-term project of the CFD course)
- Adiabatic flame temperature calculator (uses the same NASA polynomials)
- Otto and Diesel cycle calculator (the air-standard cycle challenge of the MATLAB course)
- Rankine cycle calculator
- Inverse kinematics calculator (the 2R robotic arm challenge of the same courses)
- Genetic algorithm simulator (the stalagmite function challenge of the MATLAB course)
- Student project: week 5, curve fitting
- Interpolation calculator (values between points: linear, pchip, makima, spline)
- All free engineering tools
Sources
- G. P. Smith et al., GRI-Mech 3.0, thermo30.dat (combustion.berkeley.edu/gri-mech): O₂ NASA polynomial coefficients, 200-1,000 K and 1,000-3,500 K.
- B. J. McBride, S. Gordon, M. A. Reno, Coefficients for Calculating Thermodynamic and Transport Properties of Individual Species, NASA TM-4513, 1993 (the 7-coefficient polynomial form).
- MathWorks, polyfit documentation (centring and scaling, the conditioning warning) and Curve Fitting Toolbox goodness-of-fit statistics (RMSE with n − p). SciPy, scipy.optimize.curve_fit documentation (popt, pcov).
- Skill-Lync, sample of 151 public student reports of the curve-fitting challenge (119 with Cp data and a fit), read 20 Nov 2026: tools used, split choices, printed results.
- Skill-Lync, scripts/verify/curve-fit.py: rebuilds the data from the GRI-Mech file, fits it with numpy three ways and with scipy curve_fit, and checks the published report values. This page refits in the browser code and checks its numbers against that script at build time.