Intro Polynomial Piecewise Cubic Spline Software Summary. Interpolation. Sanzheng Qiao. Department of Computing and Software McMaster University

Similar documents
Intro Polynomial Piecewise Cubic Spline Software Summary. Interpolation. Sanzheng Qiao. Department of Computing and Software McMaster University

Numerical Integration

Nonlinear Equations and Continuous Optimization

Solving Ordinary Differential Equations

Lecture 10 Polynomial interpolation

Elements of Floating-point Arithmetic

3.1 Interpolation and the Lagrange Polynomial

Cubic Splines MATH 375. J. Robert Buchanan. Fall Department of Mathematics. J. Robert Buchanan Cubic Splines

Elements of Floating-point Arithmetic

SPLINE INTERPOLATION

In practice one often meets a situation where the function of interest, f(x), is only represented by a discrete set of tabulated points,

Lecture Note 3: Interpolation and Polynomial Approximation. Xiaoqun Zhang Shanghai Jiao Tong University

Polynomial Interpolation

CHAPTER 4. Interpolation

Scientific Computing

1 Piecewise Cubic Interpolation

Lectures 9-10: Polynomial and piecewise polynomial interpolation

Polynomial Interpolation

x x2 2 + x3 3 x4 3. Use the divided-difference method to find a polynomial of least degree that fits the values shown: (b)

Curve Fitting and Interpolation

Scientific Computing: An Introductory Survey

Continuous Optimization

1 Review of Interpolation using Cubic Splines

University of Houston, Department of Mathematics Numerical Analysis, Fall 2005

We consider the problem of finding a polynomial that interpolates a given set of values:

Input: A set (x i -yy i ) data. Output: Function value at arbitrary point x. What for x = 1.2?

7. Piecewise Polynomial (Spline) Interpolation

Chapter 4: Interpolation and Approximation. October 28, 2005

MATH ASSIGNMENT 07 SOLUTIONS. 8.1 Following is census data showing the population of the US between 1900 and 2000:

Chapter 1 Numerical approximation of data : interpolation, least squares method

Applied Math 205. Full office hour schedule:

BSM510 Numerical Analysis

Function approximation

Data Analysis-I. Interpolation. Soon-Hyung Yook. December 4, Soon-Hyung Yook Data Analysis-I December 4, / 1

Some notes on Chapter 8: Polynomial and Piecewise-polynomial Interpolation

Outline. 1 Interpolation. 2 Polynomial Interpolation. 3 Piecewise Polynomial Interpolation

Additional exercises with Numerieke Analyse

INTERPOLATION Background Polynomial Approximation Problem:

Q1. Discuss, compare and contrast various curve fitting and interpolation methods

Math 365 Homework 5 Due April 6, 2018 Grady Wright

Interpolation. Chapter Interpolation. 7.2 Existence, Uniqueness and conditioning

Interpolation. 1. Judd, K. Numerical Methods in Economics, Cambridge: MIT Press. Chapter

Lecture Note 3: Polynomial Interpolation. Xiaoqun Zhang Shanghai Jiao Tong University

Consider the following example of a linear system:

Hani Mehrpouyan, California State University, Bakersfield. Signals and Systems

Numerical interpolation, extrapolation and fi tting of data

AIMS Exercise Set # 1

Interpolation Theory

Cubic Splines; Bézier Curves

MA3457/CS4033: Numerical Methods for Calculus and Differential Equations

Curve Fitting. 1 Interpolation. 2 Composite Fitting. 1.1 Fitting f(x) 1.2 Hermite interpolation. 2.1 Parabolic and Cubic Splines

CHAPTER Order points and then develop a divided difference table:

Introduction Linear system Nonlinear equation Interpolation

Maria Cameron Theoretical foundations. Let. be a partition of the interval [a, b].

CS 450 Numerical Analysis. Chapter 8: Numerical Integration and Differentiation

