You are on page 1of 920
Mathes or Eines, Fear tee atace oe) engonas 20 PREFACE Back in the Stone Age (actually the 1970s and early 1980s), numerical methods were pri= marily taught atthe graduate level. About 15 years ago. we began concurrently teaching the subject to sophomore ank junine students at Texas A&M and Michigan. We used newly developed “microcomputers” along with a strong focus on engineering problem solving to sake the subject accessible and exciting to our students. And it seemed (0 work, Based on our experiences, we wrote a thin silver book complete with a 5-1/4" floppy disk in the back (Chapra and Canale, 1985), Over time that book has evolved into the edi tion you are now reading. Although we've added new material and changed with the times, the present book is still founded on the same principles as Une oF ginal + Problem Orientation, Fnginecriny students learn best when they are motivated by problems, This is particularly trve for mathematics and computing. Consequently, we Ihave approached numerical methods from a problem-solving perspective, ‘= Student-Oriented Pedagogy. We have developed! a number of features to make this ook as student-friendly as possible. These include the overall organization, the use oF intcoductions and cpilogues to consolidate major topics and the extensive ase of worked examples and case siudies from all areas of engineering. We have also endeavored 10 keep our explanations straightforward and oriented practically ‘+ “Clear Box” Approach. Althoush we emphasize problem solving. we believe that itis self-limiting for engincers to approach numerical algorithms as “black boxes.” Thus. we include sufficient theory to allow users to understand the basic concepts behind the methods. In particulat. we stress theory related to error analysis. the limitations of the methods, and trade-offs among methods ‘+ Personal Computing Orientation. When we first wrote this book, there was a great chasm between the batch world of mainframe computers and the interactive world of PCs. Today. as PC performance grows. the distinctions are falling away. That said. this book sill emphasizes visualization and interactive calculations, which are the hallmarks of personal computing. ‘+ Empowering Students. We obviously introduce students to the standard “point-and- shoot” problem-solving capabilities of packages like Excel and MATLAB. However, students are also shown how to develop simple, well-structured programs to extend the base capabilities of those environments. This knowledge carties over to standard Chara-Grate-marenea Mathes or Eines, Fear tee om wae atace oe) cengrer, PREFACE: programming languages such as Fortran 90 and C/C++. We believe that the current Tight from computer programming represents something of a “dumbing down” of the enginzering curriculum. The bottom Tine is that as long as engineers are not content 19 be tool-limited, they will have to write code. Only now they may be called "macros" or “Milles.” This book is designed to empower them to do that Beyond these five original principles, the new edition has a number of new features: ‘Software Orientation, Just as the silver book capitalized on early hardware develop- ‘ments, the present book takes advantage of software development, In particular, material and problems throughout the fext are devoted to implementing numerical methods with Microsoft's Excel and Mathworks’ MATLAB, These tools place an ineredible array of ‘numerical capability athe ready disposal of any student engineer willing to climb their ‘mild and sunny learning curv «New and More Challenging Problem Sets. The primary enhancement ia the fourth edi- tion is the revision of the problems sels, including over 125 new problems, Many of the new problems are challenging and relate t exciting growth areas such as bioengineering, ‘© New Material, New sections have been added. These include a modified false position ‘method and multidimensional integration, As always, our primary intent is to provide students with a sound introduction to nu- ‘merical methods. We believe that motivated students who enjoy numerical methods, com- pers, and mathematics will, in the end, make better engineers. If our book fosters an enthusiasm for these suljects, we will consider cur efforts success Acknowledgments. Special thanks to Prof. Wally Grant, Olga Pierrakos. Amber Phillips, Justin Griffee, and Kevin Mace of Virginia Tech for developing many of the new problems in this edition, In addition, Prot, Theresa Good of Texas A&M contributed a number of excetlent new problems. As in past editions, David Clough of the University of Colorado and Jery Stedinger of Cornell University genecously shared their insights and suggestions, Useful suggestions were also made by Bill Pilot (Comell), David Hefer (The Technion, Israel), Dong-Hl Seo (Chungnam National University. Korea), Anton Krueger (owa), and Tim Ameet and Tahar Loulou (Liab. This edition has also benefited from comments and suggestions made by Toay Cahill (Texas A&MD, Lizette Chevalier «Southern tlinois University), Tim D'Orazio (San Francis State University), Charles K Roby (Clemson), Steve Salley | Wayne State University). and Jude T, Sommerfeld (Goorsia Tech). in addition, David V. Chase (The University of Dayton), Raymundo Condero and Korim Muci (ITESM, Mexico), James W. Hiestond (University of Tennessee at Chotanoozi, Steve Klegka (U.S. Military Academy), James L, Kuester and Robert L, Rankin (Arizona State University), lis D, Sotclino (Purdue University), and Hewlon ‘Zimmer (U.S. Merchant Marine Academy) provided useful feedback for the prior edition. Finally, i should be stressed that although we received useful adviee from the aforementioned individuals, we are responsible for any inaccuracies or mistakes you may detect in this edition Steven C. Chapra Meaford, Massachuserts Raymond P. Canale Lake Leclanau, Michigan Cengver20 PART ONE Chara-Grate-marenea Mathes or Eines, Fear tee PTL Lectin. compotrs | aaron oe) 9 Eo hes engonas 20 MODELING, COMPUTERS, AND ERROR ANALYSIS MOTIVATION ‘Numerical methods are techniques by which mathernatieal problems are formulated so that they can be solved with arithmetic operations. Akhough there are many kinds of numerical ‘methods, they have one common characteristic: they iavariably involve large numbers of tedious arithmetic calculations, I is litle wonder that with the development of fast, effi- cient digital computers, the role of numerieal methods in enginvering problem solving has ‘nereased dramatically in recent yeas. PT1.1.1 Noncomputer Methods Beyond providing increased computational firepower, the widespread availability of come puters (especially personal computers) and their partnership with numerical methods has hhad a significant influence on the aciual engineering problem-solving process. In the pre= computer era there were generally three different ways in which engineers approached problem solving: 1. Solutions were derived for some problems using analytical, or exact, methods, These solutions were often useful and provided excellent insight into the behavior of some systems. However, analytical solutions can be derived for only a limited class of problems. These include those that can be approximated with linear models and those ‘that have simple geometry and low dimensionality. Consequently, analytical solutions ‘re of limited practical Value because most real problems are nonlinear and involve ‘complex shapes and processes. 2. Graphical solutions were used to characterize the behavior of systems, These graphical solutions usually took the form of plots or nomographs. Although graphical techniques ‘can often be used to solve complex problems, the results are not very precise. Fushhermore, graphical solutions (without the aid of computers) are extremely tedious. nd awkward to implement. Finally, graphical techniques are often limited to problems, ‘that can be described using three or fewer dimensions. 3. Calculacors and slide rules were used co implement numerical methods manually. Although in theory such approaches should he perfectly adequate for solving complex. problems, in actuality several difficulties are encountered. Manual calculations are slow and tedious. Furthermore, consistent results are elusive because of simple ‘blunders that arise when numerous manual tasks are performed, During the precomputer era, significant amounts of energy were expended on the so Jution technique itself, rather than on problem definition and interpretation (Fig. PTI.La), ‘This unfortunate situation existed because so much time and drudgery were required 10 0b ‘ain numerical answers using precompater techniques. Chara-Grate-marenea Lectin. compotrs | aaron oe) Matesatr teers, oh fangs, factor 4 MODELING, COMPUTERS, AND ERROR ANALYSIS FORMULATION FORMULATION Indeoth exposition eee of lationship of sar dream nde ¥ souurion ZoLuTON Elaborate and often Enyto-use FIGURE PT1.1 complicate method 10 serious The threo phases of angi make problom tractable ‘method probe sohing he precompotr and (bf | Eomputr er. Te sizes o he ‘boxes indicate the level of emphasis ciracted toward each INTERPRETATION Te Metall jase, Compre ket te Inscepth analysis Ease of calculation implemeriation of solion limited by ime allows holistic thoughts techies ond hs allow censuming sation and india to doveton: temphonis io be placed systam sensitivity and bonevior groohe aspects of problem ‘can be studied ests . @ o Today. computers and numerical methods provide an alternative for such compli cated calculations. Using computer power to obtain solutions directly, you can approach these calculations without recourse to simplifying assumptions or time-intensive tech: niques. Although analytical solutions are still extremely valuable both for problem solv- ing and for providing insight, oumerical methods represent alternatives that greatly en large your capabilities to confront and solve problems. As a result, more time is available for the use of your creative skills, Thus, more emphasis can be placed on problem for- ulation and solution interpretation and the incorporation of rotal system, or “holistic,” ‘awareness (Eig. PTILIb.) PT1.1.2 Numerical Methods and Engineering Practice Since the late 1940s the widespread availability of digital computers has led to a veritable explosion in the use and development of numerical methods. At first, this growth was somewhat limited by the cost of access to large mainframe computers, and, consequently, ‘many engineers continued f0 use simple analytical approaches in a significant portion of their work. Needless to say, the recent evolution ef inexpensive personal computers has Chara-Grate-marenea Mathes or Eines, Fear tee Lectin. compotrs | aaron oe) rea cengrer, PT1.2_ MATHEMATICAL BACKGROUND 5 PTL2 aiven us ready access fo powerful computational capabilities. There are several additional feasons why You should study numerical methods: 1. Numerical methods are extremely powerful problem-solving tools. They arc capable of handling large systems of equations, nonlinearities. and complicated geometries that fare not uncommon in engineering practice and that are often impossible to solve analytically. As such, they grcatly enhance your problem-solving skills. 2 During your carcets. you may offen have occasion to usc commercially available prepackaged. or “canned.” computer programs that involve numerical methods. The intelligent use of these programs is often predicated on knowledge of the basic theory underlying the methods, 3. Many problems cannot be approached using canned programs, If you are conversant with numerical methods and are adept at computer programming, you can design your ‘own prograns to solve problems without having to buy or commission expensive software. 4. Numerical methods are an efficient vehicle for learning to use computers. It is well known that an effective way to leam programming is to actually write computer programs. Because numetical methods ate forthe most part designed for implementation ‘on computers, they are ideal for this purpose. Further, they are especially well-suited to illustrate the power and the limitations of computers. When you suecesstully implement numerical methods on a computer and then apply them to solve otherwise intractable problems, you will be provided with a dramatic demonstration of how computers can serve your professional development. Atthe same time, you will also leant ‘acknowledge and control the errors cf approximation that are part and parcel of Inge- scale numerical calculations, Numerical methods provide a vehicle for you to reinforce your understanding of mathematics. Because one function of numerical methods is to reduce higher mathematics to basic arithmetic operations, they get at the “nuts and bolts” of some otherwise obscure topics. Enhanced understanding and insight can result trom this ‘alternative perspective, MATHEMATICAL BACKGROUND. Every past im this book requires some mathematical background. Consequently, the intro dductory material for each part includes a section, such 2s the one you are reading, on math ematical background. Because Part One itself is devoted to background material on math- ematics and computers, this section does not involve a review of « specific mathematical topic. Rather, we take this opportunity to introduce you to the types of mathematical sub ject areas covered in this book. As summarized in Fig, PTI.2, these are 1. Rooty of Equations (Fig. PT1.2a). These problems are concerned with the value of a variable or a parameter that satistics a single nonlinear equation. These problems are especially valuable in engineering design contexts where it is often impossible to explicitly solve design equations for parameters. 2 Systems of Linear Algebraic Equations (Fig. PT1.2b). These problems are similar in spirit to roots of equations in the sense that they are concerned with values that satisty Chapra-Geate:rareneat | Ladeeting. comptes | retin oe) Matar agen, hel Conn reetton 6 MODELING, COMPUTERS, AND ERROR ANALYSIS } (a) pan 2 Roots of equations nw Sommory of ke nmerca Solves) = 0 for x medlogs coved this book Root (6) Pua 3 near alabrei equations ‘Suan thaws and thee soe : forthe Golution (©) Part4: Opumizavion Determine «that gives optimum fi. a Minimunt (@) Par 5 Curve fiting fe) fe Interpolation Regression (6) Par integration Tn feflslac Find the aroa uncer the curve, nw Chapra-Geate:rareneat | Ladeeting. comptes | retin oe) Matted Eines, area cengrer, Fear tee PT1.2_ MATHEMATICAL BACKGROUND z FIGURE PT1.2 (concluded) (Pye 7 Orceary eran equetons a a a) solve for yas function of here ut fli yar (4) Farr8: Porto cifferentiol equetions Given solve for was function of andy equations, However, in contras! to satisfying a single equation, a set of values is sought that simultaneously satisfies a set of linear algebraic equations, Such equations arise in ety of problem contexts and in all disciplines of engineering, In particular, hey nate in the mathematical modeling of Tange systems of interconnected elements such as structures, electric circuits, and fluid networks, However, they are also encountered in other areas of numerical methoxls such as curve fitting and differential equations, 3. Optimization (Fig, PTI.20. These problems involve determining a value or values ‘ofan independent variable that conrespond to a “best” or optimal value ofa fimnetion, ‘Thos, as in Fig, PTI.2c. optimiza tank! aniaima. Sueh problems occur routinely in engineering design contexts, They also arise in a nurnber ‘of other numerical methods. We address both single- and multi-variable unconstrained ‘optimization. We also deseribe constrained optimization with particular emphasis on linear programming. 4. Cune Fitting (Fig. PTL2d), You will often have occasion to fit curves to data points. ‘The techniques developed for this purpose can be divided into two general categories: regression and interpolation. Regression is employed where there is « significant degree of error associated with the data. Experimental results are often of this kind. For these situations, the strategy is to derive a single curve that represents the general trend fof the data without necessarily matching any individual points. In contrast. interpolation is used where the objective is to determine intermediate values betwet relatively error-free data points. Such is usually the case for tabulated information. For these situations, the strategy isto fit a curve directly through the data points and use the ‘curve to predict the intermediate values. 8. Integration (Fig. PTI.2e), As depicted, a physical interpretation of numerical integration is the determination of the area under a curve. Integration has many Chara-Grate-marenea Mathes or Eines, Fear tee Lectin. compotrs | aaron oe) rea cengrer, MODELING, COMPUTERS, AND ERROR ANALYSIS PTLS applications in engineering practice, ranging from the determination of the centroids of ‘oddly shaped objects to the calculation of total quantities based on sets of discrete measurements. In addition, numerical integration formulas play an important role in the solution of ditferential equations, 6. Ordinary Differential Equations (Fig. PT129). Ondinary differential equations are of _afeat significance in engineering practice. This is because many physical laws are couched in termns of the rate of ehange of a quantity rather than the magnitude of the ‘quantity itself. Examples range from population forecasting models (rate of change of Population) to the acceleration of falling body (rate Of change of velocity). Two types Of problems ate addressed: initial-value and boundary-value problems. In addition, the utation of eigenvaltes is covered. 7. Partial Digjerential Equations (Fig. PTI.2g). Pastil differential equations are used 10 characterize engineering systems whete the behavior of a physieal quantity is couched in terms of is rave of change with respect @ Wo oF more independent variables Fxamples include the steady-state distibution of temperature on a heated plate (Iwo spatial dimensions) o the time-variable rempesauire of 2 heated rod (Gin approaches are employed (© solve parcial differential equations numerically. Inthe present text, we will emphasize finite- difference methods that approxinnate the solution in a pointwise fashion (Fig. PT1.2g) However, we wil also present an introduction 1 finive-element metheds, which use a piecewise approach ORIENTATION Some orientation might he helpfal hefore proceeding with our introduction to numerical imethels. The following is intended as an overview of the material in Part One, Tn addition, some objectives have heen included to focus your efforts when studying the material. PT1.3.1 Scope and Preview Figure PTI.3 isa schematic representation of the material in Part One. We have designed this diagram to provide you with a global overview of this past of the book. We believe that fa sense of the “big picture” is critical to developing insight into numerical methods. When reading a text it is offen possible to become lost in technical details. Whenever you fee! that you are losing the big picture. refer back to Fig. PTIL3 to reorient yourself. Every part of this book includes a similar figure. Figure PT1.3 also serves as a brief preview of the material covered in Part One. Chap- ter 1 is designed to orient you to numerical methods and to provide motivation by demon- strating how these techniques can be used in the engineering modeling process. Chapter 2 is an introduction and review of computer-related aspects of numerical methods and sug~ gests the level of computer skills you should acquire to efficiently apply succeeding infor- imation. Chapters 3 and 4deal with the important topic of error analysis. which must be un- derstood for the effective use of numerical methods. In addition, an epilogue is included that introduces the trade-offs that have such great significance for the effective umplemen- tation of numerical methods. Chapra-Geate:rareneat | Ladeeting. comptes | retin oe) Matted Eines, area cengrer, Fear tee PT1.3_ ORIENTANON Modaiing, Computers, ‘and Error Anclysis CHAPTER 1 Mathematical Modeling ond Engineering Problem cHaPTER2 Computors and Solware Truncation Enis and the Taylor Series CHAPTER 3 Approximations ‘end Rounc-off Errors FIGURE PT1.3 schematic of the or 10 Chara-Grate-marenea Mathes or Eines, Fear tee Lectin. compotrs | aaron oe) 9 Eo hes engonas 20 MODELING, COMPUTERS, AND ERROR ANALYSIS col nadek of physical stem, Be copa cf compos sticec and mecidarcrogions no highievel compar language Koraw haw to handls sustvsd fonchars and peeudacods ins code ma bighlave onguoge. Sz 0 ‘anilanze yursell wih any schwave packages het you wil be vsng i comrcton 1 2 4, Dainaaie the wl tha unde shuetned program 5 6 suit eto. 8. Recognize he distinction bowers tuncotin and kurcoff eos ©, Undariand the concape of rignfeont Fgura, cearacy, and 10, Recagnize he dhorance between Ic eloive vor ey coprotinat reli ercr «and accepcble fare, and undetand hous, and, ote used to frm nate an ieraive comput, 1) Understand how nu gta coma. ord Fw this rocrevenion induces tourdof anor. Ih pasar nom the dFerance ba\waan angle and etanded pecan, 12. Recagnize have compute rtmetic con inkoduce ond cmpily eurcall ero h ealaiatons, hy partelos apprecais he erosion ef sucnoaive concalaten, tad hom the Tyr sorts ond its engi Va. row th ab 15. Beatle Vo. Bs faviiar vith onirize youl wil PT1,3.2 Goals and Objectives Stucly Objectives. Upon completing Part One. you should be adequately prepared 10 embark on your studies of numerical methods. In general. you should have gained a fun- damental understanding ofthe importance of computers and the role of approximations and terrors in the implementation and development of numerical methods. In addition to these general goals. you should have mastered cach of the specific study objectives listed in Table PTLLL Computer Objectives. Upon completing Part One, you should have mastered sofficient computer skills to develop your own software for the numerical methods in this text, You should be able to develop well-sirvetured snd reliable computer programs on the basis oF pseudocode, fovichas, or other forms of algorithms, You should have developed the ex- pabiliy 10 document your programs so that they may be effectively employed by users Finally, in addicion o your owa programs, you may be using software packages along, with this book. Packages like MATLAB or Excel are examples of such software. You shoul become familiar with these packages, so that you will he comfortable using them to solve ‘numerical problems later in the tex Chara-Gnae Mathes or Eines, Fear tee eat Lectin. compute | 1 Manet tebe ‘omnes sed Co hea sed Eginedeg Pablo engonas 20 Soking CHAPTER 1 Mathematical Modeling and Engineering Problem Solving Knowledge and understanding are prerequisites for the effective implementation of any too}. No matter how impressive your tool chest, you wil be hard-pressed t0 repair a car it yyou do not understand how it works. This is particularly true when using computers to solve engineering problems. AF ‘though they have great potential utility, computers are practically useless without a funda ‘menial understanding of how engineering systems work. ‘This understanding is initially gained by empirical means—that is, by observation and experiment, However, while such empirically derived information is essential, itis only nat the story, Over years and years of observation and experiment, engineers and scientists hnave notived that certain aspects of their empirical studies oceur repewedly, Such general behavior can then be expressed as fundamental laws that essentially embody the cumukte tive wisdom of past experience. Thus, most engineering probiem solving employs the two- pronged approach of empiricisin and theoretical analysis Fig. 1-1), Teanust be stressed that the 1we prongs are closely coupled. AS new mexsurerents are taken, the generalizations may be modified or new ones developed. Similarly, the general- dations cum have a strong influence on the experiments and observations. In particular, ‘generalizations can serve as organizing principles that can be employed to synthesize ob servations and experimental results into a coherent and comprehensive framework from which conclusions can he drawn, From an engineering problem-solving perspective, such a framework is most usefial when itis expressed in the form of a mathematical model ‘The primary abjective of this chapter is to introduce yon to mathematical modeling and its role in engineering problem solving, We will also illustrate how numerical methods figure in the process. A SIMPLE MATHEMATICAL MODEL A mathematical model can be browelly defined as a Formulation or equation that expresses the essential featutes of a phy ical system or process in mathematical terms. In a very gene eral sense, ican be represented as a Functional relationship of the Foran Dependent _-/ independent eye, f0RCing vanable (variables ° P* functions, op Caprcorterareca | Leet. conputars | 1 Manat tenn one nkcat Matted Eines, area sed Eginedeg Pablo cengrer, Fear tee Soking 12 MATHEMATICAL MODELING AND ENGINEERING PROBLEM SOLVING Problem Sefinivon THEORY DATA Proplem-solving too's: computers, sttistios, ‘numerical methods, ‘araohics, ete Numeric or graphic routs Societal intoriacoe: ‘scheduling, optimization, communication, publi interaction, ate. FIGURE 1.1 The engineering protlem solving proces where the dependent variable is a characteristic hat usually ref 1s the behavior or state of the system: the independent variables are usually dimensions, such as time and space, along witich the sy ems behavior is being determined: the parameters are reflective ofthe system's properties or composition: and the forcing furctions are external influences acting upon the system. ‘The actual mathematical expression of Eq, (1.1) ean range from a simple algebraic re lationship to larg complicated sets of differential equations. For example, on the basis of his observations, Newton formulated his second law of motion, which states that the time rate of change of momentum of « body is equal t the resultant for ‘ematical expression, er model, ofthe second law is the well-known equation Fama where F = net force acting on the body (N, or kg 1n/s?),m 4 = its acceleration (nvs" acting on it. The math= ‘mass of the object (ke). and Chara-Grate-marenea Mathes or Eines, Fear tee Lectin. compute | 1 Manet tebe one nkcat rea sed Eginedeg Pablo cengrer, Soking L.1_ASIMPLE MATHEMATICAL MODEL 13 FIGURE 1.2 Schematic diagram of he forces acting on o filing porochutst. Fa is ho downward ‘ice dus to gavty. Fy ste ‘The second law can be recast in the format of Eq. (1.1) by merely dividing both sides by mto give P =f a3 where « = che dependent variable reflecting the system's behavior, F = the Forcing func- tion, and m = a parameter representing 2 property of the systems, Note that for this simple cease there is no independent variable because we are not yet predicting how acceleration varie in time or space. Equation (1.3) has several characteristics that are typical of mathematical models of the physical world 1. Itdeseribes a natural process or system in mathematical terms, 2 It epresents an idealization and simplification of reality. That is, the model ignores negligible details of the natural process and focuses on its essential manifestations. ‘Thus, the second law does not include the effects of relativity that are of minimal im portance when applied to objects and forces that interact on or about the earth’s surface ‘at velocities and on seales visible to humans. 3. Finally, it yields oproducible results and, consequently, can be used for predictive purposes. For example, if the force on an object and the mass of an object are known, Eq, (1.3) can be used to compute acceleration Because of its simple algchraic form. the solution of Bq. (I.2) ean be obtained easily However, other mathematical models of physical phenomena may be rnuch more complex. and either cannot be solved exactly oF require more sophisticated mathematical techniques than simple algebra for their soluion, To iMlustrate a more complex model of this kind, Newton's second! Iaw can be used to determine the terminal velocity ofa free-falling body sar the earth's surface, Our falling boely will ea parachutist (Fig. 1.2). A model for this cease can be derived hy expressing the acceleration as the time rate of change of the veloc ity (du/de) and substituting it ino Bg, (1.3) (0 yield dv_F aay where v is velocity (ms) and is time (5). Thus. the mass multiplied by the rate of change of the velocity is equal to the net force acting on the body. Ifthe net force is positive. the object will accelerate. [fit is negative. the object will decelerate. If the net forec is zero, the object's velocity will remain at a constant level Next, we will express the net force in terms of measurable variables and parameters. Fora body falling within the vicinity of the earth (Fig. 1 2). the net force is composed of two ‘opposing forces: the downward pull of gravity Fy and the upward force of air resistance F P=fotho os If the downward force is assigned a positive sign, the second law can be used to for- smulate the foree due to gravity, as Fy =mg ae where ¢ = the gravitational constant, or the acceleration due to gravity, which is approxi- mately equal to 9.8 mvs Lectin. compute | 1 Manet tebe one nkcat Coane tee Matar agen, hel ‘ed Saginting Pain Conn reetton Sena 14 MATHEMATICAL MODELING AND ENGINEERING PROBLEM SOLVING [Air esistance can be formulated i a variety of ways. A simple approach is 0 assume that itis linearly proportional 10 velocity’ and acts in an upward direction, as in fo=-cw ar where © =2 proportionality constant called the drag coeficien: (kgs). Thus, the greater the fall velocity the greater the upwart force due to air resistance. The parameter © ac- counts for properies of the falling object, sueh as shope cr surface roughness that affect air resistance. For the present case, ¢ might be a fareton ofthe type of jampstit or the oren- tation used by the parachutist during freefall. “The net fore is the difference between the downward and upward force, Thecefore gs. (1 through (1.7) cam be combined to yiekd dy _mg—eo ams asy or simplifying the righ side, ae ag dr uation (19) is a maadel thet relates the aeceleraion ofa falling objet to the forces act ing oni. isa diferent equation because itis writen in terms ofthe differential rate oF change td) of the variable that we are interested in predicting. However, in contrast to the solution of Newton's second law in a. (1.3. the exict solution of Eq (19) forthe ve= locity of the falling parachutist cannot he obtained using simple algebraic manipation Rather, more advanced techniques sich as those of calculus, must be applied 0 obiin an exact oF analytical solution. For example, if the parachutist is initally a rest (1 =0 a £=0),caleuins can be wsed solve Faq. (1.9 for wi = benny (10) [Note that Fg. (1.10) is east in the gencral form of Fg, (1.1), where 0() dent variable, ¢= the independent variable, cand m = parameters, and ¢ = the forcing function. EXAMPLE 1.1 Analytical Solution to the Falling Parachutist Problem Problem Statement. A parachutist of mass 68.1 kg jumps out of a stationary hot air bal- loon. Use Eq. (1.10) to compute velocity prior to opening the chute. The drag coefficient is equal to 12.5 kas Solution. Inserting the parameters into Eq. (1.10) yields 8.8(68.1) w= (1 e289) 53,301 — 8) ‘which can be used to compute "infact, the rtlonsip Is stall seeincar nd might heuer be represen by 2 power Helaonship suc as Fi = 2 We wil expire ow sich nonlincaresllet te model na problem a the ro hi chagect, Caprcorterareca | Leet. conputars | 1 Manat tenn one nkcat ethde trainers or hash so faienig Pblen can, Fear teen Soa L.1_ASIMPLE MATHEMATICAL MODEL 15 ts vimis According (0 the model, the parachuiit accelerates rapidly (Fig. 1.3). A velocity of 44.87 is (100.4 min) is stained after 10 s, Note also that after a sufficiently Tong time, a constant velocity, called the rerminal velocity, of 53.39 ms (L194 raifh) is teached. This velocity is constant because, eventually, the force of gravity will be in balance with the air resistance. Thus, the net force is 2er0 and neceleration has ceased Equation (1.10) is called an anaiytical, or exact, solution because it exactly satisfies inal differential equation. Unfortunately. there are many mathematical models that cannot be solved exactly, In many of these cases, the only alfemative is to develop a nu- ‘merical solution that approximates the exact solution, ‘As mentioned previously, numerical methods are those in which the mathematical problem is reformulated so it ean be solved by arithmetic operations. his can be illustrated FIGURE 1.3 The aralyical solution wo ‘ali problem os cle Volocyineronses wih time cn asynpiotcaly aperoaches © ‘ominal vole Caprcorterareca | Leet. conputars | 1 Manat tenn one nkcat Matesatr teers, oh ad agen Pablen fangs, fea teon soa 16 MATHEMATICAL MODELING AND ENGINEERING PROBLEM SOLVING. . 1 ‘Approximate stope | ae Meslay | ae | FIGURE 1. ; The use ofa finne difference to approsmate be fist derivative for Newton's second law by realizing that the time rate of change of velocity c proximated by (Fig. 14): a wis) a” uth Ww 2 ab where Av and Ar = differences tervals, o(f) = velocity at an initial time f, 2 velocity and time, respectively, computed over finite ins (Gry) = velocity at some later time 1 Note thatdv/di © Av/ Ar is approximate because Ars finite, Remember fromealcuhis that do aw aa a Equation (1.11) represents the reverse process Equation (1.11) is called a finite divided difference approximation of the derivative at time f. It can be substituted into Eq. (1.9) to give wisn) = eG) font This equation can then be rearranged to yield ro) rise [r= Zotar 19 aun ‘Notice thatthe term in brackets isthe right-hand side of the differential equation itself Iq. (1.9)]. That i, it provides a means to compute the rate of change or slope of e, Thus, {he differential equation has been transformed into an equation that ean be used to deter: imine the velocity algebraically at. using the slope and previous values of v and i If you ‘are given an initial value for velocity at some tink: #,, You ean easily compute velocity at & Caprcorterareca | Leet. conputars | 1 Manat tenn one nkcat Mathes or Eines, Fear tee EXAMPLE 1.2 rea sed Eginedeg Pablo cengrer, Soking L.1_ASIMPLE MATHEMATICAL MODEL 7 later time f,. This new value of velocity at 4 ean in turn be employed to extend the computation to velocity at f--2 and so on, Thus, at any time along the way, New value Id value + slope x step size Note that this approach is formally called Buler’s method. Numerical Solution to the Falling Parochutist Problem in Example 1.1 but use Eq. (1.12) che caleulation, Problem Siotement. Perform the same computatio ‘o compute the velocity, Employ a step sizeof 2 Solution, At the start of the computation (i; = 0), the velocity ofthe parachutist is zero. Using this information and the parameter values from Example I, Eq, (1.12) can be used. to compute velocity at 41 = 2s: 12. 5. 2=19 FFor the next interval (from 1 = 2. v= 19.604 [98-5 19.60] 2 = 32.00 mis + 1 >| 049) the computation is repeated, with the result ‘The calculation is continued in a similar fashion to obtain additional valves: ts vmis 0 0.00 2 19.20 The results are plotted in Fig. 1.5 along with the exact solution. It ean he seen that the numerical method captures the essential features of the exact solution. However, because ve have employed straight-line segments to approximate a continuously curving function. there is some discrepancy between the two results, One way to minimize such diserepan- cies is to use a smaller step size. For example. applying Eq. (1.12) at ls intervals results in a sinaller ertor. asthe straight-line segments track closer to the true solution. Using hand calculations. the effort associated with using smaller and smaller step sizes would make such numerical solutions impractical. However. with the aid of the computer. large num- bers of calculations can be performed casily. Thus. you can accurately modct the velocity of the falling parachutist without having to solve the differential equation exactly. As in the previous example, a computational price must be paid for a more accurate umencal result, Each halving of the step size to aitain more accuracy leads to a doubling Lectin. compute | 1 Manet tebe one nkcat Chara-Grate-marenea Matted Eines, area sed Eginedeg Pablo cengrer, Fear tee Soking 18 MATHEMATICAL MODELING AND ENGINEERING PROBLEM SOLVING Torminal valocty Approximete, numerical solution, 40 & 5 Exact, analytical solution 0 FIGURE 1.5 © Comgison of he numer a ‘ e 4 ‘and analyicalscuton 1s of the number of computations, Thus, we see that there isa tracle-off between accuracy and ‘computational effort, Such trade-offs figure prominently in numerical methods and consti= {ute an important theme of this book, Consequently, we have devoted the Epilogue of Part One 10 an intraxinction to more of these trade-offs CONSERVATION LAWS AND ENGINEERING. Aside from Newion’s second law, there are other major onganizing principles in engineer ing, Among the most important of these are the conservation laws, Although they Form the: basis for a variety of complicated and powerful mathematical models, the great conserva- tion laws of science and engineering are conceptually easy to understand, They all boil down to Change increases — decreases or ‘This is precisely the format shat we employed when using Newton's law to develop a force bolance for the falling parachutist (Eq. (L.8)) ‘Although simple, Eq, (1.13) embodies one of the most fundamental ways in which conservation laws are used in engineering—that is, o predict changes with respeet co time, We give Eq, (1.13) the special name srme-variable (or ransient) computation, "Aside from predicting changes, another way in which conservation laws are applied is for cases where change is nonexistent. If change is zero, Bq, (1.13) becomes Change = 0 = increases ~ decreases or Increases = decreases torn Lectin. compute | 1 Manet tebe one nkcat Capac treet Mates Canes, a hel sd Engng Problem tenga, fea teon Sate 1.2. CONSERVATION LAWS AND ENGINEERING 1 Pipe frown = 80 Fioe 1 Pine 4 Flow in = 100 = TP Flow out=? FIGURE 1.6 A How bolance for stoady composes tid flow af he joncion of pipo: + Pipe 3 Flo aut = 120 Thus. if no change occurs. the increas is also given a special name—the stecad=state computation —has many applications in en- gincering. For example, for steady-state incompressible fluid flow in pipes, the flow into a junction must be balanced by flow going out, as in Flow in = flow cut For the junction in Fig. 1.6, the balance can be used to compute that the flow cut of the fourth pipe rnust he 60. For the falling parachutist, steady-state conditions would correspond to the case where the net force was zer0, oF [Eq. (1.8) with dude = 0) mg = ev aa) Thus, at steacly state, the downward and upward forces are in balance, and Bq, (1.15) ean be solved forthe terminal velocity mg Although Eqs. (1.13) and (1.14) might appear trivially simple, they embody the two fundamental ways that conservation laws are employed in engineering. As such. they will ‘orm an important part of our efforts in subsequent chapters to illustrate the connection be ‘ween numerical methods and engineering. Our primary vehicles for making this connec tion are the engineering applications that appear at the end of cach part of this book. ‘Table 1.1 summarizes some of the simple engineering models and associated conserva tion laws that will form the basis for many of these engineering applications. Most of the chemical engineering applications will focus on mass balances for reactors. The mass bal ance is derived from the conservation of mass It specifies tha the change of mass of a chem- ‘eal in the reactor depends on the amount of mass flowing in minus the mass flowing out. Both the civil and mechanical engineering applications will focus on models dev ‘oped from the conservation of momentum, For civil engineering, force bakanees are utilized to analyze structures such as the simple truss in Table I.1.’The same prineiples are employed for the mechanical engineering applications to analyze the transient up-and- down motion or vibrations of an automobile. [ scmamorwecat terre Chapra-Geate-rarencat | eet. Computers, oe) Matted Eines, area sed Eginedeg Pablo cengrer, Fear tee Soking 20 MATHEMATICAL MODELING AND ENGINEERING PROBLEM SOLVING ‘TABLE 1.1. Devices and types of balances that are commonly uted in the four major areat of engineering. For each case, the conservation law upon which the bolance is based is specified Organizing Principle Mathomatieal Expression Fold Device Chemical engineering gop LL Pesos Civil engineering Smucture, Mechanical enginaering —— Elecrical engineering Circuit Conservation of mass Maes balance: eput > Outout vero unit oFtime period ‘mess = inputs ~ouipuis Conservation of Foree balan: momentum ‘At each node > horizanial forces (Fy =O 2 vortical foro99 (F,)=0 soma oe m 2%... downward force - upward force a Conservation of charge Cusrent balance: For each node 'Spurrant i) = 0 Conservation of energy Voltage balance: ey ‘he 6 aR Around each loop omi’s-S voltage drops for resistors = 0 Sei Caprcorterareca | Leet. conputars | 1 Manat tenn ‘omnes MetbedoorEeiaer, sud Ear as Fear tee Soking PROBLEMS sed Eginedeg Pablo engonas 20 21 TABLE 1.2 Some proctcal issues that will bo explored in the engineering applications crtthe end of each part ofthis book 1. Nerlinaar seus tinea: Stor, Athough he & fer 9 exrined 2, foxge vaso evo nt ‘con be avomined cro sys, Uh! co ing comecrerts, With compu izaon to per oralcel nis reghtcon chin 2 gard noalnen” plans ta 11 ofan et Foie to exoming ests uth ene roe J numetcal meleds, moe eaishe mulleonecrart stern 3. Nevio! vows idol, ealized lows abourd in ergineeing, Oten thew oe roridedized atemotives ‘rors conpsotoraly denondes, Apgcosivola mmsion aepronzhas car foaitoe te coplcction of he soniceo eatorships 4. Sensiiy avails, Bacoie fey os racked, ney fd ce for coos irplomawaton. This semoimas disco Inatidlecompreters fr ere rec Such ompeviona!buoen is potoraers In isusialy moe df be inglonerd nc rare & spectied, Nywetcel nehode ol eoeuksioes wie a ica deal of te roges he onal! fom inplonenteg te cary losin how a sysen reserve urder dara condos ivy crolies 0 faciicled wren murevical mehock alow he comets © oxo fe goon 0 saichtonsord propaion to dowrrine the petrmonce cto stom coo hncton ot Ho sole the inves pcan i eineing ha potemekes 1 comaters ofan port Fis ok > Finally, the electrical engineering applications employ bord current nnd energy bale ances 10 model electri circuits. The current balance, which results from the conservation ‘of chagge, is similar in spitt to the flow halenee depicted in Fig. 1.6, Jus as flow must bal= ance al the junction of pipes, electri current must balance a1 the junction of electric wires, The energy balance specifies that the changes of voltage around any loop of the circuit ‘must add up to zero, The engineering applications ate designed to ilstrace how numerical methods are aetwally employed in the engineering problem-solving process. As such, they \will permit us to explore practical issues (Table 1.2) that sis in real-world applications Making these connections between mathematical techniques such as ruinerical methods and engineering practice is critical step in tapping their true potential, Careful exaunia tion of the engineering applications will help you to take this step PROBLEMS 1.1 Read the following problem descriptions and ideatfy which avoa of rumerieal methods (as oained in Fig, PTI 2) oles 10 their solton (a) You ae responsive for deteoiing the Hows in a large ite. comected network of pipes to dsrbate natural 235 © a series fof communities spread out over 20s area. () You ate performing experiments to determine the vlage op acioss @ osstor as a function of eutten. You take mca surements of voltoge drop fora amber of diferenyaluos oF © current, Although thers is some crx associated with yourdata Points, when you plot ther they suggest» smooth curvilinesr Felatioaship. You ae to derive an equation to characterize this relationship You must develop a shock-absorber system for a racing cas, Newton's second law can be used to drive an equation to pre ‘dist the rate o change in postion of fone wheal in espns to external forces. You mus! compute the motion of the wheel fas function of time afer it hits a in burp a 150 mi, Chara-Grate-marenea Mathes or Eines, Fear tee eating. compet rea Soking 22 [ scmamorwecat terre so faienig Pblen cengrer, MATHEMATICAL MODELING AND ENGINEERING PROBLEM SOLVING (2) You have to determine the annual revenaes required over a 20-year period for an emertainment center to be built fer a client, Morey ean presently be borrowed at an interest rate of| 10.37 percent, Although the information to perform ths est rate is contained in economies tables, values are listed oly for interest rates of 10 and 1 percent (©) You must determine the temperature distribution on the two- imensiova surface ofa flat gasket a a funetion ofthe fem peratures of fis edges. For the falling parachutist prablem. you must determine the valusof the drag coefficient in order thet a 20-th parachutist not exceed 100 mia within 10 s of jumping. You mest make this evasion on the basis oF the analytical solution (EC. 1D) The infomation will be vsed t design a jumps (e) Yousreona survey erew and mut detormine the area of Feld that is bounded by to roads snd & meandering seam 1.2 The following information i available fora bank account « Date Deposits Withdrawals ——Belanco v 512 38 27 26 2ie.ee 78.01 450.25 W080 3/ Dan 45061 Use the conservation of cash to compat the balance on 6/1. 7H, ‘8/1, and 9/1. Show each step in the computation. Is this a stay state or a transient compeaton’ 1.3 Verify that Eq (1,10) isa solution of Ea, (1.9. 14 Repeat Example 1.2. Compute the velocity t0 ¢ = 12 », wilh a siep siz of (@) 1 and (b) 03 s, Can you make any statement ‘reganing the estors of the eaeaation based on the esas? 1.5 Rather than the linear relationship of Eg, (1.7). the vpward force onthe falling parachutist is actully nonlinear and might bet rece represented by 4 power relationship such as Fi 6 =a second-order drag coetticient (kg/m). Using this ‘elationship,repea the caleuation i Exaanple 1-2 with the same inal condition and parameter values, Use a value of 0.25 kg/m tere’ 116 The sino ofa uniformly distributed radioactive contaminant contained in @ closed reacior is measured by its concentration © (becquereiite, or Bo/l.). The contaminant decseeses at a decay rate proportional to its concentration, that is Decay tate = ke where & = a constant with units of day”, Therefore, aeconding to Fg, (1.13), a mas balance for Ue vector Gan I> writen 38 (om)-(S) Use numerical methods f0 solve this equation fom f= 0 to 1d. vith £ = 0.1d-! Employ a step size of Ar = O.1. The concentra on ate = Dis 10 Bell LT A storage ank contains o liquid at depth y. where y = O when the tank is half full. Liquid is withdrasen at constant flow rate Q ro misct demands, The coments are resapplied at a sinusoidal ate 30 sin! (see Fig. PIL?) Equation (1.13) cam be waitin fr this system as diayy de (etopete volume =39sin' 0 ) = éatow)~cutow) oc sce the surface area, 4, is constant dy, @ ay A Use a numerical method to solve forthe height» from r = 010 5 sith a stop size of 05d. The parameter values are = 1208 a? and @ = 400 md Figure P17 1.8 For the falling parachutist problem, assume that the frst Jumper has 2 mass oF 68.1 kg and a drag everfickent of 12.5 kgs I ‘second juniper has a mass of 75 ke aud a diag cvetfcient of Chara-Grate-marenea Mathes or Eines, Fear tee eating. compet fae eas Soking PROBLEMS [ scmamorwecat terre so faienig Pblen engona,20 23 1H kgs, how long wil ica the second jumper wo atin tbe same velocity reached by the fst juraper in 10? 1.9 The volume flow rate through a pipe is given by Q = vA, Uwhere 1 isthe average velocity and A is the cross-sectional area Use volume-continuty to solve fo the required area in pipe 3 (see Fig. PL. Oy). = 30 me Caw = 20.0 Yona Sms ie? Figure P1.9 10 Amaverage man in one day ingests 1,000 g of Fone and 1.200 12 of liquid the body will prxkice mesabolically some additional ‘water. When breathing ait, 50 g and 400 g of HO are exchanged while inhaling and exhaling, respectively, aver & one dy period. The body will so kse 204 g, 1.400 g, 200 g, and 350 8 oF HO though the sweat, urine, feees, and skin, respectively In order 10 ‘maintain a steady-state condition where 3X mass in— JE mass ow = 0 what amount of HO must he produced metabolically (see Fig. PI.10), Skin Food Ai Drink sweet Metabolism Figure P1.10 1.11 Water accounts for roughly 60% of oil ody weight. Assan ingit can be categorized into si regions. the percentages of HO are q body HhO. Dense connective tissue and cartilage is 45% ofthe total body weizht and 7.9% of the total body HO, Interstitial lymph is, 12% ofthe body weight and 20% of the tol body HO. Inaccessble ‘bone water fstcughly 4.3% af theta body weight and 7.596 oF the ‘otal body 1.0. IfurascelllarH-Ois 1.5% ofthe total body weight, ‘and tol intracellular HO is $88 ofthe teal body HO, the tra scllular H.O and intracellular HO represent what percent of the {otal body 110 and tral body weight. respecsively 1.12 A group of 30 feshmaa attead a pany in a wom which wear sures 20 ft by 20 f by 10 fe Each student takes up about 2.5 evbic fect and gives oat shout 375 BTU of hex per hoor Caleulae the tir termperatre rise during the firs 15 minutes of the meeting if the room is completely sealed and insulated. Assume the C, for sic is 0.1713 BTUbm F Assume ais isan ideal gas a¢ 70 F and 17 psi, Remember that the heat absorbed by the ai, Q. is related 1 the mass of the at, mn, the beat capacity ofthe air. Cand the ‘change in temperature by the following relationship: 2 Follows: Plasma 4.5% ofthe body weight and 7.3% of the 10 oem f Gar amc.as— 1) ‘The mass of at can be ebtained from the ideal gas law: pv = er whore Pis the gas pressure, Vis the volumes ofthe gas, Mot isthe snolecular weight ofthe gos and & iste ideal gas constant 1.15 Arevorist decides to poison all he people in.sn airtight ule ing by pumping a nixne of gas thai contains I toxie gas nto the 100,600 fe building (which holds G00 Nb of ai intially) az arate (of 6 Ibgimin. When the concentration of ths toi gas reaches 48%, ‘people will sort to de. The cesorst assumes thatthe mass af gas in the building will remain constant and calculated how long i wil rae 1o ill the poople. You realize thatthe building is aight and that Ene mass of gas i the building wall menease with ie, Wil take more o¢lss time than the terrorist cniginally calculated 19 2= ccumulate a toxic level of gas into the building? (Make sure you do both the terarist’s calculation and th comrected calzalation,) 141 Compound A flows ito a wellstired tan a! 10 kg/min and immediately and complerely roets 10 Tort B. The tank i iniially at 30°C. and the reaction i exothcrmic with a heat of reaction of = 21 kik, B flows out of the tank at 10 ka/min tially, there is 1000 kg of B i the tank. The heat capacity (CofA s 6 EL/kg C. ‘The heat capacity (C, and C,)or Bis 4 Lkg. Assume that the cor tents of the tank consist entirely of B. (a) What isthe temperatre in the tank as function of time” (by If ee exit valve for Bie not opened (tics, the flow rte of from the tank is zero}, wha wil be the temperature inthe tank afer 1D minutes? Chara-Grate-marenea Mathes or Eines, Fear tee eating. compet rea Soking 24 115 Cancer cells grow exponentially with doubling ime of 20 ‘when they have an unlimited nutrient supply. However. as the cells start © form a solid spherical tumor without a blood supply growth ‘atthe center of the funior becomes limited, and eventually c2lls start io di (2) Exponential growth of cell number. N. can be expressed as shown, where 1s the grow rate of the eels. For cancer cells, find the valve oF (b) Write an equation cha al deseribe the rate of chang co [ scmamorwecat terre so faienig Pblen cengrer, MATHEMATICAL MODELING AND ENGINEERING PROBLEM SOLVING volume during exponential growth given that the diameter of an individual cell is 20 microns After a particular type of tumor exceeds 500 microns in diam. ter. the cells ar the conte of the turn de (but contin to tke up space in the tune), Determin tumor to exceed this etic! size, hw Tong it wll take forthe Chara-Gnae Mathes or Eines, Fear tee eat 24 Lmeoeigcompotrs | 2 Propamming ant oe) sed Co hea Stave engonas 20 CHAPTER 2 Programming and Software In the previous chapter, we used a net force to develop a mathematical model to predict the ‘all velocity of a parachutist, This model took the form of a differential equation, de ‘ a n We also teamed that a solution to this equation could he obiained by a simple nomerical approach called Euler's method, ay asm Tae Given an initia! vondition, this equation ean be implemented repeatedly wo compute the velocity asa function of time, However, o obtain good accuracy, many small steps must be taken, This would be extremely laborious and time-consuming to implement by hand However, with the aid of che compoter, such calculations ean be performed easily. ‘So our next task i to figure out how to do this, The present chapter will introduce you ‘to how the computer is used ss a 109! to obtain such solutions PACKAGES AND PROGRAMMING Today. there are two types of software users. On one hand, there are those who take what they are given. That is. they limit themselves to the capabilities found in the sofiware’s standard mode of operation. For example. it is a straightforward proposition t0 solve a sys fem of linear equations or to gencrate of plot of 2-y values with cither Excel or MATLAB. Because this usually involves a minimum of effort. most users tend to adopt this “vanilla” mode of operation. In addition, since the designers of these packages anticipate most typi- cal user needs. many meaningful problems can be solved in this way. But what happens when problems arise that are beyond the standard capability of the ‘oo! Unfortunately. throwing up your hands and saying. “Somry boss. no can do!” is not acceptable in most engineering circles, In such cases, you have 1wo alternatives. inst. you can look fora different package and see if itis capable of solving the prob Jem, That is one of the reasons we have chosen to cover both Excel and MATLAB in this book. As you will see, neither one is all encompassing and each has different strengths 25 26 Chara-Grate-marenea Mathes or Eines, Fear tee Lmeoeigcompotrs | 2 Propamming ant oe) rea Stave cengrer, PROGRAMMING AND SOFTWARE 2.2 By being conversant with both, you will greatly increase the range of problems you can address. Second, you can grow and become a “power user” by leaming to write Excel VBA! macros or MATLAB M-files, And what are these? They are nothing more than computer programs that allow you to extend the capabilites ofthese tools. Because engineers should never be content to be tool limited, they will do whatever is necessary to salve their prob- lems. A powerful way to do this isto Tear to write programs in the Excel and MATLAB en- ‘vironments, Furthermore, the programming skills required for macros and M-files are the same as those needed 10 effectively develop programs in languages like Fortran 90 or C. ‘The major goal of the present chapter is o show yo how this can be done. However, ‘we do assume that you have been exposed 10 the rudiments of compater programming. Therefore, our empiasis here is on facels of progeamming that ditectly affeet its use in ‘engineering problem solving 2.1.1 Computer Programs Computer programs are merely 8 set of instructions that direct the computer to perform & certain task, Sinee many individuals write programs fora broad range of applications, most high-level computer languages, like Fortran 90 and C, have rich capabilities. Although some engineers might need to tap the fall range of these capabilities, most merely require the ability o perform engineering-oriented numerical calculations, ‘Looked at from this perspective, we can narrow down the complexity 10 a few pro: ‘gramming topies. These are Simple information representation (constants, variables, and type declarations) Advanced information representation (data structure, arrays, and records). “Mathematical formulas (assignment, priority rules, and intrinsic functions). Inpuvoutput Logical representation (sequence, selection, and repetition) ‘Modular programming (runetions and subroutines). Because we assume that you have had some prior exposure to programming, we will not spend time on the first four ofthese areas. At best, we offer them as a checklist that coy: cers what you will need to know to implement the programs that follow. However. we will devote some time to the lst two topics, We emphasize Logical rep resentation because itis the single area that most influences an algorithn’s coherence and ‘understandability. We include modular programming because it also contributes greatly to 2 program's organization. In addition, modules provide a means to archive useful algo: rithms in a convenient format for subsequent applications, STRUCTURED PROGRAMMING In the early days of computer, programmers usually did not pay much attention to whether their programs were clear and easy to understand. Today, itis recognized that there are many benefits co writing organized, well-structured code, Aside from the obvious benefit of making sofiware much easier to share, it also helps generate much more efficient "Wha ste aronym fr Visa Bs for Appicaions Chara-Grate-marenea Mathes or Eines, Fear tee Lmeoeigcompotrs | 2 Propamming ant oe) rea Stave cengrer, 2.2 STRUCTURED PROGRAMMING a7 program development. That is, well-structured algorithms are invariably easier to debug and test, resulting in programs that take a shorter time to develop, test and update ‘Computer scientists have systematically studied the factors and procedures needed to develop high-quality software of this kind, In essence, structured progranaming is a set of rules that prescribe good style habits for the programmer, Although structured program- ‘ming is flexible enough to allow considerable creativity and personal expression, its rules ‘impose enough constraints to render the resulting codes far superior to unstructured ver sions. In particular, the finished product is more elegant and easier to understand. A key idea behind structured programming is that any numerical algorithwn can be composed using the three fundan iol structures: sequence, selection, and repeti= tion. By Timiting ourselves to these structures, the resulting computer code will be clearer and easier to fallow. Inthe following paragraph, we will deseribe each of these structures. To keep this de scription generic, we will employ flowcharts and pseudocode, A flowchart is 2 viswal or graphical representation of an algorithm, The flowchart employs a series of Hlocks and a= rows, each of which represents a particular operation oF step in the algorithin (Fig, 2.1, The arrows represent the sequence in which the operations are implemented Nor everyone involved with computer progeamming agrees that Mloweharting i a pro ductive endeavor In fact, some experienced programmers do not advocate Sowchar However, we feel that there are three good reasons for Studying them, First, they are still used for expressing and communicating algorithms, Second, even iFthey are noc employed rontinely, there willbe times when they will prove useful in planning, unraveling, oF com municaling the logic of your own or someone else's program. Finally, ax] most important for our purposes, hey are excellent pedagogical tools, From a teaching perspective, they il oe FIGURE 2.1 Symbols used in flowcharts SYMBOL NAME Funcrion A_ AA, Fomines Roprosonts the flow of logic. The hurnps on the horizontal arrow ineioato thet itpasses over and does nat connect with the vertical Fowlines Process Represents calculations or data manipulations. presents inputs or outputs of data and information. tomative paths to be followed. Junetion Represents the confluence of flowlines. off page Roprosonts a break that i continued on another page. connector Count-controlied Used for loons which repeat a prespecified numberof trations. loop <> Decision Roprosonts @ comparison, quoston, or decision that dotormines @ 8 ae Chara-Grate-marenea Lmeoeigcompotrs | 2 Propamming ant oe) rea Stave cengrer, PROGRAMMING AND SOFTWARE ‘are ideal vehicles for visualizing some of the fundamental control structures employed in ‘computer programming. An altemative approach to express an algorithm that bridges the gap between flow- charts and computer code is called pseudocode, This technique uses code-like statements in place of the graphical symbols of the flowchart. We have adopted some style conven- tions for the pseudocode in this book. Keywords such as IF, DO, INPUT, ete. are capital- izad, whereas the conditions, processing steps, and tasks are in lowercase. Additionally, the processing steps are indented. Thus the Keywords form a “sandwich” around the steps to ‘visually define the extent of each contol structure. One advantage of pseudocode is thal itis easier wo develop a program with it than ‘with a flowchart, The pseudocode is also easier 0 modify and share with oshers, However because of their graphic form, Moweharts sometimes are hevler suited for visualizing com. plex algorithms. Tn the present text, we will use flowelians for pedagogical purposes Pseudocode will be our principal vehicle for communicating algorithins related to mameri= cal methous 2.2.1 Logical Representation Sequence. The sequence structure expresses the trivia ida that unless you direct it oth exwise. the computer code iso be implemented one instruction ata time. Asin Fig. 2.2, the structure can be expressed generically a a Hlowehart or as pseudocode. Selection In contrast 10 the step-by-step sequence structure, selection provides "means to split the program's flow’ into branches based on the otcome of a logical condi- tion, Figure 2.3 shows the two most fundamental ways for doing this, The single-altemative decision, or IF/TTIEN structure (Fig, 2.34), allows for a detour in the program flow ifa logical condition iste, If itis False, nothing happens and the pro- ‘gran moves directly fo the next statement following the ENDIF. The double-alternative de= cision, or IF/THEN/ELSE structure (Fig, 2.36), behaves in the same manner for a ime con= dition, However, if the condition is false, the program implements the code hetween the ELSE and the ENDIF. Mathes or Eines, Fear tee 28 FIGURE 2.2 i ee i (@Flowsrat — () Pseudocode Chara-Grate-marenea Lmeoeigcompotrs | 2 Propamming ant oe) Matted Eines, area Save cengrer, Fear tee 2.2 STRUCTURED PROGRAMMING 29 Flowchor Psoudocode Tue TF condition THEN True block True Black ae (a) Sinale-ohiernative structure WFTHEN) alos we TF condition THEN True block ese False block FIGURE 2.3 Falge Block “True Block lowchort and proudacods for Fe simple selection constucts (a) Single demative selec IF/THEN) ond (b) double abernctive selection IF THEN ELSE, rer (b) Double etornative structure lf THENELSE) Although the LW/THEN and the IF/THEN/ELSE constructs are suffivient to construct any numerical algorithm, two other variants are commonly used. Suppose that the ELSE clause of an IH/THEN/ELSE contains another IF/THEN, For such cases, the ELSE and the IF can be combined in the IF/THEN/ELSEIF structure shown in Fig. 24a. Fig, 2a there is a chain or “cascade” of decisions, The first one is the IF statement, and exch successive decision is an ELSEIF statement. Going down the chain, the first condition encountered that tests crue will cause a branch to its corresponding code block followed by an exit of the structure. At the end of the chain of conditions, if all che conditions have tested false, an optional ELSE block can be included. ‘The CASE structure is «variant on this type of decision making (Pig. 2.4). Rather than testing individual conditions, the branching is based on the value of a single rest expression. Depending on its value, different blocks of code will be implemented. In addition, an optional block can be implemented if the expression takes on none of che prescribed values (CASE ELSE). Repetition, Repetition provides a means to implement instructions repeatedly. The resulting constructs, called éoops, come in two “flavors” distinguished by how they are terminated Chapra-Geate:narencat | Ldeeting. comptes | 2.Froqranming and oe) Matted Eines, area Save cengrer, Fear tee 30 PROGRAMMING AND SOFTWARE Flowchart Pseudocode JF condition He SELF con Block ELSEIF cord? Block ese Blocks END a (@) Mutvattornatve strucnire (F/THENELSEIF) SELECT CASE Tost CASE vous, CASE Volue. ioe, Block, Block, ioee, case 56 (b) CASE structuro [SELECT or SWITCH) FIGURE 2.4 Flawchari ord pseudocade fer supplementary selacicn or branching constucls. (a) Mali: dleinative selection (F/IHEN/ELSEH and fb} CASE condect ‘The first and most fundamental type is called a decision loop because it terminates based on the result of a logical condition. Figure 2.5 shows the most generic type of deci- sion loop. the DOEXIT construct, also called a break loop. This structure repeats until 8 logical condition is true Itis not necessary to have two blocks inthis structure. Ifthe first block is not included, the structure is sometimes called a pretest laop because the logical testis performed before anything occurs. Alternatively, if the second block is omitted, i is ealled a positest loop. Chara-Grate-marenea Lmeoeigcompotrs | 2 Propamming ant oe) Matted Eines, area Save cengrer, Fear tee 2.2 STRUCTURED PROGRAMMING 31 Flowchart Pseudocode oo. Block. IF eougition EXIT Brock. e090 FIGURE 2.5 The DOEM or rook loo, FIGURE 2.6 Tho courtcantoled or DOFOR tnt Peoudocode Dorow i - start, finish, step Bock eND00. Because both blocks arc included, the general case in Fig. 2.5 is sometimes called a midiest loop. It should be noted that the DOEXIT loop was introduced in Fortran 90 in an effort to simplify decision loops. This contsol construct is a standard part of the Exeel VBA macro language but is not standard in C or MATLAB, which use the so-called WHI Because we belicve that the DOEXIT is superior. we hive adopted it as our decision loop structure throughout this book, In order to ensure that our algorithms are directly imple~ ‘mented in both MATLAB and Exce!. we will show how the break loop can be simulated with the WHILE structure later in this chapter (see See. 2.5), ‘The break loop in Fig. 2.5 is called a logical loop because it terminates on a logical condition. In contrast, a couat-controtied or DOFOR loop (Pig. 2.6) performs a specified number of repetitions, or iterations. ‘The count-controlled loop works as follows. The index (represented as iin Fig. 2.6) is Variable that is set at am initial value of start. The program then tests whether the ides is 32. Chara-Grate-marenea Mathes or Eines, Fear tee Lmeoeigcompotrs | 2 Propamming ant oe) rea Stave cengrer, PROGRAMMING AND SOFTWARE EXAMPLE 2.1 less than or equal to the Final value, finish If so, it executes the body of the loop. and then yeles back to the DO siatement, Every time the ENDDO statement is encountered, the index is automatically increased by the step. Thus the index acts as a counter. Then, when the index is greater than the final value (jinish), the computer automatically exits the Loop and transfers control to the line following the ENDDO statement. Note that for neatly all computer languages, including those of Excel and MATLAB, if the step is omitted, the ‘computer assumes it is equal to L? The numerical algorithms outlined in the following pages will be developed exelu- sively from the structures outlined in Figs. 2.2 through 2.6. The following example illustrates the basic approach by developing an algorithm to determine the roots for the ‘quadratic formula Aigotthin for Roots of a Quodratic Problem Statement. arthree=0 ‘The roots of a quadratic equation ccan be determined with the quadtatie formula, ey _ —bt VIP = aaa] on Develop au algorithm that does the following: Sloe 1: Pemps to wer forthe coo Sk 2: keplnarts the quodiaic Exrule, gavdrg ageinst all svc fr example, woiding diision by 200 ord allowing‘ comes Sten 3: Deploys he ssrton, this, the vlies or Stoo 4: Alon th usr fe option fn 60 ‘nd tepeat he proces Solution. We will use a top-down approach to develop our algorithm, That is, we will suecessively refine the algorithm rather than trying to work out all the details the frst time round. To do this, let us assume for the present that the quadratic formula is foolproof regardless of the values of the coefficients (obviously not itve, but good enough for now), Astructured algorithm to implement the senieme is ” IAT 2, D, ¢ Pl = Cd + S08rt = ae) ida 12 =D —S0Rn — aecsi/i2a) DISPLAY Ps, 2 DISPLAY “Thy agatin? Answer yes o” 90 UF response TF respense = "no" pn "N aegtive stp ean be we a sc cases the lop termites when the ip sess than the Hind vale Chara-Grate-marenea Mathes or Eines, Fear tee Lmeoeigcompotrs | 2 Propamming ant oe) rea Stave cengrer, 2.2 STRUCTURED PROGRAMMING 33 A DOEXIT construct is used to implement the quadratic Formula repeatedly as long a the condition is false. The condition depends on the value of the character variable ‘response. If responve is equal to “yes” the caleulation is implemented. If not, that is, re- sponse = ‘no’ the loop terminates, Thus, the user controls termination by inputting a value {or response. Now although the above algorithm works for ceriain cases, it is not foolproof. Depend ing om the values ofthe coefMicients, the algorithin might not work. Here is what cam happen: «If a0, an immediate problem arises because of division by zero. In fact, close inspection of Eq. 2.1) indicates thar two different cases can arise, That is, IC) $0, the equation reduces wo a Tinear equation with one real root, —c/b. IC} = 0. then no solution exists, That is, the problem is isivial #1 #6, wo possible cases occur depending on the value of the discrimina d= PP = dae. Thats, Ifa & 0, 1wo real wos oceur Teal < 0, 1wo complex roots occur Notice how we have used indentation to highlight the decisional structure that underlies the mathematics. This structure then readily translates 1 a set of coupled IFITHENTEL SE structures that can be insevted in place of the shaded statements in the pre= vious code (o give the final algorichom INUT a,b, © = Oreo = dha = 0 THEM FD 0 Th r= ob RSE DISLAY "Iyial solution ent ELSE disor = bea ate sor > 0 DEN ri = (

1 Ten ee a0 dali = y teh omy vay + cuit +o paren ie i atr evapo DISPLAY y {As soon as we enter the loop, we use an IF/THEN structure to test whether adding 1 +t de will lake us beyond the end of the interval. It does not, which would usually be the ease at first, we do nothing. IF i¢does, we would need to shorten the interval by seiting the variable step Into ¢f ~ 1. By doing this, we guarantee that the next sep falls exactly on #f After we implement this final step. the loop will rerminate because the condition £ = 1 will tes true, Notice that before entering the loop, we assign the value of the time step, df, to another variable, . We ereate this dummy variable so that our routine does not change the given value of d¢ if and when we shorten the time step. We do this in anticipation that we raight need to use the original value of d somewhere else in the event that this code is integrated within a larger program. It should be noted that the algorithm is still not foolproof. For example, the user could hhave mistakenly entered a step size greater than the calculation interval, for example. tf ~ 11 =$ and dr = 20,’Thus, you might want to include error traps in your code to eateh such errors and to then allow the user to correct the mistake, MODULAR PROGRAMMING. Imagine how difficult it would be to study a textbook that had no chapters, sections, or paragraphs. Breaking complicated tasks or subjects into more manageable parts is one way to make them easier te handle. In the same spirit, computer programs can be divided into small subprograms, or modules, that can be developed and tested separately. This approach 1s called modular programming. ‘The most important attribute of modules is that they be as independent and selt- contained as possible. In addition, they are typically designed to perform a specific, well- defined function and have one entry and one exit point. As such, Uney are usually short (generally 50 to 100 instructions in length) and highly focused In standard high-level languages such as Fortran 90 or C, the primary programming clement used to represent each module is the procedure. A procedure is a series of computer instructions that together perform a given task. Two types of procedures are commonly ‘employed: junctions and subroutines. The former usually returns a single result, whereas the Jater returns several In addition, it should be mentioned that much of the programming related to software packages like Excel and MATLAB involves the development of subprograms. Hence. Chara-Grate-marenea Mathes or Eines, Fear tee 36, Lmeoeigcompotrs | 2 Propamming ant oe) rea Stave cengrer, PROGRAMMING AND SOFTWARE Excel macros and MATLAB functions are designed to receive some information, perform a calculation, and retuen results. Thus, modular thinking is also consistent with how pro- ‘gramming is implemented in package environments. “Modular programming has a number of advantages. The use of small, self-contained ‘units makes the underlying logic easier to devise and to understand for both the developer ‘and the user. Development is facilitated because exch module can be perfected in isolation, In Fact, for large projects, different programmers can work on individusl parts, Modular de- sign also increases the exse with whieh a program can be debugged and tested because errors ccan be more easily isolated. Finally. program maintenance and modification are facilitated ‘This is primarily due o the Fact that nev modules ean be developed to perform addivional tasks and then easily incorporated into the already coherent and onganized scheme. While all these atributes are reason enough to use modules, he most important reason related 10 numerical engineering problem solving is that they allow you © maintain your ‘own Hibrary of useful modules for later use in other programs, This will be the philosophy of this book: AI the algorithms willbe presented as mnedules ‘This approach is ilustrated in Fig, 2.7 which shows a fuanetion developed tw imple- ‘ment Euler's method, Notice that this Function application and the previous versions differ jn how they handle inpuviouipuc. Ta the former versions, input and output directly come from (via INPUT statements) and 10 (via DISPLAY statements) che user. Tn the function, the inputs are passed into the FUNCTION via its a meat list Function Eulertdt. tf. tf. y ‘and the output is returned via the assignment statement y= Bilerttt, ti tf. yi) In addition, recognize how generic he routine has become, There are no references to the specifics of the parachutist problem, For example, rather than calling the dependent FIGURE 2.7 sevdocnde ke solves a dillerentiol equaton Using Eulor’s method. ‘function that ANETION Fulercat. 1, UY, 9 b= nad o we cnr cpt = cyt, vay + ditt #6 bateh Ite tf eur enero 20 at > 0 THEN -t Chapra-Geate:narencat | Ldeeting. comptes | 2.Froqranming and oe) Matted Eines, area Save cengrer, Fear tee 24 EXCEL 37 variable v for velocity, the more generie label, y is used within the function. Further, notice that the detivativeis nor computed within he functioa by an explicit equation. Rather, another function, dy, must be invoked to compute it. This acknowledges the fact that We might want ‘touse this function for many different problems beyond solving forthe parachutist velocity 2.4 EXCEL Excel is the spreadsheet produced by Microsoft, Inc. Spreadsheets ate a special 1ype of ‘mathematical software that allow the user 10 enter and perform calculations on rows and columns of data. As such, they are a computerized version of a large accounting worksheet ‘0a which large interconnected calculations cam he implemented znd displayed. Because the centre calculation is updated when any value on the sheet is changed, spreadsheets are ideal for “what if?" sorts oF analysis, Excel has some builtin nusmerical capabilities inchiding equation solving, curve f ting, and optimization. [also includes VBA as a macro language that can be used (0 im plement numerical calculations. Finally, it las several visualization tools, suel as graphs and three-dimensional surface plots, chat serve as valuable adjuncts for mu analysis, In the present section, we will show how these capabilies can he used 10 solve the pare cchutist probe. “To do this, let us Fist set up a simple spreadsheet, As shown below, the First step i volves entering labels and numbers iato he spreadsheet cells. Be oD = a | 4. Parachutist Problem }2- [3m Bat kg rar 25 ys [at 413 & [zt ‘vou pda) vam js) [a uv vow [s- 2 Before we write a macro program ro calculate the numerical value, we can make our subsequent work easier by attaching names to the parameter values, To do this, select cells AG:BS (the easiest way 19 do this is by moving the mouse to A3, holding down the left ‘mouse button and dragging down to BS). Next, make the menu selection cert Nato ate Left column OK ‘To verify that this has worked properly, select cell B3 and check that the label “m” appears in the name box (locates! nthe left side of the sheet jst below the ment bars) Move to cell C8 and enter the analytical solution (Eq. 1.9), =9.88m/ed | L-exp(-cd mts) ) When this formula is entered, the value 0 should appear in cell C8. Then copy the formula down to cell C¥ to give a value of 16.405 m/s. All the above is typical of the standard use of Excel. For example, at this point you could change parameter values and see how the analytical solution changes. 38 Chara-Grate-marenea Mathes or Eines, Fear tee Lmeoeigcompotrs | 2 Propamming ant oe) rea Stave cengrer, PROGRAMMING AND SOFTWARE Now, we Will ilustrate how VBA macros can be used to extend the standard capabill- ties. Figure 2.8 lists pseudocode alongside Excel VBA code for all the control structures described in the previous section (Figs. 2.2 through 2.6). Notice how, although the details differ, the structure of the pseudocode and the VBA code are identical ‘We can now use some of the consirucis from Fig. 2.8 to write 2 macro function to ‘numerically compute velocity. Open VBA by seleeting* ols Macro Visual Basic Editor Once inside the Visual Basic Editor (VBE), sotect © Module ‘and a new code window will open up. The following VBA function ean be developed directly from the pseudocode in Fig. 2.7, Type it imo the code window. ion Explicit on Euler(dt, ti, t bin k Ag Single, © ‘Compare this macro with the pseudocode from Fig. 2.7 and recognize how similar they are. Also, see how we have expanded the function’s argument list to include the necessary parameters for she parachutist velocity model. The resulting velocity vis then passed back to the spreadsheet via the funtion name. Also notice how we have included another function to compute the derivative. This ccan be entered in the same module by typing it directly below the Euler function, Function dy(t, v, m, ed) const g A a8 ay =o - 4 Function “The hot kes combination AiGPLL is even quicker! Chara-Grate-marenea Mathes or Eines, Fear tee FIGURE 2. 12 lundoavental cor stucues in [a psexelacode care (0) Excel YA, Laven. compts | 2 rang ant ‘omen fae eas Save engona,20 (a) Pseudocode (0) Excel VBA JECTHEN: TF covet in TE b <> 0 Then True black lence s eure nd IECTHENVEL SE: IF condition THEW rf aso thea True Bleck = Sex (abe(a) False hock evare IHENVEL SEN TF congiton, THEN ELSEIF condit (on Brock: ELSEIF conest(on 1 » by / 0 #10 % Do. Block beiaa FF condition ExtT Tf i >+ 10 Then Exit Do OR i = start, Finish Brack ewan 39 Chapa-cnatemamecat Meet. comptes | 2 ror nt ‘omenciwt Mates Canes, a hel Sate tenga, Fear teen 40 PROGRAMMING AND SOFTWARE “The final step isto return co the spreadsheet and invoke the function by entering the following formula in cell BO wuler (at, a8, 29, 88,n,ca) ‘The result ofthe numerical tegration, 16.531, will appear in cell BD. You should appreciate what has happened here. When you enter the funetion into the spreadsheet cell, the parameters are passed into the VB.A program where the calcultion is performed and the result is then passed back and displayed im the cell In affect. the VBA macro language allows you to use Excel as your inpuvoutpat machanism, All sorts of ben efits arise from this fact. Forexample, now that you hve set up the calculation, you can play with t. Suppose that, the jumper was rmach heavier, say. m = 100 kg (about 220 pounds). Enter 100 into cell B3 snd the spreadsheet will update immediatly to show a value of 17.438 in cell B9. Change the mass back t 68.1 ky and the previous result, 16.531, antomatically reappears in cell B9 [Now let us take the process one step further by filling in some addtional numbers for the time, Enter the mumbers 4,6, ... 16 in cells A10 through A16. Then copy the formu- Jas from cells B9:C9 down to roves 10 through 16, Notice how the VBA program ealeu- lates the numerical result correctly foreach new row. (To verify this, change d to 2 and compare with the results previously computed by hand in Example |.2.) An additional em- bellishment would be to develop an x-y plot of the results using the Excel Chart Wizard ‘The final spreadsheet is showin below We now have created a pretiy nice problem- solving tool. You can perform sensitivity analyses by changing the values for each of the parameters. As each new value is entered, the computation and the graph would be automatically updated. Ii this imeractive nature that makes Excel so powerful. However recognize that the ability to solve this problem hinges on being able wo write the mero with VBA. x 5 1 Parachutist Problem 2 Zim 68.1 kg 60 4 led 128 kgie Sat 1s 50 6 zh num (evs) wanal (mis) | 40 5 ooo o.000 3 2 516.805 | 30 10 493 27.769 if 6 35022 36.pt2 | 20 —a— vnum (mis) 12 8 an252 41.005 13 10 45.017 44.073 | 10 vanal (mis) “ 2 arsw 47.490 15 4 aaamn—asana, | 0 ‘6 16 50.635 90.859 o 10 0 Chara-Grate-marenea Mathes or Eines, Fear tee Lmeoeigcompotrs | 2 Propamming ant oe) rea Stave cengrer, 2.5_ MATLAB. ar 25 Itis the combination of the Excel environment with the VBA programming language that cruly opens up a world of possibilities for engineering problem solving. In the coming chapters, we will illustrate how’ this is accomplished, MATLAB. MATLAB is the flagship software product of Mathwvorks, Ine., which was cofounded by the numerical analysts Cleve Moler and John N. Little. As the name implies, MATLAB was originally developed as a matrix laboratory. To this day, the major element of MATLAB is still the matrix. Mathematical manipulations of matrices are very conveniently imple ‘mented in an easy-to-use, interactive environment, To these matrix manipulations, MATLAB has added a variety of numerical functions, symbolic computations, and visua ization tools. As a consequence, the present version represents a fairly comprebensive tech- nical computing environment. MATLAB has a variety of functions and operators that allow convenient implementa tion of many of the numerical methods developed in this book. These will be described derail in the individual chapters that follow. In addition, programs can be written as so called mfles that can be used to implement numerical calculations. Let us explore how this is done. First, you should recognize that nommal MATLAB use is closely related 10 pro- _grumming, For example, suppose that we wanted to determine the analytical solution to che parachutist problem. This could be done with the following series of MATLAB commands cd* (-exp{-cd/m*t£)) ‘with the result being displayed as ‘Thus, the sequence of commands is just like the sequence of instructions in a typical pro- gramming language. Now’ what if you want to deviate from the sequential structure. Although there are ‘some neat ways to injeet some nonsequential capabilities in the standard command mode, the inclusion of dacisions and loops is hest done by creating a MATLAB document called aan mile, To do this, click on Bile New mfile andanew window will open with a heading “MATLAB Editor/Debugger.” In this window, ‘you can type and edit MATLAB programs, Type the following code there canst) 42 Chara-Grate-marenea Mathes or Eines, Fear tee Lmeoeigcompotrs | 2 Propamming ant oe) rea Stave cengrer, PROGRAMMING AND SOFTWARE Notice how the commands are written in exactly the way as they would be writen in the front end of MATLAB. Create a directory where you will keep your MATLAB m-files (for the present example we will use one called ¢:\MATLAB\mifiles|.) Save the program ‘on this directory with the name: analpara. MATLAB will automatically atigeh the exten- sion m to denote it as an m-fle: anslpara.m. ‘To mun the program, you must go back to the command mode. The most direct way to do this is to click on the "MATLAB Command Window” bation on the task bar (which is usually at the bottom of the screen), Next we have to tell MATLAB the location of the files. Click on Eile Ser par Awindow called “Path Browser” will open. Change the current directory toe\MATLABY nfiles\and press enter: Now exit the Path Browser by clicking on the x box inthe upper right comer. Every time yOu star up MATLAB, you will have fo set the path in this way ‘The program can now be sun by typing the name ofthe m-file, analpara, which should Took like [Now one problem with the foregoing is that it is set up to compute one case only. You ‘can make it more flexible by having the user input some of the variables. For example, sup pose that you wanted to assess the impact of mass on the velocity at 2s, The m-file could be rewritten asthe following to accomplish this 9.8; vegan/ed (L-exp (-e fe this as analpara2.m, If you typed analpara2 while being in command mode, the prompt would show ka} ‘The user could then enter a valuc like 100, and the result will be displayed as 17,3420 ‘Now it should be pretty clear how we ean program a numerical solution with an m-file In order to do this, we must first understand how MATLAB handles logical and looping structures. Figure 2.9 lists pseudocode alongside MATLAB code for all the control structures from the previous section. Notice how. although the details differ. the structures ofthe pseudocode and the MATLAB code are identical. In particular, look at how we have represented the DOEXIT structure. In place of the DO, we use the statement WHILE(1). Because MATLAB interprets the number | 2s corresponding to “true,” this statement will repeat infinitely in the same manner as the DO Capr-corteramect | Lectin. computers | 2 Protanminn ant one nkcat Matted Eines, fae eas Save engona,20 Fear tee (a) Pseudocode () MATLAB JECTED: TF covet in Lf bos True black Dec sb eure nd /THENIELS! IF corcition THEW itaso True Bleck = eqxt (abetal) False hock = egertay evare /THEWELS! TF congiton, THEN ELSEIF condit (on 2 Brock: ELSEIF cone 1 b) (10; Block is FF condition ExtT if iv 1 end end FIGURE 2.9 Tho fundermensel oral sructues i [al pseudocode OR i = start, Finish = asdoe2 are (bi the MATLAB Stock ® i programming languoge ewan ead 43 Chapra-Geate:narencat | Ldeeting. comptes | 2.Froqranming and oe) Matted Eines, area Save cengrer, Fear tee a4 PROGRAMMING AND SOFTWARE statement. The loop is terminated with a break command, This command transfers control {the statement following the end statement that terminates the Joop. The following MATLAB m-file can now be developed directly from the pseudocode in Fig. 2.7. Type it into the MATLAB Editor/Debugger 9.8; tmass (kg) ") hear waile (L) lec ede ste he ct ead ava : end disp('velocity (ve) +") dispiv) Save this file as numpara.m and return fo the command mode and run it by entering: ‘numpara, The following output should result: nase (ha): 100 velocity (m/s) 17.43 As a final step in this development, let us take the above m-file and convert it into & proper function. This can be done in the following mile based on the pseudocode from Fig. 2.7 nection euler = £(de,ti,tfyism, Chara-Grate-marenea Mathes or Eines, Fear tee Lmeoeigcompotrs | 2 Propamming ant oe) 9 Eo hes Stave engonas 20 2.8 OTHER LANGUAGES AND LIBRARIES 45 2.6 Save this file as euler m and then create another m-file to compute the derivative, lca Fmd Save this file as dy.m and return to the command mode. In order to invoke the function and see the result, you can type in the following commands ‘When the last command is entered, the answer will be displayed as 16.5309 I is the combination of the MATLAB environment with the m-file programming age that uly opens up a world of possibilities for engineering problem solving. In che ‘coming chapters we will lusirate how this is accomplished, OTHER LANGUAGES AND LIBRARIES In the previous sections, we showed how Excel and MATLAB function procedures for Euler's method could be developed from an algorithm expressed as pseudocode. You should recognize that similar functions can be written in high-level languages like Fortran 90 and C+. For example, 2 Fortran 90 function for Euler's method is jon eulertae, ti, cay REAL dt, ci, tf, vir Feal bh, t, y, dyat Sydr = ayn, y. m, cay vey + dvat th Exit Chara-Grate-marenea Mathes or Eines, Fear tee 46 Lmeoeigcompotrs | 2 Propamming ant oe) rea Stave cengrer, PROGRAMMING AND SOFTWARE PROBLEMS For C, the result would look quite similar to the MATLAB function. The point is that once a well-structured algorithm is developed in pseudocode form, it ean be readily imple- ‘mented in a variety of programming environments. In this book, our approach will be to provide you with well-structured procedures writ- ten as pseudocode. This collection of algorithms then constitutes a numerical ibeary that ccan be accessed to perform specific numerical tasks in a range of software tools and pro- ‘gramming languages, ‘Beyond your own programs, you should be aware that commercial programming braries contain many useful numerical procedures. For example, the Numerical Recipe brary includes a large range of algorithms written in Fortran and CS"These procedures ste deserited in both book (For example, Press et al, 1992) andl electronic form, For Fortan, the JMSZ_(Fnternational Mathematical and SiatistiealFibrary) providesover 700 procedures spanning all she numerical areas covered in this text, Because ofthe wide- spread use of Fostran in engineering, we include IMSL applications throughout the book 2.1 Write pseudocode fo implement the flowchart depicted in 22 A valve for the concentration of a pollutant in a lake is Fig. P21, Make sre that proper indentation is clued wo makethe recorded on each cant in a set of ind cards. Acard marked “end io data is placed atthe end ofthe set. Write an algorithm to deter mine the sum and the average ofthese values, 2.3 Write a siroctared flowchart for Prob. 2.2 2.4 Develop. debug, are! document a subprogram to determine the roots of a quadkatic equation in ether a high-level language or a macro language of your choice. Use a subyoutine procedure 10 ‘compute the roots (either real or complex), Perform rest runs (ya =0,b=—3 2.5 The sine Funotcn can be evaluated by the Following infinite se White an algorii to implemen tis formula so that it computes snd prints out the values of sin as cach crm in the series is added. [mother words, compute an print in sequence the valves for Figure P2.1 ‘MATLAD. Intonation en all the Numcical Recipe products co he found a siiww acon T serena ant Chapra-Geate-rarencat | eet. Computers, oe) Matted Eines, fae eas Save engona,20 Fear tee PROBLEMS a7 up wo che order term of your choosing. Fo each of tie above, com- Pte and print ou the percent relative enor as tue ~ series approximation te Semon = x 100% 26 Develop a simewired flowehan for Prob. 2.5, und write reuclocode For Pro 2.5. 2.7 Develop, debug, and documents subprozzsm for Prob, 25 inci ther a high-level language or a macro language oF your choice, Em ploy the library funcrien for the sine n your computer ty derertine the true value. Have the program prin cut the series approximation andthe error ateach step. As atest case, employ the program to com. Pate sia 1.5 for up to and including te wer x'8/151 Interpret your resus. 2.8 The following algorithm is designed to determine a grade fora course that consists OF cuizzes, homework, and a Fin exam Step | Step 2 Input course number and name Input weighting factors for quizzes (WQ). homework (WH), an te final exam (WF). Input quiz grades andi determine an average quia grade AQ. Input homework grades and determine an average home- work grade (AHD, this course ha final grade, continue to sep 6 In0t. £0 to stp 9. Input final exam grads (FE), Deterinine average grace AG aecorting Steps Step 4 Step Step 6! Step 7 WO»: AQ+ WH < ANE WPAFE ag WO+ WH + WF Step Step: Go to step 10. Determine average grade AG according 0 WO x AQ EWI AL Wwo=WH AG ‘Step 10: Print out course number name, and average ara Step 11: Terminate computation. Write, debug, and document a stractured computer program based a this algorithm, Test it using the Following data to calculate & grade without the final exam and a grade with the Final exaen WQ= 30; WH =30; WE= 40; quizzes = 98, 95. 90, 60, 99 homework = 95,90, 86, 100, 100, 77 and final exam = 91 29 An amount of money P is invested in am account where inter est is compounded atthe end of the period, The fawure wor F Yielded at an interest rat i after a periods may be determined from the following formulation F=Patir Wikie a subpngram thar will calculate the fate worth of an in vestmeat in cither a high-level Language oF a macro language of your choice, The input to the program should include the inital in ‘vesoment P, the interest rate (as a decimal}, and the aumber of yes n for which the fuuae worth isto be calculated. The output ‘Should also include these values, The oatpat should also inelode, ia 8 labeled table format, tho future worth for each yeer up (9 and i lading the mth year, Run the program for P — $100,000, 10.0 and n = 25 years 2.10 The average daily temperature for an area ean be appro ‘mated by the following Function, T = Tae + To Trunn Voosten(t ~ fy)? where Trou =e average annual temperate, Tyo =the peak temperature, co =the frequency ofthe ennual variation |= 21/65), ‘and tyaq = day 0F the peak temperature (= 205 a), Parameters for some U.S, towns are listed in Table P2.10, ‘Table P2.10 Mean daily sir temperature parameters for some selecied U.S. locotone Taan (°C) Tyan CO) 2 282) 2 396 Seal, WA 106 76 Boston, Ma OF 29 Develop @ subprogram in either © high-level langu macro language of your cholve that computes the average tempers= tare herween two days of the year entered by the user Test for (a) Janvary-February in Bismarck, North Dakota (t= 010 59) and July-August in Yuma, Aizoaa (f= 189 1042), 2.11 Economie formulas ste available t0 compute annual pay= ments for loans. Suppose that you borrow an amount of money P and agree to repay tin» annual payments a& an interest rae of. ‘Te formula to compute the annual payment Ais iain OPT Waite a sebprogram in either a high-level language ora macro lan= guage of your choice to compute A. Test it with P = $35,000 and an Anterestrate of 1S percent (i = 0.15). Set up the program so that you ‘can evan 28 many yalwes ofr as yon ike. Compe results for n= 1.2.3.4 and 5 2.12 Develop, debug, and test a Subprogram in either high-level Tanguage ora maero language of your ehoice to compuxe the veloc ity ofthe falling parachutis: as outined in Example 1.2. Design the program so tit it allows the wer to input values forthe daz Capr-corteramect | Lectin. computers | 2 Protanminn ant one nkcat ethde trainers freer Aas Setware cones 2 Fear teen 48 PROGRAMMING AND SOFTWARE coeffelent and mass, Test the progtom by duplicating the results 214 Figure P2.14 shows s cylindrical tank with n conieal base, EF from Eaample 1.2. Repeat the computation buremploy tepsizes of the liquid level is quite low in the conical part, the volume is T and 05 5. Compare your resulls with the analytical solution simply the conical volume of liquid. f the liquid level is midrange ‘oblained previcusly in Example I-1. Does 2 smaller sep size make inthe cylindrical part, the teal volume of liquid includes the filled the results better oF worse? Explain your reals conical par and the parilly filled eylindct part. 2.13 The bubble sort isan inefficient bu easy-o-program. sorting Write a function procedure to compute the tank's volume: as & technique. The idea behind the sort is to move down through an Function of given values of P and d. Use decisional control struc array comparing adjcoent pars and swapping the values if they sre tures (ike I7Theo, Elst Else, End i. Design the function so that fou of order. For this method to sort the aray completely, may it returns the volume forall cases where the depth ic Fess than 38. need to pass though it many times. As the passes proceed for an Return an err message (“Os ‘f you overt the tank, that sascending-cvder fort, the smaller elements in the array appear to is,d > 3K. Test t withthe following date: rise toward the op like bubbies. Eventally, shee will be a pss throxgh the aaray where no swaps ave required. Then the array is . Ga : : Figure P2.14. relative to an crigin into dimensional space (Fig. P21S) t wien = ue Figure P2.13 Figure P2.15 Capr-corteramect | Lectin. computers | 2 Protanminn ant one nkcat Mathes or Eines, Fear tee fae eas Stave PROBLEMS +The horizontal and vertical distances Cx, 9) in Cartesian coord The radius and angle (r 0) in todil coordinates ns olacively straightforward ro compute Cartesian coordinates (2, yan the basi of polar coordinates 9). The reverse process is not sa simple. The radius can be computed by the following Formel It the cootinates lie within the first and fourth coordintes (tha it > 0). thea simple formala can be wsed to vompute ? vom () ‘The dilficulty arises forthe ther eases. The following table sum. rmarizes the possibiies x y ‘ engona,20 49 (9) Write a wellstnactited flowchart fora subroutine procedure to calculate ancl # as a function x any Ex for6 in degrees (b) Wate a well strctares function procedure based on your flow’ chart Test your program by sing itt fill ovt the flowing table the final rests 50 Chara-Grate-marenea Mathes or Eines, Fear tee eating. compet 9 Eo hes oe) engonas 20 CHAPTER 3 Approximations and Round-Off Errors Because so many of the methods in this book are straightforward in description and appli= cation, it would be very tempting at this point for us to proceed directly tothe main body of the text and feach you how to use these fechniques. However, understanding the caneept of error is so important to the effective use of numerical methods that we have chosen to devote the next two chapters to this topic. ‘The importance of error was introduced in our discussion of the falling parachutist in Chap. I, Recall that we determined the velocity ofa falling parachutist by both analytical and numerical methods. Although the auimerical technique yielded estimates that were close to the exact analytieal solution, there was a discrepancy. or error because the numer: ical method involved an approximation. Actually. we were Fortunate in that ease because the availablity of an analytical solution allowed us to compute the error exeetly, Por many applied engineering problems, we cannot obtain analytical solutions. Therefore. we cannot compute exactly the errors associated with our numerical methods, In these cases, we must settle for approximations or estimates of the errors. Such errors are characteristic of most of the techniques described in this book. This statement might at first seem conttary to what one normally conceives of as sound engi- neering. Students and practicing engineers constantly strive to limit errors in their work. ‘When taking examinations or doing homework problems, you are penalized, pot rewarded. {or your errors, In professional practice, errors can be costly and sometimes catastrophic. Ifa structure oF deviee fails, lives can be lost. Although perfection is a laudable goal, it is rarely, if ever, attained, For example. spite the fact that the model developed trom Newton's second law is an excellent approximation, it would never in practice exactly predict the parachutist’s fall. A variety of factors steh as winds and slight variations in air resistance would result in deviations from the prediction. If these deviations are systematically high o low, then we might need to develop a niew model. However, if they are randomly distibuted and tightly grouped round the prediction, then the deviations might be considered negligible and the model deemed adequate. Numerical approximstions also introduce similar discrepancies into the analysis, Again, the question is: How much error is present in our ealeulations and is it tolerable? Chara-Grate-marenea Mathes or Eines, Fear tee eating. compet oe) 9 Eo hes engonas 20 3.1_ SIGNIFICANT FIGURES 51 3.1 ‘This chapter and the next cover basic topics related to the identification, quantifica- tion, and minimization of these errors. In this chapter, general information concemed with the quantification of error is reviewed in the fist sections. This is followed by a section on ‘one of the two major forms of numerical eror: round-off error. Rownd-of error is due to the fact that computers can represent only quantities with a finite number of digits. Then Chap. 4 deals with the other major form: truncation error. Truncation error is the discrep ancy introduced by the fact that numerical methods may employ approximations to rep resent exact mathematical operations and quantities, Finally, we briefly discuss errors not directly connected with the numerical methods themselves. These include blunders, Formulation or model ersors, and data uncertainty, SIGNIFICANT FIGURES This ook deals extensively with approximations connected with the manipulation of num bers. Consequently, before discussing the errors associated with numerical methods, i is useful «9 review basic concepis relsted © approximate representation of the nucthers themselves. Whenever we employ s number in a computation, we must have assurance that i cean be used with confidence. For example, Fig, 3.1 depieis a speedometer and odometer ‘rom an automobile. Visual inspection of the speedomever indicates thatthe ear is traveling between 48 and 49 mm/h, Because the indicator is higher then the midpoint between the markers on the gauge, we can say with assurance that the car is traveling at approximately 49 kin/h, We have confidence in this result because two or more reasonable individuals reading this gauge would arrive atthe sume conclusion, However, let us say that we insist that the speed be estimated to one decimal place. For this case, one person inight say 48.8, FIGURE 3.1 An automobile speedometer and adcmaorilusvating ho concep cf. sigaticart Figure 52. Chara-Grate-marenea Mathes or Eines, Fear tee eating. compet rea cengrer, APPROXIMATIONS AND ROUND-OFF ERRORS ‘whereas another might say 48.9 km/h. Therefore, because of the limits of this instrument, only the first two digits can be used with confidence. Estimates of the third digit (or higher) must be Viewed as approximations. It would be ludicrous co claim, on the basis of this speedometer, thatthe antomabile is traveling at 48.8642138 knvl, In contrast, the odome- ter provides up to six certain digits, From Fig. 3.1, we can conclude that the car has trav- cled slighily less than 87,324.5 km during its lifetime. In this case, the seventh digit (and higher) is uncersin. “The concept ofa significant figure, or digit, has been developed to formally designate the reliability of a numerical value. The significam digis of a number are those that can be used with confidence. They correspond to the number of certain digits plus one estimated ligt, For example, the speedometer and the odometer in Fig. 3. yield readings of three ‘and seven significant figures, respectively. For the speedometer, the wo certain digits ate 48. 1is conventional 10 set the estimated digit at one-half of the stullest scale division on wement device, Thus the speedometer reading would consist of the three signifi- 48.5. Ina similar Fashion, the odometer would yield a seven-significant- reading of $7,324.45 Aldiough itis usually a strsightforwant procedure to ascertain the significant figures ‘of a number, some exses can Tead to confusion, For example, zeros are not always signifi- cant figures because they may be necessity just 10 Tocate a decimal point. The numbers 0,00001845, 0.001845, and 0.001845 all have Four significant figures. Similarly, when trailing zeros are used in large numbers, it is not clear how many, ifany, of the zeros are significant, For example, at face value the number 45,300 may have three, four, oF five significandligits, depending on whether the zeros are known with confidence, Such uncer= tainty can be resolved by using scientific notation, where 4,53 x 108, 4.530 x 108, 4.5300 x 10* designate that the mumber is known to three, four, ang five significant figures respectively. The concept of significant figures has two important implications for our study oF ‘numerical methods: 1. Asintroduced in the falling parachutist problem, numerical methods yield approximate results, We must, therefore, develop criteria to specify how confident we are in eur approximate result. One way to do this is in terms of significant figures. For example, we might decide that our approximation is acceptable if itis correct to four significant figures. 2. Although quantities such as x. e. or v7 represent specific quantities, they cannot be expressed exactly by a limited aurnber of digits. For example, = 3.141592653589793238462643 ad infinitwn. Because computers retain only a finite number of significam Figures, such numbers can never be represented exsetly. The omission of the remaining significant Figures is ealled round-off enor Both round-off enor and the use of significant Figures to express ous confidence in ‘numerical result will be explored in detail in subsequent sections. In addition, the coneept of significant figures will have relevance fo our definition of accuracy and precision in the next soction, Chara-Grate-marenea Mathes or Eines, Fear tee eating. compet rea cengrer, 3.2_ ACCURACY AND PRECISION 53 3.2 ACCURACY AND PRECISION ‘The errors associated with both calculations and measurements can be characterized with segard to their accuracy and precision, Accuracy refers to how closely a computed or mea- sured value agrees with the true value. Precision refers to how closely individual computed ‘or measured values agree with each other, These concepts can be illustrated graphically using an analogy from target practice. ‘The bullet holes on each target in Fig. 3.2 can be thought of as the predictions of a numer ‘cal technique, whereas the bull’s-eye represents the truth. Inaccuracy (also called bias) is defined as systematic deviation from the truth. Thus, although the shots in Fig. 3.2c are ‘more tightly grouped than those in Fig. 3.24, the two cases are equally biased because they ate both centered on the upper left quadrant of the target. Jmprecision (also called wn certainty), on the other hand, refers 10 the magnitude of the scatter. Therefore, although Fig. 3.2) and d are equally accurate (shat is, centered on the bull’s-eye), the latter is more precise because the shors are tightly arouped. Numerical methods should be sufficiently accurate or unbiased to meet the require ‘ments of a particular engineering problem. They slso should be precise enough for adequate FIGURE 3.2 An eccrige ion martsmanship dhstoting he concepss ol aceuacy and precio tale and imprecise, (6 oecuals and imprecise (el inacaurae ond precise; (do preci. viate and Ineveasing accuracy 54. Chara-Grate-marenea Mathes or Eines, Fear tee eating. compet rea cengrer, APPROXIMATIONS AND ROUND-OFF ERRORS 3.3 EXAMPLE 3.1 engineering design. In this book, we will use the collective term error to represent both the inaccuracy and the imprecision of our predictions. With these concepis as background, We ‘can now discuss the factors that conteibute fo the error of nuinerical computations. ERROR DEFINITIONS ‘Numerical errors arise from the use of approximations to represent exact mathematical op- erations and quantities. These inelude sruncation errors, which result when approximations are used to represent exact mathematical procedures, and round-off errors, which result ‘when numbers having limited significant figures se used to represent exact numbers, For both types, the relationship berween the exact, oF true, result snd the approximation can be Thue value = approximation + error By rearranging Eq. (3.1), we find that the numerical error is equal to the tween the truth and the approximation, 3 in E, = true value — approximation en where Fis used 10 designate the exact value of the error, The subscripts included 1 des- ignate that this is the “true” error. This is in comtrast co other cases, as describe shortly where an 4c” estimate pf the error mast be employed. ig of this definition is thet it takes no account of the order of under examination, For example, an ertor of a centimeter is much more significant if we are measuring «rivet rather than a bridge. One way to aceount forthe mag~ aces of the quantities being evaluated is to normalize the ert to the true valve, as i ‘True fractional relative error = SHE SOE ime vale ‘where, os specified by Eg. (3.2), error = true value — approximation. The relative error can also be multiplied by 100 pereent to express it as true error 6 = EE 100% a3) ire value where 6; designates the tre percent relative e1tor Calculation of Errors Problem Statement, Suppose that you have the task of measuring the lengths ofa bridge ‘and a rivet and come up with 9999 and 9 em, respectively. Ifthe true values are 10,000 and 10 cm, respectively, compate (a) the true error and (6) the true pervent relative error for each case. Solution (a) ‘The eeror-for measuring the bridge is (1. (3.2)] FE, = 10,000 — 9999 = 1 em Chara-Grate-marenea Mathes or Eines, Fear tee eating. compet rea cengrer, 3.3 ERROR DEFININONS 55 ‘and for the rivet itis E,=10-9 Gb) The pevcent relative error forthe bridge is (Ey, .3)] 1 = ppl OO = 0.01% and for the rivet itis low | 009 550 Thos, although both measurements have an error of | em, the relative error for the sivet is much greater, We would conclude that we have done an adequate job of measuring the bridge, whereas our estimate for the rivet leaves something to be desired Notice that for Eqs. (3.2) and (3.3), 2 and ¢ are subscripted with a r10 signify that the error is normalized to the true Value. In Example 3.1. we were provided with this value. How- ever. in actual situations such information is rarely available, For numerical methods, the ‘ave value will be known only when we deal with functions that can be solved analytically. Such will typically be the ease when we investigate the theoretical behavior of a particular technique for simple systems, However, in real-world applivations, we will obviously not know the true answer a priori. For these situations, an alternative is to normalize the error using the best available estimate of the true value, that is, (0 the approximation itself, as in ag = AHEONMAKE EOF oa ‘approximation where the subscript u signifies that the ervor is normalized 19 an approximate value, Note also that for real-world applications, Eq. (3.2) cannot be used to calculate the error cerm for Eq. 3.4). One of the challenges of numerical methods isto determine error estirnates in the absence of knowledge regarding the ‘tue value. For example, certain muanerical methoxy tase an iterative approach to compute answers. [n such an approach, a present approxini= tion is made on the basis of a previous approximation. This process is performed! repeat= tally, or iteratively, to successively compute (we hope) bester and better approximations For such cases, the error is often estimated as the difference between previous anki current approximations. Thus, percent relative error is determined according t0 ent approximation — previous approximation, a Taurfent approximation 00% 6s) ‘This and other approaches for expressing errors will be elaborated on in subsequent chapters. ‘The signs of Eqs. (3.2) through (3.5) may be either positive or negative, Ifthe approx- ‘imation is greater thon the true value (or the previous approximation is greater than the ceurrent approximation), the error is negative; if the approximation is less than the true value, the error is positive, Also, for Eqs. (3.3) to (3.5), the denominator may be less than Chapra-Geate-rarencat | eet. Computers, oe) Matted Eines, area cengrer, Fear tee 56 APPROXIMATIONS AND ROUND-OFF ERRORS zero, which can also lead 10 negative error, Often, when performing computations, we ‘may not be concemed with the sign of the ercor, but we ate interested in whether the per- cent absolute value is lower than a prespecified percent tolerance «,. Therefore, itis often ‘useful 0 employ the absolute value of Eqs. (3.2) through (3.5), For such cases, the compu tation is repeated until leak <6. G6) IF this relationship holds. our result is assumed to be within the prespecified acceptable level ¢;. Note that for the remainder of this text, we will almost exclusively employ ab- solute values when we use telative errors. Itis also convenient to tclate these errors to the number of significant figures in the ap proximation. It can be shown (Scarborough, 1966) that ifthe following criterion is met, we ccan be assured that the result is correct to af least n significant figures. = OS x1 on EXAMPLE 3.2 Evror Estimates for levative Methods Problem Staiement. In mathematics, functions can often be represented by infinite series. For example, the exponential function ean be computed using +5 e220 ‘Thus, as more terms are added in sequence, the approximation becomes a better ancl better estimate of the true value of ¢. Equation (F3,2.1) is called 2 Maclourin series expansion, starting with the simplest version, e* = 1, add terms one at a time 1 estimate 6% Affer each new term is added, compute the true ank! approximate percent relative errors ‘with Eqs, 3.3) and G.5), respectively, Note that the true value ise = 1648721... Add terms until the absolute value of the approximate error estimate &, falls below a prespeci- fied error criterion &, conforming to three significant figures. Solution. First, Bg. (3.7) can be employed to determine the error criterion that ensures a result is correct to at least three significant figures: (05 x 10% 05% ‘Thus. we will add terms to the series until ¢, falls below this level The first estimate is simply equal to Eg, (E3,2.1) with a single term. Thus. the frst es- timate is equal to |. The second estimate is then generated by adding the second term. asin c=lte or forx = 08, Chara-Grate-marenea Mathes or Eines, Fear tee eating. compet cengrer, rea 3.A_ROUND-OFF ERRORS 587 Equation (3.5) can be used to determine an approximate estimate of the error, asin se TS 100% = 33.3% Because ¢, is not ess than the required value of swe would continue the computation by adding another term, x7/2!, and repeating tie error calculations. The process is continued until s, ¢,. The entire computation can be summarized as Terms Result orf) ea (%) T 2 2 1s a2 3 ve Lae 4 Leassee3a3 ous 5 1648437500 oni72 é Lesson” ‘901 Thus, afler six terms are included, the approximate extor falls below #, = 0.089 and the computation is terminated, However, notice tht, father than three significant figures, che result is accurate to five! This is because, for this case, both gs. (3.5) and (3.7) are con servative, That is, they ensure that the result is at least as good as they specify. Although, as discussed in Chap. 6, this is not always the case for Bg, G5), its ite most of the time. 3.4 With the preceding definitions as background, we can now proceed to the two types oF certor connected directly with numerical methods; rounchof? errors and truncation errors, ROUND-OFF ERRORS As mentioned previously, round-olf ecrors originate from the fact that computers retain aly a fixed number of significant figures during a calculation, Numbers such as x, ¢, or V7 cannot be expressed by a fixed number of significant figures. Therefore, they cannot be represented exactly by the computer. In addition, because computers use a base-2 repre sentation, they cannot precisely represent certain exact base-I0 numbers. The discrepancy introduced by this omission of significant figures i called round-off error 3.4.1 Computer Representation of Numbers ‘Numerical round-off errocs are directly reared to the manner in which numbers are stored sn a computer. The fundamencal unit whereby information is represented is called a word. This is an entity that consists of a string of binary digits, or bits. Numbecs are typically stored in one or more words. To understand how this is accomplished, we must first review some material related to number systems. Number Sysioms. A number system is merely a convention for representing quantities. Because we have 10 fingers and 1U toes, the number system that we are most familiar with ss the decimal, or base-10, number system. A base is the number used as the reference for 58 Chara-Grate-marenea Mathes or Eines, Fear tee cengrer, eating. compet rea APPROXIMATIONS AND ROUND-OFF ERRORS constructing the system. The base-10 system uses the 10 diatts—0, |, 2,3.4,5,6.7.8.9— to represent numbers. By themselves, these digits are satisfactory for counting from 0 t0 9, FFor larger quantities, combinations of these basie digits are used, with the position or place value specifying the magnitude, The right-most digit in a whole number represents 2 ‘number from 0 to 9, The second digit from the right represents a multiple of 10. The third dligic from the right represents a multiple of 100 and 0 on. For example. if we have the ‘number 86,409 then we have eight groups of 10,000, six groups of 1000. four groups of 100, zero groups of 10. and nine more units, or 48 10) + 6 x 10°) + (4 107) + @ x 10!) + (9 = 10°) = 86,409 lated in the Figure 3.30 provides a visual representation of how a number is for bzse-10 system. This type of representation is called positional noon. Because the decimal system is so familia, itis nox commonly realized that there are alternatives, For example, i human beings happened 10 have had eight fingers and eight toes, We would undoubtedly have developed an cial, or base-8, representation. Te the ne sense, our Friend the computer i like a twoefingered animal who is Himited to 10 States—either 0 or I. This relates to the faci that the primary logic units of digital con FIGURE 3.3 How the fo) decimal base 10) and the [8 binary base 2! systems work, In (6, the binary rom ber ID10 110" is equivalent io he decimal number 173. 108 107 10 10" 108 B64 08 oe 8 = 1.009 @) 1x 128-128 73 Caprconte tare | Meeting. compet, one nkcat ethde trainers or hash can, Fear teen 3.4_ROUND.OFF ERRORS 59 TPefe pet eleje[ el lel] fey) t Numbor Sign FIGURE 3.4 The repesenratan of he dectvalinager ~173 on a Ié-br compar usrg the signed srognlude meted are onfotf electronic components. Heres. numbers on the computer are represented with abinary, oF hase-2, system. Just as with the decimal system, quantities ean be repre- sented using positional notation, For example, the binary number 11 is equivalent to (1 x 2) (1 x 2) =2-+ 1s 3 im the decimal system. Figure 3.36 illustrates a more compli- cared example Integer Representation. Now thet we have reviewed how base-10 numbers can be £ep- resented in binary form, itis simple to conceive of how integers are represented on a com- pater. The most straightforward approach, called the signed maguitude method, employs the first bit of a word to indicate the sign, with a 0 for positive and a | for negative. The remaining bits are used to stote the number. or example, the integer value of ~ 173 would be stored on a 16-bit computer. as in Fig. 3.4 EXAMPLE 3.3. Runge of Iniegers Problem Stotement. on a 16-bit computer. Detenmnine the range of integers in base-10 that can be represented Solution, Of the 16 bits, the first bit holds the sign, The remaining 15 bits can hold bi- nary numbers from O to HILT HID The upper Tint can be converted 10 a decimal integer, asin DYE x 2) pee 2 x DM which equals 32.767 (note that chis expression can be simply evaluated as 2! — 1). Th 16-bit computer word can store decimal integers ranging from —32.767 to 32.767. In ace dition, because ze70 is already defined as 0000000000000. itis redundant to use the number 1000000000000000 to define a “minus zero.” Therefore. itis usually employed to represent an additional negative number: 32.768, and the range is from —32.768 to 32.767. Note that the signed magnitude method described above is not used to represent inte er on conventional computers. A preferred approach called the 2 complement technique directly incorporates the sign into the number’s magnitude rather than providing a separate bit to represent plus or minus (see Chapra and Canale 1994), However. Example 3.3 still serves to illustrate how all digital computers are limited in their capability to represent sntegers. That is, numbers above or below the range cannot be represented, A more serious Chara-Grate-marenea Mathes or Eines, Fear tee eating. compet rea cengrer, APPROXIMATIONS AND ROUND-OFF ERRORS Signed Manisa f Sign FIGURE 3.5 The manner in which o footngppoint nurber is stored in a were limitation is encountered in the storage and manipulation of fractional quantities as de scribed next FloalingPoin Represeniotion. Fractional quantities are yypieally represented in com- ppucers using floating-point form. inthis approach, the numer is expressed as 2 fr pat, called a mansissa oe significund, and an integer pant, called an exponent or character mB ‘where = the mantissa, b = the base of the number system being used, and ¢ = the expo nent, For instance, the number [56,78 could be represented as 0.15678 « 10° ina floating point base-10 system. Figure 3.5 shows one wey that floating-point number could be stored in a word. The first bit is reserved for the sign, the next series of bits for the signed exponent, and the last bits for the mantissa [Note thatthe mantissa is usually normalized if it has leading zero digits. For example, suppose the quantity 1/34 — 0,029411765 ... was stored in a floating-point base. 10 sys ter that allowed only four decimal places tobe stored, Thus, 1/34 would be stored as 0.0294 x 10" However, in the process of doing this, the inclusion of the useless zero (o the right of the decimal forces us to drop the digit 1 in the fifth decimal place. The number can be normal- ized to remove the leading zero by multiplying the mantissa by 10 and lowering the expo- nent by Ito give 0.2041 107! ‘Thus, we retain an additional significant figure when the number is stored ‘The consequence of normalization is that the absolute value af a is limited. That is, 1 vemet 38) 32 Gs where b = the base, For example, for a base-10 system, m would range between 0. and 1, ‘and for a base-2 system, between 0.5 and | Floating-point representation allows both fractions and very large numbers to be ex- pressed on the computer. However, it has some disadvantages, For example, floating-point Chapra-Geate-rarencat | eet. Computers, ethde trainers or hash can, Fearon 3.4 ROUND-OFF ERRORS 6 ‘numbers take up more room and take longer to process than integer numbers. More signif= ‘cantly, however, their use introduces a source of error because the mantissa holds only a finite number of significant figures. Thus, a round-off error is introduced, EXAMPLE 3.4 Hypothetical Set of Floating Point Numbers Problem Statement. Create a hypothetical Hoating-point number set for 2 machine that stores information using 7-bit words. Employ the frst bit for the siga of the oumbes, the next three for the sign and the magnitude of the exponent and the last three for the ‘magnitude of the mantissa (Fig. 3.6) Solution, ‘The smallest possible positive number is depicted in Fig. 3.6, The initial 0 ine dicates thatthe quantity is positive The I in the second place designates that the exponent hhas a negative sign. The 1's in the tid and fousth places give # maximum value to the exponent of Ix 241x253 ‘Therefore. the exponent will be ~3. Finally. the mantissa is specified by the 100 in the last three places, which conforms to 1x2! 40x274042%=05 Although a smaller mantissa is possible (eg. 000,001, 010.011), the value of 100s used because ofthe Himit imposed by normalization [Fq. (3.8)}. Thus, the smallest possible pose itive mumber for this system is +0.5 x 2~?, which isequal to 0.0625 in the hase-10 syste The next highest numbers are developed by increasing the mantissa a in ONO] = x28 $0 FFF Lx 3) 2 = OUTIL) 0 ONTO = (Le 2°44 2240 29) 22° = 0.093750) 6 ONIN = 2 ee PPE I) I> = 0.109875) 0 Notice thatthe base-10 equivalents are spaced evenly with an interval of 0.015625, this point to continue increasing, we must decrease the exponent to 1D, which gives avalue of 1x2! 40x29 FIGURE 3.6 The smiles possible positive flotng point number from Example 3.4 vere ofa |ifr[s [elo Magnitude Sign ot Sigh of son de sumer exponent | 428 Meagnivuce ‘of exponent 62 Chara-Grate-marenea Mathes or Eines, Fear tee cengrer, eating. compet rea APPROXIMATIONS AND ROUND-OFF ERRORS Chopping Rounding Uncierfow “Hola” 212070 FIGURE 3.7 The hypothetical numbar stem davelopad in Example 3.4. Each vole is indicated by cal sat weakd also extend in he rank. Only the postive rumbers are dren. An regalve direction The mantissa is decreased back to its smallest value of 100, Therefore, the next number is O1NO100 = 1 x 240 270 x 2%) 2? = 0.125000 ‘This still represents a gap of 0.125000 ~ 0.109375 = 0.015625, However, now when higher mumbers are generated by inereasing the mantissa, che gap is Fengthened 1 0.03125, OL101O1 = 1 x 2 0x27 4 Tx TY I? = 0.150250)1p ONO1ID = C1 x 244 x FO 2) 2°? = O.187500)19 OMG = x 244 1x 224 1x24) 2? = O2I87SO9 ‘This pattern is repeated as each larger qua reached OO = It Ex 24 Te) x P= Aho ty is formated until a ‘The final number set is depicted graphically in Fig. 3.7, Figure 3.7 manifests several aspects of floating-point representation that have signifi- cance regarding computer round-otf errors: A. There Isa Limited Range of Ouantities That May Be Represented. Just as forthe inte ser-ease, there are large positive and negative numbers that eannot be represented. Chara-Grate-marenea Mathes or Eines, Fear tee eating. compet rea cengrer, 3.A_ROUND-OFF ERRORS 63 Attempts to employ qumbers outside the acceptable range will result in what is called an overflow error. However, in addition to large quantities, the floating-point repre- sentation has the added Timitation that very small numbers cannot be represented. This is illustrated by the underflow “hole” between zero and the first positive number in Fig. 37. It should be noted that this hole is enlarged because of the normalization constraint of Fg. (3.8) 2 There Are Only a Finite Number of Quantiies Thai Cun Be Represented within the Range. Thus, the degree of precision is limited. Obviously, ieational numbers cannot be ‘represented exactly. Furthermore, rational numbers that do not exsetly match one ofthe values inthe set also cannot be represented precisely. The estors introduced by approx mating bout these cases are referted 1 as quantizing ettors. The actual approximation, is accomplished in either of wo ways: chopping or rounding. Forexample, suppose that the value of «= 3.14159265358 . .. is to he siozed on x hase-10 number system carr ing seven significant figures. One method of approximation would be w merely omit, ‘or ‘chop off,” the eighth and higher esms, as in = 3.141592, with the introduction of sn associated error a” [Eq. (3.2)] E, = 0,00000065 ‘This technique of retaining only the significant terms was originally dubbed “trun- cation” in computer jargon. We prefer to call it chopping to distinguish it from the ‘truncation errors discussed in Chap. 4. Note that for the base-2 number system in Fig. 3.7, chopping means that any quantity falling within an interval of length Ax wall be stored as the quantity at the lower end of the interval. Thus, the upper error bound. for chopping is Ax, Additionally, a bias is introduced hecause all erors are positive. “The shortcomings of chopping are attributable to the fact thatthe higher terms in the ‘complete decimal representation have no impact on the shortened version. For instance, in our example of 7, the first discarded digit is 6. Thus, the last retained digit, should be rounded up to yield 3.141593. Such rowiding reduces the error to F, = —0,00000035 ‘Consequently, rounding yields a lower absolute ervor than chopping. Note that for the base-2 number system in Fig. 3.7, counding means that any quantity falling within an interval of length Ax will be represented as the nearest allowable number. Thus. the upper error bound for rounding is Ax/2. Additionally, no bias is introduced be- ‘cause some errors are positive and some are negative. Some computers employ round- ing. However. this adds to the computational overhead, and, consequently. many machines use simple chopping. This approach is justified under the supposition that the number of significant figures is large enough that resulting rounc-off error is usually negligible. 3. The Interval berween Numbers, Ax, hucreases as the Numbers Grow in Magnitude. It is this characteristic. of course. that allows floating-point representation t© preserve significant digits. However: it also means that quantizing errors will be propostional to the magnitude of the number being represented. For normalized floating-point mum- bers, this proportionality can be expressed, for cases where chopping is employed. as, lax 69 Caprconte tare | Meeting. compet, one nkcat ethde trainers or hash can, Fearon APPROXIMATIONS AND ROUND-OFF ERRORS and, for cases where rounding is employed. as asl 8 0.8754549- 10! and chopped io give 0.8754. 10 Division is performed ina similar manner, but the mantissas are divided and the expo- ‘nents are subtracted, Then the results are normalized and chopped. Large Computations. Certain methoxs require extremely large numbers of arithmetic ‘manipulations to amive at their final results In addition, these compotations are often i terdepencent. Tha is, the later calculations sre dependent on the results of earlier ones, Consequently, even though an individual round-ofF error could ke smal, the cunulative effect over the course of& large computation can be significant. EXAMPLE 3.6 Large Numbers of interdependent Computations Problem Siolemen’. Investigate dhe effet of roundoff enor on large numbers of inter= dependent computations, Develop a program to sum a number 100,000 vines. Sunn che number Fin single precision, an 0.00001 in single an double precision, Solution. Figure 3.1Oshows a Fortran 90 program that performs the summation, Whereas the single-precision summation of I yields the expected result, the single-preision FIGURE 3.10 PROBRAM £90310 Fontan 90 fregiom w sum number 10" hmes. The cove sume fe rumor 1 singh precision ond the nusnber 10° in single and double erecicion, sun, x1, x2 PRECISION: sou, 222 TooDeT. TOTG0N o00980 9.99999999938083: ‘nt Chara-Grate-marenea Mathes or Eines, Fear tee eating. compet rea cengrer, APPROXIMATIONS AND ROUND-OFF ERRORS ‘summation of 0.00001 yields a large discrepancy. This erroris reduced (0.00001 is summed in double precision. iffeamtly when ‘Quantizing errors are the source of the discrepancies. Because the integer I can be rep- resented exaely within the computer, it cam be summed exactly. In contrast, 0.00001 can- not be represented exactly and is quantized by a value that is slightly different from its tue ‘value. Whereas this very slight discrepancy would be negligible fora small computation, it accumulates after repeated summations. The problem stil occurs in double precision but is ‘greatly mitigated because the quantizing error is much smaller [Note thatthe type of enor iTlustraied by de previous example is somewhat atypical in tha al the errors in the repeated operation are af the same sigh. In most eases the exvors of long computation alternate sign ina random fashion aud, thus, often cancel out. However there are also instances where such errors do not cancel but, in fact, Iead to a spurious final result, The following sections are imeended 1 provice insight into ways in which chis may ‘Adding 0 Large ond o Small Number. Suppose we add a small number, 0.0010, to 2 large mamber, 4000, using a hypothetical computer with the 4-disit mantissa and the L-digit exponent. We modify the smaller number so that its exponent matches the larger. 0.4000 «108 0.000001 - 10 (04000001 - 10 chopped to 0.4000: 10%, Thus, we might as well have mot performed the addition! ‘This type of error can cecur in the computation of an infinite series, The initial terms fn such seties are often relatively large in comparison with the later terms, Thus, after & few terms have been added, we are in the siwuation of adding a small quantity to a kage quantity ‘One way to mitigate this type of error is to sum the series in reverse order—that i, in ascending rather thant descending order. In this way, each new term will be of comparable agnitude to the aecumuated sum (see Prob. 3.4) Subtractive Cancellation. ‘This term refers to the round-off induced when subtracting ‘ovo nearly equal floating-point numbers, ‘One common instance where this can oceur involves finding the roots of a quadratic equation or parabola with the quadratic formula, G2) For cases whore 6" >> 4ac, the difference in the numerator ean be very small. In such eases, double precision ean mitigate the problem. In addition, an alternative formulation can be used to minimize subtractive cancellation, G3)

You might also like