P. 1
Applied Numerical Methods With MATLAB for Engineers and Scientists

Applied Numerical Methods With MATLAB for Engineers and Scientists

|Views: 66|Likes:
Published by viciousrabbit

More info:

Published by: viciousrabbit on Feb 21, 2012
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





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

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

MATLAB Fundomeniqls 2.1 2.2 2.3 2.4 2.5 2.6 2.7
^ l l


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

Progromming with MATTAB 42 3.1 M-Files 43 3.2 InputOutput 47


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

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

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

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



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

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

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

[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



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

Iterotive Methods 264 l2.l Lineor 264 Systems: Gouss-Seidel ,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

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

Generol lineqr Leosf-Squores 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


l l

.1.1 /


'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

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 Newton-Cotes 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 Higher-Order 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



Numericof Integrotion of Functions 'l 8.I Introducfion 426 18.2 RomberInieqrotion q 427

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:Root-Meon-Squore Current 440 Problems 444

Numericql Differentiqtion 19.I 19.2 I9.3 ,l9.4 ,l9.5 448 Inhoduction Bockground 449 ond High-Accurocy 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


Ordinory Differentiol Equotions 473
6.1 Overview 473 6.2 Porl Orgonizofion 477

Initiol-Volue Problems 479 20.I Overview 481 20.2 EuleisMethod 481 20.3 lmprovemenls Euler's of Method 487 20.4 Runge-Kutfo Methods 493 20.5 Systems Equotions 498 of 20.6 CoseStudy:Predotory-Prey Modelsond Choos 50A Problems 509

Adopfive Merhods ond Stiff Systems 514 21 .'l AdoptiveRunge-Kutto 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 /



Boundory-Volue Problems 540 22.1 lntrodvction Bockground 541 ond Method 545 22.2 lhe Shooting 22.3 Finite-Difference Methods 552 P r o b l e m s5 5 9


Modqling,CoTpute''i ,
qnd Erior Anolysis


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 pre-computerera, 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 problem-solving skills. 2. Numorical methods allow you to use "canned" so-ftware with insight. During your career,you will q*"

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 large-scale 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.




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.

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 fiee-falling jumper. The model, which is basedon Newton's secondlaw, resultsin an ordinary bungee differentialequation.After first using calculusto developa closed-formsolution,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 so-called meansto orient you kl the enviroumentand illustrateshow it is usedibr common opera-

Chapter-l 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

Based this insightandyour knowledge on .lrfi. on Understanding different typesof conservation the laws that lie beneath models the usedin the variousengineering disciplinesand appreciating diff'erence the betweensteady-state dynamic solutionsof thesemodels. Specificobjectivesand topicscoveredare o r o Learning how mathematical modelscan be formulatedon the basisof scientific principles simulate behavior a simplephysicalsystem. 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).if i . U1 I Mothemoticol Modeling. 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. l..{.. Numericol Methods. irnd Learning aboutthe difterent typesof numericalmethodswe will cover in this book. to the of Understanding how numericalmethodsirlford a meansto generate solutionsin a rnannerthat can be irnplemented a digital computer.1:i ffilT** TFi + i.l ) as a function of time during the free-fall part of thejump.r I r. r 1 FIG For f-^^ l I cc- ium r YOU'VE GOT A PROBTEM upposethat a bungee-jumping companyhires you. You're given the task of predicting the velocity of a jumper (Fig.t " i'i..

81nls21.In a very general sense. and m: the jumper's mass(kg). .f (l. you developthe following mathematical model for the rate of change r h o l ' v e l o c i t y .rass the object(kg). . r ' i t r e s p e ctto t i m e .a . . ca: a second-order drag coetficient (kg/m).of the secondlaw is the well-known equation or F:ma (t. This will involve developinga con.1) can range from a sirnple algebraic relationshipto largecomplicated setsof diff-erential equations. r : time (s). The actual mathematicalexpressionof Eq.functiotts externalintluencesacting or are upon it.tendent variable is a characteristic that usually reflectsthe behavioror state of the system:. parametersareretlectiveof the the system'sproperlies composition. model./ i n d e . such as time and space.independettt the variablesare usually dimensions. ducd. Newton formulatedhis secondlaw of motion. of (r a n dc i i s i t s a c c e l e r a t i o nn / s : ) . . can be represented a functionalrelationshipof the fonn it as D e o e n d e n ( . However.I .rodels figure prominentlyin the way engineers and scientists use numericalmethodsin their work. example. g : the accelerationdue to gravity (:9. p u r a n ) e t el r s . . m is the n. ol . along which the system's behavioris beingdetermined.c t l o n s| v ' n .In so doing. which states that the time rateof changeof momentumof a body is equalto the resultant force actingon it. our more generalobjective will be to illustrate (a) what numericalmethodsare and (b) how they figure in engineering and scientificproblen solving. I. or kg nls").we will illustrate an alternativesolution approach. 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 the following pages. The ntathematicalexpression. un \ .2) whereF is the net force actingon the body (N. (1.and thelbrring.you know that calculusmight be usedto obtain an analyticalor exactsolutionfor u as a function of /.e n c l e n t forcine \ 'n.on the basisof For his observations.I A SIMPTE MATHEMATICAL MODEL of fluid mechanics. flGURE l. _e : J [ v a n a D t e s.l) where the de. Aside from showing you how the computercan be usedto solve this particularproblem.rputerorientednumericalor approximate solution.a .l Forces ociing o on lreeJolling bungee iumpet. we will also show how mathematical n. Because this is a ditlerentialequation.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.

the solution of Eq.1. 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. due (1. modelsof physicalphenomena However. the model ignoresnegof manif'estations. ( 1. the secondlaw can be a u. (1.other mathernatical techniques mathematical and either cannotbe solvedexactly or requiremore sophisticated than simple algebrafor their solution.6) . to the net force acting on the body normalizedto its mass.the object will accelerate.4) Thus.consequently. 1. variablesand parameters.1): F: Fol Fti (1 . a terms. .the Ifthe net force is zero. Becauseof its simple algebraicform.For example. It describes naturalprocessor systemin mathematical It represents idealization an and sirnplification reality. ( 1.-31 variablereflecting the system'sbehavior. nearthe earth'ssurface. be usedfbr predictivepurcan Finally. we will expressthe net force in terms of measurable For a body talling witlrin the vicinity of the earth.F is the forcing funcwhere a is the dependent variable tion. we because are not yet predictinghow acceleration models Equation( 1. Next.To illustrate a more complex model of this kind.'elocity it duF dt nr (1. Thus.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. METHODS. SOLVING MATHEMATICAL NUMERICAL AND PROBLEM The secondlaw can be recastin the format of Eq.if the force on an object and its massare known.l) by merely dividing both sides by m to give F n1 (1.81m/s2). Newton's secondlaw can be usedto determinethe terminal velocity of a free-fallingbody jumper (Fig.1). Eq. . and nr is a parameter.If the net force is positive.the net force is composed two opposof ing forces: the downward pull of gravity Fp and the upward force of air resistanceFy (Fig. objectwill decelerate. 5 ) If force in the downward direction is assigned positive sign.sed formulatethe force due to pravity as to Fo:m8 whereg is the acceleration to gravity(9.For this case.2) was obtainedeasily. the rate of changeof the velocity is equal where u is velocity (in metersper second). (1.3)can be usedto compLlte acceleration.That is.3)to yield r. resultsand. Note that for this simple casethereis no independent variesin time or space.3)has a numberof characteristics aretypical of mathematical that of the physicalworld. may be much more complex. it yields reproducible poses. Ifit the object'svelocity will remain at a constantlevel. . is negative.6 MODELING.

A bungee liom a stationary hot air balloon. 7 Air resistance be fbrmulatedin i.the exactsolutionof Eq. ( 1.1kg leaps ProblemStotement.t is the independent variable ctt andm areparameters. ( 1.10) Note that Eq. ( 1. 7 )c a nb e c o m b i n e do y i e l d t t dt: .ivariety of ways.3). thejumper is initially at rest(r. might be a function of the type of clothing or the orienc. ( E q s . Therefbre. calculus if : can be usedto solveEq.25kg/m. the present For c&s€. Knowledge from the scienceof can fluid ntechanics suggests a gtrodfirst approxirrration that wouliJbe to assume that it is proportional to the squareof the velocitl. the jumprraster havinga particuiarly is bad dayl). Also deterr-nine the terminal velocity that will be attainedfbr an infinitely long corcl(or alternatively./ accountslbr properties ofthe ialling object.9)is castin the general fbrm of Eq.(/) is the dependent variable.9)to compute velocityfbr the first l2 s of fiee fall.e-' e .However.9) where tanh is the hyperbolic tangent that can be either computed directlyr or via the more elementary exponential function as in e' . For example. 0 at / : 0). ( 1.rrd force. more adt'anced techniques suchas thoseof calculusnrustbe appliedto obtainan exactor analytical solution.Rather.in contrast in to the solutionof Newton'ssecond law in Eq.l -cdr)' (1.1is a proporticlnalitv constantcalled the drag coefticient(kg/m).7) where r'.suchas shape surface or roughness. g is theforcing function.* e \ tanh-t: (1. EXAMPLE I . 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 .8. Thus. the greater the fall velocity. o t t .ff*"n( u(/) .Use a dragcoefticient 0. 4 1h r o u g h 1 .8) (1.I A SIMPLE MATHEMATICAL MODEI.1)wheret. The rlet fbrce is the differencebetweenthe downward and upwi. 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.8)for the velocity of the jumper cannot be obtirinedusing simple algebraicmanipulation.I . (1. f U: .1 1 . The parameterc. affect air that resistance.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..7 tationusedby thejumper duringfree tall. ( 1.8) is a ntodelthat relates accelerirtion a falling objectto the tbrces Ecluation the of actingon it.Use Eq. -. and . the greaterthe uprvardfbrce due to air resistance.i nf u n c t i o nr d n h ( r ) .5 ctt t dt tn (1. .8)for :r..) yilt / (1.

92 33I I tB 42 4762 46 9575 4 S4 2 t4 50 6175 516938 According to the model. Note also that after a sufficiently lons FIGURE I. 3 u.MATHEMATICAL MODELING. A velocity of 49.2 The onolyticol solution thebungee for problem compuredExomple1 Velociry os in I iumper increoses with time ond osympfoiicolly opproocheso terminol velociiy. 1. .mfs 0 2 4 6 B t0 12 o I B 72.4214m/s (about 110mi/h) is attainedafter 10 s. (1. the jumper acceleratesrapidly (Fig.2). NUMERICAL METHODS AND PROBTEM SOLVING Solution.9)yields the into which can be usedto compute t. Inserting parameters Eq.

eventually.6mi/h) is reached.of 51. io The of o finite the derlvotiveu wifhrespect / of use differenceopproximote firsf to AU Approximateslope -u(r.u ( t i ) ) ti+t .*. 1.Remember dtt - dt lim A1+l) Au N ( process.) Lt li+t . I I ) represents reverse the FIGURE 3 I. ( 1. the net force is zero and acceleration ceased.I A SIMPLE MATHEMATICAL MODEL 9 time.: .9) is called an anabtical or closed-form nrodels fies the oliginal diffbrentialequation. is velocity at an initial time ri.3): dtt . solution because exactly satisit Equation( I . has ancewith the air resistance. Note that du ldt = Lu I Lt is approximate from calculus Ar that because is flnite.ti (r.6983m/s (115.lr) whereAu and At aredifferences velocityand time computed in u(r1) overflnite intervals.8) by realizingthat can be solvedby arithmeticoperations. tlrereare mirny matlrematical that cannotbe solved exactly. This velocity is constantbecause.called the terminol velocitt'.) Ar_u(1.: Lt dt u ( / i + r. a constantvelocity.Unfortunately. of by the time tateof change velocitycan be approximated (Fig. the problerr is refbrmulatedso it Nttntericalntethods thosein which the mathemirtical are This can be illustratedfor Eq.the force of gravity will be in balThus. and u(ria.In many of thesecases.the only alternativeis to develop a numericalsolutionthat approximates exact solution.Au .ti .] . Equation l.) is velocityat somelater time f11.

to New valne: old vahle* slopex stepsize This approachis tbrnrally calledEuler's metlnd.2s . Thus at any time along the way.. it provides meansto compute rateof change slopeof u.to MATHEMATICAL MODELING. 1 b u tu s eE q . Eq. 6 2 t . Thus.. It can be substituted Eq. Employ a stepsizeof 2 s fbr the calculation. We'll discussit in more detail when we turn to diff'erential equations later in this book. (/{) Solution. can the equat.1)-u(t. We cannow seethat thedifferentialequation beentransformed has into an equation that canbe usedto determine velocityalgebraically ri+l usingthe slopeandprevious the at valuesof u andt. NUMERICAL MEIHODS. a the or tEq. This new valueof velocityat l.r 2 ) Notice that the tenn in brackets the right-handside of the diff'erential is equationitself (1.r' . I .rl x 2 : te.4117m/s .13)can be used to corxpute velocityat 11.. 1 3 ) S t c as to colnpute velocitywith Euler'smethod.8)1. 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 . That is.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 . (1.: o* fr./i) ) [r ( r . 6 2 1 9 .J _ _ t .r L I For the next interval lfiom r : 2 b 4 sJ. ( 1 9 .1I ) is callecl Jinite-diJJeren.+r .13) wherethe nomenclature clesignates u.ti. If you aregivenan initial valuefor velocityat sometime l. :g . At the startof the computation :0).12 and so on. This equationcan then be rearrangecl yielcl to u(/i+r: u(/i)-1_ .2 s: r. the velocity of the jumper is zero.11canin tum be employed to extendthe cornputation velocityat l.1 r " l:16. 8 1.ff(/.l 68.o. t / 7 ) )tn.8)to give into r'(1.the colnplrtationis repeated.t L l- 0. velocityattinle /i and At : ti+t . ( J .ion be rewritten as ui+:r+'!u r {1t (t. you caneasilycomputevelocityat a latertime f 11. Using this infbrmation and the parameter values from ExampleI . .62rls 68. EXAMPLE .9lrt.ce a opprcrirnationof the derivativeJt Iirnc /.H.l tr+t * t.AND PROBLEM SOLVING Equation( 1. with the result : + l ( ) . ( 1.

with the aid of the computer.4 I for problem Compcrison thenumericol onclyticol of ond solutions thebungee iumper The calculationis continuedin a similar fashionto obtain additionalvalues: ttS u.Thus. as the straighrline segments track closerto the true solution. m/s 0 r9.I .Using hand calculations. you can accurately model the velocity of thejumper without having to solve the differentialequationexactly.One way to minimize such discrepanciesis to usea smallerstepsize. applyingEq. 1.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.However.large numbers of calculations can be performedeasily.6200 36. there is some discrepancy betweenthe two results.We can seethat the numerical methodcaptures essential the featuresof the exact solution. .because we have employed straighfline segmentsto approximatea continuouslycurving function.4 along with the exact solution. (1.However.the effort associated with using smaller and smaller step sizeswould make such numerical solutionsimpractical.1 A SIMPLE MATHEMATICAL MODEL tt Terminalvelocity FIGURE.13)at 1-sintervals results in a smaller error.Forexample.

.Eq. as in by Flow in : flow out For the junction in Fig. the balancecan be used to computethat the flow out of the fourth pipe must be 60. such. the steady-state For the bungee condition would correspond the casewhere to the net lbrce was zeroor [Eq.(1. the great conservationlaws of scienceand engineeringare conceptuallyeasy to understand.Each halving of the stepsize to attainmorc accuracyleadsto a doubling of the nurnber of computations. (I. there are other major organizingprinciples in science and engineering. AND PROBLEM SOLVING price nrustbe paid for a nroreaccurate As in Example 1.the increases decreases and nrustbe in balance.the flow into a junction musl be balanced flow going out.14)and (1. ( 1. they embodythe two fundamentalways that conservation laws areemployedin engineering As and science.r2 MATHEMATICAL MODELINGNUMERICAL METHODS. L/AWS ENGINEERING lN AND SCIENCE 1.2. We will give it a special name-the time-variable (or transient) computation. balance the bungee for Although simple.r4) This is preciselythe fbrmat that we empioyedwhen using Newton'slaw to developa force jumper tEq.Although they form the basis for a variety of complicatedand powerful mathematical models. at steadystate.r5) Thns.r6) Thus. ( 1. (1. Aside from predictingchanges. (1. ifno changeoccurs. anotherway in which conservation laws are appliedis fbr cases wherechange nonexistent.ation lan:s.5. we seethat thereis a trade-offbetweenaccuracyand computational effort. They all boil down to Change: increases decreases (1. Such trade-offsfigure prominently in numerical methodsand constitutean importantthemeof this book. change zero.which is also given a specialnarne-the stea(ly-state in calculation-has many applications engiFor example.2 CONSERVATION Aside from Newton's secondlaw.fbr steady-state incompressible neeringand science.14) becomes is If is Change: 0 : increases decreases or : Increases decreases (1. fluid flow in pipes. downwardand upwardfbrcesarein balanceand Eq.15)might appeartrivially simple.Eq.a cornputational numerical result. to predict changes with respect to time. This case.8)1.| 6) can be the solvedfor the terminal velocity AlthoughEqs. Thus.14)embodies ways in which one of the most fundarnental conservation laws are used in engineering and science-that is. I .8) with du ldt : 0l l1l$ : f 6411- (1. Among the most importantof theseare the conserv. jumper.they will tbnn an importantpart of our et-forts subsequent in chapters illustratethe connection to betweennumericalmethodsand engineering and science.

Many chemical engineeringproblems involve mass balancesfor reactors. chemical engineering skills are usedextensively areassuchas environmental.I.electriccurrentmust balanceat thejunction of electricwires. will endeavor includeexamples We from these to areas the coming pages.andmechanical. essence. of We shouldnotethattherearemanyotherbranches engineering of beyondchemical..5. Finally. Many of theseale relatedto the Big Four.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.The energybalance specifies that the clranges voltagearoundany loop of the circuit must add up to zero. which resultsfrom the conservation charge. in I.6 summarizes major areas the coveredin this text. The massbalanceis derived from the conservation mass.The currentbalance.1.and biornedicalengineering. .Forcivil engineering. Civil and mechanicalengineers often focus on models developedfrom the conservation of momentum. 1. civi.aerospace engineering much in cornmonwith mechanihas cal engineering.Justasflow mnstbalance thejunction at of pipes. Sirnilarly.The sameprinciplesare employedfor the mechanical engineeringcasestudiesto analyzethe transientup-and-downmotion or vibrations of an automobile.Figure 1. In most consistof recasting mathematical operations into the simple kind of algebraicand logical operations compatiblewith digital compllters. simiof is lar in spirit to the flow balance depictedin Fig.electrical en-rploy engineering studies both currentandenergybalances model to electriccircuits. I summarizes somemodels andassociated conservation lawsthatfigurepromrnently in engineering.3 NUMERICAT METHODS COVERED THISBOOK IN We choseEuler's methodfor this introductorychapterbecause is typical of many other it classes numerical of methods.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. in petroleum.For exalnple.It specifiesthat the of changeof mass of a chemical in the reactordependson the amount of mass flowing in minus the n.rass flowing out. electrical.

AND PROBLEM SOLVING Devices ond typesof bolonces ore commonly fhot usedin ihe four moiororeosof engineering.l MATHEMATICAT MODELING.2. b For each node I current (i) = 0 I'R' Conservation of energy Voltage balance: a{A&-l . low Device OrganizingPrinciple Conservation of mass MathematicalExpression Mass balance: ffi Field Chemical engineering inort ff_--* Over a unit of time period Amass:inputs-outputs ourpur Civil i e n g in e e r n g t structyz . 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 . NUMERICAT METHODS.-. For eochcose.upward force Electrical engineering ilH3 Conservation of charge C u r r e n t a l a n c e :+ i .1m77.I voltage drops for resistors >6->a:0 . ..lhe conservotion on whichthe bolonceis bosedis specified.l4 TABTEl.\ I Conservation of momentum Forcebalance: +Fv -Fn * + I O+ +FH ffi ?7fu. \ .r * z-Y RJ i:R: f L--\A7\--J Around each loop I emf's .

= 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 : . . 1-a.6 Summorythenumericol of methods covered this in book.r so thatfi-r) = 0 Optimization: Solvefor x so that/'(r) = 0 f(xl lbl Part 3: Linear algebraic equations Giventhe a's and the b's. ab .r's a ' . = arrx.h N:flt'Yl solve for r as a function of r .I.yJAr FIGURE I.solve for the..x. r xt a r 2 x .Ii+r= -]'i+ "f(ti.3 NUMERICAL METHODS IN COVERED THISBOOK r5 lal Part 2: Roots and optimization Roots: Solve for.

1.6a.However.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 .fterential Finally. set of valuesis soughtthat simultaneously satisfies set of linear algebraic a a equations.6a. initial-value Two typesof problemsareaddressed: andboundary-value . However.6&).root locotiorrinvolvessearching the zerosof a function.The As an engineer scientist. electriccircuits. 1. Part Five also covers methodsfor nume. or to categories: regression techniques developed this pulposecanbe dividedinto two general for in regression ernployed is wherethere and interpolation.rical difr'erentiation. Examplesrangefrom population-forecasting (rateofchangeof population) tlreacceleration a tallin-e of body (rateofchangeofvelocity).A plrysical interpretation ruurrcricctl of iltegratiott is tlre determinationof the area under a curve.x on This is bein of and science. 1.they originate in the mathenratical In modeling of Jarge systems interconnected of elements suchas structures. AND PROBLEM SOLVING Part Two dealswith two relatedtopics:root finding and optimization. equations focuses the solutionof ordirro'vdi.this involves the deAs terminationof a function's slopeor its rate of change. in that they are Such systerns similar in spirit to l'ootsof equations the sense are concemedwith values that satisfy equations. to problems.As depictedin Fig. (Fig. NUMERICAT METHODS.t6 MATHEMATICAL MODELING. Part Five is devoted to integlation and differentiation. Integrationhas many applications engineering in and science. particular.root location are and optimizationboth typically arisein designcontexts.nurnerical importtrntrole in the solution of diffbrentialequations.Part Si. 1. addition.rse is the curve to predict the intermediate values.interpolutionis usedwherethe objectiveis to determineintermediate ues betweenrelativelyerror-freedata points. linear algebraicequations Part Three is devotedto solving systemsof simultaneous (Fig.In contrast. As depicted in Fig. rangingfrom the determinationof the centroidsof oddly shapedobjectsto the calculationof total quantities In integrationformulasplay an basedon setsof discretemeasurements. Suchequations arisein a varietyof problemcontextsand in all disciplinesof engineeriugand science.6c'1. 1. you will often haveoccasion fit curvesto datapoints. optimizationinvolvesidentifying maximir and minima.The strategyin suchcases to flt a curve directly throughthe datapoints and r.Thus.and fluid networks. trend of the datawithout necessarily valIn contrast. as in Fig. Although somewhat different approaches used. As is a significantdegree error associirted with the data. described Part Four tFig. for optimizarion involvesdetermininga valueor valuesof an independent variablethat correspond a to "best" or optirnalvalue of a function.Experimental resultsareoften of this of is kind. you know fiom your study of calculus. 1. strategy to derivea singlecurve that represents general the the matchingany individualpoints.6d. in contrastto satistying a single equatiou. 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. For thesesituations. Such is usually the casefor tabulatedinformation.6e).

at (b) Plot the solution on a semilog graph (i. Pl. 9/1.(outflow) F I G U RP I .14).4). I d .9) is a solutionof to Eq(1.assume first jumper is 70 kg and hasa drag coI a efficient l2 kg/s. Can you make any statement regarding crrorsof thc calculationbasedon the the results? 1.' l: \ votume / tinflowt .2 with the in the sameinitial condition and oarametervalues.Computethc velocityto t: l2 s.3Repeat Example1. a1 that is Decay rate : -tc whereft is a constantwith units of day I.e. . o The concentration /:0 is l0 Bq/L.PROBLEMS t7 PROBTEMS l. (b) Repeat numericalcalculation Example 1.3 3 1.7). 3 1 9ll . Liquid is withdrawn at a constantflow rate Q to meetdemands.8). (1. At r: l0 s.7 Thc amountof a uniformly distributedradioactivecontaminant containedin a closed reactor is measuredby its concentration (becquerel/liter Bq/L).+Av'\ AT : 3 0 s i n ' ( r )- O : (kg/s). The contaminant c or decrcases a decay rate proportionalto its concentration.a rrass balancefbr the reactorcan be wntten as dc kt / decrease \ Ul decaS \ / 5ir 2 2 41 3 6/l 327.2.26 3 7 86 1 r 0 68 0 3 5 06 r 2t6B0 7/l Bit t 2 7.4Rather thanthe nonlinear rclationship Eq.4).rls : \ n / Use conservation cashto compute balance 6/ l. at Equation(1.14) can be written fbr this systcrnas d( . ln c versus/) and detenninethe slopc. compule velocityof a free-talling 1.6 For the fiee-falling bungce jumper with linear drag (Prob. (1.r': 0 when the tank is half full.obtain the closed-fbnn solution fbr thc where case thejurnperis initiallyat rcst (u : 0 at 1: 0). asan sume the chuteis instantaneously that deployedso that thc jumps to 50 kg/s.. witha stepsize of (a) I and (b) 0.l Usecalculus velity that Eq.i" tmta. how long will it take her to reach samevelocityjumpcr I rcachedin l0 s'l the 1. ( 1. If a secondjurnper a drag coefficient of has of 15kg/sand a massof 75 kg. this and ls a steady-state a transientcomputation? or 1.according to Eq. Use an initial conditionthat the parachutist size has upwardvclocity of 20 m/s at /: 0.811. 1. The contentsare resupplied a sinusoidalrate3Q sin2(t).5 s. of 1. Showeachstcpin thc computation. wherer'' a first-orderdrag coefTicient (a) Usingcalculus.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 . E m p l o y a s t e p s i z e f A r : 0 . Thercfore. l ll A storage tank (Fig. parachutist the usrng Er-rler's methodfbr thc cascwhcre rr : 80 kg and c' : l0 kg/s..Perfbrm thc calculation from / : 0 to 20 s with a step of I s. 2 d r .Use a value 12.2The following infbrmation is availablc for a bank acc0unt: Dote Deposits Withdrowols Bolonce I 5 I 2 . drag cocllicient / c h a n e ei n \ | .5kg/s fbr c'.Intcrpretyour results. r ( nr < dt / changc\ t .5For the free-talling bungee jumper with linear drag (Prob.8) contains liquid at depth) a where. the of the on 111. 8 E .

(rz The massof air can be obtainedliom the ideal sas larv: -P7 Mwt nt Plt : a(ltr')r O .Assumethat the initial condition i s . Assume the heat capacityC. Use volume-continuity solvefor the required areain pipe3 of Fig.8 m/s2. A rnore general representation Newton's inversesquarelaw of gravitationalattractioncan be written as R: s ( x ) : g ( 0 ) ( l-( + . 10. 1 .dr : nrc.8. AND PROBLEM SOTVING NUMERICAL by nr the heat capacity. rise during the first l5 minutes Calculatethe air temperature of the class if the room is completely sealedand insulated. In is breathingair.Ir': c. the exchange 0.The body will also lose 0. 1.Mwt V is the molecularweight of the gas (1br air 28. asdue to gravity was a constant sumed that the acceleration value of 9. 1. Assurne tial condition is _r: 0. we l. = 6 m/s A:=? FIGURE I. and cv: -300.35 litersthroughsweat.13 In our example of the fiee-falling parachutist.9 For the same storagetank describedin Prob. respectively.0.2. r ) ' Skin Qr.the differential equationfbr depth can be written as dl' dt .5 d.rr) o : . The parameter 1200m2 and p : 500 m3/d. and the body metabolicallyproduces0.Although this is a decentapproxinration when we are examining falling objectsnear the surtaceof the earth. suppose that the outflow is not constantbut rather dependson the depth..and throughthe skin. METHODS.ou.IO P Metabolism F I G U RP I .2. Assume air is an ideal gas at 20 "C and 101. I 2 E . and 0.and thc changein temperature the followingrelationship: or.3 liters. is lhe volume of the gas.1 1. P I . s i n . For this case.12 FigureP1.05 liters while inhaling.4 literswhile exhalingover a one-dayperiod. Note that by the heatabsorbed the air O is relatedto the massof the air where P is the gaspressure. tbr air is 0. sincethe surfaceareaA is constant r/t'O.O : :3: _ sin-ft) : drAA Use Euler's method to solve for the depth _vfron-rr : 0 to valuesareA : I0 d with a step size of 0.r8 MATHEMATICAL MODELING. = .10 The volume flow rate through a pipe is given by Q : rA.325kPa. and rRis the ideal gas constant[8.urine.12depictsthe variousways in which an averilgeman gainsandloseswaterin oneday.5 d.31. y: 0 .7 18 kJ/(kg K). maintainsteady how much water must be drunk per day? state.. whele u is the average velocity and A is the crossto sectional area. To feces. attenda classin a room which l.(trA Use Euler's method to solve for the depth )' fiom t : 0 to valuesareA : 10 d with a step siz-e 0.in= Qz'rt: 20 m3/s Food Drink u. Each studenttakesup about 0. kPa m]/(kmol K)].97 kg/krnol).and 0. The parameter of that the ini1200m2.One liter is ingested as food. the gravitational lbrce decreasesas we lrove based on above sea level.075 mr and gives out about 80 W of heat (l W = I J/s). 1.ll A group of 30 students lneasuresl0 m by 8 m by 3 m.O:500 mr/d. 1..4.

assume that upward velocity is positive.use the chain rule For to express differentialequationas a function of altithe tuderatherthan time. dV :: dt -kA where Z: the temperature the body ("C).r dt r9 of your final computedvolume and verifying that it rs consistentwith the evaporation rate. : time (hr)..determine the other flows. at ( a n d : t h ec a r t h ' s a d i u s = 6 .l-5 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).1 the casewhere drag is negligible. |. Use Euler's method to of computethe temperature from I : 0 to l0 min using a step sizeof I min if I. l.. that a sphericaldroplet of liquid evaporates at l.1mm/min andthat thedropletinitially hasa radiusof 3 mm.000m using a stepof 10. er-:..4.O. 3 7 x 1 0 6 t . Suppose arnbienttemperature that a cup of coft-ee originally has a temperature 68 'C.-*-*L*-i FIGURE I. use a forcebalanceto derive a ditlerential equationfor velocity as a function of time that utilizes this more complete representation gravitation. rvhere volume 1mm3). Recall that the chain rule is du dt du d-r d.16 Afluid is pumpedinto the networkshownin Fig.5min.1 mr/s. -s. (d) UseEuler's rnethodto obtain a numericalsolutionfrom r : 0 to 100..6... Assess validity oi your resultsby determiningthe radius the b o. (1.000m where the initial velocity is 1400m/s upward. Assume that ft : 0. lf 0.r : 0.However. lbr this derivaof tion.Compareyor. andQo: 0.8 rn/sr).: 0.2. of k : the proportionalityconstant(per minute). r : time (rnin). : T") at (c) Use calculusto obtain the closed form solution where u = u. k : the evapol'ation V: t rate(mm/hr).r (in m) at measured upward fronr thc earth'ssurfacetm/s2).PROBLEMS where g(r) : gravitationalacceleration altitude.$ .rr result u ith the analytical solr"rtion. l{ Suppose a ratethat is proportionalto its surfacearea.8). and 7. : 2l "C and ft : 0. (b.I6 P i ilr*?-i=-i--I iri:. dT -k(T -. : ths ("C). : 0.. Use Euler's method conrputethe volume of the droplet from I : 0 to to l0 min using a step size of 0.017/min.l "i "i "'i t'ili i-.gtO) : gravitational acceleration the earth'ssurface(! 9.Qt:O.at. R r m (a) In a fashionsimilar to the derivationof Eq. P1. and A : surface area 1mmr).2.16.

\ iiEi. .1 CHAPTER OBJECTIVES chapter to providean introductiorr overviewof is and The primaryobjective o1'this modeis usedto implcmentinteractive cornputations.4+'" .. (kg/m). thc irs.: a dragcoefl'icient the to tion can alsobe rearranged computethe dragcoefficicnt m8 ut (2.. wher-e : ternrinal velocity(nr/s).. a YOU'VE GOT A PROBLEM n Chap. . the colon ope'rator'. l. Llnderstanding priority rulcsfirr constructing undcrstanding ol'built-in lr-rnctions 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 . we usctlir firrcebalance detcrnrine tcrminalvelocityof a fiec-falling 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 . I T tsi V t.f zr'+ 4_ %. .--and thc mathernatical cxpre-ssions. Lcarninghow realancl cornplex nurrrbcrs assigned variables are to are valuesusingsinrpleussignrnent. how MATLAB's calculator Specificobjectives and topicscovered are ' .q: gravilational accelerertion r.p.l i.this equaand t'.Asidefrom prcdicting terminalvelocity..m : mass(kg).r. Fundomentols MATLAB 2.:cand 1oq1.l l'ttnctitlns.t (m/s'). uE-1 5t %I zl x+ = . to on Learninghow to usc vectors crcirtc sinrplclinc plot basecl an equation. . Learninghow vectors and matrices assigned .piic:.

A B s k r p s a l i n c b c t w c c l l t | .ml s Dotoforthemossondossociotedtermino|velociliesofonumberofiumpers BO9 653 gll 929 721 6A2 5l l 836 477 54 557 la \ 509 (1.3.mmand anclgraphics we In this chapter..rake crcatcM-iiles' prompt *iii ut.ol.=..varuaun comlrtnds.ln MAft-AB progralns' ff.tring oroficientist'oacttrallyinl.ctilculaticrns. . u h .t.iumpers the we nleasure terminal Thus.1 of knclwnmass.: ii' ffi:l :'. toope * *' 1P."pt.l rl :n rk 'l j n c s t' io :r' r . c i s e . ( ..B usesthreeprimary anddata' winclow'Uscdto entercommancls .t l r r c to .. 2.llctlentthcctltntrratrclsonMATLABasyouprocccdthrotrghthe following material' witrclows: fUeif-A... i t h e rr r t a n t co.'^ :::):i:I. 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 ..r | a h e l ( l . y o t t t e n m. ::::::: "v rute grs crcn pot rn ":1 ati "'"u'TilJffill'. H( err: r r '. will leartr In this showinghowMATLABcanbeenlployedtocolnputeqtrantitieslikeclragcoetTicients'we willalsoillustratchowttsgraphicalcapabilitiesp.r to inrple.I TABTE m. wilr r. C. 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 . ^ n ^ ^ ^ .st a n c l s . a uery l'ancy -_-. .this velocityof a numberol .":ili:. B e y o n d l r n : r l v ..] ) .'Ji:'. if in The clata Table2' I werecoltt.1 i(1.i ( L mfirtis1Ac h b . many typcs.2r MAT4gllyllerynENl 2 r THE 2.' 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'r-result by line' For eachcotnurancl' Lh::s' line mancls you typc tn For calcttlator' exanlplc'if ^ii"g f if.:il{.ciscness.:. [ 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 .ws' ln chap' 3 we usc of thc c. ^ . ffi qA ' .ttt-.$ l . r r l . 51 Lo thc rcsult' MATL.kg u. n o t c ' . zt ct how MATLAB canbe uscdto we chapter. 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.::'# "'*. perfornirrg " "!i:H:i:iilll1l.* windtlw' Usedkr gaii winclow'Usecltt'rcreuteand edit M-filcs' .adtlititlnalinsightintosuchanalyses. wind. a r t. 1c ' ' M A | l ..-ILJ 8 b zt\ 4x AL ^r- 4 ?t 5t !8 . ) l t n d t h c n u n l b c r .*i. T in fiont o1'youlcornpLrtcr.. the edit window to openwith thc commancl tht tu'ntunti window will Afier startingMd. n r r n analyzer ' s r rd lh u . we show how sr'rch o n e f u r t h e r . Conrtnancl plotsand graphs' display .AB will disPlaY a. .lestimatethe drag coeificient' provideso *t-' equation lectu'lorlhis purpose' d ^ m . to create can commands be usecl Chap.i.:ll.|. shoultlrcad it while . .:.

mand line with the semicolon ) character. and if you usethe semicolon. enter >> g and then enter >>A long bank eng Seehow their valuesare distinct. 2.For example. 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.-f J _A zfTtr ='1.ans. twe short Iong short e long e long a eh^rt end 4 MATLAB treatsnames in a case-sensitive rnanner-that is.2. frT-Tbl . they will be displayed.1 Scolors The assignment values scalar of to variables sirnilar otherconlputer is to languages. 2.t. To illustrate this.x = .1 .2 ASSIGNMENT Assignment refersto assigning valuesto variablenames.st 4 zl-+ Z] X+? "L x jj€- -z th -r 15/ I . You can type several commands the sameline by separating on them with comnlas or semicolons. the nan)e a is not the same as the name a. you separatethem If with commas. typing Try >> A = 5. they will not.Thev are distinct names.22 MATLAB FUNDAMENTALS Notice that MATLAB has automaticallyassigned answerto a variable. This resultsin the storage the of valuesin the memorylocationcorresponding the variable to name. lt carrbe suppressed terminating comof the by (. Thus. >> a 4.A = 6. Try typing 1 _ A Note how the assignment echoprintsto confirmwhat you havedone: 4 Echoprintingis a charactelistic MATLAB.

type >> format short The f<rllowing is a summary of the format commands you will employ routinely in engincering and. example. .[h 4 el 5t I Z} X+ .0000 + 4. 1 4l 5 9 2 6 5 3 5 8 9 7 9 3 e + 0 0 0 3.65]. to "/J Consequently.__.1 41592. Thereare several variables.€ - -a.2.0000 + 4.1415q1b5J58979 l.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 It shouldbe notedthat MATLAB allows the symbol j to be usedto represent unit imagthe inary numberfor input. you desire If additional enterthe fbllowing: >> format long Now whenpi is enteled resultis displayed l5 signilicant the to figures: >> pi 1. r416 i. twe . For example.1416er000 3 .r416 3 .since MATLAB handlescomplex arithmetic automatically..__i + .1415e+0tr0 3 . Noticehow MATLAB displays fbur decinral places.However. >> x = 2+1*4 2.. 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 --:-----.2 ASSIGNMENT 23 We can assigncomplex valuesto variables.5897 9 1.scientific calculations.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 .141592b5-1 8979 5 To return to the four decimal version. r4r6 precision.it alwaysusesan i for display.They all have the syntax: f ormat typr. I --T a-*!1.The unit irnaginary number ir preassigned the variable i.complexvaluecan be assigned a simply as in >> x = 2+i*4 2.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. fbr >pi 3 .0000i predefined pi.:a.

For example.. lr23 456 7 8 9l ffi qA1. A column vectorcan be entered several in ways. 4 6 I 10 I' Tlre resultin all threecases will be b= 1.rple.24 MATLAB FUNDAMENTALS 2. - . or _-.Try them. or. 6. zt -+ zf :5yst LIZ 4 a_J---*= _r -{ "L x jl':vI A <. by transposing row vectorwith the ' operator. Enterkey woulclbe struckafterthe 3. of practice.For exan.->A.1 areactuallya matrix with one low andone column. 4 b S 1t) A rnatrixof values can be assigned lblkrws: as r'23 456 789 In addition. When we speakof vectors. lh -r ?l 5t I Z] X+ . Enterkey (carriage the return)can be usedto separirte rows.2. usuallyret-er column vectors. Bracketsare usedto enterarraysin the comnranilmode. a >> Lr = | 2.2 Arroys. In row vectorsare rarely usedto solve rnathematical problerns. The scalars usedin Section 2. row vectorcan a be assisned fbiiows: as 123451 Note that this assignment overrides previous the assignment a : 4. Oneby dincnsirnalarral't arc callcd tu'/ort and two-dinensional anavs are c'allednrutnce:. I0 l |2. the in the followingcase. we to which are more contmonlyused. the 6 andthe I to assign the the matrix: .2.. Vectors qnd Mqtrices An arrat' is a collection of values that are represented a single variable name. 8.>b1.

2. LrsinqJ 175 bytes Note that subscript notationcan be usedto access individualelcmentof an array. an For exarnple.( j . . ....tlt si 8 Foranarray. [h Y vI t('€ .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- . respectively.cnterthc whos command: >.If a colon is used to separate tw'o nunrbers.3 The Colon Operotor The colon opefatoris a powerfll tool fbr crertingand manipulating arrays.A(nr ) selectslheclemcntinmthrowandthcnthcolurnn.1) 6 Thsre are sevcralbuilt-in functions that can be used1ocreatcmatrices. For the ones llnd zero:i l'unctions creatcvcctol'sor matricesfilled with ones and zeros.trrb l e doubLe array array i)i I'ay arr.whos f Jd r { i e A dns l) x(comfiLex) Clrancl t-otal Sr ze lx3 I x5 LXL 5xi '1 xl Byte:: 12 40 8 4Ct I6 Class clouble cloubLa il.Lrray is 21 elenrrrnl.) J 00 UU Sintilarly. with moredetail.Forexample. ( 2 . firurthelement the o['thc columnvccfort) can be displayed as :> b(4) .r. Both havetwo argumcnts. creatca 2 x 3 matrixtl1'zeros: to thc number-of columns. .2 ASSIGNMENT 25 At any point in a session. list of all current a variables be obtained entering by the can v.y tlouLr Lc: .?'' 5l I Z] X+ . cxarnplc. c I o s .n-Zt* 5t s L <. .n A(:.s. first tbr the nulnberof rows and the second thc tbr For example. the or-resfLtnctioncan be used to create a row vector of oncs: u = o n c : : .rrc: b x or. E : 7 .2.l) 2.hocommand: > > rvho Your yariabl al es ans .

For example.( l.the colon can also be usedas a wildcard to select the individualrows and columnsof a matrix.the secondrow the the of the matrix A can be selected in as >> A(2.26 MATLAB FUNDAMENTALS incrementof one: -: l1.The 1 inspace functiongenerates row vectorofequally spaced points.the function alromatically generates points. x2. * J Zl eNst "L x vI -tlh 2- 3l ZI X+? =$8 .5000 2.2000 0.J whi For I If colonsare usedto separate threenumbers.:) We can also use the colon notation to selectivelyextract a seriesof elementsfrom within an array. n ) which generates points betweenxl and x2.When a colon is usedin placeof a specific subscript.6000 o. n) ).4 The rlnspace ond losspace Functions provideotherhandytoolsto generate The iinspacre and logspace functions vectors of points. For example n '> linspace(0. Alsc Res 2.0000 1.3 rnr 1.5:3 f- Ifn 2. MAILAB generates numbersbetweenthe the first and third numbersusing an incrementequal to the secondnumber: >> t = L:0. It has the form logspace(x1.0000 ope Note that negativeincrements can also be used >-" f = i0:-1:5 com t0 9 B 1 6 5 Aside from creatingseriesof numbers.6) 0 0.5000 3.0000 2.It spaced a has the form I i n s p a c e (x l . -t ffi= ?l -+ Ll 2.basedon the previousdefinition of the vector t : > > ' 1 -( 2 : 4 ) 981 Thus. x 2 . colon reprcsents entire row or column.4000 0.BOOO 1.1. 100 generates row vectorthatis logarithmically The logspace l-unction a equallyspaced.0000 plv thes ing Th AS lf the n is omitted.For example. the second through the fourth elements are returned.2.

because exponentiation has higher priority then negation.similar to other computerlanguages..-.0000 If n is omitted.3 MATHEMATICAL OPERATIONS with scalarquantitiesare handledin a straightforward Operations manner. scalar real variables can be included: >> Y . As with other computer calculation. 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.. order of priority.-. in zl'+ .832 Also.0000 100.r 2*pt 6 .5533 Results of calculations can be assigned to a variable.45 0. Try . 4) 0. as in the Iast example.2 . The common operators.i. the following result would be obtained: >> Y = y4 ^ 2 _r6 Thus.it automaticallygenerates points.2.0000 10..4 is first squared and then negated.3 MATHEMATICAL OPERATIONS 27 which generates logarithmicallyequally spacedpoints betweendecadesl0'r and 10": n For examp)e. as in the next-to-last example. >> logspac e (-I ."1 t lJ ----- ffi=+ r\ a - *-6 -l -=-:-T -{ 5[ s\ __. the priority order can be overridden with parentheses.Pt/4.It will be discussed detail later in this book. >> y ^ 2. or simply displayed.1000 1. [h .2.l 4 Z] X+ 6re %& <. "L x . Parentheses be usedto overridethe priorities can as in >> Y = \-4) L6 : Lefi division appliesto matrix algebra. For example. 50 2.

introducing sorne of those manipulations here.:. try >> a * A .00001 The real power of MATLAB is illustrated in its ability to carry out vector-rnatrix 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 . :. i t i s w o r t h .1011 0 >> x ^ .0000i 0. [h - .d*b r10 and likewise. and >> b = Now. The irtner product of two vectors (dot product) can be calculated using the * operator.0004 >>'1 /.2a MATTAB FUNDAMENTAIS Calculations alsoinvolvecomplexquantities. Z ffir qAb zl' -& ZN ux LJt ffi A i- x+ z_! 1J'€. f4 5 tjl'. ti 2 l.< +I2. 8 . l0 t6 4?.2000r ans -12.0000-r an!.2 0. can Herearesomeexamples usethe that valuesof x (2 + 4i) andv ( I 6) definedpreviously: '>.O0tJ0 + 4. lB.: . 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 vector-matrix mLrltiplication.sJ 5t 3$& <..llrst redefine a and l:.0000 >> x + y +1r. the outer pnrluct >:.'3 * x ans 5.

30 66 r02 35 81 t2.^ 2 2a 36 64 81 ^ operatorsignifiesthat the operationis to be carriedout elementby The ."I LJ ' -2.The MATLAB manualcalls thesearray operatlons. For example.t\€ -l- z_|lh I .\46\ 0..82 0.9f 5l .?.6 4?.5915 2.s.At times.2132 ?. lh x _-T .5366 I. n r49 16 49 ffi z\4 . precedingthe element. MATLAB provides for that too.. 9tr 1 50 results matrix multiplication a with itself.9099 2.ons must aQfree. in of What if you want to square eachelement e? That can be donewith of >> anS A = .2.2. 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.3183 'I . They are also often referred Loas element hy-cIeme t t tparutit tn.gt st -l "L 4 <.(-h Zl. you will want to carry out calculations item by item in a matrix or vector..3 MATHEMATICAL OPERATIONS 29 >>A*b 32 71 r22 Matrices cannot be multiplied if the inner dimensions are unequal.8648 We must always remember that MATLAB will apply the simple arithmetic operators in vector-matrix fashion if possible.9549 1. Matrix-rnatrix 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.

eJ 5t & Z] X+ . of You can useonline help to find out nroreaboutthem. 2.6094 2.^ 2 PressingEnter will perform the calculation again.0194 1. MATLAB will evaluate the matrix version when the letter m is appended to zf+ . not LOG(X) Complex See also results LOG2. acos. a eys\ -1 -l "L 4 <' [h Y ll'€ . have matrix definitions also.try >> log (A) 0 1.4 USEOF BUIIT. For example. such as sqr t.6931 1.to the matrix A.1912 and you will see that the natural logarithm function is applied in array style. and exp. type rn >> helP LOG log' lrlatural is logarithm. 3 1821 64 I25 343 5r2 216 129 Using the up-arrow key. For a list of all the elementary functions.natrix quantities.changeit to the line below and then pressEnter.9459 0. You shouldget back the last line you typed in. But you can also edit this line.3863 1. such as exponential and square root.Pressthe up-arrowkey. abs. For example. >> A .".0986 r. operate in anay fashion. sin. For example. of X the is elements posrtive.30 MATLAB FUNDAMENTALS MATLAB containsa helpful shortcutfor performingcalculations that you've already done. LOG10. The up-arrow shortcutis a quick way to fix errorswithouthavingto retypethe entireline. Most functions. you can go back to any command that you entered.INFUNCTIONS MATLAB and its Toolboxes havea rich collection built-infunctions."1 {z '\-- ffi= qA+. Press the uparrow until you get back the line F * - Alternatively. the nat-ural are lograrithm produced EXP. element by elenrent. Certain functions. if LOGM. >> A . of X. f anh.if you want to learnaboutthe 1og function.79'LB 2. type '> help elfun One of their importantproperties MATLAB's built-in functionsis that they will opof eratedirectly on vectorand r.you can type b and pressthe up-arrow once and it will automaticallybring up the last commandbeginningwith the letter b.

sort (F) 1 6 3.4498 + 0. >> sum(F) 19 In a similar way.594A1 0.3134 0.4 I.0.Try >> sqr:tm(A) 3l A.8000 360 13456 ffi Z} x --T<.FUNCTIONS 2. >> F = l3 5 4 6 Il.1611i 0.4 USE BUILT-IN OF the function name.5 I. For example. lh . suppose that we enter a vector: >> E = l-1. it should be pretty obvious what's happeningwith the following commands: >> min(F).3481l 0.5 -I.f\€ .A22Bi 1.2515 + 0.0.4 1.4844 2.6 -1. The round function rounds the elements of E to the nearest integers: >> round(E) -2-2-1r22 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.2068i 1.prod(F). sumfunctionreturns sum of the elements: the the arrays.1623i 1.0185 + O.AB42i I.9f >l 4 -l- & .max(F).5555 I.0385i + 0.5813 .6l.9503 .5526 + 0.mean(F).21I7i - There are several functions for rounding.

For our case.n is mass(kg).81 m/s:). jumper canbe computed that the velocityof a free-falling bungee with [Eq.25. crl = 0.5823 br. 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 : .( g * n / c ' d ) * 1 .6175 51..re array.951 a 49 .l/ r . [h - zl'+ .6416 ( ( I ffir zl Llz 7f 5[ st -{ -l L vI x j_f€ .01 62 46 .is the drag coefficient(kg/m).1871 51. w h e r et h e f b r r n u l a s i posicomputedfbr each value of tl.9)]: 2.sJ 5t <.r \V trt / due where u is velocity(m/s). . values a from 0 to 20 in steps 2: of Create columnvectort that contains .1.'> l. c. \ Th bo [0:2:20]' Yor Assignvaluesto the parameters: >:.'t292 33.9 = 9. : .r118 42 .a n h ( s q r t ( g * c c l/ n ) * t ) i t) 1B. -/ : s q L t .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.r I V c. (1.and the result is assigned a corresponding r to tion in the u array.= t.5 G gft M. / a t a n h.32 FUNDAMENTALS MATTAB Recall A common useof functionsis to evaluatea fornrula for a seriesof arguments.r1 -\ Z] X+? .4560 51.81. and r is time (s).42r4 50.g is the acceleration to gravity(9. in 68. 1 ( r ) .

' l t l '!ry l I i l I a o l 6 : 30 20 10 0 : i i j I . . lh --4 l.J ^:.€. -> y l .2."lqfjeTl zl a-* z.enter graphof the t and v arraysfrom :..r' ffi z\4+ ..ef >l 3$& .:'> qrid til. L b c l Plot of u versus f 60 50 40 : . the dataabove. ". I i ! ..f./) lne gfapn appears ln tne grapnlcs wrnoow ano can De pnnteo or translerreo vra tne clrpboard to other programs. 60 50 40 30 20 10 0 8 10 1214 16 18 You can customize grapha bit with commands suchas the following: the >> :':.re('Plot of ('Valucs ('Valucs v of ol ver':jus L') v') t') .- : "-t--* l I i I l 8 10 12 Valuesof t 14 16 18 ?.5 GRAPHICS 33 2.--.' xlabel .5 GRAPHICS For example.t. x+ -l- <. nlol (t.to createa MMLAB allows graphsto be createdquickly and conveniently.-.

.For example. hol d c.l. lLoLd on (t.o') It shouldbe Inentioned that. the :'' pIoL (1.l 5[ st J'- - ffi aAI z-t\ -x v! rf € . Fclrexampre.nconrmancl is The hoidsthecurrcntplot ancl axis propall ertiesso that additional graphingcommancls be addedto the existing can plot.1ol. \r. we had typed the tbilowing if cornmands. you want to useopen For if circlesenter r-:. Colors Bl. The h. another hanrlyfunctionis subplor.conlmand clisplays solidline by default. (t.34 MATLAB FUNDAMENTALS TAB E 2.I.) >> plot ln contrast. zt'-+ =.r) v.. yt t.) v. >(t.the f<lilowingcommandswould resurt in both lines and symborsbeing displayed: ':' '-.: 7l l Lt r --7iJ . flnal plot woukl only displaysyrnbols. v. [h - Z-} X+ .r. PlotploL -.o.l:jlot (t. pJ This cornrnand breaks graphwindow into an m-by-nmatrixof the small axes. previous plotsareerased everytirnethe plor command implernented. which alJows you lo split the graphwindow into subwindows p(ute.ruto displaymorethanonedataset on the sameprot. you wantto plot a If eachpointwith a symbol' you can includea specifierenclosed in singlequotesin the pior functio. n.:2 Specifiers colors. Table2'2 Iiststhe available specifiers. ho1c1 off In additionto ho1.1 5t a.) MATLAB allowsy. (L. .. if you want [o connect eachclata rnarker wirh a straight rineyou courdrype :'i.by clefault. ond linetypes.and selects the p-th axesfor the currentplot. .re Green Red Cyon A^agenio Yello'"v Blccl. example.tit of f cornmand returnsto the def'ault mocre. for symbols. 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. 'o') ::-. .o. has ()r It the svntax subploL (n.

p 1 o t ( s j n ( L ) .We employ the subplot commandso we can subsequently the three-diand.er e command.2. y.la. y. lh Z] X+ .eJ 5t 3$8 <. > > s u b p l o r -( 1 .5 GRAPHICS 35 We can demonstrate subplot by examiningMATLAB's capabilityto generate threedimensionalplots. First. and z arethreevectors the samelength. c o s ( r ) ) >> axis squaLe >> r. anrJ are of z. I ) .irle (' (a) ') As in Fig.I 2 A twoponeplotof (o)o two-dimensionol ond (b)o three-dimensionoJ circle helix (b) z['+ . z) wherex.y add mensionalplot."I LJ ' ffi=b 1t 5t(st -l "L Y vI _-T j} € . 2 . y.the result is a circle.= 0:pi/50:10*pi. 2. Note that the circle would have been distorted if we had not used the axis sou. of The resultis a line in three-dimensional space throughthe pointswhosecoordinates the elements x.cos(r). let's grapha circle with the two-dimensional plot function using the parametric representation: sin(t) -r: . >> l. Plotting a helix providesa nice exampleto illustrateits utility. FIGURE . The simplest manifestation this capability is the p1or3 command of which has the syntax p1ot3 \x.

log-logor semilog next.rres graphicsthat are useful-fbr example.and technical Thereyou will l'indlinks to productinfbrmation.As described to as solll. that lnc. ) .kt .mathworks Finally.Fol example. newsgroups.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. couldenter the cornrnands functions l-. websiteat www..plottingon the complexplane.y coordinatessketchout the circumference the circle in the r-r' plane in the plot. by Help lacility thatcan be accessed itsell'includes extensive an Further.you can obtainhelp tionr The MathWorks.Palrn.Thi..ces available learnaboutthese well as otherMATL.|€. [h - zl .2005. p 1 o t 3 ( s i n ( L ) . support as ffiar z[ Ll 2 --=-al' -l_t zl'+ L1 r -r 5y 5t "L x 4 a.AB capabilities.rri t-hnr. of Thele ale other f'eatr. . it prclvides rnodeby typing the in As described this chiLpter. 2.r . However..l b. let's add the helix to the graph'sright pane. in help is also available interactive or fbllowedby the narne o1'acommand function. well as a varietyofother uselitlresources.corn.and contourplots. L ).)' cos(1). simultaneously. 2 .. The resultis shownin Fig.r and . lrclp conrmand If you do not know the narne. Can you visualize of the .you shouldconsultone If o1'MATLAB's capabilities.familiesol curvesplots. 2 ) .As such. access a numberof instructive to dition. package the window.1 5t =$8 . we againemploy a para: and metl'icrepresentation: sin(/). The net resultis the characteristic cally as the : coordinate of springor spiralstaircase shape the helix.three-dimensional meshplots. 2 0 0 7 ) .g. are 2.x+ 1.Hanselman Littlefield.you can use the lookfor commandto searchthe suppose you wantto find all that of MATLAB Help files lbr occurrences text.36 MATLAB FUNDAMENTALS Now. 2 0 0 5 :a n dM o o r e . books. you are interested learning and to of the excellent booksdevoted MAILAB (e.) >> rirle('(b) what'sgoingon?As time evolves.. . adof ditl'erent optionsfbr exploringand searching detnos. u : / x: ' > s u b p L o t ( I . c o s ( 1 .To do this. you and that relateto logarithrns.'t irhn anclMATLAB will displayall ret'erences includethe word loq.plotting obiects of instead lines.a varietyof replots.s will provideyou with a nutnber clicking on the Help nrenuin the comnrand In thloughMATLAB's Help material. curverisesvertithe samefashionas the two-dimensional increases linearlywith time.it is obviouslynot a comprehensive of in rnore.

we must include periodsprior to the operators: on element-by-element (r.')t) lr) for some statistics the We can now use someof MATLAB's built-in functionsto cenerate results: . let's start to play with this data by using Eq.1 line) to help assess a ffi= qA+..ll. the averagevalue is 0.9 .(lL'r '.) f " 1i. 1 . superimpose line indicating exact predictions(the l.9 (tir. /!. . (2.3125 of Now. engineersand scientheserelationships collectingand analyzingtheir own data.l llr.il') l). ' a l.1).'rrt::l!-1 !. r. ln tion. r ' " ')1 Vpl t r:Ci 'rl-t. .1 alongwith gravitational acceleration be entered . .1 . However.1. we will use MATLAB's graphicalcapabilitiesto discernpatternsin the data.. 1.Although theseare of great utility. most cases. v l ) ] .we will illustratehow MAILAB facilitatessuch exploratorydata analysis. 9.)/ 4'1..1 . can as The datafrom Table2. : l a l r L ( g * r l t r t r . :.25l l to 0. J . tr-jrr1.1 !0. .l ... prior to arriving at a final predictive equaand developingplots.. .4 4|J.l 8t:1. intent is to gain insight into the pattemsand mechanisms our In this casestudy. 1 r 7 q = 1 1 r ( ' .i !.tt.fl1. The drag coefficientscan then be computedwith Eq. i i r = t i ri r r ( .r. beyond merely computing the drag coefficient. r 1). renowned scientistsand engineers..--. ..1) and the data from Table 2.o f. (2 l) to makea prediction the drag: teuninal velocity basedon the average '.4 rlclrrr rr i i) l i .e[ st -{ x -z [h /_l€ .. However. Becausewe are performing operations vectors. . 'q ( r ' r l ) - r'clnt.r . by tists often must supplement Sometimesthis leads to a new forrnula.ll 7. i Li. (2.7 CASE STUDY 37 EXPLORATORY ANALYSIS DATA Your textbooks are filled with formulas developed in the past by Bockground.2854 with a rangettom 0.1l'-r'r. 9).1 l) .l r .1. : 0. n . we usually "play" with the data by per{ormingcalculations hiddenin the data.tr:.iii. z(r Ll 2 .l 91. '.91.1 4l.2..ll1r'.eJ 5t ?$ Z] X+? I .e[r + tr4 -\ 2.We will do this by estimatingthe drag coefficientof a free-falling human based on Eq.1 'rl. Solution. .i ] .0.1. J12! kg/m.We will also the results. l .-.. l).5. 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. 1 . (l ( i l l a '. Thus. .

Labelth scissa z.) q(t): Lrurar(l l. i ' l : rl. notice how the model tends to underpredictthe low velocitiesand overpredictthe high.R t / ( 2s 1 ('Irr i. i 1 1 l rf li l .. drag coefficientsversusmass: This can be seenby plotting the estimated x l .l] r o l r i I i . . ) .2' suggeststhat rather than being constant. we employ the subplot command: . 1 ) . v l .' : : 1 i t n a l ' t .rlrt'I 2.35 o)F o<.qo:tl and ( L: inductance. . r( k q r ' t ' .1 A simple electricc pacitor. ' 1 . L r r i : c l)' I i.) The resulting plot. that This suggests ratherthanbeing constant. t! o.crd generallyfollow the l: I line.l ' . ( ' . v p r ( . . l wherer:time.:rlic lL-'tl') 'l'l ol ol I i t lc] ( I)t ('(l i ( t. ' l .R:50'L-j 2..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. r l L l r . t g r : t . . theremight be a trendin the dragcoefficients. the drag coefficient seems to be increasing as the mass of the jumper Use MATLAB to ger z: -4 to 4. L i r l . : .Y oo r!o U. and an inductor on the capacitor4(l) as as ( " / t . [ ' l ' I i c t l l r l ( ] i q / 1 l l) L i 1 . generatea plot of this fi 4o:12.J q) o O o o c. ' ' : : u l . q t ) g . .2. i c t l L \ " r r : . [h - rtE-:l 5 I Z] X+? . which is the bottom graph in Fig.However. r : r f . 2. p l o t x I a b . i . 1 ( .) 0 .2 FIGURE with Twoolols creoted MATIAB Becausewe are going to eventually generatea secondplot. ) ' . 2| 60 65 70 80 75 Mass (kg) 85 95 PROBTE 2. t : .2 The standard norrr curve that c bell-shaped . as ffi= llAb zl'+ : : "L x vI <. you the As in the top plot of Fig.2. L e 1 ' .l ( ' l r . r i r e I ( ' n r . . ..because predictions might initially concludethat the averagedrag coefficientyields decentresults. y r l o L( 2 .O I c 1 L . ' .

r7o thc initial chargc. a n d C : l 0 r . In addition. 2.1A simplcelcctlic circuit consisting a rcsistur.2) good assumption thejumpersall took off from the sameheighton the sameday). [h - 1-l€. you would discoverthat the parameter of c. : a dimensionless drag coefficient. caol' a pacitor. the result might also stimulateyou to go to the fluid mechanicsliteratureand leam more aboutthe science drag. ] . zt'+ . with a larger numberof At the least.4 Usc colon notution lo crcatc vectors idcntical to the (#)' .:.If you did this. .. 5 I l ' a l i r r c c I . Z] X+ .. .p : tir density (kg/mr).r : l i r n c t i o n : ( a ) r ' : I i r s l ) . givcn that q o .0 . as FIGURE 2."1 {t 6 e[st -l -l x <.I P ffir qA+. Assuming that the densitieswere relatively constantduring data collection (a pretty (2. I 1l I = inductance.L : 5 . I I ) 2 . P2.q.7. 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 : .1 2 R : 5 0 .] : whcre= time.=-. T'hcchargc is and onthecapacitor as a lirnctionol'limc can bc cor. f U s c t h c I i r : r p .F.you might concludethat your model needsto be improved.. : the rcsistancc.1 to.1 2. r c e ( r .0 Io 0.i t s d i s a placcmcnt. if that heavierjumpersmight have largerareas.PROBLEMS 39 continued increases. Basedon this result.l. This hypothesis could be substantisuggests the atedby rneasuring frontal areas individualsof varying masses.r (rn) can ol'tcn bc rnodclcd by Hookc's law: Ij:lr Inductor It-\/ \ ! / - -.2Thestandard norrnalprobabilitydensitylirnction is a bell-shaped that can be rcprescntcd as curve l/ . Usc MATLAB to and generate o1'this r plot lirnctionliorn / . and A : frontal area (m2). 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.eJ 5t 8.it rnight motivateyou to conductfurther experiments jumpers to confirm your preliminaryfinding.( 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 . which is the areaprojectedon a plane normal to the direction of the velocity. C : capacitancc. :-'- \/ llt Resistor UseMATLAB to gencrate a plot ol' this fbnction liom . r c ' { Ir l . of PROBTEMS 2. I . Labcl thc ordinateas lrcquencyand the abscissa:.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. t t ( b ) | : 1 r n s l r .nputcd ri(t) as 2 .2) where C. [ ) .1. an induc(or dcpictcdin Fig.

1() Hcrc is sorncwirrd tunncl data lirr lirrcc (I-) vcrsus ( velocityr'): LlseMATLAB to storeF and.srnI nt . slopc.g4.v.r! 51 . 5[st ::- jlg- . 0 3 0 4A 50 60 7A B0 hascdon the cubic lbrmula.r. th -r .rction: BH \t' /s/ .6 The density treshwater he computed a lunction can as oi ol'tcmperaturc with the firllowinecubiccquation: p: x .6 oF.Thc lirllo'.8 f ( l . Use thc max l-unction dcterminethe maxirnumpotential to cncruy.r2 l- 2. Usc whcre p : dcnsity (g/cnr31 and 16' : tcmperat. lunctiont)pcratcs an idcntical in fashion (basc-10) to thc frlot. Use MATLAB to creatca plot o1'thcsine(solid linc) along (dashed line) up ttr andinr. l 2. >> x ^ 3.99987 i (a) If the air density is p compute values of tl cD.9 The senii 1o9i.2._ 4.12 Thc Maclaurinscrics cxpansion thc sinc is tirr 00r5 0 030 a a22 oo0to 0 0008 0 0c103 l9 '24 t5 t5 3 '25 .5622x l0 s 11 + 0. min c.t1.40 FUNDAMENTALS MATLAB 2. (c) Develop a side-by-si C.:.13 You conractthe jur Table 2. m l s 1 0 ? .020 0 0002 2. >> r (nr/s). u .N ll r. Rccallthat T( 2.17 B 4.t as vectorsand thcn conrputc vcctorsof the spring constants and the potentialenergics. Usc the Lruilt-in tirnctionf . n: roughncss LJsr'MATLAB to crcatc a plot displayingboth thc data whercU: r. the Make the rangeof li'onr.r7 71 .Convcltthrsvccof the'n tor tclclcgrecs Celsius ancl computca vcclorof dcrrsitics -i"1.r : O to 3tr 12.(usingdiantondsymbols)and thc llnction (usirrga dottcd Ii: ing datais availablc I'ivechannels: lbr l i n e ) .6 7 < ? 6 6.1 and measure values. Usc this l'unctionto plot the data ancl l'unctionas dcscribcdirr Prob. yu. lt 2.v=e . versusrr (right sic and titles on the plots 2.Explain thc resu s. 2..15 Exactly what will t MATLAB commandsare I (a)>>x=z.r'') +-9l one Storethesevaluesin a matrix whereeachrow reprcscnts oi'thc channels cachcolumn represents of the paranrone and ctcrs. lirnctioncxccpt(hatlogarithnric arc n5BH lirr both thc.v.1..clocity S: channel (nr). The potentialenergy storedin the spring U (J) can then be computedas U: -(. B I 0. .rr 0013 )2 0020 l5 9 0C09 0010 t2 0012 by This datacan bc dcscribed the lbllowing function: (. Crcatea plot ol'p vcrsus 't5 7A 3BO It.l) in the bottompane 2. I" : 0.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 . which are order conespondingvaluesin T Five springsare testcdand the following data compiled: t. ('C).r'axcs..1. (b) Determine the averag resulting values. cosl .5 2 8 9 l 0 ..r:c -ll l.r5 .r'l . .r ancl .14 The following para that contractsexponentia .1/ both the data Usc MATLAB to crci\tc a plot displayirrg (r-rsing squarcsymbols)and the function (using a dotted l l i n e ) .5 .2711ttt')x+t rt \B+2H/ Use subplor to genera (. Explaintlrc rcsults.m2 o45A O4OlO F. N 550 6r0 t22A 830 1450 : 519(7'F 32).5 0 1x t o ' .urc rangingliorn MATLAB to gencrate vcctor of tcrnpcratures a 'F 32 "F to 82. 2. PPm t0 4 41 2A 26 3C t6 40 t3 50 i0 60 c5 A. .rct1)r ial in conrputing scriesexpansion.l 1 5t I Z] X+? .vith plot o1'thc scries cxpansion a cluding the terni -r7171.-1 usingincrcrncnts 3. Sln. lirnction cxccpt that a logarithrnic scale is used lirr thc l axis. the abscissa +fi6-qrlal 4-t z['+* 7l Llz *2f -----j zl 4+ " a.8 . . cocfl'icient.ll Thc loc_{locr lirnclionopcratcs an idcnticalI'ashion in scalcs uscd to the fil()'..r and /1 : dcpth(rn).Writc a single-line MATLAB statcment conputc a to column vectorcontaining vclocities the based the virlues on in the parameter matrix.00r.I=-t-+ . 2.8 lt is general practice in enginceringand sciencc that equationsbe plotted as lines and discretedata as syrnbols. I0.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. .Usc this lunctionto plot thc dau 0 035 0 0001 t0 2 and lunctionas dcsclihcdin Proh. >>Y=B-x (b) rt q = 4:2:L0. ({) Here is somcdatafbr conccntration \'elsustimc (l) for the photodegradation aqueous of bromine: P where k : the spring constant(N/m). 3 >> sum(q) * r(2.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 .

r:e commancl.1 (or ratc(s r). l. from -t : 0 to 100m in incremcnts 5 m. lbr E. . : i n i t i a lv e l o c i t y( m / s ) . Enrploythc semi l.454 0 401 0 453 0 485 0 532 a 474 A 4B6 2.15Exactlywhal will be displaycd alier thc lirllowing MATLABcourmands typcd'l arc (a) -'> x :':' X' >-.Use to axis liibels C.The resulting values. are in A. use MATLAB to height.16 The trajecbry of an object can be modeledas R. l. in the top pane and a three-dirncnsional plot ol' line i') (.nt2 0.jF -\ ffi= z\4+ zl : F+ "L x z. ):(tando). r(2.A : thc prccxponential tiewherck: rcaction quency)lactor.rr to gcneratca two-dimcnsional (-r. a valucsol reactionratcs1brtemUsc MATLAB to gcncrate peralurr-s rangin-u lrom 273 to 333 K.d0 4l where _v: height (m).u . . d. minimunrand rnaxirlumol'thc the in be assernblcd an array where thc first dimension(rows) resulting values.225kg/mr. Usc subplot to gc'ngraphol'(a) /<vcrsusI. ) 6 3) 2. lh .R . 2. 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 ..1 and measure their frontal areas.gascontemperaturc stant [8. Use MATLAB to rangingliom l5 to 75o in and r. 8 1 m / s r ) . 1: 2 : 1 0 .11The lbllowing paramctric among the dill-crent cases. bc ctrrnprrtetl theArrhaniu.._! u=4 3$8 <. Thc resultsshould of (b) Deternrine average. : i n i t i a l find the displacement r'. and l. and (b) logr{) ft crater sidc-by-side vcrsus l/7.hclix horizontal l equllirrrrs 2.t linc plot ot' Usesubpl. which are ordered in thc same sequenceas the conesponding valuc-s Table 2. corresponds the distances..versus (right side).r. \'=c CosI reactions dcpendence ol'chcmical can 2. IU.. lt x k . r..COs. and scale legencl di.:30 nr/slbr initial angle-s valuesof the dimensionless drag coefficicrrt compute Employ a rangeof horizontal distances increrncnts 1.E: activation energy(J/mol).stinguish to r'xponentially it cvolvcs as that contriicts thc plot so that thc minirnumheightis zero usingthe azis ol'sinI ...314J/(rnolc . your rcsults. : absolute ( K ) .rnploy a distances eachof the initial angles. 2./ (b)tq . g = gravitationalacceleration ( : 9 . K)].\--r'*)t. :) in thc bottom panc.. f] I 4.17 The lcmperature wilh t'tlutrtittn. Interpret . >> r > > : i L l l r ( c 1 )* zt + .13You contactthe jumpers used to generatethe data in Table2.Aa t'itRI. : Q fbr (a) Il the air densityis p : 1. .PROBTEMS 2. of cD.lncludedescriptivc rr a this matrix to gcncrate singlc plot of thc hcightsversus andtitlcson the plots.a n d y .).r : horizontal distance (m). to and the seconddimension (c) Dcvelop side-by-side ofA versus (lcft sidc)and plot rr a (columns)corresponds the difl'erent initial angles. : initial angle (radians).5". generrlc..rqy functionto crcatc(b).

suchcalculations can be performedeasily.However.9)]: [recalEq. zf x+ . bungee File. 3. Openthe editorwith the menuselection: junrperat a specifictime ing statenrents compute velocityof the fiee-tallingbungee to the (1. M-file. to obtain good accuracy. in EXAMPTE 3. This chapterwill introduceyou to how MATLAB M-files can be usedto obtainsuchsolutions.12.81.3.Returnto the command window and typc >>scriptdemo The result will be displayedas 50. rn. ffi zl'+ zf Lt. M-files comein lwo flavors:scrintfiles and functionfiles. L .Note that the nomenclature that fact that suchfiles are storedwith a .The scriptcan be executed typing the file name in the commandwindow or by by invokingthe nrenuselections the edit window: Debug. m = 68. --At t -{ -l ---_ 5[ st ^l L zJ x <.1. 3.Run.This would be extremelylaboriousand time consumingto implementby hand. [h - lj€- eJ 5 8. M-files provide an alternative way of performing operationsthat greatly expand MATLAB's problem-solvingcapabilities.I S c r i p tF i l e Problem Stotement. They are useful for retaininga seriesof commandsthat you want to executeon more than one occasion. cd = 0. v = sqrt(g * m / cd) * tanh(sqrt-(g * cd / m) * L) Savethe file as scriptdemo .l. Developa script lile to computethe velocity of the lree-falling jumper.New.with the aid of MATLAB.Type in the fbllowSolution.I M-FITES 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. I M-FIEs The most common way to operateMATLAB is by enteringcommandsone at a time in the command window. many small stepsmust be taken.m extension. However.25.An M-file containsa seriesof "M-flle" comesliom the statements can be run all at once.6175 just Thus.the scriptexecutes as if you had typedeachof its linesin the comntand winclow. So our problem now is to figure out how to do this. t ScriptFiles A scriptfile is merely a seriesof MMLAB comnrands that are savedon a file. l 9 = 9.

In contrast scriptfiles.g l Ji ^ .44 PROGRAMMING WITHMATLAB As a final step. Type the following statements the file eclitor: in f rrnction v = % f reef al1vel: Z input: f reef aLLvel. functionsin programming suchas Fortran..it retainsits value back in this is an important disworkspace. : r . . ffi --l = -l ^l zt\ zl 4+ -jy !t'i 5t ?JLJ' zl'+ "L Y a.r-/uer is assigned ourvar.-elocitiz conputes c:f an cd) rviLh the objecL sjecond-order frc-e-faL1 wiLh drag velocity drag r. where outvar'= the name of the output variable.. As in Example3. into valuesthat are passed list argt)st = the function'sargument (i. we will seein the following section..e. Files 3. [h -r af xh VT jjg-:l 5t 3$8 .nLs s L.m. window asillustrated the fbllowingexarnple.l % v=freefallrrel(L. Note thateven in in f uncname thecommand your computer'soperatingsystem may not be.l.and can be invokedby typing t1elp f uncnane in the cornmand function(lhese thitt to that the srare.mL-lLs= MATLAB statements compute r. two diff'erentvariables.l t emell L.furcr. languages VisualBasicor C. called the Beyondits role in describing function.(L. compufe velocityof the free-falling bungee the jumper.m.cd) second*order l-ir_. by the Hl line.t) Z he)pconme. 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 . bungee '. you shoulcl wclrds related the file on this line. m.s auLva r.determinethe value of s by typing >>g 9.but now we will usea functiorr for the task.1. generally as The syntaxfor the functionfile can be represe'nted funcLion outr'.comma-delirnited = regarding the ?reTpcornnerts tcxt thatprovides userwith infbrmation tlre the function. is the line that is searched the lookf or command(recallSec.v'al ue.2. EXAMPLE 3. 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.2 r u n c l t o nr i l e ProblemStotement. window).. though MATLAB is case-sensitivc. to includekey descriptive The functioncan thenbe run by typing The M-flle shouldbe savedas furrcnarne.they are Hencethey are analogous user-deflned to and can acceptinput arguments returnoutputs.rme = the function's name.2 Function to FunctionJiles M-files thatstartwith thewclrdf unction.the first line ol'the he/pcorrurrcnts. BlOO So you can seethat even though q was definedwithin the script. l.Thus.rr= funclanlc ( .6). file Solution.

I M-FILES % r = rime (s) (ks) m=mass % 2 cd = second-order drag coef ficrent % output: (m/s ) % v = dovrnward velocity 9 = 9.3. at the end of the previous example./ 45 (kSi m) m) * t). if we had typed >>g zr+ ztf-ai3'" -\ ffi= z$4+ = "L vI 3l F+ <.m.6i75 One advantage of a firnction M-file is that it can be invoked repeatedly for diff'erent argument values.p freefallveL which results in the comments being displayed freef..: t = time (s) m = nass (kS) (kglm) cd = second-order drag coefficient ourpu[: (m/s) v = downward velocrty dr:ag If at a later date.'=freefallvel(t.ccl) compuLes the free fall veloclty of an object with second-order i nput. 0. m.81.lh Y jtE- ?l 5t I . but remembered that it involved bungeejumping.25) The result will be displayed as 50. v = sert(g of ? acceleration * m / cd)*tanh(sqrt(g gravity * cd. Suppose that you wanted to compute the velocity of a 100-kg jumper after 8 s: >> freefallvel (8.2t) 53. return to the command window \12.1 . Save the file as f reef al lvel and type in >> freefallvel .r1lve1: bungee velocity with second-order drag . 68. you could enter >> lookfor-bungee and the following information would be displayed f r e e f a l . you tbrgot the narne of thjs functiorr. To invoke the function... m : % f reef al1: bunqee velocity with second-order clr:agt Note that.187B To invoke the help comments type >. hel. 1. 100. 0.

. f unction = veL (t. cd).2 (without conrments) could have been split into two functionsand saved as a single M-file': v rr = f reef al Lsubf unc (L.In suchcases. v = sqrt m .^2/ (n-1) ) ) . in they rnay also be contained a singleM-file.?l 5t 3$8 Z] X+? "Y ZN <.46 WITHMATLAB PROGRAMMING would have beendisplayed the following message ??? Undefined function or variable 'S' . r t l al. it would not have a value in the As command workspace. For example.5000 S= 2 . Here is an example of how it can be applied: '> !' lB 5 1D 12' 6 1.m.In contrast. this is an important distinction betweenfunctions and scripts. Functions can call otherfirnctions.rptional single-function are in M-lilcs. mean = sum(x) /n. 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 comma-delimited and enclosedin brackets. noted previously at the end of Example 3.1. Hence. m.1. th - .J Lt€ . ccl) end function n 9 v (q = vel * (t. s s ffi qA+. 3.For example. sqr t (sum( (x-mean) .the following function.81 within the M-file.873'1 Because scriptM-files have limited utility.z cd) *tanh(sqrt end I Note that althoughend statements t. a r .the M-file in Example3. So even thoughs had a value of 9.we will ofien refer to function M-files as simply M-flles.5 4l. stdev . 3. functionM-flles will be our prirnaryprogramming tool fbr the remainderof this book.staf s.2 m= 7.3 Subfunctions Althoughsuchfunctionscan exist as separate M-files. n r. Function M-files can retum more than one l'esult. zt Ll 2 jt -l -l el' st zl'+ LA z. stdev] = stats (x) n = length(x). cd) (g * cd / m) * t).the variablesin a script retain their existenceafter the script is executed. m.The variableswithin a function are said to be local and are erasedafter the function is executed. computes meanand the standard the deviirtion a vector: of function Imean.

2 INPUT.68. l ' r r l F u n c t i o n .1. 68. [h . first functionis the called the main or primary function. 0.6175 However. value fiom the keyboard.25) 50. information passed is into the functionvia the argument and is outlist put via the function'sname.OUTPUT As in Section 3. 1.Its syntaxis a disp(rza-Zue) where yalue = the valueyort would like to display.2 INPUT.OUTPUT 47 This M-file would be savedas f reefallsubfunc. zt+ '"1 -r ffiE\ z\4+ zl x+ zf\ -x : 4 -l- --T . the otherfunctions(in this case. an . T h e r r . useris prompted the with the message Mass (kg): waits for keyboard input. 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. This functionprovides handyway to displaya value.r = input \'promptstring') The lunctiondisplaysIhepronptstring.or a string message enclosed hyphens.ve1) are Al1 referred to as subfunctions. ) The .Function.!.ti.Two otherfunctions provideways to enterand displayinformationdirectlyusingthe command window.ef >t I <.s. In suchcases. A subtunctionis only accessible the main firnction and other subfunctionswithin to the M-file in which it resides.To do this.2: .25) command/ funct ion 've1 ' ?? ? Undefined 3. m = input ('Mass (kS)r ') When this line is executed. name = input ( 'Enter your natne: ' .s . .if we atternpt run the subfunction to vel. an errormessage occurs: >> vel(I2.m. For example.and then returnsthe If the userenters value.3..lts syntaxis . It is the only function that is accessible the comto mand window and otherfunctionsand scripts.'> freefa]lsubfunc (12.For example.t€ .1. The inpur funclion can also returnuserinput as a string. we run f reef al lsubf unc from the commandwindow If the result is identicalto Example3.Its applicationis illustratedin the following in example.it would thenbe assigned the variable a to n. can be a numericconstant variIf or able. is appendedto the function's argunlentlist.

48 PROGRAMMI WITHMATLAB NG EXAMPLE 3. 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.To irrvoke the function. A simple representation its syntax ts of fprinLf ('formaL'. in Solution. r r r r l 1 F u n c t i o n . % acceleration of grraviLy 9 . (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 . cd = input ('Drag coefficient ').9.compute velocityof the free-falling the bungee jumper.the statement along with the resulting output would be >> fprinLf ('T'he velocity is %8.o r : d e r d r a g . > < . As in Example3. velocity) The velocit. A sirrrpleexample would be to display a value along with a message.For instance.l Drag coeffrcierrt Time (s): 12.rl I i Mcrss (kg): 6g.F i l eF u n c t i o n M ProblemStqtement. the format codes allow you to specify whether numeric values are ffi zl' + zl Llz .2. m = input('Mass (kqlm) : ') . it first encounters a % and recognizes that the following text is a format code.1 .t -l "L x -.-r J ffii: :.3 A n I n t e r o c t i v e . .plays the labels until it detectsone of the symbols: %or \.suppose that the variable velocity has a value of -50. [h -.active computatjon of the free fal1 rrelocity of an object \ r itt h s e c o n d . In our example.: t is a string specifying how you want the value of the variable x to be displayed. .6175 mr's This example should make it clear how the format string works. return to the command window and type >> f reef . m. MATLAB starts at the left end of the string and di:. The operation of this function is best illustrated by examples. but now usethe inprut and disp functionsfor input/output. As in Table 3.eracti're bungee velocity inter. .2 5 T h e t 1 . . 6 1 ' 15 (kg/m) : 0 .\'I Y+ N vI j-l € .4f n/s\n'.7 i s 50. display the value using eight To digits with four digits to the right of the decimal point along with a message. ( l < g ): ' ) .sJ 5t I a./s): 5 0 . Type the fbllowing statements the file editor: runct ion free[-.6175.lr (nr. Velocit.. ) where form. B1. L: % f reef a1li: int.

3f %8.sJ 5t I z[+ '-A Z} Fh .6 r7. x. >> fprintf 100 ('%5d %10. 2A. Thesevectors thencombined are into a matrix.I.B BB.1 e[st --:> .400 z(\ Ll 2 -zl -- ffi qAb :I . 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. If we had omittedthe code \n in the previousexample. As in Table 3.400 12 .283 3.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 . the MATLAB continuesdisplayingthe character information(in our casethe units: m/ s) until it detectsthe symbol \. z) .which is thendisas Dlaved a tablewith headines: fprrnt fdemo function '2 x = lI I 4 5l.3f\n'.700 120.6AA 17.After displaying valueof veLociry.5e\n'.the comrnand prompt would appearatthe end of the labelm/s ratherthan the next line as would typicallybe on desired.OUTPUT TABTE 3. This tells MATLAB that the following text is a control code.2 INPUT.800 88.100.For example. lh - rf €.7 L2a.the control codesprovidea meansto perfbrm actionssuchas skippingto the next line.pi) 6. a I x <.Here is an M-file that enterstwo setsof valuesas vectors. scientific or format.2*pi.^. The fprintf functioncan alsobe usedto displayseveral valuesper line with diff'erent formats. It can also be usedto display vectorsand matrices./--5 ---"'_.3. in decimal.4l.yl j - fprintf fprintf (' y\n' ) x ('%5d %10. The resultof runningthis M-llle is >> f pr:int X 1 2 3 4 5 f de rno Y 20.14159e+000 .4 12.

all the workspace are variablesaresaved.. 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. As was the case with save. called a MAT-fi\e.9) to generatevelocitiesfor a set of drag coefficients: > > 9 = 9 . >> clear p F At this point. B 1 ..213]'. all the variablesare retrieved. m = B Ot . If the variables omitted. the velocitiesare availableas can be verified by typing >> who Your cdv variables are: 3 TI Although MAT-files are q.2.?l 5t & Z] X+ . The l oad commandcan subsequently usedto retrievethe file: be load fr lename varl var2 . which is expresslydesignedtbr implementationwithin MATLAB.rite useful when working exclusivelywithin the MATLAB environment. The save commandcan be usedto generate MAT-file holding eitherthe entire worka spaceor a few selected variables. varn This commandcreates MAT-file named fi-tename.= 5 . zl -h .you can recoverthem by entering >> load veldrag T m n a Now./cd) .28 ..a somewhatdifferent approachis requiredwhen interfacingMATLAB with otherprograms. ( 1.mar that holds the variablesyarl a through varn. Such files are createdand accessed with the save and load commands.25 .50 PROGRAMMING WITHMATLAB 3. varrl which retrieves variables the rzar-1 throughvarn from f ilenante.J <. >> v=sqrt (g"m . The simplestapproachinvolves a special type of binary file.. For example._r _l ffi= qA+ -i 5[ sl "L x z. [h -r j. simple representation its syntaxis A of save fifename varT var2 .l'€.*tanh(sqrt(g*cdlm) *t) . simpleapproach to createtext files written in the widely a is accessible ASCII format.245 .supposethat you use Eq.3 ! CI However. removeall variablesfrom the workspace with the clear command.In suchcases. zl Llt --a . >> cd=[. if the variablesare omitted. I Creoting ond AccessingFiles MATLAB has the capability to both read and write data files.261 .mat. if you tried to display the velocitiesyou would ger rhe result: >> v u al ??? Undefined function or variable 'v' 3.

In contrastto MAl-files where you might want to savethe entire workspace.irpt-rt.0000000e+000 Alternatively. The looping of flow to allow statements be repeated. the program stateThe simplestof all M-files perform instructionssequentially.3 STRUCTURED 5l in ASCII files can be generated MATLAB by appending ascii to the save command.Its general syntaxis if condition statemenf-s ffi z\4 . would typically savea singlerectangular >> A=15 I >> save 9 2. 0 0 0 00 0 O e + 0 0 0 3. In eithercase. For example.the save commandstoresthe valuesin A in 8-digit ASCII form.For example. 0 0 0O O 0 O e + O 0 O .9f 5l . Alternatively. lmport Doto.0000000e+000 9.txt In this case.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 . Because strict sequence highly limiting.txt) The foregoing material covers but a small portion of MATLAB's file managementcapabilities. ' i>: = l9 simpmaLr ix 5192 zF. txt is not a MAT-file. you could use the load command as a function and assign its values to a variable as in >> A = loeid(simpmatrix. >> load sirnpmatrix.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. 0 0 0 0O0 O e + 0 0 0 2 .. a handy import wizard can be invoked with the menLrselections: the import wizards convenienceby File. t x t . in the to storethe numbers doubleprecision. 0 00 0 0 0 0 e + 0 0 9 .3 6 3 9l.3 STRUCTUREDPROGRAMMING That is.you matrix of values. to 3. branching t-lowbased a decision. I n a d d i t i o n . s of Loolts (or Repetition). As an exercise.3.0000000e+000 3.f\€ -l- 3l x+ & . you openthis file with a text editor. The on Decision (or Selection). If you want -ascii -doubLe. by or example. ascii simpmatrix. .MATLAB creates doubleprecision namedafter the fi l ename.1 Decisions if The i r Siructure. all computerlanguages a paths.' ' ."I LJ ' zl'+ -J 9t" 5\ "L 4 <. Thesecan be classified programs takenonsequential to as mentsallowing .txt a array Because simpmaLrix.you can read the valuesback into MATLAB with the load command..0000000e+000 6.just append For file can be irccessed other proglams such as spreadsheets word processors. 3. mentsare executedline by line startingat the top of the function and moving down to the is include stateend. This structureallows you to executea set of statements a logical conditionis true.PROGRAMMING 3.you will see if 7 5 . lh x --4 .

The fbllowing M-file 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.it displays the text message. er ror (' 'zcrc:value f = I/x. that hereis a simpleM-tile to evaluate whethera gradeis passing: Iuncrion % grader % gldder(g oder (grade ) : determines whether = numericalmessage qrade value is of passing grade (0-100 ) % input: r" grade % output: % if drsplayed grade >= 50 di sp ( 'pass rr-rg grrade ' ) end The following illustratesthe result >> glader(95. er ror (msgr) When this firnction is encountered. the multiline if structure is usually preferable because it is easier to read. ??? ErroL usit'tg ==> err-or:test zero value encountered ffi= zr+ '"1 "L x <.. if grade > 60. djsp('passing gr.rsEr and causesthe M-tile to terminate and return to the conrmand window.For example. This involves using the cr:ror: function which has the syntax. lh -tl -l- zl x+ : 4 Lrt9 .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.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 . end This structure is called a single-lirrc rf For cases where rnore than one statel. encountered' ) . cnd If a nonzero argument is used. 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 .nent irnpleis mented.52 PROGRAMMING WITHMATLAB wherecondr tron is a logicalexpression is eithertrueor false. it is ofien convenient to implenrent the i f structure as a single line.6) passingr grade For cases where only one statementis executed. E r r o r F u n c t i o n .rdc'). An example of its r"rse would be where we might want to terminate an M-file to avoid a division by zero.9n 5 I .

can be constants.'. . orderof priorityof ihe operotors shownol the loo of thetoble. These there is a priority order lbr evalualing arithmeticoperations. :'io"1 i If either or both express.the resultis false. Just as fbr lor outcomes eachof theseoperators.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.9 . the expressjon is false. Highesr ryx x&y Lowesl xly I I F F T T T F "L x 4 <Ah 1-1.on!r evaluateto true.r d pxp/ | . the resultis true. the result is true. wherethe yaf ue-s orexpressions the relaLion is one of and Table 3. Usedto perlbnn a logicalconjunction two expressions.3 STRUCTURED TABTE3.3 for the outcomes logicoloperotors A truthtoblesummorizing possible is The in employed MATLAB.Used to performa logicaldisjunction two expressions.Usedto perfbrmlogicalnegation an expression. relaL i. on +.3 summarizes possible logical operations.€ 5t 8. (Or). on FXp/ ps.on vaf ue.2.t .PROGRAMMING 3.sr.1p7 essr. if If the expre..lNot). the resultis true. will ernphasize fbllowing: ..-. If eitheror both expre-sto to srons evaluates false. The simplest form of the condr Lron is a singlerelational expresfwo valuesas in sion that compares vaf ue.S.Converscly. If both e)<pre. on .rOn .exp res-s r o.the resultis false. the relationaloperatorslisted in MATLAB alsoallowstesting morethanone logicalcondition cmployinglogical of by We the operators.ion is true. TABTE3.'on I PxLrt r-. all lhble 3. . * (And). variables.:= 3/3 r: >= 0 Logicol Conditions.ssrorsevaluate true.

r ' t : : . hasthe Seneral condition. b .1.1 .a special form of decision two options lbr a par-ticular It the syntax structure.'(y > . the left-to-right rule is appliedand (p the first expression r r) is evaluated: F&FIT priority The r againhashighest FIT in as is Finally. if ffi= qA+. 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.d ) To make it easierto evaluate. 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 . > . a a The j r .. Since there are two. statements.. evaluateall the relationalexpressions -2 > A & 2 -= 2 & I > I T F & & tr 1 -('b' l. example. . { . .2 . e l s e structureis anotherdecision.For such cases. MATLAB evaluatesthem from left to right. Finally. z(| Ll 2 7[e1. .d') I The first thing thatMMLAB doesis to evaluate mathematical expressions. else Stdae'nlenLS. end EXAMPLE 3.hashighest are (-r) is evaluated priority. a * b > 0 & b == 2 & x > 7 | .54 PROGRAMMING WITHMATLAB are from highest to lowest: -. .The entireprocess depicted Fig.andy='b'. .Its general syntaxis if r:ondjtion stat€ment:. t i . if.the lastexpression first to give F&T&F T The a operatoris evaluatednext. this ln any thereis only one: -7 x 2. statements. . Let's investigate how the computeremploysthe prioritiesto evaluatea logical expres=t. In choosingbetweenoperatorsof equal priority.' 'd') F in At this point.evaluatewhetherthefollowingistrueorfalse: s i o nI f a = .Sincethe .parenthesescan be used to override the priority order. x ..-.This type of structureoften occurswhen we have more than problem setting.substitute valuesfor the variables: the * 2 > 0 & 2 =_ 2 & 1 > 7 | _(.s\ -l ^l "L x 4 <. [h - ll'€.eJ 5t 3$8 zl'+ LA _\ zl /+? . as with arithmetic operators.the I is evaluated true. -2 > A & 2 == 2 t''I > 1 -('b' > 'd') Next. S t r u c t u r e .b. a and I . 3. r l t S t r u c t u r e . efseif hasbeendeveloped.4 ono T h e i r . elseif conditron. the logical operators evaluated priority order.

.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.. I )..I A slep'by'srep evollotion o{ o conrplexdecision.f\t .l5. a[h I . -- Z d...-.ef 3$ 8. statementsr else statements. >> sign (25.-.PROGRAMMING 3... 0..! --I s\ o ffi qA+..I...776) -1 >> sign(0) 0 Develop an M-file to perform the same function.6) 1 >> sign (-4. end EXAMPLE 3.3 STRUCTURED 55 a*b >u& 1^.the built-in MATLAB sign function returnsthe sign of its argument(. Here's a MATLAB session that illustrateshow it works."1 { 9l < --zl-.4 i t Structures Problem Stotement. . zt+ . 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 \. elseif condition-. For a scalar. zt\ aY Z] x+ 4 -l- _-T .

elseif case: this final funct-ion sgn = mysign(x) 1 if returns % mysign(x) x x is is is gtreater less eq-al to than than zero. end This function can be run as >> mysign(-0. z z lf x > 0 sgn = 1.6) 1 Although the function handles positive numbers correctly.An rf . -1 is erroneously structurecan be used to incorporate returned if azero argument is used. % mysign(x) This function can be run as >> mysign(25. First.aro. il{#ff: er' "L x 4 zl xh %8. The function now handlesall possiblecases. . _ - _t. . To partially remedy this shortcoming. sqn = nYSiQIn(x) returns 1 if x is gTreater than zero. For example. . >> mysign(0) 0 ffi!3r qA+. O if x 7. . an i f structurecan be usedto return 1 if the argumentis positive: function if end x sgn > = 0 1. -^. if it is run with a negative or zero argument. x < O elseif sgn = -1.56 WITHMATLAB PROGRAMMING Solution. end zero.e1se structure can be used to display -1 ifthe condition is false: sgtn = mysign (x) f unction 1 if x is greater than zero. returns % mysign(x) I if x i-s less than or equal Z if x > 0 sgn = 1. . else sgn = O. ^t ^-. an if. nothing is displayed.176) -1 Although the positive and negative cases are now handled properly.9 r ! to zero.

. d{oult vulueis a numberthat is automatically A assigned the eventthat the in userdoesnot passit to a function.r rr:i.lluht end "Good" would be displayed.sf r. case staIe. y a .. 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 .xprcss r z a Ju e . . l - z. MATLAB hasa functioncallednarsin that provides numberof input arguments the suppliedto a function by a user.9f 5l la^.c h S t r u c t u r e .PROGRAMMING 3. Dependingon its value. qracle: 'A' ( 'Excellent' 'B' ) clisp('Good') 'C' ( 'Mc.-diocre' ) D disp disp('Whoops') 'F' case drsp ( 'Wou1d like ) fries wj th your order?' ) otherw]se disp(. gracle svrifch case drsp case case L r15ri = 'R'.menrs. addition.it would be nice to have the program supply a value if they omitted it from the argumentlist. message the Vorioble Argument List.1u e .cd) Although a userwould obviously needto specifythe time and mass.f. When this codewas executed. hereis functionthatdisplays message a depending the valueof the on gra de.-T ' LJ -b b ZN -x 3!h . end As an example.3 STRUCTURED 57 T h e s r v i t . e 1 s e i f is However.. oInerwrse s I a a emel] f .Tlris featurecan come in handy for incorporating defaultvaluesinto your functions.the branchingis basedon the structure. .m.they might not have a good idea of an appropriate dr.€ .sol lr. which had threearguments: v = freefallvel (t.Therefbre. MATLAB allowsa variable numberof arguments be passed to to a function.. developed function freewe a f a 1 1ve i.It can be usedin conjunctionwith decisionstructures like ffi . As an example.different blocks of code are implemented.It has the generalsyntax sr.an optionalblock is implemented the expression In if takeson noneof the prescribed values. value of a single test expression. I on "-ctaaemencs.ratherthan testingindividual conditions.J . stringvariable.ricch case I . .recall that earlierin this chapter.9 coefficient.

ng mass () ltrofallvol and mass (12 ) f reefallv-l ??'. cd) drag velocity drag. m. it must include a string argumentspecifyingthe funcin tion and it returnsthe numberof arguments the function. or Notice how we have used a switch structureto either display error messages set the passed the user.68. Err(.Here is a command by on default. % acceleration * m.6B. For example.depending the numberof arguments showingthe results: window session >'> f reef allvc.I. cd) computes free-fa11 the of an object with v=freefallvel second-order % input: Z L = time (s) %m=mass(ks) (default = 0.75 >> freefallvel ( 12.lvel Error enter *sing time EXAMPLE 3./ of gravity * cd 9. A .r Must >> ??? Must enter f reef al.sqrt(g cd)*Lanh(sqrt(g / m) * t). - - . l (12. freefallvef with second-order bungee vefocity (t. [h. >> nargin ('freefallvel' ) ffir zt'--h" "L x zJ 7) F+ il'€ .l) 48 .81.58 PROGRAMMING WITHMATLAB into to the if or switch constructs incorporatedefault valuesas well as error messages your functions.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 en1-er nass ' ) 2 = 0.81 41 >> freefallvel us. = . how this can be done for fr^eefallvel: The following code illustrates function ? % v = freefalLvelt : (t.m.21 .61.:l 5 I . In the commandwindow.5 when it is invoked in the command Note that narsin behaves little diff'erently a window.25) 50.fl1fu= -\ <.

doesit haveto be positive.inj sh is lessthanthe start and the loop terminates when the index is lessthan the f inish. lh . a r ea l l a c c e p t a b l e . 1.the def-ault step is 1.the f . l.2 the 0!: I ll- I I v ) -') 2 Note that MATLAB has a builrin function f acLoriat that oerformsthis comnutation zl' . The size of the srep can be changedfrom the defaultof 1 to any other numeric value. 2 .5 . A f or loop repeats statements specificnumberof times. 5.the loop will "countdown"in reverse.Its a g e n e r as y n t a x s l i f o r i n d e x . . stepsizesof A . for end i = 1:5 djsp(i) When this executes. When the it reaches end line that marks the end of the loop.fin jsh. start."1 {t -& ffi=b z$4 I 71et st ^ z I x+ / ZI\ x _-T<. 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. If the index is lessthanor equalto the frnish. 1.1-uteme'. . MMLAB would displaythe classic 10 B. the index variableis increased the by the srep and the programloopsbackup to the for statement. If a negativestep is used. suchcases. Thus. 4.I . Note that if an incrementof 1 is desired(asis ofien the case). the programexecutes sLatemenrs.3. o r .the s t ep can be dropped. It doesnot haveto be an integer. 5 E . The programthencompares rndex with a desired the final value.rs The f or loop operatesas follows..2. In other 1 words.2 Loops As the name implies. U s i n g o 1 .3 STRUCTURED PROGRAMMING 59 3. The ror . nor For example. . 2 . For example.3. For the loop's logic is reversed. 4. the loop terminates the the index becomes as programskipsdown to the line immediately following the end statement.3. encl Structure. The rndex is a variable that is set at an initial value. process The continues until greaterthan the f ini sh value. When this executes. MATLAB would displayin succession.f\€ . Developan M-file to compute factorial.There are two types of loops.9l 4 -t- >l & . For example.At this point. 5. for i = l0: disp rj r end 1:1 "countdown"sequence: . loops perform operations repetitively. dependingon how the repetitionsare terminated. 9 . . 6. E X A M P L3 .f i n i s h ". for loop endsafter a specifiednumber A of repetitions A whi I e loop endson the basisof a logical condition.s t a r t : s t e D : .

and we the f or loop would not execute. for Vectorizotion. which can be run as >> f actor: (5 ) 12a x This loop will execute5 tirnes(fiom I to 5). For example. a the most efficient meansto repeatstatements specific MATLAB. will hold a value I of5l (rneaning5factorialol x 2 x 3 x 4 x 5 : 120).42:5C = i + l.That said. It shouldbe noted that for morc complex code.This can becometime consunringwhen you perform actions suchas addingnew valuesone at a time within a loop. all Lhe integiers from I to n' I I for x end fout end i = l:n = x * i.{ Z] X+ . For exarnple.vectorizotion numberof times. = cos(r). = cos(t). For this case.= 0:0. The for loop is easy to implement and understand.1 "L x <.the filllowing f or structure: i for r end n. it is not necessarily directly on arrays. would get the desiredresult. = x. vectorization recommended. .r'.Because MATLAB's ability to operate of providesa much rnoreefficient option. At the end of the process.0! : I However. Ih -r VT jl'€- sJ 5r I zl'+ .60 WITHMATLAB PROGRAMMING 3l:lx2x3:6 4l:lx2x3x4:24 5!:1x2x3x4x5:120 Solution.it may not be obvious how to vectorizethe code. t. y(i) can be represented vectorizedform as in t y = 0:0.02:50. is Preollocotion of Memory. MATLAB automaticallyincreases size of arays every the tirne you add a new element. hereis somecodethat T Ir sl SI fiT=fiar qA+ zf Ll L A e[st -. if Notice what happens n : 0. A simple function to implement this calculation can be developed as fout = factor (n) function (n) : % factor Computes the producc of % -t.wherever possible.

1. e^ndirian st. l I l:lerg. tt er. In addition. The follow_ ing code preallocates proper amount the oi -". .hLt L:.Structure.: ' 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 .0:.01:5.o..tion.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.MATLAB rnustresizey every time a new value is determined."" be mimicked -WT-Tbl zl zt -+ -tA-\ aA+. Although suchstruduresarecurrenrlynor availablein MATLAB.tte. The r.l.3 STRUCTURED PROGRAMMING 6t setsvalue of elementsofy dependingon whether or not valuesof t are greaterthan one: L U:. the resultis is E 5 2 -1 T h e r r ' i l r : ' ' ' 1 r r r . y .hrL/ F^LOr t ( i ) >l = I/t-(r). iun.ii. encj end Thus.rnert.eJ 5t %8 af x+? . Y(i) else r f y(j) end end For this case.y by using a vectorizecl statementto assignonesto y prior to enteringthe loop.one-q(si:e(t)).. preailocation herps reduce memory frag_ mentation.UI:). for i = l:lengLh(t) rf r (i )>l y(i) . which also enhancesefficiency. general syntax is vrh. a s[sl Ll 2 -{ L 4 x <.Iir(i). lh lj€ . Its while disp encl x > 0 (x) When this cocle run. 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.3.s end The statemencs betweenthe whiLe anclthe end are repeatedas long as the condr jon t is t r u e . the anay is onry sized once.

That is.An exampleis while If -.N17 =L x j]\€ ..the break can be placedin the middle of the loop (i.can be written as r. 1+%-q'tr 4 -zt+ zl . with statements is loop.. The 1.. work togetherto measure elapsedtime. couldalsoplacethe i f . < (. The syntaxof this version. [h - .Thus. break. all threestructures really the same. in which casethe procedure halt as will fbr n seconds. we could placethe break at the very beginningto createa prel(.t ic and toc.e.Note that as shown.The commandpause causes procedure stopand wait until any key is hit. end end so Notice how 5 is subtracted from x on eachiteration.The beep commandcauses computerto emit the a beep sound. a to A nice exampleinvolvescreatinga sequence plots that a userrnight wanf to leisurely of peruse befbremoving on to the next. Commond.Two other functions..'rXtutements wherebreak. 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..-. Sucha structure calledarnicltest If the problem requiredit. a singleline i f is usedto exit the loop of if the conditionteststrue. ffiat - .. (1) x w < - 0.terminates execution the loop. a at Alternatively. middle.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 ). l. This featurecan be demonstrated irnplementingit in conjunction with by severalother usefulMATLAB functions.'.. depending on wherewe put the exit (beginning.or posttest.?l 5t I <.called a while. break structure. Thereare often timeswhen you might want a programto temporarily halt..62 PROGRAMMING WITHMATTAB by a specialversionof the while loop.This represents rnechanism that a the loop eventually terminates. break.or end)dictates who developed It nrid. . . whetherwe havea pre-. whrle if end x 3 (1) q.vhile if (1) conditron. Otherwise it would becorne so-calledinfinite loop that would neverstop.hile structure. Every decision loop must havesucha mechanism. end stafements . break statement the very end and crewe ate a posttest lo)P.). break.r. in fact. beforeand atier it).st loop. end EXAMPLE 3 are It shouldbe clear that.

sl 5t I <.006306 seconds. 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.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 .This "big-picture" versioncan be programmed tionalcases as f .4 NESTING INDENTATION AND 63 The tic command saves the cuffent time that toc later employs to display the elapsed time. b.ond otdet coefficient ffi ?JLI' zl+ z$4 J "t J :I 5\ "L x 4 --4 1. tic and toc can be employed identify the partsof an algoto rithm that consumethe most execution time. 6 E N e s t i n gS t r u c t u r e s ProblemStqtement. ktp-down r/e. By the way.g. b . Although the foregoing examplesmight seema tad frivolous. c ) % quadroots: roots of (a.we first recognize that depending whetherthe parameter on (e. c) : quadratic real and o'quod'ot equation complex jaqua' llt Lth nit ]e.1':. you can return to the command prompt by typing Ctrl*C or Ctrl+Break. v a providesa nice approach designingan algorithm to comSolution.4aa LA Developa functionto implement this fbrmulagivenvaluesof the coeff'cients.u n c t i ..f i l e s . The the E X A M P L3 .3. the computer will beep. % quadroots rool-s jon z % input: 9o o lo.Nc.To start.For instance. MATLAB will go into an infinite loop.singlerootsor trivial values) convena is zero.4 NESTING AND INDENTATION We needto understand that structures can be "nested"within eachother.rlgn for pute the roots. o nq u a d r o o t s ( a . The rootsof a quadratic eqLration /(x):uxl+lrx+r' can be determined with the quadratic formula -b +. Five seconds later it will beep again and display the following message: ELapsed time is 5. Further.. following exampleillustrtrtes concept. 3. the commandscan be quite useful.sllrrc ref-ers to placingstructures within other structures. if you ever have the urge to use the command pause ( inf ).we will eitherhave"special"cases or usingthe quadratic fbrmula. [h Z] Xh .This involvesdeveloping the generalstructure without detailsand then refining the algorithm.t8-. In such cases.

O f or:mul a 4 * a * c.I Lrtg - 9r\ 5 -J 3l x+ I .sqrt.tl cases e '^rl-L1a end Next. Try again' ) error('Trivial And we can developrefinedcodeto handlethe quadratic formulacases: d %qu. * a) a) / (2 * a) end We can thenmerelysubstitute these blocksback into the simple"big-picture"framework to give the final result: functron ? % % input: % a = Z b % c % ouLput: % rI Z i1 = Z second-order first order zero-aYder real partquadrooLs (a. c) quadratic real of equafion and compl-ex roots quadratic equation coefficient coefficient coefficient of first part. c) : b.64 PROGRAMMING WITHMATLAB Z b = fir-st-order 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.of roo[ first root imaginary ffi= zl+ .rbs(d)) r) r-l / / 12 * ().(d)) t'2 el se %complex roots = rl b / \2 * a) i1 = serL(. [h :. else?tr i vial end sol ut ior-r solutron. rooLs %real r1 = ( b + sqrt(d) ) ( b .rdrat ic .b ^ 2 if cl >. we developrefined codeto handlethe "special" cases: %special cases if b -'= 0 e. quadroots: quadroots roots of (a .b."1 {z 7t 5fi st l-l "L x 4 <.srngle root 11 | V.

underlying logical structurecrear.4 NESTING AND INDENTATION % Z i r aF a 65 12 = r. 1 ) -0.the structures are.notice how indentationhelps to make the .5.5000 -0.how "modular.1.8660 ml zI zl Llz zl' + .1) -0.eal.f a - end end o.1 5t '$8 Z] X+ . [h -. 5. 4 *a Ediscriminant rf d >= 0 ?rea1 roois ri.2087 4.r highrightedby the shading.1) 17 = -0. t-r _\ 4 -{ + 5[st "L x a. Here rs a command window session illustratinghow the function f"rfo.3. ^lu gl*:r. - v1 .7913 >> quaCroots (0. VT Lf €.part of second root 12 = imag.2000 = 0.5000 i1 12_0..8660 >> quadroots (l.Also nodc. ! .inary part of second root == u Sspecial cases ifb-=0 ?single roor 1L--c/b else ?trivial solution ( ' ?rivial solut ion.rs >> quadroots(1. ) Sguadratic formula d=b * c. (abs (d) ) / (2 * a) r) l . e-Lse ?ry agaln . .= (-b + sqrt(d)) / (2 * a) 12= (-b-sqrr(d)) / (2*a) else Scomplex roots rl-=-b/(2*al i1 = gqy.o.

0. we will first introduceanonymous functions.' f2 (3) = 36 Note that if subsequently enter new valuesfor a and b.functionsallow you to createa simple function without creating an M-file. \ Asidefiom the variables its argument in list.1 Anonymous Functions Anonyntous .5. the anonymousfunction we doesnot chanse: f 2 (3 ) ans ffiar qAb zf Ll t zl -& ''l -\ --c. thesefunctions In are given a special name:function.FIIES TO Much of the remainderof the book involves developingfunctionsto numericallyevaluate other functions.argTist = a comma separated of input arguments be passed the function. Before describinghow they work. betteralternative to designa genericfunction and passthe particular a is equationwe wish to analyzeas an argument.5 PASSING FUNCTIONS M.L l l u s u >> >> >> ans a >> b 4.functictns.r where f. example.eJ 5t 3$8 3l x+ . Once thesefunctionsare defined in the commandwindow. 3.=. = 2. f2=@ (x) t t a*x^b.4) ans. 0 ) ??? Error using ==> quadroots TriviaL solution.which provide a handy meansto define simple userdefinedfunctionswithout developinga full-blown M-file. an anonymous functioncan includevariables that exist in the workspacewhere it is created.and express l on = list to to any singlevalid MATLAB expression. Try again 3. we could createan a n o n y l n o ufs n c t i o n / t l ) : . They can be definedwithin the comrnand window with the following syntax: ihandLe = @(argJist) expressro. they can be usedjust as other functions: >> f1(3. For >> fl-@(x'Y) x^2 + Y^2.66 j PROGRAMMING WITHMATLAB >> quadroots (0.lh T -l 5[st Y j-l'€.Although a customizedfunction could be developedfor every new equation we analyzed.harrdle . .the functionhandleyou can use to invoke the function. the parlanceof MATLAB.For example.

25. to 3. .somereaders might be usingearlierversions. and so we thoughtit would be helpfulto mentionthem.If we of want the variablesto take on new values.For this case.B1*6il.r\€ -l- Z] X+ I .will havemanyoccasions useMATLAB's this to builrin functionfunctions.5 PASSING TO 67 Thus.'y').FUNCTIONS M. role as anonymous Forexample. 'x'.For example.2 FunctionFunctions Function functions are functionsthat operateon other functionswhich are passed it as to input arguments.wc. the function handleholds a snapshot the function at the time it was created.5. in the following example./681) *t) .J <.havthe ing changed to 3. in Notethatin theremainderof book. with the result >> ans f2 (3) 27 It shouldbe notedthat prior to MATLAB 7. vrl 'Jrrj ( sqrt (9.FILES 3. ffi= . sirnpleexampleis the built-in function fp1oL.2. anonymous the functiondeveloped above.f 1. 3. Here is an exampleof how fplot can be usedto plot the velocityof the free-falling jumper. a >> f2=@x a*x^b.MATLAB helpcan be consulted learnmoreabouttheiruseand limitations.f un is the passed automatically analyzes functionand decides the how many valuesto use so that the plot will exhibitall the function'sf'eatures.we must recreate function.I /0."I LJ ' zl+ ---- --1t _-l -l 5[ s\ - "L z. B1*A.The f unctioncan be created bungee with an anonymous function: . could be written as >> fl=rnline:('x^2 + y^2'. Although they arebeing phased in favor of anonymous out function. which plots the graphs A A of functions. simplerepresentation its syntaxis of fplot\iun. The functionthat is passed the functionfunctionis ref'erred as the to to passed. inline functionspertbrmed the same firnctions. will also be developing As we our own. This functionis "smart" in that it [xmin xmax].25) *Lanh(sclrt-9.91 5l . [0 f2 I ) The resultis displayed Fig. We can then generate plot from / : 0 to l2 as a >> fplot (vel. [h x --T .lins) where fun is the tirnctionbeing plotted between the -r-axislimits specified 7 rms = by function.function.

Develop M-filefunction an function determine average to value the jumpervelocity of a function overa range. lh -l 4 eJ 5t I Z] X+ .2 A ploio{velocily versus generoted thefplor funclion trrne wilh E X A M P L E2 3.68 PROGRAMMING WITHMATLAB FIGURE 3. The average commands as >> t=linspace (A.81.1+ t"l - 5[ st -r. I2) .0870 lnspectionof a plot of the function (Fig. 81"0./0 >> mean(v) *tanh (sqrt (9.2) showsthat this resultis a reasonable estimate of the curve's average heieht. 81"68. ffir qAb z[ Ll 2 --zl ?.l'€ -r <. lllustrate usefbr thebungee its overtherange f t o mr : 0 t o l 2 s : u(r): ffo"n(rH.) whereg:9.m:68 value of the function can be comnutedwith standardMATLAB Solution. 1.ar "L x j. 1 ) 36.25 / 68. >> v=sqrf (9. 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. 3.

25 ) *tanh >> funcavg (vel . = mean(Y) .o.vQ) oQe llnspace(a.25 / 68. lavg u v L ! s Y I uncavg {o.5 PASSING FUNCTIONS M. ffi z$4 zt+ +.aa f rrrnl {r. B1*68.n) :nr hoiqhL % % ? Z % % % z % x = Y = favg favg-funcavg(f input: f = a b n = - .b. 25 ) *tanh (sqrt (9.9f 5l --T -a.on 9 r r nr"a. A. n ) ro f rra. lh 3$8 .1 ) *t ) .FILES TO We can write an M-file to perform the samecomputation: funcr o 69 ion ^. "L x l{t 3l x+ 4 -l- .ohL f '. this versionis truly generic.n): % % input: 2 a = lower bound of ranqe % b = upper bound of range % n .mean(y) . Because haveremovedthe subfunctionf unc. favg .l:.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 = .I / 0. it evaluates a nonspecific function name f that is passed in as an argument: [*nct. Finally. 1 ) *t ) . end compuLes average of function over value d range function f = func(t) f-sqrt (9. n l-.number of intervals % output: % favg = average value of functron x = linspace(a. f (x). B1*4. the average value is computed.25 / 68.b.f\€ .a.n) : comput-es of average over val. B1*4.-^r- e" f-vg funcavg(o. 12. fun-dvg .n). (0.3. end The main function first uses linspace to generate equally spaced -r values across the range. b.b.60) iavg r'7^.n).It can be run we fiom the commandwindow as >> vel-@ (t ) sqrt ( 9.o. y = func(x).b.60) ( sqrt ( 9.I/ 0. 12 . These values are then passed to a subfunction func in order to generate the corresponding y values. The function can be run from the command window as >> funcavS 36.4121 Now let's rewrite the M-file so that rather than being specific to f unc. B1* 68.

180) 6.) (g) is the (rr) and dragcoefTicient areparameters. 0.. and In Example3. ratherthan "wiring" the nurneric functions.it could be usedto determinethe average value of the built-in sin function between0 and 2z as >> funcavg(Gsin. 0 . time (t) is the independent model.A121 we the However.3.every tirne the passed term vararsin{: } shouldbe addedto the end of its within the function function.7. rangingtiom nonlinear contexts 3.the velocity(u) is the dependent (c. could havedonethe we For example.rch This involves how thedependent variable changes ana\-sis.1. fbllowing: >> m-b8. 36.5. I and c.if we want the parameters take on new values. MATLAB offers a better alternativeby adding the term varargin as the function function is invoked In function'slast input argument.Suppose tn parameters.2. the gravitational and constant rnass the modelsby perto fbrcing function. and usedit to determine a jumper velocity fbr the casewherethe parameters wereset value of the bungee the average thatwe wantedto analyze samefunction. parameters anonymous into As we learned Sec.25. must recreate anonyto mous function. observing fbrmingtr sensitivitlt are as the parameters forcingtunctions varied.I.it is possible incorporate in to values.f uncavg.1.5.70 PROGRAMMING WITHMATLAB 36 .we could retypethc functicln with new valuesfbr each Of dif'ferent but it would be preferable tojust change parameters.81*cdz'm) *t) .. >> -ze1=@(t.r:d-(). fbrcing functions. course. addition.2. the variable.3 Possing Porqmelers modelscan be divided into dependent Recall from Chap.t'.3001e 017 Doesthis resultmakesense? any valid MATLAB expression. For example..funcavg can easily be appliedto anothercaseby merely passingit a different function.s conciseness): for fbr f uncavg' ffi= =1_h" .0. 0 r 2 ' 7 its To demonstrate genericnature.5. pararneters. 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. 2*pi.lt is cornmonplace investigate behaviorof sr.6A) *Lanh(sqrt (9. I that the terms in mathematical variables.but with the L\t :68.For the bungeejumper and and independent variable.rrA { = "L x zJ Zl F+z I .) sqrt (9. the case.we developed functionfunction. (clmittingcomnrent.81*m/cd) >> f uncavg (ve1.the Here is how both modificationscan be implemented argumentlist (notethe curly brackets).

analyzing different parameters becomes easy. we could enter >> funcavg(ve1.1 and c1 .an algorithm to solve the problem would be basedon a loop. This involves obtaininga solution of du cd .linspace(a.Therefore. could be rapidly generated by merely >> funcavg(ve1. b.the problem involved iteratively solving the formula. When all these changes have been made.9345 BUNGEE VELOCITY JUMPER ) t I 1 Bockground.25) ans = 36 . we would probably want to apply the formula repeatedlyto step out from our initial time to attain the value at the final time. 4.Bl*m edr*Lanh(sqrr_r9. this can be done as in . Suppose that we started the computation at / = 0 and wanted to predict velocity at t : 12 s using a time stepof A/ : 0. 1. n---24 0. 68. f avg = mean(Y) . given an initial conditionfor time and velocity. 0. n.5 s. If we used an anonymous function.Bl"cd ml*Lr.f2. the actual parameters should be added at the end of the argument list.28. Z] X+ .12. Consequently. a.we will use MATLAB to solve the free-falling bungee jumper problem we posedat the beginningof this chapter. 64. 100. To implement the case where m : 68.m.edt SqrL(q. In this section. 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.t -:8--UaIm Recall that.3.6 CASE STUDY function favg = funcavg (f.-ILJ ' ?. Solution.varargin{:}).n).b. Ar21 An altemative case. 60.we would employ small steps.28) 38.0. We would thereforeneedto apply the iterative equation 24 times-that is. 0.tr+ ffi q b {e[ --{ st -r "L x 4 3$ 8.5 t2 . varargin) x . vol=t?(r. y = f(x.25. U i + l: u 1 * du' -At AI ) e v)n its ed Now also remember that to attain good accuracy. 7l When the passed function is defined.

uler uret]rod soluLion of bungerj u m p e ] r z eL o c i t y rnpuL: dL .0 .rme step (s) ti = initi. .al Lime (s) Lt = f.25 / 68. = Vl. computation To cover such cases. is not foolproof.9259 Note that the true value obtained from the analytical solution is 50.BL .on for bungee velocity vend = veloc-Ltyl (dt.5 .(A. 1?. v : v + dvdt * dt. This function can be invoked from the command window with the result: >> vel.6175 (Example 3.e. o c r i . We can then try a much smaller value of dt to obtain a more accurate numerical result: > > v e .deri-z (v) dv = 9. while . iL v a t t f = Li.we can usea for loop as the basisfor the algorithm. t. break loop can be substituted place of the a in (notethatwe haveomitted comments conciseness): shaded area the for ffi= zt+ .6i.oci Lyl (0. rf .rl time (s) (rn/s) vi = initial val.1). .ocityl: Iiuler solutj.Bi Althoughthis functionis certainly it simpleto program. 0 0 1 . dvdt = deriv(v) .he intervalis not evenlydivisibleby thetime step. v1) [.72 PROGRAMMING WITHMATLAB continued where n : the number of iterations of the loop.i. t = t + dt.' end f unct ion d. vi ) vel.f -\ 4F+ = \'r?+. 1 2 ./ (dt.n ti) / dL..rble outDut: (n/s) v e n d = v e l .ue of dependent vari. o c . 0 . particIn ular. Lf . end Varnd = V.' n = Far (tf i - 1.= t.Here'san M-file to do this including a subfunction defining the differential equation: f uncLion vend = velocityl % % Z Z % % % Z % t % L 1.. Becausethis result is exact (i.in. O ) 1 50. the ratio is an integer). ti.l)) dns = 50. r .1) end x v''2.y( 0 ..it will not work if '.

25 / 68.(0. 5 .. you might want to include error traps in your code to catch such errors and then allow the user to correct the mistake.the result will be the sameas for the versionbasedon the for structure: .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. - t. We create this dummy variable so that our routine does not change the given value of dt if and when we shortenthe time step.If not (which would usually be the caseat first).the loop will terminatebecause the condition t >= tf will test true. 35. If so. the user could havemistakenlyenteredast3psizegreaterthanttrecalculationinterval(e. Notice that before entering the loop. . [h ^r- sJ 5t 3$& . e ffi . tf. As soon as we enterthe while loop. we do nothing. vend = velociLy2 (dt. we assignthe value of the time step dt to another variable h.L) end * v""2.834r1 We should note that the algorithm is still not foolproof. A) ci: 50 . end dv f unct ion clv = deriv (v ) . h = tf . t1. 12. vi ) while (1) if t + dt > tf.\ ^ f .9 2\9 Further. dvdt = deriv(v) v=v+dvdt*h. ' > v e l o c i L y 2 ( 0 . we can use a dt that is not evenly divisible into tf :'-."I LJ ' -b ffii --I ^l \ Z] X+ x <. we guarantee that the last stepfalls exactly on t f .6 CASE STUDY 73 continued function t = ti. If we run this new version.g. Thus. end tf. tf .9. we set the variablesteph to the intervalremaining:tf * t. 0 .3. After we implementthis final step. break. end if t >end vend = v. we usea singleline if structureto test whether adding t + dt will take us beyondthe end of the interval. we would shortenup the interval-that is. l : 1. 0 ) 5A.ti = 5 and dt = 2 0). ] . For example. 1t r h.velociLy2 (0.81. A. By doing this.

2 An amountof mon interest is compounde worth F yielded at an determined from the fo F:P(l*i)" b.ti. y j ) i{ c-nrl Yenrl t.3 Economic formulr payments for loans. 1 r ) As a test case. l l ) - "/oeffOr 'r(l-92b'r We could then analyzea different function without having to go in and modify the dy/dt : -0..74 PROGRAMMI WITHMATLAB NG continued As a final note. t.4'l Write an M-file that wi vestment for each year function should includt rate i (as a decimal).f . we should recognize that the foregoing code is not generic. For example.se q .by specifyingthe differentialequationwith an anonymous funcbungee tion and passingits function handleto odesimp to generate solution the .:ii nrr.* 11.th -r zl F+ jl€ .lI ll .r x2 cos-r: I - t -2 COS. a r ..an future worth is to be ca a table with headingsa gramforP:$100. t. compute and d true .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.employ up to and including e xto 14r. ! . { l * y .'= = Y' tf. h = dt.1 . In o sequencethe valuesfc 'l : COS.y hasthe anaM-file. y = yi. ) ' :(.r . m o r . o d e s i r n p ( d y d r . the differentialequation : l0e-0 r'. : up to the order term o ceding. r < .: . PROB 3.\r (1+i). t-f L. The ment A is AP "' i/ | a./ + dyclL(1) * I'ii f ts h.-t "t . h =. ) .*ufleqqte zr+ ffi -- x -a.\ ( . r l .. Therefore.if .t -.rrd . l .. break. l t i. i J { . C t . A more generic version can be developed as f L r n c . 1 3. Su money P and agree to interest rate of i.(. od. r .y: lQ al t :0. (1) whiIe j 1' t + c]t. 0 (y ( ) -0..2 \ / 6 f .00( 3. . . That is. .0653.:l 5t 3$ & .1 The cosine funct infinite series: cos"r: I - t i.' . l e : r 1 p ( c 1 v c 1 l .the solutionat t : 5 would be y(5) : l0e*0 r('5) lytical solutionjy 6. L .' t i o ny e n d = . can thenusethis routineto solvethe of jumper example. Lf. r:nd Create an M-file to ir putes and displays thr series is added.0i. l. a ( v ) .We can use oclesimp to obtainthe sameresultnumericallyas in '':. l l . L ) * u " " .. l : l . we have designed it to solve the specific problem of the velocity of the bungee jumper. I l .

: thc frequencyof thc annualvariation o (:2n1365).S.. and tn"^r. an interestrateof 6. l Usc dccisional structures writc an M-f ile to compule to Write M-filc that will calculate futurcworth of an inan the the tank'svolumc as a flnction ol'given valucsof'R and 11..3Economictbrmulas are available to compute-annual payments loans.5 FIGURE W ZN 4Y NT ffiE\ zJLJt -& +.1 The cosinefunction can be evaluatedby the following rnfinite series: x2 . 4 . The tblmula to computc the annual payrate ment is A t-p iil + l)" (l+i). in MA (t: 180to 242). vestment each ycar lrorn I through rr. The output shouldconsistof future atable rvithhcadings and columnsfirr n and F. AZ B i s m o r cN .: day ol' the pcak tempcrature (=205 d). FI Yumo. In. up to thc tcrm lirr (a) January-Fcbruary Miarni."nn) cos((. towns are listcd here: fbr 2l .6 229 : For eachol-thc preupto thc orderterm ol'youl choosing.s: thc peaktcmpelaturc. 3 . In other words. FL 1l : 0 to -59)and (b) JLrlyAugusttcnrpcraturc Boston.I. MA T-. computeand display the perccntrelativeerror as ceding.06. +x4 4l the -"Y City AAiomi.-) t + | | Create M-file to irnplementthis folrrrula so that it coman putes displaysthe values of cos x as each term in the and series added. r'* f141. kD WA Seoiile. I and the numbcrof yearsn firr which the worthis to be calculatcd.lf'the liquid levcl is quitc low. P t 3. includethe initial investment thc interest rate (asa decinral).4 The averagedaily temperature for an area can be approxirnaled the followingfunction: by 7 : 4.000and A.Supposethat you borrow an amountof for money and agreeto repay it in n annrralpaymentsat an P inlerest ol l.-.on ("C) T"""r ("C) 283 336 221 )7.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 . Run the progtamfor : $100. in the conicalpart.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 .r :I : cos-r I cos."^n * (Q"ar. compute and display in is sequence valuestbr the C O s . : 0.2 An amount rnoneyP is investcd an account ol in inlerest conrpoundcd thc cnd ol'thc pcriod.(/ .l f t h e l i q u i d level is rnidrangc the cylindricalpart. <Ah -1 st 4 fi:l - sp 5t 3$8 .5 Figurc P3.Conlpu(eresultsftrr l : 1 .senesapproxrmatlon true ) the [nc- 221 231 52 t0 6 la7 : T. 2 .. Boston.employ the prograrnto corllputecos(L5) fbr in up to and including eight tcnrs-that is. 3. shows a cylindrical tank with a conical wherc 3.066).-5 3.lp"ol)) : where Z. Tcst it As a testcase. lrue .-l P3. Paramcters someU. y the ) anal5t Dcvelop an M-file that cornputesthe averagctcmperature hetrvcentwo claysol' thc year {irr a particularcity.000. r : I . 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 .Tlrc luturc' basc.r: I Write an M-file to compLrte Test it with P : $55.error ^ 1007.PROBLEMS 75 PROBtEMS we Dre 3.6% (l : 0. The input t0 the lbr the Desisnthc tunctionso thatit rcturns volurncfirr all cascs function should P.u""u the averageannual temperaturc.

Test it summarizes possibilities: the with the lbllowine data: xy0 R d 1.f 4+-4-n-vI -\ ?..8+2H/ Develop M-filethat< an distance along beam the the beam.76 PROGRAMMING WITHMATLAB where the depth is less than 3R.6. 60 < numeric rode < l0C o numeric rode < 90 g q numerlc rode . Enter thesev umn represents param a nel. 1 0 .e.zxl r * loo lr.rJ 1T T rl2 -n /2 0 where U: velocity(m cocfficient. BO numeric rode < /0 c n u m e r i cg r o d e < 6 0 + 7 5 ( x./.6 Two distancesare required to specify the location of a point relative to an origin in two-dimensionalspace (Fig.sJ 5t I <. and(). sing the follows: (x-tt)":lt*-o l0 to 3. d > 3R.U s i n g s i n g along the heamcan be e -0 A B C D F 90 < BO 1 /O .9 Manning'scquationcan be r.Includeheadin 3.rsed computethe velocity of water in a rectangular open channel: FIGURE P3.11 grade liom 0 to 100 and returnsa lettcr grade according to the schcmc: Criterio Write an M-file that cor channels. .6.6). However.sosirnple. then a sirnplelbrmula can bc uscd to compute 9: rl : tan I (I) \_r.5 I t5 2 t5 45 t< Note that the tank's radiusis R. If the coorilinates within the first and lburth coordilic nates(i.x > 0). y) in Cartesian tion of -r and_r'.y) on the basis of polar coordinatcs reverseprocess not. rathcr lhan designingthe function to cvaluatea single casg.. Thc radiusand angle(r. Return an error message The difliculty arisesfor the other cases.0 OO 00t 0. Have the M-file di computedvelocityin ta colurnn. 3. The following table ("Overtop") iI you overtopthe tank-that is.Testthe prograrn the cases fbr outlined in Prob.6): Write a well-structured M-file to calculater and 0 as a funcTest Expressthe final resultsfor d in dcgrees.passvectorsof ir and).y.6 Jsr BH \2' il \.0 00( as 3. your programby evaluating fbllowingcases: thc coordinates.The radiuscan be cornputcd is by the fbllowing lbrmula: <0 <0 <0 :0 :0 :0 >0 <0 :0 >0 <0 :0 ton l"-. P3.. Havethe f unctiondisplaythe results a tablewith columns as forx./ 0 0 I 0.B: width ing data is available fo . d) in polarcoordinates.7 Dcvelop an M-filcr to detcrmine polar coordinates descrihedin Prob.7 By dcfinition.3. ffi= =n . [h - 3l x+ . . The nates(-r. r. 3. Dcvelop an M-lllc function that is passeda numenc 3.035 0 020 o 0t5 0 030 o 422 0.t'+ +Y jl€ . The horizontal and vertical distances(-r. P 3 .10 A simply suppo F i g . xJr| It is relatively straightfbrwardto computeCartesian coordi(r.:l*?-+v.

r > a both thc rcsult value. tc.r + ulx along bcamcan bc cxprcsscdhy thc ccpration: the 2 -s t5 ' Writc a wcll-structurcd M-l'ilc lunction bascd on a :'*l ...J I 3l x+ . .b|eak loop structurco irnplcmcnthis algo57 r so rithur. width (m). _. 4 . old-timcmcthod an lncludc hcadings thc tablcto lahcl thc colurnns... _ J I tt and the errur.ncl-ilfl.rurirpprtrrillluli()n thc irs 6 t._ tt .Designyour programso that it returns .25 t * ? 5 ( r 7 ) r+ clrch\tcp c\tinrirtc crrrrrin y.For the latter that For examversus casc.ol'radius andlcngth is related thedepth hbv lbr ls: ing is availahle llve channe data n l-.12 Dcvclop a vectorized vclsion ol'thc lbllowing codc: 1.10. L ( L) -ts1. lirr approximating square thc 3./r V-lt-cor'[L \r 0 000t 0 0002 0 0010 0 000/ 0 0003 hr | / rr --l htr2rlt h. I O E (rn/s). Have M-l'ilcdisplaythc input data along with thc c l r t ( l thc vclocity tabular in firm whcrc velocity is the l'ilth computed 3.:ildrL=U.t -I t r n r u ' l follows: Rcpeatthe loop until a is lessthan or cqual to a spccified (.r/lr ^ -. where vclocity V horizontal cylinder U: S: channcl slope. (. rtl" when. singularitylunctioncan be cxprcsscd as thc " . 3. .r.At '\ 23tt. Notc that. on column.t1L.. . Makc surc that it can cvaluatethc squareroot when -r < a I I o1'nunrbers arc equalto and lcssthanzero. Write M-filcthatconrputcs vclocitylirl cachol'thcse an thc Enter channels.roughness 3.ll Thc "dividc and average" method.Use ploper indentation that thc structnrcis clcar.displaythe resultas an imaginarynumber.The tbllow.2 rn and L: -5m. displacerrncnt . thc Using singularityf'unctions. root ol'any positivcnumbcra..rrn Test your program distance the beam. DevelopM-filc thatcrcatcs plot ol-displacemenl a an 21..r)--l'r-0)'-(r oo t whil--..1l Thcvolumc of liquidin a hollow n liquid r L to o1'the B: coefficient. a n d."I LJ ' zl'+ ffi A b q st Z} z. uthebeam. rri 5. P3. thc square along b y e v a l u a t i n g 0 . Tcst thc program l-orr . root of -4 would retr. and H : dcpth(rn).. thesevaluesinto a matrix whcrc cach column reprcscnts a prramctcrand cach row rcprcscnts chana nel._.Il/ I 0035 0020 00r5 0030 0422 r0 B 20 24 t5 Dcvclopan M-fllc to cl'cilte plot ol'volume versus a depth.t : 0 at the leli end ol' plc. 2 .. ..PROBLEMS 77 20 kips/ft :-c F I G U RP 3 .1 Bydetinition.r x) lrr.10 simply supportcdbcant is loadcd as shown in A can bc firrrnulatcd as Fig.\t'ltl .9 f b r c : I x l 0 r .

3. 3. Z] X+ 3$8 . B L :':' oclesirnp (dvtlt . year) 3.1 2 ) .5. 3. A . February 29.n.3 l ) . 5. m .6 so that it can bc passcdthc argumcntsof days in a year. /(x) -0.2002.June21.18 Developa lunctionfunctionM-flle thatreturns difthe ferencebetweenthc passcdiunction's maximum and minimum value givcn a range of the independentvariable.78 PROGRAMMING WITHMATLAB w h e r em o : t h e m o n t h ( l . 2000.The first line of the function should be set up as function nd = days(mo. Testthe prograrnby roundingcachof thc fbllowing to 2 dec.0 .June21.March l. tt9. 2002.andDecember 31.17 Develop an M-file function to determinc the elapsed days in a year.March 1.t ^l +.135. use this lunction to gcneratea plot of u versus/ fbr t5to50. 467. c : c 1) 9 .(b) : es'sin( l/x) liom -r : 0. 2001.4 ( a ) . and at 3.the velocity of a rocket might be described l1t2-5t ll00-5/ 50t+2(t-20)2 l52oe 0.0 2 s ' s i n ( l ) f r o mt : o t o 6 T . inraldigits:r : 467.16 Develop an M-filc function to dctcrmine the elapsed the end ol'Sec. The first line of the f'unction shouldbe setup as f uncrt i.2000. 0.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. Tcstit lbr thc lbllowing casc: up as Develop an M-flle function to compute u as a function of /.For by by example. L2 . le. -0. 0.0. The f'irst linc ol' thc function should be set the passed lunction. d a : t h e d a y ( l .25) >> zt'+ .958'7. a n d year : the year. d v c l t . l ( 0 : l 0 e .9587.1999. January 1999.I. (c) The built-inhumpslunclionliom x : 0 to 3. February29. cla.Test it for l. 2001.rp) (c<1/m)*v^2.3 1 ) .@ ( v .Test it for January l. d a : t h e d a y ( l .rn nd = days (mo. n) . Then.14 Piecewise useful when the refunctions are sometimes variable lationshipbetweena dependent and an independent cannot be adequately represented a single equation. havethe lunctiongenerate plot of the functionfbr a the range.19 Modily the functionfunction odesimp developed -1.1 2 ) . a n d leap : (0 for non-leap year and I for leap year).2. 125. Hint: A nice way to do this combinesthe for and the switch structures.13."1 {t _q ffir st 71--sy -.125. 3. 0.01 to 0.Test it for the following cases: function xr = rounder(x.15 Developan M-file lunctioncallcdrounder to rounda number-r to a specifiednumberof dccimal digits. 2004.2004. and December31. da.

the with a llnite difl-erence.a n d :st it for Iune 1..1li. Recognizing that truncation errorsoccurwhenexactmathematical fbrmulations ilrc represcnlcd approx by irnat ions..ti 79 . and ccntered finite-difTerence approximations first and seconcl of derivatives.."lii-: i Roundoff Truncotion Errors ond ll). Understanding to write lbrwarcl. ""j 'rt6.1ffi : i irl:r 1. t. accuracy how to quantityerror. . 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. Learning Learninghow errorcstimates be usedto decidewhen to terminate iterrti'.+t . and nry 29. YOU'VE GOT A PROBLEM jumper. 2 do this elapsed J be set ."'. !]=!!dr Lt u(ti+r)-u(ri) t..'e can an calculation. Understanding lloating-point why numbers have[irnitson their rangeand precision. Speciticobjectives topicscovered and are :loped at mentsof ' ' ' ' r) ' ' ' t ' Understanding distinction the between and precision. you developed numerical a model for the velocity of a bungee you had to approximate derivativeof velocity solve the problemwith a computer.1 . In ction for CHAPTER OBJECTIVES The primaryobjective this chapter to acquaint you with the major sources of is of errorsinvolvedin nurnerical rnethods. Knowing how to uscthe Taylorseliesto estimate truncation errors.2001. Understanding roundofferrorsoccllrbccause how digitalcornputers havea limitedahility to represent nunrbers.To n Chap. how backward. the difrd miniiable.ii i 1 ) . I.

Ccneralinformation concerned ol'erroris of l-his is fbllowed by Sections 1. rniniThis chaptercovcrs basictopicsrclated tlreidentification. thenwe viationsftom the are new modcl.Imltrecision(alsocalledurrc:ertaintl). thc two cases equallybiased arc theyate of both cerrtered the upperleft quadrant thc target. graphically Theseconcepts can be illustrated using an analogyfiom targetpractice. it is rarely if ever atperf'ection a laudable is on uncertain information.\'iorr computed or measured valuesagreewith sachother. I e a to w L( rh .and datauncertaintv. Att'unrc'. to and with the quantification mizaticln these errors. of The bulletholeson eachtargetin Fig. 4. it has error. computer limitedin its ability to represent magnitudes the precision numbers.Thus.Prct'1.Inaccurucy ical technique.4. lD and on the other hand. whereas bull's-eye the the defirrecl systematic deviationfrom the truth. 4.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.centered thc bull's-eye).we briefly discuss crrorsnot duce truncation error sometinres Thessincludeblundcrs.2 and4. machine the itselfyieldsresults and of thatcontainenor. increase Finally. is an excellent it cxactly approxirnation. Beit is the cause is a digitaldevice. despite tact that the rnodeldeveloped the neverin practice predictthejumper'sfall.1. thesedeviations systematically If high or low. the resultingsolution is not exact-that is. errors. alsodescribe roundolT.althoughFig. to (i.4.However.v rcl'ers how closelya corlputedor meato regardto theiraccuracy ref'ers how closelyindividuitl to suredvalueagrees with thetruevalue. scientists to deal use 4. on Therefore.i)is represents truth.ref'ers the magnitudeo1'thescatter. you to sonleapproaches concepts and that engineers and certainty'? This chapterintroduces with this dilemma. quantification. Consequently.For example.nation and Your problemis: How do you deal with suchunsulting rnodelpredictionto be uncertain.1 Accurocy ond Precision with The errorsassociated with both calculations measuremenls be characterized can arrd and precision. l can be thoughtof as the predictions a numer(alsocalledbirr. althoughthe shotsin Fig. I I i I 4. (due error:rclundof'l'crror to computer and majorfirrmsof numerical approximations) trunWe how strategies reto cationerlor (dueto mathe nratical approximations). your digital computercauseyour lrboth your mathenlatical So approxir. with the nurnerical nrethods rnodel directlyconnected themselves.the computeryou use to obtain the solutionis also an imperfecttool.lc are as because more tightly grouped than in Fig. In addition. also i n t o t h ea n a l y s i s .I ERRORS constantly find themselves havingto arccomplish objectives based i Engineers scientists and goal. rzrndomly and might needto devclopa il-theyare distributed tightly grouped prediction. latteris more precise on the because d are eqLrally accurate the shotsaretightly grouped. would would resultin desuchas windsand slightvariations air rcsistance in A varietyof factors prediction. rnightbe considered negligible thenthe deviations andthe aroundthe Numericalapproxirlations introduce similar discrepancies model deemedadequate.la. Although liom Newton'ssecond law tained.e.. dealingwith the two reviewedin this section.3.

l). numeris bia.They also shouldbe precise nrentsof a enoughfbr adequate design. 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 .u p p r o x i m a t i o n (4. A shortcoming this definitionis that it takesno accrount the order of magnitudeof of of thevalueunderexarnination.(4.or true. 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 . sucherrors.s) 4. example. In tlris book. It in delhen we I tightly and the :pancies r c lm i n i 'error is the two s to reors not . (.l ) Eq. rlodel o) FIGURE 4.result the and thc approximation be lbrmulated carr as True value: approximation error f ( 4 .2 Error Definitions ed with or rneamputed rractice. i n t as 6 r : t r u ev a l u e.l b and because Numerical errorsariscfrom the useof approximations rL'present to exactmathematical opand quantities. our rerch unersand o 0) a s based )ver atrnd law :r'sfall.I ERRORS 8r accuracy Increasing ol.1. 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 . J p e c : s e .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 . the The subscript is included dest to ignatethat this is the "true" error.as describedshortly. e o e n .Beritudes n errol'.Note that the true error is of commonly expressed an absolutevalue and referredto as the absoLute as error. (b ( o ) i n o c c u r o t c n d r m p r e c i s e . o ("o r\umerrcalmethocls snoulcl sufiicrenrly oe accura("e unorused meet irc requtreor particular problem.d { r J ) ( ' L r o ' ee r '. t o 4. wherean "approximate"estimate the enor must be employed.I c' are they are rtainty).21 whereCr is usedto designate exactvalueof theerror. errorof a centimeter muchrnoresignificant For an is . l.4.the relationship For erations between exact.This is in contrastto other cases.


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


wl be pe to

express as it The relativeerrorcan alsobe multipliedbv 100%,to true value - approximation
cl -

true value


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 real-world 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 value-that 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 real-world 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 ries

Thu esti
: '

^0.5 c .1

with term fied t



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.



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


,\'l 2




.r" nt


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 7-2 1 . . s t I =9.02(/t I x 1001, t.64812t I


estimateof the error, as in Equation(4.5) can be usedto determinean approximate

:.1.1.3(Z r., : l-j_=_-l x 100(/o
I l.) |


I b


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



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


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


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 ill-conditioned, 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



I Sign



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 base-10,number system.A baseis the number used as the ref-erence 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.se-8, an representation. the samesense, In our fiiend the computeris like a two-fingcrcd animalwho is limited to two states-either 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"^e-2, 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 base-10 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 l6-bit 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 l6-bit 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

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


















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 l6-bit 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 n-bit word,the rangewould be from -2" to 2" - l. Thus, frorn . 32-bit 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. Flooting-Point quantities typically represented comRepresentotion. Fractional are in putersusing floating-point.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.00-5671t l0('.Howin x ever,nort.ttalization would yield -5.678,. l0 I which eliminates useless the zeroes. Beforedescribing base-2 implementation the usedon computers, will first explore we the fbndamental irnplications suchfloating-point 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




F o


e) 0


pl TI or


w( ca


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 5-digit 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




nt nis

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 base-10, 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, one-digit 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 2-5 : 0.0-l125 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-


. the :imal ne so rle, a {owplore at are ntissa a nice

1 0.03 25

: 0.00t1

FIGURE 4.3 5-digit 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

9 . 9x 1 0 s Overflow l--{

- 1 . 0x 1 0 s

1 . 0x 1 0 s

9 . 9x 1 0 s


|-{Overflow ,Underflow " H o l e " tz e r o a



0.980.99 1

1. 1


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



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



- 3 3 . 1 4 1 5 9 .r
3 .l 4 I . 5 9


: 0 . 03 2 1


F Although adding significand digits can improve the approximation, such quantities will alwayshavesomeroundofT errorwhen storedin a computer. Anotherrnoresubtleef'fbct of'f'loating-point 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 roundofl-error 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).


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 lloating-point Now, representation. quilntities actuallyrepresented a real computer let us examinehow floating-point are in usingbase-2 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 binaryfloating-point numbers can be expressed as X(l-tf)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 2-3.


P "l

Signed exponen 1 1b i t s



52 bits

Sign (1 brt)

FIGURE 4.5 The monner which flooting-point isstored on B-byte 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 double-precision.fbnncrt which eight in bytes(64 bits) are usedto represent floating-point 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 l-argest 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 2r0-rr 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 -



I. I4I592ri53a891.) Note thatthe machine epsilonis 2 sr : 2.2204x l0 16.


MATLAB has a numberof built-in functionsrelatedto its internalnumberrepresentafunction displaysthe largestpositivereal number: tion. For example,the rea Zmax
>> >>




format realmax






Numbersoccurringin computations that exceed this valuecreatean overflow.In MATLAB they are set to infinity, inf . The te-aLmitT functionclisplays smallestpositivereal the number:
>> realmirr




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.p-s function displays the machine epsilon:
>> eps

L n
are set t0



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 base-10 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 4-digit mantissa a l-digit exponent. and When two floating-pointnumbersare addecl, numbersare first expressed that the so they havethe sane exponents. example, we want to add | .5-57 0.0434 thecomFor if l, + puter would express nurnbers 0. 1557x l0r + 0.00+3-ll 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.095-5 l0' For this casethe resultmust be nornralized because leadingzero is unnecessary. the S0 we mustshift thedecirnal one placeto theright to give0.9-550 l0' : 9.-550. x Noticethat





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 base-2. Suppose thefollowingM-file that is constructed: function

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 significant-digit 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 base-10, it cannot be expressedexactly in base-2. 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



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 the4-digit nrantissa the l-digir 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




: .trr,vr *,r:.v:*''' * x,,-)',, E.tr.t'
in linearalgebraic This operation very comrron,particularly thesolution is ol-simultaneous 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

(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 du-A,u
dt Lt



u(ri+r) u(t1)
t;"t I ti


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 fashion-the Taylol series. in numericaln'rethods express to


.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 Tay-lor.renes as then providesa meansto express this in ideamathematically a form thatcanbe usedto comeup with practical results.

fac on tior


r i + r )] .r r O 5 . You are givcn the task of pledictingthe heightat a to p o s i t i o nr .it is likely thatthe new valueis probablysimif i l a r t o t h eo l d v a l n e . Equation(4.a 2 5 x * l 2 o t r : n zero order.r ) : O l . constant.r.6 l by way to gain insightinto the Taylor scriesis to build it term by terrn.9. you areplaced a platfbrmthat is completely horizontal thatyou haveno so on idea that the hill is slopingclownaway fiorn yon.11'? you think aboutit (remcmbcr you haveno ideawhatsoever at If what'sin front of you).ond secondorder Toylor series exponsions.r. 11 .ERRORS 4. indicates that the valueof I at the new point is the sameas the valucat the old point. c At first.).r a.first order. 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 r . which is calledthe7. andr. provicles perf'ect estinrate the functionbeing approxirnated in f'act.what would be your best guess the heightat x.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 .9) This rclationship.'3"^ ri=0 h r. our problcm.You immediately in sense that the front .r'(.you would be right only if you happened be standing For a to on a pcrfectlyflat plateau. a if is.if thc functionchanges all over the interval.n):fk) \:[:*':d::-s = /(.A good A usel'ul problcrnc()ntext this cxercise to predicta functionvalucat one point in ternrs the is tbr of point. However. . are to So now you are allowcdto get ofTthe plattbrrn and standon the hill surface with one leg positionecl tiont of you anclthe otherbehind. 1 ( x r ) G.11 erecloseto eachother.. At this point.4.th \..ero-onler uppntximatiorz.6).+t : 1 T h eo p p r o x i m o l i o o { / ( .1) ^xt .This resultmakesintuitivesense because r.3 TRUNCATION 93 Zero order Jk.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 ( . respect thc baseof the hill l(. FIGURE 4.O l 5 .

r). Recognize Before proceeding.. .f (xi+). slopebehindyou by keepingone foot plantedat x.fi. n ' * " f ' ' .{ 4 . and moving the other one forward Ax.j J .In essence.You canuse of To makeuseof this fbrmula. 1 3 1 . valuesthat you have estimated.94 ERRORS AND TRUNCATION ROUNDOFF estimate of foot is lower than the back foot.fi(. You immediately Clearly the drop in height is "accelerating"in l'rontof you. Let's call this slope that the slopebehindis milder than the one in front. f ( r . f ( h ) = a 2 h 2I a 1 h a 1 y the Taylorseries approximates functionwith a second' Thus. ' + " * f " ) t t i t r ' + R .a1.ri) *. this case. the expression .it at quently. .You can express (4.r. t ' ' G i ) +/ ' ( r .11. (4. and moving the otherone back a dist a n c e A . Conse' That is. Thus. (. . term to your equation you're now going to add a second-order As you rnightexpecf.Thus. * J"lr'l L : ^2 No1 sig acc wht and app ide non SOIC con den yie inn eno req Thi SOIT . With this additional information. subscripted represent r positionx.1 t ' l r .Q ing one fbot planted at x.we could keep addingmore derivativesto capturemore of the function's expansion Taylorseries vature.11. functionbetween and-t. .rr)is even lower than your previouslinear prediction.it is only exactfor a straight-line or linear.firsrorder approximation is between and x. you're allowedto obtain a quantitative the slope by measuringthe differencein elevationand dividing it by the distancebetween your feet. rn + oft lou onl trat -----c-- .t') (n- the derivative. l ' G i + ) = .. the odds are that . + / ' ( . use the slopeestimate projecta straightline out t0 by this predictionmathematically . a slope/'(.r. recognize .we needto add more terms to our equation.1.l0) can predict a change. In fact.u)h of the because additionalllrst-orderterm consists This is calleda.So now you the First. f" ( r i + ) 7 JQ con . only unknowns the values the prediction the are is a quadratic equation the tbrm of -r .f (. cut' Clearly.fiQt) .) multipliedby /r. they are numbers.. L .' (o.f'(. Although Eq.we can seethatthesecond-order orderpolynomial. rate Thus.Qi) Ax CITC (4. trend. 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 . L e t ' s c a l l t h i s s l o lp e i ) . Thus.1. you measure are allowed to standon the hill surfaceand take two measurements.we arive at the complete wht The this plv bas wil ter l . of that all the values let's look carefullyat Eq.Consequently.2) r wer ber the of in of derivativeis merelya derivative a derivative. ..you needan estimate the second you determined estitlateit as to lasttwo slopes .r. .ri+r) "f(.To get a betterprediction.10) = f (. The Taylor seriesprovidesthe correctway to do this as in and make it into a parabola.. the distance ofthe or now in the form of a straightline that is capableofpredicting an increase decrease J. you're clearly in a better position to predict the to you heightat . r ): f ( r . the second change the slope.I l).

+l g. ( . f t h ec r l r r ri s 0 ( f i r ) .ical (Eq. 4. ' * ' ) 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 .3. cxample. For otherclifl'ererrtiablc continuous Eachadditional term will will not yicld an exactestimate. / ( . and.9) through (4. soids.nn * I to infinitv: R .a finite nunrber o1'terms r s n.. lbr high ternrsusuallyaccount a dispropoltionately percent the error'.3 TRUNCATION an the Eq. 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 ) .o c v a l u a t e q . D e s p i t et h i s d i l c n r n r aE q . to only a t-ew termsare required obtainan adequate tratedb_\.e a valueof -t'thatlies somewhcre states that any smoothfunctioncan be Thus. is valueof Taylorscries expansions that. can usuallyassume the truncation sn'rall. n s e q u e n i y . H o w c v e r . 1 ) i s u s u a l l y x p t e s . fiorn.l1) fbr n that this is the rernainder the nth-olderapproximation wherethe subscript connotes . to o1'the that multiderivativcs implics nothingregarding magnitudc the this approximation rnethods error of nurncrical ply firr+t.l . 1 .judgingthe comparative Fur based Taylor scries on exparrsions.l4).l ) . q . will be exactfirr arrrth-ordcr polyTiiylor series expansion In general. A remaincler accountfor all terms fror.. of Thus. is between lifldr.h a l v i n g h r .w e n e e dt o k n o w . otherwords. lower'-order This propertyis illusapproxinration. ( 4 .anclother In il'fi is sLrfficiently the of tcrnrsto the Taylor series. it is extremclyLrseful in.r).. Note that because (4. Although That is. 1 .: 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 . ( 4 . S c c o n dt.. a n d . the ellol is proportional thc stepsize/r raiscdto the (rr * l)th power'. is Althoughthe tbregoing true.if the crror is O(fi). s e d a . f r v e k n c w i o /(. (4. 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. T o d o t h i s . equalsign replaccs approximate terrn is also includedto sign that was used in Eqs. \ .+ r .r'wc wiurlto evaluate (r ). nth-orcler the suchas exponentials sinuand and functions. E c r s o m e w h e rb e t w e e n. following exanrple.{ is not known cxactlybut melely lics This relationship two major drawbacks. the .I l).13)is an infinite series.J / t / / + ll l i \ l'-7. 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 . 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.t e ps i z ew i l l q u a r . on tcrrnof the expansion is required get "closeenclugh" bascd the remainder to has First. first. by that erroris decreased the addition we In general. ter the error.ERRORS 4.This is because rlo havecontrnlovcr lhc term ir in thc cquation.we can now seewhy the Taylortheorem provides means express to this a and that thc Taylor series approximated a polynornial as ideamathematically. norlial. many cases. s rs R . . r ) . 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 . llrere would be no neeclto pcrlirrrnthc Tirylor scrics expansionin the present ! contexf i . . 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. The assessmcnt how many tL'nxsare ol cnollghto the true valuc lilr placl. 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 . andwe cilltcontrolIhenttntwe canchoose how lar awa-v 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 .h ei n c l u s i o n l ' o n l ya f ' e r ve r n .t'.1.the practical i tn o t s i n r n o s t a s e st.o w e v c r l i g h t .1 .

lu_L9#Sl'or* :rt o* term where. 4 B si t ( i ) = ' " . .0267a' of more althoughthe addition of we have attained99.the irr.)\i)- /z\ /r\ /r\/r\ cos(]r/4(l\':0. by For thiscase' the comeswith the initial terms' alsonoticehow mostof the improvement that which means the tinre we have aclded third-orderterm. s 2 r e 8 6 6 s s ' /\ \ 4 -+ \-rl derivawe approximatitln.(4.Consequently.)--'"|'..Thepfocesscanbectrntinueclandtheresultslistedasin Order n * Gr fr"(x) cos.44g%. Thus. add the second which has le.r -sin -r cos.( i ) : o' o ' r 0 6 7 8 l a which represents percentrelativeerror tlf er se : .96 4 EXAMPLE .'. For the seconcl-ttr<ler "('r ) : .l:4. / " ) .rprovement terms will reduce .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. the inclusionof additionalterms results nrate.9)l 4.4()1154491 2 \r2l ---"' estiir.f'(x) : -sinxl the we approximatit'rn. 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.400/o.-= . ..t s i n. negligible' becomes the enor turther.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.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.l:0.l 414 440 a.ran improved with le. i n f 4 /)\(1 2 l) : 0 .5. adtJ first clerivative For the fir-st-order It sll r\/r1.974o/a the true value.cosr: tive terln where/ AI (jfl ca ap t(i):c.'s(. Use Taylor seriesexpansions .Thezero-orderapproxinrationis[Eq. Our knowledgeof the true tunction meansthat we can determine va|uef(r13):0. the error is reducedto 0.

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. 4. L l s e i p s 6 r c f l .+r). ( 4 . f"tv t f{l'tt t .8. (4. : r . \ it h+ + h 2 + ' . . .. 4.as in simplification might be to truncate remainder the = R11 . of this prediction.visuallybased explanation. The remainder.['(x1)lt (4.. r ' r o ' ) .1s) lower-order for Although. we will use a f simple.onu"Ji./(-ti) predictionis shown in Fig. or A visual depictionof this zero-order of error.3 TRUNCATION 97 4.higher-older terms. consists the iniinite seriesof terms that were truncated R r:r. I 3)] after the zero-order term to yield ] .nt to deallith the remainder this inflnite series in itself. (4. / ' ( . t d .7 r GrophicoL depiclicn cf o .2 The Remoinderfor the Toylor SeriesExponsion how the Taylor seriesis actuallyusedto estimatenumericalerrors.To do this.14). the derivative As menn-value states that basedon a grnphical thertrem FIGURE 4.l^''h3+. r o r .7. Before demonstrating we must explainwhy we includedthe ar-{ument in Eq. This "inexactness" impliedby the is cause the neglected of and ( : ) e m p l o y e dn E q .T o .3.as statedin the previous section. format. in Fig. )t.r./(. which is also shown in the iliustration. de n o r .this resultis still inexactbesecond. derivatives usuallyaccount a greatershaleof the remainder than the lrigher-order terms.One It is obviouslyin. 1e at re . d e t l (xl Ll.ERRORS 4. the Suppose that we truncated Taylor seriesexpansion [Eq. l 5 ) .

l7) exact.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.la) Similarhigher-order 4.).(a.dividedby the run h. matic and i .t)h Ro-.A physicalillustration this theorem that. then thereexistsat leaslone point on the functionthat hasa slope.8). version is I"(F) R1 : :--------7. 4. two pointswith an average therewill be at leastone momentduringthecourseof the trip when you will be moving at that average velocity. used Eq.ri+1.l{.4. . thatis by parallelto the line joining.theslope is equalto the riseR.*.r.3 versions f'hus. is simpletu realize it that. Li Equ ence r4l7r For this case. versions be developed can from Eq.il'you travelbetween is of velocity.t''' Thu tion shou expe (416) 4.16).oto -{ive (. or .. (4.ng..as illustrated Fig..we havederived zero-order the version Eq. it may not be clear to you how the expansion to can actuallybe applied wher. in By invokingthis theorern.designated /'(€).f{x} n R o1 Nr Eq h FIGURE 4. f'(t): + whichcanb.r.valuecorresponding the second derivative to thatmakes (4.8.the value of { confbrmsto the r.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 .:r. this { slopeoccurs(Fig.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.) The parameter marksthe r valuewhere and.3 Using lhe Toylor Seriesto EstimqteTruncotionErrors will be extremely usefulin estimating truncation enorsthrough' AlthoughtheTaylorseries out this book. ilurr.l(.14). to.3.l'(6) R. of The higher-order Thefirst-order aremerely logical a exfension ol'thereasoning to derive (4.

and i * I to estimate derivative the .l4) and (4.finitedifJ'ergenerallyas ence. As specified Eq. u(l) of time. I . First-ordcr appfoxr nra(ron Truncatiorl e[ot that was usedto approximate is The first part of Eq. we have alreadydone so in our exampleof the bungee I and I . ) * { f LI .f 9) is given a formal label in numerical methods-it is calleda.4 Numericql Differentiotion Equation(4..ti.However.19. l l ) o r t h e l l r s t p a l t o f E q .t i ) z+ " ' . we were interested deterrnining in can be expanded a Taylor series: . ( 4 . Now let us truncatethe seriesafter the first derivativeterm: r u ( t i + r ): u ( t i ) f u ' ( / r ) ( t r * . - ti) or R' ti+t.3. le with this approximaof error associated we havenow obtained estimate the trtrncation an tion of the derivative.) T h u s .fi) Ji+t -r. That is.Ii :O(tirr-l. It is termeda "forward" difference because utilizesdataat i it (Fig.becauseoftheTaylorseriesapproach.lt can be represented f '(.l 8 ) t. f O(-ri+t . r .) exactlythe samerelationship thederivafiveinExamp1.we would if to expectto halvethe errorof the derivative. f ( r i ) + o( h) (4. numericalmethods.. In other words.ri\ : .x. Using Eqs. r i + r ).ri+t.ri) (1.20) f '(ri) : .l9) yields rRr ti+t li : -(ti+t 2! u"(6).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 . * . l ( .(4. the length of the interval over which the approximation is rnade. . we halvethe stepsize.(4.ll)l. ( / i + r:) u ( t ) * u ' { / 1 1 1 ' * .9a). .In fact.4. the error of our derivativeapproximation shouldbe proportional the stepsize.2[Eq. 4.2 was to predictvelocity as a function Recall that the objectiveof both Examplesl.ERRORS 4.21) where /r is called the step size-that is. Equation l8) can be solvedfbr rl I I ( 4 . 13). | - u(ri+r)-u(ti) R1 ri+l ri - {i+l - {i (4r9) . r . by in u(/)./ i ) * R r (4.(l.f\ri+t) /(..3 TRUNCATION 99 jumper..( l .Consequently.F R.l 9 ) ] h a sa t r u n c a o [ tion error of order ti+r .(4.

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.9 finite-difference depiclion (o)forword. {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.

' to yield J'^l(*'\ : rt * . (Fig.3 TRUNCATION ERRORS 'I'his rol forward differenceis but one of many that can be developedfrom the Taylor seriesto approximate derivatives numerically..f'r)(".9c).9b for a graphical representation. r . 4.21) . all the foregoingversionscan also be developed second. whereasfor the central difference.4.backwardand centered difference approximationsof the llrst derivative can be developedin a fashion similar to the derivationof Eq.re. whereas the latter uses valuesthat are equally spacedaround the point at which the derivative is (Fig. l ' ( r i*) l ' ( . are Bockword DifferenceApproximotion of the FirstDerivotive.22)from the fbrwardTaylorseries expansion: J ' ( x i .f(xi-r) (4. Finally. Centered DifferenceApproximotion of the First Derivotive. l ( x i + r ) . f( x i ) .the error would be quartered. and higherderivatives.l ' ( x i f t + ::'t Truncatingthis equationafter tlre first derivativeand rearranging yields (4. For exarnple. Taylor seriesanalysisyields the the practical information that the centereddifferenceis a more accurate representation of fhe derivative(Fig.More accurate estimated approximations the first derivativecan be developed of by including higher-order termsof the Taylor series. (4. SeeFig. The Taylorseries be can expanded backward calculate previous to a valueon the basisof a present valr.25) is a centercd representation the first derivative.9c). 4. is (4.r t : J ' ( x t t. The following sections provide brief summaries illustratinghow someof thesecases derived.9b). r i _ r )2h o(t) (4.t ) .). 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.. * whichcanbesolvcd for .t 2 l ' ( x .For example.we would approximatelyhalve the truncationerror.25) Equation (4.23) wlrerethe error is O(/z). b 2h t't". The former utilizesvaluesat r. for third. r i + r ) l ( .fGi t) . 4.l''(x) : (1.Consequently. .-1 and.22) 1 ' G i )= J (ri) .19).r. A third way to approximatethe first derivative to subtractEq. in as f h f t x i .l(-ri+r) .\ - * " f ( .4. r*i + f f n ' + ' .if we halve the stepsize using a forward or backward difference.

: 0. 4 5 x 2 1 ' 0 x.l:3g"7o/o clifl'erence (a'2-5)l' IEq.2 .r.925 l'(-ri+t):0'2 the [Eq' (a'21)J' Thesevaluescan be usedto compute forwarddifference o'2 .5. Note thatthe the ar -r : 0.1. r a .t. l ' ' ( 0 . Use forward anclbackwarcl to estimatethe first derivativeof n differenceapproximatio of O(h2) a centered J (. So|ution.6a/<.0 f (xi-t): l'2 .I :0. . 4 ' t 3 . -r.t 1 ' 2 usingh:0-25.4 ERRORS AND TRUNCATION ROUNDOFF o Ap Finite-Difference proximotions f Derivotives of approximations o1/r1and clifference problem Stotement.1 0.0 . r r.-5) * difference lEq.5 r .5.l:26.6363281 0. 5 ) =f f Forft : 0.0 .5):-o.0 ' 2 5 ' t.0 . l 5 . the backward /'(05) .5"/o 4.(0. (a.3 .f @) :0.0 ' 2 5 andcanbeusedtocomputethetruevalueas/.925 T EI = l''(0. : 0.25 le : 21.r): .l(ri-r): l'10351563 J'Qi): 0'gZS 3 . l ( r i * r ): 0 ' 6 3 6 3 2 l8 dif'ference' the to whichcanbe usecl compute fbrward 30.0 . 5 . l: 5 8 ' 9 % l''(0. l . 9 2 5 1 .r02 4 EXAMPLE .1 0 3 1 5 6 3 -0.0 le.s) 0. 6 3 6 3 2 8 .0'925 -l : '15 = l e .5 V .l T rh diff-erence' andthecentered 5 0 .5 usinga stepsizeft : 0.'|Eo . \: 2 ' 4 % ' o ca E si c( .4 I the backwarddiff'erence' -0. 1 1: 0 . For h:0. Repeat comprrtation directly its can be calculated clerivative f'(r): .5 : rri+r 1.l-55 0.9|25. and the centered .25 lt.l:9.934 1 1 : f / ( o S \\ : = ' O { 0. 1 0 3 5 1 5 :6 3 11 l''(o. ri t:0 to the functioncanbe employed determine .1 = 0 . 7 5 --1.5) :-l.0 .55 0.5 le.23)1.0'925.25 .l'2:-0. t r .25.

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 .. Besides first derivatives.4. * 1 ): which can be solvedfbr . r i + t )* . r i )_ I G i ) . / ' ( x i t ) hh f ' "( r i ) 1 Thus.): I ( r i + : ) . contrast. [ ( r i ) + . l ( .. t . . r i ) : f f + o f t 2 ) .26)to give . Further.27) This relationshipis called thc second./"(r. l ' ( r i ) I + J'lxi)t2hl '")r''' ./{.1 (ri 3) . .oarl increase or in computations.\ . or in of in In Example4.the centereddifferenceapproximationis more accurate than forward or backwarddifl'erences. ".24)canbe multipliedby 2 and subtracted Equation from Eq./.2 .unl. 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 .l2)1. Noticealsothatthecentercd vcrsioncanbe altemativelv cxnrsssed as l ( . Also. halving the stepsize approximately halvesthe error of the backwardand forward diff-erences quarand ters the error of the centered difference. - . 1 ' ( r . + r ): . 7 ' " 1 t 1 1+ .):ff+o(h) and a centered version . as predictedby the Taylor seriesanalysis.just as the second derivativcis a derivative ofa derivative.4 IOIAL NUMERICAL ERROR r 03 For both step sizes. (a. as are .. Irecall 4.1"(r.26) (4.4 demonstrated that the truncationerror can be reducedby decreasing step the size.rrt* " (4.2 . second the finite dilfbrenceapproximation a differcnce two first finite differcnces is of Eq. Finite-Difference Approximotionsof Higher Derivotives.r. / ' ( r . 1 ' ( .r i ) : ii t) . truncartion the errors aredecreased the roundofTerrors increasetl. the Taylorseries expansion be usedto derivenumerical can estirnates higherderivatives.finite diflerenca.t. r ) . + o(h) (1. / ' ( . l ' Q i + ) . r i + r ) . . h . In the only way to minimizeroundofT errorsis to increase numberof significant the figures of the computer. centered the caseis moreaccurate.f ont. / ( . f ( .2 . r i r ) - As wasthccascwith thefirst-derivativc approximations.general. t il I l ( .2 / ( .. rn+ e r m s o f .4 TOTAT NUMERICAT ERROR The total nnnerit:aL error is the summation the truncation of and ror-rndofTerrors.. (4.)-2. of To d o t h i s .Because decrease stepsizecan lead to subtractive in a cancellation t.we havenotedthat roundoff error may int:rease to subtractive due cancellation dueto an increase the number computations an analysis. r i ) h2 ./'(-tr)*. f ( .

r. One challenge that we laceis our step computation. would like to choose Wc' to determine appropriate size1bra particular an the and a largestepsizeto decrease amountof calcr. shown.f'(.ti r) .28) 2h Finite-difference approximation f d H fr Truncation error h.4. suchsituations relatively uncommon of are a Nevcrtheless. 10.the only error is due to truncation.4. We certaincomputerized numerical EXAMPLE 4.our remedybecomes problem(Fig. 4. we couldconceivably thc errorsonly to discover clecrease stcpsizeto minimizetruncation that in doing so. ./'(t)(6). 4.5 R P c 4.rlations roundoffcrrorswithout incur ring the penaltyof a largetruncation error. l0).If the total error is as shown in Fig.1 (. of bccause its l5.25) l P b R ire . are facedby the following dilemma:Thc strategy decreasing we one of ln of to component the totalerrorleads an increase the othercomponent.r04 ERRORS ROUNDOFF AND TRUNCATION Point of diminishing returns o J Log step size F I G U R4 . do principle"thatplaces absolute certainty an limit on theaccuracy that rnaybe obtained using methods.4.the is returnswhere roundofT error begins to challenge to identily the point of dinrirrishing negate beneflts step-size the reduction. the roundofferror beginsto dominateths solutionand the total error grows!Thus. -I . erplore sucha caseirr thc lollowing section. sometimes occurandsuggcst sortof "numerical they undigit precision.3. 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 step-size to lhe of reduclion. 4. a cornputation. error t i t fbr Therefore.' ---116 (4. if the two functionvaluesin the numerator the finite-difference of approximation have no roundoff error. D E Thus.): True value J(ri+tl -.1 Error Anolysis of Numericol Differentiotion in As described Sec.a centered difl'erence approximation the first derivative of can be writtenas (Eq.to I6WhenusingMATLAB.

l 5 . themaximumpossible valueof M. E X A M P4 .ERROR 4..28)gives these errors. /(.'r a.r : 0.Recallthatthe truevalueof the derivative -0.) . r r .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.€iwill be 2e.ue=df unc (x ) . Further.l lxit: f(*. h-1. f ( x i + ) I e i + r roundoff where the . D(1) = ( func (x+h) -func E (1 ) =abs (dftrue-D(1) (x-h) ) ./'t_r.the function values do include roundoff error as in -t ei-t . we useda centered fbllowing functionat. how roundoffbecomes of I 0 to demonstrate by a f'actor is your results Eq.-5. [ 1 r .rl 2h Pi t . valueof thedifl'erence upperboundof e.E L5 .e the rounded function values and the e's are the associated valuesinto Eq. becausewe are using digital computers. n) format df t r. H ( I ) =h. We can develop following M-tlle to perfbrmthe computations plot the as derivative arguments: that we passboth the functionand its analytical Notice results. Then progressively dominantasthe stepsizeis reduced.4 IOIAL NUMERICAL r05 However.9 125. . f ( . to Relate and the Solution. r a .-: " 6 True value Finite-difference approximation RoundofT error Truncation error consistsof a roundoff approximation We can seethat the total error of the finite-difference with stepsize.4. . 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 | . _ ..0 . t'.30).r i + r ): .r. n I G.f(xi t) : f @i.ri t) . 5 1 2. l . (4. (4. x.r ) .29).0 . 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. 2 5 x 1 . In Example4.settingthe result by An optimal step size can be determined diffbrentiating equalto zeroand solvingfbr ('1. of the O(h]) to estimate first derivative the -l t ' G ) : . funcLion di ffex 1ong ( func. (4.r1A./'l'(€).0 .29\ Eq. 2 divide the stepsize Peribrm the samecomputationstartingwith ft : l.o . / ( x rr ) < _ + _ h6 l.Substituting ?t. cLfunc. ) / (2*h) .

1 50 . =[ l | L)' E' I ' .15*r^3-0.0f)000000 t 0 0.r0 6 AND TRUNCATION ERRORS ROUNDOFF i rr i . s t a r t i n g a t / z : 0 . 0 .First. I t l f l r r ( l 0 L l 0 r ) ( l 1 . gra. rninimum A t'.10f lprintf %]5.u..> -> f f=G(x) c 1 f = ( .I I.5 ) l . ('%14. 0. O(r()()000(l() I Q.0. . H o w ' c v e r . r _ l0 0 0 0 0 0 0 0 0. 1 * x ^ . r . end 1 .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 .')12 500llr00000I i l . . t4. 1 r j. . .!*x^2 0.1 . (r x ) L l. D(r1= (func(x+h) (..Sr-rbstituting bc into thescvalues g E q .. r .29). j 9 ! r t r ' .sc afc dcalingw'ithan easilyclittbrcntitble w'e l'unction.J0000075ir006 0. czur wc also investigate whctherthescresults consistent arc with Eq. ( r ( ) 0I ( l o L r ( ) i ) r l l ) . . l'irst. l l ) f :tr--p :.) .14f \16.r 0 0 ( .0(l 10000{)(r0 A. 0 ( ) ( l l r 0 0 0 L( l o 0. r l L . !.. n h = h .2 .1 0 . 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 .rr)( 1 0 0 t r r . l a . > : ' c l i f I r . 0 ( j t ' r ( ) t l L l t ) u t .the crrol irrcrcascs roundofTclominates. 1 r .2. as Becuu.0il(l0l 5000000t) ) 0. 0 r ) [ )( ) 0 t )0 U 0 1 . [ )( ) { ) 0 0 U1 ( )( ] i ) 0 .-5 x l0 r('.l4 9 t j c ) ' ) 4 5 ( r l I q 12 5 O ( t r ) (I i 1 6 0 ' r 0.lif\n'.3 x l0 (' ('ohtuined withMATLAB. ) !.45*_r^2 x (1.y Label ( 'Error' ) rt l . 2 .izc f inite c ii 1 f e t .) 0 1 :0 ( r i l0 t l 0 0 0 0 ) 0 . H(r)=5. r .25*x+1 . w e s e e roundol'f'error bcginto crcepin andcrodethc rateat whichthecrrordirninishes. 0 0 0 ( 10 0 0 f { l ( l I . ! ) l : 5 ( ) L r l r { ll . l l l t ) ( ) 1 0 . 0 0 ( ) 0 ( ) ( l ( ) l l 0 r ) | 0 . in Eq.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 . 0.500u t 9 98q44 U. q 0 .912.0r100000000054 0 . 1 1 0 1 1 6 6 3 0 ( )1 3 6 1 l 0.2b.) o. (4.0000 10 0 r ) r J 0 0. xl abel ( 'SLcp Si zc' ) . i r .4.9 I 2!00(l i 'rb(10!tl 0 . I r i .r'uLr error I . h) ) / (2*h\ . o r \ / c r . r 9 3 t1 0.91 2!t)000000b4. U(r0t0Ol!t)000t) 0 . 0 0 0 0 0 10 0 | 0 ( l .30). 2 1 1 0 . { i ( | ) 1 ) { ) ( ) ( )3 ) i .') IormaL sholf errol'\n'). !ll2 5J i00000011(r ll. 3 0 ) i v e s l0r(') :4.l(il uF func(x | )). ' 1 . thc rcsLrlts asexpcctcd.M : I l ' t ' t ( 1' -.L) .( 4 . 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.0000000199894 0.. tf h upperboundorl rounclofl'would abollt€r:0.-.00utJ000000112 0. fprincl(' finite diflerenr-e srt-rp siz. t l 21000rJ00 l l I ! 0. . in foundol'f minimalandthe is arc At Hcnce. at crror is reachcd /r: l0 Bcyondthis point. As clepicted Fig. l .r1-1e('Fla'! The M-flle can thenbc run usingthe tbllowingcommallcls: .: t1. ( ) : 2 . l ogLog (H. 4 9 / 0 . x (f f . E) .n c c t. 0 0 0 l .4*x^ i 0.the totalerrordrops r'stimatc dclrninnted truncation is by L-rror. t t 2!. rS L e p Stz.l . 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 .. 1 0.

rnost approxitnations unnecessary.4 Step size 4. course. Prediction total numerical of erroris very complicated foreven rnoderately sizedproblemsandtends be pessirristic.4.Sometimes you canrearrange refbrmulate problenr avoidsubtractive or the to cancellation.Thrsavoidslossof significance. Beyond thesecomputationalhints. many ciises In error estimates based the expericnce juclgnrent ale on and o1'the engiueer scientist. of Theretbre. there are severalpractical programmingguidelines can suggest.bya /e see imum rtes. cases. one can attemptto predict total numericalerrors using theoreticalformulations.2 Control of Numericol Errors rd the . itigate uating practical Forrrrost with rnethods. to Theretbre. it attempted only srlall-scale : of the es into .4.4 TOTAT ERROR NUMERICAL r07 FIGURE I 4. you may wirnt to use extended-precision Furthermore. is bestto sortthe numbers numbers andsubtracting it and work with the srnallest first.8 1 0 -1 0 10 8 10-6 10 . this is not If possible.avoid subtracting two nearly Loss of significance equalnurnbers. do not know tlreexacterrorassociated numerical we is which makesour numerical The exception. when adding arithnretic. is usually for tasks. or Although error analysisis to a certain extent an art. for engineering scientific and applications we fbr o1'the mustsettle someestimate errorin our calculations.4 10 . whenwe know theexactsolution.I Plot of error versus step saze 100 1o-2 10 . almostalwaysoccurswhen this is done.The Taylor seriesis our primary tool fbr analysisof such errors.6 t! 10 . we First and fbremost. numbers. and numerical Thereare no systematic general approaches evaluating to errorslor all problems.

exAn ampleof a negligible moclel erroris thef-act thatNewton'ssecond doe not account law s fbr relativistic This doesnot detract eff'ects.3 For computcrs. with most of thenuol'error are not dircctly connected Although the following sources havegreatimpacton the success a of mericalmcthodsin this book. 4.therearc usually sirnpleways to check ln whethcra particular nunrerical methodis workingproperly. AI Er i. tt . Throughout lhis book. parfor lrabits usetul ticular.1 Convert thc lirllowi l0l l00l and| 10. MODELERRORS. they can sometimcs techmodelingeflbrt.s. This is nodoubt disr-egardeddiscussions are extentunavoidable. Blunders usually are in ol'rrurnerical rlethods. I zl. blundcrs. This may involvethe ussof two or morc indepcndent problenr that theirresults canbe compared. dueto thef actthat.conornic ramiflcations. n. lle th hit es ifr Ltl\ 4. and tional strategies.(X)10 4.UNDERS.andmostblundcrs impert'ection.5. theearlyyears or In ol'conrputcrs.3 areextrcmely mitigatingprogramnring blundcr. is appropriate takespecial lossof humanlit'eor havescvere groups solvethesame to precautions. to Wc will leavethcseinvestigations spccificsections.srelatcto biasthat can be ascribed incorrrplete to mathu'matical rnodels.1 Blunders We areall f'amiliar with gross errors. may employsensitivity the or when we changernodel parameters input values. Horve we ver. so of irr ancl Thc rolesof errorswill be a topicof cclncern analysis all sections thisbook. Thus. they must alwaysbe kepl in mind when applyingnunrcrical problcms. of principles and of fundarnental and by the carewith which you approach designyoursolution to a problenr. fiorrrthe adequacy the solutionin Exanrple of l. based dit'terent arc cal algorithms propcl'ties stabilitycharacteristics. thissource ol'ernlris highly unlikcly. Such experiawareness cor.thegoodplogramnring tlratwereoutlinedin Chap.2 Model Errors Model error. rnustbc attributcd humar to Today.nputatiorral of errorsand possibleill-conditioned the with a difl'erent step size or methodand mentsmay involve repeating computations analysis seehow our solution to changes We comparing results.2 Convcrt the lirlloui 7 I 563 and 3. to your to Finally you shouldbe prepared pertbrm numericalexperiments increase to problols. PR be Ju as ca ca w( 4.try aswe may.wc discusswaysto chcckthe results nurrerical of calculations. er tn PROBTE 4.ues thc contextof real-world in ist lo As th 4.t drr r\o\'td .rnistakes to a certain of can In believe thattherearea number waysin which thcir occurrcnce bc nrinimizecl.r.5.5 AND DATAUNCERTAINTY BI. addition.l S t e pl : S e t r : I . erroneous numericalresultscor-rld sornetimes attributcd rnalfunctions be to ol'the cornputer itself. 4.t08 ERRORS AND TRUNCATION ROUNDOFF and try to estimate The tendencyis to pnsh forward with the numericalcomputations be the accuracyof your results.hernltical rnodeling and canconThey can be rvoicled <-rnly soundknowledge by tributeto all the otherconlponcnts err<lr. Step 2: Il' I + e is less th Otherwiscgo to I Step 3: r' : e/2 Step4: Rcturrrto Stcp2 Step5: -1x€ a . haveditferentconvergence or are When the resultsof numerical computations extrcmelycritical and may involve it to c.This can sometimes done by seeingif the resultssatisfy to as be the back someconditionor equation a check. nic.thc thought ol' as lhc srnirllc givcs a numbcI grcrtcr th idtr ca1\.We may want to try different numerion computathat havedif'tbrent theoretical foundations. process Blunders can occur at any stageof the rnat.Or it n'ray possible substitute results into the originalequation checkthat it is actuallysatisfied. ntc ol.

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 . hiascd. are Measurernent by thc errors can bc quarrtil'icd sr-rnrnrarizing datawillr oneor morewellregarding specil'ic choscns(atistics convcyas much inlbrrnation possible lhat as character( statistics most olien selected represent I) the isticsol' thc data. the nrostpilrt.cro rnd a nurnbcr than l. as in Eq. would flll taster Thcsccrrorscancxthe parachutist duringsome hibit bothinaccuracy irnpr-ecision. If suchwere the in case.Each valuc in 5 : t= 2 x t is storcdin :1 hytcs with I bit lor thc sign. can studynunrcrical wc errorswitl'toul crlrlplicating PROBTEMS Writc your own M l'ilcbascd this al-uorithrn dctclnrirrc on to hy it thc nrachinc psilon. d r . ( 1. l o w zl. rcspcctivcly.theyprovidca rnsasurc ol'Lhc biasandirnprccision. 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.On thc otherhand.indepcndently can Thcrelilrc. thesc conditir)ns. suppose airresistance not proportional thesquare that is to ofthe fall velocity. ns Uncertainty bc jumps thanduringothcrs. ( X ) 1 0 1 .PROBLEMS r 09 scales with the br-rngee because theseerrorsare rninimalon the timc and sDace associatcd jumper problern.no numerical method will provideadcquatc rcsults. 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. arcdcalingwith an inaccuratc. rlost of thishook. anduncertain clata. we arcdcalingwith a qr-rcstion ol'prccision. boththeanalytical numerical and solutions obtained Chap. the if the measurements ranclomly high andlow.I wouldbe erroneous beYou should cognizant that.we will assr"rmc wc havcnot lnadcgross that ol'thcsc errors. r r . ftrr we nrodel. wouldundoubtcdly associatcd thcsc with mcasllrcmcnts. will retLrrn Wc to (hctopicol'charactcrizing r-rncertainty whenwe discuss regression PartFour.but is relatedto velocity and otherfactorsin a diffbrentway. have a souncl l'actors. r-rroclcl errors. Forcomputcrs.MATLAB allows 3:t = t:12 1:Return Stcp2 to nuntben lo bc cxpresscd single prccisiolr. y 4 .5. .5 Although it is not conrrnonlyuscd. r I m i r . in dala Althoughyou mr-lst cognizant hc o1'blundcrs.7). 3.i ln u r t c t i ort.ifyou are cause ofmodel error.1. r l i r i n .and wc are dcalirrg with crror-ll'cc rncusurcnrcnts. instance. Bascyour algorithrn the notionthatyoLrr on cornol as thc smallcst nurnbcl that whcn addcd to onc grcatcr huscdon this putcrwill hc unablcto rcliablydistinguish bctwccnz. rnachinccpsilon c can also hc thc . 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. c o d ca n c t h o s c h t a i n c d i t h r r . instmments and If our consistently underestirnate overor wc or dcvicc. be ofthis typeoferror andrealizc model.3 Dofq Uncertointy in Errorssomctimes enterinto an analysis because uncertainty thephysical of dataon which jr"rmper modelby hava rnodelis based. 23 hits li)r thc Convcrt lirllowing basc-2 nurnbcrsto basc l0: thc l 0 0 r n dI 1 0 . . workingwith a poorlyconceived 4. Undcr en'ors.t1 r r . l Convert lirllowing basc-l{ nurnhcls to basc l0: the and 1. nuthe lilr rncricalrncthods uscdfirr bLrilcling nroclels bc stndicd. takingthc base logalitlrrn ol'thenun.5. o o n o o As such.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 . An algorithrn l . estimate velocity.rhcr ratcdby your' to go Olherwisc to Stcp3. However. 1 . 4 I n a l n s h i o r s i n r i l a lt o P r o b . For suppose wanted testthe bungee we to jurrrps his ing an individualrnakerepeated andthenrneasuring or hcr velocityalicr a spccrfied time interval. thcn go to Stcp.

f (Eq.2. thc oI at .{ r"-Ii-tl-r l.37. have wc t0 a s o l v ct h c l u n c t i o n l(r'): t .1. zl. . v a l [ 2 .l-digit arithmcticwith mation.35 the in cstimate second dcrivativcof'thc lunctionexamined .l. is Craphall thrccI'ilst-derivafive ing to two signif icantI'igures. .Usc 3-digit arithrnclic your results the hasisol'thc remainInterprct dcrivativc.21 Repcat 4. for eachapproximation.l.t l .r. s i n ( z / 3) .r if'l(-r)= urz+hr+c.12 Use zero.2.cascol'thc Taylor sclicscxpansiun 0 ancllr : .r : 0..5771 Try it using 3.r _rr' 4. Exanrple at 151 .88 mantissa.I I I is exactfbr all valucs of.tro ERRORS ROUNDOFF AND TRUNCATION 4.Altcr cachncw lcrm is acldcd.5. ( 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 . Note that the exponcntsrangefrom .3xr): is givenby ot the usinga bascpointat. 4. 4.^ Lct thc bascpoint bc u: xi : rf2 <tnthc intcrval[0. Compute truepercent relative error c.7).7Thc derivative l(.r-o. 1 7 l l l . firl each appsoxiDo you cxpectto havc diff iculticsevaluating Discuss rneaning thc results. 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 ' ( . a d dt e r n r s n e bctwecn w t o (H c x p u n s i o n . U s et h c l i r r w a r db a c k w a r d . I . tlris Ir-rnction Computc the true percenlrclativc cror r.15 Usc lirrward rnd backwarcl dillirencc approximations chopping.-1. r l < I i t i s k n o w nt h a t -rl .r * 8).r.ell as and largcst as the machine epsilon lor single precision represcntation. 12. 2 1 w i t h l : 0 . cxpansion the sin -r to cstirnatc lbr use thc Maclaurinscries 4. Pcrlirrnr evirluation .rr-7-r:+u. 125.Intcrprctyoul rcsults the on n l a t cd ' : tcnr ol'thc Taylol scricscxllunsion.: 0.r.1.tcalculnloror MATLAB to dc-lcrrninc Usc _vour thc centcrcdllnitc dil'lcrcnce approximations lhc firstand lirr valucol'thc approxvaluc.25 and0. (a) Provc that this MaclaLtrirr \criL'\ rr\piln\irur a spucial is (Eq. on thc perccntrclativc crror. . and thirdortlcrvelsions and contputc lr:r lirr cachcascthc I -1. Compare rcsultswith the true valueof the with r:hopping.l0 lirr this scricsli)r -r : 0. 10. (4. i n t :S o l v cg r a p h i c a l l y . but finitc di{l-ercncc as anddo thc samelix lhe sccond as clcrivativc wcll.zl. l.r 0. to predictl(2) fbr f(-r) : In "r'usinga basepoint at. yoLrr h : 0.l Usezero-throughfourth-ordcrTaylor serics expansrons (I . approximations along with the theoretical.r .\': 2 usingslepsizes 12.\ = n 16.r -l.ll Perlbrm thc sarncconrputation in Prob. .10The Maclaurinscricscxparrsion cos-r is lirr ll R r: i' t 2.05.Add tcrnrsuntil thc ahsolutc trLrc scconddcrivativcs as to graphically so whichapillustratc imater-rrorcstimatcI'allsbclow an crror critcrionconlirrmproxinration mostilccrrlltc..1.l.r) = l/1 I .: 1.l(3) firr + I (.-15 Prob.llJ To calculatc planct'sspacccoordirrates.r) . 4.throughthird-ordcrTaylor sericsexpanslons to pre'dic:t. but lbr l( r) : cos(.3rt).2.rf -r(' . .+ .4. computcrin Exarnplc provethatthe rnachinc epsilonis 0. thc at with part (a). I .16 Usc a ccntcrcddillercnccapproxirnation 0(h2)to of : ((.Evaluato clcrivativc : 2 usinga stcpsize the at. sccond-.r].5sin. dcr term ol'lhc Til) ltrr se (a) (b) Rcpeat hut cxplcssI'as .lI n'.I-0.6 For the hypothetical base-10 4.21 3! 5! 7t l'crence ). 6-r 4.126to 127. l3) with . basisol'thc rcrnaindcr 1 . Evaluutc clror and cornpare the Curnparc your cstimatcs with thctrue ol'/r .r = l.rl .4 on the intercornpLrte thc truc and appr0xirnatcpcrccnt rclltivc crrors.r.f.30).2(l Dcrivc Eq.l ProvethatEq.1. 6! ttl Thc error is cc. r : l .0 15 on thc specil'icd C(r\\-l I I ) 4'. a n d .l'irst-. 4. pockr. 19 Consitlcrthc lirnction l'(-r): -rr .5.r' Prob.(4. of to ctfO(h) and a ccntercddiff'ercnce apprtrximation O(ft2) l.tt (a) Evaluate polynornial thc cstimalc thc l'irst dcrivativc ol' the firnctioncxamincdin I:. Dctclnrinclhc highcstolclcr'l-aylor scricscxpansion resulf .0.6-12 7x ..r.9 Thc lirllowing infinitc scricscan hc used to approxivaluc ol'thr' sccontl dclivativc. ) at a tinrcto estirnate cos(z/3). rir'scxpiln)ii{)r). ..r: l.+ Sln. but lirr thc torwilrd divideddif: -\ . r )c ' a t . '1.Determinethe smallcst positivefloating-point numbcrs u. 2 5 .ualto thc absolutcvrluc ol'thc dill'erence thc givcn lunction and thc specilicTaylor series .2. Enrploythc zcro-. r i * r : I l t : lirr. Evaluatc c>l' irt .r) : 25-1r.rS intcrval.and 8 bits fbr the signedexponent.22 RepcatExarnple4. ing in a rnaximutn crnrr ol'0.1 o\ Yea to s The sen time RcpcatProb.and .

(PT2.anotherguessis made.l). of Although the quadraticformula is handy for solving Eq. The valuescalculatedwith Eq.2) :.2).:.therewere a numberof ways to solve for the roots of suchequations.as the with Eq. The processis repeateduntil a guess results anfir) that is closeto zero. rootsare sometimescalled lhez.. in ltl ' :. This point. onlv alterthe native is an approximatesolution technique.1 ovERvtEw Yearsago.If not (as is almost always the case).2'l equalto zero. of they are limited because their lack of precision. ln suchinstances. . you leamed to use the quadratic formula *b+4F-4ai (PT2. Before the adventof digital computers..eros the equation.there were. of An alternativeapproachis to use trial and error.2). is the root. tPT2..For this reason. This "technique"consists guessing value of x and evalof a uating whether/(r) is zero.many more that could nol.and/("r) is again evaluated to determine whether the new value provides a betterestimateof the root.For some cases. roots could be obtainedby direcf methods. (PT2. there are many other functions for which the root cannot be determinedso easily. which represents the r value for which/(x) : 0.1) are called the "roots" of Eq. (PT2.. Although graphicalmethods are useful for obtainingrough estimates roots.Rootsond Optimizstion 2.1) LU to solve fk):qf+bx+c:o (Yrz. (W2. One method to obtain an approximate solution is to plot the function and determinl where it crossesthe r axis.They represenl the valuesolx that make Eq. Althoughtherewere eQuations like this that could be solved directly.

secantmethods.computer solutions.rolr.ton-Raph.In contrast. whereits derivativeis zero.ation.most practicaloptimizationproblemsrequire nunrerical.1.suchsolLltions be obtirined analyticaltyby deoptimi. can terminingthevalueat which the functionis f'lat. fbr involve guessing searching a locationon a function. are Thatis.l'('r) = 0 f"G) > 0 PT2.Thesemethodsstartwith guesses reducethe width of the bracket.From a numerical standpoint. Graphicalmethodsare usedto provide visual insight covered:bisectionand. and .f"(r) < o f(x) = o . level of precision. solutionof most appliedroots-of-equations Besicles roots.I FIGURE the A funciion o singlevoriobleillustrciing difference belween rootsond oplimo of methodsareobviouslyinefficientand inadequate the requirements Suchhaphazard for of engineeringpractice. As elaboratedin the to following pages. We illustrate severalopen methods including theJixed-point iteration.rt2 PART ROOTS 2 AND OPTIMIZATION l'(.Chapter5 focuseson brackthat bracket.r) = o . 2.anotherfeatureof lunctionsof interestto engineers and scientists its are minimum and maximum values.such numerical optiwe mizationmethods sinrilarin spirit to the root locationmethocls just discussed.false Error formulationsare developedto help you determinehow much into the techniques.thatis.The detennination suchoptimal valuesis ref'en-ed as of to As you learnedin calculus. also involve systematic trial-and-error bracketthe root.the combinationof thesesystematic methodsand computersrrrakes the problemsa simple and efficient task.or contain. eting methodsfor finding roots. fbr searching the functiorr'sextremepoints.The fundamental and difference both betweenthe two types of problemsis illustratedin Figure PT2.Numerical methodsrepresentalternatives that are also approximate but employ systematicstrategies home in on the true root. Nev. Root location involves for optimizationinvolves searching the locationwherethe functionequalszero.Two specificmethodsare the root and then systematically position.2 PARTORGANIZATION in The first two chapters this part are devotedto root location. computational effort is requiredto estimatethe root to a prespecified Thesemethods Chaltter6 coversopenruetltods.Although such analyticalsolutionsare sometimes feasible. We will discoverthat iterationsbut do not requirethat the initial guesses efficientthan bracketingmethodsbut that thesemethodsare usuallymore computationally they do not always work.

Chap. After describing and illustrating fmlnbnd.we discuss robust. the last part of the chapterprovidesa brief descriptionof optimization of multidimensional functions. After illustrating how f zero canbe usedfor engineeringand scientificproblemssolving.which again is attributecl Brent.for finding the optima of a function of a single variable. hybrid approach golden-section Then.f zero. pafticular. a that combines search and quadraticinterpolation.First. the chapter problems endswith an exampleof how MATLAB can be employed solveoptimization to in engineering science. it fbrms the basis fbr MATLAB's root-finding function. describe In we MATLAB's excellentbuilt-in capabilities this task. for goldenwe two bracketingmethods.As such.2 PART ORGANIZATION rr3 S L ' Following the descriptionof theseindividual open methods. and K- n. re nt )h or at at nt .This approach.we then discussa hybrid approachcalledBrent's root-findingmethodthat exhibits the reliability of the bracketing rnethodswhile exploiting the speedof the open methods. 6 endswith a brief discussion special of methodsdevotedto finding the rootsof polynontials. describe sectionsearchandparabolic interpolation.2. Finally. fbrms the to basis for MAILAB's one-dimensional root-finding function:fminbnrl.The emphasis on describingand illustratingthe is use of MAILAB's capability in this area:the fminsrearch function. Chapter Z dealswith optimization.

' .. objectives topicscovered Specific and are ' ' .l Fundomentol Principle Heotbolonce Mossbolonce Forcebolonce Energy bolonce Newton'slows of motron Kirchhofflows s De Vc T^.r i r n h.2-5 kg/rn.r R CHAPTER OBJECTIVES Thc primaryobjective thischapter to acquaint with bracketing you of is mcthods fbr findingthe rootof a singlenonlinear equation. the orl Cu tl4 . . Knowing how to determine root graphically. You know f}om your previousstudiesthat the fbllclwinganalyticalsolutioncanbe usedto prcdictfall velocityas a functionof tirne: lntf^t ' r ' ( r ) : . | rt_B-lE Fund3 s. fbr Understanding lalsepositicln how it difl'ers and fiom biscction. Your bossat the bungec-jumping 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.ion rnethod. 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 liee-1all significantly velocityexceeds 36 m/s afier4 s of fiee fall. / i . you cannotntanipulate equation explicitly solvefbr rn-that is. Llnderstanding incremental the search Knowing how to solvea rootsproblemwith the bisecl. Knowing horvto cstimate errorol'biscction the why it difl'crsliom error ancl estimates othcrtypesclf root locationalgorithms.. l : . Cc of Mc ofl Ch AC( V t.r \V rtt / rsrr Try as you might. a methodand its shortcomings.\ Roots: Brocketing Methods N et rf 5. what rootsproblems and wherethey occurin cngineering Uncierstaniling nrc and science.

:.g{i.n. 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 .cr. If the pa. 5.n o r l r a q a r"f /ro<r<fnnr a opollronra induclonce) . 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: :.'Jrt dtpendtrt. That is.'.l n r . This chapter you to how will introduce the computer usedas a tool to obtainsuchsolutions.r.scribed fbr period. as posed at the start of the chapter.-. However. 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 .dpN. cannotbe solvedexplicitlyfbr nrass.:':.:r. is 5. or An examp\e of such a mode\ is the equationfor the bungee\umper's ve\ocity.:'j.we call thisa "roots"problem.un(..1*. torii. it is isolated one sideof the eqLral on sign. mathematical in equations modelsderivedfiom or theseprinciplesare employedto predict dependent variablesas a function of independent rurrr. Table5... syslem(leomelry . ::] lorts ol ewlons )l of rchhoffiorrys s p o t e n t l oe n e r g y l Acce/ercllion.:.:. cases.).i.::':r. r(m) (5 . parameters. r'*i.set in tinre ma.Hence.'. or ocotron Currenlsond vo/toges T r m eo n d p o s r t i o n Lme qeometry Alc-rss molerrol syslemgeomefry.^\'::-^\j-'-::-'. ..ss a jumper with a given drag coefficientto attaina pre.'-'. is said to be irrrnlicit.AlthoughEq.|i:.F') -.it)rl Poromelers Itei.):l:.roots of equationsfrequentlyoccur in the principles areaof design.'i:. 2 1 Now we can seethat the answerto the problem is the value of ru that makesthe function equalto zero. 1. such arrd /r.1) provides mathematical representation the interrelationship of a In it amongthe modelvariables parameters. whereasthe pararneters of represent its propertres composition.suppose that we had to determinethe velociryt a .rm'-t--rs.rsi.m o s sl r o n s f e r .-'.veloci\'. As introduced Chap.' ) S 1 -:.rlh'i. I listsa numberof tundamental thatareroutinelyusedin designwork. \ote \hrt \r.ables ret'lectthe stateor pertbrmance the syster.b\t:.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 .r:'.'s. t principles Fundomentol usedin designproblems Dependenl Vorioble Independent Vorioble ine CtC pOS. dissipoiive of potcmerers F l o r t r l r' . (5. s y s l e r n r l D0iOriCe bolonc-.'.-'-\-:.'.I G 5 .:.-\..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.i\'..

11-r). q c ). ' 1 r. . v '. l l 1 .Thisvaluealsorepproblem. The solutionto thc dilemmais provided numerical To solvethe problemusingnumerical methods. a n l r ( : . 8 1n r / s r . 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 . l ( r l : 0 .raxis.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 .qrr11 (.spararneters) ensure ol of by to thatit perfornrsin a desiredmanner(as represented its variables). is conventional reexpress (5.). I f ) = r i q f L ( r 1 * m 1 rc : c 1 ) * t . resents rnass thc that solVes clesign the fbr The lbllclwingpages and graphical methods deterdealwith a varietyof numerical rniningrootsof relationships suclras Eq.25 kg/rn to havca velocityol'36 m/s after4 s of free i 1 a l l . J * c d . theseproblemsoften reqr-rile determination implicit parameters.\./(r) :0 is to make whercitcrosses a plot ol'thc firnctionant'l observc thc.r.Thesetechniclues be appliedto many can in otherproblcn.rs conl'ronted ror-rtincly cnginecring science.2).2). by Thus. I LE TheGrophicolApprooch to the Problem Stotement.2. plot (rrr. of The subtracting dcpendent the to valueof'nithatrnakcs. and 5.3 "50 150 I I] e .1)by it to Eq. Ho ma un me ab pl( Cv !' ro ar ha Fc tr rh te -1 -z pr u ri 5.f(m):0is. (-5. the of tbr by methods rootsof equations. variableu frornboth sides the equation give Eq.therootoftheequation.3 pro cal t ans whi par 5.which represents t h e . a o wh E X A M P5 . (5. / n l r ) * L ) .because a properties cornposition a system(as represented it. p r o v i d c s r o u g ha p p r o x i r . q r 1 ( . '\6. Use thc graphicalapproach dcternrine nrass the bungee of jurrper with a drag coel'l'icicnt ol'0. rv a l u el b r w h i c h . 1 .. r ol m M s S o l u t i o n .nate the root of the ecluation of .2 METHODS GRAPHICAT A sinrplenrcthodlbrobtainingan estir.thercfbre. . 2 )v e r s u s a s s : '' ' ::' r'ril = nlll '' I = 4.This point.( 5 . n a t io fnt h er o o t .tt6 ROOTS: BRACKETING METHODS many designproblemsinvolve specifyrng the This represents real dilemma.

lherearecases wherethcy do not holcl.i5)*t. .. (S.The process . and METHODS AND INITIAT GUESSES 5.4 ) .5 .rr)and . tt r r u l t i l t l c r t t . r./ wherea singlcroot is brackctcd ncgative positive he r f e v e r . Visualinspection the plot thc providesa roughestimate the root of 145kg (about320 lb).r ' ) ( r .r. / (. thc havethe sarnc sign. Althoughthcse l u ) F o r c x a r n p l e . l r l . and Canale. that Although they are bcyond the scopeof this book.TheseestiHowevcr. It can also be checked by substitutingit into Eq.Figurc-5. (5. ) a r e a l s o o n o p p o s i t e s itd c s.2 of of guaranteed locate the rootsin an interval. 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 . makeguesses until the function error" to come up with the root.sqrt.2 ) ( . confl'onted tions are extrerrelyusefulfor solvingrnanyproblerns scientists.r .whcn proof computer algorithms to all in with graphical approaches.graphicul interpretations usefirl are Asidefiorn providing ol'the lirnctions and anticipating pitlallsof the nurnerical the understanding ploperties the rrethods.1 showsa numberof'ways in which rootscan occur (or be prescribcd a lowcr bound.oa x i s . For cxample.(g*cd/1. valuebeciiuse thcy arc not vcly precise.2)to yield >> sqrt (g*145/ccL)*r.r r ) a n d . ) ol'rootsbctwecn values.l ) along with the parametcr values liorr this exarnple to give ( >>. F i g .r1 lb by andan uppcrbound.. thcrc rootsoccurring within the interval.2 ) ( x .. / ' ( .3 BRACKETING you'd oftenbe told to use"trial and If you hada rootsproblemin thedaysbefbrccomputing.thereareeitherno rootsor an evennunrbcr gcncralizalions usually arc lrue. N o t i c ct h a t . of by arean odd number rootsin the interval. 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 .3 BRACKETING tt7 of The functioncrosses llr axisbetweenlrl0 and l50 kg.) As la r'. expressly designed locaternultipleroots(Chapra to in nrakes difiicult to dcveloptirolit The cxistencc cases thc typedepictcd Fig. Howevcr.r. 5 . are Craphicaltechniques ilf limited practical ol'roots. r: 2 t n a k c sw t r . 2 b )c a nv i o l a t e h e s c r i n c i p l c s . rnethods the described thc lirlkrwingsccusedin conjunction routinelyby cnginccrs.-5.f (. .0456 which is close to the desired fall velocity ol-36 rr/s.M a t h e r n a t i c a l l y .0456 which is close to zero.) v 0.5 . thereare specialtcchniqr"rcs arc 2002).METHODS AND INITIAL GUESSES 5. w h e r ( . (q*14filco) *t. general.-5. 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 .Fig. r a x i s( F i g .anh(scTrt-q*cd/ r45) *t) 35. mates can be ernployed starting as firr roughestimatcs ol'tlreroot. r : 2 i s c a l l e d' . 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/ ( .. The validity of the graphiof can by it cal estimate be checked substituting into Eq. indicatcd Fig. ancl il' l (rr ) and./ (-r. Howpictsthecase by and values o1'.. dcabsent) an interval in (r). -5.anh(::qrt.graphical methods can be utilizedto obtainroughcstimatcs gucsscs numcrical in lor mcthods discussed this chapter.Thiit is. appliedrnathematicians. you'd repcatcdly wasgrea(ly lacilitated the advent sofiware by of was sufTiciently closeto zero.

5. .n< iho'c ore FIGURE .) 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 .ile s..either .nn Jho ./(r. there wili sign.o^./ (rr) ond . For thiscose.. ". thoa^ . .I 5 I lustrolion crnumber generol of woysthoio roolmoy of prescribed o lowerbound ond by . of for on even numberof oxis inlercepiions the inlervol. e r e i l l n si o be on odd number roolsin theintervol.ol lnr nolarmrn.tt8 ROOTS: BRACKETING METHODS xl (d) xu (bl FIGURE 5. 1 (o) Multiplerootsfhot. [^.2 lllustrotion someexceptions lhe generolcoses lo of when depicted in Fig. occurrnon intervol (oJ thct on upperbound Poris ond (c)indicoie if both x. 'nh lnc e"d nninls nr'p rf nnno. (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 .. or[o o ' s . S p e o l s t r o ' e g i e s ^ro .^^r.r.. is ol'h.occur the function tongentic to the -r oxis.-..o.

.. open to In the rnethods not alwayswork (i. good initial guesses may not be obvicontextyou are analyzing. the are ' This lirnction rs a rnoditledversionof an M-lilc originally prcscntcdby Recktenwald (2000). the eventthat a sign change occurs! upperand lower bounds storedin an arrayxb.A f or: loop is usedto stepthrough set eachinterval. . The following ous.4).3) rre site lies thenthereis at leastone real root between afld. 5.theseapproaches as require Then they systernatically home in on the loot in an iteraan initial "guess" to get started. section describes suchapproach.3 BRACKETING METHODS AND INITIAL GUESSES lr9 tools suchas spreadsheets.there is a possibilitythat closely (Fig.1 Incremenlol Seorch you observed in that /'(.A potentialproblcm with an incremental where the functionchanges length. automated approaches obtainguesses to would be useful.3. 5.) haveoppositc < /(-r1)/'(x.r1 by Ircrenrentalseun:hmethodscapitalizeon this observation locatingan intcrval scarchis the sign. 5.they typically take more iterations home in on the answer)./(x) is real and continut'rus the interval of if in from x7 to.r) changed When applyingthe graphical technique Example-5.but when they do they usuallycondo vergequicker.r7) signs. An M-file can be developedr implements incremental that an search locate roots to the within the rangefrom xmin to xmax (Fig.it is preferableto have methodsthat come up with the correctanswerautomatically. suchtools can actuallymake the trial-and-error approach attractivefbr someproblems. But. Thesemethods involveone or moreinitial guesses.lf ns is ornitted.3). are on either sidc of the root.in othercases. They are . incremental one the search.5. are Thesemay naturally ariseliom the physical In bothcases. that is.. . . .rnc it ns allowsthe userto specifythe numberof intervals within the range.The problemis compounded the possible by exisspaced rootsmight be misscd tcnceof multipleroots.On tho other hand. needfor them to bracketthe root.In suchcases.) 0 (5. lnterestingly..r. the namc implies.e.s.l (. if the length is too great. sign on opposite sides the root. However. tive fashion. search the can be very time concl. with trial and error.e.If the lengthis too srnall.An optionalargument of a firnctionf L. initial guesses required.. thereis no can but Opertmethotls. As that Brttcketingmethod. problems.they can divergc).1. allowing you to make many guesses By rapidly. for many other problems.roice ol'the incrcment suming. and ... In is automatically to 50. contrast. The two major classes methodsavailable of are distinguished the type of initial by guess. and f (x. In general. bracketing slclwly For well-posed the methods alwayswrlrk but converge (i. thescare basedon two initial guesses "bracket" the root-that is.r.

Ll.incsearch (func.2) = x (k+1).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) ?displzr-v 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.xb is: null for k = 1..-aLor ? incsearch: xb . of a function ? ? inpuL: furLc . . ns = 50.l) xb(t'tb. x{kl.name of funct-iorr % Y xni -. xb . n s ) .i I .n .x m a x .ns) function incremental search root fo. xmin.s found.func (x) .xmin. xb = incsearch(func.xmax.2 EXAMPLE FIGURE 5.ns) : % finds brackets of x that contain sign chattges % on an interval. xmax. xmax cndpoitr(s oI inrerva] (default = 50) ? ns = number of subintervals % outpuL: xb(k. xbrnl. unless sign c|ranqe det ec t ed nb = 0r :{b . % if nargin < 4. e.sign(f (k+-L)) licheck for sign chanqe nb = nb + 1. end end jf isempt).1) is the lower bound of Lhe kth % 2 xb (k. 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 . f .5. length (x) -1 if sign(f (k)) ^-.4 on seorch An M-file fo irnplemenl incrernelrtcl r20 .2 ) is tire upper bound of t-he kth If no brackef.

6] fbr the function: J G ) : s i n (l 0 . it is clearthat eachrepresents real two rootsthal are very closc together.36'. /34-t 4. The MATLAB session usingthe detaultnumberof intervals >> incscarch(@r of possiblc 5 afnS 3 .5.t3 3. r ) * c o s ( 3 .However.5 Althoughfive sign changes dctccted. is J ?E 4.3. are because subintervals too wide. number of 9 3 .{:i2 l t. The f'unction can be run againwith more subintervals with thc resultthat all nine sign changcs located arc > : .3061 3. 6) numbcr (s. 49 3.4) A plot ol'Eq. 3 .6531 \. UsetheM-flle i ncsearch (Fig.a)to identifybrackets within the interval[3.3 BRACKETING METHODS AND INITIAL GUESSES EXAMPLE Incrementql 5. usingthe zoom in tool.5 5. 2 1 . t9\9 4.6939 sin(10*x) rooLs: +cos (3*x). (5.3061 3.2.4)alongwith thc root Iocations shownhere. the functhe are possible tion rnisses rootsat . i n c s e a r c h ( t l x s i n ( ' lO * r ) + c o s ( 3 * x ) .3536 possiblc rooLs: b.2 Seorch t2l ProblemStotement. mots look like they might be Thesepossible by doLrble nrots. a.5.25and-5.2 424 3.3939 . r ) (50) is Solution.212.r j 4.100) 3 .'-t14j.7 3.

-t213 4. ':.t21 4 . 1 ? .5910 .2424 4. The first stepin bisection to guess is two values the unknown(in the present of problenr. until the root is known to the reqr"rired A graphical depictionof the methodis provicled Fig.1B1B 5.The lbllorvingexample goes in throughthe actualcornputations involvedin the method.. 566J 3. firnctionvalueat the the rnidpointis evaluated. Nc lat EXAMPLE . The subinterval then beconres intervalfix the process lepeated the next iteration.You would be wise to supplement suchautomatic techniques with any otherinlbrnration insightinto the location thatprovides ol'the roots.1515 5.2r2). Usebisection solvethe sarne problenr to graphically approached in E x a r n p l5 .2424 4. 5. . l .1318 5 .3 5 The Biseciion ethod M ProblemStotement. 13 5.'flre is precision.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 .t57ia 4 .oi Ex plc let Th 5. o FIG Ac f.r. e Solution. thatgive valuesl'or t 0n') with dillelent sisns.122 ROOTS: BRACKETING METHODS 3. t.591 A <1 2 The lbregoing illustrates brute-lbrce example that nrethods suchas incremental search are not lbolproof.5.2121 4. The locationof the root is thendetermined lying within the as subinterval wherethe sign changeoccurs.4 BtSECT|ON The bisection ntctlu)d a variation the incremental is of search rnethod which the interval in is alwaysdividecl hali.2'. If a functionchanges in sign over itn interval.Frornthe graphical rr) solution in Nr wl thr .

5. Thcrcfble.4 BISECTION r23 ./rI .Therefbre.50+ l(x) _ | -)< .' \ gi e l4 lct's assume clon'thave the bencfitof the plcltand havc madeconservative we guesses.r. at thc midpointof the interval of lies ./. means thc value 125calcuNotethattlreexact of This that of lated here a trueDercent has error relative of r.5:1 .7316 I Next wc compute productof the functionvalueat the lower boundandat the midpoint: the : I ( 5 0 )I ( 1 2 .the initial estimatc the rtx>t.-l 142. 4 0 9 1 1 .f(nt) 2 U -2 -6 First iteration V Second iteration I xl "r/ I t Third iteration # xt )(f x.5 A grophicoldepiclionof the bisection method Thisploi corresponds the firsifour iterotions to trom Er. root mustbe locatecl the upperinterval betweerr and in the 125 200. l. 5 1 9 ( .1316 r2-sx 1 0 0 ? 4 : 1 2 .. wc can seethatthe t'unction changes signbetween values -50 clf and 200. l i 112. ffi xt.0 .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 .t FIGURE 5. 4 3 4 / .. create new intervalby redefining lower boundas 125.4 ..\r xu V Fourth iteration I Fo_.and henceno sign change which is greater occursbetween lower boundand the the midpoint.Consequently. we a the . s a y 0 a n d I 5 0 . 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 . value tlrerootis 142. 3 7 1 thanzero.1316..omple 3 5 Example5.

5:. 5 r e d t<t0. For -0.0. i relative elrur of a.r]1""is the loo{ lirr the present iteration and -rf'l'lis the rool lionr the plevious itemi t i o n .anclthe root cstinrate the third iteration calculated as is as 125 162. 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 . -y i1 l d s t v i 5 .3. 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 . U s eE q . ..'lil 1 T] te to T} on Fl Er of lo'.'l:lff]l(x)%' (55) whc.5 .101)()(.say. 3 ..F o r i n s t a n c en E x a r n p l e . .:.l 3 w a ne l 6 2 .5 I Fig exh ---__--*---+ . couldbe continued obtain We endedE.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(.|1l : 6 .y..lr:o f3 . t h e c o n r p u t u t i o ns s Icillriilulc(1. 11 1 2 . 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 . ng e i E q . 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 . strategy llawed because errorestlmates This tlrr in theexanrple wer-e based knowledge ol'thetruer(x)tol'the firnction. : 0. lrAl 5-r?sl #ll\l0o/. EXAMPLE .5 % . . l u rl. f ( t t s l . tulrcady thal on ol'the There lirre. 8 5 % .-5.3 that to r'stimate therrxrt. rnightclecide we shouldternrinate is the when the errordropsbelow. 5 ) | _ 1_r1c' . 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. W h e n c . rnustnow develop olr.xarnple with the statement the rnethod 5. :200.re. t) e t S o l u t i o n .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 . This woulclnotbe i t h e c a s ci r r a n a c t u u l i ( r . 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. nrethod be repeated The can until which represents percent a the resultis accurafe enoughto satistyyour needs.4 We thtt duringthecoulseof'thecornputttion.5o c o n r [ ] u t t h ee r r o r s .( 4 .:23.1 .jective of We an criterion deciding lirr when a lel'ined to terminate rnethod. . upperbound the The tirr is rede[ined l 62. 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 . example.7-5 ) .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 . C o n t i n u e x a r n p l e . Therelbre.-5%..124 BRACKETING METHODS ROOTS: fronrxl : At this point.5 * : | 43.59) -(t.J09(.: 0 .08c/o I6t.5 . 4 3 f i.the new intervalextends rnatecau thenbe calculated as l2-5to r. ( .409(0. 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 .

6.l..l.l . .5was 13. true root can lie anywhere the within the bracketing interval.85%. 5.7 5 143.fbr bisection.. ond opproximcte True errors piotted ore versus number the of iterolions. This behavioris manifested the other iterations: for Iterolion xt xu xl le. CS be VC he all o 1n Approximateerror. plot the exhibitsthe extremelyattractive characteristic le. -5...I is greaterthan le. and the computation be can le... I.6 suggests la.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 €. captures general that l the downwardtrendof le. le.z o rais (t) rl or Although the approximateerror does not provide an exact estimateof the true error. Thus. They are close when the true root firlls at either end of the interval.6 5 Errors lhe bisection for meihod.The "ragged"natureof the true error is due are in to the fact that. terminated. is alwaysgreater that l than le.4 BISECTION 125 Recallthat the true percentrelativeeror for the root estimate 162. Theseresults summarized Fig. FIGURE .5.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. Fig. le. of Therefore. The true and approximateerrors are far apart when the interval happensto be centered on the true root.l ("/"1 2 30 B 1 34 4 698 337 )66 o82 o4l le.:0.5c/o. In addition.

.l for bisection. "2 A. 5 8 5 9 l In essence. l 6 4+ 0 .13.r) log(_r)/log(b).4 was terminated. true root would haveto fall outsidethe bracketinginterval.1641 + 1.r.beforestartingthe methodwe areat tlre the "zero iteration.which by definithe tion could nevetoccur fbr bisection. before startingthe computation.(5. = -. exceeded.7500142.------ .142. we could rnakethe definitive staternent that x.5859):8 I MATLAB providesthe I oq2 function to evaluatethc base-2logarithmdirectly. absolute the the erroris sll:r!l-rro:AxO where the superscript designates iteration..126 ROOTS: BRACKETING METHODS when le.the root must lie within *Ax/2 of our estimate. Otherroollocatingtechniques not alwaysbehave do as nicely. neatness its the of error analysisis a positivefeaturethat nrakesit attractivefor certainengineering and scientific applications..Although bisectionis generallyslowerthan other methods. After eisht iterations.518t1 1r43.For Example -5.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.This is due to the fact l that eachtime an approximate : root is locatedusingbisectiofloS.r\tf E. Another benefitof the bisectionmethodis that the numberof iterationsrequiredto attain an absoluteenor can be computedu priori-that is. will alwaysbe greaterthan le. we know that the trlle root lies somewhere within an interval of A-r : xu ."r. (xt * x) /2. (5.50 : 150. absolute the errorwAS tr- .(.6) give these into to n: log:(150/0. While it is dangerous draw generalconclusionsfrom a single example.(#) 0.it can be to demonstrated that le. This can be seenby recognizing that beforestarting technique.For this boundto be Eq.lf the pocket calculatoror computerlanguage you are usinl doesnot includethe base-2logarithm as an intrinsic function.1 the desired is error. For when Example5. errorbecomes the F'-_ .1) log 2 :.Hence.5) providesan upperboundon the true error." After the first iteration. instance.this etluation showsa handy way to computc it.6) Let'stesttheformula.58-59 (5..r" Because eachsucceeding iterationhalvesthe error.xt.A-rt' .... : 143.5781 : I ' 1 3 . ln general.\D L If C.ro : 200 .log.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 (" .. initialinterval the wasA. Therelbre.7s00 = Wecansubstitr-rte values Eq..4.this ecluation be solvedfbri can log(L..

(l) while xr:old = xr'.xl.(func.e n d i f xr -= 0. ea. bisection alongwith Eq.er I -bi secr (f unc.tunc (xl. It is passed function(func) is in the (es) alongwith lower(x1) andupper(xu) guesses. if test XU = < 0 XI.varargin{: } ).4.end .1 MATTAB M-file! bisecr An M-file to implement bisection displayed Fig. 5.ect: r:oot Iocation zer. (x1 +. .xu. 0 0 .0001. xrold) /xr) test = func (xl. optional ln an stopping criterion FIGURE .6)can providea useful root locationalgorithm.varargin{: } ) *func (xr..0. x r louer orrd . ea = abs ( (xr . <= Xl-. . (5. elseif xf el se ea end i f end l:OOt = ea = - test xr. % uses bisect-ion rnethod to find the root of func ? inpuL: func : name of function % ts xl .Fr gues:-s = 0. * 1 . it. xu.xu)/2. if we knew beforehand that an error of lessthan 0. iterl=bisec1.end itcr.es. addition. the therewill be caseswhere (usually through knowledgeof the problem context) you will be able to specifyan absolute error. maxit=50.maxit. t h n f unct ion Iroot.end test .ea.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 .0001%) es = desired relative % erroi: (default (defaul t = s0) % maxiL = maximum allowabl e iterations pl.5859was acceptable.7..pl.p?. xl. = additional parameters used by func % % output: ? r oot .. es-0. xr = xl. rea L root ? ea = approximate : rlumbef of ? rter relative er:ror iterations (t) if nargin<3. es >'0 iter >= maxit.break. ) : lroot. i f test>0 / €rror ( 'no si gn change' ) . varargin) % bis.varargin{ : } ) *func(xu.p1.varargin{ : }) . 5... es. 0.error('at least 3 input argurnents requi red').4 BISECTION 127 Thus. formula the tells us that eight iterationswould yield the desiredresult. maxit.end :lf nargin<5 i lsempty(maxit).5.oes ? . end if nargin<1 I i sempty(es).For these cases.p2.. Although we haveemphasized useof relativeenors for obviousreasons.. xr'iLer = iLer + 1..

) i t h a s t r a i g hlti n e ( F i g .81*11.)(x1 r. We can employ this functionto solvethe problemposedat the beginningof the chapter. l ( .Y. Fr Fo mclSS 142..If not..f Gt) (5.. The function first checkswhetherthere are sufficient argumentsand if the initial guesses bracket a sign change. jumper's fiee-ftrll velocity Recall that you need to determinethe massat which a bungee exceeds rrls after4 s of free fall given a drag coefficientof 0.0000-534-50/o. 5. Ratherthanbisecting interval. -) . 5 .25lm) *4) 36.It is very similarto bisection with the exception thatit uses diffbrentstrategy a to come up with its new root estimate.7 can be usedto determine the root as >> >> fm-G(rn) [mass = *tarrh(sqrt.7) Fir wh Se .a resultof nr : 142. locates root the it the .81*m/0.1311 is obtained kg after2l iterations with an approximate relativeerrorol'€.r. .13'11 5. .. b y j o i n i n g .. an error message displayed is andthe functionis terminated.Thus.Thus. 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 . 40.the shape the function an of of influences new root estimate. f (.r Thus.r28 ROOTS: BRACKETING METHODS and maximum iterations(maxit) can be entered.200 ) (9.25) ea i ter] =brsect ( fm. intersection the straight the of line with ther axis can be estimated (seeChapra as and Canale.wecansubstitutetherootbackintothel'unctiontoverify that it yieldsarvaluecloseto zero: >:' fm(mass) is bis EXAMPLE Th 5. . you have 36 t0 find the root of 5 .3450e 0Olr q" ico ' pla 2. r .) .f (.5 Prc cal 4.1 we generated plot of this functionversus a rltlssandestimated theroot that fell between140and 150kg. 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 .for details). l ( r r ) a n d .3 6 In Example5. 2002.6089c 007 xu So 5. The bi sect f unctionfrom Fig.81m @. sqrL (9. 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."n ( f (m): \.1 025 9.) .5 FArSEPOSTTTON Falseposition(alsocalledthe linearinterpolation method) another is well-knownbracketing method. t bisectionalgorithmuntil the approximate error falls below es or the iterations exceed m a x it .25 kg/rn.:0.. the Using similartriangles.

860291 r(50 0.. rity 3to root rine 6. o/r .r. 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 (. (-5. 5 1 9 3 8 1.5.) : -2. In this way the values . F i r s ti t c l u t i o n : rcketategy 3 root ) with )n inlt line .rJ6029 . i s l S o l u t i o n . . .0 . vah-re ..fhl. A s i n E x a m p l e.sc-po.5 0 a n d i w o . : 200 .l (.2(n) which hasa true rclativeerrorof 23.ri) : -'4.. 3 .: 200. r . r 1.5 .5 FALSE POSITION lre ror ir the )ed 129 ter. 2 1 l1 .litrmula.r. algorithmis identical the one for Thc tcl ( b i s e c t i o nF i g .5 .5 . 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 .computed The of with Eq. 8 6 0 2 9 1 .7) .rt : 50 'r.).4 .\.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 .sition. 1 t The Folse-Position Method Problem Stotement.rr) 6.r.592132 f .The proccss of thc is repeated until the loot is estimated adequately.8 5 Folse osition p relaerifY EXAMPLE 5 5 This is the.f (. w i t h t h r :c x c e p t i o nh a rE q .r. 1 a n c . ( 5 .. alwaysbracket true root..) 0.519381 .:200 - : l 76 . FIGURE . 7 )i s u s e d . 7 . 3 .5clo Second iteration: (5.r7andr. 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 . ./ (.

2113 t'Q. 3 3 3 3 3 0 09430 a tBl76 o 26287 0338Ir a 40788 e. Use bisection and lalseposition(o locatethe root of 'l(r) :'r"'-l b e t w e e nr : 0 r n d 1 .9. : 116. (Y"l 48I 309 223 l7 l e. curve As the violates prernise which fhlsepositionwas based-that is.t30 ROOTS: BRACKETING METHODS Therefbre.3828 .16a/c 8. true error is reduced lessthan 27r. S o l u t i o n . r .l(. r. 3 . Additional iterations be performed refinethe estimates the root.2113) : .566174 t 62.-{n which has true and approximate relativeerrorsof 13. .f (rt) : -4. and r.. becomes upperlimit for the next the iteration.-56olc.(7"1 906 BIB 737 662 592 0 a a943O 0 tBtz6 o 26287 0 3 3 8 II After five iterations.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. if . U s i n sb i s e c t i o n . EXAMPLE . r0 6.5 4 975 I 1375 ) 05625 I 0t5625 e" ("/"1 r 0 00 333 1 43 77 4A e. alier l'ive iterations. : 176.rr) is muchcloserto the on . 2 7 7 3 -1519381 . in Fig.. -5.r. true error hasonly beenreduced about59%. can to of Although firlse positionolien perfbrmsbetterthan bisection.566t 74(50 t] 6.the root lies in the first subinterval.J 4 5 O (r5 0 975 o 975 o 975 3 .. there are other cases where it does not.. Insight the to into theseresultscan be gainedby exarlininga plot of the l'unction.0.) :0.566114 0.r. 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. and respectively. : 1 1 6 .519381 .2713. the to a very diffbrentoutcomeis obtained: herotion I 2 3 4 5 xt .J J 137 5 45625 35 25 t3B 56 6 Thus.Frlr falseposition. thereare certaincaseswhere bisection yieldssupericlr results. As in the fbllowing exarnple. (o/ol u 2 ..

35 2. F ( x ) x l r t p n c{ 1 .5 FATSE r3l he next r cases section (. and determining into the originalequation estimate method:its oneof a The examplealso illustrates major weakness the false-position pointswill tend to of the bracketing one are That is. ". rior to bisection.the results by be checked substituting root shouldalways in additionto usingEq.Possibleremediesfor this shortcomingare Canale.9 5 : P o to f ..2002). as iterations proceeding. whetherthe resultis closeto zero. the (5.5). a methodsuchas lalse positiorr often supeAlthough are rnethods usuallynot Therefbre.e curve loser to . (7"1 906 Br 8 737 66. sidedness. This can lead to pool'convergence. availableelsewhere(Chapra and curvature.POSITION 5. 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"). fbr particularly functionswith significant stay fixed.the opposite true. (recallFig. 5.thereare invariably casesthat violate this generalconclusion. of the present .Because is function.a e.8)..2 5q2 roclt-location regarding illustrates blanketgeneralizations that The fbregoing exarrrple is possible. r t l o n .5 r3 8 56 t6 FIGURE . ght into . thentheroot is closerto x1thanto x.

5 )+ 1 .n[H+] 6.I H * J l H C O i I " Kr-10"' ' Knpco. pH is the measureof the activity of hydrogen ions and..r32 'il ROOTS: BRACKETING METHODS "ii'i*.IO Averogeonnuol portiolpressures otmospheric o{ corbon dioxide (ppm)meosured MounoLoo. A tr AI I In Part Four. therefore. .s) w ul tr st l( where [H+] is the molar concentration hydrogenions. : 0 .Fig. Hawaii polynomial. 0 1 1 8 2 5 ( r .The trendin the datacanbe nicely fit with a quadratic p c o . Outsideof urban and industrialareas. The data indicate that levels have increased a little over 19Vo over the period from 315 to 376 ppm. It is well documentedthat the atmosphericlevels of severalso-called "greenhouse" gaseshave been increasingover the past 50 years. ol Howoii.For dilute aqueous its solutions.10 shows data for the pafiial pressureof carbon dioxide (COr) collected at Mauna Loa. One questionthat we can addressis how this trend is affecting the pH of rainwater.SY Bcckground.iei{rl: { ' GASES AND RAINWATER GREENHOUSE .. acidity or alkalinity."' .it can be computedas pH: -log.COz partial pressure(ppm).3 from 1958through2003. .9) rh fc a 5r is pc Sr 1960 1980 1990 2000 2010 Er FIGURE 5. of goverx the chemistryof rainwater: The following five equations ^ . 3 5 6 9 7 5-( t 1 9 8 0 .. is well documented it that carbondioxide is theprimary determinant of the pH of the rain.lf. 5+ 3 3 9 2 ) where pgo. fa .1 9 8 0 .5.For example. (5..iry'ii"". we will learn how to determinesuchpolynomials.

l7) this result is a third-orderpolynomial in Although it might not be immediatelyapparent.6 CASE continued 1ll:- - tH+llcol2l tHcotl (5. was 3 l5 and in 2003 when it was 375 ppm. a n dK . to causethe pH can only be measured two decimalplacesof precision.1 4 6 .[H+] The five where Ks : Henry's constant.t o : .11)into Eq. One way Solution. first solveEqs. providesus with two good initial guesses. 'z##I KuPcoz.10) (5.l a .12). Notice how the partialpressure CO2 showsup in Eqs. ( 5 .21 : carbonate. s) I {#F rcor2l: S u b s t i t u tE q . (5. Now we must decidewhich numericalmethodto employ to obtainthe solution.1 0 .3 ) 1 K. falls within the rangefrom 2 to 12. . and tOH-l : hydroxyl ion.63 .we will be satisfied . 1 5 ) e : tco. There are a variety of ways to solve this systemof five equations.10)for Kupco.16)canbe substituted alongwith Eq.To do this.21+toH-l .(5.. .There are two reasons why bisectionwould be a good choice.STUDY 5.tHCOtl: tCO. Use these equations to compute the pH of rainwater given that K n = i t l 0 . C o m p a r eh er e s u l t sn 1 9 5 8w h e n the pgo. K z . its root can be usedto computethe pH of the rainwater. (5. to You also know that pH can only be measured two placesof decimalprecision. bicarbonate.: [H-][OH-] . Thus. and K.. 1 4 )i n t o ( 5 .First. 4) r (5. tHcotl: -fu.14)and(5.9)and (5.K 1 : l Q . is to eliminate unknowns by combining them to producea single function that only dependson [H+1.9)and (5. K2. of [H*] : hydrogenion..13)to give Equations (s.co'+ # ( 5 . [H+]. : l Q . unknowns utec7 : total inorganiccarbon. 2) r ( s .(5.I 6 ) (5.l) r (5.[H+] Ku. the fact that the pH always beSecond.21 ffi*rnro" o: #*. considerthe following: .r: fu#+ ' 106 tHcotl11coJ2l 0: tHcoil + 2tco.and K1. areequilibrium coefficients. (5. You know with certainty that the pH of rain in pristine areasalways falls between2 and 12. When selectinga numericalmethod for your computation.

7. i. Befbre implementingbisection.. ILI l i i i i.1 Use bisectionto de so that an 65-kg bungee 4.ir . rit i 'r...')l lrl )*l'i (L.1li)ll. resultis the 5.) PROBTE l 'i .7 to obtain the solution. ' . 5. ll..rrl-1-l.2 Develop your own J ion to Fig. 5. l .settingmaxi t to 50 yields ll.l. i. r.I ..: ' ' rr-ll. L...t.ir ".Becauseit is relativelycomplicated. : I x l0-8) at a very low level so thattheiteration limit (maxit) is reached first so that exactly I I iterationsare implemented lrijlli. r r ) ll I li) i.1ll)t). For example.4 Develop an M-file b y s o l v i n gP r o b .we can computethe numberof iterationa priori. Note:Tl Start with initial guesse ate until the approximat 5. the pH is computedas 5. 5. This can be verifiedby performing anotherrun with more iterations.lr'(l .ll^i.) I ilI't l-ilrli.t34 ROOTS: BRACKETING METHODS continued with an absolute error of Ea. (5.t We can then use the M-file from Fig.L.*lll*llj*1.. 5 . I.rll |r1i1l. For 2003.0001 . L ) .6279with a relativeerror of 0. '. . r irilill. Substituting present valthe uesinto Eq. Remember that given an initial bracket andthe desirederror. . li.'lLl. irrrl I '. i l.ll ll.lr I ( r .l-l ll Thus. ' .. (5.3 RepeatProb.l .-l 1 l . t l L .7: 0. 1l)l (ljil.1..0868%.63 is correctto two decimalplaces.{.17) as a function.we must first expressEq..i'. ll'.i tt. . ' ri l Eleven iterationsof bisectionwill producethe desiredprecision. Make sureto r next highest integer.'.lll'r iill" rr .. t.1r .' l:i. iLr. tl t.5) criterion.l I llrr ]...6) gives r l . .{). 5.5 s of freelall.'. i.:..005. (5.we will storeit as an M-file: lii|aij f.l'i.'ll r. 5. Howev iterationsand Eq.d : t0.r.iiir. lri:'. t obtain your solution.. .Ter using 8 .We can be confident that the roundedresultof 5.Notice how we have set the value of the desiredrelativeerror (r.''. rl il l'.i.l i.

your Devclop own M-l'ilefirr biscction a sirnilarlashin toFi. Howcvcr.5.2 and i.e..L. unit drop in pH repa a resentsa l0-fold increasein the hydrogen ion.engineers and scientists hopefully usesuch can tools to gain increased understanding suchphenomena help rationalize debate of and the over their ramifications. resultsindicatethat the l9%orise in atmospheric the CO2 levels has produced only a 0.rathcr than usin_r:l rnaxir.5. [H*l : lO-pHand its percent I : I 1112.the hydrogenion concentration increased has about.. but nsc thc falsc-position Repeat Incthodto your solution. Thc accclcration Notc: ol'gravitv 9. :0.nrrrn -5. is sobering realize of it to thatsomething as largeas our atmosphere changedso much over a relativelyshorttime period. :ll ri.-5). gas Most of this debatefbcuseson whetherthe increases contributingto global warming. (-5. There is quite a lot of controversy relatedto the meaningof the greenhouse trends.Over the coming years.. \ lr p ] ]1 9 5 8 ) * 1 0 0 clil:l ( r .97o. crnployEq. Although this is cenainly true. (5. I 5.3 anclitcrthc unlil approxilnatc rclativccrror lirllsbcl<tw 2()tr.-5.5 A bcantis loadcdas shownin Fig.8I nr/sl is initial Stafiwith gLrcsscs of'. 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 . are However. The concentrationcan be computed as change can be calculated as.6)up to thc highcst integer. Makcsureto roundthc rcsultol'Eq.1. Testyour llnction by solvingProb..This case has study illustrates how nunrerical methodsand MATLAB can be employedto analyzeand interpretsuchtrends.o q l ( l Therefbre. 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.67Vodrop in pH. I FIGURE 5. rememberthat the pH represents logarithmic scaleas definedby Eq.5 P .k gb u n r c c . 100 lb/ft 100 b t = 8. Develop M-filc fbl thc lnlsc-position an rnethod.ri{ .5.. thc andEq.PROBLEMS r35 lii . I r ..r 0'000I Prob. Usc thc biscction rncthocl solvc lir thc 1-rosition to insiclc bcantwhcrc thc thclc is no rnolllcn(. Tcstit solvine Prob.' continued ll . regardless the ultimateimplications.0E74 i r ^.6) as yoLrrstopping .Consequently.- tL Interestingly.8)... i' tt'_. (5.7. P5. (5.ri :0.1.

(P5.st your lunclionlirro. x 6. in (i lhc lhc (a) Il'thc initial gucsscs sct as 0 and 40 "C. Justif. O2 0:I LA. p u t a t i o n :: 6 ( X ) c m . anclitcrate until the approximatc errorljrllsbelorvl% or thc numhcrof iterations exccccls Discr. tion or' lulsc position to ohtaiu your solution. trar it necessaryto determinc s trends of a city and adjacer urbanareais dccliningwith PuQ):Pu.62| ntg/l.clbl theroorwith inirial ol guesu tr ^.10Watcr is llowing in a trapezoidal channclat a ratc ol' whcrc thc subscript dcsicnatcs 0 thc initial concentrution Q : 20 mr/s.rl) : ().ocate first nontrivialfu()tof sin(. which is thc s{.7 : thc saturation conccntration ot'dissolvcd oxy: casc whctc .. Choosc cither bisec tvherc g : 9. I ) trction. 2.7 (a) gfaphically.r' : 28.r'. 10.{asrepre.J lrolcs/L/d. ol'yea A: "' P-" (t+i). 5. : thc hall-saturarion constan[.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 * . n (b) On thc basis (a). : 2c/..{bstratc I at rvhichuptake half lcve is ol Lhcrnaximumlrnolcs/Ll.tr- I*- Solve lbr the criticaldepthusing (a) the graphical method. Q'.S: Sir..7 l. thc valueof _rwhcn valueinto Eq. is lessthan o. .tt.l whcrc .5 and -{r..016.1 t3().Gir. l0lnd l2 rng/LA'.The critical clepth tirr suchI channelrnust ol cachconstitucnt. :0.i T.r = Inrxi.13 Figure P5.this dil'f'crcntial cquiltion can bc solvcd fbr .5 kN/crn.Tc..15 Many ficlds ol'engine estimates.rss results..L. j 5.3i3 graphically. .! = suhstrate (rnolcs/L). (b) using threc iterations of the biseetion nrethod.Sp l0 ntolcs/L.r36 ROOTS: BRACKETING METHODS case. 5._l i(l. : ahsolutc *..: 2. 20 to r.r7: -l and. determinethe flrst root of the lunction with (b) bisection and (c) falscposition. (i.3441 +lj]I01"]o' 1. thc (a) Obtainthc solLrtion graphically.2z1.. can bc churactcrizcd thc cqLrilibriunt at 0 "C to 6.s.05'C'l vutior)ol' nlasscirt bc uscd kr lclirrrnulatc cquilibrium thc on {b) Basecl (a).0: -1..cl dcto lclrnincthatoxygcnconccntration rangcs by lclationship li'orl 14.with initial gucsses -r1: 0. (b) bisection.l (x) : . with the sameinitial gucsses in (b).r w hcre. Perfblm the computation until e.l.l r.. r*r.o: 42.1 5. Use a graphical tcchnique and biscction with the initial intervallrom 0.5 to l.-5.tl. biscction itcrations would bc rcquircdtu dctcrniinc rnte scnting nurnbcr thc ol'rnolcs ol'C thatarc ploduccd.F-orexamplc.: 2.'hcre rrorlcnclaltrrc l'cptcscnts conccntrationof alc constitucnt Supposc l.0.T3 P : tnr.and r): and gen in licshwatcrat I atnr (ntg L 4.tJ Detcrminethe positivc rcal rrrtr(of ln(. and of (c) using threciterations thc talse-position of rnethod. For typical rratural 24+B-c u'atcrs tcnipcrate in climatc.4. Rcmcmbcrthat f.. lhat wc rlcl'inca variublc.64230U 107 -l- l.ltl00 l0r(l x r. conccntration rlum uptakcrltc (rnolcs/L/d).T : lJ.For (b) and (c) use initial -guesses .: 0 rnd -r.this lirlrnr"rla and the biscctiontncthodcan hc uscdto solvc lirr tcmpcratulc "C. . 1.(.5%. ano A .2x))(t 1.v and R : thc width ol'thc channel lt the surtacc (m). * _f- 2 .n^^e-k"'aI .: the cross-scctionillarca (rrr)./ ft. Il' thc initial substritlc at level / : 0 is So.r) Checkyoul results. dctcrntinc valucol'.r s + l ' l20EIt.. K .14 You huy a $25.81 rll/s1.0 . anclA.ll Thc Michaelis-Mcntcn rnoclel dcscribcs kineticsof thc cnr)/nlemccliatcd rstctiolls: tlS tlr .6 (a) Dctermine the roots of . (K). 5.dcvclopandtcsta biscction M-l'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 .\t] 5. how rlany r.S "1. pcralurcto an absolulc crror of'0..n -5.. ( r 2 9 4 9x 1 0 " 1 Devclopan M-l'ilc to gcncratc plot ol'S vcrsus lbr the a r whcrc o.12 A rcvcrsilrlc chcntical rcaction ("C).20r * l9-{2 .: 3 r .r..0 ...9 Thc salurationconccntration clissolved of oxygcn in licshwatcrcan be calculatcd with thc etluation B: 3*. In(Sg/S) * \a t i . Use bisectionto dctcrminc (thatis.^1 : of'-t. . Conser. as 5.-5 iind-r. and (c) falsc position.14 ..500 pcr ycar Ic you paying'? The firrmular payments nr-rmbcr A.Accordingto thiscquation. : (). E : L : ond u.r . cquation the can hc usc.+S I FIGURE 5.the width and the cross-scctional areacirnbe related to dcpth -r'by P 5.l3a show early increasi distributc ng ing elastic urvcis rstc Fig c 'v : .r. Fol this your choicc.and Il satisfvthe cquation (.rrs the ) in radians.For (b) and (c) use initialgucsscs tll'ri : 0.r : .000 1 down at $5. soh. r : tcmperature satLlrilti()n dccrcascs with incrcasing tempcraturc. : 2 rlttlcs/L.cn a valucol oxygcnconK: ccntration..In adclition.rr :0 and a of stoppingcriterion of I o/c. : T +213. wherc lcmpcrature 5. yonr 5.4I 3 nig/L at 40 ''C.

t + 21-r. 8 5x 1 0 ' t C t ( N m l y .rr Lr r ) (p5.(/) when thc sLrburbs 2()%largerthan the city.500 voar lor' 6 vcars. Usc (a) biscction and and a dcsircdlt (b) the lllsc positionnrcthod. P0 : people.*. A. Determincthe timc and con'esponding 'nt'c P.What intcrcst ratc arc w h c r c c . P-5. usc (a) graphical.iu 100.I population rcquireaccuratc Many lields of'cnginccring Forcxarnplc. 1 : 30.and t.08/yr. n . 10. : The pararnctcrvalues arc P. i l t l t r37 I +LP.ninc noint ol'rnaxirnurn thc dcf'lcctitrrr value whcrctlt'ld. 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 .as in rvhilethe suburban is P rrt rD \ . nurnber ol'ycirrs lnd intercst i(l+ l)" A =P i l + l ) . and k. k. Uscthclirllowingprrarnctcr ion: . ittrttual t h c l o r c c i s 1 . 75. Thc ccluatiorr thc rcsultlirr atlyrncreasing hgelastic is (sccFig.. : 0. P..13) . kN/crn'..r this into tP5. and thc referenccmobility pr1y. P. valucsof ralnctcrs.ncfc iIGURE 3 P5.l3r) cun'c r = -l_l.t 'r : ub=25kN/ctn.000 and mcthods.000cm*.': You buy a $2-5....I 5'13 Figure showsir unilirlnrbcarnsubjcclto a linP5.5 0 c r nV s ) r ...To obtain your solutions...0). : 0..thc clectrondcnsity n. thc valucsin y()r.. A chlrgc q is l()catcd a at The -r drstancc lhrrn thc ccntcrol'thc ring ( Fig.5 x l0t' V s cm/C.u.'.L I . . 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. I u0. nt tcr is with timc accorcling area declining P .". givcn 7ir : 300 K.riccc ol' cqr:ipnrcnt at$5.max/Po lls-t'r (a) (.rl + a2yr.17).The thc dopingdcnsityN iinclthc intrinsic by clcctronrnobilityis describcd the tempcrature thc rcfI The crcncc tempcraturcI. r.l3rr distlihutcd loacl.t 120F.000 lil' nothing . " * t .(t)andP.. Thc electrondcnsity is givcn in terms of carricrdensityn.. Then sr.rrin . 7 x l 0 r ' ) .t=0) $'hcrc P.lace suhurh..rbstitulc ts. .n. 9 r n ..r'. P5. lirrcc cxcrtcdon thc chargcby the ring is given by Usebisectron ttrdctcrr...r'corlr . C : 6.u .u.16 Thc rcsistivily p ol' doped silicon is based on thc chargcr7on an clcctron..000. 1n co (.. : 6 . 2 1 x l 0 ' ) c r n . : ernpiricallyderivcd patu. (b) false-position -i. P."*. cqLlntions rcquircdto computcthe rcsistivity are I qnlr \4'. l"' P FIGURE 5. transportation cngincclsrnightlind necessary to dctcrnrincscparatclythc populationgrttwth o{' Thc populatiort (hc of a city andad. anclthc electrorrrrobility ll.4 : 1 . : t 1 .PROBLEMS population growing. f : 1000 K. : : P..000people.045/yr.(r(X) E : -50.x 100'000 people. ratci is pyments rr.l3)to dctcrminc valucol'thcrnaxirnurn Eq..:j(r* : DctcrnrincN.17 * Prr.thc ol'..(XX) L crn.. A totalchargc is unilirrrnly distrihuted around ringa B shapccl conductor with rldius a.r=0. l7 -5. 1111 3 (r l 3 .

.{ lfl -llln 5.fr i c' i on . computethc tirne at which u = 7-50rn/s.qI whereu : upwardvclocity. 9 5 2 0 l 0 l ' 1 r l x x Detcrmine tcmperaturc conesponds a specilic heat the that to oi l.The Faness t ning liiction factor is dependcnton a nurnberof paramctcrs related to the sizc of the pipe and the fluid. . .(X)0 and = kg. wcll as mostothercnginecrs. rt.000to 500. as in uscthernrodynamics cxtcnsivcly thcirwork.re value Chcckyoul answer.the F unn i ng . q : thc l'uclconsumption irnd = ths riite.kJ/(kg K) to tcmperaturc r .) results0 thutit is w'ithinl% ol'thc trr.000anclfbr thc Fanningfiiction tactor arc f ) . which can all be representcd anotlrcrdimcnsionlcss quantity. : 150.Thc lirllowing polynornial can bc uscd to rclatc thc zelu-prcssurc spccilic ttto .00000-5. 0 0 It o 0 .(nc. l) u. 0. 0 1 . 6..rpplicd valuc of Rc bctween2.l kJ/(kg )./ given a uscr-sr.20 The upwardvclocity of a rockct can be computcd by thc tbllowinglblnrula: l' : .9 .^ Senl F valu time Hov quic a sir ing I ..l given Rc is the r. K 5. .r38 ROOTS: BRACKETING METHODS (K): heatof dry air r1. 5. .500and 1.+ R Jf Typical valucstbr thc Reynoldsnumbcrtbr turbulentI'low are 10. solutecrror in thc'rcsulti.000.19 Mcchanical cnginccrs. 6 1 x l 0 1 I + 9 .. g = dorvnwat'daccelera(ionol' gravitv (assumcd constant t). 4 2700 kg/s.qt - . 7 2 1 5x l 0 8 r : | .(Hint: t ycurr is somcwhcrc bctwccnl0 and.lJI rn/sr).. A firrmula that prcdicts.on Kurnuul etlutttion: Ir-\ ::4toc.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..50 Dcternrinc s. Designthe lunctionso that it cnsures thatthe ab. : 0 .the by Revnoldsttumher Re. friction is dcscribedby a dimerrsionl number.5 8 3 8 l 0 r r T 3+ 1 ./??o thc initial mass ofthe rockctat tinlc/ :0.lti For tluid flow in pipcs.fot:to r . : thc vclocityat whichfuel is u : expcllcdrelrtivc tt>thc rosket.000.If r.1...r: 2000 m/s. 9 9 4 0 3 + 1 .

Knowing how to irnplement boththe secant secant rnetlrods. 6.5. la). ' . open ntethotls the in described this chapter requireonly a singlestarting valueor two startingvaluesthat do not necessarily bracket root.lc) they usuallydo so ntuch more quickly thanthe bracketing methods. . Learninghow tclrnanipulate dctcrmine rootsof polynomials and witlr the MATLAB. 6.Repeated application these mcthods of alwaysresults in closerestimates lhe true valuc ol'thc root. the Understanding fixed-point 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 .genl In contrast. ch c Knowing how to solvea rootsproblern with the Ncwton-Raphson methodand the appreciating concept quadratic of convergence. Suchmethods saiil lo be converof are (Fig. r39 . and are . will beginour discussion opentechniques We with of that is usefulfor illustrating a simpleapproach theirgeneral fbrm and alsoltr demonstrating the concept ofconvergence. and the modil'ied Knowing how to useMATLAB's f zero lunctionto estimate roots.when the open methodsconverge1Fig. they moveclosertclthe truthas the cornputation progresses because . times divergeor move away from the true root as the computationprogresses However.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. o . lb).As such. the root is located in within an intcrvalprescribed by a lower and an upperbound.they somethe (Fig. or the brackcting rnethods Chap.6. Recognizing diff'erence the between bracketing openmethods root lbr arrd location.

t40 ROOTS: METHODS OPEN EXAMPLE 6.. ^1 ^u # xt -lx lH -tt-ru lol FIGURE6. io -r. E Q .I ITERATION SIMPLEFIXED-POINT As just mentioned.:l Tt an .r. opennrcthods to employa fbrnrula predictthe root. T h u s . ri s o n t h el e f t .r.]n controst.r i . r 0 s o t h a t . (6.one-point 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 .I djfference Grophicol deplcfionof lhe fundcmentol betweenthe (o) brocketing ond (b) ond {c) for open rnelhods root loccrlionln (crl.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 . of The utility of Eq.11eSexprcssed the iterative by fbrnrula a ri+t : g(ri) (6. I in cn itercrtive cr foslrion. as it is alsocalled. ( 6 l.rasa a f u n c t i o n f a n o l d v a l u eo f r . ihe open methoddepictedin (b) ond (c).^ n i l i o l t L r e s s i TI 6.rr shope of the frnc-rion ( c n d t h e v o l u eo f t h .q(x) (61) pr pr sit gri be alt This tlansfbrnlation be accourpli.Sucha fbrmulaca be developed sirnple.id1y. |J ri r.whLch Newlon Rophson. or dependingc-.li. l ( ..r1 ond -r.) c a n h a o usedto compute new estimate . which is bisection.h a n d su by h sideof the equation: -r:. Thus the thr: methodccrneilher(bJcliverge (c) convergerop. lorrnulois used fo proiectfrorn. l) is that it provides tbrmula to predicta new valueof .red-poitrt fbr itcration(or.shed or can eitherby algebraic manipulafion by simpJy addingr 1oboth sides the originalequation.. rooi is consircrinecl the wilhinlheinfef for s vol pr-^scribed by.

to the Solution./:(-r) (6.-5 0.l (r ) : e ' .ll(.2a for the f unction.5 ) (6.t'::.1 (6. "/" le. "h t 00 000 76322 35 I 3-5 22 450 t) 755 6 894 3 835 2 tsg ) 239 o 7a5 0 399 le.ftG): JzG) Thenthetwoequations .-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. .56714329.vr.. we graphed functionto visualize structure a its and bchavior.4) ..x. Rccallthat in Scction5.3) Problem Stotement.fi+l : C-" :0. mustcommcntat this point aboutthe "poswe sibility" ol'convergence. I is roughly of (by proportional a firctorof about0. The concepts convcrgence of and divergence can be depictcd graphically. the approximate error for this equation can be determined using the error estimator: '":lfflrcou" Fixed-Point lterotion EXAMPLE Simple 6.r. charactcristic ol'fixed-pointiteratron.6 .2) as in .6) to thc errorfiom the previous to iteration.r) and .. eachiterationbrinssthe estimatc closcrto the true valueof the root: 0. is calledIinearconvergen(e. 6. The function can be separated directly and expressed the form of Eq./le'1. is Suchan approach ernployed Fig. 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 . (6. Aside liom the "rate" of convergence. this iterative Starting with an initial gr-ress -r11 of equation can be appliedto computc: rt U I 2 3 4 -5 6 7 B 9 le ol.Use simple fixed-point iteration locate rootof /(r) : 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. Noticethat the true percent relativcerrorlbr eachiteration Examplc6.l.l.2. This propcrty.

rr)l and then to [x2.r1. g(r0) l.rr] is located pointon rhe.therootsoll'(x):0correspondtotheabscissavalueattheintersec' shapeslbr.142 METHODS OPEN ROOTS: 6.il..:r2]. 6.Thesemovements equivalent the first itcration .)'t : 't and tbur diflbrent plottedin Fig.rr. FIG Gro ilnrn osc canbeplottedseparately(Fig. mateof . ""li.. The function . in both the equation This g(.(l'2b).v2 : g('r) ate tion of the two cufves.1 6 FIGURE .the initial guess r11 usedto <letermine to by movinglefl horizontally curvel. a starting Thus.3a). 6.3. The next iteration iteri roo Fig Cha por slop .(6.2 root the fordererminins 'l l9l :-:^:-Jr !1ll::' "' 'h' methods.l) can he rcexpressed of fixed-pointiteration.y2:g(r). t o t t h el n i e r s e c t io fnh " .. The point [xr .e ' rvvu "E srophiccrl iunctions oi lhe O t . theconesponding of is For the first case(Fig.. " . o * p o n " n t ooiniwhereit crosses . .re is usedto obtainanestiand in the plot.4)and(6'5). u i.r 1: g ( x s ) valueof .The-rvalucscorrespondingtotheintersectionsof of the represent rclots /('r) : 0' thesefunctions and divergence 'l'he twcl-curvemethodcan now be usedto illustratethe convergence =r as a pair of equiltions'\'l First. (6.{(. Eq. with As separately' was the case can then be plotted ancl. of consists moving to [. Thesetwo equalions Eqs.yz of the fixed-pointmethod: to are the -yrcurve.

this is not the casefbr root with eachiteration.3a is convergent because estimates -r move closerto the the The sameis true for Fig. wherethe iterationsdivergefrom the root.ITERATION 6. As in A theoretical derivation can be usedto gain insightinto the process. described Chapraand Canale(2002).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. 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 . 6. 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 ' ( . of The solutionin Fig.3b. Fig.3c and d. r ) l . Grophs (o) ond (c) ore co led monotonepotlerns . 6.rt.I SIMPLE FIXED-POINT r 43 J0 (cl (d) FIGURE6. e iterationis equivalent the equation to 12: s(. 6.< 1 . However.

Use thc Newton-Raphson rnethod estimate root of /(x) = to thc ? ' .2 6 N e w t o n . o method can be derived on the basisof this geometrical interThe Newton-Raphson pretation. Notice that the true percent on relative each iteratiorr el'rorat clecreases much faslerthan il doesin simple fixed-noint iteration (compare with Example6.andhence the errorswill havethe samesign (Fig.lg'l < I. .re : 0..l. 6.i-l it tt Startingwith an initial gucssof . in Fig.R o o h s oM e t h o d n Problem Siotement. Notice also that if the derivativeis positive. 0.tremployingan initial guess ol'.l which is calledtlreNcwton-Raph.3aandc).6)to give alongwith the originall'unction '\'. . If the initial guessat the root is . z. (6.2 NEWTON.son lbnnulu.r is equivalent the slope: to /'('r't: /('r't-o Ji -'ft+l which can be rearranced vield to ' r r + l: ' .r1.the enors will be positive. if For lg'l > I theenors grow... I (. a tangentcan be extendedfrom the point The crosses r axisusuallyrepresents improved the an Iri.r1 ^. the approachrapidly converges the true root. e-'.567 432()0 IOO llB a )47 0 0000220 EXAMPLE 6. t/ .a)..10 8 Thus.'1 \'. . If the derivative negative. the first derivative As at. 6. change sign on eachiteration 6.6. the errorsdecrease with eachiteration.3b andd).3 P R . this iterative equation can be appliedto compute le. point wherethis tangent e s l i m a t c l ' t h er o o t . 6. Solution. (66) f ( .r. The I'irstderivative functioncan bc cvaluatcd o1'the as /'tt) : -<" I T which can be substituted into Eq. l). enors is will the (Fig. EXAMPLE .4.l Gi) f o.RAPHSON Perhaps most widely usedof all root-locatingformulasis IlteNewtott-Raphsott the method (Fig..144 ROOTS: OPEN METHODS Consequer-rtly. "/" 0 I 2 3 4 0 0 -500000000 056631t003 o 56/143165 1 4 .)1.

6.2002).r.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.cven whcn dcalingwith simple roots. tongent ihefunction .A special case-multiple roots-is discussed elscwhcre and Canale. (Chapra ln addition. Determine positiveroot of . Although thc Ncwton-Raphson method is ofien very efl-icient.7) Thus.r oxislo provide estimote ihe rootot .l(l) : . tl { ) | I using the Newton- l.11 on of .thc crror shouldbc roughlyproportional the square to ol'the prcvious error.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 .rr0lhe Raphsor.In other words.r.2 NEWTON-RAPHSON t45 l(r) FIGURE .4 6 Grophicol depiction theNewton-Rcrphson of method. The Newton-Raphson tbrmulafbr this caseis .i r .rion. thcorctical a analysis andCanale.-5. 2002)provides insightregarding the rateof'convergence expressed as by (6. A to of oteddown to the. of Solution. fthotis.r : 0.r : . t l r -t -. (6.)rI: which can be usedto compute . However.t . thcre irrc situations (Chapra whereit pcrlbrmspoorly. .r methodand an initial guess . crite.3)can be usedas a termination Eq. e E X A M P6 . /'(. the numbcr of' significantfiguresof accuracy approxirnately doubleswith each iteraticrn. This behavior calledqruulrilic (onvcrgcn(eandis oneo1' rra.jor is the reasons fbr thc popularity ol'thc rnethod.r)l is exlropo As with otherroot-location mcthods.

Thus.6-5) where. FIGURE . is Why doesthis happen? shownin Fig.5.6. "/" 0 l 2 3 4 0.a simpleplot of the first few iterations As Noticehow thelirst guess in a regionwherethe slope neat is is helpfulin providinginsight. other difficulties can in arise.5 6 Rophson method o cosewilh s ow convergence for The Grophicol depiction theNewton of how o neorzero shoots solution fromtherool Thereofter.432 4A 4l 42 0 0 2 3I 6 aaoa24 2130 o 22g 0 002 is on Thus.6a depicts casewherean inflection the . afterthe first poor prediction. 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.r: -5 fbr over 40 iterations until convcrging the root with adequate on accuracy.146 ROOTS: METHODS OPEN xi leol. the first iterationflings the solutiontar away from the initial guess a new 1. technique converging the true root of I. but the at a very slow rate.65285 99. to zero. 6. 6.as illustrated Fig.6.The solutionthcn plodsalong value(./(x)has an extremely high value.5 5 16 5 46. For example.485 4t 8365 37.

6.2 NEWTON-RAPHSON FIGURE .6 6 poorconvergence method exhibits wheretheNewton-Rophson Four coses .

nl ) / . / L r a I J n ( G ( x ) . or 6..r2 . As it means thatthesolution shoots horizontally neverhitsthex axis.6)1.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.the derivativeof this function rnustbeevaluirtedwith resDect the unknown. of Solution.6. thc root cun he dcterminecl in as : ' j ' n e \ ^ ./'(.H')#..7). they can be passed thefunction as to as arguments. Figure6.This tendency move awayfronr thearcaof interest dueto thefact to is that near-zero slopes encountered.9.'(l. off and Thus.3 SE As the ma '+#:irF-""^(. Notice that iterations beginning .it can be invokedto solve tbr root.7.e. Alier the M-file is enterecl saved.r48 ROOTS: OPEN METHODS ' point (i.6c showshow an initial guess thatis closeto one root canjump to a location several rootsaway. 6. Forexamole.f ltnl :rff""n( grii.n: to 6. (E6.. Thc firnctionto bc evaluated is .(6. and lor the simplcfunction.41) To apply thc Newton-Raphson method. are Obviously." .It also suggests that good cornputersoliware shouldhe designed recognize to slow convergence divergence.81 nr/sr. And for some functions.a near-zero 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.re at progressively diverget'rorntheloot. 6. Fig.r. or. ( 4 ( x ) 2.r) : 0l is a rcaldisasterbea cause causes it divisionby zeroin thetr-ewton-Raphson fornrulaIEq.6b. in Fig. 6. zeroslope[.5) Ft An . convergence Its depends the natureof the lunction and on the accuracy the initial guess.1 I ATLAB M-file! newtraph An algorithm the Newton-Raphson lbr (Fig.25kg/rnto havea velocityol-36rn/safter4sof bungee of fice fall.2.' EXAMPLE 4 6 ' N e w t o n .6.6. method canbe easilydeveloped Notethat theprogrammusthaveaccess thefunction(f unc) andits first derivative (ofunc).7 to detcrntine rnass thc the of jumper with a dragcoefTicient 0.rrt( .Suchoscillations to maypersist.6d.f (x) : 0) occursirr the vicinity of a root. ^ : l ! 1. Usc the M-file functiontiom Fig. in the algorithmin Fig. The acceleration grlvity is 9.6b illustrates tendency theNewton-Raphson the of technique oscillatearounda localmaximumor minimunl. only on of The remedyis to havean initial guessthat is "sufficiently" closeto the root.thereis no general convergence criterionfor Newton-Raphson. asin Fig."x.These to can be simply accomplished the inclusionof user-dcfined by functionsto compute these quantities. Alternatively.) -fir r.

> > d y = G n I / 2 * : . .p2. ea = abs( (xr . ea. d y . it:erl=newtraph (f unc.t t t i i involvcsa bit of concentration eflbrt to arriveat thc l-inal and rcsult.3 SECANT MEIHODS f u n c c i o n I r o o u . error ( 'at least 3 input arguments required' nargin<4 isernpt)'(es). end (1) while = xr . xr. ^ (1 / 2 .rc(xr)/df unc (xr) .p2. es.d) /xr) if ea <= es i iter >= maxit. d i r n c . df unc.l * 0 .zat irze of f unct lon ? xr = init ia1 guess . 1 .maxit.m a x ' L . 2 5 ) ) * t a n h ( ( 9 . parameLers usted by function % % output: % root = real root (?) % ea .n e w L ' d p h r f u n . xrold xr = xr . . / r " > n c w L r . end end root : xr.-50 . ) : ? uses Newton Raphson nethod to find the root of func % input: % func = name of function ? df unc = nane of derir. iLer ir<r * 100.{ .\2.25lm)*4) l6. ) . e a . 1 * 4 ) 9 .0001. (9.orimate relative error = number of iterations % iter if if if itoz nargin<3. F S . Although this is not inconvenient polynomialsand of fbr many other tunctions. end FIGURE 6.thereare certainfunctionswhosederivatives may be difficult or ..appr. end O... naxit. r p h ( . wtr nLih [o evaluatc root: the >> y = Gn sqrt ('j.0001?) (default % es = desired relative error (default = 50) % maxit = maximum allowable iterations . 0 0 0 0 1 ) t \.81*0.25)*l. 0 .anh(sqrl. 1l . .'tj]6 METHODS 6. 2 5 l m ) .0.r c 1 r l1 9 . B l * 0 . i r e r J. .xrol. y r .4. * s c c h ( s q r l ( ( 1. . ' E I / ( m * 0 .6. a potential problemin implementing Newton-Raphson the methodis the evaluation the clerivative.es=0.f u.additional p1. v o ' d r g i n ) % newtraph: Newton-Raphson root Location zeroes % lroot. The two fbrntulascan now be used in cilnjunctionwith the function n. 1.end nargin<5 | isempty (maxit )..3 SECANT As in Example6.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.pl . 8 . if xr -= 0. break. r 3 1 ( 2 * p . 2 5 l m ) * 4 ) ^ 2 .81*m/0.

r).it providesa nice meansto attain the efTiciency Newton-Raphson of without having to computederivatives.r) is not requiredto changesignsbetween of the .f -./(xi-r) .(-4.an alternative approach involves a fractionalperturbation the independent of variableto estirnate.ri) dri where 6 : a small perturbation fraction.4 Ml The but ofb fepr l'(xo) : -4.the derivativecan be approximatedby a backward finite divided difference: f (xi-t) . 3 9 9 3 1 ( l:€ .xi 6. (6.Notice that the approach requires two initial estimates x. However.6)to yield the following iterative equation: ri+l : - "tr 6x. the into Solution.f(xi+lxi)f(./(.1 (6. is not classifiedas a bracketingmethod.ro*dru:50.57938708 I .f estimates. 8 l / s 2 .00005 Jl asi alw cha nifir rese met 6. Use the modified secantmethod to determinethe massof the jumper with a drag coefficientof 0. der EXAMPLE .f(x.f (xo+ 6xo). whe .r. ) .This approximation can be substituted into Eq.3 8 .8) is the fbrmula for the secantmethod.579381l8 .8) Th Equation(6.because (.25 kg/m to havea velocity of 36 m/s atter4 sof bungee f r e e f a l l .57938rI r8 . it Rather than using two arbitrary values to estimatethe derivative.5 6 M o d i f i e d S e c o n tM e t h o d Problem Stotement.For thesecases. . 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. (6.. (6.9)yields First iteration: xrt : 50 ./'(. We call this the modified secan..57938708) I 0-b(50) -4.5r)- (-4.) ' { x i \ = .9) swa Eq.f(ri) xi-t ..rso ROOTS:OPEN METHODS inconvenientto evaluate.As in the fbllowing example. 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 .l'(x.6) to yield the following equation: xi+t : xi /(xi)(xi t xi) .t nrcthod. l % oe o l : 4 3 . Se This approximation can be substitutedinto Eq.f (xi + 6. 4 V o ) 1 l.57938708) whe Nott = 8 8 .ri) (6. Inserting parameters Eq.-4.

3993l : .rr:88. x0 is the initial guess. 124.tr : 88.l. g general Further. univariate functionis rnerely entitythat returns a single value in return for valuessent to it.08910( | : The calculation be continued yield can to i 0 .4 MATLAB FUNCTIONz fzero The rnethods havedescribed this pointareeithcrreliable slow (bracketing) f-ast we to but or (open).69220't1t) r6 l3.1. but possiblyunreliable The MMLAB f zerc-r functionplovidcsthe bestclualities of both. 3 9 9 4 0 .-1.F 6 . J is too snrall.For suchcases. example. Perceived this scnsc.f(. methodcan be If the swampedby round-oft error causedby subtractive cancellation the denominator in of inefTlcient evendivergent.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 . if chosencorrectly.functionsare not in like theone-line alwayssimpletbrrnulas equations solvedin thepreceding in examples this For chapter. 4 M A T L A B U N C T I O Nf: z e r o l5l Seconditeration: . e r o( f u n c t i o n .r-r): -1. 7 6 14 2 7 3 7 a letl.l7c: lo.rr+dxr) . . lxa xl ) ) where x0 and x1 are guesses that bracketa sign change. functionmight evenrepthe program./(. its m<-rst in a an sense.6922017 1)(t) -t.If it is too big.3993 1.l :28.The f zero functionis designed find the realroot of a singleequation. Eq. sorne In cases.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 . r 1f d r 1 : 8 8 .16a/") je. (6. x a J where funcrjon is the nameof the functionbeingevaluated.39931 . f unctionmight consist nranylinesof cocle a of thatccluld takea significantamountof execution time to evaluate. "/" 64971 38064 I3064 t 53B o o2t 4 l x lO' 3 4 x l0 ' le.9). secant resent independent an computer and modifledsecant the methocls valuable. T" 43 438 28 762 II 746 I 5t7 a a2t 4 I x lO '' The choiceof a propervaluefor 6 is not automatic.692203516 10-6(88. are 6.(. the technique can becorne Howarrd ever.69220111 . and Note that two guesses that bracketthe root can be passed a vector: as f zero (funcLion. sirnple to A representation its syntaxis of f z .6922035 .

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 . in Fig.e. .6 Pr< . : 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. 1..0) F C 1 rh x3 the an is doesnot occurbctween two guesses.4) f :ero(tj (x) x^2 9.1-6(t. r : .: x fzcro((4(x) x-"'). firstperThis search from lhe incremental search forms a search identily a sign change. intelsection thisstraight quadratic The inverse interpolation line with the. to difl'ers in starts the singleinitial guess then at and takes described Section in 5.r1. ir. To find the negative root: Clearlytwo rootsexistat >> x = fzero(Glx) x3 If we warrt to find the positive root. : I 1 r u.r52 ROOTS: METHODS OPEN for Hereis a simpleMATLAB session solves theroot of a simplequadratic:.8rr.Lhc clif fcr in srgrr. the (i.l(1) x'2 !). bisection inrplemented is until an acceptable wl et( thi EXAMPTE Th 6.r l' pr( So . it The f zcro functionworks as filllows. ... 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. errormessage displayed Also. 6.r'axisrepresents new root estimate.1-4 4l) IS | .9.1 'lhe' f unct-ion val ues.-1. 6.|0 Ii) x^2'.g.f: 9 that -3 and 3. secant As the l'he of on computing straight a line thatgoesthroughlwo guesses. pa us c . ' t v . abad the lf lessan unacceptable root is obtained with one of resulthappens. we could enter twil guesses in >:. unare Thereafter. thatthe search directions until a signchangeis increasirrgly bigger steps boththe positive in andnegative detectecl. fast methods(secantand inversequadraticinterpolation) used the resultoccurs(e.8b).1. if a signchange o{ >.. r1 e ' n c l p o i f l l . lf a singleinitial guessis passed. 4) -r^2 9.the root estimate falls outsicle bracket). lrn'ersc quudrutic interlnethodancl inverse algorithnrs: secanl the poltttionis sirnilarin spiritto the secant rnethod based is method.a parabola) that cln a equation usesa similarstrategy is based computing quadratic but goesthroughthreepoints(Fig. f2c.

x A .. are any parermeters that the function requires.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 . Notc that il'you dcsirc to pass in parametersbut not u s e t h e o p t . l r ' . Whensetto'ir-er'displaysadetailcdrecordol'all theiterltions. . The opt imsct functicln has the syntax optiors o f i L r m s c t . e r c :\ f u n c : Lj o n . and p1 . R e c a l lt h a t i n E x a m p l e . J where I xr . . 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 . . at the command prornpt. fx] i z . .6 toofld.. 3 . f . l ' . ' . i o n s . optron.. technique the A morecomplete rcplescntation the f'zcrrosyntaxcan be writtenas of Ix.5.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 ' . . might bc expected. shiftsto the tasterrrrcthods. o l . 4 M A T L A B U N C T I O Nf: z e r o J(x) (al FIGURE 6.<] .tl .:2. 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 . is approached.s is a data structure created by the opt. Lolx: A positive scalar that sets a termination tolerancs on x. . An interactive >> opLions = op1-imset ('display'..( ' p .i rnsct. . a vector containing the root x and the function evaluatcd at the root f-x.J h w h e r e t h e p a r a m e t e r p a r .a s t h c v a l u e v a l . r ' r o l h etrh o n i n at As bisection typicallydominates first but asthe root thefastmethods.v.lmseL function. w e f b u n dt h e p o s i t i v e o o t o f . . p l . p 2 . t t i o n s . 'ite r').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 . w as MATLAB session be implemented fbllows: can Solution. v c t l . EXAMPLE The r 6.F 6 . 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. x ) : rl0 .I using the Newton-Raphson mcthodwith an initial guessol'0. The parameters that are cornmonly used with the Izer o function are clisplay.

a .998709 -0. on Suppose we would like to usea lessstringent that We tolerance.324 5e D11 0 --/. .98q128 0.sect i on bisectron bisecl-ior-r interpo laf.opl-ions = optim:.e1.9q9267 0.998554 0. y 2 u .I4 for a zero -0.options) >) lx.xl 1 0009 0. . B306ie 007 I.t .l.ion interpolaLion inl-erpolatlorr rnterlrolaLiorr i nterpolat i ni erpoIat i orr i on 26 21 2B '2 .after25 iterations searching.999454 Procedure initial search search search search search v n n r a fr E 23 24 25 Looking 0 . . f r ( .2572t I -0.5 c.14.0:3itsJ 0. L 141. ! a . 3i a .103511 . t "* a 2 x ' ' . . 1.141 interpolati.154 ROOTS: OPEN METHODS Func-count L 2 5 6 x 0. searcll search search 6 t 0.orr br.('l-olx'.52 4. 01Laarl 7. thenuses interpoto lationandbisection until it getscloseenough theroot so that interpolation takes overand rapidlyconverges the root. 4.24\21 :.Q 30 31 3'.0 .385007 -1 2. of It fzero finds a signchange.0.9P.s the 6.90tr318 1. = tzcro(G (x) x^10 1. ' + o u t .00001 I If n( H U de let wl no 34 l1 Zero found ir'l- VA it'-rv.9993s1 -0.95254E 0.5.. a 1 ertJ the MI tior foll .6BlB5e-005 -1. .47I r-L6 f (x) -0.. 1e-l).61 2636 O.5 POTYNOMTATS Polynomialsare a specialtype of nonlinearalgebraicequationof the generalfbrnr t + | .1442.rl: eig we 0 Thus.14 I.14122 in l-he interrral -1 0. r ): d 1 .999023 -4.. a.48 0.626056 o .998894 1.0090 Di {.485358 4.5r4r42 0. canusethe opt imset functionltl set a low maxinrumtolerance and a lessaccurate estimate ol'the rootresults: >.

fbr this task. Polynomialshave many applicationsin engineeringand science. nomial. In general. eigenvalue we we will merelyprovidean overviewhere.in many cases. However. alongwith someotherrelated following example. (l\ Ul (Irr Al ( 6 . of the roots functionconsists merely settingup the companionmatrix and then using MATLAB's powerful eigenvalueevaluationfunction to determinethe roots. It has the usefulpropEquation(6. ful applications in characterizing dynamicsystems-and. rootscan be real and/orcomthe the plex. r .both real and complex.6. includereactors. structures. l3) is calledthe polynomial's Thus. many (but not all) cases.12) fiom the right-hand 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. described the are tion. Suppose havea polynomial we . 1) | (6. So hclwdoesthe roots functionwork? MATLAB is very good at finding the eigenis the task as an valuesof a matrix.For suchcases. approach to recast root evaluation the problems problem.13) r:ompanion matix. and the a's are constantcoefficients. the algorithmunderlying are erty that its eigenvalues the rootsof the polynomial.they are used extensivelyin curve fitting. r 5 o 2 x 1+ . . coefficientswill be real.an nth order polynomial will haven roots.one of their most interestingand poweris linear systems. all However. However.5 POLYNOMIALS r55 In where n is the order of the polynomial. engineers fbr like are simpletechniques bisection and Newton-Raphson not available Unfbrtunately. 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 .1 MATLAB Function: roots If you are dealing with a problem where you must determinea single real root of a polymethodcanhaveutility. x. mechanical devices.Consequently. electrical and circuits. polynomials. / t . The roots functionhasthe svntax. . Its applicapolynomial in manipulation functions.the techniques suchasbisection andthe Newton-Raphson desireto determine the roots.Because will be describing eigenvalue Iaterin the book.. roots(c) the wherex is a columnvectorcontaining rootsand c is a row vectorcontaining polythe nomial'scoefficients. roots function.in particular.5.MATLAB has an exceldetermining the rootsof higher-order all the lent built-in capability. Examples 6.For example.

b) Bqe man of l' rang with the resultbeinga quotient(a third-order polynontial.0 .5.r56 ROOTS: OPEN METHODS We shouldnote that roots has an inverse po1y.5 1]) 1.r : I.5.0 .5 Note that this polynomialhasthreereal rcots:0.3 .7. 1 0. We i I r I i ' We X: . 2 -:5 . 2 .. We can then proceedto manipulate polynomial. 1 2 5 i l ) .5000 1.3 .r] = deconv(a.251. 2 5 : l( r 2 ) + t).( Be )'> cie where r is a column vector containingthe roots and c is a row vectorcontainingthepoly. r .. r* l ) = .5()00 (r.0000 Note that the pc.51. nonliai'scclef ficients.r ' ' .0. Polynomials enteredinto MATLAI) by storingthe coefflcients a row are as \/ector. by ryping :> p c )l y v a l (a.000r1 0 calle calcr . example.1y function can bc useclto pellirrrn tlre satle task as in -: D- b = poly([0. l ) 5 . 1 5 ( t t + 2 .will returnthe polynornial's coefficients. u 7 5 . 5 . ir la) Fin i i .. 1 2 3 . w i t ht h er e s u l t .1) r / ! ( x ) : .875 1.and 2: and one pair ol complex I roots: +0. 1 2 5 . + 1 . It can be entered into MATLAB as the vectorb: . . I ) wit fbu X 2. Use the following equation explorehow MATLAB canbeemto ployedto ntanipulate polynomials: 1 (E6. EXAMPLE . .1.1) cl- 1.5 ..3 . syntaxis Its '' '' l'1Y(r) 6.5a n d . 8 7 5 0 + 1 . 5 ( l ) 1 2 .15 2. Ftir entering following line stores coefficients the vector the the in a: -'' a = f I 3. r '* 2 .2 6 ' .0000 0 -4. ' / 5 x t 1 2 .5.5000 0. 1.: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 .For exarnplewe can evaluate at the it .\ i. l ) : 0 .500t) We can divide this polynomialinto the originalpolynornial by r:> [q.1 . 5 ) ( . 1.'l'2. Z . Solution.0.5 2. t B 0 .r. and a rerrrainder(r) . T h i s q u a d r a t i cs ( .0000 0.. r r * i 0. which whenpassed functionc-alled of the values the roots.. 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.

I ) are tbund: 2. the remainderpolynomial has zero coeffias can be determined cients.5000 againby usingthe poLy function: Finally. Scientists interested topics are of liquids and gases system..8'l!. 2.i2')A -1.otion calculatethe friction factor: o: +* 2orog U1#) (rr. to througha plant'svascular rangingfrom flow in bloodvessels nutrienttransmission number The resistance flow in such conduitsis parameterizedbya dimensionless to providesa meansto called the.: x I-.87!0 1.7. therootsof the quotientpolynomial >> X rootS(q) with the expected resultthat the remainingrootsof the original polynomialEq.l: 1 .A0 2.Now.Jal. b) . (E6. we can return to thc original polynomial _:: J l). the Colebrookequ. ln typical applications includethe flow many areas ofengineeringand science.2500 P IP E R IC T ION F in Determiningfluid flow throughpipes and tubeshas great relevance Bockground.15A() 2.0000 r.2500 We can thendetermine thc rootsof the originalpolynomialby all .. engineering.\O 3.lil t:tionJ'actor For turbulenttlow. (6.00rlrr 0.5011 0 ?. in throughpipelines andcooling systems. f .6 CASE STUDY 157 Becausethe polynomial is a perfect divisor.5000i We can now multiply c1 b to colrL-up with the originalpolynomial: by -> if conv (q.5000i r.0000 r.Jr-\1(X) -l.1) _\: .S(.14) .0000 2.0 1.5000i 0-!0t)(li t..0 t )( l r -3.1).000t) + 0. rJ000 1.0000 + 0.

t t. Thus.03.eitherof thebrack we eting methodsfrom Chap.t)0:r ( i .79x I 0r N . lJ. Hr di Ar gu fo . 7 4 3 Jlf ) J. an explicit formulationcalledthe Swamee-Juin equationprovidesan approximate estimate: f- 1. the Reynoldsnumber also serves in asthe criterion for whetherflow is turbulent(Re > 4000). In addition. D : diameter(m)..: r . .0000015 2.).Forthis case. z st l o This value along with the other parameters be substituted can into Eq. 5.. : 0.rl 0.! : 1. _ vl . r f r r i ( ' r r ( i ) ) f lrl. 5 could be used. could becomeprohibitiveif many evaluations but were made.0289678with a percentrelativeerror of error of 5. f {). i . Note that friction factorsrangefrom about0. 1 r (t i ) ) ) .. I 1(1.... and & : dynamic viscosiry (N.08. ill (.7 gives a value of/: 0.rrr As in Fig. r /0. 7 ( 0 .008 andx. * : : . the root is locatedat about0.005 : 13'743 R e.'tti i .0015 mm. liI . In this casestudy. l4) to give Er gc 0. : 0. .#)]' Solution. (6. .325 (6.t : l. . > : t .'"1.1t ['"(t.thin tube.:i | 1. it is advisableto plot the function to estimate initial guesses possibledifficulties.1. 0 0 s ) 1 3 . i ) ( lr l t i .For example. : . * \ . r l r .926 x l0{ in 22 iterations. (x/ Because are suppliedinitial guesses : 0..005m. (6."r.st \ g(J'l: -+2.f)0It)/'I(){)0. V: its velocity (m/s).theparameters p . t * l ) .we will illustratehow the numericalmethodscoveredin this part of the book can be employedto determine/for air flow througha smooth.l4). I(I) 1 r t L i t 1 .t58 ROOTS: METHODS OPEN continued (m).This can be done easily with MATLAB: and to anticipate Ilt.9. In addition to appearing Eq.0loel=-+=" \ 3 .the bisect function developed in Fig..they are somewhatinefficient. / [ l L t. althoughthey producethe corect result. and Re = the Reynoldsnumber: where e : the roughness oVD Re .008 to 0. slmz.:__ LL where p : the fluid's density {kg/m3t.23kglm3.f Before detennining the root.. V = 40nr/s are D and e :0. 6.23(40)0.. The Reynoldsnumbercan be computedas 1 oV D 1.sim2).1)il .08).( I ) t l*ilLl (rr.False position yields a result of similar precision in 26 iterations. ] { ). I l l t .This would not be importantfor a singleapplication.: . ' i ( ' 1 ' ) . ().'l I.l) 1 l .

14) is relatively straightfbrward differentiate. l( r. .l i . ' ' : rl rr I l.. As can be seenby inspectingFig.L .....1 .fi l iilt.. 6.05 0. 0 1 0.l r 'liril . only occurs .convergence when the initial guessis below about0. 5) _?L! " 0 . [e: 1 i\ i'\-iiii i i i 1 ..... this occursbecause function'sslopeat the initial the guesscauses first iterationto jump to a negativevalue.6 CASE STUDY r59 c o n tn u e d i ty ne \2 2 of tis t/s tn...ill-1 il .t :i' t i.08)..the newtraph functiondeveloped Fig..when the initial guessis setat the upperend of the range(xn: 0.07 0.1.08 f FIGURE 6..For example.06 0.9 We could try to attain improvedperformance turning to an open method.02 0.:irlt i ll ( l i r i rl ir.6...Lirll((i.Because by to method is a Eq.using an initial guessat the lower end of the range (ro : quickly: in 0. ilir l However.(6..... Further runs demonstrate that the for this case.\ i\\ r -2 H. r ).the routine diverges..7 converges )(..9.. r.Llir.066. 6...008)..03 0.04 0. t{I j ':.the Newton-Raphson good candidate.

) r . . r i r o r l i i r ( 1 r . t u.: i!.1 it l. . I / ( r I ( r ( r .r :i r r q :rccl r (j1r.25 ('"'(#.gn . 9 2 A ' . in this case. is The easiest and most straightforward versioninvolves solving for the first.However. 7 4 . 1 .l ) . 1 -L r L c ' ti . ' ..For the Colebrook equation. 6. c i rr l s { r J L i n c' l i o n . / R c ' 0 . .( i : )i ' r ) t ) / ' . 1 i l r r . t ) . ' l 6 1 1( t : r /{ 1 .j 1 i l j ( . l . / LI rr l . zero does f just fine: ' I I t. 9 ) ^ 2 r ). 1 | r c t r l .14): f. l ' .:rIl!t .016.15) to providethe initial guessas in Swamee-Jain ' I :l. f ' . 1 ./in Eq. ' Li I i i . a i n i l q . l ) -i J l l . l ) ! f )I { ) ir . . r:. ' r t .( ' . However.l r . s l a r .*#))' (6. r t I 'lilll . . : . si.let's seewhetherconvergence possiblefor simple fixed-point iteration. r n i n l .I 1 j. t f l ' l r f j I 0 1 ' ' I 4 4 {i i () i t ... ) r r \ . ( J l l l . .t60 ROOTS: OPEN METHODS ' continued So we can see that although the Newton-Raphson is very efficient. (6. . it requires good initial guesses. 1( 1 . .i . - 0.. )B ? . J L ' t t : t r . . l .the routine worksnicely.1 i 4 o n u tl b As a final note. I f ! t r f If the iterationsare displayedusing optimset (recall Exanple 6. 4 . r r . a good strategy might be to employ the equation(Eq.6). r r t r J . / " D ) + 5 . t r 0 r . L u e . c 1 r 1 .for singleinitial guesses aboveabout0. L . r r l I l . : a t r . 1 ) . . . . .16) .1 . l ': . For example. i J : .l . r .for the guessof 0... .00tlli ) \ t. l b l r II . 'r r c . guessesat the lower end of the range cause problems. . r r .rr / l L . just as with the Newton-Raphson method. However.. . ' I 0 I '. . Aside from our homemade functions. f ) .. h . r irj . r r . we can also use MATLAB's built-in f zero function. i r .1 . l I . it is revealedthata phasebeforea sign changeis detected negativevalue occursduring the search and theroutine aborts. r n L c r e d ( l .: 14 . divergence also occurs when f zero function is used with a single guess.08 that caused problemsfor Newton-Raphson. t . r rI I I l j l ) . l ! i . r i( l f L l r l l i omJtl r:r l : : . ' .. i . For example. ' L r r .. i :. r r c o i . ) . / ' ( i lr : o r r l . r .r p )1 .

it conbut vergesfairly rapidly! In fact. lg'({)l < l).10. estimates The take-homemessage fiom this casestudy is that even great. 6. addition.. . Recallthat fixed-pointiterationconverges when the)2 curve hasa relativelyflat slope(i. this simple approach that requiresonly one guessand no derivative performsreally well for this particularcase. for initial guesses anywherebetween0. fixedpoint iteration yields predictionswith percentrelative errors less than 0.08 meansthat not only doesfixed-pointiterationconverge.0087oin six or fewer iterations!Thus.IO The two-curvedisplayof this functiondepicted indicates surprising a result(Fig.008 to 0. the fact that the y.Sophisticated usersunderstand strengths the and weaknesses the availablenumericaltechniques.04 0.08 "( FIGURI 6. As indicatedby Fig.they understand of In enoughof the underlying theory so that they can effectively deal with situationswhere a method breaksdown. 6.08.6 CASE STUDY l6l !t= s6) 0. 10).6.008 and 0.e.professionally developed software like MMLAB is not always foolproof. curve is quite flat in rhe range from/: 0. Further.01 0.there is usually no single methodthat works bestfbr all problems.

ti: 0.. l'inaliutswer.t1 1 Provc that this firnnula is bascd on the Newton-Raphson algorithm..rgiven and Y6 : 6. 7 . Usc an initial-{ucss -rir: 0.18 Figure P6. ) nlethodand (b) lhc nrocli6. .. : l.Pcrtirlntthe conrputation unlil e.l t ) 2 .l) l() gct Eq. t ) r' . r l | 0 . a 6. . 45tt0kPa arrd7.ra 0.2. 6. f .ti luf . ils thc (wo initial gucsscs.t 12.6. T . . polynomial / (.\' whcr-c is thc rcaction's K ccluilibrium constultandp.l : 0./ (.l6a. lbr approxirrating :iquarc thc root ol'any positivcrrulttbcr can be firrrlulatcclas .5 -r.r has bctwcenl-5and20.nl\ w */\ .05) to clctclnrirtc r-oot .18showr tor. 9 .16 A catenary cable i points not in the sarr Fig.1) 1-. and.8r * 2. t 1 . (d) Using tlrc rnodilicd scculrt nlethorl (llvc itcllti()n\.5 . (86.(b) Did thc nrcthodexhibit convergence for on(o its r:al root?Sketchthe plot with thc rcsults each iteration labclcd.1. of dctcrnrinc valr. (c) Dctcrnrinc thc rootswith MATLAB. u s i n g n r n i a aud tial gucss (a)..6 : (a) Graphically..r'): a ./ (-r) : 8 s i n ( . as Tcst thLEq. 5 . r*ru u . r .I3.0u66R l). whcrc 7r.10 Thc "di"iclcand avcragc"tlctlrocl.284"rr + 6.0(X) kPa.h : l L V = l l r c . (H:0) proccss. Calculus can be employ height of the cable y as a Ta .9 Enrploy the Ncwton-Rlphsonnlcthod to dctennincr c a lr t x r tf o r .t) : -.ll DiUcrcrrtiate (86.-5. (K).6 Dcvclt>p M-filc lirr thc sccantnrcthod.I + 6 . 1 6 .. i l n d .r ): : s i n ( v 4 ) . 1 .r 1-. its weigh length along the cable u scction AB is depicted in the tension forces at the e cal force balances. splits irpart. 12 Thc -1--5 a rcal ro<./ y : _.2).. un 6. wiitelvapor 6.1 6 . Explain-your Along with an 6..0.9-5. Thus.2 andtakea minimum of of thrcc itcrations. w valuesof/ such that 1 : 6. r - .13 In a chcmicalcnginccring that is hcatcdto sLrlTiciently tcrnpcraturcs n significant high portion of thc watcr dissociatcs. rnethod thet'unction to 6. thc by ccluation statcis given ol 6./ (.you arc askcdto dctcrnrint: ilmountof methane thc that can bc hcld in a 3-rnt tilnk a( a tcmperirture -50'C of methodof with a prcssurc ol'65. (d) Using thc rnodificd sccant rncllrod (thrc'c itcnttiotrs.7 Dcvelop an M-l'ilc tir thc modificclsccant nrcth()d. of Eq. (lt) Using the Nervton-Ruphson (thrcc itctirtiotts. . the fol ofthe cable can be derive d'y --::dxz w TA r:(t:I lt)JT gas whcrc R : tlre uniycrs:rl constrntl:0-5ltt kJ(kgK)1.1.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 ?".3 Dctcrrnincthc hishcst rcal root ol . : 0. 3 . old-timcInctho(l zr.l(r): tanh(.DiscLrss usegraphical of and to in and analyticalr.-5 lncl iterate 6.ll (a) Apply the Ncwton-Raphson . r r l l 6 . (c) LIsing the sccantmcthod (thrce itcrations./ (. l ( .'1.13.r : -1. P T c s ti t b y s o l v i n g r o b . Thc parameten ir tr irntllt arc calculatccl by .4 Dctcrrninc the l()wcst positivc r(x)t ol' .M. As a chcrnical fuel ncer.1 I L \T / J /r r Dctelntinegiven : 2 nr. (b) 4..r -.3.3-5. t i : 3 .5 Usc (a) the Ncwton-Raphsorr (3 ol ticclsecant nrcthocl : 0. r .. 5 . 0 5 . / ( .\' Tr\ .E x p l a i n o u r r c s u l t s . -1 +(. Along with the initial gucssand the perturbation pass I'Lrnction rn argurncnt. ttrctl. in cylindu 6.(\l(tr./^r f Y l (P6.I : (a) Craphically..tod .5mr.Usean initial guess . :0.05%. (kPa). it is subje weight... .14 Thc Rccllich-Kwong R7 ''t-lt 6.162 METHODS ROOTS: OPEN PROBTEMS to thc 6.. 6. thrr Ncwton-Raphson 'r. passthc l'unctiorr an argunrctrt.Ola/a.rc : engi.andl': 8. .43.t : 0. suchthat -t:50. 3) .c- . 3. : (). .1 Enrployfixed-pointitcration locate root ot . (b) Develop a plor of v ) 6. Also checkyor-rl 6. fiirclion. 3 5 r + 3 1 ..Usea rtxrtlocating your choicc to calculatcl and the-n the of dctcrnrine mass rncthilnc containcd the tank. the If it is assLrnrcd this is the only rcaction thut nrolctiaction-t of HlO thatclissociates bc Rrpresentedby can I A:.-5using-i'11 -5.17 An oscillatingcurre bY I :9e-t cos(2tt)..o.r.0.rc .o .21. totalprcssurc thc rnixturc.1. 9 .0074.L : 5 rnr. (b) LIsin-c mcthod (thrcc itcrations. is lcssthane. J: 0 .01 . tll 6. s{ ( ' . 0 1 7 5 .} / r . /r -..ucthods cxplairtany pcculiaritics y'our rcstrl ts. r .3.4)7 R r r ': ' 5 l).rl + : l./ i t ' . 3.t:t5 . 6 . r r +1 0 .f) : 0. it by solvingProb. 6 u 7 5 r * usirrg an initial gucss ot' . 5 . ^ of until t'.183. = l9l K.: 3 5 ) . < 0.:3'5)' (c) Using thc sccantnrclhod(thrcc itcrations. i ) : .r'.r2 9) to evaluatc its known real rootat : . I .5.J atnrandK . pressurc 7 : afrsolute tenrpcrnturc / : absolute luncl : thc volurncol a kg ol'gas {rrr/kg). t.-5t125 ancl t.r thatsatisl'ies (P6. t i : 0 . rcsults. isthe ll'7r.t) : 0. t 5.0.t1.2 Use (a) fixed-point itefltion antl (b) tlrc Nc'ur'lonRaphsonmcthod to cletcrminca t'clotoi . and a capacitor in par to expressthe impedance .rru)cthod thisfunc(ion to usingan Apply thc Ncwton-Raphson y o 1 i n i t i a ls u c s s f .4. filnnoxyo| to (H:): gcn (O1)anclhydrogcn H:OP H: * lO: involved. P6. l . 0 1 ) .5 .0. .

l6D.its weight acts as a unilblm load per unit along lcngth the cable ur (N/m).-5 H. is subjectto no loads other than its own it Ihus.r': I 5 at r. @t +(.19 .where/ is in seconds. where T1 and Ts are AB in tension lbrcesat the end. 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. An oscillating current an electric in circuitis described l=9e-' cos(2rt). and R L : 0.Kircfihofl-'sru/escan be used in theimpedance o1'thesystemas I ll p: (Q).0.l6u.6 .a b l o c k o f m a s sr i i s s .I6 P A cable is one which is hung between tw<r 116 catenary pintsnot in the same vertical line. P6.s r=50.18 showsa circuit with a resistor. Basedon horizontaland vcrtiforce balances.q and = $. P 6 . A lree-body diagrarnol' a ion is depicted Fig. FIGUREP6.r : 50 to 100. fbllowingdif l-erential 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. (r.I8 #=.1 . As depicted rn P6.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 .c- FIGUREP6. Drvelopaplot versus-r of. inducan acaoacitor oarallel. suchthat the cablehas a height of .. Deterrrrine all oftsuchthatl:3 Figure P6. l0-r' F.PROBLEMS r 63 (b) FIGURE 6.FfS canbe employedto solvc this equationtbr the ofthe cable asa lunctionof distance.y fbr. 1 9 .r: l 7. C' .

r )= ( . that of thrown by a right ficlder is clefinedby thc (x. and R : t h e t a n kr a d i u s m l .4 ) ( .r | 2 ) ( .ith the traicctory of a thrown ball.5(sH)''' rt(B i 2H Jtlt where B : flow (m3/s). A transf'erfunction for a robotic as displayed Fi-e. r 3 ) * ( s * D r ) ( s* b : ) ( .The resisa I tancefbrce F of the spring is given by F--\krt! *k.tt- = N(. 6. yor result for Reynolds numbe e/ D ranging from 0.r*66 l 9 s r+ 1 2 2 s+ 2 9 6 s.l ) ( x . C(s) : systemoutput. polynomial ft and /r .S : slope (mlm).n : 9 0 8 . fespectively. . Determine the approximate relatir"e cror after cach iteration.A relatecl problemdcals 6.S:0.d3i'1 FIGURE P6. 6.(tan0o)-r -j5r' Ltrai cos-u0 Usea rootlocation techn this function.0002 your schemeconvergesf< equal to zero.r64 ROOTS: OPEN METHODS P fixed-point iteration sch givenQ:5.2| You arc designing sphcrical a tank (Fig.25 Seeifyou can deve the friction factor base describedin Sec. l s ( x ) : ( . Thc traje-ctory o1'a ball devr'loped mathematically rclatethc dynamics asys. and s : Laplacetransfbrmcomplexfrequency. and the the distanceto the catcheris 3-5 Note that thc throw lcavcsthc m.l 1 9 2 2 F-ind appropriate initial angledo. k 1 t l '". initialguesses of0 and R will bracket a single root ti)r this cxample.2I Employ initialguesses ( of results.N(s) systcminput.2I ) to hold water fbr a small villagc in a devcloping country.5.2-l The Manning equationcan bc wriften fbr a rectangulat openchannel as (^ : =r . initial guessof R will alwaysconverge. . transfer tunctions ae u.4-5 m. Also. r+ . r* 5 ) ( x . right ficlder's hand at an elcvationof 2 m and the catclrcr rcceivcs at I m.r'1y C(. 0 0 0 g / s 2 . r* h ) ( s * l t t ) .. and tor.22 Pertbrm the identical MATLAII operationsas thost valuc-s: : ft1 Solvc fbr z/. ( : 9 .x "-ns^ 4 Conscrvation ofcnergl'can bc uscdttr show that (a) infbrmation: For hracketing methods. l If R : -l m. and nominatorand t:rctorthescinto the form G ( . 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 . what depth rnustthe tank be filled to so that it holds 30 m3? Use three iterations the most efllcient of nurnericalmethodpossibleto determineyour answer.0. r. providejustification fbr your choice of method. s a r ) ( . an {): + . l and n : the Manning roughncsscoefficient.27 Given f (x) : -2xb .26 Use rhe Newton-Ra f (x) : FIGUREP6. z : 4 0 g / ( s 2 n r s ) .if urr:20 m/s.6. The volume of liquid it can hold can bc computed as l / n l t ' - = where C(s) : system gain. it 6. Develop .Extra wherc zri andb1 : the rootsof thc nume-rator denomina.2-1 ln control systcrns analysis. 6./r : depthof waterin tank Im]..0000 6. s + d 2 ) ( .Perform iter tive error falls below5Zo *.7 to manipulatc and iind all the roots the of 5 0 .t) r'u + c(s) rt+ l2.llR-rl 3 wherc' V: volunreIm]1.5sl*-50.1.(b) Fot 2krtl5l2 I open mcthods.. 5.20.n t g < l .1. given thr: fbllowing parameter in Example6. H = depth(m). P6. 8 1 m / s 2 .2O released distance above a nonlincarspring. Use MATLAB to find thc roots of thc numerator de.20 Aerospacc enginc'ers somctimes computc the trajcctories of projcctilcssuch as rockcts.t t t q l t tl 6.7 ) 6. in P6.) coordinatcs tem's input to its output.5 1 .

B :20. If you use the Newton-Raphson the modified sccantnrethod. Ifyou usethe sccant tial guess method. .f(. (b) 6. fiom to 6.r)-. techniques known thatfi:rr except all fixed-point iterationwill cvcntually converge.If you usc a br'ackctingmcthod. : 1.0 and -r. is choose technique the that is bestsuited to this problem. : l. you use a bracketing method. 6J?Given -Zrb . I : 0 and Xi : l. r65 of use initial guesses x1 .26 the Nervton-Raphson Use method to find the root of :'@_ x)-2 . a .usc initial guesses r1 : [) of and.r. ?"-^ = )-l-r justify your choice. 6.6.00001 0.use an initial guessof .0.use an inior of.l.03.0002. 1 :0 and 0 JT ie le re S- ic v. Perlbrmiterationsuntil the approximaterelative enorialls below 5%.Justify your choice.-5. t0 6.r.6.r.Perfbrm iterrelative If ationsuntil the approximate error fallsbclow 2olc.Note that it is then to the positivcinitial guesscs. If you usc thc secantmethod.Provethat 0 your greaterthan or scheme converges all initial guesses for equal zero.rr function should return a precise rcsult Reynolds numberranging from .PROBLEMS fixed-point iterationscheme to solve this equation for H given= 5. and (c) 8. If you usethc Newton-Raphson the modified or secant mcthod.r.r4 lox * 2 * f (t) = (he Useroot a location technique dctt'rminc maxirnumof to this function. Lrseinitial guesscsof . Yor.: 2.211 You must determine the root of the following easily f di fferentiable unction: n <.7. usethal technique determine root.. Assumingthxt converof gence not an issue.-tr Employ gucsses (a) 2. initial use guesses x.and Pick the bestnunrcrical tcchnique. )..1000to | 07and lbr for e/D ranging 0.25 if youcandevelopa lbolproof function to compute See fie frictionlactor based on the Colebrook equation as described in Sec. S : 0.05. Explainyour initial of rcsults. and r?: 0..

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 . T) tr . Recognizing difference the betwecn one-dimensional multidimensional and optirnization. Specificobjectives topicscovered and are ' t ' ' ' ' ' ' ' t Understanding and whereoptirnizafion why occursin engineering scientific and problemsolving.driu e n s i o r t a u n ci t r t . n object like a bLrngee at If is subject Iineardrag. Beingableto developMATLAB contourand surlhce plotsto visualize twodimensional functions..Opti mizqtion CHAPTER OBJECTIVES The primaryobjective the present you to how optinrization of chapter to introduce is can be usedtcldetermine minirrra and rnirxima bothone-dimensional of and multidimensional functions. Knowing how tclapply the f minse.its altitude a functionof time can be computed to as as I ( f I t66 -ry. Localingthe optinrum a single-vnriahlc clf functionwith thegolden-section search. 'l 7. t-- .e I(tn'I)t) (. 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 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. Locatingthe optirnumof a singlc-variable f'unction with parabolic interpolation. g Bcing ableto definethe goldenrati<l why it makesoneand understand dinrensional optitni zatione'flicient..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 .. * Z: zr* 1 (.

t l a pnrblerls that uttcnrptto balarrcc pellirrnrancc frontingoptirnizittion and linritatiorrs. No(c that (irr tlris liurnulation" positivcvckrcityis corrsidcrcd bc in thc rrpwlrci tinrc to d i r c c t i o n . In adclition. optimizationproblenrs requirc nurnerical. 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 . substituted its back into thc functionto conrputc optinralvalucs. .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 . 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 .( : a l i n c a r d r a g o e l ' l ' i c i c( ktg / s ) . r : i n i t i ' r lv c l o c i t y r n / s )a n dI : ( q ( . l o : t h c i n i t i a lt l t i t u c l e e s a ( m ) . l O 0 m .optinri:/.1 : t l Ok g . to perspective. Root thc is in Iocationinvolvessearching thc locationwherethe I'unction for cqualszcro. 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 .2. optirniz. a n d r ' : l 5 k g / s . involvessearching the function's for cxtrenre optimiz-ation .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.Z.t\l:u c s :{ l r r . cornputcrsolutions.l r : r n i l s sk g ) . ' o .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 .ation dcirlswith l'indingthe nraxinra and rninimaof a l'unctiorr dcpends one or m()revariablcs.rc li l h i r n i r t t r o l r t . 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.7 . . 1 INTRODUCTION AND BACKGROUND In thc nrostgcneralscnsc. 5 ancl tilr The fundarne'ntal dif'lbrcncc between two typesol'problerns illustrated Fig. That is.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 . l .1 E l e v a l t a no t q f u l l a l i . Frour a nrathcnraticirl optinriz. 9. 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 .atit'rn sinrilar in spirit to lhe root locationrncthods just covercdin is wc Chaps. .E c 1(. scientists Iravcintcresl optirnalphenorncna irr firlnr thc pcukclcvationo1' rurnging projectiles the minirnunrli'eccncrgy.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 ) . c n (s). 1 1: r 5 .7. . both involveguessing searching a pointon a function. 5 r n / r . T . that on The goal is to detcrnlinc the valuesof tlre variables Thcsecan then be that yield rnaximaor minirnalbr the I'unction. r / s r .I INTRODUCTION BACKGROUND AND Maximum elevation F'GURE 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 . s ri ' A t d i s p l a y c d i n F i g . " . s c r r g i u c e r w.T h L r sc n g i n c e r s r c a l w a y sc o n . p<lints. From a numcricalstandpoint. introducc is 7 . 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 : . ln contrast.':. and6.

Note that because .this corresponds the .Forthis case.t'"(x) < 0.L l. 7. andr: : l-5kg/c.s \ . to the this can be accomplished settingthedcby rivativeto zeroand solvingEq. E q u a t i o n 7 . 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.In mathare enrirtical terms.r). u0 .: l5ts5t 8o r n/ r r \ :3. the point is a maximLlm.r) > 0. ( S o l u t i o n . (E7. you can diff-crentiate function thc andlocateth root (thatis.) 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 .r166s 80r9.r) 0 TT FIGURE .ldt. by solvingthe root problenr: .2.8. the zero)of the ncw tunction.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. 0 . indicates whether optimurn a rninimumor the is .r value whcre thc derivative / '(. 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.1 a maximum:il' .Thus.Ll-. (l - e- i'l"'t'.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.81)/ l. . min n : D'e \ ( / t t t) t - tn8 (.168 OPTIMIZATION l'('t) : o l"(r) < o l(x) : o T m /'("t) = o > "f"(.-55m/s.:g : 9 . nt : u0 kg. second thc derivative. "(.theoptirnunrs thepointswhcrethccurveis flat. to t0 Additionally. Deterrnine tirnc and magnitude the il1'thepeakelevation basedon r E q .o : 100m. understanding relationship thc betwecn a ble strategy finding the latter.r1 EXAMPLE .f'(.r) is equal zelo. this is actLrally ccluation the vclocity. 1 ) . 8 1 m / s t .ln fhct.someoptirnization rnethods justthis do : 11. for That is. the pointis a minimun if rootsandoptimawouldsuggestposiNow.l'"(.the problen anrounts deterrnining root. 1) thc fbr Themaximun elevationoccursat the value ol I that drives this gquationto zero. .

) ( t _ .d i m e n s i o n o l l i m i z o t i o n . 8 1 r 6 . couldhaveobtained fbr we the sameresultusingthe root locationmethods described Chaps. variety as a of directnumerical optimization methods available. L3a. . 8 3 t 6 6 ) : 9 2 . r ) ( b )T w o . Althoughan analytical solutionwaspossible this case. FIGURE 7.1) to computethe maximum elevation as z: R 80 / 8 0 r Q lr \ 8 0. possible approach Althoughit is certainly to optimization a rootsproblem./ . . _ . are These methods available both are fbr problems.. in Fig.u t ) € . the result makesphysicalsense sincethe acceleration shouldbe solely equalto the force of gravity at the maximum when the vertical velocity (and hencedrag) is zero. This will be left in 5 as a homeworkexercise. the name implies.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 . | 5 0 + " " . n \ t "ntlt ln .l("r) . 1 0 0 + : . (7. / ( .. .. " . t t .: (llc . .' t/t. r . r 9 . Further. . n .m .. the As search thenconsists climbingor desccnding of peaks one-dimensional andvalleys.one-dimensional one-dimensional multidimensiontrl and As problems involvefunctions thert depend a singledependent on variable.9^ 8-1 . t . . 3 8 3 1 6 " "1 _= " .( t-/(' .1. 1 58 0 . g 6 0 9 1 m 15\ rs /' ls We can verify that the resultis a maximum by differentiatingEq.u'r Minimum.1)to obtainthe secondderivative d2z -.r . 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'. and 6. (E7.INTRODUCTION AND BACKGROUND This value along with the parameters then be substituted can into Eq. The fhct that the secondderivativeis negativetells us that we have a maximum. .. Multidiproblemsinvolve lunctionsthat dependon two or more dependent mensional variables.

c represcnts very best solution.a two-dimensional optimizalion againbe visualized searchingout can as just as in realhiking. golden-section 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. 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 . 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 . the lett rolr Euc whe Mul wher q 'l FIGURE 7.is betterthunits inrmecliate neighbols.4. .we will describesome of the mole common approaches onefor dimensional Then we will providea brief description how MATLAB optimization.the process findrng a maximum versusfinding a minimumis essentially of -/(. 7.ln suchcases.7 . ) . brackcting approach-parabolic polation.3d. Similarly. optimizrtionin one dimension be dividc'd bracketing into can andoperr methods. 7. we arenot constrained walk peirksand valleys(Fig.2 ONE-DIMENSIONALOPTIMIZATION 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 .-r i g h tr r r e o c o o p t i m o .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 .-v i c i n i t y f l h e o r i q i n . to a singledirection. of can be employed determine to optimafor multidinrensional functions.instead topography examined efTiciently is the to reachthegoal. In aft the pr( ofi 7.3b). Finally. will almost we always intercstcd findingtheglobal be in optimunl In addition. l ( . that includelocaloptima calld Cases are multimodol.r). Recall liorn Clraps.T h e l w o p o i r l s1 ot h . dcscribed thenextsection. 7. A gkhul olttittrtrm the not though th very best. 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 . In the next section. 7. Justas in rool location.d i n r e n s i o nr o l l e r coaster"-like lirnction dcpicteclin Fig.A local optinrunr.r70 OPTIMIZATION ln the samespirit. mustbe concerned wc aboutmistaking localrcsulttor theglobal a optimum. This identicalbecause the samevalue x* both minimizes/(x) and maximizes graphically a one-dirnensional equivalence illustrated is for in ftrnction Fi_c. In ad provir termir T roots i guess tween The rc xr Global maxtmum FIGUR Euclid s rotioof smoile Global F- 1- .-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 .However.

terminingthe optimumof a single-variablc lbl searchis sirnilarin spirit to the'bisection approach locating The golden-section hingedon definingan interval. In addition.) and. _ .-5. 'tt I x.3) 4.1) pleasing Western aesthctically in cultures. The goldenratio has long beenconsidered it includingbiology. is the greater the lesser. The positiveroot of this equation the goldenlatit'r (7.: l+\."The mostcomnlonof thesearethe ratio to z logarithme. was veriflcclby deterrninimg that.r.) tweenthesebouncls as The root was thcn estimated the midoointof this intcrval: '. was originally definedby ELrclid(ca.6 : 1 .1 Golden-Section Seorch qualities.2 ONE. 6t 8 0 ... 7. . are rnagicai quantities.r.For our pulposes. sirnple. the golden ratio shouldsurelybe includedin the panThis quantity. example.The prcsence a root beof a had difl'ercnt signs.As depictedin Fig. (7.DIMENSIONAL OPTIMIZATION l7l 7..2 termsyields and collecting o'-Q-I:0 is where@ : L r / ( z.5 so of on Euclidsdefinition the golden roiio is bo-sed dividing o iine i. of a circle'scircumference its diameter and the baseof the natLlral Although not as widely known." so The actr"ral valueof the golclen ratiocan be derivedby expressing Euclicl's definitionas t1IL2 Ir lr (': (7.it arisesin a varietyof ilthcr contexts general-purpose provides basisfbr thc golden-sectiorl rncthocl dcfbl the scarch."Beyondsuchsuperstitious well-known numbers that havesuchinteresting powerful mathematical thereareseveral and properties that they could truly be called"rnagical.51 FIGURE 7.2) Multiplying by t 1/(. r . in theWest certainnumbers ascribed For we In manycultures.2. (7.2. of or "A Iine is saidto havebeencut in extreme andmeanratio Euclid'sdefinitionreads: straight when.) that bracketed singleroot.Recallthat bisection guess(-r.rfolwo segmc-nls 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 .l(.)and an upperguess({. a function. by fee). 300 BCE) because its of leuer Q (pronounced.r 9 8 8 7 4 9 8 9 . specil'ied a lorver by rootsin Chap. (2- t-+l-.l'(. areall familiarwith "lucky 7" and "Fridaythe 13th. role in the construction the pentagram five-pointedstar.-5.as the whole line is to the greater to segment. which is typicallyreprescnted the Greek theonof remarkable numbers.

. intervalis shrunkto about0. Asimila would lre usecl thc alternate fbr cascwherethe optirlLrnr in the leli subinterval. clurbe clintinatcd becausc doesnot containthc rninirnum. If I ( v:) .-. .r./ (. can adoptthe samenomenclature for bisection. f-cll approach For this case. This is done we Eq. sinceitis comesthe new rr.r. andhence contains singleanswer. r t o t h e For leli cll'x.this was accornplished choosingthe midpoint. Fclrthis case.8% of itsinitiai the length.). We respcctively. thc caseillustrated Fig.87c). we were interested detenniningthe minimurn of function. goal is to nrininrize the evaluations rcplacing valueswith by old nerv values. t h e n .I (about61. this is a harcler with bisection achicved birt Droblem. (50Vo\.r.. needonly detcrminethe new.liorn -r. the intervalshouldcontaina singlenrinirnurrr. u..r : ) . example. fiom ol' lirr .For bisection. I t ' . can startby dcfining an interval a one-dimensional we that a That is.2 Gol< Prob 50lu .and4 is calleduninrodal. EXAMPLE 7.r. in beFor tbr lbr the new. 6 a . it the this casc. intervalcontaining extremum reduced are the Thatnleans In fact. on rud. r . l ( .6) with r/cornputed with Eq.r. 7 . by of or that after l0 rounds. in Now suppose that insteadof a root. as where. we would need two intermediatc occun'ed.. two intcrnediirte the ratio: . thc the and r. new.would bc eliminatcd. suchan interval.r):xu-d where .008or 0.r. conlrasl we for to bisection.r.r. hencea usinga singleintermediate to and function valuesto detcct whcthera minimum zero).rr) at Two results can occur: Thc functionis evaluatcd thcsetwo inleriolnoints.).1) ) Now. of However. n i n i n r u n r .. a s i n F i g .r.172 OPTIMIZATION The final stepin a bisection iteratjon involveddetennining new smaller a bracket.A kev advantage this approach of bounds.8) based the new valueso1'.rr). bscclmes new ..7).6. the sameas the l'unction To cornplete with thc algorithrn.rrwould be computed with Eq. d:((h .Forthe by golclen-section points are chosenaccordingto the golden sc'arch. r bccotnes new . in definedthe lower and upperbounds.rr to . (7. a n d t h e d o m a i n o f .r. thc ncxtround. then/ (. (7. we do not havc to recalculatc thc function all valuesfor thc next iteration.rll( . The key to makingthis approach etficicntis thc rvise choiceof the intermediate lirrrction As in bisection. replaced oneof the old as-l(r.Becallsc oliginal..r. (7.81 (76) (1. . points. needa new strategy findinga rninimumwithin the interval.0066% This is not quite as good as the reduction .l(r. Ratherthan value(which is sufflcient detecta sign change. l ( x. .)is the rrinimum andthc donrain r to thc rightof .lirr thc next round. This meirns that we alrcadyhavethe value valueat the old.Yt:..crechosenusingthe goldenlatio.\/ +d .\ thc (7.it is about0./had a functiorrvalue with the same was that the new valner.. ) i s t h c r .6l8r{) 0. the the is rapidly.7.to r). was This sign done by replacingwhicheverof the bounds-r1 or-r.l)(:r.theold"r.Afier 20 rounds. hereis thc neirl hcnelit {l'clm uscol'thc goldenratio. As the iterations repeated.As with bisection.l. l.eachroundthe intervalis reduced a 1'actor d ..

: 0 to jr.0) : 2. ls on )eis irh lar al. .6 (o) The initiclstep of the golden-section seorcholgorithminvolves choosingiwo inleriorpornts occordingto the golden rotio (b) The secondstep involves deflnlngo new intervol thot encomoossene oollmum. rl-d+xl St ..4121 :1.4'. Solution.2 ProblemStotement. (al rh le :n x2 -t1 t I I New (b) ne or )m rd.4121:2.2. tns :ial ron n EXAMPLE G o l d e n .2 0 i n r l within the interval from -r.721 rr :0 +2. Use the golden-section search find the minimum of to 1 s "f ( x t : . First.S e c t i o S e o r c h 2.rl FIGURE 7.61803 .n n S.the goldenratio is usedto create two interiorpoints: the (4 d : 0..4121 xz:4-2.: 4. Llv.5279 .2 ONE-DIMENSIONAL OPTIMIZATION r73 S n d )f rt e f.

53l0.rr.. Thus. alsoknow that . r .. r : 1 .(7.9443 T h e f u n c t i o n e v a l u a t i o n a t .ln adclition...1 . do nothave the we to recalculate on /(x1).[(xt1 .4)..r.4J21 .) a I 2 3 4 5 6 7 B 0 0 o 9443 4 s443 3050 30.sc) 3050 3 9 0I I 5279 o 944'3 5279 3050 5279 4427 3 9 0I 4427 24721 I 5279 I.174 OPTIMIZATION The function can be evaluated the interior ooints: at l(x:): I (rr) : 1. 5 2 1 9 ) : .1.] t . 1 7 2 1.r1). Because the interiorpointsale symmetrical. and..our best estimate the minimum at this point is thatit n focatedat r : | .100 2 4721 (l c. Alier the eighth i t e r a t i o nt. 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 ' . : / 0 .1641.Sincethisvalueislcssthanthe f u n c t i o n a l u ea t .1 .7)to computethe new valueof r/andr.. by The process be repcatcd.2 s i n ( 2 .BB85 ) 5279 1 66-s6 I 5279 I 4427 I 4752 40000 :tIt36 -o 6.5432 BBB-5 ) 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.2 s i n ( l.. . h u s .r. 4 1 2 1:) .0s 9 9 4 3 ) i. it was determined the previousiterationas l (.1 . optimumwill eitherfall inone is the of two intervals.5. 6 3 0 0 t0 Because of .. upperboundfbr golden-section an search canbe derivedaslbllows:Oncean iteration complete... 1 1 2 10 ) : 1. . 1 1 5 7 a t . Using similarreasoning. i on No Co Recallthat for bisection (Sec.r.r.rr. can with the results tatrulated hcre: or the Th xt J'$. 6 1 8 0 3 ( 2 . 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 . r ./r(.). 1 6 4 7 l0 2.5219 or es : rz :2..0 . : 2 . h a ti s . gol wo nuI l. -r. 5 2 1 9 ) : .5219with a valueof .412t2 .h e m i n i m u r n c c u r s t x : 1 .) 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 . it will be in the lower interval lf (.r.f (r) : -l .5279.for the next iteration.r. ..7641 addition.. t h e m i n i m u mi s l ( 1 .1 (. a n dx . t 5432 --17647 t 7136 t 7647 -1 7755 -t 7732 xu J'(x.it will be in the upperinterval(. b e c o n r ets e u p p e rb o u n d . the optimumfunctionvalueis at. All that remainsis to use Eqs. an exactupperboundfor the crror can be calculatedat eachiteration..). thatis..8) and(7. : l.| . t h t the fbrmer-r.ln we the minimum is in the intervaldefinedby -r.If optimumfirnctionvalueis at -r. lower the b o u n dr e n r a i r l s . and... . 1 2 1 6 .5279 0... valuebecomes new . 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 .r.5219) : -1. scribed rr.x.rr. 1 1 5 5 . I n a d d i t i o n . .: d : 0 .52192 . eithercasecan be usedto definetheerror.r.lOO 2 4721 B8B5 t.

i.. r ..d ) ( .).(Q l)(. r r ) Therefbre.. i > ) ' q .n.8317 lnii n 192.)...O ) l ' : ! .. (zt)+rnlc*(vO+m*!.7. .r. O .5 5 .l)(. 1 ) .t't (Q l)(x.. speedsavings Of thc would bc negligible. : : -{.r1.x i ) : (rr .. to for . :r..Therefbre.Z.I 0 l ) .-ri) : (r. a the fbr the golden-section searchfor minimizationis presentcd in An M-file f'unction returns Fig._. the maximumilistance or 0. Therearc cases search Many evaluations. may be calledmany times.:-TI -.r1) : (20 .However.l ) ( x . the The M-file can bc uscdto solvethe problenr frorn Example7. ) >xmin = c/m*1.7.)+ 2(0 . the This result can or 0. course. .the maxif imum distance from the estimatewould be Ar.. the true valuewereat the fiu'left.r.. . 3. f rn i ri . 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 .2361(-r.3)(..r. : ( 2 ....'nr I t. z r ) .: J lx t 0 0 % I r. wherethe golden-section algorithmmay be a part of a much largercalculation.86r) 9 th ne 6.The l'uncticln the locationof the rninimum.'. 79 1? l6 This cstimate provides basisfbr tcrminating iterations.the approximate crror.the valueof the function.r1).'c)*(I erp( :=Q(L) r: > | x n i n .r1) : ( 2 . 8 1 .x . .sl .artcl numberof iterations. v O . . thenbe normalizecl thc optimalvaluefcrrthat iteraticln to yicld to .1. .) ) nr*q. r u.:ra .r r ) .1.t..rr) -.r2 : . -'f .r'.. y. i'.r. e h a v ee n t e r e d h c n e g a t i v e f E q .1 e... it In kecpingfunctiort evaluations a minimumcoulclpay greatdividends suchcases..there are two importantcontextswhere mininrizingthe Theseare numberof functioncvaluations be important. le t .. fiorn the would be estimate A. i L c r I = g o 1 ctn i n ( z .*L) )7 29 7B il .2 ONE-DIMENSIONAL OPTIMIZATION Lookingat the upperinterval(xr.r. .{rr I . . If the true vaiuewereat the far right. . 8 6 0 9 .r.rr) .3820(..lor solvirrga single optirnization. case this would represent maximumerror..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 .+ ( Q . can l.D Q . r t I ( Q .. suchcases. : = 1 \ .... n t = 8 0. h You nraybe wondering why we havestressed reduccd the functiorr of evaluations thc goldcn-section sL-ilrch. ( 7 .9356c (l0'-r u.

"function" involvestime-consuming fhe model integration.he minimum of f % inpr'rg. use simple functionsil we most of our examples.end if nargin<5 isempt-y(maxit). xopt vl. . optimization can be usedto estimatr the parameters a modelconsisting a systemo1' of of diff'erential equations.error('at least 3 input argumenLs required').xu. while(1) xI).phi) * abs( (xu i-f ea <= es I iter >= maxit.. irer l.pI.e-. xl lower and Jppe' oLesses (dlefault . x2 = xu ..0001. es=0.3. FI ul 7.end p h i . 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 .\tararqirr{:}) < f (x2.iterj=goldmin(f. to For exarnple.3 $(AAAPLE Po Pr 7 Time-consunring evaluation.goJdrin lf .end wl op FIGURE .p2. For pedagogical reasons. if xopt---0.7 7 A" A/i.) % uses grolden sectjon search to find t. xl) / xopt) * 100.xl.-1)* (xu xI = xl r cl.'/a'or:in :] .end if nargin<4 rsempty(es).on of mrnrmum fx = minimum function value ea . co ca x2.ea. SO tic end iter=iter+1. ea = (2 .es. = additionaL parameters used by f % 9: ^rrrrirrts. t = name of functior1 Z oo r'l . Pa pr. s Sr .. .br:eak.vararcJin{:}) xopt = x1. end end x-xol'. maxit=50.rrgin.es. d = (phi. ( 1 + s q r r -( 5 ) ) / 2 .approxirnaLe relative error = number of iterations iter (?) if rr.xu. You shouldunderstand that a functioncan be very compler and tinre-consunling evaluate. ? ? ? ? x = locaLj. if t(x7.seclion eorcfr s 7.176 OPTIMIZATION r -n-r io-r Ix.le 13 jpr5.i x 'pL.p2.C.rminp the mrnimum of o lunciion with the oolden-.maxic. x1 = 7)..varargin) % goldrrLin: mj-nimizaLior-r golclen section search % : fx.[x.fx. iter-0.ired relative error (default = 561 % maxit = naximum allowable iterations p1 .x'.maxit. sucl For cases.0001?) % es = de:. fx.d..

8 7 G r o p h i c o l.r. v ii EXAMPLE 2.2.rthat corresporrds the tq o p t i r r r u n ta l u cu l ' t h cp a r .3 P o r o b o l i cI n t e r p o l o t i o n ProblemStotement. It can bc shownlhloughsontcalgcbraic rlanipulat i o n st h a tt h c r c s u l ti s rl:.):0 l(r.2.ancl it.fustits lhcrc is tlnll'ttnc sl.): -1. to Thcn wc candil'lcrcntiatc sctthc rcsultcqualto zcro. : s S o l u t i o n ..1 . r . y o i n t h r a c k ea n o p t i m u m w c . -A .g).-s829 /(r'.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. :. in Lex ate rch lat r v i t hi n i t i a lg u e s s eo f . Use parabolic interpolation approxirnatc rnipirnurn to thc ol' /(r) : rl lo 2sin'r l . -r. r ..l0l where-r'.):3.r True function Parabolic approximation of maximum Parabolic function FIGURE . .r) ncaran optimum(Fig. cltnl.ita parabola the points. and. a to of . . .1136 .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 .rl(7. a n d _ r . ancl iirc thc initial gucsses. 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 . .2 ONE-DIMENSIONAL OPTIMIZATION 177 Truemaximum ) . 7. solvelbr atr estitnale o1'lhcoptimal-r.l(r.0 .i 1 ' w ch a v ct h r c cp o i n t s h a t . a h o l f ct t o t h eg u e s s e s .rd-ordcr polynornial olien providcs good approxirnation the shapc l(./(.2 Porobolic Interpolotion Parabolic interpolation tltkesadvantagc ol'the lirct that a sccor. h t ly t .rr is the valueo1'.raight corrnccting points.

55 : 1. 1 l 3 6 l 2 ( 1 . the next iteration: lbr rr :l r: : I. a strategysirrrilarto the golden-section searchcan be employedto determine which point should be discarded.7757 a t . 4 2 J 6 .( t . 5 8 2 9 0 1 [ ) 2 ( 1.the lower guess (x.4903 I ( r . 5 8 2 9 3 . r: 1 .r:) and the new x value is to the right of the intermediate than for the interrnediate point. within five iterations.):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 . ( 7 .The process be repeated.) is discarded. The fminbn.1114.4 we described built-in MATLAB functionf zero. s 8 2 9 0 ] : which frasa functionvalueof /(1.1 . l l 3 6 l .rilarapproachfor one-dimensional minimiza. 7 6 9 1 )- which lrasa functionvalueofl( I .t 7691 -1 7757 4qa:J .5055 t 4903 I 4256 I 4266 14 2 7 5 Thus. It and ap plying it as long as acceptable results obtained. first attempts parabolicinterpolation keeps unreliable. Thisfunction the combined severalroot-finding methodsinto a single algorithm that balanced reliability with efliciency. resultis converging the rapidlyon the true valueof -1.butpossibly parabolicinterpolation. 5 0 5 s 1 ) l . i l 3 6 1 ( l .r . I 7.I . "f(rJ -1 /691 -1 7711 -) 7757 -t 7757 -)7757 I 2 3 4 5 0 000cr 0 0000 I0000 r 5829 I0000 .5829 .l X.3 MU Asid func a rol valle a har .1691. dependable searchwith the faster.5829 3. 7 6 9 t.r78 OPTIMIZATION into and substituted Eq.Becausethe function value for the new point is lower point (. (7.3 MATTAB Function: rminbnd Thu itera Recallthat in Sec. not.r'): -1. l 0 ) t o g i v e n c ra = 1.suIts tabulated her-e: . 5 0 .t f k.+[ .t .rl J@.-50-5-5 xl: 4 whe func Exar .t 5829 1 4 2 5 6 . . Next.r 5 8 2 9 4 0000 3 r 36 I 7 6 9 1 4 0000 J I I 36 t 7714 5055 .3 * l (l . ] .it usesthe golden-section are If search to set mattersin hand.4903): -l .50.1 .4 ) 2 .6.s 0 5 -r5 t ..2.11361 0 .t 5B2a r o o o o .5 5 I ) 2[ . golden-section tion. Therefore..li.f function provides a sin.0 ) [ ./k.76e1 . thc with can r-e.t 7 75 7 0000 5055 4903 4256 4266 .l'Q): -1.r .t /714 -) 7757 49A3 I 7714 1 . It combinesthe slow.50-55) -1. 7 6 9-t 3 .) x2 f (x2) r.10)to give r'. 7. 7.0)2L-1.

optional parameterscan be specified using optimser.5 5 r m= BO.05573 4.851 192 .7 . we can display calculation details: options fnr i = ofitimset ( 'display' .1.83.For two-dimensionalcases.As in the lbllowing example.81544 3. X= 3.x2) wherex and f va1 arethe locationand valueof the minimum.87I 792. function is the nameof the function being evaluated.ToLX of 004 3 .mrnated: x satisfies 1.000000e rB9. the to of 7.94427 r.0001. z A. For example.861 the termrnation criteria usinq OPTIC)NS.3 MULTIDIMENSIONAL OPTIMIZATION A simple expression its syntaxis of fxmrn.83r52 3. .19 I/I.159 187.3a that our visual imageof a one-dimensional search was like a roller coaster.85836 3. minimum is determined a tolerance 0.2.L 0 a .rrabolic parabo 1i c 4 5 5 1 B 9 OptimizaLion the current 3. B6r 192.8609 As with f zero. B6r 792 .the image becomesthat of mountainsand valleys(Fie..3b). after three iterations.88854 3 . fvafl = fminbnd(functian.851 -r92 . > > g = 9 .83332 3 .r1 Thus.l.and alier eight iterations. optimization also deals with multidimensional functions. Func-count 1 2 -l X f (x) Procedure inirial go 1den qolden parabo 1i cpirrabolic perrabolic parabolic P. MAILAB's graphiccapabilities provide a handy meansto visualizesuchfunctions.the nrethodswitclresf'romgolden to parabolic.83159 ter. >> z=@(t) (20+n/c*(v0+m"g/c)x \1-exp( c/m"t))-m*g/c*t) . 'iter') . Here is a simple MATLAB sessionthat uses fminbnd to solve the problem from Example7.xl. c = 1 5 .Recallfrom Fig. B(r1 792.83166 3. B1 . v 0 .3 MUTTIDIMENSIONAT OPTIMIZATION Aside from one-dimensionalfunctions.8317 f-192. and xl and x2 arethe boundsof the interval being searched.

zmin=f l. The fbllowing scriptgenerates contourand meshplotsof the function: .D i m e n s i o n q lu n c t i o n Problem Stqtemeni.2. 4 0 ) .r. _ ?).i Sta mu dire han whr the (' f (x_I. = c o n t o l l r ( X . . = l i n s p a c e ( 0 .rbel (':<-2' ) . . ^ 2 + 2* x . C) 4 A ) .xz -'l.9 (o) Confourond (b) rneshplc.9. r x( Z ) ) . * Y + Y .2 .4 nLtn on {fAt uat funr 7. r b e l ( c s ) . z l a b e l title('(b) M es l .2. Use MATLAB's graphicalcapabilities display the following to function and visually estimateits minimum in the range-2 < xr < 0 and 0 < ._2)' ) .r ..4 7 OPTIMIZATION F V i s u o l i z i n gq T w o . r ' . Y . z = .:<.r2 < 3: 2 .r80 EXAMPLE .e iI ( m .r t r ' l o t ' ) .' ' 2 . i. Z ' ) .7).Jrid. 3 . : l.xl +2xrrz + xi Solution. trtle(' subplol \I. 2 + x .-5. x . { subploL i'7. both plots indicatc that function has a minimurn value of about -l and. xlabel ('r-1' ) . c ! . ( -l :.ls o Mo-dimensionol functron of Bo wh forc fere * lo) | the (a) {b) . '. f t .ror(min (Z)).'{.2 t. (a) Contour ploL' ) . the As displayed in lrig.f(xt'x) :2 i xt .'+ 2 * x .. 7. x l a b c l { ' y _ 1 ' ) . : r I a b e l ( ' : ' . cs=surf c lX.Z). ( t]:' Yl = meshqtrid x'Y) . z m a x .\'l. 7. " r 2 ) : 0 t o I l o c a t e da t a b o u t r l : FIGURE 7.l i n sp ac e ( .

4 CASE STUDY t8l Techniques multidimensional for unconstrained optimizationcan be classifiedin a we numberof ways. will divide them depending on whetherthey require derivative evaluation. As in Fig.70a.5kx2 * Fx (7. and xl and x2 are the boundsof the intervalbeing searched. The approaches do not requirederivativeevalor that uationare callednongradient. 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. descent ascent). is The displacement relatedto the When a horizontalforce is applied.For purposes the present of discussion.x2) andvalueof theminimum. simpleexpression its syntaxis of fxnrr. force by Hookes law.the spring stretches. OO0O = 0.As describednext.11) 7.fttnction is thenameof where xmrn and frzal arethe location the tunctionbeing evaluated.xl. The potential energyof the deformed stateconsistsof the difference betweenthe strainenergyofthe springand the work doneby the force: PE(i :0. fvall = fminsearch( function.4: : .5001) ENERGY EAUILIBRIUM AND MINIMUM POTENTIAL to Bockground. )^ 2 . > f = @( x ) 2+x\1 ) -x(2) +2*xlI) ^ 2 + 2 * x ( l * x ( 2 ) + x ( t : .7. 7.Those that require derivativesare called (or gradient.7500 1. 7. or direct. the built-in MATLAB functionfniinsearch is a directmethod.IO FIGURE force An spring oitoched o wollmount.3. It is basedon the Nelder-Meadmethod. F : kx. ) f v e ri -1. methods.which is a lnum of a rnultidimensional direct-search method that uses <lnly function values (does not require derivatives)and A handles non-snrooth obiective functions.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. methods.

7.OPTIMIZATION it. Sincethe potentialenergy will beat a equilibrium. r : F / k ..I FIGURE I A 1"wo-spring (o)unlooded (bllocded.1. and x.I+:l. t Example along 7.. : 2 N/cm. . Becausethis equation is so easy to differentiate.skb ( If the parameters ko: 9 N/cm. methods to fu dl values of r.the equilibrium mationsare the valuesof x.. k. F o r e x a m p l ei. r = 5 N / ( 2 t 2. (7. the following f =3+6.F. use MATLAB to solve for the displacementsand the potentialenergy. = 2 are Fz = 4 N.t 'i.|. Lo: 70 cnt Lo: l0 cm.10)yi guesses ofr.we can solvefor p l a c e m e na s . continued Equation(7.11)definesa parabola.In this two degreesof freedom in that the system can move both horizontallyand are In the same way that we approachedthe one-dimensionalsystem. system: ond PROBTEM three iterations the root of Eq.1 the formula =-x2*8x-12 the maximum this function analytir thatEq..S-.i l .x ) 2 * L") + o.5x6+2r4+1 fiurction. Thu loca 7.r+5x2+3 6e minimum by findi Use bisection r -1. the solution for displacement be viewed as a one-dimensional can tion problem.0. that minimize the potentialenergy: 12 5€ The _ ^ l2 + (L.f k : 2 N / c m a n d F : 5 N .5 cm.. A more interestingtwo-dimensionalcaseis shown in Fig. .11.

b + x \ ' 2 ) ) ^ 2 ) L b ) ' l .f (xl : ro + 2-tr + 8x2 + 5r = + method of three Perform iterations the Newton-Riiphson valfherootof Eq. u x 2* 1 . L a = 1 0 . (a) Golden-section search ol' nrinimunr findingthe root ol'the derivative by the (b) Parabolic (. 5 * k a * ( s q r t ( x ( 1 ) ^ 2 + ( L a .7-5.| .r. nlethodsto t'ind the nrarirnum ol7. 5 l 4 . l . c h ( B P E . ) P E a =0 . The connecting position. l -2 ) function ^.! r . 2 . Enrplo-v ly rfor functirx lhis initialguesses . E " t . of Employ initial guesses using the golden-section 12 J ( r= . the Given fonnula the Plot function. (.PROBLEMS continued Solution.: . .l). ] I x .ll Consider fbllowing function: Given r 1 y ).1 3 0 .r r + 8 r ) . 5 . to An M-file can be developed hold the potentialenergyfunction: p = P H ( x .f in Prob. F ' I ..-5.. k a = 9 .(r RepeatProb. rnd perof Vedfy Eq.W . and perfbrrnthree iterations.r)and the corresponding valueul'r.: of function. except use parabolicintcrpolation. valueol' 7.rr: -2.7.v.t. (7. xp1l.t n 2 x " + 1 2 l .1 . r'z 2 Use with initial [uesses -r. f ] . that : of. k b .e. 7. initialguesses : fl.l(. 8. .l-ra (.4 search.cm.r a '(r) r = 3 { 6-r* -5-r + 3xr +." 0 . 1) ' 2 + ( l .r. : 2. : 4. .10)yields the sanreresultsbasedon lbrm threeiterations. ir : 2. =i 0 .2'. l . bisection : iterations -5).9 .ti : 0 and-r. .1.. cm located4.: 2. t .5 Solve for the value of -xthat rnuximizes. (E7. -r. the and the colresponding Delermine maxinrurn analvtical ( i. L b . .42.9523 cm to the right and 1.the potentialenergyis -9. $.l . : l(n.r) 7.: point is Thus. 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.v. at equilibrium.r) : 0. 3 .09 l . P The solution can be obtained with the fminsearch function: .7 Enploy the l'o|l<'tring the Considerlbllowinglunction: I ( r ) : 4 x . : I.1 -La '2.r ( 2 ) ) ) ( s q r t ( x ( . r r =l ' the 7. methods show the lirnction to and graphical Use analytical has a minirnum fbr some value ol' x in the range *2 < x < l.P F l a + E b. : b .. F 1 . . i .L a .2'759 aboveits original PROBtEMS (c) Dil-l-erentiatc' lirnction and then Lrse root-location a the methodIo solve lor the maxirlurn. p .usingdill-erentiation).6422 N.9\2 i f- I ..f r n i n s e a r .2 .tr interpolation : 1.5*kb* W .5. 0 .F l * x ( 1 ) + F 2* x ( 2 ) . andxr : 2. PEb=0. k a . (. methods prove that the function is conto Useanalytical of for cave all values -r.

Li l'unctitln.r84 OPTIMIZATION : constant 9.Il ntlt.22 7.rr: 0. 2t . p7.r. criIteratcuntil thc rclativcclror talls bclow a sk)pping to ob. othcr words.14 PrcssurcmcasurL'nlcnts takcn at ccrtain poiltts (.1: 30.1 . The product B separatlon unit.. 7. onlibioiic versusthe fooo t FIGURE P7. 7.2 brackctit tlaxitrturn. : 5) -rr deto dcsigned locate ahd ur.1 . r ' . P7.rnction: 7. antibiotic a rncthodto l'indthc nrininrunr o1'thcgoldcn-search Sctr. f(.t . shouldhavc finding thc rninimunrof with a ntassof 100 kg is prolected upward 7. r ' ) 4 . 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..h .5 kN/cm. Rcturnboth thc optinral and.8: (.1. 1 9 l J s c t h c f r l i n r .rlinimrnl ot' 7. . of scarchto clc(crttrirrc location the Usc thc goldcn-scction . .. .: 2. tcriorior cxcccdsl rnaxintunt . 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. 2 5 .17 A ob.r * 2 . ' -3 r 2 but shouldnot inrplcntcnt algorithrn. curvedefinedby is clistribution a bcll-shapcd 7. : l% with initial sct if up so that it dircctly finds the traxitnum ratherthan until tlie approximatc -/'(. r r .rr : -2. v ) . d.r * J 0 . 1 . tolcrance.Commenton tlreconvclgcnce yttur rcsults cm{.and0. -t .r Rcturnboth thc optinral and.16 The dcflection of a unifbrm bearnsubjectto a linearly loadcan be computed as distributed increasing P 7. criItcratcuntil thc rclativccrror lllls bckrrva stoppinS .5 sin.5+r .specificgrow.r r ...9) as thc stoppingcritcria.oinitialgucsscs.attd. Iterate usinginitialguesses approxirnate ertor falls below €. {lection(a) graphically'. : (a) Golden-section search (b) Parabolicinterpolation(.ttiot'l is lirnction prcssurc.e . .usethegoldenis nunrbclol'itcration.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 .r: : 0. chargeof sewage (Fig. detcrtnincthe point of maximum 7.r: 0 to 6 s.y)--5.. ( X X ) 1 .r.000 Giverr thatL: 600 crn.rt'lr ltlnction to detcrrnine .:2and.r)./(. Dctcnnine thc rninirnuttr (hc lrLj rr. ) 2 .. -v.: 0 ancl.i{r.1 .rr' ttutnhcltll'itcrationr. r''*rir r : ( t i u l H { ) ) .: ol'thc lixld conccntr. thc thc subroutinc l 7 . 5 . 7.C = 50.[f the . .8r+(l+0. 2 u s i n gL ' .1 )o ..t'n the initial height(rn).: $Qp. 7 . z thc tr. bchind an airlbil ovcr tiuic..nputcd 7.r.1.: 0 . / ( . a tcrionor cxcecds ntaxinrunt thc heighttheobjeo to scctionscarch detcrrninc rnaxirnum . iterations 0. (7.r) : 2.on. and Bascit on tu. : Check whethclthc gtrcsscs / 1 ."ntrrn.ingf'eaturcs: liom tlre surluccol'thc carth at a vclocity of 50 rn/s. dissolvedoxygen .3.k .L.25 o: o. the . Thc subroutine guesscsof -r..r 2 lirllowingl-caturcs: the r 7.sls .icct subjcct lincaldrag(r' : l5 kg/s).:li ancl(c) suhstittrte result with 7. l '.000 (.r. Ralhcrthanusingthc Inaxitrtuuitcrations und L.ig where-r : end displacernen values fr.Findthevalueof h u.r).t mo.15 Thc trajcctory 4+0.22 Thc spccilic gt'owthratc of a vcast that producesan _r: 6 cos-r (.. : l. Llsclilur iterations 7.:'1. r r + 2 .1-1t)cvelop an M-filc (o irnplcntcnt shouldhavc thc Thc subroutinc tion (o krcatca nrininturn.20 Usc tllc f nriri. t h a tn t i n u n 7.23 A compounclA will I tank reactor..1 .1{)Considcr fbllowing 3 .22.t l'rom.(b) nurnetically . . .r. .: -2.itions to toxicitycitects. high conccntri. -y:: -1.r. "t attai ns.pprpfogramrvith thc samcptoblctrtas Exanrplc tlic ol' (b) back into thc l'unutionio clcternrinc rninimum arc 7.r2-5xy*2 7. S/. Unreacted processengineer has fbund is a function of the convers will result in the lowest cos constant. I.r.: 50".lc/c). .24 A finite-element mode loading and monrenrsrF.4 I ( d .= _5(-ys+2L)r'-Lrrl to Perform IO itcrationsof parabolicinterpolation locate I)()EIL of the minimum. : thc initial angle(radiansl. usingthe golden-scction [n with thc golden-scction a rnaxirnum error falls below e.dctcnninc(hc 0 lne.r'): -tl-r * -rl + l2r' + 4r'l 2.2(] l0$ As dcpictedin Fig.r. :5).Itl The nornral with thc sarncproblctnas Exanrplc Test y()ur prograrn 7. . r .(t .icct thc tblloia.: lc/o of . .^ . -.9 Employ the fbllowing mcthodsto fincl the minitnurn of thc function fiorn Prob./(. 7 .25 The Streeter-phelp.Usethe golclen-section search to determine the nraximum height : untilthe givcn -t'u I nt. r. kN/cnr:.r).*!L p k a * k . 2 . r .. : .l (. -.24 A contilever beom ..-5.: g and-r..I. .ll Devclopan M-filc that is expressly (b) sealch scarch. . Thc data bcst l'its thc curvc I ./ (.___ f(x. : the initial vclocity (rn/s). Tcst 1. (a) glaphically.12 Dcvclop arr M-f ilc to krcittc l ntinimum with thc goldcn-scction search. 2t with o1'aball can bc cot. u' : 25 rn/s..r.8 I rn/s:.j crrs'11" cosr:.growth gocs to zc() at very to It conccntrations to lcxrdlirnitation.'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 .ttitxirtrunr ()f cratcthc third initiirlvalucat thc rnidpoint thc intcrlal. i'). alsogoes zelo8l cluc due whcre .r. havcthc progllut gcuofl.. r---r__' I I FIGURE P7.q.= j .and o 1 .r': the height(m). r . .

24)is civen by optinrizing Deternrine critical tlavcl tirrre' hoding monrents and v thclbllowing alues: rl J{.24 Aconli beom ever 4ten 1t2 1 47131t rrCr/(Nmr.( i .26 The two-dirnensional trationin u channcl can be described by r ' ( r . is a proportionality in C on$ant.r:0.05d I L. r* 0 .27 A total chiirgeQ is unitbrnrlydistributed sh'.t'turtr.0 .\' ( .r : lraveltirne(d).. ( X ) 7 . 14. . 2 1 .r7: l0 iC' w h e r e a .\'. The Streeter-Phclps mtxlel can bc usedto cornputethe oxygen concentration a river helowa pnin(clisin (Fig.r I :ally )sult num :s an o = 0'.-ft. ln levela. r : .\..f . t ' -0 .22 ch ial rrd . pocess engineer foundthat the initial costol'thc syslcm has ilafunction conversion Find the conversion ol'the r. = .\1.:. arrd5.: I rng/L/d ol' distribution ptrllutant concen7..r.nent. r ) : 7 . Deterrnine distance whet'ethe force is a mlxir.i ttt. r \ thrr. productB and unreacted ureA The loparation UnreactedA is recycled to thc reactor. ratL'(d r). . r e a e r a t i o n (mg/L/d).(.y : end mol.\.)./.\' .r1.25 A dissolved cxygen "sog" below o point dischorgeof sewoge Inroo rver.-t 1 o\:\mc€ -\ lr()m tnt center ()1Lr\er\ng tFrg..d .: 0 . P7. : r a t e o l ' s e t t l i n g o l ' B O D I ) .. r x 'r 'tO ..r "cri(icirl"hecause rcpresents lrrcation it the wherebiotuthat L\{l-.. This point is called '6('-l Cost=clt-l I \. 'I : - - . P7.25).21 t.tl'rltt I ---rr.: sediurent oxygendemand (P7.PROBLEMS r85 rt )S 0.\'+ i.rl)-/ I dependon oxygcn (likc I'ish)would be thc most stressed.).r.A unit.nininrunr a l. kz : rate ol'decomposition thc ol' (d B O D ( d r ) . t. that rillresult thelowestc()stsystenr.. 6 t l I r .The \orce exerted on thc charge by the ring is given by )' low ro at le of | RGURE P7. f r . rof the (mg/L). P1 .t .2 ld_1) ly (mg/L) or' 0 0510 c (mg/L) lc..he :nThespecific growthrcle of o yeosf thot produces cn onlibiol c vefsus food concentrotion. : whcre o : dissolvedoxygen conccnlration (mg/L).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(..r..:c r1 i. 0 1 6 r ' r.\r':q11-{ .' Sr -p(l-e-^"t ej.0 . ..r*k.r. ..:-50rng/L 5r. IIGURE P7.rned cr-rnductrq rr\th rrli\rl. srrme oxygen"sag" thal reaches criticalr. i + t ' ) / ) ' li. and Find the of. ofsewage P7.J t t ! ( c t ' .:.| . the FIGURE P7. oxygensaturatiorl conccntrrti()n (BOD) concentralion L. t 1x5 l 0 Q:2x x the and d : 0. I r"" r |r""l traveltirnet.2-5.. arounda t'ing7. .25)produces As indicatcdin Fig. t d | r = enddisplacenrent . r:ivcn (Fig. 0 5 .randl thatrninimizc11. : 8 .9 m. A rnodelof a cantileverbearnsubjectto ?J4 finite-element thc and concentriltiun. 9 t O .r. bclow thc point discharge.. o.:0. t. F. : biochemical oxygendenrand irt rnixing point (rng/L). l ( ) u r g / L k. by 7J3 compound will be converted into B in a stirred A A purified in a hnkreactor.4 8 0. r'). l 3 .

28showstlris function.and V: r.(xx) with o : 0.r that rraxirnizcs /(. 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.31 Twofrictionless mosses cor lineor elostic springs. Thc problenr of to dctcrminethc slip at which tlrc rlaximum torqueoccurs.'. P7.thc drag due to lifi dccreascs.000 (r FIGURE P7.3O Roller beorings.r86 OPTIMIZATION PR r_ ro D 20. thc location ol' the maxirnum stress can akrngthc -r axis (o 7. with velocity.tlrc two l'actors contributing drag alc i'rfWhcreasfrietion lected difl'ercntlyas velocity increases.4+'F -/t __/ rr'Ir \ ---_]+.-10 Rollcr bcaringsarc sub.dcterrnine minimun the drag anclthc velocity at which it occurs. P7.. anllysisto determim devclopa sensitivity to d how this optirrum varics in response a range l5s(l. FIGURE P7. scen to in Fig.r l+r-l 04 \ Find thc . Minimiz to determine the equilibrium ltc forcing function F: l0( Jd t6 .6 and W : 16.iect fatiguetailure FigureP7.29. ( b ) In adclition. to ( I .3L plots in MATLAB.l5 N/m.000.' w: 12. lV: wcight.32 .sr 3 s * z l ) to 7.o : ratio of air densitybctween flight thc As altitudcandscalevcl.28 Torclue ironsmifted on inductoros o function slip to of FIGURE P7.29 Plot drcg versus velocity cn oirfoil for of FIGURE P7. 7. whcre rr : rcvolutionspcr seconcl rotatingstatorspccd of laws can bc usedto show and ri.clocity.-10).000 10.2t1The torquctransrlitted an indr"rction tion of the slip betwccnthe rotationof the statorfield and thc rotrrrspced whele slip is detincdas .Usc u numcricalmcthod lirrgc contrct loacls/i tFig. develop the poten tem depicted in Fig.29 Thc total drag on an airtiril can bc cstirnatcdby shownto bc cquivulcnt maxirnizing function: the n D : 1 1 .6. P7.000 20.: rotor spced.4.31 In a similar fashionr Sec.27 motoris a flncto 7.r').r.r)(4.. 1 1 1 o Y ' +( # ) ' :T F-r'iction Liit whcrc D : drag. P7. 7. drag increascs 0.. 732 As an agricultural engi open channel to carr) ine the optimal dime perimeterfor a cross-section 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.1 .s) . tl tlR 0 FIGUREP7.

P7. sfnfi stn(ft-a-H) "'' : For the casewhere ur1 : r.32) to netheoptinrill to dinrensions rninimizethe wettecl fbrr cross-sectit'rnal of 50 rn'..4. Are the relativc area l universal the tm Use lunction i ri:.. Testit lirr the case L _ . you rnustdesigna trapeAsanagricultural engineer. P7.1 can be determined by _-.3 | hictionless connected o woll by o poirof mosses fo e . open channel carry irrigationwater (Fig. t Thelengthof the longestladder that can negotiate in comer depicted Fig..t . usea numericalrnethod describedin this chapter (including MATLAB's built-in capabilities) develop plot ol'L versus range to a a ola's from 4-5o 135".1.r < . P7. given .f 'i . Developcontour and surflce tnMATLAB.. l5 N/rn. F forcing tunction : 100N und the paral)rcters k.+ -.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."s.32 FIGURE P7.33). .PROBLEMS r87 P7.34 A iodder negolioting formed trrohollwoys o corner by . '. derelop poterrtial the fol energyf'unction the sysin depicted Fig.il.33 um ine of ln a similar lashionto the case study describedin ?."'.:20 = tr.^J .lr2 2 m. P7. P7. Minimizethc potcntialcnergyl'unction letermineequilibriurn the displacenrents and -r. i n.e:ach to deternrine length r the shortest ladder that reaches liorrr the ground over the tolhebuilding's wall (Fig. q -n FIGURE P7.r.

these teadto balance continuiry or equarions rhar llT'p.xr) : 0 . : xr..t) wherethe rz'sareconstant coefficients.* ar.1x1 anZXZ * I ...lx.:l T are based ano. : aztxt * azzxz . b. otheralgebraic All equations nonlinear.. x.s arecon_ the stanrs. fJ..f r) : 0. thatsimultaneously *r.x..i Lineor Syrtems ii 3.. Such systems are either linear or nonlinear.": ..' '. nrri..xr-.f. we O"rrrrn::. rhe * unknowns.: equatrons. .. (PT3. .x. Now. part In .xp .1|.anO momenturn. and n is .x. .. . I ovERVtEW Whof Are Lineqr Algebroic Equotions? In Part Two. . Sometamllar quantities that conformto such laws are mass. : 12. we deal with the caseof determining the values* ri. satisfy a set of equations: f.. * a2.):0 fik.Te value xrhar satisfied a single equation. fi .. * : -: a1... fi. . are linear Algebroic Equotionsin Engineeringond Science ::iy^. .*:.. of 1.:l!:li... + aloxn [... ''. scrence on conservation laws..*. energy. mathematical In terms.relare systembehavioras represented by ifre levels or fundamenlat equarions engineering of r89 .. deal with lincar algebraic equarions that are of the generalform a|xt * anxz+.i:o : .y.

(PT3. The. reactor4 receives chemicalinputsfiom reactors and3.The forcing lunctions arethe feed ratesof the chemicalinto the system.t's areusuallyrneasures the magniof tudesof the responses the individualcomponents. principleof massconservation be usedto formulatea model the can for a series chemical of (Fig. The equations coupledbecause individualpartsofthe systerrale influencedby other are the parts. The a's typically represent properties the andcharacteristics that bearon the interactions betweencomponents. PT3. its is on When thesedependencies expressed are rnathematically.l )..suchasthe feedrate.in Fig. and so lbrth. The systempropertiesare the reactioncharacteristics of the chernicaland the reactors'sizesand f-lowrates.r2 in rnightquantifytheamount in the second. you saw how single-component systems result in a single equationthat can be solved using root-locationtechniques. As an example. For this case. "r1 might quantifythe amountof chernical rnass thefirst reactor. When we studiecl rootsof equations. a's for Fig.I Two lpes of syslems thot ccn be modeledusinglineorolgebrclcequotions. instance.ln as an example. of Using Fig.lc. 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. the Finally.l a). PT3. PT3. resLrlting the equations are often of the linear algebraic folm of Eq. Multicolnponentsystems resultin a coupledset of mathematical equations that r.la mightbe For the reflectiveof the flow ratesof mass between reactors. response the quantitybeing modeledto the properties characteristics the system of or of alrdthe external stimuli or fbrcingfunctions actingon the system. quantitybeingrnodeled reactors the is the massof the chemicalin eachreactor. response dependent the quantityofchernical in theseotherreactors. 2 Consequently.nust solvecl be sinrultaneously.Lrrmpedvoriable probLems involve coupled .t90 PART LINEAR 3 SYSTEMS Feed @t FIGURE PT3.For example.theb's usuallyrepresent the forcing functionsactingon the system.PT3.

Theseresultwhen mathematical functions are required satisfyseveral sirnultaneously. s\/stet)-ls.rinary material.Similar examplescould be developedfor the spatialdistributionof temperature. o Aside fiom phvsicalsystems.reactors.the concentration at the middle of the reactor in Fig. Chttpter 9 is devotedto the most fundamentaltechniquefor solving linear algebraic systems: launching into a detailed GausseLimittolion. After this prelirr.Before discussion this technique.in subsequent sections. Chap.These differentiai equationscan be solved numericallyby convertingthem to an equivalentsystemof simultaneous algebraic equations. and discussed this in The techniques part can be used to solve fbr the unknownswhen the equationsare linear and algebraic. Chapter10 illustrates Such solutiontechniques valuable1br caseswhere many right-hand-side are vectorsneed The chaoterends witlr a brief outline of how MATLAB solveslinear to be evaluated. TheseeqLrations coupledbecause variables one locaare the at tion are dependent the variables acljoining on in regions. 8 provides a brief overviewof tnotrir algebru.is a function of the concentration adjoining in regions. of a pleliminary sectiondealswith simple methodsfor solving small systerns. reactor(Fig. starl with We it this "stripped-down"versionbecause allows the fundarnental technique be elaborated to potential probon without complicating Then. m o n r e n l u m . bungee of Conversely.8 area lumpedsystem.The chapter endswith a brief description eflicientmethods of fbr solving tridiagonal ntatrrces.orpdrlial pit'oting. cl. .r e l e c t r i c i t l . PT3. These approaches presented provideyou witlr visualinsightandbecause oftlre rrrethodsare one to the elimination unknowns-represents basis Gauss of lbr the elimination. The distribution chemicals of alongthe lengthof an elonqated. we lems of the naive appr-oirch presenta number of mocliflcations mjnirlize and cirto and cunlventtheseproblenrs. discuss details.2 PARTORGANIZATION Due to its importancein fbrmulating and solving linear alsebraicequations. how Gauss eliminationcanbe lbrmulatedas an LI) factoriZation.and electric circuits. condition Each to conditions results an equation in that contains known coefflcients uuknownvariables. The solutionof suchsetsof equations leplesents major applicationareatbr the metha cldsin the fbllowing chapters.Aside from coveling the rudimentsof matrix representation anclrnanipulation. PT3.2 PART ORGANIZATION t9r finite components.distributed variable problems attempt to describethe spatial detail of systerns a continuous semicontinuous on or basis.3.For example. Somervidely usednunrerical techniques that employ simultaneous equations legresare sion analysis and splineintel'polation. The three jumpersat the beginning Chap. Diffbrential equations derivedfiom conservation laws specify the distribution of the dependentvariable fbr such systems. The fbcus of this discussion will be the process switching of rows.lb) is an exantple a continuous rectangular of variablemodel.Examples include trusses. 3."naive" Gausseliminationis discussed.11. simultiineous linearal-uebraic equations also arisein a varietyof mathernatical problemcontexts.rapter the also describeshow man'icesare handledin MATLAB.

. they involve guessThe emphasis on the Gaussis ing a solutionand then iteratingto obtaina refinedestimate. the Jacobi method. response of The remainderof the chapteris devotedto the importantconceptof matrix condition.r92 PART LINEAR 3 SYSTEMS Chapter -1-1 startswith a descriptionof how LU factorizationcan be employedto efficiently calculatethe mati.6.The chapterendswith a brief description how nonlinearsimultaneous of equations canbe solved.which has tremendous utility in analyzingstimulusrelationships physicalsystems. which are similar in spirit to the for in approximate methods rootsof equations discussed Chap. That is. Chapter I2 dealswith iterative solutiontechniques.The conditionnumberis introducedas a measure of the roundoff errorsthat can result when solving ill-conditionedmatrices. Seidel method. although a description is provided of an alternative approach.r inverse.

r: . U n d e r s t a n d i n ga t r i xn o t a t i o n . for in forcebalance be writtenforeachjumper: law.l.. a in Knowing how to solve linear algebraicequations with left division and matrix inversion MNLAII. Knowing how to represent systemof linear algebraicequations matrix form. .k: (. free-bodydiagramscan be developed eachjumper as depicted Fig.PROBLEN\ by upposerhat three jumpers are connected bungeecords. in \OU'\E GO\ A.\. 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.nrl effiuluso the [e of o the UESSAUSS- . \ dovnv'ard from eachof their . 8. it1 takeshold and thejumpers s ill evenunstretched positions tually cometo the equilibriun-r shownin Fig.1b.ln shows them It We but so U.diagonal.t'2.If we of as assumethat eachcord behaves a linear spring and follows Hooke's law.and tridiagonal.triangular. Knowing how to perlbrm matrix multiplication and being able to assess when it is feasible.Figure 8.1 1 ). a steady-state can Using Newton'ssecond : m 1 gI k z ( x z. 8. Suppose that you are askedto computethe displacernent eachof thejumpers.2. .. r r ) : 0 t1tlg kr(. ' .Specificobjectivesand topics coveredare e . as measured 9/ grar po:itions.tierrhel are reJeased.x ) .f t 1 r 1 Q ..k z ( x z. . m Being able to identify the following typesof matrices:identity.ing held in placeverticerlly that eachcord is fully extended unstretched.I.t'1. symmetric.and .irz) : 0 .r: * tntS . crn define threedistances.

.81 ( k 1l k ) x 1 -kzxz :mtg (8. Chapters8 through l2 will introduceyou to how MATLAB is used to solve such systemsof equations.r94 LINEAR ATGEBRAIC EQUATIONS AND MATRICES ". the problem reducesto solving a systelnof three simultaneous equationsfor the threeunknown displacements.(x2. wherenr. andg = gravitational acceleration nr/sr). Collecting termsgives 19.11) tr n ?r i I tl I m tg t.2 Free-body diogroms. A.I MATRIXALGEBRA OVERVIEW Knowledgeof matricesis essential understanding solutionof linear algebraicequafor the tions.tr * (kz I kz)xz. : the springconstant cordT(N/m).nl ) ffi il z'^r li: n ffi ?r t:tl l _ Hr ) ffi I'l ffi Arxr t2(r. : the massof jumper r (kg).-ri : the for displacernent jumper I measured of downwardliom the equiiibrium position (m).: Iri i . Because have useda linear law for the cords."r2) tr (a) Unstretched lr) 11 ffi ( b ) Stretched tt nrrg k. 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 . 8.:Ol?r u -n 1 ffi .r1 .these we are equations linear algebraicequations.1) -ftz.(.I Threeindividuo conneciedbv bunqeecords s FIGURE 8.k:-rr : mzg -ktxzlk3xl-7n19 Thus.x) tl ill m$ FIGURE 8.

thereare occasions this is One way to accomplish to distinguisha column matrix from other typesof matrices.3. the first subscriptof each element is to dropped. in {c}.l way to accomplishthis is to employ specialopen-topped Matrices with column dimensionn : l. the secondsubscriptis dropped.1 Mqfrix Nototion by A matrix consistsof a rectangulararray of elementsrepresented a single symbol.3 A moirix.t. -Row2 t^t - onl o. It is referredto as an m by n matrix.elementa23is in row 2 and column 3. Note that for simplicity. Matrices with row dimensionm : l..1. to employ specialbrackets. A horizontalset of elementsis called a row and a vertical set is called a column.r. as I In addition to specialbrackets.n3 a. 8. 8.) a.' FIGURE 8. As an notationfor the matrix andai 1 designates indidepictedin Fig. [A] is the shorthand vidual element of the matrix. will use caseto distinguishbetweenvectors(lowercase) and matrices we (uppercase).:&it:''.The secI designates column.2) .Also.The the first subscript alwaysdesignates numberof the row in which the elementlies. in fbl .MATRIX ALGEBRA OVERVIEW I I ll I I 3 Column a:t azt at2 I at3 I iiil Qt. 8. azu azz . it shouldbe mentionedthat thereare times when it is desirable employ a specialshorthandnotation to distinguisha row matrix from other types of matrices. For simplicity. suchas lnl j tL - are ref'erredto as column vectors. the ond subscriptT The matrix in Fig.For example.One as brackets. suchas [b]: [bl b2 brl are called row yectors.3 has m rows and n columns and is said to have a dimensionof m by n (or m x n). L: (8.As with the notation when it is desirable employ a specialshorthand to row vector.

c A diagonal nmtrix is a square matrix where all elements off the main diagonal are equal to zero.. as in rl12 o.rl ott ctstJ [. An identi4' matrix is a diagonalmatrix where all elementson the main diagonalare equalto 1...square matrices. tttl n ate called .. : aii for all i i's and. a ) ..j3is termed theprincipal or maitt diagonal of the matrix. as in fo. The identity matrix hasproperties similar to unity.4]: [''' 422 I .) .and a. as in tAl:l [rl I LlJ I The symbol Ul is usedto denotethe identity matrix. That is. r r .r96 LINEAR ALGEBRAIC EAUATIONS AND MATRICES Matrices where rn : l-r.: .1 The diagonalconsistingof the elementsas. I lAl=l a-) ) | orr) L Note that wherelarge blocks of elementsate zero. 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 columns-that is.the number of equations(corresponding rows) and the to numberof unknowns(corresponding columns)must be equalfor a uniquesolutionto be to possible'Consequently. a 3 x 3 matrix is atrl I A l : I a r . [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 .For such systems.. Squarematricesare particularly important when solving setsof simultaneous linear equations.I L. a. a22.". [A]Ul: [/][A]: tAl An upper triangular malrx is one whereall the elements below the main diagonal are zero.l's. For example.they are left blank. For example.

. . .ALGEBRA 8...for a 3 x 3 matrix: I D I: s l A l: l i o l fPo. Rart Pat. .2.. . 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..2. is accomplished addingcorresponding of terms in eachmatrix.Two nr by n natrices are equalif. .as in [E] dij:eij -fij I i I i I forl: l.. is obtainedby subtracting terms..1.n.. 2 . [Al and [B|...n...with the exceptionof a band centered on the main diagonal: f al ln l: t ..] "'. [A] : [B] if ai1 : bi1 for all i andj. dt. For example..I MATRIX OVERVIEW 197 A lower triangular malrrx is one whereall elements abovethe main diagonalarezero. . we can definesomeoperatingrules that govern its use.. every elementin the first is equal to every elementin the second-that is.ltfollowsdirectlyfromtheprecedingdefinitions that addition and subtractioncan be performedonly betweenmatriceshaving the same dimensions. 2 .r.. by Addition of two matrices. . Zt a I I | at) a:z .I efltt . i o r ..i1b.h e s u b t r a c t i o n t w o m a t r i c e s . tAl : | i o11 1.. m a n dj : 1 . . . S i m i l a r l yt.. and only if. I 8. as ln I f o.. The elements the resultingmatrix [C] are computedas c'i1: ct.r (/tl L Or. I aat aqc ) lI I I j I I I I The precedingmatrix has a bandwidthof 3 and is given a specialname-the tridiagonal matri.f i o . of s corresponding ninus [F]. I I on.i f o r i : 1 ..2 Motrix Operoting Rules Now that we have specifiedwhat we meanby a matrix.a y .. n .mand j:l. . .say. i I i I I i A bandedntotix has all elernents equalto zero. are Both addition and subtraction commutative.

Figure 8.5 provides an easy way to check whethertwo matrices can be multiplied.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 .Figure 8. in this case[A].the resulting[C] matrix would have the dimensionof m by /. According to this definition.multiplicationis not generallycommutative: tAttBl+ lBltAl Thot ic rh.4 Visuol depictionhowtherows columns upin of line ond FIGURE 8. by theTthcolumn of the secondmatrix [B]. the multiplication could not be performed. if [B] were an m by i matrix."t . [B] could be an n by / matrix. Thus. if [A] is an m by n matrix. elenent is obtainedby addingthe productof individual elenents from the lth row of thefir$ matrix. s The productof two matricesis represented [C] : tAltBl. multiplication is possible [::]Ll motrix ultiplicoiion.3) where n : the column dimensionof [A] and the row dimensionof [B]. the c. matrix multiplication can be perfbrmedonly if the first matrix has as many columnsas the nunber of rows in the secondmatrix.. That is.oislt11 t:l {8. For this case.r98 LINEAR ALGEBRAIC EAUATIONS AND MATRICES q-l t. m [sxs-1x7-22 II Exteriordimensionsdefine thedimensions f the result o FIGURE 8.' order nf mrrltinlic:rtion is .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.) Interiordimensions are equal.'I rl -> { 1i : [A'l^"n [Blnt = [C]. F.4 depicts how the rows and columnsline up in matrix multiplication. If the dimensions the matricesare suitable. However. wherethe elements [C] as of are dellned as + ) .

r I L. . i . ' u ..for the 3 x 3 ntatrix: l A l : l . l-. .. .s d e l i n e d s de a .t. -. cailed the inver. 0t) a.the multiplication a matrixby theinverse analogous division..J t h e t n r n s p o s e .. For examole.I Lr. J lc1 c2 c:l tnen {c}r : In addition.. fbr which of tAltAl ' : 1 J e ]: 1 1 1 [A] Thus. we We might wish to augmentthis matrix [A] with a 3 x 3 identity matrix to yield a 3-by-6-dimensional matrix: a1t [.l the rr. are ChapterI I will deal rvith techniques using numericalmethodsand the computerto calculatethe inverse lbr lbr suchsystems.l f u... ..in the sense of is to that a numberdividedby itself is equalto l. I ..se [Aj.B.I [... The trartslto.suppose have a 3 x 3 matrix of coefficients. . t l. thereis anothermatrix [Al r. . u . is A tnartrix augrnented the additionof a column (or colunurs)to the original matrix. ( t ) .se a matrix involvestransformingits of of rows into columnsand its columnsinto rows. ..One simple advantage The transpose is that it allows a column vector to be written as a row and vice versa. That is.the transpose numerousmathematical has applications. However. 't2 r.if a matrix [A] is squareand nonsingular. 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 . For is by example.'I MATRIX ALGEBRA OVERVIEW Althoughmultiplication possible. .t a. r . t m The inverseof a 2 x 2 matrix can be represented simply by lAl ': ail422 anq2l I o" -n'tl itrrJ L-at tbr Similarfbrmulas higher-dimensional matrices muchmoreinvolved. matl'lx. is matrix divisionis not a definedoperation..For example. element of ['' l t A l r: I .l In otherwords. The final matrixmanipulation that will haveutility in our discussion uuqmenratiott.lr.. the transpose equalto the zu element the original is of 7.. Two othermatrix manipulations thirtwill have Lrtility our discussion the transin are poseand the augmentation a matrix.. ". ' (t.if J . d1 l ool 0l0l 0 0 r_j ott I LaSt a2t a3t 021 {41 . has a variety of functionsin matrix algebra. s i g n a t elde 1 ri.

t4 B 2l. S o l u t i o n . y.-4 -3 6 2 6 The transpose [A] can be obtainedusing the ' operator: of >> A'. 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 . The following exampleillustrateshow a variety of matrixmanipuwith MATLAB. >> Y = t 5 8 1l. Then we can combinetheseto tbrn. >> z -.-_t 61] . we can perlbrm the operations the singleaugmented ratherthan on the two individual matrices. EXAMPTE 8.7 4 2.i6 27 -3 Next we will createanother3 x 3 matrix on a row basis.rthe matrix >> p B = lx. C r e a t e a 3x 3 m a t r i x : >> A = [1 5 b.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. It is bestapproached a hands-on lationsareimplemented as exercise onthe computer.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. Thus. 1 5 6 -i .First createthreerow vectors: >> x _ 1B 6 gJ.

[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 element-by-element basis by including a period with the multiplication operator as in >> A. if we reverse the order of multiplication so that the inner dimensions match.3231 -0.'?! LTta' usrnq Inner matrix dimensions must agree.4769 .3846 4.8462 0. an error message will occur >> A*D .01s4 0.MATRIX ALGEBRA OVERVIEW Further.2154 0. *B B -35 12 30 32 48 54 2 14 A2 x 3 matrix can be set up >> D = t1 4 3.2462 -a.8308 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.5 B 1l. However.we can subtract[B] from [C] to anive back at [A]: >>C=C-B 1 'l 5 4 6 2 -3 6 7 Because their inner dimensions equal. If [A] is multiplied times [D].6L54 -0.

0000 0.I .V : t t 2 1 x 1* 4:rrr a 2 2 \ 2 1 o 2 1 .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. 3 x 3 setof linearequations. AXAI - 1.0000 -0.natrices pror.0(r00 1.4) : * a32.0000 0.5) . (8.202 LINEAR ALGEBRAIC EAUATIONS AND MATRICES To testthat this is the conect result.3 RepresenlingLineor Algebroic Equotionsin Motrix Form It shoulclbe clear that r.the inversecan be multiplied by the originalmatrixto give the identityrratrix: >> a rl:.0000 0. example.0000 The eye functioncan be usedto generate identitymatrix: an 22 I= f = e)re(3) 0 0t) 0 1 Finally. For a drtrr * a12.rr000 1.0i100 0.0000 -0.Y2 {13113 I can be expressecl as [A]{r} : {b} (8.'ide concisenotation tbr representing a simultaneous linearequations.t2 f 0t1. fi. r 3: bl b.

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. mosl efficientway is to employthe backslash.2.y).b} : '[A] equals identitymatrix. is systems an equation < A equationsthan unknowns.q)*b . (8.6) example how theinverse of Therefbre.. "left-division. hasgreatvaluein the engineering analyses suchsystems. (rows)thanunknowns with moreequations It shouldbe notedthatsystems regression where A m > n.3)l to convinceyourselfthat Also.2 SOTVINGTINEAR MATLAB provides The two directwaysto solvesystems linearalgebraic of equations. n of the second This part of the book is devoted solvingEq. realizethat Eq. typical example of is oDtimization. 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. typical exampleis least-squares with less with n coefficients fitto m datapoints(x.?? n. equation beensolvedfbr {r}.lt shouldbe notedthatthis is not a Thus.. t a:2 . (8.2 SOLVING where [A] is the matrix of coefficients: ttrl tdl - otl . (8.t I I a): arrJ r Lrl-r {D} is the columnvectorof constants: lblr : lb1 bz bt) and {. of (columns). However." 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(. are said to be nndertletermined.5)areequivalent.5)lbr {x}.r1 x2 r:] Recall the definition of matrix rnultiplication tEq.Conversely.5)is a valid matrixmultiplicaEqs.EQUATIONS WITHMATLAB LINEAR ALGEBRAIC 8..4)and (8. matrix inverseitself in the numericalalgorithms. are said to be overdetermined.r}is the columnvectorof unknowns: {r}r : [. underdetermined svstems numerical WITH MATTAB ATGEBRAIC EQUATIONS 8.the equation the becomes Because [A] tr) : tAl r{b} (8.-l l o ..:: o.as discussed Section11.1. other approaches employedin are very efficient way to solve a systemof equations.

:-so -. (8. I L o Start up MATLAB and enterthe coefficientmatrix and the righrhand-sidevector: >> K = t150 v- 100 0.the matrix inversesolutionis lessefficient thanusing the backslash. 184.3.8000 Employing left division yields >> x K\m9 4r. Substituting parameter valuesinto Eq.7.6.s so-l ['.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.8]J {::::.0 50 501 150 100 rJ >> mq - -100 150 -50 [588.1)gives these -i:B : [-l.1. 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.81 588.t}Izs+.l {. 100 150 50.9170 71.7000 784.5130 Alternatively. UseMATLAB to solvethebungeejumperproblemdescribedatthe The parameters the problem are for beginningof this chapter.2020 55.6000 586.204 LINEAR ALGEBRAIC EQUATIONS AND MATRICES As statedat the end of Section8. multiplying the inverse of the coeflicient matrix by the right-hand-sidevec. EXAMPLE . t o r g i v e s t h e s a m er e s u l t : . 0 50 50 586. Both optionsare illustratedin the following example.

>> x = inv(K)*mg

4r.2A2A 55.9170 7I.613A Because jumpers were connected 20-m 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

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, steady-state 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



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




( 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


ffi,ryl M
Circuit (c) Electrical engineering



(d) Mechanical engineering



which, ot steodyslote,con be modeledwilh lineorolgebroic Engineering syslems equolrons.

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



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






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

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 : i5a-iaj:Q Application of the voltage rule to each of the two loops gives ios-isz-ls+:0 -is,rRs+- i+-tR+: i12Ri2* l52R52 Q * : -iosRos - iszR-sr ieRe - 200 : 0 I or, substituting the resistances from Fig. 8.9 and bringing constants to the right-hand side, *151s+- 5ia3- 10i32 10i52 Q : * -20ios-10is:f5ip:200 Therefore, the problem amounts to solving six equations with six unknown currents. These Q

equationscan be expressedin matrix form as





l0 lo

0 o


-1 o o





l12 r52 t3z .oJ

0 o


0 I

Ls -ro

_20 o

r -1 I | --sl ol

0 |





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


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



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 1-s1 x tBl [A]

(3)tBl _ tEl (6)(C],




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



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 ,

algebraicequationsin matrix form and then use MATLAB to solve fbr the unknowns. 8.7 Considerthe threemass-fourspringsystemin Fig.P8.?, Determining the equationsof motion from XF' : ma,for eachmassusing its free-bodydiagramresultsin 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

\ 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.rcos60-l 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 1000-lb 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


Spring Conslont (N/m) BO 50 70 r00 20

Cord tength (m) t0 10 t0 t0



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 M-file to multiply matrices-that [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 M-file to generate tmnsposea matrix. Employ f or. . . end loops to imof thetranspose. it on the matricesfiom Prob. 8.4. Test



R:10O Vr : 150volts


R:2O R=5O Vo: 0 volts






Vr : 10 volts

R:8f) Vo= 150volts

F I G U RP 8 . I 2 E

. ...


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 ill-condition. 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 left-division, fbr of equations: methods solvingsystems linearalgebraic





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 built-incapabilities.



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 MA|LAB.


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: 3-rr 2.rr: *
-,-. --L l,^ -


r, If vee.\uncrhet.i>thezb:cil:ta,{eaan.>olrccachttlthtseteuilUoilsror-rt!
-rl:--.tl+9 -t-t: I -f I t-rl


lines-that 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 three-dimensional 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 ill-conditirnecl.Graphically, this conesponds the to are Ill-conditioned 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.




9.I FIGURE ofthe The equotions. intersection lineor of of solutlon o set Mo simultoneous olgebroic Grcphicol
the linesrepresenls solution.


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


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 ill-conditionine of a matrix.

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


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:atla22-an(7zt For the third-ordercase,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:






ForFig.9 2a:
L _ !


2--l 1


: - ; ( ll ) - t l ; l :/01 \
L \L,/

ForFig.9.2b: D:l l-1 2

r - : - t I : 2 )( 1(-l):0

9 -0. Use Cramer's 0 . b 2 . (Fig.3 *0. b .o r t h r e ee q u a t i o n s .5 -4.01 | 0.44 -0.9rj: canbe evaluated [Eq. 5 1 3: .44 0.52 I 0.. Additionally.0022 10.0022 -0.67 r. . .: EXAMPLE Cromer's 9. 3 x 2 * 0 .1 4 .52 .2c: D_ )' . 9.3 0.s r 0. 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 .5.0 . in that eachunknownin a system linearalgebraic equaCromer's Rule.rr + r:: -0.67 r 1.0649 _ _)a < -0. 9. The determinantD a:o:lol: l?l-o"l3i A:31.043s6 -0.the singular systemshad zero determinants.01 0. .. These ideas will be pursuedfurther in our subsequent discussion illof conditioning Chap.0r 0.0022 I o. .F o r e x a m p l ef.01 10.0022 i o.'.44 0.3 0. _:l :- tr I ___1)_ll_l:_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. .0.8 . l .I l.I I I 0. (9. r r 0 . .03278 .67 0 . 4. 1 r r + 0 . 9 : -0.5| -0.r I - -0.r 0.0022 : 19.'l3i -ooo22 o:l: The solutioncan be calculated as .r 0.e I -0.04 I /-? ?\ 2 \s/ In the foregoing example.3 -0..1)]: as Solution. 4 4 x2f l.6l I o.2c) has a detenninant the resultssuggest that the systemthart ahnostsingular' is that is close to zero.0022 0.2 Rule rule to solve ProblemStotement.216 GAUSSELIMINATION For Fig. 5 2 x 21 + 0. . 3 .

(9.0. (9.61 . Eq.2)might be multipliedby a21andEq.The result is a single equation that can be solvedfor the remainingunknown.0000 1.0422 D=det (A) ii ti 1 1.using the systemfrom the previousexample: >> A-10.0000 0.SOLVING SMALT NUMBERS EAUATIONS OF 217 can The ae-' Function.4) (9.441 -0. (9.1.Consequently.0I. more efficient alternatives used.3)by all to give aztTltxt I a21A12X: AZlbt 2 attbz (e.Someof thesealterare nativesarebasedon the last noncomputer solutiontechnique coveredin Section9.0.9000 For more than three equations.5200 1.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. The determinant be computeddirectly in MATLAB with the det function.0.9000 0.51.5 >> D= 0 .eliminatethe x1 term from the equations to yield : a t t a 2 2 x.a y b t 2 which can be solvedfor a 1 1 b 2a21b1 .Cramer's rule becomesimpractical because. to For example.1.4) from Eq.0100 0.2) (e. determinants time consumingto evaluateby hand the are (or by computer).6700 -0. 9.3000 1.52 I.1 0.3 0.a y l a l 2 x z a t t b Z. -0.3 0.1)= t-0. therefore.9.5) will.This valuecan thenbe substituted into either of the original equations computethe other variable. For example.as the number of equationsincreases.5) : a11a21Xy a11a22x2 { SubtractingEq.5000 (A) /D -14.4400 >> x1=det X1 = 0.{i as in >> A(:.3) The basic strategyis to multiply the equations constants that one of the unknowns by so will be eliminatedwhen the two equationsare combined. Cramer'srule can be appliedto compute.3-the elimination of unknowns. (9.

as described Section lbr the techniquecan be formalizedand readily programrned the conlputer. However.218 ELIMINATION GAUSS (9.1.8c) .8r) :: 1a. were manipulated elin. procedure The consisted two steps 1.which can be solvedfbr Equation into a 2 2 b 1. one of the original equations to a This basicapproach can be extended largesetsofequationsby developing systemto and Gausselimination atic schemeor algorithrnto eliminateunkr..some modifications will be required to obtain a reliable algorithrn. equation this solvefor the remainingunknown.: $t (9. r x .3.: (9.3): of equations.rowns to back-substitute.lrr an J tltt I I to with rnorethan two or three The eliminationof unknownscan be extended systems make eqLrations. (9. tor This sectionincludesthe systematic techniques forward eliminationandbacksubfor are stitutionthatcompliseGauss elimination.However._t-_-t_ lb. a n l l 1O t t A)) l o " ut' l b 1.3 will deal with the additional featuresrequired for an et'fectivecomputer progranl.2.6)can thenbe substituted Eq. to The approachis designed solve a generalsetof n equations: d 1 1 1 1 q s z x z l 4 r . . the numerouscalculationsthat are requiredfor larger systems in 9.-r. 2.. 9.the computerprogram must avoid division by zero.u p b 2 d 1 1 4 2 202yAp Noticethat Eqs.a12lt2 0|tt)2 A2jal? Il. Althoughthese techniques ideallysuited implementationon computers. to one The equations of this elimination stepwas that we had one equationwith one unknown. is the most basicof theseschemes.. attl aybl .9. .84) .7)follow directlyh'om Cramer's rule: th' anl . ' I a 2 . (9.rinate of the unknownstiom the equations.2). The result into could be solveddirectly andthe l'esultback-substituted Consequently. Section 9..the eliminationof unknownswas usedto solve a pair of simultaneous (Fig. the methodextremelytediousto irnplementby hand..In particular.1____J___i_I lrr.x.(9. .' I d 1 1 b 21 a21b1 lQtr tl I c.2 NAIVE GAUSSELIMINATION In Section9. The tbllowing method is called "naive" Gausselimination beciiuseit does not avoid this problem.6)and (9.: S .3 _.t x :* ' " * ayll * ( r 2 X 2I d z r j r : * ' I ay.

3a). + where thc prime indicatesthat the elements havebeenchangedfrom their original values.8a) can be multiplied by a3llayl and the result subtracted from the third equation..) r . Forword Eliminotion of Unknowns.. for For instance.... The procedureis then repeated the remainingequations.. .*.* * a1ux.b. (9. x 3 a " ' * r t l .. .:. or : tt'rrxz . .10a) rq InA\ a \ t x t * a \ .azo of eliminotion:forword eliminotion (b)bock ond substitution As was the casewith the solutionof two equations. .r).dr:.!) !6.. The first phaseis designed reducethe set of to equations an uppertriangularsystem(Fig..4 (a) Forward elimination I (b) Back substitution h: xt: b"z/a'\z (b'r.rrl dz2 azt a32 att o' ['" | { .Eq.. x . multiply Eq.' 4rr .3 The phoses Gouss (ol h.' "" I i o'. FIGURE 9.r:.aptzlfa. (9. (9.: $... + (.. do this.+ .8b) to give (.ELIMINATION GAUSS 9....a|34lftt'22 -t. techniquefor n equations the consists of two phases: eliminationof unknownsand solutionthroughback substiturion.' lt" Lo! .9 u ..e) Now this equationcan be subtracted from Eq.#.'+ 9or... ..The initial stepwill be to elininate the to first unknownr1 from the secondthroughthe nth equations. 9. : (b. 6. (9.8a) To by a21 all to give f a21xy\op*z * alt *Lanxz+ atl ..Repeating the procedurefor the remainingequations resultsin the lbllowing modified system: (t11x1 ctpl2 l * n1-3-t3 .2 NAIVE 2r9 drz an [r. att - eht br Qtt (e.. I a'"r-r. 6 \ .' o'.

. : 6. r 1 1 .-l-)J" .-1 termfrom is to the rth equation. r * .la'i..l )th equationto solve for ir. lOc). 6\ : l o ) .rrx.:lt.r.. the dividing itby all and multiplying it by a21 Sometimes division operationis refened a with as nonnalization.x.l4 1 . (9.. Eq.8a)is calledthe pivot equationandall is called pivot elentenr.x.. t al. Equation ht \ n .11d)can now be solvedfor x. tI d l r r .*ct...r:6\ (9. \l '-1 7 l t ..multiply Eq.l i:i+l fori:n-l...: Bock Substitution.10c) E} a ' r ' r ..:['! :: ai.r*.x. tr\ar2+ aztu * .We makethis distinctionbecause zeropivot elementcan interfere we normalizationby causinga division by zero. 1. . a'1. * a'1.: b'.n-2. and subtractthe result from Eq. . . I a 1 r r . I a'r. this point. .. r r a "'LA1nX. To do this.which is repeated evaluatethe remaining:r's. . r l x2 a'. ..1 t) (9.'* .llc) (r-l) ann pn into the (n .10r) by aj2la\. i it.1 ld) (9. the systemwill have beentransformed an uppertrianguAt lar system: a t t x t I a n x z I r z 1 3 J* .rr.. is to Note that the process multiplying the first row by u21 tr11 equivalent of f to .x.: 11'. : h " t t (9.10d) the For the foregoingsteps.. (9. (9..11r) (9. The next step is to eliminatex2 from Eq. . f ..We will returnto this importantissueafter complete our description naiveGauss of elimination..220 GAUSSELIMINATION 3 ctr. I a'r. can be represented thefollowins fbrmula: il 6ri tt _ 'r \/- . The This result can be back-substituted to by cedure.. nipulationin the sequence to usethe (n . . ro u r r . (9.xz|a1]. ai{tr. --t..:6'.10d).I )th equationto eliminatefhe x..4* . Perform a simila to elimination for the remainingequations yield { 7 1 1 .' have beenmodified twice.. r : f i 1 3 a'zzrz* c/ar4 + .10c) through (9.. (9.. where the double prime indicatesthat the elements The finalmaThe procedurecan be continuedusing the remaining pivot equations..

This eliminates from the third equationand reduces systemto an upper r? triangularform.3) is Multiply Eq. multiply Eq. Eq.2 NAIVE ETIMINATION GAUSS E X A M P L E3 9.3.3. Use Gauss 3xr.5617 .5) (E9.0.o.293333(7.19000011.0.0 .30000 -19.t9.1 22r (Ee. (89. 2 x 31.00333x:.50000) 0. can be solvedfor 7. (E9.293333xt: -19.6).t h e s e to f e q u a t i o nis 0 .34: 0.1(3) 7(-2.2 .5)by -0. (89. (89. This can be verified by substituting resultsinto the .293333x: .3. 4 0 0 3 1 1 . (89.3. (E9.00003andx2 : -2.3 71. 5 ) + l 0 ( 7 .To accomplish this.85+ 0.6).which can then be solvedfor .0.00333x2 0. ^z: -2.3. s t i o n s .00003) + : 3. The first part of the procedure fbrward elimination.3.2x2+ l0.50000 canbe substituted backinto Eq.561'7 10.50000 7.3.00333x: 0. Noive GoussEliminotion elimination solve to Problem Stqtement.rj original equationset: -r(-r).3. &I1d : 7.5617 it After theseoperaThen multiply Eq.rr rrIUSt removedfiom Eq.1(-2.6150 * be To completethe lbrward elimination. as in (E9.oooo3 10.3.lxr* .3. (E9.1)by 0. 4 = ) .3.8).85 rFq 1 4) (89.3 + : : 0 . (E9.3.0120 into This result can be back-substituted Eq. the resultsare very closeto the exact solutionof the rr : 3.293333n : -19.85 -19.1l3 and subtract resultfrom Eq. I (.00003) -19.3. 2 ( .3x1 7. (E9.7) 7.2x2: 7x2-0.t: 0. 0 0 0 0 3 : 1 1 .5. (E9.3(7. solvedfor 70'0843 : 7.1) Solution.7).190000x2 10.l x z 0 .0200x:: 70.8) (E9.9) First.lrz -0.5617 0.-5) 0.00003) + : -"r:: 7.0843 (E9.00333 and subtractthe result from the Eq.3).81999 7.5) .00003) 7.00000 Although thereis a slight round-off eror.9.0.2)to give 7.9)can be We can now solve these equationsby back substitution. 3 ( 3 ).0120-tj: 70.3.l) rFq ? ?r (E9.85 3xr0 .6) 7.3. the by 0.313and subtract from Eq.2.00333 which Finally.2(7.85 : = 0. 2 x 3: 7.3.19.

4. end ? forward elimination for k = 1:n-1 for i = k+1:n .k:nb) . withouL pivoting. if m-=n.Again.222 GAUSSELIMINATION function x = caussNaive(A. oLtter loop movesdown the matrix from one pivot row to the next. nt G e 9.13) to be programmed asa sinsleline.floating-point on operatilv . The inner loopmoves below the pivot row to each of the subsequent rows where elimination is to takeplam. Finally.k:nb) Aug(i. Noticethat the coefficient matrix A and the right-hand-side vector b are combinedin the augmented matrix Aug.J(i.n+1.Aug(n.i) x(i) end .2.Aug(i.nl = size (A) . end end ? back subsr'rue ion x = zeros{n. the operations performedon Auq ratherthan separately A and are on b.k:nb) -factor*Aug(k. (9. FIGURE . factor = Aug(i. (9. The back-substitutionstep follows directly from Eqs. Two nestedloops provide a conciserepresentation the tbrward elimination Al of step./Auq(n. 9. i+1:n) *x(i+1:n) ) iAug(i. ? x=solutionvector lm.k) .b): Gauss elimination ? input: % A=coefficientmaLrix % b = rigirr hand side vector % outpuL.l). MATLAB's ability to perform matrix operations allows Eq. Thus. ior i = n-1:-i:1 .12) and (9. x(n) .2 Operotion Counting The execution tine of Gauss eliminationdepends the amountof .nb). A must be square').n) . error('Matrix nb .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 .(Au. Aug .[A b].b) % GaussNaive: naive Gauss eliminatiorr % x = GaussNaive(A.2. the actual elimination is represented a single line that takes advantage by of MATLAB's ability to perform matrix operations.k) /Auq(k.nb)-Aug(i.13). 9.I MATTAB M-file! GaussNaive An M-file that implementsnaive Gausselimination is listed in Fig.

Together amountsto fl + I multiplications/divisions n and with the singledivision.14e) \-. thereis one division to calculatethe factor.l)(n) and Qt addition/subtractions.tt"r Flops {rr.n 3.'.\ . r r \ -. \. then Because : n f 1... r i r //_.l (ir k)(n+2-k) : k kll n ln k)ln I -k) + rr.rz-m(ml1)-(2mll) :!*o@2) lJ 6-i (:e.I j(r) ln-2)ln-t) Flops {.2 NAIVE GAUSS ELIMINATION 223 providesinsightinto which partsofthe algorithmare Therefore.tolp i 2.' -.11:nt : | +2 +3 +.this iteration of the inner loop.r . n ( r) ( 2 ) ( r) { 3 ) flops Therefbre. (9." (Fig..r'ir /--' /-'"' . On Therefbre. the iterations the of Similarreasoning be usedto estimate flopsfor the subsequent can outer loop.-t /_6". .. going fiom 2 to izb resultsin r multiplications nb and n subtractions. I i I \ . The total for the first pass addition/subtractions every tor throughthe outerloop is therefore .+3-+.l n.9.r-llfu+1) l.+ .+ 2. 2 1 (9. r + o r ir . L' : I * I + | +.. According Eq.9.| 1a.16) .this the to means that the numberof iterations the innerloop will be of f-. i-l (9. as Before analyzingnaive Causselirnination. r.. Now let us examine naiveGauss the elimination algorithm We will first count the flops in the eliminationstage..Thesecan be summarized as '"r... 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 .t1. k : I . +t1t t n ( n r* It: l) : nr_ k+l nr) I O(nt) (9.4) in detail.t:tt_ / i:2 2+l:n- I (9.".2)l.14d).r : l. limits on the inner loop are from i: 2ro n. rt aatiri""Tsritr".. will first define somequantities we that facilitate operation counting: il1 il1 111 In tn tl li \-.t...The next line performs a multiplication and a subtractiontbr each column element from 2 to nb.ion Outer Loop k I 2 tr.f ) where O(rl") means"terms of order tn" and lower.14cd) (9.l)(n 1 l) multiplication/divisions (n .r. totalingup theseoperations most tirneconsumingand how computation time increases the systemgetslarger. ."n". 'o.b) \-r L \-. the first passthroughthe outer loop.tu..t i-l . s) I For every one ofthese iterations.

14) yields + l n 3+ o ( . J L. l : | * o r r . ) ) . on f Because only a singleloop is used.r9) gives Summingthese results ln' ^ J lO(tt') (9. the effort involved in forward eliminationconverges 2n3 3.l) 12.We are therefore ing that for largen.The result is written in this way because n gets as justifieclin concludlarge.1. o G e Bock Substituiion Tolol Flops 2tt313 Percenl Due to Eliminction TABTE . 6 7x I A B 87.224 GAUSSELIMINATION n ( n t .201 Thus.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 .221 oUr'.'' J A similar analysislbr the multiplication/division flops yields + [n3 olf)]- + + [n3 o(n)] [].2\ EXA (9. is The numberof addition/subtraction flops is equalto n(n . These canbe added to arrive at a total of n2+ o1ny Thus. t)tl L_) ' (9.) -ll-I1r\ T * Forward elimination Back substitution 2! * oCl Two usefulgeneralconclusionscanbe drawn ftom this analysis: l.the amountof flops increases nearly three ordersof magnitudefor every order of magnitudeincrease the numberof eouations. (9.t * orr'.the computationtime increases greatly.the O (n?) andlower termsbeconre negligible.back substitution much simplerto evaluate.+ n2 +o(i. the total number of flops is equal to 2nt 13 plus an irdditionalcomponent proportional to terms of orclernr and lower.As in Table 9. . t + o r n z . i t .. 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 .[ n 3 o r n z t ] *[ ] .] : | * orrt.58% 9853% 99 85% . the total efTortin naive Gausseliminationcan be represented as (9.Q t * t )f r + f t ' t t A:t k:t t-l Applying someof the relationships from Eq. the numberof multiplication/division flops is n(n -l l)12. As the systemgets larger. Because the extradivision of prior to the loop.



Most of the effort is incurredin the eliminationstep.Thus, effortsto make the method n.rore efficient shouldprobably focus on this step.


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 back-substitution occur.For examole.if we usenaive Gausseliminationto solve 2 - r 2 1 . 1 18 1 4rr * 6-ru *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 round-off 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 minimizesround-off eror. As such,it also serves avoiding as a partial remedy fbr ill-conditioning.


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 -r-f 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

2.0001 3(2/3) 0.0003

(E9.4. r)


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


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 almost-equal 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.0000-tr 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



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 M-file that implementsGausselimination with partial pivoting is listed in Fig.9.5. prel'iouslyin Section 9.2.1 is identicalto the M-file for naive Gausselirninationpresented with the exceptionof the bold porlion that implementspartial pivoting. availNotice how the built-in 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)



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:n-l eo partiaL pivoting t b i g , i l = m a x ( a b E( A u g ( k : n , k ) ) ) ; ipr=i+k-1; 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 = n-1:-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

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

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

e2 .fz 8z e3 ft

.{1 41




€tt I




' n- |


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 space-saving 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 l-r

-, ll;tt_lTfl ':i -:l t:t t,3;1-l 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:


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 '+#:
t'3 - 83r-l

r5e 48o

,'z - 82x3

rl ^l -



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


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


= e(k) /f (k-1-); factor - factor*g{k-1); = f{k) f(k) - factor*r(k-1); = r(k) r(k) end % back substirurion (n) ; x(n) = r(n)/f for k = n-1:-1:1 x(k) = (r(k)-s(k)*x(k+1) )/f end

(k) ;

FIGURE 9.6 AnMJile soveo tridiogonol to sysiern.


MATLAB M-file! rridiag

An M-file 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.

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 steady-state case,a differential equation basedon heat conservation can be written for such a system as d2T ax-

_ +h'(7.- z):0




7,,: 20

fo: 40


Is = 200


-- Lx;'7,,:20

{ ,

FIGURE 9.7 two temperoture rod between wolls constont different of but A noninsuloted uniform positioned
The {inite-d'flerence representolion 66is5. employsf6ur inls1ie,

where I: temperature("C), x : distancealong the rod (m), h' : aheat transfercoefficient ("C). betweenthe rod and the surroundingair 1m-2), 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.4523e-0 + 20


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,finite-difference algebraic form. For example, the second derivative at each node canh derivatives into approximated as dzT d-t
Ti+t -ZTi *Ti-r


into where I designatesthe temperatureat node i. This approximation can be substituted (9.2q to give Eq. Ti+r-2Tr * Ti*t
n, -2



gives Collecting terms and substituting parameters the -Ti*r*2.04Ti4+r:0.8


Thus, Eq. (9.24)has beentransformed from a differentialequationinto an algebraicequation. Equation(9.26) can now be appliedto eachof the interior nodes:

-To*2.04T1-4:0.8 - Tt * 2.0472 Z: : 0.8 - Tz 12.04\ - Z+: 0.8 - Tt -t 2.0474 Is : 0.8 -


The values of the fixed end temperatures, : 40 and Ts:200, can be substituted Io and with four unknownsexpressed moved to the right-handside.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


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; >> TT-0 (r) 13.452 -j*exp (0.1*xanal) (-0.1*xanal)+20;
t-,r.rj=1:(Ydl--); >> plot it ,'l , 'o' , ranal, Tartal )


. . .

As in Fig. 9.8, the numericalresultsare quite closeto thoseobtainedwith calculus.



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

(c) c 9.4 (


(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 M-file in Fis. 9.6 to obtain the solution:
>> e=10 -1 -1 -11; >> f=12.04 2.04 2.04 2.04); >> g=i-1 -1 -1 01. >> r= [40.8 0.8 0.8 200.8] ; >> TridiaS (e, f, q, r )

tc 9.5 C






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


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 2r1-6x2-r-r:-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 l0-r2: 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.niiss-balarrce 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


0 . 5 ;-1 . r 2 : t 1 . 0 2- 2 x 2 : t1

9.-5 -18.8

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 6-t2* 2.r3: -61.5 .r1.r2*5.r::-21.5 * (l) Solve naiveGuuss bv elirnination. Shorr,' steps the all of computatton.

200 mg/s

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.



9.10 A civil engincer involved in constructionrcquires the lbllowin-e difl'crential equationrcsultsfiom a steady-$ate .1800,5800, and 5700 rnr of sand, line gravel, and coarse massbalance a chemicalin a onc-dimensional fbr clnal: gravel,respectivcly, a building plojcct. Thcre arc thrcc lbr d-t llt: 0:D --U ,--kc pits from which thesematerialscan be obtaincd.The cornd.\' d.r of thcsepits is 1-rosition r.'u'hcre: conccntri.ition,: tinlc, r : distance, = diffuD c / sion coclllcicnt. U - f-luiclvelocity. and li = a first-order 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 sr-rch systems, streamcontainirtga weight tiactionr',,, a of a F,. Hou, nran-v 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 rr-raterial- Thus,lbr stagci, a nrass
' ) t


rnctal.plastic,anclrubber-are 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 tlve-stage ancl if rcactoris uscd.Note that Eq. (P9.l3c) must be modified to accountfbr thc inflou, wei-shtliactions when applied the to filst and laststages. r' ,/ ('\

9.14 A viscous pipe se( mechan flows ir to obtai Q, I Qt) 3Qt



t5 l7 t9

03c o4a
0 5-5




If totalsof 3.89,0.09-5, 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

<_FIOW: A_



process A stoqe extroclion


74+ BC+(3/s)BD:0 AB - G/s)BD:0 -BC+(315)CE:0
-lJ - CD -AD r DE

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

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



€tt I d,

Jr-t €rt

8n .f,,




rl r2 r3

Qt= Qti Qs

Qs=Qo* Q,

15A trussis loaded as shown in Fig P9.15. Using the in-e of equations,solve for the l0 unknowns. set AB. BD.CD. DE, CE, A,. A,, and E, ,AD.


Developan M-file 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_;


-l 9 -4 3 al 1 ,1 0 -7

0 I


:ltii til


. Knowing how to express multiple right-hand-side knowing how to evaluate Given an LIJ factorization. becomes equationswith the sarnecoefficients[A]. a Recognizingthat Cholesky'smethodprovidesan efficient way to decompose can symmetricmatrix and that the resultingtriangularmatrix and its transpose be vectorsefficiently. but with different right-hand'side when solving constants {b}. usedto evaluateright-hand-side when MATLAB's backslash in Understanding generalterms what happens is operator usedto solvelinearsystems. Specific objectivesand topics coveredare . vectors. havechosento employ the terminology LU factori:ationlor consistent approach is Note that LLl tlecomposition very comntonly usedto describethe same the subiectof this chapter. right-hand-side Gausseliminationas an LU factortzation. of s describedin Chap. . the involves decomposing coetlcient matrix that LIJ factortzation Understanding different into two triangularmatricesthat can then be usedto efficiently evaluate vectors. 9.the tenns "factorization"and "decotttposition" synonyrnous. rizotion LU Focto OBJECTIVES CHAPTER The primary objective of this chapter is to acquaint you with LIJ factorizationt. .1) inefficienl it a Although it certainly represents soundway to solve such systems. | [n the parlanceof numericalmethods. . be To are we with the MATLAB doculnentation.. Gausselimination is designedto solve systems linear algebraicequations: [A]{r}: {b} (i0. ' .

2)is the resulr. The results can be directlyextencled ir-dimensional to systems." multipleright-hand-side vectors can be evaluated an efTicient in manncr.2)could be expressed au uppertriangr"rlar as system. For fbr a3x3svstenr: ['l t/l- Ll12 . fbllowing explanation limited to a setof threesimultaneous the is equations. elimination is used to reducethe system to upper triangular form.However.6) lLllul: lAl and (r 0 . Ir 0 0l I 0l ( r0 .a)is premultiplied it. elimination itselfcanbe cxpressed an LU facrorizttlion. (r 0 .let us lirst providea mathernatical oven. r-} { c t l l : t A l { r } . That is. r .In to we addition. 5 ) Ll.3).I OVERVIEWOF LU FACTORIZATION Just as was the casewith Gausselimination. t t l I t u ] { . Thus..[ b ] If this equation holds.rl's on the diagonal..{ b }: 0 (10.2.3)can alsobe expressed rnatrixnotation in anci rearranged give 11'r t u l { .{ d }: 0 } Now assume that thereis a lower diagonal natrix witl. by That is.it follows fiom the rulesfor matrixnultiplicationthat (10. Gauss as Bcfole shclwing how this can be done. -l . will omit pivoting. 7 ) ILlldj : lbl (10.ation methodsseparate time-consuming the elimination of the matrix [A] from the rnanipulations the right-handside {b}.OVERVIEW [U FACTORIZAIION OF Recallthat Gausseliminationinvolvestwo steps: fbrwardeliminationand back substitution(Fig. simplifythe followingdescription. Equation(10.. r ) tLt li.2 ) Suppose that Eq.the forwald-elimination compriscs step the bulk of the conrputational etlbrt. of LU factoriz.2.As we learned Section in 9. once [A] has been "factored" or of "decornposed.r': Lltt l/ rr I I 0 . (10.r.I I can be rearranged give to l A l { r } . (10.8) .. 9. /r: l l that has the propertythat when Eq.This is particularly truefor largesystems equations. I O.3) Recognize that this is similar to the manipLrlation occursin the first step of Gauss that elimination. Eq. pivotingto avoid LU factorization requires divisionby zero. example.f- :1 t_ d1 dt dz t 0.iewof the factorizationstrategy. ( Equation 10. ( 10. Inter-estingly.

238 IU FACTORIZATION tAl {.. LU factorizationstep.the resultis substituted Eq.I a i . fbr Now let us shorvhow Ganss elinrination be imnlemented this wav.Recall that the forward-elimination is step intendedto reducethe original coefficientmatrix [A] to the form ot' ac 4rr -l f .3).r}: {ll} tut f. 7 )a n d( 1 0 . Then.l 0 assJ whiclr is in the desireduppertriangularformaf. Gauss elimination can be usedto decompose into [Ll and IU]. ( 1 0 . (10. This can be easilyseen [U]. I lUl:10t- L0 . in A two-stepstrategy(seeFig.torirution { lul A lLl . 1. .lul V ir) : trl) t {ri I Substitution I . I ) for obtainingsolutionscan be basedon Eqs._- tLl I {d}: {b} V {d} I .I I ihe steps 1Uloctorizotion.[t] and [U] areusedto determine solution{x } for a right-hand a side of First. 2.". 8 ) : . can in I o.o FIGUREO.Eq. I 0.0 |. [A] is factoredor "decomposed"into lower [L] and upper [U] triangularmatrices.(10. ar. This stepitselfconsists two steps.8)is usedto gerrerate interan {b}.. mediatevector{z/}by forward substitution.2 GAUSSETIMINATION [U FACTORIZATION AS Although it might appearat face value to be unrelatedto LU factorization.( 10.3) into which can be solvedby back substitution {x }.. Substitution step. which for [Al is a direct product of the fbrward elimination.

system... in a. in a. azz and subtractthe result from the third row to eliminatea-.we could savethe/'s and manipulate later. alsohaveto do the sameto the righthand side {b}.9)] 0il Jzt:- 0)l and subtractthe result from the secondrow to eliminatea.. a22 ott I r r 1o.ri AS I O. But thereis absolutelyno reasonthat we have to perform the manipulations Thus. (9. represents efficient storage the LU factorizationof [Al. . .a.. '... .1 (r0.r. elirnination andl..t. an lAl tLltul (10.After elimination.row I is multiplied by A2t | 1t Ltll fiom the third row to eliminatea.I (r0. ur or.: l. in fact. 7 1I3 -rl I o. a\.t .Thus.fsr ..r 3 ) a12 n'.J. T a ' r ..f:z 1I The following exampleconfirms that [A] : tLl[n..r'? was to createzerosin at1..the [A] matrix can therefbrebe written as l . {/lt apl If..2 GAUSSELIMINATION IU FACTORIZATION the Though it might not be as apparent..we can storerr in a.-| 1: - .: The first stepin Gausselimination is to multiply row I by the f'actor[recall Eq. {b} Recall that the whole idea behind the Where do we store the factors/. This can be readily illustratedfor a three-equation f a r r o t z . Similarly. aj2 ar ) _ti.. andf..1l) where f lUl :10 and on L o t o.. of This matrix.r2) [r 7rl:lft L . simultaneously.f.1.r.. . we Clearly.if we do not want to changethe equations. matrix [L] is also producedduring the step.10) ) L... Now supposethat we merely perfbrm all these manipulationson the matrix [A]..... a \ . The final stepis to multiply and the result subtracted the modified secondrow by t..and a3.fp ai:.) 0 0l 1 0l 0 0 .

(10.3 :0.033333] | 1 0 . : 7J0333 : -0. Solution. solutioncan be generated a panicularright-handa First.the fbllowing uppertriangularmatrix was obtained: -0.00333 0.2 the where minordiscreoancies dueto roundoff. are for Afier the matrix is decomposed.f.21 [ 3 -0. 2 r 0 I1 10. at the end of this step.3.r -0. 0.1 . The elements and d11were eliminatedby using the factors d21 EXAA .19 . a forward-substitution stepis executedby side vector {b}. This is done in two steps.3 1 I -0. 1 0 0 0 0 0 . 0 1 2I0 0 L0 employedto obtain the uppertriangularmatrix can be assembled into a lower The f'actors triangularmatrix.the right-hand on .0271.99996 ) -0.1 -0. ln Example 9.jeeeeee 7 L 0. 0. Derive an LU factorizationbasedon the Gauss eliminationoerformed previously in Example9.1 -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. the lower triangularmatrix is 1 0l 0 [ r 0l tll :10.2 I the Thisresult be verifiedby performing multiplication [L][L/]to give can of lLltul:l}.UFACTORIZATION E X A M P L E0 .0333333 /. that this merely amounts performside ing the eliminationmanipulations {bl.0333333 r L 0.3 [ 3 -0. 0 2 7 1 3 0 0l l L 0 tI I 0 otT3 tt -0. Thus.I..3.100 Thus.100000-0.3 After forward elimination.3 7 lAl:10.0271300 J uentlv the LU factorizationis Conseq .1000000 and the elementd12woSeliminatedby using the factor -0.2 9.293333 [A):[LilU):10.0 ..0 . 2 9 3 3 3 3 tul 1 1 0 .8)for {d}.2 [3 I : 1 0 7 . we used Gausselimination to solve a set of linear algebraic equations that had the following coefficientmatrix: -0. It is importantto recognize to solving Eq. 0 0 3 3 30 .1 -0. I r 0 | l0 7. 0 1 2 I0 0 1 0 . f u: . :.1 :0.

.n The second step then merely amounts to irnplementingback substitutionto solve Eq.85 -19.i3 I.J. : drf Q.l2) and (9.r -92lJ.. : 7. 8 s I l0 -0.85 -J l I J The forward-subsriturion phaseis implemented applyingEq. lcompare t.0 .(9. I [ 7 ..4 + 7. ( 10.l3)l.100000dr-... it is importantto recognizethat this is identicalto the back-substitution phase conventional of Gausseliminarion with Eqs..0271300d2 d3 11. 0 2 7 1 3 0 03l{:l L 0. As just stated..4]and {}} sirnultaneously.: -19..l0..1 -0.5617 : .: r o I [ * r | | t r .Again. Recall that the systembeingsolvedis -0.3 d2 0. Soluiion.2 l l l : l .1r or multiplying out the left-handside: : d1 7.3). ? fo1 0 0 0 0 0. (10..os+: T3 -0. rI Lo.1 by generating the in the final solution with forward and back substitution. .g): by o. I J [.2 ( 7. 7 . +J andthattheforward-elimination phase conventional of Gauss elimination resulted in tfxr I -02e3333 : | -'i?i'tI 7oo.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.ibi j:l i-l fori : 1.2.0333333dr+ :-19. intent of forward substitution to imposethe elimination the is manipulations that we had formerly appliedto [A] on the right-hand-side vector {b}.3 * 0.85 0. Ll.3|.85.0 0 | Ilj l t0. . . which can be substituted into the second equationto solve for d : .0333333(7.yr | . 3 . 'fl:-- u11x1 | j:i+1 uii .2 GAUSSELIMINATION TUFACTORIZATION AS will be in the samestatethat it would have beenhad we performedforward manipulation on [..0lt0 lrr L zo. The fbrward-substitution stepcan be represented conciselyas s-.l e .4 We cair solve the tirst equation for d.IO.85) -19. tl.0.3 1t.0 . il d.:fi.

r*y {.2 I xl .I and 10.Note that this function usespartial pivotingt0 avoid division by zero. 2 . andd.ill.3).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.io''.r -0 .2.8s 7 -o.IU]{. has the general the It = lu (X) lL. I The LU factorizatioualgorithmrequiresthe sametotal flops as for Gausselimination. lO.02713(-19.r I or Inr 12.4. substitution are phasetakesa the little more eflbrt. 1a n d 1 0 . : {.:1 I _o1 ro ll*. EXAMPLE 0. (10.t-t) where I and u are the lower triangularand uppertriangularmatrices.3 1 tU Foctorizotion ith MATLAB w Problem Stotement.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 . derived fron-rthe LIJ factorizalionof the matrix -x.242 TUFACTORIZATION Bothd.5617) + Thus. I I :..3 for details)for the final solution: n b {d\ : I t o.ql -_02llri .0843 dt : 7 1. |l: l-ro..l MATTAB Function:ru MMLAB syntax: has a built-in function lu that generates LU factorization.1 (7.0120 L0 ) 13 -0 .85) 0.0./} This resultcan thenbe substituted Eq.2: -0r f . Use MATLAB to compute LheL(l factorization and find the solutionfor the samelinear systemanalyzedin Examples10.00333 -0.. The only differenceis that a little lesseftbrt is expended the factorization in phase sincethe operations not appliedto the riglrt-handside.r} : {di: into 0 7.2e3333 | | 0 10. canbe substituted thethirdequation give into to : 70.Conversely.respectively.#*vl whicl. I ltt.

0843 And then Llsethis result to compute the solution >> x = U\d 3.85.2933 t].1000 1.11 70.0000 0. we flrst cornpute >> d = L\b d= ?.1(1 0 0 0 1.I >r b = 17..1004 0.ai2a 3.0000 0.1 .3.0333 0.2000 0.8500 19 .1.3.4l. the LU factorizationcan be computedwith L- 1. Next. 5.2000 0.0000 2. The coetlicientmatrix and the right-hand-side vectorcan be entered stanin dard fashion as >> A = t3 -.3000 10. C)001r' -0.GAUSSELIMINATION IU FACTORIZATION AS Solution.a27I 0 0 1.3000 -0.2000 0. iL'00 1.0000 To generate the solution.2 I0).0000 -0..0000 These results conlbrm to those obtained by hand in Example 10.0033 0 This is the sameresultthat we oblainedby hand in Example 10. . -. We can testthat it is correct by computingthe original matrix as >> L*LT 3.5000 7.2.01r00 a 0 0. 1 .r. -19.3 'lI.2.0000 -t..

Thefactotization can be generatted efTiciently recurrence by relations.Compute Cholesky the factorization thesymmetric for matrix I A t -l?rt 1. science are They offer computational Specialsolution techniques availablefbr such systems. to otz q tt - EXAM Llll at3 .rrl..l4) ciln be rnultipliedout and setequalto eachother.r6) EXAMPLE 10.15) ernployedtocompute is u11:Jan:16:2.44949 5-5 :22. canbeused deten.. of The termsof Eq.2 .f a.nine l6) Eq. the resultingtriirn-eular factorsare the transpose eachother.5 22s I 1 5 5 225 919) Ib I r-5 Solution..44949 l-5 For-the row (l : 2): second : 1r. . : .. (10.ntion One crf the most popular approaches Choleskydecornposition).vmmetric nratrix is one whereeij : aji for all i and Inother rvords.u) That is. Eq. Such systems occLlrcommonly in hoth mathematical engineering/ and problemcontexts.rt 1l ai1 ' 1. ( 10.3 CHOTESKY j.4 CholeskyFoctorizotion Problem Stotement./55 : (6. Forthe firstrow(i : l).123124 2. for (alsocalle/ involves Choleskt ._l | upltl11 forj-i+1.45366 2.1833 . algorithmis based the fact thata syrnmc'tric This on matrix can be decomposed.244 IU FACTORIZATION FACTORIZATION I O.[Al : IA)r.(10.For the lth row: (l0.44949 Then.factoriz.12372q2 4.r - L ll l :6..5 s5 I 5.r l.n tlii (r0. in as l A l :l u fw l tr o. advantages because only half the storageis neededand only half the computation is time required their solution. 8 tlrirta s. Recall from Chap.

First.15366) 225. .it can be used to determinea solution fbr a rightvector hand-side vector {b} in a mannersimilar to LU factorization.6. we looked at in the EXAMPLE Cholesky Foctorizotionwith MATLAB I O. : @ : 6 . .the final solutioncan be obtained solving by (10. i t * u l . an intermediate is created solving by {d} IUlr {d} : {b} Then.r8) the It MATLAB has a built-in function chol that generates Choleskyfactorization.5 factorization the same for the Problem Stotement.3 CHOLESKY 423 Ul2ut3 Ll22 245 24(22. (10.I 8 3 3 F o r t h e t h i r dr o w ( i : 3 ) : u . Note that for this case.chol (x) where y is an uppertriangularmatrix so that t/' * t/ = r.l MATTAB Funcfion: cuot (r0.. 9 1 6 5| 6 .4. t/ .. the Choleskyfactorizationyields r 1 JJq4q t6 I 11711 11 457661 | tul : | L 4. fo . r : . in [6 t A l: l l s 155 ls ssl ss 22sl 2 2 5 9 1 9J Also obtain a solutionfor a right-hand-side vector that is the sum of the rows of [A]. 1 l o l o l Thus.l4) to seeif theirproductyields originalmatrix [Al.I l 0 l 0 l I into The validity ofthis factorization canbe verified by substituting and its transpose it the Eq.the answerwill be a vector of ones.| 231 :10.17) l u l { x }: l d } lO. After obtaining the factorization. The following exampleshowshow it can be employedto generate both the factorizationand a solutionfor the samematrix that previousexample.9165 4 . hasthe generalsyntax.3.1833 2 0 . This is leflforan exercise. Use MATLAB to compute Cholesky matrix we analyzed Example10.FACTORIZATION I 0.

0000 15.45.mul minethat I 10.0000 1.4Use Prob.31 2[). 15 55 225.10. can provide a simplifid we -t^.l Dete of equatio tion.246 TUFACTORIZATION Solution.0000 15. :'> A = The matrix is entered in standard fashion as f 6 l5 55.5 6.1813 0 22 .4 MATTABIEFTDIVISION We previouslyintroducedleft division without any explanation how it works.0000 1..:)ll 16 295 7259 Next.91{. A right-hand-side vector that is the sum of the rows of [A] can be generatedas >> b = fsum(A(1. we flrst conrpute >> d = A'\b d= 3r. r23t 4.:)) .5Solv factorizat 2xr -3xr - .0998 6.3 thesystem lblr : I O.r1 -3xr xrj r.1101 And then use this result ti'r compute the solution >>x=A\y l0.0000 55.3 Use system ac 10. sum(A(3.4495 0 0 6 .0000 Then.0000 55. the Cholesky tactorization can be computed with >> U = chol(A) u= 2 .^-:^ti ^^ ^f :*^-^-^ri.0000 To generate the solution.0000 979..- 10.0000 225. and( versionof 10.0000 225.1101 We can test that this is correct by computing the original matrix as >> U' *IJ 6.Now thu of we have some backgroundon matrix solution techniques. 10.0000 55.2 Use Eqs(10. .0269 25.. 55 225 919). :))/' sum\tt(2.

r1 -r2*7x3 : -34 -Rr. First. [A] in to lU Use factorization solvethe systemof equations Also solve 10. -)O . dethe velop a function that is passed squarematrix and returns the triangular matrices [Ll and [U]. 2 It should be noted that in the event that solutionis obtainedwith an approach [Al is not square. That is.2 1. (10.a simplified overview can be outlined. 10.7) (10. Showall the stepsin the computation.fac t or i zati on. : .Although the detailsof the algorithmarebeyondour scope.6 Developyour own M-file to determine LU factoization of a squarematrix without pafiial pivoting. or (c) symmetric.6rz + 2x'.14)to the Example10.l Determine total flops as a function of the number equations the (a) factorization.(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.(b) triangutar(or easily transformed that are techniques the solution is obtainedwith the efTicient of thesecasesare detected. include bandedsolvers. If none of thesesimplified solutionsare possibleand the matrix is square.6) Solvethe following system of equations using LU with partial pivoting: 2x1-6x2.MATLAB invokes a When we implement left division with the backslash MATLAB examinesthe algorithm to obtain a solution.If any banded. These include systemsthat are (a) sparseand into triangularform). develop a function that is passedthe symmetric matrix and returnsthe matrix [tIl. Q PROBTEMS the l0.a least-squares c allecJ R .4by substituting results and l Ll yieldslA l. highly sophisticated structureof the coefficient matrix and then implementsan optimal method to obtain the solution. ln essence.8 and use the builrin function is working Ibf = Lt2 18 .: -61 . and Choleskyfactorization.13:-38 -3.5 I x z + 5 a : . -L -)v.8)follow from Eq.PROBLEMS 247 operator.3. (10. That is. 10. verify that the productof l L/17 l0.7 Confirm the validity of the Cholesky factorizationof into Eq. svstem an alternativerisht-hand-side vector for }:t*l tl ilill{i: (b) Verify your hand calculation with the built-in cnol function.back and forSome of the techniques availablefbr such systems.6).xt: . and Usenaive Gauss elimination to factor the following to according the descriptionin Section 1 0 . Test your function by using it to solve the systemin Prob. 2 : rhe 10. 5 multiplythe resulting [L] and [{l matricesto deterthat is produced.9 Develop your own M-file to determinethe Cholesky factorizationof a symmetric matrix without pivoting.Conlirm that your f'unctionis working properlyby verifying that [L][Ul : tA] and by using the builrin function 1u. MAILAB checks to see whether [A] is in a format where a solution can be obtained without full Gausselimination. ward substitution. Test your function by using it to solve the systemin Prob. 10.tl (a) Perform a Choleskyfactorizationof the fbllowing b1 system hand: symmetric 27 l2rz.3. (c) Employ the resultsof the factorizationIL4 to vector.'a general triangular factorizationis computed by Gausselimination with partial pivoting and the solution obtainedwith substitution. determinethe solutionfor the right-hand-side 10.

s ol 1l [8 2 7 2l l3 L2 -r e_.6667 I I L -0.15) and 0 0 .U (a) Determine the LU factorizationwithout pivoting by hand for the fbllowing matrix and check your resultsby validating that [L][U] : [A|. 16 ) ? L. 3 6 3 6 .lrr*l 3.6364) L .2 x 2 * . IAl:10 o [e 2.2 t 2 * 5 . (10.14 Computethe Choleskyfactorization of 10.j (b) Employ the result of (a) to compute the determinant.248 IU FACTORIZATION factorizationto determineIU] sothat 10.r0 44 . 10.rt]_6xz-4x7: . r r : .10 Solve the following set of equationswith LU factorization: 3 x 1..3333 . (c) Repeat(a) and (b) using MAILAB.13UseCholesky 10.tl 2) 1().1 0 Z. lAl:tLlfUl: [1 | 0. 1 3 : .26).12 Use the following LU factorizalion to (a) compute tl.0 .2 6 44 LA): W)rlul: [2 l-r Lo -r -1 0l 2 . ] ' [3 -2 1.x 1.redeterminant and (b) solve [Al{r} : {b} with {b]r: Lo o 4-l 0l Do your results make sensein terms of Eqs.3333 -^.

called the inverseof [A]. we introducedthe notion that if a matrix [A] is square. for which : t A l t A l . Specificobjectivesand topicscoveredare o r r o r Knowing how to determinethe matrix inversein an efficient mannerbasedon lU factorization.l Colculoting the Inverse The inversecan be computedin a column-by-column fashionby generating solutionswith unit vectorsas the right-hand-side constants.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.l. the Knowing how to use norms to computethe matrix conditionnumber. I l. how the magnitudeof the conditionnumbercan be usedto Understanding estimatethe precisionof solutionsof linear algebraicequations.': [ A ] .1) Now we will focus on how the inversecan be computednumerically. addition. For example. t.if the right-hand-side constant . thereis anothermatrix [A]-r .2). Then we will explore how it can be usedfor engineering analysis. Understanding meaningof matrix andvectornormsandhow they arecomputed.r t A l t / l (r1. Understanding how the matrix inversecan be usedto assess stimulus-response characteristics engineering of systems.1.l THEMATRIXINVERSE In our discussionof matrix operations(Section8.a methodto assess matrix solution'ssensitivity In a to roundofTerror is described.

zzz+s o-l l A l ' : l .2 I 1 0l ttl:10..] 7 .if a unitvector with a I at the second row'is used [b]:{ tol columnof the matrix inverse.3 1 10.3 -0. The tirst column of the matrix inversecan be determined perfbrmingthe forward-substitution solution procedurewith a unit vector (with I in the first row)asthe right-hand-side vector.03333 -0.ratrix inverse: o f o. 2 9 3 3 3 3 tul:10 1 1 0 .ui] {l}:{i} [: l0 l[.o .01008j which can be solvedby back substitution {.0.8]) -0. Employ LU factorizationto determinethe matrix inverse the for from Exaurple10.1} the resultingsolutionwill be the first column of the matrix inverse.r -0.1 7 _0.0333333 l0 L 0.r}r : 10. Recallthat one of the greatstrengths LU factorization that it providesa very efficientmeans evaluof is to ate multiple righrhand-sidevectors.0 .l: system -or -o)l I l t-""-l Al:10.Similarly. 0 1 2_ j 0 0 L0 [r -o.1 Recall that the lactorizationresultedin the following lower and uppertriangularmatrices: 7 . it is ideal fbr evaluating multiple unitvecton the needed computethe inverse. t .1.250 MATRIX INVERSE AND CONDITION has a I in the tirst positionand zeroselsewhere. o r 2[ ' . 2 e 3 3 3l. o o s r so o l I nntooR n n I . the lower triangularsystemcan be setup as (recallEq.l1 : l : l 1 .1000000. to I E X A M P L EL l MqtrixInversion Problem Stotement. [0.1333 1 I lo [-orooeJ -0. 0 0 3 3 .h t\ t ' s) f0l l} {il.2 10 _.027130J t I o o'l by Solution.rtion {d}r : ll for can then be used as the right-handside of the upper triangularsystem(recallEq. 0 0 3 3 30 . o r o .10091.00518 -0. J Io -or -02 [ r 0. Thus. Thisvector and solvedwith forward substitr.-1-1249 for which is the first column of the u.. .Thus.[10. the resultwill be the second The bestway to implementsucha calculationis with LU factorization.3]): [r*-0.

(10.8) is formulatedas [ro l | 0. stateor response.thesesystemsare linear and. vector {b} containsthose elementsof the balancethat are indeThe right-hand-side pendentof behavior of the system-that is. they arethe massof chemicalin each vertical forcesin eachmember.004l83 0.In either case.010080.they In represent the or externalstimuli that drive the system.THEMATRIX INVERSE To determinethe secondcolumn. force. .00sr8 o.For many cases.002111. fbr balanceequations. {r }7 : 10.In a fbrce balanceof a structure. heat.the momentum.4) have a definite physical interprethe tation.00s18 0.oo4s44 0. and the resultsare usedwith Eq.resultingin a set of equationsdefining the behavior of the property for the entire system.142e03 0 with {b}/: l0 0 1l to solve fbr Finally.3)to determine {r}' : 0.the elementsof {x} are the levels of the property being balancedfor they represent horizontaland the eachpart of the system.0333333 L 0.zzz+e f -0. many problems.2 Stimulus-Response Computotions and As discussed PT 3. (10.t.in that eachequationmay include one or more of the variables or from the other equations. ( I 1.1429030. the sameprocedures can be implemented .099881 which is thefinal columnof thematrixinverse: [A]-' : | -0. they are constants.004183 | L -0.For the massbalance.006798l that Thevalidity thisresult bechecked verifying [A][A] ' : Ul.the propertiesmight be the mass in each reactor of a would yield similar examples.oo4s44 0.4) Now. Eq. chemicalprocess.142903 0.1.01008 0. For a force balanceon a structure. therefore. many of the linear systems equations in of arisingin engineering laws. terms of Eq.electrostatic propertiesmight be horizontalor vertical components the forcesacting on eachnode of of the structure.0998801 t 03324so.0021100.which is the secondcolumn of the matrix inverse: 10. coupled.0027r0 ol o. of can by | 1.they represent system's the determine.100000 -0. For example.006798 0.of the exactform dealt with in this chapter: [A]{x}: {b} (11.Theseequations are interrelated. The mathematical expressionof these laws scienceare derived from conservation is some form of balanceequationto ensurethat a particularproperty-mass. potential-is conserved.For a mass balance. which we are trying to reactor. Other fields of engineering and science A singlebalanceequationcan be written for eachpart of the system.027r 300 'lllil :{:} This can be solvedfor irl).004944 [Al ': | | 0_j L -0.

\'J : . the nratrix of coeflicients [lJ usui.4) mi-qht h reexpressed as : {s IInteractionsl{response} timuli} As we know from plevious chapters. \ ' .(11. Even for sntal.The fbrmal solution can be expressed as h : l: [ A l .xrrameter. 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. rr. which are retlected in the coefficients of no' trnda. l. Eq.r1. we used MATLAB to solve this system for the vertical positionsof the jumpers(ther's).. -f rt. Consequently.illycontains the . r. roportionalin' meansthat multiplying the stimuli by a quantityresults the P in re. {l{.rso -roo -o 0 r00 rsO s I --50 s0 I | I. r: ]{1r} or (r'ecalling detlnitionof ntatrixmultiplication our from Section . theretbre.rltositirn meansthat if a systemis subjectto severaldifTerent stimuli b's). At the beginning chap. can draw the general we conclusion theelement that a. exhas trernelyuseful properties.I L t.6 I In Example 8.toftheinvertedmatrixrepresentsthevalueofx.252 INVERSE MATRIX AND CONDITION Finally.. on tilr | L 0 I .etiil -ss8.. 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.4).. such behavior individualstimulus-response of interactions would not be intuitivelyobvious. ThLrs.r thatexpres how the parts of fhe systernirte ract or are coupled.l systems. lr. In the present example. respectively. D. the of elementrr.the matrix inverseprovidesa powerful technique understanding interrelation1br the shipsof component partsof complicated systems. we lind that the invertedmatrix itself'.r1 l.. there are a variety of ways to solveEq.I. to of Notice that theselbrmulationsare Iinearand.cluetoaunitquantityofb. eachof its elementsrepresents response asingle the of part of the system a unit stimulus any otherpart of the system. Usin-e exanrple the structure. As such. MATLAB to compute matrixinverse use the and .tr -a u n i t l e v e l o f l r s . T h i s r e s u l t r s eof o 1 independentof the eft-ects b2 and 6: on .l' of the matrix inversewouldrepresent the fbrce in member i due to a unit externalforce at node1. That is.2) 8. sponse thosestimuli beingmultipliedby the sarne to quantity. ( I I . L {lt I /)l -f {/l. Therefore. lrl + L (lr-t /tt lr l/rl l)t + Thus.\'t : . the responses be corr-rputed individuallyand the resultssummed obtainatotal can to response. Supe.We deriveda s)'stem linear by of algebraicequations basecl fbrce balances eachjuniper.asidetiom providing a solution. -f rlt l /)r I (l)t l)l f l.'..using the matrix inverseyielclsatparticularlyinteresting result. superposition proportionand (the ality hold.I _ . However. EXAMPLE2 I I. 8.2. we set up a probleminvolvingrhree of individualssuspended vertically connected bungeecords.

j : / .02 m.the numbersin the secondcolumn (j :2) indicatethat applying a force jumper would move the first jumper down by 0. and 20 N were appliedto the first.2 ERROR ANALYSIS AND SYSTEM CONDITION Aside tiom its engineering and scientificapplications. and third jumpers.0300 0. This makessense.. inversealsoprovidesa meansto the discernwhether systemsare ill-conditioned.03 m because along with the first cord. The inversecan then be computedas >> KI Kf = = inv(K) 0. 0 5 ( 2 0 ) 2 . This can be done simply by using the appropriate elements the third row of the inverseto compute. 50. 0 3 ( 5 0 ) 0 . ln contrast. because additionalforce would only elongatethe first cord the by that amount.03 m. The 0. r A F r : 0 .Three direct methodscan be devisedfor this purpose: l. 100 150 50. Scalethe matrix of coefficients[A] so that the largestelementin eachrow is l.0300 0.0200 0.the third jumper showsthe identicaltranslationas the second jumper as thereis no additionalforce on the third cord that connects them.O -50 501. but the second of I N to the second and third by 0. Superposition and proportionalitycan be demonstrated using the inverseto deterby mine how much fartherthe third jumper would move downwardif additionalforcesof 10.And of course.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. Start up MAILAB and enterthe coefficienrmatrix: >> K t150 -100 0.the third column (.0200 0. First.0200 0. second. However. I of the invertedmatrix represents vertical change k.0200 0. 3 r r A F r+ k r j a f ' 2 + k . As expected. the third jumper is moved fartherdownward.02-m 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. for the secondjumper the elongationis now 0. However. Invert the scaledmatrix and if there are elementsof [A] | that are severalordersof magniq\/{fcm .0500 Each elementof the inverse. observethat the numbersin the first column ("1: l) indicatethat the positionof all threejumpers would increaseby 0. the in position (in meters)of jumper I due to a unit changein fbrce (in Newtons) applied to Jumper/.now because the additional when the force was appliedto the second of elongationof the third cord.respectively.0200 0. 0 2 ( 1 0 ) 0 . the secondcord alsoelongates due to the additionalforce.02 m if the force on the first jumper was increased by 1 N.0300 0. of A r . 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.I I . J m : + + II.

1 Vector qnd Motrix Norms Anonn is a real-valuedfunction that providesa measure the size or "length"of multi.254 MATRIX INVERSE AND CONDITION 2. b.rn a Euclidean norm Similarly. indicatet that this length is referred to as the Euclideannorn l"r1 x2 j..it would be pret'erable obtail to a singlenumberthat could serveas an indicatorof the problem..2. + f of Ln. l1. lengthof The this vector-that is. b. c)-can besimpty computedas lFll.l) thatcan Euclideanspace be represented as lFl:la b cl where c.respectively. 1. where the nomenclaturell F ll. Multiply the inverseby the original coefficientmatrix and assess whethertheresultis closeto the identity matrix. of | 1. Although thesemethodscan indicateill-conditioning. the distancefrom the coordinate(0.l : would be computedas llXll": if \ t. for an n-dimensional vector lX. 3. 0. it againindicatesthat the systemis ill-conditioned. A simple exampleis a vectorin three-dimensional (Fig. inal coefficientmatrix. . Invert the invertedmatrix and assess whetherthe resultis sufficientlyclose the to orig. i:r i FIGURE I.Attemptsto formulate such a matrix condition numberare basedon the mathematical conceDt the norm. 0) to (a. : {/o\ h. If not. it indicatesill-conditioning. of componentmathenaticalentitiessuchas vectorsand matrices. and c are the distances along the r.I I Grophicol depictton o vector Euclideon of in spoce .and z axes. If not.

2. in contrastto vectors. \ll l /iX l ll t . Usinga similarapproach.Whereasthe Frobeniusnorm llAllf can be easily deterrnined by Eq..\-r. we will learnmore about of eigenvalues.\/-J /r--t "? I (il. As with the other vector norms. 11.the matrix 2 norm llA llz is calculated as l lA l l : : ( / r n ' " * ) ' ' " where4..tot'tn. of Another the is maximum-magnitude or uniform-vector (p : oo). is the mirrimum nonl and. This is called the columnis sum . resultine in a unifbrm-matrix or row-sumnorm: l l A l l . A similar determinationcan be made for the rows."* the largest is eigenvalue [A]r[A].therefore.r i= | whichrepresents normasthesumof theabsolute the values theelements.-l ". a summationof the absolutevaluesof the coetlicientsis performedfor eachcolumn.{l tinll/ - \. ( t I .5).7l 1:1 It should be noted that. In AppendixA. 1/'l I / We can seethat the Euclidean norm and the 2 norm. example. areidentical vectors.. norms be developed matrices. the time being.' l l .l. or spectralnorm. we can use it to define C o n d l A l: l l A l l ' l l l . .llX ll.. norm llXll-: max.\t t 'tt l \..1 u ' i-l That is.the important For point is that the llAll2.ril whichdefines normastheelement the with thelargest etbsolute vallle..max)-. l..: L ' 'l.For to vectors.aua^ f t n - l.1972).s) which is given a specialname-the Frobenius notm.2 Motrix ConditionNumber Now that we have introducedthe conceptof the norm. as in ll. and the largestof thesesummations taken as the norm.r.ERROR ANALYSIS AND SYSTEM CONDITION The conceptcan be extendedfurther to a matrix [A].1 r l-i-.thereale alternatives calledp norms that can be represented generallyby / . providesthe tightestlreAsureof size (Ortega.. for Otherimportant examples (p : 1) are rYr . a It slrouldbe notedthat therearealternatives the EuclideanandFrobeniusnorms. : . can for For llAll : .the 2 norm and the Frobeniusnorm for a matrix are not the same. it provides singlevalueto quantifythe "size" of [A].

+r I I I ll: : : : l1 l Lfl nrl Use the row-surnnorm to estimate matrix conditionnumberfor the 3 x 3 Hilbertmatrix. -e0 30 60 -l L I g -18 10l l Note that the elementsof this natrix are larger than the original matrix.1989)that l^xll .which is notoriously sentedgenerallyas [r l.ll^Ail = condlAl il"il Mil That is.e.35. 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. l l.roundingerrorsare the order of 10-r) and Cond[Al : 10'. the t-.+.INVERSE MATRIX AND CONDITION where Cond[A] is called the nntix conditiort number Note that for a matrix [A].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 ill-conditioned..which is comoutedas . the solution[X] may be valid to only r . this numberwill be greaterthan or equalto l.1667. bereprecan Problem Stotement. The Hilbert matrix..cdigits (roundingelrorsry l0' -' ).1978: Gerald and Wheatlev.l I 1l 'i I ltl tJ .35 The inverseof the scaledmatrix can be computedas lAl-l:l-tr' r b -bul ) | _ . I 5l so in row Solution. EXAMPLE 1. . Thus. 2. exof For 0n arnple..4ll-:l+.:2.the matrix canbe normalized thatthe maximumelement each is1: rAl: fr 1 | Ll il iJ 31 +i Summing each of the rows gives L833.1 t A' l : l l{t tl lr i i' L3 I I ll EXAM I\ rI I . t l - I I - . the third row hasthe Iargest surnand therow-sumnorm is ll. First. This is also reflected in its row-sum norrn.if the coefficientsof [A] are known to t-digit precision(i. and 2. - + 4 i nll . It can be shown (Ralstonand Rabinowitz.

2.p) the where x is the vector or matrix and p designates type of norm or condition number(1 . :2. I 4 tl sl (c) As in Example I 1. Hence.notethatif p is omitted._ cond{X.the last threesignificantdigits of the solutioncould exhibit rounding 451.L 3/4 3/51. Then.3. i n f ) 2.2 always overpredict the actual error.3504 > > c o n d (A . to inf .3: [' tAr:lr I + | lr L' t_-l l^ i2 r rl l . first computethe row-sum versions(p : inf ). the row-sum norm and conditionnumbercan be computedas > > n o r m (A .2 ERROR as Thus.35(192\: 451. they errors.AND SYSTEM CONDITION ANALYSIS I 1.log The extentofthe ill-conditioning is ill-conditioned.65.A]: 2. Notethatthe cond functionis equivalent >> norm(X. Use MAILAB to evaluateboth the norms and condition numbers in for the scaledHilbert matrix previously analyzed Example 11. p) ano . the condition numbercan be calculated Cond[. Note that such estimatesaln-rost are usefulin alertingyou to the possibilitythat roundoff elrors may be significant.P) set it Also. or ' f ro').3 Norms ond Condition Number in MATLAB MATLAB has built-in functionsto computeboth norms and conditionnumbers: >> norm (X.4 MotrixCondition with Evoluotion MATLAB EXAMPTE Problem Siotement. r n f ) . However.enterthe matrix: >> A l\ \/2 I/3. z . | 1. is automatically to 2' I 1.p) * norm(inv(X) . (b) Also compute (p (p the Frobenius :' fro' ) andthe spectral : 2)conditionnumbers.2 257 that the system The fact that the condition number is much greaterthan unity suggests c can be quantifiedby calculating .I 2/3 Ii2. Solution: (a) First.

and work areas. a truck-stop restaurantlocatedadjacent the ventilation system for Bubba's Gas to an eight-lane freeway. F I G U R EI I . I L2. indoor air pollution deals with air contamination Bockground. in enclosed spacessuch as homes. offices. As depicted in Fig.258 MATRIX INVERSE AND CONDITION These results con'espond to those that were calculated by hand in Example 11.. ' fro' ) 368. the restaurantserving areaconsistsof two rooms for smoken and kids and one elongatedroom. smoker grillloods The ond odd corbon monoxide moss thesystem negllgible fo but oirflow.3. 2 view of roomsin o restouront.3503 "'i "i. As the name implies.0866 >> cond (A) 366. Overheod orrowsrepresent whereos Mo-woyorrows ihe represent diffusive mixing.1 Il..Supposethat you are studying 'N Guzzle. rooms I and 2 gain carbon monoxide from air intakes that unfortunately arepositioned alongside the freeway. In addition.'j\'. respectively. Room I and section 3 have sourcesof carbon monoxide from smokers and a faulty grill. one-woy The volumetric oirflows. 0.= 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 . (b) The condition numbers based on the Frobenius and spectral norms are >> cond (A.

In addition.c:) * Ey(!+ .Qa)c'+ Q.(Outflow)f (Load) *(lnflow) (Mixing) Similar balances can be written for the other rooms: 0 : Qt. First.0062069 0. 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). (Q' . and (3) the intakevents.00055L12 0.432 6e-005 0. For example.i MATLAB can be used to generate the solution.00010728 0.ct.000r3193 n nnn 6aaG6 0.determinewhat percentof the calbon monoxidein the kids' sectionis (2) due to (l) the smokers. Write steady-state mass balancesfor each room and solve the resulting linear algebraic equationsfor the concentration carbonmonoxide in eachroom.0049655 n nnt9) . balancefor the smoking section(room l) is 0:lVrmore.c+) * Ezq(cz c+)' Qoc+ yields the final systemof equation: Substitutingthe parameters i.Qoct 0 : Qoc3* Ez+Gt.the Solution. the grill. In addition.0034483 0.0449962 0.j lT - 0. .* QncoQocr *f13(ca-c1) . For example. continued l . . Finally.cz'l E2a@a c2) + 0 : wern * Qnct I EnGt .CASESTUDY trli':'l). we can compute the inverse. compute imthe provementin the kids' sectionconcentration the carbonmonoxide load is decreased if by banning smoking and fixing the grill.00096552 .0034483 0.0049555 ^ nAtQ-)aC I. 0 >> A L=f nV (A.. . genof erate the matrix inverse and use it to analyze how the various sourcesaffect the kids' room.0034483 0. to Steady-state mass balances can be written for eachroom. }:'tl l:t* !i.. i .l . 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.r]{.6 0.cs) .

.j:continued The solution can then be generatedas >> b=11400 r00 2000 0l .snl: arytWsrin: 0..897 12..0:j ".93Vo The grill: : cz.tt Lw.irrrakesarl Q.moke.4483 1 : 7o..'. d ? 4/l'9...37931 0.0034483( 000) : 3...0034483 : 1.0034483(-2000) + _ 3 . L6.? 1L345 x l00vo : 2'7. These results take place because(a) carbon monoxide is conservative and (b) the only air exhaustsare out of sections2 and 4 (Q.I I l0 -3 6.891 Vo".c' + arrt Qt ct.i.897 16.877o Check useaI .smokers Wr*ok* : 0. 4 4 8 3_ 6 . .0034483(-1000) + 0. ok". and 0a). For example.t: The intakes: : (200)2 + 0. the elementsof the matrix inverse can be used to determinethe percentof the carbonmonoxide in the kids' sectiondue to eachsource: The smokers: : uitr c2.S.260 MATRIX INVERSE AND CONDITION '' i'' {. aur LW-*lr:0.345 x l00%o:55.: 0. The inverse can also be employed to determine the impact of proposedremediessuch as banning smoking and fixing the grill.0062069(50)2 c2.. 8 9 6 6 : _ 1 0 . $ { . Room 3 is sobad becausenot only does it get the load from the faulty grill. Becausethe model is linear. superpositionholds and the results can be determined individually and summed: Lc2: a.. . : '"snrr ' 12.0034483(2000) 6. Although the foregoing is interesting.62069: 2 * ) Vr '. >> c=Af *b c= 8.483 Thus.. 3 4 5 : il. the real power of linear systems comesfron using the elementsof the matrix inverse to understandhow the parts of the systeminteract. . but it also receives the effluent from room l. " .345 The faulty grill is clearly the most significant source..15. 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.0996 1) ?/q.

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. s e f o l to w i n g s y s t e m : l l . r 2 * 5 x .6 1 .345mg/m3. in ofthe smokerand grill loads.it was not necessary recomputethe solution.The result by would bring the kids'room concentrationto 12.265 mp/m3.if the mixing betweenthe kids' areaand to zone 4 is decreased. j : .-rj : 2'7 .rl PROBTEMS ii i li continued Note that the samecomputation would be made in MATLAB as >> AI \2.2 1 .xr:-38 3r1 .-t2*'7x.345 :2 mg/mt. Because the foregoingcalculations all involved changingthe fbrcing functions.the only sources the air inare takeswhich areat 2 mg/m3.However.10.rz. This makes because the absence sense. ll. 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 . 1 2 * 2 r : : . the resultis Cl c2 C4 l:{r*tr1 -8rr+ x2-2x3:-/Q 2x1-6x2. 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.rr 2.345 Implementingboth remedieswould reducethe concentration 10. 5 yourresults verifyingthat [A][A]-r : [1] Donot by aplv0tlng stratcgy. 5 r 1* . this remedy would only improve the kids' area concentration by a paltry 0.I)* (-1000)+AI (2..3\* (-2000) -10.345 .3 r 16 . : -34 Therefore.3 The following system of equationsis dcsignedto (the c's in g/m3) in a series of determine concentrations .

solve to for thc casewherc all thc unknownsshouldbe exactlyone. computethe condition L2.Fr.l o 0 t2.l3 (a) Dcternrine rratrix invcrseandcondition thc num.Mead. i n P r o b . .422 t2.ector consists ofthesum.1 -50.mationof the coefTicicnts its low.nbcrs. the for Mohave. u'hich has thc fbllou. its ofprccision expected bc lostdueto ill-conditioningl are to the Deterrr-rine solutionfor this systern thc case thc fbr where each {b) Usc the inverscto dctermine solution.6. -2000 lb and ahorizontalload t3.l Determinelhc rni"rtrix inversefbr thc systcnrdescribcd dinrensional : :5.. rl ['i r 'I ll l-rl .3.377 n.122 0 0 0 of F3.1. a n dl l A l l .i t : * l 2 c . It = 2. 8. c2. ll.owerColorado : rescrvoirs shownin Fie.2and I 1.ing tbrm: :tl_ i-l where th. scale thc rnatrix by making the nraxirnum clementin eachrow equalto one.5 if the inflow concentrations of of are ll.I 1.10 Usc MATLAB to determine the spectralcondition -4. andc'. 6 D c t e l m i n el A l l r . atedby solvingthis systcnr. t a t . . [ ' 5 6ll 14 Lt8eJ ( b ) R c p e a( a ) b u t c h a n c e .-r-+ 10.= fie ber for the fbllowin-c system. 11. 1 . l l R e p e a tP r o b . in where the right-hand-sidc vector consistsof the loadingsof l l.xs : 3. -500 lb is appliecl nodc -3. Mass balancescan be written fbr each reservoir.r'r.252 0 12..3 c 2cl :3800 conclition nur.262 MATRIX INVERSE AND CONDITION (a) Dctermine condifionnumberbased therow-sum the on n o r m f o r t h ec a s e h e r e . there arc olher matrices ll. 5 .4 .and Havasu. :1 2 6 9 1 3 c r+ l 8 c : 6r'.ll Use MATLAB to deternline spectfalconditionnurn.l4 Po unique (z Such poll f(x): z [t rAt: I -e I ls -l L l. {b} in lnust be increascd inc[rcca l0 g/m3rise in the con. & and F-r) if thc vertical load at resuts: : node I is doubledto F1.. Use the matrix inverscto dcterrnine fbrce in Iinearalgehraic equalions the I thc thrcc nrembers(.7 Determincthe Frobenius arrdrorv-sumnorms fitr the systems Probs. -I: : 7.ral and Frobenius l 5 c 1.rcspeclively? Vandcnrondcmatrix {sce Prob.l l A l l . n l Com. 12.797 P Belbre detcrmining thc norms. r := 7 .f o r 1 l couplcd reactors a function of thc antountof nrassinput tcr as e-ach reactor(the righrhand sidesin g/day): ll.l nurnbcr and how many suspcctdigits would begenuCompute the condition rrumberbasccl the row-sur. respectively. Onc such case is the ber fbr thc fbllowing rnatrix: Vantlcnnotttle rzalnr.12 The l.. w (b] Usc MATLAB to cornputethc spcct.pal'c thc resultins crrors with those cxpectcdbased the in on duced if thc rate of mass input lo reactorsI and 2 is conditionnumbcr. In otlrerwords. to t3. ll. 1 s 16 r. a n d .l: 2 3 5 0 nutnbertbr a I0-dinrcnsiontrl Hilbclt nratrix.-l (a) Use the matrix invelse to solve tbr the concentrationsin [t e 16 2s 361 eachof thc fbur lakes. that are inherentlyill-conditioned. Pl l.5 36 4e 61 8r .chloride to eacho1-thc thc fbr"rr lakesalld {rl.\inverse. s l i s h t l v o 9 .n on norrn. l 0 . x z : 2 . b u t f b r t h e c a s e o f a s i x . 8 .. c e n l r u t i ou l r c a c t o r .9) wnere ll. l l . :[ll. l l .3'71 0 12. (a) Dctcrnrinc lnatri. reducedby 500 and 250 g/day. 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.252 0 0 l l .r' t .How many dig. (d) How much will thc concentraticrn reactor 3 be rc. Do not nornralize systcm: rcsulting chloriclecoltcentratious Lakcs Powell. 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 column-sumnorm.5 Dctermine matrix invcrselbr the system the and dc'scribed the lbllowing setof sirnultancous in Prob. and"16 t Rivcr consists a series tbur concentrationin reactor . r l. as changedto c1l : 20 and ci. ll. thc (c) Detcrminchorv much the ratc of rnassinpLttto reactor3 elcnient ofthe righrhand-side r.9 Bcsidesthe Hilbert matrix. way for r algebraic the coeff l'1.

(Pl1.I2 P TheLower River Colorodo coefficientsof the fourth-orderpolynomialf(r) : p6" * pzx3t pzx2+ p$ + psthat passes throughthe following five (300. .A straightforward unknowns(thep's). Suppose Polynomial interpolationconsistsof determiningthe (n . Use this approach solve for the coeffor computingthe coefficients is to generate n linear ficients.1)th-orderpolynomial that fits n data points.PROBLEMS Upper Colorado River Lake Powell Lake Havasu FIGURE I I.746). polynomials havethe generalform.675).l4) to yield a systemof five equations with five to thep's are constant coefficients.14) l p2x"-2+ .616).0. points:(200.(250. + pn rx * pn Eq. equations that we can solve simultaneouslyfor that we want to determine the coefficients. .525). determineand interpret the condition number.0.0. into and (500.0. (400.457). In addition.Each of thesepairs can be substituted (Pl 1. 0.

l IINEAR SYSTEMS: GAUSS-SEIDEL The Gauss-Seidel methodis the most commonly used iterativemethod for solving lineu algebraicequations. thedragonal we to If elementsare all nonzero. Understanding how to solve systems nonlinearequations of with successive substitutionand Newton-Raphson.'.andthe for . Because present part of the book dealswith a similar problem-obtaining the the valuesthat simultaneously satistya setol'equations-we might suspect that suchapproximate methodscould be useful in this context. l2. In this chapter.5 and 6.'" t. EXAM terative or approximatemethods provide an alternativeto the eliminationmethodi I describedto this point. Those approaches consistedof guessinga value and then using a systematic method to obtain a refined estimate the 0f root. the second x2. Assumethat we are given a setof il equations: lAl{"r} : {b} Suppose that for conciseness limit ourselves a 3 x 3 setof equations. Such approaches similar to the techniques developed are we to I I obtain the roots of a single equationin Chaps.we will presentapproaches for solving both linear and nonlinearsimultaneous equations. the and Knowing how to assess diagonaldominanceand knowing what it means.the first equationcan be solvedfor. Recognizinghow relaxationcan be usedto improve the convergence iterative of methods.r1. Iterotive Methods CHAPTER OBJECTIVES The primary objectiveof this chapteris to acquaintyou with iterativernethods for solving simuitaneous equations.. Specificobjectivesand topicscoveredare ' ' ' ' Understanding differencebetweenthe Gauss-Seidel Jacobimethods.

t.-! (t2..llr) bt-ayrl-a:txl 1133 (12.85+0. Then we substitute this new valueof r1 alongwith the previous guess zerofor-rr into Eq.r2f0.3x -l 0. (12.r2 x3 arezero. (12.r(0)+0. l ) .4.I are the presentand previousiterations.initial guesses must be made for the x's.1 LINEAR SYSTEMS: GAUSS-SEIDEL third for x.Convergence be checkedusing the criterion that for can .3 0.5 i ).3. First.1.r.r to yield 265 xi .r 1 1 1.85+0.3 0 .616661 . Use the Gauss-Seidel methodto obtainthe solutionfor 3xr-0.Eq. ! '10 1 : 3 .1r2-0.1) be used compute can to ^t - 7.lrr t 0.3) By assuming that.r2-0.1b)to compute'a of new is value for x2.. The process repeated Eq.1.19.l l2 Gouss-Seidel ethod M ProblemStqtemeni.1.2x3: 1. 2 t 2 + l 0 r 3 71.1a) atl . (Er2.3xj: -19.12. To start the solution process. Then for for we return to the first equationand repeatthe entire procedureuntil our solutionconverges closely enoughto the true values.solveeachof the equations its unknownon the diagonal: for 7. a22 (12. 3 1 10 .1a).2) (E12.2x2 r ( Er 2 r . and (E12.1c)to calculatea new estimate x3.0.lli' Jri -r/'I e.2(0) :2. < x 100% r. which can be used to calculatea new value for -r1 : bt latr.85 0. (12.': l--l lr.l :XAMPLE .Thesezeroscan be substituted into Eq.f1 .4 Note that the solutionis ixlr : |3 -2. rll rr Solution.lxr* 7. Dr-4ttX.1yl . A simple approach is to assumethat they are all zero. t:l -clrr-{l .1.rc) whereT and j . i tl D) -AtqXi -d)t.

1 9 . (12'2) provides means estitnate for x1: example..194524) 0. Rather than using the latest available thistch utilizes a somewhatdifferent u a setof new . if the solutionis converging'th in the next equationto cleter-n-rine Jttcobiiteratiwr' called An will be enrployed. 9 9 0 s 5 7 0 .0. therefore.266 METHODS ITERATIVE into valueof -rr : 0. bestavailableestimates r's. 1 Q . 1 ( 2 .5(/' | 2. we would not applied to improve the answers.1 1qL\)L by The first iteration is completecl substitutingthe calculatedvaluesfor 'rt and'r: into to E q .the sameprocess repeated compLlte ^l - I 0) + 7. .2(-2. 3 ) y i e l d a 1 - 1 1. .convergingon the true solution. 3 ( 0 ) +0 - .I) to compute forthenefl are not immediatelyusedbut ratherareretaiued they new valuesaregenerated. Although therearecefiain usuallymakesit the methodof preference' utilization of the bestavailableestimates . true answerapriori.990s57 0.499625) 1 r.0 .qe0ss7 | N F o r x 2 a n c l ' r r .alongwith the assumed to calculate .r's on the basisof a setof old x's.1 4 q q 6 r 5 + ) . in an actualproblem.1 9 . altemativeapproach.Thus. 1 .616661 0. can be substituted Eq'(El2'l This value.3(2.Additional iterations th know However. when they aremet.005610 )+ : l0 to is For the seconditeration.2'616661| . theenorFr to a Eq.0. appraisal ally provide a conservative that the result is kttown to at leastthe tolerancespecifiedby e'' ud method.00" : |2'990551 : 12.4. 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.2(. '. Thus. 12. iteration.I .4 .theyensun ofconvergence. niqueusesEq. t h e e r r o r e s t i m a t o s i l t ' € € .85+ 0. 3 ( 7 . 0 0 s 6 1 0 . 3.it is immediately for As eachnew r value is computecl the Gauss-Seiclel another-r value.1'0056 :2.r(-2. 3 0 .Consequently.000291 l0 couldh The method is.3 )+ :1. ( 12.) 7 (2.( E 1 2 .191524) 7.990551 3 . methocland Jacobi iterationis depictedin betweenthe Gauss-Seidel The clifTerence wheretheJacobirnethodis useful.Gauss-Seidel! cases Fig. tactic.Thus. 6 1 6 6 6 1 ). .2(-2.

14)/azz . simple fixed-point iteration was sometimes gressed.t . That is.its lt nonlinearequations. . 2 .\fu' \: (bz- a 3 1 x .atzr2 * arrx)/atr x.tt.a2trt . t- ap:t1)/a1z ..ir 1 xr: (b2 .x.(b. 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. can be shownthat if the following conditionholds. 3 ) i+i That is.d: rrr a v . convergence guaranteed the condition is satisfied.rx.S E I D E L 267 First iteration . (12.r tt. x.)faa. r .ar. . answermoved fartherand fartherfrom the correctresult.Recall that iterationthat That is.r.because is designedfor linear Although the Gauss-Seiclel ability to convergeis much more predictablethan for fixed-point iteration of systems. a represents feasibleapproachto solve many problemsin engineeringand Gauss-Seidel science. .Gauss-Seidel will converge: II \i1t ul i-l lo.ttyx2)/a1 Second iteration i r : ( h r . : (bq - a31r. I to solvelbr the roots of a singleequation.r2)/ajz -rr .G S I 2 .l:> ) l0.Therefore.azrxt .(bt . if is met.c l .many englneering and scientific problems of practical imporlance fuhlll this requirement.Such but not necessary This cliterionis sufficient are systenis said tobe tliugonalb dotninant. . : I b2 .ct.. as the iterationsprononconvergent.l Convergence ond Diogonol Dominonce of methodis similar in spirit to the technique simplefixed-point Note that the Gauss-Seidel wls usedin Section6.a.Fortunately.l I (r 2 .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. 1 L I N E A R Y S T E M S :A U S S . although the method may sometilneswork if Eq. I iferotive (o) ond between theGoussSeidel (b)theJocobi of C1opn.l.ttr)f ar. . (b) @l I F I G U R E2 .3) is not for convergence. \ 1 \/ a . the it methoclcan also diverge. 1 r 1 ) / .n .r.u27x.

ration..268 METHODS ITERATIVE 12. is intended toward the estimateby pushing it closer to the truth. is The approach also calledsuccessive vergentsystem.l . | 2.Thus. o-v ) _ .^.rild (12..a)is listedin Fig.* : ' U.r. is a weighting f'actorthat is assigned value between0 and2.0 all ll2-l - 4f | -. to the true solution but at too slow a rate.1). out by conversence dampening oscillations.].t osz L f lctr onlottf ay/a2 l 0 -l Eq.r' . ntl a. the addedweight of /.!!*uc* rl33 in concisely matrixform as can Noticethatthesolution be expressed { : r }: { d } .t AT . is typicallyemployed make sr"rlts. ( 12.2.( 1 2 .3 Reloxolion to methodthat is designed a Relaxationrepresents slight modificationof the Gauss-Seidel thatvalue is usingEq.. the result is a weightedaverage the presentand tl. . enhance iterations: average the resultsof the previousand the present of modifiedby a wei-ahted .).. However. let us first recastGauss-Seidel a form thiit is comThis is done by expresstttg pelfbrm matrix operations. . 1 a s bt Cltl b2 _ a12 Atl .l c l { : r } where : tfl:'. from I to 2..this type of rnodification.n.l ^r^' xi.if I is setata If )... An M-file to irnplernent (12. In this inthat the new value is moving in the correctdirection thereis an implicit assumption stance.re to It is Tlris type of modification calledwtderrela.1} and anlal 0 [ :laztlttzz 0 tcl a.-nl. (1 .* . designed accelerate convelgence an alreadyconor ot'errelaxcfiion. systemconvergeor to hasten a ltonconvergent For valuesof .1.1.. pltible with MATLAB's ability to ) E q . After eachnew valueof r is compr"rted convergence. previous reof value between0 and I .t a.. Hence.l'*: lr.i.2 MATTAB M-file: Gaussseidel in Before developing an algorithm. SOR.5) a where ). 12.i) is equalto 0 and the resultis unmodified.lt" + (l ). extra weight is placed on the presentvalue.':) {. : 1. improve of to the is which is calledot'errelaratiort.

maxtt) ? GaussSeidel: causs Seidel nethod eo x = Gaus:lSei.nl . g u m e n -s y o q u i r e d . .:)*x. the efficiency introdr-rced a wise by choiceof )..l r p . .- = d(i)-C(i. end for i = 1:n = 0. 2 Mfile MATLAB fo implemenl Gouss-Seidel The choice of a proper value for ). es=0. while xold F^I (1) _ x.al.del (A. Fnr i l.LINEAR SYSTEMS: GAUSS-SEIDEL functiol-r x = GaussSeidel (A.xold(i))/x(i)) * 1-00.2.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. d if nargin<4 | isernpty (maxit ) .ze(A) . if the system under study is to be solved repeatedly. maxit=50.i) = 0i x(i) end x=x'.i). rter if max(ea)<-gS iter end .1:n) end for i = 1:n = b(i)/A(i.n)iA(i. end if nargin<3 | isempty(es). b) : causs SeicleI vrithout % lnput: % A=coefficientnatrix Z b .0 if x(i) . if m-=n. 1. is highly problenl-specific anclis often detern. es. r .n = C(1.err. it However. c(i.u '. can be extremelyimportant. l t d i . d(i) end i f ar n.Good examples the very largesystems linear are of in algebraicequations that can occur when solvillg partialdifferentialequations a variety of engineering and scientificproblemcontexts.si.erLd In. empirically. 011001. error('Matrix A nust be scluare'). end I F I G U R E2 .b. >= maxit.s((x(l) ea(i) encl end = rter+1.i) C(i.rlghL hand side vector ( d e f a u l l . F t . x(i) -.1. break.rined For a singlesolutionof a setof equations is oftenunnecessary.

such systems of equations be expressed generally can as .1 Successive Substitution A simple approach solving Eq. r t . illustrated tlre lbllowingexample.As in Fig.{ .r 2 .2 NONTINEAR SYSTEMS The following is a setof two sinrultalleous nonlinear equations with two unknowns: EXAM "f+"'"2:lg . . : . ) : 0 Therefbre. .) O J . .rsive substitution.3. = 5 / . t 1+ J J t .270 ITERATIVE METHODS 12..r:. r . (12.to) (r26r) In contrastto linear systemswhich plot as straightlines (recallFig. \ .That is.)/ (12. eachone of the nonlinear equa.r.the solutionis the inte$ection0f . ) : 0 . Thil irpproach. Justas we did when we determined rootsfor singlenonlinear equations.7) is to usethe samestrategythat was employed fbr for fixed-point iterationand the Gauss-Seidel method.n irvo simultoneous of nor. l i ( .Theseequatiollscan then be implemenled iteratively to computenew valueswhich (hopefully) will convergeon the solutions. (12.. r 2 .the solutionare the valuesof the r's that make the equations equalto zero..7) 12. ( r t .l). 9.r1 the curves. . . 12.3 Grcphicol depictionof the soluiic-. .2.. . r l + J . . is in FIGURE I2.. which is calledsrcce. r ./:(xr. these equations plot as curvesoD an ..r2 VeTSUS graph. tions can be solved for one of the unknowns.lineor equoiions . t 1-.

2)to deter_ mine a new valueof 't2: .6a) is .21429)(3. (E12.J. 5 /--:2.2.t' : . can be usedto determine new value a 1 0.2) ofxl: On the basisof the initial guesses.2 NONLINEAR YSTEMS S E X A M P L E. z 3 .709 rz : 57 .yi ^ l (E12."rri xz ( Er 2 . t r l: - l0 .5)2 -24. :I.rl _ 2 ancl : 3 Initiate ./iblT'r2 and of Eq. : yTo . (12.3( -0.86051 . (F.37 5 tO2 : ObvioLrsly. Equation(12.yz: 5'7. Use successive substitution determine roots of Eq. : Solution.12. . For example. (12. r 1: 1 .3.6b)can be solvedfor 12 : )/ .20910 -24. (12.This behavioris even more pronouncedon the seconditeration: I0 .20910)(-24.5t35 :2.37 16 : 5 429.rr the computafionwith guesses o f . ( I 2. to the Note that a correctpair of roots is.915 y 3(2.21429 3. 5a n d . the Now we will repeatthe conlputationbut with the original equations up in a diff'erset ent fbrmat.) and Eq.I2.1. f .3(2.6b)is Now the resultsare more satistactory: r.6a)can be solvedfbr . 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.rl.21429t2 Lt - -0.5 can be substituted into Eq.1) Eq. 5 .5 This resultand the initial valueof x2 .12.2.an alternativesolutionof Eq.1:.s ) 2 :2. approachis deteriorating.( 1 .3i 516 : Thus' the approachseemsto be diverging.17e4s) 5 7.6).3 .

a first-order of Taylor seriescan be written for eachnonlinearequationas f t .r axis. \ f ' \ x i ) (128) "f wherex. ".2.r1 and to where/y .i+r -\'r.272 ITERATIVE METHODS tr:@:1. i + r * r .ttu . other of open root location methodssuchas the Newton-Raphson methodcan be usedfor the sanepurpose.r" .86051 : 3.For this situation.1.diveris gencecan occur if the initial guesses are insufficiently close to the true solution. .. ) ?xz (xz. f 2 ..' r 0fuI (x2.11and . i + t: . ( 12. (.. 12.x .ltal (t2.| ( .1H ' .l0) canberearranged give . .At this intercept.However.%0s) Thus. The previous example illustrates the most serious shortcoming of successive often depends the mannerin which the equations substitution-that is.1.An alternativeis to derive it fiom a first-orderTavlor seriesexpansion: ( x r * r ) : f ( x i ) * ( x i + r.the root estimate corresponds the valuesof . l Y f Exz ( 12.2 Newton-Rophson Justas fixed-pointiterationcan be usedto solve systems nonlinearequations. iv t : .6. even in those instanceswhere convergence possible.1 0i) Justas for the single-equation version.e.8)canbe reananged I to yield l.These criteria are so restrictivethat fixed-point iterationhas limited utility for solving nonlinear systems. t 1 . the approachis convergingon the true valuesof "r1: 2 andxz : 3.111eQual zero. the slope) of a function to estimateits intercept with the axis of the independent variable-that is.l}a) f z .[. The multiequationfonn is derived in an identical fashion.94053 51 . we useda graphicalderivationto computethisestimate.Additionally. (12. (12. convergence on are formulated. is the initial guessat the root andx.d2.) "-" dlt. For the two-variablecase. * .11 is the point at which the slopeintercepts the .2. i . Eq. a multivariable variable Taylor seriesmust be usedto accountfor the fact that more than one independent contributesto the determination the root.. In Chap.-1 : ri - EXAA f (x') ^l \xt ) tl29t form of the Newton-Raphson which is the single-equation method.Recall that the Newton-Raphson methodwas predicated employingthe derivative on (i. * ( x r .ri*r) by definitionequals zeroandEq.. . to (12. the root.04955 3(1. f i .

s) 1.i \fz.i+t : -tl. 5-) 2 l : 1 .125 -^-?<_ 2. : . 3 LE N e w t o n . method.. T h u s .Xt q 3fi.g.i 3fz._ Jt. (12.Initiate computation of them at the initial guesses Solution. f z .S)2 1. a1 -d x 2 .i 0fzl * 3fi.5(3.5) : 36.i - \fi.o (l._ Jl.2 . OX2 t^.s) r.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 Newton-Raphson Problem Stotement. .t25 Thus. .i dx2 a .{t.12b) The denominator of eachof theseequationsis formally referred to as the determinantof the Jacobian of the system.5(32. 5 : + . until an acceptable can be repeated . of the rootsof Eq. First computethe partial derivativesand evaluate -r and y: # : 2xr xz:2(1.r r.JZ.125 as at ofthe functions be evaluated theinitialguesses can Thevalues .5 andxz:3.i5 3xr is for Thus.s) : 156.5 3 : + W # :rr : 1. 5+ 3 (1 . The computation accuracyis obtained. E q 1 2 .s(32.12) is the two-equationversion of the Newton-Raphson the following example.03603 2.algebraic manipulations (e.12) give can These -2. (12.) 1 0: .5 : | * 6xtxz:I + 6( r . i p r o x i m a t i o n ) .75) 156.s) 156.J)..7 r.5) ( 3..6).As in Equation (12. t3 . Cramer'srule) can be employedto solvefor t.12a1 x2.5 5 .r " \J\. ]fz.5) ofz'o -3xi 2 : 3(3..fr. the resultsare convergingto the true valuesofxl : 2 and12 : 3.i _ \fi.i - dXt n t.i \fzl 0x10x2 3x2'dx1 3f .i \xt \xz 3x2 \xy (12.5t* .t _ (12.i " " t^. 6 2 5 . t h e o n l y u n k n o w n s o r e " r l . 5 ) ( 3 .62s (r.5(36.62s(6.s) s) (36. 1 1 ) s a s e t o f t w o -n1 linear equations with two unknowns.t n " 3f2. into to values be substituted Eq.5.84388 .(-2.thedeterminant theJacobian thefirst iteration of : 6. Consequently. i a 1 . Use the multiple-equation with guesses x1 : 1.5 : 32..it can be employediterativelyto home in on the rootsof two simultaneous equations.5 6. E X A M P1 2 .i+t : x2.Ir NONLINEAR SYSTEMS I I to with i 's areknown (they correspond the latestguessor apBecause valuessubscripted all (.

rconsisting at of the partial derivatives: 3ft.t.x2.i x n .--r'l.i l.it exhibitsthe samespeedy quadratic as version. Noticethat the only unknowns Eq.r+r ..\' dxt frf I t . .i ofi. t4) wherethe partial derivativesevaluated i are written as theJacobianmatri..fz.e.i .i+rtermson theleft-hand in side..Consequently.fi..2.13) arethe.l'r.)i Finally.i t "' t Jn.+t l l . the function valuesat I can be expressed as {f }r : 1. . often the initial guesses must be obtainedon the basisof trial and error and knowledgeof the physicalsystem being modeled. with iteration..*r)r:L. (12.i dxil rt : . ( I 2.i d"h UI: 3f2.just as with successive convergence the single-equation subsfitution.i iJxr 3f.i 0r.Although there are for someadvanced approaches obtainingacceptable first estimates.i 3xn The initial and final valuesare expressed vectorforrr as in {x..t-.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)... oIz. r .i }xz : 0f.t l l + l J l { x i } '(r2. r 1. as l J l [ r i _ r ]: .t/J2 itJ r... are knownatany generallyrepresented Eq.tl.Whereas graphicalmethodscould be employedto derive good guesses the single-equation for case.f n. OX? Dfi. xz. in Matrix notationcan be employed express (12.J (" 1 f . no such simple procedureis availablefbr the multiequationversion.13)concisely to Eq. ofzl 8-r. The two-equationNewton-Raphson approachcan be generalized solve n simultato To neousequations.--lrr.}r: and {. All other quantitiesare locatedat the presentvalue (i ) and. ()X1 0Jt.i+r )ra.l3) (i.. .. ( 12. thus. Eq. .rr.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.I I ) can be written for the ftth equationas . d.274 ITERATIVE METHODS When the multiequation Newton-Raphson works.t .f <JIt itf r. it can diverge if the initial guesses are not sufficiently close to the true roots.i+t dx2 +' ''f ih.i ) ..1 Dxz :: 0..i f..*.t-::. .i 'l':.However. setof equations the by k:1. do this.rr.

x(2) +3*x(1) *x \2)'2-51 I Then.7500 >> f=tx(1) f2.l4) with nratrixinvelsion. The secondshortcomingof the multiequationNewton-Raphson method is that excellent initial guesses usuallyrequired ensure are to convergence. alternafiveapproaches that are slower I i . As in Figure 12.6250 1.n 12. Jacobian analogous thederilativeofa rnultir"ariate In the to tunction.1] '{.16) and (12.17) to yield the improved estimates >> x=x . As might be expected.After this by using MATLAB to duplicate calculations the Exalr. variations the NewtonEq.in.l7) in an iterative Eq.3. resultis of the {. to Theretbre.l\f X. we can implementEq.5u00 1. (12.5000 32.this routineis passed M-file that the an computesthe fbnction valuesand the Jacobian a given value of "r./('ri ) 'f \x1l (12.It then calls this funcat fashion.0360 2.nations the partialderivatives fbr basedon using finite-difference that compriseIJ]. (12.17) clearly illustratesthe parallelsbetweenthe two is equations.I2.ri*r}: {t.15)is sometimes inconvenient evaluate.lr 12.3"x(2)^2 I+6*x(1)*x(2) I 6 . We can ilhrstrate fror.4.5]. >> T- J=[2*x(1)+x(2) x(1) . Because these are -somefimes difTicult or inconvenientfo obta.}. (12. Insightinto the solutioncan be obtained solvingEq.16) If Eq.3. by versionof the Newton-Raphson methodis Recall that the single-equation ^/+l ^/ .[. until an upper (maxit) or a specified percent relativeenor (es) is reached.First. essence.11) of Clomparison Eqs. of Raphson approachhave been developedto circunrventtlris dilemma.rple we defining the initial guesses! can computethe Jacobianand the function valuesas >> x=[1.s000 ^2+x(1) *x(2) -10 . limit of iterations We should note that there are two shortcomingsto the foregoing approach. The routineiterates tion and implernents (12. Such matrix calculationscan be implernentedvery efficiently in MATLAB. ( 12.- 2.5.14)is solvedby multiplyingit by the inverse the Jacobian.a niceralternative is to express algorithmas an M-file. (12.most alre approxir. 5 t 10 0 36.3. This processcan be repeated in iteratively to obtaiu refined estirnates a f'ashionsimilar to the two-equation casein Example12.2 NONLINEAR YSTEMS S 275 Equation(12.'-]/139 Althoughwe couldcontinue iterations the command the in mode.14)cirn be solvedusing a technique such as Gausselimination.

.maxit.-l l . dx=J\ f .end icer = 0. f] =func (x. . / x ) ) .maxit=50. values of of The of r that nlinimize this ftrnctionalso reprcserlt solutionof the nonlinearsystem. can ..7). f .p2.es. i r . fore.. e s . ). i t e r l = n s . = additional parameters used by function ? ?r output: ? x = \/ector of roots Z f = vector of frrnctions evaluated ar roors 2 ea = approxinate percent. ) 1 2 whereI (x1.initial guess % es = desired percent relative (default = 0. . iter = irer . x=x-dx. e a = 1 0 0 * m a x( a b s ( d x .o r r l x .. ' . % uses the Newton-Raphson method to find the roots of % a system of nonlinear equations ? input: % func = name of function that returns f and J Z x0 .276 ITERATIVE METHODS f u n c L i .. x=x0.{2.iter]=nev/tmult(func.p1. .relative (Z) error = number of iterations % iter rf nargin<2.it lea<=es.0001.erra]. . the Thfle. ' 5 m u 1( f L l n c . break. nonlinearoptimizationtechniques be employedto obtain solutions.ea.l. f i G t .. ('at least 2 input arguments required'). while (1) IJ. x z ..es=0. ..end if nargin<4 |isempty(maxit) ... if iter>=nrax. z a r a r g i n ) t % newtmult: Newton-Raphson root zeroes nonlinear systems ? [x.l a singlefunction: the as _L F(x) _\.p2. end end FIGURE I2.4 MATIAB Mljleto implement Newton Rophson method noniineor for syslemsequoiions of than Newton-Raphson which have betterconvergence but behaviorhave beendeveloped One approachis to refbrrrruiate nonlinearsysteu... m a x i t .0001?) error a maxit = maximum allowable (default = 50) iterations p1. x 0 . .r.) is the ith rnember the originalsystem Eq.. e a .x0. varargin{ : } ) .end if nargin<3 |iserLpty(es). (12. f.

tt : 4_x K.7 x 10-2 (12.0 2x1. can in and_r.the concenrrations of eachconstituent be represented termsof -r. If x. (12.x 2 ): 5*xr+xz (50-2xr-x)2(20-x) (50-2xt-x)(10-xz) -4x10-a .c.19).theycanbe characterized by Kl :3cicu Kz.x) f 2 @ 1 .25) where the subscript0 designates initial concentration the ofeach constituent.r8) (12. (12.26) (12. Thesevalues can be substiruted inro Eqs. and cr.0+xr*xz) * (c".o*-tt*.\-Xz 12 (r2. = 3:7. Usingrhe stoichiometry Eqs.22) (12.21) . Sofution.20) (12..x lo-2.3cac.relationships a pair as of two simultaneous nonlinearequations.as ca:cu.3. c6.I2.s 20.lg) of and(12..Thus. formulatethe equilibrium.21)ro sive Kr: (c.o: Newton-Raphson methodto solve theie equations. and x" are the number of molesof C that are produceddue to the first and second reactions.theseare two nonlinear equationswith two unknowns.0*xL+x2) (co.o.0 .3 CASE STUDY 277 CHEMICAL REACTIONS Bockground' Nonlinearsystems equations of occur frequentlyin the characterization of chemicalreactions.t (12.x2)@a.\-xI cc=cc.20) and. For example.the following chemicalreactions takeplacein a closed sysrem: 2 A+ B ---> <-_ C (12.6: 10.2x1 .2t\ where the nomenclatureci representsthe concentration of constituent i. employ the ll !o-4.o-2x1 cb:cb.o: 5. respectively.23) (12.rz Cd=Cd.ox:) * Given the parametervalues.24) (r2.x)2{c6. the solution to this problem involves determining the roots of f1@1.19) A+D*c At equilibrium. : 50.(t2.. .xr) (c".co.

1 ITERATIVE METHODS . f2='1(x(1. ..) .4)can thenbe employedto determine rootsgiven the inis t i a l g u e s s eo f x .x(2) ) c l f 2 d x 1 = ( .ft(xt.r2) .we must determinethe Jacobianby taking thepartial derivativesof Eqs.)). xz) 3. xz) 3xr x2) fz(xr + E.xz * 6x2) J\$r. I t-o I L (b) Rep 12.r(rlel*x(1.71 The function nevrtmul t (Fig.( r r( x ( I ) + c l e l * x ( I ) .6. ) .fi(xt + 6x1. y ) f = (i. a : . x ( 2 ) ) . x (2) )-rr (x (1). x ( 2 ) ) ( ' d f 2 c 1 : : 2 =r . evaluating the derivativesis time consuming. : x z : 3 : --> forii-.278 iiir". x ( 2 ) ) J=fclf1dx1 df Ldx2..I2B5e-01.') I t2.000001. . 3fz o.fi(xr.. the partial derivativescomprisingthe Jacobian can be evaluated as 3fr dxt x2) .f ?t f unct ion f =u (x.a./) i (1Ll -.7 8.rrgin) del=0. shorL e 12. + x + . ) / (ciel*x(2))..fz\r.a x x -'!) ^ '2 i 120 | 0-r1 -3xt Xl (50 * - * ':<. a-f f 1. c l f l d x l .uz fz(xt.rct{x.-f 2l+000 f= -'l .ai. x ( 2 ) + d e l * x ( 2 ) ) . ) / (del.5 Ust and (b) v tem to a equation a.illr ir. alternative to represent An is them by finite diff'erences inr fashionsinilar to the approach usedlbr the modified secantmethodin Sec. (12.4 Tht termine( reactors reactor(1 3..' (de1*x t 1) ) . xz) 3x.223 7e 010 i Ler = 4 .x(l)). .x{lt)).Forexanple. a . ( x( 1 ) + d e l * x ( l .26) and (12.0.. * fi(xr.-01'1 Solve tl E s: 5 V o 12..2 Us< system u x) . 12.t (^) ) . xz + d.27).3.rz) 6xr ofr -: 3xz 3fz oxz .rr These relationships can then be expressedas an M-file to compute both the function valuesand the Jacobianas f unction lJ.3 Re1 12.0004.*x(2)).. system ul T O. var.i: (50 - .x(2) +del"x(2) ) -u(x(1 ) .3. u ( x ( 1 ) ..).. .lr =jf r-c.r66c+000 2. x (2 )) dfldx2= (r (x(1) . !') (5 + x + y) f . 1 f 1=u(x(1).r1./ funct ion f-v (x.lx?. ( x ( L ) .Although this is certainly possible.v ( x ( f ) .5?7-r€.df2cixi df2r.'' continued In order to use Newton-Raphson.

: 5olc: identify 12. (12.2) to solve the following sysof reanangethe t0 a tolerance s.r*_y*6:: B -3.6496 : cn:20 . .r*4y*5::6 6.. solution x. i14i.* 3y*:: l3 . r .6cr : 1200 c2't l2ca: 2350 Ithis problem with the Gauss-Seidel method to Usethe Gauss-Seidel method (a) without relaxation (b)withrelaxation(.57o: Set One Set lwo Set Three 9.3c:'r * l8r2 .y ::5 -2:+2t--3::-3 ?r -'-. of &Dd (12.2.3.rr -tt*5x-r:-21.2 z : 4 I Repeat Prob.T] I {i: Jrr+2./) )*5x-r':-r2 Use the Newton-Raphson methodand ernployinitial guesses : of .6634 cc : 5 + 3. 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 Itheright-hand sidesin g/day): r'::3800 t.2.jrj: 27 3 r t.3 r 1.0138 + :7.: : 6 A .2(3. . : 57o.7 Determine the solution of the simultaneous equatlons l':--l-+x+u.3366) 2. nonlinear 12. 5 .6 x 2 1 } s l : . to achievec'onvergence 2 " t.)": 1. Show using any number of iterations that is necessarythat your solution does not converge.3 4 -Rr." r 3 : .t r"-)r. Clearly stateyour convergence criteria (how you know ir is n()1 converglng).5 * 4l 25 l0-s (a) Repeat but use overrelaxation with I : 1. the set(s)that you could not solve using an iterativemethod such as Gauss-Seidel. : 2. 12.r:.3228 : lO .3366 x.2 Usethe Gauss-Seidel method to solve the followine untilthe percentrelative error falls below e" .r : _t' 1.3 8 1 . . : .2but useJacobiiteration.6772 ca PROBTEMS methodto solvethe followine I (a)UsetheGauss-Seidel untilthepercentrelativeerror falls below s.6112: 11.25) compute equilibrium into through to the concanthenbe substituted Eq.6772 40. t : l ' 7 t .r: .3366 16. r 2 y.6772 obtained.3. If necessary.22) of centrations thefourconstituents: : ca : 50 .r+B::2 r*5.8 Determinethe solution of the simultaneous equatlons: ^ )-) -J-! )il:.--)O nonlinear 12.r2 (a) Graphically. (b) Successive substitution using initial guesses r : of (c) puesses : r.3366 2.PROBLEMS continued a is These values After four iterations.6 Of the following threesetsof linearequations.6 .6 1 .l 2 r + 5 r .

I 0). andD : the gas-liquidexchange rate. of 25"C 25"C Tn r2) 100'c where Q" and Q. Q t: c c o : 1 0 0c m : 7 0 . D : 0. Solvefor the concentrationsgiventhefollowingvalues: l.ITERATIVE METHODS C Tcct- > ccz- 2Cc:- I cc.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. . by centered finite differences can be substituted for the second FIGURE I2.IO P . P I 2.10..t - F cc.8.Qcccr+ D(cuand for the liquid as Qrctz .a in massbalancefor the first reactor can be written for the sas as Qccco. 12. The transferof a chemicalfrom the gasinto the liquid occurs at a ratethat is proportionalto the differencebetweenthe gas and liquid concentrations each reactor. Pl2. are the gas and liquid flow rates.10 The steady-statedistribution of temperatureon a heated plate can be modeled by the Laplace equation: T..C /5 L If the plate is represented a seriesof nodes(Fig.Similar balances can be written for the other reactors. respectively.9 Figure P12. Tl 100"c o:*** dx' dy- 75. Q6 :2.Qrcu I D(ccr .which result in a system of linear algebraic equations.9 12.s - l l -ctzl o. L -cLr< I I -Cn< +' + + -cu< -cl-s< Qt l I l cL6 FIGURE P12.At steadystate.crr) : 0 ccr):0 derivatives.Use the Causs-Seidelmethod to solve for the temperatures the nodesin Fig.

. One *uy to ooil."r.. ."gfi.rh .cr.rgrriiion (Fig.. such data usually orii......"j. Rather.you will .Both of theseapplicationsare known. we make no efiort to ln^tersect every point. Tri. exhibitssignificant a degree error "scatter. fc1nd.. is of curves rhat passdirectly through each of the points.inatesfrom tables. Curve Fitting braic r the 4. . .. ..!! l\l ' the_ basicapproach to fit..t u. tJi.. Dara often is given discrere for varues arong mayrequire esrimalesat pointsbetween discrete the values. " "11111111.rr". i".o.: or from steam fables for thermodynamics. pT4. inr"r"r. e . a simplerfunction of i: may be derived to fir thesevalues.t. whererue datais knownro be very pre_ cise.t' squares . the Because any individualdadpoint *"V l. of or .. The esti_ mation of valuesbetweenwell-known discretepoints is called inrerpolarion(Fig. .1.. lb and e.n.ii.pT4.::.. singre curve that represents generaltrend of the data... In addi-rion..rnroughout irequentoccasion estimate to intermediate values fave trom such tabJes.r.1...\.. the remainder of your career.narureis"uudt. Examples are valuesForthe density of water or for the heaica_ pacity of gasesas a function of temperature. Curve Fittingond Engineering ond Science.a curve or u ..takenas a s.".the curve is de_ srgnedto follow the pattern of the points . funct'ion a numberof discretevalue alon^g at . tar). s the range.1you other'on basis rhearnount errorassqciared the of of .1 oVERVIEW WhEr ls Curve Fiffing? 13 througtrto oescribe-techniques to fit curves to such data to esti-ates....::!::1'j-"H'. from interest tables for *"...t & &.. your ftrst exposure[o curve fitting may have been to deter_ mtne lntermediatevalues from mbulated data_for instance.."):.*'r. rn.rluuru! wucrs theoaul ls Known to very Dre_ .ascurre trtting..i'. chapters "o*. t.oure values of the : .. ri:" you rnay require a sirn. . lftaln ilterm"diute plified version of a complicared funcrion.ffi.

Special ulated. Here. For caseswherethe datais measured regression..you might utilize interpotions.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.282 4 FITTING PART CURVE f(x) (c) P FIGURE T4. with least-squares lating polynomials.If the model coefficients with measured model is compared existingmathematical . lineor through doto points: leosl-squores five curve fo Three ottempts fit o "best" io) i n l e r p oo i o n o n o ( c ) c . r f l e o f r r t e t o oo l o n . testing.an are data. All fields of engineeringand scienceinvolve problems this type. A second application of experimentalcurve fitting is hypothesistesting.Imprecisedatais often analyzed variable. havebeentab' and scientificproperties Although many of the widely usedengineering availablein this convenientform.I (b) regression. data:trend analysisand hypothesis when fitting experimental pattern of the data to make predicTrend analysis representsthe processof using the with high precision. 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.

which is depolynomials. l3 focuses linear regre. l4 ale del'otedtononlinearregression.. polynomials databut in a piecewise techniclrre. Chup. to approximate i ! 4. hol'r'to deon After a brief review of statistics. illustrate model.it may be appropripledictecl valuesof the nrodelwith observed valuesto testthe adequacy of ate to compare on the model.2 PARI ORGANIZATION to valuesthat bestflt the observed data. we also present how to calculate slopeand intercept this straight the of fbr In we ser'the and visualmethods evaluating validity of the lesults. is a linear functionof two or more signedtbr the casewhere the dependent exvariables 12. acldition.or higher-order Pctlynomial dealswith developinga bc'st clf parabolas. /5. tages This for an Finally. The first. xtn.culve-fitting ferential equations. fashion. of variable). well suitedfbr fitting datathat is genelallysmoothbut exhibits As such. curvefitting is irnIn additionto the foregoing engineering scientific and applications.15. of tical properties.sirtn cubics. in . intermediate interpolation usedfbr estimating is As discussed We the for precise ln are datapclints. to fit This approachis designedto computea least-squares of a nonlinearequaticln data.Among otherthings.it is particularly is interpolation with an overviewof how piecewise The encls abruptlocalclranges. Then.ssion. l6 presents alternativetechnique fitting precisedatapo.2 PARTORGANIZATION that Chap. has advanrvhenthe properolder is known befbrehand.if estimates the model coetTicients alreadyavailable. Chap. The second. discussing terminethe "best" straightline througha setof uncertain datapoints.On the ulrknown. Ofien. introduce to linesand parabolas connect of by basicconcept polynomialinterpolation usingstraight polynomill. of Chapter/4 begins with brief discussions polynomial fit regres.we developa generalized fbrm. preferable is Newton's interpolotirrg mial is unknown.Two procedure fitting an rrth-ordel ior points. called in fbr thesepolynomials equation fbrmatsare presentcd expressing whenthe appropriate orderof the polynopolt'nontial.4. portantin other nurnerical methodssuchas integrationand the approxirnate solutionof dil'Finally. are we how polynomialand multiple regression After multiple regression. describe fbr of equations. the alternativeclrrve-tittillg techniquecaTled valuesbetween previously. chapter imolemented MATLAB. techniqtres be usedto derivesimplefunctions can cornplicated functioDs.the last sections Chap.called the Lagrange interpolotingpoltrutmial. is on of perimental of datawherethe variable interest dependent a nunlber differentfactors. In Clnp.it nray be necessary determine are of other hand. to lits callecl splineirrterytolation.Besides quantitativc line.this will allow of both subsets a generullinear least-squares its statisand discuss general matrir representation ofregression a us to introduce concise Finally.ints. is. interpolutionis describecl. alternativenodels are comparedand the "best" one is selected the basisof empiricalobservations. eral approaches the linearization nonlinear andmultiplelinearreglessiou.polynomials derived this purpose. irrdependent .This is followed by a clescription multiple linear regression. This approach specialutility for evaluating has r1.

' . Lineo R"gression r CHAPTER OBJECTIVES The primary objectiveof this chapteris to introduceyou to how least-squares and data. /j.and u : velocity[m/si. a first approximation. Expressions play a critical role in their formuiaexperiments relationihipsderive in part fl-orntheor.Specificobjectives regression can be usedto fit a straightline to measured topics coveredare ' . the Knowing how to computeand understand meaningof the coefficientof error of the estimate.':. that this forcewas As we upward tbrce of air resistance. 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]. ( I 3. with MATLAB. .t : a drag coefficient [N (kg/m). An irrcJiridual susperrded . Knowing how to implementlinear regression YOU'VE GOT A PROBTEM to | we noted thaf a free-falling object such as a bungeejumper is subject the I n Chap. determination and the standard so to how to usetransformations linearizenonlinearequations that Understanding they can be fit with linear regression.1) come from the lield of fluid mechanics.One suchexperinrent depictcdin Fig. ' and the normal Familiarizing yourselfwith somebasicdescriptivestatistics distribution. inawind is is riorr. such Although suchas Eq. Knowing how to computethe slopeand interceptof a best-fit straightline with linear regression. l. c.v.

.N (m/s)fromo wind tunnel Experimentol for force(N) ond velocity doto exoeriment. be obvious. 2 Plot forceversus wind velocifu on obieclsusoended o w nd lunnel for in of TABTEl3.m/s F I G U R EI 3 .l .the relationshipbetweenforce and velocity may not be linear.As in Fig. severalfeaturesof the relationshipbear mention. The relationshipcan be visualizedby plotting force versusvelocity.This conclusion becomesmore apparentif we assume that force is zero for zero velocity. The resultmight be as listedin Table l3. 13. First. althoughit may not rather significant scatter. 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. the points indicate that the force increases velocity increases.Finally. m/s F. I I Wind tunnelexperimenl mecsurehow the force of o r reslstonce fo dependson veocily 1600 1200 z t\ 800 40 u.but exhibit particularlyat the higher velocities.2.R LINEAR EGRESSION 285 ri {\' * ----+ F I G U R E3 .the points do not increasesmoothly.t u. as Second.

If the numberis even. a of information-that is. that the valuesrangefrom a minimum of 6.For example.the rnedian the middlevalue.residualsumof the squares. and the normal distribution. The mostcomlron measure centraltendency the arithmetic is of mean. t . 6655 6 605 668_5 . standarddeviation.655 6775 6 55. r 3. will illustrate we how relationships Eq.l3 and 14. or \-.In addition. will first review some basic concepts we frorn the field of statistics.severalmeasurements were madeof a particularquantity. the fbllowing material is designed a brief introduction. If you are familiar with these subjects.For example.) dividedby the numberof points(n).r. There are severalalternatives the arithmeticrnean.Thesedescriptive statistics mostotien selected l'epresentl) thelocaare to tion of the centerof the distributionof the dataand (2) the degreeof spread the dataset.2.71 5 6 555 o 635 6 625 6 575 6 395 6 485 6715 6.Ifthe numberof measurements odd. These include the mean.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 .the conceptis not very practical.we will explorehow to flt a "besf' line or curveto suchdata.l DescriptiveStotistics Suppose that in the courseof an engineering study.395 to a maximumof 6. 2 ) (and all the sr.thereare actually TABLE13. ' ' F ) ] . as I 3.The urithntetic ntean (l) of a sampleis defined as the sunr of the individualdata points(.we describehow simple descriptive statistics and distributions in can be generated MATLAB.The medianis the midpointof a to groupofdata.rcceeding wherethe summation summaticlns this section)is from I = I in throughn.If you are unf'amiliarwith theseconceptsor rrrein need of a review.286 LINEAR EGRESSION R In Chaps.For continuous or variables such as the data in Table 13.775. ( 13.. [(x to 6 595 6 505 6 625 65r5 6 615 6 435 6.2. n (1 3 . The mode is the value that occursmost fiequenlly.The medianis sometimes called the 50th percentile. 13. Additional insight can be gained by summarizingthe data in one or more wellchosenstatistics that convey as much informationas possibleaboutspecificcharacteristics ( of the dataset. feel tiee to skip the following pagesand proceed directly to Sec.the dataprovides linritedamount ofa structural steel..The conceptusually hasdirectutility only when dealingwith discrete coalselyroundeddata.I ) arisefrom experimental like data.2 contains24 readingsof the coefllcientof thermal expansion Takenat facevalue. In so cloing.it is thearithis is metic mean of the two middle values. It is calculated first puttingthe datain ascending by order. of Meosure of Locotion.r STATISTICS REVIEW Before describingleast-squares regression.Table | 3.

a Meosures of Spreod. 5 ) Note that the denominator borhEqs.? (Dt'.5)is ru.)r-y.ifyis known and rr .6. e v The rnostcomlnon measure spreadfor a sampleis thestandarddeviation(s.555.3)and (13.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 .Hences.. However. Anotherjustification for diviclingby n ..As such.v.which all occurtwice. deviationwill be small..1.715. We should note that an alternative.)iszero.(13.. remainingvalueis fixed..625.I in is ref'enedto as the tlegrees freedont.5).which is called the variance: r r ' .y-)2.more convenientforrnula is availableto compute the variance: -2 . n-l (I3. Eqs. Thus.I STATISTICS REVIEW 287 ll ii t four modesfor this data:6. The simplestmeasureof spreadis the range. are said to be basedon rr . For the casewhere n : 7. and s. is often multiplied by 100 so that it can be of It expressed the form of a percent: in s). ..conare out sequently.If the numand bers had not beenroundedto 3 decimal digits..3)and (13. ) will be large.ir2 (13. The spreadcan also be represented the squareof the standard by deviation.This statisticis the ratio of the standard deviationto the mean. The quantityru.(13. ( 13. x 1007o (1 3 . if the individual measurements spread widely aroundthe mean.\:)l n| (1 3 . about of ) the mean: ( 1 3 .I deof greesof fieedom.5)yield a meaningless resulrof infinity.6. This nomenclature derivesfrom the fact that the sum of the quantities uponwhichgisbased(i. it would be unlikely that any of the values would even have repeated twice. 7 ) t .I3.)' l. ------------l- frv.I of the valuesare specified..1 the of the valuesare said to be freely determined.e.1 is the fact that there is no suchthing as the spreadof a singledatapoint. is not it considereda very reliable measure because is highly sensitiveto the samplesize and is it verysensitivto extrelne alues.If they aregroupedtightly.: f u.)r.4) Thus.Although it is certainlyeasyto determine.6) This versiondoesnot requireprecomputation ! andyieldsan identicalresultasEq.)3 where S' is the total sum of the squares the residualsbetweenthe data points and the of mean. . the standard s.or $: Itr'.it providesa normalizedmeasure the spread.655.if continuous dataare groupedinto equispaced intervals.Consequently.s. only n .). (and. of A final statisticthat has utilrty in quantifying the spreadof data is the coefficient of variation (c.f-yt.it can be an informative statistic. 6. the differencebetweenthe largestand the smallest value.

t -: 158'4 :6.02402 0 03062 T I s8zdo 67Tn6 40 896 41 449 42 055 42. as [Eq.oao22 0.605+ 6.Jl3"un t" compured (13.3 Dotoond summotions compuiing for simple descriptive stotistics the for .variance.L : 0 .2.02723 0 0t323 0.61. The data can be assembled tabularform and the necessary in sumscomputed ""HJt.23) 43 494 43.3.00062 0.615)12:6.3)]: sr.2.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.445 42568 42968 43 099 43. (13. Computethemean.00203 0 001 3 2 0 00063 0 00003 0.00002 0.289 44422 44.median.00302 a 00422 a.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.655 6 655 6 665 6.62b 43 758 4 38 9 1 4 3B 9 l 44 023 44 285 44.315 42.2)].71 5 6755 6.217000.685 6.)85 42.00203 0. coefficients thermol of exoonsion fromToble (Jl -.andcoefficient of variation for the datain Table 13.oo723 0. As in Table 13.00062 o. 0 9 7 1 3 3 z TABTE13.6 24 Becausethere are an even number of values.288 LINEAR REGRESSION EXAMPLE .61 5 6 625 6 625 6.775 o 04243 4.689 45091 45091 45630 45901 u4) 0J/ . : 0.635 6.715 6. standard deviation.555 6 565 6 575 5 595 6 605 6.ao722 o 41322 a o)322 o.217000 *+ . of is which canbe usedto computethe standard deviation [Eq.oat22 0 00302 0.l3.01103 0 00903 o.the sum of the squares the residuals 0.the median is computed as the arithmetic meanof the middletwo values:(6. Solution.

r.A histograntis constructed sorting the meaof by surenrents into intervals.I3.i.3 is one such on characteristic shape-the normal distribution.6 289 The validity of Eq. br.3 A histogrom of used to depict the distribution doto.Given enoughadditionalmeasurements. the histogramfor this particularcasecould eventuallyapproach normal distribution.6)can alsobe verifiedby computing \: a 1045.i rli 'i"ll]r[io . The units of measurement plotted on the abscissa are and the frequencyof occurrence eachinterval is plotted on the ordinate. If we have a very large set of data. The symmetric.097133 x1007a:1. rh^ ^"-h^.v. :0.6.a histogramcan be createdfor the data from Table 13.or bins.we can seethat the bin with the most values is from 6. a.fpq 1l^p .7)]: of c.that now that we have groupedthe data.bell-shaped curve superimposed Fig. (13.A histogramprovidesa simple visual representation the distribution. it is more common to report the most frequent range as the modal class interval.ofronnnn.5)]. is the shapewith which the data is spreadaroundthe mean."" .64. ihe q-66+\ hcll. 13. of As an example. l3. (13. OJ 0) L2 .47Vo 6.009435 s.009435 24-l 13.Although we could say that the mode is the midpoint of this b|n.6 to 6. the FIGURE I3.62.ion.:_ 0. (13.2 The Normol Distribution Another characteristic that bearson the presentdiscussion the datadistribution-that is.651 (158.I STATISTICS REVIEW the variance [Eq.097133)2 and the coefficient variation[Eq.6.The result (Fig.r'..?: (0. ^{ l^r^ ^^inis increcses. the histogramoften can be approximatedby a smooth curve.2. Notice also.a) suggests that most of the datais groupedcloseto the meanvalue of 6.t-cned n:srr-a.1.rn.400)' /24 : 0.

it returns a row vector containingthe arithmetic mean of eachcolumn of >r.2s.61. )r + 2.2.we calculated Example13. Basedon our analysis.35. For the arithmeticmeanis computedasmean(x). standarddeviation. to y * . the rangedefined by tr .from random rrrrmher oeneration fn . 6.max(s) :ne 6.0094348 anc - 0.3B >> var(s).290 LINEAR EGRESSION R The conceptsof the rrrean.r example. very simpleexamA ple is their useto quantify the confidencethat can be ascribed a particularmeasurement.the rangedefinedby.6 for in and s.405'734 of and6.1.1that) = 6. norand mal distribution all have greatrelevance engineering to and science. will encompass approximately68Vcof the total rneasurements.median (s) h h 6.555 >> min(s). If x is a vector.'rrrve fiffinq fo desiqn .794266.std(s) :nc 0.2: g >> format short (s). t/a >> range=max(s) min(s) O. to If a quantity is norrnallydistributed.s.the function returnsthemean of the vector's values.s. The following is the result of using meanand the otherstatistical functionsto analyzea column vector s that holds the datafrom Table 13. :0.097133.395 o.If it is a matrix.3 DescriptiveStotisticsin MATLAB StandardMATLAB has severalfunctionsto computedescriptivestatistics.ifsomeonetold us that they had rneasured valueof7. the datain Table 13. to approximately95Vo.residualsum of the squares..097133 I MATI-AB also ofiers a Statistics Toolbox that provides a wide range of common statistical tasks.mode >> mean (s). Similarly. 13. will encornpass For example. Becauseit is so far outsidethesebounds.we a would suspect that the measurement might be erroneous. can tentativelymake the statement we that approximately95Vo thereadingsshouldfall between6..1.v .

8 6.528 6. the mode function only returnsone of the values: 6.75 6.1.55 6.85 . x : a vector specifyingthe midpoint of eachbin.2.Note that althoughthere are four valuesthat occur twice.his'* (y) without output arguments l0 bins determinedautomaticallybasedon the ranqeof valuesin r.'.65 6_7 6. and y is the vector being analyzed.49 6.5 6.642 6.4 Hisfogrcmgeneroiedwith the MATLAB hist funciion 6.5.68 6.566 6.For just producesa histogrambar plot with example.6 6.604 6. MATLAB can also be usedto generate histogrambasedon the hist function.55.4I4 6. 13.4is similarto the one we generated handin in by Fig.the result is >> hist(s) 3 6. For the datafiom TabIe 13. The a hist functionhasthe syntax ln.7s5 The resulting histogram depicted Fig.I STATISTICS REVIEW 29r Theseresultsare consistent with thoseobtainedpreviouslyin Example 13. xl = hisL (Y. FIGURE I3.718 6. x) where n : the number of elementsin each bin.3.452 6. 13.Note that all the arguments and outputswith the exceptionof y are optional.I3.

any straightline falling within the dashedlines will minimize the sum of the absolute values ofthe residuals. ( x z . and e is the error. l 3 . Although such"eyeball" calculaapproaches have commonsense appealand are valid for "back-ot'-the-envelope" tions. L ' i=l : Lt. 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 .this is an inadequate criterion. y ) . .as.as in L't r:l i ".2 TINEAR TEAST. . or rcsidual.8)as by Eq.t "r.5bdemonstrates why this criterionis also inadequate. the residualis the discrepancy value.which can be represented rearranging (13.t. equal to zero because ab One way to remove the effect of the signs might be to minimize the sumof the solutevaluesof the discrepancies. they are deficient becausethey are arbitrary.'t i:l i . predictedby the linear equation.:) l ) . this criterion also doesnot yield a uniquebestfit.any straightline passingthroughthe midpoint the of connectingline (excepta perfectly vertical line) resultsin a minimum valueof Eq.somecriterion must be devisedto establish basis a fit.2. 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.9) Thus. unlessthe points defineaperfert would straightline (in which case. 5 a . . . . . One way to do this is to derivea curve that minimizesthe discrepancy between data the points and the curve.r. f for The mathematical expression the straightline is y : a o* z t l x1 e (13.292 LINEAR EGRESSION R 13.However. forthe To removethis subjectivity. I 3.SQUARES REGRESSION Where substantial with data. differentanalysts draw different lines. .That is.asillustratedby F i g . ( .a o- alxil (l3ll) Figure 13. .However. : i . To do this.) /-'rl i:l (l3. ' . t h e b e s t f i t i s t h e a line connectingthe points. ' . betweenthe true valueof y andthe approximate a0 + atx.the best curve-fitting strategy to derive is error is associated an approximatingfunction that fits the shapeor generaltrend of the datawithoutnecessarily matching the individual points. Thus. we must first quantify the discrepancy.interpolationwould be appropriate).10) positive and negativeerors cancel. between the model and the observations. ) n ) . 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.a1x. in as i .lo) wheren : total numberof points.(13. r . e:!-as-a1x (13. One approachto do this is to visually inspect the plotted data and then sketch a "best" line through the points.respectively. the four points For shown. O b v i o u s l y .8) where a6 and a 1 are coefficients representingthe intercept and the slope.

and Wilkes. Luther. a single point with a large error.2 LINEAR LEAST-SQUARES REGRESSION 293 1il lrl li .l il il ttltt' -l. .il I 3.e 6 1 1 t o s r 6 .5c. )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.5 F x n m n l e s n [ s o n . e o p A third strategyfor fitting a bestline is theminitna-r criterion. As depictedin Fig. 1969). e s s i o n l .arxi)z L-t /' . t . A strategythat overcomesthe shortcomings the aforementioned of approaches to is minimize the sum of the squares the residuals: of iltI S.l il ii . t h o t o r c : n g l e n r r n t c f n r .this strategyis ill-suited forregression because gives undue it influence to an outlier-that is. : 5-"i : f-.-l (13.r lm ^ m . b e s ' r i . It should be noted that the mininax principle is sometimeswell-suitedfor fitting a simple function to a complicatedfunction (Carnahan. 13.' : (v. e .as.-1 .i (c) FIGURE I3. 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 . z e s t h e s u m {9 b o f t h er e s i d u o l(s .12) . line the is chosenthat minimizes the rnaximumdistance that an individual point falls from the line.In this technique.tt't7'j-'''-- oo2ote' . .

r r . I. Fit a straight line ro rhevaluesin Table i3. In this application.r5) Solution.( / { ). all summationsare fiorn r : I to rl.. The data can be set up in tabulirrfbrm and the necessarv sums ..unlessotherwiseindicated.Ifthis is done.)' This resultcan rhenbe usedin conjunction with Eq.rI.-..i ltT. will prewe senta techniquefor cletermining varuc-s (h andat that minimize the of Eq. . un - tt 1x.t'' O . .r... has a numberof advantages.) t't A^: 2 : -z \ .vand.2 1 L i n e o rR e g r e s s i o n ProblemStotement.s and at.respectively.t. f . l Lll.f o : f r. Settingtheseclerivatives equalto zero will result in a minimurn S.u-f.r. .-f.atx wherev andi arethe means . 13. + ( \ \s r- \LJ'ri)tt1 Thesearecalledthe nonnal equation. .the equations be expressed can as o:f.. (ao tr (1. which is called leastsquares. realizingthat I a0 : noy.r.a 1 ' r .r.r.12) differentiated respect each for is with to unknown coeflicient: i)s' ilLti : -? \. fbrce is the clependent variable(. _ . (13.r3) to solvefor ao: i . .(I '.r'. They canbe solvedsimultaneously for . .'? .r). of EXAMPLE 3.. ( 13...*i . (r3. \t r .\'l Note that we have simplified the summationsymbols. : f . (f * ". L Now. (13.1. Eq.. :Lr.1.Beforediscussing a these pr-o-pertles. .) '\ .. we can express equations a setof the as two simultaneous lirrear equations with two unknowns andnr)..r.n...n.294 TINEAR EGRESSION This criterion.r'..v) and velocityis the independent variable (. includingthat it yielcls uniqueline lor a given setof data.2.. I. ) r .2 leost-SquoresFir of o Srroight Line To determine varues .r 2)..

400 22AAO 30500 73 204 5 8 .41024u withthedata.400 I 1. 13.16) with and as The slopeand the interceptcan then becalculated Eqs.135 : 641.6.000 3r2850 The meanscan be computedas 360 _ i-_-45 8 _ }: 5.along FIGURE I3.15) (13.41024 8(20.. is in Theline.4 295 needed compule best-fit for the doto to the line Dotoond summotions Toble. 4 5 0 254 t. from xi li x! r00 4AO 900 r.REGRESSION I 3. shown Fig.47024(45)-234..904 6.r 0 0 I 16. I ]3.2857 fit ofy and.600 2 500 3. 8 s 03 6 0 ( s 3 5 ) :19.400)-(360P * : ao : 641. o' : .6 ine Toble 3 t 1 leosf-squores o strclght to thedotofrom fitof 1600 1200 800 z .) r.2 LINEAR LEAST-SAUARES TABTE13. theleast-squaresis Usingforceandvelocityin place F : -2342851-t 19.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.600 4.815 g (13.875 19. 8 ( 3 1 2 .

7 The residuol lineor in regression representsverticol the dlstonce o betweendotopoint ihe ond s t r o i g hlti n e .(13.7).3. A number of additional propertiesof this fit can be elucidated by examiningmore closely the way in which residuals were computed.17) s.18).the square ofthe residualrepresented square the ofthe discrepancy between the dataand a singleestimate the measure centraltendency-the mean. we will show how transformations be employedto derivean altemativebesrfit line thatis can more physically realistic.2resultsin a largersurnof thesquares of the residuals. the line is uniqueand in termsof our chosencriterion is a "best" line through the points.17). Thus.296 LINEAR EGRESSION R Notice that althoughthe line fits the datawell.:ftlr-l)2 (13.3 Quontificotion of Error of lineor Regression Any line other than the one computedin Example 13.In Eq. It can be demonstrated if these that criteria FIGURE I3. 13.: f (li -ao-arxi)2 -i:t Notice the similarity betweenthis equationand Eq. of of the square the residualrepresents square the verticaldistance of the of between data the and anothermeasure centraltendency-the straightline (Fig.12)) n .t. (13. (13.2. 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 Section 13. the zero interceptmeansthat theequa" tion predicts physically unrealisticnegativeforces at low velocities.4) (13.18) In Eq. | 3. -------*o Measurement a0+ afii .Recall that thesum of is the squares definedas [Eq. (13.

called the standard error of the estimate. the standarderror of the estimate quantifies the spreadof the data.q.The subscriptnotation "y /x" desigis nates that the error is for a predicted value of y correspondingto a particular value of x.anotherjustification dividing by n . for yieldsa meaningless the casewheren : 2.ifthese criteria are met. This is the magnitude of the residual error associatedwith the dependent variable prior to regression. tbl . 13.19) resultof infinity. (13.os ind.F.this quantityis designated .8b in contrastto the standarddeviation s. that quantified the spreadaround the mean (Fig. ( 13. n-2 (13. (13.e./r - s. variable(in our case. As was the casefor Eq. we returnto the origiuseful for comparisonof severalregressions nal data and determine the total sum of the squaresaround the mean for the dependent g y).the most likely) estimates ag of and a1 (Draperand Smith. least-squares regression will provide the best (i.After performing the regression. syTxQuantifiesthe spreadaround the regression line as shown in Fig. a "standarddeviation"for the regression line can be determinedas [comparewith Eq. 13..2 because two data-derived estimates----as a1and were used to compute S. 13. thus..9).we can compute Sr.17).18). Also. in in (o)to (b).3)] J].I 3.To do this.This characterizes residual the FIGURE3.8 I (o) Regression showing thespreod thedotooround meonof thedependent doto of the vorioble ond (b)lhespreod thedotooround best-fit Thereduction thespreod goingfrom of the line. This is particularly (Fig.However.This is called the maximumlikelihoodprinciple in statistics.19) where s_"7.Thus. we have lost two degrees freedom.As with our discusof sion of the standard deviation.8a). ot represenls improvement to the due linecregression. noticethat we now divide by n . Just as was the case with the standarddeviation. the sum of the squaresof the residualsaround the regression line with Eq.cored t[e be'lsl^'oped by cu'ues therigl^t.. Theseconceptscan be usedto quantify the "goodness"of our fit.2 is that thereis no for such thing as the "spreadof data" arounda straightline connectingtwo points. In addition.(13. 1981).2 LINEAR LEAST-SQUARES REGRESSION 297 are met.

.2. fit. the differenceis normalizedto S. : 0 and 12 : l.9 E r o r ' . s.? (I r. where 12 is called the coefficient of determination and r is the correlation coefficient . therefore.)' EXAMPLE 13. and coefficient thefit in Example lrlyl'?: . Thedatacanbe setup in tabular form andthenecessary computedin sums as .)'J.sometimes The differencebetweenthe two quantities.) (L. : Sr and the fit represents improvement. Compute totalstandard the deviation. . to vield . no is alternativeformulation for r that is more convenientfor computerimolementation r: nf(xiri) . is. 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. Becausethe magnitudeof this quantity is scale-dependent. /-.$ . signifying that the line explains100% the An variability of the data.'2 : S'-S.(Ir')(Iy. S.3 Estimotion Errors theLineor of for Leqsl-Souores Fit Problem Stqtement. of \: J r)). provement or error reduction due to describing the data in terms of a straight line rather than as an averagevalue. standard of the the error estimate. For 12 : 0. quantifies imthe sum of the squares.S. thecorrelation for 13.298 LINEAR REGRESSION o o I3 FIGURE .I r. For a peri-ect t.

79 Thus.297 4. (13.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 .at the rninimum. Justbecause is "close" to I doesnot meanthat the r'2 "good.3)] is : 508.52 735.579 8. 3 23 3 380.808. 1 li eo i atxi (yi .t2863 1 .g .441 68. 2. 8 3 7 8 9 .5r. addition.r( s. 297 of or r : \/0.Although their graphsare very different. Theseresultsindicatethat 88.Although the coefficientof determia provides a handy measureof goodness-of'-fit. 0 h four datasetsconsisting I I datapointseach.23 9 3 39 3 I.245 9il 30 I6. 808.you shouldalwaysinspecta plot of the dataalong ln with your regression curve.all of have the samebest-fit equation.r is not even linear.699 B ]. 0 6 6 t . and the samecoefficientof determination.171 7./.2 LINEAR LEAST-SQUARES REGRESSION 299 TABTE13.. word of cautionis in order. ] .0r6 3 3 42 2 9 3 53 9 r 6 53 .454 -39 58 t55t2 349 82 544.t)2 (!i . 1 1 8 92 : 0. (13.535 327..ao .1tB The standard deviation [Eq..5 Dotoond summotions needed compute goodness-of-fit to the stotistics f o r t h ed o t of r o mT o b l e 3 . . 8 0 8 .26 and the standard errorof the estimate [Eq.444 216. Draper and Smith ( 198l) provide guidanceand additionalmaterialregardingassessment resultsfor linear of regression.I 3.I B O 16. The extentof the improvement is quantified [Eq.e c a m e u p w i t h si . ." For example. ( 13. A1 3n lF i.880-5 l . by Before proceeding.)': 3 + 0. is possible obtaina relativelyhigh valueof r'l fit is necessarily it to when the underlying relationshipbetween-y and .441 1.the linear regression model has merit.88[5 : 0.7 1 6 .19)] is : 189.20)l by I 1 . should be careful not to ascribe you nation more meaningto it than is warranted. 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 ) . because sr.05clc the orisinal uncertaintv hasbeenexplained the linearmodel.9383.

populationgrowthor radioactivedecaycan exhibit suchbehavior. For others.However.300 LINEAR EGRESSION R t3 t:) 10 10 10 F T G U R E. 13.3 TINEARIZATION NONTINEAR RETATIONSHIPS OF Linear regression providesa powerful techniquefor fitting a bestIine to data. techniques such as polynomial regression. Another exampleof a nonlinearmodel is the simplepower equation: ). in are appropriate. This model haswide applicabilityin all fields of engineeringand science. which is described Chap.11a.the equation representsa nonlinearrelationship(for fu l0) between.23) wherea2 andB2 areconstant coefficients.y: 3 + O.and the first step in any regression analysis shouldbe to plot and visually inspectthe datato ascertain whethera linear model applies.As depictedin Fig. As depicted Fig. I3.vand x. In some cases.For example.It is very frequently used to fit experimentaldata when the underlyingmodel is not known.22) where a1 and B1 are constants.14.transformations be usedto expressthe datain a form that can w iscompatible ith linear egression.11b.: a2xP) ( 13. 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. it is predicated the fact that the relationship on betweenthe dependent and independent variables is linear. equation (for B2 l0)is in the nonlinear. l O t3 Anscombe's doto olong ihebestfit . . 13. r One exampleis the exltonential model: ! : olreP" FIGU {o)Th (fJore (l3.5x four sets with Jine. This is not always the case.

ond equotion. It neeringand science. has many applications.r (d).1lc) that levelsoff. or "saturates." x nonlinearrelationship -y particularlyin biologically relatedareasof both engiincreases. .a simpleralternative to usemathematical can be employed to fit the form the equationsinto a linear form. l3.1.I 3.2+) where a3 and B3 are constantcoefficients. (b) Ports {e). versions these lhct lineorized oi equolions resu tromsimple lllore equation: A third exampleof a nonlinearmodel is the saturation-growth-rate J . Then linear regression to equations data. ond Theerponentiol equotion. r (1. (c)thesoiurotron-growth-role lol i tronstormotions. Nonlinear regressiontechniquesare availableto fit theseequationsto experimental to is manipulations transdatadirectly.3 LINEARIZATION NONLINEAR ELATIONSHIPS OF 301 :-: azxQl (dl GI (f) HGURE I r3.\':0.However.r_ D r+ .This model. thepowerequolion. also representsa as between and r (Fig. which is particularly well-suited for characterizingpopulation growth rate under limiting conditions.

l li log ri IogJi (log r. Equation(13. 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 .23) thedatain Table13. 1 l / ) .)2 log x. a plot of ln y versusr will yield a straightline with a slopeof B1 and an intercept ln o1 (Fig. 13.302 LINEAR REGRESSION For example. to In their transformedforms. for predictivepurposes.785 3. l3. Fit Eq.162 3. They can then be transformedback to their original stateandused for The following illustratesthis procedure the power model.AAl 3Bll 4 390 4. (13.4 EXAMPLE Equotion Fitting Dotowith the Power transto ProblemStqtement.2t of Thus.23) is linearizedby taking its base-l0logarithm to give logy:loga2*B2logx (r3. thesemodels can be fit with linear regression evaluate the constantcoefficients.488 5 386 6016 I ilbn .26) Thus.622 /0 ) 1a r 398 2.6 xi t: ?Tr :2'5644 needed fit the powermodelto thedotofrom to Dotoond summotions r l l l o b l el 3 .|ld).1usinga logarithmic formation. The datacan be set up in tabularform and the necessary Table 13.Ile). a plot of log y versuslog x will yield a straightline with a slope of B2 andaninter' this cept of log cv2fig. a plot of 1/y versusl/x will be linear. as done here.Eq.404 3. Equation(13.886 3.1 3 . Note that any base logarithm can be used to linearize model.t6r lu) ) I 000 I 693 2. The meanscan be computedas . as sumscomputed in Solution.5i5't TABTE| 3.182 2 567 2.:Yy:1.with a slopeof Bzluz and an intercept 1/o3 ( F i e .845 L903 tlouo 1 398 t B45 2.24)is linearizedby inverting it to give 1 I I qS ful +'-- (13. However.919 3.21) a3x of Thus.6. logy.744 2.the base-10logarithmis most commonly employed.732 5.584 2. 13.22)can be linearizedby taking its naturallogarithmto yield l n y : l n c v r* f r x (13. (13.086 2.

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.I3.0 . 6 2 2-) I 2 .( 1 3 .r( 1 2 .0 . 5 1. 8 ( 1 0 . 13.9842 1 c t l : 2 . 1 5 ) a n d 1 3 . 6 0 6 ) 2 6 :1.3 TINEARIZATION NONTINEAR OF RELATIONSHIPS 303 ri log v 1000 100 log. 6 0 6 1 2 0 l 5 ) . . equotionolong the ( The slopeand the interceptcan then be c a l c u l a t ew i t h E q s . 5 6 2 0 t l . 5 1 5: ) . 5 6 4 4 1 .r ) 1600 1200 800 400 0 40 (b) 60 80 F I G U RI 3 . 1 6 ) s d a al : 8 ( 3 . 9 8 4 2 l o gr The fit along with the datais shown in Fig. 5 6 2 0 The least-sqLrares is fit l o g r ' : . 9 8 1 2 ( 1 .12a.r .

somestatistical assumptions areinherent that procedures in the linear least-squares are l. vslusr for a given x must be normally distributed.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. For example. 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 . You are urgedto consultother of referencessuchas Draperand Smith ( 1981)to appreciate aspects nuances regression and of that are beyondthe scopeof this book. Eachx has a fixed valuel it is not randomand is known without error.9942.2J4lut This equation. cierrts the power nrodelare determined cy2 19 05610 of oS : Using fbrceand velocityin placeof . knowledge fromthe field you are studying often has a large bearingon the choice of the appropriate mode.4 (Fig. have focused the We on simple derivationand practicaluse of equations fit data. In this section. t3.1 Genercrl Commenlson Lineor Regression Beforeproceeding curvilinearand multiple linear regression. The I' Such assumptionsare relevant to the proper derivirtion and use of regression.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.12b. do this. transformed to the resulthastheadvantage that it doesnot yield negativeforce predictionsat low velocities.3. For example.rst to we ernphasize inthe troductory nature of the foregoing materialon linear regression. 13. the least-squares is fit e8a2 F :0.304 LINEAR EGRESSION R We can also display the fit usingthe untransformed coordinates. The -yvaluesare independent randomvariablesand all have the samevariance.You shouldbe cognizant to ofthe fact that there are theoreticalaspects regression of that are of practicalimportance are but beyondthe scopeof this book.6)usinglinearregression the untransformeddata on Although both resultswould appear be acceptable.is shownin Fig. 13. . 2. The fits in Example 13. mr. We will also show lrow linearregression be implemented can with the built-in po ly f i t f unction.2(Fig.2741 andB.Further.12)should be comparedwith the one obtained previouslyin Example13.the first assumption rneirns that (l) the "r valuesmust be en'or-tiee (2)the and regression -t'versusx is not the sameas r versus1.we will show how a simple M-file can be developed determine the fo slope and interceptas well as to createa plot of the dataand the best-fit line. I3. alongwith the data. 3.r'andr.thecoeffiTo :0. 13. =1. by Thus.

the coefficient of determination. The routinedisplays the interceptand slope.4)by applyingthe Iosl0 function to the data as in >> linregr (log10 (x). 4 1A 2 .9481 -0.13). BB05 r 9 .16). and a plot of the best-fit line along with the measurements.4 COMPUTER APPLICATIONS 13. 13.2 3 4 .I 3.9842 .(13.15)and(13.The required summationsare readily computedwith MATLAB's sum function.1 MATLAB M-file: linresr 305 An algorithm for linear regressioncan be easily developed(Fig. A simple exampleof the use of this M-file would be to fit the force-velocitydatathat was analyzed Example13. 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. > > 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.2: in >> x = 110 20 30 40 50 60 70 B0l.4.5624 7.1oql0 (y) ) 0.y) 0. Theseare then used to compute slopeandthe intercept the with Eqs.

a(1).sy^2) ^Z ) ..*x).*y). 12l = linregr(x. a(2) = sy/n-a(1)*sx/n.y) % linregr: linear regression curve fitting % 1. xp.I3 I3 AnM{ilelo implement regression. sx2 = srtn(x. % 12 = coefficient of determination a(2) n = length(x).id o n . a (1) = (n*sxy-sx*sy) / (n*sx2-sx.2) . if length(y)-=n.max\x\ .5 2.5 t.4 t.. 12 ] = linreqr (x. T2 = ((n*sxy-sx*sy) /sqrt (n*sx2-sx^2) /sqrt (n*sy2.*y). and incercept.5 1. lineor function Ia.yp) g r r . sy2 = Sum(y.y.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.)..'2) .Sum(x. end x = x(:).306 LINEAR EGRESSION R 3.x and y musf be same length. % create plot of data and best fit lrne xp = lanspace (min (x) . plot(x.t 1. ? convert i_o column vectors ( S X = 3 s 1 1 tx ) .o 1.8 FIGURE .o. S y = S u m ( y ) . yF a(l)*xp-a(2). error(. y = y(:). sxy .

it}fS9. B 75 A . >>x >>y >>a t10 20 30 40 50 60 70 B0l. n) wherex andy arc the vectorsof the independent the dependent and variables.5 CASE STUDY 13. 4702 -23 4 .i] : substrateconcentration. another. .y.I3.2857 Thus. r 1 Because straightline is a first-order a polynomial. . t) will returnthe slope and the interceptofthe best-fit straightline.TheMichaelis-Menten eqvation commonly is usedto describe suchreactions: u-- u_ [. + p n x* p n ..It can be applicdas in >> p = polyfit (x.14. the slopeis 19.!l. .and y: >> y = polyval(a. 6 4 7 .The function returnsa vectorp containingthe polynomial's coefficients. polyf it (x. . For example.As in Fig.the uct. respectively. 13.. and ft" : a half-saturation constant..S]. y. the equation describes a saturating relationship which levels off with increasing [. .4. u-: the maximum initial reaction velocity. prodone into the ."fl. Enzymes as catalysts speed the rateof chemical act to up reactions in living cells. - E N Z Y ME IN E T IC S K Bockground.y . [. can then be used to compute a value using the coefficients. x) wherep : the polynomial coefficients. potyf ir (x.1) I9 .In mostcases.. : d .2 MATTAB Functions: polyf ir ond polwal 307 MAILAB has a built-in function polyf it that fits a least-squares nth-orderpolynomial to data.28) where u : the initial reaction velocity.s] k"+ [s] (13. The graph also illustrates that the half-saturation constant correspondsto the substrateconcentration at which the velocity is half the maximum.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 " .t+ . . is Another function. . polyval.2857.q.. olld n : the order of the polynomial. 125 7A 380 550 610 1220 830 14501.It has the seneralfbrmat: >> y = polyval (p."1' 1 rr .4702and the inrercepr -234.45) the best-fit value at x. theyconvert chemical substrate.

13.29) This model also describesa saturatingcurve but.35 036 Employ linear regressionto fit this data with linearized versions of Eqs.3 o.[s]2 ft"2 [S]2 + (t3.308 LINEAR EGRESSION R continued Second-order Michaelis-Menten model 0. Solution.2D.27) k.28)and (13. 13. Two versionstheMichoelis-Menien of enzyme of reAlthough the Michaelis-Menten model provides a nice starting point. 33 5 B9 0. I . k" FIGURE .assess the validity of the fits with both statistical measuresand graphs.5u.a7 IB 0. it has been fined and extended to incorporate additional features of enzyme kinetics. as depictedin Fig. Aside from estimating the model parameters. One simple molecule where the binding of a substrate extension involves so-calledallosteic enzymes. Supposethat you are provided with the following data: tsl U 1.For cases at one site leads to enhancedbinding of subsequent two interacting bonding sites. with molecules at other sites.1 3 3 0.T4 I3 model kinetics. 8.22 d a7 F.. (13.the squared concentrationstend to make the shapemore sigmoid.which is in the format of the saturation-growth-rate (Eq.14.28). model Equation(13. 6 0 . can be linearizedby inverting it to give (recallEq.29). or S-shaped. the following second-orderversion often results in a bettu fit: 't): - - u..

19u2 tl.iir 0.25701S1 86.Ll 0.2ja A. O._ 5. If this is correct. the dataclearly appears be curved. the maximum velocity 15'2510)is much greaterthan the highestobservedvelociry tb.9t4l The model coefficients can then be calculated as >> vm-Ira(2) \ rrr i: aRral ( 1 j -':' .. Equation (13.36.28) can also be linearizedby inverting it to give I I k? I +j_ u D.8 3 4. ii{.l7iiO i:l = ? .15b). > > L a . 13. and ft.13can againbe usedto determine the least-squares fit: l9... Beyondthe visual evidence.13 can then be used to determine the least-squaresfit: >> S=11.I5ta shows the transformedversion.07 0.2260) is much bigger than the maximum substrate concentration (9). an eyeball estimatewould suggestthat u^ shouldbe about 0.5 CASE STUDY . 4 4 9 ) . / v ) ... the halfsaturation tate (86.0.the fit is obviously unacceptable. the best-fit model is .l 5 0. 1.2?. 2 : t6 ( ) Thus. Figure l3. In addition. of 2 to 3.When the original equationis plotted to along with the data in the untransformed version (Fig.j.1n*J ks= 3 r :.36 or 0..{S=\. . . For the untransformed case. x. shouldbe in the rung.i:.2260 [s] + Although the highralue of 12might lead you to believe rhat this result is acceptable. 13.sol. r . >> v=f 0.4A22 12= The linreqr 0. For example. [Sl2 The linreqr function from Fig. 309 :i continued function from Fig. .37. The foregoing analysiscan be repeated the second_order for model. The data is clearly leveling off at about 0. The problem is underscored when the fit is plotted along with the clata.Although the straightline iollows the upward trend.1 . r ' 2 l = L i n r e g r ( 1 .6406 is obtained.ryifiru::ri.5 G 8 9l. inspection of the coefficients might raise doubts.a much less acceptable result of I . j. 16.3t. u. 13.I3.3 1. poorness the fit is alsoreflected the of by statistics like the coefficient of determination.

the ft.6 a 0.2 0 isl I3 FIGURE .r1 (b) Originalmodel 0.is slightly greaterthan the highest observedvelocityand stthcfrqfc nnnnanfrofinn /o\ . (a) Transformed model 0.4 0.91r+[s:p of Although we know that a high I doesnot guarantee a good fit. In addition. (13.3to LINEAR REGRESSION #i*.i .1 Substituting these values into Eq. the parameters trends in the data.9929) is promising.I5 The model with fit{line) Plots leosl-squores of theMichoelis-Menten olonq doto(points}. BI?. that is. in how when the fit.5 14.4083 >> ks-sqrt(vm*a(1)) 2 . the fact thatit is very withthe values also seemconsistent high (0.8 0.29) gives 0. The model coefficients can then be calculated as >> vm=I/a(2) 0. plotin of viewed iheuntronsformed.4083ts12 u:7. {o)shows tionsformed ond{blshows lhefit looks oilgrnorrorm.4 o.

we should never solely rely on statistics such as I as the sole goodness offit. Beyond this specific result. a graph of the untransformed model and data should always be inspected. First. And for caseswhere transformations are employed.I3.2 0. 13. Beyond the graphs. this does not always translateinto an acceptablefit in the original format. in Fig. the goodnessof the fit is also reflected by the fact that the coefficient of determination for the untransformedcasecan be computed as I : 0.16a.lt+F.4 0.1 0 tsl FIGURE3. regression equations shouldalwaysbe assessed basisofassessing graphically. Based on our analysis. although transformationsmay yield a decent fit of the transformed data.ond ihe originol the The {points). When the original equation is plotted along with the data in the untransformed version (Fig. there are a few other generalconclusionsthat can be drawn from this case study. Finally.9896. 0.we can conclude that the second-ordermodel provides a good fit of this data set. form graphically.the fit nicely follows the trend in the measurements.$fii:{:ifl (a)Transformedmodel t/$12 ( b )O r i g i n a l o d e l m 0. Second.5 CASE TUDY S 3ll ffi'-ru conrinued i. plotin (o)shows trqnsformed ib)shows untronsformed. Linear regression assumesthat the scatter of points around the .the transformed As The adequacy the fit can be assessed of results appear linear. This might suggestthat we are dealing with an allosteric enzyme. The reasonthat this might occur is that minimizing squaredresidualsof transformeddata is not the sameas for the untransformed data.I6 T Michoelis-Menfen olono doto model with Plots leost-squores of thesecond-order fit(line) of fit.16b).3 . 13.

65 28. switch the variables.'C p. 14.0 9.Nlm2 -aO t 2A 0 40 B0 160 6 9 0 0 B1 0 0 9 3 0 0 1 0 .85 26.15 2'7.3r2 LINEAR REGRESSION ":.11( related ber of (kg) git Enrploy the ideal gas law pV .9 That is.1Using the same approachas was empioyed to derive Eqs. and that the standarddeviation is the same at every value of the dependentvariable. (f) variance. Note that for the 1aw.65 27. T.4 10. continued Lineariz on the f( the data.35 28.6 The following datawas gathered determine relato the tionship betweenpressureand temperature a fixedvolof unteof I kg of nitrogcn.35 27.-l Given the data regression fit a straightlineto to 13. Then repeatthe problem.5 9.65 26. (c) mode.55 29. x 0 .10 F (Eq.8 10. (b) median. " ':.-5.nonlinear regressionshould be employed to fit curvilinear data.65 27. 13.65 27. l3.4 11 . (f) variance.I must be expressed in kelvins. the Determinewhetherthis compasses is a valid estimatefor the data in this problem.8 10.Fit the following data with this model and display the result graphically. \) : a.65 2'/. best-fit line follows a Gaussiandistribution. (e) standarddeviation.75 13. 5 0 0 I 1 .^rep1r w(kgl A (m') Show t well.l Given the data f (hr) c (CFU/ 8. (h) Constructa histogram..I. Plot thedata and the regressionline.6 9.9 8.45 27.15)and (13.9 9.someanalystssuggest of that ratherthanusing linear transformations.65 28.nRT to deterrnine onthe rt basis of this data.but regress x versus}-that is. t0ill41720 876912 Determine (a) the mean.8s 28.5 9. (c) mode.l 10.'75 21. the lower and the upper values) that en68Vo<>f readings.2 7. de 13. Theseassumptionsare rarely true after transforming data.4 9.7 Beyond the examples in Fig.65 29.8 10.'.05 28.Interpretyour results.25 3 r .'i:-.6 x 2 4 6 7 y4565 8. (e) standarddeviation.65 28. 13. Use a rangefrom 7.2s Determine (a) the mean.and (g) coefficient of variation. computethe range (that is.6s 27.s5 28. (b) median. 13. l3 model: 28. As a consequence the last conclusion. of (i) Assuming that the distribution is normal.25 26. a best-fit curve is developed that directly minimizes the untransformed residuals. 13.8 9.1 y O.derivethe least-squales of the fit following model: \t-n1r'-lp Along with the slopeand intercept.0 9.5.5with intervalsof 0.65 28.Use a range from 26 to 32 with increments 0.16). FoI example.8 Fir I natural 13.4 9.5 Use least-squares 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. and that your estimateof the standard deviation is valid. 2 0 0 t 2 9 0 0 When t results parame (d'). lJ. (13.8 9. (d) range. 13.65 26..4 8.35 28.2 Construct a histogram from the data from Prob. E .2 t0.1 9. 3 10.and (g) coefficient of variation.computethe standardenor of the estimate and the correlation coefficient. (d) range.ll.5 l0. We will describehow this is done in Chap.EMS l3. The volumeis l0 mr.9 Tb area n is PROBI.5 to 11. there areother models that can be linearized usins transformations. .85 26. In this approach.U tion. ' 'i '". detelminethe slope that resultsin the least-squares fit for a straightline with a zero intercept.75 27.

3r3 13. cr+c' t a where c.9 concentration E.15 Developan M-file function to computedescriptive stafistics a vector of values. c 13. Measurements a nunto on ber individuals height 180 cm and dil-lerent of of weishts (kg) valuesof A (m2) in the lbllowing rable: -eive k 0. along with the 12fbr the untransfbrmed model. mean.16 Modify the lrnresr funcrionin Fig. /< : Krnaxc---.range.Have the function detennine for and displaynumberof values.version. 13.9.mode..17 Develop 2 an M-file function to fit a power model.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. it gencralc huvc . the base-IOversionto solve Prob. and predict what the the transfbrmed and untransformed equations along with the orrf.r.l.{h4 B mL) c(CFU/l0O ls9O I 320 t6 2A 24 900 650 560 x v 04 800 08 975 I'. 13.ays and dewith creases time.ingdata. Rather than using the base-e exponential nrodel 13.rrchoice of model should consistent the fact that negative concenrations be with ale inpossible that the bacteria concentrationah.1-1 an exponential modei to 4 .In addiUse develop formulation to relateut ro frs tion. Note that yor. a l3. Have the function return the best-fit coefficient a2 and powerB. standard deviation. and (b) usesthe sDbptor functionto alsodisplaya plot of W(kS)7A 75 77 BO 82 84 87 90 the resicluals (the predictedminLrs measured versus-r-.4 t5 53 25 76 4 B9 13." 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. the -r.8 a powermodel to the datafiom Table 13. 13.median. hasreportedthe datatabulated 13. and ftn.5 tt OB 2.rhisloglinr..jubp l ot function that to displaygraphsof both well.Test with When usedfbr curve iitting.Develop a plot of your fit along rvith the data.l3 so that it (a) computesand returnsthe standard error of the estimate.10 .2 I 500 1.r based follou.13. but the value of thL-exponent to (lr) parameter will diff'erfrom that estirnated with Eq.and coefficientof variation. Then use linear regression to estimate c.rpr ic fnr r Q5_Lo nersrrn data. and ftnra\are parameters.3.).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 l-1. e { E qI. 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 base-10 model: Plot the data and the equationon both standardand semilogarithmicgraphpaper.In addition.PROBLEMS this model and use it to estimatear and 6.. Use a transformation ro linearize this equation. In Show a power law A : oil' fits thesedata reasonablv addition. il wilhlhcrl.r Trrsl lromProb.22 (0.ll On average. to nafural The of 13.x and predict the growth rate at c :2 mg/L.tl.) 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. It c is known that such data can be modeled by the fbllowing equatiou: .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.r'ariiince.^a.the surface area A of human beings is rclated weight W and height H.usethe . this equatior'l yields identical results the base-r. but use logarithms perform the transformations.3 . . Evaluate the conslantsa and b.14 An investigator below fbr an experimentto determinethe growth rate of bacteria ft (per d) as a function of oxygen concentration (mg/L). c'oli bacteriain a swimnrins uea monil.

The bacteria allowed are to grow as fast as possiblefor the first 2. and E.- TL.3r4 LINEAR REGRESSION (sl& are shown beloll.. -. fbr the following on bike-lanewidths and average distance between bikes and kineticmodel: passingcars. 3 2 1 I B 2 7 1 .The folas lowing data is collected: '4: dt r. 52 9 1 5 .4< t0 550 The stres force in r value car the mast mast's le estimate 13. ICells].20 lt is known that the tensile strengthof a plastic increases a tunction of the time it is heat treated. Add this line to the plot.L^ f:r^ ^f rL. (b) Repeat tl.l^.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.historicalrecordso1'such flow data are difficult to obtain. determine cor.B:.. Model A Model B l3. 13.V irA (a) On cur an{ (b) Re ^^l^. N/<m2 (a) Fit a straight line to this data and use the equation to h0t23 determinethe tensilestrengthat a time of 32 min.5 l2 312 l o n e W i d r hm 2 9 2 .65cn alloy.5 hours..the sizing of reserxA*.67 93 33 I 35 O0B5 I48 Bq 3I5. Flow.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.and p is the specrfic growth rateof the bacteriaduring exponential growth. . to the points for the datawas takenat l5 time 13. responding polyrnerization reaction: 11.26 "t) 10.Which model bestdescribes data the tistically)?Explain your chorce. voirs dependson accurateestimatesof water flow in the We assumethe reaction occurs via a complex mechanism river that is being impounded.andthen they are inducedto producea recombinant protein.22 Concentration minimum lanewidth. 1 2 .m 24l524IB1. 13.N.meteorolo-vical 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...82..For some rivers.estimatethe specificgrowth rate of thebacteria duling the tirst 2 hoursof growth and during thenext4 houm of erowth. (b) Fit a straightline to the data with linearregression.---:.'J.. Use the data to determinethe dctL'rmine properdesignof bike lanes.s/m2 26.Severalmodelshavebeenhypoth. (a) Plot (b) Fit r Supt (c) Use the 1 (d) rf th of th ratio use. The theoretical growth of bacteriacan be described by Use regression determinea nrodel to predict c as a functo tion of I.-. In esized.' € Viscosiry p. Te tween stl Slroin..-1.1. Basd on tbe data.21 The following data was taken from a stirred tank recngineeringstudy was conductedto 13.The clatafrorn I I streets is dA .: AT A0le A Time. After taking the log of the data. (a) Plot the clata. ^f rli^ .24 A transportation actor for the reactionA .B.56 0 012 OOoO/5 ModelC i00 determir This rela when or followin Procipifqtion.Datawasgathered the best possible estimatesfor ft.use linear regression find the equationof to the line thrt bestfits the dataand the r2 value.27 Tl that mee voltages V.25 In water-resourccs engineering. (c) If the minimum safeaverage distance between bikes and passingcars is considered be 2 m. pro the duction of which slows the bacterial growth significantly..ltl The tbllowing data shows the relationshipbetween the viscosityof SAE 70 oil and temperature. T e m p e r o t u r e . 13. Dislonce. long-term consistingof many steps.A. cm/cm Stress.1^r^J f^- .00198kcal/mol/K.- '-. m3/s s" Number of Model Porometers Fif 135 2 r05 3 13.19 You perform experimentsand determinethe fbllowing valuesoi heat capacityc at varioustemperatures for a Z gas: T50-30060 l25O I 280 c I 350 I480 90 I 580 ilO l70A 13.tB . where R is the gas constantand equals0.23 Below is data taken from a batch reactorofbactenal growth (after lag phasewas over).and the sum of the squares the residualshad been contrast.

The resulting data is listed below. Employ an exponential model and linear regression make this prediction.65 and is constructedof an experimentalaluminum Tests allo1. use the data to If estimate deflectionof a 9-m mast. whereF : by in force themast and /. mls 0.519 I 3A7 I 002 O7 9 7 5 A. T"Elongotion 75 86 87 lO ll3 475 600 127 153 (a)Plotthedata. the is (d)If rhe drarnage areais I 100km:.29 The populationp of a small community on the outskirts of a city grows rapidly over a 2O-year 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.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.000 N. estirnate what fraction of theprecipitationis lost via processes such as evaporation. the .A 705r0203040 p I 7B7 I . and D and B are parameters. N/cm2 4970 5170 . and consumptive use. data of ?ncipit o t i o nc m B B9 l 0 B 5 1 0 4 I . Predict the 7o following is availabletbr a river that is to be dammed: elongationfor a temperature 400 'C.5 V. Al. Superimpose line on your plot.30 The velocity r of air flowing past a flat surface is measured severaldistances away from the surface. 4 : (K). this (c)Use besffit line to predict the annual water flow if the precipitation 120cnr. to 13.PROBLEMS 315 determine a relationshipbetween flow and precipitation. J. deep groundwaterinfiltration.6529 2 52 3 7B 4 lo7 5 t3 7la 1 93 27. the The 13.31 Andrade'secluation has beenproposedas a model of the elfect of temperatureon viscosity: P: PsB/r" where p : dynamic viscosity of water ( l0-3 N's/m2).5 (a)0n thebasisof a linear regression this data.h u.27 following data was taken from an experiment measured that the cunent in a wire for various imposed voltages: 13.002 A 2B7 0006 0 899 o0l2 r9r5 00rB 3048 oo24 4299 thestress caused wind can be computedasF/A. Plot the line and the data for and evaluate fit.26 mast of a sailboat has a cross-sectional area of cmr 10. : mast'. you must forecast the population -5years into the future in order to anticipate the demand for power. were performed to define the relationshipbetween stress strain. 13. where L : the mast's length. the wind force is 25. The perature. lbw m3/s 1 4 6 1 6 7 1 53 1 3 9 .: strain x l.This value thenbe substituted can into Hooke's law to determine mast's the deflection. The 13. the (b)Redo regressionand force the interceptto be zero. a at l Fit curve to this data assumingthat the velocity is zero at the surface(y : 0). 0) Fit a sffaight line to the data with linear regression.5500 3590 6900 t24A As an engineer working for a utility company. 71 2 7 9 4 r 1 6 B 9 9 1 232 195 )61 II I 16. absolutetemperature Fit this model to the following data for water: v.6 oC 200 25a 300 375 425 Temperofure. Use your resultto determinethe shearstress (du/dy) at the surface.determine of cunent a voltage of 3.The test resultsare and 11.v i. cross-sectional area.

will then show how suchapplications be generalized applied a We can and to Finally. Some data. Knowing how to implementpolynomial regression. a procedurewas developedto derive the equationof a straightline using the least-squares criterion. or Understanding how to implementnonlinearregression with optimization techniques. although exhibiting a marked pattern such as seen in Fig.lrt. 13. For example. we will illustratehow optimizationtechniques be broadergroup of problerns. one method to acconrplish in this objective is to use transformations. GenerolLineorLeost-Squores 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.acurvewouldbebetter suited to fit the data.Forthesecases.I POTYNOMIAL REGRESSION In Chap. I4. procedurecan be readily extendedto fit the data to a higher-order The least-squares polynomial. Another alternativeis to fit polynomialsto the datausingpolynotnial regression. can usedto implementnonlinearregression.As discussed Chap.suppose polynomial or quadratic: that we fit a second-order . Specilicobjectives and topicscoveredare o o o o ." l rol-.l.y:uotal.ispoorlyrepresentedbyastraightline.r+ a z r :+ e (14.f.1) .. Understanding formulation of the generallinear least-squares the model. Knowing how to implementmultiple linear regression. Understanding how the generallinear least-squares model can be solvedwith MATLAB usingeitherthe normalequations left division.13.

a1x1 .orri)t \14. 2l \-\r .u7.xi ()l -L r a2xl) \/ rtu.2) with respectto coefficientsof the polynomial.'t. d0t : -/' .\'.2) fit.us.d0t aS.".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+ (I-t. rt .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 leost-squores (o)Doto is ill-suited lineor ihot preferoble. is of For this casethe sum of the squares the residuals II s.) : . . as in eachof the unknown as.?) oo+ (T. .\-r ) \. -c'ltt -a.-t^sa ) .REGRESSION POLYNOMIAL 317 (b) I4 FIGURE . -t'1*. : t i-l (y.a0 .nu. the To generate least-squares we take the derivativeof Eq.t) n' + (t ti) a2: lxiYi (Ir.atxi . :-: at . (14.1)"' + (I-'1) tL2: lx!)'i .d1x.

20).1.47 1 4 00 3 3.l4. (13. is formulatedas s. The following can be computedfrom the data: \'-..1 EXAMPLE Polynomiol Regression polynomial thedatain thefirst twocolumns to ProblemStotement..l l s L Lx!:9tg I".wecan The foregoinganalysiscan be easily extended this more generalcase.(m -l1) (14.r< /r ^t r J n:6 .-aS)r 0. Note that the precedingthreeequations are can linear and have three unknowns:a0. The coefficientsof the unknowns be calculateddirectly from the observed data..12 239.9 6t t 152.-r< r-)5477 I l'i : 152. ctt. for of leost-sguores Compuiotions on erroronolysis the quodrotic v.x'' I e Thus. a a.we see that the problem of determininga least-squares second-order polynomial is equivalentto solving a systemof three simultaneous The linear equations... anda2.6 \-'l-ss \ '-..1) 2 5 r 33 9 Soluiion. .'t J .6 544 44 314.)t :585'6 L*?Y.we have lost z * I degrees freedom. an-were usedto computeSr.ao-arx. (Ji -J)- tg. to polynomial is equivalent to recognize that determining the coefficients of an mth-order enot For this case. Fit a second-order ofTable 14.t 4332 r 00286 r 08r60 0 80487 9 0 61 59 0 09434 3.the standard solving a systemof m I I simultaneous linear equations. of fot In addition to the standarderror.: 2488'8 . two-dimensional to casecan be easily extended an mth-orderpolynomial as in + ! : aotdlx f a2x2 .22 1272.(m I l) because * l) data-derived o0.SQUARES NONLINEAR REGRESSION where all summationsare from i : I through n. at.3) (m coefficientsThis quantity is divided by n . n . For this case. . thus. TABTEl4.l xi fif.74657 0 I 2 j? 4 5 I 2l 77 t3..6 272 40. a coefficient of determination can also be computed polynomialregression with Eq.318 AND GENERAL LINEAR TEAST.

>> r = 1152.41 B6 2.i\r Therefore. using MMLAB: >> N = l6 15 55.l:hfx 2 .6 535.: / _1.74651 :0.3)l | 1.99851 2513.39 3.55 225 9j9). the least-squaresquadratic equation for this case is ! : 2 . is FIGURE4. 4 1 8 6* 2 .. 3 5 9 3 r * 1 ..6 24BB. tr iil'i.99925. /+b) / :lll75 !o-rz+ l) ""'- The coefficientof determination is r- 2513. (14.the simultaneous linear equations are 319 'i.]4.3593 1.39 and the comelation coefficient r :0.Bl. 1 POIYNOMIAL REGRESSION Therefore.8507 These equations can be solved to evaluate the coefficients. 8 6 0 7 1 2 The standard error of the estimate based on the regression polynomial is tEq. For example.2 I Filof o second orderpolvnomiol . >> a = l..15 55 225.

. .2.rt. asin 1 l .3 Grophicol depictionmultiple regression y iso lineor of lineor where function..2 'T4UITIPIE TINEAR REGRESSION Another useful extensionof linearregression the casewheref is a linear functionof two is variables.: OQ) -2 ^ \-- ) .ao-(r-tl.cttxt. i l 1 as.i .t t 1 1 .i()i .i)? EXA (r4.: d L ll 11 ) xt.i .0"i .1) and differentiatingwith respectto eachof the unknown coefficients: aS.i * azxz.2^ . an fit. or more independent For example.r2.:.the "best" values of the coefficientsare determined by formulating the sum of the squares the residuals: of $ : i ()'i .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.r.ond of rr. For this two-dimensional case.az. _-__---!-_ // .azxz. might be a linear function of r. 14.-:-l) dao _\-r.320 LINEAR GENERAL LEAST-SAUARES NONLINEAR AND REGRESSION These results indicate that 99.ao .i) FIGURE I4. regression a As with the previous cases. 14.This resultsupports conclusion the that the quadratic equation represents excellent as is alsoevidentfrom Fig. x 1 . . andxr. y : r r o { c t 1 .851 percent of the original uncertaintyhas beenexplainedby the model.o 2 x 2 .- as.(t0- a1xl. 14.t 1 1 .i) .. ( l ' i . the "line" becomes "plane"(Fig.3).

3r2'. 2 s4 8 | I 48 54) L t4 which can be solvedfor (10:5 at:4 a0 Al A1 54 243.2. The foregoing two-dimensionalcasecan be easily extendedto m dimensions. to Solution.. The summationsrequired to develop Eq.2 Lineor Regression l . " + a r x ^ * e TABTE I o:? c:Tp':olio1'...5) Problem Sfotement. I"ti.r Ir.rilr 0 20 225 0 l2 t89 )77- x2y 0 t0 IB 0 ]B \.25 I t6 49 . Substituting them into Eq. i r z . The following data was calculated from the equation y : 5 + 4x1.5)gives 16.rt x2 00s 2 2. Irr.?. .525 r30 463 7227 I t0 Use multiple linear regression fit this data.5 rll [ 6 1 6 .r: (14. I .rr.6) aZ:-3 which is consistent with the original equationfrom which the datawas derived.. (14..5 100 (14.:'i':d dey3lon '::::l fo1 Elomele 1o li:.1nt EXAMPLE Multiple 14.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.rt.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*'.:"".l) xl 0 l 4 I 36 4 J4 xrx2 .' I x r .. Ifoo f n I"'t.. iI l a r I [ L I tr.5) are computedin Table 14.4 o 2 5 3 24 l4 0) T6 lao ./a. ::q. 5 1 6 .oti:. (1a..as in * ) : a oI a 1 x 1 a 2 x 2 + . _..I4.

322 LINEAR LEAST-SQUARES NONLINEAR AND REGRESSION GENERAL error is formulatedas where the standard s n-(nt+D is and the coefficientof determination computedas in Eq. zl. x::i... Znt:. 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. r : x . I4. Further. .s: l.?) linear It where :0.xm. : : l .' . z...rl. 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 .t2= fall within this model-that and multiple linear regression if polynomialregression alsoincluded the basisfunctionsate is x2.:. ( 13. In the preceding pa-qes. can easilybe seenhow simple is.the :'s can be y : ao * a1 cos(a.3 LEAST SQUARES TINEAR GENERAT we have introduced three types of regression:simple linear. Z p 11 6 m .t . wherea variableis linearly relatedto two or mole be Aithough therernary certaincases has other variables... . and multiple linear.zm € I (14.l .7). zr :.. the a's. . As in the caseof sinusoids." data. equationis transformed taking its logarithm to yield l o g .For example. . . in as selvescan be highly nonlinear. . .8) EXA values valuesof the basisfunctionsat the measured where[Zl is a matrix of the calculated variables: of the independent zll t7l t-t- :*t-f .To usemultiplelineat Are Suchequations extremelyusefulwhen fitting experimental by the regression. In fact. a simple-lookingmodel suchas ): do(l e-ol') into the format of Eq. it is truly nonlinearbecause cannotbe manipulated in Equation(14.multiple linear regression additionalutility in the derivationof power equations the generalform of x. .7) canbe expressed matrix notationas [y]:fzl{a}+lel (14. all three belong to the following general model: least-squares -l''' ) ' : a o z oI a r z t * a y Z z I an. linear polynomial.. functions the them' parameters-that is. On the other hand. ( 14.marem * I basisfunctions. [::: a. t t : x 2 .t )' : ctox'.20).r) I azsin(o_tx) Such a format is the basisof Fourier analysis.

The column vector {y} containsthe observed valuesofthe dependent variable: {-v}r : L.14. can be expressed vector form as in {y} . First. Becattse > m * 1. you shouldrecognize n that most of the time. i-l / -lo1r1. Solution. and S.ti t2 where! : the predictionof the least-squares The residualsbetweenthe best-fit curve fit.j. . 1 1 3 . and multiple linear regression.) 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. 2 4 A .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.0 ) It can be shownthat Eq.-urt' I().:Ilt.equivalenf the normalequations to developed previouslyfor simple linear. . in fact. Recallthat r: is definedas t S.vr !2.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. as EXAMPLE I4. .y.polynomial. and the data.1. -}nl The column vector ia] containsthe unknown coefficients: {a)r : la11 at a .9) \ This quantity can be minimized by taking its partial derivativewith respectto eachof the coefficientsand settingthe resultingequationequalto zero.$ | _ _S ..l j=tt / . - s. 9 6 1 . usematrixoperations described this but as in section. > > y = 1 2 . Substitutingthe definitionsof S.r . (14. gives r--l- I(vr .-S.l0) is.enterthe datato be fit >> x .t0 12 3 4 5l'. [Z] is not a square marrix. \2 (14. 1 1 ' .3 GENERAL LINEAR LEAST SAUARES 323 where m is the number of variablesin the model and n is the number of data points. 6 2 ' 7. Repeat Example14. The coefficientof determination and the standard error can alsobe formulatedin terms of matrix algebra.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. I 1 .

5.'t 466 Then r2 can be computed as > > 1 2 = I . ^ 2 ) EX 3 .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.SQUARESAND NONI\NEAR RIGRESS\ON GENERAT Next.Z * a ) . can be computed as >> syx sqrt (Sr/ (lenqth (x) -lenqth (a) ) ) 1. It also sets the stage for the next section where we will gain some insights into the preferred strategies for solving Eq.10). The matrix notation will also have relevance when we tum to nonlinear regression in Section 14..47 B6 2. create the [Z] matnx: >> Z = [ones(slze(x) ) x x.S r / s u m( ( y .I .998s and sr.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.8607 In order to compute 12 and s"7-. ^ 2 ) 0.7.m e a n ( y ) ) .324 L\NEARIEASI.^2] 100 111 1. (1a. We can solvefor the in quadraticby implementingEq. > > S r = s u m( ( y .3593 1. . first compute the sum of the squares of the residuals: . (14.l0): coefficientsof the least-squares >> a = \Z'*Z) \(Z'*y) 2 .

QRfactorization andsingular valuedecomposition. I 1 .41 B6 The sameresult can also be calculated using the backslash: >>a=Z\y 2 . are more robustin this regard. the built-in polyf it function automatically usesQR factorizationto obtain its results.3593 1 8607 As just stated.> m).2). 1 1 3 . howcan to ever. 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 .QR factorizationis automaticallyusedin two simple ways within MATLAB. 1 y y: l Z l { a l ( 1 4I. 9 6 I . .2) 1.4.4 QR FACTORIZATION AND THEBACKSIASH OPERATOR is Generating bestfit by solving the normal equations widely usedand certainlyadequate a It for many curve-fittingapplications engineering in and science. (14. ( 14..3.tlrat the normal equations be ill-conditionedandhencesensitive roundoff errors. we mention them herebecause they can be implementedwith MATLAB.V.I4.Although the descriptionof thesemethodsis beyondthe scope of this text.4 AND OPERATOR QR FACTORIZATION THEBACKSLASH 32s 14. generallinear least-squares the Recallthat the general modelis formulated Eq. problemcan be directly solvedwith the backSecond. it . 6 2 1 .but use the builrin polyf ir functionand left division to calculatethe coefficients.8)as derivedfiom general the is When the number of unknowns (n. y i j t o n d L e f tD i v i s i o n P l n Problem Stotemeni. MATLAB senses you want to solvesuchsystems with left division. to QR factorization obtainthe solution. both these results are obtained autornatically with QR factorization..8) as slashoperator. 2 4 A .the datacan be enteredand usedto createthe lZl matrix as in >> x = >> y = >> z = The potyf 3 4 5l'. As in Example 14.The following exampleillustrates E X A M P L1 4 . Ieastsquares.41 86 2. RepeatExample 14.we usedleft divisionwith thebackslash operator solvesystems linearalto of gebraicequations wherethe numberof equations equalsthe numberof unknowns(n : n). I l ' [ones (size (x)) x x. for caseswhere you want to fit a polynomial. Two more advanced methods. numberof equations greaterthan For Eq.it automatically uses that how this is done. Solution. First.Such systemsare said to be overdetermined. must be mentioned. lA I2 1 2. l ) In Section10.3593 2.8607 2 .3. Further. function can be used to compute the coefficients: >> a = polyf it \x.

but use nonlinear regression. f va7 = that the value of the function at the minimum. tails on this approachare providedelsewhere An alternative to useoptimizationtechniques directly determine least-squares is to the fit.7).we fit the power model to datafrom Table l3. (la. etc.l by linearizationusing logarithms. Recallthat in Example13. De(Chapraand Canale.In the presentcontext. x0 : a vector of the initial guesses the parafor meters. (14. example. and pl.If you would like to passadditional for (p1.326 LEAST-SAUARES NONLINEAR AND GENERAL LINEAR REGRESSION I4.the Gauss-Newtonmethod uses a Taylor seriesexpansionto expressthe original nonlinear linear form.. .2002). but do not want to setthe aptions.MATLAB uses to default valuesthat are reasonable most problems.7.2741ut Repeat this exercise. Then least-squares equationin an approximate.a t r ' 1 1 2 (t4.2 ) 1 This equationcannotbe manipulatedso that it conformsto the generalform of Eq.1.a 1 ) : )- [t.options: a structure containingvaluesof the optimizationparameters created as with the optimset function (recall Section6. previously Sec.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. -v:ao(l -r-at"r)*e ( 1 4 . p2. useempty brackets[] asa arguments place holder. For example. There are techniquesexpresslydesignedfor nonlinearregression.. As with linear least squares.pl.l2) can be expressed an objectivefunction to computethe sum as of the squares: n f ( a q .) wherex = a vector of the valuesof the parameters minimize the function f un.Eq.4.3. options.This yielded the model: e\a2 F : 0. fvaLl = fmlnsearch(fun.5 NONTINEAR REGRESSION There are many casesin engineeringand sciencewhere nonlinearmodels must be fit to data... nonlinearregression basedon determiningthe values is of the parameters that minimize the sum of the squares the residuals. Note that if options is omitted.r3) An optimizationroutine can then be usedto determinethe valuesof ae and a1 that minimize the function. xa. p2.a o \ .4). theory can be usedto obtain new estimates the parameters of that move in the directionof minimizing the residual.It has the generalsyntax lx..for the of nonlinearcase.the solution must proceedin an iterativefashion. For example.).Employ initial guesses I for the of coefficients. .p2. .: additionalarguments that are passed the objectivefunction.MATLAB's fminsearch functioncanbe used As described in for this purpose. EXAMPLE 4. ..thesemodelsaredefinedas thosethat have a nonlineardepenFor denceon their oarameters.However.

^a(2). the datacan be enteredas >> x = t10 20 30 40 50 60 7O B0l. an M-file function must be createdto computethe sum of the squares.125 7A 380 550 610 1220 830 14501. >> y .5 NONLINEAR REGRESSION 327 Solution.5384u1 Both the original transformedfit and the presentversion are displayedi-li'Frg. f = s u m (( y m .I4.4. ^ 2 ) . The minimization of the function is then implemented by >> fminsearch (@fSSR.ym) yp = a(1)*xm.4359 tL x. called f ssR. This model using nonlinearregression versuslinear regression is because former minimizes the residualsof the orisinal datawhereas latter minithe the data. This example illustrateshow different best-fit equations result when fitting the same employingtransformations.[1. In commandmode. m/s . mizes the residualsof the transformed FIGURE I4. y) The besrfit model is therefore a35e F : 2. Note that althoughthe model coefficientsarevery different.xm. 1] . 2.14.it is difficult to judge which fit is superiorbasedon inspectionof the plot. m. z { 800 Untransformed 4 40 u.5384 r.is set up for the power equation: function f = fSSR(a. The following file.y p ) .4 versus velocity from ond model for dcto Comporisontronsformed unlronsformed fits force of ODIE J a . First.a a a ta .

specifies the height of the F I G U R EI 4 . f ol (r e.sin{arol).Thesummotion thethree curvein {o).5 ond B.As might be expected.where : 0. The mean valued sets the average height above the abscissa. 14. C . 7 . fad . : -0 866.the results will be identical.328 LINEAR LEAST-SAUARES NONLINEAR AND REGRESSION GENERAL F IT T INS IN U S OIDs G Bockground. 5 .5a). of curves (b)yields single the {b). Engineersand scientistsoften deal with systemsthat oscillateor vibrate.which is expressedgenerally as * f (t) : As * Cv cos(a-rst d) (14. = ) . will usethe term sinusoidto represent we any waveformthatcanh describedas a sine or cosine.sinusoidalfunctions play a fundamentalrole in modelingsuch problem contexts.14) Thus. For this chapter.we will use the cosine.There is no clear-cut convention for choosing either function. In this discussion. cl t. three I(r) : Ao + A) + A. four parametersserve to characterizethe sinusoid (Fig. ao: 2n/l 1.The amplitude C. and in any case.ond 0 : r/3 rodions(b)An olternotive expression thesome of curve is in The components thisfunction depicted of ore * B.5 s). { 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 .cos{coor} in A.s at. c o s ( a r o r * 9 ) F o r t h i s c o sA n = 1 .

or phase shift 0 parameterizesthe extent to which the sinusoid is shifted horizontally.I4.r7) Dividing the two partsof Eq.r6) Although Eq.Eq.7). 14.sin(cosr) Cr cos(abl + d) : C1[cos(rr-rst) SubstitutingEq. This deficiency can be overcome by invoking the trigonometric identity: sin(p)] cos(p).3 AM 27.8 ) (r4. 2 3 6 . This means that it is not in the form of the general linear least-squares model (Eq. 1 23.4characterizes phase angle.J lB9 F 2 1 .18)represents alternative four parametersbut that is cast in the format of a general linear model (Eq. Squaringand summingthe two partsof Eq. (14. Finally.f and frequency in turn is related to period I (in units of time) by I (14.19) ( 1 4l.19) leadsto (14.19)gives / B'\ 0 : arctan| --| \ Arl (14.14)that still requires Thus. Aizona.15) r-T (14. It can be measuredas the distancein radiansfrom / : 0 to the point at which the cosine function begins a new cycle.14) is an adequatemathematicalcharacterizationof a sinusoid.2r) an formulationof Eq. U.8 A J 3 7 . (14.3 r8. ( 1a. Thus. (14.14)and collectingtermsgives (Fig. add z to 9. the oscillation.17)into Eq.I S 1. (14.5b) * f (t) : Ao * Ar cos(a-lol) 81 sin(arer) where Ar : Ct cos(0) Br : -Cr sin(9) (14.7). it fit. The angular frequency (in radians/time) is related to frequency/(in cycles/time) by oo :2r. (14. it is awkward to work with from the standpoint of curve fitting becausethe phase shift is included in the argument of the cosine function.9 .4 23.1 ^ 32.6 CASE STUDY 329 continued how often the cycles occur. 4.2 OND 2e. The angular frequency o. can be simply applied as the basis for a least-squares The average monthly maximum air temperaturesfor Tucson. and we must use nonlinear regressionto estimate the coefficients. have been tabulated as Month T'"C .20) where if A r < 0. (14.

Equations (14.8972 -lengch(a) ) ) A plot can be developed as >> tp=[0:360]. BA02 The statistics can also be determined as >> Sr=sum((T-Z*a) .4398 >> r2=I-Sr / s u m( ( T . fit a sinusoid to this data.3 27. r=118. tp. Assuming each month is 30 days long.3.2s59 . lZl matrix: >> >> >> >> vrO=2*pi/360.14): >> theta=at. Solution.r6'76 .2 31 . which is displayed in Fig.an2 (-a (3 ) .6.T.Tp) The fit.9862 >> syx=sqrt (Sr/ (length(t) syx = 0. The coefficients for the least-squares fit can then be computed as >> a= lZ'*Z ) \ (Z'*T) 28.21) can be used to express the best-fit sinusoid in themore descriptive format of Eq.381 B -9 .4 z= [ones(size(t)) cos(w0*L) sin(w0*t)].20) and (14.2 36.9 2r.r 23. 34. generally describes the data trends.'o'. Use the resulting equation to predict the value in mid-July.3 18.8 32.2 .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. ^ 2 ) 0. >> Tp=a (1)+a (2 ) *cos (w0*tp)+a (3 ) *sin (w0*tp) .^2) 6 . (14. In a similar fashion to Example 14.1. t=t15 45 75 105 135 165 225 255 285 315 3451'.4 29.91'. >> plot (t. a (2J ) *360 i (2*pi ) theta = L63. 14.m e a n ( T ) ) .330 G E N E R AL I N E A R E A S T . we can enter the data and createthe 23.

6 Leost-squoreso sinusoid temperoture forTucson.0530 .7 to 38.CASESTUDY 331 continued 15 300 350 FIGURE I4. the maximum monthly temperaturein Tucson rangesfrom about 18. the phase shift is expressed in days rather than in radians. fitof to doto Arizono >> C1=sert (a(2 ) ^2+a (3) ^2) 9 . 1 6 7|6 ) \Jbu / We can see that the mean is 28. 6 7 0 2 c o|s* . and the amplitude is 9.3878. The value in mid-July (/ : 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.1 'C.t r + 1 6 3 . which translates to about day 197 (July 17). Hence. the final model is /2n \ R : 2 8 . The phaseshift tells us that the peak temperatureoccurs about 163 days prior to the start of the year. 3 8 7 8+ 9 .6702.67 A2 Note that because it is more physically meaningful. Therefore.

9 It3 r0 t 903 B.8 Use multiple linear regression fit to xrO1)223344 I x2 0 2 y 151 t7. Explain possible causes fot the discrepancy.9 Thc lbltowing datawas collectedfbr the steady of flow u a(erin a concretc cilcularpipe: Experiment Diomeler.a Dissolved Oxygen (mglt) for Iemperolure ('C) ond Concenlrotion of Chloride (g/t) T.16).and S: slope. m3/s .use polynornialrellession to derive a predictive equation for dissolved oxygen as lor concentration a function of temperature the casewhere the chlorideconcentration equalto zero.a11D"' 5"' where Q : flow. 1 . Test it by solving Prob. of TABTE P14. 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) *. 14.9 03 06 O9 0.4 Develop an M-file to implement polvnomial regression.14. t 2 + e That is. Computethepercent relative error for vour prediction.squares approachto fit tlis model to the data in TablePl4. and the conelation coefticient.1 Fit a parabolato the data lrom Table 13. Passthe M-file two vectorsholding the -r and ) values along with the desiredorder rn. Computethe coefTicients.6 Along with the coefficients. determinethe coefficientsthat reslllts in the leastpolynomial with a zero intersquares for a second-order fit cept. Test the approach by using it 1(Jfit the data fiom T a b l e1 3 .1.1 and fi I 1 2 2 2 I 256 205 35 I 297 454442 User follor .5. a third-orderpolynomial in temperature a linear and relationshipin chloride is assumed yield superior to results. 14.2 9 28 t) 3B )2 4.332 G E N E R AL I N E A R E A S T . 14. 14.1.Determine the r2 for the fit and comment on the efficacy of the result. C C C t l t( 5( 45 2B t3 r5 20 25 0r 909 B.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.5. Compute the percentrelativeenorfor your prediction. "C 0 5 t0 c:OglL c=lOg/L c=20g/L l 2 3 4 5 6 7 B I 03 06 0. standard the error of the estimate. t l i r+ a 2 .3 195 566 14.09 mg/L./r(c) Estim and C t p(t) 14.determinerr and -s..09 rng/L. m Slope.rr P. Use the general linear least-.3 06 09 0 00t 0 001 0 00r 001 00r 0 0t 005 005 005 aa4 4.r34 16 J 36 578 44 34 2.Employ a polynois mial that is of sufficiently high order that the predictions match the numberof significantdigits displayedin the table.5 Dissolvedoxygen concentrotionin woier os o function of lemoerofure ("C) ond chloride concentrotion(g/L). That is. b" r5( P9( 14.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 . 14. Use the resulting equadon to estimate dissolvedoxygenconcentration a chloride the for concentrationof 15 g/L aI T : 12 'C. 14. and Use the equationto estimatethe concentration dissolved of oxygenfora chloride concentration 15 glL atT : 12"C.3 Fit a cubic polynornial the following data: to Note that the true value is 9.e t27 where maxin radiati UtE n Iru.26 12.6 Use multiple linear regressionto derive a predictive equationfbr dissolvedoxygen concentration a function of as temperature chloridebasedon the dataliom TableP14.3. teriz exan U( wher subs The lsl.5. lzl. Note that thetrue valuc is 9.ll) Three disease-canyingorganisms decay exponentially in scawater accordingto the following model: .. solar r . D : diameter. 14.derive the least-squares of the fit following model: _ \ ' : .15)and (13. m/m Flow.6.5 For the data from Table P14.5 and 14. (13.2 Using the same approachas was employed to derive Eqs.7_.rU.7 As comparedwith the rnodelsfiom Probs.24 069 0 13 a82 238 r 0. 14. l4.

For (b) and (c).PROBLEMS rii . P.9 7 1.4 9 Ll . 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.Test thc approachby using it to fil the data fron.5 "C. Determinethc bestequationto predict the amouut. t O 5 cc r 005 0l 0-\ rc 50 r00 .8 we used transtbrmations linearrze 106 fit and thefollowing moclel: )' B ' .jLrstify.7 the E$imate initial concentrationof cach organisrn(A.)75 0 6529 lsl.15 The following datarepresents bacterialgrorvthin the a liquid cultureover of numberof days: Doy Amounl x to l{.. 7 3 7x l O ' 2423v.. B..r T a b l e1 3 .5 (a) Plot this data. a2 04 06 oe r3 r5 t7 IB 14..f': l]1. 14..\:lazx2+e. Display the data and the rnodelfit on a grapn..M uo.. s / m 2 . IS]3 ''-K+[. Usc nonlinear rcgression evaluate and to P..ll Thefollowing model is uscdto represent efl'cctol the plants: r fadiation the photosynthesis of aquatic on ratc solar I P=P.13 Enzymaticrcactionsarc usetl extcusively to characterize biologicallyrnediatedreactions.here : the initial rate of the reaction (M/s). employ transfbnnations to Iincarizc data. (b) a usc least-squares to (c) power equation. where: the photosynthesis 1mg m 3d-l). T substrate Ihefollowingdata can be tit with this modcl: lL 0 5 i.n1't g'. I3..12In Prob. a saturation-growth-rate equirtion. lpEm based the fbllowing data: 1..Plot the dataalongwith all the curves. bacteof riu atier 30 days.r a 4 B t2 t6 20 67 38 7467 8274 9l69 rot 6C l]2 58 Find a best-flt equation to the data trend. [S] : tne u0 concentration(M).*t I . PV At At .-1.t 4 2 5 1. Ii.).. 1 4 .16 Derive the lcast-squares ol the fbllowing model: flt 1 4 . and (d) a parabola.14 Givcn thc data I i I t l4. : 82.ii 6 1. (b) Use linearintcrpolatior.The following is rn of reactions: example a model that is usedto fit sr-rch k. 1 : solar (lrE radiation nr 2s l).. .r predictp at T :7 .595x la r 6C)63 l0 3 5ZBBxlOr' 1 .logarithmic. 1 . M/s 6 O Z Bx l O ') 7.Is the arly one of fhe curve-s superior?If so. 1 3 1.r basedon the possibilities-polynomial. \ A (a) Use a transfbrnration linearizethe nrodeland evaluate to thc paramcters. nonlinear to Use data..)a5 2430xlC' 2431 x la' 2 ..tll . following Developa plot ol your fit along with thc da1a. 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 .u.18 Use the following setof pressure-volume to find data (A. . determinethe coefficientsthat resultsin the leasl squares tit tbr a second-orderpolyuomial with a zero intercept. Tr)r several regression estimateaa arrdp.05rnL atm/gnlol K. 14. the bestpossiblcvirial constants and Ar) fbr the following equation state. order lo make the si. I.787 I5t9 t0 I3Q7 20 I002 30 4a a 7. : the P rate photosynthcsis maximum ratc (mg m-id l. That is. C) and siventhe followins mcaslrremcnts: r f\t 05 l AA '2 )-) 3 . and exponential.and K are paramcters. (b) Pcrforrn sanre thc evaluation in (a) bul usenonlinear as regrcsslon.. and ft. i s l c o I('C)in the ftrllowinglranner: latecl telnperaturc to u./: optimal solar radiation rs-l).rme 14. and 1. to (c) Usepolynornialregression lit a parabola thc data in to to prediction. and I : of R -r03 K. 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.

Use r 5 0 0 0 these l.rggested following equation to model this the relationshio: .o 7. has been tabulated as Time. 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.km/h Thinking.m 30 56 5.l9 Environmentalscientistsand engineersdealing with thc impacts of acid rain must detcrmine the value of the ion productof water K.m B r o k i n g . Arizona. as a lunction of temperature.l o 9 1 6K . 1 6 4x 2950x 6846x 1467x 2 . . 9 2 9x t0 : l0 r5 l0-r5 t0-' t0. I d ll. and d are where I.l. Use the resulting equationto predict the radiatron rn mid-August. b.0 45 8. eachof which is a function of its speed. 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. a Speed. I cT.99t BS 79 7 (K).1. Developbest-fitequaequationsto estimatethe total stoppingdistance a for car travclingat ll0 km/h.204 I 363 rB000 163t tions fbr both the thinking and braking components.22The solar radiation fbr Tucson. fit a sinusoidto this data.. I . and time of maximumpH. c. fime. h 0 2 4 5 7 I l2 t5 2A 2224 pH 76 72 7 6..The fbllowing cxperimentaldata was Assumingeach month is 30 days long. 9 4 76 t2A 224 847 * D log. P (orm) V (mt) O 9B5 2saoo I t0B 22.5 l2 3 60 75 90 ltt 145 )67 2 l . Use least-squares regression fit a sinusoidto the following to data. Use your fit to determinethe mean. Scientists have sr. amplitude. : absolutetemperature parameters.5 72 8. o 3 2 . and a.2l The pH in a reactor varies over the courseof a day. mo F OND I Rodiotion.5 7.334 GENERAL LINEAR LEAST-SAUARES NONLINEAR AND REGRESSION collectedto quantify this relationship.' 1..

Table 15. YOU'VE GOT A PROBTEM jumper. That is. Knowing how to solve an inverseinterpolationproblem by recastingit as a roots problem.. Appreciatingthe dangersof extrapolation. we might f we want to improve the velocitypredictionfor the free-fallingbungee I expandour model to accountfor other fhctorsbeyondmassand the drag coefTicient.: r"it'liii Polynomiol Interpolotion CHAPTER OBJECTIVES The primary objectiveof this chapteris to introduceyou to polynomial interpolation. example.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.In such not a case.'. Suppose that you desiredthe densityat a temperature includedin the table. Recognizingthat higher-order polynomialscan manif-est largeoscillations.you would have to interpolate. Knowing how to pertbrm an interpolationwith a Lagrangepolynomial. As I I was previouslymentionedin Section2.7.1is reprintedfrom a popularfluid mechanics For textbook (Whire. Knowing how to pelfbrm an interpolationwith Newton's polynomial. 1999). you would have to estimatethe value at the . Specific objectivesand topics coveredare ' ' ' ' ' ' ' polynonrial Recognizing that evaluating with simultaneous coefficients equations is an ill-conditionedproblem. Air density and viscosity are commonly presented tabular form as a function of in temperature. 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 .

(p). 15.kg/m3 1t..99x l0 r 1 33 x l0-r j l ..only one parabola connects setofthree a points (Fig.457 5t x 7l x B Ox 95 x 2 . Similarly.there is only one straightline (i. N .l)th-order polynomialcan be written as .thereis one andonly onepolynomialof order (n . This polynomial then provides a formula to values.we will explore a numberof different for estimates suchsituations. The generalformula for an (a . by p. Polltnomialinterpolationconsists determiningthe unique@ . . f (x): plx" I p:. m2ls 0. . for approaches obtainingadequate EXA I5. Density dynomic viscosily ond kinemotic (u) viscosity os o functiono[ (f) temperoiure ot I otm os reported White (1999). in error can be introduced the data exhibits significantcurvature.1a).l) that passes through all the points. 4 5x l 0 . when linear interpolationis perfectlyadequate many cases.e.946 0 835 0 746 0 675 06r6 0 525 a.f(x):ar*azxla3x2 +.lr). l5.t T.'C /A 0 2A 50 100 150 200 254 300 400 500 52 29 2A 09 4.38 x 2 . it uses decreasingpowers as in I '+'.t' 1 1 t .4 with MATLAB. For example.336 POLYNOMIAL INTERPOLATION (p).r. To be consistent . 5 7x 2 75 X / YJ lO 5 lO 5 l0 5 lO r lO-5 l0 5 lO-r IU' x l0 r X ]U U' 355 X basedon the densities desiredtemperature that bracketit. powersof .j 4 O Bx l O .1) For n datapoints. should note that MATLAB reprcsents we (15. The simplestapproach t0 deis termine the equationfor the straightlirre connectingthe two adjacentvaluesandusethls Althoughsuch equation to estimatethe density at the desiredintennediatetemperature.l)thof order polynomial that fits n data points.1 t I p . (15.j 4 75 x lA': 6 20 x lO-j 777 xla5 TABTEl5. 1 7x 2. a first-orderpolynomial) that connects two points(Fig.'+erx"t (15.Ratherthan usingincreasing in a different mannerthan Eq. 5 Ox l 0 l /9 x 105 230xl0j 2 85 x l0-5 3 .1).. s/m2 v .I INTRODUCTION INTERPOTATION TO You will frequently have occasionto estimateintermediatevalues betweenprecise data points.In this chapter. The most common method usedfor this purposeis polynonial interpolation. computeintermediate polynomialcoefficients Before proceeding. we will adontthis schemein the followins section.

2)is basedon the fact that n datapoints are requiredto determinethe n coefficients.o r d ec u b ' c l {r conneciinoiour ooints.2)to yield a system three Eachof these into of equations.15. (15. 0. r : : 5 0 0 . t 2 1 p 2 . Suppose that we want to determine coefficients the parabola. 4 5 ' 7 pairscanbe substituted Eq.l Defermining Polynomiol Coefficients A straightforward way for computingthe coefficientsof 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. (15. the problem reducesto solving three simultaneous linear algebraicequationsfor the three unknown coefficients.| [ o. l5.4s7 | Thus. s z s ] 00 I p.525 .this As allows us to generate linear algebraicequations n that we can solve simultaneously the for coefficients.616: pt(30q2 + p2Q00) p3 + 0. 04 o ot l l n z | : l o . E X A M P L1 5 .A simple MATLAB sessioncan be used to obtain the . I INTRODUCTION INTERPOLATION TO 337 FIGUREI5. 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.oro I 1 6 0 .525: p1@00)z p2(400) p3 + + -l -t 0.l. : .+ p l . in the following example.6t6 xz:400 f(x):0. f ( x ) : p t .ooo rl I p. the of \ l5.I (o) Exomples nierpoloting of (lineor) polynomiols: firsl-order connecfing two poinis.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 .000 J 10. s00 lJ I 1250. :' g { b ) s e c o r d .451: pr(500)2 p2600) pt or in matrixform: 3oo I oo. f( x ) : 0 .

the value of densityat a temperature 350 oC can be calculated of as : + / ( 3 5 0 ) : 0 .-l Lxi .00171s00000000 r.6r6 4. notice that the coefficient matrix in Examplel5. 0 2 70 . 0 2 1 This polynomial then providesa meansto determineintermediate points. As a consequence. which is quite large for a 3 x 3 matrix. This can be seenclearlyby expressing ingenit eral terms: t'. .525 0.r I J l-"i -rr I I lr. >> A I.338 INTERPOLATION POLYNOMIAL solution: >> >> format long . 0 0 1 7 t 5 ( 3 5 0 ) 1 . r 1 .190000 3tl0 1.To understand this flaw.l has a decided structure. it has a seriousdeficiency. 0 0 0 0 0 1 1 5 .That is.r.tr) ll i\l:{ } ilii Coefficient rnatricesof this tbrm arc ref-eflrd to as Vctndennonde nntrices.1 provides an easy way to performinterpolation. we will also describe two altematives tlrat are well-suited for computer implementation: the Newton and the Lagrange polynomials. Before doing this. however.1as >> cond (A) 5.'.451 )'. 5 6 1 6 2 5 Although the approachin Example 15. there are altemative approaches that do not nranifbst rhis shortcoming. 0 0 0 0 0 1 1 5 ( 3 5 0 ) 20 . we will first briefly review how the coefficients of the interpolating polynomial can be estimated directlv with MAILAB's builrin functions. The ill-conditioning becomes even worse asthe number of simultaneous equations becomes larger.160000 404 b = [0. 0 0 1 7 1 5 . Such matrices are very ill-conditioned. their solutionsarevery sensitive round-offenon. r 2 0 .8912e+a)0t-.42100ct00000000 Thus. In this chapter. x ): 0 .00000115000000 -0.250000 500 1l. the parabolathat passes exactly throughthe threepoints is + f ( . implies that about six digits of the solution would be questionable.For example. to This can be illustratedby using MATLAB to computethe conditionnumberfor thecoefficient matrixfrom Examole15. This condition number. >>P=A\b 0.

The notation /r (-r) designatesthat this is a first-order interpolatingpolynomial.2).and second-order of 15.1) which can be rearranged yield to fr?):f(x)+ f(xr)- f(. Using similar triansles. Before presentingthe generalequation. >> density = i0. This technique.56762500000000 These results agree with those obtainedpreviously in Example l5. it returnsthe coefficientsof the polynomial that pass directly through the datapoints.1: >> format longt >> r: [300 400 500]. 350) 0.525 0.2.we will introducethe versionsbecause their simple visual interpretation. first.l with simultaneous equations.That is. 15. popular and useful forms.called linear interpolation.4.. s ) which is the Newton linear-interpolation formula. Notice that besidesrepresenting slope of the the --rr) is a finite-difference line the the term .rr) x2*xl (1 s .2 NEWTONINTERPOLATING POLYNOMIAL 15. ly f it performs interpolation. passthrough any of the points. density.is depictedgraphicallyin Fig.2 ) _ I O. number of data points is greaterthan the number of In the Consequently.02'IOOOOOT]OOOOO We can then use the polyval >> d = polyval d.2 MATTAB Functions: polyf it dlrd potyval 339 Recall from Section 13. po For the casewherethe numberof datapointsequalsthe numberof coefficients.1. such applications.451).616 0.I 5. >> p = polyfit (T.OO1715OOOOOOOO . 15. function to perform an interpolation as in (p.it can be usedto determinethe coefficients of the parabolathat passes throughthe last threedensityvaluesfrom Table 15.that the polyf it function can be usedto perform polynomial regression.1 lineor Interpolotion The simplestform of interpolationis to connecttwo datapoints with a straightline.2. fi(r) .2.2 NEWTONINTERPOTATING POTYNOMIAT forms for expressing interpolatingpolynomial beyond There are a variety of alternative an (15. Newton's interpolatingpolynomial is among the most the familiar format of Eq. For example.f (rt) ^ 4l f(x)-f(xt) x2-xl ( 15. least-squares line doesnot necessarily the fit coefficientsbeing estimated.OOOOOll5OOOOI]O O.but ratherfollows the generaltrend of the data.

3 V c . 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 .. line.340 INTERPOLATION POLYNOMIAL . of depiction ineor C. the better the approximation.asthe betweenthe data points.2 L i n e o rI n t e r p o l o t i o n EXAMPLE First.li(r) ---_-------:-. general. r : = 4 o yields t.20)1. Solution. 7 97 5 e.4 -/ .' -/ .2 used triongles the indiccrte slmilor oreos The shoded inierpolofion.fi (2:o + ff ) 1 .x) T FIGURE5. true valueof ln 2 is 0.4620e81 .0 1 (2._*\\ f(xz) d . We useEq.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 . between by perform the computation interpolating the Notethat but repeatthe procedure. 5 ) ] l In approximationof the first derivative{recallEq.f: 4 S .5)from :r1: I to rz : 6 to give .386294). E. a interval decreases. ( 15. continuousfunction will be better approximatedby a straight in is This characteristic demonstrated the following example' 'I5.stimate naturallogarithmof 2 using linear intelpolation./ r "f(.the smallertheinterval This is due to the fact that.-r86294 0 lre\ :0+ t) :0. Then.791759. ln I :0 and ln 6: 1. the Problem Stotement.6931412./ I rii t . use a smallerinterval liom ln I to ln 4 ( I .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 . (a.

l(. (15.. polynomial (alsocalled a quadraticpolynomial or a parabola).3.3 Two lrneorinterpololions esiimoleln 2 Note how the smollerinlervolprovideso better lo esiimote. r r ) ( x .7)and (15. B<tth interpolations shownin Fig.6) For A simple procedurecan be used to determinethe values of the coefTicients.tr) .\'l into at Finally.{2 f(.xy) (15.Eqs.rz).t with -r :.8) br:"{2 .9) {.3Vo. Eq.r) 2 05r FIGURE I5. strategyfor improving the estimate to introducesomecurvatureinto the line a this can be accomplished with a the connecting points.rt . are | 5.7)can be substituted into Eq.: r r ) .r solved(after some algebraic and ..8)can be substituted Eq./(-r:) h-1l .| D : ( . 15.6). b1.f (.7) at Equation(15.1 .rl can be usedto conrpLlte b1: .6..6)..2 NEWTONINTERPOLATING 341 J(. z ) (15. Thus. t.2 GluqdroticInterpolotion The error in Exaniple 15.If threedatapointsare available.rr) (I 5. usirrg the shorter interval reducesthe percentrelative error to €t :33.POLYNOMIAL I 5. (15.2. Conis sequently.(15. particularly A second-order 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 \ .f .: . (15.which can be evaluated rrranipulation.s) for -t : -r-.which can be evaluated r : -r2 for (x) .f(-t:).l'(. (15.2resultedfrom approximatinga curve with a straightline.alongwith the true function.

2: rl:1 .r:) : l.r) 2 fl. introduces second-ordercurvature the formula.\z:4 r-r : 6 "/(xt):0 J$):1.It is very similar to the finite-differenceapproximationof the secondderivative introducedpreviously inEq.r -. o | \.4610981 +.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. That is.xr)(. (15. Employ a second-orcler Newton polynomial to estimateln 2 with the samethreeooints usedin Example 15. and rr. terms are addedsequentially t0 captureincreasinglyhigher-order curvature. A p p l y i n gE q .6) are equivalent to linear interpolationbetweenx. ( 1 5 . 7 ) i e l d s bt:o Equation( 15.386294. (15. as specifiedpreviouslyin Eq.. (15. . we should examinethe form of the coefficient &. 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 .38629.t Ouadraticestimate Linearestimate .6) is beginningto manifesta structure that is very similar to the Taylor seriesexpansion. T h el i n e o irn l e r p o l o i ifo n m . the into Before illustrating how to use Eq.27'). (4.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. Eq. (15.l5. still represents slope the b2 the of line connectingpoints xy and "r2.5)..The lastterm.'791159 y S o l u t i o n .Thus.| FIGURE5. the first two terns of Eq.8)gives b 1: |. b:(-r .342 POLYNOMIAL INTERPOLATION Notice that.6).0 _:0.r:2).1 -f(.r) : In. as was the casewith linear interpolation.

.12) (r5. . . t s r e v a l u a t eh e c o e f f i c i e n tb 1 . f " _ t f ) : b r 1 -b z j .rt (15.x ) . . r r ] .10) As was done previously with linear and quadraticinterpoliition. .r . . _ _yl ( 1 s1 5 ) . 1 1 .l)th-order polynornialto n data to points. 15.6)yieldsthe quadratic formula Substituting .386294 343 6-4 6. .The (n . 15. J Q ) 1 . expressed : f lx. . l x z .4) improves the interpolation cornparedwith the result obtained using straight lines in Example15.I 5.5658444.f .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' : . 0 .5 1 8 7 3 1.3. q J. ( 1 5 .x n .\n - ( rs . For exanrole.. . which representsa relative error of e.I )th-order polynomialis . r"i l : r.r r ) 1 . .1 .0.. . F o r a n ( n . . .t . r r ) ( x .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 . .f [ r i .x r ) * .r] (15.0518731 these valuesinto Eq.791759 1.47o.16) Similarlv. xz.r : 2 for fz(2) : 0. * b u \ . b . The secondfinite divided difference. .0 .: f l x . (r-5. . .tlre are generallyas first finite divided differenceis represented J l ^ i . 4 6 2 0 9 8 1 ( . . r .datapointscan be usedto .q. | 5.x l ) ( : r . l ' ( ...kit-f(rl) -t. (15.2 NEWTONINTERPOLATING POLYNOMIAL a n d E q .r l) (1s. x1.1 ) t h . b 2 . the curvatureintroducedby the quadraticformuia (Fig.( x .4 ) which can be evaluated At .2and Fig. the nth finite divided differenceis . x r l ri . [ . ( x * . : 18.l ) .I .l f [ x . _ r ) (15.2.f'[r2.r3) x fu : f fv. . " r z . f z @ ): 0 r ( + 0 .whichrepresents difference two first divided the of generallyas is differences.14) where the bracketedtunction evaluations finite divided differences.3 Generol Form of Newfon's Interpolofing Pofynomiqfs The precedinganalysiscan be generalized fit an (zz. Thus. 1 7 ) "Yl . . 9 )y i e l d s 1.462098r : -0. I . . tl : b . .

10)with n : 4. (15. higher-order differences are computed by taking differences of lower-order differences(Fig. .f (.4 Newton Interpoloting olynomiol P Problem Siotement.. and./ L^41 ^-{l -_ l - :0.r. r z ) .(15.x t ) f l x z .15)through(15..x r ) * b z ? .t ) . xz:4.[] . x2]: fr.11) can the in through which can then be substituted interinto Eq. . Thesedifferences be usedto evaluate coefficients Eqs.j.10)to yield the generalform of Newton's polating polynomial: f. x z .3. 15.x ) f l .r3= ( 1ry91s = used to estimate ln 2 with a parabola. Eq. Also. polynomial.x r ) ( .. . "f["r:. .4620981 : -_.19r'759 :0.. x 1 l + .t . Now. (15.:rl it::ti=. x r l( 1 5 .eferred oso divided to difference toble. t r n . to the 1 EXAMPLE 5. adding a fourth point [xa : 5.386294 .. The third-order f z @ ) : b r * b z @. xt.-rG):.609438 1..x r ) ( x .5 I5 Grophicol depiction iherecursive of finite is of noture divided This differences.data pointsat xt : l. .asillustrated in the following exarnple.a4t. FIGURE . r r ) ( x .'^' f(x) First Second Third xl r flxo.17)arerecursivethat is. (15. is Solution. " + ( x .':^:). 1 8 ) We should note that it is not necessary that the data points used in Eq. . f l x n .2.x n . (15.1823216 5-6 r.f ( x t ) + ( r .344 POLYNOMIAL INTERPOLATION xi xl '. r . .15)] for 1.x 3 ) The first divided differences the problem ale [Eq.5).{) L6094381.estimateln 2 with a third-orderNewton's interpolatingpolynomial. (15.:0. (15. representotion .2027326 6-4 1. r r l * ( " r.x ) * b q ( x.l ii. 4 .:. ( r .rr] 4 f [4.14).18) be valuesnecessarily in ascending equally spacedor that the abscissa be order.791159 r. noticehow Eqs. x 2 .x2. .x 2 ) ( x .This property will be exploitedwhen we developan efficientM-file in Section15. In Example 15.386294 O . .x r ) ( x .4 implement method.

-r1] : 345 o.007865529 Thus. (15.6.I 5.3Vo. x z .2021326 0. .4620981 . x 2 . FIGURE . the divided differencetable is f (xi) j 4 6 5 0 386294 791759 609438 a 462A9Bt 0.6287686. x t ] .the interpolating cubicis -f:(r) :0 + 0 .6) n which can be used to evaluate.1823216 0.fzQ) :0. coefficients b2. . t l l r e p r e s e ntth e b1. 0 5 1 8 7 3 1 l (-" t l ) ( x .4)(r .02041100 The third divideddiff'erence [Eq. of Thus.x 1 ] : -0. (15.0 .2 NEWTONINTERPOLATING POLYNOMIAL The second divideddifferences [Eq.007865529(x l)(x . Eq.02041 . x 1 ] .4 ) + 0.16)] are /[. 15. .0 ..t2. f [ x t .t3. (l-5.x 2 ) : 5-4 : -0. andb'.051873l) r00 1 J | : 0. a n d f l x q . 4 6 2 0 9 8 1 ( x l ) .2427326 o l823216 -cc5lBZ3il .(-0.t 2 .6 I5 The use of cubic interoolofion estimoten 2 lo f(x) z . 0 5 1 8 7 3 1 : I 6-l 0. . f f x a .2027326 . r t . respectively. f [ x z .10).b3.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 ) . which represents relative error of a €t :9. The complete cubicpolynomialis shownin Fig. x z .17)with n :4] is J ' [ x + . x 3 .

4 MATTAB M-file: Newtinr It is straightforward developan M-file to implementNewtoninterpolation. (15.1)-order Newton ? yint .2. : r 1Fr ifnlv. in Fig.j) . difThe ferences then usedin conjunctionwith Eq. are the An exampleof a session using the function would be to duplicatethe calculation we just performed Exarnple15.j-1))/(x(i+j-r)-x(i)) b(i.346 INTERPOLATION POLYNOMIAL 15. = yrnt+b(1.7.x.lanor F va ' ' rh'c 1Fr x% z % vxx= r.y.y.omnrl e rlre finir-e % difference table n = length(x). 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.j-1)-b(i.fdf variable C-IaLeO at which z ? output yint % = inierpolated value of dependent variable !} . xx.1) % the (:) ensur:es that y is a column vector for j . for j = 1.xx): Uses an (n . 1 - divided differences to inLerpolate yint = b(1.ts .18)to per-form interpolation. end end ? use Lhe finite 1.n for i = 1:n j+1. ? assign dependenL variables to the first column of b.3: in >> form. j+1) *xt. = y(:).(b(i+1. to As the first step is to compute the finite divided differencesand storethem in an array.2. = Newtint(x.xx) function yint polynomial I'Jewton interpolal-ing % Newtint: = llewtrnt(x.lanonrlanf rr:ri :h-l o value Intrlyn^ i of independent a-'o.15.et = tl Longt 4 6 5l >> x I5.n) . % % input: :-.7 FIGURE An M fl e lo imp ement Newton inferpolotion.n-1 xr xt"{xx x(jr). b(:. yint end .i).

J r i l r . { : . The samestrategycan be employedto fit a parabola throughthreepoints. 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 / ( .x : ). is logical that the first weightingcoefficient It is the straightline that is equal to I at x1 and 0 at x2: xt-x2 Similarly.).r): Lt J @r)-t Lzf Qz) (rs.and second-order versionsas well as higher-order Lagrangepolynomials can be represented conciselyas (1s.2) 347 0. Equation (15.r ( .the second coefficient the straight is line that is equalto I at "r2and0 at. 8 ) : .For this case threeparabolas would be usedwith eachone passingthroughone of the points and equaling zero at the othertwo.r l.. : "/r(x) ./ (r3-x1)(xj-r2) /. r :...19yields the straightline that connectsthe p o i n t s( F i g .r-X2' xl-X2 ^ J 6) -l .r-rr)(. >> Newtint(x. T h e o t h e r t11 terms work in a similar fashion.x r ) ( .6287685"t890841 I5. \ -.rl Substituting thesecoefticients into Eq..y.22) . ( . 15.l .20) where the nomenclatnrefi(l) designatesthat this is a iirst-order polynomial.3 TAGRANGE INTERPOTATING POTYNOMIAT Suppose formulatea linear interpolatingpolynomialas the weightedaverage the two we of valuesthat we are connectingby a straightline: .20) is refered to as the LinearLagrangeinterpolatingpolynomial.t .r-xr) -( x r x z.-=1..t - (. 1 r ) --. r 2 a n d . r . Such a second-order Lagrangeinterpolatingpolynomial can be written as At. \ ' r ) ( r .I 5.r t ) - (x-x1)(x-x3) j tt2t + (.3 LAGRANGE INTERPOLATING POLYNOMIA. > > y = t o S f( x ) . Their sum would thenrepresent uniqueparabola the that connects the threepoints.r9) where the Z's are the weightingcoefficients.r r l . 1 5 .Xr X2-Xt J'.r1: r tr-trI 12 .\'-. r 1 ) aa n d i s e q u a l t o z e r o a t . ( r5 . Both the first./(.6) (1s.

2-0 t 5 . Theflrst-order can tEq.r: I5: t 5 .r : the numberof datapoints and fl designates "product of. 5 6 2 5 -1.20)l be used obtain estimateal to the Solution.(.o" 0 .212 polynomial (15. . r ) : ' " 0-20 t0-0 .r): | | ' ! I j=l .|5. 8 0 0 : 1 .r:):0.85+'" 'f r( .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 . ^1 (15 23) _ | ." the . r/ where .iherefore.The summoiion ihe two fermsmusi. Use a Lagrange interpolating data: orderto evaluate density unused the of motoroil at T : 15'C based thefollowing on rr :0 r::20 .IFI tl _- -fl y.5 Interpoloting Polynomiol EXAMPLE Logronge polynomial thefirst and of second ProblemStotement.85 l(.gtlO f(rt):0.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.348 INTERPOLATION POLYNOMIAL LtfU) FIGURE I5. the uniquestroiqhl of b"e ihe thot connects h^/opornts. where r L.{::40 f(xr):3.

duct.23).t ] .Lag"6ngerx.9 inlerpolction AnMJile implement lo Logronge y nL .(15.I'i for j = 1. end for: i = 1:n .212:1._.y.40) (0 . in It is straightforward develop M-file based Eqs.. second-order is as In a similar the /.xx) : Uses an (n . yint .0)(20. 6 t .1)thorderpolynomial fit.9. is alsopassed valueof theindependent (xx).n if i -= i product = product* (xx-x(:) ) / (x(i) -x(j) ) . end end s = s+prc.22) to (x) two vectorscontaining independent and the the Fig. U (20. Z input: x .J316875 (40-0)(40-20) | 5. Produce .20)(0.1)-order yint 2 polynomial based on n data points ? Lagrange inLerpolating (yint) at to determine a value of the dependent variable % xx. I 5. Z a given value of the independent variable.21)] fashion.3 LAGRANGE 349 polynomial developed [Eq.i .\. 15.1 MATTAB M-file: Lasranse As an on and (15.{r): - (15-20)tl5-40. _ n.r^^_ (15-0)(15-40)^^^^ . if lenqth(y)-=n. error('x and y must be same length'). n values passed.INTERPOLATING POLYNOMIA. (n .xxl trrccion polynomial Z Lagrange: Lagrange interpolatingr = Lagr:ange(x._ U.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). .3. 6 1+ . end = s.40) + (l5-O)fl5-20).( 15. FIGURE I5. Theorderof thepolynomial based thelength thex vector is on of wantto interpolate is If are an thatis passed.the functionis passed (y) you variable where It the dependent variables.

the . to because lunction is availableand easyto manipulate.t valuesin most interpolationcontextsarethe dependentand independent variables. This can occur even for lower-orderpolynomials. In most cases.22II2B I5.merelyplot x versus to use /(x)] and an approachlike Newton or Lagrangeinterpolationto determinethe result. in many cases.1667 7 0 . For thiscase. the dataabove. d. when you reversethe variables.1.but you are given a value for .rthat makesthis polynomial equalto the given /(.29 i. For example.r)and.e.r's are evenly spaced.r)./(r) and must determinethe corresponding value of x. 3 3 3 3 . valuesof the As the r's are typically unilbrmly spaced. 3 3 . Thus.f ('. (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. (r). correctanswercan be deterthe the : m i n e dd i r e c t l )& S { : 1 1 0 ./.r)'s] will be evenly spaced." That is. For instance. >> d = 1r. the interpolationproblemreduces to .3. a consequence.2 >> densitY i I \ 41 22222 2 LaQfange r o-ns t.respectively.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.An alternative strategyis to fit an nth-orderinterpolatingpolynomial.r) and. a third-orderpolynomial would be implementedby the r-aqrange functionto gile: >> format longr >>T=t-40024541. fact. this polynomialwill not f (r) versus. they will have the appearance a logarithmic scalewith some adjacent of points bunchedtogetherand othersspreadout widely. Such a problem is called inverse interpolatiort For a rnore conrplicatedcase.Unfortunately. with because the.rl. 15 ) I .091. to Becausefour valuesare passed the function. t4 2 9 Now suppose that you must use the samedata.e. be ill-conditioned.r values[i. 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. you might be tempted switchthe /(. thereis no guarantee the valuesalong the new abscissa that In the [the /(.52 1.suppose for that you were askedto determinethe value of I that corresponded l (-r) : 0.. valueswill be "telescoped. to the original data[i.The answerto your problem then amountsto finding the valueof .4 INVERSE INTERPOLATION As the nomenclature implies..

e n d en a t u r eo f i d F I G U RI 5 . I5. 15.r) : 0.296.r 2 .375 + (-0. . the secondroot. The resr.If additional accuracywere desired. .78333 : 5. ' E x t r a p o l a t i o n - r.of interpolatin o Il . 0 8 3 3 3 1 For this simplecase.a simple approachwould be to fit a quadraticpolynomial thethreepoints:(2. A s d e p i c t e dn F i g .295842 2(0.(3.f (x) . . I O E lllustrolion possible of the divergenceon extropoloted of prediction.1 Extropolofion Ertapolation is the process estimatinga value of .e 0 .for the problernjust outlined.041661)0. .3333). t . 1 5 .0 .3 would therelbreinvol'r. 3 1 5 x+ 1. quadratic the fbrmulacan be usedto calculate 0. 3.r r .is a good approximationof the true value of 3..1 0 .rlt wouldbe / : ( r ) : 0 . Extrapolation I 01 tnteroolatt r.333.r correspondingto determiningthe root of l (.041667) Thus.r) that lies outsidethe rangeof the of k n o w n b a s ep o i n t s . I n t e r p o l a t i o n. to and(4. .25). polynomial I I I I I I t> l"r .0 .5 or 6 couid be employed. 3 7 5 r+ 1 . .there are twcl issuesrelatedto polynornialinterpolationthat must be addressed. . 0.a third.37s)2 4Q. Theseare extrapolation and oscillations. . t h e o p e n .5 EXTRAPOTATION OSCITLATIONS AND Before leaving this chapter.5). 0 4 1 6 ' 7 2 .5 EXTRAPOTATION OSCILLATIONS AND 351 For example./ (.or tburth-orderpolynomial along with one of the root-locationmethodsfrom Chaps.704158 3. extropolofion The isbosed through first poinfs on fittinq porobolo o thL three known .I 5. 0.5. 0 4 1 6 6 7 r r. 3 : 0 . 0. 0 8 3 3 : l The answer to the inverse interpolation problem of finding the .

6 1 Dongers f Extropololion o Problem Stqtement. 1 ) .27 180. >.42.when this is implemented. r 6 l .05 227.pp) E} .. 2 0 0 A ). Extreme care should. >> pop = 1105. therefore. following message displayed: the is VJarnlncJ: Polynomial po'nt s or POLYFIT..45 I23. >> pp = polyval (p. First. (tt-1955) /35). (2000-f955)i 35) 175. pop.0800 which is much lower that the true value of 281. andMoler.7 ) However.23 2 4 a.61 205.{)oD. ' >p = p o l y f i t ( t s .I2 L52. p o p . a > > t t = l i n s p a c e ( I 9 2 A .Use it to cornpute Fit a seventh-order the population in 2000 by extrapolationand compareyour prediction with the actualresult. Solution.o.be exercisedwhenever a case arises where one mu$ extrapolate. 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 .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. The polyf it function can be usedto computethe coefficients >> p .polyfrt (t.the true curve could easilydivergeliom the prediction. >> rs = (r Now po11. by from 1920to 2000 can Malcolm.As such. EXAMPLE 5.3s2 POLYNOMIAL INTERPOLATION a the processextendsthe curve extrapolationrepresents step into the unknown because beyondthe known region.08 132.fit works without an errormessage: r . is badLy condil-ioned.1955)/35.Insight into the problem can be gained by generating plot of the dataand the polynomial. This example patterned is afteroneoriginallydeveloped Forsythe.08 13212 15227 tBA67 2A505 22723 24946 28142 polynomial to the first 8 points (1920 to 1990). nloi (r.tt. We can then use the polynomial coefficientsalong with the Fo1yr731function to predict in fhe population 2000 as :'> polyval (p.the datacanbe entered as >> t = L\924:10:19901.

. He took equidistantlyspaceddata points from this function over the interval t-l. Further.?.. considerably. they tend to be highly sensitiveto round-off eror.*::TJ:il:1Jl:J. In 1901.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.1]ii*:.He looked at the following simple-lookingfunction: I : rt l l\t . Z 5 D o n g e r so f H i g h e r .24) which is now called Runge's function.Carl Rungepublished studyon the dangers highera of order polynomial interpolation.5.5 EXTRAPOLATION OSCILLATIONS AND 353 250 r\ 200 150 1920 1930 1940 1950 1960 1970 1980 1990 2000 FIGURE5. E X A M P L lE . (15. He then used interpolatingpolynomials of increasing order and found that ashe took more points.' l *25x2 (t5.Higher-orderpolynomialstend to be very ill-conditioned-that is. the | 5.". 1 .2$. The following exampleillustratesthis point nicely.:. e p r e d i c t i o n U .[i.I 5..'n::ilT::.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 .p o o u l o n . ll.: polynomialplungesto the erioneous extrapolation..*1.and tenthorder polynomials to 5 and 1l equally spacedpoints generated with Eq. Create .the polynomialsandthe original curve differed greatly as the order was increased. " 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 .it is absolutelynot true for polynomial interpolation. seventh-order predictionin 2000. S .the situationdeteriorated Duplicate Runge'sresult by using the polyf it and polyval functionsto fit fourth.2 Oscillotions Although "more is better" in many contexts.

|t t.354 POLYNOMIAL INTERPOLATION Solution.xx. 15. i -0. the Continuingwith the analysis.l ( x "f(r) (a) C al fc . I.yt:.4 -1 r\ i\ ta\ tta t5.11).y . a more finally spacedvector of rx valuescan be computedso that we cancreate a snooth plot of the results: >> xx = linspace(-1. Recall that l inspace automaticallycreates100 points if the desirednumberof points is not specified. / (I+25*x. 4) . (p.The po ly f r t function can be usedto generate coefficients thefounhof the order polynomial.2 -0.5 .-I.y.' o'. The five equally spaced datapointscan be generated in as >> x >> Y = linspace(. Finally.y4.^2).5). polynomialdoesa poorjob of followingRunge'stunction. Next.xx.' --' ) As in Fig. we can generate valuesfor Runge'sfunction itself and plot them alongwiththe polynomial fit and the sampleddata: >> yr . tenth-order polynomial can be generated plotted the and with >> >> X y = = linspace( 1./(I+25*x. I . ^2) . /t It r\ r\ t\ l! t. FIGURE . and the polval function can be usedto generate polynomial the interpolation at the finely spaced valuesof xx: = po11'f it >> 1.4 = polyval >> p (x. >> plot (x. xx) .1.12.0."2).L.I2 I5 funct]on Cornporison o[ Runges line) polynomiol 5 poinis fitto {doshed wiiho fourth-order sompleo lrom lne iunclron.1).i (1+25*xx.I.

25 5 )9. accuracy.l Giventhe data 2.75 6 36 polynomialsof polynomi.Runge's fit polynomiol to I I points {doshedline)with o tenih-order somoledfrom lhe lunction >> p = polyfrt (x.y. x x ) .lower-order polynomials of the type describedin this chaptercan be used effectively to capturethe curvins trendsof datawithout sufferins from oscillations.yr. Choose sequence for your estimatesto attain the best possibleaccuracy.5 F I G U RI 5 .In most engineeringand scientific contexts.xx.PROBLEMS 355 a -0.13.xx. 15. I 3 E functlon Comporisonof.4) usingNewton'sinterpolating ofthe points order I through 4.'o'.. Although theremay be certaincontextswherehigher-order they are usually to be avoided. > > y 1 0 = p o l y v a l (p .5 7 3 65 5r o "f(r) I 475 3 5. PROBTEMS Giventhe data l5.5 -1 -nF 0.10) .y10.What do your results indicate regarding the order . Chooseyour basepoints to attain good the alsoforder I through3.nnminl ofthe usedto generate data in the table? the .particularlyat the endsof the interval! polynomialsarenecessary.y. >> plot(x.aL^ r ^_-^-_^ -^l.Calculate/(4) usingNewton's interpolating Calculate /i3.' -' ) As in Fig.the fit has gotteneven worse.

as well as the precisionof the experimentalmethods.6 for the following tabulateddata: to to measure voltagedrop and corresponding the curent for a rcsistor. : I R.r12 67 TAB T.5lists valuesfor dissolved oxygenconcentration in water as a function of temoeratureand chloride T ' K 204 254 300 350 404 450 concentratlon. Use intemolation estimate of to To quantify this relationship.6 Employ inverseinterpolationto determinethe value of r that coresponds to l (-t) : 0.throughthe resisteras in V However. third-.11 RepeatExample 15.Thatis. 15.for the linear predictionusethe datafrom I 980 and I 990. m3/kg o to377 0 il 144 4 1 2 5 4 7 and fourth-ordcr interpolating polynomials to predictthe 64147 s.. pola r01 J@) 0 0. 15. " o 5 t0 t5 20 25 30 f(x) 36 lB 12 09 072 I 5 A .l in s pres T. 15.3 RepeatProb. What is your tion is listedin TablePl5.and to casebasedon the true value.Determinethe percentrelative enor for each dratic interpolation.5 Employ inverseinterpolationusing a cubic interpolat.Estimate"/1 I ) using third-. MATLAB's builrin tunctionbessel j.10.93 for the following tabulated data: -2 637 -t 96 5 -05 -245 05 205 t 965 the r 637 lO o 15.10 Besselfunctionsoften arisein advanced engineering analyses such as the study of electric fields.the smoothness the relationof ship.Use firsl throughfifth-order polynomialsto water as a function of temperature and chloride concentraestimatethe densityat a temperature 330 K. which can be determined with entropyof 6.l lar a the r ( Bec has ( whe dim . u. (b) the same coffesponding entropy using qua. where R is the resistance. Here aresome selcctcdvalueslbr the zero-order Besselfunction ofthe first kind SOIV the < the r for l r5.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). and I 990. ing polynomial and bisectionto determinethe value of x that Suppose that you performedsomevery prcciseexperiments corresponds /(-t) : 1. ideal resistoris linearly proportionalto the currcnt I flowing (c) Repeat(b) but use quadraticinterpolation.12 The saturationconcentration dissolvedoxygen of in high precision. (b) Use quadraticinterpolationand the quadraticformula to determinethe value numerically.1 TableP14. curve must be fit to thedata. polation. regressionwould typically be the preferred method of curvc litting fbr analyzing such experimental data. suggests that interpolationmight be appropriate. and fourth-orderinterpolating (c) the volume corresponding an polynomials. l5.2 using the Lagrangepolynomial of best estimate? Employ this best estimateand inverseinten order I through 3.356 POLYNOMIAL INTERPOLATION 15. Densiry \ 7 0 8 I 3 6 7 I r 3 9 0 . x IB 20 2. (a) Determinethe correctvalue analytically.. (c) Use cubic interpolation and bisection to determinethc value numerically.However. second-. real resistorsmay not always obey Ohm's law. I 980.l2. Which approach yields the best result? sus temperature comesfrom a table that was measured with 15. 54 2 9 t t *". polation to determinethc conespondingtemperature.9 Ohm's law statesthat the voltage drop V across an centrationfor I : 12 "C and c : 15 glL.6but using tirst-.7 Use the portion of the given steam table for super. 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. a Becauseof mcasurcmcnterror.2 24 26 15.and so on.for the quadraticpredictionusethe datafrom I 970. 15. Jl Dett r5. kJl(kg K) 6 5453 6 7664 populationin 2000basedon the most recentdata. The following resultssuggest curvilinearrelationa 2.45 using inverseinterpolation. 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.lt The following data for the densityof nitrogengas ver. t ' ( r ) 0 .4( ship rather than the straightline represented Ohm'slaw: by . v 15. 15. "1 a. for (tl) Use linear interpolationto determinethe oxygen con15. I l8 with linear inter(2. Usea fifthorder interpolatingpolynornialto fit the dataand compute V fori : 0.

t:tj'Tltt \tre\s..". r r 7 i r eti r r rn b r E r p r e s s ) o u r .idecollccntrationisequa|tol0g/L.13241 a.ro fo |rheeaser. rI t r r : + n : i l + m 2 n 2 m 2 + n 2 + l L TABIEPl5.t t' t l r l"a 't " t r"| ' i n 1 c n s i t .#| TI Forexampre ata I .2 +-.venl11t J '.-*':::.:ul.iliH.a aa-7 a l2t14 a l4060 a li5o9 c-16613 ltabulatecI. DissolvedOxygen (mglt) for Temperoture("C)ond Concentrotionof Chloride (g/Ll s=OglL '. I q I z*nJ.ll:l*.t1. : levclfor ?': 18'C with chloride the clissolve.-'-.-'.12.l-5.0856 r 0 t0941 0 13003 o .'.6 andh : 14.6 0 03058 a a59q4 a aB709 0l]t35 a.sincorrvenient solye nranually. polynomial use inter15.f(rrr.lhel.sc cquationi.l oxygcn P15.0 I I t? 3 '3A. 146 ssl I lt.f-\nt.a 7Ba I t "t I rolunre ol'u :upcrlicrte(l steanr listctl is lS.lt3/lb. n) where. The influcncevalue is then n=1.+#)l Bccau. 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 .under thc c.at it depthI (i rl beiou' thc corner of a rectangu.l5 tt = 1. and a and b are defined in Fis.45733 0 08323 0 t063t c 12626 0 14309 0 I 5203 0 I 6843 .l5a27 1 0 1 6 55 4.'. N ( ) t c ' J t re s).2 -si.cch|or. (#. l s E I /oo 72.i:J^"Ti::#T?.4 0 a3aa7 0 a5Bq4 0.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 ) .'."" 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.. 1 44 9 7 016199 0 rz3B9 n=1.-..iect a total ]oad of 100 t (metric to . L. Pl5.-.:'. /"/ / i esrimare rhe di:solvcd conce-nrrarirrn o\yscn / " _ _+ I ff."a c=l}g/L l?9 c=2Og/L I I I r: I l.I I I f nfU Pt5.17739 0l o2 03 a4 05 06 a7 OB c 42a26 4..15.r I .D.:'"*"'.: ' 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 . n) and lr and rr are dirnensionless ratios.specil'ic r I u. Bt6 756 lJt 3?9 7 46 685 n '33u 93! 114 673 62A I Iosrr-.usea third-orderinterpolating polynomiat to I ^ I at : 750 'F compufeo. is calledthe influenccvalut-. I I I 25 .*"H"t.13 thc datain Table For l-m-----lT I EU:-'. this fo it has beenreformulatedas o.UseJ.rncr of a rectangu15'15The lar footing that is sub.U Thc.iffiiil:temrcraturcs F r c u RP r s .aportionol'whichisgiveninTablePl5.Ifa: 1.7+ t r] + n2+2 J .) z*a 7(.

49 48.00 Temperotures ot vorious ("C) points plote.00 x=4 B0 00 53 .04 6000 48 I5 5 00 0 3 5 0 3 4000 2 7 . a n d b ) r : 4 .Interpretyour results. 2 3 . m g. 0 0 2000 t t l-i. I 7 The currentin a wire is measured with greatprecision as a function of time: 425 -o. 7 .| 144 6.358 POLYNOMIAL INTERPOLATION TABTEP I 5.15. 6. to a 0 01250 624 02500 775 03250 4.5453 0.K) 0 14377 6.y : 2 .r25. 5 a70 t5 ]BB 20 60 70.19). J = 8 | 5.000 60. 5 0 3 84 3 30 39 3000 x=6 x=8 15.85 0 30.16 You measure voltagedrop Vacrossa resistorfor a the number of ditl'erentvaluesof current L The resultsare "y=O y =2 t=4 Use first.7664 .00 40. i entropys for a specificvolurne u of 0. 5 0 0 0 heated plate (Table P15.78 35. find the samecorresponding (b) entropy usrng the surfaceof the earth is given by quadraticinterpolation.108 mi/kg with linear l5.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 . u (m3lkg) s (kJlke. ) ' : 3 . m/s2 9 8100 9 7487 9. 0 7 3000 2 5 .000 !.llJ The acceleration due to gravity at an altitudey above interpolation. 2 . | 9 x=O 10000 8 50 0 2000 s5. 19 Temperatures are measuredat various pointsona at 0 .000 90 000 120. 3 .45 475 1. and (c) find the volumeconespond. Estimate the temperature ( 0 0 0 0 0 ( a ) . 2 .4147 a. r : 4 .6 using inverseinterpolation.6879 9 6278 9 568'2 g cornpute ar-y: -55.000 m. 15. ir-rg an entropyof 6. on o squore heoted x=2 90 00 64.through fourth-orderpolynomial interpolationto t = 6 estimatethe voltagedrop tbr i: 1.

Knowing how to developcode to perform a table lookup.rltidimensional interpolation implemented is with MATLAB. .f i..i.clamped. However. between .Specificobjectives and topics coveredare .. This For example. ". i' Splines nd rrecewrse o Interpolotion l'l .An alternativeapproachis to of apply lower-orderpolynomialsin a piecewisefashionto subsets datapoints.. third-order curves employed to connect each pair of data points are Thesefunctionscan be constructed that the connections so calledcubic spl.. Understanding conditionsthat underliea cubic splinefit. I6.l)th-order polynomialswere used to interpolatebetweenn data points.*i . . o . Such connectingpolynomialsare calledsplinefunctions. Understanding how mr. o ' Understanding that splinesminimize oscillationsby fitting lower-order polynomialsto datain a piecewisefashion. . Recognizingwhy cubic polynomialsare preferable quadraticand higher-order to splines.and not-a-knotend conditions. for eight points. the Understanding differences the betweennatural.ines. For example..:r. we can derive a perfect seventh-order (at curve would captureall the meanderings leastup to and including seventhderivatives) suggested the points. polynomial. 15 (n . CHAPTER OBJECTIVES The primary objectiveof this chapteris to introduceyou to splines.'. ...I INTRODUCTION SPLINEs TO ln Chap.. Knowing how to fit a splineto datawith MATLAB's builrin functions.i.thereare caseswhere thesefunctionscan lead to erroby neous results becauseof round-off error and oscillations.

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 .1 illustratesa situationwhere a spline performs betterthan a higher-order an polynomial.In contrast. occeolobJe it are adjacentcubic equations visually smooth. polynomialstendto swingthrough how higher-order Figure 16. the spline also connects . order approximationof the splineswould be inferior to the seventh-order might wonder why a splinewould ever be preferable. 16.1 along the region of interest. t o to connections.la throughc illustrates wild oscillations in the vicinity of an abrupt change. The funciionto be fit undergoes obruptincreose -r : O.On the surface.360 INTERPOLATION AND PIECEWISE SPLINES FIGUREI6. Porls(o) through l. Figure 16. lineorsp ine {d) provideso much more becouseit is lirnited strcigl-Lt-line coproximotron.The stepincrease changesomewhere an extremeexampleof such a changeand servesto illustratethe point. would seemthat thethirdYou expression.This is the casewhere a function is generallysmoothbut undergoes abrupt is depictedin Fig.I interpoloting to where splines ore superior higher-order A visuolrepresentotion o situotion of (c) ol on polynomiols.

I intervals. The conceptof the splineoriginatedfrom the drafting techniqueof using a thin.l l r o l g .the name "cubic spline" hasbeenadopted polynomialsof this type.Ji .(x). abruptchanges..In this technique.ri) where ai is the intercept. . drafterplaces the paperover a woodenboard and hammersnails or pins into the paper(and board) at the location of the datapoints. 16.2 for a seriesof five pins (datapoints).The process deis picted in Fig. -nio o hc l r u e .eachfunction is merely the straightline connectingthe two points at eachend of the interval.1 6 . for In this chapter.This is followed by materialon the cubic spline. As such. ui-- (16. there are n .2) and b. Each interval i has its own spline function. which is formulatedas (16. is the slopeof the straightline connectingthe points. r6.3. l i s< o le d o ' " o ' u r o l s p i n e e oui minimum.simple linear functionswill first be usedto introducesomebasic conceptsandissues associated with splineinterpolation.the splineusuallyprovidesa superiorapproximation the behaviorof of functionsthat have local. Then we derivean algorithmfor fitting quadraticsplinesto data. flexible strip (calleda spline) to draw smoothcurvesthrougha setof points.A smoothcubic curve resultsfrom interweavingthe strip between the pins.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 . .3) . 2. I 6. we describeMATLAB's capabilitiesfor piecewiseinterpolationincluding its ability to generate splines. Ji+r . Hence.which is definedas ai: Ji (16. For linear splines. . s.r) si (r) : ar I bi(x . .2 T I N E A R P T IN E S S The notationusedfbr splinesis displayedin Fig.For n datapoints (l : I. n).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 the most common and useful version in engineeringand science. Finally. 2 L I N E AS P L I N E S R 36r FIGURE6.e n s.

2)into Eq. E X A M P L6 .l.I intervols ond n dcio points gives where li is shorthand l(r. I 2 3 A Dototo be lit with splinefunc]ions.4)indicates to that the linear spline amountsto using Newton'sfirst-orderpolynomial[Eq.).0 --::-------:t.3 I l \ o ' o ro n u t e dl o d e ' ' . (.ti ) (r6.r .O r d eS p l i n e s r Problem Stotement. (16.5)] interpolate within eachinterval. (15.0 . The data can be substituted into Eq. for intervalfrom x : 4.. (16.l .4.r .. (16...f.Evaluate the functionotx:5.4 90 25 I0 2..5 . the second sr(x ) : 1. _ f. TABIE l6. Substituting Eqs.5 0. I IE F i r s t .r ) : .5 Solution.4. is by first locating the interval within which the point lies.0 + 25-1. 30 45 7. -r L)----l' rirl-ri f. l ix.51 1. the function is For exarnple. Fit the data in Table 16.I with first-ordersulines.4) Theseequationscan be usedto evaluatethe function at any point betweenx1 ard.5 to x :7.1)and (16. (16.3) for Ji (. es o l ' ^ e s Notice thot therecre n .362 SPTINES AND PIECEWISE INTERPOLATION J(.Inspectionof Eq. Then the appropriate equation usedto determinethe function valuewithin the interval.4) to generate the linearspline functions.tt attttrr_ FIGURE6.

5.I6. s . . 16.and the resultingfirst-ordersplines a r ep l o t t e di n F i g .o )L i n e o rp l i n e(. ng The equationsfor the other intervalscan be computed. the data points where two splinesmeet at (called a knot).the following sectionprovides an applicationwhere linear splinesare useful. Beforedoing that. the slopechanges abruptly.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 .as will be discussed subsequently.t .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{.-l Visual inspectionof Fig.4 .5): s-r ( x ) : 1 . This deficiency is overcomeby using higher-order polynomial splinesthat ensuresmoothness the knots by equatingderivativesat these at points.In formal terms. 0. 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 .the first derivativeof the function is discontinuousat thesepoints. 2 . 3 . l 6 . 0 -4.( 5 1 . 4 c tT h e v a l u ea t x : 5 i s 1 . 5 l. 0 + .4aindicatesthat the primary disadvantage first-order of splinesis that they are not smooth. .In essence.2 TINEARPTINES S 363 FIGURE 6.

In thesecases.. to determine thanthevalueatthetopoftheinterval. Before searching.the sequential it must searchthrough all the precedingpoints to find values. sort is inetTicient because For situationsfor which thereare lots of data.For example.Forcaseswherexxisinthesecondintetval or higher.interpolation yr = 1.Here is an M-file that performsa binary search . variThus.this will not testtrue at first.15. so. more generalapproach would be to passin vectorscontainingall thedata A and have the M-file detenninethe bracket. we know that the unknown falls betweenthis value and the previous is Here a that we examined.<. In this casethe counter i is incremented one by intetso that on the next iteration. end i = i + 1.For datain asbeing cendingorder. 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 .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.2. y. < r(1) xx > x(n) (' Inter1. end ? linear .As the name implies.in can as which casethe loop is exited. there are two simple ways to find the interval.x(i+1). or a For ordereddata.It is useful tbr variables.First.At this point. simple a followed alternativeisthe binan.= x(i + 1). rr) cngrn'X). the M-file would perform two tasks.This is called a table lookup. The loop is repeated until the xx is lessthan or equalto the interval'supperbound. A r r r [ ] l s . it would search independent the able vector to find the intervirl containingthe unknorvn. break. we move to the next value and repeatthe comparison. error trap is included ento an dependent hex .xx. this simpleM-file that accomplishes objective: functior-i n if xx error yi = Tablelook(. The first is called value sequentialsearch.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.search.lf not.olation outside search range' ) end 13 seclLrential i . the inter-polation be performedsimply shown.(i) + (y(i+1) y(i))/(x(i+1) x(i))*(xx x(i)).xx is comparedwith the value at the top of the second val.this methodinvolves comparingthe desired with each elementof the vector in seqnence until the interval is located. variablesare storedin ascending order in the arrayx and the The table's independent variablesstoredin the arrayy. whether isless it the compares value at which the interpolationis desired.this can be doneby testingwhetherthe unknownis lessthanthe value one lf assessed. One way to do this would be to passtlreM-file to the temperature which you want the interpolation be performedalong with thetwoadat joining values.suppose that you would like to set up independent and dependent teman M-file that would use linear interpolationto determineair density at a particular peraturebasedon the datafrom Table 1-5.r. while (1) if xx .1.Then it would performthelineat in interpolation usingone of the techniques described this chapter in Chap.

(rL) ) .rbleLookBrn (x. 10 0 5 0 o l . t'1" / (x(iL+1)-x(iL) )*(xx :.olation outside zx search iu (1) iU rL . u) / z).300) 0. : r 3 1 . either the lower or upperindex is redefinedas being the betweenthe upperand the lower middle index.Although third and higher .s70s : To ensurethat the nth derivatives contiDuous the knots. i l.1. 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. \'..the index at the rridpoint r1. rvh i le r.r = t 4 0 0 2 0 5 0 1 0 0 1 5 0 2 0 0 2 5 0 3 0 0 . the lower index lies at the lowel bound of is the interval containingx:.5705 This result can be verifiecl by the hand calculation: : * I (350) 0.n) €irror: ( 'InterI.the loop terminates. > . Q 9 .' >> TableLoo]<Bin (T.The sequential the searchwould be similar.Third-order be or that ensure and secondderivativesare most frequently used in practice. > > d e n s i t v = f 1 . 6 1 6 . range') end % binary l.At this point.The process repeated is until the difference index is lessthan or equal to zero. 2 9 I .a splineof at leastn * I are at polynomials cubic splines continuous llrst order r. ilnd Here is a MATLAB session illustratinghow the binary search function can be applied to calculate air densityat 350 "C basedon the datafi'ornTable l-5.= xx l.3 SPTINES QUADRATIC os?5-o615 . < x(1) | zx > x(. xx) Lenql-h (x ) .l5 O l 0. 5 : 1 . break.f rf tL/L:'tY((tt < x(iM) rT = rI{: t:lse a:Ilal end % Linear yi 1i inLerpolati.on . 6 1 1 .616 1ffi(3s0 I6. 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. 2 I . en-l t.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. Dependingon where it lies. ' .densit\'. the linear interpolation pertbrmed.nust used.:.Justas in bisection. .I 6 .l 4 6 . The approachis akin to the bisectionmethodfbr root location. 9 4 6 .

The function must passthrough irll the points.6) EX Therefore. .lt.consequently.t i ) f c i ( r i + r . The objectivein quadraticsplinesis to derivea secoud-order polynomialfor each interval betweendata points.the constantin eachquadraticmust be equal to the value of the dependent variableat the beginningof the interval.filbi\i+r . 1 x. . has to The function valuesof adjacent polynomialsmust be equalat the knots.For n datapoints (i : 1.This resultcan be incorporated into Eq.hl : fi+t (16.r i + r ) f c . t.-1 i + r ) 2 1t (1 6 . or are 3rr to the These canbe developed fbllows: as l.rportance.l).the nurnberof condiwe tionsto be evaluated now beenreduced 2(n .These illustratethe conceptof splineinterpolationusing second-order dratic splines"havecontinuous first derivativesat the knots. n).t .3. 16. .7)simplifies to .l) unknownconstants d's.Although quadratic splines are not of practicalin. .2.\. n n f l .Eq. . b's.(r ) : l. they servenicely to demonstrate generalapproach dethe for velopinghi gher-order splines.r.-r.fi : ai I b.(. f i + r * b i + r ( x i + r .(16.t... S i n c e h i sa m o u n t s t o t n-l t c o n d i t i o n si .1 (the intervals and.r. .xi) * c. .5): .The polynonial for each interval can be represented generally as s i ( r ) : a i t b t Q .366 SPLINES AND PIECEWISE INTERPOLATION when usingcubicsplines.x i ) 2 : .r .)l 2. a 1 ( . therearen . andc's)to evaluate. are Because derivationof cubic splinesis somewhat the irrvolved.This condition can be writterrfbr knot i -F I as . .x i ) * c .)l which sinrplifies to ai : Ji (16.we havedecided first to "quapolynomials. .) f ri (. equations conditions required evaluate unknowns.It mathematically can be expressed its . m e a n st h a t t h e r ea r e 2 ( n . 3(n Therefore.(ri .. ( 16.7 ' ) : n 1 remaining conditions. .r . 7 ) This equationcan be simplified nrathematically defining the width of the ith interby val as hi :xi+t-xi Thus.r i ) 2 (16.fi I bihl -t c. derivatives be discontinuous can they usuallycannot detected be visuallyand consequently ignored.This is called a continuitycondition. .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. Note that because have detenninedone of the coefficients.: 1 .( n .5) wherethe notationis as in Fig.1 ) .x.

(16. 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.r . ratherthanin thejagged fashionthat we saw for the linear splines.Equation(16..we selectthe fbllowing condition.I (Table16. must make an we arbitrarychoiceto successfully computethe constants.This meansthat thereis n . Assumethat the secondderivativeis zero at the first point.we havefourdata pointsand rz: 3 intervals. l ' z * b 2 h 2. 367 The first derivatives the interior nocles at must be equal. after applying the continuity condition and the zero second-derivative condition. to the problem.xi) The equivalence ofthe derivatives an interiornode.r : 5. 5 14:9. 16 EXAMPLE . Although thereare a numberof different choicesthat can be made.-?s l. Use the results estimate valueat.| .8)iswrittenfbri:1 through3 (with cr : 0) to give .?..2 conditions.h. Solution.2) : I remaining condition. because meansthat adjacent it splineswill bejoined smoothly.2 Q u q d r o i i cS p l i n e s Problem Stotement.9).:o5 h t : 1 .l).9-1.0:2. (16. Fit quadratic splinesto the samedataemployedin Example16.0 .5)can be ditferentiated yield to : sj(.filbrlt1 :7. Eq. Writing this equation for all the interior nodes amounts to .r1 bi l2cii. :4 . 5: 2 . Unlesswe have some additionalintbrmation regardingthe functionsor their derivatives.This is an importantcondition.lcfi\:7.3 AUADRATIC SPLINES 3.this meansthat2(4-1)-l:5conditionsarerequired. Equation( 16.(n .l c 2 h i : f t -) Jt lb.4 . For the present Therefore.5)is 2c.I6.i f I can therefore written as at be bi *2ciht:bi+r 116. 0 . Continuityof derivatives.\ { ll: I '\ conditions(again. 0 l. Because secondderivthe ativeof Eq. J z: 1 .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?.creirtes additional .1 4.v.

6 . r ) : a i ' l b . can be substituted the into original quadraticequationsto developthe following quadraticsplinesfor eachinterval: sr(x)-2.Cubic splinesareprefered because they provide the simplestrepresentation exhibitsthe desiredappearance smoothness. 16.1 .4 CUBTC SPL|NES As statedat the beginningof the previoussection. r i ) 3 (r6.4b. .The shortcomings Iinearand qr"radratic of splineshave alreadybeendiscussed.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.7 . The cubic splinesin thenext section do not exhibit these shortcomings and. 5 ) r .s 4 . 16. 6 ( r . 2 . . 1 ( x:) 2 .7 . 6 4 ( x . : -1 h.-)) .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:.. 1 ) . r 1 ( 5 ) 1 .1 .l 6 j ll fl [Ii'i' {l} These results. 16.r.cubic splinesare most frequently used in practice.we use.. to Consequently. A A. f o r n d a t ap o i n t s ( i : 1 .5-(x-3) t s .. 0 ) r Because : -5lies in the secondinterval. l) conditions required their are for evaluation. ( . 2 ( .. ) * c i ( . r. -. t h e r ea r e / . r i ) 2 + r / . as a consequence.6). . 0. x : . r . along with the values for the a's (Eq. r ): 1 . 0 ) .r . 0. bettermethods are for splineinterpolation. they tend to exhibit the instabilities Quarlic ol higher-ordersplinesare not usedbecause inherentin higher-order polynomials.r0) T h u s .l ) u n a known coefficients evaluate.. 5+ 0 .1 . 5 + 2 .( . that of Tlre objective in cubic splinesis to derive a third-orderpolynomial for eachinterval betweenknots as represented generallyby s ..5 4 .+ . 6 6 : + The total quadraticsplinelit is depictedin Fig.4(.r2to make the prediction. 5 1 2 9 . . 1 ( ._ r . . ( . r. . ( . r .J . s ) 0 . r . r . .I i n t e r v a l s n d 4 ( n . . ( .

4.1 Derivqtionof CubicSplines As was the case with quadratic splines.11) x i ) * c i ( .the lirst condition is that the spline must pass through all the datapoints. For the following derivation. (16. ) l Therefore.We will describetheseoptions in Section 16. they are set up so that the functionspassthroughthe pointsand that the first derivativesat the knots are equal.fi (16.I equations.A very common approacl.the gain in efficiencyis well worth the effort. will lirnit ourselves natural we to splines. J'i: ai I bi6iwhich simplifies to ai : . This resultcan be incorporated Eq. two additionalconditionsare requiredto obtain the solution.For knot i * 1. r r . therefore.Tlrat is.ti) J^i bi(r * ci(x xi)2* di(-r -. t i ) 2 * d i ( x r.4 CUBIC PLINES S 369 The tirst conditionsare identicalto thoseusedlbr the quadraticcase.these last two conditions can be formulated in severaldifferent ways.t t ) 1 h l: .This greatlyenhances fit's smoothness.In that case.2).the constantin eachcubic must be equal to the value of the dependent variable at the beginning ofthe interval.conditionsare developedto ensurethat the secondderivatives at the knots are also equal.r.we had to arbitrarily specily a zero second derivativefor the first interval. Further.the simultaneous will be tridiagonal equations and hencecan be solved very efficiently. For cubic splines.1) condiare we tions needed evaluatethe4(n .4.v ): -l -. this can be represented as J ' i I b i h i . Once the additional end conditions specitied.r to assurne is that the second derivatives the first and last at knots areequalto zero. at For cubic splines.we will present altenrative an approach that requires the solutionof only n . 16.Although the derivationof this approachis less straightforward than for quadraticsplines.hencemaking the resultasymmetric. apply them evenhandedly both ends. In additronto these. 16.. we are in the advantageous position of needingtwo additionalconditionsand can.The visual interpretation ofthese conditionsis that the function becomesa straight line at the endnodes. f i + r (16.13) .I6.This is a much nicer outcomethan occurredfor quadraticsplineswhere rve neededto specify a singlecondition. the After theseconditionsare developed.x . It is given this name becausethe drafting spline naturally behaves this fashion(Fig.I ) unknowncoefficients.12) Next.l c y h i .2. we will apply the conditionthat eachof the cubicsmustjoin at the knots.3 (t6. to Whereasit is certainlypossible to developcubic splinesin this fashion. Specification suchan endconditionleads what of to is termed a "natural" spline. Two of the more popular are the clampedcondition anclthe not-a-knotconditions. in There are a variety of other end conditionsthat can be specified. would havethe 4(n . ( .10): into s .

1 6 ) The equivalenceof the second derivativesat an interior node.21) The inder of this equationcanbe reducedbl' |.xi)2 (r6. . ( 16.r-ti h.Equation(16.22)can be sr"rbstituted Eq.: f.r4) The equivalence the derivativesat an interior node. we can solveEq.:ci+l J11.-1 : .Jr-l -t ( 16.'l (16.1 c 1 --1 2 ( h i r . l).72) The index of Eq.^ -.i3) ro give into h? Ii I bihi -t lt2ci* -t c . f. * rJi .15) The second derivatives the interiot'nodes at must alsobe equal. ci (1 6 .17) d1 fbr : t u. (16.f'-r lli-1 --llC.14) can be difTerentiated yield to sj'(-r) : 2ci * 6di(x . 1 5 ) o g i v e c id r b i + t: b i I h i k i i ci+r) (r6.(x .20) Equation(t6.r * c .23) (16. hi .19) can be solvedfor .) ( 16. (16. hi.1) : f i * t (r6.I * I can therefore be written as of bi l2ciht +3dihl : bi+l (16. .12)is dift'erentiated to yield sj(x) : bi {2c.c^ i tc.23)and the resultsimplified Equations into to yield t I t 1 . I f 1 can thereforebe wntten as ci l3dihi : ci+t Next.20)can alsobe reduced l: by b i : b i . o.370 SPLINES AND PIECEWISE INTERPOLATION where hi:xi+t-xr The first derivativesat the interior nodesmust be equal..r9) ( E q u a t i o n 1 6 .Equation (16. ( 1 6 . 1 7 ) (r 6r 8 ) This can be substituted Eq. c .xi) ( I6 .rr) * 3tli7 . .r1) (r6.r * f t r r ( c i .t . . trC. .21) .i l / .h i ) c i * h . (16. 1 8 ) a na l s ob e s u b s r i r u r en t o E q . r : 3 1 I } .21)and (16.J ' fffr h.

flx. : si'(. t i-l ri "f[. 15. can solvefor the c's..15): sidearefinite differences right-hand / t r i .tz.4 CUBIC PLINES 37r This equationcan be made a little more conciseby recognizingthat the terms on the (recallEq.21) cients. . 0 fhc srrqtem is tridironnal and I .i + r . we can convenientlydefine an extraneous parameter in which c.rr) 0:2ct ( * 6 d 1 x 1. (-.b and d..ri.26) becomes c u . '.. r 1 ) Thus.r2.t * 3 d .18)can be usedto determine remaining Once this is done. 'n .l t r .3.. r]) ( 16. . the coeffiand (16. this condition amountsto settingc1 eQual zero. One common approach. (16.." .1 : c r r: 0 Thus.S I6.we set ('/? 0. r'f Therefore. As statedpreviously.:)0 :2c.I unknown coefficients.26) Recalling Eq..-t (16. tlt. r i ]' f. caseEq. .. i :2.assumes the that the secondderivativesat the end knots are equal to zero. naturalspline. to impose a zero secondderivativeat the last node. c2..2' which Equation (16. To see how these can be integratedinto the solution s c h e r n e .r.-t t 6d. 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.rn rl .-f.f[x:.x.2s) .Eqs.the two additionalend conditionscan be torrnulatedin a number of ways..-t 2(.E qc a n b e s e t t o z e r o a s i n 16l6) . Jt rl ri-rl Therefore.-t. 1[ .-t1 I Cn-l cil 0 3(.r-r. (16.( 16.17). we havetwo additional cr.2'7) .h)ci I hici+t: 3 ( .3 sinrultaneous we conditions..2a) can be written as /r1-1ci-1 2\hr-r * . tridiagonal equations with n results in n . . . (16.h.Eq.25) can be written for the interior knots. to The sameevaluation be madeat the last node: can s j j .l qolve (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 (.-z h.l /[.rr]) 3(flx. ( x ... .

The total cubic splinefit is depicted Fig.31l .5 Theseequations be solvedusingMATLAB witlr the results: can cr :0 ct : -0. 8 3 9 5 4 3 1 2 6 ( 5 4 .0 .5 ) + 0 .5)r s t 0 .0..l0) to developthe following cubic splines eachinterval: for s r ( .0)r .160456274 cz:0.t2tht-rhttI:l I : I -3ltt//l lrrr r. 2 1 4 t 4 4 4 8 7 ( 5 4 .214144181 dt :0. Fit cubic splines the samedatausedin Examples to 16.r. (16.r. 1 1 r s 2 ( x ): . 1 6 0 4 5 6 2 1 4 (-r 4 ..4 . in sj(.. .r -1.0) .r.4c. (16.1l)1.27 to generate setof simultaneous tl.0 f--)\ h 1: ! .is calculated as s 2 ( 5 ) : 1 .0 l+ : O.5 +0.0-1... t t Solution.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.l6.ll o // 1.r 7.0:2. r ).372 . s 6 6 -r .r .7. 1 8 6 5 6 5 2 7 2 1.0-1. can be substituted into Eq. t E q u a t i o n ( 1 6 . 0 : 1 .166539924(.*x.along with the valuesfbr the a's lEq. 5 hz:7.r .186565212 dz : -0. 1 2 7 7 . .re equa) tions that will be utilized to determinethe c cocfficients: hz) I | . 4 1 9 7 7 1 8 6 3 ( x 3 ) + 0 . (16. 2 1 ) n d( 1 6 . J . . valueat r : 5.r e 8 2.5 ht:9. 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 .3 . .1 16.r.0 .119711863 -1). r : 5 .1 .l ( 7 .166539924 b1: b2 : -1..5 . For example. 5 . 0.r) : 2.5 + 3 7 2 6 1 r 1 .s ) 0 .2 and ( T a b l e1 6 .1 .0 .2 . 5 ) 2.831)543726 c+ : 0 dt :0.. t 6 0 4 5 6 2 1 4 ( .022053232(x. The first stepis to employ Eq.-s Thesecan be substituted yield to Ir lr-5 L .2 Il l-l-l-167t. t r + 0. 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.5:2. 0 ) ' Tlre threeequations can then be employedfo computevaluesrvithin eachinterval.I 'llI o' I l:l I I I The necessary lunctionand intervalwidth valuesare 1t :2'5 fz : 1. 16.l{ii} 1'l rt.121156654 ..022053232 These results. 8 3 9 . 20. 5 ) 3 5 : L102889734. 0. which talls within the second the interval.

TABTE16. specimeansthat the samecubic functionswill apply to fying continuous third derivalives segments. l t h r o u g h 6 . The clampedcaseis setup so that the derivatives the endsareequalto zero. 1 t .tIhu 2c'..It hasthe additionalpropusingan ordinarycubic erty thatfor firur points. i:2.A third alternative to fbrce continuity of the third deis knots.This is due to the fact that the naturalsplinerequireszerir secondderivativesat the end knots.3 . Sincethe first internal knotsno longer eachofthe first and iasttwo adjacent represent thejunction of two different cubic functions.lore thespeciled Cicmped first .Notice how the the natural spline tendsto straighten as would be expected out because secondderivatives go to zero at the ends. at Clunlted End Condition. ' . 3 a r e s u m m a r i z e d i n F i l 6 .l.:Q .. / i 2 .3. interpolating in . . if zerolirst derivatives specilied. Firsi ond lost Equotions r : 1: Q .25t fbr the interior knots.the notit a-knotexhibitsmore curvature.4 CUBIC PLINES S 373 T h e r e s u l t s oE x a m p l e s6 . whereas cubic polvnomial has no such constraint.li respeci dt--rlvotlves first vely) ct the ond iostnodes.the resultingfit differs fronr that obtained usingthe third-orderpolynomial. 16.In contrast. o 1) l6 Figure 16.. naturalsplineis only one of several the end conditionsthat can be specified splines. . x1 . .4. at As expected. 6 . . These conditionscan be readily applied by using Eq.this caseis refenedto as the "not-a-knot" contlition. t r l .l h : t t . Hence.Sincethe splinealready specifies that rivativeat the second andthe next-to-last the tunction value and its first and secondderivativesare equal at theseknots.i 7 J .2 End Conditions Althoughits graphical basisis appealing. : 3 l .n2 .. . . lloto knol 2 h 1 t 11 ' f t 1 r ' . 15. r . l l . gressiveimprovementof the flt as we move from linear to quaclratic cubic splines.Although the cubic have also superirnposed cubic interpolating spline consistsof a seriesof third-ordercurves. splinewill level off or becomehorizontalat the the are ends.the the natul'aland not-a-knotcasesfollow the trend of the data points nrore closely.We to a polynomialou Fig. . spline fit for the clarnpedcaselevels off at the ends. . .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 . tt6. 2 . h r = 1 c . N o t i c e t h e p r o f l l g.. i<r.:I11. 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 .I6.For example. 1 a 2 h . .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.Because hasnonzerosecondderivativesat the ends.3 .they are no longer true knots.. 4 . : Q (rvhere cnd 7.it yieldsthe sameresultasis obtained polynomialof the sortdescribed Chap.4c.{ l t t * h : l c z * / z r r ' :: O hn tcn)-0t. for Two of the mostpopularare .5showsa comparison the threeendconditions appliedfo tit the datafrom of as ?rble 16. the 16. " Not-a-Knot" End Condition.

this option providesthe meansto implement the clamped-end condition. tion implementspiecewisecubic Hermite interpolation.The interpl function canalso implement spline and Hermite interpolation. spli-ne.374 AND PIECEWISE INTERPOLATION SPLINES f(x) 2 t 1 0 I6 FIGURE . y. Consequently.5 PIECEWISE INTERPOTATION MATTAB IN The spline MATLAB hasseveral built-in functionsto implementpiecewise interpolation.splir. can also perform a numberof othertypes but of piecewise interpolation.7) : f(x): I | + 25x2 . )<x) (16. doto from Toble 16 l I6. Ithas the generalsyntax. yy = spl ine (x. By default.However.4 1 S p l i n e si n M A T L A B Problem Stqtement.5.andyy = a vector containingthe resultsof the splineinterpolation evaluated the pointsin the vector as at xx.28) where x and y : vectorscontaining the valuesthat are to be interpolated. noturol for ond splines lhe Comporisonof the clomped (wilh zero firslderivotives). 16.5 nof-o-knot.re usesthe not-a-knot condition.1 MATTAB Function: spline Cubic splinescan be easily computedwith the built-in MATLAB function. in The pchip funcfunction performscubic splineinterpolationas described this chapter.then the first and last value in y areusedas the derivatives the at end points. EXAMPLE 6.if y contains more two valuesthan x has entries. Runge'sfunctionis a notorious example of a function that cannot be fit well with polynomials(recallExample r 5.

I.6..1)."2) . yy. a more finely spaced so plot of the resultsas generated with the spline function: >> xx = >> YY sPLine 1. I6.Employ (a) a not-a-knot splineand (b) a clampedsplinewith end slopes -4.IN I6. ' ) As in Fig.y . (i+25*x. linspace( (x. 'o ' . o f f i : 1 a n df i . (x. . xx..^2). (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. a vectorof valuescan be generared tFlat can crearc sn?ooai we Next. (a) The nine equally spaced datapoints can be generated in as >> x = >> ]1 L. xx. yr./ (1+25*xx. : Solution.xx) . 16.the not-a-knotspline doesa nice job of following Runge'sfunction without exhibiting wild oscillationsbetweenthe points.Finally. linspace(-I.9). l].5 PIECEWISE INTERPOLATION MATTAB 375 Use MATLAB to fit nine equally spaceddata points sampledfrom this function in the interval[-1.Y. we cau generate along with the splinefit and the original data: >> yr >> plot = L. Recall that I inspace automaticallycreates100 points if the desirednumberof points are valuesfor Runge'sfunction itself and display them not specified.6 FIGURE funciion(doshedlrne) spllnefit generoted wilh Comporisonof Runge's with o 9-poinlnoto-knot MATLAB(solidline).

'o' . This methodsetsthe valueof an interpolatedpoint to the value of the nearestexisting data point.xx.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. 1 (x. polynomials. 16.y. which can be thoughtof as zero-order ' I inear ' -linear interpolation. 1:. This methodusesstraightlines to connectthepoints.yc. The various methods are . . 16.2 MATTAB Function: interpl The built-in function interpl providesa handy meansto implementa numberof different typesof piecewiseone-dimensional interpolation. clampedsplinetendsto improve the fit.yr. v e c t o r c 0 n e :. >> yyc = spline(x.5. xi.'--') As in Fig. the results of the interpolationas evaluatedat the points in the vector xj. 'rneLhad t w h e r e x a n d y .It has the generalsyntax yi = interL. .xx /yljc.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 .7. e l . and taining 'methocJ' : the desired method.xx). . i e s ' e s p e c r .5 F I G U R EI 6 . 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 isolid-ine) r i g hb o u n d o .the clampedsplinenow exhibitssomeoscillationsbecause the artificial of In slopesthat we have imposedat the boundaries. 'nearest '-nearest nei-ehbor interpolation. ' EXA generate and plot the splinefit: >> yc = [1 y 4]. Thus. where we haveknowlthe edgeof the true first derivatives. >> plot (x. the interpolation looks like a seriesof plateaus. other examples.

Therefore. This is identicalto the spline iTItt"Ji una ' cubic '-piecewise cubicHermiteinrerpolarion.it will be moreaccurate the dataare valif ues of a smoothfunction.Rather.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. 16. tt. Note that you never decelthe erate during the experiment.\-n) ). the interpolatedvaluesdo not tend to overshootthe data pointsas can sometimes happen with cubic splines. Further the first derivatives the knots will not at be the sarneas for cubic splines. (a) The datacan be entered.tt. .8c)are not smooth.(c) cubic splinewith not-a-knotend conditions. >> vl >> plot (t.[0 20 40 56 68 B0 34 95 t04 110].as well asthoseinvolving the otheroptions.interpl (t.therearetrade-offsbetween spline and the pchip options.pchip has no overshoots and lessoscillation if the dataare not smooth. (b) The commandsto implementand plot the nearest neighborinterpolationare >> vn . >> tt = incerpl(t. nearest (b) to neighbor.''1r l Problem Stotement. is the The pchip option (short for "piecewisecubic llermite lnterpolation")merits more pchip usescubic polynomials connectdatapoints discussion..In addition. >> v = l0 2A 20 38 B0 80 100 10A \25 7251. = finspace ( O.'nearest' >> nl ol.r-t).v.' o'.However.vl) The results(Fig.ra test drive on an automobilewhere you alternately accelerate automobileand then hold it at a steadyvelocity. 377 'spL. You perfom.I6. On the other hand.5 Trode-OffsUsing in..they are expressly chosenso that the interpolation is "shape preserving.Thesetrade-ofI\.. e. EXAMPLE 16.it ditTers frorn cubic splinesin that the second derivativesarenot necessarily continuous. .'.5 PIECEWISE INTERPOLATION MATTAB IN .(t-.' o' ./. with linearinterpolation. with cubic splines.but do not exhibit any overshoot. tt.and (d) piecewise cubic Hermite interpolation. Solution.The results the of using spline will generallyappearsmoother because humaneye can detectdisthe continuitiesin the secondderivative. As to with continuousfirst derivatives.ine'-piecewise cubic spline interpolation. 110 ) .are exploredin the following example. If the 'method' argunrent omittecl. defaultis linearinterpolation."That is. plottedwith the fit and following commands: >> r .v.

tt.. 16. nor an accurate depictionof the underlyingprocess. v. Theseresults(Fig." . v s ) 'spline' ) .8b. t t . on funciionto performpiecewisepolynomiol nterpolciion o velocilyiime serles This option is neithera smooth As in Fig.However.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.severeovershootoccursat several severaltimes duringthe locations. .This makes it appearthat the automobiledecelerated . 16.8c)are quite smooth.8 Use of severoloptionsof the rnterpt toron ouiomobile. interpl ( t .the resultslook like a seriesof plateaus.' o ' . (c) The commandsto implementthe cubic splineare >> vs >> plot = (t.

F I G U R EI 6 . yt) . 16. 9 rv G ' o p r .t:.\/.V. nature.8d)arephysicallyrealistic. addition.6 MULTIDIMENSIONAL INTERPOLATION (d) The commandsto implementthe piecewisecubic Hermite interpolationare >> vh = >> plot interpl(t. l e ' n e o o e .r'r).'pchip').rethods one-dimensional for sionerl interpolation. ]r). this section.the velocitiesincrease and Although the resultis not as smoothas for the cubic splines.Wewanttointerpolatebetweenthesepoints "f(xr. As depictedin Fig.r':)...andl(. 16.f (xi..tt.r'r). results(Fig.we will describeMATLAB's capabiliIn ties for rnultidimensional interpolation. 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.6 INTERPOTATION MUTTIDIMENSIONAL problemscan be extendedto multidimenThe interpolationn.vh) 379 For this case.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 ..'o'.f(rr. 16..we have valuesat four points: yz). (t. the Because ofits shape-preserving rnclnotonically never exhibit deceleration.9.6. will describe simplest In the caseof two-dimensional we interpolationin Cartesiancoordinates. tt. continuityof the f irst derivatives the knots at makesthe transitions betweenpointsmore gradualand hencemore realistic.I 6. c o le p c l i o n o { o .1 BilineorInterpolotion 'lnao-dintensional interpolctrion deals with determiningintermediatevalues for functions of two variables z: .f(rz. I 6.

.r rJ r )+ S f . re. r .{l )z _ ).10.rrr)is .. r r .rr) f f(x. f( r 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 . poi the ) sult is a plane connectingthe points as in Fig.r ' r ): 3 f Jt andat (.J:) .2s) . . If we use a linear function. l { . i o l e s c o r t ^ e nb e . )'r . r .. v : ) . r .r _ -r': )Z (16 34 -{i ..rdiin rection. r ef n t i to estimatethe value at an intermediate nt / (-ri. -ri -t: ]i .t2 16. r ' . .rt t r : .'t): . 16.. the resultat (r. r o i m e n s .v: r'' -'t2 't '{i-xt '\'i ..9.ft..!r l l ' ( .r1) is .. for First. . (16.rt ): -. . 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 .t. - \'r 116.) rl ./ ( x .\'l \'.:) .311 A single equation can be developedby substitutingEqs.rl . 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 .fZ . .v r ) (16.30) into Eq.r. . .29) and (16.\'2 )'r . . v.r..r'r 12 -.r. we can hold the v value fixed and apply one-dimensional lineu interpolation the.31) to give -." X2 . h e .IO I6 Two dimensionol bilineorinlerpolction con be implemenled f irstopplyinq one dir-nensioncl by ' e o ^ l ^ ro c l o l i ^ no r o n q' n e . .-..r . . o r o d e . _vi.lr ^.e . r .rr ).Usingthe Lagrange form. : : } . 16.^ r r n e . . r r y-rV 2 r : ) ) It + " t --{ .r . . (16. o L e co . A simple approach developingthe bilinear tunction is depictedin Fig. . x2 .i .1-)l.Xt .. / ( x i .10) Thesepointscan then be usedto linearly interpolate along theI'dimension to yieldthefinat result: .Suchfunctionsare calledbilinear. r ' 1 )t }: lr \'' ) .f ' l.ri.

2r43 .2113 | 6.4. 'mechod' ) where x and y . linear.1) 5l .nethod argument omitted.2).5.I 6. 1 :)6 0 T(2. u-. xi.5./8 . zo : 6r.a simplerepresentation ate in a similar fashionto interpl (Section 16. if the. o f t h es y n t a x f i n e o 2 i s o zi = Lt\r-e(p2 lx.2 Multidimensionol Interpolotion in MATLAB piecewiseinterpolaMATLAB has two builrin functionsfor two.25. spline.. 2 5 .u 2-9 l-6 5 . As you might expectfrom their names.(16. 6 ) 5 ..y.and three-dimensional tion: interp2 and interp3. is For example.the defaultis linearinterpolation. yi.8.4.. that is.8) 5r . u-. j ) .interp2 can be usedto make the sameevaluation in Example 16.. Supposeyou have measured temperatures a number of coordiat nateson the surfaceof a rectangular heatedplate: T ( 2 .5 T(9. + -. zi : d matrix containingthe resultsof the interpolationas evaluated at the points in the matricesxr and yr.25-94.6):10 interpolation estimate temperature : 5.6as as >> X= >> y= >> >> zlnl- 2 9). erp2(x. 6A 51 . _ { .6 16 Bilineorlnterooloiion 381 Problem Stotement. 1 6l. As with interpl.55 701.32) these into : "/ ( ) . nearesf .6 . z.-6 + 9-2 t-6 s-}s-24R-l 5.6 MULTIDIMENSIONAL INTERPOLATION EXAMPLE .2 4).6.25and1'.8-l ss + -. Substituting values Eq.9 4o 8 .. a n dc u b i c .matrices containing the coordinatesof the points at which the values in the matrix z are given.5.Note that the methodsareidenticalto thoseusedby in*'erpi.2. and method: the desiredmethod.rr gives Solution. 2 5 . Usebilinear to the at.\ .6): 55 : T(:9. thesefunctionsoperFor example. y.

In particular. C)]. separated a plane called the thermocline.As depictedin Fig.The latter is also depth and temperature usedto determinethe heatflux for the casewherea = 0. Such stratification effectively divides the lake vertically into two layers: the epilitnnion and the hypolimnion. 16. natural cubic splines are employed to determinethe thermocline gradientfor PlatteLake.warm. dT az (r6. 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. Michigon. cv: an eddy diffusion coefficient (cm'ls). The location of the thermoclinecan be defined as the inflection point of the temperatur€d e p t hc u r v e .9 tl.8 4.3 2. The temperaturegradient is important in its own right becauseit can be usedin coniunction with Fourier's law to determinethe heatflux acrossthe thermocline: J:-DpC. .01 cm2/s.3). s)].33) where "f : heat flux [call(cm' . decompositionof organic matter can lead to severedepletionof oxygen in the isolated bottom waters. denserbottom water.382 AND PIECEWISE INTERPOLATION SPLINES . 2. Lakes in the temperatezone can becomethermally stratified duringthe summer. p : density (31 g/cm3).t 27.t h a t i s ."C Temperoture depthduringsummer Plotte versus for Loke. I6 FIGURE .11.9 228 9 I 2A6 137 t3I 1 83 11. r ('c) Epilimnion Thermocline Hypolimnion TABTE16. buoyantwater near the surfaceoverliescolder. H E A T R A N S F E R Bockground.7 22. t h e p o i n t a r w h i c h d 2 T / d z 2 : 0 .6 T. and C: specific heat[: I call(g .the thermoclinegreatly diminishesmixing between the two layers.I I Temperoiure depth versus for during summer Plotte Michigon Loke.3 22. Michigan (Table 16.. In this case study.2 ilt 0 228 .As a result. by Thermal stratification has great significancefor environmentalengineersand scientists studying such systems.

' a: : i a h .12.'dlii lrl - r r a r .1 . l t s .r) . i. end x n o t s t : . ."ivofives o noiurolspline..:i+nj-s (r lr + l.fEL I') Lal- h. .1t(r..'--.vaLiv-:r ii'e: a: ai ar.the built-in MATLAB it spl ine function doesnot meetour needs. (16. e s tf cle!)€:n.irrr.J'ialt:es i.r e n t : .=: r. l.1: ri aiirr i-iat ir II Il' - anr. I .i j ndepenLlenL ricpenrlelt ..r .f \. t-he varues oi the ::. i) cr{:L)) .t € f ilo l. . : b r e s .lrtl:'.1 ire xx) uses .because usesnot-a-knotend conditions.we setup and solveEq.27)for the second-order (c). x.Note fhot fhe oif f with {unclion employedfor errortroppingis describedin Seclion 19.th Le:: i- i. e n d '. want to usenaturalsplineend conditions performthis we to analysis. :c o n t i n u e d . / l.hi-. -.r!{t-h' ) . i)). i-ol c i:oetficier. . n d d r:c-el-fi.r. After somepreliminaryerror trapping.ly rrr:st be same L. r j i f l . b . r L 1 ( . y . l \ . .12 MJile fo deiermineintermediole voluesond de. i 1. +-lj t. t : . fci(j.Unfortunately.y.j e J .'(x. s.c . Solution.a l ues der. ' ar i . ?solve i ll.. 1 ..r r .. r s c e n c r r : r r ') ./ 3 / lilx. / i i).i.y. * 1l-rili + i') v)). Sucha codeis shownin Fig. / \ . it is not difficult to develop our own M-file to implementa natural spline and retum the derivatives.2 CASE STUDY 383 " . I 2 * r l r{ x .-xril: : a)n la.. r b i c rtnderlving r :pLtne furr:Lion l. il i).) f r f r d ua t t r:s in j.ts : " S O l . However.ttf ie : rl .-. .the sp1 ine functiondoesnot returnthe first and secondderivatives we require for our analysis.i c " L y . . . * (2 * c(:) = c1i - 1)) (continued) .yector The vecLcf xx.-':'. Notice how coefficients FfGURE r6. r -a .h(x. lc .. L) r x. t. As just described. . xr .{){ r:: iirsi ::=i:.- r " : i! : ] . r l spi. 16.: i p l i n e 1r . . hlr:. . . l n .i r:e.r.] 6.. i 1.7. Further. . slle.ri e.

h and fd. b.000001 yyy-a (1i) +b ( ii) * (xi-x (ri) ) +c (ii) * (x!. and d). A loop is thenemployed t0 generate other coefficients(a.. xr ) if .2(i)l n. xx(:.)).f"{x1 :2ci + 6di. (16. splineTnterp. i ) * ( x l .x (ir ) ) ^2+o( ii) . 0. . c. n. d2y-2*c (ii ) +5*d (ii) * (z.6 13.384 SPLINES AND PIECEWISE INTERPOLATION coniinued I .(ii xi >= x(ri) x + i) + 0.00U001 & xr <_.i-x (ii ) ) . d. a. end enC frrrlctfon hti = 11*. we have all we need to generate intermediate values with the cubic equation: f (x) : ai * bt(x .l 17.8 22. i) hl'r = x(r + i) .L1. b.h 1 (x.y(j)) fcld .) * 3diQ .3 4. the At this point. ?i l inqnaeo(z(1\.8 20. for end iun -l n i. x .1:m %perforn interpolaLions at desired values .12.x(i).Once Eq. to determinethe values and the derivativesat the desiredintermediate values. we solve for the c's with back division. 16. 1) .t2 lContinued) we use two subfunctions. .7 IB.3 22.7 I1 .Splinelnrerp(x.x { .t) 27.21. enC function lyyy. i ) ) ^ 2 .x ( i i ) ) + I * r l ( i i ) * ( x i . b. .xi)3 We can also determine the lirst and second derivatives by differentiating this equation twice to give '(x) : bi * ci(x .(y(i) r' (. br eak end end end FIGURE t6.9 17.dy{r).r(i) x(j)).8 22. Here is a script file that usesthe naLspl ine function to generatethe splineandcreats plots of the results: z = i0 2.27) is set up. . these equations can then be implemented in another subfunctign. . 1 c ( i .. dyy.rr) + ct(x * x)2 * di@ . = 5 1i I ) + .x.x. a. d. r=122.xi) f As in Fig. *(xi x(ri))^3.C'J fdri. d2yl -gp] inelnterp f nr i i 1 . to compute the required finite differences. c.d.zi . lyt'(r).)z .1 13.9 9. * d y i .

1.. the thermoc\ine appearsto be \ocated at a depth of about 11.5 0.35 m where the gradientis .61'C/m.I6.zzt c 1 z ') .!rlot (d't2. FIGURE6.n. ieqrei.' l " rcvc!-s /.T.Pioi ' l. We can use root \oeation (zero second denrative) or optimization methods (minimum fust denvative) to refine this estimate.. zz) I )... zz) As in Fig.The result is that the thermocline is located at 11. z. zzJ .5 . (T.I3 T Plots {o) temperoture..-t- .'reverse').The rhermocline islocoted ot the in[lecrion point o[ ihe temperoruredeplh curve.i. g'odient..pfor(ai.7 CASE STUDY " continued natsptine \2. \6.13.ll'ciaca'.TT.5 m.grid . ( a )T \bl dr/dz (cl dzTtdz2 _L______ 10 10 . ond (c} secondderivotive uers.- 10 \ t5 ----F------ F_ I 15 -r-----I I I I I t5 rI I L 20 I 20 zv 25 25 25 30L 10 20 1n 30 1 -2 30 -0.' a'.s depth (ml generoted with of {b} the cubic sprineprogro.9r-!v / ..

16.5 depi x PROBTEMS 16.r range.846 2t 703 r6. In ea whic f 16.If k:0. Here are some valuesthathave beengenerated intervalsof 0. the point at which d2Tldzz:0. 16. tank can be idealized as the f $) ]t 692 12382 t7.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.9)2*0. (b) a cubic spline with not-a-knotend conditions. cm .K . The depth of the thermoclinecan be defined as the inflection point of the temperature-depth curve-that Fit this datawith a (a) cubic splinewith not-a-knot conend is. m Temperoture..0rt*' * r4 . such.04 -0 The humpsfunction exhibitsboth flat and steepregions ovet a relatively shon.t6rI) * . At this depth. 1 7 6 1 5 4 7 t 45 887 96 500 47 448 19400 06 07 08 0.'C 0 05 7A 7A I 55 t5 22 2 25 13 l0 3 l0 t2 r ftt) x 02 03 4.6 natu cate t6.7 orde 15.l Given the data to Use a clampedcubic spline tit with zero end derivatives determine thermocline the depth. In flux from the surface to the bottom layer can be computed both cases..2 A reactol is thermaily stratified as in the tbllowing table: (x-0.: .the heat ditions and (b) piecewisecubic Hermite interpolation. alsois anexample numerical of differAs how numerical approaches differentareas beused from can entiation.t3 'a * r-:"1^* (.3)2*0.01 cal/ (s .* g.19. d r J data with (a) naturalend conditionsand (b) not-a-knot end az (a) F ditio (b) F slopt tiatir 16.8 and Thes evalr .000 two zones separatedby a strong temperaturegradient or thennocliue. n 2ca -l -0.9 I Based on these temperatures.01 (x-0.4 x Depih.386 AND PIECEWISE SPTINES INTERPOLATION continued with The gradientcan be usedto computethe heat flux acrossthe thermocline Eq.create a plot comparing the fit with the exact with Fourier's law: humpsfunction..However. 16. "C) compute the tlux acrossthis interface.^l=.3 The following is the built-in 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.33): J: 8 6 ' 4 o :0 s ' 9 .We will be describing topicof numerical for the differentiation in detailin Chap.(16.and (c) piecewisecubic Hermite interpolation. I over the rangefrom.4 Develop a plot of a cubic sptine fit of the following .it illustrates in tandem problemsolving.r = 0 at to l: Fir tl ditio tions case 16.4 05 O 0l 5 .

9 The following data defines the sea-levelconcentration of dissolved oxygen for fresh water as a function of tempelature: 200 T.(b) a fifth-order polynomial. develop a plot using (c) piecewise In cubic Hermiteinterpolation. Fit this data using (a) cubic spline with (b) not-a-knot end conditions. are olien compiled in standald . Presentyour resultsgraphically. 16.456i 27. (c) piecewisecubic Hermite interpolation.8 Besselfunctions often arise in advancedengineering scientificanalysessuch as the study of electric fields.5683.14 3. mathematical tables.o I Fitthis datawith a (a) cubic spline with not-a-knotend con(b) ditions. a 16.[n each create plot comparing the fit with the step function.6 Developan M-file to cor.I : in -t /(r) 0 0 -02 0lrt 02 0.)24 (a) this data with a cubic splinewith not-a-knotend conFit ditions.t from r : 0 to 2n.Display the resultsgraphicallyanduseeachapproach estilnate to o(27). Createa plot comparing the fit with the tunction.7 The fbllowing data was generated with the liftlr: polynomial:.3.556 24 A 5202 26 A 47OB Ineach case.at "t : 1.069x .and (c) piecewisecubic hermite interpolation.l). : approximatioll. (b) (a) Repeat but useclampedend conditionswherethe end slopes set at the exact values as determined by differenare liating function.l4.and (c) a spline. 16.9125xrorder * 15.986 mg/L. { ) : x -e -r!r t')rr Estimate polynomialand Jl2.'C 0 B t6 24 32 40 o.Note that the true value is 0.5. case.018515 0.ll Runge's functionis writtenas I " |*25rt Generatefive equidistantlyspacedvalues of this function over the interval: [. /(r) 2 172 4 22O 5 430 4.true) for each. 16.l: + r135679 1000 x v 0 2A 2 2A 4 12 7 7 l0 6 l2 6 Determinethe value of 1.Test your code by using it to dupliend ExampleI 6. addition.912 9. 1].npute cubic spline 11twith a natural conditions.For example. colnpareyour plot with the following equation which usedto generate dala: was the I"{ . (a) using an interpolating (b) using cubic splines. Fit this data with (a) a fourth-order polynomial. therelbre.10 (a) Use MATLAB to fit a cubic splineto the followins data: 16.5 following data is sarnpledfrom the step function The depictedFig.PROBLEMS conditions. (b) a linear spline. cubic splinewith derivative end conditionsequalto the exactvaluescalculatedwith differentiation.f(x) 0. the 16. 16. Developplots of eachfit aswell asplots of the absolute error (8. 387 . and (c) a cubic spline. cubic spline with zero-slope clampedend condiand trons.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. but with zero first derivativesat the end knots. and These functionsare usually not amenable straightforward to evaluation and.411. cate 16.(b) Repeat(a). Note that the exactresultis 7.mg/L 1462) IlB43 IBZ0 B4lB 73A5 64)3 Use MATLAB to fit the data with (a) piecewise linear interpolation.12 Use MAILAB to generateeight points lrom the function / (/) : stn. l6.

an integralcan be envisagedas lsummation.There you learnedtechniquesto obtain analytical er exact derivatives and integrals. integrationcan be usedto determine the distance lraveled: v(/) : fr' dr u(t) As in Fig.1 I d b .Among other 389 .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.the derivativecan be visualizedas the slopeof a function.Juqt as differentiation usesdifferences quantify an into stantaneous process. you were introduced to differential and integral calculus. for functions lying above the abscissa.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. PTs.. Mathematically. Infegrationis the inverse of differentiation. Because oflhe close relationshipbetweendifferentiation and integmtion. integrationinvolvessumminginstantaneous information to give a total result over an interval. PT5. t 'ii.'i:'.Thus.: '.lb. Consequently.just as a clerivative can be thoughtof as a slope. l.1 ovERvtEw In high school or during your first year of college. we have opted to devorethis part of lhe book to both processes..'#i$k*tffi#*ri lntegrotion o nd Diff. we are given a functionXt) that if specifies object'spositionas a functionof time.1n.rentiotion 5.. For example.*"'!. iI-we are providedwith velocity as a function of time.

4. andSimpson's318 integratedis evenly spaced.een differentiotion {b) integrotion from things.I (o) ond The confrost behr. althoughnumericaldifferentiationis not as widely employed. part because is much in a less sensitiveto roundoff errors.it makes it does have great significancefor the solution of differentialequations. integrationrepresents more highly developedareaof numericalmethods. Although differentiationis taughtbeforeintegrationin calculus. Hence.this will provide the opporlunityto highlight their similaritiesand differences ln to a numericalperspective. All theseformulasare designed cases for 1l3 rule. Simpson's in widely usedNewton-Cotes wherethe data rule.In addition. tabulateddata with a simple polynomial that is easy to integrate.Finally. we havealreadyintroduced in the following clrapters.we reversetheir order First.Three of the most or formulasarediscussed detail: the trapezoidalrule. in sense cover it as the last topic prior to describingdifferentialequations Part Six. do this for several We reasons. addition.390 PART INTEGRATION 5 AND DIFFERENTIATION 0 ) 400 200 0 tal 0 FIGURE PT5.the materialwill haverelevance the next paft of the book where we will cover differentialequations. we also include a discussionof numerical to be .2 PARTORGANIZATION for Chapter 17 is devoted to the most common approaches numerical integration-the function Theserelationships basedon replacinga complicated are Newton-Cotes formulas. Second. in it you to the basicsof numericaldifferenltiation Chap. to 5.

17. At the end of Chap.fferentiation suppleto ment the introductorymaterialfrom Chap. The effect of errorson both numericaldiff'erentiation intesrationis also discussed.ORGANIZATION 5. addition.where tlre function valuesat the ends of the limits of integrationare known. 17 can be employedto analyzeboth tabulated data and equationsChapter18 dealswith two techniques areexpressly . equations Computeralgorithms providedfor both of thesemethods. wherethe integrationlimits extendbeyondthe rangeof the known data. that designed integrate to and functions:Rombergintegrationand Gaassquadrature. and the differentiation of unequally spaceddata. Topicsincludehigh-accuracl. 19. Richardson extrapokttion. we presentadditionalintbrn. In are ln Chap. and . Although they are not commonly usedfor definite integration. we presentopen integrationfonnulas.open integrationformulas here because are presented they are utilized in the solution of ordinary differential equations in Part Six. finite-dffirence formulas. The formulationscoveredin Chap. All the above materialrelatesto closedintegratiott. This is a very important topic becausemany realworld applications deal with datathat is in this forrn.adaptiveintegrationis discussed.4.ration numeric:al on di.2 PART 39r integrationof unequally spaceddata.

' .0.i \''l n rr rrr .' : rotion NumericolInteg Form los u CHAPTER OBJECTIVES The primary objectiveof this chapteris to introduceyou to numericalintegration. the ' ' . Knowing how to implementthe following singleapplicationNewton-Cotes formulas: Trapezoidal rule 1/3 rule Simpson's Simpson's 3/8 rule Knowing how to implementthe following compositeNewton-Cotes formulas: Trapezoidal rule 113 rule Simpson's Recognizingthat even-segment-odd-point formulaslike Simpson's1/3 rule achievehigher than expected accuracy.14 .:' .unn 8a.. YOU'VE GOT A PROBTEM h ftf l\ ecall that the velocity of a free-falling bungeejumper as a function of time can be computed as ( . Understanding differencebetweenopen and closedintegrationformulas.f ') : uu\ . Specific objectivesand topicscoveredare . Knowing how to usethe trapezoidal rule to integrateunequallyspaced data.) V . .'. Recognizingthat Newton-Cotes integrationformulas arebasedon the strategy of replacinga complicatedfunction or tabulated datawith a polynomial that is easy to integrate. .

1i)n t o E q . evaluated betweenthe limits x : a to . . 1 1 7 .to indicatethe total amount.Calculus can be usedto solveEq.Furlher. parts.r." Mathemafically. I7.3)for f '"[. suppose that there was some way to measure jumper's velocityat varioustimesduringthe lall.it as would also be possibleto integrate discretedatato determine distance. This distance be evaluated integration: can by fl :(r): / t'(ndr . or summation. Thesevelocities the alongwith their associated times could be assembled a table of discretevalues. definiteinteglation is represented by t: f a^ l. the term quadratureis generallytaken to be synonymouswith numerical definite integratior.4) Although a closedform solutioncan be developed this case. 2 ) i v e s g z(t): . the symbol / is actually a stylizedcapital S that is intendedto signify the closeconnection betweenintegration and summation.I INTRODUCTION AND BACKGROUND 17.2) g S u b s t i t u t i nE q .-r.5) corresponds the area under the by to curve of l (x) betweenr : a andb.to (1j. As suggested the dictionarydefinition. (17..For functions lying a of above the x axis. . (17.r. Numerical integrationis sometimes referredto as quadrature.I INTRODUCTION BACKGROUND AND 393 Supposethat we would like to know the vertical distancez the jumper has fallen after a certaintime r.ln this situation.r : b.1 Thr.the "meaning"of Eq.rs.8""n( .thereare other funcfor tions that cannot be integratedanalytically. integrationprovidesthe meansto deterrrrine distance the fiom the velocity. This is an archaicterrn that originally meantthe construction a square of having the sameareaas somecurvilinear figure. ( 1 7 ."'n( .. Figure 17.I Z.into as a whole. 5 ) which standsfor the integralof the function f (x) with respectto the independent variable ..o {. are to Chapters and l7 l8 will introduce vou to someof thesemethods.1 represents graphicalmanifestation the concept. to integratemeans"to bring together. (17.l Whof ls Integrotion? According to the dictionarydefinition.to unite.r) (r 7 . I. both these the the In instances.5) is the roral by value.l. In fact. the integral expressed Eq.' ( I 7. Today. .l*. numerical integration methods availirble obtainsolutions.of /("r) dx overthe rangex : a to b.)0.E')l (t7.

r.2)1..394 NUMERICAL INTEGRATION FORMULAS I . by 14s2n.utax D-A .6) n where -yiare individual measurements. you might also be interested computingthe mean or average the continuous in of function : / (x) for the interval fron a to b.thereare an infinite numberof valuesbetween and a b.as specified by . lhe enl is t t l o D T h ei n t e g r o l FIG Exo nee kno nonl 17.3b. in In contrast. 17.3a.r is a continuousfunction of an independent variable as .1.suppose that .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. is it used to calculatethe center of gravity of irregular objectsin mechanicaland civil engineeringand to determinethe root-mean-square currentin electricalengineering. Integrationis usedfor this purpose.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. .1) This formula has hundredsof engineeringand scientific applications.2depicts few a caseswhere integrationis usedtbr this purpose. Just as Eq.Many specificexamplesof suchapplicationscould be given in all fields of engineering A and science. Recall that the meanof discrete n discrete of datapointscan be calculated tEq. Other common applications relateto the analogybetweenintegrationand summation. determination the meanof discrete The points of is depicted Fig. 17. For example. number of examples reIate directly to the idea of the integralas the areaunder a curve.6) can be appliedto detenninethe mean of the discretereadings."- (11. l'! f t.{ FIGURE7.l Mean: i.For this case. (17.For example. (i3. depictedinFig. Figure 17.5r t17.a common applicationis to determine meanof a continuous the function.

4 (a) r : "f(-r) (b) FIGURE I7.ei en nonuniform blowing wind ogo nsfthesideof c skyscroper.2 TIGURE (o) of how integrotion used evoluole is to oreosin engineering scientific Exomples cnd opplicotions.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 .I INTRODUCTION BACKGROUND AND 395 i _l -l (b) (c) I7.3 An illustrotion meon (o)discreie (b)continuous for of the ond dotc . 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 ' . A surveyor might to need knowtheoreoof o fied bounded o meonderinq ond roods.

sr a volume. total massof chemicalcontained a reactor girenasth the in is product of the concentration chemicaland the reactorvolume.formulas the most common numerical integrationschemes. However.396 INTEGRATION NUMERICAL FORMUTAS Integralsare also employedby engineers and scientists evaluate totalamount0r to the quantity ofa given physicalvariable. which is referredto as an areal integral.r\tl. Thesearejust a few of the applications integrationthat you might taceregukuly of in the pursuit of your profession. the underlyingfunction is often unknownand definedonly by measurementdisat you must have the ability to obtain approximate values crete points. y.For example.2 NEWTON-COTES FORMULAS The Newton-Cotes. elementalvolumes A Vi: : Mass I.z ) d r d v d z Mirss: lllrtvtdV JJJ v which is referredto as a volume integrctL.addition. t) isa case. whereA : areo. for integralsusing numericaltechniques described as next. y. 17.you will normally chooseto evaluatethem analytically. suppose of that concentration varies from location to location within the reactor. When the functionsto be analyzedare simple.In this case. is typicallythe casein morerealistic as In examples.ttt t .x . Similarexamples could be givenin otherfieldsof engineering science.However.The integralmay be evaluated over a line. Mass: III. or of Mass : concrentration volume x where concentration has ur. For both thesecases. 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. Notice the strong analogybetweensummation and integration. known function andx.. and : are independent positionin Cartesian variables designating integrationcan be usedfor the samepurpose: coordinates.. .(.rits mass per volume.. are They are basedon the strategyof replacingn complicated function or tabuiated datawitha polynomial that is easyto integrate: 7b 7b I:lf(r\dr=1f. it is often difficult or impossible whenthe functionis complicated. examand For ple. anNea.on whererr is the numberof discrete volumes.it is necessary sumthe to productsof local concentrations and corresponding c. For the continuous wherec(x.

r) .5 I7 three by oreo under stroightline segmenis The opproximotion integro fhe of on l(r) .tx" . 17.fr(.4 I7 (o) ine by The opproximction integrol thecreounder c stroight ond(b)o porobolo of on "f(.inFig.r):a0+alx +"'+ 397 (17.a parabolais employedfor the In samepurpose. For example.5. using a series polynomialsappliedpiecewise of The integralcan also be approximated of to the function or data over segments constantlength. (. Fig.4a.f (tt) FIGURE .r) : a polynomial of'the form a.in Fig.9) wheren is the order of the polynomial. 17.For example.2 NEWTON-COTES where ll. rtr"I la.three FIGURE .. a first-orderpolynomial (a straightline) is usedas an approximation..4b. 17.FORMULAS I 2.

formulas al'eavailable.r r ) f' 2 (17. ")]d.8)is first-order: to t: l""lt^ 1- .Higher-order the be utilized for the samepurpose.10 which is calledthe trapezoidalrule.r.the conceptis the sarrie the trapezoidis on its side.This chapteremphasizes closedforms. 17. However.6 inlegrotion The difference behween closed ib)open ond formulos {o) polynomialscan are straight-linesegmerlts usedto approximate integral. Recall from geor etry that the formula for computingthe areaof a trapezoidis the heighttimestheaverage of the bases.398 FORMULAS NUMERICAL INTEGRATION FIGURE I7.7. onrcase.6b). (17.f (b) . con+ The trapezoidalrul.I .7. formulas is briefly introducedin Section17.[(b) .3 THETRAPEZOIDAT formulas. 17.6a). l7 .verageheight EXAM .width x . Geometrically.the trapezoidalrule is equivalentto approximatingthe area fu of g trapezoidunderthe straightline connectin I O1 andJ @) inFig.f (a) (. D-0 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) + .The openfornrs have integrationlimits that extendbeyond rangc the the material m of the data (Fig. open Newton-Cotes RUIE 17. In but Therefog the integralestimatecan be represented as / . closedlorns The Closedand openforms of tlre Newton-Cotes are those where the data points at the beginning and end of the limits of integration ac known (Fig.. .eis the first of the Newton-Cotes It closedintegration sponds the casewherethe polynomialin Eq.

:--. they difl'er only with respect to the formulationof the average height.11) wheref lies somewhere the intervalfrom a to in b.f (o) + J @)112.the trapezoidal is rule wiil be exactbecause second the derivative of a straightline is zero. t! I -ul.2 _t 25x _ 200x2* 615x3_ 900ra f 40015 from a : 0 to D : 0'8.f"tE)tl.3 THETRAPEZOIDAL RULE 399 F I G U R E7 . with curvaturc).r3) where. 'lZ.1 Error of the TropezoidolRule when we employ the integral under a straight-rine segmentto approximatethe integral undera curve. Note that the exactvalue of the integralcan be determined analytically to be 1. fbr the trapezoidalrule. l 7.14)indicates that if the tunction being integrated linear. the average height is the averageof the function valuesat the end points.oo.e.or l.we obviouslycan incur .g).3. An esti_ mafe forthe local truncationenorof a "n singleapplicationof the trapezoidal rule is E.640533. (Ij. that rnay be substantial (Fig. Use Eq. All the Newton-cotes closed lormulas can be expressed in the generalfbrmat of Eq' ( 17'13)' That is. Equation(:i.rr) x average height (r7. .someerror can occur. | 7.lD ro numerically integrate l ( r) : 0. (17.17. otherwise..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. 7 I Grophicol depicrionthe of tropezoidol rule I : (b . for functions with second-and higher-order derivatives(i.

we would have no for-eknowledge the true value.800:rr 8. 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 .7)l as '' '^'f /.1128: 1.8.|(-60)(0.8-n.400 FORMULAS NUMERICAL INTEGRATION lnteqralestimate 0.050x . ol S o l u t i o n . an whichconespondsh fron|b a percentrelatil.the function's twicc derivativeover the interval can be computedby differentiatingthe originalfunction to give t f" (x) : -400 + 4. Therefoq hr actual situations.r)--A.80012 8. 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. t I ) t o y i e l d 1:{0.t-:0to0.: .10.To obtain this estimate..8-o which can be substituted Eq.461133. 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.l T .. 1+ 0 . (17. 2 3 1 :0.8 F I G U R EI 7 . 8 .050.2.8)3 .000x3 The average valueof the secorrd derivative can be computed lEq. a n d .t.14)to yield into I E.( l 7 . 2 E q . 8 ) : 0 . f ( 0 .eerror of st :89. 0 .2+25x-2oa^-r*675rr-9OO-r+*4OOr'trom. (17. : 1. T h e f u n c t i o nv a l u e sl ( 0 ) : 0 . 8 rule Grcphicol depiction theuseof o single-opplicotion tropezoidol to opproximote of of the iheinteqrol(./. .56 | .\ _ r/:r * /1'* t-+oo * 4.17:8 .5c/c.00013) 0. of serord an approxirnate error estimateis required.10.640533 0.

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. 9 trcoezoidcrle ru Comoosite .t ... muhiple-application. x2.site.A discrepancy' secondderivative however. respectively..'t ) d x + . ' ' f .l segments equalwidth: of sequently.3. la- b-a t7 (l7. . Therearen * I equallyspaced composite basepoints(ro.. Thus..l-s) the If a and b are designated . total integral can be repreas sentedas r : L'' (x)ru1..RULE 17. n : b-c ---il. r r u . ' +l .because the fact that tbr an interval of this size..fitrmulas..rate using the notation E.rs and 1..the average of of is not necessarily accurate approximation l"(6). 17.). and which is of the sameorderof magrritude sign as the trueerror. Tlre resulting equationsare called compo.9 shows the generalfbrmat and nomenclature will use to characterize integrals. 17.9).f + f ( F I G U R EI 7 ..rr.. we Figure 17.. .3 THETRAPEZOIDAT 401 doesexist. -r.2 The CompositeTrqpezoidolRule interval of rule is to divide the integration One way to improve the accuracy the trapezoidal (Fig. ratherthan exactby using E.Conthere are . .. we denotethat the error is an by approxin.

1 . ( 1 7 . . Employ F. 2 0 ) .q.l3): F.: _. An error fbr the compositetrapezoidalrule can be obtainedby summingtheindividto ual errorsfor eachsegment give E.l'" (€..n' Note is Thus. " [ ( r r ) .ro) and . .19) can be rewritten as ThereforeL. the interior points are given twice the weight of the two end points/(./ r\ r-\ ' .I '/ t .the truncationerror will be quarlered. . 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 ' .". can where /" (6i) t.:. 1/ ( x : ] or. . . r / i) t:(b-a) \.I ot' .l8).n" .. | 1. - - {r7 re) i.:: tln' .Recallthat theexact of the intesralis 1... 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 . ( 17. Use the two-segment trapezoidal -t * f (x) : 0.J (x. 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.17) in the general n-l f ( r n ) r . (.) | I J I form of Eq.' lD - .640533.).21) to estimate error.(17.T nl i-l I I(ri) l. the average Eq.q.8.11 .l tD L .( 1 1.-widlh j-1 2rt Avcragc hcight Because summationof the coefficientsof /(:r) in the numeratordividedby 2il is equal the According to of heightrepresents weightedaverage the function values. E X A M P L E2 .l5) to express (17.1 -.3 900x4 * 400x5 value the from a : 0 to D : 0. if the number of segments doubled. or.2 * 25x 200x2 675r. grouplng terms: I : : l / ' ( x o+ 2 ) ) . a to I .) r 2 r r ( . .ond derivativeat a point {..rl -L nt ..f (x.re.. n .402 FORMULAS NUMERICAL INTEGRATION rule for eachintegralyields Substitutingthe trapezoidal .. r . t of of t h a t E q . I(rn\* /trr) 22 - t t - .) = n f " and Eq. ( 17. usingEq. ( 17.1 tll' il ) f /.

where -60 is the averagesecondderivativedetermined rl o d The resultsof the previousexample. 4 s+ 0 .1 ..8 f ( 0 .21)). v e.belbreinvestigating are first discusshow MATLAB can be usedto implementthe trapezoidal rule. 2 3 2 6) 4 :1. However.r 3 3 3 0l143 0t 0. This is because error is inverselyrelatedto the square r [Eq. 6 4 n t2(2\t previouslyin Example 17. 6 4 0 5 3 3 1 .I2.we will tegralas the segments increased.2 + 25.3 RULE THETRAPEZOIDAL 403 Solution.( .9 t6 .along with three-through ten-segment applications of the trapezoidal rule.l.\the of 200-t2 675x3 9O0r+ 4OO. (1. ( 1 7 .Therefore.0688 €t :34.97c 0 E .2 0 t6 0 .r : O t o 0 . 4 5 6 . 8 ): 0 . are surnmarized Table l7. 5 1 1 7 3 0.5 95 6l 4. ("/"1 2 3 4 5 6 7 B I t0 0.2 1:0.1. 17. 0 6 8:8 . 6 4 0 5 3 3 .4 a 2667 0. quartersthe error.3 32 2. I hopezoidol ruleto Resulis thecomposite for estimote integrol /(." : . : 1 .gr [ .1 the of . Forn : 2(h :0. | 7.6 0 ) : 9 . 4 ): 2 . f ( 0 .alsonoticethat the rateof decrease gradual. The function to be integrated passed integrationand the numberof segments.r'l :0.3 MATTAB M-file: trap A simple algorithm to implement the compositetrapezoidalrule can be written as in is into the M-file along with the limits of Fig.T h ee x o c t o l u ei s 1 .. 1 8 ) . 8 . loop is then employedto generate integral A the f o l l o w i n gE q .l 0.0889 008 06BB 3695 484B 5399 5703 5B87 6008 609 I 6t50 349 t6.3. Notice how the error decreases in is as the numberof segments increases. In subsequent sectionswe develop doubling the number of segments fbrmulasthat iue more accurate higher-order and that convergemorequickly on the true intheseformulas.rs from + + . TABLE| 7.4): /(0) : 0. 2 + 2 ( 2 . However.4 1. 2 3 2 0 .

fori=1. .p2.end x = a. 9 4 8 0 5 . Note that the exact value of the integralcan be computedwith Eq. >> v=@(t) sqrt (9..p2.number of seg. 2 5 ) * t a n h ( s q r t ( 9 . 5 ) o. An applicationof the M-file can be developedto determinethe distancefallenby the free-falling bungeejurnper in the first 3 s by evaluatingthe integralof Eq.4) a s4 1 ./ooo 'J e.' tt ap (v.s + func(b. : 68.a)/n.25) *tanh(sqrt (9. 1 ) * t ) First. h = (f .n.b.. the assume following parameter values:g : 9.trap(f unc. and m ca :0.n.error('upper bound must be greater than lower.vararg:in{:}). S-func (q.25i68. let's evaluatethe integralwith a crudefive-segment approximation: formaL long >.404 NUMERICAL INTEGRATION FORMULAS function I = trap(func.6oo2o :.L/0.vdrarg i -. B 1* A . .ar" )-1>5 .varargin{:}).1)*t) C ( t ) s q r i ( 9 .. . ): % composite trapezoidal rule % input: func = name of function ? Lo be integrated Z a.a. end s .81*A.n-100. 1 .. 't ).a) * s/(2*n).end if -'(b>a).error('at least 3 input arguments required').81*68. ( 17. The function to be integratedcan be developedas an M-file or with an anonymous function.b.{:' ) .pi.1kg.(17. s = s + 2*func(x. For this exarnple. 2 5 / 6 8 . 3 1 * 6 8 .a.n-1 X = X + h.3).end r f narqin-4 risempLylnt..81 mlsz.25 kg/m.te tt:apezoidal rule quadraLure 'Z I . 3. 0.varargin) % trap: composi. . F I G U RI 7 .additional parameters used by func ? Z output: I=integralestimate % if nargin<3. b = inteqration limits ? n . I O E 'he M-f le -o imp enr-cnl conpo:iie rope.ments (defaulL = 100) pl . I = (b .).

l7. F o r e x a m p l e .11a). The formulasthat resultfrom takingthe integrals underthese polynonrialsare called Sintpson's rules.) f(x) . 1 ..4 SIMPSON'S RUTES 405 As would be expected. 1 0 t 1 0 0 ) 4!. there are two points equally lf spacedbetween.-rz) (x .r . 3 .l1b).xe)(.O . O nC O ' n e c l 1(/. 17. the tbur points can be connected with a third-orderpolynomial (Fig. obtaina more To accurate result.IZ.x2) + (xr - x e ) ( x 1- : l(xr) (. 17. / (rtt) (x1y xy)(xs .rz) . the anotherway to obtain a more accurateestimateof an integral is to use higher-orderpolynomials to connectthe p o i n t s .r.000segments: > > t .we can use a very fine approximationbasedon 10.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 a p ( r r .67o.r2 .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^. ^^. t h e t h r e e f f en points can be connected with a parabola(Fig.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 ) .. I FIGURE7. this resulthasa relativelyhigh true enor of 18. 17.4.:rr) (x2 .xo) ("t .: I:.xo)1.'l + n/l^r .8)is secondto order: (x-xr)(.rla.948049999r1528 which is very closeto the true value.f (a) and f (b). ( 17.^ri - ' g 'I O J r p O t n ' 5 .1 Simpson'sl/3 Rule Simpson'sI /3 rule coresponds the casewherethe polynomialin Eq.Y t ) 4 rr'.4 SIMPSON'S RULES Aside from applyin-q trapezoidal rule with finer segmentation.

36746j 0. (17. the etror is proportional to the fourth derivative.--' ' - ( b .:!11 thirds and rhe rwo end points by one_sixih. respectively.8 0. Thus. o7.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 .22)./z -(!srems fromtherfactthat//r lis u t v l ( j e o by J lin Eq. t b/ . n tion enor of I E . : 1.2+4(2.232 = 1.rr : the point midway between a andb. ::. Use Eq.l 0 b : 0'8' Employ Es.23) ro inregrate f ( .f (xzl f) xo. x ' ) : 0 .u-t"s 2990 "\ f t 1 . 2 * 2 5 " u . f ( 0 ..n : 2 l h : J'(0) : 0.4). (17. s n I/3rule.2 0.forthiscase.r2. S o l u t i o n .f(x) + J-(.2730667 : . Recall that theexacr !9 inregratis 1. 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 .640-533. s / 3 R u l e l ProblemSiotement. where f lies somewhere the interval from in a to b. \ t l' or.according Eq. r r w 4 v r r r 1 4 r t s atvided y D 3 n l2t.640533 1.406 NUMERTCAL tNIEGRAT|ON FORMULAS wherea andb arc designated -:16 12. 4 ): 2 .T'e + .becauseh:(b-a)12: E.comparisonwith Eq. r7.8) : 0.] that )indicates it is nore accuratethan expected'Rather than being proportionalto the third denvative. (17.l (:to) 4f (r) * . It can be shown that a single_segment ication applicati of Si npson'sl/3 rulehasa trunca.367467 €t:16. (17.23). In other words.Simpson. middlepoi to the int is weighted twoby .456)+0. Si Simpson's rule l/3 can a ) / 2 .l3): I-(b-a wherea : where.2 0 0 1 2* 6 i 5 x 3 _ 9 0 0 x a *400rs frorn.24) to esrimare rhe enor.67o E.b:. 4 5 6 /(0. Simpson's1/3 ruleismore accuratethan the trapezoidal rule' However.sl/3 rule is thirdorder accurate even though it is basedon only threepoints.which is glven rs si by 90 h 5 r/ ' f r t '. &s and The result of the intesration is lt - / : -r 1 [/(xo) 4. consequently.232 1 :0.y)l (n :?::.^t1]_ alsobe expressed usingthe formatof Eq.it yields exact resultsfor cubic porynomialseven thoughit is derivedfrom a parabora! EXAMPLE 7."otice 'fld.' : - that.

As was the casein Example 17.) because averagefourth derivativeis generally the not an exactestimate /'+rt{ ). 1.'.4. of because casedealswith a fifth-olderpolythis nomial.1. ( 17.17-10667 where -2400 is the averagefourth derivativelbr the interval..'f{*)ar+f l. is . The approximate errorcan be estimated oRs E" . Simpson'srule can be improvedby dividing the integraJust as with the trapezoidal tion interval into a number of segments equal width (Fig. the result matchesexactly. 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.the error is approximate(E.^n'< 1/3 rule.2-s) F I G U R E7 . 1880 : 0. ' + ttx+ o. : .12). (x) . 17.I 2 .I ).'er. .The relotive rveights ore depictedcbove the funclion vclues Not-^thoi the methodcon be employedonly if the numberof segmenls even.2 4 0 0 ) .l t .The total integralcan of be represented as r: [^.rU. I7 . Hower.. 1 2 1 fnrrnn<lfo lim^."' .2 The Composite Simpson/s l/3 Rule rule.

..4. z) * 4J'Q. 6 4 0 5 3.2 * 25r .1.12. (11.2+ 4( 1. 6:)3 . the coefficients"4" and "2" in F. r) + l (r.408 NUMERICAL INTEGRATION FORMUTAS SubstitutingSimpson'sI /3 rule fbr eachintegralyields I :2lt + I (xo) 66 (r." . 4 I (l) rh - l80na '' . 17. odd pointsrepresent rniddleterm tbreach applicationand the hencecarry the weight of four from Eq.q.f(o') o. tbr I Composite impson's/3 Rule S the of ProblemSiotement. However.an even numberof segments mustbe utilized might to implement the method.27) to estinlatethe error.n :4(h: 0. Solution.8. 17.1) 2.232 From Eq. (11.456) 0.12. 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 . 0 1 7 0 6 1 1 ..04Vo Et : 1 ..200x2-| 675.6 fG)+f(r.26\ with n : 4 to estimate integral f (. where f E X A M P L E2 .) Notice that. 1 .456 : I : l (0. (17.26) from Simpson's rule.8) 0.The even points are commonto adjacent applications and henceare countedtwice.' 1 5 ! I'o' ' \1j.623467 I2 €t : I. UseEq.3.. 4 6 4 . 1 7 .10533.l 1 .5 fQi)+2 L 3n j:2.6.z (). : .ro)+4I J:(b-tt) i.".21) is the averase tourthderivative the interval. as illustratedin Fig. Employ Eq. In addition.(17.232 + + + : 1.x) : 0.900x4* 400x5 from a : 0 to b : 0. groupingtermsand usingE q . (11.288 3.they follow natul'ally in the illustrated Fig.) J 6 +.464) 2(2.'+2h ( or. l/3 As seempeculialat first glance.5 ) : n I n2 l(..3.288 ( 0 .26): / -nrl : f \0.2): : .23).2) 1. Recall that theexact integralis 1.f 0. 6 2 3 4 6 : 0 .

: .28) Thus.'f 6t/ 't'te t or.fr)l o If(. ) * 3 / ( r r ) * 3 .An alternative would be to applv Simpson'sl/3 rule to the first two seqments Simoson's 3/8 nrle to the last . however.2. / ( .3. is limited to situations it where thereare an evennumberof segments an odd numberof points.U17067 l t t 0 ( 4) + which is exact(aswas alsothe casefor Example17.the 3/8 rule has utility when the nunrberof segments odd. because the large truncationerror associated of with this method.29) Because denor. a fhirdorder Lagrangepolynomial can be fit to four points and integrated yield to f : 3h -t3f(x) +.RULES IZ. . However.. Thts may not be advisable.It is the third Newton-Cotes formula. Suppose that you desiredan estimatefor five segments. disand as cussedin Section 17.4.r : ) (11. x )+ . i L l ' . because : (l) .3 Simpson's 378 Rule In a similar mannerto the derivation the trapezoidal of and Simpson'sl/3 rule. One option would be to usea compositeversion of the trapezoidalrule as was done in Example 11.ThisequationisknownasSimpsons3I8rulebecause/rismultiplied by 3l8.f(.torEq.f( x r . Simpson's rule hasan emorof 3/8 E.3).24). Examis in ple 17. the compositeversionof Simpson's1/3 rule is considered superior to the trapezoidalrule fbr most applications. .4 SIMPSON'S The estimated error 1Eq. (17. is (o'8)5 E'. f ( . .rr1)*-]l(. For instance.as mentionedpreviously.rr) where/r:(b. the two interior pointsare given weightsof three-eighths. (11.4.' b .nina.Consequently.a) 13: h . Further.17. : .a)/3. 3/8 rule is somethe of the what more accurate thanthe 1/3 rule.4 we usedSimpson'srule to integratethe function for four segments. Simpson'sl/3 rule is usuailytlre rnethod pref-erence of because attains it third-order accuracywith three points ratherthan tlre tour points requiredtbr the 3/8 version. : -]/. _ t-2100) : ().29)is largerthanfor Eq. whereas end pointsare the weightedwith one-eighth. As in Example 17.The 3/8 rule can alsobe closedintegration expressedthefbrmof Eq. odd-segment-even-point 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.4.However. / 'r' ' r { 64rJ0 (1.27'.13): in i:\U-UrT .it is Iimitedto cases wherethe valuesareequispaced. of 17. .(17.

2+ 3(t . s o F I G U R E7 .28): Eq.2 :3..(a) UseSimpson's ruleto integrare 3/8 -l400x5 * .5 S i m p s o n3s 8 R u l e '7 Problem Stqtement.8):0.f (0.^l J : f (0.: t ) .4ro NUMERICAL INTEGRATION FORMULAS 0 # 0.8. (b) Useit in conjunction Simpson's ruleto integratethe with l/3 same function five segnlents./(r) : 0.2 1/3 rule 0.20012 675x'r 900. 5 1 9 7 0 l. EXAMPLE 12.487117) 0.2r 25x. 1 3 1 jn lllusirotion Simpsons cnd 3/Brules beopplied tondem hondle of how 1/3 con 1o muriple (Fig. ll . tbr Soluiron.r+ froma :0 to b :0.266j):t.232 Segmentr (n) TABTEI . (c) A single application Simpson's rule requires equally of 3/8 four spaced points: /(0):0.4 0.432j24 .432724 3.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 .wecouldobtain estimate third-order three an with accuracy across theentire interval.13).In thisway. + + -0.232 1 .5333) Using (17.6 3/8 rule 0.4811'71 /(0.

16) is for app : .." . ' 9 O 1 t r :' r ' t .r'.l 1.the five. \t) .g. and Boole's rule) are usually the methodsof IABLE 17.COTES FORMULAS (b) Thedataneeded a five-segment I ication(/z: 0. 8 s 5 Bccle rr. f ( .r1r.^ : .)/{./{. 3 2 1 1 . 4 8 ): 3 .\.I 3) The in ore the so thoi theweighting thedoto points estimote overoge to heightis opporent. f ( r o ) + 4 .2+ 4(.2 Newton-Cotes closedintegrotion formulos.rr I 9 l (-rx) Z5. 1 0 ) + 3 2 1 ( x t )+ l 2 l ( x l + 3 2 . {l Z.COTES As noted previously.3803237 0 4tl For the last threesegments..:0.r : ) \p .f (l.181929 .2.264154 g The total integralis computedby summingthe two results: : 1 : 0.l ) + 501(.3803237 1.5 HIGHER-ORDER FORMUTAS NEWTON.2969t9) t.le s \ 1 .r-'./(ro)+l(-rr) n.232 f Q.t.HIGHER-ORDER NEWTON. 1 8 6 0 1+ 3 . 3/8 rule can be usedto obtain the 1:0.32 0. formulos presented theformotof Eq.296919 : f ( 0 . ( r t ) + 7 J \ t 1 ) 1 6 182 90 (.r r )+ f ( . . .r 3 S m p s o n3 / 8 r u e .r:) 501(. along with their truncation-eror estimates.r.645017 + 17.and six-point formulas have the sameorder error..{r) 75l (xa) I 9 l (r5) -127 + + + + 5 / I 2.. stepsizeis The of givenbyh:(b-a)ln. Poinls Nome Trcpezoidol rule S i m p s o r '1 3 ' r . Some of the formulas are summarizedin Table 11. r1'6 1 (r .1rt': 16.264754 1. ) Formulo Truncolion Error -ll /I2lhtl"tI) . . 1 4 3 3 9+ 3 ( 3 . This generalcharacteristic holds for the higher-point fornrulas fonnulas(e./ (0. 2 3 2 3 5 + :1.143393 + :0. . Notice that. 7 4 3 3 9 3 l ( 0 . 1 8 6 0 1 5 : 3.-7r.l/3 rule and leadsto the resultthat the even-segment-odd-point preference. 288 .f (0) : 0.48 1 .tt.1 . as was the casewith Simpson'sI l3 and 3/8 rules. .2 . r 8 r 9 2 9 ) 0 .the trapezoidalrule and both of Simpson'srules are menbers of a family of integratingequationsknown as the Newton-Cotes closedintegrationformulas.a9dnt .u ) \b-0) 7 .Stl.. . . 1 -13l80)15 11)(6) f Q 4 5 ) l i r .A+1 Theintegral thefirst two segnrents obtained for is 1 usingSimpson's/3 rule: I :0.16) | .

13393 2 074943 2.412 NUMERICAL TEGRATION RMUTAS IN FO However.8Vc.there are ntany situittionswherethis assurnption doesnot holdand we r. in engineering and science the order (i.309129 + + 0.200x2 67513 9O0ra 400"t'.( 17.e. example.ril+f@r) lhz f (u) + f (r..12 1. greaterthan four-point) lbrrnulasare not commonly used. In practice. of f (-x) 000 0t2 o22 o32 036 040 0 200000 1 . in described Chap. For thesecases. all lbrrrrulasfor numericalintegrationlrave been basedon equispaced data points. 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.309129 1.) * " .Sirnpson's rules are sufficientfor most applications.ic9729 t 345241 ) 7.experimentally For deriveddata often is of this type.it must alsobe stressed practice.+Ot0:_::'lj:_:::=t.10 + 2. Furthermore.1(x.305241 0. t) * . is TABTE 17.f(. ( 1 7 . 18. I + with uneguolly spoced volues :r.l G.3 Doto for "f(. in the to for Recallthat the correctanswer 1. r h . Accuracycan be improvedby usingthe composite version.1.30) determine integral thisdata. whereft..Use Eq. . : the width of segment Note that this was the sameapproach 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 . A p p l y i n gE q .The only difference and( 17.161 0 212 +.16) is the in the formel are constant. 3 0 y i e l d s I :0..) . ( 17.when the function is known and high accuracyis required.640533.30) that ft's between Eqs.2+ r.3was generated usingthesame polynomialemployed Example17. The informationin Table 17. 17.2 + 25x . E X A M P L E2 .nust deal with unequal-sized seqments. higherthat.-s9-1801 perceut which represents absolute relative an errorof €t :2..6 INTEGRATION WITH UNEQUAL SEGMENTS To this point. usedfor thecompositetrapezoidal rule.methods as such Rombergintegrationor Gaussquadrature. one method is to apply the trapezoidalrule to eachsegment and sum the results: I -ht .r) : 0. offer viableandattractive altematives.

i n r . 17.error('at not monotonically ascendrnq' ).6. ^ 4 + 4 0 0 * x .-r-. to accountfor the fact that MATLAB doesnot allow z.--4)a L .4.14.1 ' 2 + 5 15 * x .36 . Two error trapsareincludedto ensure ablesarepassed order. >lJdLFu :d L r a p s T u '.error('x n = lengrth(x).x andy. y) : Z rule to determine the integral ? Applies the trapezoidal (x. o ir_n r.rA loop is employedto tors are of the samelength and (b) the x's are in ascending from thoseof Eq.1 MATLAB M-file: trapuneq A simple algorithm to implement the trapezoidalrule for unequally spaceddata can be variand written as in Fig.4.end if any(diff (x)<0). (17. for k = 1. 7I.6 INTEGRATION WITHUNEOUAL SEGMENTS 413 17. if lengch (y) -=n.n-1 +y\k+\\) /2. v. .Bl.54 .32 >> v . > > l -r a p L l n e q ( x . end s = 0.tr-L o t dlt:alt_ftte YJ T = trapuneq (x.u J f \y. 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 .I2 . = trapuneq(x.64. holding the independent dependent that (a) the two vecinto the M-file./ector of independent var:rables % y = vector of dependenE variai:les ? % output: 2 I=inteqralestimate least 2 input arguments resuired').i.l0 . ( . 1 4 1 the rule spoced dofo. " 5 . 6 : >> x . : (o t! which is identical the resultobtained Example17.12. in to F I G U R E7 .9 0 0 " x .ero for An applicationof the M-file can be developed the sameproblemthat was solvedin E x a m p l e1 7 . ^ 3 .he * for n daLa points ascending % same lengrth and x must be monotonically % input: x = .y) J..30) generate integral.22 .end if nargin<2. y ) . error ( 'x and y must be same length' ) . y) where x and y must be of t.6.44.Two vectors.Notice that we havemodified the subscripts the in subscripts anays. M{ileio lmplement tropezoidol lorunequolly function o.2+25*x-200*x. r_ l .

81. " ' 2 + 6 1 * x . Someunequally spaced timesandrounded velocities be generated as can '.5948 In addition. l') wherethe two vectors.a). cct=O 2'15 . x and y.2 0 0 " x .14.6 1-9.54 . Solution.2): Eq.t .275 kg/m. / c d )* t a n h ( s q r t ( g * c d / m ) * t ) ) .2) to syntheticallygeneratesuch inforrnation a 70-kg for jumper with a drag coefficient of 0. 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 ) . 8L.5=[Q 1 L. As described the beginning this chapter. Then usecum1. that computes the cumulativc inte_ural. (17.1 B).2 MATTAB Functions: trapz Qrd cumcrapz MATLAB has a built-in function that evaluates integralsfor datain the same fashion asthc M-file we just presented Fig.develop plotofth a analyticalar. 3 : >> x = t0 . Use Eq.I2 . hold the independent and dependent variables.y) 1. cumtrapz (x. 2 + 2 5 * x .'. z EXAMPLE l7. respectively.6.3. MAILAB has another function.m=70.7 . hold the independent dependent and variables. 17.vJ o 5 9. :trl: I u \ t ld t J(\ Suppose that we had measurements velocityat a series discrete of of unequally spaced tirno during free fall.) where the two vectors.7 I44.4 .2 4L1 80. rapz to determine distance and the fallen comparethe resultsto the analyticalsolution(Eq. 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.7 UsingNumericol Integrotion Compute to Distonce fromVelociiy ProblemStotement.^ 5 . It hasthe general in syntax z t rapz (x.rd cornputed distances along with velocity on the samegraph. y.In addition.32 . >> 9-9. The distances can then be comouted as / J^Ltopt( . Incorporate some randomerrorby rounding the velocitiesto the nearest integer. i7.x &od-r.45 173. lornot shu'r (J . cumr rapz. > > y = 0 .4 2 3 4. ^ 4 + 4 0 0 * x . sirnple representationof its syntax is A z .h .22 .85 23r. ^ 3 .respectively. .64 . > > v = r o u n d ( s q r t ( g * m . 5 >> trapz (x.414 FORMULAS NUMERICAL INTEGRATION 17..9 0 0 * x .3 5 5. Here is a simple MATLAB sessionthat uses this function to integratethe data from T a b l e1 7 .44 .

This result is reasonably the closeto the analytical solution(Eq.7m. za=n/ cd*1og (cosh (sqrt (qr*cd/m) *t.[ . z.I2. t rve As in Fig.. 1 5 1 Ploi distonce of time line versus The woscompufed theonolyticol with rne solufion.h e u r -. . numerical the and analytical results matchfairly well. 17. om A graph of the numericaland analyticalsolutionsalong with both the exact and rounded velocitiescan be generated with the following commands: (t (1) . .ylabel ('x (m) ') 'numerical Legend ( 'analyticaL' .6 INTEGRATION UNEQUAL WITH SEGMENTS 4t5 the Thus. F I G U R E7 .t. . za.. on in Distance versustime O LCS a nalytical numerical kg ng nd he . wnereos w p o i ' r ' s e e d e l e ' r i r e d ^ l n e c o l . t (length(r) ) ) .a) ) . .ly.15. after 8 seconds. >> ploL (La. jumper hasfallen 231. r.' a' ) >> >> >> >> ra=linspace >> xlabel title('Distance t-1me') versus ('t (s) ') . 11. fu^clio.4): )ly.

( t 1 . simpleexarnple A would be to take the doubleintegral a of functionover a rectangular area(Fig. in As with the closed versions.1hod Segmenls (n\ Formulq Truncotion Enor 2 3 ( b .Equation(11.t r .{17. .successive pairs of the formulas have the same-order enor. / ( . The techniques in discussed this chapter(and Chap. t)r + .The fbrmulasare expressed the tbmr of Eq.l14A)h1 f\6)Gl 2A (b a\ 17.r.(f x r . The l3) ore in so thottheweighting the doto points estimote overogeheightis opporent.32) Thus. The openformulasarenot ofien usedfor definiteintegration.6b that openintegration formulashavelimits that extend beyond the rangeof the data.4 Newton-Cotes open integrotion formu/os. 1 ' ) t 1 x ) (d-c)(b-a) (r7. l ( .* I 1 . ( 17. 1 \D a)- | /3V13 f"G) t. .7 OPENMETHODS Recalltiorn Fig.theyhave utility for analyzingimproperintegrals.v) L' (1.f d) lD 2A ) ) i l .(.a ) .32) states in that the orderofin- .a general equatiorlto compute the averageof a two-dimensional function can be written as lrecall E q . the integral in one of the dimensionsis evaluated first. Recallfrom calculus that suchintegrals be computed iterated can as integrals: a..iar161 3 I l .u ) l n . l8) can be leadily employedto evaluate multiple integrals. l ( .1 4 . in EXA 17.. r t ) . r r 1 ) (b-u) l+t. The result of this firstintegration is integrated the seconddimension. 1 ' 1 .4summarizes Newton-Cotes the openinteT4rotion.) : at r) L' (1. siep to of the The size i s g i v e nb y h : ( b . f ( x t )+ l ( . 17.r 1 ) (1)(6) (95/l44lr5. formulos presenied the formotof Eq. .8 MULTIPLE INTEGRATS Multiple integralsare widely used in engineering and science.. 1 ) l l .I 3 ) so that the weightingfactorsareevident.314)h3 f"G) 2 J ( r t ) .20 and 21. f( . 11.they will haverelevance our discussion In to of methodsfor solving ordinary differentialequations Chaps. The even-segment-odd-point fbrmulas are usuallythe methodsof pref'erence because they require f'ewerpoints to attainthe sameaccuracyas the odd-segment-even-point fbrmulas.3 r) The numeratoris called a double integntl. 1( f r ) * .416 NUMERICAL INTEGRATION FORMULAS TABTE| 7.r 2 .Table 17.'/(r' or) (17. ' '( I i l t r . 1( r : ) I I I ( r r ) (l4rJ5ll5. However.. f ( . r r )* 1l .16). addition.fbnnulas. Poinls I 2 Nome M dpolf t rn-.l : 1 1 ( f r + 2 b l ( x t ).For example.'.

in Note that a simple average thesevaluesis 47.66667.2 t ' 2+ 1 2 If the plate is 8 m long (. a Now we can applya single-segment Simpson'sl/3 rule in thesamefashion.I 2.The function can also be evaluated of analyticallyto yield a resultof 58. for Sincethehighest- .r at and y valuesare depicted Fig. The temperatures the necessary.the trapezoidalrule is first implemented along the .r . First. of of which is exact. Dividing this by the areayields the averagetemp e r a t u r e s2 6 8 8 / ( 6 x 8 ) : 5 6 .66667.17.33.y dimension).let us merelyusetwo-segment applications thetrapezoiclal in each of rule dirnension. To make the same evaluationnumerically. 17. methodssuchas the cornpositetrapezoidalor Simpson'srule would be applied in the first climension with eachvaltteof the second dimensionheld constant. Supposethat the temperature a rectangularheatecl of plate is described the following function: by : I ( t . .8 MULTIPLE INTEGRATS 417 FIGURE 17.r dimension)and 6 m wide (. Why doesthisoccur? Reperfect call thatSimpson's /3 ruleyielded | results cubicpolynomials. Then the methodwould be appliedto integratethe seconddimension.The approach illustratedin the following example.16 Double infegrol theoreo os under function the surfoce A numericaldouble integralwould be basedon the sameidea. Thesevaluesare then integratedalong the I' dimensionto give the final result of 2688.computethe average temperature.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.r climensionfor each-r'value.This results in an integral 28 l6 andan average 58. ) 2 x ) * 2 r .x 2 . First. is EXAMPLE 12. Solution.

6) 2816 . Here is an exampleof how this function can be usedto computethe double integral evaluated Example17. x n t i n .y) 2*x*y+2xx x.A. In Chap.0.1 MATTAB Functions: dblquad ond triplequad MATLAB has functions to implernentboth double (dnlquad) and triple (rriptequad) integration. (u-u)0+2{40)+48 +256 4 54 70 . y m a x .4t8 NUMERICAL INTEGRATION FORMULAS Iftiit U 40 48 ^.vm. .7: in >> q . wouldbe it necessary use compositeapplicationsto attain accul'ate to integralestimates. simple representation the syntaxtbr dbtquad is A of q = d b l q u a d ( f u n.8. ( 8 . t o l ) where q is the double integralof the function fun over the rangesfrom xmrn to xmaxad to a of . addition. defaulttolerance I x 10-6is used. x m a : . "2-2*y. Theseoften providea superiormeans implement to the numericalintegrations multiple integrals.If rol is not specified. l8 introduces techniques that are more efficient thanthe Newton-Cotes formulasfr evaluatingintegralsof given f-unctions. 1 7 1 Nurnericol evoluotionc double of integrol thetwo-segmenl using rule tropezoidol For higher-ordelalgebraicfunctionsas well as transcendental functions.''2+72.zn ymax.dblquad(@(x. for 17.y m i n .0 ) 5 4 + 2 | \ 7 0 ]+ 5 4 4-+ 496 72 64 24 X 8_u7?:3gej4+ 448 (6-0) 256+?(4961+448 I F I G U R E7 .8.

Simpson'srules.In fact.realistic problem settingsareusually morecomplex.34) might be solvedanalytically. 17. when analyzing measureddata.and F(x) : a force that varies as a function of position (N). 17.For example. For such cases.33) can be evaluatedanalytically.33) where W: work (J).W 07.Eq. The results are interesting becausethe most accurateoutcome occurs for the simple two-segment trapezoidal rule. as in w:1"" F(. numerical methodsprovide the only alternativefor determining the integral.In suchcases. 17. ff f(-r) is easyto integrate.18).suppose that the force variesduring the courseofthe calculation. the work would be calculated as50J(ljoule:lN.6.it is more likely that the functionalrelationshipis complicated. if a force of 10 N was used to pull a block a distanceof 5 m. ever. assume that. More refined estimates using more segments.sirnpleapplications were presentedusing forces that remained constant throughout the displacement.Although the figure showsthe continuous valuesfor F(x) and d(x).For example. : the initial and final positions (m). xo and x. The resultsof the analysisare summarized Table 17.' ( 17.This is particularly .A percentrelative in error r/ was computed in referenceto a true value of the integral of 129.CASESTUDY 419 Bockground.The work equationcan be modified further to account for this effect.numerical integration is the only viable option for the evaluation. work equationis reexpressed the as w: F(x) dx 1. as in Fig. respectively.34) HowAgain. (17.18.r) cos[9(x)] dx (r7. The calculation of work is an important component of many areasof engineering and science.m).. Although such a simple computationis useful for introducing the concept. you are providedwith only discretemeasurements constraints. the force might be available only in tabular form.The general formula is Work:force x distance When you were introducedto this conceptin high schoolphysics. if F(-r) and e @) arc simplefunctions. Solution. because experimental of at versionsof the trapex : 5-m intervals(Table 17.and multiple-application zoidal rule and Simpson's1/3 and 3/8 rules to computework for this data. However. For this sifuation.18.5). 17. in a realistic problem setting.the force might not be expressedin such a manner.52 that was estimatedon the basisof valuestakenfrom Fig..Use single.18at 1-m intervals. 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. Further complexity is introduced if the angle between the force and the direction of movement also varies as a function of position (Fig. Suppose that you have to perform the computation for the situation depicted in Fig.as well as results.

F(r).8082 1.18 For cose ongle. wellosihe the os force on The cose o vorioble octing o block.0 l4.0 r3.5297 9 .0000 1.3532 .0 5.{.75 090 F(x) cos d 0 5 t0 t5 20 25 30 0. rod 050 1.0 9.0 r30 I . m FIGURE 17.7025 2. 5r 2 0 8. lhis of vories.theforce of TABLE t 7.48 r. mognitude.5 of Doto for forceF("r)ond ongle 0(x) os o function position -r.o 105 12. N 0.50 0.420 FORMULAS NUMERICAL INTEGRATION continued 0 . 0 8 8 1 0.40 0.

09 175.98 r19. Thepercent relotive error€. 17. evident in Fig. where we have plotted the continuous curve for the product of F(.r) and cos [0(x)].93 95.84 35 r 8. 1 3 r39.5 ond 12.52 thot wos to Po) estimoted the bosisof volues l-m intervols. 17. os computed in reference o truevolueof the integrol1129.19.lnl r.20). The omission of thesetwo points effectively limits the accuracyof the numerical integration estimatesin Table 17.5 m.9 2.05 35 75 L57 804 5 5 F|GURE 17.6 Estimoies work colculoted of usingthe tropezoidol ond rule Simpson's rules. The conclusion to be drawn from Fig.5 and 12.'^ir4rrr6 af E"/t {\ onc tA(1 5\l ? . Notice how the useof sevenpointsto characterize continuouslyvarying the function missesthe two peaks at r :2.6. 17.9 CASE STUDY 421 continued TABIE 17.6 Notice how the use of sevenpointsto chorccterize voryingfunction thisconfinuously misses two peoksot x :2. if data were ^.82 I l 7.I2.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. For the present case. The fact that the two-segment trapezoidal rule yields the most accurateresult is due to the chance positioning of the points for this particular problem (Fig.'lo Simpson! /3 rule 1 Simpson's rule 3/B 5 3t r 133 9 124.5 m. on ot Segmenls I 2 3 6 2 6 3 Work e[ o/o Technique Trnnpzni.20 is that an adequatenumber of measurements must be made to accurately compute integrals.

t. 17.5 15 20 25 301. can be r spacedd rO f(x) 2 determine an improved integral estimate. the inclusion of the additionaldata incorporates peaks the that missed previously and.35371. 17. 6 I tt (a) analy (c) coml applicat and (f) E through 17.2 Evalnte the lbllowing integral: r4 JO I tt .e-2'1tlx applicationof Simpson'sl/3 rule.422 NUMERICAL INTEGRATION FORMULAS continued (a) anal (c) coml applicat | /3 rule the num cent rela 17. ( 17.4)by integrating (17.5291 9. using the MAILAB trapz tion.. use trrotro[ezoids integrol this the of cose.3940 8. y ) r32 .L.4 Ev 1a a.8087 . as a consequence.l6Eo). 17. determine prthe cent relativeerror basedon (a). >> trapz (x. (e) composite Simpson\ 1/3 rule with n : 4.6 Ev I J-t J el Including the two additional points yields an improved integral estimateof I (e.20 Grophicol depiction whvthetwo-seqment of lropezoidol vields ooodestimote rule o ofthe for poriictrlor Bychoice.9007. h"oppens toon to leod even bolonce between posifiveond negotive errors.0881 0. and (f) Simpson's 3/8 rule. lead to better results.. 3.Foreachof the numerical estimates(b) through (f). 1.5 5 r0 L2.For example. we could compute >> x=t0 >> y=[0 2.:2. (a) anal with n = 1/3 rule PROBtEMS Eq.6458 Evaluate lytical rn of the tri attain thr cent rela 17.1025 2.5 Th .1 DeriveEq.5t"20 11. ft f (x) FfGURE 17.3 Evaluatethe following integral: rn /2 .3). Thus.

(b) Fit the data with a cubic equationusine polynomial equation1odeterminethe regression.PROBLEMS 423 (a) (b) rule.6 Evaluate double intesral the and essentially cancelsout).) . 17..9rz.(b) using thc composite trapezoidalrule r I) with : 2.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 03-5 f ( x ) 2 1 8 5 5 .thc total force is obtainedby evaluating (a)analytically.9 foce Woter pressure lheupslrecm of o exerting on dom { o J: .p : densityol i to water. which fbr thi.and (c) a combinltion D : elevation(in m) ofthe watersurfaccabovethe reservoir lytical means. 17. the total force .z) 0o where p(.ssunred be a constant Evaluate inlc-eral the from rr :0 to b :0.1.)(1. r 3 3 r ' : )d t d t ' d : 1/3rulewith n : 4.sproblem is a.J. ^ r r e o s . 17.7 Evaluatethe triplc integral analytically. singlc applicationof thc trapezoidal (c)composite trapezoidalrule with n : 2 antl .9 Walcr exertspressure thc upstream on can be characterized by shown in Fig. For eacho1' estimates(b) through (f). n g . Integrate cLrbic the distance. computethe percentrelative 17. compute the percentrelativeeror.6 using (a) ana_ 103kg/rn3. For (b). and (f) Simpson's3/8 rule. Simpson'sl/3 rule..and (b) using single applicationsof on cerrt relltirc erlor hased 1a. (P17.Becauseboth pressurcand area vary with elevation.{5 (a) Use the tlapezoidalrule. (e) composite of 1 ..8 Detcrmine the distancetravelcd tionr the follouinc + 2xs)ri-r velocitydata: I .pressure pascals(or N/ml) exertedat an in 0s308 08131 elevation mctersabovethe rescrvoirbottorn.9b). can be dctertimes the areaof the dam tace mined by multiplying pressure (as shown in Fig. analytically. For cach of the numericalestimate (b) (f).9). : I psw(z)(D-i./. Pl7. (e) Simpson's of (f) s and Boole'srule.with dcpth. JO l/3 rule.Accordingto Eq.1 Evaluate lirllowins inte-eral: the efror.as depictedin Fig. singlc applicationof the trapezoidal (c)conrposite tlapezoidal rule with n :2 iutd 4.81m/s2):antl (b) rule. application Sirr.9. (d) single n) n) rl l/3 Simpson's application Sirnpson's rule. P17. and (c) using singleapplications Simpson's ir of f. d e r i e w s L o * r n gf o ' t e .5 The firnction . the trapezoidal and Simpsou'srules whereverpossibleto bottom.The prcssure pQ): pg(D. determinethe perthe numerical (a) analytically.For (b) and (c).rpson's rule. computethe per.51 5 9 7 0 1 3 7 4 6 l l 8 3 l r A. cent (because works against prcssure it both sidesof the dam facc 17.9 : acceleration to -eravity due t9. on through determinethc percentrelativeerror based (a). P17.1. OU 40 zu 200 190 175 160 (b) FIGURE P17.Ornittingatmospheric aurin highestaccuracy. face of a dam as I 7. relativeerror'.pressure ofthetrapezoidal linearly increases the For (b) and (c).(d) single l/3 3/8 rulc. J^ (a) (b) rule.

N/m Height l.:s'a \/T'l where: : the elevationabovethe deck and H : thc height of the mast.14 A transportation engineeringstudy requiresthatyou determinethe numbcr of cars that passthroughan intersection traveling during morning rush hour.12 An I l-m beam is subjected a load. cm2 loo lo3 l06 I lo )2a I33 lsc 17.r c.). You standat the sideol the road and count the numberof carsthatpass every 4 minutesat severaltimes as tabulatedbelow. I pG)A..m a 2 4 l0 3 6 B p. F(/).) Time (hr) 73A Rote (cors per 4 min) I B 7 45 24 B:OO B:15 l4 24 O:J. mls - . Perfbrm thc intcgral necdedtbr thc avcragcin theorder shown by the following equation: o: 1 L/".: I . p.For (b) and (c) usel-m lncrements.17 The c Putedas B : 4 5 9 :i 5 21 9 A. the integral reprcscntsthe summationof the product of flow times concenqivo I et tlr U.I ll J.. ffi M: M:M.. (b) Repeat(a).I k..It:ta: -u Use numerica data listed bel t.We know that V : dM ldx.+ I Vttt' .9b). The line of action can also be obtainedby evaluating IoDpgzu:(z)(D z. N/m 17. m3/mi.. whererz : filoSS.The total force F exertedon the mast can be dctermined by integratingthis function over the height of the mast: x.rl. where u(l): width of the dam face (m) at elevation. (Hint: Be careful withunits.the initial and final times.r). but use the composite Simpson'sl/3 rule. and(c) multiplc-application Simpson's rules.: (Fig.(..- /(\.424 INTEGRATION FORMULAS NUMERICAL If M.su(:)(D .fr:\ 'l: (a) Use the compositetrapezoidalrule to compute F rnd d for the casewhcre 11 : 30 (n : 6).1. 17.r: Jt) p(. . This formula makesintuitive sense you rccall thc analogy if betweenintegrationand summation. 17. Pl7.1r I w h e r eU : a numerica data: )rfi I.t Jt where I.60 34t 33l A.iszero and. mln Q.)tJz. Integrationyields the relationship rI ft) l.Thus. and (b) the rateofcan going throughthe interscctionpcr minute. tributed 17.glcm34ao 395 389 3Bo 3..r is length in distancealong the beam. andcl. l7.r : dist averagetlov IB0 2)A 240 3200 3500 3800 17. m Force. m Force. and.r : I l. Determinethe massin grams to the best possibleaccurac).15. calculate using(a) analyticalinM tcgration.(b) multiple-application trapezoidalrulc.-r:200(.r) : dcnsity. r. fng/Jn" J. -r : distance alongthe rod and L : the total lengthoftherod.16 Integrationprovides a meansto computehow much 17. respectively. and the shear massentersor leavesa reactorover a specifiedtimeperiod.15 Determine average the valuefor thedatain Fig.r): cross-sectional area.(r)dx / . Use thebest numericalmethodto determine(a) the total numberof can that passbetwecn7:30 and 9:15.PI 7./0 0 0 I50 3r00 30 340 60 l2aa 90 t2a t600 27aa where R : t and.l Compute the net fbrce and thc line of action due to this diswind. A.10 The tbrce on a sailboatmastcan bc reprcscntcd the by following function: . F(/). The following datahasbeenmeasured a l0-m length for rod.. to force follows the equation a si n V(-r):5+0.\ Jr.ll A wind fbrce distributed againstthe side of a skyscraperis measured as Height /.) t1: ft Use Simpson'srule to compute J. f r.13 The total mass of a variable density rod is given bi71.. and M is the bending moment.) dz The linc of action can also be deterrnined integratron: by r rH J0 :.2512 where V is the shearforce.l./..'. and t2 .

r.' (mr/s) can be contputccl average as 0 0 : rl] I U{.g/m3 )a'2 85 7. wherc Z : the total depth (ni).hercthe arcaA.9.lrrlr.25 l7 005 412 l 0Ir a.25 oa2 . thc l1ou.)z/r.m H. ftr U.17 The cross-sectional area of a channelcan bc computed as A. 1odetcrmine cunrulativc the work andplot the resultversus r. l a 6 m 2 9 8 1 7 5 5 t 0 5 t 1 .h 0.= I H(r')r1r.n B 12 )6 A . J0 rll O 4 z. mg/m3 l7.0 34 (-: .t'(.1m:) varieswith depth l(m) can be computcd integration: by 0 4 t0 t0 2a 30 4B 52 50 3_s . I 5 numerical Use inte_uration evaluate to this equation the fbr data listed bclow: l. min Q.7 60 38 90 37 l2O 3 150 1. In a similarfashion. Jo 17. B and : distance r' liom the bank (m ). the curnLrcipzfunction Use g.rad 0 0 I 30 2. m/s 05 003 r3 006 56 1.da.and B fbr the fbllorving a nurnerical data: n)/1 J.19 As wasdonein Section17..4 52 4l wherc : the total channelwidth (rn). m3/min c.Use these where : watervelocity' U relationships and methodto determineA. cletermine work perthe fbrmed ifa constant force of I N appliedat an anglegresults in the iollowing displacernents. Determinethe averageconcenlrationbasedon thc followins data: 17.4 lB0 (m/s). H : rhedepth(m).PROBTEMS 425 -u -8 4 -4 -3 I 7 -z 1 10 12 0 F I G U R EP I 7 . 9 6 3 5A 3 5 2 7 O O O O O c.lll The average concentfation ofa substance -(g/rn3)in a lake u.r.6 43 43 4A 37 32 50 5.-55 52 35 4A 45 4.+.

the fuuctionis available. NumericolIntegrotion of Functions CHAPTER OBJECTIVES The primarv objectiveof this chapteris to introduceyou to numericalmethods for integratinggiven firnctions. The form of the datahas an important influenceon the approaches can be usedto evaluate integral. I 8.Specificobjectivesand topicscoverecl are ' . at q.-r.. Bolt capitalize on the ability to generatefunetion values tcl develop efflcient schemes fr numericalintegration.:d r-1uadl integrate Knowing how to useMATLAB's built-infunctions and to functions. The flrst technique is based on Richardson extr(tpolatiorz.thecomposite Sirnpson's rulemight seem be a reasonable l/3 to toolfa such problems. we noted that functionsto be integrated numericallywill typically oftno be lbrms: a table of valuesor a tunction. Tlc computationalalgorithm lbr implementingRichardson extrapolation a highlyefficied in rnanneris called Rombergintegration.Although it is certainly adequate many problems.if* . for therearemore efficient methodsthat are available.This techniquecan be usedto generate integnl an esrimate wifhin a nresnecified eror . 17.I INTRODUCTION In Chap. are limited by the numberof points that are given. o providesa meansto create more Understanding how Richardson extrapolation a integral accurate estimate combiningtwo lessaccurate by estimates. In contrast.This chapteris devotedto three suchtechniques.l (r) as are requiredto attainacceptable as accuracy. more accllrate value. yut if can generate many valuesof.For tabulated that you the information. At tacevalue. which is a method fa combining two nunrericalintegral estimatesto obtain a thild. providessuperiorintegralestimates Understanding how Gaussquadrature by picking optimal abscissas which to evaluatethe function.

For example.2 ROMBERG INTEGRATION Rombergintegrationis one technique that is designed attainefficientnumericalintegrals to of functions. Generally calledRichardsonextrapolallon.throughmathematical manipulations.It is quite similar to the techniques discussed Chap. 18. superiorresultsare attainedfor lesseffort. However.2) . (18.a) I n.1 Richordson Extropolotion Techniques availableto improve the resultsof numericalintegrationon the basisof the are integral estimatesthemselves.In this way. rnore accurate of approximation.r.These error estimatesare then used to determinewhether more refined estimatesare required for a subinterval. (18. if we usedthe trapezoidal rule to determine integral.2.r valuesthat arepositionedbetweenthe integrationlimits in sucha mannerthat a much more accurate integralestimateresults.thesemethods use two estimates an integralto computea third. I (h) : the approximationfrom an n-segment applicationof the trapezoidal rule with stepsizeh : (b . we have made it possibleto utilize the information embodiedby Eq. In so doing. in Chap. andE (h) : the truncation error.2) canbc uscdto <lctcrmine that the ratio of the two errors will be E(ht) E(hz) _hh i . 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 . 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. values of at /(x) for the Newton-Cotesformulas were determined specifiedvaluesof .ROMBERG INTEGRATION The secondmethod is called Gcuss qundrnfure.Recall that.2) lf rtis assumedthat f" is constantregarcl\ess of stcp sizc.! (1 8 . 17 in the sensethat it in is basedon successive applicationof the trapezoidal rule. The third approachis called adaptivequadrature. 18. Two built-in MATLAB functionsthat useadaptivequadratureare illustrated. more refined segmentation is only usedwhereit is necessary.This techniques appliescomposite Simpson's I /3 rule to subintervals the integrationrangein a way that allows error estiof mates to be computed. (18. 3 ) This calculationhas the importanteffect of removing the term 1" from the computation. ( 1 7 . 17.we were constrained takethe an to weightedaverageof /(x) at the endsof the interval.Gauss-quadrature formulasemploy. 1 ) Now recall that the error of the compositetrapezoidalrule can be represented approxlm a t e l yb y E q . 2 1 ) w i t h n : ( b .Eq.a ) l h ) : [ b-u E-__htf" 12 .

428 NUMERICAL TEGRATION FUNCTIONS IN OF without prior knowledge of the function's secondderivative. a'(") (185) EXA Extropolotion E X A M P 1 B . For example.\2 I(h1tLEthrtl .UseRichardson to extrapolation evaluate integral /(.thisequation becomes ' : 4l lt(nt. (18. I Richordson LE Problem Stotement. havecombined two trapezoidal estimates O(h2) to yielda newesrule timateof O(h\. we reanange Eq.I\hzl 1-(htlh)2 Thus. we have developedan estimateof the truncationelror in tenns of the integral estimatesand their step sizes. 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. Thr-rs. \: \n2/ I which can be substituted Eq.r)= the of .8.2t 25x t Solution. 0. For the special casewherethe intervalis halved(h2: hr12).4) \tt1/h2)' | It can be shown (Ralston and Rabinowitz. / h.1): into /h. | -I(h2tlE(htl \tt2 / which can be solvedfor p \ ( / r/ ' ) : E "1 Itlttl. ll ttt:l ttttrtl fl8.( 18.This estimate can then be substituted into I : I(hz) _t E]tz) to yield an improvedestimateof the integral: I : l(hz) + -1-t--.To do this.200x2 675x-r 900r+*400x5 tioma : 0 to b : 0.3) o give t E ( h t t= E ( h : t l . 1978) that the error of this estimate is we of O(h4).the estimatesfor one ancltwo sesments .5% 349% 5.5% Richardson can estimates extrapolation be usedto combinetheseresultsto obtainimproved can be combined of the intesral.

as accuracv increases. l.IN I 8. In Example18.6c/r:). For the special casewherethe originaltrapezoidal estimates evenbettervaluewith Oth6l.(18.the equation usedfbr O(li6) accuracy are basedon successive I : -1. : :0. en 4 fl ) .1.. two where f.. The two integralestimates O(h1) obtained Examplel8. rule of Equation(18.361461 in of into Eq.and (18. upon whichis superior the to to for can manner. 1.Similarly.623461 l.. . can be combinecl compute integralthat is O(/r8)using to an O(ft6) results [ : -1..oazsr. Utilize Eq.011061 (et : 1. Theerrorofthe estirnates whichit wasbased.2 ROMBERG TEGRATION 429 to yield 3' : (st is Et improvedintegral : 1.623461 -53 t :lt. thev reoresent weichtinefactorsthat. and 11 arc the more and less accurateestimates. usedRichardson we extrapolation compute to two integralestimates Othl).4)providesa way to combinetwo applications the trapezoidal (h2) to computea third estimate is of with error O (ha1. be combined yield an to Thesetwo can.2 :XAMPLE Higher-OrderCorrections ProblemStotement.ru ro u r): r .ja/c).4848)(1.640533 1. (18. zoidalrule estimates. is halvingof the stepsize.5). This approach a subset with error O For generalmethodfor combiningintegralsto obtainirnprovedestimates. Solution.2130(t1 : 76.6)to combinetheseestimates computean of to w integral ithOtlr6t. we computed improvedintegrals in turn. 64 - -ll I 63"' 61 ' ( 18 . a more of of two improvedintegrals O (ha) on thebasis threetrapein ExampleI 8.0688): 1.6) to yield and 1. I : -(1.6). 18.640533 1. ( I 8. instance.640533 t5' t5 which is the exactvalueof the intesral.2..rzz8):1.7)l add up to l.Thesevaluescan be substituted r : ! l .5 16 -lt I-5 r (r 8 .36146i 3' an which represents errorof E.623461.l8. 7 ) .361461 0. Thus.(18.!t. 6 ) respectively.lro. estimates two andfour segments becombined give the In thesame 41 -.l were 1.2 The Romberg Infegrotion Algorirhm in Notice that the coefficients eachof the extrapolation equations [Eqs.

r. where 17-.j=3isfor of application[stepsize is (b . ( 18.430 INTEGRATION FUNCTIONS OF NUMERICAL placerelatively greaterweight on the superiorintegralestimate. andso forth.* 1.a) l4].j:2isforatwo-segmentapplication[stepsizeis(b-d)12].. .andits systemby isa atic applicationto evaluateintegralsis known as Rombergintegratiut.361461which hasan errorof O(h41. of For example. : 3 to the O (ho). (18. l:13?333-1623467 0.respectively.1 4i-l .640533 . ib) o(h2) o(h4) o(h6) o@8) t'r ?:163333-1s67467 0j72800 ---------? 1.z: 1.r.for ft : 2 and j : 1. - 4r tlj*r.367467 F 1.The index ft signifiesthe level of the integration. ( I 8.172800 1. The indexj is usedto distinguish ft For example.1 graphical depiction of the sequence integral estimatesgenerated of using this approach. es e q . E q u a t i o n ( 1 8 . U + l) and the less (l) accurate becomes tr.367467 --.k:2 conesponds the O(ft4) to e$ito between more the mates. FIGURE ." 1:333333---163s467 .r 1-11. Eq.(18.. rule the Each matrix corresponds a singleiteration.I I8 g n G ' o p n i c o d e p i q f i e n f t l .068800 1. 18.8) to obtain successively better matrix are generated systematically by estimates the integral..640533 1. Second iferotion. 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.623467 .and so forth.1.I integrals.8) estimates. The othercolumns the a four-segment applying Eq.1a) involves computing the one-andtwos 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 ) .t:!4 J which is equivalentto Eq. The generalform represented Eq.1-1 I1. Figure18. 8 ) i s t h e n u s e d t o c o m p u t e s( nd the elementIt. where : I is for a single-segment application evaluations that are designated "r sizeisb-a).whereft : I cotresponds the original trapezoidal rule estimates.8)is attributedto Romberg.The first column contains trapezoidal to ($ep 1.r r : the more and less accurate and andllt = the improved integral.-5).r<. Theseformulations be can in expressed a generalfbrm that is well suitedfor computerimplementation: I. Third iterotion.640533 ' 1.640533 ----* 1. the first iteration (Fig.

2presents M-file lbr Romberg an integration. trrep to implement composite tl'apezoidal evalutrtions the rule Here is a MAILAB sessior. Note that the function usesanotherfunction (recallFig. I:t .a )h. is combinedwith 11I usingEq.':l )re The objcctof the second iteration(Fig.068800 | lx1001 :21. fbur-. 3 is Romber-e integlation more efficientthan the trapezoidal rule and Sinrpson's rules. becausewe are evaluatinga filih-order polynomial. (18.For Eq. conrpare new estimate iterative we the with a previous value. the of One methodthat can be employed the present for purposes is :orstiore i.as was donepreviously other in processes.5405 .this evaluationindicates the following percentchangeover the colrrse the first iteration.| .10)is to obtairrthe O(h6l estirnate-Ir.this algorithm By implementsthe methodin an eficient manner..8q t.5 3 i s)e x a c t . 18. lle ep br le er lp. (18. Thus.8) 2 0i]*t'-+b75*x 1. 6 4 0 .I level of integration with j :2).9). a when comparedwith the previousresult 12.l: >> >> f =(i lx) 0.criterionis required e1 to assess accuracy the results. in turn. and eight-segnrent the on trapezoidal rules-that is.640533.I 8.10). cornbined with /1. Sirnpson's of l/3 rulc' would requireabouta 2lS-segment in application doubleprccision yield an estimate to of the integralto sevensignificant di-sits:1. In an eight-segment trapezoidal estimate addedto the first column..For Fig.9) can be appliedto determine that this result represents changeof 1.36i461 .e) wheret.s approxirnate in book.r showinghow it canbe usedto determine integral thepolynomial the of fiom Examole18.and then Eq.640-533...4848.1a.2. usingloops.lc) continues process tlresame the in fashion. For example.. Rombergintegration proln dr-rces sameresult basecl cornbiningone-.is determined.8) 1.the resr-rlt ( l t t : 1 . the estimate fr . we must check to determinewhetherthis result is adequate our needs. When the change between old auclnew values the as represented r:.. To do this.2 ROMBERG TEGRATION IN th 8.0.l. 5lpppin!.623461 The resultis.two-.fbr determination the integralas shownin Fig.r. Afier only three iterations. 18.1:.. contrast. of I r.0. Equation i18. l8.8) t: Now. with only l5 functionevaluations! Figure18.1to yielcl1r : : 1. The third iteration(Fig. 18.8)to generate : |.e.167-167 I I m.2+25*x rombcrg(f.}c/r.a lernrinrrtion.. in fbr As othe-r rnethclds thi. the computation terminated.. 17. : an estinrate the percent of relative error.is below a prespecified by is error criterion0. this case. a four-segment Then it trapezoidal rule estimate.r:lu. (18..r : 1.-lxroo"/.8)is is appliedto computesuccessively more accurate integrals along the lower diagonal.the previousvalue is the most accllrate fiom the previouslevel of integration(i. (18.

location of thebase the points usedin theseequations was predetermined fixed.varargin{ : } ) . or For example..egration lirnits (default = 0. end i I nargin-5tis-mpfy(maxlr...maximum allowable it.p2.end rt nargin<4 isempty (es ) .a. k .) % : Romhero inteoration.Consequently.. 1) .. iter n = 2^iter. if ea<-es.es.as depictedin Fig.000001.a. maxit=50. I8. i terl'romberg ( func. ). b = int. iter while iter<maxit. break. characteristic theseformulas A of (with the exceptionof the specialcaseof unequallyspaced data)was that the integral estimate was basedon evenly spacedfunction values.es..n.iter+l).iter+l) ) /I(1.1) }) .b.1 ) .varargin) quadrature ? romberg: Romberg integration q = romberg(func.1 ) .p2.r (j .b.n. FIGURE I8. es-0 .maxit. k ) = (4 ^ (k . we employedthe Newton-Cotes equations. The formula that is usedto computethis areais = (h + f (u) . I (j . ea. % % input: ? func ..errot ('dL leasu : inpuc argumenrs requireo').2 MJile implement to Romberg integrotion. = trap(func. = iter+l.a. 18. l. end -T(2.000001?) % es = desired relative error (default.1 ) * I (j + 1 .varargin{: I(1. I (iter+1.iter+l) ) "100.1 ) .iter) ea = abs( (I(1.trap(func.end r 1.name of function Co be integrated % a. = 0. for k = 2:iter+l j = 2+iter-k.maxjr.3a.the trapezoidal rule is basedon takingthearea underthe straightline connecting function valuesat the endsofthe integration the interval.pI. end end q = I(1. = 30) ? maxit . 17.b.a.erations pl.432 INTEGRATION FUNCTIONS NUMERICAL OF function le.3 GAUSSQUADRATURE In Chap. = additional parameters used by func % % output: q-integralestimate % (%) Z ea = approximate relative error = number of iterations ? iter if narqin.b. k .1 ) | / ( 4 ^ (k .

Now.3awhere the fbrmula resultsin a large error.1 Method of UndeterminedCoefficients In Chap.Hence. 17. positive the ond negclive errors beller ore bolonced. of Gttussquadratureis the name for a classof techniques implementsuch a strategy.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. The methodof undetermined .3. 18. on improved ond integrol estimote resulls whereaandb:thelimitsofintegrationandb-a:thewidthoftheintegrationinterval. 18.asin Fig.3b. Becausethe trapezoidalrule must pass through the end points.Before describingthe approach. 18. to The particular Gauss quadratureformulas describedin this section are called GaussLegendreformulas.we would arriveat an improvedestimate the integral. there are casessuch as Fig.3 GAUSS AUADRATURE 433 "f("r) (b) FIGURE8. This methodwill then be employedto developthe Gauss-Legendre formulas. will show how numericalintegrawe tion formulas suchas the trapezoidal rule can be derivedusingthe methodof undetermined coefficients. By positioning thesepointswisely. we could definea straightline that woLlldbllancc thc positil'c tnd ncgativeerrors.we derivedthe trapezoidal rule by integrating linearinterpolating a polynomial and by geometricalreasoning.I8. positioning By these points wisely. suppose that the constraint tixed basepointswas removedand we were free to of evaluatethe areaunder a straightline joining any two points on the curve.

'j. Now realizethat the trapezoidal rule shouldyield exactresults when the function being integratedis a constantor a straightline.4 Twointegrols should evoluoted thct be exocfly l h a ' r 6 p " r o . 6 o lr u l ^ .l-t._. the following equalities shouldhold: {i.+ : I'. as I = c g J @ )+ c t f ( b ) (r8. (18.. 18...{ o )o by (b) stroight c line Lonsio"rro'ro To illustrate approach.10)is expressed the Eq.4).434 NUMERICAL TEGRATION FUNCTIONS IN OF FIGURE I8. *. :l r(h-u\ 12 I J -(b-u)12 ldx and b-a 2 o.0.il) where the c's : constants.Thus. .". Two simple equations that represent thesecasesare J : I and ) : -r (Fig.

12)fits the integralof a constant iurda linearfunctionexactly.3 GAUSS QUADRATURE or. the object of Gauss quadrature to detenninethe coefficientsof an equationof the lbrm is I = cof 6il + clf {.3.ls.J(It+ zz b-u^.5).u n c t i o ns yd o i n gt h i s . evaluatingthe integra. Then. 18. y: x r ) l . .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( . However. I: b-a^ . when substituted back into Eq. we cirn obtain two of theseconditionsby assuming that Eq. requirefour conditionsto determine and we them exactly. we now have a total of four unknowns that must be evaluated. Just as for the trapezoidalrule.in contrastto the trapezoidal rule that usedfixed end points a and b.w e d e t e r m i n e l t.Thus.to anive at the other two conditions. | 8. co*ct:b-a and -cu 435 b-a 2 b-a b-o n 2 with two unklrownsthat can be solvedfor Theseare two equations L() Ll - z ^ gives which.5 I Grophicol depiction theunknown of voriobles. the function arguments and r1 &rerot fixed at the end 16 points.rz) wherethe c's : the unknowncoefficients.rt) rrg.rs r1 for integrotion Gouss ond by quodrolure . FIGURE8. (18. consequently.11).I8. ^ J(bl which is equivalentto the trapezoidal rule. ( I 8.2 Derivqtion of the Two-PointGouss-legendreFormulo Just as was the casefor the previousderivationof the trapezoidal rule. but are unknowns (Fig.

/3/ '\'/3/ (r8.\ : ..436 OF NUMERICAL TEGRATION FUNCTIONS IN formula thatis all four unknownsand in the bargainderivea lineartwo-pointintegration to exactfor cubics.. corresponds x4 : 1. w h i c h c a n be solvedfor -. we arrive at the_interesting that is third-orderaccurate. thesevaluescan be substituted into (18. r7) at result that the simple addition of the functionvalues Thus.2^ | ) - EX into this that./: If tlre lower limit. (18..corresponds E q .r :0 (r8. r x +l c l x .( 18./x:o ( y E q u a t i o n s 1 8 .14) (r8.13) Noticethattheintegration and to make the fbmrulation as generalaspossible.f ( t . ( 1 8 .r8) -1. . the upperlimit.5173503. trvo-point the t:r(i)*/f+) " \. ( 1 8 . ( 1 8 . This lirnitsin Eqs.. to give .1/ J3 andl/V3 yields an integrirlestimate through( I8. t. Substituting result Sincex6 and 11 cannotbe equal.-. VJ .rr.. F i r s t .r':]:0. 1 3 )t t b l l o w st h a t ca :61 : I resultsinto Eq. \ r r.I6) arefrom .l : r. 1 4 ) i e l d sc s : c r .r. r : b.1*i : c . : i f rt 2 / 12rlr:= 3 lt pl Jt / . was done to simplify the mathematjcs intothis A simple changeof variable can be used to translateother limits of integration by assumingthat a new variablex.r6) r i r .as in -r:al +azxd (18. ( 1 8 .frr -I /a : -0.The fbur equations be solvedare fl crtltt: J -l I ldr:2 fl | Jl . This is accomplished variable-r in a linear fashion.r1. ( 1 8 . V3 is fbrmula Gauss-Legendre Therefbre. 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 .. 1 3 )t h r o u g h( 1 8 .r2 _ . C o n s e q u e n tfl iy m E q . 1 8r)o y i e l d a : at + a2el) to Similarly.7is relatedto the original form.this rneans o i y E q .15)gives Substituting these : . 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 .13) (18. s o l v e E q .5773503.19) to-r. r t : cut. 1 6 ) .15) (r8.I to l.re .t d.

llr (r 8 .17)to evaluate integralof + . (18.we substitute : 0 andb : 0.22)and ( 18.4 0. theeqtration can be substituted Equarigns transformthe integrationinterval without eff'ectively Thesesubstitutions to be integrated. .the right-hand side is in the form that is suitablefor evaluationusing Gauss quailrature.This resultis a percent which r-epresents 1.Tlris latterresultis to be expected application of rules are also third-orderaccurate.17)is0.4x.However. 1 9 ) n d( I 8 . because the clever choice of Sirnpson's base poinfs.2 * 25r 200.18)to yield (b + rr) 'l (b .900(0.I to +1._ dr.17c.4x7 and dx :0.(18.2+ 25(0. we must perform a changeof variableso that rz the limits arefiom . Use Eq.l 2' rl8.640533.1)a 400(0. 2 3 ) in (18.15 is the between limits r : 0 to 0. E X A M PIL E3 Two-Point the ProblemStotement.2675(0. (18.5167ttl and at function can be evaluated The transtormed asl. Before integratingthe function. 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:: b-a 437 (18.1.r andd. rule or a single applicationof the trapezoidal in magnitudeto a four-segment comparable because ol'Simpson's 113 and 3/8 rules.3GAUSS s y a E q u a t i o n( 1 8 .21) 2 into which can be substituted Eq.1 2 to This equationcan be differentiated give b-u dx .Therefore. Solution.516741* iu:1/J1 : relativeeror of -11.22) a n d( 1 8 . changingthe value of the integral.4+ 0. Gauss quadrafureattains this accuracyon the basis of only two function evaluarions.1) 200(0.8 |I Jt) : dx n2.822578.(18. Formulo Gouss-Legendre 8. To do this. 2 3t)o y i e l d x : 0.a)r. respectively.4 .200:12 6j5x3 .The fbllowing exampleillustrateshow this is done in practice.+ -25r .theintegralaccordingtoEq.f(r) : 0.4* 0.4x.rr+ 675-rr 900"ra 400.4 0.8.4dra + J_l I Therefore.305837 1. atxT : .QUADRATURE 18.r.4dxd to into Both of these can be substituted the originalequation yield r 0.305S3T.8 into Eqs.4-t.4r7)3 + * * [0.900x4 400x5) + + '" ' "" 10.4xi5l0.4* 0.| I \4 as 0.23) fbr. The exactvalueof the integral 1.

f h i k n o w n s F i r s t . be solvedfor -o ^t l '-1 EXA into that.. t 5) gives Substituting these .r as x " x: a t I a z x a If the lower limit. ( 18. ( 18.l results into Eq.I 3) through( 18. .l3) it follows that Eq.5773503.f ax:O (18.17) result that the simple addition of the functionvalues at Thus. variablecan be usedto translateother limits of integration intothis A simple changeof variable. x : d. ( 1 8 .r1.7: to 1.--. to give (18.rl : .the two-point Gauss-Legendre formula is t : f l " \ . t . 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 . 11 8 . ( 1 8 .16) can be solved sir.This was done to simplify the mathematics and to make the formulation as generalaspossible.0..14)yieldsc1. thesevaluescan be substituted into (18.s o l v eE q . 1 8t)o y i e l d 0:a1*a:(-l) Similarly. ( | 8. v --) /..( 18.1t:= -l J-. corresponds xt: to E q .the upperlimit. Tlris is accomplished assuming by that a new variable in a lineartashion.16) arefrom .-5773503.16) for Equations(18. Notice that the integrationlimits in Eqs. j' . cr . we affive at the_interesting r : . Consequently c r t: c t ./3 and I /V3 yields an integralestimate that is third-orderaccurate. i .13) through (18.rJx:0 ) (ir-ruT(l. corresponds x. \ ' r:) xt: I .r7 is relatedto theoriginal form.t\ r l\ Therefore. ^. this r.. ' / 3 /l + f l ^ \'/3/ -l (18. 1 6 )w h i c h c a n . ir. : v') I /::0. 1 r i : / *l. rl c r r x i*]c 1 x f : J "-1 | .19) .| l.436 NUMERICAL TEGRATION FUNCTIONS IN OF is all fbur unknownsand in the bargainderive a linear two-point integrationlbrmulathat exactfor cubics. Substitutingthis result Since xe and x1 cannot be equal. The fbur equations be solvedare to c a* c 1 : l'.re .18) -7.neans : from Eq.1 to 1.nultaneously the fourun..t : b.

we must perform a changeof variableso that thelimitsaretiom-lto+l.:_ and b-a (tZ: -. 3 0 5 8 3 7 .4-r.f (.5I 674 I and at The s l t T . transformed function can be evaluated x.r 1510.900(0.7)l .2'*.r . ( I 8.Todothis.8.1x.i ) 0 .8intoEqs. 3 Two-Point LE the of ProblemSlotement.4.)a + 400(0.2+25rc.22) to This equationcan be diffbrentiated give dx : b-n d.i and th :O.1) in for-rEquations \18.11 400x5)r/r * + '"'nl -lLv'!'-"\ I n.14 400x5 is the between limits x : 0 to 0. 1 7 . respectively.4*0. / : I l " E a s 1 .)2+675Q.231 yield r : 0. because the clever choice of Simpson'srules are also third-orderaccurate.This latterresultis to be expected of However.4.19)and (18.21) into which can be substituted Eq.2. Jo : rc.QUADRATURE 18.200x2* 67513 900. base points.h e i n t e g r a a c c o r d i n go E q . h e r e f b r et.u I .4elxd into the otiginal equationto yield Both of thesecan be substituted n 0.l7o.2 * 25. . changingthe value of the integral.4x.4 t0.4i_0.640533. Before integratingthe function.3GAUSS for (18.x) :0.22) to and ( I 8.4 * 0.' '25.r. Gauss quadratureattains this accuracyon the basis of only two function evaluations.t 2' (18.20)can be solvedsimultaneously Equations at : b'ta -.4d x. applicationof the trapezoidalrule or a single compa'ablein magnitudeto a four-segment because of application Simpson'sl/3 and 3/8 rules.the right-hand side is in the form that is suitablefor evaluationusing Gauss at quaclrature.1 4 J _l Therefore. (18..(18. Solution.I 8) to yield (b*a)l(b-tt)x.r . theequation effectively transformthe integrationinterval without Thesesubstitutions to be integrated.1: .822518.t : ---- 2 (18. ( 1 8 .4x.r 20012 6i5r3 . 437 (i8.23)canbe sLrbstituted anddx. Use Eq.The following exampleillustrateshow this is done in practice..4+ 0.900.22)and (18. The exactvalueof the integral 1.| I J1 as 0. { . This resultis which represents percent a 1.200(0.t. 5 1 6 7 4 1 + : relativeerrorof -lI.305837 1.17)to evaluate integral + .wesubstitutea:0andb:0.4* 0. Formulo Gouss-Legendre E X A M P1 8 .

1 : 0. According to Table 18.L e g e n d rF o r m u l o IE Problem Siotement.114s967) + 0.r:{tB-. 1 6: 0 .1to estimate integral the for the samefunctionas in Example18.66 I 249386466265 .28t3013 + 0. ./to'({) c6:{lB-"tr6ltso c j: ( 1 8 + ' t r d t s a c::lu+V.7 O \ / 3 0 1 3 5 x2: y'\15 -7sJfr135 = 1t8){{) c.0 2 3 8 6 1 98 6 0 8 39 Z l I x::O2386l9l86083l97 = -ra 0 661209386466265 xs:0. 0 Truncolion Error = -ro: -tl : I lJ3 l/VJ 1t2){{) G) 7t1) co : 5/9 (t : B/9 cz -* 5/Q : r11 -nl3/5 .Ul /JO x o: * .3. + cu-rf(xn-t) (18.y : -li4514Jfi121 . Weighting Foclors co:2 co: I l Function Argumenls .7'74s961) + f which is equalto : / : 0.rt : 0.:n/1[i-14ffip1 u: '/ltii1tJfrlzt I 8.rr : : .I .640533 + which is exact.trotza :1322 t3.24) wherer?: the numberof points.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 1 7 1 3 2 4 4 9 2 3 7 9 1 7 A '.r. 4 6 7 9 3 9 3 4 5 7 2 6I9 1 c t : 0 .:: 0. r z .1 the three-pointformula is .l 3 ! r y d / 9 O A rx:0171324492379174 t't:O360761 73048139 5 c z: 0 . : Jili +7aJi6t35 - ro:-t/245+14\/7Al2l . Use the three-point formula from Table 18. 4 T h r e e .0 - /{to)16. 4 6 7 9 1 3 9 3 4 5 7 2 6 9 1 c z: 0 .. / 5 2 5+ 7 0 \ / 3 0 1 3 5 r r : .5555556f(-0. ..3.438 NUMERICAL INTEGRATION FUNCTIONS OF TABTE| 8.t Points I 2 foctors ond function orguments Weighting usedin Gouss-Legendre formulos.3 Higher-Point Formulos Beyond the two-point formula described the previoussection.v 5 2 5.higher-pointversions in can be developedin the generalform I = c s f ( x 0 )+ c l l ( x r ) + .555s556 (0. in G e E X A M P L8 .P o i n t o u s s .Valuesfor c's andx's for up to and includingthe six-point fonnula are summarized Table 18. .4859876 1.9324695142A3152 . 3 6 4 7 6 1 5 2 3 0 4I8 l 3 r .88888891'(0) 0.0 X2: \/ J/) =. ro : -a 9324695142031 52 16. Solution. 1{tzr -0.8132444 0.

Adaptive quadrature ing the stepsize so that small stepsare takenin regionsof sharpvariationsand largersteps gradually. are taken where the function changes l/3 rule to the Simpson's are Most of thesetechniques basedon applyin-u corrrposite rule was usedin Rombergintegrain subintervals a mannersimilal to how the trapezoidal tion. The following function syntax for the quacl function is the same for the qr-Ladl function: g = qu.it is not suited This is particularlytrue the function is known. b. tt ace to use the def-ault .It shouldbe noted that array operators.botlr basedon algorithrns (2000). a. values.It may be more efficient for high accuracies and smoothfunctions. . must be performed. / are parameters that yor-r pass emptymatrices rol or fbr and . t. rra.If the truncationerror is acceptable. or quadl.4. fine spacingmust be appliedeverywhereeven though it is only neededfor the regionsof methodsremedythis situationby automaticallyadjustsharpchange.4 ADAPTIVE QUADRATURE the Althoughthe composite Simpson'sI /3 rule can certainlybe usedto estimate integral points. ln addition.I 8. the error estimate too large. until the enor falls to acceptable MMLAB includes two built-in functions to implerrrentadaptivequadrature:cluad describes how they can be applied. for where within the integrationinterval.the stepsizeis r-efined the process levels. nonzerovalue causes additionalcomputational want to passto f rrn. That is. hasthedisadvantage it uses doesnot take into account that somefunctionshaveregionsof relativelyabruptchanges the where more refined spacingmight be required.-e is a variablethat when set to a desiredabsolute error tolerance detail to be displayed. This constraint equallyspaced it that of givenfunctions.rrther refinementis required and the integral estimatefbr the subintervalis deemed repeated and If is acceptable.rol : the a where fun is the functionto be integrated. .tcl achievea desiredaccuracy.4 ADAPTIVE QUADRATURE 439 points requirestunctionevaluations nonuniformlyspaced at Because Gaussquadrature the functionis unknown. This function useswhat is calledLobotto quadrature. pl. taf . This function usesadaptiveSimpsonquadrature. when numerousintegralevaluations I8. wlrere forengineering that data.it is not appropriate cases problems dealwith tabulated However. and b: the integration (default: l0 6).race.and pt.The following section 18. for irnplementingadaptivequadrature: . the no fi. the 1/3 rule is appliedat two levelsof reflnementand the differencebetween thesetwo levelsis usedto estimate truncationerror.1 MATTAB Functions: quad ond quadl by developed Canderand Gautschi MATLAB has two functions. may be more efficient fbr lt low accuracies nonsmooth functions. its efficiency can be a decidedadvantage. p2. p2. quad.^ shouldbe usedin the definitionof fun.Hence. and quad1.ad(fun. .) bounds. * . Thus.

11.^2+0.r:0./((x = m"yhumps(x.thisisthebuiltin humps function that MATLAB usesto demonstrate some of its numericalcapabilities. lo. . we can develop an M-file for the function: functi-on y Y .ands:6. the computation executes f'aster. .85832612842164 Thirs.4 . 0. .l8.usingthebuilt-in version of humps along with the defaulttolerance: >> format lonEt >> quad (@humps. First.85812133214492 Notice that because we used a larger tolerance.scorrect to . and .q)2+ 0.seven-si-clnificant digits. hence.5 Adoptive Quodroiure EXAMPLE Problem Stotement.9.01 (x .04) si Then.01) + l. the ./ - ((x r). Bockground. we can integrate it with an error tolerance of l0-a as in . > > q u a d ( @ m y h u m p sO .r)2 + 0.NotethatforQ:0.04 -s belweenthelimitsr:0tol. First.-rmps functioncan be integrated integralof 29. Notethat routines analyticallybetweenthe given limits to yield anexact the t. although it would not be apparent liorn a single application.0.3. fewer function evaluations were made and. 6) 29. Hence.r.q. we can sojve the sarneproblem.^Z + O. 1 . but using a looser tolerance and passingq. Because resultsin efficient energytransmission.it is uselulfor demonstrating testin-s and like quad and quadl.9. Usequadto integrate following function: the (.ur: the peak current (A).1.r as parameters.r. the result is now only accurate to five significant digits.solution i.r range. let's evaluatethe integralin the simplestway possible. Next. ar : the angularfrequency (radians/s)and / : time (s). 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). 39549861 Solution. However. 1 ) 0 29.440 NUMERICAL INTEGRATION FUNCTIONS OF .s) cl). r. l e .858325 . The humps t-unctionexhibits both flat and steepregions over a relatively short. The angularfrequencyis relatedto the period r(s) by a =zrlT.3.

(A). electrical engineersand scientistsdetermine the root mean squarecurrent i*.70707. 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.2s) Thus. the rms current is the squareroot of the mean of the squared current' Because1/ \/2 : 0. In this case study.For a resistor. We will use both the Newton-Cotes formulas from Chap.. i. the AC circuit generates equivalent power as a DC circuit with a consrant the current of 1*r. although the simple sinusoid is widely employed.For some of theseforms. Now./) . the l*" can be evaluated analytically with closed-form integration. as the nameimplies.-!:= (. (r8.slqw states that the voltage is directly proportional to the currenr: V:iR whereR : the resistance : V/A: (Q P:izR (18. 17 as well as the -__--^^^L^^ )^^^. some wavefbrms must be analyzedwith numerical integration methods.Substituting Eq (18. However.. result: r E -_ . an alternative to the averagecuffent must be derived. which is calculated as il"ssin?(att)dt ipea. Ohm. lpear sln(tr.k /. it is by no means the only waveform that is used.I8.26) whereP: the power (W: J/s). is equal to about j}vo of the peak current for our assumed sinusoidal wave form. Integration can be used to determine the averagecurrent over one cycle: . : I fr. 2 c r \ p m Thus.l^^A it}|ic nhanfar . we will calculate the root-mean-square current of a non-sinusoidal wave form.".and y: voltage(V: J/C). Therefore.. To understandthis.28) The average powercanbe determined integrating (lg.21)into (18. This quantity has meaning becauseit is directly related to the averagepower absorbed by an element in an AC circuit. To do this. s/Cz).26)gives (18. such as triangular or squzue waves.5 CASE STUDY 441 continued The power generatedis relatedto the magnitude of the current. l'.r.2g) overa period with the by Eq.27) J . such a cuffent is capable of generating power.cos(2n) + cos(Q)) dt :0 T Ju Despite the fact that the averageis zero.

k. 15 . We can obtain an even better resultif we impose a rnore stringent stopping criterion: r5. trapezoidal The M-file we developed in Fig. a changein variable is performedby applying Eqs.442 NUMERICAL TEGRATION FUNCTIONS IN OF continued Solution. (18.0 0 8 5 Thus. 4 8 0 0 5 8 7 8 7 3 2 6 94 6 e .)sinzt(0.the exact value of this integral to fifteen signilicant digitsis 15.23)to yield 1lI t:-*-t.: Io'' dr sin2nt)2 eoe-t (18. Integral estimatesfor various applications of the trapezoidal rule and Simpson's1/3 rule are listed in Table 18.^2.29)to yield t.*sin(2*pi*t) | .t " 4 Theserelationships can be substituted into Eq.1". 18. The value for the integralto sevensignificantdigits is obtainedusing a 128-segment rule or a 32-segment Simpson's rule.29) For comparative purposes.25q)l'0.2.30) J-t .4r'2608A4288977 1 .First. Theintesral mustbe evaluated is that .25 dt / [l0e-.o (18. (18.2 can be used to evaluate the integralwith Rombergintegration: >> format longi (10*exp(-t) >> i2-@(t) .41260804810169.r 4 4" dt:-dt. we obtain a result that is correct to over nine significant figures in five iterations. with the default stopping criterion of es : 1 x 10-6.22)and (18.Notice that Simpson'srule is more accuratethan the trapezoidal rule.4L260804810169 0 i ra- l Gaussquadraturecan also be used to make the sameestimate.25 zs+o + 0.: lt -) zs'.

.2 Iechnique Tropezoidol rule I 2 4 B )b 32 o4 I?B 2 4 B t6 32 0. Finally.b 20217688657 t 5 .a L B 2x l 0 . 0 1x l 0 .412607893 lOl x 10-6 1 54 1 2 6 0 8 0 3 8 6 .b08C1 4509 493 .3 Poinfs Results usingvorious-point of Goussquodroture to formulos opproximote integrol.1c/o.6Vo. The three-pointformula is (Table 18. the Estimote e. \tr.4 5 6 t L9978243 15.412568151 259x1O-a 15412605565 I6lxlO-s 15. l"/"1 22. 2 8x l O .A725 l 5 4 11 9 5 8 3 6 0 4 .5 2 3 . 4 2x l 0 .0 r00 0000 1s.5555556(2.17)to yield an integralestimateof 11.4I2. 5) i.480816629 r5415468il5 I5 412771415 l5 412618037 3\ 1763 4.0r86 I 06x 1O-3 6 48 x l0-5 S i m p s o n1 / 3 r u l e s TABTE18. at 1/V3. which represents an error of €r : 22. 4 1 2 6 3 9 1 154126109 Forthe two-point Gauss-Legendre formula.3 15.respectively. 0.1 1.65755 which has €t: l. (18.16321)+0.684096and 4.4426 0.401429095 0.684915):15. rms:2 .I8.t : -1 I J3 and.8888889(15. the integral can be evaluatedwith the built-in MATLAB function quad and quadL: >> irms2-quad(i2.237449)+0.65755A2 r5 4058023 I 5.1) 1:0.313728. These values can be substituted into Eq.The resultsof using the higher-pointforrnulasare summarizedin Table 18.59 4 .99182.-. Segmenls Inlegrol e..2 2 . with the resultsbeing 7. 2 2x 1 O .5555556(1.3.5 CASE STUDY iir' rh-T:'- " continued for Volues the iniegrol colculoted usingNewton-Cotes formulos. this function is evaluated t.l"/ol TABTE18.163266493 ) 6178 15.

(b) the two-point Gauss integralto deterrnine percentrelativeerrorof theresultobthe formula.r I to an accuracv s.rr 0./r r. : 0. (18.5%). 11 : 6 0 8 0 4 8 C 9 9 5 7 r Both these results are very accurate. As we did for the simple sinusoid in Eq.vith Rombergintegration. 5) 18.25).i: where the pip meter. is lessthane. 2 2 4 B 1 9 4 0 3 1. t: I xe'd."8 ] 6.444 NUMERICANTEGRATION FUNCTIONS IL OF continued >> irirs2=quad1 (i:. 0. an interesting calculation involves comparing this result with the peak current.92588945948554 This result could then be employed to guide other aspects of the design and operationof the circuit such as power dissipation computations.(). of the n (a) Rom two-poir quad fu Iti. . We can now compute the i.rr -1. the root-mean-square value is about 49. B B 6 B 5 3 8 l3 9 3 2 5 A A maximum cuffent of 7.5 Th where .1.to I .0547. t (r) i(I) Evalual ance ol formulr 18. (d) MATLAB quadfunction: and .2249 s.-).8 l riod oi M ..7 .l Use Rombergintegrationto evaluate Gaussquadrature tbrmLrla. Recognizing that this is an optimization prob lem.2917x2d. 18. Checkthate.s6-t6. (c) the three_point (F" Ji 1: I {:. Use the analytical solutionof the quadrature eration (tr :0.88685 A occurs at t : 0. -r 1562.. for this particular wave form.n by merely taking the square root of the integral.3 Evaluatethe tbllowing integralwith (a) Romberg intein the tbnnat of Fig. we evaluate the negative of the function: Use the formula error for termine followir 1. using ( two-p( quad'l 18.4 Tt erf(c irms2 = 1 5 . of Your resultsshouldbe presented 18. Becausewe are looking for a maximum. Hence./ Jt \ f: / 1\- | : .5a/.87o of the maximum.7 T represe PROBTEMS l8. we get >> i rrrs=sqrt.6 Th I*^ ForT = 0 . and (c) MATLAB quad and quadl functions: tainedr.9 3 2 L inax = .: r8.5%).2 Evaluate the following integral (a) analytically. . For ex" arnple.(irrns2 ) irms = 3.. using the result computed with quadl. with quadt being a little better. 73 18.t rk\ Dnmhprcinreorarion :0. we can readily employ the fminbnd tunction to determine this value.

and three-pointGauss-Legendre of 1= I s.5.Compute F fbr the case where I1 : -10using (a) Romberg integrationto a tolerance e.:cr function.ll The alrount of nrasstransported a pipe over a pe.10 Computework as described Sec.PROBLEMS 18. : 0.: the elevation abovethe deck and 11: the heighr of themast.9 Evaluate doubleinteglal the \ / t--.r * * The force is in new.045.1Vc: (b) the MMLAB qu. and . E For : l.r Jn Jt sentations define the temporal variations in flow and Use (a) two-point and (b) rhree-pointGauss-Legendre croncentration: the formulas estimateeri( 1.r2 .5 e . Q1r) : flow rare (m3/min).v implementthe tunction. ) " " (b) AssLrrne Ohm's law doesnot hold and that voltage that and currentare related the following nonlinear.ll Perfbrnr samecomputarion in Sec.7The velocity profile of a fluid in ir circular pipe can be 18.t cr.6 cunent cln tre cornputed as l_.r): for F(./' 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 1o-5 using (a) Rornbergintegrationto a tolerthe i(t) :0 for Tl2 < t < T ance 0. and (c) rhe MNLAB q:r.(b) the two. l. Usehetp to understand hor.-' .I.5olc.6x-0.4r1 enorforeach based the true value. formulas.25i3). r: : rhe llnal tirne (rnin). and "vhere 18.5.12 Computethe power absorbed an element a cirby in cuit as described Sec. (b) the of Ilo-poinl GaLrss-Legendre fbunula.5). l8. but usethe in tbllowing equations F(_r)and d1. i n ( " 4 ) qu:r. Theforce on a sailboatmastcan be represented the 18.2 -r.000-5-5rr 0. r1. (a) Assumethat Ohm's law holdsand R : -5e. (c) the MATLAB quac function.o-point Gauss-Legendre formula.' Qft)c(t) dt Compute the averagevoltage over / : multiple-segment Simpson's /3 rule.perfbrm the integrationf}onr -r : 0 to 30 m. fbr the as but the curlent as specifiedby 'rrr: I\nu.F = I too{ ----.relaby where : velocity. . ancl (c) the MATLAB i ( r ) = ( 6 0.r.4 Thereis no closedfcrrnrsolution fbr the error functiol 445 whereM = nlass(mg). to 18. Determinethe f'low in the pipe if r() = Q.9.r i ..) : a rd = concentration (mg/rn3).icos21o.dI'unction./. to is l8.0123.5 by Determine the mass transported between 1r : 2 and following function: l: : 8 min rvith (a) Romberg integrationto a toleranceof rH 0..' (.Determinethe percentrelarive to QQ):9+.:il function.12 0.r):1.and the resistance a function of the current: via riodof tirne can be cornputedas R : l O i+ 2 i 2 / 3 = n. 1 0 to 60 using the .i tunction. suppose f that i (r) is definedas : t ( r ) l o d .3-r. L.but for a simple sinusoidal represented in as currenti : sin(2trt/T')where I = I s. n d.. rr : the initial time (min). the (b) of tu. l3. j7. = r o ( r. I 7o.. I %.and n : a paraItt./ Jo where.l3 Supposethat the currenrthrough a resistoris demeter. theprpes centerline.r0 : the pipe's radius./ ) 2+ ( 6 0. r : radial distancemeasured u out front tionship:I/: (5i .Note that flow is eqLral velocity times area.r'3) r1-r.12 d(t) : -0.75 andn = J scribedby the function (a) usin-e Romber_e integlarionto a tolerance 0. Theroot-mean-square 18..which canbe decase on ' * c ( t ) .r * (a) analyticallylnd (b) usinr the MATLAB dl. The following functional represySqal :f | . 18.tons anclthe angle is in radians. and c(r. ).18. 18.( )5 / Z e t t ' 1 s r rvith termined MATLAB's built-in tirnction +r:f .

l7).I u(2rr)dr ity of an object in the dircction of a fbrce is given by JIJ llt. Io-3 A o2 and be computedby Q : J u tl A. 1 5 52. A ol is the pipe's cross-sectional area. Pl8.18 stretcl tor: M o d u l u so j t o u g hn e s s F.use the fbllowing currentdata to develop mericalintegrationto computethe modulusof toughness curve seenin Fig. Therefore. I 6b.0082 0 )441 tween summation and integration.0 0.Usenufol If C: l0 s farad.l5 The work doneon an object is equalto the fbrce times rl the distancemoved in the direction of the force.i6n) will be to curvein Fi P I 8.16 A rod subject an axial load (Fig.as shownin the stress-strain r FIGURE I8. where u is the velocity.1 the vr U u U 0f out The areaunderthe curve from zero stress to thepoint of It ruptureis calledthe modulusof toughness the material. 1 0 43. llt.l7 If the velocity distributionof a fluid flowing through a pipe is known (Fig. the stress-strain a plot of voltageversustime: Itt. providesa measure the energyper unit volumerequired to of lfr ol causethe materialto rupture. The velocO -.0 0 .02 40. 2 2 8 2 volume of water passingthroughthe pipe per unit time)can i.) For a circular pipe.l{ If a capacitorinitially holds no charge.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 . fn F.n ds t r o i n d l m e n s i o n l e s s p 18.s 0B bethis relationshipphysically. Itl. Pl 8.it is representative Vttt:.25 55.(To graspthe meaning ) 12 t.the 04 06 t'3 0 a2 03683 038t9 0 . l( rrfi (al FIGURE I8.IQ x. I 60.0 0 .0 Ruptu re where using result 18.I7 P . 2 0 60. A: r12 anddA:2rr dr.05 0 .0 0.446 IN OF NUMERICAL TEGRATION FUNCTIONS where center 0.As such.recall the close connection i. Io-3 A 0 0486 0. Determinethe work if a constantforce of 200 N is appliedfor all r. I ittllt LJo the material'sability to withstand an impact load. the flow rate Q (thatis.the voltage acrossit as a function of time can be computedas u:4t u:16*(4-r)t 0<t<4 4<t<14 Al I I where r is in m/s. 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. deformed. Pl8.

/ / -.22 Use Rornbergintegrationto evaluate ^)/ .determine kg.rno. \ r . : 0.andg : downward acceleration gravity (assumed of conslant: 9. \l'6 ltt.^ --E:e-\-t2 \/ L]T (in where is the total ladir"rs this case. 0 0 0 063 020 00r 005 O C)82 025 0 028 o l0 0ll 0.r : . / t .18 a spling that has a springconstantof ft : 300 N/nr slretchrng : to. I and frorn (b) Use MATLAB to determinethe inflection points of this function. rrf. n. 18. 0 the trapezoidalrule. L 0<l<10 l0 < r < 20 2O<t<30 I -d.tno qt / whererr: upwardvelocity.20 The upward velocity of a rocket can be computedby the fclllt>win-e tomula: r. 18. .21 ]'he not'rraldistributionis definecl as .l 0 3 .000 and q : 2500 kg/s. \.PROBLEMS r where is the radial distancemeasuredoutward from the ol is center thepipe. r . Evaluatethe vertical dislancetraveledby a rocket if 18..\to an accuracyof e.) .rt : t... Discussthe using multiple-application results.30 0 046 0 15 0 13 035 N 4 t03.N rrf.€'sln-\ Jo l +.calculatethe work done by 18.3 cm).8 m/sr). q : fuel consumption rate. I 8. how high the rocket will f1y in 30 s.Your results should be presentedin the form of Fig.s .initial massof the rocket al time / :0.5 r r'=-50I+2(t-20)) .35m: F .19 vertical velocityis given by the r':lll---\1 (a) Use MATLAB to integrate this function fiom. : velocityat which fuel is exrr pelled relativeto the rocket.r r l n { "-' ' . : 160.I to -2 to 2. Usingthe following data.= 1 1 0 0 .r 0.=lIl-]l \ / .57c. conrpute r.If the velocilydistribution given by 447 18.If I : 1800m/s.

Understanding how Richardson extrapolation appliedfbr numerical is differentiation.rtour YOU'VE GOT A PROBTE'I/I R::*. z z(t.*::TJ u(r ) velocity of a free-talling bungeejurnper as a function of time canbe At the beginningof Chap. Specific objectivesand topicscoveredare ' ' ' ' ' ' the Understanding applicationof hish-accuracy numericaldifferentiation lbrmulas for equispaced data. we usedcalculusto integratethis equationto determine the vertical distance the jumper has fallen after a time /.Differentiotion Numericol CHAPTER OBJECTIVES The primary objectiveof this chapteris to introduceyou to numericaldiftbrentiation. 17. Recognizingthe sensitivityof numericaldifferentiationto dataerror. Knowing how to generate plots and vector fields with MATLAB. col.) (19. Knowing hclwto evaluatederivativestbr unequallyspaced data. Knowing how to evaluate derivativesin MATLAB with the dif f and qlradient functions.2) .

y A. (19. To do this.l.) A.".I INTRODUCTION AND BACKGROUND | 9. we could either take the first derivativeof velocity.f (xi + Ar) . Substituting (19. 5 ) Although a closed-formsolutioncan be developed this case.suppose that there was some way to measurethe jumper's position at various times during the fall."Mathematically..t. resultwould be the a(t) :S . distinguish.t\i/:-----:-: d 2 z t tl dt dt2 (19. that calculusis an essential tool of our profession. 6 ) where -y and /(:r) are alternativerepresentatives the dependent variable and x is the for independent variable. depicted Fig. the difference a becomes derivative: -' dr - {r+0 lim .That is.thereare other funcfor tions that may be difficult or irnpossible differentiate to analytically. which servesasthe fundamentalvehicle for diff-erentiation. I Whor ls Differentiorion? of Calculusis the mathematics change. Because engineers scientists and must continuously deal with systems and processes clrange.ftt ( #. . I9. Thesedistances along with their associated times could be assembled a table of discrete as values./(. In numericaldifTerentiation methods are availableto obtain solutions. derivetthe in the tive. the As in mathematical definitionof the derivativebeginswith a difference approximation: A./("r.Further.r (t9. to perceive difference or between.asoccursin moving from Fig. This chapterwill introducevou to someof thesemethods. wouldbring us backto Eq. you were askedto dethat you were given the reverse termine velocity basedon the jumper's positionas a function of time. ( 19. .) (1 9 .1).r f (xi + Ar) .I INTRODUCTION BACKGROUND AND 449 Now suppose problem. you might also be askedto computethe jumper's acceleration.1a to c. 19. or the secondderivative of displacement: du(t\ . at 119.2)into Eq. to dffirentiate means"to mark off by differences.4\ In eithercase.In this situation. Standingat the heartof calculusis the mathematical conceptof differentiation.7) .3) Eq.If A:r is allowedto approach zero.it would be usefulto differentiate discrete the datato determine the velocity and the acceleration.3)anddifferentiating Beyond velocity. 19.) Ar (1 9 . | . both theseinstances. According to the dictionary definition. differentiationcould be usedto make the determination: r'(l): dz0) .I9. Because is the init verseof intergration. represents rate of change the variablewith respect an independent to of a dependent variable.

l i m 3y Ar-o / ( .thedifference of os becomes derivotive. r') A.t') .9) Similarly. y' Note that Newton used the so-calleddot notation: i. o whereh ldx [which can erlso designated . r ' ). whereas is attributedto Lagrange.given a function/ that depends both . r .r.8) Thus.r e v a l u a t e d a t r .f that yields elevationas a function of longitude(the east-west oriented 'The fbrm dt'ldx was devisedby Leibnitz.r-.r all but one variableheld constant.r..)is detined and as af 3. + ar AI (b) (c) F I G U R EI 9 .r Ax ( 19. r0) To get an intuitive graspof partial derivatives.partialderivatives usedfor lunctionsthat depend morethanonevariable. . 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. the partial derivativeof/with respectto. the secondderivativetells us how fast the slopeis changing.r.r+o ( 19.rr. Todav.ri) ]' is the first derivative ./(. 1.{ at an arbitrarypoint (x. l c . l ' ) Ay (19. ./(r * A. the partial derivativeof. Ar.l/ o. f(x) r./with respectto y is definedas a" f.r'with be oS of r e s p e c t t o . .f '(.ature.450 NUMERICAL DIFFERENTIATION + . the #:*(#) . .Suppose to access a function.recognizethat a function that depends on you aremountainclimbingand have two variablesis a surface ratherthan a curve. . on y.r ' * A .because high value for the secondderivativemeanshigh curvature. I Thegrophicol zero opprorimolion defrnition o derivotive: Ax opprooches in goingfrom(o)to (c).the dot notationis usuallv usedfbr time derivatives./ ( x . are on Partialderivativescan be thoughtof as taking the derivativeof the functionat a pointwith For example.l(.It is commonlyrefened to as the cun. A s s e e n i n t h e v i s u a l d e p i c tF og . The secondderivativereDresents derivativeof the first derivative.o9 . a Finally.

rn.' ax ( 1 9r ) r.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.I9.this can be expressed mathematically as Q: . numerouslaws involving the spatialbehaviorof variablesare expressed terms of derivatives. slope negotive thiscose.Thisis iheorigin lhe"minus in Fourier's of XeotcJnduction. For example. fact. depiciion o temperoture of Becouse moves heol fromhighlo low lemperoture. -r' -r'e).wherethepositrve in lo grodlenl leods o negotive flow fromright left heot to Directionof heatflow . negolive o grodient leods c positive to flow. where q (. andx : distance (m).Many specificexamplesof suchapplications could be given in all fields of engineering and science.) I 0.andthe slopeto the noth would be 8/(xo.r) : heat flux (Wm2).If you stopat a particularpoint (.r axis) and latitude(the north-south oriented axis). k : coefficientof thermalconductivitytW(m .1.r.Fourier's law oJ heat conductionquantifiesthe observation that heatflows from regions of high to low temperature. K)1. A prime exampleis Newton's second law. FIGURE9.y. f : (K). 19. reverse of low The sign coseis depicied {b). is for Thus.whichdrivesthetransferofheat(Fig. d T . of theintensityofthespatial temperaturechange.I INTRODUCTION BACKGROUND AND 451 . flo* in {o)is fromleftiorlght However. to theorientotion Corteiion ihu due of the coordinoies. Thus. Aside from suchtemporalexamples.providesa measure temperature . in Among the most colnmon of thesearc the cortstitutive /au's that define how potentialsor gradientsinfluencephysicalprocesses.K. Differentiationis commonplace engineering science in because much of our work involves so and characterizing changes variables both time and space. y6)/3"r. the slopeto the eastwould be 0f(xu.2 I "downhill" Grophlcol grooient.the derivative or gradient.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. 19.2). which is not couchedin termsof thepositionof an objectbut ratherin its change with respectto time. For the one-dimensional case.

the underlyingfunction is often unknown deIn and points. These arejust a few of the applicationsof differentiation that you might faceregularly you in the pursuitof your profession..rorio1 Mc.r | -t^ du . Recall tha we employedTaylor series expansions derivefinite-difference of to approximations derivaforward. In Chap. ax Velocity Sheor Sfress Siress Dynomic Viscosily Young s Modulus o : EL-! L Deformction Sirnilar laws prcvide workablemodelsin nranyotherareasof engineering science.1).2 HIGH.4. chernicalreactionkinetics. When the functionsto be analyzed simple. we developed difference approximations of tirst and higher derivatives.4. Iu Heod For'vflux Currenflux t ic Hydrou Conductivi\ Elecfricol ConductivilY _^ dv _ d. have ability the valuesfor derivatives. Beyond direct engineering and scientificapplications.dh dT Mossdiffusion Flowthrough porous medicr flow Currenl Fluids Eosticily Cor.wewillinvestigatehow numerical differentiationprovides the basis for solving boundary-value problemsof ordinary differentialequations. next. to obtain approximate usingnumericaltechniques described as EXA r9.theseestimates errors were had that O(h2)-that is.andcentered tives. masstransf'er. addition.probablythe mostimportant of appliWe cation of numericaldifferentiationinvolvesthe solutionof differentialequations. 1.e.recall that in Chap.v Theone-dimensionol 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 . willnorare mally chooseto evaluate when them analytically.at best.In Chap. 6 the secantmethod was basedon a finitedifferenceapproximation the derivative. their enors were proportionalto the squareof the stepsize.K *.The ability to accurately is derivativesan tricity.Thislevel of . and elecincluding the modeling of lluid dynurmics.452 DIFFERENTIATION NUMERICAL TABTE| 9. numericaldifferentiationalso is importantin a variety of generalmathernatical contextsincluding otherareas numerjcal of methods. I Low Fourier's lor.r .For example. g Equotion q : . (Table19. However.ACCURACY DIFFERENTIATION FORMUTAS We have already introduced the notion of numerical differentiation in Chap.ln addition. 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.it is oftendifficult or impossible the function is complicated.backward. Remernber that. and solid mechanics estimate importantfacet of our capabilityto work effectivelyin theseareas. have alreadyseenan examplein the fbrm of Euler's methodin Chap.22.For both these you rnust fined only by measurement discrete at cases.

19. r *. l 9 t.27)]: the following forward-difference approximation the second of . to Notice that incl-rsion the second-derivative of Similar irnprovedversionscan be developedfor the backward and centeredformulas as in of The formulasare summarized well as for the approximations higher-order derivatives. For example.): tSt# - +r+ o(h2) (19. Recall that in Exarnnle4.f'(x)h * which can be solvedfor (1e. 5 .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. f (ri+t) : f (xi)+ .3throueh .the forward Taylor seriesexpansion can be written as [recall Eq.. (4..I 9. 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.2 HIGH-ACCURACY DIFFERENTIATION FORMULAS derivationof thesefbrnrulas. Fig. ( 1 9 . 1 3 ) o y i e l d t J l x i + z t.Gi):W+o&) i n t oE q . 19.0 . 2 and a step sizeof h: 0.f'(. / t x . (a. zn' or. .r4) In contrastto this approach. 2 5 x* 1 .5 using finite-differences in the following table.1-5) ( l s . now retain the second-derivative we term by substituting derivative[recallEq.x):-ry+o(h2) (r9. The resultsare summarized at -r : 0.4.1 6 ) . 4.0 . r ' 0 .25. 1 5 x ' . H i g h .f..1 ) + .5along with the lower-orderversionsfrom Chap. ) . wrll now illustrate We how high-accuracy finite-difference formulascan be generated includingadditionaltermsfrom the Taylor series by expansion.-lthroughFig. we truncatedthis result by excludingthe second-and higher-derivative tenns formula: and were thus left with a fbrward-difference + o(h) (r9.. 1 . ing exampleillustrates utility of theseformulasfor estimating the E X A M P TlE ...13) In Chap.2 f ( r .12) f'(r): {!tP f'(. The followderivatives.r7) term hasimprovedthe accuracy O\h21. by collectingterms: (19. 19.0 .4 we estimated derivativeof f ( r ) : . h+Otlr) .13)] f"!:t) ^z *.emolov hut formulasfrom Fis.

) h2 o(h) o(h2) --f (xin:)+ 4.878125 €.5. 8 5 9 3 7 5 € r: 5 .)= h1 o(h) 0T'?) f "" (r. l ..4f (t. 6 3 6 3 2 8 18 ( 1 . lotier hryo ore for The version incorporcles more terms the icyior series of exponsion ond rs.) + ) . Thebackward difference accuracy \h2I iscornputecl(Fig.3 Forwordf inite-difference formulos: versions presented eoch derivotive./(.2 4 f ( .4l'6i+) + f (ri) f"" (x. 6 3 6 3 2 8 1 f ( . The data neededfor this exampleare xi-2:0 xi. + + ).2 + 4(0.llVt 2(025) Thecentered difference accuracy of Otha. r .5 ri+r :0'75 li+l : I f(ti-):1. * .rr*J .) f @i*) 3f h3 0(h) o(h2) -31 (1.r :0.) = (x.*') . ) ( + Ita FIGURE I9. 1 0 3 s 1 s 6 1 .1"' ThirdDerivotive (ti*z) * 3/(. r n 1 * 1 4 . f ( x i + r ): 0 . 2 + 8 ( 0 . l \xil: Enor 0(h\ oftll -f (r.2 f(xi-t): 1.*.*r) .2f (x.5"f (..*) . 9 2 5-) 4 ( 1 1 0 3 1 5 6+ 1 .9t5) r) : .n. 2 .5) as .925 .4) of O as / t t .*.r. r i + ): 0 .)+ 2f (x.) -l ln' FourthDerivoiive f (x.ri : 0.consequenlly. -:s ) l ( 0 . moreoccurole.is computed (Fig. 19.1035156 J'Gi) :0.+r)* 6/('r. ) 3 / ( .f (xi+) .19. 2 Theforwarddifference accuracy (h2)is computed (Fig.5) -0.+1) 14f (x.+r). f ( x .3f (x) + 2h Second Derivotive -f (.t.3) of O as : / (0.) * f (.1 24f $i+2)+ iB.0 .r.636328. 8 2 V t 2(0j.2 .) = .:3. Solution. * ) + l l / ( x . x i * ) * 2 6 7 1 . 2 .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 . 5 ) : -0.f(r.19.3(0. f x .25 .0 .) h2 .*21 4f (xi+) .

) .the errors for the forward and backwarddifferencesare considerably the difthan the resultsffom Exanrple4.5.r8) /r1 Because usingtwo stepsizes: and /22. This is because tbrmula basedon the Taylor seriesis equivalentto passinga fourth-orderpolynomialthroughthe datapoints._r) lr' 3"f(.-r).rzo Bockwordfinlte-diiference more occurote exponsion ond is.t.2) .v : 0.t . we have seenthat there arc two ways to improve derivativeestimates (l) decrease step size or (2) use a higher-orderforrnula the employing llnite diff'erences: extrapolation. of wlrere/(ft1) andI(h) areintegralestimates when expressed a computeralgorithm.I 4 f (x.2f(x.3 RICHARDSON EXTRAPOTATION when To this point.-) + 26 f @-.) 2h3 Fourth Derivotive * f lt. corrsequently.t .r..) h7 3.4f (r. t._r).-:) hl Ihird Derivoiive o(h) o(._r) 14f (.). I : I ( r r : )* .r.4 formulos.r t h l \ l (te.- l r t t t l .t+ 6f (r.l (r.r.2f (.i that Richardson ( I 8..4.4)] improved integralestimateby the formula [Eq. this formula is usually written as its convenience for the casewherehz : ht 12. cre for Thelotier h.. \ n Lt1 o(h) o(hr) f ' ( . .f(il .. derivativeestimates computea third.19.A third approach.l l \.18f(x.r.r.) + 3f (xi. more accurate.. versions r..4f rr. as in 4 . versionincorpoTotes more ferms ihe Toylorserres of As expected.-J + J(. usestwo basedon Richardson that eniploys more poiuts.4) .-s) 1""(t) = o(h) o(h2) FIGURE I9.ttt/tt)). to approximation.f (r) .3 RICHARDSON EXTRAPOLATION Firsl Derlvotive +rr\ J \ i' - Error frY J '4i t. ) 3J(.)= -t 5.5/[r'-r) * 4f (x.2. 18. I9. provideda means obtainan to extrapolation Recallfrom Sec.4. centered more accurate the ferenceyields the exact derivative at ._)+ 24f (.f"(r') : 2f (x) .\i).24f (ti ) + I I /(. However.resenled eoch derivotive.surprisingly.'r) f"(xi)= h2 o(h) o(h?) .{._.) * f (x._.-f (.\i 1) : Second Derivolive .f (xir.f (xi_t) /(r:. ._) 3f (.h'1) f"'( r'.

f (t+) + 2f (xi r) -.rr+r)+ 56f (x.47a .1 3 1 1 " r .5): -----:.).5 ( I9.20) For centered difference with O\h2). _ ) . can as estimates be computed with centereddifferences Solution.r.n) + 6f (r. The first-derivative 0 .)= f"'(x) = oft'1) 0(hn) 2hl .. 5 a n dh z : 0 . z) l2tt2 Third Derivotive -f (r.934375 : 0. 2 D(0.0 .25) 0 .*2) + B/(iri+r) .o.67c I and : D(0.f (xi z) f"" (x) = h4 oftI) 0(ho) : f"" (x.) * 3O/(ri) + 16/(:r. der i v a t i v e t .19)can be written fbr derivativesas 4l D::D(h:t_:D\ht) 1 -)J TIGURE I9. * . .) + l (.B .f ln Error (.-r) + 12.+t) 2f $.. 2./(.2 1 Richordson xtropolotion E estimate first the Problem Stqtement. T h e nu s eE q .) + f \x. 6 3 6 3 2 8 1r .- o(h'1\ o(h') -f (x.) 12f (x. +) l 3 / ( r r .-. the applicationof this formula approximutions will yield a new derivative estimate O(h+).9125.( 1 9 . f( x i _ z ) * f ( x .4f {x.5 m p l o y i n g t e ps i z e s f h 1 . f ( r i + r ) + B / ( x i + 2 ).1 .5 €r : -2..f(x_r) 1-f (xi_z) + + 6h1 rvvg ['^rle-d'{Ie'ence fo'rnulos: ve s onso'e oresetred eochderivotive.*. 8ft3 Fourth Derivotive f (r. 1 i a a ^ n a o ^ ' a n l l r r m n r o occurote.-. ).1 -_f (t.). Using the samefunctionas in Example19.: *1.r ) .r.t.+2i\ 39/(._.39/(r. r . version more incorporotes terms theToylor of series exponsion. 2 0 ) a e s o to computean improvedestimatewith Richardson Recallthatthetrue value extrapolation.*) .0 tt : *9. 2 5 .) . of EXAMPLE 9.Bf (. lofier Cenie'ed for The ^.456 DIFFERENTIATION NUMERICAL FirslDerivoiive f ( x . ) h2 0(h') o(hn\ f" (x): -f (xi*) + l6/(. r : 0 .xi-) -------.4f (xi_)-l .f (x. is-0. 1 0 3 5 t 6 -0.t.*r) * 2. (19.) f "'(x.r. Eq.-1) I ltr Second Derivotive f t. In a similarfashion.1.

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. for equispaced at t&q.. a only in cases In contrast.4 DERIVATIVES UNEQUALLY OF SPACED DATA The improvedestirnate be determined applyingEq.9125 457 The previousexampleyielded an exactresultbecause function being analyzed the was a fourth-orderpolynomial. points thernselves not have by the do is to be equally spaced.. approachcan the be appliediteratively using a Rombergalgorithmuntil the resultfalls below an acceptable en'or criterion.934315) -0. 19.21)evaluated t : xt redifference In fact. ( 19.The exactoutcomewas due to the fact that Richardson extrappolynomialthroughthe dataand then olation is actuallyequivalentto litting a higher-order evaluatingthe derivativesby centered divided differences. data from experiments fic-ld or studies-are often collected at unequal intervals. r i . ' z )i.. e v e n l y s p a c e d . Suchcontrolofdata spacbe evenly spaced and generated successively ing is usuallyavailable wherewe canusea functionto generate tableof values. it can provide estimates anywhere within the rangeprescribed the threepoints. I r ) . r t i ) . . 19. r o ) ( . the derivativeestimate of the sameaccuracyas the centered points.Second.4 DATA DERIVATIVES UNEGIUATTY SPACED OF The approaches discussed this point areprimarily designed determine derivativeof to to the approximations Sec.r s ) ( x 1 - 12) * lGz) ( . 19.25)1. . (19.of course.t o .. First.3through certainly more complicatedthan the first-derivativeapproximation Fig.Remernber that this polynomial does not require that the points be equispaced. a n d ( .and our derivativeestirnate quently.r-. For the finite-difference l o Sec.f (rr). 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 .r1 -. the derivativeof the foLrrth-order For would be irnprovedbut not exact.I9. (15.9.as was the casefor the applicationof Richardson extrapolation.) 2x-rg-x1 -t . r 2 ( x r. ducesto Eo.4. Third. r :. 2 x . . r u . 19.21)J a setofadjacentpointsthat bracket locationvalueat which you want to evaluatethe derivative.Such intormation cannot be analyzed with the techniques discussed this point.Consethis would not occur. Although this equationis the frorn Fig.the datahad to be of a given function. most otherfunctions.l r- rr) (le.t2 xr )(xe . t h e d a t a a l s o h a d t o for halvedintervals. 4I D:4(-0. points For example. Thus.2.ernpiricallyderived information-that is.x.21) where-r is the value at which you want to estimate derivative. the presentcasematched polynomialprecisely.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 . to One way to l-randle nonequispaced data is to fit a Lagrangeinterpolatingpolynomial the to [recallEq.25\.5. r . polynomial can thenbe diff'erentiated analyticallyto yield a forThe the mula that canbe usedto estimate derivative.you can fit a second-order Lagrangepolynomialto threeadjacent ( . r g .f3 . it has some importantadvantages.Eq.

: 6 6 .0 . drl I a_ l.037s 0)(0.6 Tempercfure versus depth info the soii I9. Use numericalditTerentiation evaluate gradientat the soil-air interface to the andenploy this estimateto determinethe heatflux into the ground.0r . (19.0125. I : temperature (K).458 NUMERICAL DIFFERENTIATION EXAMPLE I9.0.-l-13 _J FIGURE I9.0.5 (0 +10 2(0) 0. &rd 1 : distance measured down fron the surface the into soil (m).5W k (m .l): q(::0t:-ft. : coetllcientof thennal conductivityfbr soil [: 0.333 : K/m + whichcanbe used compute to w/ K\ w q ( z: 0 ) : .0375 0.6.333 .)(0 ffi z(0)-0-0.037-5) t2 0.A shortcomingof numericalcliff'erentiation .3 DifferentiotingUnequolly Spoced Doto Problem Stotement.012s) : __1140 1440 133.21)canbeusedtocalculatethederivativeattheair-soilinterfaceas Solution. 6 0-1 K(-133.anotherproblernrelatecl diff'erentiating to ernpirical is data that it usuallv includesmeasurement error. 5 . 19. As in Fig.0t25 (0.5 DERIVATIVES AND INTEGRATS FORDATAWITH ERRORS Aside fronr unequalspacing. K)1.0375 2(0)-0-0.133. Equation /'(0) : 13.=u whereq (r) : heatflux 1W/m2). Note that a positive valuefor f'lux rneans frorntheair tothe that heatis transf-erred soil.a temperature graclient be measured can downinto the soil.037-5 2s + . The heatflux at the soil-airintert'ace be computedwith Fourier'slaw (Table can l9.

(d) the ond {o).In essence. the truefunctional if relationship between dependent indethe and pendentvariableis known. datais to useleast-squares to ditferentiable functionto the data.In contrast.7cusesthe samedata.the resulting in et-fbct Fig.In the absence any other information.random positiveanclnegirtive errorstetrdto add.f frorn to (c)by toking oreounder tends ottenuofe operolion integrotion the to Imoving or srnooTn errors. Fig. dotc modified (cJ resrlting differenfiotioncurve mcnifesting of irrcrecsed ln voricbility controst. points lre as summedto fbrm an integral. this relationshipshouldlbrnr the basisfor the least-squares fit. is The error arnplificationoccursbecause differentiationis sr-rbtractive. 19. theresuhing of slightly.randompositive and negativeerrorscancelout. However.6 PARTIAT DERIVATIVES Partialderivatives alouga singledimension computed the samefashionas ordinary in are derivatives. reverse the (dJ (d)] o.Incontrast.but with alternating points raised and lowered slightly.Obviously. As might be expected. ootc with no (b) numericol differenfiofioncurve (cJ error. fact thatintegration a sumrring the ib process makesit very forgiving with regardto uncertain data. oolo Fig. 19. For example.7b). 19. This minor modification is barely apparentfiom Fig.7c.7 I (o) lllustrolion how smcrLl errors omplified numericol of doto cre by difiereniioiion.I 9.6 PARTIAL DERIVATIVES 459 I 4 dr 4I dl FIGURE 9. primary approach deterrnining the fbr derivatives imprecise tbr regression fit a smooth. r9.7tl significant.7ttshows smooth. 19. Hence.a lower-order polynomialregression of might be a good first choice.suppose that we want to determine partialderivatives a to for . 19.error-fieedata that wlren numericallydilferentiatedyields a smoothresult(Fig.

tr vectorof lengthn. r ' ) -1 (r i.t') "f /(r*Ar.r yieldsthefinal result Collecting terms (1 9.r Af dl' EX (19.thesecan then he employedto determinefinite-dift-erence t i o n so f f i r s td e r i v a t i v e s .r.The result is called a ntixedpartiaLderivcttive.) \' I Ar."/("r.22\ (19..23) - l(r-A". example.r./ ( x ..t .r'* A1'). : 19. r' 2Lx (1 9 .A .r') /(./(x. v * A y ) .y. we rnight want to take the partialderivativeof/(-r.r. we can usetlnite differcncesto evaluateeachof the partialsin r': .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 .y) .). t0 For higher-orcler we a derivatives.rAy (19.f (i-r d 1' 2L.21) we in approximation. r '.. y ) l ( r .f(r + A.A ..r of theparTo developa finite-ditference tial derivativesin r': ja tf ( . mi-ehtwant to differentiarte function with respect For two or rnore ditl'erentvariables. described the of length zr . [ d-rrd I' ( l 9. I _ 2Ar lAr' a2. .r (..r') - af .l.l' * A.A . r ' .I containingthe differences approxirnafbllowing example.Af') .r)- 2Ar discussed this point can be appliedto evaluate to All the other fbrmulas and approaches partial derivativesin a similar fashion.r) with respectto both independent variables #:*(#) ( t -.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. the di f f function retunlsa vector When it is passed one-dimensional a in As betweenadjacent elements.. 2 5 ) Then.A r ' ) 4A. can first form a diff'erence .7.A x .u) 2A.26) atJ 0x01 * /(r * Ax.1 MATTAB Function:ai.v* A. I * At') l(:r f Ax.r ..t' ..27) WITH 19.A . .A.A y ) l(x * Ax.A. r . .

1. We can first express ) as an anonymous .we must first elements./('T > > f = G ( x ) a .in order to developa plot of our results.1000 0.1 :0. . 1) +x(2:n) J . /drf f (x) Columns 10. Compareyour resultswith the exact solution: f '(x) : 25 .1000 0. result represents differences approximations the derivative.400x2+ 202512. >> Y-f (r) .3900 Note that becausewe are using equally spacedvalues.2 -t 25x 20012l61513 from x : 0 to 0.we could have simply performedthe abovecomputationconciselyas >> d=diff(f(r))i0. valuesof the independent and dependent a We can then generate seriesof equally spaced variables: >> x=0:0.2 0 0 * x . 2 + 2 5 * x .1900 B -21 .we merely perform a vector of computedivided-difference by by division of the y differences the x differences entering >> d=dif f (y) .I9.1000 0.4900 8.0100 5 3. >> diff (x) Columns l througth 5 0.010A 1. " 2 + 6 15 * x .8. To the the As expected.1000 0.6900 6 through -11. 4 E Usingit i r i for Differentiotion to ProblemStqtemenf. I to The vector d now containsderivative estimatescorresponding the midpoint beTherefbre.8.2 WITH DIFFERENTIATION MATLAB NUMERICAL 461 E X A M P L1 9 .31"00 8.900x4 + 400r-5 l (x) : 0..after generatingthe x values. Explorehow the MAILAB dif f functioncan be employed differentiate the function .1000 0. tween adjacent generate vectorholding the x valuesfor the midpoint of eachinterval: a >> n=Length >> xm=(x(1:n (r) . ^ 4 + 4 0 0 * x .For examPle.1000 betweeneachpair of elementsof x.8900 Columns 1 through -0. /2.1000 0.14 * function: Solution. ^ 3 9 0 0 * x . elementsof betweenadjacent the The dif f function can be usedto determine differences each vector.1000 Columns 6 through B 0.3600"t3 2000. ^ 5 .

a . For example. A plot of the nurnerical and analytical estimates be generated can with >> plot (xm..d. error ( 'ur. E .iequal spacing' ) . C l : .8.f. E) As a final step. ^ 24 * 9 0 0 * r .:-f.For example. 19.8 (line) (circles) Comporisonof the exoct derivotive wiih nunrericol with esiimofes compufed MATIAB'sdif f function. > . r .r .' >X a = O :.):a. erro]-(.:.the dif f function comesin handyasa progranming tool fbr testingcertaincharacteristics vectors.. ' a = 2 5 4 0 0 * x a + 3 * 5 7 5 * x .r.'o'.the results lavorablyfor this case.. ^ 3 + 5 * 4 O O * x a^ 4 . compare Note that asidefrom evaluatingderivatives. .n.the following code rejectsa vector that is not in ascending (ntonotoniorder ""'t. following of the statement displaysan error nlessage and terminates M-file if it determines a vector an that :i has unequal spacing: if any (drf f (drf f (x) )-=91 .ya) in As displayed Fig. r .we can computevaluesfor the analyticalderivativeat a finer levelof resolution to includeon the plot for comparison.t:t:::::*.i .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. end Another common useis to detectwhethera vectoris in ascending descending or order.

^ 4 + 40 t J * x ..44C\0 10.. >> !a=25 .@( x ) A .l.the values.0.3100 8.'.rating derivativesat the valuesthemselves rather tharr in the intervalsbetweenvalues. ^ 3 9 0 0 * x ..8100 5. We can then use the graclient function to determinethe derivativesas >> dv_sradient1y. However.1500 5.riii+rL for Differentiotion the that Problem Stotement.^2 0 r.with forward for differences all the intermediate resultsare akin to using centered and backwarddiflbrencesat the ends.89t)0 Columns 6 through 9 -4.'l ('' \/ectorof lengthn. Note that the spacingbetweenpoints is assumedto be one.5900 16. > > x = 0 : 0 .1) ^. In the samefashionas Exanrple19.2 0 O * x .4. we can generate values for the analytical derivative and display both tlre numerical and analytical estimates on a plot: >> xa=O:.fi r (19.I 9 . for the intermediate valuesis returned tered dif'fbrence basedon the adiacent .we can generate seriesof equally variables: and valuesof the independent dependent spaced > > f . If the vector represents equally spaceddata. h) where h : the spacingbetweenpoints. .0400 1.Hence. Use the qiradient tunctionto diflbrentiate samef'unction we analvzedin Example 19. However. f. the flrst value returnedis the difvalues. 1 : O .r.B .8400 -21. ^ 2 + 6 15 * x .^ 5 .5 EXAMPLE Usingu. ''3+5*400*xa. .dy. Columns 1 through 5 5. 2 + 2 5 * x .2 MATTAB Function: sradienr The gradienr. a Solution.the following versiondivides all the resultsby the interval and hence returnsthe actualvaluesof the derivatives. Just as with the dirf function. and fx is a vectorof lengthn containing where f : a one-dimensional differencesbasedon f.4with the di tf function.l9.01:.8.4.7. ^4.1 . 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.28) dtlIi : 2 The last value is then computedas the differencebetweenthe final two values.'o'.ya) >' plot(x.it doesso in a mannerthat is more compatible with evalr.functionalso returns differences. 4*900*xa.1 0*xa+3*675*xa.A simple representation its syntaxis of 'x 9.E = gradient ( f.5900 As in Example 19. .tr .a cenferencebetweenthe first and secondvalue. Y I rl.

h) where fx corresponds the differencesin the r (column) direction. Beyond one-dinrensional the gradient functionis particularly maFor example.If .nis to ornitted.and 4.9 (circles) (line) with wiih numericol eslimotes computed Compcrisonof the exccf derivotive MATLAB qradient function.we will illustratehow gradient can be usedto visualizevectorfields.In thenext section. the function can be invoked as I fx.h : the spacingbetweenpoints. well suited vectors.1).corresponds to to the ditferencesin the y (row) direction.the rcsults arc not as accurateas thoseobtained intervals dif f function in Example 19. .464 NUMERICAL DIFFERENTIATION 25 20 10 FIGURE I9.for a two-dimensional for determiningthe partialderivatives matrices. and .2)as wide as for thoseusedfor dl rf (0.4. f. s with the As displayedin Fig. 19.This is due to the f'actthat gradient ernploys that aretwo times(0. fyl = gradient (f. of trix.the spacingbetweenpoints in both dimensionsis assumed be one.9.

The following example demonsffates use of quiver the to visualizea field.y -7xZ -2.y. Employ the srradient function to determineto partial derivativesfor the following two-dimensional function: /fr rr\ : 1.8 CASE STUDY 465 FIELDS VISUALIZING Beyond the determinationof derivativesin one dimension.the sradienr Bockground. 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. if you were mountainclimbing. you would probably be much more interested in determining the direction of the maximum slope.1. To understand how this is done. it would indicate how a ball would travel as it rolled downhill from any point. and "y : distance measuredalong the north-south axis. Conversely. Now supposethat we generatea grid of points in the x-y plane and used the foregoing equations to draw the gradient vector at each point. which representsthe steepest slope. Such graphical representationsare so useful that MATLAB has a special function. tZ . function is also quite useful for determining partial derivatives in two or more dimensions. In particular. The result would be a field of arrows indicating the steepest route to the peak from any point.y) where z : elevation. we can returnto our discussion partial derivatives of at the end of Section 19.1. (#)' and a direction ^ _r / 0: tan-'A f / A y \ \ffi ) where I : the angle measuredcounterclockwisefrom the r axis. it can be usedin conjunction with other MATLAB functions to produce visualizationsof vector fields.However. called quiver.u.Recall that we usedmountain elevationas an exampleof a two-dimensional function. has a magnitude (#)'.xt.I9. the partial derivatives provide the slopes in the directions of the axes.A simplerepresentation its syntaxis of quiver(x. We can representsuch a function mathematically as z: fk. x : distance measuredalong the east-westaxis. to createsuchplots.If we think of the two partial derivatives as component vectors.if we plotted the negative of the gradient. For this example.v) where x and y are matrjces containing the position coordinates and u and v are matrices containing the partial derivatives.

x .as r on contourplot: >> quirzer(x.the resultant of the partial derivatives can be superimposed vectors the -' -"-. Thenusequiver y to superimposevector 1 a fieldonacontour plot of thefunction.' redr -1.2 l9.t riva and initi l9.1 mati mati tt". * x .y-).ho1d oft : FIGURE I9.2 * x .clabel (cs).ho1d on 19.4 -0.6 -1.y.466 NUMERICAT DIFFERENTIATION continued from : -2 to 2 and : l to 3. seco diffe In ei inter 2L..4 -1. ^ 2 . The gradient function can be emproyedto determinethe paftiar derivatives: We can then developa contourplot of the results: >> cs-contour(x. y ) y . Wecanfirst express/(.6 19. Solution.fy). A seriesof equally spacedvalues of the independent and dependentvariablescanbe generated as >> z_f (x. asananonymous y) function > > f = G( x .2 .i .2 -0.6 -0.ruj:lof J o twodimensionol rhe function resulionrporriol with ofthe 2.3 finit. t9. ^ 2 .l of -r ro= the diff 19.z )._fx.6 2.t.y.r l9.2 first E'np As a final step.IO ffil$?":rui5r*n'". * y _ y .t equi first eacl cate 19.

data points. ilnd.r at t : n llusing stepsizes h 1: n f I h.4.t 3 .3 through 19. The In each case.6 t6 x 2 f ( x ) 0 9 0 3 6 4 . to 19.. 5 . 19. Carry enou-{hterms along ilt each expansionto evaluatethe first term that will be truncated determinethe orilcr oi the approximation. and dydx and dy2d. (19. 19.or (b) the valueslor the independent Test your programwith the data able are not equally spaced. l ' = c o s .5 The resultis shownin Fig.^+:-.dh {'. r'r+t .l 2 .. 6.l (r) : 5z-2'.4Use Richardsonextrapolationto estimatethc first deof of rivative r' : cos.7Provc that fbr cquispaced reduces Eq.r will bc used in each casc of i .1 9 .i and i * l.r : -r| . andcentralclif'furencc mations O(h)\ and O(h1l for the flrst derivativeof of .11The fbllowing data was collectcdfor the distance traveledversustime fbr a rocket: It S J. in order that they point "downhill.e' ar x : 2 fot' ft : 0. 19. J r : 1 . f i o m P r o b . 19. Cornpareyour rcsulls with the true derivati ves..-f ^ ft.6 .8 Develop an M-fllc to apply a Romberg algorithm to -. to 19.. the approrinrrtions estimate to the difl'erence 19.'Ib this.j Use a Taylor series expansion to derivc a ccntercd approximationto the third clerivalive that is finite-difference do second-order accurate.-ti.'1 wherc x and y are input vectorsof length n containingthe variables. 3 7 3 4 0 . cxpansion the A..PROBLEMS 467 Note that we have displayed the negative of the resultants. 3 6l 25 35 o o B 4 2 2 0 0 15 9 6 rvhcre . The function's and then drops away in all directions. a t t dx 2 : 2 .2 are output vectorsof lcngth il estimatesat containing the first.6Employ Eq.19 Devclop an M-file function that computcsfirst and second estirnates oltler O1lir1brsedon the fbrof clerivativc mulas in Figs." peakoccursat x : *1 and y : 1. Ernploy centered initialestimates. Eq. Havc your generatea plot of c1y'dx M-file returnan error messlgeif (a) the input vectorsare not varithe samelength..2[) to determine o o f r : 2 . . and 2A.r.+2.| .Thc function'sflrst line shouldbe sct up as f unctiotr ( lrll'dx. fbr Employ both O(h?) andO(ft4) formulas your cstimates..21) 19. indicatedby the lengthening As arrows. -.10.8 a t r : 0 b a s e d n v a l u e sa 1 t x o= . km 25 3'2 50 5B 75 7B t00 92 125 r00 to Usc numericaldiff'erentiation estimatethe rocket's velocilr .Tcstit with thc fbllowin-e 15 0. (19.5RepcatProb.but fbr the first derivativeof ln. Il .r.you will have(o use four expansions the pointsli :.1Compute forward and backward difference approxiof approrirnations O(h) ard O(h]).0 . ri I ..:----!iL -l-.The function shoulcl cach value of thc indcpendcnt arrd dy2dx2 versusx. difl'erences O(/rr) for the of and : r/6. fbr diff'erent will be aroundthe point -r.r interval will be usedin eachcaseof i . r .ra t .rn 19. the first dcrivative 19. OrOO*2I = flif f rzq .r : r / 4 u s i n ga v a l u co f h : r 1 1 2 . 19.2 antli * 2..2Usecentered firstand secondderivativesof -r . PROBTEMS 19.9 Develop an M-filc to obtain flrst-tlerivativeestimates data: for unequally spaced data. r = 5 u s i n gh t : 2 a r u lh z ..the gradient drops off more steeply to the northeastand the southwest.5.and second-derivative variable. The four' must then be combined in a way to climinate the equations first and secondderil'atives. 19. rcspecvaluesof the indepcndent dependeut and tively.r. 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. E s t i m a l e truepercentrelative error rr fbr eachapproximation.r a . 19.r.-:. I .r at .25) at .

rs Include exact r.and data is taken orclel accurate forwiucl.s O 2 Position.r2 ): t9.s x. for the first evaluation.and (c) backward finitemethods. .5 and 2. s 2AA 0. commancl develop MATLAB M-file tunction to computeflnite-difftrenceapproximations to the first and sccondderivativeat each-r valuein thetable below. y 1 4 2 ) 3 .r : 2 t 0. 3 5 2 A 74 2 4 1 9 7 4 . Then. is 19.2atq7 (1 352070 3e894 /(x) ::::: J.r 0 : ) 2 . and centered finiteeverysccondin polarcoordinates and r I ditfbrence approxirrations thc first clerivative a function of of to the actr. 1 2 9 5 2 0 5 3 9 9 Use MATLAB to estimatethe inflectionpointsof thisdata.15 Use regrcssionto estimatc the accelerationat each is time thatthe centered difl'erence computed theloop. the orderoolvnomials. s Volume.20 You have to measure flo$.ral value of the derivativc.5 r 05 l 2 0 f ( x \ 0 . of polar coordinates r : 1.I \ r 0 + 2 i 0 ) A e (c) Repeatpart (b) lbr the second-order fbrwardandback(Notethatthese bedone the ward difTerences.25 45 678 B 5 9 3 t 0 surallpipe.r valuesfor thecenorder correct) to find the vector cxpressions vclocity il given rn tered differencc approximationwill be . tn 5l 20 2A2 244 246 2 0 8 2 t O A72 o 7 a 0 6 8 4 57 4. Usc finitc-ditfbrence approximations aresecondthat ordercorrcct.the . to a 19. can at same 19.r = 0. neer! temp 0 0 B 164 x rrh T.2.17 The fbllowing data was generated distribution: Use i r (N/ T t.rate of waterthrougha 1 2 3. 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.t.01. cm3 t.22 M lime. l( (a) Use calculusto dctclmine the corrcctvalueof thedenvativeatr:2. (b) Develop an M-f ile function to evaluatethe centered fi nite-difl'erence s. ftl where x is the distancefrom the end of the carricr. mi v.24 'T: t. 6 7 5 8 .5or and acceleration The velocitv and acceleratiorr rl. Plot the results: rcsullon the plot lol eomparison.third-. i : i a .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.(rodl A75 t'.Estimate (a) velocity @.rO I 2 J 4 5 6 7 B 9 10 Use sccond-order correct (a) centered flnite-diftcrence.5.-r-t-1z "f ( r ) J2tr Use MAILAB . Estimatethe flow rateat t = 7 s.O (. .tif f iy. s 3 9 9 O ) 2 9 5 2 a.m 0 153 o:.66 5374 5560 5800 6 0 3 0 6 2 4 a lG):e-2'-x Use t estirr conjr pollu (D= sedin the lr t9. and fburth(d) Plot the resultsof (b) and (c) versr. In order to do it. + t 0 4 0 a n d a : G . clifTercnce second19. 9 1 0 9 t 0 (b) fbrward finite-difl'erence.^ Time.) in time fbr the following data with second-.19 The objcctive of this problem is to compare lg. 19.x. use the centercdfinite-diflerence(secondlor Thus.12 A jet fighter'spositionon an aircraftcarier's runway was timed during landin-u: ftom the normal 19. At 206 seconds. backward. decreasc increments 0.l1 A plane is being trackcdby radar. lr Calc order 19.5.l85 lAA 2aB t 75 2 37 '249 261 325 271 383 273 -l -s -05 x 2 I 0 { l 0 .13 Use the fbllowing datato find the velocitl'and accelerationat / : l0 seconds: r.16 The nolrlal distribution definedas I .18 Usc the .h U. approximation startingwith . cn c.This will be done for wher u n r t€ (cmz Ane centr derly i::: x. the 19. 3 4 B 6 B 6 6 8 . "l to determine the inflection points of this 19.1 in ale down to a nrinimumvalueof A-r = 0.21 The velocity rr (m/s) of air flowing pasta flat surface If thr .rldt') and (b) acccleration(duldrl using nunrericaldifferentiation.

: tlepth (nr) as nreasured rvhere I.] r:Q)A. l0-6 g/cm3 the total depth (rr). andl:: a propoflionality conrnedium the surrounding /aw stant(per minute). s/A).m/s 0 0. T.r : distance 1cm).v : 0).22) to colrlpute massllux of corlunction the pollutant of the sediments out and into the overlying walers . this equation(calledNcruton's to that the rateof cooling is proportional of cooling) specifies .28) beexcan of of pressed as JT dT klT Tul I.27 gxr.002 0006 a2B7 0899 00r2 I915 0 0r B 3 048 A A?! 4 299 (ml) and . 8 ).23 fne fbllowing data was collectedwhen a large oil tanker was loading: /.1. Cm c.[. min I'. lf.'C 0 t9 008 l7 O. : temperature ('C).Thus.rmerical the difl'erentiation techniclue availableto estimate derivalive at "rr: 0. (:) aa. the followuse law ing voltagedatato estimate inductance a currentof 2 A the if over'400 milliseconrls./ : volur.2tt 15srare cooling a body(Fig.25 Ths horizontal surface areaA. 6 x 1 0 6r n 2o f sediments. Determinethe averageconcentralionbasedon the following data: 16 4 B 12 O z. 1 .PROBLEMS 469 19. : a dil-fusioncoefTicient (g/cm3).22) whereZ: e. is passerJ thloughthe inductor t.1 .h 0 u.r : 0 at the sediment-water a interfaceancl downward): increases r.. cornputeft. engineer measuresthe fbllowing conAn environmental centration a pollutant in the pore watersof sediments of underlying lake (. 5 2 x 1 0 . 1: x l0-5 N ' s/rn2.26 p. The fbllowing to are temperatures measuredfiorn the surface(. volts the Calculate flow rate Q (that is. in L-lL- 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 .ne fiom the surfacedown to the bottom.sion 19'22 /rnl stalesthat 71vst flux : -Dd' Mass dr ctz. 4. of of whereI: temperature the body ("C). (cmr/s). (PI9. ms V. dV . Ernploy this estimate in the with Eq. 19. I : current(A).r : 0) into a 0 t0 20 40 60 B0 12a lB0 280 400 35 26 15 7 a)829414946 l9. (nr21 a lakeataparof fionr volumeby difTerentiation: ticulardepthcanbecomputed 4 . L: indtrctance henrys: I H : I V.9/m3 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 7.: r.2 c. tll/ ldt) fbr eachtirne to the order /rI .4 on Faraday's 1Prob.: concentration c and .n v. t7l 0 006 a32 3 06 Use bestnr.4 4.rczrsln' larl to determine the shear stress Use at r (N/m:) the surface(.e diffir. (g/m3).t U If theflux at x : 0 is 60 W/mr.1 B5 5.oltage drop (V). of 19'24psudsp'slaw is usedroutinelyby architectural engineers detennineheat flow through walls. 106 borrels (i1 where V.orr4^r's law characterizes r'oltagedrop across the an inductor as di Vt. Newtott's r:i. The averageconcentlationof a substance varieswith depth.s1az 1'd where massflux : the quantity of massthat passes acrossa per D unit area unit tirne (g/cmr/s. Q . Determine the voltage drop as a functionof time from the fbllowing datafor an inductance H.6 c m 2 / s )F o r a l a k ew i t h 3 . lo6m3 I8175 5 105I I 9635 Q3927 00000 lo. 19.: L. and I: time (s). much pollutantu'ould be transported how into the lakeover a year's time? 19. be that can computecl integlatron: by pi711'. P19.(z) (Pt9. .26).

Note that I J = 1W. dT 19.3I The pressure gradientfor larninarflow through cona stantradiustube is given by dn d.s/nrr. otm r=350K r=400K T=450K difl'erentiation dcterminen. and I : absolute lemperature (K). ml 19.mm 2 135 1.32 the following data for the specificheatof benzene V. Irr . K Cr. = flow centerline 0 (mr/s)anclr: radius(m).K 0 900 I 480 35 270 dioxi deter lated and < T'K 2t0 FIGURE PI9. in as If thc plate's dimensionsare 200 cm long and 50 cm wide. K).Usenumerical with an /rth-order P. have occured if thc tubc had a constant radius eoual to 7P .5 523 27 I 5.30 p61 fluid tlorv over a surface.. Estimatethe enthalpyof the f'luid at zl00K and 50 atm (evaluate (b) Comparcyour result with the pressure drop thatwould thc integral frorn 0. The data was takcn lbr a real fluid.and (b) the heattransf'er watts.7 462 700 800 900 ' K) I Z5 036 ) 89 273 20A 9n 19. r. follor urea: t.s.density p = I x I03 kg/mr) with a flow of l0 x 10-6 m3/s andthe fbllowing varying radii along its length: wher. If a metal ball heatedto 80 'C is dropped into water that is held constant f.. (a) Detcrminc the pressuredrop for a l0-cm length tuh = for a viscous liquid (p : 0.K) T.4 T. s/mr.lt/ ') the averagcradius.y : distancenor(m).r': distance norrnal to thc surface: Jr clll T. in I 9. "c 0 80 5 r0 .28 the ditlcrencein thc tempcratures the body and of thc surol rounding medium. reacti (minevalu logar lo Thert log(slope meth..\ I49 t2 099 475 4. I atm to 50 atm).rp ' d (c) Dctelminc the averageReynoldsnuntberfor thetube to Ju \ verit'y that flow is truly laminar (Re : pu D I 1t < 21N whercu: velocity).34 l6 142 2 dcscribcclbelow. 19.kl/lkmol. to where p = pressureiN/rn:).470 NUMERICAL DIFFERENTIATION madetbr air flowing over a flat plate where. kll(kmol 300 4AO 500 600 82 BBB Il2 136 136933 157744 1000 210450 I03 a78 4..3 react reacti 8uQ ft14 d.33 Tllg specificheat at constantpressure tJ/(kg. -r : distancealongthetube\ (m). mi c. K)l q.31 El(m: 0t 5 t0 20 25 30 4A 45 50 224 22 r35 tl 090 O68 o6t a54 250 47 2.028J/(s . andemploy lir"rear regression evaluate to fr. the temperature at of the ball changes. and I : 0.5 I ). : 20'C.the heat l]ux to the surface can be computedwith Fourier's law: . The fbllowing enthalpiesare providedfor carbon .005N .r ! a 19.67 5 06 282. min r.. (a) deterrnine flux at thesurthe face.cm O 2 4 5 710 6 19'29 Tl1senthalpyof a real gas is a function of pressure as l58 r. K C. The tbllowing measurements mal to the surf-acc are whcre ft : enthalpy (kJ/kg). Time.7. H : I ( r ' r (\ 1T / p l ). Plot dT f dt versw T .L was generated polynomial. fr : dynamicriscosity{N. of an ideal gas is relatedto enthalpyby dh ..145 300 t5 24t 20 2t7 25 2A7 Utilize nunrericaldiff'erentiation determinedTldr ateach to value of tirnc.

35).35The sedirnent oxygen demand [SOD in units of g/1mr. The di. Use the dilferential methodand linear regressionto deternrinet and n for the following data fbr the conversionof arnmoniumcyanateto urea: l.445 42. l l 4. 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.fferentialmethod can be used to evaluate parametersk and rt.769 Sediments I I 19.11is measured by placing a sediment core in a cylindrical conniner (Fig. oxygenated water abovethe sedinrents. Note that the atomic weights of carbon g/mol. min c.rlcr Iog{ . Based on the following data and H : O. }=logk -\ dt / L. .179 1000 900 37.K h. the nth-order rate law holds. a plot of the log(-dcldf) versuslog c should yield a straightline with a slopeof n and an intercept of logfr.and an oxygen probetracks how the water's oxygen concentrationdecreases over time. n : reactionorder (dimensionless). dioxide Use these valuesto determine specificheat in J/(kg . This involves applying a the logarithmic transformto the rate law to yield. 0 3 19. containeris covthe A eredto preventgas transfer.59 2 57 r r s 0 3 3 0 . / . K) fbr eachof the tabuthe lated temperatures. d.223 t .9994 471 Lid Water H T.l m. P I 9.01 and I -5. and ft : reaction rate (min I moler "). m g / L 1 o 7 .kt/kmol 754 29 629 800 32.35 P -dl : *kc" dissolved oxygencontentof a naturalwater. r'espectively oxygen I and are 12.34 nth-orderrate law is often usedto model chemical An of reactions solely dependon the concentration a single that reactant: dc FIGURE I9.and t: time (d).?log( if Therefore.After carefullyintroducinga layer of distilled. The SOD can then be computedas SOD: -I1: do at wherec : concentration (mole).11 an important parametel in determining the is . I : time (rnin). o : oxygenconcentration (g/mr).mole 0 A750 where11: the depthof water (m). stirel is usedto rnix the water gently. d 0 0125 425 0375 05 0625 475 o .PROBLEMS (COr) at severaltemperatures.



Ordinory Differentiol



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 free-fallingbungee 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



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..


, r,i:,ir.,.,, t.,..






du -^ -6
, aIm





V Solution


Analytical (calculus)


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,

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 afirst-order equatio,? because highestderivativeis a lirst derivative.Asecondthe order equationwould includea second derivative. For example, equationdescribing the the positionx of an unforcedmass-spring systemwith damping is the second-order equation: m -*t. dt'







where ,fl is mass,c is a damping coefficient,and k is a spring constant.Similarly, an nthorder equationwould include an nth derivative. Higher-orderdifferentialequations be reducedto a systemof first-orderequations. 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





Equations(PT6.3)and (PT6.4)can be substituted into Eq. (PT6.2)to convertit into a firstorder equation: du m7-tcu+kr:0


As a final step,we can express Eqs.(PT6.3)and (PT6.5)as rateequations: dx



ck tn nt




Thus, Eqs. (PT6.6) and (PT6.7) are a pair of first-order equationsthat arreequivalent to the original second-order equation (Eq. PT6.2). Becauseother nth-order 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 fourth-orderpolynomial, . 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.


d.r - _2r3 * l2,rr - 20r * 8.5 dx


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 flat-that is, where it hiis a zero slope.Also, the maximum absolutevaluesof the derivativesale at the endsofthe intervalwherethe slopes ofthe functionaregreatest. we Altlrou-eh, 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



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



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 first-orderODEs, 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 -r-8.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



( =Z

C=1 C=0

PT6.3 FIGURE of to Sixpossibe solutions theiniegrol -2-r'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 nth-orderdifferentialequation,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 initial-valueproblem. problemswhere specification conditionsoccursat of This is in contlastto boundary-value valuesof the independent variable.Chapters and 2l will fbcus on initial-value 20 difTerent problems. problems coveledtn Chap.22. are Boundary-value


Chapter 20 is devotedto one-stepmethodsfor solving initial-value ODEs. As the name suggests, one-step 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, one-step the methods in wh:rt are called, Runge-Kuttatechni4lle.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.



Next, we usevisually orientedarguments developtwo improvedversionsof Euler's to nretlrod-tlre Heun and the midpoint techniques. After this introduction,we formally develop the conceptof Runge-Kutta(or RK) approaches demonstrate and how the foregoing techniques actuallyfirst- and second-order methods.This is followed by a discusare RK sion of the higher-orderRK fbrmulations that are fi'equentlyused fbr engineeringand scientific problem solving. In addition, we cover the applicationof one-stepmethodsto .t-y.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 initial-valueproblems. 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 implementstep-size control.We describea sinrplemethod-the non-self-starting Heun method-to introducethe essential f-eatures 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 built-in functionsfor solving stiff ODEs. ln Cha1t.22, we focus on two approaches obtaining solutionsto boundary'-value for problems:theshootingandJinite-dffirencemethods. Aside from demonstrating how these techniques implemented, illustratehow they handleclerivatiye are we boundorvconditions and nonlinear ODEs.

''; 'j:''



l:io ":i'

Initiol-Volue Problems

The primary objectiveof this chapteris to introduceyou to solving initial-value problemsfbr ODEs (ordinarydifferentialequations). Specificobjectivesand topics coveredare t . Understanding meaningof local and global truncationerrorsand their the relationshipto stepsize for one-step methodsfor solving ODEs. Knowing how to implementthe following Runge-Kutta(RK) methodsfor a singleODE: Euler Heun Midpoint RK Fourth-older Knowing how to iteratethe coffectorof Heun'smethod. fbr Knowing how to irnplement followingRunge-Kutta the methods systems of ODEs: Euler RK Fourth-order

. .

e startedthis book with the problemof simulatingthe velocity of a free-falling 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.


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


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


-(r ilt



I.l -

-U m



(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


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



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


: I (l' -Y)


In Chap. l, we developed nnmericalmethodto solve suchan equationfor the velocity of a jumper. Recall that the methodwas of the generalform the free-falling bungee New value : old value* slope x stepsize terms, or, in mathematical
.Vi+r: )'i*Qh


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 calledute-ste1t 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 one-step 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 one-stepmethodsthat employ alternativeslope estimates predictions. that result in more accurate

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


or This formula is ref-ened as Euler's rnethod(or the Euler-Cauchy point-slopemethod). 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).



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')12n-o5r ) : r r-lrtrut' 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 :

1 is


r o * rt t tr (eust - e 05(l)) 2e-05n\:6.19463 +

Thus.the percent relative erroris t6.19463-51 F, : l-l 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 ) :


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



lerl (%)

0 I 2 3

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

we seethat the local error is proportionalto the square of the step size and the first derivative of the differential equation.The sum of the two is the total enor.0t*l)191 (20. . f'(tj'-Yi) . Insight into the magnitudeand properties the truncationerror can be gainedby deof riving Euler's methoddirectly from the Taylor seriesexpansion.3).6) where/u : ti+t . (20. (20.+ o ( h ..7)to yield Eq.. An alternativeform can be develin opedby substituting (20.the tluncationerror in Euler's methodis attributable the remainto ing terms in the Taylor seriesexpansionthat were not included in Eq. to /. (4.. a part of thetrue We or solution..yr+r: )'i t f (ti. If the solution-that is.It can also be demonstratedthat the global truncationerror is O(/r)-that is. 11. l0) or Eo : O(h2) (. : the approximate local truncationeffor..7) where f lies somewhere the interval from /.[ 1 + P h 2+ . (20. thustruncate. r . It is referredto as the gbbal truncation error.yr). realizethatthe To differential equation being integratedwill be of the general fbrm of Eq. .h ' . : the true local truncationerror. it can be represented a Taylor seriesexpansionabout a starting value (/. (20.. By cornparingEqs. Subtracting Eq. and t and y are the independent and the dependent variables.8)yields E .20." 4n * nl (20. leaveout. (20.For example. where dyldt : -y'.9) are usually negligible.yi)/r.(20. (20...f ).\r. as in by [recallEq.ll).484 INITIAL-VALUE PROBLEMS previous steps.13)]: t ln| ]i+r: ti *yitt +*n'+ :! " +'r'i' 7. do this. and Ra : the remainderterm.3)into Eqs. .For sufficientlysmall lz. the comparison indicatesthat a truncationenor occursbecause approxirnate true solutionusinga fiwe the nite numberof termsfrom the Taylor series. definedas R. (20. According to Eq. ' j------::-/r"+l ( t z* 1 ) ! .6)and (20. the function describingthe behaviorof y-has continuous derivatives. it is proportionalto the stepsize ./. Additionally.).1) where E.ql where E.5) and (20.l')h + where O(h'+l) specifiesthat the local truncationetror is proportional to the stepsize raisedto the (n * l)th power. + i I t20.2 * .5)from Eq.the higher-order terms in Eq.5).and the result is often represented as F ^ - . (20.respectively..it can be seenthat Euler's rnethodconesponds to the Taylor seriesup to and including the term /(r.8).

a h ) (20. calculuscan be usedto determinethe solutionas V : ltr€ -'' (20.Theseobservations et lead to someusefulconclusions: 1. Hence. tf. 1969).e.e that we useEuler's methodto solve the sameproblernnumerically: Now suppose .the stability depends the step size /r. That is." *fution startsat 1.t l 0 .The stability of a particular applicationcan dependon three f-actors: differential equation.)0. the solution of the differential equation)is linear..2 Stobility of Euler'sMethod ln the precedingsection.) ' i + r : 1 . because a straightline the second for derivativewould be zero. Euler's methodis said to be conditionall.2. This latter conclusionmakesintuitive sense Euler's methodusesstraight-line because segments to approximatethe solution.So clearly. 2.y i + l: l i + . d)'.That is.2 EULER'S METHOD (Carnahan al. A nurnericalsolution is said to be unstableif enors grow sider-ed exponentiallyfor a problem for which there is a boundedsolution.and the stepsize.we learnedthat the truncationerror of Euler's methoddepends on This is an accuracy the step size in a predictableway basedon the Taylor series. 20. S u h s t i t u t i n gq . Note that thereare certainODEs where errorsalways grow regardless the method. at If r(0) . issue.v stable. ffrrr. 485 The global error can be reducedby decreasing stepsize. an nth-ordermethodwill yield perfect in polynomial. If its absolutevalue is greaterthan unity. Basedon this analyon sis. i ( l. if /z > 2la.13) quantity 1 . the The method wiil provide error-freepredictionsif the underlying function (i.the local truncation resultsif the underlying solution is an rrth-order errorwill be O (h"+1) and the globalerror O (h').lyil --+ oo as i -+ oo. Euler's method is referredto as a first-order method. It should also be noted that this generalpatternholds for the higher-olderone-step methodsdescribed the following pages. . the solutionwill grow in an unbounded fashion.ah is called an cunplifictttion The parenthetical foctor.12) and asymptoticallyapproaches zero. Insight into the step size required for stability can be exanined by studying a very simpleODE: dt' -ay . of SrrchODEs are called ill-conditioned..20.the numerical the method. I 2 t g i v e s E -Yr+l: ji \'ih or . is The stability of a solutionrrrethod anotherimportantconsideration must be conthat when solving ODEs. Further. .

Recallfrom Section3.Ir.an inaccurate methodcanbe very stable.y0. . {'Jpper lrmit musl be grearer Lhar' .21.3 showsan M-file that usesEuler's methodto computevaluesof thedependentvariabley over a rangeof valuesof theindependent variablet .end tspan(2). Now. tspan.y(i). : method to integrate z uses Euler's an ODE % rnput: 96 dydt = name of the M-file Chat evaluates the ODE z tspan = lti. n = n+1. p l .6'r.they are distinct problems.tf ' lLf>ri). that this function usedEuler's bungee nethod to compute the velocity after a given time of tiee fall.3. The nameof thefunction holding the right-hand side of the differential equationis passedinto the function as the FIGURE 20. = additional parameters used by dydt % output : z t . . Figure 20.4. 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. add an additional value of t ^^ac rIOm L ri tO Ct {'ar r (n)<tf t(n+1) = tf . e u .vararqini:])*(t(i+1)-t(i) end ). end y = y0'ones{n. r ee F f i c i e n .4 z z if least 4 inpuu argumen-s reqL rq. .ower'l. . rh)l 1a"-. in 20.486 INITIAL.2.l o d e r d y d t . h .For example.vector of independent variable z y = vector of solution ior dependent variable if nargin. y for i = 1:n-l ?implement Euler's method = y(i) y(i+1) + dydt(t(i).We will retum to the topic when we discussstilTsystems Chap..However. .y] B eulode: EUIET O-DE SOIVCT z [ r .h.p2.i o n It. % p r e a L l o c a t e y L o i m p r o ' . . r s p o n .6. necessary.elror = tspan(1). .error = if q^ ri if t (ti:h:tf)'. y 0 . This is probablybecause bothrepresentsituationswhere the numericalsolutionbreaksdown and (b) both are affected step by size. y ] .end n = length(t-).all-purposealgorithm.3 AnMJile implemeni method to Euler's = eulode(dydt. p l .VALUE PROBLEMS (a) Inaccuracyand instability are often confused.3 MATLAB M-file Function: eulode We have alreadydevelopeda simple M-file to implementEuler's method for the falling jumper problem in Chap.rarargin) f u n c r . . let's developa rnore general.

because they have very straightwe forward graphicalinterpretations. the ODE from Example20. The initial and llnal valuesof the desiredrangeof the independent variable is passed a vector tspan.3 IMPROVEMENTS METHOD OF EULER'S 487 variabledydt.4422 25. the last value will fall short of the final value of the range.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.4132 55. variable The lunction first generates vector t over the desired a rangeofthe dependent using an incrementof t'r.0000 2.4. respectively.var).Yl ) with the result(compare with Table20. will presentthem prior to their forrnal derivationas Runse-Kuttamethods.the final value is addedto r so that the seliesspansthe completerange. by At this point.5"yt The solutioncan then be generated as >> disP(tt.First.0000 2. In addition. ically calculated between adjacent in based the difference on ways.20.3 TI. . both modifications(as well as Euler's methoditself) actuallybelongto a largerclassof solution techniques calledRunge-Kuttamethods. Notice how a function is usedto generate value for the derivativeat the appropriate a valvariables.4400 3.For example.The length of the r vector is detervariabley is preallocated mined as n. Euler'smethod(Eq.I : fbr > > t i 1 ' c i t = G( t .0000 5.there will be more complex problemswherethe definition of the ODE requiresseverallines of code. a vector of the dependent with n values of the initial condition to improve efficiency. The initial value and the desiredstepsizearepassed y0 and as as h.In M-file is the only option. Also noticehow the time stepis automatues of the independent and dependent values the vectort.0000 1r. suchinstances.1): 0 1. V ' ) 4*exp (0.As will be dernonstrated Section20.v(i))*(t(i+l)-t(i). 20.5)is implemented a simpleloop: y(i+1) end = y(i) + dydt(t(i).In the eventthat the stepsizeis not evenly divisible into the range.000a 4. the differentialequationcan The ODE being solvedcan be setup in several be definedasan anonymous funcfionobject. creatinga separate 20. Two simple modificationsare to in availableto help circumventthis shorlcoming.If this occurs. 3*t) o.However.8493 Although using an anonymousfunction is feasiblefor the presentcase.

1 5 ) i s n o t t h ea'nn a l e r .Thisapto proach.is depictedgraphicallyin Fig.16)] can be combinedto obtainan average slope for the interval.f(ri* r. . However. l6) Thus.14)and (20.l'j!.f (ti. f ( r . _ /(r. T h i s i s c t i sw why we havedistinguished with a superscript Equation(20.the slopeat the besinnins of an interryal )"i : . l Q i .r'.y i) .4. Recall that in Euler's method.) (20.f(rr+r.488 INITIAL-VALUE PROBLEMS 2O. .The two derivatives are then averaged obtain an improved estimateof the slopefor the entire interval. the two slopes[Eqs.4) r (20. )rrl:)'ir------.1 Heun's Method One methodto improve the estimateof the slopeinvolves the determination two derivof ativesfor the interval-one at the beginningand anotherat the end. 1 * .. y i l ' .t . It providesan estimate that allowstlrecalculation a slopeat the end of the interval: of : )'1+r .a l c u l a t e c l i n E q .9+r) 2 This averageslope l s then used to extrapolatelinearly frorn -1'.f (rt*t.3.4 Grophicol depiciiono[ Heun'smelhod (o) Predictor ond (b) correctof = . .7) r which is called a corrector eguation.-vi) is usedto extrapolate linearly to y111 : ))+r:litJ'Gi. equatiott.in Heun'srnethod the r ' . ( 2 0 . * -.Yi)h (20. 20.) /l (20.calledHeun'smethod.to li+r using Euler's method: ' . ' 'l ) . FIGURE 20.15)is calledaprcdictor it 0. 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 .-r'r) . YP*) r : f(ti.r5) For the standard Euler methodwe would stopat this point.(20.

0.rg) ! ' o : 4 e " .19)haslial on both sidesof the eclual can be usedlepeatedlyto provide That is. 20.0 .20. r : 2 .f. 20. in of As with similar iterativemethodsdiscussed previoussections the book.yi)+ / ( t . it can be expressed as concisely (Fig.5y Problem Stotement.4e08' .yi)h (20.5. 1 ' .00001c/o terminirtetlre correctoriterations.as verge on the true answerbut will convergeon an estimate in demonstrated the following example.]'/ 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 . 1 * ' ' ) (20.11.9) r (for.5 of of fhe Grophicol representotion iteroting corrector Heun'smethodto obloin cn improved estimcte.4a): Preclictor Corector (Fig.the predictor usedto compute valueat 1.(orrectorapproach.v/*1 are the resulttiom the prior and the presentiterationof the corrector. to as Solution.3 IMPROVEMENTS 489 FIGURE 20. a termiof nation criterion for conversence the correctoris frrovidedby tc t- t#l"ouo" where1. Use Heun's methocl .o*r: y j " * y/+r : l'i' I f (ti. 20.8) r h l'(rt.4b): y. an old estimate an iterativefashionas indicated.0: v9:2*3(t):5 . .lt should be understood with a finite truncationerror. in is The of an improvedestimate -v.As just derived.:1. is calculated . Note that because (20. 5 ( 2 ): 3 is a Then. The Heun method is a pretlic'tor. First.2""'m) sign. process depicted Fig. * .the slopeat (16.OFEULER'S METHOD 20.2 Heun's EXAMPLE Method with iterationto integrate .r and. conthat the iterative processdoes not necessarily respectively. E m p l o ya s t o p ping criterion of 0.it can be appliedin Eq.

r'f'to predict the slopeat the we end of the interval t .28a/o. (20.2758 .'l whichrepresentstruepercent relative a I errorof 1.' r : l l '" - 6 . 5 ( 5 ): 6 . TABTE20. I 9463 l4 84392 3 3. 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. 2 7 5 8I1 .VH"un le. usethe value. l ' ): 4 e 0 8 ( r ) . r . 6 7 77 l 75 33896 200000 5 00000 t 140216 2 5. 5 ( 6 .4e0E of ol 0 . to improve the estimatefor .2 With lterotion .y : 2 o t t : O .uo Jtiur".19)]to give the prediction at t l.17581 1 | I : 1001 6. 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.101082 ^ L This result can then be substituted into the corrector[Eq. the Heun methodwithout a iterationof the correctorreduces absolutevalue of the error by a f'actorof about2. of qnd with iierqtion TheHeunmethod wos imolemented without both o[ thecorrector.62 0 I '2 3 4 200000 6. (20.tB 994 la 46 t4. Without lterqlion Jt.The true velluein Table 20.490 INITIAL-VALUE PROBLEMS Comporison trueond numericol of volues the integrol tr ..0 . we can alsocomputean approximate erroras l670roR)-sl l r .l .v.: l . l n ol were computed usingthe Euler ond Heunmethods with o stepsize 1. l6 t .70 1 l c .6. 7 0 1 0 8 2 which represents true percentrelative error of -8. 7 0 1 0 8 2 ) I : 6 .Thus.19)to give 3 -2 .4as the comparedwith Euler's method.At this point.116(/t . 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. i : ..2 showsthat it corresponds a percentrelativeerror of 19.l x 6. to Now. 7 0 1 0 8 2 (: ) .l ('hl le. i : 2 + 4 .T h en u m e r i c v o l u e s .1("/"1 B.1.102164 :4.0 .t 14"o t r t D.l("/"1 l9 28 2319 24 24 2454 2 00000 6 7010B I6 31978 37 19925 83 33777 l€. 7 0 r 0 8 2^ ' " " l Now the estimateof r'1 can be refined by substitutingthe new result back into the right-hand sideof Eq.' '> -t. / ( x r .3 percent an approximate 0f and enor il r082 1 6.

r .25) . For casessuchas polynomials.r'i. The connection between two methods the can be formally demonstrated startingwith the ordinary differentialequation by tl 1' -: dt fut (20.t': .t'.23) .isely as . which yields .after l2 iterationsthe approximateerror falls below the stoppingcriterion. is For suchcases. The approxirnate error will keep droppingas the iterativeprocess convelges a staon ble final result.r'by integration: f . (20. l l ) l i s d e f i n e d s a '' [" f ( t i ) + J Q i + r ). In this example..666vc.2 showsresultsfor the remainderof the computafionalong with resultsfor Euler's methodand for the Heun methodwithout iterationof the corrector.03voandan approximate a error of 1. + .5(6.At this point. ( l 7 . .r).275g1 1) I :6.which represents rrue rela ative error of 2.l8)] is not requiredand the corrector appliedonly oncefbr eachireration.i+:l. recall that the trapezoidal rule [Eq.22) . (20. frndt (20. techniqueis expressed the conc.the derivativeis a function of both the dependent variabley and the independent variable/. where the ODE is solely a function of the inclependent variable.\. the result at r : I is 6. (l7. [(t\dt (20.382129 which represents true error of 3.36087. ft'tl J.l'. ft'+t JI.ll)]. In the previousexample. (20.687c.3 IMPROVEMENTS OF EULER'S METHOD The next iterationgives )l 491 )^ - L-r 3 a 4rrt'xrtt 0.i+ l / Ittlrlt (20.24\ Now.-t J r. the predictor step [Eq.. I J dt:l f t.2t) This equation can be solvedlbr. 2 /.ltri)-f [(ri. -l : l'. ' f ( r ) d t : ---2 (20.'.20) Notice the sirnilaritybetweenthe secondterrnon the right-handsideof Eq.20)anc1 the trapezoidal rule [Eq.20.Table 20. Insight into the local error of the Heun methodcan be gainedby recognizingthat it is relatedto the trapezoidal rule.

20. (20.2 The Midpoint Merhod Figure 20.Called the nitlpoittt ntethod.y1. tl. y | L Then. Because Eq. tr (20. l(titl J/rt-.20).:/ r ) ' i * J ' ( t . (20. r ' i + 1 .27 ) wheref is betweenti afidt.this techniqueusesEuler's method to predict a value of .14)l Eq. 1 i 1 1 2). .3.6 Grophico depictionof Heun'smethod.4. i +t p) t:. (. (20.' i 1 1 1 2 ) 1 . the 20. Subsriruring (20. acldition. local and In the global errors are o(h3) and o(lf). Heun's methodis sometimes refemed t0 as the trapezoidal rule.For this reason. .26) which is equivalenttoEq.(o) Predictor ond (b) corrector v = J ( . r J f " ( \t \r 1a ' ' I ' (20. decreasingthe step size decreases error at a lasterrate than fbr Euler's rnethod. Thus.28) FIGURE 20.y af the miclpointof the interval(Fig.24)yields Eq. the local truncation error is given by [r'ecall (17. this predictedvalue is usedto calculatea slopeat the midpoint: i )"t+r1 : .-1 1 .t+ r lz.f (.6 illustratesanothersimple modificationof Euler's method.29) (20.25)into Eq.re methodis secondorder because second the derivative of the ODE is zero when the true solution is a quaclratic. respectively.26) a direct expression the trapezoidal is of rule.20.Therefbre.492 INITIAL VATUE PROBLEMS where/r : !i+1 .6a): .tr---2 l ( t . .

This slope is then usedto extrapolate r'r+t : )'i I f Q i* t .30).. t . ' . it The midpoint methodis superiorto Euler's methodbecause utilizes a slopeestimate predictioninterval.30)]cannotbe applied -r'.34b) (20. *Ii q t t k t l t i q n k z h ) : : f ' ( t . As in our discussionof Heun's method.i* q .a iterativelyto improvethe solutionas was donewith Heun'smethod. : j ' ( t i .Qh (20..(b . just as the Heun method can be called the trapezoidalrule. . local and the of approximation Euler's method. . . fbrm of Eq. r .t p z h .l a . r k t hI q u .-r0) Observethat because I is not on both sides.t ' i ) l .32) Substitutionof this formula into Eq.can be rcpresented open integrationtbrmula.3. respectively.. t .4 that centered samesense.-t. I * q. ) ' i+ t n ) l t (:0.3:lc) (20. the with the forward eror of O1h:1in comparison suchas F.29)has a local truncation O (h).rh) (20. * p . (20. Consequently. .34a) (20. .4): the generirlized )r+r : )'i l.4 RUNGE. Jt rh I J'trl dx =.Usingthe nomenclature the presenl h e e x p r e s s ea s d it can |r l t t t .4 that the simplest Newton-Cotes as which is calied the nridpointmethod.20. k .34d) . global erors of the midpoint method areO(h3) and METHODS 2O.(20. f]tdrZh. D). t.q. which can be interpreted a repl'esentative function can be written in generalform as over the interval.a)J'(r) (20. (20. 20.33) as slope where@is called an incrcment function.the rnidpoint rnethodcan also be linked to Newton-Cotes integration lbrmulas.r1is the midpointof the interval(n.6b): interval. Thus. where the a's are constants and the k's are k 1 : J ' Q . .24)yields Eq.u-:k.Recallfrom our discussion numericaldifTerenof at tlre miclpointof the of finite differences betterapproximations derivaare tiation in Section4. The incremerrt + Q : a r k tI a z k z ' ' ' ' . the midpoint method gets its name from the underlyingintegrationformula on which it is based. .the corrector[Eq. : f ( t i * p 1 h .) ' i * q r r k r l t ) k .t'ftia1P) It i J Q0..i4) (20.which has an en'orof O(h2). (20. 120. . where.4 RUNGE-KUTTA Runge-Kutta (RK) methods achieve the accuracyof a Taylor series approachwithout Many variationsexist but all can be cast in requiring the calculationof higher derivatives.KUTTA METHODS 493 which is assumedto representa valid approximationof the averageslope for the entire linearly from /1 to /111(Fig. z k z h k.Recallfron Table 11. t h .31) fbr case. cenlered approximation In a tives thaneitherlbrward or backwardversions.

Suppose fbr can be solvedsimultaneously ut:l-o: (20.By doing this. (20.40) Because can choosean infinite numberof valuesfor a2.k1 +a2k)h (20.or a constant. (20.second-order methodsusean increment the approach. and so forth.37\ (20. a2. q's areevaluatedby settingEq.36)through the other three.li+l :li where ky : k2: J'Qi' 1t. solutionto the ODE were quadratic.they linear. Note that the first-order methodwith n : I is.in Section20. methodswill be exactif thesolution function with two terms (n : 2). thereare an infinitenumwe if RK ber of second-order methods. therefore.4. Euler's method. three equations second-order are (seeChapraand Canale.the local truncationenor is O1ft31and the globalenoris for O (h2).because droppedduring the derivation.1. (20.36) (20. Then Eqs.p's. in ft1 appearsin the equationfor k2.) -t pth. which appears the equationfor ftr.ln addition. ln Section 20.38) are Becausewe have three equationswith four unknowns. (20'35)equal a the can be derivedto evaluate Taylor series. underdetermined.35c) (20. in fact.4.valuesfor the a's.and Thus.Befor causeeach ft is a functional evaluation.Every versionwould yield exactlythe sameresults the reyield different However. computer numVarious types of Runge-Kuttamethodscan be devisedby employing different RK bers of terms in the increment function as specifiedby n. n 20. commonlv usedand preferredversionsare presented . (20. and qy1 are evaluated settingEq. is. li i qlhh) f Qi l(a.2006. Threeof themost sults when (as is typically the case)the solution is more complicated.theseequations saidto be of one of the unknownsto determine a must assume value We.494 PROBTEMS INITIAL-VALUE That relationships.this recurrencemakes RK methodsefficient calculations.35b\ to by The valuestbr ar.fbr details).35) (20.2. next. pt. numberof termsn usuallyrepresents order RK For example. Thesesecond-order are terms with /rr andhigher to the differentialequationis quadratic.The threeequations four unknown constants a 1I c t 2 : l a z P r: : azQtr l12 I12 (20. Notice that the ft's arerecurrence wherethep's and 4's areconstants.33)is The second-order .38) that we specify a value for a2.4. of the the at leastfor the lower-orderversions.39) I /42 (20.the fourth-orderRK 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 .Oncen is chosen.33)equal to terms in a Taylor seriesexpansion.1 Second-Order Runge-Kuttq Methods versionof Eq.

4.41a) (20.43) (20.r'.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. r . (20.35). v . : and For this version. Eq..r.39) and (20.43b) k t : l ( t ..l+' l ) r 20. .+ (]t. pt when substitutedinto Eq. / (20.ftr \z + ljt . -\l1 . . r ' . r. ) (20. : 2/3). l t (20-42) (20. T h e M i d p o i n t M e t h o d( o z: and Eq.35)becomes : . (20. The most popularRK methodsarefourth order. J'i krhl2) * method. thereforecall it the classicalfourth-orderRK ntetlrcd: . and we are an inlinite numberof versions.+rr'.42b) k2: f fti + h/2. l t a 2 i s a s s u m e d t o b .20. t .t'i+[. / ( r .e h e n a 1: 0 . +f.2 ClqssicolFourth-OrderRunge'KuttoMethod there approaches. r ' i ) -t k2: f ft.(20.35)becomes .* /1. .) 1 1 . Ralston(1962)and Ralstonand Rabinowitz(1978)determined that cltoosingo? :213 provides a minimum bound on the truncationeror for :7tt:314. n ' o r .13a) (20.\'ijr:. p r : 4 t r : l 1 2 . Thisis themidpoint Rqlston'sMethod lo.(11 lf3 and 7r1 RK the second-order algorithrus.4 RUNGE. Eqs.44) .. second-order this Runge-Kutta nethod is actually Heun's technique without iferationof the corrector. : 1 / 2 ) If a2 is assumed tobe ll2.. .40) can be solvedfor a1 : 1/2 ancJ : Qu : L Theseparameters.As with the second-order The following is the most commonly usedtbrm.42a) (20.l'i+r: )'i'l k2h where / < 1: /(t. yield /t .4r ) where f t r: . (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 . kth) (20.rr) n where f t 1: / ( . Conseqr-rently. +: ) ' i* i *2k:*2h*k+)h U(kr I (20.

As depictedin Fig. as Solution.t.496 PROBLEMS INITIAL-VALUE i /' .Equation(20.44b) kz: f ('. In addition.'.k' -t-l+ \Alo \ 20.*)r.y. the classical fourth-order RK method is similar to Simpson's1/3 rule. '.ot where k1: f fti. a: a: a.7.n) t.0 . For this case.pt-.44) then represents weightedaverageof theseto arrive at the imProvedsloPe. +!.: f (t. eachof theft's with an improved averageslopefor the a a represents slope. Employ the classicalfourth-order RK method to integrate I 4 r 0 8 t.3 r C l o s s i c q lF o u r t h . *'/-*<l L l "l *.+hh) (20.the fourth-orderRK methodis simito of lar to the Heun approachin that multiple estimates the slope are developed comeup interval.O r d eR K M e t h o d y'= Problem Stqtement. 20.!i) (20.r..44c) (20.tI u: a'.n) ka:f(tith.2\ : 3 .*)r.+1.44a) (20.44d) Notice that for ODEs that are a function of / alone. 2 EXAMPLE 0.7 FIGURE melhod RK the comprising fourth-order estimotes of depiction theslope C. 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 .i.the slopeat the beginningof the interval is computed : (0.!-)' .

1e08(0 .0) : 2 + 4.. I It is certainly possibleto developfifth.45) t. + ftr.r(.(7kr * -llAr+ 12k1* -l2As 7k)h + (20. .5.91291 + 5.+ + jr.4sd) (20.20.]l a/ l) (20. : : . slope is thenusedto rnakethe final prediction the end of the interval. a and a slopeat the end of the interval. . 2 1 1 2 9 9 ) 497 This slope in turn is usedto computeanothervalue of . 5 ) 4 .5) 4.211 ] u 1.r..-5(4.2.t.0 . t --i) r . Butcher's As this rnethocl a global truncationerror of O (h5). \r.9|2974 Finally.' * Io. . has Although the fifth-order versionprovidesmore accuracy. * + )0.45e) (20.n .y(1. : /(.0) : 6. :0.108649) 108649) 3.5):3.5):2+3(0.4 RUNGE-KUTTA METHODS This valueis usedto compute valueof .5): 2 + 1.45a) (20. 9 4 5 6 7 7 -0 ka: f (1.912974 -) 4 e 0 8 ( t 0 ) . : k7 : .912974 Next.vand anotherslopeat the midpoint: : r(0. i ) k:: ..'n hh) y(ti rr5: *ton.5..201031 1) [3 + 2(1.\'(1. this siopeis usedto compute valueof . expected.For example. 9 1 2 9 7 4 ) : 5 .19463 (e. Butcher's (196. + 2 r r ' n + |r. noticethat six tunction evaluationsarerequired.) jr'r k+: . * h .4sf ) I .5 ( 3 .201031 .3 .r'.45c) (20.4sb) (20.f (0. . .lr^o +]r.0) 5.-r. 5 ( 5 .5 5 : k r : l ' ( 0 . 5 .0. 103Vo).945611 : 4. ) ..n+ft*.211299(0.r..rn) Note the similaritybetween methodandBoole'srule in Table17.0..+) fiflh-order RK methodis written as )'i+r : )'i * where k 1: f G i .201031( which cornpares favorablywith the true solutionof 6.n) r o : f ( .912914(1. 108649 5) .Recall thatup throughthefourth-order versions.0) 2 + 3.5 : 4 c 08 ( 0 ).r and a slopeat the midpoint: a l'(0.and higher-orderRK methods.4.the four slopeestimates combined yield an average This average are to slope. * ). flnction evaluations n . at Q: 2e9)+ 2(3.t(.

of involve thousands simultaneous of ODEs.46) The solution of such a systemrequiresthat n initial conditionsbe known at the stafting value of /. i. ordershigher than four. .47) (20. : . r ' 1 . and integrate / : l0switha usingEuler'smethod. . two additional reltime. : . . for Euler's method. r .) t .t. .VALUE for are requiredlbr an nth-orderRK method.498 PROBLEMS INITIAL. .l t .5 SYSTEMS EQUATIONS OF of requirethe solutionof a system siand science Many practicalproblemsin engineering may ratherthan a singleequation.25kg/m. is best illustratedby the following example 2 EXAMPLE 0. this problem up amountsto solving the following systemof ODEs: dx ' dt du dt wat -6 ) (20.. ) . . .For the free-fall portion of the jump. Engineeringapplicationscan of equationssimply involves applyingthe each case. ) " : .As was donepreviouslyin Examples1. . r . atively popularityof the fourth-orderRK method. the initial would be x(0) : u ( 0 ) : 0 . conditions 20. :!:! dt :.5.8I m/sr. .1 Euler'sMerhod to can be extended systems in All the methodsdiscussed this chapterfor single equations In equations. r : u : 0. accelerastepsizeof 2 s. and thejumper hasa massof 68. r ' . ) ) i ) (20.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. .. .48) m If the stationaryplatform from which the jumper launchesis defined as r : 0.Interestingly.the gravitational tion is 9. .(1. \.) 4z : J : ( r .I kg with a dragcoefficientof 0. 20. . t. methodsof orderfive and higherare usuallyconsidered for the most computation for This is one of the main reasons the lessefficient than the fourth-orderversions. jumper's velocity and positionthatwe set An exampleis the calculationof the bungee at the beginning of this chapter.2.f. Solvefor the velocity andpositionof the free-fallingbungee to Assumingthat at / : 0.Suchsystems multaneous ordinary differentialequations generallyas be represented d)'t i : / 1 ( t . .1and 1.oneor acc0unt Because functionevaluations the are function evaluations necessary.) .the procedurefor solving a system This to one-step techniquefor every equationat eachstepbeforeproceeding the next step.

r ( I ) : . The process can be repeated computethe resultsat t : 4 as to . 8 1 ( 2:)1 9 .81 dt 0.3 Distonce velocity o freeJolling ond of bungeeiumper computed os numericolly Euler's with method.(/ : l!!! 61nn( ) lE!!. ll as du :::9.r:0*19.\ 1 9 . 6 2 ) 2z : 3 0 . 6 2 + f 9 . / a rl r l cl L \V. (a) 0 2 4 6 B t0 0 1 91 6 6 3 7t 9304 147.5 SYSTEMS EAUATIONS OF Recallthat the analyrical solurion velocityis tEq.r:0*0(2):0 u : 0 * 9 . e.62(2):39.\ V c.20. uEul".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.24 u: / 0. The ODEs can be usedto compute slopes / :0 the at dr -:U dt i::^ t i-l- ' Gr. 8 t .240A I t2 A674 2A4 664A 3 0 54 2 4 4 o r9 6200 36. l o 8 ) 68. respectively.9)l: for r.16629 r'(2) : 18.l f ' r 1 9 . the percentrelativeeffors are 1007oand4. Soluiion.81 68.25 .72919.3.86% 3 B3:d .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 . 6 2 The analytical solutions be computed can as.1 Euler's methodis then usedto computethe valuesat t : 2 s.4214 0 39.1567a.t \ l Proceeding a like mannergives the resultsdisplayedin Table 20. and Thus. rt. .I n l c o s h { . (20. + t . (x) e.u" ur"u* rEul".i \V .7292 33 il tB 42 4762 469575 49.n'J) Use theseanalyticalsolutionsto conpute the true relativeerrorsof the results.z ) This result can be substituted into Eq. (1.t(2) : 19.25 -::::(142:9. in TABTE2O.97% 1003% 6.

Finally. r t:]o + 9 .2 Runge'Kutfo Methods of RK methodsin this chaptercan be appliedto systems Note rhat any of the higher-order is helpfulin Figure 20.VALUE for how Euler'smethodcanbe implemented Although the fbregoingexampleillustrates largestepsize.0 ) : 9 . it is convenientto expressthe ODEs in the functional lbrmat of Eq.soo PROBLEMS INITIAL.9 J 1 t': {'t. equations.7 However. u) : I . : . 0 . the resultsarenot very accurate . ! : O + O i : o 22 u ( r ): u ( o + r r .u ) : 1 . 0) : 0 k 1 . we first develop 's) Theseslopes(a setof ft1 arethenusedto make slopesfbr all variablesat the initial vah-re.Next.r and u at the rnidpointof tlre first step: r r ' ( l ): r ( 0 ) f .5. .Using a much snrallerstepgreatlymitigatesthesedeficiencies.Thesenew slopes that are then taken back to the startingpoint to rnakeanotherset of midpoint predictions to make leaclto 1ew slopepredictionsat the midpoint (the l-1's). is the lth value of ft for theTth dependent first valuesof . illustrates approach.r : 0. (20. the lowing exarnple EXAMPLE 20.:u' tn Ct t . 8 1.r because does not changeuntil the second the resultsfor distanceare a bit unsatisfying next. we must calculate where ki. .. Use the fburth-orderRK methodto solve fbr the sameproblemwe addressed Example 20. r . : 1 2 ( 0 . 8 1 2 68. in Solution.That is.41))rhat are brought back to the beginningto make the final predictions.1 the variable. because the of of systems ODEs.. . x. First. t t ) . solverprovidesdecentresultsevenwith a relatively using a higher-order 20.l 2 : 9 .4.5 RK Solving Systems ODEs with the Fourth-Order Method of Problem Stotement. variableat the midpoint of the interval. (20. As iteration.In addition. the fr's are combined into a set of increment functions [as in The fblEq.Theseare then employed predictionsat the end of the interval that are usedto developslopesat the end of the interval (the fta's). visualizing the proper way to do this for the fourth-ordermethod.46)as dx E 0Il : f i ( t . J'r(O.Thesemidpointvalpredictionsof the dependent ues are i1 turn usedto cornputea setof slopesat the midpoint (the ft2's). described iargestepsize.fzj.caremust be takenin determiningthe slopes.tr The first stepin obtainingthe solutionis to solve for all the slopesat the beginningofthe interval: kr. 8 1 ]: n .

oilpur. r ( l ): .4817 k3.4561 whichcanbeused. the fourth-orderRK predictionsare much closerto the true values.Further. 8 1 0 0 : k 2 .4561) These used determine predictions theendof theinterval: are to the at . 4 5 6 1 ) Theseare usedto deternine the secondsetof midpoint predictions: 501 : 9 .8 : t'.4567 : 18. .7256 330995 42.2 18. 9 6 3 4 1 2 : 1 9 .. 4 8 9 8 1 2 :1 8 . ! : o + 9 .. 8 1 : 9 .9345 49. 8 1 ): 9 .4.a highly accurate. 4 5 6 9 . 9 6 3 4 1 + wltichcanbe used compute endpoint to the slopes: : : k q . 9. 0 .18. on Distonce velocity o freeJolling of os ond bungeeiumper computed numericolly thefourth-order method. .9575 4 44 2 \ 4 0 19.9 . [ 9 .u" e.1 8 .(2. 9 3 )I t47 9s21 2 3 75 t O 4 3 3 41 6 2 6 0 I8. fi(1.8100.4547 46. 8 1 0 0 + 2 ( 9 . : 8. 9.. u(l): u(0) tz:i + 0+9.. (u) 0.20.(20. 2 f 2 ( 1 .4427 .0 .5 SYSTEMS EQUATIONS OF which can be usedto computethe first set of midpoint slopes: k z : : . t J t ( 2 .9634) Tlrevalues t canthenbe used compute to of [Eq. 4 8 1 1 ( 2 ) :8 .u" JnKr unx{ TABLE2O.t : . 8 1 0 0 1 .4567.8100. 4 8 1 7 ) 8 .9134. 9 6 3 4 ) 1 8 .4 rt. r ( 0 )* t .o. 9 .r656 7 ) .4561 : :9. 9 1 3 11 .fr(1.0r 9% oo37% 0 051% a a49iL 0 038% 0 2 4 6 8 t0 0 1 91 6 6 3 71 9304 147.1h (2) u ( 2 ) : u ( 0 ) k 3 . 9 6 3 4 .004% 0 00r% 0 004% 0 000?{ 0 005% e. 1 6 5 6 6 u \ 2 ): 0 + .0 + 9.1898 ka. 8 1 0 0 .456't) 9.:9.0762 46. 4 5 6 1+ 1 8 .9134 x(2) : r(0) + k3. with RK ut. 9.9462 2 3 75 1 0 4 3 3 41 7 8 2 0 )B7292 331|8 42.(x) 0. ] 2 5 6 + 7 + 6Proceedingin a like manner for the remaining stepsyields the values displayedin Table 20. f t( 1 . nonzerovalue is computedfor distance the first step. In contrastto the resultsobtainedwith Euler's method.thesecond'setmidpoint of slopes: : kt.4a)l: x ( 2 ): 0 + I +9 ) t 0 + 2 ( 9 . 9. 2 l t : 0 + 9 . 8 l o o .

For example.h.f t(n+1) = tf. ? t. € . end y(I . initial times wiLh output.8 AnMJile implemenl RK4 ihe for lo meihod o system oltCDEs.5. I ^ ^ . np = 1r tp(np) . J . % times where solution output specific y0 = initial ? values of dependent variables % h = step size p1 . n = n+1. . .^ i . (Continued) . tt = tii = y(1. end ti I F = tspan(1..p2. FIGURE 2O.eqrates lt. | . \ F + 1.it is passed the function name defining the ODEs throughits argument.. 1 h . tfl. n = length(t).l t a ' t .error('ar arrJ I ).tspan. ' i ' ! N l b y 5 \ u l q L . L = (ti:h:tf)'. -^-LrlJarrr c^rr\ landfh/f input 'rcn:r LJPcrr argumenrs requ ired' t. l -. .3 MATLAB M-file Funclion: rk4sys Figure 20.8 showsan M-file called rk4 sys that usesthe fourth-orderRunge-Kutta method to solve a systemof ODEs.tf _ _ f = tspan(n) . or = ltO t1 .p2.t(nP).fl. = additi"onal parameters used by dydt % % output: % tp = vector yp = vector % it r! h - of of independent variable solution for dependent variables redsL 4 .). end else t .) % : int. end not ascendi ng order') . yp(np.F .y0. .\ r r r r v o r a r v g rk4sys: fou:-th-order Runge-Kurta for d system ol ODts = rk4sys(dydt.3) to solve a singleODE with Euler's method.). ' ^ l t L I l ' y .:) i -1 .. if t (n) <t. ^ h f f ^ .yl Z a system of ODEs wit. 4 y v ' L t t t ' .' r . 20. hh = g(nP+1) .4.tspan. This code is similar in many ways to the functiondeveloped earlier (Fig.h fourth-order RK meEhod ? inpuL: ?' dydt = name of the M-file that evaluaLes the ODES tspan = tti. . and final generaLed ac interval Z of h.I J a r t ' ..p1.:) = y0.tL.502 INITIAL-VALUE PROBLEMS 20._O I a: /^r / nargin..h L ... while(1) Eend = t(np+1).

end end np = np+1.0000 10. k2 = dydt |. Alternatively. r ts ts | rlih .:) + k2*hrh/2. k4 = dydL(tL+hh.y1i.:) + phi*hh.0000 6. the routine automaticallygenerates you want to obtainresultsat specifictimes. was can set rspan = tri rf l. be in ascending We can employ rk4sys to solvethe sameproblemas in Example20j.8 lContinued) output ln two ways. 2 ' : / 6 8 .:) + kL.we can developan M-file to hold the ODEs: f unction cty = dydtsys (t. end end = Y{1. end while(1) i f ct+hh>tend. if tt>=Lend.you As dependingon how the input variabletspan is specified.varargini ymid = y(i. :).0995 42 .it has an additionalfeaturethat allows you to generate the casefbr Fig.vararqrin{ : } )' .3.2))\ 0 2.:) . B \ . output valuesbetweentheselimits lf done in this way.952r a?? (141 334.4427 . Vei-rd=y(i. } )' . k3 .93II !47. . . t 1 .OF 20. 20. .:) i f tt>=t f.if at equalspaced the L span valuesmust can define r span = i t 0 .0 . . 1 * Y ( 2 ) as wherey ( 1) : distance(:r)and y 12) : velocity (u). r656 1L.12a6 33. older./2.tt+hY\/2. t f I . +k3*hh. brea]:. y) ^21. \. ymid = y(1.you intervalsh. 9 .1 526 0 tB.yend. hh = tend-tt.'end : })' .0000 0 19. YP(nP. : ] )' . First. break.*hrh. The solutioncan thenbe generated >> disp( [1-' ]'(:. whereri arld tf are the initial and final times. tp(nP) = tt. h]-i = h.varargin{ phi = (k1+2*(k2+k3) +k4) /6.0541 45.:) y(i+l.respectively.. = y(i.9344 49 . However.0000 8.dydt (tt+hh.0000 4. Note that in both cases.ymid.ymid.5 SYSTEMS EAUATIONS 503 i f hh>h./2.:) FfGURE20.1) Y( t.vararqin{ kl = dydt (tt. d y = l V ( 2 .

and b andd : the ratescharacterizingthe effect of the predatorprey interactions on the prey death and the predator growth.504 INITIAL.50) where x and y . >> tsPan=t0 >> disp(lt' 5 101.0000 334.0000 141.4021 PREDATOR.This case study focuseson two of these applications.the number of prey and predators. Predator-prey models were developedindependentlyin the early part of the twentieth century by the Italian mathematician Vito Voltena and the American biologist Alfred Lotka.The simplest version is the following pairs of ODEs: dx E:ax-oxY dv E--'Y+axY (20. The first relates to predator-preymodels that are used to study species interactions.VALUE PROBLEMS We can also use t span to generate results at specific values of the able.1) y(:. thoseinvolving xy) are what make suchequations nonlinear.The multiplicative terms(that is. respectively.9521 10.6 x .2))) 000 6.The secondare equationsderived from tluid dynamics that are usedto simulate the atmosphere.A5t11 49.respectively. These equations are commonly called Lotka-Volterra equations..tt Lorenz developed theseequations to relate the intensity of atmospheric fluid motionr to tAmhArotrrra rrariotinnc 1r enrl 7 in thp qnA . dz :-bz*xy .o y E u dt --rx-)-Jcl.PREY MODELS AND CHAOS Engineers and scientistsdeal with a variety of problems involving sysBockgrcund.49) (20. y(:.1626 42. c : the predator deathrate. For example.a = the prey growthrate. An example of a simple nonlinear model basedon atmosphericfluid dynamicsis the Lorenz equations createdby the American meteorologist Edward Lorenz: dx . tems of nonlinear ordinary differential equations.

c. Employ initial conditions _r:2 andy : I and integratefrom t: 0 to 30.9c.20. This decrease.3.0625. 0 6 2 5 .r and y versus /). Plot the results to visualize how the dependentvariableschangetemporally.c.c " y ( 2 ) + d * y ( L J* y ( 2 ) ] .V (: . 2 . t .2) . Because the predator population is initially small.6. h . the increasedpredatorscausethe prey to decline. b .plot(t. the RK4 method yields good results with the same time step. d = 0 .plot (y( :.Eventually.2). as expected' the ofthe predators.b. turn.b :0. b = 0 .8).6 CASE STUDY 505 coniinued with the predator-preymodel. a cyclical pattern emergesin time. B. c: 0.b * y ( 1 ) * y ( 2 ) . 6 .8. In addition.Note that the function eulersys was basedon modifyof ing the rk4sys function (Fig. '--') title (' (c) RK4 rime plot') subplot \2.9b).L s p a n .21 j title ( ' (d) RK4 phase plane plot') The solution obtained with Euler's method is shown at the top of Fig. 1).tspan. h = 0 . using a step size of h:0.t. At a certain point. t s p a n = t 0 4 0 1. andd: 0. a .2. 20.2. The time series (Fig.y.1) . the script also generatesa plot ofy versus x.9. because of its much smaller truncation error. Hence. 20.4 ) . 20. a.h. Use numerical methods to obtain solutions for these equations. First. 20. c . y 0 . the prey grows exponentia\.2. the prey become so numerous that the predator population begins to grow' leads in Eventually. the processrepeats. . The following script employs this function to generate solutions with both the Euler and the fourth-order RK methods.title(' subplot \2.y0. that. As in Fig.Notice to a decrease . graph the dependent variables versus each other to seewhether any interestingpatternsemerge.We will leavethe development suchan M-file as a homework problem. the nonlinearitiesstem from the simple multiplicative terms: xz and xy. . we can develop a lunction to hold the differential equations: function yp = predprey {t. theseresults indicate that the crude reinforced by the phase-plane Euler method would require a much smaller time step to obtain accurateresults.2. ) subplot (2. c = 0 .y(:. 20.d) y p = [ a * y ( 1 ) . 3 .9a) indicates that the amplitudes of the oscillations are expanding. The following parametervalues can be used for the predator-prey simulaSolution. In addition to displaying the solution as a time-series plot (.L).L) .b.2. Such phase-plane plots are often useful in elucidating featuresof the model's underlying structure that may not be evident from the time series.21 | title (' (b) Euler phase plane plot') tt V) = rk4sys(GpredPreY.1) . d ) . of tion: a : 1.Y (: .a. This is plot (Fig.3 ) .d) . In contrast.'--' (a) Euler tine plot' ) legend('prey'.'predaror' ) . I t y l = e u l e r s y s ( @ p r e d p r e y .plot(t.2 ) .plot (y ( : . subplot (2.y ( .v ( :.Y {:. y 0 = [ 2 i l i a = L .

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. for The phase-plane representation the accurateRK4 solution (Fi9.s06 ti. PROBTEMS INITIAL.VALUE . 0) and (cld. (20.49) and (20. Employ initial conditions .and r : 28.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 . andthe populationswill remain constant.50) to steadystate (dy ldt : dx /dt : 0) and solving for (x. method fime-series {b)phose-plone ond ond plots.Thelatter is the more interesting outcome that if the initial conditions are set at x:cld and : a /b. nothing will happen. the derivativeswill be zero. alb). ! Now. Also.b = 8I 3. i -+"t '. y) = (0. The exact location of this point can be determined by setting Eqs. observe that the processhas a fixed period-that is.9Q indicates that the interaction between the predator and the prey amounts to a closed counterclockwise orbit. {o) (cJ plots. II I ll tl tl tl ll t t I I I I I I I I t\ r I lt tl . The formeris the trivial result that if we start with neither predatorsnor prey.9 Solution theLoiko-Volterro Euler's for model. it repeatsin a set time.] I I . let's use the sameapproachto investigatethe trajectoriesof the Lorcnz equations with the following parameter values:a = 10. there is a resting or critical point at the center of the orbit. :i. Interestingly.20. RK4 meihod time-series ld)phcse-plone ond predator peak lags the prey.

0 0 1r.Thedoshed is wheretheinitiol line for.20. even though the pattems seem random.10.The resultsare superimposed the dashedline in as Fig. 5 ) of"r:.Forthiscase.Thus.03125.' .wewillusethefourth-order RK methodto obtain solutionswith a consrant time stepof h :0. the frequency of the oscillation and the amplitudesseemfairly consistent.)':z:5andintegratefrom/:0to20.In his original study.001). An interestingfeatureof suchsolutionscan be illustratedby changingthe initial condition for "r slightly (from 5 to 5.10. 20.6 CASE STUDY 507 confinued 20 E Lorenz model "{ versus r 15 5 . we can see that the Lorenz equationsare quite sensitiveto their initial conditions. The other variablesexhibit similar behavior.r is perturbed condttion {5. this led Lorenz to the conclusionthat long-rangeweatherforecastsmight be impossible! .: : 5 10 -5 -10 tc -20 FIGURE20. slighily i s 0 0 r. after about / : l5 they diverge significantly. 20.The term chaotic is used to describesuch solutions.As in Fig. solidtimeseries for the The is initiol conditions 5.Although the solutionstrack on eachother for a time. to an bouncing around from negative values to positive values. However. The results are quite different from the behavior of the Lotka-Volterra equations.IO Time-domoin representotion versusfor fheLorenz of x t equotions. 5 .r seems be undergoing almostrandompatternof oscillations. 5).the variable.

Because we are dealing with three independent variables.In a similar fashion to Fig. phase-planeplots reveal an underlying structure.rotions rr.even different computers)can introducesmall differencesin the solutions.Noticehowa structure is manif-est when perceivedfrom the phase-plane perspective.The idea is that rhe flapping of a butterfly'swings might induce tiny changesin the atmospherethat ultimately leads to a large-scale weather phenomenon like a tornado. ond (cJyl proiections The sensitivityof a dynamicalsystemto small perturbations its initial conditions of is sometimescalled the butte(Iy ffict.1i. 20.z1abe} ('2.xz. MATLAB's p1or3 function providesa vehicle to directly generate three-dimensional a phase-piane plot: >> plot3 (y(: .11 showsprojectionsin the x. Thesepoints are called strangeattractorsin the jargon of mathematicians who study suchnonlinearsystems. Beyond the two-variableprojections.I I represeniot. 20. we can generate projections.y(:. ) . the sensitivity of chaotic systemsto initial conditions hasimplications for numericalcomputations. differentstepsizes or different algorithms (and in some cases.12) depictstrajecrories cycling in a definite pattem arounda pair ofcritical points.Figure 20.Someof the problemsin this chapterand in Chap. the three-dimensional plot (Fig20. and the yz planes.L) .508 INITIAL. thesediscrepancies will eventuallylead to large deviations.10. 21 are designedto demonstrate this issue. versus! 45 40 JU 25 20 l5 1q -20 10 5 -20 -10 0 "r 10 10 5 -40 -20 0 ) 20 FIGURE 20. (bl .r:. As a final note.grid 20.on fhe Lorenz Phose-plone for (oJ eqL.VALUE PROBLEMS continued (a) y versusr (b) z versus-r 10 n -10 45 40 35 30 a25 (c) . Although the time-series plots are chaotic.y.ylabel('y' ) . (a) (b) (c) (d) . intt res As was the casefor Fig.The solutionforms orbits around what appearto be critical points. Beyond the initial conditionsthemselves.2) .2) ) >> xlabel('x' ) .y(:.

l.\ r'r'v/ /i. Display all your resultson the sameglaph.l*lin conrinued t'\ 30 20 10 v0 --l I I l\l tI l-I I --l -\| _10 -20 -2n I rl t-t_ t- 40 . -:.12 Three-dimensionol phose-plone representoiion Lorenz forlhe equolions generoted MATLABs with p}ot 3 iunction.2 Solve the following problem over the interval from -r : 0 to I usinga stepsizeof 0.5.4 0 -20 FIGURE 2O. UsineRalston's method.1 Solve the following initial value problem over the interval frorn / : 0 to 2 where r'(0) : l Display all your resultson the samegraph. (a) (b) (c) (d) Analytically.5. Using the fburth-orderRK method. dr' dt -:\. Usingthe fburth-order methodwith i : 0. . Using Euler'smethod. PROBLEMS 20.PROBLEMS 509 K. RK (a) (b) (c) (d) (e) Analytically. . Using Heun's methodwithout iteration. Using the midpointmethodu'ith ft : 0./--l l\.25. 20.5 and 0.1r -" r I I ).25 where-l (0) . Using Euler'smethodwith /i : 0.

y(0) : 2 and .-O dtt . . lions from 1950throush2000was for Pl so 20 m( pl( po 5' 2tJ mi of uIr 5.i. the rate of changeof populationcan be modeledas dp dt : * c . -.but can be formulatedas circuit that aroseback in the days of vacuumtubes: k. . Display all your results the samegraph. R e c o g n i t i n g that dxldt: u. = p 4 4 5 4 4 8 5 0 5276 5686 6079 1400m/s..r(l .h 3 factors such as fbod shortages.Display your resultsasa plot has 20.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.. t : time (s). (c) the midpoint method.4 works adequately -yawhen population growth is unlinrited.'10. (P20. : the maximum growth rate under unlimited conditions.. rnd (b) the fourth-orderRK Obtain your solutions with (a) Heun's method without method with a step size of 5 years. f t. 3 7 z 1 0 6m ) . : kr. 2A fb al Pr 2( Er rh 2( pe is b1 ta Si h: ---(l at' . 20. =6.4. p . Using this growth rate fbrmulation.5 Although the model in Prob. 1*t ) .000million people.kgtn:0."*.J The growth of populationsof or. pollution.rr. )(0): I and 1.t2.6 Suppose rnodelsassumes that the rate ofchange ofthe population is 7' earth'ssurface. 1(0) : )'(0) : 1.4 using a step size of 0. ( l .t. and /max: the rnaximurn population.p/p.0261yr.3 Solve the iollowing problem over the interval fi'om I :0 to 3 using a stepsizeof 0. (b) Heun's method with iteratingthe initial conditionsand parameter valuesfor your simulation: correctoruntil s. Plot both solutions thesame on grrph.(0) : 4.r'') .. and lack of space inhibit growth. (b) Use the fburth-orderRK method along with the results (a) Euler's methodand (b) the fourth-order RK method.it breaksdown when .. . K-dlf)\o"'(R+. One of the simplest that a projectileis launchedupwardflom the 20.4. 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.. L The initial conditions are . solve the fbllowing initial-valueproblem from t : 0 to 4: r/l l -lQ\. Disof (a) to stimulate the world population from 1950 to play your resultsas a plot. 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.8rnAr). at low popr"rlationdensity p {{ p^u.: kpp ul dt.5ro INITIAL.. a fbrce balancecan be usedto derive dLt (P20. dl 20. is sontetimes called the curn-ing t'upucin. 20..7 Solve the lbllowing pair of ODEs over the interval (a) Assumingthat Eq.-5 wherey(0) : l..4. Thus. gineeringand scicntiflc applications.Display your simulation (/l' :-21.the growth rate is not a con20.along with the datafiorn Prob. k.tt The van der Pol equation is a model of an electronic stant. 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 .population.9 Given the initial conditions. pmrr : 12. Obtain your solution with I 950 throughI97Oto estimate ks. Assumethat the only force actingon theobproportionalto the existing populationat any time /: ject is the downward force of gravity. solvethis equationfrom 1 : 0 to 10 using Euler's methodwith a step sizeof (a) 0. .and (d) Ralston's methc)d. Employ the tbllowing iteratingthe conector.55-5 million people.Note that 2n.ganisms rrany en.1) .2 ancl(b) 0. < 0. and px (in 1950):2. Given the initial conditions. 20..p f 1 t .1) holds. As p approaches7r.x: altitude (m) measured upward from the earth's surface.". Under theseconditions. The world population in milwhere u : upward velocity (m/s)..r12 tlt where ftq : the growth rate.the growth rate approacheszero. 2050 with a ste-p sizeof 5 yeals.VALU E PROBLEMS This is referredto as the logistic model. 20. -+ kr.:0 dt .The analytical solution to this modelis y-P\l 2{). ) where t. use the data from frorr r : 0 to 0..t4et results alongrvith the dataon a plot. ln such cases.1%.

PROBLEMS and (b) the Obtainyour solutionswith (a) Euler's rr. I . LltdI vegeravaging rtpprouched.5. .2.6) liom 1960through2020 using the tbllowing coeflicient 5 years. l. The damping coefficientc takes on three values havebeen tracked.: : 0 .Testyour prograrnby using it to solvefbr population as describedin Prob. from equiliblium position(m)' t : wolves crossedan ice bridge from Ontario.Test your programby using it to solvefor population describedin Prob.. < 0' l7o. 20. wherex : displacement tation. Plot both solutionson the same graph along with the exact solution : cos34. 20. 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. v a l u e sa : 0 .I5 (a) tntegratethe Lotka-Volterraequations (Section 20.5.10 Developan M-file 1tlsolvea singleODE with Heun's methodwith iteration.l5) is describedby the fbllowing oldiniuy difl-er$ith a stepsizeof 0.Designthe M-file so that it creates plot miclpoint of theresults.7 20. ' 1 a n d d : 0 .15 The motion of a damped spring-nrass system theresults. Employ a step size of as and iteratethe correctol'until c. nrclose and the wolves.i:0 Moose arrived around 1900. RK fourth-order method.25. : 20.Employ Prob.+k. 5 1'eiirs 20. (s).t' 20.14 Isle RoyaleNationalPark is a 2[0-square-mile pelagocomposedof a sinele large islandand many snrall dx d2x lr-.+t . 20.rethod use In both cases.In 19. 0 0 0 4 .Design the M-file so that it createsa plot of the results. their population b1 l9-10.193 143-5 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 . (Fig. plot a Euler'smethod..11 Develop an M-tile lo solve a single ODE with the l method.Design the M-file so that it creates tburth-order of seriesplot and determinethe sum of the squares the a plot of the results. entialequation: archi20.Designthe M-tile so that it creates plot ol 20.13 Develop an M-file to solve a systemof ODEs with (b) Developa phase-plane olyour solution. 2 3 . .i000. rn :20-kg mass.Tesl your program by using it to solve residualsbetweenyour model and the data for both the a stepsizeof 0. a stepsizeoi 0.and c : the damping coefficient Sincethe late 1950s. and islandsin Lake Superior'. 0 : 0 . 20. P20.Test your programby using it 1osolveProb. 0 1 3 3t.the numbersof the mooseand wolves time (N .19. Employ a step size of 5lt FIGURE P2O. s/m).12 Develop an M-file to solve a single ODE with the Compare your simulation with the data using a tinreRK method.

is The ambient temperature 20 'C.is the constantambi.55.it is 23. in 20.CB:-l .tration and temperatureol' the reactor as a function of time.n A(h 20. and tlre initial displacement : I m..20The following equationcan be usedto modelthededT .) flection of a sailboatmast subiectto a wind force: (1-)' t ) where K > 0 is a constantof proportionality.the to oa- -1. E : modulus of elasticity.^-^^ . We assumethat at the time of death. of lnitially. (critically damped). 20..r 0 / ( r + 2 7 3 ) ) C.18 The reaction A --+ B takes place in two reactors series. 20. Use one of the numericalmethodsdescribed this 20.l 0 ( T .: outflow (mr/st.t ) / ( T + 2 7 3 ) ) g death.2 0 ) it experimeutalobservations..ssnssrtration ofA at the outlet of the secondreactor.The spring constantk : 20 N/m.^n+ ^f :-^-1:\T^r^ ]L^* +L^.16 A sphericaltank has a circular orifice in its bottom through which the liquid flows out (Fig. CB r : concentration B at of the outlet of the first reactor(andinlet of the second).kCB: T where CA6 : concentration of A at the inlet of the first reactor.The reactorsare well mixed but are not at steady massbalancefor eachstirredtank state. :-K(T dt Calc Use I: 20. This is perature the reactor.: 1 0 0 0 e ( .CA 1 .3 : the gnrvitacocfficient. : the areaof the oril'ice A tion. (b) Solve the ODE nunerically and plot the results. lional constant the tank. Use t : 0. (a) DetermineK and the time of death.16). find the concentrations is of.. = I -^A I ff^'.2 Und diff( ! ( whe A(h gra\ outl area min h(0" h. I --j:-(CAt-CA)-kCAt dt T J dCBt ----:-.: dl I -tCBt . 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. and i".5"C. C : an empirically derived and B in both reactors during their first 10 minutes operaof (mr).The flow rate through the hole can be estimatcdas dCB. nonisothermal batch reactor can be described in by chapter determine to horvlong it will takefor the waterto flow the following equations: out of a 3-m diametertank with an initial height of 2. The initial velocity is zero. Plot the displacement versustirne for eachof the three valuesof the dampingcoefficient on the sameplot. atr FIGURE P2O. CA2 . . the reactor at I 5 "C and is has known as Newton's law of cooling. I : -CBtIkCAt . . = CB2 concentration B in the secondreactor.5 "C.and 200 (over40 damped).und h: the depthof liquid in initial conditions of all the dependentvariablesarezero.2 gair enc sho this stru dev -7.r: produceB. Solve . if f(/) is the tem.lf C:Ao equalto 20.r this equationusing a numericalmethodover the time period 0 < / < 15 s. dC --.s6nqsrtration ofA at the outlet of the first reactor (and inlet of the second).a concentrationof reactantC of 1.4 where Qou. - 11- ) tl:: where /(l) ?E I : wind force.Supposethat at time I : 0 a corpse is discoveredand its temperature is nreasured be I. P20./ (a ) - . Note that the orifice hasa diameterof 3 cm and C: 0. of 5 (underdamped).and k : the rate constantfor reaction ofA to C AnEii Q.The unsteady-state reactoris shownbelow: ICA' I :-(CAu-CA:)-kCAr . aIt varl( dCA.19 A.0 gmoyl-.17 ln the investigationof a honricideor accidental dl it is often important to estimatethe time of death. ent temperature: 2(1.81 m/srt. and that two hours later. Find theconcenperatureof the object at time /. l2lmin and r : 5 nrin and assume the that (:9. : residence of r time fbr eachreactor.512 INITIAL-VALUE PROBLEMS Suppose that the temperature the corpsewhen it wasdisof coveredwas 29.15 m.I6 A sphericol tonk.From the dT -. Thus._ _ r \ .

'itational constant 9.the analysisis limited to horizontal motion of the can structure.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. m2 6 I l7 .r1/th : I m/s.tt ( r 2 l.developa three-dimensional the displacements. e : I m .05 for your computation. : 0 to 20 s. ( 0 ) : 6 r n .23 Repeattlre the sarnesirnulationsas in Section 20.1 { .22 Engineers and scientists gain insight into the dynamics of structures under the influFigure P20. P20.2-5 108. Presentyour results as plots of (a) displacements (b) velocities. Undera numberof simplifying assumptions.forcebalances be UsingNewton'ssecond developedfor this systemas d--tr Simulatethe dynamicsof this structurefrom . and c : depthof pipe outletbelow the pond bottom (m).21A ponddrainsthrougha pipeas shownin Fig. 20.her-e : depth (m).6 for the Lorenz equations use a value of r :99 . fbr shorvssuch a representation a three-storybuilding.22 ence of disturbancessuch as earthquakes.21.al: pipediarneter /i A\h): pond surf'ace areaas a functionoldepth (m2t. d : 0 . ^l -{. /-q\tl Le) qT0-\-= / 7 r r 1 2 1 0 0 0k g k.24Perfornl the samesimulationsas in Section 20.PROBLEMS 5r3 -_-_t_ e d P2O. solve this diff'erentialequation to determine how long it takes tbr the pond to empty. For this case.2I FIGURE varies with height accordingto " 200.6. - . two time-series and phase-plane plot of In addition.. given that l . 2400kN/m qTn'-r FIGURE P2O. 2(1.-\\)- - )l) dt: t ) t tn: K1 .+: = -rrr Ilt| t ^) ---:(. dl' . Ure paranreterl'alues of L :30.1. n: = 8..81 m/s2). E : 1. 2 5m . ffr A(h). and all other initial values of displacementsand velocitiesare zero. u. Compare but vour resultswith thoseobtainedin Section20. dr h. r: time (s).6 for the Lorenz equations but generate solutionswith the the rnidpointmethod. siven the initial condition that the velocity of the ground floor is d. m1 m2 A'x3 /1t- .g : (: grar.t) . -/ T.tu f--. 20. arrd x / : 0.000 kg ) .22 k1 3000kN/m 543210 a97 A67 445 A32 01B 0 use mass-spring modelsto 20. utt -: --\ -.96.\r ltr (l * j('r: -.rr) ]tl| t. fbllowing the differential how depthchanges equationdescribes with time: ' 1A(h)' (nr). L--]"il1\ = In2 10. Basedon the following area-depth table..tt. law.

I METHODS ADAPTIVE RUNGE. a consequence.l 5 to 2. a for that a fairly large step size could be employedto obtain adequate The an localizedregion from t : | . 21.1. Learninghow to adjustthe optionsfor MATLAB's ODE solvers. 2I.the solutionchanges Suchbehaviol'suggests results. Understanding what is meantby stiffnessand its implicationsfor solvingODEs. For a signiticant nuinber of solution of the type depicted in example.many more calculations-would be wastedon the regions gradualchange.the solutionundergoes abruptchange.this can representa selious limitation. to Learninghow to passparameters MATLAB's ODE solvers. o of how the Runge-KuttaFehlbergmethodsuse RK methods Understanding to that are usecl adjustthe stepsize. practical consequence dealing with such functions is that a very small stepsizewouldbe of algorithm required to accuratelycapturethe impulsive behavior.For most ofthe range. objectivesand topics coveredare r r o o . Fig. therefore.Adoptive Methods ond StiffSystems CHAPTER OBJECTIVES methods The primary objectiveof this chapteris to introduceyou to more advanced Specific for solving initial-valueproblemsfor ordinary differentialequations.supposethat we are integratingan ODE with a gradually. difl'erentordersto provide error estimates Familiarizing yourselfwith the bLrilt-inMATLAB lunctions for solvingODEs. . we have presented problems.KUTTA methodstor solving ODEs that employ a constant step To this point.However.If a constantstep-size wouldhave were employed. much smallerstepsizethan of necessary-and.25. for the and multistepmethods solving Understanding differencebetweenone-step ODEs. For size.the srnallerstep size requiredfor the region of abruptchange As a to be applied to the entire computation.

e control. {^.I 2 A ^ e x o m o o i o s o. There are two primary approaches incorporate to adaptivestep-size control into onestepmethods.lr'Urgiventhe initial conditiony(a) : 0. or the Betbre proceeding. they are said to have adaptivestep-sii.A u t o m o i i c t e p s i z e e o s nli. .r<lmant hn< nrnnt ^l.we should mention that aside from solving ODEs.1 ADAPTIVE RUNGE-KUTTA METHODS 5r5 FIGURE I. Implementation suchapproaches of requiresthat an estimateof the local truncationerror be obtainedat each step. the fbllowing techniques be emcan ployed to efficiently evaluate definite integrals involving functions that are generally smoothbut exhibit regionsof abruptchange. .^^o.The evaluationof the definite integral t: _t rb Ja I J$tdx is equivalentto solving the differentialequation dy -/rr.This error estimatecan then serveas a basisfor either shortening lengthening stepsize. Thus.o.Stephalving involves taking eachsteptwice. the methods describedin this chaptercan also be usedto evaluatedefinite integrals.r^^.I 21. Because they "adapt" to the solution'strajectory. suchcoses.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 . Algorithms that automatically adjustthe stepsizecan avoid suchoverkill and hencebe of great advantage. once as a full stepand then as . .

local truncation RK the enor is estimatedasthe differencebetweentwo predictions usingdiff'erent-order methods. ode23.45)1.*)r. The ode23 function usesthe BS23 algorithm (Bogacki and Shampine. The formulas to advance the solution are J'i+r : li -t where I n(.kt)lt (2 . The step size can then be adjusted basedon this error estimate.(20.nvented problernby deriving a fifth-orderRK this methodthat employsmost of the samefunctionevaluations requiredfor un accompanying fourth-orderRK rnethod.they aresometimes by refen'edto as RK-Fehlbergmethods. For example. In the secondapproach.c ) 1 I * (*5kr -t 6kz + 8k1.calledetnbedded methods.l MATTABFunctions for Nonstiff Systems Since Fehlbergoriginally developed approach.r./(r.. 1989.. Shampine.tb) ir") ( 21 ..andfifthorder predictionamountsto a total of I 0 function evaluations step[recallEqs. facevalue.r) (2t. The embedded methodswere first developed Fehlberg.2h -t 3kz 1.:f(. thereare really only three because after the first step.2a) Note that althoughthereappearto be fbur function evaluations. t r : f ( r .gk+)h /l (71.l.44) per and 120. The error is estimated as Er+t : where k q : .a fburth. his other even betterapproaches havebeen developed.1994).the ideaof usingtwo predictions difAt of ferent order might seemtoo computationally expensive. Thus. the approach yields a predictionand error estimate basedon threeevaluations rather .r. f ( t i + t .1) r t1 : . Severalof theseare availableas built-in functionsin MATLAB.the k1 for the presentstepwill be the ftafrom the previous step.Hence.* ' r r . Thus. r .) ' i + t ) lr. Fehibergcleveriy circur. the approach yieldedthe error estimate the basis only on of six functionevaluationsl 2l.2) (2t. RK These are cun'entlythe methodsof choicebectruse they ale more efficient than stephalving.which simultaneously usessecondand third-orderRK fbrmulas solve to the ODE and make error estimates step-size for adjustment.516 ADAPTIVE METHODS AND STIFF SYSTEMS two half steps. The differencein the two resultsrepresents estimateof the localtruncaan tion eror.) (2Lla) r.

After each step.r": 4e08t.2. A b s T oIl l (21. odefun is the name of the function row corresponds a time in the column to returninga column vectorof the right-hand-sides the difTerential of equations.If it is. or use t-qpal = ltA t1 tn).MATLAB recommendsthat ode45 is the best function to apply as a "first try" for most problems.I ADAPTIVE RUNGE-KUTTA METHODS 5r7 than the five that would ordinarily result tiom using second-(two evaluations) and thirdorder (tlrreeevaluations) RK formulasjn tandem. >:.r.'d) variables and each wherey is the solutionarraywhereeachcolumn is one of the dependent vector t.and y0 : a vectorcontainingthe initial values.I that the analyticalsolutionat t:4 is 75. tl tn (all increasing all decreasing).21 . .5*y. Thesefunctionscan be calledin a numberof differentways. = -t.lA 4).If the error and k1 with reducedstepsizesuntil the estimated is too large.0. Herc is an example of how ode45 can be used to solve a single ODE. the stepis repeated error satisfies E < n t a x ( R e l T ox l y l . the value of y. Second. odeL13. Observethat the criteriafor the relativeeffor usesa fractionratherthan a percentrelativeerror as we havedone on many occasions prior to this point.-5.Note or n'rethod we will describe in that this is a multistep as subsequently Section21. span spect ifies the integrationinterval. f-spar Ltt tf).'1 ocle45(odefun.First. The ode45 functionusesan algorithmdeveloped Dormandand Prince(1990). k+ becomes lbr the next step. It. t-span. if it is entered a vectorof two as numbers. -r.(dydt.33896.to obtain solutionsat specific times t0. It is useful for stringentemor tolerances computationallyintensiveODE functions.8*L) -0. Note that .a1 is accepted.y) 4*exp(0. ode45. ode4 5 can be usedto generate sameresultnumericallyas the >> dydt=G(t. Representing ODE as an the anonymousfunction.The simplestapproach is Ir.2I . fiom /:0 to 4 with an initial conditionof y(0):2. s-pancan be lbrmulatedin two ways. The ode113 functionusesa variable-order Adams-Bashforth-Moulton solver. the ODE is typically stored in its own M-file when . >> y(Length(tt ) 75. the integrationis perfornredfrom ti to tf. by which simultaneously usesfburth. Recallfrom Example 20.3) j) (default: l0 where RelTol is the relativetolerance andAbsTol is the absolute tolerance (default : l0-6).y1=66"4!.andfifth-order RK formulasto solvethe ODE and make error estimatesfbr step-sizeadjustment.3390 As described in the following example.the error is checkedto determinewhetherit is within a desiredtolerance.

6)rlz Solution. Suchequationsarerefered to aspredator-pre)'cquations. 0.B"y(2)+0. 21.81'*0. We storedthis M-file underthe name:predprey.y) whiclr yieldsFig. 7l.l 2l ADAPTIVE METHODS AND STIFF SYSTEMS Using MATLABto Solve o Systemof oors ProblemStotement.m over the range definedby tspan usingthe initial conditions found in yo. One way to do this is to createan M-file as in yp r uflCLi on yp = pr eopi e/ ( L. The results canbe displayed by typing simply >1 plot(t. spdn fn 20 . 2 E Solution predotor-prey of moo'el MATLAB with . .5r8 EXAMPLE .2.6*y(1) . you must createa functionto compute the right-handside of the ODEs.2ytE: : where .2*y lI) 0.1'2 1 at 1 : 0. Before obtaininga solutionwith MATLAB.3y1y2 .3*y(1) *y(2) I . m.-A. F I G U R2 I .y) *y\2\ = l'1. >> ya = 12. dt'' dv.r'1: 2 and. Employ ode45 to solvethe following setof nonlinear ODEsfrom t:0to20: -0. The solvercan then be invoked by This command will then solve the differential equationsin predprey.:l. Next. enter the following commandsto specify the integrationrange and the initial conditions: '.

1). Some comare monly usedparameters 'RelTol ' 'AbsTol ' .v (:. To tions' parameters. turesof the solution. a plot of the dependent >> plot (y( :. 3 E model plot Sloie spoce of predofor-prey wiihMATLAB ?. addition.'par ' . options' p1' p2.-."') where options is a datastructurethat is createdwith the odeset function to control feathat andpJ .METHODS RUNGE-KUTTA 2I .rnirialsrep' 'MaxSf ' Allows you to adjustthe relativetolerance. .vaf. tolerance Allows vou to adiustthe absolute the determines initial step.I ADAPTIVE 5r9 plota In addition to a time seriesplot. p2. it is also instructiveto generate phase-plane variablesversuseachother by that is. F I G U R2 I . A completelisting of all the possibleparawhere the parameter meterscan be obtainedby merely enteringodeset at the commandprompt.This optionalautomatically The solver you to setyour own. yA. 3 . The odeset function has the generalsyntax options = odeset ('parr' . !.This .. lows of The maximum stepdefaultsto one-tenth the t span interval.. .vaf ' . tspan. havecontroloverthese lt.2 1 oot ? y1 to the As in the previousexample. areparameters you want to passinto odefun.yl = ode45(odefun.2) ) w h i c h v i e l d sF i s . has the value vaf ..thereis alsono controloverthe differentialequaious aspects are additionalarguments includedasin features. 2 l . MAILAB solverusesdefaultparameters control varIn ofthe integration.) par.

A 7 5 ^ 2 ) ) 0 .a .( L .5).5. options). 4 E Solution CDEwithMATLAB (b). MATLAB automaticallycreatesa plot of the resultsdisplayingcirclesat the valuesit hascomputed. ( 'ReLTol t0. y) y P = 1 O * e x p( . F I G U R2 I . . we can implementthe solverwithout settingthe options.A 4l .from I : 0 to 4: /t al 2 dt : 10e {r l) [ 2 r 0 . in Fig.the solvertakesmore snall steps attainthe increased to erccuritcy. 2l. Use ode23 to solvethe followine ODE. Solution.4b. 0 7 5 r| 0.5. smoiler of For o relotive toleronceuseo nence error is monv ono no'esteo:oerol. Ie-4) . 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. 4).2 I * ( t . ' As in Fig. First.we will create M-file to computethe right-hand an sideof theODE: function yp = dydt (t. noticehow ode2 As 3 takes relatively large stepsin the smoothregionsof the solufion whereasit takessmaller stepsin the region ol'rapid changearoundI : 2. y I . When we implement one of the ODE solversin this way.er'. 2 l .4a. Obtainsolutions the default(10-3)and for a more stringent for (l0r) relative error tolerance.6t where1(0) :0.2 ) / ( 2 * . 0. 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.Hencethe defaultvaluefor the relativeenor ( l0 3) is automatically used: >> ode23(@c1ydr. t Note that we have not setthe function equalto output variables tr . 6 * y t Then.520 ADAPTIVE METHODS AND STIFF SYSTEMS EXAMPLE . ( b ) R e l T o l= 1 0 .

. (Fig.y.2.) f (t.11(Fig.2 MULTISIEP METHODS 52r 21.15)]: as : . This suggests that the predictoris the weak link in the methodbecause has greatesterror. In this section.17)]: .Consequently.1 ' .5b). Thus.1 The Non-Self-Sforfing Heun Method Recallthat the Heun approach uses Euler'srnethod apredictor[Eq.2 MUITISTEP METHODS The one-stepmethodsdescribed the previous sectionsutilize information at a single in point tr to predict a value of the dependent variable. Alternative approaches. the of 21.y.5a).v)h rule and the trapezoidal as a corector [Eq.*.2I .))+r)'i*fQi. (20.The curvatureof the lines connectingthesepreviousvaluesprovidesinformation regardingthe trajectoryof the solution. (20. arebasedon the insightthat..4) y!*.Multistep methodsexploit this informationto solve ODEs. 21. way to improve Heun'smethodis to developa predictor thathasa local errorof Othi). 1 g: J'.we will presenta simple second-order method that servesto demonstrate generalcharacteristics multistepapproaches. 21. This can be FIGUR2I.This weakness signilicant because efficiency of the iterativecoris the the one on of rector stepdepends the accuracy the initial prediction.5 E ond beiween one-step {b)multlstep drfference of Grophicol depiction thefundomentol {o) for merhods solvingODLs .+rrt a future point 1. n ( 2 1s ) . calledmultistepmethods once the computation has begun. the predictorand the correctorhavelocal truncationerors of O(/rr) and O(h3). it respectively.)+ f (t. valuable infbrmation from previous points is at our command. * (21.

6.a s i n p lj+r : -l'r-r * . of note that the equationis not self-starting because involvesa previousvalue' thedepenit of problem. In addition. 21./ ( r . . r .522 ADAPTIVE METHODS AND STIFF SYSTEMS froma accomplished usingEuler's methodand the slopeat. As depicted Fig. and extrainformation by p r e v i o u s o i n t l ' r . ) EXA S4-ri . r . (21.5) and (21. 6 E (o) midpolnt A grophicol depiction lhenon of Heun method isused thot self-sfirrling method The rrle is oso predictor The os {b) tropezoidol thot employed c corrector S l o p e.'!.t'i)2h (2r. (21. This centering inrproves local errorof the predictor O1h3 the to 1.6) is now located themidpoint at ratherthan at the beginningof the interval over which the predictionis rnade.f (ti. Eqs.6) This fornrulaattainsO(h3) at the expeuse employinga largerstepsize2h.6) are called the nort-selfsterting Heunmethod.. * .thederivative in in estimate Eq. F I G U R2 I . dent variable)r-r. f * . Sucha valuewould not be available a typicalinitial-value in Becauseof this fact.

2 .An approximate an estimateof the error can be determined usingEq. ( p o i n t .0 .6b): 1 l/+r : _1'.'t(t" ''t/'") ' ' /r (for. (2 1. Note that -r'j" and y'l_.t .9): l" Lr((ir- 6.-s493-31 which represents true percentrelativeerror of -5.8)] then usedto conpute the varlue: vi:2+ j r . we arenow dealingwith a multithe initial conditionat / because stepmethod. Solution. the approximateerror.. Eq. to the l^ .6a): f'j'*r: . 7 )t h r o u g h 2 1 .2I . yi)Zn Predictor + Corrector (Ftg.tl z-T srtr 3 1 4.54933 l) 1 I : 6.313749 6. J 7 c " ..1Solc the incun'ed the self-startins Heun. Use the non-self-starting Heun methodto performthe samecomputations were peltbrmedpreviouslyin Example20.21.0 .)'.o .8)canbe appliediteratively irnprove solution: Now.S t o r t i n ge u n ' sM e t h o d H Problem Stotement.T : m . ' ) J (r'''1". are the final results of the corrector The iterationsare terminatedbasedon an estimateof iterationsat the previoustirne steps. 2 l" : 0 is -t': 2.607005 is The conectorIEq. (21. o o z o o . T h e u s eo f E q s .2l .l:1..That i.t .54933 r 6.0.313149 which represents error of -1. 9 ) When lenI is lessthana prespecified At error tolerance the iterationsareterminated.0.5(6.5(2)] 2 : 5.( 2 1 .+ n " * ' .927o.7)lis usedto extrapolate linearlyfrorn r : -l to 1: .""nr) where the superscripts denotethat the correctoris applied iteratively fiom 7 : I to la to obtain refined solutions. However.313149 | lx | r 0 0 ":/3 .ltr 523 (2t. 5 ( 2 )J .194631).3929953+ [4"'r'iu' .s t 2 I : 6..trr-li . A s w i t h E x a m p l e 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 . (21.1 " 0l i t t . EXAMPLE l .we requirethe additionalinfbrmationthat1 is equalto -0.2 MULTISTEP METHODS Thenon-self-starting method be summarized Heun can as (Fig. s t s .s.2. as i n t e g r a t e .'lr t' (t. this e.1) ( 2 18 ) . 121.l e r ) x ' I ' . 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.3 2 N o n . .!i+ r I x l00lo ( 2r .2 using Heun's method.'/+l r.r| : -0. The predictortEq.3929953 at / : -1. I t i-l -'i+l lo t_1.S e l f .139o(true value :6. This a in error is somewhat smallerthan valueof -8.

because the initial predictorvalueis more accurate. the at For the secondstep.30224 : -3.. the iterationsconvelge a on value of 6.8)canbe appliediteratively Equation until e.(3){6. . iterationsconvergeon the sameresultas was obtainedwith the self-starting Heunmethod.68%)..ub.the rate of convergence theconecof tor is somewhat irrprovedbecause the betterinitial prediction.r.r u r r r 1 6 ) < It (2r.]'l': 2 * l4ett 13.dividing (21. r i i ' : .13) 0: . Hence.il) By recognizingthat the correctoris equivalentto the trapezoidalrule. (t.8vc).5(6.un b.14) 5 andreanangingthe r.36087(et : -2.U. 1 (2t. { by gives result ) .36087)2: .2).13) yield from ro (21.As with the adaptive RK methods Section in 21.524 ADAPTIVE METHODS AND STIFF SYSTEMS (21.1r.4) t.. : -ih ) ' t ' ( { .l t ' f " t € .097o). of 21.9+r *r.ift3.t'.15) ./ r ' . .and subsequent (e. :1-.l2l Thiserrorestimate becombined can with theconector result to . a similarestimate of the local truncationerror lbr the correctoris (Table 17.1 7 r . Eq.76693 .u.its localtruncation erroris (Table17. However.1 Equation I) .(21.This errorestinrate 7r can be cornbinedwith the estimateof )'i+ r from the predictorstepto yield value:t.14) where is now between 1 andf .ructed Eq.The first correctoryields 15.As with tlre previousstep. rnultistepmethodconverges a somewhatfasterrate.As was the casewith the Heun method (recall Example 20.) : --ht l"l€.. r ' ' " ' { € .2.1r True + fir.y..tr.) (2I.r{.2 Error Estimqtes Aside from providing increased efficiency.0. t : ..1ir r.. the error estimatethen providesa criterionfbr changingthe sfepsize.44346 €t :9. ) 1 (21..1'. (21.r (21.6.0826(et : l87o) rhar was computedwith the original Heun method.11 give True value: )'. The error estintate can be derivedby recognizingthat the predictoris equivalent the to midpointrule.10) where the subscript designates that this is the error of the predictor.1. I 6 'r.falls belowa prespecified value ofe.43a/c wlrich is superior to the prediction of 12. 15.2) ll E. Now.the predictoris sitt .the non-sell-starting Heurrcan alsobe used to estimate local truncation the ernrr. . 9 +-r.

Et : 14. 2r.30224 13.We have thereforeincluded this sectionto introduceyou to their basic principles.15) are identical. At /.If the third derivativedoesnot vary appreciably over the interval in question. (21.3.194631and 14. Additional information can be found elsewhere(e.l6) to give Thesevaluescan be substituted F_ 6. A stiff systemis one involving rapidly changingcomponents changing ones.That said.12) and (21. yields 6.g. ple 21.31176 : which also compares favorably with the exact error.84392.30224.a1 : l.11 :2.3 STIFFNESS 525 Notice that the right-handsidesof Eqs. and therefore. respectively. the predictorgives 13.with the exception of the argumentof the third derivative. 2006).1662341 and the correctoryields 15..0 _ ri-t 1'ri (l l. In some cases.607005 into Eq. and the comector Solution.we can assumethat the right-handsidesare equal.multistep methods are usually not the method of choice for most problemsroutinely confrontedin engineeringand science.the MATLAB function odel13 is a multistep method.84392. after which the solutionbecomes . the predictorgives5.15.2I . ) EXAMPLE 21.360865 5.360865 -0.45831. Chapra and Canale. (21..194631 6.For example.44346 can be usedto comDute F -_ 15.16) to estimatethe per-step .the left-handsidesshouldalso be equivalent. they are still used.9*r anAthe corrector("vlir) .4 Error Estimqte Per-Steo Truncotion of truncationerror of ExamProblem Stotemenf Use Eq.16) truncation Thus.44346 -0. The foregoing has been a brief introduction to multistep methods.360865.3 STTFFNESS Stiffnessis a specialproblem that can arisein the solution of ordinary differentialequatogetherwith slowly tions. Note that the true valuesat / : I and 2 are6.the rapidly varying componentsare ephemeraltransients dominatedby the slowly varying that die away quickly.30224: -0.607005 -0.as in E ' 5.) i-t . which compares : Et : 6.150722 : well with the exacteffor. (21. we have arrived at a relationshipthat can be usedto estimatethe per-step error on the basis of two quantitiesthat are routine by-productsof the computation:the predictor(1.which At /. Although they still have their place for solving certain types of problems.

the analyticalsolutioncan be developed as .l't+l :)'i -alih .oo2e' (21. is in F I G U R2 I . . 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.1 by term(e-rmr). 1 0 0 0 . Although the transient existfor only a shortpart of theintegration interval. l 9 t g i v e s E . 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 / ) .005).0and asymptoticallyapproaches zero.1 0 0 0 r '* . p e ' c e pi b l e e n l . calculuscan be usedto determinethe solutionas -t' : -1'ue ttl Thus.1 0 0 0 e ' (2r. (21.. there tronsienl octuo ly o fost ircnsieni from y : g 1e I thct occursin lessthnn the 0 OO5lime unil. Both individual and systems ODEs can be stilT. Insight into the stepsize requiredfor stability of such a solutioncan be gained exby part tlre aminirrg hor. .An exampleof a singlestitT of is ODE .This .17): dt' dt EX (2t. n dt S u h s t i t u t i n gq .t 2 | . Euler'smethodcan be usedto solvethe sameproblemnumerically: -]i+t :)'it dt.they can dictatethe time steplbr the entire solution.t9) If f'(0) .998e-l{){xt 2. w h e n h e r e : p o n s e viewed on the finertimescole the insel. : .)t).526 ADAPTIVE METHODS AND STIFF SYSTEMS D l o 'o r o . the solution startsat ).17) If I (0) : 0. 7 E phenomena components.l : 3 0.the solutionis initially dominated the fast exponential governed After a shortperiod (t < 0. 21.nogeneous of Eq.18) As in Fig./ r' .

(21. For the fast transient part of Eq. l .