ECEG2102 CM Notes - Ch4
ECEG2102 CM Notes - Ch4
Page 1 of 15
List of Figures
Figure 1: Least Squares Fit V/s Single Polynomial Interpolation .................................................................................... 4
Figure 2: Linear Fit and Error ............................................................................................................................................ 4
Figure 3: Best Fit using true errors................................................................................................................................... 5
Figure 4: Best Fit using absolute errors ........................................................................................................................... 5
Figure 5: Common Functions to linearize Non-linear data (a,d) Exponential (b,e) Power (c,f) Saturation ....... 6
Figure 6: Example 1 : Best-fit curve for Non-linear data ................................................................................................. 7
Figure 7: Example 1 : Best-fit curve for Non-linear data with Saturation Function ....................................................... 7
Figure 8: Example 2 : Best-fit curve for Non-linear data ................................................................................................. 8
Figure 9: Example 2 : Best-fit curve for Non-linear data using Power Function ............................................................ 8
Figure 10: Example 2 : Verification of Best-fit curve for Non-linear data Power Function ........................................... 8
Figure 11: Example 3 : Best-fit curve for Non-linear data using Quadratic Polynomial Function ............................... 11
Figure 12: Example 4 : Comparing Best-fit curve for Non-linear data using Quadratic and Cubic Polynomial
Functions ........................................................................................................................................................................ 12
Figure 13: Lagrange’s Interpolating Polynomials (a) First-degree Polynomial (b) Second-degree Polynomial .......... 12
Page 2 of 15
List of Tables
Table 1 : Example 1 : Best fit for Non-linear data ............................................................................................................. 8
Table 2 : Example 2 : Best fit for Non-linear data using Power Function ......................................................................... 9
Table 3 : Example : Best fit for Non-linear data using Second Degree Polynomial ........................................................ 10
Table 4 : Example : Best fit for Non-linear data using Third Degree Polynomial ............................................................ 11
Table 5 : Divided Differences Table for five data points ................................................................................................. 14
Table 6 : Divided Differences Table for example problem ............................................................................................. 14
Page 3 of 15
Chapter 4
4.1. Approximation
Curve-fitting is a procedure where a function is used to fit a given set of data in the “best” possible way without having to match the data
exactly. As a result, while the function does not necessarily yield the exact value at any of the data points, overall it fits the set of data well.
Several types of functions and polynomials of different orders can be used for curve fitting purposes.
As mentioned above, a single polynomial may be sufficient for the interpolation of a small set of data. However, when the data has substantial
error, even if the size of data is small, this may no longer be appropriate. Consider Figure 1, which shows a set of seven data points collected
from an experiment. The nature of the data suggests that for the most part, the y values increase with the x values. A single interpolating
polynomial goes through all of the data points, but displays large oscillations in some of the intervals. As a result, the interpolated values near
𝑥 = 1.2, 𝑥 = 2.5 and 𝑥 = 2.9 will be well outside of the range of the original data.
Figure 1: Least Squares Fit V/s Single Polynomial Interpolation Figure 2: Linear Fit and Error
In these types of situations, it makes more sense to find a function that does not necessarily go through all of the data points, but fits the data
well overall. One option, for example, is to fit the “best” straight line into the data. This line is not random and can be generated systematically
via Least-Squares Regression.
Linear Regression
The simplest case of a least-squares regression involves finding a straight line (linear function) in the form:
𝒚 = 𝒂𝟏 𝒙 + 𝒂𝟎
that best fits a set of 𝑛 data points (𝑥1 , 𝑦1 ), (𝑥2 , 𝑦2 ), … , (𝑥𝑛 , 𝑦𝑛 ). Of course, the data first needs to be plotted to see whether the
independent and dependent variables have a somewhat linear relationship. If this is the case, then the coefficients 𝑎1 and 𝑎0 are determined
such that the error associated with the line is minimized. As shown in Figure 2, at each data point (𝑥𝑖 , 𝑦𝑖 ) the error
𝑒𝑖 is defined as the difference between the true value 𝑦𝑖 and the approximate value 𝑎1 𝑥𝑖 + 𝑎0 :
𝒆𝒊 = 𝒚𝒊 − (𝒂𝟏 𝒙𝒊 + 𝒂𝟎 )
𝑬 = ∑ 𝒆𝒊 = ∑ 𝒚𝒊 − (𝒂𝟏 𝒙𝒊 + 𝒂𝟎 )
𝒊=𝟏 𝒊=𝟏
Page 4 of 15
Figure 3: Best Fit using true errors Figure 4: Best Fit using absolute errors
This criterion, however, does not offer a good measure of how well the line fits the data because, as shown in Figure 3, it allows for positive
and negative individual errors—even very large errors—to cancel out and yield a zero sum (observe that, in Figure 3, 𝑒1 = −𝑒3 and
𝑒2 = −𝑒4 .)
𝑬 = ∑ 𝒆𝒊 𝟐 = ∑[ 𝒚𝒊 − (𝒂𝟏 𝒙𝒊 + 𝒂𝟎 )]𝟐
𝒊=𝟏 𝒊=𝟏
Noting that 𝐸 is a (nonlinear) function of 𝑎1 and 𝑎0 , it attains its minimum where 𝜕𝐸/𝜕𝑎0 and 𝜕𝐸/𝜕𝑎1 vanish, that is,
𝒏 𝒏
𝝏𝑬
= 𝟎 ⇒ −𝟐 ∑[ 𝒚𝒊 − (𝒂𝟏 𝒙𝒊 + 𝒂𝟎 )] ⇒ ∑[ 𝒚𝒊 − (𝒂𝟏 𝒙𝒊 + 𝒂𝟎 )] = 𝟎
𝝏𝒂𝟎
𝒊=𝟏 𝒊=𝟏
𝒏 𝒏
𝝏𝑬
= 𝟎 ⇒ −𝟐 ∑{𝒙𝒊 [ 𝒚𝒊 − (𝒂𝟏 𝒙𝒊 + 𝒂𝟎 )]} ⇒ ∑{𝒙𝒊 [ 𝒚𝒊 − (𝒂𝟏 𝒙𝒊 + 𝒂𝟎 )]} = 𝟎
𝝏𝒂𝟏
𝒊=𝟏 𝒊=𝟏
Expanding and rearranging the above equations yields a system of two linear equations to be solved for 𝑎1 and 𝑎0 :
𝒏 𝒏
𝒏𝒂𝟎 + (∑ 𝒙𝒊 ) 𝒂𝟏 = ∑ 𝒚𝒊
𝒊=𝟏 𝒊=𝟏
𝒏 𝒏 𝒏
(∑ 𝒙𝒊 ) 𝒂𝟎 + (∑ 𝒙𝒊 𝟐 ) 𝒂𝟏 = ∑ 𝒙𝒊 𝒚𝒊
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
By Cramer’s rule, the Coefficients of Linear Regression are found as
𝒏(∑𝒏𝒊=𝟏 𝒙𝒊 𝒚𝒊 ) − (∑𝒏𝒊=𝟏 𝒙𝒊 )(∑𝒏𝒊=𝟏 𝒚𝒊 )
𝒂𝟏 =
𝒏(∑𝒏𝒊=𝟏 𝒙𝒊 𝟐 ) − (∑𝒏𝒊=𝟏 𝒙𝒊 )𝟐
𝒏(∑𝒊=𝟏 𝒙𝒊 𝟐 )(∑𝒏𝒊=𝟏 𝒚𝒊 ) − (∑𝒏𝒊=𝟏 𝒙𝒊 )(∑𝒏𝒊=𝟏 𝒙𝒊 𝒚𝒊 )
𝒏
𝒂𝟎 =
𝒏(∑𝒏𝒊=𝟏 𝒙𝒊 𝟐 ) − (∑𝒏𝒊=𝟏 𝒙𝒊 )𝟐
Example : Using least-squares regression, find a straight line that best fits the following data : (0.2 , 8.0), (0.4 , 8.4), (0.6 , 8.8), (0.8 , 8.6),
(1.0 , 8.5), (1.2 , 8.7).
Page 5 of 15
Solution : First, let us calculate all the sums :
𝟔 𝟔
∑ 𝒙𝒊 = ∑( 𝟎. 𝟐 + 𝟎. 𝟒 + 𝟎. 𝟔 + 𝟎. 𝟖 + 𝟏. 𝟎 + 𝟏. 𝟐) = 𝟒. 𝟐 ∑ 𝒚𝒊 = ∑( 𝟖. 𝟎 + 𝟖. 𝟒 + 𝟖. 𝟖 + 𝟖. 𝟔 + 𝟖. 𝟓 + 𝟖. 𝟕) = 𝟓𝟏. 𝟎
𝒊=𝟏 𝒊=𝟏
𝟔
∑ 𝒙𝒊 𝟐 = ∑( 𝟎. 𝟐𝟐 + 𝟎. 𝟒𝟐 + 𝟎. 𝟔𝟐 + 𝟎. 𝟖𝟐 + 𝟏. 𝟎𝟐 + 𝟏. 𝟐𝟐 ) = 𝟑. 𝟔𝟒
𝒊=𝟏
𝟔
∑ 𝒙𝒊 𝒚𝒊 = ∑[(𝟎. 𝟐)(𝟖. 𝟎) + (𝟎. 𝟒)(𝟖. 𝟒) + (𝟎. 𝟔)(𝟖. 𝟖) + (𝟎. 𝟖)(𝟖. 𝟔) + (𝟏. 𝟎)(𝟖. 𝟓) + (𝟏. 𝟐)(𝟖. 𝟕)] = 𝟑𝟔. 𝟎𝟔
𝒊=𝟏
Now,
(𝟔)(𝟑𝟔. 𝟎𝟔) − (𝟒. 𝟐)(𝟓𝟏) (𝟔)(𝟑. 𝟔𝟒)(𝟓𝟏) − (𝟒. 𝟐)(𝟑𝟔. 𝟎𝟔)
𝒂𝟏 = = 𝟎. 𝟓𝟏𝟒𝟑 𝒂𝟎 = = 𝟖. 𝟏𝟒
(𝟔)(𝟑. 𝟔𝟒) − (𝟒. 𝟐)𝟐 (𝟔)(𝟑. 𝟔𝟒) − (𝟒. 𝟐)𝟐
Therefore, the line that best fits the data is described by 𝑦 = 0.5143𝑥 + 8.14
In general, non-linear data fits into one of the three curve-fitting functions, as follows.
Exponential Function
One is the exponential function :
𝒚 = 𝒂𝒆𝒃𝒙 (𝑎, 𝑏 𝑎𝑟𝑒 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡𝑠)
Because differentiation of the exponential function returns a constant multiple of the exponential function, this technique applies to situations
where the rate of change of a quantity is directly proportional to the quantity itself; for instance, radioactive decay.
Conversion into linear form is made by taking the natural logarithm on both sides of equation to obtain
𝐥𝐧 𝒚 = 𝒃𝒙 + 𝐥𝐧 𝒂
Therefore, the plot of ln 𝑦 versus 𝑥 is a straight line with slope 𝑏 and intercept ln 𝑎 (see Figure 5 (a) and (d).)
Figure 5: Common Functions to linearize Non-linear data (a,d) Exponential (b,e) Power (c,f) Saturation
Power Function
Another strategy to fit nonlinear function is the power function
Page 6 of 15
𝒚 = 𝒂𝒙𝒃 (𝑎, 𝑏 𝑎𝑟𝑒 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡𝑠)
Linearization is achieved by taking the common logarithm (base 10) on both sides of equation:
𝐥𝐨𝐠 𝒚 = 𝒃 𝐥𝐨𝐠 𝒙 + 𝐥𝐨𝐠 𝒂
so that the plot of log 𝑦 versus log 𝑥 is a straight line with slope 𝑏 and intercept log 𝑎 (see Figure 5 (b) and (e).)
Saturation Function
The saturation function is in the form
𝒙
𝒚= (𝑎, 𝑏 𝑎𝑟𝑒 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡𝑠)
𝒂𝒙 + 𝒃
Inverting both sides of equation yields
𝟏 𝟏
= 𝒃( )+𝒂
𝒚 𝒙
so that the plot of 1⁄𝑦 versus 1⁄𝑥 is a straight line with slope 𝑏 and intercept 𝑎 (see Figure 5 (c) and (f).)
Example : Find the best fit curve for the following data : (10 , 1.9), (20 , 3.0), (30 , 3.2), (40 , 3.9), (50 , 3.7), (60 , 4.2) , (70 , 4.1), (80 , 4.4),
(90 , 4.5), (100 , 4.4).
Solution : To find the best fit it is best to plot the given data. It is shown in Figure 6.
Figure 6: Example 1 : Best-fit curve for Non-linear data Figure 7: Example 1 : Best-fit curve for Non-linear data with
Saturation Function
Looking at the graph intuition tells us that the function that best fits this data should be Saturation Function. But, let us not go through our
intuition, but rather try to fit this data in all three possible functions (viz. exponential, power, and saturation). To do that we should find
equivalent auxiliary data for given data (𝑥𝑖 , 𝑦𝑖 ) , 1 ≤ 𝑖 ≤ 10. Let us name these auxiliary data as follows: Exponential (𝑥𝑒𝑖 , 𝑦𝑒𝑖 ) =
(ln 𝑥𝑖 , ln 𝑦𝑖 ), Power (𝑥𝑝𝑖 , 𝑦𝑝𝑖 ) = (log 𝑥𝑖 , log 𝑦𝑖 ), and Saturation (𝑥𝑠𝑖 , 𝑦𝑠𝑖 ) = (1⁄𝑥𝑖 , 1⁄𝑦𝑖 ). After producing auxiliary data it is easy to
find best fit linear curve for these auxiliary data:
Exponential : 𝒚𝒆𝒊 = 𝒂𝒆𝟏 𝒙𝒆𝒊 + 𝒂𝒆𝟎 Power : 𝒚𝒑𝒊 = 𝒂𝒑𝟏 𝒙𝒑𝒊 + 𝒂𝒑𝟎 Saturation : 𝒚𝒔𝒊 = 𝒂𝒔𝟏 𝒙𝒔𝒊 + 𝒂𝒔𝟎
Finally, to find the most appropriate function we calculate the errors, and pick up the function which gives least error :
Exponential : Power : Saturation :
𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎
𝟐
𝑬𝒆 = ∑ 𝒆𝒆𝒊 𝟐 = ∑[ 𝒚𝒆𝒊 − (𝒂𝒆𝟏 𝒙𝒆𝒊 + 𝒂𝒆𝟎 )]𝟐 𝑬𝒑 = ∑ 𝒆𝒑𝒊 𝟐 = ∑[ 𝒚𝒑𝒊 − (𝒂𝒑𝟏 𝒙𝒑𝒊 + 𝒂𝒑𝟎 )] 𝑬𝒔 = ∑ 𝒆𝒔𝒊 𝟐 = ∑[ 𝒚𝒔𝒊 − (𝒂𝒔𝟏 𝒙𝒔𝒊 + 𝒂𝒔𝟎 )]𝟐
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
All these calculations are shown in Table 1. The remaining six quantities which are not shown in Table 1 are as follows:
(𝟏𝟎)(𝟓𝟎. 𝟖𝟏𝟕𝟕) − (𝟑𝟖. 𝟏𝟑𝟎𝟑)(𝟏𝟐. 𝟖𝟖𝟔𝟑) (𝟏𝟎)(𝟏𝟓𝟎. 𝟐𝟐𝟕𝟔)(𝟏𝟐. 𝟖𝟖𝟔𝟑) − (𝟑𝟖. 𝟏𝟑𝟎𝟑)(𝟓𝟎. 𝟖𝟏𝟕𝟕)
𝒂𝒆𝟏 = = 𝟎. 𝟑𝟒𝟕𝟖 𝒂𝒆𝟎 = = 𝟑𝟔𝟎. 𝟐𝟒𝟑𝟑
(𝟏𝟎)(𝟏𝟓𝟎. 𝟐𝟐𝟕𝟔) − (𝟑𝟖. 𝟏𝟑𝟎𝟑)𝟐 (𝟏𝟎)(𝟏𝟓𝟎. 𝟐𝟐𝟕𝟔) − (𝟑𝟖. 𝟏𝟑𝟎𝟑)𝟐
(𝟏𝟎)(𝟗. 𝟓𝟖𝟒𝟖) − (𝟏𝟔. 𝟓𝟓𝟗𝟖)(𝟓. 𝟓𝟗𝟔𝟒) (𝟏𝟎)(𝟐𝟖. 𝟑𝟑𝟒𝟕)(𝟓. 𝟓𝟗𝟔𝟒) − (𝟏𝟔. 𝟓𝟓𝟗𝟖)(𝟗. 𝟓𝟖𝟒𝟖)
𝒂𝒑𝟏 = = 𝟎. 𝟑𝟒𝟕𝟖 𝒂𝒑𝟎 = = 𝟏𝟓𝟔. 𝟒𝟓𝟏𝟕
(𝟏𝟎)(𝟐𝟖. 𝟑𝟑𝟒𝟕) − (𝟏𝟔. 𝟓𝟓𝟗𝟖)𝟐 (𝟏𝟎)(𝟐𝟖. 𝟑𝟑𝟒𝟕) − (𝟏𝟔. 𝟓𝟓𝟗𝟖)𝟐
(𝟏𝟎)(𝟎. 𝟏𝟎𝟔𝟔) − (𝟎. 𝟐𝟗𝟐𝟗)(𝟐. 𝟖𝟓𝟕𝟔) (𝟏𝟎)(𝟎. 𝟎𝟏𝟓𝟓)(𝟐. 𝟖𝟓𝟕𝟔) − (𝟎. 𝟐𝟗𝟐𝟗)(𝟎. 𝟏𝟎𝟔𝟔)
𝒂𝒔𝟏 = = 𝟑. 𝟑𝟎𝟓𝟐 𝒂𝒔𝟎 = = 𝟓. 𝟗𝟒𝟗𝟕
(𝟏𝟎)(𝟎. 𝟎𝟏𝟓𝟓) − (𝟎. 𝟐𝟗𝟐𝟗)𝟐 (𝟏𝟎)(𝟎. 𝟎𝟏𝟓𝟓) − (𝟎. 𝟐𝟗𝟐𝟗)𝟐
Hence, as is evident from the calculations, the given data fits best to a Saturation Function as follows:
𝟏 𝟏 𝒙
= 𝟑. 𝟑𝟎𝟓𝟐 + 𝟓. 𝟗𝟒𝟗𝟕 or 𝒚=
𝒚 𝒙 𝟓.𝟗𝟒𝟗𝟕𝒙+𝟑.𝟑𝟎𝟓𝟐
i 𝒙𝒊 𝒚𝒊 𝒙𝒆𝒊 𝒚𝒆𝒊 𝒙𝒑𝒊 𝒚𝒑𝒊 𝒙𝒔𝒊 𝒚𝒔𝒊 𝒙𝒆𝒊 𝟐 𝒙𝒆𝒊 𝒚𝒆𝒊 𝒙𝒑𝒊 𝟐 𝒙𝒑𝒊 𝒚𝒑𝒊 𝒙𝒔𝒊 𝟐 𝒙𝒔𝒊 𝒚𝒔𝒊 𝒆𝒆𝒊 𝟐 𝒆𝒑𝒊 𝟐 𝒆𝒔𝒊 𝟐
1 10 1.9 2.3026 0.6419 1.0000 0.2788 0.1000 0.5263 5.3019 1.4779 1.0000 0.2788 0.0100 0.0526 128984.6 24195.4 57.6
2 20 3.0 2.9957 1.0986 1.3010 0.4771 0.0500 0.3333 8.9744 3.2911 1.6927 0.6207 0.0025 0.0167 128368.3 24123.2 49.0
3 30 3.2 3.4012 1.1632 1.4771 0.5051 0.0333 0.3125 11.5681 3.9561 2.1819 0.7462 0.0011 0.0104 128326.1 24071.6 48.3
4 40 3.9 3.6889 1.3610 1.6021 0.5911 0.0250 0.2564 13.6078 5.0205 2.5666 0.9469 0.0006 0.0064 127896.6 24042.1 45.9
5 50 3.7 3.9120 1.3083 1.6990 0.5682 0.0200 0.2703 15.3039 5.1182 2.8865 0.9654 0.0004 0.0054 128095.3 24009.6 46.6
6 60 4.2 4.0943 1.4351 1.7782 0.6232 0.0167 0.2381 16.7637 5.8757 3.1618 1.1082 0.0003 0.0040 127782.9 23991.0 45.2
7 70 4.1 4.2485 1.4110 1.8451 0.6128 0.0143 0.2439 18.0497 5.9946 3.4044 1.1306 0.0002 0.0035 127892.8 23969.1 45.4
8 80 4.4 4.3820 1.4816 1.9031 0.6435 0.0125 0.2273 19.2022 6.4924 3.6218 1.2245 0.0002 0.0028 127711.5 23954.5 44.7
9 90 4.5 4.4998 1.5041 1.9542 0.6532 0.0111 0.2222 20.2483 6.7681 3.8191 1.2765 0.0001 0.0025 127669.3 23939.7 44.5
10 100 4.4 4.6052 1.4816 2.0000 0.6435 0.0100 0.2273 21.2076 6.8230 4.0000 1.2869 0.0001 0.0023 127767.0 23924.5 44.8
∑→ 38.1303 12.8863 16.5598 5.5964 0.2929 2.8576 150.2276 50.8177 28.3347 9.5848 0.0155 0.1066 1280494.3 240220.8 472.0
Example : Find a Power Function that best fits the following data : (10 , 1.9), (20 , 3.0), (30 , 3.2), (40 , 3.9), (50 , 3.7), (60 , 4.2) , (70 , 4.1),
(80 , 4.4), (90 , 4.5), (100 , 4.4).
Figure 9: Example 2 : Best-fit curve for Non-linear data using Figure 10: Example 2 : Verification of Best-fit curve for Non-linear
Power Function data Power Function
Auxiliary data as Power Function is calculated as follows: Power (𝑥𝑝𝑖 , 𝑦𝑝𝑖 ) = (log 𝑥𝑖 , log 𝑦𝑖 ). Using this auxiliary data the best fit linear
curve is found using Table 2. The linear coefficients of auxiliary equation are calculated as:
(𝟏𝟎)(𝟑. 𝟎𝟐𝟒𝟏) − (−𝟑. 𝟒𝟒𝟎𝟐)(−𝟑. 𝟓𝟒𝟗𝟏) (𝟏𝟎)(𝟐. 𝟎𝟗𝟓𝟔)(−𝟑. 𝟓𝟒𝟗𝟏) − (−𝟑. 𝟒𝟒𝟎𝟐)(𝟑. 𝟎𝟐𝟒𝟏)
𝒂𝒑𝟏 = = 𝟏. 𝟗𝟕𝟔𝟗 𝒂𝒑𝟎 = = 𝟎. 𝟑𝟐𝟓𝟐
(𝟏𝟎)(𝟐. 𝟎𝟗𝟓𝟔) − (𝟓. 𝟓)𝟐 (𝟏𝟎)(𝟐. 𝟎𝟗𝟓𝟔) − (𝟓. 𝟓)𝟐
Auxiliary Power Function : 𝒚𝒑𝒊 = 𝒂𝒑𝟏 𝒙𝒑𝒊 + 𝒂𝒑𝟎 = 𝟏. 𝟗𝟕𝟔𝟗𝒙𝒑𝒊 + 𝟎. 𝟑𝟐𝟓𝟐 Power Function : 𝒚 = 𝒆𝒂𝒑𝟎 𝒙𝒂𝒑𝟏 = 𝟐. 𝟏𝟏𝟒𝟓𝒙𝟏.𝟗𝟕𝟔𝟗
Page 8 of 15
Table 2 : Example 2 : Best fit for Non-linear data using Power Function
In the previous section, we learned that a curve can fit into nonlinear data by transforming the data into a form that can be handled by Linear
Regression. Another method is to fit polynomials of different orders to the data by means of Polynomial Regression.
The Linear Least-Squares Regression of previous section can be extended to fit a set of 𝑛 data points (𝑥1 , 𝑦1 ), (𝑥2 , 𝑦2 ), … , (𝑥𝑛 , 𝑦𝑛 ) with an
𝑚𝑡ℎ − 𝑑𝑒𝑔𝑟𝑒𝑒 polynomial in the form
𝒚 = 𝒂𝒎 𝒙𝒎 + 𝒂𝒎−𝟏 𝒙𝒎−𝟏 + ⋯ + 𝒂𝟐 𝒙𝟐 + 𝒂𝟏 𝒙 + 𝒂𝟎
with a total error
𝒏 𝒏
𝟐
𝑬 = ∑ 𝒆𝒊 = ∑[ 𝒚𝒊 − ( 𝒂𝒎 𝒙𝒊 𝒎 + 𝒂𝒎−𝟏 𝒙𝒊 𝒎−𝟏 + ⋯ + 𝒂𝟐 𝒙𝒊 𝟐 + 𝒂𝟏 𝒙𝒊 + 𝒂𝟎 ) ]
𝟐
𝒊=𝟏 𝒊=𝟏
The coefficients 𝑎𝑚 , 𝑎𝑚−1 , … , 𝑎2 , 𝑎1 , 𝑎0 are to be determined such that 𝐸 is minimized. A necessary condition for 𝐸 to attain a minimum is
that its partial derivative with respect to each of these coefficients vanishes, that is
𝒏
𝝏𝑬
= 𝟎 ⇒ −𝟐 ∑[𝒚𝒊 − ( 𝒂𝒎 𝒙𝒊 𝒎 + 𝒂𝒎−𝟏 𝒙𝒊 𝒎−𝟏 + ⋯ + 𝒂𝟐 𝒙𝒊 𝟐 + 𝒂𝟏 𝒙𝒊 + 𝒂𝟎 )] = 𝟎
𝝏𝒂𝟎
𝒊=𝟏
𝒏
𝝏𝑬
= 𝟎 ⇒ −𝟐 ∑{𝒙𝒊 [𝒚𝒊 − ( 𝒂𝒎 𝒙𝒊 𝒎 + 𝒂𝒎−𝟏 𝒙𝒊 𝒎−𝟏 + ⋯ + 𝒂𝟐 𝒙𝒊 𝟐 + 𝒂𝟏 𝒙𝒊 + 𝒂𝟎 )]} = 𝟎
𝝏𝒂𝟏
𝒊=𝟏
𝒏
𝝏𝑬
= 𝟎 ⇒ −𝟐 ∑{𝒙𝒊 𝟐 [𝒚𝒊 − ( 𝒂𝒎 𝒙𝒊 𝒎 + 𝒂𝒎−𝟏 𝒙𝒊 𝒎−𝟏 + ⋯ + 𝒂𝟐 𝒙𝒊 𝟐 + 𝒂𝟏 𝒙𝒊 + 𝒂𝟎 )]} = 𝟎
𝝏𝒂𝟐
𝒊=𝟏
⋮ ⇒ ⋮ = ⋮
𝒏
𝝏𝑬
= 𝟎 ⇒ −𝟐 ∑{𝒙𝒊 𝒎 [𝒚𝒊 − ( 𝒂𝒎 𝒙𝒊 𝒎 + 𝒂𝒎−𝟏 𝒙𝒊 𝒎−𝟏 + ⋯ + 𝒂𝟐 𝒙𝒊 𝟐 + 𝒂𝟏 𝒙𝒊 + 𝒂𝟎 )]} = 𝟎
𝝏𝒂𝒎
𝒊=𝟏
Manipulation of these equations yields a system of 𝑚 + 1 linear equations to be solved for 𝑎𝑚 , 𝑎𝑚−1 , … , 𝑎2 , 𝑎1 , 𝑎0 :
𝒏 𝒏 𝒏 𝒏 𝒏
(∑ 𝒙𝒊 𝟎 ) 𝒂𝟎 + (∑ 𝒙𝒊 𝟏 ) 𝒂𝟏 + (∑ 𝒙𝒊 𝟐 ) 𝒂𝟐 + ⋯ + (∑ 𝒙𝒊 𝒎+𝟎 ) 𝒂𝒎 = ∑ 𝒙𝒊 𝟎 𝒚𝒊
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
𝒏 𝒏 𝒏 𝒏 𝒏
(∑ 𝒙𝒊 𝟏 ) 𝒂𝟎 + (∑ 𝒙𝒊 𝟐 ) 𝒂𝟏 + (∑ 𝒙𝒊 𝟑 ) 𝒂𝟐 + ⋯ + (∑ 𝒙𝒊 𝒎+𝟏 ) 𝒂𝒎 = ∑ 𝒙𝒊 𝟏 𝒚𝒊
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
𝒏 𝒏 𝒏 𝒏 𝒏
(∑ 𝒙𝒊 𝟐 ) 𝒂𝟎 + (∑ 𝒙𝒊 𝟑 ) 𝒂𝟏 + (∑ 𝒙𝒊 𝟑 ) 𝒂𝟐 + ⋯ + (∑ 𝒙𝒊 𝒎+𝟐 ) 𝒂𝒎 = ∑ 𝒙𝒊 𝟐 𝒚𝒊
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
⋯ = ⋯
𝒏 𝒏 𝒏 𝒏 𝒏
The objective is to fit a set of 𝑛 data points (𝑥1 , 𝑦1 ), (𝑥2 , 𝑦2 ), … , (𝑥𝑛 , 𝑦𝑛 ) with an 𝑠𝑒𝑐𝑜𝑛𝑑 − 𝑑𝑒𝑔𝑟𝑒𝑒 polynomial in the form
𝒚 = 𝒂𝟐 𝒙𝟐 + 𝒂𝟏 𝒙 + 𝒂𝟎
Such that the total error
Page 9 of 15
𝒏 𝒏
𝟐
𝑬 = ∑ 𝒆𝒊 𝟐 = ∑[ 𝒚𝒊 − ( 𝒂𝟐 𝒙𝒊 𝟐 + 𝒂𝟏 𝒙𝒊 + 𝒂𝟎 ) ]
𝒊=𝟏 𝒊=𝟏
is minimized. Following the general procedure outlined above, the coefficients 𝑎2 , 𝑎1 , 𝑎0 are determined by solving a system of three linear
equations:
𝒏 𝒏 𝒏 𝒏
(∑ 𝒙𝒊 𝟎 ) 𝒂𝟎 + (∑ 𝒙𝒊 𝟏 ) 𝒂𝟏 + (∑ 𝒙𝒊 𝟐 ) 𝒂𝟐 = ∑ 𝒙𝒊 𝟎 𝒚𝒊
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
𝒏 𝒏 𝒏 𝒏
(∑ 𝒙𝒊 𝟏 ) 𝒂𝟎 + (∑ 𝒙𝒊 𝟐 ) 𝒂𝟏 + (∑ 𝒙𝒊 𝟑 ) 𝒂𝟐 = ∑ 𝒙𝒊 𝟏 𝒚𝒊
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
𝒏 𝒏 𝒏 𝒏
(∑ 𝒙𝒊 𝟐 ) 𝒂𝟎 + (∑ 𝒙𝒊 𝟑 ) 𝒂𝟏 + (∑ 𝒙𝒊 𝟒 ) 𝒂𝟐 = ∑ 𝒙𝒊 𝟐 𝒚𝒊
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
Example : Using Quadratic Least-Squares Regression, find the second-degree polynomial that best fits the following data : (0 , 2.9), (0.4 , 3.1),
(0.8 , 3.56), (1.2 , 4.6) , (1.6 , 6.7).
Solution : The aim is to find the coefficients 𝑎2 , 𝑎1 , 𝑎0 of the equation 𝑦 = 𝑎2 𝑥 2 + 𝑎1 𝑥 + 𝑎0 by solving the three equations:
𝟓 𝟓 𝟓 𝟓
(∑ 𝒙𝒊 𝟎) 𝒂 + (∑ 𝒙𝒊 𝟏) 𝒂 + (∑ 𝒙𝒊 𝟐) 𝒂 = ∑ 𝒙𝒊 𝟎 𝒚𝒊
𝟎 𝟏 𝟐
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
𝟓 𝟓 𝟓 𝟓
𝟏) 𝒂 𝟐) 𝒂 𝟑) 𝒂
(∑ 𝒙𝒊 𝟎 + (∑ 𝒙𝒊 𝟏 + (∑ 𝒙𝒊 𝟐 = ∑ 𝒙𝒊 𝟏 𝒚𝒊
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
𝟓 𝟓 𝟓 𝟓
𝟐) 𝒂 𝟑) 𝒂 𝟒) 𝒂
(∑ 𝒙𝒊 𝟎 + (∑ 𝒙𝒊 𝟏 + (∑ 𝒙𝒊 𝟐 = ∑ 𝒙𝒊 𝟐 𝒚𝒊
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
To do so we prepare Table 3:
Table 3 : Example : Best fit for Non-linear data using Second Degree Polynomial
i 𝒙𝒊 𝒚𝒊 𝒙𝒊 𝟐 𝒙𝒊 𝟑 𝒙𝒊 𝟒 𝒙𝒊 𝒚𝒊 𝒙𝒊 𝟐 𝒚𝒊
1 0.0 2.90 0.0000 0.0000 0.0000 0.0000 0.0000
2 0.4 3.10 0.1600 0.0640 0.0256 1.2400 0.4960
3 0.8 3.56 0.6400 0.5120 0.4096 2.8480 2.2784
4 1.2 4.60 1.4400 1.7280 2.0736 5.5200 6.6240
5 1.6 6.70 2.5600 4.0960 6.5536 10.7200 17.1520
∑ → 4.0 20.86 4.8000 6.4000 9.0624 20.3280 26.5504
Hence, the three equations to be solved are:
𝟓𝒂𝟎 + 𝟒𝒂𝟏 + 𝟒. 𝟖𝒂𝟐 = 𝟐𝟎. 𝟖𝟔
𝟒𝒂𝟎 + 𝟒. 𝟖𝒂𝟏 + 𝟔. 𝟒𝒂𝟐 = 𝟐𝟎. 𝟑𝟐𝟖
𝟒. 𝟖𝒂𝟎 + 𝟔. 𝟒𝒂𝟏 + 𝟗. 𝟎𝟔𝟐𝟒𝒂𝟐 = 𝟐𝟔. 𝟓𝟓𝟎𝟒
Solution of these equations yield 𝑎2 = 1.9554, 𝑎1 = −0.8536, 𝑎0 = 2.9777. Hence, the quadratic function which best fits the given set of
data is 𝑦 = 1.9554𝑥 2 − 0.8536𝑥 + 2.9777. This can be verified from Figure 11
Page 10 of 15
Figure 11: Example 3 : Best-fit curve for Non-linear data using Quadratic Polynomial Function
The objective is to fit a set of 𝑛 data points (𝑥1 , 𝑦1 ), (𝑥2 , 𝑦2 ), … , (𝑥𝑛 , 𝑦𝑛 ) with an 𝑡ℎ𝑖𝑟𝑑 − 𝑑𝑒𝑔𝑟𝑒𝑒 polynomial in the form
𝒚 = 𝒂𝟑 𝒙𝟑 + 𝒂𝟐 𝒙𝟐 + 𝒂𝟏 𝒙 + 𝒂𝟎
Such that the total error
𝒏 𝒏
𝟐
𝑬 = ∑ 𝒆𝒊 = ∑[ 𝒚𝒊 − ( 𝒂𝟑 𝒙𝟑 + 𝒂𝟐 𝒙𝒊 𝟐 + 𝒂𝟏 𝒙𝒊 + 𝒂𝟎 ) ]
𝟐
𝒊=𝟏 𝒊=𝟏
is minimized. Following the general procedure outlined above, the coefficients 𝑎3 , 𝑎2 , 𝑎1 , 𝑎0 are determined by solving a system of four linear
equations:
𝒏 𝒏 𝒏 𝒏 𝒏
𝟎) 𝒂 𝟏) 𝒂 𝟐) 𝒂 𝟑) 𝒂
(∑ 𝒙𝒊 𝟎 + (∑ 𝒙𝒊 𝟏 + (∑ 𝒙𝒊 𝟐 + (∑ 𝒙𝒊 𝟑 = ∑ 𝒙𝒊 𝟎 𝒚𝒊
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
𝒏 𝒏 𝒏 𝒏 𝒏
(∑ 𝒙𝒊 𝟏 ) 𝒂𝟎 + (∑ 𝒙𝒊 𝟐 ) 𝒂𝟏 + (∑ 𝒙𝒊 𝟑 ) 𝒂𝟐 + (∑ 𝒙𝒊 𝟒 ) 𝒂𝟑 = ∑ 𝒙𝒊 𝟏 𝒚𝒊
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
𝒏 𝒏 𝒏 𝒏 𝒏
(∑ 𝒙𝒊 𝟐 ) 𝒂𝟎 + (∑ 𝒙𝒊 𝟑 ) 𝒂𝟏 + (∑ 𝒙𝒊 𝟒 ) 𝒂𝟐 + (∑ 𝒙𝒊 𝟓 ) 𝒂𝟑 = ∑ 𝒙𝒊 𝟐 𝒚𝒊
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
𝒏 𝒏 𝒏 𝒏 𝒏
(∑ 𝒙𝒊 𝟑 ) 𝒂𝟎 + (∑ 𝒙𝒊 𝟒 ) 𝒂𝟏 + (∑ 𝒙𝒊 𝟓 ) 𝒂𝟐 + (∑ 𝒙𝒊 𝟔 ) 𝒂𝟑 = ∑ 𝒙𝒊 𝟑 𝒚𝒊
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
Example : Using Cubic Least-Squares Regression, find the third-degree polynomial that best fits the following data : (0 , 2.9), (0.4 , 3.1),
(0.8 , 3.56), (1.2 , 4.6) , (1.6 , 6.7).
Solution : The aim is to find the coefficients 𝑎3 , 𝑎2 , 𝑎1 , 𝑎0 of the equation 𝑦 = 𝑎3 𝑥 3 + 𝑎2 𝑥 2 + 𝑎1 𝑥 + 𝑎0 by solving the four equations:
𝟓 𝟓 𝟓 𝟓 𝟓
(∑ 𝒙𝒊 𝟎 ) 𝒂𝟎 + (∑ 𝒙𝒊 𝟏 ) 𝒂𝟏 + (∑ 𝒙𝒊 𝟐 ) 𝒂𝟐 + (∑ 𝒙𝒊 𝟑 ) 𝒂𝟑 = ∑ 𝒙𝒊 𝟎 𝒚𝒊
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
𝟓 𝟓 𝟓 𝟓 𝟓
(∑ 𝒙𝒊 𝟏 ) 𝒂𝟎 + (∑ 𝒙𝒊 𝟐 ) 𝒂𝟏 + (∑ 𝒙𝒊 𝟑 ) 𝒂𝟐 + (∑ 𝒙𝒊 𝟒 ) 𝒂𝟑 = ∑ 𝒙𝒊 𝟏 𝒚𝒊
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
𝟓 𝟓 𝟓 𝟓 𝟓
(∑ 𝒙𝒊 𝟐 ) 𝒂𝟎 + (∑ 𝒙𝒊 𝟑 ) 𝒂𝟏 + (∑ 𝒙𝒊 𝟒 ) 𝒂𝟐 + (∑ 𝒙𝒊 𝟓 ) 𝒂𝟑 = ∑ 𝒙𝒊 𝟐 𝒚𝒊
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
𝟓 𝟓 𝟓 𝟓 𝟓
(∑ 𝒙𝒊 𝟑 ) 𝒂𝟎 + (∑ 𝒙𝒊 𝟒 ) 𝒂𝟏 + (∑ 𝒙𝒊 𝟓 ) 𝒂𝟐 + (∑ 𝒙𝒊 𝟔 ) 𝒂𝟑 = ∑ 𝒙𝒊 𝟑 𝒚𝒊
𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏 𝒊=𝟏
To do so we prepare Table 4:
Table 4 : Example : Best fit for Non-linear data using Third Degree Polynomial
i 𝒙𝒊 𝒚𝒊 𝒙𝒊 𝟐 𝒙𝒊 𝟑 𝒙𝒊 𝟒 𝒙𝒊 𝟓 𝒙𝒊 𝟔 𝒙𝒊 𝒚𝒊 𝒙𝒊 𝟐 𝒚𝒊 𝒙𝒊 𝟑 𝒚𝒊
1 0.0 2.90 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
2 0.4 3.10 0.1600 0.0640 0.0256 0.0102 0.0041 1.2400 0.4960 0.1984
3 0.8 3.56 0.6400 0.5120 0.4096 0.3277 0.2621 2.8480 2.2784 1.8227
4 1.2 4.60 1.4400 1.7280 2.0736 2.4883 2.9860 5.5200 6.6240 7.9488
5 1.6 6.70 2.5600 4.0960 6.5536 10.4858 16.7772 10.7200 17.1520 27.4432
∑ → 4.0 20.86 4.8000 6.4000 9.0624 13.3120 20.0294 20.3280 26.5504 37.4131
Hence, the four equations to be solved are:
𝟓𝒂𝟎 + 𝟒𝒂𝟏 + 𝟒. 𝟖𝒂𝟐 + 𝟔. 𝟒𝒂𝟑 = 𝟐𝟎. 𝟖𝟔
𝟒𝒂𝟎 + 𝟒. 𝟖𝒂𝟏 + 𝟔. 𝟒𝒂𝟐 + 𝟗. 𝟎𝟔𝟐𝟒𝒂𝟑 = 𝟐𝟎. 𝟑𝟐𝟖
𝟒. 𝟖𝒂𝟎 + 𝟔. 𝟒𝒂𝟏 + 𝟗. 𝟎𝟔𝟐𝟒𝒂𝟐 + 𝟏𝟑. 𝟑𝟏𝟐𝒂𝟑 = 𝟐𝟔. 𝟓𝟓𝟎𝟒
𝟔. 𝟒𝒂𝟎 + 𝟗. 𝟎𝟔𝟐𝟒𝒂𝟏 + 𝟏𝟑. 𝟑𝟏𝟐𝒂𝟐 + 𝟐𝟎. 𝟎𝟐𝟗𝟒𝒂𝟑 = 𝟑𝟕. 𝟒𝟏𝟑𝟏
Solution of these equations yield 𝑎3 = 1.0417, 𝑎2 = −0.5446, 𝑎1 = 0.5798, 𝑎0 = 2.8977. Hence, the cubic function which best fits the
given set of data is 𝑦 = 1.0417𝑥 3 − 0.5446𝑥 2 + 0.5798𝑥 + 2.8977.
Page 11 of 15
Figure 12: Example 4 : Comparing Best-fit curve for Non-linear data using Quadratic and Cubic Polynomial Functions
Figure 12 clearly shows that the cubic polynomial fit is superior to the quadratic one. The cubic polynomial fit almost goes through all five
data points, but not exactly. This is because a third-degree polynomial has four coefficients but the data contains five points. A fourth-degree
polynomial, which has five coefficients, would exactly agree with the data in this example.
In general, if a set of 𝑛 data points is given, then the (𝑛 − 1)𝑡ℎ − 𝑑𝑒𝑔𝑟𝑒𝑒 polynomial that best fits the data will agree exactly with the data
points. This is the main idea behind interpolation, and such polynomial is called an interpolating polynomial.
Figure 13: Lagrange’s Interpolating Polynomials (a) First-degree Polynomial (b) Second-degree Polynomial
The second-degree Lagrange Interpolating Polynomial that goes through the three points (𝑥1 , 𝑦1 ) , (𝑥2 , 𝑦2 ) and (𝑥3 , 𝑦3 ) is in the form
𝒑𝟐 (𝒙) = 𝑳𝟏 (𝒙)𝒚𝟏 + 𝑳𝟐 (𝒙)𝒚𝟐 + 𝑳𝟑 (𝒙)𝒚𝟑
Page 12 of 15
Where
(𝒙 − 𝒙𝟐 )(𝒙 − 𝒙𝟑 ) (𝒙 − 𝒙𝟏 )(𝒙 − 𝒙𝟑 ) (𝒙 − 𝒙𝟏 )(𝒙 − 𝒙𝟐 )
𝑳𝟏 (𝒙) = , 𝑳𝟐 (𝒙) = 𝑳𝟑 (𝒙) =
(𝒙𝟏 − 𝒙𝟐 )(𝒙𝟏 − 𝒙𝟑 ) (𝒙𝟐 − 𝒙𝟏 )(𝒙𝟐 − 𝒙𝟑 ) (𝒙𝟑 − 𝒙𝟏 )(𝒙𝟑 − 𝒙𝟐 )
Then, 𝐿1 (𝑥1 ) = 1 = 𝐿2 (𝑥2 ) = 𝐿3 (𝑥3 ), while 𝐿𝑖 (𝑥𝑗 ) = 0 , 𝑓𝑜𝑟 𝑖 ≠ 𝑗. This guarantees 𝑝2 (𝑥1 ) = 𝑦1 , 𝑝2 (𝑥2 ) = 𝑦2 and 𝑝2 (𝑥3 ) = 𝑦3 , so
that the polynomial goes through the three points. See Figure 13 (b).
In general, the 𝑛𝑡ℎ − 𝑑𝑒𝑔𝑟𝑒𝑒 Lagrange Interpolating Polynomial that goes through 𝑛 + 1 data points (𝑥1 , 𝑦1 ), … , (𝑥𝑛+1 , 𝑦𝑛+1 ), is formed as
𝒏+𝟏
Example : Find the second-degree Lagrange interpolating polynomial for the data (0.1, 0.12) , (0.5, 0.47) , (0.9, 0.65). Use this polynomial
to find the interpolated value at 𝑥 = 0.3.
Page 14 of 15
Page 15 of 15