1 interpolation - application.wiley-vch.de · 1.3 polynomial interpolation when the polynomial has...

60
1 Interpolation Examples of this chapter can be found in the directory Vol2_Chapter1 within the enclosed CD-ROM. 1.1 Introduction This chapter deals with the problems of estimating the N parameters a of a model f ðx; aÞ that exactly satisfy N conditions assigned a priori. A second topic concerns the use of such a model to make previsions. A common choice of the N conditions is to assign N values x i , y i and ask the model to exactly interpolate them, by requiring the solution of the system: y i ¼ f ðx i ; aÞ ði ¼ 1; ... ; NÞ ð1:1Þ The method of exact interpolation is based on the solution either of the system (1.1) or of the other systems deriving from the N conditions that have to be satised by the N model parameters. The exact interpolation presents two obvious but fundamental limitations. 1) The method can be applied only if x and y are unbiased (not affected by experimental errors); otherwise, the interpolating function would assume the trend of the random error. 2) The points x i and the selected interpolating function must allow to univocally evaluate parameters a. A typical application of the exact interpolation is in the simulation of hard time- computing functions. Only the case with a single independent variable x is considered. Support points are the pairs of values ðx i ; y i Þ; specically, values x i shall be called abscissas, whereas values y i ordinate. j1 Interpolation and Regression Models for the Chemical Engineer: Solving Numerical Problems Guido Buzzi-Ferraris and Flavio Manenti Copyright Ó 2010 WILEY-VCH Verlag GmbH & Co. KGaA, Weinheim ISBN: 978-3-527-32652-5

Upload: truongbao

Post on 21-Aug-2019

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

1Interpolation

Examples of this chapter can be found in the directory Vol2_Chapter1within theenclosed CD-ROM.

1.1Introduction

This chapter deals with the problems of estimating the N parameters a of a modelf ðx; aÞ that exactly satisfyN conditions assigned a priori. A second topic concerns theuse of such a model to make previsions.

A common choice of theN conditions is to assignN values xi, yi and ask themodelto exactly interpolate them, by requiring the solution of the system:

yi ¼ f ðxi; aÞ ði ¼ 1; . . . ;NÞ ð1:1Þ

Themethod of exact interpolation is based on the solution either of the system (1.1)or of the other systems deriving from theN conditions that have to be satisfied by theN model parameters.

The exact interpolation presents two obvious but fundamental limitations.

1) The method can be applied only if x and y are unbiased (not affected byexperimental errors); otherwise, the interpolating function would assume thetrend of the random error.

2) The points xi and the selected interpolating function must allow to univocallyevaluate parameters a.

A typical application of the exact interpolation is in the simulation of hard time-computing functions.

Only the case with a single independent variable x is considered.

Support points are the pairs of values ðxi; yiÞ; specifically, values xi shall be calledabscissas, whereas values yi ordinate.

j1

Interpolation and Regression Models for the Chemical Engineer: Solving Numerical ProblemsGuido Buzzi-Ferraris and Flavio ManentiCopyright � 2010 WILEY-VCH Verlag GmbH & Co. KGaA, WeinheimISBN: 978-3-527-32652-5

Page 2: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

1.1.1Which Model to be Adopted?

The model adopted for interpolation should have the following features.

1) It should represent the function at the best.2) It should be easy to use.3) It should not be time consuming in providing a prevision.4) Adaptive parameters should be easily and univocally evaluable.

Polynomial functions are one of the most important classes:

PnðxÞ ¼ a0 þ a1xþ � � � þ anxn ð1:2Þ

When there are numerous points to be interpolated, the use of a single interpo-lating polynomial function is generally avoided and a series of low-order polynomialsare surely preferred. Alternative strategies exist, such asHermite polynomials, spline,and B�ezier curves.

Rational functions represent another important class. They are denoted by the ratiobetween two polynomial functions:

Qn;mðxÞ ¼ PnðxÞPmðxÞ ð1:3Þ

Even though it is nonlinear in parameters, this function can be easily linearized tosimplify the evaluation of parameters.

It is more difficult to operate with rational functions rather than polynomials(i.e., consider derivative and integral calculations), but rational functions have theadvantage to better interpolate many functions.

Other interesting classes of functions are trigonometric functions, which areuseful for periodic phenomena, and exponential functions.

1.1.2Which Points?

Sometimes, values of x required in the exact interpolation cannot be selected, forexample, when the experimental data have been already acquired by the field or whenthe data set is provided by tables and no other experiments can be carried out.

Conversely, it is possible to appropriately select the xi values in interpolating anevaluable function.

Especially with polynomial interpolations, a bad choice of the experimental pointsmay lead to erroneous results.

Some authors discriminate between two cases: they call interpolation the problemwith the data stored in a table and function approximation the problem where thepoints can be accurately selected.

2j 1 Interpolation

Page 3: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

1.2Classes for Function Interpolation

Polynomial and rational interpolations are particularly important and they were bothimplemented in an appropriate class.

In the BzzMath library, the class is called BzzInterpolation.

TheBzzInterpolation class has two constructors: the default constructor anda constructor that requires the vectors x of abscissas and the corresponding values ofordinates y as argument.

Example 1.1

Let x ¼ f1:; 2:; 3:g be the abscissas. In correspondence with these points, theordinates y ¼ f1:; 2:; 4:g are obtained.The program to initialize BzzInterpolation objects is

#include ‘‘BzzMath.hpp’’

void main(void)

{

BzzVector x(3,1.,2.,3.);

BzzVector y(3,1.,2.,4.);

BzzInterpolation a; // default constructor

a(x,y);

a.BzzPrint(‘‘BzzInterpolation a’’);

BzzPrint(‘‘\n\nConstructor’’);

BzzInterpolation b(x,y);

b.BzzPrint(‘‘BzzInterpolation b’’);

BzzPause();

}

Support points can be changed during the program execution through the operator(,). For example,

BzzInterpolation p;

BzzVector x(......),y(......);

p(x,y);

Additional examples of the use of BzzInterpolation class can be found in

BzzMath/Examples/BzzMathBasic/

Interpolation

either on the enclosed CD-ROM or at the web site:

www.chem.polimi.it/homes/gbuzzi.

1.2 Classes for Function Interpolation j3

Page 4: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

1.3Polynomial Interpolation

When the polynomial has a single variable x, it can assume the form

PnðxÞ ¼ a0 f0ðxÞþ a1 f1ðxÞþ a2 f2ðxÞþ � � � þ an fnðxÞ ð1:4Þwhere f0ðxÞ; f1ðxÞ; . . . ; fnðxÞ are polynomial expressions with an order smaller thanor equal to n and known coefficients.

For example,

P4ðxÞ ¼ a0 þ a1xþ a2x2 þ a3x3 þ a4x4

P4ðxÞ ¼ a0ðxþ 3Þþ a1ð1�x2Þþ a2ðx2 þ xÞ2 þ a3ðx3�2x2 þ 4xÞþ a4x4

P4ðxÞ ¼ a0 þ a1xþ a2ð2x2�1Þþ a3ð4x3�3xÞþ a4ð8x4�8x2 þ 1Þare valid formulations of a 4-degree polynomial.

Since an-degree polynomial has ðnþ 1Þ coefficients, ðnþ 1Þdistinct conditions arenecessary to evaluate them univocally.

Given ðnþ 1Þ support points ðxi; yiÞ and ðnþ 1Þ distinct abscissas x, coefficientsaiði ¼ 0; . . . ; nÞ can be obtained by solving the linear system with ðnþ 1Þ equations:

Fa ¼ y ð1:5ÞA polynomial is in its standard form when

PnðxÞ ¼ a0 þ a1xþ � � � þ anxn ð1:6Þ

If the polynomial is written in the standard form, the linear system (1.5) becomes

Xa ¼ y ð1:7Þwhere the matrix

X ¼

1 x0 ðx0Þ2 . . . ðx0Þn1 x1 ðx1Þ2 . . . ðx1Þn. . . . . . . . . . . . . . .

1 xn�1 ðxn�1Þ2 . . . ðxn�1Þn1 xn ðxnÞ2 . . . ðxnÞn

�������������

�������������ð1:8Þ

is known as Vandermonde matrix.If the abscissas xi are different from each other, the following features are valid.

1) The determinant of Vandermonde matrix X is always nonzero.2) The system (1.7) has a single solution.3) An interpolating polynomial passes through support points.

The interpolating polynomial can be obtained with different techniques and it canapparently assume different forms.

Given nþ 1 support points ðxi; yiÞ, there is a unique n-degree polynomial passingthrough them.

4j 1 Interpolation

Page 5: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

As a result, even though the polynomial apparently assumes different forms, it is everthe same polynomial with the same maxima, minima, and zeros.

Polynomials are largely employed in each field of numerical analysis: they areadopted in calculating integrals, in solving differential equations, in functionapproximation, and so on.

They are used especially for their following properties.

1) They are easy to manage and use: polynomial integration and derivatives do notpresent any problematic issues.

2) Parameter evaluation is quite easy.3) It is easy to carry out a prevision in a point different from the assigned

abscissa.4) Some properties that make them unique: the sum, the subtraction, the product

between two polynomials are still polynomial functions; if the scale of thevariable x is changed, by adding or multiplying abscissas by any value, apolynomial is again obtained.

Moreover, polynomials were studied for several years and, as a result, the theory ofpolynomial approximation is well known and well established.

Finally, the analysis of the exact interpolation is educationally very important since itallows to analyze some numerical problems.

In spite of these favorable properties, a negative feature is to be underlined:polynomials obtained through the exact interpolation often do not approximate someclasses of function in an accurate manner. Specifically, the inaccuracy arises whenthe polynomial interpolation is carried out without an appropriate care and when thepolynomial degree is considerably high.

It is incorrect to think a function approximation is better for higher degrees of thepolynomial obtained by the exact interpolation.

This thinking originates for three reasons.

1) There are theoretical motivations to think that the polynomial approximationaccuracy improves by increasing the polynomial degree.

Weierstrass theorem. If f ðxÞ is a continuous function with continuous derivatives inan interval ½a; b�, by assigning any positive value d, there is a polynomial PnðxÞ witha degree equal to n that allows the following inequality for any value of x in theinterval ½a; b�:

f ðxÞ�PnðxÞj j < d ð1:9ÞIn this case, the ambiguity is dictated by the fact that the interpolating polynomial inWeierstrass theorem is not the function that exactly interpolates some assignedpoints.

2) There is another theoretical reason that seems to motivate the use of largerpolynomial degrees to achieve better approximations.

1.3 Polynomial Interpolation j5

Page 6: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Taylor polynomials. It is possible to approximate a derivable function f ðxÞ around thepoint x0 through a Taylor expansion:

PnðxÞ ¼ f ðx0Þþ f 0ðx0Þðx�x0Þþ � � � þRnþ 1ðxÞ ð1:10Þwhere

Rnþ 1ðxÞ ¼ 1n!

ðxx0

ðx�tÞnf ðnþ 1ÞðtÞdt ¼ ðx�x0Þnþ 1

ðnþ 1Þ! f nþ 1ðjÞ ð1:11Þ

for j in the range ½x0; x�.The Taylor polynomial is not obtained by using the exact interpolation for nþ 1

points; moreover, its validity cannot be extended to the range ½x0; xn�, but it is limitedto the neighborhood of x0.

3) Finally, many examples of polynomial interpolations obtained through the exactinterpolation where the error decreases by increasing the polynomial degree arereported in many books on numerical analysis. These examples are generallyreferred to special functions, such as sinðxÞ and cosðxÞ.

Contrary to the appearance, it is very rare for higher interpolating polynomial degreesto continuously improve the function approximation; usually, the opposite is true.

When the function to be approximate presents asymptotic behaviors and/or dis-continuities in the derivatives, the approximation through the method of exactpolynomial interpolation must be avoided.

The polynomial coefficients can be evaluated by imposing conditions differentfrom the ones already discussed (that is passing through nþ 1 data points). Forexample, the first or higher order derivatives may be assigned in some specificsupport points.

Specifically, if both the ordinate yi and the first derivative y0i are assigned for eachsupport point, a Hermite polynomial is obtained.

It is always suitable to exploit the value of first or higher derivatives of the functionto be interpolated to improve the accuracy.

The following assumption is made in the first part of this chapter.

The nþ 1 conditions needed for evaluating model parameters require to passthrough the nþ 1 support points ðxi; yiÞ with i ¼ 0; . . . ; n, xi 6¼ xj, and i 6¼ j.

1.3.1Error in Polynomial Interpolation

Suppose to approximate the function f ðxÞ through a polynomial PnðxÞ obtained byan exact interpolation with nþ 1 support points. As a certain function is approxi-mated by a simplified model, one should also ask what is the difference betweenthe complete model (the function) and the simplified model (the interpolatingpolynomial).

6j 1 Interpolation

Page 7: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Obviously, the gap between the function and the polynomial is negligible incorrespondence with the support points. For all the remaining points, the followingproperty is valid.

Error in a polynomial approximation. If the polynomial PnðxÞ is adopted to make aprevision of the value that the function f ðxÞ would have in correspondence with aspecific point x ¼ z, which is not a support point, the error estimation is given by therelation

f ðzÞ�PnðzÞ ¼ ðz�x0Þðz�x1Þ � � � ðz�xnÞ fnþ 1ðjÞðnþ 1Þ! ð1:12Þ

where x0 < x1 < � � � < xn and x0 � j � xn. The point j, where the ðnþ 1Þthderivative is evaluated, is unknown and varies according to z.

Relation (1.12) is valid for functions f ðxÞ that can be differentiated nþ 1 times.The formula to evaluate the error has some practical limitations.

1) The ðnþ 1Þth derivative is rarely known.2) Usually, the error evaluated by the formula is not very useful, as its estimate is

inaccurate.

For example, consider the function

yðxÞ ¼ 1x

Given the abscissas 1:; 2:; 3:; 4:; 5: and using a 4-degree polynomial, the previsionP4ð4:5Þ ¼ :216146 is obtained in correspondence with z ¼ 4:5, with an absoluteerror equal to 0:006076.

The error can be estimated by using formula (1.12).

ð4:5�1:Þð4:5�2:Þð4:5�3:Þð4:5�4:Þð4:5�5:Þ 5!

j65!

where 1: � j � 5: As a consequence, the error estimation is in the range of0:000021� 3:28125 and it is too inaccurate to be useful.

However, this formula allows to make some important considerations.

1) About polynomial degree or the amount of support points.2) On the prevision point z.3) On the interval size.4) About the disposition of abscissas x0; x1; . . . ; xn.

1) From the formula, it seems that the error proportionally decreases to ðnþ 1Þ! byincreasing the polynomial degree. It is true only for functions, with derivativesincreasing slower than n!, that is, sinðxÞ and cosðxÞ, whereas derivatives usuallygrow faster. For this reason, the ratio between the ðnþ 1Þth derivative and theterm ðnþ 1Þ! initially decreases and then increases with the polynomial degree.As a consequence, the error of the polynomial interpolation can increasewith thepolynomial degree beyond a certain value.

1.3 Polynomial Interpolation j7

Page 8: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

2) The function

Wnþ 1ðz; x0; x1; . . . ; xnÞ ¼ ðz�x0Þðz�x1Þ � � � ðz�xnÞ ð1:13Þ

is a ðnþ 1Þ-degree polynomial with zeros in correspondence with the abscissas; as aresult, it should alternate maxima andminima while their value exclusively dependson the location of abscissas.

If the abscissas x0; x1; . . . ; xn are evenly spaced, maxima and minima of thefunctionWnþ 1ðzÞ are small in the center of the interval ½x0; xn� and large toward theborders. The higher the polynomial degree, the larger the absolute value of maximaand minima of the function Wnþ 1ðzÞ.3) If the dimensions of the interval ½x0; xn� increase, the error in polynomial

