An introduction to least-squares curve fitting: linear, polynomial, exponential, power-law, logarithmic, logistic, and Gaussian models; what R² and residual plots reveal; and how to recognize overfitting.

Contents
  1. What Is Curve Fitting? Which Equation Describes My Data?
  2. Least Squares: What Is Minimised
  3. Methods
    1. Linear
    2. Polynomial
    3. Exponential
    4. Power Law
    5. Logarithmic
    6. Logistic (Sigmoid)
    7. Gaussian Peak
    8. The Seven Side by Side
  4. Which One Should You Choose?
  5. Key Ideas
    1. Polynomial Order: Too Low Misses, Too High Chases the Noise
    2. Goodness of Fit: R², Adjusted R², RMSE
    3. The Residual Plot Reads What R² Hides
    4. Linearised Fits: A Logarithm Changes the Answer
    5. Non-Linear Fits Need a Starting Guess
    6. Extrapolation: Where a Model Is Worst
    7. Standard Error: How Far a Coefficient Could Move
  6. REFs

What Is Curve Fitting? Which Equation Describes My Data?

A calibration run gives a sensor reading for each known concentration. A cooling experiment records the temperature every half-minute. A growth study records a count each day. In every case, the question is the same: which equation describes the data, what values should its coefficients have, and how well does it describe the observations?

Curve fitting provides a model: a formula with a few coefficients, such as $y = a + b\,x$, and the values of $a$ and $b$ that make it miss the data least. The model is the product; the fitted curve is only a picture of it. Two coefficients can stand for a hundred measured points, and they also give $y$ at an $x$ nobody measured.

This distinguishes curve fitting from two related methods (Fig 1). Interpolation passes through every point and treats each one as exact. Smoothing returns one cleaned value per point, but no formula. A fit misses points on purpose: it assumes the scatter is noise and keeps only the shape.

The same nine points three ways: left, a blue line joining every point; middle, a green line of smoothed values, one per point; right, one red straight line through the middle of the points, continuing faintly past the last point

Fig 1. Interpolation joins the points, smoothing cleans them, a fit replaces them with an equation

Take an example: three columns measured at $x$ = 1, 1.5, 2, … 13—25 rows. The example has three purposes.

Chart 1 shows all 25 rows of the three columns.

Scatter of three columns against x from 1 to 13: blue y1 rising steadily from 3 to 12; red y2 rising from 2.8 to a peak of about 9 near x = 7 and falling to 2.3; green y3 falling quickly from 7.4 and leveling near 0.4

Chart 1. The example: a line, a curve, and a decay, each with noise

The task: recover the equation behind each column—and tell, from the data alone, when the equation is the wrong one.

Least Squares: What Is Minimised

A model $f(x)$ misses each point by its residual, the vertical distance between the measured $y_i$ and the model's value at the same $x_i$. Least squares picks the coefficients that make the sum of the squared residuals, SSE, as small as possible (Math 1):

$$r_i = y_i - f(x_i), \qquad \mathrm{SSE} = \sum_{i=1}^{N} r_i^2 = r_1^2 + r_2^2 + \dots + r_N^2 \;\rightarrow\; \text{minimum}$$
$$\begin{aligned} r_i &= y_i - f(x_i) \\ \mathrm{SSE} &= \sum_{i=1}^{N} r_i^2 \\ &= r_1^2 + r_2^2 + \dots + r_N^2 \;\rightarrow\; \text{minimum} \end{aligned}$$

Math 1.

Chart 2 shows the residuals of $y_1$ around its fitted line. Two choices hide in Math 1.

y1 points with a blue fitted line y = 2.207 + 0.7458 x; short red vertical segments join each point to the line

Chart 2. $y_1$ and its line: each residual is a vertical miss

Methods

The seven models fall into two groups (Fig 2). Linear, polynomial and logarithmic are linear in their coefficients: the minimum of Math 1 is found in one step, exactly. Exponential, power, logistic and Gaussian are not: the minimum is found by non-linear least squares, an iteration that starts from a guess.

