When data are recorded on two variables simultaneously — say \(X\) (independent) and \(Y\) (dependent) — they are called bivariate data: \((x_1, y_1), (x_2, y_2), \ldots, (x_n, y_n)\).
Curve fitting is the process of finding a mathematical curve that best describes the relationship between \(X\) and \(Y\).
Once a curve is fitted, it can be used to estimate future values (interpolation / extrapolation) and to understand the trend in the data.
Among all possible curves \(Y = f(X)\) of a given form, the best-fit curve is the one for which the sum of squares of the residuals (vertical distances between observed and predicted values) is a minimum:
\[ S \;=\; \sum_{i=1}^{n}\bigl(y_i - f(x_i)\bigr)^2 \;=\; \min. \]Setting \(\partial S/\partial(\text{parameter}) = 0\) for each parameter gives a system of normal equations whose solution provides the optimal parameter values.
For each observation the fitted curve gives an estimated value \(\hat y_i = f(x_i)\). The difference \(e_i = y_i - \hat y_i\) is the residual. Positive and negative residuals would cancel in a plain sum, so the principle squares them first. The method was published by A.-M. Legendre in 1805; C. F. Gauss, who had used it earlier, gave its probability justification.
For the straight line, \(E(a, b) = \sum (y_i - a - bx_i)^2\). Setting the first partial derivatives to zero finds a stationary point; the second derivatives show it is a minimum:
\[ \frac{\partial^2 E}{\partial a^2} = 2n, \qquad \frac{\partial^2 E}{\partial b^2} = 2\sum x_i^2, \qquad \frac{\partial^2 E}{\partial a\,\partial b} = 2\sum x_i . \]The test for a minimum needs \(\frac{\partial^2 E}{\partial a^2} > 0\) and
\[ \frac{\partial^2 E}{\partial a^2}\cdot\frac{\partial^2 E}{\partial b^2} - \Big(\frac{\partial^2 E}{\partial a\,\partial b}\Big)^2 = 4\Big[n\sum x_i^2 - \big(\textstyle\sum x_i\big)^2\Big] = 4n\sum (x_i - \bar x)^2 > 0, \]which holds whenever the \(x_i\) are not all equal. Since \(E\) is a sum of squares it has no maximum, and the single stationary point is the least value. The same holds for every curve whose parameters enter linearly.
Suppose we wish to fit \(Y = a_0 + a_1 X + a_2 X^2 + \cdots + a_k X^k\) to bivariate data. The least-squares normal equations are:
This is a system of \(k + 1\) linear equations in the \(k + 1\) unknowns \(a_0, a_1, \ldots, a_k\).
Form: \(Y = a + bX\).
Solving for \(a\) and \(b\):
\[ b \;=\; \dfrac{n \sum xy - \sum x \sum y}{n \sum x^2 - (\sum x)^2}, \qquad a \;=\; \bar y - b \bar x. \]The first normal equation also says \(\sum (y_i - \hat y_i) = 0\): the residuals of a least-squares line add to zero. That gives a quick arithmetic check on any fit.
Fit a straight line to the data:
| x | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| y | 3 | 7 | 13 | 21 | 31 |
\(\sum x = 15,\; \sum y = 75,\; \sum xy = 295,\; \sum x^2 = 55,\; n = 5\).
\(b = (5 \cdot 295 - 15 \cdot 75)/(5 \cdot 55 - 225) = (1475 - 1125)/(275 - 225) = 350/50 = 7\).
\(a = \bar y - b\bar x = 15 - 7 \cdot 3 = -6\). Fitted line: \(Y = -6 + 7X\).
Sales (₹ '000) of a company in 5 years are 12, 15, 18, 22, 25. Fit a linear trend (use \(x = 1, 2, 3, 4, 5\)).
\(\sum x = 15, \sum y = 92, \sum xy = 309, \sum x^2 = 55\).
\(b = (5 \cdot 309 - 15 \cdot 92)/50 = (1545 - 1380)/50 = 165/50 = 3.3\).
\(a = 18.4 - 3.3(3) = 8.5\). Trend line: \(\hat y = 8.5 + 3.3 x\). Predicted Year-6 sales \(= 8.5 + 3.3(6) = 28.3\), i.e. ₹28 300.
Form: \(Y = a + bX + cX^2\).
With \(E = \sum [y_i - (a + bx_i + cx_i^2)]^2\), each partial derivative brings down the factor that multiplies its parameter: \(-1\) for \(a\), \(-x_i\) for \(b\), and \(-x_i^2\) for \(c\):
\[ \frac{\partial E}{\partial a} = -2\sum [y_i - (a + bx_i + cx_i^2)] = 0, \] \[ \frac{\partial E}{\partial b} = -2\sum x_i[y_i - (a + bx_i + cx_i^2)] = 0, \] \[ \frac{\partial E}{\partial c} = -2\sum x_i^2[y_i - (a + bx_i + cx_i^2)] = 0 . \]Multiplying out and moving the unknowns to the right gives the three normal equations above. The pattern is the general rule: multiply the equation of the curve by each term that carries a parameter (\(1, x, x^2\)) and add over the data.
Fit parabola to data:
| x | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| y | 1 | 1.8 | 1.3 | 2.5 | 6.3 |
\(\sum x = 10\), \(\sum y = 12.9\), \(\sum x^2 = 30\), \(\sum x^3 = 100\), \(\sum x^4 = 354\), \(\sum xy = 37.1\), \(\sum x^2 y = 130.3\).
Solve the 3×3 system:
Solution (by elimination): \(a \approx 1.42,\; b \approx -1.07,\; c \approx 0.55\).
Fitted parabola: \(Y \approx 1.42 - 1.07 X + 0.55 X^2\). Worked Problem 4 below carries out the
elimination step by step.
For data with \(x = -2, -1, 0, 1, 2\) (centred origin), normal equations simplify because \(\sum x = \sum x^3 = 0\). The parabola \(Y = a + bx + cx^2\) reduces to:
This trick (centred \(x\)) is widely used in time-series trend fitting.
Take logarithms: \(\log Y = \log a + X \log b\). Let \(Y' = \log Y, A = \log a, B = \log b\). Then \(Y' = A + B X\) — fit a straight line.
Then \(a = \text{antilog}(A),\; b = \text{antilog}(B)\).
Same idea: \(\ln Y = \ln a + bX\). Fit straight line in \((x, \ln y)\).
With common logarithms. \(\log_{10} Y = \log_{10} a + (b\log_{10} e)X\). Put \(A = \log_{10} a\) and \(B = b\log_{10} e\); fit \(\log_{10} y = A + Bx\) as a straight line, then
\[ a = \text{antilog}(A), \qquad b = \frac{B}{\log_{10} e} = \frac{B}{0.4343} . \]Both routes give the same \(a\) and \(b\). Natural logarithms are the shorter route when a calculator is at hand.
For curves fitted through logarithms (sections 6 and 7) the least-squares principle is applied to \(\log y\), not to \(y\). The fit minimises \(\sum (\log y_i - \log \hat y_i)^2\), so it controls relative errors, \(y_i/\hat y_i\), rather than the residuals \(y_i - \hat y_i\). It is the standard textbook method and is easy to compute, but the curve is not the least-squares curve in the original units. Always compare the fitted values with the data.
Fit \(Y = a b^X\) to:
| x | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| y | 2 | 4 | 8 | 16 |
\(\log y\) values: 0.301, 0.602, 0.903, 1.204. \(\sum x = 10,\; \sum \log y = 3.010,\; \sum x \log y = 9.030,\; \sum x^2 = 30\).
\(B = (4 \cdot 9.030 - 10 \cdot 3.010)/(4 \cdot 30 - 100)\) \(= (36.12 - 30.10)/20 = 0.301\); \(A = 3.010/4 - 0.301 \cdot 2.5\) \(= 0.7525 - 0.7525 = 0\).
\(\Rightarrow a = 10^0 = 1,\; b = 10^{0.301} = 2\). Fitted: \(Y = 1 \cdot 2^X = 2^X\). ✓
Population (in lakhs) of a city for 4 years:
| Year (x) | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| Pop (y) | 10 | 11.6 | 13.5 | 15.7 |
Fit \(y = ae^{bx}\). Step 1. \(\ln y\) = 2.3026, 2.4510, 2.6027, 2.7537, so \(\sum \ln y = 10.1099\), \(\sum x\ln y = 26.0273\), with \(\sum x = 10\), \(\sum x^2 = 30\), \(n = 4\).
Step 2. \(b = \dfrac{4(26.0273) - 10(10.1099)}{4(30) - 10^2} = \dfrac{3.0102}{20} = 0.1505\), and \(\ln a = (10.1099 - 10 \times 0.1505)/4 = 2.151\), so \(a = e^{2.151} = 8.60\).
Step 3. \(\hat y = 8.60\,e^{0.1505x}\). Each year multiplies the population by \(e^{0.1505} = 1.162\), a growth of about 16.2% a year; the fitted values 9.99, 11.61, 13.50, 15.69 match the data.
Form: \(Y = a X^b\). Take logs: \(\log Y = \log a + b \log X\). Let \(X' = \log X,\; Y' = \log Y\):
Apply linear least squares to \((X', Y')\).
Fit \(Y = a X^b\) to:
| x | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| y | 1 | 4 | 9 | 16 | 25 |
\(\log x\): 0, 0.301, 0.477, 0.602, 0.699; \(\log y\): 0, 0.602, 0.954, 1.204, 1.398.
Sums: \(\sum X' = 2.0792\), \(\sum Y' = 4.1584\), \(\sum X'^2 = 1.1693\), \(\sum X'Y' = 2.3386\), \(n = 5\). Then \(b = \dfrac{5(2.3386) - 2.0792(4.1584)}{5(1.1693) - 2.0792^2} = \dfrac{3.0469}{1.5234} = 2.000\) and \(A = (4.1584 - 2 \times 2.0792)/5 = 0\), so \(a = 10^0 = 1\) and \(Y = X^2\). ✓ (Every \(Y'\) is exactly \(2X'\), so the fit is exact.)
Allometric relation: weight \(W\) and length \(L\) of fish often follow \(W = aL^b\). Logs and least squares yield estimates of \(a\) and \(b\) (typically \(b \approx 3\) for isometric growth).
Eight problems, grouped by the curve fitted. Each one sets up the normal equations, builds the table of sums, solves the equations step by step and checks the result. Constants are carried at full precision and rounded only when written: a small rounding in a coefficient that multiplies a large \(x^2\) can move an estimate a long way.
Fit a straight line \(y = a + bx\) to:
| \(x\) | \(y\) | \(x^2\) | \(xy\) |
|---|---|---|---|
| 2 | 2 | 4 | 4 |
| 3 | 3 | 9 | 9 |
| 5 | 4 | 25 | 20 |
| 7 | 6 | 49 | 42 |
| 8 | 6 | 64 | 48 |
| 10 | 8 | 100 | 80 |
| 12 | 9 | 144 | 108 |
| 13 | 10 | 169 | 130 |
| 60 | 48 | 564 | 441 |
Normal equations with \(n = 8\), \(\sum x = 60\):
\[ 48 = 8a + 60b \quad\text{(i)}, \qquad 441 = 60a + 564b \quad\text{(ii)} . \]Eliminate \(a\). Multiply (i) by \(7.5\): \(360 = 60a + 450b\). Subtract from (ii): \(81 = 114b\), so \(b = 0.7105\) \(= 27/38\).
Back-substitute. From (i), \(8a = 48 - 60 \times 0.7105 = 5.3684\), so \(a = 0.6711\).
Result. \(\hat y = 0.6711 + 0.7105x\). Check: \(\bar x = 7.5\), \(\bar y = 6\), and \(0.6711 + 0.7105 \times 7.5 = 6\): the line passes through \((\bar x, \bar y)\), as it must.
Fit a straight line \(y = a + bx\) to:
| \(x\) | \(y\) | \(x^2\) | \(xy\) |
|---|---|---|---|
| 1 | 7 | 1 | 7 |
| 3 | 12 | 9 | 36 |
| 5 | 16 | 25 | 80 |
| 7 | 22 | 49 | 154 |
| 9 | 25 | 81 | 225 |
| 11 | 31 | 121 | 341 |
| 36 | 113 | 286 | 843 |
Normal equations with \(n = 6\), \(\sum x = 36\):
\[ 113 = 6a + 36b \quad\text{(i)}, \qquad 843 = 36a + 286b \quad\text{(ii)} . \]Eliminate \(a\). Multiply (i) by \(6\): \(678 = 36a + 216b\). Subtract from (ii): \(165 = 70b\), so \(b = 2.3571\) \(= 33/14\).
Back-substitute. From (i), \(6a = 113 - 36 \times 2.3571 = 28.1429\), so \(a = 4.6905\).
Result. \(\hat y = 4.6905 + 2.3571x\). Check: \(\bar x = 6\), \(\bar y = 18.8333\), and \(4.6905 + 2.3571 \times 6 = 18.833\): the line passes through \((\bar x, \bar y)\), as it must.
Fit a straight line \(y = a + bx\) and estimate \(y\) at \(x = 7\) to:
| \(x\) | \(y\) | \(x^2\) | \(xy\) |
|---|---|---|---|
| 1 | 22 | 1 | 22 |
| 2 | 48 | 4 | 96 |
| 3 | 71 | 9 | 213 |
| 4 | 88 | 16 | 352 |
| 5 | 99 | 25 | 495 |
| 6 | 104 | 36 | 624 |
| 21 | 432 | 91 | 1802 |
Normal equations with \(n = 6\), \(\sum x = 21\):
\[ 432 = 6a + 21b \quad\text{(i)}, \qquad 1802 = 21a + 91b \quad\text{(ii)} . \]Eliminate \(a\). Multiply (i) by \(3.5\): \(1512 = 21a + 73.5b\). Subtract from (ii): \(290 = 17.5b\), so \(b = 16.5714\) \(= 116/7\).
Back-substitute. From (i), \(6a = 432 - 21 \times 16.5714 = 84\), so \(a = 14\).
Result. \(\hat y = 14 + 16.5714x\). Check: \(\bar x = 3.5\), \(\bar y = 72\), and \(14 + 16.5714 \times 3.5 = 72\): the line passes through \((\bar x, \bar y)\), as it must.
At \(x = 7\): \(\hat y = 14 + 16.5714 \times 7 = 130\).
A caution. \(x = 7\) lies outside the data, and the data are levelling off (88, 99, 104). The residuals \(-8.6, 0.9, 7.3, 7.7, 2.1, -9.4\) run negative, positive, negative: the points lie below the line at both ends and above it in the middle, the sign of a curve. A parabola fitted to the same data, \(\hat y = -11.5 + 35.696x - 2.732x^2\), has a residual sum of squares of 1.6 against the line's 280.3, and estimates 104.5 at \(x = 7\). Fig 1.2 shows both.
Fit \(y = a + bx + cx^2\) to:
| \(x\) | \(y\) | \(x^2\) | \(x^3\) | \(x^4\) | \(xy\) | \(x^2y\) |
|---|---|---|---|---|---|---|
| 0 | 2.1 | 0 | 0 | 0 | 0 | 0 |
| 1 | 1.6 | 1 | 1 | 1 | 1.6 | 1.6 |
| 2 | 2 | 4 | 8 | 16 | 4 | 8 |
| 3 | 3.2 | 9 | 27 | 81 | 9.6 | 28.8 |
| 4 | 5.3 | 16 | 64 | 256 | 21.2 | 84.8 |
| 10 | 14.2 | 30 | 100 | 354 | 36.4 | 123.2 |
Result. \(\hat y = 2.0971 - 0.9143x + 0.4286x^2\) (coefficients to four decimals). Check: the fitted values 2.1, 1.61, 1.98, 3.21, 5.3 add to 14.2, the same as \(\sum y\), as the first normal equation requires.
Fit a parabola to a car's fuel use \(y\) (litres per 100 km) at speed \(x\) km/hr, and estimate the fuel use at 150 km/hr.
| Speed \(x\) | 30 | 50 | 70 | 90 | 110 | 130 |
|---|---|---|---|---|---|---|
| Fuel \(y\) | 6.8 | 6.1 | 6.3 | 7.2 | 8.9 | 11.4 |
Code the speeds. With \(u = (x - 80)/10\) the speeds become \(u = -5, -3, -1, 1, 3, 5\), so \(\sum u = \sum u^3 = 0\) and the three normal equations nearly separate:
| \(u\) | −5 | −3 | −1 | 1 | 3 | 5 | Total |
| \(uy\) | −34 | −18.3 | −6.3 | 7.2 | 26.7 | 57 | 32.3 |
| \(u^2y\) | 170 | 54.9 | 6.3 | 7.2 | 80.1 | 285 | 603.5 |
With \(\sum u^2 = 70\) and \(\sum u^4 = 1414\), the normal equations for \(y = A + Bu + Cu^2\) are
\[ 46.7 = 6A + 70C, \qquad 32.3 = 70B, \qquad 603.5 = 70A + 1414C . \]Back to \(x\). Substituting \(u = (x - 80)/10\) and collecting powers of \(x\): \(a = A - 8B + 64C = 9.2318\), \(b = B/10 - 1.6C = -0.111\), \(c = C/100 = 0.0009821\). So
\[ \hat y = 9.2318 - 0.111x + 0.0009821x^2 , \]which reproduces the data closely (6.79, 6.14, 6.27, 7.2, 8.91, 11.4).
Estimate at 150 km/hr (\(u = 7\)): \(A + 7B + 49C = 6.6375 + 3.23 + 4.8125 = 14.68\) litres per 100 km. Because \(c\) multiplies \(150^2 = 22\,500\), rounding it to two significant figures would move the estimate by about 0.05. And 150 km/hr is beyond the fastest speed observed, so the estimate assumes the parabola continues.
Fit \(y = ax^b\) to:
| \(x\) | \(y\) | \(X = \log x\) | \(Y = \log y\) | \(X^2\) | \(XY\) |
|---|---|---|---|---|---|
| 1 | 820 | 0.0000 | 2.9138 | 0.0000 | 0.0000 |
| 2 | 610 | 0.3010 | 2.7853 | 0.0906 | 0.8384 |
| 3 | 430 | 0.4771 | 2.6335 | 0.2276 | 1.2564 |
| 4 | 160 | 0.6021 | 2.2041 | 0.3625 | 1.3271 |
| 5 | 95 | 0.6990 | 1.9777 | 0.4886 | 1.3824 |
| 6 | 40 | 0.7782 | 1.6021 | 0.6056 | 1.2468 |
| Total | 2.8574 | 14.1165 | 1.7749 | 6.0511 |
Normal equations for \(Y = A + bX\), \(A = \log a\):
\[ 14.1165 = 6A + 2.8574b, \qquad 6.0511 = 2.8574A + 1.7749b . \]Solve. \(b = \dfrac{6(6.0511) - 2.8574(14.1165)}{6(1.7749) - 2.8574^2} = \dfrac{-4.0299}{2.4847} = -1.6219\), and \(A = (14.1165 - (-1.6219)(2.8574))/6 = 3.1252\).
Result. \(a = \text{antilog}(3.1252) \approx 1334\), so \(\hat y = 1334\,x^{-1.622}\).
How good is the fit? The fitted values are 1334, 433, 225, 141, 98, 73 against 820, 610, 430, 160, 95, 40: too high at both ends and too low in the middle (Fig 1.3). The calculation is right, but a power law does not describe these data well. Plotting \(\log y\) against \(\log x\) first, to see whether the points lie near a line, would have shown that.
Fit \(y = ab^x\) to:
| \(x\) | \(y\) | \(Y = \log y\) | \(x^2\) | \(xY\) |
|---|---|---|---|---|
| 1 | 1.4 | 0.1461 | 1 | 0.1461 |
| 2 | 1.9 | 0.2788 | 4 | 0.5576 |
| 3 | 2.5 | 0.3979 | 9 | 1.1937 |
| 4 | 3.6 | 0.5563 | 16 | 2.2252 |
| 5 | 4.8 | 0.6812 | 25 | 3.4060 |
| 6 | 6.7 | 0.8261 | 36 | 4.9566 |
| 7 | 9.0 | 0.9542 | 49 | 6.6794 |
| 8 | 12.6 | 1.1004 | 64 | 8.8032 |
| 36 | 4.9410 | 204 | 27.9678 |
Normal equations for \(Y = A + Bx\), \(A = \log a\), \(B = \log b\): \(4.9410 = 8A + 36B\) and \(27.9678 = 36A + 204B\).
Solve. \(B = \dfrac{8(27.9678) - 36(4.9410)}{8(204) - 36^2} = \dfrac{45.8664}{336} = 0.13651\), and \(A = (4.9410 - 36 \times 0.13651)/8 = 0.0033\).
Result. \(a = \text{antilog}(0.0033) = 1.008\) and \(b = \text{antilog}(0.13651) = 1.369\), so \(\hat y = 1.008\,(1.369)^x\): \(y\) grows by about 37% for each unit of \(x\). The fitted values (1.38, 1.89, 2.59, 3.54, 4.85, 6.64, 9.10, 12.46) follow the data closely.
Fit \(y = ae^{bx}\) to:
| \(x\) | \(y\) | \(Y = \log y\) | \(x^2\) | \(xY\) |
|---|---|---|---|---|
| 1 | 2.3 | 0.3617 | 1 | 0.3617 |
| 2 | 5.1 | 0.7076 | 4 | 1.4152 |
| 3 | 12.2 | 1.0864 | 9 | 3.2592 |
| 4 | 27.9 | 1.4456 | 16 | 5.7824 |
| 5 | 66 | 1.8195 | 25 | 9.0975 |
| 6 | 150 | 2.1761 | 36 | 13.0566 |
| 21 | 7.5969 | 91 | 32.9726 |
Normal equations for \(Y = A + Bx\), with \(A = \log a\) and \(B = b\log_{10} e\):
\[ 7.5969 = 6A + 21B, \qquad 32.9726 = 21A + 91B . \]Solve. \(B = \dfrac{6(32.9726) - 21(7.5969)}{6(91) - 21^2} = \dfrac{38.3007}{105} = 0.36477\), and \(A = (7.5969 - 21 \times 0.36477)/6 = -0.0105\).
Back-transform. \(a = \text{antilog}(-0.0105) = 0.976\) and \(b = B/\log_{10} e = 0.36477/0.43429 = 0.840\). So \(\hat y = 0.976\,e^{0.840x}\): the fitted values are 2.3, 5.2, 12.1, 28.1, 65.1, 150.7. Natural logarithms give the same \(a\) and \(b\) directly.