You are on page 1of 58

Chapter four

CURVE FITTING
Application

2
Interpolation of data
1.5 What is kinematic
v, 10-2 cm 2/s

1 viscosity at 7.5º C?
o -2 2
0.5 T ( C) v, 10 (cm /s)
0 1.7923
0 4 1.5615
0 10 20 30
8 1.3874
T(oC)
12 1.2396
16 1.1168
20 1.0105
24 0.9186
f(x)

We want to find the best “fit” of a curve through the data.


Mathematical Background
• The prerequisite mathematical background
for this chapter:
• Simple statistics
average
standard deviation
normal distribution
Least Squares Regression
• A more appropriate strategy for such cases
is to derive an approximating function that
fits the shape or general trend of the data
without necessarily matching the individual
points.
Linear Regression
• Simplest is fitting a straight line to a set of
paired observations
(x1,y1), (x2, y2).....(xn, yn)
• The resulting mathematical expression is
y = ao + a1x + e
• We will consider the error introduced at each
data point to develop a strategy for
determining the “best fit” equations
n n 2

Sr   e2i    yi  a o  a1xi 
i 1 i 1

f(x)

yi  a o  a1xi

x
n n 2

Sr   e2i    yi  a o  a1xi 
i 1 i 1

To determine the values for ao and a1, differentiate


with respect to each coefficient

Sr
 2  yi  ao  a1xi 
ao
Sr
 2   yi  ao  a1xi  xi 
a1
Note: we have simplified the summation symbols.
What mathematics technique will minimize Sr?
Sr
 2  yi  ao  a1xi 
ao
Sr
 2   yi  ao  a1xi  xi 
a1

Setting the derivative equal to zero will minimize Sr.


If this is done, the equations can be expressed as:
0   yi   ao   a1xi
0   yi xi   ao xi   a1xi2
0   yi   ao   a1xi
0   yi xi   ao xi   a1xi2

Note: a o  nao

We have two simultaneous equations, with two


unknowns, ao and a1.

What are these equations? (hint: only place terms with


ao and a1 on the LHS of the equations)

What are the final equations for ao and a1?


nao   xi a1   yi These first two
equations are
called
 i o  i a1   xi yi
x a  x 2

the normal
equations
n xi yi   xi  y i
a1 
n x    xi 
2 2
i

ao  y  a1x
Example
Fit a straight line to the x and y values in the
following Table.
Example
Error
Recall:
n n 2
f(x)
Sr   e i    yi  a o  a1xi 
2

i 1 i 1

The most common measure of the “spread” of a sample is


the standard deviation about the mean: St   yi  y  2
St
sy 
n1
Introduce a term to measure the standard error of the
estimate:
Sr
sy 
x n2

Coefficient of determination r2:


St  Sr
r 
2

St

r is the correlation coefficient


St  Sr
r 
2

St
The following signifies that the line explains 100
percent of the variability of the data:

Sr = 0
r = r2 = 1

If r = r2 = 0, then Sr = St and the fit is invalid.


Example
• Compute the total standard deviation, the
standard error of the estimate, and the
correlation coefficient for the data in previous
Example.
• Solution
The summations are performed and presented in
table below.
Example
• The standard deviation is
• the standard error of the estimate is

• Thus, because sy/x < sy, the linear


regression model has merit. The extent of
the improvement is quantified by

• These results indicate that 86.8 percent of


the original uncertainty has been explained
by the linear model.
Consider the following four sets of data

Data 1 Data 2 Data 3 Data 4


10 8.04 10 9.14 10 7.46 8 6.58
8 6.95 8 8.14 8 6.77 8 5.76
13 7.58 13 8.74 13 12.74 8 7.71
9 8.81 9 8.77 9 7.11 8 8.84
11 8.33 11 9.26 11 7.81 8 8.47
14 9.96 14 8.10 14 8.84 8 7.04
6 7.24 6 6.13 6 6.08 8 5.25
4 4.26 4 3.10 4 5.39 19 12.50
12 10.84 12 9.13 12 8.15 8 5.56
7 4.82 7 7.26 7 6.42 8 7.91
5 5.68 5 4.74 5 5.73 8 6.89
14 14

12 12

10 10

8 8
y

y
6 6

4 4
y = 0.5001x + 3.0001 y = 0.5x + 3.0009
2 2 2 2
R = 0.6665 R = 0.6662
0 0
0 5 10 15 0 5 10 15
x x

14
14
12
12
10
10

8 8
y

6 6

4 4
y = 0.4997x + 3.0025 y = 0.4999x + 3.0017
2 2 2 2
R = 0.6663 R = 0.6667
0 0
0 5 10 15 0 5 10 15 20
x x
Linearization of non-linear
relationships

Some data is simply ill- f(x)

suited for linear least


squares regression....

