1- Curve Fitting
1- Curve Fitting
Outlines:
1. Introduction
2. Linear Regression
The first step in curve fitting (also called regression) is to assume a particular
mathematical model that might fit the data.
E.g.
The second step in curve fitting is to find the mathematical model parameters that
By minimize the sum of the squares of the errors between the model
Ideally, the form of the mathematical model should be chosen based on the
physics of the problem.
If nothing is know about the data, you can test different model function and
choose the best
Given a set of data 𝑥𝑖 , 𝑦𝑖 with 1 ≤ 𝑖 ≤ n, a linear model fit to this set of data
has the form:
𝑦 𝑥 = 𝑎𝑥 + 𝑏
Where 𝑎 and 𝑏 are the model parameters.
Numerical Analysis ME309 Curve Fitting Linear Regression 8
The model parameters can be found by minimizing the sum of the squares of the
∆2𝑖 = 𝑦 𝑥𝑖 − 𝑦𝑖 2
→ 𝑆 = σ𝑛𝑖 = 1 𝑦 𝑥 𝑖 − 𝑦 𝑖 2
→ 𝑆 = σ𝑛𝑖 = 1 𝑎 𝑥 + 𝑏 − 𝑦 𝑖 2
𝑛 σ𝑛 𝑥 𝑦 −σ𝑛 𝑥 σ𝑛 𝑦
𝜕𝑆
𝜕𝑎
= 2 σ𝑛𝑖=1 𝑎𝑥𝑖 + 𝑏 − 𝑦𝑖 𝑥𝑖 = 0 𝑎= 𝑖=1 𝑖 𝑖 𝑖=1 𝑖 𝑖=1 𝑖
2
𝑛 2 𝑛
𝑛 σ𝑖=1 𝑥𝑖 − σ𝑖=1 𝑥𝑖
σ𝑛 𝑛
𝑖=1 𝑦𝑖 −𝑎 σ𝑖=1 𝑥𝑖
𝜕𝑆
𝜕𝑏
= 2 σ𝑛𝑖=1 𝑎𝑥𝑖 + 𝑏 − 𝑦𝑖 = 0 𝑏= 𝑛
Coefficient of Determination:
The coefficient of determination (also called correlation coefficient) denoted 𝑅2 or
𝑟 2 and pronounced 𝑅 squared, is a number that provides a statistical measure of
how well the model fits the data.
The value of 𝑅2 can be calculated as follows:
2
σ𝑛
𝑖=1 𝑦𝑖 −𝑦 𝑥𝑖
𝑅2 = 1 − 2
𝑛 1 𝑛
σ𝑖=1 𝑦𝑖 − σ𝑖=1 𝑦𝑖
𝑛
ഥ
𝒚
Note that 0 ≤ 𝑅2 ≤ 1 A value closer to 1 indicates that the model is a good fit for
the data, while a value of 0 indicates that the model does not fit the data.
Numerical Analysis ME309 Curve Fitting Linear Regression 11
Coefficient of Determination:
𝑛 2
σ𝑖=1 𝑦𝑖 −𝑦 𝑥𝑖
Consider the limiting values: 𝑅2 = 1 − 1 𝑛 2
𝑛
σ𝑖=1 𝑦𝑖 − σ𝑖=1 𝑦𝑖
𝑛
2
1. σ𝑛𝑖=1𝑦𝑖 − 𝑦 𝑥𝑖 = 0 » 𝑅2 = 1
The residual sum of square is zero, indicating the regression line is a perfect fit
2 2
2. σ𝑛𝑖=1 𝑦𝑖 − 𝑦 𝑥𝑖 = σ𝑛𝑖=1 𝑦𝑖 − 1 𝑛
σ 𝑦
𝑛 𝑖=1 𝑖
» 𝑅2 = 0
The residual sum of square is equal to the total sum of square, indicating that the
regression line is no better than a simple average i.e. 𝑦 is linearly independent of
𝑥.
Numerical Analysis ME309 Curve Fitting 12
Example 1.1
Find the best linear fit to the data:
Numerical Analysis ME309 Curve Fitting 13
Example 1.1
Numerical Analysis ME309 Curve Fitting Extension of Linear Regression 14
In a general sense, the model function 𝑦 can be composed of 𝑚 terms with the
following form:
Numerical Analysis ME309 Curve Fitting Extension of Linear Regression 15
Note that the linear regression model can be viewed as a special case of this
Another special case of this general form is polynomial regression where the
The regression procedure constitutes finding the coefficients 𝑎𝑗 that would yield
the least sum of squared differences between the data and model prediction.
differences between a general linear regression model and the data, then 𝑆 has
the form:
Numerical Analysis ME309 Curve Fitting Extension of Linear Regression 17
It can be shown that the above system always has a unique solution when the
functions 𝑓𝑖 𝑥 are non-zero and distinct.
Solving these equations yields the best fit to the data, i.e., the best coefficients 𝑎𝑖
that would minimize the sum of the squares of the differences between the model
and the data.
Example 1.2
𝑦 = 𝑎1 + 𝑎2 𝑥 + 𝑎3 𝑐𝑜𝑠 π𝑥
Find the coefficients 𝑎1 , 𝑎2 and 𝑎3 that would give the best fit and 𝑅2 .
Numerical Analysis ME309 Curve Fitting 21
Example 1.2
𝑦 = 𝑎1 + 𝑎2 𝑥 + 𝑎3 𝑐𝑜𝑠 π𝑥
𝑓1 𝑥 = 1 8
𝑓2 𝑥 = 𝑥 6
𝑓3 𝑥 = cos 𝜋𝑥
4
y
3
0
0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0
X
To find the best coefficients, the following linear system of equations needs to be solved:
Numerical Analysis ME309 Curve Fitting 22
Example 1.2
In Excel:
7 28 −1 𝑎1 24
28 140 −4 𝑎2 = 119.5
−1 −4 7 𝑎3 1
Numerical Analysis ME309 Curve Fitting 23
Example 1.2
Example 1.2
Example 1.2
𝑛 2
σ𝑖=1 𝑦𝑖 −𝑦 𝑥𝑖
To find the coefficient of determination: 𝑅2 = 1 − 1 𝑛 2
𝑛
σ𝑖=1 𝑦𝑖 − σ𝑖=1 𝑦𝑖
𝑛
0.13095
𝑅2 =1− = 0.994235 𝑅2 is closed to 1, indicating a very good fit.
22.71429
Numerical Analysis ME309 Curve Fitting 26
Example 1.3
Example 1.3
𝑦 = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + 𝑎3 𝑥 3
𝑓0 𝑥 = 1, 𝑓1 𝑥 = 𝑥, 𝑓2 𝑥 = 𝑥 2 , 𝑓3 𝑥 = 𝑥 3
To find the coefficients, the following linear system of equations must be solved:
Numerical Analysis ME309 Curve Fitting 28
Example 1.3
In Excel:
Numerical Analysis ME309 Curve Fitting 29
Example 1.3
Numerical Analysis ME309 Curve Fitting 30
Example 1.3
Example 1.3
𝑦 𝑥 = −1.6247 + 6.6301𝑥 − 5.7273𝑥 2 + 2.46212𝑥 3
Numerical Analysis ME309 Curve Fitting 32
Example 1.3
Coefficient of Determination:
2
σ𝑛
𝑖=1 𝑦𝑖 −𝑦 𝑥𝑖
𝑅2 = 1 − 2
𝑛 1 𝑛
σ𝑖=1 𝑦𝑖 − σ𝑖=1 𝑦𝑖
𝑛
0.9506
𝑅2 =1− = 0.9921
120.0129
Numerical Analysis ME309 Curve Fitting Linearization of Nonlinear Relationships 33
In the previous two sections, the model function was formed as a linear
combination of functions 𝑓1 , 𝑓2 , … . 𝑓𝑚 .
The minimization of the sum of the squares of the differences between the model
prediction and the data produced a linear system of equations to solve for the
coefficients in the model.
The following are some examples of nonlinear functions that can be transformed
into the linear functions:
𝑣 = 𝑎0 + 𝑎1 𝑢
Numerical Analysis ME309 Curve Fitting Linearization of Nonlinear Relationships 36
2
σ𝑛 𝑦𝑖 −𝑦 𝑥𝑖
𝑅2 = 1 − 𝑖=1
σ𝑛 2
𝑖=1 𝑦𝑖
Numerical Analysis ME309 Curve Fitting 37
Example 1.4
Example 1.4
The exponential model has the form:
𝑦 = 𝑏1 𝑒 𝑎1 𝑥 ln 𝑦 = 𝑎1 𝑥 + ln 𝑏1
The data needs to be converted to 𝑥𝑖 , ln𝑦𝑖 . We will 𝑣𝑖 and 𝑢𝑖 to represent the
converted data with 𝑣𝑖 = ln𝑦𝑖 and 𝑢𝑖 = 𝑥𝑖
Numerical Analysis ME309 Curve Fitting 39
Example 1.4
The linear regression procedure (Linear Regression Section) will be used to find
the best fit for the model
ln 𝑦 = 𝑎1 𝑥 + ln 𝑏1 𝑣 = 𝑐1 𝑢 + 𝑐2
𝑛 σ𝑛 𝑢 𝑣 − σ 𝑛 𝑢 σ𝑛 𝑣
𝑐1 = 𝑖=1 𝑖 𝑖 𝑖=1 𝑖 𝑖=1 𝑖 = 13(33.8013_−20.8(19.3085) = 1.5976
𝑛 σ𝑛 𝑢 2 − σ𝑛 𝑣 2 13 35.10 −(20.8)2
𝑖=1 𝑖 𝑖=1 𝑖
σ𝑛 𝑣 −𝑐 σ𝑛
1 𝑖=1 𝑢𝑖
𝑐2 = 𝑖=1 𝑖
𝑛
= 19.3085−1.5976(20.8)
13
= −1.0709
Numerical Analysis ME309 Curve Fitting 40
Example 1.4
Using Excel:
Numerical Analysis ME309 Curve Fitting 41
Example 1.4
Therefore, the best exponential model based on the least squares of the linearized
version has the form:
𝑦 = 0.3427𝑒1.5976𝑥
Numerical Analysis ME309 Curve Fitting 42
Example 1.4
Example 1.4
Coefficient of Determination:
We could calculate the coefficient of determination for the linearized version of
this model, however, it would only describe how good the linearized model is.
𝑛 2
σ𝑖=1 𝑦𝑖 −𝑦 𝑥𝑖
𝑅2 = 1 − σ𝑛 2
𝑖=1 𝑦𝑖
0.97
𝑅2 = 1 − 479.59 = 0.998
Numerical Analysis ME309 Curve Fitting 44
Example 1.5
Fit a power model to the data: (1,1.93) ,(1.1,1.61) ,(1.2,2.27) ,(1.3,3.19) ,(1.4,3.19)
,(1.5,3.71) ,(1.6,4.29) ,(1.7,4.95) ,(1.8,6.07) ,(1.9,7.48) ,(2,8.72) ,(2.1,9.34) ,(2.2,11.62).
Numerical Analysis ME309 Curve Fitting 45
Example 1.5
The power model has the form 𝑦 = 𝑏2 𝑥 𝑎2 .
This form can be linearized as follows: ln 𝑦 = 𝑎2 ln 𝑥 + ln 𝑏2
The data needs to be converted to ln 𝑥𝑖 , ln 𝑦𝑖 . We will 𝑣𝑖 and 𝑢𝑖 to represent the
converted data with 𝑣𝑖 = ln𝑦𝑖 and 𝑢𝑖 = ln𝑥𝑖 .
Numerical Analysis ME309 Curve Fitting 46
Example 1.5
The linear regression procedure (Linear Regression Section) will be used to find
the best fit for the model.
ln 𝑦 = 𝑎1 ln 𝑥 + ln 𝑏2 𝑣 = 𝑐1 𝑢 + 𝑐2
𝑛 σ𝑛 𝑢 𝑣 −σ𝑛 𝑢 σ𝑛 𝑣
𝑐1 = 𝑖=1 𝑖 𝑖 𝑖=1 𝑖 𝑖=1 𝑖
2 = 13(10.3985_−5.7357(19.3085)
13 3.3013 −(5.7357)2
= 2.4387
𝑛 2 𝑛
𝑛 σ𝑖=1 𝑢𝑖 − σ𝑖=1 𝑣𝑖
σ𝑛 𝑣 −𝑐 σ𝑛
𝑖=1 𝑢𝑖
𝑐2 = 𝑖=1 𝑖
𝑛
1
= 19.3085−2.4387(5.7357)
13 = 0.4387
Numerical Analysis ME309 Curve Fitting 47
Example 1.5
Using Excel:
Numerical Analysis ME309 Curve Fitting 48
Example 1.5
Therefore, the best power model based on the least squares of the linearized
𝑦 = 1.5058𝑥 2.4387
Numerical Analysis ME309 Curve Fitting 49
Example 1.5
Example 1.5
Coefficient of Determination:
For the nonlinear model, we will use the coefficient of determination as:
2
σ𝑛 𝑦 −𝑦 𝑥
𝑅2 = 1 − 𝑖=1 𝑖
σ𝑛 𝑦
𝑖
2
𝑖=1 𝑖
3.25
𝑅2 = 1 − 479.59 = 0.9932
Numerical Analysis ME309 Homework 1 51
Problem 1:
Problem 2: