You are on page 1of 19

Computational Approach to

Materials Science and Engineering


Prita Pant and M. P. Gururajan

October, 2012

Copyright c 2012, Prita Pant and M P Gururajan. Permission


is granted to copy, distribute and/or modify this document under
the terms of the GNU Free Documentation License, Version 1.3
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-
Cover Texts. A copy of the license is included in the section
entitled “GNU Free Documentation License”.

1
Module: Interpolation

1 Pre-requisites
The modules in Part II of this course material.

2 Learning goals
• Given a set of data, to interpolate between the given data points

3 Interpolation
Interpolation is a procedure that is used to obtain the values of y for any
x in the range x1 to x2 given the data points (x1 , y1 ) and (x2 , y2 ). There
are many different interpolations that are possible. In this module, we shall
discuss some examples.

4 Air bubbles in polar ice


It is possible to track the atmospheric concentrations of CO2 on a millennial
time scale by analysing the air bubbles trapped in the polar ice. For example,
consider the data of the age of air bubbles found in two different ice cores,
namely GRID in Greenland and Byrd in Antarctica, shown in Table. 1. The
age is given in units of years Before Present (BP), that is, years calculated
with 1 January 1950 as the origin (after 1950, the years can not be reliably
calculated using radioactive isotope abundance data due to nuclear weapons
testing). Given this data, let us consider the following questions:

1. To what depth in GRID ice core should one go to find air bubbles of
age 20500 years BP?

2. At a depth of 1600 m in the Byrd ice core, what would be the age of
air bubbles?

2
3. A depth of 2000 m in GRID ice core corresponds to what depth in Byrd
ice core?

All these questions can be answered using interpolation.

4.1 Linear interpolation


The simplest interpolation to consider is the linear interpolation. In this,
one assumes that the data at the two points (x1 , y1 ) and (x2 , y2) can be
connected through a straight line so that for any intermediate value of x,
the corresponding y value can be obtained and vice versa. The GNU Octave
command interp1 can be used to carry out such a linear interpolation. In
the script below, for example, we show how interp1 can be used to answer
all the three questions above: and the answers for the three questions are,
1947.5 m, 30316 years BP and 1437.2 m, repsectively.

# DepthGasAge.oct
#
# Copyright (C) 2011 Prita Pant and M P Gururajan
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
#
% Load the given data from the data file
X = load("DepthGasAge.dat");

3
Age (in years BP) Depth (in m) Depth (in m)
GRID: Greenland Byrd: Antarctica

17000 1854 1284


18000 1889 1317
19000 1916 1350
20000 1937 1383
21000 1958 1410
22000 1979 1421
23000 1994 1432
24000 2010 1446
25000 2026 1472
26000 2046 1504
27000 2068 1530
28000 2080 1552
29000 2093 1573
30000 2112 1594
31000 2125 1613
32000 2140 1633
33000 2162 1653
34000 2173 1673
35000 2192 1702
36000 2216 1722
37000 2225 1742
38000 2237 1763
39000 2253 1780
40000 2263 1793
41000 2281 1805
42000 2290 1817
43000 2305 1832
44000 2323 1850
45000 2338 1867
46000 2347 1885

Table 1: The age of air bubbles at various depths in two ice cores, namely
GRID in Greenland and Byrd in Antarctica. The data is taken from At-
mospheric CO2 concentration and millennial-scale
4 climate change during the
last glacial period, B. Stauffer et al, Nature, 392, pp. 560-561, 1954.
% Read the first column of the data as age
A = X(:,1);
% Read the second column of the data as depth for GRID
dgrid = X(:,2);
% Read the third column of the data as depth for Byrd
dbyrd = X(:,3);
% Interpolate the GRID data for the age of air bubble to be 20500
interp1(A,dgrid,20500)
% Interpolate the Byrd data for the depth of 1600
interp1(dbyrd,A,1600)
% Interpolate the Byrd data to a depth of 2000 m of GRID
interp1(dgrid,dbyrd,2000)

From the above script, it is clear that the x and y data are given as the first
two parameters of interp1 and the third parameter is the x for which we
want to calculate the y value. For example, a command such as interp1(dbyrd,
dgrid, 1880) will give the depth of GRID that correpsonds to 1880 m of
Byrd while interp1(dgrid,dbyrd,1880) gave the depth of Byrd that cor-
responds to 1880 m of GRID.
Here are two important points to remember while using interp1.

• The sample points x, that is, the first set of parameters passed on to
interp1 should be strictly monotonic – that is, either continuously
increasing or decreasing.
• The point at which we want to calculate the y value should lie between
the given data; calculating the y values for x values that lie outside the
given range of sample data is known as “extrapolation”. It is possible
to exptrapolate using interp1. For that, we need to explicitly mention
extrap as the fourth input parameter in the command: for example,
interp1(dbyrd,dgrid,1900,‘extrap’) will give answers while with-
out the ‘extrap’ string, the program will return NA.

5 Error function evaluation


Consider a diffusion couple, consisting of two very long rods, welded face
to face as shown. The composition in such a diffusion couple, as a fun-

5
Diffusion couple