Seven small panels, one curve each: a straight line, a hump-shaped parabola, a falling exponential, a rising power curve that bends over, a logarithmic curve rising steeply then slowly, an S-shaped logistic curve leveling off, and a bell-shaped Gaussian peak

Fig 2. The seven model shapes

Linear

In plain words. A straight line: a constant slope, the same change in $y$ for every step in $x$. The simplest model, and the one every other model is compared with.

Control parameters. None: the model has two coefficients, $a$ and $b$, and both are fitted.

Basic idea. Simple linear regression. With the means $\bar{x}$ and $\bar{y}$, the minimum of Math 1 is (Math 2):

$$f(x) = a + b\,x, \qquad b = \frac{\sum_{i=1}^{N} (x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=1}^{N} (x_i - \bar{x})^2} = \frac{(x_1 - \bar{x})(y_1 - \bar{y}) + \dots + (x_N - \bar{x})(y_N - \bar{y})}{(x_1 - \bar{x})^2 + \dots + (x_N - \bar{x})^2}, \qquad a = \bar{y} - b\,\bar{x}$$
$$\begin{aligned} f(x) &= a + b\,x \\ b &= \frac{\sum_{i=1}^{N} (x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=1}^{N} (x_i - \bar{x})^2} \\ &= \frac{(x_1 - \bar{x})(y_1 - \bar{y}) + \dots}{(x_1 - \bar{x})^2 + \dots} \\ a &= \bar{y} - b\,\bar{x} \end{aligned}$$

Math 2.

Example. Take the first five rows of $y_1$ (Table 1). The $x$ values add up to 10 and the $y$ values to 18.39, so $\bar{x} = 2$ and $\bar{y} = 3.678$.

$x$$y_1$$x - \bar{x}$$y_1 - \bar{y}$$(x - \bar{x})(y_1 - \bar{y})$$(x - \bar{x})^2$
12.99−1.0−0.6880.68801.00
1.53.01−0.5−0.6680.33400.25
23.870.00.1920.00000.00
2.53.450.5−0.228−0.11400.25
35.071.01.3921.39201.00
sum2.30002.50

Table 1. The first five rows of $y_1$, the two sums of Math 2

Then (Math 3):

$$b = \frac{2.3000}{2.50} = 0.9200, \qquad a = 3.678 - 0.9200 \times 2 = 1.8380$$

Math 3.

The line $y = 1.838 + 0.92\,x$ misses the five points by 0.2320, −0.2080, 0.1920, −0.6880 and 0.4720; their squares add up to SSE = 0.8301. Over all 25 rows, the same formulas give $y = 2.207 + 0.7458\,x$ (Chart 2), close to the $2 + 0.8\,x$ the data were made from.

Important characteristics. One closed-form step, no starting guess, always the same answer.

Advantages

Disadvantages

Polynomial

In plain words. A straight line plus powers of $x$: $x^2$ adds one bend, $x^3$ a second, and so on. The order sets how many bends the curve may have—and every extra order is one more coefficient to fit.

Control parameters. order ($n$ below, default 2, from 1 to 10): the highest power of $x$. The model has $n + 1$ coefficients; order 1 is the linear model.

Basic idea. Polynomial regression (Math 4):

$$f(x) = c_0 + c_1\,x + c_2\,x^2 + \dots + c_n\,x^n$$

Math 4.

Example. $y_2$ at order 2 gives $y = 0.803361 + 2.31723\,x - 0.169773\,x^2$, with R² 0.9604—against 0.0113 for the straight line, which cannot bend (Chart 3). The data were made from $1 + 2.2\,x - 0.16\,x^2$.

Important characteristics. A polynomial can be flexible near the data but unreliable outside the measured range, because its highest powers can grow without bound.

Advantages

Disadvantages

Exponential

In plain words. Constant relative change: each step in $x$ multiplies $y$ by the same factor. Growth when $b > 0$, decay when $b < 0$—cooling, discharge, radioactive decay, compound interest.

