Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 26

ENG 1060

Computing for Engineers


Lecturer: Dr Christopher Hutchinson
Lecture 20:
Curve Fitting, Interpolations and Extrapolations
Last Lecture
• Last lecture • We introduced the concepts of Curve fitting,
• Today’s Lecture interpolation and extrapolation as they apply to sets
• Polynomial
of data.
Interpolation

• Polynomial
Extrapolation • Introduced the basic statistics underlying Curve
• Be Careful! fitting methods including methods for quantifying
• Lecture
the ‘goodness of fit’
Summary

• Next lecture
• We looked at both linear and non-linear curve fits

• Used MATLAB to perform both linear and non-linear


curve fits

2
Last Lecture
Consider you are lucky enough to be designing the
• Last lecture
aero package on a F1 car.
• Today’s Lecture

• Polynomial
Interpolation

• Polynomial
Extrapolation

• Be Careful!

• Lecture
Summary

• Next lecture

The name of the game is to maximize down force


and minimizing drag
3
Last Lecture
The down force can be measured by a pressure
• Last lecture
sensitive floor in a wind tunnel.
• Today’s Lecture

• Polynomial
Interpolation

• Polynomial
Extrapolation

• Be Careful!

• Lecture
Summary

• Next lecture

The floor can measure the ‘weight’ (i.e. force exerted by the car
on the floor) of the car as a function of wind velocity.
4
Last Lecture
The down force can be measured by a pressure
• Last lecture
sensitive floor in a wind tunnel.
• Today’s Lecture

• Polynomial After many tests, typical data collection may look like:
Interpolation

• Polynomial
Extrapolation

• Be Careful!

• Lecture
Summary

• Next lecture

• Results show a trend


• Results scattered
5
Last Lecture
Often in engineering you require the convenience of
• Last lecture
representing this data by a function
• Today’s Lecture
(i.e. you require a curve fitting function)
• Polynomial
Interpolation

• Polynomial • a reasonable fit?


Extrapolation

• Be Careful!

• Lecture
Summary

• Next lecture

You will already be familiar with one type: ‘line of best fit’6
Last Lecture
Or you may need an estimate of the down force for
• Last lecture
a velocity of 55 m/s even though you have not done
• Today’s Lecture
experiments at this velocity.
• Polynomial
Interpolation (i.e you need to interpolate between data points)
• Polynomial
Extrapolation

• Be Careful!

• Lecture
Summary

• Next lecture
?

7
Last Lecture
Or your wind tunnel may have a maximum wind
• Last lecture
velocity of 80m/s but you need to estimate the
• Today’s Lecture
down force at 120 m/s
• Polynomial
Interpolation (i.e you need to extrapolate the data)
• Polynomial
Extrapolation

• Be Careful!

• Lecture ?
Summary

• Next lecture

8
Last Lecture
Commonly accepted method :
• Last lecture
n n For a ‘linear’ line of
• Today’s Lecture
Minimise S r   e   ( yi  a0  a1 xi )
2
i
2
best fit (y=a0+a1x)
• Polynomial i =1 i =1
Interpolation

• Polynomial
Extrapolation Sr  Sr (a0, a1 )
• Be Careful!

• Lecture
Summary

• Next lecture  Sr minimum when :


Sr
Sr Sr
 0 and 0
a 0 a 1

a1 a0


9
Last Lecture
In practice how do we find ao and a1 of the line of best fit;
• Last lecture
y=ao+a1x?
• Today’s Lecture
n a0  ( x i ) a1   y i
• Polynomial
Interpolation Resultant equations :
• Polynomial ( x i ) a0  ( x i2 ) a1   x i y i
Extrapolation

• Be Careful!
Using the ‘elimination of unknowns’ from last weeks lecture we arrive
• Lecture at equations for a0 and a1.
Summary

n  x i y i   x i  y i
• Next lecture

a1  x i  nx y i  ny
 x 
2
n  x i2  i