interpolation increases, too, especially when n is large, as the functionWnþ 1ðz; x0; x1; . . . xnÞ increases. On the other hand, the error generally ap-proaches zero when the interval tends to be zero.

4) Values of both Wnþ 1ðzÞ and error strongly depend on the selection of theabscissas. Therefore, it is possible to select the abscissas for minimizing themaximum value ofWnþ 1ðzÞ in the interval ½x0; xn�. We will discuss this problemlater.

The following general rules have to be taken into consideration to carry out anexact interpolation.

1) Do not use polynomials with a degree higher than 3–5.2) The interpolation may become less accurate toward the borders of the interval.3) If possible, abscissas must be appropriately selected.4) The error may be very large outside the interval: avoid any extrapolation.

1.4Roots-Product Form

A n-degree polynomial is equal to zero for n values of x:w1;w2; . . .wn. They are calledpolynomial roots and they can assume either real or complex values.

For example, the polynomial

P3ðxÞ ¼ x3�2x2 þ 4x�8

¼ ðx�2Þðx2 þ 4Þ ¼ ðx�2Þðx�i2Þðxþ i2Þ

has one real root and two complex roots.

A n-degree polynomial may always be written in the following form, called roots-product form:

PnðxÞ ¼ anðx�w1Þðx�w2Þ � � � ðx�wnÞ ð1:14ÞThe roots-product form is always well conditioned.

8j 1 Interpolation

Page 9: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Knowing the n real roots w1;w2; . . . ;wn of the polynomial function and the value ofthe coefficient an, the unique n-degree polynomial can be immediately obtained.

If a polynomial is given in the roots-product formand the roots are all real numbersand the coefficient an is known, the prevision p in correspondence with x ¼ z can beevaluated with the following algorithm.

Algorithm 1.1 Roots-product form prevision

Input: the polynomial degree n, the roots w1;w2; . . . ;wn, and the abscissa z.Output: the prevision p in x ¼ z.

p ¼ anfor i ¼ 1ðstep 1Þ;n

p ¼ p�ðz�wiÞAlgorithm 1.1 requires n flops.

Algorithm 1.1 is always stable.

1.5Standard Form

The most common form to describe an n-degree polynomial is the standard form

PnðxÞ ¼ a0 þ a1xþ � � � þ anxn ð1:15Þ

This form clearly highlights the fact that PnðxÞ is a polynomial with a degreeequal to n.

The standard form (1.15) could be ill conditioned.

This means that a small perturbation on coefficients can completely modify thepolynomial features, for example, roots can strongly change their values.

For example, if the polynomial (Wilkinson, 1963)

P20ðxÞ ¼ ðx�1Þðx�2Þðx�3Þ � � � ðx�19Þðx�20Þis written in the standard form, it becomes

P20ðxÞ ¼ x20�210x19 þ � � � þ 20!

If the coefficient 210multiplying x19 ismodified by a factor 10�7, some roots of thenew polynomial become complex numbers, even though they are evaluated withoutany numerical error.

The standard form is important to describe the polynomial and it should be usedwith care. It is worth noting the following points.

1) One is not obliged to write a polynomial in this form to represent the samepolynomial.

1.5 Standard Form j9

Page 10: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

2) It is unsuitable to perform operations with this form for evaluating a prevision inan assigned abscissa x ¼ z.

3) More performing methods exist to calculate polynomial coefficients.

Point No. 1If the polynomial is obtained in a different form, it is improper to transform it into thestandard form; as already seen in the previous example, some ill-conditionedproblems can arise.

For the same reason, it is also unseemly to explicitly get the polynomial in thestandard form: since this form is ill conditioned, some problems can arise inevaluating a prevision. In addition, in a specific abscissa, the prevision can differfrom the value of the data point adopted to evaluate the coefficients.

The 1-degree polynomial passing through the support points

x0 ¼ 100000:; y0 ¼ 1:=3: and x1 ¼ 100001:; y1 ¼ �2:=3:

with a precision of seven significant digits is

y ¼ 100000:3�x

Again, using seven significant digits, the value .3 rather than .3333333 incorrespondence with x ¼ 100000: is obtained.

A polynomial can be written in its centered standard form:

PnðxÞ ¼ a0 þ a1ðx�cÞþ � � � þ anðx�cÞn ð1:16Þwhere the center c is an assigned constant.

Whether a polynomial is obtained through an exact interpolation and if the abscissax ¼ 0 is not a support point, it is inappropriate to use the standard form. A partialremedy against round-off errors is to adopt the centered standard form by choosingan internal abscissa c of the polynomial interval.

For example, selecting c ¼ 100000: as center, the previous polynomial becomes

y ¼ :3333333�ðx�100000:ÞThen, y ¼ :3333333 is obtained in correspondence with x ¼ 100000.

Point No. 2Suppose to know the parameters ai of the polynomial in the standard form. Theseparameters may be obtained by techniques that differ from the exact interpolation,that is, by a Taylor expansion. To calculate the prevision in a specific point x ¼ z, it issuitable to adopt the so-called Horner method.

Horner procedure:

PnðzÞ ¼ a0 þ zða1 þ zða2 þ zð. . . zðan�1 þ anzÞÞÞÞ ð1:17Þ

Algorithm 1.2 Standard form prevision

Input: the polynomial degree n, coefficients of the polynomial in the standard forma0; a1; . . . ; an, and the abscissa z.

10j 1 Interpolation

Page 11: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Output: the prevision p.

p ¼ anfor i ¼ n�1ðstep�1Þ; 0

p ¼ p�zþ ai

Algorithm 1.2 requires n flops.

Horner algorithm allows improving performances in computational time whilepreserving the standard form.For example, the 4-degree polynomial

P4ðzÞ ¼ a0 þ a1zþ a2z2 þ a3z

3 þ a4z4

requires 10 multiplications and 4 additions; Horner algorithm needs only 4 multi-plications and 4 additions.

Sometimes, it can be useful to also evaluate the first derivative in correspondencewith z. It can be implemented by slightly modifying the previous algorithm.

Algorithm 1.3 Horner procedure with first derivative

Input: the polynomial degree n, coefficients of the polynomial in the standard forma0; a1; . . . ; an, and the abscissa z.Output: the prevision p and the first derivative dy.

dy ¼ 0p ¼ anfor i ¼ n�1ðstep�1Þ; 0dy ¼ dy�zþ pp ¼ p�zþ ai

Algorithm 1.3 requires 2n flops.

Point No. 3Usually, it is unsuitable to get parameters ai by solving system (1.7) for the followingreasons.

1) Vandermonde matrix is ill conditioned for high polynomial degrees and forabscissas not cleverly selected.

2) The standard form is ill conditioned when x ¼ 0 is not a support point.3) The solution of system (1.7) requires a number of calculations that are propor-

tional to the cube of the polynomial degree. It will be shown how it is possibleto obtain an interpolating polynomial with a computational effort equal to thesquare of the polynomial degree.

It is for the aforementioned reasons numerous books on numerical analysisconsider it unwise the use of the standard form for polynomials.

On the other hand, this form has been revalued and largely used to solvedifferential equation problems with boundary conditions (see Buzzi-Ferraris andManenti, 2011b), especially because of the following problem features:

1.5 Standard Form j11

Page 12: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

. Polynomials have a relatively small degree (3–5).

. Data points can be accurately selected.

. The zero is one of the data points.

. Many polynomials of the same order and with the same support abscissas areused.

. The first and the second derivatives must be evaluated in some predeterminedpoints within the polynomial validity interval.

It shall be shown how the standard form is significantly advantageous for suchpolynomials (Buzzi-Ferraris and Manenti, 2011b).

1.6Lagrange Method

With the Lagrange method, one need not solve the square system (1.5) in nþ 1equations (Ralston and Rabinowitz, 1988).

Then-degree polynomial is written as the linear combination of nþ 1 polynomials,each one with degree n:

PnðxÞ ¼ a0L0ðxÞþ a1L1ðxÞþ � � � þ anLnðxÞ ð1:18ÞIn Lagrange method, the polynomials L0ðxÞ; L1ðxÞ; . . . ; LnðxÞ are selected in order tomake unitary the diagonal of the matrix F of the system (1.5).

The polynomial

LiðxÞ ¼ ðx�x0Þðx�x1Þ � � � ðx�xi�1Þðx�xiþ 1Þ � � � ðx�xnÞðxi�x0Þðxi�x1Þ � � � ðxi�xi�1Þðxi�xiþ 1Þ � � � ðxi�xnÞ ð1:19Þ

is called Lagrange polynomial.

The polynomial LiðxÞ is an n-degree polynomial that is null in correspondencewith the abscissa xjðj ¼ 0; . . . ; n; j 6¼ iÞ and it is equal to 1 in correspondence with theabscissa xi.

The system (1.5) becomes

Ia ¼ y ð1:20Þand the polynomial PnðxÞ passing through the nþ 1 support points is immediatelywritten

PnðxÞ ¼Xni¼0

yiLiðxÞ ð1:21Þ

For example, given the support points ð1; 1Þ, ð2; 2Þ, and ð3; 4Þ, the 2-degreepolynomial in the Lagrange form is

P2ðxÞ ¼ ðx�2Þðx�3Þð1�2Þð1�3Þ þ 2

ðx�1Þðx�3Þð2�1Þð2�3Þ þ 4

ðx�1Þðx�2Þð3�1Þð3�2Þ

12j 1 Interpolation

Page 13: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Coefficients ai of the Lagrange form correspond to the values yi of the support points.

This version of Lagrange method is useful in manual evaluations or simpleprograms, but it does not perform so well when it is implemented within a generalprogram.

Therefore, the Lagrange method should be improved to overcome the followingweaknesses.

1) The number of calculations required for a prevision in an abscissa x ¼ z is largeby adopting equation (1.21).

2) When several previsions are performed in correspondence with differentabscissas, shared evaluations are not exploited.

3) The method does not allow to exploit the calculations already performed whena new support point is to be introduced (by increasing the polynomial degree).

It is useful to adopt a rearranged Lagrange form (Hamming, 1962).

A Lagrange polynomial can be written in the barycentric form:

ci ¼ 1ðxi�x0Þðxi�x1Þ � � � ðxi�xi�1Þðxi�xiþ 1Þ � � � ðxi�xnÞ ð1:22Þ

with ði ¼ 0; . . . ; nÞ;

vi ¼ ciz�xi

ði ¼ 0; . . . ; nÞ ð1:23Þ

wi ¼ viXnk¼0

vk

ði ¼ 0; . . . ; nÞ ð1:24Þ

PnðzÞ ¼Xni¼0

wiyi ð1:25Þ

Coefficients ci are called coefficients of the Lagrange form.

Coefficients ci can be iteratively obtained by introducing a support point at a time.In doing so, a limitation of the Lagrange method is overcome: new points can beintroduced (so to increase the polynomial degree) without restarting from thebeginning.

In fact,

c0 ¼ 1 ð1:26Þ

c0 ¼ 1ðx0�x1Þ ; c1 ¼ 1

ðx1�x0Þ ¼ �c0 ð1:27Þ

c0 ¼ 1ðx0�x1Þðx0�x2Þ ; c1 ¼ 1

ðx1�x0Þðx1�x2Þ ; c2 ¼ �c0�c1 ð1:28Þ

and so on.

1.6 Lagrange Method j13

Page 14: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Algorithm 1.4 Coefficients for Lagrange form

Input: the polynomial degree n, abscissas x0; x1; . . . ; xn.Output: coefficients for Lagrange c0; c1; . . . ; cn.

c0 ¼ 1for k ¼ 1ðstep 1Þ; nfor i ¼ 0ðstep 1Þ; k�1

ci ¼ ci=ðxi�xkÞck ¼ �

Xk�1

i¼0

ci

Algorithm 1.4 requires nðnþ 1Þ=2 flops.

Once the coefficients are evaluated, it is possible to make a prevision in correspon-dence with a point x ¼ z using the barycentric formula.

For the reasons that will be explained later, it is suitable to separately evaluate theweight wi of the barycentric formula and the prevision p.

Algorithm 1.5 Coefficients wi for Lagrange form

Input: the polynomial degree n, abscissas x0; x1; . . . ; xn, coefficients c0; c1; . . . ; cn, andabscissa x ¼ z.Output: weights of the barycentric formula w0;w1; . . . ;wn.

for i ¼ 0ðstep 1Þ;nwi ¼ ci

z�xi

den ¼Xni¼0

wi

for i ¼ 0ðstep 1Þ;nwi ¼ wi

den

Algorithm 1.5 requires nþ 1 flops, nþ 1 divisions, and n addictions.

Once the coefficientswi are known, the prevision in x ¼ z can be evaluated using thebarycentric formula.

Algorithm 1.6 Prevision with the barycentric Lagrange formula

Input: the polynomial degree n, weights w0;w1; . . . ;wn, and ordinates of the supportpoints y0; y1; . . . ; yn.Output: prevision p.

p ¼ 0for i ¼ 0ðstep 1Þ; n

p ¼ pþwiyi

14j 1 Interpolation

Page 15: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Algorithm 1.6 requires nþ 1 flops. As a result, to make an estimate, the Lagrangemethod requires nðnþ 1Þ=2 flops to evaluate ci, nþ 1 flops, nþ 1 divisions andadditions to evaluate wi, and other nþ 1 flops to get PnðzÞ.The Lagrange function in the BzzInterpolation class evaluates polynomialprevisions through the Lagrange method.

Example 1.2

Let x ¼ f1:; 1:3; 1:6; 1:9; 2:2g be the abscissas for the function yi ¼ e�x2i : EvaluateLagrange previsions for z ¼ f1:8; 1:65; 1:35g.The program is

#include ‘‘BzzMath.hpp’’

void main(void)

{

BzzPrint(‘‘\n\nLagrange’’);

BzzVector x(5,1.,1.3,1.6,1.9,2.2);

BzzVector y(5);

for(int i = 1;i <= 5;i++)

y[i] = exp(-x[i]*x[i]);

BzzInterpolation p(x,y);

double prev = p.Lagrange(1.8);

BzzPrint(‘‘\nPrevision %e’’,prev);

prev = p.Lagrange(1.65);

BzzPrint(‘‘\nPrevision %e’’,prev);

prev = p.Lagrange(1.35);

BzzPrint(‘‘\nPrevision %e’’,prev);

BzzPause();

}

The polynomial interpolation using the Lagrange method presents the followingshortcomings.

1) The polynomial form is more difficult to use, for example, when the polynomialis adopted to estimate the derivatives of the interpolated function.

2) The error is harder to estimate.3) Theoretically, it requires a number of calculations slightly larger than theNewton

method (explained in the next paragraph). In practice, differences in thecomputational time are negligible.

The Lagrange method is important from a theoretical point of view, as it allows tosimply demonstrate the existence and theuniqueness of an interpolating polynomial.It is even practically important for three features.

1) The value of parameters corresponds to the ordinate of support points.2) It allows better performances in CPU times when the interpolation is performed

for the same points but with different functions.3) The Lagrange form is always well-conditioned.

1.6 Lagrange Method j15

Page 16: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Therefore:

1) As the matrix F of the system is converted into the identity matrix I, modelparameters are the values of y in correspondence with the abscissas. Therefore,they have amathematical (and sometimes physical)meaning, contrary to all otherforms. This feature is fundamental for the finite elementsmethod. In fact, in thisimportant method for integrating differential equations with boundary condi-tions, it is possible tomake two polynomials continuous in two adjacent elementsby simply imposing the equivalence of their parameters in the shared point.

