CASE STUDY HEAT TRANSFER CASE STUDY continued

18.7 CASE STUDY continued

Solution. As just described, we want to use natural spline end conditions to perform this analysis. Unfortunately, because it uses not-a-knot end conditions, the built-in MATLAB spline function does not meet our needs. Further, the spline function does not return the first and second derivatives we require for our analysis. However, it is not difficult to develop our own M-file to implement a natural spline and return the derivatives. Such a code is shown in Fig. 18.12. After some preliminary error trapping, we set up and solve Eq. 18.27 for the second-order coefficients c. Notice how 18.7 CASE STUDY 453 function [yy,dy,d2] = natsplinex,y,xx natspline: natural spline with differentiation [yy,dy,d2] = natsplinex,y,xx: uses a natural cubic spline interpolation to find yy, the values of the underlying function y at the points in the vector xx. The vector x specifies the points at which the data y is given. input: x = vector of independent variables y = vector of dependent variables xx = vector of desired values of dependent variables output: yy = interpolated values at xx dy = first derivatives at xx d2 = second derivatives at xx n = lengthx; if lengthy~=n, errorx and y must be same length; end if anydiffx=0,errorx not strictly ascending,end m = lengthxx; b = zerosn,n; aa1,1 = 1; aan,n = 1; set up Eq. 18.27 bb1=0; bbn=0; for i = 2:n-1 aai,i-1 = hx, i - 1; aai,i = 2 hx, i - 1 + hx, i; aai,i+1 = hx, i; bbi = 3 fdi + 1, i, x, y - fdi, i - 1, x, y; end c=aa\bb; solve for c coefficients for i = 1:n - 1 solve for a, b and d coefficients ai = yi; bi = fdi + 1, i, x, y - hx, i 3 2 ci + ci + 1; di = ci + 1 - ci 3 hx, i; end continued FIGURE 18.12 M-file to determine intermediate values and derivatives with a natural spline. Note that the diff function employed for error trapping is described in Section 21 . 7 . 1 .

18.7 CASE STUDY continued

for i = 1:m perform interpolations at desired values [yyi,dyi,d2i] = SplineInterpx, n, a, b, c, d, xxi; end end function hh = hx, i hh = xi + 1 - xi; end function fdd = fdi, j, x, y fdd = yi - yj xi - xj; end function [yyy,dyy,d2y]=SplineInterpx, n, a, b, c, d, xi for ii = 1:n - 1 if xi = xii - 0.000001 xi = xii + 1 + 0.000001 yyy=aii+biixi-xii+ciixi-xii2+dii... xi-xii3; dyy=bii+2ciixi-xii+3diixi-xii2; d2y=2cii+6diixi-xii; break end end end FIGURE 18.12 Continued we use two subfunctions, h and fd , to compute the required finite differences. Once Eq. 18.27 is set up, we solve for the c’s with back division. A loop is then employed to generate the other coefficients a , b , and d . At this point, we have all we need to generate intermediate values with the cubic equation: f x = a i + b i x − x i + c i x − x i 2 + d i x − x i 3 We can also determine the first and second derivatives by differentiating this equation twice to give f ′ x = b i + 2c i x − x i + 3d i x − x i 2 f ′′ x = 2c i + 6d i x − x i As in Fig. 18.12, these equations can then be implemented in another subfunction, SplineInterp , to determine the values and the derivatives at the desired intermediate values. Here is a script file that uses the natspline function to generate the spline and create plots of the results: z = [0 2.3 4.9 9.1 13.7 18.3 22.9 27.2]; T=[22.8 22.8 22.8 20.6 13.9 11.7 11.1 11.1]; zz = linspacez1,zlengthz;

18.7 CASE STUDY continued