View
267
Download
6
Embed Size (px)
Citation preview
Engineering Computation Curve Fitting: Interpolation 1
Curve Fitting by
Interpolation
2. We now discuss Interpolation & ExtrapolationThe function passes through all (or at least most) points.
Curve Fitting:
1. We have discussed Least-Squares Regression where the function is "best fit" to points but does not necessarily pass through the points.
extrapolationinterpolation
Engineering Computation Curve Fitting: Interpolation 2
Curve Fitting by Interpolation:
C&C covers four approaches:
1. Polynomials (C&C 18.1-18.5 – “skim” only)
a) n + 1 equations & n + 1 unknowns
b) Lagrange Polynomials
c) Newton's Divided Difference (NDD) Polynomials
2. Splines (C&C 18.6 – assigned reading)
The first 3 approaches find the same polynomial. We will only cover superficially, and concentrate on Splines.
Engineering Computation Curve Fitting: Interpolation 3
Curve Fitting by Interpolation:
General Scheme
Given: Set of points (xi,yi), not necessarily evenly spaced or in ascending order.
Assume: x = independent variable;
y = dependent variable.
Find: y = f(x) at some value of x not in the set (xi).
Method: Determine the function f(x) which passes through all (or most) points.
Engineering Computation Curve Fitting: Interpolation 4
n+1 Equations and n+1 Unknowns (C&C 18.3)
Given n+1 data points (xi,yi), find an nth order polynomial:
y = a0 + a1x + a2x2 + … + anxn
that will pass through all the points.
Too much work! • Equations are notoriously ill-conditioned for large n. • Equations are not diagonally dominant. • Method is rarely used.
Engineering Computation Curve Fitting: Interpolation 5
Lagrange Interpolating Polynomials (C&C 18.2)
Given n+1 data points (xi,yi), find the nth order polynomial:
y = pn(x) = a0 + a1x + a2x2 + … + anxn
that passes through all of the points. The Lagrangian polynomials
approach employs a set of nth order polynomials, Li(x), such that:
i
n
* L (x)n i
i 0
p (x) y
ii
j
1 at x = x L (x) 0 at x = x where j i
where Li(x) satisfies the condition:
Engineering Computation Curve Fitting: Interpolation 6
Newton's Divided Difference (NDD) Polynomial (C&C 18.1)
Gives the same polynomial as the Lagrange method but is computationally easier. General form for n+1 data points:
pn(x) = b0 + b1(x–x0) + b2(x–x0)(x–x1)
+ … + bn(x–x0)(x–x1)(x–x2)…(x–xn-1)
with b0, b1, ... , bn all unknown.
Note that ith term is zero at xj for j < i. Each term insures that
the polynomial correctly interpolates at one new point. The
algorithm is recursive and readily suited for spreadsheet or
other programmed calculation.
Engineering Computation Curve Fitting: Interpolation 7
i xi f(xi) First Second Third
0 x0 f(x0) f[x1,x0] f[x2,x1,x0] f[x3,x2,x1,x0]
1 x1 f(x1) f[x2,x1] f[x3,x2,x1]
2 x2 f(x2) f[x3,x2]
3 x3 f(x2)
Newton's Divided Differences (NDD)versus
Lagrange Polynomials:
1. Both methods give the same results.2. Comparison based on a count of the FLOPS:
Evaluate coefficients: Interpolate for
one x:
Lagrange: (n+1) (n+1) n2 (n+1) (n+1)
n2
NDD: (n+1) (n+1)/2 n2/2 n
3. Easy to add a node with NDD. Need to start over with Lagrange.
4. Both methods share a major problem: as the number of points increases, so does the order of polynomial. This may cause excessive "wiggles" or "waves" between points.
Engineering Computation Curve Fitting: Interpolation 9
Engineering Computation Curve Fitting: Interpolation 10
Splines (C&C 18.6)
Issue:Need to overcome the "wiggle" or "wave" problem
Idea: Use a piecewise polynomial approximation
Simplest idea:Straight line on each segment.
The problem is that
g'(x) and g"(x) are discontinuous
0
1
2
3
4
0 1 2 3 4 5
Engineering Computation Curve Fitting: Interpolation 11
Splines (cont.)
Most frequently used: Cubic Splines
==> Separate Cubic polynomial on each interval.
This is the analytical/numerical analog of a flexible edge (spline) which is used by draftsmen. With this tool the first and second derivatives (curvature) are continuous, and the function appears "smooth".
Engineering Computation Curve Fitting: Interpolation 12
Cubic Splines
Objective:
Define a 3rd-order polynomial for each interval:
fi(x) = aix3 + bix2 + cix + di
For n+1 data points, (x0,y0), (x1,y1), … , (xn,yn), there are
n intervals with
4 unknowns per interval (ai, bi, ci, and di).
==> Total 4n unknowns.
n=3, 3 segments, n+1 points
0
1
2
3
4
0 1 2 3 4 5
We need 4n equations to compute all 4 unknowns for each interval
Engineering Computation Curve Fitting: Interpolation 13
Cubic Splines
How do we obtain the required 4n equations?• functions must pass through fi(x) at knots (points):
yi-1 = ai(xi-1)3 + bi(xi-1)2 + ci(xi-1) + di
yi = ai(xi)3 + bi(xi)2 + ci(xi) + di
[2 equations/interval = 2n]
• 1st and 2nd derivatives must be equal at interior knots (xi,yi):
3ai(xi)2 + 2bixi + ci = 3ai+1(xi)2 + 2bi+1xi + ci+1
6aixi+ 2bi = 6ai+1xi+ 2bi+1
[ 2 equations/interior knot = 2n-2 ]
TOTAL: 2n + (2n - 2) = 4n - 2
Engineering Computation Curve Fitting: Interpolation 14
Cubic Splines
We need an extra 2 conditions for Cubic SplinesNatural Splines
Setting the 2nd derivatives at exterior knots equal to zero allows the function to "relax":
0 = 6a1x0 + 2b1 & 0 = 6anxn+1
+ 2bn
[ 1 equations/exterior knot = 2 ]
TOTAL: 2n + (2n - 2) +2 = 4n
Engineering Computation Curve Fitting: Interpolation 15
Cubic Splines
Alternatives for two extra conditions(instead of setting 2nd derivative = 0)
1) Specify 1st derivatives at exterior knots.f '(x0) = 3a1x0
2 + b1x0
f '(xn+1) = 3anxn+12 + bn xn+1
2) Add an extra point to the first and last intervals through which spline must pass: not-a-knot splines.
Engineering Computation Curve Fitting: Interpolation 16
Cubic Splines Computation
If set up cleverly, the 4n x 4n system of equations can be reduced to solving an (n-1) x (n-1) tridiagonal system of equations.
Define a new set of unknowns: Let si = f "(xi) be the second derivative of the cubic spline at interior point i, i = 1, ..., n-1. First set up the n-1 equations to solve for curvatures, f "(x) at each of the interior knots (see C&C Box 18.3):
(xi – xi-1) s i-1 + 2 (x i+1– x i-1) s i + (x i+1– x i) s i+1
i 1 i i i 1
i 1 i i i 1
6[f (x ) f (x )] 6[f (x ) f (x )]
(x x ) (x x )
Engineering Computation Curve Fitting: Interpolation 17
Natural Cubic Splines
Convenient tridiagonal equations for natural splines:These basic equations for the second derivatives can also be written in terms of the distances hi = (xi+1–xi) between the points or knots and of yi = f(xi):
For i = 0: s0 = 0 [natural spline condition]
For i = 1: 2 (h0 +h1) s1 + h1s2 = RHS1
For i = 2 to n–2: hi-1s i-1 + 2 (h i-1 + h i) s i + h is i+1 = RHSi
For i = n–1: h n-2 sn-2 + 2 (h n-2 + h n-1) s n-1 = RHSn-1
For i = n: sn = 0 [natural spline condition]y y y yi 1 i i i 1RHS 6i h hi i 1
Engineering Computation Curve Fitting: Interpolation 18
Natural Cubic Splines
Noting that this is a triangular banded system of equations of order n-1, we solve with a method which takes advantage of this, i.e., the Thomas algorithm given in C&C Section 11.1.
Solve the triangular banded system, e.g., for n = 7 looks like:
s1 s2 s3 s4 s5 s6
x x - - - -x x x - - -- x x x - -- - x x x -- - - x x x- - - - x x
Engineering Computation Curve Fitting: Interpolation 19
Cubic Spline Interpolation
If we want to find specific interpolants, we do not need to determine the cubic functions in all of the intervals, rather just the interval in which the x lies. For the ith interval spanning [xi-1,xi]:
fi(x) = ai(x–xi-1)3 + bi(x–xi-1)2 + ci(x–xi-1) + di
in which: i i 1i
i 1
s sa
6h
i 1
is
b2
i i 1 i 1 i i 1i
i 1
y y (2s s )hc
h 6
di = yi-1
Obtain a different cubic polynomial for each interval [xi-1, xi].
However, first need to solve for the values of all the si .
Engineering Computation Curve Fitting: Interpolation 20
Natural Cubic Splines: Example
Set up equations to solve for the unknown curvatures at each interior knot for the following data:
(xi – xi-1) si-1 + 2 (xi+1 – xi-1) si + (xi+1 – xi) si+1
i 1 i i i 1
i 1 i i i 1
6[f (x ) f (x )] 6[f (x ) f (x )]
(x x ) (x x )
i xi y = f(xi) si
0 1 4.75 0
1 2 4 ?
2 3 5.25 ?
3 5 19.75 ?
4 6 42 0
Engineering Computation Curve Fitting: Interpolation 21
Splines: Example (cont'd)
For i = 1: (2 – 1) s0 + 2 (3 – 1) s1 + (3 – 2) s2
6[5.25 4] 6[4 4.75]
(3 2) (2 1)
or 4 s1 + s2 = 12
For i = 2: s1 + 6 s2 + 2 s3 = 36
For i = 3: 2 s2 + 6 s3 = 90
i xi y = f(xi)
si
0 1 4.75 0
1 2 4 ?
2 3 5.25 ?
3 5 19.75 ?
4 6 42 0
Engineering Computation Curve Fitting: Interpolation 22
Splines: Example (cont'd)
1
2
3
4 1 0 s 12
1 6 2 s 36
0 2 6 s 90
1
2
3
s 2.85
s 0.59
s 14.80
Solve by any appropriate method (e.g., Thomas algorithm):
Engineering Computation Curve Fitting: Interpolation 23
Splines: Example (cont'd)
Now estimate f(2.4) by using the spline:
We need only solve for the ith=2nd interval, i.e.,
(x1 = 2) < x < (x2 = 3):
f2(x) = a2(x–x1)3 + b2(x–x1)2 + c2(x–x1) + d2
i xi y = f(xi) si
0 1 4.75 0
1 2 4 2.85
- 2.4 ? -
2 3 5.25 0.59
3 5 19.75 14.80
4 6 42 0
Engineering Computation Curve Fitting: Interpolation 24
Splines: Example (cont'd)
Solving: f2(x) = a2(x–x1)3 + b2(x–x1)2 + c2(x–x1) + d2
2 1
22 1
s s ) 0.59 2.85a
6(x x ) 6 32
-0.377
12
s 2.85b
2 2 1.43
2 1 1 2 2 12
2 1
f (x ) f (x ) [s s ](x x )c
(x x ) 6
5.25 4 [2(2.85) 0.59](3 2)
(3 2) 6
0.202
d2 = f(x1) = 4.00
f2(2.4) = – 0.377(2.4–2)3 + 1.43(2.4–2)2 + 0.202(2.4–2) + 4.00 = 4.29
x = 0:10; y = sin(x); xx = 0:0.25:10; yy = spline(x,y,xx); plot(x,y,'o',xx,yy)
Splines: Example in Matlab
0 2 4 6 8 10-1
-0.5
0
0.5
1
x = 0:10; y = sin(x); xx = 0:0.25:10; yy = spline(x,[0 y 0],xx); plot(x,y,'o',xx,yy)
0 2 4 6 8 10-1
-0.5
0
0.5
1
Engineering Computation Curve Fitting: Interpolation 25
npts = 10;xy = [randn(1,npts); randn(1,npts)];plot(xy(1,:),xy(2,:),'ro','LineWidth',2);for n = 1:npts, text(xy(1,n),xy(2,n),[' ' num2str(n)])endset(gca,'XTick',[],'YTick',[])
cv = cscvn(xy);fnplt(cv,'r',2)fnplt(cscvn(xy),'r',2)
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2-1.5
-1
-0.5
0
0.5
1
1.5
1
2 3
4
5
6
7
8
9 10
Engineering Computation Curve Fitting: Interpolation 26