2) The value of the ordinates yi are not involved in the evaluation of the coefficientswi. Hence, if the same procedure is applied to different functions with the samepoints xi, the same coefficients wi are obtained. For this reason, it is suitable toseparately evaluate coefficients wi and the prevision P.

For example, suppose to evaluate a prevision by a parabolic interpolation in aninterval L in correspondence with the point 0:75L for different functions, once thefunction values are assigned in correspondence with three points: 0; 0:5L; L. The2-degree interpolating polynomial is

P2ðxÞ ¼ y0ðx�0:5LÞðx�LÞð�0:5LÞð�LÞ þ y1

xðx�LÞð0:5LÞð�0:5LÞ þ y2

xðx�0:5LÞLð0:5LÞ

The prevision in 0:75L is

P2ð0:75LÞ ¼ y00:25ð�0:25Þ

0:5þ y1

0:75ð�0:25Þð0:5Þð0:5Þ þ y2

0:75ð0:25Þ0:5

and by simplifying it

P2ð0:75LÞ ¼ �0:125y0 þ 0:75y1 þ 0:375y2

This feature is fundamental whenever a linear operator is involved, for example, toget formulae for the numerical integration (see Buzzi-Ferraris and Manenti, 2011b).Another advantage of the Lagrangemethod is the easiness ofwriting a programbasedon this algorithm to obtain a polynomial prevision (given nþ 1 support points).

Algorithm 1.7 Prevision with Lagrange

Input: the polynomial degree n, points x0; x1; . . . ; xn, ordinates of the support pointsy0; y1; . . . ; yn, and the prevision point x ¼ z.Output: prevision p.

p ¼ 0

for i ¼ 0ðstep 1Þ;nr ¼ yifor k ¼ 0ðstep 1Þ;nif ði 6¼ kÞr ¼ rðz�xkÞ=ðxi�xkÞ

p ¼ pþ r

16j 1 Interpolation

Page 17: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

The algorithm is not optimized, but it could be useful to carry out some previsionswhen one has not yet an ad hoc program.

1.7Newton Method

This method presents some interesting features to get an n-degree polynomialpassing through nþ 1 points (Kincaid and Cheney, 1990).

1) The k-degree polynomial is built by starting from the ðk�1Þ-degree polynomial.2) To build the polynomial and to evaluate the prevision, the required number of

flops is small.3) Round-off errors are small, too, throughout the calculations.4) It is possible to estimate the error by using the polynomial rather than the

function.

An n-degree polynomial in the Newton form is

PnðxÞ ¼ a0 þ a1ðx�x0Þþ a2ðx�x0Þðx�x1Þþ � � �þ anðx�x0Þ � � � ðx�xn�1Þ

ð1:29Þ

For example, given the support points ð1; 1Þ, ð2; 2Þ, and ð3; 4Þ, the 2-degreepolynomial in the Newton form is

P2ðxÞ ¼ 1þðx�1Þþ 0:5ðx�1Þðx�2Þ

1.7.1Coefficients� Evaluation

If the polynomial is written in the Newton form, system (1.5) becomes

a0 ¼ y0a0 þ a1ðx1�x0Þ ¼ y1. . .

a0 þ a1ðxn�x0Þþ � � � þ anðxn�x0Þ � � � ðxn�xn�1Þ ¼ yn

ð1:30Þ

The system solution is immediate as the coefficient matrix is triangular.For example, given the support points ð1; 1Þ, ð2; 2Þ, and ð3; 4Þ, the system (1.30) is

a0 ¼ 1

a0 þ a1ð2�1Þ ¼ 2 ða1 ¼ 1Þa0 þ a1ð3�1Þþ a2ð3�1Þð3�2Þ ¼ 4 ða2 ¼ :5Þ

The solution of system (1.30) requires n2 flops.

It is useful to change the point of view.

Newton method was considered as a way to get a linear system with an easier solution.It can also be considered as a criterion to build polynomials with increasing degree.

1.7 Newton Method j17

Page 18: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

The kth coefficient ak of the polynomial in the Newton form is denoted byexpression f ½x0; x1; . . . ; xk�, where xi denotes the points adopted to get coefficientsa0; a1; . . . ; ak.

Using this kind of representation, one can rewrite the polynomial as follows:

PnðxÞ ¼ f ½x0� þ f ½x0; x1�ðx�x0Þþ f ½x0; x1; x2�ðx�x0Þðx�x1Þþ � � �þ f ½x0; x1; . . . ; xn�ðx�x0Þðx�x1Þ � � � ðx�xn�1Þ

ð1:31Þ

The k-degree polynomial passing through the support points ðx0; y0Þ, ðx1; y1Þ, . . .,ðxk; ykÞ is denoted by Pkðx; x0; x1; . . . ; xkÞ. The points x0; x1; . . . ; xk are arbitrarilyselected from among the possible points.

The polynomial Pkðx; x0; x1; . . . ; xkÞ does not change even when the sequence of thesupport points is changed. However, only the aspect of the polynomial is different.

For example,

P2ðxÞ ¼ f ½x0� þ f ½x0; x1�ðx�x0Þþ f ½x0; x1; x2�ðx�x0Þðx�x1Þ¼ f ½x1� þ f ½x1; x2�ðx�x1Þþ f ½x1; x2; x0�ðx�x1Þðx�x2Þ

Therefore, by assigning the support points

x0 ¼ 1; y0 ¼ 1; x1 ¼ 2; y1 ¼ 2; x3 ¼ 3; y3 ¼ 4

it takes place

P2ðxÞ ¼ 1þðx�1Þþ 0:5ðx�1Þðx�2Þ¼ 2þ 2ðx�2Þþ 0:5ðx�2Þðx�3Þ

By knowing the ðk�1Þ-degree polynomial Pk�1ðx; x0; x1; . . . ; xk�1Þ, it is possibleto evaluate the polynomial Pkðx; x0; x1; . . . ; xkÞ as follows.

Denoting by Wkðx; x0; x1; . . . ; xk�1Þ the k-degree polynomial:

Wkðx; x0; x1; . . . ; xk�1Þ ¼ ðx�x0Þðx�x1Þ � � � ðx�xk�1Þ ð1:32ÞThis polynomial is equal to zero in correspondence with the points x0; x1; . . . ; xk�1

and it has also the coefficient ak ¼ 1.The k-degree polynomial

SkðxÞ ¼ Pk�1ðx; x0; x1; . . . ; xk�1Þþ lkWkðx; x0; x1; . . . ; xk�1Þ ð1:33Þis a linear combination of the two polynomials Pk�1ðx; x0; x1; . . . ; xk�1Þ andWkðx; x0; x1; . . . ; xk�1Þ. Therefore, it passes through the support points ðx0; y0Þ,ðx1; y1Þ, . . ., ðxk�1; yk�1Þ. Varying the parameter lk, a family of k-degree polynomialsall passing through the support points is generated. By imposing a new support pointðxk; ykÞ selected from among the data points not yet used, lk can be determined and,therefore, the polynomial Pk passing through the support points ðx0; y0Þ, . . . ,ðxk�1; yk�1Þ, ðxk; ykÞ.

Since

lk ¼ yk�Pk�1ðxkÞðxk�x0Þðxk�x1Þ � � � ðxk�xk�1Þ ð1:34Þ

18j 1 Interpolation

Page 19: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

it results

PkðxÞ ¼ Pk�1ðxÞþ ðyk�Pk�1ðxkÞÞ ðx�x0Þðx�x1Þ � � � ðx�xk�1Þðxk�x0Þðxk�x1Þ � � � ðxk�xk�1Þ ð1:35Þ

For example, suppose to find out the 2-degree polynomial passing through thesupport points ð1; 1Þ, ð2; 2Þ, and ð3; 4Þ.

The zero-degree polynomial passing through the first point is

P0ðxÞ ¼ 1

The 1-degree polynomial passing through first two points is

P1ðxÞ ¼ P0ðxÞþ ðy1�P0ðx1ÞÞðx�x0Þðx1�x0Þ ¼ 1þðx�1Þ ¼ x

Finally, the 2-degree polynomial is

P2ðxÞ ¼ P1ðxÞþ ðy2�P1ðx2ÞÞðx�x0Þðx�x1Þðx2�x0Þðx2�x1Þ

¼ xþ 0:5ðx�1Þðx�2ÞThe number of operations required to determine the polynomial parameters is equalto the number of operations required to solve the system (1.30), even though theirinterpretation is changed. Hence, n2 flops are required.

The idea to sequentially get the polynomials starting from other polynomial with asmall degree can be further improved by developing a more performing algorithm.

Rather than carrying out the linear combination between the ðk�1Þ-degreepolynomial and the k-degree polynomial, it is worth considering the following linearcombination between two ðk�1Þ-degree polynomials:

SkðxÞ ¼ ðx�x0ÞPk�1ðx; x1; . . . ; xkÞ�ðx�xkÞPk�1ðx; x0; . . . ; xk�1Þlk

ð1:36Þ

By varying lk, one still obtains a family of k-degree polynomials. Assigning thevalue xk�x0 to lk, the k-degree polynomial is obtained:

Pkðx; x0; x1; . . . ; xkÞ ¼ ðx�x0ÞPk�1ðx; x1; . . . ; xkÞ�ðx�xkÞPk�1ðx; x0; . . . ; xk�1Þðxk�x0Þ

ð1:37Þpassing through the support points ðx0; y0Þ; . . . ; ðxk�1; yk�1Þ; ðxk; ykÞ.It is worth noting that expression f ½x0; x1; . . . ; xk� is coefficient ak of Pkðx; x0;x1; . . . ; xkÞ.

Coefficient ak�1 of Pk�1ðx; x1; x2; . . . ; xkÞ can be rewritten with expressionf ½x1; . . . xk�, whereas coefficient ak�1 of Pk�1ðx; x0; . . . ; xk�1Þ is f ½x0; . . . xk�1�. Fromrelation (1.37), it can be deduced that coefficient ak of the polynomial Pk should be

f x0; x1; . . . ; xk½ � ¼ ð f ½x1; . . . ; xk��f ½x0; . . . ; xk�1�Þxk�x0

ð1:38Þ

1.7 Newton Method j19

Page 20: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Coefficient f ½x0; x1; . . . ; xk� is called kth rational difference for relation (1.38).

For example, to find out the 2-degree polynomial passing through the points ð1; 1Þ,ð2; 2Þ, and ð3; 4Þ, it is possible to set up the following scheme:

x0 ¼ 1

x1 ¼ 2

x2 ¼ 3

y0 ¼ f ½x0� ¼ 1

y1 ¼ f ½x1� ¼ 2

y2 ¼ f ½x2� ¼ 4

f ½x0; x1� ¼ 1

f ½x1; x2� ¼ 2f ½x0; x1; x2� ¼ 0:5

Relation (1.38) can be adopted to suitably evaluate the coefficients of the polyno-mial in the Newton form (1.31).

Algorithm 1.8 Coefficients in the Newton form

Input: the polynomial degree n, abscissas x0; x1; . . . ; xn, and ordinates y0; y1; . . . ; yn.Output: coefficient of Newton form a0; a1; . . . ; an.

for i ¼ 0ðstep 1Þ;ndi ¼ yi

a0 ¼ d0

for k¼ 1ðstep 1Þ;nfor i ¼ 0ðstep 1Þ; n�k

di ¼ ðdiþ 1�diÞ=ðxiþ k�xiÞak ¼ d0

Algorithm 1.8 requires nðnþ 1Þ=2 flops.

It is not necessary to store all the scheme of the rational differences: an auxiliaryvector d is sufficient.

The rational differences can also be written in the following form:

f ½x0; x1; x2; . . . ; xk� ¼ f ½x0�ðx0�x1Þðx0�x2Þ � � � ðx0�xkÞ

þ f ½x1�ðx1�x0Þðx1�x2Þ � � � ðx1�xkÞ þ � � �

þ f ½xk�ðxk�x0Þðxk�x1Þ � � � ðxk�xk�1Þ

ð1:39Þ

From relation (1.39), it can be deduced that f ½xi0 ; xi1 ; . . . ; xik � is constant against indexpermutations i0; i1; . . . ; ik in classical analysis.

For example,

f ½x0; x1; x2� ¼ f ½x2; x0; x1� ¼ f ½x1; x2; x0� ¼ f ½x0; x2; x1�

20j 1 Interpolation

Page 21: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

From a theoretical point of view, the order of support points is not relevant. In fact,

P2ðxÞ ¼ f ½x0� þ f ½x0; x1�ðx�x0Þþ f ½x0; x1; x2�ðx�x0Þðx�x1Þ¼ f ½x1� þ f ½x1; x2�ðx�x1Þþ f ½x1; x2; x0�ðx�x1Þðx�x2Þ

In numerical analysis, permutations are not equivalent.

If the polynomial is used for a prevision in a point x ¼ z, which is known beforeevaluating the polynomial coefficients, it is suitable to sort the points against theirdistance from z. The prevision is more accurate as the operations are better focusedon the final objective and the maximum accuracy is obtained by adjusting theprevision with terms that are gradually less relevant.

As an example, given the support points ð1; 1Þ, ð2; 2Þ, and ð3; 4Þ, it is suitable tointroduce the points sorted as ð2; 2Þ, ð3; 4Þ, and ð1; 1Þ in evaluating a prevision incorrespondence with z ¼ 2:1.The fact that the rational differences do not depend on the index permutations leadsto another consequence.

There are different ways (equivalent for the classical analysis) to write the rationaldifferences.

For example,

f x0; x1; x2½ � ¼ ð f ½x0; x1��f ½x0; x2�Þx1�x2

¼ ð f ½x0; x1��f ½x1; x2�Þx0�x2

Among the possible choices to evaluate the rational differences, the best sequencein terms of numerical accuracy is generally the following one, called Neville sequence:

x0x1x2x3x4

y0 ¼ f ½x0�y1 ¼ f ½x1�y2 ¼ f ½x2�y3 ¼ f ½x3�y4 ¼ f ½x4�

f ½x0; x1�f ½x1; x2�f ½x2; x3�f ½x3; x4�

f ½x0; x1; x2�f ½x1; x2; x3�f ½x2; x3; x4�

f ½x0; x1; x2; x3�f ½x1; x2; x3; x4�

f ½x0; x1; x2; x3; x4�

Aitken sequence is a possible alternative, even though it is usually less accurate:

x0x1x2x3x4

y0 ¼ f ½x0�y1 ¼ f ½x1�y2 ¼ f ½x2�y3 ¼ f ½x3�y4 ¼ f ½x4�

f ½x0; x1�f ½x0; x2�f ½x0; x3�f ½x0; x4�

f ½x0; x1; x2�f ½x0; x2; x3�f ½x0; x3; x4�

f ½x0; x1; x2; x3�f ½x0; x2; x3; x4�

f ½x0; x1; x2; x3; x4�

For example, to find out the 2-degree polynomial passing through the support pointsð1; 1Þ, ð2; 2Þ, and ð3; 4Þ ordered by growing abscissas, the Neville scheme is

x0 ¼ 1

x1 ¼ 2

x2 ¼ 3

y0 ¼ f ½x0� ¼ 1

y1 ¼ f ½x1� ¼ 2

y2 ¼ f ½x2� ¼ 4

f x0; x1½ � ¼ 2�12�1

¼ 1

f x1; x2½ � ¼ 4�23�2

¼ 2f x0; x1; x2½ � ¼ 2�1

3�1¼ 1

2

1.7 Newton Method j21

Page 22: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

and the Aitken scheme is

x0 ¼ 1

x1 ¼ 2

x2 ¼ 3

y0 ¼ f ½x0� ¼ 1

y1 ¼ f ½x1� ¼ 2

y2 ¼ f ½x2� ¼ 4

