2.4.1 Polynomial intErPolation
Sometimes we need to represent a function based on knowledge about its behavior at a set of dis- crete points. Interpolation is a method that produces a function that best matches a given data, while also providing a good approximation to the unknown values at intermediate points. Suppose that values of a function f(xi) are known at a set of undependent variables xi as shown in Table 2.9.
TABLE 2.9
Known Function Values
xi f(xi)
x1 f(x1)
x2 f(x2)
⋮ ⋮
xn f(xn)
DataModel
51 5.2 5.4 5.6 5.8 6 6.2 6.4 6.6
2 3 4 5
n
r
6 7 8
×10–3
FIGURE 2.7 Comparison between rate data and model.
The general objective in developing interpolating polynomials is to choose a polynomial Pn(x) of the form
P xn
( )
=a0+a x1 + + a xn nso that this equation best fits the base points of the function, and connect these points with a smooth curve. In short, we find Pn(x) such that
P xn
( )
= f x( )
i(
i=0 1, ,¼,n)
The resulting polynomial Pn(x) can be used to estimate data values at intermediate points.
Example 2.19: Determination of Interpolating 2nd-Order Polynomial
Table 2.10 shows an entropy data of saturated steam at three different temperatures. Identify the 2nd-order polynomial that best fits the data, and estimate the entropy at T = 373.15 K using this polynomial.
Solution
We have to choose the coefficients of the quadratic equation f x
( )
=a x2 2+a x a+1 0
so that this equation best fits the data points.
The procedure shown in Table 2.11 can be represented as 313 15 313 15 1
363 15 363 15 1 413 15 413 15 1
2 2 2
2 1 0
. .
. .
. .
é ë êê ê
ù û úú ú
a a a éé ë êê ê
ù û úú ú
= é ë êê ê
ù û úú ú 8 2583 7 4799 6 9284
. . .
TABLE 2.10
Entropy of Saturated Steam
T(K) Entropy (kJ/(kg · K))
313.15 8.2583
363.15 7.4799
413.15 6.9284
TABLE 2.11
Determination of 2nd-Order Polynomial to Fit Entropy Data
i xi f x(( ))i == ==fi a x2 i2++a xi++a
1 0
1 313.15 8.2583 = a2(313.15)2 + a1(313.15) + a0 2 363.15 7.4799 = a2(363.15)2 + a1(363.15) + a0
3 413.15 6.9284 = a2(413.15)2 + a1(413.15) + a0
The coefficients can be calculated by the backslash operator.
>> format long
>> T = [313.15 363.15 413.15]';
>> A = [T.^2 T ones(3,1)];
>> b = [8.2583 7.4799 6.9284]';
>> x = A\b x =
0.000045380000000 -0.046258494000000 18.294051973049942
We can see that the entropy of saturated steam can be represented as S T
( )
=0 00004538. T2-0 0462585. T+18 294052. At T = 373.15 K, the entropy is calculated as>> s = x(1)*373.15^2 + x(2)*373.15+x(3) s =
7.351448
From the steam table, we obtain s = 7.3554 kJ/(kg · K) at T = 373.15 K. Thus, we can see that the entropy of the saturated steam can be adequately estimated by the 2nd-order polynomial.
2.4.2 usEofthE functionpolyfit
The built-in function polyfit can be implemented as p = polyfit(x, y, n)
where p is a vector of coefficients sorted in descending powers, x is a vector of independent vari- ables, y is a vector of dependent variables, and n is the order of the polynomial. When using polyfit, the number of data points should be greater than that of polynomial coefficients.
Function values at intermediate points can be calculated using the built-in function polyval. For example, the function value at an intermediate point z can be found by
f = polyval(p, z)
where p is the vector of coefficients sorted in descending powers, as determined by the function polyfit. f and z may be vectors.
Example 2.20: Polynomial Regression by polyfit
Table 2.12 presents the enthalpy of saturated steam versus temperature. Determine a 2nd-order polynomial that fits this enthalpy data, and estimate the enthalpy at T = 373.15 K using the polynomial.
Solution
The following commands find coefficients of the 2nd-order polynomial:
>> format long
>> T = [283.15 303.15 323.15 363.15 393.15 413.15];
>> H = [2519.9 2556.4 2592.2 2660.1 2706.0 2733.1];
>> p = polyfit(T, H, 2)
>> Tv = 280:0.1:415; Hv = polyval(p,Tv);
>> plot(Tv,Hv,T,H,'o'), legend('2nd-order interpolation','Steam table')
>> xlabel('T(K)'), ylabel('H(kJ/kg)') p =
1.0e+03 *
-0.000002096326670 0.003108694505382 1.807163153926139
We can see that the enthalpy of the saturated steam shown in Table 2.12 can be represented by the quadratic polynomial
H T
( )
= -0 002096327. T2+3 108695. T+1807 163154. The graph of this curve and the enthalpy data points are shown in Figure 2.8.The enthalpy of the saturated steam at T = 350.15 K can be obtained as
>> f = polyval(p, 350.15) f =
2.638652356432598e+03
We have H = 2638.65 kJ/kg. From the steam table, we get H = 2638.7 kJ/kg at T = 350.15 K. Thus, we can see that 2nd-order polynomials can adequately estimate the enthalpy of the saturated steam.
TABLE 2.12
Enthalpy of Saturated Steam
T (K) Enthalpy (kJ/kg)
283.15 2519.9
303.15 2556.4
323.15 2592.2
363.15 2660.1
393.15 2706.0
413.15 2733.1
2nd-order interpolation Steam table
2500280 2550 2600 2650 2700 2750
300 320 340 360
T (K)
H (kJ/kg)
380 400 420
FIGURE 2.8 Interpolation of the enthalpy value.
2.4.3 cubic sPlinE intErPolation
We assume that n data points are given: (x1, y1), (x2, y2), …, (xn, yn). We also assume that x1 < x2 < ⋯ < xn. The cubic polynomial Pi(x) on the interval xi≤x ≤xi+1 is given by
P x a x x
h a x x
h b x x c x x
i i i
i
i i
i
i i i i
( )
=(
+1-)
3+ +1(
-)
3+(
+1-)
+(
-)
where hi = xi+1 − xi. In this relation, bi and ci can be represented in terms of ai as b y
h a h c y
h a h
i i
i
i i i i
i
i i
= - , = +1- +1
The n − 2 unknown parameters ai(i = 2, 3, …, n − 1) can be obtained by solving the following n − 2 equations:
h a h h a h a y y
h
y y
h i
i i i i i i i i i
i
i i
i
+
(
+ +)
+ + + + = + - + - - =+
2 1 1 1 2 2 1 + 1 2
1
1
(
, ,¼¼ -,n 2)
with a1 = an = 0.
MATLAB® provides the built-in function spline to perform cubic spline calculations. The basic calling syntax is
yi = spline(x, y, xi)
where x and y are vectors of independent and dependent variables, respectively, and yi is the vector of the values of the interpolant at xi.
Example 2.21: Cubic Spline Interpolation
Table 2.13 shows experimental data on a pressure drop (kPa) according to flow rates (liter/sec) in a filter. Perform cubic spline interpolation using the built-in function spline.
Solution
The following code produces the graph of the interpolation and the data points shown in Figure 2.9.
>> x = [0 9.7 15.6 21.3 31.7 35.2 38.4 42.9];
>> y = [0 0.28 0.524 0.998 1.695 2.306 2.781 3.205];
>> xi = min(x):0.1:max(x); yi = spline(x,y,xi);
TABLE 2.13
Pressure Drop versus Flow Rates
Flow Rate (liter/sec) Pressure Drop (kPa) Flow Rate (liter/sec) Pressure Drop (kPa)
0 0 31.7 1.695
9.7 0.28 35.2 2.306
15.6 0.524 38.4 2.781
21.3 0.998 42.9 3.205
>> plot(xi,yi,x,y,'o'), xlabel('Flow rate(l/s)'), ylabel('Pressure drop(kPa)')
>> legend('Cubic spline interpolation','Experimental data','Location','Best')
2.4.4 intErPolationin onE dimEnsion
For one-dimensional data, the vector of an independent variable x must be monotonically increasing or decreasing. MATLAB®’s built-in function interp1 finds the interpolated value yi for the specified value xi (xi and yi are vectors), based on data given in the vectors x and y. The calling syntax is yi = interp1(x, y, xi, 'method')
where 'method' is the desired interpolation method, and the default method is linear interpolation.
MATLAB provides several methods as shown in Table 2.14. These methods can also be used in two- and three-dimensional interpolation except pchip.
As with cubic splines, the pchip method uses cubic polynomials to connect data points with continuous first derivatives. In this method, the second derivatives are not necessarily continuous.
Cubic spline interpolation Experimental data
00 0.5 1 1.5 2 2.5 3 3.5
5 10 15
Flow rate (liter/sec)
Pressure drop (kPa)
20 25 30 35 40 45
FIGURE 2.9 Cubic spline interpolation.
TABLE 2.14
Interpolation Methods Used in the Function interp1
Method Features
Nearest Nearest neighbor interpolation. This method sets the value of an interpolated point to the value of the nearest existing data point.
Linear Linear interpolation. This method uses straight lines to connect the points.
Spline Piecewise cubic spline. This is identical to the spline function.
pchip Piecewise cubic Hermite interpolation.
Example 2.22: One-Dimensional Interpolation
Determine y at x = 0.45 using the benzene–toluene equilibrium data shown in Table 2.15.
Try various interpolation methods.
Solution
The commands to implement one-dimensional interpolation are
>> x = [0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1.0];
>> y = [0.00 0.21 0.38 0.51 0.62 0.71 0.79 0.86 0.91 0.96 1.00];
>> lv = interp1(x,y,0.45,'linear');
>> pv = interp1(x,y,0.45,'pchip');
>> sv = interp1(x,y,0.45,'spline');
>> nv = interp1(x,y,0.45,'nearest');
>> fprintf('\nlinear: %6.4f\npchip: %6.4f\nspline: %6.4f\nnearest: %6.4f\n', lv,pv,sv,nv);
linear: 0.6650 pchip: 0.6668 spline: 0.6670 nearest: 0.7100
Example 2.23: One-Dimensional Fitting
Table 2.16 shows the time series of measurements of reaction temperature. Use MATLAB®’s built- in function interp1 to fit these data with the pchip (piecewise cubic Hermite) option.
Solution
The commands to implement and plot the pchip interpolation shown in Figure 2.10 are
>> tm = [0 18 42 55 70 82 86 95 102 115];
>> TC = [15 23 24 36 78 80 98 96 127 126];
>> tinv = linspace(0,115);
>> yp = interp1(tm, TC, tinv, 'pchip');
>> plot(tm,TC,'o',tinv,yp), xlabel('Time(min)'), ylabel('Temperature(deg.C)')
>> axis([0 115 0 130]), title('cubic Hermite')
TABLE 2.15
Benzene–Toluene Equilibrium Data at 1 atm
x 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
y 0.00 0.21 0.38 0.51 0.62 0.71 0.79 0.86 0.91 0.96 1.00
TABLE 2.16
Reaction Temperature versus Time
Time (min) 0 18 42 55 70 82 86 95 102 115
Temperature (°C) 15 23 24 36 78 80 98 96 127 126
2.4.5 intErPolationin multidimEnsion
Two-dimensional interpolation deals with determining intermediate values for functions of two independent variables z = f(xi, yi). Suppose that we have values at four different points: z11 = f(x1, y1), z21 = f(x2, y1), z12 = f(x1, y2), z22 = f(x2, y2). Let’s interpolate between these points to estimate the func- tion value z = f(xi, yi) at an intermediate point (xi, yi) (x1≤xi≤x2, y1≤yi≤y2). Using the Lagrange interpolation method, the function value at (xi, y1) is
z f x y x x
x x z x x x x z
i i i i
1 1 2
1 2
11 1
2 1
=
(
,)
= -- + -- 21and at (xi, y2) is
z f x y x x
x x z x x x x z
i i i i
2 2 2
1 2
12 1
2 1
=
(
,)
= -- + -- 22These points can then be used to linearly interpolate along the y-axis to yield z f x y y y
y y z y y y y z x x
x x y y y y
i i i
i i
i
i i
=
( )
= -- + --= - -
- -
, 2
1 2
1 1
2 1
2
2
1 2
2 1 22
11 1
2 1
2
1 2
21 2
1 2
1
2 1
z x x 12
x x y y
y y z x x x x
y y
y y z x x
i i i i i
+ - -
-
- + -
- -
- + - 11
2 1
1
2 1
x x 22
y y yi y z -
- -
The MATLAB®’s built-in function interp2 implements two-dimensional piecewise interpolation.
A simple syntax representation of interp2 is
zi = interp2(x, y, z, xi, yi, 'method')
where x and y are matrices containing coordinates of the points at which the values in matrix z are given, zi is a matrix containing the results of the interpolation as evaluated at the points in the
00 20 40 60 80 100 120
20 40
Cubic Hermite
Time (min)
60 80 100
Temperature (°C)
FIGURE 2.10 One-dimensional interpolation (use of interp1).
matrices xi and yi, and 'method' is the desired interpolation method. The methods are identical to those used by interp1, that is, nearest, linear, and spline. As with interp1, if the 'method' argument is omitted, the default is linear interpolation.
Three-dimensional interpolation deals with determining intermediate values for functions of three independent variables v = f(x, y, z). The MATLAB’s built-in function interp3 implements three- dimensional piecewise interpolation. The calling syntax of interp3 is
Vi = interp3(X, Y, Z, V, Xi, Yi, Zi)
where X, Y, and Z are matrices containing the coordinates of the points at which the values in the matrix V are given; Vi is a matrix containing the results of the interpolation as evaluated at the points in the matrices Xi, Yi, and Zi; and 'method' is the desired interpolation method.
Example 2.24: Two-Dimensional Interpolation
Temperatures are measured at various points on a heated metal plate (Table 2.17). Estimate the temperature at xi = 6.4 and yi = 5.2 using two-dimensional piecewise cubic spline interpolation.
Solution
The commands to implement two-dimensional piecewise cubic spline interpolation are
>> x = [2 10]; y = [1 8]; z = [80 78; 75 90];
>> zi = interp2(x, y, z, 6.4, 5.2, 'spline') zi =
81.5100
Example 2.25: Interpolation of Humidity and Dew Point
Table 2.18 presents the absolute humidity (H) and the dew point (DP) of the air as a function of relative humidity (RH). Estimate H and DP when RH = 58.4 and T (dry bulb temperature) = 46.8°C using two-dimensional piecewise cubic spline interpolation.
TABLE 2.17
Temperatures at Various Points on a Heated Metal Plate
x y T = f(x, y)
2 1 80
2 8 75
10 1 78
10 8 90
TABLE 2.18
Absolute Humidity and Dew Point
Relative Humidity (%RH) 10% 30% 50% 70% 90%
Dry bulb (°C)
51 H, g/m3 8.27 24.75 41.30 58.10 73.51
DP, °C 10.10 26.89 37.01 43.21 47.80
44 H, g/m3 6.52 19.58 32.70 45.75 58.78
DP, °C 6.40 23.34 32.18 38.32 42.81
Solution
The commands to implement two-dimensional spline interpolation are
>> RH = [10 30 50 70 90]; T = [51 44];
>> H = [8.27 24.75 41.30 58.10 73.51; 6.52 19.58 32.70 45.75 58.78];
>> DP = [10.10 26.89 37.01 43.21 47.80; 6.40 23.34 32.18 38.32 42.81];
>> Hv = interp2(RH,T,H,58.4,46.8,'spline')
>> DPv = interp2(RH,T,DP,58.4,46.8,'spline') Hv =
42.2701 DPv = 36.9549
We can see that the absolute humidity is 42.27 g/m3 and the dew point is 36.95°C when the rela- tive humidity is 58.4% and the dry bulb temperature is 46.8°C.