Exam 2. Average: 85.6 Median: 87.0 Maximum: Minimum: 55.0 Standard Deviation: Numerical Methods Fall 2011 Lecture 20

CS412: Introduction to Numerical Methods

Eigenvalue Problems and Singular Value Decomposition

1 Lecture 8: Interpolating polynomials.

Numerical Analysis: Interpolation Part 1

LECTURE NOTES ELEMENTARY NUMERICAL METHODS. Eusebius Doedel

CS 257: Numerical Methods

An Introduction to Numerical Analysis. James Brannick. The Pennsylvania State University

Cubic B-spline Collocation Method for Fourth Order Boundary Value Problems. 1 Introduction

you expect to encounter difficulties when trying to solve A x = b? 4. A composite quadrature rule has error associated with it in the following form

Lösning: Tenta Numerical Analysis för D, L. FMN011,

Interpolation (Shape Functions)

Physics 331 Introduction to Numerical Techniques in Physics

Interpolation and Polynomial Approximation I

CS 323: Numerical Analysis and Computing

Preliminary Examination in Numerical Analysis

Numerical Methods I: Polynomial Interpolation

CS 323: Numerical Analysis and Computing

MATRICES. a m,1 a m,n A =

Polynomials. p n (x) = a n x n + a n 1 x n 1 + a 1 x + a 0, where

Numerical Methods I: Interpolation (cont ed)

Computational Physics

Application of modified Leja sequences to polynomial interpolation

Algebraic Equations. 2.0 Introduction. Nonsingular versus Singular Sets of Equations. A set of linear algebraic equations looks like this:

There is a unique function s(x) that has the required properties. It turns out to also satisfy

Department of Applied Mathematics and Theoretical Physics. AMA 204 Numerical analysis. Exam Winter 2004

9.4 Cubic spline interpolation

Engineering 7: Introduction to computer programming for scientists and engineers

Interpolation and extrapolation

Interpolation. Escuela de Ingeniería Informática de Oviedo. (Dpto. de Matemáticas-UniOvi) Numerical Computation Interpolation 1 / 34

Chapter 2 Interpolation

Illustration of Gaussian elimination to find LU factorization. A = a 11 a 12 a 13 a 14 a 21 a 22 a 23 a 24 a 31 a 32 a 33 a 34 a 41 a 42 a 43 a 44

TABLE OF CONTENTS INTRODUCTION, APPROXIMATION & ERRORS 1. Chapter Introduction to numerical methods 1 Multiple-choice test 7 Problem set 9

Empirical Models Interpolation Polynomial Models

HOMEWORK PROBLEMS FROM STRANG S LINEAR ALGEBRA AND ITS APPLICATIONS (4TH EDITION)

Assignment 6, Math 575A

NUMERICAL MATHEMATICS AND COMPUTING

Review I: Interpolation

Cubic Splines. Antony Jameson. Department of Aeronautics and Astronautics, Stanford University, Stanford, California, 94305

Math 502 Fall 2005 Solutions to Homework 3

Introduction to Numerical Analysis

Least squares regression

446 CHAP. 8 NUMERICAL OPTIMIZATION. Newton's Search for a Minimum of f(x,y) Newton s Method

Quaternion Cubic Spline

APPM/MATH Problem Set 6 Solutions

Transcription:

Interpolation Sanzheng Qiao Department of Computing and Software McMaster University July, 2012

Outline 1 Introduction 2 Polynomial Interpolation 3 Piecewise Polynomial Interpolation 4 Natural Cubic Spline 5 Software Packages

Introduction Problem setting: Given (x 0, y 0 ),(x 1, y 1 ),...,(x n, y n ), x 0 < x 1 < x n, for example, a set of measurements, construct a function f : f(x i ) = y i, i = 0, 1,..., n Desirable properties of f : smooth: analytic and f (x) not too large (the first and second derivatives are continuous). simple: polynomial of minimum degree, easy to evaluate.