Control parameters. space (advanced calculator): non-linear, the default, minimises Math 1 on $y$ itself; linearised fits a straight line to $\ln y$ instead (see Linearised Fits).

Basic idea. With $a$ the value at $x = 0$ and $b$ the rate (Math 5):

$$f(x) = a\,e^{b\,x}$$

Math 5.

Example. $y_3$ gives $y = 9.928\,e^{-0.295743\,x}$, R² 0.9928, close to the $10\,e^{-0.3\,x}$ it was made from; the half-life is $0.6931 / 0.2957 = 2.34$.

Important characteristics. Never crosses zero; the curve keeps the sign of $a$.

Advantages

Disadvantages

Power Law

In plain words. Constant relative change per relative change: doubling $x$ always multiplies $y$ by the same factor. Area against length, drag against speed, metabolic rate against body mass—a power law is a straight line on a log–log plot.

Control parameters. space, as for the exponential: non-linear, or a straight line through $(\ln x, \ln y)$.

Basic idea. With $b$ the exponent (Math 6):

$$f(x) = a\,x^{b}, \qquad x > 0$$

Math 6.

Example. $y_1$ gives $y = 2.14011\,x^{0.65642}$, R² 0.9731—lower than the straight line's 0.9824. A power law with $b < 1$ bends over, and the line in $y_1$ does not.

Important characteristics. Passes through $(0, 0)$ when $b > 0$: no offset.

Advantages

Disadvantages

Logarithmic

In plain words. Constant change per relative change: each doubling of $x$ adds the same amount to $y$. Steep at first, then ever slower, and never leveling off completely—loudness in decibels, the pH scale, learning curves.

Control parameters. None.

Basic idea. A straight line in $\ln x$ (Math 7):

$$f(x) = a + b \ln x, \qquad x > 0$$

Math 7.

Example. $y_3$ gives $y = 7.22275 - 2.90895 \ln x$, R² 0.9740: a falling curve that flattens, but less well than the exponential's 0.9928.

Important characteristics. Unbounded in both directions: no plateau.

Advantages

Disadvantages

Logistic (Sigmoid)

In plain words. An S-curve: slow start, rapid middle, and a plateau it never passes. Population growth with limited resources, a dose–response curve, the spread of an adoption.

Control parameters. None to set: the three coefficients are fitted from an automatic starting guess.

Basic idea. The logistic function, with the plateau $L$, the steepness $k$ and the midpoint $x_0$ (Math 8):

$$f(x) = \frac{L}{1 + e^{-k\,(x - x_0)}}$$

Math 8.

Example. $y_1$ gives $L = 15.4927$, $k = 0.207324$, $x_0 = 7.44603$, R² 0.9800—almost as good as the straight line, because 25 points of a line also look like the lower half of an S. The plateau of 15.49 lies above every measured value: the model predicts it, the data never showed it. On $y_3$ the fit reaches R² 0.9928 with $L = 473113$, a plateau that means nothing.

Important characteristics. A coefficient is only meaningful if the data cover the feature it describes: $L$ needs the plateau, $x_0$ the midpoint.

Advantages

Disadvantages

Gaussian Peak

In plain words. A symmetric bell: one peak, falling to zero on both sides. A spectral line, a chromatography peak, a beam profile.

Control parameters. None to set: the three coefficients are fitted from an automatic starting guess.

Basic idea. The Gaussian function, with the height $A$, the center $\mu$ and the width $\sigma$ (Math 9):

$$f(x) = A\,e^{-\frac{(x - \mu)^2}{2\sigma^2}}$$

Math 9.

Example. $y_2$, a hump, gives $A = 8.94966$, $\mu = 6.77743$, $\sigma = 4.20695$, R² 0.9395—lower than the parabola's 0.9604: the hump of $y_2$ is a parabola, with no tails falling toward zero. On $y_3$, which has no peak, the solver does not converge, and the calculator reports that instead of a curve.