c c
1 2

t=0
c

x
Figure 1: The schematic of a diffusion couple. Two long rods of different
compositions, c1 and c2 ae welded face to face. The initial composition
profile looks as shown below the couple. If this assembly is kept at high
enough temperatures, then the diffusion profile evovles and the profile is
described by error function.

tion of time,

is

known to be described by the expression c(x, t) = c1 +c
2
2

c1 −c2 x
2
erf 2√Dt where c1 and c2 are the initial compositions of the two rods,
and D is the diffusivity.

The error function, erf(z), for any z has to read off from tables. Typically, the
tabulation is done for a certain number of z values and for any intermediate
values, the error function has to be obtained using interpolation. Let us
suppose that we know the error function values for nine different points as
shown in Table. 2. For any intermediate values of z, one can then calculate
the error function values by interpolation.

6
z erf(z)

-2.0 -0.9953
-1.5 -0.9661
-1.0 -0.8427
-0.5 -0.5205
0.0 0.0000
0.5 0.5205
1.0 0.8427
1.5 0.9661
2.0 0.9953

Table 2: Tabulation of error function values in the range x = −2 to x = 2.


See Materials Science and Engineering: a first course, V Raghavan, Third
edition, Prentice-Hall of India Pvt. Ltd, 1995, for example.

With the given data, if we try a linear interpolation (See Problem 1), the
interpolation looks as shown in Figure. 2. As is clear from the figure, the
interpolation is not satisfactory since it gives rise to a jagged profile.

It indeed is possible to get a much better plot of error function using a table
with much finer sampling: see Table. 3 and Fig. 3.

5.1 Spline interpolation


It is also possible to get a smooth curve for the error function by carrying
out spline interpolation instead of linear interpolation. In the script below
we show how to fit even a small number of data (as given in Table. 2) using
spline interpolation to achieve a smoother profile, which is comparable to
what is achieved using a much larger data set. The plot obtained using the
spline interpolation is shown in Fig. 4

7
1

0.5
erf(z)

-0.5

-1
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
z

Figure 2: The linear interpolation for the error function.

8
z erf(z) z erf(z)
-2.8 -0.9999 2.8 0.9999
-2.6 -0.9998 2.6 0.9998
-2.4 -0.9993 2.4 0.9993
-2.2 -0.9981 2.2 0.9981
-2.0 -0.9953 2.0 0.9953
-1.9 -0.9928 1.9 0.9928
-1.8 -0.9891 1.8 0.9891
-1.7 -0.9838 1.7 0.9838
-1.6 -0.9763 1.6 0.9763
-1.5 -0.9661 1.5 0.9661
-1.4 -0.9523 1.4 0.9523
-1.3 -0.9340 1.3 0.9340
-1.2 -0.9103 1.2 0.9103
-1.1 -0.8802 1.1 0.8802
-1.0 -0.8427 1.0 0.8427
-0.95 -0.8209 0.95 0.8209
-0.90 -0.7970 0.90 0.7970
-0.85 -0.7707 0.85 0.7707
-0.80 -0.7421 0.80 0.7421
-0.75 -0.7112 0.75 0.7112
-0.70 -0.6778 0.70 0.6778
-0.65 -0.6420 0.65 0.6420
-0.60 -0.6039 0.60 0.6039
-0.55 -0.5633 0.55 0.5633
-0.50 -0.5205 0.50 0.5205
-0.45 -0.4755 0.45 0.4755
-0.40 -0.4284 0.40 0.4284
-0.35 -0.3794 0.35 0.3794
-0.30 -0.3268 0.30 0.3268
-0.25 -0.2763 0.25 0.2763
-0.20 -0.2227 0.20 0.2227
-0.15 -0.1680 0.15 0.1680
-0.10 -0.1125 0.10 0.1125
-0.05 -0.0564 0.05 0.0564
-0.025 -0.0282 0.025 0.0282
0.000 0.0000

Table 3: Tabulation of error function values in the range x = −2.8 to x = 2.8.


See Materials Science and Engineering: 9 a first course, V Raghavan, Third
edition, Prentice-Hall of India Pvt. Ltd, 1995, for example.
1

0.5
erf(z)

-0.5

-1
-3 -2 -1 0 1 2 3
z

Figure 3: The plot of error function with a much finer tabulation.

10
# ErfSplineInterpolation.oct
#
# Copyright (C) 2011 Prita Pant and M P Gururajan
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
#
X = load(’errorfunction.dat’);
a = X(:,1);
b = X(:,2);
plot(a,b,’o’);
hold on
x = [-2.:0.1:2.];
xx = interp1(a,b,x,’spline’);
plot(x,xx,’r’)
axis(’square’)
xlabel("z")
ylabel("erf(z)")
print -depsc ../figures/ErfSplineInterpolation.eps

11
1

0.5
erf(z)

-0.5

-1
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
z

Figure 4: The plot of error function with very few points of tabulated data
and spline interpolation. The spline interpolation results in a smoother pro-
file.