Example Measurements of the speed of sound in ocean water 5060 5040 5020 5000 speed (ft/sec) 4980 4960 4940 4920 4900 4880 4860 0 2000 4000 6000 8000 10000 12000 depth (ft)

Example Interpolation 5060 5040 5020 5000 speed (ft/sec) 4980 4960 4940 4920 4900 4880 4860 0 2000 4000 6000 8000 10000 12000 depth (ft)

Polynomial Interpolation Advantages: easy to evaluate and differentiate Weierstrass Approximation Theorem: If f is any continuous function on the finite closed interval [a,b], then for every ǫ > 0 there exists a polynomial p n (x) of degree n = n(ǫ) such that max f(x) p n(x) < ǫ. x [a,b] Impractical (degree is often too high)

A straightforward approach A polynomial of degree n is determined by its n + 1 coefficients. Given (x 0, y 0 ),...,(x n, y n ) to be interpolated, we construct the linear system (Vandermonde matrix): 1 x 0 x0 n a 0 y 0 1 x 1 x1 n a 1 y 1... 1 x n xn n. a n solve for the coefficients of the polynomial =. y n p n (y)(x) = a 0 + a 1 x + + a n x n

Vandermonde matrix When x 0,..., x n are distinct, the Vandermonde matrix is nonsingular. Thus the system has a unique solution (coefficients of the interpolating polynomial). Example. Given two points (28, 0.4695) and (30, 0.5000), we have the system [ ] [ ] [ ] 1 28 a0 0.4695 = 1 30 0.5000 and the solution [ ] a0 = 1 [ 30 28 2 1 1 a 1 a 1 ] [ 0.4695 0.5000 ] = [ 0.04250 0.01525 ].

Vandermonde matrix problem: The coefficient (Vandermonde) matrix is often ill-conditioned question What is the condition number of the Vandermonde matrix constructed by x i = 2000 + i, i = 0, 1,..., 7? Answer: 1.87 10 37