f x0; x1½ � ¼ 2�12�1

¼ 1

f x0; x2½ � ¼ 4�13�1

¼ 32

f x0; x1; x2½ � ¼ 1:5�13�2

¼ 12

Many forms of Newtonmethod can be obtained from these two schemes to representthe same polynomial. For example, from the Neville scheme

P2ðxÞ ¼ f ½x0� þ f ½x0; x1�ðx�x0Þþ f ½x0; x1; x2�ðx�x0Þðx�x1Þ¼ 1þðx�1Þþ 0:5ðx�1Þðx�2Þ

P2ðxÞ ¼ f ½x1� þ f ½x1; x2�ðx�x1Þþ f ½x0; x1; x2�ðx�x1Þðx�x2Þ¼ 2þ 2ðx�2Þþ 0:5ðx�2Þðx�3Þ

P2ðxÞ ¼ f ½x2� þ f ½x1; x2�ðx�x2Þþ f ½x0; x1; x2�ðx�x2Þðx�x1Þ¼ 4þ 2ðx�3Þþ 0:5ðx�3Þðx�2Þ

1.7.2Previsions

Once the coefficients ak are calculated, the estimate in x ¼ z is obtained by applyingthe Horner scheme:

PnðzÞ ¼ a0 þðz�x0Þða1 þðz�x1Þð � � � ÞÞ ð1:40Þ

Algorithm 1.9 Prevision with the Newton form

Input: the polynomial degree n, points x0; x1; . . . ; xn, coefficients of Newton forma0; a1; . . . ; an, and the point z.Output: prevision p.

p ¼ an

for i ¼ n�1ðstep�1Þ; 0p ¼ ai þ pðz�xiÞ

Algorithm 1.9 requires n multiplications and 2n additions.

For example, given the polynomial

P2ðxÞ ¼ 1þðx�1Þþ 0:5ðx�1Þðx�2Þ

the prevision in z ¼ 1:5 is

p ¼ 0:5; p ¼ 1þ 0:5ð1:5�2Þ ¼ 0:75; p ¼ 1þ 0:75ð1:5�1Þ ¼ 1:375

The function Newton in the BzzInterpolation class evaluates polynomialprevisions with the Newton method.

22j 1 Interpolation

Page 23: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Example 1.3

Let x ¼ f1:; 1:3; 1:6; 1:9; 2:2g be the abscissas for the function yi ¼ e�x2i . EvaluateNewton previsions in correspondence with z ¼ f1:8; 1:65; 1:35g.The program is

#include ‘‘BzzMath.hpp’’

void main(void)

{

BzzPrint(‘‘\n\nNewton’’);

BzzVector x(5,1.,1.3,1.6,1.9,2.2);

BzzVector y(5);

for(int i = 1;i <= 5;i++)

y[i] = exp(-x[i]*x[i]);

BzzInterpolation p(x,y);

double prev = p.Newton(1.8);

BzzPrint(‘‘\nPrevision %e’’,prev);

prev = p.Newton(1.65);

BzzPrint(‘‘\nPrevision %e’’,prev);

prev = p.Newton(1.35);

BzzPrint(‘‘\nPrevision %e’’,prev);

BzzPause();

}

1.7.3Additional Data Point

When a new point is introduced in a polynomial approximation, it is necessaryto decide whether the point is to be added to the existing ones (and the interpolatingpolynomial degree is to be increased) or the point must replace an old point (bymaintaining the current polynomial degree).

The former problem is solved by adopting either the Newton method or thebarycentric Lagrange method. Evidently, the latter problem can be solved by con-sidering the previous n support points that are still active together with the new point,as if nþ 1 support points were simultaneously assigned.

If the parameters of the previous polynomial were evaluated by using the Newtonmethod, it is possible to exploit the calculations already executed.

In fact, let us consider four support points ðx1; y1Þ, ðx2; y2Þ, ðx3; y3Þ, and ðx4; y4Þadopted to define the polynomial P3ðxÞ. To replace the point x4; f ½x4� with the newpoint x0 ¼ v; f ½x0� ¼ f ðvÞ, the rational differences necessary to build the newpolynomial are

P3ðxÞ ¼ f ½x0� þ f ½x0; x1�ðx�x0Þþ f ½x0; x1; x2�ðx�x0Þðx�x1Þþ f ½x0; x1; x2; x3�ðx�x0Þðx�x1Þðx�x2Þ

ð1:41Þ

1.7 Newton Method j23

Page 24: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

which uses the support points ðx0; y0Þ, ðx1; y1Þ, ðx2; y2Þ, ðx3; y3Þ and even the rationaldifferences to build the previous polynomial:

P3ðxÞ ¼ f ½x1� þ f ½x1; x2�ðx�x1Þþ f ½x1; x2; x3�ðx�x1Þðx�x2Þþ f ½x1; x2; x3; x4�ðx�x1Þðx�x2Þðx�x3Þ

ð1:42Þ

x0 ¼ v

x1

x2

x3

x4

f ½x0� ¼ f ðvÞf ½x1�f ½x2�f ½x3�f ½x4�

f ½x0; x1�f ½x1; x2�f ½x2; x3�f ½x3; x4�

f ½x0; x1; x2�f ½x1; x2; x3�f ½x2; x3; x4�

f ½x0; x1; x2; x3�f ½x1; x2; x3; x4�

New terms f ½x0�, f ½x0; x1�, f ½x0; x1; x2�, and f ½x0; x1; x2; x3� of the scheme are thecoefficients of the new polynomial. They can be evaluated from coefficients of theprevious polynomial, without necessarily re-evaluating the overall scheme.

For example, consider the following support points: ð1; 1Þ, ð2; 2Þ, and ð3; 4Þ. In thiscase, the polynomial in the Newton form is

P2ðxÞ ¼ 1þðx�1Þþ 0:5ðx�1Þðx�2ÞNow, by introducing the new support point ð0; 3Þ and, at the same time, removing

the existing point ð3; 4Þ, the scheme of rational differences becomes

x0 ¼ 0

x1 ¼ 1

x2 ¼ 2

x3 ¼ 3

y0 ¼ f ½x0� ¼ 3

y1 ¼ f ½x1� ¼ 1

y2 ¼ f ½x2� ¼ 2

y3 ¼ f ½x3� ¼ 4

f ½x0; x1� ¼ �2

f ½x1; x2� ¼ 1

f ½x2; x3� ¼ 2

f ½x0; x1; x2� ¼ 1:5

f ½x1; x2; x3� ¼ 0:5

and the polynomial passing through the support points ð0; 3Þ, ð1; 1Þ, ð2; 2Þ becomes

P2ðxÞ ¼ 3�2xþ 1:5xðx�1Þ

1.7.4Derivatives Evaluation

Let us consider Algorithm 1.9 to evaluate a polynomial prevision with the Newtonform.

The same algorithm can be seen from a different point of view: it allows tomodify thepolynomial parameters ai in order to replace the last support point through thepolynomial prevision in z.

Algorithm 1.10

Input: the polynomial degree n, points x0; x1; . . . ; xn, coefficients of Newton forma0; a1; . . . ; an, and the new point z for replacing xn.

24j 1 Interpolation

Page 25: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Output: the new nþ 1 coefficients a0; a1; . . . ; an of the Newton form.

for i ¼ n�1ðstep�1Þ; 0ai ¼ ai þ aiþ 1ðz�xiÞ

As the algorithm is substantially the same as Algorithm 1.9, it results

PnðzÞ ¼ a0 ð1:43ÞAs a result, the same polynomial (apart from round-off errors) is now expressed as ifthe support points were ðz;PnðzÞÞ; ðx0; y0Þ; . . . ; ðxn�1; yn�1Þ:

PnðxÞ ¼ f ½z� þ f ½z; x0�ðx�zÞþ f ½z; x0; x1�ðx�zÞðx�x0Þþ � � �þ f ½z; x0; x1; . . . ; xn�1�ðx�zÞðx�x0Þðx�x1Þ � � � ðx�xn�2Þ

ð1:44Þ

It is important to realize the difference between the following problems.

1) An authentic new support point is introduced and one of the existing points isdeleted: a new polynomial is obtained and it is far from the original one.

2) As a new support point, the polynomial prevision is adopted in a specific pointand one of the existing support points is deleted: the polynomial is the same asthe original one, but it is presented in a Newton form as though support pointswere changed.

For example, by considering the following support points ð1; 1Þ, ð2; 2Þ, and ð3; 4Þ, thepolynomial in the Newton form is

P2ðxÞ ¼ 1þðx�1Þþ 0:5ðx�1Þðx�2ÞIn x ¼ z ¼ 1:5, the polynomial prevision is P2ðxÞ ¼ 1:375. The polynomial coeffi-cients can be modified in order to write it into the Newton form with the followingpoints: the polynomial prevision z ¼ ð1:5; 1:375Þ and the two previous points ð1; 1Þand ð2; 2Þ.The new polynomial form is

a2 ¼ 0:5

a1 ¼ 1:þ 0:5ð1:5�2Þ ¼ 0:75

a0 ¼ 1:þ 0:75ð1:5�1Þ ¼ 1:375

The same previous polynomial can be rewritten as follows

P2ðxÞ ¼ 1:375þ 0:75ðx�1:5Þþ 0:5ðx�1:5Þðx�1ÞIf Algorithm1.10 is repeated n timeswith ever the samepoint z, the polynomial in thespecial Newton form, where all the support points have the same abscissa z, isobtained.

PnðxÞ ¼ f ½z� þ f ½z; z�ðx�zÞþ f ½z; z; z�ðx�zÞ2 þ � � �þ f ½z; z; . . . ; z�ðx�zÞn ð1:45Þ

This special form corresponds to the standard centered form with z as the center.

1.7 Newton Method j25

Page 26: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

For example, applying the previous algorithm just once results in

a2 ¼ 0:5; a1 ¼ 0:75þ 0:5ð1:5�1:Þ ¼ 1:; a0 ¼ 1:375þ 1ð0Þ ¼ 1:375

P2ðxÞ ¼ 1:375þðx�1:5Þþ 0:5ðx�1:5Þ2

This polynomial form is useful to easily evaluate the derivatives in x ¼ z.

In fact, the ith derivative evaluated in x ¼ z is

PðiÞðzÞ ¼ i!ai ð1:46ÞFor example, writing the polynomial in the form

P2ðxÞ ¼ 1:375þðx�1:5Þþ 0:5ðx�1:5Þ2

results in

P2ð1:5Þ ¼ 1:3755; P02ð1:5Þ ¼ 1:; P00

2 ð1:5Þ ¼ 2ð0:5Þ ¼ 1:

Once again, it is important to underline that a polynomial can assume differentforms: in particular, it can be written in the Newton form, where the support pointsare highlighted, and in the standard centered form, where the derivatives of the pointadopted as center are highlighted.

For example,

P2ðxÞ ¼ 1:þðx�1Þþ 0:5ðx�1Þðx�2ÞP2ðxÞ ¼ 1:375þðx�1:5Þþ 0:5ðx�1:5Þ2

are two different forms of the same polynomial.

The Newton form that employs a single support point shall be very useful in theintegration of differential systems through themultivaluemethods (see Buzzi-Ferrarisand Manenti, 2011b), as it allows to vary the integration step without any trouble.

Actually, although in a generic Newton form the abscissa of all the support points isexplicit, the form (1.45) refers to a single-point z; hence, all the required informationto describe the polynomial is collected on it.

1.8Neville Algorithm

The development of the aforementioned algorithms consists of two phases: first, thecoefficients of the interpolating polynomial are evaluated, and second, a prevision inthe assigned z is carried out.

To get only a prevision in correspondencewith an assigned abscissa z, one does notrequire to evaluate the polynomial coefficients by simply adopting one of thetechniques described above.

Let us denote by Pkðz; x0; x1; . . . ; xkÞ the prevision achieved with the k-degreepolynomial Pkðx; x0; x1; . . . ; xkÞ passing through ðx0; y0Þ, ðx1; y1Þ, . . . , ðxk; ykÞ.Relation (1.37), which allows to build a new k-degree polynomial starting from twoðk�1Þ-degree polynomials, can be used not only to get a new polynomial but also to

26j 1 Interpolation

Page 27: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

get the prevision in x ¼ z for a k-degree polynomial, starting from the previsionsobtained by two ðk�1Þ-degree polynomials.

In fact, it is sufficient to write relation (1.37) by using z rather than x to get

Pkðz; x0; x1; . . . ; xkÞ ¼ ðz�x0ÞPk�1ðz; x1; . . . ; xkÞ�ðz�xkÞPk�1ðz; x0; . . . ; xk�1Þxk�x0

ð1:47Þ

The same rules described for f ½x0; x1; . . . ; xk� are valid for Pkðz; x0; x1; . . . ; xkÞ too,and it is useful, in particular, to adopt the scheme proposed by Neville against theother alternatives (i.e., Aitken method).

The following sequence, exemplified for n ¼ 3, allows to evaluatePkðz; x0; x1; . . . ; xkÞ by using the Neville method.

x0

x1

x2

x3

y0 ¼ P0ðz; x0Þy1 ¼ P0ðz; x1Þy2 ¼ P0ðz; x2Þy3 ¼ P0ðz; x3Þ

P1ðz; x0; x1ÞP1ðz; x1; x2ÞP1ðz; x2; x3Þ

P2ðz; x0; x1; x2ÞP2ðz; x1; x2; x3Þ

P3ðz; x0; x1; x2; x3Þ

For example, the scheme to evaluate the prevision in z ¼ 1:5 obtained by apolynomial interpolation passing through ð1; 1Þ, ð2; 2Þ, and ð3; 4Þ is

x0 ¼ 1

x1 ¼ 2

x2 ¼ 3

P0 ¼ 1

P0 ¼ 2

P0 ¼ 4

P1 ¼ :5ð2Þþ :5ð1Þ ¼ 1:5

P1 ¼ �:5ð4Þþ1:5ð2Þ ¼ 1:P2 ¼ ð:5ð1Þþ1:5ð1:5ÞÞ

2¼ 1:375

Algorithm 1.11 Prevision with the Neville method

Input: the polynomial degree n, abscissas x0; x1; . . . ; xn, ordinates y0; y1; . . . ; yn, andthe new point z where the prevision is to be evaluated.Output: previsions p0; p1; . . . ; pn obtained with polynomials from 0 to n.

for i ¼ 0ðstep 1Þ;npi ¼ yi

for k ¼ 1ðstep 1Þ;nfor i ¼ n ðstep�1Þ; kpi ¼ pi þðz�xiÞðpi�pi�1Þ=ðxi�xi�kÞ

Algorithm 1.11 requires nðnþ 1Þ flops.Thealgorithmprovides theprevisions obtainablewithgrowing-orderpolynomials and,therefore, it is possible to calculate the error generated by excluding a polynomial term.In the BzzInterpolation class, the function Neville evaluates polynomialprevisions through the Neville method. This function returns the previsions obtain-able with growing-order polynomials.

1.8 Neville Algorithm j27

Page 28: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Example 1.4

Let x ¼ f1:; 1:3; 1:6; 1:9; 2:2g be the abscissas for the function yi ¼ e�x2i . Evaluatethe Neville previsions in z ¼ f1:8; 1:65; 1:35g.The program is

#include ‘‘BzzMath.hpp’’

void main(void)

{

BzzPrint(‘‘\n\nNeville’’);

BzzVector x(5,1.,1.3,1.6,1.9,2.2);

BzzVector y(5);

for(int i = 1;i <= 5;i++)

y[i] = exp(-x[i]*x[i]);

BzzInterpolation p(x,y);

BzzVector prev;

prev = p.Neville(1.8);

prev.BzzPrint(‘‘Neville Previsions’’);

BzzPause();

prev = p.Neville(1.65);

prev.BzzPrint(‘‘Neville Previsions’’);

BzzPause();

prev = p.Neville(1.35);

prev.BzzPrint(‘‘Neville Previsions’’);

BzzPause();

}