Important characteristics. Falls to zero on both sides: no baseline.

Advantages

Disadvantages

The Seven Side by Side

Each model fitted to each data column (Table 2; polynomial at order 2):

model$y_1$ (line)$y_2$ (curve)$y_3$ (decay)
linear0.9824 (0.3601)0.0113 (2.0099)0.8080 (0.8865)
polynomial0.9824 (0.3601)0.9604 (0.4020)0.9794 (0.2904)
exponential0.9588 (0.5508)0.0089 (2.0124)0.9928 (0.1714)
power0.9731 (0.4449)0.0264 (1.9945)0.9131 (0.5963)
logarithmic0.8894 (0.9022)0.0336 (1.9872)0.9740 (0.3261)
logistic0.9800 (0.3838)0.4191 (1.5406)0.9928 (0.1714)
Gaussian0.9801 (0.3825)0.9395 (0.4973)no convergence

Table 2. R² (with RMSE in parentheses) for each model and data column; the model used to generate each column is in bold

The true model has the best R² in each column—but by small margins, and not alone: on $y_1$, the order-2 polynomial ties the line (its $x^2$ coefficient is −0.0000290), and on $y_3$, the logistic ties the exponential with a meaningless plateau. R² alone cannot choose a model; the next sections show what can.

Which One Should You Choose?

your datausein the example
a constant rate of change, a calibration linelinear$y_1$: 0.9824
a smooth curve, no physical model, used inside the data rangepolynomial, lowest order that leaves no pattern$y_2$: order 2, 0.9604
growth or decay at a rate proportional to the sizeexponential$y_3$: 0.9928
a scaling law, straight on a log–log plotpower law—
a response to ratios of $x$, slowing without a limitlogarithmic—
an S-curve with a visible plateaulogistic—
a single symmetric peak on a flat zero baselineGaussian—

Table 3. Choose a model by the process that generated the data, not by the highest R²

Start from what produced the data. A physical model—decay, growth, a scaling law—has coefficients that mean something and that hold outside the data. A polynomial is a description, valid only where it was fitted. In either case, check the residual plot before believing the fit.

Key Ideas

Polynomial Order: Too Low Misses, Too High Chases the Noise

Chart 3 fits $y_2$ at three orders. Order 1 is a straight line fitted to a hump: it misses the shape entirely (R² 0.0113). Order 2 follows the hump. Order 10 follows it too, with nine more coefficients, and bends toward single noisy points—R² rises only from 0.9604 to 0.9644.

y2 points with three fitted curves: a nearly flat blue line for order 1, a smooth green parabola for order 2, and a red order-10 curve almost on top of the parabola, wiggling slightly toward individual points

Chart 3. $y_2$ at orders 1, 2 and 10: order 1 misses the shape, order 10 adds nothing but wiggles

At the extreme, an order of $N - 1$ through $N$ points has as many coefficients as there are points, and passes through every one of them. Chart 4 takes seven points of $y_2$, every fourth row, and fits order 6: every residual is 0, R² is 1, and the curve overshoots to 9.535 between the points. It is no longer a fit at all; it is polynomial interpolation. Order 2 through the same seven points stays close to the curve the data came from.

Seven black points of y2; a red order-6 curve passes exactly through all seven and overshoots above 9.5 between x = 5 and 7; a green order-2 parabola misses the points slightly and stays near a gray dashed curve, the curve the data were made from

Chart 4. Seven points, order 6: through every point, which is interpolation, not a fit

This is the bias–variance trade: too few coefficients and the model is wrong everywhere (bias); too many and it follows this particular noise, and would follow different noise in a repeat measurement (variance).

Goodness of Fit: R², Adjusted R², RMSE

Three numbers summarise a fit, with $\bar{y}$ the mean of the data and $p$ the number of coefficients (Math 10):

