AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD Mathca Release 13 Khyruin Akbar Ansari, Ph.D., P.E. Professor of Mechanical Engineering School of Engineering Gonzaga University SDC PUBLICATIONS Schroff Development Corporation www.schroff.com www.schroff-europe.com
Chapter 2: Introuction to Numerical Methos 43 C H A P T E R 2 INTRODUCTION TO NUMERICAL METHODS 2.1 THE USE OF NUMERICAL METHODS IN SCIENCE AND ENGINEERING Analysis of problems in engineering an the physical sciences typically involves four steps as follows. (1) Development of a suitable mathematical moel that realistically represents a given physical system. (2) Derivation of the system governing equations using physical laws such as Newton's laws of motion, conservation of energy, the laws governing electrical circuits etc. (3) Solution of the governing equations, an (4) Interpretation of the results. Because real worl problems are generally quite complex with the generation of close-form analytical solutions becoming impossible in many situations, there exists, most efinitely, a nee for the proper utilization of computer-base techniques in the solution of practical problems. The avancement of computer technology has mae the effective use of numerical methos an computer-base techniques very feasible, an thus, solutions can now be obtaine much faster than ever before an with much better than acceptable accuracy. However, there are avantages as well as isavantages associate with any numerical proceure that is resorte to, an these must be kept in min when using it. 2.2 COMPARISON OF NUMERICAL METHODS WITH ANALYTICAL METHODS While an analytical solution will be exact if it exists, a numerical metho, on the other han, will generally require iterations to generate a solution, which is only an approximation an which certainly cannot be consiere exact by any means. A isavantage associate with analytical solution techniques is that they are generally applicable only to very special cases of problems. Numerical solutions, on the contrary, will solve complex situations as well. While numerical techniques have several avantages incluing easy programming on a computer an the convenience with which they hanle complex problems, the initial estimate of the solution along with the many number of iterations that are sometimes require to generate a solution can be looke upon as isavantages.
44 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD 2.3 SOURCES OF NUMERICAL ERRORS AND THEIR COMPUTATION It is inee possible for miscalculations to creep into a numerical solution because of various sources of error. These inclue inaccurate mathematical moeling, wrong programming, wrong input, rouning off of numbers an truncation of an infinite series. Roun-off error is the general name given to inaccuracies that affect the calculation scene when a finite number of igits are assigne to represent an actual number. In a long sequence of calculations, this roun-off error can accumulate, then propagate through the process of calculation an finally grow very rapily to a significant number. A truncation error results when an infinite series is approximate by a finite number of terms, an, typically, upper bouns are place on the size of this error. The true error is efine as the ifference between the compute value an the true value of a number. E True = X Comp X True (2.1) while the relative true error is the error relative to the true value X Comp X True e r = X True (2.2) Expresse as a percentage, the relative true error is written as X Comp X True e r = 1 (%) (2.3) X True 2.4 TAYLOR SERIES EXPANSION The Taylor series is consiere as a basis of approximation in numerical analysis. If the value of a function of x is provie at " x ", then the Taylor series provies a means of evaluating the function at " x + h", where " x " is the starting value of the inepenent variable an " h " is the ifference between the starting value an the new value at which the function is to be approximate ( ) = fx ( ) + h fx + h ( ) fx x + h 2 2 2! 2 x fx ( ) h 3 3 + fx ( 3! 3 ) +... x (2.4) This equation can be use for generating various s of approximations as shown below. The of approximation is efine by the highest erivative inclue in the series. For example, If only terms up to the secon erivative are retaine in the series, the result is a secon approximation.
Chapter 2: Introuction to Numerical Methos 4 First approximation: ( ) = fx ( ) + h fx + h x fx ( ) (2.) Secon approximation: ( ) = fx ( ) + h fx + h x fx ( ) + h 2 2 2! 2 x fx ( ) (2.6) Thir approximation: ( ) = fx ( ) + h fx + h x fx ( ) + h 2 2 2! 2 x fx ( ) + h 3 3 3! 3 x fx ( ) (2.7) It is to note that the significance of the higher terms in the Taylor series increases with the nonlinearity of the function involve as well as the ifference between the " starting x" value an the "x" value at which the function is to be approximate. Thus, the fewer the terms that are inclue in the series, the larger will be the error associate with the computation of the function value. If the function is linear, however, only terms up to the first erivative term nee to be inclue. Example 2.1 Using the Taylor series expansion for f(x) = -.1 x 4 -.17 x 3 -.2 x 2 -.2 x + etermine the zeroth, first, secon, thir, fourth an fifth approximations of f(x + h ) where x = an h = 1,2, 3, 4, an compare these with the exact solutions. h=1.: Put in the function an generate its erivatives: fx ( ) :=.1 x 4.17 x 3.2 x 2.2x + x := h:= 1. fprime( x) :=.6x 3.1x 2.x.2 <--Generate erivatives f2prime( x) := 1.8x 2 1.2x. f3prime( x) := 3.6x 1.2 f4prime( x) := 3.6 fprime( x) :=.
46 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD h 2 term1 := f( x) term2 := hfprime( x) term3 := f2prime x 2 ( ) h 3 term4 := f3prime x 6 ( ) term h 4 := f4prime x 24 ( ) term6 h := fprime x 12 ( ) ftaylor := term1 <---- one-term or zero- approximation ftaylor1 := term1 + term2 <---- first approximation with two terms ftaylor2 := term1 + term2 + term3 <---secon approximation with 3 terms ftaylor3 := term1 + term2 + term3 + term4 <---thir approximation with 4 terms ftaylor4 := term1 + term2 + term3 + term4 + term <---fourth approximation with terms ftaylor := term1 + term2 + term3 + term4 + term + term6 <----- fifth approximation with 6 terms x := x + h x = 1 ftaylor = ftaylor1 = 1 ftaylor2.7 ftaylor3 =.8 ftaylor4 =.43 ftaylor =.43 = These are the zero- fifth <-- approximations of the given function f(x) using the Taylor series. f1 ( ) =.43 <---EXACT ANSWER USING FUNCTION GIVEN. err := f( x) ftaylor err =.82 err1 := f( x) ftaylor1 err1 =.7 err2 := f( x) ftaylor2 err2 =.32 These are errors ( ifferences between exact <-- values an approximations ) for the above zero - fifth approximations. err3 := f( x) ftaylor3 err3 =.1 err4 := f( x) ftaylor4 err4 = err := f( x) ftaylor err = Similarly, by using h= 2, 3, 4,, the zeroth- fifth approximations for f(2), f(3), f(4), f() an the associate errors can be etermine. These are given in Tables 2.1 an 2.2 Plots of the various Taylor series approximations of the given function an associate errors are generate below an are presente in Figs. 2.1 an 2.2
Chapter 2: Introuction to Numerical Methos 47 x := x :=,.1.. ftaylor( x) := fx ( ) <-- zeroth- approximation ftaylor1( x) := ftaylor( x) + ( x x) fprime( x) <---first- approximation ( x x) 2 ftaylor2( x) := ftaylor1( x) + f2prime( x) <--secon- approximation 2 ( x x) 3 ftaylor3( x) := ftaylor2( x) + f3prime( x) <---thir- approximation 6 ( x x) 4 ftaylor4( x) := ftaylor3( x) + f4prime( x) <---fourth- approximation 24 ( x x) ftaylor ( x) := ftaylor4( x) + fprime( x) <---fifth- approximation 12 Errors generate with the various approximations are as follows Zero approximation: err( x) := fx ( ) ftaylor( x) First approximation: err1( x) := fx ( ) ftaylor1( x) Secon approximation: err2( x) := fx ( ) ftaylor2( x) Thir approximation: err3( x) := fx ( ) ftaylor3( x) Fourth approximation: err4( x) := fx ( ) ftaylor4( x) Fifth approximation: err ( x) := fx ( ) ftaylor ( x) The various approximations generate by the above calculations an the associate errors are compare in Table 2.1.
48 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD Taylor series approx of given function Function approximate by Taylor series ftaylor( x) ftaylor3( x) ftaylor( x) 1 Zeroth approx Thir approx Fifth approx/ given function 1 1 2 3 4 x x- value Figure 2.1. Taylor series approximation of given function x :=,.1.. Errors gen ue to Taylor-series approx errors as function of x err( x) err3( x) err( x) 1 Zero approx Thir approx Fifth approx 1 1 2 3 4 x x- value Figure 2.2. Errors associate with the various Taylor series approximations
Chapter 2: Introuction to Numerical Methos 49 The various approximations generate by the above calculations an the associate errors are compare in the following tables. Table 2.1 h := 1.. x :=, 1.. Various s of approximation generate by Taylor series approach versus true values of given function zeroth first secon thir fourth fifth True Value h = 1 2 3 4 x = ftaylor( x) 1 2 3 4 ftaylor1 = ( x) 1.7..2 =ftaylor2( x).7 -.2-1.7-3.7-6.2 ftaylor3 = ( x).8-1.61-6.34-14.63-27. =ftaylor4( x).43-4.1-18.49-3.3-12 =ftaylor ( x).43-4.1-18.49-3.3-12 =f( x) =.43-4.1-18.49-3.3-12 Table 2.2 Errors associate with the ifferent s of approximation zeroth first secon thir fourth fifth h = 1 2 3 4 x = err( x) 1 2 3 4 -.82 -.26-19.74-4.28-122. = err1( x) -.7-4.76-18.99-3.28-12 = err2( x) -.32-3.76-16.74-49.28-11 = err3( x) -.1-2.4-12.1-38.4-93.7 = err4( x) = err ( x) = PROBLEMS 2.1. Using the Taylor series expansion for cos x, which is given as f(x) = cos x = 1- x 2 / 2 + x 4 / 24, etermine the one-term, two-term an three-term approximations of f(x + h ), where x =
AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD ra an h =. 1,.2...1. ra, an compare these with the exact solution. Using Mathca, generate plots of the various Taylor series approximations an associate errors as functions of the inepenent variable x. 2.2 Develop a Taylor series expansion of the following function: f(x) = x - 6 x 4 + 3x 2 + 9. Use x =3 as the base an h as the increment. Using Mathca, evaluate the series for h=.1,.2...1., aing terms incrementally as in Problem 2.1. Compare the various Taylor series approximations obtaine with true values in a table. Generate plots of the approximations an associate errors as functions of x. 2.3 Given the following function: f(x) = x 3-3 x 2 + x + 1, etermine f ( x + h ) with the help of a Taylor series expansion, where x = 2 an h =.4. Compare the true value of f ( 2.4 ) with estimates obtaine by resorting to (a) one term only (b) two terms (c) three terms an () four terms of the series. 2.4 Given the following function fx ( ) = 3x 3 6x 2 + 1 x + 2 use a Taylor series expansion to etermine the zeroth, first, secon an thir approximations of f(x +h) where x = 2 an h =.. Compare these with the exact solution. 2. By eveloping a Taylor series expansion for f(x) = e x about x =, etermine the fourth- approximation of e 2. an compare it with the exact solution. 2.6. By eveloping a Taylor series expansion for f(x) = ln(2-x) about x =, etermine the fourth- approximation of ln (.) an compare it with the exact solution
Chapter 2: Introuction to Numerical Methos 1 2.7. By eveloping a Taylor series expansion for f(x) = x 3 e - x about x = 1, etermine the thir- approximation of f(1.2) an compare it with the exact solution. 2.8. By eveloping a Taylor series expansion for f(x) = e cos x about x =, etermine the fourth- approximation of f (2π) an compare it with the exact solution.. 2.9. By eveloping a Taylor series expansion for f(x) = (x - 2) 1/2 about x = 3, etermine the thir- approximation of f (2.2), that is, (.2) 1/2, an compare it with the exact solution. 2.1. Given the function f(x) = x 2 - x. + 6, use a Taylor series expansion to etermine the first, secon, thir an fourth approximations of f (2. ) by resorting to x = 2 an h =.. Compare these with the exact solution. 2.11. Given the function f(x) = 6 x 3-9 x 2 +2 x + 4, use a Taylor series expansion to etermine the zeroth, first, secon an thir approximations of f (x + h ) where x = 3 an h = 1. Compare these with the exact solution. 2.12. Given the function f(x) = 4 x 4-7 x 3 + x 2-6 x + 9 use a Taylor series expansion to etermine the zeroth, first, secon, thir an fourth
2 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD approximations of f (x + h ) ) where x = 3 an h =.. Compare these with the exact solution. Calculate errors an generate calculations to three ecimal places. 2.13. Given the function f(x) = 8 x 3-1 x 2 + 2 x + 4, use a Taylor series expansion to etermine the zeroth, first, secon an thir approximations of f (x + h ) where x = 2 an h = 1. Compare these with the exact solution. 2.14. Given the function f(x) = 1 + x + x 2 / 2! + x 3 / 3! +x 4 / 4! use a Taylor series expansion to etermine the zeroth, first, secon, thir an fourth approximations of f (x + h ) where x = an h =.. Compare these with the exact solution. Generate answers correct to four ecimal places. 2.1. Given the function f(x) = x + x 3 / 3 + 2 x / 1 use a Taylor series expansion to etermine the zeroth, first, secon, thir an fourth approximations of f (x + h ) where x = an h =.8. Compare these with the exact solution by computing percentage errors. Generate answers correct to four ecimal places. 2.16. Given the function f(x) = sin (x) use a Taylor series expansion to etermine the fifth approximation of f (x + h ) where x = an h =.2 raians. Compare your answer with the true value. Generate answers correct to four ecimal places. 2.17. Given the function f(x) = 3 x 2-6 x. + 9, use a Taylor series expansion to etermine the zeroth, first, secon, thir an fourth approximations of f (x + h ) whereo x = 3 an h = 1. Compare these with the exact solution by computing percentage errors. Generate answers correct to four ecimal places.