Transcript Document
ES 240: Scientific and Engineering Computation.
Interpolation
Polynomial
Definition
– a function f(x) that can be written as a finite series of power functions
like
n
f n ( x) a i x i
i 0
– fn is a polynomial of order n
– A polynomial is represented by coefficient vector from highest power.
– p1=[3 -5 -7 1 9] p1(x) = 3x4 - 5x3 – 7x2 + x + 9
ES 240: Scientific and Engineering Computation.
Interpolation
Polynomial Operations
poly(r)
– convert roots to a polynomial
– r=[ 1 2 3 ]; poly(r)
roots(p)
– find roots of a polynomial
– p=[ 2 3 4 ]
– roots(p)
Y = polyval(p, x)
– returns the value of a polynomial, p(x).
– P is a vector of length N+1 whose elements are the coefficients of the
polynomial in descending powers.
– p=[3 -5 -7 1 9] p(x) = 3x4 - 5x3 – 7x2 + x + 9
ES 240: Scientific and Engineering Computation.
Interpolation
Polynomial Operations
P = polyfit(x, y, n)
– Returns the least squares fit coefficients of a polynomial
p(x) of degree n
– P is of length N+1.
conv(p1,p2)
– multiply two polynomials p1 and p2
– conv(p1,p2)
[q r]=deconv(p1,p2)
– polynomials p1 divide by p2, where q is quotient and r is
remainder
– [q r]=deconv(p1,p2)
ES 240: Scientific and Engineering Computation.
Interpolation
Polynomial Operations
polyint(p,c)
– integrate of polynomial p with integration constant c
(default 0)
– polyint(p)
– polyint(p,1)
polyder(p)
– differentiate polynomial p respective to x
– >> polyder(p)
ES 240: Scientific and Engineering Computation.
Interpolation
Curve Fitting
Applications
– Estimating the value of points between discrete values
– Simplifying complicated functions
Methods
– Interpolation
• Data are very precise
• curve passes through all points
– curve fitting
• Data are just approximations
• curve represent a general trend of the data
ES 240: Scientific and Engineering Computation.
Interpolation
Curve Fitting and Interpolation
Curve Fitting
(linear or non-linear)
Linear Interpolation
(most popular
interpolation)
Other Interpolation
(higher order
polynomial,
spline, nearest,…)
ES 240: Scientific and Engineering Computation.
Interpolation
Linear Interpolation
The linear interpolation is
achieved by fitting a line
between two known data points
The resulting formula based on
known points x1 and x2 and the
values of the dependent
function at those points is:
f1 x f x1
f x2 f x1
x x1
x2 x1
ES 240: Scientific and Engineering Computation.
Interpolation
Quadratic (Polynomial) Interpolation
One problem that can occur with solving for the coefficients of a
polynomial is that the system to be inverted is in the form:
x1n1
n1
x
2
n1
xn1
n1
xn
x1n2
x2n2
x1
x2
n2
xn1
xnn2
xn1
xn
1 p1 f x1
f x
1
p
2 2
1pn1 f xn1
1
pn f xn
Matrices such as that on the left are known as Vandermonde matrices,
and they are very ill-conditioned - meaning their solutions are very
sensitive
to round-off errors.
The issue can be minimized by scaling and shifting the data.
ES 240: Scientific and Engineering Computation.
Interpolation
Newton Interpolating Polynomials
Another way to express a polynomial interpolation is to use Newton’s
interpolating polynomial.
This is a achieved by an extension to linear interpolation
Order
1st
2nd
Simple
f1 ( x) a1 a2 x
f 2 ( x) a1 a2 x a3 x 2
Newt on
f1 ( x) b1 b2 ( x x1 )
f 2 ( x) b1 b2 ( x x1 ) b3( x x1 )(x x2 )
b1 f1 ( x );
b2
f ( x2 ) f ( x1 )
x2 x1
f x3 f x2
f x2 f x1
x3 x2
x2 x1
b3
x3 x1
ES 240: Scientific and Engineering Computation.
Interpolation
Newton Interpolating Polynomials (cont)
The second-order Newton interpolating
polynomial introduces some curvature
to the line connecting the points, but
still goes through the first two points.
The resulting formula based on known
points x1, x2, and x3 and the values of the
dependent function at those points is:
f x3 f x2 f x2 f x1
x2 x1
f x2 f x1
x x1 x3 x2
x x1 x x2
f 2 x f x1
x2 x1
x3 x1
ES 240: Scientific and Engineering Computation.
Interpolation
Generalization
An (n-1)th Newton interpolating polynomial has all the terms of the
(n-2)th polynomial plus one extra.
The general formula is:
where
fn1 x b1 b2 x x1
bn x x1 x x2
and the f[…] represent divided differences.
b1 f x1
b2 f x2 , x1
b3 f x3 , x2 , x1
bn f xn , xn1, , x2 , x1
x xn1
ES 240: Scientific and Engineering Computation.
Interpolation
Divided Differences
Divided difference are calculated as follows:
f xi , x j
f xi f x j
f xi , x j , xk
xi x j
f xi , x j f x j , xk
xi xk
f xn , xn1, , x2 , x1
f xn , xn1, , x2 f xn1 , xn2 , , x1
xn x1
Divided differences are calculated using divided difference
of a smaller number of terms:
ES 240: Scientific and Engineering Computation.
Interpolation
Example
15.1
Do by hand
Use the newtint function
ES 240: Scientific and Engineering Computation.
Interpolation
Lagrange Interpolating Polynomials
Weighted average of the two values being connected
The differences between a simple polynomial and Lagrange interpolating
polynomials for first and second order polynomials is:
Order
1st
2nd
Simple
f1 (x) a1 a2 x
f2 (x) a1 a2 x a3 x 2
Lagrange
f1 (x) L1 f x1 L2 f x2
f2 (x) L1 f x1 L2 f x2 L3 f x 3
where the Li are weighting coefficients that are functions of x.
ES 240: Scientific and Engineering Computation.
Interpolation
Lagrange Interpolating Polynomials (cont)
The first-order Lagrange
interpolating polynomial may be
obtained from a weighted
combination of two linear
interpolations, as shown.
The resulting formula based on
known points x1 and x2 and the
values of the dependent
function at those points is:
f1 (x) L1 f x1 L2 f x2
x x2
x x1
L1
, L2
x1 x2
x2 x1
f1 (x)
x x2
x x1
f x1
f x2
x1 x2
x2 x1
ES 240: Scientific and Engineering Computation.
Interpolation
Lagrange Interpolating Polynomials (cont)
In general, the Lagrange polynomial interpolation
for n points is:
n
fn1 xi Li x f xi
i1
where Li is given by:
x xj
Li x
j1 xi x j
n
ji
ES 240: Scientific and Engineering Computation.
Interpolation
Example
15.1
Solve by hand
Use the lagrange function
ES 240: Scientific and Engineering Computation.
Interpolation
Extrapolation
Extrapolation is the process
of estimating a value of f(x)
that lies outside the range of
the known base points x1, x2,
…, xn.
Extrapolation represents a
step into the unknown, and
extreme care should be
exercised when extrapolating!
ES 240: Scientific and Engineering Computation.
Interpolation
Extrapolation Hazards
World population using a 7th order polynomial
extrapolation.
ES 240: Scientific and Engineering Computation.
Interpolation
Oscillations
Higher-order polynomials can not only lead to round-off errors due to
ill-conditioning, but can also introduce oscillations to an interpolation
or fit where they should not be.
In the figures below, the dashed line represents a function, the circles
represent samples of the function, and the solid line represents the
results of a polynomial interpolation:
ES 240: Scientific and Engineering Computation.
Interpolation
Introduction to Splines
An alternative approach to using a single
(n-1)th order polynomial to interpolate between n
points is to apply lower-order polynomials in a
piecewise fashion to subsets of data points.
These connecting polynomials are called spline
functions.
Splines minimize oscillations and reduce roundoff error due to their lower-order nature.
ES 240: Scientific and Engineering Computation.
Interpolation
Higher Order vs. Splines
Splines eliminate oscillations by using
small subsets of points for each
interval rather than every point. This is
especially useful when there are jumps
in the data:
a)
b)
c)
d)
3rd order polynomial
5th order polynomial
7th order polynomial
Linear spline
•
seven 1st order polynomials generated by using
pairs of points at a time
ES 240: Scientific and Engineering Computation.
Interpolation
Spline Development
a)
First-order splines find straight-line
equations between each pair of
points that
•
b)
Go through the points
Second-order splines find quadratic
equations between each pair of
points that
•
•
c)
Go through the points
Match first derivatives at the interior
points
Third-order splines find cubic
equations between each pair of
points that
•
•
Go through the points
Match first and second derivatives at
the interior points
Note that the results of cubic spline
interpolation are different from the results of
an interpolating cubic.
ES 240: Scientific and Engineering Computation.
Interpolation
Spline Development
Spline function (si(x))coefficients are calculated
for each interval of a data set.
The number of data points (fi) used for each spline
function depends on the order of the spline
function.
ES 240: Scientific and Engineering Computation.
Interpolation
Cubic Splines
While data of a particular size presents many options for the order of
spline functions, cubic splines are preferred because they provide the
simplest representation that exhibits the desired appearance of
smoothness.
In general, the ith spline function for a cubic spline can be written as:
si x ai bi x xi ci x xi di x xi
2
3
For n data points, there are n-1 intervals and thus 4(n-1) unknowns to
evaluate to solve all the spline function coefficients.
There is no ‘one equation’ that can represent the whole spline function
on the domain
ES 240: Scientific and Engineering Computation.
Interpolation
Piecewise Interpolation in MATLAB
MATLAB has several built-in functions to implement
piecewise interpolation. The first is spline:
yy=spline(x, y, xx)
This performs cubic spline interpolation
ES 240: Scientific and Engineering Computation.
Interpolation
Example
Generate data:
x = linspace(-1, 1, 9);
y = 1./(1+25*x.^2);
Calculate 100 model points and
determine not-a-knot interpolation
xx = linspace(-1, 1);
yy = spline(x, y, xx);
Calculate actual function values
at model points and data points, the
9-point (solid),
and the actual function (dashed),
yr = 1./(1+25*xx.^2)
plot(x, y, ‘o’, xx, yy, ‘-’, xx, yr, ‘--’)
ES 240: Scientific and Engineering Computation.
Interpolation
MATLAB’s interp1 Function
While spline can only perform cubic splines, MATLAB’s
interp1 function can perform several different kinds of
interpolation:
yi = interp1(x, y, xi, ‘method’)
– x & y contain the original data
– xi contains the points at which to interpolate
– ‘method’ is a string containing the desired method:
•
•
•
•
‘nearest’ - nearest neighbor interpolation
‘linear’ - connects the points with straight lines
‘spline’ - not-a-knot cubic spline interpolation
‘pchip’ or ‘cubic’ - piecewise cubic Hermite interpolation
ES 240: Scientific and Engineering Computation.
Interpolation
Lab
15.9