$$R^2 = 1 - \frac{\mathrm{SSE}}{\mathrm{SST}}, \quad \mathrm{SST} = \sum_{i=1}^{N} (y_i - \bar{y})^2 = (y_1 - \bar{y})^2 + \dots + (y_N - \bar{y})^2, \quad R^2_{\text{adj}} = 1 - (1 - R^2)\,\frac{N - 1}{N - p}, \quad \mathrm{RMSE} = \sqrt{\frac{\mathrm{SSE}}{N}}$$
$$\begin{aligned} R^2 &= 1 - \frac{\mathrm{SSE}}{\mathrm{SST}} \\ \mathrm{SST} &= \sum_{i=1}^{N} (y_i - \bar{y})^2 \\ &= (y_1 - \bar{y})^2 + \dots + (y_N - \bar{y})^2 \\ R^2_{\text{adj}} &= 1 - (1 - R^2)\,\frac{N - 1}{N - p} \\ \mathrm{RMSE} &= \sqrt{\frac{\mathrm{SSE}}{N}} \end{aligned}$$

Math 10.

Example. For the five points of Table 1, SST = 2.9461 and SSE = 0.8301 (Math 11):

$$R^2 = 1 - \frac{0.8301}{2.9461} = 0.7182, \qquad \mathrm{RMSE} = \sqrt{\frac{0.8301}{5}} = 0.4075$$

Math 11.

Table 4 fits $y_2$ at every order from 1 to 10.

orderR²adjusted R²RMSE
10.0113−0.03172.0099
20.96040.95680.4020
30.96160.95620.3959
40.96200.95440.3939
50.96300.95320.3889
60.96300.95070.3887
70.96310.94790.3882
80.96310.94470.3882
90.96320.94110.3878
100.96440.93900.3813

Table 4. $y_2$ at polynomial orders 1 to 10: R² never decreases, while adjusted R² peaks at order 2

R² rises at every order, because a model with one more coefficient can always do at least as well; it cannot choose an order. Adjusted R² peaks at order 2—the order the data were made from—and falls after. RMSE at order 2, 0.4020, is the noise put into the data, 0.4: there is nothing left to explain. Report RMSE alongside R²: an R² of 0.96 does not tell you whether the typical miss is 0.4 or 400.

The Residual Plot Reads What R² Hides

A straight line through $y_3$ has R² 0.8080—respectable on its own. Its residuals, plotted against $x$, tell a different story (Chart 5): the first five are positive, the next thirteen negative, the last seven positive again. A U-shape is curvature the model did not capture. The exponential leaves residuals that change sign at random: noise, and nothing else.

Residuals of y3 against x: blue dots for the straight line form a U, from +2.25 at x = 1 down to about -1 in the middle and back up to +1.3 at x = 13; green dots for the exponential scatter closely around zero with no pattern

Chart 5. Residuals of $y_3$: the straight line leaves a U, the exponential leaves noise

The rule is simple: a pattern in the residuals suggests that the model is wrong, regardless of how high R² is. A good fit leaves residuals with no visible pattern—no curve, no trend, no spread that grows with $x$. The NIST e-Handbook builds its whole model check on this plot.

Linearised Fits: A Logarithm Changes the Answer

Taking the logarithm turns an exponential into a straight line, and a straight line can be fitted in one exact step (Math 12):

$$y = a\,e^{b\,x} \quad\Longrightarrow\quad \ln y = \ln a + b\,x$$

Math 12.

It is not a shortcut to the same answer. Fitting $\ln y$ minimises the squared misses in $\ln y$—relative errors—while Math 1 minimises the misses in $y$. On $y_3$, the two disagree (Chart 6):

y3 points with two decay curves: green, the non-linear fit, through the first points near 7.4; purple, the linearised fit, starting lower at about 6.7 and running slightly above the green curve at the end

Chart 6. $y_3$: the non-linear and the linearised exponential are two different fits

Which is right depends on the noise. If the noise is a fixed amount, as in $y_3$, the non-linear fit is the honest one. If it is a fixed percentage—counts, concentrations over several decades—the linearised fit weighs the points correctly. The advanced calculator offers both and names which one ran.

