Professional Documents
Culture Documents
Lecture Notes 6
Lecture Notes 6
Interpolation and
Approximation
y mx n
• For this type of relationship, a line best fits the data points
100
Math Score
90
80
70
60
50
85 105 125 145
IQ
100
Fuel Consumption
80
60
40
20
25 50 75 100
Velocity
f ( x) ax b
• How can we pick the coefficient that best fits the line to the data?
• What makes a particular straight line a good fit?
• Why does the red line appear to us to fit the trend better?
f(x)
Error in the case red fitting func.
e4.
Sum(error)=(e1)^2+(e2)^2
e1.
x
• Add up the length of the all red and blue vertical lines.
• This is an expression of the “error” between data and fitted line.
• The one line that provides a minimum error is said the “best”
straight line
1
(10/11)*100%=91%
x
LEAST SQUARE
102 + (-1)2=101
(100/101)*100%=99%
100
1
f(x) (x4,f(x4)) f ( x) ax b
Given data
points (x3,y3)
x y e4
e3
x1 y1 (x2,f(x2))
(x4,y4)
(x3,f(x3))
x2 y2 (x1,y1)
y1 e2
x3 y3 e1 (x2,y2)
x4 y4 f ( x1 ) ax1 b (x1,f(x1))
y1
• The “best” line has minimum error between the line and data points.
• This is called the least squares approach, since we minimize the square
of the error.
• Take the derivative of the error with respect to a and b , set each to
zero
(error ) n
2 xi ( yi axi b) 0
a i 1
(error ) n
2 ( yi axi b) 0
b i 1
• Solve for the a and b so that the previous two equations both are equal
to zero. Re-write these two equations:
n n n
a xi b xi xi yi
2
i 1 i 1 i 1
n n
a xi b n yi
i 1 i 1
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Curve Fitting
• If we put these into matrix form
n
n
n x i
b i 1
i
y
i 1
n 2 a
n n
xi ( xi ) i i
( x y )
i 1 i 1 i 1
• Example: Fit a straight line to the x and y values
i 1 2 3 4 5 6
x 0 0.5 1.0 1.5 2.0 2.5
y 0 1.5 3.0 4.5 6.0 7.5
• Solution:
n6
6 6
x i x1 x2 x3 x4 x5 x6 7.5 (x )
i 1
i
2
( x1 ) 2 ( x2 ) 2 ( x3 )2 ( x4 )2 ( x5 ) 2 ( x6 )2 13.75
i 1
6
6
y i y1 y2 y3 y4 y5 y6 22.5 x y
i 1
i i x1 y1 x2 y2 x3 y3 x4 y4 x5 y5 x6 y6 41.5
i 1
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Curve Fitting
• Substituting the values into equation
n
n
n x i
b i 1
i
y
6 7.5 b 22.5
a3
i 1
7.5 13.5 a 41.5 b0
n 2 a
n n
xi i i
( x ) ( x y ) f ( x) 3 x 0
i
i 1 i 1 i 1
i 1 2 3 4 5 6
x 0 0.5 1.0 1.5 2.0 2.5
y -0.4326 -0.1656 3.1253 4.7877 4.8535 8.6909
x y x2 x.y
0 -0.4326 0 0
0.5 -0.1656 0.25 -0.0828
1 3.1253 1 3.1253
1.5 4.7877 2.25 7.18155
2 4.8535 4 9.707
2.5 8.6909 6.25 21.72725
= 7.5 20.8592 13.75 41.6583
• Substituting the values:
n
n
n xi
b i 1
yi
6 7.5 b 20.86
a 3.56
i 1
7.5 13.75 a 41.66 b 0.98
n 2 a
n n
xi ( xi ) i i
( x y ) f ( x) 3.56 x 0.98
i 1 i 1 i 1
i 1 i 1
• Total sum of the squares around the mean of dependent variable is represented S t .
n y i
St ( yi y ) 2
where y i 1
(mean)
i 1 n
• Then, (r2) (Coefficient of determination) is calculated as follows:
St S r
r2
St
• For perfect fit Sr=0 and r2=1
• If Sr= St and r2=0 (Poor fit)
• c)
Tensile Strength=0.8179+1.059×(Time)
For time=32
• Solution
a b r2
1.5429 2.6429 0.8011
12
11
y=1.5429+2.6429x 10
2
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
x x
• Linear regression provides a powerful technique for fitting a “best” line
to data if the relationship between the dependent and independent
variables is linear. However, this is not always the case.
y a1eb1x
• Another example of a nonlinear model is the simple power equation.
y a2 xb2
• The third example is hyperbolic equation
x
y a3
b3 x
• Nonlinear regression techniques are available to fit these equations to
experimental data directly. However, a simpler alternative is to use
mathematical manipulations to transform the equations into a linear
form.
form Then simple linear regression can be employed to fit the
equations to data.
CENG 202: NUMERICAL ANALYSIS, DR. SEMIH ERHAN
Curve Fitting
• Linearization of Equations
1) y a1eb1x
taking the natural logarithm, the equation yields;
ln y ln a1 b1 x ln e ln y c b1 x
c 1
2) y a2 x b2
is linearized by taking its natural logarithm to give.
ln y ln a2 b2 ln x ln y c b2 ln x
c
(c) (d)
x 1 2 3 4 5
y 0.5 1.7 3.4 5.7 8.4
a1 i 1 i 1 i 1 i 1
n
n
n ln x i
a0
ln yi
i 1
i 1
n 2 a1
n n
Yield stress (MPa) 345 362 419 454 485 633 831
y a0 a1 t 0.16
y a0 a1 x m m 0.16
n
error= ( yi (a0 a1 xi m )) 2
i 1 a0 n
n n n
(error ) n
a0
2 ( yi a0 a1 xi m ) 0 a
i 1
0 a1 xi yi
i 1
m
i 1
i 1
n n n
(error ) n
2 xi m ( yi a0 a1 xi m ) 0 a0 xi a1 ( xi ) yi xi m
m m 2
a1 i 1 i 1 i 1 i 1
n
n
n xm
i
a i y
i 1
0 i 1
n m 2 m a1
n n
xi i i
m
xi x y
i 1 i 1 i 1
n
Σ= 3720.1 3529 9.3663 22.204 6036.4 St ( yi y ) 179100
i 1
St S r 179100 451.96
r2 0.9975 Good Fit !!!
St 179100
i 1 i 1
n n n n
Sr n
a0
2 ( yi a0 a1 xi a2 xi 2 ) 0 a
i 1
0 a1 xi a2 xi yi
i 1 i 1
2
i 1
i 1
n n n n
Sr n
a0 xi a1 xi a2 xi yi xi
2 3
2 xi ( yi a0 a1 xi a2 xi 2 ) 0
a1 i 1 i 1 i 1 i 1 i 1
n n n n
Sr n
a0 xi a1 xi a2 xi yi xi 2
2 3 4
2 xi 2 ( yi a0 a1 xi a2 xi 2 ) 0
a2 i 1
i 1 i 1 i 1 i 1
xi 0 1 2 3 4 5
0 2.1 0 0 0 0 0 6 15 55 a0 152.6
15 55 225 a 585.6
1 7.7 1 1 1 7.7 7.7 1
55 225 979 a2 2488.8
2 13.6 4 8 16 27.2 54.4