piecewise hermite interpolation - university of...

50
Third Year Project Piecewise Hermite Interpolation Author: Leanne Henkelis Supervisor: Milan Mihajlovic Submitted: 04/05/2011 BSc. Computer Science and Mathematics School of Computer Science The University of Manchester

Upload: vuongkien

Post on 16-Jul-2018

238 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Third Year Project

Piecewise Hermite Interpolation

Author: Leanne HenkelisSupervisor: Milan Mihajlovic

Submitted: 04/05/2011

BSc. Computer Science and MathematicsSchool of Computer Science

The University of Manchester

Page 2: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Abstract

Polynomial interpolation is a special case of curve fitting, where a polynomialis constructed which fits a set of discrete data points exactly. An important use ofinterpolation is to approximate known functions. The accuracy of interpolation isdetermined by how close the polynomial interpolant is to the original function.

This report details a new system which implements both Lagrange and Hermiteinterpolation methods to approximate known real-valued functions. The purpose ofthe system is to evaluate the accuracy of these methods with respect to a numberof interpolation parameters.

Page 3: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Acknowledgements

I would firstly like to thank my supervisor Milan Mihajlovic for his continuousguidance and support throughout the project. Without his patience and knowledgethe project would not have been a success.

I would also like to show gratitude to Anthony Chiu for proofreading (most of)this report and my father for proofreading the rest.

Finally, I would like to show my appreciation to my friends and family for theirconstant encouragement throughout my final year of university.

Page 4: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Contents

1 Introduction 31.1 Polynomial Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.1.1 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.1.2 Interpolation Methods . . . . . . . . . . . . . . . . . . . . . . 4

1.2 The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.3 Report Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

2 Background 72.1 Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.2 Interpolation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2.2.1 Lagrange Interpolation . . . . . . . . . . . . . . . . . . . . . . 82.2.2 Hermite Interpolation . . . . . . . . . . . . . . . . . . . . . . . 102.2.3 Piecewise Interpolation . . . . . . . . . . . . . . . . . . . . . . 11

2.3 Interpolation Accuracy . . . . . . . . . . . . . . . . . . . . . . . . . . 122.3.1 Extension to 2D Domains . . . . . . . . . . . . . . . . . . . . 13

2.4 Existing Interpolation Systems . . . . . . . . . . . . . . . . . . . . . . 15

3 Design 183.1 System Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

3.1.1 Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . 183.1.2 Plot the Interpolant . . . . . . . . . . . . . . . . . . . . . . . 183.1.3 Calculate Interpolation Error . . . . . . . . . . . . . . . . . . 193.1.4 Save and Load Interpolation Data . . . . . . . . . . . . . . . . 193.1.5 Analyse Interpolation Accuracy . . . . . . . . . . . . . . . . . 19

3.2 Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193.3 Graphical User Interface (GUI) Design . . . . . . . . . . . . . . . . . 19

4 Implementation 244.1 Software Choice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

4.1.1 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244.1.2 Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244.1.3 Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254.1.4 Graphical User Interface . . . . . . . . . . . . . . . . . . . . . 25

4.2 Lagrange Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . 254.3 Hermite Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . 264.4 2D Lagrange Interpolation . . . . . . . . . . . . . . . . . . . . . . . . 274.5 GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

1

Page 5: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Contents Contents

4.5.1 Handling Input Data . . . . . . . . . . . . . . . . . . . . . . . 284.5.2 Some Interesting Aspects . . . . . . . . . . . . . . . . . . . . . 284.5.3 Users’ Error Handling . . . . . . . . . . . . . . . . . . . . . . 28

5 Results 305.1 Initial GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305.2 Simple Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

5.2.1 Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . 315.2.2 Error Calculations . . . . . . . . . . . . . . . . . . . . . . . . 31

5.3 Piecewise Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . 325.3.1 Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . 325.3.2 Error Calculations . . . . . . . . . . . . . . . . . . . . . . . . 335.3.3 Saving Results . . . . . . . . . . . . . . . . . . . . . . . . . . 36

5.4 Interpolation Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . 365.5 2D Lagrange Interpolation . . . . . . . . . . . . . . . . . . . . . . . . 38

5.5.1 Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

6 Testing and Evaluation 416.1 Testing Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416.2 Lagrange Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . 416.3 Hermite Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . 426.4 Piecewise Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . 426.5 Error Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426.6 2D Lagrange Interpolation . . . . . . . . . . . . . . . . . . . . . . . . 43

7 Conclusions 447.1 Achievements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447.2 Further Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

Bibliography 47

2

Page 6: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 1

Introduction

1.1 Polynomial Interpolation

Interpolation is a method of curve fitting. Curve fitting is the process of approxi-mating a given function (given either analytically or as a set of discrete points) byanother function that is close to it. For interpolation the resulting curve, knownas the interpolant, must agree exactly with the set of given data points, known asinterpolation points or interpolation nodes [15, p. 179]. In polynomial interpolationa polynomial is used to approximate any real function. An alternative curve fittingapproach aims to minimise the squares of the differences between the interpolationpoints and the curve. This is known as least square approximation [16]. In this casethe curve may not pass through the interpolation points.

In the simplest case interpolation is concerned with approximating a real functionof a single variable y = f(x) : R→ R. The data needed to construct an interpolantis of the form (x0,y0),. . . ,(xn,yn). The points yi may be the values of the functiondirectly evaluated at each xi, as yi = f(xi), or possibly data collected from somesampling or an experiment. In the latter case the analytical function f(x) may notbe known. A more complex problem may arise in cases where values of derivativesare also given at interpolation points. In Figure 1.1 the interpolation points (green)are given and the aim of interpolation is to construct the polynomial that passesexactly through these points (blue).

The problem of interpolation can be extended to higher spatial dimensions. In-terpolation of functions of two independent variables z = f(x, y) : R2 → R will alsobe considered in this project.

1.1.1 Applications

Interpolation is generally used in situations where it is necessary to find intermediatedata values for a discrete set of data. The interpolant can be used as an approximatefunction for the discrete data set which can then be evaluated at any point.

In digital image processing, interpolation is involved in bitmap resampling andcolour reconstruction [12, 11]. Bitmap resampling is used to increase the size and/orresolution of a bitmap image by adding new pixels. Colour reconstruction involvesreconstructing a full colour image from an incomplete image sample captured by

3

Page 7: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 1. Introduction 1.1. Polynomial Interpolation

Figure 1.1: Example of polynomial interpolation.

an imaging device. Many modern digital cameras require this technique to displayviewable images. Interpolation can also be applied to resample other types of digitalsignal.

In Mathematics, interpolants are used to approximate complicated functionswhich may be difficult or even impossible to evaluate. Another important area, inwhich the application of interpolation is critical, is numerical methods for solvingpartial differential equations. In particular, Galerkin’s method uses interpolation toapproximate the solution of a given differential equation. The interpolation in thiscase can be either global or local. For the global case the solution of the differen-tial equation over the entire domain is interpolated by polynomials of progressivelyincreasing degree. This is known as the spectral method. In the local case the do-main of interest is subdivided into a number of non-overlapping sub-intervals andthe solution of the differential equation is interpolated in each sub-domain indepen-dently. This latter case is present in finite element and finite volume methods [14].Numerical methods for the solution of differential equations can give the solutioneven in cases where analytical methods cannot. In practice analytical methods areapplicable only in very special cases.

1.1.2 Interpolation Methods

There are numerous methods for calculating interpolation polynomials and thechoice of method can depend on the given data and/or the purpose of the resulting

4

Page 8: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 1. Introduction 1.2. The Project

interpolant. One of the simplest methods, which requires only the knowledge ofthe function values (y0),. . . ,(yn) at a set of points (x0),. . . ,(xn), is known as La-grange interpolation. If the function values are given on a set of n + 1 distinctpoints, we can uniquely determine an interpolation polynomial of degree at mostn. Another approach using the same initial data is Newton’s divided differenceinterpolation [17]. Both of these methods produce the same interpolant, due tothe uniqueness of interpolation polynomials; the only difference is in the method ofcomputing the interpolant.

In the cases where the derivative is also provided at interpolation points theassociated method is called Hermite interpolation. The resulting polynomial hasdegree equal to the sum of all constraints imposed at the interpolation points. Thedivided difference method can also be adapted to include derivatives.

There are two variants of interpolation with respect to the domain. The firstinvolves constructing a global polynomial that satisfies the constraints at all in-terpolation points simultaneously. To increase the accuracy of interpolation morepoints can be introduced; this results in an increase in degree of the interpolant.The alternative is to sub-divide the domain of interest into a number of smallernon-overlapping sub-intervals and to construct a separate polynomial for each sub-interval. This approach achieves a good level of accuracy with relatively low orderinterpolants by increasing the number of sub-intervals, and is known as piecewiseinterpolation [15, p. 292]. Piecewise interpolation has the additional problem that itmust ensure continuity of the interpolants across the sub-interval boundaries. Thiscan be achieved by ensuring that interpolation points are placed on the sub-intervalboundaries.

Interpolation of functions of two independent variables has the added difficultyof the selection of suitable interpolation points. One approach is the construction ofinterpolation points on a simplex [8]. Any domain in R2 with a polygonal boundarycan be subdivided into a set of non-overlapping simplexes. By introducing a setof interpolation points on the simplex, a polynomial of two independent variablescan be constructed which interpolates f(x, y) on that simplex. This is an exampleof piecewise interpolation in two dimensions. Global interpolants could also beconstructed, although these will not be considered in this project.

1.2 The Project

The main aim of the project was to create a software system in MATLAB, withan accompanying GUI, that implements a number of interpolation methods andalso estimates the error associated with interpolation. More specifically, the systemimplements Lagrange and Hermite interpolation (both global and piecewise) of a realfunction of a single variable and also piecewise Lagrange interpolation for functionsof two independent variables. The system calculates the interpolant in analyticalform and plots its graph. For interpolation of functions of a single variable, thepointwise interpolation error is also calculated and plotted.

5

Page 9: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 1. Introduction 1.3. Report Overview

1.3 Report Overview

Chapter 2 introduces the essential mathematical background to the project andlooks at existing interpolation systems.

Chapter 3 describes the main requirements of the system and outlines the struc-ture of its main features.

Chapter 4 details the important implementation stages of interpolation.

Chapter 5 illustrates the functioning system and details how it is intended to beused.

Chapter 6 outlines how various parts of the system were tested.

Chapter 7 summarises the main achievements of the project and identifies possibleimprovements.

6

Page 10: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 2

Background

This chapter introduces the mathematical background for interpolation methodsand discusses some existing interpolation systems.

2.1 Curve Fitting

Curve fitting is the process of constructing a function that best fits a set of discretedata points. The function may fit the data points exactly, this is known as interpo-lation, or aim to find a best-fit approximation, by minimising the difference betweenthe function and data point values. Since interpolation requires that the functionpass through all of the given data points, the order of the interpolation polynomialis determined by the number of these constraints. An approximation method aimsto find the trend over the entire range of data and may not pass exactly throughany data points; the resulting polynomial order is independent of the initial data.In practice, it is usually much lower.

An example of curve fitting by approximation is the least squares method. Thismethod finds a best-fit curve by minimising the sum of the squares of the errors,denoted R, at each point in a set of data points [16]. R is defined by

R =∑

[yi − f(xi, (ai, . . . , an))]2,

where f is the resulting function with fit parameters ai. The square of each erroris used, rather than its absolute value, to allow the errors to be regarded as acontinuous, differentiable quantity, with greater errors being weighted more heavily.The aim of the method is to find the set of fit parameters that minimise the residualR. The procedure to achieve this involves taking the derivative of R with respect toeach of the coefficients ai and setting them to zero. The resulting set of equationscan then be solved using numerical methods.

The choice of interpolation or approximation as a method of curve fitting de-pends on the given data set and its application. The degree of an interpolant ispredetermined by the number of interpolation points and so for a large number ofpoints the polynomial will be of high degree. High order polynomials can behavevery badly and contain large oscillations. Approximation methods allow high di-mensional data to be used to create a polynomial of a relatively low degree. Such

7

Page 11: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 2. Background 2.2. Interpolation Methods

polynomials tend to be smoother. Also, in certain applications, it may be desirableto smooth the original data, rather than fit it exactly, and so the approximationapproach would be more appropriate. For example, if the data sample containedrandom erroneous information, using interpolation could create a distorted result.

This report is interested in the use of curve fitting to interpolate given functions.The initial data will be the values of a function, directly evaluated at a set of points,and therefore will contain no erroneous information. Interpolation is the best methodto use in this case as the given data is preserved in the resulting interpolant. Whilsthigh order polynomials may be subject to oscillations, the aim of the project is tofind ways to increase accuracy in such cases.

2.2 Interpolation Methods

This section follows the assumption that the interpolation values yi and derivativeszi are the result of a function f being directly evaluated at each point xi, that isyi = f(xi) and zi = f ′(xi).

Basic problem: Given a set of equally spaced points xi ∈ R, i = 0, . . . , n, andyi ∈ R, i = 0, . . . , n, find a polynomial pn ∈ Pn (where Pn is the set of all realpolynomials of degree ≤ n) such that

pn(xi) = yi, i = 0, . . . , n.

In other words, the polynomial pn must agree exactly with the values yi at interpo-lation points xi [15, p. 180].

2.2.1 Lagrange Interpolation

Definition: Let xi ∈ R, i = 0, . . . , n, be distinct points and yi ∈ R, i = 0, . . . , n.The unique polynomial pn defined by

pn(x) =n∑k=0

Lk(x)yk,

with basis polynomials Lk defined by

Lk(x) =n∏i=0i 6=k

(x− xi)(xk − xi)

,

k = 0, . . . , n, is called the Lagrange interpolation polynomial of degree n for the setof points (xi, yi), i = 0, . . . , n [15, p. 182].

It is clear that Lagrange interpolation solves the basic interpolation problemabove and simultaneously satisfies all constraints. It seems reasonable to assumethat increasing the number of interpolation points, and hence the degree of theinterpolant, increases the accuracy of interpolation, but this is not always the case.

8

Page 12: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 2. Background 2.2. Interpolation Methods

Consider the Lagrange interpolation polynomial pn, for a real-valued function f ,on the interval [a, b]. The interpolation error f(x)− pn(x) is given in the followingtheorem.

Theorem: [4] For x ∈ [a, b], there exists ξ ∈ (a, b) such that

f(x)− pn(x) =f (n+1)(ξ)

(n+ 1)!

n∏j=0

(x− xj).

Moreover,

maxx∈[a,b]

|f(x)− pn(x)| ≤(

maxξ∈[a,b]

|f (n+1)(ξ)|(n+ 1)!

)(maxx∈[a,b]

n∏j=0

|x− xj|

). (1)

The error term (1) has a number of interesting features. Firstly, consider thenodal polynomial

n∏j=0

(x− xj).

This polynomial is clearly affected by the choice of interpolation points. For equidis-tant interpolation points large oscillations can occur towards the endpoints of theinterval of interest [a, b] [6, p. 54]. Secondly, the derivative term f (n+1). If thederivative term is not bounded, a reasonable bound on the error is not guaranteedfor large values of n, and the polynomial again may oscillate above and below f .In these cases increasing the number of interpolation points leads to an increase inthe oscillation of the polynomial and therefore an increase in the maximum error.This oscillatory behaviour is known as Runge’s phenomenon [15, p. 186]. The phe-nomenon indicates that high order polynomials can be unsuitable for interpolation.

One way to alleviate this behaviour, and therefore reduce the maximum error,is to minimise

maxx∈[a,b]

∣∣∣∣∣n∏j=0

(x− xj)

∣∣∣∣∣.This can be achieved by choosing interpolation nodes which are distributed moredensely towards the edges of the domain of interest [6, p. 55]. Note that this approachdoes not guarantee the convergence of the polynomial pn to f , as the behaviour ofthe higher derivatives is still a factor, but it can increase accuracy for a range offunctions.

In cases where the polynomial interpolant pn does converge to the function f ,a high order polynomial is still usually required to ensure an approximation ofsufficient accuracy, especially if the interpolation interval is long. An alternative ap-proach is to subdivide the domain into a number of pairwise disjoint sub-intervals,and find an interpolation polynomial for each sub-interval. This approach is dis-cussed in Subsection 2.2.3.

9

Page 13: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 2. Background 2.2. Interpolation Methods

2.2.2 Hermite Interpolation

The Lagrange interpolation method constructs the lowest degree interpolating poly-nomial that satisfies interpolation conditions. This interpolation can be extendedto include additional constraints.

Theorem: [15, p. 188] Suppose the interpolation points xi ∈ R, i = 0, . . . , n,are distinct. Then, given yi, zi ∈ R, i = 0, . . . , n, there exists a unique polynomialp2n+1 ∈ P2n+1 such that

p2n+1(xi) = yi, p′2n+1(xi) = zi, i = 0, . . . , n.

Definition: Let xi ∈ R, i = 0, . . . , n, be distinct points and yi, zi ∈ R, i = 0, . . . , n.Let the polynomial p2n+1 be defined by

p2n+1(x) =n∑k=0

[Hk(x)yk +Kk(x)zk],

with basis polynomials Hk and Kk defined by

Hk(x) = [Lk(x)]2(1− 2L′k(xk)(x− xk)),Kk(x) = [Lk(x)]2(x− xk),

where

Lk(x) =n∏i=0i 6=k

(x− xi)(xk − xi)

,

k = 0, . . . , n. The polynomial p2n+1 is called the Hermite interpolation polynomialof degree 2n+ 1 for the set of points (xi, yi, zi), i = 0, . . . , n.

Hermite interpolation satisfies the additional condition that the interpolant poly-nomial must agree with the first derivative of a given function f at interpolationpoints. This is known as simple Hermite interpolation. In a more complex case,higher derivatives (not necessarily of the same order at each node) can be matchedbetween the function f and the interpolation polynomial.

Typically, in cases where Lagrange interpolation incurs large errors, the deriva-tives of the interpolant and original function f differ significantly [5]. Forcing theinterpolant to fit both the function f and the derivative f ′ exactly at interpolationpoints can increase both smoothness and accuracy of the interpolant.

The error f(x) − p2n+1(x) associated with Hermite interpolation is related tothe Lagrange error in that it is also dependent on the position of the interpolationpoints and the behaviour of the derivatives. Consider the Hermite interpolationpolynomial p2n+1, of a real-valued function f , on the interval [a, b].

Theorem: [15, p. 190] For x ∈ [a, b], there exists ξ ∈ (a, b) such that

f(x)− p2n+1(x) =f (2n+2)(ξ)

(2n+ 2)!

n∏j=0

(x− xj)2.

10

Page 14: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 2. Background 2.2. Interpolation Methods

Moreover,

maxx∈[a,b]

|f(x)− p2n+1(x)| ≤(

maxξ∈[a,b]

|f (2n+2)(ξ)|(2n+ 2)!

)(maxx∈[a,b]

n∏j=0

|x− xj|2). (2)

It is clear from the error term (2) that Hermite interpolation is also susceptibleto Runge’s phenomenon. The reduction of this behaviour can be approached usingthe same methods as with Lagrange interpolation.

Hermite interpolation can be generalised to include higher derivative values.Incorporating this derivative information, however, is not a simple task, as newbasis functions need to be determined. Let f be the function to be interpolatedand q be the order of the highest derivative given. The general Hermite case can beconstructed as follows [18]

p(x) =n∑i=0

αi(x)fi +n∑i=0

βi(x)f(1)i + · · ·+

n∑i=0

θi(x)f(q)i ,

with constraints

p(xj) = fj j = 0, . . . , n,

p(1)(xj) = f(1)j j = 0, . . . , n,

...

p(q)(xj) = f(q)j j = 0, . . . , n.

2.2.3 Piecewise Interpolation

As discussed in the previous sections, high order polynomial interpolants may berequired in order to produce a good level of accuracy for interpolation over a globaldomain. These interpolants can be susceptible to large errors. One alternativeapproach is to partition the domain of interest into a number of smaller sub-intervals,or elements, and then apply a method of interpolation to each element independently.The function f can then be interpolated on each of these elements to a sufficientaccuracy, with polynomials of relatively low degree. This approach is known aspiecewise interpolation.

Piecewise interpolation has the added difficulty of ensuring continuity of theinterpolants across the element boundaries. A simple way to guarantee this is toplace the end interpolation points, for each element, on the element boundaries.

There are a number of ways to increase the accuracy of piecewise interpolation:

• Increase the order of interpolants (p-refinement),

• Increase the number of elements (h-refinement),

• Alter the distribution of elements.

11

Page 15: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 2. Background 2.3. Interpolation Accuracy

Increasing the number of interpolation points on each element, and thereforeincreasing the order of each interpolant, can reduce the interpolation errors. Dif-ferent orders of interpolation polynomials may be used on each element to reflectthe changing shape of the function. However, as with global interpolation methods,high order polynomials should be avoided.

A different technique is to increase the number of elements and therefore decreasethe size of each element. Smaller elements will require lower order polynomials toachieve the required order of accuracy.

The simplest way of partitioning the global interval is to use elements of equalsize. An alternative is to vary the size of the elements. As with interpolation overa global domain, clustering interpolation points towards the edges of the domaincan increase accuracy. More generally, clustering of elements is desirable in partsof the domain where the function f exhibits rapid changes. In the piecewise case,the elements can be distributed more densely towards the edges of the domain.The following functions calculate the distribution of E elements, over the domain[a, b], using a variable stretch parameter α, with the aim of clustering more elementstowards the domain endpoints [3, p. 1023].

X (j) =

(E(j)∑Ee=1 E(e)

)W, j = 1, . . . , E,

where

E(j) =1

h|E + 1− 2j|α + 2, h =

W

E, j = 1, . . . , E.

E(j) controls the element width distribution using the average element size h andthe width of each element X (j), where W is the width of the domain [a, b]. Astretch parameter of zero gives uniform distribution of elements. As the parameteris increased the elements become more closely distributed towards the edges of thedomain.

The piecewise approaches detailed above can be used simultaneously, and theparameters for each can be varied accordingly to establish accurate interpolation.An interpolation system that allows these parameters to be managed by the userwould be useful for accurate interpolation and interpolation analysis.

2.3 Interpolation Accuracy

The simplest way of testing the accuracy of interpolation is to calculate the differencebetween the interpolant p(x) and the original function f(x) at a number of samplepoints, and then find the maximum

maxxk∈[a,b]

|f(xk)− p(xk)|.

As the number of sample points is increased, the maximum error tends to a con-stant. For piecewise interpolation this involves comparing each polynomial with itscorresponding element and taking the maximum over the entire domain.

12

Page 16: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 2. Background 2.3. Interpolation Accuracy

If the function f is unknown the approach above cannot be taken. Instead anadaptive method can be used which compares the interpolation polynomials of anincreasing number of points

maxxk∈[a,b]

|pn+1(xk)− pn(xk)|.

2.3.1 Extension to 2D Domains