Non-Linear Fits Need a Starting Guess

An iterative solver walks downhill on SSE from where it starts. From a good start it finds the minimum; from a bad one it can stop somewhere else, or not stop at all. Chart 7 fits a Gaussian to $y_2$ twice.

y2 points with two Gaussians: green, the fit from the automatic guess, a bell over the data peaking near 9 at x = 6.8, beside its dashed starting curve; red, a narrow bell at x = 20 far right of the data, identical to its dashed starting curve, flat at zero over all the points

Chart 7. A Gaussian on $y_2$: from the automatic guess the solver finds the hump; from $\mu = 20$ it never moves

The second run "converged": the solver reported success. A fit should be checked rather than trusted. The calculator rejects a non-linear result that does worse than a flat line (R² below 0). If a fit fails to converge, as the Gaussian fit to $y_3$ does in Table 2, the calculator reports the failure instead of returning a misleading curve. And a converged fit can still be meaningless, as the logistic plateau of 473113 on $y_3$ shows.

Extrapolation: Where a Model Is Worst

A model gives $y$ at any $x$, including those outside the data. Interpolation stops at the last point; a fit cannot refuse, so it must be read with care. Chart 8 fits $y_2$ at orders 2 and 8. Inside the data the two are the same curve—R² 0.9604 and 0.9631. At $x = 20$, seven units past the last point, order 2 gives −20.76 and order 8 gives +157.94; the curve the data came from gives −19.00.

y2 points from x = 1 to 13 with two fits that overlap on the data; in a gray band beyond x = 13 labeled no data here, the green order-2 curve keeps falling to -20 at x = 20 while the red order-8 curve turns sharply upward past 20 before x = 18

Chart 8. $y_2$ at orders 2 and 8: the same inside the data, opposite directions outside it

Extrapolation is safe only as far as the model is known to hold. A physical model (decay, growth) carries its reason with it; a polynomial carries none, and its highest power takes over as soon as the data stop holding it.

Standard Error: How Far a Coefficient Could Move

Fitted coefficients come from noisy data, so they are noisy too. The standard error of each coefficient says how far it would typically move if the measurement were repeated with new noise. With $J$ the matrix of the model's derivatives with respect to each coefficient, one row per point, and $s^2 = \mathrm{SSE} / (N - p)$ the residual variance (Math 13):

$$\mathrm{SE}(\beta_j) = \sqrt{s^2 \left[(J^{\mathsf{T}} J)^{-1}\right]_{jj}}, \qquad s^2 = \frac{\mathrm{SSE}}{N - p}$$

Math 13.

Example. $y_3$, exponential: $a = 9.9280 \pm 0.2168$ and $b = -0.2957 \pm 0.0074$. The values the data were made from, 10 and −0.3, lie 0.33 and 0.58 standard errors away. For $y_2$ at order 2, the intercept is $c_0 = 0.8034 \pm 0.3253$—a wide range, because $c_0$, $c_1$ and $c_2$ can trade off against each other: a little more of one, a little less of another, and the curve barely changes. That trade-off is the subject of parameter correlation, a topic of its own. The advanced calculator shows the standard error of every coefficient.

A fit is a model, not a curve that must pass through every point. Choose the model by the process behind the data, then check it with the residuals.

REFs

NIST/SEMATECH e-Handbook, Process Modeling NIST/SEMATECH e-Handbook, How can I tell if a model fits my data? NumPy numpy.polyfit SciPy scipy.optimize.curve_fit Wikipedia, Curve fitting Wikipedia, Least squares Wikipedia, Simple linear regression Wikipedia, Polynomial regression Wikipedia, Non-linear least squares Wikipedia, Levenberg–Marquardt algorithm Wikipedia, Coefficient of determination Wikipedia, Overfitting Interpolation Methods Smoothing Methods: Moving Average, EWMA, Savitzky-Golay, Gaussian, Median