Professional Documents
Culture Documents
Computational Approach To Materials Science and Engineering: Prita Pant and M. P. Gururajan October, 2012
Computational Approach To Materials Science and Engineering: Prita Pant and M. P. Gururajan October, 2012
October, 2012
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.
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?
# 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
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
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
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.
7
1
0.5
erf(z)
-0.5
-1
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
z
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
0.5
erf(z)
-0.5
-1
-3 -2 -1 0 1 2 3
z
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?
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
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
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
• 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.
19