Part 5 Chapter 19 - CAU

Transcription

Part 5Chapter 19Numerical DifferentiationProf. HaeHae--Jin Choihjchoi@cau.ac.krNumerical Methods 2010-21School of Mechanical EngineeringChung-Ang University

Chapter Objectivesl Understanding the application of high-accuracy numericaldifferentiation formulas for equispaced data.l Knowing how to evaluate derivatives for unequally spaced data.l Understanding how Richardson extrapolation is applied fornumerical differentiation.l Recognizing the sensitivity of numerical differentiation to dataerror.l Knowing how to evaluate derivatives in MATLAB with the diff andgradient functions.l Knowing how to generate contour plots and vector fields withMATLAB.Numerical Methods 2010-22School of Mechanical EngineeringChung-Ang University

Introduction to DifferentiationThe one dimensional forms of some constitutive laws commonly lconstatFourier’slawq -kdTdxHeat �slawJ -DdcdxMass wq -kdhdxFlow throughporous mediaHeadFlowHydraulicconductivityOhm’slawJ -sdVdxCurrent viscositylawt ke’slaws erical Methods 2010-23School of Mechanical EngineeringChung-Ang University

Differentiationl The mathematical definition of a derivative begins with adifference approximation:Dy f (xi Dx) - f (xi ) DxDxand as Δx is allowed to approach zero, the differencebecomes a derivative:f (xi Dx) - f (xi )dy limdx Dx 0Dx Numerical Methods 2010-24School of Mechanical EngineeringChung-Ang University

High--Accuracy DifferentiationHighFormulasl Taylor series expansion can be used togenerate high-accuracy formulas forderivatives by using linear algebra tocombine the expansion around severalpoints.l Three categories for the formula includeforward finite-difference, backward finitedifference, and centered finite-difference.Numerical Methods 2010-25School of Mechanical EngineeringChung-Ang University

Differentiation derivedfrom Taylor series expansionsllThere are forward difference, backward difference and centereddifference approximations, depending on the points used:Forward:f (x i 1 ) - f (x i )f (x i ) O(h)h'lBackward:f (x i ) - f (x i-1 )f (x i ) O(h)h' lCentered:f (x i 1 ) - f (x i-1 )f (x i ) O(h 2 )2h' Numerical Methods 2010-26School of Mechanical EngineeringChung-Ang University

High Accuracy Differentiationl Forward Taylor series expansionf ( xi ) 2f ( xi 1 ) f ( xi ) f ( xi )h h L2!f ( xi ) f ( xi 1 ) - f ( xi ) f ( xi )h O(h 2 )h2!l Forward-difference approximation of 1st derivative excluding thesecond and higher derivative term (In chapter 4)f ( xi 1 ) - f ( xi ) f ( xi ) O ( h)hl Forward-difference approximation of 2nd derivativef ( xi ) f ( xi 2 ) - 2 f ( xi 1 ) f ( xi ) O ( h)2hNumerical Methods 2010-27School of Mechanical EngineeringChung-Ang University

High Accuracy Differentiationl Forward-difference approximation of 1st derivative including2nd derivative termf ( xi ) f ( xi 1 ) - f ( xi ) f ( xi 2 ) - 2 f ( xi 1 ) f ( xi )2h O(h)2h2hf ( xi ) - f ( xi 2 ) 4 f ( xi 1 ) - 3 f ( xi ) O(h 2 )2hl Notice that inclusion of second-derivative term hasimproved the accuracy to O(h2) .Numerical Methods 2010-28School of Mechanical EngineeringChung-Ang University

Forward FiniteFinite-DifferenceNumerical Methods 2010-29School of Mechanical EngineeringChung-Ang University

Backward FiniteFinite-DifferenceNumerical Methods 2010-210School of Mechanical EngineeringChung-Ang University

Centered FiniteFinite-DifferenceNumerical Methods 2010-211School of Mechanical EngineeringChung-Ang University

