4 Curve Fitting And Regression

Numerical Methods · Unit 4

Curve Fitting and Regression

Exam-focused notes for Curve Fitting and Regression (Numerical Methods, BIT203): what the TU syllabus asks and how it has actually been tested, with 5 solved past questions from this unit.

Curve Fitting and Regression in short

Measured data usually contain small errors, so forcing a curve through every point is not sensible. Curve fitting finds a simple curve that follows the general trend of the data as closely as possible.

Curve Fitting and Regression: notes

Least squares method for function fitting

Suppose we have data points $(x_i, y_i)$ for $i = 1, \ldots, n$ and a model $y = f(x)$ with unknown constants. For each point the residual is $e_i = y_i - f(x_i)$. The least squares method chooses the constants that make the sum of squared residuals smallest.

$$S = \sum_{i=1}^{n} \left[y_i - f(x_i)\right]^2$$

We set the partial derivative of $S$ with respect to each constant to zero. This gives the normal equations. Squaring makes all residuals positive, so errors above and below the curve do not cancel.

Straight line $y = a + bx$. The normal equations are

$$\sum y = na + b\sum x, \qquad \sum xy = a\sum x + b\sum x^2$$

Example. Fit a line to $(1,2), (2,4), (3,5), (4,4), (5,5)$. The sums are $\sum x = 15$, $\sum y = 20$, $\sum xy = 66$, $\sum x^2 = 55$.

$$\begin{aligned} b &= \frac{n\sum xy - \sum x \sum y}{n\sum x^2 - (\sum x)^2} = \frac{5(66) - 15(20)}{5(55) - 225} = \frac{30}{50} = 0.6 \ a &= \frac{\sum y - b\sum x}{n} = \frac{20 - 9}{5} = 2.2 \end{aligned}$$

The line is $y = 2.2 + 0.6x$.

What this unit covers

  • Least squares method for function fitting
  • Quadratic polynomial fitting
  • Exponential curve fitting
  • Regression versus interpolation
  • Fitting algorithms and applications

Exponential curve fitting

2082 · 5 marks

Fit the exponential curve $y = ae^{bx}$ for (1,15), (2,22), (3,33), (4,48), (5,70) using least square method. [5]

Points: $(1,15), (2,22), (3,33), (4,48), (5,70)$, with $n = 5$. Taking natural log: $$\ln y = \ln a + bx$$ Let $Y = \ln y$, $A = \ln a$. Then $Y = A + bx$ (linear). Normal equations: $$\sum Y = nA + b\sum x$$ $$\sum xY = A\sum x + b\sum x^2$$ $x$ $y$ $Y = \ln y$ $x^2$ $xY$ --------------- 1 15 2....

Open this answer on its own page →

2078 · 5 marks

The temperature of a metal strip was measured at various time intervals during heating and the values are given in the table below. If the relation between the time 't' and temperature 'T' is of the form: $T = be^{t/4} + a$. Estimate the temperature at t = 6 minute.

Time ('t' min)1234
Temp ('T' °C)7083100124

[5]

Relation: $T = be^{t/4} + a$ t (min) 1 2 3 4 --------------- T (°C) 70 83 100 124 Estimate: T at t = 6. Substitute $u = e^{t/4}$, giving the linear form $T = a + bu$. Compute u values: t T $u = e^{t/4}$ --------- 1 70 $e^{0.25} = 1.28403$ 2 83 $e^{0.50} = 1.64872$ 3 100 $e^{0.75} = 2.11700$ 4 124...

Open this answer on its own page →

Quadratic polynomial fitting

2080 · 5 marks

Fit a second order polynomial to the data in the table below:

$$\begin{array}{|c|c|c|c|c|c|}\hline X & 1 & 2 & 3 & 4 & 5 \ \hline F(x) & 2 & 6 & 12 & 20 & 30 \ \hline \end{array}$$

[5]

X 1 2 3 4 5 ------------------ f(X) 2 6 12 20 30 Model: $f(x) = a0 + a1 x + a2 x^2$, with $n = 5$. x f x² x³ x⁴ xf x²f --------------------------- 1 2 1 1 1 2 2 2 6 4 8 16 12 24 3 12 9 27 81 36 108 4 20 16 64 256 80 320 5 30 25 125 625 150 750 Σ 70 55 225 979 280 1204

