4-Curve Fitting and Interpolation
4-Curve Fitting and Interpolation
Topic 4
2
Curve fitting: Objectives
y
¦y i
yi : measured value
e : error
yi = a0 + a1 xi + e
e = yi - a0 - a1 xi
a1 : slope
a0 : intercept Line equation
y = a0 + a1 x
Choosing Criteria For a “Best Fit”
• Minimize the sum of the residual errors
for all available data? Regression
n n Inadequate! line
¦e ¦(y
i 1
i
i 1
i ao a1 xi ) (see ÎÎÎ)
i 1 i 1
wS r
2¦ ( yi ao a1 xi ) 0 ¦ y ¦a ¦a x 0
wao
i 0 1 i
wS r
2¦ >( yi ao a1 xi ) xi @ 0 ¦ y x ¦a x ¦a x 2
0
wa1
i i 0 i 1 i
Since ¦a 0 na0
na0 ¦x i a1 ¦yi
¦x i a0 ¦x 2
i a1 ¦yx
i i
ª n
«
¦ x º» ªa º ª« ¦ y º»
i 0 i
«a »
¬¦ xi ¦ ¼ ¬ ¼ ¬¦ y x ¼
2
x i 1 i i
n¦ xi yi ¦ xi ¦ yi Mean values
a1
n¦ x ¦x
2 2
i i
n n
Sr ¦e ¦ y a
2
i i 0 a1 xi
2
i 1 i 1
r : correlation coefficient
r 2 St S r St ¦ i
( y y ) 2
r2 : coefficient of determination St n
Sr ¦(y
i 1
i a0 a1 xi ) 2
exponential : y D1eE1 x
power : y D2 x E2
x
saturation - growth - rate : y D 3
E3 x
Transformation Examples
Data to be fit to the power equation:
y D 2 x E2
log y E 2 log x log D 2
x y log x log y
1 0.5 0 -0.301
2 1.7 0.301 0.226
3 3.4 0.477 0.531
4 5.7 0.602 0.756
5 8.4 0.699 0.924
y a0 a1 x a2 x 2
n n
Minimize error : Sr ¦e
i 1
2
i ¦ i 0 1 i 2 i)
(
i 1
y a a x a x 2 2
wS r
2¦ ( yi ao a1 xi a x ) 0
2 na0 ¦x i a1 ¦x 2
i a2 ¦y i
wao
2 i
wS r
2¦ xi ( yi ao a1 xi a2 xi2 ) 0 ¦ xi a0 ¦ i a1
x 2
¦ i a2
x 3
¦x y
wa1 i i
wS r
2¦ xi2 ( yi ao a1 xi a2 xi2 ) 0
wa2 ¦ i a0
x 2
¦ i a1
x 3
¦ i a2
x 4
¦ i yi
x 2
20
Polynomial Regression
• To fit the data to an mth order polynomial, we need to solve the following
system of linear equations ((m+1) equations with (m+1) unknowns)
ª n ¦x i ¦x º ª a0 º
m
i ª ¦ yi º
« »« » « »
« ¦ xi ¦x ¦x « ¦ xi yi »
2 m 1
i » « a1 »
i
« »« » « »
« mm » « » « »
«¬¦ i ¦ i ¦ xi »¼ ¬am ¼ «¬¦ xi yi »¼
m m 1 m
x x
Matrix Form
Multiple Linear Regression
• A useful extension of linear regression is the case where y is a linear function
of two or more independent variables. For example:
y = ao + a1x1 + a2x2 + e
• For this 2-dimensional case, the regression line becomes a plane as shown
in the figure below.
Multiple Linear Regression
n n
Example (2 - vars) : Minimize error : Sr ¦e ¦(y
i 1
2
i
i 1
i a0 a1 x1i a2 x2i ) 2
wS r
2¦ ( yi ao a1 x1i a2 x2i ) 0 na0 ¦x 1i a1 ¦x 2i a2 ¦y i
wao
wS r
wa1
2¦ x1i ( yi ao a1 x1i a2 x2i ) 0 ¦x1i a0 ¦x 2
1i a1 ¦x x a2
1i 2 i ¦x 1i yi
wS r
2¦ x2i ( yi ao a1 x1i a2 x2i ) 0
wa2 ¦x 2i a0 ¦x 1i x2 i a1 ¦x 2
2i a2 ¦x 2i yi
ª n
«
¦x 1i ¦x 2i º ª a0 º
»« »
ª ¦ yi º
« »
« ¦ x1i ¦x ¦x x « ¦ x1i yi »
2
1i 1i 2 i » « a1 »
« ¦ x2 i ¦x x ¦x 2
»« »
2 i ¼ ¬ a2 ¼
« ¦ x 2 i yi »
¬ 1i 2 i ¬ ¼
Multiple Linear Regression ª n
«
¦x 1i ¦x 2i º ª a0 º
»« »
ª ¦ yi º
« »
« ¦ x1i ¦x ¦x x « ¦ 1i i »
2
1i 1i 2 i » « a1 » x y
Example « ¦ x2 i ¦x x ¦x 2
»« »
2 i ¼ ¬ a2 ¼
« ¦ x 2 i yi »
¬ 1i 2 i ¬ ¼
The following data is calculated from the equation y = 5 + 4x1 - 3x2
x1 x2 y
0 0 5
2 1 10
2.5 2 9
1 3 0
4 6 3
7 2 27
Solution:
this system can be solved using
ª 6 16.5 14 º ªa0 º ª 54 º
«16.5 76.25 48» « a » «243.5» Gauss Elimination.
« »« 1 » « » The result is: a0=5 a1=4 and a2= -3
«¬ 14 48 54»¼ «¬a2 »¼ «¬ 100 »¼ y = 5 + 4x1 -3x2
INTERPOLATION
Topic 4 continued…
1
Polynomial Interpolation
• You will frequently have occasions to estimate
intermediate values between precise data points.
• The function you use to interpolate must pass
through the actual data points - this makes
interpolation more restrictive than fitting.
• The most common method for this purpose is
polynomial interpolation, where an (n-1)th order
polynomial is solved that passes through n data
points: f ( x) a a x a x 2 ... a x n 1
1 2 3 n
MATLAB version :
f ( x) p1 x n 1 p 2 x n 2 ... p n 1 x p n
f ( x) a1 a2 x a3 x 2 an x n 1
• There is one and only one nth-order polynomial that fits n+1
points
Polynomial Interpolation Problems
• One problem that can occur with solving for the coefficients of
a polynomial is that the system to be inverted is in the form:
• Two methods:
• The Newton polynomial
• The Lagrange polynomial
0 2 0 2 1 1
1 3 1 3 3
2 6 2 6
1 3 3 63
3
2 6 2 1
>
f xi , x j , xk @ > @ >
f xi , x j f x j , xk @
xi xk
xi F[ ] F[ , ] F[ , , ]
0 2 1 1
1 3 3 3 1
1
2 6 20
xi F[ ] F[ , ] F[ , , ]
0 2 1 1
1 3 3
2 6
f 2 ( x) 2 1( x 0) 1( x 0)( x 1)
f 2 ( x) 2 x x x2
x 2
2
Ordering the points should not affect the divided difference:
f [ x0 , x1 , x2 ] f [ x1 , x2 , x0 ] f [ x2 , x1 , x0 ]
Exercise: x f(x)
0 1
• Find a polynomial to 2 5
interpolate the data.
4 1
6 6
Lagrange Interpolating Polynomials
• Another method that uses shifted value to express an
interpolating polynomial is the Lagrange interpolating
polynomial.
• The differences between a simple polynomial and
Lagrange interpolating polynomials for first and
second order polynomials is:
• Order Simple Lagrange
1st f1 (x) a1 a2 x f1 (x) L1 f x1 L2 f x2
2nd f2 (x) a1 a2 x a3 x 2 f2 (x) L1 f x1 L2 f x2 L3 f x 3
f1 (x) L1 f x1 L2 f x2
x x2 x x1
L1 , L2
x1 x2 x2 x1
x x2 x x1
f1 (x) f x1 f x2
x1 x2 x2 x1
Lagrange Interpolating Polynomials
(cont…)
In general, the Lagrange polynomial interpolation for n points
is: n
fn1 xi ¦ Li x f xi
i 1
n
x xj
where Li is given by: Li x x x
j 1 i j
jzi
x x1 x x0
f1 ( x ) f ( x0 ) f ( x1 )
x0 x1 x1 x0
x x1 x x2 x x0 x x2 x x0 x x1
f 2 ( x) f ( x0 ) f ( x1 ) f ( x2 )
x0 x1 x0 x 2 x1 x0 x1 x 2 x2 x0 x2 x1
The cardinals are n th order polynomials :
0 i z j
Li ( x j ) ®
¯1 i j
f 2 ( x) f ( x0 ) L0 ( x) f ( x1 ) L1 ( x) f ( x 2 ) L2 ( x)
x f(x)
x x1 x x2 x2 x4 ( x 6 x 8)
2
L0 ( x)
x0 x1 x0 x 2 02 04 8
x x0 x x2 x0 x4 x 2 4x
0 1
L1 ( x)
x1 x0 x1 x 2 20 24 4
x x0 x x1 x0 x2 x 2 2x 2 5
L2 ( x )
x 2 x0 x 2 x1 40 42 8
( x 2 6 x 8) ½ x 2 4 x ½ x 2 2 x ½ 4 1
f 2 ( x) 1® ¾ 5® ¾ 1® ¾
¯ 8 ¿ ¯ 4 ¿ ¯ 8 ¿
x 2 4x 1
Exercise: x f(x)
4 1
6 6
Inverse Interpolation
• Sometimes, it is useful to find the value of x for a given f(x) -
this is inverse interpolation.
Soln: Get the value of interpolation function f(x) using the given
data set.
Alternatively: x y y x
exchange xi with
0 1 1 0
yi and solve for x
as a function of y 2 5 5 2
4 1 1 4
6 6 6 6
Spline Interpolation
• There are cases where
polynomials (esp. high order
polynomials) can lead to
erroneous results because of
round off error and overshoot.
EXAMPLE:
spline fits for a set of 4 points:
(a) linear
(b) quadratic
(c) cubic splines
• linear spline Î ÎÎ
is superior to higher-order
interpolating polynomials (parts
a, b, and c) 23
Quadratic Spline example
Quadratic Splines - Equations
Given (n+1) points, there are 3(n) coefficients to find and hence, need to
determine 3n equations to solve:
1. Each polynomial must pass through the endpoints of the interval that
they are associated with. And these
conditions result in 2n equations:
ai xi2 bi xi ci f ( xi ) i 1,2, , n
ai xi21 bi xi 1 ci f ( xi 1 ) i 1,2,, n
1. The first derivatives at the interior knots must be equal ((n-1)
equations):
2ai xi bi 2ai 1 xi bi 1 i 1,2,, n 1
2. So far, we are one equation short. Since the last equation will be
derived using only 2 points, it must be a line equation with only bn and
cn to be determined (an = 0)