Sunteți pe pagina 1din 19

Computational Approach to

Materials Science and Engineering


Prita Pant and M. P. Gururajan
October, 2012

c 2012, Prita Pant and M P Gururajan. Permission


Copyright
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 BackCover Texts. A copy of the license is included in the section
entitled GNU Free Documentation License.

Module: Interpolation
1

Pre-requisites

The modules in Part II of this course material.

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

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.

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.
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
%
X

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


= load("DepthGasAge.dat");
3

Age (in years BP) Depth (in m)


GRID: Greenland

Depth (in m)
Byrd: Antarctica

17000
18000
19000
20000
21000
22000
23000
24000
25000
26000
27000
28000
29000
30000
31000
32000
33000
34000
35000
36000
37000
38000
39000
40000
41000
42000
43000
44000
45000
46000

1284
1317
1350
1383
1410
1421
1432
1446
1472
1504
1530
1552
1573
1594
1613
1633
1653
1673
1702
1722
1742
1763
1780
1793
1805
1817
1832
1850
1867
1885

1854
1889
1916
1937
1958
1979
1994
2010
2026
2046
2068
2080
2093
2112
2125
2140
2162
2173
2192
2216
2225
2237
2253
2263
2281
2290
2305
2323
2338
2347

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 Atmospheric CO2 concentration and millennial-scale
climate change during the
4
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 corresponds 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 without the extrap string, the program will return NA.

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 fun5

Diffusion couple
c
1

c
2
t=0

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.
2
tion of time,
is
known to be described by the expression c(x, t) = c1 +c

2


x
c1 c2
erf 2Dt where c1 and c2 are the initial compositions of the two rods,
2
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.

erf(z)

-2.0
-1.5
-1.0
-0.5
0.0
0.5
1.0
1.5
2.0

-0.9953
-0.9661
-0.8427
-0.5205
0.0000
0.5205
0.8427
0.9661
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

erf(z)

0.5

-0.5

-1
-2

-1.5

-1

-0.5

0.5

1.5

Figure 2: The linear interpolation for the error function.

z
-2.8
-2.6
-2.4
-2.2
-2.0
-1.9
-1.8
-1.7
-1.6
-1.5
-1.4
-1.3
-1.2
-1.1
-1.0
-0.95
-0.90
-0.85
-0.80
-0.75
-0.70
-0.65
-0.60
-0.55
-0.50
-0.45
-0.40
-0.35
-0.30
-0.25
-0.20
-0.15
-0.10
-0.05
-0.025
0.000

erf(z)
-0.9999
-0.9998
-0.9993
-0.9981
-0.9953
-0.9928
-0.9891
-0.9838
-0.9763
-0.9661
-0.9523
-0.9340
-0.9103
-0.8802
-0.8427
-0.8209
-0.7970
-0.7707
-0.7421
-0.7112
-0.6778
-0.6420
-0.6039
-0.5633
-0.5205
-0.4755
-0.4284
-0.3794
-0.3268
-0.2763
-0.2227
-0.1680
-0.1125
-0.0564
-0.0282
0.0000

z
2.8
2.6
2.4
2.2
2.0
1.9
1.8
1.7
1.6
1.5
1.4
1.3
1.2
1.1
1.0
0.95
0.90
0.85
0.80
0.75
0.70
0.65
0.60
0.55
0.50
0.45
0.40
0.35
0.30
0.25
0.20
0.15
0.10
0.05
0.025

erf(z)
0.9999
0.9998
0.9993
0.9981
0.9953
0.9928
0.9891
0.9838
0.9763
0.9661
0.9523
0.9340
0.9103
0.8802
0.8427
0.8209
0.7970
0.7707
0.7421
0.7112
0.6778
0.6420
0.6039
0.5633
0.5205
0.4755
0.4284
0.3794
0.3268
0.2763
0.2227
0.1680
0.1125
0.0564
0.0282

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.

erf(z)

0.5

-0.5

-1
-3

-2

-1

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

erf(z)

0.5

-0.5

-1
-2

-1.5

-1

-0.5

0.5

1.5

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

12

The specific heat data: polynomial interpolation

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 fitting for further examples of polynomial interpolation, with specific reference
to specific heats.

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?

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

0.0
0.7854
1.5708
2.3562
3.1416
3.9270
4.7124
5.4978
6.2832

0.0
0.7071
1.0000
0.7071
0.0000
-0.7071
-1.0000
-0.7071
0.0000

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


The y values are oscillatory.

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. Calculate 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
07.24
16.76
25.75
36.30
46.68
56.43
65.69
76.25
87.49
98.87

0.0000
0.0001
0.0002
0.0003
0.0004
0.0005
0.0006
0.0007
0.0008
0.0009
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

0.5

-0.5

-1
0

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

0.5

-0.5

-1

-1.5
0

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 Wiley and Sons, 1999.
2. Elementary Numerical Analysis, K. E. Atkinson, 3rd edition, Wiley
India, 2003.

19

S-ar putea să vă placă și