CONTENTS
About the Aufhor Prefoce xiii xvii iv
Guided Tour
PanrOur Modeling, Computers, Error ond Anolysis I
l. I Motivofion I 1.2 Port Orgonizotion 2
CHAPTER I
Mothemqticol Modeling, Numericol Methods, ond Problem Solving 4 I . l A S i m p l e o t h e m o t i c Mlo d e l 5 M o 1.2 Conservotion Lowsin Engineering Science 12 ond 1.3 Numericol Methods Covered ThisBook l3 in P r o b l e m s1 7
CHAPTER 2
MATLAB Fundomeniqls 2.1 2.2 2.3 2.4 2.5 2.6 2.7
^ l l
20
TheMATIABEnvironmenl21 Assignment 2 2 Mothemoticol Operotions 27 U s eo f B u i l t l n u n c t i o n s 0 F 3 Groohics 33 OtherResources36 CoseStudy: Explorotory DotoAnolysis 37
rroblems JY
CHAPTER 3
Progromming with MATTAB 42 3.1 MFiles 43 3.2 InputOutput 47
vl
CONTENTS
d 3 . 3 S h u c t u r eP r o g r o m m i n g l 5 3.4 Nesting nd Indentotion 3 o 6 g 3 . 5 P o s s i nF u n c t i o no M  F i l e s 6 6 ts 3.6 CoseStudy: Bungee Velocity 71 Jumper Problems 75
4 CHAPTER
Roundoff qnd Truncotion Errors 79 4.1 ErrorsB0 4.2 Roundoff Errors 84 4.3 Truncotion Errors 92 ,l03 4 . 4 T o t oN u m e r i c o l r r o r l E 4 . 5 B l u n d e r s , o d e lE r r o r so n d D o t oU n c e r t o i n t y1 0 8 M , ,l09 Problems
.11 PnnrTwo Roots Optimizotion ond
2.1 Overview tll 2.2 Port Orgonizotion I 12
CHAPTER 5
Roois: Brocketing Methods I l4 i 5 . 1 R o o t sn E n g i n e e r i n g d S c i e n c e I l 5 on 5 . 2 G r o p h i c oM e t h o d s I l 6 l 5 . 3 B r o c k e t i n g e t h o d s n d I n i t i oG u e s s e s 1 1 7 M o l 5 . 4 B i s e c t i o n1 2 2 5.5 Folse osition128 P 5.6 CoseStudv: Greenhouse Gosesond Roinwoter 132 ,l35 Problems
CHAPTER 6
Roots: Open Methods  39 6 . 1 S i m p l e i x e d  P o ilne r o t i o n 1 4 0 F ti 6 . 2 N e w t o n  R o p h s o1 4 4 n 6 . 3 S e c o nM e t h o d s 1 4 9 t 6 . 4 M A T L A B u n c t i o n :z e r o r 5 1 F f 6 . 5 P o l v n o m l o l s1 5 4 : 6 . 6 C o s eS t u d yP i p eF r i c t i o n 1 5 7 Problems 162
CONTENTS
Yrt
7 CHAPTER
Optimizofion 166 7.1 Introduciion Bockground 167 ond 7 . 2 O n e  D i m e n s i o n o l O p t i m i z o t i1 7 0 on 7 . 3 M u l t i d i m e n s i o n o l O p t i m i z o t i1 7 9 on E m P 7 . 4 C o s eS t u d yE q u i l i b r i uo n d M i n i m u m o t e n t i o ln e r g y l 8 l : P r o b l e m s1 8 3
,89 PrnrTxnrr Lineor Systems
3.1 Overview 189 3.2 Port Orgonizotion l9l
CHAPTER 8
Lineqr Algebroic Equofions ond Motrices  93 8.1 MotrixAlgebroOverview 194 Algebroic Equotions MATLAB 203 with Lineor 8.2 Solving in Currents Voltoges Circuits 205 ond 8.3 CoseStudy: Problems 209
9 CHAPTER
Gouss Eliminotion 212 o s 9.1 Solving molNumbers f Equotion 213 S l E 9 . 2 N o i v eG o u s s l i m i n o t i o n2 1 8 9.3 Pivoting225 9 . 4 T r i d i o g o n o ly s t e m s2 2 7 S 9.5 CoseStudy: Model of o HeotedRod 229 Problems 233
IO CHAPTER
[U Foctorizotion 236 2 l0.l Overview f lU Foctorizotion 37 o E 2 10.2 Gouss liminotion lU Foctorizotion 38 os 2 1 0 . 3 C h o l e s kF o c t o r i z o t i o n 4 4 y 'l0.4 Left MATLAB Division 246 Problems 247
vIt!
CONIENTS
CHAPTER I I
Motrix lnverse ond Condition 249 1 i .l TheMotrix lnverse 249 I I .2 ErrorAnolysis ond System Condition 253 I 1 . 3 C o s eS t u d yI n d o o r i r P o l l u t i o n2 5 8 A : Problems 261
CHAPTER I2
Iterotive Methods 264 l2.l Lineor 264 Systems: GoussSeidel ,l2.2 N o n l i n e oS y s t e m s2 7 0 r 1 2 . 3 C o s eS t u d vC h e m i c oR e o c t i o n s 7 7 : l 2 P r o b l e m s2 7 9
PtrnrFoun Curve Fitting 281
4.1 Overview 281 4.2 Port Orgonizotion 283
CHAPTER I3
Lineor Regression 284 R 1 3 . I S t o t i s t i c se v i e w 2 8 6 1 3 . 2 L i n e o L e o s t  S q u oRe s r e s s i o n 9 2 r r eg 2 ,l3.3 L i n e o r i z o t i o n N o n l i n e oR e l o t i o n s h i p3 0 0 f s r 1 3 . 4 C o m p u t eA p p l i c o t i o n s3 0 4 r 1 3 . 5 C o s eS i u d y E n z y m e i n e t i c s 3 0 7 : K
rroDtems 5  z
^ l l
CHAPTER I4
Generol lineqr LeosfSquores qnd Nonlineqr Regression 316 1 4 .I P o l y n o m i o le g r e s s i o n 16 R 3 1 4 . 2 M u l t i p l e i n e oR e g r e s s i o n 2 O L r 3 1 4 . 3 G e n e r oL i n e oL e o sS o u o r e s 3 2 2 r l t 14.4 QR Foctorizotion the Bockslosh ond Ooerotor 325 1 4 . 5 N o n l i n e oR e g r e s s i o n 2 6 3 r 1 4 . 6 C o s eS l u d y F i t t i n g i n u s o i d s3 2 8 : S
h
rrontcms
l l
.1.1 /
CONIENIS
'I 5.'l lntroduction Interpotofion to 336 I5.2 NewronInterpoloring polynomiol 33g polynomiol j: .j tosron9etnrerpoloring 347 rJ.4 tnverse tnterpolotion350 I J.J txkopolotion ndOscillotions o 351 Problems 355
t6 cHAPTER
Splines ond piecewise Inferpofofion I 6 . I l n k o d u c t i otn S p l i n e s o 359 l o . z L i n e aS p l i n e s3 6 1 r 16.3 Quodroiic plines S 365 1 6 . 4 C u b i cS p l i n e s 3 6 8 359
374 i 6.6 Multidimensionol Interpolotion 37g l6 Z CoseStudy: HeotTronsfer 3g2 rrobtems 386
Inrerpotorion inMATLAB l9: liTewise
PnnrFvr Infegrotion Differentiotion ond 3g9
5.1 Overview 3g9 5.2 Port Orgonizotion 39O !H,\PTER 7
Numericof fnfegrofion Formutos Sg2 'l Z.J lnhoduction Bocrground ond 393 I7.2 NewtonCotes Formutos 396 17.3 TheTropezoidol Rule 39g 1 7 . 4 S i m p s o n , su l e s 4 0 5 R 17.5 HigherOrder Newfon_Cotes Formulos 4j j l7 6 lntegration Unequol with Segments 412 17.7 OpenMerhods 416 I 7 8 M u h , p l en t e g r o l s I 4j 6 Compuring Work wirh Numericol lnregrorion 4j9 ;lJ;:r";udy:
t I
t I
!
i{.
Numericof Integrotion of Functions 'l 8.I Introducfion 426 18.2 RomberInieqrotion q 427
CONTENTS
1 8 . 3 G o u s s u o d r o t u r e4 3 2 Q I 8 . 4 A d o p t i v e u o d r o t u r e4 3 9 Q I8.5 CoseStudy:RootMeonSquore Current 440 Problems 444
CHAPTER I9
Numericql Differentiqtion 19.I 19.2 I9.3 ,l9.4 ,l9.5 448 Inhoduction Bockground 449 ond HighAccurocy Differentiotion Formulos 452 R i c h o r d s o nx t r o p o l o t i o n 5 5 E 4 Derivotives Unequolly of SpocedDoto 457 Derivotives ond lntegrols Dofo with Errors 458 for 19.6 Portiol Derivotives 459 I9.2 Numericol Differentiotion MATLAB 460 with I 9 . 8 C o s eS t u d yV i s u o l i z i nF i e l d s 4 6 5 : g Problems 467
Pnnr5x
Ordinory Differentiol Equotions 473
6.1 Overview 473 6.2 Porl Orgonizofion 477
CHAPTER 20
InitiolVolue Problems 479 20.I Overview 481 20.2 EuleisMethod 481 20.3 lmprovemenls Euler's of Method 487 20.4 RungeKutfo Methods 493 20.5 Systems Equotions 498 of 20.6 CoseStudy:PredotoryPrey Modelsond Choos 50A Problems 509
CHAPTER 2I
Adopfive Merhods ond Stiff Systems 514 21 .'l AdoptiveRungeKutto Methods 514 2l .2 Multistep Methods 521 2l .3 Stiffness525 2 l . 4 M A T L A B p p l i c o t i o n : u n g e J u m p ew i t hC o r d 5 3 1 A B e r 2l .5 CoseStudy: Pliny's lntermittent Fountoin 532 r r o D l e m s3 J /
CONTENTS
xl
22 CHAPTER
BoundoryVolue Problems 540 22.1 lntrodvction Bockground 541 ond Method 545 22.2 lhe Shooting 22.3 FiniteDifference Methods 552 P r o b l e m s5 5 9
APPENDIX EIGENVALUES A: 565 FUNCTIONS 576 APPENDIX MATLAB B: BUILTIN APPENDIX MATIAB MFltE FUNCTIONS 578 €: BIBLIOGRAPHY579 rNDEX 580
Modqling,CoTpute''i ,
qnd Erior Anolysis
t.t
MoTtvATtoN
What are numericalmethodsand why shouldyou stridythem? problemsare formulatedso are by Numericalmethods techniques which mathematical Because digital computers that they can be solved with arithmeticand logical operations. numericalmethodsare sometimes referredto as comexcel at perform.ing suchoperations. puter mathematics. se.In the precomputerera, the time and drudgeryof implementingsuchcalculations riously limited their practical use. However, with the advent of fast, inexpensivedigttul computers,the role of numerical methodsin engineeringand scientific problem solving has exploded.Becausethey figure so prominently in,:' much of our work, I believe that numerical methods should be a part of every engineer'sand scientist's basic education.Just as we a.ll must have solid founand science, dationsin the other areasof mathematics of we should also have a fundamentalunderstanding numerical methods.In particular,we should have a solid appreciation both their capabilitiesand their of limitations. Beyond contributing to your overall education. .thog T9 several additibnat reasons why you shoutO .",,,,rr,,,r,,,r,,,,,, study numerical methods: 1. Numerical methods greatly expqld the types of , problems you can address. They are capableof nonlinearihandlinglarge systems equations. of , , d.l, and complicated geometriesthat are not uncommon in engineeringand scienceand that are often impossible solve analyticallywith stanto your dard calculus. such"they greatlyenhance As problemsolving skills. 2. Numorical methods allow you to use "canned" software with insight. During your career,you will q*"
AND ANALYSIS I COMPUTERS, ERROR PART MODELING,
prepackaged computerproto available invariablyhave occasion use commercially grarnsthat involve numericalmethods. The intelligentuseof theseprogramsis greatly of enhanced an understanding the basic theory underlyingthe methods.In the abby you will be left to treat suchpackages "black boxes" as senceof suchunderstanding, with little critical insightinto their inner workingsor the validity of the resultsthey produce. Many problemscannotbe approached using cannedprograms.If you are conversant with numerical methods,and are adept at computer programming,you can design expensive your own programs solveproblems withouthavingto buy or commission to software. Because nuNr.rrnerical methodsare an efficient vehiclefbr learningto usecomputers. they areidealtbr mericalmethods expressly ale designed computerimplementation, for implement powers and limitations.When you successfully illustratingthe conrputer's numericalmethodson a computer,and then apply them to solve otherwiseintractable problenrs, you will be plovided with a dramaticdernonstration how computerscan of At serveyour professional development. the sarne lime, you rvilI alsolearnto acknowledge and control the errors of approximationthat are part and parcel of largescale numericalcalculations. Numericalmethodsprovidea vehiclefbr you to reinforceyour understanding mathof ernatics. Because one tunction of numericalmethodsis to reducehigher mathematics to basic arithmetic operations.they get at the "nuts and bolts" of some otherwise and insight can result from this alternative obscuretopics. Enhancedunderstanding perspective.
3.
4.
5.
how numerical With thesereasons motivation.we can now set out to understand lls work in tandemto generate reliablesolutionsto mathematmethodsand digital computers ical problems.The remainderof this book is devotedto this task.
1.2 PART ORGANIZATION
This book is divided into six parts.The latter five partsfocus on the major areasof numerAlthough it might be temptingto jump right into this material,Part One conical methods. of four chapters dealng with essential backgroundmaterial. sists 1 providesa concreteexampleof how a numericalmethodcan be employed Chapter to solve a real problem. To do this, we develop tt muthematicalmodel of a fieefalling jumper. The model, which is basedon Newton's secondlaw, resultsin an ordinary bungee differentialequation.After first using calculusto developa closedformsolution,we then with a simple numerical method.We show how a comparablesolution can be generated that we cover in an overview of the major areers numericalrnethods of end the chapterwith Parts Two throughSir. Chapters2 and 3 provide an introduction to the MATLAB' software environment. way of operatingMATLAB by enteringcommandsone Chapter2 dealswith the standard modeprovidesa straightforward t'alculatornuttle.Thisinteractive at a time in the socalled meansto orient you kl the enviroumentand illustrateshow it is usedibr common opera
I.2 PART ORGANIZATION
Chapterl shows how MATLAB's programming mode providesa vehicle for assemThus,our intentis to illustrate how MATLAB into algorithms. bling individualcommands a convenientprogrammingenvironmentto developyour own software. servesas ChapterI dealswith the irnportanttopic of error analysis,which must be understood for the effective use of numerical methods.The first part of the chapterfocuseson the roundoJferrors thar result becausedigital computerscannot representsome quantities truncationerrctrsthat arisefiom using an approximation exactly.The latter part addresses procedure. in place of an exactmathematical
lrfi.if i .. l. Numericol Methods. This inlbrmationwill be usedaspart of a largeranalysisto determine the length and requiredstrengthof the bungeecord for jumpers of differentmass You know from your studies ofphysics that the acceleration shouldbe equalto the ratio of the tbrceto the mass(Newton'ssecond law).. irnd Learning aboutthe difterent typesof numericalmethodswe will cover in this book. ond Problem Solving I 4 CHAPTER OBJECTIVES The prirnary objectiveof this chapteris to provide you with a concreteidea of what numericalmethodsare and how they relateto engineering anclscientificproblem solving. Specificobjectivesand topicscoveredare o r o Learning how mathematical modelscan be formulatedon the basisof scientific principles simulate behavior a simplephysicalsystem. You're given the task of predicting the velocity of a jumper (Fig. U1 I Mothemoticol Modeling.1:i ffilT** TFi + i. r 1 FIG For f^^ l I cc ium r YOU'VE GOT A PROBTEM upposethat a bungeejumping companyhires you.{. to the of Understanding how numericalmethodsirlford a meansto generate solutionsin a rnannerthat can be irnplemented a digital computer.t " i'i.. Based this insightandyour knowledge on . on Understanding different typesof conservation the laws that lie beneath models the usedin the variousengineering disciplinesand appreciating diff'erence the betweensteadystate dynamic solutionsof thesemodels.r I r.l ) as a function of time during the freefall part of thejump.
rodels figure prominentlyin the way engineers and scientists use numericalmethodsin their work. However. our more generalobjective will be to illustrate (a) what numericalmethodsare and (b) how they figure in engineering and scientificproblen solving. dt''m Upwardforce d u et o a i r resistance t til Downward forcedue to gravrty til v where rr : vertical velocity (n/s).In a very general sense. can be represented a functionalrelationshipof the fonn it as D e o e n d e n ( .I A SIMPTE MATHEMATICAL MODEL of fluid mechanics.1) can range from a sirnple algebraic relationshipto largecomplicated setsof differential equations. g : the accelerationdue to gravity (:9.c t l o n s v ' n .e n c l e n t forcine \ 'n.we will illustrate an alternativesolution approach. model.of the secondlaw is the wellknown equation or F:ma (t. ol . Newton formulatedhis secondlaw of motion.in the following pages.a .on the basisof For his observations.l A SIMPTE MATHEMATICAT MODET A motlrcnntical ntodelcan be broadly definedas a tbrmulation or equationthat expresses features a physicalsystemor process mathematical the essential of in terms.2) whereF is the net force actingon the body (N./ i n d e . . m is the n. ca: a secondorder drag coetficient (kg/m). which states that the time rateof changeof momentumof a body is equalto the resultant force actingon it.I .independettt the variablesare usually dimensions.f (l. I.functiotts externalintluencesacting or are upon it. example. .l Forces ociing o on lreeJolling bungee iumpet. .In so doing. Because this is a ditlerentialequation.rass the object(kg).and thelbrring.l) where the de. .81nls21. .tendent variable is a characteristic that usually reflectsthe behavioror state of the system:. you developthe following mathematical model for the rate of change r h o l ' v e l o c i t y .rputerorientednumericalor approximate solution. we will also show how mathematical n. or kg nls"). This will involve developinga con. _e : J [ v a n a D t e s. un \ . of (r a n dc i i s i t s a c c e l e r a t i o nn / s : ) . p u r a n ) e t el r s . r ' i t r e s p e ctto t i m e . r : time (s).you know that calculusmight be usedto obtain an analyticalor exactsolutionfor u as a function of /.a . ducd. and m: the jumper's mass(kg). along which the system's behavioris beingdetermined. flGURE l. (1. The ntathematicalexpression. The actual mathematicalexpressionof Eq. parametersareretlectiveof the the system'sproperlies composition. such as time and space. Aside from showing you how the computercan be usedto solve this particularproblem.
F is the forcing funcwhere a is the dependent variable tion.For example.l) by merely dividing both sides by m to give F n1 (1. we will expressthe net force in terms of measurable For a body talling witlrin the vicinity of the earth. (1. .the object will accelerate. it yields reproducible poses. to the net force acting on the body normalizedto its mass. objectwill decelerate. .1): F: Fol Fti (1 .1.3)to yield r.3)can be usedto compLlte acceleration. resultsand. Ifit the object'svelocity will remain at a constantlevel.sed formulatethe force due to pravity as to Fo:m8 whereg is the acceleration to gravity(9.81m/s2). ( 1. may be much more complex. the solution of Eq. Eq. is negative.4) Thus.other mathernatical techniques mathematical and either cannotbe solvedexactly or requiremore sophisticated than simple algebrafor their solution. we because are not yet predictinghow acceleration models Equation( 1.'elocity it duF dt nr (1.31 variablereflecting the system'sbehavior. Note that for this simple casethereis no independent variesin time or space. METHODS.3)has a numberof characteristics aretypical of mathematical that of the physicalworld. It describes naturalprocessor systemin mathematical It represents idealization an and sirnplification reality.6) . modelsof physicalphenomena However. 5 ) If force in the downward direction is assigned positive sign. nearthe earth'ssurface.2) was obtainedeasily. the model ignoresnegof manif'estations. the secondlaw can be a u. be usedfbr predictivepurcan Finally.if the force on an object and its massare known.That is. the rate of changeof the velocity is equal where u is velocity (in metersper second). SOLVING MATHEMATICAL NUMERICAL AND PROBLEM The secondlaw can be recastin the format of Eq. ligible detailsof the naturalprocessand focuseson its essential minimal importance the secondlaw doesnot include the effects of relativity that are of when appliedto objectsand forcesthat interacton or aboutthe earth'ssurfaceat velocities and on scales visible to humans.consequently.For this case. 1. a terms. Thus. Newton's secondlaw can be usedto determinethe terminal velocity of a freefallingbody jumper (Fig. .If the net force is positive.the Ifthe net force is zero. (1.1). ( 1. Next. due (1.Our falling body will be a bungee a model can be derived by expressing the acceleration the time rate of changeof the as (tluldr) and substituting into Eq. Becauseof its simple algebraicform.To illustrate a more complex model of this kind.6 MODELING. variablesand parameters.the net force is composed two opposof ing forces: the downward pull of gravity Fp and the upward force of air resistanceFy (Fig. and nr is a parameter.
1kg leaps ProblemStotement. thejumper is initially at rest(r.the exactsolutionof Eq.25kg/m. and .1 1 . the greaterthe uprvardfbrce due to air resistance.in contrast in to the solutionof Newton'ssecond law in Eq..9)is castin the general fbrm of Eq.l cdr)' (1. . g is theforcing function. affect air that resistance.e' e .ivariety of ways. o t t .rrd force. 0 at / : 0).1is a proporticlnalitv constantcalled the drag coefticient(kg/m).I A n o l y t i c o lS o l u t i o nt o t h e B u n g e e u m p e rP r o b l e m J jumper with a massof 68. The parameterc.5 ctt t dt tn (1.8. For example. the greater the fall velocity. ( 1. ( E q s .7 tationusedby thejumper duringfree tall. of I MATLABG'allows d i r e c rc a l c u l a t i o n f t h e h y p c r b o l i c a n g e n v i a t h c b u i l t . more adt'anced techniques suchas thoseof calculusnrustbe appliedto obtainan exactor analytical solution.3).1)wheret. Knowledge from the scienceof can fluid ntechanics suggests a gtrodfirst approxirrration that wouliJbe to assume that it is proportional to the squareof the velocitl. 4 1h r o u g h 1 .8)for the velocity of the jumper cannot be obtirinedusing simple algebraicmanipulation.7) where r'. A bungee liom a stationary hot air balloon.Use a dragcoefticient 0.8)for :r.* e \ tanht: (1.However.10) Note that Eq. (1.8) (1./ accountslbr properties ofthe ialling object.t is the independent variable ctt andm areparameters.I A SIMPLE MATHEMATICAL MODEI.9) where tanh is the hyperbolic tangent that can be either computed directlyr or via the more elementary exponential function as in e' .I . ( 1. ( 1.(/) is the dependent variable. the present For c&s€. f U: . .Use Eq. EXAMPLE I . The rlet fbrce is the differencebetweenthe downward and upwi.8) is a ntodelthat relates accelerirtion a falling objectto the tbrces Ecluation the of actingon it. ( 1. the jumprraster havinga particuiarly is bad dayl). might be a function of the type of clothing or the orienc. It is a tli/Jerential equtttiort because is written in terntsof the differentialrate it of change(d uI dt 1 of the variablethat we are interested predicting.. ( 1.i nf u n c t i o nr d n h ( r ) .suchas shape surface or roughness. 7 Air resistance be fbrmulatedin i. Also deterrnine the terminal velocity that will be attainedfbr an infinitely long corcl(or alternatively.ff*"n( u(/) . Thus. Therefbre. 7 )c a nb e c o m b i n e do y i e l d t t dt: .Rather. calculus if : can be usedto solveEq.) yilt / (1.9)to compute velocityfbr the first l2 s of fiee fall.
mfs 0 2 4 6 B t0 12 o I B 72. Inserting parameters Eq. .2). 1. 3 u.4214m/s (about 110mi/h) is attainedafter 10 s.MATHEMATICAL MODELING.92 33I I tB 42 4762 46 9575 4 S4 2 t4 50 6175 516938 According to the model. (1. the jumper acceleratesrapidly (Fig. NUMERICAL METHODS AND PROBTEM SOLVING Solution.9)yields the into which can be usedto compute t. A velocity of 49. Note also that after a sufficiently lons FIGURE I.2 The onolyticol solution thebungee for problem compuredExomple1 Velociry os in I iumper increoses with time ond osympfoiicolly opproocheso terminol velociiy.
6983m/s (115. has ancewith the air resistance.6mi/h) is reached. the problerr is refbrmulatedso it Nttntericalntethods thosein which the mathemirtical are This can be illustratedfor Eq. I I ) represents reverse the FIGURE 3 I.Au . 1.Remember dtt  dt lim A1+l) Au N ( process. the net force is zero and acceleration ceased.ti . eventually. Note that du ldt = Lu I Lt is approximate from calculus Ar that because is flnite.I A SIMPLE MATHEMATICAL MODEL 9 time. a constantvelocity. tlrereare mirny matlrematical that cannotbe solved exactly.8) by realizingthat can be solvedby arithmeticoperations.: Lt dt u ( / i + r.Unfortunately.*. solution because exactly satisit Equation( I .the force of gravity will be in balThus. and u(ria.lr) whereAu and At aredifferences velocityand time computed in u(r1) overflnite intervals.u ( t i ) ) ti+t . is velocity at an initial time ri.ti (r. ( 1.: . of by the time tateof change velocitycan be approximated (Fig.) is velocityat somelater time f11.] . io The of o finite the derlvotiveu wifhrespect / of use differenceopproximote firsf to AU Approximateslope u(r.3): dtt . This velocity is constantbecause. Equation l.) Lt li+t .called the terminol velocitt'.) Ar_u(1.the only alternativeis to develop a numericalsolutionthat approximates exact solution.In many of thesecases.9) is called an anabtical or closedform nrodels fies the oliginal diffbrentialequation.of 51.
Thus. NUMERICAL MEIHODS.to MATHEMATICAL MODELING. Eq. Using this infbrmation and the parameter values from ExampleI . the velocity of the jumper is zero.13)can be used to corxpute velocityat 11. 6 2 t .the colnplrtationis repeated. That is.l tr+t * t.1I ) is callecl JinitediJJeren.H.o. with the result : + l ( ) .11canin tum be employed to extendthe cornputation velocityat l. 8 1. This equationcan then be rearrangecl yielcl to u(/i+r: u(/i)1_ .4117m/s .2s . We'll discussit in more detail when we turn to diff'erential equations later in this book.rl x 2 : te. ( 1. velocityattinle /i and At : ti+t . Employ a stepsizeof 2 s fbr the calculation. P e r f o r m h es a m e o r n p L l t a t i o ni n E x a m p l e1 .AND PROBLEM SOLVING Equation( 1. 6 2 1 9 .9lrt.t L l 0.ion be rewritten as ui+:r+'!u r {1t (t.8)1.J _ _ t .. ( J .l 68.13) wherethe nomenclature clesignates u. :g .ff(/.1 r " l:16.r' .+r . Thus at any time along the way. (/{) Solution. . a the or tEq. you caneasilycomputevelocityat a latertime f 11. t / 7 ) )tn.2 1 ' N u m e r i c o lS o l u i i o nt o t h e B u n g e e u m p e rP r o b l e m J P r o b l e m t o i e m e n t .12 and so on.ce a opprcrirnationof the derivativeJt Iirnc /. can the equat. We cannow seethat thedifferentialequation beentransformed has into an equation that canbe usedto determine velocityalgebraically ri+l usingthe slopeandprevious the at valuesof u andt. I .: o* fr.r 2 ) Notice that the tenn in brackets the righthandside of the diff'erential is equationitself (1.1)u(t.8)to give into r'(1. (1. It can be substituted Eq. EXAMPLE .2 s: r.r L I For the next interval lfiom r : 2 b 4 sJ.ti. it provides meansto compute rateof change slopeof u. At the startof the computation :0). This new valueof velocityat l.. to New valne: old vahle* slopex stepsize This approachis tbrnrally calledEuler's metlnd.. 1 b u tu s eE q . 1 3 ) S t c as to colnpute velocitywith Euler'smethod. If you aregivenan initial valuefor velocityat sometime l./i) ) [r ( r . ( 1 9 .62rls 68.
applyingEq. you can accurately model the velocity of thejumper without having to solve the differentialequationexactly.We can seethat the numerical methodcaptures essential the featuresof the exact solution.However.4 I for problem Compcrison thenumericol onclyticol of ond solutions thebungee iumper The calculationis continuedin a similar fashionto obtain additionalvalues: ttS u.I . . as the straighrline segments track closerto the true solution.4 along with the exact solution.because we have employed straighfline segmentsto approximatea continuouslycurving function.4137 46 2983 50 r802 5t 3123 5 r 6008 5r 6938 r0 l2 oo 0 2 4 6 B The resultsare plotted in Fig.1 A SIMPLE MATHEMATICAL MODEL tt Terminalvelocity FIGURE.the effort associated with using smaller and smaller step sizeswould make such numerical solutionsimpractical. with the aid of the computer.One way to minimize such discrepanciesis to usea smallerstepsize.Using hand calculations. m/s 0 r9.6200 36.13)at 1sintervals results in a smaller error.large numbers of calculations can be performedeasily.Thus. (1.Forexample. there is some discrepancy betweenthe two results.However. 1.
.Eq.15)might appeartrivially simple. They all boil down to Change: increases decreases (1. This case.2 CONSERVATION Aside from Newton's secondlaw. at steadystate. Such tradeoffsfigure prominently in numerical methodsand constitutean importantthemeof this book.the increases decreases and nrustbe in balance. to predict changes with respect to time.which is also given a specialnarnethe stea(lystate in calculationhas many applications engiFor example.fbr steadystate incompressible neeringand science. Aside from predictingchanges. there are other major organizingprinciples in science and engineering. I .the flow into a junction musl be balanced flow going out. fluid flow in pipes.14)embodies ways in which one of the most fundarnental conservation laws are used in engineering and sciencethat is.8)1. the great conservationlaws of scienceand engineeringare conceptuallyeasy to understand. such.5. L/AWS ENGINEERING lN AND SCIENCE 1. they embodythe two fundamentalways that conservation laws areemployedin engineering As and science.8) with du ldt : 0l l1l$ : f 6411 (1.Each halving of the stepsize to attainmorc accuracyleadsto a doubling of the nurnber of computations.they will tbnn an importantpart of our etforts subsequent in chapters illustratethe connection to betweennumericalmethodsand engineering and science. the steadystate For the bungee condition would correspond the casewhere to the net lbrce was zeroor [Eq. Among the most importantof theseare the conserv. balance the bungee for Although simple.14)and (1. anotherway in which conservation laws are appliedis fbr cases wherechange nonexistent. (I. change zero.Eq. (1.r6) Thus. ( 1.r2 MATHEMATICAL MODELINGNUMERICAL METHODS. Thus. We will give it a special namethe timevariable (or transient) computation. ifno changeoccurs.ation lan:s. as in by Flow in : flow out For the junction in Fig.14) becomes is If is Change: 0 : increases decreases or : Increases decreases (1. 6) can be the solvedfor the terminal velocity AlthoughEqs.r5) Thns.2.r4) This is preciselythe fbrmat that we empioyedwhen using Newton'slaw to developa force jumper tEq. downwardand upwardfbrcesarein balanceand Eq. ( 1.a cornputational numerical result.Although they form the basis for a variety of complicatedand powerful mathematical models. we seethat thereis a tradeoffbetweenaccuracyand computational effort. jumper.(1. the balancecan be used to computethat the flow out of the fourth pipe must be 60. AND PROBLEM SOLVING price nrustbe paid for a nroreaccurate As in Example 1. (1.
Civil and mechanicalengineers often focus on models developedfrom the conservation of momentum. will endeavor includeexamples We from these to areas the coming pages. Finally. which resultsfrom the conservation charge. 1.The energybalance specifies that the clranges voltagearoundany loop of the circuit must add up to zero.Forcivil engineering. Sirnilarly. in petroleum.The sameprinciplesare employedfor the mechanical engineeringcasestudiesto analyzethe transientupanddownmotion or vibrations of an automobile.It specifiesthat the of changeof mass of a chemical in the reactordependson the amount of mass flowing in minus the n. in I.1.aerospace engineering much in cornmonwith mechanihas cal engineering.6 summarizes major areas the coveredin this text. I summarizes somemodels andassociated conservation lawsthatfigurepromrnently in engineering. of We shouldnotethattherearemanyotherbranches engineering of beyondchemical.The currentbalance..electrical enrploy engineering studies both currentandenergybalances model to electriccircuits.Justasflow mnstbalance thejunction at of pipes.andmechanical.5 I n A f o w b o o n c ef o rs t e o d yn c o m p r e s s ifb e df l o t v t t h ej u n c l i oo f p i p e s i ui o Tablel. force balances utilized to analyzestructures are suchas the simple trussin Table 1.Many chemical engineeringproblems involve mass balancesfor reactors. electrical. . simiof is lar in spirit to the flow balance depictedin Fig.5.3 NUMERICAT METHODS COVERED THISBOOK IN We choseEuler's methodfor this introductorychapterbecause is typical of many other it classes numerical of methods. The massbalanceis derived from the conservation mass.rass flowing out. chemical engineering skills are usedextensively areassuchas environmental. In most consistof recasting mathematical operations into the simple kind of algebraicand logical operations compatiblewith digital compllters.For exalnple. civi.I. essence. Many of theseale relatedto the Big Four.electriccurrentmust balanceat thejunction of electricwires.and biornedicalengineering.3 NUMERICAL METHODS COVERED THISBOOK IN r3 P i p e2 F l o wi n = 8 0 Pipe 1 F l o wi n : 1 0 0 Pipe 4 Flow out = P i p e3 F l o wo u t : 1 2 0 FIGURE.Figure 1.
\ I Conservation of momentum Forcebalance: +Fv Fn * + I O+ +FH ffi ?7fu.. V at/ I At each node forces (FH)= o I horizontal I vertical forces (I'u) : 0 Mechanical engineering Conservation of momentum Forcebalance: I Upward force lr=0 I m Li I V Downward force = downward force .l MATHEMATICAT MODELING.2.1m77.r * zY RJ i:R: f L\A7\J Around each loop I emf's .I voltage drops for resistors >6>a:0 . low Device OrganizingPrinciple Conservation of mass MathematicalExpression Mass balance: ffi Field Chemical engineering inort ff_* Over a unit of time period Amass:inputsoutputs ourpur Civil i e n g in e e r n g t structyz .l4 TABTEl. AND PROBLEM SOLVING Devices ond typesof bolonces ore commonly fhot usedin ihe four moiororeosof engineering. \ . For eochcose. NUMERICAT METHODS.lhe conservotion on whichthe bolonceis bosedis specified. . b For each node I current (i) = 0 I'R' Conservation of energy Voltage balance: a{A&l ..upward force Electrical engineering ilH3 Conservation of charge C u r r e n t a l a n c e :+ i .
solve for the.yJAr FIGURE I. = b2 f\x\ Optima lcl Part 4: Curve fitting o ldl Part 5: Integration and differentiation Integration: Find the area under the curve Find the slooe of the curve Differentiation: lel Part 6: Differential equations Given dv Av : . ab .I.x.h N:flt'Yl solve for r as a function of r .r's a ' .6 Summorythenumericol of methods covered this in book. = arrx. 1a. r xt a r 2 x .Ii+r= ]'i+ "f(ti.. .r so thatfir) = 0 Optimization: Solvefor x so that/'(r) = 0 f(xl lbl Part 3: Linear algebraic equations Giventhe a's and the b's.3 NUMERICAL METHODS IN COVERED THISBOOK r5 lal Part 2: Roots and optimization Roots: Solve for.
Although somewhat different approaches used. 1. for optimizarion involvesdetermininga valueor valuesof an independent variablethat correspond a to "best" or optirnalvalue of a function.A plrysical interpretation ruurrcricctl of iltegratiott is tlre determinationof the area under a curve. you know fiom your study of calculus. linear algebraicequations Part Three is devotedto solving systemsof simultaneous (Fig. Part Five also covers methodsfor nume.6d. Part Five is devoted to integlation and differentiation.root location are and optimizationboth typically arisein designcontexts.and fluid networks. Integrationhas many applications engineering in and science.Experimental resultsareoften of this of is kind. NUMERICAT METHODS.this involves the deAs terminationof a function's slopeor its rate of change.6e). in contrastto satistying a single equatiou.rical difr'erentiation. equations focuses the solutionof ordirro'vdi.Part Si.However. optimizationinvolvesidentifying maximir and minima. to problems. AND PROBLEM SOLVING Part Two dealswith two relatedtopics:root finding and optimization. For thesesituations. in that they are Such systerns similar in spirit to l'ootsof equations the sense are concemedwith values that satisfy equations. Such is usually the casefor tabulatedinformation.x on This is bein of and science.root locotiorrinvolvessearching the zerosof a function. trend of the datawithout necessarily valIn contrast. electriccircuits. rangingfrom the determinationof the centroidsof oddly shapedobjectsto the calculationof total quantities In integrationformulasplay an basedon setsof discretemeasurements. As is a significantdegree error associirted with the data.they are also encountered other areasof numericalmethodssuchas curve titin e l i n g l r n dd i f f e r e n t i a l q u u t i o n s .t6 MATHEMATICAL MODELING. 1.fterential Finally.As depictedin Fig.6a. However.rse is the curve to predict the intermediate values.The As an engineer scientist. as in Fig.they originate in the mathenratical In modeling of Jarge systems interconnected of elements suchas structures.Thus. 1.6a. 1. 1.nurnerical importtrntrole in the solution of diffbrentialequations.6c'1. Examplesrangefrom populationforecasting (rateofchangeof population) tlreacceleration a talline of body (rateofchangeofvelocity).6&). (Fig. 1.The strategyin suchcases to flt a curve directly throughthe datapoints and r. As depicted in Fig. Suchequations arisein a varietyof problemcontextsand in all disciplinesof engineeriugand science. described Part Four tFig. you will often haveoccasion fit curvesto datapoints. or to categories: regression techniques developed this pulposecanbe dividedinto two general for in regression ernployed is wherethere and interpolation. set of valuesis soughtthat simultaneously satisfies set of linear algebraic a a equations.interpolutionis usedwherethe objectiveis to determineintermediate ues betweenrelativelyerrorfreedata points. particular.In contrast. strategy to derivea singlecurve that represents general the the matchingany individualpoints. Such equations of greatsignificance all areirs engineering are of in cause many physicallaws arecouched termsof the rateof change a quantityratherthan rnodels the magnitudeof the quantity itself. initialvalue Two typesof problemsareaddressed: andboundaryvalue . addition.
.3 3 1.811.7 Thc amountof a uniformly distributedradioactivecontaminant containedin a closed reactor is measuredby its concentration (becquerel/liter Bq/L).(outflow) F I G U RP I . 9/1. asan sume the chuteis instantaneously that deployedso that thc jumps to 50 kg/s.9) is a solutionof to Eq(1.Perfbrm thc calculation from / : 0 to 20 s with a step of I s. Can you make any statement regarding crrorsof thc calculationbasedon the the results? 1.26 3 7 86 1 r 0 68 0 3 5 06 r 2t6B0 7/l Bit t 2 7.a rrass balancefbr the reactorcan be wntten as dc kt / decrease \ Ul decaS \ / 5ir 2 2 41 3 6/l 327. at Equation(1. this and ls a steadystate a transientcomputation? or 1. ln c versus/) and detenninethe slopc. If a secondjurnper a drag coefficient of has of 15kg/sand a massof 75 kg.Intcrpretyour results.i" tmta. drag cocllicient / c h a n e ei n \  .e. at (b) Plot the solution on a semilog graph (i. (1.2The following infbrmation is availablc for a bank acc0unt: Dote Deposits Withdrowols Bolonce I 5 I 2 .PROBLEMS t7 PROBTEMS l.4Rather thanthe nonlinear rclationship Eq. 2 d r . Use an initial conditionthat the parachutist size has upwardvclocity of 20 m/s at /: 0. .14). o The concentration /:0 is l0 Bq/L.4). a1 that is Decay rate : tc whereft is a constantwith units of day I. 1.obtain the closedfbnn solution fbr thc where case thejurnperis initiallyat rcst (u : 0 at 1: 0). The contaminant c or decrcases a decay rate proportionalto its concentration.l Usecalculus velity that Eq.14) can be written fbr this systcrnas d( . witha stepsize of (a) I and (b) 0. 8 E .2.7). parachutist the usrng Errler's methodfbr thc cascwhcre rr : 80 kg and c' : l0 kg/s. how long will it take her to reach samevelocityjumpcr I rcachedin l0 s'l the 1.4). Pl.5For the freetalling bungee jumper with linear drag (Prob. ( 1.+Av'\ AT : 3 0 s i n ' ( r ) O : (kg/s). 3 1 9ll . l ll A storage tank (Fig.you of mightchooseto model the upward fbrce on the bungee j u m p ca sa l i n e a r e l a t i o n r h i p : r rl f tt : C U (a) Use Euler's mcthod to solve this equationfrom t : 0 to I d w i t hk : 0 . Showeachstcpin thc computation. (1.5kg/s fbr c'. (b) Repeat numericalcalculation Example 1.8) contains liquid at depth) a where. the of the on 111. I d .8). compule velocityof a freetalling 1.' l: \ votume / tinflowt . The contentsare resupplied a sinusoidalrate3Q sin2(t).r': 0 when the tank is half full.6 For the fieefalling bungce jumper with linear drag (Prob.assume first jumper is 70 kg and hasa drag coI a efficient l2 kg/s. E m p l o y a s t e p s i z e f A r : 0 . wherer'' a firstorderdrag coefTicient (a) Usingcalculus..2 with the in the sameinitial condition and oarametervalues. r ( nr < dt / changc\ t . Thercfore. At r: l0 s. Liquid is withdrawn at a constantflow rate Q to meetdemands. of 1.according to Eq.rls : \ n / Use conservation cashto compute balance 6/ l.Use a value 12.5 s.Computethc velocityto t: l2 s.3Repeat Example1.
maintainsteady how much water must be drunk per day? state. Assume air is an ideal gas at 20 "C and 101.1 1. and the body metabolicallyproduces0. AND PROBLEM SOTVING NUMERICAL by nr the heat capacity.7 18 kJ/(kg K).12 FigureP1.rr) o : . To feces. suppose that the outflow is not constantbut rather dependson the depth.075 mr and gives out about 80 W of heat (l W = I J/s).8 m/s2. the exchange 0.97 kg/krnol).One liter is ingested as food.5 d.and 0.ll A group of 30 students lneasuresl0 m by 8 m by 3 m..5 d.Mwt V is the molecularweight of the gas (1br air 28.Although this is a decentapproxinration when we are examining falling objectsnear the surtaceof the earth.ou.325kPa. = . Assume the heat capacityC. The parameter 1200m2 and p : 500 m3/d. 1 . 1.O : :3: _ sinft) : drAA Use Euler's method to solve for the depth _vfronrr : 0 to valuesareA : I0 d with a step size of 0.12depictsthe variousways in which an averilgeman gainsandloseswaterin oneday. the gravitational lbrce decreasesas we lrove based on above sea level.10 The volume flow rate through a pipe is given by Q : rA. The parameter of that the ini1200m2.4 literswhile exhalingover a onedayperiod.Ir': c. and rRis the ideal gas constant[8.Assumethat the initial condition i s .9 For the same storagetank describedin Prob. respectively.O:500 mr/d.IO P Metabolism F I G U RP I .. and 0. I 2 E .dr : nrc. Use volumecontinuity solvefor the required areain pipe3 of Fig.(trA Use Euler's method to solve for the depth )' fiom t : 0 to valuesareA : 10 d with a step size 0. tbr air is 0. asdue to gravity was a constant sumed that the acceleration value of 9.The body will also lose 0.8. Each studenttakesup about 0. attenda classin a room which l. In is breathingair. 1. y: 0 .05 liters while inhaling.and throughthe skin. P I . 1. r ) ' Skin Qr. 1.2.3 liters.2.0.31.the differential equationfbr depth can be written as dl' dt . sincethe surfaceareaA is constant r/t'O. kPa m]/(kmol K)]. METHODS..(rz The massof air can be obtainedliom the ideal sas larv: P7 Mwt nt Plt : a(ltr')r O .13 In our example of the fieefalling parachutist. is lhe volume of the gas. s i n .in= Qz'rt: 20 m3/s Food Drink u.urine. Assurne tial condition is _r: 0. = 6 m/s A:=? FIGURE I. rise during the first l5 minutes Calculatethe air temperature of the class if the room is completely sealedand insulated. Note that by the heatabsorbed the air O is relatedto the massof the air where P is the gaspressure. 10.r8 MATHEMATICAL MODELING.4.and thc changein temperature the followingrelationship: or. we l.35 litersthroughsweat. and cv: 300. whele u is the average velocity and A is the crossto sectional area. A rnore general representation Newton's inversesquarelaw of gravitationalattractioncan be written as R: s ( x ) : g ( 0 ) ( l( + . For this case.
(b. l.: 0.000m using a stepof 10. k : the evapol'ation V: t rate(mm/hr). Assess validity oi your resultsby determiningthe radius the b o. and 7.However. assume that upward velocity is positive.r : 0. lf 0.O.l5 Newton's law oicooling saysthat the temperature a of body changes a rateproportionalto the differencebetween at its temperature that of the surrounding and medium (the ambient temperature).**L*i FIGURE I.16 Afluid is pumpedinto the networkshownin Fig.8). P1.use the chain rule For to express differentialequationas a function of altithe tuderatherthan time. : 2l "C and ft : 0. use a forcebalanceto derive a ditlerential equationfor velocity as a function of time that utilizes this more complete representation gravitation. Recall that the chain rule is du dt du dr d.l "i "i "'i t'ili i.1 the casewhere drag is negligible. : ths ("C).PROBLEMS where g(r) : gravitationalacceleration altitude.1 mr/s. at ( a n d : t h ec a r t h ' s a d i u s = 6 .4. R r m (a) In a fashionsimilar to the derivationof Eq..Qt:O.000m where the initial velocity is 1400m/s upward.8 rn/sr). Use Euler's method to of computethe temperature from I : 0 to l0 min using a step sizeof I min if I. er:.017/min... lbr this derivaof tion.gtO) : gravitational acceleration the earth'ssurface(! 9. andQo: 0. (d) UseEuler's rnethodto obtain a numericalsolutionfrom r : 0 to 100. . (1. that a sphericaldroplet of liquid evaporates at l.Compareyor. of k : the proportionalityconstant(per minute).1mm/min andthat thedropletinitially hasa radiusof 3 mm. l{ Suppose a ratethat is proportionalto its surfacearea. Suppose arnbienttemperature that a cup of coftee originally has a temperature 68 'C.2. : time (hr). rvhere volume 1mm3).$ . Assume that ft : 0. : T") at (c) Use calculusto obtain the closed form solution where u = u.5min..r dt r9 of your final computedvolume and verifying that it rs consistentwith the evaporation rate.16. : 0..I6 P i ilr*?i=iI iri:. and A : surface area 1mmr).r (in m) at measured upward fronr thc earth'ssurfacetm/s2). 3 7 x 1 0 6 t . dV :: dt kA where Z: the temperature the body ("C). dT k(T . Use Euler's method conrputethe volume of the droplet from I : 0 to to l0 min using a step size of 0. r : time (rnin).2.determine the other flows.rr result u ith the analytical solr"rtion. s...at.6.
r.l i.m : mass(kg). the colon ope'rator'. where : ternrinal velocity(nr/s). uE1 5t %I zl x+ = .\ iiEi. to on Learninghow to usc vectors crcirtc sinrplclinc plot basecl an equation.t (m/s').p.4+'" . .and thc mathernatical cxpressions. I T tsi V t... .Asidefrom prcdicting terminalvelocity.l l'ttnctitlns.. thc irs. Lcarninghow realancl cornplex nurrrbcrs assigned variables are to are valuesusingsinrpleussignrnent.f zr'+ 4_ %.q: gravilational accelerertion r. how MATLAB's calculator Specificobjectives and topicscovered are ' .piic:.1 CHAPTER OBJECTIVES chapter to providean introductiorr overviewof is and The primaryobjective o1'this modeis usedto implcmentinteractive cornputations.:cand 1oq1. we usctlir firrcebalance detcrnrine tcrminalvelocityof a fiecfalling to the I je o h j e c ll i k c i r h L r r r g cu r n p c r . Fundomentols MATLAB 2.this equaand t'. . a YOU'VE GOT A PROBLEM n Chap..: a dragcoefl'icient the to tion can alsobe rearranged computethe dragcoefficicnt m8 ut (2. Llnderstanding priority rulcsfirr constructing undcrstanding ol'builtin lrrnctions how you can lcarntnore and Gaininga gencral a b o u t h e mw i t h M A ' I ' L A B ' sH e l p f a c i l i t i e s .. I 20 ffi=?N . (kg/m). . l. Learninghow vectors and matrices assigned .
( .B usesthreeprimary anddata' winclow'Uscdto entercommancls .. ffi qA ' . a r t.2r MAT4gllyllerynENl 2 r THE 2.ttt. will leartr In this showinghowMATLABcanbeenlployedtocolnputeqtrantitieslikeclragcoetTicients'we willalsoillustratchowttsgraphicalcapabilitiesp. .'^ :::):i:I.adtlititlnalinsightintosuchanalyses.ws' ln chap' 3 we usc of thc c. A B s k r p s a l i n c b c t w c c l l t  .i ( L mfirtis1Ac h b .. wilr r.. n r r n analyzer ' s r rd lh u . 51 Lo thc rcsult' MATL.I TABTE m.r  a h e l ( l . ) l t n d t h c n u n l b c r ..$ l . perfornirrg " "!i:H:i:iilll1l.kg u.'Ji:'. r r l ..:ll. y o t t t e n m. zt ct how MATLAB canbe uscdto we chapter. B e y o n d l r n : r l v . ^ .::'# "'*.3.. 1c ' ' M A  l .llctlentthcctltntrratrclsonMATLABasyouprocccdthrotrghthe following material' witrclows: fUeifA. ::::::: "v rute grs crcn pot rn ":1 ati "'"u'TilJffill'.st a n c l s .iumpers the we nleasure terminal Thus. M A T L A B i s a c o m p u t e r p r o g r a l x t h a t p r o v i < l c s . Conrtnancl plotsand graphs' display .ln MAftAB progralns' ff."pt.ctilculaticrns.lestimatethe drag coeificient' provideso *t' equation lectu'lorlhis purpose' d ^ m . shoultlrcad it while .' bcingdisPlaYcd a y o L lt y p c i n c o n t ll lrI a s c q u c n t i i t l t s h i t r n s nlode o1'MATLAB oper''rtcs The calculator you can think of it as operyou get'rresult by line' For eachcotnurancl' Lh::s' line mancls you typc tn For calcttlator' exanlplc'if ^ii"g f if. n o t c ' .rake crcatcMiiles' prompt *iii ut. if in The clata Table2' I werecoltt. . many typcs. H( err: r r '.l rl :n rk 'l j n c s t' io :r' r . . t h eprtlvides a c o nnice tool t e n v i r o n t l r e n t f c l r h it u s e r w i t a very v e l r i e t r ln particular.this velocityof a numberol .] ) .:.AB will disPlaY a. the edit window to openwith thc commancl tht tu'ntunti window will Afier startingMd.1 i(1..: ii' ffi:l :'.1 of knclwnmass.. [ h i s c h a p t e r h a s b e e n w r i tThe a s a l relficientway x c lbeconle h a t i s . c i s e . C.t l r r c to .:il{..:. a uery l'ancy _.*i. to create can commands be usecl Chap. lr : l l ud ! ' ' I ) r l i n e ' sa r e i n c l u d c t l w i t h t h c a u n .i.":ili:. u h .varuaun comlrtnds.* windtlw' Usedkr gaii winclow'Usecltt'rcreuteand edit Mfilcs' . we show how sr'rch o n e f u r t h e r .r to inrple.tring oroficientist'oacttrallyinl.=.. 2. .ol. ^ n ^ ^ ^ . toope * *' 1P.ILJ 8 b zt\ 4x AL ^r 4 ?t 5t !8 . T in fiont o1'youlcornpLrtcr.t.ml s Dotoforthemossondossociotedterminovelociliesofonumberofiumpers BO9 653 gll 929 721 6A2 5l l 836 477 54 557 la \ 509 (1. i t h e rr r t a n t co..mmand anclgraphics we In this chapter. wind.ciscness.
You can type several commands the sameline by separating on them with comnlas or semicolons. 2.x = . they will be displayed. Try typing 1 _ A Note how the assignment echoprintsto confirmwhat you havedone: 4 Echoprintingis a charactelistic MATLAB.Thev are distinct names.2. the nan)e a is not the same as the name a.For example.1 . >> a 4.2 ASSIGNMENT Assignment refersto assigning valuesto variablenames.A = 6. you the could now use ans in a subseouent calculation: 'ars + II with the result 50 MATLAB assigns resultto ans whenever you do not explicitlyassignthe calculation the to a variableof your own choosing. and if you usethe semicolon. twe short Iong short e long e long a eh^rt end 4 MATLAB treatsnames in a casesensitive rnannerthat is. Thus.st 4 zl+ Z] X+? "L x jj€ z th r 15/ I .ans. typing Try >> A = 5. 2. frTTbl . they will not. you separatethem If with commas.f J _A zfTtr ='1. mand line with the semicolon ) character. To illustrate this.1 Scolors The assignment values scalar of to variables sirnilar otherconlputer is to languages.22 MATLAB FUNDAMENTALS Notice that MATLAB has automaticallyassigned answerto a variable. enter >> g and then enter >>A long bank eng Seehow their valuesare distinct.t. This resultsin the storage the of valuesin the memorylocationcorresponding the variable to name. lt carrbe suppressed terminating comof the by (.
__i + .scientific calculations.The unit irnaginary number ir preassigned the variable i.__.65]. to "/J Consequently.p o i nfto r m o iw i t h 1 5 d i g l h f o r d o u b l e o n d Z d i g i t sf o r s i n g l e F l o o i i n g ' p o i nfto r m c tw i i h 5 d i q i t s F l o o i i n g o i n t l o r m o tw i f h l 5 d i g i i s f o r d o u b l e o n d / d l g i t sl o r s i n g l e p ll B e s lo f f i x e d o r f l o o f i n g ..0000i predefined pi.it alwaysusesan i for display.. I T a*!1.[h 4 el 5t I Z} X+ . example.0000i It shouldbe notedthat MATLAB allows the symbol j to be usedto represent unit imagthe inary numberfor input.0000 + 4.1415e+0tr0 3 .However.2.5897 9 1. >> x = 2+1*4 2.2 ASSIGNMENT 23 We can assigncomplex valuesto variables. r416 i. fbr >pi 3 .They all have the syntax: f ormat typr.1 41592.complexvaluecan be assigned a simply as in >> x = 2+i*4 2. 1 4l 5 9 2 6 5 3 5 8 9 7 9 3 e + 0 0 0 3.1415q1b5J58979 l.p o i no r m o tw i t h 5 d i c t l t s B e s to [ [ i x e d o r f l o o t i n g o i n l f o r m o fw i t h 1 5 d l g i t sf o r d o u b l e p andTaglsforsingie f E n g i n e e r l n go r m o fw i t h o l l e c s l5 d i g i t so n d o p o w e r i h o t i s c r r n u t l p l eo f 3 E n g i n e e n g f o r m o tw i i h e x c c l l y 1 6 s i g n i f i c o nd i g i t so n d o p o w e r r t t h o t s o m ut i p l eo f 3 F i x e dd o l l o r so n d c e n t s Exomple 3. you desire If additional enterthe fbllowing: >> format long Now whenpi is enteled resultis displayed l5 signilicant the to figures: >> pi 1. r4r6 precision.r416 3 . For example.c h o ]t Lcrng srrort lonlJ e shorl: g lonq g short eng e Result S c o e d f i x e dp o i n t f o r m c tw i t h 5 d i g i t s f S c c r i e dr x e d . 1 4 1 5 9 26 5 3 5 8 9 79 e + 0 0 0 1 1A long eng bank el + '"1 F{'J ffiE\ z\ _ a :. Noticehow MATLAB displays fbur decinral places. Thereare several variables.0000 + 4.since MATLAB handlescomplex arithmetic automatically.1416er000 3 . type >> format short The f<rllowing is a summary of the format commands you will employ routinely in engincering and.:a. twe ..141592b51 8979 5 To return to the four decimal version.€  a. .
2 Arroys. the in the followingcase. Enterkey woulclbe struckafterthe 3. Vectors qnd Mqtrices An arrat' is a collection of values that are represented a single variable name. I0 l 2. lh r ?l 5t I Z] X+ .>A.. 4 6 I 10 I' Tlre resultin all threecases will be b= 1. A column vectorcan be entered several in ways. 6..For exan.1 areactuallya matrix with one low andone column.>b1. zt + zf :5yst LIZ 4 a_J*= _r { "L x jl':vI A <. a >> Lr =  2. or _.24 MATLAB FUNDAMENTALS 2.rple. or. Enterkey (carriage the return)can be usedto separirte rows. When we speakof vectors. by transposing row vectorwith the ' operator. 8.Try them. lr23 456 7 8 9l ffi qA1. For example. we to which are more contmonlyused.2. the 6 andthe I to assign the the matrix: . 4 b S 1t) A rnatrixof values can be assigned lblkrws: as r'23 456 789 In addition. row vectorcan a be assisned fbiiows: as 123451 Note that this assignment overrides previous the assignment a : 4. The scalars usedin Section 2. Bracketsare usedto enterarraysin the comnranilmode.  .2. Oneby dincnsirnalarral't arc callcd tu'/ort and twodinensional anavs are c'allednrutnce:. usuallyreter column vectors. of practice. In row vectorsare rarely usedto solve rnathematical problerns.
2.hocommand: > > rvho Your yariabl al es ans . LrsinqJ 175 bytes Note that subscript notationcan be usedto access individualelcmentof an array..( j . .If a colon is used to separate tw'o nunrbers. ( 2 . .y tlouLr Lc: . Both havetwo argumcnts.l) 2.s.r. .1) 6 Thsre are sevcralbuiltin functions that can be used1ocreatcmatrices.3 The Colon Operotor The colon opefatoris a powerfll tool fbr crertingand manipulating arrays.. c I o s . [h Y vI t('€ .?'' 5l I Z] X+ .trrb l e doubLe array array i)i I'ay arr..A(nr ) selectslheclemcntinmthrowandthcnthcolurnn. cxarnplc.) J 00 UU Sintilarly. the orresfLtnctioncan be used to create a row vector of oncs: u = o n c : : .Lrray is 21 elenrrrnl.cnterthc whos command: >. .n A(:. respectively. creatca 2 x 3 matrixtl1'zeros: to thc numberof columns.tlt si 8 Foranarray.Forexample.nZt* 5t s L <.MATLAB generales n u m b e r s c l w e e n h e m u s i n ua n l lhe b ffial z$4 zl' + zf Ll 2 i 'A I la . first tbr the nulnberof rows and the second thc tbr For example. firurthelement the o['thc columnvccfort) can be displayed as :> b(4) . an For exarnple. E : 7 . list of all current a variables be obtained entering by the can v.whos f Jd r { i e A dns l) x(comfiLex) Clrancl total Sr ze lx3 I x5 LXL 5xi '1 xl Byte:: 12 40 8 4Ct I6 Class clouble cloubLa il.2.rrc: b x or. with moredetail. For the ones llnd zero:i l'unctions creatcvcctol'sor matricesfilled with ones and zeros.2 ASSIGNMENT 25 At any point in a session.
n ) which generates points betweenxl and x2. n) ). It has the form logspace(x1. t ffi= ?l + Ll 2.0000 ope Note that negativeincrements can also be used >" f = i0:1:5 com t0 9 B 1 6 5 Aside from creatingseriesof numbers.0000 1.26 MATLAB FUNDAMENTALS incrementof one: : l1. For example n '> linspace(0.For example. x2. the second through the fourth elements are returned.4000 0.6) 0 0. 100 generates row vectorthatis logarithmically The logspace lunction a equallyspaced.3 rnr 1.the function alromatically generates points.5:3 f Ifn 2.J whi For I If colonsare usedto separate threenumbers.:) We can also use the colon notation to selectivelyextract a seriesof elementsfrom within an array.4 The rlnspace ond losspace Functions provideotherhandytoolsto generate The iinspacre and logspace functions vectors of points.BOOO 1. * J Zl eNst "L x vI tlh 2 3l ZI X+? =$8 .2.When a colon is usedin placeof a specific subscript.For example.It spaced a has the form I i n s p a c e (x l .the colon can also be usedas a wildcard to select the individualrows and columnsof a matrix.0000 plv thes ing Th AS lf the n is omitted.( l.5000 3.5000 2.The 1 inspace functiongenerates row vectorofequally spaced points. colon reprcsents entire row or column. MAILAB generates numbersbetweenthe the first and third numbersusing an incrementequal to the secondnumber: >> t = L:0.basedon the previousdefinition of the vector t : > > ' 1 ( 2 : 4 ) 981 Thus.6000 o. x 2 . Alsc Res 2.2000 0.0000 2.1.the secondrow the the of the matrix A can be selected in as >> A(2.
It will be discussed detail later in this book. 50 2. in zl'+ .. the priority order can be overridden with parentheses. "L x .. because exponentiation has higher priority then negation.r 2*pt 6 . as in the Iast example.2.3 MATHEMATICAL OPERATIONS 27 which generates logarithmicallyequally spacedpoints betweendecadesl0'r and 10": n For examp)e. >> logspac e (I . or simply displayed..0000 If n is omitted.similar to other computerlanguages. the following result would be obtained: >> Y = y4 ^ 2 _r6 Thus.l 4 Z] X+ 6re %& <. For example.2 .i.0000 100.4 is first squared and then negated. >> y ^ 2. Try .. The common operators. are in Exponenliolion Negotion Multiplicotion nd division o L e f td i v i s i o n ' l A d d i t i o no n d s u b l r o c t i o n These operators will work in calculator fashion. [h .2.Pt/4.0000 10."1 t lJ  ffi=+ r\ a  *6 l =:T { 5[ s\ __. 4) 0.. order of priority.832 Also. As with other computer calculation. scalar real variables can be included: >> Y .it automaticallygenerates points.3 MATHEMATICAL OPERATIONS with scalarquantitiesare handledin a straightforward Operations manner. as in the nexttolast example.45 0.1000 1. Parentheses be usedto overridethe priorities can as in >> Y = \4) L6 : Lefi division appliesto matrix algebra.5533 Results of calculations can be assigned to a variable.
f4 5 tjl'.1011 0 >> x ^ . ti 2 l. and >> b = Now.0000i 0. lB.sJ 5t 3$& <. try >> a * A . b * allS  2 4 6 8 10 4 8 12 16 2t) b 12 18 24 l0 8 16 24 3'2 4L) I0 20 30 4t) 50 To further illustrate vectormatrix mLrltiplication.0000 >> x + y +1r. introducing sorne of those manipulations here. can Herearesomeexamples usethe that valuesof x (2 + 4i) andv ( I 6) definedpreviously: '>.< +I2.d*b r10 and likewise.00001 The real power of MATLAB is illustrated in its ability to carry out vectorrnatrix i c a l c u l a t i o n s A l t h o u g h w e w i l l d e s c r i b es u c h c a l c u l a t i o n s n d e t a i l i n C h a p . [h  .0004 >>'1 /. the outer pnrluct >:.2a MATTAB FUNDAMENTAIS Calculations alsoinvolvecomplexquantities.: . 8 .'3 * x ans 5. Z ffir qAb zl' & ZN ux LJt ffi A i x+ z_! 1J'€.llrst redefine a and l:. l0 t6 4?.2 0.2000r ans 12.:.. The irtner product of two vectors (dot product) can be calculated using the * operator.0000r an!.O0tJ0 + 4. :. i t i s w o r t h .
For example..ons must aQfree.2132 ?.5915 2. you will want to carry out calculations item by item in a matrix or vector.The MATLAB manualcalls thesearray operatlons.3183 'I . 30 66 r02 35 81 t2.t\€ l z_lh I . lh x _T .s.6 4?.?. Matrixrnatrix multiplication is carried out in likewise fashion: >> A * A 30 66 102 35 B1 126 42 96 150 Mixcd operations with scalarsare also possible: >> A/pi ans = 0.2.(h Zl.8648 We must always remember that MATLAB will apply the simple arithmetic operators in vectormatrix fashion if possible.At times.^ 2 2a 36 64 81 ^ operatorsignifiesthat the operationis to be carriedout elementby The .3 MATHEMATICAL OPERATIONS 29 >>A*b 32 71 r22 Matrices cannot be multiplied if the inner dimensions are unequal. n r49 16 49 ffi z\4 . 9tr 1 50 results matrix multiplication a with itself."I LJ ' 2. in of What if you want to square eachelement e? That can be donewith of >> anS A = . precedingthe element. Try >> A * a MATLAB automatically displays the error message: ??? ETrar using => mtimes Inner matrix dimensi.. Here is what happens when the dimensions are not those required by the operations.2.9099 2.9f 5l .82 0.5366 I. They are also often referred Loas element hycIeme t t tparutit tn. MATLAB provides for that too..9549 1.gt st l "L 4 <.\46\ 0.
^ 2 PressingEnter will perform the calculation again. not LOG(X) Complex See also results LOG2.natrix quantities. For a list of all the elementary functions.6931 1. of X the is elements posrtive.79'LB 2.4 USEOF BUIIT.INFUNCTIONS MATLAB and its Toolboxes havea rich collection builtinfunctions. The uparrow shortcutis a quick way to fix errorswithouthavingto retypethe entireline. But you can also edit this line. For example. 2. operate in anay fashion. >> A .changeit to the line below and then pressEnter. >> A . Most functions.1912 and you will see that the natural logarithm function is applied in array style. of X. abs.try >> log (A) 0 1. Press the uparrow until you get back the line F *  Alternatively. You shouldget back the last line you typed in. acos. the natural are lograrithm produced EXP.Pressthe uparrowkey.0194 1. such as exponential and square root. type '> help elfun One of their importantproperties MATLAB's builtin functionsis that they will opof eratedirectly on vectorand r. Certain functions. f anh. a eys\ 1 l "L 4 <' [h Y ll'€ .you can type b and pressthe uparrow once and it will automaticallybring up the last commandbeginningwith the letter b.if you want to learnaboutthe 1og function.9459 0. 3 1821 64 I25 343 5r2 216 129 Using the uparrow key. element by elenrent. you can go back to any command that you entered. For example. sin.". For example. MATLAB will evaluate the matrix version when the letter m is appended to zf+ . and exp.eJ 5t & Z] X+ . type rn >> helP LOG log' lrlatural is logarithm."1 {z '\ ffi= qA+. if LOGM.30 MATLAB FUNDAMENTALS MATLAB containsa helpful shortcutfor performingcalculations that you've already done. such as sqr t.6094 2. have matrix definitions also.to the matrix A.3863 1. of You can useonline help to find out nroreaboutthem.0986 r. LOG10.
0.f\€ . lh . >> sum(F) 19 In a similar way. For example.2515 + 0.4498 + 0.mean(F).1611i 0.Try >> sqr:tm(A) 3l A.2068i 1.5526 + 0.AB42i I.max(F).1623i 1.A22Bi 1.0.8000 360 13456 ffi Z} x T<.4 I.3134 0.21I7i  There are several functions for rounding.4 1.9f >l 4 l & .sort (F) 1 6 3.prod(F). The round function rounds the elements of E to the nearest integers: >> round(E) 221r22 The ceil (short fbr ceiling) function rounds to the nearestintegerstoward infinity: (E) 1 >> ceil 1 The f loor >> 1 2 2 2 function rounds down to the nearestintegers toward minus infinity: ( F t) f loor 22 2rl There are also functionsthat perform specialactionson the elementsof matricesand For example.4844 2. suppose that we enter a vector: >> E = l1. >> F = l3 5 4 6 Il.0385i + 0.FUNCTIONS 2.5 I.6l. sumfunctionreturns sum of the elements: the the arrays.9503 .4 USE BUILTIN OF the function name.5 I.0185 + O. it should be pretty obvious what's happeningwith the following commands: >> min(F).5555 I.594A1 0.5813 .3481l 0.6 1.
c. : . a M A T L A B a l l o w sy o u t o e v a l u a t e f o r m u l as u c ha s r : . w h e r et h e f b r r n u l a s i posicomputedfbr each value of tl..l/ r . \ Th bo [0:2:20]' Yor Assignvaluesto the parameters: >:.( g * n / c ' d ) * 1 .9)]: 2.r \V trt / due where u is velocity(m/s). values a from 0 to 20 in steps 2: of Create columnvectort that contains .g is the acceleration to gravity(9.25. (1.For our case.1871 51.951 a 49 .a n h ( s q r t ( g * c c l/ n ) * t ) i t) 1B.42r4 50.r118 42 .sJ 5t <. jumper canbe computed that the velocityof a freefalling bungee with [Eq. crl = 0.01 62 46 .5 G gft M. / : s q L t . 1 ( r ) .5823 br.= t.9 = 9. in 68.6416 ( ( I ffir zl Llz 7f 5[ st { l L vI x j_f€ .81.re array. and r is time (s).81 m/s:).4560 51.r1 \ Z] X+? .'> l.1. .is the drag coefficient(kg/m).32 FUNDAMENTALS MATTAB Recall A common useof functionsis to evaluatea fornrula for a seriesof arguments.n is mass(kg). / a t a n h.0 2 4 6 B l0 t2 l4 15 1B 20 Check the number of items in the t array with the lerrgLh function: >> I cnqtlr (L ) allS = 1I lom / loe. [h  zl'+ .6175 51.'t292 33.r I V c.and the result is assigned a corresponding r to tion in the u array.
' xlabel .J ^:.' l t l '!ry l I i l I a o l 6 : 30 20 10 0 : i i j I . lh 4 l. x+ l <.re('Plot of ('Valucs ('Valucs v of ol ver':jus L') v') t') .t. . 60 50 40 30 20 10 0 8 10 1214 16 18 You can customize grapha bit with commands suchas the following: the >> :':.to createa MMLAB allows graphsto be createdquickly and conveniently. nlol (t.5 GRAPHICS For example./) lne gfapn appears ln tne grapnlcs wrnoow ano can De pnnteo or translerreo vra tne clrpboard to other programs..ef >l 3$& .enter graphof the t and v arraysfrom :. : "t* l I i I l 8 10 12 Valuesof t 14 16 18 ?.2. ". > y l .r' ffi z\4+ ...€.f. I i ! .. the dataabove.5 GRAPHICS 33 2.:'> qrid til. L b c l Plot of u versus f 60 50 40 : .."lqfjeTl zl a* z.
which alJows you lo split the graphwindow into subwindows p(ute.I.the f<lilowingcommandswould resurt in both lines and symborsbeing displayed: ':' '. flnal plot woukl only displaysyrnbols. you want to useopen For if circlesenter r:. . v. 'o') ::. [h  Z} X+ . previous plotsareerased everytirnethe plor command implernented.For example.:2 Specifiers colors.34 MATLAB FUNDAMENTALS TAB E 2. the :'' pIoL (1.) v.r.nconrmancl is The hoidsthecurrcntplot ancl axis propall ertiesso that additional graphingcommancls be addedto the existing can plot.l. hol d c. zt'+ =. we had typed the tbilowing if cornmands.. PlotploL .: 7l l Lt r 7iJ .conlmand clisplays solidline by default. pJ This cornrnand breaks graphwindow into an mbynmatrixof the small axes. (t. for symbols.l:jlot (t. .) >> plot ln contrast.by clefault. you wantto plot a If eachpointwith a symbol' you can includea specifierenclosed in singlequotesin the pior functio.1 5t a.o.tit of f cornmand returnsto the def'ault mocre.) MATLAB allowsy.l 5[ st J'  ffi aAI zt\ x v! rf € . yt t. >(t.ruto displaymorethanonedataset on the sameprot. Table2'2 Iiststhe available specifiers.r) v. another hanrlyfunctionis subplor.. Fclrexampre.o.and selects the pth axesfor the currentplot. ond linetypes. n.re Green Red Cyon A^agenio Yello'"v Blccl. ho1c1 off In additionto ho1. . The h. Colors Bl. \r. lLoLd on (t.1ol. if you want [o connect eachclata rnarker wirh a straight rineyou courdrype :'i. (L.o') It shouldbe Inentioned that. has ()r It the svntax subploL (n. . Symbols Poinl Circle 'MO TK Line Types Solid Dolied Dcshdot Doshed m v k Pus l Slcr Squore Diomond Triong (dovrn) e Triongle{up) Triongie(leftJ Trongle{right) i Penlogronr nexogfcrn The 1rlot. example.
cos(r). Plotting a helix providesa nice exampleto illustrateits utility. 2. c o s ( r ) ) >> axis squaLe >> r.I 2 A twoponeplotof (o)o twodimensionol ond (b)o threedimensionoJ circle helix (b) z['+ . y. and z arethreevectors the samelength.2. z) wherex. let's grapha circle with the twodimensional plot function using the parametric representation: sin(t) r: . > > s u b p l o r ( 1 . anrJ are of z. The simplest manifestation this capability is the p1or3 command of which has the syntax p1ot3 \x. >> l. p 1 o t ( s j n ( L ) . lh Z] X+ . y. First.la.irle (' (a) ') As in Fig.er e command.the result is a circle. FIGURE .We employ the subplot commandso we can subsequently the threediand.= 0:pi/50:10*pi. Note that the circle would have been distorted if we had not used the axis sou. 2 .eJ 5t 3$8 <."I LJ ' ffi=b 1t 5t(st l "L Y vI _T j} € . of The resultis a line in threedimensional space throughthe pointswhosecoordinates the elements x.y add mensionalplot. I ) . y.5 GRAPHICS 35 We can demonstrate subplot by examiningMATLAB's capabilityto generate threedimensionalplots.
simultaneously. The resultis shownin Fig. adof ditl'erent optionsfbr exploringand searching detnos.y coordinatessketchout the circumference the circle in the rr' plane in the plot. you are interested learning and to of the excellent booksdevoted MAILAB (e.r and .x+ 1. we againemploy a para: and metl'icrepresentation: sin(/).g.AB capabilities.you can use the lookfor commandto searchthe suppose you wantto find all that of MATLAB Help files lbr occurrences text.To do this. newsgroups. in help is also available interactive or fbllowedby the narne o1'acommand function.familiesol curvesplots.. let's add the helix to the graph'sright pane. The net resultis the characteristic cally as the : coordinate of springor spiralstaircase shape the helix. ..a varietyof replots. curverisesvertithe samefashionas the twodimensional increases linearlywith time. it prclvides rnodeby typing the in As described this chiLpter. books. c o s ( 1 . .. ) .rres graphicsthat are usefulfbr example.6 RESOURCES OTHER ol'MATLAB that we will be using The fbregoingwas designed lbcus on thosef'eatures to overviewof all in the remainder this book.you can obtainhelp tionr The MathWorks. by Help lacility thatcan be accessed itsell'includes extensive an Further.threedimensional meshplots. support as ffiar z[ Ll 2 =al' l_t zl'+ L1 r r 5y 5t "L x 4 a. [h  zl . u : / x: ' > s u b p L o t ( I .l b.As such. Can you visualize of the . that lnc.plotting obiects of instead lines.corn. are 2.1 5t =$8 ..Palrn. websiteat www. couldenter the cornrnands functions l.2005. 2.Fol example.'t irhn anclMATLAB will displayall ret'erences includethe word loq.kt .€.rri thnr.)' cos(1).r .loglogor semilog next. you and that relateto logarithrns.and contourplots. well as a varietyofother uselitlresources. 2 . of Thele ale other f'eatr.s will provideyou with a nutnber clicking on the Help nrenuin the comnrand In thloughMATLAB's Help material.you shouldconsultone If o1'MATLAB's capabilities. 2 0 0 7 ) .Hanselman Littlefield. access a numberof instructive to dition. p 1 o t 3 ( s i n ( L ) . However.. lrclp conrmand If you do not know the narne.ces available learnaboutthese well as otherMATL.) >> rirle('(b) what'sgoingon?As time evolves.plottingon the complexplane.Thi.it is obviouslynot a comprehensive of in rnore.mathworks Finally.36 MATLAB FUNDAMENTALS Now. 2 0 0 5 :a n dM o o r e . L ).and technical Thereyou will l'indlinks to productinfbrmation. package the window. 2 ) .As described to as solll.
.l r . l). renowned scientistsand engineers. /!. n .1) and the data from Table 2.. beyond merely computing the drag coefficient. most cases. engineersand scientheserelationships collectingand analyzingtheir own data. However.Although theseare of great utility. .4 4J. : 0. by tists often must supplement Sometimesthis leads to a new forrnula. The drag coefficientscan then be computedwith Eq.. i Li.We will do this by estimatingthe drag coefficientof a freefalling human based on Eq.5.1 4l. intent is to gain insight into the pattemsand mechanisms our In this casestudy.r.3125 of Now.)/ 4'1.1).ll1r'. 'q ( r ' r l )  r'clnt. 1 .4 rlclrrr rr i i) l i . r ' " ')1 Vpl t r:Ci 'rlt.i !. prior to arriving at a final predictive equaand developingplots.')t) lr) for some statistics the We can now use someof MATLAB's builtin functionsto cenerate results: .25l l to 0.l llr.(lL'r '. 9).e[ st { x z [h /_l€ .r . 1. 1 r 7 q = 1 1 r ( ' .l 8t:1.'rrt::l!1 !.1 .l 91..9 ..fl1.1 l) . 9.0.... (2 l) to makea prediction the drag: teuninal velocity basedon the average '.1.91. (2.iii. (l ( i l l a '.eJ 5t ?$ Z] X+? I . i i r = t i ri r r ( . trjrr1. .l . Solution. . . r.1 alongwith gravitational acceleration be entered .1l'r'r.1.ll 7. However. (2. l . .1 . ln tion.2. .. Becausewe are performing operations vectors. superimpose line indicating exact predictions(the l.2854 with a rangettom 0. ' a l.we will illustratehow MAILAB facilitatessuch exploratorydata analysis.il') l).) f " 1i.1.o f...ll.9 (tir. can as The datafrom Table2. Thus.. :. z(r Ll 2 . J . .we must include periodsprior to the operators: on elementbyelement (r. r 1). let's start to play with this data by using Eq. '. the averagevalue is 0. J12! kg/m. Notice that we do not have to use periodsprior to the operatorsin this formula? Do you why? understand We can plot thesevaluesversusthe actualmeasured terminal velocities. v l ) ] .We will also the results. .1 'rl. 1 ..1 line) to help assess a ffi= qA+.tt. .tr:.1 !0.e[r + tr4 \ 2.7 CASE STUDY 37 EXPLORATORY ANALYSIS DATA Your textbooks are filled with formulas developed in the past by Bockground. : l a l r L ( g * r l t r t r . we usually "play" with the data by per{ormingcalculations hiddenin the data.i ] . we will use MATLAB's graphicalcapabilitiesto discernpatternsin the data.
r( k q r ' t ' . t : . that This suggests ratherthanbeing constant. L r r i : c l)' I i. 2. theremight be a trendin the dragcoefficients. ' l . Labelth scissa z.1 A simple electricc pacitor.rlrt'I 2.. ) ' . : . t! o. . 2 60 65 70 80 75 Mass (kg) 85 95 PROBTE 2.. ' ' : : u l . you the As in the top plot of Fig. . t g r : t . 1 ) .l ( ' l r . q t ) g . the drag coefficient seems to be increasing as the mass of the jumper Use MATLAB to ger z: 4 to 4.R t / ( 2s 1 ('Irr i. p l o t x I a b .Y oo r!o U.l] r o l r i I i .However.2 The standard norrr curve that c bellshaped .R:50'Lj 2.J q) o O o o c. ' 1 . l wherer:time. . ) .crd generallyfollow the l: I line.2 FIGURE with Twoolols creoted MATIAB Becausewe are going to eventually generatea secondplot. r i r e I ( ' n r . i c t l L \ " r r : . and an inductor on the capacitor4(l) as as ( " / t .because predictions might initially concludethat the averagedrag coefficientyields decentresults.35 o)F o<. r : r f . i ' l : rl. ( ' .38 FUNDAMENTALS MATLAB Plot of predictedversusmeasuredterminal velocities 3ss € 0) o' 50 45L 48 49 50 52 51 Measured 53 54 56 versusmass Plot of drag coefficient 0. drag coefficientsversusmass: This can be seenby plotting the estimated x l .. L e 1 ' . r l L l r . . [ ' l ' I i c t l l r l ( ] i q / 1 l l) L i 1 .) 0 . generatea plot of this fi 4o:12. L i r l . which is the bottom graph in Fig. ' . [h  rtE:l 5 I Z] X+? . . v l . y r l o L( 2 .2. v p r ( .) The resulting plot. i .qo:tl and ( L: inductance. notice how the model tends to underpredictthe low velocitiesand overpredictthe high. we employ the subplot command: .O I c 1 L .2.l ' . .' : : 1 i t n a l ' t . as ffi= llAb zl'+ : : "L x vI <.:rlic lL'tl') 'l'l ol ol I i t lc] ( I)t ('(l i ( t. . i 1 1 l rf li l .2' suggeststhat rather than being constant. 1 ( .) q(t): Lrurar(l l.
. Basedon this result.F. T'hcchargc is and onthecapacitor as a lirnctionol'limc can bc cor. I I ) 2 . givcn that q o . I 1l I = inductance. I .2Thestandard norrnalprobabilitydensitylirnction is a bellshaped that can be rcprescntcd as curve l/ .] : whcre= time.0 .7.p : tir density (kg/mr).l. ] .it rnight motivateyou to conductfurther experiments jumpers to confirm your preliminaryfinding. you would discoverthat the parameter of c.r (rn) can ol'tcn bc rnodclcd by Hookc's law: Ij:lr Inductor It\/ \ ! /  .r7o thc initial chargc.If you did this.I P ffir qA+. . This hypothesis could be substantisuggests the atedby rneasuring frontal areas individualsof varying masses.i t s d i s a placcmcnt. the result might also stimulateyou to go to the fluid mechanicsliteratureand leam more aboutthe science drag. an induc(or dcpictcdin Fig. and A : frontal area (m2).1A simplcelcctlic circuit consisting a rcsistur.( N ) i s a p p l i c d t o c o n r p r c s s s p r i n g .4 Usc colon notution lo crcatc vectors idcntical to the (#)' .L : 5 . l i r l l o w i n g c r c u t c dw i t h t h c I i n : . 5 I l ' a l i r r c c I . caol' a pacitor. : the rcsistancc. if that heavierjumpersmight have largerareas.PROBLEMS 39 continued increases. Labcl thc ordinateas lrcquencyand the abscissa:.0 Io 0.1. which is the areaprojectedon a plane normal to the direction of the velocity.1 2. [h  1l€. with a larger numberof At the least. 2.1 to. Z] X+ . [ ) . .. Assuming that the densitieswere relatively constantduring data collection (a pretty (2. Usc MATLAB to and generate o1'this r plot lirnctionliorn / . r c ' { Ir l ."1 {t 6 e[st l l x <. P2. In addition.. : a dimensionless drag coefficient. r c e ( r . of PROBTEMS 2. C : capacitancc.2) good assumption thejumpersall took off from the sameheighton the sameday).2) where C. a n d C : l 0 r .nputcd ri(t) as 2 .q. :' \/ llt Resistor UseMATLAB to gencrate a plot ol' this fbnction liom .is actually a lumped drag coefficient that along with the true drag includes other factors such as thejumper's frontal areaand air density: CoPA (2.:. zt'+ .1 2 R : 5 0 . f U s c t h c I i r : r p . as FIGURE 2.eJ 5t 8. t t ( b )  : 1 r n s l r . r c e l ' u n c t i o nt o c r c a t c v c c t o r s i d c n t i c a l t o t h c l i r l l o w i n g c r c a t c c lw i t h c o k l n n o l a t i o n : (a) t : !:{r:JlJ (b) r: t.you might concludethat your model needsto be improved.=.r : l i r n c t i o n : ( a ) r ' : I i r s l ) .
2. n: roughncss LJsr'MATLAB to crcatc a plot displayingboth thc data whercU: r. (c) Develop a sidebysi C. lt 2.5 .r'axcs.5 0 1x t o ' .1/ both the data Usc MATLAB to crci\tc a plot displayirrg (rrsing squarcsymbols)and the function (using a dotted l l i n e ) .6 The density treshwater he computed a lunction can as oi ol'tcmperaturc with the firllowinecubiccquation: p: x .l 1 5t I Z] X+? ..clocity S: channel (nr).v.Convcltthrsvccof the'n tor tclclcgrecs Celsius ancl computca vcclorof dcrrsitics i"1. th r . .12 Thc Maclaurinscrics cxpansion thc sinc is tirr 00r5 0 030 a a22 oo0to 0 0008 0 0c103 l9 '24 t5 t5 3 '25 .r'l . B I 0. Use MATLAB to creatca plot o1'thcsine(solid linc) along (dashed line) up ttr andinr.40 FUNDAMENTALS MATLAB 2. Usc this l'unctionto plot the data ancl l'unctionas dcscribcdirr Prob.m2 o45A O4OlO F.g4. cocfl'icient. 3 >> sum(q) * r(2. Usc whcre p : dcnsity (g/cnr31 and 16' : tcmperat.Thc lirllo'. I0. the abscissa +fi6qrlal 4t z['+* 7l Llz *2f j zl 4+ " a. .r ancl . .5 2 8 9 l 0 . . Rccallthat T( 2. Crcatea plot ol'p vcrsus 't5 7A 3BO It.14 The following para that contractsexponentia . lirnctioncxccpt(hatlogarithnric arc n5BH lirr both thc.r:c ll l. lunctiont)pcratcs an idcntical in fashion (basc10) to thc frlot. ({) Here is somcdatafbr conccntration \'elsustimc (l) for the photodegradation aqueous of bromine: P where k : the spring constant(N/m).rr 0013 )2 0020 l5 9 0C09 0010 t2 0012 by This datacan bc dcscribed the lbllowing function: (.1() Hcrc is sorncwirrd tunncl data lirr lirrcc (I) vcrsus ( velocityr'): LlseMATLAB to storeF and.Explain thc resu s.ll Thc loc_{locr lirnclionopcratcs an idcnticalI'ashion in scalcs uscd to the fil()'.urc rangingliorn MATLAB to gencrate vcctor of tcrnpcratures a 'F 32 "F to 82.Usc this lunctionto plot thc dau 0 035 0 0001 t0 2 and lunctionas dcsclihcdin Proh.r7 71 . (b) Determine the averag resulting values. N 550 6r0 t22A 830 1450 : 519(7'F 32).r! 51 .v=e .2.1. cosl .9 The senii 1o9i.5622x l0 s 11 + 0.15 Exactly what will t MATLAB commandsare I (a)>>x=z. PPm t0 4 41 2A 26 3C t6 40 t3 50 i0 60 c5 A. slopc.Writc a singleline MATLAB statcment conputc a to column vectorcontaining vclocities the based the virlues on in the parameter matrix.1 and measure values.13 You conractthe jur Table 2.t as vectorsand thcn conrputc vcctorsof the spring constants and the potentialenergics. Sln.N ll r.P l o tt h c l u n c t i o n b r r : 0 t o 7 5 r n i n .17 B 4. 5[st :: jlg .1. the Make the rangeof li'onr. ('C).r.8 .v.rct1)r ial in conrputing scriesexpansion. yu.1 usingincrcrncnts 3.8 lt is general practice in enginceringand sciencc that equationsbe plotted as lines and discretedata as syrnbols._ 4.:. which are order conespondingvaluesin T Five springsare testcdand the following data compiled: t. Use thc max lunction dcterminethe maxirnumpotential to cncruy..y) in the top pane and (x.7 Manning's cquation canbc uscdto compulcthc vclocity 01'wirtcr a rectanglllar in opcn channcl: This clata can bc describcdhy thc lirllowingl'ur.rction: BH \t' /s/ .6 oF. >> r (nr/s).. .020 0 0002 2. The potentialenergy storedin the spring U (J) can then be computedas U: (. >>Y=Bx (b) rt q = 4:2:L0.2711ttt')x+t rt \B+2H/ Use subplor to genera (.t1. 2.99987 i (a) If the air density is p compute values of tl cD. l 2. min c.r5 ..r'') +9l one Storethesevaluesin a matrix whereeachrow reprcscnts oi'thc channels cachcolumn represents of the paranrone and ctcrs.00r.6 7 < ? 6 6.(usingdiantondsymbols)and thc llnction (usirrga dottcd Ii: ing datais availablc I'ivechannels: lbr l i n e ) . >> x ^ 3. lirnction cxccpt that a logarithrnic scale is used lirr thc l axis. u . 2.l) in the bottompane 2. I" : 0. Explaintlrc rcsults..vith plot o1'thc scries cxpansion a cluding the terni r7171. m l s 1 0 ? .srnI nt .P l o tt h c l u n c t i o n i r r u : 0 t o 9 ( )n r / s .r2 l 2.r : O to 3tr 12.8 f ( l .I=t+ . 0 3 0 4A 50 60 7A B0 hascdon the cubic lbrmula.r and /1 : dcpth(rn). Usc the Lruiltin tirnctionf . versusrr (right sic and titles on the plots 2.
. ..16 The trajecbry of an object can be modeledas R. 2.5". : Q fbr (a) Il the air densityis p : 1. generrlc.._! u=4 3$8 <.jF \ ffi= z\4+ zl : F+ "L x z. 2. in the top pane and a threedirncnsional plot ol' line i') (. bc ctrrnprrtetl theArrhaniu. from t : 0 to 100m in incremcnts 5 m. and (b) logr{) ft crater sidcbyside vcrsus l/7. lbr E.COs. corresponds the distances. f] I 4.1 (or ratc(s r). ):(tando). . : absolute ( K ) .Use to axis liibels C.225kg/mr. \'=c CosI reactions dcpendence ol'chcmical can 2./ (b)tq . r. your rcsults.15Exactlywhal will be displaycd alier thc lirllowing MATLABcourmands typcd'l arc (a) '> x :':' X' >.d0 4l where _v: height (m).u .13You contactthe jumpers used to generatethe data in Table2. >> r > > : i L l l r ( c 1 )* zt + .314J/(rnolc .. Interpret .\r'*)t. lt x k . of cD.454 0 401 0 453 0 485 0 532 a 474 A 4B6 2. to and the seconddimension (c) Dcvelop sidebyside ofA versus (lcft sidc)and plot rr a (columns)corresponds the difl'erent initial angles.lncludedescriptivc rr a this matrix to gcncrate singlc plot of thc hcightsversus andtitlcson the plots.E: activation energy(J/mol). r(2. use MATLAB to height. l. : i n i t i a l find the displacement r'.PROBTEMS 2.1 and measure their frontal areas. : initial angle (radians). 8 1 m / s r ) .. minimunrand rnaxirlumol'thc the in be assernblcd an array where thc first dimension(rows) resulting values. Usc subplot to gc'ngraphol'(a) /<vcrsusI.stinguish to r'xponentially it cvolvcs as that contriicts thc plot so that thc minirnumheightis zero usingthe azis ol'sinI . l.r. d. a valucsol reactionratcs1brtemUsc MATLAB to gcncrate peralurrs ranginu lrom 273 to 333 K.:30 nr/slbr initial angles valuesof the dimensionless drag coefficicrrt compute Employ a rangeof horizontal distances increrncnts 1.Aa t'itRI..gascontemperaturc stant [8. are in A. which are ordered in thc same sequenceas the conesponding valucs Table 2.. g = gravitationalacceleration ( : 9 .rr to gcneratca twodimcnsional (r.rqy functionto crcatc(b).11The lbllowing paramctric among the dillcrent cases.. Use MATLAB to rangingliom l5 to 75o in and r.hclix horizontal l equllirrrrs 2. 1: 2 : 1 0 .17 The lcmperature wilh t'tlutrtittn..versus (right side).r:e commancl. :) in thc bottom panc.The resulting values. ) 6 3) 2.. IU. and scale legencl di.R .t linc plot ot' Usesubpl. lh .A : thc prccxponential tiewherck: rcaction quency)lactor.nt2 0. and l. : i n i t i a lv e l o c i t y( m / s ) .). K)].r : horizontal distance (m).rnploy a distances eachof the initial angles. Enrploythc semi l. A c o r n p o u n d h a s El :x l O s J / m o ln d A : 7 x 1 0 1 6 . Thc resultsshould of (b) Deternrine average.a n d y .
I MFITES 43 We also learnedthat a numericalsolutionof this eouationcould be obtainedwith Euler's method: Ui+l: ui + dut dt Lt This equation can be implementedrepeatedlyto compute velocity as a function of time.Note that the nomenclature that fact that suchfiles are storedwith a .l.New.Returnto the command window and typc >>scriptdemo The result will be displayedas 50.the scriptexecutes as if you had typedeachof its linesin the comntand winclow. Openthe editorwith the menuselection: junrperat a specifictime ing statenrents compute velocityof the fieetallingbungee to the (1.Type in the fbllowSolution. rn. in EXAMPTE 3. cd = 0. This chapterwill introduceyou to how MATLAB Mfiles can be usedto obtainsuchsolutions. l 9 = 9. 3.6175 just Thus.with the aid of MATLAB. [h  lj€ eJ 5 8. Developa script lile to computethe velocity of the lreefalling jumper. m = 68.12.9)]: [recalEq. bungee File. t ScriptFiles A scriptfile is merely a seriesof MMLAB comnrands that are savedon a file. At t { l _ 5[ st ^l L zJ x <.I S c r i p tF i l e Problem Stotement.Run. suchcalculations can be performedeasily.An Mfile containsa seriesof "Mflle" comesliom the statements can be run all at once. So our problem now is to figure out how to do this. L . Mfiles comein lwo flavors:scrintfiles and functionfiles.This would be extremelylaboriousand time consumingto implementby hand.81. Mfiles provide an alternative way of performing operationsthat greatly expand MATLAB's problemsolvingcapabilities. many small stepsmust be taken. 3. I MFIEs The most common way to operateMATLAB is by enteringcommandsone at a time in the command window. ffi zl'+ zf Lt.However. zf x+ .m extension.3.25.1. They are useful for retaininga seriesof commandsthat you want to executeon more than one occasion. Mfile. to obtain good accuracy.The scriptcan be executed typing the file name in the commandwindow or by by invokingthe nrenuselections the edit window: Debug. v = sqrt(g * m / cd) * tanh(sqrt(g * cd / m) * L) Savethe file as scriptdemo . However.
furcr.g l Ji ^ . into valuesthat are passed list argt)st = the function'sargument (i..1.r/uer is assigned ourvar.but now we will usea functiorr for the task.v'al ue. n l W h e r e a s A T L A B w o u l dt r e a tf u n c t i o n a r n e si k e f r : e el a l l v e i a n d F ' r e c t r a l l v e l a s M your operatingsystemmight not. called the Beyondits role in describing function. l.e. ffi l = l ^l zt\ zl 4+ jy !t'i 5t ?JLJ' zl'+ "L Y a. languages VisualBasicor C. to includekey descriptive The functioncan thenbe run by typing The Mflle shouldbe savedas furrcnarne.. Files 3.mLlLs= MATLAB statements compute r. though MATLAB is casesensitivc.2 Function to FunctionJiles Mfiles thatstartwith thewclrdf unction. In contrast scriptfiles. EXAMPLE 3. Type the following statements the file eclitor: in f rrnction v = % f reef al1vel: Z input: f reef aLLvel.s auLva r. functionsin programming suchas Fortran.2. the command As s b a t i n c t i o n e t w e e n c r i p t s u df u n c t i o n s . window asillustrated the fbllowingexarnple.2 r u n c l t o nr i l e ProblemStotement.m.rr= funclanlc ( .. BlOO So you can seethat even though q was definedwithin the script. : r . where outvar'= the name of the output variable..rme = the function's name.(L.l % v=freefallrrel(L.l. compufe velocityof the freefalling bungee the jumper.determinethe value of s by typing >>g 9.m. two diff'erentvariables.they are Hencethey are analogous userdeflned to and can acceptinput arguments returnoutputs. is the line that is searched the lookf or command(recallSec.44 PROGRAMMING WITHMATLAB As a final step. m. you shoulcl wclrds related the file on this line. As in Example3. generally as The syntaxfor the functionfile can be represe'nted funcLion outr'.it retainsits value back in this is an important disworkspace. [h r af xh VT jjg:l 5t 3$8 .and can be invokedby typing t1elp f uncnane in the cornmand function(lhese thitt to that the srare. by the Hl line.Thus.commadelirnited = regarding the ?reTpcornnerts tcxt thatprovides userwith infbrmation tlre the function. bungee '.elocitiz conputes c:f an cd) rviLh the objecL sjecondorder frcefaL1 wiLh drag velocity drag r.cd) second*order lir_. we will seein the following section. .6).the first line ol'the he/pcorrurrcnts.nLs s L. window).l t emell L.t) Z he)pconme. Note thateven in in f uncname thecommand your computer'soperatingsystem may not be. file Solution.
m. but remembered that it involved bungeejumping. 0./ 45 (kSi m) m) * t). hel. 1. if we had typed >>g zr+ ztfai3'" \ ffi= z$4+ = "L vI 3l F+ <.ccl) compuLes the free fall veloclty of an object with secondorder i nput.1 . 68.: t = time (s) m = nass (kS) (kglm) cd = secondorder drag coefficient ourpu[: (m/s) v = downward velocrty dr:ag If at a later date. Save the file as f reef al lvel and type in >> freefallvel . 0.. you tbrgot the narne of thjs functiorr. at the end of the previous example. 100.81. v = sert(g of ? acceleration * m / cd)*tanh(sqrt(g gravity * cd..'=freefallvel(t.I MFILES % r = rime (s) (ks) m=mass % 2 cd = secondorder drag coef ficrent % output: (m/s ) % v = dovrnward velocity 9 = 9. you could enter >> lookforbungee and the following information would be displayed f r e e f a l .p freefallveL which results in the comments being displayed freef.m.lh Y jtE ?l 5t I . return to the command window \12. To invoke the function. Suppose that you wanted to compute the velocity of a 100kg jumper after 8 s: >> freefallvel (8.25) The result will be displayed as 50. m : % f reef al1: bunqee velocity with secondorder clr:agt Note that.6i75 One advantage of a firnction Mfile is that it can be invoked repeatedly for diff'erent argument values.3..r1lve1: bungee velocity with secondorder drag .2t) 53.187B To invoke the help comments type >.
noted previously at the end of Example 3. 3.5 4l. s s ffi qA+. So even thoughs had a value of 9. mean = sum(x) /n.The variableswithin a function are said to be local and are erasedafter the function is executed.rptional singlefunction are in Mlilcs.z cd) *tanh(sqrt end I Note that althoughend statements t. zt Ll 2 jt l l el' st zl'+ LA z. n r.the Mfile in Example3. Functions can call otherfirnctions. stdev . Hence.1.the following function. a r . we like to includethem whcn t s u b f u n c t i o na r e i n v o l v c dt o h i s h l i g h tt h e b o u n d a r i eb e t w c e n h c m a i n f u n c t i o na n d t h e s u b f u n c t i o n s . variablescontainthe ing the resultsare commadelimited and enclosedin brackets.2 (without conrments) could have been split into two functionsand saved as a single Mfile': v rr = f reef al Lsubf unc (L. computes meanand the standard the deviirtion a vector: of function Imean.46 WITHMATLAB PROGRAMMING would have beendisplayed the following message ??? Undefined function or variable 'S' . functionMflles will be our prirnaryprogramming tool fbr the remainderof this book. m. r t l al. sqr t (sum( (xmean) .3 Subfunctions Althoughsuchfunctionscan exist as separate Mfiles.m. this is an important distinction betweenfunctions and scripts. it would not have a value in the As command workspace..81 within the Mfile.we will ofien refer to function Mfiles as simply Mflles.5000 S= 2 . Here is an example of how it can be applied: '> !' lB 5 1D 12' 6 1.2 m= 7.?l 5t 3$8 Z] X+? "Y ZN <.the variablesin a script retain their existenceafter the script is executed.staf s. in they rnay also be contained a singleMfile. Function Mfiles can retum more than one l'esult. th  .J Lt€ .1. 3. ccl) end function n 9 v (q = vel * (t.For example.In contrast. cd) (g * cd / m) * t).^2/ (n1) ) ) . m. For example.In suchcases. v = sqrt m .873'1 Because scriptMfiles have limited utility. stdev] = stats (x) n = length(x). f unction = veL (t. cd).
1. the otherfunctions(in this case.and then returnsthe If the userenters value. 1. 68.it would thenbe assigned the variable a to n.1. ) The . The inpur funclion can also returnuserinput as a string. In suchcases.Two otherfunctions provideways to enterand displayinformationdirectlyusingthe command window.25) 50.6175 However.s .2: . . For example. It is the only function that is accessible the comto mand window and otherfunctionsand scripts. This functionprovides handyway to displaya value. first functionis the called the main or primary function. l ' r r l F u n c t i o n . name = input ( 'Enter your natne: ' .ve1) are Al1 referred to as subfunctions.OUTPUT As in Section 3. zt+ '"1 r ffiE\ z\4+ zl x+ zf\ x : 4 l T . T h i s f u n c t i o na l l o w sy o u t o p r o m p tt h e u s e rf o r v a l u e sd i r e c t l y from the commandwindow. an . [h .68.s.r = input \'promptstring') The lunctiondisplaysIhepronptstring.2 INPUT.To do this.m. value fiom the keyboard. we run f reef al lsubf unc from the commandwindow If the result is identicalto Example3. T h e r r .For example. useris prompted the with the message Mass (kg): waits for keyboard input.Its applicationis illustratedin the following in example.lts syntaxis .'> freefa]lsubfunc (12.Function.Its syntaxis a disp(rzaZue) where yalue = the valueyort would like to display.t€ .OUTPUT 47 This Mfile would be savedas f reefallsubfunc.if we atternpt run the subfunction to vel. 0.2 INPUT. A subtunctionis only accessible the main firnction and other subfunctionswithin to the Mfile in which it resides. information passed is into the functionvia the argument and is outlist put via the function'sname. can be a numericconstant variIf or able.ef >t I <.or a string message enclosed hyphens. is appendedto the function's argunlentlist.25) command/ funct ion 've1 ' ?? ? Undefined 3. an errormessage occurs: >> vel(I2. m = input ('Mass (kS)r ') When this line is executed.3.ti..!.
suppose that the variable velocity has a value of 50. r r r r l 1 F u n c t i o n .l Drag coeffrcierrt Time (s): 12. A simple representation its syntax ts of fprinLf ('formaL'. return to the command window and type >> f reef . the format codes allow you to specify whether numeric values are ffi zl' + zl Llz .3 A n I n t e r o c t i v e . A sirrrpleexample would be to display a value along with a message.7 i s 50. 6 1 ' 15 (kg/m) : 0 .For instance.: t is a string specifying how you want the value of the variable x to be displayed.eracti're bungee velocity inter. . . cd = input ('Drag coefficient ').plays the labels until it detectsone of the symbols: %or \.4f n/s\n'. L: % f reef a1li: int.active computatjon of the free fal1 rrelocity of an object \ r itt h s e c o n d .6175..\'I Y+ N vI jl € . ) where form.6175 mr's This example should make it clear how the format string works. .2 5 T h e t 1 . [h .rl I i Mcrss (kg): 6g. % acceleration of grraviLy 9 .o r : d e r d r a g .sJ 5t I a. . ( l < g ): ' ) . m = input('Mass (kqlm) : ') . Velocit. As in Example3. but now usethe inprut and disp functionsfor input/output. in Solution.To irrvoke the function./s): 5 0 .the statement along with the resulting output would be >> fprinLf ('T'he velocity is %8. it first encounters a % and recognizes that the following text is a format code.compute velocityof the freefalling the bungee jumper.9. As in Table 3.1 . T h i s f u n c t i o n p r o v i d e s a d d i t i o n a lc o n t r o l o v e r t h e d i s p l a y o f infbrmation.2. > < . MATLAB starts at the left end of the string and di:.t l "L x . velocity) The velocit.48 PROGRAMMI WITHMATLAB NG EXAMPLE 3. m.F i l eF u n c t i o n M ProblemStqtement. In our example.lr (nr. display the value using eight To digits with four digits to the right of the decimal point along with a message. Type the fbllowing statements the file editor: runct ion free[. B1. (s): t =:nput('Time disp('') (m/s) :' ) drsp('Velocity * m / cd)*tanh(sqrt(g drsp(sqrt(g ? Z eo freefalli * cd / m) * t)) Save the file as f reef a11 i . The operation of this function is best illustrated by examples.r J ffii: :.
the comrnand prompt would appearatthe end of the labelm/s ratherthan the next line as would typicallybe on desired. This tells MATLAB that the following text is a control code.which is thendisas Dlaved a tablewith headines: fprrnt fdemo function '2 x = lI I 4 5l.4l.4 12.I Formoi Code zd 49 Commonly usedformotond control codesemployed w i t ht h e f p r i n t f f u n c t i o n . >> fprintf 100 ('%5d %10./5 "'_.yl j  fprintf fprintf (' y\n' ) x ('%5d %10.100. x. The resultof runningthis Mllle is >> f pr:int X 1 2 3 4 5 f de rno Y 20.pi) 6.Here is an Mfile that enterstwo setsof valuesas vectors. in decimal.2*pi.3f\n'.283 3.the control codesprovidea meansto perfbrm actionssuchas skippingto the next line.14159e+000 .800 88. It can also be usedto display vectorsand matrices.400 12 .6AA 17. Descripiion l n l e g e rl o r m c i S c i e n t i f l co r m o fw l t h l o w e r c o s e f e S c i e n t i c f o r m o tw i t h u p p e r c o s e f E D e c l m o lf o r m o t T h e m o r e c o m p o c to [ % e o r % f zf Zg Conirol Code \Il Description S l o r fn e w l i n e Tob \t displayed integer. Thesevectors thencombined are into a matrix.1 e[st :> . z) . lh  rf €. As in Table 3. The fprintf functioncan alsobe usedto displayseveral valuesper line with diff'erent formats.400 z(\ Ll 2 zl  ffi qAb :I .2 INPUT.3.6 r7.For example.OUTPUT TABTE 3. 2A.I. If we had omittedthe code \n in the previousexample.3f %8.^. a I x <.700 120.sJ 5t I z[+ 'A Z} Fh .After displaying valueof veLociry.7 L2a.5e\n'. the MATLAB continuesdisplayingthe character information(in our casethe units: m/ s) until it detectsthe symbol \.B BB. scientific or format.
you can recoverthem by entering >> load veldrag T m n a Now.*tanh(sqrt(g*cdlm) *t) . [h r j. called a MATfi\e.. if the variablesare omitted. As was the case with save./cd) . I Creoting ond AccessingFiles MATLAB has the capability to both read and write data files.25 .In suchcases. simple representation its syntaxis A of save fifename varT var2 . >> cd=[.J <.?l 5t & Z] X+ . B 1 .. ( 1. simpleapproach to createtext files written in the widely a is accessible ASCII format. zl h .mat. For example.245 . >> v=sqrt (g"m .261 . varn This commandcreates MATfile named fitename. if you tried to display the velocitiesyou would ger rhe result: >> v u al ??? Undefined function or variable 'v' 3. all the variablesare retrieved. Such files are createdand accessed with the save and load commands. You can then createa tile holding the valuesof the dragcoefficientsand the velocitieswith >> save veldrag v cd To illustratehow the valuescan be retrievedat a later time. The save commandcan be usedto generate MATfile holding eitherthe entire worka spaceor a few selected variables. the velocitiesare availableas can be verified by typing >> who Your cdv variables are: 3 TI Although MATfiles are q.= 5 . >> clear p F At this point.all the workspace are variablesaresaved. The simplestapproachinvolves a special type of binary file.50 PROGRAMMING WITHMATLAB 3.supposethat you use Eq.2. If the variables omitted.28 .3 ! CI However. zl Llt a .rite useful when working exclusivelywithin the MATLAB environment.a somewhatdifferent approachis requiredwhen interfacingMATLAB with otherprograms.9) to generatevelocitiesfor a set of drag coefficients: > > 9 = 9 . removeall variablesfrom the workspace with the clear command.l'€.213]'. which is expresslydesignedtbr implementationwithin MATLAB. The l oad commandcan subsequently usedto retrievethe file: be load fr lename varl var2 .. varrl which retrieves variables the rzar1 throughvarn from f ilenante.._r _l ffi= qA+ i 5[ sl "L x z.mar that holds the variablesyarl a through varn. m = B Ot .
0 0 0 0O0 O e + 0 0 0 2 .3.irptrt. 3. >> load sirnpmatrix.you can read the valuesback into MATLAB with the load command. s of Loolts (or Repetition).txt In this case. If you want ascii doubLe. by or example..For example.MATLAB creates doubleprecision namedafter the fi l ename. Thesecan be classified programs takenonsequential to as mentsallowing . This structureallows you to executea set of statements a logical conditionis true. branching tlowbased a decision. ' i>: = l9 simpmaLr ix 5192 zF. you could use the load command as a function and assign its values to a variable as in >> A = loeid(simpmatrix. to 3. As an exercise.9f 5l . would typically savea singlerectangular >> A=15 I >> save 9 2. In eithercase.0000000e+000 Alternatively. t x t . The on Decision (or Selection). Because strict sequence highly limiting.PROGRAMMING 3.Its general syntaxis if condition statemenfs ffi z\4 . The looping of flow to allow statements be repeated.1 Decisions if The i r Siructure.. 0 0 0 00 0 O e + 0 0 0 3. mentsare executedline by line startingat the top of the function and moving down to the is include stateend. Alternatively.3 STRUCTURED 5l in ASCII files can be generated MATLAB by appending ascii to the save command.you can denronstrate u s i n g i t t o o p e n s i m p m a t r i x .f\€ l 3l x+ & . lmport Doto. in the to storethe numbers doubleprecision. ascii simpmatrix.3 STRUCTUREDPROGRAMMING That is.just append For file can be irccessed other proglams such as spreadsheets word processors.y o u c a n a l w a y s c o n s u l th e l p t o l e a r nm o r e about this and other l'eatures.3 6 3 9l.you will see if 7 5 . txt is not a MATfile. In contrastto MAlfiles where you might want to savethe entire workspace. I n a d d i t i o n . the program stateThe simplestof all Mfiles perform instructionssequentially.0000000e+000 3. 0 00 0 0 0 0 e + 0 0 9 . . you openthis file with a text editor.0000000e+000 6. a handy import wizard can be invoked with the menLrselections: the import wizards convenienceby File. lh x 4 .you matrix of values. 0 0 0O O 0 O e + O 0 O . all computerlanguages a paths.' ' .txt) The foregoing material covers but a small portion of MATLAB's file managementcapabilities.0000000e+000 9."I LJ ' zl'+ J 9t" 5\ "L 4 <. For example.txt a array Because simpmaLrix.the save commandstoresthe valuesin A in 8digit ASCII form.
The fbllowing Mfile illr"rstrates how this could be done: function f = errortest(x) 11 1 == 0 . the division would be implemented successfully as in >> errotLest(l0) 0.l i n ei f i s t o e m p l o y i t f o r r u d i r rnentary error trapping.For example.rsEr and causesthe Mtile to terminate and return to the conrmand window.1000 Howevel fbr a zero argument! the function would ternrinate prior to the division and the error messagewould be displayed in red typef'ace: >> error test (L . lh tl l zl x+ : 4 Lrt9 ..52 PROGRAMMING WITHMATLAB wherecondr tron is a logicalexpression is eithertrueor false.9n 5 I . if grade > 60. end This structure is called a singlelirrc rf For cases where rnore than one statel. er ror (' 'zcrc:value f = I/x.rdc'). it is ofien convenient to implenrent the i f structure as a single line. the multiline if structure is usually preferable because it is easier to read.nent irnpleis mented. encountered' ) . that hereis a simpleMtile to evaluate whethera gradeis passing: Iuncrion % grader % gldder(g oder (grade ) : determines whether = numericalmessage qrade value is of passing grade (0100 ) % input: r" grade % output: % if drsplayed grade >= 50 di sp ( 'pass rrrg grrade ' ) end The following illustratesthe result >> glader(95. A n i c e e x a m p l e o l ' t h e u t i l i t y o f a s i n g l e . djsp('passing gr. cnd If a nonzero argument is used. An example of its r"rse would be where we might want to terminate an Mfile to avoid a division by zero. This involves using the cr:ror: function which has the syntax.6) passingr grade For cases where only one statementis executed. er ror (msgr) When this firnction is encountered. ??? ErroL usit'tg ==> error:test zero value encountered ffi= zr+ '"1 "L x <.it displays the text message.. E r r o r F u n c t i o n .
on!r evaluateto true.the resultis false.the resultis false.sr.:= 3/3 r: >= 0 Logicol Conditions.'on I PxLrt r.Usedto perfbrmlogicalnegation an expression. Just as fbr lor outcomes eachof theseoperators.on vaf ue. variables. on . . :'io"1 i If either or both express. the resultis true.Used to performa logicaldisjunction two expressions. If eitheror both expresto to srons evaluates false. if If the expre.ion is true.2 Exomple unit = a< 0 s>t 53 operoiors MATLAB in Summory relotionol of Operotor Relotionship Equol Not equcl Lesslhon G r e o l e rl h o n L e s sh o no r e q u o l l o t G r e o i e rl h o n o r e q u o l t o 'r 3.3 STRUCTURED TABTE3.lNot). wherethe yaf ues orexpressions the relaLion is one of and Table 3. . orderof priorityof ihe operotors shownol the loo of thetoble. on +.3 for the outcomes logicoloperotors A truthtoblesummorizing possible is The in employed MATLAB.. the result is true. Highesr ryx x&y Lowesl xly I I F F T T T F "L x 4 <Ah 11.rOn . can be constants. The simplest form of the condr Lron is a singlerelational expresfwo valuesas in sion that compares vaf ue.'..1p7 essr. Usedto perlbnn a logicalconjunction two expressions. These there is a priority order lbr evalualing arithmeticoperations.3 summarizes possible logical operations. relaL i. (Or). the expressjon is false. all lhble 3.Converscly. the relationaloperatorslisted in MATLAB alsoallowstesting morethanone logicalcondition cmployinglogical of by We the operators.t .2. the resultis true..ssrorsevaluate true. will ernphasize fbllowing: .r d pxp/  . * (And). TABTE3. If both e)<pre. on FXp/ ps.exp ress r o.PROGRAMMING 3.S.9 .€ 5t 8.
eJ 5t 3$8 zl'+ LA _\ zl /+? . as with arithmetic operators.the lastexpression first to give F&T&F T The a operatoris evaluatednext. a a The j r .This type of structureoften occurswhen we have more than problem setting.Its general syntaxis if r:ondjtion stat€ment:. elseif conditron.. a and I .hashighest are (r) is evaluated priority.. r ' t : : . MATLAB evaluatesthem from left to right.d ) To make it easierto evaluate. Let's investigate how the computeremploysthe prioritiesto evaluatea logical expres=t.Sincethe . .The entireprocess depicted Fig. r l t S t r u c t u r e . Finally. > .1 . efseif hasbeendeveloped. the logical operators evaluated priority order. T h i s s t l u c t u r e l l o w sy o u t o e x e c u t e s e to f s t a t e m e n itfs a logical condition is true and to executea secondset if the condition is firlse. else Stdae'nlenLS.andy='b'. if.d') I The first thing thatMMLAB doesis to evaluate mathematical expressions..'(y > . t i .substitute valuesfor the variables: the * 2 > 0 & 2 =_ 2 & 1 > 7  _(. e l s e structureis anotherdecision. 3. . Since there are two. if ffi= qA+.' 'd') F in At this point. b .the I is evaluated true.. z( Ll 2 7[e1. S t r u c t u r e . x . [h  ll'€. In choosingbetweenoperatorsof equal priority.parenthesescan be used to override the priority order. I t o f i e n h a p p e n s t h a t t h e t a l s e o p t i z r n if . the lefttoright rule is appliedand (p the first expression r r) is evaluated: F&FIT priority The r againhashighest FIT in as is Finally. statements. hasthe Seneral condition. . .54 PROGRAMMING WITHMATLAB are from highest to lowest: . a * b > 0 & b == 2 & x > 7  . .1.evaluatewhetherthefollowingistrueorfalse: s i o nI f a = .For such cases.a special form of decision two options lbr a particular It the syntax structure.4 ono T h e i r .b.2 .s\ l ^l "L x 4 <. . evaluateall the relationalexpressions 2 > A & 2 = 2 & I > I T F & & tr 1 ('b' l. end EXAMPLE 3. 2 > A & 2 == 2 t''I > 1 ('b' > 'd') Next. example. this ln any thereis only one: 7 x 2. { . statements.
776) 1 >> sign(0) 0 Develop an Mfile to perform the same function.PROGRAMMING 3.! I s\ o ffi qA+. a[h I . I )..ef 3$ 8. statementsr else statements. zt\ aY Z] x+ 4 l _T .."1 { 9l < zl.. zt+ ..the builtin MATLAB sign function returnsthe sign of its argument(.6) 1 >> sign (4...3 STRUCTURED 55 a*b >u& 1^. Here's a MATLAB session that illustrateshow it works.I A slep'by'srep evollotion o{ o conrplexdecision.f\t . x>'7 r*2 ll 2 i >0 & I I  ( Y ' 2& 7>'7 t I >0 &2==2 &1 I '('b' t I Substitute constants t Evaluate mathematical expressions >7 ii tl tl & F&T \..  Z d. .I.Y# F & I I  _('b' >'d') Fl t \YJ I I Evaluate relational expressions F T Fl T + I Evaluate compound expressions T FIGURE 3.l5.. For a scalar.4 i t Structures Problem Stotement..... 0.. elseif condition. >> sign (25. end EXAMPLE 3.
^. >> mysign(0) 0 ffi!3r qA+. an if. . sqn = nYSiQIn(x) returns 1 if x is gTreater than zero. else sgn = O. end zero. ^t ^.9 r ! to zero. First.An rf . The function now handlesall possiblecases. nothing is displayed. end This function can be run as >> mysign(0. To partially remedy this shortcoming. il{#ff: er' "L x 4 zl xh %8.6) 1 Although the function handles positive numbers correctly. 1 is erroneously structurecan be used to incorporate returned if azero argument is used. . O if x 7. . For example. _  _t. % mysign(x) This function can be run as >> mysign(25.e1se structure can be used to display 1 ifthe condition is false: sgtn = mysign (x) f unction 1 if x is greater than zero. if it is run with a negative or zero argument. . returns % mysign(x) I if x is less than or equal Z if x > 0 sgn = 1. x < O elseif sgn = 1. z z lf x > 0 sgn = 1. an i f structurecan be usedto return 1 if the argumentis positive: function if end x sgn > = 0 1. .aro. elseif case: this final function sgn = mysign(x) 1 if returns % mysign(x) x x is is is gtreater less eqal to than than zero.56 WITHMATLAB PROGRAMMING Solution.176) 1 Although the positive and negative cases are now handled properly.
MATLAB allowsa variable numberof arguments be passed to to a function.. I on "ctaaemencs. When this codewas executed.an optionalblock is implemented the expression In if takeson noneof the prescribed values..r rr:i.ratherthan testingindividual conditions. Dependingon its value. e 1 s e i f is However.9f 5l la^.€ . d{oult vulueis a numberthat is automatically A assigned the eventthat the in userdoesnot passit to a function.Tlris featurecan come in handy for incorporating defaultvaluesinto your functions. message the Vorioble Argument List. developed function freewe a f a 1 1ve i.It has the generalsyntax sr. oInerwrse s I a a emel] f . stringvariable.cd) Although a userwould obviously needto specifythe time and mass. case staIe. hereis functionthatdisplays message a depending the valueof the on gra de.3 STRUCTURED 57 T h e s r v i t .different blocks of code are implemented.menrs.It can be usedin conjunctionwith decisionstructures like ffi .sol lr. y a .they might not have a good idea of an appropriate dr.c h S t r u c t u r e .T ' LJ b b ZN x 3!h . end As an example. which had threearguments: v = freefallvel (t.PROGRAMMING 3.xprcss r z a Ju e .diocre' ) D disp disp('Whoops') 'F' case drsp ( 'Wou1d like ) fries wj th your order?' ) otherw]se disp(. As an example..9 coefficient. .1u e .ricch case I .it would be nice to have the program supply a value if they omitted it from the argumentlist. .J . value of a single test expression. qracle: 'A' ( 'Excellent' 'B' ) clisp('Good') 'C' ( 'Mc.m. MATLAB hasa functioncallednarsin that provides numberof input arguments the suppliedto a function by a user. l  z.sf r.the branchingis basedon the structure.recall that earlierin this chapter.lluht end "Good" would be displayed.. .Therefbre. gracle svrifch case drsp case case L r15ri = 'R'.f. T h e s w i t c h s t r u c t u r e s i m i l a ri n s p i r i tt o t h e i f . addition.
81 41 >> freefallvel us.58 PROGRAMMING WITHMATLAB into to the if or switch constructs incorporatedefault valuesas well as error messages your functions.   .m.l) 48 .75 >> freefallvel ( 12.r Must >> ??? Must enter f reef al. freefallvef with secondorder bungee vefocity (t. In the commandwindow. Err(. m.ng mass () ltrofallvol and mass (12 ) f reefallvl ??'. >> nargin ('freefallvel' ) ffir zt'h" "L x zJ 7) F+ il'€ .5 when it is invoked in the command Note that narsin behaves little diff'erently a window.Here is a command by on default.:l 5 I .fl1fu= \ <.sqrt(g cd)*Lanh(sqrt(g / m) * t).25) 50. % acceleration * m. [h. cd) computes freefa11 the of an object with v=freefallvel secondorder % input: Z L = time (s) %m=mass(ks) (default = 0. it must include a string argumentspecifyingthe funcin tion and it returnsthe numberof arguments the function./ of gravity * cd 9.depending the numberof arguments showingthe results: window session >'> f reef allvc. or Notice how we have used a switch structureto either display error messages set the passed the user.61. how this can be done for fr^eefallvel: The following code illustrates function ? % v = freefalLvelt : (t. cd) drag velocity drag.21 cd = drag coefficient % % outpul: (mrs) v = downward velocity % swi t ch nargrin case O enter trme and mass') error('Must ^^ a kg/m) error case cd end A = v ( 'Must en1er nass ' ) 2 = 0.6B.lvel Error enter *sing time EXAMPLE 3.21 .I. For example.68. = . A . l (12.81.
In other 1 words. 9 . If a negativestep is used.5 .rs The f or loop operatesas follows. stepsizesof A .I . A f or loop repeats statements specificnumberof times. . for loop endsafter a specifiednumber A of repetitions A whi I e loop endson the basisof a logical condition. 6. for i = l0: disp rj r end 1:1 "countdown"sequence: . For example.s t a r t : s t e D : .3. When the it reaches end line that marks the end of the loop. . suchcases.At this point. 5. U s i n g o 1 . the programexecutes sLatemenrs. Developan Mfile to compute factorial.Its a g e n e r as y n t a x s l i f o r i n d e x . lh . 4. 1. The size of the srep can be changedfrom the defaultof 1 to any other numeric value. For example.2 Loops As the name implies. the loop terminates the the index becomes as programskipsdown to the line immediately following the end statement.9l 4 t >l & .the default step is 1. nor For example. When this executes. 2 ."1 {t & ffi=b z$4 I 71et st ^ z I x+ / ZI\ x _T<. encl Structure.f i n i s h ". For the loop's logic is reversed. Note that if an incrementof 1 is desired(asis ofien the case).There are two types of loops.f\€ . .. The ror .2.fin jsh.3. r L o o p t o C o m p u i et h e F o c t o r i o l ProblemStotement. 1. The rndex is a variable that is set at an initial value. MATLAB would displayin succession. the index variableis increased the by the srep and the programloopsbackup to the for statement. 5. The programthencompares rndex with a desired the final value. Thus. 4. 2 .3 STRUCTURED PROGRAMMING 59 3.3. loops perform operations repetitively.the s t ep can be dropped. o r .1uteme'.the loop will "countdown"in reverse. dependingon how the repetitionsare terminated. 5 E . doesit haveto be positive. E X A M P L3 . If the index is lessthanor equalto the frnish. MMLAB would displaythe classic 10 B. start. a r ea l l a c c e p t a b l e .inj sh is lessthanthe start and the loop terminates when the index is lessthan the f inish. l.the f . . process The continues until greaterthan the f ini sh value.2 the 0!: I ll I I v ) ') 2 Note that MATLAB has a builrin function f acLoriat that oerformsthis comnutation zl' . for end i = 1:5 djsp(i) When this executes. It doesnot haveto be an integer.
For this case. MATLAB automaticallyincreases size of arays every the tirne you add a new element.the filllowing f or structure: i for r end n.1 "L x <. it is not necessarily directly on arrays.vectorizotion numberof times. which can be run as >> f actor: (5 ) 12a x This loop will execute5 tirnes(fiom I to 5). t. For exarnple. would get the desiredresult.42:5C = i + l.it may not be obvious how to vectorizethe code. y(i) can be represented vectorizedform as in t y = 0:0. vectorization recommended. will hold a value I of5l (rneaning5factorialol x 2 x 3 x 4 x 5 : 120). The for loop is easy to implement and understand. is Preollocotion of Memory. Ih r VT jl'€ sJ 5r I zl'+ . . a the most efficient meansto repeatstatements specific MATLAB. for Vectorizotion.r'.That said. all Lhe integiers from I to n' I I for x end fout end i = l:n = x * i.= 0:0.This can becometime consunringwhen you perform actions suchas addingnew valuesone at a time within a loop. = x. and we the f or loop would not execute. For example.{ Z] X+ .Because MATLAB's ability to operate of providesa much rnoreefficient option.02:50. = cos(t). = cos(r).wherever possible. A simple function to implement this calculation can be developed as fout = factor (n) function (n) : % factor Computes the producc of % t.60 WITHMATLAB PROGRAMMING 3l:lx2x3:6 4l:lx2x3x4:24 5!:1x2x3x4x5:120 Solution. if Notice what happens n : 0. It shouldbe noted that for morc complex code. hereis somecodethat T Ir sl SI fiT=fiar qA+ zf Ll L A e[st .0! : I However. At the end of the process.
UI:).hrL/ F^LOr t ( i ) >l = I/t(r). .3.rnert.hLt L:. tt er.tte.o.. s y the f'act that it always exits at the beginning lf the structureon a false result is somewhat constraining'For this reason'languages such as Fortran90 and visual Basic have special structures fhat allow loop terminationon a true condition anywherein the loop."" be mimicked WTTbl zl zt + tA\ aA+.Structure.s end The statemencs betweenthe whiLe anclthe end are repeatedas long as the condr jon t is t r u e .tion. preailocation herps reduce memory frag_ mentation.A s i m p l ee x a n r p l ei s vO A while lclop repeats as long as a logical condition is true.: ' rs t r u c t u r e ' k A l t h o u g ht h ew h i l e s t r u c t u r ie e x t r e m e r u s e f u r . The follow_ ing code preallocates proper amount the oi ".01:5. 1. which also enhancesefficiency. e^ndirian st. a s[sl Ll 2 { L 4 x <.eJ 5t %8 af x+? .Iir(i). iun. y .MATLAB rnustresizey every time a new value is determined.ii. Although suchstruduresarecurrenrlynor availablein MATLAB.y by using a vectorizecl statementto assignonesto y prior to enteringthe loop. In addition. the anay is onry sized once.oneq(si:e(t)). encj end Thus. general syntax is vrh. the resultis is E 5 2 1 T h e r r ' i l r : ' ' ' 1 r r r .. The r. l I l:lerg.3 STRUCTURED PROGRAMMING 6t setsvalue of elementsofy dependingon whether or not valuesof t are greaterthan one: L U:. lh lj€ . Y(i) else r f y(j) end end For this case.l. Its while disp encl x > 0 (x) When this cocle run.0:. for i = l:lengLh(t) rf r (i )>l y(i) .
end end so Notice how 5 is subtracted from x on eachiteration..or posttest.. Sucha structure calledarnicltest If the problem requiredit...r. break structure.e. .called a while. Every decision loop must havesucha mechanism.Two other functions.62 PROGRAMMING WITHMATTAB by a specialversionof the while loop. 1+%q'tr 4 zt+ zl . end stafements . < (..The beep commandcauses computerto emit the a beep sound.. This featurecan be demonstrated irnplementingit in conjunction with by severalother usefulMATLAB functions.The commandpause causes procedure stopand wait until any key is hit. break. break.terminates execution the loop. couldalsoplacethe i f . with statements is loop. . middle.vhile if (1) conditron. [h  . Otherwise it would becorne socalledinfinite loop that would neverstop. whrle if end x 3 (1) q.N17 =L x j]\€ .Note that as shown. l.t ic and toc. Commond. The 1. we could placethe break at the very beginningto createa prel(. break statement the very end and crewe ate a posttest lo)P. ffiat  . beforeand atier it).This represents rnechanism that a the loop eventually terminates.). a at Alternatively. a singleline i f is usedto exit the loop of if the conditionteststrue. depending on wherewe put the exit (beginning. is fhis simplicity that led the computerscientists Fortran 90 and Visual Basic to favor this structureover ofher forms of the decision looo suchas the conventional v..the break can be placedin the middle of the loop (i. Thereare often timeswhen you might want a programto temporarily halt. a to A nice exampleinvolvescreatinga sequence plots that a userrnight wanf to leisurely of peruse befbremoving on to the next.hile structure.or end)dictates who developed It nrid.st loop.'rXtutements wherebreak. end EXAMPLE 3 are It shouldbe clear that..An exampleis while If .. in which casethe procedure halt as will fbr n seconds.The following codeemploysa f or loop to createa sequence interesting plots that can be viewed in this manner: of for n = 3:10 mesh (magic (n) ) pause end The pause canalsobe formulated pause (n ). (1) x w <  0. The syntaxof this version. work togetherto measure elapsedtime.'. break. in fact. all threestructures really the same.?l 5t I <.That is.can be written as r.Thus. whetherwe havea pre.
rlgn for pute the roots. tic and toc can be employed identify the partsof an algoto rithm that consumethe most execution time. c ) % quadroots: roots of (a.g. v a providesa nice approach designingan algorithm to comSolution. The rootsof a quadratic eqLration /(x):uxl+lrx+r' can be determined with the quadratic formula b +. the computer will beep. In such cases.For instance.. Although the foregoing examplesmight seema tad frivolous.4 NESTING AND INDENTATION We needto understand that structures can be "nested"within eachother. % quadroots rools jon z % input: 9o o lo. if you ever have the urge to use the command pause ( inf ).sl 5t I <. The following code then confirms that pause (n)works as advertisedcomplete with sound effects: tic beep pause (5) beep toc When this code is run. c) : quadratic real and o'quod'ot equation complex jaqua' llt Lth nit ]e. b. following exampleillustrtrtes concept. ktpdown r/e.ond otdet coefficient ffi ?JLI' zl+ z$4 J "t J :I 5\ "L x 4 4 1.sllrrc refers to placingstructures within other structures. you can return to the command prompt by typing Ctrl*C or Ctrl+Break.we first recognize that depending whetherthe parameter on (e.t8.006306 seconds. the commandscan be quite useful. By the way. MATLAB will go into an infinite loop.1':.To start.3. o nq u a d r o o t s ( a .4aa LA Developa functionto implement this fbrmulagivenvaluesof the coeff'cients.This "bigpicture" versioncan be programmed tionalcases as f ..This involvesdeveloping the generalstructure without detailsand then refining the algorithm.f i l e s . The the E X A M P L3 .4 NESTING INDENTATION AND 63 The tic command saves the cuffent time that toc later employs to display the elapsed time. [h Z] Xh . Five seconds later it will beep again and display the following message: ELapsed time is 5.the Ctrl*C or Ctrl+Break key combinations comein real handyin the eventthatyou inadvertently create infiniteloop an i n o n eo f y o u r M .Nc.we will eitherhave"special"cases or usingthe quadratic fbrmula. Further.u n c t i .singlerootsor trivial values) convena is zero. 6 E N e s t i n gS t r u c t u r e s ProblemStqtement. 3. b .
rooLs %real r1 = ( b + sqrt(d) ) ( b ."1 {z 7t 5fi st ll "L x 4 <.(d)) t'2 el se %complex roots = rl b / \2 * a) i1 = serL(.of roo[ first root imaginary ffi= zl+ .64 PROGRAMMING WITHMATLAB Z b = firstorder Z c = zera order % output: Z rI = real part Z i1 = imagrinary Z 12 = real part % i2 = imaqinary 1t d == u coefflcient coefficient of first root part of flrsL root of second root part of second root %special els<: "ir'r 'dr.sqrt. [h :. else?tr i vial end sol ut iorr solutron. c) : b.rbs(d)) r) rl / / 12 * ().tl cases e '^rlL1a end Next. * a) a) / (2 * a) end We can thenmerelysubstitute these blocksback into the simple"bigpicture"framework to give the final result: functron ? % % input: % a = Z b % c % ouLput: % rI Z i1 = Z secondorder first order zeroaYder real partquadrooLs (a.rdrat ic . quadroots: quadroots roots of (a .b.b ^ 2 if cl >. we developrefined codeto handlethe "special" cases: %special cases if b '= 0 e.srngle root 11  V. c) quadratic real of equafion and complex roots quadratic equation coefficient coefficient coefficient of first part.I Lrtg  9r\ 5 J 3l x+ I .O f or:mul a 4 * a * c. Try again' ) error('Trivial And we can developrefinedcodeto handlethe quadratic formulacases: d %qu.
eal.inary part of second root == u Sspecial cases ifb=0 ?single roor 1Lc/b else ?trivial solution ( ' ?rivial solut ion.notice how indentationhelps to make the .part of second root 12 = imag.8660 >> quadroots (l.5000 i1 12_0.rs >> quadroots(1.. 4 *a Ediscriminant rf d >= 0 ?rea1 roois ri. (abs (d) ) / (2 * a) r) l .5000 0.= (b + sqrt(d)) / (2 * a) 12= (bsqrr(d)) / (2*a) else Scomplex roots rl=b/(2*al i1 = gqy.Also nodc. eLse ?ry agaln .7913 >> quaCroots (0.the structures are.  v1 .r highrightedby the shading.4 NESTING AND INDENTATION % Z i r aF a 65 12 = r. ! . tr _\ 4 { + 5[st "L x a.how "modular. VT Lf €. underlying logical structurecrear. . 5.5.3.1 5t '$8 Z] X+ .2087 4. 1 ) 0. [h .o. Here rs a command window session illustratinghow the function f"rfo.1.2000 = 0.1) 17 = 0. ^lu gl*:r.1) 0. ) Sguadratic formula d=b * c.8660 ml zI zl Llz zl' + .f a  end end o.
which provide a handy meansto define simple userdefinedfunctionswithout developinga fullblown Mfile. example. thesefunctions In are given a special name:function. an anonymous functioncan includevariables that exist in the workspacewhere it is created.functictns. \ Asidefiom the variables its argument in list.5 PASSING FUNCTIONS M.harrdle .L l l u s u >> >> >> ans a >> b 4. For >> fl@(x'Y) x^2 + Y^2.FIIES TO Much of the remainderof the book involves developingfunctionsto numericallyevaluate other functions. = 2. Before describinghow they work. 3.lh T l 5[st Y jl'€.1 Anonymous Functions Anonyntous .5.functionsallow you to createa simple function without creating an Mfile. they can be usedjust as other functions: >> f1(3.' f2 (3) = 36 Note that if subsequently enter new valuesfor a and b. Try again 3.4) ans. the anonymousfunction we doesnot chanse: f 2 (3 ) ans ffiar qAb zf Ll t zl & ''l \ c. They can be definedwithin the comrnand window with the following syntax: ihandLe = @(argJist) expressro. Once thesefunctionsare defined in the commandwindow.For example. the parlanceof MATLAB. we will first introduceanonymous functions. 0 ) ??? Error using ==> quadroots TriviaL solution. 0.argTist = a comma separated of input arguments be passed the function.r where f.Although a customizedfunction could be developedfor every new equation we analyzed.the functionhandleyou can use to invoke the function. we could createan a n o n y l n o ufs n c t i o n / t l ) : .and express l on = list to to any singlevalid MATLAB expression.66 j PROGRAMMING WITHMATLAB >> quadroots (0.=.eJ 5t 3$8 3l x+ . . betteralternative to designa genericfunction and passthe particular a is equationwe wish to analyzeas an argument. f2=@ (x) t t a*x^b.
2.MATLAB helpcan be consulted learnmoreabouttheiruseand limitations.5 PASSING TO 67 Thus. with the result >> ans f2 (3) 27 It shouldbe notedthat prior to MATLAB 7.5.2 FunctionFunctions Function functions are functionsthat operateon other functionswhich are passed it as to input arguments.will havemanyoccasions useMATLAB's this to builrin functionfunctions. 'x'. in the following example.For example."I LJ ' zl+  1t _l l 5[ s\  "L z. to 3.havthe ing changed to 3. B1*A. and so we thoughtit would be helpfulto mentionthem.FUNCTIONS M. will also be developing As we our own.J <. We can then generate plot from / : 0 to l2 as a >> fplot (vel. ffi= . 3. anonymous the functiondeveloped above. The functionthat is passed the functionfunctionis ref'erred as the to to passed. a >> f2=@x a*x^b. Although they arebeing phased in favor of anonymous out function.function.For this case./681) *t) .f un is the passed automatically analyzes functionand decides the how many valuesto use so that the plot will exhibitall the function'sf'eatures.25) *Lanh(sclrt9.FILES 3. This functionis "smart" in that it [xmin xmax].The f unctioncan be created bungee with an anonymous function: . . [0 f2 I ) The resultis displayed Fig. which plots the graphs A A of functions.'y'). [h x T .B1*6il.f 1. Here is an exampleof how fplot can be usedto plot the velocityof the freefalling jumper.wc. vrl 'Jrrj ( sqrt (9.25. in Notethatin theremainderof book. the function handleholds a snapshot the function at the time it was created.91 5l .somereaders might be usingearlierversions. inline functionspertbrmed the same firnctions. could be written as >> fl=rnline:('x^2 + y^2'.we must recreate function.I /0.If we of want the variablesto take on new values. role as anonymous Forexample.lins) where fun is the tirnctionbeing plotted between the raxislimits specified 7 rms = by function. sirnpleexampleis the builtin function fp1oL. simplerepresentation its syntaxis of fplot\iun.r\€ l Z] X+ I .
2) showsthat this resultis a reasonable estimate of the curve's average heieht.2 A ploio{velocily versus generoted thefplor funclion trrne wilh E X A M P L E2 3. 1.) whereg:9.25 / 68./0 >> mean(v) *tanh (sqrt (9. 1 ) 36. 81"0. 81"68.68 PROGRAMMING WITHMATLAB FIGURE 3. lh l 4 eJ 5t I Z] X+ .ar "L x j. lllustrate usefbr thebungee its overtherange f t o mr : 0 t o l 2 s : u(r): ffo"n(rH.81. ffir qAb z[ Ll 2 zl ?.m:68 value of the function can be comnutedwith standardMATLAB Solution. 3.1+ t"l  5[ st r. nF B u i l d i n o n d l m p l e m e n t i o g u n c t i oF u n c t i o n g n Problem Stqtement. >> v=sqrf (9.0870 lnspectionof a plot of the function (Fig. I2) . The average commands as >> t=linspace (A.Develop Mfilefunction an function determine average to value the jumpervelocity of a function overa range.l'€ r <.
n) :nr hoiqhL % % ? Z % % % z % x = Y = favg favgfuncavg(f input: f = a b n =  . end The main function first uses linspace to generate equally spaced r values across the range.60) ( sqrt ( 9. These values are then passed to a subfunction func in order to generate the corresponding y values. fundvg .aa f rrrnl {r.1 ) *t ) . f (x).25 / 68.ue a range function function lower upper to be of of evaluated range range bound bound of = number intervals volue of f une t ion outpuc: l'aV9 = . lavg u v L ! s Y I uncavg {o.b.o. y = func(x). B1*68.b.^r e" fvg funcavg(o. end compuLes average of function over value d range function f = func(t) fsqrt (9. 12. A. b. "L x l{t 3l x+ 4 l . ffi z$4 zt+ +.25 / 68.5 PASSING FUNCTIONS M.mean(y) . Because haveremovedthe subfunctionf unc.It can be run we fiom the commandwindow as >> vel@ (t ) sqrt ( 9.l:.60) iavg r'7^.25 ) *tanh >> funcavg (vel . B1*4.number of intervals % output: % favg = average value of functron x = linspace(a.4121 Now let's rewrite the Mfile so that rather than being specific to f unc.a.on 9 r r nr"a. Finally.I/ 0.ohL f '.b. n l. The function can be run from the command window as >> funcavS 36. this versionis truly generic. (0.n): % % input: 2 a = lower bound of ranqe % b = upper bound of range % n .vQ) oQe llnspace(a. B1* 68. it evaluates a nonspecific function name f that is passed in as an argument: [*nct. n ) ro f rra. the average value is computed. lh 3$8 . 25 ) *tanh (sqrt (9.I / 0.f\€ .o. favg .n). = mean(Y) .n). 1 ) *t ) .FILES TO We can write an Mfile to perform the samecomputation: funcr o 69 ion ^.9f 5l T a.b. 12 .n) : computes of average over val.3. B1*4.
rch This involves how thedependent variable changes ana\sis.Suppose tn parameters. 0 . fbllowing: >> mb8. and usedit to determine a jumper velocity fbr the casewherethe parameters wereset value of the bungee the average thatwe wantedto analyze samefunction. pararneters.it is possible incorporate in to values. parameters anonymous into As we learned Sec. 180) 6.3001e 017 Doesthis resultmakesense? any valid MATLAB expression. fbrcing functions.2.funcavg can easily be appliedto anothercaseby merely passingit a different function.2.. the gravitational and constant rnass the modelsby perto fbrcing function. must recreate anonyto mous function. time (t) is the independent model.the Here is how both modificationscan be implemented argumentlist (notethe curly brackets). the case.we could retypethc functicln with new valuesfbr each Of dif'ferent but it would be preferable tojust change parameters.but with the L\t :68.I.25.70 PROGRAMMING WITHMATLAB 36 . For example.3.A121 we the However. 36.1. We can seethat f uncavg is now designed evaluate to throughout remainder this text in a numberof the of We will do this on numerous occasions equation solvingto the solutionof differential equations. course.) (g) is the (rr) and dragcoefTicient areparameters. 2*pi. MATLAB offers a better alternativeby adding the term varargin as the function function is invoked In function'slast input argument.6A) *Lanh(sqrt (9.rrA { = "L x zJ Zl F+z I . 0.5.if we want the parameters take on new values.81*m/cd) >> f uncavg (ve1.. rangingtiom nonlinear contexts 3.1.5.we developed functionfunction. observing fbrmingtr sensitivitlt are as the parameters forcingtunctions varied. (clmittingcomnrent..3 Possing Porqmelers modelscan be divided into dependent Recall from Chap. and In Example3. could havedonethe we For example.0. I that the terms in mathematical variables. 0 r 2 ' 7 its To demonstrate genericnature.81*cdz'm) *t) .the velocity(u) is the dependent (c. >> ze1=@(t.it could be usedto determinethe average value of the builtin sin function between0 and 2z as >> funcavg(Gsin.s conciseness): for fbr f uncavg' ffi= =1_h" . addition.every tirne the passed term vararsin{: } shouldbe addedto the end of its within the function function.r:d(). I and c.f uncavg.t'. the variable.5.For the bungeejumper and and independent variable.7.lt is cornmonplace investigate behaviorof sr. ratherthan "wiring" the nurneric functions.) sqrt (9.
ILJ ' ?. 7l When the passed function is defined. If we used an anonymous function.edt SqrL(q. 4.5 t2 .n). b. we could enter >> funcavg(ve1. n24 0. say m : c h a n g i n gt h e u r g u m e n t s : 100 and cd : 0. When all these changes have been made. 0.Bl*m edr*Lanh(sqrr_r9. y = f(x. given an initial conditionfor time and velocity. this can be done as in .f2. varargin) x . n. the actual parameters should be added at the end of the argument list.we would employ small steps.t :8UaIm Recall that. 64.1 and c1 .the problem involved iteratively solving the formula. 60. 0.an algorithm to solve the problem would be basedon a loop. Solution. In this section. U i + l: u 1 * du' At AI ) e v)n its ed Now also remember that to attain good accuracy.25) ans = 36 . This involves obtaininga solution of du cd .28) 38.we will use MATLAB to solve the freefalling bungee jumper problem we posedat the beginningof this chapter. 1. We would thereforeneedto apply the iterative equation 24 timesthat is.6 CASE STUDY function favg = funcavg (f.0.linspace(a.12. Consequently. Ar21 An altemative case.5 s.Bl"cd ml*Lr.28.varargin{:}).tr+ ffi q b {e[ { st r "L x 4 3$ 8. vol=t?(r. 68.Therefore. f avg = mean(Y) . a. analyzing different parameters becomes easy. we would probably want to apply the formula repeatedlyto step out from our initial time to attain the value at the final time. 100. could be rapidly generated by merely >> funcavg(ve1.m.b. Suppose that we started the computation at / = 0 and wanted to predict velocity at t : 12 s using a time stepof A/ : 0. To implement the case where m : 68.25.9345 BUNGEE VELOCITY JUMPER ) t I 1 Bockground.3. Z] X+ . 0.
We can then try a much smaller value of dt to obtain a more accurate numerical result: > > v e .1) end x v''2..6i. while . end Varnd = V. break loop can be substituted place of the a in (notethatwe haveomitted comments conciseness): shaded area the for ffi= zt+ . o c . the ratio is an integer). t. v1) [.' end f unct ion d. .5 .(A. Lf .6175 (Example 3. Becausethis result is exact (i. particIn ular.ue of dependent vari.uler uret]rod soluLion of bungerj u m p e ] r z eL o c i t y rnpuL: dL ./ (dt. computation To cover such cases.y( 0 .f \ 4F+ = \'r?+. 0 0 1 .al Lime (s) Lt = f.Bi Althoughthis functionis certainly it simpleto program.Here'san Mfile to do this including a subfunction defining the differential equation: f uncLion vend = velocityl % % Z Z % % % Z % t % L 1.oci Lyl (0.72 PROGRAMMING WITHMATLAB continued where n : the number of iterations of the loop. .deriz (v) dv = 9. vi ) vel. is not foolproof.ocityl: Iiuler solutj.' n = Far (tf i  1.BL .on for bungee velocity vend = velocLtyl (dt.we can usea for loop as the basisfor the algorithm. This function can be invoked from the command window with the result: >> vel.e. v : v + dvdt * dt. iL v a t t f = Li. t = t + dt..9259 Note that the true value obtained from the analytical solution is 50.in. = Vl. 0 . rf . dvdt = deriv(v) . ti.he intervalis not evenlydivisibleby thetime step.rble outDut: (n/s) v e n d = v e l .i. o c r i .it will not work if '.l)) dns = 50.1).n ti) / dL.25 / 68. 1?.0 . r .rl time (s) (rn/s) vi = initial val. O ) 1 50. 1 2 .= t.rme step (s) ti = initi..
9. We create this dummy variable so that our routine does not change the given value of dt if and when we shortenthe time step. tf .  t.834r1 We should note that the algorithm is still not foolproof. 0 ) 5A. By doing this. 1t r h. break. end if t >end vend = v. we do nothing. t1.velociLy2 (0.(0. we usea singleline if structureto test whether adding t + dt will take us beyondthe end of the interval. . 0 .. l : 1. A) ci: 50 .3.81."I LJ ' b ffii I ^l \ Z] X+ x <. For example. vi ) while (1) if t + dt > tf. we can use a dt that is not evenly divisible into tf :'. end tf. the user could havemistakenlyenteredast3psizegreaterthanttrecalculationinterval(e. Notice that before entering the loop.g. If so.9 2\9 Further.the loop will terminatebecause the condition t >= tf will test true.ti = 5 and dt = 2 0). vend = velociLy2 (dt. dvdt = deriv(v) v=v+dvdt*h. we would shortenup the intervalthat is. h = tf . As soon as we enterthe while loop. tf.L) end * v""2. If we run this new version. we guarantee that the last stepfalls exactly on t f . 12. we set the variablesteph to the intervalremaining:tf * t. Thus.If not (which would usually be the caseat first). ' > v e l o c i L y 2 ( 0 . A. 5 .6 CASE STUDY 73 continued function t = ti.\ ^ f . After we implementthis final step. end dv f unct ion clv = deriv (v ) . e ffi . ] .25 / 68.the result will be the sameas for the versionbasedon the for structure: . we assignthe value of the time step dt to another variable h. 35.We do this in anticipationthat we might need to use the original value of dt somewhere else in the event that this code were integrated within a larger program. [h ^r sJ 5t 3$& . you might want to include error traps in your code to catch such errors and then allow the user to correct the mistake.
Su money P and agree to interest rate of i.: . 1 3. PROB 3.1 . I l .r x2 cosr: I  t 2 COS.3 Economic formulr payments for loans. od. y j ) i{ cnrl Yenrl t. { l * y . break.0i. m o r . y = yi.* 11.:l 5t 3$ & .r .t "t .an future worth is to be ca a table with headingsa gramforP:$100. the differentialequation : l0e0 r'. l . l t i.00( 3. (1) whiIe j 1' t + c]t. r < . l. That is.rrd .. r l . L .f .2 \ / 6 f . o d e s i r n p ( d y d r .'= = Y' tf. Therefore. l l .' t i o ny e n d = ..lI ll . ! .We can use oclesimp to obtainthe sameresultnumericallyas in '':..y: lQ al t :0.1 The cosine funct infinite series: cos"r: I  t i.y hasthe anaMfile.4'l Write an Mfile that wi vestment for each year function should includt rate i (as a decimal). i J { . Lf.t . compute and d true . ) . h = dt.se q . r . For example.(.. .' . tf L.ti.\ ( . we should recognize that the foregoing code is not generic. r:nd Create an Mfile to ir putes and displays thr series is added. l : l .employ up to and including e xto 14r.. : up to the order term o ceding.0653. C t . 0 (y ( ) 0. a r . l e : r 1 p ( c 1 v c 1 l . .2 An amountof mon interest is compounde worth F yielded at an determined from the fo F:P(l*i)" b. h =./ + dyclL(1) * I'ii f ts h.\r (1+i).*ufleqqte zr+ ffi  x a. l l )  "/oeffOr 'r(l92b'r We could then analyzea different function without having to go in and modify the dy/dt : 0. t. we have designed it to solve the specific problem of the velocity of the bungee jumper. The ment A is AP "' i/  a.74 PROGRAMMI WITHMATLAB NG continued As a final note.f : I  Notice how we have strippedout the parts of the algorithm that were specific to the bungeeexample(including the subfunctiondefining the differentialequation)while keepWe ing the essential features the solutiontechnique.:ii nrr.if . .th r zl F+ jl€ . can thenusethis routineto solvethe of jumper example. In o sequencethe valuesfc 'l : COS. t. ) ' :(. A more generic version can be developed as f L r n c .by specifyingthe differentialequationwith an anonymous funcbungee tion and passingits function handleto odesimp to generate solution the .the solutionat t : 5 would be y(5) : l0e*0 r('5) lytical solutionjy 6. 1 r ) As a test case. L ) * u " " .. a ( v ) .
: day ol' the pcak tempcrature (=205 d). FL 1l : 0 to 59)and (b) JLrlyAugusttcnrpcraturc Boston.thc totrl volLrncol' in liom deternrined thc lbllowing fbrmula: includcs fillcclconicalpart and thc partiallytillcd thc liqLrid F=P(l+i)" c y l i n d r i c ap a r t .5 Figurc P3. and tn"^r. In.000and A. I and the numbcrof yearsn firr which the worthis to be calculatcd.r :I : cosr I cos.PROBLEMS 75 PROBtEMS we Dre 3.error ^ 1007. Run the progtamfor : $100. in the conicalpart.1 The cosinefunction can be evaluatedby the following rnfinite series: x2 . r : I . includethe initial investment thc interest rate (asa decinral). : 0.senesapproxrmatlon true ) the [nc 221 231 52 t0 6 la7 : T. Boston. MA T. a n d5 a n dd i s p l a y h er e s u l t s s a t a b l ew i t h h e a d t a ings and columnsfbr n and A.t4 c o s .. <Ah 1 st 4 fi:l  sp 5t 3$8 .Tlrc luturc' basc. 2 .5 FIGURE W ZN 4Y NT ffiE\ zJLJt & +.06.5 3.I.066)."nn) cos((. 4 . 3.l P3.Supposethat you borrow an amountof for money and agreeto repay it in n annrralpaymentsat an P inlerest ol l. Tcst it As a testcase. an interestrateof 6. up to thc tcrm lirr (a) JanuaryFcbruary Miarni. compute and display in is sequence valuestbr the C O s .6 229 : For eacholthc preupto thc orderterm ol'youl choosing.employ the prograrnto corllputecos(L5) fbr in up to and including eight tcnrsthat is. Paramcters someU. In other words. r'* f141.s: thc peaktcmpelaturc.. : thc frequencyof thc annualvariation o (:2n1365). in MA (t: 180to 242). y the ) anal5t Dcvelop an Mfile that cornputesthe averagctcmperature hetrvcentwo claysol' thc year {irr a particularcity."^n * (Q"ar.r: I Write an Mfile to compLrte Test it with P : $55.000. FI Yumo.S. 3 .(/ .lp"ol)) : where Z. AZ B i s m o r cN . computeand display the perccntrelativeerror as ceding..and n : 7 years.thc is at l worthF yielded at an intercsl ratc I aficr n periodsrnay be v o l u m ei s s i m p l yt h e c o n i c a v o l u m eo l ' l i q u i d .4 The averagedaily temperature for an area can be approxirnaled the followingfunction: by 7 : 4. +x4 4l the "Y City AAiomi. shows a cylindrical tank with a conical wherc 3..6% (l : 0.2 An amount rnoneyP is investcd an account ol in inlerest conrpoundcd thc cnd ol'thc pcriod.u""u the averageannual temperaturc. lrue . The tblmula to computc the annual payrate ment is A tp iil + l)" (l+i). l Usc dccisional structures writc an Mf ile to compule to Write Mfilc that will calculate futurcworth of an inan the the tank'svolumc as a flnction ol'given valucsof'R and 11.on ("C) T"""r ("C) 283 336 221 )7. kD WA Seoiile.lf'the liquid levcl is quitc low. P t 3. The output shouldconsistof future atable rvithhcadings and columnsfirr n and F. towns are listcd here: fbr 2l .Conlpu(eresultsftrr l : 1 .3Economictbrmulas are available to computeannual payments loans. The input t0 the lbr the Desisnthc tunctionso thatit rcturns volurncfirr all cascs function should P. vestment each ycar lrorn I through rr.) t +   Create Mfile to irnplementthis folrrrula so that it coman putes displaysthe values of cos x as each term in the and series added.l f t h e l i q u i d level is rnidrangc the cylindricalpart.
11 grade liom 0 to 100 and returnsa lettcr grade according to the schcmc: Criterio Write an Mfile that cor channels.y) on the basis of polar coordinatcs reverseprocess not.B: width ing data is available fo . y) in Cartesian tion of r and_r'.. Dcvelop an Mlllc function that is passeda numenc 3.rJ 1T T rl2 n /2 0 where U: velocity(m cocfficient.6). The nates(r.t'+ +Y jl€ . and().sJ 5t I <.5 I t5 2 t5 45 t< Note that the tank's radiusis R.The radiuscan be cornputcd is by the fbllowing lbrmula: <0 <0 <0 :0 :0 :0 >0 <0 :0 >0 <0 :0 ton l". However. BO numeric rode < /0 c n u m e r i cg r o d e < 6 0 + 7 5 ( x. xJr It is relatively straightfbrwardto computeCartesian coordi(r.U s i n g s i n g along the heamcan be e 0 A B C D F 90 < BO 1 /O .6.10 A simply suppo F i g .76 PROGRAMMING WITHMATLAB where the depth is less than 3R. d) in polarcoordinates.:l*?+v./.e..0 OO 00t 0. 3. Test it summarizes possibilities: the with the lbllowine data: xy0 R d 1. P3. .7 Dcvelop an Mfilcr to detcrmine polar coordinates descrihedin Prob.f 4+4nvI \ ?.Testthe prograrn the cases fbr outlined in Prob.y. [h  3l x+ . P 3 . d > 3R. 60 < numeric rode < l0C o numeric rode < 90 g q numerlc rode . 1 0 . The horizontal and vertical distances(r. Have the Mfile di computedvelocityin ta colurnn. Return an error message The difliculty arisesfor the other cases.0 00( as 3. sing the follows: (xtt)":lt*o l0 to 3.. r. 3. then a sirnplelbrmula can bc uscd to compute 9: rl : tan I (I) \_r.6. The following table ("Overtop") iI you overtopthe tankthat is.035 0 020 o 0t5 0 030 o 422 0.9 Manning'scquationcan be r.passvectorsof ir and). rathcr lhan designingthe function to cvaluatea single casg.6): Write a wellstructured Mfile to calculater and 0 as a funcTest Expressthe final resultsfor d in dcgrees.3.rsed computethe velocity of water in a rectangular open channel: FIGURE P3. If the coorilinates within the first and lburth coordilic nates(i.Includeheadin 3.zxl r * loo lr.7 By dcfinition. Havethe f unctiondisplaythe results a tablewith columns as forx. Thc radiusand angle(r./ 0 0 I 0.6 Jsr BH \2' il \.8+2H/ Develop Mfilethat< an distance along beam the the beam.sosirnple.6 Two distancesare required to specify the location of a point relative to an origin in twodimensionalspace (Fig. your programby evaluating fbllowingcases: thc coordinates. . Enter thesev umn represents param a nel. ffi= =n .x > 0).
Write Mfilcthatconrputcs vclocitylirl cachol'thcse an thc Enter channels.rrn Test your program distance the beam.At '\ 23tt.J I 3l x+ .PROBLEMS 77 20 kips/ft :c F I G U RP 3 ..t I t r n r u ' l follows: Rcpeatthe loop until a is lessthan or cqual to a spccified (. tc..12 Dcvclop a vectorized vclsion ol'thc lbllowing codc: 1.r > a both thc rcsult value. thc Using singularityf'unctions. DevelopMfilc thatcrcatcs plot oldisplacemenl a an 21.10. rtl" when. where vclocity V horizontal cylinder U: S: channcl slope. .Use ploper indentation that thc structnrcis clcar._./r Vltcor'[L \r 0 000t 0 0002 0 0010 0 000/ 0 0003 hr  / rr l htr2rlt h. thesevaluesinto a matrix whcrc cach column reprcscnts a prramctcrand cach row rcprcscnts chana nel.1 Bydetinition."I LJ ' zl'+ ffi A b q st Z} z.. width (m).. displacerrncnt . L ( L) ts1.ll Thc "dividc and average" method.:ildrL=U. I O E (rn/s). P3. rri 5.t : 0 at the leli end ol' plc.r.\t'ltl . . lirr approximating square thc 3..25 t * ? 5 ( r 7 ) r+ clrch\tcp c\tinrirtc crrrrrin y.nclilfl. singularitylunctioncan be cxprcsscd as thc " .For the latter that For examversus casc. . oldtimcmcthod an lncludc hcadings thc tablcto lahcl thc colurnns. on column.displaythe resultas an imaginarynumber. uthebeam.Il/ I 0035 0020 00r5 0030 0422 r0 B 20 24 t5 Dcvclopan Mfllc to cl'cilte plot ol'volume versus a depth.roughness 3.r + ulx along bcamcan bc cxprcsscdhy thc ccpration: the 2 s t5 ' Writc a wcllstructurcd Ml'ilc lunction bascd on a :'*l .Designyour programso that it returns .rurirpprtrrillluli()n thc irs 6 t.2 rn and L: 5m.9 f b r c : I x l 0 r . a n d. Tcst thc program lorr . _. _ J I tt and the errur.t1L. (.1l Thcvolumc of liquidin a hollow n liquid r L to o1'the B: coefficient._ tt .r/lr ^ . .. 2 . Makc surc that it can cvaluatethc squareroot when r < a I I o1'nunrbers arc equalto and lcssthanzero.. root of 4 would retr.The tbllow.. root ol'any positivcnumbcra. 4 .. Notc that.ol'radius andlcngth is related thedepth hbv lbr ls: ing is availahle llve channe data n l. 3..beak loop structurco irnplcmcnthis algo57 r so rithur.10 simply supportcdbcant is loadcd as shown in A can bc firrrnulatcd as Fig.r x) lrr. thc square along b y e v a l u a t i n g 0 .r)l'r0)'(r oo t whil. Have Ml'ilcdisplaythc input data along with thc c l r t ( l thc vclocity tabular in firm whcrc velocity is the l'ilth computed 3.. and H : dcpth(rn).
6 so that it can bc passcdthc argumcntsof days in a year.The first line of the function should be set up as function nd = days(mo. L2 . year) 3. 2000.andDecember 31. 5.@ ( v . n) .125.June21.16 Develop an Mfilc function to dctcrmine the elapsed the end ol'Sec. d a : t h e d a y ( l .1 2 ) .78 PROGRAMMING WITHMATLAB w h e r em o : t h e m o n t h ( l .rp) (c<1/m)*v^2. inraldigits:r : 467. /(x) 0.18 Developa lunctionfunctionMflle thatreturns difthe ferencebetweenthc passcdiunction's maximum and minimum value givcn a range of the independentvariable.01 to 0. Tcstit lbr thc lbllowing casc: up as Develop an Mflle function to compute u as a function of /.0.2002. m . The f'irst linc ol' thc function should be set the passed lunction. 467.3 l ) . c : c 1) 9 . A . 2002.15 Developan Mfile lunctioncallcdrounder to rounda numberr to a specifiednumberof dccimal digits.2(t10) 0 0<t<10 10<t<20 20<r<30 / > 30 othcrwise w h e r ei r o : t h e m o n t h( l . In addition.0 .4 ( a ) . 0.n. and at 3. 125. d a : t h e d a y ( l .9587. 2001. a n d year : the year.I. use this lunction to gcneratea plot of u versus/ fbr t5to50.1999.14 Piecewise useful when the refunctions are sometimes variable lationshipbetweena dependent and an independent cannot be adequately represented a single equation. 0.958'7.March 1. le.1 2 ) . l ( 0 : l 0 e . Hint: A nice way to do this combinesthe for and the switch structures. (c) The builtinhumpslunclionliom x : 0 to 3.135. da. January 1999. B L :':' oclesirnp (dvtlt .Test it for l. Z] X+ 3$8 .5. 3. havethe lunctiongenerate plot of the functionfbr a the range. 3. d v c l t . February29.13."1 {t _q ffir st 71sy . tt9. 3.June21.19 Modily the functionfunction odesimp developed 1.2. Testthe prograrnby roundingcachof thc fbllowing to 2 dec. 2004.March l.For by by example.the velocity of a rocket might be described l1t25t ll005/ 50t+2(t20)2 l52oe 0. 3.3 1 ) . 2001. and December31. February 29.Test it for January l. 0.25) >> zt'+ .2000.0 2 s ' s i n ( l ) f r o mt : o t o 6 T . 0. cla.17 Develop an Mfile function to determinc the elapsed days in a year.Test it for the following cases: function xr = rounder(x. Then. The first line of the f'unction shouldbe setup as f uncrt i. a n d leap : (0 for nonleap year and I for leap year).(b) : es'sin( l/x) liom r : 0.2004.t ^l +.rn nd = days (mo.
1li. YOU'VE GOT A PROBLEM jumper. . Learning Learninghow errorcstimates be usedto decidewhen to terminate iterrti'. Knowing how to uscthe Taylorseliesto estimate truncation errors.1 .. ""j 'rt6. Speciticobjectives topicscovered and are :loped at mentsof ' ' ' ' r) ' ' ' t ' Understanding distinction the between and precision. 2 do this elapsed J be set .+t . !]=!!dr Lt u(ti+r)u(ri) t. how backward. you developed numerical a model for the velocity of a bungee you had to approximate derivativeof velocity solve the problemwith a computer. accuracy how to quantityerror. the difrd miniiable. t. Recognizing that truncation errorsoccurwhenexactmathematical fbrmulations ilrc represcnlcd approx by irnat ions.. I.a n d :st it for Iune 1. Understanding lloatingpoint why numbers have[irnitson their rangeand precision."lii: i Roundoff Truncotion Errors ond ll).1ffi : i irl:r 1. and ccntered finitedifTerence approximations first and seconcl of derivatives.."'.ii i 1 ) . the with a llnite diflerence.ti 79 . R e c o g n i z i ntg a te l ' f i r r tls r n i n i m i z e" r u n c r r t i e r r o r s a ns o m e l i m eis c r c a s e h o l on c n roundofT errors.2001. In ction for CHAPTER OBJECTIVES The primaryobjective this chapter to acquaint you with the major sources of is of errorsinvolvedin nurnerical rnethods. and nry 29.'e can an calculation.. Understanding roundofferrorsoccllrbccause how digitalcornputers havea limitedahility to represent nunrbers. Understanding to write lbrwarcl.To n Chap.
Beit is the cause is a digitaldevice. despite tact that the rnodeldeveloped the neverin practice predictthejumper'sfall..the computeryou use to obtain the solutionis also an imperfecttool. dealingwith the two reviewedin this section.2 and4. alsodescribe roundolT. the resultingsolution is not exactthat is.la.4. l can be thoughtof as the predictions a numer(alsocalledbirr.80 R O U N D O FA N D T R U N C A T I OE R R O R S F N Thus.lc are as because more tightly grouped than in Fig.I ERRORS constantly find themselves havingto arccomplish objectives based i Engineers scientists and goal. to (i. thesedeviations systematically If high or low.Prct'1. althoughthe shotsin Fig.althoughFig.4. I e a to w L( rh . Although liom Newton'ssecond law tained. on Therefore.1.centered thc bull'seye). of The bulletholeson eachtargetin Fig. it is rarely if ever atperf'ection a laudable is on uncertain information.Imltrecision(alsocalledurrc:ertaintl). I I i I 4.\'iorr computed or measured valuesagreewith sachother. errors. increase Finally. Att'unrc'. In addition. rzrndomly and might needto devclopa iltheyare distributed tightly grouped prediction.However. also i n t o t h ea n a l y s i s .Inaccurucy ical technique.e.and datauncertaintv. you to sonleapproaches concepts and that engineers and certainty'? This chapterintroduces with this dilemma. is an excellent it cxactly approxirnation. computer limitedin its ability to represent magnitudes the precision numbers. to and with the quantification mizaticln these errors. lD and on the other hand. Consequently. scientists to deal use 4. 4.3. would would resultin desuchas windsand slightvariations air rcsistance in A varietyof factors prediction. whereas bull'seye the the defirrecl systematic deviationfrom the truth. thc two cases equallybiased arc theyate of both cerrtered the upperleft quadrant thc target.nation and Your problemis: How do you deal with suchunsulting rnodelpredictionto be uncertain. your digital computercauseyour lrboth your mathenlatical So approxir. it has error. 4. machine the itselfyieldsresults and of thatcontainenor.we briefly discuss crrorsnot duce truncation error sometinres Thessincludeblundcrs.1 Accurocy ond Precision with The errorsassociated with both calculations measuremenls be characterized can arrd and precision. quantification.Thus. (due error:rclundof'l'crror to computer and majorfirrmsof numerical approximations) trunWe how strategies reto cationerlor (dueto mathe nratical approximations). graphically Theseconcepts can be illustrated using an analogyfiom targetpractice. rniniThis chaptercovcrs basictopicsrclated tlreidentification.For example. thenwe viationsftom the are new modcl.v rcl'ers how closelya corlputedor meato regardto theiraccuracy ref'ers how closelyindividuitl to suredvalueagrees with thetruevalue. latteris more precise on the because d are eqLrally accurate the shotsaretightly grouped. with the nurnerical nrethods rnodel directlyconnected themselves.i)is represents truth. Ccneralinformation concerned ol'erroris of lhis is fbllowed by Sections 1. rnightbe considered negligible thenthe deviations andthe aroundthe Numericalapproxirlations introduce similar discrepancies model deemedadequate.ref'ers the magnitudeo1'thescatter.
our rerch unersand o 0) a s based )ver atrnd law :r'sfall. errorof a centimeter muchrnoresignificant For an is .or true.l ) Eq.u p p r o x i m a t i o n (4. In tlris book. example. t o 4. (b ( o ) i n o c c u r o t c n d r m p r e c i s e . l). (.l b and because Numerical errorsariscfrom the useof approximations rL'present to exactmathematical opand quantities. o ("o r\umerrcalmethocls snoulcl sufiicrenrly oe accura("e unorused meet irc requtreor particular problem.Note that the true error is of commonly expressed an absolutevalue and referredto as the absoLute as error. rlodel o) FIGURE 4.2 Error Definitions ed with or rneamputed rractice. the The subscript is included dest to ignatethat this is the "true" error. l o c c u r o t e n c ii n r p r e c i s e c Ji n c c c u r o t o n d p r e c i s e .1.21 whereCr is usedto designate exactvalueof theerror.the relationship For erations between exact.Beritudes n errol'. It in delhen we I tightly and the :pancies r c lm i n i 'error is the two s to reors not .d { r J ) ( ' L r o ' ee r '. sucherrors.result the and thc approximation be lbrmulated carr as True value: approximation error f ( 4 .s) 4. l.I ERRORS 8r accuracy Increasing ol. i n t as 6 r : t r u ev a l u e. numeris bia.4.They also shouldbe precise nrentsof a enoughfbr adequate design.(4.This is in contrastto other cases.I tg illust o o o from Anexomple morksmonship r o t i n h e c o n c e p l s l o c c u r o c y n d p r e c i s n .I c' are they are rtainty). we find that the numericalerror is equal to the discrepancy By rearranging b e t w e e nh el r u t hi t n dt h eu p p r o x i m a l i o n . e o e n . J p e c : s e . wherean "approximate"estimate the enor must be employed. A shortcoming this definitionis that it takesno accrount the order of magnitudeof of of thevalueunderexarnination.as describedshortly. we will use thc collectiveterm errur to represent both the inaccuracy and i m p l c c i s i o nl l 'o u r p r e d i ci t n s .
82
ERRORS ROUNDOFF AND TRUNCATION
of if we are measuringa rivet than a bridge. One way to accountfor the magnitudes the quantities is being evaluated to normalizethe error to the true value,as irTrue fractionalrelativeerror : true value  approxlmatlon true value
4.
wl be pe to
express as it The relativeerrorcan alsobe multipliedbv 100%,to true value  approximation
cl 
true value
1004/t
Th to aln
where e, designates true percentrelativeerror. the the of that you havethe taskof measuring lengths a bridgeand For example, suppose and If rivet and come up with 9999 and 9 cm, respectively. the true valuesare 10.000 a relativeerrors their percent is 10 cm, respectively, enor in both cases I cm. However. the Thus,although bothntearespectively. usingEq. (4.3) as0.0I 7oand l0olo, canbe computed We error of 1 cm, the relativeerrorfor the rivet is much greater. surcments havean absolute job the probablyconcludethat we have done an adequate of measuring bridge. wor.rlcl to for sornetl.ring be desired. whereas estimate the rive( leaves our with a 1 to signifythatthe that for Eqs.(4.2)and (4.3),t and e aresubscripted Notice of the true value.For the exanrple the rivet and the bridge,we wercproerror is based on such infbrrnation rarcly available. is vided with this value.Howcvcr,in actualsituations that nrethods, truevaluewill only be knownwlrenwe dealwith functions the For nurnerical the will typicallybc the casewhen we investigate theoSuch can be solvedanalytically. apHowever,in realworld fbr of techniqLre simplesysterns. reticalbehavi<,rr a particular an plications, will obviouslynot know the true answcra priori. For thesesiturtions. we of usirrg bestavailable the estimate thetrue valuethat is the alternative to norrnalize error i s , t o t h ea p p r o x i m a t i oins e l f , s i n t a error' approxrmate l00a/( approximation value.Note to a wherethe subscript signifiesthat the error is norrnalized an approximate the applications, (4.2)cannotbe usedto !:alculate errttrtermin Eq. alsothatfbr realworld rnethods to determine is of of the nunterator Eq. (4.4).One of the challenges numerical regardiug true value.For example, certain the crror estimates the absence knowledge in of In a approximamethodsuseiteratiotr computcanswers. suchcases, present to nunrerical repeatThis process performed is tion is rnadeon the basisof a previousapproximation. to compute(hopefully)betterand betterapproximations. edly,or iteratively, successively betweenthe previousandpreas the For suchcases. error is often estimated the dil'lerence to relative error is determined accorcling Thus,percent sentapproximations. present approximation approximation previous l\OC/t present approxrmatlon ( 45 )
pro
can
EXAMPLE 4.1
Err
Pro ries
Thu esti
: '
^0.5 c .1
with term fied t
JOIU
resu
I
Thui tima
or fc
on chapters. fbr errors is elaborated in subsequent This and other approaches expressing The signsof Eqs.(4.2) through(4.5) may be either positiveor negative.lf the approx' is thanthecurapproximation greater thanthe truevalue(or theprevious im:rtionis greater the rent approximation), error is negative:if the approximationis lessthan the true value, the may be lessthanzero, Also, for Eqs.(4.3) to (4.5), denonrinator the error is positive.
Thir
4.I ERRORS
which can also lead to a negativeerror. Often, when performing computations.we may not be concemed with the sign of the eror but areinterested whetherthe absolute in valueof the percentrelativeerror is lower than a prespecified tolerunce Theretbre,it is often useful e.. to employ the absolute value of Eq. (4.5). For suchcases, computationis repeated the until Ir,, <€., I (4.6)
This relationship refered Io as a stop1'ting is criterion.If it is satisfied. resultis assumed our to be within the prespecified acceptable level o.. Note that for the remainder this text, we of almostalways employ absolutevalueswhen using relativeerrors. It is alsoconvenientto relatetheseerrorsto the numberof significantfiguresin the approximation. can be shown(Scarborough. It 1966) thatif the followingcriterionis met,we can be assured thatthe resultis correctto at leastrr significant figures. e.,:(0.5 xl02"1%o (4.7 )
EXAMPLE E r r o rE s t i m o t e fso r l t e r o t i v e e t h o d s 4 M
Problem Stotement. In mathematics, functionscan otien be represented infinite seby ries.For exanrple, exponential functioncan be computed the using
e':l+.r+'
'Ihus,
,\'l 2
F:+..
l!
.rl
1
.r" nt
(E4.l.l)
as more terms are addedin sequence, apploxirnation the becomes betterand better a (E4.L l) is calleda Moclourinserit,,s of c'slimate the true valueof e'. Equation a.rput,sion. with the shnplest version. : l, add termsone at a time in orderto estimate Starting e' e" 5.After eachnew term is added,computethe true and approximate percentrelativeerrors with Eqs.(4.3)and (4.5), respectively. Notethatthe truevalueis e" 5  .648121. . . . Add valueof the approximate termsuntil the absolute en'orestimate falls below a prespecic,, fied errorcriterions.rcontbrmingto threesignil'icant figures. S o l u t i o n . F i r s t , E q .( . 4 . 1 ) c a n b e e m p l o y e d t o d e t e r m i n e t h e e r r o r c r i t e r i o n t h a t e n s u r e s a resultthat is correctto at leastthreesignificant figures: x €. : (0.5 102 t)"/n:0.05a/,,
Thus,we will add termsto the series until e,,falls belowthis level. The first cstimate simplyequalto Eq. (E4. . l) with a singleterm.Thus,the first esis t i r t i a t e i s e q uta lll . T h c s e c o n d e s t i l n a l . e i s t h e n g e n e l a t e d b y a d d i n g t h e s e c o n d t e r m a s i n c et: l*x
or fbr,r : 0.5 e o 5 I * 0 . 5 : 1.5
This represents true percent a relative enor of tEq. (a.3)l
^_l .' _ 
r . 6 " 1 8 72 1 . . s t I =9.02(/t I x 1001, t.64812t I
84
ERRORS ROUNDOFF AND TRUNCATION
estimateof the error, as in Equation(4.5) can be usedto determinean approximate
:.1.1.3(Z r., : lj_=_l x 100(/o
I l.) 
t15ll
I b
S
the by valueof e,, we would continue computation Because is not lessthanthe required en The processis continued adding anotherterm,x212r.,and repeatingthe error calculations. as until le,, < e,. The entire computationcan be summarized l
Ierms
n
D,
Result 5 625 645833333 648437544 6486979)7 393 942 I44 a t75 a at72 000142 3 33 769 I27 0 t58 00t58
I 2 3 4 5 6
nl di nl tu
TI
aft
and the errorfalls below e, : 0.05olo, the Thus, after six termsare includcd, approxirnate figures, the is Howcver,noticethat,ratherthan threesignificant computation terminated. tbr both Eqs.(4.5) and(4.1) areconresultis accurate five! This is because, this case, to Although, servative. That is, they ensure that the resultis at leastas good as they specify. it this is not alwaysthe casclbr Eq. (4.5), is true mostof thc time.
w( ou 0c ele or po (0
4.2
ROUNDOFF ERRORS
somequantities exactly. digital computerscannotrcpresent Rountlo.ff'errur,s arisebecause solvingbecause they canleadto to and They are irnportant engineering scicntificproblern going unstable results. ccrtaincases, In they can actuallylcad to a calculation erroneous are results. Suchcalculations saidto be illconditioned, and yiefdingobviouslyerroneous that to discrepancies aredifTicult detect. Worsestill, thcy can lcad to subtler involvedin numerical calculations: Thcreare two major facets nrundoll'crrors of 1. 2. numben. limits on theirability to represent Digital computers havesizeandprecision This canremanipulations highly sensitive roundofT are to errors. Certainnumerical as sult liorn both rnathematical considerations well as fiorn the way in which computi()ns. arithmetic operilt e'rsperl'orm
Int res put the mai sen
The inF
repr mair
4.2.1 Computer Number Representotion
Numerical roundofTerrorsare directly relatedto the mannerin which numbersarestoled is in a computer.The fundamentalunit wherebyinfbrmation is represented calleda word, or bits. Numbers are typically This is an entity that consistscf a string of binary digil.l, we how this is accomplished, must first revierry storedin one or more words.To understand somematerialrelated numbersystems. to
FIGI Thet mog
lll
rT
IL
I Sign
4.2 ROUNDOFF ERRORS
85
Anumber systemis merely a conventionfor representing quantities. Because have we l0 fingers and l0 toes,the numbersystemthat we are most familiar with is the decimal,or base10,number system.A baseis the number used as the reference constructingthe fbr s y s t e m . T h e b a s e  l 0 s y s t e m u s els td ie i t s  O , 1 , 2 , 3 , 4 , 5 , 6 , 1 , 8 , a n d 9  t o r e p r e s e n t 0h g numbers.By themselves, thesedigits are satisfactory countingfrom 0 to 9. for For largerquantities, combinations these of basicdigits are used,with the positionor place vuluespecifying magnitude. the The rightmost digit in a whole numberrepresents a numberfrom 0 to 9. The seconddigit fiom the right represents multiple of 10.The third a digit from the right represents multipleof 100and so on. For example,if we have the a number8642.9, thenwe haveeightgroupsof 1000,six groupsof 100,four groupsof 10, two groupsof l, and nine groupsof 0. l, or (8x 103)+(6x 102)+(,1 l0r)+(2x x 100)+(9x l0 ry:9642.9
This type of representation calledpositionol notatktn. is Now, because decimalsystem so familiar,it is not commonlyrealized the is thal there Forexample, humanbeingshappened havceightfingersand toeswe arealternatives. if to would undoubtedly havedeveloped ot'tul,t>rba.se8, an representation. the samesense, In our fiiend the computeris like a twofingcrcd animalwho is limited to two stateseither 0 or l. This relates the fact that the prirnarylogic unitsof digital computcrs on/off to are electronic components. Hence,numberson the computerare rcpresented with a birtary, system. or ba"^e2, Just as with the decirnalsystcm,quantities can be represented using p o s i t i o n an o t a t i o n F o r e x a m p l c , h c b i n a r y n u r n b e rI 0 l . l i s e q u i v a l e ntto ( l x 2 r ) + l . t ( 0 x 2 l ) * ( l x 2 " )+ ( l x 2   t ): 4 + 0 + I + 0 . 5 : 5 . 5 i n t h c d e c i m a ly s t e m . s Integer Representotion. Now that we havereviewed numbers how base10 can be represented binarylorm, it is simplcto conccive in of'how integers represented a cornare on puter.The most straightforwardapproach, callcd thc ,signad nngnitrule metfutd,employs the first bit of a word to indicate sign,with a 0 fbr positivcanda I firr negative. the The remainingbits arc usedto storeths number. Forexample, integervalueof 173is reprethc s e n t e dn b i n a r y s l 0 l 0 l l 0 l : i a ( l 0 l 0 l l 0 l ) , : 2 1+ 2 s+ 2 r + 2 2+ 2 t t : 1 2 8 3 2 + I l + 4 + I : ( 1 7 3 ) , , , + Therefore, binaryequivalent  173wouldbe stored a l6bit computer, depicted the of on as in Fig.4.2. If sucha scheme cmployed, is thereclearlyis a lirnitedrangeof integers that can be represented. Again assurrring l6bit word size,if one bit is usedtbr the sign,the l5 rea mainingbits can represent liom 0 to I I I I I I I I I I I I I I I . The upperlimit can binaryintegers
FIGURE 4.2
T h eb i n o r yr e p r e s e n t o l i o n t h e d e c i m c li n t e g e r l / 3 o n o l 6 b i t c o m p u t e u s i n gt h e s i g n e d of r mognilude ethod m
t
1
0
0
0
0
0
0
01110
Magnitude
1
0
1
0
1
Y
Sign
I
86
ROUNDOFF AND TRUNCATION ERRORS
be converted adecimalinreger, in (1 * 2'o)+(1 x 2'3)+. . . + (l x 2') * (l x 20;= to as 32,161 Note that this valuecan be simply evaluared 2rs  l. Thus,a l6bit computer . as word can storedecimalintegers rangingfrom 32,761 to 32,767 . In addition, becausezero is already defined as 0000000000000000, is redundant it to use the number 1000000000000000 define a "minus zero." Therefore,it is convento 32.768, and the rangeis tionally employedto represent additional an negative nurrrber: 32.768 to 32,761 For an nbit word,the rangewould be from 2" to 2"  l. Thus, frorn . 32bit integerswould rangefrom 2,141.183.648 to +2,141,483,641. Note that,althoughit providesa nice way to illustrateour point, the signedmagnitude method is not actually usedto represent integersfor conventionalcomputers. prefened A approachcalled the 2s complemenl techniquedirectly incorporates sign into the numthe ber's magnituderatherthanproviding a separate to represent plus or minus.Regardless, bit the rangeof numbersis still the sameas for the signedmagnitudemethoddescribed above, The foregoingserves illustratehow all digital computers limited in their capabilto are ity to represent integers. That is, numbersaboveor below the rangecannotbe represented. A moreserious limitationis encountered the storage rnanipulation fractit'rnal in and of quantitiesas described next. FlootingPoint quantities typically represented comRepresentotion. Fractional are in putersusing floatingpoint.format. this approach, In which is very much like scientific notation, numberis expressed the as *sxb" wheres : the significand, b: the baseof the numbersystembeing used,and e : the cxponent. Priorto beingexpressed this forrn,the nurnbelis normaliz.ed movingthedecirnal in by placeoverso thatonly onesignificant digit is to thelefi of thedecirnal point.This is doneso rtlcmoryis not wustedcln storinguseless cornpLlter nonsignificant zeros.For example, a value like 0.00567ti could be represented a wastefulmanneras 0.005671t l0('.Howin x ever,nort.ttalization would yield 5.678,. l0 I which eliminates useless the zeroes. Beforedescribing base2 implementation the usedon computers, will first explore we the fbndamental irnplications suchfloatingpoint of representation. particular, In whatare the ramificationsof the fact that in order to be storedin the cornputer, both the mantissa and the exponent mustbe limited to a finite numberof bits?As in the nextexample, nice a way to do this is within the contextof our moretirmiliarbase decimalworld. l0
tS :p
i"
i
rq
F o
V
e) 0
of
pl TI or
VA
w( ca
EXAMPLE .2 4
l m p l i c o t i o n s f F l o o t i n g  P o i n te p r e s e n t o t i o n o R ProblemStotement. Suppose we hada hypothetical that I0 base computer with a 5digit word size.Assumethatonedigit is usedfbr the sign,two for the exponent, two forthe and mantissa. simplicity, For assume one of theexponent that digitsis usedfirr its sign,leaving a singledigit for its magnitude. Solution. A generalrepresentation the numberfollowing normalizationwcluldbe of s 1 d 1 . dx l O r o ' d ) 2
* Th
fio<
4.2 ROUNDOFF ERRORS
87
)r
nt nis
1S,
w h e r e . r o a n d  sh e s i g n s ,d , : t h e r n a g n i t u d e oh e e x p o n e n t . a d r a n d d , : t h e m a g .t , tf nd nitudeof the significand digits. Now, let's play with this system.First, what is the largestpossiblepositive quantity that can be represented? Clearly,it would correspond both signs being positive and all to possible nragnitude digits setto the largest valuein base10, is. 9: that Largest value: +9.9 x l0+Y possible So the largest numberwould be a little lessthan l0 billion. Althoughthis might seemlike a big nunrber, reallynot that big. For example, it's this computer would be incapableof representing commonlyusedconstant a like Avogadro's number16.022x l0rr.1. possible positivenumberwould be In the samesensc, smallest the '' S m a l l e sv a l u e: * 1 . 0 x l 0 t Again, althoughthis value rnighf seempretty snrall,you could nol use it trl represent a q u a n t i t yi k e P l a n c k ' s o n s t a n(t6 . 6 2 6x l ( )  r r J . s ) . l c Similarnegative values couldalsobe developed. resulting The ranges displayed are in Fig.4.3. Largepositiveand negative numbers that fall outsidethe rangewould causean overflowerrcr. ln a similar sense, very small quantities for thereis a "hole" at zero.and very smallquantities would usuallybe converted zero. to Recognize that the exponent overwhelrningly determines theserangelimitations. For exarlple,if we increase manfissa onedigit. the maxirnum the by valueincreases slightlyto 9.99 x l0'. In contrast, onedigit increase theexponent raises maximurn 90 orders a in the by to of magnitude 9.9 x l0e'r! When it comes precision, to however, situation reversed. the is Whereas signil'icand the playsa minor role in detiningtherange, hasa profound it etl'ect specifying precision. on the illustrated this exanrple This is dramatically fbr wherewe have limited the significand to just as thereis a "hole" at zero,thercarealso"holes"between only 2 digits.As in Fig. 4..1, values. Forexample. simplerational a number with a finitenurnber cligits of like 25 : 0.0l125 would haveto bc storedas 1. x l0 r or 0.03I . Thus.'t rountlofferror is introduced. this I Fclr it case, represcnts relative a errorof 0 . 0 3 1 2 5 0 . 0 3
de ed mrSS,
ve. riled. an
)mtific
. the :imal ne so rle, a {owplore at are ntissa a nice
1 0.03 25
: 0.00t1
FIGURE 4.3 5digit for the eaving
T h e n u m b e r i n es h o w i n gt h e p o s s i b er o n g e s o r r e s p o n d i ntg t h e h y p o t h e t i c o o s e l O l o c b f l o o t i n g o i n ls c h e m e e s c r i b e d n E x o m p l e 2 . p d 4 i Minimum Smallest Maximum
l,,\l
9 . 9x 1 0 s Overflow l{
 1 . 0x 1 0 s
1 . 0x 1 0 s
9 . 9x 1 0 s
be
{Overflow ,Underflow " H o l e " tz e r o a
88
ROUNDOFF AND TRUNCATION ERRORS
0.01
0.980.99 1
1. 1
1.2
FIGURE4.4 A smoll poriion thenumber correspondingthehypotheticol lO flootingpoinl of line to bose n s c s c h e m d e s c r i b eid E x o m p l4 . 2 . I h e n u m b e r s d i c o fv o l u e t h o t o n b e r e p r e s e n t e d e e in e All quonlilies folling the"holes" rn ihese exoctly. other belvreen volues wouldexhibit some
rounootterror.
t'rr
I
(
F T p
While we could storea numberlike 0.03125 exactlyby expanding digits of the the quantities significand, with infinite digits must alwaysbe approximated. example, For a c o m m o n l y u s e d c o n s t a n t s u c h a s ; r ( : 3 . 1 4 1  5 9 . . .h a v e tlo b e r e p r c s e n t e d a x 3 0lr ) )wou cl s l. or 3. l. For this case, representedrelative it a errorof
"I
fl
 3 3 . 1 4 1 5 9 .r
3 .l 4 I . 5 9
b
: 0 . 03 2 1
e/
F Although adding significand digits can improve the approximation, such quantities will alwayshavesomeroundofT errorwhen storedin a computer. Anotherrnoresubtleef'fbct of'f'loatingpoint representation illustrated Fig.4.4. is by Notice how the intervalbetween numbers increases we move befween as ordersof magnitude.Iror nurnbers with an exponent  I (that is, between of 0.1 and I ;, the spacing is 0.01. Once we crossover into the rangefiom I to 10,the spacingincreases 0. l. This to meansthat the roundoflerror a nurnber of will be proportional its rnagnitude. addrto In tion, it nreans that the relativeerrorwill havean upperbound.For tlrisexample. maxthe inrum relativeerror would be 0.05.This value is calledthe nuc'hineepsilon(or machine precision).
p
S
R tl b
u tl tI
As illustrated Exanrple in 1.2,thefact thatboththeexponent and significand finite are means thaf thereareboth rangeand precision limits on lloatingpoint Now, representation. quilntities actuallyrepresented a real computer let us examinehow floatingpoint are in usingbase2 binarynumbers. or First,let's look at normalization. consist Sincebinarynumbers exclusively 0s and of ls, a bonusoccurswhen they are nornralized. That is, the bit to the left clf the binarypoint will alwaysbe onel This meansthat this leadingbit doesnot have to be stored. Hence, nonzero binaryfloatingpoint numbers can be expressed as X(ltf)x2'' partof the significand). example. wenorwheref': thennntisso(i.e.,the fractional For if malized binarynumbell l 0 l. l, theresult the wouldbe l. l0 l l x (2)'r or ( l + 0. l0 l l ) x 23.
T
P "l
4.2 ROUNDOFF ERRORS
Signed exponen 1 1b i t s
89
Mantissa
52 bits
Sign (1 brt)
FIGURE 4.5 The monner which flootingpoint isstored on Bbyte in IEEE in o number in word double
prec s on formol.
Thus.although originalnumberhasfive signilicant the bits,we only haveto storethe four fractional bits:0. l0l I . By default, MATLAB has adoptedthe IEEE doubleprecision.fbnncrt which eight in bytes(64 bits) are usedto represent floatingpoint numbers. in Fig. 4.5, one bit is reAs servecj the nurnber's fitr sign.In a similarspiritto the way in which integers stored, are the expollL'nt its sign are storcdin ll bits. Finally.52bits are set asidefor the mantissa. and Howevcr.becausc normalization, significand of 53 bits can be stored. Now,just as in Exarnple.i.2, nreans this thatthenunrbers havea lirnitedrange will and precision. However,because IEEE firrnratuseslnany more bits. the resulting the number system can be usedtbr practical purposes. Ronge. In a tashionsimilarto the way in which integers stored, are the ll bits usedfor the exponent translatcs a rangefrom  I 022 to  023.The largest into positivenumbercan be reprcscnted binaryas in largest lue: +l.llll va ... llll x ?+10]'t
wherethe52 bits in the nrantissa all l. Sinccthesignificand approximately (it is acare is 2 tually 2  2 t'), the largest valueis therelbre 2r0rr l.i9ii x 10108. a similarfashion, In positivenumbercan be reprcsentcd the smallest as S r r a l l e sv a l u e: + 1 . 0 0 0 0 . . . 0 0 0 0 * 2  t t \ 1 2 t T h i s v a l u ec a nb e t r a n s l a t eid t oa b a s e  1 0 a l u eo f 7 r { ) 1 1 2 . 2 2 5 1x l 0 r t ) 8 . n v P r e c i s i o n . T h e 5 2 b i t su s e df b r t h c m a n t i s sc o l r e s p o ntd a b o u tl 5 t o l 6 b a s e  l 0 i g i t s . a o d Thus,z would be expressed as
' "pl
.1ll S 
formaL
Iong
I. I4I592ri53a891.) Note thatthe machine epsilonis 2 sr : 2.2204x l0 16.
90
ROUNDOFF AND TRUNCATION ERRORS
MATLAB has a numberof builtin functionsrelatedto its internalnumberrepresentafunction displaysthe largestpositivereal number: tion. For example,the rea Zmax
>> >>
Anq
4.
th
CI
nr
format realmax
=
long
t.
9
o9lI
lo02J.oeJtru
Numbersoccurringin computations that exceed this valuecreatean overflow.In MATLAB they are set to infinity, inf . The teaLmitT functionclisplays smallestpositivereal the number:
>> realmirr
\4
IL
rt
n
0 ? . . 2 2 . 5 7 3 8 5 8 5 4 i  2O i e  3 0 8 Nrlmbers that are smaller than this value create an underflow and, in MATLAB, zero. Finally, the e.ps function displays the machine epsilon:
>> eps
L n
are set t0
d
S
o n il
2 . ?.2 A ,!.4 6 0 4 9 2 5 0 3 I I e  C I tj
4.2.2 Arithmetic Monipulotions of Computer Numbers
Aside frornthe lirnitations a cclmputer's of numbersysteul, actualarithmetic the manipulations involving thesenunrbers can also resultin roundofT error.To understand how this performs occurs. let's look at how the computer simpleadditionanclsubtraction. Because their familiarity,normalized base10 numbers to of will be ernployed illuserrorson simpleadditionand subtraction. tratethe effectof roundofT Other numberbases would behave a sirrrilar in fashion. simplify the cliscussion, will ernploya hypothet?l we ical decimalcomputer with a 4digit mantissa a ldigit exponent. and When two floatingpointnumbersare addecl, numbersare first expressed that the so they havethe sane exponents. example, we want to add  .557 0.0434 thecomFor if l, + puter would express nurnbers 0. 1557x l0r + 0.00+3ll x l()r.'fhen the nrantissas the as are addedto give 0. 16004 x t0r . Now. because hypothetical I this computer only carries a 4  d i g i t m a n t i s s a . t h e e x c e s s n u m b e r o f d i g i t s g e t c h o p p e d o f f a n d t h e r e s u l lt 0s . . l 6 0 0 x i l0 Notice how the last two digits of the secondnumber(41) that were shifiedto the right have essentially beenlost fiom the computation. is Subtraction perfbrmed identically additionexceptthatthe sign of the subtrahend to is rcversed. Forexample. suppose that we are subtracting 26.86frorn 36.41.That is, 0.164x 102 1 0.2686 x 102 x 0.0955 l0' For this casethe resultmust be nornralized because leadingzero is unnecessary. the S0 we mustshift thedecirnal one placeto theright to give0.9550 l0' : 9.550. x Noticethat
I
I
(
4.2 ROUNDOFF ERRORS
9l
is to the zero addedto the end of the mantissa not significantbut is merely appended fill the empty spacecreatedby the shift. Even more dramaticresultswould be obtainedwhen the numbers very closeas in are 0.1642 x 103 0.7641 x 103 0.0001x lOj which would be convertedto 0. 1000 x 100: 0. 1000.Thus, for this case,threenonsignificant zerosare appended. The subtractingof two nearly equal numbersis called subtractivecancellation.lt ts handlemathematics leadto the classicexampleof how the mannerin which computers can numericalproblems.Other calculations that can causeproblemsinclude: Lorge Computotions. Certainmethodsrequireextremelylargenumbersof arithmetic In are manipulations arrive at their final results. addition,thesecomputations often interto That is, the later calculations dependent the resultsof earlierones.Condependent. are on sequently, eventhoughan individualroundofferrorcould be small,the cumulative effect A overthe course a largecomputation be significant. very simplecaseinvolvessumof can ming a roundbase number l0 thatis not roundin base2. Suppose thefollowingMfile that is constructed: function
n.
sout = sumdemo()
for i  1:10000 s = s + 0.0001; end sout = s; When this tunction is executed.the result is >> formaL long >> sumdemo ans = 0.9999999999999r The f ormat long command lets us see the l5 significantdigit representation used by MATLAB. You would expect that sum would be equal to l. However, although 0.000 I is a nice round number in base10, it cannot be expressedexactly in base2. Thus, the sum comes out to be slightly diff'erent than l. We should note that MAILAB has features that are designed to minimize such e:'rors. For example, suppose that you form a vector as in >> format longr >> s = [0:0.0001:1]; For this case, rather than being equal to 0.99999999999991 , the last entry will be exactly one as verified by >> s (10001) 1
92
ERRORS ROUNDOFF AND TRUNCAIION
w o A d d i n g o L o r g eo n d o S m o l l N u m b e r . S u p p o s e e a d da s m a l l n u m b e r , 0 . 0 0 1t0 ,a largenumber, 4000,usinga hypothetical computer with the4digit nrantissa the ldigir and After modifying the smallernumberso that its exponentrnatches larger, exponent. the 0.4000x l0a x 0.0000001 lOa x 0.4000001 l0a which is choppedto 0.4000 x 104. Thus, we rright as well have not performecl addithe tion! This type of errorcan occurin the computation an infiniteselies. of The initial terms in suchseries ollen relatively largein comparison with the laterterms. are Thus.aftera few termshavebeenadded, are in the situation addinga smallquantityto a largequanwe of tity. One way to mitigatethis typeof erroris to sum the series revelse in order.In thisway, rnagnitude the accurnulatecl to surn. eachnew ternrwill he of comparable the individualterms in a summation larger are Smeoring. Smearingoccurswhenever thanthe sunrmation itself.One casewherethis occursis in a series ol'rnixedsicns. Inner Products. As shouldbe clearfrom the lastsections, someinfiniteseries particure the is more ularly proneto roundolT error.Fortunately, calculation series not one o1'the of in A manipulation thecalcommonoperations numerical nrethods. lll more ubiquitous is c u l u t i o rttr f i n n e rl ) r o d u e (u s i l l s
T
FI
ze
: .trr,vr *,r:.v:*''' * x,,)',, E.tr.t'
in linearalgebraic This operation very comrron,particularly thesolution is olsimultaneous Suchsummations proneto roundoll are error. Cc'rnsequently. ofiendesirable to equations. it is suchsumrnations doubleprecision is doneautorratically MATLAB. in in compute ns
pr fu
do rel
ERRORS 4.3 TRUNCATION
(rrors are those that result fiom using an approximationin place ol an exact Trwrccttion procedure. example.n Chap.I we approx For the rnathematical r imated clerivative velocof j cq o i t y o f a b u n g e eu m p e rb y a f i n i t e  d i f f b r e n e e u a t i o n l ' t h ef i r r n r[ E q .( l . l l ) l duA,u
dt Lt
po
idt
u(ri+r) u(t1)
t;"t I ti
(48)
gu wt no
A truncation into the nurnerical scllution because difl'erence the equaerrorwas introduced (recallFig. 1.3). gain insight To into tion only approximates truevalueof thederivative the we theproperties sucherrors, now turn to a rnathematical of tormulaticln is used that widely furrctions an approxinrate in fashionthe Taylol series. in numericaln'rethods express to
Th
.f ar ber ilar
4.3.1 The Toylor Series
Taylor'stheorem and its associated formula,theTaylorseries, of greatvaluein thestudy is statesthat any smoothtirnction of nurnericalrnethods. essence. Tay'lorthertrem [n the can be approxirnated a polynomial.The Taylor.renes as then providesa meansto express this in ideamathematically a form thatcanbe usedto comeup with practical results.
fac on tior
leg
w h i c h i s a d i s t a n c / r a w a yf r o m y o u .n):fk) \:[:*':d::s = /(.what would be your best guess the heightat x. r i + r )] .1) ^xt .You immediately in sense that the front . 1 ( x r ) G.r ) : O l .6).r r O 5 .if thc functionchanges all over the interval.+t : 1 T h eo p p r o x i m o l i o o { / ( .9. However. our problcm. are to So now you are allowcdto get ofTthe plattbrrn and standon the hill surface with one leg positionecl tiont of you anclthe otherbehind. indicates that the valueof I at the new point is the sameas the valucat the old point. You are givcn the task of pledictingthe heightat a to p o s i t i o nr .This resultmakesintuitivesense because r. c At first.ERRORS 4.9) This rclationship.you would be right only if you happened be standing For a to on a pcrfectlyflat plateau.first order.).it is likely thatthe new valueis probablysimif i l a r t o t h eo l d v a l n e . r r .addiat tionaltermsof theTaylorseries required providea betterestimate. the bestguess would be the sameheightas whereyou're standing now! You c<lulcl express this precliction rnathcmatically as l ( .r. constant.'3"^ ri=0 h r.A good A usel'ul problcrnc()ntext this cxercise to predicta functionvalucat one point in ternrs the is tbr of point. At this point.r'(. 11 . andr.a 2 5 x * l 2 o t r : n zero order..6 l by way to gain insightinto the Taylor scriesis to build it term by terrn. you areplaced a platfbrmthat is completely horizontal thatyou haveno so on idea that the hill is slopingclownaway fiorn yon. provicles perf'ect estinrate the functionbeing approxirnated in f'act. .r.4.11'? you think aboutit (remcmbcr you haveno ideawhatsoever at If what'sin front of you).eroonler uppntximatiorz.ond secondorder Toylor series exponsions. a if is.th \.O l 5 . firnctionvalueand its dcrivatives anothcr at Suppose that you ale blindfblded ancltakcn to a locationon thc side of a hill facing We'll call your horizontal location and your vcrticaldistance with downslopc Jri 1Fig.r a. respect thc baseof the hill l(.3 TRUNCATION 93 Zero order Jk.11 erecloseto eachother. FIGURE 4.. Equation(4. which is calledthe7.
rr)is even lower than your previouslinear prediction.You can express (4.it at quently.To get a betterprediction.we could keep addingmore derivativesto capturemore of the function's expansion Taylorseries vature.r. t ' ' G i ) +/ ' ( r . l ' G i + ) = .{ 4 . use the slopeestimate projecta straightline out t0 by this predictionmathematically .we arive at the complete wht The this plv bas wil ter l . you're allowedto obtain a quantitative the slope by measuringthe differencein elevationand dividing it by the distancebetween your feet. functionbetween andt. (4. valuesthat you have estimated.a1. Thus.ri) *. Let's call this slope that the slopebehindis milder than the one in front. rn + oft lou onl trat c .fi(.we can seethatthesecondorder orderpolynomial. .I l). they are numbers. .' (o.1 t ' l r .f (xi+). n ' * " f ' ' .2) r wer ber the of in of derivativeis merelya derivative a derivative. L . With this additional information.Qi) Ax CITC (4. Conse' That is.. trend. this case. Thus.f (. only unknowns the values the prediction the are is a quadratic equation the tbrm of r . * J"lr'l L : ^2 No1 sig acc wht and app ide non SOIC con den yie inn eno req Thi SOIT .You canuse of To makeuseof this fbrmula. Although Eq. the expression . subscripted represent r positionx. + / ' ( .t') (n the derivative. Recognize Before proceeding. rate Thus. you're clearly in a better position to predict the to you heightat .11. r ): f ( r . ' + " * f " ) t t i t r ' + R . .u)h of the because additionalllrstorderterm consists This is calleda.11. The Taylor seriesprovidesthe correctway to do this as in and make it into a parabola. In fact.10) = f (. and moving the other one forward Ax.fiQt) . slopebehindyou by keepingone foot plantedat x.1..it is only exactfor a straightline or linear. the odds are that .In essence. . 1 3 1 .So now you the First.ri+r) "f(.j J .f'(.Q ing one fbot planted at x. the second change the slope. f" ( r i + ) 7 JQ con . f ( h ) = a 2 h 2I a 1 h a 1 y the Taylorseries approximates functionwith a second' Thus. of that all the values let's look carefullyat Eq.fi. a slope/'(.Consequently. . You immediately Clearly the drop in height is "accelerating"in l'rontof you... T h e n y o u m e a s u r e t h e s l o p e i n f r o n t o f y o u b y k e e p f . (.) multipliedby /r.r.we needto add more terms to our equation.firsrorder approximation is between and x.l0) can predict a change.r. and moving the otherone back a dist a n c e A . f ( r .Thus.r). cut' Clearly.94 ERRORS AND TRUNCATION ROUNDOFF estimate of foot is lower than the back foot. L e t ' s c a l l t h i s s l o lp e i ) . you measure are allowed to standon the hill surfaceand take two measurements. term to your equation you're now going to add a secondorder As you rnightexpecf. .1.. the distance ofthe or now in the form of a straightline that is capableofpredicting an increase decrease J. recognize .you needan estimate the second you determined estitlateit as to lasttwo slopes .
first. ( 4 . (4. H o w c v e r .I l). cxample. / ( .h ei n c l u s i o n l ' o n l ya f ' e r ve r n . 1 . ter the error. s rs R .o w e v c r l i g h t . can usuallyassume the truncation sn'rall. .3 TRUNCATION an the Eq. A remaincler accountfor all terms fror.1 . S c c o n dt. w c n c c dt o d e t e r m i n t h c c t ( r r+ l ) t h d e r i v a t i v e l ' / ( r ) . equalsign replaccs approximate terrn is also includedto sign that was used in Eqs.l ) . ( .ERRORS 4.l . halvingthe stepsizc i t s w i l l h a l v ct h ee r m r rO n t h c o t h e rh a n d . to only a tew termsare required obtainan adequate tratedb_\. fiorn. norlial. Although That is.{ is not known cxactlybut melely lics This relationship two major drawbacks. the ellol is proportional thc stepsize/r raiscdto the (rr * l)th power'.+ r . r ) . is valueof Taylorscries expansions that. lbr high ternrsusuallyaccount a dispropoltionately percent the error'. a n d . n s e q u e n i y . E c r s o m e w h e rb e t w e e n.w e n e e dt o k n o w .r'wc wiurlto evaluate (r ).a finite nunrber o1'terms r s n. T o d o t h i s . f r v e k n c w i o /(. llrere would be no neeclto pcrlirrrnthc Tirylor scrics expansionin the present ! contexf i .. following exanrple.. t t s c o n t r i b u t e o m ci m p r o v e m e nh. rw i l l r e s u l t n a n a p p r o x i r n a t i oh a ti s c l o s e c purposcs. alc Only if an infinitenurrbcrof terrns addcdwill thc serics in demonstrateci Example y i e l da n e x a c tr c s u l t .3.ical (Eq.+l g. to o1'the that multiderivativcs implics nothingregarding magnitudc the this approximation rnethods error of nurncrical ply firr+t. will be exactfirr arrrthordcr polyTiiylor series expansion In general. ( 4 .l4).This is because rlo havecontrnlovcr lhc term ir in thc cquation. Note that because (4.. . is between lifldr. 1 ) i s u s u a l l y x p t e s . The assessmcnt how many tL'nxsare ol cnollghto the true valuc lilr placl.o c v a l u a t e q . lower'order This propertyis illusapproxinration.judgingthe comparative Fur based Taylor scries on exparrsions. on tcrrnof the expansion is required get "closeenclugh" bascd the remainder to has First.1. For otherclifl'ererrtiablc continuous Eachadditional term will will not yicld an exactestimate. q .13)is an infinite series. nthorcler the suchas exponentials sinuand and functions.l1) fbr n that this is the rernainder the ntholderapproximation wherethe subscript connotes .nn * I to infinitv: R .we can now seewhy the Taylortheorem provides means express to this a and that thc Taylor series approximated a polynornial as ideamathematically. many cases.9) through (4. of Thus. 1 ) s s t i l l u s e f u lt i r r g a i n i n gi n s i g h ti r r t ot r u n c a t i o n In we errols. by that erroris decreased the addition we In general.t e ps i z ew i l l q u a r . 4.: O ( l t " ' ' ) t n e w h e r ct h e n o m e n c l a t u rO ( 1 . s e d a . o t h ea p p r o x i r n a t i oT h i s b e h a v i ow i l l b e .the practical i tn o t s i n r n o s t a s e st. andwe cilltcontrolIhenttntwe canchoose how lar awav l E e r Co b e r < r f ' r e r n w c i n c l u d e n t h ee x p a n s r o r r .t'.if the crror is O(fi).h a l v i n g h r .anclother In il'fi is sLrfficiently the of tcrnrsto the Taylor series.e a valueof t'thatlies somewhcre states that any smoothfunctioncan be Thus. 1 . ' * ' ) m e a r t sh a tt h e t r u n c a t i o c r r o r i s o f t h c o r d e ro l / t " * i .J / t / / + ll l i \ l'7. \ . is Althoughthe tbregoing true. f t h ec r l r r ri s 0 ( f i r ) . it is extremclyLrseful in.. soids. D e s p i t et h i s d i l c n r n r aE q . otherwords. and. the .r).
l:4. add the second which has le. s 2 r e 8 6 6 s s ' /\ \ 4 + \rl derivawe approximatitln.f'(x) : sinxl the we approximatit'rn. 4 B si t ( i ) = ' " .l:0.96 4 EXAMPLE .3 ERRORS AND TRUNCATION ROUNDOFF w A p p r o x i m o t i o no f o F u n c t i o n i t h o T o y l o rS e r i e sE x p o n s i o n with n : 0 to 6 to approxirnate Problem Stotement.r sin r cos. r COS Y l'0r/t1 o 7 a 7l 0 6 / 8 l 9 0 521 B6e'59 o 497754491 c )4 9 9 8 6 9 1 4 7 0 50000/551 0 500000304 0 499999988 le.(4.. lu_L9#Sl'or* :rt o* term where. .44g%.974o/a the true value. adtJ first clerivative For the firstorder It sll r\/r1. by For thiscase' the comeswith the initial terms' alsonoticehow mostof the improvement that which means the tinre we have aclded thirdorderterm. Use Taylor seriesexpansions . For the seconclttr<ler "('r ) : .)'"'..Consequently.= . the inclusionof additionalterms results nrate. Thus.the irr.f(r):cosxat'rr+t:rl3onthebasisofthevalueof/(x)anditsderivativesat * t x i : t l 4 ' N o t et h a t t h i sm e a n s h a t h : r 1 3 n l 4 : T l 1 2 ' the correct Solution.400/o.5.t s i n.)\i) /z\ /r\ /r\/r\ cos(]r/4(l\':0.449 ) 262x lA l 5l x lO r 608 x lO 5 244xlO" 0 I 2 3 4 5 6 sirt r cos r nevergo to zero as would be the casefor a polynornial' Notice that the derivatrves Howevet' in eachadditionalterm resultsin some improvement the estimate' Therefbre.cosr: tive terln where/ AI (jfl ca ap t(i):c. the error is reducedto 0. / " ) .9)l 4.'s(.0267a' of more althoughthe addition of we have attained99. Our knowledgeof the true tunction meansthat we can determine vauef(r13):0.rprovement terms will reduce .'.Thezeroorderapproxinrationis[Eq.4()1154491 2 \r2l "' estiir.l 414 440 a.ran improved with le. . negligible' becomes the enor turther.( i ) : o' o ' r 0 6 7 8 l a which represents percentrelativeerror tlf er se : . i n f 4 /)\(1 2 l) : 0 .Thepfocesscanbectrntinueclandtheresultslistedasin Order n * Gr fr"(x) cos.
(4.visuallybased explanation. derivatives usuallyaccount a greatershaleof the remainder than the lrigherorder terms. \ it h+ + h 2 + ' . the Suppose that we truncated Taylor seriesexpansion [Eq.7.higherolder terms.To do this.1s) lowerorder for Although. t d . This "inexactness" impliedby the is cause the neglected of and ( : ) e m p l o y e dn E q .8. r ' r o ' ) .3 TRUNCATION 97 4.T o . f"tv t f{l'tt t .l^''h3+.7 r GrophicoL depiclicn cf o . Before demonstrating we must explainwhy we includedthe ar{ument in Eq.One It is obviouslyin. . (4. . format. l 5 ) . r o r . de n o r .. consists the iniinite seriesof terms that were truncated R r:r.as statedin the previous section. d e t l (xl Ll..onu"Ji. / ' ( .2 The Remoinderfor the Toylor SeriesExponsion how the Taylor seriesis actuallyusedto estimatenumericalerrors.this resultis still inexactbesecond. : r . or A visual depictionof this zeroorder of error.as in simplification might be to truncate remainder the = R11 .['(x1)lt (4. which is also shown in the iliustration.ERRORS 4. in Fig. the derivative As mennvalue states that basedon a grnphical thertrem FIGURE 4.nt to deallith the remainder this inflnite series in itself. we will use a f simple. 1e at re . i s a p p r o x i m a te q u a l i t y y m b o l e the into is An alternative simplitication thattransfbrms approximation an equivalence insight./(.+r). ( 4 .r. I 3)] after the zeroorder term to yield ] . of this prediction. )t. 4.14).3. L l s e i p s 6 r c f l . 4./(ti) predictionis shown in Fig. The remainder.
ilurr.la) Similarhigherorder 4.r. versions be developed can from Eq.l{. f'(t): + whichcanb. .8 h G r o p h i c o ld e p i c t i o n [ t h e d e r ] v o t i v m e o n .ri+1.l(.dividedby the run h. this { slopeoccurs(Fig.r..(a.l'(6) R. 4. used Eq.4.designated /'(€).ng.valuecorresponding the second derivative to thatmakes (4. is simpletu realize it that.8. thatis by parallelto the line joining.f{x} n R o1 Nr Eq h FIGURE 4.16). version is I"(F) R1 : :7. it may not be clear to you how the expansion to can actuallybe applied wher.t''' Thu tion shou expe (416) 4.v o l ule e o r e m o e Tht the we tlor if a functionl(r) and its first derivative continuous are over an intervalfrorn. Li Equ ence r4l7r For this case.t)h Ro.) The parameter marksthe r valuewhere and. in By invokingthis theorern..14). or . (4.).. then thereexistsat leaslone point on the functionthat hasa slope.oto {ive (.*.8).3 versions f'hus.3 Using lhe Toylor Seriesto EstimqteTruncotionErrors will be extremely usefulin estimating truncation enorsthrough' AlthoughtheTaylorseries out this book.we havederived zeroorder the version Eq.as illustrated Fig.il'you travelbetween is of velocity. two pointswith an average therewill be at leastone momentduringthecourseof the trip when you will be moving at that average velocity.the value of { confbrmsto the r.3. matic and i .:r. to.A physicalillustration this theorem that.theslope is equalto the riseR.98 R O U N D O FA N D T R U N C A T I OE R R O R S F N . Eq.l7) exact. of The higherorder Thefirstorder aremerely logical a exfension ol'thereasoning to derive (4.
l 9 ) ] h a sa t r u n c a o [ tion error of order ti+r . That is.finitedifJ'ergenerallyas ence.3 TRUNCATION 99 jumper.19.(4.ri) (1. .l 8 ) t. Equation l8) can be solvedfbr rl I I ( 4 . It is termeda "forward" difference because utilizesdataat i it (Fig.In fact.x.ERRORS 4..f 9) is given a formal label in numerical methodsit is calleda.Consequently./ i ) * R r (4.becauseoftheTaylorseriesapproach.(4. Firstordcr appfoxr nra(ron Truncatiorl e[ot that was usedto approximate is The first part of Eq.f\ri+t) /(.lt can be represented f '(. f ( r i ) + o( h) (4.2 was to predictvelocity as a function Recall that the objectiveof both Examplesl.fi) Ji+t r. 4. the length of the interval over which the approximation is rnade.4 Numericql Differentiotion Equation(4.l4) and (4.. 13).( l .) exactlythe samerelationship thederivafiveinExamp1. * . numericalmethods.21) where /r is called the step sizethat is.9a).   u(ri+r)u(ti) R1 ri+l ri  {i+l  {i (4r9) . we have alreadydone so in our exampleof the bungee I and I . As specified Eq. l l ) o r t h e l l r s t p a l t o f E q .4. . f O(ri+t . we were interested deterrnining in can be expanded a Taylor series: .ri\ : . In other words. ( 4 . l ( .. ) * { f LI . r i + r ).(4.ri+t. Using Eqs. by in u(/). and i * I to estimate derivative the .ti. r .2[Eq. u(l) of time.(l.3. ( / i + r:) u ( t ) * u ' { / 1 1 1 ' * .ll)l.F R.t h e e s t i m a t e f t h e d e r i v a t i v eE q . r .. the error of our derivativeapproximation shouldbe proportional the stepsize. I .t i ) z+ " ' .l9) yields rRr ti+t li : (ti+t 2! u"(6).20) f '(ri) : .  ti) or R' ti+t. le with this approximaof error associated we havenow obtained estimate the trtrncation an tion of the derivative.we would if to expectto halvethe errorof the derivative. Now let us truncatethe seriesafter the first derivativeterm: r u ( t i + r ): u ( t i ) f u ' ( / r ) ( t r * . we halvethe stepsize.Ii :O(tirrl.However.) T h u s .
9 finitedifference depiclion (o)forword.too ERRORS ROUNDOFF AND TRUNCATION S e d tt e5 b1 al br Bc CX Trr wht Cer mat to yi whic tc) FIGURE 4. {c) Grophicol of ond centered {b) c n n r o < i m n t i o n so f r h e f i r q rd e r i r o t i v e Noti appr the tr the r war( tral r . bockword.
f'r)(". in as f h f t x i . whereas the latter uses valuesthat are equally spacedaround the point at which the derivative is (Fig. SeeFig.4.. for third.backwardand centered difference approximationsof the llrst derivative can be developedin a fashion similar to the derivationof Eq.f(xir) (4.4.For example. is (4.the error would be quartered.25) is a centercd representation the first derivative. The Taylorseries be can expanded backward calculate previous to a valueon the basisof a present valr. whereasfor the central difference. Centered DifferenceApproximotion of the First Derivotive.re.l''(x) : (1. Taylor seriesanalysisyields the the practical information that the centereddifferenceis a more accurate representation of fhe derivative(Fig.l(ri+r) .t 2 l ' ( x .22) 1 ' G i )= J (ri) .22)from the fbrwardTaylorseries expansion: J ' ( x i .25) Equation (4. r*i + f f n ' + ' .). 4.if we halve the stepsize using a forward or backward difference. r i + r ) l ( .3 TRUNCATION ERRORS 'I'his rol forward differenceis but one of many that can be developedfrom the Taylor seriesto approximate derivatives numerically. 4.19). (4..r.fGi t) .t ) . b 2h t't". The following sections provide brief summaries illustratinghow someof thesecases derived. . The former utilizesvaluesat r.9c). * whichcanbesolvcd for . l ( x i + r ) . l ' ( r i*) l ' ( . ' to yield J'^l(*'\ : rt * .1 and.we would approximatelyhalve the truncationerror. (Fig. all the foregoingversionscan also be developed second. r i _ r )2h o(t) (4. r .9c). A third way to approximatethe first derivative to subtractEq. and higherderivatives. 4. For exarnple.23) wlrerethe error is O(/z).9b for a graphical representation.Consequently.r t : J ' ( x t t.9b). of finire tlifJbrence Notice that the truncationerror is of the order of ft2in contrastto the forward and backward approximations that were of the order of /2. f( x i ) .21) .More accurate estimated approximations the first derivativecan be developed of by including higherorder termsof the Taylor series.\  * " f ( . Finally.l ' ( x i f t + ::'t Truncatingthis equationafter tlre first derivativeand rearranging yields (4. are Bockword DifferenceApproximotion of the FirstDerivotive.
5 r .0 . 5 ) =f f Forft : 0. r a .925.l'2:0.0 ' 2 5 ' t.l T rh difference' andthecentered 5 0 .6363281 0.1 = 0 .0 ' 2 5 andcanbeusedtocomputethetruevalueas/. Use forward anclbackwarcl to estimatethe first derivativeof n differenceapproximatio of O(h2) a centered J (.5"/o 4.r. r.2 . l: 5 8 ' 9 % l''(0.0'925 l : '15 = l e .5 le.0 .0 . 9 2 5 1 .5.4 I the backwarddiff'erence' 0. Soution. l .25.s) 0.5 usinga stepsizeft : 0. 6 3 6 3 2 8 . 7 5 1. l ( r i * r ): 0 ' 6 3 6 3 2 l8 dif'ference' the to whichcanbe usecl compute fbrward 30. and the centered .0 f (xit): l'2 .5):o.l:26.t.0 .I :0.r): . \: 2 ' 4 % ' o ca E si c( . 5 .5) * difference lEq.925 l'(ri+t):0'2 the [Eq' (a'21)J' Thesevaluescan be usedto compute forwarddifference o'2 .0 le.25 lt.1 0. t r .(0.5) :l. : 0.55 0.l(rir): l'10351563 J'Qi): 0'gZS 3 .25 le : 21.934 1 1 : f / ( o S \\ : = ' O { 0. Note thatthe the ar r : 0. .3 . 4 ' t 3 . ri t:0 to the functioncanbe employed determine .4 ERRORS AND TRUNCATION ROUNDOFF o Ap FiniteDifference proximotions f Derivotives of approximations o1/r1and clifference problem Stotement.925 T EI = l''(0.5 : rri+r 1.6a/<. 4 5 x 2 1 ' 0 x. the backward /'(05) . l ' ' ( 0 .1 0 3 1 5 6 3 0.1. 1 0 3 5 1 5 :6 3 11 l''(o.0'925.0 .25 . 1 1: 0 .f @) :0. l 5 .l:3g"7o/o clifl'erence (a'25)l' IEq.'Eo . Repeat comprrtation directly its can be calculated clerivative f'(r): . (a.5.r02 4 EXAMPLE .23)1. r r.l55 0.5 V .t 1 ' 2 usingh:025. For h:0.l:9. : 0.
r i ) : f f + o f t 2 ) ./"(r./{.2 .)2. w e w r i t e a f b r w a r d T a y l o r s e r i e s e x p a n s i o n f o r . 7 ' " 1 t 1 1+ .just as the second derivativcis a derivative ofa derivative. l ' ( r i ) I + J'lxi)t2hl '")r''' .2 . Noticealsothatthecentercd vcrsioncanbe altemativelv cxnrsssed as l ( .24)canbe multipliedby 2 and subtracted Equation from Eq. 1 ' ( . * 1 ): which can be solvedfbr .2 .27) This relationshipis called thc second..4 IOIAL NUMERICAL ERROR r 03 For both step sizes. / ( .\ . l ( .. FiniteDifference Approximotionsof Higher Derivotives..4 TOTAT NUMERICAT ERROR The total nnnerit:aL error is the summation the truncation of and rorrndofTerrors. Irecall 4.r i ) : ii t) . / ' ( r .. r i r )  As wasthccascwith thefirstderivativc approximations.26)to give . 1 ' ( r . Further.4 demonstrated that the truncationerror can be reducedby decreasing step the size.f ont. l ' Q i + ) . . second the finite dilfbrenceapproximation a differcnce two first finite differcnces is of Eq. halving the stepsize approximately halvesthe error of the backwardand forward differences quarand ters the error of the centered difference. r i ) h2 . + o(h) (1. Also.1"(r. contrast.l2)1.t.Because decrease stepsizecan lead to subtractive in a cancellation t.the centereddifferenceapproximationis more accurate than forward or backwarddifl'erences. + r ): . .finite diflerenca.we havenotedthat roundoff error may int:rease to subtractive due cancellation dueto an increase the number computations an analysis.4. or in of in In Example4.. Besides first derivatives.general. the Taylorseries expansion be usedto derivenumerical can estirnates higherderivatives. / ' ( .26) (4.r. truncartion the errors aredecreased the roundofTerrors increasetl.2 / ( .  . t . r i + t )* . f ( . [ ( r i ) + .oarl increase or in computations./. rn+ e r m s o f . of To d o t h i s .):ff+o(h) and a centered version . (4.): I ( r i + : ) .unl.rrt* " (4. as predictedby the Taylor seriesanalysis. f ( . r i + r ) . In the only way to minimizeroundofT errorsis to increase numberof significant the figures of the computer. r ) . centered the caseis moreaccurate. t il I l ( . ".S i r n i l a r a n i p u l a t i o n s n m ca bc cmployedto derivea backwirrd version . r i )_ I G i ) ./'(tr)*. / ' ( x i t ) hh f ' "( r i ) 1 Thus. (a. .1 (ri 3) . as are . h .
our remedybecomes problem(Fig. D E Thus. we couldconceivably thc errorsonly to discover clecrease stcpsizeto minimizetruncation that in doing so.1 Error Anolysis of Numericol Differentiotion in As described Sec. erplore sucha caseirr thc lollowing section.28) 2h Finitedifference approximation f d H fr Truncation error h.r04 ERRORS ROUNDOFF AND TRUNCATION Point of diminishing returns o J Log step size F I G U R4 .): True value J(ri+tl . 4.f'(./'(t)(6).the only error is due to truncation.4.r.3. One challenge that we laceis our step computation.25) l P b R ire . the roundofferror beginsto dominateths solutionand the total error grows!Thus. shown. if the two functionvaluesin the numerator the finitedifference of approximation have no roundoff error.4. suchsituations relatively uncommon of are a Nevcrtheless. are facedby the following dilemma:Thc strategy decreasing we one of ln of to component the totalerrorleads an increase the othercomponent. a cornputation. 4. 10.4.rlations roundoffcrrorswithout incur ring the penaltyof a largetruncation error. l0). . do principle"thatplaces absolute certainty an limit on theaccuracy that rnaybe obtained using methods.to I6WhenusingMATLAB. sometimes occurandsuggcst sortof "numerical they undigit precision. We certaincomputerized numerical EXAMPLE 4.ti r) .5 R P c 4. would like to choose Wc' to determine appropriate size1bra particular an the and a largestepsizeto decrease amountof calcr. I O E roundoff iruncotion thotsomelimes error A croohlcodeoiction thelrodeoff of between ond methodThepointof diminishing relurns is coiresintoploy in thecourse o numerico ol whereroundoff begins negole benefits stepsize to lhe of reduclion.1 (.the is returnswhere roundofT error begins to challenge to identily the point of dinrirrishing negate beneflts stepsize the reduction. of bccause its l5. I .a centered difl'erence approximation the first derivative of can be writtenas (Eq. error t i t fbr Therefore. 4.If the total error is as shown in Fig.' 116 (4.
Then progressively dominantasthe stepsizeis reduced.l lxit: f(*.: " 6 True value Finitedifference approximation RoundofT error Truncation error consistsof a roundoff approximation We can seethat the total error of the finitedifference with stepsize.0 .4 IOIAL NUMERICAL r05 However.r. themaximumpossible valueof M.0 . f ( . h1.29).E L5 .) . how roundoffbecomes of I 0 to demonstrate by a f'actor is your results Eq.rl 2h Pi t . funcLion di ffex 1ong ( func.0 . n I G. becausewe are using digital computers.5. f ( x i + ) I e i + r roundoff where the . We can develop following Mtlle to perfbrmthe computations plot the as derivative arguments: that we passboth the functionand its analytical Notice results.30) E o R o u n d o f f n d T r u n c o t i o n r r o r si n N u m e r i c o lD i f f e r e n t i o t i o n of approximation diffbrence Problem Stotement. (4. E X A M P4 . /(. l .€iwill be 2e. (4. Further. of the O(h]) to estimate first derivative the l t ' G ) : . 5 1 2. x. / ( x rr ) < _ + _ h6 l. r r . 2 divide the stepsize Peribrm the samecomputationstartingwith ft : l..4. ) / (2*h) . An upperboundon hasa maximumabsolute thtrtthe third derivative assume as value of the total error can therefbrebe represcnted the absolute t T o t ue r r o r : l e h2M  . to Relate and the Solution. H ( I ) =h.9 125.Substituting ?t.'r a. 2 5 x 1 . we useda centered fbllowing functionat.the function values do include roundoff error as in t eit . In Example4. valueof thedifl'erence upperboundof e. .r ) .Recallthatthe truevalueof the derivative 0. .e the rounded function values and the e's are the associated valuesinto Eq.30).settingthe result by An optimal step size can be determined diffbrentiating equalto zeroand solvingfbr ('1./'t_r./'l'(€). cLfunc. that decreases with stepsize and a truncationerror error that increases of the roundoff error has an Assuming that the absolutevalue of each component €i+r. [ 1 r .ue=df unc (x ) . t'.28)gives these errors.f(xi t) : f @i.r1A. _ .29\ Eq. l 5 . n) format df t r. r a ..r i + r ): .o . D(1) = ( func (x+h) func E (1 ) =abs (dftrueD(1) (xh) ) . (4.r : 0.ri t) .ERROR 4.
u. O(r()()000(l() I Q. in foundol'f minimalandthe is arc At Hcnce. l a .J0000075ir006 0.91 2!t)000000b4.r11e('Fla'! The Mflle can thenbc run usingthe tbllowingcommallcls: .r 0 0 ( . l ogLog (H.the crrol irrcrcascs roundofTclominates.( 4 . . !.!*x^2 0. j 9 ! r t r ' . l l l t ) ( ) 1 0 . s t a r t i n g a t / z : 0 . .l . > : ' c l i f I r ..') IormaL sholf errol'\n'). i r . . r . 2 .1 0 . q 0 .l(il uF func(x  )). r 9 3 t1 0. x (f f .n c c t. t l 21000rJ00 l l I ! 0. at crror is reachcd /r: l0 Bcyondthis point. r l L .10f lprintf %]5. H(r)=5. 0. I r i .. n h = h .29).1 .2 . 1 r .0000000199894 0.0f)000000 t 0 0. E) .: t1. I t l f l r r ( l 0 L l 0 r ) ( l 1 . o r \ / c r .) 0 1 :0 ( r i l0 t l 0 0 0 0 ) 0 .25*x+1 . ) !.500u t 9 98q44 U. (r x ) L l.14f \16.we canestimatc by evaluating M t h e l ' u n e l i o n 'ts i r t ld c r i v l r l i v c s h u . As clepicted Fig. 3 0 ) i v e s l0r(') :4. as Becuu. (4. =[ l  L)' E' I ' .0(l 10000{)(r0 A. 0 r ) [ )( ) 0 t )0 U 0 1 .. t t 2!.5 x l0 r('. 0 . H o w ' c v e r . 0 ( ) ( l l r 0 0 0 L( l o 0. czur wc also investigate whctherthescresults consistent arc with Eq.lif\n'. { i (  ) 1 ) { ) ( ) ( )3 ) i . equ you pos and first w h i c h i s o n t h es a m e r d e ra s t h er e s u l o l I x l 0 t o usir erro lem. r . l l ) f :trp :. .y Label ( 'Error' ) rt l . .30). end 1 .L) .2. w e s e e roundol'f'error bcginto crcepin andcrodethc rateat whichthecrrordirninishes. D(r1= (func(x+h) (.) o.l 0 d i g i t s a r r o u g e s t i m a t e oh e c 0 .l4 9 t j c ) ' ) 4 5 ( r l I q 12 5 O ( t r ) (I i 1 6 0 ' r 0. l . 1 * x ^ .15*r^30. 0 0 0 0 0 10 0  0 ( l . ! ) l : 5 ( ) L r l r { ll . t4. U(r0t0Ol!t)000t) 0 . ' 1 ..9 I 2!00(l i 'rb(10!tl 0 .4*x^ i 0. fprincl(' finite diflerenre srtrp siz.the totalerrordrops r'stimatc dclrninnted truncation is by Lrror. [ )( ) { ) 0 0 U1 ( )( ] i ) 0 . 0. r _ l0 0 0 0 0 0 0 0 0.r'uLr error I .. thc rcsLrlts asexpcctcd.0.5 ) l . I 1 For Thr apt mu pro eng B e c a u s M A T L A B h a sa p r c c i s i o n 1 ' a b o ult5 t o l 6 b a s c . 2 1 1 0 . xl abel ( 'SLcp Si zc' ) . 1 1 0 1 1 6 6 3 0 ( )1 3 6 1 l 0.45*_r^2 x (1.. r .sc afc dcalingw'ithan easilyclittbrcntitble w'e l'unction.0il(l0l 5000000t) ) 0. 0 0 0 ( 10 0 0 f { l ( l I . l'irst.00utJ000000112 0..) . ( ) : 2 .4.izc f inite c ii 1 f e t .0000 10 0 r ) r J 0 0. rS L e p Stz.First.I I.r0 6 AND TRUNCATION ERRORS ROUNDOFF i rr i . ( r ( ) 0I ( l o L r ( ) i ) r l l ) . 0 0 ( ) 0 ( ) ( l ( ) l l 0 r )  0 .0r100000000054 0 . in Eq.3 x l0 (' ('ohtuined withMATLAB.1 50 . 4 9 / 0 .M : I l ' t ' t ( 1' . 1 r j. 0 ( j t ' r ( ) t l L l t ) u t . . gra. 1 0. rninimum A t'.Srrbstituting bc into thescvalues g E q .912.rr)( 1 0 0 t r r .2b. !ll2 5J i00000011(r ll. h) ) / (2*h\ . ('%14.> > f f=G(x) c 1 f = ( .')12 500llr00000I i l . tf h upperboundorl rounclofl'would abollt€r:0. as bya s ep t a c t o r o l ' I 0 0 e a c h t i n i e w c d i v i d c tth e b yI 0 . 0 0 0 l .
numbers. many ciises In error estimates based the expericnce juclgnrent ale on and o1'the engiueer scientist. or Although error analysisis to a certain extent an art. is bestto sortthe numbers numbers andsubtracting it and work with the srnallest first.I Plot of error versus step saze 100 1o2 10 . almostalwaysoccurswhen this is done. course. and numerical Thereare no systematic general approaches evaluating to errorslor all problems. it attempted only srlallscale : of the es into . of Theretbre.4 10 .The Taylor seriesis our primary tool fbr analysisof such errors. you may wirnt to use extendedprecision Furthermore. Prediction total numerical of erroris very complicated foreven rnoderately sizedproblemsandtends be pessirristic.6 t! 10 . we First and fbremost. whenwe know theexactsolution. for engineering scientific and applications we fbr o1'the mustsettle someestimate errorin our calculations.4 Step size 4. to Theretbre.2 Control of Numericol Errors rd the .avoid subtracting two nearly Loss of significance equalnurnbers.4 TOTAT ERROR NUMERICAL r07 FIGURE I 4. rnost approxitnations unnecessary. itigate uating practical Forrrrost with rnethods.Thrsavoidslossof significance. cases.4.Sometimes you canrearrange refbrmulate problenr avoidsubtractive or the to cancellation.4.bya /e see imum rtes. there are severalpractical programmingguidelines can suggest. is usually for tasks. do not know tlreexacterrorassociated numerical we is which makesour numerical The exception. this is not If possible.8 1 0 1 0 10 8 106 10 . Beyond thesecomputationalhints. one can attemptto predict total numericalerrors using theoreticalformulations. when adding arithnretic.
theearlyyears or In ol'conrputcrs. thissource ol'ernlris highly unlikcly. 4.3 For computcrs. so of irr ancl Thc rolesof errorswill be a topicof cclncern analysis all sections thisbook.UNDERS. tt . ntc ol. of principles and of fundarnental and by the carewith which you approach designyoursolution to a problenr.t08 ERRORS AND TRUNCATION ROUNDOFF and try to estimate The tendencyis to pnsh forward with the numericalcomputations be the accuracyof your results.3 areextrcmely mitigatingprogramnring blundcr. to Wc will leavethcseinvestigations spccificsections. MODELERRORS. This is nodoubt disregardeddiscussions are extentunavoidable.5. Such experiawareness cor. they can sometimcs techmodelingeflbrt. I zl. erroneous numericalresultscorrld sornetimes attributcd rnalfunctions be to ol'the cornputer itself.Or it n'ray possible substitute results into the originalequation checkthat it is actuallysatisfied. parfor lrabits usetul ticular. Step 2: Il' I + e is less th Otherwiscgo to I Step 3: r' : e/2 Step4: Rcturrrto Stcp2 Step5: 1x€ a .conornic ramiflcations.2 Model Errors Model error.rnistakes to a certain of can In believe thattherearea number waysin which thcir occurrcnce bc nrinimizecl. rnustbc attributcd humar to Today. with most of thenuol'error are not dircctly connected Although the following sources havegreatimpacton the success a of mericalmcthodsin this book. and tional strategies. blundcrs.wc discusswaysto chcckthe results nurrerical of calculations. is appropriate takespecial lossof humanlit'eor havescvere groups solvethesame to precautions.nputatiorral of errorsand possibleillconditioned the with a difl'erent step size or methodand mentsmay involve repeating computations analysis seehow our solution to changes We comparing results.s.try aswe may.This can sometimes done by seeingif the resultssatisfy to as be the back someconditionor equation a check. Blunders usually are in ol'rrurnerical rlethods. AI Er i.We may want to try different numerion computathat havedif'tbrent theoretical foundations.1 Blunders We areall f'amiliar with gross errors.5.hernltical rnodeling and canconThey can be rvoicled <rnly soundknowledge by tributeto all the otherconlponcnts err<lr. exAn ampleof a negligible moclel erroris thefact thatNewton'ssecond doe not account law s fbr relativistic This doesnot detract eff'ects. Throughout lhis book.therearc usually sirnpleways to check ln whethcra particular nunrerical methodis workingproperly. Thus. er tn PROBTE 4.l S t e pl : S e t r : I .thc thought ol' as lhc srnirllc givcs a numbcI grcrtcr th idtr ca1\. addition.2 Convcrt the lirlloui 7 I 563 and 3. Horve we ver. process Blunders can occur at any stageof the rnat. 4.5 AND DATAUNCERTAINTY BI.srelatcto biasthat can be ascribed incorrrplete to mathu'matical rnodels. dueto thef actthat. n. to your to Finally you shouldbe prepared pertbrm numericalexperiments increase to problols. may employsensitivity the or when we changernodel parameters input values. PR be Ju as ca ca w( 4.t drr r\o\'td . they must alwaysbe kepl in mind when applyingnunrcrical problcms.andmostblundcrs impert'ection.r. haveditferentconvergence or are When the resultsof numerical computations extrcmelycritical and may involve it to c.1 Convert thc lirllowi l0l l00l and 10. nic.ues thc contextof realworld in ist lo As th 4.thegoodplogramnring tlratwereoutlinedin Chap. fiorrrthe adequacy the solutionin Exanrple of l.(X)10 4. lle th hit es ifr Ltl\ 4. based dit'terent arc cal algorithms propcl'ties stabilitycharacteristics. This may involvethe ussof two or morc indepcndent problenr that theirresults canbe compared.
7).rhcr ratcdby your' to go Olherwisc to Stcp3.5.theyprovidca rnsasurc ol'Lhc biasandirnprccision. we arcdcalingwith a qrrcstion ol'prccision. rcspcctivcly. . For suppose wanted testthe bungee we to jurrrps his ing an individualrnakerepeated andthenrneasuring or hcr velocityalicr a spccrfied time interval. instmments and If our consistently underestirnate overor wc or dcvicc.5. d r . the nrostpilrt. as in Eq.PROBLEMS r 09 scales with the brrngee because theseerrorsare rninimalon the timc and sDace associatcd jumper problern. l Convert lirllowing bascl{ nurnhcls to basc l0: the and 1. r r . 3c l o v c k r p o u r o w n positivcrcal nunrhel M l'ilcto dctclnrinc srnallest thc usedin MAf LAts. 23 hits li)r thc Convcrt lirllowing basc2 nurnbcrsto basc l0: thc l 0 0 r n dI 1 0 . r l i r i n . . 4 I n a l n s h i o r s i n r i l a lt o P r o b . ns Uncertainty bc jumps thanduringothcrs. Bascyour algorithrn the notionthatyoLrr on cornol as thc smallcst nurnbcl that whcn addcd to onc grcatcr huscdon this putcrwill hc unablcto rcliablydistinguish bctwccnz.i ln u r t c t i ort. ( X ) 1 0 1 .ifyou are cause ofmodel error. anduncertain clata.Each valuc in 5 : t= 2 x t is storcdin :1 hytcs with I bit lor thc sign.indepcndently can Thcrelilrc. boththeanalytical numerical and solutions obtained Chap. Forcomputcrs. will retLrrn Wc to (hctopicol'charactcrizing rrncertainty whenwe discuss regression PartFour. o o n o o As such. have a souncl l'actors.t1 r r .MATLAB allows 3:t = t:12 1:Return Stcp2 to nuntben lo bc cxpresscd single prccisiolr. However. rrroclcl errors.cro rnd a nurnbcr than l. An algorithrn l . are Measurernent by thc errors can bc quarrtil'icd srrnrnrarizing datawillr oneor morewellregarding specil'ic choscns(atistics convcyas much inlbrrnation possible lhat as character( statistics most olien selected represent I) the isticsol' thc data.On thc otherhand. takingthc base logalitlrrn ol'thenun. arcdcalingwith an inaccuratc. estimate velocity.1. y 4 .5 Although it is not conrrnonlyuscd.and wc are dcalirrg with crrorll'cc rncusurcnrcnts. thesc conditir)ns.but is relatedto velocity and otherfactorsin a diffbrentway.3 Dofq Uncertointy in Errorssomctimes enterinto an analysis because uncertainty thephysical of dataon which jr"rmper modelby hava rnodelis based. nuthe lilr rncricalrncthods uscdfirr bLrilcling nroclels bc stndicd. ftrr we nrodel. r I m i r . instance. 1 . in dala Althoughyou mrlst cognizant hc o1'blundcrs. Undcr en'ors. rlost of thishook.I wouldbe erroneous beYou should cognizant that. thcn go to Stcp. wouldundoubtcdly associatcd thcsc with mcasllrcmcnts.Thcscdcscriptivc arc to l o c a t i o n f ' t h cc c n t c r l ' t h cd i s t r i h u t i oo l ' ( h cd a t aa n d( 2 ) t h ed e g r e e f s p r e a d l ' t h ed a t a . c a n cd c v c l r r p c d s h a q u a n t i t y h a l i s s r n a l l c t h a n t h i s n u n r b e rN o t c t h a l t h c t a rcsultyou obtain will clil'l'cr ll'onrthc valLrc conrpLrtcd with l:Sets: l. hiascd. ( 1. suppose airresistance not proportional thesquare that is to ofthe fall velocity. be ofthis typeoferror andrealizc model. C h a l l c n g e u c s t i o n : n v c s t i g a t eh c r c s u l t sb y q I t 2 gcne 2:Il'I * e is lesstharror eqrral l. can studynunrcrical wc errorswitl'toul crlrlplicating PROBTEMS Writc your own M l'ilcbascd this aluorithrn dctclnrirrc on to hy it thc nrachinc psilon.Validatc lcsLrlt comparing with e thc ' t h c v a l u cc o r n p u t c w i t h t h c b L r i l t . would flll taster Thcsccrrorscancxthe parachutist duringsome hibit bothinaccuracy irnprecision. workingwith a poorlyconceived 4. 3. c o d ca n c t h o s c h t a i n c d i t h r r . the if the measurements ranclomly high andlow.no numerical method will provideadcquatc rcsults. If suchwere the in case. l o w zl.we will assr"rmc wc havcnot lnadcgross that ol'thcsc errors. rnachinccpsilon c can also hc thc .
. and thirdortlcrvelsions and contputc lr:r lirr cachcascthc I 1.r 0.12 Use zero. . rir'scxpiln)ii{)r).tcalculnloror MATLAB to dclcrrninc Usc _vour thc centcrcdllnitc dil'lcrcnce approximations lhc firstand lirr valucol'thc approxvaluc.Altcr cachncw lcrm is acldcd.lI n'. Evaluatc c>l' irt . pockr.I0. tlris Irrnction Computc the true percenlrclativc cror r.l0 lirr this scricsli)r r : 0. dcr term ol'lhc Til) ltrr se (a) (b) Rcpeat hut cxplcssI'as . thc at with part (a).37.1.5sin.tt (a) Evaluate polynornial thc cstimalc thc l'irst dcrivativc ol' the firnctioncxamincdin I:.f.1 o\ Yea to s The sen time RcpcatProb.2.Add tcrnrsuntil thc ahsolutc trLrc scconddcrivativcs as to graphically so whichapillustratc imaterrrorcstimatcI'allsbclow an crror critcrionconlirrmproxinration mostilccrrlltc.r .r]. ing in a rnaximutn crnrr ol'0.1.: 0..21 3! 5! 7t l'crence ).ldigit arithmcticwith mation.and 8 bits fbr the signedexponent. I .r: l.05. 19 Consitlcrthc lirnction l'(r): rr . 1 7 l l l . on thc perccntrclativc crror. (a) Provc that this MaclaLtrirr \criL'\ rr\piln\irur a spucial is (Eq.r. Compare rcsultswith the true valueof the with r:hopping. approximations along with the theoretical. thc oI at . l ' ( .l ProvethatEq. r i * r : I l t : lirr.. Dctclnrinclhc highcstolclcr'laylor scricscxpansion resulf .Usc 3digit arithrnclic your results the hasisol'thc remainInterprct dcrivativc.t l .l UsezerothroughfourthordcrTaylor serics expansrons (I .35 the in cstimate second dcrivativcof'thc lunctionexamined .r.rl . basisol'thc rcrnaindcr 1 .r. 2 1 w i t h l : 0 . S t a r t i n g i t h t h c s i m p l c sv c r s i o nc o s .l. Evaluutc clror and cornpare the Curnparc your cstimatcs with thctrue ol'/r . but finitc di{lercncc as anddo thc samelix lhe sccond as clcrivativc wcll.4. . l3) with . v a l [ 2 . firl each appsoxiDo you cxpectto havc diff iculticsevaluating Discuss rneaning thc results. Enrploythc zcro. ) at a tinrcto estirnate cos(z/3).15 Prob.{ r"Iitlr l.tro ERRORS ROUNDOFF AND TRUNCATION 4.ell as and largcst as the machine epsilon lor single precision represcntation. 6! ttl Thc error is cc.15 Usc lirrward rnd backwarcl dillirencc approximations chopping. 4. i n t :S o l v cg r a p h i c a l l y .r _rr' 4. cxpansion the sin r to cstirnatc lbr use thc Maclaurinscries 4.612 7x . Note that the exponcntsrangefrom . Exanrple at 151 .7).r. to predictl(2) fbr f(r) : In "r'usinga basepoint at.9 Thc lirllowing infinitc scricscan hc used to approxivaluc ol'thr' sccontl dclivativc.1. .l. 4. Pcrlirrnr evirluation .3xr): is givenby ot the usinga bascpointat.. 4.126to 127.r) = l/1 I . ( b ) U s c t h c 1 ' a y l o s c r i c s o c s t i n r a t c .2.+ Sln.10The Maclaurinscricscxparrsion cosr is lirr ll R r: i' t 2.16 Usc a ccntcrcddillercnccapproxirnation 0(h2)to of : ((. 10.: 1.30). l. .22 RepcatExarnple4.r if'l(r)= urz+hr+c.5771 Try it using 3.3rt).zl. (4.l'irst. I .r' Prob. .Intcrprctyoul rcsults the on n l a t cd ' : tcnr ol'thc Taylol scricscxllunsion.ll Perlbrm thc sarncconrputation in Prob. but lbr l( r) : cos(.\ = n 16.r) : 251r.2(l Dcrivc Eq. for eachapproximation. have wc t0 a s o l v ct h c l u n c t i o n l(r'): t . 12.throughthirdordcrTaylor sericsexpanslons to pre'dic:t.I I I is exactfbr all valucs of.5.1.2.Determinethe smallcst positivefloatingpoint numbcrs u.25 and0.r) . r l < I i t i s k n o w nt h a t rl .rr7r:+u. Compute truepercent relative error c. computcrin Exarnplc provethatthe rnachinc epsilonis 0.(4.cascol'thc Taylor sclicscxpansiun 0 ancllr : .7Thc derivative l(.4 on the intercornpLrte thc truc and appr0xirnatcpcrccnt rclltivc crrors.21 Repcat 4.6 For the hypothetical base10 4.1. .l(3) firr + I (. r : l . U s et h c l i r r w a r db a c k w a r d . yoLrr h : 0. a d dt e r n r s n e bctwecn w t o (H c x p u n s i o n .ualto thc absolutcvrluc ol'thc dill'erence thc givcn lunction and thc specilicTaylor series . a n d .f (Eq.r * 8). 4. 125. sccond.ro.r.Evaluato clcrivativc : 2 usinga stcpsize the at.rf r(' .llJ To calculatc planct'sspacccoordirrates. zl. 6r 4. but lirr thc torwilrd divideddif: \ . '1.2. is Craphall thrccI'ilstderivafive ing to two signif icantI'igures.^ Lct thc bascpoint bc u: xi : rf2 <tnthc intcrval[0.r l.rS intcrval. of to ctfO(h) and a ccntercddiff'ercnce apprtrximation O(ft2) l.0.0 15 on thc specil'icd C(r\\l I I ) 4'.l. 2 5 . s i n ( z / 3) . r )c ' a t .+ .r = l.88 mantissa.and .\': 2 usingslepsizes 12.r : 0.5.
ln suchinstances. there are many other functions for which the root cannot be determinedso easily.anotherguessis made. you leamed to use the quadratic formula *b+4F4ai (PT2.1) LU to solve fk):qf+bx+c:o (Yrz.They represenl the valuesolx that make Eq.. (PT2. Although graphicalmethods are useful for obtainingrough estimates roots. Before the adventof digital computers. which represents the r value for which/(x) : 0.Rootsond Optimizstion 2.. of Although the quadraticformula is handy for solving Eq. of they are limited because their lack of precision.:.there were.2) :.2'l equalto zero. of An alternativeapproachis to use trial and error.as the with Eq. tPT2..l).. in ltl ' :.For this reason.1) are called the "roots" of Eq. roots could be obtainedby direcf methods. The valuescalculatedwith Eq.many more that could nol. (PT2. The processis repeateduntil a guess results anfir) that is closeto zero.If not (as is almost always the case).For some cases. rootsare sometimescalled lhez. Althoughtherewere eQuations like this that could be solved directly.eros the equation.2). This "technique"consists guessing value of x and evalof a uating whether/(r) is zero.and/("r) is again evaluated to determine whether the new value provides a betterestimateof the root. (W2. is the root. One method to obtain an approximate solution is to plot the function and determinl where it crossesthe r axis.2). onlv alterthe native is an approximatesolution technique. (PT2.therewere a numberof ways to solve for the roots of suchequations.1 ovERvtEw Yearsago. This point. .
computer solutions. level of precision. can terminingthevalueat which the functionis f'lat. secantmethods. fbr searching the functiorr'sextremepoints.Chapter5 focuseson brackthat bracket.tonRaph. and .r) = o .Two specificmethodsare the root and then systematically position.The detennination suchoptimal valuesis ref'ened as of to As you learnedin calculus.Numerical methodsrepresentalternatives that are also approximate but employ systematicstrategies home in on the true root. Root location involves for optimizationinvolves searching the locationwherethe functionequalszero.anotherfeatureof lunctionsof interestto engineers and scientists its are minimum and maximum values.most practicaloptimizationproblemsrequire nunrerical.From a numerical standpoint.thatis.ation. 2.Although such analyticalsolutionsare sometimes feasible.rolr. As elaboratedin the to following pages. Graphicalmethodsare usedto provide visual insight covered:bisectionand.the combinationof thesesystematic methodsand computersrrrakes the problemsa simple and efficient task. whereits derivativeis zero.In contrast. We illustrate severalopen methods including theJixedpoint iteration.1. computational effort is requiredto estimatethe root to a prespecified Thesemethods Chaltter6 coversopenruetltods.f"(r) < o f(x) = o . fbr involve guessing searching a locationon a function.2 PARTORGANIZATION in The first two chapters this part are devotedto root location.rt2 PART ROOTS 2 AND OPTIMIZATION l'(. are Thatis.suchsolLltions be obtirined analyticaltyby deoptimi.The fundamental and difference both betweenthe two types of problemsis illustratedin Figure PT2. Nev.such numerical optiwe mizationmethods sinrilarin spirit to the root locationmethocls just discussed.false Error formulationsare developedto help you determinehow much into the techniques. We will discoverthat iterationsbut do not requirethat the initial guesses efficientthan bracketingmethodsbut that thesemethodsare usuallymore computationally they do not always work.or contain.Thesemethodsstartwith guesses reducethe width of the bracket.l'('r) = 0 f"G) > 0 PT2. solutionof most appliedrootsofequations Besicles roots. also involve systematic trialanderror bracketthe root.I FIGURE the A funciion o singlevoriobleillustrciing difference belween rootsond oplimo of methodsareobviouslyinefficientand inadequate the requirements Suchhaphazard for of engineeringpractice. eting methodsfor finding roots.
a that combines search and quadraticinterpolation. re nt )h or at at nt .we then discussa hybrid approachcalledBrent's rootfindingmethodthat exhibits the reliability of the bracketing rnethodswhile exploiting the speedof the open methods.f zero. Chapter Z dealswith optimization.2.which again is attributecl Brent. describe In we MATLAB's excellentbuiltin capabilities this task. and K n. 6 endswith a brief discussion special of methodsdevotedto finding the rootsof polynontials. pafticular. After describing and illustrating fmlnbnd. the last part of the chapterprovidesa brief descriptionof optimization of multidimensional functions. After illustrating how f zero canbe usedfor engineeringand scientificproblemssolving.As such. hybrid approach goldensection Then.we discuss robust. the chapter problems endswith an exampleof how MATLAB can be employed solveoptimization to in engineering science. Chap. for goldenwe two bracketingmethods. Finally.for finding the optima of a function of a single variable.This approach. fbrms the to basis for MAILAB's onedimensional rootfinding function:fminbnrl.The emphasis on describingand illustratingthe is use of MAILAB's capability in this area:the fminsrearch function.2 PART ORGANIZATION rr3 S L ' Following the descriptionof theseindividual open methods. describe sectionsearchandparabolic interpolation.First. it fbrms the basis fbr MATLAB's rootfinding function.
You know f}om your previousstudiesthat the fbllclwinganalyticalsolutioncanbe usedto prcdictfall velocityas a functionof tirne: lntf^t ' r ' ( r ) : . / i . the orl Cu tl4 . ' .r \V rtt / rsrr Try as you might. what rootsproblems and wherethey occurin cngineering Uncierstaniling nrc and science. l : . a methodand its shortcomings.\ Roots: Brocketing Methods N et rf 5.  rt_BlE Fund3 s.l Fundomentol Principle Heotbolonce Mossbolonce Forcebolonce Energy bolonce Newton'slows of motron Kirchhofflows s De Vc T^. Your bossat the bungecjumping you compirny wants to detet'mine ntassat which this criterion is exccedecl the given a drag coeliicient of 0.. this to you cannotisolate masson the lefi sideof the equation. Knowing how to determine root graphically.25 kg/rn. Knowing horvto cstimate errorol'biscction the why it difl'crsliom error ancl estimates othcrtypesclf root locationalgorithms. A ar dr rh at pr ra tic pa mi pe an ca YOU'VE GOT A PROBTEM j h e d i c a ls t u d i e s a v ec s t a b l i s h ctd a t a b u n g e eu n r p c r ' s h a n c e s f s u s t a i n i n g h c o a signilicant injury increase vertebrae it'the liee1all significantly velocityexceeds 36 m/s afier4 s of fiee fall. .r i r n h. Llnderstanding incremental the search Knowing how to solvea rootsproblemwith the bisecl. objectives topicscovered Specific and are ' ' . fbr Understanding lalsepositicln how it difl'ers and fiom biscction. you cannotntanipulate equation explicitly solvefbr rnthat is.r R CHAPTER OBJECTIVES Thc primaryobjective thischapter to acquaint with bracketing you of is mcthods fbr findingthe rootof a singlenonlinear equation. Cc of Mc ofl Ch AC( V t.ion rnethod..
as posed at the start of the chapter. or An examp\e of such a mode\ is the equationfor the bungee\umper's ve\ocity.'Jrt dtpendtrt.:. mathematical in equations modelsderivedfiom or theseprinciplesare employedto predict dependent variablesas a function of independent rurrr.'. 5 l r e r r g t lo l r n o l e r i o ls l r u c i u r cp r o p e r le s .we call thisa "roots"problem.rm'trs.. torii.AlthoughEq.'\:.n o r l r a q a r"f /ro<r<fnnr a opollronra induclonce) . If the pa. is said to be irrrnlicit..:':.r:'.\.r.. As introduced Chap.set in tinre ma.roots of equationsfrequentlyoccur in the principles areaof design. dissipoiive of potcmerers F l o r t r l r' . This chapter you to how will introduce the computer usedas a tool to obtainsuchsolutions.un(..::':r. 5. such arrd /r. parameters.:.^\'::^\j'::'.aa picpeiites a: ratai a: :i:ie': Ee')"4"/ C h e m i c o lb e h o v i o ro f m c l e r l o l .cr. cannotbe solvedexplicitlyfbr nrass.:'j.F') . is 5.ss a jumper with a given drag coefficientto attaina pre..'s.I R O O T S N EN GIN E E R INA N D S CIENCE I G Although they arise in other problem contexts. 2 1 Now we can seethat the answerto the problem is the value of ru that makesthe function equalto zero.dpN. ::] lorts ol ewlons )l of rchhoffiorrys s p o t e n t l oe n e r g y l Acce/ercllion.b\t:. I listsa numberof tundamental thatareroutinelyusedin designwork. Table5. 00lonce tenpeteioie o Concenlrolion r quontity of moss M c a n i t u d e o n d d r t e c lo n T i m eo n d p o s i l i o n T i m eo n d p o s i t i o n .I G 5 .i\'.i:..rlh'i. (5. \ote \hrt \r.1) provides mathematical representation the interrelationship of a In it amongthe modelvariables parameters.it)rl Poromelers Itei.m o s sl r o n s f e r .'. whereasthe pararneters of represent its propertres composition. or ocotron Currenlsond vo/toges T r m eo n d p o s r t i o n Lme qeometry Alcrss molerrol syslemgeomefry.veloci\'.i.):l:. r(m) (5 .'.:r.rsi.'. s y s l e r n r l D0iOriCe bolonc. However.l n r . it is isolated one sideof the eqLral on sign. r'*i.n.'.Hence.suppose that we had to determinethe velociryt a . 1.ables ret'lectthe stateor pertbrmance the syster. That is.1*.scribed fbr period.g{i. .'i:.:.''..' ) S 1 :.). t principles Fundomentol usedin designproblems Dependenl Vorioble Independent Vorioble ine CtC pOS.:. cases. I R O O T SN E N G I N E E R I NA N D S C I E N C E tt5 An alternative way of looking at the probleminvolvessubtracting u(t) from both sides to give a new function: :. syslem(leomelry .
n a t io fnt h er o o t . a o wh E X A M P5 .qrr11 (. by Thus. q c ). p r o v i d c s r o u g ha p p r o x i r . v '. 8 1n r / s r ./(r) :0 is to make whercitcrosses a plot ol'thc firnctionant'l observc thc. theseproblemsoften reqrrile determination implicit parameters.rs conl'ronted rorrtincly cnginecring science. l ( r l : 0 .Thesetechniclues be appliedto many can in otherproblcn. / n l r ) * L ) .Thisvaluealsorepproblem. I f ) = r i q f L ( r 1 * m 1 rc : c 1 ) * t .3 "50 150 I I] e . 2 )v e r s u s a s s : '' ' ::' r'ril = nlll '' I = 4. a n l r ( : .. q r 1 ( . ' 1 r. rv a l u el b r w h i c h .1)by it to Eq.25 kg/rn to havca velocityol'36 m/s after4 s of free i 1 a l l .f(m):0is. the of tbr by methods rootsof equations. J * c d .which represents t h e . variableu frornboth sides the equation give Eq.11r). resents rnass thc that solVes clesign the fbr The lbllclwingpages and graphical methods deterdealwith a varietyof numerical rniningrootsof relationships suclras Eq. T h c l i r l k r w i n g A T L A B s c s s i o n c t su p a p l o t o l ' E q .r. plot (rrr.2).3 pro cal t ans whi par 5. (5.tt6 ROOTS: BRACKETING METHODS many designproblemsinvolve specifyrng the This represents real dilemma. l l 1 . Use thc graphicalapproach dcternrine nrass the bungee of jurrper with a drag coel'l'icicnt ol'0.\.because a properties cornposition a system(as represented it.N o t c :T h e a c c e l c r a t i o n ' u r i r v i t vs 9 . I LE TheGrophicolApprooch to the Problem Stotement.thercfbre.This point. Ho ma un me ab pl( Cv !' ro ar ha Fc tr rh te 1 z pr u ri 5.spararneters) ensure ol of by to thatit perfornrsin a desiredmanner(as represented its variables).2. 1 .raxis. . is conventional reexpress (5. (5. and 5.).( 5 . '\6. of The subtracting dcpendent the to valueof'nithatrnakcs.therootoftheequation. The solutionto thc dilemmais provided numerical To solvethe problemusingnumerical methods.2 METHODS GRAPHICAT A sinrplenrcthodlbrobtainingan estir. . r ol m M s S o l u t i o n .nate the root of the ecluation of .2).
rr)and .1 showsa numberof'ways in which rootscan occur (or be prescribcd a lowcr bound.graphical methods can be utilizedto obtainroughcstimatcs gucsscs numcrical in lor mcthods discussed this chapter. The validity of the graphiof can by it cal estimate be checked substituting into Eq.Figurc5.lherearecases wherethcy do not holcl. n c x a r n p l c l ' a I ' u n c t i o t h a ti s t a n g e n t i a t l t : ' t t h ca x i si s t h ec u b i ce q u a t i o n/ ( .4 ) .r. 2 aa n dd i s c o n t i n u o u's n c ti s to p A o n tl t i o n s( F i g . / ' ( .r1 lb by andan uppcrbound.oa x i s . are Craphicaltechniques ilf limited practical ol'roots. Visualinspection the plot thc providesa roughestimate the root of 145kg (about320 lb).. makeguesses until the function error" to come up with the root.(g*cd/1. 5 . you'd repcatcdly wasgrea(ly lacilitated the advent sofiware by of was sufTiciently closeto zero. w h e r ( . tt r r u l t i l t l c r t t .TheseestiHowevcr.r .r. thc havethe sarnc sign.l ) along with the parametcr values liorr this exarnple to give ( >>..3 BRACKETING tt7 of The functioncrosses llr axisbetweenlrl0 and l50 kg. of by arean odd number rootsin the interval.. ) ol'rootsbctwecn values. / (.Thiit is.5 . and Canale.graphicul interpretations usefirl are Asidefiorn providing ol'the lirnctions and anticipating pitlallsof the nurnerical the understanding ploperties the rrethods.2 of of guaranteed locate the rootsin an interval.) v 0. appliedrnathematicians. rnethods the described thc lirlkrwingsccusedin conjunction routinelyby cnginccrs. Althoughthcse l u ) F o r c x a r n p l e .whcn proof computer algorithms to all in with graphical approaches. l n t e r m si n t h i s p o l y n o m i ac q u a lt o z e r o . r : 2 i s c a l l e d' . ) a r e a l s o o n o p p o s i t e s itd c s.3 BRACKETING you'd oftenbe told to use"trial and If you hada rootsproblemin thedaysbefbrccomputing. It can also be checked by substitutingit into Eq. that Although they are bcyond the scopeof this book.anh(scTrtq*cd/ r45) *t) 35.5. mates can be ernployed starting as firr roughestimatcs ol'tlreroot. r: 2 t n a k c sw t r .0456 which is close to the desired fall velocity ol36 rr/s. s h o w s t h r c c /c In if havcoppositc signs. r n c t i o n t h a ta r ct a n g c n t i a l t h c . (q*14filco) *t. and METHODS AND INITIAT GUESSES 5.2 ) ( x .r r ) a n d .thereareeitherno rootsor an evennunrbcr gcncralizalions usually arc lrue.The process . general.i5)*t. For cxample. . expressly designed locaternultipleroots(Chapra to in nrakes difiicult to dcveloptirolit The cxistencc cases thc typedepictcd Fig. F i g .M a t h e r n a t i c a l l y .2)to yield >> sqrt (g*145/ccL)*r. r. thcrc rootsoccurring within the interval. valuebeciiuse thcy arc not vcly precise. indicatcd Fig./ wherea singlcroot is brackctcd ncgative positive he r f e v e r .f (. thereare specialtcchniqr"rcs arc 2002).2 ) ( .METHODS AND INITIAL GUESSES 5.5.) As la r'. N o t i c ct h a t ./ (r.anh(::qrt.0456 which is close to zero.. . r a x i s( F i g . 2 b )c a nv i o l a t e h e s c r i n c i p l c s . (5. dcabsent) an interval in (r).r ' ) ( r .5 ..sqrt. ancl il' l (rr ) and. Howevcr. Howpictsthecase by and values o1'.Fig. 5. confl'onted tions are extrerrelyusefulfor solvingrnanyproblerns scientists. l r l . (S.
of for on even numberof oxis inlercepiions the inlervol.) hovethesome be no roots there be on evennumber rools will of or h w i t h i n h ei n t e r v oP o r t ( b )o n d ( d )i n d i c o tte o tl f l h e t l s ih w f u n c l i oh o sd i f f e r e n t g n s i t h ee n d p o i n t s .occur the function tongentic to the r oxis.. is ol'h. e r e i l l n si o be on odd number roolsin theintervol.ile s.nn Jho . occurrnon intervol (oJ thct on upperbound Poris ond (c)indicoie if both x../ (rr) ond . . S p e o l s t r o ' e g i e s ^ro . thoa^ ..o^. For thiscose. ".n< iho'c ore FIGURE . 1 (o) Multiplerootsfhot. (b) Discontinuous funciions where end pointsof opposiie ic s i g ^ o ' o c [ e t o n e v e nn u r r b e r .^^r.2 lllustrotion someexceptions lhe generolcoses lo of when depicted in Fig.tt8 ROOTS: BRACKETING METHODS xl (d) xu (bl FIGURE 5. [^.either . there wili sign.I 5 I lustrolion crnumber generol of woysthoio roolmoy of prescribed o lowerbound ond by .r.o. 5. .. or[o o ' s .ol lnr nolarmrn./(r.. 'nh lnc e"d nninls nr'p rf nnno..
. initial guesses required. suchtools can actuallymake the trialanderror approach attractivefbr someproblems. open to In the rnethods not alwayswork (i. sign on opposite sides the root. As that Brttcketingmethod.e. for many other problems. The two major classes methodsavailable of are distinguished the type of initial by guess. allowing you to make many guesses By rapidly. tive fashion. and f (x. with trial and error.... thereis no can but Opertmethotls.it is preferableto have methodsthat come up with the correctanswerautomatically. In general.. good initial guesses may not be obvicontextyou are analyzing. the namc implies. In is automatically to 50. . problems.In suchcases. contrast. needfor them to bracketthe root.1 Incremenlol Seorch you observed in that /'(.3 BRACKETING METHODS AND INITIAL GUESSES lr9 tools suchas spreadsheets.r7) signs. An Mfile can be developedr implements incremental that an search locate roots to the within the rangefrom xmin to xmax (Fig.r. The following ous./(x) is real and continut'rus the interval of if in from x7 to.in othercases. .An optionalargument of a firnctionf L. thescare basedon two initial guesses "bracket" the rootthat is. search the can be very time concl.there is a possibilitythat closely (Fig. But.3) rre site lies thenthereis at leastone real root between afld.e. automated approaches obtainguesses to would be useful.lf ns is ornitted.The problemis compounded the possible by exisspaced rootsmight be misscd tcnceof multipleroots.4).they typically take more iterations home in on the answer).r) changed When applyingthe graphical technique Example5. 5.. 5. .they can divergc). are Thesemay naturally ariseliom the physical In bothcases. the eventthat a sign change occurs! upperand lower bounds storedin an arrayxb. are on either sidc of the root. However. that is. if the length is too great. lnterestingly.rnc it ns allowsthe userto specifythe numberof intervals within the range.roice ol'the incrcment suming. section describes suchapproach.r1 by Ircrenrentalseun:hmethodscapitalizeon this observation locatingan intcrval scarchis the sign.5.) haveoppositc < /(r1)/'(x. the are ' This lirnction rs a rnoditledversionof an Mlilc originally prcscntcdby Recktenwald (2000).r.A f or: loop is usedto stepthrough set eachinterval. and .1.If the lengthis too srnall.. . They are . incremental one the search.A potentialproblcm with an incremental where the functionchanges length. 5.) 0 (5.3.but when they do they usuallycondo vergequicker.s. bracketing slclwly For wellposed the methods alwayswrlrk but converge (i. Thesemethods involveone or moreinitial guesses.On tho other hand.l (.3).theseapproaches as require Then they systernatically home in on the loot in an iteraan initial "guess" to get started.
2) = x (k+1).sign(f (k+L)) licheck for sign chanqe nb = nb + 1. e.5.l) xb(t'tb.n . x{kl.xmin. encl %rf ns blank sigtn chatrge sign change set to 50 search Z Increm€ntal x = I i n s p a c e ( x m r . % if nargin < 4.aLor ? incsearch: xb .(xb) ?displzrv that no brackets disp ( 'no brackets forind' ) or increase ns' ) disp ( 'check rnterval disp('nr:mber disp (nb) end ol brackets:') were f ound %drspLay number of brackets FIGURE 5.ns) : % finds brackets of x that contain sign chattges % on an interval. xb = incsearch(func.i I . unless sign cranqe det ec t ed nb = 0r :{b . . n s ) .1) is the lower bound of Lhe kth % 2 xb (k. length (x) 1 if sign(f (k)) ^.ns) function incremental search root fo.name of functiorr % Y xni .2 ) is tire upper bound of the kth If no brackef.incsearch (func. xb . ns = 50. of a function ? ? inpuL: furLc . xbrnl.. xmax cndpoitr(s oI inrerva] (default = 50) ? ns = number of subintervals % outpuL: xb(k.Ll. xmin.xmax.x m a x . xmax.3 the length lhe seorchprocedureis of whereroots couldbe missed becouse incremenlol Coses of o r o t t o o l o r g e N o t et h o t h el o s t o o t n l h er i g h i s m u l t i p e n d w o u i db e missedregordless the e i n c r e m eln ln g l h .xb is: null for k = 1. end end jf isempt).func (x) . f .s found.2 EXAMPLE FIGURE 5.4 on seorch An Mfile fo irnplemenl incrernelrtcl r20 .
3061 3. 3 . number of 9 3 . 6] fbr the function: J G ) : s i n (l 0 . t9\9 4. (5.100) 3 .'t14j.3939 .3 BRACKETING METHODS AND INITIAL GUESSES EXAMPLE Incrementql 5.r j 4. is J ?E 4.25and5. r ) (50) is Solution. usingthe zoom in tool.7 3.5 5. the functhe are possible tion rnisses rootsat .4) A plot ol'Eq.5. a.6531 \.36'. i n c s e a r c h ( t l x s i n ( ' lO * r ) + c o s ( 3 * x ) . 49 3. are because subintervals too wide. 6) numbcr (s.a)to identifybrackets within the interval[3.3536 possiblc rooLs: b. /34t 4.212.5. r ) * c o s ( 3 . UsetheMflle i ncsearch (Fig.6939 sin(10*x) rooLs: +cos (3*x).4)alongwith thc root Iocations shownhere. 2 1 .t3 3. The MATLAB session usingthe detaultnumberof intervals >> incscarch(@r of possiblc 5 afnS 3 .However.2 Seorch t2l ProblemStotement.3061 3.2 424 3. it is clearthat eachrepresents real two rootsthal are very closc together. mots look like they might be Thesepossible by doLrble nrots.5 Althoughfive sign changes dctccted.2. The f'unction can be run againwith more subintervals with thc resultthat all nine sign changcs located arc > : .3.{:i2 l t.
1 ? . 13 5. 566J 3.5.4 BtSECTON The bisection ntctlu)d a variation the incremental is of search rnethod which the interval in is alwaysdividecl hali.oi Ex plc let Th 5.'flre is precision.122 ROOTS: BRACKETING METHODS 3.r. The first stepin bisection to guess is two values the unknown(in the present of problenr.t213 4.591 A <1 2 The lbregoing illustrates brutelbrce example that nrethods suchas incremental search are not lbolproof.2424 4.1B1B 5.1318 5 . thatgive valuesl'or t 0n') with dillelent sisns. The locationof the root is thendetermined lying within the as subinterval wherethe sign changeoccurs.2424 4. e Solution.You would be wise to supplement suchautomatic techniques with any otherinlbrnration insightinto the location thatprovides ol'the roots.2r2).Suchintbrrnation can be fbunclby pkrttinglhe functionand throughLrnderstandinc physicalproblern the frorn w h i c ht h ee q u a t i o n r i g i n a t e d . Usebisection solvethe sarne problenr to graphically approached in E x a r n p l5 . .Frornthe graphical rr) solution in Nr wl thr .1515 5. If a functionchanges in sign over itn interval. 5.2121 4.t21 4 . ':. until the root is known to the reqr"rired A graphical depictionof the methodis provicled Fig. The subinterval then beconres intervalfix the process lepeated the next iteration. firnctionvalueat the the rnidpointis evaluated.3 5 The Biseciion ethod M ProblemStotement.2'. l . t.. Nc lat EXAMPLE .5910 .The lbllorvingexample goes in throughthe actualcornputations involvedin the method. o FIG Ac f.t57ia 4 .
5 A grophicoldepiclionof the bisection method Thisploi corresponds the firsifour iterotions to trom Er.Consequently. ffi xt./rI .and henceno sign change which is greater occursbetween lower boundand the the midpoint...\r xu V Fourth iteration I Fo_./.1316 r2sx 1 0 0 ? 4 : 1 2 .Thc p l o t o b v i o u s l y s u g g e s t s b e t t e r i n i t ua l s s e s .r.0 . s a y 0 a n d I 5 0 .the initial estimatc the rtx>t. 4 3 4 / . value tlrerootis 142..l 142. 4 0 9 1 1 .4 BISECTION r23 .4 . Thcrcfble. create new intervalby redefining lower boundas 125. we a the .5.. 5 1 9 ( . root mustbe locatecl the upperinterval betweerr and in the 125 200.1316. 3 7 1 thanzero. l. wc can seethatthe t'unction changes signbetween values 50 clf and 200.omple 3 5 Example5. at thc midpointof the interval of lies .7316 I Next wc compute productof the functionvalueat the lower boundandat the midpoint: the : I ( 5 0 )I ( 1 2 .t FIGURE 5. b u t t i l r i l l u s t r a t i ' u ' t ' p u r p o s r .50+ l(x) _  )< . means thc value 125calcuNotethattlreexact of This that of lated here a trueDercent has error relative of r.Therefbre.f(nt) 2 U 2 6 First iteration V Second iteration I xl "r/ I t Third iteration # xt )(f x.' \ gi e l4 lct's assume clon'thave the bencfitof the plcltand havc madeconservative we guesses.5:1 . l i 112.
rnustnow develop olr. S u b 25 er d e s t i t u t i n gh e s e a l u e sn t o E q .1 . 5 . .101)()(.5o c o n r [ ] u t t h ee r r o r s .. the rnightbe to end the calculation when the errrlrl'allsbelow some An initial suggestion p l c s p e c i e d l e v e l .re. 3 u n t i l t h e a p p l o x i r n a te r r o r t a l l s b e l o wa E 5 e s ( o p p i n c r i t e r i o ro l ' € . roo( is now irr the lower intervalbetween125and 162. tulrcady thal on ol'the There lirre.jective of We an criterion deciding lirr when a lel'ined to terminate rnethod. A revised l'ootesti 125 20t) + 2 r v h i c h r e p r e s e n t s a t r u e p e r c e n t e rlre .5 . Therelbre.t h c t r L r ee l a t i v e r r o r d r o p p e f r o m l 2 . T h e p r o c e s s c a n b e r e p e a t e d t o o b o 1 tain refineilestinrates. U s eE q . t h e c o n r p u t u t i o ns s Icillriilulc(1.r]1""is the loo{ lirr the present iteration and rf'l'lis the rool lionr the plevious itemi t i o n . C o n t i n u e x a r n p l e . upperbound the The tirr is rede[ined l 62.xarnple with the statement the rnethod 5.. .08c/o I6t.: 0 . W h e n c .'l:lff]l(x)%' (55) whc. l u rl. nrethod be repeated The can until which represents percent a the resultis accurafe enoughto satistyyour needs. strategy llawed because errorestlmates This tlrr in theexanrple were based knowledge ol'thetruer(x)tol'the firnction. y i1 l d s t v i 5 .( 4 .4 We thtt duringthecoulseof'thecornputttion. .:. :200.3.l 3 w a ne l 6 2 . . T h e r e s u l t s o l ' t h e f i r s t t w o i t e r a t i o n s l b r E x a r r p l e 5 . ng e i E q .anclthe root cstinrate the third iteration calculated as is as 125 162. 4 3 f i. 5 )  _ 1_r1c' .75 ) .lr:o f3 .J09(. ( .5. t) e t S o l u t i o n . : 0.the new intervalextends rnatecau thenbe calculated as l25to r. b e c o r n c sl e s s t h a n a p r e s p e c i l ' i e d t o p p i n gc r i t e r i o n e .y.1l : 6 . This woulclnotbe i t h e c a s ci r r a n a c t u u l i ( r .F o r i n s t a n c en E x a r n p l e . f ( t t s l .5 I Fig exh __*+ . example. 8 5 % . For 0.'lil 1 T] te to T} on Fl Er of lo'.say..5%. 3 .5 * :  43. i relative elrur of a.. 11 1 2 . lrAl 5r?sl #ll\l0o/.5 .5:. r a t i o n c r u s c h e r ew o u l d b c n o p o i n ti n u s i n gt h e r n c t h o df w e s be t knew the roo(. rnightclecide we shouldternrinate is the when the errordropsbelow.59) (t.409(0.0. couldbe continued obtain We endedE.3 that to r'stimate therrxrt. EXAMPLE .wc recluire errorestirnate is not contingerrt lbreknowledge an e r o o 1O n e w a y ( o c l ot h i si s b y r ' s t i r n r L t iu r rl p p r o x i r n r r tp e r c e nlte l a t i v e r r o l ' i l sn I r e c a l l .124 BRACKETING METHODS ROOTS: fronrxl : At this point.5 % . .4 5 s E r r o rE s t i m q t e f o r B i s e c t i o n P r o b l e mS t o t e m e n t .5. 5 r e d t<t0.:23. .
6 suggests la.5c/o. Theseresults summarized Fig. FIGURE . The true and approximateerrors are far apart when the interval happensto be centered on the true root.. le.l l7"l 1 24 3 t385 a7l 586 258 093 0il 030 50 125 125 125 1 3 43 7 5 t39 a625 l4t 4463 t 4 25 7 B l 204 204 t 6 25 I43. They are close when the true root firlls at either end of the interval. plot the exhibitsthe extremelyattractive characteristic le.85%.l... 5. true root can lie anywhere the within the bracketing interval. ...7 5 143.4 BISECTION 125 Recallthat the true percentrelativeeror for the root estimate 162. ond opproximcte True errors piotted ore versus number the of iterolions. In addition. 5.l ("/"1 2 30 B 1 34 4 698 337 )66 o82 o4l le.z o rais (t) rl or Although the approximateerror does not provide an exact estimateof the true error. CS be VC he all o 1n Approximateerror.l .l. This behavioris manifested the other iterations: for Iterolion xt xu xl le.6 5 Errors lhe bisection for meihod. le. Thus. and the computation be can le.6. I. captures general that l the downwardtrendof le.I is greaterthan le.. of Therefore.5. is alwaysgreater that l than le.75 1 4 37 5 1 4 37 5 1 4 37 5 125 t 6 25 1 4 37 5 1 3 43 7 5 r3 9 0 6 2 5 14t 4063 42 578t 43t64) In )n 1e +1 Thus aftereight iterations l finally falls below €. Fig.fbr bisection.The "ragged"natureof the true error is due are in to the fact that.5was 13. terminated.:0.
neatness its the of error analysisis a positivefeaturethat nrakesit attractivefor certainengineering and scientific applications. After eisht iterations.xt.1641 + 1.4.50 : 150. Otherroollocatingtechniques not alwaysbehave do as nicely.a generalfbrmula relatingthe errorand r t h e n u r n b eo l ' i t e r a t i o nrs i s E (" .. ..Art' . (5.Although bisectionis generallyslowerthan other methods.r) log(_r)/log(b). instance.beforestartingthe methodwe areat tlre the "zero iteration.1 the desired is error. : 143. ln general. "2 A.For Example 5.lf the pocket calculatoror computerlanguage you are usinl doesnot includethe base2logarithm as an intrinsic function. true root would haveto fall outsidethe bracketinginterval. = .ro : 200 .\D L If C..it can be to demonstrated that le.1) log 2 :. will alwaysbe greaterthan le.. we could rnakethe definitive staternent that x. we know that the trlle root lies somewhere within an interval of Ar : xu .5859):8 I MATLAB providesthe I oq2 function to evaluatethc base2logarithmdirectly. absolute the errorwAS tr . This can be seenby recognizing that beforestarting technique.4 was terminated. absolute the the erroris sll:r!lrro:AxO where the superscript designates iteration.(#) 0.For this boundto be Eq..6) Let'stesttheformula..l for bisection..r\tf E." After the first iteration. before startingthe computation.126 ROOTS: BRACKETING METHODS when le.."r.13. initialinterval the wasA.this ecluation be solvedfbri can log(L.(5.5781 : I ' 1 3 .Hence.This is due to the fact l that eachtime an approximate : root is locatedusingbisectiofloS. (xt * x) /2. Another benefitof the bisectionmethodis that the numberof iterationsrequiredto attain an absoluteenor can be computedu priorithat is.(.I falls below s' the computation could be terminated with confidence that theroot is known to be at leastas accurate the prespecified as acceptable level. exceeded. For when Example5.5) providesan upperboundon the true error.7500142.7s00 = Wecansubstitrrte values Eq. 5 8 5 9 l In essence.. l 6 4+ 0 .this etluation showsa handy way to computc it. Therelbre.6) give these into to n: log:(150/0.518t1 1r43.r.which by definithe tion could nevetoccur fbr bisection.the root must lie within *Ax/2 of our estimate..5859 (5.r" Because eachsucceeding iterationhalvesthe error. While it is dangerous draw generalconclusionsfrom a single example.142.log. errorbecomes the F'_ .
0.break.7.maxit.1 MATTAB Mfile! bisecr An Mfile to implement bisection displayed Fig. t h n f unct ion Iroot. 5. varargin) % bis.xu.p?...end . formula the tells us that eight iterationswould yield the desiredresult.varargin{ : }) .varargin{: } ).p1.es.xl. 5.er I bi secr (f unc.error('at least 3 input argurnents requi red'). * 1 . xrold) /xr) test = func (xl.end itcr.ect: r:oot Iocation zer.(func. (5. es. i f test>0 / €rror ( 'no si gn change' ) .0001. xr = xl..e n d i f xr = 0.For these cases. optional ln an stopping criterion FIGURE . Although we haveemphasized useof relativeenors for obviousreasons. ea = abs ( (xr .end test . . maxit=50. iterl=bisec1.end :lf nargin<5 i lsempty(maxit).tunc (xl. (x1 +.p2. <= Xl. ) : lroot.7 5 A n M J r l e o i m p l e m etn teb i s e c t i om e t h o d . (l) while xr:old = xr'. es0.xu)/2. the therewill be caseswhere (usually through knowledgeof the problem context) you will be able to specifyan absolute error.varargin{ : } ) *func(xu. xu. maxit.0001%) es = desired relative % erroi: (default (defaul t = s0) % maxiL = maximum allowabl e iterations pl. x r louer orrd . xl.pl.. rea L root ? ea = approximate : rlumbef of ? rter relative er:ror iterations (t) if nargin<3. It is passed function(func) is in the (es) alongwith lower(x1) andupper(xu) guesses. % uses bisection rnethod to find the root of func ? inpuL: func : name of function % ts xl . addition. = additional parameters used by func % % output: ? r oot .4. it..Fr gues:s = 0. ea.5859was acceptable. es >'0 iter >= maxit. 0 0 .varargin{: } ) *func (xr.6)can providea useful root locationalgorithm. xr'iLer = iLer + 1. if we knew beforehand that an error of lessthan 0. if test XU = < 0 XI.4 BISECTION 127 Thus..0. elseif xf el se ea end i f end l:OOt = ea =  test xr. end if nargin<1 I i sempty(es)...oes ? .5.ea. bisection alongwith Eq. .
locates root the it the .If not."n ( f (m): \..) .25lm) *4) 36.200 ) (9. 5.. 2002. f (.6089c 007 xu So 5. .5 Prc cal 4. Fr Fo mclSS 142.r.81*m/0. an error message displayed is andthe functionis terminated. . b y j o i n i n g .the shape the function an of of influences new root estimate.1 we generated plot of this functionversus a rltlssandestimated theroot that fell between140and 150kg. ) .Thus. l ( r r ) a n d . 5 .1311 is obtained kg after2l iterations with an approximate relativeerrorol'€.) .000053450/o. the Using similartriangles. 8 ) T h e i n t e r s e c t i o n ' t h i sl i n ew i t h w ol the r axis represents improvedestimate the root. sqrL (9. t bisectionalgorithmuntil the approximate error falls below es or the iterations exceed m a x it .It is very similarto bisection with the exception thatit uses diffbrentstrategy a to come up with its new root estimate..3 6 In Example5.81*11. The bi sect f unctionfrom Fig.7) Fir wh Se ..r28 ROOTS: BRACKETING METHODS and maximum iterations(maxit) can be entered.13'11 5.25) ea i ter] =brsect ( fm.)(x1 r.5 FArSEPOSTTTON Falseposition(alsocalledthe linearinterpolation method) another is wellknownbracketing method.f Gt) (5. 40. you have 36 t0 find the root of 5 .wecansubstitutetherootbackintothel'unctiontoverify that it yieldsarvaluecloseto zero: >:' fm(mass) is bis EXAMPLE Th 5. . ) i t h a s t r a i g hlti n e ( F i g . jumper's fieeftrll velocity Recall that you need to determinethe massat which a bungee exceeds rrls after4 s of free fall given a drag coefficientof 0.. l ( .for details). r .Thus. intersection the straight the of line with ther axis can be estimated (seeChapra as and Canale. The function first checkswhetherthere are sufficient argumentsand if the initial guesses bracket a sign change.81m @. b r e a k l o o p i s e r n p l o y e do i m p l e m e ntth e .25 kg/rn.Y.:0. alsoassigns It detaultvaluesif maxit a n d e s a r e n o t s u p p l i e dT h e n a w h i l e .1 025 9.3450e 0Olr q" ico ' pla 2.r Thus. We can employ this functionto solvethe problemposedat the beginningof the chapter.7 can be usedto determine the root as >> >> fmG(rn) [mass = *tarrh(sqrt..a resultof nr : 142.f (. Ratherthanbisecting interval.
3 .l (. n i t i a t ct h e c o m p u t a t i o n i t h l l u e s s e s f .8 5 Folse osition p relaerifY EXAMPLE 5 5 This is the.5 .7) then rep l a c c s h i c h c v eo f t h et w o i n i t i a lg u c s s c s . r 1. FIGURE .r.4 ...r7andr.r.rr) 6. 1 t The FolsePosition Method Problem Stotement. : 200 .rJ6029 . 7 )i s u s e d .rt : 50 'r. algorithmis identical the one for Thc tcl ( b i s e c t i o nF i g . A s i n E x a m p l e.5clo Second iteration: (5. 7 . i e l d sa l i r n c t i o n a l L r w i t h t h es a m e w r r y v e sign as l (.) : 2..fhl.litrmula. .scpo.0 .r.2(n) which hasa true rclativeerrorof 23. 8 6 0 2 9 1 . 2 1 l1 . 5 1 9 3 8 1.5 0 a n d i w o .sition. alwaysbracket true root. i s l S o l u t i o n . (5.) 0.592132 f .r.5./ (.ri) : '4.5 .519381 .7) .f (..).5 . vahre .:200  : l 76 .: 200. w i t h t h r :c x c e p t i o nh a rE q . r . F i r s ti t c l u t i o n : rcketategy 3 root ) with )n inlt line .5 FALSE POSITION lre ror ir the )ed 129 ter. ( 5 . rity 3to root rine 6.860291 r(50 0.The proccss of thc is repeated until the loot is estimated adequately. Use lalse position1o solvc the sameproblern graphiapproachecl c a l l ya n dw i t h b i s e c t i o nn E x a n r p l e5 . 1 a n c . . 3 . o/r ..\.computed The of with Eq. In this way the values . .
. 3 .) :0. : 176. e r e s u l t s a nb e s u n r m a r i z e d th c as Iterotion l xt X. S o l u t i o n . true error hasonly beenreduced about59%.{n which has true and approximate relativeerrorsof 13.16a/c 8. alier l'ive iterations. EXAMPLE .Frlr falseposition. r . Use bisection and lalseposition(o locatethe root of 'l(r) :'r"'l b e t w e e nr : 0 r n d 1 . and respectively.2113) : .. thereare certaincaseswhere bisection yieldssupericlr results.6 5 A C o s e W h e r e B i s e c t i o ns P r e f e r o b l eo F o l s eP o s i t i o n l t ProblemStotement.f (rt) : 4.. becomes upperlimit for the next the iteration.J 4 5 O (r5 0 975 o 975 o 975 3 . U s i n sb i s e c t i o n .t30 ROOTS: BRACKETING METHODS Therefbre. 5.0. true error is reduced lessthan 27r. (o/ol u 2 .the root lies in the first subinterval.r.566t 74(50 t] 6.5 4 975 I 1375 ) 05625 I 0t5625 e" ("/"1 r 0 00 333 1 43 77 4A e. there are other cases where it does not.(7"1 906 BIB 737 662 592 0 a a943O 0 tBtz6 o 26287 0 3 3 8 II After five iterations. . : 116. Additional iterations be performed refinethe estimates the root.9. (Y"l 48I 309 223 l7 l e. 3 3 3 3 3 0 09430 a tBl76 o 26287 0338Ir a 40788 e. in Fig. r. if .J J 137 5 45625 35 25 t3B 56 6 Thus.519381 ..rr) is muchcloserto the on .3828 .56olc. : 1 1 6 . and r.2713.r.2113 t'Q. can to of Although firlse positionolien perfbrmsbetterthan bisection. 2 7 7 3 1519381 .566174 t 62. As in the fbllowing exarnple. curve As the violates prernise which fhlsepositionwas basedthat is.l(. r0 6.566114 0. the to a very diffbrentoutcomeis obtained: herotion I 2 3 4 5 xt .. Insight the to into theseresultscan be gainedby exarlininga plot of the l'unction.
availableelsewhere(Chapra and curvature.thereare invariably casesthat violate this generalconclusion.Possibleremediesfor this shortcomingare Canale. of the present . (7"1 906 Br 8 737 66. (recallFig..a e. ".the opposite true. rior to bisection.e curve loser to .5). ght into . i l l u s t r o t is lg wc o n v e r g e n o et h ef o l s e o s i i i om e t h o d no ofthe shape zerothanf(x"). r t l o n .. fbr particularly functionswith significant stay fixed.2 5q2 rocltlocation regarding illustrates blanketgeneralizations that The fbregoing exarrrple is possible. sidedness. the (5. 35 2.the results by be checked substituting root shouldalways in additionto usingEq. thentheroot is closerto x1thanto x.Because is function.8). F ( x ) x l r t p n c{ 1 . whetherthe resultis closeto zero.POSITION 5.9 5 : P o to f .2002). and determining into the originalequation estimate method:its oneof a The examplealso illustrates major weakness the falseposition pointswill tend to of the bracketing one are That is. This can lead to pool'convergence. a methodsuchas lalse positiorr often supeAlthough are rnethods usuallynot Therefbre.5 FATSE r3l he next r cases section (. 5.5 r3 8 56 t6 FIGURE . as iterations proceeding.
3 5 6 9 7 5( t 1 9 8 0 . .1 9 8 0 ."' .n[H+] 6.IO Averogeonnuol portiolpressures otmospheric o{ corbon dioxide (ppm)meosured MounoLoo.s) w ul tr st l( where [H+] is the molar concentration hydrogenions.I H * J l H C O i I " Kr10"' ' Knpco..it can be computedas pH: log. pH is the measureof the activity of hydrogen ions and. Outsideof urban and industrialareas.. The data indicate that levels have increased a little over 19Vo over the period from 315 to 376 ppm.9) rh fc a 5r is pc Sr 1960 1980 1990 2000 2010 Er FIGURE 5.For dilute aqueous its solutions. (5.. 5 )+ 1 .SY Bcckground. ol Howoii.. A tr AI I In Part Four. 5+ 3 3 9 2 ) where pgo. we will learn how to determinesuchpolynomials. Hawaii polynomial. 0 1 1 8 2 5 ( r .3 from 1958through2003. of goverx the chemistryof rainwater: The following five equations ^ . It is well documentedthat the atmosphericlevels of severalsocalled "greenhouse" gaseshave been increasingover the past 50 years.r32 'il ROOTS: BRACKETING METHODS "ii'i*.lf.5. : 0 .The trendin the datacanbe nicely fit with a quadratic p c o . acidity or alkalinity. One questionthat we can addressis how this trend is affecting the pH of rainwater. fa .COz partial pressure(ppm).iei{rl: { ' GASES AND RAINWATER GREENHOUSE .Fig.iry'ii"".For example. is well documented it that carbondioxide is theprimary determinant of the pH of the rain. therefore.10 shows data for the pafiial pressureof carbon dioxide (COr) collected at Mauna Loa. .
One way Solution. (5..11)into Eq. its root can be usedto computethe pH of the rainwater.. K z .10)for Kupco.First. [H+].(5. .21 : carbonate.we will be satisfied .21 ffi*rnro" o: #*. considerthe following: .[H+] The five where Ks : Henry's constant. to causethe pH can only be measured two decimalplacesof precision. a n dK .I 6 ) (5. ( 5 .16)canbe substituted alongwith Eq.14)and(5. 'z##I KuPcoz.l a . to You also know that pH can only be measured two placesof decimalprecision. Use these equations to compute the pH of rainwater given that K n = i t l 0 .13)to give Equations (s.[H+] Ku. Thus. falls within the rangefrom 2 to 12. and K..r: fu#+ ' 106 tHcotl11coJ2l 0: tHcoil + 2tco. of [H*] : hydrogenion. (5.STUDY 5. was 3 l5 and in 2003 when it was 375 ppm.There are two reasons why bisectionwould be a good choice. 1 5 ) e : tco. You know with certainty that the pH of rain in pristine areasalways falls between2 and 12. providesus with two good initial guesses. s) I {#F rcor2l: S u b s t i t u tE q .: [H][OH] .12). (5.6 CASE continued 1ll:  tH+llcol2l tHcotl (5.1 0 . l7) this result is a thirdorderpolynomial in Although it might not be immediatelyapparent.21+toHl .l) r (5. 1 4 )i n t o ( 5 . 2) r ( s . tHcotl: fu.and K1. and tOHl : hydroxyl ion. is to eliminate unknowns by combining them to producea single function that only dependson [H+1. the fact that the pH always beSecond.3 ) 1 K.(5. There are a variety of ways to solve this systemof five equations.10) (5. Now we must decidewhich numericalmethodto employ to obtainthe solution. . areequilibrium coefficients.9)and (5. unknowns utec7 : total inorganiccarbon. first solveEqs. bicarbonate.t o : . K2.K 1 : l Q . Notice how the partialpressure CO2 showsup in Eqs.1 4 6 .9)and (5.63 . 4) r (5. C o m p a r eh er e s u l t sn 1 9 5 8w h e n the pgo. : l Q . When selectinga numericalmethod for your computation.To do this.tHCOtl: tCO.co'+ # ( 5 .
t obtain your solution. This can be verifiedby performing anotherrun with more iterations.lr'(l .l I llrr ].. (5. r r ) ll I li) i.t We can then use the Mfile from Fig.'ll r.1.t34 ROOTS: BRACKETING METHODS continued with an absolute error of Ea.. ILI l i i i i.'. ..63 is correctto two decimalplaces.rrl1l.005.d : t0.rll r1i1l.. 5. t l L .ll^i.0001 ..iiir.L.1ll)t). irrrl I '. i.' l:i.l.17) as a function.i. . ' . : I x l08) at a very low level so thattheiteration limit (maxit) is reached first so that exactly I I iterationsare implemented lrijlli.ir .) I ilI't lilrli.We can be confident that the roundedresultof 5. Note:Tl Start with initial guesse ate until the approximat 5. li. Howev iterationsand Eq.1r ..1 Use bisectionto de so that an 65kg bungee 4.: ' ' rrll. ' . i. L. rit i 'r. (5.we can computethe numberof iterationa priori.2 Develop your own J ion to Fig.ir ". Substituting present valthe uesinto Eq.4 Develop an Mfile b y s o l v i n gP r o b .Becauseit is relativelycomplicated. the pH is computedas 5. 5. For example. . 5. . i.5 s of freelall. (5. 5.0868%. Befbre implementingbisection. tl t. ll.l 1 l .6) gives r l . i l.we must first expressEq.*lll*llj*1.we will storeit as an Mfile: liiaij f. ll'. iLr.i'. lri:'. r. rl il l'..')l lrl )*l'i (L. Remember that given an initial bracket andthe desirederror. L ) . t.Notice how we have set the value of the desiredrelativeerror (r.. resultis the 5.r.'lLl...settingmaxi t to 50 yields ll..1li)ll. Make sureto r next highest integer.. 1l)l (ljil.'.l .) PROBTE l 'i .. I. For 2003.i tt.7.I . .l'i.. 5.''.lll'r iill" rr .t. '. 5 .l i. r irilill.{.lr I ( r ... l .7 to obtain the solution. ' ri l Eleven iterationsof bisectionwill producethe desiredprecision.7: 0.Ter using 8 .6279with a relativeerror of 0.5) criterion.{).:.ll ll.ll ll Thus.3 RepeatProb.
unit drop in pH repa a resentsa l0fold increasein the hydrogen ion.nrrrn 5.engineers and scientists hopefully usesuch can tools to gain increased understanding suchphenomena help rationalize debate of and the over their ramifications.1.o q l ( l Therefbre.5. tL Interestingly.67Vodrop in pH.PROBLEMS r35 lii .2 and i.7. (5. resultsindicatethat the l9%orise in atmospheric the CO2 levels has produced only a 0.6)up to thc highcst integer. I r . Thc accclcration Notc: ol'gravitv 9.. Testyour llnction by solvingProb.8). gas Most of this debatefbcuseson whetherthe increases contributingto global warming. (5.. [H*l : lOpHand its percent I : I 1112.Consequently. (5.L. :0. :ll ri.Over the coming years.rathcr than usin_r:l rnaxir. PROBTEMS b l l U s e i s c c t i o nt o d c t c r r n i n c t h e d l a g c o c l ' l ' i c i c n (n c c d c c l a m t h a tn6 5 ..This case has study illustrates how nunrerical methodsand MATLAB can be employedto analyzeand interpretsuchtrends.. 100 lb/ft 100 b t = 8.k gb u n r c c . Howcvcr.97o. j u n r p c r h u s a v c l o c i t y o l ' 3 5 n r / s a l t c r ' soffrcelall. regardless the ultimateimplications.0E74 i r ^. Although this is cenainly true. There is quite a lot of controversy relatedto the meaningof the greenhouse trends.e. The concentrationcan be computed as change can be calculated as. is sobering realize of it to thatsomething as largeas our atmosphere changedso much over a relativelyshorttime period.5. i' tt'_. I 5. thc andEq. P5. (5.ri :0. Tcstit solvine Prob.5. Makcsureto roundthc rcsultol'Eq. I FIGURE 5. crnployEq. but nsc thc falscposition Repeat Incthodto your solution. Usc thc biscction rncthocl solvc lir thc 1rosition to insiclc bcantwhcrc thc thclc is no rnolllcn(.5).5.5 A bcantis loadcdas shownin Fig.5 P ..8I nr/sl is initial Stafiwith gLrcsscs of'.the hydrogenion concentration increased has about.' continued ll .3 anclitcrthc unlil approxilnatc rclativccrror lirllsbcl<tw 2()tr.r 0'000I Prob. Develop Mfilc fbl thc lnlscposition an rnethod.1..ri{ .6) as yoLrrstopping . your Devclop own Ml'ilefirr biscction a sirnilarlashin toFi. rememberthat the pH represents logarithmic scaleas definedby Eq.. are However.. \ lr p ] ]1 9 5 8 ) * 1 0 0 clil:l ( r .
..l (x) : .T : lJ.n^^ek"'aI .t l L .l) ro dcr d e f l e c t i o nU s c t h c l i r l l q y * . 10.7 l.. 2. n (b) On thc basis (a).Accordingto thiscquation.l.Forexamplc. as 5.rr :0 and a of stoppingcriterion of I o/c.Gir.r7: l and. (P5. yonr 5.r w hcre.clbl theroorwith inirial ol guesu tr ^./ ft. (K).tl.5 to l. K . . ol'yea A: "' P" (t+i). soh. : thc hallsaturarion constan[.+S I FIGURE 5.S: Sir.15 Many ficlds ol'engine estimates.: 3 r . 20 to r.r'. Perfblm the computation until e.10Watcr is llowing in a trapezoidal channclat a ratc ol' whcrc thc subscript dcsicnatcs 0 thc initial concentrution Q : 20 mr/s.Tc. tion or' lulsc position to ohtaiu your solution. 5. trar it necessaryto determinc s trends of a city and adjacer urbanareais dccliningwith PuQ):Pu.tr I* Solve lbr the criticaldepthusing (a) the graphical method.4.{asrepre..2z1..r . Q'.1 5.14 . Fol this your choicc.rss results.4I 3 nig/L at 40 ''C. ( r 2 9 4 9x 1 0 " 1 Devclopan Ml'ilc to gcncratc plot ol'S vcrsus lbr the a r whcrc o. anclA.! = suhstrate (rnolcs/L).and r): and gen in licshwatcrat I atnr (ntg L 4.cn a valucol oxygcnconK: ccntration.3441 +lj]I01"]o' 1.dcvclopandtcsta biscction Ml'ilcl'une lion r c l a t i o n s h ia s p to dcternrine as a lunctionol a givcn oxygcncor)ccn7 ( t . biscction itcrations would bc rcquircdtu dctcrniinc rnte scnting nurnbcr thc ol'rnolcs ol'C thatarc ploduccd. : ahsolutc *. r : tcmperature satLlrilti()n dccrcascs with incrcasing tempcraturc. (b) bisection. Use a graphical tcchnique and biscction with the initial intervallrom 0. lhat wc rlcl'inca variublc.0.: the crossscctionillarca (rrr)..: 2. . conccntration rlum uptakcrltc (rnolcs/L/d).rl) : ()..\t] 5. Choosc cither bisec tvherc g : 9.r.05'C'l vutior)ol' nlasscirt bc uscd kr lclirrrnulatc cquilibrium thc on {b) Basecl (a).. 5. 5. I ) trction.0: 1. Justif. : ().500 pcr ycar Ic you paying'? The firrmular payments nrrmbcr A. Il' thc initial substritlc at level / : 0 is So.81 rll/s1. can bc churactcrizcd thc cqLrilibriunt at 0 "C to 6. Conser. and of (c) using threciterations thc talseposition of rnethod. anclitcrate until the approximatc errorljrllsbelorvl% or thc numhcrof iterations exccccls Discr.{bstratc I at rvhichuptake half lcve is ol Lhcrnaximumlrnolcs/Ll.7 : thc saturation conccntration ot'dissolvcd oxy: casc whctc .6 (a) Dctermine the roots of .2x))(t 1. how rlany r. which is thc s{. 1.62 ntg/l.12 A rcvcrsilrlc chcntical rcaction ("C).the width and the crossscctional areacirnbe related to dcpth r'by P 5. : 2c/.T3 P : tnr.1 t3()..'hcre rrorlcnclaltrrc l'cptcscnts conccntrationof alc constitucnt Supposc l.and Il satisfvthe cquation (.The critical clepth tirr suchI channelrnust ol cachconstitucnt.Sp l0 ntolcs/L.this dil'f'crcntial cquiltion can bc solvcd fbr .l whcrc .tt.9 Thc salurationconccntration clissolved of oxygcn in licshwatcrcan be calculatcd with thc etluation B: 3*. For typical rratural 24+Bc u'atcrs tcnipcrate in climatc. ..016. with the sameinitial gucsses in (b).14 You huy a $25.this lirlrnr"rla and the biscctiontncthodcan hc uscdto solvc lirr tcmpcratulc "C. in (i lhc lhc (a) Il'thc initial gucsscs sct as 0 and 40 "C.o: 42. : T +213.J lrolcs/L/d.5 and {r.. thc (a) Obtainthc solLrtion graphically. and (c) falsc position.7 (a) gfaphically..ll Thc MichaelisMcntcn rnoclel dcscribcs kineticsof thc cnr)/nlemccliatcd rstctiolls: tlS tlr . r*r. j 5. thc valueof _rwhcn valueinto Eq. .^1 : of't. cquation the can hc usc. O2 0:I LA.000 1 down at $5. Rcmcmbcrthat f. x 6.v and R : thc width ol'thc channel lt the surtacc (m).r : .64230U 107 l l.ocate first nontrivialfu()tof sin(.l r.0 .0 .5%.(. Use bisectionto dctcrminc (thatis.rrs the ) in radians. ano A ._l i(l..r36 ROOTS: BRACKETING METHODS case.5.. (b) using threc iterations of the biseetion nrethod.r = Inrxi.r s + l ' l20EIt. wherc lcmpcrature 5. * _f 2 .L..20r * l9{2 .13 Figure P5. p u t a t i o n :: 6 ( X ) c m .3i3 graphically.: 0 rnd r.cl dcto lclrnincthatoxygcnconccntration rangcs by lclationship li'orl 14. is lessthan o. : 2 rlttlcs/L.: 2.tJ Detcrminethe positivc rcal rrrtr(of ln(.i T. (i. pcralurcto an absolulc crror of'0.For (b) and (c) use initialgucsscs tll'ri : 0. :0.r' : 28.. E : L : ond u.. l0lnd l2 rng/LA'.n 5.5 iindr.with initial gucsses r1: 0.. dctcrntinc valucol'.r. determinethe flrst root of the lunction with (b) bisection and (c) falscposition..5 kN/crn.st your lunclionlirro.l3a show early increasi distributc ng ing elastic urvcis rstc Fig c 'v : .r) Checkyoul results.In adclition.S "1.ltl00 l0r(l x r.For (b) and (c) use initial guesses .s. In(Sg/S) * \a t i .
To obtain your solutions. 9 r n .. lirrcc cxcrtcdon thc chargcby the ring is given by Usebisectron ttrdctcrr. Then sr. : 0. C : 6.riccc ol' cqr:ipnrcnt at$5. n . Determincthe timc and con'esponding 'nt'c P.. l7 5..and t.5 x l0t' V s cm/C. and k.thc ol'.0).rbstitulc ts.t 120F.I 5'13 Figure showsir unilirlnrbcarnsubjcclto a linP5..': You buy a $25.".u.5 0 c r nV s ) r ..r this into tP5. 1 : 30. 1n co (..r'. : t 1 .17 * Prr.t 'r : ub=25kN/ctn. r.r=0.What intcrcst ratc arc w h c r c c ...(t)andP. Thc ccluatiorr thc rcsultlirr atlyrncreasing hgelastic is (sccFig. 10.045/yr.. 7 x l 0 r ' ) .ninc noint ol'rnaxirnurn thc dcf'lcctitrrr value whcrctlt'ld. l"' P FIGURE 5. anclthc electrorrrrobility ll.. 2 5 N i l ' 1 1a n d Q a r c 2 x l 0 C l i ) r a r i n g w i t h a Thc r a d i u so l ' 0 . i l t l t r37 I +LP. A totalchargc is unilirrrnly distrihuted around ringa B shapccl conductor with rldius a.rr Lr r ) (p5..The thc dopingdcnsityN iinclthc intrinsic by clcctronrnobilityis describcd the tempcrature thc rcfI The crcncc tempcraturcI. .. Usc (a) biscction and and a dcsircdlt (b) the lllsc positionnrcthod. A.(/) when thc sLrburbs 2()%largerthan the city.I population rcquireaccuratc Many lields of'cnginccring Forcxarnplc. ittrttual t h c l o r c c i s 1 . P5.(XX) L crn. : The pararnctcrvalues arc P.thc clectrondcnsity n. I u0. transportation cngincclsrnightlind necessary to dctcrnrincscparatclythc populationgrttwth o{' Thc populatiort (hc of a city andad..ncfc iIGURE 3 P5. P. (b) falseposition i."*.000 and mcthods.(r(X) E : 50. f : 1000 K. : : P.*.'. F i n d t h c c l i s t a n c c x w h c r c Dcl' 5 pupaying'l lirrnrularclatingpfcscntworth P.000 lil' nothing .l3rr distlihutcd loacl.16 Thc rcsistivily p ol' doped silicon is based on thc chargcr7on an clcctron... cqLlntions rcquircdto computcthe rcsistivity are I qnlr \4'.rl + a2yr. nt tcr is with timc accorcling area declining P .500 voar lor' 6 vcars. P0 : people. usc (a) graphical.t=0) $'hcrc P. 75. .x 100'000 people.13) . ratci is pyments rr. 1111 3 (r l 3 .l3r) cun'c r = l_l. P5..:j(r* : DctcrnrincN..17). Thc electrondcnsity is givcn in terms of carricrdensityn. : 0. 8 5x 1 0 ' t C t ( N m l y .. Uscthclirllowingprrarnctcr ion: .L I ..000..lace suhurh... 2 1 x l 0 ' ) c r n .4 : 1 ..r'corlr . givcn 7ir : 300 K. " * t .000cm*.l3)to dctcrminc valucol'thcrnaxirnurn Eq. kN/crn'. : ernpiricallyderivcd patu. nurnber ol'ycirrs lnd intercst i(l+ l)" A =P i l + l ) .rrin . thc valucsin y()r.PROBLEMS population growing.n.u. A chlrgc q is l()catcd a at The r drstancc lhrrn thc ccntcrol'thc ring ( Fig..iu 100.max/Po llst'r (a) (. : 6 .as in rvhilethe suburban is P rrt rD \ .08/yr. k.u . P.000people.t + 21r. valucsof ralnctcrs. and thc referenccmobility pr1y. P.
q : thc l'uclconsumption irnd = ths riite.qt  . as in uscthernrodynamics cxtcnsivcly thcirwork.Thc lirllowing polynornial can bc uscd to rclatc thc zeluprcssurc spccilic ttto ..^ Senl F valu time Hov quic a sir ing I .. 6 1 x l 0 1 I + 9 . g = dorvnwat'daccelera(ionol' gravitv (assumcd constant t)..on Kurnuul etlutttion: Ir\ ::4toc.000005.9 . .+ R Jf Typical valucstbr thc Reynoldsnumbcrtbr turbulentI'low are 10.r38 ROOTS: BRACKETING METHODS (K): heatof dry air r1..fot:to r ./ given a uscrsr.lJI rn/sr).1. .(nc../??o thc initial mass ofthe rockctat tinlc/ :0. . 9 9 4 0 3 + 1 .D e v c l o p al i r n c t i o nh i l tu s e s i s c c t i o t o s o l v c t b n lor.l given Rc is the r. . solutecrror in thc'rcsulti.the by Revnoldsttumher Re. : 150. 0 1 . rt.fr i c' i on . 5.000to 500.(X)0 and = kg.20 The upwardvclocity of a rockct can be computcd by thc tbllowinglblnrula: l' : .000anclfbr thc Fanningfiiction tactor arc f ) . which can all be representcd anotlrcrdimcnsionlcss quantity. l) u. computethc tirne at which u = 750rn/s. K 5. 9 5 2 0 l 0 l ' 1 r l x x Detcrmine tcmperaturc conesponds a specilic heat the that to oi l.rpplicd valuc of Rc bctween2.50 Dcternrinc s. 0 0 It o 0 .qI whereu : upwardvclocity. 0. 7 2 1 5x l 0 8 r :  ..r: 2000 m/s. : 0 .. : thc vclocityat whichfuel is u : expcllcdrelrtivc tt>thc rosket.) results0 thutit is w'ithinl% ol'thc trr.000.19 Mcchanical cnginccrs.500and 1. 6.If r..{ lfl llln 5.lti For tluid flow in pipcs.5 8 3 8 l 0 r r T 3+ 1 .re value Chcckyoul answer. friction is dcscribedby a dimerrsionl number.the F unn i ng .l kJ/(kg ).The Faness t ning liiction factor is dependcnton a nurnberof paramctcrs related to the sizc of the pipe and the fluid. wcll as mostothercnginecrs.kJ/(kg K) to tcmperaturc r .000.(Hint: t ycurr is somcwhcrc bctwccnl0 and. A firrmula that prcdicts. Designthe lunctionso that it cnsures thatthe ab. 4 2700 kg/s.
Learninghow tclrnanipulate dctcrmine rootsof polynomials and witlr the MATLAB. Knowing how to irnplement boththe secant secant rnetlrods.they somethe (Fig. o . 6. la). lb).Repeated application these mcthods of alwaysresults in closerestimates lhe true valuc ol'thc root. ch c Knowing how to solvea rootsproblern with the NcwtonRaphson methodand the appreciating concept quadratic of convergence. Recognizing diff'erence the between bracketing openmethods root lbr arrd location. they moveclosertclthe truthas the cornputation progresses because . or the brackcting rnethods Chap. and are . open ntethotls the in described this chapter requireonly a singlestarting valueor two startingvaluesthat do not necessarily bracket root.genl In contrast.il Roots: Open Methods CHAPTER OBJECTIVES The primary objectiveof this chapteris to acquaintyou with open methodslilr finding the root of a singlenonlinear Specificobjectives topicscoverecl equation. ' .lc) they usuallydo so ntuch more quickly thanthe bracketing methods. the root is located in within an intcrvalprescribed by a lower and an upperbound. and the modil'ied Knowing how to useMATLAB's f zero lunctionto estimate roots. times divergeor move away from the true root as the computationprogresses However. r39 . . Suchmethods saiil lo be converof are (Fig.5. the Understanding fixedpoint iterationrlethodand how you can cvaluatc its c o n v e r g e n c c a l u c l c r i is ts . 6.6.As such. will beginour discussion opentechniques We with of that is usefulfor illustrating a simpleapproach theirgeneral fbrm and alsoltr demonstrating the concept ofconvergence.when the open methodsconverge1Fig.
rr shope of the frncrion ( c n d t h e v o l u eo f t h .redpoitrt fbr itcration(or.whLch Newlon Rophson. ^1 ^u # xt lx lH ttru lol FIGURE6.Sucha fbrmulaca be developed sirnple..11eSexprcssed the iterative by fbrnrula a ri+t : g(ri) (6. rooi is consircrinecl the wilhinlheinfef for s vol pr^scribed by.I ITERATION SIMPLEFIXEDPOINT As just mentioned.r i .onepoint iterirtionor :) s u c c e s s i v e b s t i t u t i o n ) r c a r r a n g i ntg c ' f ' u n c t i o n . io r. or dependingc.h a n d su by h sideof the equation: r:.id1y. lorrnulois used fo proiectfrorn. l) is that it provides tbrmula to predicta new valueof . of The utility of Eq.r. r 0 s o t h a t . ri s o n t h el e f t .r. T h u s .. ( 6 l.]n controst.g i v e na n i n i t i a lg u e s s t t h e r o o t .^ n i l i o l t L r e s s i TI 6. ihe open methoddepictedin (b) ond (c). opennrcthods to employa fbrnrula predictthe root. E Q . J ri r. l ( .li.I djfference Grophicol deplcfionof lhe fundcmentol betweenthe (o) brocketing ond (b) ond {c) for open rnelhods root loccrlionln (crl.t40 ROOTS: METHODS OPEN EXAMPLE 6. (6.shed or can eitherby algebraic manipulafion by simpJy addingr 1oboth sides the originalequation.) c a n h a o usedto compute new estimate . which is bisection. as it is alsocalled..:l Tt an .q(x) (61) pr pr sit gri be alt This tlansfbrnlation be accourpli. I in cn itercrtive cr foslrion.rasa a f u n c t i o n f a n o l d v a l u eo f r . Thus the thr: methodccrneilher(bJcliverge (c) convergerop.r1 ond r.
5 0. mustcommcntat this point aboutthe "poswe sibility" ol'convergence.vr.2a for the f unction.t'::.l (r ) : e ' . Rccallthat in Scction5.56714329.x. "/" le. is Suchan approach ernployed Fig.r) and .l.Use simple fixedpoint iteration locate rootof /(r) : e '\ . we graphed functionto visualize structure a its and bchavior.ftG): JzG) Thenthetwoequations . "h t 00 000 76322 35 I 35 22 450 t) 755 6 894 3 835 2 tsg ) 239 o 7a5 0 399 le.3) Problem Stotement. The function can be separated directly and expressed the form of Eq.1 (6.ll(. to the Solution.5 ) (6. 6../le'1.fi+l : C" :0. is calledIinearconvergen(e. 1 S I M P LF I X E D O I N T T E R A T I O N E P I t4l As with many other iterativeformulasin this book.. the approximate error for this equation can be determined using the error estimator: '":lfflrcou" FixedPoint lterotion EXAMPLE Simple 6.2) as in .t 4 763 a 46A a 628 0 533 0 586 0 556 o 573 4 564 0 569 4 566 0 0000 r 0000 4 3675 0 6922 0 5005 o 6462 a 5454 4 5796 0 560r o 5/II 4 5649 r00 000 t7l B2B 46 854 38 309 )7447  157 5 903 3 4Bt t93I t r09 r0 Thus. I is roughly of (by proportional a firctorof about0. eachiterationbrinssthe estimatc closcrto the true valueof the root: 0.. The concepts convcrgence of and divergence can be depictcd graphically. this iterative Starting with an initial grress r11 of equation can be appliedto computc: rt U I 2 3 4 5 6 7 B 9 le ol. (6.2. An in a l t c r r t u t i v r ' g r u p h i c a lp r o u c h t o s e p a r u ttc cc q u a l i o nn l o t w o c o m p o n e n t a r t s a s i n ap is h i p .l.6 .6) to thc errorfiom the previous to iteration. ./:(r) (6. Aside liom the "rate" of convergence.r. This propcrty. Noticethat the true percent relativcerrorlbr eachiteration Examplc6.4) . charactcristic ol'fixedpointiteratron.
r 1: g ( x s ) valueof .Thervalucscorrespondingtotheintersectionsof of the represent rclots /('r) : 0' thesefunctions and divergence 'l'he twclcurvemethodcan now be usedto illustratethe convergence =r as a pair of equiltions'\'l First. . t o t t h el n i e r s e c t io fnh " ..l) can he rcexpressed of fixedpointiteration.rr)l and then to [x2. 6. " .y2:g(r). 6.Thesemovements equivalent the first itcration .2 root the fordererminins 'l l9l ::^:Jr !1ll::' "' 'h' methods.(6.. mateof . (6.rr] is located pointon rhe.:r2]. ""li. Thesetwo equalions Eqs.)'t : 't and tbur diflbrent plottedin Fig. of consists moving to [. The function .4)and(6'5). in both the equation This g(. theconesponding of is For the first case(Fig..yz of the fixedpointmethod: to are the yrcurve.e ' rvvu "E srophiccrl iunctions oi lhe O t . u i. FIG Gro ilnrn osc canbeplottedseparately(Fig.. g(r0) l. The next iteration iteri roo Fig Cha por slop . o * p o n " n t ooiniwhereit crosses .1 6 FIGURE .(l'2b). with As separately' was the case can then be plotted ancl. The point [xr .rr.142 METHODS OPEN ROOTS: 6. Eq.r1.3a).{(.3.therootsoll'(x):0correspondtotheabscissavalueattheintersec' shapeslbr.re is usedto obtainanestiand in the plot.the initial guess r11 usedto <letermine to by movinglefl horizontally curvel. a starting Thus.il.v2 : g('r) ate tion of the two cufves.
Grophs (o) ond (c) ore co led monotonepotlerns . 6. wherethe iterationsdivergefrom the root. 6. Fig. w o s c il o l i n go r s p i r o p o t t e r n sN o t e t h o tc o n v e r g e n co c c u r s h e n g ' ( .3b. it can be shown that the error for any iterationis linearly proportional to the error from the previous iterationmultiplied by the absolutevalue of the slopeof g: Ei+r : g'G)Ei . r ) l .I SIMPLE FIXEDPOINT r 43 J0 (cl (d) FIGURE6.3 o e o G r o p h i c o ld e p i c t i o n f ( o )o n d ( b ) c o n v e r g e n co n d ( c )c n d i d ) d i v e r g e n c e f s i m p e f i x e d p o i n l whereos(b) ond (c) ore colled iterotion. As in A theoretical derivation can be usedto gain insightinto the process.3c and d. However. 6.< 1 . described Chapraand Canale(2002). e iterationis equivalent the equation to 12: s(.ITERATION 6.rt. of The solutionin Fig.3a is convergent because estimates r move closerto the the The sameis true for Fig.this is not the casefbr root with eachiteration.
"/" 0 I 2 3 4 0 0 500000000 056631t003 o 56/143165 1 4 .re : 0.4. o method can be derived on the basisof this geometrical interThe NewtonRaphson pretation. (66) f ( . If the derivative negative. I (.2 NEWTON. the first derivative As at. in Fig..r.r is equivalent the slope: to /'('r't: /('r'to Ji 'ft+l which can be rearranced vield to ' r r + l: ' . t/ .a).the enors will be positive. EXAMPLE .3b andd). l). Notice that the true percent on relative each iteratiorr el'rorat clecreases much faslerthan il doesin simple fixednoint iteration (compare with Example6.6.son lbnnulu.l which is calledtlreNcwtonRaph. . the errorsdecrease with eachiteration.'1 \'.tremployingan initial guess ol'. e'.andhence the errorswill havethe samesign (Fig.2 6 N e w t o n .RAPHSON Perhaps most widely usedof all rootlocatingformulasis IlteNewtottRaphsott the method (Fig.il it tt Startingwith an initial gucssof . this iterative equation can be appliedto compute le. a tangentcan be extendedfrom the point The crosses r axisusuallyrepresents improved the an Iri. 6.r1 ^.3 P R . z.. if For lg'l > I theenors grow. 6.l.lg'l < I. .3aandc). ..l Gi) f o.R o o h s oM e t h o d n Problem Siotement. Notice also that if the derivativeis positive.r1. Use thc NewtonRaphson rnethod estimate root of /(x) = to thc ? ' ..)1. 0..10 8 Thus.6)to give alongwith the originall'unction '\'. the approachrapidly converges the true root. change sign on eachiteration 6. (6.144 ROOTS: OPEN METHODS Consequerrtly. The I'irstderivative functioncan bc cvaluatcd o1'the as /'tt) : <" I T which can be substituted into Eq. 6. point wherethis tangent e s l i m a t c l ' t h er o o t . If the initial guessat the root is .567 432()0 IOO llB a )47 0 0000220 EXAMPLE 6. enors is will the (Fig. Solution.
tongent ihefunction . Although thc NcwtonRaphson method is ofien very eflicient.In other words. However. fthotis.r : 0. (6.r)l is exlropo As with otherrootlocation mcthods. t l r t .E L3 A S l o w l y C o n v e r g i n gF u n c t i o n i i h N e w t o n ' R o p h s o n w Problem Stotement.r. . crite.2 NEWTONRAPHSON t45 l(r) FIGURE .5.r : . 2002)provides insightregarding the rateof'convergence expressed as by (6.4 6 Grophicol depiction theNewtonRcrphson of method.rr0lhe Raphsor.r oxislo provide estimote ihe rootot .i r . /'(.11 on of .l(l) : . the numbcr of' significantfiguresof accuracy approxirnately doubleswith each iteraticrn.rion.)rI: which can be usedto compute .7) Thus. (Chapra ln addition. The NewtonRaphson tbrmulafbr this caseis . of Solution.r.A special casemultiple rootsis discussed elscwhcre and Canale.t .6.jor is the reasons fbr thc popularity ol'thc rnethod. A to of oteddown to the.difficulliescan also a r i s e a s i n t h ef b l l o w i n se x a m o l . Determine positiveroot of . thcorctical a analysis andCanale.cven whcn dcalingwith simple roots. This behavior calledqruulrilic (onvcrgcn(eandis oneo1' rra.2002).r methodand an initial guess . e E X A M P6 . thcre irrc situations (Chapra whereit pcrlbrmspoorly. tl { )  I using the Newton l.thc crror shouldbc roughlyproportional the square to ol'the prcvious error.3)can be usedas a termination Eq.
65) where. but the at a very slow rate.6.r: 5 fbr over 40 iterations until convcrging the root with adequate on accuracy. FIGURE . afterthe first poor prediction.432 4A 4l 42 0 0 2 3I 6 aaoa24 2130 o 22g 0 002 is on Thus./(x)has an extremely high value.a simpleplot of the first few iterations As Noticehow thelirst guess in a regionwherethe slope neat is is helpfulin providinginsight. is Why doesthis happen? shownin Fig.65285 99.The solutionthcn plodsalong value(.Thus.6a depicts casewherean inflection the .6.5 5 16 5 46.as illustrated Fig. the for inset shows slopeinitiolly thesolulion slowly very on converqes lhe root l(x) 2E+17 1E+17 FI T Aside from slow convergence due to the natureof the function.146 ROOTS: METHODS OPEN xi leol. For example. technique converging the true root of I.5.5 6 Rophson method o cosewilh s ow convergence for The Grophicol depiction theNewton of how o neorzero shoots solution fromtherool Thereofter. 6. 6. the first iterationflings the solutiontar away from the initial guess a new 1. "/" 0 l 2 3 4 0. Fig. other difficulties can in arise. to zero.485 4t 8365 37.
6.2 NEWTONRAPHSON FIGURE .6 6 poorconvergence method exhibits wheretheNewtonRophson Four coses .
( 4 ( x ) 2.This tendency move awayfronr thearcaof interest dueto thefact to is that nearzero slopes encountered.5) Ft An . Fig.6b.It also suggests that good cornputersoliware shouldhe designed recognize to slow convergence divergence.e. (E6.H')#. they can be passed thefunction as to as arguments."x. or 6. only on of The remedyis to havean initial guessthat is "sufficiently" closeto the root.3 SE As the ma '+#:irF""^(. zeroslope[.r.81 nr/sr.6d." .7 to detcrntine rnass thc the of jumper with a dragcoefTicient 0.7.f ltnl :rff""n( grii.n: to 6.These to can be simply accomplished the inclusionof userdcfined by functionsto compute these quantities.7).r2 . Alier the Mfile is enterecl saved.Suchoscillations to maypersist.1 I ATLAB Mfile! newtraph An algorithm the NewtonRaphson lbr (Fig. of Solution. Thc firnctionto bc evaluated is . Forexamole. nl ) / .. in the algorithmin Fig.r) : 0l is a rcaldisasterbea cause causes it divisionby zeroin thetrewtonRaphson fornrulaIEq.6)1.41) To apply thc NewtonRaphson method.R o p h s o B u n g e e u m p e rP r o b l e m n J Problem Stotement. Usc the Mfile functiontiom Fig. ^ : l ! 1.25kg/rnto havea velocityol36rn/safter4sof bungee of fice fall.' EXAMPLE 4 6 ' N e w t o n .9. Alternatively.6.6c showshow an initial guess thatis closeto one root canjump to a location several rootsaway. asin Fig.f (x) : 0) occursirr the vicinity of a root.6.a nearzero slopeis reached whereupon solutionis sentf'arfrom thearea the of interest.no guess will workl Good guesses usuallypredicated knowledge thephysare on of ical problemsettingoron devices suchas graphs that provideinsightinto thebehaviorof the solution.. Figure6.6b illustrates tendency theNewtonRaphson the of technique oscillatearounda localmaximumor minimunl. / L r a I J n ( G ( x ) .2.'(l. or.it can be invokedto solve tbr root.rrt( . convergence Its depends the natureof the lunction and on the accuracy the initial guess.6. are Obviously. thc root cun he dcterminecl in as : ' j ' n e \ ^ .. 6. 6. Notice that iterations beginning ./'(. And for some functions. in Fig. off and Thus.thereis no general convergence criterionfor NewtonRaphson. and lor the simplcfunction. As it means thatthesolution shoots horizontally neverhitsthex axis.(6.r48 ROOTS: OPEN METHODS ' point (i.) fir r.the derivativeof this function rnustbeevaluirtedwith resDect the unknown. The acceleration grlvity is 9. 6. method canbe easilydeveloped Notethat theprogrammusthaveaccess thefunction(f unc) andits first derivative (ofunc).re at progressively diverget'rorntheloot.
4.7 A n M l i l e t o i m p e m e n if h e N e w l o n R c p h s o n e t h o d m W e s h o u l dn r e n t i o nl r a ta l t h o u g hh i sd e r i v a t i v cs n o t d i f T i c u l[to e v a l u a t en p r i n c i p l ei. it:erl=newtraph (f unc. .f u. . B l * 0 . r p h ( .end nargin<5  isempty (maxit ).anh(sqrl. The two fbrntulascan now be used in cilnjunctionwith the function n.81*m/0. end FIGURE 6. (9. 2 5 l m ) * 4 ) ^ 2 .rc(xr)/df unc (xr) .d) /xr) if ea <= es i iter >= maxit. > > d y = G n I / 2 * : . xrold xr = xr .additional p1. d y . 0 . end end root : xr.orimate relative error = number of iterations % iter if if if itoz nargin<3.pl . break. 1 * 4 ) 9 . * s c c h ( s q r l ( ( 1. v o ' d r g i n ) % newtraph: NewtonRaphson root Location zeroes % lroot. Although this is not inconvenient polynomialsand of fbr many other tunctions. ea. es.xrol. 2 5 l m ) .es=0.p2. 0 0 0 0 1 ) t \.'tj]6 METHODS 6. end O. wtr nLih [o evaluatc root: the >> y = Gn sqrt ('j.maxit. i r e r J. ) .. ' E I / ( m * 0 . error ( 'at least 3 input arguments required' nargin<4 isernpt)'(es).. d i r n c .appr. 2 5 ) ) * t a n h ( ( 9 . / r " > n c w L r . ..25)*l. if xr = 0. F S . iLer ir<r * 100.. 1 . y r .\2.p2. xr.81*0. parameLers usted by function % % output: % root = real root (?) % ea .3 SECANT MEIHODS f u n c c i o n I r o o u .zat irze of f unct lon ? xr = init ia1 guess .m a x ' L . r 3 1 ( 2 * p .{ . 1. a potential problemin implementing NewtonRaphson the methodis the evaluation the clerivative. ^ (1 / 2 . . 8 .thereare certainfunctionswhosederivatives may be difficult or .6.n e w L ' d p h r f u n .l * 0 . e a . end (1) while = xr . .0.3 SECANT As in Example6.r c 1 r l1 9 .0001?) (default % es = desired relative error (default = 50) % maxit = maximum allowable iterations . naxit.t t t i i involvcsa bit of concentration eflbrt to arriveat thc linal and rcsult. ea = abs( (xr .25lm)*4) l6. ) : ? uses Newton Raphson nethod to find the root of func % input: % func = name of function ? df unc = nane of derir.0001. df unc.50 . 1l .
/(.f estimates.r). U s e a n i n i t i a l g u e s 5 o f g a n d a gra m s0k value of I 0 6 for the perturbation fraction.8) Th Equation(6. whe .f (xi + 6.9)yields First iteration: xrt : 50 .ro*dru:50./'(.3 8 ./(xir) . is not classifiedas a bracketingmethod. (6.6) to yield the following equation: xi+t : xi /(xi)(xi t xi) . . Inserting parameters Eq.5r) (4.f(xi+lxi)f(.rso ROOTS:OPEN METHODS inconvenientto evaluate.ri) dri where 6 : a small perturbation fraction.00005 Jl asi alw cha nifir rese met 6.ri) (6.4 Ml The but ofb fepr l'(xo) : 4.t nrcthod.For thesecases. 4 V o ) 1 l. Se This approximation can be substitutedinto Eq.the derivativecan be approximatedby a backward finite divided difference: f (xit) .it providesa nice meansto attain the efTiciency NewtonRaphson of without having to computederivatives.9) swa Eq.4.(4.f ..579381l8 . the into Solution.f(x.25 kg/m to havea velocity of 36 m/s atter4 sof bungee f r e e f a l l ..xi 6.As in the fbllowing example.Notice that the approach requires two initial estimates x.) ' { x i \ = .f(ri) xit .r) is not requiredto changesignsbetween of the . ) . der EXAMPLE . We call this the modified secan.f (xo+ 6xo). 3 9 9 3 1 ( l:€ . (6.6)to yield the following iterative equation: ri+l :  "tr 6x.57938708 I .57938rI r8 .l'(x.5 6 M o d i f i e d S e c o n tM e t h o d Problem Stotement. 8 l / s 2 .57938708) I 0b(50) 4.57938708) whe Nott = 8 8 .because (. (6.8) is the fbrmula for the secantmethod. N o t e : T h e a c c e l e r a t i o n o fv i t y i s g . However.This approximation can be substituted into Eq..1 (6. l % oe o l : 4 3 . Use the modified secantmethod to determinethe massof the jumper with a drag coefficientof 0.r. it Rather than using two arbitrary values to estimatethe derivative.an alternative approach involves a fractionalperturbation the independent of variableto estirnate.
univariate functionis rnerely entitythat returns a single value in return for valuessent to it.(.The f zero functionis designed find the realroot of a singleequation. T" 43 438 28 762 II 746 I 5t7 a a2t 4 I x lO '' The choiceof a propervaluefor 6 is not automatic. Eq. functionmight evenrepthe program. 7 6 14 2 7 3 7 a letl. lxa xl ) ) where x0 and x1 are guesses that bracketa sign change.1. if chosencorrectly. but possiblyunreliable The MMLAB f zercr functionplovidcsthe bestclualities of both. and Note that two guesses that bracketthe root can be passed a vector: as f zero (funcLion. 124.rr): 1. J is too snrall.For suchcases.1.692203516 106(88.rr:88.l.4 MATLAB FUNCTIONz fzero The rnethods havedescribed this pointareeithcrreliable slow (bracketing) fast we to but or (open).16a/") je. methodcan be If the swampedby roundoft error causedby subtractive cancellation the denominator in of inefTlcient evendivergent.rr+dxr) .l7c: lo.f(.08910(  : The calculation be continued yield can to i 0 .F 6 . the technique can becorne Howarrd ever. x0 is the initial guess.l :28./(.2 3 4 5 () I xi 500000 B B3 9 9 3 t24 ABS7 ) 4 A5 4 1 7 t 42 7A72 1 4 27 3 . e r o( f u n c t i o n . secant resent independent an computer and modifledsecant the methocls valuable. g general Further. are 6. sirnple to A representation its syntaxis of f z .If it is too big.9). .39931 . r 1f d r 1 : 8 8 . Perceived this scnsc.3993l : .69220111 . "/" 64971 38064 I3064 t 53B o o2t 4 l x lO' 3 4 x l0 ' le.69220't1t) r6 l3.6922035 . x a J where funcrjon is the nameof the functionbeingevaluated.3993 1. 3 9 9 4 0 . its m<rst in a an sense.6922017 1)(t) t.it providesa nice alternative caseswhere evaluatingthe for i s d e r i v a t i v es d i f f l c u l ta n dd e v e l o p i n t w o i n i t i a lg u e s s eis i n c o n v c n i e n t . 4 M A T L A B U N C T I O Nf: z e r o l5l Seconditeration: . example. sorne In cases.functionsare not in like theoneline alwayssimpletbrrnulas equations solvedin thepreceding in examples this For chapter.tr : 88. (6. f unctionmight consist nranylinesof cocle a of thatccluld takea significantamountof execution time to evaluate.
1 'lhe' f unction val ues. i t l i n d s t h e n e g a t i v er o o t >> x x3 as If we wanted to ensure that we fbund the positive root. it The f zcro functionworks as filllows.0 Ii) x^2'.. 1. errormessage displayed Also.0) F C 1 rh x3 the an is doesnot occurbctween two guesses.14 4l) IS  . the (i. f2c. pa us c . intelsection thisstraight quadratic The inverse interpolation line with the. firstperThis search from lhe incremental search forms a search identily a sign change. r1 e ' n c l p o i f l l .g. lrn'ersc quudrutic interlnethodancl inverse algorithnrs: secanl the poltttionis sirnilarin spiritto the secant rnethod based is method. thatthe search directions until a signchangeis increasirrgly bigger steps boththe positive in andnegative detectecl.8b).r1. use a guess that is near it: > > x = f z e r o ( ( 4( x ) x3 I f w e p u t i n a n i n i t i a l g u e s so f z e r o . : j l i l u : i t ill rnethocl of with two t'aster The f zero lunctionis a combination the relitble bisection quaclratic interpolation.the root estimate falls outsicle bracket).1. 6. we could enter twil guesses in >:.r'axisrepresents new root estimate. To find the negative root: Clearlytwo rootsexistat >> x = fzero(Glx) x3 If we warrt to find the positive root. .: x fzcro((4(x) x"').6 Pr< .16(t.. ' t v . fast methods(secantand inversequadraticinterpolation) used the resultoccurs(e. if a signchange o{ >.e. in Fig. unare Thereafter.r52 ROOTS: METHODS OPEN for Hereis a simpleMATLAB session solves theroot of a simplequadratic:. ir. secant As the l'he of on computing straight a line thatgoesthroughlwo guesses. abad the lf lessan unacceptable root is obtained with one of resulthappens.8rr..l(1) x'2 !). bisection inrplemented is until an acceptable wl et( thi EXAMPTE Th 6.a parabola) that cln a equation usesa similarstrategy is based computing quadratic but goesthroughthreepoints(Fig..r l' pr( So .Lhc clif fcr in srgrr.9. to difl'ers in starts the singleinitial guess then at and takes described Section in 5. r : . lf a singleinitial guessis passed.1. . : I 1 r u.4) f :ero(tj (x) x^2 9.f: 9 that 3 and 3. 6. 4) r^2 9.
. .i rnsct.:2. A c c l m p l e t e l i s t i n g o f a l lt h e p o s s i b l e p a r a m eters can be obtained by rnerely entering opt. w e f b u n dt h e p o s i t i v e o o t o f . . p 2 . Solve the same p r o b l e m i t h o p l i m s e t a n df z e r o . Whensetto'irer'displaysadetailcdrecordol'all theiterltions.lmseL function.8 N i q o C o m p o r i s o n f ( o )t h e s e c o n tm e l h o do n d ( b ) i n v e r s e u o d r o f i c n t e r p o l o t l o n . .o i e t h o tt h e " t f i o p p r o o c hi n ( b ) i s c o l l e d" i n v e r s eb e c o u s e h e q u c d r o t i c u n c t i o ns w r i l i e ni n .1 rl iir F un c t i o n s r 6 P r o b l e mS t o t e m e n t . ' p a r ' . fx] i z . The opt imsct functicln has the syntax optiors o f i L r m s c t .s is a data structure created by the opt. optron. and p1 . at the command prornpt. p l .. . J where I xr . EXAMPLE The r 6. o l . . .I using the NewtonRaphson mcthodwith an initial guessol'0. Lolx: A positive scalar that sets a termination tolerancs on x. x ) : rl0 .J h w h e r e t h e p a r a m e t e r p a r . 'ite r'). a vector containing the root x and the function evaluatcd at the root fx. shiftsto the tasterrrrcthods.tl . r ' r o l h etrh o n i n at As bisection typicallydominates first but asthe root thefastmethods.<] . R e c a l lt h a t i n E x a m p l e .. ' . 3 . is approached. .5. The parameters that are cornmonly used with the Izer o function are clisplay. p a s sa n e m p t y v e c t o r I I i n i t s p l a c c .6 toofld. might bc expected. 4 M A T L A B U N C T I O Nf: z e r o J(x) (al FIGURE 6. l ' . t t i o n s .. are any parermeters that the function requires.F 6 . technique the A morecomplete rcplescntation the f'zcrrosyntaxcan be writtenas of Ix. i o n s . An interactive >> opLions = op1imset ('display'. .v. v c t l .( ' p . . l r ' . w as MATLAB session be implemented fbllows: can Solution. f . e r c :\ f u n c : Lj o n . Notc that il'you dcsirc to pass in parametersbut not u s e t h e o p t . .a s t h c v a l u e v a l . x A .
e1. thenuses interpoto lationandbisection until it getscloseenough theroot so that interpolation takes overand rapidlyconverges the root.0 . a.141 interpolati..orr br.90tr318 1.options) >) lx. ! a .Q 30 31 3'.after25 iterations searching.ion interpolaLion inlerpolatlorr rnterlrolaLiorr i nterpolat i ni erpoIat i orr i on 26 21 2B '2 .1442. .t . . of It fzero finds a signchange.14122 in lhe interrral 1 0.14.999023 4. 1el).00001 I If n( H U de let wl no 34 l1 Zero found ir'l VA it'rv. .0. on Suppose we would like to usea lessstringent that We tolerance.('lolx'. y 2 u .5r4r42 0.98q128 0. 4.47I rL6 f (x) 0.998709 0.. t "* a 2 x ' ' .998554 0.l. canusethe opt imset functionltl set a low maxinrumtolerance and a lessaccurate estimate ol'the rootresults: >. = tzcro(G (x) x^10 1.998894 1.5 c. a 1 ertJ the MI tior foll .0:3itsJ 0.9993s1 0.6BlB5e005 1.324 5e D11 0 /. a .61 2636 O. searcll search search 6 t 0.52 4.9P.s the 6.sect i on bisectron bisecliorr interpo laf.5 POTYNOMTATS Polynomialsare a specialtype of nonlinearalgebraicequationof the generalfbrnr t +  .I4 for a zero 0.0090 Di {.24\21 :.5.oplions = optim:.154 ROOTS: OPEN METHODS Funccount L 2 5 6 x 0. r ): d 1 .95254E 0. ' + o u t . f r ( .48 0. B306ie 007 I. 1. L 141.485358 4.xl 1 0009 0.999454 Procedure initial search search search search search v n n r a fr E 23 24 25 Looking 0 .. .385007 1 2.14 I.626056 o .103511 .rl: eig we 0 Thus. . 3i a .2572t I 0.9q9267 0. 01Laarl 7.
many (but not all) cases. structures. In general. engineers fbr like are simpletechniques bisection and NewtonRaphson not available Unfbrtunately.13) r:ompanion matix. (l\ Ul (Irr Al ( 6 . r t + a + x z* o 5 x+ a 6 : 0 + yields Dividing by rrsand rearranging 5 I : (Il f dl I (l I l' (ll I (14 At . approach to recast root evaluation the problems problem. x. However. roots function. .5. 1)  (6. roots(c) the wherex is a columnvectorcontaining rootsand c is a row vectorcontaining polythe nomial'scoefficients. So hclwdoesthe roots functionwork? MATLAB is very good at finding the eigenis the task as an valuesof a matrix.they are used extensivelyin curve fitting. and the a's are constantcoefficients. However. r .1 MATLAB Function: roots If you are dealing with a problem where you must determinea single real root of a polymethodcanhaveutility.MATLAB has an exceldetermining the rootsof higherorder all the lent builtin capability. the algorithmunderlying are erty that its eigenvalues the rootsof the polynomial.Because will be describing eigenvalue Iaterin the book.6. polynomials. electrical and circuits. The roots functionhasthe svntax.the techniques suchasbisection andthe NewtonRaphson desireto determine the roots. described the are tion. alongwith someotherrelated following example. all However.fbr this task. includereactors. . nomial.Consequently. Examples 6. Suppose havea polynomial we . of the roots functionconsists merely settingup the companionmatrix and then using MATLAB's powerful eigenvalueevaluationfunction to determinethe roots.an nth order polynomial will haven roots.in many cases. Its applicapolynomial in manipulation functions. eigenvalue we we will merelyprovidean overviewhere. / t . rootscan be real and/orcomthe the plex. coefficientswill be real. ful applications in characterizing dynamicsystemsand. It has the usefulpropEquation(6.both real and complex.For example.5 POLYNOMIALS r55 In where n is the order of the polynomial. r 5 o 2 x 1+ . Polynomialshave many applicationsin engineeringand science. mechanical devices..one of their most interestingand poweris linear systems. l3) is calledthe polynomial's Thus.12) fiom the righthand sideas A specialmatrix can be constructed usingthe coefflcients by 's the first row and with I and 0's writtenfbr the otherrows as shown: o2fu1 u3fd1 aafo1 a5fu1 aolar 10000 0t000 00r00 00010 (6.For suchcases.in particular.
t B 0 .5a n d . . EXAMPLE . 1.b) Bqe man of l' rang with the resultbeinga quotient(a thirdorder polynontial.1y function can bc useclto pellirrrn tlre satle task as in : D b = poly([0.1 .. ir la) Fin i i . Use the following equation explorehow MATLAB canbeemto ployedto ntanipulate polynomials: 1 (E6.5()00 (r.For exarnplewe can evaluate at the it . It can be entered into MATLAB as the vectorb: .'l'2. by ryping :> p c )l y v a l (a.5000 0.0 . r .0..15 2. We i I r I i ' We X: .5 1]) 1.5 Note that this polynomialhasthreereal rcots:0. l ) 5 .( Be )'> cie where r is a column vector containingthe roots and c is a row vectorcontainingthepoly.0.0 .3 .251.1) r / ! ( x ) : .1.3 ..7. which whenpassed functioncalled of the values the roots. syntaxis Its '' '' l'1Y(r) 6.. 1 2 5 i l ) . 5 ( l ) 1 2 . u 7 5 . l ) : 0 .5000 1. r '* 2 .5 2. Solution.875 1.51.:tb0o We ciin createa quadraticpolyn<lmial that has roots con'esponding two of the to o r i g i n a lr o o t so l ' E q .0000 0 4.000r1 0 calle calcr . and a rerrrainder(r) . 2 :5 .r. 2 5 : l( r 2 ) + t). 5 ) ( .5. nonliai'scclef ficients.3 . Ftir entering following line stores coefficients the vector the the in a: '' a = f I 3.\ i. + 1 . ' / 5 x t 1 2 . Z .5.r : I.1) cl 1.5 .2 6 ' .0000 Note that the pc. 1 2 3 .r56 ROOTS: OPEN METHODS We shouldnote that roots has an inverse po1y. 1 2 5 . T h i s q u a d r a t i cs ( .0000 0. 1 0. 1 5 ( t t + 2 . Polynomials enteredinto MATLAI) by storingthe coefflcients a row are as \/ector.. 5 . I ) wit fbu X 2. .and 2: and one pair ol complex I roots: +0.will returnthe polynornial's coefficients.r ' ' . 1. example. 2 . We can then proceedto manipulate polynomial.. 8 7 5 0 + 1 .500t) We can divide this polynomialinto the originalpolynornial by r:> [q. r r * i 0. r* l ) = . U s i n g M A T L A Bt o M o n i p u l o t eP o l y n o m i o l q n d D e t e r m i n e h e i r R o o t s s T Problem Siotement. w i t ht h er e s u l t .r] = deconv(a.5.
0000 + 0.15A() 2. to througha plant'svascular rangingfrom flow in bloodvessels nutrienttransmission number The resistance flow in such conduitsis parameterizedbya dimensionless to providesa meansto called the..0000 r. rJ000 1.5000i 0!0t)(li t. therootsof the quotientpolynomial >> X rootS(q) with the expected resultthat the remainingrootsof the original polynomialEq.87!0 1.S(.5000i r.Jal.6. (6.6 CASE STUDY 157 Becausethe polynomial is a perfect divisor.1.000t) + 0.lil t:tionJ'actor For turbulenttlow. in throughpipelines andcooling systems.I ) are tbund: 2.0 1.A0 2.. engineering. ln typical applications includethe flow many areas ofengineeringand science.5011 0 ?.0000 0. f .5000i We can now multiply c1 b to colrLup with the originalpolynomial: by > if conv (q.Now.: x I.l: 1 .14) .i2')A 1. 2. b) .otion calculatethe friction factor: o: +* 2orog U1#) (rr.500tr 2. Scientists interested topics are of liquids and gases system.1).2500 We can thendetermine thc rootsof the originalpolynomialby all .2500 P IP E R IC T ION F in Determiningfluid flow throughpipes and tubeshas great relevance Bockground.0000 r.Jr\1(X) l.0 t )( l r 3. we can return to thc original polynomial _:: J l).. the Colebrookequ.7. the remainderpolynomial has zero coeffias can be determined cients.\O 3. (E6.8'l!.5000 againby usingthe poLy function: Finally.1) _\: .00rlrr 0.
'l I. . r /0. Thus. * : : ..#)]' Solution.79x I 0r N . and & : dynamic viscosiry (N. I(I) 1 r t L i t 1 .: . ().005m. it is advisableto plot the function to estimate initial guesses possibledifficulties. D : diameter(m). . liI . t * l ) . r l r . (6.:i  1.eitherof thebrack we eting methodsfrom Chap.005 : 13'743 R e. 5 could be used. z st l o This value along with the other parameters be substituted can into Eq.( I ) t l*ilLl (rr.: r .325 (6.we will illustratehow the numericalmethodscoveredin this part of the book can be employedto determine/for air flow througha smooth. V = 40nr/s are D and e :0.. 7 4 3 Jlf ) J..... 7 ( 0 . slmz.03.For example.1)il . the Reynoldsnumber also serves in asthe criterion for whetherflow is turbulent(Re > 4000).0loel=+=" \ 3 . V: its velocity (m/s).1t ['"(t.. althoughthey producethe corect result. > : t .t : l.thin tube.f)0It)/'I(){)0. lJ.they are somewhatinefficient. : 0. (6.1.).008 to 0.. ] { ).23(40)0.:__ LL where p : the fluid's density {kg/m3t.. 0 0 s ) 1 3 . I 1(1.7 gives a value of/: 0.0000015 2. l4) to give Er gc 0. and Re = the Reynoldsnumber: where e : the roughness oVD Re . : . I l l t . an explicit formulationcalledthe SwameeJuin equationprovidesan approximate estimate: f 1. i . : 0.l4).st \ g(J'l: +2. .This would not be importantfor a singleapplication. ' i ( ' 1 ' ) .'tti i . In addition to appearing Eq.9.t)0:r ( i . the root is locatedat about0. In addition. In this casestudy. 1 r (t i ) ) ) . / [ l L t. (x/ Because are suppliedinitial guesses : 0.0289678with a percentrelativeerror of error of 5.08).rrr As in Fig.rl 0. r f r r i ( ' r r ( i ) ) f lrl.This can be done easily with MATLAB: and to anticipate Ilt.False position yields a result of similar precision in 26 iterations.f Before detennining the root.0015 mm. 6.sim2).t58 ROOTS: METHODS OPEN continued (m). i ) ( lr l t i .08.l) 1 l . f {). ill (.23kglm3. could becomeprohibitiveif many evaluations but were made. The Reynoldsnumbercan be computedas 1 oV D 1.926 x l0{ in 22 iterations. * \ .008 andx. . _ vl ."r.. 5.the bisect function developed in Fig. Note that friction factorsrangefrom about0.Forthis case.theparameters p . Hr di Ar gu fo .! : 1.'"1.t t.
\ i\\ r 2 H.08).l r 'liril ..Llir.05 0.convergence when the initial guessis below about0.....6 CASE STUDY r59 c o n tn u e d i ty ne \2 2 of tis t/s tn. t{I j ':..l i .1 . l( r.:irlt i ll ( l i r i rl ir.Because by to method is a Eq.04 0.008)... ilir l However. 6.08 f FIGURE 6. [e: 1 i\ i'\iiii i i i 1 .066.the newtraph functiondeveloped Fig..02 0.For example.L . .07 0..the NewtonRaphson good candidate..1. 6. 0 1 0.. this occursbecause function'sslopeat the initial the guesscauses first iterationto jump to a negativevalue.when the initial guessis setat the upperend of the range(xn: 0..(6.14) is relatively straightfbrward differentiate.7 converges )(...the routine diverges. Further runs demonstrate that the for this case.9. only occurs ... 5) _?L! " 0 .. r ).fi l iilt. As can be seenby inspectingFig.9 We could try to attain improvedperformance turning to an open method.using an initial guessat the lower end of the range (ro : quickly: in 0.06 0.. ' ' : rl rr I l.Lirll((i.03 0.6.ill1 il . r.......t :i' t i...
I 1 j.gn . 1 ) .l . . r r l I l . zero does f just fine: ' I I t. t ) . 6. i :.for singleinitial guesses aboveabout0. ' .  0. f ) . r irj .l ) . : .08 that caused problemsfor NewtonRaphson.1 . . ' r t . ' . just as with the NewtonRaphson method.14): f. I f ! t r f If the iterationsare displayedusing optimset (recall Exanple 6.15) to providethe initial guessas in SwameeJain ' I :l. t u. f ' .016. ) r r \ . . l ': . r rI I I l j l ) . .00tlli ) \ t.. . ( J l l l . r r c o i . i J : .: 14 . )B ? ..:rIl!t . in this case. 1 . r n L c r e d ( l . r r .the routine worksnicely. ' I 0 I '. .j 1 i l j ( .. . 1 i l r r . ' Li I i i .r p )1 . r r . For example. J L ' t t : t r . ) . 1( 1 .i . is The easiest and most straightforward versioninvolves solving for the first.However. r:. t .. . r r t r J . i r .l r . l ./in Eq.. we can also use MATLAB's builtin f zero function.. / R c ' 0 . c 1 r 1 . s l a r . L u e . it is revealedthata phasebeforea sign changeis detected negativevalue occursduring the search and theroutine aborts.. / " D ) + 5 . l ' .16) . 4 . . l . r i( l f L l r l l i omJtl r:r l : : .1 . 7 4 . : a t r . i . 1 L r L c ' ti ..r :i r r q :rccl r (j1r. .*#))' (6. r . Aside from our homemade functions. r n i n l .6). . . l I . . a i n i l q . l ) i J l l . a good strategy might be to employ the equation(Eq. divergence also occurs when f zero function is used with a single guess.1 it l. L .for the guessof 0. r r .. guessesat the lower end of the range cause problems. l b l r II . / ' ( i lr : o r r l . However. ) r . . l ! i . / LI rr l . r . it requires good initial guesses. I / ( r I ( r ( r .let's seewhetherconvergence possiblefor simple fixedpoint iteration. ' l 6 1 1( t : r /{ 1 . h . . si. t f l ' l r f j I 0 1 ' ' I 4 4 {i i () i t . For example. However. 'r r c . r i r o r l i i r ( 1 r . r t I 'lilll .For the Colebrook equation.25 ('"'(#.: i!. c i rr l s { r J L i n c' l i o n ..( ' . 1 . . (6. 9 ) ^ 2 r ). 9 2 A ' . l ) ! f )I { ) ir . t r 0 r .t60 ROOTS: OPEN METHODS ' continued So we can see that although the NewtonRaphson is very efficient. ' L r r .1 i 4 o n u tl b As a final note. .rr / l L .( i : )i ' r ) t ) / ' . 1  r c t r l .
it conbut vergesfairly rapidly! In fact.08. Recallthat fixedpointiterationconverges when the)2 curve hasa relativelyflat slope(i.there is usually no single methodthat works bestfbr all problems. 6. curve is quite flat in rhe range from/: 0. the fact that the y.they understand of In enoughof the underlying theory so that they can effectively deal with situationswhere a method breaksdown.e.08 "( FIGURI 6.04 0. 6. addition. 10)..008 and 0.professionally developed software like MMLAB is not always foolproof. fixedpoint iteration yields predictionswith percentrelative errors less than 0.0087oin six or fewer iterations!Thus. . estimates The takehomemessage fiom this casestudy is that even great.IO The twocurvedisplayof this functiondepicted indicates surprising a result(Fig.008 to 0.6.Sophisticated usersunderstand strengths the and weaknesses the availablenumericaltechniques. for initial guesses anywherebetween0.10.6 CASE STUDY l6l !t= s6) 0.08 meansthat not only doesfixedpointiterationconverge. As indicatedby Fig. Further. lg'({)l < l).01 0. this simple approach that requiresonly one guessand no derivative performsreally well for this particularcase.
'1. / ( .7 Dcvelop an Ml'ilc tir thc modificclsccant nrcth()d.r'.r'): a . .ll (a) Apply the NcwtonRaphson . 3) .. l'inaliutswer.5 .rc .r . .Pcrtirlntthe conrputation unlil e.284"rr + 6. .. rnethod thet'unction to 6. and.5 r. lbr approxirrating :iquarc thc root ol'any positivcrrulttbcr can be firrrlulatcclas . ^ of until t'. t i : 0 . As a chcrnical fuel ncer. 45tt0kPa arrd7.1.(\l(tr. fiirclion. 1 +(.5 lncl iterate 6. l ( .0../ i t ' .0(X) kPa. r r l l 6 . wiitelvapor 6.4.15 Thc volurncof liquid V in a hollow horizontal o1'r'adius lcngth is relatcd thedepth thcliquid/tby land L to of (a) Use a numerical me parameter ?".5usingi'11 5. I . (86.I : (a) Craphically.21. un 6.r.1 I L \T / J /r r Dctelntinegiven : 2 nr.4 Dctcrrninc the l()wcst positivc r(x)t ol' .13 In a chcmicalcnginccring that is hcatcdto sLrlTiciently tcrnpcraturcs n significant high portion of thc watcr dissociatcs. the fol ofthe cable can be derive d'y ::dxz w TA r:(t:I lt)JT gas whcrc R : tlre uniycrs:rl constrntl:05ltt kJ(kgK)1. .5mr.6 : (a) Graphically. (c) LIsing the sccantmcthod (thrce itcrations. ) nlethodand (b) lhc nrocli6. r*ru u .f) : 0.l(r): tanh(.6 Dcvclt>p Mfilc lirr thc sccantnrcthod. polynomial / (. suchthat t:50./^r f Y l (P6.tod . (K). 6.Usean initial guess . is lcssthane.2 Use (a) fixedpoint itefltion antl (b) tlrc Nc'ur'lonRaphsonmcthod to cletcrminca t'clotoi .rc : engi.\' Tr\ .5. = l9l K./ (..5 Usc (a) the NcwtonRaphsorr (3 ol ticclsecant nrcthocl : 0.ti luf . (b) Develop a plor of v ) 6.3. w valuesof/ such that 1 : 6. r r +1 0 .1 6 . Explainyour Along with an 6.DiscLrss usegraphical of and to in and analyticalr. and a capacitor in par to expressthe impedance ..t 12.0.t:t5 . t i : 3 .. 6 u 7 5 r * usirrg an initial gucss ot' .1) 1.1. f . tll 6.rru)cthod thisfunc(ion to usingan Apply thc NcwtonRaphson y o 1 i n i t i a ls u c s s f . 1 . t 1 . pressurc 7 : afrsolute tenrpcrnturc / : absolute luncl : thc volurncol a kg ol'gas {rrr/kg). 1 6 .13.2 andtakea minimum of of thrcc itcrations. . 5 . 5 . totalprcssurc thc rnixturc. (kPa).l6a.18showr tor. 3 5 r + 3 1 ./ (.t : 0. :0. 5 .} / r . whcrc 7r. l .. < 0. /r .05%. rcsults. t ) r' .rl + : l. filnnoxyo to (H:): gcn (O1)anclhydrogcn H:OP H: * lO: involved.01 . 0 1 7 5 . thrr NcwtonRaphson 'r. ttrctl. . r .you arc askcdto dctcrnrint: ilmountof methane thc that can bc hcld in a 3rnt tilnk a( a tcmperirture 50'C of methodof with a prcssurc ol'65. as Tcst thLEq. ./ y : _.J atnrandK . the If it is assLrnrcd this is the only rcaction thut nrolctiactiont of HlO thatclissociates bc Rrpresentedby can I A:.L : 5 rnr.ll DiUcrcrrtiate (86.. 0 1 ) .l t ) 2 . passthc l'unctiorr an argunrctrt. in cylindu 6.16 A catenary cable i points not in the sarr Fig. r  .nl\ w */\ . 3.3 Dctcrrnincthc hishcst rcal root ol . 9 . it is subje weight. t.t1. (d) Using tlrc rnodilicd scculrt nlethorl (llvc itcllti()n\.r thatsatisl'ies (P6. Also checkyorrl 6. its weigh length along the cable u scction AB is depicted in the tension forces at the e cal force balances./ (.Ola/a.. of dctcrnrinc valr.3. s{ ( ' . thc by ccluation statcis given ol 6. J: 0 . 12 Thc 15 a rcal ro<.h : l L V = l l r c ..t1 1 Provc that this firnnula is bascd on the NewtonRaphson algorithm. P6. r l  0 .andl': 8.0.ucthods cxplairtany pcculiaritics y'our rcstrl ts. it by solvingProb.I + 6 .6. (b) 4.1.0u66R l). 6 . i l n d . (b) LIsinc mcthod (thrcc itcrations. Thc parameten ir tr irntllt arc calculatccl by . T . : 0. 0 5 .162 METHODS ROOTS: OPEN PROBTEMS to thc 6. t 5.17 An oscillatingcurre bY I :9et cos(2tt). r .(b) Did thc nrcthodexhibit convergence for on(o its r:al root?Sketchthe plot with thc rcsults each iteration labclcd. Calculus can be employ height of the cable y as a Ta . of Eq.ti: 0.0074. 6.c .r 1.2). (lt) Using the NervtonRuphson (thrcc itctirtiotts..l) l() gct Eq. (c) Dctcrnrinc thc rootswith MATLAB.10 Thc "di"iclcand avcragc"tlctlrocl.43.r ): : s i n ( v 4 ) .1 Enrployfixedpointitcration locate root ot . : (). i ) : . u s i n g n r n i a aud tial gucss (a). isthe ll'7r. (H:0) proccss.5 . r ..18 Figure P6.l : 0... Along with the initial gucssand the perturbation pass I'Lrnction rn argurncnt. 3 . Usc an initial{ucss rir: 0. . oldtimcInctho(l zr.\' whcrc is thc rcaction's K ccluilibrium constultandp.8r * 2./ (r) : 8 s i n ( . a 6..5t125 ancl t.05) to clctclnrirtc root . 7 .r has bctwcenl5and20.E x p l a i n o u r r c s u l t s ..183.ra 0.0.t) : 0. P T c s ti t b y s o l v i n g r o b .2.I3. (d) Using thc rnodificd sccant rncllrod (thrc'c itcnttiotrs.t) : . splits irpart. ils thc (wo initial gucsscs.: 3 5 ) .14 Thc RccllichKwong R7 ''tlt 6. : l.Usea rtxrtlocating your choicc to calculatcl and then the of dctcrnrine mass rncthilnc containcd the tank.r : 1. 3. 9 . Thus.r2 9) to evaluatc its known real rootat : .M.95..rgiven and Y6 : 6.o .5.35.:3'5)' (c) Using thc sccantnrclhod(thrcc itcrations.9 Enrploy the NcwtonRlphsonnlcthod to dctennincr c a lr t x r tf o r .o.4)7 R r r ': ' 5 l).
a b l o c k o f m a s sr i i s s .19 . inducan acaoacitor oarallel.6 . Basedon horizontaland vcrtiforce balances.its weight acts as a unilblm load per unit along lcngth the cable ur (N/m).I8 #=. and ror the angularlreguenc_v whereZ: impedance is Frnd the ar that resultsrn an rmpedance /i (/ usrngthe ot tzero lunction with initial guesses I and 1000for the of lirllowing parameters: :225 A.where/ is in seconds.18 showsa circuit with a resistor.l6u. 1 9 .0.19 Real mechanical syslemsmay involve the deflection o l ' n o n l i n e a r p r i n g s I n F i g . P6. is subjectto no loads other than its own it Ihus.r: l 7. Deterrrrine all oftsuchthatl:3 Figure P6. A lreebody diagrarnol' a ion is depicted Fig. l0r' F.Kircfihofl'sru/escan be used in theimpedance o1'thesystemas I ll p: (Q). C' .q and = $.y fbr..1 . and R L : 0.r : 50 to 100. @t +(. An oscillating current an electric in circuitis described l=9e' cos(2rt). P 6 . suchthat the cablehas a height of .s r=50.c FIGUREP6.l6D. Drvelopaplot versusr of. fbllowingdif lerential the cquation rnode I cablecan be derived.cosn ur Use numerical a methodto calculatea value lbr the parameter given values for the parametersut : 12 I.FfS canbe employedto solvc this equationtbr the ofthe cable asa lunctionof distance. FIGUREP6.I6 P A cable is one which is hung between tw<r 116 catenary pintsnot in the same vertical line. (r. As depicted rn P6.5 H. where T1 and Ts are AB in tension lbrcesat the end.r': I 5 at r.PROBLEMS r 63 (b) FIGURE 6.
llRrl 3 wherc' V: volunreIm]1. H = depth(m). 0 0 0 g / s 2 .if urr:20 m/s. P6. Thc trdecbry can be modelecl as p o s i t i o n i ns y s t c nils g i v c nh y g Rt v .5 1 . initialguesses of0 and R will bracket a single root ti)r this cxample. polynomial ft and /r . s + d 2 ) ( .26 Use rhe NewtonRa f (x) : FIGUREP6.S : slope (mlm).2l The Manning equationcan bc wriften fbr a rectangulat openchannel as (^ : =r . 6. z : 4 0 g / ( s 2 n r s ) . and tor. r* h ) ( s * l t t ) . yor result for Reynolds numbe e/ D ranging from 0.7 ) 6.7 to manipulatc and iind all the roots the of 5 0 . and R : t h e t a n kr a d i u s m l .) coordinatcs tem's input to its output.Extra wherc zri andb1 : the rootsof thc numerator denomina.1.r64 ROOTS: OPEN METHODS P fixedpoint iteration sch givenQ:5. Use MATLAB to find thc roots of thc numerator de. l and n : the Manning roughncsscoefficient./r : depthof waterin tank Im]..S:0.l 1 9 2 2 Find appropriate initial angledo.27 Given f (x) : 2xb .A relatecl problemdcals 6. transfer tunctions ae u.2 You arc designing sphcrical a tank (Fig.n t g < l .0002 your schemeconvergesf< equal to zero.d3i'1 FIGURE P6.5sl*50. l If R : l m.x "ns^ 4 Conscrvation ofcnergl'can bc uscdttr show that (a) infbrmation: For hracketing methods. given thr: fbllowing parameter in Example6.21 ln control systcrns analysis.ith the traicctory of a thrown ball.0.2I ) to hold water fbr a small villagc in a devcloping country. an {): + . The volume of liquid it can hold can bc computed as l / n l t '  = where C(s) : system gain.(b) Fot 2krtl5l2 I open mcthods. ( : 9 .5(sH)''' rt(B i 2H Jtlt where B : flow (m3/s). Develop .r'1y C(. initial guessof R will alwaysconverge.. k 1 t l '".2I Employ initialguesses ( of results.The resisa I tancefbrce F of the spring is given by F\krt! *k.5. C(s) : systemoutput. that of thrown by a right ficlder is clefinedby thc (x. and the the distanceto the catcheris 35 Note that thc throw lcavcsthc m.0000 6.45 m. 6. fespectively.n : 9 0 8 .(tan0o)r j5r' Ltrai cosu0 Usea rootlocation techn this function.4 ) ( . 5. in P6.r  2 ) ( . Determine the approximate relatir"e cror after cach iteration. .20 Aerospacc enginc'ers somctimes computc the trajcctories of projcctilcssuch as rockcts.20. and nominatorand t:rctorthescinto the form G ( . right ficlder's hand at an elcvationof 2 m and the catclrcr rcceivcs at I m. Thc trajectory o1'a ball devr'loped mathematically rclatethc dynamics asys. l s ( x ) : ( .N(s) systcminput. r 3 ) * ( s * D r ) ( s* b : ) ( . providejustification fbr your choice of method. .22 Pertbrm the identical MATLAII operationsas thost valucs: : ft1 Solvc fbr z/. 6. 8 1 m / s 2 . A transf'erfunction for a robotic as displayed Fie. what depth rnustthe tank be filled to so that it holds 30 m3? Use three iterations the most efllcient of nurnericalmethodpossibleto determineyour answer.1.l ) ( x . and s : Laplacetransfbrmcomplexfrequency. r )= ( . it 6. Also.2O released distance above a nonlincarspring.t) r'u + c(s) rt+ l2. r..tt = N(. r* 5 ) ( x .r*66 l 9 s r+ 1 2 2 s+ 2 9 6 s.Perform iter tive error falls below5Zo *. r+ . s a r ) ( .25 Seeifyou can deve the friction factor base describedin Sec.t t t q l t tl 6.6.
r). fiom to 6.0. a .use an inior of. and (c) 8. S : 0.B :20.05.tr Employ gucsses (a) 2.0 and r.0002.f(. 6J?Given Zrb .use an initial guessof . Assumingthxt converof gence not an issue. : 1.6. Yor.211 You must determine the root of the following easily f di fferentiable unction: n <. If you usc thc secantmethod..Note that it is then to the positivcinitial guesscs. usethal technique determine root.usc initial guesses r1 : [) of and.7. ?"^ = )lr justify your choice. is choose technique the that is bestsuited to this problem. : l.rr function should return a precise rcsult Reynolds numberranging from . (b) 6. and r?: 0.Perfbrm iterrelative If ationsuntil the approximate error fallsbclow 2olc.PROBLEMS fixedpoint iterationscheme to solve this equation for H given= 5..r..r. r65 of use initial guesses x1 . . 6.: 2. If you use the NewtonRaphson the modified sccantnrethod. techniques known thatfi:rr except all fixedpoint iterationwill cvcntually converge. you use a bracketing method. Perlbrmiterationsuntil the approximaterelative enorialls below 5%.26 the NervtonRaphson Use method to find the root of :'@_ x)2 . initial use guesses x.03.r.l. 1 :0 and 0 JT ie le re S ic v. Lrseinitial guesscsof .5.00001 0.If you usc a br'ackctingmcthod.6.Justify your choice. I : 0 and Xi : l.25 if youcandevelopa lbolproof function to compute See fie frictionlactor based on the Colebrook equation as described in Sec. Explainyour initial of rcsults. If you usethc NewtonRaphson the modified or secant mcthod.r. Ifyou usethe sccant tial guess method.and Pick the bestnunrcrical tcchnique.Provethat 0 your greaterthan or scheme converges all initial guesses for equal zero. t0 6. ).r4 lox * 2 * f (t) = (he Useroot a location technique dctt'rminc maxirnumof to this function.1000to  07and lbr for e/D ranging 0.
e I(tn'I)t) (.its altitude a functionof time can be computed to as as I ( f I t66 ry.. t . g D i s t i n g u i s h i nb e t w c e n l o b a la n dl o c a lo p t i m u . g problemso thatit can bc solvedwith a Knowing how to recast nraximization a r n i n i r n i z i na l g o r i t h n r . Knowing how tclapply the f minse.rrch lirnctionto clctermine minimumof a the m u l t i d i n t e n s i o nIau n c t i o n . g Bcing ableto definethe goldenrati<l why it makesoneand understand dinrensional optitni zatione'flicient. Specificobjectives topicscovered and are ' t ' ' ' ' ' ' ' t Understanding and whereoptirnizafion why occursin engineering scientific and problemsolving. T) tr . Knowing how to apply the f mi n brid f unctionto deterrnine minimumof a the l' l o o n e .1 YOU'VEGOT A PROBLEM jurrper can be projectedupwarrd a specifiedvelocity. * Z: zr* 1 (. n object like a bLrngee at If is subject Iineardrag. Recognizing difference the betwecn onedimensional multidimensional and optirnization. 'l 7. Locatingthe optirnumof a singlcvariable f'unction with parabolic interpolation..Opti mizqtion CHAPTER OBJECTIVES The primaryobjective the present you to how optinrization of chapter to introduce is can be usedtcldetermine minirrra and rnirxima bothonedimensional of and multidimensional functions. Beingableto developMATLAB contourand surlhce plotsto visualize twodimensional functions. Localingthe optinrum a singlevnriahlc clf functionwith thegoldensection search..driu e n s i o r t a u n ci t r t .
to perspective.I INTRODUCTION BACKGROUND AND Maximum elevation F'GURE 7.l r tr< t / t v t ( w h e r e : : a l l i t u c l ( r n )a h i l v ct h cc a r t h ' s u d : r c ed c t ] n c d s : : 0 ) . .t\l:u c s :{ l r r .Z.1 E l e v a l t a no t q f u l l a l i . involvessearching the function's for cxtrenre optimization . scientists Iravcintcresl optirnalphenorncna irr firlnr thc pcukclcvationo1' rurnging projectiles the minirnunrli'eccncrgy. introducc is 7 . .1s .':.j o h l ' d c t c r r r i n i ntg ce x l c t l i r n eo l ' t h cp c a kc l c v a l i o n .1 : t l Ok g .atit'rn sinrilar in spirit to lhe root locationrncthods just covercdin is wc Chaps. optimizationproblenrs requirc nurnerical. G i v c n t h e l b l l o w i n g p a r a r n c l c r v. l O 0 m . r / s r .T h L r sc n g i n c e r s r c a l w a y sc o n . t l a pnrblerls that uttcnrptto balarrcc pellirrnrancc frontingoptirnizittion and linritatiorrs. 9. substituted its back into thc functionto conrputc optinralvalucs.rc li l h i r n i r t t r o l r t . ' o . and6.l r : r n i l s sk g ) .E c 1(.h i s c h a p t c r i l l n i t T w y()uto how the cornputcr uscdto nrakcsuchdetcrn'riniltions. p<lints. ol i b at l S u p p o s t h a ty o u a r cg i v c nt h c . ln contrast.7.I ) c a n b c u s c dt o c u l c u l a t ch c j r r r n pres a l t i t u c l c . . a n d r ' : l 5 k g / s . s c r r g i u c e r w.( : a l i n c a r d r a g o e l ' l ' i c i c( ktg / s ) . c n (s). 1 1: r 5 . optirniz. . t h c j u r n p c r r i s c s t o a p c a k c l c v a t i o n1 9'0rn ro u a h o u t l : . Root thc is in Iocationinvolvessearching thc locationwherethe I'unction for cqualszcro. Frour a nrathcnraticirl optinriz.c n r u s e o n t i n u < l u s l c s i g n c v i c c s n c p r o d u c t t h a ( A t s a s dy a l d p e r l i r r m a s k si n a n c f f i c i c n lI a s h i o n i l r t h c l c a s tc o s t .optinri:/. " . both involveguessing searching a pointon a function. l . t r ' o f t i m e f o r o n o b f t r c l i r r r i l o / / i p r o f e c l e t l u S t . T .7 . l o : t h c i n i t i a lt l t i t u c l e e s a ( m ) . 1 INTRODUCTION AND BACKGROUND In thc nrostgcneralscnsc. that on The goal is to detcrnlinc the valuesof tlre variables Thcsecan then be that yield rnaximaor minirnalbr the I'unction. r : i n i t i ' r lv c l o c i t y r n / s )a n dI : ( q ( . cornputcrsolutions. 5 r n / r . 5 ancl tilr The fundarne'ntal dif'lbrcncc between two typesol'problerns illustrated Fig. That is. No(c that (irr tlris liurnulation" positivcvckrcityis corrsidcrcd bc in thc rrpwlrci tinrc to d i r c c t i o n . c o h T h c d e t c r m i n a t i oo l ' s u c hc x l r c m cv a l u e s s r c l c r r c d o l s o p l i r n i z a t i o n . A l t h o u g ht h e s c s o l u t i o n s a n s o r n c t i r n cb c o b t a i n c da n a l y t i c a l l yn r o s tp r a c t i c a l c s . From a numcricalstandpoint. s ri ' A t d i s p l a y c d i n F i g .ation dcirlswith l'indingthe nraxinra and rninimaof a l'unctiorr dcpends one or m()revariablcs.2.ati()rl thc ploccsso1'crcatirrg is sonrcthing that is as c f f b c t i v c s p o s s i b l e . In adclition.
. the point is a maximLlm.r value whcre thc derivative / '(. 7.Forthis case. ( S o l u t i o n . "(. by solvingthe root problenr: .the problen anrounts deterrnining root. second thc derivative.t'"(x) < 0.r166s 80r9.l'"(. min n : D'e \ ( / t t t) t  tn8 (.r).168 OPTIMIZATION l'('t) : o l"(r) < o l(x) : o T m /'("t) = o > "f"(. for That is.ln fhct.theoptirnunrs thepointswhcrethccurveis flat.1 a maximum:il' .2 7 n v e A f u n c t i oo f o s i n g l e o r i o b l i l l u s t r c f itn q d i f f e r e n c e l w e e no o f s n do p l r r n o he r be o tht gri As can be seenin Fig.) analytically lbr r :'!rn (r + !a ) tng/ r' \ r S u b s t i t u t i n gt h e p i l r i i r n e l e r g i v c s .r1 EXAMPLE .r) 0 TT FIGURE . (E7. Deterrnine tirnc and magnitude the il1'thepeakelevation basedon r E q .55m/s. u0 .ldt. you can diffcrentiate function thc andlocateth root (thatis. this is actLrally ccluation the vclocity.L l. U s e t h e l b l l o w i n g p a r a n r e t ev a l u e sf o r y o u r c a l c u l a l i o n.81)/ l.o : 100m. to the this can be accomplished settingthedcby rivativeto zeroand solvingEq.l Z D e t e r m i n i n g h e O p t i m u m A n o l y t i c o l l y y R o o tL o c o t i o n t b Problem Stotement.this corresponds the . nt : u0 kg.Ll. E q u a t i o n 7 . understanding relationship thc betwecn a ble strategy finding the latter. 1 )c a nb e d i l f ' e r e n t i a t eo g i v e td dz sat AS t__ ofr on( prc sea me ltou * ( {oJ PqU rh. Note that because . andr: : l5kg/c. 8 1 m / s t . 1) thc fbr Themaximun elevationoccursat the value ol I that drives this gquationto zero. to t0 Additionally.In mathare enrirtical terms.f'(. 0 .someoptirnization rnethods justthis do : 11.8.2. indicates whether optimurn a rninimumor the is .Thus. the pointis a minimun if rootsandoptimawouldsuggestposiNow.:g : 9 . .r) is equal zelo.s \ . the zero)of the ncw tunction. (l  e i'l"'t'.r) > 0. 1 ) .: l5ts5t 8o r n/ r r \ :3.
8 1 r 6 . . t . r 9 ./ .. n .. Further.m . " .' t/t. 8 3 t 6 6 ) : 9 2 .1) to computethe maximum elevation as z: R 80 / 8 0 r Q lr \ 8 0. r . 3 8 3 1 6 " "1 _= " . are These methods available both are fbr problems.. the As search thenconsists climbingor desccnding of peaks onedimensional andvalleys. o t e t h o tt h i s o N op l r g u r e o n b e t o l e n t o r e p r e s e n i i t h e r m o x i m i z o i i o( c o n t o u ris c r e o s en e l e v o l i o n p t o c n e o n i u lhe moximumiike o mountcrin) c minimizotion or decreosein eJevotion down to the icontours m i n i m u mi k eo v o l l e v ) l Optimum/ir'. The fhct that the secondderivativeis negativetells us that we have a maximum. Althoughan analytical solutionwaspossible this case.u t ) € . . FIGURE 7.  5 0 + " " . Multidiproblemsinvolve lunctionsthat dependon two or more dependent mensional variables. g 6 0 9 1 m 15\ rs /' ls We can verify that the resultis a maximum by differentiatingEq. and 6. / ( . _ . ) ( t _ . L3a... 1 58 0 .u'r Minimum. This will be left in 5 as a homeworkexercise. r ) ( b )T w o . (7. in Fig. possible approach Althoughit is certainly to optimization a rootsproblem. variety as a of directnumerical optimization methods available. (E7.3 ( o )C n e ' d i m e n s i o n o lp t i m i z o t i o n h i sf l g u r eo l s o i l l u s t r o t e s w m i n i m i z c i i oo f 1 1 l r 1i s T ho n i t e q u i v c r l e no t h e m o r i m i z o l i o n f .INTRODUCTION AND BACKGROUND This value along with the parameters then be substituted can into Eq. the name implies.9^ 81 .l("r) .r .1. 1 0 0 + : . . .onedimensional onedimensional multidimensiontrl and As problems involvefunctions thert depend a singledependent on variable. n \ t "ntlt ln .1)to obtainthe secondderivative d2z .: (llc . t t . . . the result makesphysicalsense sincethe acceleration shouldbe solely equalto the force of gravity at the maximum when the vertical velocity (and hencedrag) is zero.d i m e n s i o n o l l i m i z o t i o n .( t/(' . . couldhaveobtained fbr we the sameresultusingthe root locationmethods described Chaps.
Finally.5ancl6 that root location was complicated the fact that several by rootscan occur tbr a singlefunction. p o w h e r e c sf h et w o t o t h e l e f to r e q o b o l .T h e l w o p o i r l s1 ot h . A gkhul olttittrtrm the not though th very best. we arenot constrained walk peirksand valleys(Fig. A u s e f u l i m a g e i n t h i s r e g a r di s t h e o n e .is betterthunits inrmecliate neighbols.we will describesome of the mole common approaches onefor dimensional Then we will providea brief description how MATLAB optimization.However. brackcting approachparabolic polation. 7. 7.7 .3d.ln suchcases. the lett rolr Euc whe Mul wher q 'l FIGURE 7.v i c i n i t y f l h e o r i q i n .instead topography examined efTiciently is the to reachthegoal.r70 OPTIMIZATION ln the samespirit. of can be employed determine to optimafor multidinrensional functions. ) . Similarly. that includelocaloptima calld Cases are multimodol. In the next section.r i g h tr r r e o c o o p t i m o . mustbe concerned wc aboutmistaking localrcsulttor theglobal a optimum. dcscribed thenextsection. 7. Recall liorn Clraps.d i n r e n s i o nr o l l e r coaster"like lirnction dcpicteclin Fig. h o t hl o c a lu n dg l o h u lo p t i m i r u r o c c u ri n t r p t i n r i z a t i o n . In aft the pr( ofi 7.2 ONEDIMENSIONALOPTIMIZATION This section will describe techniques find the mrnrmnmor maxrmum a function a to of of " al s i n g l e v a r i a b l e .4 A {unction thirl osymptoticolly trvo moximum opprooches zero ot p us ond minLrs ond hcrs oc ond t w o r n i n i m u m o n l s i n t h . Justas in rool location.the process findrng a maximum versusfinding a minimumis essentially of /(.A local optinrunr. optimizrtionin one dimension be dividc'd bracketing into can andoperr methods. 7. will almost we always intercstcd findingtheglobal be in optimunl In addition. c represcnts very best solution.4. wit We will then show how thcsctwo nrethods cornbined are and irnplemented M A T L A B ' s I r ni n b n d f u n c t i o n .3b). .a twodimensional optimizalion againbe visualized searchingout can as just as in realhiking. to a singledirection. goldensection As in search anexamplc is thc of a bracketing nrethod in nlethod thatis very sirnilar spiritto thc hisection lbrrootlocation This is firllowcdby a somcwhat moresophisticated inter. In ad provir termir T roots i guess tween The rc xr Global maxtmum FIGUR Euclid s rotioof smoile Global F 1 . l ( .r). This identicalbecause the samevalue x* both minimizes/(x) and maximizes graphically a onedirnensional equivalence illustrated is for in ftrnction Fi_c.
2) Multiplying by t 1/(.3) 4. of or "A Iine is saidto havebeencut in extreme andmeanratio Euclid'sdefinitionreads: straight when. was veriflcclby deterrninimg that. a function.r 9 8 8 7 4 9 8 9 ...)and an upperguess({.l'(. _ ." so The actr"ral valueof the golclen ratiocan be derivedby expressing Euclicl's definitionas t1IL2 Ir lr (': (7.6 : 1 .2.The prcsence a root beof a had difl'ercnt signs. (2 t+l. areall familiarwith "lucky 7" and "Fridaythe 13th.Recallthat bisection guess(r.it arisesin a varietyof ilthcr contexts generalpurpose provides basisfbr thc goldensectiorl rncthocl dcfbl the scarch. The positiveroot of this equation the goldenlatit'r (7. of a circle'scircumference its diameter and the baseof the natLlral Although not as widely known. 'tt I x. r .r.1 GoldenSection Seorch qualities. by fee).. in theWest certainnumbers ascribed For we In manycultures."Beyondsuchsuperstitious wellknown numbers that havesuchinteresting powerful mathematical thereareseveral and properties that they could truly be called"rnagical."The mostcomnlonof thesearethe ratio to z logarithme. example.For our pulposes.51 FIGURE 7. (7. specil'ied a lorver by rootsin Chap. 7. (7. which is typicallyreprescnted the Greek theonof remarkable numbers.) and. 300 BCE) because its of leuer Q (pronounced. terminingthe optimumof a singlevariablc lbl searchis sirnilarin spirit to the'bisection approach locating The goldensection hingedon definingan interval.l(.rfolwo segmcnls lhot fhe io segrnenl ihe roiio of ihe whole line io the lorgersegmentis eguol lo the roiroof the lcrrger segmeni Ihis roiio is co//edthe go/den rofio smo//er (t . The goldenratio has long beenconsidered it includingbiology.5. is the greater the lesser. are rnagicai quantities. 6t 8 0 .1) pleasing Western aesthctically in cultures. sirnple.5. was originally definedby ELrclid(ca.As depictedin Fig.2.5 so of on Euclidsdefinition the golden roiio is bosed dividing o iine i. In addition. the golden ratio shouldsurelybe includedin the panThis quantity. role in the construction the pentagram fivepointedstar.: l+\.) tweenthesebouncls as The root was thcn estimated the midoointof this intcrval: '. .) that bracketed singleroot.as the whole line is to the greater to segment.DIMENSIONAL OPTIMIZATION l7l 7.r.2 termsyields and collecting o'QI:0 is where@ : L r / ( z.2 ONE.
008or 0..r.eachroundthe intervalis reduced a 1'actor d .87c).r.2 Gol< Prob 50lu . l ( x.crechosenusingthe goldenlatio.A kev advantage this approach of bounds. Fclrthis case. intervalis shrunkto about0. r bccotnes new . Ratherthan value(which is sufflcient detecta sign change. This is done we Eq. I t ' ...lirr thc next round. fiom ol' lirr .r : ) ..172 OPTIMIZATION The final stepin a bisection iteratjon involveddetennining new smaller a bracket. two intcrnediirte the ratio: .8) based the new valueso1'.rr) at Two results can occur: Thc functionis evaluatcd thcsetwo inleriolnoints. If I ( v:) .rll( .r.Forthe by golclensection points are chosenaccordingto the golden sc'arch. .I (about61.For bisection.6) with r/cornputed with Eq. it the this casc.r. new. Asimila would lre usecl thc alternate fbr cascwherethe optirlLrnr in the leli subinterval. can adoptthe samenomenclature for bisection. hereis thc neirl hcnelit {l'clm uscol'thc goldenratio. by of or that after l0 rounds. intervalcontaining extremum reduced are the Thatnleans In fact. . The key to makingthis approach etficicntis thc rvise choiceof the intermediate lirrrction As in bisection. the sameas the l'unction To cornplete with thc algorithrn./had a functiorrvalue with the same was that the new valner. fcll approach For this case..l.7. the the is rapidly.). hencea usinga singleintermediate to and function valuesto detcct whcthera minimum zero). a s i n F i g . we do not havc to recalculatc thc function all valuesfor thc next iteration.\/ +d . (50Vo\.\ thc (7. was This sign done by replacingwhicheverof the boundsr1 orr. in Now suppose that insteadof a root. a n d t h e d o m a i n o f .r. can startby dcfining an interval a onedimensional we that a That is.r.r.l(r. (7.). this is a harcler with bisection achicved birt Droblem. needonly detcrminethe new.r.this was accornplished choosingthe midpoint.Yt:. bscclmes new .and4 is calleduninrodal. r . points. on rud..7). we were interested detenniningthe minimurn of function.As with bisection. ) i s t h c r .r.r. l ( . r t o t h e For leli cll'x. replaced oneof the old asl(r. then/ (. (7. . conlrasl we for to bisection. As the iterations repeated.l)(:r. example.)is the rrinimum andthc donrain r to thc rightof . as where.theold"r. goal is to nrininrize the evaluations rcplacing valueswith by old nerv values.0066% This is not quite as good as the reduction . We respcctively. 6 a . in beFor tbr lbr the new. EXAMPLE 7. we would need two intermediatc occun'ed. This meirns that we alrcadyhavethe value valueat the old. in definedthe lower and upperbounds. needa new strategy findinga rninimumwithin the interval.rrwould be computed with Eq. t h e n . sinceitis comesthe new rr.Becallsc oliginal.rr). suchan interval. thc ncxtround.rr to .8% of itsinitiai the length.it is about0. clurbe clintinatcd becausc doesnot containthc rninirnum.r):xud where .. of However. l. (7. 7 .liorn r.. andhence contains singleanswer../ (.would bc eliminatcd.to r). u. n i n i n r u n r . d:((h . thc the and r.1) ) Now. the intervalshouldcontaina singlenrinirnurrr..6.Afier 20 rounds.6l8r{) 0.81 (76) (1.r. thc caseillustrated Fig. .
Solution.2 ONEDIMENSIONAL OPTIMIZATION r73 S n d )f rt e f.n n S. (al rh le :n x2 t1 t I I New (b) ne or )m rd.721 rr :0 +2.0) : 2. ls on )eis irh lar al.rl FIGURE 7.61803 ..2 ProblemStotement. .the goldenratio is usedto create two interiorpoints: the (4 d : 0.2 0 i n r l within the interval from r. First. tns :ial ron n EXAMPLE G o l d e n .: 0 to jr.4'.4121 :1. Use the goldensection search find the minimum of to 1 s "f ( x t : ..S e c t i o S e o r c h 2.4121:2.4121 xz:42.: 4.2.5279 . rld+xl St .6 (o) The initiclstep of the goldensection seorcholgorithminvolves choosingiwo inleriorpornts occordingto the golden rotio (b) The secondstep involves deflnlngo new intervol thot encomoossene oollmum. Llv.
.2 s i n ( l..0 . valuebecomes new . . it was determined the previousiterationas l (.53l0.. an exactupperboundfor the crror can be calculatedat eachiteration. r . do nothave the we to recalculate on /(x1).. t 5432 17647 t 7136 t 7647 1 7755 t 7732 xu J'(x. r.7)to computethe new valueof r/andr.our best estimate the minimum at this point is thatit n focatedat r :  .1641.5. Using similarreasoning./r(.If optimumfirnctionvalueis at r.2 s i n ( 2 . .1 .5219 or es : rz :2.8) and(7.5219with a valueof .4).).) 0 0 r0 _53 5 3t 0 7595 7595 7595 7742 x2 f (xr) 7647 5310 7647 7595 7647 7755 7742 7755 xt l&) 0 6300 *1 7647 . .174 OPTIMIZATION The function can be evaluated the interior ooints: at l(x:): I (rr) : 1.f (r) : l ..r... 5 2 1 9 ) : .5279.1 (.BB85 ) 5279 1 66s6 I 5279 I 4427 I 4752 40000 :tIt36 o 6.. gol wo nuI l.52192 .1 . scribed rr.rr.x. 5 2 1 9 ) : .0s 9 9 4 3 ) i. ..rr. . by The process be repcatcd. thatis.Sincethisvalueislcssthanthe f u n c t i o n a l u ea t .. a n dx .r.rr.1 .sc) 3050 3 9 0I I 5279 o 944'3 5279 3050 5279 4427 3 9 0I 4427 24721 I 5279 I.1. All that remainsis to use Eqs. the optimumfunctionvalueis at. I n a d d i t i o n .lOO 2 4721 B8B5 t. : 2 .100 2 4721 (l c.[(xt1 .h e m i n i m u r n c c u r s t x : 1 . b e c o n r ets e u p p e rb o u n d .) a I 2 3 4 5 6 7 B 0 0 o 9443 4 s443 3050 30.7641 addition. : l.ln we the minimum is in the intervaldefinedby r..ln adclition.for the next iteration.5432 BBB5 ) 5432 6656 I 7136 5279 t 7647 t 7647 5279 2 A/21 ) 5279 0 9443 0 5836 0 360/ a 2229 0 I3l8 0 0B5r Fit ap Note that the current minimum is highlightedtbr cvery iteration. r . 1 1 5 5 . optimumwill eitherfall inone is the of two intervals.5279 0.. upperboundfbr goldensection an search canbe derivedaslbllows:Oncean iteration complete. t h t the fbrmerr. can with the results tatrulated hcre: or the Th xt J'$.: d : 0 .. Alier the eighth i t e r a t i o nt. and.. 7 6 1 1 a n d i t i s i n t h e i n t c r v a l r e v p . h u s . Because the interiorpointsale symmetrical.r. h a ti s . it will be in the lower interval lf (.9443 T h e f u n c t i o n e v a l u a t i o n a t ...r. : / 0 .. i on No Co Recallthat for bisection (Sec..r. t h e o a v T r e s u l t s c c r n v e r g i n g t h e t r u ev a l u eo f . 1 1 2 10 ) : 1.).] t . 1 2 1 6 .412t2 . eithercasecan be usedto definetheerror. 1 1 5 7 a t .(7. 1 6 4 7 l0 2.it will be in the upperinterval(. t h e m i n i m u mi s l ( 1 . 1 7 2 1.r. 4 4 2 1 w i t h a l i r n c t i o n a l u c c t l ' ..4J21 . alsoknow that . 6 1 8 0 3 ( 2 .5219) : 1. Thus.r. and. r : 1 .r1). 6 3 0 0 t0 Because of .r.. 4 1 2 1:) . lower the b o u n dr e n r a i r l s ..
.rr) .7.r1).i.7.).5 5 . it In kecpingfunctiort evaluations a minimumcoulclpay greatdividends suchcases. 8 6 0 9 .d ) ( . z r ) .lor solvirrga single optirnization. to for . suchcases.1. fiorn the would be estimate A. .h ne xF SC w N o t i c eh o w b e c a u s eh i s i s a m u x i m i z a t i o n .) ) nr*q... .rr) .. h You nraybe wondering why we havestressed reduccd the functiorr of evaluations thc goldcnsection sLilrch.. .2 ONEDIMENSIONAL OPTIMIZATION Lookingat the upperinterval(xr..:ra .r.. the This result can or 0..86r) 9 th ne 6.sl . i > ) ' q .1 e.Therefbre.I 0 l ) .n. thenbe normalizecl thc optimalvaluefcrrthat iteraticln to yicld to .3820(. O .r'. i'. .+ ( Q ..O ) l ' : ! . .)+ 2(0 .t..x . the maximumilistance or 0. f rn i ri . a the fbr the goldensection searchfor minimizationis presentcd in An Mfile f'unction returns Fig.r1. If the true vaiuewereat the far right.the approximate crror..'. (zt)+rnlc*(vO+m*!. :r. n t = 8 0. t t o t s o C o n s e q u e n t l [ m i n c o r r e s p o n c lo a m a x i m u m e i g h t l ' 1 9 2 .. . wherethe goldensection algorithmmay be a part of a much largercalculation.r1) : ( 2 . 1 ) .8317 lnii n 192. 8 1 . 'f . r u.*L) )7 29 7B il . : = 1 \ .However..'nr I t.l)(.). . ) >xmin = c/m*1...r. the true valuewereat the fiu'left.artcl numberof iterations. i L c r I = g o 1 ctn i n ( z . r t I ( Q . r r ) Therefbre. Therearc cases search Many evaluations... v O . .D Q ..r1) : (20 . case this would represent maximumerror..(Q l)(.. 79 1? l6 This cstimate provides basisfbr tcrminating iterations.3)(.:TI .there are two importantcontextswhere mininrizingthe Theseare numberof functioncvaluations be important.r.x i ) : (rr . ( 7 . le t .t't (Q l)(x.1. r . y.2361(r. : : {.9356c (l0'r u.. .r..Z._.r2 : .: J lx t 0 0 % I r. 3. : ( 2 .r r ) .the maxif imum distance from the estimatewould be Ar.the valueof the function... e h a v ee n t e r e d h c n e g a t i v e f E q .{rr I . speedsavings Of thc would bc negligible. can l.'c)*(I erp( :=Q(L) r: >  x n i n . course..The l'uncticln the locationof the rninimum.l ) ( x . may be calledmany times.r.. the The Mfile can bc uscdto solvethe problenr frorn Example7..r.ri) : (r.
. xl lower and Jppe' oLesses (dlefault . x2 = xu .C. sucl For cases.p2.. SO tic end iter=iter+1. to For exarnple.d. if xopt0.maxic. You shouldunderstand that a functioncan be very compler and tinreconsunling evaluate.rrgin.0001.[x.error('at least 3 input argumenLs required'). s Sr . co ca x2.approxirnaLe relative error = number of iterations iter (?) if rr. optimization can be usedto estimatr the parameters a modelconsisting a systemo1' of of diff'erential equations. iter0.end wl op FIGURE .he minimum of f % inpr'rg. t = name of functior1 Z oo r'l .7 7 A" A/i. xopt vl. = additionaL parameters used by f % 9: ^rrrrirrts.i x 'pL.on of mrnrmum fx = minimum function value ea .176 OPTIMIZATION r nr ior Ix. fx. .br:eak..goJdrin lf . ea = (2 .iterj=goldmin(f. while(1) xI). end end xxol'.vararcJin{:}) xopt = x1.1)* (xu xI = xl r cl.end p h i .'/a'or:in :] . use simple functionsil we most of our examples.0001?) % es = de:.. Pa pr. es=0.end if nargin<5 isempty(maxit).) % uses grolden sectjon search to find t. Any method thal m i n i r n i z e s u c he v a l u a t i o nw o u l db e a d v a n t a s e o u s . FI ul 7. irer l. d = (phi. "function" involvestimeconsuming fhe model integration.3.varargin) % goldrrLin: mjnimizaLiorr golclen section search % : fx. For pedagogical reasons. if t(x7.x'.p2. x1 = 7).phi) * abs( (xu if ea <= es I iter >= maxit.e.ea. ? ? ? ? x = locaLj.es. ( 1 + s q r r ( 5 ) ) / 2 .fx.rminp the mrnimum of o lunciion with the oolden.maxit. xl) / xopt) * 100.ired relative error (default = 561 % maxit = naximum allowable iterations p1 .end if nargin<4 rsempty(es). . maxit=50.le 13 jpr5.es.3 $(AAAPLE Po Pr 7 Timeconsunring evaluation.xl.\tararqirr{:}) < f (x2..xu.seclion eorcfr s 7.xu.pI.
ancl iirc thc initial gucsses. .l0l wherer'.p i c t i o n p o r o b oc i n t e r p o l o t i o n d of i 7. . r .2 ONEDIMENSIONAL OPTIMIZATION 177 Truemaximum ) .rr is the valueo1'.. a to of . y o i n t h r a c k ea n o p t i m u m w c .. . v ii EXAMPLE 2. a n d _ r .r True function Parabolic approximation of maximum Parabolic function FIGURE .2.2. 7.r. A . : s S o l u t i o n .raight corrnccting points.i 1 ' w ch a v ct h r c cp o i n t s h a t .ancl it. r .8 7 G r o p h i c o l. cltnl. a h o l f ct t o t h eg u e s s e s .0 .s829 /(r'./(.g). to Thcn wc candil'lcrcntiatc sctthc rcsultcqualto zcro.rl(7.): 1.3 P o r o b o l i cI n t e r p o l o t i o n ProblemStotement. :.fustits lhcrc is tlnll'ttnc sl.rdordcr polynornial olien providcs good approxirnation the shapc l(.l(r. h t ly t .1136 . in Lex ate rch lat r v i t hi n i t i a lg u e s s eo f .ita parabola the points.1 . and.r) ncaran optimum(Fig. Use parabolic interpolation approxirnatc rnipirnurn to thc ol' /(r) : rl lo 2sin'r l . It can bc shownlhloughsontcalgcbraic rlanipulat i o n st h a tt h c r c s u l ti s rl:. solvelbr atr estitnale o1'lhcoptimalr.thereis only 9nc parabola linc two c o n n e c t i ntg r e ep o i n t sT h u s . .2 Porobolic Interpolotion Parabolic interpolation tltkesadvantagc ol'the lirct that a sccor. r.):0 l(r. T h c l u n c t i o n a l u e s t t h c t h r e e u e s s ec a n b e e v a l u a t e d : v a g s r f) r'l .rthat corresporrds the tq o p t i r r r u n ta l u cu l ' t h cp a r .):3.
r . 5 0 5 s 1 ) l .l X. resultis converging the rapidlyon the true valueof 1.r78 OPTIMIZATION into and substituted Eq. i l 3 6 1 ( l . the next iteration: lbr rr :l r: : I.rl J@. first attempts parabolicinterpolation keeps unreliable.55 : 1. "f(rJ 1 /691 1 7711 ) 7757 t 7757 )7757 I 2 3 4 5 0 000cr 0 0000 I0000 r 5829 I0000 .s 0 5 r5 t .4903 I ( r .t f k.5829 3.r 5 8 2 9 4 0000 3 r 36 I 7 6 9 1 4 0000 J I I 36 t 7714 5055 . 7 6 9 t.t 7 75 7 0000 5055 4903 4256 4266 .+[ .4 we described builtin MATLAB functionf zero. 5 8 2 9 3 .( t .1114.6. I 7.4 ) 2 .1691.li.3 MATTAB Function: rminbnd Thu itera Recallthat in Sec.The process be repeated. Therefore.1 . It and ap plying it as long as acceptable results obtained.) x2 f (x2) r. 7.2.Becausethe function value for the new point is lower point (.5055) 1. ] . dependable searchwith the faster.it usesthe goldensection are If search to set mattersin hand.50. Next.butpossibly parabolicinterpolation. goldensection tion.t /714 ) 7757 49A3 I 7714 1 .5055 t 4903 I 4256 I 4266 14 2 7 5 Thus. 4 2 J 6 .5829 . a strategysirrrilarto the goldensection searchcan be employedto determine which point should be discarded.. 1 l 3 6 l 2 ( 1 .r'): 1.the lower guess (x. 5 0 ./k.76e1 .3 * l (l . .r .r:) and the new x value is to the right of the intermediate than for the interrnediate point. l l 3 6 l . 7.0 ) [ . It combinesthe slow.1 . within five iterations.11361 0 .l'Q): 1. l 0 ) t o g i v e n c ra = 1. The fminbn. 7 6 9 1 ) which lrasa functionvalueofl( I .I .f function provides a sin. 7 6 9t 3 . not.5 5 I ) 2[ .0)2L1. Thisfunction the combined severalrootfinding methodsinto a single algorithm that balanced reliability with efliciency.t 7691 1 7757 4qa:J . s 8 2 9 0 ] : which frasa functionvalueof /(1. ( 7 .) is discarded. (7.):3'1136 r v h i c h a nb e s u b s t i t u t ei d t o E q . 5 8 2 9 0 1 [ ) 2 ( 1.4903): l .7757 a t .t .t 5B2a r o o o o .suIts tabulated here: .rilarapproachfor onedimensional minimiza.3 MU Asid func a rol valle a har . r: 1 .10)to give r'.t 5829 1 4 2 5 6 .5055 xl: 4 whe func Exar . thc with can re..
Here is a simple MATLAB sessionthat uses fminbnd to solve the problem from Example7.and alier eight iterations.ToLX of 004 3 . the to of 7. z A. B(r1 792.0001.mrnated: x satisfies 1.83159 ter.851 192 .xl. B1 .5 5 r m= BO. we can display calculation details: options fnr i = ofitimset ( 'display' .85836 3. B6r 792 .r1 Thus.the image becomesthat of mountainsand valleys(Fie. > > g = 9 .2.83. after three iterations.88854 3 .83r52 3. 'iter') . . c = 1 5 . For example.81544 3.851 r92 . >> z=@(t) (20+n/c*(v0+m"g/c)x \1exp( c/m"t))m*g/c*t) . optional parameterscan be specified using optimser.000000e rB9.87I 792.3 MUTTIDIMENSIONAT OPTIMIZATION Aside from onedimensionalfunctions.8317 f192. and xl and x2 arethe boundsof the interval being searched. MAILAB's graphiccapabilities provide a handy meansto visualizesuchfunctions.05573 4.x2) wherex and f va1 arethe locationand valueof the minimum.the nrethodswitclresf'romgolden to parabolic. B6r 192.861 the termrnation criteria usinq OPTIC)NS. X= 3.94427 r. Funccount 1 2 l X f (x) Procedure inirial go 1den qolden parabo 1i cpirrabolic perrabolic parabolic P.As in the lbllowing example.1.l. optimization also deals with multidimensional functions..3 MULTIDIMENSIONAL OPTIMIZATION A simple expression its syntaxis of fxmrn.3b). v 0 . function is the nameof the function being evaluated.7 .83166 3.159 187.L 0 a .3a that our visual imageof a onedimensional search was like a roller coaster.rrabolic parabo 1i c 4 5 5 1 B 9 OptimizaLion the current 3.19 I/I.For twodimensionalcases. fvafl = fminbnd(functian.8609 As with f zero.83332 3 . minimum is determined a tolerance 0.Recallfrom Fig.
2. z l a b e l title('(b) M es l .xl +2xrrz + xi Solution. zmin=f l. ( t]:' Yl = meshqtrid x'Y) . cs=surf c lX. Use MATLAB's graphicalcapabilities display the following to function and visually estimateits minimum in the range2 < xr < 0 and 0 < . Z ' ) .xz 'l. * Y + Y . both plots indicatc that function has a minimurn value of about l and. '.D i m e n s i o n q lu n c t i o n Problem Stqtemeni. 7.ror(min (Z)). 7.i Sta mu dire han whr the (' f (x_I. : r I a b e l ( ' : ' . 4 0 ) .. = l i n s p a c e ( 0 ._2)' ) . { subploL i'7. x l a b c l { ' y _ 1 ' ) . " r 2 ) : 0 t o I l o c a t e da t a b o u t r l : FIGURE 7.r. ( l :. Y . r ' .f(xt'x) :2 i xt .9.l i n sp ac e ( . 3 . _ ?).ls o Modimensionol functron of Bo wh forc fere * lo)  the (a) {b) .4 7 OPTIMIZATION F V i s u o l i z i n gq T w o . .5.2 t..r t r ' l o t ' ) .Jrid.2 .'{. x . trtle(' subplol \I.r80 EXAMPLE . xlabel ('r1' ) . : l. f t . r b e l ( c s ) . .r2 < 3: 2 . the As displayed in lrig. z m a x .4 nLtn on {fAt uat funr 7. 2 + x .'+ 2 * x .:<. C) 4 A ) . r x( Z ) ) .\'l.' ' 2 .2. i. The fbllowing scriptgenerates contourand meshplotsof the function: .7).r . c ! . ^ 2 + 2* x . z = .9 (o) Confourond (b) rneshplc.rbel (':<2' ) .e iI ( m . (a) Contour ploL' ) .Z). = c o n t o l l r ( X .
or direct. As in Fig. is The displacement relatedto the When a horizontalforce is applied. will divide them depending on whetherthey require derivative evaluation. The potential energyof the deformed stateconsistsof the difference betweenthe strainenergyofthe springand the work doneby the force: PE(i :0.7.1 IVIATLAB Functiohs fminsearch the Standard MMLAB has a function fminsearch that can be usedto determine minifunction.an unloadedspring can be attached a wall mount.As describednext. the builtin MATLAB functionfniinsearch is a directmethod.the spring stretches. simpleexpression its syntaxis of fxnrr.11) 7. F : kx.IO FIGURE force An spring oitoched o wollmount. It is basedon the NelderMeadmethod.3. 7.x2) andvalueof theminimum. > f = @( x ) 2+x\1 ) x(2) +2*xlI) ^ 2 + 2 * x ( l * x ( 2 ) + x ( t : .7500 1. )^ 2 . OO0O = 0. methods.5001) ENERGY EAUILIBRIUM AND MINIMUM POTENTIAL to Bockground. ) f v e ri 1.4 CASE STUDY t8l Techniques multidimensional for unconstrained optimizationcan be classifiedin a we numberof ways.fttnction is thenameof where xmrn and frzal arethe location the tunctionbeing evaluated.For purposes the present of discussion. 7. Applicotiono horizoniol siretches to of {b} {o) unlooded isdescribed Hookes thespring the force by low where relotionship beiween onddisplocement . that usesf rninsearch to determine rninimumfor Hereis a simpleMATLAB session in thefunctionwe just graphed Example7. fvall = fminsearch( function. and xl and x2 are the boundsof the intervalbeing searched.5kx2 * Fx (7. The approaches do not requirederivativeevalor that uationare callednongradient. methods.Those that require derivativesare called (or gradient. force by Hookes law. descent ascent).4: : .70a.xl.which is a lnum of a rnultidimensional directsearch method that uses <lnly function values (does not require derivatives)and A handles nonsnrooth obiective functions.
skb ( If the parameters ko: 9 N/cm.11)definesa parabola. continued Equation(7. : 2 N/cm.F. Lo: 70 cnt Lo: l0 cm.I+:l.5 cm.I FIGURE I A 1"wospring (o)unlooded (bllocded.In this two degreesof freedom in that the system can move both horizontallyand are In the same way that we approachedthe onedimensionalsystem. the solution for displacement be viewed as a onedimensional can tion problem.r+5x2+3 6e minimum by findi Use bisection r 1.11..t 'i. (7.10)yi guesses ofr. methods to fu dl values of r.5x6+2r4+1 fiurction. Sincethe potentialenergy will beat a equilibrium.OPTIMIZATION it. that minimize the potentialenergy: 12 5€ The _ ^ l2 + (L.f k : 2 N / c m a n d F : 5 N .x ) 2 * L") + o. and x. k.. r = 5 N / ( 2 t 2. . t Example along 7. r : F / k . system: ond PROBTEM three iterations the root of Eq..S. . use MATLAB to solve for the displacementsand the potentialenergy..i l ..0. the following f =3+6.1 the formula =x2*8x12 the maximum this function analytir thatEq. Becausethis equation is so easy to differentiate. = 2 are Fz = 4 N.1. 7. Thu loca 7. F o r e x a m p l ei.the equilibrium mationsare the valuesof x..we can solvefor p l a c e m e na s . A more interestingtwodimensionalcaseis shown in Fig.
r. . b + x \ ' 2 ) ) ^ 2 ) L b ) ' l .(r RepeatProb.tr interpolation : 1.2 .1 La '2. of Employ initial guesses using the goldensection 12 J ( r= . F 1 .ll Consider fbllowing function: Given r 1 y ).2'.l .1 .P F l a + E b.f r n i n s e a r . .! r .f (xl : ro + 2tr + 8x2 + 5r = + method of three Perform iterations the NewtonRiiphson valfherootof Eq. =i 0 . 1) ' 2 + ( l . 5 .r) : 0. (7.: .5 Solve for the value of xthat rnuximizes.ti : 0 andr. (.. (E7.1. $.t n 2 x " + 1 2 l . PEb=0. xp1l.5*kb* W . initialguesses : fl. (a) Goldensection search ol' nrinimunr findingthe root ol'the derivative by the (b) Parabolic (.r ( 2 ) ) ) ( s q r t ( x ( . andxr : 2. L b . i . .. l .F l * x ( 1 ) + F 2* x ( 2 ) .: of function.: 2. ] I x . the and the colresponding Delermine maxinrurn analvtical ( i.l).L a . P The solution can be obtained with the fminsearch function: . ir : 2. methods show the lirnction to and graphical Use analytical has a minirnum fbr some value ol' x in the range *2 < x < l. f ] .W .5. : b . except use parabolicintcrpolation.09 l .the potentialenergyis 9.7.10)yields the sanreresultsbasedon lbrm threeiterations.2'759 aboveits original PROBtEMS (c) Dillerentiatc' lirnction and then Lrse rootlocation a the methodIo solve lor the maxirlurn.9\2 i f I . : 2.r. : I. c h ( B P E . L a = 1 0 . 0 . 2 . t . r r =l ' the 7. at equilibrium.42.75.rr: 2.t. u x 2* 1 . E " t . ) P E a =0 . and perfbrrnthree iterations. ." 0 . valueol' 7. bisection : iterations 5).f in Prob. to An Mfile can be developed hold the potentialenergyfunction: p = P H ( x .9 . Enrplov ly rfor functirx lhis initialguesses . : l(n. cm located4.r a '(r) r = 3 { 6r* 5r + 3xr +.1 3 0 . F ' I . r'z 2 Use with initial [uesses r. p . that : of.6422 N. rnd perof Vedfy Eq.9523 cm to the right and 1.v. l 2 ) function ^.7 Enploy the l'ol<'tring the Considerlbllowinglunction: I ( r ) : 4 x . k a .cm.r r + 8 r ) . Usethe parameter o E x a m p7e 1a l o n gw i t h a n i n i t i a lg u e s s f r : 3 s l. the Given fonnula the Plot function. methods prove that the function is conto Useanalytical of for cave all values r. (. 7.v. .e.r) 7.usingdillerentiation). . r.l(.5.PROBLEMS continued Solution. 5 * k a * ( s q r t ( x ( 1 ) ^ 2 + ( L a .r)and the corresponding valueul'r. nlethodsto t'ind the nrarirnum ol7. 3 . 8. 5 l 4 .: point is Thus. : 4. The connecting position.. l . .4 search. k a = 9 .. k b ..lra (.
z thc tr..11t)cvelop an Mfilc (o irnplcntcnt shouldhavc thc Thc subroutinc tion (o krcatca nrininturn.e ./(. . shouldhavc finding thc rninimunrof with a ntassof 100 kg is prolected upward 7. 2 u s i n gL ' .: $Qp. i')..17 A ob.attd.r Rcturnboth thc optinral and.1 .. r .: g andr.25 The Streeterphelp.r). tolcrance.:li ancl(c) suhstittrte result with 7.q.1{)Considcr fbllowing 3 .8r+(l+0. thc thc subroutinc l 7 .rnction: 7. : thc initial angle(radiansl.8: (. kN/cnr:. P7.r25xy*2 7.22 Thc spccilic gt'owthratc of a vcast that producesan _r: 6 cosr (.ig wherer : end displacernen values fr.g : thc gravitational r u ( r l r i e g r o ut h i s u r t r : r x i n r u m . usingthe goldenscction [n with thc goldenscction a rnaxirnum error falls below e. .3. onlibioiic versusthe fooo t FIGURE P7.rt'lr ltlnction to detcrrnine . Unreacted processengineer has fbund is a function of the convers will result in the lowest cos constant.lc/c).C = 50.L. Dctcnnine thc rninirnuttr (hc lrLj rr. t h a tn t i n u n 7.24 A contilever beom .(b) nurnetically . ..: lc/o of .rr : 2. criItcratcuntil thc rclativccrror lllls bckrrva stoppinS . 7.___ f(x.2 brackctit tlaxitrturn. Rcturnboth thc optinral and..: 2.dctcnninc(hc 0 lne.r.I.ttitxirtrunr ()f cratcthc third initiirlvalucat thc rnidpoint thc intcrlal..r: 0 to 6 s."ntrrn.rr' ttutnhcltll'itcrationr.r r .Commenton tlreconvclgcnce yttur rcsults cm{. .y)5.1 .5 kN/cm. alsogoes zelo8l cluc due whcre .ingf'eaturcs: liom tlre surluccol'thc carth at a vclocity of 50 rn/s. . dissolvedoxygen .2(] l0$ As dcpictedin Fig..Findthevalueof h u.. ) 2 .1.*!L p k a * k ..itions to toxicitycitects.rlinimrnl ot' 7.: 50". of scarchto clc(crttrirrc location the Usc thc goldcnscction .. :5).20 Usc tllc f nriri. r''*rir r : ( t i u l H { ) ) .22. The product B separatlon unit.oinitialgucsscs. r ' .25 o: o. Ralhcrthanusingthc Inaxitrtuuitcrations und L.. 5 . ( X X ) 1 ..r) : 2.t mo.9 Employ the fbllowing mcthodsto fincl the minitnurn of thc function fiorn Prob. 1 .icct thc tblloia.and o 1 .1 )o .on.sls . t .12 Dcvclop arr Mf ilc to krcittc l ntinimum with thc goldcnscction search. . : l.and0.r. tcriorior cxcccdsl rnaxintunt .t . : the initial vclocity (rn/s). : .r': the height(m). Thc data bcst l'its thc curvc I .5.5 sin.1.24 A finiteelement mode loading and monrenrsrF.14 PrcssurcmcasurL'nlcnts takcn at ccrtain poiltts (. 2 .r 2 lirllowinglcaturcs: the r 7.ttiot'l is lirnction prcssurc. p7.16 The dcflection of a unifbrm bearnsubjectto a linearly loadcan be computed as distributed increasing P 7.k . rr__' I I FIGURE P7. criIteratcuntil thc rclativcclror talls bclow a sk)pping to ob. y:: 1. l '. S/.usethegoldenis nunrbclol'itcration. (a) glaphically. u' : 25 rn/s. .: ol'thc lixld conccntr. 2 1 C i v e nt l r cl i r l l o r v i n gu n c t i o n : shouldrcturnirncrr()rnrcssagc. 2t with o1'aball can bc cot. 7. v ) .j crrs'11" cosr:. a tcrionor cxcecds ntaxinrunt thc heighttheobjeo to scctionscarch detcrrninc rnaxirnum . r..: 0 ancl. 7 .r. . v.'fcst the inflcctionpoint of this curvc firr positivc t() numbcrof itcralions nccdcd attaina dcsircd e y o u r f u n c t i o n y s o l v i n gE x r n t p l c7 .specificgrow. {lection(a) graphically'.r * J 0 . 7.r. r . r r + 2 .[f the ./ (. Tcst 1.rr: 0. Iterate usinginitialguesses approxirnate ertor falls below €. .i{r. .. 7 . : (a) Goldensection search (b) Parabolicinterpolation(. chargeof sewage (Fig. detcrtnincthe point of maximum 7.r'): tlr * rl + l2r' + 4r'l 2.r * 2 .1: 30. havcthc progllut gcuofl.1 ..nputcd 7.^ .r.: 2. the .(t . r .t l'rom. Llsclilur iterations 7.r84 OPTIMIZATION : constant 9.: 0 .= j .icct subjcct lincaldrag(r' : l5 kg/s).Li l'unctitln.000 (. / ( .ll Devclopan Mfilc that is expressly (b) sealch scarch. . 1 9 l J s c t h c f r l i n r .Usethe golclensection search to determine the nraximum height : untilthe givcn t'u I nt. (7. r I r : l rl u n c t i o n t o d c t c r m i n t h e b parabolicintcrpoltl.4 I ( d .9) as thc stoppingcritcria. .pprpfogramrvith thc samcptoblctrtas Exanrplc tlic ol' (b) back into thc l'unutionio clcternrinc rninimum arc 7.5+r .22 7.Itl The nornral with thc sarncproblctnas Exanrplc Test y()ur prograrn 7.8 I rn/s:. . iterations 0. high conccntri.l (. r r . "t attai ns. I.23 A compounclA will I tank reactor. bchind an airlbil ovcr tiuic. antibiotic a rncthodto l'indthc nrininrunr o1'thcgoldcnsearch Sctr. : l% with initial sct if up so that it dircctly finds the traxitnum ratherthan until tlie approximatc /'(.r. 2 5 . : Check whethclthc gtrcsscs / 1 . f(.000 Giverr thatL: 600 crn. and Bascit on tu. othcr words.= _5(ys+2L)r'Lrrl to Perform IO itcrationsof parabolicinterpolation locate I)()EIL of the minimum.:'1.h .15 Thc trajcctory 4+0. . 2t . : 5) rr deto dcsigned locate ahd ur.r. .growth gocs to zc() at very to It conccntrations to lcxrdlirnitation.r. Thc subroutine guesscsof r.r.r).Il ntlt.r)..t'n the initial height(rn). d. ' 3 r 2 but shouldnot inrplcntcnt algorithrn.r. ./(..1 .r: : 0.:2and. r ' ) 4 . curvedefinedby is clistribution a bcllshapcd 7.
r7: l0 iC' w h e r e a .. r : .r1. rof the (mg/L). o.\1.25). P7. .r "cri(icirl"hecause rcpresents lrrcation it the wherebiotuthat L\{l.rned crrnductrq rr\th rrli\rl. r* 0 .nent.:c r1 i. : r a t e o l ' s e t t l i n g o l ' B O D I ) . the FIGURE P7.r.t . ( X ) 7 .A unit. is a proportionality in C on$ant. 2 1 .\'. r ) : 7 .r. 'I :   .J t t ! ( c t ' . bclow thc point discharge.9 m. srrme oxygen"sag" thal reaches criticalr. l ( ) u r g / L k. t ' 0 . pocess engineer foundthat the initial costol'thc syslcm has ilafunction conversion Find the conversion ol'the r. IIGURE P7.:..t 1 o\:\mc€ \ lr()m tnt center ()1Lr\er\ng tFrg. l 3 . arounda t'ing7. 0 1 6 r ' r. r x 'r 'tO .r. .24 Aconli beom ever 4ten 1t2 1 47131t rrCr/(Nmr.(.4 8 0.21 t.05d I L. 14.r.' Sr p(le^"t ej. r:ivcn (Fig. A rnodelof a cantileverbearnsubjectto ?J4 finiteelement thc and concentriltiun..ft. = ..: sediurent oxygendemand (P7.\. that rillresult thelowestc()stsystenr.The \orce exerted on thc charge by the ring is given by )' low ro at le of  RGURE P7.r I :ally )sult num :s an o = 0'..27 A total chiirgeQ is unitbrnrlydistributed sh'. t.:0.\'+ i.he :nThespecific growthrcle of o yeosf thot produces cn onlibiol c vefsus food concentrotion.: 0 .25)produces As indicatcdin Fig. : whcre o : dissolvedoxygen conccnlration (mg/L).25 A dissolved cxygen "sog" below o point dischorgeof sewoge Inroo rver./.r:0.. .randl thatrninimizc11. 9 t O .rl)/ I dependon oxygcn (likc I'ish)would be thc most stressed.y : end mol. Deterrnine distance whet'ethe force is a mlxir.24)is civen by optinrizing Deternrine critical tlavcl tirrre' hoding monrents and v thclbllowing alues: rl J{.. The StreeterPhclps mtxlel can bc usedto cornputethe oxygen concentration a river helowa pnin(clisin (Fig. r e a e r a t i o n (mg/L/d). . 0 5 . .. : 8 . kz : rate ol'decomposition thc ol' (d B O D ( d r ) . and Find the of. arrd5.r..( i .\r':q11{ .: I rng/L/d ol' distribution ptrllutant concen7.\' ( . t. ofsewage P7. ln levela.0 . t d  r = enddisplacenrent .PROBLEMS r85 rt )S 0. This point is called '6('l Cost=cltl I \.d . I r"" r r""l traveltirnet.). ratL'(d r).\.tl'rltt I rr.f .r : lraveltirne(d).22 ch ial rrd . productB and unreacted ureA The loparation UnreactedA is recycled to thc reactor..26 The twodirnensional trationin u channcl can be described by r ' ( r .:.. oxygensaturatiorl conccntrrti()n (BOD) concentralion L.r*k.. .:50rng/L 5r.nininrunr a l. t 1x5 l 0 Q:2x x the and d : 0. i + t ' ) / ) ' li.2 ld_1) ly (mg/L) or' 0 0510 c (mg/L) lc.. P7.i ttt.).. F. r \ thrr. : biochemical oxygendenrand irt rnixing point (rng/L).\' . by 7J3 compound will be converted into B in a stirred A A purified in a hnkreactor. 6 t l I r .t'turtr.25\ Determine exacrt the Iocation given ol'thepeakconcentration t h e l u n c t i o n n dt h e k r r o w l e c l g c ' l hh e p e u kl i e sr v i t h i n h e a t l at {1(..0 . r'). P1 . f r .25.
o : ratio of air densitybctween flight thc As altitudcandscalevcl. FIGURE P7. thc location ol' the maxirnum stress can akrngthc r axis (o 7.2t1The torquctransrlitted an indr"rction tion of the slip betwccnthe rotationof the statorfield and thc rotrrrspced whele slip is detincdas .. Minimiz to determine the equilibrium ltc forcing function F: l0( Jd t6 . drag increascs 0.000 (r FIGURE P7. P7.dcterrnine minimun the drag anclthc velocity at which it occurs..10 Rollcr bcaringsarc sub. lV: wcight. tl tlR 0 FIGUREP7.6.r86 OPTIMIZATION PR r_ ro D 20.29.000.31 In a similar fashionr Sec.Usc u numcricalmcthod lirrgc contrct loacls/i tFig.(xx) with o : 0.s) .r').29 Plot drcg versus velocity cn oirfoil for of FIGURE P7.and V: r.thc drag due to lifi dccreascs.28 Torclue ironsmifted on inductoros o function slip to of FIGURE P7.sr 3 s * z l ) to 7.clocity.4.27 motoris a flncto 7. with velocity.: rotor spced.31 Twofrictionless mosses cor lineor elostic springs. Thc problenr of to dctcrminethc slip at which tlrc rlaximum torqueoccurs.3L plots in MATLAB. develop the poten tem depicted in Fig.l5 N/m.6 and W : 16.' w: 12. to ( I . P7..r l+rl 04 \ Find thc .iect fatiguetailure FigureP7.3O Roller beorings.tlrc two l'actors contributing drag alc i'rfWhcreasfrietion lected difl'ercntlyas velocity increases.'. scen to in Fig. P7. whcre rr : rcvolutionspcr seconcl rotatingstatorspccd of laws can bc usedto show and ri. ( b ) In adclition.29 Thc total drag on an airtiril can bc cstirnatcdby shownto bc cquivulcnt maxirnizing function: the n D : 1 1 .r that rraxirnizcs /(.r.000 10. anllysisto determim devclopa sensitivity to d how this optirrum varics in response a range l5s(l.10).4+'F /t __/ rr'Ir \ _]+.32 .r)(4. 1 1 1 o Y ' +( # ) ' :T Fr'iction Liit whcrc D : drag.28showstlris function. P7. 7.1 . KirchhotT's in fbrm) artd slip that the torquc (cxpressed dirncnsionlcss arc rclatcdby The combination of the two f'actorsleads to a minimum clrag ( a ) lf o :0.000 20. 732 As an agricultural engi open channel to carr) ine the optimal dime perimeterfor a crosssection dimensionsuniversal'l 733 Use the funcrion rmins ofthe shortest ladder that rea ftnce to the building's wall (l where ?34 The length of rhe long 6e corner depicted in Fig. 7.
sfnfi stn(ftaH) "'' : For the casewhere ur1 : r. Minimizethc potcntialcnergyl'unction letermineequilibriurn the displacenrents and r."s. t Thelengthof the longestladder that can negotiate in comer depicted Fig.+ . Testit lirr the case L _ .lr2 2 m. i n.32) to netheoptinrill to dinrensions rninimizethe wettecl fbrr crosssectit'rnal of 50 rn'. usea numericalrnethod describedin this chapter (including MATLAB's builtin capabilities) develop plot ol'L versus range to a a ola's from 45o 135". l5 N/rn. P7.. F forcing tunction : 100N und the paral)rcters k.4. derelop poterrtial the fol energyf'unction the sysin depicted Fig.1 can be determined by _. Are the relativc area l universal the tm Use lunction i ri:..t ^ A l o d d e rl e o n i n g g o i n s o f e n c eo n d i u s t o u c h i n g w o l l o l t o computing the value of e that minimizes the fbllowing function: by ing be L(o): Ll. given .e:ach to deternrine length r the shortest ladder that reaches liorrr the ground over the tolhebuilding's wall (Fig.f 'i .34 A iodder negolioting formed trrohollwoys o corner by .. ."'.33 um ine of ln a similar lashionto the case study describedin ?.33). q n FIGURE P7.:20 = tr. P7.r < . P7.^J .1. open channel carry irrigationwater (Fig. you rnustdesigna trapeAsanagricultural engineer. P7.3  hictionless connected o woll by o poirof mosses fo e .t .PROBLEMS r87 P7. '. P7.il.r. Developcontour and surflce tnMATLAB..32 FIGURE P7.
s arecon_ the stanrs.xr. Such systems are either linear or nonlinear.. (PT3. fJ.": ..f r) : 0.:l!:li. we deal with the caseof determining the values* ri. fi..:l T are based ano. . mathematical In terms. part In .... Now...i:o : . satisfy a set of equations: f.*:.x. . nrri. : aztxt * azzxz .. Sometamllar quantities that conformto such laws are mass.* ar.. : xr..Te value xrhar satisfied a single equation. .' '.. are linear Algebroic Equotionsin Engineeringond Science ::iy^.anO momenturn. .. energy. and n is .. .i Lineor Syrtems ii 3.these teadto balance continuiry or equarions rhar llT'p.xr) : 0 . : 12.. I ovERVtEW Whof Are Lineqr Algebroic Equotions? In Part Two.t) wherethe rz'sareconstant coefficients.x. x. + aloxn [..relare systembehavioras represented by ifre levels or fundamenlat equarions engineering of r89 . of 1. b.y.xp .1.1x1 anZXZ * I .: equatrons. deal with lincar algebraic equarions that are of the generalform axt * anxz+..*.x.. . thatsimultaneously *r.):0 fik. otheralgebraic All equations nonlinear. we O"rrrrn::.lx. ''..f. . * : : a1.... scrence on conservation laws. rhe * unknowns. * a2. fi .
nust solvecl be sinrultaneously. you saw how singlecomponent systems result in a single equationthat can be solved using rootlocationtechniques. PT3. Multicolnponentsystems resultin a coupledset of mathematical equations that r.PT3. and so lbrth. the Finally.l ). As an example. response the quantitybeing modeledto the properties characteristics the system of or of alrdthe external stimuli or fbrcingfunctions actingon the system. resLrlting the equations are often of the linear algebraic folm of Eq.theb's usuallyrepresent the forcing functionsactingon the system. principleof massconservation be usedto formulatea model the can for a series chemical of (Fig.The forcing lunctions arethe feed ratesof the chemicalinto the system. Multicomponentproblemsof thesetypes arisefiom both lurnped(macro)or distributed (nricro) variable mathematical models. lumpedvorioble (ol system thot involves coupled linitecomponents ond (b) distributed vcrioble system lhot involves o confinuum.I Two lpes of syslems thot ccn be modeledusinglineorolgebrclcequotions.la mightbe For the reflectiveof the flow ratesof mass between reactors.Lrrmpedvoriable probLems involve coupled . PT3.. response dependent the quantityofchernical in theseotherreactors. of Using Fig. reactor4 receives chemicalinputsfiom reactors and3. its is on When thesedependencies expressed are rnathematically. instance.l a).t90 PART LINEAR 3 SYSTEMS Feed @t FIGURE PT3. The systempropertiesare the reactioncharacteristics of the chernicaland the reactors'sizesand flowrates. The equations coupledbecause individualpartsofthe systerrale influencedby other are the parts.suchasthe feedrate.r2 in rnightquantifytheamount in the second. 2 Consequently. a's for Fig.For example.ln as an example.in Fig. (PT3. The. quantitybeingrnodeled reactors the is the massof the chemicalin eachreactor.t's areusuallyrneasures the magniof tudesof the responses the individualcomponents. "r1 might quantifythe amountof chernical rnass thefirst reactor.lc. When we studiecl rootsof equations. The a's typically represent properties the andcharacteristics that bearon the interactions betweencomponents. PT3. For this case.
rapter the also describeshow man'icesare handledin MATLAB.reactors.the concentration at the middle of the reactor in Fig. Diffbrential equations derivedfiom conservation laws specify the distribution of the dependentvariable fbr such systems.and electric circuits. Chapter10 illustrates Such solutiontechniques valuable1br caseswhere many righthandside are vectorsneed The chaoterends witlr a brief outline of how MATLAB solveslinear to be evaluated.Aside from coveling the rudimentsof matrix representation anclrnanipulation. PT3.The chapter endswith a brief description eflicientmethods of fbr solving tridiagonal ntatrrces. o Aside fiom phvsicalsystems.Before discussion this technique. 8 provides a brief overviewof tnotrir algebru.2 PART ORGANIZATION t9r finite components. starl with We it this "strippeddown"versionbecause allows the fundarnental technique be elaborated to potential probon without complicating Then.lb) is an exantple a continuous rectangular of variablemodel.in subsequent sections. The three jumpersat the beginning Chap. The solutionof suchsetsof equations leplesents major applicationareatbr the metha cldsin the fbllowing chapters. TheseeqLrations coupledbecause variables one locaare the at tion are dependent the variables acljoining on in regions. and discussed this in The techniques part can be used to solve fbr the unknownswhen the equationsare linear and algebraic. Theseresultwhen mathematical functions are required satisfyseveral sirnultaneously. The distribution chemicals of alongthe lengthof an elonqated. After this prelirr. s\/stet)ls.8 area lumpedsystem. Chap. . cl. m o n r e n l u m .orpdrlial pit'oting. we lems of the naive approirch presenta number of mocliflcations mjnirlize and cirto and cunlventtheseproblenrs.rinary material. Somervidely usednunrerical techniques that employ simultaneous equations legresare sion analysis and splineintel'polation. discuss details.distributed variable problems attempt to describethe spatial detail of systerns a continuous semicontinuous on or basis.3.2 PARTORGANIZATION Due to its importancein fbrmulating and solving linear alsebraicequations. how Gauss eliminationcanbe lbrmulatedas an LI) factoriZation.11."naive" Gausseliminationis discussed. The fbcus of this discussion will be the process switching of rows.is a function of the concentration adjoining in regions. bungee of Conversely.These differentiai equationscan be solved numericallyby convertingthem to an equivalentsystemof simultaneous algebraic equations. of a pleliminary sectiondealswith simple methodsfor solving small systerns. 3.Similar examplescould be developedfor the spatialdistributionof temperature. PT3. reactor(Fig.r e l e c t r i c i t l .Examples include trusses. condition Each to conditions results an equation in that contains known coefflcients uuknownvariables. These approaches presented provideyou witlr visualinsightandbecause oftlre rrrethodsare one to the elimination unknownsrepresents basis Gauss of lbr the elimination.For example. simultiineous linearaluebraic equations also arisein a varietyof mathernatical problemcontexts. Chttpter 9 is devotedto the most fundamentaltechniquefor solving linear algebraic systems: launching into a detailed GausseLimittolion.
The chapterendswith a brief description how nonlinearsimultaneous of equations canbe solved. Seidel method. Chapter I2 dealswith iterative solutiontechniques. although a description is provided of an alternative approach.which has tremendous utility in analyzingstimulusrelationships physicalsystems.The conditionnumberis introducedas a measure of the roundoff errorsthat can result when solving illconditionedmatrices. which are similar in spirit to the for in approximate methods rootsof equations discussed Chap. the Jacobi method.r92 PART LINEAR 3 SYSTEMS Chapter 11 startswith a descriptionof how LU factorizationcan be employedto efficiently calculatethe mati. they involve guessThe emphasis on the Gaussis ing a solutionand then iteratingto obtaina refinedestimate. response of The remainderof the chapteris devotedto the importantconceptof matrix condition. . That is.6.r inverse.
\.nrl effiuluso the [e of o the UESSAUSS . Knowing how to represent systemof linear algebraicequations matrix form.x ) . symmetric.tierrhel are reJeased.2.triangular. ' . as measured 9/ grar po:itions.. Suppose that you are askedto computethe displacernent eachof thejumpers.1 1 ).r: * tntS .and .t'2. .I.f t 1 r 1 Q . U n d e r s t a n d i n ga t r i xn o t a t i o n . a steadystate can Using Newton'ssecond : m 1 gI k z ( x z.If we of as assumethat eachcord behaves a linear spring and follows Hooke's law. in \OU'\E GO\ A.. it1 takeshold and thejumpers s ill evenunstretched positions tually cometo the equilibriunr shownin Fig. m Being able to identify the following typesof matrices:identity.PROBLEN\ by upposerhat three jumpers are connected bungeecords. . freebodydiagramscan be developed eachjumper as depicted Fig.and tridiagonal.irz) : 0 .ing held in placeverticerlly that eachcord is fully extended unstretched.ln shows them It We but so U.t'1. for in forcebalance be writtenforeachjumper: law.k: (.diagonal.Figure 8.Specificobjectivesand topics coveredare e . \ dovnv'ard from eachof their .l. 8. Knowing how to perlbrm matrix multiplication and being able to assess when it is feasible. a in Knowing how to solve linear algebraicequations with left division and matrix inversion MNLAII. r r ) : 0 t1tlg kr(..k z ( x z. .r: . crn define threedistances. t l" Lineor Algebroic Eqrotions ond Motrices Tcobi tions CHAPTER OBJECTIVES The primary objectiveof this chapteris to acquaintyou with linear algebraicequations and their relationshipto matricesand matrix algebra. 8.1b.
x) tl ill m$ FIGURE 8. .(x2.:Ol?r u n 1 ffi .81 ( k 1l k ) x 1 kzxz :mtg (8.k:rr : mzg ktxzlk3xl7n19 Thus.nl ) ffi il z'^r li: n ffi ?r t:tl l _ Hr ) ffi I'l ffi Arxr t2(r. A.tr * (kz I kz)xz.ri : the for displacernent jumper I measured of downwardliom the equiiibrium position (m).I MATRIXALGEBRA OVERVIEW Knowledgeof matricesis essential understanding solutionof linear algebraicequafor the tions.1) ftz. andg = gravitational acceleration nr/sr). The tbllowing sections outline how matricesprovide a conciseway to represent and m a n i n u l a l e n e r ra l q e h r a i c li . Because have useda linear law for the cords. 8.: Iri i ."r2) tr (a) Unstretched lr) 11 ffi ( b ) Stretched tt nrrg k.11) tr n ?r i I tl I m tg t. wherenr. Chapters8 through l2 will introduceyou to how MATLAB is used to solve such systemsof equations.2 Freebody diogroms. : the springconstant cordT(N/m). : the massof jumper r (kg).(.r94 LINEAR ATGEBRAIC EQUATIONS AND MATRICES ".these we are equations linear algebraicequations. the problem reducesto solving a systelnof three simultaneous equationsfor the threeunknown displacements.r1 . Collecting termsgives 19.I Threeindividuo conneciedbv bunqeecords s FIGURE 8.
L: (8. the ond subscriptT The matrix in Fig.3 has m rows and n columns and is said to have a dimensionof m by n (or m x n).The the first subscript alwaysdesignates numberof the row in which the elementlies.2) . azu azz . Matrices with row dimensionm : l.' FIGURE 8.For example. 8. will use caseto distinguishbetweenvectors(lowercase) and matrices we (uppercase)..thereare occasions this is One way to accomplish to distinguisha column matrix from other typesof matrices. As an notationfor the matrix andai 1 designates indidepictedin Fig. the secondsubscriptis dropped. the first subscriptof each element is to dropped.1 Mqfrix Nototion by A matrix consistsof a rectangulararray of elementsrepresented a single symbol. Note that for simplicity.:&it:''.MATRIX ALGEBRA OVERVIEW I I ll I I 3 Column a:t azt at2 I at3 I iiil Qt. it shouldbe mentionedthat thereare times when it is desirable employ a specialshorthandnotation to distinguisha row matrix from other types of matrices. in fbl .n3 a.r. to employ specialbrackets.3. in {c}.Also. [A] is the shorthand vidual element of the matrix. suchas lnl j tL  are ref'erredto as column vectors.As with the notation when it is desirable employ a specialshorthand to row vector.1. as I In addition to specialbrackets.3 A moirix. For simplicity.One as brackets. It is referredto as an m by n matrix.elementa23is in row 2 and column 3.t.l way to accomplishthis is to employ specialopentopped Matrices with column dimensionn : l. 8. suchas [b]: [bl b2 brl are called row yectors.The secI designates column. A horizontalset of elementsis called a row and a vertical set is called a column. Row2 t^t  onl o.) a. 8.
. Squarematricesare particularly important when solving setsof simultaneous linear equations. as in rl12 o. [A]Ul: [/][A]: tAl An upper triangular malrx is one whereall the elements below the main diagonal are zero. An identi4' matrix is a diagonalmatrix where all elementson the main diagonalare equalto 1.. For example.. For example. tttl n ate called .square matrices. as in fo.r96 LINEAR ALGEBRAIC EAUATIONS AND MATRICES Matrices where rn : lr.I L.".rl ott ctstJ [.: . square matricesof coefTicients encountered are when dealinewith suchsystems There are a number of specialforms of squarematricesthat are important and should be noted: A symmetricmatrix is one wherethe rows equalthe columnsthat is. : aii for all i i's and. a 3 x 3 matrix is atrl I A l : I a r .4]: [''' 422 I ..and a.1 The diagonalconsistingof the elementsas. The identity matrix hasproperties similar to unity. a.l's.. I lAl=l a) )  orr) L Note that wherelarge blocks of elementsate zero. r r .) .For such systems. a ) . c A diagonal nmtrix is a square matrix where all elements off the main diagonal are equal to zero. as in tAl:l [rl I LlJ I The symbol Ul is usedto denotethe identity matrix. a22.the number of equations(corresponding rows) and the to numberof unknowns(corresponding columns)must be equalfor a uniquesolutionto be to possible'Consequently.j3is termed theprincipal or maitt diagonal of the matrix.they are left blank. That is.. [st2f tAl:ll3 7l L2 7 8_l i s a 3 x 3 s y n r m e t r in r a t r i x .
I 8.. Zt a I I  at) a:z .. .2 Motrix Operoting Rules Now that we have specifiedwhat we meanby a matrix. ...h e s u b t r a c t i o n t w o m a t r i c e s ..2..... I I on. is accomplished addingcorresponding of terms in eachmatrix. I aat aqc ) lI I I j I I I I The precedingmatrix has a bandwidthof 3 and is given a specialnamethe tridiagonal matri. [A] : [B] if ai1 : bi1 for all i andj. [Al and [B.I MATRIX OVERVIEW 197 A lower triangular malrrx is one whereall elements abovethe main diagonalarezero. . ... by Addition of two matrices. . is obtainedby subtracting terms.Two nr by n natrices are equalif.for a 3 x 3 matrix: I D I: s l A l: l i o l fPo.r (/tl L Or..I efltt ... 2 . The elements the resultingmatrix [C] are computedas c'i1: ct.2. S i m i l a r l yt.ALGEBRA 8.a y .. 2 .r..1. m a n dj : 1 . are Both addition and subtraction commutative. n .. . . and only if..n. . .. tAl :  i o11 1.ltfollowsdirectlyfromtheprecedingdefinitions that addition and subtractioncan be performedonly betweenmatriceshaving the same dimensions. i o r .mand j:l.as in [E] dij:eij fij I i I i I forl: l.. we can definesomeoperatingrules that govern its use. For example.n. t A l + t B l: [ B ] +[ A ] and associative: ( t A l +t B l ) [ c ] : [ A l + ( t B ] t c l ) + + The multiplication of a matrix [A] by a scalarg is obtainedby multiplying every element of [Alby S. . of s corresponding ninus [F]. every elementin the first is equal to every elementin the secondthat is..i1b.f i o .i f o r i : 1 . . as ln I f o.say.] "'.with the exceptionof a band centered on the main diagonal: f al ln l: t . i I i I I i A bandedntotix has all elernents equalto zero.. dt. Rart Pat.
. F. That is. if [B] were an m by i matrix.) Interiordimensions are equal. However. m [sxs1x722 II Exteriordimensionsdefine thedimensions f the result o FIGURE 8. [B] could be an n by / matrix.matrix rnultiolicationis associative: of (tA JtB[c] : tA l (tB l tcl ) l) 'aLnd distributive: t A l ( t B l +[ c ] ) : [ A l [ B ] +t A l t c l or ( [ A ] + t B l ) l c l : [ A ] [ c ]+ l B l l c l However.multiplicationis not generallycommutative: tAttBl+ lBltAl Thot ic rh. According to this definition. if [A] is an m by n matrix.3) where n : the column dimensionof [A] and the row dimensionof [B].Figure 8. by theTthcolumn of the secondmatrix [B].' order nf mrrltinlic:rtion is . For this case. Figure 8. elenent is obtainedby addingthe productof individual elenents from the lth row of thefir$ matrix.oislt11 t:l {8.'I rl > { 1i : [A'l^"n [Blnt = [C]. the c. s The productof two matricesis represented [C] : tAltBl.the resulting[C] matrix would have the dimensionof m by /.4 depicts how the rows and columnsline up in matrix multiplication. Thus. matrix multiplication can be perfbrmedonly if the first matrix has as many columnsas the nunber of rows in the secondmatrix.r98 LINEAR ALGEBRAIC EAUATIONS AND MATRICES ql t. multiplication is possible [::]Ll motrix ultiplicoiion.4 Visuol depictionhowtherows columns upin of line ond FIGURE 8. in this case[A]."t .5 Motrix multlplicotionbe per{ormed if con only l h e i n n e rd i m e n s i o no r e e q u o l . If the dimensions the matricesare suitable. wherethe elements [C] as of are dellned as + ) . the multiplication could not be performed.5 provides an easy way to check whethertwo matrices can be multiplied.
That is. s i g n a t elde 1 ri.. cailed the inver.the transpose numerousmathematical has applications. thereis anothermatrix [Al r...s d e l i n e d s de a . are ChapterI I will deal rvith techniques using numericalmethodsand the computerto calculatethe inverse lbr lbr suchsystems. the transpose equalto the zu element the original is of 7.J t h e t n r n s p o s e . However. J lc1 c2 c:l tnen {c}r : In addition. Two othermatrix manipulations thirtwill have Lrtility our discussion the transin are poseand the augmentation a matrix. nultiplicationof a matrix by its inverse l e a d s o t h ei d e n t i t y a t r i x .. ...if a matrix [A] is squareand nonsingular.. fbr which of tAltAl ' : 1 J e ]: 1 1 1 [A] Thus. 0t) a. . .l the rr..if J . l. has a variety of functionsin matrix algebra. d1 l ool 0l0l 0 0 r_j ott I LaSt a2t a3t 021 {41 . i .l In otherwords. The trartslto.the multiplication a matrixby theinverse analogous division. ' u .I [.'I MATRIX ALGEBRA OVERVIEW Althoughmultiplication possible.suppose have a 3 x 3 matrix of coefficients. element of ['' l t A l r: I . ".For example. 't2 r. u . .I Lr. For examole. ( t ) .lr.. t m The inverseof a 2 x 2 matrix can be represented simply by lAl ': ail422 anq2l I o" n'tl itrrJ Lat tbr Similarfbrmulas higherdimensional matrices muchmoreinvolved..t a.r I L. The final matrixmanipulation that will haveutility in our discussion uuqmenratiott.One simple advantage The transpose is that it allows a column vector to be written as a row and vice versa. ' (t.se [Aj. is matrix divisionis not a definedoperation... r . we We might wish to augmentthis matrix [A] with a 3 x 3 identity matrix to yield a 3by6dimensional matrix: a1t [. I ...se a matrix involvestransformingits of of rows into columnsand its columnsinto rows. t l. .in the sense of is to that a numberdividedby itself is equalto l. . .t.. is A tnartrix augrnented the additionof a column (or colunurs)to the original matrix. For is by example. matl'lx.for the 3 x 3 ntatrix: l A l : l .. .B. .l f u.
>> Y = t 5 8 1l.1 M A T L A BM o t r i x M o n i o u l o t i o n s Problem Stotement. 1 5 6 i . y. It is bestapproached a handson lationsareimplemented as exercise onthe computer.i6 27 3 Next we will createanother3 x 3 matrix on a row basis.t4 B 2l. S o l u t i o n . Thus.rthe matrix >> p B = lx.First createthreerow vectors: >> x _ 1B 6 gJ. Then we can combinetheseto tbrn. C r e a t e a 3x 3 m a t r i x : >> A = [1 5 b._t 61] .200 LINEAR EAUATIONS AND MATRICES ALGEBRAIC has utility when we must perform a set of identicaloperations the on Such an expression on matrix rows of two matrices.4 3 6 2 6 The transpose [A] can be obtainedusing the ' operator: of >> A'. we can perlbrm the operations the singleaugmented ratherthan on the two individual matrices. EXAMPTE 8. z) 5 4B B 9 1 2 We can add [A] and [Bl together: >>C=A+B c= 9 212 1 1l 14 l5 3 9 . >> z . The following exampleillustrateshow a variety of matrixmanipuwith MATLAB.7 4 2.
*B B 35 12 30 32 48 54 2 14 A2 x 3 matrix can be set up >> D = t1 4 3. However.8462 0. matrix multiplication works >> D*A 20 58 ro 53 35 53 The matrix inverse can be computed with the inv function: >> lT l\1 = fnv(Al 4.2154 0.MATRIX ALGEBRA OVERVIEW Further.3846 4.5 B 1l.4769 .3231 0.2462 a.'?! LTta' usrnq Inner matrix dimensions must agree. an error message will occur >> A*D . If [A] is multiplied times [D].[A] and [B] can be multiplied are >> A*B 7 44 26 94 9A 86 26 7r 1 Note that [A] and [B] can also be multiplied on an elementbyelement basis by including a period with the multiplication operator as in >> A.6L54 0.01s4 0.we can subtract[B] from [C] to anive back at [A]: >>C=CB 1 'l 5 4 6 2 3 6 7 Because their inner dimensions equal.8308 0. if we reverse the order of multiplication so that the inner dimensions match.
0000 0.4) : * a32.I .0000 0.0000 0.rr000 1.3 RepresenlingLineor Algebroic Equotionsin Motrix Form It shoulclbe clear that r.5) .'ide concisenotation tbr representing a simultaneous linearequations.t2 f 0t1.the inversecan be multiplied by the originalmatrixto give the identityrratrix: >> a rl:.natrices pror. AXAI  1.202 LINEAR ALGEBRAIC EAUATIONS AND MATRICES To testthat this is the conect result.0000 0. For a drtrr * a12. (8.0000 0.0(r00 1.V : t t 2 1 x 1* 4:rrr a 2 2 \ 2 1 o 2 1 .Y2 {13113 I can be expressecl as [A]{r} : {b} (8.0000 The eye functioncan be usedto generate identitymatrix: an 22 I= f = e)re(3) 0 0t) 0 1 Finally.0i100 0. 3 x 3 setof linearequations. example. r 3: bl b.matrices be augmented can sirnplyas in >> Aug Lrrn = tA Il t4: 361 010 Note that the climensionsof a matrix can be determined bv tlre size firnction: 3 8. fi.
4)and (8. matrix inverseitself in the numericalalgorithms. "leftdivision. (8.6) example how theinverse of Therefbre.3)l to convinceyourselfthat Also.5)lbr {x}.:: o.t I I a): arrJ r Lrlr {D} is the columnvectorof constants: lblr : lb1 bz bt) and {. typical exampleis leastsquares with less with n coefficients fitto m datapoints(x.5)is a valid matrixmultiplicaEqs.r}is the columnvectorof unknowns: {r}r : [. hasgreatvaluein the engineering analyses suchsystems.Conversely.1. of (columns). other approaches employedin are very efficient way to solve a systemof equations.l l o .2 SOLVING where [A] is the matrix of coefficients: ttrl tdl  otl . equation beensolvedfbr {r}. t a:2 . (8. n of the second This part of the book is devoted solvingEq.?? n. mosl efficientway is to employthe backslash.lt shouldbe notedthatthis is not a Thus.b} : '[A] equals identitymatrix." or operator in as >>r=A\b T h e s e c o n ds t o u s em a t r i xi n r e r s i o n : i >> x = inv(. typical example of is oDtimization.r1 x2 r:] Recall the definition of matrix rnultiplication tEq. However.. is systems an equation < A equationsthan unknowns.q)*b ..EQUATIONS WITHMATLAB LINEAR ALGEBRAIC 8.2 SOTVINGTINEAR MATLAB provides The two directwaysto solvesystems linearalgebraic of equations.as discussed Section11.the equation the becomes Because [A] tr) : tAl r{b} (8. are said to be nndertletermined. (8.y).. realizethat Eq. underdetermined svstems numerical WITH MATTAB ATGEBRAIC EQUATIONS 8. A formal way to obtaina to solution using matrix algebrais to multiply eachside of the equationby the inverseof [A] to yield tAl rtAl{r}tAl1i.(8...2. are said to be overdetermined..5)areequivalent. (rows)thanunknowns with moreequations It shouldbe notedthatsystems regression where A m > n. tion because numberof columnsn of the first matrix [A] is equalto the numberof rows the matrix {r}. This is another the has playsa role in matrix algebra thatis similarto division.
184.8]J {::::. 0 50 50 586. (8.9170 71. UseMATLAB to solvethebungeejumperproblemdescribedatthe The parameters the problem are for beginningof this chapter.8000 Employing left division yields >> x K\m9 4r.204 LINEAR ALGEBRAIC EQUATIONS AND MATRICES As statedat the end of Section8.5130 Alternatively. EXAMPLE . I L o Start up MATLAB and enterthe coefficientmatrix and the righrhandsidevector: >> K = t150 v 100 0.2 8 S o l v i n gt h e B u n g e eJ u m p e rP r o b l e m i t h M A T L A B w ProblemStotement.3.the matrix inversesolutionis lessefficient thanusing the backslash.7.s sol ['.81 588.0 50 501 150 100 rJ >> mq  100 150 50 [588.7000 784. Substituting parameter valuesinto Eq. Both optionsare illustratedin the following example.6.6000 586.2020 55.1. t o r g i v e s t h e s a m er e s u l t : .1)gives these i:B : [l. multiplying the inverse of the coeflicient matrix by the righthandsidevec. Jumper Top(1) ( Middie 2) (3) Bottom Moss (kg) Spring Constont (N/m) Unstretched Cord Length (m) 6A 7A BO 50 100 50 2A 2A 2A Solution.:so .t}Izs+.l {. 100 150 50.
8.3 CASE STUDY
>> x = inv(K)*mg
4r.2A2A 55.9170 7I.613A Because jumpers were connected 20m cords,their initial the by positionsrelative to the platform is >> xl = t20;40;501;
Thus, their final positionscan be calculated as >> xf = x+xi
6I.2A2a 95.9rtA 131.6130 The results, which are displayed in Fig. 8.6, make sense. The first cord is extendedthe longest becauseit has a lower spring constant and is subject to the most weight (all three jumpers). Notice that the secondand third cords are extended aboutthe sameamount.Because is subjectto the weight of two it jumpers,one might expectthe second cord to be extended Ionger than the third. However,because is stiffer (i.e., it has a higher it spring constant),it stretchesless than expectedbased on the weight it carries.
(a) (bl
FIGURE 8.6 Positions of three
i n d i v i d u o lo n n e c i e d cs by bungee cords. (o)Unsiretched ond (b)stretched
CURRENTS AND VOLTAGES CIRCUITS IN
Bockground. Recallthat in Chap. 1 (Table 1.1),we summarized somemodelsand associatedconservationlaws that figure prominently in engineering. in Fig. 8.7, each As model represents systemof interactingelements. a Consequently, steadystate balances derived liom the conservation laws yield systems simultaneous of In equations. many cases, such systems linear and hencecan be expressed matrix form. The presentcasestudy are in focuseson one such application:circuit analysis. A common problem in electrical engineering involves determining the currents and voltagesat various locationsin resistorcircuits. Theseproblemsare solved using Kirclr(orpoint) rulestalesthatthealgebraicsumof hoff'scurrentandvoltagerules.Thecurrent (Fig. 8.812), all currentsenteringa node must be zero or
fi:o
(8.7)
where all current enteringthe node is considered positive in sign. The current rule is an applicationof the principle of conservation charge(recallTable 1.1). of
206
ALGEBRAIC EQUATIONS AND MATRICES LINEAR
*_.r*_
il
( a ) C h e m i c ae n g i n e e r i n g l
( b ) C i v i le n g i n e e r i n g
f,,
(a)
ffi,ryl M
Circuit (c) Electrical engineering
+AA]
R,
WI
(d) Mechanical engineering
Srhamntir
+
i,j
(b)
FIGURE 8.8 FIGURE 8.7
which, ot steodyslote,con be modeledwilh lineorolgebroic Engineering syslems equolrons.
ronro<anlntinnq
rule of (o)Kirchhoff's current o o n d ( b ) C h m ' ls w .
The voltage (or loop) rule specifiesthat the algebraic sum of the potential differences (that is, voltage changes)in any loop must equal zero. For a resistor circuit, thisis as expressed
fcI^:o
(8.s)
of where f is the emf (electromotive force) of the voltage sources,and R is the resistance any resistors on the loop. Note that the secondterm derives from Ohm's law (Fig. 8.8b), which statesthat the voltage drop across an ideal resistor is equal to the product of the cunent and the resistance.Kirchhoff's voltage rule is an expressionof the consentationol enerSy. Application of theserules results in systemsof simultaneouslinear algebraic Solution. conequationsbecausethe various loops within a circuit are interconnected.For example, sider the circuit shown in Fig. 8.9. The currents associatedwith this circuit are unknown
one no This presents greatdifficultybecause simply and bothin magnitude direction.
assumesa direction for each current. If the resultant solution from Kirchhoff's lawsis negative,then the assumeddirection was incorrect.For example,Fig. 8.10 showssone currents. assumed
8.3 CASE STUDY
continued
ili
,il
'll
I
!l
R:5O Vt:200Y
a n =i s o
7o:0V R:20f)
A resistor lo be solved circuit using simultoneous
i;n"^' ^l^.h.^;. an, rnf innc
FIGURE 8.IO
Assumed cLruenr direction5
Given these assumptions, Kirchhoff's current rule is applied at each node to yield in*isz*jrr:0 ios* isz ls+ :0 i a 3 1 2 : i5aiaj:Q Application of the voltage rule to each of the two loops gives iosiszls+:0 is,rRs+ i+tR+: i12Ri2* l52R52 Q * : iosRos  iszRsr ieRe  200 : 0 I or, substituting the resistances from Fig. 8.9 and bringing constants to the righthand side, *151s+ 5ia3 10i32 10i52 Q : * 20ios10is:f5ip:200 Therefore, the problem amounts to solving six equations with six unknown currents. These Q
equationscan be expressedin matrix form as
[r
l0
l
l
r
0
I
l0 lo
t0
0 o
l0
t0
1 o o
0
l
0
0l
l12 r52 t3z .oJ
r)4
0 o
0
15
0 I
Ls ro
_20 o
r 1 I  sl ol
0 
iqz
:{,L
208
ALGEBRAIC EQUATIONS AND MATRICES LINEAR
continued
Although impractical to solve by hand, this system is easily handled by The solution is >>A=t111000 0 1 0 1 *1 0 0 0 1 0 0 1 0 0 0 0 1 1 0 10 10 0 15 *5 5 *10 A 20 0 ol; >> b=t0 0 0 0 a 2001,; >> current=A\b
= current 6.1538 4.6L54 1.5385 6.1538 *1.5385 1.5385 Thus, with proper interpretation of the signs of the result, the circuit curents and ages are as shown in Fig. 8. 1L The advantages of using MATLAB for problems of this should be evident.
8.1 ci
comm€ the oril E.2 At
tAl
rcI
LE tFl
Answe (a) WI (b) Ide (c) Wl
J t2'
(d) Pe (l
FIGURE I 8.I obtoined MATLAB using The solution currents voltoges for ond
V: 169.23
(4
PROBLEMS
PROBTEMS
Given square a matrix [A], writc a single line MATLAB thatwill createa new matrix [Ang] that consists of original matrix[AJ augnrented an identity matrix by [11. A number mafices are definedas of
(7) [Bl x [Al ( 1 0 ) 1 ]x t 8 l [
(8) [D]7 r n 1 [ E l rx [ E l
(e) tAl x { C} ( t 2 ){ C } rx { C }
8.3 Writethefollowing of equations rnatrix set in forrn: 50:5,r:7.r: 4rz+7.\*30:o : rr  7.r.r ^10, 3xzf 5;:r UseMATLAB to solve theunknorvns. addition, rt lbr In use to compute transpose the inverse thecoefficient the and of matrix. 8.4 Three matrices defined are as
L2 0 4J
lr _ (\t, 
[137f lr 2 7l
, il pt:l: i ,'l
[r s 8l
L4 0 6_l
Ll LG):17 6 4J
t rrl=17 rl
r r r = f ro .1  l 7 11
, fi ,",: l'],",=l],.,:;,'l [,i, f
(a) Perfbrm all possible nrultiplicationslhat can be com_ puted betweenpairs of thesematrices. (b) Justify why the remainirigpairs cannot be rnultiplied. (c) Use the resultsof (a) to illustratewhy the order of multiplication is important. 8.5 Five reactorslinked by pipes are shown in Fie. pg.5. The rate of massflow througheachpipe is computed as the prodr"rct flow (Q) and concentration of {r.). At steadystate, the mass llow irrto and out of each reactor must be equal.
thefbllowingquestions regardingthcse matrices: What the dimensions the matrices,l are of Identify square, the coluntn, and row matrices. What the values of the clcments.. are ar,. b.1, dzt, ezz, /r:,8r:? Perform following operations: the
(r)trt + tBl (1) x [B] 7
(2)IAI+ lFl 1s1 x tBl [A]
(3)tBl _ tEl (6)(C],
FIGURE P8.5
2ro
AND MATRICES EAUATIONS TINEAR ALGEBRAIC
For example, for the first reactor, a mass brilance can be written as 0 o r r : ofr Q t t ' t : Q r s c tI Q n c t
+"x?
I
Write massbalancesfor the remainingreactorsin Fig. P8.5 and expressthe equationsin matrix fbrm. Then use MATLAB in to solve for the concentrations eachreactor. is 8.6 An importantproblem in sffucturalengineering that of truss(Fig. P8.6). finding the forcesin a staticallydeterminate as This type of structure can be described a systemof coupled The linear algcbraicequationsderived from force balances. sum of the forces in both horizontal and vertical directions must be zero at each node, becausethe system is at rest. Therefore,for node 1: Lf,:0:Fr \ n'' : 0 : cos]0 Fjcos60 *F1.71 Fr sin 10 F.rsin 60 * Fr ,
P8.7 TIGURE
algebraicequationsin matrix form and then use MATLAB to solve fbr the unknowns. 8.7 Considerthe threemassfourspringsystemin Fig.P8.?, Determining the equationsof motion from XF' : ma,for eachmassusing its freebodydiagramresultsin thefollow' ing differentialequations:
/'
8.10 Tl identica the bott 2.5kg,t displac 8.11 Pe the circ 8.12 Pi the circ 8.13 D two mal lZl is n multipli the prol 8.14 D the tran plemen
for node 2: )/nr :0: F : I F 1c o s 3 0 I F z nI H z
rr*lrlrrllr::0
\ rzr /
/kr+ k"\
/kr\ \mt/ /l'\
/ \m2 l
5' n 'u : 0 : /
for node 3: \/r'n
F rs i n 3 01 F 2 . , V : I
/,t'\ /k,*t,\ tr (31'' (
\m:/ \ ,t:
;,
/k.\ /k,'" \ ( a ) * r +{ ^ r r ^ 4 } x , : o
\.n.r/ \ m3 /
: r   0 :"
F,  F.rcos60l Fr.r,
: D a u o : F : s i n 6 o ' *F : , ,r v :
where F,.,,is the externalhorizontal force applied to node I (wherea positiveforce is from left to right) and {.,. is the external vertical force appliedto node i (where a positiveforce is upward). Thus, in this problem, the 1000lb downward to fbrce on node I corresponds F,.,:  1000.For this casc, all other F,.,'s and {,,,'s are zero. Expressthis set of linear
andmr=mt= w h e r e f t k t : 1 0 N / m , k . : f t r : 3 0 N / m , , mz: I kg. The three equationscan be written in matrixform: vector} Q : {Acceleration vectorx } + lk I m matrixl{displacement At a specifictime where:r, : 0.05 m, r: : 0.04 m, andl,= 0.03 m, this forms a tridiagonal matrix. Use MATLABto of solve for the acceleration eachmass. 8.8 Solve
[ 3 + 2 i + ' lJ : r [ _ l I 2 + , I i I I i r . l[ z : f
8.9 Perform the same computationas in Example8.2,but with the following characteristics use five parachutists
Moss Jumper I 2 3 4 5 55 75 60 75 90
(ks)
Spring Conslont (N/m) BO 50 70 r00 20
urltr"*ffi
Cord tength (m) t0 10 t0 t0
r0
P FIGURE 8.6
PROBLEMS
Three masses suspended are vertically by a seriesof springs wheremass I is at the top and mass3 is at bottom.g : 9.81 m/s', ffir : 2 kg, m2: 3 kg, 2, : lf kg, theft's: l0 kg/s2,useMATLAB to solve for the and ntsr Perform samecomputationas in Sec. 8.3, but for the circuit Fig. P8.I l. in Perform samecomputationas in Sec. 8.3, but for the circuit Fig.P8.12. in Develop, debug,and test your own Mfile to multiply matricesthat [X] : lYl[Zl, where lY] is m by n and is, isn by p. Employ f or. . . end loops to implement the ion andinclude error trapsto flag bad cases. Test program usingthe matrices from Prob. 8.4 Develop, debug,and test your own Mfile to generate tmnsposea matrix. Employ f or. . . end loops to imof thetranspose. it on the matricesfiom Prob. 8.4. Test
2ll
R:20c)
R:10O Vr : 150volts
ti
R:2O R=5O Vo: 0 volts
FIGURE 8.I I P
R=30O
,
R=35O
1
Vr : 10 volts
R:8f) Vo= 150volts
F I G U RP 8 . I 2 E
. ...
GoussEliminotion
CHAPTER OBJECTIVES
The primary objectiveof this chapteris to describethe Gausseliminationalgorithm fbr solving linear algebraicequations. Specificobjectivesand topics coveredare ' . ' . . ' with the graphicalmethod Knowing how to solve small setsof linear equations and Cramer'srule. as how to implementfbrward eliminationand back substitution in Understanding Gauss elimination. how to count flops to evaluatethe efficiency of an algorithm. Understanding of and illcondition. Understanding concepts singularity the how partial pivoting is implemented and how it differs from Understanding pivoting. complete Recognizinghow the bandedstructureof a tridiagonalsystemcan be exploited to obtain extremelvef1icientsolutions.
t the end of Chap. 8, we statedthat MATLAB provides two simple and direct leftdivision, fbr of equations: methods solvingsystems linearalgebraic
>>
x
=
A\D
and matrix inversion,
>> x = inv (A) *b
ThismaChapters9 and l0 provide backgroundon how such solutionsare obtained. In terial is includedto provide insight into how MATLAB operates. addition,it is intended to show how you can build your own solution algorithmsin computationalenvironments that do not haveMATLAB's builtincapabilities.
9.I SOLVING NUMBERS EQUATIONS SMALL OF
2r3
The technique in is it described thischapter cllleclGauss elimination because involves combiningequafions climinateunknowns. to Althoughit is clne the eirrliest of rnethods fbr solving simultaneous equctions, remainsarnongthe most importantalgorithnrs use it in today and is the basisfur linear equationsolving orl many popular sotiwarepackagesinclucling MALAB.
il
9 . 1 SOIVING StylAtt NUMBERS EQUATIONS OF
Betbreproceediug Gausselinrination, will describe to we several methods that are appropriate fbr solving small (n < 3) setsof simultaneous equationsand that do not recluirea computer. Theseare the graphicalmethocl, Cramer'srule. ancl eliminationof unknowns. the
9.1.1 fhe GrophicolMethod
A graphicalsolutionis obtainable two linearequations plottingthen on Cartesian for by with one axis colresponding r1and the othel to r2.Because equations coordinates to the will plot as a straight arelinear.eachequation line. For example, suppose we havethe that tbllowing equations: 3rr 2.rr: *
,. L l,^ 
l8
)
r, If vee.\uncrhet.i>thezb:cil:ta,{eaan.>olrccachttlthtseteuilUoilsrorrt!
rl:.tl+9 tt: I f I trl
3
linesthat is..t2 : (slope)rr * interThe equations now in the forrn of straight are of cept. When theseequationsare graphed,the valuesof r1 and x2 at the intersection the lines represent solution (Fig. 9. l ). For this case,the solutionis ,r1 : 4 andxz : 3 . the would be represented a planein a by For threesimultaneous equations. eachequation intersect would repThe pointwherethe threeplanes threedimensional coordinate system. resentthe solution. Beyond three equations,graphical methodsbreak down and. consequently,have little practical value fbr solving simultaneous equations. However,they are properties the solutions. of usefulin visualizing when solvingsetsof For example. threecases thatcan poseproblems Fig. 9.2 depicts represent parallellines. linearequations. Fig. 9.2a showsthe casewherethe two equations For suchsituations, thereis no solutionbecause lines nevercross.Figure9.2b depicts the For thereis an inllnite number the casewhele the two linesarecoincident. suchsituations Both typesof systems saidto be singular. are of sollrtions. In addition,systerns that are very closeto being singular(Fig. 9.2c) can also cause problems.Thesesysterns said tobe illconditirnecl.Graphically, this conesponds the to are Illconditioned fact thatit is diiTicult identifythe exactpoint at which the linesintelsect. to solution systems will alsoposeproblenrs when they areencountered duringthe numerical sensitive roundofTerror. to This is because they will be extremely of linearequations.
2r4
ELIMINATION GAUSS
6rt
9.I FIGURE ofthe The equotions. intersection lineor of of solutlon o set Mo simultoneous olgebroic Grcphicol
the linesrepresenls solution.
EX
9.2 FIGURE (o) ond solutions, systems:nosoluiion, lnflnlte ond of depictionsinqulor ilfconditioned {b) Grophicol visuolly to isdifficuh detecl thol oI ore (c)ilicondiiioned the system wtrere slopes soclose ihepoint intersection
9.1.2
Determinqnls qnd Cromer's Rule
Cramer's rule is anothersolution techniquethat is best suitedto small numbersof equa' tions. Before describing this method, we will briefly review the conceptof the determinant, t0 has which is usedto implementCramer'srule. In addition,the determinant relevance the evaluationof the illconditionine of a matrix.
SOLVING SMALL NUMBERS EAUATIONS OF
Determinonts. The determinant can be illustratedfor a set of threeequations: [A]{,t} : {b} where [A] is the coefficientmatrix [Al: ort an anf f I a21 a22 azz I
tl LA3t 432 AT J
2r5
The determinantof this systemis formed from the coefficients [Aj and is represented of as
D : latt I u., at2 Ltt at3 o)1
i" ;;
Although the determinant and the coefficientmatrix [Al are composed the same D of elements, they are completelydifferent mathematical That is why they are disconcepts. tinguishedvisually by using bracketsto enclosethe matrix and straightlines to enclosethe determinant. contrastto a matrix, the determinant a single number.For example,the In is value of the determinant two simultaneous for ecuations
" .;
o .,
ott D : latr I I A)t A)) 
  l
is calculatedby D:atla22an(7zt For the thirdordercase,the determinant can be computedas
ott o"l ot'l D : a ,' , aa !  o ,r aort t a . r : l* o ,.l , l:o " a J 2 l l sz ll : a r .?.r.rl l
where the 2 by 2 determinantsare called minors. Determinonts
(e. 1)
Problem Stotement. Computevaluesfor the determinants the systems of represented in Figs.9.l and9.2. S o l u t i o n . F o r F i g . 9 . 1:
D
JL
1
2
:3(:2)2(1):8
ForFig.9 2a:
L _ !
D
2l 1

:  ; ( ll )  t l ; l :/01 \
L \L,/
ForFig.9.2b: D:l l1 2
lr
r  :  t I : 2 )( 1(l):0
z
3 *0.0r 0. r r 0 .e I 0. .2c: D_ )' . b ..04 I /? ?\ 2 \s/ In the foregoing example. . 1 r r + 0 .r 0. Use Cramer's 0 . (Fig.I I I 0.1 w o u l d b e b s x computed as lbr oe 4r:i I bz azz azt I I tr^ a1. (9. These ideas will be pursuedfurther in our subsequent discussion illof conditioning Chap.s r 0.5.1)]: as Solution.9rj: canbe evaluated [Eq.67 r.01 0.52 I 0.0022 : 19.67 0 ..01 10. 3 .0022 I o.03278 . This nrle states of tions may be expressed a fraction of two determinants as with denorninator andwiththe D numeratorobtained from D by replacing the colurnn of coefficientsof the unknown in q u e s t i o n y t h e c o n s t a n tb r .o r t h r e ee q u a t i o n s .67 r 1. The determinantD a:o:lol: l?lo"l3i A:31.'l3i ooo22 o:l: The solutioncan be calculated as .043s6 0. . b 2 . .r 0.1 4 . 5 1 3: . in that eachunknownin a system linearalgebraic equaCromer's Rule.3 0. _:l : tr I ___1)_ll_l:_0.0022 0. 4 4 x2f l.52 . .0. 9.44 0. 9 : 0.F o r e x a m p l ef. .'.8 .0022 i o.I l.5 0.2 Rule rule to solve ProblemStotement.216 GAUSSELIMINATION For Fig.2c) has a detenninant the resultssuggest that the systemthart ahnostsingular' is that is close to zero.9 0. 9.44 0.the singular systemshad zero determinants.01  0. 5 2 x 21 + 0. l .r I  0.3 0.3 0.0649 _ _)a < 0.0 . 4.0022 10.5 4..: EXAMPLE Cromer's 9.0022 0.rr + r:: 0.44 0. Additionally.6l I o. . 3 x 2 * 0 .
as the number of equationsincreases.3000 1. determinants time consumingto evaluateby hand the are (or by computer).Cramer's rule becomesimpractical because. more efficient alternatives used.5 >> D= 0 .441 0.{i as in >> A(:.3 0.9.Consequently.0. (9.5) will.0000 0. (9.3 0. Cramer'srule can be appliedto compute.1 0.Someof thesealterare nativesarebasedon the last noncomputer solutiontechnique coveredin Section9. therefore.6700 0. (9.4400 >> x1=det X1 = 0.0000 1.eliminatethe x1 term from the equations to yield : a t t a 2 2 x.0100 0. The determinant be computeddirectly in MATLAB with the det function.9000 For more than three equations.0.2) (e.1)= t0.4) (9.5000 (A) /D 14. to For example.a y l a l 2 x z a t t b Z.SOLVING SMALT NUMBERS EAUATIONS OF 217 can The ae' Function. (9.5200 1.1. For example.2)might be multipliedby a21andEq. 9.4) from Eq.3) The basic strategyis to multiply the equations constants that one of the unknowns by so will be eliminatedwhen the two equationsare combined.61 .0422 D=det (A) ii ti 1 1.3the elimination of unknowns. 0.0.52 I.3)by all to give aztTltxt I a21A12X: AZlbt 2 attbz (e.0I.51.a y b t 2 which can be solvedfor a 1 1 b 2a21b1 .9000 0.1.5) : a11a21Xy a11a22x2 { SubtractingEq.This valuecan thenbe substituted into either of the original equations computethe other variable.using the systemfrom the previousexample: >> A10.3 Eliminotion Unknowns of The elimination of unknownsby combiningequations an algebraicapproach is that can be illustrated a setof two equations: for a 1 1 x 1* a 1 2 X 2 : 1 1 1 aZtxtla22x2:fi2 (e. Eq.The result is a single equation that can be solvedfor the remainingunknown.
. The result into could be solveddirectly andthe l'esultbacksubstituted Consequently. one of the original equations to a This basicapproach can be extended largesetsofequationsby developing systemto and Gausselimination atic schemeor algorithrnto eliminateunkr.3): of equations..1____J___i_I lrr.x.7)follow directlyh'om Cramer's rule: th' anl .' I d 1 1 b 21 a21b1 lQtr tl I c. is the most basicof theseschemes. However. 2.3 _.(9. .In particular.. r x .2.9. to one The equations of this elimination stepwas that we had one equationwith one unknown.the computerprogram must avoid division by zero.8c) . ' I a 2 .: (9..which can be solvedfbr Equation into a 2 2 b 1.218 ELIMINATION GAUSS (9. (9..some modifications will be required to obtain a reliable algorithrn. the numerouscalculationsthat are requiredfor larger systems in 9.r. procedure The consisted two steps 1.: S . Section 9.1.2). (9. to The approachis designed solve a generalsetof n equations: d 1 1 1 1 q s z x z l 4 r . Althoughthese techniques ideallysuited implementationon computers.6)and (9.t x :* ' " * ayll * ( r 2 X 2I d z r j r : * ' I ay.a12lt2 0tt)2 A2jal? Il. tor This sectionincludesthe systematic techniques forward eliminationandbacksubfor are stitutionthatcompliseGauss elimination.8r) :: 1a.84) .3 will deal with the additional featuresrequired for an et'fectivecomputer progranl.2 NAIVE GAUSSELIMINATION In Section9.rinate of the unknownstiom the equations.u p b 2 d 1 1 4 2 202yAp Noticethat Eqs._t_t_ lb.: $t (9.6)can thenbe substituted Eq. . The tbllowing method is called "naive" Gausselimination beciiuseit does not avoid this problem. attl aybl .However.the eliminationof unknownswas usedto solve a pair of simultaneous (Fig. were manipulated elin. . 9. the methodextremelytediousto irnplementby hand.3. equation this solvefor the remainingunknown.rowns to backsubstitute.as described Section lbr the techniquecan be formalizedand readily programrned the conlputer. a n l l 1O t t A)) l o " ut' l b 1.lrr an J tltt I I to with rnorethan two or three The eliminationof unknownscan be extended systems make eqLrations.
. . 6..10a) rq InA\ a \ t x t * a \ .9 u .) r ...3 The phoses Gouss (ol h.azo of eliminotion:forword eliminotion (b)bock ond substitution As was the casewith the solutionof two equations. techniquefor n equations the consists of two phases: eliminationof unknownsand solutionthroughback substiturion.. + (. (9. att  eht br Qtt (e.4 (a) Forward elimination I (b) Back substitution h: xt: b"z/a'\z (b'r.b.+ ... I a'"rr. The first phaseis designed reducethe set of to equations an uppertriangularsystem(Fig. 6 \ .' lt" Lo! .'+ 9or. ..*. multiply Eq...r:. . : (b.* * a1ux.dr:.. Forword Eliminotion of Unknowns..#....' 4rr .ELIMINATION GAUSS 9.8a) can be multiplied by a3llayl and the result subtracted from the third equation. 9.aptzlfa.: $. (9... + where thc prime indicatesthat the elements havebeenchangedfrom their original values.The initial stepwill be to elininate the to first unknownr1 from the secondthroughthe nth equations.!) !6.Eq..8b) to give (.Repeating the procedurefor the remainingequations resultsin the lbllowing modified system: (t11x1 ctpl2 l * n13t3 ..e) Now this equationcan be subtracted from Eq.' "" I i o'..2 NAIVE 2r9 drz an [r. (9.:.a34lftt'22 t.3a). or : tt'rrxz . .rrl dz2 azt a32 att o' ['"  { .' o'. for For instance. FIGURE 9..r). do this. ..8a) To by a21 all to give f a21xy\op*z * alt *Lanxz+ atl . x . x 3 a " ' * r t l . (9. The procedureis then repeated the remainingequations.
nipulationin the sequence to usethe (n . \l '1 7 l t .. tI d l r r .' have beenmodified twice. this point.r:6\ (9.x.l4 1 . (9.: Bock Substitution. can be represented thefollowins fbrmula: il 6ri tt _ 'r \/ .which is repeated evaluatethe remaining:r's.. .rr. Equation ht \ n . . 1. where the double prime indicatesthat the elements The finalmaThe procedurecan be continuedusing the remaining pivot equations.x. and subtractthe result from Eq.x.: 11'.10d).r..10d) the For the foregoingsteps.220 GAUSSELIMINATION 3 ctr.la'i.x.. 6\ : l o ) .'* . (9. is to Note that the process multiplying the first row by u21 tr11 equivalent of f to . I a'r. r * . Perform a simila to elimination for the remainingequations yield { 7 1 1 . I a'r.rrx.:6'.1 ld) (9.. (9.. i it.llc) (rl) ann pn into the (n .We makethis distinctionbecause zeropivot elementcan interfere we normalizationby causinga division by zero..11r) (9.r*. r 1 1 ..4* . .: b'.. t. : 6.I )th equationto eliminatefhe x..11d)can now be solvedfor x. The This result can be backsubstituted to by cedure. The next step is to eliminatex2 from Eq.. lOc).10c) through (9. a'1.multiply Eq. . the systemwill have beentransformed an uppertrianguAt lar system: a t t x t I a n x z I r z 1 3 J* . r r a "'LA1nX.. tr\ar2+ aztu * ...l)J" . r : f i 1 3 a'zzrz* c/ar4 + . (9.. : h " t t (9..l i:i+l fori:nl. Eq..*ct. . .1 termfrom is to the rth equation. ai{tr. ..We will returnto this importantissueafter complete our description naiveGauss of elimination.xza1].10c) E} a ' r ' r . ro u r r .. r l x2 a'.8a)is calledthe pivot equationandall is called pivot elentenr. To do this.. t al. the dividing itby all and multiplying it by a21 Sometimes division operationis refened a with as nonnalization. * a'1. .n2. (9.10r) by aj2la\.:['! :: ai. f .... I a 1 r r . .l )th equationto solve for ir.:lt.1 t) (9..
00333x: 0.00333x:.t h e s e to f e q u a t i o nis 0 .2 NAIVE ETIMINATION GAUSS E X A M P L E3 9. can be solvedfor 7. 2 ( . (89.2x2+ l0.6) 7.293333(7.3.00000 Although thereis a slight roundoff eror.0.561'7 10. 4 0 0 3 1 1 .0.lxr* .t: 0.81999 7.l) rFq ? ?r (E9. ^z: 2.3 71.5617 0.oooo3 10.3.50000 canbe substituted backinto Eq.1 22r (Ee. (E9. solvedfor 70'0843 : 7. multiply Eq.5) (E9.00333x2 0. This can be verified by substituting resultsinto the .2.3. 3 ( 3 ).2(7.3.2)to give 7.5617 .50000) 0.19.8) (E9.34: 0. (89.85 : = 0.To accomplish this.3.293333n : 19.0120tj: 70. (E9.6). 0 0 0 0 3 : 1 1 . (E9.00003andx2 : 2. the by 0.o.2(7.3.8). (E9.0 ."r:: 7.9.lrz 0.3.3(7.3.00333 which Finally.0. Noive GoussEliminotion elimination solve to Problem Stqtement.0.6150 * be To completethe lbrward elimination.313and subtract from Eq. 2 x 31.3.1(3) 7(2.5.00003) + : 2.3x1 7.2 .1) Solution.3) is Multiply Eq.l x z 0 . The first part of the procedure fbrward elimination.3.3 + : : 0 .7). Eq.00003) + : 3.rr rrIUSt removedfiom Eq.t9.85 rFq 1 4) (89.0120 into This result can be backsubstituted Eq.1(2. Use Gauss 3xr.This eliminates from the third equationand reduces systemto an upper r? triangularform.3.0200x:: 70.190000x2 10.3).85 3xr0 . (89.6).2x2: 7x20. (89. s t i o n s .50000 7.3. (E9.00003) 19.9)can be We can now solve these equationsby back substitution.5) .5617 it After theseoperaThen multiply Eq. &I1d : 7. as in (E9.which can then be solvedfor . 2 x 3: 7.3.30000 19.3.85+ 0.3.0.1l3 and subtract resultfrom Eq.00333 and subtractthe result from the Eq. I (. (E9.00003) 7.85 19.3.293333xt: 19.5) 0.1)by 0.19000011. 4 = ) .293333x: . 5 ) + l 0 ( 7 .5)by 0.0843 (E9.rj original equationset: r(r).3. the resultsare very closeto the exact solutionof the rr : 3.7) 7.9) First.
Aug .(Au.Aug(i.k:nb) factor*Aug(k.4. MATLAB's ability to perform matrix operations allows Eq./Auq(n.2. Thus. Two nestedloops provide a conciserepresentation the tbrward elimination Al of step. Finally. if m=n.2 Operotion Counting The execution tine of Gauss eliminationdepends the amountof .J(i. oLtter loop movesdown the matrix from one pivot row to the next.Aug(n.nb).n) . end ? forward elimination for k = 1:n1 for i = k+1:n .k) /Auq(k. i+1:n) *x(i+1:n) ) iAug(i. (9.l). 9. The inner loopmoves below the pivot row to each of the subsequent rows where elimination is to takeplam.b) % GaussNaive: naive Gauss eliminatiorr % x = GaussNaive(A. ior i = n1:i:1 . error('Matrix nb . A must be square').Again. nt G e 9. The backsubstitutionstep follows directly from Eqs. (9.4 9 A n M J i l ei o r m p i e m en o i v e o u s s l i m i n a t i o n . Noticethat the coefficient matrix A and the righthandside vector b are combinedin the augmented matrix Aug.nl = size (A) .k:nb) .222 GAUSSELIMINATION function x = caussNaive(A. x(n) . the actual elimination is represented a single line that takes advantage by of MATLAB's ability to perform matrix operations.2.k:nb) Aug(i.13). ? x=solutionvector lm.i) x(i) end . 9.12) and (9.nb)Aug(i. the operations performedon Auq ratherthan separately A and are on b. end end ? back subsr'rue ion x = zeros{n. FIGURE .13) to be programmed asa sinsleline.[A b]. withouL pivoting.k) . factor = Aug(i.floatingpoint on operatilv .b): Gauss elimination ? input: % A=coefficientmaLrix % b = rigirr hand side vector % outpuL.n+1.I MATTAB Mfile! GaussNaive An Mfile that implementsnaive Gausselimination is listed in Fig.
16) . I i I \ . r r \ .this the to means that the numberof iterations the innerloop will be of f.t il .tolp i 2. then Because : n f 1.l n.9.4) in detail.this iteration of the inner loop. limits on the inner loop are from i: 2ro n..+ .tt"r Flops {rr..t /_6". 1a. (9.Thesecan be summarized as '"r.. k : I ..' . n ( r) ( 2 ) ( r) { 3 ) flops Therefbre. Together amountsto fl + I multiplications/divisions n and with the singledivision. The total for the first pass addition/subtractions every tor throughthe outerloop is therefore .b) \r L \.'. 2 1 (9.2)l.. Now let us examine naiveGauss the elimination algorithm We will first count the flops in the eliminationstage. the iterations the of Similarreasoning be usedto estimate flopsfor the subsequent can outer loop. total addition/subtraction for elimination the can be computed as ir I rr I k't(n+fr):f t k ( 2 n *t ) + / ..r .rzm(ml1)(2mll) :!*o@2) lJ 6i (:e.The next line performs a multiplication and a subtractiontbr each column element from 2 to nb.2 NAIVE GAUSS ELIMINATION 223 providesinsightinto which partsofthe algorithmare Therefore.r : l. 'o.11:nt :  +2 +3 +. On Therefbre. the first passthroughthe outer loop.r. r + o r ir .f ) where O(rl") means"terms of order tn" and lower. going fiom 2 to izb resultsin r multiplications nb and n subtractions.t.I j(r) ln2)lnt) Flops {. as Before analyzingnaive Causselirnination. il (9..t:tt_ / i:2 2+l:n I (9. rt aatiri""Tsritr".l)(n 1 l) multiplication/divisions (n .r'ir /' /'"' .14e) \. totalingup theseoperations most tirneconsumingand how computation time increases the systemgetslarger. L' : I * I +  +.9. r. +t1t t n ( n r* It: l) : nr_ k+l nr) I O(nt) (9.+3+.t1.". ..." (Fig. s) I For every one ofthese iterations. \.rllfu+1) l. will first define somequantities we that facilitate operation counting: il1 il1 111 In tn tl li \. According Eq."n".l (ir k)(n+2k) : k kll n ln k)ln I k) + rr.l)(n) and Qt addition/subtractions.r . r i r //_. thereis one division to calculatethe factor.+ 2...tu.. ..\ .ion Outer Loop k I 2 tr.n 3.14d).14cd) (9.
i t .) llI1r\ T * Forward elimination Back substitution 2! * oCl Two usefulgeneralconclusionscanbe drawn ftom this analysis: l. As the systemgets larger. These canbe added to arrive at a total of n2+ o1ny Thus. the total efTortin naive Gausseliminationcan be represented as (9.the O (n?) andlower termsbeconre negligible.1.2\ EXA (9. t + o r n z .The result is written in this way because n gets as justifieclin concludlarge. J L. Because the extradivision of prior to the loop.t * orr'.224 GAUSSELIMINATION n ( n t .As in Table 9. 6 7x I A B 87. in N u m b e r f f l o p sf o r n o i v e o u s s l i m i n o t i o n .'' J A similar analysislbr the multiplication/division flops yields + [n3 olf)] + + [n3 o(n)] [].58% 9853% 99 85% .[ n 3 o r n z t ] *[ ] .201 Thus.+ n2 +o(i. ) ) . (9. the numberof multiplication/division flops is n(n l l)12.We are therefore ing that for largen. the total number of flops is equal to 2nt 13 plus an irdditionalcomponent proportional to terms of orclernr and lower.r9) gives Summingthese results ln' ^ J lO(tt') (9.Q t * t )f r + f t ' t t A:t k:t tl Applying someof the relationships from Eq.back substitution much simplerto evaluate.the amountof flops increases nearly three ordersof magnitudefor every order of magnitudeincrease the numberof eouations.] :  * orrt. o G e Bock Substituiion Tolol Flops 2tt313 Percenl Due to Eliminction TABTE .14) yields + l n 3+ o ( . on f Because only a singleloop is used. is The numberof addition/subtraction flops is equalto n(n . ..the computationtime increases greatly. t)tl L_) ' (9.l) 12. l :  * o r r .221 oUr'.1 9 n Eliminqfion 705 6 7 15 5 0 667xlaE t0 r00 r000 r00 r0000 I x lOo 805 6 8I 5 5 0 668 x IOB 667 666667 6 . the effort involved in forward eliminationconverges 2n3 3.
9.3 PIVOTING
2.
225
Most of the effort is incurredin the eliminationstep.Thus, effortsto make the method n.rore efficient shouldprobably focus on this step.
9.3
PTVOTNG
The primary reasonthat the foregoing techniqueis called "naive" is that during both the phases,it is possiblethat a division by zero can elimination and the backsubstitution occur.For examole.if we usenaive Gausseliminationto solve 2  r 2 1 . 1 18 1 4rr * 6ru *7.rt  3 2x1  3x2 f 613 5 the normalizationof the first row would involve division by ay1:0. Problemsmay also if arisewhen the pivot elementis close,ratherthan exactlyequal,to zerobecause the magthen roundoff errors nitude of the pivot elementis small comparedto the other elements, can be introduced. it to the Therefore, beforeeachrow is nomralized, is advantageous determine coefficient value in the column below the pivot element. The rows can thenbe with the largestabsolute partial pivoting. This is called elementis the pivot element. switchedso that the largest If columnsas well as rows are searched the largestelementand then switched,the for pivoting. Cornpletepivoting is rarely usedbecause procedure called contplete is switching and, consequently, adds signilicant and usually uncolumns changesthe order of the .r's justified complexity to the computerprogram. The following example illustrates the advantages partial pivoting. Aside from of division by zero,pivoting also minimizesroundoff eror. As such,it also serves avoiding as a partial remedy fbr illconditioning.
9.4 EXAMPLE ,
Poriiol Pivoting to elimination solve ProblemStotement. Use Gauss :2.0001 0.0003xr+ 3.0000x2 : 1.0000xr 1.0000.12 1.0000 * , ; r arr Note that in this fom.rthe first pivot element, : 0.0003,is very closeto zero.Then repeat the computation,but partial pivot by reversingtl'reorder of the equations. The exact is 'r1 : I /3 andxz : 213. solution by Solution. Multiplyingthe first equation 1/(0.0003)yields rf 10,000x2:6661 r ' which can be usedto eliminater1 from the secondequation: 9999x2: 6666
back into the first equawhich can be solved fbr:r2 : 213.This result can be substituted tion to evaluate  : "rr
^t
2.0001 3(2/3) 0.0003
(E9.4. r)
226
GAUSSELIMINATION
Due to subtractivecancellation,the result is very sensitiveto the number of significant c f i g u r e s a n i e di n t h ec o m p u t a t i o n : AbsoluteVolue of PercenlRelotive Error for x,
Significonr Figures 3 4 5 6 7
x2
.rt
I 11
0 667 0.6667 4 66667 0 666667 0.6666667
0 0000 0 30000 0 330000 0 3330000
r099 r00 t0
I 0t
Note how the solutionfor.r, is highly dependent the numberof significantfigures. on Thn is because Eq. (E9.4.1), we are subtracting in two almostequal numbers. On the other hand,if the equations solvedin reverseorder,the row with thelarger are pivot elementis norrnalized. The equations are : 1.0000xr 1.0000x2 1.0000 * :2.0001 0.0003xr* 3.0000tr Elimination and substitutionagain yields x3 :213. For different numbersof significant figures,rr can be computedfrorn the first equation,as in
  (213)
I figuresin the computation: This caseis much lesssensitive the numberof significant to AbsoluteVolue of PercenlRelotive Error for x,
Significont Figures
x2
xl
3 4 5 6 7
4 667 4 6667 0.66667 4 666667 0.6666667
0 333 0 33?? 0 33333 0 333333 0 3333333
0l 00r 0 00t 0 000r 0.0000
Thus, a pivot strategyis rnuch rnore satisfactory.
9.3.1 MATLAB M'file3 GaussPivot It An Mfile that implementsGausselimination with partial pivoting is listed in Fig.9.5. prel'iouslyin Section 9.2.1 is identicalto the Mfile for naive Gausselirninationpresented with the exceptionof the bold porlion that implementspartial pivoting. availNotice how the builtin MATLAB function max is usedto determinethe largest able coefficient in the column below the pivot element.The rnax function hasthe syntax [y,r] = max(x)
9.4 TRIDIAGONAL SYSTEMS
227
function x = caussPivot(A,b) pivotingr % GaussPivot: causs eLimination % x = GaussPivot (A,b) : Gauss eliminarion % input: ? A=coefficienLmatrix ? b  right. hand side vector Z outpuc: % x=solutionvector
vrith pivor,ing
Im,n]=size (A) ; A must be square'); if m.=n, error('Matr.ix end nb=n+1 ; Aug= [A b] ; % forward elimination for k  1:nl eo partiaL pivoting t b i g , i l = m a x ( a b E( A u g ( k : n , k ) ) ) ; ipr=i+k1; if ipr=k AUS([k,ipr],: )=Aug( [ipr,k],: ); end for i = k+1:n factor=Augr (i, k) /Auq (k, k) ; Aug ( i, k : nb) =Aug( i, k : nb)  factor*Aug (k, k : nb) ; end ? back substitution x=zeroS(n,1); x ( n )  4 L 1 g( n , n b ) / A u g ( n , n ) ; for i = n1:1:1 x ( i ) = ( A u g ( i , n b )  A u g r( i , i + 1 : n ) * x ( i + 1 : n ) ) / A u g ( i , i ) ; end
FIGURE 9.5
A n M J i l e t o i m p l e m e ntfh eG o u s se l i m i n o t r ow i i h p o r i i o lp i v o t i n g n
9.4 TRIDIAGONAT SYSTEMS
Certain matriceshave a particularstructurethat can be exploitedto developefficient soluFor example,a bandedmatrix is a squarematrix that has all elementsequal tion schemes. zero, with the exceptionof a bandcentered the main diagonal. to on generallyas A tridiagonal systemhas a bandwidthof 3 and can be expressed
fi8r
e2 .fz 8z e3 ft
r1
.{1 41
rl
r.

(9.23)
€tt I
.f,,l
8nt
Xnl
' n 
GAUSSELIMINATION
Notice that we have changedour notationfor the coefficientsfrom a's and b's to e's,/s, zerosin thesquare ma,g's,and r's. This was doneto avoid storinglargenumbersof useiess algonthn modificationis advantageous because resulting the trix of a's. This spacesaving requires lesscomputerrnemory. An algorithmto solvesuchsystems be directly patterned can afterGauss eliminationthat is, using forward elimination and back substitution.Howeveq because mostof thc This matrix elementsare already zero, much less effort is expendedthan for a full matrix. efficiency is illustratedin the following exarnple. EXAMPLE .5 9 S o l u t i o no f o T r i d i o g o n o lS y s t e m
Stotement. Problem Solve the following tridiagonalsystem:
l f2.04 2.04 lr
, ll;tt_lTfl ':i :l t:t t,3;1l I
:2.04 l
the Solution. As with Gausselimination,the tirst step involves transforming matrixh e2l upper triangular form. This is done by multiplying the first equationby thef actor f ynl equation. This creates zero in placeof e2and a transsubtracting resultfrom the second the forms the other coefficientsto new values. f z : . f zt'j:
?a
T+t
t') ?,',:0.8f,
1) : I '550 2JJ4e I : t40.8t20,8 1.04
the Notice that g, is unmodifiedbecause elementaboveit in the first row is zero. is After performinga similal calculationlbr the third andfourth rows,the system tranr formed to the upper triangularform
,.lo :os lI;i I:I i33 [204 tI ,.i,J I I ift','; f l;; I
the Now back substitution can be appliedto generate linal solution:
: ,o: 'io '+#:
,r:
t'3  83rl
r5e 48o
fa
,'z  82x3
,fz
rl ^l 
8lJ2
.ft
14.22r (l)159.480 :124.538 1.395 20.800 ( l) 124.538 : 93.718 1.550 40.800 ( l)93.778 :65.910 2.040
9.5 CASE STUDY
function x = Tridiaq(e,f,g,r) % Tridiag: Tridiaqonal equation solver banded system t x = Tridiag(e, f,g,r): Tridiagonal system solver. % input: ? e=subdiagonalvector Z f= diagonalvector g = superdiagonal 2 vector 2 r = right hand side vector % output: ? x=solutionvector n=lengrth (f ) ; ? forward
f ar V ).r
elimination
= e(k) /f (k1); factor  factor*g{k1); = f{k) f(k)  factor*r(k1); = r(k) r(k) end % back substirurion (n) ; x(n) = r(n)/f for k = n1:1:1 x(k) = (r(k)s(k)*x(k+1) )/f end
(k) ;
FIGURE 9.6 AnMJile soveo tridiogonol to sysiern.
9.4.1
MATLAB Mfile! rridiag
An Mfile that solvesa tridiagonalsystemof equations listed in Fig. 9.6. Note that the is algorithmdoesnot includepartialpivoting. Although pivoting is sometimes required,most tridiagonalsystems routinely solvedin engineering and science not requirepivoting. do Recall that the computationaleffort for Gauss elimination was proportional to n3. Because its sparseness, effort involved in solving tridiagonalsystems proportional of the is to n. Consequently, algorithm in Fig. 9.6 executes the much, much fasterthan Gausselimination,particularlyfor large systems.
MODELOF A HEATED ROD
Bockground. Linear algebraicequationscan arise when modeling distributed systems. For example,Fig.9.7 shows a long, thin rod positionedbetweentwo walls that are held at constant temperatures.Heat flows through the rod as well as between the rod and the surrounding air. For the steadystate case,a differential equation basedon heat conservation can be written for such a system as d2T ax
_ +h'(7. z):0
(9.24)
230
ELIMINATION GAUSS
continued
7,,: 20
fo: 40
I
Is = 200
ti{it
,r=0
 Lx;'7,,:20
{ ,
FIGURE 9.7 two temperoture rod between wolls constont different of but A noninsuloted uniform positioned
The {inited'flerence representolion 66is5. employsf6ur inls1ie,
where I: temperature("C), x : distancealong the rod (m), h' : aheat transfercoefficient ("C). betweenthe rod and the surroundingair 1m2), ztrdTo: the air temperature Given values for the parameters,forcing functions, and boundary conditions,calculus = can be usedto developan analyticalsolution.For example,if h' :0.01, 4, : 20, T(0) 40, and (10) : 200, the solutionis t^ T :13.4523n0'tx  53.4523e0 + 20
(9.2'
In Although it provided a solutionhere,calculusdoesnot work for all suchproblems. we such instances,numerical methods provide a valuable alternative. In this case study, will use finite differencesto transformthis differentialequationinto a tridiagonalsystem deof linear algebraic equationswhich can be readily solved using the numerical methods scribed in this chapter. into a setof linear algebraic equations by Solution. Equation(9.24) canbe transformed the conceptualizing rod as consistingof a seriesof nodes.For example,the rod in Fig.9.7 between nodes.Sincethe rod has a length of 10, the spacing is divided into six equispaced nodesis A,x :2. it Calculus was necessary solve Eq. (9.20 because includes a secondderivative. to provide a meansto transfom approximations As we learnedin Sec.4.3.4,finitedifference algebraic form. For example, the second derivative at each node canh derivatives into approximated as dzT dt
Ti+t ZTi *Tir
Lx2
into where I designatesthe temperatureat node i. This approximation can be substituted (9.2q to give Eq. Ti+r2Tr * Ti*t
n, 2
+h'(7"4):0
9.5 CASE STUDY
continued
gives Collecting terms and substituting parameters the Ti*r*2.04Ti4+r:0.8
(e.26)
Thus, Eq. (9.24)has beentransformed from a differentialequationinto an algebraicequation. Equation(9.26) can now be appliedto eachof the interior nodes:
To*2.04T14:0.8  Tt * 2.0472 Z: : 0.8  Tz 12.04\  Z+: 0.8  Tt t 2.0474 Is : 0.8 
(9.27)
The values of the fixed end temperatures, : 40 and Ts:200, can be substituted Io and with four unknownsexpressed moved to the righthandside.The resultsarefour equations in matrix form as
a; {i^l:{}i} ffi' 'it;]
>> A=12.A4 1 0 0 r 2.04 1 0 a r 2 .0,1 L t) 0 1,2.A4); >> b=i40.8 0.8 0.8 200.81';
>> T= (A\ b) ' T= 65.9698 93.',i18a 'r24.5382 r59.41 9a
(9.28)
So our original differentialequationhas been convertedinto an equivalentsystemof lineal algebraicequations. Consequently, can usethe techniques we described this chapin ter to solve for the temperatures. For example, using MATLAB
A plot can also be developed comparing these results with the analytical solution obtained with Eq. (9.25),
. L j' i t;
>> x=[0:2:101; >> xanal=f0:101; >> TT0 (r) 13.452 j*exp (0.1*xanal) (0.1*xanal)+20;
t,r.rj=1:(Ydl); >> plot it ,'l , 'o' , ranal, Tartal )
53.4523*exp
. . .
As in Fig. 9.8, the numericalresultsare quite closeto thoseobtainedwith calculus.
232
GAUSSELIMINATION
confinued
Analytical (linel and numerical (pointsl solutions
220 200
180 160
9.1  the r (Fig. 4
9.2 
Chec equa
9.3 (
140 r. 120 100 80 60
(a) S
rl (b) c
fi
(c) c 9.4 (
10
(a) C
FIGURE 9.8 versus rod. A plot temperoture distonce of olong heoted Boih o onolyticol ondnumericol {line)
solutions disployed. ore lpoints)
(b) u (d) s
(c) U tt
We In addition to being a linear system,notice that Eq. (9.28) is also tridiagonal. can use an efficient solutionschemelike the Mfile in Fis. 9.6 to obtain the solution:
>> e=10 1 1 11; >> f=12.04 2.04 2.04 2.04); >> g=i1 1 1 01. >> r= [40.8 0.8 0.8 200.8] ; >> TridiaS (e, f, q, r )
tc 9.5 C
c
l.(
65.9598
93.7185
r2,4.5382
r59 .4195
(a) So (b) Co (c) On reg (d) Sol (e) Sol Intr
The system is tridiagonal because each node depends only on its adjacent Becausewe numberedthe nodes sequentially,the resulting equationsare tridiagonal. cases basedon conservation laws. often occur when solving differentialequations
9.6 ci' )
(a) Sol con
PROBLEMS
PROBTEMS
Determine number of total flops as a function of the number equationsn for the tridiagonal algorithrn of (b) Substituteyour results into the original equations to check your answL.t's. 9.7 Given the cquations 2r16x2rr:38
9.6). Use graphical the method solve to t . r t :  2 4 8 4.r  {* 6 , r : : r 31
your resultsby substitutingrhem back into the Given system equations the of
3rr;r:*7.r.t:34 *8rr +  r 2  2 x 1:  2 0
(a) Solvebv Gauss c.limination with partialpivoting.Show all stepsol the computation.  l . l r 1* (b) Sr"rbstitutc your rcsults into the original equationsto l0r2: l16 checkyour answers.  2xt+ ll .4.b : 174 9,8 Pcrforn.r samccalculations in Example9.5, but lbr thc as graphically check your resultsby substituting the tridiagonal Solve and system: them into thc equati(rn\. bucl 0.4 0nthe basis thegraphicalsolution,what do yor.r of expect Ios l tll . r I c).4 0.8  0.a regarding conditionof thc system) the    L1 Compute deter the minant. {).4 o8j.r. L Given systemof equations the 9.9 Figure P9.9 shows three reactorslinked by pipes. As  3 t : + 1 x ' ,: 2 indicated, the rate of transf'erof chenticalsthrough each pipe is equal to a f'low rate (Q, with units of cubic meters x112.12 .rr : 3 per second)rnultiplied by the concentration the reactor of :2 5 . t 2  r 2 1 (c, fronr which thc flow originates with unirsof milligrarns Compute deterninant. the per cubic metcr).If the systcmis at a steadystate,the transUse Cramer's rule to solve fbr thc r's. f'cr into each reactorwill balancethe transferout. Develop Use Gauss elimination wi(h partialpil,otingto solvetor r.niissbalarrce equations for lhe reactors and solve the the r's. threc simultaneous linear algebraic equations for their Substitute resultsback into the original equations your conccntratlons. tocheck yoursolut.ion. Given equations the
:{f,
0 . 5 ;1 . r 2 : t 1 . 0 2 2 x 2 : t1
9.5 18.8
Qnct
Solve graphically. Compute deterrninant. the {b) (c)0n the basis of (a) and (b), what would you expect regarding system'scondition? the Solve the elinrinationof unknowr.rs. by {d) (t)Solve again.but with a,, modified slightly to 0.52. your Interpret results. 9,6 Given equatiolrs the 1 0  *1  r 2  , r 3 : t2 27  3.t1 6t2* 2.r3: 61.5 .r1.r2*5.r::21.5 * (l) Solve naiveGuuss bv elirnination. Shorr,' steps the all of computatton.
200 mg/s
Qz(z
Qr.: 120 Qrr: 40 Q,z= 90 Qz::60 Qyt:3o
FIGUREP9.9 Three reociors linked pipes Theroteof moss by trcnsfer through eochpipeis equo to theproduct f ow Q cnd of concenirotion thereccfor c of fromwhichthef ow orgrnoles.
234
GAUSS ELIMINATION
9.10 A civil engincer involved in constructionrcquires the lbllowine difl'crential equationrcsultsfiom a steady$ate .1800,5800, and 5700 rnr of sand, line gravel, and coarse massbalance a chemicalin a oncdimensional fbr clnal: gravel,respectivcly, a building plojcct. Thcre arc thrcc lbr dt llt: 0:D U ,kc pits from which thesematerialscan be obtaincd.The cornd.\' d.r of thcsepits is 1rosition r.'u'hcre: conccntri.ition,: tinlc, r : distance, = diffuD c / sion coclllcicnt. U  fluiclvelocity. and li = a firstorder dccay late. Convert this ciitilrcntial ccluation an equiva. to Fine Grovel Sond Coorse Grovel lent systcm simultaneous equations. D of algcbraic Given = : 2, U : l, k  0.2, r'(0.1 311 and c(10) : 20. strive these P iIr 30 t5 55 equations fi'om.r : 0 to l0 and dcvelopaplot of concentfa' ?it2 45 25 30 tion velsusdistance. Pir3 2a 25 55 process depicted Fig.P9.13. 9.13 A stage cxtraction is in In srrch systems, streamcontainirtga weight tiactionr',,, a of a F,. Hou, nranv cubic melels rxlrst be hauleclfiorn u'achpit in chemical enters fronr the lcft at a mass f'low late of Siniultaneously, solvcntcarryinga rvcighttiaction of x,n a nceds? orderto meetthe cnginecr's 9.ll Arr clectricalengineer supervise's ploductionof three the samechemicalentersflom the right at a flow rate0fF,. the balancc can be reprcsentcd as typcs of clectrical componcnts.Three kinds of rrraterial Thus,lbr stagci, a nrass
' ) t
FIGUR
rnctal.plastic,anclrubberare rctpriledtirl production.The to eachcomponent are amountsneeded prodLrcc Metol (g/ component) Plosric (9/ component) Rubber (9/ component)
(P9. 3n) l At cach stage. equilibriumis assumcd be established an to bctween,r', r, as in and ''i K : {P9.ll}) (P9.13b) wheleK is called distribution Equation a coefficient. into Eq. (P9.13a) yield can be solvedfbr r,and substitutcd to F, \ /F, \ rPg.ljir  (;=K Ir, ':g r t' r a /) r ' , \ r t / : lf Fr : 500 kg/h, .\'in 0. 1, F.: 1000kg/h, rin = 0, and K : 4, delerminethe valuesof _r',,u, x.,,,, a tlvestage ancl if rcactoris uscd.Note that Eq. (P9.l3c) must be modified to accountfbr thc inflou, weishtliactions when applied the to filst and laststages. r' ,/ ('\
9.14 A viscous pipe se( mechan flows ir to obtai Q, I Qt) 3Qt
Componenf
I
2
J
t5 l7 t9
03c o4a
0 55
o
2
o,=
Qt=
o.=
If totalsof 3.89,0.095, and 0.282 kg of rnetal,plastic,and lubber', respectivcly, availablecachday, how nranyconrarc ponentscan be producedper day'? 9.12 As cle.scribed Scc. 9.3, lineal algebraicequations in can arise in the solution of difl'erentialcquations.For example.
9 . 1 5A followi BC,AL
74k
<_FIOW: A_
Flow:
F,+
P9.I3 FIGURE
process A stoqe extroclion
FIGUI
PROBLEMS A,+AD:o A,*AB:0
74+ BC+(3/s)BD:0 AB  G/s)BD:0 BC+(315)CE:0
lJ  CD AD r DE
235
A / 5 t CE : 0 (3l5tBD:0
CD+ (415)BD:O  D E  ( 3 / s ) C E0 : E \ . 1( 4 l s ) C E  0
9.1(t A pentodiagonal witlr a bandwidth flve can system of generally beexprcssed as
P9.r4
pump deliversa unit llow (Q, ) of a highly A 9.11 peristaltic viscous The network is depictedin Fig. P9.14. Every tluid. section the samelength and dianreter. has Thc massand I energybalancecan be simplitied to obtain thc in everypipc. Solve the following systemof equations in theflow in every stream. Q z + 2 Q t 2 Q : : O Qst2Qo 2Qr:0 3Q. 2Q(,:0
.ft ,9r hr e2 .fz 8z h2
dt e1 h Br ht
d,,
t
€tt I d,
Jrt €rt
8n .f,,
I
,{l
.rl
rl r2 r3
0,=0,+O,
Qt= Qti Qs
fttl
Qs=Qo* Q,
15A trussis loaded as shown in Fig P9.15. Using the ine of equations,solve for the l0 unknowns. set AB. BD.CD. DE, CE, A,. A,, and E, ,AD.
xrt
Developan Mfile to etficientlv solvesuchsystems withor,rt pivotingin a similar fashionto the algorithmusedfbr tridiagonal matrices in Sec.9.4.1.Tcst it fbr the following case:
24 kN
lo Lo
t_" t_;
2
l 9 4 3 al 1 ,1 0 7
0 I
lf
3
:ltii til
P9.r5
Knowing how to express multiple righthandside knowing how to evaluate Given an LIJ factorization. be To are we with the MATLAB doculnentation. . the involves decomposing coetlcient matrix that LIJ factortzation Understanding different into two triangularmatricesthat can then be usedto efficiently evaluate vectors. becomes equationswith the sarnecoefficients[A]. ' . of s describedin Chap.1) inefficienl it a Although it certainly represents soundway to solve such systems. . righthandside Gausseliminationas an LU factortzation..the tenns "factorization"and "decotttposition" synonyrnous.  [n the parlanceof numericalmethods. havechosento employ the terminology LU factori:ationlor consistent approach is Note that LLl tlecomposition very comntonly usedto describethe same the subiectof this chapter. . Specific objectivesand topics coveredare .. but with different righthand'side when solving constants {b}. Gausselimination is designedto solve systems linear algebraicequations: [A]{r}: {b} (i0. usedto evaluaterighthandside when MATLAB's backslash in Understanding generalterms what happens is operator usedto solvelinearsystems. vectors. a Recognizingthat Cholesky'smethodprovidesan efficient way to decompose can symmetricmatrix and that the resultingtriangularmatrix and its transpose be vectorsefficiently. rizotion LU Focto OBJECTIVES CHAPTER The primary objective of this chapter is to acquaint you with LIJ factorizationt. 9.
once [A] has been "factored" or of "decornposed. 9.. of LU factoriz. r .r.I I can be rearranged give to l A l { r } . t t l I t u ] { .3) Recognize that this is similar to the manipLrlation occursin the first step of Gauss that elimination.iewof the factorizationstrategy.[ b ] If this equation holds.r': Lltt l/ rr I I 0 . The results can be directlyextencled irdimensional to systems.However. r ) tLt li.3)can alsobe expressed rnatrixnotation in anci rearranged give 11'r t u l { .As we learned Section in 9. (r 0 .OVERVIEW [U FACTORIZAIION OF Recallthat Gausseliminationinvolvestwo steps: fbrwardeliminationand back substitution(Fig. For fbr a3x3svstenr: ['l t/l Ll12 .it follows fiom the rulesfor matrixnultiplicationthat (10. Equation(10. Ir 0 0l I 0l ( r0 . (10.2)could be expressed au uppertriangr"rlar as system.. /r: l l that has the propertythat when Eq..f :1 t_ d1 dt dz t 0. Interestingly. example.3). 7 ) ILlldj : lbl (10.In to we addition.{ d }: 0 } Now assume that thereis a lower diagonal natrix witl.let us lirst providea mathernatical oven.the forwaldelimination compriscs step the bulk of the conrputational etlbrt. That is. elimination itselfcanbe cxpressed an LU facrorizttlion.6) lLllul: lAl and (r 0 . pivotingto avoid LU factorization requires divisionby zero. elimination is used to reducethe system to upper triangular form. l . by That is. ( Equation 10.a)is premultiplied it. I O.8) . simplifythe followingdescription.{ b }: 0 (10. ( 10. will omit pivoting. (10. Thus. 5 ) Ll.This is particularly truefor largesystems equations.2)is the resulr. Eq.2.2 ) Suppose that Eq." multiplerighthandside vectors can be evaluated an efTicient in manncr. r} { c t l l : t A l { r } .I OVERVIEWOF LU FACTORIZATION Just as was the casewith Gausselimination..ation methodsseparate timeconsuming the elimination of the matrix [A] from the rnanipulations the righthandside {b}. fbllowing explanation limited to a setof threesimultaneous the is equations.rl's on the diagonal.2. Gauss as Bcfole shclwing how this can be done.
the resultis substituted Eq.[t] and [U] areusedto determine solution{x } for a righthand a side of First. ar. Substitution step.o FIGUREO.Eq.238 IU FACTORIZATION tAl {. . Gauss elimination can be usedto decompose into [Ll and IU].I a i .".3) into which can be solvedby back substitution {x }.I I ihe steps 1Uloctorizotion.2 GAUSSETIMINATION [U FACTORIZATION AS Although it might appearat face value to be unrelatedto LU factorization. ( 1 0 . 8 ) : . This can be easilyseen [U]. mediatevector{z/}by forward substitution. 1.. 7 )a n d( 1 0 .torirution { lul A lLl .0 .. [A] is factoredor "decomposed"into lower [L] and upper [U] triangularmatrices. I ) for obtainingsolutionscan be basedon Eqs. in A twostepstrategy(seeFig. 2.._ tLl I {d}: {b} V {d} I .lul V ir) : trl) t {ri I Substitution I . Then. This stepitselfconsists two steps.r}: {ll} tut f.(10. can in I o.l 0 assJ whiclr is in the desireduppertriangularformaf. which for [Al is a direct product of the fbrward elimination. I 0.Recall that the forwardelimination is step intendedto reducethe original coefficientmatrix [A] to the form ot' ac 4rr l f .( 10. I lUl:10t L0 .8)is usedto gerrerate interan {b}. fbr Now let us shorvhow Ganss elinrination be imnlemented this wav. LU factorizationstep. (10.3).
This can be readily illustratedfor a threeequation f a r r o t z .if we do not want to changethe equations... an lAl tLltul (10.fp ai:. ur or.we could savethe/'s and manipulate later.) 0 0l 1 0l 0 0 . of This matrix. azz and subtractthe result from the third row to eliminatea.... system. a \ .. a\. aj2 ar ) _ti..9)] 0il Jzt: 0)l and subtractthe result from the secondrow to eliminatea...f:z 1I The following exampleconfirms that [A] : tLl[n.1..a. ... {b} Recall that the whole idea behind the Where do we store the factors/.r2) [r 7rl:lft L ..1 (r0.. elirnination andl. .the [A] matrix can therefbrebe written as l .Thus. in fact.we can storerr in a.10) ) L. Similarly. (9. 1:  . represents efficient storage the LU factorizationof [Al. in a.. The final stepis to multiply and the result subtracted the modified secondrow by t.ri AS I O.2 GAUSSELIMINATION IU FACTORIZATION the Though it might not be as apparent. in a.: l..r'? was to createzerosin at1.t .. '..J.r 3 ) a12 n'.After elimination. andf.: The first stepin Gausselimination is to multiply row I by the f'actor[recall Eq. But thereis absolutelyno reasonthat we have to perform the manipulations Thus. {/lt apl If.. . simultaneously.f.I (r0. matrix [L] is also producedduring the step.. Now supposethat we merely perfbrm all these manipulationson the matrix [A]..1l) where f lUl :10 and on L o t o. a22 ott I r r 1o.t..and a3.r..r.fsr . 7 1I3 rl I o. we Clearly. . T a ' r . alsohaveto do the sameto the righthand side {b}.row I is multiplied by A2t  1t Ltll fiom the third row to eliminatea..
jeeeeee 7 L 0. l l l U F o c t o r i z o t i o n i t h G o u s sE l i m i n o t i o n w Problem Stqtement.f.2 9.293333 [A):[LilU):10.0333333 /. that this merely amounts performside ing the eliminationmanipulations {bl.100 Thus.19 ..3 :0.1 :0.3 After forward elimination.2 [3 I : 1 0 7 ..1000000. Derive an LU factorizationbasedon the Gauss eliminationoerformed previously in Example9.99996 ) 0.3.8)for {d}.I. f u: .2 I the Thisresult be verifiedby performing multiplication [L][L/]to give can of lLltul:l}.1 0.0271.00333 0. ln Example 9. are for Afier the matrix is decomposed. 2 9 3 3 3 3 tul 1 1 0 . I r 0  l0 7. 2 r 0 I1 10.3 [ 3 0.033333]  1 0 .the fbllowing uppertriangularmatrix was obtained: 0. at the end of this step. 0.1 0.1 . Thus. 0 1 2I0 0 L0 employedto obtain the uppertriangularmatrix can be assembled into a lower The f'actors triangularmatrix. 0.0333333 r L 0. solutioncan be generated a panicularrighthanda First.r 0. we used Gausselimination to solve a set of linear algebraic equations that had the following coefficientmatrix: 0.0271300 J uentlv the LU factorizationis Conseq .2 the where minordiscreoancies dueto roundoff. :. 0 1 2 I0 0 1 0 .UFACTORIZATION E X A M P L E0 . 0 0 3 3 30 . This is done in two steps.3.1000000 and the elementd12woSeliminatedby using the factor 0.1 0. 0 2 7 1 3 0 0l l L 0 tI I 0 otT3 tt 0. 1 0 0 0 0 0 . The elements and d11were eliminatedby using the factors d21 EXAA . a forwardsubstitution stepis executedby side vector {b}. the lower triangularmatrix is 1 0l 0 [ r 0l tll :10.the righthand on .3 1 I 0.3 7 lAl:10.0 . Solution. (10.0 .21 [ 3 0. It is importantto recognize to solving Eq. : 7J0333 : 0.
Ll. .: r o I [ * r   t r . 7 .J. (10. I J [.3 d2 0.. which can be substituted into the second equationto solve for d : .g): by o... 8 s I l0 0. Soluiion.0333333dr+ :19.0271300d2 d3 11. : 7.l3)l.Again. +J andthattheforwardelimination phase conventional of Gauss elimination resulted in tfxr I 02e3333 :  'i?i'tI 7oo..yr  . intent of forward substitution to imposethe elimination the is manipulations that we had formerly appliedto [A] on the righthandside vector {b}. ..3. it is importantto recognizethat this is identicalto the backsubstitution phase conventional of Gausseliminarion with Eqs.1 0.1 by generating the in the final solution with forward and back substitution.i3 I. 3 ..3). Recall that the systembeingsolvedis 0. The fbrwardsubstitution stepcan be represented conciselyas s.2. lcompare t.0 . .os+: T3 0.0 .r 92lJ..0.l2) and (9.85 0.85) 19.2 EXAMPLE T h e S u b s t i t u t i oS t e p s n Problem Stotement' Complete probleminitiateci Example10.0lt0 lrr L zo. I [ 7 . 'fl: u11x1  j:i+1 uii .l0.IO.3 1t.4]and {}} sirnultaneously.85 19.3 * 0. rI Lo.4 + 7..4 We cair solve the tirst equation for d.l e . ? fo1 0 0 0 0 0..0 0  Ilj l t0.1r or multiplying out the lefthandside: : d1 7. ( 10. 0 2 7 1 3 0 03l{:l L 0.85. : drf Q.2 GAUSSELIMINATION TUFACTORIZATION AS will be in the samestatethat it would have beenhad we performedforward manipulation on [. il d. As just stated.n The second step then merely amounts to irnplementingback substitutionto solve Eq.:fi.2 ( 7..: 19.. tl.100000dr.85 J l I J The forwardsubsriturion phaseis implemented applyingEq.0333333(7.(9.2 l l l : l .5617 : .ibi j:l il fori : 1.
Use MATLAB to compute LheL(l factorization and find the solutionfor the samelinear systemanalyzedin Examples10.r I or Inr 12.5617) + Thus.l MATTAB Function:ru MMLAB syntax: has a builtin function lu that generates LU factorization.io''./} This resultcan thenbe substituted Eq. canbe substituted thethirdequation give into to : 70.r*y {.2e3333   0 10..0843 dt : 7 1.242 TUFACTORIZATION Bothd. The only differenceis that a little lesseftbrt is expended the factorization in phase sincethe operations not appliedto the riglrthandside.ill. 2 . has the general the It = lu (X) lL.85) 0.The following exampleshowshow it can be employedto generate both the factorization and a solution fbr the same problem that was solved in Examp l e s 1 0 .02713(19. andd. (10. EXAMPLE 0.3 1 tU Foctorizotion ith MATLAB w Problem Stotement. I I :. derived fronrthe LIJ factorizalionof the matrix x. I ltt. : {.r canb e s o l v e d 1 h a c ks u b s t i t u t i o(seeExample9.3). lO.Conversely.:1 I _o1 ro ll*.0.2.2 I xl . substitution are phasetakesa the little more eflbrt..8s 7 o.4.r} : {di: into 0 7.#*vl whicl.IU]{.Note that this function usespartial pivotingt0 avoid division by zero. 1a n d 1 0 . l: lro.ql _02llri .r 0 ..respectively.00333 0.3 for details)for the final solution: n b {d\ : I t o.0120 L0 ) 13 0 .tt) where I and u are the lower triangularand uppertriangularmatrices.I and 10. I The LU factorizatioualgorithmrequiresthe sametotal flops as for Gausselimination.2: 0r f .1 (7.
0033 0 This is the sameresultthat we oblainedby hand in Example 10.3000 0..3.2..1(1 0 0 0 1.0843 And then Llsethis result to compute the solution >> x = U\d 3. 19.2 I0).2933 t].0000 0. C)001r' 0.I >r b = 17. We can testthat it is correct by computingthe original matrix as >> L*LT 3. . 5.2000 0.1.2000 0.2000 0..01r00 a 0 0.4l.5000 7.1 .8500 19 . 1 .0333 0.3.GAUSSELIMINATION IU FACTORIZATION AS Solution.1004 0. The coetlicientmatrix and the righthandside vectorcan be entered stanin dard fashion as >> A = t3 .1000 1.2.a27I 0 0 1.ai2a 3.0000 0.11 70.0000 2. we flrst cornpute >> d = L\b d= ?. the LU factorizationcan be computedwith L 1.0000 To generate the solution.85. Next.0000 t.3000 10.0000 0. iL'00 1.3 'lI.r. .0000 These results conlbrm to those obtained by hand in Example 10.
8 tlrirta s.45366 2.. algorithmis based the fact thata syrnmc'tric This on matrix can be decomposed.[Al : IA)r.n tlii (r0..15) ernployedtocompute is u11:Jan:16:2.l4) ciln be rnultipliedout and setequalto eachother.f a. Forthe firstrow(i : l).nine l6) Eq. Thefactotization can be generatted efTiciently recurrence by relations.44949 Then.44949 55 :22.rrl. canbeused deten.r6) EXAMPLE 10.123124 2. ( 10.44949 l5 Forthe row (l : 2): second : 1r. .1833 .vmmetric nratrix is one whereeij : aji for all i and Inother rvords. of The termsof Eq.(10. Eq.4 CholeskyFoctorizotion Problem Stotement. the resultingtriirneular factorsare the transpose eachother../55 : (6.244 IU FACTORIZATION FACTORIZATION I O. Recall from Chap. in as l A l :l u fw l tr o.2 .factoriz. for (alsocalle/ involves Choleskt .r l. to otz q tt  EXAM Llll at3 .r  L ll l :6.5 s5 I 5..5 22s I 1 5 5 225 919) Ib I r5 Solution.rt 1l ai1 ' 1. science are They offer computational Specialsolution techniques availablefbr such systems. (10. advantages because only half the storageis neededand only half the computation is time required their solution.. Such systems occLlrcommonly in hoth mathematical engineering/ and problemcontexts.For the lth row: (l0._l  upltl11 forji+1.ntion One crf the most popular approaches Choleskydecornposition).3 CHOTESKY j. : .u) That is.12372q2 4.Compute Cholesky the factorization thesymmetric for matrix I A t l?rt 1.
Use MATLAB to compute Cholesky matrix we analyzed Example10.6. First.5 factorization the same for the Problem Stotement. r : . t/ .1833 2 0 .I 8 3 3 F o r t h e t h i r dr o w ( i : 3 ) : u .I l 0 l 0 l I into The validity ofthis factorization canbe verified by substituting and its transpose it the Eq. .. .3 CHOLESKY 423 Ul2ut3 Ll22 245 24(22. 9 1 6 5 6 . After obtaining the factorization. in [6 t A l: l l s 155 ls ssl ss 22sl 2 2 5 9 1 9J Also obtain a solutionfor a righthandside vector that is the sum of the rows of [A].it can be used to determinea solution fbr a rightvector handside vector {b} in a mannersimilar to LU factorization. i t * u l .17) l u l { x }: l d } lO.l4) to seeif theirproductyields originalmatrix [Al..the answerwill be a vector of ones.4.chol (x) where y is an uppertriangularmatrix so that t/' * t/ = r.FACTORIZATION I 0. the Choleskyfactorizationyields r 1 JJq4q t6 I 11711 11 457661  tul :  L 4. 1 l o l o l Thus. : @ : 6 .l MATTAB Funcfion: cuot (r0. Note that for this case. an intermediate is created solving by {d} IUlr {d} : {b} Then. (10. fo . we looked at in the EXAMPLE Cholesky Foctorizotionwith MATLAB I O.9165 4 .the final solutioncan be obtained solving by (10.3.r8) the It MATLAB has a builtin function chol that generates Choleskyfactorization. hasthe generalsyntax. This is leflforan exercise. 231 :10. The following exampleshowshow it can be employedto generate both the factorizationand a solutionfor the samematrix that previousexample.15366) 225.
1101 We can test that this is correct by computing the original matrix as >> U' *IJ 6.0000 55.10. 10.mul minethat I 10.5 6.0000 15. 15 55 225.0000 1. 10.0000 1. can provide a simplifid we t^.r1 3xr xrj r. and( versionof 10. A righthandside vector that is the sum of the rows of [A] can be generatedas >> b = fsum(A(1.0000 225. sum(A(3..l Dete of equatio tion.4 MATTABIEFTDIVISION We previouslyintroducedleft division without any explanation how it works.Now thu of we have some backgroundon matrix solution techniques. 55 225 919). :))/' sum\tt(2.0000 979.0000 55.45...3 Use system ac 10.0000 To generate the solution. . we flrst conrpute >> d = A'\b d= 3r.2 Use Eqs(10.1813 0 22 .0998 6.0000 225.3 thesystem lblr : I O.1101 And then use this result ti'r compute the solution >>x=A\y l0.:)ll 16 295 7259 Next. r23t 4.:)) .5Solv factorizat 2xr 3xr  .0000 55.246 TUFACTORIZATION Solution.0000 Then.0269 25.0000 15.31 2[).91{. the Cholesky tactorization can be computed with >> U = chol(A) u= 2 .^:^ti ^^ ^f :*^^^ri.4495 0 0 6 . :'> A = The matrix is entered in standard fashion as f 6 l5 55.4Use Prob.
MATLAB invokes a When we implement left division with the backslash MATLAB examinesthe algorithm to obtain a solution. 2 : rhe 10.'a general triangular factorizationis computed by Gausselimination with partial pivoting and the solution obtainedwith substitution.back and forSome of the techniques availablefbr such systems.(b) triangutar(or easily transformed that are techniques the solution is obtainedwith the efTicient of thesecasesare detected. MAILAB checks to see whether [A] is in a format where a solution can be obtained without full Gausselimination. [A] in to lU Use factorization solvethe systemof equations Also solve 10. (c) Employ the resultsof the factorizationIL4 to vector.8 and use the builrin function is working Ibf = Lt2 18 . Test your function by using it to solve the systemin Prob. 10.xt: .6) Solvethe following system of equations using LU with partial pivoting: 2x16x2. dethe velop a function that is passed squarematrix and returns the triangular matrices [Ll and [U].13:38 3.6 Developyour own Mfile to determine LU factoization of a squarematrix without pafiial pivoting. ln essence. Showall the stepsin the computation.4by substituting results and l Ll yieldslA l. If none of thesesimplified solutionsare possibleand the matrix is square.: 61 .6rz + 2x'. 2 It should be noted that in the event that solutionis obtainedwith an approach [Al is not square.2 1.6). 10. include bandedsolvers. (10. svstem an alternativerishthandside vector for }:t*l tl ilill{i: (b) Verify your hand calculation with the builtin cnol function.r1 r2*7x3 : 34 Rr.tl (a) Perform a Choleskyfactorizationof the fbllowing b1 system hand: symmetric 27 l2rz. and Usenaive Gauss elimination to factor the following to according the descriptionin Section 1 0 . highly sophisticated structureof the coefficient matrix and then implementsan optimal method to obtain the solution.a simplified overview can be outlined. develop a function that is passedthe symmetric matrix and returnsthe matrix [tIl.7 Confirm the validity of the Cholesky factorizationof into Eq.Although the detailsof the algorithmarebeyondour scope. determinethe solutionfor the righthandside 10. Test your function by using it to solve the systemin Prob.3.l Determine total flops as a function of the number equations the (a) factorization.5 I x z + 5 a : . 5 multiplythe resulting [L] and [{l matricesto deterthat is produced. First. (10. )O . 10. verify that the productof l L/17 l0. That is. Q PROBTEMS the l0.PROBLEMS 247 operator. or (c) symmetric. These include systemsthat are (a) sparseand into triangularform).a leastsquares c allecJ R .3.fac t or i zati on.Conlirm that your f'unctionis working properlyby verifying that [L][Ul : tA] and by using the builrin function 1u. That is.8)follow from Eq.14)to the Example10. and Choleskyfactorization.(b) forward substitun for (c) phases the lU factorization of and back substitution of Gauss elimination Usethe rules of matrix multiplication to prove that (10.9 Develop your own Mfile to determinethe Cholesky factorizationof a symmetric matrix without pivoting.7) (10. : . L )v. ward substitution.If any banded.
16 ) ? L.3333 ^.lrr*l 3. (10. 3 6 3 6 .j (b) Employ the result of (a) to compute the determinant.tl 2) 1().0 .3333 .redeterminant and (b) solve [Al{r} : {b} with {b]r: Lo o 4l 0l Do your results make sensein terms of Eqs.248 IU FACTORIZATION factorizationto determineIU] sothat 10.15) and 0 0 .. r r : .r0 44 .6667 I I L 0.2 x 2 * . (c) Repeat(a) and (b) using MAILAB.U (a) Determine the LU factorizationwithout pivoting by hand for the fbllowing matrix and check your resultsby validating that [L][U] : [A.10 Solve the following set of equationswith LU factorization: 3 x 1.13UseCholesky 10.1 0 Z.rt]_6xz4x7: .2 t 2 * 5 .2 6 44 LA): W)rlul: [2 lr Lo r 1 0l 2 . ] ' [3 2 1. lAl:tLlfUl: [1  0.12 Use the following LU factorizalion to (a) compute tl.6364) L . 10.s ol 1l [8 2 7 2l l3 L2 r e_.26). IAl:10 o [e 2.x 1.14 Computethe Choleskyfactorization of 10. 1 3 : .
2). the Knowing how to use norms to computethe matrix conditionnumber. Understanding meaningof matrix andvectornormsandhow they arecomputed. how the magnitudeof the conditionnumbercan be usedto Understanding estimatethe precisionof solutionsof linear algebraicequations.l.Motrix Inverse ond Condition CHAPTER OBJECTIVES The primary objectiveof this chapteris to show how to computethe matrix inverse and to illustratehow it can be usedto analyzecomplex linear systems that occur in engineeringand science.': [ A ] . Then we will explore how it can be usedfor engineering analysis.r t A l t / l (r1. I l. addition.a methodto assess matrix solution'ssensitivity In a to roundofTerror is described. we introducedthe notion that if a matrix [A] is square. For example. Specificobjectivesand topicscoveredare o r r o r Knowing how to determinethe matrix inversein an efficient mannerbasedon lU factorization. Understanding how the matrix inversecan be usedto assess stimulusresponse characteristics engineering of systems. called the inverseof [A].1. thereis anothermatrix [A]r .if the righthandside constant . t. for which : t A l t A l .1) Now we will focus on how the inversecan be computednumerically.l THEMATRIXINVERSE In our discussionof matrix operations(Section8.l Colculoting the Inverse The inversecan be computedin a columnbycolumn fashionby generating solutionswith unit vectorsas the righthandside constants.
0 .r}r : 10. The tirst column of the matrix inversecan be determined perfbrmingthe forwardsubstitution solution procedurewith a unit vector (with I in the first row)asthe righthandside vector. o r o . J Io or 02 [ r 0.11249 for which is the first column of the u.10091. t . to I E X A M P L EL l MqtrixInversion Problem Stotement. Employ LU factorizationto determinethe matrix inverse the for from Exaurple10. 0 0 3 3 .027130J t I o o'l by Solution.03333 0.l: system or o)l I l t""l Al:10. 2 9 3 3 3 3 tul:10 1 1 0 .1} the resultingsolutionwill be the first column of the matrix inverse.Thus.3 1 10.o .Similarly.[10. o o s r so o l I nntooR n n I .ratrix inverse: o f o. o r 2[ ' ..2 10 _. 2 e 3 3 3l. Thisvector and solvedwith forward substitr. the resultwill be the second The bestway to implementsucha calculationis with LU factorization.1333 1 I lo [orooeJ 0.3]): [r*0.0.ui] {l}:{i} [: l0 l[. it is ideal fbr evaluating multiple unitvecton the needed computethe inverse.00518 0.3 0. the lower triangularsystemcan be setup as (recallEq. [0.2 I 1 0l ttl:10.0333333 l0 L 0. Recallthat one of the greatstrengths LU factorization that it providesa very efficientmeans evaluof is to ate multiple righrhandsidevectors.if a unitvector with a I at the second row'is used [b]:{ tol columnof the matrix inverse.l1 : l : l 1 ..] 7 .1000000.r 0. 0 0 3 3 30 .8]) 0. . 0 1 2_ j 0 0 L0 [r o.01008j which can be solvedby back substitution {.zzz+s ol l A l ' : l .1 Recall that the lactorizationresultedin the following lower and uppertriangularmatrices: 7 .1.h t\ t ' s) f0l l} {il.250 MATRIX INVERSE AND CONDITION has a I in the tirst positionand zeroselsewhere.1 7 _0.rtion {d}r : ll for can then be used as the righthandside of the upper triangularsystem(recallEq. Thus.
they arethe massof chemicalin each vertical forcesin eachmember.142e03 0 with {b}/: l0 0 1l to solve fbr Finally. therefore.In a fbrce balanceof a structure. For a force balanceon a structure. potentialis conserved.For many cases.of the exactform dealt with in this chapter: [A]{x}: {b} (11.00s18 0.010080.THEMATRIX INVERSE To determinethe secondcolumn.004183  L 0. .0021100. coupled. For example.027r 300 'lllil :{:} This can be solvedfor irl). (10. stateor response. The mathematical expressionof these laws scienceare derived from conservation is some form of balanceequationto ensurethat a particularpropertymass.01008 0.oo4s44 0.which is the secondcolumn of the matrix inverse: 10.100000 0.4) have a definite physical interprethe tation.thesesystemsare linear and.0998801 t 03324so.resultingin a set of equationsdefining the behavior of the property for the entire system.002111. terms of Eq.2 StimulusResponse Computotions and As discussed PT 3. Eq. vector {b} containsthose elementsof the balancethat are indeThe righthandside pendentof behavior of the systemthat is.they In represent the or externalstimuli that drive the system.the propertiesmight be the mass in each reactor of a would yield similar examples.zzz+e f 0. {r }7 : 10.006798l that Thevalidity thisresult bechecked verifying [A][A] ' : Ul.0333333 L 0.in that eachequationmay include one or more of the variables or from the other equations. which we are trying to reactor.For a mass balance.142903 0. heat.they represent system's the determine. force. the sameprocedures can be implemented .0027r0 ol o. fbr balanceequations. many problems.8) is formulatedas [ro l  0. of can by  1. chemicalprocess.oo4s44 0.4) Now.3)to determine {r}' : 0.1.In either case. (10. ( I 1.00sr8 o.006798 0.the momentum.1429030.004l83 0.004944 [Al ':   0_j L 0.For the massbalance. Other fields of engineering and science A singlebalanceequationcan be written for eachpart of the system. they are constants.electrostatic propertiesmight be horizontalor vertical components the forcesacting on eachnode of of the structure.the elementsof {x} are the levels of the property being balancedfor they represent horizontaland the eachpart of the system.t. and the resultsare usedwith Eq.099881 which is thefinal columnof thematrixinverse: [A]' :  0.Theseequations are interrelated. many of the linear systems equations in of arisingin engineering laws.
coefficientour the is a p r o p o r t i o n a l i t y c o r l s t a n t t h a t g i v e s t h e v a l ud u e. the responses be corrrputed individuallyand the resultssummed obtainatotal can to response.using the matrix inverseyielclsatparticularlyinteresting result.(11.I L t. on tilr  L 0 I . we used MATLAB to solve this system for the vertical positionsof the jumpers(ther's).l systems. That is.asidetiom providing a solution. eachof its elementsrepresents response asingle the of part of the system a unit stimulus any otherpart of the system.r1 l. Consequently.cluetoaunitquantityofb. we set up a probleminvolvingrhree of individualssuspended vertically connected bungeecords. {l{. Even for sntal. Supe.4)..\'J : . f rt.I. r. In the present example. r: ]{1r} or (r'ecalling detlnitionof ntatrixmultiplication our from Section . we lind that the invertedmatrix itself'.toftheinvertedmatrixrepresentsthevalueofx. l. ( I I .rltositirn meansthat if a systemis subjectto severaldifTerent stimuli b's). roportionalin' meansthat multiplying the stimuli by a quantityresults the P in re. f rlt l /)r I (l)t l)l f l. Usine exanrple the structure.\'t : .r1. \ ' . there are a variety of ways to solveEq. lrl + L (lrt /tt lr l/rl l)t + Thus. sponse thosestimuli beingmultipliedby the sarne to quantity.l' of the matrix inversewouldrepresent the fbrce in member i due to a unit externalforce at node1. L {lt I /)l f {/l. such behavior individualstimulusresponse of interactions would not be intuitivelyobvious. to of Notice that theselbrmulationsare Iinearand. rr.'.xrrameter. can draw the general we conclusion theelement that a. MATLAB to compute matrixinverse use the and .252 INVERSE MATRIX AND CONDITION Finally.We deriveda s)'stem linear by of algebraicequations basecl fbrce balances eachjuniper.. superposition proportionand (the ality hold.illycontains the . the nratrix of coeflicients [lJ usui.. At the beginning chap. However. D.tr a u n i t l e v e l o f l r s .etiil ss8. exhas trernelyuseful properties. lr.I _ . Eq.2. As such.6 I In Example 8. EXAMPLE2 I I. the of elementrr.r thatexpres how the parts of fhe systernirte ract or are coupled.. ThLrs.rso roo o 0 r00 rsO s I 50 s0 I  I.4) miqht h reexpressed as : {s IInteractionsl{response} timuli} As we know from plevious chapters.. Therefore.2) 8.The fbrmal solution can be expressed as h : l: [ A l . which are retlected in the coefficients of no' trnda. 8. A n o l y z i n g t h e B u n g e eJ u m p e rP r o b l e m Problem Stotement. theretbre. respectively. T h i s r e s u l t r s eof o 1 independentof the eftects b2 and 6: on ..the matrix inverseprovidesa powerful technique understanding interrelation1br the shipsof component partsof complicated systems.
0300 0. First. The 0.j : / .respectively. J m : + + II. and third jumpers.0200 0.02m elongationof the first jumper makes sensebecause the first cord is subjectto an extra I N regardless whetherthe force is appliedto the first or of secondjumper. 100 150 50. This makessense.. of A r . and 20 N were appliedto the first.I I . Invert the scaledmatrix and if there are elementsof [A]  that are severalordersof magniq\/{fcm . 3 r r A F r+ k r j a f ' 2 + k . 2 E R R O A N A L Y S IA N D S Y S T E M O N D I T I O N R S C 253 Solution.0200 0. the in position (in meters)of jumper I due to a unit changein fbrce (in Newtons) applied to Jumper/. second.03 m because along with the first cord. 0 5 ( 2 0 ) 2 .02 m.0500 Each elementof the inverse. for the secondjumper the elongationis now 0. inversealsoprovidesa meansto the discernwhether systemsare illconditioned.2 ERROR ANALYSIS AND SYSTEM CONDITION Aside tiom its engineering and scientificapplications.O 50 501. 50.the numbersin the secondcolumn (j :2) indicatethat applying a force jumper would move the first jumper down by 0. As expected.0200 0.the third jumper showsthe identicaltranslationas the second jumper as thereis no additionalforce on the third cord that connects them. However.0300 0.now because the additional when the force was appliedto the second of elongationof the third cord. 0 2 ( 1 0 ) 0 . This can be done simply by using the appropriate elements the third row of the inverseto compute.Three direct methodscan be devisedfor this purpose: l.And of course. observethat the numbersin the first column ("1: l) indicatethat the positionof all threejumpers would increaseby 0. the secondcord alsoelongates due to the additionalforce. but the second of I N to the second and third by 0.02 m if the force on the first jumper was increased by 1 N.j:3) indicatesthat applying a force of I N to the jumpers moving the samedistances occured third jumper resultsin the first and second as jumper. Superposition and proportionalitycan be demonstrated using the inverseto deterby mine how much fartherthe third jumper would move downwardif additionalforcesof 10. 0 3 ( 5 0 ) 0 . Scalethe matrix of coefficients[A] so that the largestelementin eachrow is l.03 m.0200 0. ln contrast.0300 0. the third jumper is moved fartherdownward. r A F r : 0 . However.the third column (. I of the invertedmatrix represents vertical change k. The inversecan then be computedas >> KI Kf = = inv(K) 0. Start up MAILAB and enterthe coefficienrmatrix: >> K t150 100 0. because additionalforce would only elongatethe first cord the by that amount.0200 0.
b.Attemptsto formulate such a matrix condition numberare basedon the mathematical conceDt the norm. it againindicatesthat the systemis illconditioned.I I Grophicol depictton o vector Euclideon of in spoce . where the nomenclaturell F ll.it would be pret'erable obtail to a singlenumberthat could serveas an indicatorof the problem. Multiply the inverseby the original coefficientmatrix and assess whethertheresultis closeto the identity matrix. A simple exampleis a vectorin threedimensional (Fig.rn a Euclidean norm Similarly.1 Vector qnd Motrix Norms Anonn is a realvaluedfunction that providesa measure the size or "length"of multi..l) thatcan Euclideanspace be represented as lFl:la b cl where c. If not. + f of Ln. If not. for an ndimensional vector lX. c)can besimpty computedas lFll. . lengthof The this vectorthat is. 3.respectively. Invert the invertedmatrix and assess whetherthe resultis sufficientlyclose the to orig. indicatet that this length is referred to as the Euclideannorn l"r1 x2 j. inal coefficientmatrix. : {/o\ h. i:r i FIGURE I. the distancefrom the coordinate(0. l1.l : would be computedas llXll": if \ t. and c are the distances along the r. 1. of componentmathenaticalentitiessuchas vectorsand matrices.254 MATRIX INVERSE AND CONDITION 2. 0. Although thesemethodscan indicateillconditioning. b. 0) to (a. it indicatesillconditioning. of  1.and z axes.2..
\r..the important For point is that the llAll2. example.{l tinll/  \.: L ' 'l. areidentical vectors. providesthe tightestlreAsureof size (Ortega.llX ll.. : . and the largestof thesesummations taken as the norm..l ". for Otherimportant examples (p : 1) are rYr . or spectralnorm. In AppendixA. \ll l /iX l ll t .r i=  whichrepresents normasthesumof theabsolute the values theelements."* the largest is eigenvalue [A]r[A].5).Whereasthe Frobeniusnorm llAllf can be easily deterrnined by Eq. of Another the is maximummagnitude or uniformvector (p : oo). ( t I .1 u ' il That is.r. norm llXll: max. 11.tot'tn.1972).. l. 1/'l I / We can seethat the Euclidean norm and the 2 norm.l. as in ll... in contrastto vectors. is the mirrimum nonl and. we will learnmore about of eigenvalues. can for For llAll : . Usinga similarapproach.\/J /rt "? I (il..2. the time being.thereale alternatives calledp norms that can be represented generallyby / .2 Motrix ConditionNumber Now that we have introducedthe conceptof the norm.For to vectors. it provides singlevalueto quantifythe "size" of [A].7l 1:1 It should be noted that.the matrix 2 norm llA llz is calculated as l lA l l : : ( / r n ' " * ) ' ' " where4. a summationof the absolutevaluesof the coetlicientsis performedfor eachcolumn. norms be developed matrices.max). resultine in a unifbrmmatrix or rowsumnorm: l l A l l .s) which is given a specialnamethe Frobenius notm. we can use it to define C o n d l A l: l l A l l ' l l l .aua^ f t n  l.\t t 'tt l \. As with the other vector norms. This is called the columnis sum . a It slrouldbe notedthat therearealternatives the EuclideanandFrobeniusnorms.the 2 norm and the Frobeniusnorm for a matrix are not the same..ERROR ANALYSIS AND SYSTEM CONDITION The conceptcan be extendedfurther to a matrix [A]. .1 r li.therefore. A similar determinationcan be made for the rows.' l l .ril whichdefines normastheelement the with thelargest etbsolute vallle.
. This is also reflected in its rowsum norrn.35.1667. I 5l so in row Solution.1 t A' l : l l{t tl lr i i' L3 I I ll EXAM I\ rI I .if the coefficientsof [A] are known to tdigit precision(i.  + 4 i nll .:2. It can be shown (Ralstonand Rabinowitz..35 The inverseof the scaledmatrix can be computedas lAll:ltr' r b bul )  _ . Thus.4ll:l+.the matrix canbe normalized thatthe maximumelement each is1: rAl: fr 1  Ll il iJ 31 +i Summing each of the rows gives L833. the solution[X] may be valid to only r .3 I M o i r i x C o n d i t i o nE v o l u o t i o n illconditioned. l l. exof For 0n arnple.which is notoriously sentedgenerallyas [r l. the third row hasthe Iargest surnand therowsumnorm is ll. EXAMPLE 1.+.+r I I I ll: : : : l1 l Lfl nrl Use the rowsurnnorm to estimate matrix conditionnumberfor the 3 x 3 Hilbertmatrix. bereprecan Problem Stotement. this numberwill be greaterthan or equalto l. The Hilbert matrix.1978: Gerald and Wheatlev. the relative error of the norm of the computedsolutioncan be as large astherelative error of the norm of the coefficients [A] multiplied by the conditionnumber.cdigits (roundingelrorsry l0' ' ). e0 30 60 l L I g 18 10l l Note that the elementsof this natrix are larger than the original matrix.ll^Ail = condlAl il"il Mil That is.roundingerrorsare the order of 10r) and Cond[Al : 10'. and 2..l I 1l 'i I ltl tJ .INVERSE MATRIX AND CONDITION where Cond[A] is called the nntix conditiort number Note that for a matrix [A].e.which is comoutedas . First. t l  I I  .. the t. 2.1989)that l^xll .
65.2 ERROR as Thus. i n f ) 2.35(192\: 451.2 257 that the system The fact that the condition number is much greaterthan unity suggests c can be quantifiedby calculating .3. Use MAILAB to evaluateboth the norms and condition numbers in for the scaledHilbert matrix previously analyzed Example 11.A]: 2.P) set it Also. z .notethatif p is omitted.3 Norms ond Condition Number in MATLAB MATLAB has builtin functionsto computeboth norms and conditionnumbers: >> norm (X.enterthe matrix: >> A l\ \/2 I/3. to inf . I 4 tl sl (c) As in Example I 1. However. (b) Also compute (p (p the Frobenius :' fro' ) andthe spectral : 2)conditionnumbers. first computethe rowsum versions(p : inf ).2. the condition numbercan be calculated Cond[.p) the where x is the vector or matrix and p designates type of norm or condition number(1 .log The extentofthe illconditioning is illconditioned.2 always overpredict the actual error.4 MotrixCondition with Evoluotion MATLAB EXAMPTE Problem Siotement. Notethatthe cond functionis equivalent >> norm(X. is automatically to 2' I 1.3: [' tAr:lr I +  lr L' t_l l^ i2 r rl l .I 2/3 Ii2. :2. they errors. Solution: (a) First.  1. Then. or ' f ro').3504 > > c o n d (A . Note that such estimatesalnrost are usefulin alertingyou to the possibilitythat roundoff elrors may be significant.AND SYSTEM CONDITION ANALYSIS I 1._ cond{X. the rowsum norm and conditionnumbercan be computedas > > n o r m (A .L 3/4 3/51. p) ano . Hence.p) * norm(inv(X) .the last threesignificantdigits of the solutioncould exhibit rounding 451. r n f ) .
rooms I and 2 gain carbon monoxide from air intakes that unfortunately arepositioned alongside the freeway. Overheod orrowsrepresent whereos Mowoyorrows ihe represent diffusive mixing. In addition.3.3503 "'i "i.. As the name implies. indoor air pollution deals with air contamination Bockground. respectively. the restaurantserving areaconsistsof two rooms for smoken and kids and one elongatedroom.Supposethat you are studying 'N Guzzle. a truckstop restaurantlocatedadjacent the ventilation system for Bubba's Gas to an eightlane freeway. As depicted in Fig. in enclosed spacessuch as homes. F I G U R EI I . onewoy The volumetric oirflows.= 150m3/hr i I Qt = 50 m3lhr ct = 2 mglm3 Qa: 1oo I 25 m3lht 4 c+ t %_l_l El I oi I 2 (Kids'section) Q":20Q m 3lhr 1 (Smoking ection) s > 25 m3/hr 3 cu = 2 mglm J Smoker load (1000mg/hr) ) I G r i l ll o a d . ' fro' ) 368. I L2.'j\'. Room I and section 3 have sourcesof carbon monoxide from smokers and a faulty grill. and work areas.0866 >> cond (A) 366. (b) The condition numbers based on the Frobenius and spectral norms are >> cond (A. 0..258 MATRIX INVERSE AND CONDITION These results con'espond to those that were calculated by hand in Example 11. offices. smoker grillloods The ond odd corbon monoxide moss thesystem negllgible fo but oirflow.1 Il. 2 view of roomsin o restouront.
For example.determinewhat percentof the calbon monoxidein the kids' sectionis (2) due to (l) the smokers. Write steadystate mass balancesfor each room and solve the resulting linear algebraic equationsfor the concentration carbonmonoxide in eachroom.l .000r3193 n nnn 6aaG6 0. compute imthe provementin the kids' sectionconcentration the carbonmonoxide load is decreased if by banning smoking and fixing the grill.ct. }:'tl l:t* !i.0034483 0.00055L12 0. the grill.CASESTUDY trli':'l). continued l .0449962 0.cs) . to Steadystate mass balances can be written for eachroom. Note that we use the "short g" format in order to obtain five significant digits of precision: >> fcrrrrat short g >> A= 1225 A 25 0 0 175 0 125 225 0 274 5C 25 254 215). For example.432 6e005 0.0049655 n nnt9) . analyzehow the concentration the kids' in area would changeif a screenis constructed that the mixing between areas and 4 is 2 so decreased 5 mj/hr.j lT  0. Finally. . (Q' .Qa)c'+ Q. . genof erate the matrix inverse and use it to analyze how the various sourcesaffect the kids' room.0034483 0.c:) * Ey(!+ ..In addition.. i .c+) * Ezq(cz c+)' Qoc+ yields the final systemof equation: Substitutingthe parameters i.Qoct 0 : Qoc3* Ez+Gt.0062069 0. and (3) the intakevents.the Solution. First.0034483 0. In addition.(Outflow)f (Load) *(lnflow) (Mixing) Similar balances can be written for the other rooms: 0 : Qt.00010728 0.r]{. .cz'l E2a@a c2) + 0 : wern * Qnct I EnGt .* QncoQocr *f13(cac1) .0049555 ^ nAtQ)aC I. 0 >> A L=f nV (A.00096552 . we can compute the inverse.i MATLAB can be used to generate the solution. balancefor the smoking section(room l) is 0:lVrmore.6 0.
: 0.0:j ".'.. L6.smokers Wr*ok* : 0.? 1L345 x l00vo : 2'7.j:continued The solution can then be generatedas >> b=11400 r00 2000 0l . Although the foregoing is interesting. . d ? 4/l'9.S. ok".897 16. aur LW*lr:0. the elementsof the matrix inverse can be used to determinethe percentof the carbonmonoxide in the kids' sectiondue to eachsource: The smokers: : uitr c2. For example. superpositionholds and the results can be determined individually and summed: Lc2: a.moke.891 Vo".. Room 3 is sobad becausenot only does it get the load from the faulty grill.0062069(50)2 c2. . .. 8 9 6 6 : _ 1 0 .. $ { .897 12.0034483(2000) + _ 3 . 3 4 5 : il. The inverse can also be employed to determine the impact of proposedremediessuch as banning smoking and fixing the grill. Becausethe model is linear.15... These results take place because(a) carbon monoxide is conservative and (b) the only air exhaustsare out of sections2 and 4 (Q. 4 4 8 3_ 6 .345 The faulty grill is clearly the most significant source.0034483(2000) 6.snl: arytWsrin: 0..62069: 2 * ) Vr '. we get the surprising result that the smoking section has the lowest carbon monoxidelevels! The highestconcentrations occur in rooms 3 and4 with section2having an intermediate level.tt Lw.4483 1 : 7o.37931 0.0034483(1000) + 0. but it also receives the effluent from room l.877o Check useaI .I I l0 3 6..i. the real power of linear systems comesfron using the elementsof the matrix inverse to understandhow the parts of the systeminteract..irrrakesarl Q.93Vo The grill: : cz.345 x l00%o:55.0034483( 000) : 3.t: The intakes: : (200)2 + 0. >> c=Af *b c= 8.483 Thus. : '"snrr ' 12..0034483 : 1.260 MATRIX INVERSE AND CONDITION '' i'' {.0996 1) ?/q.c' + arrt Qt ct.. and 0a).. " .
The result by would bring the kids'room concentrationto 12.t2*'7x. ll. r 2 * 5 x . : 34 Therefore.if the mixing betweenthe kids' areaand to zone 4 is decreased.rr 2.6 1 .345 . 5 yourresults verifyingthat [A][A]r : [1] Donot by aplv0tlng stratcgy. This makes because the absence sense.rl PROBTEMS ii i li continued Note that the samecomputation would be made in MATLAB as >> AI \2.the only sources the air inare takeswhich areat 2 mg/m3.rj : 2'7 .. s e f o l to w i n g s y s t e m : l l . PROB[EMS D e t e r m i n e t h e n r a t r i x i n v e r .3 r 16 . j : . matrix is changed the L 2s 0 f225 0 lt Cl 0 rss 0 r0slJ  22s 0 21s s0 I I  5 250 0 2s5Jt C4 :{*}*l l The resultsfor this caseinvolve a new solution.3 The following system of equationsis dcsignedto (the c's in g/m3) in a series of determine concentrations . the resultis Cl c2 C4 l:{r*tr1 8rr+ x22x3:/Q 2x16x2. Because the foregoingcalculations all involved changingthe fbrcing functions. in ofthe smokerand grill loads.265 mp/m3. 5 r 1* .345 Implementingboth remedieswould reducethe concentration 10.345mg/m3. 1 2 * 2 r : : .2 1 .However.Using MATLAB. 2 D e t e l n r i n e t h e m a t l i x i n v e r s e f b r t h e f o l l o w i n g s y s t e m l he F l0.10.xr:38 3r1 .I)* (1000)+AI (2.3\* (2000) 10.rz. this remedy would only improve the kids' area concentration by a paltry 0.345 :2 mg/mt.it was not necessary recomputethe solution.
Use the matrix inverscto dcterrnine fbrce in Iinearalgehraic equalions the I thc thrcc nrembers(. In otlrerwords. andc'.f o r 1 l couplcd reactors a function of thc antountof nrassinput tcr as each reactor(the righrhand sidesin g/day): ll.ector consists ofthesum. rl ['i r 'I ll lrl .ral and Frobenius l 5 c 1.l nurnbcr and how many suspcctdigits would begenuCompute the condition rrumberbasccl the rowsur.9) wnere ll.3.n on norrn.rcspeclively? Vandcnrondcmatrix {sce Prob.252 0 0 l l . Pl l.252 0 12.3 c 2cl :3800 conclition nur. 8. i n P r o b .5 36 4e 61 8r .l Determinelhc rni"rtrix inversefbr thc systcnrdescribcd dinrensional : :5. 8 .5 if the inflow concentrations of of are ll. 2000 lb and ahorizontalload t3. x z : 2 . Onc such case is the ber fbr thc fbllowing rnatrix: Vantlcnnotttle rzalnr.and Havasu. . U s e t h e m a t r i x i n v c r s et o d e t c r m i n e h e fr : 4.797 P Belbre detcrmining thc norms.r'r.r' t . 6 D c t e l m i n el A l l r . l l . c2.l l A l l .2and I 1.xs : 3.r+ 10.ll Use MATLAB to deternline spectfalconditionnurn.nbcrs. u'hich has thc fbllou. l 0 ..422 t2.9 Bcsidesthe Hilbert matrix. [ ' 5 6ll 14 Lt8eJ ( b ) R c p e a( a ) b u t c h a n c e .owerColorado : rescrvoirs shownin Fie.. n l Com. . as changedto c1l : 20 and ci.mationof the coefTicicnts its low. r := 7 . w (b] Usc MATLAB to cornputethc spcct. c e n l r u t i ou l r c a c t o r .l3 (a) Dcternrine rratrix invcrseandcondition thc num.I 1. to t3. a n dl l A l l . 5 .Mead.6.i t : * l 2 c .. s l i s h t l v o 9 . ll. 1 . 500 lb is appliecl nodc 3.4 . (a) Dctcrnrinc lnatri. l l R e p e a tP r o b . Do not nornralize systcm: rcsulting chloriclecoltcentratious Lakcs Powell.pal'c thc resultins crrors with those cxpectcdbased the in on duced if thc rate of mass input lo reactorsI and 2 is conditionnumbcr.\inverse.l: 2 3 5 0 nutnbertbr a I0dinrcnsiontrl Hilbclt nratrix.1.122 0 0 0 of F3. atedby solvingthis systcnr. ll.377 n. scale thc rnatrix by making the nraxirnum clementin eachrow equalto one.solve to for thc casewherc all thc unknownsshouldbe exactlyone. (d) How much will thc concentraticrn reactor 3 be rc.chloride to eacho1thc thc fbr"rr lakesalld {rl. I: : 7. l l .= fie ber for the fbllowinc system. :1 2 6 9 1 3 c r+ l 8 c : 6r'. the for Mohave.12 The l. computethe condition L2.7 Determincthe Frobenius arrdrorvsumnorms fitr the systems Probs.262 MATRIX INVERSE AND CONDITION (a) Dctermine condifionnumberbased therowsum the on n o r m f o r t h ec a s e h e r e . that are inherentlyillconditioned. 1 s 16 r. 12. there arc olher matrices ll. {b} in lnust be increascd inc[rcca l0 g/m3rise in the con.3'71 0 12.. in where the righthandsidc vector consistsof the loadingsof l l. l4 e 16 2s 36 4el (b) How much must the loading to Lake Powellbereduced I 2s 36 1e 641 firr thc chlolide concentration Lake Havasu be75? of to l16 (c) Using the columnsumnorm.1 50. Mass balancescan be written fbr each reservoir.How many dig. respectively. way for r algebraic the coeff l'1.l o 0 t2.. and"16 t Rivcr consists a series tbur concentrationin reactor . ll. & and Fr) if thc vertical load at resuts: : node I is doubledto F1.10 Usc MATLAB to determine the spectralcondition 4. r l. thc (c) Detcrminchorv much the ratc of rnassinpLttto reactor3 elcnient ofthe righrhandside r. t a t . It = 2.l4 Po unique (z Such poll f(x): z [t rAt: I e I ls l L l. its ofprccision expected bc lostdueto illconditioningl are to the Deterrrrine solutionfor this systern thc case thc fbr where each {b) Usc the inverscto dctermine solution.Fr.l (a) Use the matrix invelse to solve tbr the concentrationsin [t e 16 2s 361 eachof thc fbur lakes. b u t f b r t h e c a s e o f a s i x .5 Dctermine matrix invcrselbr the system the and dc'scribed the lbllowing setof sirnultancous in Prob. :[ll. reducedby 500 and 250 g/day. a n d . 11.ing tbrm: :tl_ il where th.
into and (500. In addition. Suppose Polynomial interpolationconsistsof determiningthe (n .1)thorderpolynomial that fits n data points.l4) to yield a systemof five equations with five to thep's are constant coefficients. equations that we can solve simultaneouslyfor that we want to determine the coefficients.0.675).0.0.I2 P TheLower River Colorodo coefficientsof the fourthorderpolynomialf(r) : p6" * pzx3t pzx2+ p$ + psthat passes throughthe following five (300. (Pl1.(250. polynomials havethe generalform. . + pn rx * pn Eq.525). 0.457).14) l p2x"2+ .616). Use this approach solve for the coeffor computingthe coefficients is to generate n linear ficients. (400.Each of thesepairs can be substituted (Pl 1.0.A straightforward unknowns(thep's).746). determineand interpret the condition number. points:(200.PROBLEMS Upper Colorado River Lake Powell Lake Havasu FIGURE I I. .
Because present part of the book dealswith a similar problemobtaining the the valuesthat simultaneously satistya setol'equationswe might suspect that suchapproximate methodscould be useful in this context. Specificobjectivesand topicscoveredare ' ' ' ' Understanding differencebetweenthe GaussSeidel Jacobimethods.5 and 6. Understanding how to solve systems nonlinearequations of with successive substitutionand NewtonRaphson.we will presentapproaches for solving both linear and nonlinearsimultaneous equations.. Assumethat we are given a setof il equations: lAl{"r} : {b} Suppose that for conciseness limit ourselves a 3 x 3 setof equations.the first equationcan be solvedfor.l IINEAR SYSTEMS: GAUSSSEIDEL The GaussSeidel methodis the most commonly used iterativemethod for solving lineu algebraicequations.'.r1. thedragonal we to If elementsare all nonzero. In this chapter. Such approaches similar to the techniques developed are we to I I obtain the roots of a single equationin Chaps. the and Knowing how to assess diagonaldominanceand knowing what it means. Recognizinghow relaxationcan be usedto improve the convergence iterative of methods. Iterotive Methods CHAPTER OBJECTIVES The primary objectiveof this chapteris to acquaintyou with iterativernethods for solving simuitaneous equations.'" t. EXAM terative or approximatemethods provide an alternativeto the eliminationmethodi I describedto this point.andthe for . Those approaches consistedof guessinga value and then using a systematic method to obtain a refined estimate the 0f root. l2. the second x2.
1. < x 100% r.3 0.85 0. a22 (12.1c)to calculatea new estimate x3.3) By assuming that.solveeachof the equations its unknownon the diagonal: for 7. Then for for we return to the first equationand repeatthe entire procedureuntil our solutionconverges closely enoughto the true values. l ) .lrr t 0.Convergence be checkedusing the criterion that for can . Dr4ttX. rll rr Solution.r 1 1 1.initial guesses must be made for the x's.3xj: 19.r(0)+0.3x l 0.r20.5 i ).l :XAMPLE .l l2 GoussSeidel ethod M ProblemStqtemeni.': ll lr. (12..12.1b)to compute'a of new is value for x2. 3 1 10 .19. 2 t 2 + l 0 r 3 71. (Er2.r2f0.2) (E12.3.f1 . which can be used to calculatea new value for r1 : bt latr.616661 .1 LINEAR SYSTEMS: GAUSSSEIDEL third for x.2(0) :2. A simple approach is to assumethat they are all zero.Eq.1a).1. The process repeated Eq.4.3 0 .2x3: 1.lli' Jri r/'I e. First. ! '10 1 : 3 . To start the solution process.t.1.I are the presentand previousiterations. (12.2x2 r ( Er 2 r .1) be used compute can to ^t  7. and (E12.llr) btayrla:txl 1133 (12. Use the GaussSeidel methodto obtainthe solutionfor 3xr0. t:l clrr{l . i tl D) AtqXi d)t. (12.rc) whereT and j .85+0.lxr* 7.r.1a) atl ..0. Then we substitute this new valueof r1 alongwith the previous guess zeroforrr into Eq.Thesezeroscan be substituted into Eq.! (t2.r to yield 265 xi .1.85+0.4 Note that the solutionis ixlr : 3 2.r2 x3 arezero.1r20.1yl .
I . 0 0 s 6 1 0 .GaussSeidel! cases Fig. Although therearecefiain usuallymakesit the methodof preference' utilization of the bestavailableestimates .2(2. can be substituted Eq'(El2'l This value.85+ 0. Rather than using the latest available thistch utilizes a somewhatdifferent u a setof new .Consequently. appraisal ally provide a conservative that the result is kttown to at leastthe tolerancespecifiedby e'' ud method. therefore.( E 1 2 . 9 9 0 s 5 7 0 .alongwith the assumed to calculate . . .990s57 0. 1 .000291 l0 couldh The method is. we would not applied to improve the answers.0.191524) 7. iteration. 2 : l l 'o tcdo ' ' 3 : A ' 0 J 6 c / c ' o t e t h a t ' a s w o 8l/e usu fbrmulationssuchasEq' (12'2) the casewhen determiningroots of a singleequation.3(2.4 . in an actualproblem.Thus. altemativeapproach.2'616661 .266 METHODS ITERATIVE into valueof rr : 0.2(.616661 0.qe0ss7  N F o r x 2 a n c l ' r r . if the solutionis converging'th in the next equationto cleternrine Jttcobiiteratiwr' called An will be enrployed.1 9 . 3. 3 0 . 3 ( 7 .I) to compute forthenefl are not immediatelyusedbut ratherareretaiued they new valuesaregenerated.r(2.990551 3 .it is immediately for As eachnew r value is computecl the GaussSeiclel anotherr value. 1 ( 2 . true answerapriori. ( 12. .499625) 1 r.Thus.0 . 3 ( 0 ) +0  . bestavailableestimates r's. 3 ) y i e l d a 1  1 1.1 1qL\)L by The first iteration is completecl substitutingthe calculatedvaluesfor 'rt and'r: into to E q .1'0056 :2.00" : 2'990551 : 12. t h e e r r o r e s t i m a t o s i l t ' € € . methocland Jacobi iterationis depictedin betweenthe GaussSeidel The clifTerence wheretheJacobirnethodis useful. (12'2) provides means estitnate for x1: example.1 9 .) 7 (2.0.r's on the basisof a setof old x's.005610 )+ : l0 to is For the seconditeration.3 )+ :1.194524) 0.theyensun ofconvergence. 1 Q .2(2.1 4 q q 6 r 5 + ) . when they aremet. tactic. 6 1 6 6 6 1 ). niqueusesEq.the sameprocess repeated compLlte ^l  I 0) + 7.4.5(/'  2. 12.convergingon the true solution. Thus. '.Additional iterations th know However. theenorFr to a Eq..
1 r 1 ) / .)faa. a represents feasibleapproachto solve many problemsin engineeringand GaussSeidel science.r. . 2 . can be shownthat if the following conditionholds.Recall that iterationthat That is.because is designedfor linear Although the GaussSeiclel ability to convergeis much more predictablethan for fixedpoint iteration of systems.tt.(bt .ar.r.14)/azz .u27x. .S E I D E L 267 First iteration . 1 L I N E A R Y S T E M S :A U S S .Therefore. 3 ) i+i That is.t . . simple fixedpoint iteration was sometimes gressed..Fortunately. \ 1 \/ a .ttr)f ar.l:> ) l0.a. convergence guaranteed the condition is satisfied. r . although the method may sometilneswork if Eq. I to solvelbr the roots of a singleequation.l Convergence ond Diogonol Dominonce of methodis similar in spirit to the technique simplefixedpoint Note that the GaussSeidel wls usedin Section6.atzr2 * arrx)/atr x. t ap:t1)/a1z .a2trt .ir 1 xr: (b2 .l I (r 2 . as the iterationsprononconvergent.Such but not necessary This cliterionis sufficient are systenis said tobe tliugonalb dotninant. if is met.n .ttyx2)/a1 Second iteration i r : ( h r .x. (12.azrxt .r2)/ajz rr . I iferotive (o) ond between theGoussSeidel (b)theJocobi of C1opn. : I b2 .l.GaussSeidel will converge: II \i1t ul il lo.(b.c l . answermoved fartherand fartherfrom the correctresult.. .\fu' \: (bz a 3 1 x . : (bq  a31r. That is.many englneering and scientific problems of practical imporlance fuhlll this requirement. x. the absolutevalue of the diagonal coefficient in each of the equationsmust be in larger than the sum of the absolutevaluesof the other coelTicierrts the equation. .G S I 2 .its lt nonlinearequations. (b) @l I F I G U R E2 .otdepicrron thedifference o c i o q i m e t h o dfs rs o l v l n s l m uo n e o u sn e o r l g e b r o ie q u o t i o n s l2. the it methoclcan also diverge.ct.3) is not for convergence.rx.d: rrr a v .r tt.
l'*: lr. An Mfile to irnplernent (12.lt" + (l ). systemconvergeor to hasten a ltonconvergent For valuesof . the addedweight of /.1. pltible with MATLAB's ability to ) E q .if I is setata If ).rild (12.nl. : 1. (1 .3 Reloxolion to methodthat is designed a Relaxationrepresents slight modificationof the GaussSeidel thatvalue is usingEq.1).^.2. to the true solution but at too slow a rate.1} and anlal 0 [ :laztlttzz 0 tcl a..Thus. However.t osz L f lctr onlottf ay/a2 l 0 l Eq. 12.2 MATTAB Mfile: Gaussseidel in Before developing an algorithm... improve of to the is which is calledot'errelaratiort.. enhance iterations: average the resultsof the previousand the present of modifiedby a weiahted . .re to It is Tlris type of modification calledwtderrela.ration.t AT .268 METHODS ITERATIVE 12. After eachnew valueof r is compr"rted convergence. designed accelerate convelgence an alreadyconor ot'errelaxcfiion. from I to 2.a)is listedin Fig.l c l { : r } where : tfl:'. . In this inthat the new value is moving in the correctdirection thereis an implicit assumption stance. 1 a s bt Cltl b2 _ a12 Atl .0 all ll2l  4f  .. ( 12..':) {. SOR. extra weight is placed on the presentvalue. let us first recastGaussSeidel a form thiit is comThis is done by expresstttg pelfbrm matrix operations.5) a where ).* . previous reof value between0 and I . is The approach also calledsuccessive vergentsystem..t a.i.1.l ^r^' xi.. Hence.r. is intended toward the estimateby pushing it closer to the truth.n. ov ) _ .this type of rnodification.( 1 2 . is a weighting f'actorthat is assigned value between0 and2.l .). out by conversence dampening oscillations.].!!*uc* rl33 in concisely matrixform as can Noticethatthesolution be expressed { : r }: { d } . is typicallyemployed make sr"rlts.i) is equalto 0 and the resultis unmodified.* : ' U.  2. the result is a weightedaverage the presentand tl.r' . ntl a..
r . rter if max(ea)<gS iter end . = d(i)C(i.si. x(i) .Good examples the very largesystems linear are of in algebraicequations that can occur when solvillg partialdifferentialequations a variety of engineering and scientificproblemcontexts. 1.maxtt) ? GaussSeidel: causs Seidel nethod eo x = Gaus:lSei.0 if x(i) . if the system under study is to be solved repeatedly.i).xold(i))/x(i)) * 100. end if nargin<3  isempty(es). maxit=50.erLd In. break.err. can be extremelyimportant.. error('Matrix A nust be scluare').:)*x. it However. d if nargin<4  isernpty (maxit ) .s((x(l) ea(i) encl end = rter+1.i) = 0i x(i) end x=x'.rined For a singlesolutionof a setof equations is oftenunnecessary. l t d i .n)iA(i.u '. is highly problenlspecific anclis often detern. b) : causs SeicleI vrithout % lnput: % A=coefficientnatrix Z b . if m=n.2. while xold F^I (1) _ x.n = C(1. es=0.ze(A) . .1:n) end for i = 1:n = b(i)/A(i. >= maxit. 2 Mfile MATLAB fo implemenl GoussSeidel The choice of a proper value for ).nl . . d(i) end i f ar n. F t .l r p .al.i) C(i. g u m e n s y o q u i r e d . end for i = 1:n = 0. 011001.LINEAR SYSTEMS: GAUSSSEIDEL functiolr x = GaussSeidel (A.1.b. c(i. empirically. es. end I F I G U R E2 . Fnr i l.del (A.rlghL hand side vector ( d e f a u l l .l0 0 0 0 1 2 ) ? es = stop criterion (default = 50) Z maxlt = rnax iteratrons Z output: Z x= soluLionYector relaxation { dr ledSL 2 i I naroin. the efficiency introdrrced a wise by choiceof ).
2 NONTINEAR SYSTEMS The following is a setof two sinrultalleous nonlinear equations with two unknowns: EXAM "f+"'"2:lg .) O J .{ .to) (r26r) In contrastto linear systemswhich plot as straightlines (recallFig. . ) : 0 Therefbre. \ . r t . ( r t . t 1.. . r l + J . 12. Justas we did when we determined rootsfor singlenonlinear equations..l). which is calledsrcce. t 1+ J J t .rsive substitution. l i ( .. : .the solutionare the valuesof the r's that make the equations equalto zero. (12.the solutionis the inte$ection0f .2. . Thil irpproach. . such systems of equations be expressed generally can as . . ../:(xr.3 Grcphicol depictionof the soluiic. 9. .7) 12.As in Fig.Theseequatiollscan then be implemenled iteratively to computenew valueswhich (hopefully) will convergeon the solutions. = 5 / .n irvo simultoneous of nor. r 2 . illustrated tlre lbllowingexample.)/ (12. tions can be solved for one of the unknowns. .r.r2 VeTSUS graph.lineor equoiions .. r . r .3.r 2 .. is in FIGURE I2. (12.r:. ) : 0 .270 ITERATIVE METHODS 12. eachone of the nonlinear equa.That is. these equations plot as curvesoD an .r1 the curves.7) is to usethe samestrategythat was employed fbr for fixedpoint iterationand the GaussSeidel method.1 Successive Substitution A simple approach solving Eq.
f . to the Note that a correctpair of roots is.yi ^ l (E12.5t35 :2.an alternativesolutionof Eq.37 16 : 5 429. : yTo .This behavioris even more pronouncedon the seconditeration: I0 .2)to deter_ mine a new valueof 't2: . 5 .12."rri xz ( Er 2 .5 can be substituted into Eq.( 1 .2 NONLINEAR YSTEMS S E X A M P L E. (12.915 y 3(2.3.86051 . t r l:  l0 .t' : .yz: 5'7. (12.20910)(24.6b)is Now the resultsare more satistactory: r. r 1: 1 .17e4s) 5 7.2.21429 3.709 rz : 57 . Equation(12.3( 0.rl _ 2 ancl : 3 Initiate .6).6a)can be solvedfbr .21429t2 Lt  0.5 This resultand the initial valueof x2 ./iblT'r2 and of Eq.20910 24.s ) 2 :2.3 . 2 12 27r s u c c e s s i v e u b s t i t u t i o no r q N o n l i n e o r s y s f e m s f Problem Stotement.2.1:.5)2 24.) and Eq. 5a n d . can be usedto determine new value a 1 0. . Use successive substitution determine roots of Eq. z 3 .1.I2.6b)can be solvedfor 12 : )/ . For example.2. (12.3i 516 : Thus' the approachseemsto be diverging.J. the Now we will repeatthe conlputationbut with the original equations up in a diff'erset ent fbrmat.rr the computafionwith guesses o f . (E12.37 5 tO2 : ObvioLrsly.21429)(3.3(2. 5 /:2.1) Eq.12. ( I 2.6a) is . : Solution. (F. :I.rl. approachis deteriorating.2) ofxl: On the basisof the initial guesses.
d2. (. . (12.the root estimate corresponds the valuesof .Additionally..At this intercept.2.ttu . we useda graphicalderivationto computethisestimate.11and . \ f ' \ x i ) (128) "f wherex.e. The previous example illustrates the most serious shortcoming of successive often depends the mannerin which the equations substitutionthat is. l Y f Exz ( 12.272 ITERATIVE METHODS tr:@:1. the root. ( 12.2.ltal (t2.l}a) f z ..These criteria are so restrictivethat fixedpoint iterationhas limited utility for solving nonlinear systems. other of open root location methodssuchas the NewtonRaphson methodcan be usedfor the sanepurpose.An alternativeis to derive it fiom a firstorderTavlor seriesexpansion: ( x r * r ) : f ( x i ) * ( x i + r. f 2 .1.' r 0fuI (x2.1 : ri  EXAA f (x') ^l \xt ) tl29t form of the NewtonRaphson which is the singleequation method. even in those instanceswhere convergence possible. t 1 .8)canbe reananged I to yield l.l0) canberearranged give .111eQual zero. For the twovariablecase.11 is the point at which the slopeintercepts the . Eq.1.r axis. * ( x r .r1 and to where/y . the slope) of a function to estimateits intercept with the axis of the independent variablethat is.1H ' . ( . the approachis convergingon the true valuesof "r1: 2 andxz : 3.diveris gencecan occur if the initial guesses are insufficiently close to the true solution.. to (12. (12.a firstorder of Taylor seriescan be written for eachnonlinearequationas f t .%0s) Thus. i . ) ?xz (xz.2 NewtonRophson Justas fixedpointiterationcan be usedto solve systems nonlinearequations. . is the initial guessat the root andx. In Chap. a multivariable variable Taylor seriesmust be usedto accountfor the fact that more than one independent contributesto the determination the root.04955 3(1.However.ri*r) by definitionequals zeroandEq.86051 : 3. i + t: . f i . iv t : . i + r * r ..r" .94053 51 .i+r \'r. convergence on are formulated.) "" dlt. The multiequationfonn is derived in an identical fashion.. * . .6.[.For this situation. ".1 0i) Justas for the singleequation version.Recall that the NewtonRaphson methodwas predicated employingthe derivative on (i.x . 12.
S)2 1.12b) The denominator of eachof theseequationsis formally referred to as the determinantof the Jacobian of the system.o (l. of the rootsof Eq.5t* .JZ.t n " 3f2.) 1 0: ... First computethe partial derivativesand evaluate r and y: # : 2xr xz:2(1. i p r o x i m a t i o n ) .6). 5+ 3 (1 . 6 2 5 .s) 156.12) is the twoequationversion of the NewtonRaphson the following example.75) 156.r r. into to values be substituted Eq. .i \fzl 0x10x2 3x2'dx1 3f .7 r.i " " t^._ Jl. 5 : + .125 as at ofthe functions be evaluated theinitialguesses can Thevalues . Cramer'srule) can be employedto solvefor t.i  \fi.i5 3xr is for Thus.{t.it can be employediterativelyto home in on the rootsof two simultaneous equations.12a1 x2.Initiate computation of them at the initial guesses Solution.5. ]fz.5 andxz:3.fr.Xt q 3fi. t h e o n l y u n k n o w n s o r e " r l .J).5 6.5 3 : + W # :rr : 1. i a 1 .5 :  * 6xtxz:I + 6( r .5) ofz'o 3xi 2 : 3(3.5) : 36. (12.(2. : .i 3fz.5(32. .62s (r.r " \J\.i  dXt n t.t25 Thus.i dx2 a .thedeterminant theJacobian thefirst iteration of : 6.algebraic manipulations (e.5) ( 3.5(36. 5) 2 l : 1 .i+t : tl.s) s) (36. method.s) r. Consequently. 5 ) ( 3 .s(32.03603 2. Use the multipleequation with guesses x1 : 1. T h u s . E q 1 2 .5 : 32.i \fz.2 .As in Equation (12. until an acceptable can be repeated .84388 . E X A M P1 2 .i \xt \xz 3x2 \xy (12.s) : 156. 1 1 ) s a s e t o f t w o n1 linear equations with two unknowns.s) 1.5(3..5 5 . t3 .._ Jt.g. 3 LE N e w t o n .R o p h s o f o r o N o n l i n e q rS y s t e m n method to determine NewtonRaphson Problem Stotement. OX2 t^.62s(6. The computation accuracyis obtained.125 ^?<_ 2.Ir NONLINEAR SYSTEMS I I to with i 's areknown (they correspond the latestguessor apBecause valuessubscripted all (.t _ (12.i _ \fi. the resultsare convergingto the true valuesofxl : 2 and12 : 3. (12..i+t : x2.12) give can These 2.i 0fzl * 3fi. a1 d x 2 . f z .
it can diverge if the initial guesses are not sufficiently close to the true roots.1 Dxz :: 0. The twoequationNewtonRaphson approachcan be generalized solve n simultato To neousequations.just as with successive convergence the singleequation subsfitution.i+t dx2 +' ''f ih. .i f.i ofi. are knownatany generallyrepresented Eq. . no such simple procedureis availablefbr the multiequationversion.fi. r 1.i }xz : 0f.i d"h UI: 3f2. setof equations the by k:1.e.rr.l'r.i oxil where the first subscriptk represents equationor unknown and the second the desubscript noteswhetherthe valueor function in questionis at the present value(l) or at thenextvalue (t + 1)...r'l.i t "' t Jn. t4) wherethe partial derivativesevaluated i are written as theJacobianmatri.rconsisting at of the partial derivatives: 3ft. do this.J (" 1 f . . as l J l [ r i _ r ]: . ofzl 8r.t::.it exhibitsthe samespeedy quadratic as version. All other quantitiesare locatedat the presentvalue (i ) and.tl..rr. Eq.274 ITERATIVE METHODS When the multiequation NewtonRaphson works.13)concisely to Eq.r+r .Although there are for someadvanced approaches obtainingacceptable first estimates.t/J2 itJ r..\' dxt frf I t .However.I I ) can be written for the ftth equationas .Whereas graphicalmethodscould be employedto derive good guesses the singleequation for case. (12. ( 12.t. OX? Dfi. oIz. often the initial guesses must be obtainedon the basisof trial and error and knowledgeof the physicalsystem being modeled.i l.x2.i 'l':.n) c o n s t i t u t e a s e t o f l i n e a r s i m u l t a n e o ue q u a t i o n sh a t c a n b e s o l v e d s s t numericallyby the elimination methodselaborated previouschapters. r . Noticethat the only unknowns Eq.*r)r:L.2..Consequently..l3) (i.fz..i+rtermson thelefthand in side.i+r )ra.+t l l . ( I 2.)i Finally. ()X1 0Jt.i dxil rt : ... thus. with iteration.*.f <JIt itf r.i 3xn The initial and final valuesare expressed vectorforrr as in {x.i 0r.lrr.t l l + l J l { x i } '(r2.. the function valuesat I can be expressed as {f }r : 1. .}r: and {. in Matrix notationcan be employed express (12.i x n . .t.i ) . xz.i iJxr 3f..f n.i ... d.t ..13) arethe..
until an upper (maxit) or a specified percent relativeenor (es) is reached.this routineis passed Mfile that the an computesthe fbnction valuesand the Jacobian a given value of "r. The secondshortcomingof the multiequationNewtonRaphson method is that excellent initial guesses usuallyrequired ensure are to convergence. we can implementEq.']/139 Althoughwe couldcontinue iterations the command the in mode. Such matrix calculationscan be implernentedvery efficiently in MATLAB. (12.After this by using MATLAB to duplicate calculations the Exalr. (12.s000 ^2+x(1) *x(2) 10 .5].3.ri*r}: {t.16) and (12. Insightinto the solutioncan be obtained solvingEq.6250 1.}.3. essence. As in Figure 12. to Theretbre. The routineiterates tion and implernents (12.l7) in an iterative Eq.l4) with nratrixinvelsion. by versionof the NewtonRaphson methodis Recall that the singleequation ^/+l ^/ . Jacobian analogous thederilativeofa rnultir"ariate In the to tunction. 2./('ri ) 'f \x1l (12.3. alternafiveapproaches that are slower I i .a niceralternative is to express algorithmas an Mfile.4.14)cirn be solvedusing a technique such as Gausselimination.15)is sometimes inconvenient evaluate. >> T J=[2*x(1)+x(2) x(1) .7500 >> f=tx(1) f2. of Raphson approachhave been developedto circunrventtlris dilemma. ( 12.16) If Eq.17) to yield the improved estimates >> x=x . We can ilhrstrate fror. 5 t 10 0 36.nations the partialderivatives fbr basedon using finitedifference that compriseIJ].[.11) of Clomparison Eqs.17) clearly illustratesthe parallelsbetweenthe two is equations.1] '{.It then calls this funcat fashion.3"x(2)^2 I+6*x(1)*x(2) I 6 .n 12.in. This processcan be repeated in iteratively to obtaiu refined estirnates a f'ashionsimilar to the twoequation casein Example12.lr 12.x(2) +3*x(1) *x \2)'251 I Then.14)is solvedby multiplyingit by the inverse the Jacobian. variations the NewtonEq.rple we defining the initial guesses! can computethe Jacobianand the function valuesas >> x=[1.5u00 1.5000 32.2 NONLINEAR YSTEMS S 275 Equation(12.First. resultis of the {.5.l\f X.I2.0360 2. (12. As might be expected.most alre approxir. (12. limit of iterations We should note that there are two shortcomingsto the foregoing approach. Because these are somefimes difTicult or inconvenientfo obta.
break.it lea<=es. varargin{ : } ) . ' ...p2.end if nargin<3 iserLpty(es). . = additional parameters used by function ? ?r output: ? x = \/ector of roots Z f = vector of frrnctions evaluated ar roors 2 ea = approxinate percent.. z a r a r g i n ) t % newtmult: NewtonRaphson root zeroes nonlinear systems ? [x.. f i G t .{2. e a .) is the ith rnember the originalsystem Eq. / x ) ) . end end FIGURE I2.0001. f .4 MATIAB Mljleto implement Newton Rophson method noniineor for syslemsequoiions of than NewtonRaphson which have betterconvergence but behaviorhave beendeveloped One approachis to refbrrrruiate nonlinearsysteu.0001?) error a maxit = maximum allowable (default = 50) iterations p1..l a singlefunction: the as _L F(x) _\.initial guess % es = desired percent relative (default = 0.l l . can .maxit=50.7)... ' 5 m u 1( f L l n c .o r r l x .r..maxit. . x=x0. the Thfle. x 0 . dx=J\ f . . i t e r l = n s . iter = irer . .relative (Z) error = number of iterations % iter rf nargin<2. (12... f] =func (x. . fore.x0..p2. .l.. ('at least 2 input arguments required'). i r . while (1) IJ. values of of The of r that nlinimize this ftrnctionalso reprcserlt solutionof the nonlinearsystem. f. m a x i t . ).erra].p1. if iter>=nrax. % uses the NewtonRaphson method to find the roots of % a system of nonlinear equations ? input: % func = name of function that returns f and J Z x0 .end icer = 0.. e a = 1 0 0 * m a x( a b s ( d x .es=0. x z . ) 1 2 whereI (x1.iter]=nev/tmult(func.end if nargin<4 isempty(maxit) .es. x=xdx. e s .276 ITERATIVE METHODS f u n c L i . nonlinearoptimizationtechniques be employedto obtain solutions.ea.
(t2.6: 10.7 x 102 (12.21) .3 CASE STUDY 277 CHEMICAL REACTIONS Bockground' Nonlinearsystems equations of occur frequentlyin the characterization of chemicalreactions. If x.\xI cc=cc. = 3:7.x)2{c6.rz Cd=Cd.s 20.0 2x1.x 2 ): 5*xr+xz (502xrx)2(20x) (502xtx)(10xz) 4x10a .26) (12.o. For example.25) where the subscript0 designates initial concentration the ofeach constituent.. formulatethe equilibrium. .22) (12. respectively.24) (r2. : 50.x lo2.t (12.23) (12..theseare two nonlinear equationswith two unknowns. and cr.0 .2t\ where the nomenclatureci representsthe concentration of constituent i. Sofution. Thesevalues can be substiruted inro Eqs. can in and_r.x) f 2 @ 1 . and x" are the number of molesof C that are produceddue to the first and second reactions.o*tt*.\Xz 12 (r2.2x1 .20) (12.Thus.co.19).o: 5.I2. employ the ll !o4. c6.3cac.the concenrrations of eachconstituent be represented termsof r.lg) of and(12..0+xr*xz) * (c". tt : 4_x K.relationships a pair as of two simultaneous nonlinearequations.3.r8) (12.theycanbe characterized by Kl :3cicu Kz.o2x1 cb:cb.21)ro sive Kr: (c.o: NewtonRaphson methodto solve theie equations. (12. (12.xr) (c".x2)@a.the following chemicalreactions takeplacein a closed sysrem: 2 A+ B > <_ C (12.0*xL+x2) (co. Usingrhe stoichiometry Eqs.19) A+D*c At equilibrium.as ca:cu. the solution to this problem involves determining the roots of f1@1.20) and.ox:) * Given the parametervalues.c.
3.x(l)).I2B5e01.ai.7 8.r66c+000 2. the partial derivativescomprisingthe Jacobian can be evaluated as 3fr dxt x2) . c l f l d x l . x ( 2 ) ) J=fclf1dx1 df Ldx2.ft(xt. xz) 3xr x2) fz(xr + E.r1. shorL e 12..01'1 Solve tl E s: 5 V o 12. f2='1(x(1.. . var.f 2l+000 f= 'l .fi(xt + 6x1. 1 f 1=u(x(1).t (^) ) .71 The function nevrtmul t (Fig..lr =jf rc. ) .)). .Forexanple.rr These relationships can then be expressedas an Mfile to compute both the function valuesand the Jacobianas f unction lJ..5 Ust and (b) v tem to a equation a. af f 1. ) / (del..6. !') (5 + x + y) f . a : ..') I t2. u ( x ( 1 ) .xz * 6x2) J\$r.3.we must determinethe Jacobianby taking thepartial derivativesof Eqs. x (2) )rr (x (1).2 Us< system u x) .f ?t f unct ion f =u (x.rct{x./ funct ion fv (x.x{lt))..rz) 6xr ofr : 3xz 3fz oxz .) .Although this is certainly possible.000001.3 Re1 12.x(2) ) c l f 2 d x 1 = ( .r2) . ( x ( L ) .. * fi(xr. a . alternative to represent An is them by finite diff'erences inr fashionsinilar to the approach usedlbr the modified secantmethodin Sec.a x x '!) ^ '2 i 120  0r1 3xt Xl (50 *  * ':<.27).223 7e 010 i Ler = 4 .uz fz(xt. : x z : 3 : > forii.0004.*x(2)).).x(2) +del"x(2) ) u(x(1 ) .' (de1*x t 1) ) ..26) and (12.. x ( 2 ) ) ( ' d f 2 c 1 : : 2 =r ./) i (1Ll . 12. .lx?. x (2 )) dfldx2= (r (x(1) . I to I L (b) Rep 12.'' continued In order to use NewtonRaphson. (12.( r r( x ( I ) + c l e l * x ( I ) . 3fz o.278 iiir". xz) 3.rrgin) del=0. ( x( 1 ) + d e l * x ( l .4 Tht termine( reactors reactor(1 3.v ( x ( f ) . x ( 2 ) ) . system ul T O. evaluating the derivativesis time consuming.i: (50  . xz + d. y ) f = (i.5?7r€.df2cixi df2r. xz) 3x. x ( 2 ) + d e l * x ( 2 ) ) .0.1 ITERATIVE METHODS .4)can thenbe employedto determine rootsgiven the inis t i a l g u e s s eo f x .fz\r. + x + .illr ir.r(rlel*x(1. .a.fi(xr. ) / (ciel*x(2)).
. r 2 y. nonlinear 12. (b) Successive substitution using initial guesses r : of (c) puesses : r.6cr : 1200 c2't l2ca: 2350 Ithis problem with the GaussSeidel method to Usethe GaussSeidel method (a) without relaxation (b)withrelaxation(. : . If necessary.3 4 Rr.8 Determinethe solution of the simultaneous equatlons: ^ )) J! )il:.: : 6 A .y ::5 2:+2t3::3 ?r '. Show using any number of iterations that is necessarythat your solution does not converge../) )*5xr':r2 Use the NewtonRaphson methodand ernployinitial guesses : of . .3 8 1 . of &Dd (12. t : l ' 7 t . the set(s)that you could not solve using an iterativemethod such as GaussSeidel.3366) 2. 12.6 1 .t r")r." r 3 : . : 2.22) of centrations thefourconstituents: : ca : 50 .3366 2.6 Of the following threesetsof linearequations. r .6496 : cn:20 .2(3.6634 cc : 5 + 3.r*4y*5::6 6.rr tt*5xr:21. i14i.3. solution x.)": 1.r2 (a) Graphically.2 z : 4 I Repeat Prob.jrj: 27 3 r t.6772 ca PROBTEMS methodto solvethe followine I (a)UsetheGaussSeidel untilthepercentrelativeerror falls below s.2but useJacobiiteration.3.* 3y*:: l3 .r: .PROBLEMS continued a is These values After four iterations.r*_y*6:: B 3.6772 40.7 Determine the solution of the simultaneous equatlons l':l+x+u. : 5olc: identify 12.3228 : lO .3c:'r * l8r2 . Thefollowing systemof equationsis designedto de(the ine concentrations c's in g/m3)in a series coupled of asa function of the amount of mass inout to each Itherighthand sidesin g/day): r'::3800 t.r+B::2 r*5.6 x 2 1 } s l : .2 Usethe GaussSeidel method to solve the followine untilthe percentrelative error falls below e" .3 r 1.6112: 11. (12.3366 x.0138 + :7.3366 16.2) to solve the following sysof reanangethe t0 a tolerance s.25) compute equilibrium into through to the concanthenbe substituted Eq. Clearly stateyour convergence criteria (how you know ir is n()1 converglng).T] I {i: Jrr+2.6772 obtained. 5 .r:.57o: Set One Set lwo Set Three 9.5 * 4l 25 l0s (a) Repeat but use overrelaxation with I : 1.)O nonlinear 12. .6 .2.2. to achievec'onvergence 2 " t.r : _t' 1. : 57o.l 2 r + 5 r .
t  F cc. of 25"C 25"C Tn r2) 100'c where Q" and Q.IO P .s  l l ctzl o.. andD : the gasliquidexchange rate. L cLr< I I Cn< +' + + cu< cls< Qt l I l cL6 FIGURE P12. Q t: c c o : 1 0 0c m : 7 0 .10.a in massbalancefor the first reactor can be written for the sas as Qccco.crr) : 0 ccr):0 derivatives. Solvefor the concentrationsgiventhefollowingvalues: l.Use the CaussSeidelmethod to solve for the temperatures the nodesin Fig.10 The steadystatedistribution of temperatureon a heated plate can be modeled by the Laplace equation: T. Pl2.8. D : 0.At steadystate.9 depicts a chemical exchange process consistingof a seriesof reactors which a gasflowing from in left to right is passed over a liquid flowing from right to left. are the gas and liquid flow rates.9 Figure P12.Qcccr+ D(cuand for the liquid as Qrctz . I 0). 12.ITERATIVE METHODS C Tcct > ccz 2Cc: I cc.Similar balances can be written for the other reactors. by centered finite differences can be substituted for the second FIGURE I2.Qrcu I D(ccr . The transferof a chemicalfrom the gasinto the liquid occurs at a ratethat is proportionalto the differencebetweenthe gas and liquid concentrations each reactor.. Tl 100"c o:*** dx' dy 75. respectively. Q6 :2.9 12.C /5 L If the plate is represented a seriesof nodes(Fig. P I 2. .which result in a system of linear algebraic equations.
pT4.the curve is de_ srgnedto follow the pattern of the points . your ftrst exposure[o curve fitting may have been to deter_ mtne lntermediatevalues from mbulated data_for instance. of or ...r...i'.o.. fc1nd. pT4. lb and e.rr". ..rgrriiion (Fig.::!::1'j"H'.. The esti_ mation of valuesbetweenwellknown discretepoints is called inrerpolarion(Fig. inr"r"r.... " "11111111.n.::.\. t. Curve Fittingond Engineering ond Science.a curve or u .". exhibitssignificant a degree error "scatter. In addirion.t' squares . Curve Fitting braic r the 4... we make no efiort to ln^tersect every point.rh .cr. ... .rluuru! wucrs theoaul ls Known to very Dre_ ."r. Dara often is given discrere for varues arong mayrequire esrimalesat pointsbetween discrete the values. tJi. i"... One *uy to ooil. the remainder of your career.ffi.1.you will ...Both of theseapplicationsare known. Examples are valuesForthe density of water or for the heaica_ pacity of gasesas a function of temperature."gfi..takenas a s. tar)..."j. from interest tables for *". whererue datais knownro be very pre_ cise.oure values of the : . chapters "o*..rnroughout irequentoccasion estimate to intermediate values fave trom such tabJes. singre curve that represents generaltrend of the data.. is of curves rhat passdirectly through each of the points. lftaln ilterm"diute plified version of a complicared funcrion. Rather. the Because any individualdadpoint *"V l..1 oVERVIEW WhEr ls Curve Fiffing? 13 througtrto oescribetechniques to fit curves to such data to estiates. Tri.narureis"uudt. s the range. .."):. rn.ii.1you other'on basis rhearnount errorassqciared the of of ..t u.. . such data usually orii.. ri:" you rnay require a sirn.*'r.!! l\l ' the_ basicapproach to fit.. funct'ion a numberof discretevalue alon^g at .1. e . .: or from steam fables for thermodynamics.t & &. a simplerfunction of i: may be derived to fir thesevalues..t.ascurre trtting..inatesfrom tables..
lineor through doto points: leoslsquores five curve fo Three ottempts fit o "best" io) i n l e r p oo i o n o n o ( c ) c .there are a greatmany more that are not your own dataand develop and new problemcontextsoften requirethat you measure cases Two types of applicationsare generally encountered your own predictive relationships. testing. values of the dependent Trend analyslsmay be used to predict or forecast observeddata or interpolation This can involve extrapolationbeyond the limits of the of within the range of the data.Imprecisedatais often analyzed variable. Special ulated.you might utilize interpotions.282 4 FITTING PART CURVE f(x) (c) P FIGURE T4. with leastsquares lating polynomials.an are data. A second application of experimentalcurve fitting is hypothesistesting.If the model coefficients with measured model is compared existingmathematical .. For caseswherethe datais measured regression.I (b) regression. Here. data:trend analysisand hypothesis when fitting experimental pattern of the data to make predicTrend analysis representsthe processof using the with high precision. All fields of engineeringand scienceinvolve problems this type. havebeentab' and scientificproperties Although many of the widely usedengineering availablein this convenientform. r f l e o f r r t e t o oo l o n .
2 PARI ORGANIZATION to valuesthat bestflt the observed data. well suitedfbr fitting datathat is genelallysmoothbut exhibits As such. alternativenodels are comparedand the "best" one is selected the basisof empiricalobservations. Chap. we also present how to calculate slopeand intercept this straight the of fbr In we ser'the and visualmethods evaluating validity of the lesults. portantin other nurnerical methodssuchas integrationand the approxirnate solutionof dil'Finally.culvefitting ferential equations.Besides quantitativc line. l6 presents alternativetechnique fitting precisedatapo. introduce to linesand parabolas connect of by basicconcept polynomialinterpolation usingstraight polynomill. techniqtres be usedto derivesimplefunctions can cornplicated functioDs. called in fbr thesepolynomials equation fbrmatsare presentcd expressing whenthe appropriate orderof the polynopolt'nontial.polynomials derived this purpose. of variable). in . describe fbr of equations. l4 ale del'otedtononlinearregression. are we how polynomialand multiple regression After multiple regression. the alternativeclrrvetittillg techniquecaTled valuesbetween previously. xtn. l3 focuses linear regre. Chup. The second.. tages This for an Finally. The first. illustrate model. is a linear functionof two or more signedtbr the casewhere the dependent exvariables 12.this will allow of both subsets a generullinear leastsquares its statisand discuss general matrir representation ofregression a us to introduce concise Finally. Then. fashion.it may be appropripledictecl valuesof the nrodelwith observed valuesto testthe adequacy of ate to compare on the model.Among otherthings. eral approaches the linearization nonlinear andmultiplelinearreglessiou. Ofien.15. chapter imolemented MATLAB. to lits callecl splineirrterytolation.Two procedure fitting an rrthordel ior points. of tical properties.This is followed by a clescription multiple linear regression. In Clnp. /5. to fit This approachis designedto computea leastsquares of a nonlinearequaticln data.ints. which is depolynomials. has advanrvhenthe properolder is known befbrehand.2 PARTORGANIZATION that Chap.sirtn cubics. acldition. to approximate i ! 4.called the Lagrange interpolotingpoltrutmial.it is particularly is interpolation with an overviewof how piecewise The encls abruptlocalclranges. is on of perimental of datawherethe variable interest dependent a nunlber differentfactors. is.ssion. hol'r'to deon After a brief review of statistics.the last sections Chap. interpolutionis describecl. polynomials databut in a piecewise techniclrre.we developa generalized fbrm. preferable is Newton's interpolotirrg mial is unknown. irrdependent .if estimates the model coetTicients alreadyavailable. of Chapter/4 begins with brief discussions polynomial fit regres.or higherorder Pctlynomial dealswith developinga bc'st clf parabolas. discussing terminethe "best" straightline througha setof uncertain datapoints.4.On the ulrknown. This approach specialutility for evaluating has r1. curvefitting is irnIn additionto the foregoing engineering scientific and applications.it nray be necessary determine are of other hand. intermediate interpolation usedfbr estimating is As discussed We the for precise ln are datapclints.
Knowing how to computethe slopeand interceptof a bestfit straightline with linear regression.Specificobjectives regression can be usedto fit a straightline to measured topics coveredare ' . . Knowing how to implementlinear regression YOU'VE GOT A PROBTEM to  we noted thaf a freefalling object such as a bungeejumper is subject the I n Chap. c. ' and the normal Familiarizing yourselfwith somebasicdescriptivestatistics distribution. a first approximation. An irrcJiridual susperrded . assumed I I p r o p o r t i o n a o t h es q u a r e f v e l o c i t y s i n a tl o Fu : cdu2 (13 l) where Fy: the upward fbrce of air resistance : kg m/sr].t : a drag coefficient [N (kg/m). that this forcewas As we upward tbrce of air resistance. ( I 3. Expressions play a critical role in their formuiaexperiments relationihipsderive in part florntheor.':. the Knowing how to computeand understand meaningof the coefficientof error of the estimate.v.One suchexperinrent depictcdin Fig. inawind is is riorr.1) come from the lield of fluid mechanics. ' . l. with MATLAB. Lineo R"gression r CHAPTER OBJECTIVES The primary objectiveof this chapteris to introduceyou to how leastsquares and data. such Although suchas Eq.and u : velocity[m/si. /j. determination and the standard so to how to usetransformations linearizenonlinearequations that Understanding they can be fit with linear regression.
but exhibit particularlyat the higher velocities.the points do not increasesmoothly. the points indicate that the force increases velocity increases. 13. 2 Plot forceversus wind velocifu on obieclsusoended o w nd lunnel for in of TABTEl3.As in Fig. The relationshipcan be visualizedby plotting force versusvelocity. First.the relationshipbetweenforce and velocity may not be linear.2.Finally.N (m/s)fromo wind tunnel Experimentol for force(N) ond velocity doto exoeriment.l .This conclusion becomesmore apparentif we assume that force is zero for zero velocity. The resultmight be as listedin Table l3.m/s F I G U R EI 3 . . severalfeaturesof the relationshipbear mention. be obvious. as Second.t u. m/s F. althoughit may not rather significant scatter. I I Wind tunnelexperimenl mecsurehow the force of o r reslstonce fo dependson veocily 1600 1200 z t\ 800 40 u. to 25 2A 7A 30 380 4A 550 50 610 60 l22A 70 830 BO I 450 for tunnel (any volunteers?) and the force measured various levels of wind velocity.R LINEAR EGRESSION 285 ri {\' * + F I G U R E3 .
the conceptis not very practical. will first review some basic concepts we frorn the field of statistics. The mostcomlron measure centraltendency the arithmetic is of mean.Ifthe numberof measurements odd. the fbllowing material is designed a brief introduction.r STATISTICS REVIEW Before describingleastsquares regression. 2 ) (and all the sr. feel tiee to skip the following pagesand proceed directly to Sec.2 contains24 readingsof the coefllcientof thermal expansion Takenat facevalue. n (1 3 .2 6 495 6 665 6755 6 56_s Meosuremenis the coefficient thermol of of exponsion siructurol of steel 6 )i n l { i n .r..we will explorehow to flt a "besf' line or curveto suchdata. or \.For continuous or variables such as the data in Table 13. a of informationthat is.The medianis sometimes called the 50th percentile. If you are familiar with these subjects. of Meosure of Locotion. ' ' F ) ] . t . It is calculated first puttingthe datain ascending by order.) dividedby the numberof points(n).l3 and 14.it is thearithis is metic mean of the two middle values. as I 3.If the numberis even.71 5 6 555 o 635 6 625 6 575 6 395 6 485 6715 6.thereare actually TABLE13. and the normal distribution.In addition. In so cloing. 13. will illustrate we how relationships Eq.I ) arisefrom experimental like data.Table  3.If you are unf'amiliarwith theseconceptsor rrrein need of a review. standarddeviation.the dataprovides linritedamount ofa structural steel.395 to a maximumof 6. 6655 6 605 668_5 .For example.Thesedescriptive statistics mostotien selected l'epresentl) thelocaare to tion of the centerof the distributionof the dataand (2) the degreeof spread the dataset.The conceptusually hasdirectutility only when dealingwith discrete coalselyroundeddata.rcceeding wherethe summation summaticlns this section)is from I = I in throughn. Additional insight can be gained by summarizingthe data in one or more wellchosenstatistics that convey as much informationas possibleaboutspecificcharacteristics ( of the dataset. There are severalalternatives the arithmeticrnean. These include the mean.we describehow simple descriptive statistics and distributions in can be generated MATLAB.655 6775 6 55. [(x to 6 595 6 505 6 625 65r5 6 615 6 435 6.The medianis the midpointof a to groupofdata. ( 13.the rnedian the middlevalue.residualsumof the squares.severalmeasurements were madeof a particularquantity. r 3. The mode is the value that occursmost fiequenlly.l DescriptiveStotistics Suppose that in the courseof an engineering study.For example. that the valuesrangefrom a minimum of 6.2.2.286 LINEAR EGRESSION R In Chaps..The urithntetic ntean (l) of a sampleis defined as the sunr of the individualdata points(.775.
y)2.)iszero. ( 13.4) Thus. ) will be large. .)3 where S' is the total sum of the squares the residualsbetweenthe data points and the of mean. 6. the standard s. (and.ir2 (13.). Eqs.555. the differencebetweenthe largestand the smallest value. it would be unlikely that any of the values would even have repeated twice. a Meosures of Spreod. nl (I3.. of A final statisticthat has utilrty in quantifying the spreadof data is the coefficient of variation (c. 5 ) Note that the denominator borhEqs.Hences.(13. The spreadcan also be represented the squareof the standard by deviation. about of ) the mean: ( 1 3 .which is called the variance: r r ' .5)is ru. This nomenclature derivesfrom the fact that the sum of the quantities uponwhichgisbased(i.ifyis known and rr .Consequently.As such. and s. Thus..If the numand bers had not beenroundedto 3 decimal digits.I in is ref'enedto as the tlegrees freedont.which all occurtwice.This statisticis the ratio of the standard deviationto the mean.625. is not it considereda very reliable measure because is highly sensitiveto the samplesize and is it verysensitivto extrelne alues. Anotherjustification for diviclingby n .fyt. The simplestmeasureof spreadis the range.If they aregroupedtightly.6.I of the valuesare specified.(13.more convenientforrnula is availableto compute the variance: 2 .. only n .)r. We should note that an alternative.\:)l n (1 3 . .: f u.1 is the fact that there is no suchthing as the spreadof a singledatapoint..6) This versiondoesnot requireprecomputation ! andyieldsan identicalresultasEq.v. x 1007o (1 3 . is often multiplied by 100 so that it can be of It expressed the form of a percent: in s).I STATISTICS REVIEW 287 ll ii t four modesfor this data:6.Although it is certainlyeasyto determine.s.)ry. l frv. e v The rnostcomlnon measure spreadfor a sampleis thestandarddeviation(s. For the casewhere n : 7..We will return to the mode when we describe h i s t o g r a mls t e ri n t h i ss e c t i o n .3)and (13.715. if the individual measurements spread widely aroundthe mean.1 the of the valuesare said to be freely determined.)' l.it can be an informative statistic.6. are said to be basedon rr . remainingvalueis fixed.655.3)and (13.conare out sequently. 7 ) t .1.I deof greesof fieedom.it providesa normalizedmeasure the spread. The quantityru.e..? (Dt'..I3.5)yield a meaningless resulrof infinity.or $: Itr'..if continuous dataare groupedinto equispaced intervals. However. deviationwill be small.5).
the median is computed as the arithmetic meanof the middletwo values:(6. As in Table 13. (13.l3.6 24 Becausethere are an even number of values. standard deviation.yr I 2 3 4 5 6 7 8 9 t0 tl )2 t3 l4 t5 t6 l7 IB t9 2A 2l 22 23 aA 6 395 6 435 6 485 6 495 6 505 6 515 6 555 6.00062 o.289 44422 44.02723 0 0t323 0.689 45091 45091 45630 45901 u4) 0J/ .oo723 0. as [Eq. coefficients thermol of exoonsion fromToble (Jl .655 6 655 6 665 6.3.00302 a 00422 a.L : 0 .71 5 6755 6.605+ 6.00203 0. Solution.217000.02402 0 03062 T I s8zdo 67Tn6 40 896 41 449 42 055 42.635 6.61 5 6 625 6 625 6.775 o 04243 4.2.Jl3"un t" compured (13. The data can be assembled tabularform and the necessary in sumscomputed ""HJt.2)].00203 0 001 3 2 0 00063 0 00003 0.715 6. of is which canbe usedto computethe standard deviation [Eq.685 6. t : 158'4 :6.615)12:6.median.oao22 0. Computethemean.andcoefficient of variation for the datain Table 13.3)]: sr.the sum of the squares the residuals 0.variance.)85 42.oat22 0 00302 0. : 0.2. 0 9 7 1 3 3 z TABTE13.315 42.217000 *+ .i l3 S i m p l eS t o t i s t i co f o S o m p l e s Problem Stotement.555 6 565 6 575 5 595 6 605 6.62b 43 758 4 38 9 1 4 3B 9 l 44 023 44 285 44.3 Dotoond summotions compuiing for simple descriptive stotistics the for .01103 0 00903 o.445 42568 42968 43 099 43.00002 0.288 LINEAR REGRESSION EXAMPLE .61.23) 43 494 43.ao722 o 41322 a o)322 o.00062 0.
(13.tcned n:srra.we can seethat the bin with the most values is from 6.6 289 The validity of Eq. The symmetric. The units of measurement plotted on the abscissa are and the frequencyof occurrence eachinterval is plotted on the ordinate. ihe q66+\ hcll.A histograntis constructed sorting the meaof by surenrents into intervals. of As an example. the FIGURE I3.bellshaped curve superimposed Fig. Notice also.3 is one such on characteristic shapethe normal distribution.ofronnnn. rh^ ^"h^.i rli 'i"ll]r[io .r.3 A histogrom of used to depict the distribution doto.rn."" .6 to 6.v.?: (0. If we have a very large set of data.Given enoughadditionalmeasurements. br.:_ 0.5)].47Vo 6.6.097133)2 and the coefficient variation[Eq.009435 24l 13. OJ 0) L2 . the histogramfor this particularcasecould eventuallyapproach normal distribution. ^{ l^r^ ^^inis increcses.A histogramprovidesa simple visual representation the distribution. :0.i. a.6)can alsobe verifiedby computing \: a 1045. it is more common to report the most frequent range as the modal class interval.or bins.7)]: of c. (13.ion. l3.I STATISTICS REVIEW the variance [Eq.400)' /24 : 0.1.009435 s.a histogramcan be createdfor the data from Table 13..that now that we have groupedthe data.r'.62. the histogramoften can be approximatedby a smooth curve.fpq 1l^p .64.651 (158. (13.a) suggests that most of the datais groupedcloseto the meanvalue of 6.097133 x1007a:1. is the shapewith which the data is spreadaroundthe mean.The result (Fig.I3.2.6.Although we could say that the mode is the midpoint of this bn.2 The Normol Distribution Another characteristic that bearson the presentdiscussion the datadistributionthat is. 13.
.794266.the rangedefinedby.ifsomeonetold us that they had rneasured valueof7.61. to y * .2: g >> format short (s).2.we calculated Example13.v .1.'rrrve fiffinq fo desiqn . very simpleexamA ple is their useto quantify the confidencethat can be ascribed a particularmeasurement.35. 13. standarddeviation.555 >> min(s).If it is a matrix.097133. will encornpass For example.we a would suspect that the measurement might be erroneous.395 o. For the arithmeticmeanis computedasmean(x). t/a >> range=max(s) min(s) O. to approximately95Vo.the function returnsthemean of the vector's values.6 for in and s. can tentativelymake the statement we that approximately95Vo thereadingsshouldfall between6. )r + 2.median (s) h h 6. will encompass approximately68Vcof the total rneasurements.mode >> mean (s). it returns a row vector containingthe arithmetic mean of eachcolumn of >r.max(s) :ne 6.r example.s.097133 I MATIAB also ofiers a Statistics Toolbox that provides a wide range of common statistical tasks. Basedon our analysis. norand mal distribution all have greatrelevance engineering to and science.std(s) :nc 0.1. 6.s. The following is the result of using meanand the otherstatistical functionsto analyzea column vector s that holds the datafrom Table 13.0094348 anc  0.from random rrrrmher oeneration fn . to If a quantity is norrnallydistributed.. the datain Table 13. :0.1that) = 6.3B >> var(s). Becauseit is so far outsidethesebounds.2s. Similarly. the rangedefined by tr .3 DescriptiveStotisticsin MATLAB StandardMATLAB has severalfunctionsto computedescriptivestatistics.residualsum of the squares.405'734 of and6.290 LINEAR EGRESSION R The conceptsof the rrrean. If x is a vector.
5.4 Hisfogrcmgeneroiedwith the MATLAB hist funciion 6. 13.the result is >> hist(s) 3 6.I STATISTICS REVIEW 29r Theseresultsare consistent with thoseobtainedpreviouslyin Example 13.85 .'.Note that althoughthere are four valuesthat occur twice.642 6.2.1.For just producesa histogrambar plot with example.6 6.4I4 6.3.4is similarto the one we generated handin in by Fig. The a hist functionhasthe syntax ln. MATLAB can also be usedto generate histogrambasedon the hist function. xl = hisL (Y. and y is the vector being analyzed. 13.528 6. For the datafiom TabIe 13.his'* (y) without output arguments l0 bins determinedautomaticallybasedon the ranqeof valuesin r.7s5 The resulting histogram depicted Fig. x) where n : the number of elementsin each bin.Note that all the arguments and outputswith the exceptionof y are optional. x : a vector specifyingthe midpoint of eachbin.718 6.55.55 6.68 6.566 6.8 6. FIGURE I3.I3.65 6_7 6.452 6.5 6.75 6. the mode function only returnsone of the values: 6.49 6.604 6.
The simplest examrr p l e i s f i t t i n g a s t r a i g h t l i n e t o a s e t op a i r e d o b s e r v a t i o(n s. and e is the error.somecriterion must be devisedto establish basis a fit. y ) .'t i:l i .as.However. Although such"eyeball" calculaapproaches have commonsense appealand are valid for "backot'theenvelope" tions. in as i . any straightline falling within the dashedlines will minimize the sum of the absolute values ofthe residuals. : i .) /'rl i:l (l3.t. betweenthe true valueof y andthe approximate a0 + atx. unlessthe points defineaperfert would straightline (in which case. the residualis the discrepancy value. w h i c h d e p i c t s t h e f i t o fs t r a i g h t l i n e t o t w o p o i n t s . I 3. L ' i=l : Lt. 5 a .10) positive and negativeerors cancel. t h e b e s t f i t i s t h e a line connectingthe points. we must first quantify the discrepancy.asillustratedby F i g . O b v i o u s l y . Thus. .lo) wheren : total numberof points. ( . .2.a1x.However.8) where a6 and a 1 are coefficients representingthe intercept and the slope.interpolationwould be appropriate).9) Thus. predictedby the linear equation. ' . One approachto do this is to visually inspect the plotted data and then sketch a "best" line through the points.That is. . e:!asa1x (13. . or rcsidual. . .a o alxil (l3ll) Figure 13. I Criteriq for o "Best" Fit One strategyfor fitting a "best" line through the data would be to minimize the sumofthe residualerrorsfor all the availabledata. One way to do this is to derivea curve that minimizesthe discrepancy between data the points and the curve. ( x z . l 3 .any straightline passingthroughthe midpoint the of connectingline (excepta perfectly vertical line) resultsin a minimum valueof Eq. . ' . ) n ) . .:) l ) . between the model and the observations.respectively. differentanalysts draw different lines. r .t "r.which can be represented rearranging (13.as in L't r:l i ".8)as by Eq.5bdemonstrates why this criterionis also inadequate. the four points For shown. To do this. forthe To removethis subjectivity.the best curvefitting strategy to derive is error is associated an approximatingfunction that fits the shapeor generaltrend of the datawithoutnecessarily matching the individual points.2 TINEAR TEAST. this criterion also doesnot yield a uniquebestfit. they are deficient becausethey are arbitrary. equal to zero because ab One way to remove the effect of the signs might be to minimize the sumof the solutevaluesof the discrepancies. f for The mathematical expression the straightline is y : a o* z t l x1 e (13.SQUARES REGRESSION Where substantial with data.this is an inadequate criterion.(13.292 LINEAR EGRESSION R 13.r.
As depictedin Fig. b e s ' r i . e o p A third strategyfor fitting a bestline is theminitnar criterion. and Wilkes. 1969).il I 3.tt't7'j''' oo2ote' . .as.l il ii .e 6 1 1 t o s r 6 . 13. e s s i o n l . A strategythat overcomesthe shortcomings the aforementioned of approaches to is minimize the sum of the squares the residuals: of iltI S.' : (v.In this technique.5 F x n m n l e s n [ s o n .1 .i (c) FIGURE I3. : 5"i : f. )m i n i m i z eh es u m f t h eo b s o l u tv o l u e o f i h er e s i d u o lo n d ( c )m i n i m i z e s ts o e s s.2 LINEAR LEASTSQUARES REGRESSION 293 1il lrl li .l il il ttltt' l. It should be noted that the mininax principle is sometimeswellsuitedfor fitting a simple function to a complicatedfunction (Carnahan. line the is chosenthat minimizes the rnaximumdistance that an individual point falls from the line.12) .5c.l (13. t .this strategyis illsuited forregression because gives undue it influence to an outlierthat is. a single point with a large error. z e s t h e s u m {9 b o f t h er e s i d u o l(s .r lm ^ m . l h em o x i m u mr r o r f o n y i n d i v i d u o lo i n t . Luther. . e . t h o t o r c : n g l e n r r n t c f n r .arxi)z Lt /' .
.f.Ifthis is done.1.r.n.vand..uf.) '\ . .... l Lll.r r .atx wherev andi arethe means .. The data can be set up in tabulirrfbrm and the necessarv sums . realizingthat I a0 : noy.r).. L Now. 13. we can express equations a setof the as two simultaneous lirrear equations with two unknowns andnr).)' This resultcan rhenbe usedin conjunction with Eq..Beforediscussing a these propertles. Eq.rI. fbrce is the clependent variable(. includingthat it yielcls uniqueline lor a given setof data. \t r .r.f o : f r.. f ..r 2). of EXAMPLE 3. : f .( / { ).'? .2 leostSquoresFir of o Srroight Line To determine varues .r. (13. Fit a straight line ro rhevaluesin Table i3.r'..r5) Solution.) t't A^: 2 : z \ .. .2 1 L i n e o rR e g r e s s i o n ProblemStotement.294 TINEAR EGRESSION This criterion. un  tt 1x.v) and velocityis the independent variable (. . + ( \ \s r \LJ'ri)tt1 Thesearecalledthe nonnal equation. all summationsare fiorn r : I to rl. . They canbe solvedsimultaneously for .r3) to solvefor ao: i ..12) differentiated respect each for is with to unknown coeflicient: i)s' ilLti : ? \. (r3.(I '.. In this application.r.t.r'. :Lr.i ltT. (ao tr (1. .the equations be expressed can as o:f. . (13. ) r .n.r. _ . Settingtheseclerivatives equalto zero will result in a minimurn S.. I.respectively..t'' O ..s and at. (f * "..1.\'l Note that we have simplified the summationsymbols.*i .unlessotherwiseindicated. which is called leastsquares..a 1 ' r .r. I. will prewe senta techniquefor cletermining varucs (h andat that minimize the of Eq.2. ( 13. has a numberof advantages.
shown Fig.REGRESSION I 3.000 3r2850 The meanscan be computedas 360 _ i_45 8 _ }: 5.400 22AAO 30500 73 204 5 8 .404 2a 404 xJi I 2 3 4 5 6 7 B I JOU : to 2A 30 4A 50 60 7A BO q I ?< 25 7A 380 550 610 t 224 830 1.4 295 needed compule bestfit for the doto to the line Dotoond summotions Toble.15) (13.135 : 641.6.47024(45)234.815 g (13.600 4.904 6. I ]3.. 13. is in Theline.400 I 1.16) with and as The slopeand the interceptcan then becalculated Eqs. 4 5 0 254 t.6 ine Toble 3 t 1 leosfsquores o strclght to thedotofrom fitof 1600 1200 800 z .r 0 0 I 16..2 LINEAR LEASTSAUARES TABTE13.400)(360P * : ao : 641. 8 ( 3 1 2 .875 19. from xi li x! r00 4AO 900 r.41024u withthedata.41024 8(20.600 2 500 3. theleastsquaresis Usingforceandvelocityin place F : 2342851t 19.2857 fit ofy and.) r.along FIGURE I3. o' : . 8 s 03 6 0 ( s 3 5 ) :19.
Thus.3 Quontificotion of Error of lineor Regression Any line other than the one computedin Example 13. (13.7 The residuol lineor in regression representsverticol the dlstonce o betweendotopoint ihe ond s t r o i g hlti n e .  3.: f (li aoarxi)2 i:t Notice the similarity betweenthis equationand Eq. 13.17) s.Recall that thesum of is the squares definedas [Eq. we will show how transformations be employedto derivean altemativebesrfit line thatis can more physically realistic.In Section 13.7).2resultsin a largersurnof thesquares of the residuals. *o Measurement a0+ afii .18). A number of additional propertiesof this fit can be elucidated by examiningmore closely the way in which residuals were computed. It can be demonstrated if these that criteria FIGURE I3. the line is uniqueand in termsof our chosencriterion is a "best" line through the points.(13.296 LINEAR EGRESSION R Notice that althoughthe line fits the datawell. the zero interceptmeansthat theequa" tion predicts physically unrealisticnegativeforces at low velocities. of The analogy can be extendedfurther for caseswhere (1) the spreadof the points around the line is of similar magnitude along the entire range of the data and (2) thedistribution of thesepoints about the line is normal.In Eq.2. (13.17).4) (13. (13.3.:ftlrl)2 (13.18) In Eq.the square ofthe residualrepresented square the ofthe discrepancy between the dataand a singleestimate the measure centraltendencythe mean.t. of of the square the residualrepresents square the verticaldistance of the of between data the and anothermeasure centraltendencythe straightline (Fig.12)) n .
2 is that thereis no for such thing as the "spreadof data" arounda straightline connectingtwo points.8 I (o) Regression showing thespreod thedotooround meonof thedependent doto of the vorioble ond (b)lhespreod thedotooround bestfit Thereduction thespreod goingfrom of the line.this quantityis designated .This is called the maximumlikelihoodprinciple in statistics.e. This is the magnitude of the residual error associatedwith the dependent variable prior to regression. ( 13.. 13.This characterizes residual the FIGURE3. n2 (13.The subscriptnotation "y /x" desigis nates that the error is for a predicted value of y correspondingto a particular value of x.(13.3)] J]. variable(in our case.9). Also. for yieldsa meaningless the casewheren : 2. Theseconceptscan be usedto quantify the "goodness"of our fit. that quantified the spreadaround the mean (Fig.17).cored t[e be'lsl^'oped by cu'ues therigl^t.To do this.F. noticethat we now divide by n . (13.. Just as was the case with the standarddeviation. tbl . the standarderror of the estimate quantifies the spreadof the data.the most likely) estimates ag of and a1 (Draperand Smith.I 3.As with our discusof sion of the standard deviation.q. the sum of the squaresof the residualsaround the regression line with Eq.Thus. we returnto the origiuseful for comparisonof severalregressions nal data and determine the total sum of the squaresaround the mean for the dependent g y). called the standard error of the estimate.After performing the regression.19) where s_"7.8b in contrastto the standarddeviation s.. In addition. thus./r  s.8a).os ind. 13. 1981).19) resultof infinity.we can compute Sr.ifthese criteria are met. leastsquares regression will provide the best (i. in in (o)to (b). As was the casefor Eq. 13.anotherjustification dividing by n . (13.2 LINEAR LEASTSQUARES REGRESSION 297 are met. syTxQuantifiesthe spreadaround the regression line as shown in Fig.18).However. we have lost two degrees freedom. This is particularly (Fig. ot represenls improvement to the due linecregression. a "standarddeviation"for the regression line can be determinedas [comparewith Eq.2 because two dataderived estimatesas a1and were used to compute S.
therefore..$ . : Sr and the fit represents improvement. p l eo f i n e o e g r e s s i o w r t n ( o )s m o l 'o n d { b ) s n 'orge residroe'rors l It called the unexplained error that remains after the regression. : 0 and 12 : l. Compute totalstandard the deviation.I r.S.) (L. s.2. standard of the the error estimate.)' EXAMPLE 13. thecorrelation for 13.sometimes The differencebetweenthe two quantities. to vield .9 E r o r ' .(Ir')(Iy. . Thedatacanbe setup in tabular form andthenecessary computedin sums as . fit. /.)'J. S. signifying that the line explains100% the An variability of the data. where 12 is called the coefficient of determination and r is the correlation coefficient . the differenceis normalizedto S.3 Estimotion Errors theLineor of for LeqslSouores Fit Problem Stqtement.'2 : S'S. For a periect t. Becausethe magnitudeof this quantity is scaledependent. provement or error reduction due to describing the data in terms of a straight line rather than as an averagevalue. is.298 LINEAR REGRESSION o o I3 FIGURE . of \: J r)). quantifies imthe sum of the squares. no is alternativeformulation for r that is more convenientfor computerimolementation r: nf(xiri) .? (I r. and coefficient thefit in Example lrlyl'?: . For 12 : 0.
20)l by I 1 .8805 l .297 4.g . Draper and Smith ( 198l) provide guidanceand additionalmaterialregardingassessment resultsfor linear of regression.79 Thus./.r( s. because sr.579 8.1tB The standard deviation [Eq. should be careful not to ascribe you nation more meaningto it than is warranted. 808.I 3.at the rninimum. 8 3 7 8 9 .ao .444 216.19)] is : 189." For example. 0 h four datasetsconsisting I I datapointseach.Although the coefficientof determia provides a handy measureof goodnessof'fit.Although their graphsare very different. (13. 8 0 8 . Theseresultsindicatethat 88. A n i c e e x a r n p l e w a s d e v e l o p e d b y A n s c o m b e ( 1 9 7 3 ) .441 1.you shouldalwaysinspecta plot of the dataalong ln with your regression curve. addition.05clc the orisinal uncertaintv hasbeenexplained the linearmodel. .t)2 (!i .26 and the standard errorof the estimate [Eq.808.3)] is : 508. 0 6 6 t .454 39 58 t55t2 349 82 544.2 LINEAR LEASTSQUARES REGRESSION 299 TABTE13.699 B ]. 3 23 3 380. .52 735.535 327. is possible obtaina relativelyhigh valueof r'l fit is necessarily it to when the underlying relationshipbetweeny and .0r6 3 3 42 2 9 3 53 9 r 6 53 .. ] ..9383.88[5 : 0. (13. 1 1 8 92 : 0.5r. The extentof the improvement is quantified [Eq. 2.)': 3 + 0.I B O 16.the linear regression model has merit.all of have the samebestfit equation.t2863 1 . and the samecoefficientof determination.245 9il 30 I6.r is not even linear.a$)2 l 2 3 4 5 6 7 B L t0 20 30 4A 50 60 7A BO JOU 25 7A 380 550 610 t 224 830 I . 297 of or r : \/0. ( 13.171 7. word of cautionis in order.441 68.5 Dotoond summotions needed compute goodnessoffit to the stotistics f o r t h ed o t of r o mT o b l e 3 . 1 li eo i atxi (yi .e c a m e u p w i t h si . Justbecause is "close" to I doesnot meanthat the r'2 "good.23 9 3 39 3 I..7 1 6 . A1 3n lF i. by Before proceeding.
in are appropriate. equation (for B2 l0)is in the nonlinear.22) where a1 and B1 are constants. techniques such as polynomial regression.vand x.It is very frequently used to fit experimentaldata when the underlyingmodel is not known. I3. l O t3 Anscombe's doto olong ihebestfit .: a2xP) ( 13. 13.11b. This is not always the case.populationgrowthor radioactivedecaycan exhibit suchbehavior.For example.transformations be usedto expressthe datain a form that can w iscompatible ith linear egression. For others.23) wherea2 andB2 areconstant coefficients.and the first step in any regression analysis shouldbe to plot and visually inspectthe datato ascertain whethera linear model applies. This model haswide applicabilityin all fields of engineeringand science. Another exampleof a nonlinearmodel is the simplepower equation: ).11a. As depicted Fig. This model is usedin many lields of engineering sciand (positiveB1 or decrease enceto characterize quantities (negativeB1 at a rate that increase ) ) that is directly proportionalto their own magnitude.y: 3 + O.14.However. which is described Chap.the equation representsa nonlinearrelationship(for fu l0) between. In some cases. it is predicated the fact that the relationship on betweenthe dependent and independent variables is linear.300 LINEAR EGRESSION R t3 t:) 10 10 10 F T G U R E.3 TINEARIZATION NONTINEAR RETATIONSHIPS OF Linear regression providesa powerful techniquefor fitting a bestIine to data.5x four sets with Jine.As depictedin Fig. r One exampleis the exltonential model: ! : olreP" FIGU {o)Th (fJore (l3. 13. .
l3. . Then linear regression to equations data. ond Theerponentiol equotion.r_ D r+ .1.This model.\':0. (c)thesoiurotrongrowthrole lol i tronstormotions.a simpleralternative to usemathematical can be employed to fit the form the equationsinto a linear form. It neeringand science.I 3.However.ond equotion." x nonlinearrelationship y particularlyin biologically relatedareasof both engiincreases.3 LINEARIZATION NONLINEAR ELATIONSHIPS OF 301 :: azxQl (dl GI (f) HGURE I r3. Nonlinear regressiontechniquesare availableto fit theseequationsto experimental to is manipulations transdatadirectly. which is particularly wellsuited for characterizingpopulation growth rate under limiting conditions. r (1. thepowerequolion. (b) Ports {e).2+) where a3 and B3 are constantcoefficients. has many applications.1lc) that levelsoff. versions these lhct lineorized oi equolions resu tromsimple lllore equation: A third exampleof a nonlinearmodel is the saturationgrowthrate J .r (d). or "saturates. also representsa as between and r (Fig.
l li log ri IogJi (log r. (13. a plot of 1/y versusl/x will be linear.2t of Thus.732 5.22)can be linearizedby taking its naturallogarithmto yield l n y : l n c v r* f r x (13.162 3. 13.886 3. 1 l / ) .21) a3x of Thus.26) Thus.t6r lu) ) I 000 I 693 2.4 EXAMPLE Equotion Fitting Dotowith the Power transto ProblemStqtement.23) thedatain Table13.AAl 3Bll 4 390 4. l3. However. I 220 330 440 550 660 774 BBO t0 25 7A 380 550 610 l22A 830 I 450 000 301 477 642 699 L778 I .24)is linearizedby inverting it to give 1 I I qS ful +' (13. a plot of log y versuslog x will yield a straightline with a slope of B2 andaninter' this cept of log cv2fig. Fit Eq. Equation(13. a plot of ln y versusr will yield a straightline with a slopeof B1 and an intercept ln o1 (Fig. thesemodels can be fit with linear regression evaluate the constantcoefficients.182 2 567 2.Ile). logy.488 5 386 6016 I ilbn .302 LINEAR REGRESSION For example. The datacan be set up in tabularform and the necessary Table 13.1usinga logarithmic formation. They can then be transformedback to their original stateandused for The following illustratesthis procedure the power model.845 L903 tlouo 1 398 t B45 2. The meanscan be computedas . for predictivepurposes.622 /0 ) 1a r 398 2. (13.584 2. as done here.6.23) is linearizedby taking its basel0logarithm to give logy:loga2*B2logx (r3.919 3.744 2.Eq.with a slopeof Bzluz and an intercept 1/o3 ( F i e .:Yy:1. to In their transformedforms. Equation(13.785 3.ld). 13.the base10logarithmis most commonly employed.6 xi t: ?Tr :2'5644 needed fit the powermodelto thedotofrom to Dotoond summotions r l l l o b l el 3 .086 2.5i5't TABTE 3. Note that any base logarithm can be used to linearize model.404 3.1 3 .)2 log x. as sumscomputed in Solution.
r( 1 2 .0 .( 1 3 . 6 0 6 1 2 0 l 5 ) . 13. I 2 E fir l3 Leost squoresof c po*ermodel thedoto lo from Toble I {oJ fitof theli'onsfoi'med lhe doto (b) power The fit with dolo. 5 6 4 4 1 . 5 6 2 0 The leastsqLrares is fit l o g r ' : . 1 6 ) s d a al : 8 ( 3 . 9 8 1 2 ( 1 . 8 ( 1 0 .0 .9842 1 c t l : 2 . 5 1 5: ) . equotionolong the ( The slopeand the interceptcan then be c a l c u l a t ew i t h E q s . 1 5 ) a n d 1 3 . 6 0 6 ) 2 6 :1. 9 8 4 2 l o gr The fit along with the datais shown in Fig.I3. . 5 1. 6 2 2) I 2 . 5 6 2 0 t l .12a.r .3 TINEARIZATION NONTINEAR OF RELATIONSHIPS 303 ri log v 1000 100 log.r ) 1600 1200 800 400 0 40 (b) 60 80 F I G U RI 3 .
the leastsquares is fit e8a2 F :0.rst to we ernphasize inthe troductory nature of the foregoing materialon linear regression. . In this section.r'andr. knowledge fromthe field you are studying often has a large bearingon the choice of the appropriate mode.4 C OMP U T E R A P P L IC A TIONS Linear regression so commonplace is that it can be implementedon most pocketcalculators.304 LINEAR EGRESSION R We can also display the fit usingthe untransformed coordinates.4 (Fig. 13. For example.it is known from the discipline of fluid mechanicsthat the drag force on an object moving through a flLridis often well described a model with velocity squared. do this. =1.Further.the first assumption rneirns that (l) the "r valuesmust be en'ortiee (2)the and regression t'versusx is not the sameas r versus1. vslusr for a given x must be normally distributed. y e q u a t i o n o u u s ef o r c u r v eI ' i t t i n g . Eachx has a fixed valuel it is not randomand is known without error. The I' Such assumptionsare relevant to the proper derivirtion and use of regression. The fits in Example 13.9942.2741 andB. 3.1 Genercrl Commenlson Lineor Regression Beforeproceeding curvilinearand multiple linear regression.2J4lut This equation. The yvaluesare independent randomvariablesand all have the samevariance. 13. alongwith the data.6)usinglinearregression the untransformeddata on Although both resultswould appear be acceptable.somestatistical assumptions areinherent that procedures in the linear leastsquares are l.12)should be comparedwith the one obtained previouslyin Example13.we will show how a simple Mfile can be developed determine the fo slope and interceptas well as to createa plot of the dataand the bestfit line. cierrts the power nrodelare determined cy2 19 05610 of oS : Using fbrceand velocityin placeof .2(Fig. transformed to the resulthastheadvantage that it doesnot yield negativeforce predictionsat low velocities.is shownin Fig. We will also show lrow linearregression be implemented can with the builtin po ly f i t f unction. 13. 2. I3.3.thecoeffiTo :0.12b. You are urgedto consultother of referencessuchas Draperand Smith ( 1981)to appreciate aspects nuances regression and of that are beyondthe scopeof this book.You shouldbe cognizant to ofthe fact that there are theoreticalaspects regression of that are of practicalimportance are but beyondthe scopeof this book. mr. have focused the We on simple derivationand practicaluse of equations fit data. by Thus. t3. For example.
y) 0.15)and(13.13).2: in >> x = 110 20 30 40 50 60 70 B0l. A simple exampleof the use of this Mfile would be to fit the forcevelocitydatathat was analyzed Example13.4)by applyingthe Iosl0 function to the data as in >> linregr (log10 (x). 4 1A 2 .1 MATLAB Mfile: linresr 305 An algorithm for linear regressioncan be easily developed(Fig. 13.4 COMPUTER APPLICATIONS 13.2 3 4 .I 3.the coefficient of determination.9481 0. Theseare then used to compute slopeandthe intercept the with Eqs. 2 8 5 1 1600 1400 1200 1000 800 600 400 200 200 10 80 It can just as easily be used to fit the power model (Exarnple 13.4.16).(13. > > y = 1 2 5 1 A 3 8 0 5 5 0 6 1 0 1 2 2 0 8 3 0 1 4 5 0 1t >> linregr (x.5624 7.The required summationsare readily computedwith MATLAB's sum function. The routinedisplays the interceptand slope. and a plot of the bestfit line along with the measurements.1oql0 (y) ) 0. BB05 r 9 .9842 .
and incercept. T2 = ((n*sxysx*sy) /sqrt (n*sx2sx^2) /sqrt (n*sy2.o 1. end x = x(:).'2) .Sum(x.yp) g r r . % 12 = coefficient of determination a(2) n = length(x).*y). % create plot of data and best fit lrne xp = lanspace (min (x) .4 t.id o n .t 1.).y): Least squares fit of straight ? line to data by solving the normal equations Z input: ? x = independent variable y= Z dependentvariable Z output: Z a = vector of slope. plot(x.. ? convert i_o column vectors ( S X = 3 s 1 1 tx ) .5 t. a(1).. y = y(:).. error(. 12l = linregr(x.306 LINEAR EGRESSION R 3.5 2. lineor function Ia.5 1.8 FIGURE . 12 ] = linreqr (x. a(2) = sy/na(1)*sx/n. a (1) = (n*sxysx*sy) / (n*sx2sx. if length(y)=n.o. sy2 = Sum(y. yF a(l)*xpa(2).x and y musf be same length. xp.*y).sy^2) ^Z ) . sxy .y) % linregr: linear regression curve fitting % 1.I3 I3 AnM{ilelo implement regression. S y = S u m ( y ) .*x). sx2 = srtn(x.y.2) .max\x\ .
5 CASE STUDY 13.. 4702 23 4 .2857 Thus. prodone into the . r 1 Because straightline is a firstorder a polynomial.28) where u : the initial reaction velocity.1) I9 .2 MATTAB Functions: polyf ir ond polwal 307 MAILAB has a builtin function polyf it that fits a leastsquares nthorderpolynomial to data.. theyconvert chemical substrate.It has the seneralfbrmat: >> y = polyval (p."1' 1 rr .We should note that it represents polynomial using decreasing the powers of r as in the following representation: f ( x ) : I t t x " * p 2 x " ..q. another. The graph also illustrates that the halfsaturation constant correspondsto the substrateconcentration at which the velocity is half the maximum.45) the bestfit value at x..the uct. . . 13. t) will returnthe slope and the interceptofthe bestfit straightline. olld n : the order of the polynomial.TheMichaelisMenten eqvation commonly is usedto describe suchreactions: u u_ [.As in Fig. u: the maximum initial reaction velocity. is Another function. x) wherep : the polynomial coefficients.S].In mostcases. ."fl. can then be used to compute a value using the coefficients. the slopeis 19.y. polyval. the equation describes a saturating relationship which levels off with increasing [.and y: >> y = polyval(a.s] k"+ [s] (13.. 125 7A 380 550 610 1220 830 14501. and ft" : a halfsaturation constant. : d . .The function returnsa vectorp containingthe polynomial's coefficients.2857.t+ . .I3. it}fS9. For example.i] : substrateconcentration. [.y . respectively.4. polyf it (x.It can be applicdas in >> p = polyfit (x.4702and the inrercepr 234.  E N Z Y ME IN E T IC S K Bockground.!l. B 75 A . y. Enzymes as catalysts speed the rateof chemical act to up reactions in living cells. >>x >>y >>a t10 20 30 40 50 60 70 B0l. . 6 4 7 . + p n x* p n .14. n) wherex andy arc the vectorsof the independent the dependent and variables. potyf ir (x. .
. model Equation(13.3 o. 13.a7 IB 0. 8. the following secondorderversion often results in a bettu fit: 't):   u. Solution.27) k.35 036 Employ linear regressionto fit this data with linearized versions of Eqs.2D. it has been fined and extended to incorporate additional features of enzyme kinetics.T4 I3 model kinetics.29). One simple molecule where the binding of a substrate extension involves socalledallosteic enzymes.. 13.For cases at one site leads to enhancedbinding of subsequent two interacting bonding sites. as depictedin Fig.14.the squared concentrationstend to make the shapemore sigmoid.28)and (13.assess the validity of the fits with both statistical measuresand graphs. with molecules at other sites. Supposethat you are provided with the following data: tsl U 1.308 LINEAR EGRESSION R continued Secondorder MichaelisMenten model 0.1 3 3 0. I . 33 5 B9 0.[s]2 ft"2 [S]2 + (t3.5u.29) This model also describesa saturatingcurve but. k" FIGURE .28). Two versionstheMichoelisMenien of enzyme of reAlthough the MichaelisMenten model provides a nice starting point. Aside from estimating the model parameters. (13.22 d a7 F. can be linearizedby inverting it to give (recallEq.which is in the format of the saturationgrowthrate (Eq. or Sshaped. 6 0 .
/ v ) . 1.28) can also be linearizedby inverting it to give I I k? I +j_ u D. If this is correct. poorness the fit is alsoreflected the of by statistics like the coefficient of determination.I3.13 can then be used to determine the leastsquaresfit: >> S=11.the fit is obviously unacceptable. For example. ii{. [Sl2 The linreqr function from Fig..ryifiru::ri.36 or 0.. the maximum velocity 15'2510)is much greaterthan the highestobservedvelociry tb.l 5 0. >> v=f 0. 16.j.{S=\. 13. u..5 CASE STUDY .3t. 309 :i continued function from Fig. Equation (13.2260) is much bigger than the maximum substrate concentration (9).2?. ..I5ta shows the transformedversion. the halfsaturation tate (86.9t4l The model coefficients can then be calculated as >> vmIra(2) \ rrr i: aRral ( 1 j ':' . the bestfit model is . j. 13.19u2 tl.0. The data is clearly leveling off at about 0.8 3 4.37.i:.2ja A.25701S1 86. the dataclearly appears be curved.6406 is obtained. For the untransformed case.sol. .15b). In addition. Figure l3.a much less acceptable result of I .Although the straightline iollows the upward trend._ 5. x. . r ' 2 l = L i n r e g r ( 1 . The problem is underscored when the fit is plotted along with the clata. 13..2260 [s] + Although the highralue of 12might lead you to believe rhat this result is acceptable. r .1n*J ks= 3 r :. O.Ll 0. an eyeball estimatewould suggestthat u^ shouldbe about 0.3 1.iir 0. 2 : t6 ( ) Thus.5 G 8 9l.1 . shouldbe in the rung.36. inspection of the coefficients might raise doubts.l7iiO i:l = ? . The foregoing analysiscan be repeated the second_order for model. 4 4 9 ) .07 0. and ft.4A22 12= The linreqr 0. > > L a .When the original equationis plotted to along with the data in the untransformed version (Fig.. of 2 to 3. Beyondthe visual evidence.13can againbe usedto determine the leastsquares fit: l9.
4083ts12 u:7. the fact thatit is very withthe values also seemconsistent high (0.6 a 0. The model coefficients can then be calculated as >> vm=I/a(2) 0. that is.29) gives 0.3to LINEAR REGRESSION #i*.4 o.2 0 isl I3 FIGURE .4083 >> kssqrt(vm*a(1)) 2 . the parameters trends in the data.4 0. BI?.8 0. in how when the fit. plotin of viewed iheuntronsformed.i .r1 (b) Originalmodel 0. the ft. (a) Transformed model 0. {o)shows tionsformed ond{blshows lhefit looks oilgrnorrorm. In addition.5 14. (13.I5 The model with fit{line) Plots leoslsquores of theMichoelisMenten olonq doto(points}.is slightly greaterthan the highest observedvelocityand stthcfrqfc nnnnanfrofinn /o\ .91r+[s:p of Although we know that a high I doesnot guarantee a good fit.9929) is promising.1 Substituting these values into Eq.
we should never solely rely on statistics such as I as the sole goodness offit. First.9896.4 0.I6 T MichoelisMenfen olono doto model with Plots leostsquores of thesecondorder fit(line) of fit.16b). plotin (o)shows trqnsformed ib)shows untronsformed. This might suggestthat we are dealing with an allosteric enzyme. form graphically. Linear regression assumesthat the scatter of points around the .2 0. this does not always translateinto an acceptablefit in the original format. 13.I3.$fii:{:ifl (a)Transformedmodel t/$12 ( b )O r i g i n a l o d e l m 0. Beyond this specific result. And for caseswhere transformations are employed. 13. the goodnessof the fit is also reflected by the fact that the coefficient of determination for the untransformedcasecan be computed as I : 0. Finally.we can conclude that the secondordermodel provides a good fit of this data set. Based on our analysis.ond ihe originol the The {points).16a. regression equations shouldalwaysbe assessed basisofassessing graphically. The reasonthat this might occur is that minimizing squaredresidualsof transformeddata is not the sameas for the untransformed data. in Fig. 0. a graph of the untransformed model and data should always be inspected.the fit nicely follows the trend in the measurements. there are a few other generalconclusionsthat can be drawn from this case study.5 CASE TUDY S 3ll ffi'ru conrinued i.1 0 tsl FIGURE3. When the original equation is plotted along with the data in the untransformed version (Fig. Beyond the graphs. Second.the transformed As The adequacy the fit can be assessed of results appear linear.3 .lt+F. although transformationsmay yield a decent fit of the transformed data.
nRT to deterrnine onthe rt basis of this data.0 9.l 10.and (g) coefficient of variation.'i:.8 9. 3 10.'C p.3r2 LINEAR REGRESSION ":. and that the standarddeviation is the same at every value of the dependentvariable. As a consequence the last conclusion.25 3 r .65 29.1Using the same approachas was empioyed to derive Eqs.nonlinear regressionshould be employed to fit curvilinear data.65 2'/.25 26. The volumeis l0 mr.8s 28. . " ':.Fit the following data with this model and display the result graphically.5.2 7. 13.4 10. 14. of (i) Assuming that the distribution is normal.Interpretyour results. Theseassumptionsare rarely true after transforming data. the lower and the upper values) that en68Vo<>f readings.55 29. FoI example. (e) standarddeviation.15)and (13.65 27.1 y O. and that your estimateof the standard deviation is valid. a bestfit curve is developed that directly minimizes the untransformed residuals.8 Fir I natural 13. t0ill41720 876912 Determine (a) the mean. 13.9 9.65 28. (13.4 9.6 The following datawas gathered determine relato the tionship betweenpressureand temperature a fixedvolof unteof I kg of nitrogcn. continued Lineariz on the f( the data.65 28.45 27.8 10. de 13.4 9.derivethe leastsquales of the fit following model: \tn1r'lp Along with the slopeand intercept.l Given the data f (hr) c (CFU/ 8. x 0 . (d) range. 13. (c) mode.Nlm2 aO t 2A 0 40 B0 160 6 9 0 0 B1 0 0 9 3 0 0 1 0 .4 8.4 11 .'75 21.I must be expressed in kelvins.65 26. 13.l Given the data regression fit a straightlineto to 13.EMS l3.6 9.5 l0. (c) mode. (h) Constructa histogram. the Determinewhetherthis compasses is a valid estimatefor the data in this problem. lJ.65 27. 5 0 0 I 1 .65 26. ' 'i '". l3.8 10. (f) variance.10 F (Eq.2 Construct a histogram from the data from Prob. 13. l3 model: 28.65 27. Then repeatthe problem.9 8. In this approach.. Use a rangefrom 7.7 Beyond the examples in Fig.6 x 2 4 6 7 y4565 8.9 Tb area n is PROBI. detelminethe slope that resultsin the leastsquares fit for a straightline with a zero intercept.5 9..65 28.1 9.8 9.6s 27.'.05 28.U tion. computethe range (that is.8 10.65 28. T.ll.9 That is.someanalystssuggest of that ratherthanusing linear transformations.35 27.85 26. Plot thedata and the regressionline.5 9. (d) range.35 28.2 t0. (b) median. there areother models that can be linearized usins transformations. (b) median.5.16).^rep1r w(kgl A (m') Show t well.computethe standardenor of the estimate and the correlation coefficient.5 to 11.2s Determine (a) the mean.5with intervalsof 0.75 13.11( related ber of (kg) git Enrploy the ideal gas law pV . switch the variables.I.75 27.and (g) coefficient of variation. \) : a. 2 0 0 t 2 9 0 0 When t results parame (d').5 Use leastsquares x02469 y56764 ll I 12 t5 710 l7 l2 19 t2 The timr and the r estimate and (b) 2OOCF be cons impossi creases 13. bestfit line follows a Gaussiandistribution. We will describehow this is done in Chap.15 2'7. (f) variance.Use a range from 26 to 32 with increments 0.85 26. (e) standarddeviation.s5 28. Note that for the 1aw.but regress x versus}that is.0 9.35 28. E .
13.Test with When usedfbr curve iitting. Then use linear regression to estimate c.In addition.10 . Measurements a nunto on ber individuals height 180 cm and dillerent of of weishts (kg) valuesof A (m2) in the lbllowing rable: eive k 0.).usethe .ll On average.6 I 950 2 2900 23 3600 is in Ihetime measured hoursfollowing the end of the storrn ard unit CFU is a "colony lbrrning unit.8 a powermodel to the datafiom Table 13. In Show a power law A : oil' fits thesedata reasonablv addition. the baseIOversionto solve Prob. and (b) usesthe sDbptor functionto alsodisplaya plot of W(kS)7A 75 77 BO 82 84 87 90 the resicluals (the predictedminLrs measured versusr.r'ariiince.rrchoice of model should consistent the fact that negative concenrations be with ale inpossible that the bacteria concentrationah..r based follou. and ftn.22 (0.16 Modify the lrnresr funcrionin Fig. the r.3 . c'oli bacteriain a swimnrins uea monil.range.Develop a plot of your fit along rvith the data.ays and dewith creases time.15 Developan Mfile function to computedescriptive stafistics a vector of values. 13.rhisloglinr.tl.x and predict the growth rate at c :2 mg/L.PROBLEMS this model and use it to estimatear and 6. this equatior'l yields identical results the baser. Note that yor. along with the 12fbr the untransfbrmed model.11 an exponential modei to 4 . but the value of thLexponent to (lr) parameter will diff'erfrom that estirnated with Eq.ingdata. 2 2 t u c o m n l o na l t e r n a t i v i s to employ a base10 model: Plot the data and the equationon both standardand semilogarithmicgraphpaper. hasreportedthe datatabulated 13.2 I 500 1.ored is after a slorm: Cow Humcn Sheep Hen Rcri Dove 400 7A 45 2 03 0 16 270 B2 50 4B 1 /< 497 Fit l1.l. . Use a transformation ro linearize this equation.r. it gencralc huvc .4 t5 53 25 76 4 B9 13.{h4 B mL) c(CFU/l0O ls9O I 320 t6 2A 24 900 650 560 x v 04 800 08 975 I'. but use logarithms perform the transformations. 3r3 13.^a.Have the function detennine for and displaynumberof values. Have the function return the bestfit coefficient a2 and powerB.12 Determinean equationto pledict metabolismrateas a function of massbasedon the followinc data: Animol Moss (kg) Metqbolism (wofts) 0t 02 04 06 c) 9 t3 t5 17 tB a/5 125 145 125 085 055 035 028 0 rB Fit 13. /< : Krnaxc.rpr ic fnr r Q5_Lo nersrrn data.median.17 Develop 2 an Mfile function to fit a power model. and predict what the the transfbrmed and untransformed equations along with the orrf. It c is known that such data can be modeled by the fbllowing equatiou: .. il wilhlhcrl. c 13.. cr+c' t a where c. Evaluate the conslantsa and b.r Trrsl lromProb. e { E qI.mode. to nafural The of 13. a l3.3.In addiUse develop formulation to relateut ro frs tion.jubp l ot function that to displaygraphsof both well. 13.l3 so that it (a) computesand returnsthe standard error of the estimate. 13.5 tt OB 2." Use this data to the (a) estimate the concentration the end of the storm (t: 0) at (b) and the time at which the concentrationwill reach 200 CFU/100mL. mean. and ftnra\are parameters.14 An investigator below fbr an experimentto determinethe growth rate of bacteria ft (per d) as a function of oxygen concentration (mg/L).) A ( r n 2 l 1 0 2 t 2 2 t 5 2 2 A 2 2 2 2 2 3 2 2 6 2 3 0 13.and coefficientof variation.the surface area A of human beings is rclated weight W and height H.9. Rather than using the basee exponential nrodel 13.9 concentration E.version. standard deviation.
and p is the specrfic growth rateof the bacteriaduring exponential growth.5 l2 312 l o n e W i d r hm 2 9 2 ..: AT A0le A Time. voirs dependson accurateestimatesof water flow in the We assumethe reaction occurs via a complex mechanism river that is being impounded.. 13. ICells].L^ f:r^ ^f rL.20 lt is known that the tensile strengthof a plastic increases a tunction of the time it is heat treated. 1 2 . ^f rli^ .meteorolovical of data on precipitationis often avail dAldt (moles/Us) A (moles/t) r (K) 46A 960 2485 l600 200 150 50 20 2BA 320 45O 500 14.l^.82. Dislonce. . and E.22 Concentration minimum lanewidth.andthen they are inducedto producea recombinant protein.ltl The tbllowing data shows the relationshipbetween the viscosityof SAE 70 oil and temperature. 52 9 1 5 .. m3/s s" Number of Model Porometers Fif 135 2 r05 3 13.26 "t) 10.3r4 LINEAR REGRESSION (sl& are shown beloll.21 The following data was taken from a stirred tank recngineeringstudy was conductedto 13.For some rivers.65cn alloy. (c) If the minimum safeaverage distance between bikes and passingcars is considered be 2 m.. Model A Model B l3.56 0 012 OOoO/5 ModelC i00 determir This rela when or followin Procipifqtion. 3 2 1 I B 2 7 1 . responding polyrnerization reaction: 11.Severalmodelshavebeenhypoth.s/m2 26. fbr the following on bikelanewidths and average distance between bikes and kineticmodel: passingcars.. T e m p e r o t u r e .. TL.use linear regression find the equationof to the line thrt bestfits the dataand the r2 value.Datawasgathered the best possible estimatesfor ft.00198kcal/mol/K.24 A transportation actor for the reactionA . Te tween stl Slroin.4< t0 550 The stres force in r value car the mast mast's le estimate 13.67 93 33 I 35 O0B5 I48 Bq 3I5.23 Below is data taken from a batch reactorofbactenal growth (after lag phasewas over). (a) Plot (b) Fit r Supt (c) Use the 1 (d) rf th of th ratio use.. (b) Repeat tl.:. 13. Basd on tbe data.m 24l524IB1.the sizing of reserxA*. After taking the log of the data.reanalysis but for a straight line with a zercr S l L 0 1 0 0 0 3 3 2 t t 0 2 I 6 4 4 2 453 3 660 5 460 intercept..1^r^J f^ . 13.27 Tl that mee voltages V. where R is the gas constantand equals0.and the sum of the squares the residualshad been contrast.The folas lowing data is collected: '4: dt r.B. longterm consistingof many steps.estimatethe specificgrowth rate of thebacteria duling the tirst 2 hoursof growth and during thenext4 houm of erowth. to the points for the datawas takenat l5 time 13.historicalrecordso1'such flow data are difficult to obtain.. cm/cm Stress. Add this line to the plot.1. (a) Plot the clata.19 You perform experimentsand determinethe fbllowing valuesoi heat capacityc at varioustemperatures for a Z gas: T5030060 l25O I 280 c I 350 I480 90 I 580 ilO l70A 13.25 In waterresourccs engineering.N.Which model bestdescribes data the tistically)?Explain your chorce. Flow.B:.x Time l0 )5 2A 25 40 50 55 60 Z5 TensifeStrength 5 2A I B 40 33 54 70 6A 78 where X is the number of bacteria. The theoretical growth of bacteriacan be described by Use regression determinea nrodel to predict c as a functo tion of I. (b) Fit a straightline to the data with linearregression. Use the data to determinethe dctL'rmine properdesignof bike lanes.The clatafrorn I I streets is dA . '. N/<m2 (a) Fit a straight line to this data and use the equation to h0t23 determinethe tensilestrengthat a time of 32 min. . The bacteria allowed are to grow as fast as possiblefor the first 2..5 hours.'J.tB . In esized.' € Viscosiry p.A. pro the duction of which slows the bacterial growth significantly.V irA (a) On cur an{ (b) Re ^^l^.1. determine cor.
A 705r0203040 p I 7B7 I . the The 13. The 13.The test resultsare and 11. T"Elongotion 75 86 87 lO ll3 475 600 127 153 (a)Plotthedata. Use your resultto determinethe shearstress (du/dy) at the surface.6529 2 52 3 7B 4 lo7 5 t3 7la 1 93 27.5 (a)0n thebasisof a linear regression this data.5 V. Predict the 7o following is availabletbr a river that is to be dammed: elongationfor a temperature 400 'C.30 The velocity r of air flowing past a flat surface is measured severaldistances away from the surface. data of ?ncipit o t i o nc m B B9 l 0 B 5 1 0 4 I . 0) Fit a sffaight line to the data with linear regression. a at l Fit curve to this data assumingthat the velocity is zero at the surface(y : 0).002 A 2B7 0006 0 899 o0l2 r9r5 00rB 3048 oo24 4299 thestress caused wind can be computedasF/A.519 I 3A7 I 002 O7 9 7 5 A.31 Andrade'secluation has beenproposedas a model of the elfect of temperatureon viscosity: P: PsB/r" where p : dynamic viscosity of water ( l03 N's/m2). the wind force is 25. Plot the line and the data for and evaluate fit. where L : the mast's length. 71 2 7 9 4 r 1 6 B 9 9 1 232 195 )61 II I 16. Employ an exponential model and linear regression make this prediction. and D and B are parameters. Superimpose line on your plot. estirnate what fraction of theprecipitationis lost via processes such as evaporation.000 N.5500 3590 6900 t24A As an engineer working for a utility company.27 following data was taken from an experiment measured that the cunent in a wire for various imposed voltages: 13. and consumptive use. you must forecast the population 5years into the future in order to anticipate the demand for power. mls 0.v i. this (c)Use besffit line to predict the annual water flow if the precipitation 120cnr.26 mast of a sailboat has a crosssectional area of cmr 10. J. absolutetemperature Fit this model to the following data for water: v. N/cm2 4970 5170 .determine of cunent a voltage of 3.h u. the . lbw m3/s 1 4 6 1 6 7 1 53 1 3 9 .PROBLEMS 315 determine a relationshipbetween flow and precipitation. deep groundwaterinfiltration.28 An experiment is performed to determine the Voelonthisrelationship then be usedto estimate can flows for years gation of electricalconductingmaterialas a function of temwhen only precipitation measurementswere made. 4 : (K). the (b)Redo regressionand force the interceptto be zero. 13. The perature. use the data to If estimate deflectionof a 9m mast. : mast'. were performed to define the relationshipbetween stress strain. crosssectional area.This value thenbe substituted can into Hooke's law to determine mast's the deflection. whereF : by in force themast and /.The resulting data is listed below. Al.29 The populationp of a small community on the outskirts of a city grows rapidly over a 2Oyear period: ta p 1OO 5 204 to 450 t5 950 20 2000 *" cm/cm 0 0 0 3 2 0 0 0 4 5 0 0 0 5 5 0 0 0 r 6 0 0 0 8 5 0 0 0 0 5 5lrcss.65 and is constructedof an experimentalaluminum Tests allo1.: strain x l. to 13. the is (d)If rhe drarnage areais I 100km:.6 oC 200 25a 300 375 425 Temperofure.
13.lrt.r+ a z r :+ e (14.Forthesecases.y:uotal. procedurecan be readily extendedto fit the data to a higherorder The leastsquares polynomial.As discussed Chap.acurvewouldbebetter suited to fit the data. 13. will then show how suchapplications be generalized applied a We can and to Finally. Understanding formulation of the generallinear leastsquares the model. Knowing how to implementpolynomial regression. one method to acconrplish in this objective is to use transformations. we will illustratehow optimizationtechniques be broadergroup of problerns. Specilicobjectives and topicscoveredare o o o o . GenerolLineorLeostSquores ond NonlineorR"gression CHAPTER OBJECTIVES This chaptertakesthe conceptoffitting a straightline and extends to (d) fitting a it polynomialand (D)fitting a variablethat is a linear function of two or more independent variables. Understanding how the generallinear leastsquares model can be solvedwith MATLAB usingeitherthe normalequations left division. Some data. For example. Knowing how to implementmultiple linear regression.l.I POTYNOMIAL REGRESSION In Chap.ispoorlyrepresentedbyastraightline.suppose polynomial or quadratic: that we fit a secondorder . a procedurewas developedto derive the equationof a straightline using the leastsquares criterion." l rol.f. Another alternativeis to fit polynomialsto the datausingpolynotnial regression.. although exhibiting a marked pattern such as seen in Fig. I4. or Understanding how to implementnonlinearregression with optimization techniques. can usedto implementnonlinearregression.1) .
orri)t \14. (14. : t il (y.) : .d1x. :: at .a1x1 .1)"' + (I'1) tL2: lx!)'i .a0 . .'t.xi ()l L r a2xl) \/ rtu.d0t aS.us. is of For this casethe sum of the squares the residuals II s.2) with respectto coefficientsof the polynomial.u7. the To generate leastsquares we take the derivativeof Eq.t^sa ) .?) oo+ (T. 2l \\r .\r ) \. d0t : /' .2) fit.". t'1*.tl) These equationscan be set equal to zero and reananged to develop the following set of normal equations: zz (n)ao+ It')ar + (f t''a) :Lvi ( (I r') oo+ (It.\'.REGRESSION POLYNOMIAL 317 (b) I4 FIGURE . rt .atxi . c'ltt a. as in eachof the unknown as.t) n' + (t ti) a2: lxiYi (Ir. .nu.I (b i h regression)l n d i c o i i otn o io p o r o b o l os for leostsquores (o)Doto is illsuited lineor ihot preferoble.
47 1 4 00 3 3.9 6t t 152. The following can be computedfrom the data: \'. .6 544 44 314.74657 0 I 2 j? 4 5 I 2l 77 t3.318 AND GENERAL LINEAR TEAST.SQUARES NONLINEAR REGRESSION where all summationsare from i : I through n.aoarx.aS)r 0. (13.. n .r< r)5477 I l'i : 152. TABTEl4...x'' I e Thus.1. anda2..'t J .r< /r ^t r J n:6 . twodimensional to casecan be easily extended an mthorderpolynomial as in + ! : aotdlx f a2x2 .l4. to polynomial is equivalent to recognize that determining the coefficients of an mthorder enot For this case.12 239... .wecan The foregoinganalysiscan be easily extended this more generalcase. is formulatedas s.we have lost z * I degrees freedom.we see that the problem of determininga leastsquares secondorder polynomial is equivalentto solving a systemof three simultaneous The linear equations.: 2488'8 . for of leostsguores Compuiotions on erroronolysis the quodrotic v.6 272 40.(m I l) because * l) dataderived o0. at. ctt. anwere usedto computeSr. Fit a secondorder ofTable 14.)t :585'6 L*?Y. The coefficientsof the unknowns be calculateddirectly from the observed data. a a. (Ji J) tg.. of fot In addition to the standarderror.20).1) 2 5 r 33 9 Soluiion.6 \'lss \ '.the standard solving a systemof m I I simultaneous linear equations. a coefficient of determination can also be computed polynomialregression with Eq. thus.22 1272.l xi fif.t 4332 r 00286 r 08r60 0 80487 9 0 61 59 0 09434 3. Note that the precedingthreeequations are can linear and have three unknowns:a0.(m l1) (14.3) (m coefficientsThis quantity is divided by n .l l s L Lx!:9tg I". For this case.1 EXAMPLE Polynomiol Regression polynomial thedatain thefirst twocolumns to ProblemStotement.
3)l  1. 4 1 8 6* 2 ..99851 2513. >> a = l.8507 These equations can be solved to evaluate the coefficients.Bl.i\r Therefore.l:hfx 2 . (14.3593 1. is FIGURE4.55 225 9j9). >> r = 1152.15 55 225. 3 5 9 3 r * 1 .74651 :0.. /+b) / :lll75 !orz+ l) ""' The coefficientof determination is r 2513. the leastsquaresquadratic equation for this case is ! : 2 .99925. 8 6 0 7 1 2 The standard error of the estimate based on the regression polynomial is tEq.41 B6 2.]4.the simultaneous linear equations are 319 'i.39 and the comelation coefficient r :0.: / _1.39 3.2 I Filof o second orderpolvnomiol .. tr iil'i.6 535.6 24BB. 1 POIYNOMIAL REGRESSION Therefore. using MMLAB: >> N = l6 15 55. For example.
(t0 a1xl.r.ond of rr.This resultsupports conclusion the that the quadratic equation represents excellent as is alsoevidentfrom Fig.i) . might be a linear function of r.r2.the "best" values of the coefficientsare determined by formulating the sum of the squares the residuals: of $ : i ()'i .i) FIGURE I4.320 LINEAR GENERAL LEASTSAUARES NONLINEAR AND REGRESSION These results indicate that 99. regression a As with the previous cases.azxz.0"i . 14.3 Grophicol depictionmultiple regression y iso lineor of lineor where function. or more independent For example. an fit.cttxt. For this twodimensional case.2 'T4UITIPIE TINEAR REGRESSION Another useful extensionof linearregression the casewheref is a linear functionof two is variables.t t 1 1 .t 1a 2 x z a s Such an equationis particularly useful when fitting experimental data where the variable being studiedis often a function of two other variables. the "line" becomes "plane"(Fig.. .1) and differentiatingwith respectto eachof the unknown coefficients: aS.ao . andxr.i .. .2.3).. x 1 . as.i . 14.t 1 1 .o 2 x 2 .851 percent of the original uncertaintyhas beenexplainedby the model.: OQ) 2 ^ \ ) . ( l ' i .2^ .ao(rtl. y : r r o { c t 1 .az.: d L ll 11 ) xt.:.i * azxz.i)? EXA (r4. ___!_ // . 14.rt.i()i . i l 1 as. asin 1 l .:l) dao _\r.
.r: (14.525 r30 463 7227 I t0 Use multiple linear regression fit this data..2. I"ti. The following data was calculated from the equation y : 5 + 4x1.5 100 (14.rilr 0 20 225 0 l2 t89 )77 x2y 0 t0 IB 0 ]B \. The foregoing twodimensionalcasecan be easily extendedto m dimensions./a.3r2'.?.. Ifoo f n I"'t.I4..2 MULTIPLE LINEAR REGRESSION 32r The coefficientsyielding the minimum sum of the squares the residuals obtainedby of are settingthe partial derivativesequal to zero and expressing resultin matrix form as the I*'.' I x r . Substituting them into Eq. iI l a r I [ L I tr. " + a r x ^ * e TABTE I o:? c:Tp':olio1'.rr.rt..5) Problem Sfotement. (14.r Ir.rt x2 00s 2 2. to Solution. 2 s4 8  I 48 54) L t4 which can be solvedfor (10:5 at:4 a0 Al A1 54 243.. The summationsrequired to develop Eq. Irr.:"". i r z .6) aZ:3 which is consistent with the original equationfrom which the datawas derived.as in * ) : a oI a 1 x 1 a 2 x 2 + .l) xl 0 l 4 I 36 4 J4 xrx2 .i: la'2 ! 50 t0 9 0t 34 277 a4 xr 2 25 xz 0 I 2 3 6 2 t4 x! 0 4 6. .oti:.1nt EXAMPLE Multiple 14.. (1a.4 o 2 5 3 24 l4 0) T6 lao .5)gives 16.5) are computedin Table 14.5 rll [ 6 1 6 .:'i':d dey3lon '::::l fo1 Elomele 1o li:.. I . 5 1 6 .25 I t6 49 .2 Lineor Regression l .. ::q. _.
zm € I (14.l .the :'s can be y : ao * a1 cos(a..To usemultiplelineat Are Suchequations extremelyusefulwhen fitting experimental by the regression.rl. As in the caseof sinusoids. all three belong to the following general model: leastsquares l''' ) ' : a o z oI a r z t * a y Z z I an. m s s i m p f e o n o m i a la si n : o on Note that the terminology "linear" refers only to the model's dependence its polynomial regression. I4. r : x . : : l . linear polynomial.20).t2= fall within this modelthat and multiple linear regression if polynomialregression alsoincluded the basisfunctionsate is x2. in as selvescan be highly nonlinear. . [::: a. ( 13..multiple linear regression additionalutility in the derivationof power equations the generalform of x.. x::i..' .. Further.3 LEAST SQUARES TINEAR GENERAT we have introduced three types of regression:simple linear. Znt:. a simplelookingmodel suchas ): do(l eol') into the format of Eq.7) canbe expressed matrix notationas [y]:fzl{a}+lel (14. In the preceding paqes. .t )' : ctox'.?) linear It where :0.r) I azsin(o_tx) Such a format is the basisof Fourier analysis. functions the them' parametersthat is." data.322 LINEAR LEASTSQUARES NONLINEAR AND REGRESSION GENERAL error is formulatedas where the standard s n(nt+D is and the coefficientof determination computedas in Eq. .marem * I basisfunctions. On the other hand. Z p 11 6 m . zr :. can easilybe seenhow simple is. .7).. z. wherea variableis linearly relatedto two or mole be Aithough therernary certaincases has other variables. zl. and multiple linear.t .xm. the a's. .For example. .:. ( 14.. In fact.8) EXA values valuesof the basisfunctionsat the measured where[Zl is a matrix of the calculated variables: of the independent zll t7l tt :*tf . . t t : x 2 . equationis transformed taking its logarithm to yield l o g . y: l o g a 0* a 1l o g x l * a 2 l o g x 2 + ' ' ' + a n l o g t . it is truly nonlinearbecause cannotbe manipulated in Equation(14.s: l.. .
0 ) It can be shownthat Eq. and S.  s.f"zlla} Matrix algebracan then be usedto manipulatethis vector to computeboth the coefficient of determination and the standard error of the estimate illustratedin the following example.t0 12 3 4 5l'. The column vector {y} containsthe observed valuesofthe dependent variable: {v}r : L. }nl The column vector ia] containsthe unknown coefficients: {a)r : la11 at a . usematrixoperations described this but as in section. . . 1 1 3 . and the data.enterthe datato be fit >> x .urt' I().S.1. Becattse > m * 1. . gives rl I(vr . > > y = 1 2 .ti t2 where! : the predictionof the leastsquares The residualsbetweenthe bestfit curve fit.polynomial. 6 2 ' 7. in fact.$  _ _S .l0) is. Substitutingthe definitionsof S. can be expressed vector form as in {y} . il / lo1r1.. I 1 .3 GENERAL LINEAR LEAST SAUARES 323 where m is the number of variablesin the model and n is the number of data points.3 P o l y n o m i oR e g r e s s i ow i t h M A T L A B l n ProblemSiotement.j.vr !2.9) \ This quantity can be minimized by taking its partial derivativewith respectto eachof the coefficientsand settingthe resultingequationequalto zero. \2 (14. Recallthat r: is definedas t S. you shouldrecognize n that most of the time.l j=tt / .r . 2 4 A .) and the column vector {e} containsthe residuals: {e}r : le1 e2 e u) The sum of the squares the residuals this model can be definedas of for n s.The outcomeof this process is the normal equations that can be expressed conciselyin matrix form as [ z ] r V \ { a } : { t z l r{ y } ) ( 1 4r. Repeat Example14.14.equivalenf the normalequations to developed previouslyfor simple linear. First. as EXAMPLE I4.:Ilt. The coefficientof determination and the standard error can alsobe formulatedin terms of matrix algebra. [Z] is not a square marrix. Solution. and multiple linear regression.y. (14. 1 1 ' . 9 6 1 .
5.998s and sr. (14. The matrix notation will also have relevance when we tum to nonlinear regression in Section 14.m e a n ( y ) ) .SQUARESAND NONI\NEAR RIGRESS\ON GENERAT Next.S r / s u m( ( y .24 139 7416 1 5 25 We can verify that LZf lzl resultsin the coefficientmatrix for the normal equations: >> z'*z 6 15 55 15 ss 225 55 225 979 This is the sameresultwe obtainedwith summations Example 14. can be computed as >> syx sqrt (Sr/ (lenqth (x) lenqth (a) ) ) 1.. first compute the sum of the squares of the residuals: . ^ 2 ) EX 3 .1175 Our primary motivation for the foregoing has been to illustrate the unity among the three approaches and to show how they can all be expressed simply in the same matrix notation.3593 1. ^ 2 ) 0.I .10).'t 466 Then r2 can be computed as > > 1 2 = I . > > S r = s u m( ( y .324 L\NEARIEASI. (1a.^2] 100 111 1.l0): coefficientsof the leastsquares >> a = \Z'*Z) \(Z'*y) 2 .7.8607 In order to compute 12 and s"7. . We can solvefor the in quadraticby implementingEq. It also sets the stage for the next section where we will gain some insights into the preferred strategies for solving Eq.Z * a ) . create the [Z] matnx: >> Z = [ones(slze(x) ) x x.47 B6 2.
but use the builrin polyf ir functionand left division to calculatethe coefficients.QRfactorization andsingular valuedecomposition.The following exampleillustrates E X A M P L1 4 . ( 14. howcan to ever.2). must be mentioned.> m). lA I2 1 2. for caseswhere you want to fit a polynomial. Ieastsquares.QR factorizationis automaticallyusedin two simple ways within MATLAB. to QR factorization obtainthe solution. 1 1 3 .. both these results are obtained autornatically with QR factorization. function can be used to compute the coefficients: >> a = polyf it \x. generallinear leastsquares the Recallthat the general modelis formulated Eq.3593 1 8607 As just stated. y i j t o n d L e f tD i v i s i o n P l n Problem Stotemeni. 9 6 I .4. I l ' [ones (size (x)) x x. problemcan be directly solvedwith the backSecond.V.4 AND OPERATOR QR FACTORIZATION THEBACKSLASH 32s 14.41 86 2. First. l ) In Section10. Solution.Although the descriptionof thesemethodsis beyondthe scope of this text. . numberof equations greaterthan For Eq. 6 2 1 . are more robustin this regard. (14.Such systemsare said to be overdetermined. 4 E l m p l e m e n t i n g o l y n o m i o R e g r e s s i o w i t h p o . Two more advanced methods.8) as slashoperator.2) 1.41 B6 The sameresult can also be calculated using the backslash: >>a=Z\y 2 .I4.4 QR FACTORIZATION AND THEBACKSIASH OPERATOR is Generating bestfit by solving the normal equations widely usedand certainlyadequate a It for many curvefittingapplications engineering in and science. the builtin polyf it function automatically usesQR factorizationto obtain its results. we mention them herebecause they can be implementedwith MATLAB. RepeatExample 14. I 1 .it automatically uses that how this is done. 1 y y: l Z l { a l ( 1 4I. 2 4 A . MATLAB senses you want to solvesuchsystems with left division. Further.3.we usedleft divisionwith thebackslash operator solvesystems linearalto of gebraicequations wherethe numberof equations equalsthe numberof unknowns(n : n).. it .tlrat the normal equations be illconditionedandhencesensitive roundoff errors.8)as derivedfiom general the is When the number of unknowns (n.the datacan be enteredand usedto createthe lZl matrix as in >> x = >> y = >> z = The potyf 3 4 5l'.3593 2. As in Example 14.8607 2 .3.
For example.the GaussNewtonmethod uses a Taylor seriesexpansionto expressthe original nonlinear linear form. theory can be usedto obtain new estimates the parameters of that move in the directionof minimizing the residual. p2. Recallthat in Example13.It has the generalsyntax lx.l by linearizationusing logarithms. v:ao(l rat"r)*e ( 1 4 . options..4).However.Employ initial guesses I for the of coefficients. . nonlinearregression basedon determiningthe values is of the parameters that minimize the sum of the squares the residuals.2741ut Repeat this exercise. As with linear least squares. useempty brackets[] asa arguments place holder.MATLAB's fminsearch functioncanbe used As described in for this purpose..but use nonlinear regression.5 1 N o n l i n e o rR e g r e s s i ow i t h M A T L A B n Problem Stotemeni..r3) An optimizationroutine can then be usedto determinethe valuesof ae and a1 that minimize the function. but do not want to setthe aptions. and pl. De(Chapraand Canale. (la.a t r ' 1 1 2 (t4. There are techniquesexpresslydesignedfor nonlinearregression. For example..5 NONTINEAR REGRESSION There are many casesin engineeringand sciencewhere nonlinearmodels must be fit to data.2 ) 1 This equationcannotbe manipulatedso that it conformsto the generalform of Eq.2002).3.326 LEASTSAUARES NONLINEAR AND GENERAL LINEAR REGRESSION I4. x0 : a vector of the initial guesses the parafor meters. xa.: additionalarguments that are passed the objectivefunction.p2.) wherex = a vector of the valuesof the parameters minimize the function f un.a o \ .thesemodelsaredefinedas thosethat have a nonlineardepenFor denceon their oarameters.the solution must proceedin an iterativefashion.. EXAMPLE 4. .In the presentcontext. .7).MATLAB uses to default valuesthat are reasonable most problems.l2) can be expressed an objectivefunction to computethe sum as of the squares: n f ( a q .for the of nonlinearcase.). f va7 = that the value of the function at the minimum. previously Sec.This yielded the model: e\a2 F : 0. p2. etc. ..7. Note that if options is omitted. tails on this approachare providedelsewhere An alternative to useoptimizationtechniques directly determine leastsquares is to the fit. example. fvaLl = fmlnsearch(fun.Eq.options: a structure containingvaluesof the optimizationparameters created as with the optimset function (recall Section6. Then leastsquares equationin an approximate.If you would like to passadditional for (p1.1. (14.a 1 ) : ) [t.pl.we fit the power model to datafrom Table l3.4.
it is difficult to judge which fit is superiorbasedon inspectionof the plot. 2. m. In commandmode.5384u1 Both the original transformedfit and the presentversion are displayedili'Frg.125 7A 380 550 610 1220 830 14501. ^ 2 ) .xm.14.is set up for the power equation: function f = fSSR(a.4359 tL x.a a a ta . an Mfile function must be createdto computethe sum of the squares. z { 800 Untransformed 4 40 u.5 NONLINEAR REGRESSION 327 Solution.[1. mizes the residualsof the transformed FIGURE I4. First. y) The besrfit model is therefore a35e F : 2.4. The following file.4 versus velocity from ond model for dcto Comporisontronsformed unlronsformed fits force of ODIE J a . m/s . f = s u m (( y m . >> y . This model using nonlinearregression versuslinear regression is because former minimizes the residualsof the orisinal datawhereas latter minithe the data. Note that althoughthe model coefficientsarevery different.^a(2).I4. This example illustrateshow different bestfit equations result when fitting the same employingtransformations.5384 r.ym) yp = a(1)*xm.y p ) . the datacan be enteredas >> x = t10 20 30 40 50 60 7O B0l. called f ssR. The minimization of the function is then implemented by >> fminsearch (@fSSR. 1] .
sinusoidalfunctions play a fundamentalrole in modelingsuch problem contexts.the results will be identical.ond 0 : r/3 rodions(b)An olternotive expression thesome of curve is in The components thisfunction depicted of ore * B. specifies the height of the F I G U R EI 4 . three I(r) : Ao + A) + A. The mean valued sets the average height above the abscissa.328 LINEAR LEASTSAUARES NONLINEAR AND REGRESSION GENERAL F IT T INS IN U S OIDs G Bockground. cl t.where : 0.There is no clearcut convention for choosing either function. Engineersand scientistsoften deal with systemsthat oscillateor vibrate.we will use the cosine. = ) . 5 . { o }A p l o t o ft h e s i n u s o i du n c t i o n } : A)o * C . will usethe term sinusoidto represent we any waveformthatcanh describedas a sine or cosine. In this discussion. 7 . C .sin{arol). f ol (r e.5 s). For this chapter.5a). : 0 866.As might be expected.cos{coor} in A. and in any case.Thesummotion thethree curvein {o). fad . ao: 2n/l 1.s at. of curves (b)yields single the {b).5 ond B. 14. c o s ( a r o r * 9 ) F o r t h i s c o sA n = 1 .14) Thus.which is expressedgenerally as * f (t) : As * Cv cos(arst d) (14. four parametersserve to characterizethe sinusoid (Fig.The amplitude C.
19)gives / B'\ 0 : arctan  \ Arl (14. ( 1a.r6) Although Eq.20) where if A r < 0.3 AM 27. It can be measuredas the distancein radiansfrom / : 0 to the point at which the cosine function begins a new cycle. it is awkward to work with from the standpoint of curve fitting becausethe phase shift is included in the argument of the cosine function. (14.I S 1. (14.6 CASE STUDY 329 continued how often the cycles occur. (14.8 ) (r4. 1 23.7). or phase shift 0 parameterizesthe extent to which the sinusoid is shifted horizontally.1 ^ 32. This means that it is not in the form of the general linear leastsquares model (Eq.7).3 r8.sin(cosr) Cr cos(abl + d) : C1[cos(rrrst) SubstitutingEq.17)into Eq. 4.Eq. and we must use nonlinear regressionto estimate the coefficients.14)and collectingtermsgives (Fig. The angular frequency (in radians/time) is related to frequency/(in cycles/time) by oo :2r.r7) Dividing the two partsof Eq. U. (14.4 23. Finally.5b) * f (t) : Ao * Ar cos(alol) 81 sin(arer) where Ar : Ct cos(0) Br : Cr sin(9) (14.8 A J 3 7 .2r) an formulationof Eq.19) ( 1 4l. Aizona. it fit.I4.9 . the oscillation. Thus. 14. 2 3 6 .19) leadsto (14. add z to 9. Squaringand summingthe two partsof Eq.f and frequency in turn is related to period I (in units of time) by I (14. can be simply applied as the basis for a leastsquares The average monthly maximum air temperaturesfor Tucson. (14.18)represents alternative four parametersbut that is cast in the format of a general linear model (Eq. The angular frequency o. (14.14)that still requires Thus.J lB9 F 2 1 . have been tabulated as Month T'"C .15) rT (14. This deficiency can be overcome by invoking the trigonometric identity: sin(p)] cos(p).4characterizes phase angle.14) is an adequatemathematicalcharacterizationof a sinusoid.2 OND 2e.
The coefficients for the leastsquares fit can then be computed as >> a= lZ'*Z ) \ (Z'*T) 28.1. lZl matrix: >> >> >> >> vrO=2*pi/360. BA02 The statistics can also be determined as >> Sr=sum((TZ*a) . generally describes the data trends.Tp) The fit.14): >> theta=at. which is displayed in Fig.3 18. Equations (14.3 27.2 36.m e a n ( T ) ) .9862 >> syx=sqrt (Sr/ (length(t) syx = 0. In a similar fashion to Example 14. t=t15 45 75 105 135 165 225 255 285 315 3451'. we can enter the data and createthe 23.20) and (14. fit a sinusoid to this data.an2 (a (3 ) .T. Assuming each month is 30 days long.r 23. 34.S Q U A R E S N O N T I N E A R E G R E S S I O N L L AND continued Observe that the July value is missing.8972 lengch(a) ) ) A plot can be developed as >> tp=[0:360].2 . 14. tp.3. r=118.4 29. (14.'o'.^2) 6 .91'.330 G E N E R AL I N E A R E A S T . ^ 2 ) 0. a (2J ) *360 i (2*pi ) theta = L63.21) can be used to express the bestfit sinusoid in themore descriptive format of Eq. >> Tp=a (1)+a (2 ) *cos (w0*tp)+a (3 ) *sin (w0*tp) .4 z= [ones(size(t)) cos(w0*L) sin(w0*t)].r6'76 . >> plot (t.2s59 .9 2r.381 B 9 . Use the resulting equation to predict the value in midJuly.8 32. Solution.6.4398 >> r2=ISr / s u m( ( T .2 31 .
1 'C.t r + 1 6 3 . the final model is /2n \ R : 2 8 . and the amplitude is 9. 6 7 0 2 c os* . the phase shift is expressed in days rather than in radians.6702. 3 8 7 8+ 9 . 1 6 76 ) \Jbu / We can see that the mean is 28. Therefore. The phaseshift tells us that the peak temperatureoccurs about 163 days prior to the start of the year. The value in midJuly (/ : 195 d) can thereforebe computed as > > T p J u l = a ( 1 ) + C 1 * c o s( w 0 * ( 1 9 5 + t h e t a ) ) TpJul = 38. fitof to doto Arizono >> C1=sert (a(2 ) ^2+a (3) ^2) 9 .CASESTUDY 331 continued 15 300 350 FIGURE I4.6 Leostsquoreso sinusoid temperoture forTucson.3878.0530 . the maximum monthly temperaturein Tucson rangesfrom about 18.67 A2 Note that because it is more physically meaningful. Hence. which translates to about day 197 (July 17).7 to 38.
a Dissolved Oxygen (mglt) for Iemperolure ('C) ond Concenlrotion of Chloride (g/t) T. solar r .6.6 Use multiple linear regressionto derive a predictive equationfbr dissolvedoxygen concentration a function of as temperature chloridebasedon the dataliom TableP14.9 It3 r0 t 903 B.7 As comparedwith the rnodelsfiom Probs. Use the general linear least. 14. determinethe coefficientsthat reslllts in the leastpolynomial with a zero intersquares for a secondorder fit cept. Test the approach by using it 1(Jfit the data fiom T a b l e1 3 . That is.e t27 where maxin radiati UtE n Iru. D : diameter.and S: slope./r(c) Estim and C t p(t) 14. 14.8 Use multiple linear regression fit to xrO1)223344 I x2 0 2 y 151 t7. and the conelation coefticient. Computethe coefTicients.5. "C 0 5 t0 c:OglL c=lOg/L c=20g/L l 2 3 4 5 6 7 B I 03 06 0. (13. Explain possible causes fot the discrepancy.S Q U A R E S N O N L I N E A R E G R E S S I O N T L AND PROBLEMS 14.6 Along with the coefficients. Use the resulting equadon to estimate dissolvedoxygenconcentration a chloride the for concentrationof 15 g/L aI T : 12 'C. standard the error of the estimate.a11D"' 5"' where Q : flow. t 2 + e That is.3 06 09 0 00t 0 001 0 00r 001 00r 0 0t 005 005 005 aa4 4.5 and 14.5 For the data from Table P14.rr P. a somewhatmore sophisticated model that accounts fbr the eflect of both temperatureand chloride on dissolved oxygen saturation can be hypothesized beingof theform as o: hG) *.15)and (13.24 069 0 13 a82 238 r 0.332 G E N E R AL I N E A R E A S T . of TABTE P14. 14. 14. m Slope.derive the leastsquares of the fit following model: _ \ ' : .16). t l i r+ a 2 .use polynornialrellession to derive a predictive equation for dissolved oxygen as lor concentration a function of temperature the casewhere the chlorideconcentration equalto zero..14.2 9 28 t) 3B )2 4.5. m/m Flow.ll) Three diseasecanyingorganisms decay exponentially in scawater accordingto the following model: .squares approachto fit tlis model to the data in TablePl4. lzl.4 Develop an Mfile to implement polvnomial regression.1.1 and fi I 1 2 2 2 I 256 205 35 I 297 454442 User follor .Employ a polynois mial that is of sufficiently high order that the predictions match the numberof significantdigits displayedin the table.1 Fit a parabolato the data lrom Table 13. Computethepercent relative error for vour prediction.determinerr and s. Compute the percentrelativeenorfor your prediction.Determine the r2 for the fit and comment on the efficacy of the result.09 mg/L. 14. 1 .7_.26 12. Note that thetrue valuc is 9. 14.3 Fit a cubic polynornial the following data: to Note that the true value is 9. Passthe Mfile two vectorsholding the r and ) values along with the desiredorder rn.2 Using the same approachas was employed to derive Eqs.3 195 566 14. 14. m3/s . and Use the equationto estimatethe concentration dissolved of oxygenfora chloride concentration 15 glL atT : 12"C. 14. teriz exan U( wher subs The lsl.9 03 06 O9 0.3.1.5 Dissolvedoxygen concentrotionin woier os o function of lemoerofure ("C) ond chloride concentrotion(g/L). C C C t l t( 5( 45 2B t3 r5 20 25 0r 909 B.09 rng/L. l4. b" r5( P9( 14.r34 16 J 36 578 44 34 2.9 Thc lbltowing datawas collectedfbr the steady of flow u a(erin a concretc cilcularpipe: Experiment Diomeler.5.rU. Test it by solving Prob. a thirdorderpolynomial in temperature a linear and relationshipin chloride is assumed yield superior to results..l7 746 A e5 114 t0 3 B96 BOB 735 573 6)O Use multiple linear regression fit the fbllowing model to to this data: Q .
ll Thefollowing model is uscdto represent efl'cctol the plants: r fadiation the photosynthesis of aquatic on ratc solar I P=P..8 we used transtbrmations linearrze 106 fit and thefollowing moclel: )' B ' . determinethe coefficientsthat resultsin the leasl squares tit tbr a secondorderpolyuomial with a zero intercept./: optimal solar radiation rsl).jLrstify. . Tr)r several regression estimateaa arrdp.r T a b l e1 3 .05rnL atm/gnlol K.The following is rn of reactions: example a model that is usedto fit srrch k.13 Enzymaticrcactionsarc usetl extcusively to characterize biologicallyrnediatedreactions.14 Givcn thc data I i I t l4. B.n1't g'.f': l]1.M uo. PV At At .9 7 1. M/s 6 O Z Bx l O ') 7. (b) Pcrforrn sanre thc evaluation in (a) bul usenonlinear as regrcsslon. lpEm based the fbllowing data: 1. and I : of R r03 K.12In Prob. 7 3 7x l O ' 2423v. where: the photosynthesis 1mg m 3dl). and 1. and (d) a parabola. (b) Use linearintcrpolatior.5 (a) Plot this data. s / m 2 . : 82..787 I5t9 t0 I3Q7 20 I002 30 4a a 7. Ii.18 Use the following setof pressurevolume to find data (A.. C) and siventhe followins mcaslrremcnts: r f\t 05 l AA '2 )) 3 .7 the E$imate initial concentrationof cach organisrn(A.15 The following datarepresents bacterialgrorvthin the a liquid cultureover of numberof days: Doy Amounl x to l{.Test thc approachby using it to fil the data fron. [S] : tne u0 concentration(M). 14... (b) a usc leastsquares to (c) power equation.)75 0 6529 lsl.\:lazx2+e. a2 04 06 oe r3 r5 t7 IB 14. nonlinear to Use data. 1 . 1 4 . i s l c o I('C)in the ftrllowinglranner: latecl telnperaturc to u.r predictp at T :7 . Determinethc bestequationto predict the amouut. 1 : solar (lrE radiation nr 2s l).)a5 2430xlC' 2431 x la' 2 .Plot the dataalongwith all the curves. and ft.For (b) and (c).tll .u.r basedon the possibilitiespolynomial.Is the arly one of fhe curves superior?If so. IS]3 ''K+[.4 9 Ll . a saturationgrowthrate equirtion. Display the data and the rnodelfit on a grapn. bacteof riu atier 30 days. T substrate Ihefollowingdata can be tit with this modcl: lL 0 5 i.5 "C.*t I . the bestpossiblcvirial constants and Ar) fbr the following equation state. on r 5a\ y 5 l7 l0 24 15 20 31 33 25 37 30 37 35 4A 45 40 40 42 50 4l P99 80 r30 200 254 350 454 550 7AA )77 2A2 248 229 219 173 142 72 legression {lt (a) a straightlinc. .and K are paramcters. order lo make the si..ii 6 1. 14. P. 1 3 1. and exponential. t O 5 cc r 005 0l 0\ rc 50 r00 . Usc nonlinear rcgression evaluate and to P. 7 5) 2 5 I 4 5 1 2 5 0 8 5 0 5 5 0 3 5 0 2 8 0 1 8 11. I.rme 14.logarithmic.. employ transfbnnations to Iincarizc data.PROBLEMS rii ...1.t 4 2 5 1.. : the P rate photosynthcsis maximum ratc (mg mid l... That is.595x la r 6C)63 l0 3 5ZBBxlOr' 1 . following Developa plot ol your fit along with thc da1a.). to (c) Usepolynornialregression lit a parabola thc data in to to prediction.16 Derive the lcastsquares ol the fbllowing model: flt 1 4 . 1 7 D y n a m i cv i s c o s i t y f w a t e rp ( 1 0 3 N ..r a 4 B t2 t6 20 67 38 7467 8274 9l69 rot 6C l]2 58 Find a bestflt equation to the data trend. I3.here : the initial rate of the reaction (M/s). \ A (a) Use a transfbrnration linearizethe nrodeland evaluate to thc paramcters...
2l The pH in a reactor varies over the courseof a day. a Speed. Use the resulting equationto predict the radiatron rn midAugust.m B r o k i n g .1. eachof which is a function of its speed. I d ll.0 45 8. amplitude. . fime.The fbllowing cxperimentaldata was Assumingeach month is 30 days long.' 1.5 l2 3 60 75 90 ltt 145 )67 2 l ..22The solar radiation fbr Tucson. fit a sinusoidto this data. h 0 2 4 5 7 I l2 t5 2A 2224 pH 76 72 7 6. and d are where I.l o 9 1 6K . Scientists have sr.m 30 56 5.5 72 8. 1 6 4x 2950x 6846x 1467x 2 . c. Use leastsquares regression fit a sinusoidto the following to data. Use r 5 0 0 0 these l. Developbestfitequaequationsto estimatethe total stoppingdistance a for car travclingat ll0 km/h. Arizona..l9 Environmentalscientistsand engineersdealing with thc impacts of acid rain must detcrmine the value of the ion productof water K. mo F OND I Rodiotion.5 7.l. Employ the fbllowing data and rcgression esto timate the parameters: r (K) 0 t0 2A 30 4A K.20 The distancerequircd to stop an automobileconsists of both thinking and braking components. I cT. and time of maximumpH. w/m2 1 4 4 t B B ) / \ 3 r r 3 5 r 3 5 93 0 82 8 72 6 4 2 r l r 5 9 r 3 r 11. I .o 7. as a lunction of temperature. 9 4 76 t2A 224 847 * D log. and a. : absolutetemperature parameters.334 GENERAL LINEAR LEASTSAUARES NONLINEAR AND REGRESSION collectedto quantify this relationship..204 I 363 rB000 163t tions fbr both the thinking and braking components. o 3 2 . has been tabulated as Time.km/h Thinking.rggested following equation to model this the relationshio: . Use your fit to determinethe mean. P (orm) V (mt) O 9B5 2saoo I t0B 22. b.99t BS 79 7 (K). 9 2 9x t0 : l0 r5 l0r5 t0' t0.
Air density and viscosity are commonly presented tabular form as a function of in temperature.7.you would have to interpolate. Knowing how to solve an inverseinterpolationproblem by recastingit as a roots problem.In such not a case. Specific objectivesand topics coveredare ' ' ' ' ' ' ' polynonrial Recognizing that evaluating with simultaneous coefficients equations is an illconditionedproblem. Knowing how to pertbrm an interpolationwith a Lagrangepolynomial. 1999). example.. polynomial coetficientsand interpolate Knowing how to evaluate with M A T L A B ' s p o l y f i t a n dp o 1 7 v a 1 f u n c t i o n s . Recognizingthat higherorder polynomialscan manifest largeoscillations. That is. As I I was previouslymentionedin Section2.Table 15.1is reprintedfrom a popularfluid mechanics For textbook (Whire. we might f we want to improve the velocitypredictionfor the freefallingbungee I expandour model to accountfor other fhctorsbeyondmassand the drag coefTicient. Suppose that you desiredthe densityat a temperature includedin the table.the drag coefficientcan itself be formulatedas a function of other factors such as the areaof the jumper and characteristics such as the air's density and viscosity. Appreciatingthe dangersof extrapolation.'.: r"it'liii Polynomiol Interpolotion CHAPTER OBJECTIVES The primary objectiveof this chapteris to introduceyou to polynomial interpolation. YOU'VE GOT A PROBTEM jumper. you would have to estimatethe value at the . Knowing how to pelfbrm an interpolationwith Newton's polynomial.
In this chapter. in error can be introduced the data exhibits significantcurvature.336 POLYNOMIAL INTERPOLATION (p). 5 Ox l 0 l /9 x 105 230xl0j 2 85 x l05 3 .. .t' 1 1 t . The most common method usedfor this purposeis polynonial interpolation. The simplestapproach t0 deis termine the equationfor the straightlirre connectingthe two adjacentvaluesandusethls Althoughsuch equation to estimatethe density at the desiredintennediatetemperature.l)thorder polynomialcan be written as . 1 7x 2.l) that passes through all the points. Polltnomialinterpolationconsists determiningthe unique@ . 5 7x 2 75 X / YJ lO 5 lO 5 l0 5 lO r lO5 l0 5 lOr IU' x l0 r X ]U U' 355 X basedon the densities desiredtemperature that bracketit.l)thof order polynomial that fits n data points. s/m2 v .99x l0 r 1 33 x l0r j l .there is only one straightline (i. The generalformula for an (a ..t T.e. .only one parabola connects setofthree a points (Fig. Similarly.1 t I p . 15.946 0 835 0 746 0 675 06r6 0 525 a.j 4 75 x lA': 6 20 x lOj 777 xla5 TABTEl5. for approaches obtainingadequate EXA I5. To be consistent . when linear interpolationis perfectlyadequate many cases. computeintermediate polynomialcoefficients Before proceeding.1) For n datapoints.we will explore a numberof different for estimates suchsituations.457 5t x 7l x B Ox 95 x 2 . (p).r. l5. it uses decreasingpowers as in I '+'. should note that MATLAB reprcsents we (15.kg/m3 1t.1a).'+erx"t (15. This polynomial then provides a formula to values. For example. f (x): plx" I p:.thereis one andonly onepolynomialof order (n .f(x):ar*azxla3x2 +. a firstorderpolynomial) that connects two points(Fig.4 with MATLAB. (15.. 4 5x l 0 .I INTRODUCTION INTERPOTATION TO You will frequently have occasionto estimateintermediatevalues betweenprecise data points. we will adontthis schemein the followins section. m2ls 0.Ratherthan usingincreasing in a different mannerthan Eq.'C /A 0 2A 50 100 150 200 254 300 400 500 52 29 2A 09 4.lr). Density dynomic viscosily ond kinemotic (u) viscosity os o functiono[ (f) temperoiure ot I otm os reported White (1999). by p.1).38 x 2 .j 4 O Bx l O . N . powersof .
Suppose that we want to determine coefficients the parabola. 4 5 ' 7 pairscanbe substituted Eq. l E D e t e r m i n i n g o l y n o m i oC o e f f i c i e n t w i t h S i m u l t o n e o uE q u o t i o n s P l s s Problem Stotemeni.2)to yield a system three Eachof these into of equations. (15.000 J 10.l Defermining Polynomiol Coefficients A straightforward way for computingthe coefficientsof Eq.A simple MATLAB sessioncan be used to obtain the . E X A M P L1 5 . 0. s z s ] 00 I p.this As allows us to generate linear algebraicequations n that we can solve simultaneously the for coefficients. s00 lJ I 1250. :' g { b ) s e c o r d .451: pr(500)2 p2600) pt or in matrixform: 3oo I oo. in the following example.525: p1@00)z p2(400) p3 + + l t 0.6t6 xz:400 f(x):0. 04 o ot l l n z  : l o . I INTRODUCTION INTERPOLATION TO 337 FIGUREI5. .ooo rl I p.o r d e ' { o u o d ' o l o6o o ' o b o l i c )c o n n e c r i n t L r e ep o i n t s o n d { c )r h i r d . the problem reducesto solving three simultaneous linear algebraicequationsfor the three unknown coefficients.l.+ p l . r : : 5 0 0 . [ o. l5.616: pt(30q2 + p2Q00) p3 + 0.525 . f( x ) : 0 . (15.oro I 1 6 0 . t 2 1 p 2 .4s7  Thus. : . t h a t p a s s e s t h r o u g h t h e l a s t t h r e e d e n s i t y v a l u e s f r o m T la b l e rr : 300 "f(rr) : 0. f ( x ) : p t .I (o) Exomples nierpoloting of (lineor) polynomiols: firslorder connecfing two poinis.o r d ec u b ' c l {r conneciinoiour ooints. the of \ l5.2)is basedon the fact that n datapoints are requiredto determinethe n coefficients.15.
we will first briefly review how the coefficients of the interpolating polynomial can be estimated directlv with MAILAB's builrin functions. In this chapter. which is quite large for a 3 x 3 matrix. >> A I. 5 6 1 6 2 5 Although the approachin Example 15. The illconditioning becomes even worse asthe number of simultaneous equations becomes larger. we will also describe two altematives tlrat are wellsuited for computer implementation: the Newton and the Lagrange polynomials. however. there are altemative approaches that do not nranifbst rhis shortcoming.6r6 4. 0 0 1 7 1 5 . 0 0 1 7 t 5 ( 3 5 0 ) 1 .00171s00000000 r.That is.8912e+a)0t.'.For example.00000115000000 0. 0 2 70 . 0 2 1 This polynomial then providesa meansto determineintermediate points.451 )'.r I J l"i rr I I lr.190000 3tl0 1. This condition number. notice that the coefficient matrix in Examplel5.160000 404 b = [0.r.42100ct00000000 Thus. As a consequence. 0 0 0 0 0 1 1 5 .338 INTERPOLATION POLYNOMIAL solution: >> >> format long . it has a seriousdeficiency. the parabolathat passes exactly throughthe threepoints is + f ( . Before doing this.525 0.tr) ll i\l:{ } ilii Coefficient rnatricesof this tbrm arc refeflrd to as Vctndennonde nntrices. 0 0 0 0 0 1 1 5 ( 3 5 0 ) 20 .To understand this flaw. . r 1 . This can be seenclearlyby expressing ingenit eral terms: t'.250000 500 1l.l has a decided structure. Such matrices are very illconditioned. to This can be illustratedby using MATLAB to computethe conditionnumberfor thecoefficient matrixfrom Examole15. r 2 0 .l Lxi . implies that about six digits of the solution would be questionable. >>P=A\b 0. x ): 0 .1 provides an easy way to performinterpolation.1as >> cond (A) 5. the value of densityat a temperature 350 oC can be calculated of as : + / ( 3 5 0 ) : 0 . their solutionsarevery sensitive roundoffenon.
Using similar triansles.OOOOOll5OOOOI]O O. 350) 0.l with simultaneous equations.1: >> format longt >> r: [300 400 500]. it returnsthe coefficientsof the polynomial that pass directly through the datapoints. 15.f (rt) ^ 4l f(x)f(xt) x2xl ( 15.02'IOOOOOT]OOOOO We can then use the polyval >> d = polyval d.2 NEWTONINTERPOTATING POTYNOMIAT forms for expressing interpolatingpolynomial beyond There are a variety of alternative an (15. Before presentingthe generalequation.we will introducethe versionsbecause their simple visual interpretation.2.2 ) _ I O.525 0. For example.it can be usedto determinethe coefficients of the parabolathat passes throughthe last threedensityvaluesfrom Table 15.1) which can be rearranged yield to fr?):f(x)+ f(xr) f(.2).451).2 NEWTONINTERPOLATING POLYNOMIAL 15. >> p = polyfit (T. The notation /r (r) designatesthat this is a firstorder interpolatingpolynomial.2.but ratherfollows the generaltrend of the data.is depictedgraphicallyin Fig. popular and useful forms.2.and secondorder of 15. fi(r) . ly f it performs interpolation.56762500000000 These results agree with those obtainedpreviously in Example l5.rr) x2*xl (1 s . such applications.4. 15.That is. function to perform an interpolation as in (p. Newton's interpolatingpolynomial is among the most the familiar format of Eq. This technique. density. passthrough any of the points. >> density = i0. s ) which is the Newton linearinterpolation formula.I 5. number of data points is greaterthan the number of In the Consequently. po For the casewherethe numberof datapointsequalsthe numberof coefficients.OO1715OOOOOOOO .1. Notice that besidesrepresenting slope of the the rr) is a finitedifference line the the term .. leastsquares line doesnot necessarily the fit coefficientsbeing estimated.2 MATTAB Functions: polyf it dlrd potyval 339 Recall from Section 13.that the polyf it function can be usedto perform polynomial regression.616 0.called linear interpolation. first.1 lineor Interpolotion The simplestform of interpolationis to connecttwo datapoints with a straightline.
2 used triongles the indiccrte slmilor oreos The shoded inierpolofion.791759. We useEq. E.li(r) _:.6931412./ r "f(. use a smallerinterval liom ln I to ln 4 ( I . Then.0 1 (2. U s i n g t h e s m a l l e r i n t e r v a l f r o m x l I t o ..stimate naturallogarithmof 2 using linear intelpolation. (a.r86294 0 lre\ :0+ t) :0./ I rii t . general. 7 97 5 e.' / .1): 0' 358351e : w h i c h r e p r e s e n t s a n e r r o re .5)from :r1: I to rz : 6 to give . Solution.fi (2:o + ff ) 1 .20)1.4 / . 3 V c . a interval decreases.phicol o i i o d e r i v ei h e N e w t o n l l n e o r n l e r p o l o l i ofn r m u l o F q { 1 5 . true valueof ln 2 is 0. the better the approximation.2 L i n e o rI n t e r p o l o t i o n EXAMPLE First.asthe betweenthe data points. ( 15.x) T FIGURE5. between by perform the computation interpolating the Notethat but repeatthe procedure. of depiction ineor C. ln I :0 and ln 6: 1.f: 4 S . 5 ) ] l In approximationof the first derivative{recallEq. the Problem Stotement. line. r : = 4 o yields t.the smallertheinterval This is due to the fact that.4620e81 . continuousfunction will be better approximatedby a straight in is This characteristic demonstrated the following example' 'I5._*\\ f(xz) d .340 INTERPOLATION POLYNOMIAL .386294).
6). 15.s) for t : r.1 .l'(.If threedatapointsare available.: r r ) .2resultedfrom approximatinga curve with a straightline.3.6) For A simple procedurecan be used to determinethe values of the coefTicients.7)can be substituted into Eq.7) at Equation(15. (15.6).Eqs. (15. (15..2 NEWTONINTERPOLATING 341 J(. t.\'l into at Finally. Thus..POLYNOMIAL I 5..f(t:). (15.l(. r r ) ( x . polynomial (alsocalled a quadraticpolynomial or a parabola). particularly A secondorder i c o n v c n i e nltb r r nl b r t l r i sp u l p o s es l : ( x ) : b t * b z \ ..which can be evaluated r : r2 for (x) . are  5. usirrg the shorter interval reducesthe percentrelative error to €t :33. b1. B<tth interpolations shownin Fig.: .2 GluqdroticInterpolotion The error in Exaniple 15.rt .which can be evaluated rrranipulation.6.alongwith the true function.r) 2 05r FIGURE I5.3Vo.f .{2 f(.tr) .9) {.f (. z ) (15. Eq.t with r :.7)and (15. D : ( . Conis sequently.2.r solved(after some algebraic and .(15.xy) (15.rr) (I 5. strategyfor improving the estimate to introducesomecurvatureinto the line a this can be accomplished with a the connecting points.3 Two lrneorinterpololions esiimoleln 2 Note how the smollerinlervolprovideso better lo esiimote.8) br:"{2 ./(r:) h1l .rl can be usedto conrpLlte b1: .8)can be substituted Eq.rz).
 FIGURE5.\z:4 rr : 6 "/(xt):0 J$):1.r) 2 fl.4 I T h eu s eo f q u o d r o t irc t e r p o o t i o ne s tm o l eI n 2 . . Thus. (15.. we should examinethe form of the coefficient &.1 f(.l5.5).27'). o  \.The lastterm. introduces secondordercurvature the formula.8)gives b 1: .6) is beginningto manifesta structure that is very similar to the Taylor seriesexpansion. terms are addedsequentially t0 captureincreasinglyhigherorder curvature.342 POLYNOMIAL INTERPOLATION Notice that.Thus.r) : In. (4. Employ a secondorcler Newton polynomial to estimateln 2 with the samethreeooints usedin Example 15.386294. ( 1 5 .r:2). (15. (15.xr)(. b:(r .0 _:0.. Eq. still represents slope the b2 the of line connectingpoints xy and "r2. and rr. That is. the into Before illustrating how to use Eq. as was the casewith linear interpolation.'791159 y S o l u t i o n .It is very similar to the finitedifferenceapproximationof the secondderivative introducedpreviously inEq.3 EXAMPLE Q u o d r o t i cI n i e r p o l o t i o n Problem Stotement. (15. T h el i n e o irn l e r p o l o i ifo n m . A p p l y i n gE q .4610981 +.t Ouadraticestimate Linearestimate . 7 ) i e l d s bt:o Equation( 15.2: rl:1 .6).6) are equivalent to linear interpolationbetweenx.r:) : l.r . r : I t o4 i s n lo ro o l s oi n c l u d efd rc o m p o r i s o n . as specifiedpreviouslyin Eq. the first two terns of Eq.38629.
. .1 .r l) (1s. . .x r ) * . * b u \ . l x z .I .386294 343 64 6. Thus.4 ) which can be evaluated At .14) where the bracketedtunction evaluations finite divided differences.f . .. (15. I . . . 4 6 2 0 9 8 1 ( . x1. ( x * . .462098r : 0.I 5. r r ] . .4) improves the interpolation cornparedwith the result obtained using straight lines in Example15.0.tlre are generallyas first finite divided differenceis represented J l ^ i . the curvatureintroducedby the quadraticformuia (Fig. . (r5.\n  ( rs . r"i l : r.3. The secondfinite divided difference. J Q ) 1 . " r z .r r ) 1 . .q. . which representsa relative error of e. ..o r d ep o l y n o m i a ln d a t ap o i n t s a r e r e q u i r e dr:r . 9 )y i e l d s 1. 1 1 .6)yieldsthe quadratic formula Substituting .5658444.1 ) t h .47o.W e u s e t h e s e d a t a p o i n t s a n d t h e l following eqnations evaluate coefficients: to the b1:fQ1) 6' : . 1 7 ) "Yl .r] (15..: f l x .r3) x fu : f fv.l ) . t s r e v a l u a t eh e c o e f f i c i e n tb 1 . .3 Generol Form of Newfon's Interpolofing Pofynomiqfs The precedinganalysiscan be generalized fit an (zz. .l f [ x . l ' ( . ( 1 5 .datapointscan be usedto .x ) . f z @ ): 0 r ( + 0 .0518731 these valuesinto Eq.I )thorder polynomialis . b 2 . the nth finite divided differenceis . r r ) ( x . 15. 0 .. f " _ t f ) : b r 1 b z j .t .f'[r2.x n . b .10) As was done previously with linear and quadraticinterpoliition. [ .2and Fig.( x .rt (15.The (n .whichrepresents difference two first divided the of generallyas is differences. r . _ r ) (15. : 18.r . tl : b . . . x r l ri . .16) Similarlv. xz. _ _yl ( 1 s1 5 ) .2 NEWTONINTERPOLATING POLYNOMIAL a n d E q . F o r a n ( n .x l ) ( : r .12) (r5.0 .kitf(rl) t. q J.791759 1. .2.5 1 8 7 3 1.r : 2 for fz(2) : 0. 15.l)thorder polynornialto n data to points. expressed : f lx. For exanrole.. .f [ r i . .  5.
xt. x 2 .x r ) * b z ? . r . In Example 15.t .rG):. is Solution. .. (15. representotion .344 POLYNOMIAL INTERPOLATION xi xl '. (15. .386294 .x ) f l . The thirdorder f z @ ) : b r * b z @. noticehow Eqs. (15. 15./ L^41 ^{l _ l  :0. x 1 l + .10)with n : 4..':^:).3.:0.j. 1 8 ) We should note that it is not necessary that the data points used in Eq.eferred oso divided to difference toble.(15.f ( x t ) + ( r .:rl it::ti=.x r ) ( . x r l( 1 5 .t ) .'^' f(x) First Second Third xl r flxo.2.data pointsat xt : l. x2]: fr. Eq.This property will be exploitedwhen we developan efficientMfile in Section15. . r r l * ( " r. (15. xz:4. Now.r3= ( 1ry91s = used to estimate ln 2 with a parabola. f l x n .x ) * b q ( x.4 Newton Interpoloting olynomiol P Problem Siotement.11) can the in through which can then be substituted interinto Eq. t r n .asillustrated in the following exarnple. FIGURE . to the 1 EXAMPLE 5.{) L6094381. Also.609438 1.791159 r.estimateln 2 with a thirdorderNewton's interpolatingpolynomial.x2. r r ) ( x .18) be valuesnecessarily in ascending equally spacedor that the abscissa be order. higherorder differences are computed by taking differences of lowerorder differences(Fig.15)through(15.x t ) f l x z . (15.r. ( r . adding a fourth point [xa : 5.rr] 4 f [4..f (. .19r'759 :0.17)arerecursivethat is.1823216 56 r.386294 O . .x r ) ( x .x n . .15)] for 1. x z .l ii. " + ( x . polynomial.. Thesedifferences be usedto evaluate coefficients Eqs. . .:.a4t.4 implement method.[] .x 2 ) ( x . and..5).2027326 64 1. 4 . r z ) . .5 I5 Grophicol depiction iherecursive of finite is of noture divided This differences.4620981 : _.10)to yield the generalform of Newton's polating polynomial: f..x r ) ( x . "f["r:. (15.14).x 3 ) The first divided differences the problem ale [Eq.
1823216 0. 0 5 1 8 7 3 1 l (" t l ) ( x . r1] : 345 o.t3.x 2 ) : 54 : 0. x z .16)] are /[. (l5.0 .fzQ) :0.the interpolating cubicis f:(r) :0 + 0 .2027326 . 0 5 1 8 7 3 1 : I 6l 0. FIGURE .4 ) + 0. x t ] . the divided differencetable is f (xi) j 4 6 5 0 386294 791759 609438 a 462A9Bt 0.t2.. . r t . andb'. (15. x 2 .b3.6287686.6 I5 The use of cubic interoolofion estimoten 2 lo f(x) z . (15.4620981 .6. 4 6 2 0 9 8 1 ( x l ) . t l l r e p r e s e ntth e b1. x 3 .x 1 ] : 0.10).2021326 0. f f x a . .02041100 The third divideddiff'erence [Eq. 15.(0. f [ x z . x z .007865529 Thus. f [ x t .4)(r . . of Thus.t 2 .17)with n :4] is J ' [ x + .0 0 2 0 4 10 0 r 00a786552a t) e d T h e r e s u l t sf o r / ( x r ) . coefficients b2.02041 .2427326 o l823216 cc5lBZ3il .007865529(x l)(x . respectively.2 NEWTONINTERPOLATING POLYNOMIAL The second divideddifferences [Eq. The complete cubicpolynomialis shownin Fig.0 . Eq.3Vo. which represents relative error of a €t :9.6) n which can be used to evaluate.051873l) r00 1 J  : 0.I 5. x 1 ] . a n d f l x q . .
= y(:). yl polynrmial inr erpofdtrng "o bosed or n dala poinrs lyrnt) % to determine a vafue of the dependent variable at a given value of the independenc variable.:Fr^1 divided differences in the form of a !  s rli z mr c ho camo etnt hri: 6ncl b = zeros (n.y.4 MATTAB Mfile: Newtinr It is straightforward developan Mfile to implementNewtoninterpolation. xx. 1  divided differences to inLerpolate yint = b(1.j1)b(i. difThe ferences then usedin conjunctionwith Eq.n1 xr xt"{xx x(jr). = Newtint(x. b(:. yint end .x.y. : r 1Fr ifnlv.n for i = 1:n j+1.j) .(b(i+1.i).lanonrlanf rr:ri :hl o value Intrlyn^ i of independent a'o. to As the first step is to compute the finite divided differencesand storethem in an array.fdf variable CIaLeO at which z ? output yint % = inierpolated value of dependent variable !} . % % input: :. end end ? use Lhe finite 1.2. in Fig.7 FIGURE An M fl e lo imp ement Newton inferpolotion.omnrl e rlre finire % difference table n = length(x).15.1) % the (:) ensur:es that y is a column vector for j .2.n) .346 INTERPOLATION POLYNOMIAL 15.7.lanor F va ' ' rh'c 1Fr x% z % vxx= r.3: in >> form.18)to perform interpolation. are the An exampleof a session using the function would be to duplicatethe calculation we just performed Exarnple15. j+1) *xt. for j = 1.1)order Newton ? yint . (15.j1))/(x(i+jr)x(i)) b(i. ? assign dependenL variables to the first column of b.ts . = yrnt+b(1.xx): Uses an (n .et = tl Longt 4 6 5l >> x I5.xx) function yint polynomial I'Jewton interpolaling % Newtint: = llewtrnt(x.
\'.r ( .l .x r ) ( .x : ).y.r1: r trtrI 12 . > > y = t o S f( x ) ..and secondorder versionsas well as higherorder Lagrangepolynomials can be represented conciselyas (1s.22) . J r i l r .r l. r ./ (r3x1)(xjr2) /. ( .. >> Newtint(x.Xr X2Xt J'.3 TAGRANGE INTERPOTATING POTYNOMIAT Suppose formulatea linear interpolatingpolynomialas the weightedaverage the two we of valuesthat we are connectingby a straightline: . : "/r(x) . \ ' r ) ( r . 15.r): Lt J @r)t Lzf Qz) (rs. The samestrategycan be employedto fit a parabola throughthreepoints.=1. Both the first.For this case threeparabolas would be usedwith eachone passingthroughone of the points and equaling zero at the othertwo./(.r9) where the Z's are the weightingcoefficients. Such a secondorder Lagrangeinterpolatingpolynomial can be written as At.the second coefficient the straight is line that is equalto I at "r2and0 at. Their sum would thenrepresent uniqueparabola the that connects the threepoints.2) 347 0. T h e o t h e r t11 terms work in a similar fashion. { : .6) (1s.. 1 r ) .). 8 ) : .t  (.t .I 5. 1 5 .3 LAGRANGE INTERPOLATING POLYNOMIA. r 1 ) aa n d i s e q u a l t o z e r o a t . r :. is logical that the first weightingcoefficient It is the straightline that is equal to I at x1 and 0 at x2: xtx2 Similarly. \ ..rl Substituting thesecoefticients into Eq.rX2' xlX2 ^ J 6) l .rrr)(. r 2 a n d .20) where the nomenclatnrefi(l) designatesthat this is a iirstorder polynomial.6287685"t890841 I5.20) is refered to as the LinearLagrangeinterpolatingpolynomial.rxr) ( x r x z. ( r5 . 2 1 ) N o t i c e l r o w t h e f l r s t t e r n r i s e q u a l t o / ( .r r l ..19yields the straightline that connectsthe p o i n t s( F i g .r t )  (xx1)(xx3) j tt2t + (. Equation (15.
.gtlO f(rt):0.r:):0.8 i g T s A v i s u o l e p i c t i o n f t h e r o f i o n o b e h l n dL o g r o n g en t e r p o l o i i np o l y n o m i o l s .5 Interpoloting Polynomiol EXAMPLE Logronge polynomial thefirst and of second ProblemStotement.o" 0 .r : the numberof datapoints and fl designates "product of.20)l be used obtain estimateal to the Solution.r: I5: t 5 .IFI tl _ fl y.20 t 5 .212 polynomial (15.348 INTERPOLATION POLYNOMIAL LtfU) FIGURE I5. ^1 (15 23) _  .85 l(. the uniquestroiqhl of b"e ihe thot connects h^/opornts.r):   ' ! I j=l . Use a Lagrange interpolating data: orderto evaluate density unused the of motoroil at T : 15'C based thefollowing on rr :0 r::20 . 8 0 0 : 1 ." the .(.The summoiion ihe two fermsmusi.{::40 f(xr):3. r/ where .h ef i g u r e h o w s d o e t h e f i r so r d e r c o s e E o c h o ft h e t r " o l e r m s oE q ( 1 5 2 0 ) p o s s e s t h r o u o h o n elo fe p o i n t s o n d i [ h line is zero ot the other. 5 6 2 5 1.85+'" 'f r( . where r L. Theflrstorder can tEq.iherefore.5. r ) : ' " 020 t00 .
21)] fashion.xx) : Uses an (n . end for: i = 1:n . is alsopassed valueof theindependent (xx). U (20. _ n.duct.3.9.212:1. Produce .40) (0 .23).0)(20._.\. secondorder is as In a similar the /.{r):  (1520)tl540.i . Theorderof thepolynomial based thelength thex vector is on of wantto interpolate is If are an thatis passed.(15. n values passed. in It is straightforward develop Mfile based Eqs.( 15. end end s = s+prc.I'i for j = 1. Z a given value of the independent variable.20)(0.INTERPOLATING POLYNOMIA.y. .n if i = i product = product* (xxx(:) ) / (x(i) x(j) ) .3 LAGRANGE 349 polynomial developed [Eq. 6 t .independent variable Z y= % dePendentvariable xx = value of independent variable at whlch the % is calculated % inter:polation ? output: = interpolated yint value of dependenc variable ? n = lenqth(x).1)thorderpolynomial fit.J316875 (400)(4020)  5. if lenqth(y)=n. end = s. FIGURE I5.the functionis passed (y) you variable where It the dependent variables. 6 1+ .xxl trrccion polynomial Z Lagrange: Lagrange interpolatingr = Lagr:ange(x.22) to (x) two vectorscontaining independent and the the Fig.9 inlerpolction AnMJile implement lo Logronge y nL .1)order yint 2 polynomial based on n data points ? Lagrange inLerpolating (yint) at to determine a value of the dependent variable % xx.t ] . (n . yint . error('x and y must be same length'). 15.r^^_ (150)(1540)^^^^ . Z input: x .1 MATTAB Mfile: Lasranse As an on and (15.. I 5.Lag"6ngerx.40) + (l5O)fl520)._ U.
valuesof the As the r's are typically unilbrmly spaced./. be illconditioned.r). Thus. (r). the interpolationproblemreduces to . the dataabove.rl.Unfortunately. the .22II2B I5.2 >> densitY i I \ 41 22222 2 LaQfange r ons t.for /(r) : l lx theresult is f (x) x o t429 7 o 1667 6 02 54 425 03333 32 05 Suchnonunitbrm spacingon the abscissa often leadsto oscillationsin the resulting interpolatingpolynomial.091. you might be tempted switchthe /(. >> d = 1r. For example.An alternative strategyis to fit an nthorderinterpolatingpolynomial.29 i. (T. thereis no guarantee the valuesalong the new abscissa that In the [the /(. to Becausefour valuesare passed the function. when you reversethe variables. In most cases. 15 ) I . For instance.. in many cases.f ('. this polynomialwill not f (r) versus. For thiscase. This can occur even for lowerorderpolynomials.but you are given a value for .1. 3 3 . fact.rthat makesthis polynomial equalto the given /(. valueswill be "telescoped.52 1. with because the. t4 2 9 Now suppose that you must use the samedata.e.3so POLYNOMIAINTERPOLATION I An exampleof a sessionusing the function would be to predict the densityof airat of I atm pressureat a temperature 15 "C basedon the first lour values from Table15.r values[i.r)'s] will be evenly spaced.t valuesin most interpolationcontextsarethe dependentand independent variables.1667 7 0 .suppose for that you were askedto determinethe value of I that corresponded l (r) : 0. a consequence.The answerto your problem then amountsto finding the valueof .r) and.r's are evenly spaced.r)and./(r) and must determinethe corresponding value of x. Such a problem is called inverse interpolatiort For a rnore conrplicatedcase.4 INVERSE INTERPOLATION As the nomenclature implies.3.e. correctanswercan be deterthe the : m i n e dd i r e c t l )& S { : 1 1 0 . simple exampleis a table of valuesderivedfor the A tunction(r): l/x: f x f@) 2 05 3 0 3333 4 025 5 o2 6 0.. to because lunction is availableand easyto manipulate." That is. 3 3 3 3 . they will have the appearance a logarithmic scalewith some adjacent of points bunchedtogetherand othersspreadout widely.merelyplot x versus to use /(x)] and an approachlike Newton or Lagrangeinterpolationto determinethe result.respectively. d. a thirdorderpolynomial would be implementedby the raqrange functionto gile: >> format longr >>T=t40024541. to the original data[i.
0 8 3 3 : l The answer to the inverse interpolation problem of finding the .3333).a simple approachwould be to fit a quadraticpolynomial thethreepoints:(2. Extrapolation I 01 tnteroolatt r.25).r 2 .704158 3.296. 0 4 1 6 ' 7 2 .f (x) . 3 1 5 x+ 1. . 3.(3.r) that lies outsidethe rangeof the of k n o w n b a s ep o i n t s . .a third. I n t e r p o l a t i o n. quadratic the fbrmulacan be usedto calculate 0. 0.r correspondingto determiningthe root of l (. .0 . 3 : 0 . t . 0. to and(4.there are twcl issuesrelatedto polynornialinterpolationthat must be addressed.for the problernjust outlined.5 EXTRAPOTATION OSCILLATIONS AND 351 For example. I5. 1 5 . . The resr.78333 : 5. 15. . polynomial I I I I I I t> l"r . ' E x t r a p o l a t i o n  r.1 Extropolofion Ertapolation is the process estimatinga value of ..of interpolatin o Il .r r . 0.e 0 .3 would therelbreinvol'r.5).I 5. .375 + (0.37s)2 4Q.333. 3 7 5 r+ 1 .rlt wouldbe / : ( r ) : 0 . Theseare extrapolation and oscillations.5. the secondroot. extropolofion The isbosed through first poinfs on fittinq porobolo o thL three known . I O E lllustrolion possible of the divergenceon extropoloted of prediction.is a good approximationof the true value of 3.5 EXTRAPOTATION OSCITLATIONS AND Before leaving this chapter.If additional accuracywere desired.r) : 0.5 or 6 couid be employed. 0 8 3 3 3 1 For this simplecase.295842 2(0. t h e o p e n . .041667) Thus.e n d en a t u r eo f i d F I G U RI 5 . 0 4 1 6 6 7 r r.0 .041661)0. A s d e p i c t e dn F i g .or tburthorderpolynomial along with one of the rootlocationmethodsfrom Chaps./ (.1 0 . .
. We can then use the polynomial coefficientsalong with the Fo1yr731function to predict in fhe population 2000 as :'> polyval (p.fit works without an errormessage: r . This example patterned is afteroneoriginallydeveloped Forsythe.rThe populationin millions of the United States be tabulated as Dote )S2O I 930 l94O I 950 I 960 l97O I 980 I 990 2000 Populalion 10646 123.when this is implemented.{)oD.. andMoler.05 227. therefore.7 ) However.o. rr2l conter ino Remove repeated drid scdr jng ds desct ibeo data in HELD We can follow MATLAB's suggestion scalingand centeringthe datavaluesas in by .0800 which is much lower that the true value of 281. EXAMPLE 5.61 205.Use it to cornpute Fit a seventhorder the population in 2000 by extrapolationand compareyour prediction with the actualresult.tt.the datacanbe entered as >> t = L\924:10:19901.As such.pp) E} .the true curve could easilydivergeliom the prediction. >> rs = (r Now po11.Insight into the problem can be gained by generating plot of the dataand the polynomial. >> pp = polyval (p. is badLy condilioned. The polyf it function can be usedto computethe coefficients >> p .be exercisedwhenever a case arises where one mu$ extrapolate. (2000f955)i 35) 175. p o p .08 132. >> pop = 1105.42. >. following message displayed: the is VJarnlncJ: Polynomial po'nt s or POLYFIT. a > > t t = l i n s p a c e ( I 9 2 A .27 180. ' >p = p o l y f i t ( t s . Extreme care should.45 I23.polyfrt (t. by from 1920to 2000 can Malcolm. 2 0 0 A ).1955)/35. 1 ) . First.I2 L52. Solution.08 13212 15227 tBA67 2A505 22723 24946 28142 polynomial to the first 8 points (1920 to 1990). (tt1955) /35). nloi (r.6 1 Dongers f Extropololion o Problem Stqtement. r 6 l .23 2 4 a.3s2 POLYNOMIAL INTERPOLATION a the processextendsthe curve extrapolationrepresents step into the unknown because beyondthe known region. pop.
Create .. they tend to be highly sensitiveto roundoff eror.24) which is now called Runge's function. He took equidistantlyspaceddata points from this function over the interval tl.Carl Rungepublished studyon the dangers highera of order polynomial interpolation. E X A M P L lE .I I I ro U s eo t o s e v e ^ t l ' o ' d e ' p o l y ^ o n io rtr o i .it is absolutelynot true for polynomial interpolation.O r d e r o l y n o m i o I n t e r p o l o t i o n P l Problem Stotement.. seventhorder predictionin 2000. The following exampleillustratesthis point nicely.2$.: polynomialplungesto the erioneous extrapolation. He then used interpolatingpolynomials of increasing order and found that ashe took more points.5 EXTRAPOLATION OSCILLATIONS AND 353 250 r\ 200 150 1920 1930 1940 1950 1960 1970 1980 1990 2000 FIGURE5.[i. Further.the situationdeteriorated Duplicate Runge'sresult by using the polyf it and polyval functionsto fit fourth. 1 .'n::ilT::..the polynomialsandthe original curve differed greatly as the order was increased.1]ii*:. Z 5 D o n g e r so f H i g h e r .2 Oscillotions Although "more is better" in many contexts. considerably.I 5.and tenthorder polynomials to 5 and 1l equally spacedpoints generated with Eq.?.He looked at the following simplelookingfunction: I : rt l l\t . ll.*::TJ:il:1Jl:J. " 2 O O Oo s e o n o o[ o b o d o t oi r o ml 9 2 Of h r o u q h 9 9 0 . the  5. S . In 1901.' l *25x2 (t5.*1.:.. e p r e d i c t i o n U .". (15.5.p o o u l o n .Higherorderpolynomialstend to be very illconditionedthat is.
' o'. polynomialdoesa poorjob of followingRunge'stunction.12. the Continuingwith the analysis. I . xx) .I. ^2) . >> plot (x. / (I+25*x.1). we can generate valuesfor Runge'sfunction itself and plot them alongwiththe polynomial fit and the sampleddata: >> yr .4 1 r\ i\ ta\ tta t5.5). I. FIGURE .354 POLYNOMIAL INTERPOLATION Solution.xx.yt:. Next. t t. i 0. The five equally spaced datapointscan be generated in as >> x >> Y = linspace(./(I+25*x.L.0.' ' ) As in Fig. and the polval function can be usedto generate polynomial the interpolation at the finely spaced valuesof xx: = po11'f it >> 1.I2 I5 funct]on Cornporison o[ Runges line) polynomiol 5 poinis fitto {doshed wiiho fourthorder sompleo lrom lne iunclron.i (1+25*xx.xx. tenthorder polynomial can be generated plotted the and with >> >> X y = = linspace( 1.11).y. Recall that l inspace automaticallycreates100 points if the desirednumberof points is not specified.The po ly f r t function can be usedto generate coefficients thefounhof the order polynomial. 15. /t It r\ r\ t\ l! t.1.I.^2).y .y4. Finally."2). 4) .5 .4 = polyval >> p (x.2 0. a more finally spacedvector of rx valuescan be computedso that we cancreate a snooth plot of the results: >> xx = linspace(1.l ( x "f(r) (a) C al fc . (p.
the fit has gotteneven worse. > > y 1 0 = p o l y v a l (p .PROBLEMS 355 a 0.xx.5 1 nF 0.y.lowerorder polynomials of the type describedin this chaptercan be used effectively to capturethe curvins trendsof datawithout sufferins from oscillations. Although theremay be certaincontextswherehigherorder they are usually to be avoided.75 6 36 polynomialsof polynomi.particularlyat the endsof the interval! polynomialsarenecessary.5 7 3 65 5r o "f(r) I 475 3 5. Choose sequence for your estimatesto attain the best possibleaccuracy. I 3 E functlon Comporisonof. PROBTEMS Giventhe data l5. Chooseyour basepoints to attain good the alsoforder I through3.y10.In most engineeringand scientific contexts.25 5 )9.l Giventhe data 2.nnminl ofthe usedto generate data in the table? the .xx.'o'.Runge's fit polynomiol to I I points {doshedline)with o tenihorder somoledfrom lhe lunction >> p = polyfrt (x.y. x x ) . accuracy.' ' ) As in Fig.What do your results indicate regarding the order .. >> plot(x.10) .5 F I G U RI 5 .4) usingNewton'sinterpolating ofthe points order I through 4.13.Calculate/(4) usingNewton's interpolating Calculate /i3.aL^ r ^_^_^ ^l.yr. 15.
4( ship rather than the straightline represented Ohm'slaw: by . ideal resistoris linearly proportionalto the currcnt I flowing (c) Repeat(b) but use quadraticinterpolation.for the quadraticpredictionusethe datafrom I 970.r12 67 TAB T.93 for the following tabulated data: 2 637 t 96 5 05 245 05 205 t 965 the r 637 lO o 15. x IB 20 2.10 Besselfunctionsoften arisein advanced engineering analyses such as the study of electric fields. Usea fifthorder interpolatingpolynornialto fit the dataand compute V fori : 0.s 23 0B 09 45 o 94lt76 0 9 6 t5 3 8 Note that the values in the table were gencratedwith the function f (x) : x2I 0 + x2). 9 6 1 0 8 5 4 0 / 5 9 (a) Use quadraticand cubic interpolationto determinethe kglm3 oxygen concentration I : 12 'C and c:10 glL.2 using the Lagrangepolynomial of best estimate? Employ this best estimateand inverseinten order I through 3.. curve must be fit to thedata.1 TableP14. kJl(kg K) 6 5453 6 7664 populationin 2000basedon the most recentdata.Estimate"/1 I ) using third. I 980. suggests that interpolationmight be appropriate.Thatis.and so on. 15. regressionwould typically be the preferred method of curvc litting fbr analyzing such experimental data. third. : I R. Here aresome selcctcdvalueslbr the zeroorder Besselfunction ofthe first kind SOIV the < the r for l r5. MATLAB's builrin tunctionbessel j. Which approach yields the best result? sus temperature comesfrom a table that was measured with 15.the smoothness the relationof ship. m3/kg o to377 0 il 144 4 1 2 5 4 7 and fourthordcr interpolating polynomials to predictthe 64147 s.l in s pres T. polation to determinethc conespondingtemperature. l5. polation.10.6 Employ inverseinterpolationto determinethe value of r that coresponds to l (t) : 0. for (tl) Use linear interpolationto determinethe oxygen con15. real resistorsmay not always obey Ohm's law. pola r01 J@) 0 0.lt The following data for the densityof nitrogengas ver. Jl Dett r5. 15. which can be determined with entropyof 6.7 Use the portion of the given steam table for super. "1 a.11 RepeatExample 15. (a) Determinethe correctvalue analytically. (b) Use quadraticinterpolationand the quadraticformula to determinethe value numerically. The following resultssuggest curvilinearrelationa 2. v 15.and to casebasedon the true value.2 24 26 15.However.9 Ohm's law statesthat the voltage drop V across an centrationfor I : 12 "C and c : 15 glL. t ' ( r ) 0 . (c) Use cubic interpolation and bisection to determinethc value numerically.for the linear predictionusethe datafrom I 980 and I 990.Determinethe percentrelative enor for each dratic interpolation. I l8 with linear inter(2.. 15.Use firsl throughfifthorder polynomialsto water as a function of temperature and chloride concentraestimatethe densityat a temperature 330 K.356 POLYNOMIAL INTERPOLATION 15. What is your tion is listedin TablePl5. 15. (b) the same coffesponding entropy using qua. second. " o 5 t0 t5 20 25 30 f(x) 36 lB 12 09 072 I 5 A . a Becauseof mcasurcmcnterror.6but using tirst. 5 8 l 5 o 5767 0 5560 4 5242 0 4/08 heated water at 200 MPa to tind (a) the conesponding entropy s for a specific volume u of 0. as well as the precisionof the experimentalmethods.6 for the following tabulateddata: to to measure voltagedrop and corresponding the curent for a rcsistor. ing polynomial and bisectionto determinethe value of x that Suppose that you performedsomevery prcciseexperiments corresponds /(t) : 1. where R is the resistance. u.throughthe resisteras in V However.l2.45 using inverseinterpolation. Use intemolation estimate of to To quantify this relationship.12 The saturationconcentration dissolvedoxygen of in high precision.3 RepeatProb. and fourthorderinterpolating (c) the volume corresponding an polynomials.l lar a the r ( Bec has ( whe dim . 15.5lists valuesfor dissolved oxygenconcentration in water as a function of temoeratureand chloride T ' K 204 254 300 350 404 450 concentratlon. and I 990. Densiry \ 7 0 8 I 3 6 7 I r 3 9 0 .5 Employ inverseinterpolationusing a cubic interpolat. 54 2 9 t t *".
at it depthI (i rl beiou' thc corner of a rectangu.under thc c.'.13 thc datain Table For lmlT I EU:'.iect a total ]oad of 100 t (metric to .2 si.:'"*"'. (#. l s E I /oo 72.6 andh : 14. and a and b are defined in Fis.".iffiiil:temrcraturcs F r c u RP r s . I I I 25 .lhel. I q I z*nJ.iliH.15. n) and lr and rr are dirnensionless ratios.17739 0l o2 03 a4 05 06 a7 OB c 42a26 4.aportionol'whichisgiveninTablePl5. Pl5."" r o s' I t h e o l u t i o n l B o u s : i n c s q 'e q u a t i o n : inat 4 ts cquatto Inc tolloper area.'.7+ t r] + n2+2 J .l5 tt = 1.with nt : al: and rr : b/. l2 _ PRoBLEMS in of Dissolved oxygenconcentrotion woteros o function iemperoture ond chloride {"C) (n c o n c e n t r o t i og / L ) .usea thirdorderinterpolating polynomiat to I ^ I at : 750 'F compufeo. N ( ) t c ' J t re s).U Thc.2 +. L.*':::. /"/ / i esrimare rhe di:solvcd concenrrarirrn o\yscn / " _ _+ I ff.t t' t l r l"a 't " t r" ' i n 1 c n s i t .venl11t J '.UseJ.+#)l Bccau. this fo it has beenreformulatedas o.0856 r 0 t0941 0 13003 o .r I .cchor. polynomial use inter15..I I I f nfU Pt5.t1.sincorrvenient solye nranually.idecollccntrationisequatol0g/L.l oxygcn P15. n) where.a aa7 a l2t14 a l4060 a li5o9 c16613 ltabulatecI.'.rncr of a rectangu15'15The lar footing that is sub.'..f(rrr.specil'ic r I u.*"H"t.4 0 a3aa7 0 a5Bq4 0. : levclfor ?': 18'C with chloride the clissolve.lt3/lb.0 I I t? 3 '3A.ro fo rheeaser.t:tj'Tltt \tre\s.. t n s $ c r i n ( o n n e s p e r s q u a r e n l c t c r .'. DissolvedOxygen (mglt) for Temperoture("C)ond Concentrotionof Chloride (g/Ll s=OglL '.ll:l*.l5a27 1 0 1 6 55 4.f\nt..12.: ' l I r r a r e a : t r b j c c t e d  o l u n i  t r t .45733 0 08323 0 t063t c 12626 0 14309 0 I 5203 0 I 6843 .13241 a.) z*a 7(."a c=l}g/L l?9 c=2Og/L I I I r: I l.Ifa: 1. Bt6 756 lJt 3?9 7 46 685 n '33u 93! 114 673 62A I Iosrr.:'. r r 7 i r eti r r rn b r E r p r e s s ) o u r .D.6 0 03058 a a59q4 a aB709 0l]t35 a. The influcncevalue is then n=1.# TI Forexampre ata I .:ul..i:J^"Ti::#T?.sc cquationi. is calledthe influenccvalut.l5.a 7Ba I t "t I rolunre ol'u :upcrlicrte(l steanr listctl is lS. 1 44 9 7 016199 0 rz3B9 n=1. rI t r r : + n : i l + m 2 n 2 m 2 + n 2 + l L TABIEPl5. 146 ssl I lt..
5 0 3 84 3 30 39 3000 x=6 x=8 15.19). m g.4147 a.16 You measure voltagedrop Vacrossa resistorfor a the number of ditl'erentvaluesof current L The resultsare "y=O y =2 t=4 Use first. 19 Temperatures are measuredat various pointsona at 0 . 2 3 . J = 8  5. m/s2 9 8100 9 7487 9.000 90 000 120. 0 7 3000 2 5 .108 mi/kg with linear l5.04 6000 48 I5 5 00 0 3 5 0 3 4000 2 7 . 3 . on o squore heoted x=2 90 00 64. i entropys for a specificvolurne u of 0.5453 0.00 Temperotures ot vorious ("C) points plote. 7 .85 0 30. Estimate the temperature ( 0 0 0 0 0 ( a ) .7664 . u (m3lkg) s (kJlke.Interpretyour results.  9 x=O 10000 8 50 0 2000 s5. r : 4 . 6.000 m.K) 0 14377 6.45 475 1.6879 9 6278 9 568'2 g cornpute ary: 55. and (c) find the volumeconespond. ) ' : 3 .r25.000 60.90 38.20 Use the portion of the given stearntablefor superheated HrO at 200 MPa to (a) tind the conesponding D e t e r m i n ea l t : 0 . 0 0 2000 t t li.through fourthorderpolynomial interpolationto t = 6 estimatethe voltagedrop tbr i: 1.00 40. find the samecorresponding (b) entropy usrng the surfaceof the earth is given by quadraticinterpolation.49 48. irrg an entropyof 6.15.00 x=4 B0 00 53 . 2 . 5 a70 t5 ]BB 20 60 70.y : 2 . to a 0 01250 624 02500 775 03250 4.358 POLYNOMIAL INTERPOLATION TABTEP I 5. a n d b ) r : 4 .llJ The acceleration due to gravity at an altitudey above interpolation.78 35. 15. I 7 The currentin a wire is measured with greatprecision as a function of time: 425 o. 2 . 144 6.6 using inverseinterpolation.000 !. 5 0 0 0 heated plate (Table P15.
ines.'.*i .. .An alternativeapproachis to of apply lowerorderpolynomialsin a piecewisefashionto subsets datapoints.i. .I INTRODUCTION SPLINEs TO ln Chap... between .f i. ". This For example. Recognizingwhy cubic polynomialsare preferable quadraticand higherorder to splines. o ' Understanding that splinesminimize oscillationsby fitting lowerorder polynomialsto datain a piecewisefashion..i. I6. polynomial. we can derive a perfect seventhorder (at curve would captureall the meanderings leastup to and including seventhderivatives) suggested the points. Understanding how mr. i' Splines nd rrecewrse o Interpolotion l'l .l)thorder polynomialswere used to interpolatebetweenn data points. Knowing how to fit a splineto datawith MATLAB's builrin functions.clamped. Such connectingpolynomialsare calledsplinefunctions.Specificobjectives and topics coveredare . o . thirdorder curves employed to connect each pair of data points are Thesefunctionscan be constructed that the connections so calledcubic spl.and notaknotend conditions.. the Understanding differences the betweennatural. However. Understanding conditionsthat underliea cubic splinefit. .:r. CHAPTER OBJECTIVES The primary objectiveof this chapteris to introduceyou to splines.rltidimensional interpolation implemented is with MATLAB. for eight points. 15 (n ...thereare caseswhere thesefunctionscan lead to erroby neous results becauseof roundoff error and oscillations.. . Knowing how to developcode to perform a table lookup. For example.
In contrast. the spline also connects . order approximationof the splineswould be inferior to the seventhorder might wonder why a splinewould ever be preferable.la throughc illustrates wild oscillations in the vicinity of an abrupt change. The funciionto be fit undergoes obruptincreose r : O. 16.1 illustratesa situationwhere a spline performs betterthan a higherorder an polynomial.The stepincrease changesomewhere an extremeexampleof such a changeand servesto illustratethe point. polynomialstendto swingthrough how higherorder Figure 16. in g o i n d i c o t e h o tt h e o b r u p tc h o n g ei n d u c e s s c i l l o t i o n s i n t e r p o l o i i np o l y n o m l o l s n c o n t r o s t . Figure 16. Porls(o) through l. occeolobJe it are adjacentcubic equations visually smooth.On the surface.I interpoloting to where splines ore superior higherorder A visuolrepresentotion o situotion of (c) ol on polynomiols.This is the casewhere a function is generallysmoothbut undergoes abrupt is depictedin Fig.360 INTERPOLATION AND PIECEWISE SPLINES FIGUREI6. would seemthat thethirdYou expression. lineorsp ine {d) provideso much more becouseit is lirnited strciglLtline coproximotron. t o to connections.1 along the region of interest.
.2 T I N E A R P T IN E S S The notationusedfbr splinesis displayedin Fig.which is the most common and useful version in engineeringand science.A smoothcubic curve resultsfrom interweavingthe strip between the pins. I 6.e n s.l l r o l g . . 2. drafterplaces the paperover a woodenboard and hammersnails or pins into the paper(and board) at the location of the datapoints. l i s< o le d o ' " o ' u r o l s p i n e e oui minimum. 2 L I N E AS P L I N E S R 36r FIGURE6. 16.3. n).(x). we describeMATLAB's capabilitiesfor piecewiseinterpolationincluding its ability to generate splines. ui (16.2 for a seriesof five pins (datapoints). s. Hence.eachfunction is merely the straightline connectingthe two points at eachend of the interval. .the splineusuallyprovidesa superiorapproximation the behaviorof of functionsthat have local.o s e r i eo f p o n i s \ o t i c e l of s [ ' h o w o l t e e n dp o i n l s [ e s p l i n s t r o i g h .2 I ' t T [ e o r c l t i n ge c h n i q u e l s i n go s p l i n eo o r o w.which is definedas ai: Ji (16. Finally.For n datapoints (l : I. is the slopeof the straightline connectingthe points.r) si (r) : ar I bi(x . The conceptof the splineoriginatedfrom the drafting techniqueof using a thin.The process deis picted in Fig. r6.the name "cubic spline" hasbeenadopted polynomialsof this type. Each interval i has its own spline function.In this technique. Ji+r .. As such. flexible strip (calleda spline) to draw smoothcurvesthrougha setof points.ri) where ai is the intercept.Ji . for In this chapter.2) and b.This is followed by materialon the cubic spline.1 6 . which is formulatedas (16. . nio o hc l r u e .3) . For linear splines. abruptchanges.simple linear functionswill first be usedto introducesomebasic conceptsandissues associated with splineinterpolation. Then we derivean algorithmfor fitting quadraticsplinesto data.I intervals. there are n .
O r d eS p l i n e s r Problem Stotement. (15. The data can be substituted into Eq.r .3) for Ji (.I with firstordersulines. TABIE l6. Fit the data in Table 16..4..4) to generate the linearspline functions.4)indicates to that the linear spline amountsto using Newton'sfirstorderpolynomial[Eq. (.5)] interpolate within eachinterval.3 I l \ o ' o ro n u t e dl o d e ' ' .).4. Substituting Eqs.r .5 Solution. (16. Then the appropriate equation usedto determinethe function valuewithin the interval. I 2 3 A Dototo be lit with splinefunc]ions. (16. (16. _ f. E X A M P L6 .Evaluate the functionotx:5.l.. es o l ' ^ e s Notice thot therecre n .I intervols ond n dcio points gives where li is shorthand l(r.f.ti ) (r6.2)into Eq. the function is For exarnple.1)and (16.0 :::t.5 0. l ix. for intervalfrom x : 4. (16.l .4) Theseequationscan be usedto evaluatethe function at any point betweenx1 ard. I IE F i r s t ..r ) : .51 1.4 90 25 I0 2.5 .5 to x :7. r L)l' rirlri f. is by first locating the interval within which the point lies.362 SPTINES AND PIECEWISE INTERPOLATION J(.0 + 251.. 30 45 7.0 . the second sr(x ) : 1.tt attttrr_ FIGURE6.Inspectionof Eq.
4aindicatesthat the primary disadvantage firstorder of splinesis that they are not smooth.the following sectionprovides an applicationwhere linear splinesare useful. the data points where two splinesmeet at (called a knot). 0 + .( 5 1 .5): sr ( x ) : 1 . .4 .as will be discussed subsequently.2 TINEARPTINES S 363 FIGURE 6. s . o c u b i ci n f e r p o l o l i p o l y n o m i o l s op l o t t e d . Beforedoing that.l Visual inspectionof Fig.the first derivativeof the function is discontinuousat thesepoints. This deficiency is overcomeby using higherorder polynomial splinesthat ensuresmoothness the knots by equatingderivativesat these at points. the slopechanges abruptly. ng The equationsfor the other intervalscan be computed. 0. 16.t .o )L i n e o rp l i n e(.b )q u o d r o f is p l i n eo n d ( c )c u b i c p l i n ew i t h f o s c . l 6 . 5 l. 5.In formal terms.I6.In essence. 3 . 4 c tT h e v a l u ea t x : 5 i s 1 . .4 T S p l i n ei t s f o s e lo f f o u rp o i n i s{. 2 . 0 4.and the resultingfirstordersplines a r ep l o t t e di n F i g .
end ? linear . One way to do this would be to passtlreMfile to the temperature which you want the interpolation be performedalong with thetwoadat joining values. or a For ordereddata. it would search independent the able vector to find the intervirl containingthe unknorvn. this simpleMfile that accomplishes objective: functiori n if xx error yi = Tablelook(.search.xx is comparedwith the value at the top of the second val. end i = i + 1. to determine thanthevalueatthetopoftheinterval.At this point.2. The first is called value sequentialsearch.Here is an Mfile that performsa binary search . A r r r [ ] l s .this can be doneby testingwhetherthe unknownis lessthanthe value one lf assessed. error trap is included ento an dependent hex . sort is inetTicient because For situationsfor which thereare lots of data. variThus. Before searching. there are two simple ways to find the interval. y.suppose that you would like to set up independent and dependent teman Mfile that would use linear interpolationto determineair density at a particular peraturebasedon the datafrom Table 15.r.For example.x(i+1). the interpolation be performedsimply shown. we know that the unknown falls betweenthis value and the previous is Here a that we examined. simple a followed alternativeisthe binan.For datain asbeing cendingorder. break. so. The loop is repeated until the xx is lessthan or equalto the interval'supperbound.. while (1) if xx . breakloop s u r e t h a t t h e d e s i r e d v a l u e x x f a l l s w i t h i n t h e r a ntg e o f' s . whether isless it the compares value at which the interpolationis desired. rr) cngrn'X). In thesecases.interpolation yr = 1. variablesare storedin ascending order in the arrayx and the The table's independent variablesstoredin the arrayy.olation outside search range' ) end 13 seclLrential i .xx.364 IE S P L I N EA N D P I E C E W I SN T E R P O L A T I O N S 16.It is useful tbr variables.This is called a table lookup.As the name implies.15. more generalapproach would be to passin vectorscontainingall thedata A and have the Mfile detenninethe bracket.Forcaseswherexxisinthesecondintetval or higher. < r(1) xx > x(n) (' Inter1.1 Toble lookup in and A table lookup is a common task that is frequentlyencountered engineering science performing repeated interpolations fiorrr a table of computer applications.1. we move to the next value and repeatthe comparison.First.(i) + (y(i+1) y(i))/(x(i+1) x(i))*(xx x(i)).in can as which casethe loop is exited. the Mfile would perform two tasks.this will not testtrue at first.the sequential it must searchthrough all the precedingpoints to find values.this methodinvolves comparingthe desired with each elementof the vector in seqnence until the interval is located.lf not.Then it would performthelineat in interpolation usingone of the techniques described this chapter in Chap.<.= x(i + 1). In this casethe counter i is incremented one by intetso that on the next iteration.
densit\'. : r 3 1 . break.the index at the rridpoint r1.Although third and higher . u) / z). The approachis akin to the bisectionmethodfbr root location..The process repeated is until the difference index is lessthan or equal to zero. t'1" / (x(iL+1)x(iL) )*(xx :.r = t 4 0 0 2 0 5 0 1 0 0 1 5 0 2 0 0 2 5 0 3 0 0 .s70s : To ensurethat the nth derivatives contiDuous the knots. the linear interpolation pertbrmed. either the lower or upperindex is redefinedas being the betweenthe upperand the lower middle index. 3 Q U A D R A T IS P L I N E S C 365 by linear interpolation: f unction n tf = yi = T. 5 : 1 . 9 4 6 . 2 I .At this point.4 computedas the average the filst or "lower" index i L = 1 is of with the valueof and the last or "upper" index iu : n.I 6 . > . enl t. < x(1)  zx > x(. ilnd Here is a MATLAB session illustratinghow the binary search function can be applied to calculate air densityat 350 "C basedon the datafi'ornTable l5. 6 1 6 . the lower index lies at the lowel bound of is the interval containingx:. Q 9 .rbleLookBrn (x. range') end % binary l.f rf tL/L:'tY((tt < x(iM) rT = rI{: t:lse a:Ilal end % Linear yi 1i inLerpolati.1.n) €irror: ( 'InterI. \'.The sequential the searchwould be similar.:.Thirdorder be or that ensure and secondderivativesare most frequently used in practice.the loop terminates. > > d e n s i t v = f 1 . Dependingon where it lies. rvh i le r. xx) Lenqlh (x ) .olation outside zx search iu (1) iU rL .nust used.616 1ffi(3s0 I6.l5 O l 0.l 4 6 .on . The unknownxx is thencompared whetherit is in the lower half of the anay or in the upper x at the midpoint r (iM) to assess half.= xx l.a splineof at leastn * I are at polynomials cubic splines continuous llrst order r.' >> TableLoo]<Bin (T. 6 1 1 . 2 9 I . 10 0 5 0 o l .Justas in bisection. ' .3 SPTINES QUADRATIC os?5o615 . i l. .5705 This result can be verifiecl by the hand calculation: : * I (350) 0.300) 0.(rL) ) .
m e a n st h a t t h e r ea r e 2 ( n .5): .the nurnberof condiwe tionsto be evaluated now beenreduced 2(n . . t. S i n c e h i sa m o u n t s t o t nl t c o n d i t i o n si .the constantin eachquadraticmust be equal to the value of the dependent variableat the beginningof the interval.r. .rportance.(ri . are Because derivationof cubic splinesis somewhat the irrvolved. ..r. therearen . ( 16.Eq.) f ri (. or are 3rr to the These canbe developed fbllows: as l. .consequently.hl : fi+t (16.8) T h i s e q u a t i o c a nb e w r i t t e n o r t h e n o d e si.fi I bihl t c.(r ) : l. has to The function valuesof adjacent polynomialsmust be equalat the knots.we havedecided first to "quapolynomials..t . equations conditions required evaluate unknowns. The objectivein quadraticsplinesis to derivea secoudorder polynomialfor each interval betweendata points.This is called a continuitycondition.This condition can be writterrfbr knot i F I as .These illustratethe conceptof splineinterpolationusing secondorder dratic splines"havecontinuous first derivativesat the knots. . The function must passthrough irll the points.(. n). .( n .It mathematically can be expressed its . Note that because have detenninedone of the coefficients. 7 ) This equationcan be simplified nrathematically defining the width of the ith interby val as hi :xi+txi Thus.: 1 .1 (the intervals and..7 ' ) : n 1 remaining conditions.r.1 ) .\. .7)simplifies to .1 i + r ) 2 1t (1 6 .fi : ai I b. f i + r * b i + r ( x i + r .)l which sinrplifies to ai : Ji (16. derivatives be discontinuous can they usuallycannot detected be visuallyand consequently ignored. 16. a 1 ( .This resultcan be incorporated into Eq. n n f l .xi) * c. 1 x.r .366 SPLINES AND PIECEWISE INTERPOLATION when usingcubicsplines.t i ) f c i ( r i + r . .l) unknownconstants d's.x i ) * c .lt.x.2.5) wherethe notationis as in Fig.6) EX Therefore. andc's)to evaluate.x i ) 2 : . .)l 2. b's. they servenicely to demonstrate generalapproach dethe for velopinghi gherorder splines. .Although quadratic splines are not of practicalin.t.r i + r ) f c .For n datapoints (i : 1.r . . 3(n Therefore.The polynonial for each interval can be represented generally as s i ( r ) : a i t b t Q .filbi\i+r .r i ) 2 (16.(16.3.l).
filbrlt1 :7. must make an we arbitrarychoiceto successfully computethe constants.I :2 an 3 : 0): recallthat cr br:bz b z* 2 c z h z : b t The necessary furrctionand interval width valuesare l?.Equation(16.I6.This meansthat thereis n .r . this conditioncanbe expressed mathematically as ct :0 The visual interpretation this conditionis that the first two points will be connected of by a straight line. Unlesswe have some additionalintbrmation regardingthe functionsor their derivatives. because meansthat adjacent it splineswill bejoined smoothly.lcfi\:7.8)iswrittenfbri:1 through3 (with cr : 0) to give .2) : I remaining condition.0:2. Use the results estimate valueat.2 Q u q d r o i i cS p l i n e s Problem Stotement.I (Table16. . after applying the continuity condition and the zero secondderivative condition.0 .i f I can therefore written as at be bi *2ciht:bi+r 116.5)is 2c. 0 l.?. 0 . (16. l ' z * b 2 h 2. Equation( 16.\ { ll: I '\ conditions(again. Assumethat the secondderivativeis zero at the first point.?s l.we selectthe fbllowing condition.we havefourdata pointsand rz: 3 intervals. J z: 1 .91.2 conditions.3 AUADRATIC SPLINES 3. 5 14:9. Writing this equation for all the interior nodes amounts to . Solution.r : 5.r1 bi l2cii.4 .this meansthat2(41)l:5conditionsarerequired.. 5: 2 .creirtes additional ..l). 16 EXAMPLE . to the problem. Continuityof derivatives.xi) The equivalence ofthe derivatives an interiornode. (16. Because secondderivthe ativeof Eq. Although thereare a numberof different choicesthat can be made.5)can be ditferentiated yield to : sj(. Eq.(n . :4 . For the present Therefore.9).v. 367 The first derivatives the interior nocles at must be equal. ratherthanin thejagged fashionthat we saw for the linear splines. Fit quadratic splinesto the samedataemployedin Example16.:o5 h t : 1 .h.l c 2 h i : f t ) Jt lb.1 4.This is an importantcondition.
The cubic splinesin thenext section do not exhibit these shortcomings and. . can be substituted the into original quadraticequationsto developthe following quadraticsplinesfor eachinterval: sr(x)2.r0) T h u s . r ) : a i ' l b . bettermethods are for splineinterpolation. 5 1 2 9 . 2 ( .Cubic splinesareprefered because they provide the simplestrepresentation exhibitsthe desiredappearance smoothness. ( .The shortcomings Iinearand qr"radratic of splineshave alreadybeendiscussed. 16._ r . r i ) 2 + r / . to Consequently. t h e r ea r e / ..5 4 .1 .. . ( .7 . 0 ) .368 SPLINES AND PIECEWISE INTERPOTATION These valuescan be substituted into the conditionswhich can be expressed matnx in form as Theseequations be solvedusingMATLAB with the resulrs: can br:l b:.s 4 . 6 4 ( x . 1 ( . r . as a consequence. r .. f o r n d a t ap o i n t s ( i : 1 . r . 2 . : 1 h. . .. ( . 1 ) . r .r2to make the prediction.I i n t e r v a l s n d 4 ( n . 5+ 0 . 0 ) r Because : 5lies in the secondinterval.l 6 j ll fl [Ii'i' {l} These results.4 CUBTC SPLNES As statedat the beginningof the previoussection.4(.r . s ) 0 . along with the values for the a's (Eq. 0.1 . 16. ( .. r. 1 ( x:) 2 .. r ): 1 . 16. l) conditions required their are for evaluation.Notice that therearetwo shortcomingstlratdetractfrom the fit: (l) the straightline connecting first two pointsand the (2) the spline fbr the last interval seemsto swing too high. they tend to exhibit the instabilities Quarlic ol higherordersplinesare not usedbecause inherentin higherorder polynomials. 0.cubic splinesare most frequently used in practice.r. . r i ) 3 (r6. 5 ) r .we use.6).J .4b. r 1 ( 5 ) 1 . 6 ( r .+ .7 .( . .)) . .1 . ) * c i ( . A A.5(x3) t s . 6 6 : + The total quadraticsplinelit is depictedin Fig. . 5 + 2 .l ) u n a known coefficients evaluate. r. x : . that of Tlre objective in cubic splinesis to derive a thirdorderpolynomial for eachinterval betweenknots as represented generallyby s . . 6 .
Tlrat is. therefore. Two of the more popular are the clampedcondition anclthe notaknotconditions. Specification suchan endconditionleads what of to is termed a "natural" spline.hencemaking the resultasymmetric. This resultcan be incorporated Eq.4 CUBIC PLINES S 369 The tirst conditionsare identicalto thoseusedlbr the quadraticcase.11) x i ) * c i ( .l c y h i .2). t i ) 2 * d i ( x r. would havethe 4(n .3 (t6.r to assurne is that the second derivatives the first and last at knots areequalto zero.We will describetheseoptions in Section 16. apply them evenhandedly both ends. two additionalconditionsare requiredto obtain the solution.x .The visual interpretation ofthese conditionsis that the function becomesa straight line at the endnodes. f i + r (16. For cubic splines. In additronto these. we will apply the conditionthat eachof the cubicsmustjoin at the knots.In that case.A very common approacl. we are in the advantageous position of needingtwo additionalconditionsand can.I equations. in There are a variety of other end conditionsthat can be specified.conditionsare developedto ensurethat the secondderivatives at the knots are also equal. Once the additional end conditions specitied.we had to arbitrarily specily a zero second derivativefor the first interval. the After theseconditionsare developed.1) condiare we tions needed evaluatethe4(n .these last two conditions can be formulated in severaldifferent ways. at For cubic splines.t t ) 1 h l: .This is a much nicer outcomethan occurredfor quadraticsplineswhere rve neededto specify a singlecondition.r.4.12) Next. (16.For knot i * 1.ti) J^i bi(r * ci(x xi)2* di(r .I ) unknowncoefficients.the simultaneous will be tridiagonal equations and hencecan be solved very efficiently.I6.the constantin eachcubic must be equal to the value of the dependent variable at the beginning ofthe interval.Although the derivationof this approachis less straightforward than for quadraticsplines.10): into s .2. they are set up so that the functionspassthroughthe pointsand that the first derivativesat the knots are equal. J'i: ai I bi6iwhich simplifies to ai : . r r .This greatlyenhances fit's smoothness. ( . Further. this can be represented as J ' i I b i h i . 16.4. 16.we will present altenrative an approach that requires the solutionof only n . to Whereasit is certainlypossible to developcubic splinesin this fashion.the gain in efficiencyis well worth the effort. It is given this name becausethe drafting spline naturally behaves this fashion(Fig. will lirnit ourselves natural we to splines..v ): l .fi (16.1 Derivqtionof CubicSplines As was the case with quadratic splines.the lirst condition is that the spline must pass through all the datapoints. For the following derivation.13) . ) l Therefore.
: f. .rr) * 3tli7 . I f 1 can thereforebe wntten as ci l3dihi : ci+t Next.h i ) c i * h . (16. c . 1 7 ) (r 6r 8 ) This can be substituted Eq.23)and the resultsimplified Equations into to yield t I t 1 .1 c 1 1 2 ( h i r .15) The second derivatives the interiot'nodes at must alsobe equal.(x . * rJi .21)and (16. 1 5 ) o g i v e c id r b i + t: b i I h i k i i ci+r) (r6.:ci+l J11.Equation(16. .14) can be difTerentiated yield to sj'(r) : 2ci * 6di(x . o.Jrl t ( 16.) ( 16..20) Equation(t6. 1 8 ) a na l s ob e s u b s r i r u r en t o E q .rti h. ( 1 6 . ci (1 6 .r1) (r6.22)can be sr"rbstituted Eq.17) d1 fbr : t u. (16.20)can alsobe reduced l: by b i : b i .xi) ( I6 .J ' fffr h.^ .c^ i tc. r : 3 1 I } .72) The index of Eq.r * f t r r ( c i .f'r lli1 llC.r * c . 1 6 ) The equivalenceof the second derivativesat an interior node. .t .1 : . . (16.i3) ro give into h? Ii I bihi t lt2ci* t c . . f.'l (16.I * I can therefore be written as of bi l2ciht +3dihl : bi+l (16.xi)2 (r6.370 SPLINES AND PIECEWISE INTERPOLATION where hi:xi+txr The first derivativesat the interior nodesmust be equal.21) .1) : f i * t (r6. trC. ( 16. l).r9) ( E q u a t i o n 1 6 .21) The inder of this equationcanbe reducedbl' .19) can be solvedfor .r4) The equivalence the derivativesat an interior node. we can solveEq.12)is dift'erentiated to yield sj(x) : bi {2c.Equation (16. hi.i l / .23) (16. hi .
26) becomes c u .25) can be written for the interior knots. c2.. '.b and d.2a) can be written as /r11ci1 2\hrr * .2'7) .Eqs. this condition amountsto settingc1 eQual zero. .l t r .l /[. .2s) . : si'(. t il ri "f[. we havetwo additional cr. r]) ( 16.h)ci I hici+t: 3 ( . ( x . (16.E qc a n b e s e t t o z e r o a s i n 16l6) . 'n . to The sameevaluation be madeat the last node: can s j j .t t 6d. r'f Therefore.rr]) 3(flx.flx.ri. caseEq. to impose a zero secondderivativeat the last node. we can convenientlydefine an extraneous parameter in which c..l qolve (16. r i ]' f. naturalspline.tz. tlt.t. .I unknown coefficients.. 15.2' which Equation (16." .t1 I Cnl cil 0 3(..f[x:.t * 3 d .S I6. 0 fhc srrqtem is tridironnal and I . (16. . t h e s e c o n d d e r i v a t i v e a t t h e f i r s t n o d e (.21) cients.we set ('/? 0.18)can be usedto determine remaining Once this is done.rr.i + r .3. The final equations can now be written in matrix form as [1 I ht Cl I 2 ( h t 1 h ) hz C2 I I I L h. Jt rl rirl Therefore. One common approach.f... (.z h.r.15): sidearefinite differences righthand / t r i . the coeffiand (16.assumes the that the secondderivativesat the end knots are equal to zero. . i :2. As statedpreviously. .x.4 CUBIC PLINES 37r This equationcan be made a little more conciseby recognizingthat the terms on the (recallEq.3 sinrultaneous we conditions.26) Recalling Eq.rr) 0:2ct ( * 6 d 1 x 1. To see how these can be integratedinto the solution s c h e r n e . r 1 ) Thus.h.rn rl ...r2.the two additionalend conditionscan be torrnulatedin a number of ways.( 16.. 1[ .:)0 :2c.17).Eq.t (16... can solvefor the c's. tridiagonal equations with n results in n .1 : c r r: 0 Thus.. (16.t 2(.
3 EXAMPLE S P L I N EA N D P I E C E W I SN T E R P O L A T I O N S IE NoturolCubic Solines Problem Stotement. (16. .r 1.186565212 dz : 0..166539924 b1: b2 : 1.s ) 0 .121156654 .0 f)\ h 1: ! .3 . 20.27 to generate setof simultaneous tl. t E q u a t i o n ( 1 6 .166539924(. The first stepis to employ Eq. 8 3 9 .0 .r.l{ii} 1'l rt.1 16.022053232 These results.ll o // 1. 5 ) 2. in sj(.1l)1. 5 hz:7..r.2 Il lll167t.022053232(x.4 . 0.1 .31l . t 6 0 4 5 6 2 1 4 ( .0 . s 6 6 r . which talls within the second the interval. valueat r : 5.5:2.5 Theseequations be solvedusingMATLAB witlr the results: can cr :0 ct : 0.s Thesecan be substituted yield to Ir lr5 L .r.0)r . t t Solution. can be substituted into Eq. 2 1 ) n d( 1 6 .160456274 cz:0. For example.2 .0 . . 5 . 16.372 .7. .*x.0:2. r : 5 ..r) : 2.r.. t r + 0.5 .t2thtrhttI:l I : I 3ltt//l lrrr r..1 . .5 ) + 0 .l ( 7 .r .119711863 1).0.l0) to developthe following cubic splines eachinterval: for s r ( .5)r s t 0 . r ). 1 6 0 4 5 6 2 1 4 (r 4 . 2 1 4 t 4 4 4 8 7 ( 5 4 . 4 1 9 7 7 1 8 6 3 ( x 3 ) + 0 . 1 8 6 5 6 5 2 7 2 1..5 ht:9.01. 1 2 7 7 .. 0 ) ' Tlre threeequations can then be employedfo computevaluesrvithin eachinterval..0 l+ : O.01. The total cubic splinefit is depicted Fig.r 7.r . J . 1 1 r s 2 ( x ): . 1 ) U t i l i z et h er e s u l t so e s t i m a t eh ev a l u ea t .re equa) tions that will be utilized to determinethe c cocfficients: hz) I  .4c. (16.2 and ( T a b l e1 6 .along with the valuesfbr the a's lEq. 1 8 ) a nb e u s e d o c o m p u t eh e b ' s a n dd ' s s a c t t h :0. 8 3 9 5 4 3 1 2 6 ( 5 4 .is calculated as s 2 ( 5 ) : 1 .l6. 0 : 1 . 0.5 +0. Fit cubic splines the samedatausedin Examples to 16. (16.214144181 dt :0.831)543726 c+ : 0 dt :0.5 + 3 7 2 6 1 r 1 .0) .I 'llI o' I l:l I I I The necessary lunctionand intervalwidth valuesare 1t :2'5 fz : 1.r e 8 2. 5 ) 3 5 : L102889734.
spline fit for the clarnpedcaselevels off at the ends.25t fbr the interior knots. l l .For example.4 CUBIC PLINES S 373 T h e r e s u l t s oE x a m p l e s6 . x1 .3 . interpolating in . gressiveimprovementof the flt as we move from linear to quaclratic cubic splines.Because hasnonzerosecondderivativesat the ends.4. The clampedcaseis setup so that the derivatives the endsareequalto zero.. ' . . 1 a 2 h .it yieldsthe sameresultasis obtained polynomialof the sortdescribed Chap.. whereas cubic polvnomial has no such constraint. " NotaKnot" End Condition. i<r.n2 .In contrast. . o 1) l6 Figure 16.:Q . 3 a r e s u m m a r i z e d i n F i l 6 . 16. / i 2 . . : Q (rvhere cnd 7. 2 .l.3. i:2. Firsi ond lost Equotions r : 1: Q . h r = 1 c .A third alternative to fbrce continuity of the third deis knots. . 1 t . tt6. 15. lloto knol 2 h 1 t 11 ' f t 1 r ' .This option involvesspecifyingthe first derivatives tlrefirst it and lastnodes. This is sometimes calleda "clamped"splinebecause is what occurs when you clamp the end of a draftingsplineso that it hasa desiredslope. 4 . naturalsplineis only one of several the end conditionsthat can be specified splines. at As expected. 6 .This is due to the fact that the naturalsplinerequireszerir secondderivativesat the end knots.the the natul'aland notaknotcasesfollow the trend of the data points nrore closely.i 7 J . at Clunlted End Condition.It hasthe additionalpropusingan ordinarycubic erty thatfor firur points.tIhu 2c'.We to a polynomialou Fig.4c. These conditionscan be readily applied by using Eq.lore thespeciled Cicmped first .{ l t t * h : l c z * / z r r ' :: O hn tcn)0t. . Sincethe first internal knotsno longer eachofthe first and iasttwo adjacent represent thejunction of two different cubic functions.:I11. if zerolirst derivatives specilied. TABTE16.2 End Conditions Althoughits graphical basisis appealing. splinewill level off or becomehorizontalat the the are ends. l t h r o u g h 6 ..Although the cubic have also superirnposed cubic interpolating spline consistsof a seriesof thirdordercurves. a n d u s i n g f i r s t ( l ) a n d l a s t e q u a t(in n s a s w r i t t e n i n T a b1e . . : 3 l .5showsa comparison the threeendconditions appliedfo tit the datafrom of as ?rble 16. specimeansthat the samecubic functionswill apply to fying continuous third derivalives segments.. .3 .they are no longer true knots.this caseis refenedto as the "notaknot" contlition.the notit aknotexhibitsmore curvature.I6.the resultingfit differs fronr that obtained usingthe thirdorderpolynomial. . r .Notice how the the natural spline tendsto straighten as would be expected out because secondderivatives go to zero at the ends. for Two of the mostpopularare ..Sincethe splinealready specifies that rivativeat the second andthe nexttolast the tunction value and its first and secondderivativesare equal at theseknots.. . Hence. N o t i c e t h e p r o f l l g. . the 16.li respeci dtrlvotlves first vely) ct the ond iostnodes.l h : t t . t r l .2 Condition Noiuro Thefirstond lostequotions needed specify to somecommonly usedend o c o n d i t i o nfs r c u b i cs o l i n e s .
5.4 1 S p l i n e si n M A T L A B Problem Stqtement. )<x) (16. 16.re usesthe notaknot condition. doto from Toble 16 l I6.7) : f(x): I  + 25x2 . y.then the first and last value in y areusedas the derivatives the at end points. in The pchip funcfunction performscubic splineinterpolationas described this chapter.1 MATTAB Function: spline Cubic splinescan be easily computedwith the builtin MATLAB function.splir. By default.However. tion implementspiecewisecubic Hermite interpolation. spline.if y contains more two valuesthan x has entries. Consequently. Ithas the generalsyntax.28) where x and y : vectorscontaining the valuesthat are to be interpolated. can also perform a numberof othertypes but of piecewise interpolation.5 nofoknot. yy = spl ine (x. EXAMPLE 6.374 AND PIECEWISE INTERPOLATION SPLINES f(x) 2 t 1 0 I6 FIGURE .andyy = a vector containingthe resultsof the splineinterpolation evaluated the pointsin the vector as at xx.The interpl function canalso implement spline and Hermite interpolation.5 PIECEWISE INTERPOTATION MATTAB IN The spline MATLAB hasseveral builtin functionsto implementpiecewise interpolation. this option providesthe meansto implement the clampedend condition. noturol for ond splines lhe Comporisonof the clomped (wilh zero firslderivotives). Runge'sfunctionis a notorious example of a function that cannot be fit well with polynomials(recallExample r 5.
(a) The nine equally spaced datapoints can be generated in as >> x = >> ]1 L.1). a vectorof valuescan be generared tFlat can crearc sn?ooai we Next.^2).the notaknotspline doesa nice job of following Runge'sfunction without exhibiting wild oscillationsbetweenthe points. o f f i : 1 a n df i . ./ (1+25*xx. (x.5 PIECEWISE INTERPOLATION MATTAB 375 Use MATLAB to fit nine equally spaceddata points sampledfrom this function in the interval[1. (b) The clampedcondition can be implementedby creatinga new vector yc that has the The new vector can then be used to desiredfirst derivativesas its first and last elements. (i+25*x. linspace( (x. I6.6.Finally. ' ) As in Fig.xx) . l]. we cau generate along with the splinefit and the original data: >> yr >> plot = L. xx. 16.6 FIGURE funciion(doshedlrne) spllnefit generoted wilh Comporisonof Runge's with o 9poinlnotoknot MATLAB(solidline).. xx. yy. a more finely spaced so plot of the resultsas generated with the spline function: >> xx = >> YY sPLine 1.9).Y."2) .y .I.Employ (a) a notaknot splineand (b) a clampedsplinewith end slopes 4.IN I6. linspace(I. Recall that I inspace automaticallycreates100 points if the desirednumberof points are valuesfor Runge'sfunction itself and display them not specified. yr. : Solution. 'o ' ..
clampedsplinetendsto improve the fit.yc.5 F I G U R EI 6 . the interpolation looks like a seriesof plateaus.v e c t o r s c o n l a i n i n g v a l u e s t h a t a r e t o b e i n t e r p o l a t y id a .xx. 7 (doshed wiih o 9 pointclomped functlon line) Comporison Runge's of end spline generoted fit withMATLAB Notethotfirst derivoiives I ond 4 orespecifred thJlelt of ct ond isolidine) r i g hb o u n d o .7. 'nearest 'nearest neiehbor interpolation. >> yyc = spline(x.376 IE S P L I N EA N D P I E C E W I SN T E R P O L A T I O N S 0. Thus.It has the generalsyntax yi = interL. .2 MATTAB Function: interpl The builtin function interpl providesa handy meansto implementa numberof different typesof piecewiseonedimensional interpolation. other examples. the results of the interpolationas evaluatedat the points in the vector xj. polynomials.y. v e c t o r c 0 n e :.xx /yljc. 1 (x. which can be thoughtof as zeroorder ' I inear ' linear interpolation. .xx).5. i e s ' e s p e c r . >> plot (x. e l . This methodusesstraightlines to connectthepoints. This methodsetsthe valueof an interpolatedpoint to the value of the nearestexisting data point. xi. 1:. 'o' .'') As in Fig. The various methods are . where we haveknowlthe edgeof the true first derivatives. ' EXA generate and plot the splinefit: >> yc = [1 y 4]. 16. .the clampedsplinenow exhibitssomeoscillationsbecause the artificial of In slopesthat we have imposedat the boundaries. 'rneLhad t w h e r e x a n d y . 16. and taining 'methocJ' : the desired method.yr.
ine'piecewise cubic spline interpolation. is the The pchip option (short for "piecewisecubic llermite lnterpolation")merits more pchip usescubic polynomials connectdatapoints discussion. .' o'.it will be moreaccurate the dataare valif ues of a smoothfunction.(c) cubic splinewith notaknotend conditions."That is. On the other hand.they are expressly chosenso that the interpolation is "shape preserving.The results the of using spline will generallyappearsmoother because humaneye can detectdisthe continuitiesin the secondderivative. You perfom. Note that you never decelthe erate during the experiment.as well asthoseinvolving the otheroptions. 16. with linearinterpolation.. .and (d) piecewise cubic Hermite interpolation. = finspace ( O.v. >> v = l0 2A 20 38 B0 80 100 10A \25 7251. (a) The datacan be entered. >> vl >> plot (t.5 TrodeOffsUsing in.''1r l Problem Stotement. As to with continuousfirst derivatives.The time seriesof spot nleasurements velocity can be of tabulatedas 2a 20 4A 2A 56 3B 6B 80 BO BO B4 r00 96 t00 lo4 t25 I t0 125 Use MATLAB's incerpl fr"rnction fit this datawith (a) linear interpolation. defaultis linearinterpolation.ThesetradeofI\. the interpolatedvaluesdo not tend to overshootthe data pointsas can sometimes happen with cubic splines.(t. This is identicalto the spline iTItt"Ji una ' cubic 'piecewise cubicHermiteinrerpolarion.it ditTers frorn cubic splinesin that the second derivativesarenot necessarily continuous. Solution.8c)are not smooth.ra test drive on an automobilewhere you alternately accelerate automobileand then hold it at a steadyvelocity.\n) ). Therefore.rt).' o' .tt. 110 ) . EXAMPLE 16.[0 20 40 56 68 B0 34 95 t04 110]. If the 'method' argunrent omittecl.vl) The results(Fig.therearetradeoffsbetween spline and the pchip options. with cubic splines.but do not exhibit any overshoot./.5 PIECEWISE INTERPOLATION MATTAB IN .I6. plottedwith the fit and following commands: >> r .interpl (t.'.pchip has no overshoots and lessoscillation if the dataare not smooth.'nearest' >> nl ol. nearest (b) to neighbor.. 377 'spL. Further the first derivatives the knots will not at be the sarneas for cubic splines.Rather.In addition.. tt. e.are exploredin the following example.However.v. tt. >> tt = incerpl(t. (b) The commandsto implementand plot the nearest neighborinterpolationare >> vn .
However. v. on funciionto performpiecewisepolynomiol nterpolciion o velocilyiime serles This option is neithera smooth As in Fig.This makes it appearthat the automobiledecelerated . tt. 16. (c) The commandsto implementthe cubic splineare >> vs >> plot = (t. nor an accurate depictionof the underlyingprocess." . .' o ' .8b.. interpl ( t . 16.8 Use of severoloptionsof the rnterpt toron ouiomobile. t t .severeovershootoccursat several severaltimes duringthe locations.8c)are quite smooth. Theseresults(Fig.the resultslook like a seriesof plateaus.378 IE S P L I N EA N D P I E C E W I SN T E R P O L A T I O N S 140 tzu 't40 120 100 80 60 40 20 bu {a} linear IUU 100 80 60 40 20 120 100 60 80 40 (b)nearest neighbor 140 120 100 80 bU 120 140 120 100 80 60 40 20 60 ( c )s p l i n e 100 '120 40 20 100 120 FIGURE I6. v s ) 'spline' ) .
f(rr. I 6.6 INTERPOTATION MUTTIDIMENSIONAL problemscan be extendedto multidimenThe interpolationn. continuityof the f irst derivatives the knots at makesthe transitions betweenpointsmore gradualand hencemore realistic.Wewanttointerpolatebetweenthesepoints "f(xr.1 BilineorInterpolotion 'lnaodintensional interpolctrion deals with determiningintermediatevalues for functions of two variables z: .9. 16.. nature.8d)arephysicallyrealistic.r'r). o l e { l i o or r " d g c i r c e ) se s t i m c t e d s e d n f o u r i v e n o l u e{ o p e n i r c l e s ) i v s bo o c f(xz.'o'.6. will describe simplest In the caseof twodimensional we interpolationin Cartesiancoordinates.vh) 379 For this case.'pchip').r':).. this section.t:.rethods onedimensional for sionerl interpolation. (t.I 6. c o le p c l i o n o { o .r'r). As depictedin Fig.tt. yt) .\/.f (xi. addition. 16.6 MULTIDIMENSIONAL INTERPOLATION (d) The commandsto implementthe piecewisecubic Hermite interpolationare >> vh = >> plot interpl(t.we have valuesat four points: yz).. l e ' n e o o e ..andl(. ]r).f(rz.. the Because ofits shapepreserving rnclnotonically never exhibit deceleration. results(Fig. 16.the velocitiesincrease and Although the resultis not as smoothas for the cubic splines. tt. 9 rv G ' o p r .we will describeMATLAB's capabiliIn ties for rnultidimensional interpolation.d r r r e r s i o n lo ln e o ' i n e p o l o ' i o r w l e e o n .V. F I G U R EI 6 .
r.:) .'t): .!r l l ' ( ..r. r ' 1 )t }: lr \'' ) . (16. x2 .J:) . r ' d i m e n s i o n t o y i e l d t h e s iu l oo t .^ r r n e ..Usingthe Lagrange form.\'l \'.rrr)is .r . 16. r o i m e n s .r _ r': )Z (16 34 {i . .IO I6 Two dimensionol bilineorinlerpolction con be implemenled f irstopplyinq one dirnensioncl by ' e o ^ l ^ ro c l o l i ^ no r o n q' n e . ri t: ]i . v : ) .r1) is . If we use a linear function. r ef n t i to estimatethe value at an intermediate nt / (ri..10) Thesepointscan then be usedto linearly interpolate along theI'dimension to yieldthefinat result: . / ( x i .Suchfunctionsare calledbilinear. r . .ft.. l { . . r r .r.v r ) (16..9.rt t r : . r .r rJ r )+ S f .rl . the resultat (r.{l )z _ ).rr) f f(x.. .ri. . o L e co .lr ^. r ' . i o l e s c o r t ^ e nb e . poi the ) sult is a plane connectingthe points as in Fig. . r .r ' r ): 3 f Jt andat (. 16.29) and (16. .2s) .) rl . _vi.r'r 12 .i .380 S P L I N EA N D P I E C E W I SN T E R P O L A T I O N S IE EX FIGURE ..t.rdiin rection. we can hold the v value fixed and apply onedimensional lineu interpolation the.10.rr ). . A simple approach developingthe bilinear tunction is depictedin Fig. . v. . o r o d e . u s e d t o i n e c r l y i n t e r p o l o t e o l o n g l h e .. r .1)l. for First.  \'r 116..r .v: r'' 't2 't '{ixt '\'i . .Xt . : : } . h e .30) into Eq.. )'r .rt ): .fZ . .31) to give . re./ ( x .\'2 )'r .e .311 A single equation can be developedby substitutingEqs.t2 16." X2 .. r r yrV 2 r : ) ) It + " t { ..f ' l. . f( r i . (16.
2 5 . o f t h es y n t a x f i n e o 2 i s o zi = Lt\re(p2 lx.8l ss + .4. 1 :)6 0 T(2. linear. 1 6l. a n dc u b i c . nearesf . y. xi. thesefunctionsoperFor example.4.5. yi. 6A 51 .rr gives Solution. and method: the desiredmethod.6 MULTIDIMENSIONAL INTERPOLATION EXAMPLE .\ .matrices containing the coordinatesof the points at which the values in the matrix z are given.8.I 6.6as as >> X= >> y= >> >> zlnl 2 9).1) 5l . u. zo : 6r.2). is For example. that is.2113  6.u 29 l6 5 .nethod argument omitted.. 'mechod' ) where x and y .2 4).2.Note that the methodsareidenticalto thoseusedby in*'erpi.the defaultis linearinterpolation. erp2(x.. spline.9 4o 8 .and threedimensional tion: interp2 and interp3.25and1'.6 . + . Supposeyou have measured temperatures a number of coordiat nateson the surfaceof a rectangular heatedplate: T ( 2 .6 16 Bilineorlnterooloiion 381 Problem Stotement. 2 5 .32) these into : "/ ( ) .6.interp2 can be usedto make the sameevaluation in Example 16. _ { .2 Multidimensionol Interpolotion in MATLAB piecewiseinterpolaMATLAB has two builrin functionsfor two. u.8) 5r .a simplerepresentation ate in a similar fashionto interpl (Section 16. As you might expectfrom their names.2r43 .2594.55 701. Usebilinear to the at.6 + 92 t6 s}s24Rl 5.. z. Substituting values Eq. if the./8 .5.6):10 interpolation estimate temperature : 5.y..5 T(9.. 6 ) 5 .5. zi : d matrix containingthe resultsof the interpolationas evaluated at the points in the matricesxr and yr. j ) .25.6): 55 : T(:9.(16. As with interpl.
warm.As a result. The temperaturegradient is important in its own right becauseit can be usedin coniunction with Fourier's law to determinethe heatflux acrossthe thermocline: J:DpC. by Thermal stratification has great significancefor environmentalengineersand scientists studying such systems.. dT az (r6."C Temperoture depthduringsummer Plotte versus for Loke. l t i s a l s o t h e p o i n t a t w h i c ht h e absolutevalue of the first derivative or gradient is a maximum. 16.t 27.8 4. 2. .3 2.01 cm2/s.2 ilt 0 228 . Lakes in the temperatezone can becomethermally stratified duringthe summer.In particular. p : density (31 g/cm3). buoyantwater near the surfaceoverliescolder. H E A T R A N S F E R Bockground.9 228 9 I 2A6 137 t3I 1 83 11. The location of the thermoclinecan be defined as the inflection point of the temperatur€d e p t hc u r v e . C)]. In this case study.6 T. s)].3 22.the thermoclinegreatly diminishesmixing between the two layers. Such stratification effectively divides the lake vertically into two layers: the epilitnnion and the hypolimnion.t h a t i s .33) where "f : heat flux [call(cm' . I6 FIGURE .382 AND PIECEWISE INTERPOLATION SPLINES .7 22. natural cubic splines are employed to determinethe thermocline gradientfor PlatteLake. separated a plane called the thermocline. Michigan (Table 16. decompositionof organic matter can lead to severedepletionof oxygen in the isolated bottom waters. and C: specific heat[: I call(g . Michigon.The latter is also depth and temperature usedto determinethe heatflux for the casewherea = 0.11. cv: an eddy diffusion coefficient (cm'ls).9 tl.3). t h e p o i n t a r w h i c h d 2 T / d z 2 : 0 . denserbottom water.As depictedin Fig. r ('c) Epilimnion Thermocline Hypolimnion TABTE16.I I Temperoiure depth versus for during summer Plotte Michigon Loke.
I . r " : i! : ] .2 CASE STUDY 383 " . iol c i:oetficier..r) ..7...lrtl:'.the builtin MATLAB it spl ine function doesnot meetour needs. r a .'dlii lrl  r r a r . However.27)for the secondorder (c). l. . i)). As just described. i) cr{:L)) . x.ri e. xr . / i i).y.1 .r . the varues oi the ::. * (2 * c(:) = c1i  1)) (continued) .th Le:: i i. : b r e s .'. ?solve i ll.i r:e. .: i p l i n e 1r . / l.. * 1lrili + i') v))./ 3 / lilx. Notice how coefficients FfGURE r6.1t(r. . r l spi. lc .c .J'ialt:es i..j e J . ' ar i . 16. / \ . n d d r:celfi.f \.12. l t s .. .12 MJile fo deiermineintermediole voluesond de.1: ri aiirr iiat ir II Il'  anr. r s c e n c r r : r r ') . i 1.{){ r:: iirsi ::=i:. . :c o n t i n u e d .yector The vecLcf xx. .i c " L y .irrr. s.] 6.=: r. want to usenaturalsplineend conditions performthis we to analysis.i j ndepenLlenL ricpenrlelt . l n .ttf ie : rl . 1 . t. Sucha codeis shownin Fig. i 1. I 2 * r l r{ x .xril: : a)n la. (16.1 ire xx) uses . ' a: : i a h . . il i).. hlr:.y. it is not difficult to develop our own Mfile to implementa natural spline and retum the derivatives.a l ues der. L) r x.we setup and solveEq. end x n o t s t : . . b .) f r f r d ua t t r:s in j.ly rrr:st be same L. .i.r.t € f ilo l. e s tf cle!)€:n. fci(j.r!{th' ) . Further. r j i f l . l \ .. y .fEL I') Lal h. . .':'.vaLiv:r ii'e: a: ai ar.r e n t : . e n d '. . +lj t.h(x. t : . r b i c rtnderlving r :pLtne furr:Lion l. r L 1 ( . After somepreliminaryerror trapping.r r .hi.r.ts : " S O l .. .Unfortunately.Note fhot fhe oif f with {unclion employedfor errortroppingis describedin Seclion 19.'(x. i.:i+njs (r lr + l.the sp1 ine functiondoesnot returnthe first and secondderivatives we require for our analysis."ivofives o noiurolspline. . Solution.because usesnotaknotend conditions. slle.
xi)3 We can also determine the lirst and second derivatives by differentiating this equation twice to give '(x) : bi * ci(x . h and fd. 0. ?i l inqnaeo(z(1\. = 5 1i I ) + . * d y i . r=122.1:m %perforn interpolaLions at desired values .t) 27. d2y2*c (ii ) +5*d (ii) * (z. br eak end end end FIGURE t6.Splinelnrerp(x. splineTnterp. *(xi x(ri))^3.t2 lContinued) we use two subfunctions.7 IB. i ) ) ^ 2 .x { . a..h 1 (x. a. dyy.(ii xi >= x(ri) x + i) + 0. to compute the required finite differences.x (ir ) ) ^2+o( ii) .6 13.3 4. A loop is thenemployed t0 generate other coefficients(a. end enC frrrlctfon hti = 11*. enC function lyyy. these equations can then be implemented in another subfunctign.y(j)) fcld .ix (ii ) ) .2(i)l n.dy{r). n.1 13.x. c.f"{x1 :2ci + 6di. i ) * ( x l .Once Eq.zi .000001 yyya (1i) +b ( ii) * (xix (ri) ) +c (ii) * (x!. the At this point.12. b.x ( i i ) ) + I * r l ( i i ) * ( x i .x.9 17..3 22.9 9.8 22.384 SPLINES AND PIECEWISE INTERPOLATION coniinued I . Here is a script file that usesthe naLspl ine function to generatethe splineandcreats plots of the results: z = i0 2. lyt'(r). . 1) . d2yl gp] inelnterp f nr i i 1 .r(i) x(j)).21.00U001 & xr <_. we have all we need to generate intermediate values with the cubic equation: f (x) : ai * bt(x . we solve for the c's with back division. b. c.8 22. i) hl'r = x(r + i) .rr) + ct(x * x)2 * di@ .) * 3diQ .l 17. 16. d.7 I1 . xr ) if . .)z .27) is set up.8 20.x(i). . x . d.(y(i) r' (.d. 1 c ( i . . for end iun l n i. to determinethe values and the derivativesat the desiredintermediate values.C'J fdri. and d). (16.L1. xx(:.)). b.xi) f As in Fig. .
(T..TT..7 CASE STUDY " continued natsptine \2.5 m.t .13.i.I6.Pioi ' l.' l " rcvc!s /. \6.The result is that the thermocline is located at 11.s depth (ml generoted with of {b} the cubic sprineprogro. g'odient. FIGURE6.zzt c 1 z ') . We can use root \oeation (zero second denrative) or optimization methods (minimum fust denvative) to refine this estimate.5 0. the thermoc\ine appearsto be \ocated at a depth of about 11.1. ond (c} secondderivotive uers. zz) I ).'reverse').T.!rlot (d't2.5 .ll'ciaca'.. zz) As in Fig. 10 \ t5 F F_ I 15 rI I I I I t5 rI I L 20 I 20 zv 25 25 25 30L 10 20 1n 30 1 2 30 0. zzJ .pfor(ai.. ieqrei.The rhermocline islocoted ot the in[lecrion point o[ ihe temperoruredeplh curve.9r!v / .' a'...61'C/m.35 m where the gradientis .grid ..n. ( a )T \bl dr/dz (cl dzTtdz2 _L______ 10 10 .I3 T Plots {o) temperoture. z.
the point at which d2Tldzz:0. 16. In flux from the surface to the bottom layer can be computed both cases. cm .9 I Based on these temperatures.33): J: 8 6 ' 4 o :0 s ' 9 ..6 natu cate t6. such.However.846 2t 703 r6. d r J data with (a) naturalend conditionsand (b) notaknot end az (a) F ditio (b) F slopt tiatir 16. Here are some valuesthathave beengenerated intervalsof 0.8 and Thes evalr .01 cal/ (s . 16. "C) compute the tlux acrossthis interface. The depth of the thermoclinecan be defined as the inflection point of the temperaturedepth curvethat Fit this datawith a (a) cubic splinewith notaknot conend is. m Temperoture. alsois anexample numerical of differAs how numerical approaches differentareas beused from can entiation. 1 7 6 1 5 4 7 t 45 887 96 500 47 448 19400 06 07 08 0.0rt*' * r4 .9)2*0.4 x Depih.* g. In ea whic f 16. n 2ca l 0.4 Develop a plot of a cubic sptine fit of the following .it illustrates in tandem problemsolving.19. At this depth.If k:0.3 The following is the builtin humps function that MATLAB uses to demonstrate some of its numerical clpabilities: f (x\ xl2 f{x1 I 5 25 7 5 I Fit this data with (a) a cubic spline with natural end conditions.'C 0 05 7A 7A I 55 t5 22 2 25 13 l0 3 l0 t2 r ftt) x 02 03 4. 16. tank can be idealized as the f $) ]t 692 12382 t7.t3 'a * r:"1^* (.r = 0 at to l: Fir tl ditio tions case 16.K .r range.2 A reactol is thermaily stratified as in the tbllowing table: (x0.^l=.the heat ditions and (b) piecewisecubic Hermite interpolation..3)2*0.: . I over the rangefrom.We will be describing topicof numerical for the differentiation in detailin Chap. (b) a cubic spline with notaknotend conditions.t6rI) * .create a plot comparing the fit with the exact with Fourier's law: humpsfunction.4 05 O 0l 5 .(16.04 0 The humpsfunction exhibitsboth flat and steepregions ovet a relatively shon.l Given the data to Use a clampedcubic spline tit with zero end derivatives determine thermocline the depth.386 AND PIECEWISE SPTINES INTERPOLATION continued with The gradientcan be usedto computethe heat flux acrossthe thermocline Eq..000 two zones separatedby a strong temperaturegradient or thennocliue.5 depi x PROBTEMS 16.01 (x0.L cmr m/ l0ocm d s \ cond cubi( x f (x) x f (x't The foregoinganalysis demonstrates splineinterpolation be usedfor engihow can and it neering scientificproblemsolving.and (c) piecewisecubic Hermite interpolation.7 orde 15.
colnpareyour plot with the following equation which usedto generate dala: was the I"{ . the 16.Note that the true value is 0. 16.Display the resultsgraphicallyanduseeachapproach estilnate to o(27).and (c) piecewisecubic hermite interpolation.(b) Repeat(a). develop a plot using (c) piecewise In cubic Hermiteinterpolation. cubic splinewith derivative end conditionsequalto the exactvaluescalculatedwith differentiation.For example.069x .12 Use MAILAB to generateeight points lrom the function / (/) : stn.and (c) a spline.5683.f(x) 0. : approximatioll.986 mg/L. Fit this data using (a) cubic spline with (b) notaknot end conditions.mg/L 1462) IlB43 IBZ0 B4lB 73A5 64)3 Use MATLAB to fit the data with (a) piecewise linear interpolation. Presentyour resultsgraphically. (c) piecewisecubic Hermite interpolation.t from r : 0 to 2n. case. (b) a linear spline. Note that the exactresultis 7.5.at "t : 1.[n each create plot comparing the fit with the step function. 16.556 24 A 5202 26 A 47OB Ineach case. 16. are olien compiled in standald .ll Runge's functionis writtenas I " *25rt Generatefive equidistantlyspacedvalues of this function over the interval: [. therelbre. and These functionsare usually not amenable straightforward to evaluation and.3.5 following data is sarnpledfrom the step function The depictedFig.l4.10 (a) Use MATLAB to fit a cubic splineto the followins data: 16.o I Fitthis datawith a (a) cubic spline with notaknotend con(b) ditions.true) for each.l: + r135679 1000 x v 0 2A 2 2A 4 12 7 7 l0 6 l2 6 Determinethe value of 1.PROBLEMS conditions.912 9.7 The fbllowing data was generated with the liftlr: polynomial:. 387 . (a) using an interpolating (b) using cubic splines.npute cubic spline 11twith a natural conditions. Createa plot comparing the fit with the tunction.)24 (a) this data with a cubic splinewith notaknotend conFit ditions. l6. (b) (a) Repeat but useclampedend conditionswherethe end slopes set at the exact values as determined by differenare liating function. cubic spline with zeroslope clampedend condiand trons.Test your code by using it to dupliend ExampleI 6. Fit this data with (a) a fourthorder polynomial. cate 16.14 3.8 Besselfunctions often arise in advancedengineering scientificanalysessuch as the study of electric fields.'C 0 B t6 24 32 40 o. 16. and (c) a cubic spline.(b) a fifthorder polynomial. addition.456i 27.9125xrorder * 15.9 The following data defines the sealevelconcentration of dissolved oxygen for fresh water as a function of tempelature: 200 T. /(r) 2 172 4 22O 5 430 4. Developplots of eachfit aswell asplots of the absolute error (8.r /(r) /(x) 0 0 600 0 4060l lB ro0 0 82436 800 0 t99r5 2aa I 00000 r000 009r58 0 5 8t 5 400 "Ir(J) 0 73576 2 0 5767 22 0. { ) : x e r!r t')rr Estimate polynomialand Jl2. mathematical tables. a 16. but with zero first derivativesat the end knots.411. 1].I : in t /(r) 0 0 02 0lrt 02 0.l).018515 0.6 Developan Mfile to cor.
. you were introduced to differential and integral calculus.'#i$k*tffi#*ri lntegrotion o nd Diff.. Consequently.Thus.just as a clerivative can be thoughtof as a slope. for functions lying above the abscissa. integrationinvolvessumminginstantaneous information to give a total result over an interval. PTs. integrationcan be usedto determine the distance lraveled: v(/) : fr' dr u(t) As in Fig.lb. Infegrationis the inverse of differentiation.Among other 389 .1n..: '.Juqt as differentiation usesdifferences quantify an into stantaneous process. l.'i:'. Because oflhe close relationshipbetweendifferentiation and integmtion.the derivuriverepresents rate o[ changeof a clependent the variable willr respect an independent to variable. in: as d u(l): y(l) dt' As in Fig. Mathematically. we have opted to devorethis part of lhe book to both processes. For example.1 ovERvtEw In high school or during your first year of college.differentiation an provides means dea to termineils velocity.the integral can be visualized as the areaunder the curve of u(r) from 0 to r.the derivativecan be visualizedas the slopeof a function. PT5. t 'ii.1 I d b .*"'!.There you learnedtechniquesto obtain analytical er exact derivatives and integrals. we are given a functionXt) that if specifies object'spositionas a functionof time. iIwe are providedwith velocity as a function of time.an integralcan be envisagedas lsummation.rentiotion 5.
we also include a discussionof numerical to be . integrationrepresents more highly developedareaof numericalmethods. Simpson's in widely usedNewtonCotes wherethe data rule. Second.2 PARTORGANIZATION for Chapter 17 is devoted to the most common approaches numerical integrationthe function Theserelationships basedon replacinga complicated are NewtonCotes formulas. in sense cover it as the last topic prior to describingdifferentialequations Part Six. tabulateddata with a simple polynomial that is easy to integrate.I (o) ond The confrost behr.Finally. part because is much in a less sensitiveto roundoff errors.Three of the most or formulasarediscussed detail: the trapezoidalrule. andSimpson's318 integratedis evenly spaced.In addition.this will provide the opporlunityto highlight their similaritiesand differences ln to a numericalperspective. althoughnumericaldifferentiationis not as widely employed.een differentiotion {b) integrotion from things. in it you to the basicsof numericaldifferenltiation Chap. addition. Hence.4.it makes it does have great significancefor the solution of differentialequations.390 PART INTEGRATION 5 AND DIFFERENTIATION 0 ) 400 200 0 tal 0 FIGURE PT5. Although differentiationis taughtbeforeintegrationin calculus.we reversetheir order First.the materialwill haverelevance the next paft of the book where we will cover differentialequations. All theseformulasare designed cases for 1l3 rule. we havealreadyintroduced in the following clrapters. to 5. do this for several We reasons.
2 PART 39r integrationof unequally spaceddata. addition.where tlre function valuesat the ends of the limits of integrationare known. At the end of Chap. All the above materialrelatesto closedintegratiott. The formulationscoveredin Chap. and . 17. In are ln Chap. Although they are not commonly usedfor definite integration. This is a very important topic becausemany realworld applications deal with datathat is in this forrn. equations Computeralgorithms providedfor both of thesemethods.fferentiation suppleto ment the introductorymaterialfrom Chap. wherethe integrationlimits extendbeyondthe rangeof the known data. The effect of errorson both numericaldiff'erentiation intesrationis also discussed. 17 can be employedto analyzeboth tabulated data and equationsChapter18 dealswith two techniques areexpressly . and the differentiation of unequally spaceddata. we presentadditionalintbrn.adaptiveintegrationis discussed. we presentopen integrationfonnulas. finitedffirence formulas. Richardson extrapokttion.4.ORGANIZATION 5.ration numeric:al on di. Topicsincludehighaccuracl. that designed integrate to and functions:Rombergintegrationand Gaassquadrature. 19.open integrationformulas here because are presented they are utilized in the solution of ordinary differential equations in Part Six.
Knowing how to implementthe following singleapplicationNewtonCotes formulas: Trapezoidal rule 1/3 rule Simpson's Simpson's 3/8 rule Knowing how to implementthe following compositeNewtonCotes formulas: Trapezoidal rule 113 rule Simpson's Recognizingthat evensegmentoddpoint formulaslike Simpson's1/3 rule achievehigher than expected accuracy.'.14 . . .0. Specific objectivesand topicscoveredare .f ') : uu\ . the ' ' .) V .i \''l n rr rrr . ' . Knowing how to usethe trapezoidal rule to integrateunequallyspaced data. YOU'VE GOT A PROBTEM h ftf l\ ecall that the velocity of a freefalling bungeejumper as a function of time can be computed as ( . Understanding differencebetweenopen and closedintegrationformulas..:' .' : rotion NumericolInteg Form los u CHAPTER OBJECTIVES The primary objectiveof this chapteris to introduceyou to numericalintegration. Recognizingthat NewtonCotes integrationformulas arebasedon the strategy of replacinga complicatedfunction or tabulated datawith a polynomial that is easy to integrate.unn 8a.
or summation. definiteinteglation is represented by t: f a^ l. 1i)n t o E q . (17.rs.5) corresponds the area under the by to curve of l (x) betweenr : a andb.l*. I7. I.to unite. . As suggested the dictionarydefinition. evaluated betweenthe limits x : a to .)0.Furlher.into as a whole." Mathemafically.r) (r 7 .l Whof ls Integrotion? According to the dictionarydefinition. In fact. numerical integration methods availirble obtainsolutions. .l.1 Thr. parts.5) is the roral by value.it as would also be possibleto integrate discretedatato determine distance.o {. This is an archaicterrn that originally meantthe construction a square of having the sameareaas somecurvilinear figure. integrationprovidesthe meansto deterrrrine distance the fiom the velocity. the integral expressed Eq.the "meaning"of Eq."'n( . (17.Calculus can be usedto solveEq. are to Chapters and l7 l8 will introduce vou to someof thesemethods.to (1j.r : b. suppose that there was some way to measure jumper's velocityat varioustimesduringthe lall.I INTRODUCTION BACKGROUND AND 393 Supposethat we would like to know the vertical distancez the jumper has fallen after a certaintime r. to integratemeans"to bring together. Today.E')l (t7.ln this situation. This distance be evaluated integration: can by fl :(r): / t'(ndr .8""n( .' ( I 7. 5 ) which standsfor the integralof the function f (x) with respectto the independent variable . both these the the In instances. .4) Although a closedform solutioncan be developed this case.I Z. 1 1 7 ..r. ( 1 7 .r. Thesevelocities the alongwith their associated times could be assembled a table of discretevalues.r. 2 ) i v e s g z(t): .1 represents graphicalmanifestation the concept. the symbol / is actually a stylizedcapital S that is intendedto signify the closeconnection betweenintegration and summation.to indicatethe total amount.For functions lying a of above the x axis.I INTRODUCTION AND BACKGROUND 17. Numerical integrationis sometimes referredto as quadrature.2) g S u b s t i t u t i nE q ...3)for f '"[.thereare other funcfor tions that cannot be integratedanalytically. Figure 17. (17. the term quadratureis generallytaken to be synonymouswith numerical definite integratior.of /("r) dx overthe rangex : a to b.
5r t17.as specified by . you might also be interested computingthe mean or average the continuous in of function : / (x) for the interval fron a to b. 17. in In contrast. (i3." (11.{ FIGURE7.Many specificexamplesof suchapplicationscould be given in all fields of engineering A and science. number of examples reIate directly to the idea of the integralas the areaunder a curve. Recall that the meanof discrete n discrete of datapointscan be calculated tEq.2depicts few a caseswhere integrationis usedtbr this purpose. by 14s2n. determination the meanof discrete The points of is depicted Fig.3a.For example.a common applicationis to determine meanof a continuous the function.r is a continuousfunction of an independent variable as .1) This formula has hundredsof engineeringand scientific applications.2 Integrotionin Engineering ond Science Integrationhas so many engineering and scientific applications that you were required to take integral calculusin your first year at college. l'! f t.utax DA .I I representotion integro l ( x ) b e t w e e n h e l i m i t s : Grophicol of the o1 t r equivo lo theoreounder curve.suppose that .2)1. lhe enl is t t l o D T h ei n t e g r o l FIG Exo nee kno nonl 17.6) can be appliedto detenninethe mean of the discretereadings.. is it used to calculatethe center of gravity of irregular objectsin mechanicaland civil engineeringand to determinethe rootmeansquare currentin electricalengineering. (17.r.For this case.l Mean: i. Just as Eq. . Integrationis usedfor this purpose. 17.thereare an infinite numberof valuesbetween and a b.3b. Other common applications relateto the analogybetweenintegrationand summation. depictedinFig.1.394 NUMERICAL INTEGRATION FORMULAS I . Figure 17.6) n where yiare individual measurements. For example.
I INTRODUCTION BACKGROUND AND 395 i _l l (b) (c) I7. A surveyor might to need knowtheoreoof o fied bounded o meonderinq ond roods.2 TIGURE (o) of how integrotion used evoluole is to oreosin engineering scientific Exomples cnd opplicotions.ei en nonuniform blowing wind ogo nsfthesideof c skyscroper.I Z. e e r m i g h te d i o d e t e r m i n e t h ein c e d J e t o o l eoo ne f .3 An illustrotion meon (o)discreie (b)continuous for of the ond dotc . 4 (a) r : "f(r) (b) FIGURE I7. A hvdroloqist miqht needto bv streom hryo {b) k n o w i h e c r o s s s e co iro n o fo r i v e ri c )A s t r u c t u r o l g i ' .
are They are basedon the strategyof replacingn complicated function or tabuiated datawitha polynomial that is easyto integrate: 7b 7b I:lf(r\dr=1f.396 INTEGRATION NUMERICAL FORMUTAS Integralsare also employedby engineers and scientists evaluate totalamount0r to the quantity ofa given physicalvariable. However. For the continuous wherec(x. For both thesecases. . which is referredto as an areal integral.addition.x . elementalvolumes A Vi: : Mass I. or of Mass : concrentration volume x where concentration has ur.sr a volume. known function andx. examand For ple. suppose of that concentration varies from location to location within the reactor. the total rate of energy transferacrossa plane where the flux (in caloriesper square per centimeter second) a lunctionof positionis givenby is ?? Flux:llnu*ae " "r. Thesearejust a few of the applications integrationthat you might taceregukuly of in the pursuit of your profession. total massof chemicalcontained a reactor girenasth the in is product of the concentration chemicaland the reactorvolume. Mass: III.ttt t . for integralsusing numericaltechniques described as next.For example.z ) d r d v d z Mirss: lllrtvtdV JJJ v which is referredto as a volume integrctL.However. anNea. y. the underlyingfunction is often unknownand definedonly by measurementdisat you must have the ability to obtain approximate values crete points. y.. t) isa case.on whererr is the numberof discrete volumes.In this case.it is necessary sumthe to productsof local concentrations and corresponding c. Notice the strong analogybetweensummation and integration.2 NEWTONCOTES FORMULAS The NewtonCotes.rits mass per volume.formulas the most common numerical integrationschemes. is typicallythe casein morerealistic as In examples..you will normally chooseto evaluatethem analytically.(. 17. and : are independent positionin Cartesian variables designating integrationcan be usedfor the samepurpose: coordinates.r\tl.The integralmay be evaluated over a line. it is often difficult or impossible whenthe functionis complicated. Similarexamples could be givenin otherfieldsof engineering science.. whereA : areo. When the functionsto be analyzedare simple.
r) : a polynomial of'the form a.4a.r) .tx" . For example.in Fig.9) wheren is the order of the polynomial. (.r):a0+alx +"'+ 397 (17. a firstorderpolynomial (a straightline) is usedas an approximation.For example.2 NEWTONCOTES where ll.f (tt) FIGURE .FORMULAS I 2.a parabolais employedfor the In samepurpose. 17..4 I7 (o) ine by The opproximction integrol thecreounder c stroight ond(b)o porobolo of on "f(.5 I7 three by oreo under stroightline segmenis The opproximotion integro fhe of on l(r) ..inFig. 17.5.three FIGURE . using a series polynomialsappliedpiecewise of The integralcan also be approximated of to the function or data over segments constantlength. Fig. 17.4b. rtr"I la.fr(.
17. Recall from geor etry that the formula for computingthe areaof a trapezoidis the heighttimestheaverage of the bases.r.verageheight EXAM . Geometrically. .6b).f (b) .eis the first of the NewtonCotes It closedintegration sponds the casewherethe polynomialin Eq.[(b) .6a).3 THETRAPEZOIDAT formulas.This chapteremphasizes closedforms.width x . closedlorns The Closedand openforms of tlre NewtonCotes are those where the data points at the beginning and end of the limits of integration ac known (Fig. formulas is briefly introducedin Section17.f (a) (.I .7. D0 s T h e r e s u l t f t h e i n t e g r a t i oin o l:(b kl) + .6 inlegrotion The difference behween closed ib)open ond formulos {o) polynomialscan are straightlinesegmerlts usedto approximate integral. l7 . onrcase.398 FORMULAS NUMERICAL INTEGRATION FIGURE I7. formulas al'eavailable.. (17.Higherorder the be utilized for the samepurpose.the trapezoidalrule is equivalentto approximatingthe area fu of g trapezoidunderthe straightline connectin I O1 andJ @) inFig. ")]d. con+ The trapezoidalrul. 17.7. open NewtonCotes RUIE 17.r r ) f' 2 (17.the conceptis the sarrie the trapezoidis on its side.10 which is calledthe trapezoidalrule. In but Therefog the integralestimatecan be represented as / .8)is firstorder: to t: l""lt^ 1 . However.The openfornrs have integrationlimits that extendbeyond rangc the the material m of the data (Fig.
otherwise. All the Newtoncotes closed lormulas can be expressed in the generalfbrmat of Eq' ( 17'13)' That is.14)indicates that if the tunction being integrated linear. Equation(:i. 7 I Grophicol depicrionthe of tropezoidol rule I : (b .f (o) + J @)112.lD ro numerically integrate l ( r) : 0.or l. t! I ul.the trapezoidal is rule wiil be exactbecause second the derivative of a straightline is zero. they difl'er only with respect to the formulationof the average height. that rnay be substantial (Fig.rr) x average height (r7. (17. (Ij. the average height is the averageof the function valuesat the end points.  7.r3) where.we obviouslycan incur .640533.someerror can occur. for functions with secondand higherorder derivatives(i. with curvaturc). l 7. Use Eq.17.1 Error of the TropezoidolRule when we employ the integral under a straightrine segmentto approximatethe integral undera curve.oo.l EXAMPLE S i n g l eA p p l i c o r i o no f r h e T r o p e z o i d o lR u l e ProblemStqiement.3.2 _t 25x _ 200x2* 615x3_ 900ra f 40015 from a : 0 to D : 0'8.g)..:.e.3 THETRAPEZOIDAL RULE 399 F I G U R E7 . Note that the exactvalue of the integralcan be determined analytically to be 1.11) wheref lies somewhere the intervalfrom a to in b. An esti_ mafe forthe local truncationenorof a "n singleapplicationof the trapezoidal rule is E. 'lZ.f"tE)tl. . fbr the trapezoidalrule.
2.8.t. 8 rule Grcphicol depiction theuseof o singleopplicotion tropezoidol to opproximote of of the iheinteqrol(. 2 E q .8 F I G U R EI 7 .To obtain this estimate. 8 ) : 0 . t I ) t o y i e l d 1:{0.8o which can be substituted Eq.2+25x2oa^r*675rr9OOr+*4OOr'trom.(60)(0.8)3 . T h e f u n c t i o nv a l u e sl ( 0 ) : 0 .5c/c. f ( 0 .eerror of st :89.640533 0. an whichconespondsh fronb a percentrelatil.00013) 0.000x3 The average valueof the secorrd derivative can be computed lEq. 8 .56  .t:0to0.17:8 . 0 . 2 3 2 c a nb e s u b s t i t u t e d i n b which represents effor of E. Therefoq hr actual situations./.8n.14)to yield into I E...7)l as '' '^'f /. N o t i c e t h a t t h e a r e a u n d e r t h e s t r a i g h t l i n e n e g l e c t s a s i g l nificant portion of the integrallying abovethe line.the function's twicc derivativeover the interval can be computedby differentiatingthe originalfunction to give t f" (x) : 400 + 4.050x . : 1.800:rr 8.80012 8. 1+ 0 .l T .10. a n d .\ _ r/:r * /1'* t+oo * 4.400 FORMULAS NUMERICAL INTEGRATION lnteqralestimate 0.1128: 1. of serord an approxirnate error estimateis required.10.050. (17.we would have no foreknowledge the true value.461133.r)A. .( l 7 . ol S o l u t i o n . 2 3 1 :0. (17. The reasonfor this largeerror is evident g r a p h i c ad e p i c t i o n i n F i g .: .
f + f ( F I G U R EI 7 ...2 The CompositeTrqpezoidolRule interval of rule is to divide the integration One way to improve the accuracy the trapezoidal (Fig..3.A discrepancy' secondderivative however.'t ) d x + . 9 trcoezoidcrle ru Comoosite .fitrmulas. Tlre resulting equationsare called compo.9 shows the generalfbrmat and nomenclature will use to characterize integrals..because the fact that tbr an interval of this size..Conthere are .rs and 1..). and which is of the sameorderof magrritude sign as the trueerror. apply the ntethodto eachsegment liom a to b into a numberof segments ernd can then be addedto yield the integralfbr the entire inThe areasof individual segments integration or terval. ' ' f . total integral can be repreas sentedas r : L'' (x)ru1.3 THETRAPEZOIDAT 401 doesexist.9). n : bc il..t . x2. ' +l .l segments equalwidth: of sequently. ratherthan exactby using E.. r r u . we denotethat the error is an by approxin..ls) the If a and b are designated . 17..RULE 17.rate using the notation E. Thus. la ba t7 (l7. 17. .rr. muhipleapplication..site. we Figure 17. r.the average of of is not necessarily accurate approximation l"(6). . respectively... . Therearen * I equallyspaced composite basepoints(ro.
11 . 1/ ( x : ] or.:. t of of t h a t E q .q.l8).widlh j1 2rt Avcragc hcight Because summationof the coefficientsof /(:r) in the numeratordividedby 2il is equal the According to of heightrepresents weightedaverage the function values. ./ r\ r\ ' . . . r / i) t:(ba) \.I '/ t .640533. grouplng terms: I : : l / ' ( x o+ 2 ) ) . .(17. E X A M P L E2 .T nl il I I(ri) l.. the average Eq.".. a to I . 2 1 )i s a n a p p r o x i m a t e e r r o r b e c a u s eh e a p p r o x i m a t e n a t u r eE q . Employ F.1 . usingEq. I(rn\* /trr) 22  t t  .ond derivativeat a point {.rl L nt .J (x.n' Note is Thus. r ..( 1 1. (. Use the twosegment trapezoidal t * f (x) : 0. ( 17.l tD L .' lD  .) = n f " and Eq. ( 17. .ro) and ..n" . locatedin segment This result be simplified by estimatingthe mean or averagevalue of the secondderivativefor theentire interval as t1 \ 1' J f ' l r \l \ i ' . 1 . can where /" (6i) t. " [ ( r r ) . ( 17.402 FORMULAS NUMERICAL INTEGRATION rule for eachintegralyields Substitutingthe trapezoidal .I ot' .21) to estimate error.) r 2 r r ( .8..the truncationerror will be quarlered.   {r7 re) i.:: tln' .: _. ( 1 7 .q..l'" (€. if the number of segments doubled.l5) to express (17. An error fbr the compositetrapezoidalrule can be obtainedby summingtheindividto ual errorsfor eachsegment give E. or.)  I J I form of Eq.1 tll' il ) f /.re..).f (x.2 * 25x 200x2 675r. n .17) in the general nl f ( r n ) r ..19) can be rewritten as ThereforeL. 2 I C o m p o s i t eA p p l i c o t i o no f t h e T r o p e z o i d o lR u l e of rule to estimate integral the Problem Stotement. the interior points are given twice the weight of the two end points/(. 2 0 ) .3 900x4 * 400x5 value the from a : 0 to D : 0.  1.l3): F. .Recallthat theexact of the intesralis 1.
9 t6 .97c 0 E . Forn : 2(h :0.l.alsonoticethat the rateof decrease gradual. 2 + 2 ( 2 . 4 s+ 0 .0889 008 06BB 3695 484B 5399 5703 5B87 6008 609 I 6t50 349 t6.I2. 8 .2 + 25.r'l :0. are surnmarized Table l7. 4 ): 2 .3 32 2. 5 1 1 7 3 0. 6 4 0 5 3 3 .5 95 6l 4.3 MATTAB Mfile: trap A simple algorithm to implement the compositetrapezoidalrule can be written as in is into the Mfile along with the limits of Fig. loop is then employedto generate integral A the f o l l o w i n gE q .2 1:0.6 0 ) : 9 ..3 RULE THETRAPEZOIDAL 403 Solution.0688 €t :34. However.1 the of .r 3 3 3 0l143 0t 0. ("/"1 2 3 4 5 6 7 B I t0 0. However. 2 3 2 6) 4 :1. where 60 is the averagesecondderivativedetermined rl o d The resultsof the previousexample.l 0. 6 4 n t2(2\t previouslyin Example 17.4 1. v e.21)).2 0 t6 0 . The function to be integrated passed integrationand the numberof segments.we will tegralas the segments increased." : .4 a 2667 0.8 f ( 0 . 2 3 2 0 .  7. 8 ): 0 . : 1 . 0 6 8:8 .4): /(0) : 0.1 . 17. 6 4 0 5 3 3 1 .along with threethrough tensegment applications of the trapezoidal rule..r : O t o 0 . TABLE 7. f ( 0 . 4 5 6 . I hopezoidol ruleto Resulis thecomposite for estimote integrol /(.belbreinvestigating are first discusshow MATLAB can be usedto implementthe trapezoidal rule.( . This is because error is inverselyrelatedto the square r [Eq. 1 8 ) .\the of 200t2 675x3 9O0r+ 4OO. ( 1 7 .gr [ .rs from + + . quartersthe error.3. In subsequent sectionswe develop doubling the number of segments fbrmulasthat iue more accurate higherorder and that convergemorequickly on the true intheseformulas. Notice how the error decreases in is as the numberof segments increases.Therefore.T h ee x o c t o l u ei s 1 .1. (1.
1kg. h = (f .end r f narqin4 risempLylnt. I O E 'he Mf le o imp enrcnl conpo:iie rope. 0. 9 4 8 0 5 .n.ments (defaulL = 100) pl . b = inteqration limits ? n .(17. : 68.{:' ) . B 1* A . >> v=@(t) sqrt (9.error('at least 3 input arguments required')../ooo 'J e.vararg:in{:}).p2. end s .ar" )1>5 .' tt ap (v. . I = (b .pi. fori=1. the assume following parameter values:g : 9.6oo2o :.end x = a. For this exarnple.81 mlsz.25i68.number of seg. 3 1 * 6 8 .s + func(b.25 kg/m.varargin{:}). ): % composite trapezoidal rule % input: func = name of function ? Lo be integrated Z a. The function to be integratedcan be developedas an Mfile or with an anonymous function.b.te tt:apezoidal rule quadraLure 'Z I .varargin) % trap: composi.vdrarg i .error('upper bound must be greater than lower.end if '(b>a). .25) *tanh(sqrt (9. and m ca :0.1)*t) C ( t ) s q r i ( 9 . 2 5 ) * t a n h ( s q r t ( 9 . 3.p2.a. 5 ) o. An applicationof the Mfile can be developedto determinethe distancefallenby the freefalling bungeejurnper in the first 3 s by evaluatingthe integralof Eq.b.L/0. 2 5 / 6 8 . . let's evaluatethe integralwith a crudefivesegment approximation: formaL long >. Sfunc (q.additional parameters used by func ? Z output: I=integralestimate % if nargin<3. Note that the exact value of the integralcan be computedwith Eq.a)/n. 1 . s = s + 2*func(x.. F I G U RI 7 .a) * s/(2*n).404 NUMERICAL INTEGRATION FORMULAS function I = trap(func.. 't ).3).81*68.4) a s4 1 . . 1 ) * t ) First.n.a.n100..trap(f unc.). ( 17..81*A.n1 X = X + h.
rz) .r .f (a) and f (b).: I:.xo)1. t h e t h r e e f f en points can be connected with a parabola(Fig.l1b).'l + n/l^r .rla. I FIGURE7.67o. O nC O ' n e c l 1(/.x2) + (xr  x e ) ( x 1 : l(xr) (.IZ. ( 17. F o r e x a m p l e .tih e r e i s a n e x t r a p o i n t m i d w a y b e t w e ( a ) a n d / ( b ) . there are two points equally lf spacedbetween.000segments: > > t .4 SIMPSON'S RUTES 405 As would be expected.11a). 17. 3 . The formulasthat resultfrom takingthe integrals underthese polynonrialsare called Sintpson's rules.:rr) (x2 .xo) ("t .. 1 0 t 1 0 0 ) 4!..O . the tbur points can be connected with a thirdorderpolynomial (Fig.Y t ) 4 rr'.r2 .) f(x) .948049999r1528 which is very closeto the true value.we can use a very fine approximationbasedon 10.8)is secondto order: (xxr)(.b )G r c p h i c o e p i c f i oo f S i m p s o n ' s6 r u l e l t c o n s i sos t o k i n gh e th p d n : 3/ if t ^rA^. 1 . this resulthasa relativelyhigh true enor of 18.4 SIMPSON'S RULES Aside from applyinq trapezoidal rule with finer segmentation.rz) (x .4. / (rtt) (x1y xy)(xs . r a p ( r r . 17.1 Simpson'sl/3 Rule Simpson'sI /3 rule coresponds the casewherethe polynomialin Eq. l7. the anotherway to obtain a more accurateestimateof an integral is to use higherorderpolynomials to connectthe p o i n t s . 17.I I I ( o )G r o p h l c o l e p i c l i oo f S i m p s o n1 / 3 r u i e l:t c o n s i sos t o k i n gh eo r e ou n d eo p c r o b o l o d n s lf t r c o n n e c i i n g r e e o i n t s(.r. ^^. obtaina more To accurate result.xe)(.^ri  ' g 'I O J r p O t n ' 5 .
Simpson. f ( 0 .it yields exact resultsfor cubic porynomialseven thoughit is derivedfrom a parabora! EXAMPLE 7.r2.l3): I(ba wherea : where.2 0.232 = 1. Thus. consequently. middlepoi to the int is weighted twoby .8 0. It can be shown that a single_segment ication applicati of Si npson'sl/3 rulehasa trunca.according Eq. x ' ) : 0 .3 1 S i n g l eA p p l i c o r i o no f S i m p s o n .^t1]_ alsobe expressed usingthe formatof Eq. where f lies somewhere the interval from in a to b.2730667 : .y)l (n :?::.367467 €t:16.becauseh:(ba)12: E. the etror is proportional to the fourth derivative.232 1 :0. 2 * 2 5 " u .24) to esrimare rhe enor. Simpson's1/3 ruleismore accuratethan the trapezoidal rule' However. (17. \ t l' or. In other words.' :  that.ut"s 2990 "\ f t 1 . o7.l (:to) 4f (r) * .23) ro inregrate f ( .comparisonwith Eq.sl/3 rule is thirdorder accurate even though it is basedon only threepoints./z (!srems fromtherfactthat//r lis u t v l ( j e o by J lin Eq."otice 'fld.b:.f (xzl f) xo.which is glven rs si by 90 h 5 r/ ' f r t '. Use Eq.l 0 b : 0'8' Employ Es. r r w 4 v r r r 1 4 r t s atvided y D 3 n l2t. : 1.f(x) + J(. s / 3 R u l e l ProblemSiotement. 4 ): 2 . t b/ . (17. r7. n tion enor of I E . Recall that theexacr !9 inregratis 1.640533.:!11 thirds and rhe rwo end points by one_sixih. Si Simpson's rule l/3 can a ) / 2 .T'e + . (17.8) : 0.36746j 0. &s and The result of the intesration is lt  / : r 1 [/(xo) 4. 4 5 6 /(0.640533 1.22).2+4(2. ::.2 0 0 1 2* 6 i 5 x 3 _ 9 0 0 x a *400rs frorn..forthiscase.456)+0.67o E.n : 2 l h : J'(0) : 0. (17. respectively.406 NUMERTCAL tNIEGRATON FORMULAS wherea andb arc designated :16 12. s n I/3rule.] that )indicates it is nore accuratethan expected'Rather than being proportionalto the third denvative.' '  ( b . S o l u t i o n .4).23). T h i s e q u a t i o n i s k n o wa s S i n t p s o n .rr : the point midway between a andb.
is ."' .1. Hower. 4 S I M P S O N 'R U L E S S 407 which is approximatelyfive times more accurate than for a singleapplicationof the trapezoiclal as rule (Example17. : .4.. the result matchesexactly.^n'< 1/3 rule.the error is approximate(E..The relotive rveights ore depictedcbove the funclion vclues Not^thoi the methodcon be employedonly if the numberof segmenls even.) because averagefourth derivativeis generally the not an exactestimate /'+rt{ ). of because casedealswith a fiftholderpolythis nomial.I 2 . Simpson'srule can be improvedby dividing the integraJust as with the trapezoidal tion interval into a number of segments equal width (Fig.l t .rU. ' + ttx+ o. 1880 : 0.1710667 where 2400 is the averagefourth derivativelbr the interval.'. ( 17.2s) F I G U R E7 . 1 2 1 fnrrnn<lfo lim^.I ).'er. I7 . .2 The Composite Simpson/s l/3 Rule rule.'f{*)ar+f l. The approximate errorcan be estimated oRs E" .12).The total integralcan of be represented as r: [^. 17.2 4 0 0 ) .As was the casein Example 17. 1. (x) .
900x4* 400x5 from a : 0 to b : 0.x) : 0.) Notice that.456) 0... the coefficients"4" and "2" in F.8. 4 6 4 . (11.'+2h ( or.1.ro)+4I J:(btt) i. (17.04Vo Et : 1 . l/3 As seempeculialat first glance. In addition." .q.5 fQi)+2 L 3n j:2.3. where f E X A M P L E2 .1) 2.464) 2(2.) J 6 +.' 1 5 ! I'o' ' \1j.. Recall that theexact integralis 1. z) * 4J'Q.(17. fashionas An error estimatefor the compositeSimpson'srule is obtainedin the same and tor the trapezoidalrule by summing the individual errors for the segnlents averaging the derivativeto vield t . tbr I Composite impson's/3 Rule S the of ProblemSiotement.26) from Simpson's rule. r) + l (r.". (11.456 : I : l (0.200x2 675. Employ Eq..l 1 .4. odd pointsrepresent rniddleterm tbreach applicationand the hencecarry the weight of four from Eq. 17.12.623467 I2 €t : I.2) 1.they follow natul'ally in the illustrated Fig. 4 I (l) rh  l80na '' . 6 2 3 4 6 : 0 . 6:)3 . groupingtermsand usingE q . However.n :4(h: 0. 1 . (11.10533. Solution..288 ( 0 .8) 0.an even numberof segments mustbe utilized might to implement the method.408 NUMERICAL INTEGRATION FORMUTAS SubstitutingSimpson'sI /3 rule fbr eachintegralyields I :2lt + I (xo) 66 (r.2 * 25r .The even points are commonto adjacent applications and henceare countedtwice.232 From Eq.z (). 6 4 0 5 3.6.2+ 4( 1.23).f(o') o.6 fG)+f(r. UseEq.5 ) : n I n2 l(. 1 7 . 17.21) is the averase tourthderivative the interval.26\ with n : 4 to estimate integral f (.f 0.12.288 3. 0 1 7 0 6 1 1 . : . as illustratedin Fig.3.26): / nrl : f \0.232 + + + : 1.27) to estinlatethe error..2): : .
f( x r .29) Because denor.it is Iimitedto cases wherethe valuesareequispaced. . because the large truncationerror associated of with this method. i L l ' . For instance. the two interior pointsare given weightsof threeeighths.However. 3/8 rule is somethe of the what more accurate thanthe 1/3 rule. / 'r' ' r { 64rJ0 (1. disand as cussedin Section 17.24). : . Simpson's rule hasan emorof 3/8 E. Thts may not be advisable.3). : ]/. / ( . : . .4 we usedSimpson'srule to integratethe function for four segments.3 Simpson's 378 Rule In a similar mannerto the derivation the trapezoidal of and Simpson'sl/3 rule. _ t2100) : (). whereas end pointsare the weightedwith oneeighth.4 SIMPSON'S The estimated error 1Eq.27'. x )+ . a fhirdorder Lagrangepolynomial can be fit to four points and integrated yield to f : 3h t3f(x) +.'f 6t/ 't'te t or.as mentionedpreviously. . However.the 3/8 rule has utility when the nunrberof segments odd. As in Example 17.4. (17. One option would be to usea compositeversion of the trapezoidalrule as was done in Example 11. is (o'8)5 E'.ThisequationisknownasSimpsons3I8rulebecause/rismultiplied by 3l8. Examis in ple 17. Suppose that you desiredan estimatefor five segments. Further. of 17.4..13): in i:\UUrT . oddsegmentevenpoint an formula known as Simpson's 3/8 rule can be usedin conjunctionwith the I /3 rule to permit evaluationo1'both evenand odd numbers equispaced segments.(17.' b .2.a)/3.28) Thus.Consequently.rr1)*]l(. f ( .torEq.rr) where/r:(b. ) * 3 / ( r r ) * 3 .nina. . is limited to situations it where thereare an evennumberof segments an odd numberof points. the compositeversionof Simpson's1/3 rule is considered superior to the trapezoidalrule fbr most applications. (11. Simpson'sl/3 rule is usuailytlre rnethod preference of because attains it thirdorder accuracywith three points ratherthan tlre tour points requiredtbr the 3/8 version.U17067 l t t 0 ( 4) + which is exact(aswas alsothe casefor Example17.fr)l o If(.4.f(. however.The 3/8 rule can alsobe closedintegration expressedthefbrmof Eq.An alternative would be to applv Simpson'sl/3 rule to the first two seqments Simoson's 3/8 nrle to the last .r : ) (11.a) 13: h .3.It is the third NewtonCotes formula. because : (l) .29)is largerthanfor Eq.RULES IZ.17.
(b) Useit in conjunction Simpson's ruleto integratethe with l/3 same function five segnlents.432j24 .r+ froma :0 to b :0.^l J : f (0.8 o p p l i c o l i o nw i t h o d d n u m b e r s f j n t e r v o l s .: t ) .20012 675x'r 900.wecouldobtain estimate thirdorder three an with accuracy across theentire interval.432724 3.487117) 0.4ro NUMERICAL INTEGRATION FORMULAS 0 # 0.5 S i m p s o n3s 8 R u l e '7 Problem Stqtement.6 3/8 rule 0. tbr Soluiron.2 1/3 rule 0.f (0.4 0. 1 3 1 jn lllusirotion Simpsons cnd 3/Brules beopplied tondem hondle of how 1/3 con 1o muriple (Fig.13).28): Eq.232 1 .4811'71 /(0.2 :3.In thisway.8):0. (c) A single application Simpson's rule requires equally of 3/8 four spaced points: /(0):0../(r) : 0.232 Segmentr (n) TABTEI . s o F I G U R E7 .(a) UseSimpson's ruleto integrare 3/8 l400x5 * . 5 1 9 7 0 l. EXAMPLE 12.2r 25x. + + 0.8.266j):t.2+ 3(t . ll .5333) Using (17.
48 1 .r'.32 0..2 NewtonCotes closedintegrotion formulos. .the trapezoidalrule and both of Simpson'srules are menbers of a family of integratingequationsknown as the NewtonCotes closedintegrationformulas. 288 .16)  .and sixpoint formulas have the sameorder error.7r.r:) 501(. . . 1 4 3 3 9+ 3 ( 3 .the five.r r )+ f ( . f ( .264154 g The total integralis computedby summingthe two results: : 1 : 0..t. . as was the casewith Simpson'sI l3 and 3/8 rules. ' 9 O 1 t r :' r ' t . 2 3 2 3 5 + :1. .HIGHERORDER NEWTON./{./ (0.l ) + 501(.3803237 0 4tl For the last threesegments.l/3 rule and leadsto the resultthat the evensegmentoddpoint preference.r 3 S m p s o n3 / 8 r u e ..:0. ) Formulo Truncolion Error ll /I2lhtl"tI) .2+ 4(.r : ) \p . r 8 r 9 2 9 ) 0 .A+1 Theintegral thefirst two segnrents obtained for is 1 usingSimpson's/3 rule: I :0.rr I 9 l (rx) Z5.1rt': 16..Stl. 3 2 1 1 .1 .r1r. 1 8 6 0 1 5 : 3.g. Notice that. 1 13l80)15 11)(6) f Q 4 5 ) l i r .l 1.232 f Q. stepsizeis The of givenbyh:(ba)ln.{r) 75l (xa) I 9 l (r5) 127 + + + + 5 / I 2. Poinls Nome Trcpezoidol rule S i m p s o r '1 3 ' r . along with their truncationeror estimates. 3/8 rule can be usedto obtain the 1:0. formulos presented theformotof Eq.181929 .f (l." . f ( r o ) + 4 .r.r'. \t) .COTES FORMULAS (b) Thedataneeded a fivesegment I ication(/z: 0.264754 1.2.f (0) : 0. 1 8 6 0 1+ 3 .645017 + 17.\. 1 0 ) + 3 2 1 ( x t )+ l 2 l ( x l + 3 2 ./(ro)+l(rr) n. This generalcharacteristic holds for the higherpoint fornrulas fonnulas(e. {l Z. r1'6 1 (r .u ) \b0) 7 .5 HIGHERORDER FORMUTAS NEWTON.COTES As noted previously. 7 4 3 3 9 3 l ( 0 .^ : . . 8 s 5 Bccle rr.a9dnt . and Boole's rule) are usually the methodsof IABLE 17.. 4 8 ): 3 . Some of the formulas are summarizedin Table 11.2 .143393 + :0.2969t9) t.I 3) The in ore the so thoi theweighting thedoto points estimote overoge to heightis opporent. ( r t ) + 7 J \ t 1 ) 1 6 182 90 (. .296919 : f ( 0 .3803237 1.16) is for app : .tt.)/{.le s \ 1 .
3was generated usingthesame polynomialemployed Example17.there are ntany situittionswherethis assurnption doesnot holdand we r. E X A M P L E2 .ril+f@r) lhz f (u) + f (r.) .r) : 0. In practice.f(. Accuracycan be improvedby usingthe composite version.( 17.. The informationin Table 17. example. offer viableandattractive altematives.) * " . t) * .12 1.8Vc. one method is to apply the trapezoidalrule to eachsegment and sum the results: I ht .l G.methods as such Rombergintegrationor Gaussquadrature. in engineering and science the order (i.456040 044 o5a 064 o70 080 f {x1 2 842985 3 547297 3 rBr929 2 363000 0 232AAA ) S o l u t i o n .2+ r. greaterthan fourpoint) lbrrnulasare not commonly used. 18.16) is the in the formel are constant. ( 1 7 . in the to for Recallthat the correctanswer 1.experimentally For deriveddata often is of this type.s91801 perceut which represents absolute relative an errorof €t :2.when the function is known and high accuracyis required. 17.309129 + + 0. is TABTE 17.2 + 25x . whereft. all lbrrrrulasfor numericalintegrationlrave been basedon equispaced data points. higherthat.3 Doto for "f(. 6 I T r o p e z o i d o lR u l ew i t h l ' l 1 s ^ " ^ l s a a m a n l c Problem Stotemeni..1. ( 17..200x2 67513 9O0ra 400"t'.13393 2 074943 2.e. A p p l y i n gE q . usedfor thecompositetrapezoidal rule.+Ot0:_::'lj:_:::=t.30) that ft's between Eqs.161 0 212 +.305241 0. Furthermore.nust deal with unequalsized seqments.ic9729 t 345241 ) 7.10 + 2.it must alsobe stressed practice. : the width of segment Note that this was the sameapproach i.6 INTEGRATION WITH UNEQUAL SEGMENTS To this point..30) determine integral thisdata.Use Eq. of f (x) 000 0t2 o22 o32 036 040 0 200000 1 . in described Chap. r h .Sirnpson's rules are sufficientfor most applications.1(x.The only difference and( 17. 3 0 y i e l d s I :0.640533.309129 1.. For thesecases. . I + with uneguolly spoced volues :r.412 NUMERICAL TEGRATION RMUTAS IN FO However.
n1 +y\k+\\) /2.22 .r.12. end s = 0.error('x n = lengrth(x). s : s + (x(k+1)x(k))(y(k) end T c ' ' The r : l u n c t i o n s J e s e r i h eiJ S c c t i o r1 9 . in to F I G U R E7 . . 6 : >> x .Two vectors.ero for An applicationof the Mfile can be developed the sameproblemthat was solvedin E x a m p l e1 7 .i.end if nargin<2..64. error ( 'x and y must be same length' ) ./ector of independent var:rables % y = vector of dependenE variai:les ? % output: 2 I=inteqralestimate least 2 input arguments resuired').trL o t dlt:alt_ftte YJ T = trapuneq (x. M{ileio lmplement tropezoidol lorunequolly function o.1 MATLAB Mfile: trapuneq A simple algorithm to implement the trapezoidalrule for unequally spaceddata can be variand written as in Fig.14. = trapuneq(x. >lJdLFu :d L r a p s T u '.6.u J f \y. if lengch (y) =n.6.end if any(diff (x)<0).9 0 0 " x .32 >> v .l0 . 17.2+25*x200*x. > > l r a p L l n e q ( x .1 ' 2 + 5 15 * x .rA loop is employedto tors are of the samelength and (b) the x's are in ascending from thoseof Eq. 7I. y) where x and y must be of t.4)a L . (17.54 . y) : Z rule to determine the integral ? Applies the trapezoidal (x.44. ^ 3 . ( . y ) . holding the independent dependent that (a) the two vecinto the Mfile. for k = 1. " 5 .y) J. to accountfor the fact that MATLAB doesnot allow z.I2 . i n r . v. 1 4 1 the rule spoced dofo.30) generate integral.error('at not monotonically ascendrnq' ). : (o t! which is identical the resultobtained Example17.36 . o ir_n r. r_ l .Notice that we havemodified the subscripts the in subscripts anays.4.6 INTEGRATION WITHUNEOUAL SEGMENTS 413 17.x andy.4.he * for n daLa points ascending % same lengrth and x must be monotonically % input: x = . ^ 4 + 4 0 0 * x .Bl. Two error trapsareincludedto ensure ablesarepassed order.
5=[Q 1 L. MAILAB has another function.x &odr. Here is a simple MATLAB sessionthat uses this function to integratethe data from T a b l e1 7 .vJ o 5 9.1 B). Then usecum1.m=70.7 . cumr rapz.I2 . Incorporate some randomerrorby rounding the velocitiesto the nearest integer.14. l') wherethe two vectors. x and y.275 kg/m. i7.^ 5 .54 . hold the independent and dependent variables. The distances can then be comouted as / J^Ltopt( .64 .'.2) to syntheticallygeneratesuch inforrnation a 70kg for jumper with a drag coefficient of 0. " ' 2 + 6 1 * x . It hasthe general in syntax z t rapz (x. respectively. Solution.2 MATTAB Functions: trapz Qrd cumcrapz MATLAB has a builtin function that evaluates integralsfor datain the same fashion asthc Mfile we just presented Fig.In addition. > > y = 0 . Someunequally spaced timesandrounded velocities be generated as can '.7 UsingNumericol Integrotion Compute to Distonce fromVelociiy ProblemStotement. 17. hold the independent dependent and variables. 3 : >> x = t0 .t .44 . ^ 4 + 4 0 0 * x .4 . Use Eq.3 5 5.5948 In addition.22 . :trl: I u \ t ld t J(\ Suppose that we had measurements velocityat a series discrete of of unequally spaced tirno during free fall. cct=O 2'15 .32 . > > v = r o u n d ( s q r t ( g * m . lornot shu'r (J . ^ 3 .respectively. As described the beginning this chapter.2 4L1 80.develop plotofth a analyticalar. rapz to determine distance and the fallen comparethe resultsto the analyticalsolution(Eq. y.. / c d )* t a n h ( s q r t ( g * c d / m ) * t ) ) .) where the two vectors.3. . niceapplication at of a of integration to compute distance of an objectbased its velocity as is the u(t) in on z(r) (recall 17.9 0 0 * x . cumtrapz (x.81.h .7 I44. (17.2 0 0 " x . sirnple representationof its syntax is A z .6 19.4 2 3 4. 2 + 2 5 * x .45 173.6.2): Eq.y) 1. 5 >> trapz (x.rd cornputed distances along with velocity on the samegraph.414 FORMULAS NUMERICAL INTEGRATION 17.85 23r. that computes the cumulativc inte_ural.a). >> 99. 8L. a n d z : a v e c t o r w h o s e e l e n r e n t s ( k ) h o l d t h e i n t e g r a lf r o r n x ( 1 ) t o x ( k ) . z EXAMPLE l7.
[ . z.' a' ) >> >> >> >> ra=linspace >> xlabel title('Distance t1me') versus ('t (s) ') . numerical the and analytical results matchfairly well.. 11. after 8 seconds. om A graph of the numericaland analyticalsolutionsalong with both the exact and rounded velocitiescan be generated with the following commands: (t (1) .7m. t rve As in Fig.h e u r . jumper hasfallen 231. . wnereos w p o i ' r ' s e e d e l e ' r i r e d ^ l n e c o l . .I2. r.6 INTEGRATION UNEQUAL WITH SEGMENTS 4t5 the Thus.15. This result is reasonably the closeto the analytical solution(Eq. za. on in Distance versustime O LCS a nalytical numerical kg ng nd he . . . 17. za=n/ cd*1og (cosh (sqrt (qr*cd/m) *t.t. F I G U R E7 . fu^clio. >> ploL (La.ly.4): )ly. 1 5 1 Ploi distonce of time line versus The woscompufed theonolyticol with rne solufion. t (length(r) ) ) .ylabel ('x (m) ') 'numerical Legend ( 'analyticaL' .a) ) ..
.1hod Segmenls (n\ Formulq Truncotion Enor 2 3 ( b . the integral in one of the dimensionsis evaluated first.theyhave utility for analyzingimproperintegrals.'.{17.successive pairs of the formulas have the sameorder enor. The l3) ore in so thottheweighting the doto points estimote overogeheightis opporent.4 NewtonCotes open integrotion formu/os.4summarizes NewtonCotes the openinteT4rotion. r t ) ..For example.1 4 .fbnnulas.v) L' (1. .t r . f ( x t )+ l ( .they will haverelevance our discussion In to of methodsfor solving ordinary differentialequations Chaps.) : at r) L' (1.32) states in that the orderofin . f( . Poinls I 2 Nome M dpolf t rn. However.314)h3 f"G) 2 J ( r t ) . Recallfrom calculus that suchintegrals be computed iterated can as integrals: a.l : 1 1 ( f r + 2 b l ( x t ). siep to of the The size i s g i v e nb y h : ( b . l ( ..20 and 21.6b that openintegration formulashavelimits that extend beyond the rangeof the data.'/(r' or) (17. The result of this firstintegration is integrated the seconddimension.iar161 3 I l .r.8 MULTIPLE INTEGRATS Multiple integralsare widely used in engineering and science. .Equation(11.Table 17. 11.l14A)h1 f\6)Gl 2A (b a\ 17. r r 1 ) (bu) l+t. in As with the closed versions.r 1 ) (1)(6) (95/l44lr5. . simpleexarnple A would be to take the doubleintegral a of functionover a rectangular area(Fig.f d) lD 2A ) ) i l .The fbrmulasare expressed the tbmr of Eq. 1 ) l l .(. in EXA 17. formulos presenied the formotof Eq. The openformulasarenot ofien usedfor definiteintegration.a ) . f ( .I 3 ) so that the weightingfactorsareevident. 1( r : ) I I I ( r r ) (l4rJ5ll5. ( 17.3 r) The numeratoris called a double integntl. t)r + .* I 1 .a general equatiorlto compute the averageof a twodimensional function can be written as lrecall E q .416 NUMERICAL INTEGRATION FORMULAS TABTE 7. 1 ' ) t 1 x ) (dc)(ba) (r7. l8) can be leadily employedto evaluate multiple integrals.16).7 OPENMETHODS Recalltiorn Fig. addition. 1 \D a)  /3V13 f"G) t. 1 ' 1 . 1( f r ) * . The evensegmentoddpoint fbrmulas are usuallythe methodsof pref'erence because they require f'ewerpoints to attainthe sameaccuracyas the oddsegmentevenpoint fbrmulas..(f x r . / ( . ' '( I i l t r . l ( . r r )* 1l .u ) l n .32) Thus.r 2 . The techniques in discussed this chapter(and Chap. 17.( t 1 .
17.r dimension)and 6 m wide (.8 MULTIPLE INTEGRATS 417 FIGURE 17.x 2 . methodssuchas the cornpositetrapezoidalor Simpson'srule would be applied in the first climension with eachvaltteof the second dimensionheld constant.33. Why doesthisoccur? Reperfect call thatSimpson's /3 ruleyielded  results cubicpolynomials. First.the trapezoidalrule is first implemented along the .17. in Note that a simple average thesevaluesis 47. Thesevaluesare then integratedalong the I' dimensionto give the final result of 2688.r climensionfor eachr'value. First. To make the same evaluationnumerically. Supposethat the temperature a rectangularheatecl of plate is described the following function: by : I ( t .8 U s i n g D o u b l e I n t e g r o lt o D e i e r m i n e v e r o g eT e m p e r o t u r e A Problem Stotement.I 2. for Sincethehighest .This results in an integral 28 l6 andan average 58. ) 2 x ) * 2 r . . of of which is exact.The approach illustratedin the following example. Then the methodwould be appliedto integratethe seconddimension. is EXAMPLE 12. a Now we can applya singlesegment Simpson'sl/3 rule in thesamefashion.let us merelyusetwosegment applications thetrapezoiclal in each of rule dirnension.2 t ' 2+ 1 2 If the plate is 8 m long (. Dividing this by the areayields the averagetemp e r a t u r e s2 6 8 8 / ( 6 x 8 ) : 5 6 . The temperatures the necessary.The function can also be evaluated of analyticallyto yield a resultof 58. Solution.computethe average temperature.r .y dimension).66667.66667.r at and y valuesare depicted Fig.16 Double infegrol theoreo os under function the surfoce A numericaldouble integralwould be basedon the sameidea.
''2+72.y) 2*x*y+2xx x.7: in >> q .0 ) 5 4 + 2  \ 7 0 ]+ 5 4 4+ 496 72 64 24 X 8_u7?:3gej4+ 448 (60) 256+?(4961+448 I F I G U R E7 .6) 2816 . "22*y. addition.If rol is not specified. 1 7 1 Nurnericol evoluotionc double of integrol thetwosegmenl using rule tropezoidol For higherordelalgebraicfunctionsas well as transcendental functions. ( 8 .8. t o l ) where q is the double integralof the function fun over the rangesfrom xmrn to xmaxad to a of . y m a x . l8 introduces techniques that are more efficient thanthe NewtonCotes formulasfr evaluatingintegralsof given functions. wouldbe it necessary use compositeapplicationsto attain accul'ate to integralestimates.vm. Theseoften providea superiormeans implement to the numericalintegrations multiple integrals.4t8 NUMERICAL INTEGRATION FORMULAS Iftiit U 40 48 ^.y m i n . Here is an exampleof how this function can be usedto computethe double integral evaluated Example17.zn ymax. . simple representation the syntaxtbr dbtquad is A of q = d b l q u a d ( f u n.A.1 MATTAB Functions: dblquad ond triplequad MATLAB has functions to implernentboth double (dnlquad) and triple (rriptequad) integration.8.dblquad(@(x. x n t i n . In Chap. defaulttolerance I x 106is used.0. for 17. x m a : . (uu)0+2{40)+48 +256 4 54 70 .
it is more likely that the functionalrelationshipis complicated. 17.18. Simpson'srules. if F(r) and e @) arc simplefunctions.suppose that the force variesduring the courseofthe calculation. you are providedwith only discretemeasurements constraints. xo and x. For such cases. when analyzing measureddata.5). as in w:1"" F(. ff f(r) is easyto integrate. For this sifuation.For example..' ( 17. Although such a simple computationis useful for introducing the concept.18.as well as results.sirnpleapplications were presentedusing forces that remained constant throughout the displacement.W 07. because experimental of at versionsof the trapex : 5m intervals(Table 17.The work equationcan be modified further to account for this effect.realistic problem settingsareusually morecomplex. the force might be available only in tabular form.Use single. : the initial and final positions (m). However.33) where W: work (J).m). (17. The calculation of work is an important component of many areasof engineering and science. assume that.For example. Further complexity is introduced if the angle between the force and the direction of movement also varies as a function of position (Fig. More refined estimates using more segments.This is particularly .CASESTUDY 419 Bockground.and F(x) : a force that varies as a function of position (N). 17. work equationis reexpressed the as w: F(x) dx 1.18).33) can be evaluatedanalytically. The results are interesting becausethe most accurateoutcome occurs for the simple twosegment trapezoidal rule. numerical methodsprovide the only alternativefor determining the integral. ever. yield lessaccurate The reason for this apparently counterintuitive result is that the coarse spacing of the points is not adequateto capturethe variations of the forces and angles.r) cos[9(x)] dx (r7.18at 1m intervals.In fact.52 that was estimatedon the basisof valuestakenfrom Fig.34) HowAgain.The general formula is Work:force x distance When you were introducedto this conceptin high schoolphysics. 17. the work would be calculated as50J(ljoule:lN.6. The resultsof the analysisare summarized Table 17.the force might not be expressedin such a manner.Eq. 17.34) might be solvedanalytically. Suppose that you have to perform the computation for the situation depicted in Fig.A percentrelative in error r/ was computed in referenceto a true value of the integral of 129.In suchcases. respectively..and multipleapplication zoidal rule and Simpson's1/3 and 3/8 rules to computework for this data.Although the figure showsthe continuous valuesfor F(x) and d(x). Solution. if a force of 10 N was used to pull a block a distanceof 5 m. in a realistic problem setting. as in Fig.numerical integration is the only viable option for the evaluation.
0000 1. mognitude. N 0.50 0.0 r30 I . 5r 2 0 8.0 l4.{.40 0.0 r3.3532 .5297 9 .420 FORMULAS NUMERICAL INTEGRATION continued 0 . F(r).8082 1.5 of Doto for forceF("r)ond ongle 0(x) os o function position r.7025 2. wellosihe the os force on The cose o vorioble octing o block.75 090 F(x) cos d 0 5 t0 t5 20 25 30 0.48 r.18 For cose ongle. 0 8 8 1 0.0 9. rod 050 1.theforce of TABLE t 7.o 105 12.0 5. lhis of vories. m FIGURE 17.
93 95.'lo Simpson! /3 rule 1 Simpson's rule 3/B 5 3t r 133 9 124. Thepercent relotive error€.82 I l 7.05 35 75 L57 804 5 5 FGURE 17. The fact that the twosegment trapezoidal rule yields the most accurateresult is due to the chance positioning of the points for this particular problem (Fig. 17. The omission of thesetwo points effectively limits the accuracyof the numerical integration estimatesin Table 17.6.19 A continuous of F(x)cos[d(r)]versus plot posilion theseven with discrete points to develop used ihe numericol integroiion in estimotes Toble 17. evident in Fig.'^ir4rrr6 af E"/t {\ onc tA(1 5\l ? . if data were ^.5 and 12.84 35 r 8.20 is that an adequatenumber of measurements must be made to accurately compute integrals.98 r19. Notice how the useof sevenpointsto characterize continuouslyvarying the function missesthe two peaks at r :2.5 m.6 Estimoies work colculoted of usingthe tropezoidol ond rule Simpson's rules. 17. where we have plotted the continuous curve for the product of F(.9 CASE STUDY 421 continued TABIE 17.09 175.lnl r.20).6 Notice how the use of sevenpointsto chorccterize voryingfunction thisconfinuously misses two peoksot x :2.r) and cos [0(x)].19.5 m. 17. 1 3 r39. The conclusion to be drawn from Fig. os computed in reference o truevolueof the integrol1129. on ot Segmenls I 2 3 6 2 6 3 Work e[ o/o Technique Trnnpzni.9 2.52 thot wos to Po) estimoted the bosisof volues lm intervols. For the present case.I2.5 ond 12.
and (f) Simpson's 3/8 rule. 17.3 Evaluatethe following integral: rn /2 . (a) anal with n = 1/3 rule PROBtEMS Eq. can be r spacedd rO f(x) 2 determine an improved integral estimate.20 Grophicol depiction whvthetwoseqment of lropezoidol vields ooodestimote rule o ofthe for poriictrlor Bychoice.t. Thus. 17. 1.35371.For example. ( 17. use trrotro[ezoids integrol this the of cose. lead to better results.6 Ev I Jt J el Including the two additional points yields an improved integral estimateof I (e.5291 9. we could compute >> x=t0 >> y=[0 2.0881 0.4)by integrating (17.5 5 r0 L2.3940 8.L. 6 I tt (a) analy (c) coml applicat and (f) E through 17.1 DeriveEq.5t"20 11.4 Ev 1a a. determine prthe cent relativeerror basedon (a).3). (e) composite Simpson\ 1/3 rule with n : 4.5 15 20 25 301.l6Eo).. the inclusion of the additionaldata incorporates peaks the that missed previously and.8087 . >> trapz (x. h"oppens toon to leod even bolonce between posifiveond negotive errors. ft f (x) FfGURE 17. 3. using the MAILAB trapz tion.Foreachof the numerical estimates(b) through (f). as a consequence. 17.5 Th .9007.1025 2.2 Evalnte the lbllowing integral: r4 JO I tt .6458 Evaluate lytical rn of the tri attain thr cent rela 17.. y ) r32 .e2'1tlx applicationof Simpson'sl/3 rule.:2.422 NUMERICAL INTEGRATION FORMULAS continued (a) anal (c) coml applicat  /3 rule the num cent rela 17.
the trapezoidal and Simpsou'srules whereverpossibleto bottom.Becauseboth pressurcand area vary with elevation. 17. For (b).f(r): 2e r 5' tl2 u56 325 55 45 7 6 B5 7 B B 6 B5 7 93 t0 5 can useclto generatethe following table of unequally be spaced data: 425 O OO5 015 035 f ( x ) 2 1 8 5 5 . which fbr thi. singlc applicationof the trapezoidal (c)conrposite tlapezoidal rule with n :2 iutd 4. can be dctertimes the areaof the dam tace mined by multiplying pressure (as shown in Fig. compute the percentrelativeeror.with dcpth.5 The firnction .1 Evaluate lirllowins inteeral: the efror. Integrate cLrbic the distance.. Pl7..rpson's rule./.Accordingto Eq. Simpson'sl/3 rule. (e) Simpson's of (f) s and Boole'srule. P17. and (f) Simpson's3/8 rule.9b). the total force . (e) composite of 1 .)(1. (P17.9 : acceleration to eravity due t9. OU 40 zu 200 190 175 160 (b) FIGURE P17.7 Evaluatethe triplc integral analytically. r 3 3 r ' : )d t d t ' d : 1/3rulewith n : 4. relativeerror'. n g .Ornittingatmospheric aurin highestaccuracy.) . analytically. For eacho1' estimates(b) through (f).p : densityol i to water.1.and (c) a combinltion D : elevation(in m) ofthe watersurfaccabovethe reservoir lytical means. computethe per. J^ (a) (b) rule.(d) single l/3 3/8 rulc.81m/s2):antl (b) rule. P17. : I psw(z)(Di.J.8 Detcrmine the distancetravelcd tionr the follouinc + 2xs)rir velocitydata: I .51 5 9 7 0 1 3 7 4 6 l l 8 3 l r A. computethe percentrelative 17. and (c) using singleapplications Simpson's ir of f.and (b) using single applicationsof on cerrt relltirc erlor hased 1a.9). 17.6 using (a) ana_ 103kg/rn3. cent (because works against prcssure it both sidesof the dam facc 17. (b) Fit the data with a cubic equationusine polynomial equation1odeterminethe regression. face of a dam as I 7.(b) using thc composite trapezoidalrule r I) with : 2.9 Walcr exertspressure thc upstream on can be characterized by shown in Fig. For cach of the numericalestimate (b) (f). on through determinethc percentrelativeerror based (a).9 foce Woter pressure lheupslrecm of o exerting on dom { o J: .{5 (a) Use the tlapezoidalrule. JO l/3 rule.pressure pascals(or N/ml) exertedat an in 0s308 08131 elevation mctersabovethe rescrvoirbottorn.PROBLEMS 423 (a) (b) rule.pressure ofthetrapezoidal linearly increases the For (b) and (c). (d) single n) n) rl l/3 Simpson's application Sirnpson's rule.ssunred be a constant Evaluate inlceral the from rr :0 to b :0.sproblem is a. application Sirr.6 Evaluate double intesral the and essentially cancelsout). d e r i e w s L o * r n gf o ' t e .. ^ r r e o s .as depictedin Fig. determinethe perthe numerical (a) analytically.9.9rz.z) 0o where p(.thc total force is obtainedby evaluating (a)analytically.The prcssure pQ): pg(D. 17. singlc applicationof thc trapezoidal (c)composite trapezoidalrule with n : 2 antl .For (b) and (c).1.
.glcm34ao 395 389 3Bo 3./0 0 0 I50 3r00 30 340 60 l2aa 90 t2a t600 27aa where R : t and.12 An I lm beam is subjected a load.\ Jr. r : distance alongthe rod and L : the total lengthoftherod. where u(l): width of the dam face (m) at elevation.We know that V : dM ldx. F(/). mln Q.r is length in distancealong the beam. r. fng/Jn" J.. ffi M: M:M. The following datahasbeenmeasured a l0m length for rod. Perfbrm thc intcgral necdedtbr thc avcragcin theorder shown by the following equation: o: 1 L/". I pG)A.16 Integrationprovides a meansto computehow much 17.l. Pl7.r:200(.:s'a \/T'l where: : the elevationabovethe deck and H : thc height of the mast.su(:)(D .60 34t 33l A. and (b) the rateofcan going throughthe interscctionpcr minute. mls  . N/m 17.r c. (b) Repeat(a).m a 2 4 l0 3 6 B p. but use the composite Simpson'sl/3 rule. (Hint: Be careful withunits.: (Fig.)tJz.r: Jt) p(.2512 where V is the shearforce. Integrationyields the relationship rI ft) l.r : I l.. tributed 17. m3/mi.Thus.'.l Compute the net fbrce and thc line of action due to this diswind. p.I ll J..r).) t1: ft Use Simpson'srule to compute J.+ I Vttt' . Determinethe massin grams to the best possibleaccurac).424 INTEGRATION FORMULAS NUMERICAL If M. the integral reprcscntsthe summationof the product of flow times concenqivo I et tlr U.: I . whererz : filoSS.) dz The linc of action can also be deterrnined integratron: by r rH J0 :. F(/). Use thebest numericalmethodto determine(a) the total numberof can that passbetwecn7:30 and 9:15. m Force. l7..) Time (hr) 73A Rote (cors per 4 min) I B 7 45 24 B:OO B:15 l4 24 O:J.).The total force F exertedon the mast can be dctermined by integratingthis function over the height of the mast: x.14 A transportation engineeringstudy requiresthatyou determinethe numbcr of cars that passthroughan intersection traveling during morning rush hour. and. and the shear massentersor leavesa reactorover a specifiedtimeperiod.15. f r. andcl.fr:\ 'l: (a) Use the compositetrapezoidalrule to compute F rnd d for the casewhcre 11 : 30 (n : 6). calculate using(a) analyticalinM tcgration.. 17. You standat the sideol the road and count the numberof carsthatpass every 4 minutesat severaltimes as tabulatedbelow. and t2 .r): crosssectional area. cm2 loo lo3 l06 I lo )2a I33 lsc 17.9b).iszero and.It:ta: u Use numerica data listed bel t.(r)dx / .rl.(b) multipleapplication trapezoidalrulc.PI 7. respectively.1. N/m Height l.1r I w h e r eU : a numerica data: )rfi I. and M is the bending moment. The line of action can also be obtainedby evaluating IoDpgzu:(z)(D z. A./. and(c) multiplcapplication Simpson's rules. 17.ll A wind fbrce distributed againstthe side of a skyscraperis measured as Height /.r : dist averagetlov IB0 2)A 240 3200 3500 3800 17.For (b) and (c) uselm lncrements.. This formula makesintuitive sense you rccall thc analogy if betweenintegrationand summation.. .(..r) : dcnsity.the initial and final times..I k.t Jt where I.15 Determine average the valuefor thedatain Fig. /(\. m Force.17 The c Putedas B : 4 5 9 :i 5 21 9 A.10 The tbrce on a sailboatmastcan bc reprcscntcd the by following function: . to force follows the equation a si n V(r):5+0.13 The total mass of a variable density rod is given bi71.
' (mr/s) can be contputccl average as 0 0 : rl] I U{.hercthe arcaA.4 lB0 (m/s).r.rad 0 0 I 30 2. min Q.25 oa2 . 9 6 3 5A 3 5 2 7 O O O O O c. 1odetcrmine cunrulativc the work andplot the resultversus r.6 43 43 4A 37 32 50 5. mg/m3 l7.0 34 (: .55 52 35 4A 45 4.7 60 38 90 37 l2O 3 150 1.h 0.4 52 4l wherc : the total channelwidth (rn). Determinethe averageconcenlrationbasedon thc followins data: 17.m H.9.+.t'(.r. ftr U.da.lll The average concentfation ofa substance (g/rn3)in a lake u. thc l1ou. m/s 05 003 r3 006 56 1.Use these where : watervelocity' U relationships and methodto determineA.lrrlr.)z/r. wherc Z : the total depth (ni).19 As wasdonein Section17. m3/min c. B and : distance r' liom the bank (m ).and B fbr the fbllorving a nurnerical data: n)/1 J.25 l7 005 412 l 0Ir a.g/m3 )a'2 85 7. Jo 17. H : rhedepth(m). I 5 numerical Use inte_uration evaluate to this equation the fbr data listed bclow: l.= I H(r')r1r. the curnLrcipzfunction Use g. J0 rll O 4 z. In a similarfashion. cletermine work perthe fbrmed ifa constant force of I N appliedat an anglegresults in the iollowing displacernents.17 The crosssectional area of a channelcan bc computed as A. l a 6 m 2 9 8 1 7 5 5 t 0 5 t 1 ..1m:) varieswith depth l(m) can be computcd integration: by 0 4 t0 t0 2a 30 4B 52 50 3_s .PROBTEMS 425 u 8 4 4 3 I 7 z 1 10 12 0 F I G U R EP I 7 .n B 12 )6 A .
thecomposite Sirnpson's rulemight seem be a reasonable l/3 to toolfa such problems. The form of the datahas an important influenceon the approaches can be usedto evaluate integral. yut if can generate many valuesof. 17. more accllrate value.. are limited by the numberof points that are given. which is a method fa combining two nunrericalintegral estimatesto obtain a thild.Specificobjectivesand topicscoverecl are ' . The flrst technique is based on Richardson extr(tpolatiorz.if* .For tabulated that you the information. providessuperiorintegralestimates Understanding how Gaussquadrature by picking optimal abscissas which to evaluatethe function.This techniquecan be usedto generate integnl an esrimate wifhin a nresnecified eror . for therearemore efficient methodsthat are available. the fuuctionis available. At tacevalue.This chapteris devotedto three suchtechniques. Tlc computationalalgorithm lbr implementingRichardson extrapolation a highlyefficied in rnanneris called Rombergintegration.l (r) as are requiredto attainacceptable as accuracy. o providesa meansto create more Understanding how Richardson extrapolation a integral accurate estimate combiningtwo lessaccurate by estimates. In contrast.Although it is certainly adequate many problems. Bolt capitalize on the ability to generatefunetion values tcl develop efflcient schemes fr numericalintegration. I 8. NumericolIntegrotion of Functions CHAPTER OBJECTIVES The primarv objectiveof this chapteris to introduceyou to numericalmethods for integratinggiven firnctions. we noted that functionsto be integrated numericallywill typically oftno be lbrms: a table of valuesor a tunction. at q.r.:d r1uadl integrate Knowing how to useMATLAB's builtinfunctions and to functions.I INTRODUCTION In Chap.
thesemethods use two estimates an integralto computea third. The estimateand the error associated with the compositetrapezoidalrule can be repgenerallyas resented r:t(h)+E(h) where 1 : the exact value of the integral.This techniques appliescomposite Simpson's I /3 rule to subintervals the integrationrangein a way that allows error estiof mates to be computed. superiorresultsare attainedfor lesseffort. more refined segmentation is only usedwhereit is necessary.It is quite similar to the techniques discussed Chap.2) lf rtis assumedthat f" is constantregarcl\ess of stcp sizc. Two builtin MATLAB functionsthat useadaptivequadratureare illustrated. 18. rnore accurate of approximation. The third approachis called adaptivequadrature. in Chap.r valuesthat arepositionedbetweenthe integrationlimits in sucha mannerthat a much more accurate integralestimateresults. values of at /(x) for the NewtonCotesformulas were determined specifiedvaluesof . 17. (18. 18. (18.2) .r. 2 1 ) w i t h n : ( b . If we make two separate estimates using stepsizesof h1 andh2 andhave exact values for the error: I (hr)t E(ht) : I (h) f E(hz) (1 8 . ( 1 7 .we were constrained takethe an to weightedaverageof /(x) at the endsof the interval. andE (h) : the truncation error. (18. 1 ) Now recall that the error of the compositetrapezoidalrule can be represented approxlm a t e l yb y E q .! (1 8 . 17 in the sensethat it in is basedon successive applicationof the trapezoidal rule.ROMBERG INTEGRATION The secondmethod is called Gcuss qundrnfure. Generally calledRichardsonextrapolallon. we have made it possibleto utilize the information embodiedby Eq.throughmathematical manipulations. For example.2 ROMBERG INTEGRATION Rombergintegrationis one technique that is designed attainefficientnumericalintegrals to of functions. In so doing.a ) l h ) : [ bu E__htf" 12 .Gaussquadrature formulasemploy.2.Eq. However.In this way.a) I n.2) canbc uscdto <lctcrmine that the ratio of the two errors will be E(ht) E(hz) _hh i . I (h) : the approximationfrom an nsegment applicationof the trapezoidal rule with stepsizeh : (b .Recall that. 3 ) This calculationhas the importanteffect of removing the term 1" from the computation. if we usedthe trapezoidal rule to determine integral.These error estimatesare then used to determinewhether more refined estimatesare required for a subinterval.1 Richordson Extropolotion Techniques availableto improve the resultsof numericalintegrationon the basisof the are integral estimatesthemselves.
/ h.This estimate can then be substituted into I : I(hz) _t E]tz) to yield an improvedestimateof the integral: I : l(hz) + 1t.1): into /h.3) o give t E ( h t t= E ( h : t l . 1978) that the error of this estimate is we of O(h4). havecombined two trapezoidal estimates O(h2) to yielda newesrule timateof O(h\.To do this. Thrrs. (18.I\hzl 1(htlh)2 Thus.428 NUMERICAL TEGRATION FUNCTIONS IN OF without prior knowledge of the function's secondderivative. 0. Single composite and rule be to applicationsthetrapezoidal can used evaluof atetheintegral: Segments h Integrol E I 2 4 0B a4 02 o t72B I 0688 t 4B4B 89.200x2 675xr 900r+*400x5 tioma : 0 to b : 0. a'(") (185) EXA Extropolotion E X A M P 1 B . we have developedan estimateof the truncationelror in tenns of the integral estimatesand their step sizes.  I(h2tlE(htl \tt2 / which can be solvedfor p \ ( / r/ ' ) : E "1 Itlttl. I Richordson LE Problem Stotement. we reanange Eq.4) \tt1/h2)'  It can be shown (Ralston and Rabinowitz.( 18.8. ll ttt:l ttttrtl fl8.\2 I(h1tLEthrtl . For example.5% 349% 5.r)= the of . For the special casewherethe intervalis halved(h2: hr12). \: \n2/ I which can be substituted Eq.2t 25x t Solution.5% Richardson can estimates extrapolation be usedto combinetheseresultsto obtainimproved can be combined of the intesral.UseRichardson to extrapolation evaluate integral /(.thisequation becomes ' : 4l lt(nt.the estimatesfor one ancltwo sesments .
two where f.6c/r:).2 ROMBERG TEGRATION 429 to yield 3' : (st is Et improvedintegral : 1. l. This approach a subset with error O For generalmethodfor combiningintegralsto obtainirnprovedestimates.ru ro u r): r .(18. Theerrorofthe estirnates whichit wasbased.0688): 1. Thus. 1.5 16 lt I5 r (r 8 .lro.2 :XAMPLE HigherOrderCorrections ProblemStotement.361461 0.640533 1.623461..623461 l.ja/c).2 The Romberg Infegrotion Algorirhm in Notice that the coefficients eachof the extrapolation equations [Eqs. ( I 8. we computed improvedintegrals in turn. instance.the equation usedfbr O(li6) accuracy are basedon successive I : 1.oazsr. 64  ll I 63"' 61 ' ( 18 .. a more of of two improvedintegrals O (ha) on thebasis threetrapein ExampleI 8.2. I : (1.5). en 4 fl ) .and (18.. Solution. estimates two andfour segments becombined give the In thesame 41 . and 11 arc the more and less accurateestimates.36146i 3' an which represents errorof E. zoidalrule estimates.6) to yield and 1.Similarly.!t. : :0. usedRichardson we extrapolation compute to two integralestimates Othl). rule of Equation(18. In Example18.. as accuracv increases.rzz8):1.4)providesa way to combinetwo applications the trapezoidal (h2) to computea third estimate is of with error O (ha1.623461 53 t :lt.IN I 8. thev reoresent weichtinefactorsthat. . (18. can be combinecl compute integralthat is O(/r8)using to an O(ft6) results [ : 1.1.l8. upon whichis superior the to to for can manner. Utilize Eq.7)l add up to l.011061 (et : 1. be combined yield an to Thesetwo can. 6 ) respectively..640533 1.(18. is halvingof the stepsize.361461 in of into Eq.4848)(1.l were 1. 7 ) . 18.6).6)to combinetheseestimates computean of to w integral ithOtlr6t.Thesevaluescan be substituted r : ! l .2130(t1 : 76. The two integralestimates O(h1) obtained Examplel8. For the special casewherethe originaltrapezoidal estimates evenbettervaluewith Oth6l.640533 t5' t5 which is the exactvalueof the intesral.
The indexj is usedto distinguish ft For example.k:2 conesponds the O(ft4) to e$ito between more the mates.respectively." 1:333333163s467 . FIGURE .  4r tlj*r.t:!4 J which is equivalentto Eq.1 graphical depiction of the sequence integral estimatesgenerated of using this approach. ib) o(h2) o(h4) o(h6) o@8) t'r ?:1633331s67467 0j72800 ? 1.I integrals.367467 . Second iferotion.andits systemby isa atic applicationto evaluateintegralsis known as Rombergintegratiut.1. Third iterotion. ( I 8.r 111.361461which hasan errorof O(h41.r.11 I1.1a) involves computing the oneandtwos e g m e n t t r a p e z o i d a l r u l e e s t i m a t e1 a1 1 1 21 ) . l:13?3331623467 0..(18. 18.8) to obtain successively better matrix are generated systematically by estimates the integral. e ' c e o f i n e g r o J c i m o l e 5 e n e ' o l e dr s i n gR o r n [ 6 1i9 i e g r o t i o n o e (o) First (c) iterotion.430 INTEGRATION FUNCTIONS OF NUMERICAL placerelatively greaterweight on the superiorintegralestimate.8) estimates.and so forth.r. rule the Each matrix corresponds a singleiteration. where : I is for a singlesegment application evaluations that are designated "r sizeisba).z: 1. andso forth. : 3 to the O (ho).* 1.1 4il . U + l) and the less (l) accurate becomes tr.whereft : I cotresponds the original trapezoidal rule estimates.The first column contains trapezoidal to ($ep 1. ( 18. of For example. (18. E q u a t i o n ( 1 8 ..8)is attributedto Romberg.r r : the more and less accurate and andllt = the improved integral. Figure18.a) l4].r<. es e q .The index ft signifiesthe level of the integration.068800 1..I I8 g n G ' o p n i c o d e p i q f i e n f t l .for ft : 2 and j : 1.640533 * 1.640533 1. The generalform represented Eq.5). .640533 .j=3isfor of application[stepsize is (b .640533 ' 1. The othercolumns the a foursegment applying Eq.623467 .367467 F 1.172800 1. Eq.j:2isforatwosegmentapplication[stepsizeis(bd)12]. Theseformulations be can in expressed a generalfbrm that is well suitedfor computerimplementation: I. 8 ) i s t h e n u s e d t o c o m p u t e s( nd the elementIt. the first iteration (Fig. where 17.
becausewe are evaluatinga filihorder polynomial.and then Eq.8)is is appliedto computesuccessively more accurate integrals along the lower diagonal. fbur. Sirnpson's of l/3 rulc' would requireabouta 2lSsegment in application doubleprccision yield an estimate to of the integralto sevensignificant disits:1.640533.as was donepreviously other in processes. Rombergintegration proln drrces sameresult basecl cornbiningone.. lle ep br le er lp.10).I 8. cornbined with /1.2presents Mfile lbr Romberg an integration.s approxirnate in book.I level of integration with j :2). the estimate fr .l.For Eq.. l8.the previousvalue is the most accllrate fiom the previouslevel of integration(i. The third iteration(Fig.fbr determination the integralas shownin Fig. trrep to implement composite tl'apezoidal evalutrtions the rule Here is a MAILAB sessior. To do this. (18. 17.0. 6 4 0 . the computation terminated.lxroo"/.criterionis required e1 to assess accuracy the results.0.the resrrlt ( l t t : 1 .. this case. is combinedwith 11I usingEq. When the change between old auclnew values the as represented r:.}c/r.2 ROMBERG TEGRATION IN th 8. 5lpppin!... and eightsegnrent the on trapezoidal rulesthat is. 3 is Rombere integlation more efficientthan the trapezoidal rule and Sinrpson's rules.8) 2 0i]*t'+b75*x 1.8) 1. .1:.1to yielcl1r : : 1. I:t .a lernrinrrtion.a )h. 18.2+25*x rombcrg(f.. of I r. (18.623461 The resultis. For example.l: >> >> f =(i lx) 0..8) t: Now.is determined.. a foursegment Then it trapezoidal rule estimate. Note that the function usesanotherfunction (recallFig. 18.r:lu.068800  lx1001 :21.8q t.8)to generate : . with only l5 functionevaluations! Figure18. a when comparedwith the previousresult 12. Thus. Equation i18..this evaluationindicates the following percentchangeover the colrrse the first iteration.two. : an estinrate the percent of relative error.5405 .r : 1.9) can be appliedto determine that this result represents changeof 1. we must check to determinewhetherthis result is adequate our needs. conrpare new estimate iterative we the with a previous value. the of One methodthat can be employed the present for purposes is :orstiore i. Afier only three iterations.r. (18. usingloops.e) wheret.4848.9).r showinghow it canbe usedto determine integral thepolynomial the of fiom Examole18. 18.10)is to obtairrthe O(h6l estirnateIr.lc) continues process tlresame the in fashion.e.':l )re The objcctof the second iteration(Fig. in fbr As other rnethclds thi. in turn.5 3 i s)e x a c t . In an eightsegment trapezoidal estimate addedto the first column.2.167167 I I m.1a.For Fig.640533.is below a prespecified by is error criterion0. (18. contrast.36i461 .this algorithm By implementsthe methodin an eficient manner..
= additional parameters used by func % % output: qintegralestimate % (%) Z ea = approximate relative error = number of iterations ? iter if narqin.n.es. i terl'romberg ( func.a.iter+l).end r 1.p2. 18. end end q = I(1.end rt nargin<4 isempty (es ) . l. k ) = (4 ^ (k .n.3 GAUSSQUADRATURE In Chap.1 ) . location of thebase the points usedin theseequations was predetermined fixed.iter) ea = abs( (I(1.egration lirnits (default = 0.b. end i I nargin5tismpfy(maxlr.. I (j . for k = 2:iter+l j = 2+iterk.1 ) * I (j + 1 ..iter+l) ) "100. I8.r (j .000001. k . ).as depictedin Fig.000001?) % es = desired relative error (default. maxit=50. characteristic theseformulas A of (with the exceptionof the specialcaseof unequallyspaced data)was that the integral estimate was basedon evenly spacedfunction values..432 INTEGRATION FUNCTIONS NUMERICAL OF function le. es0 . = 0..1) }) . = iter+l.p2. 1) . we employedthe NewtonCotes equations.varargin) quadrature ? romberg: Romberg integration q = romberg(func.Consequently.es.b. if ea<es.b. or For example.maxjr.1 )  / ( 4 ^ (k . ea. I (iter+1. end T(2.the trapezoidal rule is basedon takingthearea underthe straightline connecting function valuesat the endsofthe integration the interval.errot ('dL leasu : inpuc argumenrs requireo'). The formula that is usedto computethis areais = (h + f (u) . = trap(func.iter+l) ) /I(1.a.maximum allowable it.a.name of function Co be integrated % a.maxit.pI.2 MJile implement to Romberg integrotion. = 30) ? maxit . FIGURE I8.3a.trap(func. iter n = 2^iter. break.1 ) .erations pl. % % input: ? func .varargin{: I(1.a. b = int. iter while iter<maxit.1 ) ..varargin{ : } ) .) % : Romhero inteoration.b.. k . 17..
positioning By these points wisely. 18. This methodwill then be employedto developthe GaussLegendre formulas.we would arriveat an improvedestimate the integral.I8.Hence. we could definea straightline that woLlldbllancc thc positil'c tnd ncgativeerrors. positive the ond negclive errors beller ore bolonced. will show how numericalintegrawe tion formulas suchas the trapezoidal rule can be derivedusingthe methodof undetermined coefficients.3awhere the fbrmula resultsin a large error.3b.1 Method of UndeterminedCoefficients In Chap.we derivedthe trapezoidal rule by integrating linearinterpolating a polynomial and by geometricalreasoning.asin Fig.Before describingthe approach. 18. to The particular Gauss quadratureformulas describedin this section are called GaussLegendreformulas. 18.3 GAUSS AUADRATURE 433 "f("r) (b) FIGURE8. on improved ond integrol estimote resulls whereaandb:thelimitsofintegrationandba:thewidthoftheintegrationinterval. The methodof undetermined .3 I depiction thelropezoidol os fheoreounder stroight loinino of rule the {o)Grophicol line fixed end points{b)An improved integrol eslimofe obtoined toking oreounder stroight by the the line possing through intermediote two points. 17. of Gttussquadratureis the name for a classof techniques implementsuch a strategy. Now. Becausethe trapezoidalrule must pass through the end points. suppose that the constraint tixed basepointswas removedand we were free to of evaluatethe areaunder a straightline joining any two points on the curve. By positioning thesepointswisely. there are casessuch as Fig.3.
"._..Thus. .0. :l r(hu\ 12 I J (bu)12 ldx and ba 2 o. (18.10)is expressed the Eq.. Now realizethat the trapezoidal rule shouldyield exactresults when the function being integratedis a constantor a straightline.434 NUMERICAL TEGRATION FUNCTIONS IN OF FIGURE I8.4 Twointegrols should evoluoted thct be exocfly l h a ' r 6 p " r o .+ : I'.'j.lt. *..il) where the c's : constants. the following equalities shouldhold: {i. 6 o lr u l ^ .4). as I = c g J @ )+ c t f ( b ) (r8. Two simple equations that represent thesecasesare J : I and ) : r (Fig.{ o )o by (b) stroight c line Lonsio"rro'ro To illustrate approach. 18.
However.3 GAUSS QUADRATURE or.3. requirefour conditionsto determine and we them exactly.to anive at the other two conditions. FIGURE8. the object of Gauss quadrature to detenninethe coefficientsof an equationof the lbrm is I = cof 6il + clf {.5 I Grophicol depiction theunknown of voriobles.I8. ( I 8. we cirn obtain two of theseconditionsby assuming that Eq.J(It+ zz bu^. consequently. ^ J(bl which is equivalentto the trapezoidal rule.in contrastto the trapezoidal rule that usedfixed end points a and b.Thus. I: ba^ . co*ct:ba and cu 435 ba 2 ba bo n 2 with two unklrownsthat can be solvedfor Theseare two equations L() Ll  z ^ gives which.2 Derivqtion of the TwoPointGousslegendreFormulo Just as was the casefor the previousderivationof the trapezoidal rule.w e d e t e r m i n e l t. 18. evaluatingthe integra. y: x r ) l . 12)fits the integralof a constant iurda linearfunctionexactly. when substituted back into Eq.rs r1 for integrotion Gouss ond by quodrolure .u n c t i o ns yd o i n gt h i s .11).ls.  8.5). we now have a total of four unknowns that must be evaluated. Then. (18. but are unknowns (Fig. .rt) rrg.we merely extendthis reasoning assuming by that it also fits the i n t e g r ao f a p a r l b o l i c( ) : x 2 ) a n da c u b i c( . Just as for the trapezoidalrule.rz) wherethe c's : the unknowncoefficients. the function arguments and r1 &rerot fixed at the end 16 points.
13) (18.The fbur equations be solvedare fl crtltt: J l I ldr:2 fl  Jl . 1 6 ) . ( 1 8 . : i f rt 2 / 12rlr:= 3 lt pl Jt / . ( 1 8 ./: If tlre lower limit.1/ J3 andl/V3 yields an integrirlestimate through( I8./x:o ( y E q u a t i o n s 1 8 . w h i c h c a n be solvedfor . 1 a ) f o rn d s u b s t i t u t e t h e r e s u l t i n t o E q .frr I /a : 0. Substituting result Sincex6 and 11 cannotbe equal. r : b.15)gives Substituting these : .r8) 1. we arrive at the_interesting that is thirdorderaccurate.. corresponds x4 : 1. VJ . (18.19) tor.r':]:0.2^  )  EX into this that. trvopoint the t:r(i)*/f+) " \.436 OF NUMERICAL TEGRATION FUNCTIONS IN formula thatis all four unknownsand in the bargainderivea lineartwopointintegration to exactfor cubics. ( 1 8 . ( 1 8 . 1 3 )t t b l l o w st h a t ca :61 : I resultsinto Eq.. This is accomplished variabler in a linear fashion. 1 4 ) i e l d sc s : c r . r t : cut. \ r r. .as in r:al +azxd (18. V3 is fbrmula GaussLegendre Therefbre.r1.( 18. r x +l c l x . thesevaluescan be substituted into (18.13) Noticethattheintegration and to make the fbmrulation as generalaspossible.r :0 (r8.5773503.f ( t .. ( 1 8 .. This lirnitsin Eqs.r. r7) at result that the simple addition of the functionvalues Thus.t d. C o n s e q u e n tfl iy m E q .. to give .15) (r8..r2 _ .rr. was done to simplify the mathematjcs intothis A simple changeof variable can be used to translateother limits of integration by assumingthat a new variablex. 1 8r)o y i e l d a : at + a2el) to Similarly.r6) r i r .corresponds E q . 1 3 )t h r o u g h( 1 8 . 1 6 )c a n b e s o l v e ds i n r u l t a n e o u sflo r t h e l b u r u n a cr k n o w n s .I6) arefrom .\ : .14) (r8.. t.5173503.I to l. s o l v e E q .re . F i r s t .7is relatedto the original form.l : r.1*i : c ./3/ '\'/3/ (r8. the upperlimit.this rneans o i y E q .
21) 2 into which can be substituted Eq.900(0.4x7 and dx :0. theeqtration can be substituted Equarigns transformthe integrationinterval without eff'ectively Thesesubstitutions to be integrated.1)a 400(0.900x4 400x5) + + '" ' "" 10.8.1.the righthand side is in the form that is suitablefor evaluationusing Gauss quailrature. 1 9 ) n d( I 8 .(18.+ 25r .18)to yield (b + rr) 'l (b .640533.15 is the between limits r : 0 to 0.5167ttl and at function can be evaluated The transtormed asl. atxT : . 2 3t)o y i e l d x : 0.3GAUSS s y a E q u a t i o n( 1 8 .4dxd to into Both of these can be substituted the originalequation yield r 0.305837 1.2675(0.a)r.22) a n d( 1 8 . (18. Gauss quadrafureattains this accuracyon the basis of only two function evaluarions.4r7)3 + * * [0.1) 200(0. I \4 as 0. Solution.2+ 25(0. 2 3 ) in (18. E X A M PIL E3 TwoPoint the ProblemStotement. Use Eq.516741* iu:1/J1 : relativeeror of 11. 2 0 tc a nb e s o l v e d i m u l t a n e o u sflo r s ot : blcr 2 antl a:: ba 437 (18.4xi5l0.17)to evaluate integralof + .4x. rule or a single applicationof the trapezoidal in magnitudeto a foursegment comparable because ol'Simpson's 113 and 3/8 rules.305S3T. respectively.The fbllowing exampleillustrateshow this is done in practice. To do this.(18.4+ 0. because the clever choice of Sirnpson's base poinfs.17c.However.8 I Jt) : dx n2.4* 0.4 0.r. (18. Formulo GoussLegendre 8.822578. Before integratingthe function.f(r) : 0.l 2' rl8.Therefore.4t. The exactvalueof the integral 1._ dr.1 2 to This equationcan be differentiated give bu dx .rr+ 675rr 900"ra 400. changingthe value of the integral.4* 0.QUADRATURE 18.theintegralaccordingtoEq.4 .Tlris latterresultis to be expected application of rules are also thirdorderaccurate.23) fbr.r andd.2 * 25r 200.we substitute : 0 andb : 0.I to +1.8 into Eqs.200:12 6j5x3 .llr (r 8 .22)and ( 18. .This resultis a percent which represents 1.4x.17)is0.4dra + J_l I Therefore. we must perform a changeof variableso that rz the limits arefiom .4 0.
1 8t)o y i e l d 0:a1*a:(l) Similarly.1 to 1. 1 r i : / *l.. we affive at the_interesting r : ./3 and I /V3 yields an integralestimate that is thirdorderaccurate. this r. ^.16) arefrom . Consequently c r t: c t .r as x " x: a t I a z x a If the lower limit. corresponds xt: to E q . t . i ..the twopoint GaussLegendre formula is t : f l " \ . corresponds x.. 1 4 ) o r c r a n ds u b s t i t u tte er e s u l t n t oE q .rrtx:2 pt  JI pt .t\ r l\ Therefore.14)yieldsc1. ' / 3 /l + f l ^ \'/3/ l (18.the upperlimit.16) can be solved sir. \ ' r:) xt: I ..18) 7.s o l v eE q . . j' . (  8. x : d. l. cr .rl : .436 NUMERICAL TEGRATION FUNCTIONS IN OF is all fbur unknownsand in the bargainderive a linear twopoint integrationlbrmulathat exactfor cubics. thesevaluescan be substituted into (18. f h i k n o w n s F i r s t .r7 is relatedto theoriginal form.l results into Eq. be solvedfor o ^t l '1 EXA into that.nultaneously the fourun.17) result that the simple addition of the functionvalues at Thus. ( 18.rJx:0 ) (irruT(l. 1 6 )w h i c h c a n . 11 8 . ir.r1. Notice that the integrationlimits in Eqs.l3) it follows that Eq.I 3) through( 18. t 5) gives Substituting these . rl c r r x i*]c 1 x f : J "1  . v ) /. Tlris is accomplished assuming by that a new variable in a lineartashion.0. The fbur equations be solvedare to c a* c 1 : l'. ( 1 8 .t : b..5773503.1t:= l J. ( 18.re .19) .5773503. Substitutingthis result Since xe and x1 cannot be equal.7: to 1.This was done to simplify the mathematics and to make the formulation as generalaspossible.16) for Equations(18. ( 1 8 . variablecan be usedto translateother limits of integration intothis A simple changeof variable. to give (18.( 18. : v') I /::0.f ax:O (18..neans : from Eq.13) through (18.
8intoEqs.t. applicationof the trapezoidalrule or a single compa'ablein magnitudeto a foursegment because of application Simpson'sl/3 and 3/8 rules. { .11 400x5)r/r * + '"'nl lLv'!'"\ I n. base points.23)canbe sLrbstituted anddx.4i_0.4x.x) :0.640533.:_ and ba (tZ: . Gauss quadratureattains this accuracyon the basis of only two function evaluations. 5 1 6 7 4 1 + : relativeerrorof lI.h e i n t e g r a a c c o r d i n go E q . Before integratingthe function.2.4 * 0. Formulo GoussLegendre E X A M P1 8 .I 8) to yield (b*a)l(btt)x. This resultis which represents percent a 1.i and th :O.822518.t :  2 (18.1 4 J _l Therefore.u I . transformed function can be evaluated x. because the clever choice of Simpson'srules are also thirdorderaccurate.4elxd into the otiginal equationto yield Both of thesecan be substituted n 0.200x2* 67513 900.19)and (18.4r. h e r e f b r et.14 400x5 is the between limits x : 0 to 0. (18. respectively.4x.r 1510. we must perform a changeof variableso that thelimitsaretiomlto+l.5I 674 I and at The s l t T .r 20012 6i5r3 . Use Eq.4.17)to evaluate integral + . 437 (i8.21) into which can be substituted Eq. ( I 8. 3 TwoPoint LE the of ProblemSlotement.This latterresultis to be expected of However.200(0.20)can be solvedsimultaneously Equations at : b'ta .i ) 0 .2'*.2+25rc.22)and (18.8.QUADRATURE 18.(18.2 * 25. / : I l " E a s 1 .22) to This equationcan be diffbrentiated give dx : bn d.t 2' (18..r.the righthand side is in the form that is suitablefor evaluationusing Gauss at quaclrature. ( 1 8 .Todothis.22) to and ( I 8..900(0.)a + 400(0.f (.231 yield r : 0.1: . I J1 as 0. theequation effectively transformthe integrationinterval without Thesesubstitutions to be integrated.4.305837 1.The following exampleillustrateshow this is done in practice. changingthe value of the integral. 3 0 5 8 3 7 .7)l . . The exactvalueof the integral 1.r .4d x.4 t0.1) in forrEquations \18.4* 0.' '25.l7o.)2+675Q. Solution.4+ 0. Jo : rc.r . 1 7 .3GAUSS for (18.900.4*0.1x.wesubstitutea:0andb:0.
9324695142A3152 . 1 6: 0 .trotza :1322 t3./to'({) c6:{lB"tr6ltso c j: ( 1 8 + ' t r d t s a c::lu+V. 4 T h r e e .r.5555556f(0. . Weighting Foclors co:2 co: I l Function Argumenls .3. r z . 3 6 4 7 6 1 5 2 3 0 4I8 l 3 r .3 HigherPoint Formulos Beyond the twopoint formula described the previoussection.Valuesfor c's andx's for up to and includingthe sixpoint fonnula are summarized Table 18.24) wherer?: the numberof points. 0 Truncolion Error = ro: tl : I lJ3 l/VJ 1t2){{) G) 7t1) co : 5/9 (t : B/9 cz * 5/Q : r11 nl3/5 .v 5 2 5. + curf(xnt) (18.0  /{to)16. ro : a 9324695142031 52 16. : Jili +7aJi6t35  ro:t/245+14\/7Al2l .438 NUMERICAL INTEGRATION FUNCTIONS OF TABTE 8.rr : : . / 5 2 5+ 7 0 \ / 3 0 1 3 5 r r : .higherpointversions in can be developedin the generalform I = c s f ( x 0 )+ c l l ( x r ) + .0 2 3 8 6 1 98 6 0 8 39 Z l I x::O2386l9l86083l97 = ra 0 661209386466265 xs:0. 4 6 7 9 1 3 9 3 4 5 7 2 6 9 1 c z: 0 .7 O \ / 3 0 1 3 5 x2: y'\15 7sJfr135 = 1t8){{) c.Ul /JO x o: * .I .rt : 0.:: 0 1 7 1 3 2 4 4 9 2 3 7 9 1 7 A '.1to estimate integral the for the samefunctionas in Example18. Solution. .28t3013 + 0. 1{tzr 0. in G e E X A M P L8 .:n/1[i14ffip1 u: '/ltii1tJfrlzt I 8.na) q1 PO] p c 1: 1 3 2 2 + ) 3 y r y 1 )A j c2: 128/225 q : 1 3 2 2 + ) 3 ! r y op A O c a: 1 3 2 2 .0 X2: \/ J/) =.8132444 0..1 the threepointformula is .y : li4514Jfi121 .7'74s961) + f which is equalto : / : 0.88888891'(0) 0.r:{tB. . Use the threepoint formula from Table 18.3.P o i n t o u s s .114s967) + 0.L e g e n d rF o r m u l o IE Problem Siotement.66 I 249386466265 . 1 : 0.. 4 6 7 9 3 9 3 4 5 7 2 6I9 1 c t : 0 .l 3 ! r y d / 9 O A rx:0171324492379174 t't:O360761 73048139 5 c z: 0 . According to Table 18.4859876 1.555s556 (0.t Points I 2 foctors ond function orguments Weighting usedin GoussLegendre formulos.:: 0.640533 + which is exact.
/ are parameters that yorr pass emptymatrices rol or fbr and .4 ADAPTIVE QUADRATURE 439 points requirestunctionevaluations nonuniformlyspaced at Because Gaussquadrature the functionis unknown.rrther refinementis required and the integral estimatefbr the subintervalis deemed repeated and If is acceptable.It shouldbe noted that array operators. . pl.race. tt ace to use the default . hasthedisadvantage it uses doesnot take into account that somefunctionshaveregionsof relativelyabruptchanges the where more refined spacingmight be required. fine spacingmust be appliedeverywhereeven though it is only neededfor the regionsof methodsremedythis situationby automaticallyadjustsharpchange. quad.If the truncationerror is acceptable. This function useswhat is calledLobotto quadrature. wlrere forengineering that data. and quad1. when numerousintegralevaluations I8.e is a variablethat when set to a desiredabsolute error tolerance detail to be displayed. t.botlr basedon algorithrns (2000). Adaptive quadrature ing the stepsize so that small stepsare takenin regionsof sharpvariationsand largersteps gradually. or quadl.I 8.4. for irnplementingadaptivequadrature: . . This constraint equallyspaced it that of givenfunctions. for where within the integrationinterval. The following function syntax for the quacl function is the same for the qrLadl function: g = qu. a.4 ADAPTIVE QUADRATURE the Althoughthe composite Simpson'sI /3 rule can certainlybe usedto estimate integral points. * .tcl achievea desiredaccuracy.The following section 18.rol : the a where fun is the functionto be integrated. its efficiency can be a decidedadvantage. nonzerovalue causes additionalcomputational want to passto f rrn. That is. must be performed. the no fi. b. are taken where the function changes l/3 rule to the Simpson's are Most of thesetechniques basedon applyinu corrrposite rule was usedin Rombergintegrain subintervals a mannersimilal to how the trapezoidal tion.it is not suited This is particularlytrue the function is known. until the enor falls to acceptable MMLAB includes two builtin functions to implerrrentadaptivequadrature:cluad describes how they can be applied.It may be more efficient for high accuracies and smoothfunctions. taf .and pt.it is not appropriate cases problems dealwith tabulated However. values. Thus.the stepsizeis refined the process levels.1 MATTAB Functions: quad ond quadl by developed Canderand Gautschi MATLAB has two functions. p2. . may be more efficient fbr lt low accuracies nonsmooth functions. and b: the integration (default: l0 6). This function usesadaptiveSimpsonquadrature. the 1/3 rule is appliedat two levelsof reflnementand the differencebetween thesetwo levelsis usedto estimate truncationerror. ln addition.Hence.^ shouldbe usedin the definitionof fun. rra.ad(fun.) bounds. p2. the error estimate too large.
/((x = m"yhumps(x.r.1.usingthebuiltin version of humps along with the defaulttolerance: >> format lonEt >> quad (@humps.01 (x .0.it is uselulfor demonstrating testins and like quad and quadl. r. Because resultsin efficient energytransmission. First.rmps functioncan be integrated integralof 29.r.ands:6.NotethatforQ:0. the result is now only accurate to five significant digits.r range. we can integrate it with an error tolerance of l0a as in . the computation executes f'aster.s) cl).thisisthebuiltin humps function that MATLAB usesto demonstrate some of its numericalcapabilities.440 NUMERICAL INTEGRATION FUNCTIONS OF . fewer function evaluations were made and. ar : the angularfrequency (radians/s)and / : time (s). 11. However. Next.solution i.04 s belweenthelimitsr:0tol.9.q.scorrect to . let's evaluatethe integralin the simplestway possible.q)2+ 0.ur: the peak current (A). we can develop an Mfile for the function: function y Y . l e . Hence.5 Adoptive Quodroiure EXAMPLE Problem Stotement.4 .r)2 + 0. .r:0. Notethat routines analyticallybetweenthe given limits to yield anexact the t. 6) 29.01) + l. Usequadto integrate following function: the (.9.3.^Z + O. . 1 . and . 39549861 Solution. currentin anAC it the circuit is often in the form of a sine wave: i : ipeat sin(arr) where i : the current (A : c/s). Bockground. hence. The angularfrequencyis relatedto the period r(s) by a =zrlT. 0. but using a looser tolerance and passingq. although it would not be apparent liorn a single application. The humps tunctionexhibits both flat and steepregions over a relatively short.l8. we can sojve the sarneproblem.r as parameters./  ((x r). 1 ) 0 29.85832612842164 Thirs. > > q u a d ( @ m y h u m p sO .858325 .sevensiclnificant digits.85812133214492 Notice that because we used a larger tolerance.^2+0. .04) si Then. lo. the . First.3.
is equal to about j}vo of the peak current for our assumed sinusoidal wave form..I8. However. (A).70707. although the simple sinusoid is widely employed. as the nameimplies.28) The average powercanbe determined integrating (lg.5 CASE STUDY 441 continued The power generatedis relatedto the magnitude of the current. To do this. the rms current is the squareroot of the mean of the squared current' Because1/ \/2 : 0.21)into (18. We will use both the NewtonCotes formulas from Chap.27) J . (r8. Ohm.For some of theseforms. we will calculate the rootmeansquare current of a nonsinusoidal wave form.Substituting Eq (18. Now.. i.!:= (. the AC circuit generates equivalent power as a DC circuit with a consrant the current of 1*r."./) . 2 c r \ p m Thus.26)gives (18. such as triangular or squzue waves.26) whereP: the power (W: J/s). the l*" can be evaluated analytically with closedform integration. s/Cz). lpear sln(tr. an alternative to the averagecuffent must be derived. some wavefbrms must be analyzedwith numerical integration methods. To understandthis. it is by no means the only waveform that is used.and y: voltage(V: J/C).l^^A it}ic nhanfar .2s) Thus. Therefore. : I fr.k /. which is calculated as il"ssin?(att)dt ipea. In this case study. This quantity has meaning becauseit is directly related to the averagepower absorbed by an element in an AC circuit. l'.2g) overa period with the by Eq. 17 as well as the __^^^L^^ )^^^.r.cos(2n) + cos(Q)) dt :0 T Ju Despite the fact that the averageis zero. Integration can be used to determine the averagecurrent over one cycle: . result: r E _ . electrical engineersand scientistsdetermine the root mean squarecurrent i*..slqw states that the voltage is directly proportional to the currenr: V:iR whereR : the resistance : V/A: (Q P:izR (18. such a cuffent is capable of generating power. recall thatJoule's lai statesthat the instantaneouspower absorbed by a circuit element is equal to the product of the voltage acrossit and the current throush it: P:iV (18.For a resistor.
k.22)and (18. 4 8 0 0 5 8 7 8 7 3 2 6 94 6 e .Notice that Simpson'srule is more accuratethan the trapezoidal rule.2 can be used to evaluate the integralwith Rombergintegration: >> format longi (10*exp(t) >> i2@(t) .: Io'' dr sin2nt)2 eoet (18.25 zs+o + 0. 18.*sin(2*pi*t)  .4L260804810169 0 i ra l Gaussquadraturecan also be used to make the sameestimate. The value for the integralto sevensignificantdigits is obtainedusing a 128segment rule or a 32segment Simpson's rule.25q)l'0.: lt ) zs'.r 4 4" dt:dt.0 0 8 5 Thus. Theintesral mustbe evaluated is that . We can obtain an even better resultif we impose a rnore stringent stopping criterion: r5. a changein variable is performedby applying Eqs.o (18.23)to yield 1lI t:*t.^2.41260804810169.30) Jt .25 dt / [l0e. 15 .4r'2608A4288977 1 . (18. trapezoidal The Mfile we developed in Fig.442 NUMERICAL TEGRATION FUNCTIONS IN OF continued Solution.29) For comparative purposes. (18. we obtain a result that is correct to over nine significant figures in five iterations. with the default stopping criterion of es : 1 x 106.1".2.the exact value of this integral to fifteen signilicant digitsis 15. Integral estimatesfor various applications of the trapezoidal rule and Simpson's1/3 rule are listed in Table 18.29)to yield t.First.)sinzt(0.t " 4 Theserelationships can be substituted into Eq.
163266493 ) 6178 15.a L B 2x l 0 ..2 Iechnique Tropezoidol rule I 2 4 B )b 32 o4 I?B 2 4 B t6 32 0.t : 1 I J3 and.4I2. with the resultsbeing 7.5555556(1. rms:2 .1c/o.59 4 .401429095 0.0r86 I 06x 1O3 6 48 x l05 S i m p s o n1 / 3 r u l e s TABTE18. 2 8x l O . 4 1 2 6 3 9 1 154126109 Forthe twopoint GaussLegendre formula.412568151 259x1Oa 15412605565 I6lxlOs 15.4426 0.3 15. 0 1x l 0 .5 CASE STUDY iir' rhT:' " continued for Volues the iniegrol colculoted usingNewtonCotes formulos.412607893 lOl x 106 1 54 1 2 6 0 8 0 3 8 6 . 2 2x 1 O .99182.I8. Segmenls Inlegrol e.1) 1:0.684915):15.respectively. These values can be substituted into Eq.1 1.480816629 r5415468il5 I5 412771415 l5 412618037 3\ 1763 4. 0.The resultsof using the higherpointforrnulasare summarizedin Table 18. Finally. (18.8888889(15.65755A2 r5 4058023 I 5.l"/ol TABTE18.65755 which has €t: l.6Vo. at 1/V3.684096and 4.0 r00 0000 1s.3.5 2 3 .16321)+0.313728..b 20217688657 t 5 . the integral can be evaluatedwith the builtin MATLAB function quad and quadL: >> irms2quad(i2.3 Poinfs Results usingvoriouspoint of Goussquodroture to formulos opproximote integrol. \tr.A725 l 5 4 11 9 5 8 3 6 0 4 .2 2 .4 5 6 t L9978243 15.17)to yield an integralestimateof 11.237449)+0. l"/"1 22.5555556(2. 4 2x l 0 . this function is evaluated t. . the Estimote e. 5) i.b08C1 4509 493 . which represents an error of €r : 22. The threepointformula is (Table 18.
i: where the pip meter.1. using ( twop( quad'l 18.l Use Rombergintegrationto evaluate Gaussquadrature tbrmLrla. (d) MATLAB quadfunction: and . of the n (a) Rom twopoir quad fu Iti. (18.92588945948554 This result could then be employed to guide other aspects of the design and operationof the circuit such as power dissipation computations. 2 2 4 B 1 9 4 0 3 1. and (c) MATLAB quad and quadl functions: tainedr.t rk\ Dnmhprcinreorarion :0.5a/. Hence.0547.6 Th I*^ ForT = 0 . using the result computed with quadl. t (r) i(I) Evalual ance ol formulr 18. B B 6 B 5 3 8 l3 9 3 2 5 A A maximum cuffent of 7.(). Checkthate..(irrns2 ) irms = 3. 0. Use the analytical solutionof the quadrature eration (tr :0.to I . For ex" arnple.vith Rombergintegration.. for this particular wave form. 18.8 l riod oi M .87o of the maximum.: r8. (c) the three_point (F" Ji 1: I {:."8 ] 6.88685 A occurs at t : 0.4 Tt erf(c irms2 = 1 5 .. t: I xe'd.5 Th where . 73 18. an interesting calculation involves comparing this result with the peak current./ Jt \ f: / 1\  : . we get >> i rrrs=sqrt. we evaluate the negative of the function: Use the formula error for termine followir 1./r r.25).).9 3 2 L inax = . Recognizing that this is an optimization prob lem. with quadt being a little better. As we did for the simple sinusoid in Eq.rr 0.n by merely taking the square root of the integral.7 . is lessthane. 5) 18.rr 1. r 1562.2 Evaluate the following integral (a) analytically. 11 : 6 0 8 0 4 8 C 9 9 5 7 r Both these results are very accurate. Becausewe are looking for a maximum. of Your resultsshouldbe presented 18.7 T represe PROBTEMS l8. we can readily employ the fminbnd tunction to determine this value.2917x2d. . (b) the twopoint Gauss integralto deterrnine percentrelativeerrorof theresultobthe formula.2249 s. We can now compute the i.5%). the rootmeansquare value is about 49.3 Evaluatethe tbllowing integralwith (a) Romberg intein the tbnnat of Fig. : 0.s6t6. .5%).r I to an accuracv s.444 NUMERICANTEGRATION FUNCTIONS IL OF continued >> irirs2=quad1 (i:.
which canbe decase on ' * c ( t ) .25i3). and .relaby where : velocity. rr : the initial time (min). l.7The velocity profile of a fluid in ir circular pipe can be 18./' s i n ( Z r 7J l ( t ): 0  / r\ [or0<t <T12 forTl2<t<T i(i1:5r'ls'sin2zr < f t ' t r Ot < T l 2 Evaluate 1o5 using (a) Rornbergintegrationto a tolerthe i(t) :0 for Tl2 < t < T ance 0.9.Determinethe percentrelarive to QQ):9+.but for a simple sinusoidal represented in as currenti : sin(2trt/T')where I = I s.v implementthe tunction. Determinethe f'low in the pipe if r() = Q.ll The alrount of nrasstransported a pipe over a pe.:il function. I %. (c) the MATLAB quac function.r Jn Jt sentations define the temporal variations in flow and Use (a) twopoint and (b) rhreepointGaussLegendre croncentration: the formulas estimateeri( 1.and the resistance a function of the current: via riodof tirne can be cornputedas R : l O i+ 2 i 2 / 3 = n. Theforce on a sailboatmastcan be represented the 18..5olc. The following functional represySqal :f  .PROBLEMS 18. n d.Note that flow is eqLral velocity times area.5 e .: the elevation abovethe deck and 11: the heighr of themast.) : a rd = concentration (mg/rn3).r0 : the pipe's radius. : 0. j7. L./. l8.18.and threepointGaussLegendre of 1= I s. and "vhere 18. I 7o.opoint GaussLegendre formula.1Vc: (b) the MMLAB qu.perfbrm the integrationf}onr r : 0 to 30 m.r. l3. r : radial distancemeasured u out front tionship:I/: (5i .045.tons anclthe angle is in radians. ).75 andn = J scribedby the function (a) usine Romber_e integlarionto a tolerance 0.10 Computework as described Sec.F = I too{ .. 18.0123. the (b) of tu.6x0. fbr the as but the curlent as specifiedby 'rrr: I\nu. i n ( " 4 ) qu:r.ll Perfbrnr samecomputarion in Sec. Q1r) : flow rare (m3/min).' Qft)c(t) dt Compute the averagevoltage over / : multiplesegment Simpson's /3 rule.( )5 / Z e t t ' 1 s r rvith termined MATLAB's builtin tirnction +r:f . (b) the of Ilopoinl GaLrssLegendre fbunula. E For : l. Usehetp to understand hor.. formulas.' (.i tunction. ) " " (b) AssLrrne Ohm's law doesnot hold and that voltage that and currentare related the following nonlinear. (a) Assumethat Ohm's law holdsand R : 5e. r: : rhe llnal tirne (rnin). but usethe in tbllowing equations F(_r)and d1.5).. to is l8. and c(r.:cr function.r * * The force is in new.(b) the two.l3 Supposethat the currenrthrough a resistoris demeter. Therootmeansquare 18.4r1 enorforeach based the true value.12 d(t) : 0.3r.and n : a paraItt.00055rr 0.5. theprpes centerline.12 Computethe power absorbed an element a cirby in cuit as described Sec. ancl (c) the MATLAB i ( r ) = ( 6 0.5.r i . suppose f that i (r) is definedas : t ( r ) l o d .r'3) r1r.2 r.icos21o. 18.dI'unction. 1 0 to 60 using the . to 18.r): for F(.12 0.r):1.. . = r o ( r.9 Evaluate doubleinteglal the \ / t. r1.t cr.Compute F fbr the case where I1 : 10using (a) Romberg integrationto a tolerance e.6 cunent cln tre cornputed as l_.' .r * (a) analyticallylnd (b) usinr the MATLAB dl./ ) 2+ ( 6 0.I.4 Thereis no closedfcrrnrsolution fbr the error functiol 445 whereM = nlass(mg).r2 . and (c) rhe MNLAB q:r./ Jo where.5 by Determine the mass transported between 1r : 2 and following function: l: : 8 min rvith (a) Romberg integrationto a toleranceof rH 0.
02 40. I ittllt LJo the material'sability to withstand an impact load.it is representative Vttt:.As such.n ds t r o i n d l m e n s i o n l e s s p 18.i6n) will be to curvein Fi P I 8.the 04 06 t'3 0 a2 03683 038t9 0 . A ol is the pipe's crosssectional area.I u(2rr)dr ity of an object in the dircction of a fbrce is given by JIJ llt.0 0 . Pl8.(To graspthe meaning ) 12 t. 1 0 43.IQ x. providesa measure the energyper unit volumerequired to of lfr ol causethe materialto rupture.s 0B bethis relationshipphysically. deformed.l5 The work doneon an object is equalto the fbrce times rl the distancemoved in the direction of the force. fn F. 2 0 60. Pl8. A: r12 anddA:2rr dr.0082 0 )441 tween summation and integration.1 the vr U u U 0f out The areaunderthe curve from zero stress to thepoint of It ruptureis calledthe modulusof toughness the material. The velocO .0 Ruptu re where using result 18. Io3 A o2 and be computedby Q : J u tl A.0 0.05 0 .I6 P ( o ) r o du n d eo x i o lo o d i n g n d ( b )t h er e s u l i ns t r e s s s l r c un v ew h e r e o g oi r .l7 If the velocity distributionof a fluid flowing through a pipe is known (Fig. A r is in i ) s t r e si s i n k i p s e rs q u o r e c h l 0 r l b / i n ' 7 o.I7 P .16 A rod subject an axial load (Fig.446 IN OF NUMERICAL TEGRATION FUNCTIONS where center 0. Io3 A 0 0486 0. the flow rate Q (thatis. Itl.) For a circular pipe.0 0. l( rrfi (al FIGURE I8. Determinethe work if a constantforce of 200 N is appliedfor all r.l{ If a capacitorinitially holds no charge.as shownin the stressstrain r FIGURE I8. I 6b. 1 5 52.the voltage acrossit as a function of time can be computedas u:4t u:16*(4r)t 0<t<4 4<t<14 Al I I where r is in m/s. llt.0 0 .Usenufol If C: l0 s farad.l7). 2 2 8 2 volume of water passingthroughthe pipe per unit time)can i. I 60.18 stretcl tor: M o d u l u so j t o u g hn e s s F. the stressstrain a plot of voltageversustime: Itt. Therefore. where u is the velocity. Pl 8.recall the close connection i.25 55.use the fbllowing currentdata to develop mericalintegrationto computethe modulusof toughness curve seenin Fig.
q : fuel consumption rate. 0 the trapezoidalrule.If I : 1800m/s. 18.20 The upward velocity of a rocket can be computedby the fclllt>wine tomula: r. I 8.30 0 046 0 15 0 13 035 N 4 t03. L 0<l<10 l0 < r < 20 2O<t<30 I d.I to 2 to 2. . \.s .000 and q : 2500 kg/s.r : .) .PROBLEMS r where is the radial distancemeasuredoutward from the ol is center thepipe. \ r .r 0.18 a spling that has a springconstantof ft : 300 N/nr slretchrng : to. rrf.determine kg.21 ]'he not'rraldistributionis definecl as . r .22 Use Rornbergintegrationto evaluate ^)/ . how high the rocket will f1y in 30 s.\to an accuracyof e.3 cm).57c..andg : downward acceleration gravity (assumed of conslant: 9.l 0 3 . \l'6 ltt.19 vertical velocityis given by the r':lll\1 (a) Use MATLAB to integrate this function fiom.35m: F . : 0.= 1 1 0 0 .^ E:e\t2 \/ L]T (in where is the total ladir"rs this case..N rrf.rno.8 m/sr).. conrpute r.tno qt / whererr: upwardvelocity.5 r r'=50I+2(t20)) . : velocityat which fuel is exrr pelled relativeto the rocket.Your results should be presentedin the form of Fig. : 160.If the velocilydistribution given by 447 18.calculatethe work done by 18.rt : t.initial massof the rocket al time / :0. Usingthe following data. / t . Discussthe using multipleapplication results. Evaluatethe vertical dislancetraveledby a rocket if 18. n./ / . 0 0 0 063 020 00r 005 O C)82 025 0 028 o l0 0ll 0.=lIl]l \ / .€'sln\ Jo l +.r r l n { "' ' . I and frorn (b) Use MATLAB to determinethe inflection points of this function. 18.
we usedcalculusto integratethis equationto determine the vertical distance the jumper has fallen after a time /.rtour YOU'VE GOT A PROBTE'I/I R::*. 17.Differentiotion Numericol CHAPTER OBJECTIVES The primary objectiveof this chapteris to introduceyou to numericaldiftbrentiation.*::TJ u(r ) velocity of a freetalling bungeejurnper as a function of time canbe At the beginningof Chap. Knowing hclwto evaluatederivativestbr unequallyspaced data. Recognizingthe sensitivityof numericaldifferentiationto dataerror. Understanding how Richardson extrapolation appliedfbr numerical is differentiation. Knowing how to generate plots and vector fields with MATLAB.) (19. col. Knowing how to evaluate derivativesin MATLAB with the dif f and qlradient functions.2) . Specific objectivesand topicscoveredare ' ' ' ' ' ' the Understanding applicationof hishaccuracy numericaldifferentiation lbrmulas for equispaced data. z z(t.
thereare other funcfor tions that may be difficult or irnpossible differentiate to analytically. Because is the init verseof intergration. you were askedto dethat you were given the reverse termine velocity basedon the jumper's positionas a function of time. According to the dictionary definition.) A. . To do this. . 19. depicted Fig. differentiationcould be usedto make the determination: r'(l): dz0) ."Mathematically. distinguish. Standingat the heartof calculusis the mathematical conceptof differentiation. In numericaldifTerentiation methods are availableto obtain solutions. resultwould be the a(t) :S . I9.7) .) Ar (1 9 .it would be usefulto differentiate discrete the datato determine the velocity and the acceleration.4\ In eithercase. or the secondderivative of displacement: du(t\ .asoccursin moving from Fig. wouldbring us backto Eq. 19.If A:r is allowedto approach zero.1a to c.2)into Eq.suppose that there was some way to measurethe jumper's position at various times during the fall..t. that calculusis an essential tool of our profession.3) Eq.y A. at 119. we could either take the first derivativeof velocity.t\i/::: d 2 z t tl dt dt2 (19. the difference a becomes derivative: ' dr  {r+0 lim . which servesasthe fundamentalvehicle for differentiation.ftt ( #. Substituting (19. you might also be askedto computethe jumper's acceleration.l. derivetthe in the tive. to perceive difference or between.I INTRODUCTION BACKGROUND AND 449 Now suppose problem.". I Whor ls Differentiorion? of Calculusis the mathematics change. both theseinstances. (19.In this situation. 6 ) where y and /(:r) are alternativerepresentatives the dependent variable and x is the for independent variable.) (1 9 ./("r.1).I9. Thesedistances along with their associated times could be assembled a table of discrete as values. represents rate of change the variablewith respect an independent to of a dependent variable.3)anddifferentiating Beyond velocity.I INTRODUCTION AND BACKGROUND  9. 5 ) Although a closedformsolutioncan be developed this case.r f (xi + Ar) .That is. This chapterwill introducevou to someof thesemethods.r (t9. ( 19.  ./(. to dffirentiate means"to mark off by differences.f (xi + Ar) . the As in mathematical definitionof the derivativebeginswith a difference approximation: A. Because engineers scientists and must continuously deal with systems and processes clrange.Further.
A s s e e n i n t h e v i s u a l d e p i c tF og .o9 . the #:*(#) .9) Similarly. Todav. are on Partialderivativescan be thoughtof as taking the derivativeof the functionat a pointwith For example. . I Thegrophicol zero opprorimolion defrnition o derivotive: Ax opprooches in goingfrom(o)to (c). + ar AI (b) (c) F I G U R EI 9 .f that yields elevationas a function of longitude(the eastwest oriented 'The fbrm dt'ldx was devisedby Leibnitz.r. . the secondderivativetells us how fast the slopeis changing.It is commonlyrefened to as the cun. the partial derivativeof. 1..ri) ]' is the first derivative .r+o ( 19. on y. r .l(.{ at an arbitrarypoint (x./(./with respectto y is definedas a" f. f(x) r.t') .given a function/ that depends both . .8) Thus.r'with be oS of r e s p e c t t o .r ' * A . . o whereh ldx [which can erlso designated . y' Note that Newton used the socalleddot notation: i. r ' ).partialderivatives usedfor lunctionsthat depend morethanonevariable. the partial derivativeof/with respectto. t h e d e r i v a t i v e i s ii n l f the slopeof the tangentto the curve at.rr./ ( x . Ar.r Ax ( 19. r') A.r.ature.because high value for the secondderivativemeanshigh curvature.l/ o. whereas is attributedto Lagrange./(r * A. a Finally.r.the dot notationis usuallv usedfbr time derivatives.recognizethat a function that depends on you aremountainclimbingand have two variablesis a surface ratherthan a curve. . l ' ) Ay (19.r e v a l u a t e d a t r .r all but one variableheld constant. The secondderivativereDresents derivativeof the first derivative. r0) To get an intuitive graspof partial derivatives.f '(.l i m 3y Aro / ( .thedifference of os becomes derivotive.r.Suppose to access a function.450 NUMERICAL DIFFERENTIATION + . l c .)is detined and as af 3.
depiciion o temperoture of Becouse moves heol fromhighlo low lemperoture.andthe slopeto the noth would be 8/(xo.Many specificexamplesof suchapplications could be given in all fields of engineering and science. negolive o grodient leods c positive to flow. f : (K). K)1.rn.Fourier's law oJ heat conductionquantifiesthe observation that heatflows from regions of high to low temperature.I9.Thisis iheorigin lhe"minus in Fourier's of XeotcJnduction. Differentiationis commonplace engineering science in because much of our work involves so and characterizing changes variables both time and space. r' r'e). For the onedimensional case.this can be expressed mathematically as Q: . which is not couchedin termsof thepositionof an objectbut ratherin its change with respectto time.2).providesa measure temperature . andx : distance (m). of theintensityofthespatial temperaturechange. 19. y6)/3"r.K. d T .) I 0.wherethepositrve in lo grodlenl leods o negotive flow fromright left heot to Directionof heatflow . reverse of low The sign coseis depicied {b).' ax ( 1 9r ) r. A prime exampleis Newton's second law. the slopeto the eastwould be 0f(xu. numerouslaws involving the spatialbehaviorof variablesare expressed terms of derivatives. fact. where q (.I INTRODUCTION BACKGROUND AND 451 .r) : heat flux (Wm2).many of the laws and of tlre in In other generalizations that figure so prominentlyin our work are basedon the predictable ways in which changemanifestsitself in the physicalworld.1.2 Differentiotion in Engineering ond Science The differentiationof a function has so rnany engineering and scientific applications that you wererequiredto takedifferentialcalculusin your first yearat college. 19. Thus. For example. in Among the most colnmon of thesearc the cortstitutive /au's that define how potentialsor gradientsinfluencephysicalprocesses. slope negotive thiscose.r.2 I "downhill" Grophlcol grooient. to theorientotion Corteiion ihu due of the coordinoies. is for Thus.r axis) and latitude(the northsouth oriented axis).y. Aside from suchtemporalexamples. k : coefficientof thermalconductivitytW(m . FIGURE9.the derivative or gradient. flo* in {o)is fromleftiorlght However.whichdrivesthetransferofheat(Fig.If you stopat a particularpoint (.
e.ln addition. masstransf'er. PhysicolAreo !eot conduclion Grodient [:mperoture Flux Heotfiux Proportionolily Thermoi Conductivily Drru:iu] d7' lcw Fick's D'Arcy's ow low Ohm's Newion's viscosily low flooke'sow I  dc a. (Table19.Thislevel of .r  t^ du .1). Iu Heod For'vflux Currenflux t ic Hydrou Conductivi\ Elecfricol ConductivilY _^ dv _ d.andcentered tives.452 DIFFERENTIATION NUMERICAL TABTE 9. have alreadyseenan examplein the fbrm of Euler's methodin Chap.4.at best. we developed difference approximations of tirst and higher derivatives. These arejust a few of the applicationsof differentiation that you might faceregularly you in the pursuitof your profession.K *. When the functionsto be analyzed simple. However.2 HIGH. to obtain approximate usingnumericaltechniques described as EXA r9.For both these you rnust fined only by measurement discrete at cases.In Chap.v Theonedimensionol of someconstitutive commonly in forms lows used e n g i n e e r i no n d s c i e n c e .dh dT Mossdiffusion Flowthrough porous medicr flow Currenl Fluids Eosticily Cor. and elecincluding the modeling of lluid dynurmics.4. willnorare mally chooseto evaluate when them analytically.The ability to accurately is derivativesan tricity.backward.r . Recall tha we employedTaylor series expansions derivefinitedifference of to approximations derivaforward.22. g Equotion q : . Beyond direct engineering and scientificapplications. I Low Fourier's lor. numericaldifferentiationalso is importantin a variety of generalmathernatical contextsincluding otherareas numerjcal of methods. their enors were proportionalto the squareof the stepsize. addition.probablythe mostimportant of appliWe cation of numericaldifferentiationinvolvesthe solutionof differentialequations.the underlyingfunction is often unknown deIn and points.For example. and solid mechanics estimate importantfacet of our capabilityto work effectivelyin theseareas.rorio1 Mc. In Chap. ax Velocity Sheor Sfress Siress Dynomic Viscosily Young s Modulus o : EL! L Deformction Sirnilar laws prcvide workablemodelsin nranyotherareasof engineering science.wewillinvestigatehow numerical differentiationprovides the basis for solving boundaryvalue problemsof ordinary differentialequations.ACCURACY DIFFERENTIATION FORMUTAS We have already introduced the notion of numerical differentiation in Chap. 6 the secantmethod was basedon a finitedifferenceapproximation the derivative. Remernber that.recall that in Chap.theseestimates errors were had that O(h2)that is. 1. next.. chernicalreactionkinetics. have ability the valuesfor derivatives.it is oftendifficult or impossible the function is complicated.
19.. H i g h .2 HIGHACCURACY DIFFERENTIATION FORMULAS derivationof thesefbrnrulas.Gi):W+o&) i n t oE q ..13)] f"!:t) ^z *.2 f ( r . The resultsare summarized at r : 0. Recall that in Exarnnle4.the forward Taylor seriesexpansion can be written as [recall Eq.f'(x)h * which can be solvedfor (1e. l 9 t. 19. / t x .I 9. Fig. ( 1 9 . 4.. 5 .15) ( l s . r ' 0 . h+Otlr) .emolov hut formulasfrom Fis. . (a.0 .x):ry+o(h2) (r9..1 ) + . we truncatedthis result by excludingthe secondand higherderivative tenns formula: and were thus left with a fbrwarddifference + o(h) (r9.12) f'(r): {!tP f'(.0 . (4.27)]: the following forwarddifference approximation the second of .5 using finitedifferences in the following table.lthroughFig. zn' or. Note that the errors are based on the true value of /'(0 5) : 0'9125' Bockword Centered Forword I o(h) Estimote o(h\ a 934 a/a o(h) t 155 a^ qa/ 4 714 Renear fhis comnutation. by collectingterms: (19. ) . 19. ing exampleillustrates utility of theseformulasfor estimating the E X A M P TlE . to Notice that inclrsion the secondderivative of Similar irnprovedversionscan be developedfor the backward and centeredformulas as in of The formulasare summarized well as for the approximations higherorder derivatives. 2 5 x* 1 ..4 we estimated derivativeof f ( r ) : . 1 5 x ' . 2 and a step sizeof h: 0.f. wrll now illustrate We how highaccuracy finitedifference formulascan be generated includingadditionaltermsfrom the Taylor series by expansion. 1 .3throueh .): tSt#  +r+ o(h2) (19. now retain the secondderivative we term by substituting derivative[recallEq. r *. f (ri+t) : f (xi)+ . The followderivatives.A c c u r o c y i f f e r e n t i o t i oF o r m u l o s D n the Problem Stotement.5along with the lowerorderversionsfrom Chap.4.1 6 ) .r7) term hasimprovedthe accuracy O\h21.f'(.r4) In contrastto this approach..13) In Chap. 1 3 ) o y i e l d t J l x i + z t.0 .25. For example.
6 3 6 3 2 8 1 f ( .0 .4f (t.9t5) r) : . moreoccurole.) h2 .+r)* 6/('r.2 4 f ( .925 .consequenlly. + + ).ri : 0.f(r. 2 . * ) + l l / ( x . ) 3 / ( . 2 Theforwarddifference accuracy (h2)is computed (Fig.1 24f $i+2)+ iB. 2 + 8 ( 0 .878125 €.)+ 2f (x. Solution. r n 1 * 1 4 .19.2f (x. r i + ): 0 . * .5 ri+r :0'75 li+l : I f(ti):1.) h2 o(h) o(h2) f (xin:)+ 4.n.3 Forwordf initedifference formulos: versions presented eoch derivotive.) + ) . l .*. lotier hryo ore for The version incorporcles more terms the icyior series of exponsion ond rs.25 .) = . The data neededfor this exampleare xi2:0 xi.) * f (. l \xil: Enor 0(h\ oftll f (r.454 NUMERICAL DIFFERENTIATION  F i r s lD e r i v o l i v e f ( xi + t t f l x ./(.is computed (Fig. ) ( + Ita FIGURE I9.4l'6i+) + f (ri) f"" (x.*) .r :0.*') .2 + 4(0.)= h1 o(h) 0T'?) f "" (r. x i * ) * 2 6 7 1 .3(0. 1 0 3 s 1 s 6 1 . f ( x . r .f (r.5.19..*21 4f (xi+) .rr*J . 8 2 V t 2(0j.*r) .2 f(xit): 1.5) 0. f ( x i + r ): 0 . 6 3 6 3 2 8 18 ( 1 .3f (x) + 2h Second Derivotive f (.3) of O as : / (0. 19..+r).) l ln' FourthDerivoiive f (x.+1) 14f (x. 2 .1"' ThirdDerivotive (ti*z) * 3/(. 9 2 5) 4 ( 1 1 0 3 1 5 6+ 1 .) f @i*) 3f h3 0(h) o(h2) 31 (1.5) as .636328. f x .5"f (. :s ) l ( 0 .:3.4) of O as / t t . 5 ) : 0.r.1035156 J'Gi) :0.2 . 8 5 9 3 7 5 € r: 5 .t.llVt 2(025) Thecentered difference accuracy of Otha.) = (x.0 .r.*. Thebackward difference accuracy \h2I iscornputecl(Fig.f (xi+) .
h'1) f"'( r'. 18. However.)= t 5.v : 0.r8) /r1 Because usingtwo stepsizes: and /22.rzo Bockwordfinltediiference more occurote exponsion ond is.f (r) .f (xi_t) /(r:. t._) 3f (.r. provideda means obtainan to extrapolation Recallfrom Sec.._. centered more accurate the ferenceyields the exact derivative at . as in 4 .t+ 6f (r. derivativeestimates computea third. I : I ( r r : )* .18f(x.'r) f"(xi)= h2 o(h) o(h?) . \ n Lt1 o(h) o(hr) f ' ( .l l \. of wlrere/(ft1) andI(h) areintegralestimates when expressed a computeralgorithm.. corrsequently.) + 3f (xi.3 RICHARDSON EXTRAPOTATION when To this point.4) .r.r). This is because tbrmula basedon the Taylor seriesis equivalentto passinga fourthorderpolynomialthroughthe datapoints. more accurate.f (.J + J(.4)] improved integralestimateby the formula [Eq..f (xir.2) . ) 3J(.t ._r) lr' 3"f(. cre for Thelotier h. l r t t t l .the errors for the forward and backwarddifferencesare considerably the difthan the resultsffom Exanrple4.4.r t h l \ l (te.s) 1""(t) = o(h) o(h2) FIGURE I9. . we have seenthat there arc two ways to improve derivativeestimates (l) decrease step size or (2) use a higherorderforrnula the employing llnite diff'erences: extrapolation.2. usestwo basedon Richardson that eniploys more poiuts._r).19.surprisingly. this formula is usually written as its convenience for the casewherehz : ht 12.{._)+ 24f (.24f (ti ) + I I /(.\i).i that Richardson ( I 8. ...) h7 3.ttt/tt)).I 4 f (x.3 RICHARDSON EXTRAPOLATION Firsl Derlvotive +rr\ J \ i'  Error frY J '4i t.r. to approximation.resenled eoch derivotive.r. versions r.2f(x.2f (.\i 1) : Second Derivolive ._r) 14f (.f(il .) + 26 f @.f"(r') : 2f (x) .4 formulos.A third approach..) 2h3 Fourth Derivotive * f lt.4f rr.) .) * f (x._.5/[r'r) * 4f (x. I9.t. versionincorpoTotes more ferms ihe Toylorserres of As expected.:) hl Ihird Derivoiive o(h) o(.4.4f (r.r.5.).t .l (r.
4f (xi_)l . f( x i _ z ) * f ( x .*2) + B/(iri+r) . 1 i a a ^ n a o ^ ' a n l l r r m n r o occurote..1 _f (t.: *1.t.n) + 6f (r._. f ( r i + r ) + B / ( x i + 2 ).5): :.*) .. 6 3 6 3 2 8 1r .20) For centered difference with O\h2). is0.*r) * 2.. r . r : 0 . _ ) .1 .2 1 Richordson xtropolotion E estimate first the Problem Stqtement. (19. 8ft3 Fourth Derivotive f (r./(. z) l2tt2 Third Derivotive f (r.47a .( 1 9 .r ) .)= f"'(x) = oft'1) 0(hn) 2hl .. version more incorporotes terms theToylor of series exponsion.) + l (.) + f \x.+2i\ 39/(. Using the samefunctionas in Example19.) * 3O/(ri) + 16/(:r.1.B . ) h2 0(h') o(hn\ f" (x): f (xi*) + l6/(.1 3 1 1 " r .xi) . of EXAMPLE 9. . can as estimates be computed with centereddifferences Solution.39/(r. 1 0 3 5 t 6 0.5 €r : 2. 2..1) I ltr Second Derivotive f t.f ln Error (.r. o(h'1\ o(h') f (x. 2 0 ) a e s o to computean improvedestimatewith Richardson Recallthatthetrue value extrapolation.f (t+) + 2f (xi r) .0 tt : *9.f(x_r) 1f (xi_z) + + 6h1 rvvg ['^rled'{Ie'ence fo'rnulos: ve s onso'e oresetred eochderivotive. lofier Cenie'ed for The ^.). T h e nu s eE q . 5 a n dh z : 0 .67c I and : D(0.t. Eq.25) 0 . ). In a similarfashion.+t) 2f $.) f "'(x.r) + 12.5 ( I9.f (xi z) f"" (x) = h4 oftI) 0(ho) : f"" (x.4f {x. der i v a t i v e t .5 m p l o y i n g t e ps i z e s f h 1 . +) l 3 / ( r r . The firstderivative 0 .o.934375 : 0.19)can be written fbr derivativesas 4l D::D(h:t_:D\ht) 1 )J TIGURE I9.Bf (.).r.0 . * . 2 D(0.9125.) .*.r. the applicationof this formula approximutions will yield a new derivative estimate O(h+).) 12f (x.f (x.rr+r)+ 56f (x. 2 5 .456 DIFFERENTIATION NUMERICAL FirslDerivoiive f ( x .
21)evaluated t : xt redifference In fact. For the finitedifference l o Sec.934315) 0.t o .25)1. Although this equationis the frorn Fig.2. it has some importantadvantages. I r ) . . 19. a only in cases In contrast.5. to One way to lrandle nonequispaced data is to fit a Lagrangeinterpolatingpolynomial the to [recallEq. ( 19. for equispaced at t&q.21) wherer is the value at which you want to estimate derivative. r t i ) . (15.25\.. r o ) ( . 19. ducesto Eo. e v e n l y s p a c e d .ernpiricallyderived informationthat is. F o r t h e R i c h a r d s o n e x t r a p o l a t i o n t e c h n i q u e9 . points thernselves not have by the do is to be equally spaced.you can fit a secondorder Lagrangepolynomialto threeadjacent ( . the derivativeestimate of the sameaccuracyas the centered points.f f e r e n t i a t i ntg ep o l y n o m i ay i e l d s : ( l D h l'(x) : l'(re) (xo 2.4. .the datahad to be of a given function. 4I D:4(0. it can provide estimates anywhere within the rangeprescribed the threepoints. r . data from experiments ficld or studiesare often collected at unequal intervals.. r g .r s ) ( x 1  12) * lGz) ( . t h e d a t a a l s o h a d t o for halvedintervals.20) give can by to J(l): t w h i c h l ' o rt h ep r e s e nc a s ei s e x a c t ..r1 .f (rr). most otherfunctions.Remernber that this polynomial does not require that the points be equispaced. r i . .Such intormation cannot be analyzed with the techniques discussed this point.of course. r 2 ( x r..r. ' z )i.4 DERIVATIVES UNEQUALLY OF SPACED DATA The improvedestirnate be determined applyingEq.Second. 2 x .x.Consethis would not occur. Suchcontrolofdata spacbe evenly spaced and generated successively ing is usuallyavailable wherewe canusea functionto generate tableof values. 19.3through certainly more complicatedthan the firstderivativeapproximation Fig. 19.as was the casefor the applicationof Richardson extrapolation. r u . approachcan the be appliediteratively using a Rombergalgorithmuntil the resultfalls below an acceptable en'or criterion. the presentcasematched polynomialprecisely. points For example.21)J a setofadjacentpointsthat bracket locationvalueat which you want to evaluatethe derivative.t2 xr )(xe . r :. a n d ( .9125 457 The previousexampleyielded an exactresultbecause function being analyzed the was a fourthorderpolynomial. Thus. (19.I9. polynomial can thenbe diff'erentiated analyticallyto yield a forThe the mula that canbe usedto estimate derivative.The exactoutcomewas due to the fact that Richardson extrappolynomialthroughthe dataand then olation is actuallyequivalentto litting a higherorder evaluatingthe derivativesby centered divided differences. the derivativeof the foLrrthorder For would be irnprovedbut not exact.4 DATA DERIVATIVES UNEGIUATTY SPACED OF The approaches discussed this point areprimarily designed determine derivativeof to to the approximations Sec.l r rr) (le.f3 .) 2xrgx1 t .and our derivativeestirnate quently.9. First.Eq. Third.
133. 6 01 K(133. (19.333 . 5 . drl I a_ l.a temperature graclient be measured can downinto the soil.5W k (m .3 DifferentiotingUnequolly Spoced Doto Problem Stotement.012s) : __1140 1440 133.0125.)(0 ffi z(0)00.5 DERIVATIVES AND INTEGRATS FORDATAWITH ERRORS Aside fronr unequalspacing.0 . 19.l): q(::0t:ft.=u whereq (r) : heatflux 1W/m2). Equation /'(0) : 13. : coetllcientof thennal conductivityfbr soil [: 0.0t25 (0.A shortcomingof numericalcliff'erentiation . I : temperature (K).0375 2s + .6 Tempercfure versus depth info the soii I9.5 (0 +10 2(0) 0. As in Fig.0375) t2 0.037s 0)(0.458 NUMERICAL DIFFERENTIATION EXAMPLE I9. The heatflux at the soilairintert'ace be computedwith Fourier'slaw (Table can l9.0.0r .0375 2(0)00. Use numericalditTerentiation evaluate gradientat the soilair interface to the andenploy this estimateto determinethe heatflux into the ground.l13 _J FIGURE I9.6. &rd 1 : distance measured down fron the surface the into soil (m).anotherproblernrelatecl diff'erentiating to ernpirical is data that it usuallv includesmeasurement error. K)1. Note that a positive valuefor f'lux rneans frorntheair tothe that heatis transferred soil. : 6 6 .21)canbeusedtocalculatethederivativeattheairsoilinterfaceas Solution.333 : K/m + whichcanbe used compute to w/ K\ w q ( z: 0 ) : .0375 0.0.
7c.suppose that we want to determine partialderivatives a to for .7cusesthe samedata. 19.7b). datais to useleastsquares to ditferentiable functionto the data. However.7 I (o) lllustrolion how smcrLl errors omplified numericol of doto cre by difiereniioiion. This minor modification is barely apparentfiom Fig.7ttshows smooth. is The error arnplificationoccursbecause differentiationis srrbtractive.In essence.6 PARTIAL DERIVATIVES 459 I 4 dr 4I dl FIGURE 9. dotc modified (cJ resrlting differenfiotioncurve mcnifesting of irrcrecsed ln voricbility controst.randompositive and negativeerrorscancelout. fact thatintegration a sumrring the ib process makesit very forgiving with regardto uncertain data. 19.the resulting in etfbct Fig. r9.but with alternating points raised and lowered slightly. 19.a lowerorder polynomialregression of might be a good first choice.6 PARTIAT DERIVATIVES Partialderivatives alouga singledimension computed the samefashionas ordinary in are derivatives. Fig. the truefunctional if relationship between dependent indethe and pendentvariableis known. As might be expected.errorfieedata that wlren numericallydilferentiatedyields a smoothresult(Fig.In the absence any other information. 19.I 9. For example.7tl significant. oolo Fig. ootc with no (b) numericol differenfiofioncurve (cJ error. Hence.f frorn to (c)by toking oreounder tends ottenuofe operolion integrotion the to Imoving or srnooTn errors. 19.Incontrast.In contrast. theresuhing of slightly. primary approach deterrnining the fbr derivatives imprecise tbr regression fit a smooth.random positiveanclnegirtive errorstetrdto add. reverse the (dJ (d)] o. (d) the ond {o). points lre as summedto fbrm an integral. this relationshipshouldlbrnr the basisfor the leastsquares fit.Obviously.
can first form a diff'erence .r yieldsthefinal result Collecting terms (1 9.26) atJ 0x01 * /(r * Ax. miehtwant to differentiarte function with respect For two or rnore ditl'erentvariables.7./(x.22\ (19."/("r.t') "f /(r*Ar.u) 2A.) \' I Ar. r '.1 MATTAB Function:ai. . y ) l ( r . we rnight want to take the partialderivativeof/(r. v * A y ) .).t' . [ drrd I' ( l 9.l.A .f(r + A.r) with respectto both independent variables #:*(#) ( t .21) we in approximation. I * At') l(:r f Ax. r ' .r (. described the of length zr .thesecan then he employedto determinefinitedifterence t i o n so f f i r s td e r i v a t i v e s . 2 5 ) Then. we can usetlnite differcncesto evaluateeachof the partialsin r': .r')  af .v* A. example.A y ) l(x * Ax.r .r of theparTo developa finiteditference tial derivativesin r': ja tf ( .r') /(. t0 For higherorcler we a derivatives.The result is called a ntixedpartiaLderivcttive.27) WITH 19.rAy (19..Af') . r' 2Lx (1 9 .23)  l(rA". r .. : 19.A .r.l' * A.A x .A .A.460 D N U M E R I C A LI F F E R E N T I A T I O N with centered approximated diff'erences: Af i. .y) .r.t .tr vectorof lengthn./ ( x . the di f f function retunlsa vector When it is passed onedimensional a in As betweenadjacent elements. r ' ) 1 (r i..f (ir d 1' 2L.I containingthe differences approxirnafbllowing example.r. ...r) 2Ar discussed this point can be appliedto evaluate to All the other fbrmulas and approaches partial derivativesin a similar fashion.A.. I _ 2Ar lAr' a2.7 NUMERICAT DIFFERENTIATION MATTAB the on MATLAB softwarehasthe ability to cletermine derivativesof databased twobuilti n f u n c t i o n sd i f f a n dg r r a d i e n t .A r ' ) 4A.A .r Af dl' EX (19.r'* A1')...y.
/('T > > f = G ( x ) a . Explorehow the MAILAB dif f functioncan be employed differentiate the function .1000 0. >> Yf (r) . 4 E Usingit i r i for Differentiotion to ProblemStqtemenf.we merely perform a vector of computedivideddifference by by division of the y differences the x differences entering >> d=dif f (y) . I to The vector d now containsderivative estimatescorresponding the midpoint beTherefbre.8. result represents differences approximations the derivative.8900 Columns 1 through 0. ^ 3 9 0 0 * x .1 :0.0100 5 3.we could have simply performedthe abovecomputationconciselyas >> d=diff(f(r))i0.1000 0. elementsof betweenadjacent the The dif f function can be usedto determine differences each vector.3600"t3 2000.1000 0.400x2+ 202512.2 0 0 * x .I9.1900 B 21 .1000 betweeneachpair of elementsof x.For examPle. We can first express ) as an anonymous . valuesof the independent and dependent a We can then generate seriesof equally spaced variables: >> x=0:0.1000 Columns 6 through B 0. 1) +x(2:n) J . ^ 4 + 4 0 0 * x .3900 Note that becausewe are using equally spacedvalues. . 2 + 2 5 * x . /drf f (x) Columns 10. Compareyour resultswith the exact solution: f '(x) : 25 . ^ 5 .14 * function: Solution.8. >> diff (x) Columns l througth 5 0.after generatingthe x values. tween adjacent generate vectorholding the x valuesfor the midpoint of eachinterval: a >> n=Length >> xm=(x(1:n (r) .010A 1.6900 6 through 11. /2.31"00 8. To the the As expected.900x4 + 400r5 l (x) : 0.1000 0.1..2 WITH DIFFERENTIATION MATLAB NUMERICAL 461 E X A M P L1 9 .2 t 25x 20012l61513 from x : 0 to 0.in order to developa plot of our results.4900 8.we must first elements. " 2 + 6 15 * x .1000 0.1000 0.
:. r .the following code rejectsa vector that is not in ascending (ntonotoniorder ""'t.i .we can computevaluesfor the analyticalderivativeat a finer levelof resolution to includeon the plot for comparison.n. end Another common useis to detectwhethera vectoris in ascending descending or order. error ( 'ur.):a. E .8 (line) (circles) Comporisonof the exoct derivotive wiih nunrericol with esiimofes compufed MATIAB'sdif f function. C l : .8. r . > .' >X a = O :. erro](. . ^ 24 * 9 0 0 * r . E) As a final step.:f.the results lavorablyfor this case.t:t:::::*. A plot of the nurnerical and analytical estimates be generated can with >> plot (xm.462 D N U M E R I C A LI F F E R E N T I A T I O N FIGURE I9.the dif f function comesin handyasa progranming tool fbr testingcertaincharacteristics vectors. compare Note that asidefrom evaluatingderivatives. ' a = 2 5 4 0 0 * x a + 3 * 5 7 5 * x . a . ^ 3 + 5 * 4 O O * x a^ 4 .r . 19.f.. following of the statement displaysan error nlessage and terminates Mfile if it determines a vector an that :i has unequal spacing: if any (drf f (drf f (x) )=91 .d. For example.For example.r.ya) in As displayed Fig..iequal spacing' ) .'o'..
it doesso in a mannerthat is more compatible with evalr.1500 5.0.1 . ^4.'.E = gradient ( f..A simple representation its syntaxis of 'x 9.'l ('' \/ectorof lengthn.functionalso returns differences.^ 5 .a cenferencebetweenthe first and secondvalue.the following versiondivides all the resultsby the interval and hence returnsthe actualvaluesof the derivatives.we can generate seriesof equally variables: and valuesof the independent dependent spaced > > f .. 1 : O .7.4.8400 21.4with the di tf function. >> !a=25 .riii+rL for Differentiotion the that Problem Stotement. Just as with the dirf function.5900 16.44C\0 10.1 0*xa+3*675*xa.0400 1. .1) ^.^2 0 r.l9. Z N U M E R I C A LI F F E R E N T I A TW I NHM A T L A B D IOT 19.3100 8. f. ^ 4 + 40 t J * x .. . a Solution.5900 As in Example 19.2 0 O * x . 4*900*xa. We can then use the graclient function to determinethe derivativesas >> dv_sradient1y.4. In the samefashionas Exanrple19.8100 5. Use the qiradient tunctionto diflbrentiate samef'unction we analvzedin Example 19.'o'. we can generate values for the analytical derivative and display both tlre numerical and analytical estimates on a plot: >> xa=O:. the flrst value returnedis the difvalues.28) dtlIi : 2 The last value is then computedas the differencebetweenthe final two values.89t)0 Columns 6 through 9 4. .dy.01:. for the intermediate valuesis returned tered dif'fbrence basedon the adiacent .l. Y I rl.2 MATTAB Function: sradienr The gradienr. h) where h : the spacingbetweenpoints.fi r (19.5 EXAMPLE Usingu.tr .I 9 . ''3+5*400*xa.r. and fx is a vectorof lengthn containing where f : a onedimensional differencesbasedon f. If the vector represents equally spaceddata. However.rating derivativesat the valuesthemselves rather tharr in the intervalsbetweenvalues. ^ 2 + 6 15 * x . > > x = 0 : 0 . 2 + 2 5 * x .Hence.8.the values. Note that the spacingbetweenpoints is assumedto be one. Columns 1 through 5 5. ^ 3 9 0 0 * x . However.B .with forward for differences all the intermediate resultsare akin to using centered and backwarddiflbrencesat the ends.ya) >' plot(x.@( x ) A .
and .h : the spacingbetweenpoints. the function can be invoked as I fx.corresponds to to the ditferencesin the y (row) direction.464 NUMERICAL DIFFERENTIATION 25 20 10 FIGURE I9.4. Beyond onedinrensional the gradient functionis particularly maFor example. h) where fx corresponds the differencesin the r (column) direction. f.we will illustratehow gradient can be usedto visualizevectorfields. of trix.This is due to the f'actthat gradient ernploys that aretwo times(0. fyl = gradient (f.the rcsults arc not as accurateas thoseobtained intervals dif f function in Example 19.1).2)as wide as for thoseusedfor dl rf (0.the spacingbetweenpoints in both dimensionsis assumed be one.and 4.9 (circles) (line) with wiih numericol eslimotes computed Compcrisonof the exccf derivotive MATLAB qradient function.If .9. 19. well suited vectors.In thenext section.for a twodimensional for determiningthe partialderivatives matrices. s with the As displayedin Fig. .nis to ornitted.
if you were mountainclimbing. Employ the srradient function to determineto partial derivativesfor the following twodimensional function: /fr rr\ : 1. the partial derivatives provide the slopes in the directions of the axes. and "y : distance measuredalong the northsouth axis. Now supposethat we generatea grid of points in the xy plane and used the foregoing equations to draw the gradient vector at each point. Conversely. which representsthe steepest slope.1. it would indicate how a ball would travel as it rolled downhill from any point. we can returnto our discussion partial derivatives of at the end of Section 19.If we think of the two partial derivatives as component vectors. x : distance measuredalong the eastwestaxis.u.v) where x and y are matrjces containing the position coordinates and u and v are matrices containing the partial derivatives. Such graphical representationsare so useful that MATLAB has a special function. The result would be a field of arrows indicating the steepest route to the peak from any point.xt. has a magnitude (#)'. We can representsuch a function mathematically as z: fk. function is also quite useful for determining partial derivatives in two or more dimensions. it can be usedin conjunction with other MATLAB functions to produce visualizationsof vector fields.Recall that we usedmountain elevationas an exampleof a twodimensional function. to createsuchplots. you would probably be much more interested in determining the direction of the maximum slope.I9. the answer is provided very neatly by vf:+i++j dx af af dv where V/ is referred to as the gradient of I This vector. In particular. (#)' and a direction ^ _r / 0: tan'A f / A y \ \ffi ) where I : the angle measuredcounterclockwisefrom the r axis. called quiver.y) where z : elevation.8 CASE STUDY 465 FIELDS VISUALIZING Beyond the determinationof derivativesin one dimension.However.y.the sradienr Bockground.A simplerepresentation its syntaxis of quiver(x.The following example demonsffates use of quiver the to visualizea field.1.if we plotted the negative of the gradient. For this example. tZ .y 7xZ 2. To understand how this is done.
ho1d on 19.t riva and initi l9.6 19.ho1d oft : FIGURE I9.fy).4 1. asananonymous y) function > > f = G( x . ^ 2 .2 * x . * y _ y .1 mati mati tt".t.3 finit. Wecanfirst express/(.y. A seriesof equally spacedvalues of the independent and dependentvariablescanbe generated as >> z_f (x.l of r ro= the diff 19.' redr 1.IO ffil$?":rui5r*n'".y.clabel (cs).as r on contourplot: >> quirzer(x. Thenusequiver y to superimposevector 1 a fieldonacontour plot of thefunction.2 l9.z ).t equi first eacl cate 19.2 .466 NUMERICAT DIFFERENTIATION continued from : 2 to 2 and : l to 3.r l9..the resultant of the partial derivatives can be superimposed vectors the ' "._fx. seco diffe In ei inter 2L.4 0.2 0. y ) y .ruj:lof J o twodimensionol rhe function resulionrporriol with ofthe 2.6 2.i .6 0. ^ 2 .6 1.x . The gradient function can be emproyedto determinethe paftiar derivatives: We can then developa contourplot of the results: >> cscontour(x. * x . Solution. t9.y).2 first E'np As a final step.
r = 5 u s i n gh t : 2 a r u lh z . f i o m P r o b . and 2A.9 Develop an Mfilc to obtain flrsttlerivativeestimates data: for unequally spaced data. fbr Employ both O(h?) andO(ft4) formulas your cstimates.. r'r+t .t 3 . C o m p a r c h i s r e s u l tr v i t h thetruevalue and with an estimateobtainedusing a centered differencc approxiniationbasedon /i : l. andcentralclif'furencc mations O(h)\ and O(h1l for the flrst derivativeof of . 19.r at t : n llusing stepsizes h 1: n f I h.f ^ ft. fbr diff'erent will be aroundthe point r.25) at .r : r .dh {'.2 antli * 2.e' ar x : 2 fot' ft : 0.3 through 19.j Use a Taylor series expansion to derivc a ccntercd approximationto the third clerivalive that is finitedifference do secondorder accurate. 19. The function's and then drops away in all directions. the approrinrrtions estimate to the difl'erence 19.. 19. cxpansion the A. ri I .rn 19.. difl'erences O(/rr) for the of and : r/6.:. km 25 3'2 50 5B 75 7B t00 92 125 r00 to Usc numericaldiff'erentiation estimatethe rocket's velocilr .0 . PROBTEMS 19.'Ib this.2Usecentered firstand secondderivativesof r .1Compute forward and backward difference approxiof approrirnations O(h) ard O(h]). . 19.r : r / 4 u s i n ga v a l u co f h : r 1 1 2 . 5 . the first dcrivative 19.the gradient drops off more steeply to the northeastand the southwest.i and i * l. l ' = c o s . .:!iL l.you will have(o use four expansions the pointsli :. The In each case. Ernploy centered initialestimates. . E s t i m a l e truepercentrelative error rr fbr eachapproximation.4Use Richardsonextrapolationto estimatethc first deof of rivative r' : cos. rcspecvaluesof the indepcndent dependeut and tively.5 The resultis shownin Fig. 19.r.Tcstit with thc fbllowine 15 0.. 19.r will bc used in each casc of i . 3 7 3 4 0 .r a .The function shoulcl cach value of thc indcpendcnt arrd dy2dx2 versusx.or (b) the valueslor the independent Test your programwith the data able are not equally spaced.5RepcatProb. Carry enou{hterms along ilt each expansionto evaluatethe first term that will be truncated determinethe orilcr oi the approximation.10.^+:. The four' must then be combined in a way to climinate the equations first and secondderil'atives..5. to 19.8 Develop an Mfllc to apply a Romberg algorithm to ..19 Devclop an Mfile function that computcsfirst and second estirnates oltler O1lir1brsedon the fbrof clerivativc mulas in Figs. a t t dx 2 : 2 . (19.2[) to determine o o f r : 2 .r interval will be usedin eachcaseof i . Eq. indicatedby the lengthening As arrows. Cornpareyour rcsulls with the true derivati ves. I .1 9 . and dydx and dy2d.r.8 a t r : 0 b a s e d n v a l u e sa 1 t x o= .+2.2 are output vectorsof lcngth il estimatesat containing the first. data points.Thc function'sflrst line shouldbe sct up as f unctiotr ( lrll'dx.ti.4.ra t .but fbr the first derivativeof ln. (19.and secondderivative variable. Havc your generatea plot of c1y'dx Mfile returnan error messlgeif (a) the input vectorsare not varithe samelength.PROBLEMS 467 Note that we have displayed the negative of the resultants.7Provc that fbr cquispaced reduces Eq.r. 19. OrOO*2I = flif f rzq . r .'1 wherc x and y are input vectorsof length n containingthe variables.6 ..21) 19. ilnd." peakoccursat x : *1 and y : 1.l (r) : 5z2'.. to 19.6 t6 x 2 f ( x ) 0 9 0 3 6 4 ..11The fbllowing data was collectcdfor the distance traveledversustime fbr a rocket: It S J.r. J r : 1 .l 2 . in order that they point "downhill.r at .6Employ Eq. Il . 3 6l 25 35 o o B 4 2 2 0 0 15 9 6 rvhcre .. 6.
15 Use regrcssionto estimatc the accelerationat each is time thatthe centered difl'erence computed theloop. backward.I \ r 0 + 2 i 0 ) A e (c) Repeatpart (b) lbr the secondorder fbrwardandback(Notethatthese bedone the ward difTerences.25 45 678 B 5 9 3 t 0 surallpipe.s x. is 19. In order to do it.22 M lime. the orderoolvnomials.(rodl A75 t'.^ Time.and (c) backward finitemethods. 9 1 0 9 t 0 (b) fbrward finitedifl'erence.rt1z "f ( r ) J2tr Use MAILAB . approximation startingwith . clifTercnce second19.t.20 You have to measure flo$.24 'T: t. cn c. of polar coordinates r : 1. + t 0 4 0 a n d a : G . s 3 9 9 O ) 2 9 5 2 a.Estimate (a) velocity @.rs Include exact r.m O a7 4 IB 6 34 B 5l l0 63 l2 l4 16 7:3 B0 B4 .468 N U M E R I C A LI F F E R E N T I A T I O N D 19.This will be done for wher u n r t€ (cmz Ane centr derly i::: x.third.l85 lAA 2aB t 75 2 37 '249 261 325 271 383 273 l s 05 x 2 I 0 { l 0 . s 2AA 0.2atq7 (1 352070 3e894 /(x) ::::: J.1 in ale down to a nrinimumvalueof Ar = 0.2.5. for the first evaluation.) in time fbr the following data with second. neer! temp 0 0 B 164 x rrh T.rate of waterthrougha 1 2 3. 1 2 9 5 2 0 5 3 9 9 Use MATLAB to estimatethe inflectionpointsof thisdata.tif f iy.ral value of the derivativc.r valuesfor thecenorder correct) to find the vector cxpressions vclocity il given rn tered differencc approximationwill be .and data is taken orclel accurate forwiucl.5or and acceleration The velocitv and acceleratiorr rl. can at same 19.5 r 05 l 2 0 f ( x \ 0 . 3 5 2 A 74 2 4 1 9 7 4 . l( (a) Use calculusto dctclmine the corrcctvalueof thedenvativeatr:2. 3 4 B 6 B 6 6 8 . and fburth(d) Plot the resultsof (b) and (c) versr.12 A jet fighter'spositionon an aircraftcarier's runway was timed during landinu: ftom the normal 19. you placea bucketat thepipe's t u lO )2 ll 1 4 1 7 t 6 t 2 t 4 l 4 t 0 outlet and measure volumc in the bucketasa function the of titnc as tabulatedbelow. tn 5l 20 2A2 244 246 2 0 8 2 t O A72 o 7 a 0 6 8 4 57 4.x. "l to determine the inflection points of this 19. i : i a .16 The nolrlal distribution definedas I . Estimatethe flow rateat t = 7 s. mi v. cm3 t.rO I 2 J 4 5 6 7 B 9 10 Use sccondorder correct (a) centered flnitediftcrence. Usc finitcditfbrence approximations aresecondthat ordercorrcct. Then.5. commancl develop MATLAB Mfile tunction to computeflnitedifftrenceapproximations to the first and sccondderivativeat eachr valuein thetable below.s O 2 Position. .l1 A plane is being trackcdby radar.rldt') and (b) acccleration(duldrl using nunrericaldifferentiation. At 206 seconds. the 19. 19.the . ftl where x is the distancefrom the end of the carricr.r2 ): t9. use the centercdfinitediflerence(secondlor Thus.r 0 : ) 2 . to a 19. lr Calc order 19.18 Usc the .13 Use the fbllowing datato find the velocitl'and accelerationat / : l0 seconds: r. . y 1 4 2 ) 3 .O (.21 The velocity rr (m/s) of air flowing pasta flat surface If thr .r : 2 t 0. Plot the results: rcsullon the plot lol eomparison.19 The objcctive of this problem is to compare lg.r = 0. 6 7 5 8 .m 0 153 o:. s Volume. decreasc increments 0.66 5374 5560 5800 6 0 3 0 6 2 4 a lG):e2'x Use t estirr conjr pollu (D= sedin the lr t9. (b) Develop an Mf ile function to evaluatethe centered fi nitedifl'erence s.01.h U.5 and 2.17 The fbllowing data was generated distribution: Use i r (N/ T t. and centered finiteeverysccondin polarcoordinates and r I ditfbrence approxirrations thc first clerivative a function of of to the actr.
.r : distance 1cm).22) whereZ: e. : temperature ('C).. T. The averageconcentlationof a substance varieswith depth. 4. ms V. 6 x 1 0 6r n 2o f sediments.n v.r : 0) into a 0 t0 20 40 60 B0 12a lB0 280 400 35 26 15 7 a)829414946 l9.002 0006 a2B7 0899 00r2 I915 0 0r B 3 048 A A?! 4 299 (ml) and .28) beexcan of of pressed as JT dT klT Tul I.sion 19'22 /rnl stalesthat 71vst flux : Dd' Mass dr ctz.ne fiom the surfacedown to the bottom.rmerical the difl'erentiation techniclue availableto estimate derivalive at "rr: 0. (PI9.[.t U If theflux at x : 0 is 60 W/mr. 8 ).h 0 u. Newtott's r:i. and I: time (s). andl:: a propoflionality conrnedium the surrounding /aw stant(per minute). 106 borrels (i1 where V. of 19'24psudsp'slaw is usedroutinelyby architectural engineers detennineheat flow through walls. be that can computecl integlatron: by pi711'. I : current(A). 19. (g/m3).1. lo6m3 I8175 5 105I I 9635 Q3927 00000 lo.s1az 1'd where massflux : the quantity of massthat passes acrossa per D unit area unit tirne (g/cmr/s. Q . Ernploy this estimate in the with Eq. 19. ol4 t i a 0 0I 016 02 a32 03 056 05 084 0/ 2A 0 r 0 2 0 3 0 4 5 60 04 07 027 0BB r 05 \17 (:.2 c. min I'.'C 0 t9 008 l7 O.27 gxr.25 Ths horizontal surface areaA. : a dilfusioncoefTicient (g/cm3).: r. Determine the voltage drop as a functionof time from the fbllowing datafor an inductance H.PROBLEMS 469 19. (nr21 a lakeataparof fionr volumeby difTerentiation: ticulardepthcanbecomputed 4 . lf.v : 0). the followuse law ing voltagedatato estimate inductance a currentof 2 A the if over'400 milliseconrls. (cmr/s).23 fne fbllowing data was collectedwhen a large oil tanker was loading: /. much pollutantu'ould be transported how into the lakeover a year's time? 19.r : 0 at the sedimentwater a interfaceancl downward): increases r. 5 2 x 1 0 ./ : volur.(z) (Pt9. : tlepth (nr) as nreasured rvhere I. Determinethe averageconcentralionbasedon the following data: 16 4 B 12 O z. t7l 0 006 a32 3 06 Use bestnr. this equation(calledNcruton's to that the rateof cooling is proportional of cooling) specifies . of of whereI: temperature the body ("C).oltage drop (V).6 c m 2 / s )F o r a l a k ew i t h 3 .22) to colrlpute massllux of corlunction the pollutant of the sediments out and into the overlying walers . l06 g/cm3 the total depth (rr).] r:Q)A. 1: x l05 N ' s/rn2. (:) aa.m/s 0 0. s/A).1 . tll/ ldt) fbr eachtirne to the order /rI . Cm c. dV .4 4.: concentration c and . The fbllowing to are temperatures measuredfiorn the surface(.9/m3 19.2 7.rczrsln' larl to determine the shear stress Use at r (N/m:) the surface(. 1 .2tt 15srare cooling a body(Fig.26 p. L: indtrctance henrys: I H : I V. in LlL aI dyn t: A s s u m e a v a l u e o fa m i c v i s c o s i4 y1 . volts the Calculate flow rate Q (that is. P19.4 on Faraday's 1Prob.1 B5 5. engineer measuresthe fbllowing conAn environmental centration a pollutant in the pore watersof sediments of underlying lake (.e diffir.26).orr4^r's law characterizes r'oltagedrop across the an inductor as di Vt.: L.Thus. cornputeft. is passerJ thloughthe inductor t.
7 462 700 800 900 ' K) I Z5 036 ) 89 273 20A 9n 19.3 react reacti 8uQ ft14 d. andemploy lir"rear regression evaluate to fr. in I 9.the heat l]ux to the surface can be computedwith Fourier's law: . (a) Detcrminc the pressuredrop for a l0cm length tuh = for a viscous liquid (p : 0.31 El(m: 0t 5 t0 20 25 30 4A 45 50 224 22 r35 tl 090 O68 o6t a54 250 47 2.K) T..\ I49 t2 099 475 4.028J/(s ..33 Tllg specificheat at constantpressure tJ/(kg. of an ideal gas is relatedto enthalpyby dh . The data was takcn lbr a real fluid. s/mr. The tbllowing measurements mal to the surfacc are whcre ft : enthalpy (kJ/kg). r. Time. Irr ..density p = I x I03 kg/mr) with a flow of l0 x 106 m3/s andthe fbllowing varying radii along its length: wher. = flow centerline 0 (mr/s)anclr: radius(m). reacti (minevalu logar lo Thert log(slope meth. otm r=350K r=400K T=450K difl'erentiation dcterminen. 19. "c 0 80 5 r0 . mi c.28 the ditlcrencein thc tempcratures the body and of thc surol rounding medium.7. the temperature at of the ball changes. r : distancealongthetube\ (m). dT 19.34 l6 142 2 dcscribcclbelow. fr : dynamicriscosity{N..r': distance norrnal to thc surface: Jr clll T.rp ' d (c) Dctelminc the averageReynoldsnuntberfor thetube to Ju \ verit'y that flow is truly laminar (Re : pu D I 1t < 21N whercu: velocity).s.470 NUMERICAL DIFFERENTIATION madetbr air flowing over a flat plate where.32 the following data for the specificheatof benzene V. and I : 0. K Cr. Plot dT f dt versw T .mm 2 135 1. follor urea: t.r ! a 19..145 300 t5 24t 20 2t7 25 2A7 Utilize nunrericaldiff'erentiation determinedTldr ateach to value of tirnc.lt/ ') the averagcradius. If a metal ball heatedto 80 'C is dropped into water that is held constant f.L was generated polynomial.67 5 06 282.5 523 27 I 5.005N .and (b) the heattransf'er watts. : 20'C. Estimatethe enthalpyof the f'luid at zl00K and 50 atm (evaluate (b) Comparcyour result with the pressure drop thatwould thc integral frorn 0. H : I ( r ' r (\ 1T / p l ). K)l q.30 p61 fluid tlorv over a surface. K).y : distancenor(m). kll(kmol 300 4AO 500 600 82 BBB Il2 136 136933 157744 1000 210450 I03 a78 4. have occured if thc tubc had a constant radius eoual to 7P .s/nrr. K C. The fbllowing enthalpiesare providedfor carbon . I atm to 50 atm).3I The pressure gradientfor larninarflow through cona stantradiustube is given by dn d. to where p = pressureiN/rn:)..Usenumerical with an /rthorder P. (a) deterrnine flux at thesurthe face. min r. in as If thc plate's dimensionsare 200 cm long and 50 cm wide.cm O 2 4 5 710 6 19'29 Tl1senthalpyof a real gas is a function of pressure as l58 r.4 T. and I : absolute lemperature (K). ml 19.5 I ).Note that I J = 1W.kl/lkmol.K 0 900 I 480 35 270 dioxi deter lated and < T'K 2t0 FIGURE PI9.
mole 0 A750 where11: the depthof water (m). I : time (rnin). m g / L 1 o 7 .rlcr Iog{ .769 Sediments I I 19. P I 9.9994 471 Lid Water H T. dioxide Use these valuesto determine specificheat in J/(kg . n : reactionorder (dimensionless). Note that the atomic weights of carbon g/mol.?log( if Therefore. / .35The sedirnent oxygen demand [SOD in units of g/1mr.35 P dl : *kc" dissolved oxygencontentof a naturalwater. This involves applying a the logarithmic transformto the rate law to yield.34 nthorderrate law is often usedto model chemical An of reactions solely dependon the concentration a single that reactant: dc FIGURE I9. The di.11is measured by placing a sediment core in a cylindrical conniner (Fig. the nthorder rate law holds. d 0 0125 425 0375 05 0625 475 o . min c.K h. containeris covthe A eredto preventgas transfer.After carefullyintroducinga layer of distilled. o : oxygenconcentration (g/mr). Based on the following data and H : O.11 an important parametel in determining the is . 0 3 19. stirel is usedto rnix the water gently.445 42. use nuplots of (a) SOD versus merical differentiationto generate oxygenconcentralioll: lime and (b) SOD versus 5 o 594 l5 a 420 30 a 29t 45 0. oxygenated water abovethe sedinrents.59 2 57 r r s 0 3 3 0 .and an oxygen probetracks how the water's oxygen concentrationdecreases over time. }=logk \ dt / L. K) fbr eachof the tabuthe lated temperatures. . The SOD can then be computedas SOD: I1: do at wherec : concentration (mole). l l 4.PROBLEMS (COr) at severaltemperatures.179 1000 900 37.fferentialmethod can be used to evaluate parametersk and rt. r'espectively oxygen I and are 12.01 and I 5.223 t . Use the dilferential methodand linear regressionto deternrinet and n for the following data fbr the conversionof arnmoniumcyanateto urea: l.l m.35). a plot of the log(dcldf) versuslog c should yield a straightline with a slopeof n and an intercept of logfr.and t: time (d). and ft : reaction rate (min I moler "). d.kt/kmol 754 29 629 800 32.
&
t
f,
Ordinory Differentiol
l
Equolrons
r'
6.r ovERvrEw
The fundamental laws of physics. mechanics,electricity, and thermodynamicsare usually' basedon empirical observationsthat explain variations in physical properties and statesof i' systems.Rather than describing the stateof physical systemsdirectly, the laws are usually_ .r" *F '* , couched in terms of spatial and temporal changes. These laws define mechallt** .oj ., momentum, differenliol change.When combined with continuity laws for enorgy,mass,or resultsin matheequations of result.Subsequent integration thesedifferential equations " in termsof energy, the matical functionsthat describe spatialand temporalstateof a syslem , '" As mass,or velocity variations. in Fig. FT6,1, the integrationcan be implementedanalyti* l,, cally with calculus or numericalli with the computer jumper problem introThe freefallingbungee of of ducedin Chap. I is an example the derivation a ditferential equationfrom a fundamentallaw. Recall that Newton's second law was used to develop an ODE describingthe rate of changeof velocity of a'" jumper: falling bungee du c,t t
i:r:u
(PT6'l)
constant.rr is the mass, where g is the gravitationa.l which are and c, is a drag coefficient.Suchequations, of composed an unknown functionand its derivatives. are called dffirential equntions,,:TheY sometimes ate referred to as rate equationsbecausethey expressthe rate of change of a variable as'a"'functionof variables and parameters. u In Eq. 1PT6.l), the quantitybeingdifferentiated is called the dependent variable. The quantity with respectto which u is differentiated t is calledthe independent variable. When the function involves one independentvariable, the equation is called an ordinary
' .: ,.:: ..1.r..
473
, r,i:,ir.,.,, t.,..
474
PART ORDINARY 6 DIFFERENTIAL EQUATIONS
Physicallaw
F=ma
f
I I
du ^ 6
, aIm
I
I
I
cJ..2
ODE
V Solution
I
Analytical (calculus)
n
\
Numerical (computer)
/
u  . / " " ' t a n h[ , / r y r ]
\ t,j \V /n /
l o m / l o ^ ,
\
u r + r= u i + ( s  ' ! r ' ) n,
FIGURE PT6.I
i I e s e o u e n r e t e v e n t s n r h ed e u e l o p r r e n ' ^ d s o l  l l o ^o l O D E sf o e n g  e e r i n go n d s c i e n c e i o o Ihe exompleshown is for the velocityof the freefcliingbungee iumper.
dffirential equation (or ODE). This is in contrastto a partial dffirentiul equation (or PDE) that involves two or more independent variables. Differentialequations also classifiedas to their orderFor example.Eq. (PT6.l) is are called afirstorder equatio,? because highestderivativeis a lirst derivative.Asecondthe order equationwould includea second derivative. For example, equationdescribing the the positionx of an unforcedmassspring systemwith damping is the secondorder equation: m *t. dt'
d'x
,)
dx
,
dt
*Ax:0
(PT6.2)
where ,fl is mass,c is a damping coefficient,and k is a spring constant.Similarly, an nthorder equationwould include an nth derivative. Higherorderdifferentialequations be reducedto a systemof firstorderequations. can This is accomplished defining the first derivativeof the dependent by variable as a new variable.For Eq. (PT6.2),this is doneby creatinga new variableu as the first derivativeof disolacement dx dt where u is velocity.This equationcan itself be differentiated yield to
(l t' o'l
(PT6.3)
dt
dtz
(PT6.4)
Equations(PT6.3)and (PT6.4)can be substituted into Eq. (PT6.2)to convertit into a firstorder equation: du m7tcu+kr:0
(PT6.s)
6.I OVERVIEW
As a final step,we can express Eqs.(PT6.3)and (PT6.5)as rateequations: dx
l
475
ut
(Pr6.6)
ck tn nt
tlu
dT
'
(PT6.t)
Thus, Eqs. (PT6.6) and (PT6.7) are a pair of firstorder equationsthat arreequivalent to the original secondorder equation (Eq. PT6.2). Becauseother nthorder ciiff'erential equations can be similarlyreduced, this part of our book fbcuses the solutionof firston orderequations. A solutionof an ordinarydifl'erential equationis a specificfunctionof the independent variable and parameters that satisfiesthe original differential equation.To illustrate this concept,let us startwith a simple fourthorderpolynomial, . t ,:  0 . 5 1 4 + 4 . r 3  l 0 x 2 + 8 . 5 . r* I Now. if we differentiate (PT6.8).we obtainan ODE: Eq.
(Pr6.8)
d.r  _2r3 * l2,rr  20r * 8.5 dx
(Pr6.e)
This equationalso describes behaviorof the polynomial,but in a mannerdifferentfrom the Eq. (PT6.8). Rather than explicitly representing values of ,r for each vahre of .r, the Eq. (PT6.9)givesthe rateof change ywith respect (thatis, the slope)at everyvallle of to.r of .r. FigurePT6.2showsboth the functionand the deriv'atile plottedverslls.{. Noticehow the zero valuesof the derivativescorrespond the point at which the original function is to flatthat is, where it hiis a zero slope.Also, the maximum absolutevaluesof the derivativesale at the endsofthe intervalwherethe slopes ofthe functionaregreatest. we Altlroueh, just denronstrated, can determine differential as a equationgiven the originalfunction, objecthereis tt.r the determine originalfunctiongiventhedifferential the eqr.ration. original function then represents solution. The the Withoutcomputers, For with calculus. example, ODEs areusuallysolvedanalytically Eq. (PT6.9)could be multiplied by dx andintegrated yield to
t:
lrztt
r/.r1 l2.r'r 2ox* 8.5)
(PT6.I 0)
The righrhand sideof this equationis calledan indefiniteintegraL because limits of inthe tegrationare unspecified. previously This is in contrastto the definite integralsdiscussed in PartFive [compare (PT6.l0) with Eq. (17.5)]. Eq. An analyticalsolutionfor Eq. (PT6.l0) is obtained the irrdefinite if integralcan be evaluatedexactlyin equation fblnr. For this sinrple case, is possible do this witlr the result: it to r ' :  0 . 5 1 4 * 4 . t 1  1 0 . 1+ 8 . 5 . r* C 2
(PT6.r ) r
which is identicalto the original function with one notableexception.In the courseof differentiatingand then integrating,we lost the constantvalue of I in the original equation and gainedthe value C. This C is called a u)nstantof integrorion The f'actthat suchan arbitraryconstant appears indicates that the solutionis not uniclue. fact,it is but one ofan In
476
PART ORDINARY 6 DIFFERENTIAL EAUATIONS
FIGURE PT6.2
P l o t s f ( o )\ ' v e r s u so n d l b ) d  r . / r h e r s u s r f o r t h e f u n c t i o n l , : O 5 r a * 4 x 3 l O ; + B 5 j r + I o r v
infinite numberof possible (con'esponding an infinitenumberof possible functions to values of C) that satisfythe differentialequation.For example,Fig. PT6.3 showssix possible functions that satisfyEq. (PT6.l I ). Therefore,to specifythe solutioncompletely, ditTerential a equationis usuallyaccompanied by auxiliary conditions.For firstorderODEs, a type of auxiliary condition called ln initial value is requiredto determirre constant the and obtain a uniquesolution.For example,the original diff'erentialequationcould be accompanied the initial condition by . thatat r: 0 . . 1 ' : l . T h e s ev a l u e sc o u l d b e s u b s t i t u t eid t o E q . ( P T 6 l l ) t o d e t e r m i n e n C : l. Therefore,the unique solution that satisfiesboth the differentialequationand the s p e c i l ' i eid i t i a lc o n d i t i o ns n i r': 0.5x1 f 4rr  l0rr r8.5r * I Thus,we have"pinneddown" Eq. (PT6.l1) by forcingit to passthroughthe initial condition, and in so doing, we havedeveloped uniquesolutionto the ODE and havecornefull a circle to the originalfunction[Eq. (PT6.8)]. Initial conditionsusually have very tangibleinterpretations diff'erential for equations derived from physical problem settings. For example.in the bungeejumper problem, the
ORGANIZATION 6.2 PART
477
C=3
( =Z
C=1 C=0
PT6.3 FIGURE of to Sixpossibe solutions theiniegrol 2r'r l2x2 2Or* 8.5.Eoch for conformso differenl I
C. vo ue of lhe constont inteqrolion of
initial conditionwas reflectiveof the physicallact that at time zero the verticalvelocity jumperhad already was zero.If the bungee beenin verticalnlotiorlat time zero,the solufor tion would havebeenmodified to accoLlllt this initial velocity. When dealingwith an nthorderdifferentialequation,n conditionsare requiredto obtain a unique solution.If all conditionsare specifiedat the samevalue of the independent variable (fbr example,at,v or t : 0), then the problem is called an initialvalueproblem. problemswhere specification conditionsoccursat of This is in contlastto boundaryvalue valuesof the independent variable.Chapters and 2l will fbcus on initialvalue 20 difTerent problems. problems coveledtn Chap.22. are Boundaryvalue
6.2
PARTORGANIZATION
Chapter 20 is devotedto onestepmethodsfor solving initialvalue ODEs. As the name suggests, onestep methodscomputea futurepredictiony;a1, basedonly on information at a single point r; and no other previous information.This is in contrastto multistepapproachesthat useinlbrmation from several previouspointsas the basisfor extrapolating to a newvalue. presented Chap.20 belongto With all but a rninorexception, onestep the methods in wh:rt are called, RungeKuttatechni4lle.s. Although the chapterrnight havebeenorganized to aroundthis theoretical notion, we haveoptedfor a more graphical.intuitive approach inThus, we begin the chapterwrth Euler's ntethod,which has a very troduce the rnethods. graphicalinterpretation. addition,because have alreadyintroduced we straightforward In Euler's methodin Chap. l, our emphasis hereis on quantifyingits truncationeror and describineits stabilitv.
478
PART ORDINARY 6 DIFFERENTIAL EQUATIONS
Next, we usevisually orientedarguments developtwo improvedversionsof Euler's to nretlrodtlre Heun and the midpoint techniques. After this introduction,we formally develop the conceptof RungeKutta(or RK) approaches demonstrate and how the foregoing techniques actuallyfirst and secondorder methods.This is followed by a discusare RK sion of the higherorderRK fbrmulations that are fi'equentlyused fbr engineeringand scientific problem solving. In addition, we cover the applicationof onestepmethodsto .ty.r/erns ODEs.Note that all the applications Chap.20 are limited to cases oJ in with a fixed stepsize. ln Chop. 21, we cover more advanced approaches solving initialvalueproblems. for First. we describeadaptiveRK ntethods that automaticallyadjustthe stepsize in response to the truncationerror of the computation. Thesemethodsare especiallypertinentas they are employedby MATLAB to solve ODEs. Next, we discussnrulti,step methods. mentionedabove,thesealgorithmsretain inAs fbrn'ration previousstepsto more ell'ectivelycapturethe trajectoryof the solution.They of alsoyield the truncationerror estimates that canbe usedto implementstepsize control.We describea sinrplemethodthe nonselfstarting Heun methodto introducethe essential features the multistepapproaches. of Finally, the chapterends with a descriptionof ,stiffODEs. Theseare both individual and systems ODEs thathaveboth fastand slow components their solution. a conof to As sequence, they require specialsolution approaches. introducethe idea of an implicit We solution techniqueas one commonly usedremedy.We also describeMMLAB's builtin functionsfor solving stiff ODEs. ln Cha1t.22, we focus on two approaches obtaining solutionsto boundary'value for problems:theshootingandJinitedffirencemethods. Aside from demonstrating how these techniques implemented, illustratehow they handleclerivatiye are we boundorvconditions and nonlinear ODEs.
ti'.iili,
'l
''; 'j:''
':
'ii.
*l\,
l:io ":i'
InitiolVolue Problems
CHAPTER OBJECTIVES
The primary objectiveof this chapteris to introduceyou to solving initialvalue problemsfbr ODEs (ordinarydifferentialequations). Specificobjectivesand topics coveredare t . Understanding meaningof local and global truncationerrorsand their the relationshipto stepsize for onestep methodsfor solving ODEs. Knowing how to implementthe following RungeKutta(RK) methodsfor a singleODE: Euler Heun Midpoint RK Fourtholder Knowing how to iteratethe coffectorof Heun'smethod. fbr Knowing how to irnplement followingRungeKutta the methods systems of ODEs: Euler RK Fourthorder
. .
YOU'VE GOT A PROBLEM
e startedthis book with the problemof simulatingthe velocity of a freefalling jumper.This problemamounted formulatingand solving an ordinary to bungee diffbrentialequation,the topic of this chapter. Now let's return to this problem and make it more interesting computingwhat happens by when thejumper reaches end the of the bunseecord.
480
INITIALVALU E PROBLEMS
To do this, we should recognizethat the jumper will experience dedifferentforces pendingon whetherthe cord is slack or stretched. it is slack,the situationis thatoffree If fall where the only forces are gravity and drag. However, because jumpel cannow the move up as well as down, the sign of the dragforce mustbe nrodifiedso that it always tends to retardvelocity,
du g  srgn(r,)?u2 dt
(20.1a)
where u is velocity (m/s), t is time (s), g is the acceleration to gravity (9.81m/s'),c7is due the drag coefficient(kg/m), and m is mass(kg). The .signum a Jilndion.t sign,returns  I or its is a I depending whether argument negative positive, on or respectively. Thus,when the jumper is falling downward (positive velocity, sign  l), the drag fbrce will be negative and hence will act to reduce velocity. In contrast.when the jumper is moving upward (negative velocity.sign: l), the dragfbrce will be positiveso that it againreduces the velocity. As Once the cord beginsto stretch,it obviouslyexefis an upward force on thejumper. donepreviouslyin Chap.8. Hooke's law can be usedas a first approximation thisforce. of In addition,a dampeningforce shouldalso be includedto accountfor frictional effects as the cord stretches and contracts. Thesef'actors can be incorporated along with gravityand drag into a secondfbrce balancethat applieswhen the cord is stretched. The resultis the f o ll o w i n ed i f f e r e n t i a l o u a t i o n : e du
al :,9 
) Slgn(u)U tn
c,t
(r ilt
k

I.l 
U m
y
(20,\b)
(N/m), x is verticaldistance wherek is the cord's springconstant measured downward from jump platform (m). L is the lengthof the unstretched the bungee cord (m), and 7 is a damp(N eningcoefticient . s/m). (x BecauseEq. 120.1b) only holds when the cord is stretched > L), the springforce will always be negative.That is, it will always act to pull the jumper back up. The dampening force increase.s magnitudeas the jumper's velocity increases in t0 and alwaysacts slow the jumper down. If we want to simulatethejumper's velocity,we would initially solveEq. (20.1a) until the cord was fully extended. Then, we could switch to Eq. (20.1b) for periodsthatthecord is stretched.Although this is fairly straightforward,it rneans that knowledgeof the jumper's position is required.This can be done by formularting anotherdifferential equation lbr distance: dx
dt
(20.2)
jurrrper'svelocity amountsto soiving two ordinary Thus, solving tbr the bungee differentialequations whereone of the equations takesdifferentforms depending thevalue on
I Some computerlanguages represent signum functionas sgr (x). As represented the the here,MATLAB uses n o m e n c l a t u rs i o n 1 x ) . e
20.2 EULER'S METHOD
48r
of one of the dependent variables. 20 Chapters and21 exploremethodsfor solving this and similar problems involvingODEs.
20.t ovERvrEw
This chapteris devotedto solvine ordinarv diflerentialequations the form of
d.y
dr
: I (l' Y)
(20.3)
In Chap. l, we developed nnmericalmethodto solve suchan equationfor the velocity of a jumper. Recall that the methodwas of the generalform the freefalling bungee New value : old value* slope x stepsize terms, or, in mathematical
.Vi+r: )'i*Qh
(20.4)
where the slope@is calledan incrcnzent Junction.Accordingto this equation,the slopeestimate of @is usedto extrapolate liom an old value yrto a new value y;11 over a distance /2.This fornrula can be appliedstep by stepto traceout the trajectoryof the solution into ntethods the future. Suchapproaches calleduteste1t are because value of the increment the point i. They are also refelred to as Rwrgefunction is basedon information at a single who first discussed them in the early Kutta ntethodsafter the two appliedmathematicians 1900s.Another class of rnethodscalled multistepmethodsuse information from several previous points as the basisfor extrapolating a new value. We will describemultistep to rnethods briefly in Chap.21. All onestep methodscan be expressed the generalform of Eq. (20.4),with the only in differencebeing the mannerin which the slopeis estimated. The sirnplestapproachis to the use the differentialequationto estimate slopein the fbrm of the first derivativeat /;. ln other words, the slopeat the beginningof the interval is taken as an approximationof the averageslope over the whole interval.This approach, called Euler's method,is discussed next. This is followed by other onestepmethodsthat employ alternativeslope estimates predictions. that result in more accurate
2O.2 EULER's METHOD
The first derivativeprovidesa direct estimateof the slopeat /i (Fig. 20.I ): q : f ftr,),i) where /(t;,1;) is the differentialequationevaluated /; and _r'i. at This estimatecan be subinto Eq. (20.1t: stituted
)'i+r: t"i* fGi,v"t)h
(20.s)
or This formula is refened as Euler's rnethod(or the EulerCauchy pointslopemethod). to A new value of " predictedusing the slope (equalto the first derivativeat the original is valtte of r) to extrapolate linearly over the stepsize h (Fig.20.1).
482
INITIALVALUE PROBTEMS
FIGURE 20.I Eulermeihod. s
EXAMPLE 0..l 2
Euler's ethod M ProblemStotement. Use Euler'smethodto integrate  4ro s'  0.5.r' from r : 0 to 4 !' : 0 is y : 2. Note that the exactsolution with a stepsizeof 1. The initial conditionat / can be determined analvticallvas ^' * e o5r')12no5r ) : r rlrtrut' Solution. Equation(20.5)can be usedto implement Euler'smethod: : , ] , ' ( l ) ) , ( 0 )+ J ( 0 , 2 ) ( l ) where 1,(0) :2 andthe slopeestimateat / : 0 rs . f ( 0 , 2 ) : 4 e o 0 ' 5 ( 2 ) : 3 Therefbre, ](l):2+3(l):5 The true solutionat r : r :
4
1 is
,,
r o * rt t tr (eust  e 05(l)) 2e05n\:6.19463 +
Thus.the percent relative erroris t6.1946351 F, : ll x 1007o: l9.28Vo 6.19463 l For the secondstep: )(2) : y(l) * /(l. s)(l) :  5+ f 4 e 0 8 ( r t 0 . 5 r 5 t l 0 ) :
11.40216
METHOD 20.2 EULER'S
TABLE20.l of volues the integrol of Comporison trueond numericol q1 n l , o f . r ": 4 e ( t 8 t 0 . 5 , vw i t ht h ei n i t i oc o n d i t i o t h o t y : 2 usingEuler's computed were volues r : O.Thenumericol with o stePsizeof I ' method
Jt"u"
483
JEur".
lerl (%)
0 I 2 3
4
2 00000 6 19463 I4.84392 3 36 7 7 l 7 75 . 3 3 8 9 6
2 00000 5 00000 t 1.442t6 25 51321 568493l
1 92 8 2319 24 24 24.54
T r u es o l u t i o n
20.2 FIGURE me,tf using solution Eulers o! !,]^"I59.1 ?t wiih solurion o numericol of fhe.rrue Comporison * 2 c n T h J l n i t i o lo n d i l i oo t / : O i s
i",\;, 6 i" f,.r ;:O;4 s o w 1 ho s i e p i z e f I o 1':
and, therefore,the true percentrelative error is The true solution at t : 2.0 is 14.84392 1 and Fig' 20'2' and 23.l97o.Thecomputationis repeated, the resultscompiledin Table20' the captures generaltrend of the true solution,the error Note that althoughthe computation in As discussed the next section,this error can be reducedby using a is considerable. smaller step size.
20.2.1 Error Anolysis for Euler'sMethod
4): The numericalsolutionof ODEs involves two typesof error (recallChap' employed errorscausedby the natureof the techniques l. Truncation,or discretization, to approximatevaluesof Y. canbe retained by 2. Roundnfi'enofscaused the limited numbersof significantdigits that by a comPuter. effor The truncationerrors are composedof two parts.The first is a local truncation questionover a singlestep'The secondis that resultsfrom an applicationof the methodin produced during the a propagatetl truncation error that results from the approximations
+ o ( h . it can be represented a Taylor seriesexpansionabout a starting value (/..).and the result is often represented as F ^  . If the solutionthat is.7) where f lies somewhere the interval from /. Subtracting Eq. and Ra : the remainderterm.7)to yield Eq.. the comparison indicatesthat a truncationenor occursbecause approxirnate true solutionusinga fiwe the nite numberof termsfrom the Taylor series. Additionally.the tluncationerror in Euler's methodis attributable the remainto ing terms in the Taylor seriesexpansionthat were not included in Eq. (20.20.3)..yi)/r. we seethat the local error is proportionalto the square of the step size and the first derivative of the differential equation.. ' j::/r"+l ( t z* 1 ) ! . definedas R. and t and y are the independent and the dependent variables.3)into Eqs." 4n * nl (20.ql where E.yr).yr+r: )'i t f (ti.6) where/u : ti+t .1) where E.8)yields E . (20. : the true local truncationerror. By cornparingEqs. . : the approximate local truncationeffor.The sum of the two is the total enor.(20.2 * .5).0t*l)191 (20..the higherorder terms in Eq. do this. .respectively. (20.\r.13)]: t ln ]i+r: ti *yitt +*n'+ :! " +'r'i' 7. (4. l0) or Eo : O(h2) (.For sufficientlysmall lz.For example. According to Eq. f'(tj'Yi) .5) and (20. a part of thetrue We or solution. it is proportionalto the stepsize . (20. An alternativeform can be develin opedby substituting (20. (20. + i I t20. leaveout. Insight into the magnitudeand properties the truncationerror can be gainedby deof riving Euler's methoddirectly from the Taylor seriesexpansion. 11. as in by [recallEq.l')h + where O(h'+l) specifiesthat the local truncationetror is proportional to the stepsize raisedto the (n * l)th power.484 INITIALVALUE PROBLEMS previous steps.It can also be demonstratedthat the global truncationerror is O(/r)that is.f ).9) are usually negligible. to /. .. (20. where dyldt : y'. (20.5)from Eq. realizethatthe To differential equation being integratedwill be of the general fbrm of Eq.6)and (20. the function describingthe behaviorof yhas continuous derivatives.h ' . thustruncate./...ll).[ 1 + P h 2+ .8).. (20. r . It is referredto as the gbbal truncation error.it can be seenthat Euler's rnethodconesponds to the Taylor seriesup to and including the term /(r.
Theseobservations et lead to someusefulconclusions: 1. if /z > 2la.13) quantity 1 . tf. Note that thereare certainODEs where errorsalways grow regardless the method. calculuscan be usedto determinethe solutionas V : ltr€ '' (20.and the stepsize. Hence. 1969). If its absolutevalue is greaterthan unity.That is.lyil + oo as i + oo.the stability depends the step size /r. 20. the The method wiil provide errorfreepredictionsif the underlying function (i.the numerical the method.a h ) (20.2. A nurnericalsolution is said to be unstableif enors grow sidered exponentiallyfor a problem for which there is a boundedsolution. the solution of the differential equation)is linear.20..t l 0 .12) and asymptoticallyapproaches zero. Euler's method is referredto as a firstorder method. I 2 t g i v e s E Yr+l: ji \'ih or . issue. It should also be noted that this generalpatternholds for the higherolderonestep methodsdescribed the following pages. the solutionwill grow in an unbounded fashion. 485 The global error can be reducedby decreasing stepsize. Basedon this analyon sis. because a straightline the second for derivativewould be zero.The stability of a particular applicationcan dependon three factors: differential equation. . at If r(0) .e.the local truncation resultsif the underlying solution is an rrthorder errorwill be O (h"+1) and the globalerror O (h'). i ( l.v stable. d)'.) ' i + r : 1 . Insight into the step size required for stability can be exanined by studying a very simpleODE: dt' ay .." *fution startsat 1.ah is called an cunplifictttion The parenthetical foctor. an nthordermethodwill yield perfect in polynomial. S u h s t i t u t i n gq .we learnedthat the truncationerror of Euler's methoddepends on This is an accuracy the step size in a predictableway basedon the Taylor series.2 Stobility of Euler'sMethod ln the precedingsection. ffrrr.)0.y i + l: l i + .e that we useEuler's methodto solve the sameproblernnumerically: Now suppose . of SrrchODEs are called illconditioned.2 EULER'S METHOD (Carnahan al. Further. is The stability of a solutionrrrethod anotherimportantconsideration must be conthat when solving ODEs. 2. .So clearly. Euler's methodis said to be conditionall. That is. This latter conclusionmakesintuitive sense Euler's methodusesstraightline because segments to approximatethe solution.
6. .3. .3 showsan Mfile that usesEuler's methodto computevaluesof thedependentvariabley over a rangeof valuesof theindependent variablet . .y] B eulode: EUIET ODE SOIVCT z [ r . in 20.elror = tspan(1). n = n+1.vector of independent variable z y = vector of solution ior dependent variable if nargin. . tspan. . Figure 20.4 z z if least 4 inpuu argumens reqL rq. : method to integrate z uses Euler's an ODE % rnput: 96 dydt = name of the Mfile Chat evaluates the ODE z tspan = lti. y 0 . add an additional value of t ^^ac rIOm L ri tO Ct {'ar r (n)<tf t(n+1) = tf .21. This is probablybecause bothrepresentsituationswhere the numericalsolutionbreaksdown and (b) both are affected step by size.6'r.i o n It.end tspan(2).We will retum to the topic when we discussstilTsystems Chap.3 MATLAB Mfile Function: eulode We have alreadydevelopeda simple Mfile to implementEuler's method for the falling jumper problem in Chap. let's developa rnore general.4..tf ' lLf>ri). Now. rh)l 1a".end n = length(t).an inaccurate methodcanbe very stable.ower'l. . e u .y0.allpurposealgorithm. y ] .VALUE PROBLEMS (a) Inaccuracyand instability are often confused. = additional parameters used by dydt % output : z t .l o d e r d y d t . p l .vararqini:])*(t(i+1)t(i) end ).p2. h . p l . r s p o n . . % p r e a L l o c a t e y L o i m p r o ' .Ir. .However. y for i = 1:nl ?implement Euler's method = y(i) y(i+1) + dydt(t(i).they are distinct problems. {'Jpper lrmit musl be grearer Lhar' .For example.error = if q^ ri if t (ti:h:tf)'. necessary. end y = y0'ones{n.h. tfl where ti and tf = initial and z final values of independent variable value of dependent variable z y0 = initial 9a h = step size z pI.rarargin) f u n c r . Recallfrom Section3. r ee F f i c i e n .y(i). that this function usedEuler's bungee nethod to compute the velocity after a given time of tiee fall.2.3 AnMJile implemeni method to Euler's = eulode(dydt.486 INITIAL. The nameof thefunction holding the righthand side of the differential equationis passedinto the function as the FIGURE 20.
0000 1r.In Mfile is the only option.because they have very straightwe forward graphicalinterpretations.In the eventthat the stepsizeis not evenly divisible into the range. ically calculated between adjacent in based the difference on ways. both modifications(as well as Euler's methoditself) actuallybelongto a largerclassof solution techniques calledRungeKuttamethods.For example.there will be more complex problemswherethe definition of the ODE requiresseverallines of code.4132 55.The length of the r vector is detervariabley is preallocated mined as n. 20.var).20. suchinstances. In addition. the last value will fall short of the final value of the range. Two simple modificationsare to in availableto help circumventthis shorlcoming.000a 4.3 IMPROVEMENTS METHOD OF EULER'S 487 variabledydt. 3*t) o. The initial and llnal valuesof the desiredrangeof the independent variable is passed a vector tspan.0000 2.I : fbr > > t i 1 ' c i t = G( t .3 TI. . will presentthem prior to their forrnal derivationas RunseKuttamethods. by At this point.5"yt The solutioncan then be generated as >> disP(tt.1): 0 1.As will be dernonstrated Section20. creatinga separate 20.4. Euler'smethod(Eq.4422 25.0000 2.If this occurs.4400 3. V ' ) 4*exp (0. Also noticehow the time stepis automatues of the independent and dependent values the vectort.the final value is addedto r so that the seliesspansthe completerange.8493 Although using an anonymousfunction is feasiblefor the presentcase. respectively. Notice how a function is usedto generate value for the derivativeat the appropriate a valvariables.0000 5. The initial value and the desiredstepsizearepassed y0 and as as h. variable The lunction first generates vector t over the desired a rangeofthe dependent using an incrementof t'r. a vector of the dependent with n values of the initial condition to improve efficiency. the differentialequationcan The ODE being solvedcan be setup in several be definedasan anonymous funcfionobject.However.Yl ) with the result(compare with Table20.5)is implemented a simpleloop: y(i+1) end = y(i) + dydt(t(i).First.v(i))*(t(i+l)t(i). the ODE from Example20.IETHOD IMPROVEMENTS EULER'S OF A fundamentalsourceof error in Euler's methodis that the derivativeat tlre beginningof the interval is assumed apply acrossthe entire interval.
is depictedgraphicallyin Fig.) /l (20.a l c u l a t e c l i n E q .7) r which is called a corrector eguation.(20.the slopeat the besinnins of an interryal )"i : .f(rr+r.488 INITIALVALUE PROBLEMS 2O.Yi)h (20. l6) Thus. y i l ' .16)] can be combinedto obtainan average slope for the interval.f (ti.14)and (20.to li+r using Euler's method: ' . FIGURE 20.vi) is usedto extrapolate linearly to y111 : ))+r:litJ'Gi.in Heun'srnethod the r ' .l'j!.1 Heun's Method One methodto improve the estimateof the slopeinvolves the determination two derivof ativesfor the intervalone at the beginningand anotherat the end.4. equatiott. 1 * . the two slopes[Eqs.r'r) .t . 20.15)is calledaprcdictor it 0. Recall that in Euler's method.3. It providesan estimate that allowstlrecalculation a slopeat the end of the interval: of : )'1+r . YP*) r : f(ti.calledHeun'smethod. l Q i .9+r) 2 This averageslope l s then used to extrapolatelinearly frorn 1'. )rrl:)'ir. .r'. However. f ( r .f(ri* r. 1 5 ) i s n o t t h ea'nn a l e r .Thisapto proach.f (rt*t.y i) . * . T h i s i s c t i sw why we havedistinguished with a superscript Equation(20.r5) For the standard Euler methodwe would stopat this point. ( 2 0 .) (20. .The two derivatives are then averaged obtain an improved estimateof the slopefor the entire interval.. _ /(r. .4 Grophicol depiciiono[ Heun'smelhod (o) Predictor ond (b) correctof = . ' 'l ) . b u t a n i n t e r n r e d i a t e p r e c l i c t i o n .4) r (20.
Note that because (20. 1 * ' ' ) (20. an old estimate an iterativefashionas indicated. 20.]'/ s f r o m t : 0 t o 4 w i t h a s t e p i z eo f L T h e i n i t i a lc o n d i t i o n aIt : 0 i s .4e08' .5 of of fhe Grophicol representotion iteroting corrector Heun'smethodto obloin cn improved estimcte.it can be appliedin Eq. The Heun method is a pretlic'tor. 5 ( 2 ): 3 is a Then. E m p l o ya s t o p ping criterion of 0.rg) ! ' o : 4 e " . conthat the iterative processdoes not necessarily respectively.4a): Preclictor Corector (Fig.v/*1 are the resulttiom the prior and the presentiterationof the corrector.r and.0 . a termiof nation criterion for conversence the correctoris frrovidedby tc t t#l"ouo" where1. is calculated .4b): y. it can be expressed as concisely (Fig.the slopeat (16.3 IMPROVEMENTS 489 FIGURE 20.lt should be understood with a finite truncationerror. in is The of an improvedestimate v.20. process depicted Fig.:1.as verge on the true answerbut will convergeon an estimate in demonstrated the following example.8) r h l'(rt. * .As just derived. 20.11. Use Heun's methocl .(orrectorapproach.5y Problem Stotement.OFEULER'S METHOD 20.5. 1 ' . First.9) r (for. .00001c/o terminirtetlre correctoriterations. to as Solution.o*r: y j " * y/+r : l'i' I f (ti. in of As with similar iterativemethodsdiscussed previoussections the book.2 Heun's EXAMPLE Method with iterationto integrate .f.0. 20.yi)+ / ( t .yi)h (20.19)haslial on both sidesof the eclual can be usedlepeatedlyto provide That is.2""'m) sign.0: v9:2*3(t):5 . r : 2 .the predictor usedto compute valueat 1.
19)]to give the prediction at t l.62 0 I '2 3 4 200000 6. 2 7 5 8I1 . I 9463 l4 84392 3 3.2 showsthat it corresponds a percentrelativeerror of 19.tB 994 la 46 t4. i : . 5 ( 5 ): 6 .28a/o.17581 1  I : 1001 6. / ( x r .19)to give 3 2 ..1.uo Jtiur".t 14"o t r t D.4e0E of ol 0 . 7 0 1 0 8 2 which represents true percentrelative error of 8.' r : l l '"  6 .' '> t.At this point.y : 2 o t t : O .2 With lterotion . l ' ): 4 e 0 8 ( r ) . usethe value. TABTE20.0 . the Heun methodwithout a iterationof the correctorreduces absolutevalue of the error by a f'actorof about2.l . i : 2 + 4 . to Now. 5 1 'w i t ht h e i n i t i oc o n d i t i o t h o t .18c/c.: l . l6 t .l ('hl le. of qnd with iierqtion TheHeunmethod wos imolemented without both o[ thecorrector. (20.2758 .3 percent an approximate 0f and enor il r082 1 6. 4 0 2 1 6 1 which can be combined with the initial slope to yield an averageslope over the interval f r o n rf : 0 t o l : r': 3 + 6..116(/t .0 .v. 5 ( 6 . l n ol were computed usingthe Euler ond Heunmethods with o stepsize 1.The true velluein Table 20. r . 7 0 1 0 8 2 (: ) .VH"un le. to improve the estimatefor . Without lterqlion Jt.4as the comparedwith Euler's method.1("/"1 B.r'f'to predict the slopeat the we end of the interval t . 5 I3 2 1 56 84931 2 00000 636487 268 I534224 309 3 47 4 3 2 8 3 l l 777351A 3 tB Note that this is the result that would be obtainedby the standard Euler method. 7 0 1 0 8 2 ) I : 6 . (20.102164 :4.6. we can alsocomputean approximate erroras l670roR)sl l r .Thus. 7 0 r 0 8 2^ ' " " l Now the estimateof r'1 can be refined by substitutingthe new result back into the righthand sideof Eq.'l whichrepresentstruepercent relative a I errorof 1.T h en u m e r i c v o l u e s .l("/"1 l9 28 2319 24 24 2454 2 00000 6 7010B I6 31978 37 19925 83 33777 l€.101082 ^ L This result can then be substituted into the corrector[Eq.l x 6.490 INITIALVALUE PROBLEMS Comporison trueond numericol of volues the integrol tr . 6 7 77 l 75 33896 200000 5 00000 t 140216 2 5.70 1 l c .
ft'tl J. (20.r . The approxirnate error will keep droppingas the iterativeprocess convelges a staon ble final result.382129 which represents true error of 3. [(t\dt (20.i+:l. In this example.ltri)f [(ri.22) .i+ l / Ittlrlt (20. (l7. l l ) l i s d e f i n e d s a '' [" f ( t i ) + J Q i + r ). 2 /.isely as . is For suchcases. Insight into the local error of the Heun methodcan be gainedby recognizingthat it is relatedto the trapezoidal rule.At this point. The connection between two methods the can be formally demonstrated startingwith the ordinary differentialequation by tl 1' : dt fut (20..ll)].5(6. recall that the trapezoidal rule [Eq.36087.which represents rrue rela ative error of 2.03voandan approximate a error of 1.r'i. ft'+t JI.20. the predictor step [Eq.3 IMPROVEMENTS OF EULER'S METHOD The next iterationgives )l 491 )^  Lr 3 a 4rrt'xrtt 0. ' f ( r ) d t : 2 (20.20)anc1 the trapezoidal rule [Eq.l8)] is not requiredand the corrector appliedonly oncefbr eachireration..2t) This equation can be solvedlbr.275g1 1) I :6.666vc. where the ODE is solely a function of the inclependent variable.after l2 iterationsthe approximateerror falls below the stoppingcriterion. l : l'.2 showsresultsfor the remainderof the computafionalong with resultsfor Euler's methodand for the Heun methodwithout iterationof the corrector.'.t J r. the result at r : I is 6.r). ( l 7 .the derivativeis a function of both the dependent variabley and the independent variable/. For casessuchas polynomials. (20. which yields .\.25) .20) Notice the sirnilaritybetweenthe secondterrnon the righthandsideof Eq.Table 20. . In the previousexample.23) . I J dt:l f t. techniqueis expressed the conc. + .t'.l'.687c.t': . frndt (20.24\ Now. (20.r'by integration: f .
4.20.this techniqueusesEuler's method to predict a value of . Because Eq. tl. the local truncation error is given by [r'ecall (17.6 illustratesanothersimple modificationof Euler's method. Heun's methodis sometimes refemed t0 as the trapezoidal rule.' i 1 1 1 2 ) 1 .Therefbre.y1. decreasingthe step size decreases error at a lasterrate than fbr Euler's rnethod. y  L Then. (20.(o) Predictor ond (b) corrector v = J ( .20.24)yields Eq.26) which is equivalenttoEq. i +t p) t:. l(titl J/rt.492 INITIAL VATUE PROBLEMS where/r : !i+1 . respectively.26) a direct expression the trapezoidal is of rule. (20. (.y af the miclpointof the interval(Fig. Subsriruring (20. Thus.1 1 .2 The Midpoint Merhod Figure 20.6 Grophico depictionof Heun'smethod.For this reason.27 ) wheref is betweenti afidt. tr (20. the 20.6a): . 1 i 1 1 2). . .f (. local and In the global errors are o(h3) and o(lf).:/ r ) ' i * J ' ( t .t+ r lz.tr2 l ( t .29) (20. this predictedvalue is usedto calculatea slopeat the midpoint: i )"t+r1 : . (20.28) FIGURE 20. acldition.Called the nitlpoittt ntethod.3. .20).25)into Eq. r ' i + 1 .14)l Eq.re methodis secondorder because second the derivative of the ODE is zero when the true solution is a quaclratic. r J f " ( \t \r 1a ' ' I ' (20.
Recallfrom our discussion numericaldifTerenof at tlre miclpointof the of finite differences betterapproximations derivaare tiation in Section4. fbrm of Eq. just as the Heun method can be called the trapezoidalrule. cenlered approximation In a tives thaneitherlbrward or backwardversions. t .20.t'ftia1P) It i J Q0. t h .4 RUNGEKUTTA RungeKutta (RK) methods achieve the accuracyof a Taylor series approachwithout Many variationsexist but all can be cast in requiring the calculationof higher derivatives. ' .r1is the midpointof the interval(n.a)J'(r) (20.31) fbr case. where. which can be interpreted a repl'esentative function can be written in generalform as over the interval. r . ) ' i+ t n ) l t (:0. (20.30)]cannotbe applied r'. Jt rh I J'trl dx =.u:k. the with the forward eror of O1h:1in comparison suchas F. : f ( t i * p 1 h . global erors of the midpoint method areO(h3) and METHODS 2O..24)yields Eq. As in our discussionof Heun's method. : j ' ( t i .q.i4) (20. z k z h k.30)...) ' i * q r r k r l t ) k . ..l a .6b): interval.Usingthe nomenclature the presenl h e e x p r e s s ea s d it can r l t t t . Thus. I * q. t . (20. .t. local and the of approximation Euler's method.a iterativelyto improvethe solutionas was donewith Heun'smethod.KUTTA METHODS 493 which is assumedto representa valid approximationof the averageslope for the entire linearly from /1 to /111(Fig.34b) (20. . f]tdrZh.(20.the corrector[Eq. where the a's are constants and the k's are k 1 : J ' Q . respectively.rh) (20.33) as slope where@is called an incrcment function. r k t hI q u .3:lc) (20.t ' i ) l . *Ii q t t k t l t i q n k z h ) : : f ' ( t . t. * p . Consequently.(b .4): the generirlized )r+r : )'i l.34d) . D).which has an en'orof O(h2).3.r0) Observethat because I is not on both sides. 20.4 RUNGE.34a) (20.i* q .This slope is then usedto extrapolate r'r+t : )'i I f Q i* t . .29)has a local truncation O (h).4 that the simplest NewtonCotes as which is calied the nridpointmethod. . k . (20. it The midpoint methodis superiorto Euler's methodbecause utilizes a slopeestimate predictioninterval.can be rcpresented open integrationtbrmula.32) Substitutionof this formula into Eq.Qh (20. the midpoint method gets its name from the underlyingintegrationformula on which it is based.t p z h . . 120. .4 that centered samesense. The incremerrt + Q : a r k tI a z k z ' ' ' ' . .the rnidpoint rnethodcan also be linked to NewtonCotes integration lbrmulas.Recallfron Table 11.
4. Thesesecondorder are terms with /rr andhigher to the differentialequationis quadratic.secondorder methodsusean increment the approach.valuesfor the a's. is.k1 +a2k)h (20. underdetermined. Threeof themost sults when (as is typically the case)the solution is more complicated.1 SecondOrder RungeKuttq Methods versionof Eq. commonlv usedand preferredversionsare presented . and qy1 are evaluated settingEq.in Section20. therefore.2.ln addition.1. Note that the firstorder methodwith n : I is.35c) (20. ln Section 20.Oncen is chosen.2006. and so forth.the local truncationenor is O1ft31and the globalenoris for O (h2).39) I /42 (20.or a constant. Notice that the ft's arerecurrence wherethep's and 4's areconstants.p's.the fourthorderRK method (n : 4) is presented whichthe g l o b a lt r u n c a t i o e r r o ri s O t f t l t . (20.4. three equations secondorder are (seeChapraand Canale. q's areevaluatedby settingEq.The threeequations four unknown constants a 1I c t 2 : l a z P r: : azQtr l12 I12 (20.By doing this.because droppedduring the derivation.Suppose fbr can be solvedsimultaneously ut:lo: (20. (20'35)equal a the can be derivedto evaluate Taylor series.this recurrencemakes RK methodsefficient calculations. pt.36)through the other three.) t pth.fbr details).and Thus. of the the at leastfor the lowerorderversions.4. methodswill be exactif thesolution function with two terms (n : 2).they linear. numberof termsn usuallyrepresents order RK For example. in ft1 appearsin the equationfor k2. solutionto the ODE were quadratic.38) are Becausewe have three equationswith four unknowns. computer numVarious types of RungeKuttamethodscan be devisedby employing different RK bers of terms in the increment function as specifiedby n. Euler's method.494 PROBTEMS INITIALVALUE That relationships. in fact. (20.37\ (20. thereare an infinitenumwe if RK ber of secondorder methods. n 20.35) (20. a2.35b\ to by The valuestbr ar.33)equal to terms in a Taylor seriesexpansion.theseequations saidto be of one of the unknownsto determine a must assume value We.Befor causeeach ft is a functional evaluation.40) Because can choosean infinite numberof valuesfor a2. (20. (20. Then Eqs.li+l :li where ky : k2: J'Qi' 1t. next.36) (20. which appears the equationfor ftr.33)is The secondorder .Every versionwould yield exactlythe sameresults the reyield different However.38) that we specify a value for a2. li i qlhh) f Qi l(a.
(20..4tb) N o t e t h a t f r il s t h e s l o p e a t t h e b e g i n n i n g o f t h e i n t e r v a l a n d k 2 i s t h e s l o p e a t t h e e n d o f t h e interval. +: ) ' i* i *2k:*2h*k+)h U(kr I (20. .. (20. thereforecall it the classicalfourthorderRK ntetlrcd: . p r : 4 t r : l 1 2 . Conseqrrently.44) .+ (]t. l t a 2 i s a s s u m e d t o b . T h e M i d p o i n t M e t h o d( o z: and Eq.42b) k2: f fti + h/2.(11 lf3 and 7r1 RK the secondorder algorithrus.As with the secondorder The following is the most commonly usedtbrm.r.41a) (20.2 ClqssicolFourthOrderRunge'KuttoMethod there approaches. / (20.20.40) can be solvedfor a1 : 1/2 ancJ : Qu : L Theseparameters.l'i+r: )'i'l k2h where / < 1: /(t. . r . Thisis themidpoint Rqlston'sMethod lo. Eq. : 1 / 2 ) If a2 is assumed tobe ll2.ftr \z + ljt . .35)becomes : . r.42a) (20.KUTTA METHODS H e u n M e t h o d w i t h o u f l t e r o t i o n( o . The most popularRK methodsarefourth order.e h e n a 1: 0 .l+' l ) r 20. Eqs. : 2/3).39) and (20. t .4.r'. \l1 .\'ijr:. (20. / ( r . n ' o r .35).t'i+[. yield /t .. J'i krhl2) * method.35)becomes . r ' i ) t k2: f ft.4 RUNGE. l t (2042) (20..4r ) where f t r: . secondorder this RungeKutta nethod is actually Heun's technique without iferationof the corrector. r ' .13a) (20.43) (20. +f. kth) (20. (20. Ralston(1962)and Ralstonand Rabinowitz(1978)determined that cltoosingo? :213 provides a minimum bound on the truncationeror for :7tt:314. .* /1. and we are an inlinite numberof versions.43b) k t : l ( t . ) (20.) 1 1 .+rr'. pt when substitutedinto Eq. : and For this version. v .rr) n where f t 1: / ( .
Equation(20.!i) (20.2\ : 3 . Employ the classicalfourthorder RK method to integrate I 4 r 0 8 t.As depictedin Fig.+hh) (20.n) t.y.r.3 r C l o s s i c q lF o u r t h .44a) (20.496 PROBLEMS INITIALVALUE i /' . 2 EXAMPLE 0. eachof theft's with an improved averageslopefor the a a represents slope.'.44) then represents weightedaverageof theseto arrive at the imProvedsloPe.ot where k1: f fti.44d) Notice that for ODEs that are a function of / alone. the classical fourthorder RK method is similar to Simpson's1/3 rule.44b) kz: f ('. 5 y f r o m / : 0 t o l u s i n g a s t e p s i z e o f w i t h y ( 0 ) : 2 .*)r.+1.t.44c) (20.k' tl+ \Alo \ 20.0 .*)r. *'/*<l L l "l *.n) ka:f(tith.i.!)' .: f (t.tI u: a'.the fourthorderRK methodis simito of lar to the Heun approachin that multiple estimates the slope are developed comeup interval. In addition. '.O r d eR K M e t h o d y'= Problem Stqtement. a: a: a. 20.the slopeat the beginningof the interval is computed : (0. For this case. +!.pt. as Solution.7 FIGURE melhod RK the comprising fourthorder estimotes of depiction theslope C..7.
912974 Next.4 RUNGEKUTTA METHODS This valueis usedto compute valueof .5 5 : k r : l ' ( 0 .t(.945611 : 4.. : /(.]l a/ l) (20.0 ..5.5 : 4 c 08 ( 0 ).4sd) (20. expected. 5 ( 5 ..and higherorderRK methods.5(4.f (0.91291 + 5.) jr'r k+: .r.912974 ) 4 e 0 8 ( t 0 ) . t i) r .45a) (20. + 2 r r ' n + r.45c) (20.4sf ) I .t.5):3.4sb) (20.lr^o +]r. . 2 1 1 2 9 9 ) 497 This slope in turn is usedto computeanothervalue of . ) .r'. Butcher's (196. * ). 9 1 2 9 7 4 ) : 5 .y(1. I It is certainly possibleto developfifth. :0.211 ] u 1.r.+) fiflhorder RK methodis written as )'i+r : )'i * where k 1: f G i . flnction evaluations n .108649) 108649) 3.0) : 2 + 4.vand anotherslopeat the midpoint: : r(0.3 . + ftr.. . slope is thenusedto rnakethe final prediction the end of the interval. 9 4 5 6 7 7 0 ka: f (1.'n hh) y(ti rr5: *ton. has Although the fifthorder versionprovidesmore accuracy.r. 108649 5) .4.92974 Finally.45e) (20.5): 2 + 1.n+ft*.+ + jr..2.20.45) t.n .' * Io.n) r o : f ( . i ) k:: .. 5 ) 4 . . this siopeis usedto compute valueof .0..(7kr * llAr+ 12k1* l2As 7k)h + (20. * + )0.5) 4.For example.r and a slopeat the midpoint: a l'(0.5.0) : 6. .201031 1) [3 + 2(1. * h . noticethat six tunction evaluationsarerequired.201031 .0. 103Vo).\'(1.5):2+3(0.5 ( 3 . \r.the four slopeestimates combined yield an average This average are to slope.0) 5. a and a slopeat the end of the interval. .211299(0. : : . : k7 : .Recall thatup throughthefourthorder versions.912914(1. at Q: 2e9)+ 2(3.0) 2 + 3. 5 .r(.rn) Note the similaritybetween methodandBoole'srule in Table17.19463 (e.1e08(0 .201031( which cornpares favorablywith the true solutionof 6. Butcher's As this rnethocl a global truncationerror of O (h5).
Suchsystems multaneous ordinary differentialequations generallyas be represented d)'t i : / 1 ( t . . conditions 20. the initial would be x(0) : u ( 0 ) : 0 .46) The solution of such a systemrequiresthat n initial conditionsbe known at the stafting value of /.For the freefall portion of the jump. two additional reltime. accelerastepsizeof 2 s.oneor acc0unt Because functionevaluations the are function evaluations necessary.the gravitational tion is 9. t.25kg/m. :!:! dt :. ordershigher than four.48) m If the stationaryplatform from which the jumper launchesis defined as r : 0. atively popularityof the fourthorderRK method.1 Euler'sMerhod to can be extended systems in All the methodsdiscussed this chapterfor single equations In equations. i. r . for Euler's method.1and 1. ) " : . ) . and thejumper hasa massof 68.4 M o S o l v i n gS y s t e m s f O D E s w i t h E u l e r ' s e t h o d jumper Problem Stotement. r ' . r : u : 0. : . jumper's velocity and positionthatwe set An exampleis the calculationof the bungee at the beginning of this chapter. .I kg with a dragcoefficientof 0. . r ' 1 . this problem up amountsto solving the following systemof ODEs: dx ' dt du dt wat 6 ) (20. ) ) i ) (20. . . is best illustratedby the following example 2 EXAMPLE 0. .(1. : .5 SYSTEMS EQUATIONS OF of requirethe solutionof a system siand science Many practicalproblemsin engineering may ratherthan a singleequation. methodsof orderfive and higherare usuallyconsidered for the most computation for This is one of the main reasons the lessefficient than the fourthorderversions.) 4z : J : ( r .Interestingly.2. and integrate / : l0switha usingEuler'smethod.8I m/sr. .498 PROBLEMS INITIAL.5..47) (20. . r .t. Solvefor the velocity andpositionof the freefallingbungee to Assumingthat at / : 0. \. 20.As was donepreviouslyin Examples1.) . ..f. . . .VALUE for are requiredlbr an nthorderRK method. Engineeringapplicationscan of equationssimply involves applyingthe each case. .l t . of involve thousands simultaneous of ODEs.) t .the procedurefor solving a system This to onestep techniquefor every equationat eachstepbeforeproceeding the next step. .
(1. uEul". 8 1 ( 2:)1 9 .25 ::::(142:9. (a) 0 2 4 6 B t0 0 1 91 6 6 3 7t 9304 147.1567a.3. respectively.1 Euler's methodis then usedto computethe valuesat t : 2 s.20.72919. The process can be repeated computethe resultsat t : 4 as to .r:0*19.4214 0 39.3 Distonce velocity o freeJolling ond of bungeeiumper computed os numericolly Euler's with method.n'J) Use theseanalyticalsolutionsto conpute the true relativeerrorsof the results. (x) e.r:0*0(2):0 u : 0 * 9 .(/ : l!!! 61nn( ) lE!!.9)l: for r. 6 2 The analytical solutions be computed can as.t(2) : 19. .I n l c o s h { .62(2):39.5 SYSTEMS EAUATIONS OF Recallthat the analyrical solurion velocityis tEq. r ( I ) : .\ V c.81 dt 0. in TABTE2O.240A I t2 A674 2A4 664A 3 0 54 2 4 4 o r9 6200 36.u" ur"u* rEul".z ) This result can be substituted into Eq. the percentrelativeeffors are 1007oand4. + t . 6 2 + f 9 .i \V . and Thus.41) which can be integratedto dererminean a n a l y t i c as o l u t i o n o r d i s t a n c a s l f e m f t .25 .16629 r'(2) : 18.l f ' r 1 9 . rt. 6 2 ) 2z : 3 0 .\ 1 9 .9462 2 3 75 ) 0 4 3 3 4) 7 8 2 00 t 8.4137 46 2983 50 I 802 5r 3t23 r 0 00 0 % 45 45% ?425% ]3 83% B72% 476% 9. e.81 68. The ODEs can be usedto compute slopes / :0 the at dr :U dt i::^ t il ' Gr. (20. 8 t .t \ l Proceeding a like mannergives the resultsdisplayedin Table 20. ll as du :::9.86% 3 B3:d . l o 8 ) 68. Soluiion.97% 1003% 6.24 u: / 0.7292 33 il tB 42 4762 469575 49. / a rl r l cl L \V.
! : O + O i : o 22 u ( r ): u ( o + r r . 8 1 ]: n . the resultsarenot very accurate . Finally. visualizing the proper way to do this for the fourthordermethod.r and u at the rnidpointof tlre first step: r r ' ( l ): r ( 0 ) f . . x. First. : .Thesenew slopes that are then taken back to the startingpoint to rnakeanotherset of midpoint predictions to make leaclto 1ew slopepredictionsat the midpoint (the l1's). solverprovidesdecentresultsevenwith a relatively using a higherorder 20.Thesemidpointvalpredictionsof the dependent ues are i1 turn usedto cornputea setof slopesat the midpoint (the ft2's).l 2 : 9 .Theseare then employed predictionsat the end of the interval that are usedto developslopesat the end of the interval (the fta's). variableat the midpoint of the interval. it is convenientto expressthe ODEs in the functional lbrmat of Eq. As iteration.1 the variable.. Use the fburthorderRK methodto solve fbr the sameproblemwe addressed Example 20. J'r(O. : 1 2 ( 0 . is the lth value of ft for theTth dependent first valuesof . (20.That is. the lowing exarnple EXAMPLE 20. 8 1 2 68. 0 . the fr's are combined into a set of increment functions [as in The fblEq.Next.. (20.In addition. t t ) . in Solution.VALUE for how Euler'smethodcanbe implemented Although the fbregoingexampleillustrates largestepsize.tr The first stepin obtainingthe solutionis to solve for all the slopesat the beginningofthe interval: kr. 8 1.:u' tn Ct t . r t:]o + 9 . described iargestepsize.0 ) : 9 .2 Runge'Kutfo Methods of RK methodsin this chaptercan be appliedto systems Note rhat any of the higherorder is helpfulin Figure 20. . u) : I . equations. because the of of systems ODEs. illustrates approach.9 J 1 t': {'t.fzj.r because does not changeuntil the second the resultsfor distanceare a bit unsatisfying next.soo PROBLEMS INITIAL.u ) : 1 .46)as dx E 0Il : f i ( t .caremust be takenin determiningthe slopes.5 RK Solving Systems ODEs with the FourthOrder Method of Problem Stotement.41))rhat are brought back to the beginningto make the final predictions. we must calculate where ki. .Using a much snrallerstepgreatlymitigatesthesedeficiencies. we first develop 's) Theseslopes(a setof ft1 arethenusedto make slopesfbr all variablesat the initial vahre.7 However. r . 0) : 0 k 1 .5.4.r : 0.
.t : . the fourthorderRK predictionsare much closerto the true values.4817 k3. 4 5 6 9 .Further.9 .thesecond'setmidpoint of slopes: : kt. 8 1 0 0 + 2 ( 9 .2 18.9462 2 3 75 1 0 4 3 3 41 7 8 2 0 )B7292 3318 42. 9 3 )I t47 9s21 2 3 75 t O 4 3 3 41 6 2 6 0 I8.0r 9% oo37% 0 051% a a49iL 0 038% 0 2 4 6 8 t0 0 1 91 6 6 3 71 9304 147. 9.1898 ka. 9 1 3 11 . 0 . 8 l o o .4427 .(2.4561 whichcanbeused. 1 6 5 6 6 u \ 2 ): 0 + . 9.8100. 9 6 3 4 1 2 : 1 9 . 9 6 3 4 ) 1 8 . ! : o + 9 .18.5 SYSTEMS EQUATIONS OF which can be usedto computethe first set of midpoint slopes: k z : : ..4567. [ 9 . 4 8 1 1 ( 2 ) :8 .4.8 : t'. 2 l t : 0 + 9 .u" e. In contrastto the resultsobtainedwith Euler's method. 9.4a)l: x ( 2 ): 0 + I +9 ) t 0 + 2 ( 9 . 8 1 0 0 : k 2 . .8100. 4 5 6 1+ 1 8 . t J t ( 2 .4561) These used determine predictions theendof theinterval: are to the at .9134. f t( 1 .4547 46. with RK ut. u(l): u(0) tz:i + 0+9. 8 1 0 0 . 9 .4561 : :9. 8 1 0 0 1 . .20.7256 330995 42.:9. 4 8 9 8 1 2 :1 8 . on Distonce velocity o freeJolling of os ond bungeeiumper computed numericolly thefourthorder method. nonzerovalue is computedfor distance the first step. 8 1 ): 9 . r ( 0 )* t .oilpur.a highly accurate. 2 f 2 ( 1 . 9 6 3 4 . 9.4567 : 18.o. fi(1.9134 x(2) : r(0) + k3.fr(1.4 rt.1h (2) u ( 2 ) : u ( 0 ) k 3 .004% 0 00r% 0 004% 0 000?{ 0 005% e.0 .456't) 9. 4 8 1 7 ) 8 . 9 6 3 4 1 + wltichcanbe used compute endpoint to the slopes: : : k q .9345 49.9634) Tlrevalues t canthenbe used compute to of [Eq.0762 46.(20..1 8 . ] 2 5 6 + 7 + 6Proceedingin a like manner for the remaining stepsyields the values displayedin Table 20.r656 7 ) . 4 5 6 1 ) Theseare usedto deternine the secondsetof midpoint predictions: 501 : 9 . 8 1 : 9 .0 + 9.9575 4 44 2 \ 4 0 19. : 8.u" JnKr unx{ TABLE2O.. (u) 0.(x) 0. r ( l ): .
error('ar arrJ I ). % times where solution output specific y0 = initial ? values of dependent variables % h = step size p1 . . while(1) Eend = t(np+1).I J a r t ' . end not ascendi ng order') .p2. FIGURE 2O. n = n+1.^ i ..  .:) i 1 .5. ^ h f f ^ . l . and final generaLed ac interval Z of h. J . € .502 INITIALVALUE PROBLEMS 20.).h L . end ti I F = tspan(1. 20. .eqrates lt. end y(I ..p1.fl. ' ^ l t L I l ' y .h.\ r r r r v o r a r v g rk4sys: fou:thorder RungeKurta for d system ol ODts = rk4sys(dydt. (Continued) .f t(n+1) = tf.For example.. 1 h .4.tf _ _ f = tspan(n) .yl Z a system of ODEs wit. This code is similar in many ways to the functiondeveloped earlier (Fig. if t (n) <t. yp(np.. L = (ti:h:tf)'.t(nP)... ' i ' ! N l b y 5 \ u l q L .' r . = additi"onal parameters used by dydt % % output: % tp = vector yp = vector % it r! h  of of independent variable solution for dependent variables redsL 4 . 4 y v ' L t t t ' . tt = tii = y(1.3 MATLAB Mfile Funclion: rk4sys Figure 20.tspan.:) = y0._O I a: /^r / nargin.h fourthorder RK meEhod ? inpuL: ?' dydt = name of the Mfile that evaluaLes the ODES tspan = tti. tfl. I ^ ^ .it is passed the function name defining the ODEs throughits argument.tL. or = ltO t1 . initial times wiLh output. ..) % : int.tspan. end else t .).8 AnMJile implemenl RK4 ihe for lo meihod o system oltCDEs. \ F + 1. np = 1r tp(np) . . hh = g(nP+1) . .F .3) to solve a singleODE with Euler's method. n = length(t). ? t.8 showsan Mfile called rk4 sys that usesthe fourthorderRungeKutta method to solve a systemof ODEs.y0.l t a ' t .p2. ^LrlJarrr c^rr\ landfh/f input 'rcn:r LJPcrr argumenrs requ ired' t.
end end np = np+1.ymid. .9344 49 .*hrh.we can developan Mfile to hold the ODEs: f unction cty = dydtsys (t. \.:) + phi*hh.OF 20.:) y(i+l. output valuesbetweentheselimits lf done in this way.1) Y( t. YP(nP. Veird=y(i. :). r656 1L./2.12a6 33. k4 = dydL(tL+hh. ymid = y(1.varargin{ phi = (k1+2*(k2+k3) +k4) /6. B \ .0000 0 19. brea]:.varargini ymid = y(i. older. k3 . t f I .4427 . whereri arld tf are the initial and final times.8 lContinued) output ln two ways.1 526 0 tB.:) i f tt>=t f. d y = l V ( 2 . end end = Y{1. hh = tendtt.0 . be in ascending We can employ rk4sys to solvethe sameproblemas in Example20j. . end while(1) i f ct+hh>tend.93II !47.:) FfGURE20.yend. 2 ' : / 6 8 .vararqrin{ : } )' . +k3*hh.0000 4.0541 45.0000 10. . 20. r ts ts  rlih ./2. Note that in both cases. : ] )' . } )' .:) + kL. Alternatively. if tt>=Lend.. y) ^21.952r a?? (141 334.you As dependingon how the input variabletspan is specified.tt+hY\/2.3. h]i = h.0000 6.you intervalsh. break.dydt (tt+hh.:) .2))\ 0 2.:) + k2*hrh/2.if at equalspaced the L span valuesmust can define r span = i t 0 . 1 * Y ( 2 ) as wherey ( 1) : distance(:r)and y 12) : velocity (u).0000 8.vararqin{ kl = dydt (tt. k2 = dydt .5 SYSTEMS EAUATIONS 503 i f hh>h. t 1 .'end : })' .0995 42 . was can set rspan = tri rf l. The solutioncan thenbe generated >> disp( [1' ]'(:. the routine automaticallygenerates you want to obtainresultsat specifictimes.respectively. However. .y1i. 9 . tp(nP) = tt.it has an additionalfeaturethat allows you to generate the casefbr Fig.ymid. First. = y(i.
0000 141.VALUE PROBLEMS We can also use t span to generate results at specific values of the able. respectively. tems of nonlinear ordinary differential equations.tt Lorenz developed theseequations to relate the intensity of atmospheric fluid motionr to tAmhArotrrra rrariotinnc 1r enrl 7 in thp qnA .50) where x and y ..504 INITIAL.The secondare equationsderived from tluid dynamics that are usedto simulate the atmosphere. thoseinvolving xy) are what make suchequations nonlinear.This case study focuseson two of these applications. dz :bz*xy .PREY MODELS AND CHAOS Engineers and scientistsdeal with a variety of problems involving sysBockgrcund.o y E u dt rx)Jcl.49) (20.A5t11 49. c : the predator deathrate.respectively.9521 10. The first relates to predatorpreymodels that are used to study species interactions.The simplest version is the following pairs of ODEs: dx E:axoxY dv E'Y+axY (20.2))) 000 6. These equations are commonly called LotkaVolterra equations.6 x . An example of a simple nonlinear model basedon atmosphericfluid dynamicsis the Lorenz equations createdby the American meteorologist Edward Lorenz: dx .a = the prey growthrate. >> tsPan=t0 >> disp(lt' 5 101.the number of prey and predators. For example.1) y(:. y(:.The multiplicative terms(that is. Predatorprey models were developedindependentlyin the early part of the twentieth century by the Italian mathematician Vito Voltena and the American biologist Alfred Lotka.0000 334.4021 PREDATOR.1626 42. and b andd : the ratescharacterizingthe effect of the predatorprey interactions on the prey death and the predator growth.
2.0625. Use numerical methods to obtain solutions for these equations. h = 0 .c " y ( 2 ) + d * y ( L J* y ( 2 ) ] . the RK4 method yields good results with the same time step.3.tspan.4 ) . andd: 0.V (: .9a) indicates that the amplitudes of the oscillations are expanding.plot (y( :. I t y l = e u l e r s y s ( @ p r e d p r e y . . the script also generatesa plot ofy versus x.'predaror' ) . Plot the results to visualize how the dependentvariableschangetemporally.b. The following parametervalues can be used for the predatorprey simulaSolution.h.6 CASE STUDY 505 coniinued with the predatorpreymodel. y 0 = [ 2 i l i a = L . c. This decrease.y(:. In addition to displaying the solution as a timeseries plot (.2.r and y versus /). t s p a n = t 0 4 0 1.'' (a) Euler tine plot' ) legend('prey'.b :0. the nonlinearitiesstem from the simple multiplicative terms: xz and xy. In contrast. c = 0 . At a certain point. 20. b = 0 . c: 0.9.9b). the prey grows exponentia\.2). turn.c.b.6.Notice to a decrease . 2 .2 ) . 20.a. This is plot (Fig.L s p a n .d) y p = [ a * y ( 1 ) . Employ initial conditions _r:2 andy : I and integratefrom t: 0 to 30.title(' subplot \2. t .1) .plot (y ( : . a . B. a cyclical pattern emergesin time. d = 0 . subplot (2.y.2. 3 . d ) . we can develop a lunction to hold the differential equations: function yp = predprey {t.y ( .Y {:. the increasedpredatorscausethe prey to decline. because of its much smaller truncation error. Hence.2.L) . that. 20. as expected' the ofthe predators. c .plot(t. Because the predator population is initially small. b .Eventually.2) .d) . First.20.We will leavethe development suchan Mfile as a homework problem. .1) . the prey become so numerous that the predator population begins to grow' leads in Eventually.b * y ( 1 ) * y ( 2 ) .2. y 0 .v ( :.9c.plot(t. '') title (' (c) RK4 rime plot') subplot \2. theseresults indicate that the crude reinforced by the phaseplane Euler method would require a much smaller time step to obtain accurateresults.Y (: .L).21  title (' (b) Euler phase plane plot') tt V) = rk4sys(GpredPreY.8. 20.3 ) . 6 .Note that the function eulersys was basedon modifyof ing the rk4sys function (Fig. Such phaseplane plots are often useful in elucidating featuresof the model's underlying structure that may not be evident from the time series.t. 20. a. using a step size of h:0.8).21 j title ( ' (d) RK4 phase plane plot') The solution obtained with Euler's method is shown at the top of Fig. 1). The time series (Fig. of tion: a : 1. 0 6 2 5 . h . As in Fig. ) subplot (2. In addition. The following script employs this function to generate solutions with both the Euler and the fourthorder RK methods. graph the dependent variables versus each other to seewhether any interestingpatternsemerge.y0. the processrepeats.
II I ll tl tl tl ll t t I I I I I I I I t\ r I lt tl . y) = (0. Also.VALUE . alb). The exact location of this point can be determined by setting Eqs.and r : 28. for The phaseplane representation the accurateRK4 solution (Fi9.ii* continued 10 20 30 ( a )E u l e t i m ep l o t r 5 10 ( b ) E u l e rp h a s ep l a n ep l o t I I I .] I I . RK4 meihod timeseries ld)phcseplone ond predator peak lags the prey.49) and (20.50) to steadystate (dy ldt : dx /dt : 0) and solving for (x. ! Now. Interestingly. nothing will happen. PROBTEMS INITIAL. i +"t '. The formeris the trivial result that if we start with neither predatorsnor prey.9Q indicates that the interaction between the predator and the prey amounts to a closed counterclockwise orbit. let's use the sameapproachto investigatethe trajectoriesof the Lorcnz equations with the following parameter values:a = 10. (20.b = 8I 3. it repeatsin a set time.20. I I I I t I I I I t I I I 10 20 30 (c)RK4 time plot 40 24 (d) RK4phase plane plot FIGURE 2O. the derivativeswill be zero. :i. observe that the processhas a fixed periodthat is. 0) and (cld. there is a resting or critical point at the center of the orbit.Thelatter is the more interesting outcome that if the initial conditions are set at x:cld and : a /b. Employ initial conditions . method fimeseries {b)phoseplone ond ond plots. andthe populationswill remain constant.s06 ti.9 Solution theLoikoVolterro Euler's for model. {o) (cJ plots.
Although the solutionstrack on eachother for a time.the variable. 5). An interestingfeatureof suchsolutionscan be illustratedby changingthe initial condition for "r slightly (from 5 to 5.r seems be undergoing almostrandompatternof oscillations. slighily i s 0 0 r. this led Lorenz to the conclusionthat longrangeweatherforecastsmight be impossible! .The resultsare superimposed the dashedline in as Fig.Forthiscase.As in Fig.Thus. The results are quite different from the behavior of the LotkaVolterra equations.The term chaotic is used to describesuch solutions.In his original study. The other variablesexhibit similar behavior. solidtimeseries for the The is initiol conditions 5.' .r is perturbed condttion {5. 0 0 1r. 5 . to an bouncing around from negative values to positive values. 5 ) of"r:.001).wewillusethefourthorder RK methodto obtain solutionswith a consrant time stepof h :0. after about / : l5 they diverge significantly.03125. the frequency of the oscillation and the amplitudesseemfairly consistent. 20.IO Timedomoin representotion versusfor fheLorenz of x t equotions. 20.20.6 CASE STUDY 507 confinued 20 E Lorenz model "{ versus r 15 5 .10.10.: : 5 10 5 10 tc 20 FIGURE20.)':z:5andintegratefrom/:0to20. we can see that the Lorenz equationsare quite sensitiveto their initial conditions. even though the pattems seem random. However.Thedoshed is wheretheinitiol line for.
) .xz.y. Beyond the initial conditionsthemselves.Someof the problemsin this chapterand in Chap.on fhe Lorenz Phoseplone for (oJ eqL.1i. As a final note. and the yz planes.10.2) . 21 are designedto demonstrate this issue.VALUE PROBLEMS continued (a) y versusr (b) z versusr 10 n 10 45 40 35 30 a25 (c) .y(:. we can generate projections. 20.ylabel('y' ) .2) ) >> xlabel('x' ) . Thesepoints are called strangeattractorsin the jargon of mathematicians who study suchnonlinearsystems.The idea is that rhe flapping of a butterfly'swings might induce tiny changesin the atmospherethat ultimately leads to a largescale weather phenomenon like a tornado. thesediscrepancies will eventuallylead to large deviations. Beyond the twovariableprojections. differentstepsizes or different algorithms (and in some cases.even different computers)can introducesmall differencesin the solutions.Figure 20.12) depictstrajecrories cycling in a definite pattem arounda pair ofcritical points.In a similar fashion to Fig.r:.L) . Although the timeseries plots are chaotic. the sensitivity of chaotic systemsto initial conditions hasimplications for numericalcomputations.I I represeniot. 20. (a) (b) (c) (d) .Noticehowa structure is manifest when perceivedfrom the phaseplane perspective. MATLAB's p1or3 function providesa vehicle to directly generate threedimensional a phasepiane plot: >> plot3 (y(: . versus! 45 40 JU 25 20 l5 1q 20 10 5 20 10 0 "r 10 10 5 40 20 0 ) 20 FIGURE 20. intt res As was the casefor Fig. the threedimensional plot (Fig20.508 INITIAL. Because we are dealing with three independent variables. phaseplaneplots reveal an underlying structure.z1abe} ('2. (bl .y(:.11 showsprojectionsin the x.rotions rr. ond (cJyl proiections The sensitivityof a dynamicalsystemto small perturbations its initial conditions of is sometimescalled the butte(Iy ffict.The solutionforms orbits around what appearto be critical points.grid 20.
4 0 20 FIGURE 2O.1r " r I I ).2 Solve the following problem over the interval from r : 0 to I usinga stepsizeof 0. UsineRalston's method.\ r'r'v/ /i. .l. Using the midpointmethodu'ith ft : 0.12 Threedimensionol phoseplone representoiion Lorenz forlhe equolions generoted MATLABs with p}ot 3 iunction. Usingthe fburthorder methodwith i : 0.25. Using Heun's methodwithout iteration.1 Solve the following initial value problem over the interval frorn / : 0 to 2 where r'(0) : l Display all your resultson the samegraph.5. .5 and 0.5. :. 20. Using the fburthorderRK method. Display all your resultson the sameglaph. Using Euler'smethod.PROBLEMS 509 K. Using Euler'smethodwith /i : 0./l l\.25 wherel (0) . (a) (b) (c) (d) Analytically. PROBLEMS 20.l*lin conrinued t'\ 30 20 10 v0 l I I l\l tI lI I l \ _10 20 2n I rl tt_ t 40 . RK (a) (b) (c) (d) (e) Analytically. dr' dt :\.
x: altitude (m) measured upward from the earth's surface.. and px (in 1950):2.r(l .5ro INITIAL. Kdlf)\o"'(R+. Employ the tbllowing iteratingthe conector.VALU E PROBLEMS This is referredto as the logistic model.".the growth rate approacheszero.1) . Under theseconditions. = p 4 4 5 4 4 8 5 0 5276 5686 6079 1400m/s.the growth rate is not a con20...and (d) Ralston's methc)d. 2050 with a step sizeof 5 yeals.4.5 wherey(0) : l. 1*t ) ..5 Although the model in Prob. and /max: the rnaximurn population.1) holds. p 2 5 5 5 2 7 8 0 3 0 4 0 3 3 4 6 3743 4AB7 e n d R : t h e e l r t h ' sr a d i u s{ } 6 .O dtt . use Euler's method to deterrnine the I t 9 B 0 t 9 B 5 I 990 I 995 2000 maximum height that would be obtained if u(t = 0. is sontetimes called the curning t'upucin.The analytical solution to this modelis yP\l 2{). the rate of changeof populationcan be modeledas dp dt : * c .9 Given the initial conditions. gineeringand scicntiflc applications.tt The van der Pol equation is a model of an electronic stant. + kr.1%.t.h 3 factors such as fbod shortages. ) where t. Thus. pollution.0261yr. on d:" dt ot Pmax Po * (2nr"" Po)e kc^t Simulate the world's population from l950 to 2050 using (a) the analytical solution. ln such cases.4 works adequately yawhen population growth is unlinrited.000million people. 3 7 z 1 0 6m ) . L The initial conditions are .555 million people.p/p. 20. Assumethat the only force actingon theobproportionalto the existing populationat any time /: ject is the downward force of gravity.4. Obtain your solution with I 950 throughI97Oto estimate ks. solve the fbllowing initialvalueproblem from t : 0 to 4: r/l l lQ\. 20. dl 20. Plot both solutions thesame on grrph.4 using a step size of 0. 1(0) : )'(0) : 1.6 Suppose rnodelsassumes that the rate ofchange ofthe population is 7' earth'ssurface. =6.it breaksdown when .3 Solve the iollowing problem over the interval fi'om I :0 to 3 using a stepsizeof 0..t4et results alongrvith the dataon a plot.J The growth of populationsof or.(0) : 4. rnd (b) the fourthorderRK Obtain your solutions with (a) Heun's method without method with a step size of 5 years. lions from 1950throush2000was for Pl so 20 m( pl( po 5' 2tJ mi of uIr 5.. < 0. 2A fb al Pr 2( Er rh 2( pe is b1 ta Si h: (l at' .Note that 2n. Disof (a) to stimulate the world population from 1950 to play your resultsas a plot. : kr. ( l . a fbrce balancecan be usedto derive dLt (P20.p f 1 t .Display your simulation (/l' :21. and lack of space inhibit growth. k. ."*. at low popr"rlationdensity p {{ p^u. (b) Use the fburthorderRK method along with the results (a) Euler's methodand (b) the fourthorder RK method. p . 20.:0 dt . t : time (s)..r12 tlt where ftq : the growth rate. 20.i.kgtn:0. : the maximum growth rate under unlimited conditions.8rnAr).population.. One of the simplest that a projectileis launchedupwardflom the 20.rr.along with the datafiorn Prob. (b) Heun's method with iteratingthe initial conditionsand parameter valuesfor your simulation: correctoruntil s. Given the initial conditions. (P20. f t. Display all your results the samegraph.y(0) : 2 and .'10. The world population in milwhere u : upward velocity (m/s)..t2.: kpp ul dt. .r'') .. (c) the midpoint method. Using this growth rate fbrmulation.4. As p approaches7r.Display your resultsasa plot has 20. )(0): I and 1.. R e c o g n i t i n g that dxldt: u.g(0) : the at r 1 9 5 0 r 955 I 960 r 965 lsTa 1 9 7 5 gravitationalacceleration the earth'ssurface(? 9. use the data from frorr r : 0 to 0..but can be formulatedas circuit that aroseback in the days of vacuumtubes: k. .7 Solve the lbllowing pair of ODEs over the interval (a) Assumingthat Eq.2 ancl(b) 0.ganisms rrany en. . . solvethis equationfrom 1 : 0 to 10 using Euler's methodwith a step sizeof (a) 0. pmrr : 12.
15 The motion of a damped springnrass system theresults.6) liom 1960through2020 using the tbllowing coeflicient 5 years.14 Isle RoyaleNationalPark is a 2[0squaremile pelagocomposedof a sinele large islandand many snrall dx d2x lr. 20. wherex : displacement tation.. : 20.19.Tesl your program by using it to solve residualsbetweenyour model and the data for both the a stepsizeof 0.+t .Designthe Mtile so that it creates plot ol 20. their population b1 l910.the numbersof the mooseand wolves time (N .In 19.12 Develop an Mfile to solve a single ODE with the Compare your simulation with the data using a tinreRK method.Testyour prograrnby using it to solvefbr population as describedin Prob. The damping coefficientc takes on three values havebeen tracked.7 20. Employ a step size of as and iteratethe correctol'until c.Employ Prob. a stepsizeoi 0. 0 1 3 3t. P20. < 0' l7o. LltdI vegeravaging rtpprouched.Designthe Mfile so that it creates plot miclpoint of theresults.i000. (Fig.10 Developan Mfile 1tlsolvea singleODE with Heun's methodwith iteration.PROBLEMS and (b) the Obtainyour solutionswith (a) Euler's rr.i:0 Moose arrived around 1900.11 Develop an Mtile lo solve a single ODE with the l method. 20. 20. 5 1'eiirs 20.+k.Design the Mfile so that it creates tburthorder of seriesplot and determinethe sum of the squares the a plot of the results.5. 0 0 0 4 . entialequation: archi20. v a l u e sa : 0 .5.25. RK fourthorder method.rethod use In both cases.Design the Mfile so that it createsa plot of the results. from equiliblium position(m)' t : wolves crossedan ice bridge from Ontario. ' 1 a n d d : 0 . 20.I5 (a) tntegratethe LotkaVolterraequations (Section 20. plot a Euler'smethod.Test your programby using it 1osolveProb.193 1435 1467 2A 22 22 23 20 26 2B 26 22 22 l7 IB 20 23 24 3l I001 I028 9r0 863 872 932 I038 ll t5 ) t92 t268 r3 3 5 I 397 1216 4) 44 34 40 43 50 30 l4 23 24 22 2A t6 t2 t2 l5 t3r3 r590 lB79 l77A 2422 I 163 500 699 750 B5O 900 I 100 400 7 5Ct 544 450 2005 2046 12 l2 t3 l7 t6 22 24 l4 25 2a t9 t7 l9 29 30 30 . s/m).t' 20. Employ a step size of 5lt FIGURE P2O. I . .and c : the damping coefficient Sincethe late 1950s. Plot both solutionson the same graph along with the exact solution : cos34. . nrclose and the wolves. and islandsin Lake Superior'. 0 : 0 . rn :20kg mass.Test your programby using it to solvefor population describedin Prob. (s). Yeor Moose Wolves Yeor 5 197 1976 1977 t97B 1979 I980 r 9 8I t9B2 I9 8 3 lsB4 I985 I986 l9B7 ]9BB I989 r 990 Moose Wolves Yeor l 991 1992 1993 1994 I 995 1996 1997 I 998 I 999 2000 200l 2AO2 2003 lvJ4 Moose Wolves I 959 I 960 196l 1962 I963 1964 I 965 1966 1967 I968 1969 t97a ) 9 7l 1973 1974 563 610 628 639 663 747 733 765 9)2 la42 I 268 )2c)5 I 439 1.: : 0 .. l.2.l5) is describedby the fbllowing oldiniuy difler$ith a stepsizeof 0.13 Develop an Mfile to solve a systemof ODEs with (b) Developa phaseplane olyour solution. 2 3 .
.16 A sphericaltank has a circular orifice in its bottom through which the liquid flows out (Fig.l 0 ( T .tration and temperatureol' the reactor as a function of time.18 The reaction A + B takes place in two reactors series. and tlre initial displacement : I m. of lnitially. 20..16). Plot the displacement versustirne for eachof the three valuesof the dampingcoefficient on the sameplot.The reactorsare well mixed but are not at steady massbalancefor eachstirredtank state.is the constantambi.2 Und diff( ! ( whe A(h gra\ outl area min h(0" h.and k : the rate constantfor reaction ofA to C AnEii Q. CB r : concentration B at of the outlet of the first reactor(andinlet of the second).und h: the depthof liquid in initial conditions of all the dependentvariablesarezero.^^^ . and i". Use t : 0. Note that the orifice hasa diameterof 3 cm and C: 0.n A(h 20. the reactor at I 5 "C and is has known as Newton's law of cooling. ent temperature: 2(1. of 5 (underdamped).) flection of a sailboatmast subiectto a wind force: (1)' t ) where K > 0 is a constantof proportionality. and that two hours later.From the dT . This is perature the reactor./ (a )  .The spring constantk : 20 N/m.kCB: T where CA6 : concentration of A at the inlet of the first reactor.Supposethat at time I : 0 a corpse is discoveredand its temperature is nreasured be I.: 1 0 0 0 e ( .The unsteadystate reactoris shownbelow: ICA' I :(CAuCA:)kCAr . : the areaof the oril'ice A tion. Find theconcenperatureof the object at time /. atr FIGURE P2O. find the concentrations is of.r: produceB. I : CBtIkCAt . P20.5"C.the to oa 1. . (a) DetermineK and the time of death. = CB2 concentration B in the secondreactor.lf C:Ao equalto 20. E : modulus of elasticity.20The following equationcan be usedto modelthededT . in 20.512 INITIALVALUE PROBLEMS Suppose that the temperature the corpsewhen it wasdisof coveredwas 29. is The ambient temperature 20 'C. if f(/) is the tem.^n+ ^f :^1:\T^r^ ]L^* +L^. is known that the surfacetemdI peratureof an object changes a rateproportionalto the difat ferencebetweenthe temperature the object and that of the where C is the concentration the reactant I is thetemof of and surrounding environment or ambient temperature.4 where Qou. : residence of r time fbr eachreactor.it is 23.5 "C.2 gair enc sho this stru dev 7. = I ^A I ff^'. C : an empirically derived and B in both reactors during their first 10 minutes operaof (mr).15 m. :K(T dt Calc Use I: 20. We assumethat at the time of death. dC . (critically damped).CB:l . l2lmin and r : 5 nrin and assume the that (:9.ssnssrtration ofA at the outlet of the secondreactor. 20.The flow rate through the hole can be estimatcdas dCB. Thus.19 A.r this equationusing a numericalmethodover the time period 0 < / < 15 s. lional constant the tank.I6 A sphericol tonk. (b) Solve the ODE nunerically and plot the results.81 m/srt.17 ln the investigationof a honricideor accidental dl it is often important to estimatethe time of death. Solve .55. The initial velocity is zero.s6nqsrtration ofA at the outlet of the first reactor (and inlet of the second)...2 0 ) it experimeutalobservations.t ) / ( T + 2 7 3 ) ) g death. I j:(CAtCA)kCAt dt T J dCBt :.. nonisothermal batch reactor can be described in by chapter determine to horvlong it will takefor the waterto flow the following equations: out of a 3m diametertank with an initial height of 2.a concentrationof reactantC of 1.r 0 / ( r + 2 7 3 ) ) C.0 gmoyl.3 : the gnrvitacocfficient.: outflow (mr/st.  11 ) tl:: where /(l) ?E I : wind force. Use one of the numericalmethodsdescribed this 20. CA2 ._ _ r \ .: dl I tCBt .and 200 (over40 damped).CA 1 . aIt varl( dCA.
law. 20..forcebalances be UsingNewton'ssecond developedfor this systemas dtr Simulatethe dynamicsof this structurefrom . 2 5m . ffr A(h). fbllowing the differential how depthchanges equationdescribes with time: ' 1A(h)' (nr).96. dr h.tt ( r 2 l. Compare but vour resultswith thoseobtainedin Section20.. utt : \ . : 0 to 20 s. arrd x / : 0. Undera numberof simplifying assumptions.6 for the Lorenz equations but generate solutionswith the the rnidpointmethod. dl' .rr) ]tl t.\\)  )l) dt: t ) t tn: K1 . m1 m2 A'x3 /1t . u.+: = rrr Ilt t ^) :(. and c : depthof pipe outletbelow the pond bottom (m).\r ltr (l * j('r: .PROBLEMS 5r3 __t_ e d P2O. e : I m .t) .000 kg ) .the analysisis limited to horizontal motion of the can structure. n: = 8.21.000 kg ftr 1800kN/m C a l c u l a tte e d e f l e c t i o nf r ' : 0 a n dd v l d z : 0 i h at z = 0. /q\tl Le) qT0\= / 7 r r 1 2 1 0 0 0k g k.21A ponddrainsthrougha pipeas shownin Fig. For this case. two timeseries and phaseplane plot of In addition. 20. fbr shorvssuch a representation a threestorybuilding. d : 0 .tu f. P20.25 108.05 for your computation. solve this diff'erentialequation to determine how long it takes tbr the pond to empty.al: pipediarneter /i A\h): pond surf'ace areaas a functionoldepth (m2t.r1/th : I m/s.tt.6 for the Lorenz equations use a value of r :99 .here : depth (m).1 { . 2400kN/m qTn'r FIGURE P2O.22 ence of disturbancessuch as earthquakes. r: time (s).2I FIGURE varies with height accordingto " 200.22 Engineers and scientists gain insight into the dynamics of structures under the influFigure P20..  . ( 0 ) : 6 r n . / T.developa threedimensional the displacements.22 k1 3000kN/m 543210 a97 A67 445 A32 01B 0 use massspring modelsto 20. m2 6 I l7 .g : (: grar.'itational constant 9.6. L]"il1\ = In2 10. E : 1. siven the initial condition that the velocity of the ground floor is d.81 m/s2).1. and all other initial values of displacementsand velocitiesare zero. ^l {. given that l . Ure paranreterl'alues of L :30.23 Repeattlre the sarnesirnulationsas in Section 20. Basedon the following areadepth table.24Perfornl the samesimulationsas in Section 20. Presentyour results as plots of (a) displacements (b) velocities. 2(1.
If a constantstepsize wouldhave were employed.1. Fig. . objectivesand topics coveredare r r o o . 21.However. a for that a fairly large step size could be employedto obtain adequate The an localizedregion from t :  .the srnallerstep size requiredfor the region of abruptchange As a to be applied to the entire computation. Learninghow to adjustthe optionsfor MATLAB's ODE solvers. we have presented problems. For a signiticant nuinber of solution of the type depicted in example.KUTTA methodstor solving ODEs that employ a constant step To this point.supposethat we are integratingan ODE with a gradually. much smallerstepsizethan of necessaryand.25.Adoptive Methods ond StiffSystems CHAPTER OBJECTIVES methods The primary objectiveof this chapteris to introduceyou to more advanced Specific for solving initialvalueproblemsfor ordinary differentialequations.For most ofthe range. for the and multistepmethods solving Understanding differencebetweenonestep ODEs. o of how the RungeKuttaFehlbergmethodsuse RK methods Understanding to that are usecl adjustthe stepsize. 2I.l 5 to 2. therefore.the solutionundergoes abruptchange. practical consequence dealing with such functions is that a very small stepsizewouldbe of algorithm required to accuratelycapturethe impulsive behavior. Understanding what is meantby stiffnessand its implicationsfor solvingODEs. difl'erentordersto provide error estimates Familiarizing yourselfwith the bLriltinMATLAB lunctions for solvingODEs.I METHODS ADAPTIVE RUNGE.this can representa selious limitation.the solutionchanges Suchbehaviol'suggests results. a consequence.many more calculationswould be wastedon the regions gradualchange. to Learninghow to passparameters MATLAB's ODE solvers. For size.
^^o.I 21.This error estimatecan then serveas a basisfor either shortening lengthening stepsize. Algorithms that automatically adjustthe stepsizecan avoid suchoverkill and hencebe of great advantage.The evaluationof the definite integral t: _t rb Ja I J$tdx is equivalentto solving the differentialequation dy /rr.1 ADAPTIVE RUNGEKUTTA METHODS 5r5 FIGURE I. . Implementation suchapproaches of requiresthat an estimateof the local truncationerror be obtainedat each step. the methods describedin this chaptercan also be usedto evaluatedefinite integrals. or the Betbre proceeding.o. Thus.r ' o r o f o n C D I t h a le x h i b i t s n o b r u p tc h o n g e . .we should mention that aside from solving ODEs.r^^. once as a full stepand then as .r<lmant hn< nrnnt ^l.Stephalving involves taking eachsteptwice.lr'Urgiventhe initial conditiony(a) : 0. Because they "adapt" to the solution'strajectory. . suchcoses. they are said to have adaptivestepsii. There are two primary approaches incorporate to adaptivestepsize control into onestepmethods.I 2 A ^ e x o m o o i o s o. {^.A u t o m o i i c t e p s i z e e o s nli. the fbllowing techniques be emcan ployed to efficiently evaluate definite integrals involving functions that are generally smoothbut exhibit regionsof abruptchange.e control.
kt)lt (2 .gk+)h /l (71. the approach yields a predictionand error estimate basedon threeevaluations rather .nvented problernby deriving a fifthorderRK this methodthat employsmost of the samefunctionevaluations requiredfor un accompanying fourthorderRK rnethod. The embedded methodswere first developed Fehlberg.(20.they aresometimes by refen'edto as RKFehlbergmethods.l MATTABFunctions for Nonstiff Systems Since Fehlbergoriginally developed approach.. facevalue.1) r t1 : .2) (2t.) ' i + t ) lr. For example. Shampine.Hence.r. thereare really only three because after the first step. The ode23 function usesthe BS23 algorithm (Bogacki and Shampine. The step size can then be adjusted basedon this error estimate.andfifthorder predictionamountsto a total of I 0 function evaluations step[recallEqs.* ' r r . Fehibergcleveriy circur.the ideaof usingtwo predictions difAt of ferent order might seemtoo computationally expensive. Thus.1994).2h t 3kz 1.a fburth.. 1989. Severalof theseare availableas builtin functionsin MATLAB.45)1.r) (2t. In the secondapproach. r . The formulas to advance the solution are J'i+r : li t where I n(.tb) ir") ( 21 . ode23. Thus. his other even betterapproaches havebeen developed.r.44) per and 120. local truncation RK the enor is estimatedasthe differencebetweentwo predictions usingdiff'erentorder methods.c ) 1 I * (*5kr t 6kz + 8k1.*)r. t r : f ( r . the approach yieldedthe error estimate the basis only on of six functionevaluationsl 2l.the k1 for the presentstepwill be the ftafrom the previous step. The error is estimated as Er+t : where k q : ..calledetnbedded methods.) (2Lla) r.516 ADAPTIVE METHODS AND STIFF SYSTEMS two half steps./(r. The differencein the two resultsrepresents estimateof the localtruncaan tion eror.2a) Note that althoughthereappearto be fbur function evaluations. RK These are cun'entlythe methodsof choicebectruse they ale more efficient than stephalving. f ( t i + t .:f(.which simultaneously usessecondand thirdorderRK fbrmulas solve to the ODE and make error estimates stepsize for adjustment.l.
ode45. The ode45 functionusesan algorithmdeveloped Dormandand Prince(1990). r.3390 As described in the following example.y) 4*exp(0. Herc is an example of how ode45 can be used to solve a single ODE. tspan. by which simultaneously usesfburth.r. After each step.I that the analyticalsolutionat t:4 is 75. k+ becomes lbr the next step.y1=66"4!.3) j) (default: l0 where RelTol is the relativetolerance andAbsTol is the absolute tolerance (default : l06).MATLAB recommendsthat ode45 is the best function to apply as a "first try" for most problems.to obtain solutionsat specific times t0. fspar Ltt tf).2I .If the error and k1 with reducedstepsizesuntil the estimated is too large. fiom /:0 to 4 with an initial conditionof y(0):2.'1 ocle45(odefun. the integrationis perfornredfrom ti to tf.Note or n'rethod we will describe in that this is a multistep as subsequently Section21.'d) variables and each wherey is the solutionarraywhereeachcolumn is one of the dependent vector t. the ODE is typically stored in its own Mfile when . >:. It. the stepis repeated error satisfies E < n t a x ( R e l T ox l y l . odeL13.(dydt. Note that . A b s T oIl l (21.5.andfifthorder RK formulasto solvethe ODE and make error estimatesfbr stepsizeadjustment. the value of y. . It is useful for stringentemor tolerances computationallyintensiveODE functions.2.lA 4).5*y. Representing ODE as an the anonymousfunction. Observethat the criteriafor the relativeeffor usesa fractionratherthan a percentrelativeerror as we havedone on many occasions prior to this point.33896.If it is.0.the error is checkedto determinewhetherit is within a desiredtolerance. Thesefunctionscan be calledin a numberof differentways. Recallfrom Example 20. Second.8*L) 0.and y0 : a vectorcontainingthe initial values.First. odefun is the name of the function row corresponds a time in the column to returninga column vectorof the righthandsides the difTerential of equations. span spect ifies the integrationinterval. The ode113 functionusesa variableorder AdamsBashforthMoulton solver. if it is entered a vectorof two as numbers. = t. >> y(Length(tt ) 75. tl tn (all increasing all decreasing). or use tqpal = ltA t1 tn).a1 is accepted.21 .The simplestapproach is Ir.I ADAPTIVE RUNGEKUTTA METHODS 5r7 than the five that would ordinarily result tiom using second(two evaluations) and thirdorder (tlrreeevaluations) RK formulasjn tandem.r": 4e08t. ode4 5 can be usedto generate sameresultnumericallyas the >> dydt=G(t. spancan be lbrmulatedin two ways.
81'*0. enter the following commandsto specify the integrationrange and the initial conditions: '.l 2l ADAPTIVE METHODS AND STIFF SYSTEMS Using MATLABto Solve o Systemof oors ProblemStotement. Next. >> ya = 12.3*y(1) *y(2) I . dt'' dv.2ytE: : where . spdn fn 20 . Before obtaininga solutionwith MATLAB. The results canbe displayed by typing simply >1 plot(t.1'2 1 at 1 : 0.m over the range definedby tspan usingthe initial conditions found in yo.6)rlz Solution. Employ ode45 to solvethe following setof nonlinear ODEsfrom t:0to20: 0.3y1y2 . 21.:l. you must createa functionto compute the righthandside of the ODEs.r'1: 2 and.y) whiclr yieldsFig.B"y(2)+0. 2 E Solution predotorprey of moo'el MATLAB with .A. The solvercan then be invoked by This command will then solve the differential equationsin predprey. m. One way to do this is to createan Mfile as in yp r uflCLi on yp = pr eopi e/ ( L.2. 7l. 0. F I G U R2 I .y) *y\2\ = l'1.2*y lI) 0. Suchequationsarerefered to aspredatorpre)'cquations.5r8 EXAMPLE . . We storedthis Mfile underthe name:predprey.6*y(1) .
tspan. a plot of the dependent >> plot (y( :. it is also instructiveto generate phaseplane variablesversuseachother by that is.thereis alsono controloverthe differentialequaious aspects are additionalarguments includedasin features.) par.This . . 3 . MAILAB solverusesdefaultparameters control varIn ofthe integration. The odeset function has the generalsyntax options = odeset ('parr' . tolerance Allows vou to adiustthe absolute the determines initial step. A completelisting of all the possibleparawhere the parameter meterscan be obtainedby merely enteringodeset at the commandprompt.2 1 oot ? y1 to the As in the previousexample.METHODS RUNGEKUTTA 2I . !. Some comare monly usedparameters 'RelTol ' 'AbsTol ' .vaf ' .I ADAPTIVE 5r9 plota In addition to a time seriesplot. F I G U R2 I .vaf.This optionalautomatically The solver you to setyour own.. turesof the solution. yA. addition. lows of The maximum stepdefaultsto onetenth the t span interval.. havecontroloverthese lt.v (:..rnirialsrep' 'MaxSf ' Allows you to adjustthe relativetolerance.'par ' ."') where options is a datastructurethat is createdwith the odeset function to control feathat andpJ . 3 E model plot Sloie spoce of predoforprey wiihMATLAB ?. options' p1' p2. has the value vaf .. .yl = ode45(odefun.2) ) w h i c h v i e l d sF i s . To tions' parameters. areparameters you want to passinto odefun. 2 l . p2. 1).
0. F I G U R2 I . options). . 0. y I .520 ADAPTIVE METHODS AND STIFF SYSTEMS EXAMPLE . ( 'ReLTol t0. noticehow ode2 As 3 takes relatively large stepsin the smoothregionsof the solufion whereasit takessmaller stepsin the region ol'rapid changearoundI : 2. Solution. 4).er'. ( b ) R e l T o l= 1 0 . MATLAB automaticallycreatesa plot of the resultsdisplayingcirclesat the valuesit hascomputed.from I : 0 to 4: /t al 2 dt : 10e {r l) [ 2 r 0 . we can implementthe solverwithout settingthe options.6t where1(0) :0. 2 l . 6 * y t Then.( L .A 4l .the solvertakesmore snall steps attainthe increased to erccuritcy. 4 E Solution CDEwithMATLAB (b). 0 7 5 r 0.2 2I U s i n go d e s e t t o C o n i r o l I n t e g r o t i o n p t i o n s O ProblemStotement.4b.2 I * ( t .we will create Mfile to computethe righthand an sideof theODE: function yp = dydt (t. We can obtailt a more accurate solutionby using the ocleser function to settherelat i v e e r r o rt o l e r a n c t o l 0 a : e >> >> optlons=odeset ode23(@dydt. First. Ie4) . Use ode23 to solvethe followine ODE. Obtainsolutions the default(103)and for a more stringent for (l0r) relative error tolerance.5. When we implement one of the ODE solversin this way. t Note that we have not setthe function equalto output variables tr .4a. y) y P = 1 O * e x p( .5).a .5. ' As in Fig. A 7 5 ^ 2 ) ) 0 . 2l.2 ) / ( 2 * . in Fig. smoiler of For o relotive toleronceuseo nence error is monv ono no'esteo:oerol.Hencethe defaultvaluefor the relativeenor ( l0 3) is automatically used: >> ode23(@c1ydr.
+rrt a future point 1.15)]: as : .5b).Consequently. calledmultistepmethods once the computation has begun.1 The NonSelfSforfing Heun Method Recallthat the Heun approach uses Euler'srnethod apredictor[Eq. way to improve Heun'smethodis to developa predictor thathasa local errorof Othi). (20.17)]: .y.))+r)'i*fQi. This suggests that the predictoris the weak link in the methodbecause has greatesterror. 21.. (Fig.*. n ( 2 1s ) .2 MUITISTEP METHODS The onestepmethodsdescribed the previous sectionsutilize information at a single in point tr to predict a value of the dependent variable.11(Fig. arebasedon the insightthat. This can be FIGUR2I. 1 g: J'. (20. it respectively.5a).Multistep methodsexploit this informationto solve ODEs.This weakness signilicant because efficiency of the iterativecoris the the one on of rector stepdepends the accuracy the initial prediction.) f (t.y.2. the of 21.2 MULTISIEP METHODS 52r 21.2I .we will presenta simple secondorder method that servesto demonstrate generalcharacteristics multistepapproaches. In this section.The curvatureof the lines connectingthesepreviousvaluesprovidesinformation regardingthe trajectoryof the solution.)+ f (t.1 ' . the predictorand the correctorhavelocal truncationerors of O(/rr) and O(h3).v)h rule and the trapezoidal as a corector [Eq. Thus. * (21.4) y!*. 21. Alternative approaches. valuable infbrmation from previous points is at our command.5 E ond beiween onestep {b)multlstep drfference of Grophicol depiction thefundomentol {o) for merhods solvingODLs ..
f (ti. This centering inrproves local errorof the predictor O1h3 the to 1.6) are called the nortselfsterting Heunmethod. ) EXA S4ri .. Eqs. f * . (21. In addition. * .522 ADAPTIVE METHODS AND STIFF SYSTEMS froma accomplished usingEuler's methodand the slopeat. (21. r . r . F I G U R2 I . As depicted Fig. 6 E (o) midpolnt A grophicol depiction lhenon of Heun method isused thot selfsfirrling method The rrle is oso predictor The os {b) tropezoidol thot employed c corrector S l o p e. dent variable)rr.5) and (21. .a s i n p lj+r : l'rr * .6) This fornrulaattainsO(h3) at the expeuse employinga largerstepsize2h.'!. of note that the equationis not selfstarting because involvesa previousvalue' thedepenit of problem. 21.6./ ( r . Sucha valuewould not be available a typicalinitialvalue in Becauseof this fact.thederivative in in estimate Eq.t'i)2h (2r.6) is now located themidpoint at ratherthan at the beginningof the interval over which the predictionis rnade. and extrainformation by p r e v i o u s o i n t l ' r .
6b): 1 l/+r : _1'.we requirethe additionalinfbrmationthat1 is equalto 0.5 t f r o m / : 0 t o 4 w i t h a s t e ps i z eo f l .313149  lx  r 0 0 ":/3 .S t o r t i n ge u n ' sM e t h o d H Problem Stotement.)'.9): l" Lr((ir 6.s. ( p o i n t .54933 r 6.5(6. to the l^ .313749 6. Use the nonselfstarting Heun methodto performthe samecomputations were peltbrmedpreviouslyin Example20.2I .s49331 which represents true percentrelativeerror of 5. 9 ) t os o l v e n O D E i s d e r r r o n s t r a tie dh e nt a followine examnle. (2 1.194631). Note that r'j" and y'l_. yi)Zn Predictor + Corrector (Ftg.l e r ) x ' I ' ...t .313149 which represents error of 1.""nr) where the superscripts denotethat the correctoris applied iteratively fiom 7 : I to la to obtain refined solutions. Solution. we arenow dealingwith a multithe initial conditionat / because stepmethod.t . 5 ( 2 )J . s t s .139o(true value :6. EXAMPLE l .o . the approximateerror. T h e u s eo f E q s .607005 is The conectorIEq. are the final results of the corrector The iterationsare terminatedbasedon an estimateof iterationsat the previoustirne steps.S e l f .That i.l:1.7)lis usedto extrapolate linearlyfrorn r : l to 1: . this e.r : 0.2. ' ) J (r'''1".8)canbe appliediteratively irnprove solution: Now.s t 2 I : 6.1) ( 2 18 ) .0 .2 MULTISTEP METHODS Thenonselfstarting method be summarized Heun can as (Fig. However. Eq. (21. as i n t e g r a t e .0.3 2 N o n . o o z o o .trrli .!i+ r I x l00lo ( 2r . The predictortEq.( 2 1 .'t(t" ''t/'") ' ' /r (for. This a in error is somewhat smallerthan valueof 8.54933 l) 1 I : 6. 121.0.927o. I t il 'i+l lo t_1. J 7 c " .'/+l r.An approximate an estimateof the error can be determined usingEq.tl zT srtr 3 1 4.2 using Heun's method.0 .3929953+ [4"'r'iu' .3929953 at / : 1.'lr t' (t.+ n " * ' .ltr 523 (2t.T : m . 9 ) When lenI is lessthana prespecified At error tolerance the iterationsareterminated.1 " 0l i t t . . (21. A s w i t h E x a m p l e 0 . 7 )t h r o u g h 2 1 .21.5(2)] 2 : 5.1Solc the incun'ed the selfstartins Heun.8)] then usedto conpute the varlue: vi:2+ j r .. 2 l" : 0 is t': 2.6a): f'j'*r: . 2 .2l ..
30224 : 3. r ' ' " ' { € .As was the casewith the Heun method (recall Example 20.14) where is now between 1 andf .44346 €t :9.ructed Eq. 1 (2t..1ir r. ..r u r r r 1 6 ) < It (2r. the error estimatethen providesa criterionfbr changingthe sfepsize. The error estintate can be derivedby recognizingthat the predictoris equivalent the to midpointrule..524 ADAPTIVE METHODS AND STIFF SYSTEMS (21.1r.13) 0: . (21. a similarestimate of the local truncationerror lbr the correctoris (Table 17. iterationsconvergeon the sameresultas was obtainedwith the selfstarting Heunmethod.l t ' f " t € .9+r *r.1r True + fir.76693 .tr.8)canbe appliediteratively Equation until e. However.and subsequent (e. ) 1 (21.The first correctoryields 15.11 give True value: )'.4) t.ift3.l2l Thiserrorestimate becombined can with theconector result to ..2 Error Estimqtes Aside from providing increased efficiency. :1. Hence.097o).1'.0.15) .its localtruncation erroris (Table17.. Eq.As with tlre previousstep. r i i ' : . 9 +r.68%).This errorestinrate 7r can be cornbinedwith the estimateof )'i+ r from the predictorstepto yield value:t..r (21.1 7 r . I 6 'r.) (2I.) : ht l"l€.ub.the rate of convergence theconecof tor is somewhat irrprovedbecause the betterinitial prediction. 15. : ih ) ' t ' ( { .1 Equation I) .falls belowa prespecified value ofe.36087(et : 2. of 21..because the initial predictorvalueis more accurate. { by gives result ) .]'l': 2 * l4ett 13.the predictoris sitt .2) ll E. Now..the nonsellstarting Heurrcan alsobe used to estimate local truncation the ernrr. .6. (t.r{. the iterationsconvelge a on value of 6.1./ r ' . t : .2.dividing (21. .2).10) where the subscript designates that this is the error of the predictor.5(6.U.u.(3){6.36087)2: .As with the adaptive RK methods Section in 21.0826(et : l87o) rhar was computedwith the original Heun method.t'.y.14) 5 andreanangingthe r.13) yield from ro (21. rnultistepmethodconverges a somewhatfasterrate.un b.43a/c wlrich is superior to the prediction of 12.(21.r. the at For the secondstep.il) By recognizingthat the correctoris equivalentto the trapezoidalrule.8vc).
30224. we have arrived at a relationshipthat can be usedto estimatethe perstep error on the basis of two quantitiesthat are routine byproductsof the computation:the predictor(1.If the third derivativedoesnot vary appreciably over the interval in question.44346 can be usedto comDute F _ 15.16) to estimatethe perstep .the MATLAB function odel13 is a multistep method.3 STTFFNESS Stiffnessis a specialproblem that can arisein the solution of ordinary differentialequatogetherwith slowly tions.4 Error Estimqte PerSteo Truncotion of truncationerror of ExamProblem Stotemenf Use Eq.. yields 6.That said. In some cases.the lefthandsidesshouldalso be equivalent.360865 5. Note that the true valuesat / : I and 2 are6. Additional information can be found elsewhere(e.360865. Chapra and Canale.the rapidly varying componentsare ephemeraltransients dominatedby the slowly varying that die away quickly.194631and 14.as in E ' 5. A stiff systemis one involving rapidly changingcomponents changing ones. ) EXAMPLE 21.45831.3.30224 13.) it .15.16) truncation Thus. 2006).44346 0. after which the solutionbecomes .3 STIFFNESS 525 Notice that the righthandsidesof Eqs.84392.we can assumethat the righthandsidesare equal. Although they still have their place for solving certain types of problems.15) are identical. which compares : Et : 6.30224: 0.31176 : which also compares favorably with the exact error.0 _ rit 1'ri (l l. 2r. respectively. and the comector Solution.607005 into Eq.For example. The foregoing has been a brief introduction to multistep methods.We have thereforeincluded this sectionto introduceyou to their basic principles. (21. (21.360865 0. Et : 14.150722 : well with the exacteffor.l6) to give Thesevaluescan be substituted F_ 6. and therefore. At /.12) and (21.which At /.11 :2.a1 : l..607005 0.g.multistep methods are usually not the method of choice for most problemsroutinely confrontedin engineeringand science.194631 6. (21. the predictorgives5. ple 21.84392.with the exception of the argumentof the third derivative.1662341 and the correctoryields 15. the predictorgives 13.9*r anAthe corrector("vlir) . they are still used.2I .
17): dt' dt EX (2t.)t). Both individual and systems ODEs can be stilT.1 by term(ermr). Euler'smethodcan be usedto solvethe sameproblemnumerically: ]i+t :)'it dt.An exampleof a singlestitT of is ODE .005).1 0 0 0 e ' (2r. calculuscan be usedto determinethe solutionas t' : 1'ue ttl Thus. l r [ s o i t ' o n o r o r i n g l eC D L Althoughlhe solution is oppeorsto slorlot l. : .nogeneous of Eq. 7 E phenomena components./ r' .1 0 0 0 r '* . 21. l 9 t g i v e s E .l't+l :)'i alih . . w h e n h e r e : p o n s e viewed on the finertimescole the insel. is in F I G U R2 I . Insight into the stepsize requiredfor stability of such a solutioncan be gained exby part tlre aminirrg hor. p e ' c e pi b l e e n l .This .526 ADAPTIVE METHODS AND STIFF SYSTEMS D l o 'o r o .the solutionis initially dominated the fast exponential governed After a shortperiod (t < 0.t9) If f'(0) .18) As in Fig. this transient dies out and the solutionbesomes (e b y t h e s l o we x p o n e n t i a l / ) .998el{){xt 2.oo2e' (21. Although the transient existfor only a shortpart of theintegration interval.0and asymptoticallyapproaches zero.. (21. .l : 3 0. there tronsienl octuo ly o fost ircnsieni from y : g 1e I thct occursin lessthnn the 0 OO5lime unil.t 2  . the analyticalsolutioncan be developed as .17) If I (0) : 0. 1 0 0 0 .they can dictatethe time steplbr the entire solution. the solution startsat ). n dt S u h s t i t u t i n gq .
002would resulr in a totally unstablesolutionthat is. Substituting (2 I . implicit methocls of'teran alternative remecly.\'i+r t'i(l .2ll For this case. evensmaller an stepsize would be requiredto obtainan accurate solution. explicitEuler'smethocl the is f J. y r l . the approach is  called unc on di t i o n al I v stubl e.'i+ 0 as r .20) The stabilitl'of this formulaclearlydepends the step sizeh. Hence.002. or Euler'smethotl.0015to solvefor of and .r. In when the stepsizeis increased a valuejustbelow the stabilto ity limit (ft : 0.\ll: I t(tn 1ll.we sl. l . this criterioncan be usedto show that the step size to maintainstabilitymust be <2/1000:0. 0 Solution.t'between/ : 0 and 0.).a/zl mLrst on be l e s st h a n l .r+r . (a) Use theexplicitEulerwith stepsizes 0. inrltlicir. (b) The implicit Euler'smerhodis : . l9) yielcls Eq. . whereas criterion (i. T h u s . UseboththeexplicitandrmplicitEulermethods solveEq.rr.0005 0. controls maximumallowable it the stepsize.0005 is displayed Fig.(21. the resultcaptures generalshapeof the anathe lytical scllution.An implicit forrn of Euler's method can be developedby evaluatingthe derivative at the future tirne: \'.yi* (_1000]. I 8 ).. 2 / a .+t : )'i ali+th which can be solvedfbr \ij .> oo.i f h : . Such representations called intplicit because unknown appears both sidesofthe are the on equation.2I .3 STIFFNESS 527 or : . EXAMPLE 5 2I E x p l i c io n d l m p l i c i E u t e r t t ProblemStotemeni.+ o o .i * 3000 _2000e ')h The result for h:0.That is.'l:\'. ro wherer'(0) : 0. Usingft > 0.e.. Ratherthan usingexplicit approaches.J.006.4. l_r. 'dt ll This is calledthe baclovutd..ah\ e .r+r: . 21.11). (2 I .althoughthe transient occurs1oronly a sntallfi'action theintegration of interval.i+rf 3000 _ )eeegti+t1p . in Although it exhibitssometruncationelror. contrast.rould note that.(b) Use the implicit Euler with a stepsizeof 0. (a) For this problem. regardless of the size of the step.o o a si . ll .0015).the solution manifests oscillations. . Thus. d.8a along with the analyticalsolution.r. it would go infinite as the solutionprogressed.05 to solve fbr I' between and0. In addition.a bounded this maintains stability solution). For the fast transient part of Eq.r (_1000].
e8eet : lJ .23b) .22h) For the initial conditions (0) : 52.67 r302.E 2r.3 0.^ dr dv .{\10tt lt : 52.01t)1t + lz (21. exampleis An dyt __:__5r.5 0 ) z 0.23a) (2t..gge302.)QQQlls ''t'r l+roooft The resultfor h :0.theexactsolutionis .29and l.4 FTGUR..we can rearrange equation that y. Notice that even though we have useda much bigger stepsize than the one that inducedinstability for the explicit Euler.96e3.8 S o i u t i oo f o s t i fC D Ew i t h( o )t h ee x p l i c o n d ( b )i m p l i c E u l em e t h o d s n f it it r Now because ODE is linear.r1.:(0) : 83.rr .1 0.05 is displayed Fig.002 0 0. the numericalresult tracksnicely on the analyticalsolution.5 I 0.82.2 (bl 0.v 1.e8eel65.s28 ADAPTIVE METHODS AND STIFF SYSTEMS .g.22a) (2t.8b alongwith the analytical in solution. Systemsof ODEs can also be stifT.83er. 2l. : dr 1001'1 l0ly2 (21.11is isolated the this so on the lefthandside: Li't ]i F 3000/r.
u s e M A T L A B t o s o l v et h e l b l l o w i n g t w o c a s e s : ( a ) f b r I L : l .r ' r ( 0 ) h ) l l d t : 1 . u s eo d e 2 3 st o solvefrom/:0to6000.5 / z .. miry be more efficientthanode15s at crudetolerances. Thus.althoughstabilityis gainedthroughimplicit irpproaches. An implicit Euler's methodfbr systems can be formulatedfor the present exarnpleas .3 STIFFNESS 529 Note that the exponents negativeand dilTerby abouttwo ordersof magnitude..This is usedfor moderately with low accuracy whereyou needa stiff problems solutionwithoutnumerical danrping. \ ' r .5 . ode2 3 s.24a) (21. a n d ( b )f o r 1 t : 1 0 0 0 .lt is a multistepsolverthat optionally usesthe Gear backwarddifferentiation forrnulas.Theseare tbr of odel5s. 1 1 . i +:tr ' 2 1 f ( 1 0 0 r ' 1 .24b) . This function is an implementation the trapezoidal of rule with a "free" interpolant.3. This is an implementation an implicit RungeKutta of formulawith a first stagethat is a trirpezoidal rule and a secondstagethat is a backwarddifferentiationformula of order2.i: 1 1 1'. y 2 . is the largeexponents respond it that rapiclly andareat the heartofthe system's stiffness.3 0 1 .:o (E21.6. with are As the singleequation. systemof nonlinearsimultaneous equations Thus.1 MATTAB Funclionsfor Stiff Systems MMLAB hasa numberof builtinfunctions solvingstiff systems ODEs.2)... EXAMPLE 6 21 MATTAB StiffODEs for Problem Stotement. . v z . This solvermay alsobe more efficientthanode15s at crudetolerances. we can seethat the problem consistsof solving a set of simultaneous equationsfbr eachtime step. ode23tb.li)! *.r'11r * (l t 301/z)1'1. 1 ) / z Collecting terms gives ( l + . This function is basedon a modified Rosenbrock formula of order 2.2I . . . a 21.Given the ini: t i a l c o n d i t i o n s.1) progressively The solutionto this equation becomes stifferas 1r getslarge. For nonlinear ODEs. u s eo d e 4 5t o s o l v e f r o m / : 0 t o 2 0 ..Tltis is usedfor stiff problemsof low to medium accuracy.priceis paid in tlreform of addedsolutionconrplexity. .25a) (2L25b) 100. The van der Pol equationis a model of an electroniccircuit that aroseback in the daysofvacuum tubes. " . # p(r. t ' ri + r f 3 . .* r3 ) ' : . Because it is a onestep solver. \ ' 2 .the solutionbeconres evenrnoredilficult sinceit involvessolvinga (recallSection12. i + l: l r i * ( . can it It solvesomekindsof stifTproblents betterthanodel5s. ode23t. 1 ' 1 . This function is a variableorder solverbasedon numericaldifferentiation formulas. i + r ) / l (21. i + t : ' )t i (21.
Tlre srnoothnatureof the plot (Fig.y (:. v. y2 z 1 0 *1 { z tl I _3 1 0 { *3 1 0 . N o t i c e h o w t h e v a r l u e1 . if you like).r ' i ) .\'.b e i n o 5can 1 voked and the resultsplotted: >> piof. \ . (b) If a standardsolverlike oc1e45 usedfor the stiff case(p : 1000). ' ' . (E2l . r: 0 An Mfile can now be createdto hold tlris pair of diff'erential equations: yp un= .we must useopenbrackets as ll that the van derPol a place holder.ocle.6.s30 ADAPTIVE METHODS AND STIFF SYSTEMS Solution. However. : s ( l . Nonstiff solved ode45 (b) form ond stiff solved io) wilh otl. (a) The first stepis to convertthe secondorder ODE into a pair of firstorder ODEs by defining 1/l'r dT .1)) F I G U R2 I .ndrpo r . ? 2 1 0 l ' lt it I I /l i it fl . f I Eq.1).y(:. ion )p ^2.t s doesan efficient I >> plot(t. A s i n E x a m p l e 2 l .'y2'). o d e .2) ') . 21.)*y (2) { (r) ly (2) . l . (t.mu* (1y(1) I .:21s.9a)suggests equationwith p : I is not il stiff system.i. 9 E for form with Soutions vonderPol's equolion.1u.y(:. writtenirs Using this equation. u . >> lesend('yl'.' we Observethat because are not specityingany options.1)can ['re d)': dt .firs p a s s e d a s a p a r a m e t e r ' . it will fail miseris job: ably (try it.
garnma ) cord = 0.thejumper is only subjectto gravitational and dragfbrces.AR solvefs.s/rn.sign(y(2))*cdr'm*y(2)^2 dydt .4 MATTABAPPLICATION: BUNGEE JUMPER WITH CORD In this section.9a.^n.2l .the spring and dampeningfbrcesof the cord must also be included: dr t'. end . This is a visual manifestation the "stiffness"of the solution.1 : 8 . dtt dT ' : g . .27a) Once the cord beginsto stretch. . n r : 6 8 .ly(2li Notice that the derivatives are returned as a column vector because this is the format .4 MATLAB APPLICATION: BUNGEE WITHCORD IUMPER 53t We have only displayedthe r1 component because resultfor . the Notice how this solution (Fig.'irert hrr the MA'l'T. cd. Perfom the computation from t :0 to 50 s and assume that the initial couditions r(0) : u(0) : 0.yzhasa much larger scale. The following Mfile canbe setup to computethe righthandsidesof the ODEs: f unct ion n Q Ql.21 .r .(. of 21. are Solution. atntmm t . 1 k g . EXAMPLE l .2'7b) The following exampleshowshow MATLAB can be usedto solvethis problem.s i g n ( uj)u .signlx)31rr nt (2t.cordl. the problem consistedof solving two coupled ODEs fbr vertical position and velocity.k.L) :u k v (21. L.Thus. k :40 N/m.The differentialequationfor positionis dr dt u (2t. m. / m * y( 2 ) . dyclt .we will use MATLAB to solve for the vertical dynamicsof a jumper connected to a stationaryplatform with a bungee cord.t .26) The diff'erential equation velocityis differentdepending whetherthejumper hasfallen for on to a distancewhere the cord is fully extendedand begins to stretch. and / : 8 N.2l . 8 1 r t / s t . if y(1) > L %determine lf the cord exerts a force c o r d = k / m * ( y ( 1 ) . 2 5 k g / m .rngee t h e f o l l o w i n g p a r a m e t e r sI: : 3 0 m .9b)hasmuch sharper edges thanis the caseinFig.bunqee (t .L ) + q a m m a . r : 0 . if the distance fallen is lessthanthe cord length. g : 9 . Determine the position and v^elocityof a br. 20. g . y. As developedat the beginning of Chap. .7 2 B u n g e eJ u m p e rw i t h C o r d jumper with Problem Stotement.
y(:. the fountain flows again. c J2gyn r2 (21..IO jumper Plot distonce ve ocilyof o bungee ond of 40 20 0 20 40 60 l\.2).t.Notice how the simulation motion.'v (m/s)') :' ) distance for the As in Fig. we can useode4 5 to obtainthe solutions display them on a plot: >> plot(t. 2 FIGURE I.As in Fig.s32 METHODS AND STIFF SYSTEMS ADAPTIVE and are Because theseequations not stiff.Pliny the Elder.igr. Thefounto tain runs until the water level decreases )1oy. 11. this point. producing a fountain at the pipe's exit.The cycle then repeatsas the tank fills until the water reaches )rrigh. 21..28) .".' (m) '.10.1). water siphons of the tank through a circular dischargepipe. we havereversed sign of distance the plot so that negative capturesthe jumper's bouncing is in the downward direction. can be computed with the following formula basedon Torricelli's law: eou. the outflow Qo.whereuponthe siphon fills with air andthe and fountain stops.''. r *too' 10 20 30 it FOUNTAIN INTERMITTENT PLINY'S Bockground. 2l. When the siphon is running. The Roman natural philosopher.y( >> leqend('x :. water entersa cylindrical tank at a conout At stant flow rate Qrnandfills until the water reaches)r. pulportedly hadanintermittent fountain in his garden.''\' l"tarr ar i.
Iro* 0.5 CASE TUDY S s33 continued )=)high F I G U R2 I . (2r.025m C:0.30) When the fountain is not running._ ^^.3 r) In the presentcontext. compute and plot the level of the water in the tank as a function of time over 100 seconds. When the fountainis running.29)gives d1. and employ the following parameters your computation: for )(0) Rr: 0'05m )r.' : 50 x 106 Solution.siphon can be thoughtof as a switch that turns the fountainoff and on. (21. (21. We can incorporate this mechanismin the model by introducing a new dimensionlessvariable siphon that equals zero when the fountain is off and equals one when it is flowing. : Qi" Qou.81m/sz m3/s 0.^t^+ +^ +rra .2I.0.ign:0'l m r: 0. Such twostate variables are called Boolean or loqical variables.007m : .. dt Q.29) where V : volume (m3).:. Assume an initial condition of an emptv tank .r __ :. the secondterm in the numerator goes to zero. d1.. y : r Rll.28)into Eq.Because tank is cylindrical. where zero is equiva. I T E An intermittent founioin Neglecting the volume of water in the pipe..6 g = 9..the rate of changein the tank's volume V(m3) is determinedby a simple balanceof inflow minus the outflow: dv 4. (21. dt QinsiphonxCJTfinrz TR? (21.substituting this rethe lationshipalong with Eq.CJTfrrrz 1T R.
1.v1qp. the siphon shuts off well before the level drops to. if y(1) <= y1o srphon = O.o b a I s i p h o n Rt . 2I.*n. it is useful in the present context.rn. Except for the original filling period.*n.013 x l02 m/s. Similarly. However.025./ (pi * Rt "'2. siphon is declared as a global variable.rel in tank. But if you did. At this point. The difficulty arises because the ODE is discontinuous at the point that the siphon switches on or off. tspan = l0 f00l. ylo = 0.00005.the outflow kicks in and the derivative abruptly drops to 1. they often get heartbum when dealing with such discontinuities..534 ADAPTIVE AND STIFF SYSTEMS METHODS rl Next we must relate siphon to the dependent variable y.6.The following Mfile function follows this logic in computing the derivative: dy = Plinyode(L. (m) ') ylabel('water Le. end * pi * r * g * y(1)) Qout .)!..siphon * C * sqrt(2 ^ 2). As shown in Fig.0. Because they infer the behavior of the solution by comparing the results0f different steps.Qout) .. Although the use of global variables is not encouraged (particularly in larger programs).0. you would discover that although these routines yield somewhat dift'erent results. the result is clearly incorrect. r = 0. First. dy = (Qin .81. Although ihe adaptive stepsize routines used by MATLAB work marvelously for many problems. they would still generate inconect solutions. as soon as the level reaches yn. >= yhi elseif sipiron = 1.007. globai siphori siphon = 0. siphon is set to zero whenever the level falls below. the derivative is dependent only on the constant inflow and for the present parameters has a constant value of 6. Conversely.366 x l0l m/s. as the tank is filling.l'p] plot (tp. y (f. = o d e 4 5 ( G P l i l t 1 z e 6 s .05. the level seems to start emptying prior to reaching vn. a discontinuity represents something akin to stepping into a deep potholeon q drrk qtreet . Notice that because its value must be maintained between function calls. when it is draining. you might be tempted to use one of the other MATLAB ODE solvers such as ode2 3 s or ode2 3 tb . For example. Qin = 0. yhr . y0 .12.y) function g i . I = 9. siphon is set to one whenever the level rises above yn.t s p a n .0.yp) (s)') xlabel('l.. The following script employs the builtin ode45 function to integrate Plinyode and generate a plot of the solution. g = 0.iine. l / 0 ) ' Itp. suspecting that the problem demands more firepower than the trusty ode4 5 routine.
engineers is to develop a remedy basedon your knowledge of numerical methods.the constantstep fourth line would be formulated as Itp. We can implement this solution strategy by merely replacing ode45 with the rk4sys function from Chap.13.08 J o c) c) q) G 0. You've probably generated the computer.tz 0. Because MAILAB."Second.small step size. 0 ' 0 6 2 5) . that's precisely the approachyou would take if you were traversing a dark.5 CASE STUDY s35 continued u. your first inclinationmight be to just give up.1 E 0. y 0 . 21. when the cycle repeats. if it's too hard for personcould expect you to come up with a solution.04 0. First. If you think about it.yp] = r k 4 s y s ( @ P l i n y o d e .2I . There are a two takehome messages opposite. you might revert to a simpler approachand use a constant.02 U 010203040506070 T i m e( s ) 80 90 100 21.12 FIGURE with ode45. potholefilled street.06 0. After all. in referenceto the impact of data quality out" .For the scriptoutlinedabove.20 (Fig. the solutionnow evolvesas expected. no reasonable your only recourse professional and scientists rarely get away with suchexcuses.After all. that can be gleaned from this case study."Everything shouldbe as simpleas possible. founloinversus time os simuloied The level in Pliny's At this point.t s p a n . to although it's human nature to think the but paraphrase Einstein. simpler is sometimesbetter. by you shouldnever blindly believeevery result "sarbase in. no simpler. and then empThe tank fills to yn'rn As in Fig. ties until it reaches )1o*. 20.8). Because the problem results from adaptively stepping across a discontinuity.
In fact. albeit incorrectly.06 a o.E 2r. (a) FTGUR. Hence. Hopefully.07 c 2l. 21. Corr the e and.536 ADAPTIVE METHODS AND STIFF SYSTEMS continued 0. 8 ) (b) on the validity of computer output. .01 0 10 20 30 40 50 60 T i m e( s ) 70 80 90 100 whe the r rate. sophisticatedengineersand scientistsalwaysexamine numerical output with a healthy skepticism based on their considerableexperience ad knowledgeof the problemsthey are solving.09 0.12 areparticularlydangerousthatis.r3 Thelevelin Pliny's {ounloin time fie versus os simuloted o smoll.1 0. the solution moves up and down in the manner an of intermittent fountain.oc > 0. this case study illustrates that even a great piece of software suchas MATLAB is not foolproof. the simulationdoesnot go unst& ble or yield negative levels.2 ane : J € 0. althou$ the output is incorrect.03 : 0.08 ? 0.os 0) I : I 5 o. That is. r k 4 s y s f u n c l i o( F i g 2 0 . with consiont size step using n .02 0. 21.l Plin thre 21.3 inter Use ofC v(2. some individuals think that regardlessof what went in (the data) and what's going on inside (the algorithm).it's not obviously wrong. it's always"gospeloul" Situationslike the one depictedin Fig.4 inter ! a Use . Unfortunately. 2r.
.9 Recallfrom Example I7.577988.5):5.5: interval from I dy dt  r'tZ  ) ('I0. a plots of all the statevariables.5 for Pliny's fountain.000people. Modify the model to include this mechanismand repeatthe computationsin (a)usingP:0.:5(l'AT r'r : If the initial condition is r. parameters: : 0. where p : the reinf'ection rate. (e) Usingthe MATLAB functionode23tb. rrtO) I Determinethe value of the deflnite integral of this function between. obtain a solutionfrom /:0to5: dv (a) Analytically. use the implicit Euler to obtain a solution from / : 0 to 2 using a step size of 0.4. 21./ = the infected. 21. 21. and r: the recovery rate.271888 21. .r range.5 Given tly . Present obtained analytically: theexactsolutions v(2.01 (x0.A city has 10.25.03/d. your resultsbasedon Computethe percentrelativeenors for your resultsin graphicalform. :aSI at rl dR :rl at whereS : the susceptible individuals. 21.2 The following ODEs havebeenproposed a model of as a ne p i d e m i c : dS .0) 2. the (b) lf .5) : 1.there is ir loss of immunity individuals hecome to susceptible. use the implicit Euler to obtain a solutionfrom t : 0 lo 4 usinga stepsizeof 0.15/d.2000rr dr f to 0. function exhibitsboth flat and steepregionsover a relatively 21.i1e2j.^ rlr^ ^Ji^r. and ode113.17o.2 I f .3 Solve the following initialvalueproblem over the intervalfiomr:2to3: If f (0) : 0.v(0) : 0.6 Given z1 t' .: /r. : 3 f l r s i n1 .a : the int'ectionrate.n .tt dt :999xt f 1999x2 dr: : 1000rr . Iterate the corrector to s.5r' le' (b) Using the fourthorderRK methodwith a constantstep dt sizeof 0.r(2. o b t a i na s o l u t i o n r o m t : 0 using a stepsizeof 0. parasiticODE was suggested (1975): by Hornbeck dt' .sI at dt 21. thatcauses recovered This reinfection mechanism can be computed as pR. .r:0 and I using(a) the quad and (b) the ode45 .(1. computethe progression the epidenric of until the number of infectedindividualsfalls below  0.l Repeat the samesinrr. r 1 ( 0 ) : x u ( 0 ) : l .all of whonr are susceptible. : 0. : 0 to 0.05 with the (a) explicit and (b) implicit Euler rnethods.0.5 that the following humps : and r'(3.03l25.5 and initial conditions of r.143883.rlations in Section the as 21.08.0021@ersonweek) and r : 0. Also Develop timeseries generate phaseplane plot of S versus1 versusR.7 Given cl. : 1oo'ooo)' 99. o :0. Then use the nonself:starting *r. l.PROBLEMS 537 PROBLEMS 2l.9)2f0.222138 and ( d ) U s i n gt h e M A T L A B l ' u n c l i o n o e ) : . a (b) Supposethat after recovery. (a) If a single infectious individual entersthe city at .0) : 4.04 + o Use the fourthorderRK methodto predict the first value at Heun method to t:0.3)?+0. ode23s.t  n 5 NTntp.999e' r (a) Estimate the step size requiled to maintain stability r"rsing explicitEulermethod. but generatethe solutionswith c. LIsethe following .8 The fbllowing nonlinear.R : the recovered. Use the nonselfstartingHeun method with a step size ( c ) U s i n gt h e M A T L A B f u n c t i o n c t e 4 . r ) + 3 c 0 s 1 tlI . Use :subplot to developa vertical plot threepane of the time series. 21. o of 0.'(0) 0.4 Solve the following initialvalue problem over the short..
11 The following system is a classic