Lagrange form (conceptually simple) Basis of polynomials: {l j (x)} (j = 0, 1,..., n) of degree n such that { 1, if i = j l j (x i ) = 0, otherwise construct l j (x) = i j x x i x j x i Thus p n (y)(x) = n l j (x)y j j=0

Example Given three points: (28, 0.4695), (30, 0.5000), (32, 0.5299), construct a second degree interpolating polynomial in the Lagrange form: p 2 (x) = (x 30)(x 32) (28 30)(28 32) 0.4695 (x 28)(x 32) + (30 28)(30 32) 0.5000 (x 28)(x 30) + (32 28)(32 30) 0.5299 p 2 (31) = 0.5150 sin(31 ) Hard to evaluate.

Horner s rule Evaluating a 0 x 3 + a 1 x 2 + a 2 x + a 3 Horner s form: ((a 0 x + a 1 )x + a 2 )x + a 3 v = a(0); for (i = 1:n) v = v*x + a(i); end The optimal (most efficient and accurate) way of evaluating a 0 x n +... + a n (not in the Lagrange form).

Dangers of polynomial interpolation An example. Runge s function (continuous derivatives of all order) 1 y(x) = 1 + 25x 2 on [ 1, 1] equally spaces x 0 = 1, x 1,, x n = 1 1 Equal Spacing (n = 13) 0.5 0 0.5 1 1.5 2 2.5 3 3.5 4 1 0.8 0.6 0.4 0.2 0 0.2 0.4 0.6 0.8 1 It is often best not to use global polynomial interpolation.

Piecewise Polynomial Interpolation Given the partition α = x 1 < x 2 < < x n = β, interpolate on each [x i, x i+1 ] with a low degree polynomial. Linear L i (z) = a i + b i (z x i ), z [x i, x i+1 ] a i = y i, b i = y i+1 y i x i+1 x i, 1 i n 1

Algorithm. Piecewise linear interpolation Given vectors x and y with interpolating points, this function returns the piecewise linear interpolation coefficients in the vectors a and b. function [a,b] = pwl(x,y) n = length(x); a = y(1:n-1); b = diff(y)./diff(x);

Evaluation Given the piecewise linear interpolation L(z) represented by the coefficient vectors a, b, how do we evaluate this function at z [α,β]? First, we locate [x i, x i+1 ] such that z [x i, x i+1 ]. Then, we evaluate L(z) using L i (z). Search method: binary search, since x i are sorted. Observation: If [x i, x i+1 ] is associated with the current z, then it is likely that this subinterval will be the one for the next value.

Algorithm. Locate Idea: Use the previous subinterval as a guess. If not, do binary search. Given the vector x of breakpoints and a scalar z between x 1 and x n, this function locates i so that x i z x i+1. The optional g is a guess. function i = Locate(x,z,g) if nargin==3 % try the guess if (x(g)<=z)&(z<=x(g+1)) i = g; return % quick return end end

Algorithm. Locate (cont.) n = length(x); if z==x(n) i = n-1; % quick return else % binary search left = 1; right = n; while right > left+1 mid = floor((left + right)/2); if z < x(mid) right = mid; else left = mid; end end i = left; end

Algorithm. pwleval Given a piecewise linear interpolation coefficient vectors a and b from pwl and its breakpoints in x, this function returns the values of the interpolation evaluated at the points in z. function v = pwleval(a,b,x,z) m = length(z); v = zeros(m,1); g = 1; for j=1:m i = Locate(x,z(j),g); v(j) = a(i) + b(i)*(z(j) - x(i)); g = i; end

Example y = 1 (x 0.3) 2 + 0.01 + 1 (x 0.9) 2 + 0.04 6 100 Interpolation of humps(x) with PWL, n = 10 80 60 40 20 0 0 0.5 1 1.5 2 2.5 3

Problem setting Given (x 1, y 1 ),(x 2, y 2 ),...,(x n, y n ), find s(x): in each subinterval [x i, x i+1 ], s(x) is cubic s(x i ) = y i, i = 1,..., n s (x) and s (x) are continuous at x 2, x 3,..., x n 1 s (x 1 ) = s (x n ) = 0 The second derivative of s(x) is zero at the end points means that s(x) is linear at the end points.

A straightforward approach Suppose a i + b i x + c i x 2 + d i x 3 on [x i, x i+1 ], i = 1,..., n 1. 4(n 1) unknowns to be determined. Interpolation: a i + b i x i + c i xi 2 + d i xi 3 = y i, i = 1,..., n 1 a i + b i x i+1 + c i xi+1 2 + d ixi+1 3 = y i+1, i = 1,..., n 1 Continuous first derivative (consider [x i 1, x i ] and [x i, x i+1 ]): b i 1 + 2c i 1 x i + 3d i 1 xi 2 = b i + 2c i x i + 3d i xi 2, i = 2,..., n 1 Continuous second derivative: 2c i 1 + 6d i 1 x i = 2c i + 6d i x i, i = 2,..., n 1 Two end conditions: 2c 1 + 6d 1 x 1 = 0 and 2c n 1 + 6d n 1 x n = 0 Total of 4(n 1) equations, a dense system.

A clever approach: Constructing s(x) In the subinterval [x i, x i+1 ], let h i = x i+1 x i and introduce new variables: w = (x x i )/h i, w = 1 w. Note: w(x i ) = 0, w(x i+1 ) = 1 and w(x i ) = 1, w(x i+1 ) = 0, (linear Lagrange polynomials). Thus wy i+1 + wy i is the (linear) Lagrange interpolation on [x i, x i+1 ]. Construct s(x) = wy i+1 + wy i + h 2 i [(w 3 w)σ i+1 + ( w 3 w)σ i ] where σ i to be determined, so that the properties (the first and second derivatives are continuous) are satisfied.

Properties of s(x) Using w = 1/h i and w = 1/h i, we can verify 1 s(x i ) = y i, s(x i+1 ) = y i+1, independent of σ, that is, s(x) interpolates (x i, y i ). 2 s (x) = 6wσ i+1 + 6 wσ i, linear Lagrange interpolation at the points (x i, 6σ i ) and (x i+1, 6σ i+1 ). Clearly s (x i ) = 6σ i, which implies that s (x) is continuous. Is s (x) continuous?

Properties of s(x) (cont.) It remains to determine σ i so that s (x) is continuous. Consider, on [x i, x i+1 ], s (x) = y i+1 y i h i + h i [(3w 2 1)σ i+1 (3 w 2 1)σ i ] Let i = (y i+1 y i )/h i. On [x i, x i+1 ], w(x i ) = 0 and w(x i ) = 1, s +(x i ) = i + h i ( σ i+1 2σ i ).

Properties of s(x) (cont.) On [x i 1, x i ], s (x) = y i y i 1 h i 1 + h i 1 [(3w 2 1)σ i (3 w 2 1)σ i 1 ] and w(x i ) = 1, w(x i ) = 0. Thus s (x i) = i 1 + h i 1 (2σ i + σ i 1 ).

Making s (x) continuous Setting we get n 2 equations: s +(x i ) = s (x i ), i = 2, 3,..., n 1, h i 1 σ i 1 + 2(h i 1 + h i )σ i + h i σ i+1 = i i 1 for i = 2, 3,..., n 1. Solve for σ 2,...,σ n 1, recalling that σ 1 = σ n = 0 (natural cubic spline).

Matrix form diagonal: [2(h 1 + h 2 ),, 2(h n 2 + h n 1 )] supper/subdiagonal: [h 2,, h n 2 ] unknowns: [σ 2,,σ n 1 ] T right-hand side: [ 2 1,, n 1 n 2 ] T The matrix is symmetric tridiagonal diagonally dominant ( a i,i > j i a i,j ), when x 1 < x 2 < < x n Can apply Gaussian elimination without pivoting, working on (two) three vectors with O(n) operations.

Modeling a problem Note. Had we taken the straightforward approach to determining the coefficients of the piecewise cubic polynomials, four coefficients for each of n 1 cubic polynomials, we would have ended up with a large (4(n 1) 4(n 1)) and dense system requiring O(n 3 ) operations. Now we have an O(n) method.

Evaluating s(x) If s(x) is evaluated many times, arrange s(x) so that s(x) = y i + b i (x x i ) + c i (x x i ) 2 + d i (x x i ) 3 and rearrange it in the Horner s form, for x i x x i+1 and calculate and store b i, c i, d i (instead of σ i ) b i = y i+1 y i h i h i (σ i+1 + 2σ i ) for i = 1, 2,..., n 1 c i = 3σ i d i = σ i+1 σ i h i

Algorithm. Natural cubic spline ncspline Given a vector x with breakpoints and vector y with function values, this algorithm computes the coefficients b, c, d of natural spline interpolation. 1 Compute h i and i ; 2 Form the tridiagonal matrix (two arrays) and the right hand side; 3 Solve for σ i ; 4 Compute the coefficients b, c, and d.

Software packages IMSL csint, csdec, csher, csval MATLAB polyfit, spline, ppval NAG e01aef, e01baf, e01bef, e02bbf, e01bff Octave interp1

Summary Polynomial interpolation: General idea and methods, Lagrange interpolation Piecewise polynomial interpolation: Construction of piecewise polynomial (linear and cubic), evaluation of a piecewise function, ncspline, seval

References [1 ] George E. Forsyth and Michael A. Malcolm and Cleve B. Moler. Computer Methods for Mathematical Computations. Prentice-Hall, Inc., 1977. Ch 4.