Whenever you use EXCEL to do


a0 
 y i  a1 x i a line of best fit, these are Eqn’s
n  it uses

This is also often referred to as : Linear Regression


10
Last Lecture: MATLAB Curve fitting
MATLAB function polyfit
• Last lecture
polyfit(x,y,n)
• Today’s Lecture
• fits a polynomial of order n to the (x, y) data
• Polynomial • x & y vectors
Interpolation

• Polynomial
Extrapolation
p1 x n  p2 x n1         pn x  pn 1
• Be Careful!

• Lecture If length(x) and length(y) = n+1 the


Summary equation passes through all points
• Next lecture 
If length(x) and length(y) > n+1 least
squares fitting is applied

polyfit(x,y,1) will return the slope and intercept of


the linear least squares line of best fit.

11
Today’s Lecture
• Last lecture • Using MATLAB for interpolations and
• Today’s Lecture
extrapolations
• Polynomial
Interpolation

• Polynomial
Extrapolation

• Be Careful!

• Lecture
Summary

• Next lecture

12
Polynomial Interpolation
Curve Fitting: Polynomial Interpolation
• Last lecture

• Today’s Lecture T ( o C)  (kg /m 3 )


• Polynomial 50 1.09
Interpolation
100 0.946
• Polynomial
150 0.835
Extrapolation Suppose we need to find 
200 0.746 at T = 350 oC
• Be Careful!
250 0.675
• Lecture
Summary 300 0.616
• Next lecture 400 0.525
500 0.457
Temperature
dependence
of air density
at 1 atmosphere

13
Polynomial Interpolation
METHOD [1]
• Last lecture
T ( o C)  (kg / m 3 ) >> T = [50 100 150 200 250 300 400 500];
• Today’s Lecture
50 1.09
• Polynomial Input data
Interpolation 100 0.946
• Polynomial
150 0.835
Extrapolation 200 0.746 fit quadratic to available data
• Be Careful! 250 0.675
• Lecture 300 0.616
Summary
400 0.525
• Next lecture
500 0.457 evaluate density at T = 350
Temperature 0.55214321852500
dependence
d350 = METHOD [1] 0.5521
of air density
Suppose we need to find  >> d350 = polyval(p3,350)
‘polyval’ MATLAB function allows us
at T = 350 oC to interpolate easily
 0.00000260649932 -
0.00278303561004 14
Polynomial Interpolation
METHOD [2]
• Last lecture
T ( o C)  (kg /m 3 ) >> T = [50 100 150 200 250 300 400 500];
• Today’s Lecture 50 1.09
• Polynomial 100 0.946 Input data
Interpolation
>> p2=polyfit(T(6:8), dens(6:8), 2)
150 0.835
• Polynomial
200 0.746 fit quadratic to T =
Extrapolation
300, 400, 500
• Be Careful! 250 0.675
• Lecture 300 0.616
Summary
400 0.525
• Next lecture 500 0.457
evaluate density at T = 350
Temperature
dependence
0.56762500000000
of air density
METHOD [2] 0.5676
Suppose we need to find  d350 = QUITE DIFFERENT
at T = 350 oC METHOD [1] 0.5521
 >> d350 = polyval(p2,350)

0.00000115000000 - 15
Polynomial Interpolation
• Last lecture METHOD [1] 0.5521

• Today’s Lecture Not fantastic here


• Polynomial
Interpolation But generally “OK”
• Polynomial
Extrapolation

• Be Careful!

• Lecture
Summary

• Next lecture METHOD [2] 0.5676

better here

But generally NOT “OK”

16
Polynomial Interpolation
• Last lecture
Curve Fitting: Polynomial Interpolation
• Today’s Lecture

• Polynomial
Interpolation

• Polynomial
f ( x)  p1x n  p2 x n1  p3 x n2    pn1 x 2  pn x  pn1
Extrapolation

• Be Careful!