It is worth to remark that the Neville function returns a BzzVector, whereasboth the Newton and the Lagrange functions return a double.

To solve this problem, as one knows the point x ¼ z where the prevision is to beevaluated, it is suitable to introduce an ordered series of points by basing on theirdistance from z. In this way, the round-off errors are minimized.

Many variants of the Neville algorithm were proposed.

1) The first modification was introduced to minimize the round-off errors. It isnecessary to operate on the differences between two previsions rather than onthe same previsions (Stoer and Bulirsch, 1983).

2) Another implementation type allows to sequentially obtain the previsions ofincreasing-order polynomials. It allows to stop calculations at any intermediate-order polynomial (Schwarz, 1989).

3) As a prevision in z ¼ 0 is often required, it is possible to specialize the algorithmfor this specific case.

Neville-type algorithms allow to obtain a previsionwithout evaluating the polynomialcoefficients; unfortunately, this entails the following disadvantages.

28j 1 Interpolation

Page 29: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

1) The algorithms cannot be used where the expression of the interpolatingpolynomial is explicitly required.

2) They are not performing if the same support points are used formore previsions.

Even though itmay be curious, any version of this kind of algorithm requires a highernumber of calculations than a prevision obtained through the aprioristic evaluationof the coefficients in the Newton form. The computational time is only equivalentwhen the previsions obtained with intermediate-order polynomials are needed. Insuch cases, it is, however, possible to implement a special algorithm (Conte andDe Boor, 1980), which requires a smaller computational time even evaluating thecoefficients of the Newton form.The advantages of these algorithms are the following.

1) They are very simple to implement and use.2) It is possible to write versions slightly sensitive to round-off errors.3) They are a useful introduction to the (similar) algorithms dealing with rational

functions (described in the following).

1.9Hermite Polynomial Interpolation

So far, we have considered the situation with nþ 1 points ðxi; yiÞ only.Knowing also the values of any derivative in some points seems logical to exploit

this information to build the polynomial function.For example, if one knows the values y0 ¼ 1; y00 ¼ 2; y000 ¼ 3 in correspondence

with x0 ¼ 0, the parameters of the polynomial P2ðxÞ ¼ a0 þ a1xþ a2x2 are imme-diately obtained by imposing the following conditions:

a0 ¼ 1; a1 ¼ 2; 2a2 ¼ 3

Although in the exact interpolation there is certainty about the interpolatingpolynomial existence using nþ 1 points (when all abscissas are different), it is notanymore true in the general case where even the derivatives are assigned.

For example, given the value of a function in x0 together with the values of itssecond derivatives in x0 and x1, the system

a0 þ a1x0 þ a2x20 ¼ y02a2 ¼ y0002a2 ¼ y001

has the matrix with null determinant.

An important case where there is certainty of the interpolating polynomial existenceand the uniqueness concerns the development of a polynomial passing throughsome support points and with the first derivative values assigned in correspondencewith the same abscissas.

1.9 Hermite Polynomial Interpolation j29

Page 30: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

The polynomialH2nþ 1ðxÞ that assumes the assigned value yi and, at the same time,has y0i asfirst derivative in correspondencewith the abscissas xiði ¼ 0; . . . ; nÞ is calledthe Hermite polynomial.

It is possible to adopt a technique either similar to the Lagrange one (Lagrange-type) or to the Newton one (Newton-type) to evaluate Hermite polynomial.

1.9.1Lagrange-Type Method

Hermite polynomial can bewritten as the sumof 2nþ 2 functions (Hamming, 1962):

H2nþ 1ðxÞ ¼Xni¼0

½ yigiðxÞþ y0ihiðxÞ� ð1:48Þ

Functions giðxÞ and hiðxÞ are obtained by imposing the following conditions:

giðxiÞ ¼ 1 ð1:49Þ

hiðxiÞ ¼ 0 ð1:50Þ

g 0iðxiÞ ¼ 0 ð1:51Þ

h0iðxiÞ ¼ 1 ð1:52Þ

giðxjÞ ¼ 0 ði 6¼ jÞ ð1:53Þ

hiðxjÞ ¼ 0 ði 6¼ jÞ ð1:54Þ

g 0iðxjÞ ¼ 0 ði 6¼ jÞ ð1:55Þ

h0iðxjÞ ¼ 0 ði 6¼ jÞ ð1:56ÞUnder these conditions, the following functions are obtained:

giðxÞ ¼ ðx�x0Þ2 � � � ðx�xi�1Þ2ðaixþ biÞðx�xiþ 1Þ2 � � � ðx�xnÞ2ðxi�x0Þ2 � � � ðxi�xi�1Þ2ðxi�xiþ 1Þ2 � � � ðxi�xnÞ2

ð1:57Þ

ai ¼ 2x0�xi

þ � � � þ 2xi�1�xi

þ 2xiþ 1�xi

þ � � � þ 2xn�xi

ð1:58Þ

bi ¼ 1�aixi ð1:59Þ

hiðxÞ ¼ ðx�x0Þ2 � � � ðx�xi�1Þ2ðx�xiÞðx�xiþ 1Þ2 � � � ðx�xnÞ2ðxi�x0Þ2 � � � ðxi�xi�1Þ2ðxi�xiþ 1Þ2 � � � ðxi�xnÞ2

ð1:60Þ

30j 1 Interpolation

Page 31: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

For example, to build a 3-degree Hermite polynomial satisfying the conditionsð0; y0Þ, ð1; y1Þ, ð0; y00Þ, and ð1; y01Þ, one has

a0 ¼ 2; b0 ¼ 1; a1 ¼ �2; b1 ¼ 3

Hence,

H3 ¼ y0ðx�1Þ2ð2xþ 1Þþ y00ðx�1Þ2xþ y1x2ð3�2xÞþ y01x

2ðx�1ÞIf the conditions are ðz0; y0Þ, ðz1; y1Þ, ðz0; y00Þ, and ðz1; y01Þ, it is possible to use the

same polynomial after the following scale exchange:

x ¼ ðz�z0Þ=ðz1�z0Þ

1.9.2Newton-Type Method

To use the Newton method when the values of ordinates, first, second, and higherderivatives are known, it is sufficient to observe the following rules.

1) Repeat the point as many times as the assigned derivatives.2) Replace rational differences with the corresponding derivatives divided by k!,

with k being the derivative order.

To build a 3-degreeHermite polynomial, where the first derivatives are assigned incorrespondence with the support points, the procedure is

x0x0x1x1

y0 ¼ f ½x0�y0 ¼ f ½x0�y1 ¼ f ½x1�y1 ¼ f ½x1�

f ½x0; x0� ¼ y00f ½x0; x1�

f ½x1; x1� ¼ y01

f ½x0; x0; x1�f ½x0; x1; x1�

f ½x0; x0; x1; x1�

The corresponding polynomial is

P3 ¼ f ½x0� þ f ½x0; x0�ðx�x0Þþ f ½x0; x0; x1�ðx�x0Þðx�x0Þþ f ½x0; x0; x1; x1�ðx�x0Þðx�x0Þðx�x1Þ

ð1:61Þ

where

f ½x0� ¼ y0

f ½x0; x0� ¼ y00

f x0; x0; x1½ � ¼y1�y0x1�x0

�y00x1�x0

f x0; x0; x1; x1½ � ¼y00 þ y01�2

y1�y0x1�x0

ðx1�x0Þ2

1.9 Hermite Polynomial Interpolation j31

Page 32: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

As it can be observed, the technique is equal to the one adopted to build thepolynomial by the Newton method, with the shrewdness to see the support pointwhere the derivative is known as a distinct point.

For example, to build a 3-degree Hermite polynomial satisfying the conditionsðx0 ¼ 0; y0 ¼ 1Þ, ðx1 ¼ 1; y1 ¼ 0Þ, ðx0 ¼ 0; y00 ¼ 0Þ, and ðx1 ¼ 1; y01 ¼ �1Þ, the fol-lowing takes place:

f ½x0� ¼ y0 ¼ 1

f ½x0; x0� ¼ y00 ¼ 0

f ½x0; x0; x1� ¼ �1

f ½x0; x0; x1; x1� ¼ 1

Hence,

P3ðxÞ ¼ 1�x2 þ x2ðx�1ÞThrough this technique, it is possible to build the polynomial even when in some

points the first derivative is unknown.Given two points ð1; 1Þ and ð2; 2Þ and the first derivative y0ð1Þ ¼ 0:5, the following

scheme is obtained:

x0 ¼ 1

x0 ¼ 1

x1 ¼ 2

f ½x0� ¼ 1

f ½x0� ¼ 1

f ½x1� ¼ 2

f ½x0; x0� ¼ y00 ¼ 0:5

f ½x0; x1� ¼ 1f ½x0; x0; x1� ¼ 0:5

Therefore,

P2ðxÞ ¼ 1þ 0:5ðx�1Þþ 0:5ðx�1Þ2

If the values of the function and its first and second derivatives are known in twopoints x0 and x1, one can write the 5-degree polynomial in the following form:

P5ðxÞ ¼ y0 þ y00ðx�x0Þþ y0002ðx�x0Þ2 þ f x0; x0; x0; x1½ �ðx�x0Þ3

þ f ½x0; x0; x0; x1; x1�ðx�x0Þ3ðx�x1Þþ f ½x0; x0; x0; x1; x1; x1�ðx�x0Þ3ðx�x1Þ2

ð1:62Þ

The required scheme to calculate the coefficients is

x0x0x0x1x1x1

y0y0y0y1y1y1

y00y00

f ½x0;x1�y01y01

y000=2f ½x0;x0;x1�f ½x0;x1;x1�

y001=2

f ½x0;x0;x0;x1�f ½x0;x0;x1;x1�f ½x0;x1;x1;x1�

f ½x0;x0;x0;x1;x1�f ½x0;x0;x1;x1;x1� f ½x0;x0;x0;x1;x1;x1�

It is possible to solve even intermediate problems where ordinates and derivativesare known in correspondence with some abscissas.

32j 1 Interpolation

Page 33: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

For example, given x0 ¼ 1; y0 ¼ 1; x1 ¼ 2; y1 ¼ 3; y01 ¼ 1; y001 ¼ 8 the followingscheme can be developed:

x0 ¼ 1x1 ¼ 2x1 ¼ 2x1 ¼ 2

y0 ¼ 1y1 ¼ 3y1 ¼ 3y1 ¼ 3

f ½x0;x1� ¼ 2f ½x1;x1� ¼ y01 ¼ 1f ½x1;x1� ¼ y01 ¼ 1

f ½x0;x1;x1� ¼ 1f ½x1;x1;x1� ¼ y001=2¼ 4

f ½x0;x1;x1;x1� ¼ 3

and we get

P3ðxÞ ¼ 1þ2ðx�1Þþðx�1Þðx�2Þþ3ðx�1Þðx�2Þ2

1.10Interpolation with Rational Functions

A function obtained as ratio between two polynomials is called rational:

Rn;mðxÞ ¼ PnðxÞPmðxÞ ¼

a0 þ a1xþ � � � þ anxn

b0 þ b1xþ � � � þ bmxmð1:63Þ

The number of the required parameters to univocally identify the rational functionRn;m is equal to nþmþ 1 (being b0 arbitrary).

The standard form for rational functions is

Rn;mðxÞ ¼ PnðxÞPmðxÞ ¼

a0 þ a1xþ � � � þ anxn

1þ b1xþ � � � þ bmxmð1:64Þ

If the np ¼ nþmþ 1 support points ðxi; yiÞ with 0; . . . ; nþm are known, para-meters can be evaluated by solving the system

Rn;mðxiÞ ¼ yi ði ¼ 0; 1; . . . ; nþmÞ ð1:65ÞThis system can be linearized bymultiplying both the terms by the denominator of

the rational function:

a0 þ a1xi þ � � � þ anxni ¼ ð1þ b1xi þ � � � þ bmxmi Þyiði ¼ 0; 1; . . . ; nþmÞ ð1:66Þ

As the condition (1.66) is necessary to solve the system (1.65), one could think thatsystem (1.65) is automatically solved by solving the system (1.66). This should be trueif condition (1.66) should be sufficient, besides a necessary condition, but this is notalways true. It is worth noting that we moved from the system (1.65) to (1.66) bymultiplying the former one by the denominator of the rational function.

Be careful: by multiplying both the right- and the left-hand side of an equation by agiven expression, the results become arbitrary if the same expression is equal to zero.

If the denominator of rational function (1.65) is null in one ormore points, the left-hand side of system (1.66) also has to be zero. In this case, the system solution doesnot satisfy the same system (1.65).

1.10 Interpolation with Rational Functions j33

Page 34: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

In other words, the polynomials PnðxÞ and PmðxÞ that satisfy the system (1.66) canbe written in the roots-product form. If one or more polynomial roots both atthe denominator and at the numerator coincide with an abscissa, those terms areremoved from the rational function, these being the common terms. In thesecircumstances, equation (1.65) is unsatisfied in with respect to that specific supportpoint.

For example, given the points ð�1; 1Þ, ð1; 2Þ, and ð2; 2Þ, system (1.66) for therational function R1;1 is

a0�a1 ¼ 1�b1

a0 þ a1 ¼ 2þ 2b1

a0 þ 2a1 ¼ 2þ 4b1

Hence,

a0 ¼ 2; a1 ¼ 2; b1 ¼ 1

R1;1 ¼ 2þ 2x1þ x

¼ 2

As a result, the rational function R1;1 does not pass through the point ð�1; 1Þ.The rational approximation presents another shortcoming when the denominator

is equal to zero in the selected interval.Let us consider the points ð0; 0Þ, ð0:5;�1Þ, and ð2; 2Þ. The rational function

R1;1 ¼ xx�1

satisfies the support points but in x ¼ 1 (just inside the interval). R1;1 !1.Therefore, the use of rational functions may face the following problems.

1) The rational function exactly passing through those specific support points maynot exist.

2) The denominator could be zero in the selected interval.3) Rational functions are more difficult to manipulate in case of integrations or

differentiations.

On the other hand, rational functions are considerably better than polynomials inapproximating functions, especially for their feature to simulate even situations withasymptotical behaviors.

Two techniques shall be analyzed in the following:

1) A technique to evaluate rational function parameters and, once they are obtained,to provide an estimate in correspondence with an assigned abscissa x ¼ z.

2) A technique to directly obtain the prevision without the need to evaluateparameters.

Both the techniques are similar to Newton and Neville methods already explainedfor polynomials.

34j 1 Interpolation

Page 35: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Only a specific class of rational functions shall be considered here: the one where thenumerator and the denominator differ at most by one order.

1.10.1Thiele�s Continuous Fractions

Thiele�s continuous fractions allow to sequentially build a rational function with thenumerator and the denominator polynomials that differ at most by one order.

The following discussion is valid under the hypothesis that the rational functionexactly passes through the given np support points. Usually, this hypothesis isverified, but it will be appropriate to check it in all programs that use rationalfunctions.

The following are called Thiele�s continuous fractions:

R0;0 ¼ c0 ð1:67Þ

R1;0 ¼ c0 þ x�x0c1

ð1:68Þ

R1;1 ¼ c0 þ x�x0

c1 þ x�x1c2

ð1:69Þ

R2;1 ¼ c0 þ x�x0

c1 þ x�x1

c2 þ x�x2c3

� � �ð1:70Þ

Thiele�s continuous fractions are a series of rational functions where both thenumerator and the denominator degrees are alternatively increased by one. It isworth noting that Thiele�s series represents a particular case of continuous fractions.

