 Engineering Computation Curve Fitting: Interpolation 1

Presentation on theme: "Engineering Computation Curve Fitting: Interpolation 1"— Presentation transcript:

Engineering Computation Curve Fitting: Interpolation 1
Curve Fitting by Interpolation

2. We now discuss Interpolation & Extrapolation
Engineering Computation Curve Fitting: Interpolation 2 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. 2. We now discuss Interpolation & Extrapolation The function passes through all (or at least most) points. extrapolation interpolation

Curve Fitting by Interpolation:
Engineering Computation Curve Fitting: Interpolation 3 Curve Fitting by Interpolation: C&C covers four approaches: Polynomials (C&C – “skim” only) n + 1 equations & n + 1 unknowns Lagrange Polynomials Newton's Divided Difference (NDD) Polynomials Splines (C&C 18.6 – assigned reading) The first 3 approaches find the same polynomial. We will only cover superficially, and concentrate on Splines.

Curve Fitting by Interpolation: General Scheme
Engineering Computation Curve Fitting: Interpolation 4 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.

n+1 Equations and n+1 Unknowns (C&C 18.3)
Engineering Computation Curve Fitting: Interpolation 5 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.

Lagrange Interpolating Polynomials (C&C 18.2)
Engineering Computation Curve Fitting: Interpolation 6 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: where Li(x) satisfies the condition:

Newton's Divided Difference (NDD) Polynomial (C&C 18.1)
Engineering Computation Curve Fitting: Interpolation 7 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.

i xi f(xi) First Second Third 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

Newton's Divided Differences (NDD)
Engineering Computation Curve Fitting: Interpolation 9 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 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

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. We need 4n equations to compute all 4 unknowns for each interval n=3, 3 segments, n+1 points

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) 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 Splines Natural 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) = 3a1x 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 (x i+1– x i-1) s i + (x i+1– x i) s i+1

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 (h i-1 + h i) s i + h is i+1 = RHSi For i = n–1: h n-2 sn (h n-2 + h n-1) s n-1 = RHSn-1 For i = n: sn = 0 [natural spline condition]

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: 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: i xi y = f(xi) si 1 4.75 2 4 ? 3 5.25 5 19.75 6 42 (xi – xi-1) si (xi+1 – xi-1) si + (xi+1 – xi) si+1

Engineering Computation Curve Fitting: Interpolation 21
xi y = f(xi) si 1 4.75 2 4 ? 3 5.25 5 19.75 6 42 Splines: Example (cont'd) For i = 1: (2 – 1) s0 + 2 (3 – 1) s1 + (3 – 2) s2 or s s2 = 12 For i = 2: s s s3 = 36 For i = 3: s s3 = 90

Engineering Computation Curve Fitting: Interpolation 22
Splines: Example (cont'd) 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: i xi y = f(xi) si 1 4.75 2 4 2.85 - 2.4 ? 3 5.25 0.59 5 19.75 14.80 6 42 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

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 d2 = f(x1) = 4.00 f2(2.4) = – 0.377(2.4–2) (2.4–2) (2.4–2) = 4.29

Splines: Example in Matlab
Engineering Computation Curve Fitting: Interpolation 25 Splines: Example in Matlab x = 0:10; y = sin(x); xx = 0:0.25:10; yy = spline(x,y,xx); plot(x,y,'o',xx,yy) x = 0:10; y = sin(x); xx = 0:0.25:10; yy = spline(x,[0 y 0],xx); plot(x,y,'o',xx,yy)

xy = [randn(1,npts); randn(1,npts)];
Engineering Computation Curve Fitting: Interpolation 26 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)]) end set(gca,'XTick',[],'YTick',[]) cv = cscvn(xy); fnplt(cv,'r',2) fnplt(cscvn(xy),'r',2)

Download ppt "Engineering Computation Curve Fitting: Interpolation 1"

Similar presentations