Example 19.1 (1/2)l Q. Recall that at in Ex. 4.4 we estimated the derivative of f(x) atx 0.5 using forward differences and a step size of h 0.25. Theresults are summarized in the table below. The exact value off’(0.5) -0.9125.f ( x) -0.1x 4 - 0.15 x 3 - 0.5 x 2 - 0.25 x 1.2Backward O(h) Centered O(h2)Forward O(h)Estimate-0.714-0.934-1.155et21.7%-2.4%-26.5%l Repeat the computation with high accuracy formulas.Numerical Methods 2010-212School of Mechanical EngineeringChung-Ang University

Example 19.1 (2/2)l Sol) xi -2 0f ( xi - 2 ) 1.2xi -1 0.25f ( xi -1 ) 1.1035156xi 0.5f ( xi ) 0.925xi 1 0.75f ( xi 1 ) 0.6363281xi 2 1f ( xi 2 ) 0.2l Forward difference of O(h2) is computed asf (0.5) -0.2 4(0.6363281) - 3(0.925) -0.8593752(0.25)e t 5.82 %l Backward difference of O(h2) is computed asf (0.5) 3(0.925) - 4(1.1035156) 1.2 -0.8781252(0.25)e t 3.77 %l Backward difference of O(h4) is computed asf (0.5) -0.2 8(0.6363281) - 8(1.1035156) 1.2 -0.912512(0.25)Numerical Methods 2010-213e t 0 %School of Mechanical EngineeringChung-Ang University

Richardson Extrapolationl As with integration, the Richardson extrapolation can be used to combinetwo lower-accuracy estimates of the derivative to produce a higheraccuracy estimate.l For the cases where there are two O(h2) estimates and the interval ishalved (h2 h1/2), an improved O(h4) estimate may be formed using:41D D(h2 ) - D(h1 )33l For the cases where there are two O(h4) estimates and the interval ishalved (h2 h1/2), an improved O(h6) estimate may be formed using: 161D D(h2 ) - D(h1 )1515l For the cases where there are two O(h6) estimates and the interval ishalved (h2 h1/2), an improved O(h8) estimate may be formed using: Numerical Methods 2010-2641D D(h2 ) - D(h1 )636314School of Mechanical EngineeringChung-Ang University

Example 19.2l Q. Using the same function as in Ex.19.1, estimate the firstderivative at x 0.5 for a step size of h1 0.5, and h2 0.25. Usethe Richardson extrapolation to compute improved estimate.The exact solution is -0.9125.f ( x) -0.1x 4 - 0.15 x 3 - 0.5 x 2 - 0.25 x 1.2Sol.) The first derivative with centered differencexi - 2 0xi -1 0.250.2 - 1.2D (0.5) -1.0e t - 9.6%xi 0.510.6363281 - 1.103516D (0.25) -0.934375 e t - 2.4% xi 1 0.750.5xi 2 1f ( xi - 2 ) 1.2f ( xi -1 ) 1.1035156f ( xi ) 0.925f ( xi 1 ) 0.6363281f ( xi 2 ) 0.2Using the Richardson extrapolation, the improved Estimate isD 41(-0.934375) - (-1) -0.912533Numerical Methods 2010-215School of Mechanical EngineeringChung-Ang University

Unequally Spaced Data One way to calculated derivatives ofunequally spaced data is to determine apolynomial fit and take its derivative at apoint. As an example, using a second-orderLagrange polynomial to fit three points andtaking its derivative yields:f (x) f (x0 ) 2x - x1 - x22x - x0 - x22x - x0 - x1 f (x1 ) f (x2 )(x0 - x1 )(x0 - x2 )(x1 - x0 )(x1 - x2 )(x2 - x0 )(x2 - x1 )Numerical Methods 2010-216School of Mechanical EngineeringChung-Ang University