In general, they are

f ðxÞ ¼ b0 þ a1

b1 þ a2

b2 þ a3

b3 þ a4b4 þ � � �

ð1:71Þ

where the coefficients ai and bi can be any known function of the variable x.

In these cases, the problem to find out a criterion for detecting which terms are tobe introduced in the series can arise. Looking at equation (1.71), one could think itwould be necessary to solve the problem by attemps: fixing the amount of terms,solving the problem, increasing the amount of terms, and repeating the procedure. Inother words, it would seem impossible to build the series from left to right.

1.10 Interpolation with Rational Functions j35

Page 36: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

One can avoid trial-and-error method by developing the series from left to right andby using two auxiliary vectors sk, qk and the following iterative formulae:

s0 ¼ b0; q0 ¼ 1

s1 ¼ b1 � b0 þ a1; q1 ¼ b1sk ¼ bk � sk�1 þ ak � sk�2; qk ¼ bk � qk�1 þ ak � qk�2; ðk ¼ 2; 3; . . .Þ

The ratio

fk ¼ skqk

provides the fraction value.

During the sequence, it might happen that the values of sk and qk progressivelyincrease or decrease. In these circumstances, there is a risk of overflow and underflow.

As one is interested in the ratio between sk and qk and not in their specific values andas both sk and qk are linear combinations of the previous terms, it is enough to dividesk�1, qk�1, sk, and qk by qk to stop any increase or decrease in their values.

In such a specific case, the following algorithm is needed when parameters ci havealready been calculated.

Algorithm 1.12 Previsions with Thiele

Input: number of support points np, abscissas x0; x1; . . . ; xnp�1, abscissa of theprevision x ¼ z, and Thiele coefficients c0; c1; . . . ; cnp�1.Output: previsions p0; p1; . . . ; pnp�1 and prevision differences dp0; dp1; . . . ; dpnp�2.

BIG ¼ 1000: TINY ¼ :001

s0 ¼ c0; q0 ¼ 1:; p0 ¼ s0

s1 ¼ c0c1 þðz�x0Þ; q1 ¼ c1

if ðq1 ¼ 0:Þquitp1 ¼ s1=q1for k ¼ 2ðstep 1Þ; np�1

aus ¼ z�xk�1

sk ¼ ck sk�1 þ aus sk�2

qk ¼ ck qk�1 þ aus qk�2

if ðqk ¼ 0:Þquitpk ¼ sk=qkif ð skj j>BIG and qk

�� ��>BIG or skj j < TINY and qk�� �� < TINYÞ

sk�1 ¼ sk�1=qk; qk�1 ¼ qk�1=qksk ¼ pk; qk ¼ 1:

for k ¼ 1ðstep 1Þ; np�1

dpk�1 ¼ pk�pk�1

36j 1 Interpolation

Page 37: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

For example, given the points ð1; 1Þ, ð2; 2Þ, and ð3; 4Þ and the coefficients c0 ¼ 1,c1 ¼ 1, and c2 ¼ �3, the previsions in z ¼ 1:5 are

p0 ¼ 1s1 ¼ 1 � 1þð1:5�1Þ ¼ 1:5q1 ¼ 1p1 ¼ 1:5s2 ¼ �3 � 1:5þð1:5�2Þ � 1 ¼ �5;q2 ¼ �3 � 1þð1:5�2Þ � 1 ¼ �3:5p2 ¼ 1:4285714

Suppose to select the abscissas

x ¼ f0:1; 0:2; 0:4; 0:8; 1:2gfor the function coshðxÞ. In correspondence with these points, the followingordinates are obtained:

y ¼ f1:005004; 1:020067; 1:081072; 1:337435; 1:810656gThe value of the function coshðxÞ in z ¼ :25 is coshð:25Þ ¼ 1:031413. When weassign the coefficients

c ¼ f1:005004; 6:638973;�0:074208;�6:879100;�7:636977gthe previsions in z ¼ :25 are

p ¼ f1:005004; 1:027598; 1:030150; 1:031390; 1:031410gand the error is

dp ¼ f0:022594; 0:002552; 0:001240; 0:000020gThe coefficients ci necessary for interpolation with Thiele�s rational functions iscalculated by the following inverse rational differences.

1) Zero-degree inverse rational difference:

g0ðxiÞ ¼ y0 ði ¼ 0; . . . ; np�1Þ ð1:72Þ2) 1-degree inverse rational difference:

g1ðxi; x0Þ ¼ xi�x0g0ðxiÞ�g0ðx0Þ ði ¼ 1; . . . ; np�1Þ ð1:73Þ

3) 2-degree inverse rational difference:

g2ðxi; x1; x0Þ ¼ xi�x1g1ðxi; x0Þ�g1ðx1; x0Þ ði ¼ 2; . . . ; np�1Þ ð1:74Þ

4) k-degree inverse rational difference ðk ¼ 2; . . . ; np�1Þ, ði ¼ k; . . . ; np�1Þ:

gkðxi; xk�1; . . . ; x1; x0Þ ¼ xi�xk�1

gk�1ðxi; xk�2; . . . ; x0Þ�gk�1ðxk�1; xk�2; . . . ; x0Þð1:75Þ

1.10 Interpolation with Rational Functions j37

Page 38: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

For example, given the points ð1; 1Þ, ð2; 2Þ, and ð3; 4Þ, the scheme of inverse rationaldifferences is

x0 ¼ 1x1 ¼ 2x2 ¼ 3

y0 ¼ g0ðx0Þ ¼ 1y1 ¼ g0ðx1Þ ¼ 2y2 ¼ g0ðx2Þ ¼ 4

g1ðx1; x0Þ ¼ 2�12�1

¼ 1

g1ðx2; x0Þ ¼ 3�14�1

¼ 23

g2ðx2;x1; x0Þ ¼ 3�223�1

¼ �3

Once the scheme is developed, the values of parameters ck are on the upper diagonalof the same scheme:

c0 ¼ g0ðx0Þ ð1:76Þck ¼ gkðxk; xk�1; . . . ; x1; x0Þ ðk ¼ 1; 2; . . .Þ ð1:77Þ

As per rational differences in the Newton form, it is not necessary to store the overallscheme of the inverse rational differences: coefficients ci are enough.

Algorithm 1.13 Coefficients in Thiele�s form

Input: number of support points np, abscissas x0; x1; . . . ; xnp�1, and ordinatesy0; y1; . . . ; ynp�1.Output: coefficients in Thiele�s form c0; c1; . . . ; cnp�1.

for i ¼ 0ðstep 1Þ;np�1

aus ¼ yij ¼ 0

for k ¼ 1ðstep 1Þ; k ¼ i

j ¼ jþ 1

den ¼ aus�cjif ðden ¼ 0Þquitaus ¼ ðxi�xjÞ=den

ci ¼ aus

It is important to prevent any possibility to have two equal values

ck�1ðxi; xk�2; . . . ; x0Þ ¼ ck�1ðxk�1; xk�2; . . . ; x0Þ ð1:78Þas it would take to a division by zero.For example, let x ¼ f0:1; 0:2; 0:4; 0:8; 1:2g be the values of abscissas for the functioncoshðxÞ. In these points, the ordinates are

y ¼ f1:005004; 1:020067; 1:081072; 1:337435; 1:810656gThe coefficients are

c ¼ f1:005004; 6:638973;�0:074208;�6:879100;�7:636977gIn the BzzInterpolation class, the function Rational evaluates rationalprevisions through the Thiele method. This function returns the previsions obtain-able with growing-order rational functions.

38j 1 Interpolation

Page 39: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Example 1.5

Let x ¼ f1:; 1:3; 1:6; 1:9; 2:2g be the abscissas for the function yi ¼ e�x2i . EvaluateThiele previsions in z ¼ f1:8; 1:65; 1:35g.The program is

#include ‘‘BzzMath.hpp’’

void main(void)

{

BzzPrint(‘‘\n\nRational’’);

BzzVector x(5,1.,1.3,1.6,1.9,2.2);

BzzVector y(5);

for(int i = 1;i <= 5;i++)

y[i] = exp(-x[i]*x[i]);

BzzInterpolation p(x,y);

double prev;

prev = p.Rational(1.8);

BzzPrint(‘‘\nRational Previsions %e’’,prev);

BzzPause();

prev = p.Rational(1.65);

BzzPrint(‘‘\nRational Previsions %e’’,prev);

BzzPause();

prev = p.Rational(1.35);

BzzPrint(‘‘\nRational Previsions %e’’,prev);

BzzPause();

}

Observe that the function Rational returns a double.

1.10.2Bulirsch–Stoer Method

Stoer and Bulirsch (1983) proposed an algorithm similar to the Neville procedure,which is valid for a sequence of polynomials and is applicable to a series of rationalfunctions. By increasing the number of points, the polynomial degree of both thedenominator and the numerator of the rational function is alternatively increased,too, so as to preserve the denominator degree ever equal or one-order higher than thenumerator degree.

In Neville algorithm (1.11), the recursive formula to evaluate the polynomialprevision can be rewritten as follows:

k ¼ 1; . . . ; n

pi ¼ pi þ pi�pi�1z�xi�k

z�xi�1

i ¼ n; . . . ; k ð1:79Þ

1.10 Interpolation with Rational Functions j39

Page 40: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Bulirsch and Stoer demonstrated that expression (1.79) for a rational function is tobe modified in

ti ¼ pi ð1:80Þ

pi ¼ pi þ pi�pi�1

z�xi�k

z�xi1� pi�pi�1

pi�ti�1

� ��1

ð1:81Þ

where ti is initialized equal to zero.

Algorithm 1.14 Prevision with Bulirsch–Stoer

Input: number of support points np, abscissas x0; x1; . . . ; xnp�1, ordinates y0; y1; . . . ;ynp�1, and prevision z.Output: previsions p0; p1; . . . ; pnp�1 and prevision differences dp0; dp1; . . . ; dpnp�2.

for i ¼ 0ðstep 1Þ;np�1

ti ¼ 0:; pi ¼ yi

for k ¼ 1ðstep 1Þ;np�1

for i ¼ np�1ðstep�1Þ; kti ¼ pi

pi ¼ pi þpi�pi�1

z�xi�k

z�xi1� pi�pi�1

pi�ti�1

24

35�1

for i ¼ 0ðstep 1Þ;np�2

dpi ¼ piþ 1�pi

As per theNevillemethod, some specific variants of the previous algorithm exist and,in particular, an optimized version for round-off errors, another one for the iterativeapplications, and some others for the case of z ¼ 0 are significantly useful.In the following examples, the Neville algorithm valid for a polynomial prevision iscompared to the Bulirsch–Stoer algorithm, which uses a rational function.

Example 1.6

Let x ¼ f1:; 1:3; 1:6; 1:9; 2:2g be abscissas for the function expð�x2Þ. The ordinatesy ¼ f0:367879; 0:184520; 0:077305; 0:027052; 0:007907g are obtained in correspon-dence with these points. Compare Neville and Bulirsch–Stoer algorithms.The program is

#include ‘‘BzzMath.hpp’’

void main(void)

{

40j 1 Interpolation

Page 41: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

BzzPrint(‘‘\n\nComparison: Neville and

BulirschStoer’’);

BzzVector x(5,1.,1.3,1.6,1.9,2.2);

BzzVector y(5);

for(int i = 1;i <= 5;i++)

y[i] = exp(-x[i]*x[i]);

BzzInterpolation p(x,y);

BzzVector prev;

prev = p.Neville(1.8);

prev.BzzPrint(‘‘Neville Previsions’’);

prev = p.BulirschStoer(1.8);

prev.BzzPrint(‘‘BulirschStoer Previsions’’);

BzzPause();

prev = p.Neville(2.1);

prev.BzzPrint(‘‘Neville Previsions’’);

prev = p.BulirschStoer(2.1);

prev.BzzPrint(‘‘BulirschStoer Previsions’’);

BzzPause();

prev = p.Neville(1.35);

prev.BzzPrint(‘‘Neville Previsions’’);

prev = p.BulirschStoer(1.35);

prev.BzzPrint(‘‘BulirschStoer Previsions’’);

BzzPause();

}

The value of the function expð�x2Þ in z ¼ 1:8 is f ðzÞ ¼ 0:039164.With the Neville algorithm,

p ¼ f0:027052; 0:043803; 0:040346; 0:039070; 0:038932gdp ¼ f0:016751;�0:003456;�0:001277;�0:000137g

whereas the Bulirsch–Stoer algorithm takes to

p ¼ f0:027052; 0:034535; 0:037918; 0:038734; 0:039256gdp ¼ f0:007483; 0:003382; 0:000816; 0:000522g

The errors are equal to 0:59 and 0:23% for Neville and Bulirsch–Stoer, respectively.The function value in z ¼ 2:1 is f ðzÞ ¼ 0:012155.Using the Neville algorithm,

p ¼ f0:007907; 0:014289; 0:010832; 0:012428; 0:012703gdp ¼ f0:006382;�0:003456; 0:001596; 0:000275g

whereas the Bulirsch–Stoer algorithm takes to

p ¼ f0:007907; 0:010348; 0:012820; 0:012446; 0:012017gdp ¼ f0:002441; 0:002472;�0:000374;�0:000429g

The errors are equal to 4:5 and 1:1% for Neville and Bulirsch–Stoer, respectively.

1.10 Interpolation with Rational Functions j41

Page 42: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Another example was proposed by Schwarz (1989). Let x ¼ f:01; :02; :03; :04; :05g bethe abscissas for the function f ðxÞ ¼ 0:1

xffiffiffiffiffiffiffiffiffiffiffiffiffi5xþ 1

p . The ordinates

y ¼ f9:759001; 4:767313; 3:108349; 2:282177; 1:788854g

are obtained in correspondence with these points.The value of the function f ðxÞ in z ¼ 0:024000 is f ðzÞ ¼ 3:937130.With the Neville algorithm,

p ¼ f4:767313; 4:103727; 3:703800; 3:843796; 3:888596gdp ¼ f�0:663586;�0:399927; 0:139996; 0:044800g

whereas the Bulirsch–Stoer algorithm takes the form

p ¼ f4:767313; 3:928613; 3:937524; 3:937132; 3:937130gdp ¼ f�0:838700; 0:008910;�0:000391;�0:000002g

The errors are equal to 1:23 and 0% for Neville and Bulirsch–Stoer, respectively.Finally, Stoer and Bulirsch (1983) proposed the following example: Letx ¼ f1:; 2:; 3:; 4:; 5:g be the abscissas for the function cotgðxÞ. The ordinatesy ¼ f57:289963; 28:636253; 19:081137; 14:300666; 11:430053g are obtained in cor-respondence with these points.The value of the function f ðxÞ in z ¼ 2:5 is f ðzÞ ¼ 22:9037655.With the Neville algorithm,

p ¼ f28:636253; 23:858696; 21:471373; 22:366619; 22:635193gdp ¼ f�4:777557;�2:387323; 0:895247; 0:268574g

whereas the Bulirsch–Stoer algorithm takes the form

p ¼ f28:636253; 22:902018; 22:903416; 22:903696; 22:903767gdp ¼ f�5:734236; 0:001398; 0:000280; 0:000071g

The errors are equal to 1:17% for the Neville algorithm, whereas only the eighth digitis wrong for the Bulirsch–Stoer algorithm.

Whenever the function has an asymptote, the interpolation through rational func-tions is better.

1.11Inverse Interpolation

In some circumstances, it may be appropriate to invert the role of variables, so as touse the variable x as dependent and the variable y as independent. By doing so,an interpolation of the variable x against y could be feasible.

42j 1 Interpolation

Page 43: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

