Title: Curve Fitting and Interpolation
1 Curve Fitting and Interpolation
2INTERPOLATION
Interpolation is a method of constructing new
data points from a discrete set of known data
points
In engineering science one often has a number
of data points, as obtained by sampling or some
experiment, and tries to construct a function
which closely fits those data points.
This is called CURVE FITTING
Interpolation is a specific case of curve
fitting, in which the function must go exactly
through the data points
3Interpolation vs. Approximation
Find a function that passes through a set
of data points. The function passes through
each point.
Interpolation
Approximation
Find a function that comes close to a set
of data points. The function might not pass
through any of the points.
4Interpolation Purpose
Find a function that passes through a set of data
points. For each data point (xi, yi),
where yi f(xi)
The function could be a polynomial, an
exponential, or any other function.
Purpose? To be able to estimate the value for
some intermediate point.
5Interpolation Purpose-cont.
Given (x0,y0), (x1,y1), (xn,yn), find the
value of y at a value of x that is not given.
We want to find these points so we need to know
the function
6Interpolating Two Points with a Linear Function
y
f(x) axb
x
Goal given two points, estimate y-value at any
x xnew
From the two points, determine a and b, to find
f(x).
Use f(x) to find the y-value at xnew by
evaluating f(xnew)
7Polynomial Interpolation
2 points a line (1st degree polynomial) 3
points a parabola (2nd degree polynomial) 4
points a cubic polynomial (3nd degree
polynomial) N points N-1 degree polynomial
Basic Polynomial Interpolation Result There
exists exactly one polynomial of degree N-1 that
passes through N points.
8Basic Polynomial Interpolation Result
There is exactly one polynomial of degree N-1
that passes through N points.
Here are different polynomials that interpolate
the same three data points.
2nd degree polynomial
5th degree polynomials
9Exact degree may be less than N-1 if coefficient
of x(N-1) is zero
NOTE?
Interpolate 3 points (N3) (0, 1), (1, 2), (2,
3)
f(x) x 1
0 x2 x 1
Nevertheless, using a polynomial of degree less
than N-1 to fit N data points, in most cases you
would be approximating the data, not
interpolating the data.
10Polynomial Interpolation in Matlab
polyfit - to find the polynomial (the
coefficients)
x -1.0, -0.7, -0.2, 0.6, 1.0 y -2.1,
0.3, 1.7, 1.9, 2.1 For 5 points, we need a
4th degree polynomial coef polyfit(x, y, 4)
polyval to evaluate the polynomial (e.g. to
graph it)
interp_at_one_fourth polyval(coef, 0.25)
Evaluate at a range of values for a smooth
plot xx -1.00.11.0 yy polyval(coef,
xx) plot(x,y,'o', xx,yy, 0.25,
interp_at_one_fourth, 'sk')
11MATLAB GIVES YOU?
p(x) -0.7801 x4 1.8224 x3 -1.0326 x2 0.2776
x 1.8126
2.5
2
1.5
1
0.5
0
-0.5
-1
-1.5
-2
-2.5
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
12Interpolation of Many Data Points
Data sets are often much larger than 4 or 5 data
points.
4
This data may look as if it could be smoothly
interpolated by a polynomial...
3.5
3
2.5
2
But problems arise where the polynomial shoots
way beyond the normal range of the data
1.5
1
0.5
0
0
1
2
3
4
5
6
7
8
9
10
13Interpolation of Many Data Points
"Well behaved" data can be conveniently
interpolated with lower order polynomials.
Irregular data is difficult to interpolate.
Polynomial interpolation of many data points
would require a higher degree polynomial.
Higher degree polynomials are usually not used
for interpolation.
14Interpolation with Splines
A function S(x) that interpolates N data points
(xk, yk) is a cubic spline if it satisfies the
following
1) Between successive xk and xk1, S(x) is a
cubic polynomial. 2) At each data point xk,
S(x) is continuous. 3) At each data point xk,
S(x) is continuous.
A spline is a piecewise function.
15Splines are Piecewise Functions
4
4
3
3
2
2
1
1
0
0
-1
-1
0
0.5
1
1.5
2
2.5
3
0
0.5
1
1.5
2
2.5
3
One function, S(x), where
Three separate functions
f(x) xx2-x3
S(x) f(x) for 0ltxlt1
g(x) -3-8x22x310x
S(x) g(x) for 1ltxlt2
h(x) 69-98x46x2-7x3
S(x) h(x) for 2ltxlt3
16Splines in Matlab
Matlab has a spline command
5 known data points x -1.0 -0.7 -0.2 0.6
1.0 y -2.1 0.3 1.7 1.9 2.1
interpolate value at 0.25 x_new 0.25 y_new
spline(x,y, x_new) find many values xx -1.0
0.1 1.0 yy spline(x, y, xx) plot(x,y,'o'
) hold on plot(x_new, y_new, 'sk') plot(xx,
yy,'g')
2.5
2
1.5
1
0.5
0
-0.5
-1
-1.5
-2
-2.5
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
17Spline vs. Polynomial for Many Data Points
Cubic spline
14th degree polynomial
18Next Lecture Curve Fitting