The first step in the interpolation of functions of two independent variables is choos-ing suitable interpolation points on the R2 plane. The points cannot all be co-linearas this would result in a polynomial of only one variable. It is not possible to uniquelydetermine a polynomial of two independent variable in this case. Selecting the in-terpolation points at the vertices of a two dimensional simplex, a triangle, ensuresthat the points are not all co-linear, and a polynomial of two independent variablescan be constructed. This also ensures inter-element continuity of the interpolantsconstructed over neighbouring simplexes.

Consider a triangle T with vertices v1 = (x1, y1), v2 = (x2, y2) and v3 = (x3, y3).Let the vertices be the interpolation nodes. An arbitrary point q = (x, y) within thetriangle can be represented as a weighted sum of the vertices

x = L1x1 + L2x2 + L3x3

y = L1y1 + L2y2 + L3y3 (3)

1 = L1 + L2 + L3

where L1, L2, L3 are area co-ordinates subject to a normalisation relation [1]. The co-ordinates L1, L2, L3 are called area co-ordinates as they correspond to the normalisedareas of the sub-triangles constructed about the point q

L1 =Area(qv2v3)

Area(v1v2v3)

L2 =Area(v1qv3)

Area(v1v2v3)

L3 =Area(v1v2q)

Area(v1v2v3).

The linear system defined by (3) can be solved, for example using Cramer’s rule, tofind formulae for the area co-ordinates. Note that the area co-ordinates are linearpolynomials of two independent variables. The resulting formulae are given by

L1 = (a1 + b1x+ c1y)/24,L2 = (a2 + b2x+ c2y)/24, (4)

L3 = (a3 + b3x+ c3y)/24,

where

4 = Area(v1v2v3) =1

2

∣∣∣∣∣∣1 x1 y1

1 x2 x2

1 x3 y3

∣∣∣∣∣∣ .13

Page 17: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 2. Background 2.3. Interpolation Accuracy

The coefficients a1, a2, a3 are defined by

a1 = x2y3 − x3y2, b1 = y2 − y3, c1 = x3 − x2,a2 = x3y1 − x1y3, b2 = y3 − y1, c2 = x1 − x3,a3 = x1y2 − x2y1, b3 = y1 − y2, c3 = x2 − x1.

The final stage is to use these area co-ordinates to identify the nodal basis func-tions Ni required for interpolation. In the case of linear interpolation the nodal basisfunctions are simply the area co-ordinates

N1 = L1,

N2 = L2, (5)

N3 = L3.

Definition: Given a function f(x, y) of two independent variables and a triangle Twith vertices (x1, y1), (x2, y2), (x3, y3), the polynomial p1(x, y) defined by

p1(x, y) = f(x1, y1)N1(x, y) + f(x2, y2)N2(x, y) + f(x3, y3)N3(x, y),

where the basis polynomials Ni are defined by (4) and (5), is the linear Lagrangepolynomial defined on triangle T .

As with interpolation of functions of one variable, the interpolant will agreeexactly with the function at interpolation points, which in this case are the vertices ofthe triangle domain. For quadratic interpolation there are six nodal basis functionswhich can be expressed by the area co-ordinates as follows:

N1 = (2L1 − 1)L1,

N2 = (2L2 − 1)L2,

N3 = (2L3 − 1)L3, (6)

N4 = 4L1L2,

N5 = 4L2L3,

N6 = 4L3L1.

Definition: Given a function f(x, y) of two independent variables and a triangle Twith vertices (x1, y1), (x2, y2), (x3, y3), the polynomial p2(x, y) defined by

p2(x, y) = f(x1, y1)N1(x, y) + f(x2, y2)N2(x, y) + f(x3, y3)N3(x, y)

+ f

(x1 + x2

2,y1 + y2

2

)N4(x, y) + f

(x2 + x3

2,y2 + y3

2

)N5(x, y)

+ f

(x1 + x3

2,y1 + y3

2

)N6(x, y),

where basis polynomials Ni are defined by (4) and (6), is the quadratic Lagrangepolynomials defined on triangle T .

14

Page 18: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 2. Background 2.4. Existing Interpolation Systems

2.4 Existing Interpolation Systems

There are many systems available that calculate interpolation polynomials, using avariety of methods. However, most do not combine multiple interpolation methodsinto a single system with a front-end user interface, and none incorporate the desiredaccuracy estimates for the computed interpolant. The systems that are available areonly very basic and are limited to interpolation of functions of one variable.

The systems most relevant to this project are An Interpolation Applet byRon Miech of University of California [13] and Interpolation Applet by Tom Kiffeof Texas A&M University [10].

The first applet takes x and y interpolation values as a text input and constructsa polynomial using Lagrange interpolation. The outputs are the interpolant graphand also the polynomial representation p(x) of the interpolant. Interpolation valuescan also be input by selecting points on the axes using the mouse. See Figure 2.1for a screenshot of the applet.

Figure 2.1: An Interpolation Applet by Ron Miech.

The second applet uses a variety of different methods including the divided dif-ference method and a natural cubic spline method. The user is required to choose amethod and to input interpolation data as text. The calculated result can be viewedas either a plot or as a list of coefficients. See Figure 2.2 for a screenshot of theapplet.

Another interesting system is the Interpolate iPhone application developedby David J Hinson [9]. This application allows the user to input a number ofinterpolation points with their associated values and then calculates the resultinginterpolant. See Figure 2.3 for a screenshot of the applet.

15

Page 19: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 2. Background 2.4. Existing Interpolation Systems

Figure 2.2: Interpolation Applet by Tom Kiffe.

Figure 2.3: Interpolate by David J Hinson.

16

Page 20: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 2. Background 2.4. Existing Interpolation Systems

The lack of sophisticated interpolation systems available demonstrated the needfor a new system that can implement a variety of interpolation methods, plot theresults and also be used to evaluate the accuracy of interpolation using a variety ofdifferent parameters.

17

Page 21: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 3

Design

This section introduces the requirements of the proposed system and the structureof its main features.

3.1 System Requirements

As detailed in Chapter 2 a system was required that can provide accurate inter-polation by using a selection of interpolation methods and a number of variableparameters. The following requirements were identified for the proposed system.

3.1.1 Interpolation

The first requirement was to implement Lagrange and simple Hermite interpolation,both over the global domain and using piecewise methods, for approximation of afunction of a single variable. The system was designed to take interpolation pointsxi and a function to be interpolated f as input, and then calculate the values yi byevaluating the function at interpolation points.

The piecewise approach was designed to also include the variable parameters:

• order of interpolants,

• number of elements,

• distribution of elements.

It was also intended that general Hermite interpolation would feature in the sys-tem, however, this was not implemented as the explicit formulae were not availablein this case.

The other main focus of the system is Lagrange interpolation for approximatingfunctions of two independent variables on a simplex domain, taking the vertices ofa simplex, a function f and the required order as input.

3.1.2 Plot the Interpolant

A graph of the interpolant plotted against the original function allows for a graphicalview of the accuracy of interpolation. In the piecewise case this involves plottingmultiple interpolant polynomials, one for each element.

18

Page 22: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 3. Design 3.2. Structure

A plot of the interpolation error, calculated at a specified number of samplepoints, was also a requirement for the interpolation of functions of a single variable.

3.1.3 Calculate Interpolation Error

Calculating the maximum error of interpolation is the other major design featureof the system. This was only implemented for interpolation of functions of a singlevariable. The error can be used to analyse the accuracy of the interpolation methodswith respect to chosen interpolation points and parameters. The adaptive methodfor calculating the error, where the function is unknown, is left as a possible futuresystem enhancement.

3.1.4 Save and Load Interpolation Data

For convenience the system was designed to be able to load required interpolationdata from a text file (see Table 3.1 for possible choices of input data) and alsoprovide the option to save the results of interpolation to be loaded later for analysis.The resulting text file should contain the input interpolation data, interpolant resultand error estimate.

3.1.5 Analyse Interpolation Accuracy

To assist with the accuracy analysis of interpolation, the system should provide atable and a set of axes to display multiple interpolation results for a single func-tion. This is a useful feature for highlighting any trends in accuracy across varyingparameters.