For example, given the points ð1; 1Þ, ð2; 2Þ, and ð3; 4Þ, the 2-degree polynomialin the Newton form, where y is the independent variable and x is the dependentvariable, is

P2ðyÞ ¼ 1þðy�1Þ�ðy�1Þðy�2Þ=6The inverse interpolation is useful particularly when there is the need to estimate thevalue of the variable x in correspondence with an assigned value of y.

In the previous example, the inverse polynomial interpolation can be adopted toestimate the value of x where y ¼ 2:1:

P2ðy ¼ 2:1Þ ¼ 1þð2:1�1Þ�ð2:1�1Þð2:1�2Þ=6Hence,

x ¼ P2ðy ¼ 2:1Þ ¼ 2:08166667

The inverse interpolation is very important to solve the following problems.

1) Function zeroing (see Buzzi-Ferraris and Manenti, 2011a).2) Extrapolation to zero in iterative processes (see Buzzi-Ferraris and Manenti,

2010a).

In these problems, there is a series of points ðxi; yiÞ and we want to know the valueof xwhere y ¼ 0.Using a direct interpolation, the problem is not easy to resolvewhenpolynomials have a degree higher than 2 or with rational functions. Conversely, byexecuting an inverse polynomial, there are no difficulties in evaluating the value of xin correspondence with y ¼ 0.

In this case, some specific versions of the Neville algorithm (for the polynomialinterpolation) and, a fortiori, of the Bulirsch–Stoer algorithm (for the interpolationwith rational functions) are useful to make a prevision in z ¼ 0.

Example 1.7

Let x ¼ f1:3; 1:4; 1:5; 1:6; 1:7g be the abscissas for the function sinh ðxÞ�2. Evaluatethe value of x that zeroes the function.The program is

#include ‘‘BzzMath.hpp’’

void main(void)

{

BzzPrint(‘‘\n\nPrevisions in zero’’);

BzzVector x(1.3;1.4;1.5;1.6;1.7);

BzzVector y(5);

for(int i = 1;i <= 5;i++)

y[i] = sinh(x[i])-2;

BzzInterpolation p(y,x);

BzzVector prev;

1.11 Inverse Interpolation j43

Page 44: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

prev = p.Neville(0.);

prev.BzzPrint(‘‘Neville Previsions’’);

prev = p.BulirschStoer(0.);

prev.BzzPrint(‘‘BulirschStoer Previsions’’);

BzzPause();

}

1.12Successive Polynomial Interpolation

As already discussed, it is unseemly to use a unique high-degree polynomial tointerpolate numerous points, as the interpolating polynomial usually goes away fromthe interpolated function when the polynomial degree is increased.

With many support points, it is suitable both to use interpolating polynomialsthat employ only a portion of the existing data and to employ some strategies toconnect them.

For example, let us consider the simplest case of a series of 1-degree polynomialsinterpolating two adjacent support points.

In this context, the following nomenclature is adopted: the n support points ðxi; yiÞhave index i ¼ 1; . . . ; n; hi is the distance between the abscissas xi and xiþ 1.Moreover, the interpolating polynomial between the point xi and xiþ 1 has index i.

The 1-degree polynomial series is

LiðxÞ ¼ yiþ 1x�xihi

�yix�xiþ 1

hixi � x � xiþ 1 ð1:82Þ

For example, given the points ð1:; 2:Þ, ð2:; 5:Þ, and ð3:; 7:Þ, the 1-degreepolynomials are

L1ðxÞ ¼ 5ðx�1Þ�2ðx�2Þ; L2ðxÞ ¼ 7ðx�2Þ�5ðx�3ÞA series of 1-degree polynomials solve the problem of interpolation divergence whilethe number of support points increases.

A series of 1-degree polynomials have discontinuous derivatives in correspondencewith support points. The function is, therefore, represented by a linear piecewise, andit is not appealing from a graphical point of view, bymaking it practically useless in allproblems requiring a graphical representation of the interpolating function.

With numerous support points, one has to decide howmany points should be usedto build the interpolating polynomial.

A series of 3-degree polynomials (cubic functions) is a good compromise to representa function on a large domain, when specific conditions of derivative continuity areimposed in correspondence with support points.

44j 1 Interpolation

Page 45: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

In the case of a series of cubic functions, parameters ai; bi; ci; di are to be calculated:

PiðxÞ ¼ aiðx�xiÞ3 þ biðx�xiÞ2 þ ciðx�xiÞþ diði ¼ 1; . . . ; n�1Þ ð1:83Þ

Various alternatives exist that shall be discussed in this chapter, such as

1) Hermite cubic polynomials2) Cubic spline

The cubic polynomial interpolations are particularly important and, therefore,they were implemented in appropriate classes.

In the BzzMath library, the classes are called BzzCubicHermite, BzzCubic-Spline and BzzCubicSmooth.

The BzzCubicHermite, BzzCubicSpline, and BzzCubicSmooth classesinterpolate a function through successive cubic polynomials.

Additional examples of the use of BzzCubicHermite, BzzCubicSpline, andBzzCubicSmooth classes can be found in

BzzMath/Examples/BzzMathBasic/CubicInterpolation

either on the enclosed CD-ROM or at the web site:

www.chem.polimi.it/homes/gbuzzi.

1.12.1Hermite Cubic Polynomials

When the first derivatives are known in correspondence with the support points, it ispossible to calculate the four parameters of each cubic function: the polynomials haveto pass through the twopoints at the interval boundary and, there, the derivatives haveto meet each other.

As already described, it is possible to employ a technique similar to Lagrange orNewton methods.

By using the Newton method, the following polynomials are obtained:

hiðxÞ ¼ f ½xi� þ f ½xi; xi�ðx�xiÞþ f ½xi; xi; xiþ 1�ðx�xiÞðx�xiÞ ði ¼ 1; . . . ; n�1Þþ f ½xi; xi; xiþ 1; xiþ 1�ðx�xiÞðx�xiÞðx�xiþ 1Þ

ð1:84Þ

with

f ½xi� ¼ yif ½xi; xi� ¼ y0i

f xi; xi; xiþ 1½ � ¼

yiþ 1�yixiþ 1�xi

�y0i

xiþ 1�xi

f xi; xi; xiþ 1; xiþ 1½ � ¼y0i þ y0iþ 1�2

yiþ 1�yixiþ 1�xi

ðxiþ 1�xiÞ2

1.12 Successive Polynomial Interpolation j45

Page 46: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Rewriting the polynomial in the form (1.83),

di ¼ yici ¼ y0ibi ¼ f ½xi; xi; xiþ 1��hif ½xi; xi; xiþ 1; xiþ 1�ai ¼ f ½xi; xi; xiþ 1; xiþ 1�

If the analytical values of y0i are unknown, the support points can be used toestimate them. Among all the alternatives, the following is recommended.

Given three adjacent points xi�1; xi; xiþ 1, the derivatives in xi is obtained throughthe central formula (see Buzzi-Ferraris and Manenti, 2010a) just adapted to the caseof variable step hi:

y0i �yiþ 1�yi

hi

� �hi�1 þ yi�yi�1

hi�1

� �hi

hi�1 þ hið1:85Þ

TheBzzCubicHermite class has three constructors: thefirst requires the vectorx of the abscissas, the corresponding ordinates y, and the vector of first derivatives y0;the second needs the vectors x and y only as the object numerically estimates the firstderivatives required to get the Hermite cubic functions. The last constructor is thedefault one.

For example,

BzzVector x(...),y(...);

BzzCubicHermite h1(x,y);

BzzVector y1(...);

BzzCubicHermite h2(x,y,y1);

BzzCubicHermite h3;

h3(x,y);

BzzCubicHermite h4;

h4(x,y,y1);

Objects are used through the operator (,). To evaluate the prevision obtained inany point belonging to the interval ½x1; xn�, it is sufficient to use the object as afunction with the abscissa for the prevision as argument.

For example,

BzzVector x(5,1.,2.,3.,4.,5.),y(5,4.,7.,2.,1.,9.);

BzzCubicHermite h(x,y);

double z = 3.123;

BzzPrint(‘‘Prevision %e’’,h(z));

One can obtain the first and the second derivatives by adding 1 or 2, respectively,in the previous argument of operator (,).

For example,

BzzVector x(5,1.,2.,3.,4.,5.),y(5,4.,7.,2.,1.,9.);

BzzCubicHermite h(x,y);

46j 1 Interpolation

Page 47: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

double z = 3.123;

BzzPrint(‘‘Prevision %e’’,h(z));

BzzPrint(‘‘First derivative %e’’,h(z,1));

BzzPrint(‘‘Second derivative %e’’,h(z,2));

Example 1.8

Introduce new points to improve the interpolation.The program is

#include ‘‘BzzMath.hpp’’

void main(void)

{

BzzVector x(11),y(11);

double z;

int i;

for(z = 1.,i = 1;i <= 11;z += .1,i++)

{x[i] = z;y[i] = 1./z;}

BzzCubicHermite h(x,y);

BzzPrint(‘‘\n h(1.76) %e exact %e’’,h(1.76),1./1.76);

int j = x.InsertElementInSortedVector(1.76);

//Element inserted in position j

double v = 1./x[j];

y.Insert(j,v);

h(x,y);

BzzPrint(‘‘\n h(1.76) %e exact %e’’,h(1.76),1./1.76);

}

It is useful to check the plot in order to validate the curve. If there are somezones with sudden variations or discontinuities, the representation is probablyunsatisfactory.

Example 1.9

Create a file for the tool BzzPlotSparse.exe to plot the function1x2

by using

the BzzCubicHermite objects.The program BzzPlotSparse.exe (Buzzi-Ferraris and Manenti, 2010a) allowsone to plot a series of curves with different support points. They should be stored asthe rows of twoBzzMatrixSparseX andY. By default, the former trend is plottedwith a solid line, whereas the latter one through a dotted line. It is, however, possibleto modify the default options.The program is

#include ‘‘BzzMath.hpp’’

void main(void)

1.12 Successive Polynomial Interpolation j47

Page 48: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

{

BzzPrint(‘‘\n\n\nRunge Example’’);

int n = 16;

BzzVector x(n,-5.,-4.,-3.,-2.,-1.,-.5,-.3,

-.1,0.,.1,.3,.5,1.,2.,3.,4.),y(n);

int nz = 50*n - 3;

BzzMatrixSparse Y(2,nz);

BzzMatrixSparse X(2,nz);

double z;

int i;

for(i = 1;i <= n;i++)

{

z = x[i];

y[i] = 1./(1. + z*z);

X(2,i) = z;

Y(2,i) = 1./(1. + z*z);

}

BzzCubicHermite h(x,y);

double dz = (x[n] - x[1])/double(nz-1);

for(z = x[1],i = 1;i <= nz && z <= x[n];z += dz,i++)

{

BzzPrint(‘‘\nx %e Correct %e’’,z,1./(1.+z*z));

BzzPrint(‘‘\nx %e Hermite %e’’,z,h(z));

X(1,i) = z;

Y(1,i) = h(z);

}

BzzSave save(‘‘RungeWithHermite.stx’’);

save << X << Y;

save.End();

}

Run the program BzzPlotSparse.exe, select the item File, New from themenu, and select the file RungeWithHermite.stx just created to visualize thetrend in Figure 1.1.

The function Integral estimates the integral of the function interpolated in theinterval ½x1; xn�.

Example 1.10

EvaluateÐ 21

1zdz.

The program is

#include ‘‘BzzMath.hpp’’

{

48j 1 Interpolation

Page 49: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

BzzVector x(11),y(11);

double z;

int i;

for(z = 1.,i = 1;i <= 11;z += .1,i++)

{x[i] = z; y[i] = 1./z;}

BzzCubicHermite h(x,y);

BzzPrint(‘‘\nIntegral %e’’,h.BzzIntegral());

}

In this example, a value of the integral equal to 0:6932331 is obtained, whereas thecorrect value is logð2:Þ ¼ :6931472.

The BzzCubicSmooth class has a single constructor requiring the vectors x and yand a parameter in the range ½0; 1�.If this parameter is equal to zero, the object simulates a linear piecewise; when it isequal to one, it interpolates with a set of Hermite polynomials. The intermediatevalues of this parameter give hybrid interpolations with intermediate characteristicsbetween these extreme cases: with small values, the interpolation is less smooth, butthe possibility to have undesired minimum or maximum is reduced.For example,

BzzVector x(...),y(...);

BzzCubicSmooth s(x,y,.1);

1.12.2Cubic Spline

Spline indicates the plastic ruler used by designers to draw a curve passing throughsome assigned points. The aim of spline functions is just to interpolate a certainnumber of support points.

Figure 1.1 Polynomials visualization using the BzzPlotSparse toolkit.

1.12 Successive Polynomial Interpolation j49

Page 50: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

To obtain a smooth curve, the continuity of derivatives even in correspondencewith the support points is required. Specifically, first and second derivatives of twodifferent interpolating pieces have to correspond.

Indicating the generic spline with

siðxÞ ¼ aiðx�xiÞ3 þ biðx�xiÞ2 þ ciðx�xiÞþ di

ði ¼ 1; . . . ; n�1Þð1:86Þ

the following conditions are imposed:

siðxiÞ ¼ yi ði ¼ 1; . . . ; n�1Þ ð1:87Þ

siðxiþ 1Þ ¼ yiþ 1 ði ¼ 1; . . . ; n�1Þ ð1:88Þ

s0iðxiþ 1Þ ¼ s0iþ 1ðxiþ 1Þ ði ¼ 1; . . . ; n�2Þ ð1:89Þ

s00i ðxiþ 1Þ ¼ s00iþ 1ðxiþ 1Þ ði ¼ 1; . . . ; n�2Þ ð1:90Þ

The number of unknown parameters is equal to 4n�4, whereas the conditions are4n�6. Other two relations are required to univocally evaluate parameters.

The second derivatives in the generic point i is indicated with s00i .The parameters ai; bi; ci; di can be easily obtained by means of ordinates yi and

derivatives s00i in the support points.Using relations (1.87), (1.88), and (1.89),

ai ¼s00iþ 1�s00i6hi

ði ¼ 1; . . . ; n�1Þ ð1:91Þ

bi ¼ s00i2

ði ¼ 1; . . . ; n�1Þ ð1:92Þ

ci ¼ yiþ 1�yihi

� hiðs00iþ 1 þ 2s00i Þ6

ði ¼ 1; . . . ; n�1Þ ð1:93Þ

di ¼ yi ði ¼ 1; . . . ; n�1Þ ð1:94Þ

The n variables s00i have to solve the following system, obtained by equation (1.89):

hi�1s00i�1 þ 2ðhi�1 þ hiÞs00i þ his00iþ 1

¼ 6ðyiþ 1�yiÞhi

� 6ðyi�yi�1Þhi�1

ði ¼ 2; . . . ; n�1Þ ð1:95Þ

Therefore, parameters ai; bi; ci; di could be explicitly obtainedwhenderivatives s00i wereevaluated. Tridiagonal system (1.95), constituted by n�2 equations, can be usedto calculate the n variables s00i ði ¼ 1; . . . ; nÞ. To get the n derivative values s00i is thennecessary to add two equations.

50j 1 Interpolation

Page 51: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Some alternatives were proposed to assign the remaining two conditions:

1) Second derivatives equal to zero on the boundary:

s001 ¼ s001ðx1Þ ¼ s00n�1ðxnÞ ¼ s00n ¼ 0 ð1:96ÞIn this case, the spline is called natural.

2) Second derivatives assigned on the boundary:

s001 ¼ s001ðx1Þ ¼ d001 ð1:97Þ

s00n ¼ s00n�1ðxnÞ ¼ d00n ð1:98Þ3) Value of the second derivatives on the boundary has to be proportional to the