or so it appears. x
P
EXPONENTIAL
EQUATIONS

P  Po e rt

Linearize
t
ln P

intercept = ln P0

slope = r
why?
t
P  P0e rt Can you see the similarity
ln P  ln P0e rt  with the equation for a line:
 ln P0   ln e rt 
y = b + mx
 ln P0   rt

where b is the y-intercept


lnP and m is the slope?

intercept = ln Po slope = r

t
After taking the natural log
P  P0e rt of the y-data, perform linear
ln P  ln P0e rt  regression.
 ln P0   ln e rt 
From this regression:
The value of b will give us
 ln P0   rt
ln (P0). Hence, P0 = eb
ln P0
The value of m will give us r
directly.

intercept = ln P0 slope = r

t
Q
POWER EQUATIONS

Q  cH a

(Flow over a weir)

H
log Q Here we linearize
the equation by
taking the log of
H and Q data.
What is the resulting
log H intercept and slope?
Q  cH a
log Q  log cH a 
 log c  log H a
 log c  a log H

log Q

slope = a

log H

intercept = log c
Q  cH a
So how do we get
log Q  log cH a
 c and a from
 log c  log H a performing regression
on the log H vs log Q
 log c  a log H
data?
From : y = mx + b
log Q
b = log c

slope = a c = 10b

log H m=a
intercept = log c
Polynomial Regression
• One of the reasons you were presented with
the theory behind linear regression was to
allow you the insight behind similar
procedures for higher order polynomials
• y = a0 + a1x
• mth - degree polynomial
y = a0 + a1x + a2x2 +....amxm + e
Based on the sum of the squares
of the residuals
Sr    yi  a o  a1xi  a x ...... a x
2 i
2
m i 
m 2

1. Take the derivative of the above equation with respect


to each of the unknown coefficients: i.e. the partial with
respect to a2
Sr
 2 xi  yi  ao  a1xi  a2 xi2 ..... amxim 
2

a2
2. These equations are set to zero to minimize Sr., i.e.
minimize the error.

3. Set all unknowns values on the LHS of the equation.


Again, using the partial of Sr. wrt a2

ao  xi  a1  xi3  a2  xi4 ..... am  xi   xi yi


2 m 2 2

4. This set of normal equations result in m+1


simultaneous equations which can be solved using
matrix methods to determine a0, a1, a2......am
Example
• Fit a second-order polynomial to the data in the
first two columns of Table below
Example
Multiple Linear Regression
• A useful extension of linear regression is
the case where y is a linear function of two
or more variables
y = ao + a1x1 + a2x2
• We follow the same procedure
y = ao + a1x1 + a2x2 + e
Multiple Linear Regression
For three variables, we would solve a 3 x 3 matrix
in the following form:
 n

x 1i x 2i   a0    yi 
   
  x1i x x x
1i 2 i   a1     x1i yi 
2
1i

 x2 i    x y
x x
1i 2 i x 2
2 i  a 2 
  2 i i 

[A] and {c}are clearly based on data given for x1, x2


and y to solve for the unknowns in {x}.
Interpolation
• General formula for an n-th order
polynomial
y = a0 + a1x + a2x2 +....amxm
• For m+1 data points, there is one, and only
one polynomial of order m or less that
passes through all points
• Example: y = a0 + a1x
– fits between 2 points
– 1st order
Interpolation
• We will explore two mathematical methods
well suited for computer implementation
• Newton’s Divided Difference Interpolating
Polynomials
• Lagrange Interpolating Polynomial
Interpolating Polynomials

• Linear Interpolation
• Quadratic Interpolation
Linear Interpolation
3
Temperature, C Density, kg/m
0 999.9
5 1000.0
10 999.7
15 999.1
20 998.2

How would you approach estimating the density at 17 C?


3
Temperature, C Density, kg/m
0 999.9
5 1000.0
10 999.7
15 999.1
20 998.2 ???
999.1 >  > 998.2

999.1
998.2


15 20

999.1
998.2


15 20

998.2  999.1  Assume a straight line


 between the known data.
20  15 T
Then calculate the slope.

999.1
998.2

17 
15 20

Assuming this linear


  999.1  relationship is constant,

17  15 T the slope is the same
between the unknown
point and a known point.

999.1
998.2

17 
15 20

998.2  999.1   999.1 


  Solve for 
20  15 17 15 T
Therefore, the slope of
one interval will equal the
slope of the other interval.
998.2  999 .1 998.2  

20  15 20  17

f  x1   f  x0 
f1  x   f  xo    x  x0 
x1  x0

Note: The notation f1(x) designates that this is a first order


interpolating polynomial
Quadratic Interpolation

f 2  x  b0  b1  x  x0   b2  x  x0   x  x1 

Prove that this a 2nd order polynomial of