3.2 Structure

The structure of the system is divided into four sections. Table 3.1 specifies themain features of each of the sections along with the required inputs and outputs.

3.3 Graphical User Interface (GUI) Design

The GUI was designed with the aim of efficient communication between the userand the system. It should facilitate simple input/updating of interpolation data andparameters using drop down menus and text input fields. Input data should also beloadable from a file. Having a GUI allows some extra features to be facilitated forpiecewise interpolation:

Show IntervalsTo add a vertical line at each element boundary.

ZoomTo allow the user to zoom in/out of the plot.

19

Page 23: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 3. Design 3.3. Graphical User Interface (GUI) Design

Function Features Inputs OutputsSimpleInterpolation

Interpolation Interpolation points:xi,function: f(x),method: Lagrange/Hermite.

Interpolantpolynomial,polynomial order,polynomial plot.

ErrorCalculation

No. of sample points:10/100/1000.

Maximum error,error position: xj,error plot.

PiecewiseInterpolation

Interpolation Interpolation bounds:x0,xn,function: f(x),no. of elements: 1-10,distribution ofelements parameter:0-5,method: Lagrange/Hermite,interpolant order.

Interpolantpolynomial,polynomial plot.

ErrorCalculation

No. of sample points:10/100/1000.

Maximum error,error position: xj,error plot.

Save File name. Text file of results.InterpolationAnalysis

Load File name. Table of results of upto 5 interpolants.

Plot Select interpolantresult to plot.

Interpolant plot/errorplot.

2D LagrangeInterpolation

Interpolation Corner co-ordinatesof simplex domain,function: f(x),interpolant order:linear/quadratic.

Interpolantpolynomial,polynomial plot.

Table 3.1: Structure of proposed system.

20

Page 24: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 3. Design 3.3. Graphical User Interface (GUI) Design

Find ErrorTo allow the user to select any point on the interpolant graph and calculatethe interpolation error.

ClearTo clear the axes and all input fields.

A separate GUI window was designed for each of the four sections described inTable 3.1, with an extra window connecting the four sections. See Figures 3.1 - 3.4for the GUI design in each particular case.

Figure 3.1: Simple Interpolation - GUI Design.

21

Page 25: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 3. Design 3.3. Graphical User Interface (GUI) Design

Figure 3.2: Piecewise Interpolation - GUI Design.

Figure 3.3: Interpolation Analysis - GUI Design.

22

Page 26: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 3. Design 3.3. Graphical User Interface (GUI) Design

Figure 3.4: 2D Lagrange Interpolation - GUI Design.

23

Page 27: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 4

Implementation

This chapter details the implementation of the features designed in the previouschapter. The main focus is interpolation with brief details of the GUI.

4.1 Software Choice

To select a suitable software platform for implementation it was important to identifythe functionality required and decide which software would best handle it. MATLABwas chosen to implement the system; see subsequent subsections for justification.The aim of the project was to use as few built-in functions as possible, with respectto the mathematical calculations, and build this code from scratch. This would fitthe learning objective of the project. The following requirements are a result of theproposed design features of the system.

4.1.1 Functions

The system was designed to take a function to be interpolated, passed as a string,and parse it as a mathematical function. This function is then needed to createthe function plot, calculate the interpolation error and provide interpolation pointvalues, yi, and also derivative values if required. MATLAB is able to construct afunction, both of one variable and two variables, from a mathematical expressioncontained in a string. It also facilitates the evaluation and differentiation thesefunctions.

4.1.2 Polynomials

Polynomials make up the foundation of both Lagrange and Hermite interpolationmethods. Polynomials of one variable are stored in MATLAB as vectors of coef-ficients and can be easily evaluated. Lagrange interpolation involves finding theproduct and sum of a number of polynomials. MATLAB is able to accommodatethis using built-in functions, however, the products and sums can also be calculatedusing matrices and matrix operations which is the preferred method. Hermite inter-polation additionally uses polynomial differentiation which is simple to implement inMATLAB without using built in functions. Whilst it is more difficult, polynomials

24

Page 28: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 4. Implementation 4.2. Lagrange Interpolation

of two variables can also be represented and manipulated in MATLAB, as requiredby piecewise Lagrange interpolation for approximating functions of two variables.

4.1.3 Plots

MATLAB supports the two-dimensional line plots and three-dimensional surfaceplots required to draw the given functions and resulting interpolants. It also fa-cilitates plotting multiple graphs on one set of axes and plot interaction. Plotinteraction is needed for the zoom and clear capabilities and also to allow the userto select points on the graph for error calculations.

4.1.4 Graphical User Interface

MATLAB provides the GUI development environment GUIDE which assists withthe layout and programming of MATLAB GUIs. MATLAB also allows GUIs to becreated programmatically, however, GUIDE was more convenient for the proposedsystem.

4.2 Lagrange Interpolation

The Lagrange interpolation polynomial, for the set of interpolation data (xi,yi), isa linear combination of n Lagrange basis polynomials. Let X = (x1, . . . , xn) bethe discrete set of interpolation points given. Each basis polynomial Lk(x) is theproduct of a sequence of linear polynomials divided by a constant, and is defined by

Lk(x) =n∏i=0i 6=k

(x− xi)(xk − xi)

.

Firstly the product of linear polynomials for each basis polynomial needed to becalculated. The coefficients ai of the resulting polynomial were determined usingcombinations of the constants xi as follows

n∏i=0i 6=k

(x− xi) = xn + (−1)n−1(x1 + x2 + · · ·+ xn)xn−1

+ (−1)n−2(x1x2 + x1x3 + · · ·+ xn−1xn)xn−2

+ · · ·+ x1x2 . . . xn

= xn + an−1xn−1 + · · ·+ a1x+ a0.

This was implemented in MATLAB using the nchoosek function. The nchoosek

function takes a row vector of length n and creates a matrix with rows consistingof all possible combinations of the n elements of length k. With the exception ofthe first coefficient, which is always one, a matrix was created for each coefficient.To find the required combination of xi values for each coefficient aj, the elements ineach row of the matrix provided the indices i. The xi values were then multipliedtogether and the resulting terms were summed.

25

Page 29: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 4. Implementation 4.3. Hermite Interpolation

Next the power of each coefficient was established, alternating between positiveand negative. This was implemented simply using powers of minus one. Calculatingthe scalar part of the basis polynomials was straightforward and required a simpleproduct implemented within a loop.

Lastly, for each basis polynomial, the i 6= k constraint needed to be included.This was implemented by creating a new vector of interpolation points which omittedthe kth value. This new vector was then used in place of the original vector X toprovide the xi values for each basis polynomial.

The linear combination of all basis polynomials was achieved by summing eachbasis polynomial multiplied by its corresponding yk value

pn(x) =n∑k=0

Lk(x)yk.

See Listing 4.1 for the complete Lagrange interpolation function. The function isreused for piecewise interpolation, where it is called for each interval. The basiscalculation code is also used within Hermite interpolation (see next section).

4.3 Hermite Interpolation

The Hermite interpolation polynomial, for the set of interpolation data (xi,yi,zi),is a linear combination of two basis polynomials, Hk(x) and Kk(x). Both basispolynomials are comprised of a combination of Lagrange basis polynomials, Lk(x),and are defined by

Hk(x) = [Lk(x)]2(1− 2L′k(xk)(x− xk)),Kk(x) = [Lk(x)]2(x− xk).

Firstly the calculation of [Lk(x)]2 was implemented. It is important to notice that[Lk(x)]2 can be expressed as the product of the square of each linear polynomial ofLk(x) as shown below

[Lk(x)]2 =(x− x1)

2...(x− xk−1)2(x− xk+1)

2...(x− xn)2

(xk − x1)2...(xk − xk−1)2(xk − xk+1)2...(xk − xn)2