value of the second derivative in the adjacent point:

s001 ¼ s001ðx1Þ ¼ as001ðx2Þ ¼ as002 ð1:99Þ

s00n ¼ s00n�1ðxnÞ ¼ bs00n�1ðxn�1Þ ¼ bs00n�1 ð1:100ÞUsually, the values for a and b are 1: or 0:5.If a ¼ b ¼ 1,

s0001 ¼ 6a1 ¼ 0 ð1:101Þ

s000n�1 ¼ 6an�1 ¼ 0 ð1:102Þ

In this case, both the polynomials s1 and sn�1 become 2-degree polynomials.

The selection ofa ¼ b ¼ 1 is reasonable (and it is a default choice) when one hasno information.

4) First derivatives assigned on the boundary:

s01ðx1Þ ¼ y01 ð1:103Þ

s0n�1ðxnÞ ¼ y0n ð1:104ÞThese two conditions take to the following equations

2h1s001 þ h1s

002 ¼ 6ðy2�y1Þ

h1�6y01 ð1:105Þ

hn�1s00n�1 þ 2hn�1s

00n ¼ � 6ðyn�yn�1Þ

hn�1þ 6y0n ð1:106Þ

that have to be added to system (1.95), so as to make it a tridiagonal.If one does not know the analytical value of y01 and y

0n, it is possible to obtain them

numerically. Actually, first four points and last four points can be used to gettwo 3-degree polynomials that interpolate exactly them; the analytical derivativeof such polynomials can be evaluated and, at last, it can be used as anapproximation of the real derivative.

1.12 Successive Polynomial Interpolation j51

Page 52: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

5) Parabolic interpolation for the two boundary polynomials:

s0001 ðx1Þ ¼ 0 ð1:107Þ

s000n�1ðxnÞ ¼ 0 ð1:108Þ

This condition is the same as the default one described above.All the aforementioned conditions introduced in system (1.95) generate a tridia-

gonal system with a dominant diagonal in the variables s00i . This system can be solvedby adopting the techniques analyzed in Buzzi-Ferraris and Manenti (2010a). As thesystem has the dominant diagonal, its solution is stable even without pivoting.6) In case of periodic functions, itmay beuseful to impose the following conditions.

If a periodic function has periodT and the support points are disposed so to havex1 at the beginning and xn at the end of the period, it is possible to impose that thefirst and the second derivatives at the initial point have to be equal to the first andthe second derivatives, respectively, at the last point.

In this last case, the resulting system has two coefficients out of the tridiagonalstructure: the first one is placed on the lower corner at the left of the matrix; thesecond one on the higher corner at the right of the matrix. Even in this case, thesystem can be solved without any difficulty by the technique for partially structuredsystems (see Buzzi-Ferraris and Manenti, 2010a).

As already said in this chapter, to univocally define the cubic spline some additionalconditions are required. In the BzzCubicSpline class, several possibilities areconsidered and are discriminated by assigning the following values to a specificparameter:

NATURAL

ASSIGNED_SECOND_DERIVATIVE

SECOND_DERIVATIVE_ADJACENT

ASSIGNED_FIRST_DERIVATIVE

PERIODIC

The BzzCubicSpline class has two constructors. The first one requires thevectors x and y of the support points, the value of the parameter to select thespline type and two double. Themeaning of the two double changes according tothe selected spline type: the boundary values of second derivatives are providedwith ASSIGNED_SECOND_DERIVATIVE, whereas the values of a and b ofequations (1.99) and (1.100) are provided with the selection of

SECOND_DERIVATIVE_ADJACENT:

s001 ¼ s001ðx1Þ ¼ as001ðx2Þ ¼ as002

s00n ¼ s00n�1ðxnÞ ¼ bs00n�1ðxn�1Þ ¼ bs00n�1

The boundary values for the first derivatives are provided with theASSIGNED_FIRST_DERIVATIVE.

52j 1 Interpolation

Page 53: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

The latter constructor is used for the NATURAL or PERIODIC spline. Thisconstructor is also used to create the default spline

s0001 ðx1Þ ¼ 0

s000n�1ðxnÞ ¼ 0

which does not require any index for discriminating the spline types.It is worth remarking that this option is equal to the case SECOND_DERIVA-

TIVE_ADJACENT with a ¼ b ¼ 1.For example,

BzzVector x(...),y(...);

//First constructor

BzzCubicSpline s1(x,y,ASSIGNED_SECOND_DERIVATIVE,

.05,-2.);

BzzCubicSpline s2(x,y,

SECOND_DERIVATIVE_ADJACENT,.5,.5);

BzzCubicSpline s3(x,y,ASSIGNED_FIRST_DERIVATIVE,

3.,2.);

//Second constructor

BzzCubicSpline s4(x,y,NATURAL);

BzzCubicSpline s5(x,y,PERIODIC);

BzzCubicSpline s6(x,y); // default spline

// or BzzCubicSpline s6(x,y,

SECOND_DERIVATIVE_ADJACENT);

// or BzzCubicSpline s6(x,y,

SECOND_DERIVATIVE_ADJACENT,1.,1.);

Example 1.11

Use an object of the BzzCubicSpline class to interpolate a periodic function.The program is

#include ‘‘BzzMath.hpp’’

void main(void)

{

BzzPrint(‘‘\n\nSpline PERIODIC’’);

int n = 22;

BzzVector x(n),y(n);

double z;

int i;

for(z = 0.,i = 1;i <= 22;z += 2.*atan(1)*4./21.,i++)

{

x[i] = z;

y[i] = sin(z);

}

1.12 Successive Polynomial Interpolation j53

Page 54: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

x.BzzPrint(‘‘x’’);

y[n] = 0.;

BzzCubicSpline s(x,y,PERIODIC);

for(z = 0.;z <= 2.*atan(1)*4.;z +=

2.*atan(1)*4./100.)

{

BzzPrint(‘‘\nSpline z %e y %e y1 %e y2 %e’’,

z,s(z),s(z,1),s(z,2));

BzzPrint(‘‘\nCorrect z %e y %e y1 %e y2 %e’’,

z,sin(z),cos(z),-sin(z));

BzzPause();

}

}

1.13Two-Dimensional Curves

An important field for the application of successive interpolation of polynomialsis in graphics, where a continuous curve passing through specific points isrequired.

The argument requires a larger discussion than the one presented in this book.Actually, only a simple application is proposed here.

To draw a curve passing through some known points ðxi; yiÞ that cannot beunivocally represented as function yðxÞ, it is possible to adopt the following device.Create a new set of points exactly equal to the existing one, with dependent variables xand y and the auxiliary parameter t defined as follows:

t1 ¼ 0 ð1:109Þ

ti ¼ ti�1 þffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiffiðxi�xi�1Þ2 þðyi�yi�1Þ2

qði ¼ 2; nÞ ð1:110Þ

The parameter t is the abscissa. The function is obtained by varying t from t1 to tnand evaluating the corresponding previsions x and y.

It is appropriate to graphically check the curve. If some zones have sudden variations,the representation is unsatisfactory.

1.14Orthogonal Polynomials

This section deals with orthogonal polynomials, which have a considerable impor-tance in numerical analysis.

54j 1 Interpolation

Page 55: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

Two vectors x and y are orthogonal if their scalar product is equal to zero:

xTy ¼Xni¼1

xiyi ¼ xk k2 yk k2cosa ¼ 0 ð1:111Þ

In other words, two vectors are orthogonal when they generate an angle a ¼ p=2.This property was extended to the functions (vectors with infinite elements) asfollows.

Two functions gðxÞ and hðxÞ are orthogonal in the interval ½a; b� against the weightfunction rðxÞ if

ðbarðxÞgðxÞhðxÞdx ¼ 0 ð1:112Þ

Orthogonal polynomials constitute special families of orthogonal functions con-sisting of zeroth-, first-, second-, and higher-degree polynomials that are mutuallyorthogonal. They are characterized by the existence interval ½a; b� and the weight rðxÞthat makes them orthogonal. The main sets are given in Table 1.1.

For example, the polynomials

P2ðxÞ ¼ 3x2�12

P3ðxÞ ¼ 5x3�3x2

belong to the Legendre family and the following equation results:

ð1�1

P2ðxÞP3ðxÞ dx ¼ 0

Each set of orthogonal polynomials has specific features. One of them correlatesthree adjacent polynomials. In the following, the recurring relations for theaforementioned polynomials are shown for n 1.

. Legendre polynomials:

ðnþ 1ÞPnþ 1ðxÞ�ð2nþ 1ÞxPnðxÞþ nPn�1ðxÞ ¼ 0 ð1:113Þ

Table 1.1 Main sets of orthogonal polynomials.

a b r(x)

Legendre �1 1 1Laguerre 0 1 e�x

Hermite �1 1 e�x2

Chebyshev �1 11ffiffiffiffiffiffiffiffiffiffiffi1�x2

p

1.14 Orthogonal Polynomials j55

Page 56: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

. Laguerre polynomials:

ðnþ 1ÞLnþ 1ðxÞ�ð2nþ 1�xÞxLnðxÞþ n2Ln�1ðxÞ ¼ 0 ð1:114Þ

. Hermite polynomials:

Hnþ 1ðxÞ�2xHnðxÞþ 2nHn�1ðxÞ ¼ 0 ð1:115Þ

. Chebyshev polynomials:

Tnþ 1ðxÞ�2xTnðxÞþTn�1ðxÞ ¼ 0 ð1:116Þ

For example, the 4-degree Legendre polynomial can be obtained by knowing 2- and3-degree polynomials:

4P4ðxÞ ¼ 7xP3ðxÞ�3P2ðxÞ ¼ 7x5x3�3x

2�3

3x2�12

Hence,

P4ðxÞ ¼ 35x4�30x2 þ 38

These polynomials have numerous features that are not explained here. In thefollowing section, only Chebyshev polynomials are discussed for their relevance.

1.14.1Chebyshev Polynomials

Let us consider the function

TnðxÞ ¼ cosðnðarccosðxÞÞÞ ðn ¼ 0; 1; . . .Þ ð1:117Þ

for x 2 ½�1; 1�.With n ¼ 0,

T0ðxÞ ¼ cosð0Þ ¼ 1 ð1:118Þ

With n ¼ 1,

T1ðxÞ ¼ cosðarccosðxÞÞ ¼ x ð1:119Þ

For the generic n, the following property can be exploited:

cosðntÞ ¼ 2cosðtÞcosððn�1ÞtÞ�cosððn�2ÞtÞ ð1:120Þ

which gives the recursive formula

Tnþ 1ðxÞ ¼ 2xTnðxÞ�Tn�1ðxÞ ð1:121Þ

56j 1 Interpolation

Page 57: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

First terms are

T0 ¼ 1;

T1 ¼ x;

T2 ¼ 2x2�1

T3 ¼ 4x3�3x

T4 ¼ 8x4�8x2 þ 1

T5 ¼ 16x5�20x3 þ 5x

T6 ¼ 32x6�48x4 þ 18x2�1

T7 ¼ 64x7�112x5 þ 56x3�7x

Functions TnðxÞ are called Chebyshev polynomials. It is possible to express thepowers of x against Chebyshev polynomials by using the previous expressions:

1 ¼ T0

x ¼ T1

x2 ¼ T0 þT2

2

x3 ¼ 3T1 þT3

4

x4 ¼ 3T0 þ 4T2 þT4

8

x5 ¼ 10T1 þ 5T3 þT5

16. . .

Chebyshev polynomials are important for some of their properties.

1) Each polynomial TnðxÞ in the interval �1 � x � 1 is even in the range ½�1; 1�.It directly derives from definition (1.117).

2) The n roots ofTnðxÞ polynomials are real, distinct, and inside the interval ½�1; 1�;moreover, they can be easily calculated through the formula:

li ¼ cosð2i�1Þp

2n

� �ði ¼ 1; . . . ; nÞ ð1:122Þ

3) Polynomials TmðxÞ and TnðxÞ with m 6¼ n are mutually orthogonal in both thecontinuous

ð1�1

1ffiffiffiffiffiffiffiffiffiffiffi1�x2

p TmðxÞTnðxÞdx ¼

0; m 6¼ np

2; m ¼ n

p; m ¼ n ¼ 0

8>>><>>>:

ð1:123Þ

1.14 Orthogonal Polynomials j57

Page 58: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

and against N discrete points xi

XN�1

j¼0

TmðxjÞTnðxjÞ ¼0; m 6¼ nN2; m ¼ n

N; m ¼ n ¼ 0

8>><>>:

ð1:124Þ

on the condition that abscissas are determined through the relation

xj ¼ cosp

Nj

� ð1:125Þ

4) As all orthogonal polynomials, even Chebyshev polynomials, satisfy the three-term relation (1.121).

5) For a large amount of functions, the series expansion of Chebyshev polynomialsconverges very fast, more than the other expansions. This feature is exploited torepresent a function through the economization process.For example, the power series expansion

lnðxþ 1Þ � x� x2

2þ x3

3� x4

4þ x5

5

has a truncation error on the order of 1:=6: ¼ :1666. This expression can beconverted into a Chebyshev polynomial series:

lnðxþ 1Þ � T1�T0 þT2

4þ 3T1 þT3

12� 3T0 þ 4T2 þT4

32þ 10T1 þ 5T3 þT5

80

Hence,

lnðxþ 1Þ � � 1132

T0 þ 118T1� 3

8T2 þ 7

48T3� 1

32T4 þ 1

80T5

In the interval 0 � x � 1, this series has the same accuracy as the previous one,even neglecting the last three terms, as 7:=48: ¼ 0:146 < 0:166. Therefore, firstthree elements of the series expansion are enough:

lnðxþ 1Þ � � 1132

þ 118x� 3

8ð2x2�1Þ ¼ � 1

32þ 11

8x� 3

4x2

6) Among all the n-degree polynomials with coefficient equal to 1 in correspon-dence with the term with maximum degree, the polynomial

WnðxÞ ¼ TnðxÞ2n�1 ð1:126Þ

has the minimum of the maximum absolute value in the interval �1 � x � 1.This last property allows one to solve a problem that was left in abeyance.

The error in a polynomial interpolation is given by relation (1.12).When it is possibleto select the abscissas x0; x1; . . . ; xn in the interval �1 � x � 1, the function

Wnþ 1ðxÞ ¼ Wnþ 1ðx; x0; x1; . . . ; xnÞ ¼ ðx�x0Þðx�x1Þ � � � ðx�xnÞ ð1:127Þ

58j 1 Interpolation

Page 59: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa

can be minimized by selecting the abscissas in correspondence with the roots of theðnþ 1Þ-degree Chebyshev polynomial.

Since ðnþ 1Þ-degree Chebyshev polynomial has the same roots as the functionWnþ 1ðxÞ and the multiplicative coefficient of its maximum order is equal to 2n,

Tnþ 1ðxÞ2n

¼ Wnþ 1ðxÞ ¼ ðx�x0Þðx�x1Þ � � � ðx�xnÞ ð1:128Þ

An important consequence of relation (1.128) is related to the fact that Chebyshevpolynomials have values less than or equal to 1: and, therefore, the functionWnþ 1ðxÞis less than or equal to 1=2n.

If the interval of interest is a � x � b, it is sufficient to evaluate the zeros of theauxiliary variable �1 � z � 1 and operate a variable transformation:

xi ¼ ziðb�aÞþ ðbþ aÞ2

ð1:129Þ

1.14 Orthogonal Polynomials j59

Page 60: 1 Interpolation - application.wiley-vch.de · 1.3 Polynomial Interpolation When the polynomial has a single variable x, it can assume the form P nðxÞ¼a 0 f 0ðxÞþa 1 f 1ðxÞþa