Example 19.3l A temperature is measured inside the soil as shown below.Compute the heat flux into the ground at the air-soil interface.dTq ( z 0) - kdzz 0where q(x) heat flux (W/m2),k thermal conductivity for soil ( 0.5 W/(m·K),T Temperature(K),z distance measured from the surfaceinto the soil.f ( 0 ) 13.5 102(0) - 0.0125 - 0.03752(0) - 0 - 0.0375 12( 0 - 0.0125)( 0 - 0.0375)( 0.0125 - 0 )( 0.0125 - 0.0375)2(0) - 0 - 0.0125 -1440 1440 - 133.333 -133.333 K / m( 0.0375 - 0 )( 0.0375 - 0.0125)q ( z 0) -0.5Numerical Methods 2010-2W æWöW133.333 66.667ç mKèmøm217School of Mechanical EngineeringChung-Ang University

Derivatives and Integrals forData with Errors A shortcoming of numerical differentiation is that it tendsto amplify errors in data, whereas integration tends tosmooth data errors. One approach for taking derivatives of data with errors isto fit a smooth, differentiable function to the data andtake the derivative of the function.(a) Data with no error(b) Resulting numericaldifferentiation of curve (a)(c) Data modified slightly(d) Resulting numericaldifferentiation of curve (a)à Small data errors are amplifiedby numerical differentiation.Numerical Methods 2010-218School of Mechanical EngineeringChung-Ang University

Numerical Differentiation withMATLAB MATLAB has two built-in functions to help take derivatives, diffand gradient: diff(x)– Returns the difference between adjacent elements in x f @(x) 0.2 25*x-200*x. 2 675*x. 3-900*x. 4 400*x. 5; x 0 : 0.1 : 0.8 ; y f(x) ; diff(x)ans Columns 10.1000Columns 60.1000Numerical Methods 2010-2through0.1000through0.100050.1000 0.1000 0.100080.100019School of Mechanical EngineeringChung-Ang University

Numerical Differentiation withMATLAB diff(y)./diff(x)– Returns the difference between adjacent values in y dividedby the corresponding difference in adjacent values of x d diff(y)./diff(x)Columns 1 through 510.8900-0.01003.1900 8.4900 8.6900Columns 6 through 81.3900 -11.0100 -21.3100 n length(x); xm (x(1:n-1) x(2:n))./2;% vector d contains derivative estimates corresponding to the% midpoint between adjacent elements xa 0: .01 : .8 ; ya 25-400*xa 3*675*xa. 2-4*900*xa. 3 5*400*xa. 4; xplot(xm, d, 'o', xa, ya)Numerical Methods 2010-220School of Mechanical EngineeringChung-Ang University

Numerical Methods 2010-221School of Mechanical EngineeringChung-Ang University

Numerical Differentiation withMATLAB fx gradient(f, h)Determines the derivative of the data in f at each of the points.The program uses forward difference for the first point, backwarddifference for the last point, and centered difference for theinterior points. h is the spacing between points; if omitted h 1. The major advantage of gradient over diff is gradient’s result isthe same size as the original data. Gradient can also be used to find partial derivatives for matrices:[fx, fy] gradient(f, h) dy gradient(y, 0.1)dy Columns 1 through 510.89005.44001.59005.8400Columns 6 through 85.0400 -4.8100 -16.1600 -21.3100Numerical Methods 2010-2228.5900School of Mechanical EngineeringChung-Ang University

xa 0: .01 : .8 ; ya 25-400*xa 3*675*xa. 2-4*900*xa. 3 5*400*xa. 4; xplot(x, dy, 'o', xa, ya)Numerical Methods 2010-223School of Mechanical EngineeringChung-Ang University

Data with Errors 18 A shortcoming of numerical differentiation is that it tends to amplify errors in data, whereas integration tends to smooth data errors. One approach for taking derivatives of data with errors is to fit a smooth, differentiable function to the data and take the derivative of the function. (a)Data with no error