Applying the Lagrange basis function to the vector (x1, x1, x2, x2, ..., xn, xn), ratherthan (x1, x2, ..., xn), achieved this in MATLAB.

To calculate the derivative of the Lagrange basis polynomial, using vector rep-resentation, it was sufficient to multiply each coefficient by its corresponding powerand increase its position in the vector by one.

The product in basis polynomial Kk(x) was implemented by creating two newvectors and subtracting one from the other. For [Lk(x)]2x each coefficient of thecorresponding vector moved one position to the right and the constant coefficientset to zero. The second new vector was simply Lk(x) multiplied by xk. The productsin Hk(x) were calculated in a similar way.

26

Page 30: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 4. Implementation 4.4. 2D Lagrange Interpolation

The linear combination of basis polynomials resulted in the Hermite interpolationpolynomial given below

p2n+1(x) =n∑k=0

Hk(x)yk +Kk(x)xk.

The Hermite code is reused in piecewise Hermite interpolation.

4.4 2D Lagrange Interpolation

Polynomials of one variable are stored in MATLAB as vectors, which can be eval-uated and manipulated quite easily. However, creating and managing polynomialsof two independent variables was not as straightforward. It was decided that thesepolynomials would also be stored as vectors. The elements of the vectors were rep-resentative of coefficients of x, y or constants terms. The linear area co-ordinatesLi defined by

Li = (ai + bix+ ciy)/24(see Chapter 2 for complete formulae) were stored in vectors, applying the MATLABconvention to have the highest power coefficients at the beginning, i.e [x y constant].

With the coefficients of the area co-ordinates calculated and stored in vectors,the next step was to determine the nodal basis functions. In the linear case this wasstraightforward as they are equal to the area co-ordinates. Quadratic interpolationrequires the product of two area co-ordinates. The polynomial result of the productof two area co-rdinates has the form

V (x, y) = c0x2 + c1y

2 + c2xy + c3x+ c4y + c5

where ci are the coefficients to be determined. These coefficients are combinationsof the coefficients of the area co-ordinates. To achieve this product the combinationsrequired for each coefficient were identified and and the result was stored in a vectorof length six.

Finally the linear combination of basis functions was calculated. To give thisresult meaning, as the vector form is not recognised as a polynomial in MATLAB,the vector was converted into a function. The only way to do this was to firstrepresent the formula as a string, to include the x, y arguments and operators, andthen convert this into a function.

To plot the resulting interpolant against the original function the meshgrid func-tion was used. This creates a matrix of values on which the surface polynomial canbe plotted. The requirement, however, was to plot the interpolant on a simplex,as this is where it is defined. To achieve this the matrix was reduced to its lowertriangular form and the surface plot was limited to the area of the given triangle.

4.5 GUI

The user interface was implemented with the help of the GUI development environ-ment GUIDE [7]. GUIDE was used to manage the layout of the GUI components

27

Page 31: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 4. Implementation 4.5. GUI

and the overall appearance. The GUI’s behaviour was implemented using callbackfunctions that made use of the interpolation and error calculation functions.

4.5.1 Handling Input Data

One difficulty was deciding how to handle the input data so that it could be sharedbetween callback functions and also between GUIs. It was decided to store therequired data in the main (parent) GUI and then store a handle to the main GUIin the root. Data stored in the main GUI could then be set or retrieved from anyfunction or GUI using the root handle.

4.5.2 Some Interesting Aspects

An interesting aspect of the PWInterpolator GUI is that the axes are interactive.The interactive features include the ability to zoom, toggle the sub-interval bound-aries and calculate a single error by choosing a point on the axes. A difficulty withthe zoom function was re-setting the position of the plot on zooming out. Thiswas achieved by re-enabling the pan function when the zoom was toggled off. Thesub-interval boundaries were drawn using dashed vertical lines. MATLAB updatesits axes by adding any new plots on top of any current plots. The removal of theboundary lines, when toggled off, was implemented by deleting the appropriate plotchildren from the axes.

The implementation of a single error calculation required a number of stages.Firstly the x position of the error, selected by the user using the mouse, had to beretrieved from the axes. The polynomial for the element containing this x positionthen had to be identified and the error at this point calculated. Lastly a red line wasdrawn between the function and the interpolant polynomial to represent the erroron the axes.

4.5.3 Users’ Error Handling

As well as the main functionality the GUIs also have a number of restrictions toprevent erroneous use. For example, the maximum error Calculate button is notenabled until some data has been interpolated. Also, if some error information isstill visible when another interpolation is initiated, the error information will bedeleted. Similarly the Save button is only available when there is data to save. Onsaving, the GUI uses the data and parameters used at interpolation rather than thatvisible in the input fields. This ensures that the correct information is saved andnot information that may have been added after interpolation.

28

Page 32: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 4. Implementation 4.5. GUI

function [V] =LagrangeFunc (X, func )

% Create a function from given stringf=i n l i n e ( func ) ;f=v e c t o r i z e ( f ) ;

% Evaluate function at interpolation pointsY=f (X) ;

n=length (X) ;V=0;

% Calc all basis polys and * Yfor l =1:n

% Create new vectorXX=[X( 1 : l −1) ,X( l +1:n ) ] ;nn=length (XX) ;c o e f f=ones (1 , n ) ; % of one basis polynomialsc =1; % scalar

% Calc each basis polyfor k=1:nn % for each coefficient

sum=0;comb=nchoosek ( 1 : nn , k ) ; % create matrix of indicesm=s ize (comb ) ;

for i =1:m(1) % loop through rowsprod=1;for j =1:m(2) % loop through cols

prod=prod∗XX(comb( i , j ) ) ;endsum=sum+prod ;

endsum=sum∗(−1)ˆk ;c o e f f ( k+1)=sum ;s c=sc ∗ ( (X( l )−XX(k ) ) ) ;

endV=V+( c o e f f / sc ∗Y( l ) ) ;

end % all basis funcs

end

Listing 4.1: Function for Lagrange interpolation

29

Page 33: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 5

Results

This chapter illustrates how the implemented system Interpolator works in prac-tice, and how it is intended to be used.

5.1 Initial GUI

The first GUI that appears when the system is run is the Welcome GUI, shown inFigure 5.1. This gives the user four options:

• Simple Interpolation,

• Piecewise Interpolation,

• Interpolation Analysis,

• 2D Interpolation.

each leading to a different GUI. The welcome GUI can be returned to from any ofthese GUIs at any time.

Figure 5.1: Welcome GUI figure.

5.2 Simple Interpolation

Selecting Simple Interpolation from the Welcome GUI opens theSimpleInterpolator GUI. This GUI provides interpolation of functions of one vari-able on a global domain. It also calculates the maximum error of interpolation and

30

Page 34: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 5. Results 5.2. Simple Interpolation

plots the interpolant against the original function. There are several functionalitiesof the system operated from this GUI. These are summarised below.

5.2.1 Interpolation

The first step is to input the interpolation points and a string representation of thefunction to be interpolated. This can be done in two ways. One way is to loadthe data from a text file, where interpolation points are read first, followed by thefunction which must be located on the proceeding line. Alternatively the valuescan be input manually. Figure 5.2 shows the SimpleInterpolator GUI with anexample of valid input values.

Figure 5.2: SimpleInterpolator GUI with input.

The next step is to choose an interpolation method from:

• Lagrange,

• Hermite

and then click the Interpolate button to initiate interpolation. Figure 5.3 showsan example of interpolation using the Hermite interpolation method. The Result isgiven in vector format, with coefficients ordered by descending powers. Notice thatthe View Symbolic button and error calculation panel are now enabled. The View

Symbolic button gives a symbolic representation of the interpolant.

5.2.2 Error Calculations

The interpolation error can be calculated, at a number of sample points, by selectingthe sample number from the drop-down menu and clicking the Calculate button.

31

