Documente Academic
Documente Profesional
Documente Cultură
Module: Interpolation
1
Pre-requisites
Learning goals
Given a set of data, to interpolate between the given data points
Interpolation
4.1
Linear interpolation
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
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.
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
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
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
erf(z)
0.5
-0.5
-1
-3
-2
-1
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
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?
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
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
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
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