the form:
f  x  a0  a1x  a2 x 2
First, multiply the terms

f 2  x  b0  b1  x  x0   b2  x  x0   x  x1 

f 2  x  b0  b1x  b1x0  b2 x 2  b2 x0 x1  b2 xx0  b2 xx1

Collect terms and recognize that:

a0  b0  b1 x0  b2 x0 x1
f  x  a0  a1x  a2 x 2
a1  b1  b2 x0  b2 x1
a2  b2
x, f(x) x2, f(x2)

f(x)
Procedure for
x1, f(x1)
Quadratic
x0, f(x0)
Interpolation
x
b0  f  x0 
f  x1   f  x0 
b1 
x1  x0
f  x2   f  x1  f  x1   f  x0 

x2  x1 x1  x0
b2 
x2  x0
Procedure for Quadratic
Interpolation
b0  f  x0 
f  x1   f  x0 
b1 
x1  x0
f  x2   f  x1  f  x1   f  x0 

x2  x1 x1  x0
b2 
x2  x0
Procedure for Quadratic
Interpolation
b0  f  x0 
f  x1   f  x0 
b1 
x1  x0
f  x2   f  x1  f  x1   f  x0 

x2  x1 x1  x0
b2 
x2  x0

f 2  x   b0  b1  x  x0   b2  x  x0   x  x1 
Lagrange Interpolating
Polynomial
n
f n  x   Li  x f  xi 
i0

n x  xj
Li  x  
j0 xi  x j
ji

wheredesignates the “product of”


The linear version of this expression is at n=1
Linear version: n=1
n
f n  x   Li  x f  xi 
i 0

n
x  xj
Li  x  
j 0 xi  x j
j i

x  x1 x  x0
f1  f  x0   f  x1 
x0  x1 x1  x0

Your text shows you how to do n=2 (second order).


What would third order be?
n
f n  x    Li  x  f  xi 
i 0
n x  xj
Li  x   
j0 xi  x j
j i

 x  x1   x  x2   x  x3 
f3  f  x0 
 x0  x1   x0  x2   x0  x3 
.......
n
f n  x    Li  x  f  xi 
i 0
n x  xj Note:
Li  x    x1 is
j0 xi  x j
j i
not being
 x  x1   x  x2   x  x3  subtracted
f3  f  x0 
 x0  x1   x0  x2   x0  x3  from the
 x  x0   x  x2   x  x3  constant
 f  x1 
 x1  x0   x1  x2   x1  x3  term x
....... or xi = x1 in
the numerator
or the
denominator
j= 1
n
f n  x    Li  x  f  xi 
i 0
n x  xj Note:
Li  x   
j0 xi  x j x2 is
j i

 x  x1   x  x2   x  x3  not being
f3  f  x0  subtracted
 x0  x1   x0  x2   x0  x3 
from the
 x  x0   x  x2   x  x3 
 f  x1  constant
 x1  x0   x1  x2   x1  x3  term x or
 x  x0   x  x1   x  x3  xi = x2 in
 f  x2 
 x2  x0   x2  x1   x2  x3  the numerator
...... or the
denominator
j= 2
n
f n  x   Li  x f  xi 
i 0
n
x  xj Note:
Li  x  
j 0 xi  x j x3 is
j i
not being
f3 
 x  x1   x  x2   x  x3 
f  x0  subtracted
 x0  x1   x0  x2   x0  x3 
from the

 x  x0   x  x2   x  x3 
f  x1  constant
 x1  x0   x1  x2   x1  x3  term x or

 x  x0   x  x1   x  x3 
f  x2  xi = x3 in
 x2  x0   x2  x1   x2  x3  the numerator

 x  x0   x  x1   x  x2 
f  x3  or the
 x3  x0   x3  x1   x3  x2  denominator
j= 3
Example
1000 Determine the density
999.5 at 17 degrees.
Density

999

998.5
3
998
Temperature, C Density, kg/m
0 5 10 15 20 0 999.9
Tem p
5 1000.0
10 999.7
15 999.1
20 998.2
f 2  17  119.964  839.244  279.496  998.776

f 2  17  998.776
Using Newton’s
f 1  17  998.74 Interpolating Polynomial
Coefficients of an Interpolating
Polynomial
f n  x   b0  b1  x  x0  ....bn  x  x0   x  x1   x  x n  1 

y = a0 + a1x + a2x2 +....amxm

HOW CAN WE BE MORE STRAIGHT


FORWARD IN GETTING VALUES?
f  x0   a 0  a1x0  a 2 x02
f  x1   a 0  a1x1  a 2 x12
f  x2   a 0  a1x2  a2 x32

This is a 2nd order polynomial.

We need three data points.

Plug the value of xi and f(xi)


directly into equations.

This gives three simultaneous equations


to solve for a0 , a1 , and a2

You might also like