Page 35: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 5. Results 5.3. Piecewise Interpolation

Figure 5.3: SimpleInterpolator GUI after interpolation.

The maximum error and its position are given as results. The Plot Error buttonplots the interpolation error over the entire domain. Figure 5.4 shows the results ofthe error calculation, using one thousand sample points, along with the error plot.

Changing the interpolation method at this stage will remove all current resultsready for the next interpolation. The Clear button has the same effect.

5.3 Piecewise Interpolation

Selecting Piecewise Interpolation from the Welcome drop-down menu opensthe PWInterpolator GUI. This GUI facilitates interpolation with respect to theglobal domain and also sub-intervals of the domain where piecewise methods areused. It calculates the maximum error of interpolation and plots the interpolantagainst the original function.

5.3.1 Interpolation

The PWInterpolator GUI takes as text inputs the bounds a, b of the interpolationdomain [a, b] and also a function represented as a string. The variable parametersfor interpolation are:

• number of elements,

• distribution stretch parameter,

• interpolation method,

32

Page 36: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 5. Results 5.3. Piecewise Interpolation

Figure 5.4: SimpleInterpolator GUI after error calculation.

• interpolant polynomial order.

These inputs can be loaded from a text file or input manually. Figure 5.5 shows thePWInterpolator GUI with an example of valid input values.

Selecting one element constructs a global polynomial which is equivalent to usingthe SimpleInterpolator GUI. Figure 5.6 gives an example of global interpolationusing a polynomial of order nine. The polynomial interpolant displays the behaviourof Runge’s phenomenon (see Chapter 2).

The alternative is to partition the domain into a number of elements. Figure 5.7gives an example of interpolation using four elements, each with polynomials oforder three. In this case, using piecewise interpolation has significantly increasedthe accuracy. The Show Intervals feature has been activated here, which plotsvertical lines at sub-interval boundaries. The resulting interpolant is representedin both vector and symbolic form, where each appears in a separate window (seeFigure 5.8 and 5.9).

5.3.2 Error Calculations

As with the SimpleInterpolator GUI, to calculate the interpolation error thesample number should be selected from the drop-down menu and the Calculate

button clicked. An additional feature of the PWInterpolator is that the maximumerror is shown on the axes. Figure 5.10 shows the results of the error calculation withthe maximum error shown on the graph. Here the zoom feature has been enabledto show the errors more clearly.

Another feature of this GUI is the Find Error feature. This allows the user toselect any point on the axes and calculate the interpolation error. This is useful

33

Page 37: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 5. Results 5.3. Piecewise Interpolation

Figure 5.5: PWInterpolator GUI with input.

Figure 5.6: PWInterpolator GUI with simple example.

34

Page 38: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 5. Results 5.3. Piecewise Interpolation

Figure 5.7: PWInterpolator GUI with piecewise example.

Figure 5.8: PWInterpolator GUI interpolant result.

Figure 5.9: PWInterpolator GUI interpolant symbolic result.

35

Page 39: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 5. Results 5.4. Interpolation Analysis

Figure 5.10: PWInterpolator GUI after error calculation.

to identify additional areas with large errors. Figure 5.11 shows a different sectionof the same interpolant as before, this time with errors calculated using the Find

Error button.

5.3.3 Saving Results

Once the interpolation and error calculations have been completed there is an optionto save the results. The idea is to use the same function and interpolation boundsand only adjust the interpolation parameters, saving the results each time to thesame file. This file can then be loaded by the InterpolantAnalyser GUI, to analysethe accuracy of interpolation with respect to the different parameters.

5.4 Interpolation Analysis

Selecting Interpolation Analysis from the Welcome drop-down menu opensthe InterpolantAnalyser GUI. This GUI is used to display multiple interpolationresults saved by the PWInterpolator GUI. Figure 5.12 shows a set of resultswith varying numbers of elements, loaded from the text file named data. It seemsreasonable to assume from these results that an even number of elements is best inthis case, and the maximum interpolation error decreases as the number of elementsincreases.

The radio buttons on the right are to select the interpolants to plot. Figure 5.13shows a plot of each interpolant on the GUI axes.

Finally, the Plot All Errors feature draws all of the interpolant error graphs on

36

Page 40: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 5. Results 5.4. Interpolation Analysis

Figure 5.11: PWInterpolator GUI after user selected error calculation.

Figure 5.12: InterpolantAnalyser GUI with data loaded.

37

Page 41: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 5. Results 5.5. 2D Lagrange Interpolation

Figure 5.13: InterpolantAnalyser GUI with interpolant plots.

the axes. It is clear by inspection that the fourth interpolant is the most accuratewith respect to the maximum error. Figure 5.14 shows the error plots for eachinterpolant.

Figure 5.14: InterpolantAnalyser GUI with error plots.

5.5 2D Lagrange Interpolation

Selecting 2D Interpolation from the Welcome drop-down menu opens theInterpolation2D GUI. This GUI facilitates interpolation of functions of two inde-pendent variables and plots the resulting interpolant against the original function.

38

Page 42: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 5. Results 5.5. 2D Lagrange Interpolation

5.5.1 Interpolation

The text inputs required for interpolation are the vertices of any right-angled tri-angle, to represent the domain, the string representation of a function and a plotparameter. The plot parameter dictates the refinement of the plot only and doesnot affect interpolation. The order of interpolation is selected from a drop-downmenu and can be linear or quadratic. Figure 5.15 shows the GUI with an exampleof valid input.

Figure 5.15: Interpolation2D GUI with inputs.

The Interpolate button is clicked to initiate interpolation. The result is ex-pressed in both vector and symbolic form. Figure 5.16 shows an example of quadraticinterpolation where the result is an exact fit to the original function. This is becausethe original function is also a quadratic polynomial. Note that the surface plot iscontained to the given triangle dimensions, as this is where it is defined. Figure 5.17shows another example of quadratic interpolation. This time the interpolant doesnot match the function exactly but matches at interpolation points as required.

39

Page 43: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 5. Results 5.5. 2D Lagrange Interpolation

Figure 5.16: Interpolation2D GUI with quadratic example.

Figure 5.17: Interpolation2D GUI with exponential example.

40

Page 44: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 6

Testing and Evaluation

This chapter is concerned with the techniques used to test various parts of thesystem and analyses the results of these tests.

6.1 Testing Method

Testing of the system was completed in two phases: unit testing of individual com-ponents followed by system testing of the complete system. Test cases were createdfor both unit and system tests, with expected results calculated to enable proof ofsuccess. Testing was then further split into two areas: functional testing and userinterface testing.

For functional unit testing, components of the system, such as converting a stringto a function and calculating the distribution of elements, were tested individuallyon the command line before being incorporated into the main system for furthertesting.

For functional system testing the majority of the test cases were created bydevising mathematical examples, with expected results calculated by solving themby hand. The system was proved by running the test cases against it and checkingthat the system results matched those expected. This approach was suitable toprove that all of the interpolation and error calculations produced correct results.

For user interface testing, the functionality of the GUI was tested incrementallyas each feature was added. The main functionality requirements were to processthe input data correctly and display the output data in a consistent way. Thiswas achieved by inputting a variety of valid inputs and checking the results. TheGUI does not currently have any input validation, this will be discussed as animprovement in Chapter 7.

The following sections discuss the specific functional system tests for the majorsystem functions.

6.2 Lagrange Interpolation

The first aspect of Lagrange interpolation to be tested was the product of linearpolynomials within the basis functions, for now omitting the i 6= k constraint, defined

41

Page 45: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 6. Testing and Evaluation 6.3. Hermite Interpolation

byn∏i=0

(x− xi). (1)

A test program was produced which took interpolation points xi as input and pro-duced the product defined by (1). The correct solutions were determined by passingthe product expression to the MATLAB expand function. The test results werethen compared with the correct solutions.