12
6 The specific heat data: polynomial inter-
polation
In the previous section, we have used either polyfit or the design matrix
to fit the given data to polynomials. It is also possible to use the fitted
polynomials for interpolation. Thus the command polyval can be used for
interpolation if polyfit was used for fitting data; in the case a design matrix
was used, the coefficients can be used for interpolation. See the module on fit-
ting for further examples of polynomial interpolation, with specific reference
to specific heats.

7 Self-assessment questions
1. What is the command in GNU Octave for interpolation?

2. interp1(x,y,a) means the x value corresponding to y = a is obtained.


True or false?

3. polyval can be used for interpolation. True or false?

4. Can interp1 be used for exptrapolation?

5. In interp1(x,y,a), should the y also be monotonic?

8 Answers to self-assessment questions


1. interp1

2. False. It means the y value corresponding to x = a is obtained.

3. True. If polyfit is used on the given data to fit a polynomial, then,


polyval can be used for interpolation.

4. Yes; by passing an optional parameter ’extrap’, extrapolation can be


carried out.

5. No. See the Problem 2, for example.

13
x y

0.0 0.0
0.7854 0.7071
1.5708 1.0000
2.3562 0.7071
3.1416 0.0000
3.9270 -0.7071
4.7124 -1.0000
5.4978 -0.7071
6.2832 0.0000

Table 4: Tabulation of x and y values in which only x values are monotonic.


The y values are oscillatory.

9 Exercises
• Problem 1.
Use the data given in Table. 2 to do linear interpolation for z values in
the range z = −2 to z = 2 (in increments of 0.1) and calculate erf(z).

• Problem 2.
Do a linear and spline interpolation for the data given in Table. 4.

• Problem 3.
The stress-strain data for a brass sample is as shown in Table. 5. Cal-
culate the strain at a stress of 50 MPa and the stress for a strain of
0.00075.

10 Solution to exercises
• Solution to Problem 1.

14
Stress (in MPa) Strain

00.00 0.0000
07.24 0.0001
16.76 0.0002
25.75 0.0003
36.30 0.0004
46.68 0.0005
56.43 0.0006
65.69 0.0007
76.25 0.0008
87.49 0.0009
98.87 0.0010

Table 5: Tabulation of stress strain data for a brass sample.

X = load(’errorfunction.dat’);
a = X(:,1);
b = X(:,2);
plot(a,b,’o’);
hold on
x = [-2.:0.1:2.];
xx = interp1(a,b,x);
plot(x,xx,’r’)
axis(’square’)
xlabel("z")
ylabel("erf(z)")
print -depsc ../figures/ErfLinearInterpolation.eps

The plot generated by this script is shown in Fig. 2.

• Solution to Problem 2
The script for carrying out linear interpolation on the given data is
given below and the figure generated is shown in Fig. 5.

15
1

0.5

0
y

-0.5

-1
0 1 2 3 4 5 6 7
x

Figure 5: Linear interpolation for the data given in Problem 2. Note that
even if the y values are non-monotonic, the interpolation can be carried out
as long as x is monotonic. From the figure, it is clear that the given data is
for a sinusoidal curve.

X = load(’SineData.dat’);
a = X(:,1);
b = X(:,2);
plot(a,b,’o’);
hold on
x = [0.0:0.01:6.3];
xx = interp1(a,b,x);
plot(x,xx,’r’)
axis(’square’)
xlabel("x")
ylabel("y")
print -depsc ../figures/SineLinear.eps

16
The script for carrying out linear interpolation on the given data is
given below and the figure generated is shown in Fig. 6.

X = load(’SineData.dat’);
a = X(:,1);
b = X(:,2);
plot(a,b,’o’);
hold on
x = [0.0:0.01:6.3];
xx = interp1(a,b,x,’spline’);
plot(x,xx,’r’)
axis(’square’)
xlabel("x")
ylabel("y")
print -depsc ../figures/SineSpline.eps

Note that even though the spline fitting gives the sinusoidal curve much
better than the linear fit, it is not exact. This can be clearly seen if
we compare the calculated value from spline and compare it with the
actual value of sine at the given point.

• Solution to Problem 3
By plotting the data, one can see that the stress strain plot is linear;
so, we are within the elastic limit. Hence, for finding the strain, one
can use linear interpolation. Using linear interpolation, we get a strain
of 0.000534 for a stress of 50 MPa and a stress of 70.97 MPa for a strain
of 0.00075. The code for calculation is as shown below.

X = load(’StressStrain.dat’);
a = X(:,1);
b = X(:,2);
interp1(a,b,50)
interp1(b,a,0.00075)

17
1

0.5

0
y

-0.5

-1

-1.5
0 1 2 3 4 5 6 7
x

Figure 6: Spline interpolation for the data given in Problem 2. Note that
even if the y values are non-monotonic, the interpolation can be carried out
as long as x is monotonic. From the figure, it is clear that the given data is
for a sinusoidal curve.

18
11 References and further reading
1. Advanced Engineering Mathematics, E. Kreyszig, 8th edition, John Wi-
ley and Sons, 1999.

2. Elementary Numerical Analysis, K. E. Atkinson, 3rd edition, Wiley


India, 2003.

19

You might also like