• Lecture
Summary Polynomial interpolation:
(a) choose the order of polynomial [usually as low as possible]
• Next lecture

(b) choose the coefficients so that the polynomial passes through the
tabulated points closest to the required intermediate point

17
Polynomial Interpolation
Examples of interpolating polynomials:
(a) first-order (linear) connecting two points,
• Last lecture
(b) second-order (quadratic or parabolic) connecting three points,
• Today’s Lecture (c) third-order (cubic) connecting four points.
• Polynomial
Interpolation • linear interpolation
• Polynomial
Extrapolation • quadratic interpolation
• Be Careful!

• Lecture
Summary

• Next lecture

• cubic interpolation

18
Polynomial Extrapolation
Extrapolation: estimating values outside the range where data is available
• Last lecture
Extrapolation based on fitting a parabola through the first three known points.
• Today’s Lecture

• Polynomial
Interpolation

• Polynomial
Extrapolation

• Be Careful!

• Lecture
Summary

• Next lecture
• It can go badly wrong!

19
Polynomial Extrapolation
Use of a seventh-order polynomial to make a prediction of U.S. population in 2000
• Last lecture based on data from 1920 through 1990.
• Today’s Lecture

• Polynomial Actual population in 2000 was 281.42


Interpolation

• Polynomial
Extrapolation

• Be Careful!

• Lecture
Summary

• Next lecture
Prediction!

20
Be careful with Inter/Extrapolation
• Last lecture
Oscillations
• Today’s Lecture
• although "more is better" in many contexts, it is absolutely not
• Polynomial
Interpolation
true for polynomial interpolation'.
• Polynomial
Extrapolation • one problem is that higher order polynomials tend to be ill-
conditioned. They are highly susceptible to round-off errors.
• Be Careful!
They are also very sensitive to changes in the values of the
• Lecture coefficients
Summary

• Next lecture • forced to go through a large number of points to determine a


large number of coefficients

• tendency for oscillations occur between the points.

21
Be careful with Inter/Extrapolation
Comparison of Runge’s function (dashed line) with a fourth-order polynomial fit to
• Last lecture 5 points sampled from the function.
• Today’s Lecture

• Polynomial
Interpolation 1
f (x) 
• Polynomial 1  25x 2
Extrapolation

• Be Careful!

• Lecture
Summary

• Next lecture

22
Be careful with Inter/Extrapolation
Comparison of Runge’s function (dashed line) with a tenth-order polynomial fit to
• Last lecture 11 points sampled from the function.
• Today’s Lecture

• Polynomial
Interpolation

• Polynomial
Extrapolation

• Be Careful!

• Lecture
Summary

• Next lecture

23
Be careful with Inter/Extrapolation
• Last lecture
•choice of interpolating function is sometimes difficult
• Today’s Lecture

• Polynomial •general rule, in the case of polynomials, is to choose the


Interpolation
lowest possible order
• Polynomial
Extrapolation
•linear interpolation is often adequate
• Be Careful!

• Lecture
Summary
•usually different coefficients will apply to different sections
of a table
• Next lecture

•always wise to plot the data and the interpolating function

24
Lecture Summary
• Last lecture • Used the ‘polyval’ MATLAB function to
• Today’s Lecture interpolate between data points and to
• Polynomial extrapolate outside the range of data points
Interpolation

• Polynomial
Extrapolation
• Highlighted a number of possible problems
• Be Careful!
with interpolation and extrapolation
• Lecture
Summary (in particular if curve fitting with high order
• Next lecture polynomials)

• Good rule is to plot you data and the curve fit


to be sure nothing surprising is happening

25
Next Lecture
• Last lecture • We move on to the much more interesting
• Today’s Lecture topic of ‘Numerical Integration’.
• Polynomial
Interpolation
• This is one of the most important numerical
• Polynomial
tools you will learn during your education
Extrapolation

• Be Careful!

• Lecture
Summary

• Next lecture

26

You might also like