The complete Lagrange interpolation code was tested using an interpolationexample from An Introduction to Numerical Analysis [15] and also some simplehand worked examples. The tests varied the numbers of interpolation points andfunctions to ensure that there were no false positives results.

6.3 Hermite Interpolation

Hermite interpolation involved a greater breakdown of testing to check all sectionsof the code. An example from An Introduction to Numerical Analysis [15] wasused to ensure that the interpolation produced the correct results. However, a falsepositive did occur. A second example identified a mistake in the code which wasthen rectified. This shows the importance of using a number of tests with varyingparameters.

6.4 Piecewise Interpolation

Piecewise interpolation involved partitioning the given domain, either uniformlyor using a given stretch parameter. This was simple to check by applying thedistribution formulae to a selection of intervals [a, b] and comparing the results withworked examples.

Interpolation methods were then applied to each element of the domain. Theindividual polynomials were tested on their corresponding domain as with globalLagrange and Hermite interpolation.

6.5 Error Calculations

The error calculation formula calculated the maximum difference between the in-terpolant and the original function, across a number of sample points. This wastested by evaluating the interpolant and function at the given sample points, usingMATLAB, and subtracting one from the other on the command line. These cor-rect results were then compared with the results of the system. It was also usefulto check that the error at all interpolation points was zero, as by the definition ofinterpolation.

42

Page 46: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 6. Testing and Evaluation 6.6. 2D Lagrange Interpolation

6.6 2D Lagrange Interpolation

As with the other interpolation methods, 2D Lagrange interpolation was tested usingnon-trivial examples evaluated by hand. There were no published examples avail-able so the accuracy of the tests was dependent on the accuracy of the hand workedcalculations. Another good test was to interpolate linear and quadratic polynomi-als, as linear interpolation of a linear polynomial, and quadratic interpolation of aquadratic polynomial, is exact.

43

Page 47: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 7

Conclusions

This chapter summarises the main achievements of the project and proposes somefurther work.

7.1 Achievements

The main aim of the project was to create a system with a graphical user interfacewhich could be used to investigate the accuracy of a number of interpolation meth-ods. This requirement was met in full and the resulting system offers numerousinterpolation approaches to be examined, with an easy to use interface that dynam-ically plots changes in parameters. The system meets all of the requirements setat the design stage (see Chapter 3), as illustrated in the results section 5, with theexception of general Hermite interpolation and the adaptive error calculation.

General Hermite interpolation is quite complex, and on investigation at im-plementation time it was found that it would have to be implemented from firstprinciples including derivation of formulae. This aspect was outside the scope of theproject and was left as a possible future enhancement.

The adaptive method for calculating the interpolation error, where the functionis unknown, was not implemented as the focus of the system was approximatinggiven functions. This method, therefore, did not fit within this objective and so wasomitted. The scope of the system could be broadened to include this extra feature.This would be useful for determining the accuracy of interpolation used in digitalsignal processing [2].

Input validation is another area which was not implemented, and whilst it wasnot a formal requirement, it is still an important area that should be included beforemaking the system generally available.

The system offers multiple features not provided by any other known interpola-tion system. The most useful achievement is that the user has complete control overinterpolation parameters and can investigate multiple factors which affect interpo-lation accuracy and see the results in real time. The two dimensional interpolationgives the system an additional edge, and is a stepping stone to more advancedinterpolation methods.

44

Page 48: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Chapter 7. Conclusions 7.2. Further Work

7.2 Further Work

Whilst the system is useful for analysing interpolation, there are a number of areaswhere it could be extended.

The accuracy of two dimensional interpolation is not currently calculated. Errorcalculations, similar to those for interpolation of functions of one variable, could beadded, with the maximum error being represented on the surface plot. Higher orderinterpolants could also be included.

A limitation of the system’s interpolation of functions of two independent vari-ables is that the surface plot limits the domain to right-angled triangles. This isbecause the plot takes an upper triangular matrix of values to plot. An improvementwould be to allow the use of any simplex domain.

Further to this, work could be done to find alternative domains on which tointerpolate. The next stage would be to implement interpolation on a regular grid.This is widely used in image processing [12].

7.3 Summary

Overall, the project was a great success and the resulting system will be a usefulinterpolation analysis tool.

45

Page 49: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Bibliography

[1] “Lecture 4: The Poisson Equation in 2D” From University of ManchesterCS609: Algorithms for Differential Equations lecture notes.

[2] Saeed Babaeizadeh. “Interpolation in Digital Signal Processing and Nu-merical Analysis”. http://www.ece.neu.edu/students/sbabaeiz/paper_

poster/interpolation.htm, 2003.

[3] D. Davis and E. Bansch. An operator-splitting finite-element approach to the8:1 thermal-cavity problem. International Journal for Numerical Methods inFluids, 40, pages 1019–1030, 2002.

[4] M. Embree. “Lecture 11: Interpolation Error Bounds” From NUMERICALANALYSIS I. http://www.caam.rice.edu/~caam453/lecture11.pdf, Octo-ber 2009.

[5] M. Embree. “Lecture 12: Hermite Interpolation; Piecewise Polynomials In-terpolation” From NUMERICAL ANALYSIS I. http://www.caam.rice.edu/

~caam453/lecture12.pdf, October 2009.

[6] Amparo Gil, Javier Segura, and Nico Temme. Numerical Methods for SpecialFunctions. Society for Industrial and Applied Mathematics, 2007.

[7] The MathWorks Inc. “Creating GUIs with GUIDE”. http://www.mathworks.com/help/techdoc/creating_guis/bqz6p81.html, 2011.

[8] Gasper Jaklic, Jernej Kozak, Marjeta Krajnc, Vito Vitrih, and Emil Zagar.Barycentric coordinates for lagrange interpolation over lattices on a simplex.Numerical Algorithms, pages 93–104, 2008.

[9] David J.Hinson. “iTunes Preview - Interpolate”. http://itunes.apple.com/us/app/interpolate/id302970523?mt=8, January 2009.

[10] Tom Kiffe. “Interpolation Applet”. http://www.math.tamu.edu/~tkiffe/

math639/Tools/interp.html.

[11] Sean McHugh. “Digital Camera Sensors.” From Cambridge in Colour. http:

//www.cambridgeincolour.com/tutorials/camera-sensors.htm.

[12] Sean McHugh. “Digital Image Interpolation.” From Cambridge in Colour.http://www.cambridgeincolour.com/tutorials/image-interpolation.

htm.

46

Page 50: Piecewise Hermite Interpolation - University of Manchesterstudentnet.cs.manchester.ac.uk/resources/library/3rd-year-projects/... · Piecewise Hermite Interpolation Author: Leanne

Bibliography Bibliography

[13] Ron Miech. “Interpolation Polynomials”. http://www.math.ucla.edu/

~ronmiech/Interpolation/HTMDOCS/Introduction/Interpolation_

Applet.htm, December 2002.

[14] Scott Small. “Introduction to Galerkin and Finite Element Methods”. http:

//www.math.uiowa.edu/~ssma/documents/FEM.pdf, December 2008.

[15] Endre Suli and David Mayers. An Introduction to Numerical Analysis. Cam-bridge University Press, The Edinburgh Building, Cambridge CB2 8RU, UK,2006.

[16] Eric W. Weisstein. “Least Squares Fitting.” From MathWorld–A WolframWeb Resource. http://mathworld.wolfram.com/LeastSquaresFitting.

html, April 2011.

[17] Eric W. Weisstein. “Newton’s Divided Difference Interpolation Formula.”From MathWorld–A Wolfram Web Resource. http://mathworld.wolfram.

com/NewtonsDividedDifferenceInterpolationFormula.html, April 2011.

[18] Joannes J. Westerink. “Lecture 9: Hermite Interpolating Polynomials”. http://www.nd.edu/~jjwteach/441/PdfNotes/lecture9.pdf, 2004.

47