Open this answer on its own page →

0 · 5 marks

Fit the quadratic curve through the following data points and estimate the value of f(x) at x=2.

$$\begin{array}{|c|c|c|c|c|c|}\hline x & 1 & 3 & 4 & 5 & 6 \ \hline y & 2 & 7 & 8 & 7 & 5 \ \hline \end{array}$$

[5]

$x$ 1 3 4 5 6 -------------------- $y$ 2 7 8 7 5 $n = 5$. Fit $y = a0 + a1 x + a2 x^2$, estimate $f(2)$. $x$ $y$ $x^2$ $x^3$ $x^4$ $xy$ $x^2y$ --------------------------------------------- 1 2 1 1 1 2 2 3 7 9 27 81 21 63 4 8 16 64 256 32 128 5 7 25 125 625 35 175 6 5 36 216 1296 30 180 Totals: - ...

Open this answer on its own page →

Least squares method for function fitting

2079 · 5 marks

What is least squares method of fitting a function? Fit the second order polynomial for the following data values.

x1234567
y2678101215

[5]

Show solved answer

Least Squares Method and Second Order Polynomial Fitting

Definition

The least squares method finds the best-fit curve for a set of data points by minimizing the sum of squares of residuals (differences between observed values $y_i$ and fitted values $f(x_i)$):

$$S = \sum_{i=1}^{n} [y_i - f(x_i)]^2 \to \text{minimum}$$

Given Data

$$x: 1, 2, 3, 4, 5, 6, 7$$ $$y: 2, 6, 7, 8, 10, 12, 15$$ $$n = 7$$

Model and Normal Equations

Fit $y = a_0 + a_1 x + a_2 x^2$. The normal equations are:

$$\sum y = na_0 + a_1\sum x + a_2\sum x^2$$ $$\sum xy = a_0\sum x + a_1\sum x^2 + a_2\sum x^3$$ $$\sum x^2y = a_0\sum x^2 + a_1\sum x^3 + a_2\sum x^4$$

Calculation Table

$x$$y$$x^2$$x^3$$x^4$$xy$$x^2y$
1211122
2648161224
37927812163
48166425632128
5102512562550250
61236216129672432
715493432401105735
$\Sigma$6014078446762941634

Also $\sum x = 28$.

Substituting

$$60 = 7a_0 + 28a_1 + 140a_2 \quad (1)$$ $$294 = 28a_0 + 140a_1 + 784a_2 \quad (2)$$ $$1634 = 140a_0 + 784a_1 + 4676a_2 \quad (3)$$

Note: The coefficient of $a_0$ in equation (1) must be $\sum x = 28$, while in equation (3) the $a_0$ coefficient is $\sum x^2 = 140$; mixing the two is an easy slip to make.

Solving

Eliminate $a_0$. Multiply (1) by 4 and subtract from (2):

$(2) - 4\times(1)$: $$294 - 240 = (28-28)a_0 + (140-112)a_1 + (784-560)a_2$$ $$54 = 28a_1 + 224a_2 \quad (4)$$

Multiply (1) by 20 and subtract from (3):

$(3) - 20\times(1)$: $$1634 - 1200 = (140-140)a_0 + (784-560)a_1 + (4676-2800)a_2$$ $$434 = 224a_1 + 1876a_2 \quad (5)$$

Eliminate $a_1$. Multiply (4) by 8: $$432 = 224a_1 + 1792a_2 \quad (4')$$

$(5) - (4')$: $$434 - 432 = (1876 - 1792)a_2$$ $$2 = 84a_2 \implies a_2 = 0.02381$$

From (4): $$54 = 28a_1 + 224(0.02381) = 28a_1 + 5.333$$ $$28a_1 = 48.667 \implies a_1 = 1.7381$$

From (1): $$60 = 7a_0 + 28(1.7381) + 140(0.02381)$$ $$60 = 7a_0 + 48.667 + 3.333$$ $$7a_0 = 8.0 \implies a_0 = 1.1429$$

Result

$$\boxed{y = 1.1429 + 1.7381x + 0.0238x^2}$$

Verification (at $x=4$): $1.1429 + 6.9524 + 0.381 = 8.476 \approx 8$ ✓ (at $x=7$): $1.1429 + 12.167 + 1.167 = 14.48 \approx 15$ ✓

Open this answer on its own page →