https://www.scribd.com/doc/132776204/ApplMatlab
07/26/2013
text
original
CONTENTS
About the Aufhor Prefoce xiii xvii iv
Guided Tour
PanrOur Modeling, Computers, ond Error Anolysis I
l. I Motivofion I 1.2 Port Orgonizotion 2
CHAPTER I
Mothemqticol Modeling, Numericol Methods, ond Problem Solving 4 I .l A Simple M o t h e m o t i cM o lo d e l 5 1.2 Conservotion Lowsin Engineering ond Science 12 1.3 Numericol Methods Covered in ThisBook l3 P r o b l e m s1 7
CHAPTER 2
MATLAB Fundomeniqls 2.1 2.2 2.3 2.4 2.5 2.6 2.7
^ l l
20
TheMATIABEnvironmenl21 Assignment 22 Mothemoticol Operotions 27 U s eo f B u i l t l n Functions 30 Groohics 33 OtherResources36 CoseStudy: Explorotory DotoAnolysis 37
rroblems JY
CHAPTER 3
Progromming with MATTAB 42 3.1 MFiles 43 3.2 InputOutput 47
vl
CONTENTS
dr o g r o m m i n g 3 . 3 S h u c t u r eP 5l 3.4 Nesting ond Indentotion 63 gunction 3 . 5 P o s s i nF to s MFiles66 3.6 CoseStudy: Bungee Velocity 71 Jumper Problems 75
4 CHAPTER
Roundoff qnd Truncotion Errors 79 4.1 ErrorsB0 4.2 Roundoff Errors 84 4.3 Truncotion Errors 92 ,l03 4 . 4 T o t oN l umerico E l rror , o d e lE r r o r so 4.5 Blunders M , n d D o t oU n c e r t o i n t y1 0 8 ,l09 Problems
.11 PnnrTwo Roots ondOptimizotion
2.1 Overview tll 2.2 Port Orgonizotion I 12
CHAPTER 5
Roois: Brocketing Methods I l4 in Engineerin 5.1 Roots og ndScience I l5 5 . 2 G r o p h i c oM l ethods I l6 ge t h o d s 5.3 Brocketin M o n d I n i t i oG l u e s s e s1 1 7 5 . 4 B i s e c t i o n1 2 2 5.5 Folse Position128 5.6 CoseStudv: Greenhouse Gosesond Roinwoter 132 ,l35 Problems
CHAPTER 6
Roots: Open Methods  39 6.1 Simple F i x e d  P o iln te i rotion 140 6 . 2 N e w t o n  R o p h s o1 n4 4 6 . 3 S e c o nM t ethods 149 6.4 MATLAB Function f :z e r o r 5 1 6 . 5 P o l v n o m l o l s1 5 4 : i p eF r i c t i o n 1 5 7 6 . 6 C o s eS t u d yP Problems 162
CONTENTS
Yrt
7 CHAPTER
Optimizofion 166 7.1 Introduciion ond Bockground 167 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 o7 n0 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 o7 n9 Eln e r g y l 8 l m ndMinimum Potentio 7 . 4 C o s eS t u d yE : q u i l i b r i uo P r o b l e m s1 8 3
,89 PrnrTxnrr Lineor Systems
3.1 Overview 189 3.2 Port Orgonizotion l9l
CHAPTER 8
Lineqr Algebroic Equofions ond Motrices  93 8.1 MotrixAlgebroOverview 194 Algebroic Equotions with MATLAB 203 Lineor 8.2 Solving in Circuits 205 Currents ond Voltoges 8.3 CoseStudy: Problems 209
9 CHAPTER
Gouss Eliminotion 212 of Equotio sn 2 1 3 9.1 Solving SmolN l umbers E l i m i n o t i o n2 1 8 9 . 2 N o i v eG o u s s 9.3 Pivoting225 9.4 Tridiogono Sly s t e m s2 2 7 9.5 CoseStudy: Model of o HeotedRod 229 Problems 233
IO CHAPTER
[U Foctorizotion 236 237 l0.l Overview of lU Foctorizotion Eliminotio 238 10.2 Gouss on s lU Foctorizotion 244 1 0 . 3 C h o l e s kF yoctorizotion 'l0.4 Left Division 246 MATLAB Problems 247
vIt!
CONIENTS
CHAPTER II
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 A i r P o l l u t i o n2 5 8 :n d o o r Problems 261
CHAPTER I2
Iterotive Methods 264 l2.l Lineor 264 Systems: GoussSeidel ,l2.2 N o n l i n e oS r y s t e m s2 7 0 1 2 . 3 C o s eS t u d vC : h e m i c oR l eoctions 277 P r o b l e m s2 7 9
PtrnrFoun Curve Fitting 281
4.1 Overview 281 4.2 Port Orgonizotion 283
CHAPTER I3
Lineor Regression 284 R 13.I Stotistic se v i e w 2 8 6 es gression 1 3 . 2 L i n e oL r e o s t  S q u oR re 292 ,l3.3 Lineorizotion f N o n l i n e oR s0 0 r e l o t i o n s h i p3 1 3 . 4 C o m p u t eA r p p l i c o t i o n s3 0 4 1 3 . 5 C o s eS i u d y E : nzyme K i n e t i c s3 0 7
rroDtems 5  z
^ l l
CHAPTER I4
Generol lineqr LeosfSquores qnd Nonlineqr Regression 316 1 4 .I P o l y n o m i o Rle g r e s s i o n 3 16 14.2 Multiple L i n e oR r egression 32O 1 4 . 3 G e n e r oL r e o sS l i n e oL t ouores322 14.4 QR Foctorizotion ond the Bockslosh Ooerotor 325 1 4 . 5 N o n l i n e oR e g r e s s i o n 3 2 6 r 1 4 . 6 C o s eS l u d y F : itting S i n u s o i d s3 2 8
h
rrontcms
l l
.1.1 /
CONIENIS
'I 5.'l lntroduction to Interpotofion336 I5.2 NewronInterpoloring polynomiol 33g polynomiol j: .j tosron9etnrerpoloring 347 rJ.4 tnverse tnterpolotion350 I J.J txkopolotion ondOscillotions 351 Problems 355
t6 cHAPTER
Splines ond piecewise Inferpofofion I 6 . I l n k o d u c t i otn o Splines 359 l o . z L i n e aS r p l i n e s3 6 1 16.3 Quodroiic Splines365 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 ond Differentiotion 3g9
5.1 Overview 3g9 5.2 Port Orgonizotion 39O !H,\PTER 7
Numericof fnfegrofion Formutos Sg2 'l Z.J lnhoduction ond Bocrground 393 I7.2 NewtonCotes Formutos 396 17.3 TheTropezoidol Rule 39g 17.4 Simpson, su l e s 4 0 5 R 17.5 HigherOrder Newfon_Cotes Formulos 4j j l7 6 lntegration with Unequol Segments 412 17.7 OpenMerhods 416 I7 8 Muh,ple I n t e g r o l s4 j 6 Compuring Work wirh Numericol lnregrorion 4j9 ;lJ;:r";udy:
t I
t I
!
i{.
Numericof Integrotion of Functions 'l 8.I Introducfion 426 18.2 RomberI qn i e q r o t i o n4 2 7
CONTENTS
18.3 Gouss Q u o d r o t u r e4 3 2 I8.4 Adoptive Q u o d r o t u r e4 3 9 I8.5 CoseStudy:RootMeonSquore Current 440 Problems 444
CHAPTER I9
Numericql Differentiqtion 19.I 19.2 I9.3 ,l9.4 ,l9.5 448 Inhoduction ond Bockground 449 HighAccurocy Differentiotion Formulos 452 Richordso nx t r o p o l o t i o n E 455 Derivotives of Unequolly SpocedDoto 457 Derivotives ond lntegrols for Dofo with Errors 458 19.6 Portiol Derivotives 459 I9.2 Numericol Differentiotion with MATLAB 460 I 9 . 8 C o s eS t u d yV : i s u o l i z i nF gi e l d s 4 6 5 Problems 467
Pnnr5x
Ordinory Differentiol Equotions 473
6.1 Overview 473 6.2 Porl Orgonizofion 477
CHAPTER 20
InitiolVolue Problems 479 20.I Overview 481 20.2 EuleisMethod 481 20.3 lmprovemenls of Euler's Method 487 20.4 RungeKutfo Methods 493 20.5 Systems of Equotions 498 20.6 CoseStudy:PredotoryPrey Modelsond Choos 50A Problems 509
CHAPTER 2I
Adopfive Merhods ond Stiff Systems 514 21 .'l AdoptiveRungeKutto Methods 514 2l .2 Multistep Methods 521 2l .3 Stiffness525 2l .4 MATLAB Applicotion :u n g e J B e u m p ew r i t hC o r d 5 3 1 2l .5 CoseStudy: Pliny's lntermittent Fountoin 532 r r o D l e m s3 J /
CONTENTS
xl
22 CHAPTER
BoundoryVolue Problems 540 22.1 lntrodvction ond Bockground 541 Method 545 22.2 lhe Shooting 22.3 FiniteDifference Methods 552 P r o b l e m s5 5 9
APPENDIX A: EIGENVALUES 565 FUNCTIONS 576 APPENDIX B: MATLAB BUILTIN APPENDIX €: MATIAB MFltE FUNCTIONS 578 BIBLIOGRAPHY579 rNDEX 580
Modqling,CoTpute''i ,
qnd Erior Anolysis
t.t
MoTtvATtoN
What are numericalmethodsand why shouldyou stridythem? problemsare formulatedso are techniques by which mathematical Numericalmethods Because digital computers that they can be solved with arithmeticand logical operations. numericalmethodsare sometimes referredto as comexcel at perform.ing suchoperations. puter mathematics. se.In the precomputerera, the time and drudgeryof implementingsuchcalculations riously limited their practical use. However, with the advent of fast, inexpensivedigttul computers,the role of numerical methodsin engineeringand scientific problem solving has exploded.Becausethey figure so prominently in,:' much of our work, I believe that numerical methods should be a part of every engineer'sand scientist's basic education.Just as we a.ll must have solid founand science, dationsin the other areasof mathematics of we should also have a fundamentalunderstanding numerical methods.In particular,we should have a solid appreciation of both their capabilitiesand their 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 of equations. , , d.l, and complicated geometriesthat are not uncommon in engineeringand scienceand that are often impossible to solve analyticallywith stanyour dard calculus. As such"they greatlyenhance problemsolving skills. 2. Numorical methods allow you to use "canned" software with insight. During your career,you will q*"
AND ERROR ANALYSIS I MODELING, COMPUTERS, PART
prepackaged computerproto use commercially available invariablyhave occasion grarnsthat involve numericalmethods. The intelligentuseof theseprogramsis greatly of the basic theory underlyingthe methods.In the abenhanced by an understanding you will be left to treat suchpackages as "black boxes" 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 withouthavingto buy or commission to solveproblems software. Because nuNr.rrnerical methodsare an efficient vehiclefbr learningto usecomputers. they areidealtbr mericalmethods ale expressly designed for computerimplementation, 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 of how computerscan At the sarne serveyour professional development. lime, you rvilI alsolearnto acknowledge and control the errors of approximationthat are part and parcel of largescale numericalcalculations. Numericalmethodsprovidea vehiclefbr you to reinforceyour understanding of mathernatics. Because one tunction of numericalmethodsis to reducehigher mathematics to basic arithmetic operations.they get at the "nuts and bolts" of some otherwise and insight can result from this alternative obscuretopics. Enhancedunderstanding perspective.
3.
4.
5.
how numerical With thesereasons lls motivation.we can now set out to understand work in tandemto generate reliablesolutionsto mathematmethodsand digital computers ical problems.The remainderof this book is devotedto this task.
1.2 PART ORGANIZATION
This book is divided into six parts.The latter five partsfocus on the major areasof numerAlthough it might be temptingto jump right into this material,Part One conical methods. of dealng with essential backgroundmaterial. sists four chapters provides 1 concrete example of how a numericalmethodcan be employed Chapter a problem. this, we develop tt muthematicalmodel of a fieefalling To do to solve a real jumper. model, is Newton's secondlaw, resultsin an ordinary The which basedon bungee to develop a closedformsolution,we then differentialequation.After first using calculus generated can with a simple numerical method.We be show how a comparablesolution rnethods that we cover in an of the major areers of numerical overview end the chapterwith Parts Two throughSir. Chapters2 and 3 provide an introduction to the MATLAB' software environment. way of operatingMATLAB by enteringcommandsone Chapter2 dealswith the standard modeprovidesa straightforward t'alculatornuttle.Thisinteractive at a time in the socalled meansto orient you kl the enviroumentand illustrateshow it is usedibr common opera
I.2 PART ORGANIZATION
Chapterl shows how MATLAB's programming mode providesa vehicle for assemThus,our intentis to illustrate how MATLAB into algorithms. bling individualcommands your software. programming environment to own a develop servesas convenient 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
if i .{. r 1 FIG For f^^ l I cc ium r YOU'VE GOT A PROBTEM upposethat a bungeejumping companyhires you..lrfi.l ) as a function of time during the freefall part of thejump. Specificobjectivesand topicscoveredare o r o Learning how mathematical modelscan be formulatedon the basisof scientific principles to simulate the behavior of a simplephysicalsystem. l. 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.. Learning aboutthe difterent typesof numericalmethodswe will cover in this book. Understanding the different typesof conservation laws that lie beneath the models usedin the variousengineering disciplinesand appreciating the diff'erence betweensteadystate irnd dynamic solutionsof thesemodels.1:i ffilT** TFi + i. Understanding how numericalmethodsirlford a meansto generate solutionsin a rnannerthat can be irnplemented on a digital computer. You're given the task of predicting the velocity of a jumper (Fig.. Based on this insightandyour knowledge .r I r. U1 I Mothemoticol Modeling.t " i'i. Numericol Methods. This inlbrmationwill be usedaspart of a largeranalysisto determine the length and requiredstrengthof the bungeecord for jumpers of differentmass You know from your studies ofphysics that the acceleration shouldbe equalto the ratio of the tbrceto the mass(Newton'ssecond law).
along which the system's behavioris beingdetermined. However.I A SIMPTE MATHEMATICAL MODEL of fluid mechanics. ol'velocity ducd.on the basisof his observations. such as time and space. : J [ v a n a D t e s. p u r a n ) e t el r  v.tendent variable is a characteristic that usually reflectsthe behavioror state of the system:the independettt variablesare usually dimensions. m is the n. .l Forces ociing on o lreeJolling bungee iumpet.rodels figure prominentlyin the way engineers and scientists use numericalmethodsin their work.r'itr he s p e ctto t i m e .of the secondlaw is the wellknown equation F:ma (t. or model.. This will involve developinga con. I. Newton formulatedhis secondlaw of motion.1) can range from a sirnple algebraic relationshipto largecomplicated setsof differential equations.we will illustrate an alternativesolution approach. or kg nls").I .a n. The ntathematicalexpression. it can be represented as a functionalrelationshipof the fonn Deoenden( / inde n. .l A SIMPTE MATHEMATICAT MODET A motlrcnntical ntodelcan be broadly definedas a tbrmulation or equationthat expresses features the essential of a physicalsystemor process in mathematical terms. (1.81nls21.you know that calculusmight be usedto obtain an analyticalor exactsolutionfor u as a function of /.e forcine \ '.l) where the de. a n dc i i s i t s a c c e l e r a t i o . you developthe following mathematical model for the rate of change r.rass of the object(kg). r : time (s).and thelbrring. The actual mathematicalexpressionof Eq.rputerorientednumericalor approximate solution.In a very general sense. ca: a secondorder drag coetficient (kg/m). . and m: the jumper's mass(kg).a o_ le unctlons \ . (n rn/s:). we will also show how mathematical n. g : the accelerationdue to gravity (:9.f (l.2) whereF is the net force actingon the body (N.In so doing. flGURE l. which states that the time rateof changeof momentumof a body is equalto the resultant force actingon it. . theparametersareretlectiveof the system'sproperlies or composition. Aside from showing you how the computercan be usedto solve this particularproblem. nclent s. our more generalobjective will be to illustrate (a) what numericalmethodsare and (b) how they figure in engineering and scientificproblen solving. For example. '.functiotts areexternalintluencesacting upon it. 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). Because this is a ditlerentialequation.in the following pages.
3)has a numberof characteristics that aretypical of mathematical of the physicalworld.4) Thus. the rate of changeof the velocity is equal where u is velocity (in metersper second).sed to formulatethe force due to pravity as Fo:m8 whereg is the acceleration due to gravity(9. It describes It represents an idealization and sirnplification of reality. it yields reproducible its poses. the solution of Eq.1): F: Fol Fti (1 . 1. we will expressthe net force in terms of measurable For a body talling witlrin the vicinity of the earth. Becauseof its simple algebraicform. 5 ) If force in the downward direction is assigned a positive sign.That is.'elocity it into Eq.the Ifthe net force is zero.2) was obtainedeasily. ( 1.1). Ifit is negative. (1. we are not yet predictinghow acceleration because models Equation( 1. a naturalprocessor systemin mathematical terms.3)can be usedto compLlte acceleration. Thus. may be much more complex. Note that for this simple casethereis no independent variesin time or space. Newton's secondlaw can be usedto determinethe terminal velocity of a freefallingbody jumper (Fig. SOLVING MATHEMATICAL NUMERICAL AND PROBLEM The secondlaw can be recastin the format of Eq.l) by merely dividing both sides by m to give F n1 (1. . and nr is a parameter.6) .To illustrate a more complex model of this kind.the net force is composed of two opposing forces: the downward pull of gravity Fp and the upward force of air resistanceFy (Fig. the model ignoresnegmanif'estations.6 MODELING. the objectwill decelerate. variablesand parameters.F is the forcing funcwhere a is the dependent variable tion. object'svelocity will remain at a constantlevel. Next.other mathernatical techniques mathematical and either cannotbe solvedexactly or requiremore sophisticated than simple algebrafor their solution. (1.For example.31 variablereflecting the system'sbehavior. . can be usedfbr predictivepurFinally. ligible detailsof the naturalprocessand focuseson its essential minimal importance the secondlaw doesnot include the effects of relativity that are of when appliedto objectsand forcesthat interacton or aboutthe earth'ssurfaceat velocities and on scales visible to humans.consequently.3)to yield duF dt nr (1. to the net force acting on the body normalizedto its mass. ( 1.Our falling body will be a bungee a model can be derived by expressing the acceleration as the time rate of changeof the (tluldr) and substituting r.If the net force is positive. modelsof physicalphenomena However. METHODS. the secondlaw can be u. (1. resultsand. . Eq. nearthe earth'ssurface.if the force on an object and massare known.the object will accelerate.For this case.81m/s2).1.
8) is a ntodelthat relates Ecluation the accelerirtion of a falling objectto the tbrces actingon it.) yilt / (1.9) where tanh is the hyperbolic tangent that can be either computed directlyr or via the more elementary exponential function as in e' . Therefbre. 7 Air resistance can be fbrmulatedin i.8) (1. ( 1. more adt'anced techniques suchas thoseof calculusnrustbe appliedto obtainan exactor analytical solution.1is a proporticlnalitv constantcalled the drag coefticient(kg/m). calculus can be usedto solveEq.1)wheret. For example. Thus.1kg leaps ProblemStotement. 4 1 through to yield dt: .(/) is the dependent variable.7might be a function of the type of clothing or the orientationusedby thejumper duringfree tall.in contrast to the solutionof Newton'ssecond law in Eq.5 ctt t dt tn (1.However. ( 1..I . EXAMPLE I .the exactsolutionof Eq. ( 1.l cdr)' (1.e' e .25kg/m.8.9)to compute velocityfbr the first l2 s of fiee fall. .t is the independent variable ctt and m areparameters. the jumprraster is havinga particuiarly bad dayl). ( 1. I MATLABG'allows d i r e c rc a l c u l a t i o n of thehypcrbolic t a n g e nv t i a t h c b u i l t . Also deterrnine the terminal velocity that will be attainedfbr an infinitely long corcl(or alternatively.* e \ tanht: (1. ( 1.i nf u n c t i o nr d n h ( r ) . f U: . The parameterc. the greater the fall velocity. For the present c&s€.ff*"n( u(/) .9)is castin the general fbrm of Eq.1 1 . if thejumper is initially at rest(r.3).suchas shape or surface roughness.Rather. c.. Knowledge from the scienceof fluid ntechanics suggests thata gtrodfirst approxirrration wouliJbe to assume that it is proportional to the squareof the velocitl. 7 )c a nb e c o m b i n e d E q s . (1.7) where r'. ( 1 ./ accountslbr properties ofthe ialling object. It is a tli/Jerential equtttiort because it is written in terntsof the differentialrate of change(d uI dt 1 of the variablethat we are interested in predicting.I A SIMPLE MATHEMATICAL MODEI.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 J u m p e rP r o b l e m jumper with a massof 68. The rlet fbrce is the differencebetweenthe downward and upwi. andg is theforcing function. that affect air resistance.ivariety of ways.Use a dragcoefticient of 0.8)for :r.rrd force.8)for the velocity of the jumper cannot be obtirinedusing simple algebraicmanipulation. . . .10) Note that Eq. A bungee liom a stationary hot air balloon. the greaterthe uprvardfbrce due to air resistance. : 0 at / : 0).Use Eq.
4214m/s (about 110mi/h) is attainedafter 10 s.9)yields which can be usedto compute t. (1.2).92 33I I tB 42 4762 46 9575 4 S4 2 t4 50 6175 516938 According to the model. 1. the jumper acceleratesrapidly (Fig. 3 u.2 The onolyticol solution forthebungee problem oscompured in Exomple I 1 Velociry iumper increoses with time ond osympfoiicolly opproocheso terminol velociiy.MATHEMATICAL MODELING. Inserting the parameters into Eq. A velocity of 49. NUMERICAL METHODS AND PROBTEM SOLVING Solution.mfs 0 2 4 6 B t0 12 o I B 72. . Note also that after a sufficiently lons FIGURE I.
9) is called an anabtical or closedform nrodels fies the oliginal diffbrentialequation.lr) whereAu and At aredifferences in velocityand time computed u(r1) overflnite intervals.the only alternativeis to develop a numericalsolutionthat approximates the exact solution.ti .I A SIMPLE MATHEMATICAL MODEL 9 time. the net force is zero and acceleration has ceased.3): the time tateof change dtt . 1. of velocitycan be approximated by (Fig.*.Remember that because dtt  dt lim A1+l) Au N ( l. I I ) represents process.the force of gravity will be in balThus. tlrereare mirny matlrematical that cannotbe solved exactly.: Lt dt u ( / i + r) u(ti) ti+t .In many of thesecases. Note that du ldt = Lu I Lt is approximate from calculus Ar is flnite.6mi/h) is reached. is velocity at an initial time ri.8) by realizingthat can be solvedby arithmeticoperations.called the terminol velocitt'.) Lt li+t . Equation the reverse FIGURE I.Au . ancewith the air resistance.Unfortunately.of 51.) is velocityat somelater time f11.) Ar_u(1.: . eventually.6983m/s (115. a constantvelocity. and u(ria. solution because it exactly satisEquation( I .3 io / The thefirsf derlvotive of u wifhrespect use difference to opproximote of o finite AU Approximateslope u(r.] .ti (r. This velocity is constantbecause. problerr is refbrmulatedso it Nttntericalntethods are thosein which the mathemirtical This can be illustratedfor Eq. ( 1.
r L I For the next interval lfiom r : 2 b 4 sJ.9lrt.1 r " l:16.62tl + 19. Perform t h es a m e cornpLltatio an si n E x a m p l e1 .rl x 2 : te.(19. 1 3 ) to colnpute velocitywith Euler'smethod. That is.to MATHEMATICAL MODELING. clesignates velocityattinle /i and At : ti+t . tEq.l tr+t * t.4117m/s . can be rewritten as the equat.62rls 68.2s .13) wherethe nomenclature u. We cannow seethat thedifferentialequation hasbeentransformed into an equation that canbe usedto determine the velocityalgebraically at ri+l usingthe slopeandprevious valuesof u andt.11canin tum be employed to extendthe cornputation to velocityat l. I . Thus at any time along the way.12 and so on. the velocity of the jumper is zero.H. This new valueof velocityat l. (/{):0). (1.+r .ce opprcrirnationof the derivativeJt Iirnc /. with the result : .the colnplrtationis repeated.r' .ti.r 2 ) Notice that the tenn in brackets is the righthandside of the diff'erential equationitself (1. EXAMPLE 1.o. This equationcan then be rearrangecl to yielcl u(/i+r ) : u(/i)1_ . At the startof the computation Using this infbrmation and the parameter values from ExampleI . . you caneasilycomputevelocityat a latertime f 11.J _ _ t . Eq.2 s: r.t L l 0. t / 7 ) )tn./i) [r ( r . :g . NUMERICAL MEIHODS.1I ) is callecl aJinitediJJeren. It can be substituted into Eq.8)to give r'(1. it provides means a to compute rate the of change slope or of u.81 l().: o* fr. We'll discussit in more detail when we turn to diff'erential equations later in this book. Solution..8)1. 1 b u tu s eE q .ff(/. New valne: old vahle* slopex stepsize This approachis tbrnrally calledEuler's metlnd.. Thus.1)u(t..ion ui+: rr + ' ! u {1t (t. 2 ' 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 J u m p e rP r o b l e m Problem Stoiement.13)can be used to corxpute velocityat 11. Employ a stepsizeof 2 s fbr the calculation.62 68. ( J .AND PROBLEM SOLVING Equation( 1. ( 1. If you aregivenan initial valuefor velocityat sometime l.
you can accurately model the velocity of thejumper without having to solve the differentialequationexactly. there is some discrepancy betweenthe two results.1 A SIMPLE MATHEMATICAL MODEL tt Terminalvelocity FIGURE I.because we have employed straighfline segmentsto approximatea continuouslycurving function. 1.the effort associated with using smaller and smaller step sizeswould make such numerical solutionsimpractical. as the straighrline segments track closerto the true solution. with the aid of the computer.4 along with the exact solution.13)at 1sintervals results in a smaller error.Thus.However.4 for thebungee problem Compcrison of thenumericol ond onclyticol solutions iumper The calculationis continuedin a similar fashionto obtain additionalvalues: ttS u. .We can seethat the numerical methodcaptures the essential featuresof the exact solution. m/s 0 r9.One way to minimize such discrepanciesis to usea smallerstepsize.Forexample.6200 36.I .Using hand calculations. applyingEq.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.large numbers of calculations can be performedeasily. (1.However.
(1. the steadystate For the bungee condition would correspond to the casewhere the net lbrce was zeroor [Eq.which is also given a specialnarnethe stea(lystate in engicalculationhas many applications For example. jumper.r4) This is preciselythe fbrmat that we empioyedwhen using Newton'slaw to developa force jumper tEq. they embodythe two fundamentalways that conservation laws areemployedin engineering As such.ation lan:s.r2 MATHEMATICAL MODELINGNUMERICAL METHODS.decreases Change: 0 : increases or : decreases Increases (1. fluid flow in pipes.2 CONSERVATION Aside from Newton's secondlaw. to predict changes with respect to time. Such tradeoffsfigure prominently in numerical methodsand constitutean importantthemeof this book.14)and (1. will tbnn an importantpart of our etforts in subsequent chapters to illustratethe connection betweennumericalmethodsand engineering and science.fbr steadystate incompressible neeringand science. 6) can be solvedfor the terminal velocity AlthoughEqs.Eq. AND PROBLEM SOLVING price nrustbe paid for a nroreaccurate As in Example 1. If change is zero. balance for the bungee Although simple.14)embodies ways in which one of the most fundarnental conservation laws are used in engineering and sciencethat is.8) with du ldt : 0l l1l$ : f 6411 (1. ( 1. We will give it a special namethe timevariable (or transient) computation.8)1. They all boil down to . (1.15)might appeartrivially simple. I . This case. there are other major organizingprinciples in science and engineering.(1.they and science.r5) Thns. the balancecan be used to computethat the flow out of the fourth pipe must be 60. ifno changeoccurs. Among the most importantof theseare the conserv.the increases anddecreases nrustbe in balance. ( 1.Eq.14) becomes .2.a cornputational numerical result.decreases Change: increases (1.5. L/AWS lN ENGINEERING AND SCIENCE 1. (I. as in Flow in : flow out For the junction in Fig.Each halving of the stepsize to attainmorc accuracyleadsto a doubling of the nurnber of computations. the great conservationlaws of scienceand engineeringare conceptuallyeasy to understand. .the flow into a junction musl be balanced by flow going out. the downwardand upwardfbrcesarein balanceand Eq. at steadystate.Although they form the basis for a variety of complicatedand powerful mathematical models. anotherway in which conservation laws are appliedis fbr cases wherechange is nonexistent. Thus. we seethat thereis a tradeoffbetweenaccuracyand computational effort.r6) Thus. Aside from predictingchanges.
most consistof recasting mathematical operations into the simple kind of algebraicand logical operations compatiblewith digital compllters.The energybalance specifies that the clranges of voltagearoundany loop of the circuit must add up to zero. which resultsfrom the conservation of charge. is similar in spirit to the flow balance depictedin Fig.5. force balances are utilized to analyzestructures suchas the simple trussin Table 1.I. Sirnilarly.Forcivil engineering.andmechanical. Finally. . We will endeavor from these to includeexamples areas in the coming pages.The currentbalance.For exalnple. We shouldnotethattherearemanyotherbranches of engineering beyondchemical. The massbalanceis derived from the conservation of mass.5 nf p i p e s A f o w b o o n c ef o rs t e o d y i n c o m p r e s s ifb ue i df l o t v o t t h ej u n c l i oo Tablel..electriccurrentmust balanceat thejunction of electricwires. 1. civi. I.It specifiesthat the changeof mass of a chemical in the reactordependson the amount of mass flowing in minus the n.6 summarizes the major areas coveredin this text. Civil and mechanicalengineers often focus on models developedfrom the conservation of momentum.The sameprinciplesare employedfor the mechanical engineeringcasestudiesto analyzethe transientupanddownmotion or vibrations of an automobile. Many of theseale relatedto the Big Four. I summarizes somemodels andassociated conservation lawsthatfigurepromrnently in engineering. petroleum.electrical enrploy engineering studies both currentandenergybalances to model electriccircuits.3 NUMERICAL METHODS COVERED IN THISBOOK 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 I. electrical.aerospace engineering hasmuch in cornmonwith mechanical engineering. chemical engineering skills are usedextensively in areassuchas environmental.rass flowing out.3 NUMERICAT METHODS COVERED IN THISBOOK We choseEuler's methodfor this introductorychapterbecause it is typical of many other classes of numerical methods.Justasflow mnstbalance at thejunction of pipes.and biornedicalengineering.Many chemical engineeringproblems involve mass balancesfor reactors. In essence.Figure 1.1.
Device OrganizingPrinciple Conservation of mass MathematicalExpression Mass balance: ffi Field Chemical engineering inort ff_* Over a unit of time period Amass:inputsoutputs ourpur Civil ig e n g in e e r n t structyz . R J . r*z i:R: f L\A7\J Around each loop I emf's .upward force Electrical engineering ilH3 Conservation of charge Current b a l a n c e :+ i ..l4 TABTEl. For each node I current (i) = 0 I'R' Conservation of energy Voltage balance: a{A&l . For eochcose.lhe conservotion low on whichthe bolonceis bosedis specified. \ . NUMERICAT METHODS.Y 2. 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 .1m77.l MATHEMATICAT MODELING.I voltage drops for resistors >6>a:0 . AND PROBLEM SOLVING Devices ond typesof bolonces fhotore commonly usedin ihe four moiororeosof engineering..\ I Conservation of momentum Forcebalance: +Fv Fn * + I O+ +FH ffi ?7fu.
6 Summory of thenumericol methods covered inthis book. r xt a r 2 x . ab =.r's a ' .yJAr FIGURE I. arrx..solve for the.r so thatfir) = 0 Optimization: Solvefor x so that/'(r) = 0 f(xl lbl Part 3: Linear algebraic equations Giventhe a's and the b's.x.Ii+r= ]'i+ "f(ti. = 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 : .h N:flt'Yl solve for r as a function of r . .3 NUMERICAL METHODS IN THISBOOK COVERED r5 lal Part 2: Roots and optimization Roots: Solve for.I. 1a.
rangingfrom the determinationof the centroidsof oddly shapedobjectsto the calculationof total quantities In addition. 1.Part Si.this involves the determinationof a function's slopeor its rate of change.6a. In particular. optimizarion involvesdetermininga valueor valuesof an independent variablethat correspond to a "best" or optirnalvalue of a function.The strategyin suchcases is to flt a curve directly throughthe datapoints and r. Examplesrangefrom populationforecasting (rateofchangeof population) of a talline body (rateofchangeofvelocity). Although somewhat different approaches are used.6e). Such is usually the casefor tabulatedinformation. satisfies a set of linear algebraic a set of valuesis soughtthat simultaneously equations. 1. the strategy the general matchingany individualpoints. in the sense that they are Such systerns are similar in spirit to l'ootsof equations concemedwith values that satisfy equations.6c'1.nurnerical integrationformulasplay an basedon setsof discretemeasurements. However. optimizationinvolvesidentifying maximir and minima. Integrationhas many applications in engineering and science. NUMERICAT METHODS. (Fig. As you know fiom your study of calculus.6a. Part Five is devoted to integlation and differentiation.rse the curve to predict the intermediate values.Thus.rical difr'erentiation.The categories: regression techniques developed for this pulposecanbe dividedinto two general in Part Four tFig.fterential This is bein all areirs of engineering and science. to fit curvesto datapoints. Such equations are of greatsignificance of a quantityratherthan in termsof the rateof change cause many physicallaws arecouched rnodels the magnitudeof the quantity itself.However.interpolutionis usedwherethe objectiveis to determineintermediate ues betweenrelativelyerrorfreedata points. electriccircuits. 1.6&). to tlreacceleration problems. in contrastto satistying a single equatiou. Suchequations arisein a varietyof problemcontextsand in all disciplinesof engineeriugand science.root location and optimizationboth typically arisein designcontexts.they originate in the mathenratical modeling of Jarge systems of interconnected elements suchas structures.t6 MATHEMATICAL MODELING. as in Fig.they are also encountered in other areasof numericalmethodssuchas curve titel q u u t i o n s . For thesesituations. l i n g l r n dd i f f e r e n t i a you will often haveoccasion As an engineer or scientist. 1. Part Five also covers methodsfor nume. AND PROBLEM SOLVING Part Two dealswith two relatedtopics:root finding and optimization.root locotiorrinvolvessearching for the zerosof a function. initialvalue Two typesof problemsareaddressed: andboundaryvalue . importtrntrole in the solution of diffbrentialequations.6d. 1. As described is a significantdegree with the data.In contrast.and fluid networks.As depictedin Fig. 1. linear algebraicequations Part Three is devotedto solving systemsof simultaneous (Fig. equations focuses Finally.Experimental resultsareoften of this of error associirted is to derivea singlecurve that represents kind. regression is ernployed wherethere and interpolation.x on the solutionof ordirro'vdi. As depicted in Fig.A plrysical interpretation of ruurrcricctl iltegratiott is tlre determinationof the area under a curve. trend of the datawithout necessarily valIn contrast.
811. I .PROBLEMS t7 PROBTEMS l. 8 .7 Thc amountof a uniformly distributedradioactivecontaminant containedin a closed reactor is measuredby its concentration c (becquerel/liter or Bq/L). r ( nr < dt / changc\ t . wherer'' (a) Usingcalculus.l \ n m a r s/ Use theconservation of cashto compute the balance on 6/ l.a rrass balancefbr the reactorcan be wntten as dc kt / decrease \ U l d e c a S \ / 5ir 2 2 41 3 6/l 327. The contentsare resupplied at a sinusoidalrate3Q sin2(t).l Usecalculus to velity that Eq. and9/1. If a secondjurnper hasa drag coefficient of 15kg/sand a massof 75 kg.3 3 1.14) can be written fbr this systcrnas d( Av'\ + AT : 3 0 s i n ' ( r ) O : a firstorderdrag coefTicient (kg/s). The concentration at /:0 is l0 Bq/L. assume thatthe chuteis instantaneously deployedso that thc jumps to 50 kg/s.8) contains a liquid at depth) where.5 s. how long will it take her to reach thesamevelocityjumpcr I rcachedin l0 s'l 1.r': 0 when the tank is half full. 3 1 9ll . (1. Thercfore.9) is a solutionof Eq( . (b) Repeat the numericalcalculation in Example 1.Computethc velocityto t: l2 s.1.6 For the fieefalling bungce jumper with linear drag (Prob.assume a first jumper is 70 kg and hasa drag coefficient of l2 kg/s.26 3 7 86 1 r 0 68 0 3 5 06 r 2t6B0 7/l Bit t 2 7.Intcrpretyour results. that is Decay rate : tc whereft is a constantwith units of day I.8).Perfbrm thc calculation from / : 0 to 20 s with a step size of I s.4).according to Eq. 1. ( 1. drag cocllicient / c h a n e ei n \  .4Rather thanthe nonlinear rclationship of Eq. Equation(1. Showeachstcpin thc computation. ls this a steadystate or a transientcomputation? 1.' l: \ votume / tinflowt . E m p l o y a s t e p s i z e of Ar:0.14). compule parachutist the velocityof a freetalling usrng Errler's methodfbr thc cascwhcre rr : 80 kg and c' : l0 kg/s.you mightchooseto model the upward fbrce on the bungee j u m p ca r sa l i n e a r e l a t i o n r h i p : rl f tt : C U (a) Use Euler's mcthod to solve this equationfrom t : 0 to I d w i t hk : 0 . l ll A storage tank (Fig. Liquid is withdrawn at a constantflow rate Q to meetdemands. 1. 2 d r .4).(outflow) F I G U RP EI .obtain the closedfbnn solution fbr thc where case thejurnperis initiallyat rcst (u : 0 at 1: 0). Use an initial conditionthat the parachutist has anupwardvclocity of 20 m/s at /: 0. Pl. (1.e.. Can you make any statement regarding the crrorsof thc calculationbasedon the results? 1.2 with the sameinitial condition and oarametervalues. (b) Plot the solution on a semilog graph (i.5kg/s fbr c'. I d. witha stepsize of (a) I and (b) 0. 1.2. The contaminant decrcases a1 a decay rate proportionalto its concentration. ln c versus/) and detenninethe slopc.5For the freetalling bungee jumper with linear drag (Prob.Use a value of 12.2The following infbrmation is availablc for a bank acc0unt: Dote Deposits Withdrowols Bolonce I 5 I 2 . At r: l0 s. 111.7).i" t:t.3Repeat Example1.
O : :3: _ : sinft) drAA Use Euler's method to solve for the depth _vfronrr : 0 to valuesareA : I0 d with a step size of 0.97 kg/krnol).and 0. suppose that the outflow is not constantbut rather dependson the depth. the gravitational lbrce decreasesas we lrove based on above sea level. AND PROBLEM SOTVING NUMERICAL by the nr the heat capacity.Assumethat the initial condition i s .7 18 kJ/(kg K).. kPa m]/(kmol K)].10 The volume flow rate through a pipe is given by Q : rA. r ) ' Skin Qr.9 For the same storagetank describedin Prob.4 literswhile exhalingover a onedayperiod.1 1. 1. 1. The parameter that the ini1200m2. 1.urine. Use volumecontinuity areain pipe3 of Fig.dr : nrc. y: 0 . In is 0.075 mr and gives out about 80 W of heat (l W = I J/s).0. = 6 m/s A:=? FIGURE PI.12 FigureP1.13 In our example of the fieefalling parachutist..35 litersthroughsweat.8.ou.One liter is ingested as food. Assurne tial condition is _r: 0. 1. For this case. rise during the first l5 minutes Calculatethe air temperature of the class if the room is completely sealedand insulated.Although this is a decentapproxinration when we are examining falling objectsnear the surtaceof the earth. attenda classin a room which l.05 liters while inhaling. due to gravity was a constant sumed that the acceleration value of 9.(rz The massof air can be obtainedliom the ideal sas larv: P7 Mwt nt Plt : a(ltr')r O .r8 MATHEMATICAL MODELING. we asl. METHODS.12depictsthe variousways in which an averilgeman gainsandloseswaterin oneday. V is lhe volume of the gas. = .Mwt is the molecularweight of the gas (1br air 28.and thc changein temperature followingrelationship: or.ll A group of 30 students lneasuresl0 m by 8 m by 3 m. and rRis the ideal gas constant[8. 1 .5 d.325kPa.31. sincethe surfaceareaA is constant r/t'O. Each studenttakesup about 0.(trA Use Euler's method to solve for the depth )' fiom t : 0 to valuesareA : 10 d with a step size of 0. To maintainsteady feces. respectively.4. P I .5 d.The body will also lose 0. and cv: 300. Note that by the air O is relatedto the massof the air the heatabsorbed where P is the gaspressure. and the body metabolicallyproduces0. the exchange 0. s i n .O:500 mr/d. A rnore general representation Newton's inversesquarelaw of gravitationalattractioncan be written as R: s ( x ) : g ( 0 ) ( l( + . and breathingair.8 m/s2. The parameter 1200m2 and p : 500 m3/d.and throughthe skin. how much water must be drunk per day? state.Ir': c. tbr air is 0.3 liters. whele u is the average velocity and A is the crossto solvefor the required sectional area.in= Qz'rt: 20 m3/s Food Drink u.the differential equationfbr depth can be written as dl' dt . 10.rr) o : .. Assume air is an ideal gas at 20 "C and 101.2. I 2 .2. Assume the heat capacityC.IO Metabolism F I G U RP EI .
$ . Use Euler's method to conrputethe volume of the droplet from I : 0 to l0 min using a step size of 0.000m where the initial velocity is 1400m/s upward.000m using a stepof 10. r : time (rnin). (b.l "i "i "'i b t'ili i... Suppose arnbienttemperature that a cup of coftee originally has a temperature of 68 'C.I6 ilr*?i=iI i iri:.r (in m) measured upward fronr thc earth'ssurfacetm/s2).O. lf 0. (1.r : 0. k : the proportionalityconstant(per minute).However.use the chain rule to express the differentialequationas a function of altituderatherthan time. : ths ("C).PROBLEMS where g(r) : gravitationalacceleration at altitude. l{ Suppose a ratethat is proportionalto its surfacearea. use a forcebalanceto derive a ditlerential equationfor velocity as a function of time that utilizes this more complete representation of gravitation.1 For the casewhere drag is negligible.determine the other flows. Recall that the chain rule is du dt du dr d.l5 Newton's law oicooling saysthat the temperature of a body changes at a rateproportionalto the differencebetween its temperature and that of the surrounding medium (the ambient temperature). (d) UseEuler's rnethodto obtain a numericalsolutionfrom r : 0 to 100.1mm/min andthat thedropletinitially hasa radiusof 3 mm. P1. : 2l "C and ft : 0.5min.gtO) : gravitational acceleration at the earth'ssurface(! 9.k ( T . andQo: 0.017/min.2.2.6. l.: 0.Compareyor.r dt r9 of your final computedvolume and verifying that it rs consistentwith the evaporation rate.4. dV :: dt kA where Z: the temperature of the body ("C). s.8). T : . Use Euler's method to computethe temperature from I : 0 to l0 min using a step sizeof I min if I.37x 106 and R : t h ec a r t h ' s radius mt.**L*i er:FIGURE PI. Assume that ft : 0. lbr this derivation. : 0... and A : surface area 1mmr).8 rn/sr). Assess the validity oi your resultsby determiningthe radius o.1 mr/s.Qt:O.rr result u ith the analytical solr"rtion.. rvhere V: volume 1mm3). k : the evapol'ation rate(mm/hr).16 Afluid is pumpedinto the networkshownin Fig. t : time (hr). and 7. . . that a sphericaldroplet of liquid evaporates at l..at.16.T " ) at (c) Use calculusto obtain the closed form solution where u = u. d . (= 6. assume that upward velocity is positive. (a) In a fashionsimilar to the derivationof Eq.
: a dragcoefl'icient the terminalvelocity.this equato computethe dragcoefficicnt tion can alsobe rearranged m8 ut (2.:cand 1oq1.piic:.l i. . ..p. how MATLAB's calculator Specificobjectives and topicscovered are ' . l. we usctlir firrcebalance to detcrnrine the tcrminalvelocityof a fiecfalling I ju o h j e c ll i k c i r h L r r r g c er n p c r . Learninghow to usc vectors a sinrplclinc plot basecl YOU'VE GOT A PROBLEM n Chap. Learninghow vectors and matrices .Asidefrom prcdicting and t'. where velocity(nr/s). (kg/m).4+'" .m : mass(kg). uE1 5t %I zl x+ = . I. and thc irs..r. 20 ffi=?N .thc priority rulcsfirr constructing mathernatical cxpressions. Fundomentols MATLAB 2.1 CHAPTER OBJECTIVES chapter is to providean introductiorr and overviewof The primaryobjective o1'this modeis usedto implcmentinteractive cornputations. I T tsi V t.l l'ttnctitlns.\ iiEi. to crcirtc on an equation. Llnderstanding undcrstanding ol'builtin lrrnctions and how you can lcarntnore 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 . the colon ope'rator'..t : ternrinal (m/s').f zr'+ 4_ %. ... Lcarninghow realancl cornplex nurrrbcrs areassigned to variables areassigned valuesusingsinrpleussignrnent.q: gravilational accelerertion r.
A B s k r p s a l i n c b c t w c c l l t  .. to MATLABisacomputerprogralxthatprovi<lcs..":ili: ff.t.ol.ws' ln chap' 3 we usc of thc c.rake In this chapter.*i.tring while it shoultlrcad oroficientist'oacttrallyinl."pt.Thatis.' bcingdisPlaYcd a s y o L lt y p c i n c o n t ll ltshitrn lrI a s c q u c n t i i t oper''rtcs o1'MATLAB nlode The calculator you can think of it as operyou get'rresult cotnurancl' each For line' by Lh::s' line mancls tn For exanlplc'if you typc calcttlator' ^ii"g f if. ^ .ILJ 8 b zt\ 4x AL ^r 4 ?t 5t !8 . n rr l n analyze r n : r l v . .'^ ::::::: :::):i:I.mmand anclgraphics we wilr r.varuaun comlrtnds.adtlititlnalinsightintosuchanalyses.ciscness.cise.ctilculaticrns. . a uery l'ancy _.B usesthreeprimary anddata' winclow'Uscdto entercommancls . .mitsllchbllnkljnc I ) r : r r ' 1 i( .i. r : ' 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 .(. winclow' wind.kg u.:.t way lrrcx c lbeconle .. typcs.r  a h e l ( l . if in Table2' I werecolThe clata estimatethe drag coeificient' tt. "' MAftAB progralns' to create can be usecl commands Chap.ml s Dotoforthemossondossociotedterminovelociliesofonumberofiumpers BO9 653 gll 929 721 6A2 5l l 836 4 7 7 5 4 557 la \ 509 (1.:il{. ztr ' s r rd clh how MATLAB canbe uscdto leartr will we chapter. ( ..] ) . ) l t n d t h c n u n l b c r .AB will disPlaY a.. :'. n o t c ' . Conrtnancl graphs' and plots display .Beyond lectu'l d ^ m .yott o1'youlcornpLrtcr.iumpers terminal the nleasure we Thus.. many perfornirrg * *' 1P. :.$1. i l L l r f r i : 1A u d ! .: ii' ffi:l "v s rn rot gp n ati crc u'T*.. ^ n ^ ^ ^ . m. ffi qA ' . we show how sr'rch to o n e f u r t h e r . 51 Lo thc rcsult' MATL.l *t' o provides equation orlhis purpose' tu.I TABTE m.ln .:ll..* windtlw' Usedkr and edit Mfilcs' creute tt'r gaii Usecl . . [ h i s c h a p t e r h a s b e e n w r i tThe t e n a s a l relficient a n c l s . 1s ct' io ' :r' r ' M A  l . 2.=. crcatcMiiles' prompt *iii ut. a r t. r r l .2r MAT4gllyllerynENl 2 r THE 2.llctlentthcctltntrratrclsonMATLABasyouprocccdthrotrghthe following material' witrclows: fUeifA.r inrple..1 of knclwnmass.th u s e r w i ta ha c o nnice v e l rtool ietrt en virontlrentfclr very iteprtlvides ln particular..::'# ":1 "'" ilJffill'.ttt. C.3. the edit window to openwith thc commancl tht tu'ntunti window will Afier startingMd. H( e rr. i t h e rr r t a n t co.'Ji:'. In this showinghowMATLABcanbeenlployedtocolnputeqtrantitieslikeclragcoetTicients'we willalsoillustratchowttsgraphicalcapabilitiesp. rute toope " "!i:H:i:iilll1l. u h .st fiont in .this velocityof a numberol .
) character.For example.1 Scolors The assignment of values to scalar variables is sirnilar to otherconlputer languages. This resultsin the storage of the valuesin the memorylocationcorresponding to the variable name. and if you usethe semicolon.2.f J _A zfTtr ='1. they will be displayed.t. they will not.2 ASSIGNMENT Assignment refersto assigning valuesto variablenames. twe short Iong short e long e long a eh^rt end 4 MATLAB treatsnames in a casesensitive rnannerthat is. 2. Thus. mand line with the semicolon Try typing >> A = 5.st 4 zl+ Z] X+? "L x jj€ z th r 15/ I . 2. enter >> g and then enter >>A long bank eng Seehow their valuesare distinct.22 MATLAB FUNDAMENTALS Notice that MATLAB has automaticallyassigned the answerto a variable.x = .1 . lt carrbe suppressed thecomby terminating (. You can type several commands on the sameline by separating them with comnlas or semicolons.A = 6.Thev are distinct names. >> a 4. To illustrate this. you could now use ans in a subseouent calculation: 'ars + II with the result 50 MATLAB assigns you do not explicitlyassignthe calculation the resultto ans whenever to a variableof your own choosing. Try typing 1 _ A Note how the assignment echoprintsto confirmwhat you havedone: 4 Echoprintingis a charactelistic of MATLAB. the nan)e a is not the same as the name a. If you separatethem with commas. frTTbl .ans.
1 4l 5 9 2 6 5 3 5 8 9 7 9 3 e + 0 0 0 3.__.0000i predefined pi. fbr example. Thereare several variables. r416 i.€ a.1 41592.since MATLAB handlescomplex arithmetic automatically. >> x = 2+1*4 2.__i .1416er000 3 .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 Sccried 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 Flooiing p 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 lo B e s lo f f i x e d o r f l o o f i n g . a complexvaluecan be assigned simply as in >> x = 2+i*4 2.They all have the syntax: f ormat typr.65]. + .:a..[h 4 el 5t I Z} X+ .589 97 1.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 r x e d . type >> format short The f<rllowing is a summary of the format commands you will employ routinely in engincering and.scientific calculations.0000 + 4.1415q1b5J58979 l.1415e+0tr0 3 . r4r6 precision.0000i It shouldbe notedthat MATLAB allows the symbol j to be usedto represent the unit imaginary numberfor input.. >pi 3 . I T a*!1.r416 3 . twe .However.it alwaysusesan i for display.141592b51 58979 To return to the four decimal version. Noticehow MATLAB displays fbur decinral places. If you desire additional enterthe fbllowing: >> format long Now whenpi is enteled the resultis displayed to l5 signilicant figures: >> pi 1.p o i n l 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 p 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 andTaglsforsingie f o 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 Engineerlng Engineen r 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 t i g i t so n d o p o w e r 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.0000 + 4..2. For example.2 ASSIGNMENT 23 We can assigncomplex valuesto variables. 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 :. "/J Consequently.The unit irnaginary number ir preassigned to the variable i.
8. When we speakof vectors. 6. lh r ?l 5t I Z] X+ . Vectors qnd Mqtrices An arrat' is a collection of values that are represented by a single variable name.2. we usuallyreter to column vectors.rple.  .1 areactuallya matrix with one low andone column. For example. lr23 456 7 8 9l ffi qA1. which are more contmonlyused. the Enterkey (carriage return)can be usedto separirte the rows.2. Onedincnsirnalarral't arc callcd tu'/ort and twodinensional anavs are c'allednrutnce:. by transposing a row vectorwith the ' operator.>A.24 MATLAB FUNDAMENTALS 2.For exan.. practice. I0 l 2. or _. A column vectorcan be entered in several ways. a row vectorcan be assisned as fbiiows: 123451 Note that this assignment overrides the previous assignment of a : 4.>b1.2 Arroys. zt + zf :5yst LIZ 4 a_J*= _r { "L x jl':vI A <. Bracketsare usedto enterarraysin the comnranilmode. in the followingcase. >> Lr =  2.Try them. 4 b S 1t) A rnatrixof values can be assigned as lblkrws: r'23 456 789 In addition.. In row vectorsare rarely usedto solve rnathematical problerns. 4 6 I 10 I' Tlre resultin all threecases will be b= 1. or. the 6 andthe I to assign the matrix: . the Enterkey woulclbe struckafterthe 3. The scalars usedin Section 2.
For cxarnplc.( j .rrc: b x or.tlt si 8 Foranarray.nZt* 5t s L <..l) 2. respectively. LrsinqJ 175 bytes Note that subscript notationcan be usedto access an individualelcmentof an array.whos f Jd r { i e A dns l) x(comfiLex) Clrancl total Sr ze lx3 I x5 LXL 5xi '1 xl Byte:: 12 40 8 4Ct I6 Class clouble cloubLa il. to creatca 2 x 3 matrixtl1'zeros: thc numberof columns.2. with moredetail.Lrray is 21 elenrrrnl.[h Y vI t('€ .s. E : 7 ..1) 6 Thsre are sevcralbuiltin functions that can be used1ocreatcmatrices.r. .cnterthc whos command: >. a list of all current variables by entering the can be obtained v.?'' 5l I Z] X+ .) J 00 UU Sintilarly.2 ASSIGNMENT 25 At any point in a session. the firurthelement o['thc columnvccfort) can be displayed as :> b(4) .r n) selectslheclemcntinmthrowandthcnthcolurnn. thc first tbr the nulnberof rows and the second tbr For example.Forexample.y tlouLr Lc: . . c I o s .. the ones llnd zero:i l'unctions creatcvcctol'sor matricesfilled with ones and zeros.2. A(:. the orresfLtnctioncan be used to create a row vector of oncs: u = o n c : : . Both havetwo argumcnts. ( 2 .hocommand: > > rvho Your yariabl al es ans . For exarnple.If a colon is used to separate tw'o nunrbers.MATLAB generales l h e m u s i n ua n lhe n u m b e r s bclween ffial z$4 zl' + zf Ll 2 i 'A I la . . .A(n .3 The Colon Operotor The colon opefatoris a powerfll tool fbr crertingand manipulating arrays.trrb l e doubLe array array i)i I'ay arr.
0000 2.2.5000 2.For example.BOOO 1. the second through the fourth elements are returned.the function alromatically generates 100points.0000 1. x 2 . Alsc Res 2. For example '> linspace(0.the secondrow of the matrix A can be selected as in >> A(2.26 MATLAB FUNDAMENTALS incrementof one: : l1. n) ).When a colon is usedin placeof a specific subscript. MAILAB generates the numbersbetweenthe first and third numbersusing an incrementequal to the secondnumber: >> t = L:0.0000 plv thes ing Th AS lf the n is omitted.basedon the previousdefinition of the vector t : > > ' 1 ( 2 : 4 ) 981 Thus.3 rnr 1.The 1 inspace functiongenerates points.It spaced a row vectorofequally spaced has the form I i n s p a c e (x l .0000 ope Note that negativeincrements can also be used >" f = i0:1:5 com t0 9 B 1 6 5 Aside from creatingseriesof numbers. n ) which generates n points betweenxl and x2. generates The logspace lunction a row vectorthatis logarithmically equallyspaced.For example. * J Zl eNst "L x vI tlh 2 3l ZI X+? =$8 .6000 o.J whi For I If colonsare usedto separate threenumbers.5000 3.( l.the colon can also be usedas a wildcard to select the individualrows and columnsof a matrix.5:3 f Ifn 2.4000 0. the colon reprcsents the entire row or column.4 The rlnspace ond losspace Functions provideotherhandytoolsto generate The iinspacre and logspace functions vectors of points. t ffi= ?l + Ll 2.:) We can also use the colon notation to selectivelyextract a seriesof elementsfrom within an array.6) 0 0. x2.2000 0. It has the form logspace(x1.1.
.3 MATHEMATICAL OPERATIONS with scalarquantitiesare handledin a straightforward Operations manner.3 MATHEMATICAL OPERATIONS 27 which generates n logarithmicallyequally spacedpoints betweendecadesl0'r and 10": For examp)e. zl'+ . >> y ^ 2. The common operators. 2.0000 If n is omitted.i. or simply displayed.It will be discussed in detail later in this book.0000 100. the priority order can be overridden with parentheses.2.45 0. 4) 0."1 lJ t  ffi= r\ a +  *6 l =:T { 5[ s\ __.832 Also. Parentheses can be usedto overridethe priorities as in >> Y = \4) L6 : Lefi division appliesto matrix algebra. Try . as in the nexttolast example. in order of priority.2 .. >> logspac e (I . are Exponenliolion Negotion Multiplicotion ond division 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.l 4 Z] X+ 6re %& <.2.it automaticallygenerates 50 points. [h .Pt/4.4 is first squared and then negated. For example.5533 Results of calculations can be assigned to a variable. As with other computer calculation. because exponentiation has higher priority then negation.similar to other computerlanguages..0000 10.r 2*pt 6 . as in the Iast example."L x . scalar real variables can be included: >> Y ..1000 1.. the following result would be obtained: >> Y = y4 ^ 2 _r6 Thus..
llrst redefine a and l:.: .2 0.0004 >>'1 /. ti 2 l. try >> a * A .00001 The real power of MATLAB is illustrated in its ability to carry out vectorrnatrix in detail in Chap.2000r ans 12. Herearesomeexamples thatusethe valuesof x (2 + 4i) andv ( I 6) definedpreviously: '>. :.sJ 5t 3$& <. lB. 8.0000i 0. The irtner product of two vectors (dot product) can be calculated using the * operator.d*b r10 and likewise. Z ffir qAb zl' & Z uN x LJt ffi A i x+ z_! 1J'€. 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 introducing sorne of those manipulations here. l0 t6 4?. b * allS  2 4 6 8 10 4 8 12 16 2t) b 12 18 24 l0 8 16 24 3'2 4L) I0 20 30 4t) 50 To further illustrate vectormatrix mLrltiplication.[h .0000r an!.O0tJ0 + 4..0000 >> x + y +1r. and >> b = Now.1011 0 >> x ^ . . the outer pnrluct >:. f4 5 tjl'.'3 * x ans 5.2a MATTAB FUNDAMENTAIS Calculations can alsoinvolvecomplexquantities. it is worth c a l c u l a t i o n sA .:.< +I2.
MATLAB provides for that too.82 0.t\€ l z_lh I .The MATLAB manualcalls thesearray operatlons. Matrixrnatrix multiplication is carried out in likewise fashion: >> A * A 30 66 102 35 B1 126 42 96 150 Mixcd operations with scalarsare also possible: >> A/pi ans = 0. r49 16 49 ffi z\4 .9f 5l . Here is what happens when the dimensions are not those required by the operations. 9tr 1 50 results in matrix multiplication of a with itself.^ 2 2a 36 64 81 ^ The . you will want to carry out calculations item by item in a matrix or vector. precedingthe operatorsignifiesthat the operationis to be carriedout elementby element. For example. lh x _T .9099 2.2132 ?.3 MATHEMATICAL OPERATIONS 29 >>A*b 32 71 r22 Matrices cannot be multiplied if the inner dimensions are unequal. Try >> A * a MATLAB automatically displays the error message: ??? ETrar using => mtimes Inner matrix dimensi.2.8648 We must always remember that MATLAB will apply the simple arithmetic operators in vectormatrix fashion if possible.3183 'I .(h Zl. What if you want to square eachelement of e? That can be donewith >> anS A = ."I LJ ' 2.5366 I..6 4?.. 30 66 r02 35 81 t2.9549 1.5915 2. They are also often referred hycIeme Loas element n t t tparutit tn.s.?.At times.\46\ 0..ons must aQfree.2.gt st l "L 4 <.
sin. >> A . For example. f anh.0986 r. you can go back to any command that you entered. a eys\ 1 l "L 4 <' . such as sqr t.9459 0. of X. 3 1821 64 I25 343 5r2 216 129 Using the uparrow key. such as exponential and square root. You can useonline help to find out nroreaboutthem. have matrix definitions also. abs. type '> help elfun One of their importantproperties of MATLAB's builtin functionsis that they will operatedirectly on vectorand r.to the matrix A. element by elenrent. For example. of X the is elements posrtive.". You shouldget back the last line you typed in. MATLAB will evaluate the matrix version when the letter m is appended to zf+ .0194 1. But you can also edit this line. if LOGM. not LOG(X) Complex See also results LOG2. 2. acos.try >> log (A) 0 1. The uparrow shortcutis a quick way to fix errorswithouthavingto retypethe entireline. Most functions.6931 1.[h Y ll'€ .79'LB 2. Certain functions.4 USEOF BUIIT.eJ 5t & Z] X+ . and exp. LOG10. the natural are lograrithm produced EXP. >> A ."1 {z '\ ffi= qA+. operate in anay fashion.changeit to the line below and then pressEnter.30 MATLAB FUNDAMENTALS MATLAB containsa helpful shortcutfor performingcalculations that you've already done.6094 2.^ 2 PressingEnter will perform the calculation again. For example.if you want to learnaboutthe 1og function.Pressthe uparrowkey. type rn >> helP LOG log' lrlatural is logarithm.1912 and you will see that the natural logarithm function is applied in array style. For a list of all the elementary functions.you can type b and pressthe uparrow once and it will automaticallybring up the last commandbeginningwith the letter b. Press the uparrow until you get back the line F *  Alternatively.3863 1.natrix quantities.INFUNCTIONS MATLAB and its Toolboxes havea rich collection of builtinfunctions.
0385i + 0. lh .9503 .max(F). it should be pretty obvious what's happeningwith the following commands: >> min(F).2515 + 0.4 USE OF BUILTIN the function name.f\€ .2068i 1. suppose that we enter a vector: >> E = l1.4 I.9f >l 4 l & . For example.6 1.AB42i I.5 I.594A1 0.FUNCTIONS 2.1611i 0.5526 + 0.Try >> sqr:tm(A) 3l A.4 1.A22Bi 1. >> sum(F) 19 In a similar way. >> F = l3 5 4 6 Il.21I7i  There are several functions for rounding.0.5 I.6l.prod(F).3481l 0.1623i 1.sort (F) 1 6 3. the sumfunctionreturns the sum of the elements: arrays.0. The round function rounds the elements of E to the nearest integers: >> round(E) 221r22 The ceil (short fbr ceiling) function rounds to the nearestintegerstoward infinity: (E) 1 >> ceil 1 The f loor >> 1 2 2 2 function rounds down to the nearestintegers toward minus infinity: ( F t) f loor 22 2rl There are also functionsthat perform specialactionson the elementsof matricesand For example.4844 2.3134 0.4498 + 0.mean(F).0185 + O.5813 .8000 360 13456 ffi Z} x T<.5555 I.
crl = 0. (1.42r4 50.r \V trt / due to gravity(9.6175 51.1871 51. and r is time (s). jumper canbe computed that the velocityof a freefalling bungee with [Eq. in 68.a n h ( s q r t ( g * c c l/ n ) * t ) i t) 1B.r118 42 .( g * n / c ' d ) * 1 . \ Th bo [0:2:20]' Yor Assignvaluesto the parameters: >:.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.5 G gft M. a f o r m u l as u c ha s r : .32 FUNDAMENTALS MATTAB Recall A common useof functionsis to evaluatea fornrula for a seriesof arguments.01 62 46 .'t292 33. 1 ( r ) .r1 \ Z] X+? .is the drag coefficient(kg/m).9)]: 2.[h zl'+ .and the result is assigned to a corresponding tion in the u array.sJ 5t <.81 m/s:). .1.n is mass(kg). .l/ r ..81. / a t a n h.9 = 9.4560 51. values a columnvectort that contains from 0 to 20 in steps of 2: Create . where u is velocity(m/s).5823 br. / : s q L t . : .951 a 49 .re r array.25. w h e r et h e f b r r n u l 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 is posicomputedfbr each value of tl.6416 ( ( I ffir zl Llz 7f 5[ st { l L vI x j_f€ .= t.For our case.'> l.r I V c.g is the acceleration c.
.2.5 GRAPHICS For example.ef >l 3$& .r' ffi z\4+ ..' l t l '!ry l I i l I a o l 6 : 30 20 10 0 : i i j I . : "t* l I i I l 8 10 12 Valuesof t 14 16 18 ?.€./) lne gfapn appears ln tne grapnlcs wrnoow ano can De pnnteo or translerreo vra tne clrpboard to other programs. > y l . 60 50 40 30 20 10 0 8 10 1214 16 18 You can customize suchas the following: the grapha bit with commands >> :':. nlol (t.J ^:.5 GRAPHICS 33 2. L b c l Plot of u versus f 60 50 40 : .. ".. enter above.:'> qrid til. lh 4 l.f.' xlabel .to createa MMLAB allows graphsto be createdquickly and conveniently.re('Plot of ('Valucs ('Valucs v of ol ver':jus L') v') t') .. I i ! .t. data the graphof the t and v arraysfrom :."lqfjeTl zl a* z. x+ l <. .
symbols.r.. Colors Bl.1ol. n.) >> plot ln contrast. \r.1 5t a. .: st 7i 5[ 7l Lt r Jl J'l  ffi aAI zt\ x v! rf € . .ruto displaymorethanonedataset on the sameprot.o. ho1c1 off In additionto ho1.:2 Specifiers for colors. lLoLd on (t.l. pJ This cornrnand breaks the graphwindow into an mbynmatrixof small axes. if we had typed the tbilowing cornmands.by clefault.) MATLAB allowsy.34 MATLAB FUNDAMENTALS TAB E 2. :'' pIoL (1. if you want [o connect eachclata rnarker wirh a straight rineyou courdrype :'i.[h Z} X+ . The hol d c. 'o') ::. yt t.nconrmancl hoidsthecurrcntplot ancl all axis propertiesso that additional graphingcommancls can be addedto the existingplot.l:jlot (t..tit of f cornmand returnsto the def'ault mocre. For example. another hanrlyfunctionis subplor. If you wantto plot eachpointwith a symbol' you can includea specifierenclosed in singlequotesin the pior functio. . zt'+ =. if you want to useopencirclesenter r:. Fclrexampre. . >(t. ond linetypes. v. previous plotsareerased everytirnethe plor command is implernented.For example. . Table2'2 Iiststhe available specifiers. (t. the flnal plot woukl only displaysyrnbols.o. Symbols Poinl Circle 'MO TK Line Types Solid Dolied Dcshdot Doshed m v k Pu l s Slcr Squore Diomond Triong e(dovrn) Triongle{up) Triongie(leftJ Tr iongle{right) Penlogronr nexogfcrn The 1rlot.the f<lilowingcommandswould resurt in both lines and symborsbeing displayed: ':' '. which alJows you lo split the graphwindow into subwindows ()rp(ute.) v. PlotploL .and selects the pth axesfor the currentplot.o') It shouldbe Inentioned that.I. The h. (L.r) v.conlmand clisplays a solidline by default.re Green Red Cyon A^agenio Yello'"v Blccl. It hasthe svntax subploL (n.
eJ 5t 3$8 <. 2. p 1 o t ( s j n ( L ) . The simplest manifestation of this capability is the p1or3 command which has the syntax p1ot3 \x."I LJ ' ffi=b 5t(st 1t l "L Y vI _T j} € .irle (' (a) ') As in Fig. 2 . The resultis a line in threedimensional space throughthe pointswhosecoordinates arethe elements of x.= 0:pi/50:10*pi. > > s u b p l o r ( 1 .la. let's grapha circle with the twodimensional plot function using the parametric representation: r: sin(t) cos(r).5 GRAPHICS 35 We can demonstrate subplot by examiningMATLAB's capabilityto generate threedimensionalplots. FIGURE 2. z) wherex. I ) .y We employ the subplot commandso we can subsequently add the threedimensionalplot.2. and. First. lh Z] X+ . Note that the circle would have been distorted if we had not used the axis sou. y. c o s ( r ) ) >> axis squaLe >> r. >> l.the result is a circle. anrJ z. Plotting a helix providesa nice exampleto illustrateits utility.er e command. y.I A twoponeplotof (o)o twodimensionol circle helix ond (b)o threedimensionoJ (b) z['+ . and z arethreevectors of the samelength. y.
to MAILAB (e. p 1 o t 3 ( s i n ( L ) .y coordinatessketchout the circumference plot.'t irhn anclMATLAB will displayall ret'erences that includethe word loq.AB capabilities. support as as a variety ffiar z[ Ll 2 =al' l_t zl'+ L1 r r 5y 5t "L x 4 a.it is obviouslynot a comprehensive in learning rnore.a varietyof replots. The net resultis the characteristic cally as the : coordinate of the helix. let's add the helix to the graph'sright pane. 2 .plottingon the complexplane. c o s ( 1 .kt .g.[h zl .corn. uselitl well ofother resources.As described to learnaboutthese as well as otherMATL. 2. product newsgroups. books. L ).and contourplots. it prclvides rnodeby typing the in interactive As described in this chiLpter.mathworks Finally.. The resultis shownin Fig. help is also available or function.rri thnr. ) . simultaneously. springor spiralstaircase shape of graphicsthat are usefulfbr example.Fol example. . 2 0 0 7 ) .)' '> subpLot( . the curverisesvertisamefashionas the twodimensional increases linearlywith time.36 MATLAB FUNDAMENTALS Now. However..r and .Hanselman of the excellent booksdevoted 2 0 0 5 :a n dM o o r e .rres of lines. lnc. the package window.. Can you visualize of the circle in the rr' plane in the the .As such.you can use the lookfor commandto searchthe suppose thatyou wantto find all of text. solll.x+ 1. and Littlefield. MATLAB Help files lbr occurrences you couldenter and functions that relateto logarithrns. . fbllowedby the narne o1'acommand lrclp conrmand If you do not know the narne.and technical There you will l'ind links to infbrmation.threedimensional meshplots. of ditl'erent optionsfbr exploringand searching detnos. 2 ) .ces areavailable 2.I .Palrn.. websiteat www. by Help lacility thatcan be accessed itsell'includes an extensive Further. andu : / metl'icrepresentation: x: sin(/).1 5t =$8 .familiesol curvesplots.you can obtainhelp tionr The MathWorks.) >> rirle('(b) what'sgoingon?As time evolves.2005.6 RESOURCES OTHER ol'MATLAB that we will be using The fbregoingwas designed to lbcus on thosef'eatures overviewof all in the remainder of this book. access to a numberof instructive dition.s will provideyou with a nutnber clicking on the Help nrenuin the comnrand In adthloughMATLAB's Help material.loglogor semilog instead next.you shouldconsultone If you are interested o1'MATLAB's capabilities. we againemploy a para: cos(1).l b.Thi. the cornrnands l. .To do this.plotting obiects Thele ale other f'eatr.€.r ..
: l a l r L ( g * r l t r t r . . . 0. z(r Ll 2 .1 alongwith gravitational acceleration .0. Becausewe are performing operations on vectors. 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. canbe entered as The datafrom Table2. (l ( a ill '. we will use MATLAB's graphicalcapabilitiesto discernpatternsin the data.. engineersand scientheserelationships by collectingand analyzingtheir own data. Thus. most cases.we must include periodsprior to the operators: elementbyelement (r.l 91.1 'rl.'rrt::l!1 !.3125 of the Now. beyond merely computing the drag coefficient.(lL'r '. r. However. 'q  r'clnt. renowned scientistsand engineers. However.we will illustratehow MAILAB facilitatessuch exploratorydata analysis. prior to arriving at a final predictive equaand developingplots..i ] ..7 CASE STUDY 37 EXPLORATORY DATA ANALYSIS Your textbooks are filled with formulas developed in the past by Bockground.fl1.1.. i i r = t i ri r r ( . tists often must supplement Sometimesthis leads to a new forrnula. trjrr1.l llr.2854 with a rangettom 0. n .i !.il') l).l 8t:1. 1( r'rl ) . i Li.tt..9 . 9).We will do this by estimatingthe drag coefficientof a freefalling human based on Eq. :. ..25l l to 0. /!. Solution.o f.. .1 v l ) ] .r :.91. we usually "play" with the data by per{ormingcalculations hiddenin the data.eJ 5t ?$ Z] X+? I .4 rlclrrr i rr i) l i .e[r + tr4 \ e[ st 2{ x z [h /_l€ . .1 4l.1.l . our intent is to gain insight into the pattemsand mechanisms In this casestudy. ' a l. 1 . 9. (2. .1).. '.1 line) to help assess ffi= qA+.ll.. J12! kg/m. The drag coefficientscan then be computedwith Eq. J .. (2 l) to makea prediction drag: teuninal velocity basedon the average '. superimpose a line indicating exact predictions(the l. r 1). . l).iii.r .) f " 1i.We will also the results. .Although theseare of great utility.1 . the averagevalue is 0.')t) lr) for the some statistics We can now use someof MATLAB's builtin functionsto cenerate results: .1 l) .1 .ll 7. .1 !0.l r ..1.)/ 4'1.1l'r'r.5.4 4J. l . r ' " ') Vpl t r:Ci 'rlt.tr:. (2.1) and the data from Table 2..9 (tir. 1.2. let's start to play with this data by using Eq.ll1r'. 1 r 7 q = 1 1 r ( ' .. ln tion.
r( k q r ' t ' .R:50'Lj 2. . 2. .. you the predictions As in the top plot of Fig.2. r i r e I ( ' n r . q t ) g . l wherer:time.. : . t g r : t .2 The standard norrr curve that c bellshaped . . ' . 2 60 65 70 80 75 Mass (kg) 85 95 PROBTE 2.35 o)F o<. L e 1 ' .1 A simple electricc pacitor. r : r f . ) ' . ) . notice how the model tends to underpredictthe low velocitiesand overpredictthe high. .) q(t): Lrurar(l l. L i r l . the drag coefficient seems to be increasing as the mass of the jumper Use MATLAB to ger z: 4 to 4. t : .2' suggeststhat rather than being constant. 1 ( . v l . theremight be a trendin the dragcoefficients.l ( ' l r .l ' .:rlic lL'tl') 'l'l ol ol I i t lc] ( I)t ('(l i ( t.Y oo r!o U. i 1 1 l rf li l .2. . generatea plot of this fi 4o:12. r l L l r . i ' l : rl. . 1 ) .l] r o l r i I i .O I c 1 L .) The resulting plot.crd generallyfollow the l: I line. y r l o L( 2 . Labelth scissa asz.rlrt'I 2. and an inductor on the capacitor4(l) as as ( " / t . ( ' .' : : 1 i t n a l ' t . that ratherthanbeing constant. t! o. ' 1 . [ ' l ' I i c t l l r l ( ] i q / 1 l l) L i 1 . .. v p r ( .[h rtE:l 5 I Z] X+? . . which is the bottom graph in Fig.J q) o O o o c. This suggests drag coefficientsversusmass: This can be seenby plotting the estimated x l . i c t l L \ " r r : . ' l .qo:tl and ( L: inductance.2 FIGURE withMATIAB Twoolols creoted Becausewe are going to eventually generatea secondplot. p l o t x I a b . we employ the subplot command: .However. L r r i : c l)' I i.R t / ( 2s 1 ('Irr i. i .because might initially concludethat the averagedrag coefficientyields decentresults.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.) 0 . ' ' : : u l . ffi= llAb zl'+ : : "L x vI <.
. I . 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.[h 1l€.2) where C.L : 5 . 2. This hypothesis could be substantisuggests the frontal areas atedby rneasuring of individualsof varying masses.you might concludethat your model needsto be improved.2Thestandard norrnalprobabilitydensitylirnction is a bellshaped as curvethat can be rcprescntcd l/ . a capacitor. that heavierjumpersmight have largerareas. Labcl thc ordinateas lrcquencyand the abscissa as:. which is the areaprojectedon a plane normal to the direction of the velocity. ] .PROBLEMS 39 continued increases. a n d C : l 0 r .. whcre I = time.eJ 5t 8.r7o 1l : the rcsistancc.p : tir density (kg/mr).. t t ( b )  : 1 r n s l r .. : a dimensionless drag coefficient. I = inductance.0 Io 0. you would discoverthat the parameter c.1.] : thc initial chargc. givcn that q o . T'hcchargc andan induc(or onthecapacitor ri(t) as a lirnctionol'limc can bc cor.5 Il'a lirrcc I(N) is applicdto conrprcss a s p r i n g . with a larger numberof At the least. .r (rn) can ol'tcn bc rnodclcd by Hookc's law: Ij:lr Inductor It\/ \ ! /  . [ ) .2) good assumption if thejumpersall took off from the sameheighton the sameday).I ffir qA+. is dcpictcdin Fig. In addition. r c ' { Ir l .nputcd as 2 .r : l i r n c t i o n : ( a ) r ' : I i r s l ) .:. Z] X+ . generate r plot o1'this lirnctionliorn / . P2. zt'+ .=.i t s d i s placcmcnt. f U s c t h c I i r : r p . : 5 0 . Basedon this result.0 . and A : frontal area (m2). r c e ( r .1A simplcelcctlic circuit consisting ol' a rcsistur. 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 : .l.1 to.it rnight motivateyou to conductfurther experiments jumpers to confirm your preliminaryfinding.If you did this. the result might also stimulateyou to go to the fluid mechanicsliteratureand leam more aboutthe science of drag. PROBTEMS 2.7.F. FIGURE P2."1 {t 6 e[st l l x <.1 2. Usc MATLAB to and C : capacitancc. Assuming that the densitieswere relatively constantduring data collection (a pretty (2.q.1 2R .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. I I ) 2. :' \/ llt Resistor UseMATLAB to gencrate a plot ol' this fbnction liom .4 Usc colon notution lo crcatc vectors idcntical to the (#)' .
1.r! 51 .13 You conractthe jur Table 2.(usingdiantondsymbols)and thc llnction (usirrga dottcd cocfl'icient. 2.t1.2711ttt')x+t rt \B+2H/ Use subplor to genera (. >> r (nr/s).clocity S: channel (nr). 2. Use MATLAB to creatca plot o1'thcsine(solid linc) along (dashed line) up ttr andinr. >>Y=Bx (b) rt q = 4:2:L0. l i n e ) .and /1 : dcpth(rn).m2 o45A O4OlO F..r5 .l) in the bottompane 2. The potentialenergy storedin the spring U (J) can then be computedas U: (.v. Crcatea plot ol'p vcrsus 't5 7A 3BO N t 2 2 A 8 3 0 1 450 I t .8 . Explaintlrc rcsults. Usc this l'unctionto plot the data ancl l'unctionas dcscribcdirr Prob. 5 5 0 6 r 0 : 519(7'F 32). B I 0. (b) Determine the averag resulting values.P l o tt h c l u n c t i o n l i r r u : 0 t o 9 ( )n r / s . Ii: yu.r:c ll l.1() Hcrc is sorncwirrd tunncl data lirr lirrcc (I) vcrsus (r'): velocity LlseMATLAB to storeF and.00r.rct1)r ial in conrputing the scriesexpansion. . PPm t0 4 41 2A 26 3C t6 40 t3 50 i0 60 c5 A.r'') +9l one Storethesevaluesin a matrix whereeachrow reprcscnts oi'thc channels one of the paranrandcachcolumn represents ctcrs. lirnction cxccpt that a logarithrnic scale is used lirr thc l axis.. th r .020 0 0002 2.srnI nt . 0 3 0 4A 50 60 7A B0 hascdon the cubic lbrmula._ 4. n: roughncss LJsr'MATLAB to crcatc a plot displayingboth thc data whercU: r. 5[st :: jlg . n5BH lirr both thc. which are order conespondingvaluesin T Five springsare testcdand the following data compiled: t.9 The senii 1o9i.P l o tt h c l u n c t i o n 2. cosl .14 The following para that contractsexponentia .l 1 5t I Z] X+? ..1/ both the data Usc MATLAB to crci\tc a plot displayirrg (rrsing squarcsymbols)and the function (using a dotted lbr r: 0 to 75 rnin. >> x ^ 3. m l s 1 0 ? .I=t+ . (c) Develop a sidebysi C.:. I" : 0.5622 l0 + i (a) If the air density is p compute values of tl cD. . slopc. versusrr (right sic and titles on the plots 2..vith scries cxpansion a plot o1'thc cluding the terni r7171. the abscissa +fi6qrlal 4t z['+* 7l Llz *2f j zl 4+ " a.t as vectorsand thcn conrputc vcctorsof the spring constants and the potentialenergics.Thc lirllo'.1 of 3.r2 l 2.g4.r ancl . Rccallthat T( 2. .ll Thc loc_{locr lirnclionopcratcs in an idcnticalI'ashion (hatlogarithnric scalcs to the lirnction cxccpt arc uscd fil()'. 2. 2.y) in the top pane and (x.6 The density canhe computed asa lunction oi treshwater ol'tcmperaturc with the firllowinecubiccquation: p: x9 l 0 .Explain thc resu lt s. Usc whcre p : dcnsity (g/cnr31 and 16' : tcmperat.1.7 Manning's cquation canbc uscdto compulcthc vclocity 01'wirtcr in a rectanglllar opcn channcl: This clata can bc describcdhy thc lirllowingl'ur.r ing datais availablc lbr I'ivechannels: l i n e ) .12 Thc Maclaurinscrics cxpansion tirr thc sinc is 00r5 0 030 a a22 oo0to 0 0008 0 0c103 l9 '24 t5 t5 3 '25 . min c.v.1 and measure values. Use thc max lunction to dcterminethe maxirnumpotential cncruy.r : O to 3tr 12.5 0 1 x 5.2.17 B 4.8 f ( l .r7 71 .N ll r. .v=e .40 FUNDAMENTALS MATLAB 2. I0. Usc the Lruiltin tirnctionf .r'l .99987 x 6.Convcltthrsvccthe'n tor tclclcgrecs Celsius ancl computca vcclorof dcrrsitics i"1. Sln.r'axcs. 3 >> sum(q) * r(2.r. u .15 Exactly what will t MATLAB commandsare I (a)>>x=z.8 lt is general practice in enginceringand sciencc that equationsbe plotted as lines and discretedata as syrnbols.528 6 to'67<? s 11 0.Usc this lunctionto plot thc dau 0 035 0 0001 t0 2 and lunctionas dcsclihcdin Proh.6 oF.Writc a singleline MATLAB statcment to conputc a column vectorcontaining the vclocities based on the virlues in the parameter matrix..rr 0013 )2 0020 l5 9 0C09 0010 t2 0012 by the lbllowing function: This datacan bc dcscribed (. Make the rangeof li'onr. ({) \'elsustimc (l) for the Here is somcdatafbr conccntration photodegradation of aqueous bromine: P where k : the spring constant(N/m). ('C). lunctiont)pcratcs in an idcntical fashion (basc10) to thc frlot.rction: BH \t' /s/ .urc rangingliorn MATLAB to gencrate a vcctor of tcrnpcratures 'F usingincrcrncnts 32 "F to 82.
.1 (or tieratc(s r).COs.r:e commancl./ (b)tq . Use MATLAB to r._! u=4 3$8 <. cD. : Q (a) Il the air densityis p : 1. g = gravitationalacceleration ( : 9 .PROBTEMS 2.r : horizontal distance (m). : initial angle (radians).Use to the difl'erent axis liibels C.13You contactthe jumpers used to generatethe data in Table2.u .15Exactlywhal will be displaycd alier thc lirllowing MATLABcourmands arc typcd'l (a) '> x :':' X' >.rr to gcneratca twodimcnsional (r. r. l.314J/(rnolc . r(2.rnploy a distances generrlcl hclix horizontal equllirrrrs 2. :) in thc bottom panc. >> r > > : i L l l r ( c 1 )* zt + . : 0 to 100m in incremcnts m. 1: 2 : 1 0 .t t'tlutrtittn.16 The trajecbry of an object can be modeledas R. line plot ol' i') in the top pane and a threedirncnsional (. and the seconddimension (c) Dcvelop plot ofA versus rr (lcft sidc)and a sidebyside (columns)corresponds initial angles.1 and measure their frontal areas. Enrploythc semi l. lh . and (b) logr{) ft crater sidcbyside vcrsus l/7. valucsol reactionratcs1brtemUsc MATLAB to gcncrate peralurrs ranginu lrom 273 to 333 K. l. bc ctrrnprrtetl linc plot ot' Usesubpl.17 The lcmperature wilh theArrhaniu.:30 nr/slbr initial angles rangingliom l5 to 75o in and valuesof the dimensionless drag coefficicrrt compute Employ a rangeof horizontal distances increrncnts of 1.versus rr (right side). Interpret .. f] I 4. d... your rcsults.r. Thc resultsshould from t of 5 (b) Deternrine minimunrand rnaxirlumol'thc the average. . Usc subplot to gc'ngraphol'(a) /<vcrsusI. 2. and scale legencl to di.225kg/mr. which are ordered in thc same sequenceas the conesponding valucs in Table 2. are A.. A c o r n p o u n d h a s El :x l O s J / m o aln d A : 7 x 1 0 1 6 . : i n i t i a lv e l o c i t y( m / s ) .gascontemperaturc stant [8.5". use MATLAB to height. IU. ) 6 3) 2..R .a n d y . 8 1 m / s r ) .. : absolute ( K ) . .stinguish r'xponentially as it cvolvcs that contriicts thc plot so that thc minirnumheightis zero usingthe azis o l ' s i n I .E: activation energy(J/mol). lbr eachof the initial angles.lncludedescriptivc a singlc plot of thc hcightsversus this matrix to gcncrate andtitlcson the plots.rqy functionto crcatc(b). E.nt2 0. ):(tando).The resulting values.A : thc prccxponential wherck: rcaction quency)lactor..11The lbllowing paramctric among the dillcrent cases.Aa t'itRI.jF \ ffi= z\4+ zl : F+ "L x z. K)]. : i n i t i a l find the displacement fbr r'. 2.d0 4l where _v: height (m).. .\r'*)t. in an array where thc first dimension(rows) be assernblcd resulting values.454 0 401 0 453 0 485 0 532 a 474 A 4B6 2.). lt x k . \'=c CosI reactions dcpendence ol'chcmical can 2. corresponds to the distances. and l.
. 3. 9 = 9.[h lj€ eJ 5 8.25.However. m = 68. rn. ffi zl'+ At zf Lt.81. L . t { l _ 5[ st ^l L zJ x <. Mfile.The scriptcan be executed by typing the file name in the commandwindow or by invokingthe nrenuselections in the edit window: Debug. However. They are useful for retaininga seriesof commandsthat you want to executeon more than one occasion. bungee File. I MFIEs The most common way to operateMATLAB is by enteringcommandsone at a time in the command window. Mfiles provide an alternative way of performing operationsthat greatly expand MATLAB's problemsolvingcapabilities. many small stepsmust be taken. 3. This chapterwill introduceyou to how MATLAB Mfiles can be usedto obtainsuchsolutions.3.l.I MFITES 43 We also learnedthat a numericalsolutionof this eouationcould be obtainedwith Euler's method: Ui+l: ui + dut dt Lt This equation can be implementedrepeatedlyto compute velocity as a function of time.6175 just as if you had typedeachof its linesin the comntand Thus.m extension.the scriptexecutes winclow.I S c r i p tF i l e Problem Stotement.Type in the fbllowSolution. So our problem now is to figure out how to do this. suchcalculations can be performedeasily. v = sqrt(g * m / cd) * tanh(sqrt(g * cd / m) * L) Savethe file as scriptdemo .Note that the nomenclature fact that suchfiles are storedwith a . EXAMPTE 3. zf x+ .with the aid of MATLAB.An Mfile containsa seriesof "Mflle" comesliom the statements that can be run all at once.Returnto the command window and typc >>scriptdemo The result will be displayedas 50.1. Developa script lile to computethe velocity of the lreefalling jumper.New. Openthe editorwith the menuselection: junrperat a specifictime ing statenrents to compute the velocityof the fieetallingbungee ( 1 .12. Mfiles comein lwo flavors:scrintfiles and functionfiles. to obtain good accuracy.This would be extremelylaboriousand time consumingto implementby hand. 9 ) ] : [recalE l q. cd = 0. t ScriptFiles A scriptfile is merely a seriesof MMLAB comnrands that are savedon a file.Run.
though MATLAB is casesensitivc.elocitiz conputes c:f an cd) rviLh the objecL sjecondorder frcefaL1 wiLh drag velocity drag r. l.2 r u n c l t o nr i l e ProblemStotement.the first line ol'the he/pcorrurrcnts. bungee '.v'al ue.r/uer srare.commadelirnited argt)st = the function'sargument = tcxt thatprovides regarding the ?reTpcornnerts tlreuserwith infbrmation the function.l % v=freefallrrel(L. f uncname your computer'soperatingsystem may not be.m.44 PROGRAMMING WITHMATLAB As a final step.cd) second*order lir_.(L. is the line that is searched you shoulcl wclrds related to the file on this line.t) Z he)pconme.determinethe value of s by typing >>g 9...m. In contrast Hencethey are analogous to userdeflned and returnoutputs. [h r af xh VT jjg:l 5t 3$8 .rr= funclanlc ( .e.nLs s L. into valuesthat are passed list (i.Thus.l.. window asillustrated Note thateven in thecommand in the fbllowingexarnple. the command As we will seein the following section. scripts between a u df u n c t i o n s .it retainsits value back in this is an important disworkspace.1.but now we will usea functiorr file for the task. : r . can acceptinput arguments languages VisualBasicor C. tinction Files 3. .2 Function to scriptfiles. Solution. the Hl line. generally as The syntaxfor the functionfile can be represe'nted funcLion outr'.l t emell L. As in Example3.2. m. compufe bungee the velocityof the freefalling jumper. two diff'erentvariables. narnes l i 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 Whereas M A T L A B w o u l dt r e a tf u n c t i o n your operatingsystemmight not.furcr.and can be invokedby typing t1elp f uncnane in the cornmand function(lhese thitt is assigned to ourvar. includekey descriptive The functioncan thenbe run by typing The Mflle shouldbe savedas furrcnarne. EXAMPLE 3. Beyondits role in describing by the lookf or command(recallSec.6).s auLva r. where outvar'= the name of the output variable. ffi l = l ^l zl 4+ zt\ jy !t'i 5t ?JLJ' zl'+ "L Y a.g l Ji ^ . window).they FunctionJiles areMfiles thatstartwith thewclrdf unction. BlOO So you can seethat even though q was definedwithin the script..rme = the function's name.mLlLs= MATLAB statements called the function. functionsin programming suchas Fortran. Type the following statements in the file eclitor: f rrnction v = % f reef al1vel: Z input: f reef aLLvel. thatcompute the r.
.'=freefallvel(t. 1. 100. Suppose that you wanted to compute the velocity of a 100kg jumper after 8 s: >> freefallvel (8. 0. return to the command window \12. m : % f reef al1: bunqee velocity with secondorder clr:agt Note that. if we had typed >>g zr+ ztfai3'" \ ffi= z$4+ = "L vI 3l F+ <. v = sert(g of ? acceleration * m / cd)*tanh(sqrt(g gravity * cd. 68.6i75 One advantage of a firnction Mfile is that it can be invoked repeatedly for diff'erent argument values..I MFILES % r = rime (s) (ks) m=mass % 2 cd = secondorder drag coef ficrent % output: (m/s ) % v = dovrnward velocity 9 = 9.lh Y jtE ?l 5t I . you tbrgot the narne of thjs functiorr.3.r1lve1: bungee velocity with secondorder drag .m. 0. .25) The result will be displayed as 50. you could enter >> lookforbungee and the following information would be displayed f r e e f a l .1 . but remembered that it involved bungeejumping./ 45 (kSi m) m) * t). To invoke the function. at the end of the previous example.. Save the file as f reef al lvel and type in >> freefallvel .81.ccl) compuLes the free fall veloclty of an object with secondorder i nput.p freefallveL which results in the comments being displayed freef.: t = time (s) m = nass (kS) (kglm) cd = secondorder drag coefficient ourpu[: (m/s) v = downward velocrty dr:ag If at a later date.187B To invoke the help comments type >. hel.2t) 53. m.
46 WITHMATLAB PROGRAMMING would have beendisplayed the following message ??? Undefined function or variable 'S' . cd) (g * cd / m) * t). cd). sqr t (sum( (xmean) . we like to includethem whcn t 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 .In suchcases. stdev .the following function. stdev] = stats (x) n = length(x). Function Mfiles can retum more than one l'esult.the Mfile in Example3. r t l al.5 4l. zt Ll 2 jt l l el' st zl'+ LA z.z cd) *tanh(sqrt end I Note that althoughend statements are t. it would not have a value in the As noted previously at the end of Example 3. f unction = veL (t.1.rptional in singlefunction Mlilcs.?l 5t 3$8 Z] X+? "Y ZN <. in a singleMfile.^2/ (n1) ) ) .J Lt€ . s u b f u n c t i o na sr 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 se t w c e n ffi qA+. 3.In contrast. Hence. the variablescontaining the resultsare commadelimited and enclosedin brackets. So even thoughs had a value of 9.we will ofien refer to function Mfiles as simply Mflles. functionMflles will be our prirnaryprogramming tool fbr the remainderof this book.81 within the Mfile.the variablesin a script retain their existenceafter the script is executed.2 they rnay also be contained (without conrments) could have been split into two functionsand saved as a single Mfile': v rr = f reef al Lsubf unc (L. v = sqrt m . a r . Here is an example of how it can be applied: '> !' lB 5 1D 12' 6 1..2 m= 7.th .5000 S= 2 .For example. 3.1.m. m.873'1 Because scriptMfiles have limited utility.staf s. n r.The variableswithin a function are said to be local and are erasedafter the function is executed. mean = sum(x) /n. this is an important command workspace. computes the meanand the standard deviirtion of a vector: function Imean. distinction betweenfunctions and scripts. For example. ccl) end function n 9 v (q = vel * (t. . Functions can call otherfirnctions. m.3 Subfunctions Althoughsuchfunctionscan exist as separate Mfiles.
and then returnsthe If the userenters a value.For example. This functionprovides a handyway to displaya value.s .To do this. zt+ '"1 r ffiE\ z\4+ zl x+ zf\ x : 4 l T .3.it would thenbe assigned to the variable n.lts syntaxis .if we atternpt to run the subfunction vel.ve1) are referred to as subfunctions.Its applicationis illustratedin the following example. the useris prompted with the message Mass (kg): waits for keyboard input.2 INPUT.. .t€ .r = input \'promptstring') The lunctiondisplaysIhepronptstring.Function.25) 50. name = input ( 'Enter your natne: ' .!. For example.25) command/ funct ion 've1 ' ?? ? Undefined 3.1.OUTPUT 47 This Mfile would be savedas f reefallsubfunc.2: . 68. an . an errormessage occurs: >> vel(I2. 1. information is passed into the functionvia the argument list and is output via the function'sname. the first functionis called the main or primary function.ti.Its syntaxis disp(rzaZue) where yalue = the valueyort would like to display.OUTPUT As in Section 3.1. Al1 the otherfunctions(in this case. If we run f reef al lsubf unc from the commandwindow the result is identicalto Example3.Two otherfunctions provideways to enterand displayinformationdirectlyusingthe command window. is appendedto the function's argunlentlist. l ' r r l F u n c t i o n . It is the only function that is accessible to the command window and otherfunctionsand scripts.6175 However. The inpur funclion can also returnuserinput as a string. In suchcases. m = input ('Mass (kS)r ') When this line is executed. value fiom the keyboard.68.or a string message enclosed in hyphens. [h . 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. 0.2 INPUT. ) The . A subtunctionis only accessible to the main firnction and other subfunctionswithin the Mfile in which it resides.ef >t I <. If can be a numericconstant or variable.m.'> freefa]lsubfunc (12. T h e r r .s.
r J ffii: :.To irrvoke the function. % acceleration of grraviLy 9 .6175. 6 1 ' 15 (kg/m) : 0 .: t is a string specifying how you want the value of the variable x to be displayed. The operation of this function is best illustrated by examples. Type the fbllowing statements runct ion free[. . To display the value using eight digits with four digits to the right of the decimal point along with a message..plays the labels until it detectsone of the symbols: %or \.compute the velocityof the freefalling bungee jumper. but now usethe inprut and disp functionsfor input/output. Velocit.48 PROGRAMMI NG WITHMATLAB EXAMPLE 3. In our example. it first encounters a % and recognizes that the following text is a format code.sJ 5t I a. [h . cd = input ('Drag coefficient ' ) . 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. (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 . A simple representation of its syntax ts fprinLf ('formaL'.2.2 5 T h e t 1 . L: % f reef a1li: int. As in Table 3. the format codes allow you to specify whether numeric values are ffi zl' + zl Llz . in the file editor: Solution. > < . B1. .eracti're bungee velocity inter. m. . ( l < g ): ' ) .1 . .\'I N Y+ vI jl € .For instance. velocity) The velocit.F i l eF u n c t i o n ProblemStqtement.9./s): 5 0 .active computatjon of the free fal1 rrelocity of an object \ r itt h s e c o n d . ) where form. m = input('Mass (kqlm) : ') . r r r r l 1 F u n c t i o n .suppose that the variable velocity has a value of 50..t l "L x . A sirrrpleexample would be to display a value along with a message.6175 mr's This example should make it clear how the format string works.lr (nr.l Drag coeffrcierrt Time (s): 12.4f n/s\n'.7 i s 50. return to the command window and type >> f reef .the statement along with the resulting output would be >> fprinLf ('T'he velocity is %8.rl I i Mcrss (kg): 6g.3 An Interoctive M . MATLAB starts at the left end of the string and di:.o r : d e r d r a g . As in Example3.
It can also be usedto display vectorsand matrices. x.I.400 12 .700 120.6 r7.100.5e\n'.7 L2a. a I x <.3f\n'.6AA 17.^.the comrnand prompt would appearatthe end of the labelm/s ratherthan on the next line as would typicallybe desired.4 12./5 "'_.14159e+000 .2 INPUT. Thesevectors arethencombined into a matrix.lh rf €. MATLAB continuesdisplayingthe character information(in our casethe units: m/ s) until it detectsthe symbol \. >> fprintf 100 ('%5d %10. As in Table 3.800 88.283 3. The fprintf functioncan alsobe usedto displayseveral valuesper line with diff'erent formats.After displaying the valueof veLociry. 2A.yl j  fprintf fprintf (' y\n' ) x ('%5d %10. This tells MATLAB that the following text is a control code. If we had omittedthe code \n in the previousexample.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 .pi) 6.OUTPUT TABTE 3. .the control codesprovidea meansto perfbrm actionssuchas skippingto the next line.400 z(\ Ll 2 zl  ffi qAb :I . The resultof runningthis Mllle is >> f pr:int X 1 2 3 4 5 f de rno Y 20.Here is an Mfile that enterstwo setsof valuesas vectors.2*pi.1 e[st :> .which is thendisas a tablewith headines: Dlaved fprrnt fdemo function '2 x = lI I 4 5l.3. Descripiion l n l e g e rl o r m c i Scientiflc f o r m o fw l t h l o w e r c o s e e Scientic f f o r m o tw i t h u p p e r c o s e 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 in integer.4l.sJ 5t I z[+ 'A Z} Fh . decimal. or scientific format.B BB.For example. z) .3f %8.
mar that holds the variablesyarl through varn.all the workspace variablesaresaved. m = B Ot . [h r j. a zl Llt . Such files are createdand accessed with the save and load commands.50 PROGRAMMING WITHMATLAB 3. For example.213]'. >> v=sqrt (g"m .you can recoverthem by entering >> load veldrag T m n a Now. called a MATfi\e.28 . a simpleapproach is to createtext files written in the widely accessible ASCII format.?l 5t & Z] X+ . removeall variablesfrom the workspace with the clear command.*tanh(sqrt(g*cdlm) *t) . The simplestapproachinvolves a special type of binary file. ( 1.In suchcases.mat. all the variablesare retrieved. varrl which retrieves the variables rzar1 throughvarn from f ilenante. the velocitiesare availableas can be verified by typing >> who Your cdv variables are: 3 TI Although MATfiles are q./cd) . >> clear p F At this point. 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..2._r _l ffi= qA+ i 5[ sl "L x z. B 1 .rite useful when working exclusivelywithin the MATLAB environment. if the variablesare omitted. I Creoting ond AccessingFiles MATLAB has the capability to both read and write data files.261 .. varn This commandcreates a MATfile named fitename.J <.= 5 .supposethat you use Eq.9) to generatevelocitiesfor a set of drag coefficients: > > 9 = 9 . zl h . if you tried to display the velocitiesyou would ger rhe result: >> v u al ??? Undefined function or variable 'v' 3.245 . The l oad commandcan subsequently be usedto retrievethe file: load fr lename varl var2 .a somewhatdifferent approachis requiredwhen interfacingMATLAB with otherprograms... As was the case with save. A simple representation of its syntaxis save fifename varT var2 .25 . The save commandcan be usedto generate a MATfile holding eitherthe entire workspaceor a few selected variables. If the variables are omitted. >> cd=[.3 ! CI However.l'€. which is expresslydesignedtbr implementationwithin MATLAB.
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.you will see 7 .9f 5l .txt) The foregoing material covers but a small portion of MATLAB's file managementcapabilities. As an exercise. a handy import wizard can be invoked with the menLrselections: the import wizards convenienceby File.you matrix of values.f\€ l 3l x+ & . 3. ' i>: = l9 simpmaLr ix 5192 zF. For example.' ' .. 0 00 0 0 0 0 e + 0 0 9 . I n a d d i t i o n .For example. This structureallows you to executea set of statements conditionis true.MATLAB creates namedafter the fi l ename. The looping of flow to allow statements to be repeated."I LJ ' zl'+ J 9t" 5\ "L 4 <.just the to storethe numbers append For file can be irccessed by other proglams such as spreadsheets or word processors.0000000e+000 6. mentsare executedline by line startingat the top of the function and moving down to the is highly limiting. 3. example. 0 0 0 0O0 O e + 0 0 0 2 . you could use the load command as a function and assign its values to a variable as in >> A = loeid(simpmatrix. ascii simpmatrix..the save commandstoresthe valuesin A in 8digit ASCII form. lmport Doto.txt a doubleprecision array Because simpmaLrix. in doubleprecision. In contrastto MAlfiles where you might want to savethe entire workspace.0000000e+000 3. the program stateThe simplestof all Mfiles perform instructionssequentially. If you want ascii doubLe.you can read the valuesback into MATLAB with the load command.Its general syntaxis if condition statemenfs ffi z\4 . . 0 0 0O O 0 O e + O 0 O 3.3 STRUCTUREDPROGRAMMING That is.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 . In eithercase. t x t . would typically savea singlerectangular >> A=15 I >> save 9 2.PROGRAMMING 3.3. of tlowbased Loolts (or Repetition). >> load sirnpmatrix.0000000e+000 Alternatively. Decision s (or Selection). all computerlanguages include stateend.txt In this case. Because a strict sequence paths.irptrt. These programs to take nonsequential can be classified as mentsallowing .3 STRUCTURED 5l in MATLAB by appending ascii to the save comASCII files can be generated mand. if you openthis file with a text editor.1 Decisions if a logical The i r Siructure.0000000e+000 9.3 6 3 9l. 0 0 0 00 0 O e + 0 0 0 5 . Alternatively. The branching on a decision. lh x 4 . txt is not a MATfile.
52 PROGRAMMING WITHMATLAB wherecondr tron is a logicalexpression that is eithertrueor false.lh tl l zl x+ : 4 Lrt9 .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 . . An example of its r"rse would be where we might want to terminate an Mfile to avoid a division by zero.rdc'). encountered' ) .nent is irnplemented.. cnd If a nonzero argument is used. the multiline if structure is usually preferable because it is easier to read. djsp('passing gr. the division would be implemented successfully as in >> errotLest(l0) 0. The fbllowing Mfile illr"rstrates how this could be done: function f = errortest(x) 11 1 == 0 . it is ofien convenient to implenrent the i f structure as a single line.. if grade > 60.6) passingr grade For cases where only one statementis executed.For example. ??? ErroL usit'tg ==> error:test zero value encountered ffi= zr+ '"1 "L x <.it displays the text message. This involves using the cr:ror: function which has the syntax.9n 5 I .l i n ei f i s t o e m p l o y i t f o r r u d i rnentary error trapping. er ror (msgr) When this firnction is encountered. E r r o r F u n c t i o n . A n i c e e x a m p l eo r l ' t h e u t i l i t y o f a s i n g l e . end This structure is called a singlelirrc rf For cases where rnore than one statel.rsEr and causesthe Mtile to terminate and return to the conrmand window. er ror (' 'zcrc:value f = I/x. hereis a simpleMtile to evaluate whethera gradeis passing: Iuncrion % grader % gldder(g oder (grade ) : determines whether = numericalmessage qrade value is of passing grade (0100 ) % input: r" grade % output: % if drsplayed grade >= 50 di sp ( 'pass rrrg grrade ' ) end The following illustratesthe result >> glader(95.
PROGRAMMING 3. * (And).Just as fbr lor eachof theseoperators.ion is true. there is a priority order lbr evalualing arithmeticoperations.. Highesr ryx x&y Lowesl xly I I F F T T T F "L x 4 <Ah 11. :'io"1 i on!r evaluateto true. TABTE3. If either or both express.€ 5t 8. . if the expressjon is false. employed the loo of thetoble.Used to performa logicaldisjunction on two expressions.the resultis false. variables. relaL i.3 for logicoloperotors the possible outcomes A truthtoblesummorizing is shownol Theorderof priorityof ihe operotors in MATLAB. FXp/ ps.S. Usedto perlbnn a logicalconjunction on two expressions. can be constants. If both e)<pre.lNot). If the expre.1p7 essr.:= 3/3 r: >= 0 Logicol Conditions.2. MATLAB alsoallowstesting of morethanone logicalcondition by cmployinglogical We will ernphasize the fbllowing: operators.sr.3 STRUCTURED TABTE3. the resultis true.9 .'on I PxLrt r. . . If eitheror both expresto false.Usedto perfbrmlogicalnegation on an expression. outcomes all possible lhble 3.r d pxp/  .ssrorsevaluate to true.3 summarizes logical These operations. wherethe yaf ues orexpressions and the relaLion is one of Table the relationaloperatorslisted in 3.exp ress r o. +. the result is true. srons evaluates (Or).rOn . the resultis true.the resultis false. The simplest form of the condr Lron is a singlerelational expresfwo valuesas in sion that compares vaf ue.'.Converscly.2 Exomple unit = a< 0 s>t 53 operoiors in MATLAB Summory of relotionol Operotor Relotionship Equol Not equcl Lesslhon G r e o l e rl h o n Less t h o no r e q u o l l o G r e o i e rl h o n o r e q u o l t o 'r 3.t . ..on vaf ue.
..' 'd') F in priority order.s\ l ^l "L x 4 <. > .This type of structureoften occurswhen we have more than problem setting..a special form of decision two options lbr a particular It hasthe Seneral the if. x a * b > 0 & b == 2 & x > 7  .54 PROGRAMMING WITHMATLAB are from highest to lowest: . Let's investigate how the computeremploysthe prioritiesto evaluatea logical expres=t. . z( Ll 2 7[e1.parenthesescan be used to override the priority order.1.r't::.hashighest At this point. a and I .substitute the valuesfor the variables: * I 2 > 0 & 2 =_ 2 & 1 > 7  _(.d ) To make it easierto evaluate.andy='b'. evaluateall the relationalexpressions 2 > A & 2 = 2 & I > I T F & & tr 1 ('b' l. statements. Finally..'(y > . else Stdae'nlenLS. 3. if ffi= qA+. condition. . example. 2 > A & 2 == 2 t''I > 1 ('b' > 'd') Next. b .ti. Since there are two. end EXAMPLE 3..eJ 5t 3$8 zl'+ LA _\ zl /+? .the I is evaluated a s e to f s t a t e m e n itfs a l l o w sy o u t o e x e c u t e The j r .f a = .d') The first thing thatMMLAB doesis to evaluate expressions. Itofienhappensthatthetalseoptiz structureis anotherdecision. e l s e The ir . elseif conditron. syntax structure. ln this any mathematical thereis only one: 7 x 2.rltStructure.Its general syntaxis if r:ondjtion stat€ment:.{. Structure.[h ll'€.Sincethe .4 or nn oi f . In choosingbetweenoperatorsof equal priority. .. MATLAB evaluatesthem from left to right.2 . efseif hasbeendeveloped.evaluatewhetherthefollowingistrueorfalse: s i o nI . the logical operators are evaluated (r) is evaluated priority. the lefttoright rule is appliedand (p r r) is evaluated: the first expression F&FIT priority The r againhashighest FIT in Fig.1 .the lastexpression first to give F&T&F T The a operatoris evaluatednext.For such cases. statements.The entireprocess is depicted Finally. as with arithmetic operators.. as true. This stlucture a logical condition is true and to executea secondset if the condition is firlse.b. .
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 \.f\t . 0. For a scalar.4 i t Structures Problem Stotement.PROGRAMMING 3.. zt+ ... Here's a MATLAB session that illustrateshow it works.3 STRUCTURED 55 a*b >u& 1^.  Z d. zt\ aY Z] x+ 4 l _T .l5. statementsr else statements.the builtin MATLAB sign function returnsthe sign of its argument(... a[h I .. >> sign (25.. elseif condition.! ffi qA+. end EXAMPLE 3..I.6) 1 >> sign (4.776) 1 >> sign(0) 0 Develop an Mfile to perform the same function."1 { 9l s\ zl I < o .ef 3$ 8. I ).. .Y# F & I I  _('b' >'d') Fl t \YJ I I Evaluate relational expressions F T Fl T + I Evaluate compound expressions T FIGURE 3...I A slep'by'srep evollotion o{ o conrplexdecision...
il{#ff: er' "L x 4 zl xh %8.An rf . To partially remedy this shortcoming. an if. . end This function can be run as >> mysign(0. an i f structurecan be usedto return 1 if the argumentis positive: function if end x sgn > = 0 1. nothing is displayed. if it is run with a negative or zero argument. For example. .6) 1 Although the function handles positive numbers correctly.aro. The function now handlesall possiblecases. elseif case: this final function sgn = mysign(x) 1 if returns % mysign(x) x x is is is gtreater less eqal to than than zero. x < O elseif sgn = 1. . % mysign(x) This function can be run as >> mysign(25. O if x 7. ^t ^. 1 is erroneously structurecan be used to incorporate returned if azero argument is used.56 WITHMATLAB PROGRAMMING Solution. >> mysign(0) 0 ffi!3r qA+. . end zero. z z lf x > 0 sgn = 1. _  _t. ^. else sgn = O. First. . returns % mysign(x) I if x is less than or equal Z if x > 0 sgn = 1.9 r ! to zero. sqn = nYSiQIn(x) returns 1 if x is gTreater than 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.176) 1 Although the positive and negative cases are now handled properly.
hereis functionthatdisplays a message depending on the valueof the gra de. the message Vorioble Argument List.9 coefficient..Therefbre. Dependingon its value. .an optionalblock is implemented if the expression takeson noneof the prescribed values. y a .diocre' ) D disp disp('Whoops') 'F' case drsp ( 'Wou1d like ) fries wj th your order?' ) otherw]se disp(.they might not have a good idea of an appropriate dr.c hS t r u c t u r e .€ . e 1 s e i f However. l  z.it would be nice to have the program supply a value if they omitted it from the argumentlist. In addition. .. As an example. T h e s w i t c h s t r u c t u r e i s s i m i l a ri n s p i r i tt o t h e i f .It can be usedin conjunctionwith decisionstructures like ffi . MATLAB allowsa variable numberof arguments to be passed to a function. case staIe. we developed a function freef a 1 1ve i. oInerwrse s I a a emel] f . value of a single test expression.It has the generalsyntax sr.sf r. end As an example. MATLAB hasa functioncallednarsin that provides the numberof input arguments suppliedto a function by a user.ratherthan testingindividual conditions.the branchingis basedon the structure..f.Tlris featurecan come in handy for incorporating defaultvaluesinto your functions.r rr:i.. gracle svrifch case drsp case case L r15ri = 'R'. .T LJ ' b b Z N x 3!h .menrs.1u e .3 STRUCTURED 57 T h e s r v i t . which had threearguments: v = freefallvel (t. I on "ctaaemencs.lluht end "Good" would be displayed.J .different blocks of code are implemented.recall that earlierin this chapter.sol lr.9f 5l la^. When this codewas executed.ricch case I .PROGRAMMING 3. stringvariable.xprcss r z a Ju e . A d{oult vulueis a numberthat is automatically assigned in the eventthat the userdoesnot passit to a function.cd) Although a userwould obviously needto specifythe time and mass. qracle: 'A' ( 'Excellent' 'B' ) clisp('Good') 'C' ( 'Mc.m.
m.l) 48 .r Must >> ??? Must enter f reef al. .ng mass () ltrofallvol and mass (12 ) f reefallvl ??'.68.75 >> freefallvel ( 12./ of gravity * cd 9. m.lvel Error enter *sing time EXAMPLE 3.25) 50. cd) computes freefa11 the of an object with v=freefallvel secondorder % input: Z L = time (s) %m=mass(ks) (default = 0.81.  .5 when it is invoked in the command Note that narsin behaves a little diff'erently window.6B. A .depending showingthe results: window session >'> f reef allvc. how this can be done for fr^eefallvel: The following code illustrates function ? % v = freefalLvelt : (t.58 PROGRAMMING WITHMATLAB into to incorporatedefault valuesas well as error messages the if or switch constructs your functions.fl1fu= \ <.Here is a command on the numberof arguments default. For example.61.21 cd = drag coefficient % % outpul: (mrs) v = downward velocity % swi t ch nargrin case O enter trme and mass') error('Must ^^ a kg/m) error case cd end A = v ( 'Must en1er nass ' ) 2 = 0. cd) drag velocity drag.:l 5 I . l (12.21 . freefallvef with secondorder bungee vefocity (t.sqrt(g cd)*Lanh(sqrt(g / m) * t). % acceleration * m. or set the Notice how we have used a switch structureto either display error messages passed by the user. In the commandwindow. it must include a string argumentspecifyingthe funcin the function. tion and it returnsthe numberof arguments >> nargin ('freefallvel' ) ffir zt'h" "L x zJ 7) F+ il'€ . Err(. = .[h .I.81 41 >> freefallvel us.
Developan Mfile to compute the factorial. o r . 5. MMLAB would displaythe classic 10 .9l 4 t >l & . 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.the f .Its g e n e r as l yntax is f o r i n d e x . The process continues until greaterthan the f ini sh value. . For example. stepsizesof A .the s t ep can be dropped. 2 . A for loop endsafter a specifiednumber of repetitions . start.2 0!: I ll I I v ) ') 2 Note that MATLAB has a builrin function f acLoriat that oerformsthis comnutation zl' .1uteme'. 4. In other words.s t a r t : s t e D : . 5 . 4. l. the loop terminates the index becomes as the programskipsdown to the line immediately following the end statement.3. loops perform operations repetitively.f\€ .inj sh is lessthanthe start and the loop terminates when the index is lessthan the f inish. U s i n g o 1 . the index variableis increased by the srep and the programloopsbackup to the for statement. for end i = 1:5 djsp(i) When this executes.There are two types of loops. 1. dependingon how the repetitionsare terminated.3.5 . The programthencompares the rndex with a desired final value.3 STRUCTURED PROGRAMMING 59 3. the programexecutes the sLatemenrs. It doesnot haveto be an integer.I . a r ea l l a c c e p t a b l e . Thus. A whi I e loop endson the basisof a logical condition.fin jsh. E X A M P L3 E. the loop's logic is reversed.the default step is 1.the loop will "countdown"in reverse. The size of the srep can be changedfrom the defaultof 1 to any other numeric value. For suchcases. A f or loop repeats statements a specificnumberof times. MATLAB would displayin succession.f i n i s h ". ."1 {t & ffi= z$4 b I st 71et ^ z I x+ / lh ZI\ x _T<.3. nor doesit haveto be positive. For example.2. B. The ror . When it reaches the end line that marks the end of the loop. If the index is lessthanor equalto the frnish. The rndex is a variable that is set at an initial value. . 1 . encl Structure. Note that if an incrementof 1 is desired(asis ofien the case). . If a negativestep is used.rs The f or loop operatesas follows. 5. 2 .2 Loops As the name implies. for i = l0: disp rj r end 1:1 "countdown"sequence: When this executes. 9 . 6. 1. For example.At this point.
for Vectorizotion. = x.it may not be obvious how to vectorizethe code. Preollocotion of Memory. of5l (rneaning5factorialo and we the f or loop would not execute.vectorizotion numberof times. a specific the most efficient meansto repeatstatements MATLAB.r'.02:50. all Lhe integiers from I to n' I I for x end fout end i = l:n = x * i.This can becometime consunringwhen you perform actions suchas addingnew valuesone at a time within a loop.1 "L x <.{ Z] X+ . For this case. = cos(t). it is not necessarily directly on arrays. Il x 2 x 3 x 4 x 5 : 1 2 0 ) .That said. = cos(r).Because of MATLAB's ability to operate providesa much rnoreefficient option.the filllowing f or structure: i for r end n. vectorization is recommended. which can be run as >> f actor: (5 ) 12a x will hold a value This loop will execute5 tirnes(fiom I to 5). hereis somecodethat T Ir sl SI fiT=fiar qA+ zf Ll L A e[st . t. Ih r VT jl'€ sJ 5r I zl'+ . if n : 0. For exarnple. MATLAB automaticallyincreases the size of arays every tirne you add a new element. The for loop is easy to implement and understand. .0! : I However. At the end of the process. y(i) can be represented in vectorizedform as t y = 0:0. Notice what happens would get the desiredresult.= 0:0. For example.42:5C = i + l. It shouldbe noted that for morc complex code.60 WITHMATLAB PROGRAMMING 3l:lx2x3:6 4l:lx2x3x4:24 5!:1x2x3x4x5:120 Solution.wherever possible. A simple function to implement this calculation can be developed as fout = factor (n) function ( n ) : % factor Computes the producc of % t.
l.o. a s[sl Ll 2 { L 4 x <. the anay is onry sized once.s end The statemencs betweenthe whiLe anclthe end are repeatedas long as the condr jon t is t r u e .01:5.3 STRUCTURED PROGRAMMING 6t setsvalue of elementsofy dependingon whether or not valuesof t are greaterthan one: L U:.eJ 5t %8 af x+? . tt er. In addition.UI:).0:.3. general syntax is vrh.. . iun.. The follow_ ing code preallocates the proper amount oi ". which also enhancesefficiency.Structure. preailocation herps reduce memory frag_ mentation. Y(i) else r f y(j) end end For this case.Iir(i). the resultis E 5 2 1 T h e r r ' i l r : ' ' ' 1 r r r .tion.rnert.h e 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. Although suchstruduresarecurrenrlynor availablein MATLAB.tte.: ' rs k t r u c t u r e ' A l t h o u g ht h ew h i l e s t r u c t u r ie s e x t r e m e ru y s e f u rt. 1. lh lj€ .hrL/ F^LOr t ( i ) >l = I/t(r). e^ndirian st.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. l I l:lerg.y by using a vectorizecl statement to assignonesto y prior to enteringthe loop.oneq(si:e(t)). Its while disp encl x > 0 (x) When this cocle is run. y .ii. encj end Thus.hLt L:. for i = l:lengLh(t) rf r (i )>l y(i) ."" be mimicked WTTbl zl zt + tA\ aA+. The r.MATLAB rnustresizey every time a new value is determined.
Otherwise it would becorne a socalledinfinite loop that would neverstop.. break..[h .or posttest. Sucha structure If the problem requiredit. at the very end and creAlternatively. middle. Every decision loop must havesucha mechanism. plots involves A nice example creatinga sequence of that a userrnight wanf to leisurely peruse befbremoving on to the next. with statements is calledarnicltest loop. . Fortran 90 and Visual Basic to favor this structureover ofher forms of the decision looo suchas the conventional v.can be written as r.. work togetherto measure elapsedtime.The following codeemploysa f or loop to createa sequence plots that can be viewed in this manner: of interesting for n = 3:10 mesh (magic (n) ) pause end The pause canalsobe formulated aspause (n ).. l.terminates execution of the loop. < (. (1) x w <  0.62 PROGRAMMING WITHMATTAB by a specialversionof the while loop.or end)dictates who developed It is fhis simplicity that led the computerscientists nrid. break. . beforeand atier it).the break can be placedin the middle of the loop (i.hile structure.. break statement ate a posttest lo)P. we couldalsoplacethe i f .'rXtutements wherebreak.vhile if (1) conditron. The syntaxof this version..An exampleis while If . a singleline i f is usedto exit the loop if the conditionteststrue. break. Thereare often timeswhen you might want a programto temporarily halt.That is. in fact. we could placethe break at the very beginningto createa prel(. This featurecan be demonstrated by irnplementingit in conjunction with severalother usefulMATLAB functions. 4 1+%q'tr zt+ zl .The beep commandcauses the computerto emit a beep sound. whetherwe havea pre. in which casethe procedure will halt fbr n seconds.Two other functions.Note that as shown. all threestructures ing on wherewe put the exit (beginning.. dependIt shouldbe clear that.r. .).N17 =L x j]\€ . The 1.The commandpause causes a procedure to stopand wait until any key is hit.t ic and toc. ffiat  .. end end so that Notice how 5 is subtracted from x on eachiteration. Commond..st loop. whrle if end x 3 (1) q. end EXAMPLE 3 are really the same.?l 5t I <.Thus. break structure.called a while.This represents a rnechanism the loop eventually terminates.'.e. end stafements .
4 NESTING AND INDENTATION We needto understand that structures can be "nested"within eachother.. the commandscan be quite useful. [h Z] Xh . o nq u a d r o o t s ( a . Further. 3.rlgn for designingan algorithm to compute the roots.u n c t i . if you ever have the urge to use the command pause ( inf ). In such cases. 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.4 NESTING ANDINDENTATION 63 The tic command saves the cuffent time that toc later employs to display the elapsed time.f i l e s .006306 seconds.This involvesdeveloping the generalstructure without detailsand then refining the algorithm.the Ctrl*C or Ctrl+Break key combinations comein real handyin the eventthatyou inadvertently create an infiniteloop i n o n eo f y o u r M .we first recognize that depending on whetherthe parameter (e. 6 N e s t i n gS t r u c t u r e s ProblemStqtement. the computer will beep.g. Although the foregoing examplesmight seema tad frivolous. E X A M P L3 E.Nc.1':.sllrrc refers to placingstructures within other structures.we will eitherhave"special"cases or convenusingthe quadratic fbrmula. you can return to the command prompt by typing Ctrl*C or Ctrl+Break. c) : quadratic real and o'quod'ot equation complex jaqua' llt Lth nit ]e. c ) % quadroots: roots of (a. MATLAB will go into an infinite loop.This "bigpicture" versioncan be programmed tionalcases as f .sl 5t I <.3.t8.. By the way. Five seconds later it will beep again and display the following message: ELapsed time is 5.ond otdet coefficient ffi ?JLI' zl+ z$4 J "t J :I 5\ "L x 4 4 1.4aa LA Developa functionto implement this fbrmulagivenvaluesof the coeff'cients. v a providesa nice approach Solution. The following exampleillustrtrtes the concept.For instance.To start.singlerootsor trivial values) a is zero. tic and toc can be employed to identify the partsof an algorithm that consumethe most execution time. The rootsof a quadratic eqLration /(x):uxl+lrx+r' can be determined with the quadratic formula b +. b . b. ktpdown r/e. % quadroots rools jon z % input: 9o o lo.
(d)) t'2 el se %complex roots = rl b / \2 * a) i1 = serL(.srngle root 11  V.of roo[ first root imaginary ffi= zl+ .b ^ 2 if cl >.I Lrtg  9r\ 5 J 3l x+ I . quadroots: quadroots roots of (a .rdrat ic . c) : b.tl cases e '^rlL1a end Next. [h :.64 PROGRAMMING WITHMATLAB Z b = firstorder Z c = zera order % output: Z rI = real part Z i1 = imagrinary Z 12 = real part % i2 = imaqinary 1t d == u coefflcient coefficient of first root part of flrsL root of second root part of second root %special els<: "ir'r 'dr. else?tr i vial end sol ut iorr solutron.rbs(d)) r) rl / / 12 * (). 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"bigpicture"framework to give the final result: functron ? % % input: % a = Z b % c % ouLput: % rI Z i1 = Z secondorder first order zeroaYder real partquadrooLs (a.sqrt. c) quadratic real of equafion and complex roots quadratic equation coefficient coefficient coefficient of first part.b. rooLs %real r1 = ( b + sqrt(d) ) ( b .O f or:mul a 4 * a * c. we developrefined codeto handlethe "special" cases: %special cases if b '= 0 e."1 {z 5fi st 7t ll "L x 4 <.
eal.1) 0. 1 ) 0.5000 0. ) Sguadratic formula d=b * c.4 NESTING AND INDENTATION % Z iF a r a 65 12 = r. Here rs a command window session illustratinghow the function f"rfo.Also nodc.8660 >> quadroots (l.8660 ml zI zl Llz zl' + .the structures are. .rs >> quadroots(1.5. eLse ?ry agaln .7913 >> quaCroots (0. ^l * g l u: r . .1.how "modular. 4 *a Ediscriminant rf d >= 0 ?rea1 roois ri. o .notice how indentationhelps to make the . (abs (d) ) / (2 * a) r) l .3.1 5t '$8 Z] X+ .  v1 .part of second root 12 = imag.= (b + sqrt(d)) / (2 * a) 12= (bsqrr(d)) / (2*a) else Scomplex roots rl=b/(2*al i1 = gqy.2087 4.inary part of second root == u Sspecial cases ifb=0 ?single roor 1Lc/b else ?trivial solution ( ' ?rivial solut ion.1) 17 = 0...5000 i1 12_0. 5. tr _\ 4 { + 5[st "L x a. underlying logical structurecrear. ! . [h VT Lf €.2000 = 0.f a  end end o.r highrightedby the shading.
they can be usedjust as other functions: >> f1(3. Once thesefunctionsare defined in the commandwindow.=. we could createan a n o n y l n o ufs u n c t i o n / t l ) : . >> fl@(x'Y) x^2 + Y^2.' f2 (3) = 36 Note that if subsequently we enter new valuesfor a and b. For example.r where f. 3.L l lu s >> >> >> ans a >> b 4.Although a customizedfunction could be developedfor every new equation we analyzed. 0 ) ??? Error using ==> quadroots TriviaL solution.and express l on = any singlevalid MATLAB expression.4) ans. f2=@ (x) t t a*x^b. 0.which provide a handy meansto define simple userdefinedfunctionswithout developinga fullblown Mfile.For example. They can be definedwithin the comrnand window with the following syntax: ihandLe = @(argJist) expressro. In the parlanceof MATLAB. = 2.66 j PROGRAMMING WITHMATLAB >> quadroots (0.functictns. Try again 3. the anonymousfunction doesnot chanse: f 2 (3 ) ans ffiar qAb zf Ll t zl & ''l \ c.harrdle .eJ 5t 3$8 3l x+ .functionsallow you to createa simple function without creating an Mfile. \ Asidefiom the variables in its argument list.argTist = a comma separated list of input arguments to be passed to the function. .the functionhandleyou can use to invoke the function.5. a betteralternative is to designa genericfunction and passthe particular equationwe wish to analyzeas an argument. we will first introduceanonymous functions.5 PASSING FUNCTIONS TO M. an anonymous functioncan includevariables that exist in the workspacewhere it is created.lh T l 5[st Y jl'€.FIIES Much of the remainderof the book involves developingfunctionsto numericallyevaluate other functions. thesefunctions are given a special name:function.1 Anonymous Functions Anonyntous . Before describinghow they work.
the anonymous functiondeveloped above.I /0. Notethatin theremainderof thisbook. This functionis "smart" in that it [xmin xmax].2. ffi= .5. The functionthat is passed to the functionfunctionis ref'erred to as the passed.J <.25. 3.For example. the function handleholds a snapshot of the function at the time it was created. >> f2=@x a*x^b. [0 f2 I ) The resultis displayed in Fig. we will also be developing our own.91 5l .MATLAB helpcan be consulted to learnmoreabouttheiruseand limitations.r\€ l Z] X+ I .will havemanyoccasions to useMATLAB's builrin functionfunctions."I LJ ' zl+  1t _l l 5[ s\  "L z. B1*A. vrl 'Jrrj (9.somereaders might be usingearlierversions. A sirnpleexampleis the builtin function fp1oL./681 sqrt (9. 'x'. of its syntaxis fplot\iun. We can then generate a plot from / : 0 to l2 as >> fplot (vel.we must recreate the function. Here is an exampleof how fplot can be usedto plot the velocityof the freefalling jumper. which plots the graphs A simplerepresentation of functions.function. ) *t) .FUNCTIONS 3.lins) where fun is the tirnctionbeing plotted between the raxislimits specified by 7 rms = function.The f unctioncan be created bungee with an anonymous function: .B1*6il. Although they arebeing phased out in favor of anonymous function.'y'). with the result >> ans f2 (3) 27 It shouldbe notedthat prior to MATLAB 7.2 FunctionFunctions Function functions are functionsthat operateon other functionswhich are passed to it as input arguments.25) *Lanh(sclrt. As in the following example. inline functionspertbrmed the same firnctions. [h x T .having changed a to 3.For this case.f 1.If we want the variablesto take on new values. role as anonymous Forexample.wc.f un is the passed automatically analyzes the functionand decides how many valuesto use so that the plot will exhibitall the function'sf'eatures. could be written as >> fl=rnline:('x^2 + y^2'.FILES 67 Thus. 3.5 PASSING TOM. and so we thoughtit would be helpfulto mentionthem.
1 ) 36.25 / 68.2 ng B u i l d i no gn d l m p l e m e n t io F u n c t i oF nu n c t i o n Problem Stqtement.Develop anMfilefunction function to determine value theaverage jumpervelocity of a function overa range. 3.0870 lnspectionof a plot of the function (Fig.) whereg:9. ffir qAb z[ Ll 2 zl ?. 1./0 >> mean(v) *tanh (sqrt (9.81. lllustrate its usefbr thebungee overtherange f t o mr : 0 t o l 2 s : u(r): ffo"n(rH.2 A ploio{velocily generoted versus trrne wilhthefplor funclion EXAMPL 3E . I2) .1+ t"l  5[ st r.2) showsthat this resultis a reasonable estimate of the curve's average heieht. lh l 4 eJ 5t I Z] X+ . >> v=sqrf (9.m:68 value of the function can be comnutedwith standardMATLAB Solution.l'€ r <. 81"68.ar "L x j. The average commands as >> t=linspace (A. 81"0.68 PROGRAMMING WITHMATLAB FIGURE 3.
^r e" fvg funcavg(o. 12.25 / 68.ohL f '. Because we haveremovedthe subfunctionf unc.25 ) *tanh >> funcavg (vel .FILES We can write an Mfile to perform the samecomputation: funcr o 69 ion ^.It can be run fiom the commandwindow as >> vel@ (t ) sqrt ( 9. Finally. the average value is computed. b. 12 .9f 5l T a.o.f\€ . B1*4. B1*68.I / 0.b.vQ) oQe llnspace(a. 1 ) *t ) .b. n ) ro f rra.60) ( sqrt ( 9.25 / 68.1 ) *t ) . lavg u v L ! s Y I uncavg {o. (0.n) :nr hoiqhL % % ? Z % % % z % x = Y = favg favgfuncavg(f input: f = a b n =  . The function can be run from the command window as >> funcavS 36.4121 Now let's rewrite the Mfile so that rather than being specific to f unc. B1*4. n l. end compuLes average of function over value d range function f = func(t) fsqrt (9. ffi z$4 zt+ +. this versionis truly generic.5 PASSING FUNCTIONS TOM.mean(y) .n). A. it evaluates a nonspecific function name f that is passed in as an argument: [*nct.n): % % input: 2 a = lower bound of ranqe % b = upper bound of range % n .n). These values are then passed to a subfunction func in order to generate the corresponding y values.n) : computes of average over val.on 9 r r nr"a.l:.60) iavg r'7^. "L x l{t 3l x+ 4 l . y = func(x).b.3. B1* 68. fundvg . 25 ) *tanh (sqrt (9. f (x).number of intervals % output: % favg = average value of functron x = linspace(a. end The main function first uses linspace to generate equally spaced r values across the range. favg .o. lh 3$8 .I/ 0.b. = mean(Y) .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 = .aa f rrrnl {r.a.
we could havedonethe For example..0.r:d(). fbllowing: >> mb8.funcavg can easily be appliedto anothercaseby To demonstrate merely passingit a different function.2. 36.we could retypethc functicln dif'ferent but it would be preferable tojust change the parameters. MATLAB offers a better alternativeby adding the term varargin as the function function is invoked In addition. parameters into anonymous As we learned in Sec.5. 0.the vararsin{: } shouldbe addedto the end of its (note Here is how both modificationscan be implemented argumentlist the curly brackets)..I.25.s conciseness): ffi= =1_h" .81*m/cd) >> f uncavg (ve1. I and c.every tirne the passed function'slast input argument. the variable.rch modelsby perto investigate fbrcing function. rangingtiom nonlinear contexts 3.Suppose parameters.it is possible to incorporate values.. time (t) is the independent model. 0 r 2 ' 7 its genericnature.1.if we want the parameters to take on new values. We can seethat f uncavg is now designed to evaluate throughout the remainder of this text in a numberof We will do this on numerous occasions equation solvingto the solutionof differential equations.5.5. 2*pi.3 Possing Porqmelers modelscan be divided into dependent Recall from Chap. For example. ratherthan "wiring" the nurneric functions.lt is cornmonplace This involves how thedependent variable changes ana\sis. mous function. and fbrcing functions.f uncavg.3. 180) 6. I that the terms in mathematical variables. and usedit to determine jumper velocity fbr the casewherethe parameters wereset value of the bungee the average thatwe wantedto analyze the samefunction.) sqrt (9.it could be usedto determinethe average value of the builtin sin function between0 and 2z as >> funcavg(Gsin. >> ze1=@(t. case.we developed a functionfunction. (clmitting for fbr f uncavg' comnrent. with new valuesfbr each Of course. term within the function function. as the parameters and forcingtunctions In Example3.rrA { x = "L zJ Zl F+z I .For the bungeejumper and independent variable. the behaviorof sr.70 PROGRAMMING WITHMATLAB 36 .1. observing fbrmingtr sensitivitlt arevaried.but with L\t tn :68.3001e 017 Doesthis resultmakesense? any valid MATLAB expression.t'. pararneters.6A) *Lanh(sqrt (9.the velocity(u) is the dependent (c.2. 0 .A121 we must recreate the anonyHowever.7.) (g) (rr) gravitational and the constant is the rnass and dragcoefTicient areparameters.81*cdz'm) *t) .
f avg = mean(Y) .25) ans = 36 . Suppose that we started the computation at / = 0 and wanted to predict velocity at t : 12 s using a time stepof A/ : 0.Bl"cd ml*Lr.5 s. Z] X+ . vol=t?(r. In this section. n24 0.6 CASE STUDY function favg = funcavg (f.an algorithm to solve the problem would be basedon a loop.25. This involves obtaininga solution of du cd .ILJ ' ?. a. this can be done as in .28) 38. 4. y = f(x.we will use MATLAB to solve the freefalling bungee jumper problem we posedat the beginningof this chapter.Bl*m edr*Lanh(sqrr_r9. To implement the case where m : 68.28.tr+ ffi q b {e[ { st r "L x 4 3$ 8. 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. the actual parameters should be added at the end of the argument list. we could enter >> funcavg(ve1. When all these changes have been made. 68. Solution.linspace(a. 0.m.Therefore. analyzing different parameters becomes easy. 0.the problem involved iteratively solving the formula.12. we would probably want to apply the formula repeatedlyto step out from our initial time to attain the value at the final time.1 and c1 .we would employ small steps.0. 7l When the passed function is defined. 100.varargin{:}). Ar21 An altemative case.9345 BUNGEE VELOCITY JUMPER ) t I 1 Bockground. If we used an anonymous function. 0. We would thereforeneedto apply the iterative equation 24 timesthat is. given an initial conditionfor time and velocity. could be rapidly generated by merely >> funcavg(ve1. varargin) x .5 t2 .3. 64. 60.n).edt SqrL(q. b. U i + l: u 1 * du' At AI ) e v)n its ed Now also remember that to attain good accuracy.b. Consequently.f2. 1. n.t :8UaIm Recall that.
o c .6i. the ratio is an integer).l)) dns = 50.we can usea for loop as the basisfor the algorithm.72 PROGRAMMING WITHMATLAB continued where n : the number of iterations of the loop.in..5 .ocityl: Iiuler solutj..1) end x v''2.BL .deriz (v) dv = 9.Here'san Mfile to do this including a subfunction defining the differential equation: f uncLion vend = velocityl % % Z Z % % % Z % t % L 1. 1 2 .= t. v1) [. To cover such cases. end Varnd = V.1).(A.6175 (Example 3. Becausethis result is exact (i.25 / 68. break loop can be substituted a while in place of the (notethatwe haveomitted shaded area thecomments for conciseness): ffi= zt+ .y( 10 . simpleto program. . vi ) vel.it will not work if '.f \ 4F+ = \'r?+. O ) 50.' end f unct ion d. r .n ti) / dL.0 . 1?. ti. t = t + dt. 0 .he interval is computation not evenlydivisibleby thetime step.i.Bi Althoughthis functionis certainly it is not foolproof. v : v + dvdt * dt.rble outDut: (n/s) v e n d = v e l . This function can be invoked from the command window with the result: >> vel.rme step (s) ti = initi.oci Lyl (0. Lf .e. . rf .uler uret]rod soluLion of bungerj u m p e ] r z eL o c i t y rnpuL: dL ..ue of dependent vari. In particular.' n = Far (tf i  1. iL v a t t f = Li. = Vl. . t. We can then try a much smaller value of dt to obtain a more accurate numerical result: > > v e . o c r i . 0 0 1 ./ (dt. dvdt = deriv(v) .on for bungee velocity vend = velocLtyl (dt.rl time (s) (rn/s) vi = initial val.al Lime (s) Lt = f.9259 Note that the true value obtained from the analytical solution is 50.
we assignthe value of the time step dt to another variable h. By doing this. . end if t >end vend = v. dvdt = deriv(v) v=v+dvdt*h. 1t r h. If we run this new version. the user could havemistakenlyenteredast3psizegreaterthanttrecalculationinterval(e.L) end * v""2. ] .9 2\9 Further. A) ci: 50 .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. Notice that before entering the loop. We create this dummy variable so that our routine does not change the given value of dt if and when we shortenthe time step.the result will be the sameas for the versionbasedon the for structure: . After we implementthis final step. vend = velociLy2 (dt. end tf. break.81. 35. 12. you might want to include error traps in your code to catch such errors and then allow the user to correct the mistake. ' > v e l o c i L y 2 ( 0 . h = tf . e ffi . we guarantee that the last stepfalls exactly on t f . A.6 CASE STUDY 73 continued function t = ti. t1.3. we set the variablesteph to the intervalremaining:tf * t. l : 1.  t. end dv f unct ion clv = deriv (v ) . If so. tf.9. we can use a dt that is not evenly divisible into tf :'. 0 ) 5A.(0. we do nothing.If not (which would usually be the caseat first). Thus. 0 .25 / 68. [h ^r sJ 5t 3$& .ti = 5 and dt = 2 0).. As soon as we enterthe while loop.the loop will terminatebecause the condition t >= tf will test true. tf .834r1 We should note that the algorithm is still not foolproof."I LJ ' b ffii I ^l \ Z] X+ x <. we would shortenup the intervalthat is.velociLy2 (0. For example. vi ) while (1) if t + dt > tf.\ ^ f . we usea singleline if structureto test whether adding t + dt will take us beyondthe end of the interval.g. 5 .
h =. : : 5 would be y(5) : l0e*0 r('5) lytical solutionjy l0e0 Therefore. r:nd Create an Mfile to ir putes and displays thr series is added. y j ) i{ cnrl Yenrl t. PROB 3.employ up to and including e xto 114r. The ment A is AP "' i/  a. l. (0 (y ) 0.t .le:r1  "/oeffOr 'r(l92b'r We could then analyzea different function without having to go in and modify the dy/dt : 0.r . l l .0653..'= = Y' tf. m p ( c 1 v c 1 l . That is. t. r < .We can use oclesimp to obtainthe sameresultnumericallyas in '':. l l ) or.:ii nrr.rrd .2 \ / 6 f . h = dt. a ( v ) .:l 5t 3$ & .lI ll . break. tf L.y: lQ al t :0.2 An amountof mon interest is compounde worth F yielded at an determined from the fo F:P(l*i)" b.1 .y hasthe anaMfile. ! ..t "t . l . we have designed it to solve the specific problem of the velocity of the bungee jumper.' .the solutionat t 6. . ) ' :(.by specifyingthe differentialequationwith an anonymous funcbungee tion and passingits function handleto odesimp to generate the solution .* 11. In o sequencethe valuesfc 'l : COS.0i. l : l ..if . Su money P and agree to interest rate of i. od. r l . 1 As a test case.(. we should recognize that the foregoing code is not generic.an future worth is to be ca a table with headingsa gramforP:$100. For example.se q .\r (1+i). r . l t i. A more generic version can be developed as f L r n c .th r zl F+ jl€ . Lf... { r) l * y .. .*ufleqqte zr+ ffi  x a.r x2 cosr: I  t 2 COS. y = yi.3 Economic formulr payments for loans. I l . i J { .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 can thenusethis routineto solvethe ing the essential features of the solutiontechnique.74 PROGRAMMI NG WITHMATLAB continued As a final note. o d e s i r n p ( d y d r . t.' t i o ny e n d = ./ + dyclL(1) * I'ii f ts h. : up to the order term o ceding.ti. L ) * u " " .4'l Write an Mfile that wi vestment for each year function should includt rate i (as a decimal). . 3.: . compute and d true . jumper example.00( 3. a r .\ ( . the differentialequation r'.1 The cosine funct infinite series: cos"r: I  t i. ) .f . L . C t . (1) whiIe j 1' t + c]t.
and tn"^r.066).senesapproxrmatlon true ) the [nc 221 231 52 t0 6 la7 : T. Tcst it hetrvcen As a testcase.: day ol' the pcak tempcrature (=205 d). D WA Seoiile. <Ah 1 st 4 fi:l  sp 5t 3$8 . AZ B i s m o r cN k. compute and display in sequence the valuestbr C O s .) t +   Create an Mfile to irnplementthis folrrrula so that it computes and displaysthe values of cos x as each term in the series is added.I.Conlpu(eresultsftrr l : 1 . Test it with P : $55.06. In other words. 3."^n * (Q"ar. MA (t: 180to 242). lrue .s: thc peaktcmpelaturc. where Z. 3 . The output shouldconsistof future atable rvithhcadings and columnsfirr n and F.employ the prograrnto corllputecos(L5) fbr (a) in Miarni.and n : 7 years.error ^ 1007.r: I Write an Mfile to compLrte A. 4 .5 Figurc P3.000. a n d5 a n dd i s p l a y t h er e s u l t s a s a t a b l ew i t h h e a d ings and columnsfbr n and A. 3.PROBLEMS 75 PROBtEMS we Dre 3.l P3.(/ . and the numbcrof yearsn firr which the worthis to be calculatcd.u""u In. towns are listcd here: 2l . 2 .1 The cosinefunction can be evaluatedby the following rnfinite series: x2 . The input t0 the the volurncfirr all cascs Desisnthc tunctionso thatit rcturns function should P.3Economictbrmulas are available to computeannual payments for loans. FL 1l : 0 to 59)and lirr JanuaryFcbruary is. computeand display the perccntrelativeerror as ceding. r : I .6% (l : 0.l f t h e l i q u i d yielded worthF 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 level is rnidrangc in the cylindricalpart.Tlrc luturc' basc.lp"ol)) : the averageannual temperaturc. Usc dccisional structures to writc an Mf ile to compule Write anMfilc that will calculate the futurcworth of an inthe tank'svolumc as a flnction ol'given valucsof'R and 11. o : thc frequencyof thc annualvariation (:2n1365).000and an interestrateof 6.5 3.6 229 : For eacholthc preupto thc orderterm ol'youl choosing. r'* f141..t4 c o s .on ("C) T"""r ("C) 283 336 221 )7. thc interest includethe initial investment rate I (asa decinral).5 FIGURE W ZN 4Y NT ffiE\ zJLJt & +. FI Yumo. shows a cylindrical tank with a conical wherc 3.thc totrl volLrncol' liom thc lbllowing fbrmula: deternrined includcs thc fillcclconicalpart and thc partiallytillcd liqLrid F=P(l+i)" c y l i n d r i c ap l art.thc l o l u m eo l ' l i q u i d . vestment lbr each ycar lrorn I through rr. Boston. Paramcters fbr someU. t : 0.lf'the liquid levcl is quitc low.r :I : I cosr cos.. The tblmula to computc the annual payment A is tp iil + l)" (l+i). y the ) anal5t Dcvelop an Mfile that cornputesthe averagctcmperature two claysol' thc year {irr a particularcity.Supposethat you borrow an amountof money P and agreeto repay it in n annrralpaymentsat an inlerest rateol l. +x4 4l the "Y City AAiomi.S.. tcrm up to and including eight tcnrsthat up to thc (b) JLrlyAugusttcnrpcraturc in Boston. Run the progtamfor P : $100. MA T.."nn) cos((. in the conicalpart.2 An amount ol rnoneyP is investcd in an account inlerest is conrpoundcd at thc cnd ol'thc pcriod.4 The averagedaily temperature for an area can be approxirnaled by the followingfunction: 7 : 4.
:l*?+v.0 00( as 3.passvectorsof ir and).sosirnple.11 grade liom 0 to 100 and returnsa lettcr grade according to the schcmc: Criterio Write an Mfile that cor channels. then a sirnplelbrmula can bc uscd to compute 9: rl : tan I (I) \_r..6 Two distancesare required to specify the location of a point relative to an origin in twodimensionalspace (Fig.y.U s i n g s i n g along the heamcan be e 0 A B C D F 90 < BO 1 /O . 1 0 . P 3 .sJ 5t I <.0 OO 00t 0..6). However.76 PROGRAMMING WITHMATLAB where the depth is less than 3R.zxl * r . 3. d > 3R.x > 0). .6 Jsr BH \2' il \. 3.7 By dcfinition.6. y) in Cartesian tion of r and_r'. The horizontal and vertical distances(r.5 I t5 2 t5 45 t< Note that the tank's radiusis R. Thc radiusand angle(r.1T loo lr.. Test it summarizes the possibilities: with the lbllowine data: xy0 R d 1. BO numerlc numeric crode < /0 n u m e r i cg r o d e < 6 0 + 7 5 ( x.7 Dcvelop an Mfilcr to detcrmine polar coordinates descrihedin Prob./.9 Manning'scquationcan be r.rsed of water in a rectangular open channel: FIGURE P3.rJ T rl2 n /2 0 where U: velocity(m cocfficient. .035 0 020 o 0t5 0 030 o 422 0.t'+ +Y jl€ . xJr It is relatively straightfbrwardto computeCartesian coordi(r.f 4+4nvI \ ?.Testthe prograrn fbr the cases outlined in Prob.The radiuscan be cornputcd by the fbllowing lbrmula: <0 <0 <0 :0 :0 :0 >0 <0 :0 >0 <0 :0 ton l"./ 0 0 I 0.3. P3. your programby evaluating thc fbllowingcases: coordinates.B: width ing data is available fo . The following table ("Overtop") iI you overtopthe tankthat is. The nates(r.8+2H/ Develop anMfilethat< distance along thebeam the beam.6.10 A simply suppo F i g .Includeheadin 3. Have the Mfile di computedvelocityin ta colurnn. Return an error message The difliculty arisesfor the other cases.6): Write a wellstructured Mfile to calculater and 0 as a funcTest Expressthe final resultsfor d in dcgrees.e. Dcvelop an Mlllc function that is passeda numenc 3. r. the sing follows: (xtt)":lt*o l0 to computethe velocity 3. and(). If the coorilinates lic within the first and lburth coordinates(i. ffi= n = . Enter thesev umn represents a param nel.y) on the basis of polar coordinatcs reverseprocess is not. rathcr lhan designingthe function to cvaluatea single casg.[h 3l x+ . 60 < numeric orode < l0C numeric grode < 90 qrode . . d) in polarcoordinates. Havethe f unctiondisplaythe results as a tablewith columns forx.
. n ..IlI 0035 0020 00r5 0030 0422 r0 B 20 24 t5 Dcvclopan Mfllc to cl'cilte a plot ol'volume versus depth... _ J I tt and the errur. Notc that.r/lr ^ . 4 ."I LJ ' zl'+ ffi q A b st Z} z.Designyour programso that it returns . Have thc Ml'ilcdisplaythc input data along with thc c l r t ( l vclocity in tabular firm whcrc velocity is the l'ilth computed 3._...25 t * ? 5 ( r 7 ) r+ clrch\tcp c\tinrirtc thc crrrrrin y.Test your program root of 4 would retr. 3.r ulx + along thebcamcan bc cxprcsscdhy thc ccpration: 2 s t5 ' Writc a wcllstructurcd Ml'ilc lunction bascd on a :'*l . Tcst thc program lorr ._ tt . Makc surc that it can cvaluatethc squareroot when r < a I I o1'nunrbers thatarc equalto and lcssthanzero..r x) lrr. rtl" when. a n d.10 A simply supportcdbcant is loadcd as shown in can bc firrrnulatcd as Fig. thebeam. hbv is availahle lbr llve channe ls: ing data n l.10. as thc singularitylunctioncan be cxprcsscd " .beak loop structurc 57 r so that thc structnrcis clcar. 2 . P3..\t'ltl .. I O (rn/s).t1L. umn reprcscnts a prramctcrand cach row rcprcscnts a channel.r)l'r0)'(r oo to irnplcmcnthis algowhil. an oldtimcmcthod lncludc hcadings on thc tablcto lahcl thc colurnns. tc.r. lirr approximating thc square 3. L ( L) ts1. _.1l Thcvolumc liquid r andlcngth L is related to thedepth o1'the B: width (m). Develop a plot oldisplacemenl anMfilc thatcrcatcs 21.0 .12 Dcvclop a vectorized vclsion ol'thc lbllowing codc: 1. where V of liquidin a hollow horizontal cylinder U: vclocity S: channcl slope./r Vltcor'[L \r 0 000t 0 0002 0 0010 0 000/ 0 0003 hr  / rr l htr2 / rlt h.The tbllow.roughness 3.For the latter For examversus casc. rri 5.nclilfl. and H : dcpth(rn)..rurirpprtrrillluli()n irs 6 t. thc displacerrncnt Using singularityf'unctions.ol'radius coefficient.1 Bydetinition. . root ol'any positivcnumbcra..rrn distance alongthe beam.displaythe resultas an imaginarynumber.ll Thc "dividc and average" method. .2 rn and L: 5m.9 f b r c : I x l 0 r .PROBLEMS 77 20 kips/ft :c F I G U RP E3 . (. .t I t r n r u ' l follows: Rcpeatthe loop until a is lessthan or cqual to a spccified (.t : 0 at the leli end ol' plc. .At rithur.:ildrL=U.Use ploper indentation '\ 23tt. column. Write anMfilcthatconrputcs thc vclocitylirl cachol'thcse Enterthesevaluesinto a matrix whcrc cach colchannels.r > a both thc rcsult value.J I 3l x+ . thc square byevaluatin ug .
a n d leap : (0 for nonleap year and I for leap year).2. 2001. and December31. d a : t h e d a y ( l .@ ( v .958'7. 2001. Hint: A nice way to do this combinesthe for and the switch structures. January 2002.Test it for January l.14 Piecewise useful when the refunctions are sometimes variable lationshipbetweena dependent and an independent cannot be adequately represented by a single equation.andDecember 31.The first line of the function should be set up as function nd = days(mo.0. B L :':' oclesirnp (dvtlt . havethe lunctiongenerate a plot of the functionfbr the range.6 so that it can bc passcdthc argumcntsof days in a year.0 .25) >> zt'+ .3 l ) . A .June21. Then.I. 0. (c) The builtinhumpslunclionliom x : 0 to 3.2002. d v c l t . 0.(l (a). The first line of the f'unction shouldbe setup as f uncrt i. le. c : c 1) 9 . 2000.19 Modily the functionfunction odesimp developed 1. 3. 1 3 5 . In addition. The f'irst linc ol' thc function should be set the passed lunction.1 2 ) .For by example.125.Test it for the following cases: function xr = rounder(x. 3. 0.1 2 ) . Z] X+ 3$8 . use this lunction to gcneratea plot of u versus/ fbr t5to50.n. a n d year : the year.rn nd = days (mo. L2 .Test it for l. m . cla.16 Develop an Mfilc function to dctcrmine the elapsed the end ol'Sec. d a : t h e d a y ( l . 2004.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 . /(x) 0. a n d. 1999. 3. n) . at 3.March 1.(b) : es'sin( l/x) liom r : 0. February 29. 467. tt9.2004. 5.2000.3 1 ) . February29.l(0: l0e02s'sin 4 ) f r o mt : o t o 6 T . 125."1 {t _q ffir st 71sy .1999. 13. inraldigits:r : 467.June21. Testthe prograrnby roundingcachof thc fbllowing to 2 dec.the velocity of a rocket might be described l1t25t ll005/ 50t+2(t20)2 l52oe 0. Tcstit lbr thc lbllowing casc: up as Develop an Mflle function to compute u as a function of /. 3.15 Developan Mfile lunctioncallcdrounder to rounda numberr to a specifiednumberof dccimal digits.0 .t ^l +.rp) (c<1/m)*v^2.78 PROGRAMMING WITHMATLAB w h e r em o : t h e m o n t h ( l .5.18 Developa lunctionfunctionMflle thatreturns the differencebetweenthc passcdiunction's maximum and minimum value givcn a range of the independentvariable. year) 3.9587.01 to 0.March l.17 Develop an Mfile function to determinc the elapsed days in a year. da.
1li. I.'e calculation.2001. Understanding how to write lbrwarcl. Speciticobjectives and topicscovered are :loped at mentsof ' ' ' ' r) ' ' ' t ' Understanding the distinction between and precision. and nry 29.a n d :st it for Iune 21.+t . Understanding how roundofferrorsoccllrbccause digitalcornputers havea limitedahility to represent nunrbers.. t. ""j 'rt6."'. Recognizing that truncation errorsoccurwhenexactmathematical fbrmulations ilrc represcnlcd by approx irnat ions.1 . backward. the difrd miniiable. do this elapsed J be set ..ti 79 . R e c o g n i z i ntg h a te l ' f i r r tls o rninimize l " r u n c r r t ie or nr o r s c a ns o m e l i m eis ncrcase roundofT errors.To n Chap. Knowing how to uscthe Taylorseliesto estimate truncation errors. Understanding why lloatingpoint numbers have[irnitson their rangeand precision."lii: i Roundoff Errors ond Truncotion ll). !]=!!dr Lt u(ti+r)u(ri) t. Learning Learninghow errorcstimates can be usedto decidewhen to terminate an iterrti'..1ffi : i irl:r 1. YOU'VE GOT A PROBLEM jumper..ii i 1 ) . . and ccntered finitedifTerence approximations of first and seconcl derivatives. the derivativeof velocity with a llnite diflerence. you developed a numerical model for the velocity of a bungee you had to approximate solve the problemwith a computer. In ction for CHAPTER OBJECTIVES The primaryobjective you with the major sources of this chapter is to acquaint of errorsinvolvedin nurnerical rnethods. accuracy how to quantityerror.
Ccneralinformation concerned ol'erroris of these lhis is fbllowed by Sections 1. This chaptercovcrs basictopicsrclated to tlreidentification. your your both mathenlatical digital So approxir.80 R O U N D O FA F N D T R U N C A T I OE NR R O R S Thus.centered on thc bull'seye). thenwe viationsftom the are systematically new rzrndomly andtightly might needto devclopa modcl.1.4. Beit is a digitaldevice. is an excellent it would neverin practice cxactlypredictthejumper'sfall.the computeryou use to obtain the solutionis also an imperfecttool. roundolT.I ERRORS constantly find themselves havingto arccomplish objectives based i Engineers and scientists goal.and datauncertaintv. is limitedin its ability to represent the magnitudes cause the computer precision yields Consequently.. althoughthe shotsin Fig. the latteris more precise because d are eqLrally accurate the shotsaretightly grouped.ref'ers to the magnitudeo1'thescatter. and rniniwith the quantification mizaticln errors.nation and computercauseyour lrprediction problem How rnodel to Your is: sulting be uncertain. rnodel directlyconnected themselves. Although For tact that the developed liom Newton's second law tained.\'iorr computed or measured valuesagreewith sachother. it has error. thc two cases arc equallybiased theyate of thc target. dealingwith the two reviewedin this section. graphically Theseconcepts can be illustrated using an analogyfiom targetpractice. 4.althoughFig. approxirlations also i n t o t h ea n a l y s i s . do you deal with suchunyou introduces to sonle and that engineers and certainty'? This chapter approaches concepts with this scientists useto deal dilemma. approxirnation.Imltrecision(alsocalledurrc:ertaintl). the itself and of numbers. I e a to w L( rh .v rcl'ers to how closelya corlputedor mearegardto theiraccuracy ref'ers to how closelyindividuitl suredvalueagrees with thetruevalue. iltheyare distributed grouped prediction. machine results thatcontainenor.Inaccurucy defirrecl deviationfrom the truth. briefly discuss crrorsnot duce truncation error sometinres Thess with the nurnerical nrethods includeblundcrs.la.e. both cerrtered on the upperleft quadrant Therefore. quantification. whereas the bull'seye the truth. If thesedeviations high or low.Prct'1. errors. l can be thoughtof as the predictions (alsocalledbirr.However. the resultingsolution is not exactthat is.1 Accurocy ond Precision with The errorsassociated with both calculations can be characterized arrdmeasuremenls and precision.i)is represents ical technique. I I i I 4. perf'ection rarely if is a laudable it is ever aton uncertain information. Att'unrc'. we increase Finally. (dueto computer error:rclundof'l'crror andtrunmajorfirrmsof numerical approximations) We alsodescribe how strategies to recationerlor (dueto mathe nratical approximations). 4.4.Thus. (i.lc are as systematic because more tightly grouped than in Fig. of a numerThe bulletholeson eachtargetin Fig. lD and on the other hand. 4. despite the rnodel example. In addition.2 and4.3. variations would resultin desuchas windsand slight in air rcsistance A varietyof factors prediction. rnight be considered negligible thenthe deviations andthe aroundthe Numerical introduce similar discrepancies model deemedadequate.
our rerch unersand o 0) a s based )ver atrnd law :r'sfall. o n c ii n r p r e c i s e en d r m p r e c i s e nd {rJ) o ( ' L r o ' ee r '. In tlris book.l b and because Numerical errorsariscfrom the useof approximations to rL'present exactmathematical opand quantities.u p p r o x i m a t i o n (4. For sucherrors.s) 4.I ERRORS 8r accuracy Increasing ol. For example.2 Error Definitions ed with or rneamputed rractice. 4.or true. from morksmonship Anexomple (c (.4. we will use thc collectiveterm errur to represent both the inaccuracy and implccision t l l 'o u r p r e d i ci t ons. Ji n c c c u r o t o end precise.1.I th illustrotin ge c o n c e p l s ol occurocy ond precis on. l). l. bloccurote ( o ) i n o c c u r o tc . sin 6 r : t r u ev a l u e. numeris bia. we find that the numericalerror is equal to the discrepancy By rearranging between t h el r u t hi t n dt h eu p p r o x i m a l i o n a.I c' are they are rtainty).(4.result and thc approximation carrbe lbrmulated as True value: approximation f error ( 4 .the relationship erations between the exact. J p e c : s e .as describedshortly.Note that the true error is commonly expressed as an absolutevalue and referredto as the absoLute error.They also shouldbe precise nrentsof a enoughfbr adequate design. an errorof a centimeter is muchrnoresignificant . It in delhen we I tightly and the :pancies r c lm i n i 'error is the two s to reors not .Beritudes n errol'. wherean "approximate"estimate of the enor must be employed. rlodel o) FIGURE 4.This is in contrastto other cases.21 whereCr is usedto designate theexactvalueof theerror.l ) Eq. A shortcoming of this definitionis that it takesno accrount of the order of magnitudeof thevalueunderexarnination. The subscript t is included to designatethat this is the "true" error. ("o r\umerrcalmethocls snoulcl oe sufiicrenrly accura("e or unorused meet irc requtreparticular problem.
82
ERRORS ROUNDOFF AND TRUNCATION
of the if we are measuringa rivet than a bridge. One way to accountfor the magnitudes quantities is to normalizethe error to the true value,as irbeing evaluated True fractionalrelativeerror : true value  approxlmatlon true value
4.
wl be pe to
express it as The relativeerrorcan alsobe multipliedbv 100%,to true value  approximation
cl 
true value
1004/t
Th to aln
where e, designates the true percentrelativeerror. the lengths of a bridgeand that you havethe taskof measuring For example, suppose values and cm, respectively. If the true are 10.000 with and 9 rivet and come up 9999 a relativeerrors their percent is I cm. However. 10 cm, respectively, the enor in both cases Thus,although bothntearespectively. usingEq. (4.3) as0.0I 7oand l0olo, canbe computed greater. for rivet We the is much 1 cm, the relative error error of surcments havean absolute job measuring the bridge. of probably that we have done an adequate wor.rlcl conclude to be desired. for the rive( leaves sornetl.ring whereas our estimate (4.2) (4.3), with a 1 to signifythatthe subscripted that for Eqs. t and e are Notice and rivet For exanrple of the and the bridge,we wercprothe value. the error is based on true such infbrrnation is rarcly available. in situations with this value. Howcvcr, actual vided wlren we deal with functions that value will only be known nrethods, the true For nurnerical when we investigate the theothe will typically bc case Such can be solvedanalytically. apHowever,in realworld fbr simplesysterns. of a particular techniqLre reticalbehavi<,rr priori. siturtions. an For these plications, true answcr a we will obviouslynot know the valuethat of the true usirrg the best available estimate is to norrnalize theerror alternative i s , t o t h ea p p r o x i m a t i oin tself, a si n error' approxrmate l00a/( approximation value.Note to an approximate a signifiesthat the error is norrnalized wherethe subscript theerrttrtermin applications, Eq. (4.2)cannotbe usedto !:alculate alsothatfbr realworld rnethods is to determine of numerical of Eq. (4.4).One of the challenges the nunterator regardiug certain the true value.For example, crror estimates in the absence of knowledge In suchcases, a present approximamethodsuseiteratiotr to computcanswers. nunrerical repeatThis process is performed tion is rnadeon the basisof a previousapproximation. to successively compute(hopefully)betterand betterapproximations. edly,or iteratively, betweenthe previousandpreas the dil'lerence the error is often estimated For suchcases. to relative error is determined accorcling Thus,percent sentapproximations.  previous present approximation approximation l\OC/t present approxrmatlon ( 45 )
pro
can
EXAMPLE 4.1
Err
Pro ries
Thu esti
: '
^0.5 c .1
with term fied t
JOIU
resu
I
Thui tima
or fc
on in subsequent chapters. fbr expressing errors is elaborated This and other approaches The signsof Eqs.(4.2) through(4.5) may be either positiveor negative.lf the approx' is greater thanthecurapproximation thanthe truevalue(or theprevious im:rtionis greater the error is negative:if the approximationis lessthan the true value, rent approximation), the denonrinator may be lessthanzero, Also, for Eqs.(4.3) to (4.5), the error is positive.
Thir
4.I ERRORS
which can also lead to a negativeerror. Often, when performing computations.we may not be concemed with the sign of the eror but areinterested in whetherthe absolute valueof the percentrelativeerror is lower than a prespecified tolerunce e.. Theretbre,it is often useful to employ the absolute value of Eq. (4.5). For suchcases, the computationis repeated until Ir,, I <€., (4.6)
This relationship is refered Io as a stop1'ting criterion.If it is satisfied. our resultis assumed to be within the prespecified acceptable level o.. Note that for the remainder of this text, we almostalways employ absolutevalueswhen using relativeerrors. It is alsoconvenientto relatetheseerrorsto the numberof significantfiguresin the approximation. It can be shown(Scarborough. 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 4 E r r o rE s t i m o t e f so r l t e r o t i v e Methods
Problem Stotement. In mathematics, functionscan otien be represented by infinite series.For exanrple, functioncan be computed the exponential using
e':l+.r+'
'Ihus,
,\'l 2
F:+..
l!
.rl
1
.r" nt
(E4.l.l)
as more terms are addedin sequence, the apploxirnation becomes a betterand better (E4.L l) is calleda Moclourinserit,,s of the true valueof e'. Equation c'slimate a.rput,sion. with the shnplest version. Starting e' : l, add termsone at a time in orderto estimate 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 c,, falls below a prespecified 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 102 t)"/n:0.05a/,, €. : (0.5
Thus,we will add termsto the series until e,,falls belowthis level. The first cstimate is simplyequalto Eq. (E4. . l) with a singleterm.Thus,the first est i r t i a t e i s e q uta clll . 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 et: l*x
or fbr,r : 0.5 e o 5 I * 0 . 5 : 1.5
This represents a true percent relative enor of tEq. (a.3)l
^_l . ' _ 
r . 6 " 1 8 72 1 t..s I =9.02(/t I x 1001, t.64812t I
84
ERRORS ROUNDOFF AND TRUNCATION
estimateof the error, as in Equation(4.5) can be usedto determinean approximate
:.1.1.3(Z r., : lj_=_l x 100(/o
I l.) 
t15ll
I b
S
the computation by valueof e,, we would continue Because en is not lessthanthe required The processis continued adding anotherterm,x212r.,and repeatingthe error calculations. as until le,, l < e,. The entire computationcan be summarized
Ierms
n
D,
Result 5 625 645833333 648437544 6486979)7 393 942 I44 a t75 a at72 000142 3 33 769 I27 0 t58 00t58
I 2 3 4 5 6
nl di nl tu
TI
aft
andthe the approxirnate errorfalls below e, : 0.05olo, Thus, after six termsare includcd, figures, the is terminated. Howcver,noticethat,ratherthan threesignificant computation tbr this case, both Eqs.(4.5) and(4.1) areconresultis accurate to five! This is because, Although, servative. That is, they ensure that the resultis at leastas good as they specify. it is true mostof thc time. this is not alwaysthe casclbr Eq. (4.5),
w( ou 0c ele or po (0
4.2
ROUNDOFF ERRORS
somequantities exactly. digital computerscannotrcpresent Rountlo.ff'errur,s arisebecause solvingbecause they canleadto to engineering and scicntificproblern They are irnportant going unstable results. In ccrtaincases, they can actuallylcad to a calculation erroneous are saidto be illconditioned, results. Suchcalculations and yiefdingobviouslyerroneous thataredifTicult to detect. discrepancies Worsestill, thcy can lcad to subtler involvedin numerical calculations: Thcreare two major facets of nrundoll'crrors 1. 2. numben. limits on theirability to represent Digital computers havesizeandprecision This canremanipulations are highly sensitive to roundofT errors. Certainnumerical as well as fiorn the way in which computsult liorn both rnathematical considerations i()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 calleda word, in a computer.The fundamentalunit wherebyinfbrmation is represented or bits. Numbers are typically This is an entity that consistscf a string of binary digil.l, we must first revierry how this is accomplished, storedin one or more words.To understand somematerialrelated to numbersystems.
FIGI Thet mog
lll
rT
IL
I Sign
4.2 ROUNDOFF ERRORS
85
Anumber systemis merely a conventionfor representing quantities. Because we have l0 fingers and l0 toes,the numbersystemthat we are most familiar with is the decimal,or base10,number system.A baseis the number used as the reference fbr constructingthe s y s t e m . T h e b a s e  l 0 s y s t e m u s els hie 0td gitsO,1,2,3,4,5,6,1,8,and9torepresent numbers.By themselves, thesedigits are satisfactory for countingfrom 0 to 9. For largerquantities, combinations of these basicdigits are used,with the positionor place vuluespecifying the magnitude. The rightmost digit in a whole numberrepresents a numberfrom 0 to 9. The seconddigit fiom the right represents a multiple of 10.The third digit from the right represents a multipleof 100and so on. For example,if we have the 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 x l0r)+(2x 100)+(9x l0 ry:9642.9
This type of representation is calledpositionol notatktn. Now, because the decimalsystem is so familiar,it is not commonlyrealized thal there Forexample, arealternatives. if humanbeingshappened to havceightfingersand toeswe would undoubtedly havedeveloped an ot'tul,t>rba.se8, representation. In the samesense, our fiiend the computeris like a twofingcrcd animalwho is limited to two stateseither 0 or l. This relates to the fact that the prirnarylogic unitsof digital computcrs are on/off electronic components. Hence,numberson the computerare rcpresented with a birtary, system. or ba"^e2, Just as with the decirnalsystcm,quantities can be represented using p o s i t i o n an l otationF . or examplc, t 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 ) + ( 0 x 2 l ) * ( l x 2 " )+ ( l x 2   t ): 4 + 0 + I + 0 . 5 : 5 . 5i n t h c d e c i m a s ly s t e m . Integer Representotion. Now that we havereviewed numbers how base10 can be represented in binarylorm, it is simplcto conccive of'how integers arerepresented on a cornputer.The most straightforwardapproach, callcd thc ,signad nngnitrule metfutd,employs the first bit of a word to indicate the sign,with a 0 fbr positivcanda I firr negative. The remainingbits arc usedto storeths number. Forexample, thc integervalueof 173is represented in binary asl0l0l l0l: ( 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, thebinaryequivalent of  173wouldbe stored on a l6bit computer, asdepicted in Fig.4.2. If sucha scheme is cmployed, thereclearlyis a lirnitedrangeof integers that can be represented. Again assurrring a l6bit word size,if one bit is usedtbr the sign,the l5 remainingbits can represent liom 0 to I I I I I I I I I I I I I I I . The upperlimit can binaryintegers
FIGURE 4.2
T h eb i n o r yr e p r e s e n t o l i o on f 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 r s i n gt h e s i g n e d mognilude method
t
1
0
0
0
0
0
0
01110
Magnitude
1
0
1
0
1
Y
Sign
I
86
ROUNDOFF AND TRUNCATION ERRORS
be converted to adecimalinreger, asin (1 * 2'o)+(1 x 2'3)+. . . + (l x 2') * (l x 20;= 32,161 . Note that this valuecan be simply evaluared as 2rs  l. Thus,a l6bit computer 32,761 word can storedecimalintegers rangingfrom to 32,767 . In addition, becausezero is already defined as 0000000000000000, it is redundant to use the number 1000000000000000 to define a "minus zero." Therefore,it is conven32.768, and the rangeis tionally employedto represent an additional negative nurrrber: 32.768 frorn to 32,761 . For an nbit word,the rangewould be from 2" to 2"  l. Thus, 32bit integerswould rangefrom 2,141.183.648 to +2,141,483,641. Note that,althoughit providesa nice way to illustrateour point, the signedmagnitude method is not actually usedto represent integersfor conventionalcomputers. A prefened approachcalled the 2s complemenl techniquedirectly incorporates the sign into the number's magnituderatherthanproviding a separate plus or minus.Regardless, bit to represent the rangeof numbersis still the sameas for the signedmagnitudemethoddescribed above, The foregoingserves to illustratehow all digital computers are limited in their capability to represent integers. That is, numbersaboveor below the rangecannotbe represented. A moreserious limitationis encountered in the storage andrnanipulation of fractit'rnal quantitiesas described next. FlootingPoint quantities Representotion. Fractional are typically represented in computersusing floatingpoint.format. In this approach, which is very much like scientific notation, the numberis expressed as *sxb" wheres : the significand, b: the baseof the numbersystembeing used,and e : the cxponent. Priorto beingexpressed in this forrn,the nurnbelis normaliz.ed by movingthedecirnal 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 in a wastefulmanneras 0.005671t x l0('.However,nort.ttalization would yield 5.678,. l0 I which eliminates the useless zeroes. Beforedescribing implementation the base2 usedon computers, we will first explore the fbndamental irnplications of suchfloatingpoint representation. In particular, 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, a nice way to do this is within the contextof our moretirmiliarbasel0 decimalworld.
tS :p
i"
i
rq
F o
V
e) 0
of
pl TI or
VA
w( ca
EXAMPLE 4.2
lmplicotions of FlootingPoin Rte p r e s e n t o t i o n ProblemStotement. Suppose thatwe hada hypothetical I0 computer basewith a 5digit word size.Assumethatonedigit is usedfbr the sign,two for the exponent, and two forthe mantissa. For simplicity, assume thatone of theexponent digitsis usedfirr its sign,leaving a singledigit for its magnitude. Solution. A generalrepresentation of the numberfollowing normalizationwcluldbe s 1 d 1 . dx 2 lOro'd)
* Th
fio<
4.2 ROUNDOFF ERRORS
87
)r
nt nis
1S,
th w h e r e . r o a n d.s , e s i g n s ,d , : t h e r n a g n i t u d e o th f eexponent.ad nr da n d d , : t h e m a g 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 to both signs being positive and all possible nragnitude digits setto the largest valuein base10, that is. 9: Largest value: +9.9 x l0+Y possible So the largest numberwould be a little lessthan l0 billion. Althoughthis might seemlike a big nunrber, it's reallynot that big. For example, this computer would be incapableof representing a commonlyusedconstant like Avogadro's number16.022x l0rr.1. possible positivenumberwould be In the samesensc, the smallest '' S m a l l e sv t a l u e: * 1 . 0 x l 0 Again, althoughthis value rnighf seempretty snrall,you could nol use it trl represent a quantity likePlanck's c o n s t a n(t6 . 6 2 6x l ( )  r r J . s ) . Similarnegative values couldalsobe developed. The resulting ranges aredisplayed in Fig.4.3. Largepositiveand negative numbers that fall outsidethe rangewould causean overflowerrcr. ln a similar sense, for very small quantities thereis a "hole" at zero.and very smallquantities would usuallybe converted to zero. Recognize that the exponent overwhelrningly determines theserangelimitations. For exarlple,if we increase the manfissa by onedigit. the maxirnum valueincreases slightlyto 9.99 x l0'. In contrast, increase raises a onedigit in theexponent themaximurn by 90 orders to 9.9 x l0e'r! of magnitude When it comes to precision, however, thesituation is reversed. Whereas the signil'icand playsa minor role in detiningtherange, it hasa profound etl'ect on specifying the precision. illustrated This is dramatically fbr this exanrple 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. a simplerational number with a finitenurnber of cligits like 25 : 0.0l125 would haveto bc storedas 1. I x l0 r or 0.03I . Thus.'t rountlofferror is introduced. Fclrthis it represcnts case, a relative errorof 0 . 0 3 1 2 5 0 . 0 3
de ed mrSS,
ve. riled. an
)mtific
. the :imal ne so rle, a {owplore at are ntissa a nice
125 0.03
: 0.00t1
FIGURE 4.3 5digit for the eaving
T h en u m b e r l i n es h o w i n gt h e p o s s i b er o n g e s o thehypothetico c o r r e s p o n d i ntg b o s el O flooting p o i n ls c h e m e described 4 2. in Exomple Minimum Smallest Maximum
l,,\l
9 . 9x 1 0 s Overflow l{
 1 . 0x 1 0 s
1 . 0x 1 0 s
9 . 9x 1 0 s
be
{Overflow ,Underflow "Hole" a tz e r o
88
ROUNDOFF AND TRUNCATION ERRORS
0.01
0.980.99 1
1. 1
1.2
FIGURE4.4 A smoll poriion of thenumber linecorresponding to thehypotheticol boselO flootingpoinl n E x o m p l4 sh o t s c h e md e e s c r i b eid e. 2 . I h e n u m b e r is n d i c o fv eo l u e t c o nb e r e p r e s e n t e d All other quonlilies folling rnthe"holes" ihese exoctly. belvreen volues wouldexhibit some
rounootterror.
t'rr
I
(
F T p
While we could storea numberlike 0.03125 exactlyby expanding the digits of the quantities significand, with infinite digits must alwaysbe approximated. For example, 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 wv oe utlo cb l ereprcsentedax s3 . lr ) l0 or 3. l. For this case, it represented a relative errorof
"I
fl
9 3.1415 3 .r
3 .l 4 I . 5 9
b
: 0 . 03 12
e/
F Although adding significand digits can improve the approximation, such quantities will alwayshavesomeroundofT errorwhen storedin a computer. Anotherrnoresubtleef'fbct of'f'loatingpoint representation is illustrated by Fig.4.4. Notice how the intervalbetween numbers increases as we move befween ordersof magnitude.Iror nurnbers with an exponent of  I (that is, between 0.1 and I ;, the spacing is 0.01. Once we crossover into the rangefiom I to 10,the spacingincreases to 0. l. This meansthat the roundoflerror of a nurnber will be proportional to its rnagnitude. In addrtion, it nreans that the relativeerrorwill havean upperbound.For tlrisexample. themaxinrum relativeerror would be 0.05.This value is calledthe nuc'hineepsilon(or machine precision).
p
S
R tl b
u tl tI
As illustrated in Exanrple 1.2,thefact thatboththeexponent and significand arefinite means thaf thereareboth rangeand precision limits on lloatingpoint Now, representation. quilntities let us examinehow floatingpoint are actuallyrepresented in a real computer usingbase2 or binarynumbers. First,let's look at normalization. consist Sincebinarynumbers exclusively of 0s and ls, a bonusoccurswhen they are nornralized. That is, the bit to the left clf the binarypoint will alwaysbe onel This meansthat this leadingbit doesnot have to be stored. Hence, nonzero binaryfloatingpoint numbers can be expressed as X(ltf)x2'' partof the significand). wheref': thennntisso(i.e.,the fractional For example. if wenormalized thebinarynumbell l 0 l. l, theresult wouldbe l. l0 l l x (2)'r or ( l + 0. l0 l l ) x 23.
T
P "l
4.2 ROUNDOFF ERRORS
Signed exponen 1 1b i t s
89
Mantissa
52 bits
Sign (1 brt)
FIGURE 4.5 The monner inwhich o flootingpoint number isstored inon Bbyte wordin IEEE double
prec s on formol.
Thus.although the originalnumberhasfive signilicant bits,we only haveto storethe four fractional bits:0. l0l I . By default, MATLAB has adoptedthe IEEE doubleprecision.fbnncrt in which eight bytes(64 bits) are usedto represent floatingpoint numbers. As in Fig. 4.5, one bit is reservecj fitr the nurnber's sign.In a similarspiritto the way in which integers are stored, the expollL'nt and its sign are storcdin ll bits. Finally.52bits are set asidefor the mantissa. Howevcr.becausc of normalization, 53 significand bits can be stored. Now,just as in Exarnple.i.2, this nreans thatthenunrbers will havea lirnitedrange and precision. However,because the IEEE firrnratuseslnany more bits. the resulting number system can be usedtbr practical purposes. Ronge. In a tashionsimilarto the way in which integers are stored, the ll bits usedfor the exponent translatcs into a rangefrom  I 022 to  023.The largest positivenumbercan be reprcscnted in binaryas larges vt alue: +l.llll ... llll x ?+10]'t
wherethe52 bits in the nrantissa areall l. Sinccthesignificand is approximately 2 (it is ac l.i9ii x 10108. tually 2  2 t'), the largest valueis therelbre 2r0rr In a similarfashion, positivenumbercan be reprcsentcd the smallest as S r r a l l e sv t a l u e: + 1 . 0 0 0 0 . . . 0 0 0 0 * 2  t t \ 1 2 1 2 . 2 2 5 1x l 0 r t ) 8 . T h i s v a l u ec a nb e t r a n s l a t eid n t oa b a s e  1 0 v a l u eo f 7 r { ) 1 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 ao l r e s p o ntd o a b o u tl 5 t o l 6 b a s e  l 0 digits. Thus,z would be expressed as
' "pl
.1ll S 
formaL
Iong
I. I4I592ri53a891.) Note thatthe machine epsilonis 2 sr : 2.2204x l0 16.
90
ROUNDOFF AND TRUNCATION ERRORS
MATLAB has a numberof builtin functionsrelatedto its internalnumberrepresentafunction displaysthe largestpositivereal number: tion. For example,the rea Zmax
>> >>
Anq
4.
th
CI
nr
format realmax
=
long
t.
9
o9lI
lo02J.oeJtru
Numbersoccurringin computations that exceed this valuecreatean overflow.In MATLAB they are set to infinity, inf . The teaLmitT functionclisplays the smallestpositivereal number:
>> realmirr
\4
IL
rt
n
0 7 3 8 5 8 5 4 i  2O i e  3 0 8 ?..22.5 Nrlmbers that are smaller than this value create an underflow and, in MATLAB, zero. Finally, the e.ps function displays the machine epsilon:
>> eps
L n
are set t0
d
S
o n il
2 . ?.2 A ,!.4 6 0 4 9 2 5 0 3 I I e  C I tj
4.2.2 Arithmetic Monipulotions of Computer Numbers
Aside frornthe lirnitations of a cclmputer's numbersysteul, the actualarithmetic manipulations involving thesenunrbers can also resultin roundofT error.To understand how this performs occurs. let's look at how the computer simpleadditionanclsubtraction. Because base10 numbers to illusof their familiarity,normalized will be ernployed errorson simpleadditionand subtraction. tratethe effectof roundofT Other numberbases would behave in a sirrrilar fashion. ?l simplify the cliscussion, we will ernploya hypothetical decimalcomputer with a 4digit mantissa and a ldigit exponent. When two floatingpointnumbersare addecl, the numbersare first expressed so that they havethe sane exponents. For example, if we want to add  .557 l, thecom+ 0.0434 puter would express the nurnbers as 0. 1557x l0r + 0.00+3ll x l()r.'fhen the nrantissas are addedto give 0. 16004 I x t0r . Now. because this hypothetical 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 0 is l. 0.l600x Notice how the last two digits of the secondnumber(41) that were shifiedto the right have essentially beenlost fiom the computation. is perfbrmed Subtraction identically to additionexceptthatthe sign of the subtrahend is rcversed. Forexample. suppose that we are subtracting 26.86frorn 36.41.That is, 0.164x 1 102 0.2686 x 102 x l0' 0.0955 For this casethe resultmust be nornralized because the leadingzero is unnecessary. S0 we mustshift thedecirnal one placeto theright to give0.9550 x l0' : 9.550. Noticethat
I
I
(
4.2 ROUNDOFF ERRORS
9l
is not significantbut is merely appended to fill the the zero addedto the end of the mantissa empty spacecreatedby the shift. Even more dramaticresultswould be obtainedwhen the numbers are very closeas in 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 the classicexampleof how the mannerin which computers can leadto numericalproblems.Other calculations that can causeproblemsinclude: Lorge Computotions. Certainmethodsrequireextremelylargenumbersof arithmetic In addition,thesecomputations areoften intermanipulations to arrive at their final results. That is, the later calculations dependent. are dependent on the resultsof earlierones.Consequently, eventhoughan individualroundofferrorcould be small,the cumulative effect A very simplecaseinvolvessumoverthe course of a largecomputation canbe significant. ming a roundbasel0 number thatis not roundin base2. Suppose thatthefollowingMfile is constructed: function
n.
sout = sumdemo()
for i  1:10000 s = s + 0.0001; end sout = s; When this tunction is executed.the result is >> formaL long >> sumdemo ans = 0.9999999999999r The f ormat long command lets us see the l5 significantdigit representation used by MATLAB. You would expect that sum would be equal to l. However, although 0.000 I is a nice round number in base10, it cannot be expressedexactly in base2. Thus, the sum comes out to be slightly diff'erent than l. We should note that MAILAB has features that are designed to minimize such e:'rors. For example, suppose that you form a vector as in >> format longr >> s = [0:0.0001:1]; For this case, rather than being equal to 0.99999999999991 , the last entry will be exactly one as verified by >> s (10001) 1
92
ERRORS ROUNDOFF AND TRUNCAIION
w e a d da s m a l l n u m b e r , 0 . 0 0 1t0 o,a 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 largenumber, 4000,usinga hypothetical computer with the4digit nrantissa andthe ldigir After modifying the smallernumberso that its exponentrnatches exponent. the larger, 0.4000x l0a x lOa 0.0000001 x l0a 0.4000001 which is choppedto 0.4000 x 104. Thus, we rright as well have not performecl the addition! This type of errorcan occurin the computation of an infiniteselies. The initial terms in suchseries largein comparison with the laterterms. areollen relatively Thus.aftera few termshavebeenadded, we are in the situation of addinga smallquantityto a largequantity. One way to mitigatethis typeof erroris to sum the series in revelse order.In thisway, rnagnitude to the accurnulatecl surn. eachnew ternrwill he of comparable the individualterms in a summation are larger Smeoring. Smearingoccurswhenever thanthe sunrmation itself.One casewherethis occursis in a series ol'rnixedsicns. Inner Products. As shouldbe clearfrom the lastsections, someinfiniteseries ureparticthe calculation is not one o1'the more ularly proneto roundolT error.Fortunately, of series in numerical A lll more ubiquitous manipulation commonoperations nrethods. is thecalc u l u t i o rttr f i n n e rl ) r o d u e (u ss i l l
T
FI
ze
: .trr,vr *,r:.v:*''' * x,,)',, E.tr.t'
in thesolution linearalgebraic This operation is very comrron,particularly olsimultaneous Suchsummations areproneto roundoll error. Cc'rnsequently. to equations. it is ofiendesirable suchsumrnations in doubleprecision in MATLAB. compute ns is doneautorratically
pr fu
do rel
ERRORS 4.3 TRUNCATION
(rrors are those that result fiom using an approximationin place ol an exact Trwrccttion procedure. For example. theclerivative rnathematical rn Chap.I we approx imated of velocj u m p e rb y a f i n i t e  d i f f b r e n e ce quation o l ' t h ef i r r n r[ E q .( l . l l ) l ity of a bungee duA,u
dt Lt
po
idt
u(ri+r) u(t1)
t;"t I ti
(48)
gu wt no
A truncation into the nurnerical scllution because the difl'erence equaerrorwas introduced (recallFig. 1.3). To gain insight into tion only approximates thetruevalueof thederivative we now turn to a rnathematical theproperties of sucherrors, tormulaticln thatis used widely furrctions in an approxinrate fashionthe Taylol series. in numericaln'rethods to express
Th
.f ar ber ilar
4.3.1 The Toylor Series
Taylor'stheorem and its associated formula,theTaylorseries, is of greatvaluein thestudy statesthat any smoothtirnction of nurnericalrnethods. [n essence. the Tay'lorthertrem can be approxirnated as a polynomial.The Taylor.renes then providesa meansto express this in a form thatcanbe usedto comeup with practical ideamathematically results.
fac on tior
leg
respect to thc baseof the hill l(. At first..4. a perf'ect if the functionbeing approxirnated is. indicates that the valueof I at the new point is the sameas the valucat the old point.if thc functionchanges at all over the interval.what would be your best guess you haveno ideawhatsoever at the heightat x..11'? If you think aboutit (remcmbcr what'sin front of you).ond secondorder Toylor series exponsions. r i + r )] .).You immediately sense that the front . So now you are allowcdto get ofTthe plattbrrn and standon the hill surface with one leg positionecl in tiont of you anclthe otherbehind.9.eroonler uppntximatiorz.w h i c h i s a d i s t a n c / cr a w a yf r o m y o u . r r . For our problcm. r . which is calledthe7.6 l by way to gain insightinto the Taylor scriesis to build it term by terrn.r'(.O l 5 .11 erecloseto eachother. the bestguess would be the sameheightas whereyou're standing now! You c<lulcl express this precliction rnathcmatically as l ( .r ) : zero order.a 2 5 x * l 2 o t r : n{ / ( .'3"^ ri=0 h r.additionaltermsof theTaylorseries arerequired to providea betterestimate.This resultmakesintuitivesense because if r. you areplaced horizontal so thatyou haveno on a platfbrmthat is completely idea that the hill is slopingclownaway fiorn yon.+t : 1 T h eo p p r o x i m o l i o o O l .it is likely thatthe new valueis probablysimi l a r t o t h eo l d v a l n e . You are givcn the task of pledictingthe heightat a position .r. FIGURE 4.r. andr. in Equation(4.r r O 5 .6).A good A usel'ul problcrnc()ntext is to predicta functionvalucat one point in ternrs tbr this cxercise of the point.3 TRUNCATION 93 Zero order Jk.first order.th /(. However. 11 .r a. 1 ( x r ) G. At this point.ERRORS 4.you would be right only if you happened a constant.1) \. to be standing on a pcrfectlyflat plateau. provicles estinrate f'act. firnctionvalueand its dcrivatives at anothcr Suppose that you ale blindfblded ancltakcn to a locationon thc side of a hill facing We'll call your horizontal location with downslopc Jriand your vcrticaldistance 1Fig.9) This rclationship.n):fk) \:[:*':d::s = ^xt .
you're allowedto obtain a quantitative the slope by measuringthe differencein elevationand dividing it by the distancebetween your feet.we could keep addingmore derivativesto capturemore of the function's expansion Taylorseries vature. Thus. l ' G i + ) = . The Taylor seriesprovidesthe correctway to do this as in and make it into a parabola.rr)is even lower than your previouslinear prediction.Thus.Consequently. and moving the other one forward Ax.l0) can predict a change. the distance ofthe or decrease now in the form of a straightline that is capableofpredicting an increase J.You can express (4. slopebehindyou by keepingone foot plantedat x.So now you the First.it is only exactfor a straightline trend. andt. In fact.u)h f (.e Qi). n ' * " f ' ' .fiQt) .1. the second change of the slope. cut' Clearly.I l). f ( h ) = a 2 h 2I a 1 h a 1 y thefunctionwith a second' Taylorseries approximates Thus.t') (n the derivative. the expression a slope/'(.it at the prediction quently.L. Let's call this slope that the slopebehindis milder than the one in front. (4. + / ' ( . * J"lr'l L : ^2 No1 sig acc wht and app ide non SOIC con den yie inn eno req Thi SOIT .1. the only unknowns are the values is a quadratic equation of the tbrm r . they are numbers.f (xi+). .. valuesthat you have estimated.r). Recognize Before proceeding.fi.firsrorder approximation is between . term to your equation you're now going to add a secondorder As you rnightexpecf.11. by this predictionmathematically . With this additional information. . and moving the otherone back a distanceA.{ 4 . . .r.Let'scallthisslo f lp . subscripted r represent positionx.1 t ' l r .94 ERRORS AND TRUNCATION ROUNDOFF estimate of foot is lower than the back foot.+ f ( r . t ' ' G i ) +/ ' ( r .j J . recognize .11.you needan estimate estitlate it as you to determined lasttwo slopes . You immediately Clearly the drop in height is "accelerating"in l'rontof you. rn oft lou onl trat c .In essence. r ): f ( r .To get a betterprediction. or linear.we can seethatthesecondorder orderpolynomial. ' + " * f " ) t t i t r ' + R .a1. functionbetween .Thenyoumeasuretheslopeinfrontofyoubykeeping one fbot planted at x..f'(. 1 3 1 .f (. Thus.Qi) Ax CITC (4.. .r.' (o. Conse' That is. derivativeis merelya derivative Thus.r.10) = *.we arive at the complete wht The this plv bas wil ter l .. you measure are allowed to standon the hill surfaceand take two measurements.ri) of the additionalllrstorderterm consists because This is calleda.fi(.You canuse of the second To makeuseof this fbrmula.) multipliedby /r.ri+r) "f(.and x. (.. of a derivative.we needto add more terms to our equation. the odds are that . f" ( r i + ) 7 JQ con . r2) wer ber the rate of in this case. Although Eq. that all the values let's look carefullyat Eq. you're clearly in a better position to predict the to projecta straightline out t0 you use the slopeestimate heightat .
l1) fbr the ntholderapproximation n connotes that this is the rernainder wherethe subscript .13)is an infinite series. . the nthorcler suchas exponentials and sinuand continuous functions. In many cases.we can now seewhy the Taylortheorem provides to express this a means and that thc Taylor series approximated as a polynornial ideamathematically. e( 1 .+l g. o1'the that multiderivativcs implics nothingregarding the magnitudc this approximation rnethods error of nurncrical ply firr+t.1 . ..the practical Althoughthe tbregoing in anapproxirnatio th na ti s c l o s e o l ' o n l ya f ' e r v t e r n . /(.I l). ( 4 . is that. Note that because terrn is also includedto sign that was used in Eqs.3 TRUNCATION an equalsign replaccs the approximate Eq. errols. / ( . the following exanrple.a finite nunrber o1'terms r ill be slight. l E exptes. w c n c c dt o d e t e r m i n t ch c r.. 1 .l4).seda.nn * I to infinitv: R . the ellol is proportional to thc stepsize/r raiscdto the (rr * l)th power'.. r ) .This is because (r control fiorn. will be exactfirr arrrthordcr polyTiiylor series expansion In general.l .1 s o m e w h e rb ce t w e e n t . onsequeniy . i s b e h a v i ow t . (4.{ is not known cxactlybut melely lics This relationship E q . valueof Taylorscries expansions is true. llrere would be no neeclto pcrlirrrnthc Tirylor scrics expansionin the present ! contexf i 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 . S c c o n dt.ical (Eq. we rlo havecontrnlovcr lhc term ir in thc cquation..anclother of tcrnrsto the Taylor series. Thus. norlial. soids.e states that any smoothfunctioncan be Thus. and. rw si l l r e s u l t in rnost c a s e st.J / t / / + ll l i \ l'7.if the crror is O(fi).1.: O ( l t " ' ' ) t h a tt h e t r u n c a t i o c nr r o r i s o f t h c o r d e ro l / t " * i . For otherclifl'ererrtiablc Eachadditional term will will not yicld an exactestimate. halvingthe stepsizc based on Taylor scries exparrsions. to obtainan adequate only a tew termsare required tratedb_\.s r n t h ee x p a n s r o r C r.r). is a valueof t'thatlies somewhcre between and.l ) .t'.o w e v c r t o t h ea p p r o x i r n a t i oT s o m ci m p r o v e m e nh contribute .9) through (4.q . (4. 1 ) D e s p i t et h i s d i l c n r n r aE In otherwords. The assessmcnt ol how many tL'nxsare cnollghto the true valuc lilr placl. sn'rall. q . on the remainder tcrrnof the expansion is bascd required to get "closeenclugh" hastwo major drawbacks. First. . ( .3. 4. H o w c v e r .\.h a l v i n g s t e ps i z ew i l l q u a r thrw i l l h a l v ct h ee r m r rO .judgingthe comparative Fur cxample. n t h c o t h e rh a n d . nh . 1 ) i s u s u a l l y b e r < r f ' r e r nw r sc i n c l u d e R . il'fi is sLrfficiently the first.+ r . ( 4 . of the error'. lbr a dispropoltionately high percent ternrsusuallyaccount lower'order This propertyis illusapproxinration.w e n e e dt o k n o w . by the addition thatthe truncation erroris decreased we can usuallyassume In general. T o d o t h i s . ' * ') m e a r t s w h e r ct h e n o m e n c l a t u rO Although That is.lifldr. alc addcdwill thc serics Only if an infinitenurrbcrof terrns in Example demonstrateci y i e l da n e x a c tr c s u l t .ERRORS 4. ter the error. i f t h ec r l r r ri s 0 ( f i r ) . A remaincler accountfor all terms fror.r'wc wiurlto evaluate and we cillt Ihenttntwe canchoose how lar awav ).h ei n c l u s i o n purposcs. it is extremclyLrseful in.o c v a l u a t e ( r r+ l ) t h d e r i v a t i v e i f r v ek n c w o l ' / ( r ) .
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 the estimate' eachadditionalterm resultsin some improvement Therefbre. / " ) . enor the terms will reduce .ran improved with le.r sin r cos.3 EXAMPLE ERRORS AND TRUNCATION ROUNDOFF w i t h o T o y l o rS e r i e sE x p o n s i o n A p p r o x i m o t i o no f o F u n c t i o n with n : 0 to 6 to approxirnate Problem Stotement.96 4. Use Taylor seriesexpansions . the inclusionof additionalterms results nrate. which has le.t s i n. the error is reducedto 0..4()1154491 2 \r2l "' estiir.l:4.0267a' tinre we have aclded more of althoughthe addition of the true value.974o/a negligible' becomes irr.Thepfocesscanbectrntinueclandtheresultslistedasin Order n * Gr fr"(x) cos. 4 B si t ( i ) = ' " . .5. Our knowledgeof the true tunction meansthat we can determine vauef(r13):0.( i ) : o' o ' r 0 6 7 8 l a percentrelativeerror tlf which represents er se :rt o* : .'s(. .)\i) /z\ /r\ /r\/r\ cos(]r/4(l\':0.l:0.44g%. we have attained99.(4. i n f 4 ) ( . s 2 r e 8 6 6 s s 'r\/r1 /\4/\12l \rl derivawe add the second approximatitln.+ ) : 0 . lu_L9#Sl'or* term where..Consequently.f'(x) : sinxl the first clerivative we adtJ approximatit'rn.9)l 4. Thus.400/o. For the seconclttr<ler : "('r r: cos ) tive terln where/ AI (jfl ca ap t(i):c.'.rprovement the turther. bythe case' For this comeswith the initial terms' alsonoticehow mostof the improvement that means which the thirdorderterm.)'"'. For the firstorder It sll \ = .f(r):cosxat'rr+t:rl3onthebasisofthevalueof/(x)anditsderivativesat thath : r13 * nl4: Tl12' x i : t l 4 ' N o t et h a t t h i sm e a n s the correct Solution. r COS Y l'0r/t1 o 7 a 7l 0 6 / 8 l 9B6e'59 0 521 o 497754491 c )4 9 9 8 6 9 1 4 7 0 50000/551 0 500000304 0 499999988 le.l 414 440 a.Thezeroorderapproxinrationis[Eq.
of this prediction.l^''h3+.3. (4.14). the Taylor seriesexpansion Suppose that we truncated [Eq. L l s e i p s 6 r c f l .One It is obviouslyin. r o r . f"tv t f{l'tt t .nt to deallith the remainder in this inflnite series itself. 4. format. we will use a simple./(./(ti) . . )t.2 The Remoinderfor the Toylor SeriesExponsion how the Taylor seriesis actuallyusedto estimatenumericalerrors.det GrophicoL depiclicn cf o .ERRORS 4. The remainder. (4. 1e at re . d l (xl Ll. t d . or A visual depictionof this zeroorder of the iniinite error. the derivative mennvalue states that basedon a grnphical thertrem FIGURE 4. As in Fig. I 3)] after the zeroorder term to yield ].as in simplification might be to truncate the remainder = R11 .visuallybased explanation.onu"Ji. : r .as statedin the previous section. which is also shown in the iliustration. / ' ( .r. symbol a p p r o x i m a te eq u a l i t y employed the approximation into an equivalence is An alternative simplitication thattransfbrms insight. 4.1s) lowerorder for a Although.this resultis still inexactbesecondterms.. derivatives usuallyaccount greatershaleof the remainder than the lrigherorder terms. r ' r o ' ) . \ it h+ + h 2 + ' . Before demonstrating we must explainwhy we includedthe ar{ument f in Eq. This "inexactness" is impliedby the cause of the neglected andhigherolder ( 4 . .To do this.+r) predictionis shown in Fig.['(x1)lt (4.3 TRUNCATION 97 4. consists seriesof terms that were truncated R r:r.7.T o .7 renor. ( : ) in Eq.8. l5)..
.v o l ule Tht the we tlor if a functionl(r) and its first derivative arecontinuous over an intervalfrorn.16).valuecorresponding derivative to the second thatmakes Eq. it may not be clear to you how the expansion to can actuallybe applied wher.8). this { marksthe r valuewhere slopeoccurs(Fig.8 heorem G r o p h i c o ld e p i c t i o n o [ t h ed e r ] v o t i vm e e o n .l{.14).3. (4.t''' Thu tion shou expe (416) 4.r. to. or to divided .) The parameter and.l7) exact. f'(t): + whichcanb.the value of { confbrmsto the r. ilurr. matic and i .98 R O U N D O FA F N D T R U N C A T I OE NR R O R S .. (4. . by the run h.3 Using lhe Toylor Seriesto EstimqteTruncotionErrors will be extremely usefulin estimating truncation enorsthrough' AlthoughtheTaylorseries out this book.designated by /'(€).4.8.*. it is simpletu realize that.ng.f{x} n R o1 Nr Eq h FIGURE 4.:r.l(.theslope By invokingthis theorern. two pointswith an average therewill be at leastone momentduringthecourseof the trip when you will be moving at that average velocity. thatis parallelto the line joining.r. (4. Li Equ ence r4l7r For this case.A physicalillustration is that.t)h Ro.. versions canbe developed from Eq. 4.ri+1.we havederived the zeroorder version of Eq.3 versions f'hus. The higherorder to derive Thefirstorder aremerely a logical exfension ol'thereasoning used Eq.).l'(6) R. then thereexistsat leaslone point on the functionthat hasa slope. version is I"(F) R1 : :7.il'you travelbetween of this theorem velocity. in Fig.la) Similarhigherorder 4.(a.as illustrated is equal the rise R.oto {ive (.
l 8 ) t.becauseoftheTaylorseriesapproach.ERRORS 4. thederivafiveinExamp1 le with this approximaof the trtrncation error associated we havenow obtained an estimate tion of the derivative. and .x.  ti) or R' ti+t. In other words.3.l8) can be solvedfbr Equation rl I I ( 4 .) T h u s .(4..finitedifJ'ergenerallyas ence.(4. r i + r ). the length of the interval over which the approximation is rnade.2 was to by Eq. * . we have alreadydone so in our exampleof the bungee predict velocity a function I I as Recall that the objectiveof both Examplesl.f 9) is given a formal label in numerical methodsit is calleda.2[Eq.. ( / i + r:) u ( t ) * u ' { / 1 { fL I . I .. .F R. Now let us truncatethe seriesafter the first derivativeterm: r /i) * Rr u ( t i + r ): u ( t i ) f u ' ( / r ) ( t r * (4. 11'* r .21) where /r is called the step sizethat is.19.) .4 Numericql Differentiotion Equation(4.l9) yields rRr ti+t li : (ti+t 2! u"(6).ri+t. numericalmethods.lt can be represented f '(.However. l ( . expectto halvethe errorof the derivative.In fact.4.3 TRUNCATION 99 jumper.ri\ : . u(l) in deterrnining u(/). l l ) o r t h e l l r s t p a l t o f E q . the error of our derivativeapproximation shouldbe proportional if we halvethe stepsize.t h e e s t i m a t e o f t h ed e r i v a t i v e [ E q .t i ) z+ " ' .fi) Ji+t r. f O(ri+t . That is. r . 4.   u(ri+r)u(ti) R1 ri+l ri  {i+l  {i (4r .(4. f ( r i ) + o( h) (4. Using Eqs.20) f '(ri) : .( l .) * .f\ri+t) /(. ( 4 .(l.9a). It is termeda "forward" difference because it utilizesdataat i (Fig.we would to the stepsize. and i * I to estimate the derivative .ri) (1. 13). .l4) and (4.Consequently.l 9 ) ] h a sa t r u n c a tion error of order ti+r ti. we were interested in a Taylor series: can be expanded .9) Firstordcr appfoxr nra(ron Truncatiorl e[ot that was usedto approximate is exactlythe samerelationship The first part of Eq..Ii :O(tirrl..ll)l. As specified of time.
9 finitedifference depiclion Grophicol of (o)forword. 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 .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. ond{c) centered {b)bockword.
l(xi+r) whichcanbesolvcd for . . A third way to approximatethe first derivative is to subtractEq. as in f h f t x i . 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. 4.9c)..backwardand centered difference approximationsof the llrst derivative can be developedin a fashion similar to the derivationof Eq.) . (Fig.For example.21) .\  * " f ( . (4. all the foregoingversionscan also be developed for second. 4.f'r)(". (4. l ' ( r i*) l ' ( .l ' ( x i f t + ::'t Truncatingthis equationafter tlre first derivativeand rearranging yields (4.f(xir) (4.25) is a centercd representation of the first derivative. The following sections provide brief summaries illustratinghow someof thesecases are derived.Consequently. r*i + J'(xit ffn'+'.9b for a graphical representation.19).r.l(ri+r) . SeeFig. * J ' ^ l ( * ' \r t * . whereasfor the central difference.re.if we halve the stepsize using a forward or backward difference. b 2h t't".the error would be quartered. Bockword DifferenceApproximotion of the FirstDerivotive..22) 1 ' G i )= J (ri) .' to yield : .3 TRUNCATION ERRORS 'I'his rol forward differenceis but one of many that can be developedfrom the Taylor seriesto approximate derivatives numerically.4.9b). Centered DifferenceApproximotion of the First Derivotive.. The Taylorseries can be expanded backward to calculate a previous valueon the basisof a present valr. Finally.22)from the fbrwardTaylorseries expansion: .1 and.More accurate estimated approximations of the first derivativecan be developed by including higherorder termsof the Taylor series. 4.23) wlrerethe error is O(/z). r i + r ) l ( . whereas the latter uses valuesthat are equally spacedaround the point at which the derivative is (Fig.4.25) Equation (4.). third. and higherderivatives.l''(x) : (1.9c). r . For exarnple.t 2 l ' ( x . f ( x i ) .r t : J ' ( x t t. The former utilizesvaluesat r.we would approximatelyhalve the truncationerror. the Taylor seriesanalysisyields the practical information that the centereddifferenceis a more accurate representation of fhe derivative(Fig.fGi t) . r i _ r )2h o(t) (4.
6363281 T EI = l''(0.925.5 le.5) * difference lEq. 1 1: 0 . r a .0.925 : 0. r r.(0.5 usinga stepsizeft : 0.r): . l: 5 8 ' 9 % l''(0.l:9.0'925.f @) :0.55 0.l:3g"7o/o clifl'erence IEq.4 I the backwarddiff'erence' .l'2:0. Soution. (a.t.l55 0.5 r .'Eo T rh difference' andthecentered 1 . (a'25)l' and the centered 0.25 le . Note thatthe the comprrtation ar r : 0.l : 21.I :0.0'925 l : '15 = l e .5) :l.0 .1 0 3 1 5 5 6 3 0.10351563 0.0 f (xit): l'2 .925 l'(ri+t):0'2 the forwarddifference [Eq' (a'21)J' Thesevaluescan be usedto compute o'2 .0 .4't3 .5)= ff Forft : 0.5. l ( r i * r ): 0 ' 6 3 6 3 2 l8 dif'ference' thefbrward to compute whichcanbe usecl 3 . 7 5 1.1 .2 1.5"/o 4.r02 4.0 ' 2 5 ' t. l . For h:0.6a/<.925 0.r.25 .0 le.5):o.25.s) 0.l''(0.5 V .25 lt. r. : 0. 6 3 6 3 2 813 : { \\ : = f/ '(o OS 0.1 11 = l''(o. l 5 . \: 2 ' 4 % ' o ca E si c( .5 : rri+r 1.23)1. Use forward anclbackwarcl of derivative first the to estimate n of O(h2) differenceapproximatio a centered J (.934 0 .45x2 andcanbeusedtocomputethetruevalueas/.l:26. t r . the backward /'(05) . Repeat directly its can be calculated clerivative f'(r): .l(rir): l'10351563 J'Qi): 0'gZS 3 . ri t:0 to determine the functioncanbe employed .0 . : 0.1 ' 0 x.1.5. . 5 .4 EXAMPLE ERRORS AND TRUNCATION ROUNDOFF of Derivotives Ae pproximotions FiniteDifferenc of o1/r1and approximations clifference problem Stotement.0 ' 2 5 0.0.t 1 ' 2 usingh:025.
r i ) h2 ././(ri): in te . / ' ( r ..we havenotedthat roundoff error may int:rease due to subtractive cancellation or dueto an increase in the number of computations in an analysis.just as the second derivativcis a derivative ofa derivative. / ' ( x i t ) hh f ' "( r i ) 1 Thus.rrt* " (4. / ' ( .):ff+o(h) and a centered version . .26) (4. thecentered caseis moreaccurate. .S i r n i l a r manipulation cs an bc cmployedto derivea backwirrd version .r i+ ) rmsof.Because in stepsizecan lead to subtractive a decrease cancellation or t.oarl increase in computations. f ( .t. + r ): .2 + o(h) (1...4 demonstrated that the truncationerror can be reducedby decreasing the step size.26)to give . In contrast.the centereddifferenceapproximationis more accurate than forward or backwarddifl'erences.finite diflerenca. FiniteDifference Approximotionsof Higher Derivotives. Further. .2 . halving the stepsize approximately halvesthe error of the backwardand forward differences and quarters the error of the centered difference. Example4.ri):ff+oft2) . * 1 ): which can be solvedfbr .27) This relationshipis called thc second. To dothis.1 (ri 3)  . Irecall 4. 1 ' ( ./'(tr . the truncartion errors aredecreased as the roundofTerrors areincreasetl. 7 ' " 1 t 1 1+ h. l ( . Besides first derivatives. " \. r i + t )* . Noticealsothatthecentercd vcrsioncanbe altemativelv cxnrsssed as l ( . the Taylorseries expansion can be usedto derivenumerical estirnates higher of derivatives. [ ( r i ) + . t il I l ( .1"(r.. the only way to minimizeroundofT errorsis to increase the numberof significant figures of the computer..24)canbe multipliedby 2 and subtracted Equation from Eq.wewriteafbrwardTaylorseriesexpansionfor.): I ( r i + : ) . the second finite dilfbr(a.4 IOIAL NUMERICAL ERROR r 03 For both step sizes..2 . r i + r ) .)*. r i r )  As wasthccascwith thefirstderivativc approximations. r i )_ I G i ) . l ' ( r i ) I + J'lxi)t2hl '")r''' .unl.r. (4. In general.2 / ( ./"(r.f ont...)2.4. l2)1./{. r ) . t . Also. 1 ' ( r .f(. enceapproximation is a differcnce of two first finite differcnces Eq. .4 TOTAT NUMERICAT ERROR The total nnnerit:aL error is the summation of the truncation and rorrndofTerrors. l ' Q i + ) . as predictedby the Taylor seriesanalysis.
. We erplore sucha caseirr thc lollowing section.r. 4. error t i t fbr decreasing Therefore.If the total error is as shown in Fig.4.our remedybecomes that we laceis our problem(Fig. 10.5 R P c 4. 4./'(t)(6).the is to identily the point of dinrirrishing returnswhere roundofT error begins to challenge negate the beneflts reduction.ti r) .1 (. suchsituations uncommon of its l5. shown. I O thotsomelimes roundoff error A croohlcodeoiction of thelrodeoff between ond iruncotion methodThepointof diminishing relurns is coiresintoploy in thecourse ol o numerico whereroundoff begins to negole lhe benefits of stepsize reduclion.25) l P b R ire . I .rlations and roundoffcrrorswithout incur a largestepsizeto decrease ring the penaltyof a largetruncation error.f'(. if the two functionvaluesin the numerator of the finitedifference approximation have no roundoff error. do occurandsuggcst principle"thatplaces certainty an absolute limit on theaccuracy that rnaybe obtained using methods. they sometimes undigit precision. ln a cornputation. Wc'would like to choose to determine an appropriate the amountof calcr. l0). the roundofferror beginsto dominateths solutionand the total error grows!Thus.3.4.): True value J(ri+tl . certaincomputerized numerical EXAMPLE 4. 4.r04 ERRORS ROUNDOFF AND TRUNCATION Point of diminishing returns o J Log step size F I G U R4 E. D E Thus.a centered As described difl'erence approximation of the first derivative can be writtenas (Eq. of the totalerrorleads to an increase component we couldconceivably thc stcpsizeto minimizetruncation errorsonly to discover clecrease that in doing so.4. we are facedby the following dilemma:Thc strategy one of the othercomponent. One challenge stepsize1bra particular computation.1 Error Anolysis of Numericol Differentiotion in Sec.' 116 (4.28) 2h Finitedifference approximation f d H fr Truncation error h. of stepsize bccause WhenusingMATLAB.to I6arerelatively a sortof "numerical Nevcrtheless.the only error is due to truncation.
. t'. /(.E . Further. H ( I ) =h. (4.: " 6 True value Finitedifference approximation RoundofT error Truncation error consistsof a roundoff approximation We can seethat the total error of the finitedifference with stepsize. .rl 2h Pi t . n I G.r. cLfunc.29\ Eq.4.ri t) .o . In Example4. We can develop as arguments: derivative its analytical function and pass the we both that Notice results. / ( x r r ) < _e _ .29).. n) format df t r. x.30).r i + r ): .'r a. 5 E X A M P4 L.5.Substituting ?t.0 .30) E 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 ond Truncotion Roundoff of approximation diffbrence Problem Stotement. that decreases with stepsize and a truncationerror error that increases error has an roundoff of the Assuming that the absolutevalue of each component 2e.) .28)gives these errors. h1./'t_r.ERROR 4.4 IOIAL NUMERICAL r05 However. becausewe are using digital computers. themaximumpossible boundon M. ) . l .settingthe result by diffbrentiating An optimal step size can be determined equalto zeroand solvingfbr ('1. (4.0 . value true that the Recall Eq.. [ 1 r . 2 divide the stepsize Peribrm the samecomputationstartingwith ft : l. funcLion di ffex 1ong ( func. €iwill be €i+rvalueof thedifl'erence upperboundof e. r r . we useda centered : 0. Then progressively step sizeis reduced.r1A.ue=df unc (x ) . your results to Relate and plot the the following Mtlle to perfbrmthe computations Solution.0 ./'l'(€).r +_ h6 l.r fbllowing of the the first derivative O(h]) to estimate l t ' G ) : . f ( . ) / (2*h) . .e the rounded function values and the e's are the associated valuesinto Eq.l lxit: f(*.f(xi t) : f @i.the function values do include roundoff error as in t eit . function at. l 5 . as the dominant becomes how roundoff I to demonstrate of 0 by a f'actor 0. An upper value of hasa maximumabsolute thtrtthe third derivative assume as value of the total error can therefbrebe represcnted the absolute t T o t ue l rror: h2M  .9 125. is of the derivative (4. 5 1 2. 2 5 x 1 . r a . f ( x i + ) I e i + r roundoff where the . D(1) = ( func (x+h) func E (1 ) =abs (dftrueD(1) (xh) ) .
r_ 0.> > f f=G(x) c 1 f = ( .r'uLr error I . 1 r . ('%14.2b.0r100000000054 0 . E) . ( r ( ) 0I ( l o L r ( ) i ) r l l ) .2. !ll2 5J i00000011(r ll. H(r)=5.) o. M : I l ' t ' t ( 1' .1 f .r0 6 AND TRUNCATION ERRORS ROUNDOFF i rr i .: t1.) .')12 500llr00000I i l . [ )( ) { ) 0 0 U1 ( )( ] i ) 0 .0000 10 0 r ) r J 0 0. t t 2!. As clepicted in Fig. . as in Eq. 0 r ) [ )( ) 0 t )0 U 0 1 . 1 0.ll) :trp :.91 2!t)000000b4.l(il uF func(x  )).First. H o w ' c v e r .l 0 d i g i t s a .15*r^30. (r x ) L l.we canestimatc M by evaluating t h e l ' u n e l i o n 'ts h i r t ld c r i v l r l i v c us .r93 0. 2 .0il(l0l 5000000t) ) Ol!t)000t) 0. wc czur also investigate whctherthescresults arcconsistent with Eq. ..912. l l l t ) ( ) 1l 0 0 0 0 0 0 0 0 0. A rninimum at /r: l0 t'. =[ l  L)' E' I ' . U(r0t0 0 .sc w'eafc dcalingw'ithan easilyclittbrcntitble l'unction. 1 1 0 1 1 6 6 3 0 ( )1 l 36 1 0.u.3 x l0 (' ('ohtuined withMATLAB.5 I )l 1 For Thr apt mu pro eng B e c a u sM c A T L A B h a sa p r c c i s i o n 0 1 ' a b o ult5 t o l 6 b a s c .500u t 9 98q44 U. r .. 12 5 O ( t r ) (I l i 160'r a .izc f inite c ii 1 f e t .14f \16.():2.10f lprintf %]5.. D(r1= (func(x+h) (.0. !. w e s e e tactorol'I00eachtiniewcdividc roundol'f'error bcginto crcepin andcrodethc rateat whichthecrrordirninishes.29).4*x^ i 0. 0 0 ( ) 0 ( ) ( l ( ) l l 0 r )  0 .00utJ000000112 0. 0 0 0 l .l4 9 t j c ) ' ) q 0. 0 ( j t ' r ( ) t l L l t ) u t . O(r()()000(l( I) Q.the crrol irrcrcascs crror is reachcd as roundofTclominates. 1 r j. . ! ) l : 5 ( ) L r l r { ll .') IormaL sholf errol'\n'). l .I ri) 0 0 1 :0 ) ( r i l0 t l 0 0 0 .0000000199894 0. r . o r \ / c r .2 . Becuu.L) .0(l 10000{)(r0 A. q45(r l I 0 . ) !. { i (  ) 1 ) { ) ( ) ( )3 t) 1i. j 9 ! r t r ' . rrouge th hstimateo fe upperboundorl rounclofl'would bc abollt€r:0. 1 * x ^ . r l L . h) ) / (2*h\ .r 0 0 ( . .Bcyondthis point. r .lif\n'.J0000075ir006 0. xl abel ( 'SLcp Si zc' ) .!*x^2 0.. 4 9 / 0 . n h = h . l ogLog (H. ..r11e('Fla'! The Mflle can thenbc run usingthe tbllowingcommallcls: . 3 0 ) l0r(') :4. equ you pos and first w h i c h i s o n t h es a m e t l I x l0 o r d e ra s t h er e s u l o usir erro lem.0.Srrbstituting into thescvalues gives E q . 2 1 1 0 ..( 4 . end 1 . fprincl(' finite diflerenre srtrp siz. t4. 0 0 0 0 0 10 0  0 ( l . 0 ( ) ( l l r 0 0 0 L( l o 0. Hcnce. (4. 0 0 0 ( 10 0 0 f { l ( l I .n c c t. I t l f l r r ( l 0 L l 0 r ) ( l 1 . 0. At l'irst.30). s t a r t i n g a t / z : 0 .5 0. rS L e p Stz.'1 .0f)000000 t 0 0.1 . i r .9 I 2!00(l i 'rb(10!tl 0 . r'stimatc is dclrninnted by truncation Lrror.y Label ( 'Error' ) rt l ..5 x l0 r('.the totalerrordrops bya stth ee p b yI 0 .45*_r^2 x (1.4.l . 0. gra. x (f f . thc rcsLrlts foundol'f is minimalandthe arcasexpcctcd.25*x+1 . t l 21000rJ00 l l I ! 0. .rr)( 1 000trr1 0. > : ' c l i f I r .I I.
which makesour numerical The exception. approxitnations unnecessary. If this is not possible. you may wirnt to use extendedprecision Furthermore. Theretbre. itigate uating practical Forrrrost with numerical rnethods. In many ciises error estimates ale based on the expericnce andjuclgnrent o1'the engiueer or scientist.4 10 .Sometimes you canrearrange or refbrmulate theproblenr to avoidsubtractive cancellation. numbers andsubtracting it is bestto sortthe numbers and work with the srnallest first.4 TOTAT ERROR NUMERICAL r07 FIGURE 4.2 Control of Numericol Errors rd the .4.4 Step size 4. when adding arithnretic.The Taylor seriesis our primary tool fbr analysisof such errors.4.8 1 0 1 0 10 8 106 10 .bya /e see imum rtes. and general numerical Thereare no systematic approaches to evaluating errorslor all problems. numbers. it is usually attempted : of the es into . for rnost engineering andscientific applications we fbr someestimate o1'the mustsettle errorin our calculations.6 t! 10 . First and fbremost. one can attemptto predict total numericalerrors using theoreticalformulations. Theretbre. Although error analysisis to a certain extent an art. for only srlallscale tasks. we do not know tlreexacterrorassociated is whenwe know theexactsolution. Beyond thesecomputationalhints. of course.I I Plot of error versus step saze 100 1o2 10 . there are severalpractical programmingguidelines we can suggest. cases. almostalwaysoccurswhen this is done.avoid subtracting two nearly Loss of significance equalnurnbers.Thrsavoidslossof significance. Prediction of total numerical erroris very complicated foreven rnoderately sizedproblemsandtends to be pessirristic.
ln addition.thc thought ol' as lhc srnirllc givcs a numbcI grcrtcr th idtr ca1\.UNDERS.s.t drr r\o\'td . MODELERRORS. Blunders areusually in discussions ol'rrurnerical rlethods. thissource ol'ernlris highly unlikcly. An exampleof a negligible moclel erroris thefact thatNewton'ssecond law doe s not account fbr relativistic This doesnot detract eff'ects. 4.3 For computcrs. This may involvethe ussof two or morc indepcndent problenr canbe compared.andmostblundcrs impert'ection. Step 2: Il' I + e is less th Otherwiscgo to I Step 3: r' : e/2 Step4: Rcturrrto Stcp2 Step5: a1x€ .5. or haveditferentconvergence are extrcmelycritical and may involve When the resultsof numerical computations it is appropriate to takespecial c. or blundcrs. tional strategies. n .Or it n'ray be possible the results back someconditionor equation into the originalequation to checkthat it is actuallysatisfied. 4.therearc usually sirnpleways to check whethcra particular nunrerical methodis workingproperly.l S t e pl : S e t r : I . with most of thenuol'error are not dircctly connected Although the following sources havegreatimpacton the success ofa mericalmcthodsin this book. they can sometimcs techmodelingeflbrt. so that theirresults of thisbook.rnistakes of waysin which thcir occurrcnce canbc nrinimizecl.srelatcto biasthat can be ascribed to incorrrplete mathu'matical rnodels.We may want to try different numeriwhen we changernodel parameters on dit'terent computathat havedif'tbrent theoretical foundations.This can sometimes to substitute as a check. nic.t08 ERRORS AND TRUNCATION ROUNDOFF and try to estimate The tendencyis to pnsh forward with the numericalcomputations be done by seeingif the resultssatisfy the accuracyof your results. erroneous numericalresultscorrld sornetimes be attributcd to rnalfunctions ol'the cornputer itself.thegoodplogramnring tlratwereoutlinedin Chap.ues in thc contextof realworld ist lo As th 4.r. lossof humanlit'eor havescvere groups to solvethesame precautions. they must alwaysbe kepl in mind when applyingnunrcrical problcms.conornic ramiflcations. In theearlyyears ol'conrputcrs. Horve ver. This is nodoubt disregarded we areto a certain extentunavoidable.try aswe may.hernltical rnodeling and canconThey can be rvoicled <rnly by soundknowledge tributeto all the otherconlponcnts of err<lr. tt lle th hit es ifr Ltl\ 4.5.nputatiorral errorsand possibleillconditioned the computations with a difl'erent step size or methodand mentsmay involve repeating analysis to seehow our solution changes We may employsensitivity comparing the results. principles and designyoursoluof fundarnental and by the carewith which you approach tion to a problenr. Wc will leavethcseinvestigations PR be Ju as ca ca w( 4. your to increase Finally you shouldbe prepared to pertbrm numericalexperiments problols. process Blunders can occur at any stageof the rnat. Thus. or input values. ntc ol. Such experiawareness of cor. Throughout lhis book.1 Blunders We areall f'amiliar with gross errors.5 AND DATAUNCERTAINTY BI.(X)10 4. dueto thef actthat. .2 Convcrt the lirlloui 7 I 563 and 3. AI Er i.1 Convert thc lirllowi l0l l00l and 10. I zl. fiorrrthe adequacy of the solutionin Exanrple l. er tn PROBTE 4. arc based cal algorithms propcl'ties and stabilitycharacteristics. irr all sections ancl analysis Thc rolesof errorswill be a topicof cclncern to spccificsections. rnustbc attributcd to humar Today.2 Model Errors Model error.wc discusswaysto chcckthe results of nurrerical calculations. In parbelieve thattherearea number for lrabits usetul ticular.3 areextrcmely mitigatingprogramnring blundcr.
Validatc thc lcsLrlt ' u r t c t i ort. dalarrncertainty Althoughyou mrlst hc cognizant o1'blundcrs. anduncertain clata. Undcr en'ors. suppose we wanted to testthe bungee jurrrps his or hcr velocityalicr a spccring an individualrnakerepeated andthenrneasuring fied time interval. t h c v a l u cc o r n p u t c w d i t h t h c b L r i l t . thesc conditir)ns. estimate thevelocity. Thcrelilrc. rcspcctivcly. If our instmments consistently underestirnate or overwc arcdcalingwith an inaccuratc.r I mir.i ln r i n r i l a lt o P r o b . If suchwere the in Chap. 23 hits li)r thc Convcrt thc lirllowing basc2 nurnbcrsto basc l0: l00r l n dI 1 0 . An algorithrn l h a n t h i s n u n r b e rN .On thc otherhand. wouldundoubtcdly with thcsc mcasllrcmcnts. As such.t1 r r .rhcr ratcdby your' to l. .1. otc thal thc can h cd c v c l r r p c d a quantity thal is srnallct as rcsultyou obtain will clil'l'cr ll'onrthc valLrc conrpLrtcd with l:Sets: l. However.no numerical method will provideadcquatc rcsults.indepcndently rncricalrncthods uscdfirr bLrilcling nroclels can bc stndicd. Olherwisc c o d ca n c t lhosc ohtaincd with rr. Convert the lirllowing bascl{ nurnhcls to basc l0: and 3. wc can studynunrcrical errorswitl'toul crlrlplicating PROBTEMS Writc your own M l'ilcbascd on this aluorithrn to dctclnrirrc hy comparing it with thc nrachinc epsilon.PROBLEMS r 09 scales with the brrngee because theseerrorsare rninimalon the timc and sDace associatcd jumper problern. be cognizant ofthis typeoferror andrealizc model. boththeanalytical andnumerical solutions obtained beYou should that. Bascyour algorithrn on the notionthatyoLrr cornol as thc smallcst nurnbcl that whcn addcd to onc grcatcr huscdon this putcrwill hc unablcto rcliablydistinguish bctwccnz.and wc are dcalirrg with crrorll'cc rncusurcnrcnts. the nulilr the nrostpilrt..5. areranclomly Measurernent by srrnrnrarizing thc datawillr oneor morewellerrors can bc quarrtil'icd regarding specil'ic choscns(atistics lhat convcyas much inlbrrnation as possible character( I) the statistics isticsol' thc data. Challcnge qucstion: Invcstigate t h c r c s u l t sb y 2 logalitlrrn gcne 2:Il'I * e is lesstharror eqrral ol'thenun.5 Although it is not conrrnonlyuscd.r lir in.cro rnd a nurnbcr than l. or hiascd. Forcomputcrs. as in Eq. rr.7). ftrrrlost of thishook. l'actors. would flll taster Thcsccrrorscancxthe parachutist duringsome hibit bothinaccuracy andirnprecision.3 Dofq Uncertointy in thephysical Errorssomctimes enterinto an analysis because of uncertainty dataon which jr"rmper modelby hava rnodelis based.I wouldbe erroneous case.1.but is relatedto velocity and otherfactorsin a diffbrentway. takingthc base go to Stcp3. suppose thatairresistance is not proportional to thesquare ofthe fall velocity. For instance. 3c . ns Uncertainty bc associatcd jumps thanduringothcrs. lovckrp your own 4 .Thcscdcscriptivc arc most olien selected to represent location o f ' t h cc c n t c r o l ' t h cd i s t r i h u t i oo nl ' ( h cd a t aa n d( 2 ) t h ed e g r e e of spread o l ' t h ed a t a . 4 I n a l n s h i o rs positivcrcal nunrhel M l'ilcto dctclnrinc thc srnallest usedin MAf LAts.we will assr"rmc we have a souncl nrodel. workingwith a poorlyconceived 4. zl. thc rnachinccpsilon c can also hc . if the measurements high andlow. we arcdcalingwith a qrrcstion ol'prccision. thatwc havcnot lnadcgross ol'thcsc errors. 1 .MATLAB allows 3:t = t:12 1:Return to Stcp2 nuntben lo bc cxpresscd in single prccisiolr. ( 1.Each valuc 5 : t= 2 x t is storcdin :1 hytcs with I bit lor thc sign.theyprovidca rnsasurc ol'Lhc biasandirnprccision. ( X ) 1 0 1 .5. rrroclcl errors.ifyou are cause ofmodel error. dcvicc. thcn go to Stcp. Wc will retLrrn to (hctopicol'charactcrizing whenwe discuss regression in PartFour.
5771 Try it using 3. 12.9 Thc lirllowing infinitc scricscan hc used to approxivaluc ol'thr' sccontl dclivativc.ri*r : I lirr.05. 2 5 .1.r = l.rS in rnaximutn intcrval.r.5.5sin. '1. to predictl(2) fbr f(r) : In "r'usinga basepoint at.6 For the hypothetical base10 4.2. ackward. Craph all I'ilstderivafive thrcc ing to two signif icantI'igures.15 : 2 usinga stcpsize Prob. Enrploythc zcro. r )c : 'at.t l . . r l < I i t i s k n o w nt h a t rl .lI n'. .I0. l ' ( .r) Exanrple at .2(l Dcrivc Eq.88 I (. rir'scxpiln)ii{)r).1.0. approximations along with the theoretical.rl .tro ERRORS ROUNDOFF AND TRUNCATION 4.1 o\ Yea to s The sen time RcpcatProb.126to 127.Usc 3digit arithrnclic your results Interprct the hasisol'thc remaindcrivativc. I . Dctclnrinclhc highcstolclcr'laylor scricscxpansion resulf .Altcr cachncw lcrm is acldcd.and [ pockr. a d dt e r n r s Starting w i t h t h c s i m p l c sv t c r s i o nc o n e bctwecn (.2.ualto thc absolutcvrluc ol'thc dill'erence thc givcn lunction and thc specilicTaylor series .l.ldigit arithmcticwith mation.zl. Note that the exponcntsrangefrom . on thc perccntrclativc crror.l ProvethatEq.1. zl.r) = l/1 I .and . clcrivativc cxpansion lbr the sin r to cstirnatc use thc Maclaurinscries 4.Pcrlirrnr thc evirluation at .25 and0. l3) with .r : 0. . for eachapproximation.: 0.3xr): is givenby the truepercent usinga bascpointat.l(3) firr + 7x . s o l v ct h c l u n c t i o n l(r'): t . but lirr thc torwilrd divideddif+ + : Sln. at .4.rf r(' .(4.ll Perlbrm thc sarncconrputation as in Prob.1.16 Usc a ccntcrcddillercnccapproxirnation of 0(h2)to the second in cstimate dcrivativcof'thc lunctionexamined .r.2.22 RepcatExarnple4. U s et h c l i r r w a r db .r: l.2.\': 2 usingslepsizes with part (a). I .: 1.15 Usc lirrward rnd backwarcl dillirencc approximations chopping.Determinethe smallcst positivefloatingpoint numbcrs as u. 6r 4. . (4.tt (a) Evaluate thc polynornial cstimalc thc l'irst dcrivativc ol' the firnctioncxamincdin I:.l0 lirr this scricsli)r r : 0. o s . 125.l.l. ) c x p u n s i on at a tinrcto estirnate cos(z/3). 4.3rt).r) : 251r.\ = n 16.r].r.r _rr' 4. ing a crnrr ol'0.0 15 on thc specil'icd C(r\\l I I ) 4'.l UsezerothroughfourthordcrTaylor serics expansrons (I . 151 .7Thc derivative ot l(. 4. dcr term ol'lhc Til) ltrr se (a) hut cxplcssI'as (b) Rcpeat .and 8 bits fbr the signedexponent.r. l. Evaluatc irt . 12. of O(ft2) to ctfO(h) and a ccntercddiff'ercnce apprtrximation l.r . but finitc di{lercncc lix lhe sccond and do thc same as wcll. Compute relative error c.21 3! 5! 7t l'crence ). basisol'thc rcrnaindcr 1 .r if'l(r)= urz+hr+c.10The Maclaurinscricscxparrsion lirr cosr is ll R r: i' t 2. : yoLrr h rcsults with c>l' 0.12 Use zero.rr7r:+u.I I I is exactfbr all valucs of. 6! ttl Thc error is cc.f.f \ (Eq. 10.ell and largcst as the machine epsilon lor single precision represcntation. (b) Uscthc1'aylos l crics t o c s t i n r a t c . tlris Irrnction Computc the true percenlrclativc cror r.21 Repcat 4.l'irst.35 Prob. Evaluutc the clror and cornpare Curnparc your cstimatcs with thctrue ol'/r . H i n t :S o l v cg r a p h i c a l l y .tcalculnloror MATLAB to dclcrrninc Usc _vour thc llnitc centcrcd dil'lcrcnce approximations lirr lhc firstand valucol'thc approxvaluc. computcrin Exarnplc provethatthe rnachinc epsilonis 0.llJ To calculatc wc have t0 a planct'sspacccoordirrates.1. (a) Provc that this MaclaLtrirr \criL'\ rr\piln\irur is a spucial (Eq.r.4 on the intercornpLrte thc truc and appr0xirnatcpcrccnt rclltivc crrors.throughthirdordcrTaylor sericsexpanslons to pre'dic:t. v a l 2 . 1 7 l l l . Compare the true valueof the with r:hopping.Evaluato the clcrivativc at. s i n ( z / 3) .{ r"Iitlr l. 19 Consitlcr thc lirnction l'(r): rr . firl each appsoxiDo you cxpectto havc diff iculticsevaluating Discuss thc rneaning oI thc results.cascol'thc Taylor sclicscxpansiun 0 ancllr : .30).7).r': ((.r 0.Intcrprctyoul rcsults on the n l a t cd ' : tcnr ol'thc Taylol scricscxllunsion.r * 8). 4.612 mantissa. and thirdlirr ortlcrvelsions and contputc thc lr:r cach cascI 1.37. but lbr l( r) : cos(.. .5. r : l . 2 1 w i t h l : 0 . 4. sccond.^ Lct thc bascpoint bc u: xi : rf2 <tnthc intcrval[0. .Add tcrnrsuntil thc ahsolutc trLrc graphically sccond so whichapdcrivativcs as to illustratc imaterrrorcstimatcI'allsbclow an crror critcrionconlirrmproxinration is most ilccrrlltc.r l.ro.
The processis repeateduntil a guess results in anfir) that is closeto zero.as with Eq. (W2. rootsare sometimescalled lhez. they are limited because of their lack of precision. is the root. there are many other functions for which the root cannot be determinedso easily. the roots could be obtainedby direcf methods. (PT2. ln suchinstances.2) :.For this reason.1) LU to solve fk):qf+bx+c:o (Yrz.there were.eros of the equation.For some cases.1 ovERvtEw Yearsago. This point. One method to obtain an approximate solution is to plot the function and determinl where it crossesthe r axis. Although the quadraticformula is handy for solving Eq. ltl ' :.. you leamed to use the quadratic formula *b+4F4ai (PT2. the onlv alternative is an approximatesolution technique. Althoughtherewere eQuations like this that could be solved directly. (PT2.2). This "technique"consists of guessing a value of x and evaluating whether/(r) is zero. Before the adventof digital computers.many more that could nol.l).and/("r) is again evaluated to determine whether the new value provides a betterestimateof the root. The valuescalculatedwith Eq. Although graphicalmethods are useful for obtainingrough estimates of roots.therewere a numberof ways to solve for the roots of suchequations. .anotherguessis made..1) are called the "roots" of Eq..If not (as is almost always the case). (PT2.They represenl the valuesolx that make Eq. which represents the r value for which/(x) : 0.Rootsond Optimizstion 2.2). tPT2..2'l equalto zero.:. An alternativeapproachis to use trial and error.
is.The detennination of suchoptimal valuesis ref'ened to as you As learned in calculus. eting methodsfor finding roots.From a numerical standpoint. terminingthevalue the is that where Although such analyticalsolutionsare sometimes feasible.anotherfeatureof lunctionsof interestto engineers and scientists areits minimum and maximum values.1. We illustrate severalopen methods including theJixedpoint iteration.tonRaph. solutionof most appliedrootsofequations Besicles roots. Root location involves for the locationwherethe functionequalszero.In contrast. such can by deat which function f'lat.f"(r) < o f(x) = o .computer solutions.2 PARTORGANIZATION in this part are devotedto root location. its derivative is zero.or contain.Chapter5 focuseson brackThe first two chapters that bracket.l'('r) = 0 f"G) > 0 PT2.ation. Graphicalmethodsare usedto provide visual insight covered:bisectionand.r) = o .Thesemethodsstartwith guesses reducethe width of the bracket.false Error formulationsare developedto help you determinehow much into the techniques.such numerical optiwejust discussed.rolr.Numerical methodsrepresentalternatives that are also approximate but employ systematicstrategies to home in on the true root. As elaboratedin the following pages. computational effort is requiredto estimatethe root to a prespecified Thesemethods Chaltter6 coversopenruetltods.most practicaloptimizationproblemsrequire nunrerical. level of precision. We will discoverthat iterationsbut do not requirethat the initial guesses efficientthan bracketingmethodsbut that thesemethodsare usuallymore computationally they do not always work. Nev.I FIGURE the difference A funciion belween rootsond oplimo of o singlevoriobleillustrciing methodsareobviouslyinefficientand inadequate Suchhaphazard for the requirements of engineeringpractice. also involve systematic trialanderror bracketthe root.Two specificmethodsare the root and then systematically position.rt2 PART 2 ROOTS AND OPTIMIZATION l'(. . solLltions be obtirined analyticalty optimi.the combinationof thesesystematic methodsand computersrrrakes the problemsa simple and efficient task. fundamental difference both searching a a betweenthe two types of problemsis illustratedin Figure PT2. searching 2. mizationmethods aresinrilarin spirit to the root locationmethocls Thatis. guessing fbr The involve and location on function. optimizationinvolves searching fbr the functiorr'sextremepoints. and secantmethods.
In pafticular. K n. goldenwe describe two bracketingmethods.2. re nt )h or at at nt .First. we describe MATLAB's excellentbuiltin capabilities for this task.we discuss a robust.The emphasis is on describingand illustratingthe use of MAILAB's capability in this area:the fminsrearch function. sectionsearchandparabolic interpolation. Chapter Z dealswith optimization. Finally.we then discussa hybrid approachcalledBrent's rootfindingmethodthat exhibits the reliability of the bracketing rnethodswhile exploiting the speedof the open methods.This approach. After illustrating how f zero canbe usedfor engineeringand scientificproblemssolving.for finding the optima of a function of a single variable. the last part of the chapterprovidesa brief descriptionof optimization of multidimensional functions. it fbrms the basis fbr MATLAB's rootfinding function.2 PART ORGANIZATION rr3 S L ' Following the descriptionof theseindividual open methods. fbrms the basis for MAILAB's onedimensional rootfinding function:fminbnrl. hybrid approach goldensection Then.which again is attributecl to Brent. After describing and illustrating fmlnbnd. 6 endswith a brief discussion of special methodsdevotedto finding the rootsof polynontials.As such. the chapter problems endswith an exampleof how MATLAB can be employed to solveoptimization in engineering and science. Chap.f zero. that combines search and quadraticinterpolation.
r R CHAPTER OBJECTIVES Thc primaryobjective you with bracketing of thischapter is to acquaint mcthods fbr findingthe rootof a singlenonlinear equation. ' . objectives Specific andtopicscovered are ' ' . Your bossat the bungecjumping you compirny wants to detet'mine the ntassat which this criterion is exccedecl given a drag coeliicient of 0. Understanding lalsepositicln and how it difl'ers fiom biscction.ion rnethod. Llnderstanding the incremental search Knowing how to solvea rootsproblemwith the bisecl.. methodand its shortcomings.rirnh rt_BlE s. what rootsproblems Uncierstaniling nrc and wherethey occurin cngineering and science.l Fund3 Fundomentol Principle Heotbolonce Mossbolonce Forcebolonce Energy bolonce Newton'slows of motron Kirchhoff s lows De Vc T^. Knowing horvto cstimate theerrorol'biscction why it difl'crsliom error ancl estimates fbr othcrtypesclf root locationalgorithms.25 kg/rn. . you cannotntanipulate this equation to explicitly solvefbr rnthat is. You know f}om your previousstudiesthat the fbllclwinganalyticalsolutioncanbe usedto prcdictfall velocityas a functionof tirne: lntf^t '.r \V rtt / rsrr Try as you might. you cannotisolate the masson the lefi sideof the equation. orl Cu tl4 .l:. r'(r): .\ Roots: Brocketing Methods N et rf 5./i. A ar dr rh at pr ra tic pa mi pe an ca YOU'VE GOT A PROBTEM junrpcr's h a v ec s t a b l i s h ctd e d i c a ls t u d i e s h a ta b u n g e e chances of sustainin ag signilicant injury increase vertebrae it'the liee1all significantly velocityexceeds 36 m/s afier4 s of fiee fall. Cc of Mc ofl Ch AC( V t. Knowing how to determine a root graphically.
:.'. 2 1 Now we can seethat the answerto the problem is the value of ru that makesthe function equalto zero.dpN.'.veloci\'.' ) S 1 :.. amongthe modelvariables arrdparameters.):l:. That is.n.).AlthoughEq.m o s sl r o n s f e r .I N E N G I N E E R I NA G 5.r.:. r(m) (5 .suppose that we had to determinethe velociryt in a .:. /r.r:'. t principles Fundomentol usedin designproblems Dependenl Vorioble Independent Vorioble ine CtC pOS.ables ret'lectthe stateor pertbrmance whereasthe pararneters of the syster.'\:..'Jrt dtpendtrt. \ote \hrt \r.'i:.''. As introduced in Chap. dissipoiive potcmerers F l o r t r l r' al nrnorlraq r"f /ro<r<fnnr a opollronra induclonce) . Table5.'. 1.'.:.I ROOTS I N EN GIN E E R INA G N D S CIENCE Although they arise in other problem contexts.it)rl Poromelers Itei. it is isolated on one sideof the eqLral sign. represent its propertres or composition.^\'::^\j'::'. This chapter you to how will introduce the computer is usedas a tool to obtainsuchsolutions..i\'. (5. syslem(leomelry . ::] lorts ol ewlons )l of rchhoff s iorrys p o t e n t l oe l nergy Acce/ercllion.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 .i. r'*i.cr. An examp\e of such a mode\ is the equationfor the bungee\umper's ve\ocity.b\t:.. However. is said to be irrrnlicit.1*.::':r. . torii.ss fbr a jumper with a given drag coefficientto attaina pre..g{i.F') . s y s l e r n D0iOriCe bolonc. parameters. I listsa numberof tundamental thatareroutinelyusedin designwork.:'j.set tinre ma. as posed at the start of the chapter.rlh'i. l r u c i u r cp 5 l r e r r g t lo r l r n o l e r i o ls l r o p e r le s .\. cases.. 5.un(. mathematical equations or modelsderivedfiom theseprinciplesare employedto predict dependent variablesas a function of independent rurrr. 00lonce tenpeteioie or quontity Concenlrolion 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 .:r.I ROOTS ND SCIENCE tt5 An alternative way of looking at the probleminvolvessubtracting u(t) from both sides to give a new function: :.scribed period..rsi.roots of equationsfrequentlyoccur in the principles areaof design.rm'trs.1) provides representation of the interrelationship a mathematical In such it cannotbe solvedexplicitlyfbr nrass.we call thisa "roots"problem.'. 5.Hence.:':. If the pa.i:. or ocotron Currenlsond vo/toges T r m eo n d p o s r t i o n Lme qeometry Alcrss of molerrol syslemgeomefry.'s.
1 1 r ) . Use thc graphicalapproach of the bungee jurrper with a drag coel'l'icicnt ol'0. *t. 5.qr 1(. (5.rs conl'ronted rorrtincly and science./nlr)*L) ( r r r . 2 )v e r s u s Solution./(r) :0 is to make whercitcrosses a plot ol'thc firnctionant'l observc thc.).therootoftheequation.thercfbre. q r r 1 1 plot (. by numerical methods The solutionto thc dilemmais provided To solvethe problemusingnumerical methods. Ho ma un me ab pl( Cv !' ro ar ha Fc tr rh te 1 z pr u ri 5.This point. . 1 a l l .Thisvaluealsorepproblem. :c1) I f ) = r i q f L ( r 1 * m 1 rc . (5. 1 . 8 1n r / s r .2. q c ). v '. Thc lirlkrwing '' ' ::' r'ril = nlll '' I = 4.nate of the root of the ecluation . theseproblemsoften reqrrile the determination of implicit parameters.f(m):0is.raxis.tt6 ROOTS: BRACKETING METHODS many designproblemsinvolve specifyrng the This represents a real dilemma.because properties ol cornposition of a system(as represented by it.J*cd.Thesetechniclues can be appliedto many in cnginecring otherproblcn. n a t io ofnt h er o o t . l l 1 . ' 1 r. Thus. (5.spararneters) to ensure thatit perfornrsin a desiredmanner(as represented by its variables). it is conventional to reexpress Eq. rv a l u el b r w h i c h . tbr rootsof equations.N o t c :T h e a r ccelcratio o n l'urirvitv mass: MATLAB scssion s c t su p a p l o t o l ' E q . p r o v i d c s a r o u g ha p p r o x i r . wh E X A M P5 L.3 "50 150 I I] e .which represents t h e . r .. resents thc rnass that solVes the clesign fbr deterThe lbllclwingpages and graphical methods dealwith a varietyof numerical rniningrootsof relationships suclras Eq. .( 5 .25 kg/rn to havca velocityol'36 m/s after4 s of free i s 9 .1)by variableu frornboth sides of the equation The subtracting thedcpendent to give Eq. '\6.2).2). l ( r l : 0 .\. E I TheGrophicolApprooch to dcternrine the nrass Problem Stotement.2 METHODS GRAPHICAT A sinrplenrcthodlbrobtainingan estir.3 pro cal t ans whi par 5. valueof'nithatrnakcs.anlr (:.
0456 which is close to the desired fall velocity ol36 rr/s.3 BRACKETING tt7 of the plot The functioncrosses thc llr axisbetweenlrl0 and l50 kg. i r n c t i o nt sh a ta r ct a n g c n t i a tl o t h c . For cxample..graphical methods can be utilizedto obtainroughcstimatcs gucsscs in this chapter.graphicul interpretations areusefirl Asidefiorn providing ol'the lirnctions and anticipating the pitlallsof the nurnerical understanding the ploperties rrethods.3 BRACKETING you'd oftenbe told to use"trial and If you hada rootsproblemin thedaysbefbrccomputing.5 .5..r .) v 0.n cxarnplc A o l ' a I ' u n c t i ot n h a ti s t a n g e n t i a tt ll t i o n s( F i g . METHODS AND INITIAT GUESSES 5. . r a x i s( F i g . andappliedrnathematicians.5. The validity of the graphican be checked by substituting it into Eq.2 nrakes it difiicult to dcveloptirolThe cxistencc of cases of thc typedepictcd guaranteed Howevcr. makeguesses until the function error" to come up with the root.l ) along with the parametcr values liorr this exarnple to give (q*cd/ r45) *t) >>. (S.. 2 b )c a nv i o l a t e thesc : : ( r ( r ' ) 2 t n a k c s 2 ) ( .(g*cd/1./ (r.thereareeitherno rootsor an evennunrbcr gcncralizalions arcusually lrue. by Fig. As indicatcd la ancl r'.rr)and . absent) in an interval (r). ) a r e a l s o o n o p p o s i t e s itd /( s. you'd repcatcdly wasgrea(ly lacilitated by the advent of sofiware was sufTiciently closeto zero.anh(::qrt.1 showsa numberof'ways in which rootscan occur (or be prescribcd lb dcby a lowcr bound.o f xis.) havcoppositc signs. 5.The process . valuebeciiuse thcy arc not vcly precise.lherearecases wherethcy do not holcl.r.M a t h e r n a t i c a l l y . Visualinspection providesa roughestimate of the root of 145kg (about320 lb). havethe sarnc sign. thereare specialtcchniqr"rcs 2002).wher c.r1 andan uppcrbound.sqrt.Figurc5.i5)*t.5 .2)to yield cal estimate >> sqrt (g*145/ccL)*r. 2 aa principlcs . the rnethods described usedin conjunction routinelyby cnginccrs. / ' ( .r r ) a n d ./ wherea singlcroot is brackctcd hc e ra ever. of rootsin the interval. Howpictsthecase by ncgative andpositive values o1'.METHODS AND INITIAL GUESSES 5. if / (.lrl..anh(scTrt 35. N o t i c c t h a t . thcrc rootsoccurring within the interval. r ' 2 ) ( x 4 ) .0456 which is close to zero. It can also be checked by substitutingit into Eq. t e r m si n t h i s p o l y n o m i ac that arc Although they are bcyond the scopeof this book. confl'onted tions are extrerrelyusefulfor solvingrnanyproblerns scientists. and Canale.5. r : 2 i s c a l l e d' ./ l q u a lt o z e r o . (q*14filco) *t. arean odd number ) ol'rootsbctwecn thc values.r.whcn proof computer algorithms to locate all the rootsin an interval.Fig. expressly designed to locaternultipleroots(Chapra in Fig. Althoughthcse l 's unc) n dd i s c o n t i n u o u Forcxarnple t. r t w tr t h ca x i si s t h ec u b i ce q u a t i o n .showsthrcc In general. il' l (rr ) and.Fig.Thiit is. lor numcrical mcthods discussed mates can be ernployed as starting firr roughestimatcs ol'tlreroot. (5. in thc lirlkrwingsccwith graphical approaches. tt r r u l t i l t l c nrtt. are ilf limited practical Craphicaltechniques ol'roots. ..TheseestiHowevcr.f (.
". 5. ol'h. 1 (o) Multiplerootsfhot.tt8 ROOTS: BRACKETING METHODS xl (d) xu (bl FIGURE 5.2 lllustrotion lo lhe generolcoses of someexceptions when depicted in Fig. .^^r..) hovethesome be no roots will be on evennumber of rools or there h o tl f l h e within t h ei n t e r v oP l o r t( sb )o n d ( d )i n d i c o tte i..occur the function is tongentic to the r oxis. or[o o ' s .ond occurrnon intervol (oJ ond (c)indicoie thctif both on upperbound x..nn Jho .n< iho'c ore FIGURE 5. for the inlervol. For thiscose. ./ (rr) ond ..either . here f u n c l i oh no sd i f f e r e n st igns o i t h ee n d p o i n t s will be on odd number of roolsin theintervol.r. on even numberof oxis inlercepiions (b) Discontinuous funciions where end pointsof opposiie io s i g ^ o ' o c [ e t o n e v e nn u r r b e r .ol lnr nolarmrn.I I lustrolion of crnumber woysthoio roolmoy of generol prescribed by o lowerbound . [^./(r. Poris there wili sign.ile s..o^. 'nh lnc e"d nninls nr'p rf nnno. thoa^ . S p e cl stro'egies ^ro .o..
r.1 Incremenlol Seorch you observed in Example5. good initial guesses may not be obvicontextyou are analyzing. The two major classes of methodsavailable are distinguished by the type of initial guess.The problemis compounded by the possible exisspaced rootsmight be misscd tcnceof multipleroots. by locatingan intcrval Ircrenrentalseun:hmethodscapitalizeon this observation scarchis the sign.in othercases. automated approaches to obtainguesses would be useful.but when they do they usuallyconvergequicker. An Mfile can be developedr thatimplements an incremental search to locate the roots within the rangefrom xmin to xmax (Fig. suchtools can actuallymake the trialanderror approach attractivefbr someproblems. that /'(. Thesemethods can involveone or moreinitial guesses..3 BRACKETING METHODS AND INITIAL GUESSES lr9 tools suchas spreadsheets. but thereis no Opertmethotls. slclwly For wellposed the bracketing methods alwayswrlrk but converge (i.. the incremental search. By allowing you to make many guesses rapidly.e.roice ol'the incrcment suming.s.In suchcases.3. and ..On tho other hand.r7) signs. problems. and f (x. a sign change occurs! the upperand lower bounds arestoredin an arrayxb. needfor them to bracketthe root. the search can be very time concl. In general. Thesemay naturally ariseliom the physical In bothcases.there is a possibilitythat closely (Fig.. section describes one suchapproach.rnc it ns allowsthe userto specifythe numberof intervals within the range.r1afld.. ' This lirnction rs a rnoditledversionof an Mlilc originally prcscntcdby Recktenwald (2000). initial guesses arerequired. However.) (5. The following ous. .If the lengthis too srnall. 5. that is.. 5. 5.e. But.theseapproaches require Then they systernatically home in on the loot in an iteraan initial "guess" to get started.they can divergc).3) rre site lies thenthereis at leastone real root between .l (.they typically take more iterations to home in on the answer). lnterestingly.A f or: loop is usedto stepthrough eachinterval.it is preferableto have methodsthat come up with the correctanswerautomatically.4). As the namc implies. if the length is too great. In the eventthat is automatically setto 50.5.A potentialproblcm with an incremental where the functionchanges length.) haveoppositc < 0 /(r1)/'(x. tive fashion.An optionalargument of a firnctionf L. the open rnethods do not alwayswork (i. They are . if . are on either sidc of the root./(x) is real and continut'rus in the interval from x7 to. as with trial and error. for many other problems.1.r. .r) changed When applyingthe graphical technique sign on opposite sides of the root.lf ns is ornitted. thescare basedon two initial guesses that Brttcketingmethod.3).. In contrast. "bracket" the rootthat is.
(xb) ?displzrv that no brackets disp ( 'no brackets forind' ) or increase ns' ) disp ( 'check rnterval disp('nr:mber disp (nb) end ol brackets:') were f ound %drspLay number of brackets FIGURE 5. % if nargin < 4.incsearch (func.l) xb(t'tb.ns) : % finds brackets of x that contain sign chattges % on an interval.3 theincremenlol length of lhe seorchprocedureis whereroots couldbe missed becouse Coses of the o n d w o u i db e missedregordless root o n l h er i g h i ts m u l t i p e t o o l o r g e N o t et h o t h el o s t eln g l h . xmin.2) = x (k+1). ns = 50.xmax.x m a x . unless sign cranqe det ec t ed nb = 0r :{b . n s ) .. xmax. xmax cndpoitr(s oI inrerva] (default = 50) ? ns = number of subintervals % outpuL: xb(k.5. 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 .2 ) is tire upper bound of the kth If no brackef. f .sign(f (k+L)) licheck for sign chanqe nb = nb + 1.aLor ? incsearch: xb .Ll.2 EXAMPLE FIGURE 5. xb . of a function ? ? inpuL: furLc .i I .4 on incrernelrtcl seorch An Mfile fo irnplemenl r20 .n . i n c r e m eln xb = incsearch(func.ns) function incremental search root fo.1) is the lower bound of Lhe kth % 2 xb (k.xb is: null for k = 1. x{kl. length (x) 1 if sign(f (k)) ^. end end jf isempt).s found. .name of functiorr % Y xni .func (x) .xmin. xbrnl. e.
2.3 BRACKETING METHODS AND INITIAL GUESSES EXAMPLE 5.100) 3 .4)alongwith thc root Iocations is shownhere. J ?E 4.'t14j. number of 9 3 .4) A plot ol'Eq. The MATLAB session usingthe detaultnumberof intervals >> incscarch(@r of possiblc 5 afnS 3 . 6] fbr the function: J G ) : s i n (l 0 .6531 \.r j 4. the funcpossible tion rnisses rootsat .2 Incrementql Seorch t2l ProblemStotement.{:i2 l t.6939 sin(10*x) rooLs: +cos (3*x). two real rootsthal are very closc together.However.a)to identifybrackets within the interval[3. 6) numbcr (s.2 424 3.t3 3. The f'unction can be run againwith more subintervals with thc resultthat all nine sign changcs arc located > : .5.5 5. 2 1 . UsetheMflle i ncsearch (Fig. i n c s e a r c h ( t l x s i n ( ' lO * r ) + c o s ( 3 * x ) .7 3.3939 . r ) (50) is Solution. t9\9 4.5. it is clearthat eachrepresents doLrble nrots. mots look like they might be Thesepossible by usingthe zoom in tool.212. (5. /34t 4.3536 possiblc rooLs: b. 3 . 49 3.3061 3.25and5. r ) * c o s ( 3 . because the subintervals are too wide.36'. a.3.5 Althoughfive sign changes are dctccted.3061 3.
t21 4 .2424 4. .You would be wise to supplement suchautomatic techniques with any otherinlbrnration insightinto the location thatprovides ol'the roots.1B1B 5. If a functionchanges sign over itn interval.t213 4.2424 4. FIG Ac f. rr) thatgive valuesl'or t 0n') with dillelent sisns.591 A <1 2 The lbregoing illustrates example that brutelbrce nrethods suchas incremental search are not lbolproof.1515 5.The lbllorvingexample throughthe actualcornputations involvedin the method. the firnctionvalueat the rnidpointis evaluated. The first stepin bisection is to guess two values of the unknown(in the present problenr. t. 1 ? .2r2).Frornthe graphical solution in Nr wl thr .3 The Biseciion Method ProblemStotement. The subinterval then beconres the intervalfix process the next iteration.t57ia 4 . Usebisection problenr to solvethe sarne graphically approached in E x a r n p l5 e. 13 5.2121 4.r.122 ROOTS: BRACKETING METHODS 3.4 BtSECTON The bisection ntctlu)d is a variation of the incremental search rnethod in which the interval is alwaysdividecl in hali. l .2'.oi Ex plc let Th 5. Nc lat EXAMPLE 5. The locationof the root is thendetermined as lying within the subinterval wherethe sign changeoccurs. 5.Suchintbrrnation can be fbunclby pkrttinglhe functionand throughLrnderstandinc the physicalproblern frorn w h i c ht h ee q u a t i o n originated.1318 5 .5910 . until the root is known to the reqr"rired A graphical depictionof the methodis provicled goes in Fig.5. 566J 3.'flre is lepeated precision. Solution. ':..
.l 142.Thc clf50 plotobviouslysuggestsbetterini g tu ia el s s e s .Therefbre. we create a new intervalby redefining the lower boundas 125./r.buttilrillustrati'u't'purposr'\ lct's assume we clon'thave the bencfitof the plcltand havc madeconservative guesses.371 I ( 5 0 )I ( 1 2 ..Consequently. s a ly 40and I50..t FIGURE 5./. Thismeans thatthc value of 125calculated here hasa trueDercent error relative of r. .and henceno sign change which is greater occursbetween the lower boundand the midpoint. in the upperinterval betweerr theroot mustbe locatecl 125and 200.434/.5.1316. lies at thc midpointof the interval .7316 I Next wc compute the productof the functionvalueat the lower boundandat the midpoint: : 1.r. l. ffi xt.f(nt) 2 U 2 6 First iteration V Second iteration I xl "r/ I t Third iteration # xt )(f x.50+ l(x) _  )< .r2s I . value Notethattlreexact of tlrerootis 142.0 . 5 1 9 ( . 4 0 9 1 thanzero.5:1 .4 .4 BISECTION r23 .. wc can seethatthe t'unction changes signbetween values and 200.5 A grophicoldepiclionof the bisection method Thisploi corresponds to the firsifour iterotions trom Er.the initial estimatc of the rtx>t.omple 5 3 Example5.\r xu V Fourth iteration I Fo_. i 112. Thcrcfble.1316 lx100?4:12.
: 0 . For example. S u b en re i e l d s stituting these values i n t o E q .5 % .5. lrAl 5r?sl #ll\l0o/.xarnple 5. .:.43 e r r o r d r o p p ef d t<t0. U s eE q . .jective criterion lirr deciding when a lel'ined to terminate the rnethod.'l:lff]l(x)%' (55) whc.anclthe root cstinrate is calculated as 125 * 162. i relative elrur of a. t h e c o n r p u t u t i o n Icillriilulc(1.:23. We rnustnow develop an olr.5 . 4 0 9 ( 0 .124 BRACKETING METHODS ROOTS: fronrxl : At this point. 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 nr g rl p p r o x i r n r r tp errol'ils in Irecall E q . l u rl. 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 tain refineilestinrates.75 ) .4 so r B i s e c t i o n E r r o rE s t i m q t e f P r o b l e mS t o t e m e n t . . 5 9 .lr:o 1f3 . rnightbe to end the calculation when the errrlrl'allsbelow some An initial suggestion p l c s p e ci e f d l e v e l . f ( t t s l .say. 5 .. ..0 . 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 o. 3 u n t i l t h e a p p l o x i r n a te er r o r t a l l s b e l o wa ) c o n r [ ] u tt s ( o p p i n c r i t e r i o ro to e h ee r r o r s . ( . :) (t.1 . t l ' € .wc recluire an errorestirnate ee r c e nlte l a t i v e r o o 1O .( 4 . :200. is llawed because the errorestlmates when the errordropsbelow.5 .y.l 3 2w 5a dl 6 2 .4 We rnightclecide thtt we shouldternrinate duringthecoulseof'thecornputttion. W h e n c . Exarnple 5 .. 3 .5 I Fig exh __*+ . EXAMPLE 5. y 51 .re. 3 .r]1""is the loo{ lirr the present iteration and rf'l'lis the rool lionr the plevious itemis t i o n .ratio bn ecrusc t 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 d knew the roo(.J09(. tulrcady thal is not contingerrt on lbreknowledge ol'the There lirre.08c/o I6t. 5 )  _ . The nrethod can be repeated until which represents a percent the resultis accurafe enoughto satistyyour needs. couldbe continued We endedE. 8 5 % . 11 1 Therelbre.5 :  43.3 with the statement thatthe rnethod to obtain r'stimate of therrxrt.t h c t r L r e relative rom l2.1l 6 2 .101)()(.F o r i n s t a n c e in . 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 s t o p p i n gc r i t e r i o n e . This strategy tlrrknowledge in theexanrple were based ol'thetruer(x)tol'the firnction.5 S o l u t i o n .5%. : 0.the new intervalextends rnatecau thenbe calculated as l25to r. The upperbound tirr the third iteration is rede[ined as l 62. the roo( is now irr the lower intervalbetween125and 162.0. C o n t i n u e Exarnple 5 . . 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 .5. This woulclnotbe if we t h e c a s ci r r a n a c t u u l si(r. .'lil 1_r1c' 1 T] te to T} on Fl Er of lo'. ..5:.
the true root can lie anywhere within the bracketing interval..The "ragged"natureof the true error is due to the fact that. 5.l .:0..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.4 BISECTION 125 Recallthat the true percentrelativeeror for the root estimate of 162.5c/o. le. and the computation terminated.6 Errors for lhe bisection meihod.7 5 143.z o rais (t) rl or Although the approximateerror does not provide an exact estimateof the true error.l. FIGURE 5. .. Thus.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 can be le. In addition. This behavioris manifested for the other iterations: Iterolion xt xu xl le. Theseresults are summarized in Fig.. Fig. They are close when the true root firlls at either end of the interval.l.85%. CS be VC he all o 1n Approximateerror. is always than l le.fbr bisection. The true and approximateerrors are far apart when the interval happensto be centered on the true root.. 5. the plot l captures exhibitsthe extremelyattractive greater characteristic that le. Therefore. I. True ond opproximcte errors ore piotted versus thenumber of iterolions.I is greaterthan le. le.6.l ("/"1 2 30 B 1 34 4 698 337 )66 o82 o4l le. l finally falls below €.6 suggests that la.5was 13.5. the general downwardtrendof le..
Eq..142.(5.ro : 200 .(. This can be seenby recognizing that beforestarting the technique.50 : 150. Another benefitof the bisectionmethodis that the numberof iterationsrequiredto attain an absoluteenor can be computedu priorithat is.1641 + .I falls below s' the computation could be terminated with confidence that theroot is known to be at leastas accurate as the prespecified acceptable level. .6) to give n: log:(150/0. (5. the absolute erroris sll:r!lrro:AxO where the superscript designates tlre iteration.5859 In essence. we could rnakethe definitive staternent that x.lf the pocket calculatoror computerlanguage you are usinl doesnot includethe base2logarithm as an intrinsic function. l 6 4+ l 0. when Example5.the root must lie within *Ax/2 of our estimate..5781 1.a generalfbrmula relatingthe errorand r l ' i t e r a t i o nrs i s t h e n u r n b eo E (" .For Example 5. For instance.Although bisectionis generallyslowerthan other methods. the neatness of its error analysisis a positivefeaturethat nrakesit attractivefor certainengineering and scientific applications.For this boundto be exceeded. After eisht iterations. the errorbecomes F'_ ." After the first iteration..this etluation showsa handy way to computc it.5) providesan upperboundon the true error.518t1 1r43.5859):8 I MATLAB providesthe I oq2 function to evaluatethc base2logarithmdirectly..This is due to the fact that eachtime an approximate : (xt * x) /2.126 ROOTS: BRACKETING METHODS when le. ..5859 (5.1) log 2 :.7s00 = Wecansubstitrrte these values intoEq. While it is dangerous to draw generalconclusionsfrom a single example."r.. l will alwaysbe greaterthan le..(#) 0. ln general.1 is the desired error.\D L If C..Hence.Art' . Otherroollocatingtechniques do not alwaysbehave as nicely.4 was terminated. "2 A.r" Because eachsucceeding iterationhalvesthe error.log.l for bisection.beforestartingthe methodwe areat the "zero iteration. : 143.it can be demonstrated that le. the true root would haveto fall outsidethe bracketinginterval.xt..which by definition could nevetoccur fbr bisection.13. the absolute errorwAS tr ..7500 : I ' 1 3 . that the trlle root lies somewhere within an interval of Ar : xu .this ecluation can be solvedfbri log(L. before startingthe computation.6) Let'stesttheformula. Therelbre.r\tf E.r) = log(_r)/log(b). theinitialinterval wasA.r.4. we know root is locatedusingbisectiofloS.142.
5.p1.0.6)can providea useful root locationalgorithm.end test .end :lf nargin<5 i lsempty(maxit). es0.pl.error('at least 3 input argurnents requi red'). 0.xl.break.varargin{: } ) *func (xr.. It is passed the function(func) (es) alongwith lower(x1) andupper(xu) guesses. = additional parameters used by func % % output: ? r oot . i f test>0 / €rror ( 'no si gn change' ) .p?. es. f unct ion Iroot. ea. 0 0 . ) : lroot.5.4.varargin{ : } ) *func(xu..er I bi secr (f unc. x r louer orrd . <= Xl.end itcr.. iterl=bisec1. * 1 .es.xu)/2.5859was acceptable. if test XU = < 0 XI..oes ? . varargin) % bis. (l) while xr:old = xr'. therewill be caseswhere (usually through knowledgeof the problem context) you will be able to specifyan absolute error. xu. (x1 +.maxit.varargin{ : }) .4 BISECTION 127 Thus. xl. Although we haveemphasized the useof relativeenors for obviousreasons.7. anoptional stopping criterion FIGURE 5.1 MATTAB Mfile! bisecr An Mfile to implement bisection is displayed in Fig.For these cases. ln addition. % uses bisection rnethod to find the root of func ? inpuL: func : name of function % ts xl . xr = xl.0001%) es = desired relative % erroi: (default (defaul t = s0) % maxiL = maximum allowabl e iterations pl. maxit=50. maxit.ect: r:oot Iocation zer. 5.. it. ea = abs ( (xr . rea L root ? ea = approximate : rlumbef of ? rter relative er:ror iterations (t) if nargin<3.e n d i f xr = 0. bisection alongwith Eq.p2. xr'iLer = iLer + 1.0001.. xrold) /xr) test = func (xl. the formula tells us that eight iterationswould yield the desiredresult. .tunc (xl..varargin{: } ). (5. es >'0 iter >= maxit.7 AnMJrle t o i m p l e m etn hteb i s e c t i om nethod.ea. end if nargin<1 I i sempty(es). elseif xf el se ea end i f end l:OOt = ea =  test xr. .xu.Fr gues:s = 0. if we knew beforehand that an error of lessthan 0.(func.end ..
7) Fir wh Se .6089c 007 xu So 5. you have t0 find the root of 5 .l(rr) and..7 can be usedto determine the root as >> >> fmG(rn) [mass = *tarrh(sqrt. 5 .1311 kg is obtained after2l iterations with an approximate relativeerrorol'€.) w i t h a s t r a i g hlti n e ( F i g . The bi sect f unctionfrom Fig.3450e 0Olr q ' " ico pla 2. an error message is displayed andthe functionis terminated. it locates theroot .wecansubstitutetherootbackintothel'unctiontoverify that it yieldsarvaluecloseto zero: >:' fm(mass) is bis EXAMPLE 5.. 5..200 ) (9.f Gt) .3 6 In Example5. We can employ this functionto solvethe problemposedat the beginningof the chapter.break loop is ernployed t o i m p l e m e ntth e bisectionalgorithmuntil the approximate error falls below es or the iterations exceed m a x it .:0. heintersectio byjoining.. Using similartriangles.81*m/0. Fr Fo mclSS 142.for details).) (5.r Thus.the shape of the function influences the new root estimate.1 we generated a plot of this functionversus rltlssandestimated thattheroot fell between140and 150kg.r.25lm) *4) 36. .5 FArSEPOSTTTON Falseposition(alsocalledthe linearinterpolation method) is another wellknownbracketing method..l(.Thus.a resultof nr : 142.5 Th Prc cal 4.)(x1 ..r.It is very similarto bisection with the exception thatit uses a diffbrentstrategy to come up with its new root estimate.r..81*11.81m @.f (.000053450/o. jumper's fieeftrll velocity Recall that you need to determinethe massat which a bungee exceeds 36 rrls after4 s of free fall given a drag coefficientof 0. ) .Thus. 40. the intersection of the straight line with ther axis can be estimated as (seeChapra and Canale. Ratherthanbisecting the interval. 8 ) T n o l ' t h i sl i n ew i t h the r axis represents an improvedestimate of the root. sqrL (9.) f (..13'11 5.25) ea i ter] =brsect ( fm. hena while. It alsoassigns detaultvaluesif maxit a n d e s a r e n o t s u p p l i e dT ."n ( f (m): \. 2002.Y.1 025 9. The function first checkswhetherthere are sufficient argumentsand if the initial guesses bracket a sign change.r28 ROOTS: BRACKETING METHODS and maximum iterations(maxit) can be entered.If not.25 kg/rn.
rity 3to root rine 6. w bisection 1 i t h t h r :c x c e p t i o n t h a rE q . Thc algorithmis identical tclthe one for ( F i g . 7 . The vahre of .. i n i t i a t ct h e c o m p u t a t i o n with lluesses o f . .. F i r s ti t c l u t i o n : rcketategy 3 root ) with )n inlt line .r.computed with Eq.: 200. 1 a n c l 5.r.fhl./ (.r.litrmula... : 200 .l (. (5. Use lalse position1o solvc the sameproblern graphiapproachecl c a l l y a n dw i t h b i s e c t i o n i n E x a n r p l e5 s.5.3.) : 2.5 .sition.The proccss is repeated until the loot is estimated adequately. 7 )i s u s e d . o r /r . 8 6 0 2 9 1 . .592132 .7) then replaccs w h i c h c v eo r f t h et w o i n i t i a lg u c s s c s .860291 r(50.8 Folse position relaerifY EXAMPLE 5 5 This is the.5 .4 .scpo.7) . alwaysbracket thc true root.ri) : '4. In this way the values of .5 0 a n d .:200  : l 76 .rr) f 6.f (. y i e l d sa l i r n c t i o n valLrw e i t h t h es a m e sign as l (.5 FALSE POSITION lre ror ir the )ed 129 ter. FIGURE 5. r 1.5clo Second iteration: (5..0 . 3 . r .519381 .r7andr.rJ6029 which hasa true rclativeerrorof 23.2(n) 0. The FolsePosition Method Problem Stotement. ( 5 . 2 1 l1 .). .rt : 50 'r.r.) 0.\. S o l u t i o n . A s i n E x a m p l e. 5 1 9 3 8 1.
t30 ROOTS: BRACKETING METHODS Therefbre.{n which has true and approximate relativeerrorsof 13.2113 t'Q.6 A Cose Where Bisection ls Preferoble t o F o l s eP o s i t i o n ProblemStotement. Insight into theseresultscan be gainedby exarlininga plot of the l'unction.J J 137 5 45625 35 25 t3B 56 6 Thus. and r. r0 6. : 1 1 6 .l(. h er e s u l t s c a nb e s u n r m a r i z e ad s Iterotion l xt X. 2 7 7 3 1519381 . the true error is reduced to lessthan 27r.Frlr falseposition.J 4 5 O (r5 0 975 o 975 o 975 3 .. becomes the upperlimit for the next iteration. 3 3 3 3 3 0 09430 a tBl76 o 26287 0338Ir a 40788 e.5 4 975 I 1375 ) 05625 I 0t5625 e" ("/"1 r 0 00 333 1 43 77 4A e. : 116. Although firlse positionolien perfbrmsbetterthan bisection.566t 74(50 : .566114 .r. if .3828 .r.f (rt) : 4.t] 6. As in the fbllowing exarnple.3.16a/c and 8.rr) is muchcloserto .0. r .2113) 0. 5. S o l u t i o n . thereare certaincaseswhere bisection yieldssupericlr results. U s i n sb i s e c t i o n t.2713. there are other cases where it does not.519381 . Use bisection and lalseposition(o locatethe root of 'l(r) :'r"'l between r : 0 rnd 1. : 176. As in Fig.) :0.56olc. thecurve violates the prernise on which fhlsepositionwas basedthat is.. Additional iterations can be performed to refinethe estimates of the root... (Y"l 48I 309 223 l7 l e.(7"1 906 BIB 737 662 592 0 a a943O 0 tBtz6 o 26287 0 3 3 8 II After five iterations. r. (o/ol u 2 . respectively.. EXAMPLE 5.566174 t 62.the root lies in the first subinterval. the true error hasonly beenreduced to about59%.9. alier l'ive iterations. . a very diffbrentoutcomeis obtained: herotion I 2 3 4 5 xt .
Possibleremediesfor this shortcomingare Canale. ".9 : xlrt P o to f . r t l o n . ght into .POSITION 5. i l l u s t r o t is nlg o wc o n v e r g e n o ofthe shape zerothanf(x").a e. function. that violate this rior to bisection. as iterations sidedness. to the result is close whether and determining into the originalequation estimate method:its oneof the falseposition a major weakness The examplealso illustrates points will tend to bracketing of the one are proceeding. (7"1 906 Br 8 737 66.the opposite of the present . (recallFig.. checked by be shouldalways the results in additionto usingEq. 35 2.. (Chapra and elsewhere available curvature.5 FATSE r3l he next r cases section (. F ( x ) p o s i i i om n ethod { t h ef o l s e ce 1 . This can lead to pool'convergence. general conclusion. a Although are usuallynot rnethods Therefbre. That is.e curve loser to . significant fbr with particularly functions stay fixed.2 5q2 rocltlocation regarding illustrates thatblanketgeneralizations The fbregoing exarrrple is positiorr often supeas lalse method such possible.2002).5 r3 8 56 t6 FIGURE 5.8). thentheroot is closerto x1thanto x. 5.Because is true.5). zero.thereare invariably cases the root substituting (5.
3 5 6 9 7 5( t 1 9 8 0 .iry'ii"". The data indicate that levels have increased a little over 19Vo over the period from 315 to 376 ppm. (5. fa .9) rh fc a 5r is pc Sr 1960 1980 1990 2000 2010 Er FIGURE 5.10 shows data for the pafiial pressureof carbon dioxide (COr) collected at Mauna Loa. Howoii.s) w ul tr st l( where [H+] is the molar concentration of hydrogenions. Hawaii polynomial.The trendin the datacanbe nicely fit with a quadratic p c o . It is well documentedthat the atmosphericlevels of severalsocalled "greenhouse" gaseshave been increasingover the past 50 years.3 from 1958through2003. Outsideof urban and industrialareas.r32 'il ROOTS: BRACKETING METHODS "ii'i*. pH is the measureof the activity of hydrogen ions and.. : 0 . One questionthat we can addressis how this trend is affecting the pH of rainwater. .IO Averogeonnuol portiolpressures o{ otmospheric corbon dioxide (ppm)meosured ol MounoLoo. . 5 )+ 2 1 .COz partial pressure(ppm)."' . therefore..I H * J "lHCOi I Kr ' 10"' Knpco.5. we will learn how to determinesuchpolynomials.For example. it is well documented that carbondioxide is theprimary determinant of the pH of the rain.SY Bcckground. 5+ )339 where pgo..it can be computedas pH: log.Fig.1 9 8 0 .n[H+] 6.. 0 1 1 8 2 5 ( r . its acidity or alkalinity.lf. goverx the chemistryof rainwater: The following five equations ^ .iei{rl: { ' GASES AND RAINWATER GREENHOUSE . A tr AI I In Part Four.For dilute aqueous solutions.
Thus.11)into Eq. You know with certainty that the pH of rain in pristine areasalways falls between2 and 12.we will be satisfied causethe pH can only be measured . unknowns utec7 : total inorganiccarbon. 1 5 ) : tco. K2. and tOHl : hydroxyl ion.and K1. falls within the rangefrom 2 to 12. tHcotl: fu. is to eliminate unknowns by combining them to producea single function that only dependson [H+1.14)and(5.1 4 6 .t o : . Now we must decidewhich numericalmethodto employ to obtainthe solution.(5. first solveEqs.13)to give Equations (s.10)for Kupco. Use these equations to compute the pH of rainwater given that K n = i n 1 9 5 8w h e n t h er e s u l t s l 0 .l r) (5. its root can be usedto computethe pH of the rainwater. providesus with two good initial guesses. ( 5 . C o m p a r e the pgo.1 0 .STUDY 5.6 CASE continued 1ll:  tH+llcol2l tHcotl (5.co'+ # ( 5 .. areequilibrium coefficients. (5. the fact that the pH always beSecond. 1 4 )i n t o ( 5 . was 3 l5 and in 2003 when it was 375 ppm. When selectinga numericalmethod for your computation. to two placesof decimalprecision.3 1) K. of CO2 [H*] : hydrogenion.10) (5.First.l a . (5. considerthe following: .K 1 : l Q . .21 ffi*rnro" o: #*.63 .9)and (5. K z . I s) {#F rcor2l: S u b s t i t u tE e q . to two decimalplacesof precision. 'z##I KuPcoz. and K.. You also know that pH can only be measured One way Solution.(5. l7) this result is a thirdorderpolynomial in Although it might not be immediatelyapparent. r2) ( s .tHCOtl: tCO.To do this.9)and (5..16)canbe substituted alongwith Eq.21+toHl . .[H+] The five where Ks : Henry's constant. [H+].21 : carbonate. bicarbonate. (5. Notice how the partialpressure showsup in Eqs.I 6 ) (5.r ' : fu#+ 106 tHcotl11coJ2l 0: tHcoil + 2tco. r4) (5. a n dK .[H+] Ku. : l Q .12).: [H][OH] . There are a variety of ways to solve this systemof five equations.There are two reasons why bisectionwould be a good choice.
.0868%.Ter using 8 . 5.we can computethe numberof iterationa priori.lr I ( r . Note:Tl Start with initial guesse ate until the approximat 5.7: 0. tl t. (5. irrrl I '. t obtain your solution.) PROBTE l 'i . 5 . iLr..d : t0.63 is correctto two decimalplaces.4 Develop an Mfile b y s o l v i n gP r o b .l I llrr ].17) as a function.{. t. For 2003.6279with a relativeerror of 0.1li)ll. r irilill. For example.')l lrl )*l'i (L. ll'. . : I x l08) at a very low level so thattheiteration limit (maxit) is reached first so that exactly I I iterationsare implemented lrijlli. Make sureto r next highest integer.1ll)t). .. '.lll'r iill" rr .Becauseit is relativelycomplicated.5 s of freelall..*lll*llj*1.'ll r. (5. ILI l i i i i.settingmaxi t to 50 yields ll.ll ll.t. 5.r.'.'lLl.I ..l i. the pH is computedas 5. i l.ir ". li.we must first expressEq. (5. ' ' rrll. theresultis 5.i'...) I ilI't lilrli.'.ir ...l . r. i.L.005. i..3 RepeatProb..iiir. .' l:i.1. 1l)l (ljil. lri:'.l. Remember that given an initial bracket andthe desirederror.. ' ri l Eleven iterationsof bisectionwill producethe desiredprecision. rit i 'r. Befbre implementingbisection.:.1 Use bisectionto de so that an 65kg bungee 4.l 1 l . ' . l . This can be verifiedby performing anotherrun with more iterations.7.ll^i.. 5..t We can then use the Mfile from Fig.7 to obtain the solution. L.i tt.We can be confident that the roundedresultof 5. rl il l'.t34 ROOTS: BRACKETING METHODS continued with an absolute error of Ea. . Howev iterationsand Eq.lr'(l . 5..rrl1l.Notice how we have set the value of the desiredrelativeerror (r.6) gives . I.we will storeit as an Mfile: liiaij f. ll.ll ll Thus.2 Develop your own J ion to Fig. r r ) ll I li) i.'. .''.5) criterion.l'i. Substituting valthe present uesinto Eq.. 5.i.: rl.{).L ) .0001 .rll r1i1l. i. t l L .1r .
(5. i' tt'_. P5. There is quite a lot of controversy relatedto the meaningof the greenhouse gastrends.. The concentrationcan be computed as change can be calculated as. 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 ' mtha soffrcelall.8).This case study illustrates how nunrerical methodsand MATLAB can be employedto analyzeand interpretsuchtrends.5. Most of this debatefbcuseson whetherthe increases are contributingto global warming. but nsc thc falscposition Repeat Incthodto your solution. Although this is cenainly true.engineers and scientists can hopefully usesuch tools to gain increased understanding of suchphenomena and help rationalize the debate over their ramifications.5. Testyour llnction by solvingProb. Usc thc biscction rncthocl to solvc lir thc 1rosition insiclc thc bcantwhcrc thclc is no rnolllcn(. :ll ri.5 .e.3 anclitcrthcapproxilnatc unlil rclativccrror lirllsbcl<tw 2()tr.r Prob.. However.rathcr than usin_r:l toFi. it is sobering to realize thatsomething as largeas our atmosphere haschangedso much over a relativelyshorttime period.Consequently.97o.ri{ .o q l ( l Therefbre. regardless of the ultimateimplications.0E74 i r ^. Notc:Thc accclcration ol'gravitv is 9. Makcsureto roundthc rcsultol'Eq.. (5.5). rememberthat the pH represents a logarithmic scaleas definedby Eq.the hydrogenion concentration has increased about. tL Interestingly.' continued ll .k gb u n r c c .6) as yoLrrstopping . I r . (5.7. (5.ri :0.5. thc rnaxir. 5.5 A bcantis loadcdas shownin Fig.PROBLEMS r35 lii .2 and i.8I nr/sl initialgLrcsscs Stafiwith of'. crnployEq..1.nrrrn andEq. I 5. PROBTEMS b 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 ll Use atn6 5 . 100 lb/ft 100 tb = 0'000I 8. :0. a unit drop in pH representsa l0fold increasein the hydrogen ion. [H*l : lOpHand its percent I : I 1112.Over the coming years.. the resultsindicatethat the l9%orise in atmospheric CO2 levels has produced only a 0.1.L. yourown Ml'ilefirr biscction Devclop in a sirnilarlashHowcvcr. \ lr p ] ]1 9 5 8 ) * 1 0 0 clil:l ( r . I FIGURE P5. Tcstit solvine Prob. Develop an Mfilc fbl thc lnlscposition rnethod..67Vodrop in pH.6)up to thc highcst integer...5.
Discr.the width and the crossscctional areacirnbe related to dcpth r'by P 5.. Perfblm the computation until e.3441 +lj]I01"]o' 1.r ) : .. Il' thc initial substritlc level at / : 0 is So.st your lunclionlirro. In(Sg/S) \a ti.15 Many ficlds ol'engine estimates. (K).6 (a) Dctermine the roots of . and (c) using threciterations of thc talseposition rnethod.ll Thc MichaelisMcntcn rnoclel dcscribcs thc kineticsof cnr)/nlemccliatcd rstctiolls: tlS tlr .5. ond u.13 Figure P5.r s + l ' l20EIt. nrrmbcr ol'yea A: "' P" (t+i).5 kN/crn.and Il K ..(. 5.dcvclopandtcsta biscction {b) Basecl Ml'ilcl'une lion r c l a t i o n s h ia ps to dcternrine 7 as a lunctionol a givcn oxygcncor)ccn( t .J lrolcs/L/d. Use a graphical tcchnique and biscction with the initial intervallrom 0. cither bisec tvherc g : 9. determinethe flrst root of the lunction with (b) bisection and (c) falscposition..'hcre lhc rrorlcnclaltrrc lhc conccntrationof (a) Il'thc initial gucsscs alc sct as 0 and 40 "C. Conser. :0.0 satisfvthe cquation (.: 0 rnd r. .rr :0 and a stoppingcriterion of I o/c./ * ft. r*r.: 2. I ) trction.l3a show early increasi ng distributc ing elastic curvcis rstc Fig 'v : . Q'.10Watcr is llowing in a trapezoidal channclat a ratc ol' whcrc thc subscript 0 dcsicnatcs thc initial concentrution Q : 20 mr/s.4. On thc basis ol soh.1 t3().0: 1.Forexamplc.Gir.0 5. : 2 rlttlcs/L.{bstratc lcveI at rvhichuptake ishalf ol Lhcrnaximumlrnolcs/Ll.(r2 1 9 4 9x 1 0 " Devclopan Ml'ilc to gcncratc a plot ol'S vcrsus r lbr the whcrc o. l0lnd l2 rng/LA'. Use bisectionto dctcrminc (thatis.rl) : ().l (x) : .tJ Detcrminethe positivc rcal rrrtr(of ln(. biscction itcrations would bc rcquircdtu dctcrniinc ternscnting thc nurnbcr ol'rnolcs ol'C thatarc ploduccd. (b) bisection.1 5.000 1 down at $5. * _f 2 . how rlany constitucnt l.s..Tc. with the sameinitial gucsses as in (b).For (b) and (c) use initialgucsscs tll'ri : 0.The critical clepth .: the crossscctionillarca (rrr). (a) Obtainthc solLrtion graphically. x 107 6. tion or' lulsc position to ohtaiu your solution.016.cl to dclclrnincthatoxygcnconccntration rangcs by thc cqLrilibriunt lclationship li'orl 14. 5.64230U l l.r'tirr suchI channelrnust ol cachconstitucnt.\t] 5..62 ntg/l.3i3 graphically._l i(l. O2 0:I L n (b) (a).r w hcre. thc valueof _rwhcn valueinto Eq.r = Inrxi.l r. : thc hallsaturarion constan[.81 rll/s1.T3 : tnr. 1. (i l'cptcscnts r.r'.v and R : thc width ol'thc channel lt the surtacc (m)....i T. Rcmcmbcrthat f.. . wherc lcmpcrature 5..5 and {r. trar it necessaryto determinc s trends of a city and adjacer urbanareais dccliningwith PuQ):Pu.this dil'f'crcntial cquiltion can bc solvcd fbr . conccntration rlum uptakcrltc (rnolcs/L/d). . Justif..l) ro dcr d e f l e c t i o nU . : (). (P5.ltl00 x l0r(l r.r7: l and.r. ^. (b) using threc iterations of the biseetion nrethod. anclitcrate until the approximatc errorljrllsbelorvl% or thc numhcrof iterations exccccls 10.^1 : 20 to r. r : tcmperature satLlrilti()n dccrcascs with incrcasing tempcraturc.5%. : T +213.c lbl theroorwith inirial guesu tr A..0.r) Checkyoul results. anclA.5 to l.2z1.l whcrc .500 pcr ycar Ic you paying'? The firrmular payments A.and casc whctc .7 l.14 You huy a $25. j 5.cn a valucol oxygcnconK: ccntration. Supposc lhat wc rlcl'inca variublc.tr I* Solve lbr the criticaldepthusing (a) the graphical method.: 3 r .n^^ek"'aI .r36 ROOTS: BRACKETING METHODS case. which is thc s{. dctcrntinc thc valucol'. 5..tl.For (b) and (c) use initial guesses of .. can bc churactcrizcd at 0 "C to 6..7 : thc saturation conccntration ot'dissolvcd oxy: l0 ntolcs/L.rrs in radians. thecquation can hc usc.r .20r * l9{2 .T : lJ..this lirlrnr"rla and the biscctiontncthodcan hc uscdto solvc lirr tcmpcratulc in "C.5 iindr.with initial gucsses of r1: 0..L.05'C'l vutior) ol' nlass cirt bc uscd kr lclirrrnulatc thc cquilibrium on (a)..12 A rcvcrsilrlc chcntical rcaction ("C).S "1.+S I FIGURE P5.. 5. Fol this your choicc. For typical rratural 24+Bc u'atcrs in tcnipcrate climatc.ocate the first nontrivialfu()tof sin(.! = suhstrate (rnolcs/L). and (c) falsc position.4I 3 nig/L at 40 ''C.o: 42. s ct h c l i r l l q y * putation L:: 6 ( X ) c m .S: Sir.: 2.7 (a) gfaphically.{asrepre.rss yonr results. ano A .Sp r): gen in licshwatcrat I atnr (ntg L and 4. : ahsolutc *. (i. Choosc of't. : 2c/.r.l. .14 . : 28.In adclition.n 2x))(t 1. pcralurcto an absolulc crror of'0.t l L . is lessthan o.tt.Accordingto thiscquation. E : : 2.9 Thc salurationconccntration of clissolved oxygcn in licshwatcrcan be calculatcd with thc etluation B: 3*. 5.
P5..000. The pararnctcrvalues arc P. " * t . and thc referenccmobility pr1y. : 0.n.riccc ol' cqr:ipnrcnt at$5..(/) when thc sLrburbs 2()%largerthan the city.x : 100'000 people.. tcr isdeclining with timc accorcling area P . 8 5x 1 0 ' t C t ( N m l y .n. F i n d t h c c l i s t a n c c x w h c r c 5 pupaying'l Thc lirrnrularclatingpfcscntworth P.lnd intercst A. 9 ratci is pyments rr.'. . Then sr.. : 75... r a d i u s o l ' 0 . Uscthclirllowingprrarnctcr ion: L .. 10.000 lil' nothing . : 100. 1n co (. givcn 7ir : 300 K. A chlrgc q is l()catcd at a The lirrcc r lhrrn thc ccntcrol'thc ring ( Fig.(r(X) crn.lace nt suhurh.17).PROBLEMS population rvhilethe suburban is growing. l7 A totalchargc 5.rrin .500 Dcl'voar lor' 6 vcars. I u0.13) ..and t.': You buy a $25.. : t 1 . i. r.17 FIGURE * Prr. ittrttual t h c l o r c c i s 1 .max/Po (a) (.To obtain your solutions.. 7 x l 0 r ' ) C..*..l3r) cun'c r = l_l. anclthc electrorrrrobility ll. P. tu.r'corlr ..thc ol'.rwhcrctlt'ld.000 and (b) falseposition mcthods.and k. 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 r n . transportation cngincclsrnightlind necessary to dctcrnrincscparatclythc populationgrttwth o{' (hc Thc populatiort of a city andad.t + 21r.(t)andP.r'.t 'r : ub=25kN/ctn.u.:j(r* : DctcrnrincN.. 1111 3.. cqLlntions rcquircdto computcthe rcsistivity are I qnlr \4'.l3)to dctcrminc valucsin y()r. drstancc cxcrtcdon thc chargcby the ring is given by Usebisectron ttrdctcrr.16 Thc rcsistivily p ol' doped silicon is based on thc chargcr7on an clcctron.. nurnber ol'ycirrs i(l+ l)" A =P i l + l ) . P5. Usc and and a dcsircdlt biscction (b) the lllsc positionnrcthod...000cm*.. usc (a) graphical.t=0) $'hcrc P..The thc dopingdcnsityN iinclthc intrinsic by the tempcrature clcctronrnobilityis describcd I thc rcfThe crcncc tempcraturcI.5 x l0t' V s cm/C. i l t l t r37 llst'r I +LP.iu people.ninc thc noint ol'rnaxirnurn dcf'lcctitrrr value ts.r=0. P.t 120F.I population rcquireaccuratc Many lields of'cnginccring Forcxarnplc.000people.21 x l0')crn : (a) 6.rl + a2yr.".E : 50. Thc ccluatiorr lirr thc rcsultatlyrncreasing hgelastic is (sccFig. P.ncfc iIGURE P5.What intcrcst ratc arc w h c r c c .thc clectrondcnsity n.. l"' P5.. thc valucol'thcrnaxirnurn tP5. distrihuted around a ringB is unilirrrnly shapccl conductor with rldius a.l3rr distlihutcd loacl. (V l350crn r s ) r .u .(XX) kN/crn'.:6. f : 1000 K. 1 : 30."*. . : 0. Determincthe timc and con'esponding 'nt'c P. IL .u.4 : 1 .rr Lr r ) (p5.08/yr. Thc electrondcnsity is givcn in terms of carricrdensityn.as in P rrt rD \ .rbstitulc this into Eq. : ernpiricallyderivcd pavalucsof ralnctcrs.0). P0 : P.I 3 5'13 Figure showsir unilirlnrbcarnsubjcclto a linP5. k..045/yr..
/??o ofthe rockctat tinlc/ :0.1.. 0 0 It o 0 . : 150..:0.lJI rn/sr). q : thc l'uclconsumption g = ths riite../ given a uscrsr.(nc. friction is dcscribedby a dimerrsionl essnumber.(Hint: t ycurr is somcwhcrc bctwccnl0 and...rpplicd valuc of Rc bctween2.If r.50 s. solutecrror in thc'rcsulti.000to 500. 9 5 2 0 x l0 l'1rl Detcrmine heat thetcmperaturc thatconesponds to a specilic oi l. which can all be representcd quantity.qI whereu : upwardvclocity. A firrmula that prcdicts.qt  . l) u.20 The upwardvclocity of a rockct can be computcd by thc tbllowinglblnrula: l' : .(X)0 kg. and4 = 2700 kg/s. irnd = dorvnwat'daccelera(ionol' gravitv (assumcd constant t). 0.61  x l 0 1 I + 9 . is u : thc vclocityat whichfuel : thc initial mass expcllcdrelrtivc tt>thc rosket. 7 2 1 5x l 0 8 r : 9.fot:to r .the F unn i ng .. 5.000005.^ Senl F valu time Hov quic a sir ing I . 6. Designthe lunctionso that it cnsures thatthe ab. ..the by anotlrcrdimcnsionlcss Revnoldsttumher Re...{ lfl llln 5.99403+ 1.+ R Jf Typical valucstbr thc Reynoldsnumbcrtbr turbulentI'low are 10.5838 x l 0 r r T 3+ 1 .000. 0 1 . rt.D e v c l o p al i r n c t i o n t h i l tu s e s biscctiot no s o l v c lor.)Dcternrinc results0 thutit is w'ithinl% ol'thc trr.19 Mcchanical cnginccrs.000anclfbr thc Fanningfiiction tactor arc f ) . in thcirwork. 5.r: 2000 m/s. Chcckyoul answer.500and 1.The Fanning liiction factor is dependcnton a nurnberof paramctcrs related to the sizc of the pipe and the fluid.000. as wcll as mostothercnginecrs. computethc tirne at which u = 750rn/s.l kJ/(kg K).l given Rc is the r.Thc lirllowing uscthernrodynamics cxtcnsivcly polynornial can bc uscd to rclatc thc zeluprcssurc spccilic ttto .on Kurnuul etlutttion: Ir\ ::4toc.lti For tluid flow in pipcs.re value .fr i c' t i on .r38 ROOTS: BRACKETING METHODS (K): heatof dry air r1.kJ/(kg K) to tcmperaturc r.
Repeated application mcthods of these alwaysresults in closerestimates of lhe true valuc ol'thc root.As such. .when the open methodsconverge1Fig.genl In contrast. o . ' .6. 6. the root is located within an intcrvalprescribed by a lower and an upperbound. 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 equation.lc) they usuallydo so ntuch more quickly thanthe bracketing methods. We will beginour discussion with of opentechniques that is usefulfor illustrating a simpleapproach theirgeneral fbrm and alsoltr demonstrating the concept ofconvergence. the fixedpoint Understanding iterationrlethodand how you can cvaluatc its convergenc cc h a l u c l c r i is cts . lb). the open ntethotls in this chapter described requireonly a singlestarting valueor two startingvaluesthat do not necessarily bracket the root.they some(Fig. or the brackcting rnethods in Chap.5. Knowing how to solvea rootsproblern with the NcwtonRaphson methodand the concept appreciating of quadratic convergence. Learninghow tclrnanipulate anddctcrmine witlr the rootsof polynomials MATLAB. la). 6. Suchmethods are saiil lo be conver(Fig. andtopicscoverecl are . Recognizing the diff'erence between bracketing lbr root arrdopenmethods location. and the modil'ied Knowing how to useMATLAB's f zero lunctionto estimate roots. Knowing how to irnplement boththe secant secant rnetlrods. r39 . they moveclosertclthe truthas the cornputation progresses because .
l) is that it provides a tbrmula to predicta new valueof .rr (tLress c n d t h e v o l u eo f t h . J ri r. ( 6 l.r. Newlon Rophson.Sucha fbrmulaca employa fbrnrula be developed fbr sirnple.I ITERATION SIMPLEFIXEDPOINT As just mentioned.]n controst. I in cn itercrtive foslrion.g i v e na n i n i t i a lg u e s s usedto compute .^ iniliol TI 6.t40 ROOTS: METHODS OPEN EXAMPLE 6. the rooi is consircrinecl wilhinlheinfef for ihe open methoddepictedin (b) ond (c). The utility of Eq.r i . T h u s . ^1 ^u # xt lx lH ttru lol FIGURE6. ri s o n t h el e f t .h a n d sideof the equation: r:.11eSexprcssed by the iterative fbrnrula a new estimate ri+t : g(ri) (6.:l Tt an .) c a n h o f a n o l d v a l u eo f r . Thus the thr:shope of the frncrion methodccrneilher(bJcliverge or (c) convergerop.r1 ond r. cr lorrnulois used fo proiectfrorn.I djfference Grophicol deplcfionof lhe fundcmentol betweenthe (o) brocketing ond (b) ond {c) for root loccrlionln (crl.whLch s vol pr^scribed by. as it is alsocalled.rasa function a t t h e r o o t .shed eitherby algebraic manipulafion addingr 1oboth sides of the originalequation.l(.li.r. (6. dependingc..r ) s o t h a t .q(x) (61) pr pr sit gri be alt This tlansfbrnlation or by simpJy can be accourpli.id1y..onepoint iterirtionor :0 successiv se ubstitution b) y r c a r r a n g i ntg hc'f'unction. open rnelhods which is bisection. E Q .redpoitrt itcration(or. io r. opennrcthods to predictthe root..
Rccallthat in Scction5.l./:(r) (6. the approximate error for this equation can be determined using the error estimator: '":lfflrcou" FixedPoint lterotion EXAMPLE 6. is ernployed Suchan approach in Fig.r) and .56714329.r.4) . Aside liom the "rate" of convergence.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..2.vr.6 . An altcrrtutivr'gruphic aa p lp r o u c h i s t o s e p a r u ttc h cc q u a l i o n i n l ot w o c o m p o n e n p . 6..6) to thc errorfiom the previous iteration. Noticethat the true percent relativcerrorlbr eachiteration of Examplc6.5 proportional to 0. this iterative Starting with an initial grress of r11 equation can be appliedto computc: rt U I 2 3 4 5 6 7 B 9 le ol.. I is roughly (by a firctorof about0.fi+l : C" :0. "/" le.3) Problem Stotement. sin ta r t sa .l. This propcrty. The function can be separated directly and expressed in the form of Eq. we graphed a functionto visualize its structure and bchavior.x.1 Simple (6. The concepts of convcrgence and divergence can be depictcd graphically./le'1. is charactcristic calledIinearconvergen(e.2) as .l (r ) : e ' . Solution. eachiterationbrinssthe estimatc closcrto the true valueof the root: 0.t'::. "h t 00 000 76322 35 I 35 22 450 t) 755 6 894 3 835 2 tsg ) 239 o 7a5 0 399 le. we mustcommcntat this point aboutthe "possibility" ol'convergence.ftG): JzG) Thenthetwoequations .ll(. ol'fixedpointiteratron.2a for the f unction. 1 S I M P LF EI X E D POINT ITERATION t4l As with many other iterativeformulasin this book.5 ) (6. (6.Use simple fixedpoint iteration to locate therootof /(r) : e '\ .
. 6.{(.1 6.:r2].2 FIGURE 'h' ::^:Jr 'l root the fordererminins methods.3.rr.r1. a starting Thus.e rvvu "' srophiccrl l9l !1ll::' i u n c t i o n s i h " . 6..yz method: fixedpoint of the to the first itcration areequivalent the yrcurve.re is usedto obtainanestiand in the plot.the initial guess to horizontally lefl moving by curvel. (6. of moving to [. with case was the As separately' can then be plotted ancl. Eq.".)'t : 't and tbur diflbrent plottedin Fig. g(r0) l.Thesemovements .3a).rr] is located pointon rhe. Ot .(6.4)and(6'5).(l'2b)..r 1: g ( x s ) valueof .v2 : g('r) ate tion of the two cufves..il.rr)l and then to [x2.therootsoll'(x):0correspondtotheabscissavalueattheintersec' shapeslbr.Thervalucscorrespondingtotheintersectionsof of /('r) : 0' the rclots represent thesefunctions and divergence 'l'he twclcurvemethodcan now be usedto illustratethe convergence =r equiltions'\'l pair of a as First. u"E' i. The function .tot ooiniwhereit crosses ""li.l) can he rcexpressed of fixedpointiteration.y2:g(r). The point [xr . o * p o n " n t o f l n i e r s e c t i o n t h e lhe. consists mateof . The next iteration iteri roo Fig Cha por slop . theconesponding is usedto <letermine of r11 For the first case(Fig. Thesetwo equalions Eqs. FIG Gro ilnrn osc canbeplottedseparately(Fig..142 METHODS OPEN ROOTS: 6. in both the equation This g(.
3 o f ( o )o n d ( b ) c o n v e r g e n c o en d ( c )c n d i d ) d i v e r g e n c e of simpefixedpoinl G r o p h i c o ld e p i c t i o n whereos(b) ond (c) ore colled iterotion. Chapraand Canale(2002).r)l<1. Fig.I SIMPLE FIXEDPOINT r 43 J0 (cl (d) FIGURE6.3c and d.rt. wherethe iterationsdivergefrom the root. of r move closerto the The solutionin Fig. o t e t h o tc o n v e r g e n co when g'(.this is not the casefbr root with eachiteration.ITERATION 6. 6.3b. However. Grophs (o) ond (c) ore co led monotonepotlerns . 6. o s c il o l i n go r s p i r o p o t t e r n sN ec c u r s iterationis equivalent to the equation 12: s(. 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. As described in A theoretical derivation can be usedto gain insightinto the process.3a is convergent because the estimates The sameis true for Fig.
the approachrapidly converges on the true root.144 ROOTS: OPEN METHODS Consequerrtly. Notice also that if the derivativeis positive. . The I'irstderivative functioncan bc cvaluatcd o1'the as /'tt) : <" I T which can be substituted into Eq. t/ . will theenors (Fig.2 NEWTON. EXAMPLE 6.a).567 IOO llB a )47 0 0000220 EXAMPLE 6.'1 \'..tremployingan initial guess ol'. l).6)to give alongwith the originall'unction '\'. 6.3b andd).l Gi) f o.)1.10 8 Thus.. if lg'l < I. As in Fig.son lbnnulu. Use thc NewtonRaphson rnethod to estimate thc root of /(x) = .l which is calledtlreNcwtonRaph. . 6. Notice that the true percent relative each el'rorat iteratiorr clecreases much faslerthan il doesin simple fixednoint iteration (compare with Example6. If the initial guessat the root is .3aandc). the errorsdecrease For lg'l > I theenors grow. the first derivative at. eslimatc o l ' t h er o o t ..the enors will be positive.l.2 N e w t o n . ? ' ..r1 ^. "/" 0 I 2 3 4 0 0 500000000 056631t003 o 56/143165 1432()0 4 . (6. 6.r is equivalent to the slope: /'('r't: /('r'to Ji 'ft+l which can be rearranced to vield ' r r + l: ' ..andhence the errorswill havethe samesign (Fig. a tangentcan be extendedfrom the point The point wherethis tangent crosses ther axisusuallyrepresents an improved Iri.re : 0. .0. I (. change sign on eachiteration 6. (66) f ( . method can be derived on the basisof this geometrical interThe NewtonRaphson pretation. this iterative equation can be appliedto compute le.r1.4. with eachiteration. Solution.3 P R .il it tt Startingwith an initial gucssof . z. If the derivative is negative.6. e'.R o o h s oM n ethod Problem Siotement.r.RAPHSON Perhaps the most widely usedof all rootlocatingformulasis IlteNewtottRaphsott method (Fig.
Determine lhe positiveroot of .jor reasons fbr thc popularity ol'thc rnethod.7) Thus. However. 2002)provides insightregarding the rateof'convergence as expressed by (6.r. Solution.thc crror shouldbc roughlyproportional to the square ol'the prcvious error.r : 0. Eq. This behavior is calledqruulrilic (onvcrgcn(eandis oneo1' the rra.2002).5. The NewtonRaphson tbrmulafbr this caseis .E 3 A S l o w l y C o n v e r g i n gF u n c t i o n wiih Newton'Rophson Problem Stotement.A special casemultiple rootsis discussed elscwhcre and Canale.4 Grophicol depiction of theNewtonRcrphson method.cven whcn dcalingwith simple roots.r oxislo provide on estimote of ihe rootot . (Chapra ln addition. /'(. (6.3)can be usedas a termination crite.rr0Raphsor.t .)rI: which can be usedto compute .tl t . s i n t h ef b l l o w i n se x a m o l .rion.2 NEWTONRAPHSON t45 l(r) FIGURE 6.6. thcre irrc situations (Chapra whereit pcrlbrmspoorly. a thcorctical analysis andCanale.l(l) : . the numbcr of' significantfiguresof accuracy approxirnately doubleswith each iteraticrn.11 .r methodand an initial guess of . A tongent to ihefunction of .In other words.r)l is exlropo As with otherrootlocation mcthods.difficulliescan also a r i s ea . e E X A M P6 L.r. tl { )  I using the Newton l. Although thc NcwtonRaphson method is ofien very eflicient.r r :.i r . oteddown to the. fthotis.
432 4A 4l 42 0 0 2 3I 6 aaoa24 2130 o 22g 0 002 is converging on the true root of I.6a depicts the casewherean inflection .485 4t 8365 37.as illustrated Fig. but Thus.5 Rophson method for o cosewilh s ow convergence The Grophicol depiction of theNewton how o neorzero shoots thesolution for fromtherool Thereofter. other difficulties can in Fig.6.a simpleplot of the first few iterations Noticehow thelirst guess is in a regionwherethe slope isneat helpfulin providinginsight. to a new zero. afterthe first poor prediction.5 5 16 5 46. "/" 0 l 2 3 4 0. the technique at a very slow rate. 6. FIGURE 6.65) where. inset shows slopeinitiolly thesolulion veryslowly on lhe root converqes l(x) 2E+17 1E+17 FI T Aside from slow convergence due to the natureof the function.65285 99.The solutionthcn plodsalong value(./(x)has an extremely high value. 6.146 ROOTS: METHODS OPEN xi leol.5.r: 5 fbr over 40 iterations until convcrging on the root with adequate accuracy. For example.Thus.6. arise. is Why doesthis happen? As shownin Fig. the first iterationflings the solutiontar away from the initial guess 1.
6.6 poorconvergence method exhibits wheretheNewtonRophson Four coses .2 NEWTONRAPHSON FIGURE 6.
6. Thc firnctionto bc evaluated is .(6.e. Obviously..r48 ROOTS: OPEN METHODS ' point (i. asin Fig.r."x. ^ : l ! 1. / L r a I J n ( G ( x ) .6.it can be invokedto solve tbr root.2.6c showshow an initial guess thatis closeto one root canjump to a location several rootsaway.9.' EXAMPLE 6 4 ' NewtonRophsoB n ungee J u m p e rP r o b l e m Problem Stotement. The acceleration of grlvity is 9.rrt(#.) .6.It also suggests that good cornputersoliware shouldhe designed to recognize slow convergence or divergence. (E6.r2 . as in the algorithmin Fig.3 SE As the ma '+#:irF""^(.re progressively diverget'rorntheloot.7 to detcrntine the rnass ofthc jumper with a dragcoefTicient bungee of 0.5) Ft An . it means thatthesolution shoots off horizontally andneverhitsthex axis.7. nl ) / .6b illustrates thetendency of theNewtonRaphson technique to oscillatearounda localmaximumor minimunl. a zeroslope[. method canbe easilydeveloped Notethat theprogrammusthaveaccess (ofunc). or.Suchoscillations maypersist.1 I ATLAB Mfile! newtraph An algorithm lbr the NewtonRaphson (Fig. Theonly remedyis to havean initial guessthat is "sufficiently" closeto the root.25kg/rnto havea velocityol36rn/safter4sof fice fall.6b.6d.These to thefunction(f unc) andits first derivative can be simply accomplished by the inclusionof userdcfined functionsto compute these quantities. Thus. Notice that iterations beginning at.81 nr/sr. Usc the Mfile functiontiom Fig. 6. lor the simplcfunction.a nearzero slopeis reached whereupon thesolutionis sentf'arfrom thearea of interest. Figure6." . Alternatively.. Forexamole. As in Fig. Its convergence depends on the natureof the lunction and on the accuracy of the initial guess.41) To apply thc NewtonRaphson method.This tendency to move awayfronr thearcaof interest is dueto thefact that nearzero slopes areencountered.r) : 0l is a rcaldisasterbecause it causes divisionby zeroin thetrewtonRaphson fornrulaIEq..6. thc root cun he dcterminecl as in : ' j ' n e \ ^ .the derivativeof this function rnustbeevaluirtedwith resDect to the unknown. they can be passed to thefunction as arguments.H') fir r.thereis no general convergence criterionfor NewtonRaphson./'(.7). And for some functions. Fig. ( 4 ( x ) 2. Alier the Mfile is enterecl and saved.no guess will workl Good guesses areusuallypredicated on knowledge of thephysical problemsettingoron devices suchas graphs that provideinsightinto thebehaviorof the solution. Solution.'(l. 6. 6.f (x) : 0) occursirr the vicinity of a root.f ltnl :rff""n( grii.6)1.n: 6.
. 1. y r .p2.50 .25lm) ^ (1 / 2 .additional p1.'tj]6 METHODS 6. if xr = 0.orimate relative error = number of iterations % iter if if if itoz nargin<3.dy .f u.25)*l. r 3 1 / ( 2 * p . end (1) while = xr . df unc.anh(sqrl.xrol.rc(xr)/df unc (xr) . xr. a potential problemin implementing the NewtonRaphson methodis the evaluation Although this is not inconvenient of the clerivative. ) . d i r n c .d) /xr) if ea <= es i iter >= maxit.3 SECANT As in Example6. ea. error ( 'at least 3 input arguments required' nargin<4 isernpt)'(es).0001?) (default % es = desired relative error (default = 50) % maxit = maximum allowable iterations ..n e w L ' d p h r f u n .t involvcsa bit of concentration andeflbrt to arriveat thc linal rcsult. es. end O. 9.zat irze of f unct lon ? xr = init ia1 guess . break.end nargin<5  isempty (maxit ).thereare certainfunctionswhosederivatives may be difficult or . .0001. 2 5 l m ) * 4 ) ^ 2 .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 method W e s h o u l dn r e n t i o n t l r a ta l t h o u g h t h i sd e r i v a t i v c i s n o t d i f T i c u l[to e v a l u a t e i n p r i n c i p l ei. v o ' d r g i n ) % newtraph: NewtonRaphson root Location zeroes % lroot.. 0 .25))*tanh((9. .es=0. B l * 0 . end FIGURE 6. > > d y = G n I / 2 * : .8l*0. r"> ncwLr. wtr nLih [o evaluatc the root: >> y = Gn sqrt ('j.81*m/0. 1. fbr polynomialsand many other tunctions. e a .1 t l.4.r c 1 r l1 .m a x ' L . parameLers usted by function % % output: % root = real root (?) % ea .pl .0. naxit.p2.rph({.. ea = abs( (xr . F S . 0 0 0 0 1 ) \.81*0.maxit. .1 * 4 ) 9 . The two fbrntulascan now be used in cilnjunctionwith the function n.3 SECANT MEIHODS f u n c c i o n I r o o u . it:erl=newtraph (f unc.. xrold xr = xr .6. i r e r J. (9.'EI/ (m*0.appr.25lm)*4) l6. . ) : ? uses Newton Raphson nethod to find the root of func % input: % func = name of function ? df unc = nane of derir. end end root : xr. * s c c h ( s q r l ( ( 1.\2. iLer ir<r * 100.
f (xo+ 6xo). Se This approximation can be substitutedinto Eq.As in the fbllowing example. (6.) ' { x i \ = .8) Th Equation(6.8) is the fbrmula for the secantmethod. it is not classifiedas a bracketingmethod.f .xi 6.because is not required to change the signs between ./(xir) . der EXAMPLE 6. However. Inserting First iteration: xrt : 50 .6) to yield the following equation: xi+t : xi /(xi)(xi t xi) . ) . .r.6)to yield the following iterative equation: ri+l :  "tr 6x. the parameters into Eq. 8 l m / s 2 .4. l % o .00005 Jl asi alw cha nifir rese met 6.25 kg/m to havea velocity of 36 m/s atter4 sof bungee freefall. (6. U s e a n i n i t i a l g u e s5 so 0f kganda value of I 0 6 for the perturbation fraction.9) swa Eq. Rather than using two arbitrary values to estimatethe derivative.5r) (4.the derivativecan be approximatedby a backward finite divided difference: f (xit) .9)yields Solution.ol:43.r)./'(.l'(x..5 M o d i f i e d S e c o n tM e t h o d Problem Stotement.r) initial estimates of x.579381 .(4. 3 9 9 3 1 ( l:€ .Notice that the approach requires two (.ri) (6..it providesa nice meansto attain the efTiciency of NewtonRaphson without having to computederivatives.f (xi + 6.ri) dri where 6 : a small perturbation fraction.For thesecases.f(ri) xit .1 (6.f(x.4 Ml The but ofb fepr l'(xo) : 4. (6.57938708) I 0b(50) 4.57938708) whe Nott = 8 8 .This approximation can be substituted into Eq.t nrcthod.3 18 .rso ROOTS:OPEN METHODS inconvenientto evaluate.f(xi+lxi)f(.57938rI r8 . Use the modified secantmethod to determinethe massof the jumper with a drag coefficientof 0.ro*dru:50. We call this the modified secan./(.57938708 I l8 .f estimates.4Vo) le whe ..an alternative approach involves a fractionalperturbation of the independent variableto estirnate.Note:Theacceleratio gn ro a fv i t y i s g .
If it is too big. lxa xl ) ) where x0 and x1 are guesses that bracketa sign change. x a J where funcrjon is the nameof the functionbeingevaluated. "/" 64971 38064 I3064 t 53B o o2t 4 l x lO' 3 4 x l0 ' le.1. . but possiblyunreliable The MMLAB f zercr functionplovidcsthe bestclualities of both.9).69220't1t) l3.l7c: lo.For suchcases. a f unctionmight consist of nranylinesof cocle thatccluld takea significantamountof execution time to evaluate. if chosencorrectly.6922017 t) t.rr:88.it providesa nice alternative for caseswhere evaluatingthe i s 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 s inconvcnient.3993 1)(.functionsare not like theoneline alwayssimpletbrrnulas equations solvedin thepreceding in this examples For example.l. If J is too snrall. In sorne cases. the technique can becorne Howarrdevendivergent. A sirnple representation of its syntaxis f z . T" 43 438 28 762 II 746 I 5t7 a a2t 4 I x lO '' The choiceof a propervaluefor 6 is not automatic.1.4 MATLAB FUNCTIONz fzero The rnethods we havedescribed to this pointareeithcrreliable but slow (bracketing) or fast (open). Perceived in this scnsc.The f zero functionis designed to find the realroot of a singleequation. in its m<rst a univariate an entitythat returns sense.f(. 3 9 9 4 0 . Note that two guesses that bracketthe root can be passed as a vector: f zero (funcLion.3993 l: . and x0 is the initial guess.l :28. : 124./(.16a/") je.rr): 1.39931 . derivative g general Further. functionis rnerely a single value in return for valuessent to it. the functionmight evenrepprogram.F U N C T I O Nf: z e r o 6.08910( The calculation can be continued to yield i 0 .tr : 88.6922035 (r6 1. the methodcan be swampedby roundoft error causedby subtractive cancellation in the denominator of inefTlcient Eq. ever. r 1f d r 1 : 8 8 .692203516 106(88.4 MATLAB l5l Seconditeration: . resent an independent computer and modifledsecant the secant methocls are valuable. 6. chapter. (6.rr+dxr) .2 3 4 5 () I xi 500000 B B3 9 9 3 t24 ABS7 ) 4 A5 4 1 7 t 42 7A72 1 4 27 3 . e r o( f u n c t i o n .69220111 . 7 6 14 2 7 3 7 a letl.
r1.the root estimate falls outsicle the bracket). 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 . if a signchange o{ >. As in Fig. (i.l(1) x'2 !). . directions until a signchangeis increasirrgly bigger steps in boththe positive andnegative detectecl..4) f :ero(tj (x) x^2 9. 1. it firstperThe f zcro functionworks as filllows. r : .9.1 'lhe' f unction val ues.r'axisrepresents the new root estimate. quadratic The inverse interpolation line with the.6 Th Pr< .1.1.0 Ii) x^2'.. pa us c .14 4l) IS  .r52 ROOTS: METHODS OPEN . : I 1 r u. lf abad lessan unacceptable root is obtained with one of resulthappens.Lhc clif fcr in srgrr.8b).9 for theroot of a simplequadratic:.f: Hereis a simpleMATLAB session thatsolves 3 root: Clearlytwo rootsexistat and 3.r l' pr( So ..g. i t l i n d s t h e n e g a t i v er o o t >> x x3 asin If we wanted to ensure that we fbund the positive root. 6.. ' t v .e. This search from lhe incremental search forms a search to identily a sign change. . we could enter twil guesses >:. ir.16(t. the fast methods(secantand inversequadraticinterpolation) resultoccurs(e. To find the negative >> x = fzero(Glx) x3 If we warrt to find the positive root.: x fzcro((4(x) x"'). 6.8rr.a parabola) that clncomputing a quadratic equation usesa similarstrategy but is based goesthroughthreepoints(Fig. difl'ers in thatthe search starts at the singleinitial guess andthen takes described in Section 5. an errormessage is displayed doesnot occurbctween Also. r1 e ' n c l p o i f l l . 4) r^2 9. the secant l'he intelsection of thisstraight on computing a straight line thatgoesthroughlwo guesses. f2c. lf a singleinitial guessis passed. lrn'ersc quudrutic interlnethodancl inverse algorithnrs: the secanl poltttionis sirnilarin spiritto the secant rnethod isbased method. : j l i l u : i t ill rnethocl of the relitble bisection with two t'aster The f zero lunctionis a combination quaclratic interpolation. bisection is inrplemented until an acceptable wl et( thi EXAMPTE 6.0) F C 1 rh x3 thetwo guesses. unareused Thereafter.
optron. bisection typicallydominates thefastmethods. e r c :\ f u n c : Lj o n . x A . .8 N.5. The opt imsct functicln has the syntax optiors o f i L r m s c t . i o n s .I using the NewtonRaphson mcthodwith an initial guessol'0. v c t l .J h a s t h c v a l u e v a l . ' p a r ' .. . The parameters that are cornmonly used with the Izer o function are clisplay. R e c a l lt h a t i n E x a m p l e rl0 . . .<] . 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 .. Whensetto'irer'displaysadetailcdrecordol'all theiterltions. . J where I xr . fx] i z . o l . f . Notc that il'you dcsirc to pass in parametersbut not u s e t h e o p t . EXAMPLE 6. p l . Solve the same problem w i t h o p l i m s e t a n df z e r o . 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 wheretheparameterpareters can be obtained by rnerely entering opt. t t i o n s . Lolx: A positive scalar that sets a termination tolerancs on x. are any parermeters that the function requires. r ' r o l h etrh o n i n 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 at first but asthe root As might bc expected. An interactive >> opLions = op1imset ('display'. ' . w e f b u n dt h e p o s i t i v e P r o b l e mS t o t e m e n t . a vector containing the root x and the function evaluatcd at the root fx. 'ite r'). l ' .tl . .6 The r toofld. at the command prornpt.s is a data structure created by the opt.( ' p .v. x ) : 6 . l r ' .i rnsct. . 3 . p 2 .o i e t h o tt h e interpolotlon quodrofic o 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 Comporison " ecouse t h eq u c d r o t i c function i s w r i l i e ni n . . the technique A morecomplete rcplescntation of the f'zcrrosyntaxcan be writtenas Ix. as fbllows: MATLAB session can be implemented Solution. shiftsto the tasterrrrcthods.F U N C T I O Nf: z e r o 6.:2. is approached..1 rl iir F un c t i o n s r o o to f .4 MATLAB J(x) (al FIGURE 6. .. and p1 . .lmseL function.
52 4.24\21 :.5 c.sect i on bisectron bisecliorr interpo laf.5.14.9993s1 0.0. functionltl set a low maxinrumtolerance and a lessaccurate estimate ol'the rootresults: >.999454 Procedure initial search search search search search v n n r a fr E 23 24 25 Looking 0 .98q128 0.('lolx'.385007 1 2. 3i a . searcll search search 6 t 0. It thenuses fzero finds a signchange..l. 1el).9q9267 0.485358 4.103511 .rl: eig we 0 Thus.47I rL6 f (x) 0.5 POTYNOMTATS Polynomialsare a specialtype of nonlinearalgebraicequationof the generalfbrnr t a.95254E 0.Q 30 31 3'. interpoto theroot so that interpolation lationandbisection until it getscloseenough takes overand rapidlyconverges on the root.xl 1 0009 0.9P.154 ROOTS: OPEN METHODS Funccount L 2 5 6 x 0. a .998554 0.0:3itsJ 0. a. r ): d 1 . 4. L 141. .e1.I4 for a zero 0..998709 0.999023 4.. Suppose thatwe would like to usea lessstringent We canusethe opt imset tolerance.0090 Di {.oplions = optim:.5r4r42 0.61 2636 O.324 5e D11 0 /. f r ( . t "* a 2 x ' ' .998894 1.48 0..6BlB5e005 1..orr br.a1 + .. 1.t ertJ the MI tior foll .90tr318 1.626056 o .ion interpolaLion inlerpolatlorr rnterlrolaLiorr i nterpolat i ni erpoIat i orr i on 26 21 2B '2 .1442.options) >) lx.0 .141 interpolati.after25 iterations of searching.2572t I 0. B306ie 007 I.00001 I If n( H U de let wl no 34 l1 Zero found ir'l VA it'rv.'+ ou t.. = tzcro(G (x) x^10 1.! .y2  u. 01Laarl 7.14122 in lhe interrral 1 0.14 I.s the 6.
r t + a + x z* o 5 x+ a 6 : 0 yields Dividing by rrsand rearranging 5 I : (Il f dl I (l I l' (ll I (14 At . and the a's are constantcoefficients.Consequently.5 POLYNOMIALS r55 In many (but where n is the order of the polynomial. It has the usefulpropEquation(6. The roots functionhasthe svntax. aredescribed tion. However.1 MATLAB Function: roots If you are dealing with a problem where you must determinea single real root of a polymethodcanhaveutility.13) r:ompanion matix. Polynomialshave many applicationsin engineeringand science. andelectrical circuits. So hclwdoesthe roots functionwork? MATLAB is very good at finding the eigenis to recast the root evaluation task as an valuesof a matrix. not all) cases.in many cases. Examples 6. (l\ Ul (Irr Al (6.the techniques suchasbisection andthe NewtonRaphson desireto determine all the roots. eigenvalue we will be describing we will merelyprovidean overviewhere. structures. . In general.r5 + o 2 x 1+ ./t.they are used extensivelyin curve fitting.one of their most interestingand poweris in characterizing linear systems. the rootscan be real and/orcomplex.1 ) (6.Because eigenvalue Iaterin the book. the algorithmunderlying are the rootsof the polynomial.an nth order polynomial will haven roots.fbr this task. r . mechanical devices. Its applicapolynomial in the manipulation functions. nomial. engineers fbr like bisection arenot available simpletechniques and NewtonRaphson Unfbrtunately.12) fiom the righthand sideas A specialmatrix can be constructed by usingthe coefflcients '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. x. erty that its eigenvalues of merely settingup the companionmatrix and then using the roots functionconsists MATLAB's powerful eigenvalueevaluationfunction to determinethe roots. the coefficientswill be real.MATLAB has an exceldetermining all the rootsof higherorder the roots function. roots(c) the polywherex is a columnvectorcontaining the rootsand c is a row vectorcontaining nomial'scoefficients.in particular. lent builtin capability. alongwith someotherrelated following example. Suppose we havea polynomial .For example. However.both real and complex. However. ful applications dynamicsystemsand.For suchcases. l3) is calledthe polynomial's Thus. .5. the approach problems problem.. includereactors.6. polynomials.
ir la) Fin i i .r ' ' . Ftir example.0000 0 4. by ryping :> p c )l y v a l (a.0. nonliai'scclef ficients.. roots: Solution. Its syntaxis '' '' l'1Y(r) 6. ..000r1 0 calle calcr ..5 1]) 1.3 . 2 :5 .875 1.5. 1 5 ( t t+ t). which whenpassed functioncalled of the roots.25 / ! ( x ) : .1 .r56 ROOTS: OPEN METHODS We shouldnote that roots has an inverse po1y.5000 1. r '* Note that this polynomialhasthreereal rcots:0. t B 0 . w i t ht h er e s u l t l.12 (E6.\ i. 8 7 5 0+ ) 1 .5.'l'2.3 .0. 2 5 : + 2 . Polynomials are enteredinto MATLAI) by storingthe coefflcients as a row \/ector. entering the following line stores the coefficients in the vector a: '' a = f I 3. I ) wit fbu X 2.r] = deconv(a.15 2.. 1. .will returnthe polynornial's the values coefficients.0 .2 ' . Z . T h i s q u a d r a t i c i s ( .( Be )'> cie where r is a column vector containingthe roots and c is a row vectorcontainingthepoly. 5 . 5 ) ( . 1.3.5000 0. r r * 0.+ 1 2. We i I r I i ' We X: . l ) : 0 . (l)5.7.251. Use the following equation to explorehow MATLAB canbeemployedto ntanipulate polynomials: .1)and a rerrrainder(r) cl 1.51. 1 0..and 2: and one pair ol complex I +0.5 .5. It can be entered into MATLAB as the vectorb: . 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 s nd Determine T h e i rR o o t s Problem Siotement.'/5xt12. 1 2 5 i l )2 3 .r. EXAMPLE 6. .0000 Note that the pc.500t) We can divide this polynomialinto the originalpolynornial by r:> [q. We can then proceedto manipulate the polynomial.0000 0.b) Bqe man of l' rang with the resultbeinga quotient(a thirdorder polynontial.125.5 2.:tb0o We ciin createa quadraticpolyn<lmial that has roots con'esponding to two of the o r i g i n a lr o o t so l ' E q .0 ..1) r 1.r : I. r* l ) = .5(l)1 r 2 . r .1y function can bc useclto pellirrrn tlre satle task as in : D b = poly([0.For exarnplewe can evaluate it at . u 7 5 .1.5()00 (r.5a n d .
in topics throughpipelines andcooling systems.00rlrr 0.A0 2.l: 1 .000t) + 0.S(. rJ000 1. ln engineering.0000 0.: x I.6 CASE STUDY 157 Becausethe polynomial is a perfect divisor.7.0000 r. to nutrienttransmission througha plant'svascular rangingfrom flow in bloodvessels number The resistance to flow in such conduitsis parameterizedbya dimensionless providesa meansto called the.otion calculatethe friction factor: o: +* 2orog (rr. we can return to thc original polynomial _:: J l).5011 0 ?.0000 r..0 r t )( l 3. therootsof the quotientpolynomial >> X rootS(q) with the expected resultthat the remainingrootsof the original polynomialEq.5000i 0!0t)(li t. U1#) (6.1) _\: .0000 + 0. 2.0 1.Jal. typical applications includethe flow many areas ofengineeringand science.i2')A 1.lil t:tionJ'actor For turbulenttlow.Now.5000 againby usingthe poLy function: Finally.1).14) . (E6..1. b) . the Colebrookequ. Scientists areinterested of liquids and gases system.Jr\1(X) l.2500 P IP E F R IC T ION in Determiningfluid flow throughpipes and tubeshas great relevance Bockground.\O 3.6..5000i We can now multiply c1 by b to colrLup with the originalpolynomial: > if conv (q.15A() 2. f .87!0 1.2500 We can thendetermine all thc rootsof the originalpolynomialby .8'l!.I ) are tbund: 2.500tr 2. the remainderpolynomial has zero coeffias can be determined cients.5000i r.
5 could be used..79x I 0r N .f)0It)/'I(){)0. 1 r (t i ) ) ) . lJ.03. D : 0. . : 0.: r . slmz. . V = 40nr/s and e :0.23kglm3.08). _ vl ..False position yields a result of similar precision in 26 iterations.005 : 13'743 R e. I 1(1. In addition.005m.008 to 0. liI . The Reynoldsnumbercan be computedas 1 oV D 1. z st l o This value along with the other parameters can be substituted into Eq.t58 ROOTS: METHODS OPEN continued (m). ().0loel=+=" \ 3 . (x/ : 0. i . r f r r i ( ' r r ( i ) ) f lrl.theparameters arep . the root is locatedat about0. > : t .'tti i . ' i ( ' 1 ' ) .they are somewhatinefficient.This can be done easily with MATLAB: and to anticipate Ilt. ill (. V: its velocity (m/s).Forthis case. and Re = the Reynoldsnumber: where e : the roughness oVD Re .rrr As in Fig. an explicit formulationcalledthe SwameeJuin equationprovidesan approximate estimate: f 1.sim2). it is advisableto plot the function to estimate initial guesses possibledifficulties. In addition to appearing in Eq. f {). (6.23(40)0.eitherof thebrack Because we are suppliedinitial guesses eting methodsfrom Chap.thin tube. D : diameter(m).08. 0 0 s ) 1 3 . Thus.'l I.For example.9. r l r .1. 7 4 3 Jlf ) J.l4). 5.926 x l0{ in 22 iterations.. althoughthey producethe corect result..008 andx. r /0.0289678with a percentrelativeerror of error of 5. I(I) 1 r t L i t 1 .#)]' Solution.: .l) 1 l . . 6.."r.0015 mm..st \ g(J'l: +2.the bisect function developed in Fig.. .rl 0.1t ['"(t.( I ) t l*ilLl (rr.325 (6.0000015 2. 7 ( 0 .). and & : dynamic viscosiry (N. I l l t .:i  1.t t.7 gives a value of/: 0. Hr di Ar gu fo ..1)il . but could becomeprohibitiveif many evaluations were made..:__ LL where p : the fluid's density {kg/m3t. t * l ) . In this casestudy. (6.t : l. * \ . Note that friction factorsrangefrom about0. i ) ( lr l t i . the Reynoldsnumber also serves asthe criterion for whetherflow is turbulent(Re > 4000).'"1.! : 1.t)0:r ( i .we will illustratehow the numericalmethodscoveredin this part of the book can be employedto determine/for air flow througha smooth. ] { )..This would not be importantfor a singleapplication. : . / [ l L t.f Before detennining the root. * : : . l4) to give Er gc 0.
r.L .Llir. 6.03 0... [e: 1 i\ i'\iiii i i i 1 .(6.04 0.. 5) _?L! " 0 .using an initial guessat the lower end of the range (ro : quickly: in Fig.l i .06 0..ill1 il .. this occursbecause the function'sslopeat the initial jump guesscauses first iteration to negative value.07 0.14) is relatively straightfbrward good candidate. ).Because to differentiate....066..l r 'liril .For example.....6 CASE STUDY r59 c o n tn i ued ty ne \2 2 of tis t/s tn. l( r. Further runs demonstrate that the to a guess for this case..the NewtonRaphson method is a Eq. 6.Lirll((i.7 converges 0.. ' ' : rl rr I l. ilir l However.\ i\\ r 2 H..08 f FIGURE 6..9 We could try to attain improvedperformance by turning to an open method...1..fi l iilt.08).. t{I j ':.. ... 0 1 0.:irlt i ll ( l i r i rl ir. .when the initial guessis setat the upperend of the range(xn: 0.convergence when the initial is below about only occurs 0.9.02 0.008).t :i' t i.6.1 r .. As can be seenby inspectingFig.the newtraph functiondeveloped )(.the routine diverges.05 0...
However.  0. ) r . r r . r r t r J . .1 i 4 o n u tl b As a final note.16) . / R c ' 0 . it is revealedthata phasebeforea sign changeis detected negativevalue occursduring the search and theroutine aborts. 1( . 'r r c . l ) ! f )I { ) ir .1 r it l. c i rr l s { r J L i n c' l i o n . ' Li I i i .08 that caused problemsfor NewtonRaphson. 9 2 A ' . )0 0 t l l i \ t. (6. . l ' . Aside from our homemade functions. . l b l II .( ' .rr. guessesat the lower end of the range cause problems.. For example.14): f. : . ' L r r . f ' . ) . ' r t . . in this case. 1 ) .1. However.*#))' (6. . )B ? . l .c1r1. t . r i r o r l i i r ( 1 r .l!i. i :. h . r n L c r e d ( l . i1. ' .: i!. .016.. just as with the NewtonRaphson method. t u.j 1 i l j ( .l) J l l .l r . r rI I I l j l ) . . i J : . r i( l f L l r l l i omJtl r:r l : : . s l a r .:rIl!t . / LI rr l .( i : )i ' r ) t ) / ' . ' .. / ' ( i lr : o r r l .(Jllll) I / ( r I ( r ( r . 6. . .1 . f ) . r r c o i .25 ('"'(#.For the Colebrook equation. 1 ../in Eq.1 t r 0 r . l . 1 i l r r . si. 4 . r n i n l . divergence also occurs when f zero function is used with a single guess. 1  r c t r l . a good strategy might be to employ the equation(Eq.6). r r l I l .. L .r :i r r q :rccl r (j1r.I 1 j. 1 L r L c ' ti .i . .. For example. f zero does just fine: ' I I t. l I . . r .. : a t r . i .15) to providethe initial guessas in SwameeJain ' I :l. a i n i l q . t f l ' l r {f i jI 0 1 ' ' I 4 4 i. .for the guessof 0. The easiest and most straightforward versioninvolves solving for the first.for singleinitial guesses aboveabout0. . However. / " D ) + 5 .l .r p )1 .the routine worksnicely. . ) r r \ . l ': .() i t .t60 ROOTS: OPEN METHODS ' continued So we can see that although the NewtonRaphson is very efficient.. ' l 6 1 1( t : r /{ 1 .. .rr / l L .gn . t ) . 9 ) ^ 2 r ). we can also use MATLAB's builtin f zero function. r r . r t I 'lilll .let's seewhetherconvergence is possiblefor simple fixedpoint iteration. L u e . r .. . 7 4 . I f ! t r f If the iterationsare displayedusing optimset (recall Exanple 6. r:. J L ' t t : t r . it requires good initial guesses. ' I 0 I '. . r irj .: 14 . i r .
.6 CASE STUDY l6l !t= s6) 0. Further.6. 6. In addition.008 and 0. 6. Recallthat fixedpointiterationconverges when the)2 curve hasa relativelyflat slope(i..08 "( FIGURI 6.e. but it convergesfairly rapidly! In fact. lg'({)l < l). for initial guesses anywherebetween0.008 to 0. As indicatedby Fig. this simple approach that requiresonly one guessand no derivative performsreally well for this particularcase.01 0.there is usually no single methodthat works bestfbr all problems.0087oin six or fewer iterations!Thus.08.04 0.professionally developed software like MMLAB is not always foolproof.they understand enoughof the underlying theory so that they can effectively deal with situationswhere a method breaksdown. estimates The takehomemessage fiom this casestudy is that even great.IO The twocurvedisplayof this functiondepicted indicates a surprising result(Fig.10. 10). fixedpoint iteration yields predictionswith percentrelative errors less than 0. the fact that the y.08 meansthat not only doesfixedpointiterationconverge. curve is quite flat in rhe range from/: 0.Sophisticated usersunderstand the strengths and weaknesses of the availablenumericaltechniques.
0 1 ) .0(X) kPa. zr./ i t ' .01 .0..0u66R l).r 1./w \ y : _. t i : 0 .13 In a chcmicalcnginccring thatn significant is hcatcdto sLrlTiciently high tcrnpcraturcs portion of thc watcr dissociatcs.} / r .6. w valuesof/ such that 1 : 6.r. I . r*ru u .t) : 0.r *r l l 6 . (K). totalprcssurc of thc rnixturc. r l  0 . it is subje weight.rru)cthod to thisfunc(ion usingan Apply thc NcwtonRaphson y o u rr c s u l t s . thLEq.(b) Did thc nrcthodexhibit convergence for each on(o its r:al root?Sketchthe plot with thc rcsults iteration labclcd.DiscLrss to cxplairtany pcculiaritics in y'our and analyticalr.. 7 .L : 5 rnr.l) l() gct Eq.5.E x p l a i n i n i t i a ls u c s s (H:0) proccss. of thrcc itcrations. < 0. . :0..0175.rgiven and Y6 : 6.r thatsatisl'ies Eq.1 .1 6 .ti: 0.Pcrtirlntthe conrputation l. is lcssthane.t bctwcenl5and20.I + 6 .95. 1 6 .4 Dctcrrninc the l()wcst positivc r(x)t ol' . J: 0 . 0 5 .5t125 ancl t.5 i l n d .3.0. 3.5 until t'. ).l(r): tanh(.1. Along with the initial gucssand the perturbation pass I'Lrnction asrn argurncnt. t i : 3 .ti luf . rcsults.(P6..o. l'inaliutswer.. . 5 .:3'5)' (c) Using thc sccantnrclhod(thrcc itcrations. r  . and a capacitor in par to expressthe impedance . fiirclion.: 3 5 ) .4)7 R r r ': ' 5 l).rc : 45tt0kPa arrd7. l ( .6 : (a) Graphically.r .3 Dctcrrnincthc hishcst rcal root ol ./ (.284"rr polynomial / (. Thc parameten tr irntllt arc calculatccl by . 9 . Calculus can be employ height of the cable y as a Ta . wiitelvapor 6.5mr.. As a chcrnical engi.rl + : 5. r r lt)2. lbr approxirrating thc :iquarc root ol'any positivcrrulttbcr can be firrrlulatcclas . un oldtimcInctho(l 6.05%.I : (a) Craphically. o f ..Usean initial guess of . (86..15 Thc volurncof liquid V in a hollow horizontal o1'r'adius land lcngth L is relatcd to thedepth of thcliquid/tby (a) Use a numerical me parameter ?".ra + 6.. .\' whcrc K is thc rcaction's ccluilibrium constultandp.r ): : s i n ( v 4 ) .6 Dcvclt>p passthc l'unctiorr ils an argunrctrt. isthe ll'7r. r. t. fuel ncer.f) : 0.Ola/a. ..16 A catenary cable i points not in the sarr Fig.10 Thc "di"iclcand avcragc"tlctlrocl.. u s i n g anrniaud usegraphical tial gucss of (a). ttrctl.5 and.1) 1. the fol ofthe cable can be derive d'y ::dxz w TA r:(t:I lt)JT gas constrntl:05ltt kJ(kgK)1. 1 +(. rnethod to thet'unction 6. / ( .you arc askcdto dctcrnrint: thc ilmountof methane that can bc hcld in a 3rnt tilnk a( a tcmperirture of 50'C methodof with a prcssurc ol'65.J atnrandK . suchthat t:50.1 I L \T / J /r given r : 2 nr. the If it is assLrnrcd thut this is the only rcaction nrolctiactiont of HlO thatclissociates canbc Rrpresentedby I A:. 3 5 r + 3 1 .l : 0. . .3. 1 5 . Dctelntine T .Usea rtxrtlocating your choicc to calculatcl and then the mass of dctcrnrine rncthilnc containcd in the tank.\' Tr\ . s{ ( ' . (lt) Using the NervtonRuphson (thrcc itctirtiotts.nl\ */ . 9 . 6 u 7 5 usirrg an initial gucss ot' .c .13. : (). cylindu 6..andl': 8. / : absolute pressurc 7 : afrsolute tenrpcrnturc luncl ir : thc volurncol a kg ol'gas {rrr/kg). (86.3. t 5..2.h : l L V = l l r c .0074. /r .5usingi'11 unlil e.0.7 Dcvelop an Ml'ilc tir thc modificclsccant nrcth()d.ll DiUcrcrrtiate a 6.l6a. 12 Thc 15 12.rc by ccluation ol statcis given 6. : 0. l .2 andtakea minimum . 5 .ll (a) Apply the NcwtonRaphson .2). whcrc 7r. nlethodand (b) lhc nrocli6./^r f Y l (P6.43. Usc an initial{ucss lncl iterate 6. Also checkyorrl 6. (c) LIsing the sccantmcthod (thrce itcrations. t 1 .183. P6.r : 1. Thus. Tcstit by solvingProb.9) to evaluatc its known real rootat . ^ of rir: 0.ucthods rcstrl ts.t1./ (. r .r'): ticclsecant nrcthocl a root .5 ..4.(\l(tr.2 Use (a) fixedpoint itefltion antl (b) tlrc Nc'ur'lonRaphsonmcthod to cletcrminca t'clotoi .0. i ) : .1.8r * 2.t : 0. 6. whcrc R : tlre uniycrs:rl (kPa)..18showr tor.5 Usc (a) the NcwtonRaphsorr (3 : 0. (b) LIsinc mcthod (thrcc itcrations. 3) . T c s ti t b y s o l v i n g 6. : l. . Prob.6.o .5./ (.162 METHODS ROOTS: OPEN PROBTEMS to locate thc root ot 6.r2 : 3. 6. dctcrnrinc of . r .t1 1 Provc that this firnnula is bascd on the NewtonRaphson algorithm.18 Figure P6.r has a rcal ro<.0. Explainyour Along with an Mfilc lirr thc sccantnrcthod. r r +1 0 .M. (d) Using thc rnodificd sccant rncllrod (thrc'c itcnttiotrs. (d) Using tlrc rnodilicd scculrt nlethorl (llvc itcllti()n\.1 Enrployfixedpointitcration . thrr NcwtonRaphson 'r. 6.05) to clctclnrirtc ol . = l9l K.21. its weigh length along the cable u scction AB is depicted in the tension forces at the e cal force balances.14 Thc RccllichKwong R7 ''tlt 6. to filnnoxy(H:): gcn (O1)anclhydrogcn H:OP H: * lO: involved.1. o splits irpart.t:t5 ..tod . and (b) 4. . . t ) r' ..r'. thc valr. (b) Develop a plor of v ) 6. (c) Dctcrnrinc tll thc rootswith MATLAB.I3.17 An oscillatingcurre bY I :9et cos(2tt). thc (wo initial gucsscs. f .35.'1./ (r) : 8 s i n ( .9 Enrploy the NcwtonRlphsonnlcthod to dctennincr c a lr t x r tf o r .t) : .
C' . an inducacaoacitor in oarallel.FfS canbe employedto solvc this equationtbr the ofthe cable l asa lunctionof distance. suchthat the cablehas a height of .6 .I6 A catenary cable is one which is hung between tw<r 116 pintsnot in the same vertical line.0.l6u. it is subjectto no loads other than its own Ihus.s r=50. 1 9 .c FIGUREP6. (r. where T1 and Ts are tension lbrcesat the end. Drvelopaplot of.r: 7. Deterrrrine all oftsuchthatl:3 Figure P6. An oscillating current in an electric circuitis described l=9e' cos(2rt). FIGUREP6.where/ is in seconds. P 6 . A lreebody diagrarnol' a lcngth ion AB is depicted in Fig.I8 #=.l6D. Basedon horizontaland vcrtiforce balances. the fbllowingdif lerential cquation rnode I cablecan be derived.5 H..a b l o c k o f m a s sr i i s @t +(.r : 50 to 100. P6.19 Real mechanical syslemsmay involve the deflection ol'nonlinear s p r i n g sI .18 showsa circuit with a resistor.cosn ur Use a numerical methodto calculatea value lbr the parameter I. and ror whereZ: impedance is the angularlreguenc_v Frnd the ar that resultsrn an rmpedance ot /i (/ usrngthe tzero lunction with initial guesses of I and 1000for the lirllowing parameters: R :225 A.19 .Kircfihofl'sru/escan be used theimpedance o1'thesystemas I ll p: (Q).r': I 5 at and r.1 . and L : 0.y versusr fbr. n F i g .its weight acts as a unilblm load per unit along the cable ur (N/m). As depicted rn P6.PROBLEMS r 63 (b) FIGURE P6. l0r' F.q given values for the parametersut : 12 = $.
polynomial and /r .0.r64 ROOTS: OPEN METHODS P fixedpoint iteration sch givenQ:5.r'1y C(. C(s) : systemoutput.27 Given f (x) : 2xb .0002 your schemeconvergesf< equal to zero.if urr:20 m/s.000g/s2 f. Develop . Thc trdecbry can be modelecl as p o s i t i o n i ns gy s t c nils g i v c nh y .7 to manipulatc and iind all the roots of the 50.t t t q l t tl 6.45 m.4 ) ( . If R : l m. thrown by a right ficlder is clefinedby thc (x. Also.The resistancefbrce F of the spring is given by F\krt! *k. 6. l and n : the Manning roughncsscoefficient. .j5r' R t v .N(s) systcminput.25 Seeifyou can deve the friction factor base describedin Sec.1.llRrl 3 wherc' V: volunreIm]1. fespectively.5(sH)''' rt(B i 2H Jtlt where B : flow (m3/s).r*66 l 9 s r+ 1 2 2 s+ 22 9 6 s. initialguesses of0 and R will bracket a single root ti)r this cxample.22 Pertbrm the identical MATLAII operationsas thost valucs: ft1 : Solvc fbr z/. providejustification fbr your choice of method. transfer tunctions ae u.(tan0o)r Ltrai cosu0 *. A transf'erfunction for a robotic as displayed in Fie.ith the traicctory of a thrown ball.2I Employ initialguesses of ( results. r )= (.5. P6. and R : t h e t a n kr a d i u s lml.6. l s ( x ) : ( .5 1 4. yor result for Reynolds numbe e/ D ranging from 0.2I ) to hold water fbr a small villagc in a devcloping country. 6.S : slope (mlm).Extra wherc zri andb1 : the rootsof thc numerator anddenomina. {): + .n t g < l . and the the appropriate distanceto the catcheris 35 m.(b) Fot 2krtl5l2 I open mcthods.0000 6.20. Note that thc throw lcavcsthc right ficlder's hand at an elcvationof 2 m and the catclrcr rcceivcs it at I m.2 You arc designing a sphcrical tank (Fig.d3i'1 FIGURE P6.20 Aerospacc enginc'ers somctimes computc the trajcctories of projcctilcssuch as rockcts. Determine the approximate relatir"e cror after cach iteration. ( : 9 . an initial guessof R will alwaysconverge.r  2 ) ( .. . given thr: fbllowing parameter in Example6.26 Use rhe NewtonRa f (x) : FIGUREP6. s + d 2 ) ( .. nominatorand t:rctorthescinto the form G ( . k 1 t l '".21 ln control systcrns analysis.5sl*50. 6. 8 1 m / s 2 ./r : depthof waterin tank Im].2O released a distance I above a nonlincarspring. t z : 4 0 g / ( s 2 n r s ) . what depth rnustthe tank be filled to so that it holds 30 m3? Use three iterations of the most efllcient nurnericalmethodpossibleto determineyour answer.l 1 9 2 Find initial angledo. r* 5 ) ( x .x "ns^ Conscrvation ofcnergl'can bc uscdttr show that (a) For hracketing infbrmation: methods. r 3 ) ( s * D r ) ( s* b : ) ( . 5. Use MATLAB to find thc roots of thc numerator and de. r* h ) ( s * l t t ) .A relatecl problemdcals 6.7 ) 6. The volume of liquid it can hold can bc computed as l / n l t '  = where C(s) : system gain.n : 9 0 8 .tt= N(. r+ . and s : Laplacetransfbrmcomplexfrequency. tor. H = depth(m).s * a r ) ( .2l The Manning equationcan bc wriften fbr a rectangulat openchannel as ^ ( =r : . r.S:0.Perform iter tive error falls below5Zo c(s) rt+ l2..1.l ) ( x .) coordinatcs tem's input to its output. 6.t) r'u + Usea rootlocation techn this function. Thc trajectory o1'a ball devr'loped rclatethc dynamics that mathematically of asys. P6.
Note that it is positivcinitial guesscs.Justify your choice. Explainyour rcsults..tr Employ gucsses initial of (a) 2. 6.r. I : 0 and Xi : l. 1 :0 and 0 JT ie le re S ic v.r. choose the technique that is bestsuited to this problem. .0.f(. and (c) 8.r4 * lox * 2 f (t) = (hemaxirnumof Use aroot location technique to dctt'rminc this function.05.r).00001 to 0.0 and r.: 2.B :20..l. use technique determine then thal to the root.0002.usc initial guesses of r1 : [) and.and Pick the bestnunrcrical tcchnique.use an initial guessof .26 Use the NervtonRaphson method to find the root of :'@_ x ) 2 . Ifyou usethe sccant tial guess method.7.PROBLEMS fixedpoint iterationscheme to solve this equation for H given 0 = 5. r65 of x1 .r.03. 6J?Given Zrb . Perlbrmiterationsuntil the approximaterelative enorialls below 5%.1000to  07and lbr forReynolds e/D ranging fiom 0. : l. : 1. 6. If you usethc NewtonRaphson or the modified secant mcthod. S : 0.6.If you usc a br'ackctingmcthod.6. Yor.use an iniof. If you usc thc secantmethod. a . ?"^ = )lr justify your choice.Perfbrm iterrelative If ationsuntil the approximate error fallsbclow 2olc. ). useinitial guesses of x. 6.rr function should return a precise rcsult numberranging from .. Assumingthxt convergence is not an issue. (b) 6.211 You must determine the root of the following easily f unction: di fferentiable n <. If you use the use initial guesses NewtonRaphson or the modified sccantnrethod.25 if youcandevelopa lbolproof function to compute See fie frictionlactor based on the Colebrook equation as described in Sec. known thatfi:rr except all techniques fixedpoint iterationwill cvcntually converge. Lrseinitial guesscsof . and r?: 0.Provethat your greaterthan or scheme converges for all initial guesses equal t0 zero. you use a bracketing method.r.5.
g l o b a la n dl o c a lo p t i m u .driu e n s i o r t a Beingableto developMATLAB contourand surlhce plotsto visualize twodimensional functions. Knowing how to apply the f mi n brid f unctionto deterrnine the minimumof a l'u l n ci t ort. Knowing how tclapply the f minse. Localingthe optinrum clf a singlevnriahlc functionwith thegoldensection search. t .1 YOU'VEGOT A PROBLEM jurrper can be projectedupwarrd n object like a bLrngee at a specifiedvelocity. o n e . (. Recognizing the difference betwecn onedimensional and multidimensional optirnization. * T) tr . Specificobjectives and topicscovered are ' t ' ' ' ' ' ' ' t Understanding why and whereoptirnizafion occursin engineering and scientific problemsolving. Locatingthe optirnumof a singlcvariable f'unction with parabolic interpolation.its altitude as a functionof time can be computed as I ( f I t66 ry.rrch lirnctionto clctermine the minimumof a m u l t i d i n t e n s i o nIa l nction. 'u 7. D i s t i n g u i s h i nb ge t w c e n problemso thatit can bc solvedwith a Knowing how to recast a nraximization r n i n i r n i z i na gl g o r i t h n r .Opti mizqtion CHAPTER OBJECTIVES The primaryobjective you to how optinrization of the present chapter is to introduce can be usedtcldetermine minirrra and rnirxima of bothonedimensional and multidimensional functions... Bcing ableto definethe goldenrati<l why it makesoneand understand dinrensional optitni zatione'flicient.. If is subject to Iineardrag.e I(tn'I)t) Z: zr* 1 (.
7. l . positivcvckrcityis corrsidcrcd tinrc No(c that tlris liurnulation" to bc in thc rrpwlrci 5. t r ' o t 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 . r o s tp r a c t i c a l optimizationproblenrs requirc nurnerical. r / s r . cornputcrsolutions. 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 o9 l '0 i rn bro au ta l h o u t l : .( : a l i n c a r d r a g ( k g / s ) .l r : r n i l s s ( k g ) . .1s . 7 .5rn/r. T h c d e t c r m i n a t i oo nl ' s u c hc x l r c m cv a l u e s is rclcrrcd to ls oplirnization T. 1 INTRODUCTION AND BACKGROUND In thc nrostgcneralscnsc. 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. Root Iocationinvolvessearching for thc locationwherethe I'unction cqualszcro.ngincers t a s k si n a n c f f i c i c n lI a s h i o n l i l r t h c l c a s tc o s t . 1 1: r : : ( 7 .ati()rl is thc ploccsso1'crcatirrg sonrcthing that is as cffbctivc A .rc vli l h i r n i r t t r o l r t .1 E l e v a l t a no t q f u l l a l i .T h L r sc a r c a l w a y sc o n pnrblerls that uttcnrptto balarrcc pellirrnrancc frontingoptirnizittion and linritatiorrs. both involveguessing ancl tilr a pointon a function. The fundarne'ntal dif'lbrcncc between thc two typesol'problerns is illustrated in Fig. ' o . l o : t h c i n i t i a lt l t i t u c l e : i n i t i ' r lv c l o c i t y ( m ) . d i r c c t i o n .':. Frour a nrathcnraticirl optinriz.ation dcirlswith l'indingthe nraxinra and rninimaof a l'unctiorr that dcpends on one or m()revariablcs.h i s c h a p t c r will y()uto how the cornputcr introducc is uscdto nrakcsuchdetcrn'riniltions.2. n dI : coel'l'icicnt (irr (s). That is.I INTRODUCTION AND BACKGROUND Maximum elevation F'GURE 7. ri t l Ok g . involvessearching for the function's cxtrenre optimization . perspective. From a numcricalstandpoint. . searching 5 and6. a n d r ' l 5 k g / s . " . optirniz. .E c 1 .s c r r g i u c e rw t ontinu<lus sh a ( a sp o s s i b l e s . The goal is to detcrnlinc the valuesof tlre variables Thcsecan then be that yield rnaximaor minirnalbr the I'unction. p<lints. q r ( r n / s )a . . ln contrast.s 1 :{ l r r . scientists Iravcintcresl irr optirnalphenorncna firlnr thc pcukclcvationo1' rurnging projectiles to the minirnunrli'eccncrgy.Z. I ) c a n b c u s c dt o c u l c u l a t c 's altituclc A t h c j r r r n pre .j o h o l ' d c t c r r r i n i ntg h 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 .c n r u s e dl c ys i g n a n cp l roductt dcviccs perlirrm .optinri:/.atit'rn is sinrilar in spirit to lhe root locationrncthods wc just covercdin Chaps. l O 0 m . T . back into thc functionto conrputc A l t h o u g ht h e s c s o l u t i o n s c a n s o r n c t i r n cb sc o b t a i n c da n a l y t i c a l l yn . substituted its optinralvalucs.s d i s p l a y c d i n F i g .l r tr< tt/ (dct]ncd w h e r e : : a l l i t u c l( er n )a h i l v ct h cc a r t h ' s sud:rce a s : : 0 ) . In adclition.t\l:u 9c . Suppost ch a ty o u a r cg i v c nt h c .
r) is equal t0 zelo.) analytically lbr r :'!rn (r + !a ) tng/ r' \ r ivcs 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 . thc second derivative.2.In mathenrirtical terms.Forthis case. (l  e i'l"'t'. Additionally.8.2 nf o s i n g l e voriobli e A f u n c t i oo l l u s t r c f itn hq ed i f f e r e n c roofs be elween o n do p l r r n o tht gri As can be seenin Fig. 8 1 m / s t .Ll. andr: : l5kg/c. nt : u0 kg. 0 . this is actLrally thc ccluation fbr the vclocity.55m/s.L l.:g : 9 .r) > 0.r) 0 TT FIGURE 7. the point is a maximLlm.r value whcre thc derivative / '(. That is.168 OPTIMIZATION l'('t) : o l"(r) < o l(x) : o T m /'("t) = o > "f"(.r). the pointis a minimun rootsandoptimawouldsuggest Now. u0 . 7. E q .ln fhct.t'"(x) < 0.r1 EXAMPLE Z.r166s 80r9. you can diffcrentiate thc function andlocateth root (thatis.81)/ l.f'(. by solvingthe root problenr: . Note that because 1). Equation to dg i v e dz sat AS t__ ofr on( prc sea me ltou * ( {oJ PqU rh. understanding thc relationship betwecn a posible strategy for finding the latter. indicates whether the optimurn is rninimumor a .l Determining t h e O p t i m u mA n o l y t i c o l l y b y R o o tL o c o t i o n Problem Stotement.o : 100m. ( 7 . (E7. the zero)of the ncw tunction. 1 )c a nb e d i l f ' e r e n t i a t e Solution. this can be accomplished by settingthedcrivativeto zeroand solvingEq.s \ .: l5ts5t 8o r n/ r r \ :3.theoptirnunrs arethepointswhcrethccurveis flat.1 a maximum:il' . if .this corresponds to the .the problen anrounts to deterrnining the root. 1 ) . "(. Themaximun elevationoccursat the value ol I that drives this gquationto zero. min n : D'e \ ( / t t t) t  tn8 (.ldt.someoptirnization rnethods dojustthis : 11. U s e t h e l b l l o w i n g p a r a n r e t ev . Deterrnine the tirnc and magnitude il1'thepeakelevation basedon r a l u e sf o r y o u r c a l c u l a l i o n.l'"(.Thus.
( t/(' . / . we couldhaveobtained the sameresultusingthe root locationmethods described in Chaps. . L3a.ut)€ "ntlt ln . onedimensional and multidimensiontrl As the name implies. . 3 8 3 1 6 " 192. As in Fig. This will be left as a homeworkexercise.INTRODUCTION AND BACKGROUND This value along with the parameters can then be substituted into Eq.l("r) .m . These methods areavailable fbr both problems.9^ . o t e t h o tt h i s c o n t o u ris lrgure con be tolen to represen n ei i t h e r o m o x i m i z o i i o( ncreose in elevolion up to lhe moximumiike o mountcrin) or c minimizotion decreosein eJevotion down to the icontours minimum l i k eo v o l l e v ) Optimum/ir'.r . Althoughan analytical solutionwaspossible fbr this case. 5 and 6. n \ t . FIGURE 7.  5 0 + " " . possible Althoughit is certainly to approach optimization as a rootsproblem. ) ( t _ .. . Multidiproblemsinvolve lunctionsthat dependon two or more dependent mensional variables. _ .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 T h i sf l g u r eo l s o i l l u s t r o t e hs o w m i n i m i z c i i oo nf 1 1 l r 1i s io equivcrlen t themorimizolion o f . 81 . t t . n . Further. the search thenconsists of climbingor desccnding peaks onedimensional andvalleys.. r ) ( b )T w o . .onedimensional problems involvefunctions thert depend on a singledependent variable. 8 3 t 6 6 ) : 1 0 0 + : . a variety of directnumerical optimization methods areavailable.u'r Minimum.1.r ..9 = .d i m e n s i o n N oo pl l i m i z o t i o n . The fhct that the secondderivativeis negativetells us that we have a maximum. the result makesphysicalsense sincethe acceleration shouldbe solely equalto the force of gravity at the maximum when the vertical velocity (and hencedrag) is zero. " .. ."8 .1) to computethe maximum elevation as z: R lr \ 80 / 80rQ 8 0 6" 1 _ . (E7.g60 9 m 15\ rs /' ls We can verify that the resultis a maximum by differentiatingEq.: (llc .t. / ( .1 r r.1)to obtainthe secondderivative d2z ..' t/t. 1 58 0 . (7.
we will describesome of the mole common approaches for onedimensional Then we will providea brief description optimization. wc mustbe concerned aboutmistaking a localrcsulttor theglobal optimum. that includelocaloptima Cases are calld multimodol. 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 . Justas in rool location.is betterthunits inrmecliate neighbols.4 A {unction thirl osymptoticolly trvo moximum opprooches zero ot p us ond minLrs oc ond hcrs ond two rninimum p o n l s i n t h .r). of how MATLAB can be employed to determine optimafor multidinrensional functions.7 .3b). 7. ) .instead is examined the topography to efTiciently reachthegoal.v i c i n i t y o f l h e o r i q i n . notth though very best. As dcscribed in thenextsection.d i n r e n s i o n a lller coaster"like lirnction dcpicteclin Fig.r70 OPTIMIZATION ln the samespirit. In ad provir termir T roots i guess tween The rc xr Global maxtmum FIGUR Euclid s rotioof smoile Global F 1 . 7. 7. In aft the pr( ofi 7. h o t hl o c a lu n dg l o h u lo p t i m i r c u r o c c u ri n t r p t i n r i z a t i o n . search is anexamplc thc goldensection of a bracketing nrethod in spiritto thc hisection nlethod thatis very sirnilar lbrrootlocation This is firllowcdby a somcwhat moresophisticated inter. Finally. This identicalbecause the samevalue x* both minimizes/(x) and maximizes graphically equivalence is illustrated for a onedirnensional in Fi_c. wit We will then show how thcsctwo nrethods arecornbined and irnplemented M A T L A B ' s I r ni n b n d f u n c t i o n .However.3d. 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 . we arenot constrained peirksand valleys(Fig.the process of findrng a maximum versusfinding a minimumis essentially /(.2 ONEDIMENSIONALOPTIMIZATION This section will describe techniques to find the mrnrmnmor maxrmum ofa of a function "r o s i n g l e v a r i a b l e .4. ftrnction 7. optimizrtionin one dimension intobracketing can be dividc'd andoperr methods. brackcting approachparabolic polation.5ancl6 that root location was complicated by the fact that several rootscan occur tbr a singlefunction. the lett rolr Euc whe Mul wher q 'l FIGURE 7. In the next section. Recall liorn Clraps.ln suchcases. l ( . Similarly.a twodimensional optimizalion can againbe visualized assearchingout just as in realhiking. represcnts A gkhul olttittrtrm the very best solution.A local optinrunr.T h e l w o p o i r l s1 ot h .r i g h tr r r e o c o o p t i m o . we will almost always be intercstcd in findingtheglobal optimunl In addition. . towalk a singledirection.
3) 4. role in the construction of the pentagram or fivepointedstar. 6t 8 0 . "A straight Iine is saidto havebeencut in extreme andmeanratio Euclid'sdefinitionreads: when. (7."Beyondsuchsuperstitious wellknown numbers that havesuchinteresting thereareseveral andpowerful mathematical properties that they could truly be called"rnagical.. was veriflcclby deterrninimg that. which is typicallyreprescnted theonof remarkable numbers. The positiveroot of this equation (7." segment. a sirnple. terminingthe optimumof a singlevariablc lbl locating searchis sirnilarin spirit to the'bisection approach The goldensection hingedon definingan interval.l(.as the whole line is to the greater to the lesser.rfolwo segmcnls Euclidsdefinition io ihe segrnenl 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'(.51 FIGURE 7.r 9 8 8 7 4 9 8 .DIMENSIONAL OPTIMIZATION l7l 7. 7. The goldenratio has long beenconsidered it includingbiology.5 so lhot fhe of the golden roiio is bosed on dividing o iine i.) tweenthesebouncls as the midoointof this intcrval: The root was thcn estimated '. 300 BCE) because of its leuer Q (pronounced.The prcsence had difl'ercnt signs. In addition.1 GoldenSection Seorch qualities.5.) and. areall familiarwith "lucky 7" and "Fridaythe 13th.) that bracketed of a root bea singleroot.For our pulposes. function. so is the greater The actr"ral valueof the golclen ratiocan be derivedby expressing Euclicl's definitionas t1IL2 Ir lr (': (7.. certainnumbers For example.2.2 ONE. (2 t+l..5. (7. specil'ied by a lorver rootsin Chap. r . areascribed rnagicai quantities.2 termsyields and collecting o'QI:0 is the goldenlatit'r where@ : L r / ( z.Recallthat bisection guess(r."The mostcomnlonof thesearethe ratio to its diameter z and the baseof the natLlral logarithme.2. the golden ratio shouldsurelybe includedin the panThis quantity.1) pleasing aesthctically in Western cultures. of a circle'scircumference Although not as widely known.r.6 : 1 .r.As depictedin Fig. we in theWest In manycultures. by the Greek fee). was originally definedby ELrclid(ca.: l+\. 'tt I x.it arisesin a varietyof ilthcr contexts generalpurpose provides rncthocl fbl dcthe basisfbr thc goldensectiorl scarch.2) Multiplying by t 1/(.9 .)and an upperguess({.
).r. the sameas the l'unction To cornplete with thc algorithrn.Forthe golclensection points are chosenaccordingto the golden sc'arch. Ratherthan value(which is sufflcient usinga singleintermediate to detecta sign change..r.eachroundthe intervalis reduced by a 1'actor of d .008or 0. As the iterations arerepeated. we needa new strategy for findinga rninimumwithin the interval. hereis thc neirl hcnelit {l'clm thc uscol'thc goldenratio.r.\r bccotnes thc new . of suchan interval.r. this casc. (7. in conlrasl definedthe lower and upperbounds.r..l(r. . theold"r. rud. This meirns that we alrcadyhavethe value valueat the old. beFor example.). However.)is the rrinimum andthc donrain fiom ol' r to thc rightof . t h e n . u.rrwould be computed the extremum is reduced rapidly.. we were interested of function.r. the two intcrnediirte ratio: . I t ' .As with bisection. r .crechosenusing latio.6) with r/cornputed with Eq.liorn r. 6 a .r):xud where . a n d t h e d o m a i n o f . . andhence contains as for bisection.Becallsc the oliginal. .0066% . clurbe clintinatcd becausc it doesnot containthc rninirnum. This sign done by replacingwhicheverof the boundsr1 orr. with Eq.rr).I (about61.lirr thc next round.. in detenniningthe minimurn Now suppose that insteadof a root. This is not quite as good as the reduction (50Vo\.8) based on the new valueso1'.rr) d:((h . the intervalcontaining Thatnleans In fact.at thcsetwo inleriolnoints..r. l ( x. we would need two intermediatc occun'ed.and4 is calleduninrodal. a onedimensional we can startby dcfining an interval that a singleanswer. the intervalis shrunkto about0./ (.172 OPTIMIZATION was The final stepin a bisection iteratjon involveddetennining a new smaller bracket. illustrated in Fig. Asimila would lre usecl fbr thc alternate cascwherethe optirlLrnr fcllin the leli subinterval. with bisection achicved birt this is a harcler Droblem.6l8r{) length. points.7. the new. 7 .2 Gol< Prob 50lu . the intervalshouldcontaina singlenrinirnurrr.r : ) .87c).. .. approach For this case.81 (76) (1.For bisection.Yt:. comesthe new rr... we needonly detcrminethe new.7). ) i s t h c r . lirr thc ncxtround.r. We can adoptthe samenomenclature respcctively.it is about0.6.rr to . the new . andhencea function valuesto detcct whcthera minimum zero). . n i n i n r u n r .A kev advantage of this approach bounds. a s i n F i g .r.l. or 0.this was accornplished by choosingthe midpoint. then/ (. Fclrthis case. That is. to recalculatc all thc function valuesfor thc next iteration.1) ) Now.r. r t o t h e For leli cll'x. Two results can occur: Thc functionis evaluatcd l. This is done Eq..\/ +d . to bisection. the golden we do not havc and r. where.to r).8% of itsinitiai that after l0 rounds. the goal is to nrininrize evaluations by rcplacing old valueswith nerv values. replaced oneof the old asl(r.rll( .l)(:r. The key to makingthis approach etficicntis thc rvise choiceof the intermediate lirrrction As in bisection./had a functiorrvalue with the same was that the new valner. EXAMPLE 7.r. (7. bscclmes If I ( v:) . tbr thc case lbr sinceitis the new.would bc eliminatcd. (7. l ( . (7.Afier 20 rounds.
: 0 to jr.n n S.4121:2.5279 . Solution. .61803 rr :0 +2.the goldenratio is usedto create the two interiorpoints: (4 .: 4.2.S e c t i o S ProblemStotement. (al rh le :n x2 t1 t I I New (b) ne or )m rd.721 d : 0.4'. First..2 l0 within the interval from r.2 ONEDIMENSIONAL OPTIMIZATION r73 S n d )f rt e f.0) : 2.4121 xz:42. Use the goldensection search to find the minimum of 1 sinr "f ( x t : . rld+xl St . Llv.6 (o) The initiclstep of the goldensection seorcholgorithminvolves choosingiwo inleriorpornts occordingto the golden rotio (b) The secondstep involves deflnlngo new intervol thot encomoosse ln s eoollmum.. on )eis irh lar al.rl FIGURE 7. tns :ial ron n eorch EXAMPLE 2.4121 :1.2 G o l d e n .
thelower b o u n dr e n r a i r l s .9943) function v a l u ea t .the T result isccrnvergin og n t h e t r u ev a l u eo f .7641 .. the fbrmerr.f (r) : l . Using similarreasoning.. 1 2 1 6 .x. thatis. Because the interiorpointsale symmetrical. 6 1 8 0 3 ( 2 .9443 rz :2. t h a ti s .rr. lf the optimumfunctionvalueis at. 7 6 1 1 pl r e ..52192 .1 .5279.r.. we do nothave to recalculate on the previousiterationas l (./r(.5219with a valueof .0 ... .r. : t / 0 ..r.100 2 4721 (l c. we alsoknow that the minimum is in the intervaldefinedby r. 1 1 5 5 .If optimumfirnctionvalueis at r. 1 1 2 10 1.5279 T h e f u n c t i o n e v a l u a t i o n a t . eithercasecan be usedto definetheerror.4J21 . Alier the eighth i t e r a t i o nt.1. .[(xt1 .5219) : 1..lOO 2 4721 B8B5 t.7)to computethe new valueof r/andr.r.it will be in the upperinterval(.rr.) 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 .. gol wo nuI l.). No Co Recallthat for bisection (Sec.r1). . and. an upperboundfbr goldensection search canbe derivedaslbllows:Oncean iteration is complete. it was determined All that remainsis to use Eqs. b e c o n r ets h e u p p e rb o u n d . . t 5432 17647 t 7136 t 7647 1 7755 t 7732 xu J'(x.. valuebecomes the new . 5 2 1 9 ) : .r.4421 with a lirnction v a l u cc t l ' .r.1 (.5.. t h e m i n i m u mi s l ( 1 .ln addition.BB85 ) 5279 1 66s6 I 5279 I 4427 I 4752 40000 :tIt36 o 6.).5432 BBB5 ) 5432 6656 I 7136 5279 t 7647 t 7647 5279 2 A/21 ) 5279 0 9443 0 5836 0 360/ a 2229 0 I3l8 0 0B5r Fit ap Note that the current minimum is highlightedtbr cvery iteration. it will be in the lower interval (. . r .Sincethisvalueislcssthanthe .ln adclition. r.2 s i n ( l... I n a d d i t i o n . The process can be repcatcd.412t2 .0 is : l.5219 or es : 0.] t .sc) 3050 3 9 0I I 5279 o 944'3 5279 3050 5279 4427 3 9 0I 4427 24721 I 5279 I. an exactupperboundfor the crror can be calculatedat eachiteration.1641. r : 1 .. : 2 . a n dx .) a I 2 3 4 5 6 7 B 0 0 o 9443 4 s443 3050 30.1 . Thus.4). 5 2 1 9 ) : ..rr. 1 7 2 1..(7.r.our best estimate focatedat r :  .r.h e m i n i m u r n occurs atx:1. /(x1). andit is in theintcrva scribed by rr.for the next iteration. r . 1 6 4 7 l0 2.174 OPTIMIZATION The function can be evaluated at the interior ooints: l(x:): I (rr) : 1. with the results tatrulated hcre: or the Th xt J'$.. 4 1 2 1:) . and... 1 1 5 7 a t .1 . 6 3 0 0 t0 Because of the minimum at this point is thatit n .2 s i n ( 2 .53l0.. theoptimumwill eitherfall inone of two intervals.8) and(7. .: ): d : 0 .hus.
r t I ( Q .r.. thiscase would represent themaximumerror.r.the maximum distance from the estimatewould be Ar. Therearc cases search Many evaluations.r.1 e.r1).. the maximumilistance or 0.3820(.(Q l)(.3)(. ......I 0 l ) .rr) . 3.lor solvirrga single optirnization. r r ) .r1) : (20 . : ( 2 . .d ) ( .1. Of course. l.). fiorn the would estimate be A..The l'uncticln the locationof the rninimum. i L c r I = g o 1 ctn i n ( z .rr) .x .. Therefbre..i.'nr I t. . to yicld le t .+ ( Q .r2 : . 79 1? l6 This cstimate provides a basisfbr tcrminating the iterations.Therefbre.D Q . thc speedsavings would bc negligible. it may be calledmany times.. r .7. i n corresponc height You nraybe wondering why we havestressed the reduccd functiorr of thc evaluations goldcnsection sLilrch.9356c (l0'r u. wherethe goldensection algorithmmay be a part of a much largercalculation.8609.).. This result can or 0.. e h a v ee n t e r e d thc negative of Eq.. fbr the goldensection searchfor minimizationis presentcd in An Mfile f'unction returns Fig. In suchcases. . .O ) l ' : ! .: J lx t 0 0 % I r..r...)+ 2(0 .. n t = 8 0. i'.{rr I .1). (zt)+rnlc*(vO+m*!. . z r ) .However.) ) nr*q.*L) )7 29 7B il . .r1) : ( 2 .r.l ) ( x . f rn i ri ..t't (Q l)(x. O . : : {._.Z. 'f . v O ..2361(r. . > ) ' q .ri) : (r..r.'c)*(I erp( :=Q(L) r: >  x n i n .. : = 1 \ .8317 lnii n 192. C o n s e q u e n t l[ ym .the valueof the function.:TI .2 ONEDIMENSIONAL OPTIMIZATION Lookingat the upperinterval(xr.there are two importantcontextswhere mininrizingthe Theseare numberof functioncvaluations can be important. :r. .r r ) .:ra . if the true valuewereat the fiu'left. If the true vaiuewereat the far right.artcl the numberof iterations..86r) 9 th ne 6.t. t lo sa m a x i m u m ol' 192.. r u. kecpingfunctiort evaluations to a minimumcoulclpay greatdividends for suchcases.'.x i ) : (rr .7. (7.r'..h ne xF SC w N o t i c eh o w b e c a u s e this is a muximization .5 5 .n.sl .the approximate crror. i 8 1 ...l)(..r1. The Mfile can bc uscdto solvethe problenr frorn Example7.1. ) >xmin = c/m*1. thenbe normalizecl to thc optimalvaluefcrrthat iteraticln .
x1 = 7). optimization can be usedto estimatr the parameters of a modelconsisting of a systemo1' diff'erential equations. x2 = xu .es.end p h i .le 13 jpr5. ( 1 + s q r r ( 5 ) ) / 2 .es.ea.e. Forsucl cases.approxirnaLe relative error = number of iterations iter (?) if rr.. t = name of functior1 Z oo r'l . ? ? ? ? x = locaLj.rrgin. Sr .1)* (xu xI = xl r cl. xl) / xopt) * 100.3.7 A" A/i.br:eak. ..rminp the mrnimum of o lunciion with the oolden. ea = (2 .\tararqirr{:}) < f (x2.d.vararcJin{:}) xopt = x1.on of mrnrmum fx = minimum function value ea .maxic. 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 s o u l db e a d v a n t a s e o u s . maxit=50. es=0.0001. end end xxol'.end if nargin<5 isempty(maxit).end if nargin<4 rsempty(es). iter0.'/a'or:in :] .pI.xu. while(1) xI).C.176 OPTIMIZATION r nr ior Ix.phi) * abs( (xu if ea <= es I iter >= maxit. xopt vl. irer l.x'.fx.error('at least 3 input argumenLs required'). = additionaL parameters used by f % 9: ^rrrrirrts. For exarnple.3 $(AAAPLE Po Pr 7 Timeconsunring evaluation..) % uses grolden sectjon search to find t. fx. if t(x7. if xopt0.p2.xl.[x.p2. SO tic end iter=iter+1. co ca x2. we use simple functionsil most of our examples. Pa pr.goJdrin lf .i x 'pL. d = (phi. xl lower and Jppe' oLesses (dlefault . .maxit. FI ul 7.ired relative error (default = 561 % maxit = naximum allowable iterations p1 . fhe "function" involvestimeconsuming model integration.0001?) % es = de:.end wl op FIGURE 7..varargin) % goldrrLin: mjnimizaLiorr golclen section search % : fx.he minimum of f % inpr'rg.seclion seorcfr 7.. You shouldunderstand that a functioncan be very compler and tinreconsunling to evaluate. For pedagogical reasons.iterj=goldmin(f.xu.
ancl . r . Use parabolic interpolation to approxirnatc thc rnipirnurn ol' /(r) : rl lo 2sin'r l . EXAMP 2L .rr is the valueo1'.2.2 Porobolic Interpolotion Parabolic interpolation tltkesadvantagc ol'the lirct that a sccor.):3.rthat corresporrds tq the optirrrun vta l u cu l ' t h cp a r .s829 /(r'. 7. sctthc rcsultcqualto zcro. in Lex ate rch lat r v i t hi n i t i a lg u e s s eo sf .ancl solvelbr atr estitnale o1'lhcoptimalr. A . r . :. ccltnl.yointh l yr a c k ea t noptimumw . .E 3 P o r o b o l i cI n t e r p o l o t i o n ProblemStotement. a h o l f ic i t t o t h eg u e s s e s ./(. .rdordcr polynornial olien providcs a good approxirnation to the shapc of l(. a n d _ r .rl(7.thereis only 9nc parabola c o n n e c t i ntg .):0 l(r.1 . It can bc shownlhloughsontcalgcbraic rlanipulat i o n st h a tt h c r c s u l ti s rl:.. .raight linc corrnccting two points.l0l wherer'.p i c t i o on f porobo ic interpolotion 7.8 Grophico dl.0 . Thc lunction values a t t h ct h r e e g u e s s ec sa n b e e v a l u a t e d : r f) r'l .ita parabola to the points.1136 . iirc thc initial gucsses.): 1.r Parabolic approximation of maximum Parabolic function FIGURE 7. .i 1 ' w ch a v ct h r c cp o i n t s h r e ep o i n t sT that. : Solution.r.2 ONEDIMENSIONAL OPTIMIZATION 177 Truemaximum ) True function .r) ncaran optimum(Fig. Thcn wc candil'lcrcntiatc it. and. r.fustits lhcrc is tlnll'ttnc sl..g).2. h u s .l(r.
lbr the next iteration: rr :l r: : I. 5 0 5 s 1 ) l .) x2 f (x2) r.I .7757 a t .):3'1136 rvhich n t oE q . 4 2 J 6 .l X. 5 8 2 9 : 1.10)to give and substituted r'.( l .5829 . a strategysirrrilarto the goldensection searchcan be employedto determine which point should be discarded.li. I 7.4903): l .The process with can be repeated.il361 . r: 1 ./k. which frasa functionvalueof /(1.( t . The fminbn. 7 6 9t 3 .t 7691 1 7757 4qa:J .6.0 ) [ . It combinesthe slow.11361 .butpossibly parabolicinterpolation.2.rl J@.r 5 8 2 9 4 0000 3 r 36 I 7 6 9 1 4 0000 J I I 36 t 7714 5055 . Thisfunction combined severalrootfinding methodsinto a single algorithm that balanced reliability with efliciency. ] . Therefore.4 we described the builtin MATLAB functionf zero. within five iterations.5055 xl: 4 whe func Exar .1 .r.rilarapproachfor onedimensional minimiza. "f(rJ 1 /691 1 7711 ) 7757 t 7757 )7757 I 2 3 4 5 0 000cr 0 0000 I0000 r 5829 I0000 .5829 [. 7. s 8 2 9 2 ( 1.5829 . .r78 OPTIMIZATION into Eq.t 7 75 7 0000 5055 4903 4256 4266 .0 .55 : 1.76e1 . l l 3 6 l .t /714 ) 7757 49A3 I 7714 1 .0)2L1. dependable searchwith the faster.r'): 1..1691. 1 l 3 6 l I ( r.) is discarded.the lower guess (x. parabolicinterpolation unreliable. ( 7 . 7 6 9 t.r:) and the new x value is to the right of the intermediate than for the interrnediate point.Becausethe function value for the new point is lower point (. 5 0 .5055 t 4903 I 4256 I 4266 14 2 7 5 Thus. If not.3 MU Asid func a rol valle a har . the resultis converging rapidlyon the true valueof 1.0] ). It first attempts andkeeps ap plying it as long as acceptable results areobtained.suIts tabulated here: .50.01 l (l .l'Q): 1.t .5055) Next.3 MATTAB Function: rminbnd Thu itera Recallthat in Sec. l 0 ) t o g i v e c a nb e s u b s t i t u t ei d ra = 1. 7 6 9 1 2 ( 1 .. 7.3.s 0 5 r5 )t which lrasa functionvalueofl( I .t 5829 1 4 2 5 6 .it usesthe goldensection search to set mattersin hand.3 * 3.f function provides a sin.t 5B2a r o o o o .5 . goldensection tion. thc re.+[ .4)2 . (7.t f k.1 .4903 5 I ) 2[ .1114.r ..
B6r 792 . we can display calculation details: options fnr i = ofitimset ( 'display' .7 .05573 4. >> z=@(t) (20+n/c*(v0+m"g/c)x \1exp( c/m"t))m*g/c*t) .and alier eight iterations. B1 .8609 As with f zero. Funccount 1 2 l X f (x) Procedure inirial go 1den qolden parabo 1i cpirrabolic perrabolic parabolic P. . v 0 .861 the termrnation criteria usinq OPTIC)NS.xl.88854 3 . > > g = 9 . and xl and x2 arethe boundsof the interval being searched.2. fvafl = fminbnd(functian.94427 r.83166 3.81544 3.mrnated: x satisfies 1.83. after three iterations.ToLX of 004 3 .3a that our visual imageof a onedimensional search was like a roller coaster.0001.r1 Thus. B6r 192. 7. MAILAB's graphiccapabilities provide a handy meansto visualizesuchfunctions. For example. the minimum is determined to a tolerance of 0.l. 'iter') . z A.851 192 .For twodimensionalcases. optional parameterscan be specified using optimser.83159 ter.L 0 a . X= 3.87I 792.3 MUTTIDIMENSIONAT OPTIMIZATION Aside from onedimensionalfunctions.Recallfrom Fig.851 r92 . function is the nameof the function being evaluated.rrabolic parabo 1i c 4 5 5 1 B 9 OptimizaLion the current 3.3b). optimization also deals with multidimensional functions.As in the lbllowing example.3 MULTIDIMENSIONAL OPTIMIZATION A simple expression of its syntaxis fxmrn.the image becomesthat of mountainsand valleys(Fie. Here is a simple MATLAB sessionthat uses fminbnd to solve the problem from Example7. B(r1 792.000000e rB9..83332 3 .85836 3.1.8317 f192.5 5 r m= BO.159 187.83r52 3.x2) wherex and f va1 arethe locationand valueof the minimum. c = 1 5 .19 I/I.the nrethodswitclresf'romgolden to parabolic.
r x( Z ) ) .Z).9.:<.2. x l a b c l { ' y _ 1 ' ) . Z ' ) . xlabel ('r1' ) .r. zmin=f l.2 t. '. : l. 2 + x .. 4 0 ) . " r 2 ) : 0 t o I l o c a t e da t a b o u t r l : FIGURE 7. Use MATLAB's graphicalcapabilities to display the following function and visually estimateits minimum in the range2 < xr < 0 and 0 < . 4 A ) .\'l. = c o n t o l l r ( X . (a) Contour ploL' ) ..Jrid. r ' .9 (o) Confourond (b) rneshplc.e iI ( m .xz 'lSolution. trtle(' subplol \I.rbel (':<2' ) . = l i n s p a c e ( 0 . the As displayed in lrig. Y .4 nLtn on {fAt uat funr 7.f(xt'x) :2 i xt .r . i.5. : r I a b e l ( ' : ' . 3 . c ! . . t]:' Yl = meshqtrid z = . (x'Y) . 7. . ( l :.D i m e n s i o n q Problem Stqtemeni.i Sta mu dire han whr the (' f (x_I. 7. z m a x .'{. ^ 2 + 2* x . f t . r b e l ( c s ) . The fbllowing scriptgenerates contourand meshplotsof the function: x ._2)' ) .4 OPTIMIZATION Flu n c t i o n V i s u o l i z i n gq T w o .ror(min (Z)). _ ?). * Y + Y .2. both plots indicatc that function has a minimurn value of about l and. z l a b e l title('(b) M es l .r t r ' l o t ' ) .7). cs=surf c lX.' ' 2 . C) .'+ { 2 * x .r2 < 3: 2xl +2xrrz + xi . subploL i'7.ls functron of o Modimensionol Bo wh forc fere * lo)  the (a) {b) .l i n s p a c e ( 2 .r80 EXAMPLE 7.
5kx2 * Fx (7. methods.3.xl. or direct. of o horizoniol siretches {b}Applicotion {o) isdescribed thespring therelotionship force onddisplocement by Hookes low where beiween .As describednext.7. > f = @( x ) 2+x\1 ) x(2) +2*xlI) ^ 2 + 2 * x ( l * x ( 2 ) + x ( t : .For purposes of the present discussion.11) 7.4: thefunctionwe just graphed : .x2) andvalueof theminimum.1 IVIATLAB Functiohs fminsearch the miniStandard MMLAB has a function fminsearch that can be usedto determine function.which is a lnum of a rnultidimensional directsearch method that uses <lnly function values (does not require derivatives)and A simpleexpression handles nonsnrooth obiective functions.Those that require derivativesare called (or ascent). that usesf rninsearch to determine rninimumfor Hereis a simpleMATLAB session in Example7. ) f v e ri 1.an unloadedspring can be attached is relatedto the The displacement When a horizontalforce is applied. 7. on whetherthey require derivative evaluation. Bockground. and xl and x2 are the boundsof the intervalbeing searched. )^ 2 . F : kx. It is basedon the NelderMeadmethod.4 CASE STUDY t8l Techniques for multidimensional unconstrained optimizationcan be classifiedin a we will divide them depending numberof ways.fttnction is thenameof where xmrn and frzal arethe location the tunctionbeing evaluated.the spring stretches. of its syntaxis fxnrr.IO FIGURE force An unlooded spring oitoched to o wollmount. As in Fig.7500 1. the builtin MATLAB functionfniinsearch is a directmethod. force by Hookes law.5001) ENERGY EAUILIBRIUM AND MINIMUM POTENTIAL to a wall mount. The potential energyof the deformed stateconsistsof the difference betweenthe strainenergyofthe springand the work doneby the force: PE(i :0.70a. methods. gradient. fvall = fminsearch( function. The approaches or descent that do not requirederivativeevaluationare callednongradient. 7. OO0O = 0.
that minimize the potentialenergy: 12 5€ The _l2 ^ + (L. and x. PROBTEM three iterations the root of Eq. .. the solution for displacement can be viewed as a onedimensional tion problem. Lo: 70 cnt Lo: l0 cm.OPTIMIZATION it. Sincethe potentialenergy will beat a equilibrium. : 2 N/cm. the following f =3+6. (7.11. Thu loca 7...5 cm. F o r e x a m p l ei. continued Equation(7. r = 5 N / ( 2 2.S.F... A more interestingtwodimensionalcaseis shown in Fig.5x6+2r4+1 fiurction. t Example 7.In this two are degreesof freedom in that the system can move both horizontallyand In the same way that we approachedthe onedimensionalsystem. 7. use MATLAB to solve for the displacementsand the potentialenergy.0. r : F / k .t 'i..11)definesa parabola.1 along the formula =x2*8x12 the maximum this function analytir thatEq.I+:l.x ) 2 * L") + o.skb ( If the parameters are ko: 9 N/cm.i l .the equilibrium mationsare the valuesof x.we can solvefor p l a c e m e na t s .I I FIGURE (o)unlooded A 1"wospring system: ond(bllocded.r+5x2+3 6e minimum by findi Use bisection r 1.10)yi guesses ofr. . = 2 Fz = 4 N.1.f k : 2 N / c m a n d F : 5 N . methods to fu dl values of r. k. Becausethis equation is so easy to differentiate.
t n +2x" +12l method iterations of the NewtonRiiphson three Perform valfherootof Eq. F 1 .r) : 0.9 .2'759 PROBtEMS (c) Dillerentiatc' a rootlocation the lirnction and then Lrse methodIo solve lor the maxirlurn.ll Consider Given r1y .t. p.L a . except use parabolicintcrpolation. and perfbrrnthree iterations. (. .f search.f (xl : ro + 2tr + 8x2 + 5r =). =i 0 .tr : 1. iterations r r =l ' the fbllowing function: 7. F ' I .r.l).cm.r ( 2 ) ) ^ 2 ' 2 + ( s q r t ( x ( l .usingdillerentiation). forall values cave methods to show the lirnction and graphical Use analytical has a minirnum fbr some value ol' x in the range *2 < x < l. of Employ initial guesses using the goldensection 12 J ( r= )rr+8r . The solution can be obtained with the fminsearch function: . : 5).(r RepeatProb." 0 . : l(n. : $.: function. 2 Usebisection with initial [uesses of r.ti : 0 andr.: point is Thus.r a '(r) r = 3 { 6r* 5r + 3xr +. 8.1 3 0 . xp1l. .75. f ] .5.. ir : 2. : I. 0 . 1a l o n gw i t h a n i n i t i a lg u e s s of r:3 s E x a m p7 le thefonnula Given the function. k a .: .l(.r)and the corresponding valueul'r.1 . 5 . 3 . andxr : 2. P E a =0 .] k a = 9 . L b .2 . . k b . I x .9\2 i f I . b + x ( . . r. (.42. l . valueol' 7. of. .. : 4. 5 * k a * ( s q r t ( x ( 1 ) ^ 2 + ( L a . Enrplov rfor lhis initialguesses of .2'.PFla+ P E b.lra (. 1) PEb=0.r.5*kb* \'2)) ) Lb)'l. (7.. i . 7.4 7. Usethe parameter . : fl.r) in Prob. 2 . (a) Goldensection search ol' nrinimunr by findingthe root ol'the derivative the (b) Parabolic (.v.: 2.. 5 l 4 .PROBLEMS continued Solution.f r n i n s e a r . : b .e.5 Solve for the value of xthat rnuximizes. . W .W . to hold the potentialenergyfunction: An Mfile can be developed p = P H ( x .0 l 9 .7.7 Enploy the l'ol<'tring thelbllowinglunction: Consider I ( r ) : 4 x .1. initialguesses nlethodsto t'ind the nrarirnum ol7. interpolation r'z : 2. l . themaxinrurn and the colresponding Delermine functirx analvtical ly ( i. l 2 ) function ^.F l * x ( 1 ) + F 2* x ( 2 ) .v. L a = 1 0 . cm aboveits original located4.10)yields the sanreresultsbasedon lbrm threeiterations. rnd perVedfy thatEq.1 ) La ) '2.l . t . Plot methods to prove that the function is conUseanalytical of r. u x 2* 1 .! r . c h ( B P E .9523 cm to the right and 1. (E7.rr: 2.6422 N.. E"t. The connecting position. at equilibrium.the potentialenergyis 9.5.
r 2 / ( .000 Giverr thatL: 600 crn. criIteratcuntil thc rclativcclror talls bclow a sk)pping to lincaldrag(r' : l5 kg/s).:2and.Findthevalueof whcre .r.(t . S/.4 I ( d ..r).L.8 I rn/s:.^ . (a) glaphically.r. thc subroutinc lunction: 7 . 7.j crrs'11" cosr:."ntrrn. r I r : l rl u n c t i o n t o d c t c r m i n t parabolicintcrpoltl.r'): tlr * rl + l2r' + 4r'l 2. with thc goldenscction a rnaxirnum error falls below e.8r+(l+0. r . Tcst 1.'fcst the inflcctionpoint of this curvc firr positivc t()attaina dcsircd numbcrof itcralions nccdcd eh e yourfunction b y s o l v i n gE x r n t p l c7 .22 7. due to toxicitycitects.24 A contilever beom . attai ns.5 kN/cm.2(] l0$ As dcpictedin Fig. .. criItcratcuntil thc rclativccrror lllls bckrrva stoppinS .ll Devclopan Mfilc that is expressly (b) usingthe goldenscction sealch scarch.:li l'unctitln.specificgrow. 2 1 C i v e nt l r cl i r l l o r v i n g shouldrcturnirncrr()rnrcssagc.rr' ttutnhcltll'itcrationr. ancl(c) suhstittrte with lrLj rr. iterations 0. r r + 2 . bchind an airlbil ovcr tiuic. 2 5 . ..oinitialgucsscs.22 Thc spccilic gt'owthratc of a vcast that producesan _r: 6 cosr I. .8: (..: $Qp. .t .: 2. high conccntri.icct thc tblloia. (b) Parabolicinterpolation(.1 brackctit tlaxitrturn.5+ .Li 7.attd.1 . . r . .9 Employ the fbllowing mcthodsto fincl the minitnurn of thc function fiorn Prob. dissolvedoxygen . l '.y)5.: 50".12 Dcvclop arr Mf ilc to krcittc l ntinimum with thc goldcnscction search. r r .t mo.ttitxirtrunr ()f thc intcrlal. cratcthc third initiirlvalucat thc rnidpoint . rr : 5) deto locate ahd ur. : the initial vclocity (rn/s). of scarchto clc(crttrirrc the location Usc thc goldcnscction . onlibioiic versusthe fooo t FIGURE P7.11t)cvelop an Mfilc (o irnplcntcnt shouldhavc thc Thc subroutinc tion (o krcatca nrininturn. f(.1. Ralhcrthanusingthc Inaxitrtuuitcrations und L. 2t with o1'aball can bc cot.r.. . Rcturnboth thc optinral Dctcnnine thc rninirnuttr (hc theresult ./(. ofl. .rnction: 7.ttiot'l is a lirnction antibiotic prcssurc.rlinimrnl ot' 7. . : I.[f the ..r.: ol'thc lixld conccntr.14 PrcssurcmcasurL'nlcnts (. Rcturnboth thc optinral "t and.r') but shouldnot inrplcntcnt thc algorithrn. 0 ancl ..r.r r .the initial height(rn).1.1 .r.pprpfogramrvith thc samcptoblctrtas Exanrplc (b) back into thc l'unutionio clcternrinc tlic rninimum ol' arc takcn at ccrtain poiltts 7.r: 0 to 6 s.r.1 )o .Il ntlt.25 The Streeterphelp. u' : 25 rn/s. v ) . kN/cnr:.lc/c). z thcfbllowing tr..r.itions h g r o ut h i s u r t r : r x i n r u m . 7 .on.: 2.ig wherer : end displacernen values o f r ..and0. t h a tn t i n u n 7.e ./ (. : 2 .15 Thc trajcctory 4+0. . rr__' I I FIGURE P7. : /'(.3. .r: : 0..q. P7. i'). Thc data bcst l'its thc curvc I 7.:'1. . chargeof sewage (Fig.000 (. 5 .t l'rom.5. curvedefinedby is a bcllshapcd clistribution 7..nputcd 7. . tolcrance.23 A compounclA will I tank reactor.r. 7.g : thc gravitational r u ( r l r i e r''*rir r : ( t i u l H { ) ) .ingf'eaturcs: liom tlre surluccol'thc carth at a vclocity of 50 rn/s. Llsclilur iterations (.. p7. d. a ntaxinrunt tcrionor cxcecds thc rnaxirnum heighttheobjeo to detcrrninc scctionscarch . : l. andhavcthc progllut gcuBascit on tu.t'n constant golclensection search to determine the nraximum height : I nt.24 A finiteelement mode loading and monrenrsrF.r'11 Check whethclthc gtrcsscs 2 .dctcnninc(hc 0 lne...Itl The nornral with thc sarncproblctnas Exanrplc Test y()ur prograrn 7.r) : 2.Usethe : 9.i{r. {lection(a) graphically'.: lc/o of . 7 .___ f(x. ) lirllowinglcaturcs: the r t'lr ltlnction to detcrrnine 7.r * J 0 . ' 3 r 2 /1.r).r84 OPTIMIZATION . (7. : l% with initial sct if up so that it dircctly finds the traxitnum ratherthan until tlie approximatc .*!L p k a * k .1{)Considcr 3 .r and.5 sin.. detcrtnincthe point of maximum dcsigned 7.icct is subjcct nunrbclol'itcration.22. gue sscs of r. 1 9 l J s c t h c f r l i n r . :4r * 2. 2 u s i n gL ' . u. Unreacted processengineer has fbund is a function of the convers will result in the lowest cos constant.25 o: o.= _5(ys+2L)r'Lrrl to locate Perform IO itcrationsof parabolicinterpolation I)()EIL of yttur rcsults the minimum.C = 50. Iterate untilthe givcn t'u usinginitialguesses approxirnate ertor falls below €. r. 7.Commenton tlreconvclgcnce cm{..= j .r).l (. ( X X ) 1 .(b) nurnetically . : thc initial angle(radiansl.growth gocs to zc() at very to zelo8l It alsogoes conccntrations clucto lcxrdlirnitation. shouldhavc Thc subroutine finding thc rninimunrof with a ntassof 100 kg is prolected upward 7.k . tcriorior cxcccdsl rnaxintunt ..1: 30. [n othcr words.r': the height(m).rr : y:: 1.rr: 0. (a) Goldensection search 2.: 0 .9) as thc stoppingcritcria. :5).r.17 A ob. .: g andr. 2 . v. rncthodto l'indthc nrininrunr o1'thcgoldcnsearch Sctr. 2t .h . The product B separatlon unit. a n d 1 .16 The dcflection of a unifbrm bearnsubjectto a linearly loadcan be computed as distributed increasing P 7.r . r .usethegoldenob.sls ./(.20 Usc tllc f nriri. t .r.r25xy*2 7.
The StreeterPhclps mtxlel can bc usedto cornputethe oxygen concentration in a river helowa pnin(clis(Fig. 9 t O . ( X ) 7 .:0. 0 5 .r. r .r. ofsewage t.24)is civen by optinrizing Deternrine hoding andmonrents values: thclbllowing rl = .. 14..t .The \orce exerted on thc charge by the ring is given by )' low ro at le of  RGURE P7. UnreactedA is recycled to thc reactor. IIGURE P7.r7: l0 iC' w h e r e a .( i . I r"" r r""l traveltirnet. ln levela.r1. F. A ureTheproductB and unreacted loparation unit. P1 . r ) : 7 . arounda t'ing7.rl)/ I (likc I'ish)would be thc most stressed. 'I :   tx O' r '.. : r a t e o l ' s e t t l i n g o l ' B O r).\1..t'turtr.f . f r .t 1 o\:\mc€ \ lr()m tnt center ()1Lr\er\ng tFrg.r : lraveltirne(d).tl'rltt I rr.r. l 3 . ..26 The twodirnensional trationin u channcl can be described by r ' ( r . FIGURE P7. Deterrnine mlxir. t 1x5 l 0 Q:2x x whet'ethe force is a the distance and d : 0.A pocess engineer hasfoundthat the initial costol'thc syslcm ilafunction ol'theconversion r.' Sr p(le^"t ej. r \ thrr.randl thatrninimizc11.\' .r "cri(icirl" it the wherebiotuthat hecause rcpresents lrrcation L\{l.\' J{. srrme oxygen"sag" thal reaches a criticalr.. t. : whcre o : dissolvedoxygen conccnlration (mg/L).: 0 . by 7J3 A compound A will be converted into B in a stirred purified in a hnkreactor.he :nThespecific growthrcle of o yeosf thot produces cn onlibiol c vefsus the food concentrotion. l ( ) u r g / L k.27 A total chiirgeQ is unitbrnrlydistributed sh'.r. P7.\r':q11{ .:.J t t ! li. oxygensaturatiorl conccntrrti()n (BOD) concentralion L.0 .P7.).21 t.: sediurent oxygendemand (P7.i ttt.. ratL'(d arrd5..\'+ i. .2 ld_1) ly (mg/L) or' 0 0510 c (mg/L) lc. rnixing point thc kz rate ol'decomposition ol' (D d I ) . This point is called '6('l Cost=cltl I \. rof the (mg/L).\'. (mg/L/d).\./.:50rng/L 5r. C is a proportionality on$ant. o. . r').y : end mol.. .. ( .25 A dissolved cxygen "sog" below o point dischorgeof sewoge Inroo rver.nininrunr l.(.r I :ally )sult num :s an o=0 ( c t ' . i + t ' ) / ) ' '.0 . r* 0 . t ' 0 . .nent.05d I L. 6 t l I r .25.rned crrnductrq rr\th rrli\rl. P7. r e a e r a t i o n B O D ( d r ) .:. Find the conversion that rillresult in thelowestc()stsystenr...ft. bclow thc point discharge. 0 1 6 r ' r..r:0. : 8 . 2 1 .4 8 0.PROBLEMS r85 rt )S 0. : biochemical oxygendenrand irt : (rng/L)..d . Find the of.9 m. depend on oxygcn A finiteelement rnodelof a cantileverbearnsubjectto ?J4 thc critical tlavcl tirrre' and concentriltiun. .25).24 beom Aconli ever 4ten 1t2 1 47131t rrCr/(Nmr. r:ivcn (Fig.r*k.22 ch ial rrd .).:c r1 i.25)produces As indicatcdin Fig. t d  r = enddisplacenrent and . r : ..\.: I rng/L/d ol' ptrllutant distribution concen7.25\ Determine theexacrt Iocation given ol'thepeakconcentration t h el u n c t i o n a n dt h e k r r o w l e c l g c ' l h ae t p e u kl i e sr v i t h i n the lh {1(.r.
7.29.29 Plot velocity for cn oirfoil of drcg versus FIGURE P7.31 In a similar fashionr Sec.6. P7.whele slip is detincdas tl tlR 0 FIGUREP7. Thc problenr of to dctcrminethc slip at which tlrc rlaximum torqueoccurs.3O Roller beorings.sr to fatiguetailure 7.27 motoris a flncto an indr"rction 7. P7.2t1The torquctransrlitted tion of the slip betwccnthe rotationof the statorfield and thc rotrrrspced .thc drag due to lifi dccreascs.31 Twofrictionless mosses cor lineor elostic springs.000 with o : 0.: rotor spced.(xx) 3s*zl) ( I .6 and W : 16.tlrc two l'actors contributing Whcreasfrietion lected difl'ercntlyas velocity increases. Minimiz to determine the equilibrium ltc forcing function F: l0( Jd t6 . P7.Usc u numcricalmcthod lirrgc contrct loacls/i tFig. thc location ol' the maxirnum stress can akrngthc r axis (o maxirnizing 7.3L plots in MATLAB.clocity.. to 20.32 .o : ratio of air densitybctween thc flight As scen altitudcandscalevcl. FIGURE P7.29 Thc total drag on an airtiril can bc cstirnatcdby shownto bc cquivulcnt thefunction: n D : 1 1 .and V: r. 7.' w: 12. lV: wcight. P7.10).. drag increascs 0.. KirchhotT's in dirncnsionlcss fbrm) artd slip that the torquc (cxpressed arc rclatcdby The combination of the two f'actorsleads to a minimum clrag ( a ) lf o :0.dcterrnine the minimun drag anclthc velocity at which it occurs.000.4. 1 1 1 o Y ' +( # ) ' :T Fr'iction Liit whcrc D : drag.r)(4.'. develop the poten tem depicted in Fig.l5 N/m.10 Rollcr bcaringsarc sub.r l+rl 04 \ Find thc .s) l 5 s ( l .000 (r FIGURE P7. 732 As an agricultural engi open channel to carr) ine the optimal dime perimeterfor a crosssection dimensionsuniversal'l 733 Use the funcrion rmins ofthe shortest ladder that rea ftnce to the building's wall (l where ?34 The length of rhe long 6e corner depicted in Fig. whcre rr : rcvolutionspcr seconcl of rotatingstatorspccd laws can bc usedto show and ri. anllysisto determim devclopa sensitivity to a range d how this optirrum varics in response .28 Torclue ironsmifted to on inductoros o function of slip FIGURE P7.r').1 .4+'F /t __/ rr'Ir \ _]+. ( b ) In adclition.000 10.28showstlris function. with velocity.r that rraxirnizcs /(.iect FigureP7. to drag alc i'rfin Fig.r.r86 OPTIMIZATION PR r_ ro D 20.
.. P7.f 'i . Minimizethc potcntialcnergyl'unction letermine theequilibriurn displacenrents .3  hictionless mosses fo o woll by o poirof connected e n"s.33). given F : 100N und the paral)rcters forcing tunction k..t ^ A l o d d e rl e o n i n g ogoinso l f e n c eo n d i u s t touching o woll computing the value of e that minimizes the fbllowing function: by ing be L(o): Ll.PROBLEMS r87 P7. Testit lirr the case L _ ..lr2 describedin this chapter (including MATLAB's builtin capabilities) to develop a plot ol'L versus a range ola's from 45 to 135". P7.32 FIGURE P7.1 comer depicted can be determined by _.+ .1. open channel to carry irrigationwater (Fig. Thelengthof the longestladder that can negotiate in Fig. P7. i n.il. Developcontour and surflce depicted tnMATLAB.34 A iodder negolioting formed o corner bytrrohollwoys .:20 = l5 N/rn.4.32) netheoptinrill to rninimizethe wettecl dinrensions fbrr crosssectit'rnal areaof 50 rn'.r. '. r < . usea numericalrnethod For the casewhere ur1 : r. derelop the poterrtial fol the sysenergyf'unction in Fig. you rnustdesigna trapeAsanagricultural engineer."'. tr. Are the relativc l universal thelunction tm i ri:. and r.t .e:a Use r ch to deternrine the length shortest ladder that reaches liorrr the ground over the tolhebuilding's wall (Fig.^J q FIGURE P7. P7.33 um ine of ln a similar lashionto the case study describedin ?. P7. sfnfi stn(ftaH) "'' : 2 m..
.*.anO momenturn.. ''.Te value xrhar satisfied a single equation. Now. rhe * unknowns... All otheralgebraic equations arenonlinear. we O"rrrrn::. thatsimultaneouslysatisfy a set of equations: f. energy.. we deal with the caseof determining the values* ri.i:o : .lx.* ar. : 12.' . In mathematical terms. deal with lincar algebraic equarions that are of the generalform axt * anxz+. scrence on conservation laws. Sometamllar quantities that conformto such laws are mass. fi . aztxt * azzxz . * : fi..:l T are based ano. of 1. . I ovERVtEW Whof Are Lineqr Algebroic Equotions? In Part Two. In part .*:. *r.. + aloxn : [.f r) : 0.s arecon_ stanrs.x..relare systembehavioras represented by ifre levels or fundamenlat equarions of engineering r89 . nrri.. : xr.. '.": .. fJ.y..1x1 * anZXZ I . : a1. * a2. . ... .. the b. Such systems are either linear or nonlinear.i Lineor Syrtems ii 3. and n is .x.xr) : 0 .. (PT3..these teadto balance or continuiry equarions rhar llT'p.x.:l!:li.):0 fik..t) wherethe rz'sareconstant coefficients.f.: equatrons.....xp .xr..1. linear Algebroic Equotionsin Engineeringond Science ::iy^. x..
in Fig. The. (PT3. As an example. and so lbrth. you saw how singlecomponent systems result in a single equationthat can be solved using rootlocationtechniques. For this case. "r1 might quantifythe amountof chernical rnass in thefirst reactor. The equations arecoupledbecause the individualpartsofthe systerrale influencedby other parts. Finally. the resLrlting equations are often of the linear algebraic folm of Eq. thea's for Fig.For example. PT3.suchasthe feedrate. (ol lumpedvorioble system thot involves coupled linitecomponents ond (b) distributed vcrioble system lhot involves o confinuum.lc.I Two lpes of syslems thot ccn be modeledusinglineorolgebrclcequotions.l ).t90 PART 3 LINEAR SYSTEMS Feed @t FIGURE PT3.nust be solvecl sinrultaneously. Multicolnponentsystems resultin a coupledset of mathematical equations that r. the principleof massconservation can be usedto formulatea model for a series of chemical (Fig. For instance.r2 rnightquantifytheamount in the second. Consequently. reactor4 receives chemicalinputsfiom reactors 2 and3.t's areusuallyrneasures of the magnitudesof the responses of the individualcomponents. its response is dependent on the quantityofchernical in theseotherreactors.The forcing lunctions arethe feed ratesof the chemicalinto the system.ln as an example. Multicomponentproblemsof thesetypes arisefiom both lurnped(macro)or distributed (nricro) variable mathematical models. When thesedependencies are expressed rnathematically. Using Fig. The systempropertiesare the reactioncharacteristics of the chernicaland the reactors'sizesand flowrates. When we studiecl rootsof equations. PT3. The a's typically represent the properties andcharacteristics that bearon the interactions betweencomponents.theb's usuallyrepresent the forcing functionsactingon the system.PT3. response of the quantitybeing modeledto the properties or characteristics of the system alrdthe external stimuli or fbrcingfunctions actingon the system.. reactors the quantitybeingrnodeled is the massof the chemicalin eachreactor. PT3.Lrrmpedvoriable probLems involve coupled .la mightbe reflectiveof the flow ratesof mass between thereactors.l a).
Diffbrential equations derivedfiom conservation laws specify the distribution of the dependentvariable fbr such systems.reactors.r e l e c t r i c i t l .2 PARTORGANIZATION Due to its importancein fbrmulating and solving linear alsebraicequations.3. PT3. Chap. discussed in this The techniques part can be used to solve fbr the unknownswhen the equationsare linear and algebraic. Aside fiom phvsicalsystems. Chapter10 illustrates Such solutiontechniques are valuable1br caseswhere many righthandside vectorsneed The chaoterends witlr a brief outline of how MATLAB solveslinear to be evaluated.Aside from coveling the rudimentsof matrix representation anclrnanipulation. The distribution of chemicals alongthe lengthof an elonqated. reactor(Fig."naive" Gausseliminationis discussed.2 PART ORGANIZATION t9r finite components.These differentiai equationscan be solved numericallyby convertingthem to an equivalentsystemof simultaneous algebraic equations.the concentration at the middle of the reactor in Fig. We starl with it allows the fundarnental this "strippeddown"versionbecause technique to be elaborated potential probon without complicating Then.rapter also describeshow man'icesare handledin MATLAB. details. The fbcus of this discussion will be the process of switching rows. the cl. simultiineous linearaluebraic equations also arisein a varietyof mathernatical problemcontexts. monrenlum o.The chapter endswith a brief description of eflicientmethods fbr solving tridiagonal ntatrrces.For example. TheseeqLrations arecoupledbecause the variables at one location are dependent on the variables in acljoining regions. 8 provides a brief overviewof tnotrir algebru.and electric circuits. The three jumpersat the beginning bungee of Chap. 3.8 area lumpedsystem.lb) is an exantple rectangular of a continuous variablemodel. Theseresultwhen mathematical functions are required sirnultaneously. Conversely. we discuss lems of the naive approirch to mjnirlize and cirand presenta number of mocliflcations cunlventtheseproblenrs. s\/stet)ls. The solutionof suchsetsof equations leplesents a major applicationareatbr the methcldsin the fbllowing chapters.distributed variable problems attempt to describethe spatial detail of systerns on a continuous or semicontinuous basis.orpdrlial pit'oting. .Similar examplescould be developedfor the spatialdistributionof temperature.Examples include trusses.is a function of the concentration in adjoining regions. Eachcondition to satisfyseveral conditions results in an equation that contains known coefflcients and uuknownvariables. PT3. These approaches arepresented oneoftlre rrrethodsto provideyou witlr visualinsightandbecause the elimination of unknownsrepresents lbr Gauss the basis elimination. After this prelirr.11. how Gauss eliminationcanbe lbrmulatedas an LI) factoriZation. Somervidely usednunrerical techniques that employ simultaneous equations are legression analysis and splineintel'polation.rinary material.Before discussion of this technique. a pleliminary sectiondealswith simple methodsfor solving small systerns.in subsequent sections. Chttpter 9 is devotedto the most fundamentaltechniquefor solving linear algebraic systems: launching into a detailed GausseLimittolion.
r92 PART 3 LINEAR SYSTEMS Chapter 11 startswith a descriptionof how LU factorizationcan be employedto efficiently calculatethe mati.which has tremendous utility in analyzingstimulusrelationships response of physicalsystems. The remainderof the chapteris devotedto the importantconceptof matrix condition. the Jacobi method.The chapterendswith a brief description of how nonlinearsimultaneous equations canbe solved. . Seidel method. which are similar in spirit to the for rootsof equations in Chap.6. Chapter I2 dealswith iterative solutiontechniques.The conditionnumberis introducedas a measure of the roundoff errorsthat can result when solving illconditionedmatrices. That is. although a description is provided of an alternative approach.r inverse. they involve guessapproximate methods discussed The emphasis is on the Gaussing a solutionand then iteratingto obtaina refinedestimate.
nrl effiuluso the [e of o the UESSAUSS . \OU'\E GO\ A.f t 1 r 1 t1tlg * kr(.t'1.. . cord is fully extended place verticerlly so that each in U. as measured crn define threedistances. .. r r ) : 0 tntS .Figure 8.and .2.and tridiagonal. freebodydiaassumethat eachcord behaves gramscan be developed for eachjumper as depicted in Fig. ' . 8. 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. Suppose that you are askedto computethe displacernent of eachof thejumpers. a steadystate canbe writtenforeachjumper: Using Newton'ssecond : Q m 1 gI k z ( x z.l.irz) : 0 .PROBLEN\ by bungeecords.ln shows them upposerhat three jumpers are connected It We but unstretched.t'2. Being able to identify the following typesof matrices:identity. unstretched positions tually cometo the equilibriunr shownin Fig. Knowing how to represent a systemof linear algebraicequations in matrix form..If we as a linear spring and follows Hooke's law.k: (. . forcebalance law.tierrhel are reJeased.r: .ing held \ dovnv'ard from eachof their .diagonal. Understandin m ga t r i xn o t a t i o n . 8. Knowing how to perlbrm matrix multiplication and being able to assess when it is feasible. 9/ grar it1 takeshold and thejumpers s ill evenpo:itions.triangular.1 1 ). symmetric.r: x) k z ( x z.1b.\.I.Specificobjectivesand topics coveredare e . Knowing how to solve linear algebraicequations with left division and matrix inversion in MNLAII.
2 Freebody diogroms."r2) tr (a) Unstretched lr) 11 ffi ( b ) Stretched tt nrrg k.r94 LINEAR ATGEBRAIC EQUATIONS AND MATRICES ". wherenr.(x2. : the springconstant for cordT(N/m). A.k:rr : mzg ktxzlk3xl7n19 Thus.x) tl ill m$ FIGURE 8.:Ol?r u1 n ffi . equations Chapters8 through l2 will introduceyou to how MATLAB is used to solve such systemsof equations. . the problem reducesto solving a systelnof three simultaneous equationsfor the threeunknown displacements.tr * (kz I kz)xz.: Iri il) n ffi il z'^r li: n ffi ?r t:tl lr) _H ffi I'l ffi Arxr t2(r.I Threeindividuo s conneciedbv bunqeecords FIGURE 8.ri : the displacernent of jumper I measured downwardliom the equiiibrium position (m). The tbllowing sections outline how matricesprovide a conciseway to represent and maninulale l i n e r ra l q e h r a i c . 8. andg = gravitational acceleration nr/sr). : the massof jumper r (kg).r1 .11) tr n ?r i I tl I m tg t.81 ( k 1l k ) x 1 kzxz :mtg (8.(.1) ftz. Collecting termsgives 19.these are linear algebraicequations. Because we have useda linear law for the cords.I MATRIXALGEBRA OVERVIEW Knowledgeof matricesis essential for understanding the solutionof linear algebraicequations.
8. [A] is the shorthand vidual element of the matrix. azu azz .3.3 A moirix.One as in fbl . the first subscriptof each element is to employ a dropped.1 Mqfrix Nototion by a single symbol.' FIGURE 8. A horizontalset of elementsis called a row and a vertical set is called a column.The secfirst subscript I alwaysdesignates designates the column.MATRIX ALGEBRA OVERVIEW I I ll I I 3 Column a:t azt at2 I at3 I iiil Qt.As with the notation when it is desirable to employ a specialshorthand row vector. I In addition to specialbrackets.elementa23is in row 2 and column 3. suchas [b]: [bl b2 brl are called row yectors.Also.t. Row2 t^t  onl o. way to accomplishthis is to employ specialopentopped Matrices with column dimensionn : l.3 has m rows and n columns and is said to have a dimensionof m by n (or m x n). as in {c}. ond subscriptT The matrix in Fig. Note that for simplicity. 8. the secondsubscriptis dropped.1.thereare occasions this is One way to accomplish to distinguisha column matrix from other typesof matrices. 8.l brackets. to employ specialbrackets.The the numberof the row in which the elementlies.n3 a. and matrices we will use caseto distinguishbetweenvectors(lowercase) (uppercase). Matrices with row dimensionm : l.:&it:''. For simplicity.2) . As A matrix consistsof a rectangulararray of elementsrepresented an indinotationfor the matrix andai 1 designates depictedin Fig. it shouldbe mentionedthat thereare times when it is desirable specialshorthandnotation to distinguisha row matrix from other types of matrices. It is referredto as an m by n matrix.) a. suchas lnl j tL  are ref'erredto as column vectors.For example.r.. L: (8.
rl ott ctstJ [.j3is termed theprincipal or maitt diagonal of the matrix.4]: [''' 422 I .For such systems....I L. as in fo.: . I lAl=l a) )  orr) L Note that wherelarge blocks of elementsate zero. : aii for all i i's and. Squarematricesare particularly important when solving setsof simultaneous linear equations. r r ..l's. That is.they are left blank. For example. The identity matrix hasproperties similar to unity.the number of equations(corresponding to rows) and the numberof unknowns(corresponding to columns)must be equalfor a uniquesolutionto be possible'Consequently.. as in tAl:l [rl I LlJ I The symbol Ul is usedto denotethe identity matrix. a 3 x 3 matrix is atrl I A l : I a r .and a. An identi4' matrix is a diagonalmatrix where all elementson the main diagonalare equalto 1. [st2f tAl:ll3 7l L2 7 8_l i s a 3 x 3 s y n r m e t r in cr a t r i x . square matricesof coefTicients are encountered when dealinewith suchsystems There are a number of specialforms of squarematricesthat are important and should be noted: A symmetricmatrix is one wherethe rows equalthe columnsthat is.1 The diagonalconsistingof the elementsas.. as in rl12 o. For example. A diagonal nmtrix is a square matrix where all elements off the main diagonal are equal to zero. a ) .". a. a22. tttl n ate called .square matrices.) . [A]Ul: [/][A]: tAl An upper triangular malrx is one whereall the elements below the main diagonal are zero.r96 LINEAR ALGEBRAIC EAUATIONS AND MATRICES Matrices where rn : lr.
ALGEBRA 8. . For example.r (/tl L Or. tAl :  i o11 1..f i o . dt..with the exceptionof a band centered on the main diagonal: f al ln l: t.Two nr by n natrices are equalif. every elementin the first is equal to every elementin the secondthat is.2. i I i I I i A bandedntotix has all elernents equalto zero..n. . Rart Pat.2. . is by corresponding in ninus obtained subtracting terms. . . I 8. aZt I I  at) a:z .1. as ln I f o.2 Motrix Operoting Rules Now that we have specifiedwhat we meanby a matrix. is accomplished of the resultingmatrix [C] are computedas terms in eachmatrix. Both addition and subtraction t A l + t B l: [ B ] +[ A ] and associative: ( t A l +t B l ) + [ c ] : [ A l +( t B ] + tcl) The multiplication of a matrix [A] by a scalarg is obtainedby multiplying every element of [Alby S.] "'. as [E] [F].. I aat aqc ) lI I I j I I I I The precedingmatrix has a bandwidthof 3 and is given a specialnamethe tridiagonal matri. n .I MATRIX OVERVIEW 197 A lower triangular malrrx is one whereall elements abovethe main diagonalarezero.n.. 2 . we can definesomeoperatingrules that govern its use.say. . ..I efltt .. . .. The elements c'i1: ct. [Al and [B..r...ltfollowsdirectlyfromtheprecedingdefinitions that addition and subtractioncan be performedonly betweenmatriceshaving the same dimensions. S i m i l a r l yt..i1b.... ..i f o r i : 1 .for a 3 x 3 matrix: I D I: s l A l: l i o l fPo. 2 .a y . dij:eij fij I i I i I forl: l. are commutative. m a n dj : 1 . i o r . by addingcorresponding Addition of two matrices. ..mand j:l...h e s u b t r a c t i o on f two matrices s.. and only if. I I on.. [A] : [B] if ai1 : bi1 for all i andj..
3) where n : the column dimensionof [A] and the row dimensionof [B]. in this case[A].4 Visuol depiction of howtherows line ondcolumns upin FIGURE 8. if [A] is an m by n matrix.. elenent is obtainedby addingthe productof individual elenents from the lth row of thefir$ matrix.' order nf mrrltinlic:rtion is .4 depicts how the rows and columnsline up in matrix multiplication."t .multiplicationis not generallycommutative: tAttBl+ lBltAl Thot ic rh. F.r98 LINEAR ALGEBRAIC EAUATIONS AND MATRICES ql t. The productof two matricesis represented as [C] : tAltBl.oislt11 t:l {8.5 provides an easy way to check whethertwo matrices can be multiplied.Figure 8. the multiplication could not be performed. multiplication is possible [::]Ll motrix multiplicoiion.matrix rnultiolicationis associative: (tA JtB[c] l ) : tA l (tB l tcl ) '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. matrix multiplication can be perfbrmedonly if the first matrix has as many columnsas the nunber of rows in the secondmatrix.) Interiordimensions are equal. if [B] were an m by i matrix. [B] could be an n by / matrix.the resulting[C] matrix would have the dimensionof m by /. wherethe elements of [C] are dellned as + ) . [sxs1x722 II Exteriordimensionsdefine thedimensions of the result FIGURE 8. According to this definition. For this case. However. If the dimensions of the matricesare suitable. the c.5 Motrix multlplicotion conbe per{ormed if only l h e i n n e rd i m e n s i o no sr e e q u o l . Figure 8. That is. by theTthcolumn of the secondmatrix [B].'I rl > { 1i : [A'l^"n [Blnt = [C]. Thus.
the transpose is equalto the zu of the original 7.se of a matrix involvestransformingits rows into columnsand its columnsinto rows. u .if a matrix [A] is squareand nonsingular.t a.For example.One simple advantage The transpose is that it allows a column vector to be written as a row and vice versa. e 1 ri. element matl'lx. ChapterI I will deal rvith techniques lbr using numericalmethodsand the computerto calculatethe inverse lbr suchsystems. A tnartrix is augrnented by the additionof a column (or colunurs)to the original matrix. ' u . However. .r I L. .l f u... J lc1 c2 c:l tnen {c}r : In addition..the multiplication of a matrixby theinverse is analogous to division.J t h et n r n s p o s e d. 0t) a..B. . For examole.if J.for the 3 x 3 ntatrix: l A l : l . element of ['' l t A l r: I . . cailed the inver. e s i g n a t eld as . has a variety of functionsin matrix algebra.s d e l i n e d l. ( t ) ..se of [Aj. The final matrixmanipulation that will haveutility in our discussion is uuqmenratiott.. t l. nultiplicationof a matrix by its inverse leads t o t h ei d e n t i t y matrix. The trartslto.l In otherwords. fbr which tAltAl ' : 1 J e ]: 1 1 1 [A] Thus. That is.in the sense that a numberdividedby itself is equalto l...suppose we have a 3 x 3 matrix of coefficients. I .. .I Lr. Two othermatrix manipulations thirtwill have Lrtility in our discussion are the transposeand the augmentation of a matrix. ' (t.t...lr. For example. . The inverseof a 2 x 2 matrix can be represented simply by lAl ': ail422 anq2l I o" n'tl itrrJ Lat tbr higherdimensional Similarfbrmulas matrices aremuchmoreinvolved. i . .. 't2 r. We might wish to augmentthis matrix [A] with a 3 x 3 identity matrix to yield a 3by6dimensional matrix: a1t [. thereis anothermatrix [Al r.'I MATRIX ALGEBRA OVERVIEW Althoughmultiplication is possible. matrix divisionis not a definedoperation.I [.. . "..l the rr.the transpose has numerousmathematical applications... r . d1 l ool 0l0l 0 0 r_j ott I LaSt a2t a3t 021 {41 .
t4 B 2l. It is bestapproached lationsareimplemented as a handson exercise onthe computer.i6 27 3 Next we will createanother3 x 3 matrix on a row basis. we can perlbrm the operations ratherthan on the two individual matrices. EXAMPTE 8. Then we can combinetheseto tbrn. The following exampleillustrateshow a variety of matrixmanipuwith MATLAB.4 3 6 2 6 The transpose of [A] can be obtainedusing the ' operator: >> A'.200 LINEAR EAUATIONS AND MATRICES ALGEBRAIC has utility when we must perform a set of identicaloperations onthe Such an expression on the singleaugmented matrix rows of two matrices. 1 5 6 i .rthe matrix >> p B = lx. Thus. 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 . S o l u t i o n . >> Y = t 5 8 1l. y.7 4 2._t 61] .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.First createthreerow vectors: >> x _ 1B 6 gJ. >> z . C r e a t e a 3x 3 m a t r i x : >> A = [1 5 b.
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. However.8462 0.3846 4.3231 0.MATRIX ALGEBRA OVERVIEW Further.8308 0. an error message will occur >> A*D . If [A] is multiplied times [D].we can subtract[B] from [C] to anive back at [A]: >>C=CB 1 'l 5 4 6 2 3 6 7 Because their inner dimensions are equal.01s4 0.5 B 1l.[A] and [B] can be multiplied >> A*B 7 44 26 94 9A 86 26 7r 1 Note that [A] and [B] can also be multiplied on an elementbyelement basis by including a period with the multiplication operator as in >> A.'?! LTta' usrnq Inner matrix dimensions must agree.4769 .2154 0.6L54 0. if we reverse the order of multiplication so that the inner dimensions match.2462 a. *B B 35 12 30 32 48 54 2 14 A2 x 3 matrix can be set up >> D = t1 4 3.
matrices can be augmented sirnplyas in >> Aug Lrrn = tA Il t4: 361 010 Note that the climensionsof a matrix can be determined bv tlre size firnction: 3 8. For example.0000 0.0000 0.the inversecan be multiplied by the originalmatrixto give the identityrratrix: >> a rl:.0000 The eye functioncan be usedto generate an identitymatrix: 22 I= f = e)re(3) 0 0t) 0 1 Finally.5) . AXAI  1.0000 0. a 3 x 3 setof linearequations. r 3: bl b.0(r00 1.4) : * a32.3 RepresenlingLineor Algebroic Equotionsin Motrix Form It shoulclbe clear that r.'ide a concisenotation tbr representing simultaneous linearequations.natrices pror.0000 0.0000 0. (8.0i100 0. fi.t2 t t 2 1 x 1* 4:rrr a 2 2 \ 2 1 o 2 1 .Y2 I {13113 can be expressecl as [A]{r} : {b} (8.rr000 1. f 0t1.V : drtrr * a12.I .202 LINEAR ALGEBRAIC EAUATIONS AND MATRICES To testthat this is the conect result.
n of the second This part of the book is devoted to solvingEq. theequation hasbeensolvedfbr {r}. (8. t a:2 .q)*b . A typical example of equationsthan unknowns.r1 x2 r:] Recall the definition of matrix rnultiplication tEq. (rows)thanunknowns with moreequations It shouldbe notedthatsystems regression where A typical exampleis leastsquares m > n. realizethat Eq.?? is numerical oDtimization.2.t I I a): arrJ r Lrlr {D} is the columnvectorof constants: lblr : lb1 bz bt) and {.4)and (8. are said to be nndertletermined.the equation becomes Because [A] tr) : tAl r{b} (8. with less with n coefficients is fitto m datapoints(x..y).6) example of how theinverse Therefbre." operator asin >>r=A\b Thesecond i s t o u s em a t r i xi n r e r s i o n : >> x = inv(.5)is a valid matrixmultiplicaEqs.(8. are said to be overdetermined.. hasgreatvaluein the engineering analyses of suchsystems... (columns). (8.EQUATIONS WITHMATLAB LINEAR ALGEBRAIC 8. A formal way to obtaina solution using matrix algebrais to multiply eachside of the equationby the inverseof [A] to yield tAl rtAl{r} :tAl1i.:: o.5)areequivalent. efficientway is to employthe backslash.2 SOTVINGTINEAR MATLAB provides The mosl two directwaysto solvesystems of linearalgebraic equations. tion because the numberof columnsn of the first matrix [A] is equalto the numberof rows matrix {r}.5)lbr {x}. systems an equation < n. This is another playsa role in matrix algebra thatis similarto division.l lo .r}is the columnvectorof unknowns: {r}r : [.b} '[A] equals the identitymatrix. or "leftdivision.Conversely. the matrix inverseitself numericalalgorithms.lt shouldbe notedthatthis is not a Thus. (8... However. underdetermined svstems WITH MATTAB ATGEBRAIC EQUATIONS 8.as discussed in Section11. other approaches are employedin very efficient way to solve a systemof equations.2 SOLVING where [A] is the matrix of coefficients: ttrl tdl  otl .3)l to convinceyourselfthat Also.1.
l .the matrix inversesolutionis lessefficient thanusing the backslash.81 588. 0 50 50 586. 100 150 50.1.: {.0 50 501 150 100 rJ >> mq  100 150 50 [588.1)gives these i:B : {::::.6000 586.8000 Employing left division yields >> x K\m9 4r.s I L o Start up MATLAB and enterthe coefficientmatrix and the righrhandsidevector: >> K = t150 v 100 0.2 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 with MATLAB ProblemStotement. Jumper Top(1) (2) Middie (3) Bottom Moss (kg) Spring Constont (N/m) Unstretched Cord Length (m) 6A 7A BO 50 100 50 2A 2A 2A Solution. UseMATLAB to solvethebungeejumperproblemdescribedatthe The parameters for the problem are beginningof this chapter.2020 55.9170 71.204 LINEAR ALGEBRAIC EQUATIONS AND MATRICES As statedat the end of Section8.t} so sol ['. 184. J Izs+. (8.7000 784.7. EXAMPLE 8. Both optionsare illustratedin the following example. Substituting parameter valuesinto Eq.5130 Alternatively. multiplying the inverse of the coeflicient matrix by the righthandsidevec.8] [l.6.3. t o r g i v e s t h e s a m er e s u l t : .
8.3 CASE STUDY
>> x = inv(K)*mg
4r.2A2A 55.9170 7I.613A Because the jumpers were connected by 20m cords,their initial 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 it is subjectto the weight of two jumpers,one might expectthe second cord to be extended Ionger than the third. However,because it is stiffer (i.e., it has a higher spring constant),it stretchesless than expectedbased on the weight it carries.
(a) (bl
FIGURE 8.6 Positions of three
individuo snnecied cl o by bungee cords. (o)Unsiretched ond (b)stretched
CURRENTS AND VOLTAGES IN CIRCUITS
Bockground. Recallthat in Chap. 1 (Table 1.1),we summarized somemodelsand associatedconservationlaws that figure prominently in engineering. As in Fig. 8.7, each model represents a systemof interactingelements. Consequently, steadystate balances derived liom the conservation laws yield systems of simultaneous In many cases, equations. such systems are linear and hencecan be expressed in matrix form. The presentcasestudy 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. all currentsenteringa node must be zero 8.812), or
fi:o
(8.7)
where all current enteringthe node is considered positive in sign. The current rule is an applicationof the principle of conservation of charge(recallTable 1.1).
206
ALGEBRAIC EQUATIONS AND MATRICES LINEAR
*_.r*_
il
( a ) C h e m i c ae l ngineering
( b ) C i v i le n g i n e e r i n g
f,,
(a)
ffi,ryl M
Circuit (c) Electrical engineering
+AA]
R,
WI
(d) Mechanical engineering
Srhamntir
+
i,j
(b)
FIGURE 8.8 FIGURE 8.7
which, ot steodyslote,con be modeledwilh lineorolgebroic Engineering syslems equolrons.
ronro<anlntinnq
rule of (o)Kirchhoff's current ow. o n d ( b ) C h m ' ls
The voltage (or loop) rule specifiesthat the algebraic sum of the potential differences (that is, voltage changes)in any loop must equal zero. For a resistor circuit, thisis as expressed
fcI^:o
(8.s)
of where f is the emf (electromotive force) of the voltage sources,and R is the resistance any resistors on the loop. Note that the secondterm derives from Ohm's law (Fig. 8.8b), which statesthat the voltage drop across an ideal resistor is equal to the product of the cunent and the resistance.Kirchhoff's voltage rule is an expressionof the consentationol enerSy. Application of theserules results in systemsof simultaneouslinear algebraic Solution. conequationsbecausethe various loops within a circuit are interconnected.For example, sider the circuit shown in Fig. 8.9. The currents associatedwith this circuit are unknown
onesimply no greatdifficultybecause This presents anddirection. bothin magnitude
assumesa direction for each current. If the resultant solution from Kirchhoff's lawsis negative,then the assumeddirection was incorrect.For example,Fig. 8.10 showssone currents. assumed
8.3 CASE STUDY
continued
ili
,il
'll
I
!l
R:5O Vt:200Y
a n =i s o
7o:0V R:20f)
A resistor circuit lo be solved using simultoneous
i;n"^' ^l^.h.^;. an, rnf innc
FIGURE 8.IO
Assumed cLruenr direction5
Given these assumptions, Kirchhoff's current rule is applied at each node to yield in*isz*jrr:0 ios* isz ls+ :0 i a 3 1 2 : i5aiaj:Q Application of the voltage rule to each of the two loops gives iosiszls+:0 is,rRs+ i+tR+: * i12Ri2* l52R52 : Q iosRos  iszRsr : 200 0 I ieRe or, substituting the resistances from Fig. 8.9 and bringing constants to the righthand side, *151s+ 5ia3 10i32 : Q * 10i52 20ios10is:f5ip:200 Therefore, the problem amounts to solving six equations with six unknown currents. These Q
equationscan be expressedin matrix form as
[r
l0
l
l
r
0
I
l0 lo
t0
0 o
l0
t0
1 o o
0
l
0
0l
l12 r52 t3z .oJ
r)4
0 o
0
15
0 I
Ls ro
_20 o
r 1 I  sl ol
0 
iqz
:{,L
208
ALGEBRAIC EQUATIONS AND MATRICES LINEAR
continued
Although impractical to solve by hand, this system is easily handled by The solution is >>A=t111000 0 1 0 1 *1 0 0 0 1 0 0 1 0 0 0 0 1 1 0 10 10 0 15 *5 5 *10 A 20 0 ol; >> b=t0 0 0 0 a 2001,; >> current=A\b
= current 6.1538 4.6L54 1.5385 6.1538 *1.5385 1.5385 Thus, with proper interpretation of the signs of the result, the circuit curents and ages are as shown in Fig. 8. 1L The advantages of using MATLAB for problems of this should be evident.
8.1 ci
comm€ the oril E.2 At
tAl
rcI
LE tFl
Answe (a) WI (b) Ide (c) Wl
J t2'
(d) Pe (l
FIGURE 8.I I obtoined using MATLAB The solution forcurrents ondvoltoges
V: 169.23
(4
PROBLEMS
PROBTEMS
Given a square matrix [A], writc a single line MATLAB thatwill createa new matrix [Ang] that consists of original matrix[AJ augnrented by an identity matrix [11. A number of mafices are definedas
(7) [Bl x [Al (10) [ 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 setof equations in rnatrix forrn: 50:5,r:7.r: 4rz+7.\*30:o : ^10, 3xzf 5;:r rr  7.r.r UseMATLAB to solve lbr theunknorvns. In addition, usert to compute thetranspose andthe inverse of thecoefficient matrix. 8.4 Three matrices aredefined as
L2 0 4J
l r t, _ (\
[137f lr 2 7l
, i ,'l il pt:l:
[r s 8l
L4 0 6_l
Ll LG):17 6 4J
t rl rrl=17
1l r r r = f ro 7 .11
,'l , fi ,",: l],.,:; [,i, f l'],",=
(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 of flow (Q) and concentration {r.). At steadystate, the mass llow irrto and out of each reactor must be equal.
thefbllowingquestions regardingthcse matrices: What arethe dimensions of the matrices,l Identify the square, coluntn, and row matrices. What arethe values of the clcments.. ar,. b.1, dzt, ezz, /r:,8r:? Perform thefollowing operations:
(r)trt + tBl (1) 7 x [B]
(2)IAI+ lFl 1s1 [A] x tBl
(3)tBl _ tEl (6)(C],
FIGURE P8.5
2ro
AND MATRICES EAUATIONS TINEAR ALGEBRAIC
For example, for the first reactor, a mass brilance can be written as 0 o r r : ofr Q t t ' t : Q r s c tI Q n c t
+"x?
I
Write massbalancesfor the remainingreactorsin Fig. P8.5 and expressthe equationsin matrix fbrm. Then use MATLAB in eachreactor. to solve for the concentrations is that of 8.6 An importantproblem in sffucturalengineering truss(Fig. P8.6). finding the forcesin a staticallydeterminate asa systemof coupled This type of structure can be described The linear algcbraicequationsderived from force balances. sum of the forces in both horizontal and vertical directions must be zero at each node, becausethe system is at rest. Therefore,for node 1: Lf,:0:Fr \ n'' : 0 : cos]0 Fjcos60 *F1.71 Fr sin 10 F.rsin 60 * Fr ,
P8.7 TIGURE
algebraicequationsin matrix form and then use MATLAB to solve fbr the unknowns. 8.7 Considerthe threemassfourspringsystemin Fig.P8.?, Determining the equationsof motion from XF' : ma,for eachmassusing its freebodydiagramresultsin thefollow' ing differentialequations:
/'
8.10 Tl identica the bott 2.5kg,t displac 8.11 Pe the circ 8.12 Pi the circ 8.13 D two mal lZl is n multipli the prol 8.14 D the tran plemen
for node 2: ) /nr :0: F : I F 1c o s 3 0 I F z nI H z
rr*lrlrrllr::0
\ rzr /
/kr+ k"\
/kr\ \mt/ /l'\
/ \m2 l
:0: 5 / ' n 'u
for node 3: \/r'n 0: r : " 
F rs i n 3 01 F 2 . , I V:
/,t'\ /k,*t,\ tr (31'' (
\m:/ \ ,t:
;,F,  F.rcos60l Fr.r,
/k.\ /k,'" \ ( a ) * r +{ ^ r r ^ 4 } x , : o
\.n.r/ \ m3 /
: D a u o : F : s i n 6 o ' *F : , ,r v :
where F,.,,is the externalhorizontal force applied to node I (wherea positiveforce is from left to right) and {.,. is the external vertical force appliedto node i (where a positiveforce is upward). Thus, in this problem, the 1000lb downward to F,.,:  1000.For this casc, fbrce on node I corresponds all other F,.,'s and {,,,'s are zero. Expressthis set of linear
andmr=mt= w h e r e f t, kt:10N/m, k.:ftr:30N/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 eachmass. solve for the acceleration 8.8 Solve
[ 3 + 2 i + ' lJ : r [ _ l I2+, I r . l i I i I [z:f
8.9 Perform the same computationas in Example8.2,but with the following characteristics use five parachutists
Moss Jumper I 2 3 4 5 55 75 60 75 90
(ks)
Spring Conslont (N/m) BO 50 70 r00 20
urltr"*ffi
Cord tength (m) t0 10 t0 t0
r0
P8.6 FIGURE
PROBLEMS
Three masses are suspended vertically by a seriesof springs wheremass I is at the top and mass3 is at bottom. lf g : 9.81 m/s', ffir : 2 kg, m2: 3 kg, 2, : kg, and theft's: l0 kg/s2,useMATLAB to solve for the ntsr Perform the samecomputationas in Sec. 8.3, but for circuit in Fig. P8.I l. Perform the samecomputationas in Sec. 8.3, but for circuit in Fig.P8.12. Develop, debug,and test your own Mfile to multiply matricesthat is, [X] : lYl[Zl, where lY] is m by n and isn by p. Employ f or. . . end loops to implement the ion andinclude error trapsto flag bad cases. Test program usingthe matrices from Prob. 8.4 Develop, debug,and test your own Mfile to generate tmnspose of a matrix. Employ f or. . . end loops to imthetranspose. Testit on the matricesfiom Prob. 8.4.
2ll
R:20c)
R:10O Vr : 150volts
ti
R:2O R=5O Vo: 0 volts
FIGURE P8.I I
R=30O
,
R=35O
1
Vr : 10 volts
R:8f) Vo= 150volts
F I G U RP E8 . I 2
. ...
GoussEliminotion
CHAPTER OBJECTIVES
The primary objectiveof this chapteris to describethe Gausseliminationalgorithm fbr solving linear algebraicequations. Specificobjectivesand topics coveredare ' . ' . . ' with the graphicalmethod Knowing how to solve small setsof linear equations and Cramer'srule. asin how to implementfbrward eliminationand back substitution Understanding Gauss elimination. how to count flops to evaluatethe efficiency of an algorithm. Understanding of singularity and illcondition. Understanding the concepts how partial pivoting is implemented and how it differs from Understanding pivoting. complete Recognizinghow the bandedstructureof a tridiagonalsystemcan be exploited to obtain extremelvef1icientsolutions.
t the end of Chap. 8, we statedthat MATLAB provides two simple and direct leftdivision, fbr solvingsystems of linearalgebraic equations: methods
>>
x
=
A\D
and matrix inversion,
>> x = inv (A) *b
ThismaChapters9 and l0 provide backgroundon how such solutionsare obtained. In addition,it is intended terial is includedto provide insight into how MATLAB operates. to show how you can build your own solution algorithmsin computationalenvironments that do not haveMATLAB's builtincapabilities.
9.I SOLVING NUMBERS SMALL OF EQUATIONS
2r3
The technique in thischapter is cllleclGauss it involves described elimination because combiningequafions to climinateunknowns. Althoughit is clne of the eirrliest rnethods fbr solving simultaneous equctions, it remainsarnongthe most importantalgorithnrs in use today and is the basisfur linear equationsolving orl many popular sotiwarepackagesinclucling MALAB.
il
9 . 1 SOIVING StylAtt NUMBERS OF EQUATIONS
Betbreproceediug to Gausselinrination, we will describe several methods that are appropriate fbr solving small (n < 3) setsof simultaneous equationsand that do not recluirea computer. Theseare the graphicalmethocl, Cramer'srule. ancl the eliminationof unknowns.
9.1.1 fhe GrophicolMethod
A graphicalsolutionis obtainable for two linearequations by plottingthen on Cartesian with one axis colresponding coordinates to r1and the othel to r2.Because the equations will plot as a straight arelinear.eachequation line. For example, suppose that we havethe tbllowing equations: 3rr *2.rr:
,. L l,^ 
l8
)
r> , thezb:cil:ta,{eaan.>olrccachttlthtseteuilUoilsrorrt! If vee.\uncrhet.i
rl:.tl+9 tt: I f I trl
3
linesthat is..t2 : (slope)rr * interThe equations are now in the forrn of straight of the cept. When theseequationsare graphed,the valuesof r1 and x2 at the intersection lines represent the solution (Fig. 9. l ). For this case,the solutionis ,r1 : 4 andxz : 3 . would be represented by a planein a For threesimultaneous equations. eachequation intersect would repThe pointwherethe threeplanes threedimensional coordinate system. resentthe solution. Beyond three equations,graphical methodsbreak down and. consequently,have little practical value fbr solving simultaneous equations. However,they are properties of the solutions. 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 the lines nevercross.Figure9.2b depicts For suchsituations thereis an inllnite number the casewhele the two linesarecoincident. Both typesof systems aresaidto be singular. of sollrtions. In addition,systerns that are very closeto being singular(Fig. 9.2c) can also cause problems.Thesesysterns this conesponds to the are said tobe illconditirnecl.Graphically, Illconditioned fact thatit is diiTicult to identifythe exactpoint at which the linesintelsect. solution systems will alsoposeproblenrs when they areencountered duringthe numerical sensitive to roundofTerror. This is because they will be extremely of linearequations.
2r4
ELIMINATION GAUSS
6rt
9.I FIGURE ofthe Theintersection equotions. lineor olgebroic of Mo simultoneous of o set solutlon Grcphicol
the solution. linesrepresenls
EX
9.2 FIGURE (o) ond solutions, nosoluiion, systems: ondilfconditioned of sinqulor depiction {b)lnflnlte Grophicol visuolly to detecl isdifficuh tholihepoint oI intersection oresoclose (c)ilicondiiioned theslopes system wtrere
9.1.2
Determinqnls qnd Cromer's Rule
Cramer's rule is anothersolution techniquethat is best suitedto small numbersof equa' tions. Before describing this method, we will briefly review the conceptof the determinant, t0the hasrelevance which is usedto implementCramer'srule. In addition,the determinant evaluationof the illconditionine of a matrix.
SOLVING SMALL NUMBERS OF EAUATIONS
Determinonts. The determinant can be illustratedfor a set of threeequations: [A]{,t} : {b} where [A] is the coefficientmatrix [Al: ort an anf f I a21 a22 azz I
tl LA3t 432 AT J
2r5
The determinantof this systemis formed from the coefficients of [Aj and is represented as
D : latt I u., at2 Ltt at3 o)1
i" ;;
Although the determinant D and the coefficientmatrix [Al are composed of the same elements, they are completelydifferent mathematical That is why they are disconcepts. tinguishedvisually by using bracketsto enclosethe matrix and straightlines to enclosethe determinant. In contrastto a matrix, the determinant is a single number.For example,the value of the determinant for two simultaneous ecuations
" .;
o .,
ott D : latr I I A)t A)) 
  l
is calculatedby D:atla22an(7zt For the thirdordercase,the determinant can be computedas
ott o"l ot'l D : a ,' , la lo  o ,r * o ,.l , la s! z la : rt t al:o r" .?.r.rl a.r:l aJ2 l
where the 2 by 2 determinantsare called minors. Determinonts
(e. 1)
Problem Stotement. Computevaluesfor the determinants of the systems represented in Figs.9.l and9.2. S o l u t i o n . F o r F i g . 9 . 1:
D
JL
1
2
:3(:2)2(1):8
ForFig.9 2a:
L _ !
D
2l 1

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

(9.23)
€tt I
.f,,l
8nt
Xnl
' n 
GAUSSELIMINATION
Notice that we have changedour notationfor the coefficientsfrom a's and b's to e's,/s, zerosin thesquare ma,g's,and r's. This was doneto avoid storinglargenumbersof useiess algonthn modificationis advantageous because the resulting trix of a's. This spacesaving requires lesscomputerrnemory. An algorithmto solvesuchsystems canbe directly patterned 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 9.5 S o l u t i o no f o T r i d i o g o n o lS y s t e m
Stotement. Problem Solve the following tridiagonalsystem:
l f2.04 2 .04 lr
, ll;tt_lTfl ':i :l t:t I t,3;1l
:2.04 l
thematrixh Solution. As with Gausselimination,the tirst step involves transforming e2l upper triangular form. This is done by multiplying the first equationby thef actor f ynl equation. This creates a zero in placeof e2and transsubtracting the resultfrom the second forms the other coefficientsto new values. f z : . f zt'j:
?a
T+t
?,',:0.8t') f,
1) : I '550 2JJ4e I :20,8 t40.8t 1.04
the elementaboveit in the first row is zero. Notice that g, is unmodifiedbecause istranr After performinga similal calculationlbr the third andfourth rows,the system formed to the upper triangularform
,.lot i33 [204 lI;i I:I I :os ,.i,J f l;;I I ift','; I
the linal solution: Now back substitution can be appliedto generate
: '+#: ,o: 'io
,r:
t'3  83rl
r5e 48o
fa
,'z  82x3
,fz
rl ^l 
8lJ2
.ft
 (l)159.480 14.22r :124.538 1.395  ( l) 124.538 20.800 : 93.718 1.550  ( l)93.778 40.800 :65.910 2.040
9.5 CASE STUDY
function x = Tridiaq(e,f,g,r) % Tridiag: Tridiaqonal equation solver banded system t x = Tridiag(e, f,g,r): Tridiagonal system solver. % input: ? e=subdiagonalvector Z f= diagonalvector g = superdiagonal 2 vector 2 r = right hand side vector % output: ? x=solutionvector n=lengrth (f ) ; ? forward
f ar V ).r
elimination
= e(k) /f (k1); factor  factor*g{k1); = f{k) f(k)  factor*r(k1); = r(k) r(k) end % back substirurion (n) ; x(n) = r(n)/f for k = n1:1:1 x(k) = (r(k)s(k)*x(k+1) )/f end
(k) ;
FIGURE 9.6 AnMJile to soveo tridiogonol sysiern.
9.4.1
MATLAB Mfile! rridiag
An Mfile that solvesa tridiagonalsystemof equations is listed in Fig. 9.6. Note that the algorithmdoesnot includepartialpivoting. Although pivoting is sometimes required,most tridiagonalsystems routinely solvedin engineering and science do not requirepivoting. Recall that the computationaleffort for Gauss elimination was proportional to n3. Because of its sparseness, the effort involved in solving tridiagonalsystems is proportional to n. Consequently, the algorithm in Fig. 9.6 executes much, much fasterthan Gausselimination,particularlyfor large systems.
MODELOF A HEATED ROD
Bockground. Linear algebraicequationscan arise when modeling distributed systems. For example,Fig.9.7 shows a long, thin rod positionedbetweentwo walls that are held at constant temperatures.Heat flows through the rod as well as between the rod and the surrounding air. For the steadystate case,a differential equation basedon heat conservation can be written for such a system as d2T ax
_ +h'(7. z):0
(9.24)
230
ELIMINATION GAUSS
continued
7,,: 20
fo: 40
I
Is = 200
ti{it
,r=0
 Lx;'7,,:20
{ ,
FIGURE 9.7 twowolls temperoture rodpositioned between of constont butdifferent A noninsuloted uniform
The {inited'flerence representolion 66is5. employsf6ur inls1ie,
where I: temperature("C), x : distancealong the rod (m), h' : aheat transfercoefficient ("C). betweenthe rod and the surroundingair 1m2), ztrdTo: the air temperature Given values for the parameters,forcing functions, and boundary conditions,calculus = can be usedto developan analyticalsolution.For example,if h' :0.01, 4, : 20, T(0) 40, and (10) : 200, the solutionis t^ 20 T :13.4523n0'tx  53.4523e0 +
(9.2'
In Although it provided a solutionhere,calculusdoesnot work for all suchproblems. we such instances,numerical methods provide a valuable alternative. In this case study, will use finite differencesto transformthis differentialequationinto a tridiagonalsystem deof linear algebraic equationswhich can be readily solved using the numerical methods scribed in this chapter. into a setof linear algebraic equations by Solution. Equation(9.24) canbe transformed the rod as consistingof a seriesof nodes.For example,the rod in Fig.9.7 conceptualizing between nodes.Sincethe rod has a length of 10, the spacing is divided into six equispaced nodesis A,x :2. it includes a secondderivative. Calculus was necessary to solve Eq. (9.20 because provide a meansto transfom approximations As we learnedin Sec.4.3.4,finitedifference algebraic form. For example, the second derivative at each node canh derivatives into approximated as dzT dt
Ti+t ZTi *Tir
Lx2
into where I designatesthe temperatureat node i. This approximation can be substituted (9.2q give Eq. to Ti+r2Tr * Ti*t
n, 2
+h'(7"4):0
9.5 CASE STUDY
continued
gives Collecting terms and substituting the parameters Ti*r*2.04Ti4+r:0.8
(e.26)
Thus, Eq. (9.24)has beentransformed from a differentialequationinto an algebraicequation. Equation(9.26) can now be appliedto eachof the interior nodes:
To*2.04T14:0.8  Z: : 0.8  Tt * 2.0472  Tz 12.04\  Z+: 0.8  Tt t 2.0474  Is : 0.8
(9.27)
The values of the fixed end temperatures, Io : 40 and Ts:200, can be substituted and with four unknownsexpressed moved to the righthandside.The resultsarefour equations in matrix form as
a; ffi' 'it;]{i^l:{}i}
>> A=12.A4 1 0 0 r 2.04 1 0 a r 2 .0,1 L t) 0 1,2.A4); >> b=i40.8 0.8 0.8 200.81';
>> T= (A\ b) ' T= 65.9698 93.',i18a 'r24.5382 r59.41 9a
(9.28)
So our original differentialequationhas been convertedinto an equivalentsystemof lineal algebraicequations. Consequently, we can usethe techniques described in this chapter to solve for the temperatures. For example, using MATLAB
A plot can also be developed comparing these results with the analytical solution obtained with Eq. (9.25),
. L j' i t;
>> x=[0:2:101; >> xanal=f0:101; >> TT0 (r) 13.452 j*exp (0.1*xanal) (0.1*xanal)+20;
t,r.rj=1:(Ydl); >> plot it ,'l , 'o' , ranal, Tartal )
53.4523*exp
. . .
As in Fig. 9.8, the numericalresultsare quite closeto thoseobtainedwith calculus.
232
GAUSSELIMINATION
confinued
Analytical (linel and numerical (pointsl solutions
220 200
180 160
9.1  the r (Fig. 4
9.2 
Chec equa
9.3 (
140 r. 120 100 80 60
(a) S
rl (b) c
fi
(c) c 9.4 (
10
(a) C
FIGURE 9.8 versus rod.Boih ondnumericol A plot of temperoture distonce olong o heoted onolyticol {line)
solutions ore disployed. lpoints)
(b) u (d) s
(c) U tt
Wecan In addition to being a linear system,notice that Eq. (9.28) is also tridiagonal. use an efficient solutionschemelike the Mfile in Fis. 9.6 to obtain the solution:
>> e=10 1 1 11; >> f=12.04 2.04 2.04 2.04); >> g=i1 1 1 01. >> r= [40.8 0.8 0.8 200.8] ; >> TridiaS (e, f, q, r )
tc 9.5 C
c
l.(
65.9598
93.7185
r2,4.5382
r59 .4195
(a) So (b) Co (c) On reg (d) Sol (e) Sol Intr
The system is tridiagonal because each node depends only on its adjacent Becausewe numberedthe nodes sequentially,the resulting equationsare tridiagonal. cases basedon conservation laws. often occur when solving differentialequations
9.6 ci' )
(a) Sol con
PROBLEMS
PROBTEMS
Determine the number of total flops as a function of number of equationsn for the tridiagonal algorithrn (b) Substituteyour results into the original equations to check your answL.t's. 9.7 Given the cquations 2r16x2rr:38
9.6). graphical Use the method to solve 8 . r t : 2 4 4.rt  {* r6,r:: 31
your resultsby substitutingrhem back into the Given thesystem of equations
3rr;r:*7.r.t:34 *8rr +  r 2  2 x 1:  2 0
(a) Solvebv Gauss c.limination with partialpivoting.Show all stepsol the computation.  l . l r 1* (b) Sr"rbstitutc your rcsults into the original equationsto l0r2: l16 checkyour answers.  2xt+ ll .4.b : 174 9,8 Pcrforn.r thc samccalculations asin Example9.5, but lbr graphically Solve and check your resultsby substituting the tridiagonal system: them buclinto thc equati(rn\. 0.4 0nthe basis of thegraphicalsolution,what do yor.r expect Ios l tll . r I c).4 0.8  0.a regarding theconditionof thc system)    L1 Compute thedeter minant. {).4 o8j.r. L Given thesystemof equations 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 of the reactor :2 5 . t12  r 2 (c, with unirsof milligrarns fronr which thc flow originates Compute thedeterninant. per cubic metcr).If the systcmis at a steadystate,the transUse Cramer's rule to solve fbr thc r's. f'cr into each reactorwill balancethe transferout. Develop Use Gauss elimination wi(h partialpil,otingto solvetor r.niissbalarrce equations for lhe reactors and solve the the r's. threc simultaneous linear algebraic equations for their Substitute your resultsback into the original equations conccntratlons. tocheck yoursolut.ion. Given theequations
:{f,
0.5;t1 .r2:1 . 0 2t2 1x2:
9.5 18.8
Qnct
Solve graphically. Compute the deterrninant. {b) (c)0n the basis of (a) and (b), what would you expect regarding the system'scondition? Solve by the elinrinationof unknowr.rs. {d) (t)Solve again.but with a,, modified slightly to 0.52. your results. Interpret 9,6 Given theequatiolrs 10* t1 2r2,r3: 27  3.t1  6t2* 2.r3: 61.5 .r1 *.r2*5.r::21.5 (l) Solve bv naiveGuuss elirnination. Shorr,' all steps of the computatton.
200 mg/s
Qz(z
Qr.: 120 Qrr: 40 Q,z= 90 Qz::60 Qyt:3o
FIGUREP9.9 Three reociors linked by pipes Theroteof moss trcnsfer through eochpipeis equo to theproduct of f ow Q cnd concenirotion c of thereccfor fromwhichthef ow orgrnoles.
234
GAUSS ELIMINATION
9.10 A civil engincer involved in constructionrcquires the lbllowine difl'crential equationrcsultsfiom a steady$ate .1800,5800, and 5700 rnr of sand, line gravel, and coarse massbalance fbr a chemicalin a oncdimensional clnal: gravel,respectivcly, lbr a building plojcct. Thcre arc thrcc dt llt: 0:D U ,kc pits from which thesematerialscan be obtaincd.The cornd.\' d.r pits of thcse is 1rosition r.'u'hcre D = diffuc : conccntri.ition, / : tinlc, r : distance, sion coclllcicnt. U  fluiclvelocity. and li = a firstorder dccay late. Convert this ciitilrcntial ccluation to an equiva. Fine Grovel Sond Coorse Grovel lent systcm equations. D= of simultaneous algcbraic Given : 311 2, U : l, k  0.2, r'(0.1 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 9.13 A stage cxtraction is depicted in Fig.P9.13. In srrch systems, ofa a streamcontainirtga weight tiactionr',,, F,. Hou, nranv cubic melels rxlrst be hauleclfiorn u'achpit in chemical enters fronr the lcft at a mass f'low late of rvcight of Siniultaneously, solvcnt a tiaction x,n a carrying nceds? orderto meetthe cnginecr's flow F,. the same enters flom the right at a rate0f chemical 9.ll Arr clectricalengineer supervise's theploductionof three balancc can be reprcsentcd as typcs of clectrical componcnts.Three kinds of rrraterial Thus,lbr stagci, a nrass
' ) t
FIGUR
rnctal.plastic,anclrubberare rctpriledtirl production.The to prodLrcc eachcomponent are amountsneeded Metol (g/ component) Plosric (9/ component) Rubber (9/ component)
(P9. l 3n) At cach stage. an equilibriumis assumcd to be established bctween,r', andr, as in ''i K : {P9.ll}) (P9.13b) wheleK is called Equation a distribution coefficient. into Eq. (P9.13a) can be solvedfbr r,and substitutcd to yield F, \ /F, \ rPg.ljir  t' r a /) r ' ,  ( ; = K I r , ' : g r \rt / : 0. 1, F.: 1000kg/h, rin = 0, and lf Fr : 500 kg/h, .\'in K : 4, delerminethe valuesof _r',,u, anclx.,,,, if a tlvestage rcactoris uscd.Note that Eq. (P9.l3c) must be modified to accountfbr thc inflou, weishtliactions when applied to the filst and laststages. r' ,/ ('\
9.14 A viscous pipe se( mechan flows ir to obtai Q, I Qt) 3Qt
Componenf
I
2
J
t5 l7 t9
03c o4a
0 55
o
2
o,=
Qt=
o.=
If totalsof 3.89,0.095, and 0.282 kg of rnetal,plastic,and lubber', respectivcly, arc availablecachday, how nranyconrponentscan be producedper day'? 9.12 As cle.scribed in Scc. 9.3, lineal algebraicequations can arise in the solution of difl'erentialcquations.For example.
9 . 1 5A followi BC,AL
74k
<_FIOW: A_
Flow:
F,+
P9.I3 FIGURE
process A stoqe extroclion
FIGUI
PROBLEMS A,+AD:o A,*AB:0
74+ BC+(3/s)BD:0 AB  G/s)BD:0 BC+(315)CE:0
lJ  CD AD r DE
235
A / 5 t CE : 0 (3l5tBD:0
CD+ (415)BD:O DE  (3/s)CE :0 E \ . 1( 4 l s ) C E  0
9.1(t A pentodiagonal witlr a bandwidth system of flve can generally beexprcssed as
P9.r4
pump deliversa unit llow (Q, ) of a highly A peristaltic 9.11 viscous tluid.The network is depictedin Fig. P9.14. Every section hasthe samelength and dianreter. 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  2Qr:0 Qst2Qo  2Q(,:0 3Q.
.ft ,9r hr e2 .fz 8z h2
dt e1 h Br ht
d,,
t
€tt I d,
Jrt €rt
8n .f,,
I
,{l
.rl
rl r2 r3
0,=0,+O,
Qt= Qti Qs
fttl
Qs=Qo* Q,
15A trussis loaded as shown in Fig P9.15. Using the ine setof equations,solve for the l0 unknowns. AB. BD.CD. DE, CE, A,. A,, and E, ,AD.
xrt
Developan Mfile to etficientlv solvesuchsystems withor,rt pivotingin a similar fashionto the algorithmusedfbr tridiagonal matrices in Sec.9.4.1.Tcst it fbr the following case:
24 kN
lo Lo
t_" t_;
2
l 9 4 3 al 1 ,1 0 7
0 I
lf
3
:ltii til
P9.r5
. of linear s describedin Chap. . 9.. usedto evaluaterighthandside when MATLAB's backslash in generalterms what happens Understanding is usedto solvelinearsystems. Knowing how to express multiple righthandside knowing how to evaluate Given an LIJ factorization. Specific objectivesand topics coveredare . we havechosento employ the terminology LU factori:ationlor with the MATLAB doculnentation. Although it certainly represents righthand'side with different but sarne coefficients with the equations when solving [A]. righthandside Gausseliminationas an LU factortzation.  [n the parlanceof numericalmethods. . operator ' . Gausselimination is designedto solve systems algebraicequations: [A]{r}: {b} (i0. constants {b}. the coetlcient matrix involves decomposing that LIJ factortzation Understanding different into two triangularmatricesthat can then be usedto efficiently evaluate vectors.1) inefficienl it becomes a soundway to solve such systems.the tenns "factorization"and "decotttposition" Tobe are synonyrnous. rizotion LU Focto OBJECTIVES CHAPTER The primary objective of this chapter is to acquaint you with LIJ factorizationt. a Recognizingthat Cholesky'smethodprovidesan efficient way to decompose canbe symmetricmatrix and that the resultingtriangularmatrix and its transpose vectorsefficiently. . vectors. consistent approach is very comntonly usedto describethe same Note that LLl tlecomposition the subiectof this chapter.
t t l I t u ] { . Bcfole shclwing how this can be done. Thus.[ b ] If this equation holds.OVERVIEW OF [U FACTORIZAIION Recallthat Gausseliminationinvolvestwo steps: fbrwardeliminationand back substitution(Fig. The results can be directlyextencled to irdimensional systems.the forwaldelimination stepcompriscs the bulk of the conrputational etlbrt. I O. Eq. the fbllowing explanation is limited to a setof threesimultaneous equations. Interestingly.f :1 t_ d1 dt dz t 0. to simplifythe followingdescription. pivotingto avoid LU factorization requires divisionby zero.2)is the resulr.{ b }: 0 (10. elimination itselfcanbe cxpressed Gauss as an LU facrorizttlion.r': Lltt l/ rr I I 0 .ation methodsseparate the timeconsuming elimination of the matrix [A] from the rnanipulations of the righthandside {b}.let us lirst providea mathernatical oven. That is.3).3) Recognize that this is similar to the manipLrlation that occursin the first step of Gauss elimination.This is particularly truefor largesystems of equations. l . ( 10.iewof the factorizationstrategy.it follows fiom the rulesfor matrixnultiplicationthat (10. r ) tLt li. That is.a)is premultiplied by it. Equation(10. fbr a3x3svstenr: ['l t/l Ll12 .I OVERVIEWOF LU FACTORIZATION Just as was the casewith Gausselimination.r. we will omit pivoting.8) . /r: l l that has the propertythat when Eq. r} { d }: 0 Now assume that thereis a lower diagonal natrix witl..2)could be expressed as au uppertriangr"rlar system. (10.I I can be rearranged Equation to give l A l { r } . (10.However. 7 ) ILlldj : lbl (10. ( 10.rl's on the diagonal.. (r 0 ." multiplerighthandside vectors can be evaluated in an efTicient manncr.3)can alsobe expressed in rnatrixnotation anci give rearranged 11'r t u l { . once [A] has been "factored" or "decornposed.6) lLllul: lAl and (r 0 .As we learned in Section 9.. Ir 0 0l I 0l ( r0 . 9. r} { c t l l : t A l { r } ..2.2 ) Suppose that Eq.In addition. 5 ) Ll.2. For example. elimination is used to reducethe system to upper triangular form. LU factoriz.
lul V ir) : trl) t {ri I Substitution I .l 0 ass J whiclr is in the desireduppertriangularformaf. [A] is factoredor "decomposed"into lower [L] and upper [U] triangularmatrices. Substitution step.0 .3) which can be solvedby back substitution fbr {x }.3).( 10.Recall that the forwardelimination step is intendedto reducethe original coefficientmatrix [A] to the form ot' ac 4rr l f . 7 )a . lUl:10 I t L0 .2 GAUSSETIMINATION AS [U FACTORIZATION Although it might appearat face value to be unrelatedto LU factorization._ tLl I {d}: {b} V {d} I . ( 1 0 .[t] and [U] areusedto determine a solution{x } for a righthand side of two steps.(10.I a i . which [Al into [Ll and IU]. n d( 1 0 .the resultis substituted into Eq.238 IU FACTORIZATION tAl {. A twostepstrategy(seeFig.Eq.". 8 ) : 1.. Then. This can be easilyseen is a direct product of the fbrward elimination. I o.. First. 2.r}: {ll} tut f.torirution { lul A lLl . (10. Now let us shorvhow Ganss elinrination canbe imnlemented in this wav. . LU factorizationstep. I ) for obtainingsolutionscan be basedon Eqs. This stepitselfconsists mediatevector{z/}by forward substitution.o FIGURE IO.8)is usedto gerrerate aninter{b}..I ihe steps in 1Uloctorizotion. ar. I 0. Gauss elimination can be usedto decompose for [U].
of the LU factorizationof [Al.) 0 0l 1 0l 0 0 .9)] 0il Jzt: 0)l and subtractthe result from the secondrow to eliminatea.I (r0.. in a.. {b} later.. But thereis absolutelyno reasonthat we have to perform the manipulations Thus.t. Similarly.and a3..r 3 ) a12 n'. elirnination andl...we could savethe/'s and manipulate simultaneously....fsr .. a22 ott I r r 1o. .: The first stepin Gausselimination is to multiply row I by the f'actor[recall Eq.r'? was to createzerosin at1. This can be readily illustratedfor a threeequation f a r r o t z ...a. {/lt apl If.. hand side {b}. system.. in fact..1.f:z 1I The following exampleconfirms that [A] : tLl[n.: l. This matrix.ri AS IU FACTORIZATION I O. .1l) where f lUl :10 and on L o t o.t . a \ .. Recall that the whole idea behind the Where do we store the factors/.After elimination.1 (r0.row I is multiplied by A2t  1t Ltll fiom the third row to eliminatea.r. azz and subtractthe result from the third row to eliminatea. in a.10) ) L.. Now supposethat we merely perfbrm all these manipulationson the matrix [A]. T a ' r .f..the [A] matrix can therefbrebe written as l .r2) [r 7rl:lft L .J. aj2 ar ) _ti. The final stepis to multiply and the result subtracted the modified secondrow by t.fp ai:. andf..we can storerr in a. .Thus. 7 1I3 rl I o. a\..r.... '. 1:  .if we do not want to changethe equations. ur or. (9. . represents an efficient storage lAl tLltul (10.2 GAUSSELIMINATION the matrix [L] is also producedduring the step. we alsohaveto do the sameto the rightClearly. Though it might not be as apparent..
1 0. at the end of this step.19 . ln Example 9.2 [3 I 3 0 : 1 0 7.0333333 /.033333]  0 . f u: .8) for {d}..2 I themultiplication Thisresult canbe verifiedby performing of [L][L/]to give lLltul:l}.1 0.00333 [A):[LilU):10.99996 ) 0.0271. 0 2 7 1 3 0 0 1 0 .3 After forward elimination. 0 1 2I0 0 L0 employedto obtain the uppertriangularmatrix can be assembled into a lower The f'actors triangularmatrix.3 [ 3 0.21 [ 3 0. This is done in two steps.2 theminordiscreoancies where aredueto roundoff.jeeeeee 7 L 0. The elements d21and d11were eliminatedby using the factors EXAA . Derive an LU factorizationbasedon the Gauss eliminationoerformed previously in Example9.UFACTORIZATION EXAMPLE l0.3 :0.0333333 rJ L 0.2 9.29333 tul 1 1 0 .the fbllowing uppertriangularmatrix was obtained: 0.00333 .1 0.3. 0.1 :0.0271300 uentlv Conseq .r 0. a solutioncan be generated First.I.3.29333 3 r 0  l0 7. 0.. that this merely amounts side ing the eliminationmanipulations on {bl. for a panicularrighthandAfier the matrix is decomposed. 2 r0 I 10. we used Gausselimination to solve a set of linear algebraic equations that had the following coefficientmatrix: 0. It is importantto recognize to performsolving Eq.1 1 0 .3 7 lAl:10. the lower triangularmatrix is 1 0l 0 [ r 0l tll :10. Solution. a forwardsubstitution stepis executedby side vector {b}.1000000. (10.3 1 I 0.100 Thus. 0 1 2 I0 ll L0 0 10.100000 t I I 0 otT3 tt 0. the LU factorizationis I 0.f. :.1000000 and the elementd12woSeliminatedby using the factor 0. Thus.the righthand .l lU Foctorizotion w i t h G o u s sE l i m i n o t i o n Problem Stqtement. : 7J0333 : 0.
: drf Q.85 : ) 19.r 92lJ.  Ilj l 0 t0..1 by generating the final solution with forward and back substitution. 0 2 7 1 3 0 0I J [ .0 .g): o. I [ 7 . 1 r or multiplying out the lefthandside: : d1 7.0271300d2 11. rI Lo.0 .l0. il d.Again. which can be substituted into the second equationto solve for d : .3 * 0.os+: T3 0.4 We cair solve the tirst equation for d.... Recall that the systembeingsolvedis 0.: 19.2 l l l : l . +J andthattheforwardelimination phase of conventional Gauss elimination resulted in tfxr I 02e3333 :  'i?i'tI 7oo.ibi j:l il fori : 1.4]and {}} sirnultaneously.n The second step then merely amounts to irnplementingback substitutionto solve Eq.i3 I. The fbrwardsubstitution stepcan be represented conciselyas s.. 1 0 0 0 0 0 . tl. 8 s I l0 0..5617 .0333333dr+ :19.85.3. : 7. ? 3l{:l fo L 0 . .3 d2 0. As just stated.3 1t..:fi.2.85 19.1 0.0333333(7. 7 .0lt0 lrr L0 zo.J.: r o I [ * r   t r .yr  .l2) and (9. lcompare t. Ll. .. (10.0.. .4 + d3  7.2 GAUSSELIMINATION AS TUFACTORIZATION will be in the samestatethat it would have beenhad we performedforward manipulation on [.2 EXAMPLE T h e S u b s t i t u t i oS n teps Problem Stotement' Complete the probleminitiateci in Example10.85 J l I J The forwardsubsriturion phaseis implemented by applyingEq. the intent of forward substitution is to imposethe elimination manipulations that we had formerly appliedto [A] on the righthandside vector {b}.l e .2 ( 7. Soluiion.l3)l.. ( 10.100000dr. 3 .85 0. 'fl: u11x1  j:i+1 uii .IO.(9.3). it is importantto recognizethat this is identicalto the backsubstitution phase of conventional Gausseliminarion with Eqs..
io''..ql _02llri .r canb e s o l v e d n Example9. phasetakesa the substitution little more eflbrt.02713(19.00333 0.IU]{.3).r I or Inr 1 2 .I and 10.tt) where I and u are the lower triangularand uppertriangularmatrices.r 0 .2e3333   0 10. 1a n d 1 0 .r} : {di: 0 7.1 (7.l MATTAB Function:ru MMLAB syntax: has a builtin function lu that generates theLU factorization.r*y {. (10. EXAMPLE 10./} This resultcan thenbe substituted into Eq.3 for details)for the final solution: b 1 h a c ks u b s t i t u t i o(see {d\ : I t o.0843 dt : 7 1..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 . I The LU factorizatioualgorithmrequiresthe sametotal flops as for Gausselimination. I I :.85) + 0.respectively. It has the general = lu (X) lL.242 TUFACTORIZATION Bothd. I ltt. Use MATLAB to compute LheL(l factorization and find the solutionfor the samelinear systemanalyzedin Examples10. andd. l: lro..ill.2 I xl . 2 . derived fronrthe LIJ factorizalionof the matrix x.#*vl whicl.2. The only differenceis that a little lesseftbrt is expended in the factorization phase sincethe operations are not appliedto the riglrthandside.0120 L0 ) 13 0 .Conversely.: I _o1 ro ll*. canbe substituted into thethirdequation to give : 70.3 tU Foctorizotion with MATLAB Problem Stotement. lO. : {.0.4. 8 s1 7 o.2: 0r f .5617) Thus.Note that this function usespartial pivotingt0 avoid division by zero.
1.2000 0.0000 0.0000 To generate the solution.a27I 0 0 1.0000 0.0333 0. 5.r.0000 t. iL'00 1.2.1(1 00 0 1.3000 10.0000 These results conlbrm to those obtained by hand in Example 10.2000 0. 1 . we flrst cornpute >> d = L\b d= ?.1000 1.2.3. .GAUSSELIMINATION AS IU FACTORIZATION Solution.ai2a 3.1004 0. Next.2000 0.3000 0..0033 0 This is the sameresultthat we oblainedby hand in Example 10.0000 0. the LU factorizationcan be computedwith L 1.5000 7.3.4l.0843 And then Llsethis result to compute the solution >> x = U\d 3.01r00 a 0 0. The coetlicientmatrix and the righthandside vectorcan be entered in standard fashion as >> A = t3 ...8500 19 .I >r b = 17.2933 t].3 'lI.11 70.2 I0).85.1 . C)001r' 0.0000 2. . We can testthat it is correct by computingthe original matrix as >> L*LT 3. 19.
r l._l  upltl11 forji+1. The termsof Eq.r  L ll l :6.n tlii (r0.ntion One crf the most popular approaches Choleskydecornposition). Such systems occLlrcommonly in hoth mathematical and engineering/ problemcontexts./55 1r. (alsocalle/ involves Choleskt .12372q2 .5 22s I 1 5 5 225 919) Ib I r5 Solution. l6) canbeused Eq.vmmetric nratrix is one whereeij : aji for all i and rvords. 8 tlrirta s.l4) ciln be rnultipliedout and setequalto eachother. science are availablefbr such systems. They offer computational Specialsolution techniques is advantages because only half the storageis neededand only half the computation time required for their solution.44949 55 :22.15) is ernployedtocompute u11:Jan:16:2.1833 (6.r6) EXAMPLE 10.2 : 4.244 IU FACTORIZATION FACTORIZATION I O.rrl. Inother Recall from Chap.(10. : .factoriz....5 s5 I 5. ( 10...u) That is.123124 2.[Al : IA)r.4 CholeskyFoctorizotion Problem Stotement.For the lth row: (l0. .44949 l5 Forthe row (l : 2): second : . as in l A l :l u fw l tr o. This algorithmis based on the fact thata syrnmc'tric matrix can be decomposed. Forthe firstrow(i : l).Compute theCholesky factorization for thesymmetric matrix I A t l?rt 1.f a.nine otz q tt  EXAM Llll at3 .45366 2. the resultingtriirneular factorsare the transpose of eachother.44949 Then.rt 1l ai1 ' 1. to deten. Eq.(10. Thefactotization can be generatted efTiciently by recurrence relations.3 CHOTESKY j.
an intermediate is by solving created {d} IUlr {d} : {b} Then. : @ : 6 .I 8 3 3 F o r t h e t h i r dr o w ( i : 3 ) : u . This is leflforan exercise.. i t * u l . . 231 :10. It hasthe MATLAB has a builtin function chol that generates generalsyntax. r : . the Choleskyfactorizationyields r 1 JJq4q t6 I 11711 11 457661  tul :  L 4. The following exampleshowshow it can be employedto generate both the factorizationand a solutionfor the samematrix that previous example. fo .6.5 Cholesky Foctorizotionwith MATLAB factorization for the same the Cholesky Problem Stotement.9165 4 .the final solutioncan be obtained by solving (10.I l 0 l 0 l I into The validity ofthis factorization canbe verified by substituting it and its transpose the originalmatrix [Al.17) l u l { x }: l d } lO.3.l MATTAB Funcfion: cuot (r0. Use MATLAB to compute matrix we analyzed in Example10. t/ .15366) 225.4. First. Eq.. [6 t A l: l l s 155 ls ssl ss 22sl 2 2 5 9 1 9J Also obtain a solutionfor a righthandside vector that is the sum of the rows of [A].l4) to seeif theirproductyields After obtaining the factorization. 1 l o l o l Thus. we looked at in the EXAMPLE I O. 9 1 6 5 6 . (10.1833 2 0 . .chol (x) where y is an uppertriangularmatrix so that t/' * t/ = r.r8) the Choleskyfactorization.FACTORIZATION I 0.3 CHOLESKY 423 Ul2ut3 Ll22 245 24(22.it can be used to determinea solution fbr a rightvector handside vector {b} in a mannersimilar to LU factorization. Note that for this case.the answerwill be a vector of ones.
0000 1.. 15 55 225.0000 225.1101 And then use this result ti'r compute the solution >>x=A\y l0.0000 Then. r23t 4.mul minethat I 10.1101 We can test that this is correct by computing the original matrix as >> U' *IJ 6. we flrst conrpute >> d = A'\b d= 3r. :'> A = The matrix is entered in standard fashion as f 6 l5 55.:)) .l Dete of equatio tion.1813 0 22 .0000 55.91{.r1 3xr xrj r.3 thesystem lblr : I O.31 2[).0000 979.0000 To generate the solution.^:^ti ^^ ^f :*^^^ri.10.45.3 Use system ac 10.Now we have some backgroundon matrix solution techniques.0000 55.0000 55.2 Use Eqs( .. the Cholesky tactorization can be computed with >> U = chol(A) u= 2 .5 6.0000 225. we can provide a simplifid t^.0000 15.0000 15. 10.:)ll 16 295 7259 Next.. 55 225 919). :))/' sum\tt(2.0998 6. 10.246 TUFACTORIZATION Solution. and( versionof 10.0269 25.5Solv factorizat 2xr 3xr  . 10. sum(A(3.4Use Prob.4495 0 0 6 .4 MATTABIEFTDIVISION We previouslyintroducedleft division without any explanation thu of how it works. A righthandside vector that is the sum of the rows of [A] can be generatedas >> b = fsum(A(1.0000 1.
and Choleskyfactorization.MATLAB invokes a When we implement left division with the backslash MATLAB examinesthe algorithm to obtain a solution.Conlirm that your f'unctionis working properlyby verifying that [L][Ul : tA] and by using the builrin function 1u. dethe squarematrix and returns velop a function that is passed the triangular matrices [Ll and [U].PROBLEMS 247 operator.a leastsquares r i o n. )O .r2*7x3 : 34 Rr.a simplified overview can be outlined.7 Confirm the validity of the Cholesky factorizationof into Eq. (c) Employ the resultsof the factorizationIL4 to vector.8)follow from Eq. MAILAB checks to see whether [A] is in a format where a solution can be obtained without full Gausselimination.tl (a) Perform a Choleskyfactorizationof the fbllowing b1 hand: system symmetric 27 l2rz.13:38 3.r1 . 10. R a c t o i c allecJ zat Q . ward substitution. highly sophisticated structureof the coefficient matrix and then implementsan optimal method to obtain the solution.xt: .Although the detailsof the algorithmarebeyondour scope. : . 2 It should be noted that in the event that solutionis obtainedwith an approach [Al is not square.6 Developyour own Mfile to determine tion of a squarematrix without pafiial pivoting.4by substituting and l Ll yieldslA l.(b) triangutar(or easily transformed that are techniques the solution is obtainedwith the efTicient of thesecasesare detected. That is. 5 multiplythe resulting [L] and [{l matricesto deterthat [A] is produced.6). ln essence.'a general triangular factorizationis computed by Gausselimination with partial pivoting and the solution obtainedwith substitution. First. verify that the productof l L/17 l0. Usenaive Gauss elimination to factor the following to the descriptionin Section 1 0 . include bandedsolvers. 2 : according rheLU factoiza10. If none of thesesimplified solutionsare possibleand the matrix is square. determinethe solutionfor the righthandside 10. 10.6rz + 2x'.l Determine equations n for the (a) factorization. develop a function that is passedthe symmetric matrix and returnsthe matrix [tIl. (10. or (c) symmetric. Showall the stepsin the computation.7) and(10. svstem for an alternativerishthandside vector }:t*l tl ilill{i: (b) Verify your hand calculation with the builtin cnol function. These include systemsthat are (a) sparseand into triangularform).14)to the results Example10.8 and use the builrin function is working Ibf = Lt2 18 . in to solvethe systemof equations lU factorization Use Also solve 10.5 I x z + 5 a : .6) Solvethe following system of equations using LU with partial pivoting: 2x16x2.: 61 .3. Test your function by using it to solve the systemin Prob.9 Develop your own Mfile to determinethe Cholesky factorizationof a symmetric matrix without pivoting. 10.If any banded.(b) forward substitu(c)back substitution phases of the lU factorization and of Gauss elimination Usethe rules of matrix multiplication to prove that (10.back and forSome of the techniques availablefbr such systems.f PROBTEMS the total flops as a function of the number l0. L )v. That is. (10. Test your function by using it to solve the systemin Prob.3.2 1.
(c) Repeat(a) and (b) using MAILAB.0 .2 6 44 LA): W)rlul: [2 lr Lo r 1 0l 2 . 16 ) ? L.tl 2) 1().15) and 0 0 ..14 Computethe Choleskyfactorization of 10.26). lAl:tLlfUl: [1  0.2 t 2 * 5 .2 x 2 * .rt]_6xz4x7: .248 IU FACTORIZATION factorizationto determineIU] sothat 10.1 0 Z. 1 3 : . r r : . 10.13UseCholesky 10.12 Use the following LU factorizalion to (a) compute tl.j (b) Employ the result of (a) to compute the determinant. (10.3333 . 3 6 3 6 .U (a) Determine the LU factorizationwithout pivoting by hand for the fbllowing matrix and check your resultsby validating that [L][U] : [A.3333 ^.10 Solve the following set of equationswith LU factorization: 3 x 1.lrr*l 3. IAl:10 o ol [e 2 .x 1.6667 I I L 0.6364) L .redeterminant and (b) solve [Al{r} : {b} with {b]r: Lo o 4l 0l Do your results make sensein terms of Eqs. ] ' [3 2 1.s 1l [8 2 7 2l l3 L2 r e_.r0 44 .
a methodto assess a matrix solution'ssensitivity to roundofTerror is described. Understanding the meaningof matrix andvectornormsandhow they arecomputed. In addition.2). Specificobjectivesand topicscoveredare o r r o r Knowing how to determinethe matrix inversein an efficient mannerbasedon lU factorization. t.1) Now we will focus on how the inversecan be computednumerically.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. Knowing how to use norms to computethe matrix conditionnumber.l Colculoting the Inverse The inversecan be computedin a columnbycolumn fashionby generating solutionswith unit vectorsas the righthandside constants. Understanding how the matrix inversecan be usedto assess stimulusresponse characteristics of engineering systems.r t A l (r1. I l. For example. called the inverseof [A].': [ A ] . thereis anothermatrix [A]r .1. for which : t/l t A l t A l .if the righthandside constant . how the magnitudeof the conditionnumbercan be usedto Understanding estimatethe precisionof solutionsof linear algebraicequations.l THEMATRIXINVERSE In our discussionof matrix operations(Section8. Then we will explore how it can be usedfor engineering analysis. we introducedthe notion that if a matrix [A] is square.l.
t h\ t.r 0.3]): [r*0. 2 e 3 3 3l.ui] {l}:{i} [: l0 l[.11249 which is the first column of the u.l1 . [0. Thisvector and solvedwith forward substitr.or2 o I[ '.[10.o .Similarly.'t s) f0l l} {il.03333 0.rtion for {d}r : ll can then be used as the righthandside of the upper triangularsystem(recallEq.if a unitvector with a I at the second row'is used [b]:{ tol columnof the matrix inverse.0.1 Recall that the lactorizationresultedin the following lower and uppertriangularmatrices: 0.01008j which can be solvedby back substitution for {.3 1 10. the resultwill be the second The bestway to implementsucha calculationis with LU factorization. the lower triangularsystemcan be setup as (recallEq.00333 tul:10 1 1 0 .1.250 MATRIX INVERSE AND CONDITION has a I in the tirst positionand zeroselsewhere.J or 02 [ r 0.0333333 0. 0 1 2_ 0j 0 L0 [r o. it is ideal fbr evaluating the multiple unitvecton needed to computethe inverse.2 10 _.100000 t I o o'l by perfbrmingthe Solution.. Recallthat one of the greatstrengths of LU factorization is that it providesa very efficientmeans to evaluate multiple righrhandsidevectors.027130 0 lJ L 0. o o s r so o l I nntooR n n I . Employ LU factorizationto determinethe matrix inverse for the from Exaurple10.:l:l o ro.8]) 0.] 7 .zzz+s l A l ' : l .1} the resultingsolutionwill be the first column of the matrix inverse. Thus.293333 7.Thus.ratrix inverse: o ol f o.10091.r}r : 10. The tirst column of the matrix inversecan be determined forwardsubstitution solution procedurewith a unit vector (with I in the first row)asthe righthandside vector.3 0.1 7 _0.00518 0. 0 0 3 3 .10 . ILl EXAMPLE MqtrixInversion Problem Stotement.2 I 1 0l ttl:10.l: system or o)l I l t""l Al:10.133 13 I lo [orooeJ 0..
010080.8) is formulatedas [ro l  0. therefore. which is thefinal columnof thematrixinverse: {r }7 : 10. which is the column of the matrix second 10.thesesystemsare linear and. For a force balanceon a structure.electrostatic propertiesmight be horizontalor vertical components of the forcesacting on eachnode of the structure. Eq. the sameprocedures can be implemented . potentialis conserved. heat.In either case. chemicalprocess.0021 11. force.027r 300 'lllil :{:} This can be solvedfor irl). the terms of Eq.00s18 0.099881 [A]' :  0. .the propertiesmight be the mass in each reactor of a would yield similar examples. In many problems.Theseequations are interrelated.in that eachequationmay include one or more of the variables from the other equations.0021100.the elementsof {x} are the levels of the property being balancedfor they represent the horizontaland eachpart of the system. fbr balanceequations. (10.01008 0.resultingin a set of equationsdefining the behavior of the property for the entire system.For a mass balance. For example.zzz+e o.oo4s44 f 0.0333333 L 0.142e03 0 with {b}/: l0 0 1l to solve fbr Finally.4) have a definite physical interpretation.For the massbalance.they represent the system's determine. ( I 1.1429030. vector {b} containsthose elementsof the balancethat are indeThe righthandside pendentof behavior of the systemthat is.006798l that[A][A] ' : Ul.3)to determine {r}' : inverse: 0.For many cases.1.they represent the or externalstimuli that drive the system. or coupled.006798 0. they arethe massof chemicalin each vertical forcesin eachmember.004183  L 0. and the resultsare usedwith Eq.0027r0 ol o. stateor response.In a fbrce balanceof a structure. many of the linear systems of equations arisingin engineering laws. which we are trying to reactor.142903 0. they are constants.the momentum.t.0998801 t 03324so.004l83 0. Thevalidity of thisresult canbechecked by verifying  1.00sr8 0. Other fields of engineering and science A singlebalanceequationcan be written for eachpart of the system.oo4s44 0.2 StimulusResponse Computotions and As discussed in PT 3.100000 0. The mathematical expressionof these laws scienceare derived from conservation is some form of balanceequationto ensurethat a particularpropertymass.4) Now.of the exactform dealt with in this chapter: [A]{x}: {b} (11. (10.THEMATRIX INVERSE To determinethe secondcolumn.004944 [Al ':   0_j L 0.
lr.illycontains the . At the beginning of chap. Consequently. ( I I .'. eachof its elementsrepresents the response of asingle part of the system to a unit stimulus of any otherpart of the system. r. there are a variety of ways to solveEq. Even for sntal. Usine the exanrple of the structure. As such.etiil ss8.toftheinvertedmatrixrepresentsthevalueofx. the coefficientour 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 eu oe f . we set up a probleminvolvingrhree individualssuspended vertically connected by bungeecords. D.l systems..rltositirn meansthat if a systemis subjectto severaldifTerent stimuli b's).6 I In Example 8.r1 l.The fbrmal solution can be expressed as h : l: [ A l .asidetiom providing a solution.rso r00 rsO s0 50 s0 I  I. {l{.r1.cluetoaunitquantityofb. f rlt l /)r I (l)t l)l f l.. Eq. f rt.I _ . 8.4). Notice that theselbrmulationsare Iinearand.xrrameter. the nratrix of coeflicients [lJ usui.2) L t. which are retlected in the coefficients no' trnda.I.We deriveda s)'stem of linear algebraicequations basecl on fbrce balances tilr eachjuniper. However. P roportionalin' meansthat multiplying the stimuli by a quantityresults re.4) miqht h reexpressed as :s e IInteractionsl{respon {} stimuli} As we know from plevious chapters. theretbre.252 INVERSE MATRIX AND CONDITION Finally.tr a 1u n i t l e v e l o f l r s . elementrr. the responses individuallyand the resultssummed can be corrrputed to obtainatotal response. in the sponse to thosestimuli beingmultipliedby the sarne quantity.(11.using the matrix inverseyielclsatparticularlyinteresting result. EXAMPL I. such behavior of individualstimulusresponse interactions would not be intuitivelyobvious. useMATLAB to compute the matrixinverse and . rr. lrl + L (lrt /tt lr l/rl l)t + Thus. superposition andproportion(the ality hold. l.\'t : ..r thatexpres how the parts of fhe systernirte ract or are coupled.l' of the matrix inversewouldrepresent the fbrce in member i due to a unit externalforce at node1. \ ' . In the present example.  L 0 roo o I I .I . That is. we used MATLAB to solve this system for the vertical positionsof the jumpers(ther's). T h i s r e s u l t r s o independentof the eftects of b2 and 6: on . Therefore. respectively. ThLrs. has extrernelyuseful properties.2 IE 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. we lind that the invertedmatrix itself'. r: ]{1r} or (r'ecalling our detlnitionof ntatrixmultiplication from Section 8. we can draw the general conclusion thattheelement a.\'J : ..2.the matrix inverseprovidesa powerful technique 1brunderstanding the interrelationshipsof component partsof complicated systems. Supe... L {lt I /)l f {/l.
r A F r : 0 .0200 0. 50.O 50 501. but the second of I N to the second and third by 0. the inversealsoprovidesa meansto discernwhether systemsare illconditioned.0200 0.the third column (. Superposition and proportionalitycan be demonstrated by using the inverseto determine how much fartherthe third jumper would move downwardif additionalforcesof 10. second.j : / .0200 0.now because when the force was appliedto the second of the additional elongationof the third cord. This can be done simply by using the appropriate elements of the third row of the inverseto compute. 3 r r A F r+ k r j a f ' 2 + k . However.05(20) II. k. ln contrast.02m elongationof the first jumper makes sensebecause the first cord is subjectto an extra I N regardless of whetherthe force is appliedto the first or secondjumper. Start up MAILAB and enterthe coefficienrmatrix: >> K t150 100 0.the third jumper showsthe identicaltranslationas the second jumper as thereis no additionalforce on the third cord that connects them. However. The inversecan then be computedas >> KI Kf = = inv(K) 0.2 ERROR ANALYSIS AND SYSTEM CONDITION Aside tiom its engineering and scientificapplications.02 m if the force on the first jumper was increased by 1 N. because the additionalforce would only elongatethe first cord by that amount.0300 0.03 m because along with the first cord.0300 0.I I .j:3) indicatesthat applying a force of I N to the jumpers moving the samedistances third jumper resultsin the first and second as occured jumper. First. observethat the numbersin the first column ("1: l) indicatethat the positionof all threejumpers would increaseby 0.J m + 0. and third jumpers.the numbersin the secondcolumn (j :2) indicatethat applying a force jumper would move the first jumper down by 0. As expected. This makessense. Invert the scaledmatrix and if there are elementsof [A]  that are severalordersof magniq\/{fcm . for the secondjumper the elongationis now 0.Three direct methodscan be devisedfor this purpose: l. The 0. 2 E R R OA RN A L Y S IA SN D S Y S T E M CONDITION 253 Solution.0300 0.0200 0.0500 Each elementof the inverse. the secondcord alsoelongates due to the additionalforce. 0 2 ( 1 0 ) :2.03 m. the third jumper is moved fartherdownward.respectively.0200 0. A r . and 20 N were appliedto the first.03(50) + 0. Scalethe matrix of coefficients[A] so that the largestelementin eachrow is l.. 100 150 50.And of course. I of the invertedmatrix represents the vertical change jumper (in in position meters)of I due to a unit changein fbrce (in Newtons) applied to Jumper/.02 m.
c)can besimpty computedas lFll. 3. it againindicatesthat the systemis illconditioned. The lengthof this vectorthat is.  1. indicatet that this length is referred to as the Euclideannorn l"r1 x2 j. it indicatesillconditioning.l : would be computedas llXll": if \ t. Multiply the inverseby the original coefficientmatrix and assess whethertheresultis closeto the identity matrix. for an ndimensional vector lX.rn norm Similarly. b.1 Vector qnd Motrix Norms Anonn is a realvaluedfunction that providesa measure of the size or "length"of multi. the distancefrom the coordinate(0.2. i:r i FIGURE I I. and c are the distances along the r. a Euclidean ... : {/o\ h.it would be pret'erable to obtail a singlenumberthat could serveas an indicatorof the problem.and z axes. Although thesemethodscan indicateillconditioning. Invert the invertedmatrix and assess whetherthe resultis sufficientlyclose to the orig. + f of Ln. A simple exampleis a vectorin threedimensional (Fig. If not.254 MATRIX INVERSE AND CONDITION 2.respectively. 0. inal coefficientmatrix.I Grophicol depictton of o vector in Euclideon spoce .l) thatcan Euclideanspace be represented as lFl:la b cl where c.Attemptsto formulate such a matrix condition numberare basedon the mathematical conceDt of the norm. 1. b. If not. where the nomenclaturell F ll. l1. componentmathenaticalentitiessuchas vectorsand matrices. 0) to (a.
we can use it to define C o n d l A l: l l A l l ' l l l .max )...5). resultine in a unifbrmmatrix or rowsumnorm: l l A l l . A similar determinationcan be made for the rows. l.7l 1:1 It should be noted that.For vectors.ril whichdefines the normastheelement with thelargest etbsolute vallle..llX ll. 11. it provides a singlevalueto quantifythe "size" of [A]. a summationof the absolutevaluesof the coetlicientsis performedfor eachcolumn.thereale alternatives calledp norms that can be represented generallyby / . It slrouldbe notedthat therearealternatives to the EuclideanandFrobeniusnorms. \ll l iX lt : t/ 'tl tl l L ' 'l.' l l . norms canbe developed for matrices. as in ll. providesthe tightestlreAsureof size (Ortega... is the mirrimum nonl and. For example.l ".ERROR ANALYSIS AND SYSTEM CONDITION The conceptcan be extendedfurther to a matrix [A]. llXll: max. ( t I ."* is the largest eigenvalue of [A]r[A].r i=  whichrepresents thenormasthesumof theabsolute values of theelements.au ^f tan  l. and the largestof thesesummations is taken as the norm.r.2.\t \. : . As with the other vector norms.1972).i '.. In AppendixA.therefore. For the time being. Usinga similarapproach.1 l . areidentical for vectors. 1 u il That is.\/J /rt "? I (il. . Otherimportant examples are (p : 1) rYr . or spectralnorm.Whereasthe Frobeniusnorm llAllf can be easily deterrnined by Eq..the important point is that the llAll2.the matrix 2 norm llA llz is calculated as l lA l l : : ( / r n ' " * ) ' ' " where4. 1/'l I / We can seethat the Euclidean norm and the 2 norm.s) which is given a specialnamethe Frobenius notm.\r.the 2 norm and the Frobeniusnorm for a matrix are not the same.2 Motrix ConditionNumber Now that we have introducedthe conceptof the norm. Another is the maximummagnitude or uniformvector norm(p : oo)... llAll r : . we will learnmore about eigenvalues. This is called the columnsum .{l tinll/  \. in contrastto vectors.tot'tn.l.
2.the matrix canbe normalized rAl: fr 1  Ll il iJ 31 +i Summing each of the rows gives L833.35 The inverseof the scaledmatrix can be computedas lAll:ltr' r )b bul  _ . t. The Hilbert matrix.which is notoriously sentedgenerallyas [r l.ll^Ail = condlAl il"il Mil That is.1989)that l^xll . the solution[X] may be valid to only r . the relative error of the norm of the computedsolutioncan be as large astherelative error of the norm of the coefficients of [A] multiplied by the conditionnumber. Thus. This is also reflected in its rowsum norrn.l I 1 'i l I ltl tJ .INVERSE MATRIX AND CONDITION where Cond[A] is called the nntix conditiort number Note that for a matrix [A].if the coefficientsof [A] are known to tdigit precision(i.1 t A' l : l l{t tl lr i i' L3 I I ll EXAM I .+r I I I l : : : : l1 ll Lfl nrl Use the rowsurnnorm to estimate the matrix conditionnumberfor the 3 x 3 Hilbertmatrix. the third row hasthe Iargest surnand therowsumnorm is ll. . It can be shown (Ralstonand Rabinowitz.e.3 M o i r i x C o n d i t i o nE v o l u o t i o n illconditioned. e0 30 60 l L I g 18 10l l Note that the elementsof this natrix are larger than the original matrix. canberepreProblem Stotement.+.4ll:l+. Forex0n arnple..roundingerrorsare the order of 10r) and Cond[Al : 10'.:2.35. \ rI I I 5l so thatthe maximumelement in each rowis1: Solution. and 2.which is comoutedas ..1978: Gerald and Wheatlev.cdigits (roundingelrorsry l0' ' ).  + 4 i nll . this numberwill be greaterthan or equalto l.. tl l  I I  . l .1667. EXAMPLE I 1. First.
the rowsum norm and conditionnumbercan be computedas > > n o r m (A . However. i n f ) 2. I 1. digits Hence.3 Norms ond Condition Number in MATLAB MATLAB has builtin functionsto computeboth norms and conditionnumbers: >> norm (X.enterthe matrix: >> A l\ \/2 I/3.35(192\: 451._ cond{X. where x is the vector or matrix and p designates to inf . (b) Also compute (p : 2)conditionnumbers.4 MotrixCondition with MATLAB Evoluotion EXAMPTE Problem Siotement. first computethe rowsum versions(p : inf ). rounding could exhibit of the solution :2.rl I 4 tl sl (c) As in Example I 1. Notethatthe cond functionis equivalent >> norm(X.L 3/4 3/51.A]: 2.the last threesignificant 451.p) the type of norm or condition number(1 .2 ERROR as Thus.  1.2.p) * norm(inv(X) . Use MAILAB to evaluateboth the norms and condition numbers in Example 11. they errors.P) setto 2' it is automatically Also.log quantified by calculating be can The extentofthe illconditioning is illconditioned. (p :' fro' ) andthe spectral the Frobenius Solution: (a) First.AND SYSTEM CONDITION ANALYSIS I 1.65. or ' f ro'). p) ano .3: for the scaledHilbert matrix previously analyzed [' tAr:lr I +  lr L' t_l l^ l i2 r .notethatif p is omitted.3504 > > c o n d (A .2 257 that the system The fact that the condition number is much greaterthan unity suggests c .2 always overpredict the actual error. Then.3. Note that such estimatesalnrost are usefulin alertingyou to the possibilitythat roundoff elrors may be significant. z . the condition numbercan be calculated Cond[.I 2/3 Ii2. r n f ) .
a truckstop restaurantlocatedadjacent the ventilation system for Bubba's Gas to an eightlane freeway.3503 "'i "i.. indoor air pollution deals with air contamination Bockground. offices.0866 >> cond (A) 366.Supposethat you are studying 'N Guzzle. Theonewoy volumetric oirflows. and work areas. respectively. 2 view of roomsin o restouront. Thesmoker ond grillloods odd corbon monoxide moss fo thesystem butnegllgible oirflow. F I G U R EI I .258 MATRIX INVERSE AND CONDITION These results con'espond to those that were calculated by hand in Example 11. I L2. rooms I and 2 gain carbon monoxide from air intakes that unfortunately arepositioned alongside the freeway. ' fro' ) 368. As depicted in Fig. Room I and section 3 have sourcesof carbon monoxide from smokers and a faulty grill. 0. the restaurantserving areaconsistsof two rooms for smoken and kids and one elongatedroom. in enclosed spacessuch as homes. In addition.'j\'..= 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 section) > 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. Overheod orrowsrepresent whereos iheMowoyorrows represent diffusive mixing.1 Il.3. As the name implies.
cz'l E2a@a .Q.00010728 0.0062069 0. Steadystate mass balances can be written for eachroom.ct.c:) * Ey(!+ .c2) 0 : Qt.0034483 0. . and (3) the intakevents.0049655 n nnt9) .Qoct . 0 >> A L=f nV (A. For example.r]{.c+) * Ezq(cz yields the final systemof equation: Substitutingthe parameters i.00096552 . i .cs) . 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).6 0.j lT  0. Finally.Qa)c'+ 0 : wern * Qnct I EnGt . In addition.l .* QncoQocr *f13(cac1) (Load) *(lnflow) (Outflow)f (Mixing) Similar balances can be written for the other rooms: .. For example.determinewhat percentof the calbon monoxidein the kids' sectionis (2) the grill. balancefor the smoking section(room l) is 0:lVrmore.0034483 0.432 6e005 0. we can compute the inverse.the Solution. compute the improvementin the kids' sectionconcentration the carbon if monoxide load is decreased by banning smoking and fixing the grill. analyzehow the concentration in the kids' area would changeif a screenis constructed 2 and 4 is so that the mixing between areas decreased to 5 mj/hr. continued l .!i. First.000r3193 n nnn 6aaG6 0. Write steadystate mass balancesfor each room and solve the resulting linear algebraic equationsfor the concentration of carbonmonoxide in eachroom.0449962 0.i }:'tl l:t* MATLAB can be used to generate the solution. + (Q' . .CASESTUDY trli':'l).0049555 ^ nAtQ)aC I.0034483 0. ..00055L12 0. due to (l) the smokers.In addition.c+)' Qoc+ 0 : Qoc3* Ez+Gt. generate the matrix inverse and use it to analyze how the various sourcesaffect the kids' room.
I I l0 3 6.irrrakes : 1.93Vo The grill: : 6.0996 1) ?/q.smokers 1000) : 3.t: The intakes: : arl Q. ..897 12.891 cz. L6.. . but it also receives the effluent from room l.897 16... : '"snrr ' 12.4483 : 7o.0034483(1000) + 0. $ { continued .i.15..8966: _10. These results take place because(a) carbon monoxide is conservative and (b) the only air exhaustsare out of sections2 and 4 (Q.0034483(2000) _ 3 . the elementsof the matrix inverse can be used to determinethe percentof the carbonmonoxide in the kids' sectiondue to eachsource: The smokers: : uitr Wr*ok* : 0.877o Check useaI . >> c=Af *b c= 8.345 il. Although the foregoing is interesting.? 1L345 x l00vo : 2'7. superpositionholds and the results can be determined individually and summed: Lc2: a.37931 * 0.345 The faulty grill is clearly the most significant source. and 0a).0034483( c2.snl: arytWsrin: 0.. + aur LW*lr:0.tt Lw. Room 3 is sobad becausenot only does it get the load from the faulty grill.'. the real power of linear systems comesfron using the elementsof the matrix inverse to understandhow the parts of the systeminteract. . 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.0034483(2000) Vo".moke.S.62069: 2 ) V r '.345 x l00%o:55.483 Thus.0034483 (200)2 + 0.. Becausethe model is linear. " .c' + arrt Qt ct..j: The solution can then be generatedas >> b=11400 r00 2000 0l .. ok".. The inverse can also be employed to determine the impact of proposedremediessuch as banning smoking and fixing the grill.: 0.. For example. d ? 4/l'9. 4 4 8 3 _ : 6.260 MATRIX INVERSE AND CONDITION '' i'' {.0:j "..0062069(50)2 c2.
t2*'7x.345 :2 mg/mt. 5 yourresults by verifyingthat [A][A]r : [1] Donot aplv0tlng stratcgy.rj : l0.I)* (1000)+AI (2. in the absence ofthe smokerand grill loads.10.the only sources are the air intakeswhich areat 2 mg/m3.rr 2'7 3r1 6.Using MATLAB.if the mixing betweenthe kids' areaand zone 4 is decreased.3 The following system of equationsis dcsignedto (the c's in g/m3) in a series of determine concentrations . : 34 Therefore. the matrix is changed L 2s 0 Cl f225 0 lt r 0 s l J r s s 0 0  22s 0 21s s0 I I  5 250 0 2s5Jt C4 : l {*}*l The resultsfor this caseinvolve a new solution.345 Implementingboth remedieswould reducethe concentration by 10.sefolt hw e ingsystem: ll.345 .rz.xr:38 3r1 .rl PROBTEMS ii i li continued Note that the samecomputation would be made in MATLAB as >> AI \2. Because all the foregoingcalculations involved changingthe fbrcing functions. this remedy would only improve the kids' area concentration by a paltry 0.it was not necessary to recomputethe solution. r 2 * 5 x .345mg/m3. j : . the resultis Cl c2 C4 l:{r*tr1 8rr+ x22x3:/Q 2x16x2.2 1 .The result would bring the kids'room concentrationto 12. This makes because sense.12*2r::61. PROB[EMS Determinethenratrixinver. ll.5 r 1* .2 Detelnrinethematlixinversefbrthefollowingsystem lo F2.3\* (2000) 10.265 mp/m3..However.
rcspeclively? Vandcnrondcmatrix {sce Prob..252 0 0 ll. Use the matrix inverscto dcterrnine Iinearalgehraic equalions the fbrce in Its: thc thrcc nrembers(. 1 s 16 r. l l .I 1. Deterrrrine thc solutionfor this systern fbr thc case where each {b) Usc the inverscto dctermine thc solution.l3 (a) Dcternrine thc rratrix invcrseandcondition num. andc'.4 . reducedby 500 and 250 g/day. Do not nornralize the systcm: rcsulting chloriclecoltcentratious for Lakcs Powell. ll.6 Dctelmine f o r a n d 1. x z : 2 . 5 .r+ Rivcr consists concentrationin reactor .mationof the coefTicicnts for thc casewherc all thc unknownsshouldbe exactlyone.12 The l. Pl l.252 0 12. scale thc rnatrix by making the nraxirnum clementin eachrow equalto one. computethe condition L2..l4 Po unique (z Such poll f(x): z [t rAt: I e I ls l L l. r := 7 .1. i n P r o b . t3. way for r algebraic the coeff l'1.i t : * l 2 c . Onc such case is the ber fbr thc fbllowing rnatrix: Vantlcnnotttle rzalnr. where the righthandsidc vector consistsof the loadingsof l l.Mead. . .. l4 e 1 6 2 s 4 e l 3 6 (b) How much must the loading to Lake Powellbereduced I 2s 36 1e 641 firr thc chlolide concentration of Lake Havasu to be75? l16 (c) Using the columnsumnorm.377 n. l 0 . I: : 7. (a) Dctcrnrinc its ofprccision areexpected to bc lostdueto illconditioningl the lnatri.ector ofthesum..9 Bcsidesthe Hilbert matrix.solve lnust be increascd to inc[rcca l0 g/m3rise in the con.3. (c) Detcrminchorv much the ratc of rnassinpLttto reactor3 elcnient ofthe righrhandside r. slishtlv to 9..5 36 4e 61 8r .and Havasu. :[ll.How many dig. (d) How much will thc concentraticrn in reactor 3 be rc. 8. to nodc 3. : 1 2 6 9 3 c r+ l 8 c : 6r'. In otlrerwords.2and I 1. a n d .5 Dctermine the matrix invcrselbr the system and dc'scribed the lbllowing setof sirnultancous in Prob.3 c 2cl :3800 conclition nur. 12.pal'c thc resultins crrors with those cxpectcdbased onthe duced if thc rate of mass input lo reactorsI and 2 is conditionnumbcr.\inverse. c2. there arc olher matrices ll.chloride to eacho1thc fbr"rr lakesalld {rl.3'71 0 12.7 Determincthe Frobenius arrdrorvsumnorms fitr the systems in Probs. {b} consists in its low.r' t . It = 2.797 P Belbre detcrmining thc norms.l Determinelhc rni"rtrix inversefbr thc systcnrdescribcd dinrensional : 10.ll Use MATLAB to deternline thc spectfalconditionnurn. u'hich has thc fbllou.500 lb is appliecl t3.6.l nurnbcr and how many suspcctdigits would begenuCompute the condition rrumberbasccl on the rowsur. & and Fr) if thc vertical load at resu : 2000 lb and ahorizontalload node I is doubledto F1. c e n l r u t i ou nl r c a c t o r l. rl ['i r 'I ll lrl .n norrn. l l R e p e a tP r o b .1 Mass balancescan be written fbr each reservoir.9) wnere ll. rescrvoirs as shownin Fie.r'r.422 t2.l (a) Use the matrix invelse to solve tbr the concentrationsin [t e 16 2s 361 eachof thc fbur lakes. that are inherentlyillconditioned. 8 .ral and Frobenius l 5 c 1. ll.l [' 5 6l 14 Lt8eJ ( b ) R c p e a( ta ) b u tc h a n c e a.Fr.= fie ber for the fbllowinc system. (b] Usc MATLAB to cornputethc spcct. l l .nbcrs. l l A l l r . b u t f b r t h e c a s e o f a s i x . respectively.ing tbrm: :tl_ il where th.. ll.l o 0 t2.262 MATRIX INVERSE AND CONDITION (a) Dctermine the condifionnumberbased on therowsum n o r m f o r t h ec a s e w h e r e .1.owerColorado : 50. 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 t h e fr : 4.1 11. Com. Mohave. .xs : 3. and"16 :5.5 if the inflow concentrations of a series oftbur are ll. r l. changedto c1l : 20 and ci.122 0 0 0 of F3.l l A l l llAllcouplcd reactors as a function of thc antountof nrassinput tcr each reactor(the righrhand sidesin g/day): ll.l: 2 3 5 0 nutnbertbr a I0dinrcnsiontrl Hilbclt nratrix.10 Usc MATLAB to determine the spectralcondition 4. atedby solvingthis systcnr.
. + pn rx * pn Eq.I2 TheLower River Colorodo coefficientsof the fourthorderpolynomialf(r) : p6" * pzx3t pzx2+ p$ + psthat passes throughthe following five (300. (400. (Pl1. In addition.0. into and (500. 0.0.525). Use this approach for computingthe coefficients is to generate n linear ficients.1)thorderpolynomial that fits n data points.746). polynomials havethe generalform.0.PROBLEMS Upper Colorado River Lake Powell Lake Havasu FIGURE PI I.457).(250. equations that we can solve simultaneouslyfor that we want to determine the coefficients.616).l4) to yield a systemof five equations with five to solve for the coefthep's are constant coefficients.675). determineand interpret the condition number. Suppose Polynomial interpolationconsistsof determiningthe (n .Each of thesepairs can be substituted (Pl 1.0. .14) l p2x"2+ .A straightforward unknowns(thep's). points:(200.
Knowing how to assess diagonaldominanceand knowing what it means. Because part of the book dealswith a similar problemobtaining the the present valuesthat simultaneously satistya setol'equationswe might suspect that suchapproximate methodscould be useful in this context. In this chapter. Understanding how to solve systems of nonlinearequations with successive substitutionand NewtonRaphson. EXAM terative or approximatemethods provide an alternativeto the eliminationmethodi I describedto this point. If thedragonal elementsare all nonzero. l2.we will presentapproaches for solving both linear and nonlinearsimultaneous equations.'.l IINEAR SYSTEMS: GAUSSSEIDEL The GaussSeidel methodis the most commonly used iterativemethod for solving lineu algebraicequations. Such approaches are similar to the techniques we developed to I I obtain the roots of a single equationin Chaps.the first equationcan be solvedfor.andthe . Iterotive Methods CHAPTER OBJECTIVES The primary objectiveof this chapteris to acquaintyou with iterativernethods for solving simuitaneous equations. Those approaches consistedof guessinga value and then using a systematic method to obtain a refined estimate 0f the root. Specificobjectivesand topicscoveredare ' ' ' ' Understanding the differencebetweenthe GaussSeidel and Jacobimethods. the second for x2.5 and 6.. Recognizinghow relaxationcan be usedto improve the convergence of iterative methods.'" t. Assumethat we are given a setof il equations: lAl{"r} : {b} Suppose that for conciseness we limit ourselves to a 3 x 3 setof equations.r1.
x 100% rll rr Solution.1..solveeachof the equations for its unknownon the diagonal: 7.Eq.1.3 0.1a) atl .llr) btayrla:txl 1133 (12.. The process for Eq.1yl .r(0)+0. i tl D) AtqXi d)t.311 71.': ll lr.1.f1 .lxr* 7.3) By assuming that.r 1 1 1.lli' Jri r/'I e.2t2+ l0r30.85+0.616661 . < r.1 LINEAR SYSTEMS: GAUSSSEIDEL third for x.12.l GoussSeide l ethod M ProblemStqtemeni.2x2 ( Er 2 r . (E12.3 .1b)to compute'a new is repeated value for x2.lrr t 0.3x r l 0.2x3: 1.3. A simple approach is to assumethat they are all zero. (12.r2f0. Dr4ttX. which can be used to calculatea new value for r1 : bt latr.85+0.r.19.1) canbe used to compute ^t  7.l :XAMPLE l2.4. ! '10 1 : 3 .3xj: 19.0. Then we substitute this new valueof r1 alongwith the previous guess of zeroforrr into Eq.2) (E12.5 i ). Then we return to the first equationand repeatthe entire procedureuntil our solutionconverges closely enoughto the true values.0.r2 andx3 arezero.1a).1r20.! (t2. a22 (12.Thesezeroscan be substituted into Eq.I are the presentand previousiterations.rc) whereT and j .85 0.t. (12.2(0) :2.1c)to calculatea new estimate for x3. To start the solution process. First.r to yield 265 xi . Use the GaussSeidel methodto obtainthe solutionfor 3xr0.initial guesses must be made for the x's.l) (Er2. (12..4 Note that the solutionis ixlr : 3 2.Convergence can be checkedusing the criterion that for .1. t:l clrr{l .r20.
qe0ss7  N ld e''3:A'0J6c/c ' otethat'aswo F o r x 2 a n c l ' r r .00" : 2'990551 : 12. can be substituted This value. .( E 1 2 .00s61 .5(/'  2.07 (2.191524) : 7. 1 .3 0 +1 ). 3. 2 : l l 'o 8tc /o usu Eq' (12'2) as such fbrmulations the casewhen determiningroots of a singleequation. theyensun are met.Consequently.990551 3 . '. r another in the next equationto cleternrine Jttcobiiteratiwr' called An altemativeapproach.2(2.616661 ) + 0. of old of set a basis the on new . 3 ( 0 )  .it is immediately for the GaussSeiclel As eachnew r value is computecl is converging'th solution if the Thus.r(2..1'0056 7. when they Thus. methocland Jacobi iterationis depictedin betweenthe GaussSeidel The clifTerence theJacobirnethodis useful.1(2. . value. 9 9 0 s 5 )1 4 q q 6 r 5 + 70 ) .005610 l0 to compLlte is repeated For the seconditeration. ( 12. 12.266 METHODS ITERATIVE into Eq'(El2'l valueof rr : 0.0.000291 l0 couldh The method is.2(2.194524) :2. theenorFr to estitnate a means Eq.the sameprocess ^l  I 0) + 0. utilizes a somewhatdifferent u x's.convergingon the true solution.499625) :1. t h e e r r o r e s t i m a t o s i l t ' € € .2'616661 .0 .alongwith the assumed to calculate . ofconvergence. 1 Q .3(2.I .Thus.0.61666 . Although therearecefiain makesit the methodof preference' usually utilization of the bestavailableestimates . we would not applied to improve the answers. thistch latest available the using than Rather tactic. 3 a 1  1 1. appraisal ally provide a conservative that the result is kttown to at leastthe tolerancespecifiedby e'' ud method.3 ) + 0.I) to compute forthenefl retaiued are rather but used not immediately are they new valuesaregenerated.GaussSeidel! where cases Fig. . bestavailableestimates r's.2(.4.Additional iterations th know However.0. in an actualproblem. will be enrployed.1 1qL\)L by substitutingthe calculatedvaluesfor 'rt and'r: into The first iteration is completecl t) oyield E q . therefore. for x1: example.19.4 .r's set of a niqueusesEq.85+ 0.1 9 .990s57 1 r. (12'2) provides true answerapriori. iteration.3(7.
although the method may sometilneswork if Eq. a feasibleapproachto solve many problemsin engineeringand represents GaussSeidel science.c l . : (bq  a31r. will converge: II \i1t ul il lo.)faa.14)/azz . as That nonconvergent.x.tt.d: rrr a v .l Convergence ond Diogonol Dominonce of simplefixedpoint methodis similar in spirit to the technique Note that the GaussSeidel equation. : I b2 .l I (r 2 . 2 . the absolutevalue of the diagonal coefficient in each of the equationsmust be in the equation.its holds. 1 r 1 ) / .Therefore.because Although the GaussSeiclel fixedpoint iteration of for predictable than more is much converge to ability systems.n . . .atzr2 * arrx)/atr x.S E I D E L SYSTEMS I2. \ 1 \/ a . (b) @l I2.G:A U S S .ttr)f ar.I FIGURE iferotive (o)theGoussSeidel ond (b)theJocobi between of thedifference C1opn.otdepicrron o l g e b r o ie cq u o t i o n s ineor ioneous o rs o l v l n s ql m u m e t h o dfs l2. 3 ) i+i That is..r2)/ajz rr .3) is not for convergence. x.Fortunately.(b.r. .rx.ar.Such larger than the sum of the absolutevaluesof the other coelTicierrts but not necessary This cliterionis sufficient are said tobe tliugonalb dotninant.\fu' \: (bz a 3 1 x .a2trt .Recall that a single of lbr roots the I to solve 6. convergence ing and scientific problems of practical imporlance fuhlll this requirement.1 LINEAR 267 First iteration .ct.u27x.GaussSeidel condition following if the that shown lt be can nonlinearequations.r. it is designedfor linear methoclcan also diverge. (12.azrxt .r tt. the answermoved fartherand fartherfrom the correctresult.ir 1 xr: (b2 . in Section wls used iterationthat the iterationsprois. systenis That is.(bt . if the condition is satisfied. t ap:t1)/a1z . r .a..l:> ) l0. .ttyx2)/a1 Second iteration i r : ( h r .t .many englneeris guaranteed met. .l. was sometimes iteration fixedpoint simple gressed.
where ). from I to 2. ntl a. ot'errelaxcfiion. ( 12.1).t a.Thus. is called modification Tlris type of sr"rlts.1.l c l { : r } where : tfl:'.* ' U.5) a value between0 and2. : .].r. enhance iterations: average of the resultsof the previousand the present modifiedby a weiahted .l ^r^' xi.i) is equalto 0 and the resultis unmodified.2..t L f lctr onlottf ay/a2 l 0 l Eq. the addedweight of /.). this type of Hence.if I is setata If ). pltible with MATLAB's ability to ( 1 2 ..!!*uc* rl33 in matrixform as concisely canbe expressed Noticethatthesolution { : r }: { d } . let us first recastGaussSeidel This is done by expresstttg matrix pelfbrm operations. 1 ) as Eq.l'*: lr. previous reof the presentand tl. bt Cltl b2 _ a12 Atl .n. : 1. 12.re value between0 and I .0 all ll2l  4f  . successive is also called The approach vergentsystem. ov ) _ . .* . (1 . the result is a weightedaverage to make It is typically employed wtderrela.l .2 MATTAB Mfile: Gaussseidel in a form thiit is comBefore developing an algorithm. In this inthat the new value is moving in the correctdirection thereis an implicit assumption stance. to a rate.^...1} and anlal 0 [ : l a z t l t t z z 0 tcl osz a..t AT .r' ..ration. is intended but at too slow the true solution toward rnodification.i. or SOR. extra weight is placed on the presentvalue.1.. An Mfile to irnplernent  2.3 Reloxolion to methodthat is designed a slight modificationof the GaussSeidel Relaxationrepresents thatvalue is usingEq.lt" + (l ). it closer by the estimate improve of an alreadyconto accelerate the convelgence is designed which is calledot'errelaratiort. pushing to the truth..a)is listedin Fig. (12. out oscillations.rild (12. by dampening conversence systemconvergeor to hasten a ltonconvergent For valuesof . After eachnew valueof r is compr"rted convergence..268 METHODS ITERATIVE 12. is a weighting f'actorthat is assigned However.nl.':) {.
maxit=50. r .:)*x.erLd In.2. = d(i)C(i. break. es=0.del (A. Fnr i l.. .2 FIGURE Mfilefo implemenl MATLAB GoussSeidel The choice of a proper value for ).i). >= maxit.1:n) end for i = 1:n = b(i)/A(i. if the system under study is to be solved repeatedly. end if nargin<3  isempty(es).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.u '.Good examples arethe very largesystems of linear in a variety of algebraicequations that can occur when solvillg partialdifferentialequations engineering and scientificproblemcontexts. the efficiency introdrrced by a wise choiceof ). es. F t .n)iA(i.rlghL hand side vector ( d e f a u l l .b. can be extremelyimportant. 011001. is highly problenlspecific anclis often detern. . g u m e n s y o q u i r e d . 1. However. end I2. d(i) end i f ar n. l t d i .xold(i))/x(i)) * 100.nl .al. c(i.err. it is oftenunnecessary.maxtt) ? GaussSeidel: causs Seidel nethod eo x = Gaus:lSei.s((x(l) ea(i) encl end = rter+1. d if nargin<4  isernpty (maxit ) .l r p .LINEAR SYSTEMS: GAUSSSEIDEL functiolr x = GaussSeidel (A.0 if x(i) . x(i) . if m=n.si.ze(A) . b) : causs SeicleI vrithout % lnput: % A=coefficientnatrix Z b .1. error('Matrix A nust be scluare').rined For a singlesolutionof a setof equations empirically. rter if max(ea)<gS iter end . end for i = 1:n = 0. while xold F^I (1) _ x.i) = 0i x(i) end x=x'.i) C(i.n = C(1.
r l + J .n of irvo simultoneous nor.270 ITERATIVE METHODS 12. . r t ..{ . (12.r2 VeTSUS . = 5 / .3..r1graph.That is. .lineor equoiions .l). 9. . 12.r 2 . : . ) : 0 Therefbre. . which is calledsrcce. FIGURE I2. ) : 0 .r.1 Successive Substitution A simple approach fbr solving Eq. tions can be solved for one of the unknowns. \ . t 1+ J J t . r . (12. .)/ (12. Thil irpproach..the solutionis the inte$ection0f the curves. l i ( . r .2 NONTINEAR SYSTEMS The following is a setof two sinrultalleous nonlinear equations with two unknowns: EXAM "f+"'"2:lg .7) 12./:(xr. ( r t .As in Fig. r 2 . t 1. such systems of equations generally can be expressed as .3 Grcphicol depictionof the soluiic.7) is to usethe samestrategythat was employed for fixedpoint iterationand the GaussSeidel method.r:.rsive substitution. Justas we did when we determined rootsfor singlenonlinear equations. is illustrated in tlre lbllowingexample..the solutionare the valuesof the r's that make the equations equalto zero... .2. .) O J . .Theseequatiollscan then be implemenled iteratively to computenew valueswhich (hopefully) will convergeon the solutions. eachone of the nonlinear equa. these equations plot as curvesoD an .to) (r26r) In contrastto linear systemswhich plot as straightlines (recallFig.
2. :I.2.5t35 :2. Note that a correctpair of roots is. (F.1:.6a) is . ( I 2.rr : 3 Initiate the computafionwith guesses o f . f .6a)can be solvedfbr .1) can be usedto determine a new value 1 0.s ) 2 :2.rl.86051 . Eq.6).5 can be substituted into Eq.2 NONLINEAR SYSTEMS EXAMPL 1E 2. 5a n d .3i 516 Thus' the approachseemsto be diverging.37 516 429.1.915 y 3(2.3( 0. Now we will repeatthe conlputationbut with the original equations set up in a diff'erent fbrmat. Solution.I2. For example.) and Eq. Use successive substitution to determine the roots of Eq.t' : .This behavioris even more pronouncedon the seconditeration: I0 .709 rz : 57 . (12.20910 24. Equation(12.3 .5)2 : 24. (12.2)to deter_ mine a new valueof 't2: .20910)(24. the approachis deteriorating.5.6b)is Now the resultsare more satistactory: r. : yTo . t r l:  l0 .6b)can be solvedfor 12 : )/ .2) ofxl: On the basisof the initial guesses.21429)(3.an alternativesolutionof Eq.( 1 . 5 /:2. (E12. z : 3.2 27r successive substitution f o r q N o n l i n e o rs y s f e m Problem Stotement.yz: 5'7. . r 1: 1 .yi ^ l (E12. (12.12.2.21429 3.37 5 tO2 : ObvioLrsly.rl _ 2 ancl ./iblT'r2 and of Eq.12.3(2.17e4s) 5 7.21429t2 Lt  : 0.5 This resultand the initial valueof x2 ."rri xz ( Er 2 .3.J.
* ..divergencecan occur if the initial guesses are insufficiently close to the true solution..Recall that the NewtonRaphson methodwas predicated on employingthe derivative (i.ltal (t2.ttu . * ( x r .l}a) .r r.2 NewtonRophson Justas fixedpointiterationcan be usedto solve systems other of nonlinearequations.At this intercept.the root estimate corresponds and to the valuesof .d2.8) (.6. convergence on the mannerin which the equations are formulated.86051 : 3. a multivariable variable Taylor seriesmust be usedto accountfor the fact that more than one independent contributesto the determination of the root. the approachis convergingon the true valuesof "r1: 2 andxz : 3.94053 51 . (12. . ".e. For the twovariablecase.%0s) Thus. \ f ' \ x i ) (128) "f wherex. 12. the root. Eq.a firstorder Taylor seriescan be written for eachnonlinearequationas f t . f 2 .i+r \'r. The multiequationfonn is derived in an identical fashion. t 1 . f i . The previous example illustrates the most serious shortcoming of successive often depends substitutionthat is.r1 where/y . we useda graphicalderivationto computethisestimate.1 : ri  EXAA f (x') ^l \xt ) tl29t form of the NewtonRaphson which is the singleequation method.1.r axis.272 ITERATIVE METHODS tr:@:1. even in those instanceswhere convergence is possible.These criteria are so restrictivethat fixedpoint iterationhas limited utility for solving nonlinear systems.Additionally.04955 3(1. . ( . i + t: .[.) I (x2. definition Eq.*r. is the initial guessat the root andx..1H 0fu '1 . .11 is the point at which the slopeintercepts the (12. i .x .. the slope) of a function to estimateits intercept with the axis of the independent variablethat is.111eQual zero. ( 12..For this situation.An alternativeis to derive it fiom a firstorderTavlor seriesexpansion: ( x r * r ) : f ( x i ) * ( x i + r..1 0i) Justas for the singleequation version. open root location methodssuchas the NewtonRaphson methodcan be usedfor the sanepurpose. i + r Exz ( 12. In Chap.l0) canberearranged togive (12. iv t : .However. '" ?xz (xz.2.) "" dlt.lYf f z .11and .2.ri*r . reananged by equals zero and can be ) I to yield l.
5(32. the resultsare convergingto the true valuesofxl : 2 and12 : 3. OX2 t^. the computation rootsof Eq.5) ofz'o 3xi 2 : 36.i \fz.5 :  * 6xtxz:I + 6( r .{t.125 6.i _ \fi.12) to give values canbe substituted These .i  dXt n t. .5(36.t25 Thus. 5 : (l. 5+ 3 (1 .r. Cramer'srule) can be employedto solvefor t.5) ( 3.g. 5 ) ( 3 .JZ.5 : 32. method.62s 2.i " " t^.Ir NONLINEAR SYSTEMS I I to the latestguessor apwith i 's areknown (they correspond Because all valuessubscripted (1 isasetof two nd .62s(6.6).i \fzl 0x10x2 3x2'dx1 3f .: .75) : 156.i \xt \xz 3x2 \xy (12..125 ^?<_ 2.i dx2 a .5 : 2xr 3. (12. .625 ._ Jt.i 3fz. T h u s .s) 156.s) (r. t3 .s) 156. E q linear equations with two unknowns. Use the multipleequation with guesses of x1 : 1. Consequently.i 0fzl * 3fi.1. ...11) p r o x i m a t i o n ) .Xt q 3fi.5t* W # :rr : 1.5) 3xr is for thefirst iteration Thus.r " \J\.7 s) : r.i+t : tl.o 5l : 1.S)2 + 1 . 5 ( 3 .12) is the twoequationversion of the NewtonRaphson the following example.(2.03603 2..r r. The computation accuracyis obtained. 2. until an acceptable can be repeated .t n " 3f2.s(32. First computethe partial derivativesand evaluate r and y: # : 6. f z .algebraic manipulations (e.a 1 1x 2 . E X A M P1 L2 E. 5) 2 into Eq._ Jl. ]fz.5. (12.thedeterminant of theJacobian .. 3 o r o N o n l i n e q rS y s t e m N e w t o n .t _ (12.i5 : 3(3.fr.s) (36.i  \fi.it can be employediterativelyto home in on the rootsof two simultaneous equations.s) as at theinitialguesses ofthe functions canbe evaluated Thevalues 2 .84388 .12a1 x2.i+t : x2.R o p h s of n method to determine NewtonRaphson Problem Stotement.12b) The denominator of eachof theseequationsis formally referred to as the determinantof the Jacobian of the system.J).5 + xz:2(1. t h e o n l y u n k n o w n s o r e " r l .5 andxz:3. 5 ) 1 0: .Initiate of them at the initial guesses Solution.As in Equation (12. i a 1 .
t/J2 itJ r.i iJxr 3f. k:1.i+r )ra. ..1 f .i ..fi..)i Finally.just as with successive convergence subsfitution..i dxil rt : . ..i+rtermson thelefthand side.. (12.i+t dx2 +' ''f ih. OX? Dfi. thus.i ofi.i ) . r 1.rr.274 ITERATIVE METHODS When the multiequation NewtonRaphson works.. it can diverge if the initial guesses are not sufficiently close to the true roots. are knownatany generallyrepresented with iteration.r.i x n .I I ) can be written for the ftth equationas neousequations.i 3xn The initial and final valuesare expressed in vectorforrr as {x.*.2.}r: and {.l3) (i.rr.. oIz..fz. the setof equations by Eq.lrr.Consequently.J (" .*r)r:L.Whereas graphicalmethodscould be employedto derive good guesses for the singleequation case. ofzl 8r.f <JIt itf r. The twoequationNewtonRaphson approachcan be generalized to solve n simultaTo do this. t4) wherethe partial derivativesevaluated at i are written as theJacobianmatri..i 0r. xz..t. Noticethat the only unknowns in Eq..e.i }xz : 0f.1 Dxz :: 0. d. ( I 2.t::.i l.r+r . ( 12.t. the function valuesat I can be expressed as {f }r : 1. All other quantitiesare locatedat the presentvalue (i ) and.\'t.t l l + l J l { x i } '(r2.13)concisely as l J l [ r i _ r ]: ..i t "' t Jn. often the initial guesses must be obtainedon the basisof trial and error and knowledgeof the physicalsystem being modeled.rconsisting of the partial derivatives: 3ft. .x2. (12.f n.r'l.. . no such simple procedureis availablefbr the multiequationversion. Matrix notationcan be employed to express Eq.i oxil where the first subscriptk represents the equationor unknown and the second desubscript noteswhetherthe valueor function in questionis at the present value(l) or at thenextvalue (t + 1). ()X1 0Jt. .tl.it exhibitsthe samespeedy quadratic as the singleequation version.i 'l':.However..n) c o n s t i t u t ea s s e t o f l i n e a r s i m u l t a n e o ue sq u a t i o n s that canbe solved numericallyby the elimination methodselaborated in previouschapters.i d"h UI: 3f2. Eq.Although there are for obtainingacceptable someadvanced approaches first estimates.13) arethe. dxt frf I t .+ lt l .l'r.i f.
We can ilhrstrate fror.17) clearly illustratesthe parallelsbetweenthe two Clomparison is analogous equations.17) to yield the improved estimates >> x=x ./('ri ) 'f \x1l (12.l7) in an iterative until an upper (maxit) or a specified percent relativeenor (es) is reached.6250 1.3.It then calls this funcfashion.5u00 1. >> T J=[2*x(1)+x(2) x(1) . versionof the NewtonRaphson methodis Recall that the singleequation ^/+l ^/ .14)is solvedby multiplyingit by the inverse of the Jacobian.s000 ^2+x(1) *x(2) 10 . (12.nations fbr the partialderivatives basedon using finitedifference that compriseIJ].16) If Eq.1] '{. ( 12.2 NONLINEAR SYSTEMS 275 Equation(12.[.First. of the NewtonRaphson approachhave been developedto circunrventtlris dilemma. Insightinto the solutioncan be obtained by solvingEq. (12.']/139 Althoughwe couldcontinue the iterations in the command mode.0360 2.n 12. 2. the resultis {. we can implementEq.After this by using MATLAB to duplicate the calculations Exalr.5. variations Eq. limit of iterations We should note that there are two shortcomingsto the foregoing approach.11) of Eqs.l4) with nratrixinvelsion.in.this routineis passed an Mfile that computesthe fbnction valuesand the Jacobian at a given value of "r.a niceralternative is to express the algorithmas an Mfile. Such matrix calculationscan be implernentedvery efficiently in MATLAB. This processcan be repeated in a f'ashionsimilar to the iteratively to obtaiu refined estirnates twoequation casein Example12. (12. The secondshortcomingof the multiequationNewtonRaphson method is that excellent initial guesses are usuallyrequired to ensure convergence.rple we can computethe Jacobianand the function valuesas defining the initial guesses! >> x=[1.5]. (12.4.x(2) +3*x(1) *x \2)'251 I Then.I2.most alre approxir. Because these are somefimes difTicult or inconvenientfo obta.3.3"x(2)^2 I+6*x(1)*x(2) I 6 .15)is sometimes inconvenient to evaluate.lr 12.5000 32. In essence. (12.ri*r}: {t.16) and (12.14)cirn be solvedusing a technique such as Gausselimination.3. alternafiveapproaches that are slower I i . The routineiterates tion and implernents Eq. As in Figure 12. As might be expected.l\f X. theJacobian to thederilativeofa rnultir"ariate tunction.}.7500 >> f=tx(1) f2. Theretbre. 5 t 10 0 36.
x=xdx. dx=J\ f .276 ITERATIVE METHODS f uncL iorr lx. nonlinearoptimizationtechniques can be employedto obtain solutions.l.es=0. if iter>=nrax. / x ) ) . ('at least 2 input arguments required').maxit..0001.ea. . .es.r. fore. ea.. (12. end end FIGURE I2.. break.relative (Z) error = number of iterations % iter rf nargin<2.p1. ).4 MATIAB Mljleto implement Newton Rophson method fornoniineor syslems of equoiions than NewtonRaphson but which have betterconvergence behaviorhave beendeveloped One approachis to refbrrrruiate the nonlinearsysteu.maxit=50.it lea<=es. i r .l as a singlefunction: _\ LF(x) _ .'5mu t 1( f L l n c . ) 1 2 whereI (x1..p2..l l . . f i G t .end icer = 0. . while (1) IJ.x0.'...iter]=nev/tmult(func.7).. f] =func (x.. iter = irer . x z . % uses the NewtonRaphson method to find the roots of % a system of nonlinear equations ? input: % func = name of function that returns f and J Z x0 .end if nargin<3 iserLpty(es). = additional parameters used by function ? ?r output: ? x = \/ector of roots Z f = vector of frrnctions evaluated ar roors 2 ea = approxinate percent. m a x i t . .initial guess % es = desired percent relative (default = 0.erra]. . . x 0 . varargin{ : } ) ..) is the ith rnember of the originalsystem of Eq..{2. e s . iterl =ns. x=x0.. z a r a r g i n ) % newtmult: NewtonRaphson root zeroes nonlinear systems ? [x..0001?) error a maxit = maximum allowable (default = 50) iterations p1.end if nargin<4 isempty(maxit) . Thevalues of r that nlinimize this ftrnctionalso reprcserlt the solutionof the nonlinearsystem. e a = 1 0 0 * m a x( a b s ( d x . f .p2. Thfle. f.
. employ the 50. Thesevalues can be substiruted inro Eqs.x lo2.r8) (12.x2)@a.19) A+D*c At equilibrium. tt : 4_x K.22) (12.theseare two nonlinear equationswith two unknowns.\xI cc=cc.o: NewtonRaphson methodto solve theie equations.o (c". : 20.0*xL+x2) (co.0 * x:) Given the parametervalues.theycanbe characterized by Kl :3cicu Kz. .as ca:cu.21) .6: 10.0+xr*xz) * 2x1.20) (12.s ll !o4. (12. Usingrhe stoichiometry of Eqs. and x" are the number of molesof C that are produceddue to the first and second reactions.x) f 2 @ 1 .relationships as a pair of two simultaneous nonlinearequations.7 x 102 (12.co.x)2{c6. Sofution.the concenrrations of eachconstituent canbe represented in termsof r.xr) (c".2x1 . c6. and_r. the solution to this problem involves determining the roots of f1@1. formulatethe equilibrium.19).\Xz 12 (r2.t (12.lg) and(12.the following chemicalreactions takeplacein a closed sysrem: 2 A+ B > <_ C (12. = 3:7.o.. For example.25) where the subscript0 designates the initial concentration ofeach constituent.3 CASE STUDY 277 CHEMICAL REACTIONS Bockground' Nonlinearsystems of equations occur frequentlyin the characterization of chemicalreactions. and cr. respectively.rz Cd=Cd.o*tt*.c.(t2.x 2 ): 5*xr+xz (502xrx)2(20x) (502xtx)(10xz) 4x10a .26) (12.21)ro sive Kr: (c..Thus. (12.23) (12.3cac.0 .I2. If x.o: 5.2t\ where the nomenclatureci representsthe concentration of constituent i.20) and.24) (r2.3.o2x1 cb:cb.
x (2 ) ) df 2c1::2= J=fclf1dx1 df Ldx2.4 Tht termine( reactors reactor(1 3. xz + d.3 Re1 12. af f 1. system ul O. xz) fz(xt.i: (50  .' (de1*x t 1) ) .2 Us< system u x) ..Forexanple.a x x '!) ^ '2 i 120  0r1 3xt Xl (50 *  * ':<.x(2) ) c l f 2 d x 1 = ( . a : .71 The function nevrtmul t (Fig..7 8. T I to I L (b) Rep 12.fi(xt + 6x1.J\$r..t (^) ) .xz * 6x2) 3. xz) . .f ?t f unct ion f =u (x. xz) . 1 f 1=u(x(1)..x(2) +del"x(2) ) u(x(1 ) . x (2 ) +del *x (2 ) ) v (x ( f ).illr ir..ft(xt. ( x( 1 ) + d e l * x ( l .0.. a . 3xr x2)..x(l)).uz . : x z : 3 : t i a l g u e s s eo > forii.( r r( x ( I ) + c l e l * x ( I ) . y ) f = (i.lx?.223 7e 010 i Ler = 4 .lr =jf rc.0004.r66c+000 2.fi(xr..27).5 Ust and (b) v tem to a equation a.rct{x.x{lt)).26) and (12.r1.we must determinethe Jacobianby taking thepartial derivativesof Eqs.r(rlel*x(1.I2B5e01. x ( 2 ) ) . 6xr ofr : 3xz 3fz oxz .f 2l+000 f= 'l ..). ) / (del.)). evaluating the derivativesis time consuming.*x(2)).rrgin) del=0. 3fz o. the partial derivativescomprisingthe Jacobian can be evaluated as 3fr dxt x2)* fi(xr. ..278 iiir".( ' x ( L). x (2) )rr (x (1).ai. + x + .3. c l f l d x l .') I t2.rz) fz(xr + E.a. var. 12.6.1 ITERATIVE METHODS . f2='1(x(1. ) / (ciel*x(2)).. !') (5 + x + y) f . . (12.'' continued In order to use NewtonRaphson.fz\r.000001.5?7r€.r2) 3x.4)can thenbe employedto determine the rootsgiven inisf x . shorL e 12.01'1 Solve tl E s: 5 V o 12. u ( x ( 1 ) . x (2 )) dfldx2= (r (x(1) .df2cixi df2r./ funct ion fv (x.rr These relationships can then be expressedas an Mfile to compute both the function valuesand the Jacobianas f unction lJ. ) .) . An alternative is to represent them by finite diff'erences inr fashionsinilar to the approach usedlbr the modified secantmethodin Sec.3./) i (1Ll .Although this is certainly possible. x ( 2 ) ) (r.
. .r ?r '.3228 : ca lO 2.6772 PROBTEMS methodto solvethe followine I (a)UsetheGaussSeidel untilthepercentrelativeerror falls below s.2) to solve the following sysof s.3c:'r * l8r2 . Clearly stateyour convergence criteria (how you know ir is n()1 converglng).3366) : 16.)O nonlinear 12. If necessary.t r")r.2but useJacobiiteration. (b) Successive substitution using initial guesses of r : (c) puesses : r. (12.3 r 1.2 Usethe GaussSeidel method to solve the followine untilthe percentrelative error falls below e" ./) )*5xr':r2 Use the NewtonRaphson methodand ernployinitial guesses : 1. .2(3. r . the set(s)that you could not solve using an iterativemethod such as GaussSeidel. 5 .3. 12.PROBLEMS continued a solution is obtained.22) through to compute the equilibrium concanthenbe substituted of thefourconstituents: centrations ..2 z : 4 Repeat Prob. : 57o. t : l ' 7 t .3 4 Rr.)": 1.7 Determine the solution of the simultaneous equatlons l':l+x+u. of x. Thefollowing systemof equationsis designedto de(the c's in g/m3)in a series ine concentrations of coupled asa function of the amount of mass inout to each Itherighthand sidesin g/day): r'::3800 t.6772 : 40. Show using any number of iterations that is necessarythat your solution does not converge.6 x 2 1 } s l : .8 Determinethe solution of the simultaneous equatlons: ^ )) J! )il:.3366 + 2.3.r:.6cr : 1200 c2't l2ca: 2350 Ithis problem with the GaussSeidel method to Usethe GaussSeidel method (a) without relaxation (b)withrelaxation(.3366 cc : 5 + 3. of . These values After four iterations.y ::5 2:+2t3::3 : : 6 A.3366 &Dd x.r*_y*6:: B 3. : 2.6634 cn:20 . : .25) into Eq.6112: 11.3 8 .57o: Set One Set lwo Set Three 9. reanangethe t0 a tolerance to achievec'onvergence 2 " t16 .T] {i: Jrr+2. i1 I 4i.2.l 2r+ 5rI 2 y.r : _t' nonlinear 12.2.r: .6772 (12.0138 :7.r2 (a) Graphically.rr * tt*5xr:21.r*4y*5::6 6.* 3y*:: l3 .6496 ca : 50 . : 5olc: identify 12." r 3 : .6 Of the following threesetsof linearequations.5 4l 25 l0s (a) but use overrelaxation Repeat with I : 1.r+B::2 r*5.jrj: 27 3 r t.6 1 .
Qcccr+ D(cuand for the liquid as Qrctz .At steadystate..9 depicts a chemical exchange process consistingof a seriesof reactors in which a gasflowing from left to right is passed over a liquid flowing from right to left..10. Solvefor the concentrationsgiventhefollowingvalues: l. 12.9 12. L cLr< I I Cn< +' + + cu< cls< Qt l I l cL6 FIGURE P12. centered finite differences can be substituted for the second FIGURE PI2. I 0).which result in a system of linear algebraic equations.9 Figure P12.s  l l ctzl o.a massbalancefor the first reactor can be written for the sas as Qccco. respectively.C /5 L If the plate is represented by a seriesof nodes(Fig.crr) : 0 ccr):0 derivatives. andD : the gasliquidexchange rate.ITERATIVE METHODS C Tcct > ccz 2Cc: I cc.10 The steadystatedistribution of temperatureon a heated plate can be modeled by the Laplace equation: T.Use the CaussSeidelmethod to solve for the temperatures of the nodesin Fig. are the gas and liquid flow rates.Qrcu I D(ccr . 25"C 25"C Tn r2) 100'c where Q" and Q.t  F cc. Q6 :2. P I 2. Pl2.Similar balances can be written for the other reactors. The transferof a chemicalfrom the gasinto the liquid occurs at a ratethat is proportionalto the differencebetweenthe gas and liquid concentrations in each reactor. Q t: c c o : 1 0 0c . D : 0.8.IO . m:70. Tl 100"c o:*** dx' dy 75.
ffi.rnroughout irequentoccasion to estimate intermediate values fave trom such tabJes. .t u. a simplerfunction i: may be derived to fir thesevalues.. .".a curve or u .t. tJi.!! l\l ' the_ basicapproach is to fit.narureis"uudt. rn. singre curve that represents the generaltrend of the data.1..inatesfrom tables. Tri.. ri:" you rnay require a sirn. lb and e. from interest tables for *"...*'r.t & &.rr"...rh .1you other'on thebasis of rhearnount of errorassqciared .. where cise...1 oVERVIEW WhEr ls Curve Fiffing? 13 througtrto oescribetechniques to fit curves to such data to estiates.::..you will .. Dara is often given for discrere varues arong mayrequire esrimalesat pointsbetween " "11111111. .. the discrete values.. of curves rhat passdirectly through each of the points..the curve is de_ srgnedto follow the pattern of the points . One *uy to ooil.. your ftrst exposure[o curve fitting may have been to deter_ mtne lntermediatevalues from mbulated data_for instance. fc1nd. i".o.: or from steam fables for thermodynamics. Curve Fitting braic r the 4... . The esti_ mation of valuesbetweenwellknown discretepoints is called inrerpolarion(Fig. the remainder of your career. ."r. tar).ascurre trtting..n.1.."gfi."):.. Because any individualdadpoint *"V l.. . chapters "o*. s alon^g the range.i'. we make no efiort to ln^tersect every point. of inr"r"r. Rather.rluuru! wucrs the rue oaul datals is Known knownto ro be very very Dre_ pre_ .takenas a s.\.cr.. Curve Fittingond Engineering ond Science...rgrriiion (Fig."j.Both of theseapplicationsare known. pT4. t..ii.r.oure values of the : . In addirion..pT4. funct'ion at a numberof discretevalue .. lftaln ilterm"diute plified version of a complicared funcrion.::!::1'j"H'. e .t' squares .. ... such data usually orii. exhibits a significant degree of error or"scatter.. Examples are valuesForthe density of water or for the heaica_ pacity of gasesas a function of temperature.
Here. data:trend analysisand hypothesis when fitting experimental pattern of the data to make predicTrend analysis representsthe processof using the you might utilize interpoprecision. testing.If the model coefficients with measured model is compared existingmathematical . with high tions.282 4 CURVE FITTING PART f(x) (c) PT4. leastsquares with lating polynomials. r f l e o f r r t e t o oo l o n .an are data. Special in this convenient available ulated. havebeentab' and scientificproperties Although many of the widely usedengineering form. A second application of experimentalcurve fitting is hypothesistesting. through fivedoto points: curve fo fit o "best" Three ottempts io) leoslsquores i n l e r p oo i o n o n o ( c ) c .Imprecisedatais often analyzed variable.there are a greatmany more that are not your data and develop you own measure and new problemcontextsoften requirethat cases generally encountered are Two types of applications your own predictive relationships. For caseswherethe datais measured regression.I FIGURE (b)lineor regression. All fields of engineeringand scienceinvolve problems this type.. dependent values of the Trend analyslsmay be used to predict or forecast or interpolation observed data This can involve extrapolationbeyond the limits of the of within the range of the data.
l6 presents fashion.2 PARTORGANIZATION that is.culvefitting ferential equations. to approximate i ! 4. has advanrvhenthe properolder is known befbrehand. Then. imolemented in MATLAB.it nray be necessary are alreadyavailable. xtn. the alternativeclrrvetittillg techniquecaTled valuesbetween previously. is a linear functionof two or more signedtbr the casewhere the dependent exvariables This approach has specialutility for evaluating r1.On the ulrknown.sirtn cubics. The first. techniclrre. we describe ser'the validity of the lesults.ints. an alternativetechnique Finally. well suitedfbr fitting datathat is genelallysmoothbut exhibits As such. curvefitting is irnIn additionto the foregoing engineering and scientific applications. irrdependent . to databut in a piecewise lits polynomials callecl splineirrterytolation.if estimates pledictecl valuesof the nrodelwith observed valuesto testthe adequacy of ate to compare on the the model. called in equation fbr expressing thesepolynomials fbrmatsare presentcd whenthe appropriate orderof the polynopolt'nontial. hol'r'to deChap. The second. discussing terminethe "best" straightline througha setof uncertain datapoints. and visualmethods fbr the linearization of nonlinear equations. l4 ale del'otedtononlinearregression. Chup. l3 focuses on linear regre. After a brief review of statistics.Two procedure ior fitting an rrthordel points. to connect linesand parabolas of polynomialinterpolation by usingstraight basicconcept polynomill. we also present how to calculate the slopeand intercept of this straight fbr evaluating In acldition.This is followed by a clescription of multiple linear regression. precise ln Chap.or higherorder Pctlynomial dealswith developinga bc'st which is depolynomials. tical properties. eral approaches andmultiplelinearreglessiou. is preferable Newton's interpolotirrg mial is unknown.it may be appropriof the model coetTicients other hand. 12.Besides quantitativc line.Among otherthings. model.we developa generalized fbrm.2 PARI ORGANIZATION to determine valuesthat bestflt the observed data..the last sections of Chap. /5. to data. In Clnp.4. fit of a nonlinearequaticln This approachis designedto computea leastsquares interpolutionis describecl.polynomials arederived datapclints. of polynomial Chapter/4 begins with brief discussions fit clf parabolas. perimental of interest datawherethe variable are we illustrate how polynomialand multiple regression After multiple regression. portantin other nurnerical methodssuchas integrationand the approxirnate solutionof dil'Finally. intermediate interpolation is usedfbr estimating As discussed We introduce the for this purpose. Ofien. techniqtres canbe usedto derivesimplefunctions cornplicated functioDs.ssion. variable).it is particularly is interpolation with an overviewof how piecewise The chapter encls abruptlocalclranges.called the Lagrange interpolotingpoltrutmial. tages This for fitting precisedatapo. . is dependent on a nunlber of differentfactors. regres.15.this will allow of a generullinear leastsquares both subsets its general statisand discuss matrir representation ofregression a concise us to introduce Finally. alternativenodels are comparedand the "best" one is selected basisof empiricalobservations.
/j. Lineo r R"gression CHAPTER OBJECTIVES The primary objectiveof this chapteris to introduceyou to how leastsquares and data. c. Knowing how to computethe slopeand interceptof a bestfit straightline with linear regression. we assumed upward tbrce of air resistance.t : a drag coefficient (kg/m). Expressions play a critical role in their formuiaexperiments relationihipsderive in part florntheor.1) come from the lield of fluid mechanics. inawind is susperrded is depictcdin Fig. .Specificobjectives regression can be usedto fit a straightline to measured topics coveredare ' . ' . ( I 3. determination and the standard sothat to linearizenonlinearequations how to usetransformations Understanding they can be fit with linear regression.':. An irrcJiridual riorr.and u : velocity[m/si. l. the meaningof the coefficientof Knowing how to computeand understand error of the estimate. such Although suchas Eq.v. that this forcewas As a first approximation. with MATLAB. Knowing how to implementlinear regression YOU'VE GOT A PROBTEM tothe  we noted thaf a freefalling object such as a bungeejumper is subject I n Chap. ' and the normal Familiarizing yourselfwith somebasicdescriptivestatistics distribution.One suchexperinrent . I l t h es q u a r e I proportiona a si n to of velocity Fu : cdu2 (13 l) where Fy: the upward fbrce of air resistance [N : kg m/sr].
the points indicate that the force increases as velocity increases.the relationshipbetweenforce and velocity may not be linear. 2 Plot wind velocifu for on obieclsusoended in o w nd lunnel of forceversus TABTEl3.As in Fig.l . althoughit may not rather significant scatter. First.2. to 25 2A 7A 30 380 4A 550 50 610 60 l22A 70 830 BO I 450 for various levels of wind velocity.REGRESSION LINEAR 285 ri {\' * + FIGURE I3.Finally. 13. Second.This conclusion becomesmore apparentif we assume that force is zero for zero velocity. severalfeaturesof the relationshipbear mention. m/s F.the points do not increasesmoothly. . The relationshipcan be visualizedby plotting force versusvelocity.m/s F I G U R EI 3 .but exhibit particularlyat the higher velocities.N (m/s)fromo wind tunnel Experimentol doto for force(N) ond velocity exoeriment.I Wind tunnelexperimenl fo mecsurehow the force of o r reslstonce dependson veocily 1600 1200 z t\ 800 40 u. The tunnel (any volunteers?) and the force measured resultmight be as listedin Table l3.t u. be obvious.
2 contains24 readingsof the coefllcientof thermal expansion Takenat facevalue. Meosure of Locotion.the conceptis not very practical.For example. In so cloing. ( 13. It is calculated by first puttingthe datain ascending order.thereare actually TABLE13.286 LINEAR REGRESSION In Chaps.we will explorehow to flt a "besf' line or curveto suchdata.The medianis sometimes called the 50th percentile.For continuous variables such as the data in Table 13.The medianis the midpointof a groupofdata. that the valuesrangefrom a minimum of 6.l DescriptiveStotistics Suppose that in the courseof an engineering study.Thesedescriptive statistics aremostotien selected to l'epresent tion of the centerof the distributionof the dataand (2) the degreeof spread of the dataset. There are severalalternatives to the arithmeticrnean. Additional insight can be gained by summarizingthe data in one or more wellchosenstatistics that convey as much informationas possibleaboutspecificcharacteristics ( l) thelocaof the dataset.r STATISTICS REVIEW Before describingleastsquares regression. The mode is the value that occursmost fiequenlly.. If you are familiar with these can be generated subjects.) dividedby the numberof points(n).Ifthe numberof measurements is the middlevalue. we will illustrate how relationships like Eq.2.I ) arisefrom experimental data. t .775.If you are unf'amiliarwith theseconceptsor rrrein need of a review.The urithntetic ntean (l) of a sampleis defined as the sunr of the individualdata points(. [(x to 6 595 6 505 6 625 65r5 6 615 6 435 6.In addition. 6655 6 605 668_5 .rcceeding wherethe summation summaticlns in this section)is from I = I throughn.2 6 495 6 665 6755 6 56_s Meosuremenis of the coefficient of thermol exponsion of siructurol steel 6 )i n l { i n .If the numberis even.The conceptusually hasdirectutility only when dealingwith discrete or coalselyroundeddata.severalmeasurements were madeof a particularquantity.71 5 6 555 o 635 6 625 6 575 6 395 6 485 6715 6. ' ' F ) ] .For example.it is thearithis odd. a linritedamount of informationthat is. I 3. These include the mean. or \. 2 ) (and all the sr.l3 and 14.we describehow simple descriptive statistics and distributions in MATLAB. standarddeviation. the fbllowing material is designed as a brief introduction. n (1 3 . feel tiee to skip the following pagesand proceed directly to Sec. r 3. 13.the rnedian metic mean of the two middle values.655 6775 6 55.r.the dataprovides ofa structural steel.Table  3.2. we will first review some basic concepts frorn the field of statistics. The mostcomlron measure is the arithmetic of centraltendency mean.395 to a maximumof 6.. and the normal distribution.residualsumof the squares.
5)is ru.(Dt'. f u.4) Thus.)r.As such. . Thus. are said to be basedon rr . ( 13.ir2 (13. The spreadcan also be represented by the squareof the standard deviation.: . Eqs. only n .)3 where S' is the total sum of the squares of the residualsbetweenthe data points and the mean. It is often multiplied by 100 so that it can be expressed in the form of a percent: s).\:)l n (1 3 .6.or $: Itr'.715.3)and (13.). the differencebetweenthe largestand the smallest value. and6. However.We will return to the mode when we describe h i s t o g r a mls a t e ri n t h i ss e c t i o n .6.6) This versiondoesnot requireprecomputation of ! andyieldsan identicalresultasEq.5).3)and (13.655. .Consequently.5)yield a meaningless resulrof infinity. the standard deviationwill be small.625. it is not considereda very reliable measure because it is highly sensitiveto the samplesize and is verysensitivt eo e x t r e l n e values.. if the individual measurements are spread out widely aroundthe mean.which is called the variance: r r ' . l frv.I STATISTICS REVIEW 287 ll ii t four modesfor this data:6. (and.If they aregroupedtightly. 5 ) Note that the denominator in borhEqs. We should note that an alternative.. ) will be large.1. s..Although it is certainlyeasyto determine.fyt. and s.. The quantityru. This nomenclature derivesfrom the fact that the sum of the quantities uponwhichgisbased(i..which all occurtwice. The rnostcomlnon measure of spreadfor a sampleis thestandarddeviation(s.consequently.This statisticis the ratio of the standard deviationto the mean.more convenientforrnula is availableto compute the variance: 2 .? nl (I3. The simplestmeasureof spreadis the range.)ry.if continuous dataare groupedinto equispaced intervals.I3.)' l. the remainingvalueis fixed. 7 ) t . Meosures of Spreod.it can be an informative statistic.e. it would be unlikely that any of the values would even have repeated twice.s. Anotherjustification for diviclingby n .1 is the fact that there is no suchthing as the spreadof a singledatapoint.(13.)iszero.I is ref'enedto as the tlegrees of freedont.it providesa normalizedmeasure of the spread.Hences.555.(13.1 of the valuesare said to be freely determined.I degreesof fieedom...If the numbers had not beenroundedto 3 decimal digits. A final statisticthat has utilrty in quantifying the spreadof data is the coefficient of variation (c. x 1007o (1 3 . ) about the mean: ( 1 3 .I of the valuesare specified.v.. For the casewhere n : 7.y)2.ifyis known and rr .
oo723 0.615)12:6.oat22 0 00302 0.median.00002 0. As in Table 13. as [Eq.62b 43 758 4 38 9 1 4 3B 9 l 44 023 44 285 44. which canbe usedto computethe standard deviation [Eq.00203 0 001 23 0 00063 0 00003 0.289 44422 44.Jl3"un t" compured (13.andcoefficient of variation for the datain Table 13.i S i m p l eS t o t i s t i co sf o S o m p l e Problem Stotement.the median is computed as the arithmetic meanof the middletwo values:(6.605+ 6.555 6 565 6 575 5 595 6 605 6.71 5 6755 6.00062 o.288 LINEAR REGRESSION EXAMPLE l3.23) 43 494 43.3)]: sr.00302 a 00422 a.)85 42.635 6.715 6. Computethemean. Solution.00062 0.l3. : 0.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.097133 * z+L TABTE13.3 Dotoond summotions for compuiing simple descriptive stotistics forthe .217000 :0. coefficients of thermol exoonsion fromToble (Jl .the sum of the squares of the residuals is 0.01103 0 00903 o.2.00203 0. (13.445 42568 42968 43 099 43.variance. standard deviation.685 6.61.3. The data can be assembled in tabularform and the necessary sumscomputed ""HJt.02723 0 0t323 0.2.689 45091 45091 45630 45901 u4) 0J/ .02402 0 03062 T I s8zdo 67Tn6 40 896 41 449 42 055 42.ao722 o 41322 a o)322 o.2)].315 42.oao22 0. t : 158'4 :6.775 o 04243 4.655 6 655 6 665 6.61 5 6 625 6 625 6.217000.6 24 Becausethere are an even number of values.
or bins.097133 x1007a:1. rh^ ^"h^.5)]. l3. (13.2.64.Given enoughadditionalmeasurements.47Vo 6.009435 24l 13.A histogramprovidesa simple visual representation of the distribution.651 /24 : 0.1.A histograntis constructed by sorting the measurenrents into intervals.009435 s.ofronnnn.v. a.62. ^{ l^r^ ^^inis increcses.i. Notice also.The result (Fig.6)can alsobe verifiedby computing \: a .7)]: c.400)' 1045.3 A histogrom of doto.?: (0. As an example.ion.we can seethat the bin with the most values is from 6.2 The Normol Distribution Another characteristic that bearson the presentdiscussion is the datadistributionthat is.I3. it is more common to report the most frequent range as the modal class interval.6 to 6.:_ 0. The symmetric.rn.i rli 'i"ll]r[io . 13. br.a histogramcan be createdfor the data from Table 13.6 289 The validity of Eq. FIGURE I3.(158.bellshaped curve superimposed on Fig. If we have a very large set of data. (13.that now that we have groupedthe data.a) suggests that most of the datais groupedcloseto the meanvalue of 6. used to depict the distribution ihe q66+\ hcll.tcned n:srra.097133)2 and the coefficient of variation[Eq. OJ 0) L2 .6..6. :0.3 is one such characteristic shapethe normal distribution. The units of measurement areplotted on the abscissa and the frequencyof occurrence of eachinterval is plotted on the ordinate."" .Although we could say that the mode is the midpoint of this bn. the shapewith which the data is spreadaroundthe mean.r. the histogramoften can be approximatedby a smooth curve. the histogramfor this particularcasecould eventuallyapproach the normal distribution.r'.fpq 1l^p .I STATISTICS REVIEW the variance [Eq. (13.
max(s) :ne 6.3B >> var(s). standarddeviation.097133 I MATIAB also ofiers a Statistics Toolbox that provides a wide range of common statistical tasks. Similarly. 6.555 >> min(s). :0. it returns a row vector containingthe arithmetic mean of eachcolumn of >r. t/a >> range=max(s) min(s) O.35. the rangedefined by tr . for the datain Table 13. andnormal distribution all have greatrelevance to engineering and science. we can tentativelymake the statement that approximately95Vo of thereadingsshouldfall between6.097133. )r + 2. If a quantity is norrnallydistributed..3 DescriptiveStotisticsin MATLAB StandardMATLAB has severalfunctionsto computedescriptivestatistics..std(s) :nc 0.395 o.405'734 and6.6 and s. The following is the result of using meanand the otherstatistical functionsto analyzea column vector s that holds the datafrom Table 13.2: g >> format short (s).61.794266.2s.'rrrve fiffinq fo desiqn . Becauseit is so far outsidethesebounds.mode >> mean (s).If it is a matrix.we would suspect that the measurement might be erroneous.the rangedefinedby.1.residualsum of the squares. to approximately95Vo. If x is a vector. will encompass approximately68Vcof the total rneasurements.the function returnsthemean of the vector's values. will encornpass For example.2.1.ifsomeonetold us that they had rneasured a valueof7.from random rrrrmher oeneration fn .we calculated in Example13. Basedon our analysis.s.s. 13. A very simpleexample is their useto quantify the confidencethat can be ascribed to a particularmeasurement. to y * .median (s) h h 6.r For example.290 LINEAR REGRESSION The conceptsof the rrrean. the arithmeticmeanis computedasmean(x).1that) = 6.0094348 anc  0.v .
528 6.Note that althoughthere are four valuesthat occur twice.55 6. MATLAB can also be usedto generate a histogrambasedon the hist function.68 6.566 6.49 6.1.718 6.7s5 The resulting histogram depicted in Fig.I3. xl = hisL (Y. FIGURE I3. the mode function only returnsone of the values: 6.6 6.his'* (y) without output arguments l0 bins determinedautomaticallybasedon the ranqeof valuesin r.5 6.4 Hisfogrcmgeneroiedwith the MATLAB hist funciion 6.2.65 6_7 6. and y is the vector being analyzed. For the datafiom TabIe 13.452 6.8 6.'.5. The hist functionhasthe syntax ln. x : a vector specifyingthe midpoint of eachbin.85 .55.3.4I4 6.the result is >> hist(s) 3 6.I STATISTICS REVIEW 29r Theseresultsare consistent with thoseobtainedpreviouslyin Example 13.Note that all the arguments and outputswith the exceptionof y are optional.75 6.604 6. 13.4is similarto the one we generated by handin Fig. 13.642 6.For just producesa histogrambar plot with example. x) where n : the number of elementsin each bin.
: . they are deficient becausethey are arbitrary.:) l ) .However.2 TINEAR TEAST.i .any straightline passingthroughthe midpoint ofthe connectingline (excepta perfectly vertical line) resultsin a minimum valueof Eq.10) positive and negativeerors cancel.. ' . . unlessthe points defineaperfert would straightline (in which case. Thus. y ) . .5a. O b v i o u s l y .8)as e:!asa1x (13. ( .respectively.interpolationwould be appropriate). .292 LINEAR REGRESSION 13. Although such"eyeball" calculaapproaches have commonsense appealand are valid for "backot'theenvelope" tions. the residualis the discrepancy value. t h e b e s t f i t i s t h e a line connectingthe points.this is an inadequate criterion.That is. Lt'rt i=l :  Lt. The simplest examrs r .'t i:l i . this criterion also doesnot yield a uniquebestfit. betweenthe true valueof y andthe approximate a0 + atx. One way to do this is to derivea curve that minimizesthe discrepancy between the data points and the curve.SQUARES REGRESSION Where substantial with data.(13. predictedby the linear equation. 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 f a i r e d o b s e r v a t i o(n for the straightline is The mathematical expression y : a o* z t l x1 e (13. r . ' . . .a1x. any straightline falling within the dashedlines will minimize the sum of the absolute values ofthe residuals.whichdepictsthefito fs t r a i g h t l i n e t o t w o p o i n t s .8) where a6 and a 1 are coefficients representingthe intercept and the slope. To do this. 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.) /'rl i:l (l3.".as. ( x z .lo) wheren : total numberof points.9) Thus. and e is the error.asillustratedby Fig.as in L't r:l i ".However. as in i . . . we must first quantify the discrepancy.r. equal to zero because ab One way to remove the effect of the signs might be to minimize the sumof the solutevaluesof the discrepancies. I 3. or rcsidual.2.the best curvefitting strategy is to derive error is associated an approximatingfunction that fits the shapeor generaltrend of the datawithoutnecessarily matching the individual points. One approachto do this is to visually inspect the plotted data and then sketch a "best" line through the points.somecriterion must be devisedto establish a basis fit.l3.a o alxil (l3ll) Figure 13. ) n ) . between the model and the observations. forthe To removethis subjectivity. (13. For the four points shown.which can be represented by rearranging Eq.5bdemonstrates why this criterionis also inadequate. differentanalysts draw different lines. .
. Luther. e .l il il ttltt' l.e 6 1 1 t o s r {6 9 . .12) .as. e s s i o n l .5 F x n m n l e sn [ s o n .r lm ^ m . t .1 .In this technique. b e s ' r i .i (c) FIGURE I3. A strategythat overcomesthe shortcomings of the aforementioned approaches is to minimize the sum of the squares of the residuals: iltI : f. : 5"i Lt /':' . A third strategyfor fitting a bestline is theminitnar criterion.this strategyis illsuited forregression because it gives undue influence to an outlierthat is. t h o t o r c : n g l e n r r n t c f n r .l il ii . It should be noted that the mininax principle is sometimeswellsuitedfor fitting a simple function to a complicatedfunction (Carnahan. z e s t h e s u m b o f t h er e s i d u o l(s . a single point with a large error. )m i n i m i z e th s es u m o f t h eo b s o l u tv e o l u eo s f i h er e s i d u o lo sn .il I 3. d ( c )m i n i m i z e s l h em o x i m u m error o f o n yi n d i v i d u o p lo i n t . 13.tt't7'j''' oo2ote' .2 LINEAR LEASTSQUARES REGRESSION 293 1il lrl li . the line is chosenthat minimizes the rnaximumdistance that an individual point falls from the line.(v. 1969).l (13.5c. and Wilkes. As depictedin Fig.arxi)z S.
\ tr.r. I. (13..t'' O \ s.. The data can be set up in tabulirrfbrm and the necessarv sums ... :Lr.)' This resultcan rhenbe usedin conjunction with Eq. realizingthat I a0 : noy.f .2. ) r .s and at.\'l Note that we have simplified the summationsymbols.12) is differentiated with respect to each unknown coeflicient: i)s' ilLti : ? \. fbrce is the clependent variable(.vand.2 L i n e o rR e g r e s s i o n ProblemStotement.r. which is called leastsquares. * (f ". : f .the equations can be expressed as o:f..v) and velocityis the independent variable (.r'.i ltT.) '\ . . .. In this application. tr (1. has a numberof advantages.1. (13.. They canbe solvedsimultaneously for .2 leostSquoresFir of o Srroight Line To determine varues for . _ .*i o : f r. L Now. ...respectively..n. Fit a straight line ro rhevaluesin Table i3.r'.unlessotherwiseindicated.( / { ). (r3. we can express the equations as a setof two simultaneous lirrear equations with two unknowns (aoandnr).rI. EXAMPLE 13.r r . ..Ifthis is done.t.n.r. Eq..r.atx wherev andi arethe means of .r.294 TINEAR EGRESSION This criterion. ( 13. I. .Beforediscussing these propertles.r 2).+(\ r \LJ'ri)tt1 Thesearecalledthe nonnal equation.1. all summationsare fiorn r : I to rl. : z l Lll.(I '.r). .f . Settingtheseclerivatives equalto zero will result in a minimurn S..r5) Solution.f...a 1 ' r .'? .r3) to solvefor ao: i ..r...uf.) t't A^: 2 \ . includingthat it yielcls a uniqueline lor a given setof data. . we will presenta techniquefor cletermining the varucs of (h andat that minimize Eq. 13. un  tt 1x..
2857 ao : 641.r 0 0 I 16.600 4.03 r3 8(312. 4 5 0 254 t.6 ineto thedotofrom Toble 1 3t leosfsquores fitof o strclght 1600 1200 800 z .815 g (13.16) as The slopeand the interceptcan then becalculated o' : .135 : 641.400)(360P * 19. I ]3. 5) :19.6.904 6.600 2 500 3.15) withEqs.47024(45) : 234.400 I 1.875 fit is ofy and. from xi li x! r00 4AO 900 r.4 295 needed to compule the bestfit linefor the doto Dotoond summotions Toble. 13. theleastsquares Usingforceandvelocityin place F : 2342851t 19.along FIGURE I3.400 22AAO 30500 73 204 5 8 .41024u withthedata.000 3r2850 The meanscan be computedas 360 _ i_45 8 _ }: 5..2 LINEAR LEASTSAUARES TABTE13.. Theline.8s ) 60(s .REGRESSION I 3. is shown in Fig. and(13.41024 8(20.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.
 3.:ftlrl)2 (13.17) s.3. (13.In Section 13.Recall that thesum of is definedas [Eq.In Eq. *o Measurement a0+ afii .(13. Thus.7 The residuol in lineor regression represents theverticol dlstonce o dotopoint ihe between ond s t r o i g hlti n e . the square of the residualrepresents the square of the verticaldistance between thedata and anothermeasure of centraltendencythe straightline (Fig.3 Quontificotion of Error of lineor Regression Any line other than the one computedin Example 13.7).the square ofthe residualrepresented the square ofthe discrepancy between the dataand a singleestimate of the measure of centraltendencythe mean. the zero interceptmeansthat theequa" tion predicts physically unrealisticnegativeforces at low velocities.t. (13.18). 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.17).4) (13. the line is uniqueand in termsof our chosencriterion is a "best" line through the points.2.12)) the squares n . A number of additional propertiesof this fit can be elucidated by examiningmore closely the way in which residuals were computed. we will show how transformations can be employedto derivean altemativebesrfit line thatis more physically realistic.2resultsin a largersurnof thesquares of the residuals.296 LINEAR REGRESSION Notice that althoughthe line fits the datawell.18) In Eq.: f (li aoarxi)2 i:t Notice the similarity betweenthis equationand Eq. (13. It can be demonstrated that if these criteria FIGURE I3. 13.
As with our discussion of the standard deviation.Thus. ( 13.F./r  s.anotherjustification for dividing by n .2 is that thereis no such thing as the "spreadof data" arounda straightline connectingtwo points. (13.3)] J]. tbl . Also.17). a "standarddeviation"for the regression line can be determinedas [comparewith Eq.this quantityis designated variable(in our case.os ind. 1981)..8a). n2 (13.8b in contrastto the standarddeviation s. the standarderror of the estimate quantifies the spreadof the data.This characterizes the residual FIGURE I3. leastsquares regression will provide the best (i. 13.However.The subscriptnotation "y /x" designates that the error is for a predicted value of y correspondingto a particular value of x. is called the standard error of the estimate. Just as was the case with the standarddeviation.. represenls theimprovement dueto linecregression.e. This is particularly (Fig.2 because two dataderived estimatesas anda1were used to compute S.q.19) where s_"7. we have lost two degrees of freedom.8 (o)thespreod Regression dotoshowing of thedotooround themeonof thedependent vorioble ond (b)lhespreod of thedotooround thebestfit line. In addition.ifthese criteria are met.cored by t[e be'lsl^'oped cu'ues ot therigl^t..19) resultof infinity. noticethat we now divide by n .I 3.To do this. (13. that quantified the spreadaround the mean (Fig.18). for yieldsa meaningless the casewheren : 2. is the magnitude of the residual error associatedwith the dependent variable prior to regression.we can compute Sr. As was the casefor Eq.After performing the regression. This y). 13. we returnto the origiuseful for comparisonof severalregressions nal data and determine the total sum of the squaresaround the mean for the dependent g .Thereduction in thespreod in goingfrom (o)to (b).(13.This is called the maximumlikelihoodprinciple in statistics.2 LINEAR LEASTSQUARES REGRESSION 297 are met. 13.9).the most likely) estimates of ag and a1 (Draperand Smith. the sum of the squaresof the residualsaround the regression line with Eq. Theseconceptscan be usedto quantify the "goodness"of our fit. thus. syTxQuantifiesthe spreadaround the regression line as shown in Fig.
for thefit in Example 13.2.)' (L.? EXAMPLE 13.(Ir')(Iy. p l eo sf i n e o e g r e s s i ow n r t n ( o )s m o l 'o n d { b ) 'orge residroe l 'rors It is.298 LINEAR REGRESSION o o I3.S. provement or error reduction due to describing the data in terms of a straight line rather than as an averagevalue. therefore.. thestandard errorof the estimate. S. : Sr and the fit represents no improvement. s. signifying that the line explains100% ofthe \: J r)). Becausethe magnitudeof this quantity is scaledependent. the differenceis normalizedto S. fit. For a periect An variability of the data. is alternativeformulation for r that is more convenientfor computerimolementation r: nf(xiri) . andthecorrelation coefficient lrlyl'?: .sometimes called the unexplained error that remains after the regression.(I r. /.'2 : S'S. . where 12 is called the coefficient of determination and r is the correlation coefficient .$ .)'J. Compute thetotalstandard deviation. quantifies theimsum of the squares.) .9 FIGURE E r o r ' . For 12 : 0. Thedatacanbe setup in tabular computed in form andthenecessary sums as .I r. t. The differencebetweenthe two quantities. to vield .3 Estimotion of Errors for theLineor LeqslSouores Fit Problem Stqtement. : 0 and 12 : l.
444 216. A1 s3 in . 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 ) . Draper and Smith ( 198l) provide guidanceand additionalmaterialregardingassessment of resultsfor linear regression.441 68.454 39 58 t55t2 349 82 544. Theseresultsindicatethat 88.79 Thus.5r. because sr.t2863 1 ./. ln addition. ( 13.88[5 : 0. more meaningto it than is warranted.Although the coefficientof determiprovides you should be careful not to ascribe nation a handy measureof goodnessof'fit.t)2 (!i .699 B ].the linear regression model has merit. Before proceeding.r is not even linear..23 9 3 39 3 I.535 327.5 Dotoond summotions needed to compute the goodnessoffit stotistics f o r t h ed o t of r o mT o b l e 13.05clc hasbeenexplained by the linearmodel..808.26 and the standard errorof the estimate is [Eq.20)l I 1 .9383. 297 216..297 4.0r6 3 3 42 2 9 3 53 9 r 6 53 .52 735. a word of cautionis in order. Justbecause r'2 is "close" to I doesnot meanthat the "good. 0 6 6 t . 297 of the orisinal uncertaintv or r : \/0.you shouldalwaysinspecta plot of the dataalong with your regression curve.8805 l .e c a m e u p w i t h four datasetsconsisting of I I datapointseach. (13.1tB The standard deviation is [Eq. (13.441 1." possible fit is necessarily For example.I B O 16.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. lF 0i.]..3)] : 508. it is to obtaina relativelyhigh valueof r'l when the underlying relationshipbetweeny and .245 9il 30 I6.r( s.Although their graphsare very different.118 : 0. 808. 3 23 3 380. and the samecoefficientof determination.all have the samebestfit equation.171 7.2 LINEAR LEASTSQUARES REGRESSION 299 TABTE13.ao .I 3. li eo i atxi (yi . The extentof the improvement is quantified by [Eq.19)] : 189. 8 0 8 . . 8 3 7 8 9 .g h.579 8.)': 3 + 0.
. Another exampleof a nonlinearmodel is the simplepower equation: ). This is not always the case. 13.: a2xP) ( 13. are appropriate.For example.y: 3 + O.populationgrowthor radioactivedecaycan exhibit suchbehavior. 13. it is predicated on the fact that the relationship betweenthe dependent and independent variables is linear. the equation nonlinear.300 LINEAR REGRESSION t3 t:) 10 10 10 FTGUR tE 3. As depicted (for B2 l0)is in Fig.It is very frequently used to fit experimentaldata when the underlyingmodel is not known.the equation representsa nonlinearrelationship(for fu l0) between. .vand x. For others. This model is usedin many lields of engineering andsci(positiveB1 enceto characterize quantities (negative that increase or decrease ) ) at a rate B1 that is directly proportionalto their own magnitude.22) where a1 and B1 are constants. techniques such as polynomial regression. This model haswide applicabilityin all fields of engineeringand science.23) wherea2 andB2 areconstant coefficients.11a.However.3 TINEARIZATION RETATIONSHIPS OF NONTINEAR Linear regression providesa powerful techniquefor fitting a bestIine to data.11b.transformations can be usedto expressthe datain a form that with linear iscompatible regression.lO Anscombe's four doto sets olong with ihebestfit Jine.and the first step in any regression analysis shouldbe to plot and visually inspectthe datato ascertain whethera linear model applies.5x I3. which is described in Chap.As depictedin Fig. In some cases.14. One exampleis the exltonential model: ! : olreP" FIGU {o)Th (fJore (l3.
1lc) that levelsoff. Nonlinear regressiontechniquesare availableto fit theseequationsto experimental to transis to usemathematical manipulations datadirectly. neeringand science.However. r (1.r_ D r+ . or "saturates. Then linear regression to data. also representsa as x between nonlinearrelationship y and r (Fig.{e)." particularlyin biologically relatedareasof both engiincreases. which is particularly wellsuited for characterizingpopulation growth rate under limiting conditions.1.ond (b)thepowerequolion. ond (c)thesoiurotrongrowthrole Theerponentiol equotion. Ports equotion.3 LINEARIZATIO ON F NONLINEAR ELATIONSHIPS 301 :: azxQl (dl GI (f) HGURE r3.This model. lol i tromsimple tronstormotions. equations .r I (d).2+) where a3 and B3 are constantcoefficients.I 3. l3. versions lhctresu lineorized oi these equolions lllore equation: A third exampleof a nonlinearmodel is the saturationgrowthrate J .a simpleralternative can be employed to fit the form the equationsinto a linear form. It has many applications.\':0.
23) is linearizedby taking its basel0logarithm to give logy:loga2*B2logx (r3.404 3.with a slopeof Bzluz and an intercept ( F i e .302 LINEAR REGRESSION For example.785 3. 1 l / ) .Eq. 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 . (13.23) formation.:Yy:1.845 L903 tlouo 1 398 t B45 2. in as sumscomputed Solution. The meanscan be computedas .ld). a plot of log y versuslog x will yield a straightline with a slope of B2 andaninter' this cept of log cv2fig.1usinga logarithmic ProblemStqtement. The datacan be set up in tabularform and the necessary Table 13.584 2. to evaluate In their transformedforms. (13.)2 log x.5i5't TABTE 3.22)can be linearizedby taking its naturallogarithmto yield l n y : l n c v r* f r x (13.24)is linearizedby inverting it to give 1 I I qS ful +' (13.919 3.t6r lu) ) I 000 I 693 2. They can then be transformedback to their original power model.6.622 /0 ) 1a r 398 2.6 xi t: ?Tr :2'5644 needed to fit the powermodelto thedotofrom Dotoond summotions r l l l o b l el 3 . l3.21) a3x of 1/o3 Thus. Equation(13. logy.2t of Thus.1 3 . 13.162 3.the base10logarithmis most commonly employed. as done here. a plot of ln y versusr will yield a straightline with a slopeof B1 and an intercept ln o1 (Fig. However. l li log ri IogJi (log r. Note that any base logarithm can be used to linearize model.086 2.AAl 3Bll 4 390 4.488 5 386 6016 I ilbn . 13. Fit Eq.886 3.Ile).732 5. Equation(13.182 2 567 2.26) Thus. for the The following illustratesthis procedure for predictivepurposes. thesemodels can be fit with linear regression state andused the constantcoefficients.744 2.4 EXAMPLE Equotion Fitting Dotowith the Power transto thedatain Table13. a plot of 1/y versusl/x will be linear.
1 5 ) a n d as al : 8 ( 3 .9842 . . 5 16 r( 1 2 . 5 6 2 0 t l . 9 8 1 2 ( 1 . 6 2 2) I 2 . 6 0 6 1 2 0 l . 13. 9 8 4 2 l o gr The fit along with the datais shown in Fig. 6 0 6 ) 2 :1.I3.5 ) 8 ( 1 0 .5644 The leastsqLrares fit is l o g r ' : .I2 firof c po*ermodel l3 I {oJ Leost squores lo thedoto from Toble lhe fitof theli'onsfoi'med doto (b) The power fitolong with the dolo.3 TINEARIZATION OF NONTINEAR RELATIONSHIPS 303 ri log v 1000 100 log.12a. equotion (13.5620 ctl : 2.( 1 3 .1 .0 .r . 5 1 5: 1) 0.r ) 1600 1200 800 400 0 40 (b) 60 80 F I G U RI E 3.16) The slopeand the interceptcan then be c a l c u l a t ew d i t h E q s .
cierrts of the power nrodelare determined oScy2 Using fbrceand velocityin placeof . For example.r'andr. I3. alongwith the data.3. To do this. The yvaluesare independent randomvariablesand all have the samevariance.4 C OMP U T E R A P P L IC A TIONS Linear regression is so commonplace that it can be implementedon most pocketcalculators.it is known from the discipline of fluid mechanicsthat the drag force on an object moving through a flLridis often well described by a model with velocity squared.1 Genercrl Commenlson Lineor Regression Beforeproceeding to curvilinearand multiple linear regression. Such assumptionsare relevant to the proper derivirtion and use of regression. 13. equation t3.rst ernphasize the troductory nature of the foregoing materialon linear regression. We will also show lrow linearregression can be implemented with the builtin po ly f i t f unction.Further. y o u u s ef o r c u r v eI ' i t t i n g .You shouldbe cognizant ofthe fact that there are theoreticalaspects of regression that are of practicalimportance butare beyondthe scopeof this book. In this section.12)should be comparedwith the one obtained previouslyin Example13.9942.the first assumption rneirns that (l) the "r valuesmust be en'ortiee and(2)the regression of t'versusx is not the sameas r versus1. We have focused onthe simple derivationand practicaluse of equations to fit data. Eachx has a fixed valuel it is not randomand is known without error. 3.6)usinglinearregression on the untransformeddata Although both resultswould appear to be acceptable.304 LINEAR REGRESSION We can also display the fit usingthe untransformed coordinates.2741 andB. 13.12b.2J4lut This equation. the transformed resulthastheadvantage that it doesnot yield negativeforce predictionsat low velocities.4 (Fig.is shownin Fig.somestatistical assumptions that areinherent procedures in the linear leastsquares are l.2(Fig. Thus. the leastsquares fit is e8a2 F :0. The fits in Example 13.thecoeffi: 19 05610 :0. 2. You are urgedto consultother referencessuchas Draperand Smith ( 1981)to appreciate aspects and nuances of regression that are beyondthe scopeof this book. For example. vslusr for given must The I' a x be normally distributed. knowledge fromthe field you are studying often has a large bearingon the choice of the appropriate mode. . 13. =1. inwe mr.we will show how a simple Mfile can be developed the fo determine slope and interceptas well as to createa plot of the dataand the bestfit line.
15)and(13.2: >> x = 110 20 30 40 50 60 70 B0l. > > 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. BB05 r 9 .9481 0.I 3. 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. The routinedisplays the interceptand slope. Theseare then used to compute the slopeandthe intercept with Eqs.4)by applyingthe Iosl0 function to the data as in >> linregr (log10 (x). and a plot of the bestfit line along with the measurements. A simple exampleof the use of this Mfile would be to fit the forcevelocitydatathat was analyzed in Example13.y) 0. 4 1A 2 .(13.16).2 3 4 .9842 .1 MATLAB Mfile: linresr 305 An algorithm for linear regressioncan be easily developed(Fig. 13.13).4.the coefficient of determination.1oql0 (y) ) 0.The required summationsare readily computedwith MATLAB's sum function.5624 7.4 COMPUTER APPLICATIONS 13.
id o n . error(. y = y(:).4 t.306 LINEAR REGRESSION 3. T2 = ((n*sxysx*sy) /sqrt (n*sx2sx^2) /sqrt (n*sy2. S y = S u m ( y ) . xp.y) % linregr: linear regression curve fitting % 1. if length(y)=n.t 1.. % 12 = coefficient of determination a(2) n = length(x).. sxy .2) . yF a(l)*xpa(2). function Ia. plot(x.'2) .8 FIGURE I3.Sum(x.5 2. a(2) = sy/na(1)*sx/n. SX = 3s11 sx2 = srtn(x.*x). a (1) = (n*sxysx*sy) / (n*sx2sx.).o.*y).x and y musf be same length. end x = x(:)..yp) g r r . 12l = linregr(x. ? convert i_o column vectors ( tx ) . and incercept. a(1). 12 ] = linreqr (x.*y).y. sy2 = Sum(y.o 1. % create plot of data and best fit lrne xp = lanspace (min (x) .5 1.sy^2) ^Z ) .5 t.I3 AnM{ilelo implement lineor regression.max\x\ .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.
As in Fig.14.i] : substrateconcentration. y.1) I9 . theprod."1' 1 rr . >>x >>y >>a t10 20 30 40 50 60 70 B0l. it}fS9. 125 7A 380 550 610 1220 830 14501.  E N Z Y ME K IN E T IC S Bockground. 6 4 7 . n) wherex andy arc the vectorsof the independent and the dependent variables.and y: >> y = polyval(a.28) where u : the initial reaction velocity. . the slopeis 19.y .It can be applicdas in >> p = polyfit (x.2 MATTAB Functions: polyf ir ond polwal 307 MAILAB has a builtin function polyf it that fits a leastsquares nthorderpolynomial to data. uct. olld n : the order of the polynomial.S].4702and the inrercepr is 234. theyconvert onechemical into another. 4702 23 4 . and ft" : a halfsaturation constant. B 75 A .the substrate.2857. potyf ir (x.45) the bestfit value at x.4.t+ .We should note that it represents the polynomial using decreasing powers of r as in the following representation: f ( x ) : I t t x " * p 2 x " .!l.2857 Thus..In mostcases. . t) will returnthe slope and the interceptofthe bestfit straightline..s] k"+ [s] (13. . the equation describes a saturating relationship which levels off with increasing [. r 1 Because a straightline is a firstorder polynomial.The function returnsa vectorp containingthe polynomial's coefficients. x) wherep : the polynomial coefficients. The graph also illustrates that the halfsaturation constant correspondsto the substrateconcentration at which the velocity is half the maximum. . For example.. . u: the maximum initial reaction velocity. . + p n x* p n . Enzymes act as catalysts to speed up the rateof chemical reactions in living cells. polyval. Another function. respectively.q.."fl.5 CASE STUDY 13. : d .TheMichaelisMenten eqvation is commonly usedto describe suchreactions: u u_ [. can then be used to compute a value using the coefficients. polyf it (x. .I3.It has the seneralfbrmat: >> y = polyval (p. 13. [.y..
which is in the format of the saturationgrowthrate (Eq. the following secondorderversion often results in a bettu fit: 't):   u. (13.For cases at one site leads to enhancedbinding of subsequent two interacting bonding sites..14. 13. it has been fined and extended to incorporate additional features of enzyme kinetics.5u. 6 0 . Supposethat you are provided with the following data: tsl U 1. Aside from estimating the model parameters. 8. k" FIGURE I3. can be linearizedby inverting it to give (recallEq.22 d a7 F.29).assess the validity of the fits with both statistical measuresand graphs.[s]2 ft"2 + [S]2 (t3.308 LINEAR REGRESSION continued Secondorder MichaelisMenten model 0.a7 IB 0.the squared concentrationstend to make the shapemore sigmoid. One simple molecule where the binding of a substrate extension involves socalledallosteic enzymes.35 036 Employ linear regressionto fit this data with linearized versions of Eqs. as depictedin Fig. 33 5 B9 0. I . or Sshaped.27) k. Two versions of theMichoelisMenien reAlthough the MichaelisMenten model provides a nice starting point.T4 model of enzyme kinetics.28).28)and (13.. with molecules at other sites. model Equation(13.1 3 3 0.29) This model also describesa saturatingcurve but.3 o. Solution.2D. 13.
ii{.6406 is obtained.2ja A. For example.1n*J ks= 3 r :.9t4l The model coefficients can then be calculated as >> vmIra(2) \ rrr i: aRral ( 1 j ':' .36. The data is clearly leveling off at about 0.the fit is obviously unacceptable.. Figure l3.. >> v=f 0.ryifiru::ri x. and ft. shouldbe in the rung. 13.I3.i:. inspection of the coefficients might raise doubts. u.a much less acceptable result of I . Beyondthe visual evidence. 2 : t6 ( ) Thus.25701S1 86.13can againbe usedto determine the leastsquares fit: l9. . of 2 to 3.1 . For the untransformed case. the halfsaturation tate (86. The foregoing analysiscan be repeated for the second_order model..j. O. 13.28) can also be linearizedby inverting it to give I I k? I +j_ u D.13 can then be used to determine the leastsquaresfit: >> S=11. an eyeball estimatewould suggestthat u^ shouldbe about 0. r .I5ta shows the transformedversion._ 5.36 or 0.Ll 0.2?..07 0. [Sl2 The linreqr function from Fig.When the original equationis plotted along with the data in the untransformed version (Fig.iir 0.15b)..4A22 12= The linreqr 0.0.sol. The problem is underscored when the fit is plotted along with the clata. > > L a . . the maximum velocity 15'2510)is much greaterthan the highestobservedvelociry tb.3 1. 1. the poorness of the fit is alsoreflectedby statistics like the coefficient of determination.Although the straightline iollows the upward trend. If this is correct.2260) is much bigger than the maximum substrate concentration (9). Equation (13.37.l 5 0. 309 :i continued function from Fig.. 4 4 9 ) .l7iiO i:l = ? . the dataclearly appears to be curved.2260 + [s] Although the highralue of 12might lead you to believe rhat this result is acceptable. ./v) 16.3t.5 CASE STUDY . 13.5 G 8 9l.{S=\.19u2 tl.8 3 4.. In addition. j. the bestfit model is . r ' 2 l = L i n r e g r ( 1 ..
(a) Transformed model 0.is slightly greaterthan the highest observedvelocityand stthcfrqfc nnnnanfrofinn /o\ .5 14. BI?.4 o.i . the parameters trends in the data. that is.4083ts12 u:7.6 a 0.4083 >> kssqrt(vm*a(1)) 2 . the ft.4 0.ond{blshows {o)shows oilgrnorrorm.r1 (b) Originalmodel 0.91r+[s:p of a good fit. The model coefficients can then be calculated as >> vm=I/a(2) 0.I5 FIGURE plotin The model withdoto(points}.3to LINEAR REGRESSION #i*. fit{line) olonq Plots of leoslsquores of theMichoelisMenten viewed in iheuntronsformed.8 0. howlhefit looks when thetionsformed fit. In addition. the fact thatit is very Although we know that a high I doesnot guarantee withthe values also seemconsistent high (0.29) gives 0. (13.1 Substituting these values into Eq.2 0 isl I3.9929) is promising.
we should never solely rely on statistics such as I as the sole goodness offit.1 0 tsl FIGURE T3. Based on our analysis. And for caseswhere transformations are employed. the goodnessof the fit is also reflected by the fact that the coefficient of determination for the untransformedcasecan be computed as I : 0. originol form thetrqnsformed The {points).3 . When the original equation is plotted along with the data in the untransformed version (Fig.9896.we can conclude that the secondordermodel provides a good fit of this data set.4 0.2 0.the transformed The adequacy of the fit can be assessed results appear linear. Beyond this specific result.the fit nicely follows the trend in the measurements. a graph of the untransformed model and data should always be inspected.16b). 13.lt+F. Second.16a.$fii:{:ifl (a)Transformedmodel t/$12 ( b )O r i g i n a l model 0. graphically.I3. The reasonthat this might occur is that minimizing squaredresidualsof transformeddata is not the sameas for the untransformed data.I6 MichoelisMenfen model olono withdoto Plots fit(line) of thesecondorder of leostsquores plotin (o)shows fit. 13. This might suggestthat we are dealing with an allosteric enzyme. although transformationsmay yield a decent fit of the transformed data. As in Fig. Linear regression assumesthat the scatter of points around the . First. Beyond the graphs. this does not always translateinto an acceptablefit in the original format.5 CASE STUDY 3ll ffi'ru conrinued i. 0. there are a few other generalconclusionsthat can be drawn from this case study. regression equations shouldalwaysbe assessed basisofassessing graphically. Finally.ondib)shows iheuntronsformed.
13.8 Fir I natural 13.computethe standardenor of the estimate and the correlation coefficient. 13.8 10. continued Lineariz on the f( the data. there areother models that can be linearized usins transformations.ll.6 x 2 4 6 7 y4565 8.nRT to deterrnine rt onthe basis of this data.4 8. de 13.5with intervalsof 0.65 29. (b) median.l Given the data regression to fit a straightlineto 13.75 27.5.l 10. (c) mode. In this approach.85 26. E . Plot thedata and the regressionline.l Given the data f (hr) c (CFU/ 8.8s 28.65 28. T.65 27. computethe range (that is.'i:. detelminethe slope that resultsin the leastsquares fit for a straightline with a zero intercept.8 9..I.'C p. (13..25 26. and that the standarddeviation is the same at every value of the dependentvariable.4 9.^rep1r w(kgl A (m') Show t well.65 28. \) : a. Theseassumptionsare rarely true after transforming data. (b) median.5.2 7.35 27.65 28.15)and (13.Fit the following data with this model and display the result graphically.65 2'/. (i) Assuming that the distribution is normal. 13.8 10.nonlinear regressionshould be employed to fit curvilinear data.1Using the same approachas was empioyed to derive Eqs.4 9. We will describehow this is done in Chap.85 26.05 28.someanalystssuggest that ratherthanusing linear transformations.5 Use leastsquares x02469 y56764 ll I 12 t5 710 l7 l2 19 t2 The timr and the r estimate and (b) 2OOCF be cons impossi creases 13. Determinewhetherthis compasses is a valid estimatefor the data in this problem. switch the variables. FoI example.10 F (Eq.1 y O. and that your estimateof the standard deviation is valid.and (g) coefficient of variation. (c) mode.Nlm2 aO t 2A 0 40 B0 160 6 9 0 0 B1 0 0 9 3 0 0 1 0 . Note that for the 1aw.2 Construct a histogram from the data from Prob.65 27.35 28.45 27.9 That is.65 28. l3 model: 28.8 10. ' 'i '".65 26.2s Determine (a) the mean.11( related ber of (kg) git Enrploy the ideal gas law pV .6 The following datawas gathered to determine therelationship betweenpressureand temperature of a fixedvolunteof I kg of nitrogcn. As a consequence of the last conclusion.6s 27. (e) standarddeviation.4 11 .16). 13. 13. bestfit line follows a Gaussiandistribution.25 3 r .and (g) coefficient of variation. (e) standarddeviation.5 9.2 t0.35 28.0 9. The volumeis l0 mr. x 0 .1 9. t0ill41720 876912 Determine (a) the mean.9 9.I must be expressed in kelvins.5 to 11. lJ.65 27. (f) variance. Use a rangefrom 7.but regress x versus}that is. 14. a bestfit curve is developed that directly minimizes the untransformed residuals.75 13.s5 28.55 29.Use a range from 26 to 32 with increments of 0. (d) range. l3. (f) variance. 2 0 0 t 2 9 0 0 When t results parame (d').4 10. .15 2'7.9 8.derivethe leastsquales fit of the following model: \tn1r'lp Along with the slopeand intercept.Interpretyour results.7 Beyond the examples in Fig. the lower and the upper values) that en68Vo<>f the readings.'.U tion. Then repeatthe problem.5 l0.5 9. (d) range.0 9.8 9.EMS l3.65 26.'75 21.6 9. (h) Constructa histogram. 3 10. 5 0 0 I 1 .9 Tb area is n PROBI. " ':.3r2 LINEAR REGRESSION ":.
r based follou. Trrsl il wilhlhcrl. this equatior'l yields identical results to the baser.the surface area A of human beings is rclated to weight W and height H.15 Developan Mfile function to computedescriptive stafistics for a vector of values.Develop a plot of your fit along rvith the data. /< : Krnaxc.rpr ic fnr r Q5_Lo nersrrn data. .median. Note that yor.^a. 13.17 Develop an Mfile function to fit a power model.)versusr.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 Fita powermodel to the datafiom Table 13.. but the value of thLexponent (lr) will diff'erfrom that estirnated parameter with Eq. Measurements on a nunber of individuals of height 180 cm and dillerent weishts (kg) valuesof A (m2) in the lbllowing rable: eive k 0.range.Test with When usedfbr curve iitting.ll On average.8 logarithms to perform the transformations.PROBLEMS this model and use it to estimatear and 6. A(rn2 2l 1 0 2 t 2 2 t 5 2 2 A 2 2 2 2 2 3 2 2 6 2 3 0 13.tl. 3r3 13.jubp l ot function to displaygraphsof both well.3 . c o m n l o na l t e r n a t i v i e s to employ a base10 { E qI.x and predict the growth rate at c :2 mg/L.r.usethe .ingdata.mode. 13.ays dewith time.r'ariiince. mean. and ftn. standard deviation.6 I 950 2 2900 23 3600 is measured in hoursfollowing the end of the storrn Ihetime ard theunit CFU is a "colony lbrrning unit. along with the 12fbr the untransfbrmed model.14 An investigator below fbr an experimentto determinethe growth rate of bacteria ft (per d) as a function of oxygen concentration c (mg/L). a formulation to relateut ro frs l3. and (b) usesthe sDbptor functionto alsodisplaya plot of W(kS)7A 75 77 BO 82 84 87 90 the resicluals (the predictedminLrs the measured r.l3 so that it (a) computesand returnsthe standard error of the estimate. 13.13. In Show that a power law A : oil' fits thesedata reasonablv addition. It is known that such data can be modeled by the fbllowing equatiou: .Have the function detennine and displaynumberof values. Usethe baseIOversionto solve Prob.2 I 500 1. c 13.).rhisloglinr.rrchoice of model should consistent concenrations be with the fact that negative ale inpossible and that the bacteria concentrationah. but use 13. nafural Theconcentration of E.In addition. Then use linear regression to estimate c.version. c'oli bacteriain a swimnrins 13. and predict what the the transfbrmed and untransformed equations along with the orrf. creases Rather than using the basee exponential nrodel 13.11 modei to 4 ." Use this data to (a) the concentration estimate at the end of the storm (t: 0) (b) the time at which the concentrationwill reach and 200 CFU/100mL.. cr+c' t a where c.ored after a slorm: Cow Humcn Sheep Hen Rcri Dove 400 7A 45 2 03 0 16 270 B2 50 4B 1 /< 497 Fit an exponential l1.9. huvc it gencralc .In addidevelop tion.5 tt OB 2.l.r lromProb. and ftnra\are parameters.9 uea is monil.16 Modify the lrnresr funcrionin Fig. 2 2 t u model: Plot the data and the equationon both standardand semilogarithmicgraphpaper.3.{h4 B mL) ls9O I 320 c(CFU/l0O t6 2A 24 900 650 560 x v 04 800 08 975 I'. Have the function return the bestfit coefficient a2 and powerB. hasreportedthe datatabulated 13.and coefficientof variation. Evaluate the conslantsa and b.4 t5 53 25 76 4 B9 13.10 ..22 (0. Use a transformation ro linearize this equation.
Use the data to determinethe dctL'rmine the properdesignof bike lanes. 13.19 You perform experimentsand determinethe fbllowing valuesoi heat capacityc at varioustemperatures Z for a gas: T5030060 l25O I 280 c I 350 I480 90 I 580 ilO l70A 13...and the sum of the squares of the residualshad been contrast.N.estimatethe specificgrowth rate of thebacteria duling the tirst 2 hoursof growth and during thenext4 houm of erowth. longterm consistingof many steps. points datawas takenat l5 time 13. cm/cm Stress.m 24l524IB1.ltl The tbllowing data shows the relationshipbetween the viscosityof SAE 70 oil and temperature. N/<m2 (a) Fit a straight line to this data and use the equation to h0t23 determinethe tensilestrengthat a time of 32 min.:.reanalysis but for a straight line with a zercr S l L 0 1 0 0 0 3 3 2 t t 0 2 I 6 4 4 2 453 3 660 5 460 intercept. 1 2 . and E.The following data is collected: '4: dt r.1.5 l2 312 l o n e W i d r hm . Basd on tbe data.l^. 3 2 1 I B 2 7 1 .andthen they are inducedto producea recombinant protein. m3/s s" Number of Model Porometers Fif 135 2 r05 3 13. (b) Repeat tl. determine cor.67 93 33 I 35 O0B5 I48 Bq 3I5. where R is the gas constantand equals0. ^f rli^ .1^r^J f^ .5 hours.'J. ICells]..25 In waterresourccs engineering.23 Below is data taken from a batch reactorofbactenal growth (after lag phasewas over).. . 52 9 1 5 (a) Plot the clata. 2 9 2 ..Which model bestdescribes thedata tistically)?Explain your chorce.A.meteorolovical 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.The clatafrorn I I streets is dA .65cn alloy.. The bacteria areallowed to grow as fast as possiblefor the first 2. The theoretical growth of bacteriacan be described by Use regression to determinea nrodel to predict c as a function of I. 13. fbr the following on bikelanewidths and average distance between bikes and kineticmodel: passingcars.V irA (a) On cur an{ (b) Re ^^l^.56 0 012 OOoO/5 ModelC i00 determir This rela when or followin Procipifqtion.s/m2 26.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.24 A transportation actor for the reactionA . Flow. (a) Plot (b) Fit r Supt (c) Use the 1 (d) rf th of th ratio use.For some rivers.26 "t) 10. (b) Fit a straightline to the data with linearregression. TL.B. 13.20 lt is known that the tensile strengthof a plastic increases as a tunction of the time it is heat treated.Datawasgathered best possible estimatesfor ft. thepro duction of which slows the bacterial growth significantly.: AT A0le A Time. voirs dependson accurateestimatesof water flow in the We assumethe reaction occurs via a complex mechanism river that is being impounded.3r4 LINEAR REGRESSION (sl& are shown beloll..21 The following data was taken from a stirred tank recngineeringstudy was conductedto 13.use linear regression to find the equationof the line thrt bestfits the dataand the r2 value.Severalmodelshavebeenhypoth. '.00198kcal/mol/K.. After taking the log of the data. responding polyrnerization reaction: 11.' € Viscosiry p.22 Concentration for the minimum lanewidth. ..and p is the specrfic growth rateof the bacteriaduring exponential growth.B:. In esized. T e m p e r o t u r e .1. Model A Model B l3.tB .the sizing of reserxA*.4< t0 550 The stres force in r value car the mast mast's le estimate 13. Te tween stl Slroin.82.historicalrecordso1'such flow data are difficult to obtain.L^ f:r^ ^f rL.27 Tl that mee voltages V. Add this line to the plot. Dislonce. (c) If the minimum safeaverage distance between bikes and passing cars is to be the considered 2 m..
use the data to estimate thedeflectionof a 9m mast. The perature. . Predict the 7o following datais availabletbr a river that is to be dammed: elongationfor a temperature of 400 'C.29 The populationp of a small community on the outskirts of a city grows rapidly over a 2Oyear period: ta p 1OO 5 204 to 450 t5 950 20 2000 *" cm/cm 0 0 0 3 2 0 0 0 4 5 0 0 0 5 5 0 0 0 r 6 0 0 0 8 5 0 0 0 0 5 5lrcss. and D and B are parameters.28 An experiment is performed to determine the Voelonthisrelationship can then be usedto estimate flows for years gation of electricalconductingmaterialas a function of temwhen only precipitation measurementswere made. crosssectional force area. The following data was taken from an experiment 13.519 I 3A7 I 002 O7 9 7 5 A.65 Tests allo1.31 Andrade'secluation has beenproposedas a model of the elfect of temperatureon viscosity: P: PsB/r" where p : dynamic viscosity of water ( l03 N's/m2). (c)Use the besffit line to predict the annual water flow if precipitation the is 120cnr.PROBLEMS 315 determine a relationshipbetween flow and precipitation.5 V. where L : the mast's length. (b)Redo the regressionand force the interceptto be zero.The test resultsare 11.: strain x l. J. Fit a curve to this data assumingthat the velocity is zero at the surface(y : 0).26 area of cmr and is constructedof an experimentalaluminum 10. whereF : in themast and /. estirnate what fraction of theprecipitationis lost via processes such as evaporation. you must forecast the population 5years into the future in order to anticipate the demand for power.6 oC 200 25a 300 375 425 Temperofure. m B B9 l 0 B 5 1 0 4 I lbw m3/s 1 4 6 1 6 7 1 53 1 3 9 .6529 2 52 3 7B 4 lo7 5 t3 7la 1 93 27.The resulting data is listed below. mls 0.A 705r0203040 p I 7B7 I . Al. Superimpose this line on your plot. Plot the line and the data and evaluate the fit. : mast'. 71 2 7 9 4 r 1 6 B 9 9 1 232 195 )61 II I 16.27 measured that the cunent in a wire for various imposed voltages: 13. N/cm2 4970 5170 .This value canthenbe substituted into Hooke's law to determine mast's the deflection.determine cunent for a voltage of 3. 13. Use your resultto determinethe shearstress (du/dy) at the surface. absolutetemperature Fit this model to the following data for water: v.h u.5500 3590 6900 t24A As an engineer working for a utility company. The mast of a sailboat has a crosssectional 13.30 The velocity r of air flowing past a flat surface is measured at severaldistances l away from the surface. ?ncipit o t i o nc .v i. T"Elongotion 75 86 87 lO ll3 475 600 127 153 (a)Plotthedata.002 A 2B7 0006 0 899 o0l2 r9r5 00rB 3048 oo24 4299 thestress caused by wind can be computedasF/A. (d)If rhe drarnage areais I 100km:. were performed to define the relationshipbetween stress and strain. 4 : (K).000 N. If the wind force is 25.5 (a)0n thebasisof a linear regression of this data. 0) Fit a sffaight line to the data with linear regression. and consumptive use. 13. deep groundwaterinfiltration. Employ an exponential model and linear regression to make this prediction.
we will illustratehow optimizationtechniques broadergroup of problerns. Another alternativeis to fit polynomialsto the datausingpolynotnial regression.y:uotal. Understanding how the generallinear leastsquares model can be solvedwith MATLAB usingeitherthe normalequations or left division.As discussed in Chap. Knowing how to implementpolynomial regression.13.1) . For example. a procedurewas developedto derive the equationof a straightline using the leastsquares criterion.lrt. 13.suppose polynomial or quadratic: that we fit a secondorder .l. although exhibiting a marked pattern such as seen in Fig. procedurecan be readily extendedto fit the data to a higherorder The leastsquares polynomial.. one method to acconrplish this objective is to use transformations. GenerolLineorLeostSquores ond NonlineorR"gression CHAPTER OBJECTIVES This chaptertakesthe conceptoffitting a straightline and extends it to (d) fitting a polynomialand (D)fitting a variablethat is a linear function of two or more independent variables. Knowing how to implementmultiple linear regression." l rol.I POTYNOMIAL REGRESSION In Chap. Understanding the formulation of the generallinear leastsquares model.r+ a z r :+ e (14. We will then show how suchapplications can be generalized and applied to a Finally. Some data.f. I4. canbe usedto implementnonlinearregression.Forthesecases. Specilicobjectives and topicscoveredare o o o o .acurvewouldbebetter suited to fit the data.ispoorlyrepresentedbyastraightline. Understanding how to implementnonlinearregression with optimization techniques.
atxi . :: at . t'1*. c'ltt a.us.REGRESSION POLYNOMIAL 317 (b) I4.?) oo+ (T.1)"' + (I'1) tL2: lx!)'i .d1x.I FIGURE ( b )l n d i c o i i otn is h o io p o r o b o l o regression forlineor leostsquores (o)Doto ihotis illsuited preferoble.\r ) \.d0t aS. : t il (y. 2l \\r .u7. we take the derivativeof Eq.nu. d0t : /' .'t. rt .t) n' + (t ti) a2: lxiYi (Ir. . as in coefficients eachof the unknown as.". is of the residuals For this casethe sum of the squares II s.a1x1 . .tl) These equationscan be set equal to zero and reananged to develop the following set of normal equations: z:Lvi z) (n)ao+ (It')ar + (f t''a (I r') oo+ (It.2) fit.xi ()l L r a2xl) \/ rtu.orri)t \14.2) with respectto the leastsquares To generate of the polynomial.a0 .t^sa ) . (14.) : .\'.
.22 1272. twodimensional to an mthorderpolynomial as in casecan be easily extended +.74657 0 I 2 j? 4 5 I 2l 77 t3. anwere usedto computeSr.SQUARES REGRESSION where all summationsare from i : I through n. TABTEl4.aoarx..9 6t t 152.r< /r ^t r J n:6 . The coefficientsof the unknowns calculateddirectly from the observed data.x'' I e ! : aotdlx f a2x2 Thus. polynomial to is equivalent recognize that determining the coefficients of an mthorder enot For this case. The following can be computedfrom the data: \'.: 2488'8 .wecan The foregoinganalysiscan be easily extended to this more generalcase.aS)r 0.l4. a coefficient of determination can also be computed polynomialregression with Eq. (Ji J) tg. at.6 544 44 314..we see that the problem of determininga leastsquares secondorder polynomial is equivalentto solving a systemof three simultaneous The linear equations. n . Note that the precedingthreeequations are can be linear and have three unknowns:a0.1 EXAMPLE Polynomiol Regression polynomial to thedatain thefirst twocolumns ProblemStotement.the standard solving a systemof m I I simultaneous linear equations. fot In addition to the standarderror. for on erroronolysis of the quodrotic leostsguores Compuiotions v.. Fit a secondorder ofTable 14. is formulatedas s.. a a.t 4332 r 00286 r 08r60 0 80487 959 0 61 0 09434 3.1. (13.6 \'lss \ 'Jlls L't Lx!:9tg I".(m I l) because o0.6 272 40.20). . .1) 2 5 r 33 9 Soluiion.l xi fif.(m l1) (14.we have lost z * I degrees of freedom.12 239. thus.)t :585'6 L*?Y.3) (m * l) dataderived coefficientsThis quantity is divided by n .318 AND NONLINEAR GENERAL LINEAR TEAST.47 1 4 00 3 3...r< r)5477 I l'i : 152. anda2. For this case. ctt.
.. tr iil'i.]4.6 535.: / _1.Bl. using MMLAB: >> N = l6 15 55. 8 6 0 7 1 2 The standard error of the estimate based on the regression polynomial is tEq. 4 1 8 6* 2 . For example.the simultaneous linear equations are 319 'i.99925.8507 These equations can be solved to evaluate the coefficients.i\r Therefore.. >> a = l. >> r = 1152.3)l  1. /+b) / :lll75 !orz+ l) ""' The coefficientof determination is r . FIGURE I4.6 24BB.l:hfx 2 .15 55 225.41 B6 2. 3 5 9 3 r * 1 .39 and the comelation coefficient is r :0. 1 POIYNOMIAL REGRESSION Therefore.39 :0.3593 1.2 Filof o second orderpolvnomiol .55 225 9j9).74651 2513.99851 2513. the leastsquaresquadratic equation for this case is ! : 2 . (14.3.
or more independent For example..(t0 a1xl.cttxt.This resultsupports the conclusion that the quadratic equation represents an excellent fit.320 LINEAR GENERAL LEASTSAUARES AND NONLINEAR REGRESSION These results indicate that 99.t 1 1 .3).t t 1 1 .ao(rtl. 14.i()i .ao .2.. x 1 .i * azxz. ( l ' i . asin la2xzas .i) .r.2^ .:.1o 2 x 2 .: OQ) 2 ^ \ ) . ___!_ // . andxr.2 'T4UITIPIE TINEAR REGRESSION Another useful extensionof linearregression is the casewheref is a linear functionof two variables. .the "best" values of the coefficientsare determined by formulating the sum of the squares of the residuals: $ : i ()'i .az.0"i .851 percent of the original uncertaintyhas beenexplainedby the model. . as is alsoevidentfrom Fig.3 Grophicol depiction y iso lineor of multiple lineor regression where function of. the "line" becomes regression a "plane"(Fig. 14.y:rro{ct1t1 Such an equationis particularly useful when fitting experimental data where the variable being studiedis often a function of two other variables.i . As with the previous cases.r2. 1 might be a linear function of r.i .1) and differentiatingwith respectto eachof the unknown coefficients: aS.:l) dao _\r. as. i l as. 14.i) FIGURE I4.azxz.ond rr.: d L ll 11 ) xt. For this twodimensional case.rt.i)? EXA (r4.
.5 rll [ 6 1 6 .2." + arx^ * e ) : a oI a 1 x 1 TABTE I o:? c:Tp':olio1'. Irr. I .3r2'.oti:... Substituting them into Eq.rilr 0 20 225 0 l2 t89 )77 x2y 0 t0 IB 0 ]B \. ::q. _..5)gives 16. 2 s4 8  I 48 54) L t4 which can be solvedfor (10:5 at:4 a0 Al A1 54 243. iI l a r I [ nt L I tr..?..2 MULTIPLE LINEAR REGRESSION 32r The coefficientsyielding the minimum sum of the squares of the residuals are obtainedby settingthe partial derivativesequal to zero and expressing the resultin matrix form as I*'. The foregoing twodimensionalcasecan be easily extendedto m dimensions.4 o 2 5 3 24 l4 0) T6 lao .5 100 (14. The following data was calculated from the equation y : 5 + 4x1.I4.rt. I " t i .5) are computedin Table 14.5) Problem Sfotement.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. i r z .rt x2 00s 2 2.. ' I x r .6) aZ:3 which is consistent with the original equationfrom which the datawas derived.525 r30 463 7227 I t0 Use multiple linear regression to fit this data.1 EXAMPLE 14.:'i':d dey3lon '::::l fo1 Elomele 1o li:.:"". 5 1 6 .l) xl 0 l 4 I 36 4 J4 xrx2 .r: (14.as in * a2x2+.rr. (1a. Solution. (14.2 Multiple Lineor Regression l .r Ir..25 I t6 49 . The summationsrequired to develop Eq./a.. Ifoo f n I " ' t . .
" )' : ctox'. As in the caseof in sinusoids. the a's.t2= l..zm I € (14. zl. [::: a.t . a simplelookingmodel suchas ): do(l eol') into the format of Eq. is.322 LINEAR LEASTSQUARES AND NONLINEAR REGRESSION GENERAL error is formulatedas where the standard s n(nt+D is computedas in Eq. ( 14.. . parametersthat is. .the :'s can be y : ao * a1 cos(a. and the coefficientof determination wherea variableis linearly relatedto two or mole be certaincases Aithough therernary has additionalutility in the derivationof power other variables.7) canbe expressed [y]:fzl{a}+lel (14.. I4. .For example. and multiple linear. t t : x 2 . all three belong to the following general model: leastsquares l''' ) ' : a o z oI a r z t * a y Z z I an. :16m.r) I azsin(o_tx) Such a format is the basisof Fourier analysis. m o n o m i a la s si n : o : l . . linear polynomial. Further. as selvescan be highly nonlinear. :.l ..rl.To usemultiplelineat Areextremelyusefulwhen fitting experimental Suchequations by taking its logarithm to yield the equationis transformed regression. In the preceding paqes.7).xm.. On the other hand. r : x .3 LEAST SQUARES TINEAR GENERAT we have introduced three types of regression:simple linear.s: and multiple linear regression if the basisfunctionsate polynomialregression is alsoincluded x2.. ( 13.:.. fall within this modelthat zr .marem * I basisfunctions.. . z. Znt:. it cannotbe manipulated is truly nonlinearbecause in matrix notationas Equation(14. 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 . . Z p 1 simpfe on its "linear" to the model's dependence refers only Note that the terminology polynomial the functions them' regression. In fact.?) linear It can easilybe seenhow simple where :0.t data.' .multiple linear regression equations of the generalform x. l o g .8) EXA values valuesof the basisfunctionsat the measured where[Zl is a matrix of the calculated variables: of the independent zll t7l tt :*tf . x::i. .20).
1 1 3 . gives rl I(vr . Repeat Example14.3 GENERAL LINEAR LEAST SAUARES 323 where m is the number of variablesin the model and n is the number of data points.ti t2 where! : the predictionof the leastsquares fit. The coefficientof determination and the standard error can alsobe formulatedin terms of matrix algebra. you shouldrecognize that most of the time. il / lo1r1.l0) is. but usematrixoperations asdescribed in this section.r .vr !2. 9 6 1 .) and the column vector {e} containsthe residuals: {e}r : le1 e2 e u) The sum of the squares of the residuals for this model can be definedas n s.polynomial.:Ilt. Recallthat r: is definedas t S. > > y = 1 2 .9) \ This quantity can be minimized by taking its partial derivativewith respectto eachof the coefficientsand settingthe resultingequationequalto zero. Substitutingthe definitionsof S. can be expressed in vector form as {y} .$  _ _S .l j=tt / . (14.f"zlla} Matrix algebracan then be usedto manipulatethis vector to computeboth the coefficient of determination and the standard error of the estimate as illustratedin the following example. I 1 .. . [Z] is not a square marrix.3 P o l y n o m i oR l egressiow n ith MATLAB ProblemSiotement.S.14.enterthe datato be fit >> x .equivalenf to the normalequations developed previouslyfor simple linear. in fact.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. First.1. EXAMPLE I4. 2 4 A . 6 2 ' 7.urt' I(). The residualsbetweenthe bestfit curve and the data. Solution. and S.t0 12 3 4 5l'. . and multiple linear regression.0 ) It can be shownthat Eq. \2 (14.  s.y. Becattse n > m * 1. 1 1 ' . The column vector {y} containsthe observed valuesofthe dependent variable: {v}r : L.j. }nl The column vector ia] containsthe unknown coefficients: {a)r : la11 at a . .
47 B6 2.SQUARESAND NONI\NEAR RIGRESS\ON GENERAT Next.998s and sr.'t 466 Then r2 can be computed as > > 1 2 = I . first compute the sum of the squares of the residuals: > > S r = s u m( ( y . (14.l0): coefficientsof the leastsquares >> a = \Z'*Z) \(Z'*y) 2 .I .3593 1.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 in Example 14. We can solvefor the quadraticby implementingEq. . create the [Z] matnx: >> Z = [ones(slze(x) ) x x.8607 In order to compute 12 and s"7.5.m e a n ( y ) ) .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.324 L\NEARIEASI.Z * a ) . can be computed as >> syx sqrt (Sr/ (lenqth (x) lenqth (a) ) ) 1.S r / s u m( ( y . (1a.. It also sets the stage for the next section where we will gain some insights into the preferred strategies for solving Eq. ^ 2 ) 0. .7.^2] 100 111 1. The matrix notation will also have relevance when we tum to nonlinear regression in Section 14. ^ 2 ) EX 3 .10).
As in Example 14. I l ' [ones (size (x)) x x. 2 4 A . 9 6 I . it . 1 1 3 .it automatically uses that you want to solvesuchsystems how this is done.. howcanbe illconditionedandhencesensitive to roundoff errors.the datacan be enteredand usedto createthe lZl matrix as in >> x = >> y = >> z = The potyf 3 4 5l'. Further.. Solution. for many curvefittingapplications in engineering and science. for caseswhere you want to fit a polynomial.3593 2. both these results are obtained autornatically with QR factorization.3. are more robustin this regard. 4 lmplementing P o l y n o m i oR l e g r e s s i ow n i t h p o .we usedleft divisionwith thebackslash operator to solvesystems of linearalgebraicequations wherethe numberof equations equalsthe numberof unknowns(n : n). the generallinear leastsquares Recallthat the general modelis formulated as Eq.41 86 2.tlrat the normal equations Two more advanced methods. 6 2 1 .V. to obtainthe solution.4 QR FACTORIZATION AND THEBACKSIASH OPERATOR is widely usedand certainlyadequate Generating a bestfit by solving the normal equations It must be mentioned. problemcan be directly solvedwith the backSecond. Ieastsquares. 1 y y: l Z l { a l ( 1 4I.4 AND THEBACKSLASH OPERATOR QR FACTORIZATION 32s 14. lA I2 1 2. l ) In Section10.but use the builrin polyf ir functionand left division to calculatethe coefficients.I4.QR factorizationis automaticallyusedin two simple ways within MATLAB.2) 1.8607 2 .2).3593 1 8607 As just stated. function can be used to compute the coefficients: >> a = polyf it \x.3.4. (14. I 1 .Although the descriptionof thesemethodsis beyondthe scope of this text.QRfactorization andsingular valuedecomposition. ever. we mention them herebecause they can be implementedwith MATLAB. the builtin polyf it function automatically usesQR factorizationto obtain its results. First. ( 14.8) slashoperator.The following exampleillustrates QR factorization E X A M P L1 E4 .> m).41 B6 The sameresult can also be calculated using the backslash: >>a=Z\y 2 . For Eq. y i j t o n d L e f tD i v i s i o n Problem Stotemeni. . MATLAB senses with left division. RepeatExample 14.Such systemsare said to be overdetermined.8)as derivedfiom general the numberof equations is greaterthan When the number of unknowns (n.
previously As described in Sec..2002).p2. theory can be usedto obtain new estimates of the parameters that move in the directionof minimizing the residual. . De(Chapraand Canale.5 NONTINEAR REGRESSION There are many casesin engineeringand sciencewhere nonlinearmodels must be fit to data.This yielded the model: e\a2 F : 0.7).r3) An optimizationroutine can then be usedto determinethe valuesof ae and a1 that minimize the function.. For example.If you would like to passadditional (p1.Eq. f va7 = : the value of the function at the minimum. useempty brackets[] asa arguments place holder. (la. and pl.4).: additionalarguments that are passed to the objectivefunction.2 1) This equationcannotbe manipulatedso that it conformsto the generalform of Eq.the GaussNewtonmethod uses a Taylor seriesexpansionto expressthe original nonlinear linear form.2741ut Repeat this exercise. . . . As with linear least squares. x0 a vector of the initial guesses for the parameters.7.for the nonlinearcase.. p2.. (14. denceon their oarameters.. EXAMPLE 14..thesemodelsaredefinedas thosethat have a nonlineardepenFor example.326 LEASTSAUARES AND NONLINEAR GENERAL LINEAR REGRESSION I4. There are techniquesexpresslydesignedfor nonlinearregression. v:ao(l rat"r)*e ( 1 4 . Note that if options is omitted.In the presentcontext.However.MATLAB uses default valuesthat are reasonable for most problems.Employ initial guesses of I for the coefficients.a t r ' 1 1 2 (t4.l by linearizationusing logarithms. p2.) wherex = a vector of the valuesof the parameters that minimize the function f un. Recallthat in Example13.the solution must proceedin an iterativefashion. options.5 N o n l i n e o rR e g r e s s i ow n ith MATLAB Problem Stotemeni.we fit the power model to datafrom Table l3.pl.3. For example.options: a structure containingvaluesof the optimizationparameters as created with the optimset function (recall Section6.a o \ . but do not want to setthe aptions.).but use nonlinear regression.It has the generalsyntax lx.1. fvaLl = fmlnsearch(fun. xa. nonlinearregression is basedon determiningthe values of the parameters that minimize the sum of the squares of the residuals.l2) can be expressed as an objectivefunction to computethe sum of the squares: n f ( a q . etc. tails on this approachare providedelsewhere An alternative is to useoptimizationtechniques to directly determine the leastsquares fit.MATLAB's fminsearch functioncanbe used for this purpose.4. Then leastsquares equationin an approximate.a 1 ) : ) [t.
it is difficult to judge which fit is superiorbasedon inspectionof the plot.14. >> y . m/s .^a(2).y p ) .a a a ta . m. mizes the residualsof the transformed FIGURE I4.is set up for the power equation: function f = fSSR(a.[1. In commandmode.4 versus velocity ondunlronsformed model fits forforce dctofrom Comporison of tronsformed ODIE J a .I4. 2.4. The following file.125 7A 380 550 610 1220 830 14501.xm.ym) yp = a(1)*xm. ^ 2 ) . f = s u m (( y m .4359 tL x. y) The besrfit model is therefore a35e F : 2. This example illustrateshow different bestfit equations result when fitting the same employingtransformations. called f ssR.5 NONLINEAR REGRESSION 327 Solution. The minimization of the function is then implemented by >> fminsearch (@fSSR. Note that althoughthe model coefficientsarevery different. 1] . an Mfile function must be createdto computethe sum of the squares.5384 r. This model using nonlinearregression versuslinear regression whereas latter miniis because former minimizes the residuals of the orisinal data the the data. z { 800 Untransformed 4 40 u.5384u1 Both the original transformedfit and the presentversion are displayedili'Frg. First. the datacan be enteredas >> x = t10 20 30 40 50 60 7O B0l.
5 s).5a).C. In this discussion. : 0 866.sin{arol).5 ond B. 14. specifies the height of the F I G U R EI 4 . Engineersand scientistsoften deal with systemsthat oscillateor vibrate.14) Thus.where curvein {o).The amplitude C. on l ction}: { o }A p l o t o ft h e s i n u s o i d ao: 2n/l 1.There is no clearcut convention for choosing either function.7. For this chapter. The mean valued sets the average height above the abscissa.we will use the cosine. =1.=).328 LINEAR LEASTSAUARES AND NONLINEAR REGRESSION GENERAL F IT T INS GIN U S OIDs Bockground. 5 . : 0.ond 0 : r/3 rodions(b)An olternotive expression of thesome curve is in Thethree components of thisfunction oredepicted * B. cl t. fad . I(r) : Ao + A) + A. o r t h i s c o sA fu (A r )o * C . and in any case.Thesummotion of thethree curves thesingle {b).the results will be identical. c o s ( a r o r * 9 ) F en .which is expressedgenerally as * d) f (t) : As * Cv cos(arst (14.cos{coor} in (b)yields A.As might be expected.s at. four parametersserve to characterizethe sinusoid (Fig.sinusoidalfunctions play a fundamentalrole in modelingsuch problem contexts. we will usethe term sinusoidto represent any waveformthatcanh describedas a sine or cosine.
7).3 AM 27. Squaringand summingthe two partsof Eq. 2 3 6 .4characterizes phase angle.r7) Dividing the two partsof Eq.15) rT (14.I4.9 .f and frequency in turn is related to period I (in units of time) by I (14. This means that it is not in the form of the general linear leastsquares model (Eq. (14. (14.J lB9 F 2 1 .19) leadsto (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. the oscillation. can be simply applied as the basis for a leastsquares The average monthly maximum air temperaturesfor Tucson. The angular frequency (in radians/time) is related to frequency/(in cycles/time) by oo :2r. Finally.I S 1.sin(cosr) Cr cos(abl + d) : C1[cos(rrrst) SubstitutingEq. (14.Eq.7).3 r8.8 A J 3 7 . U. It can be measuredas the distancein radiansfrom / : 0 to the point at which the cosine function begins a new cycle.r6) Although Eq.14) is an adequatemathematicalcharacterizationof a sinusoid.2r) an alternative formulationof Eq.18)represents four parametersbut that is cast in the format of a general linear model (Eq. it fit. Aizona. The angular frequency o.4 23.17)into Eq.20) where if A r < 0. add z to 9.8 ) (r4.19)gives / B'\ 0 : arctan  \ Arl (14.5b) * 81 sin(arer) f (t) : Ao * Ar cos(alol) where Ar : Ct cos(0) Br : Cr sin(9) (14. (14.14)and collectingtermsgives (Fig.19) ( 1 4l. (14. 4. 14. (14. or phase shift 0 parameterizesthe extent to which the sinusoid is shifted horizontally. and we must use nonlinear regressionto estimate the coefficients.1 ^ 32. This deficiency can be overcome by invoking the trigonometric identity: sin(p)] cos(p). 1 23. Thus.2 OND 2e. have been tabulated as Month T'"C .14)that still requires Thus. ( 1a.6 CASE STUDY 329 continued how often the cycles occur.
^ 2 ) 0.T.Tp) The fit. 14. a (2J ) *360 i (2*pi ) theta = L63. Assuming each month is 30 days long. >> plot (t.m e a n ( T ) ) .2 36.2 31 .8 32. fit a sinusoid to this data. The coefficients for the leastsquares fit can then be computed as >> a= lZ'*Z ) \ (Z'*T) 28.20) and (14. Solution. we can enter the data and createthe 23.2s59 .an2 (a (3 ) .4 z= [ones(size(t)) cos(w0*L) sin(w0*t)].14): >> theta=at.4 29.r6'76 .8972 lengch(a) ) ) A plot can be developed as >> tp=[0:360].'o'.1. tp. BA02 The statistics can also be determined as >> Sr=sum((TZ*a) . which is displayed in Fig. 34.4398 >> r2=ISr / s u m( ( T . In a similar fashion to Example 14. t=t15 45 75 105 135 165 225 255 285 315 3451'.3 27.^2) 6 .21) can be used to express the bestfit sinusoid in themore descriptive format of Eq.330 G E N E R AL LI N E A R LEASTSQUAR AE NS D NONTINEAREGRESSION continued Observe that the July value is missing.r 23. r=118.3 18.3. Equations (14.381 B 9 . (14.9862 >> syx=sqrt (Sr/ (length(t) syx = 0.9 2r. >> Tp=a (1)+a (2 ) *cos (w0*tp)+a (3 ) *sin (w0*tp) . generally describes the data trends. lZl matrix: >> >> >> >> vrO=2*pi/360. Use the resulting equation to predict the value in midJuly.2 .6.91'.
the phase shift is expressed in days rather than in radians.t r + 1 6 3 . The value in midJuly (/ : 195 d) can thereforebe computed as > > T p J u l = a ( 1 ) + C 1 * c o s( w 0 * ( 1 9 5 + t h e t a ) ) TpJul = 38. Therefore. the maximum monthly temperaturein Tucson rangesfrom about 18.67 A2 Note that because it is more physically meaningful. 1 6 76 ) \Jbu / We can see that the mean is 28.1 'C. and the amplitude is 9. Hence. 3 8 7 8+ 9 . which translates to about day 197 (July 17).CASESTUDY 331 continued 15 300 350 FIGURE I4. The phaseshift tells us that the peak temperatureoccurs about 163 days prior to the start of the year. 6 7 0 2 c os* .3878.6702. the final model is /2n \ R : 2 8 . Arizono >> C1=sert (a(2 ) ^2+a (3) ^2) 9 .7 to 38.6 Leostsquores fitof o sinusoid to temperoture dotoforTucson.0530 .
l4.1.rr P.09 rng/L. m3/s . 14. 14.e t27 where maxin radiati UtE n Iru./r(c) Estim and C t p(t) 14.5.. D : diameter.squares approachto fit tlis model to the data in TablePl4.9 Thc lbltowing datawas collectedfbr the steady flowof u a(erin a concretc cilcularpipe: Experiment Diomeler. C C C t l t( 5( 45 2B t3 r5 20 25 0r 909 B.8 Use multiple linear regression to fit xrO1)223344 I x2 0 2 y 151 t7. lzl. Note that thetrue valuc is 9.15)and (13. Explain possible causes fot the discrepancy.6 Along with the coefficients.9 It3 r0 t 903 B.a Dissolved Oxygen (mglt) for Iemperolure ('C) ond Concenlrotion of Chloride (g/t) T. Use the general linear least.6..a11D"' 5"' where Q : flow. 14. Computethepercent relative error for vour prediction. Test the approach by using it 1(Jfit the data fiom T a b l e1 3 .2 Using the same approachas was employed to derive Eqs.determinerr and s.16).9 03 06 O9 0. m/m Flow.3 Fit a cubic polynornial to the following data: Note that the true value is 9.1 Fit a parabolato the data lrom Table 13. t l i r+ a 2 .and S: slope.3 06 09 0 00t 0 001 0 00r 001 00r 0 0t 005 005 005 aa4 4. That is.5.derive the leastsquares fit of the following model: _ \ ' : . the standard error of the estimate. teriz exan U( wher subs The lsl.7_.3.Determine the r2 for the fit and comment on the efficacy of the result. 1 . determinethe coefficientsthat reslllts in the leastpolynomial with a zero intersquares fit for a secondorder cept.r34 16 J 36 578 44 34 2.2 9 28 t) 3B )2 4. b" r5( P9( 14. a somewhatmore sophisticated model that accounts fbr the eflect of both temperatureand chloride on dissolved oxygen saturation can be hypothesized as beingof theform o: hG) *.5 For the data from Table P14. solar r .rU.1.14.7 As comparedwith the rnodelsfiom Probs. a thirdorderpolynomial in temperature anda linear relationshipin chloride is assumed to yield superior results. 14.Employ a polynomial that is of sufficiently high order that the predictions match the numberof significantdigits displayedin the table. m Slope.3 195 566 14.6 Use multiple linear regressionto derive a predictive equationfbr dissolvedoxygen concentration as a function of temperature and chloridebasedon the dataliom TableP14. Test it by solving Prob.l7 746 A e5 114 t0 3 B96 BOB 735 573 6)O Use multiple linear regression to fit the fbllowing model to this data: Q . 14. 14.5.5 Dissolvedoxygen concentrotionin woier os o function of lemoerofure ("C) ond chloride concentrotion(g/L). Use the equationto estimatethe concentration of dissolved oxygenfora chloride concentration of 15 glL atT : 12"C. Computethe coefTicients.332 G E N E R AL TI N E A R LEASTSQUAR AE NS D NONLINEAR EGRESSION PROBLEMS 14. "C 0 5 t0 c:OglL c=lOg/L c=20g/L l 2 3 4 5 6 7 B I 03 06 0.09 mg/L. (13.5 and 14. Compute the percentrelativeenorfor your prediction.4 Develop an Mfile to implement polvnomial regression. 14. TABTE P14. t 2 + e That is. Passthe Mfile two vectorsholding the r and ) values along with the desiredorder rn. 14. Use the resulting equadon to estimate the dissolvedoxygenconcentration for a chloride concentrationof 15 g/L aI T : 12 'C.24 069 0 13 a82 238 r 0.use polynornialrellession to derive a predictive equation for dissolved oxygen as a function of temperature lor the casewhere concentration the chlorideconcentration is equalto zero.ll) Three diseasecanyingorganisms decay exponentially in scawater accordingto the following model: .26 12.1 and fi I 1 2 2 2 I 256 205 35 I 297 454442 User follor . and the conelation coefticient.
1 .)a5 2430xlC' 2431 x la' 2 .u. and 1.M uo.logarithmic./: optimal solar radiation radiation rsl). and Ar) fbr the followthe bestpossiblcvirial constants ing equation of state. s / m 2 .)75 0 6529 lsl. P.r ' .*t I . Display the data and the rnodelfit on a grapn.5 (a) Plot this data. \ A (a) Use a transfbrnration to linearizethe nrodeland evaluate thc paramcters..For (b) and (c).r T a b l e1 3 .9 7 1.787 I5t9 t0 I3Q7 20 I002 30 4a a 7... 1 7 D y n a m i cv i s c o s i t y o f w a t e rp ( 1 0 3 N . Tr)r several regression nonlinear to estimateaa arrdp.. 1 3 1. IS]3 ''K+[. I. 14. T substrate Ihefollowingdata can be tit with this modcl: lL 0 5 i. and (d) a parabola. 7 3 7x l O ' 2423v. M/s 6 O Z Bx l O ') 7. employ transfbnnations to Iincarizc the data. (b) Use linearintcrpolatior.ii 6 1..\:lazx2+e. 1 4 ..f': l]1. 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.13 Enzymaticrcactionsarc usetl extcusively to characterize biologicallyrnediatedreactions. t O 5 cc r 005 0l 0\ rc 50 r00 .14 Givcn thc data I i I t l4. (b) Pcrforrn thc sanre evaluation as in (a) bul usenonlinear regrcsslon.r basedon the possibilitiespolynomial.. . [S] : tne concentration(M).8 we used transtbrmations 106 fit thefollowing moclel: and )' B . and I : r03 K. and ft. Determinethc bestequationto predict the amouut. and lpEm based 1. Ii..15 The following datarepresents the bacterialgrorvthin a liquid cultureover of numberof days: Doy Amounl x to linearrze l{. on the fbllowing data: 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 usc leastsquares to {lt (a) a straightlinc.18 Use the following setof pressurevolume datato find (A.The following is rn of a model that is usedto fit srrch reactions: example k.7 the initial concentrationof cach organisrn(A. a2 04 06 oe r3 r5 t7 IB 14. Use data. : the photosynthcsis maximum ratc (mg mid l. PV At At .595x la r l0 3 6C)63 5ZBBxlOr' 1 .t 4 2 5 1.5 "C. following of bacteriu atier 30 days.05rnL atm/gnlol K. and exponential. equirtion.Plot the dataalongwith all the curves..Is arly one of fhe curves superior?If so.r to predictp at T :7 . B.. 1 : solar (lrE nr 2s l).). where P : the photosynthesis rate 1mg m 3dl).16 Derive the lcastsquares flt ol the fbllowing model: 1 4 ..and K are paramcters. (b) a (c) a saturationgrowthrate power equation.rme 14.tll .12In Prob. Usc nonlinear rcgression to evaluate P. E$imate C)siventhe followins mcaslrremcnts: and r f\t 05 l AA '2 )) 3 . determinethe coefficientsthat resultsin the leasl squares tit tbr a secondorderpolyuomial with a zero intercept.. 14.n1't g' a 4 B t2 t6 20 67 38 7467 8274 9l69 rot 6C l]2 58 Find a bestflt equation to the data trend.. That is.here u0: the initial rate of the reaction (M/s).1. i s l c I('C)in the ftrllowinglranner: latecl to telnperaturc u. (c) Usepolynornialregression to lit a parabola to thc data in prediction.. R : 82.4 9 Ll .Test thc approachby using it to fil the data fron.ll Thefollowing model is uscdto represent the efl'cctol plants: r fadiation on the photosynthesis ratc of aquatic solar I P=P.Developa plot ol your fit along with thc da1a.jLrstify. I3. . order lo make the si.PROBLEMS rii .
and a. I .5 l2 3 60 75 90 ltt 145 )67 2 l .. b. I cT. Employ the fbllowing data and rcgression to estimate the parameters: r (K) 0 t0 2A 30 4A K.204 I 363 rB000 163t tions fbr both the thinking and braking components. as a lunction of temperature. and d are where I. Use the resulting equationto predict the radiatron rn midAugust.km/h Thinking. amplitude. Scientists have sr.The fbllowing cxperimentaldata was Assumingeach month is 30 days long. h 0 2 4 5 7 I l2 t5 2A 2224 pH 76 72 7 6. mo F OND I Rodiotion. Use your fit to determinethe mean. fime.5 7.22The solar radiation fbr Tucson.0 45 8.' 1. . Arizona.334 GENERAL LINEAR LEASTSAUARES AND NONLINEAR REGRESSION collectedto quantify this relationship.. 1 6 4x 2950x 6846x 1467x 2 .l o 9 1 6K .m B r o k i n g . I d ll. 9 2 9x t0 : l0 r5 l0r5 t0' t0. eachof which is a function of its speed. 9 4 76 t2A 224 847 * D log. o 3 2 . : absolutetemperature parameters. Use r 5 0 0 0 these l. fit a sinusoidto this data. a Speed.o 7. has been tabulated as Time.99t BS 79 7 (K). 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.20 The distancerequircd to stop an automobileconsists of both thinking and braking components.2l The pH in a reactor varies over the courseof a day.l. P (orm) V (mt) O 9B5 2saoo I t0B 22. c..rggested the following equation to model this relationshio: .l9 Environmentalscientistsand engineersdealing with thc impacts of acid rain must detcrmine the value of the ion productof water K. and time of maximumpH. Use leastsquares regression to fit a sinusoidto the following data. Developbestfitequaequationsto estimatethe total stoppingdistance for a car travclingat ll0 km/h.m 30 56 5.1.5 72 8.
you would have to estimatethe value at the .you would have to interpolate. 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 .In such a case. Specific objectivesand topics coveredare ' ' ' ' ' ' ' polynonrial Recognizing that evaluating with simultaneous coefficients equations is an illconditionedproblem.Table 15.. Air density and viscosity are commonly presented in tabular form as a function of temperature. Knowing how to pelfbrm an interpolationwith Newton's polynomial.: r"it'liii Polynomiol Interpolotion CHAPTER OBJECTIVES The primary objectiveof this chapteris to introduceyou to polynomial interpolation.7. Knowing how to solve an inverseinterpolationproblem by recastingit as a roots problem.1is reprintedfrom a popularfluid mechanics textbook (Whire. For example. Suppose that you desiredthe densityat a temperature not includedin the table.the drag coefficientcan itself be formulatedas a function of other factors such as the areaof the jumper and characteristics such as the air's density and viscosity. As I I was previouslymentionedin Section2. 1999). Recognizingthat higherorder polynomialscan manifest largeoscillations. Appreciatingthe dangersof extrapolation. we might f we want to improve the velocitypredictionfor the freefallingbungee I expandour model to accountfor other fhctorsbeyondmassand the drag coefTicient.'. YOU'VE GOT A PROBTEM jumper. Knowing how to pertbrm an interpolationwith a Lagrangepolynomial. That is.
j 4 75 x lA': 6 20 x lOj 777 xla5 TABTEl5.t' 1 1 t . s/m2 v . For example. we should note that MATLAB reprcsents (15. The simplestapproach is t0 determine the equationfor the straightlirre connectingthe two adjacentvaluesandusethls Althoughsuch equation to estimatethe density at the desiredintennediatetemperature. computeintermediate polynomialcoefficients Before proceeding.38 x 2 .there is only one straightline (i. it uses Ratherthan usingincreasing decreasingpowers as in I '+'.r. (15.only one parabola connects a setofthree points (Fig.99x l0 r 1 33 x l0r j l . 5 Ox l 0 l /9 x 105 230xl0j 2 85 x l05 3 . N .1). 4 5x l 0 ..4 with MATLAB.j 4 O Bx l O .1 t I p . l5. Polltnomialinterpolationconsists of determiningthe unique@ . The generalformula for an (a . we will adontthis schemein the followins section. Similarly. The most common method usedfor this purposeis polynonial interpolation.I INTRODUCTION TO INTERPOTATION You will frequently have occasionto estimateintermediatevalues betweenprecise data points. p. in a different mannerthan Eq. powersof .'C /A 0 2A 50 100 150 200 254 300 400 500 52 29 2A 09 4. 1 7x 2.f(x):ar*azxla3x2 +. 5 7x 2 75 X / YJ lO 5 lO 5 l0 5 lO r lO5 l0 5 lOr IU' x l0 r X ]U U' 355 X basedon the densities desiredtemperature that bracketit. when linear interpolationis perfectlyadequate in many cases.we will explore a numberof different for obtainingadequate estimates for suchsituations.thereis one andonly onepolynomialof order (n .l) that passes through all the points.946 0 835 0 746 0 675 06r6 0 525 a.. error can be introduced the data exhibits significantcurvature.In this chapter.457 5t x 7l x B Ox 95 x 2 . m2ls 0.1a).l)thorder polynomialcan be written as .'+erx"t (15. f (x): plx" I p:.e. . ond kinemotic Density viscosily (u)os o functiono[ viscosity (f) ot I otm os reported temperoiure by White (1999). a firstorderpolynomial) that connects two points(Fig. To be consistent .l)thorder polynomial that fits n data points. This polynomial then provides a formula to values. .336 POLYNOMIAL INTERPOLATION (p).kg/m3 1t.lr). approaches EXA I5.dynomic (p).t T.1) For n datapoints.. 15.
0 0o 0ot l l n z  : l o . s z s ] 4 I p.616: pt(30q2 + p2Q00) + p3 0.15.000 J 10.o r d e r ub'cl { b ) s e c o r d . \ pl. la :ble .l Defermining Polynomiol Coefficients A straightforward way for computingthe coefficientsof Eq.4s7  Thus. .oro I oo.o r d e ' { o u o d ' o lo {c conneciinoiour ooints. Suppose that we want to determine the coefficients of the parabola.t21 p2+ rr : 300 "f(rr) : 0. As in the following example.2)to yield a system of three equations. E X A M P L1 E5 . l5.thatpassesthroughthelastthreedensityvaluesfrom l 5T . 0. s00 lJ I 1250.2)is basedon the fact that n datapoints are requiredto determinethe n coefficients. (15. (15. [ o.I (o) firslorder Exomples of nierpoloting (lineor) polynomiols: connecfing two poinis. l Determining P o l y n o m i oC l oefficientw s i t h S i m u l t o n e o uE s quotions Problem Stotemeni.525: p1@00)z + p2(400) + p3 l pt t p2600) 0. the problem reducesto solving three simultaneous linear algebraicequationsfor the three unknown coefficients. 4 5 ' 7 pairscanbe substituted Eachof these into Eq. f( x ) : 0 .this allows us to generate n linear algebraicequations that we can solve simultaneously for the coefficients. I INTRODUCTION TO INTERPOLATION 337 FIGUREI5. r : : 5 0 0 .ooo I 1 6 0 . :6 ' o o ' o b o l i c )c o n n e c r i n t g L r e ep o i n t s o n d { c )r h i r d .6t6 xz:400 f(x):0.451: pr(500)2 or in matrixform: 3oo rl I p.l.A simple MATLAB sessioncan be used to obtain the .f (x): pt.525 .
001715.00171s00000000 r.338 INTERPOLATION POLYNOMIAL solution: >> >> format long . >> A I.1 provides an easy way to performinterpolation.For example.00000115000000 0.451 )'. This can be seenclearlyby expressing it ingeneral terms: t'.To understand this flaw. their solutionsarevery sensitive to roundoffenon. Before doing this. r 2 This polynomial then providesa meansto determineintermediate points. notice that the coefficient matrix in Examplel5.r I J l"i rr I I lr.250000 500 1l.1as >> cond (A) 5. the value of densityat a temperature of 350 oC can be calculated as 2 0 . we will first briefly review how the coefficients of the interpolating polynomial can be estimated directlv with MAILAB's builrin functions. however. there are altemative approaches that do not nranifbst rhis shortcoming.8912e+a)0t.021 + f ( . As a consequence. This condition number. The illconditioning becomes even worse asthe number of simultaneous equations becomes larger.tr) ll } i\l:{ ilii Coefficient rnatricesof this tbrm arc refeflrd to as Vctndennonde nntrices. it has a seriousdeficiency. x ): 0 .'. In this chapter. 0 0 1 7 t 5 ( 3 5 0 :0.00000115(350) Although the approachin Example 15. the parabolathat passes exactly throughthe threepoints is . implies that about six digits of the solution would be questionable.l Lxi .561625 +) 1 . >>P=A\b 0. we will also describe two altematives tlrat are wellsuited for computer implementation: the Newton and the Lagrange polynomials. 0 0 0 0 0 1 1 5 . which is quite large for a 3 x 3 matrix. 0 2 7 /(350) :0.525 0.6r6 4. .190000 3tl0 1. Such matrices are very illconditioned.42100ct00000000 Thus.l has a decided structure. r 1.160000 404 b = [0.That is.r. This can be illustratedby using MATLAB to computethe conditionnumberfor thecoefficient matrixfrom Examole15.0.
the leastsquares fit line doesnot necessarily coefficientsbeing estimated. f it performs interpolation.2. 15. po ly For the casewherethe numberof datapointsequalsthe numberof coefficients.1: >> format longt >> r: [300 400 500].and secondorder of their simple visual interpretation.1) which can be rearranged to yield fr?):f(x)+ f(xr) f(. passthrough any of the points.2 NEWTONINTERPOTATING POTYNOMIAT forms for expressing There are a variety of alternative an interpolatingpolynomial beyond (15. Using similar triansles. In such applications.2 MATTAB Functions: polyf it dlrd potyval 339 Recall from Section 13.called linear interpolation.I 5.4.that the polyf it function can be usedto perform polynomial regression.2 NEWTONINTERPOLATING POLYNOMIAL 15.1.OOOOOll5OOOOI]O We can then use the polyval >> d = polyval d.is depictedgraphicallyin Fig.56762500000000 These results agree with those obtainedpreviously in Example l5.2..l with simultaneous equations.OO1715OOOOOOOO I.616 0. the familiar format of Eq.rr) x2*xl (1 s . 15. >> density = i0. the number of data points is greaterthan the number of Consequently.02'IOOOOOT]OOOOO O. 15. 350) 0.451).2. fi(r) . Newton's interpolatingpolynomial is among the most popular and useful forms.it can be usedto determinethe coefficients of the parabolathat passes throughthe last threedensityvaluesfrom Table 15. For example.we will introducethe versionsbecause first.1 lineor Interpolotion The simplestform of interpolationis to connecttwo datapoints with a straightline.525 0. Before presentingthe generalequation. The notation /r (r) designatesthat this is a firstorder interpolatingpolynomial.but ratherfollows the generaltrend of the data. density. it returnsthe coefficientsof the polynomial that pass directly through the datapoints. >> p = polyfit (T.2 ) _O.That is. function to perform an interpolation as in (p. This technique. s ) which is the Newton linearinterpolation formula.f (rt) ^ 4l f(x)f(xt) x2xl ( 15.2). Notice that besidesrepresenting the slope of the rr) is a finitedifference line the the term .
the better the approximation.386294).asthe betweenthe data points.li(r) _:../ r) "f(. We useEq. (a.5)] In general.f: 4 S . true valueof ln 2 is 0.2 FIGURE used triongles the slmilor indiccrte oreos The shoded inierpolofion.the smallertheinterval approximationof the first derivative{recallEq.x T5._*\\ f(xz) d .0 t. 3 V c .791759.340 INTERPOLATION POLYNOMIAL . 1.0 (2.r86294 lre\ :0+ t) :0.fi (2:o ) + ff 1 .20)1.phicol ormulo i n l e r p o l o l i ofn i o d e r i v ei h e N e w t o n l l n e o r lFq {15. the naturallogarithmof 2 using linear intelpolation.2 L i n e o rI n t e r p o l o t i o n EXAMPLE First.1): 0' 358351e : l I to.r:=4 w h i c h r e p r e s e n t s a n e r r o re o.stimate :0 Then.4620e81 . ln 6: I and ln between by interpolating perform the computation the Notethat but use a smallerinterval liom ln I to ln 4 ( I ./ I rii t .4 / . a continuousfunction will be better approximatedby a straight interval decreases.' / . line. Problem Stotement. Solution. repeatthe procedure. in the following example' is demonstrated This characteristic 'I5.6931412. ( 15. of ineor depiction 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 yields .5)from :r1: I to rz : 6 to give . This is due to the fact that. E. 7 97 15 e.
8) br:"{2 .r and solved(after some algebraic .f(t:).3Vo.\'l into Eq.2.rl can be usedto conrpLlte b1: .(15. (15..7) at r : r2 for Equation(15.8)can be substituted rrranipulation.Eqs.rt . Conis to introducesomecurvatureinto the line sequently.7)can be substituted into Eq.2resultedfrom approximatinga curve with a straightline. a strategyfor improving the estimate this can be accomplished with a the points.rz). r r ) ( x .  5. t.: .s) for t : r.xy) (15. z ) (15...2 GluqdroticInterpolotion The error in Exaniple 15.rr) (I 5. (15.: r r ) . B<tth interpolations are shownin Fig.alongwith the true function.6).If threedatapointsare available.which can be evaluated (x) ..f (. (15. (15. usirrg the shorter interval reducesthe percentrelative error to €t :33.{2 f(. connecting polynomial (alsocalled a quadraticpolynomial or a parabola). 15. D : ( ./(r:) h1l . A particularly secondorder is c o n v c n i e nltb r r nl b r t l r i sp u l p o s e l : ( x ) : b t * b z \ .6) For b1.which can be evaluated at Finally.3 Two lrneorinterpololions lo esiimoleln 2 Note how the smollerinlervolprovideso better esiimote. Eq. Thus.f l'(.2 NEWTONINTERPOLATING 341 J(. A simple procedurecan be used to determinethe values of the coefTicients.3.6).7)and (15.POLYNOMIAL I 5.1 .tr) .r) 2 05r FIGURE I5.9) {.l(.t with r :.6.
6). Before illustrating how to use Eq.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.Thus.4610981 +.r . and rr. as was the casewith linear interpolation.The lastterm.. as specifiedpreviouslyin Eq.0 _:0. terms are addedsequentially t0 captureincreasinglyhigherorder curvature.r) : In. b:(r . (4. (15.r:) : l.l5.r) 2 fl.xr)(. FIGURE I5.386294. 7 ) bt:o Equation( 15.r:2). Employ a secondorcler Newton polynomial to estimateln 2 with the samethreeooints usedin Example 15.. (15. r : I t o4 i s o l s oi n c l u d efd o rc o m p o r i s o n .8)gives b 1: .27'). ( 1 5 . the first two terns of Eq.  \. b2 still represents the slope ofthe line connectingpoints xy and "r2. (15. Thus. Eq.342 POLYNOMIAL INTERPOLATION Notice that.4 T h eu s eo f q u o d r o t irc nterpootio ne s tm o l eI n 2 .2: rl:1 . we should examinethe form of the coefficient &.1 f(. A p p l y i n gE q .6) are equivalent to linear interpolationbetweenx. . That is. introduces the secondordercurvature into the formula.It is very similar to the finitedifferenceapproximationof the secondderivative introducedpreviously inEq.38629. (15. T h el i n e o irn l e r p o l o i ifo lo rn o m .'791159 yields S o l u t i o n .t Ouadraticestimate Linearestimate .5).\z:4 rr : 6 "/(xt):0 J$):1.6) is beginningto manifesta structure that is very similar to the Taylor seriesexpansion.
l f [ x . .\n  ( rs . r .q. 1 7 ) "Yl . : 18.. ...4) improves the interpolation cornparedwith the result obtained using straight lines in Example15. ( x * .12) (r5.5 1 8 7 3 1(xl)(:r .f'[r2.. . Ikitf(rl) t. b . .x ) .r .1 . d a t ap o i n t s t h e c o e f f i c i e n tb s1 .: f l x . which representsa relative error of e. the nth finite divided differenceis .r3) xtl fu : f fv. x r l ri . _ r ) (15. f " _ t f ) : b r 1 b z j . l ' ( . . 15.4) + 0 .10) As was done previously with linear and quadraticinterpoliition. . . ..l)thorder polynornialto n data points.. .tlre are finite divided differences.0 .3. generallyas first finite divided differenceis represented J l ^ i . * b u \ . F o r a n ( n .x r ) * . . q J.rr] (r5.14) where the bracketedtunction evaluations For exanrole. 4 6 2 0 9 8 1 ( which can be evaluated At . . [ .  5. J Q ) 1 . r r ) ( x .5658444. .datapointscan be usedto .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 following eqnations to evaluate the coefficients: b1:fQ1) 6' : . .r] (15. ( 1 5 . 5) The secondfinite divided difference. lr ) (1s.791759 343 64 6. (15. f z @ ): 0 r l ) . 0 . Thus. 1 1 .47o.2and Fig.6)yieldsthe quadratic formula Substituting ..1.0518731 these valuesinto Eq.2. the curvatureintroducedby the quadraticformuia (Fig. .2 NEWTONINTERPOLATING POLYNOMIAL a n d E q . : b .I .The (n I )thorder polynomialis . .r : 2 for fz(2) : 0. xz. : f lx.whichrepresents the difference of two first divided generallyas is expressed differences. . r"i l : r. 15. l x z .( x .16) Similarlv.3 Generol Form of Newfon's Interpolofing Pofynomiqfs The precedinganalysiscan be generalized to fit an (zz. 9 )y i e l d s .f .o r d ep r o l y n o m i a ln evaluate arerequired l r:r . _ _yl ( 1 s1 .x n ...386294 1.rt (15. . x1. b 2 .0. .462098r : 0.f [ r i .r r ) 1 . "rz.I 5.t .1 ) t h .
3./ L^41 ^{l _ l  :0. . (15. r z ) . x r l( 1 5 .t ) .4620981 : _ .asillustrated in the following exarnple.x t ) f l x z .:0.19r'759 1.x 2 ) ( x .(15. f l x n .{) L6094381.x r ) ( x . polynomial. In Example 15. (15.x n .O 1. and.estimateln 2 with a thirdorderNewton's interpolatingpolynomial. (15. x 1 l + .386294 r.x r ) * b z ? . Eq. The thirdorder f z @ ) : b r * b z @. x z . .17)arerecursivethat is.14).x2.386294 . .18) be valuesnecessarily equally spacedor that the abscissa be in ascending order.10)with n : 4. r ..r.x 3 ) The first divided differences for the problem ale [Eq.1.r3= ( 1ry91s = used to estimate ln 2 with a parabola. t r n .2.data pointsat xt : l. . Also.rG):. . (15. .':^:).[] .x r ) ( . x2]: fr.344 POLYNOMIAL INTERPOLATION xi xl '.rr] 4 f [4.x ) f l . r r ) ( x .791159 . . 1 8 ) We should note that it is not necessary that the data points used in Eq.:rl it::ti=. r r l * ( " r.4 EXAMPLE Newton Interpoloting Polynomiol Problem Siotement.15)through(15.:..1823216 56 .r. "f["r:. Now. Thesedifferences can be usedto evaluate the coefficients in Eqs.x ) * b q ( x.x r ) ( x .j. noticehow Eqs.f (. . " + ( x . adding a fourth point [xa : 5.10)to yield the generalform of Newton's polating polynomial: f.This property will be exploitedwhen we developan efficientMfile in Section15. x 2 .l ii.609438 :0.'^' f(x) First Second Third xl r flxo. (15. xz:4.. .5 Grophicol depiction is of iherecursive noture of finite divided This representotion differences..f ( x t ) + ( r .4 to implement the method. 15..a4t.11) through which can then be substituted interinto Eq. is Solution. FIGURE I5.15)] . ( r .2027326 64 .5). 15. xt. . higherorder differences are computed by taking differences of lowerorder differences(Fig.t .. (15. 4 .eferred to oso divided difference toble.
6. 0 5 1 8 7 3 1 l (" t l ) ( x .x 1 ] : 0.0 . coefficients of Eq.t3.3Vo.l)(x .t 2 .16)] /[.the interpolating cubicis f:(r) :0 x l ) .2 NEWTONINTERPOLATING POLYNOMIAL The second divideddifferences are [Eq.2427326 o l823216 cc5lBZ3il 0 02041 r00 00a786552a t) e d T h e r e s u l t sf o r / ( x r ) . The complete cubicpolynomialis shownin Fig.10).fzQ) :0.0. .17)with n :4] J ' [ x + . Thus. the divided differencetable is f (xi) j 4 6 5 0 386294 791759 609438 a 462A9Bt 0.x 2 ) : 54 : 0. f [ x t . (15. .4)(r .4620981 o. a n d f l x q .4620981( .007865529 Thus. x 2 . (l5. x z . 15. x 3 . f [ x z .b3.007865529(x n which can be used to evaluate.051873 1l) J  : 0. respectively.051873 I1 6l .02041 r00.02041100 The third divideddiff'erence is [Eq.b2. . r1] : 345 . t l l r e p r e s e ntth e b1.2021326 : 0. x 1 ] .6) + 0. f f x a .(0. x t ] .4 ) + 0. .. x z . r t . FIGURE I5. (15.1823216 . which represents a relative error of €t :9.0.I 5.t2.6 The use of cubic interoolofion lo estimoten 2 f(x) z .2027326 0. andb'.6287686.
omnrl e rlre finire % difference table n = length(x). % % input: :.y.lanor F va ' ' rh'c 1Fr x% z % vxx= r.1)order Newton ? yint .j1)b(i.et = tl Longt 4 6 5l >> x I5.15.7 FIGURE An M fl e lo imp ement Newton inferpolotion.2.2.ts .j) .i). (15. = yrnt+b(1. Thedifferences are then usedin conjunctionwith Eq.1) % the (:) ensur:es that y is a column vector for j . for j = 1. yint end .(b(i+1.j1))/(x(i+jr)x(i)) b(i.xx): Uses an (n .lanonrlanf rr:ri :hl o value Intrlyn^ i of independent a'o. j+1) *xt.18)to perform the interpolation.xx) function yint polynomial I'Jewton interpolaling % Newtint: = llewtrnt(x.n) .3: >> form. : r 1Fr ifnlv. An exampleof a session using the function would be to duplicatethe calculation we just performed in Exarnple15. end end ? use Lhe finite 1. = Newtint(x.4 MATTAB Mfile: Newtinr It is straightforward to developan Mfile to implementNewtoninterpolation.n1 xr xt"{xx x(jr).y. b(:.x. ? assign dependenL variables to the first column of b.:Fr^1 divided differences in the form of a !  s rli z mr c ho camo etnt hri: 6ncl b = zeros (n. As in Fig. xx. the first step is to compute the finite divided differencesand storethem in an array.7.346 INTERPOLATION POLYNOMIAL 15.fdf variable CIaLeO at which z ? output yint % = inierpolated value of dependent variable !} .n for i = 1:n j+1. 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. 1  divided differences to inLerpolate yint = b(1. = y(:).
r r . > > y = t o S f( x ) . 15. Equation (15.x : ) ( .rl Substituting thesecoefticients into Eq.. It is logical that the first weightingcoefficient is the straightline that is equal to I at x1 and 0 at x2: xtx2 Similarly. : "/r(x) . 8 ) : . \ ' r ) ( r ..22) .20) is refered to as the LinearLagrangeinterpolatingpolynomial.rX2' xlX2 ^ J 6) l . Both the first.t . 1 5 .the second coefficient is the straight line that is equalto I at "r2and0 at..6287685"t890841 I5.r2and./ (r3x1)(xjr2) /.Xr X2Xt J'. The samestrategycan be employedto fit a parabola throughthreepoints. Such a secondorder Lagrangeinterpolatingpolynomial can be written as At.rl.r t )  (xx1)(xx3) j tt2t + (. >> Newtint(x.For this case threeparabolas would be usedwith eachone passingthroughone of the points and equaling zero at the othertwo.2) 347 0.=1. 1 r ) .3 LAGRANGE INTERPOLATING POLYNOMIA.I 5.t  (..and secondorder versionsas well as higherorder Lagrangepolynomials can be represented conciselyas (1s..r l.r9) where the Z's are the weightingcoefficients.19yields the straightline that connectsthe p o i n t s( F i g .3 TAGRANGE INTERPOTATING POTYNOMIAT Suppose we formulatea linear interpolatingpolynomialas the weightedaverage of the two valuesthat we are connectingby a straightline: ./(. { : .rrr)(. r 1 ) aa tn 1d 1isequaltozeroat. ( .r1: r trtrI 12 .Jrilrr ( x r x z.x r ) ( . Their sum would thenrepresent the uniqueparabola that connects the threepoints.6) (1s.r): Lt J @r)t Lzf Qz) (rs.). \ l. ( r5 .rxr) . 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 / ( .y. r :.Theother terms work in a similar fashion.\'.20) where the nomenclatnrefi(l) designatesthat this is a iirstorder polynomial.
8 0 0 : 1 .5. Theflrstorder canbe used tEq.85 l(. 5 6 2 5 1." .gtlO f(rt):0.85+'" 'f r( .(15. r/ where .r):   ' ! I j=l .20)l .h ef i g u r e shows A visuol depiction of therofiono e b e h l n dL o g r o n g e t h e f i r so i 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 hfe p o i n t s o n d line is zero ot the other.IFI tl _ fl y. r ) : ' " 020 t00 .8 i n t e r p o l o i i np go l y n o m i o l s T. thot connects where r L.o" 0 .20 t 5 .212 polynomial to obtain theestimateal Solution. b"ethe uniquestroiqhl ihe h^/opornts. Use a Lagrange interpolating data: orderto evaluate thedensity of unused motoroil at T : 15'C based onthefollowing rr :0 r::20 . ^1 (15 23) _  .348 INTERPOLATION POLYNOMIAL LtfU) FIGURE I5.r: I5: t 5 .{::40 f(xr):3.The summoiion of ihe two fermsmusi.(.iherefore.r : the numberof datapoints and fl designates the "product of.r:):0.5 Interpoloting Polynomiol EXAMPLE Logronge polynomial of thefirst and second ProblemStotement.
{r):  (1520)tl540.t ] . Z a given value of the independent variable.xxl trrccion polynomial Z Lagrange: Lagrange interpolatingr = Lagr:ange(x.. 6 t ._.J316875 (400)(4020)  5.1 MATTAB Mfile: Lasranse As in an Mfile based on Eqs. an (n . error('x and y must be same length'). If n values arepassed. is developed as[Eq.( 15. FIGURE I5. It is straightforward to develop (x) and the two vectorscontaining the independent Fig.20)(0.9.0)(20.(15.23).n if i = i product = product* (xxx(:) ) / (x(i) x(j) ) ._ U. end end s = s+prc. Theorderof thepolynomial is based on thelength of thex vector wantto interpolate is fit.i . I 5.21)] In a similar thesecondorder /. U (20. _ n.40) + (l5O)fl520). 15. yint . end for: i = 1:n .40) (0 .y.\.22) and (15.duct. 6 1+ .the functionis passed (y) variables.r^^_ (150)(1540)^^^^ .3 LAGRANGE 349 polynomial fashion.Lag"6ngerx.212:1.INTERPOLATING POLYNOMIA.1)order yint 2 polynomial based on n data points ? Lagrange inLerpolating (yint) at to determine a value of the dependent variable % xx. . Z input: x .I'i for j = 1.3.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).9 inlerpolction AnMJile lo implement Logronge y nL .1)thorderpolynomial thatis passed. end = s. if lenqth(y)=n. you variable where It is alsopassed thevalueof theindependent dependent (xx). Produce .xx) : Uses an (n .
15 ) I .1667 7 0 .rl.An alternative strategyis to fit an nthorderinterpolatingpolynomial.r). for the dataabove. they will have the appearance of a logarithmic scalewith some adjacent points bunchedtogetherand othersspreadout widely.rthat makesthis polynomial equalto the given /(.f ('.1.4 INVERSE INTERPOLATION As the nomenclature implies./. (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. in many cases. be illconditioned. you might be tempted to switchthe /(.but you are given a value for .2 >> densitY i I \ 41 22222 2 LaQf range ons t.52 1.3 Such a problem is called inverse interpolatiort For a rnore conrplicatedcase.r values[i.22II2B I5. >> d = 1r.merelyplot x versus use /(x)] and an approachlike Newton or Lagrangeinterpolationto determinethe result.suppose that you were askedto determinethe value of I that corresponded to l (r) : 0. a thirdorderpolynomial would be impleBecausefour valuesare passed mentedby the raqrange functionto gile: >> format longr >>T=t40024541.3333. That is.3so POLYNOMIAI INTERPOLATION An exampleof a sessionusing the function would be to predict the densityof airat of 15 "C basedon the first lour values from Table15. because the lunction is availableand easyto manipulate. In most cases. when you reversethe variables.r)and. the interpolationproblemreduces to . thereis no guarantee that the valuesalong the new abscissa In fact. This can occur even for lowerorderpolynomials.3. Thus.The answerto your problem then amountsto finding the valueof .091. For example. this polynomialwill not f (r) versus.r's are evenly spaced. For thiscase. to the original data[i. the valuesof the r's are typically unilbrmly spaced.r) and. (r). the correctanswercan be deter:3. the valueswill be "telescoped. with because the.r)'s] will be evenly spaced. For instance.respectively.e../(r) and must determinethe corresponding value of x. I atm pressureat a temperature to the function. A simple exampleis a table of valuesderivedfor the tunction l/x: f(r): x f@) 2 05 3 0 3333 4 025 5 o2 6 0. t4 2 9 Now suppose that you must use the samedata. m i n e dd i r e c t l )& S { : 110.29 i. the .Unfortunately." [the /(.t valuesin most interpolationcontextsarethe dependentand independent variables. As a consequence.e. d..
r 2 .5 or 6 couid be employed.there are twcl issuesrelatedto polynornialinterpolationthat must be addressed. t h e o p e n .r) : 0. . 0.041667) Thus.a simple approachwould be to fit a quadraticpolynomial to thethreepoints:(2.25).f (x) .r r . and(4.e 0. the secondroot.3333). The extropolofion isbosed through poinfs on fittinq o porobolo thLfirst three known .e n d en da t u r eo f F I G U RI E 5. 3 1 5 x+ 1.If additional accuracywere desired. Theseare extrapolation and oscillations. polynomial I I I I I I t> l"r . . . .4Q. 0 8 3 3 : l The answer to the inverse interpolation problem of finding the .333.(3.0416 '7 1 2 . . I5. A s d e p i c t e d i n F i g .1 0 .78333 (0.r) that lies outsidethe rangeof the k n o w n b a s ep o i n t s . the quadratic fbrmulacan be usedto calculate 0.for the problernjust outlined. 3 7 5 r+ 1 .296. I n t e r p o l a t i o n. t .rlt wouldbe / : ( r ) : 0 .I 5.5).041661)0. .5.375 + . 15. 0.5 EXTRAPOTATION AND OSCILLATIONS 351 For example./ (.295842 2(0. The resr. 0 8 3 3 3 For this simplecase.3 : 0.or tburthorderpolynomial along with one of the rootlocationmethodsfrom Chaps. . ' E x t r a p o l a t i o n  r.a third.0 . 0 4 1 6 6 7 r r.. Extrapolation I 0 rof 1 interpolati tnteroolattno Il .704158 3. . 0.1 Extropolofion Ertapolation is the process of estimatinga value of .is a good approximationof the true value of 3.37s)2 : 5.3 would therelbreinvol'r. 3. 1 5 .IO lllustrolion of thepossible divergence of on extropoloted prediction.0 .r correspondingto determiningthe root of l (.5 EXTRAPOTATION AND OSCITLATIONS Before leaving this chapter.
p o p .7 ) However. andMoler. First.45 I23.the datacanbe entered as >> t = L\924:10:19901.when this is implemented. nloi (r. We can then use the polynomial coefficientsalong with the Fo1yr731function to predict in 2000 as fhe population :'> polyval (p. >> pop = 1105.05 227.3s2 POLYNOMIAL INTERPOLATION a step into the unknown because the processextendsthe curve extrapolationrepresents beyondthe known region.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.tt. 2 0 0 A ).6 Dongers of Extropololion Problem Stqtement.23 2 4 a.61 205. from 1920to 2000 can Malcolm. rr2l conter ino Remove repeated drid scdr jng ds desct ibeo data in HELD We can follow MATLAB's suggestion by scalingand centeringthe datavaluesas in >> rs = (r 1955)/35. pop.the true curve could easilydivergeliom the prediction. the following message is displayed: VJarnlncJ: Polynomial po'nt s or POLYFIT. ' >p = p o l y f i t ( t s .polyfrt (t. The polyf it function can be usedto computethe coefficients >> p . (tt1955) /35).{)oD..Insight into the problem can be gained by generating a plot of the dataand the polynomial.o. >. 1 ) . EXAMPLE 15.be exercisedwhenever a case arises where one mu$ extrapolate. therefore.27 180.As such.08 132. is badLy condilioned.Use it to cornpute Fit a seventhorder the population in 2000 by extrapolationand compareyour prediction with the actualresult. This example is patterned afteroneoriginallydeveloped by Forsythe. (2000f955)i 35) 175. Now po11.pp) E} . Solution..08 13212 15227 tBA67 2A505 22723 24946 28142 polynomial to the first 8 points (1920 to 1990).I2 L52.fit works without an errormessage: r .42. r 6 l . >> pp = polyval (p. > > t t = l i n s p a c e ( I 9 2 A .0800 which is much lower that the true value of 281. Extreme care should.
'n::ilT::. The following exampleillustratesthis point nicely.[i.I I rio U s eo t o s e v e ^ t l ' o ' d e ' p o l y ^ o n ortr o i .it is absolutelynot true for polynomial interpolation.".. e o predictio on [ U ..Higherorderpolynomialstend to be very illconditionedthat is. Create .2 Oscillotions Although "more is better" in many contexts.p o o u l o on ." 2OOO boseo on d o t oi r o ml 9 2 Of h r o u q h 1990.Carl Rungepublished a studyon the dangers of higherorder polynomial interpolation.*1. He took equidistantlyspaceddata points from this function over the interval tl. . E X A M P L lE 5. (15. the seventhorder  5.: polynomialplungesto the erioneous extrapolation.2$. S .5 EXTRAPOLATION AND OSCILLATIONS 353 250 r\ 200 150 1920 1930 1940 1950 1960 1970 1980 1990 2000 FIGURE I5.5. considerably. they tend to be highly sensitiveto roundoff eror.24) which is now called Runge's function.1]ii*:.. predictionin 2000.I 5.and tenthorder polynomials to 5 and 1l equally spacedpoints generated with Eq.:.*::TJ:il:1Jl:J.O r d e r P o l y n o m i oI lnterpolotion Problem Stotement. In 1901. He then used interpolatingpolynomials of increasing order and found that ashe took more points.Z D o n g e r so f H i g h e r .the polynomialsandthe original curve differed greatly as the order was increased. ll.the situationdeteriorated Duplicate Runge'sresult by using the polyf it and polyval functionsto fit fourth.?.He looked at the following simplelookingfunction: I : ' rt l l\t  l *25x2 (t5. Further.
the polynomialdoesa poorjob of followingRunge'stunction.I.5 .11)./(I+25*x. (p. 15.1).I2 funct]on Cornporison o[ Runges line) wiiho fourthorder polynomiol fitto 5 poinis {doshed sompleo lrom lne iunclron.L.4 = polyval >> p (x.1. xx) .yt:. 4) . The five equally spaced datapointscan be generated as in >> x >> Y = linspace(.l ( x "f(r) (a) C al fc .^2).y4.I. / (I+25*x.i (1+25*xx. FIGURE I5. we can generate valuesfor Runge'sfunction itself and plot them alongwiththe polynomial fit and the sampleddata: >> yr .y.' o'.xx. /t It r\ r\ t\ l! t.5). ^2) .2 0. Continuingwith the analysis. >> plot (x. Finally. Recall that l inspace automaticallycreates100 points if the desirednumberof points is not specified.4 1 r\ i\ ta\ tta t5.y .0. i 0.' ' ) As in Fig. I . and the polval function can be usedto generate the polynomial interpolation at the finely spaced valuesof xx: = po11'f it >> 1. polynomial can be generated the tenthorder andplotted with >> >> X y = = linspace( 1. a more finally spacedvector of rx valuescan be computedso that we cancreate a snooth plot of the results: >> xx = linspace(1.12.354 POLYNOMIAL INTERPOLATION Solution. Next.xx."2). t  t. I.The po ly f r t function can be usedto generate of thefounhthe coefficients order polynomial.
xx.y10. >> plot(x.5 7 3 65 5r o "f(r) I 475 3 5.y.y.'o'. Chooseyour basepoints to attain good the sequence alsoforder I through3.yr.75 6 36 polynomialsof polynomi.I3 functlon Comporisonof.Calculate/(4) usingNewton's interpolating Calculate /i3..the fit has gotteneven worse.xx. PROBTEMS Giventhe data l5.5 F I G U RI E 5.lowerorder polynomials of the type describedin this chaptercan be used effectively to capturethe curvins trendsof datawithout sufferins from oscillations. x x ) .particularlyat the endsof the interval! polynomialsarenecessary.l Giventhe data 2.' ' ) As in Fig. Choose for your estimatesto attain the best possibleaccuracy.25 5 )9.4) usingNewton'sinterpolating ofthe points order I through 4. > > y 1 0 = p o l y v a l (p .nnminl ofthe usedto generate the data in the table? .5 1 nF 0.In most engineeringand scientific contexts.aL^ r ^_^_^ ^l. accuracy.What do your results indicate regarding the order .Runge's fit to I I points polynomiol {doshedline)with o tenihorder somoledfrom lhe lunction >> p = polyfrt (x.13. 15.PROBLEMS 355 a 0.10) . Although theremay be certaincontextswherehigherorder they are usually to be avoided.
l in s pres T.Estimate"/1 polynomials.12 The saturationconcentration of dissolvedoxygen in high precision. third. x IB 20 2..l2. regressionwould typically be the preferred method of curvc litting fbr analyzing such experimental data.lt The following data for the densityof nitrogengas ver.3 RepeatProb. v 15. " o 5 t0 t5 20 25 30 f(x) 36 lB 12 09 072 I 5 A . t ' ( r ) 0 .6but using tirst. I ) using third. Determinethe percentrelative enor for each (c) dratic interpolation.2 24 26 15.4( ship rather than the straightline represented by Ohm'slaw: . 54 t 29 t *". l5.6 Employ inverseinterpolationto determinethe value of r that coresponds to l (t) : 0.1 TableP14. which can be determined with entropyof 6. 15. Here aresome selcctcdvalueslbr the zeroorder Besselfunction ofthe first kind SOIV the < the r for l r5.for the quadraticpredictionusethe datafrom I 970. (c) Use cubic interpolation and bisection to determinethc value numerically.6 for the following tabulateddata: to measure the voltagedrop and corresponding curent for a rcsistor.Use firsl throughfifthorder polynomialsto water as a function of temperature and chloride concentraestimatethe densityat a temperature of 330 K.11 RepeatExample 15. as well as the precisionof the experimentalmethods. polation to determinethc conespondingtemperature.l lar a the r ( Bec has ( whe dim . What is your tion is listedin TablePl5. 15.5 Employ inverseinterpolationusing a cubic interpolat.10 Besselfunctionsoften arisein advanced engineering analyses such as the study of electric fields. kJl(kg K) 6 5453 6 7664 populationin 2000basedon the most recentdata.45 using inverseinterpolation. (b) Use quadraticinterpolationand the quadraticformula to determinethe value numerically. "1 a.and the volume corresponding to an casebasedon the true value. MATLAB's builrin tunctionbessel j. Usea fifthorder interpolatingpolynornialto fit the dataand compute V fori : 0.and so on.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).5lists valuesfor dissolved oxygenconcentration in water as a function of temoeratureand chloride T ' K 204 254 300 350 404 450 concentratlon.r12 67 TAB T. I 980.93 for the following tabulated data: 2 637 t 96 5 05 245 05 205 t 965 the r 637 lO o 15. 15. u. pola r01 J@) 0 0.However.for the linear predictionusethe datafrom I 980 and I 990. real resistors may not always obey Ohm's law. Densiry \ 7 0 8 I 3 6 7 I r 3 9 0 .the smoothness of the relationship. (tl) Use linear interpolationto determinethe oxygen con15. ideal resistor is linearly proportional to the currcnt I flowing (c) Repeat(b) but use quadraticinterpolation. 9 6 1 0 8 5 4 0 / 5 9 (a) Use quadraticand cubic interpolationto determinethe kglm3 oxygen concentration for I : 12 'C and c:10 glL. and I 990. Which approach yields the best result? sus temperature comesfrom a table that was measured with 15.Thatis. The following resultssuggest a curvilinearrelation2. Becauseof mcasurcmcnterror. a curve must be fit to thedata.7 Use the portion of the given steam table for super. : I R. 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. m3/kg o to377 0 il 144 4 1 2 5 4 7 and fourthordcr interpolating polynomials to predictthe 64147 s. and fourthorderinterpolating polation. (b) the same coffesponding entropy using qua. ing polynomial and bisectionto determinethe value of x that you performed Suppose that some very prcciseexperiments corresponds to /(t) : 1. Use intemolation to estimate To quantify this relationship.356 POLYNOMIAL INTERPOLATION 15.throughthe resisteras in V However. I l8 with linear inter(2. 15.10.2 using the Lagrangepolynomial of best estimate? Employ this best estimateand inverseinten order I through 3. (a) Determinethe correctvalue analytically. second. 15.9 Ohm's law statesthat the voltage drop V across an centrationfor I : 12 "C and c : 15 glL. suggests that interpolationmight be appropriate. where R is the resistance. Jl Dett r5..
2 si.t J t r" ' i n 1 c n s i t .2 +.sincorrvenient fo solye nranually.tns$crin(onnespersquarenlctcr.venl11t J ': t' t ' l r l"a lIrrarea:trbjcctedolunitrt .# TI Forexampre ata I .at it depthI (i rl beiou' thc corner of a rectangu.13 Forthc datain Table lmlT I EU:'. rI t r r : + n : i l + m 2 n 2 m 2 + n 2 + l L TABIEPl5.17739 0l o2 03 a4 05 06 a7 OB c 42a26 4. l2 _ PRoBLEMS in woteros o function of iemperoture Dissolved oxygenconcentrotion {"C)ond chloride (g concentrotio n/ L ) .a aa7 a l2t14 a l4060 a li5o9 c1 ltabulatecI.ll:l*.U Thc.45733 0 08323 0 t063t c 12626 0 14309 0 I 5203 0 I 6843 .specil'ic r 6613 I u.sc this cquationi.'.:'"*"'.t:tj'Tltt \tre\s.t1. /"/ / i esrimare rhe di:solvcd o\yscn concenrrarirrn / " _ _+ I ff. The influcncevalue is then n=1.under thc c.iect to a total ]oad of 100 t (metric ..) z*a 7(.:ul.13241 a.with nt : al: and rr : b/..iffiiil:temrcraturcs F r c u RP Er s .'.Erpress)our..cchor.l5. I I I 25 .f\nt.ro fo rheeaser.4 0 a3aa7 0 a5Bq4 0.aportionol'whichisgiveninTablePl5..N()tc L..'.7+ t r] + n2+2 J . and a and b are defined in Fis.I I I f nfU Pt5. I q I z*nJ.l5a27 1 0 1 6 55 4. (#.lhel.".D. and lr and rr are dirnensionless ratios.6 andh : 14. Pl5.+#)l Bccau.15.idecollccntrationisequatol0g/L.f(rrr.0 I I t? 3 '3A.i:J^"Ti::#T?. usepolynomial inter15. n) where.usea thirdorderinterpolating polynomiat to I ^ I at : 750 'F compufeo."a c=l}g/L l?9 c=2Og/L I I I r: I l.*':::.'. it has beenreformulatedas o.rncr of a rectangu15'15The lar footing that is sub.*"H"t.6 0 03058 a a59q4 a aB709 0l]t35 a.Ifa: 1.iliH.12. l s I /oo 72.r I .:'.. 146 ssl I lt.l5 tt = 1. n) is calledthe influenccvalut. Bt6 756 lJt 3?9 7 46 685 n '33u 93! 114 673 62A I Iosrr. : levclfor ?': 18'C with chloride the clissolve. DissolvedOxygen (mglt) for Temperoture("C)ond Concentrotionof Chloride (g/Ll s=OglL '. 1 44 79 016199 0 rz3B9 n=1.UseJ.0856 r 0 t0941 0 13003 o ."" rolution o l B o u s : i n c s q 'e sq I the ' uation: inat 4 ts cquatto Inc tolloper area. r r 7 i r e rr ern ' 't " t ti r sb )r .l oxygcn P15.a 7Ba I t "t I rolunre ol'u :upcrlicrte(l steanr is listctl lS.lt3/lb.'.
20 Use the portion of the given stearntablefor superheated HrO at 200 MPa to (a) tind the conesponding Determine ialt:0.Interpretyour results.4147 a. 5 0 0 0 heated plate (Table P15. u (m3lkg) s (kJlke.llJ The acceleration due to gravity at an altitudey above interpolation. 144 6.49 48. 6.78 35.85 0 30.r25. and (c) find the volumeconespond.5453 0. J = 8  5. Estimate the temperature ( b ) r : 4 .04 6000 48 I5 5 00 0 3 5 0 3 4000 2 7 . 3 .90 38.23. 0 0 0 0 0 ( a ) .000 !.6 using inverseinterpolation.000 cornpute m.000 90 000 120. (b) find the samecorresponding entropy usrng the surfaceof the earth is given by quadraticinterpolation. ) ' : 3 .y : 2 .6879 9 6278 9 568'2 g ary: 55. a n d 15.19).15. 2 . 0 0 2000 t t li. 0 7 3000 2 5 .00 Temperotures ("C)ot vorious points plote. entropys for a specificvolurne u of 0. irrg to an entropyof 6.K) 0 14377 6. m g. 5 a70 t5 ]BB 20 60 70.00 40. I 7 The currentin a wire is measured with greatprecision as a function of time: 425 o. 5 0 3 84 3 30 39 3000 x=6 x=8 15.  9 x=O 10000 8 50 0 2000 s5.00 x=4 B0 00 53 . 19 Temperatures are measuredat various pointsona at 0 .000 60. m/s2 9 8100 9 7487 9. a 0 01250 624 02500 775 03250 4. 7 .108 mi/kg with linear l5. on o squore heoted x=2 90 00 64.358 POLYNOMIAL INTERPOLATION TABTEP I 5.through fourthorderpolynomial interpolationto t = 6 estimatethe voltagedrop tbr i: 1. r: 4 .7664 .45 475 1. 2 .16 You measure the voltagedrop Vacrossa resistorfor a number of ditl'erentvaluesof current L The resultsare "y=O y =2 t=4 Use first.
.Specificobjectives and topics coveredare .clamped.rltidimensional interpolation is implemented with MATLAB. Recognizingwhy cubic polynomialsare preferable to quadraticand higherorder splines. Understanding how mr. thirdorder curves employed to connect each pair of data points are Thesefunctionscan be constructed so that the connections calledcubic spl. Knowing how to fit a splineto datawith MATLAB's builrin functions.thereare caseswhere thesefunctionscan lead to erroneous results becauseof roundoff error and oscillations. for eight points. Such conapply lowerorderpolynomialsin a piecewisefashionto subsets nectingpolynomialsare calledsplinefunctions. However. 15 (n .*i . Knowing how to developcode to perform a table lookup. i' Splines ond rrecewrse Interpolotion l'l ..f i.i.. ..and notaknotend conditions.. . polynomial.I INTRODUCTION TO SPLINEs ln Chap. Understanding the differences betweennatural.ines.An alternativeapproachis to of datapoints. For example.l)thorder polynomialswere used to interpolatebetweenn data points. ". I6.i. we can derive a perfect seventhorder (at leastup to and including seventhderivatives) curve would captureall the meanderings suggested by the points. Understanding the conditionsthat underliea cubic splinefit. between ...:r. This For example. CHAPTER OBJECTIVES The primary objectiveof this chapteris to introduceyou to splines. o .. . o ' Understanding that splinesminimize oscillationsby fitting lowerorder polynomialsto datain a piecewisefashion.'. .
controst.This is the casewhere a function is generallysmoothbut undergoes is depictedin Fig. Figure 16.In contrast. is n i n t e r p o l o i i np go l y n o m l o l s oscillotion indicote t h o tt h e o b r u p tc h o n g ei n d u c e s o lineorsp ine {d) provideso much more to strciglLtline connections.1 illustratesa situationwhere a spline performs betterthan a higherorder an abrupt polynomial. occeolobJe it would seemthat thethirdare visually smooth.360 INTERPOLATION AND PIECEWISE SPLINES FIGUREI6. becouseit is lirnited coproximotron. The funciionto be fit undergoes ln . Porls(o) through on obruptincreose polynomiols. 16. order approximationof the splineswould be inferior to the seventhorder might wonder why a splinewould ever be preferable.I interpoloting to higherorder where splines ore superior A visuolrepresentotion of o situotion (c) ol r : O. polynomialstendto swingthrough how higherorder Figure 16.The stepincrease changesomewhere an extremeexampleof such a changeand servesto illustratethe point.la throughc illustrates wild oscillations in the vicinity of an abrupt change.On the surface. the spline also connects .1 along the region of interest. adjacentcubic equations You expression.
e on us i. .2) and b.3) . oe f l s i n go s p l i n e sf p o n i s \ o t i c e [i' h o w o l t e e n dp o i n l s l[e splins i s< o le d o ' " o ' u r o l s p i n e et r o i g h . minimum. n).ri) where ai is the intercept.eachfunction is merely the straightline connectingthe two points at eachend of the interval.2 ' n o o hc l r u e .which is the most common and useful version in engineeringand science.2 for a seriesof five pins (datapoints).1 6 . is the slopeof the straightline connectingthe points.Ji .l l r o l g . Each interval i has its own spline function.simple linear functionswill first be usedto introducesomebasic conceptsandissues associated with splineinterpolation. Hence.This is followed by materialon the cubic spline.3. ui (16.which is definedas ai: Ji (16. Finally. The conceptof the splineoriginatedfrom the drafting techniqueof using a thin.I intervals. 2 L I N E AS RP L I N E S 36r FIGURE I6.the splineusuallyprovidesa superiorapproximation of the behaviorof functionsthat have local.In this technique. Then we derivean algorithmfor fitting quadraticsplinesto data. which is formulatedas (16. 2. .A smoothcubic curve resultsfrom interweavingthe strip between the pins. For linear splines. 16.The process is depicted in Fig.the name "cubic spline" hasbeenadopted for polynomialsof this type. Ji+r . . flexible strip (calleda spline) to draw smoothcurvesthrougha setof points. we describeMATLAB's capabilitiesfor piecewiseinterpolationincluding its ability to generate splines.2 T I N E A R S P T IN E S The notationusedfbr splinesis displayedin Fig. s. the drafterplaces paperover a woodenboard and hammersnails or pins into the paper(and board) at the location of the datapoints.For n datapoints (l : I. there are n . abruptchanges. As such.(x). In this chapter. r6..r) si (r) : ar I bi(x . .o s e r i eo techniqu T[eorclting l o o r o w. I 6.
5)] interpolate within eachinterval. I 2 3 A Dototo be lit with splinefunc]ions. Then the appropriate equation usedto determinethe function valuewithin the interval.3) for l(r.5 . (16.4 90 25 I0 2..Inspectionof Eq.tt attttrr_ FIGURE I6. I F i r s t . (16. (15.I intervols ond n dcio points gives where li is shorthand Eqs.4..r ) : . is by first locating the interval within which the point lies.4.0 + 2 51.4)indicates to that the linear spline amountsto using Newton'sfirstorderpolynomial[Eq.0 :::t.l .r ..O r d eS r plines Problem Stotement..) : 1.Evaluate the functionotx:5.1)and (16. es o l ' ^ e s Notice thot therecre n . (16. _ f..4) Theseequationscan be usedto evaluatethe function at any point betweenx1 ard.362 SPTINES AND PIECEWISE INTERPOLATION J(.I with firstordersulines. r L)l' rirlri f.2)into Eq.5 Solution. TABIE l6. Fit the data in Table 16.0 . 30 45 7.r .ti ) (r6.3 l \ o ' o ro n u t e dl o d e ' ' .5 to x :7.). (16. EXAMPL I6 E.l. for the second intervalfrom x : 4. (. Substituting Ji (.51 1. l ix. the function is For exarnple. sr(x .4) to generate the linearspline functions.f. The data can be substituted into Eq.5 0.
The equationsfor the other intervalscan be computed. the slopechanges abruptly. 5.5): sr ( x ) : 1 . 5 l.2 TINEAR SPTINES 363 FIGURE T6. 0 4.as will be discussed subsequently. 2 . l 6 .In essence.o )L i n e o srp l i n e(.l Visual inspectionof Fig.4 . . This deficiency is overcomeby using higherorder polynomial splinesthat ensuresmoothness at the knots by equatingderivativesat these points. 3 .I6. 0 + .the following sectionprovides an applicationwhere linear splinesare useful.In formal terms. Beforedoing that.t . 4 c tT h e v a l u ea t x : 5 i s 1 . at the data points where two splinesmeet (called a knot).4 Spline fits o f o s e lo f f o u rp o i n i s{. ith o c u b i ci n f e r p o l o l i p no g l y n o m i o l s op l o t t e d . 16. 0. . n d ( c )c u b i c s p l i n ew .( 5 1 .4aindicatesthat the primary disadvantage of firstorder splinesis that they are not smooth.and the resultingfirstordersplines a r ep l o t t e di n F i g .b )q u o d r o f is cp l i n eo .the first derivativeof the function is discontinuousat thesepoints.
olation outside search range' ) end 13 seclLrential i ..For datain asbeing cendingorder.364 IN ET E R P O L A T I O N S P L I N EA SN D P I E C E W I S 16. break. general would be to passin vectorscontainingall thedata A more approach and have the Mfile detenninethe bracket.this can be doneby testingwhetherthe unknownis lessthanthe value one lf so.As the name implies.suppose that you would like to set up independent and dependent teman Mfile that would use linear interpolationto determineair density at a particular peraturebasedon the datafrom Table 15.this will not testtrue at first. isa Here that we examined.the sequential it must searchthrough all the precedingpoints to find values.this methodinvolves comparingthe desired with each elementof the vector in seqnence until the interval is located.= x(i + 1).It is useful tbr variables. we know that the unknown falls betweenthis value and the previous assessed.First. < r(1) xx > x(n) (' Inter1. there are two simple ways to find the interval.This is called a table lookup. One way to do this would be to passtlreMfile to be performedalong with thetwoadthe temperature at which you want the interpolation joining values. it would search the independent able vector to find the intervirl containingthe unknorvn. The loop is repeated until the xx is lessthan or equalto the interval'supperbound. 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 whether it isless the value at which the interpolationis desired. variablesare storedin ascending order in the arrayx and the The table's independent variablesstoredin the arrayy. y.15. The first is called value sequentialsearch.For example. toenan error trap is included dependent he eo x f' s .interpolation yr = 1. variThus.(i) + (y(i+1) y(i))/(x(i+1) x(i))*(xx x(i)). end i = i + 1.lf not. a simple followed alternativeisthe binan.r.Forcaseswherexxisinthesecondintetval or higher. sort is inetTicient because For situationsfor which thereare lots of data.1 Toble lookup in engineering andscience A table lookup is a common task that is frequentlyencountered performing repeated interpolations fiorrr a table of computer applications. A r r r [ ] l s .xx.2.Then it would performthelineat in this chapter interpolation usingone of the techniques described or in Chap. to determine compares thanthevalueatthetopoftheinterval. we move to the next value and repeatthe comparison. .in can be performedsimply as which casethe loop is exited. a For ordereddata.At this point.<. the Mfile would perform two tasks. rr) cngrn'X).Here is an Mfile that performsa binary search . the interpolation shown.x(i+1). In this casethe counter i is incremented byone intetso that on the next iteration.xx is comparedwith the value at the top of the second val. while (1) if xx . Before searching.search. In thesecases. end ? linear .1. this objective: simpleMfile that accomplishes functiori n if xx error yi = Tablelook(.
Justas in bisection. xx) Lenqlh (x ) .. 6 1 6 >> TableLoo]<Bin (T.s70s To ensurethat the nth derivatives are contiDuous at the knots.'. < x(1)  zx > x(. 6 1 1 .At this point.:. > .rbleLookBrn (x. break. either the lower or upperindex is redefinedas being the betweenthe upperand the lower middle index.The process is repeated until the difference index is lessthan or equal to zero.I 6 . enl t.2 I.3 SPTINES QUADRATIC os?5o615 . 3 Q U A D R A T IS CP L I N E S 365 by linear interpolation: f unction n tf = yi = T. range') end % binary l.4 is computedas the average of the filst or "lower" index i L = 1 with the valueof and the last or "upper" index iu : n.5705 This result can be verifiecl by the hand calculation: : 0. rvh i le r.616 * I (350) 1ffi(3s0 I6.:r31 .= xx l. the lower index lies at the lowel bound of is pertbrmed.The sequential searchwould be similar.the index at the rridpoint r1.a splineof at leastn * I polynomials continuous llrst order r.(rL) ) . 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. 10 0 5 0 o l .olation outside zx search iu (1) iU rL .29 I.5: 1.the loop terminates.300) : 0.r = t 4 0 0 2 0 5 0 1 0 0 1 5 0 2 0 0 2 5 0 3 0 0 . u) / z).946 .f rf tL/L:'tY((tt < x(iM) rT = rI{: t:lse a:Ilal end % Linear yi 1i inLerpolati..Thirdorder or cubic splines that ensure and secondderivativesare most frequently used in practice.Q9 .1.on . the interval containingx:.densit\'. >> densitv = f1. ilnd the linear interpolation Here is a MATLAB session illustratinghow the binary search function can be applied to calculate the air densityat 350 "C basedon the datafi'ornTable l5. Dependingon where it lies. i l.l5 O l 0. t'1" / (x(iL+1)x(iL) )*(xx :. The approachis akin to the bisectionmethodfbr root location.Although third and higher . ' l4 6 .nust be used.n) €irror: ( 'InterI. \'. .
r. f i + r * b i + r ( x i + r . .8) T h i se q u a t i oc n a nb e w r i t t e n f o r t h e n o d e si.t.Although quadratic splines are not of practicalin.r..r i ) 2 (16.hl : fi+t (16. t.1 ) .x. andc's)to evaluate.lt. The function valuesof adjacent polynomialsmust be equalat the knots. 16. .t i ) f c i ( r i + r . intervals and. . The objectivein quadraticsplinesis to derivea secoudorder polynomialfor each interval betweendata points.the nurnberof conditionsto be evaluated hasnow beenreduced to 2(n .5) wherethe notationis as in Fig. derivatives canbe discontinuous they usuallycannot bedetected visuallyand consequently areignored.7 ' ) : n c o n d i t i o n si .2.(ri . The function must passthrough irll the points.r i + r ) f c . they servenicely to demonstrate the generalapproach fordevelopinghi gherorder splines. a 1 ( .)l which sinrplifies to ai : Ji (16.x i ) * c .11 (1 6 .The polynonial for each interval can be represented generally as s i ( r ) : a i t b t Q .This condition can be writterrfbr knot i F I as .filbi\i+r ti+r)2 . 7 ) This equationcan be simplified nrathematically by defining the width of the ith interval as hi :xi+txi Thus. Since t h i sa m o u n t s t o nl t m e a n st h a t t h e r ea r e 2 ( n .l).we havedecided tofirst "quapolynomials. n l.It mathematically can be expressed its . . ( 16.r.This resultcan be incorporated into Eq.: 1 . 1 x.consequently..These illustratethe conceptof splineinterpolationusing secondorder dratic splines"havecontinuous first derivativesat the knots. .For n datapoints (i : 1. .\.t .xi) * c.fi I bihl t c.7)simplifies to . n). .1 (thed's.Eq.5): .3.fi : ai I b.x i ) 2 : . Because the derivationof cubic splinesis somewhat irrvolved. or are required 3rrequations conditions to evaluate the unknowns.(.. b's.rportance. therearen . 3(n .366 SPLINES AND PIECEWISE INTERPOLATION when usingcubicsplines.r .(r ) : l. These canbe developed as fbllows: l. Note that because we have detenninedone of the coefficients.the constantin eachquadraticmust be equal to the value of the dependent variableat the beginningof the interval.l) unknownconstants Therefore.(16.r .( n .This is called a continuitycondition. .6) EX Therefore.)l 2. 1 remaining conditions. .) f ri (.
Equation(16. (16.:o5 h t : 1 .5)is 2c. l ' z * b 2 h 2. J z: 1 .. Although thereare a numberof different choicesthat can be made. this conditioncanbe expressed mathematically as ct :0 The visual interpretation of this conditionis that the first two points will be connected by a straight line.5)can be ditferentiated to yield : bi l2cii.this meansthat2(41)l:5conditionsarerequired.h.xi) sj(.lcfi\:7. For the present Therefore.4 . Fit quadratic splinesto the samedataemployedin Example16.3 AUADRATIC SPLINES 3. Unlesswe have some additionalintbrmation regardingthe functionsor their derivatives. Use the results to estimate the valueat. 5 14:9. 1E 6.This is an importantcondition.I6. Assumethat the secondderivativeis zero at the first point. ratherthanin thejagged fashionthat we saw for the linear splines.2 conditions. because it meansthat adjacent splineswill bejoined smoothly. 0 l.v.(n .8)iswrittenfbri:1 through3 (with cr : 0) to give .l). 5: 2 . Solution.?s l.i f I can therefore be written as bi *2ciht:bi+r 116.filbrlt1 :7.r1 The equivalence ofthe derivatives at an interiornode.2 EXAMPL Q u q d r o i i cS p l i n e s Problem Stotement.we selectthe fbllowing condition.1 4. we must make an arbitrarychoiceto successfully computethe constants.91. (16.r : 5. Equation( 16.0 .I :2 : recallthat cr 0): br:bz b z* 2 c z h z : b t The necessary furrctionand interval width valuesare l?. . 0 . 367 The first derivatives at the interior nocles must be equal.This meansthat thereis n .we havefourdata pointsand rz: 3 intervals. after applying the continuity condition and the zero secondderivative condition..creirtes an additional 3 .?. Writing this equation for all the interior nodes amounts to .l c 2 h i : f t ) Jt lb.\ { ll: I '\ conditions(again. Because the secondderivativeof Eq. Continuityof derivatives.I (Table16.9).2) : I remaining condition. Eq.0:2. problem. :4 .r .
. 0 ) r Because x : 5lies in the secondinterval. 0 ) .)) .66 + 0. r . 5+ . Tlre objective in cubic splinesis to derive a thirdorderpolynomial for eachinterval betweenknots as represented generallyby s . are required for their evaluation.7 .368 SPLINES AND PIECEWISE INTERPOTATION These valuescan be substituted into the conditionswhich can be expressed in matnx form as Theseequations can be solvedusingMATLAB with the resulrs: br:l b:.r0) T h u s .4. . 1 ( x:) 2 . ( . 2 ( . . 16. . 16. .1(The total quadraticsplinelit is depictedin Fig.5(x3) t 0. 5 1 2 : 1 .( . 5 + 2 . r . . J A A.5 . . .r2to make the prediction.1 . r i ) 3 (r6.l) conditions known coefficients to evaluate. . t h e r ea r e / ..5)r s . Consequently. r .64(x .The shortcomings of Iinearand qr"radratic splineshave alreadybeendiscussed. r i ) 2 + r / . 0. r ): 1 .r . as a consequence.. 0.l 6 j [Ii'i' ll {l}fl These results.r1(5) : 9.. r.. 16. 2 . ( . The cubic splinesin thenext section do not exhibit these shortcomings and.. r.6.Cubic splinesareprefered because they provide the simplestrepresentation that exhibitsthe desiredappearance of smoothness. f o r n d a t ap o i n t s ( i : 1 .we use. are bettermethods for splineinterpolation. r ) : a i ' l b ._ r .4(. : 1 h.s) s 4 . ( .r.7 .. 1 ) .cubic splinesare most frequently used in practice.I i n t e r v a l s and 4(n.4 CUBTC SPLNES As statedat the beginningof the previoussection. r .4b.+. ) * c i ( . along with the values for the a's (Eq.6). .1 . ( . can be substituted into the original quadraticequationsto developthe following quadraticsplinesfor eachinterval: sr(x)2. 6 ( r .1 . .Notice that therearetwo shortcomingstlratdetractfrom the fit: (l) the straightline connecting the first two pointsand (2) the spline fbr the last interval seemsto swing too high.l) un. they tend to exhibit the instabilities Quarlic ol higherordersplinesare not usedbecause inherentin higherorder polynomials.
ti) J^i bi(r * ci(x xi)2* di(r .4 CUBIC SPLINES 369 The tirst conditionsare identicalto thoseusedlbr the quadraticcase.I6.the lirst condition is that the spline must pass through all the datapoints. t i ) 2 * d i ( x r.we had to arbitrarily specily a zero second derivativefor the first interval.2.Tlrat is. 16.11) .1 Derivqtionof CubicSplines As was the case with quadratic splines. Further. Two of the more popular are the clampedcondition anclthe notaknotconditions. ) l xi) * ci(.v ): l . This resultcan be incorporated into Eq.r. Whereasit is certainlypossible to developcubic splinesin this fashion.these last two conditions can be formulated in severaldifferent ways. f i + r (16.13) . After theseconditionsare developed. For cubic splines. In additronto these.r is to assurne that the second derivatives at the first and last knots areequalto zero. we will lirnit ourselves to natural splines. J'i: ai I bi6iwhich simplifies to ai : .10): s . 16.hencemaking the resultasymmetric.We will describetheseoptions in Section 16.This is a much nicer outcomethan occurredfor quadraticsplineswhere rve neededto specify a singlecondition. we would havethe 4(n .fi (16. There are a variety of other end conditionsthat can be specified.In that case.3 (t6.4..conditionsare developedto ensurethat the secondderivatives at the knots are also equal. ( . therefore.we will present an altenrative approach that requires the solutionof only n .12) Next.the simultaneous will be tridiagonal equations and hencecan be solved very efficiently..Although the derivationof this approachis less straightforward than for quadraticsplines. Once the additional end conditions are specitied. we will apply the conditionthat eachof the cubicsmustjoin at the knots.I ) unknowncoefficients. this can be represented as J ' i I b i h i .This greatlyenhances the fit's smoothness.t t ) 1 h l: . Specification of suchan endconditionleads to what is termed a "natural" spline.2).4. they are set up so that the functionspassthroughthe pointsand that the first derivativesat the knots are equal.A very common approacl.For knot i * 1. we are in the advantageous position of needingtwo additionalconditionsand can. For the following derivation.the constantin eachcubic must be equal to the value of the dependent variable at the beginning ofthe interval.the gain in efficiencyis well worth the effort. apply them evenhandedly at both ends. It is given this name becausethe drafting spline naturally behaves in this fashion(Fig.rr Therefore.1) conditions needed to evaluatethe4(n . two additionalconditionsare requiredto obtain the solution. (16.l c y h i .The visual interpretation ofthese conditionsis that the function becomesa straight line at the endnodes. For cubic splines.x .I equations.
23)and the resultsimplified to yield t2(hi r .17) fbr d1 : t u.20)can alsobe reduced by l: b i : b i .20) Equation(t6.t .i3) ro give h? Ii I bihi t lt2ci* t c .:ci+l J11.370 SPLINES AND PIECEWISE INTERPOLATION where hi:xi+txr The first derivativesat the interior nodesmust be equal.I * I can therefore be written as bi l2ciht +3dihl : bi+l (16. 1 5 ) rogive b i + t: b i I h i k i i ci+r) (r6. .1) : f i * t (r6.J ' I t 1 . 1 7 ) (r 6r 8 ) This can be substituted into Eq.Jrl t ( 16.^ .21)and (16.r9) (16.Equation (16.21) .xi)2 (r6.Equation(16.xi) ( I6 .r4) The equivalence of the derivativesat an interior node.rr) * 3tli7 .hi)ci * h .r * c . l).^ il/c i tc.1 c 1 1 .: o f.22)can be sr"rbstituted Equations into Eq. (16. I f 1 can thereforebe wntten as ci l3dihi : ci+t Next. c .15) The second derivatives at the interiot'nodes must alsobe equal.r1) (r6.21) The inder of this equationcanbe reducedbl' . ci (1 6 . (16.1 : . trC.'l (16.rti h. we can solveEq.23) (16. ( 16. f. * rJi . ( 1 6 . hi .r * f t r r ( c i .. .72) The index of Eq. . hi.12)is dift'erentiated to yield sj(x) : bi {2c. 1 6 ) The equivalenceof the second derivativesat an interior node. : 3 1 I } .) ( 16.r fffr h.(x . (16.19) can be solvedfor .14) can be difTerentiated to yield sj'(r) : 2ci * 6di(x . .18) Equation c a na l s ob e s u b s r i r u r e in dt o E q .f'r lli1 llC.
26) becomes c u . 1[ .h. this condition amountsto settingc1 eQual to zero.l 3(flx.r2.l qolve (16./[.r. ( x .. (16.flx.t t 6d. One common approach.rr]) 3(. .. conditions.:)0 :2c..f[x:.2'7) .. lE 6q )c : 0:2ct si'(. . the naturalspline. r'fwe havetwo additional Therefore.anbesettozeroasin 1e 6(.. .thesecondderivativeatthefirstnod .t (16.t1 Cnl cil 0 . 0 fhc srrqtem is tridironnal and I . tridiagonal equations with n results in n .2a) can be written as /r11ci1 *2\hrr .Eq.3 sinrultaneous we can solvefor the c's. . in which caseEq.x..3. we can convenientlydefine an extraneous parameter c.assumes that the secondderivativesat the end knots are equal to zero.z I h.i + r .rr.4 CUBIC 37r This equationcan be made a little more conciseby recognizingthat the terms on the (recallEq. cr..1 : c r r: 0 ." .t * 3 d . Thus.tz. t il ri "f[. 15. 'n I unknown coefficients. As statedpreviously. Jt rl rirl Therefore.. To see how these can be integratedinto the solution scherne.. c2.2s) . The sameevaluation canbe madeat the last node: s j j .we set ('/? 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. . (16.rn rl .26) Recalling Eq. r]) ( 16.t 2(.17). '. the remaining coeffiand (16..15): sidearefinite differences righthand / t r i .h)ci I hici+t: 3 ( .l t r .t.2' which Equation (16.ri.rr) ( x 1.21) cients.( 16. (..the two additionalend conditionscan be torrnulatedin a number of ways.0. r i ]' f.Eqs.18)can be usedto determine Once this is done.f. r 1 ) * 6d1 Thus.b and d. tlt.25) can be written for the interior knots.SPLINES I6. (16. . to impose a zero secondderivativeat the last node. i :2..
the valueat r : 5.0)' t 0 .186565212 dz : 0.1 and16.t .0 f)\ h 1: ! .r e 8 2.022053232(x. t E q u a t i o n( s1 6 .0:2.121156654 ..11 s 2 ( x ): 1 r 1.5 +0. 0. . can be substituted into Eq.160456274 cz:0.*. which talls within the second interval.5 ) + 0 .is calculated as 5 4 . 1 6 0 4 5 6 2 1 4 (r 4 .x3 'llI I:l I o' 2thtrhtt lr I l:l I I I The necessary lunctionand intervalwidth valuesare 1t :2'5 fz : 1.l0) to developthe following cubic splines for eachinterval: s r ( .119711863 1).166539924(. 5 hz:7. s 6 6 r Tlre threeequations can then be employedfo computevaluesrvithin eachinterval. 0 : 1 .r.166539924 b1: b2 : 1. t 6 0 4 5 6 2 1 4 ( 54 .372 .r 1.tr 1 .s Thesecan be substituted to yield Ir lr5 L .0 .214144181 dt :0.r) : 2. .01. r ).5:2. : I 3ltt//l lrrr r.01. 5 . 1 ) U . 1 8 ) c a nb e u s e d to compute t h e b ' s a n dd ' s h :0. (16.0) .s ) + 0.2 ( T a b l e1 6 .along with the valuesfbr the a's lEq. 4 1 9 7 7 1 8 6 3 ( x 3 ) + 0 .ll o  .3 ./ I.. 8 3 9 5 4 3 1 2 6 ( : L102889734.0 .1 I....3 EXAMPLE S P L I N EA SN D P I E C E W I S IN ETERPOLATION NoturolCubic Solines Problem Stotement.r .5.5)r sl(7.5 ht:9. 16. 1 8 6 5 6 5 2 7 2 1r r.re equasetof simultaneous ) to generate tions that will be utilized to determinethe c cocfficients: hz) /1.7.2 Il lll167t.. 2 1 4 t 4 4 4 8 7 ( 5 4. Solution.5 Theseequations can be solvedusingMATLAB witlr the results: cr :0 ct : 0. Fit cubic splines to the samedatausedin Examples 16.022053232 These results.27 tl.0 . t i l i z et h er e s u l t s to estimate t h ev a l u ea t . (16.1 . 1 2 7 7 .4c.7.rr. . (16.831)543726 c+ : 0 dt :0.8395+3726 0. r : 5 .1l)1. 5 ) 2.0 l+ : O. J . For example. The first stepis to employ Eq.2 .5)3 s 2 ( 5 ) : 1 .r.0. 0.l{ii} 1'l rt. 20.0)r sj(. The total cubic splinefit is depicted in Fig. 2 1 ) a n d( 1 6 .l6..
As expected.We a cubic interpolating polynomialou Fig. 1s ) a s w r i t t e n i n T a b1 6. Two of the mostpopularare .I6. 6 .. These conditionscan be readily applied by using Eq. tt6. 16. specimeansthat the samecubic functionswill apply to fying continuous third derivalives segments. i:2.l. .4.3 .This is due to the fact that the naturalsplinerequireszerir secondderivativesat the end knots.This option involvesspecifyingthe first derivatives it is what occurs and lastnodes.tIhu 2c'.lore thespeciled respeci dtrlvotlves vely) ct thefirst ond iostnodes./i .3. r . x1 h : t t .Although the cubic have also superirnposed spline consistsof a seriesof thirdordercurves. N o t i c e t h e p r o gressiveimprovementof the flt as we move from linear to quaclratic to cubic splines.Notice how the secondderivathe natural spline tendsto straighten out as would be expected because tives go to zero at the ends. The clampedcaseis setup so that the derivatives at the endsareequalto zero. c o n d i t i o nfs Firsi ond lost Equotions r : 1: Q . Sincethe first internal knotsno longer eachofthe first and iasttwo adjacent represent thejunction of two different cubic functions.{ l t t * h : l c z * / z r r ' :: O hn tcn)0t. .. This is sometimes calleda "clamped"splinebecause when you clamp the end of a draftingsplineso that it hasa desiredslope.. " NotaKnot" End Condition.. : Q (rvhere Cicmped first ..25t fbr the interior knots.2 Condition Noiuro Thefirstond lostequotions needed to specify somecommonly usedend o r c u b i cs o l i n e s .Because it hasnonzerosecondderivativesat the ends.In contrast. : 3 l . Hence.. .. at tlrefirst Clunlted End Condition.For example.. .2.the resultingfit differs fronr that obtained usingthe thirdorderpolynomial. if zerolirst derivatives the splinewill level off or becomehorizontalat the arespecilied. the spline fit for the clarnpedcaselevels off at the ends. ends. TABTE16.A third alternative is to fbrce continuity of the third deknots.:I11.:Q .trl. the naturalsplineis only one of several end conditionsthat can be specified for splines.5showsa comparison of the threeendconditions asappliedfo tit the datafrom ?rble 16..Sincethe splinealready specifies that rivativeat the second andthe nexttolast the tunction value and its first and secondderivativesare equal at theseknots.2 End Conditions Althoughits graphical basisis appealing. ' .It hasthe additionalpropusingan ordinarycubic erty thatfor firur points.4 CUBIC SPLINES 373 T h e r e s u l t s oE f xamples l6.it yieldsthe sameresultasis obtained polynomialof the sortdescribed interpolating in Chap. lloto knol 2 h 1 t 11 ' f t 1 r ' 2 i7J. .4c.the natul'aland notaknotcasesfollow the trend of the data points nrore closely.. 16.l through l6.n2 .l h r = 1 c .. 15. 1 t . . i<r.they are no longer true knots.the notaknotexhibitsmore curvature. . .3. 4 .li cnd 7. l l . 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 on le Figure 16. 1 a 2 h .3aresummarizedinFil g 6 .this caseis refenedto as the "notaknot" contlition. whereas the cubic polvnomial has no such constraint.
The interpl function canalso implement spline and Hermite interpolation.1 MATTAB Function: spline Cubic splinescan be easily computedwith the builtin MATLAB function.374 AND PIECEWISE INTERPOLATION SPLINES f(x) 2 t 1 0 I6. but can also perform a numberof othertypes of piecewise interpolation.then the first and last value in y areusedas the derivatives atthe end points.28) where x and y : vectorscontaining the valuesthat are to be interpolated. doto from Toble 16 l I6.5 PIECEWISE INTERPOTATION IN MATTAB The spline MATLAB hasseveral builtin functionsto implementpiecewise interpolation.splir.andyy = a vector containingthe resultsof the splineinterpolation as evaluated at the pointsin the vector xx. for lhe ond noturol splines Comporisonof the clomped (wilh zero firslderivotives). )<x) (16.4 S p l i n e si n M A T L A B Problem Stqtement. Consequently. By default. yy = spl ine (x. 16.7) : f(x): I  + 25x2 . in this chapter.if y contains two more valuesthan x has entries. Ithas the generalsyntax. The pchip funcfunction performscubic splineinterpolationas described tion implementspiecewisecubic Hermite interpolation.5. EXAMPLE 16.re usesthe notaknot condition.5 FIGURE nofoknot.However. this option providesthe meansto implement the clampedend condition. spline. y. Runge'sfunctionis a notorious example of a function that cannot be fit well with polynomials(recallExample r 5.
I6.6.the notaknotspline doesa nice job of following Runge'sfunction without exhibiting wild oscillationsbetweenthe points. we cau generate along with the splinefit and the original data: >> yr >> plot = L. linspace(I. yy. : Solution. a sn?ooai vectorof valuescan be generared we can crearc Next. ' ) As in Fig. Recall that I inspace automaticallycreates100 points if the desirednumberof points are valuesfor Runge'sfunction itself and display them not specified.5 PIECEWISE INTERPOLATION 375 Use MATLAB to fit nine equally spaceddata points sampledfrom this function in the interval[1.Y. 16.9).xx) . yr..^2).Finally. (a) The nine equally spaced datapoints can be generated as in >> x = >> ]1 L. l].1). a more finely spaced so tFlat plot of the resultsas generated with the spline function: >> xx = >> YY sPLine 1."2) . xx. (x. o f f i : 1 a n df i . xx.I. (i+25*x.IN MATTAB I6.Employ (a) a notaknot splineand (b) a clampedsplinewith end slopes 4. (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. 'o ' . linspace( (x.. .6 FIGURE funciion(doshedlrne) spllnefit generoted wilh Comporisonof Runge's with o 9poinlnotoknot MATLAB(solidline).y ./ (1+25*xx.
The various methods are .'') As in Fig. edgeof the true first derivatives. the interpolation looks like a seriesof plateaus. 16. This methodusesstraightlines to connectthepoints.xx)..5 F I G U R EI 6 . Thus. 1 (x. 'rneLhad t wherexandyvectorsconlainingvaluesthataretobeinterpolaty e id :a . .2 MATTAB Function: interpl The builtin function interpl providesa handy meansto implementa numberof different typesof piecewiseonedimensional interpolation.the clampedsplinenow exhibitssomeoscillationsbecause of the artificial In other examples. >> yyc = spline(x. 16.yc. where we haveknowlthe clampedsplinetendsto improve the fit.5. slopesthat we have imposedat the boundaries.376 IN ET E R P O L A T I O N S P L I N EA SN D P I E C E W I S 0.vectorc0nthe interpolation points taining results of the as evaluatedat the in the vector xj.el. . .It has the generalsyntax yi = interL.7. and 'methocJ' : the desired method. 'o' . 'nearest 'nearest neiehbor interpolation. EXA generate and plot the splinefit: >> yc = [1 y 4]. xi. This methodsetsthe valueof an interpolatedpoint to the value of the nearestexisting data point.ies'especr. >> plot (x. 7 (doshed functlon line) Comporison of Runge's wiih o 9 pointclomped end spline fitgeneroted withMATLAB Notethotfirst derivoiives of I ond 4 orespecifred ct thJlelt ond isolidine) r i g hb ' oundo. which can be thoughtof as zeroorder ' I inear ' linear interpolation.y.xx. 1:.yr.xx /yljc. polynomials.
'nearest' >> nl ol. tt.5 PIECEWISE INTERPOLATION IN MATTAB .. This is identicalto the spline iTItt"Ji una ' cubic 'piecewise cubicHermiteinrerpolarion.pchip has no overshoots and lessoscillation if the dataare not smooth. the defaultis linearinterpolation.as well asthoseinvolving the otheroptions.ThesetradeofI\. 377 'spL. tt. Therefore..However."That is.\n) ).The time seriesof spot nleasurements of velocity can be 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 (b) nearest to fit this datawith (a) linear interpolation. fit with linearinterpolation.(t.vl) The results(Fig. As with cubic splines.Rather.tt. .rt).(c) cubic splinewith notaknotend conditions.ra test drive on an automobilewhere you alternately accelerate the automobileand then hold it at a steadyvelocity. >> vl >> plot (t.' o' . (b) The commandsto implementand plot the nearest neighborinterpolationare >> vn . 110 ) . >> tt = incerpl(t.they are expressly chosenso that the interpolation is "shape preserving.''1r l Problem Stotement.but do not exhibit any overshoot.it ditTers frorn cubic splinesin that the second derivativesarenot necessarily continuous. Further the first derivatives at the knots will not be the sarneas for cubic splines. You perfom.The results of using spline will generallyappearsmoother because the humaneye can detectdiscontinuitiesin the secondderivative.v.it will be moreaccurate if the dataare values of a smoothfunction.8c)are not smooth.I6.v. Solution. to connectdatapoints with continuousfirst derivatives. the interpolatedvaluesdo not tend to overshootthe data pointsas can sometimes happen with cubic splines. and plottedwith the following commands: >> r . = finspace ( O. >> v = l0 2A 20 38 B0 80 100 10A \25 7251. e.ine'piecewise cubic spline interpolation.[0 20 40 56 68 B0 34 95 t04 110].5 TrodeOffsUsing in.interpl (t. On the other hand.are exploredin the following example. EXAMPLE 16. Note that you never decelerate during the experiment. 16.In addition. The pchip option (short for "piecewisecubic llermite lnterpolation")merits more pchip usescubic polynomials discussion. neighbor.therearetradeoffsbetween the spline and the pchip options.and (d) piecewise cubic Hermite interpolation. (a) The datacan be entered. If the 'method' argunrent is omittecl./.. .' o'.'.
tt.the resultslook like a seriesof plateaus. v s ) 'spline' ) .However. v. t t .8c)are quite smooth. interpl ( t . Theseresults(Fig..' o ' . on o velocilyiime serles funciionto performpiecewisepolynomiol nterpolciion This option is neithera smooth As in Fig. 16.8 Use of severoloptionsof the rnterpt toron ouiomobile." . (c) The commandsto implementthe cubic splineare >> vs >> plot = (t.This makes it appearthat the automobiledecelerated .378 IN ET E R P O L A T I O N S P L I N EA SN D P I E C E W I 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.8b. . nor an accurate depictionof the underlyingprocess.severeovershootoccursat several severaltimes duringthe locations. 16.
. the simplest caseof twodimensional we will describe interpolationin Cartesiancoordinates.V. F I G U R EI 6 .I 6.'pchip'). Because ofits shapepreserving velocities increase rnclnotonically nature.f(rr.we have valuesat four points: yz).Wewanttointerpolatebetweenthesepoints "f(xr.6 MULTIDIMENSIONAL INTERPOLATION (d) The commandsto implementthe piecewisecubic Hermite interpolationare >> vh = >> plot interpl(t.'o'.the and never exhibit deceleration. 16. 9 r{ vodrrrersio dl i G'opr.6.rethods for onedimensional sionerl interpolation.we will describeMATLAB's capabilities for rnultidimensional interpolation.8d)arephysicallyrealistic. In this section. I 6.tt. tt. o l e "{ given circe) i se s t i m c t e d v o l u e{ so p e n b osed on four circles) f(xz.t:. In addition. (t.\/.f(rz. 16.vh) 379 For this case.r':). 16. yt) .. Although the resultis not as smoothas for the cubic splines.6 INTERPOTATION MUTTIDIMENSIONAL problemscan be extendedto multidimenThe interpolationn. the results(Fig.f (xi..co ole p c l i o n o on r lo r ln e o ' i n e p o l o ' i o r w l e e o n .andl(. ]r).r'r).. continuityof the f irst derivatives at the knots makesthe transitions betweenpointsmore gradualand hencemore realistic.1 BilineorInterpolotion 'lnaodintensional interpolctrion deals with determiningintermediatevalues for functions of two variables z: .r'r)..9. As depictedin Fig. l e ' n e o o e .
.r ' r ): 3 f Jt andat (. h e .r . :: )} .r _ r': )Z (16 34 {i .r. )'r ..ri. r r y V2 It ! + " t { .r rJ . .  \'r 116.380 S P L I N EA SN D P I E C E W I S ET E R P O L A T I O N IN EX FIGURE I6. l { . r . the resultat (r.\'2 )'r .r.1)f l .v:) . .:) . ri t: ]i . r ' .311 A single equation can be developedby substitutingEqs.. . _vi there...rl l'(. .. f( r i .r. o L e co ..i .r1) is . o r o d e . First..30) into Eq.rl .) rl . . r . . re to to estimatethe value at an intermediate point / (ri.ft. (16." X2 .t.v r ) (16. v.^ r .. r .10) Thesepointscan then be usedto linearly interpolate along theI'dimension to yieldthefinat result: .. we can hold the v value fixed and apply onedimensional lineu interpolation in the.Suchfunctionsare calledbilinear./ ( x )t }: lr \'' ) .Xt .e .r'dimensiontoyieldth es f iu nl o i tr. r n e .rr ).. ) .r'1 . r . .... r )+ S f x2 .31) to give .rt ): .v: r'' 't2 't '{ixt '\'i .2s) r.Usingthe Lagrange form.r:) / ( x i .lr ^.rrr)is .29) and (16.fZ .rr. 16.{l )z _ ). sult is a plane connectingthe points as in Fig.9.10.rt t r : . r o i m e n s .J:) .\'l \'.. r r ) f(x.. A simple approach for developingthe bilinear tunction is depictedin Fig.rdirection..IO Two dimensionol bilineorinlerpolction con be implemenled by f irstopplyinq one dirnensioncl ' e o ^ l ^ ro c l o l i ^ no r o n q' n e . (16.. If we use a linear function.t2 16.'t): .f ' l.r'r 12 . i o l e s c o r t ^ e nb e usedto inecrlyinterpoloteolonglhe. 16.
2 Multidimensionol Interpolotion in MATLAB piecewiseinterpolaMATLAB has two builrin functionsfor two. if the.a simplerepresentation ate in a similar fashionto interpl (Section 16. As with interpl. thesefunctionsoperFor example.4. spline.2).6as >> X= >> y= >> >> zlnl 2 9). u. + . _ { .6 Bilineorlnterooloiion 381 Problem Stotement.5.8) 5r .252 4 . xi.(16. z..).6): 55 : 5l .y. and method: the desiredmethod.259 4 o u. y.2594. that is. u.. For example.1) T(9.6 MULTIDIMENSIONAL INTERPOLATION EXAMPL 1E 6.25and1'. 6A 51 . 6 ) .2113  6. erp2(x..8 5. Substituting these values intoEq.5./8 .4. o f t h es y n t a x of i n e o2 is zi = Lt\re(p2 lx.8.2r43 . linear. j ) .matrices containing the coordinatesof the points at which the values in the matrix z are given.6 29 l6 \ 6 5. 1 :)6 0 T(2. Supposeyou have measured temperatures at a number of coordinateson the surfaceof a rectangular heatedplate: T ( 2 .6. zo : 6r.+ 92 t6 s}s24Rl 5.and threedimensional tion: interp2 and interp3. a n dc u b i c .5 T(:9.the defaultis linearinterpolation. nearesf . As you might expectfrom their names.6):10 interpolation Usebilinear to estimate thetemperature at.Note that the methodsareidenticalto thoseusedby in*'erpi.nethod argument is omitted.25.5.8l ss + . 'mechod' ) where x and y .I 6. yi.interp2 can be usedto make the sameevaluation as in Example 16.55 701. 1 6l.2. gives Solution.32) : "/ ( ) . zi : d matrix containingthe resultsof the interpolationas evaluated at the points in the matricesxr and yr.rr: 5.
dT az (r6. p : density (31 g/cm3)."C Temperoture depthduringsummer versus for Plotte Loke.3 22. The location of the thermoclinecan be defined as the inflection point of the temperatur€d e p t hc u r v e . 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.As a result.3 2.11. The temperaturegradient is important in its own right becauseit can be usedin coniunction with Fourier's law to determinethe heatflux acrossthe thermocline: J:DpC. cv: an eddy diffusion coefficient (cm'ls).33) where "f : heat flux [call(cm' . natural cubic splines are employed to determinethe thermocline gradientfor PlatteLake. 16.382 AND PIECEWISE INTERPOLATION SPLINES . decompositionof organic matter can lead to severedepletionof oxygen in the isolated bottom waters. and C: specific heat[: I call(g .As depictedin Fig.In particular. H E A T R A N S F E R Bockground.warm. buoyantwater near the surfaceoverliescolder.I I FIGURE Temperoiure versus forPlotte depth during summer Loke.t 27.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. Michigon r ('c) Epilimnion Thermocline Hypolimnion TABTE16.6 T.2 ilt 0 228 . t h e p o i n t a r w h i c h d 2 T / d z 2 : 0 . In this case study. Thermal stratification has great significancefor environmentalengineersand scientists studying such systems. I6. s)]. Michigan (Table 16.9 tl. C)]. denserbottom water. Michigon.t h a t i s . 2. separated by a plane called the thermocline.7 22.9 228 9 I 2A6 137 t3I 1 83 11. Lakes in the temperatezone can becomethermally stratified duringthe summer.8 4.3). .the thermoclinegreatly diminishesmixing between the two layers.01 cm2/s.
. n d d r:celfi.h(x.ly rrr:st be same L. iol c i:oetficier.irrr. y . Sucha codeis shownin Fig..we setup and solveEq. After somepreliminaryerror trapping. .. * 1lrili + i') v)).r r .i r:e.i.:i+njs (r lr + l. ."ivofives with o noiurolspline.y. L) r x.ts : " S O l . x..a l ues der.because it usesnotaknotend conditions. r " : i! : ] . t.fEL I') Lal h.27)for the secondorder (c).c . t : . . .f \.. l n . xr . .'(x.r e n t : .ttf ie : rl .1 ire xx) uses . / i i). l \ .the sp1 ine functiondoesnot returnthe first and secondderivatives we require for our analysis.i c " L y . :c . 16. +lj t. ontinued Solution. .12. 1 . i 1.':'. ' a: : i a h . r s c e n c r r : r r ') . r a . * (2 * c(:) = c1i  1)) (continued) .i j ndepenLlenL ricpenrlelt .yector The vecLcf xx. ...j e J .. e n d '.xril: : a)n la. .=: r. s. b . ..r.7.] 6.'i.{){ r:: iirsi ::=i:.y.1: ri aiirr iiat ir II Il'  anr.r!{th' ) .. end x n o t s t : ./ 3 / lilx. Further. .r . .r. r L 1 ( .r) . i) cr{:L)) . r b i c rtnderlving r :pLtne furr:Lion l.t € f ilo l. i. Notice how coefficients FfGURE r6..Unfortunately. t h L e : : i. l t s . However. the varues oi the ::. r l spi.'dlii lrl  r r a r .Note fhot fhe oif f {unclion employedfor errortroppingis describedin Seclion 19. I . i 1.J'ialt:es i. we want to usenaturalsplineend conditions to performthis analysis. (16. r j i f l .2 CASE STUDY 383 " .: i p l i n e 1r . I 2 * r l r{ x . l.lrtl:'.ri e.vaLiv:r ii'e: a: ai ar. il i). .12 MJile fo deiermineintermediole voluesond de. : b r e s . / \. it is not difficult to develop our own Mfile to implementa natural spline and retum the derivatives. slle.1t(r.the builtin MATLAB spl ine function doesnot meetour needs. As just described.1 .hi. e s tf cle!)€:n.) f r f r d ua t t r:s in j. i)). ' ar i . hlr:. fci(j. lc . / l. . ?solve i ll.
b.)z . .x ( i i ) + I * r l ( d y i . d. = 5 1i I ) + . (16. n.f"{x1 :2ci + 6di.9 17. and d). to determinethe values and the derivativesat the desiredintermediate values.i ) * ( x l .8 22. *(xi x(ri))^3. 16. i ) ) ^2 . a.9 9.)).h 1 (x.zi .x.L1. for end iun l n i. At this point..1:m %perforn interpolaLions at desired values . enC function lyyy. i) hl'r = x(r + i) .C'J fdri. d. to compute the required finite differences.t2 lContinued) we use two subfunctions. br eak end end end FIGURE t6. we solve for the c's with back division. dyy.y(j)) fcld .000001 yyya (1i) +b ( ii) * (xix (ri) ) +c (ii) * (x!.8 20.00U001 & xr <_. * c ( i . Here is a script file that usesthe naLspl ine function to generatethe splineandcreats plots of the results: z = i0 2.xi)3 We can also determine the lirst and second derivatives by differentiating this equation twice to give '(x) : bi * ci(x . b. .3 22. d2yl gp] inelnterp f nr i i 1 . 0. ?i l inqnaeo(z(1\.rr) + ct(x * x)2 * di@ . x .ix (ii ) ) . 1) . these equations can then be implemented in another subfunctign. . xr ) if . h and fd. end enC frrrlctfon hti = 11*. ) d2y2*c (ii ) +5*d (ii) * (z.7 I1 . r=122.Splinelnrerp(x. c. 1 i i ) * (xi x {. A loop is thenemployed t0 generate the other coefficients(a.x (ir ) ) ^2+o( ii) .384 SPLINES AND PIECEWISE INTERPOLATION coniinued I .1 13.Once Eq. lyt'(r).7 IB.12. . a.(y(i) r' (.xi >= x(ri) x(ii + i) + 0.) * 3diQ . xx(:.27) is set up. b. splineTnterp.dy{r).8 22.2(i)l n.r(i) x(j)). c.x.21. .d.x(i).xi) f As in Fig.t) 27. we have all we need to generate intermediate values with the cubic equation: f (x) : ai * bt(x .6 13.3 4.l 17.
pfor(ai.13. z.ll'ciaca'.!rlot (d't2.s depth (ml generoted with of {o) temperoture. {b} g'odient. ieqrei.t .I6. zzJ .T.5 m. ( a )T \bl dr/dz (cl dzTtdz2 _L______ 10 10 .zzt c 1 z ') .i.n..I3 Plots uers..The result is that the thermocline is located at 11.5 0.The rhermocline islocoted ot the in[lecrion point o[ ihe temperoruredeplh curve..35 m where the gradientis .grid . zz) I )..5 .1. \6..9r!v / .' a'. 10 \ t5 F F_ I 15 rI I I I I t5 rI I L 20 I 20 zv 25 25 25 30L 10 20 1n 30 1 2 30 0. FIGURE T6.. We can use root \oeation (zero second denrative) or optimization methods (minimum fust denvative) to refine this estimate.TT.61'C/m. ond (c} secondderivotive the cubic sprineprogro.. the thermoc\ine appearsto be \ocated at a depth of about 11.Pioi ' l. zz) As in Fig.' l " rcvc!s /. (T.'reverse').7 CASE STUDY " continued natsptine \2.
16.4 x Depih. The depth of the thermoclinecan be defined as the inflection point of the temperaturedepth curvethat Fit this datawith a (a) cubic splinewith notaknot endconis. it alsois anexample neering of numerical differAs such.If k:0.4 05 O 0l 5 .01 (x0. Here are some valuesthathave beengenerated at intervalsof 0.^l=.the heat ditions and (b) piecewisecubic Hermite interpolation.r range.846 2t 703 r6.* g.However.04 0 The humpsfunction exhibitsboth flat and steepregions ovet a relatively shon.create a plot comparing the fit with the exact with Fourier's law: humpsfunction.:0 ts 3 ' 9 .We will be describing thetopicof numerical differentiation in detailin Chap.19.l Given the data to Use a clampedcubic spline tit with zero end derivatives determine the thermocline depth. In ea whic f 16.8 and Thes evalr .4 Develop a plot of a cubic sptine fit of the following . "C) compute the tlux acrossthis interface. cm .'C 0 05 7A 7A I 55 t5 22 2 25 13 l0 3 l0 t2 r ftt) x 02 03 4.01 cal/ (s . In flux from the surface to the bottom layer can be computed both cases. n 2c 'a * r:"1^* (. d r J:Kdata with (a) naturalend conditionsand (b) notaknot end az (a) F ditio (b) F slopt tiatir 16.L cmr m / l 0 o c m d s \ cond cubi( x f (x) x f (x't The foregoinganalysis demonstrates how splineinterpolation canbe usedfor engiandscientificproblemsolving. I over the rangefrom. 1 7 6 1 5 4 7 t 45 887 96 500 47 448 19400 06 07 08 0.and (c) piecewisecubic Hermite interpolation. in tandem for problemsolving.386 AND PIECEWISE SPTINES INTERPOLATION continued with The gradientcan be usedto computethe heat flux acrossthe thermocline Eq.5 depi x PROBTEMS 16. the tank can be idealized as f $) ]t 692 12382 t7..0rt*' * r4 .3 The following is the builtin humps function that MATLAB uses to demonstrate some of its numerical clpabilities: f (x\ xl2 f{x1 I 5 25 7 5 I Fit this data with (a) a cubic spline with natural end conditions. (b) a cubic spline with notaknotend conditions.r = 0 to l: Fir tl ditio tions case 16.t6rI) * .it illustrates how numerical approaches from differentareas canbeused entiation.000 two zones separatedby a strong temperaturegradient or thennocliue.9 I Based on these temperatures.3)2*0.6 natu cate t6. .(16. 16. m Temperoture.33): J: al 86'4o 0. At this depth. the point at which d2Tldzz:0.2 A reactol is thermaily stratified as in the tbllowing table: (x0.9)2*0.7 orde 15. 16.
Note that the true value is 0.true) for each.npute a cubic spline 11twith natural end conditions.and (c) a spline.t from r : 0 to 2n.at "t : 1.10 (a) Use MATLAB to fit a cubic splineto the followins data: 16. and (c) a cubic spline.l4.[n each trons.6 Developan Mfile to cor. 16.and (c) piecewisecubic hermite interpolation.PROBLEMS conditions. create case.5683.018515 polynomial:.o I Fitthis datawith a (a) cubic spline with notaknotend con(b) cubic spline with zeroslope ditions.Test your code by using it to dupliExampleI 6. mathematical tables. 16.For example.8 Besselfunctions often arise in advancedengineering scientificanalysessuch as the study of electric fields.ll Runge's functionis writtenas I " *25rt Generatefive equidistantlyspacedvalues of this function over the interval: [.12 Use MAILAB to generateeight points lrom the function / (/) : stn.14 : 0. a plot comparing the fit with the step function.0. 16.'C 0 B t6 24 32 40 o. { ) : x e r!r t')rr Estimate polynomialand Jl2.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. clampedend condiand(c) piecewisecubic Hermite interpolation. colnpareyour plot with the following equation which wasusedto generate the dala: I"{ .I : t /(r) 0 0 02 0lrt 02 0. end conditionsequalto the exactvaluescalculatedwith differentiation. are olien compiled in standald .9 The following data defines the sealevelconcentration of dissolved oxygen for fresh water as a function of tempelature: 200 T. Note that the exactresultis 7.5. 1]. /(r) 2 172 4 22O 5 430 4. 16.3.Display the resultsgraphicallyanduseeachapproach to estilnate o(27). : approximatioll.556 24 A 5202 26 A 47OB Ineach case.l: r135679 1000 x v 0 2A 2 2A 4 12 7 7 l0 6 l2 6 Determinethe value of 1. Fit this data using (a) cubic spline with (b) cubic splinewith derivative notaknot end conditions.)24 (a) Fit this data with a cubic splinewith notaknotend conditions. In addition. but with zero first derivativesat the end knots.5 Thefollowing data is sarnpledfrom the step function depicted in Fig. 387 . Presentyour resultsgraphically.456i + 27. (b) (a) but useclampedend conditionswherethe end Repeat slopes are set at the exact values as determined by differenliating the function.986 mg/L.069x .l). therelbre. (b) a linear spline. develop a plot using (c) piecewise cubic Hermiteinterpolation. Createa plot comparing the fit with the tunction.912 9. (a) using an interpolating (b) using cubic splines.f(x) order * 3. and These functionsare usually not amenable to straightforward evaluation and.411. l6.(b) Repeat(a). Developplots of eachfit aswell asplots of the absolute error (8.mg/L 1462) IlB43 IBZ0 B4lB 73A5 64)3 Use MATLAB to fit the data with (a) piecewise linear interpolation. Fit this data with (a) a fourthorder polynomial.7 The fbllowing data was generated with the liftlr. cate 16. 16. 16.(b) a fifthorder polynomial.9125xr15.
the derivuriverepresents the rate o[ changeof a clependent variable willr respect to an independent variable.'#i$k*tffi#*ri lntegrotion o nd Diff. Infegrationis the inverse of differentiation.the derivativecan be visualizedas the slopeof a function.. iIwe are providedwith velocity as a function of time. l. t 'ii...rentiotion 5.Thus.1 I d b .differentiation provides a means to determineils velocity. if we are given a functionXt) that specifies an object'spositionas a functionof time.*"'!.the integral can be visualized as the areaunder the curve of u(r) from 0 to r.lb. Consequently.: '.Juqt as differentiation usesdifferences to quantify an instantaneous process. Mathematically.Among other 389 .just as a clerivative can be thoughtof as a slope. we have opted to devorethis part of lhe book to both processes.1 ovERvtEw In high school or during your first year of college. you were introduced to differential and integral calculus. For example.1n. integrationcan be usedto determine the distance lraveled: v(/) : fr' u(t) dr As in Fig. for functions lying above the abscissa.'i:'. PTs. PT5. as in: d u(l): y(l) dt' As in Fig.an integralcan be envisagedas lsummation. Because oflhe close relationshipbetweendifferentiation and integmtion.There you learnedtechniquesto obtain analytical er exact derivatives and integrals. integrationinvolvessumminginstantaneous information to give a total result over an interval.
390 PART 5 INTEGRATION AND DIFFERENTIATION 0 ) 400 200 0 tal 0 FIGURE PT5. book where we will cover differentialequations. Hence. We do this for several reasons.4. .the materialwill haverelevance to the next paft of the a numericalperspective.2 PARTORGANIZATION for numerical integrationthe Chapter 17 is devoted to the most common approaches replacing function These relationships are based on a complicated NewtonCotes formulas. a more highly developedareaof less sensitiveto roundoff errors.it makes it does have great significancefor the solution of differentialequations. formulas are in detail: the Simpson's widely usedNewtonCotes discussed wherethe data for cases 1l3 rule. All theseformulasare designed integrated In we also include a discussion of numerical to be is evenly spaced. sense to cover it as the last topic prior to describingdifferentialequations 5. althoughnumericaldifferentiationis not as widely employed. andSimpson's318rule. Second.I (o) differentiotion ond {b) integrotion The confrost behr. Three of the tabulated with a simple that or data trapezoidal rule. Although differentiationis taughtbeforeintegrationin calculus. in part because it is much you to the basicsof numericaldifferenltiation in Chap.een from things.Finally. we havealreadyintroduced in the following clrapters.this will provide the opporlunityto highlight their similaritiesand differences ln addition. addition.we reversetheir order First. polynomial most is easy to integrate. in Part Six. integrationrepresents numericalmethods.
Chapter18 dealswith two techniques that areexpressly designed to integrate and functions:Rombergintegrationand Gaassquadrature. wherethe integrationlimits extendbeyondthe rangeof the known data. The formulationscoveredin Chap. Although they are not commonly usedfor definite integration. This is a very important topic becausemany realworld applications deal with datathat is in this forrn.adaptiveintegrationis discussed. are ln Chap. we presentadditionalintbrn. Richardson extrapokttion. At the end of Chap.2 PART 39r integrationof unequally spaceddata. The effect of errorson both numericaldiff'erentiation and intesrationis also discussed. In addition. Topicsincludehighaccuracl. finitedffirence formulas.where tlre function valuesat the ends of the limits of integrationare known. and the differentiation of unequally spaceddata. 17. equations Computeralgorithms provided for both of thesemethods.4. 19.open integrationformulas here because are presented they are utilized in the solution of ordinary differential equations in Part Six. we presentopen integrationfonnulas. All the above materialrelatesto closedintegratiott. .fferentiation to supplement the introductorymaterialfrom Chap.ration on numeric:al di. 17 can be employedto analyzeboth tabulated data and equations .ORGANIZATION 5.
) V . YOU'VE GOT A PROBTEM h ftf l\ ecall that the velocity of a freefalling bungeejumper as a function of time can be computed as ( . .:' . Knowing how to implementthe following singleapplicationNewtonCotes formulas: Trapezoidal rule 1/3 rule Simpson's Simpson's 3/8 rule Knowing how to implementthe following compositeNewtonCotes formulas: Trapezoidal rule 113 rule Simpson's Recognizingthat evensegmentoddpoint formulaslike Simpson's1/3 rule achievehigher than expected accuracy. ' .unn uu\ . Specific objectivesand topicscoveredare .0. ' ' ..' : rotion NumericolInteg Form ulos CHAPTER OBJECTIVES The primary objectiveof this chapteris to introduceyou to numericalintegration.i \''l n rr rrr . . Recognizingthat NewtonCotes integrationformulas arebasedon the strategy of replacinga complicatedfunction or tabulated datawith a polynomial that is easy to integrate.f8a') : .14 . Knowing how to usethe trapezoidal rule to integrateunequallyspaced data.'. Understanding the differencebetweenopen and closedintegrationformulas.
Furlher.'. to integratemeans"to bring together. integrationprovidesthe meansto deterrrrine the distance fiom the velocity.rs..to unite. 1 1 7 .4) Although a closedform solutioncan be developed for this case.Calculus can be usedto solveEq. the term quadratureis generallytaken to be synonymouswith numerical definite integratior.I INTRODUCTION ANDBACKGROUND 393 Supposethat we would like to know the vertical distancez the jumper has fallen after a certaintime r.2) gives S u b s t i t u t i nE g q .l*. . As suggested by the dictionarydefinition. suppose that there was some way to measure thejumper's velocityat varioustimesduringthe lall.l Whof ls Integrotion? According to the dictionarydefinition. Figure 17. In fact. the integral expressed by Eq. (17."'n( .ln this situation. (17.to indicatethe total amount.r. I7.o (r 7 ..thereare other functions that cannot be integratedanalytically.5) corresponds to the area under the curve of l (x) betweenr : a andb.r) l. . Numerical integrationis sometimes referredto as quadrature.r. This distance can be evaluated by integration: fl :(r): / t'(ndr .1 Thr.r : b. evaluated betweenthe limits x : a to . or summation.E')l (t7. Today.it would also be possibleto integrate the discretedatato determine the distance.1 represents a graphicalmanifestation of the concept. ( 1 7 .5) is the roral value. I.of /("r) dx overthe rangex : a to b. In both these instances. Chapters l7 and l8 will introduce vou to someof thesemethods.l. 5 ) which standsfor the integralof the function f (x) with respectto the independent variable . Thesevelocities alongwith their associated times could be assembled as a table of discretevalues. (17.. definiteinteglation is represented by t: a^ f {.to (1j.r. the symbol / is actually a stylizedcapital S that is intendedto signify the closeconnection betweenintegration and summation.the "meaning"of Eq." Mathemafically.I INTRODUCTION AND BACKGROUND 17.3)for f '"[. as parts.)0. .I Z.8""n( ( I 7. This is an archaicterrn that originally meantthe construction of a square having the sameareaas somecurvilinear figure.For functions lying above the x axis. numerical integration methods areavailirble to obtainsolutions.into a whole. 1i)n t o E q . 2 ) z(t): .
Just as Eq.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.as depictedinFig. l'! f t.r.6) n where yiare individual measurements.{ FIGURE I7.I representotion Grophicol of theintegro o1 l ( x ) b e t w e e n thelimits r: equivo lhecurve.r is a continuousfunction of an independent variable .suppose that .2)1. 17. Other common applications relateto the analogybetweenintegrationand summation.5r t17.1.For this case. . (17. you might also be interested in computingthe mean or average of the continuous function : (x) for the interval fron b.1) This formula has hundredsof engineeringand scientific applications. Recall that the meanof discrete of n discrete datapointscan be calculated by tEq. Figure 17.394 NUMERICAL INTEGRATION FORMULAS I . 14s2n.2depicts a few caseswhere integrationis usedtbr this purpose. as specified by .thereare an infinite numberof valuesbetween a and b." (11.l / Mean: i. The determination points of the meanof discrete is depicted in Fig. 17. reIate directly to the idea of the integralas the areaunder a curve.Many specificexamplesof suchapplicationscould be given in all fields of engineering A number of examples and science. For example. (i3.a common applicationis to determine the meanof a continuous function. it is used to calculatethe center of gravity of irregular objectsin mechanicaland civil engineeringand to determinethe rootmeansquare currentin electricalengineering. In contrast. a to Integration is used for this purpose.For example.3a.3b.6) can be appliedto detenninethe mean of the discretereadings. enl lo theoreounder il s t t l o D T h ei n t e g r o FIG Exo nee kno nonl 17.utax DA ..
e e r m i g nonuniform wind blowing ogo nsfthesideof c skyscroper.2 TIGURE (o)A surveyor of how integrotion is used to evoluole oreosin engineering Exomples cnd scientific opplicotions. might to knowtheoreoof o fied bounded need ond hryo roods. 4 (a) r : "f(r) (b) FIGURE I7.c ee i dJetoo en oo o fo r i v e ri c )A s t r u c t u r eo nl g i ' . miqht needto bv o meonderinq streom {b)A hvdroloqist k n o w i h e c r o s s s e co l iro nh ete d i o d e t e r m i n e t h e f in .I Z.I INTRODUCTION ANDBACKGROUND 395 i _l l (b) (c) I7.3 An illustrotion for(o)discreie of themeon ond(b)continuous dotc .
However. anNea.396 INTEGRATION NUMERICAL FORMUTAS Integralsare also employedby engineers and scientists to evaluate the totalamount0r quantity ofa given physicalvariable. as is typicallythe casein morerealistic Inadexamples. 17. Thesearejust a few of the applications of integrationthat you might taceregukuly in the pursuit of your profession.In this case. dition. t) isa case. Mass: III. and corresponding elementalvolumes A Vi: : Mass I.z ) d r d v d z Mirss: lllrtvtdV JJJ v which is referredto as a volume integrctL. which is referredto as an areal integral.on whererr is the numberof discrete volumes. They are basedon the strategyof replacingn complicated function or tabuiated datawitha polynomial that is easyto integrate: 7b 7b I:lf(r\dr=1f. whereA : areo.r\tl. for integralsusing numericaltechniques as described next..x . y. or Mass : concrentration x volume where concentration has ur. When the functionsto be analyzedare simple.However. it is often difficult or impossible whenthe functionis complicated. For the continuous wherec(x. . Notice the strong analogybetweensummation and integration.it is necessary to sumthe productsof local concentrations c.you will normally chooseto evaluatethem analytically.. Forexample.ttt t .sr a volume. Similarexamples could be givenin otherfieldsof engineering and science. known function andx. the total rate of energy transferacrossa plane where the flux (in caloriesper square per second) centimeter is a lunctionof positionis givenby ?? Flux:llnu*ae " "r. For both thesecases.formulas are the most common numerical integrationschemes. y.rits of mass per volume. and : are independent positionin Cartesian variables designating integrationcan be usedfor the samepurpose: coordinates. the total massof chemicalcontained in a reactor is girenasth product of the concentration of chemicaland the reactorvolume. the underlyingfunction is often unknownand definedonly by measurement atdisyou must have the ability to obtain approximate values crete points..For example. suppose that concentration varies from location to location within the reactor.The integralmay be evaluated over a line.2 NEWTONCOTES FORMULAS The NewtonCotes.(.
For example..r) : a polynomial of'the form a. 17.. rtr"I la.4 (o)c stroight ineond(b)o porobolo bythecreounder The opproximction of on integrol "f(. In Fig.2 NEWTONCOTES where ll.f (tt) FIGURE I7.inFig.9) wheren is the order of the polynomial. 17. using a series of polynomialsappliedpiecewise The integralcan also be approximated of constantlength. (.4b.three to the function or data over segments FIGURE I7.4a.tx" . a firstorderpolynomial (a straightline) is usedas an approximation.r):a0+alx +"'+ 397 (17.5. 17.a parabolais employedfor the samepurpose.5 three byfhe oreo under stroightline segmenis The opproximotion of on integro l(r) .For example.r) .in Fig.FORMULAS I 2.fr(.
.7. Therefog the integralestimatecan be represented as / .6 inlegrotion The difference behween ondib)open formulos {o)closed polynomialscan are usedto approximate straightlinesegmerlts the integral. Geometrically. Recall trapezoidunderthe straightline connectin from geor etry that the formula for computingthe areaof a trapezoidis the heighttimestheaverage of the bases. formulas al'eavailable. The trapezoidalrul.This chapteremphasizes formulas is briefly introducedin Section17.8)is firstorder: t: l""lt^ 1 I . In onrcase.The openfornrs have integrationlimits that extendbeyond therangc the closedforms.the trapezoidalrule is equivalentto approximatingthe area of fu g I O1 andJ @) inFig. However. Theclosedlorns Closedand openforms of tlre NewtonCotes are those where the data points at the beginning and end of the limits of integration ac known (Fig.[(b) .. 17.7. l7 .f (b) .width x .the conceptis the sarrie but the trapezoidis on its side.verageheight EXAM .Higherorder be utilized for the samepurpose.6b). ")]d.f (a) (.3 THETRAPEZOIDAT formulas. 17.eis the first of the NewtonCotes It con+ closedintegration sponds to the casewherethe polynomialin Eq.r. open NewtonCotes RUIE 17. (17.10 which is calledthe trapezoidalrule.6a).398 FORMULAS NUMERICAL INTEGRATION FIGURE I7.r r ) f' 2 (17. D0 s Theresult o f t h e i n t e g r a t i oin l:(b kl) + . material m of the data (Fig.
Use Eq. t! I ul.oo. fbr the trapezoidalrule.640533.17. An esti_ mafe forthe local truncationenorof a "n singleapplicationof the trapezoidal rule is E..14)indicates that if the tunction being integrated is linear.  7.or l. they difl'er only with respect to the formulationof the average height.rr) x average height (r7.e.7 Grophicol depicrion of the tropezoidol rule I : (b . that rnay be substantial (Fig.r3) where.3 THETRAPEZOIDAL RULE 399 FIGURE I7. for functions with secondand higherorder derivatives(i. Equation(:i.1 Error of the TropezoidolRule when we employ the integral under a straightrine segmentto approximatethe integral undera curve.the trapezoidal rule wiil be exactbecause the second derivative of a straightline is zero.we obviouslycan incur . l 7.11) wheref lies somewhere in the intervalfrom a to b. . Note that the exactvalue of the integralcan be determined analytically to be 1.f (o) + J @)112. (Ij. 'lZ.lD ro numerically integrate l ( r) : 0.3.g).someerror can occur.2 _t 25x _ 200x2* 615x3_ 900ra f 40015 from a : 0 to D : 0'8.f"tE)tl. All the Newtoncotes closed lormulas can be expressed in the generalfbrmat of Eq' ( 17'13)' That is. (17. the average height is the averageof the function valuesat the end points. with curvaturc).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. otherwise.:.
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 nificant portion of the integrallying abovethe line. The reasonfor this largeerror is evident g r a p h i c ad l e p i c t i o n i n F i g . serord an approxirnate error estimateis required..l T .To obtain this estimate.t:0to0. 8 .(60)(0.8)3 ./. 8 rule to opproximote Grcphicol depiction of theuseof o singleopplicotion of thetropezoidol iheinteqr oo ll(. f ( 0 .10.7)l '' '^'f /.8.5c/c. : 1.461133. (17.1128: 1. 1+ 0 . 2 3 1 :0.050.: . which represents an effor of E. T h e f u n c t i o nv a l u e sl ( 0 ) : 0 . 8 ) : 0 . 2 E q . t I ) t o y i e l d 1:{0.56  .000x3 f" (x) : 400 + 4.the function's twicc derivativeover the interval can be computedby differentiatingthe originalfunction to give t 8.0.8 F I G U R EI 7 .2.050x . 0 .17:8 .640533 whichconespondsh fronb a percentrelatil.we would have no foreknowledge of the true value.( l 7 .00013) /1'* t+oo * 4.80012 The average valueof the secorrd derivative can be computed as lEq. (17..14)to yield I E. 2 3 2 c a nb e s u b s t i t u t e d i n b .eerror of st :89.2+25x2oa^r*675rr9OOr+*4OOr'trom.8n.t. a n d .400 FORMULAS NUMERICAL INTEGRATION lnteqralestimate 0. Therefoq hr actual situations.8o which can be substituted into Eq.\ _ r/:r * 8. .800:rr 0. S o l u t i o n .r)A.10.
Conthere are .. we denotethat the error is is not necessarily approximation an accurate by using the notation E.because of the fact that tbr an interval of this size. n : bct il .'' f F I G U R EI 7 .. respectively. 9 trcoezoidcrl rue Comoosite .rr. . we will use to characterize Figure 17.2 The CompositeTrqpezoidolRule interval of the trapezoidal rule is to divide the integration One way to improve the accuracy (Fig...3. Tlre resulting equationsare called compo.rate 17..f(x)ru l..l segments of equalwidth: sequently.)..9 shows the generalfbrmat and nomenclature integrals. la ba t7 (l7. Thus..9). x2...' .A discrepancy' which is of the sameorderof magrritude secondderivative however. sentedas r : L'' +1.'+ . and sign as the trueerror. r.ls) the total integral can be repreIf a and b are designated as . terval.RULE 17...rru.. f (t)dx+.site.the average of l"(6).. ratherthan exactby using E.3 THETRAPEZOIDAT 401 doesexist. Therearen * I equallyspaced composite basepoints(ro.fitrmulas.rs and 1. 17. 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 muhipleapplication. approxin.
Employ F.ro) and .:: tln' .)  I J I form of Eq. heightrepresents a weightedaverage to I .Recallthat theexact from a : 0 to D : 0.3 900x4 * 400x5 value the error.q. Use the twosegment trapezoidal t * f (x) : 0.). oe f a p p r o x i m a t e n a t u r eE t h a t E q .(17.f (x. r r ( .17) in the general or.J (x..T nl il I I(ri) l. An error fbr the compositetrapezoidalrule can be obtainedby summingtheindividto give ual errorsfor eachsegment E.n' Note is doubled.2 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 the integral Problem Stotement.(17. . . 1 .21) to estimate of the intesralis 1. r / i) t:(ba) \..20).widlh j1 2rt Avcragc hcight Because the summationof the coefficientsof /(:r) in the numeratordividedby 2il is equal According to of the function values..n" .:.640533. " [ ( r r ) . ( 17.2 * 25x 200x2 675r.rl L nt .11 .re. f   {r7 re) be i.l tD L .I ot' .' lD  . usingEq.1 tll' il ) /.  1. ( 17.402 FORMULAS NUMERICAL INTEGRATION rule for eachintegralyields Substitutingthe trapezoidal .".. I(rn\* /trr) 22  t t  .. the interior points are given twice the weight of the two end points/(. 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 e EXAMPLE I2. grouplng terms: I : : l / ' ( x o+ ) 2) .(17.. the average Eq. .ond derivativeat a point {.l'" (€. ( 17./ r\ r\ ' . This result can where /" (6i) t.I '/ t .1 .( 1 1. ..8.l5) to express nl )r f (rn) r 2 .) = n f " and Eq. . Thus.l3): F.l8). .19) can be rewritten as ThereforeL.i ' . if the number of segments th oq f .the truncationerror will be quarlered..q. (. locatedin segment simplified by estimatingthe mean or averagevalue of the secondderivativefor theentire interval as t1 \ 1' J f ' l r \l \ .r n .: _. 1/ ( x : ] or.
r 3 3 3 0l143 0t 0. first discusshow MATLAB can be usedto implementthe trapezoidal rule. 17.I2.l 0.T h ee x o c t v o l u ei s 1 .4): /(0) : 0.640533 0. where 60 is the averagesecondderivativedetermined rl o d The resultsof the previousexample. e. ( 1 7 .4 1.  7.3.r : O t o 0 . ("/"1 2 3 4 5 6 7 B I t0 0.2 1:0.gr [ . However.3 MATTAB Mfile: trap A simple algorithm to implement the compositetrapezoidalrule can be written as in is passed into the Mfile along with the limits of Fig. TABLE 7.along with threethrough tensegment applications of the trapezoidal rule.9 t6 .0688 €t :34.2 0 t6 0 .9O0r+ 200t2 from + 675x3 + 4OO. 8 ): 0 .we will tegralas the segments However. are surnmarized in Table l7.232 4 :1. 4 s+ 60 ) .8 f ( 0 . This is because the error is inverselyrelatedto the square of r [Eq. I hopezoidol ruleto Resulis for thecomposite estimote the integrol of /(.1 . 4 5 6 . 4 ): 2 . 2 3 2 0 . In subsequent sectionswe develop doubling the number of segments fbrmulasthat iue more accurate higherorder and that convergemorequickly on the true intheseformulas.5 95 6l 4. A loop is then employedto generate the integral f o l l o w i n gE q .97c .21)). Notice how the error decreases is gradas the numberof segments increases.belbreinvestigating areincreased.4 a 2667 0.0889 008 06BB 3695 484B 5399 5703 5B87 6008 609 I 6t50 349 t6.(60) :9.alsonoticethat the rateof decrease ual.1 . 8 . Forn : 2(h :0.. f ( 0 . (1. 0 6 8:8 0.51173 E.r'l :0.3 32 2. 6 4 0 5 3 3 .:1.64 t2(2\t previouslyin Example 17.rs . quartersthe error.2 + 25.l.1.\. 2 + 2 ( 2 ." n : 1 . The function to be integrated integrationand the numberof segments.3 RULE THETRAPEZOIDAL 403 Solution.Therefore. 1 8 ) .
.vdrarg i .end r f narqin4 risempLylnt. .n100. 2 5 ) * t a n h ( s q r t ( 9 .b.a.404 NUMERICAL INTEGRATION FORMULAS function I = trap(func.pi. 3. . and ca :0.ar" )1>5 .error('upper bound must be greater than lower.25i68. 5 ) o. The function to be integratedcan be developedas an Mfile or with an anonymous function.81 mlsz.. the following parameter assume values:g : 9. 1 ) * t ) First./o Mf le o imp enrcnl oo 'J e. s = s + 2*func(x. B 1* A .1kg. ( 17.25) *tanh(sqrt (9.varargin{:}).). fori=1. F I G U RI E 7. 0. 3 1 * 6 8 .3).a)/n..{:' ) .vararg:in{:}). let's evaluatethe integralwith a crudefivesegment approximation: formaL long >.81*68. >> v=@(t) sqrt (9.L/0. For this exarnple. end s .n1 X = X + h. 9 4 8 0 5 .4) a s4 1 . An applicationof the Mfile can be developedto determinethe distancefallenby the freefalling bungeejurnper in the first 3 s by evaluatingthe integralof Eq.. 2 5 / 6 8 .25 kg/m..varargin) % trap: composi. Note that the exact value of the integralcan be computedwith Eq.number of seg. I = (b .(17.additional parameters used by func ? Z output: I=integralestimate % if nargin<3. ): % composite trapezoidal rule % input: func = name of function ? Lo be integrated Z a.p2. b = inteqration limits ? n . Sfunc (q.ments (defaulL = 100) pl .1)*t) C ( t ) s q r i ( 9 . .a.n.a) * s/(2*n).end x = a.end if '(b>a).error('at least 3 input arguments required').b.IO 'he conpo:iie rope.te tt:apezoidal rule quadraLure 'Z I . .81*A.' tt ap (v.p2. 't ). 1 . m : 68.6oo2o :.s + func(b.trap(f unc.n. h = (f .
17.8)is secondorder: (xxr)(. r a p ( r r . ^ ^ .948049999r1528 which is very closeto the true value. 17. F o r e x a m p l e .b )G r c p h i c o d e p i c f i oo nf S i m p s o n 's : t c o n s i so 3 /6 rulel is the f toking ^rA^. lf there are two points equally spacedbetween. ( 17. To obtaina more accurate result..f (a) and f (b).4. l7. (x1y ) + (xr  x e ) ( x 1 : l(xr) (. O nC O ' n e c l 1(/.O . this resulthasa relativelyhigh true enor of 18. t h e t h r e e f( points can be connected with a parabola(Fig. The formulasthat resultfrom takingthe integrals underthese polynonrialsare called Sintpson's rules.x 2. the tbur points can be connected with a thirdorderpolynomial (Fig.l1b). 3 .1 Simpson'sl/3 Rule Simpson'sI /3 rule coresponds to the casewherethe polynomialin Eq.IZ.I I ( o )G r o p h l c o dl e p i c l i oo nf S i m p s o n1 s/ 3 r u i e l:t c o n s i so f toking ls t h eo r e ou n d eo r pcrobolo conneciin tg hree p o i n t s(.xo)1. ^ r i ' g 'I O J r p O t n ' 5 . the trapezoidal anotherway to obtain a more accurateestimateof an integral is to use higherorderpolynomials to connectthe p o i n t s . 1 .xe)(. 1 0 t 1 0 0 ) 4!.000segments: > > t .:rr) (x2 .rla.we can use a very fine approximationbasedon 10.4 SIMPSON'S RULES Aside from applyinq rule with finer segmentation.4 SIMPSON'S RUTES 405 As would be expected.r .67o.: I:.rz) .xo) ("t . I FIGURE I7.r2 .11a).'l + n/l^r / (rtt) .r.x y ) ( x s .Y t ) 4 rr'.tih f ereisanextrapointmidwaybetwe ea n) a n d / ( b ) . 17.rz) (x ..) f(x) .
^t1]_ also be expressed usingthe formatof Eq.24) to esrimare rhe enor.640533.456)+0. Thus.23).640533 : 0. the middlepoi int is weighted by two.2+4(2. Recall that theexacr !9 inregratis 1.' :  that.rr : the point midway between a andb.l 0 b : 0'8' Employ Es.] that )indicates it is nore accuratethan expected'Rather than being proportionalto the third denvative. t b/ .23) ro inregrate f ( . r7.. consequently.8 0./z (!srems fromthe rrw r fact 4 v r that rr14r/ /r t is l s atvided u t v l ( j e o by Dy 3 l n Eq.b:. : 1.which is rs glven si by 90 h 5 r/ ' f r t '.T'e + 4f (r) * . (17. In other words. 2 * 2 5 " u . The result of the intesration is lt  / : r 1 [/(xo) 4.comparisonwith Eq. (17. Use Eq.l (:to) f) xo. (17."otice 'fld.f(x) + J(. T h i s e q u a t i o n i s k n o wa ns S i n t p s o n . ''  ( b .y)l (n :?::.2730667 . o7.36746j E. x ' ) : 0 .232 = 1.it yields exact resultsfor cubic porynomialseven thoughit is derivedfrom a parabora! EXAMPLE 17.Simpson. (17.according to Eq. J in l2t.:!11 thirds and rhe rwo end points by one_sixih.8) : 0. f ( 0 .r2.367467 €t:16. where f lies somewhere in the interval from a to b. 4 ): 2 .n : 2 l h : J'(0) : 0. ::. respectively.sl/3 rule is thirdorder accurate even though it is basedon only threepoints.f (xzl .becauseh:(ba)12: E.406 NUMERTCAL tNIEGRATON FORMULAS wherea andb arc designated &s:16 and 12.3 S i n g l eA p p l i c o r i o no f S i m p s o n . s I/3rule.2 0.22). 4 5 6 /(0. s l/3 Rule ProblemSiotement.67o .1. tion enor of I E .232 1 :0.ut"s 2990 "\ f t 1 . Si Simpson's l/3 rule can a ) / 2 . \ lt ' or. the etror is proportional to the fourth derivative.forthiscase.l3): I(ba wherea : where. Simpson's 1/3 ruleismore accuratethan the trapezoidal rule' However. S o l u t i o n . It can be shown that a single_segment applicati ication of Si n npson'sl/3 rulehasa trunca.2 0 0 1 2* 6 i 5 x 3 _ 9 0 0 x a *400rs frorn.4).
2s) FIGURE 17.The relotive rveights ore depictedcbove the funclion vclues Not^thoi the methodcon be employedonly if the numberof segmenls is even. ( 17.) because the averagefourth derivativeis generally not an exactestimate of /'+rt{ ).'. I7 .'f{*)ar+ l.1. the result matchesexactly. Simpson'srule can be improvedby dividing the integraJust as with the trapezoidal tion interval into a number of segments of equal width (Fig.2 The Composite Simpson/s l/3 Rule rule.. 1.4. Hower. The approximate errorcan be estimated oRs E" .12). 17.As was the casein Example 17.I ).The total integralcan be represented as r: [^.'er."' .: .lt2400) 1880 : 0. 4 S I M P S O N 'R SULES 407 which is approximatelyfive times more accurate than for a singleapplicationof the trapezoiclal as rule (Example17..^n'< 1/3 rule.f (x) ttx+. ' + o. . because this casedealswith a fiftholderpolynomial. .12 fnrrnn<lfo lim^.rU..the error is approximate(E.I 2 .1710667 where 2400 is the averagefourth derivativelbr the interval.
Solution.623467 I2 €t : I.26) from Simpson's l/3 rule. Employ Eq. z) * 4J'Q.232 l (0.) J 6 +.232 : 1.408 NUMERICAL INTEGRATION FORMUTAS SubstitutingSimpson'sI /3 rule fbr eachintegralyields I :2lt + I (xo) 66 (r.456) + 0.The even points are commonto adjacent applications and henceare countedtwice.8) From Eq.10533.q..15): or. n I n2 l(. r) + l (r.4 (l) rh  l80na '' ." ..3.5 fQi)+2 L 3n j:2. I /3 Rule Composite Simpson's the integral of ProblemSiotement.n :4(h: 0. (17..an even numberof segments mustbe utilized might to implement the method. 0 1 7 0 6 1 .' 1 5 ! 'I ' o ' \1j.8. In addition.'+2h (17.2+ 4( 1. As seempeculialat first glance. the coefficients"4" and "2" in F.f(o') ( ) .21) is the averase tourthderivative tbr the interval. as illustratedin Fig. (11. However.26\ with n : 4 to estimate f (.they follow natul'ally in Fig. 17. groupingtermsand usingE q . UseEq.288 + 3.z .1. 1 ) :2. (11.2) ( 0 . 6 4 0 5 3l 1.6.6 fG)+f(r.. 4 6 4 .12.464) + 2(2.23). where f EXAMPLE I2.456 I : 0. 17..x) : 0.ro)+4I J:(btt) i..12. : .4. (11.62346: 10 . the odd pointsrepresent illustrated the rniddleterm tbreach applicationand hencecarry the weight of four from Eq.) Notice that.900x4* 400x5 from a : 0 to b : 0.2): : o.(17. Recall that theexact integralis 1.288 f \0.26): / nrl : 1.".f 0.2 * 25r .3.04Vo Et : 1 .200x2 675. 6 : )3 .27) to estinlatethe error. fashionas An error estimatefor the compositeSimpson'srule is obtainedin the same andaveraging tor the trapezoidalrule by summing the individual errors for the segnlents the derivativeto vield t .
' b . / 'r' ' r { 64rJ0 (1. Simpson's rule has 3/8 an emorof E. . / ( .ThisequationisknownasSimpsons3I8rulebecause/rismultiplied by 3l8. One option would be to usea compositeversion of the trapezoidalrule as was done in Example 11.nina.24).17.rr1)*]l(.3 Simpson's 378 Rule In a similar mannerto the derivation of the trapezoidal and Simpson'sl/3 rule. as discussedin Section 17. Further.27'. Thts may not be advisable. .'f 6t/ 't'te t or.as mentionedpreviously.a) 13: . : . (11.4 we usedSimpson'srule to integratethe function for four segments. x )+ .the 3/8 rule has utility when the nunrberof segments is odd.3).RULES IZ.f( x r . the 3/8 rule is somewhat more accurate thanthe 1/3 rule.r : ) (11.An alternative would be to applv Simpson'sl/3 rule to the first two seqments Simoson's 3/8 nrle to the last .29) Because the denor.4.fr)l o If(. .It is the third NewtonCotes formula. ) * 3 / ( r r ) * 3 . _ t2100) : (). : . in Example 17.The 3/8 rule can alsobe closedintegration expressed in thefbrmof Eq. whereas the end pointsare weightedwith oneeighth. However.3.However. the two interior pointsare given weightsof threeeighths. i L l ' .Consequently.4 SIMPSON'S The estimated error 1Eq.2. (17. because h : (l) . For instance.4. : ]/. of equispaced 17.4.29)is largerthanfor Eq.28) Thus.f(. a fhirdorder Lagrangepolynomial can be fit to four points and integrated to yield f : 3h t3f(x) +. As in Example 17.a)/3. it is limited to situations where thereare an evennumberof segments and an odd numberof points. because of the large truncationerror associated with this method. Simpson'sl/3 rule is usuailytlre rnethod of preference because it attains thirdorder accuracywith three points ratherthan tlre tour points requiredtbr the 3/8 version.. is (o'8)5 E'.U17067 l t t 0 ( 4) + which is exact(aswas alsothe casefor Example17.it is Iimitedto cases wherethe valuesareequispaced. f ( . however.13): i:\UUrT .rr) where/r:(b.(17. the compositeversionof Simpson's1/3 rule is considered superior to the trapezoidalrule fbr most applications.tor of Eq. . an oddsegmentevenpoint formula known as Simpson's 3/8 rule can be usedin conjunctionwith the I /3 rule to permit evaluationo1'both evenand odd numbers segments. Suppose that you desiredan estimatefor five segments.
28): + 3.4 0.6 3/8 rule 0.2+ 3(t .^l J :1. EXAMPLE 12.8.2 1/3 rule 0.8):0.(a) UseSimpson's 3/8 ruleto integrare .20012 froma :0 to b :0.: t ) .13 jntondem lllusirotion of howSimpsons 1/3cnd 3/Brules conbeopplied 1o hondle muriple (Fig.13). Soluiron.487117) + 0.232 Segmentr (n) TABTEI . (b) Useit in conjunction with Simpson's l/3 ruleto integratethe same function tbr five segnlents.266j):t..232 0.In thisway.5 S i m p s o n3 's 78Rule Problem Stqtement.f (0.4811'71 /(0.(17. FIGURE 17.900.432724 l.2 :3.432j24 . ll ./(r) : 0.r+ l400x5 * 675x'r .4ro NUMERICAL INTEGRATION FORMULAS 0 # 0. (c) A single application of Simpson's 3/8 rule requires four equally spaced points: /(0):0.51970 f (0.5333) Using Eq.2r 25x.8 o p p l i c o l i o nw s ithodd numbers of jntervols.wecouldobtain three anestimate with thirdorder accuracy across theentire interval.
{l Z. ' 9 O 1 t r 1:' r ' t . f ( r o ) + 4 .5 HIGHERORDER FORMUTAS NEWTON. f ( .r.g. the 3/8 rule can be usedto obtain 1:0. 1 4 3 3 9+ 3 3 ( 3 .232 Theintegral for thefirst two segnrents is obtained 1/3 rule: usingSimpson's I :0.296919 .. along with their truncationeror estimates.)/{.the five.2969t9) + t.HIGHERORDER NEWTON.2 .7r.:0.l 1.321 : f Q./ (0.2 NewtonCotes closedintegrotion formulos. Notice that.r1r3tt.rr I 9 l (rx) + Z5.A+1 3.r S m p s o n3 s/ 8 r u e .r r )+ f ( .COTES As noted previously.48 1 . as was the casewith Simpson'sI l3 and 3/8 rules. Thestepsizeis of thedoto points givenbyh:(ba)ln.264154 g The total integralis computedby summingthe two results: : 1.r'.r : ) \p . .3803237 + 1.COTES FORMULAS (b) Thedataneeded for a fivesegment I ication(/z: 0.and sixpoint formulas have the sameorder error. Theformulos in theformotof Eq. . 7 4 3 3 9 3 l ( 0 . \t) .t. . Poinls Nome Trcpezoidol rule S i m p s o r '1 : 3 'r^ . 1 8 6 0 1+ 5 3.1 1r t ) + 7 J \ t 1 ) 6 0 ) + 3 2 1 ( x t )+ l 2 l ( x l + 3 2 . 8 5 Bccle s rr.645017 1 : 0./(ro)+l(rr) n. r1 ( '6 r1 .f (l. ( 182 90 (. . .143393 :0.I 3) ore presented the overoge so thoi theweighting to estimote heightis opporent. 13l80)15 f11)(6) Q 4 5 ) l i r .2+ 4(.181929 .the trapezoidalrule and both of Simpson'srules are menbers of a family of integratingequationsknown as the NewtonCotes closedintegrationformulas.r8r929) +0.32 0. .. Some of the formulas are summarizedin Table 11." .264754 17.232 :1.{r) + 75l (xa) + I 9 l (r5) 127 ) + 501(.3803237 0 4tl For the last threesegments./{.le \ 1 . .1 . 288 .\. This generalcharacteristic holds for the higherpoint fornrulas fonnulas(e.16) is app :  . and Boole's rule) are usually the methodsof IABLE 17.Stl. 4 8 ): 3 .f (0) : 0.l/3 rule and leadsto the resultthat the evensegmentoddpoint preference.2.l + 501(.r:) 5 / I 2.. 1 8 6 0 1 5 f(0.r'. ) Formulo Truncolion Error ll /I2lhtl"tI) .16) : 1 ..1rt': 16.u ) \b0) 7.a9dnt ..
Furthermore. . offer viableandattracdescribed tive altematives.1(x.309129 0.when the function is known and high accuracyis required. EXAMPLE I2. 18.experimentally deriveddata is often of this type.2 + 25x .. 17. all lbrrrrulasfor numericalintegrationlrave been basedon equispaced data points. TABTE 17.456040 044 o5a 064 o70 080 f {x1 2 842985 3 547297 3 rBr929 2 363000 0 232AAA ) ields S o l u t i o n .r) : 0. Recallthat the correctanswer is 1.) * " . r h . : the width of segment i.nust deal with unequalsized seqments.1.640533.12 1.. Accuracycan be improvedby usingthe composite version.8Vc.200x2 I 67513 + 400"t'.9O0ra Doto for "f(.l . one method is to apply the trapezoidalrule to eachsegment and sum the results: I ht . The informationin Table 17. in Chap.ril+f@r) lhz * f (u) + f (r.2+ r.e.ic9729 t 345241 ) 7.309129 + 1. Note that this was the sameapproach usedfor thecompositetrapezoidal rule. f (x) 000 0t2 o22 o32 036 040 0 200000 1 .13393 2 074943 2.3 .( 17.161 +. greaterthan fourpoint) lbrrnulasare not commonly used.305241 + 0.. A p p l y i n gE q . ( 17. with uneguolly spoced volues of :r.16) is that the ft's in the formel are constant.30) the integral to determine for thisdata. 3 0 y I :0.The only difference and( 17.s91801 perceut which represents relative an absolute errorof €t :2.there are ntany situittionswherethis assurnption doesnot holdand we r. G.3was generated usingthesame polynomialemployed in Example17. that. t) . For thesecases.6 INTEGRATION WITH UNEQUAL SEGMENTS To this point. in engineering and science thehigherorder (i.it must alsobe stressed practice...+Ot0:_::'lj:_:::=t.Sirnpson's rules are sufficientfor most applications.412 NUMERICA IL NTEGRATIO F N ORMUTAS However. In practice.6 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.) whereft.methods as such Rombergintegrationor Gaussquadrature. For example.f(.10 + 0 212 2. ( 1 7 .Use Eq.30) between Eqs.
2+25*x200*x.x andy. 6 : >> x .n1 +y\k+\\) /2.44.u f \y.6.14.ero for the sameproblemthat was solvedin An applicationof the Mfile can be developed E x a m p l e1 7 ..r . (17.y) J.64.22 .Two vectors. subscripts to accountfor the fact that MATLAB doesnot allow z.1 MATLAB Mfile: trapuneq A simple algorithm to implement the trapezoidalrule for unequally spaceddata can be variand dependent written as in Fig. M{ileio lmplement function o.14 thetropezoidol rule lorunequolly spoced dofo.4)a L .end if any(diff (x)<0). >lJdLFu :d_l L r a p s T u '. > > l r a p L l n e q ( x .Bl.9 0 0 " x . . ^ 3 .error('at not monotonically ascendrnq' ).l0 . to the resultobtained FIGURE 17. ( .tr tL o dlt:alt_ftte YJ T = trapuneq (x. ^ 4 + 4 0 0 * x . end s = 0.rA loop is employedto tors are of the samelength and (b) the x's are in ascending from thoseof Eq.4. for k = 1.I2 . o ir_n Jr.end if nargin<2.4. error ( 'x and y must be same length' ) .he * for n daLa points ascending % same lengrth and x must be monotonically % input: x = . = trapuneq(x.r.12. if lengch (y) =n. 7I./ector of independent var:rables % y = vector of dependenE variai:les ? % output: 2 I=inteqralestimate least 2 input arguments resuired'). s : s + (x(k+1)x(k))(y(k) end T c ' ' The r: lunction i s J e s e r i h eiJ n S c c t i o r1 r 9 .6 INTEGRATION WITHUNEOUAL SEGMENTS 413 17.30) generate the integral.Notice that we havemodified the subscripts in anays. y) : Z rule to determine the integral ? Applies the trapezoidal (x.32 >> v . . 17. v.1 ' 2 + 5 15 * x .6.54 . : (o t! which is identical in Example17. " 5 . Two error trapsareincludedto ensure ablesarepassed order. holding the independent that (a) the two vecinto the Mfile. y ) .error('x n = lengrth(x). y) where x and y must be of t.36 .i.
7 .14. 3 : >> x = t0 .6 19. Incorporate some randomerrorby rounding the velocitiesto the nearest integer.. 2 + 25 * x . Here is a simple MATLAB sessionthat uses this function to integratethe data from T a b l e1 7 .54 .develop a plotofth analyticalar.85 23r. hold the independent and dependent variables.275 kg/m.2): :trl: I u \ t ld t J(\ Suppose that we had measurements of velocityat a series of discrete unequally spaced tirno during free fall. ^ 4 + 4 0 0 * x . Then usecum1.9 0 0 * x .414 FORMULAS NUMERICAL INTEGRATION 17.a).2 0 0 " x . " ' 2 + 6 1 5 * x .I2 . >> 99.1 B). 17.) where the two vectors..22 .2 4L1 80. a niceapplication of integration is to compute the distance u(t) as in on its velocity z(r)of an objectbased (recall Eq.2 MATTAB Functions: trapz Qrd cumcrapz MATLAB has a builtin function that evaluates integralsfor datain the same fashion asthc Mfile we just presented in Fig. i7.^ 5 .vJ o 5 9.rd cornputed distances along with velocity on the samegraph. cct=O .32 . cumr rapz. ^ 3 . cumtrapz (x y.5948 In addition.44 .7 UsingNumericol Integrotion to Compute Distonce fromVelociiy ProblemStotement.3. / c d )* t a n h ( s q r t ( g * c d / m ) * t ) ) .x &odr. EXAMPLE l7.2) to syntheticallygeneratesuch inforrnation for a 70kg jumper with a drag coefficient of 0.64 . 8L. > > y = 0 . Use Eq.81.4 2 3 4.3 5 5. hold the independent anddependent variables.h .7 I44.y) 1. respectively. x and y.t .45 173. Solution. l') wherethe two vectors.6.'. 17. and z: a vectorwhoseelenrents z ( 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 ) .m=70. As described at the beginning of this chapter.4 . > > v = r o u n d ( s q r t ( g * m . The distances can then be comouted as / J^Ltopt( .respectively. Someunequally spaced timesandrounded velocities as canbe generated '. >> trapz (x. lornot shu'r (J . A sirnple representationof its syntax is z . rapz to determine thedistance fallen and comparethe resultsto the analyticalsolution(Eq.5=[Q 1 L. 2'15 . (17. that computes the cumulativc inte_ural. MAILAB has another function.In addition. It hasthe general syntax z t rapz (x.
t. 17.[ .. za=n/ cd*1og (cosh (sqrt (qr*cd/m) *t.7m.6 INTEGRATION WITH UNEQUAL SEGMENTS 4t5 the Thus. wnereos w e e d e l e ' r i r e d ^ l n e c o l . >> ploL (La. . fu^clio.I2. z. 11.15 Ploi of distonce timeThe line woscompufed versus withtheonolyticol rne solufion. .' a' ) >> >> >> >> ra=linspace >> xlabel title('Distance t1me') versus ('t (s) ') .ly. This result is reasonably closeto the analytical solution(Eq. on in Distance versustime O LCS a nalytical numerical kg ng nd he . .a) ) . t rve As in Fig. za. thejumper hasfallen 231. the numerical and analytical results matchfairly well. after 8 seconds. r.h e u r poi'r's ..ylabel ('x (m) ') 'numerical Legend ( 'analyticaL' . .15. t (length(r) ) ) .4): )ly. om A graph of the numericaland analyticalsolutionsalong with both the exact and rounded velocitiescan be generated with the following commands: (t (1) . FIGURE 17.
1 4 .fbnnulas.For example.) at: L' (1.4summarizes the NewtonCotes openinteT4rotion. .Equation(11. ( 17.1hod Segmenls (n\ Formulq Truncotion Enor 2 3 ( b .u ) l n . / ( .'/(r' r)or) L' (1.'.successive pairs of the formulas have the sameorder enor. r r )* 1l .( t 1 .I 3 ) so that the weightingfactorsareevident.Table 17. The evensegmentoddpoint fbrmulas are usuallythe methodsof pref'erence because they require f'ewerpoints to attainthe sameaccuracyas the oddsegmentevenpoint fbrmulas. The openformulasarenot ofien usedfor definiteintegration.a general equatiorlto compute the averageof a twodimensional function can be written as lrecall E q . As with the closed versions. Theformulos l3) ore presenied in the formotof Eq. Thesiep size i s g i v e nb y h : ( b . 17.6b that openintegration formulashavelimits that extend beyond the rangeof the data. 1 \D a)  /3V13 f"G) t.16).a ) .(f x r . 1 ' ) t 1 x ) (dc)(ba) (r7. f ( . 1( f r ) * . f ( x t )+ l ( .theyhave utility for analyzingimproperintegrals..f d) lD 2A ) I 1.1'1.. l ( .* i l. 1 ) l l ..32) Thus. t)r + . The techniques in this chapter(and Chap.iar161 3 I l . f( .416 NUMERICAL INTEGRATION FORMULAS TABTE 7.4 NewtonCotes open integrotion formu/os. .32) states that the orderofin . 11. Recallfrom calculus that suchintegrals can be computed as iterated integrals: a.l(tr) l : 1 1 ( f r+ (bu) l+t. EXA 17. The result of this firstintegration is integrated in the seconddimension.7 OPENMETHODS Recalltiorn Fig. Poinls I 2 Nome M dpolf t rn. 1( r : ) I I I ( r r ) (l4rJ5ll5. so thottheweighting to estimote of the doto points theoverogeheightis opporent.r 1 ) (1)(6) (95/l44lr5.314)h3 f"G) 2 J ( r t ) . However. the integral in one of the dimensionsis evaluated first. .v) (17. l8) can be leadily employedto discussed evaluate multiple integrals..The fbrmulasare expressed in the tbmr of Eq. r t ) .l14A)h1 f\6)Gl 2A (b a\ 17.20 and 21. ' '( I i l t r .r. In addition.8 MULTIPLE INTEGRATS Multiple integralsare widely used in engineering and science.3 r) The numeratoris called a double integntl.r 2 .{17.(. A simpleexarnple would be to take the doubleintegral of a functionover a rectangular area(Fig.they will haverelevance to our discussion of methodsfor solving ordinary differentialequations in Chaps.rr1 ) 2 b l ( x t ).
First.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 A v e r o g eT e m p e r o t u r e Problem Stotement. The temperatures at the necessary.66667.y dimension).I 2.The function can also be evaluated analyticallyto yield a resultof 58.r dimension)and 6 m wide (. Supposethat the temperature of a rectangularheatecl plate is described by the following function: : 2 x ) * 2 r .16 Double infegrol ostheoreo under thefunction surfoce A numericaldouble integralwould be basedon the sameidea.This results in an integral of 28 l6 andan average of 58. methodssuchas the cornpositetrapezoidalor Simpson'srule would be applied in the first climension with eachvaltteof the second dimensionheld constant. r.let us merelyusetwosegment applications of thetrapezoiclal rulein each dirnension.r climensionfor eachr'value. Thesevaluesare then integratedalong the I' dimensionto give the final result of 2688.The approach is illustratedin the following example. Solution. First.17.33.x 2 . Sincethehighest .) If the plate is 8 m long (.r and y valuesare depicted in Fig.66667.8 MULTIPLE INTEGRATS 417 FIGURE 17.the trapezoidalrule is first implemented along the . which is exact. Note that a simple average of thesevaluesis 47. EXAMPLE 12. Then the methodwould be appliedto integratethe seconddimension. 17. Dividing this by the areayields the averagetemperature a s2 6 8 8 / ( 6 x 8 ) : 5 6 . Now we can applya singlesegment Simpson'sl/3 rule in thesamefashion. To make the same evaluationnumerically.computethe average temperature. Why doesthisoccur? Reperfect call thatSimpson's  /3 ruleyielded results for cubicpolynomials.2 t ' 2+ 1 2 I(t.
0+2{40)+48 (uu +256 4 54 70 . a defaulttolerance of I x 106is used. l8 introduces techniques that are more efficient thanthe NewtonCotes formulasfr evaluatingintegralsof given functions. 17. .A.y) 2*x*y+2xx x.6) 2816 .8. x m a : . A simple representation of the syntaxtbr dbtquad is q = d b l q u a d ( f u n. Chap. 54 ( 8 .1 MATTAB Functions: dblquad ond triplequad MATLAB has functions to implernentboth double (dnlquad) and triple (rriptequad) integration.''2+72.17 Nurnericol evoluotion of c double integrol using thetwosegmenl rule tropezoidol For higherordelalgebraicfunctionsas well as transcendental functions.0 ) 5 4 + 2  \ 7 0 ]+ 4+ 496 72 64 24 X 8_u7?:3gej4+ 448 (60) 256+?(4961+448 I FIGURE 17.8. "22*y.y m i n .7: >> q . t o l ) where q is the double integralof the function fun over the rangesfrom xmrn to xmaxad to ymax.zn Here is an exampleof how this function can be usedto computethe double integral evaluated in Example17.vm. it wouldbe necessary to use compositeapplicationsto attain accul'ate integralestimates.4t8 NUMERICAL INTEGRATION FORMULAS Iftiit U 40 48 ^) . . y m a x . In addition.If rol is not specified.0.dblquad(@(x. Theseoften providea superiormeans to implement the numericalintegrations for multiple integrals. x n t i n .
you are providedwith only discretemeasurements constraints.realistic problem settingsareusually morecomplex. the force might be available only in tabular form. 17.6. 17. assume that.numerical integration is the only viable option for the evaluation. as in Fig. the work equationis reexpressed as w: F(x) dx 1.34) might be solvedanalytically. respectively.as well as results.18.m).18. when analyzing measureddata.and multipleapplication zoidal rule and Simpson's1/3 and 3/8 rules to computework for this data. For this sifuation..suppose that the force variesduring the courseofthe calculation. xo and x. The resultsof the analysisare summarized in Table 17.52 that was estimatedon the basisof valuestakenfrom Fig.Use single.The general formula is Work:force x distance When you were introducedto this conceptin high schoolphysics. 17.For example. numerical methodsprovide the only alternativefor determining the integral. 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.Eq. ever.33) where W: work (J). : the initial and final positions (m).33) can be evaluatedanalytically.the force might not be expressedin such a manner. Suppose that you have to perform the computation for the situation depicted in Fig. as in w:1"" F(. Further complexity is introduced if the angle between the force and the direction of movement also varies as a function of position (Fig. in a realistic problem setting. For such cases. More refined estimates using more segments.and F(x) : a force that varies as a function of position (N). The results are interesting becausethe most accurateoutcome occurs for the simple twosegment trapezoidal rule. (17.W 07.A percentrelative error r/ was computed in referenceto a true value of the integral of 129. Although such a simple computationis useful for introducing the concept.In fact. 17.For example.Although the figure showsthe continuous valuesfor F(x) and d(x). the work would be calculated as50J(ljoule:lN.The work equationcan be modified further to account for this effect.In suchcases.5).r) cos[9(x)] dx (r7. Simpson'srules.18).This is particularly .34) HowAgain.18at 1m intervals. The calculation of work is an important component of many areasof engineering and science. if a force of 10 N was used to pull a block a distanceof 5 m.CASESTUDY 419 Bockground. Solution.it is more likely that the functionalrelationshipis complicated. ff f(r) is easyto integrate. if F(r) and e @) arc simplefunctions. because of experimental at versionsof the trapex : 5m intervals(Table 17.' ( 17.sirnpleapplications were presentedusing forces that remained constant throughout the displacement.. However.
8082 1.0 5.420 FORMULAS NUMERICAL INTEGRATION continued 0 .48 r. of theforce TABLE t 7.0 r30 I .5 of Doto for forceF("r)ond ongle 0(x) os o function position r.5297 9 .40 0. 5r 2 0 8.0000 1.50 0.0 9. F(r).18 For theongle. lhis cose The cose of o vorioble vories.75 090 F(x) cos d 0 5 t0 t5 20 25 30 0. 0 8 8 1 0.0 r3.{. m FIGURE 17.0 l4.o 105 12. rod 050 1. N 0. mognitude.7025 2.3532 . oswellosihe force octing ono block.
6. 17. Segmenls I 2 3 6 2 6 3 Work e[ o/o Technique Trnnpzni. if data were ^.5 m. The omission of thesetwo points effectively limits the accuracyof the numerical integration estimatesin Table 17.5 ond 12.5 m.05 35 75 L57 804 5 5 FGURE 17.9 2.6 Estimoies of work colculoted usingthe tropezoidol ruleond Simpson's rules.5 and 12. The fact that the twosegment trapezoidal rule yields the most accurateresult is due to the chance positioning of the points for this particular problem (Fig. 17.I2.20 is that an adequatenumber of measurements must be made to accurately compute integrals.6 Notice how the use of sevenpointsto estimotes chorccterize voryingfunction thisconfinuously misses two peoksot x :2.20). where we have plotted the continuous curve for the product of F(.19 A continuous plotof F(x)cos[d(r)]versus posilion withtheseven discrete points used to develop ihe numericol integroiion in Toble 17. 1 3 r39.82 I l 7. Thepercent relotive error€. evident in Fig.98 r19.93 95.19.'lo Simpson! 1/3 rule Simpson's 3/B rule 5 3t r9 133 124.lnl r. 17.52 Po)thot wos estimoted on the bosisof volues ot lm intervols. Notice how the useof sevenpointsto characterize the continuouslyvarying function missesthe two peaks at r :2.'^ir4rrr6 af E"/t {\ onc tA(1 5\l ? .84 35 r 8.r) and cos [0(x)].09 175. The conclusion to be drawn from Fig. os computed in reference to o truevolueof the integrol1129. For the present case.9 CASE STUDY 421 continued TABIE 17.
(17.t.6458 Evaluate lytical rn of the tri attain thr cent rela 17.6 Ev I Jt J el Including the two additional points yields an improved integral estimateof I (e.5 Th . the inclusion of the additionaldata incorporates the peaks that missed previously and.2 Evalnte the lbllowing integral: r4 JO I tt .9007. (e) composite Simpson\ 1/3 rule with n : 4. lead to better results. 6 I tt (a) analy (c) coml applicat and (f) E through 17. >> trapz (x. 1.8087 . as a consequence.4 Ev 1a a.422 NUMERICAL INTEGRATION FORMULAS continued (a) anal (c) coml applicat  /3 rule the num cent rela 17.4)by integrating 17.3940 8.35371.0881 0. using the MAILAB trapz tion.1 DeriveEq. of trrotro[ezoids h"oppens to leod even bolonce between posifiveond negotive errors. can be r spacedd rO f(x) 2 determine an improved integral estimate.. we could compute >> x=t0 >> y=[0 2.l6Eo).3 Evaluatethe following integral: rn /2 . Thus.5291 9.e2'1tlx applicationof Simpson'sl/3 rule.5 5 r0 L2. y ) r32 . ft f (x) FfGURE 17. Bychoice. 17.3).5t"20 11. ( 17.20 Grophicol depiction of whvthetwoseqment lropezoidol rule o ooodestimote ofthe vields forthis poriictrlor integrol theuse toon cose.L.:2. and (f) Simpson's 3/8 rule.For example. determine theprcent relativeerror basedon (a).. 17.Foreachof the numerical estimates(b) through (f).5 15 20 25 301. (a) anal with n = 1/3 rule PROBtEMS Eq.1025 2. 3.
For (b) and (c). (P17. (c)conrposite tlapezoidal rule with n :2 iutd 4. and (f) Simpson's3/8 rule. (b) Fit the data with a cubic equationusine polynomial equation1odeterminethe regression.f(r): 2e r 5' tl2 u56 325 55 45 7 6 B5 7 B B 6 B5 7 93 t0 5 can be useclto generatethe following table of unequally spaced data: 425 O OO5 015 035 f ( x ) 2 1 8 5 5 . determinethe perthe numerical (a) analytically.9).)(1..ssunred Evaluate the inlceral from rr :0 to b :0. OU 40 zu 200 190 175 160 (b) FIGURE P17. d e r i e w s L o * r n gf o ' t e .J. ^ r r e o s .Ornittingatmospheric aurin relativeerror'.(b) using thc composite trapezoidalrule r I) with ir : 2.6 Evaluate the double intesral and essentially cancelsout).rpson's (f) Boole'srule.51 5 9 7 0 1 3 7 4 6 l l 8 3 l r A. (d) single n) n) rl l/3 rule. and Simpsou'srules whereverpossibleto bottom.and (c) a combinltion D : elevation(in m) ofthe watersurfaccabovethe reservoir lytical means. P17.9 : acceleration due to eravity t9. (c)composite trapezoidalrule with n : 2 antl .9 Walcr exertspressure on thc upstream can be characterized by shown in Fig. : I psw(z)(Di. For (b)..) .PROBLEMS 423 (a) (b) singlc applicationof the trapezoidal rule.pressure in pascals(or N/ml) exertedat an 0s308 08131 elevation i mctersabovethe rescrvoirbottorn. For (b) and (c).6 using (a) ana_ 103kg/rn3. analytically.81m/s2):antl (b) the trapezoidal rule.sproblem is a. P17. determinethc percentrelativeerror based on (a). through 17.and (b) using single applicationsof on 1a. (e) Simpson's 3/8 rulc. 17. J^ (a) (b) singlc applicationof thc trapezoidal rule.The prcssure pQ): pg(D.8 Detcrmine the distancetravelcd tionr the follouinc + 2xs)rir velocitydata: I .9 foce of o Woter pressure exerting on lheupslrecm dom { o J: .3 r ' : )d t d t ' d : 1.Becauseboth pressurcand area vary with elevation. JO l/3 rule.(d) single l/3 rule. which fbr thi.p : densityol to be a constant water.9b). cerrt relltirc erlor hased Simpson'sl/3 rule.7 Evaluatethe triplc integral analytically.9rz. cent (because prcssure it works against both sidesof the dam facc 17. computethe percentrelative 17. Integrate the cLrbic distance. computethe per. (e) composite Simpson's application of Sirnpson's . n g .1 Evaluate the lirllowins inteeral: efror.9.r3 1/3rulewith n : 4. application of Sirr.1..Accordingto Eq.as depictedin Fig. and (c) using singleapplications of Simpson's f.pressure ofthetrapezoidal linearly increases thehighestaccuracy. the total force . Pl7. can be dctertimes the areaof the dam tace mined by multiplying pressure (as shown in Fig.z) 0o where p(./.with dcpth. For eacho1' estimates(b) through (f). 17.5 The firnction . For cach of the numericalestimate s (b) and (f).thc total force is obtainedby evaluating (a)analytically.{5 (a) Use the tlapezoidalrule.1. face of a dam as I 7. compute the percentrelativeeror.
and (b) the rateofcan going throughthe interscctionpcr minute.r : I l..(.1r I w h e r eU : a numerica data: )rfi I.60 34t 33l A. and.9b).fr:\ 'l: (a) Use the compositetrapezoidalrule to compute F rnd d for the casewhcre 11 : 30 (n : 6). f r.r is length in distancealong the beam..r) : dcnsity.. The line of action can also be obtainedby evaluating IoDpgzu:(z)(D z. I pG)A. cm2 loo lo3 l06 I lo )2a I33 lsc 17.. and t2 .. p.rl. and M is the bending moment.The total force F exertedon the mast can be dctermined by integratingthis function over the height of the mast: x.14 A transportation engineeringstudy requiresthatyou determinethe numbcr of cars that passthroughan intersection traveling during morning rush hour.I k. You standat the sideol the road and count the numberof carsthatpass every 4 minutesat severaltimes as tabulatedbelow.).)tJz.Thus. force follows the equation a si n V(r):5+0.We know that V : dM ldx..l. but use the composite Simpson'sl/3 rule. fng/Jn" J.+ I Vttt' .1. 17.10 The tbrce on a sailboatmastcan bc reprcscntcd by the following function: .424 INTEGRATION FORMULAS NUMERICAL If M.the initial and final times.\ Jr.r): crosssectional area.r c.:s'a \/T'l where: : the elevationabovethe deck and H : thc height of the mast.15 Determine theaverage valuefor thedatain Fig..r:200(. F(/). Integrationyields the relationship rI ft) l.iszero and. N/m Height l. r. and the shear massentersor leavesa reactorover a specifiedtimeperiod.) Time (hr) 73A Rote (cors per 4 min) I B 7 45 24 B:OO B:15 l4 24 O:J.. m Force. tributed 17.For (b) and (c) uselm lncrements. where u(l): width of the dam face (m) at elevation. and(c) multiplcapplication Simpson's rules. the integral reprcscntsthe summationof the product of flow times concenqivo I et tlr U.) dz The linc of action can also be deterrnined by integratron: r rH J0 :. The following datahasbeenmeasured for a l0m length rod. /(\. respectively. Pl7..'. Use thebest numericalmethodto determine(a) the total numberof can that passbetwecn7:30 and 9:15. andcl.t Jt where I.r: Jt) p(..(b) multipleapplication trapezoidalrulc.su(:)(D . F(/). ffi M: M:M.l Compute the net fbrce and thc line of action due to this diswind.r). Determinethe massin grams to the best possibleaccurac). mls  . (b) Repeat(a). calculate M using(a) analyticalintcgration.It:ta: u Use numerica data listed bel t. (Hint: Be careful withunits./0 0 0 I50 3r00 30 340 60 l2aa 90 t2a t600 27aa where R : t and. l7.15. A.m a 2 4 l0 3 6 B p.r : dist averagetlov IB0 2)A 240 3200 3500 3800 17. This formula makesintuitive sense if you rccall thc analogy betweenintegrationand summation.glcm34ao 395 389 3Bo 3.17 The c Putedas B : 4 5 9 :i 5 21 9 A. . whererz : filoSS.: (Fig. 17.PI 7. m Force.) t1: ft Use Simpson'srule to compute J./.ll A wind fbrce distributed againstthe side of a skyscraperis measured as Height /.16 Integrationprovides a meansto computehow much 17.: I .13 The total mass of a variable density rod is given bi71.12 An I lm beam is subjected to a load. Perfbrm thc intcgral necdedtbr thc avcragcin theorder shown by the following equation: o: 1 L/". N/m 17. r : distance alongthe rod and L : the total lengthoftherod.(r)dx / .I ll J.2512 where V is the shearforce. m3/mi. mln Q.
1m:) varieswith depth l(m) can be computcd by integration: 0 4 t0 t0 2a 30 4B 52 50 3_s .9.t'(.4 lB0 (m/s). Use the curnLrcipzfunction g. J0 rll O 4 z. In a similarfashion.da. 9 6 3 5A 3 5 2 7 O O O O O c.and B fbr the fbllorving a nurnerical data: n)/1 J.r.19 As wasdonein Section17.PROBTEMS 425 u 8 4 4 3 I 7 z 1 10 12 0 F I G U R EP I 7 . cletermine the work perfbrmed ifa constant force of I N appliedat an anglegresults in the iollowing displacernents. m/s 05 003 r3 006 56 1.rad 0 0 I 30 2. thc l1ou..r.4 52 4l wherc B : the total channelwidth (rn).25 oa2 . wherc Z : the total depth (ni).m H.Use these where U : watervelocity' relationships and methodto determineA.h 0. l a 6 m 2 9 8 1 7 5 5 t 0 5 t 1 .6 43 43 4A 37 32 50 5. m3/min c. Jo 17.0 34 (: .17 The crosssectional area of a channelcan bc computed as A.lrrlr. and r' : distance liom the bank (m ).)z/r. I 5 numerical Use inte_uration to evaluate this equation fbr the data listed bclow: l.55 52 35 4A 45 4. min Q. ftr U. mg/m3 l7.25 l7 005 412 l 0Ir a.7 60 38 90 37 l2O 3 150 1.' average as 0 (mr/s) can be contputccl 0 : rl] I U{.= I H(r')r1r.g/m3 )a'2 85 7. Determinethe averageconcenlrationbasedon thc followins data: 17. 1odetcrmine the cunrulativc work andplot the resultversus r.lll The average concentfation ofa substance (g/rn3)in a lake u. H : rhedepth(m).n B 12 )6 A .+.hercthe arcaA.
therearemore efficient methodsthat are available. o providesa meansto create Understanding how Richardson extrapolation a more integral accurate estimate by combiningtwo lessaccurate estimates. q. limited by the numberof points that are given.r. The flrst technique is based on Richardson extr(tpolatiorz. which is a method fa combining two nunrericalintegral estimatesto obtain a thild. Bolt capitalize on the ability to generatefunetion values tcl develop efflcient schemes fr numericalintegration.This chapteris devotedto three suchtechniques.For tabulated are information. The form of the datahas an important influenceon the approaches that can be usedto evaluate you the integral. providessuperiorintegralestimates Understanding how Gaussquadrature by picking optimal abscissas at which to evaluatethe function.:d Knowing how to useMATLAB's builtinfunctions and r1uadl to integrate functions. yut fuuction is if the available. more accllrate value. NumericolIntegrotion of Functions CHAPTER OBJECTIVES The primarv objectiveof this chapteris to introduceyou to numericalmethods for integratinggiven firnctions.I INTRODUCTION In Chap.thecomposite Sirnpson's l/3 rulemight seem to be a reasonable toolfa such problems.Specificobjectivesand topicscoverecl are ' . can generate as many valuesof. I 8.if* .This techniquecan be usedto generate anintegnl esrimate wifhin a nresnecified eror . 17. we noted that functionsto be integrated numericallywill typically beoftno lbrms: a table of valuesor a tunction..Although it is certainly adequate for many problems. In contrast. At tacevalue.l (r) as are requiredto attainacceptable accuracy. Tlc computationalalgorithm lbr implementingRichardson extrapolation in a highlyefficied rnanneris called Rombergintegration.
r. I (h) : the approximationfrom an nsegment applicationof the trapezoidal rule with stepsizeh : (b . 1 ) Now recall that the error of the compositetrapezoidalrule can be represented approxlm a t e l yb y E q . (18.Recall that.These error estimatesare then used to determinewhether more refined estimatesare required for a subinterval. In so doing.2) lf rtis assumedthat f" is constantregarcl\ess of stcp sizc. However.thesemethods use two estimates of an integralto computea third.2 ROMBERG INTEGRATION Rombergintegrationis one technique that is designed to attainefficientnumericalintegrals of functions.Gaussquadrature formulasemploy.2) canbc uscdto <lctcrmine that the ratio of the two errors will be E(ht) E(hz) _ h h !i (1 8 . Generally calledRichardsonextrapolallon. values of at specifiedvaluesof .ROMBERG INTEGRATION The secondmethod is called Gcuss qundrnfure.we were constrained to takethe weightedaverageof /(x) at the endsof the interval. 3 ) This calculationhas the importanteffect of removing the term 1" from the computation.r valuesthat arepositionedbetweenthe integrationlimits in sucha mannerthat a much more accurate integralestimateresults. 18. 17 in the sensethat it is basedon successive applicationof the trapezoidal rule. For exam/(x) for the NewtonCotesformulas were determined ple.This techniques appliescomposite Simpson's I /3 rule to subintervals of the integrationrangein a way that allows error estimates to be computed. rnore accurate approximation.throughmathematical manipulations. (18. Two builtin MATLAB functionsthat useadaptivequadratureare illustrated. more refined segmentation is only usedwhereit is necessary.a ) l h ) : bu E__htf" 12 . andE (h) : the truncation error. (18. 18. superiorresultsare attainedfor lesseffort.Eq. we have made it possibleto utilize the information embodiedby Eq. 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.It is quite similar to the techniques discussed in Chap. The third approachis called adaptivequadrature.a) I n. If we make two separate estimates using stepsizesof h1 andh2 andhave exact values for the error: I (hr)t E(ht) : I (h) f E(hz) (1 8 .1 Richordson Extropolotion Techniques are availableto improve the resultsof numericalintegrationon the basisof the integral estimatesthemselves. ( 1 7 .2) . in Chap.2. 17.In this way. if we usedthe trapezoidal rule to determine an integral. 2 1 ) [ w i t hn : ( b .
5% 349% 5.  I(h2tlE(htl \tt2 / which can be solvedfor p "/ 1r/ ' ) : E\ ( Itlttl.\2 I(h1tLEthrtl . Thrrs. a'(") (185) EXA Extropolotion E X A M P1 LB E . Solution. two trapezoidal ruleestimates timateof O(h\.thisequation becomes ' : 4l lt(nt. I Richordson Problem Stotement.428 NUMERICA IL NTEGRATIO O N F FUNCTIONS without prior knowledge of the function's secondderivative.r)= : : 0.4) \tt1/h2)'  It can be shown (Ralston and Rabinowitz.3) to give E ( h t t= E ( h : t l . \: \n2/ I which can be substituted into Eq.To do this.UseRichardson to evaluate extrapolation theintegral of /(. For the special casewherethe intervalis halved(h2: hr12). / h.8. Single andcomposite rulecan beused toevaluapplications of thetrapezoidal atetheintegral: Segments h Integrol E I 2 4 0B a4 02 o t72B I 0688 t 4B4B 89. ll ttt:l ttttrtl fl8. we have developedan estimateof the truncationelror in tenns of the integral estimatesand their step sizes. (18.the estimatesfor one ancltwo sesments . 1978) that the error of this estimate is we havecombined of O(h2) to yielda newesO(h4). For example. we reanange Eq.I\hzl 1(htlh)2 Thus.5% Richardson can be usedto combinetheseresultsto obtainimproved estimates extrapolation can be combined of the intesral.( 18.2t 25x 200x2 t 675xr 900r+*400x5 tioma 0 to b 0.This estimate can then be substituted into I : I(hz) _t E]tz) to yield an improvedestimateof the integral: I : l(hz) + 1t.1): /h.
In Example18.4)providesa way to combinetwo applications (h2) (ha This is a subset of estimate with error O approach with error O to computea third 1.lro. 7 ) .rzz8):1. 64  ll I 63"' 61 ' ( 18 . we computed improved integrals in turn.. methodfor combining a more of O (ha) on thebasis of threetrapetwo improvedintegrals in ExampleI 8.011061 (et : 1.640533 t5' t5 which is the exactvalueof the intesral. can be combinecl to compute an integralthat is O(/r8)using O(ft6) results [ : 1. (18.INTEGRATION I 8. 6 ) respectively.6) to yield and 1.Similarly.and (18. fbr is halving the step size...Thesevaluescan be substituted r : ! l .361461 improvedintegral Theerrorofthe which it was based.. rule of the trapezoidal Equation(18. estirnates upon whichis superior to the to give for two andfour segments canbecombined manner. general integrals to obtain irnproved estimates. The two integralestimates in Examplel8..l8.623461 I : (1.2 The Romberg Infegrotion Algorirhm in eachof the extrapolation Notice that the coefficients equations [Eqs. . theestimates In thesame 41 ) . be combined to yield an Thesetwo can.640533 1.2130(t1 (st : 76.ja/c). For the special case where the original trapezoidal estimates evenbettervaluewith Oth6l.l were 1. Utilize Eq.6). l. 18.640533 1.6)to combinetheseestimates to computean w integra l ithOtlr6t. we usedRichardson extrapolation to compute two integralestimates of Othl).5).(18. the equation used O(li6) accuracy of are basedon successive I : 1.!t.1. Thus.623461.oazsr. : which represents :0.36146i 3' an errorof E.361461 of O(h1) obtained into Eq. en 4 fl ) . as accuracv increases. ( I 8. Solution. and 11 arc the more and less accurateestimates. two where f.(18.5 16 lt I5 r (r 8 .623461 l. 1.2. For instance.0688): 1. is Et : 1.7)l add up to l.(1. zoidalrule estimates.2 :XAMPLE HigherOrderCorrections ProblemStotement.6c/r:).ru ro u r): r .2 ROMBERG 429 to yield 3' : 0. thev reoresent weichtinefactorsthat.4848 53 t :lt.
Eq. e ' c e o f i n e g r o J ecimole5 (o) First (c)Third iterotion.1 graphical depiction of the sequence of integral estimatesgenerated using this approach. U + l) and the less (l) accurate becomes tr. l:13?3331623467 0.The first column contains ($ep 1.* 1.1a) involves computing the oneandtwos e g m e n t t r a p e z o i d a l r u l e e s t i m a t e1 sa (1 n1 d 1 21 ) .. 18.r 111.. es e q . The indexj is usedto distinguish For example.367467 . which hasan errorof O(h41. E q u a t i o n ( 1 8 .640533 1.j=3isfor ofthe application[stepsize is (b .8) estimates. For example. iterotion.640533 . ib) Second iferotion.623467 . (18.I g e n e ' o l e dr s i n gR o r n [ 6 1i9 niegrotion G'opnico depiqfien o f t l . the first iteration (Fig. where 17.a) l4].  4r tlj*r.640533 * 1.r r : the more and less accurate andllt = the improved integral.172800 1. andso forth.1 4il .640533 ' 1. where : I is for a singlesegment application evaluations that are designated "r sizeisba).430 INTEGRATION OF FUNCTIONS NUMERICAL placerelatively greaterweight on the superiorintegralestimate.I integrals. The generalform represented by Eq.for ft : 2 and j : 1.and so forth.z: 1. rule thetrapezoidal Each matrix corresponds to a singleiteration. Figure18.andits systemisa atic applicationto evaluateintegralsis known as Rombergintegratiut. The othercolumns a foursegment applying Eq.whereft : I cotresponds rule estimates.j:2isforatwosegmentapplication[stepsizeis(bd)12].5). 8 ) i s t h e n u s e d t o c o m p u t e the elementIt.k:2 conesponds to the O(ft4) e$ito the original trapezoidal between themore mates.8)is attributedto Romberg.r. o(h2) o(h4) o(h6) o@8) t'r ?:1633331s67467 0j72800 ? 1.8) to obtain successively better matrix are generated by systematically estimates of the integral.The index ft signifiesthe level of the integration." 1:333333163s467 ..361461 . FIGURE I8.11 and I1. ( I 8.r.068800 1.(18. ( 18.r<. ft : 3 to the O (ho).t:!4 J which is equivalentto Eq. Theseformulations can be in a generalfbrm that is well suitedfor computerimplementation: expressed I.respectively.1.367467 F 1.
to generate I:t : .10). Thus. is more efficientthan the trapezoidal Rombere integlation rule and Sinrpson's rules.e.64053i 3 s)e x a c t . By usingloops.8) 2 0i]*t'+b75*x 1. To do this..criterionis required to assess the accuracy of the results.l. For example.e) wheret.a )h.36i461 .I level of integration with j :2). with only l5 functionevaluations! Figure18.167167 I I m.For Eq. In this case.9) can be appliedto determine that this result represents a changeof 1. and eightsegnrent trapezoidal rulesthat is. in turn. (18.2. l8.1a. The third iteration(Fig.':l )re The objcctof the second iteration(Fig.2 ROMBERG INTEGRATION th 8. (18. One methodthat can be employed for the present purposes is :orstiore i.a lernrinrrtion. the computation nated.640533.. : an estinrate of the percent relative error.l: >> >> f =(i lx) 0. Equation i18...8) 1. As in other rnethclds approxirnate in thi.8) is combinedwith 11I usingEq.is below a prespecified is termierror criterion0. Sirnpson's l/3 rulc' would requireabouta 2lSsegment in doubleprccision application to yield an estimate of the integralto sevensignificant disits:1. we must check to determinewhetherthis result is adequate fbr our needs.4848. an eightsegment trapezoidal estimate is addedto the first column. cornbined with /1. 18.lc) continues the process in tlresame fashion..as was donepreviously in other processes. I r. fbur.1to yielcl1r : : 1. 18. becausewe are evaluatinga filihorder polynomial..the resrrlt (lt t : 1. Note that the function usesanotherfunction (recallFig.10)is to obtairrthe O(h6l estirnateIr.8) t: Now.8)is appliedto computesuccessively more accurate integrals along the lower diagonal. (18.lxroo"/.5405 .2presents an Mfile lbr Romberg integration.r : 1. iterative we conrpare the new estimate with a previous value. (18.r. the estimate fr .is determined.2+25*x rombcrg(f.0.9). Rombergintegration proln contrast.1:. a foursegment Then it trapezoidal rule estimate.fbr determination of the integralas shownin Fig.068800  lx1001 :21.640533.and then Eq. The resultis.0..this evaluationindicates the following percentchangeover the colrrse of the first iteration.For Fig.two.8q t.r showinghow it canbe usedto determine theintegral of thepolynomial fiom Examole18. When the change between the old auclnew values as represented by r:. Afier only three iterations.. e15lpppin!. when comparedwith the previousresult 12.s book.I 8. 18.}c/r.. drrces the sameresult basecl on cornbiningone.. . trrep to implement tl'apezoidal the composite rule evalutrtions Here is a MAILAB sessior. lle ep br le er lp.r:lu.the previousvalue is the most accllrate fiom the previouslevel of integration(i.623461 . 17.this algorithm implementsthe methodin an eficient manner.
p2.maximum allowable it.varargin{: I(1.432 INTEGRATION NUMERICAL OF FUNCTIONS function le. A characteristic of theseformulas (with the exceptionof the specialcaseof unequallyspaced data)was that the integral estimate was basedon evenly spacedfunction values. end end q = I(1.a.r (j .egration lirnits (default = 0. = trap(func. 1) . i terl'romberg ( func. ea. we employedthe NewtonCotes equations.the trapezoidal rule is basedon takingthearea underthe straightline connecting the function valuesat the endsofthe integration interval.1) }) .1 )  / ( 4 ^ (k . 17.end r 1.trap(func.. For example. maxit=50. iter n = 2^iter.000001?) % es = desired relative error (default.1 ) . 18. b = int.p2. break.a. es0 .end rt nargin<4 isempty (es ) . for k = 2:iter+l j = 2+iterk. end T(2.n. end i I nargin5tismpfy(maxlr.2 MJile to implement Romberg integrotion.. k .. FIGURE I8.es.varargin) quadrature ? romberg: Romberg integration q = romberg(func.maxit. % % input: ? func .a.b. = iter+l.n.b. I (iter+1.1 ) * I (j + 1 . = 0.erations pl.b.varargin{ : } ) .iter+l). iter while iter<maxit. the location of thebase points usedin theseequations was predetermined or fixed.iter+l) ) /I(1.000001. k .maxjr. if ea<es.name of function Co be integrated % a. k ) = (4 ^ (k .) % : Romhero inteoration.b. = 30) ? maxit .1 ) .iter+l) ) "100.a.Consequently. I8..3a.as depictedin Fig.1 ) . = additional parameters used by func % % output: qintegralestimate % (%) Z ea = approximate relative error = number of iterations ? iter if narqin.pI.. l. I (j . The formula that is usedto computethis areais = (h + f (u) .3 GAUSSQUADRATURE In Chap.iter) ea = abs( (I(1..es.errot ('dL leasu : inpuc argumenrs requireo'). )..
3. By positioning thesepointswisely.1 Method of UndeterminedCoefficients In Chap. 17. ond on improved integrol estimote resulls whereaandb:thelimitsofintegrationandba:thewidthoftheintegrationinterval. Becausethe trapezoidalrule must pass through the end points.we would arriveat an improvedestimate of the integral.I8.we derivedthe trapezoidal rule by integrating a linearinterpolating polynomial and by geometricalreasoning. By positioning these points wisely.Hence. Gttussquadratureis the name for a classof techniques to implementsuch a strategy.asin Fig.3 GAUSS AUADRATURE 433 "f("r) (b) FIGURE I8. This methodwill then be employedto developthe GaussLegendre formulas.3b. we will show how numericalintegration formulas suchas the trapezoidal rule can be derivedusingthe methodof undetermined coefficients. The particular Gauss quadratureformulas describedin this section are called GaussLegendreformulas. 18. suppose that the constraint of tixed basepointswas removedand we were free to evaluatethe areaunder a straightline joining any two points on the curve. 18. there are casessuch as Fig.3 depiction of thelropezoidol rule os fheoreounder thestroight {o)Grophicol lineloinino fixed end points{b)An improved integrol eslimofe obtoined by toking theoreounder thestroight line possing through two intermediote points. 18.3awhere the fbrmula resultsin a large error. thepositive ond negclive errors ore beller bolonced. The methodof undetermined . we could definea straightline that woLlldbllancc thc positil'c tnd ncgativeerrors.Before describingthe approach. Now.
+ : I'.434 NUMERICA IL NTEGRATIO N O F FUNCTIONS FIGURE I8. Now realizethat the trapezoidal rule shouldyield exactresults when the function being integratedis a constantor a straightline.{ o )o (b) c stroight line Lonsio"rro'ro To illustrate the approach.. (18. :l r(hu\ 12 I J (bu)12 ldx and ba 2 o.'j. 18. ..4).Thus. 6 o lr u l ^ . Eq. Two simple equations that represent thesecasesare J : I and ) : r (Fig.". *._.0.10)is expressed as I = c g J @ )+ c t f ( b ) (r8.. the following equalities shouldhold: {i.4 Twointegrols thct should beevoluoted exocfly by l h a ' r 6 p " r o .il) where the c's : constants.lt.
in contrastto the trapezoidal rule that usedfixed end points a and b. we cirn obtain two of theseconditionsby assuming that Eq.ls. we now have a total of four unknowns that must be evaluated. when substituted back into Eq.we merely extendthis reasoning by assuming that it also fits the i n t e g r ao l f a p a r l b o l i c( ) : x 2 ) a n da c u b i c( . y: x r ) l . I: ba^ .3 GAUSS QUADRATURE or. we requirefour conditionsto determine them exactly. but are unknowns (Fig. 12)fits the integralof a constant iurda linearfunctionexactly. Then.5). ( I 8.3. andconsequently. the function arguments 16 and r1 &rerot fixed at the end points.to anive at the other two conditions.11). co*ct:ba and cu 435 ba 2 ba bo n 2 with two unklrownsthat can be solvedfor Theseare two equations L() Ll  z ^ gives which. yd o i n gt h i s .J(It+ zz bu^.  8.w e d e t e r m i n e ts FIGURE I8. Just as for the trapezoidalrule. (18.Thus. ^ J(bl which is equivalentto the trapezoidal rule. However.5 Grophicol depiction of theunknown voriobles. 18.rz) wherethe c's : the unknowncoefficients. evaluatingthe integra. .u n c t i o n .rs ond r1 for integrotion by Gouss quodrolure .I8.2 Derivqtion of the TwoPointGousslegendreFormulo Just as was the casefor the previousderivationof the trapezoidal rule.rt) rrg. the object of Gauss quadrature is to detenninethe coefficientsof an equationof the lbrm I = cof 6il + clf {.
r':]:0.re . corresponds . thesevaluescan be substituted into (18.\ : .15)gives Substituting these : . VJ .The fbur equations fl crtltt: J l I ldr:2 fl  Jl .First./3/ '\'/3/ (r8. the upperlimit.15) (r8.corresponds E q . (18.frr I /a : 0.5173503. r x+ i lc l x . 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 yr t h e l b u r u n Equations arn knowns. C o n s e q u e n tfl iy Eq. to give Similarly. as variabler in a linear r:al +azxd (18.solveEq. r : b.13) (18. t. thetrvopoint t:r(i)*/f+) " \. \ r r. r7) at result that the simple addition of the functionvalues Thus.14) ca :61 : I resultsinto Eq.(18. .f ( t .r8) 1. Substituting Sincex6 and 11 cannotbe equal./x:o ( 1 8 . V3 is fbrmula GaussLegendre Therefbre.1a)fo cd rsubstitutetheresultintoEq. we arrive at the_interesting that is thirdorderaccurate.(18.this rneans om Eq.rr. 1 8r)o y i e l d a : at + a2el) to x4 : 1.(18.19) tor.16)..13) i t t b l l o w st h a t y i e l d sc s : c r ../: If tlre lower limit.5773503..I to l.t d..r :0 (r8. f : rt 2 / 12rlr:= 3 lt pl Jt / . 1 3 )t h r o u g h( 1 8 .1/ J3 andl/V3 yields an integrirlestimate through( I8.I6) arefrom .7 is related to the a new variable that by assuming form.r. ( 1 8 ..( 18.436 O F FUNCTIONS NUMERICA IL NTEGRATIO N formula thatis all four unknownsand in the bargainderivea lineartwopointintegration to be solvedare exactfor cubics..r6) r i r .14) (r8. This lirnitsin Eqs. r t : cut.r1.1*i : c .r2 )_  EX into this result that.2^ .. This is accomplished in fashion. was done to simplify the mathematjcs intothis A simple changeof variable can be used to translateother limits of integration original x.l : r..13) Noticethattheintegration and to make the fbmrulation as generalaspossible.whichcan be solvedfor .(18.
respectively.1 2 to give This equationcan be differentiated bu dx . the limits r : 0 to 0. relativeeror of 11. we must perform a changeof variableso that rz : 0 andb : 0.22) the limits arefiom .15 . and ol'Simpson's application of the choice of clever accurate.4dxd to yield into the originalequation Both of these can be substituted r 0.4* 0.The fbllowing practice.23) fbr.we substitute a n d( 1 8 .4x.18)to yield which can be substituted (b + rr) 'l (b . To do this.17)is0.the righthand side is in the form that is suitablefor evaluationusing Gauss quailrature.l 2' rl8. because rules are also thirdorder Sirnpson's function only two basis of quadrafure on the this accuracy poinfs.4dra J_l I Therefore. 3 TwoPoint E X A M PIL the integralof ProblemStotement.1.8.900x4 + 400x5) '" .1) [0.4xi5l0. The exactvalueof the integral between Solution.17)to evaluate + 400. 2 0 tc a nb e s o l v e d E q u a t i o n( s1 8 .4 .(18. 2 3 ) in theeqtration (18.200(0.r. (18.4+ 0.1)a + 400(0. 3 0 5 S 3 T .17c.r andd._ dr.rr+ 675rr 900"ra is 1.200:12 + 6j5x3 . T h e r e f o r e .This resultis a percent which represents 1.4* 0.22)and ( 18. . 2 3t)o y i e l d x : 0. atxT : . Before integratingthe function. I \4 as 0. can be substituted Equarigns without integration interval the transform eff'ectively Thesesubstitutions to be integrated.3GAUSS s i m u l t a n e o u sflo yr a n d( I 8 .8 into Eqs.8 I Jt) : dx n2 " " . 1 9 ) ot : blcr 2 antl a:: ba 437 (18.4 10.516741* iu:1/J1 : 1.900(0.21) 2 into Eq.(18.305837 rule or a single applicationof the trapezoidal comparablein magnitudeto a foursegment expected is to be because Tlris latter result 113 3/8 rules. However. Formulo GoussLegendre 8E . t h e i n t e g r a laccordingtoEq.4x7 and dx :0.2 * 25r 200. attains Gauss base evaluarions. (18.4 * 0.QUADRATURE 18. Use Eq.2 * 0.llr (r 8 .2+ 25(0.a)r.4x.4r7)3 + 675(0.f(r) : 0.822578.640533.5167ttl and at function can be evaluated The transtormed a s l .I to +1.4t. is how this done in illustrates example changingthe value of the integral.+ ' 25r .
t\ r l\ Therefore.re ..rJx:0 ) (irruT(l. ( 1 8 . c r t: c t .rrtx:2 pt  JI pt .1 to 1. i r s t .16) for the fourunEquations(18. j' . rl c r r x i*]c 1 x f : J "1  . 1 6 )w knownsF . \ ' r:) xt: I .r x in a lineartashion. The fbur equations to be solvedare c a* c 1 : l'.the twopoint GaussLegendre formula is t:fl"\.rl : . t 5) gives Substituting these . ( 18.r7 to theoriginal that a new variable as ir..16) can be solved sir.18) 7.. to give (18. v ) /. t ./3 and I /V3 yields an integralestimate that is thirdorderaccurate. Tlris is accomplished by assuming .1t:= l J. l. corresponds to xt: E q . corresponds to x.the upperlimit. 1 r i : / *l.0.f ax:O (18. : v') I /::0. this r. we affive at the_interesting r : .. i .I 3) through( 18.14)yieldsc1. 11 8 .17) result that the simple addition of the functionvalues at Thus.19) .nultaneously . . 1 8t)o y i e l d 0:a1*a:(l) Similarly. x : d.16) arefrom .s o l v eE q . Notice that the integrationlimits in Eqs.This was done to simplify the mathematics and to make the formulation as generalaspossible. variable intothis A simple changeof can be usedto translateother limits of integration variable is related form.l results into Eq. Consequently from Eq. Substitutingthis result Since xe and x1 cannot be equal.. thesevaluescan be substituted into (18.r1.( 18.13) through (18.7: 1.. 1 4 ) be solvedfor t ^ o l '1 EXA into that. ^.neans : cr .5773503.l3) it follows that Eq.436 NUMERICA IL NTEGRATIO O N F FUNCTIONS is all fbur unknownsand in the bargainderive a linear twopoint integrationlbrmulathat exactfor cubics.'/3 ^/l + f l \'/3/ l (18.5773503. ( 1 8 . " x: a t I a z x a If the lower limit. (  8. hichcan f o r c r a n ds u b s t i t u tte h er e s u l t i n t oE q .t : b. ( 18.
)a + 400(0.19)and (18.4x. because Simpson'srules are also thirdorderaccurate. ( I 8. h e r e f b r et.4d J _l Therefore. ( 1 8 . 3 TwoPoint E X A M P1 L8 E the integral of ProblemSlotement.4i_0. I J1 as 0.Todothis.The following exampleillustrateshow this is done in practice.900.4* 0.2. 1 7 . The exactvalueof the integral between Solution. relativeerrorof lI. Gauss quadratureattains this accuracyon the basis of only two function evaluations.. function can be evaluated The transformed s 0.305837 applicationof the trapezoidalrule or a single compa'ablein magnitudeto a foursegment because of Simpson'sl/3 and 3/8 rules.4 * 0.I 8) to yield which can be substituted (b*a)l(btt)x.822518.f (.' '25.8. Before integratingthe function.22)and (18.r .7)l . base points.t :  2 (18.23)canbe sLrbstituted effectively transformthe integrationinterval without Thesesubstitutions to be integrated.(18.2 * 25. respectively. Equations \18.)2+675Q./: I l"E as 1.14 is 1.r x.1 41510.21) into Eq. .200(0.3GAUSS for (18. Formulo GoussLegendre ..x) :0.h e i n t e g r a a : 1.1x.4.QUADRATURE 18.r .1) in theequation forranddx. the limits x : 0 to 0.the righthand side is in the form that is suitablefor evaluationusing Gauss at x. 437 (i8.640533.i and th :O. This resultis which represents a percent 1. changingthe value of the integral.17)to evaluate + 400x5 .:_ and ba (tZ: .900(0.4r.4 t0.This latterresultis to be expected application of the clever choice of However.305837 .22) to give This equationcan be diffbrentiated dx : bn d.t.22) to yield and ( I 8.4+ 0.2+25rc. Jo : .4*0.4.wesubstitutea:0andb:0.2 * 400x5)r/r + 6i5r3 .516741+ l ccording t o E q .r nl lLv'!'"\ I n.1: .r.20)can be solvedsimultaneously Equations at : b'ta .200x2* 67513 900.8intoEqs.u I .20012 rc. Use Eq.l7o.4x. (18.4elxd into the otiginal equationto yield Both of thesecan be substituted n 0.i ) T .5I 674 I and at quaclrature.231 r : 0. we must perform a changeof variableso that thelimitsaretiomlto+l.11 ' " ' .{.'*.t 2' (18.
1 6: 0 .438 NUMERICAL INTEGRATION OF FUNCTIONS TABTE 8.8132444 + 0.t3.24) wherer?: the numberof points.higherpointversions can be developedin the generalform I = c s f ( x 0 )+ c l l ( x r ) + . t o u s s .7'74s961) which is equalto : 1.555s556 f (0. the threepointformula is 1 : 0. 4 6 7 9 1 3 9 3 4 5 7 2 6 9 1 c z: 0 . Solution.r./to'({) c6:{lB"tr6ltso c j: ( 1 8 + ' t r d t s a c::lu+V..114s967) + 0. According to Table 18.4859876 which is exact.0 X2: \/ J/) =. 3 6 4 7 6 1 5 2 3 0 4I8 3l r .1to estimate theintegral for the samefunctionas in Example18.88888891'(0) + 0. Use the threepoint formula from Table 18.3 HigherPoint Formulos Beyond the twopoint formula described in the previoussection.640533 / : 0. .4 ThreePoin Problem Siotement.3.v 5 2 5.Valuesfor c's andx's for up to and includingthe sixpoint fonnula are summarized in Table 18.Ul /JO x o: * . + curf(xnt) (18.66 .rz l6083 I9Z x::O2386l9l86083l97 = 0 661209386466265 ra xs:0. : Jili +7aJi6t35  ro:t/245+14\/7Al2l .trotza .:: 0. 0 Truncolion Error = ro: tl : I lJ3 l/VJ 1t2){{) G) 7t1) co : 5/9 (t : B/9 cz * 5/Q : nl3/5 r11 . 4 6 7 9 3 1 9 3 4 5 7 2 6I9 c t : 0 .l 3 ! r y d / 9 O A rx:0171324492379174 t't:O360761 573048139 c z: 0 . 1{tzr 0.0  /{to)16.na) :1322 q1 PO] p)A j c 1: 1 3 2 2 + ) 3 y r y 1 c2: 128/225 q : 1 3 2 2 + ) 3 ! r y op A O c a: 1 3 2 2 .r:{tB.3. ro : a 9324695142031 52 16. / 5 2 5+ 7 0 \ / 3 0 1 3 5 r r : .y : li4514Jfi121 .7 O \ / 3 0 1 3 5 x2: y'\15 7sJfr135 = 1t8){{) c.rr : I 249386466265 : .9324695142A3152 . ..:n/1[i14ffip1 u: '/ltii1tJfrlzt I 8.5555556f(0.0 2 3 8 6 1 98 . Weighting Foclors co:2 co: I l Function Argumenls .rt : 0. .1 .28t3013 + 0.I .t Points I 2 foctors ond function orguments Weighting usedin GoussLegendre formulos.:: 0 1 7 1 3 2 4 4 9 2 3 7 9 1 7 A '.L e g e n d rF G eo r m u l o EXAMPL I8 E .
a.ad(fun. / are parameters that yorr pass emptymatrices fbr rol or and . . This function usesadaptiveSimpsonquadrature. (default: l0 6). nonzerovalue causes additionalcomputational want to passto f rrn.botlr basedon algorithrns (2000).4 ADAPTIVE QUADRATURE the integral Althoughthe composite Simpson'sI /3 rule can certainlybe usedto estimate points.and pt. levels. no fi.race. Adaptive quadrature ing the stepsize so that small stepsare takenin regionsof sharpvariationsand largersteps gradually. .^ shouldbe usedin the definitionof fun.it is not suited particularly This is true the function is known.tcl achievea desiredaccuracy. lt may be more efficient fbr low accuracies functions. That is. its efficiency can be a decidedadvantage. p2. taf .It shouldbe noted that array operators. * . wlrere forengineering thatdealwith tabulated data.Hence.4. until the enor falls to acceptable MMLAB includes two builtin functions to implerrrentadaptivequadrature:cluad describes how they can be applied. doesnot take into account that somefunctionshaveregionsof relativelyabruptchanges the where more refined spacingmight be required. performed. values.it is not appropriate problems However. quad. Thus. . for cases where within the integrationinterval.I 8.If the truncationerror is acceptable. t. pl. The following function syntax for the quacl function is the same for the qrLadl function: g = qu. are taken where the function changes l/3 rule to the corrrposite Simpson's are basedon applyinu Most of thesetechniques rule was usedin Rombergintegrain a mannersimilal to how the trapezoidal subintervals tion. b. for irnplementingadaptivequadrature: . tt ace to use the default . fine spacingmust be appliedeverywhereeven though it is only neededfor the regionsof methodsremedythis situationby automaticallyadjustsharpchange.) bounds.1 MATTAB Functions: quad ond quadl by Canderand Gautschi developed MATLAB has two functions.The following section 18.the stepsizeis refined acceptable. p2.rol : the a and b: the integration where fun is the functionto be integrated.4 ADAPTIVE QUADRATURE 439 points requirestunctionevaluations at nonuniformlyspaced Because Gaussquadrature the function is unknown. This function useswhat is calledLobotto quadrature. rra. or nonsmooth quadl.It may be more efficient for high accuracies and smoothfunctions. This constraint equallyspaced it hasthedisadvantage thatit uses of givenfunctions.e is a variablethat when set to a desiredabsolute error tolerance detail to be displayed. and quad1.rrther refinementis required and the integral estimatefbr the subintervalis deemed repeated and the process If the error estimate is too large. the 1/3 rule is appliedat two levelsof reflnementand the differencebetween thesetwo levelsis usedto estimate the truncationerror. ln addition. must be when numerousintegralevaluations I8.
0. The angularfrequencyis relatedto the period r(s) by a =zrlT.858325 .q)2+ 0.^Z + O. > > q u a d ( @ m y h u m p sO 11. Notethat routines analyticallybetweenthe given limits to yield anexact the t. Next.r.85832612842164 Thirs.9. fewer function evaluations were made and.ur: the peak current (A).ands:6. the computation executes f'aster.01) + l. 0. Hence. we can sojve the sarneproblem. . 39549861 Solution. and . we can integrate it with an error tolerance of l0a as in . 6) 29. 1 ) 29. but using a looser tolerance and passingq. the currentin anAC circuit is often in the form of a sine wave: i : ipeat sin(arr) where i : the current (A : c/s). First. hence. ar : the angularfrequency (radians/s)and / : time (s). The humps tunctionexhibits both flat and steepregions over a relatively short. Usequadto integrate function: thefollowing (.thisisthebuiltin humps function that MATLAB usesto demonstrate some of its numericalcapabilities.r as parameters.5 Adoptive Quodroiure EXAMPLE Problem Stotement.04) si Then. 0.solution i./  ((x r). let's evaluatethe integralin the simplestway possible.85812133214492 Notice that because we used a larger tolerance.it is uselulfor demonstrating and testins like quad and quadl. . Because it resultsin efficient energytransmission. we can develop an Mfile for the function: function y Y . Bockground.s) cl). lo. First.^2+0.scorrect to .r range.1.r:0.9. r.440 NUMERICAL INTEGRATION OF FUNCTIONS .01 (x . .sevensiclnificant digits.3.q.r)2 + 0.04 s belweenthelimitsr:0tol.rmps functioncan be integrated integralof 29.NotethatforQ:0.usingthebuiltin version of humps along with the defaulttolerance: >> format lonEt >> quad (@humps. the result is now only accurate to five significant digits. However. the .r.l8.1./((x = m"yhumps(x. le4.3. although it would not be apparent liorn a single application.
For some of theseforms.r.!:= (.cos(2n) + cos(Q)) :0 T Ju Despite the fact that the averageis zero. i. an alternative to the averagecuffent must be derived. To do this. the AC circuit generates the equivalent power as a DC circuit with a consrantcurrent of 1*r.. (r8.k /. However. We will use both the NewtonCotes formulas from Chap.21)into (18.26) whereP: the power (W: J/s)..70707.slqw states that the voltage is directly proportional to the currenr: V:iR whereR : the resistance (Q : V/A: P:izR (18. we will calculate the rootmeansquare current of a nonsinusoidal wave form./) dt . In this case study. 17 as well as the __^^^L^^ )^^^.5 CASE STUDY 441 continued The power generatedis relatedto the magnitude of the current.. such as triangular or squzue waves. To understandthis. it is by no means the only waveform that is used. : I fr.28) The average powercanbe determined by integrating Eq. Now. although the simple sinusoid is widely employed.27) J .2s) Thus. such a cuffent is capable of generating power.2g) overa period with the result: r p E _ . as the nameimplies.l^^A it}ic nhanfar .Substituting Eq (18. This quantity has meaning becauseit is directly related to the averagepower absorbed by an element in an AC circuit. the rms current is the squareroot of the mean of the squared current' Because1/ \/2 : 0. lpear sln(tr.For a resistor. 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.I8.". electrical engineersand scientistsdetermine the root mean squarecurrent i*. l'.26)gives (18. Therefore. (A). 2 mcr\ Thus. the l*" can be evaluated analytically with closedform integration. (lg. Ohm. is equal to about j}vo of the peak current for our assumed sinusoidal wave form. s/Cz). which is calculated as il"ssin?(att)dt ipea.and y: voltage(V: J/C). some wavefbrms must be analyzedwith numerical integration methods. Integration can be used to determine the averagecurrent over one cycle: .
a changein variable is performedby applying Eqs. with the default stopping criterion of es : 1 x 106.*sin(2*pi*t)  . 4 8 0 0 5 8 7 8 7 3 2 6 94 6 e .41260804810169.k.the exact value of this integral to fifteen signilicant digitsis 15.Notice that Simpson'srule is more accuratethan the trapezoidal rule. we obtain a result that is correct to over nine significant figures in five iterations.25q)l'0.: Io'' dr sin2nt)2 eoet (18.25 zs+o + 0.4r'2608A4288977 1 . The value for the integralto sevensignificantdigits is obtainedusing a 128segment rule or a 32segment Simpson's rule.)sinzt(0.22)and (18.0 0 8 5 Thus.442 NUMERICA IL NTEGRATIO O N F FUNCTIONS continued Solution. We can obtain an even better resultif we impose a rnore stringent stopping criterion: r5.29) For comparative purposes. trapezoidal The Mfile we developed in Fig.First.2 can be used to evaluate the integralwith Rombergintegration: >> format longi (10*exp(t) >> i2@(t) .2.29)to yield t.t " 4 Theserelationships can be substituted into Eq. Integral estimatesfor various applications of the trapezoidal rule and Simpson's1/3 rule are listed in Table 18. (18.: lt ) zs'.30) Jt .o (18.4L260804810169 0 i ra l Gaussquadraturecan also be used to make the sameestimate. (18.r 4 4" dt:dt.25 dt / [l0e. Theintesral is thatmustbe evaluated . 18. 15 .^2.1".23)to yield 1lI t:*t.
4 2x l 0 . 4 1 2 6 3 9 1 154126109 Forthe twopoint GaussLegendre formula.0 r00 0000 1s.1 1.65755A2 r5 4058023 I 5.412568151 259x1Oa 15412605565 I6lxlOs 15.5 CASE STUDY iir' rhT:' " continued for the iniegrol Volues colculoted usingNewtonCotes formulos.I8..3 15.401429095 0.1c/o.480816629 r5415468il5 I5 412771415 l5 412618037 3\ 1763 4. 1/V3.A725 l 5 4 11 9 5 8 3 6 0 4 . Segmenls Inlegrol e. \tr.65755 which has €t: l.a L B 2x l 0 .2 Iechnique Tropezoidol rule I 2 4 B )b 32 o4 I?B 2 4 B t6 32 0.5555556(1.5555556(2.b08 4C 91 34509 . These values can be substituted into Eq.1) 1:0.17)to yield an integralestimateof 11. .313728. rms:2 . (18.3 Poinfs Results of usingvoriouspoint Goussquodroture to opproximote formulos the integrol. 5) i. 2 2x 1 O .b 20217688657 t 5 .684915):15. which represents an error of €r : 22.99182.5 2 3 .4426 0. The threepointformula is (Table 18.respectively.8888889(15. 0. with the resultsbeing 7.2 2 .0r86 I 06x 1O3 6 48 x l05 S i m p s o n1 s/ 3 r u l e TABTE18. l"/"1 22. 2 8x l O .3. this function is evaluated at t.684096and 4. Finally.163266493 ) 6178 15.t : 1 I J3 and.16321)+0.237449)+0.4 5 6 t L9978243 15.412607893 lOl x 106 1 54 1 2 6 0 8 0 3 8 6 .59 4 ..4I2. the integral can be evaluatedwith the builtin MATLAB function quad and quadL: >> irms2quad(i2. Estimote e.l"/ol TABTE18. 0 1x l 0 .The resultsof using the higherpointforrnulasare summarizedin Table 18.6Vo.
8 l riod oi M . (b) the twopoint Gauss integralto deterrnine thepercentrelativeerrorof theresultobformula./r r.3 Evaluatethe tbllowing integralwith (a) Romberg intein the tbnnat of Fig. 0.).87o of the maximum.7 T represe PROBTEMS l8.9 3 2 L inax = .s6t6. with quadt being a little better. 1562.5%). and (c) MATLAB quad and quadl functions: tainedr. we get >> i rrrs=sqrt. Use the analytical solutionof the quadrature eration (tr :0.5a/. . 5) 18.2 Evaluate the following integral (a) analytically. Checkthate.(irrns2 ) irms = 3. and (d) MATLAB quadfunction: .88685 A occurs at t : 0.rr .()..to I . t: I xe'd. (c) the three_point Ji 1: I {:.0547.rr r 0. we can readily employ the fminbnd tunction to determine this value.25).. .5%). As we did for the simple sinusoid in Eq.l Use Rombergintegrationto evaluate Gaussquadrature tbrmLrla. 18. of the n (a) Rom twopoir quad fu Iti.: r8. For ex" arnple.i: where the pip meter.2249 s."8 ] 6.5 Th where . We can now compute the i. using ( twop( quad'l 18.7 . (18. is lessthane..1. using the result computed with quadl.t rk\ Dnmhprcinreorarion (F" :0. B B 6 B 5 3 8 l3 9 3 2 5 A A maximum cuffent of 7. : 0. Your resultsshouldbe presented 18. Hence. t (r) i(I) Evalual ance ol formulr 18. Becausewe are looking for a maximum.2917x I2d.92588945948554 This result could then be employed to guide other aspects of the design and operationof the circuit such as power dissipation computations. an interesting calculation involves comparing this result with the peak current.n by merely taking the square root of the integral./ Jt \ f: / 1\  : . we evaluate the negative of the function: Use the formula error for termine followir 1. Recognizing that this is an optimization prob lem. for this particular wave form. 11 : 6 0 8 0 4 8 C 9 r 957 Both these results are very accurate. 73 18.4 Tt erf(c irms2 = 1 5 .444 NUMERICA IN L TEGRATIO N O F FUNCTIONS continued >> irirs2=quad1 (i:.1.r to an accuracv of s.6 Th I*^ ForT = 0 . 2 2 4 B 1 9 4 0 3 1. the rootmeansquare value is about 49.vith Rombergintegration.
:il function.) : a rd = :f (mg/rn3). I 7o. but usethe tbllowing equations for F(_r)and d1.Determinethe percentrelarive QQ):9+.4r1 enorforeach casebased on the true value.1Vc: and(b) the MMLAB qu. E For f : l.0123. ancl (c) the MATLAB i ( r ) = ( 6 0.5e() *Zett'1sr rvith MATLAB's builtin tirnction +r:f . rr : the initial time (min).' n.5.' .75 andn = J scribedby the function (a) Romber_e usine integlarionto a tolerance of 0.relawhere u : velocity.perfbrm the integrationf}onr r : 0 to 30 m.18. l3./ ) 2+ ( 6 0.l3 Supposethat the currenrthrough a resistoris demeter. and the resistance is a function of the current: l8.00055rr * 0. (a) Assumethat Ohm's law holdsand R : 5e.12 Computethe power absorbed by an element in a circuit as described represented in Sec. formulas.3r.5).25i3).and threepointGaussLegendre 1= I s. . Q1r) : flow rare (m3/min). : 0.I. Determinethe f'low in the pipe if r() = Q. ).t cr.18. and (c) the MATLAB quac function. tu.dI'unction.r2 .r r1r. and (c) rhe MNLAB q:r.which canbe de'5/ c (t) .5olc.r i . j7.5. (b) the Ilopoinl GaLrssLegendre fbunula.6 cunent cln tre cornputed as l_. n F = I too{ .r./' s i n ( Z r t(r) 7J l ( t ): 0  / r\ [or0<t <T12 forTl2<t<T i(i1:5r'ls'sin2zr <t <Tl2 ft'trO Evaluate the 1o5 using (a) Rornbergintegrationto a toleri(t) :0 for Tl2 < t < T ance of 0. 18.but for a simple sinusoidal as currenti : sin(2trt/T')where I = I s.ll The alrount of nrasstransported via a pipe over a periodof tirne can be cornputedas R : l O i+ 2 i 2 / 3 =L. r: : rhe llnal tirne (rnin).and n : a paraItt.' (.(b) the two. i n ( " 4 ) qu:r.. termined Theforce on a sailboatmastcan be represented 18.r  Jn Jt sentations define the temporal variations in flow and Use the (a) twopoint and (b) rhreepointGaussLegendre croncentration: formulas to estimateeri( 1.6x0..tons anclthe angle is in radians.10 Computework as described in Sec.r'3) (a) analyticallylnd (b) usinr the MATLAB dl.r): F(. but fbr the curlent as specifiedby 'rrr: I\nu.icos21o.9 Evaluate the doubleinteglal \ / t. suppose that i (r) is definedas : l o d . 0 to 60 using the ./.12 d(t) : 0.045.r):1.r The force is in new. * r. "vhere 18.7The velocity profile of a fluid in ir circular pipe can be 18. l.Note that flow is eqLral to velocity times area./ Jo where..ll Perfbrnr the samecomputarion as in Sec. Therootmeansquare 18.r0 : the pipe's radius. ) " " (b) AssLrrne that Ohm's law doesnot hold and that voltage and currentare related by the following nonlinear. Qft)c(t) dt Compute the averagevoltage over / : multiplesegment Simpson's 1/3 rule.9. theprpes centerline.Compute F fbr the case where I1 : 10using (a) Romberg integrationto a tolerance (b) the of e.PROBLEMS 18.v to implementthe tunction. = r o ( r. Usehetp to understand hor. . l8.: the elevation abovethe deck and 11: the heighr of themast..i tunction. d.4 Thereis no closedfcrrnrsolution fbr the error functiol 445 whereM = nlass(mg).opoint GaussLegendre formula.2 r1.:cr function.12 * 0. r : radial distancemeasured out front tionship:I/: (5i .5 by the Determine the mass transported between 1r : 2 and following function: l: : 8 min rvith (a) Romberg integrationto a toleranceof rH 0. I %.. and c(r. 18. The following functional repreconcentration sySqal .
As such.0 0 .i6n) will be curvein Fi P I 8. llt. 1 5 52. the flow rate Q (thatis. Pl8. The velocO . here A r o du n d eo r x i o lo o d i n g i sd l m e n s i o n l e s s inch ) .02 40.it is representative Vttt:. I 6b.1 the vr U u U 0f out to thepoint The areaunderthe curve from zero stress of the material.I6 (o) o n d ( b )t h er e s u l i ns gt r e s s s l r c ou in r v ew .as shownin the stressstrain r FIGURE PI8.I u(2rr)dr ity of an object in the dircction of a fbrce is given by JIJ llt.IQ x. Pl8.the a 04 06 t'3 0 a2 volume of water passingthroughthe pipe per unit time)can 03683 038t9 0. Pl 8.446 IL NTEGRATIO O N F FUNCTIONS NUMERICA where center 0. It ruptureis calledthe modulusof toughness providesa measure to of the energyper unit volumerequired lfr ol causethe materialto rupture. I ittllt LJo the material'sability to withstand an impact load. 1 0 43.use the fbllowing currentdata to develop mericalintegrationto computethe modulusof toughness curve seenin Fig.0082 0 )441 tween summation and integration.recall the close connection i.l5 The work doneon an object is equalto the fbrce times rl the distancemoved in the direction of the force.18 stretcl tor: M o d u l u so j t o u g hn e s s F. I 60.(To graspthe meaning ) 12 t. fn F.) For a circular pipe. Therefore.0 0.I7 . Io3 A o2 and A be computedby Q : J u tl A.Usenufol If C: l0 s farad. ol is the pipe's crosssectional area. Io3 A 0 0486 0.n ds t r o i n s t r e si s i n k i p s p e rs q u o r e i l 0 r l b / i n ' 7o 18.l{ If a capacitorinitially holds no charge.0 0 . where u is the velocity.l7 If the velocity distributionof a fluid flowing through pipe is known (Fig. Itl. Determinethe work if a constantforce of 200 N is appliedfor all r. deformed.2282 i.05 0 .the voltage acrossit as a function of time can be computedas u:4t u:16*(4r)t 0<t<4 4<t<14 Al I I where r is in m/s.0 0.l7).25 55.16 A rod subject to an axial load (Fig.s 0B bethis relationshipphysically.0 Ruptu re where using result 18. the stressstrain a plot of voltageversustime: Itt. A: r12 anddA:2rr dr. l( rrfi (al FIGURE PI8. 2 0 60.
r .tno qt / whererr: upwardvelocity. \ r . 18.andg : downward acceleration of gravity (assumed conslant: 9. / t . : 160. \.rt : t. I 8. rrf.Your results should be presentedin the form of Fig. L 0<l<10 l0 < r < 20 2O<t<30 I d. \l'6 ltt./ / .\to an accuracyof e. q : fuel consumption rate. Evaluatethe vertical dislancetraveledby a rocket if 18.20 The upward velocity of a rocket can be computedby the fclllt>wine tomula: r. (b) Use MATLAB to determinethe inflection points of this function.initial massof the rocket al time / :0.) .€'sln\ Jo l +.If I : 1800m/s.= 1 1 0 0 .r : .30 0 046 0 15 0 13 035 N 4 t03. rr : velocityat which fuel is expelled relativeto the rocket.I to 2 I and frorn to 2.N rrf.rno.calculatethe work done by 18. : 0.=lIl]l \ / ..s .18 a spling that has a springconstantof ft : 300 N/nr slretchrng : 0.^ E:e\t2 \/ L]T (in this case.8 m/sr).57c.. and q : 2500 kg/s.PROBLEMS r is the radial distancemeasuredoutward from the where ol thepipe.21 ]'he not'rraldistributionis definecl as . . Discussthe using results. n..is the total ladir"rs 0 the multipleapplication trapezoidalrule.l 0 3 .r r l n { "' ' .5 r r'=50I+2(t20)) .determine how high the rocket will f1y in 30 s. 0 0 0 063 020 00r 005 O C)82 025 0 028 o l0 0ll 0. conrpute where r. Usingthe following data.000 kg.35m: to.3 cm).19 vertical velocityis given by the r':lll\1 (a) Use MATLAB to integrate this function fiom.If the velocilydistribution is given by center 447 18.22 Use Rornbergintegrationto evaluate ^)/ . 18.r F .
) (19. we usedcalculusto integratethis equationto determine the vertical distance z the jumper has fallen after a time /. Recognizingthe sensitivityof numericaldifferentiationto dataerror. z(t.2) .Differentiotion Numericol CHAPTER OBJECTIVES The primary objectiveof this chapteris to introduceyou to numericaldiftbrentiation. 17. Knowing hclwto evaluatederivativestbr unequallyspaced data. Specific objectivesand topicscoveredare ' ' ' ' ' ' the applicationof hishaccuracy Understanding numericaldifferentiation lbrmulas for equispaced data. Knowing how to evaluate derivativesin MATLAB with the dif f and qlradient functions. col. Understanding how Richardson extrapolation is appliedfbr numerical differentiation.*::TJ u(r ) velocity of a freetalling bungeejurnper as a function of time canbe At the beginningof Chap.rtour YOU'VE GOT A PROBTE'I/I R::*. Knowing how to generate plots and vector fields with MATLAB.
. In both theseinstances./(. 19.) A. distinguish.1a to c.thereare other functions that may be difficult or irnpossible to differentiate analytically. Calculusis the mathematics Because engineers and scientists must continuously deal with systems and processes that clrange. the mathematical definitionof the derivativebeginswith a difference approximation: A.1). the difference a derivative: becomes ' dr  {r+0 lim .4\ In eithercase.2)into Eq. I9. you might also be askedto computethe jumper's acceleration. or the secondderivative of displacement: du(t\ . . to perceive the difference in or between. 6 ) where y and /(:r) are alternativerepresentatives variable and x is the for the dependent independent variable.If A:r is allowedto approach zero.) (1 9 . to dffirentiate means"to mark off by differences. differentiationcould be usedto make the determination: r'(l): dz0) .I9. As depicted in Fig. which servesasthe fundamentalvehicle for differentiation.7) ./("r.it would be usefulto differentiate the discrete datato determine the velocity and the acceleration.That is. According to the dictionary definition.l.3)anddifferentiating wouldbring us backto Eq. (19. I Whor ls Differentiorion? of change.I INTRODUCTION AND BACKGROUND  9. the resultwould be a(t) :S . 5 ) Although a closedformsolutioncan be developed for this case. we could either take the first derivativeof velocity.r (t9. (19. numericaldifTerentiation methods are availableto obtain solutions.In this situation.suppose that there was some way to measurethe jumper's position at various times during the fall.t.asoccursin moving from Fig.ftt ( #. calculusis an essential tool of our profession.I INTRODUCTION ANDBACKGROUND 449 Now suppose problem. Standingat the heartof calculusis the mathematical conceptof differentiation.Further.". 19. Substituting Beyond velocity."Mathematically. This chapterwill introducevou to someof thesemethods. at 119. you were askedto dethat you were given the reverse termine velocity basedon the jumper's positionas a function of time. the derivettive.f (xi + Ar) .y A..t\i/::: d 2 z t tl dt dt2 (19. ( 19. Thesedistances along with their associated times could be assembled as a table of discrete values. Because it is the inverseof intergration.3) Eq. To do this.) Ar (1 9 .r f (xi + Ar) . represents the rate of change variablewith respect to an independent of a dependent variable.  .
Partialderivativescan be thoughtof as taking the derivativeof the functionat a pointwith For example. Note that Newton used the socalleddot notation: i.r.l i m 3y Aro / ( .thederivativeis the slopeof the tangentto the curve at.lc./(r * A.rr. .r Ax ( 19. #:*(#) .9) Similarly. .)is detined as af 3. on both y. . whereas y' is attributedto Lagrange.ri) ]' is the first derivative of . r0) To get an intuitive graspof partial derivatives.8) Thus. The secondderivativereDresents the derivativeof the first derivative.Suppose to a function.o l9 f ./ ( x ..the dot notationis usuallv usedfbr time derivatives.r e v a l u a t e d a t r .thedifference opprorimolion defrnition of o derivotive: os Ax opprooches becomes o derivotive.ature.t') . I Thegrophicol zeroin goingfrom(o)to (c)./(.recognizethat a function that depends on you aremountainclimbingand have two variablesis a surface ratherthan a curve.r'with ig r e s p e c t t o . r') A. Todav. the partial derivativeof.450 NUMERICAL DIFFERENTIATION + Ar. . partial and the derivativeof/with respectto. partial Finally. l ' ) Ay (19. .r all but one variableheld constant. + ar AI (b) (c) F I G U R EI 9 . r . 1.l(./with respectto y is definedas a" f.given a function/ that depends .r ' * A . derivatives areusedfor lunctionsthat depend on morethanonevariable.r. A s s e e n i n t h e v i s u a l d e p i c tF io n. f(x) r.f that yields elevationas a function of longitude(the eastwest access oriented 'The fbrm dt'ldx was devisedby Leibnitz.It is commonlyrefened to as the cun.f '(. . whereh ldx [which can erlso be designated oS.{ at an arbitrarypoint (x. the secondderivativetells us how fast the slopeis changing.r.l/ o. r ' ).r+o ( 19.r.because a high value for the secondderivativemeanshigh curvature.
' ax (19 . which is not couchedin termsof thepositionof an objectbut ratherin its change with respectto time.I9. ihuflo* in {o)is fromleftiorlght However. o negolive grodient leods to c positive flow. is negotive for thiscose. k : coefficientof thermalconductivitytW(m . Among the most colnmon of thesearc the cortstitutive /au's that define how potentialsor gradientsinfluencephysicalprocesses.r axis) and latitude(the northsouth oriented r'axis). A prime exampleis Newton's second law.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.rn. Thereverse signin Fourier's coseis depicied in {b).2).2 "downhill" Grophlcol grooient. In fact. For example.many of the laws and other generalizations that figure so prominentlyin our work are basedon the predictable ways in which changemanifestsitself in the physicalworld. Differentiationis commonplace in engineering because so much of our work involves and science characterizing of variables tlrechanges in both time and space. K)1.wherethepositrve lo o negotive grodlenl leods heotflow fromright to left Directionof heatflow . or gradient. Aside from suchtemporalexamples.this can be expressed mathematically as Q: .Thisis iheorigin of lhe"minus low of XeotcJnduction.) I 0. d T . r'e). Thus.K. the slopeto the eastwould be 0f(xu. 19. andx : distance (m). y6)/3"r. For the onedimensional case. f : (K). FIGURE I9.Fourier's law oJ heat conductionquantifiesthe observation that heatflows from regions of high to low temperature.andthe slopeto the noth would be 8/(xo.y.1.Many specificexamplesof suchapplications could be given in all fields of engineering and science.I INTRODUCTION ANDBACKGROUND 451 .whichdrivesthetransferofheat(Fig.r.If you stopat a particularpoint (. Thus.r) : heat flux (Wm2).the derivative temperature . 19.providesa measure of theintensityofthespatial temperaturechange. numerouslaws involving the spatialbehaviorof variablesare expressed in terms of derivatives.) rr where q (. dueto theorientotion of Corteiion theslope coordinoies. depiciion of o temperoture Becouse heolmoves fromhighlo low lemperoture.
452 DIFFERENTIATION NUMERICAL TABTE 9. I Low Fourier's lor. Recall tha we employedTaylor series expansions of derivato derivefinitedifference approximations forward. ax Velocity Sheor Sfress Siress Dynomic Viscosily Young s Modulus o : EL! L Deformction Sirnilar laws prcvide workablemodelsin nranyotherareasof engineering andscience. These arejust a few of the applicationsof differentiation that you might faceregularly youwillnorin the pursuitof your profession.4.. chernicalreactionkinetics. 1. 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.theseestimates haderrors that O(h2)that is.1).2 HIGH.v Theonedimensionol forms lowscommonly in of someconstitutive used e n g i n e e r i no gn d s c i e n c e .For example.e.dh dT Mossdiffusion Flowthrough porous medicr flow Currenl Fluids Eosticily Cor.ln addition. When the functionsto be analyzed aresimple.ACCURACY DIFFERENTIATION FORMUTAS We have already introduced the notion of numerical differentiation in Chap. and solid mechanics estimate importantfacet of our capabilityto work effectivelyin theseareas.at best. However.andcentered tives. we developed difference approximations were of tirst and higher derivatives. to obtain approximate usingnumericaltechniques asdescribed EXA r9. Beyond direct engineering and scientificapplications. their enors were proportionalto the squareof the stepsize.backward. have theability valuesfor derivatives. Iu Heod For'vflux Currenf tlux ic Hydrou Conductivi\ Elecfricol ConductivilY _^ d v _ d. alreadyseenan examplein the fbrm of Euler's methodin Chap. In addition.22. mally chooseto evaluate when them analytically.rorio1 Mc.r  t^ du .4. 6 the secantmethod was basedon a finitedifferenceapproximation of the derivative.Thislevel of . Equotion q : .recall that in Chap. numericaldifferentiation isalso importantin a variety of generalmathernatical contextsincluding otherareas of numerjcal methods. elecincluding the modeling of lluid dynurmics.K *. (Table19.it is oftendifficult or impossible the function is complicated.In Chap. In Chap.probablythe mostimportant appliWehave cation of numericaldifferentiationinvolvesthe solutionof differentialequations. masstransf'er.wewillinvestigatehow numerical differentiationprovides the basis for solving boundaryvalue problemsof ordinary differentialequations. Remernber that.The ability to accurately isan derivatives tricity.r . next.the underlyingfunction is often unknown and depoints.For both these you rnust fined only by measurement at discrete cases.
r *.13) In Chap.2 f ( r . by collectingterms: (19.5 using finitedifferences in the following table..0 . 19.lthroughFig.0 .f'(. we truncatedthis result by excludingthe secondand higherderivative tenns formula: and were thus left with a fbrwarddifference + o(h) (r9. r ' 0 .27)]: the following forwarddifference approximation of the second .5along with the lowerorderversionsfrom Chap. Recall that in Exarnnle4. The followderivatives. 2 and a step sizeof h: 0. / t x . 5 . 19.the forward Taylor seriesexpansion can be written as [recall Eq.2 HIGHACCURACY DIFFERENTIATION FORMULAS derivationof thesefbrnrulas. 1 5 x ' . Notice that inclrsion of the secondderivative Similar irnprovedversionscan be developedfor the backward and centeredformulas as in of higherorder The formulasare summarized well as for the approximations derivatives.l t.1 6 ) . ing exampleillustrates the utility of theseformulasfor estimating E X A M P TlE 9.4.3throueh . h+Otlr) .25. 19. 1 3 ) to yield J l x i + z t.. we now retain the secondderivative term by substituting derivative[recallEq. f (ri+t) : f (xi)+ .15) ( l s .. 1 . ) .f'(x)h * which can be solvedfor (1e.4 we estimated f ( r ) : . 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 comnutationhut emolov formulasfrom Fis. HighAccurocy D i f f e r e n t i o t i oF normulos the derivativeof Problem Stotement. zn' or. 2 5 x* 1 . (a. .. Fig. (4. We wrll now illustrate how highaccuracy finitedifference formulascan be generated by includingadditionaltermsfrom the Taylor series expansion. 4.x):ry+o(h2) (r9.f. ( 1 9 ..0 . For example. The resultsare summarized at r : 0.12) f'(r): {!tP f'(.I 9.Gi):W+o&) i n t oE q .): tSt#  +r+ o(h2) (19..r7) term hasimprovedthe accuracy to O\h21.13)] f"!:t) ^z *.1 ) + .r4) In contrastto this approach.
r./(.19.+r)* 6/('r.*.r.) h3 0(h) o(h2) 31 (1.4l'6i+) + f (ri) f"" (x. 0 31 ) 1.) Ita FIGURE I9. l . + + ).*..25 . The data neededfor this exampleare xi2:0 xi.2 .*') .f (xi+) . 19. 6 3 6 3 2 8 1 f ( . * ) + l l / ( x .) h2 .) = .3 Forwordf initedifference formulos: versions hryo ore presented for eoch derivotive. 2 Theforwarddifference of accuracy O (h2)is computed as(Fig.3(0.r :0. 8 2 V t 2(0j.2 +6 1 .5) 0. moreoccurole.*) .5) 0.r. f ( x i + r ): 0 .+r).2 + 4(0.2+ 8(0.) + 3/(.*. Solution.24f $i+2)+ iB.rr*J . f (x.3) : / (0.925 .. l \xil: Enor 0(h\ oftll f (r.5"f (.) h2 o(h) o(h2) f (xin:)+ 4.)= h1 o(h) 0T'?) f "" (r.5 ri+r :0'75 li+l : I f(ti):1.f (r.)+ 2f (x.consequenlly. x i * ) * 2 6 7 1 .) ln' FourthDerivoiive f (x.1035156 J'Gi) :0.636328 r) . Thelotier version incorporcles more terms of the icyior series exponsion ond rs.454 NUMERICAL DIFFERENTIATION  F i r s lD e r i v o l i v e f ( xi + t t f l x .f(r.2 5 5 6+ : 0. 8 5 9 3 7 5 : € r: 5 .) * f (.1"' ThirdDerivotive (ti*z) * 3/(.4f (t.636328 .18 )( 1 .3f (x) 2h Second Derivotive f (.+1) l 14f (x. r n 1 * 1 4 .t. r i + ): 0 .*21 + 4f (xi+) .2 4 f ( .4) / t t .n.llVt 2(025) Thecentered difference of accuracy Otha.878125 €.19.9t5) . 1 0 3 s 1 s ) .1 . 9 2 5) 4 ( 1 1 .*r) .2 f(xit): 1.5.ri : 0.) = f @i*) 3f (x. f ( x .0 . :s ) l ( 0 .:3. Thebackward difference of accuracy O \h2I iscornputecl as(Fig.is computed as(Fig.2f (x.
. to computea third.4 formulos.). more accurate.'r) f"(xi)= h2 o(h) o(h?) . However._r) 2h3 Fourth Derivotive * /(r:.4. this formula is usually written its convenience for the casewherehz : ht 12._.:) hl Ihird Derivoiive o(h) o(..t .ttt/tt)).r..2.) . derivativeestimates provideda means to obtainan extrapolation Recallfrom Sec. .l (r.14f (.) + 26 f @.r.t.t .2f(x.i that Richardson ( improved integralestimateby the formula [Eq.f (xir.f"(r') : 2f (x) .. as in 4 . ) 3J(.r.) + 3f (xi. Because usingtwo stepsizes: of wlrere/(ft1) andI(h) areintegralestimates when expressed as a computeralgorithm.h'1) f"'( r'.4) .3 RICHARDSON EXTRAPOTATION when To this point.r t h l \ l (te. versions cre r.\i).4f rr._) t 3f (.19..f (r) .r). more accurate the tbrmula basedon the ferenceyields the exact derivative at .s) 1""(t) = o(h) o(h2) FIGURE I9. I 8.f (.4. Thelotier h. 18.f (xi_t) lr' 3"f(.t+ 6f (r.J + J(.rzo Bockwordfinltediiference more occurote exponsion ond is. t._r) f lt.) h7 3.the errors for the forward and backwarddifferencesare considerably the centered difthan the resultsffom Exanrple4.18f(x.)= .2f (. corrsequently.r.5.f(il .\i 1) : Second Derivolive . \ n Lt1 o(h) o(hr) f ' ( .resenled for eoch derivotive.I 4 f (x.4)] I : I ( r r : )* . I9.r8) /r1 and /22._)+ 24f (.surprisingly.l l \.2) . This is because Taylor seriesis equivalentto passinga fourthorderpolynomialthroughthe datapoints.. versionincorpoTotes more ferms of ihe Toylorserres As expected.r.24f (ti ) + I I /(.3 RICHARDSON EXTRAPOLATION Firsl Derlvotive +rr\ J \ i'  Error frY J '4i t.{. approximation.v : 0.5/[r'r) * 4f (x.A third approach.._r).) * f (x.) 5. we have seenthat there arc two ways to improve derivativeestimates (2) (l) a higherorder forrnula the step size or use employing llnite diff'erences: decrease extrapolation. l r t t t l ._.4f (r. usestwo basedon Richardson that eniploys more poiuts.
39/(r. ) l 3 / ( r r .25) .).636328 : 0.47a .r.r) + 12. _ ) 8ft3 Fourth Derivotive f (r. derivative at.) + 12f (x.t.xi) .).2 Richordson Extropolotion estimate thefirst Problem Stqtement. f t.n) + 6f (r. 5 a n dh z : 0 . T h e nu s eE q .) f "'(x. f ( r i + r ) + B / ( x i + 2 ). 2. + .f (x.+2i\ + 39/(.f ln Error (.*) .)= f"'(x) = oft'1) 0(hn) 2hl .r. 2 D(0.rr+r)+ 56f (x.1r .20) For centered difference with O\h2).456 DIFFERENTIATION NUMERICAL FirslDerivoiive f ( x .1) I ltr Second Derivotive .) + f \x. Using the samefunctionas in Example19.*.5): :. In a similarfashion.19)can be written fbr derivativesas 4l D::D(h:t_:D\h t) 1 )J TIGURE I9.5 employing s t e ps i z e s o f h 1 .+t) ) h2 0(h') o(hn\ f" (x): f (xi*) + l6/(..934375 0.*r) * 2..r : 0.t. EXAMPLE 19.1 _f (t. z) l2tt2 Third Derivotive f (r. 2 5 .) + l (.5 €r : 2. is0. f( x i _ z ) * f ( x .r.1 .67c I and : D(0. 2 0 ) to computean improvedestimatewith Richardson Recallthatthetrue value extrapolation. * . the applicationof this formula approximutions will yield a new derivative estimate of O(h+). o(h'1\ o(h') f (x.( 1 9 . Thelofier ^. 1 0 3 5 t 6 0.B ..: *1.._.4f {x.o..1 ia a^nao^ 'anllrr mnro version moreterms incorporotes of theToylor series exponsion occurote. (19. The firstderivative 0 .0 tt : *9. r . ).f(x_r) 1f (xi_z) 6h1 rvvg ve s onso'e oresetred ['^rled'{Ie'ence fo'rnulos: Cenie'ed for eochderivotive.1 3 1 1 " r .1.4f (xi_)l .) .Bf (.) * 3O/(ri) + 16/(:r.*2) + B/(iri+r) .0 .r ) .2f $. can be computed with centereddifferences as estimates Solution.9125.f (xi z) f"" (x) = h4 oftI) 0(ho) : f"" (x.f (t+) + 2f (xi r) .5 ( I9.. Eq./(.
Consethis would not occur. the presentcasematched polynomialprecisely.f (rr). (15.. the approachcan be appliediteratively using a Rombergalgorithmuntil the resultfalls below an acceptable en'or criterion.. . .9125 457 The previousexampleyielded an exactresultbecause the function being analyzed was a fourthorderpolynomial.21)J want to evaluatethe derivative.9.as was the casefor the applicationof Richardson extrapolation. Third.of course.Second. 19. 2 x . w h i c h l ' o rt h ep r e s e nc 4I D:4(0.x..20) to give J(l): t a s ei s e x a c t .FortheRichardsonextrapolationtechnique o. it can provide estimates anywhere within the rangeprescribed by the threepoints.r.f3 S eh ce . r :.21) wherer is the value at which you want to estimate Although this equationis the derivative. (19. only in cases In contrast. 19. Suchcontrolofdata spacbe evenly spaced and generated ing is usuallyavailable wherewe canusea functionto generate a tableof values.Such intormation cannot be analyzed with the techniques discussed to this point.4 DATA DERIVATIVES SPACED OF UNEGIUATTY The approaches discussed to this point areprimarily designed to determine the derivativeof approximations of Sec. dataalsohadto for successively halvedintervals.25)1. a n d ( . r i . fact. r 2 ( x r. the derivativeof the foLrrthorder For most otherfunctions.25\. it has some importantadvantages..t2 xr )(xe .rti) (. for difference In equispaced Eq.t evenlyspaced. r g . r u . Thus. analyticallyto yield a forThe polynomial can thenbe diff'erentiated the derivative. the derivativeestimate points. data from experiments or ficld studiesare often collected at unequal intervals. ducesto Eo.r s ) ( x 1  12) * lGz) ( .r1 .3through certainly more complicatedthan the firstderivativeapproximation Fig. I r ) .f f e r e n t i a t i ntg h ep o l y n o m i ay l'(x) : l'(re) (xo  2.The exactoutcomewas due to the fact that Richardson extrappolynomialthroughthe dataand then olation is actuallyequivalentto litting a higherorder evaluatingthe derivativesby centered divided differences.934315) 0.l r rr) (le. r o ) ( . frorn Fig. First. One way to lrandle nonequispaced data is to fit a Lagrangeinterpolatingpolynomial the locationvalueat which you to a setofadjacentpointsthat bracket [recallEq.4 DERIVATIVES OF UNEQUALLY SPACED DATA The improvedestirnate can be determined by applyingEq.2.Remernber that this polynomial does not require that the points be equispaced.and our derivativeestirnate quently. the points thernselves do not have is of the sameaccuracyas the centered to be equally spaced. would be irnprovedbut not exact. 19.. r .4.I9.21)evaluated at t : xt ret&q. For the finitedifference l9 . mula that canbe usedto estimate points For example.you can fit a secondorder Lagrangepolynomialto threeadjacent (to.5.ernpiricallyderived informationthat is. 19.the datahad to be a given function.) 2xrgx1 t . ( 19. ' z l ields: D)i.
l1 _3 J FIGURE I9. 6 01 K(133. Use numericalditTerentiation to evaluate the gradientat the soilair interface andenploy this estimateto determinethe heatflux into the ground. drl I a_ l.0375 2(0)00.)(0 ffi z(0)00.=u whereq (r) : heatflux 1W/m2). I : temperature (K). : 6 6 .458 NUMERICAL DIFFERENTIATION EXAMPLE I9. (19.a temperature graclient canbe measured downinto the soil. Equation /'(0) : 13.0.333 : __1140 : .0375) 0. &rd 1 : distance measured down fron the surface intothe soil (m).3 DifferentiotingUnequolly Spoced Doto Problem Stotement.0.012s) (0.l): q(::0t:ft.037s .0t25 .0375 .0. 5 .0 .5W (m . K)1.0r 2(0) 2s.333 K/m + 1440 whichcanbe used to compute w/ K\ w q ( z: 0 ) : . 19.133. The heatflux at the soilairintert'ace can be computedwith Fourier'slaw (Table l9.5 (0 +10 .21)canbeusedtocalculatethederivativeattheairsoilinterfaceas Solution.5 DERIVATIVES AND INTEGRATS FORDATAWITH ERRORS Aside fronr unequalspacing.0. k : coetllcientof thennal conductivityfbr soil [: 0.6 Tempercfure versus depth info the soii I9.0)(0. Note that a positive valuefor f'lux rneans frorntheair tothe that heatis transferred soil.133.0125.A shortcomingof numericalcliff'erentiation . As in Fig.6.0375 + t2 .anotherproblernrelatecl to diff'erentiating ernpirical data is that it usuallv includesmeasurement error.
(b)theresuhing numericol differenfiofion error. This minor modification is barely apparentfiom Fig.6 PARTIAT DERIVATIVES Partialderivatives alouga singledimension in the samefashionas ordinary arecomputed derivatives. However. as points lre summedto fbrm an integral. if the truefunctional relationship between the dependent and independentvariableis known.I 9. ond (d) the {o). ditferentiable functionto the data. r9.7 (o)ootc with no lllustrolion of how smcrLl dotoerrors cre omplified by numericol difiereniioiion.7cusesthe samedata.7b).In contrast.Obviously. The error arnplificationoccursbecause differentiationis srrbtractive.but with alternating points raised and lowered slightly.(cJ (cJ resrlting differenfiotion of curve mcnifesting irrcrecsed ln controst.suppose that we want to determine to partialderivatives for a . this relationshipshouldlbrnr the basisfor the leastsquares fit. 19.Incontrast. the fact thatintegration ib a sumrring process makesit very forgiving with regardto uncertain data.a lowerorder might be a good first choice.7tl etfbct is significant. For example. 19. voricbility thereverse (dJto (c)by toking (d)]tends o.6 PARTIAL DERIVATIVES 459 I 4 dr 4I dl FIGURE I9.In the absence polynomialregression of any other information. 19.7c.the resulting in Fig. Hence. 19.errorfieedata that wlren numericallydilferentiatedyields a smoothresult(Fig. 19. Fig.7ttshows smooth. As might be expected. Fig.randompositive and negativeerrorscancelout. the primary approach fbr deterrnining derivatives tbr imprecise regression datais to useleastsquares to fit a smooth.fintegrotion frorn operolion theoreounder to ottenuofe Imoving or srnooTn oolo errors. of curve dotc modified slightly.random positiveanclnegirtive errorstetrdto add.In essence.
A . I _ 2Ar lAr' a2. .r. 2 5 ) Then.thesecan then he employedto determinefinitedifterence t i o n so f f i r s td e r i v a t i v e s .27) WITH MATTAB 19.y) .r Af dl' EX (19.A .26) atJ 0x01 * /(.Af') .A. I * At') l(:r f Ax.A y ) l(x * Ax. 19..7.22\ (19.r .tr vectorof lengthn. of length zr . r ' ) 1 (r i.u) 2A.r. we can usetlnite differcncesto evaluateeachof the partialsin r': .) \' I Ar.. .I containingthe differences approxirnafbllowing example.. .y.A r ' ) /(r * Ax.r) 2Ar discussed to this point can be appliedto evaluate All the other fbrmulas and approaches partial derivativesin a similar fashion. [ drrd I' ( l 9.r') 4A.The result is called a ntixedpartiaLderivcttive.A x . the di f f function retunlsa vector When it is passed a onedimensional in the As described betweenadjacent elements. Forexamtwo or rnore ditl'erentvariables. To developa finiteditference tial derivativesin r': ja ( tf .21) we can first form a diff'erence in .r.r) with respectto both independent variables #:*(#) ( t .1 MATTAB Function:ai.v* A.A .f(r + A. r' 2Lx (1 9 . we rnight want to take the partialderivativeof/(r.rAy (19.23)  l(rA".A.t') "f /(r*Ar.r'* A1').r')  af ./ ( x . y ) l ( r .7 NUMERICAT DIFFERENTIATION the derivativesof databased on twobuiltMATLAB softwarehasthe ability to cletermine i n f u n c t i o n sd : i f f a n dg r r a d i e n t . ple../(x..t' .r yieldsthefinal result Collecting terms (1 9.r of theparapproximation. t0 For higherorcler we miehtwant to differentiarte a function with respect derivatives.f (ir d 1' 2L. v * A y ) ..460 D NUMERICA LI F F E R E N T I A T I O N with centered approximated diff'erences: Af i. r .l.A . r ' ."/("r..).t ..l' * A. r '.r (.
8. we must first of our in to develop a Therefbre.1000 0.1000 0.4 Usingit i r i for Differentiotion to difProblemStqtemenf. elementsof betweenadjacent the differences The dif f function can be usedto determine each vector. ^ 5 .1.I9.2 WITH MATLAB DIFFERENTIATION NUMERICAL 461 E X A M P L1 E 9. /drf f (x) Columns 10.3900 Note that becausewe are using equally spacedvalues.8900 Columns 1 through 0.1000 0. ^ 3 9 0 0 * x .0100 5 3. 2 + 2 5 * x .we could have simply performedthe abovecomputationconciselyas >> d=diff(f(r))i0. Compareyour resultswith the exact solution: f '(x) : 25 .2 0 0 * x . To the differences the result represents As expected.we merely perform a vector computedivideddifference by entering by the x differences division of the y differences >> d=dif f (y) .14 function: ) as an anonymous Solution. valuesof the independent and dependent a seriesof equally spaced We can then generate variables: >> x=0:0.400x2+ 202512.1000 0.1 :0.2 t 25x 20012l61513 from x : 0 to 0.1000 0.010A 1. order elements.3600"t3 * 2000.4900 8. 1) +x(2:n) J . >> Yf (r) .1900 B 21 .31"00 8.8. approximations of the derivative.after generatingthe x values. We can first express .900x4 + 400r5 l (x) : 0. tween adjacent interval: midpoint of each x values for the generate holding the a vector >> n=Length >> xm=(x(1:n (r) .1000 0../('T > > f = G ( x ) a . ^ 4 + 4 0 0 * x . >> diff (x) Columns l througth 5 0. I to the midpoint beThe vector d now containsderivative estimatescorresponding plot results. . " 2 + 6 15 * x . Explorehow the MAILAB dif f functioncan be employed ferentiate the function .For examPle.6900 6 through 11.1000 betweeneachpair of elementsof x.1000 Columns 6 through B 0. /2.
. 19.):a.the following code rejectsa vector that is not in ascending (ntonotoniorder ""'t.f..t:t:::::*.'o'. E .4.r . r . ^ 3 + 5 * 4 O O * x a^ A plot of the nurnerical and analytical estimates canbe generated with >> plot (xm.. ' a = 2 5 4 0 0 * x a + 3 * 5 7 5 * x . thefollowing statement displaysan error nlessage and terminates an Mfile if it determines thata vector :i has unequal spacing: if any (drf f (drf f (x) )=91 .i . error ( 'ur.the dif f function comesin handyasa progranming tool fbr testingcertaincharacteristics of vectors.d.:. compare Note that asidefrom evaluatingderivatives.' >X a = O :. erro](. ^ 24 * 9 0 0 * r . C l : .:f.n. E) As a final step.the results As displayed lavorablyfor this case. For example.r. end Another common useis to detectwhethera vectoris in ascending or descending order.iequal spacing' ) . r .we can computevaluesfor the analyticalderivativeat a finer levelof resolution to includeon the plot for comparison.. a .8.ya) in Fig.For example.462 D NUMERICA LI F F E R E N T I A T I O N FIGURE I9.8 (line) (circles) Comporisonof the exoct derivotive wiih nunrericol with esiimofes compufed MATIAB'sdif f function. > .
5900 As in Example 19. ''3+5*400*xa.Z NUMERICA D LI F F E R E N T I A TW IO IN T HM A T L A B 19.we can generate variables: and dependent valuesof the independent spaced > > f .1) ^..Hence. .tr . In the samefashionas Exanrple19. However.28) dtlIi : 2 The last value is then computedas the differencebetweenthe final two values.@( x ) A . 2 + 2 5 * x .0400 1.the values. . and fx is a vectorof lengthn containing where f : a onedimensional differencesbasedon f..rating derivativesat the valuesthemselves rather tharr in the intervalsbetweenvalues. for the intermediate valuesis returned tered dif'fbrence basedon the adiacent . > > x = 0 : 0 . ^ 3 9 0 0 * x .7. Y I rl.0.r. ^ 2 + 6 15 * x .dy. 1 : O .'o'.'l ('' \/ectorof lengthn. we can generate values for the analytical derivative and display both tlre numerical and analytical estimates on a plot: >> xa=O:.with forward for all the intermediate differences resultsare akin to using centered and backwarddiflbrencesat the ends.E = gradient ( f. Columns 1 through 5 5.2 0 O * x . Use the qiradient tunctionto diflbrentiate we analvzedin Example 19.I9.the following versiondivides all the resultsby the interval and hence returnsthe actualvaluesof the derivatives. ^4.5 EXAMPLE Usingu. However. Note that the spacingbetweenpoints is assumedto be one.l. . the flrst value returnedis the difvalues. h) where h : the spacingbetweenpoints.2 MATTAB Function: sradienr The gradienr.8. a seriesof equally Solution.01:. If the vector represents equally spaceddata.1 .44C\0 10.8400 21.1 00*xa+3*675*xa. >> !a=25 .B .^2 r. We can then use the graclient function to determinethe derivativesas >> dv_sradient1y.3100 8.^ 5 .A simple representation of its syntaxis 'x 9.fi r (19.5900 16.'. ^ 4 + 40 t J * x .a cenferencebetweenthe first and secondvalue.riii+rL for Differentiotion the samef'unction that Problem Stotement.l9. 4*900*xa.it doesso in a mannerthat is more compatible with evalr.4.ya) >' plot(x. Just as with the dirf function.4..1500 5.functionalso returns differences. f.4with the di tf function.8100 5.89t)0 Columns 6 through 9 4.
for a twodimensional for determiningthe partialderivatives of matrices.and 4.464 NUMERICAL DIFFERENTIATION 25 20 10 FIGURE I9. well suited vectors. h) where fx corresponds to the differencesin the r (column) direction.we will illustratehow gradient can be usedto visualizevectorfields.h : the spacingbetweenpoints.9 (circles) (line) with wiih numericol eslimotes computed Compcrisonof the exccf derivotive MATLAB s qradient function. trix. with the As displayedin Fig. Beyond onedinrensional the gradient functionis particularly maFor example.2)as wide as for thoseusedfor dl rf (0.This is due to the f'actthat gradient ernploys that aretwo times(0.If . and .In thenext ornitted.1). the function can be invoked as I fx. f.9. 19. fyl = gradient (f.the rcsults arc not as accurateas thoseobtained intervals dif f function in Example 19. .the spacingbetweenpoints in both dimensionsis assumed section.4.nis to be one.corresponds to the ditferencesin the y (row) direction.
To understand how this is done. Now supposethat we generatea grid of points in the xy plane and used the foregoing equations to draw the gradient vector at each point. tZ . For this example. We can representsuch a function mathematically as z: fk. The result would be a field of arrows indicating the steepest route to the peak from any point.if we plotted the negative of the gradient. the partial derivatives provide the slopes in the directions of the axes. which representsthe steepest slope.u.I9. and "y : distance measuredalong the northsouth axis.1. if you were mountainclimbing. it can be usedin conjunction with other MATLAB functions to produce visualizationsof vector fields. 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.v) where x and y are matrjces containing the position coordinates and u and v are matrices containing the partial derivatives. (#)' and a direction ^ _r / Af/Ay\ : tan' 0 \ffi ) where I : the angle measuredcounterclockwisefrom the r axis.y 7xZ 2. has a magnitude (#)'.the sradienr Bockground. called quiver. x : distance measuredalong the eastwestaxis.xt. Such graphical representationsare so useful that MATLAB has a special function.y) where z : elevation.A simplerepresentation of its syntaxis quiver(x. we can returnto our discussion of partial derivatives at the end of Section 19.1.y.The following example demonsffates the use of quiver to visualizea field.If we think of the two partial derivatives as component vectors. you would probably be much more interested in determining the direction of the maximum slope. to createsuchplots.However. Employ the srradient function to determineto partial derivativesfor the following twodimensional function: /fr rr\ : 1. it would indicate how a ball would travel as it rolled downhill from any point. In particular. Conversely.8 CASE STUDY 465 FIELDS VISUALIZING Beyond the determinationof derivativesin one dimension.Recall that we usedmountain elevationas an exampleof a twodimensional function. function is also quite useful for determining partial derivatives in two or more dimensions.
IO ffil$?":rui5r*n'". t9.z ). * x .ho1d on 19.6 2.l of r ro= the diff 19.the resultant of the partial derivatives can be superimposed ' ". A seriesof equally spacedvalues of the independent and dependentvariablescanbe generated as >> z_f (x.x ._fx.6 0.y).6 19.y.t riva and initi l9.y.4 1. ^ 2 .r l9.' redr 1.t..fy).2 first E'np As a final step.1 mati mati tt".i .3 finit.2 l9.ho1d oft : FIGURE I9.4 0. y ) y .asvectors r onthe contourplot: >> quirzer(x. Solution.ruj:lof J o twodimensionol function with rhe resulionr ofthe porriol 2.t equi first eacl cate 19.2 .2 0.6 1. Thenusequiver to superimpose 1 a vector fieldonacontour plot of thefunction. Wecanfirst express/(.466 NUMERICAT DIFFERENTIATION continued from : 2 to 2 and y : l to 3.2 * x . y) asananonymous function > > f = G( x . ^ 2 . The gradient function can be emproyedto determinethe paftiar derivatives: We can then developa contourplot of the results: >> cscontour(x. * y _ y . seco diffe In ei inter 2L.clabel (cs).
Tcstit with thc fbllowine 15 0.4.8 Develop an Mfllc to apply a Romberg algorithm to .:!iL l. f i o m P r o b .The function shoulcl cach value of thc indcpendcnt arrd dy2dx2 versusx. Richardsonextrapolationto estimatethc first deUse 19.r.4 of h 1: n f I of r' : cos.ra t .r.Thc function'sflrst line shouldbe sct up as f unctiotr (. (19.5. OrOO*2I = flif f rzq wherc x and y are input vectorsof length n containingthe variables. a t t dx 2 : 2 . Il . 19. Carry enou{hterms along ilt each expansionto evaluatethe first term that will be truncated to determinethe orilcr oi the approximation.2 antli * 2.. r'r+t . ilnd.PROBLEMS 467 Note that we have displayed the negative of the resultants. The function's and then drops away in all directions.r : r / 4 u s i n ga v a l u co f h : r 1 1 2 . and interval 2A.:. The In each case.19 Devclop an Mfile function that computcsfirst and second estirnates of oltler O1lir1brsedon the fbrclerivativc mulas in Figs..25) at ..and secondderivative variable. r = 5 u s i n gh t : 2 a r u lh z .ti. PROBTEMS 19.f ^ ft.e' ar x : 2 fot' ft : 0.l (r) : 5z2'.r at t : n llusing stepsizes rivative h.^+:. : r/6.i and i * l.1Compute forward and backward difference approxiof O(h) ard O(h]). C o m p a r c thetruevalue and with an estimateobtainedusing a centered differencc approxiniationbasedon /i : l.9 Develop an Mfilc to obtain flrsttlerivativeestimates data: for unequally spaced data. l ' = c o s .l2. The four' must then be combined in a way to climinate the equations first and secondderil'atives. 6.5 The resultis shownin Fig. As indicatedby the lengthening arrows.6Employ Eq. 19. and dydx and dy2d.2[) to determine o n v a l u e sa 1 of r :2ra 6t3 . ri I . km 25 3'2 50 5B 75 7B t00 92 125 r00 to estimatethe rocket's velocUsc numericaldiff'erentiation ilr .or (b) the valueslor the independent Test your programwith the data able are not equally spaced.0 .'Ib expansions fbr the pointsli :.+2.21) 19. the first dcrivative 19.1 9 . rcspecvaluesof the indepcndent and dependeut tively. J r : 1 .. Eq.6 t6 x 2 6l f (x) 0 9036 4.10. . diff'erent will be aroundthe point r." peakoccursat x : *1 and y : 1.j Use a Taylor series expansion to derivc a ccntercd approximationto the third clerivalive that is finitedifference do this.rn 19. in order that they point "downhill. 19. Ernploy centered difl'erences of O(/rr) for the and initialestimates. fbr your cstimates.2 are output vectorsof lcngth il estimatesat containing the first. Employ both O(h?) andO(ft4) formulas 19. .r : r . 19.the gradient drops off more steeply to the northeastand the southwest.11The fbllowing data was collectcdfor the distance traveledversustime fbr a rocket: It S J.'1 lrll'dx. E s t i m a l e truepercentrelative error rr fbr eachapproximation.but fbr the first derivativeof ln. 19..you will have(o use four secondorder accurate. . the cxpansion A.r will be usedin eachcaseof i ..r 8 at r : 0 based t h i s r e s u l tr v i t h x o= .7Provc that fbr cquispaced reduces to Eq.2Usecentered firstand secondderivativesof r . Cornpareyour rcsulls with the true derivati ves.3 25 35 o o B 4 2 2 0 0 15 9 6 rvhcre .r..3 through 19.r at . 19.5RepcatProb. 5 . data points. the approrinrrtions to estimate the difl'erence 19.3734 0. I . 19.. (19.r will bc used in each casc of i .r.dh {'. Havc your generatea plot of c1y'dx Mfile returnan error messlgeif (a) the input vectorsare not varithe samelength. andcentralclif'furencc approrirnations mations of O(h)\ and O(h1l for the flrst derivativeof ...
mi v.5.I \ r 0 + 2 i 0 ) A e (c) Repeatpart (b) lbr the secondorder fbrwardandback(Note same ward difTerences.Estimate (a) velocity @. "l to determine the inflection points of this 19.m 0 153 o:. 1 2 9 5 2 Use MATLAB to estimatethe inflectionpointsof thisdata.rt1z "f ( r ) J2tr Use MAILAB .rldt') and (b) acccleration(duldrl using nunrericaldifferentiation.r : 2 t 0. y 1 4 2 ) 3 .and data is taken orclel accurate forwiucl. ftl where x is the distancefrom the end of the carricr. 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 the volumc in the bucketasa function of titnc as tabulatedbelow.t.s x.13 Use the fbllowing datato find the velocitl'and accelerationat / : l0 seconds: r.r = 0. 3 5 2 A 74 2 4 1 9 7 4 . In order to do it.19 The objcctive of this problem is to compare lg.15 Use regrcssionto estimatc the accelerationat each is computed time thatthe centered difl'erence in theloop.and (c) backward finitemethods. s 3 9 9 O ) 2 9 5 2 a. tn 5l 20 2A2 244 246 2 0 8 2 t O A72 o 7 a 0 6 8 4 57 4.01. neer! temp 0 0 B 164 x rrh T.ral value of the derivativc.This will be done for wher u n r t€ (cmz Ane centr derly i::: x.l85 lAA 2aB t 75 2 37 '249 261 325 271 383 273 l s 05 x 2 I 0 (1 { l 0 . Plot the results: rcsullon the plot lol eomparison. that these can be done atthe 19. cn c. cm3 t.16 The nolrlal distribution I . s 2AA 0. use the centercdfinitediflerence(secondlor vclocity il Thus.12 A jet fighter'spositionon an aircraftcarier's runway was timed during landinu: ftom the normal 19. to develop a MATLAB 19.rO I 2 J 4 5 6 7 B 9 10 Use sccondorder correct (a) centered flnitediftcrence.1 polar coordinates r : 1. 6 7 5 8 .) time fbr the following data with second. The velocitv and acceleratiorr of 0. lr Calc order 19. 19.(rodl A75 t'. decreasc in increments ale down to a nrinimumvalueof Ar = 0. the flo$. startingwith .21 The velocity rr (m/s) of air flowing pasta flat surface If thr . for the first evaluation.rs r.s O 2 Position. clifTercnce second19. i : i a . commancl Mfile tunction to computeflnitedifftrenceapproximations to the first and sccondderivativeat eachr valuein thetable below. s Volume.r 0 : ) 2 .5 r 05 l 2 005399 f ( x \ 0 .22 M lime.third.468 NUMERICA D LI F F E R E N T I A T I O N 19.r valuesfor thecenorder correct) to find the vector cxpressions given rn tered differencc approximationwill be .O (.66 5374 5560 5800 6 0 3 0 6 2 4 a lG):e2'x Use t estirr conjr pollu (D= sedin the lr t9.5 and 2.h U.m O a7 4 IB 6 34 B 5l l0 63 l2 l4 16 7:3 B0 B4 . + t 0 4 0 a n d a : G .24 'T: t. and fburth(d) Plot the resultsof (b) and (c) versr. Include theexact orderoolvnomials.^ Time. Estimatethe flow rateat t = 7 s. approximation At 206 seconds.17 The fbllowing data was generated distribution: Use i r (N/ T t. (b) Develop an Mf ile function to evaluatethe centered fi nitedifl'erence s. 9 1 0 9 t 0 (b) fbrward finitedifl'erence.5or and acceleration rl.20 You have to measure 1 2 3.2. l( (a) Use calculusto dctclmine the corrcctvalueof thedenvativeatr:2. 3 4 B 6 B 6 6 8 . . Usc finitcditfbrence approximations thataresecondordercorrcct. is definedas 19.l1 A plane is being trackcdby radar. and centered finiteeverysccondin polarcoordinates I and r ditfbrence approxirrations of thc first clerivative of a function to the actr.5. backward. .tif f iy.r2 ): t9.25 45 678 B 5 9 3 t 0 surallpipe. Then.rate of waterthrougha 19.2atq7 352070 3e894 /(x) ::::: J.the .x.18 Usc the .
(nr21 of a lakeataparfionr volumeby difTerentiation: ticulardepthcanbecomputed 4 . Ernploy this estimate in with Eq. 4. 1: x l05 N ' s/rn2. ms V.9/m3 19. The fbllowing are measuredfiorn the surface(. lf. (PI9.oltage drop (V).'C 0 t9 008 l7 O.[. (cmr/s). (:) aa.002 0006 a2B7 0899 00r2 I915 0 0r B 3 048 A A?! 4 299 (ml) and .n v.r : distance 1cm). this equation(calledNcruton's to that the rateof cooling is proportional of cooling) specifies . o r a l a k ew i t h 3 .rmerical difl'erentiation techniclue availableto estimate the derivalive at "rr: 0. 6 x 1 0 6r n 2o f Q .8 ).4 4.26). D : a dilfusioncoefTicient unit area (g/cm3). : temperature whereI: temperature ('C).1 . 19'24psudsp'slaw is usedroutinelyby architectural engineers to detennineheat flow through walls.ne fiom the surfacedown to the bottom.Thus. cornputeft.2 7.] r:Q)A. engineer measuresthe fbllowing conAn environmental centration of a pollutant in the pore watersof sediments underlying a lake (.rczrsln' larl to determine the shear stress Use at the surface(.v : 0).(z) (Pt9. dV .orr4^r's law characterizes the r'oltagedrop across an inductor as di Vt.28) canbeexof a body(Fig.: L.25 Ths horizontal surface areaA. Newtott's r:i.s1az 1'd where massflux : the quantity of massthat passes acrossa per unit tirne (g/cmr/s. t7l 0 006 a32 3 06 Use thebestnr.sion 19'22 /rnl stalesthat 71vst flux : Dd' Mass dr ctz. 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 (:.r : 0) into a temperatures 0 t0 20 40 60 B0 12a lB0 280 400 35 26 15 7 a)829414946 l9.(g/m3).PROBLEMS 469 19. l06 g/cm3 the total depth (rr). Cm c. volts the flow rate Q (that is. ing voltagedatato estimate the inductance if a currentof 2 A over'400 milliseconrls..22) to colrlpute corlunction the massllux of pollutant out of the sediments and into the overlying walers .6 c m 2 / s )F sediments. r (N/m:) in LlL aI de yo nfa m i c v i s c o s i4 ty Assumeavalu :1.27 gxr. tll/ ldt) fbr eachtirne to the Calculate order of /rI .23 fne fbllowing data was collectedwhen a large oil tanker was loading: /. The averageconcentlationof a substance thatvarieswith depth.2tt 15srare P19.e diffir. of of the body ("C).2 c. I : current(A).m/s 0 0. T. . 5 2 x 1 0 .26 p. 106 borrels (i1 henrys: where V. Determine the voltage drop as a functionof time from the fbllowing datafor an inductance ol4 H./ : volur. and I: time (s). is passerJ thloughthe inductor t.22) whereZ: e. Determinethe averageconcentralionbasedon the following data: 16 4 B 12 O z. 19.h 0 u. usethe followlaw 1Prob. L: indtrctance I H : I V.: r. : tlepth (nr) as nreasured rvhere I. andl:: a propoflionality conrnedium the surrounding /aw stant(per minute). . canbe computecl by integlatron: pi711'.r : 0 at the sedimentwater interfaceancl downward): increases r. 1 . lo6m3 I8175 5 105I I 9635 Q3927 00000 lo. c : concentration and . min I'.1 B5 5. of cooling pressed as JT dT klT Tul I. s/A). how much pollutantu'ould be transported into the lakeover a year's time? 19.1.t U If theflux at x : 0 is 60 W/mr.4 on Faraday's 19.
67 5 06 282. have occured if thc tubc had a constant radius eoual to 7P . as in If thc plate's dimensionsare 200 cm long and 50 cm wide.33 Tllg specificheat at constantpressure q..and (b) the heattransf'er in watts. reacti (minevalu logar lo Thert log(slope meth. r ' r ( 1 ' ). K)l of an ideal gas is relatedto enthalpyby dh . 19.s. dT 19. K). the temperature of the ball changes.4 T. min r. andemploy lir"rear regression to evaluate fr. : 20'C.30 p61 fluid tlorv over a surface.K 0 900 I 480 35 270 dioxi deter lated and < T'K 2t0 FIGURE PI9. r.lt/ ') averagc the radius. The data was takcn lbr a real fluid.\ I49 t2 099 475 4. The tbllowing measurements mal to the surfacc are whcre ft : enthalpy (kJ/kg).L was generated polynomial. kll(kmol 300 4AO 500 600 82 BBB Il2 136 136933 157744 1000 210450 I03 a78 4.K) T. follor urea: t.028J/(s ..005N . I atm to 50 atm). and I : absolute lemperature (K).. where p = pressureiN/rn:).3I The pressure gradientfor larninarflow through a constantradiustube is given by dn d.31 El(m: 0t 5 t0 20 25 30 4A 45 50 224 22 r35 tl 090 O68 o6t a54 250 47 2. I 9. otm r=350K r=400K T=450K difl'erentiation to dcterminen. If a metal ball heatedto 80 'C is dropped into water that is held constant at f.. (a) deterrnine the flux at thesurface.the heat l]ux to the surface can be computedwith Fourier's law: . mi c. ml 19.28 the ditlcrencein thc tempcratures ol the body and of thc surrounding medium. fr : dynamicriscosity{N.32 the following data for the specificheatof benzene V.mm 2 135 1.3 react reacti 8uQ ft14 d.7. and I : 0.145 300 t5 24t 20 2t7 25 2A7 Utilize nunrericaldiff'erentiation to determinedTldr ateach value of tirnc..470 NUMERICAL DIFFERENTIATION madetbr air flowing over a flat plate where.rp H: I ( \dT/pl (c) Dctelminc the averageReynoldsnuntberfor thetube to Ju \ verit'y that flow is truly laminar (Re : pu D I 1t < 21N whercu: velocity).y : distancenor(m). K Cr. centerline 0 = flow (mr/s)anclr: radius(m). Plot dT f dt versw T .kl/lkmol. "c 0 80 5 r0 .5 I ).34 l6 142 2 dcscribcclbelow. (a) Detcrminc the pressuredrop for a l0cm length tuh = p= for a viscous liquid (p : 0. s/mr.7 462 700 800 900 ' K) I Z5 036 ) 89 273 20A 9n 19.density I x I03 kg/mr) with a flow of l0 x 106 m3/s andthe fbllowing varying radii along its length: wher..r': distance norrnal to thc surface: Jr clll T.Note that I J = 1W.5 523 27 I 5. The fbllowing enthalpiesare providedfor carbon . Time.cm O 2 4 5 710 6 19'29 Tl1senthalpyof a real gas is a function of pressure as l58 r. Irr .r ! a 19. K C.Usenumerical with an /rthorder P. r : distancealongthetube\ (m).s/nrr. tJ/(kg. Estimatethe enthalpyof the f'luid at zl00K and 50 atm (evaluate (b) Comparcyour result with the pressure drop thatwould thc integral frorn 0.
9994 and are 12. n : reactionorder (dimensionless). o : oxygenconcentration (g/mr). K) fbr eachof the tabulated temperatures.rlcr Io g \ { . m g / L 1 o 7 .34 An nthorderrate law is often usedto model chemical of a single reactions that solely dependon the concentration reactant: dc FIGURE PI9.35The sedirnent oxygen demand [SOD in units of g/1mr. I : time (rnin).After carefullyintroducinga layer of distilled.mole 0 A750 where11: the depthof water (m).769 Sediments I I 19.35).11 is an important parametel in determining the .01 471 Lid Water H T.59 2 57 r r s 0 3 3 0 . Use the dilferential methodand linear regressionto deternrinet and n for the following data fbr the conversionof arnmoniumcyanateto urea: l. a plot of the Therefore. The di.445 42.kt/kmol 754 29 629 800 32. l l 4. log(dcldf) versuslog c should yield a straightline with a slopeof n and an intercept of logfr.223 t . This involves applying a logarithmic transformto the rate law to yield.and t: time (d).K h. 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. min c. oxygenated water abovethe sedinrents. the containeris covA stirel is usedto rnix the water eredto preventgas transfer.l m. and ft : reaction rate (min I moler "). r'espectively oxygen I and I 5.?log( if the nthorder rate law holds. / . P I 9.PROBLEMS (COr) at severaltemperatures.179 1000 900 37. Note that the atomic weights of carbon g/mol. Based on the following data and H : O.and an oxygen probetracks how the water's oxygen concentrationdecreases over time. d. dioxide Use these valuesto determine the specificheat in J/(kg .11is measured by placing a sediment core in a cylindrical conniner (Fig. d 0 0125 425 0375 05 0625 475 o . gently. The SOD can then be computedas SOD: I1: do at wherec : concentration (mole).fferentialmethod can be used to evaluate the parametersk and rt. /} = l o g k dt L. . 0 3 19.35 dl : *kc" dissolved oxygencontentof a naturalwater.
&
t
f,
Ordinory Differentiol
l
Equolrons
r'
6.r ovERvrEw
The fundamental laws of physics. mechanics,electricity, and thermodynamicsare usually' basedon empirical observationsthat explain variations in physical properties and statesof i' systems.Rather than describing the stateof physical systemsdirectly, the laws are usually_ .r" *F '* , couched in terms of spatial and temporal changes. These laws define mechallt** .oj ., momentum, differenliol change.When combined with continuity laws for enorgy,mass,or resultsin matheequations of thesedifferential result.Subsequent integration equations the spatialand temporalstateof a syslemin termsof energy, , " matical functionsthat describe '" As in Fig. FT6,1, the integrationcan be implementedanalyti* mass,or velocity variations. l,, cally with calculus or numericalli with the computer jumper introproblem The freefallingbungee of a of the derivation ducedin Chap. I is an example ditferential equationfrom a fundamentallaw. Recall that Newton's second law was used to develop an ODE describingthe rate of changeof velocity of a'" jumper: falling bungee du c,t t
i:r:u
(PT6'l)
constant.rr is the mass, where g is the gravitationa.l which are and c, is a drag coefficient.Suchequations, of an unknown functionand its derivatives. composed are called dffirential equntions,,:TheY ate sometimes referred to as rate equationsbecausethey expressthe rate of change of a variable as'a"'functionof variables and parameters. u In Eq. 1PT6.l), the quantitybeingdifferentiated is called the dependent variable. The quantity with respectto which u is differentiated t is calledthe independent variable. When the function involves one independentvariable, the equation is called an ordinary
' .:..1.r.. ,.::
473
, r,i:,ir.,.,, t.,..
474
PART 6 ORDINARY DIFFERENTIAL EQUATIONS
Physicallaw
F=ma
f
I I
du ^ 6
, aIm
I
I
I
cJ..2
ODE
V Solution
I
Analytical (calculus)
n
\
Numerical (computer)
/
u  . / " " ' t a n h[ , / r y r ]
\ t,j \V /n /
l o m / l o ^ ,
\
u r + r= u i + ( s  ' ! r ' ) n,
FIGURE PT6.I
iIe seouenre i n r h ed e u e l o p r r e n ot events o' ^ 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 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 are also classifiedas to their orderFor example.Eq. (PT6.l) is called afirstorder equatio,? because the highestderivativeis a lirst derivative.Asecondorder equationwould includea second derivative. For example, the equationdescribing the positionx of an unforcedmassspring systemwith damping is the secondorder equation: m *t. dt'
d'x
,)
dx
,
dt
*Ax:0
(PT6.2)
where ,fl is mass,c is a damping coefficient,and k is a spring constant.Similarly, an nthorder equationwould include an nth derivative. Higherorderdifferentialequations can be reducedto a systemof firstorderequations. This is accomplished by defining the first derivativeof the dependent 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 to yield
(l t' o'l
(PT6.3)
dt
dtz
(PT6.4)
Equations(PT6.3)and (PT6.4)can be substituted into Eq. (PT6.2)to convertit into a firstorder equation: du m7tcu+kr:0
(PT6.s)
6.I OVERVIEW
As a final step,we can express Eqs.(PT6.3)and (PT6.5)as rateequations: dx
l
475
ut
(Pr6.6)
ck tn nt
tlu
dT
'
(PT6.t)
Thus, Eqs. (PT6.6) and (PT6.7) are a pair of firstorder equationsthat arreequivalent to the original secondorder equation (Eq. PT6.2). Becauseother nthorder ciiff'erential equations can be similarlyreduced, this part of our book fbcuses on the solutionof firstorderequations. A solutionof an ordinarydifl'erential equationis a specificfunctionof the independent variable and parameters that satisfiesthe original differential equation.To illustrate this concept,let us startwith a simple fourthorderpolynomial, . t ,:  0 . 5 1 4 + 4 . r 3  l 0 x 2 + 8 . 5 . r* I Now. if we differentiate Eq. (PT6.8).we obtainan ODE:
(Pr6.8)
d.r  _2r3 * l2,rr  20r * 8.5 dx
(Pr6.e)
This equationalso describes the behaviorof the polynomial,but in a mannerdifferentfrom Eq. (PT6.8). Rather than explicitly representing the values of ,r for each vahre of .r, Eq. (PT6.9)givesthe rateof change of ywith respect to.r (thatis, the slope)at everyvallle of .r. FigurePT6.2showsboth the functionand the deriv'atile plottedverslls.{. Noticehow the zero valuesof the derivativescorrespond to the point at which the original function is flatthat is, where it hiis a zero slope.Also, the maximum absolutevaluesof the derivativesale at the endsofthe intervalwherethe slopes ofthe functionaregreatest. we can determine Altlroueh, as just denronstrated, a differential equationgiven the originalfunction, the objecthereis tt.r determine the originalfunctiongiventhedifferential eqr.ration. The original function then represents the solution. Withoutcomputers, For example, with calculus. ODEs areusuallysolvedanalytically Eq. (PT6.9)could be multiplied by dx andintegrated to yield
t:
lrztt
 2ox* 8.5) r/.r1 l2.r'r
(PT6.I 0)
The righrhand sideof this equationis calledan indefiniteintegraL because the limits of integrationare unspecified. previously This is in contrastto the definite integralsdiscussed in PartFive [compare Eq. (PT6.l0) with Eq. (17.5)]. An analyticalsolutionfor Eq. (PT6.l0) is obtained if the irrdefinite integralcan be evaluatedexactlyin equation fblnr. For this sinrple case, it is possible to do this witlr the result: r ' :  0 . 5 1 4 * 4 . t 1  1 0 . 1+ 2 8 . 5 . r* C
(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. In fact,it is but one ofan
476
PART 6 ORDINARY DIFFERENTIAL EAUATIONS
FIGURE PT6.2
 O 5 r a * 4 x 3 l O ; + B 5 j r + I Plots o f ( o )\ ' v e r s u s r o n d l b )d  r . / r h versusrforthefunctionl,:
infinite numberof possible (con'esponding functions to an infinitenumberof possible values of C) that satisfythe differentialequation.For example,Fig. PT6.3 showssix possible functions that satisfyEq. (PT6.l I ). Therefore,to specifythe solutioncompletely, a ditTerential equationis usuallyaccompanied by auxiliary conditions.For firstorderODEs, a type of auxiliary condition called ln initial value is requiredto determirre the constant and obtain a uniquesolution.For example,the original diff'erentialequationcould be accompanied by the initial condition .l ) t o d e t e r m i n e 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 nto Eq. (PT6l C : l. Therefore,the unique solution that satisfiesboth the differentialequationand the s p e c i l ' i eid n i t i a lc o n d i t i o n is r': 0.5x1 f 4rr  l0rr r8.5r * I Thus,we have"pinneddown" Eq. (PT6.l1) by forcingit to passthroughthe initial condition, and in so doing, we havedeveloped a uniquesolutionto the ODE and havecornefull circle to the originalfunction[Eq. (PT6.8)]. Initial conditionsusually have very tangibleinterpretations for diff'erential equations derived from physical problem settings. For example.in the bungeejumper problem, the
ORGANIZATION 6.2 PART
477
C=3
( =Z
C=1 C=0
PT6.3 FIGURE  2Or* 8.5.Eoch of 2r'r to o differenl Sixpossibe solutions fortheiniegrol conforms I l2x2
C. vo ue of lhe constont of inteqrolion
initial conditionwas reflectiveof the physicallact that at time zero the verticalvelocity jumperhad already was zero.If the bungee beenin verticalnlotiorlat time zero,the solufor this initial velocity. tion would havebeenmodified to accoLlllt When dealingwith an nthorderdifferentialequation,n conditionsare requiredto obtain a unique solution.If all conditionsare specifiedat the samevalue of the independent variable (fbr example,at,v or t : 0), then the problem is called an initialvalueproblem. problemswhere specification of conditionsoccursat This is in contlastto boundaryvalue valuesof the independent variable.Chapters 20 and 2l will fbcus on initialvalue difTerent problems. problems arecoveledtn Chap.22. Boundaryvalue
6.2
PARTORGANIZATION
Chapter 20 is devotedto onestepmethodsfor solving initialvalue ODEs. As the name suggests, onestep methodscomputea futurepredictiony;a1, basedonly on information at a single point r; and no other previous information.This is in contrastto multistepapproachesthat useinlbrmation from several previouspointsas the basisfor extrapolating to a newvalue. presented With all but a rninorexception, the onestep methods in Chap.20 belongto wh:rt are called, RungeKuttatechni4lle.s. Although the chapterrnight havebeenorganized to inaroundthis theoretical notion, we haveoptedfor a more graphical.intuitive approach Thus, we begin the chapterwrth Euler's ntethod,which has a very troduce the rnethods. graphicalinterpretation. we have alreadyintroduced straightforward In addition,because Euler's methodin Chap. l, our emphasis hereis on quantifyingits truncationeror and describineits stabilitv.
478
PART 6 ORDINARY DIFFERENTIAL EQUATIONS
Next, we usevisually orientedarguments to developtwo improvedversionsof Euler's nretlrodtlre Heun and the midpoint techniques. After this introduction,we formally develop the conceptof RungeKutta(or RK) approaches and demonstrate how the foregoing techniques are actuallyfirst and secondorder RK methods.This is followed by a discussion of the higherorderRK fbrmulations that are fi'equentlyused fbr engineeringand scientific problem solving. In addition, we cover the applicationof onestepmethodsto .ty.r/erns oJ ODEs.Note that all the applications in Chap.20 are limited to cases with a fixed stepsize. ln Chop. 21, we cover more advanced approaches for solving initialvalueproblems. 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. As mentionedabove,thesealgorithmsretain infbrn'ration of previousstepsto more ell'ectivelycapturethe trajectoryof the solution.They alsoyield the truncationerror estimates that canbe usedto implementstepsize control.We describea sinrplemethodthe nonselfstarting Heun methodto introducethe essential features of the multistepapproaches. Finally, the chapterends with a descriptionof ,stiffODEs. Theseare both individual and systems of ODEs thathaveboth fastand slow components to their solution. As a consequence, they require specialsolution approaches. We introducethe idea of an implicit solution techniqueas one commonly usedremedy.We also describeMMLAB's builtin functionsfor solving stiff ODEs. ln Cha1t.22, we focus on two approaches for obtaining solutionsto boundary'value problems:theshootingandJinitedffirencemethods. Aside from demonstrating how these techniques are implemented, we illustratehow they handleclerivatiye boundorvconditions and nonlinear ODEs.
ti'.iili,
'l
''; 'j:''
':
'ii.
*l\,
l:io ":i'
InitiolVolue Problems
CHAPTER OBJECTIVES
The primary objectiveof this chapteris to introduceyou to solving initialvalue problemsfbr ODEs (ordinarydifferentialequations). Specificobjectivesand topics coveredare t . Understanding the meaningof local and global truncationerrorsand their relationshipto stepsize for onestep methodsfor solving ODEs. Knowing how to implementthe following RungeKutta(RK) methodsfor a singleODE: Euler Heun Midpoint RK Fourtholder Knowing how to iteratethe coffectorof Heun'smethod. fbr systems Knowing how to irnplement the followingRungeKutta methods of ODEs: Euler RK Fourthorder
. .
YOU'VE GOT A PROBLEM
e startedthis book with the problemof simulatingthe velocity of a freefalling jumper.This problemamounted to formulatingand solving an ordinary bungee diffbrentialequation,the topic of this chapter. Now let's return to this problem and make it more interesting by computingwhat happens when thejumper reaches the end of the bunseecord.
480
INITIALVALU E PROBLEMS
To do this, we should recognizethat the jumper will experience dedifferentforces pendingon whetherthe cord is slack or stretched. If it is slack,the situationis thatoffree fall where the only forces are gravity and drag. However, because the jumpel cannow move up as well as down, the sign of the dragforce mustbe nrodifiedso that it always tends to retardvelocity,
du g  srgn(r,)?u2 dt
(20.1a)
where u is velocity (m/s), t is time (s), g is the acceleration due to gravity (9.81m/s'),c7is the drag coefficient(kg/m), and m is mass(kg). The .signum a  I or Jilndion.t sign,returns its argument is negative a I depending on whether or positive, 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 of thisforce. 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 louation: e du
al :,9 
) Slgn(u)U tn
c,t
(r ilt
k

I.l 
U m
y
(20,\b)
(N/m), x is verticaldistance wherek is the cord's springconstant measured downward from jump platform (m). L is the lengthof the unstretched the bungee cord (m), and 7 is a damp(N . s/m). eningcoefticient (x > L), the springforce BecauseEq. 120.1b) only holds when the cord is stretched will always be negative.That is, it will always act to pull the jumper back up. The dampening force increase.s in magnitudeas the jumper's velocity increases t0 and alwaysacts slow the jumper down. If we want to simulatethejumper's velocity,we would initially solveEq. (20.1a) until the cord was fully extended. Then, we could switch to Eq. (20.1b) for periodsthatthecord is stretched.Although this is fairly straightforward,it rneans that knowledgeof the jumper's position is required.This can be done by formularting anotherdifferential equation lbr distance: dx
dt
(20.2)
jurrrper'svelocity amountsto soiving two ordinary Thus, solving tbr the bungee differentialequations whereone of the equations takesdifferentforms depending on thevalue
I Some computerlanguages represent the signum functionas sgr (x). As represented the here,MATLAB uses n o m e n c l a t u rs ei o n 1 x ) .
20.2 EULER'S METHOD
48r
of one of the dependent variables. 20 and21 exploremethodsfor solving this and Chapters similar problems involvingODEs.
20.t ovERvrEw
This chapteris devotedto solvine ordinarv diflerentialequations of the form
d.y
dr
: I (l' Y)
(20.3)
In Chap. l, we developed a nnmericalmethodto solve suchan equationfor the velocity of jumper. Recall that the methodwas of the generalform the freefalling bungee New value : old value* slope x stepsize terms, or, in mathematical
.Vi+r: )'i*Qh
(20.4)
where the slope@is calledan incrcnzent Junction.Accordingto this equation,the slopeestimate of @is usedto extrapolate liom an old value yrto a new value y;11 over a distance /2.This fornrula can be appliedstep by stepto traceout the trajectoryof the solution into ntethods the future. Suchapproaches are calleduteste1t because the value of the increment point They function is basedon information at a single i. are also refelred to as Rwrgewho 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 to a new value. We will describemultistep rnethods briefly in Chap.21. All onestep methodscan be expressed in the generalform of Eq. (20.4),with the only differencebeing the mannerin which the slopeis estimated. The sirnplestapproachis to the slopein the fbrm of the first derivativeat /;. ln use the differentialequationto estimate other words, the slopeat the beginningof the interval is taken as an approximationof the averageslope over the whole interval.This approach, called Euler's method,is discussed next. This is followed by other onestepmethodsthat employ alternativeslope estimates predictions. that result in more accurate
2O.2 EULER's METHOD
The first derivativeprovidesa direct estimateof the slopeat /i (Fig. 20.I ): q : f ftr,),i) where /(t;,1;) is the differentialequationevaluated at /; and _r'i. This estimatecan be subinto Eq. (20.1t: stituted
)'i+r: t"i* fGi,v"t)h
(20.s)
or pointslopemethod). This formula is refened to as Euler's rnethod(or the EulerCauchy A new value of "is predictedusing the slope (equalto the first derivativeat the original valtte of r) to extrapolate linearly over the stepsize h (Fig.20.1).
482
INITIALVALUE PROBTEMS
FIGURE 20.I Euler s meihod.
EXAMPLE 20..l
Euler's Method  4ro s'  0.5.r' ProblemStotement. Use Euler'smethodto integrate from r : 0 to 4 !' : : y with a stepsizeof 1. The initial conditionat / 0 is 2. Note that the exactsolution can be determined analvticallvas ^' * e o5r')12no5r ) : r rlrtrut' Solution. Equation(20.5)can be usedto implement Euler'smethod: : ) , ( 0 )+ J ( 0 , 2 ) ( l ) ,],'(l) where 1,(0) :2 andthe slopeestimateat / : 0 rs . f ( 0 , 2 ) : 4 e o 0 ' 5 ( 2 ) : 3 Therefbre, ](l):2+3(l):5 The true solutionat r : r :
4
1 is
,,
r o * rt tt r (eust  e 05(l)) +2e05n\:6.19463
Thus.the percent relative erroris t6.1946351 F, : ll x 1007o: l9.28Vo 6.19463 l For the secondstep: )(2) : y(l) * /(l. s)(l) t 0 . 5 r 5 t l 0 ) : :  5+ f 4 e 0 8 ( r
11.40216
METHOD 20.2 EULER'S
TABLE20.l of the integrol volues of trueond numericol Comporison  t 0 . 5 , vw hoty:2 q1 n l o n d i t i ot , i t ht h e i n i t i o c o f . r ": 4 e ( t 8 Euler's using computed were volues r : O.Thenumericol I ' of size steP with o method
Jt"u"
483
JEur".
lerl (%)
0 I 2 3
4
2 00000 6 19463 I4.84392 3 36 7 7 l 7 75 . 3 3 8 9 6
2 00000 5 00000 t 1.442t6 25 51321 568493l
1 92 8 2319 24 24 24.54
T r u es o l u t i o n
20.2 FIGURE o! me,tf Eulers using solution wiiho numericol solurion of fhe.rrue Comporison * ?t 2 o clo n d i l i o!,]^"I59.1 nt / : O i s ThJlnitio
i",\;, 6 i" f,.r ;:O;4 of I o size w 1 ho s i e p 1':
and, therefore,the true percentrelative error is The true solution at t : 2.0 is 14.84392 and the resultscompiledin Table20' 1 and Fig' 20'2' 23.l97o.Thecomputationis repeated, the generaltrend of the true solution,the error captures Note that althoughthe computation in the next section,this error can be reducedby using a As discussed 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. by the limited numbersof significantdigits that canbe retained 2. Roundnfi'enofscaused by a comPuter. effor The truncationerrors are composedof two parts.The first is a local truncation is The second step' a single over question that resultsfrom an applicationof the methodin the during produced a propagatetl truncation error that results from the approximations
the comparison indicatesthat a truncationenor occursbecause we approxirnate the true solutionusinga finite numberof termsfrom the Taylor series. .l')h + where O(h'+l) specifiesthat the local truncationetror is proportional to the stepsize raisedto the (n * l)th power.8).ql where E.6)and (20. ' j::/r"+l ( t z* 1 ) ! .r (20.8)yields E .5). (20.yi)/r. it is proportionalto the stepsize .2 * .5)from Eq./. To do this. (20. + Ii t20.+ o ( h . the function describingthe behaviorof yhas continuous derivatives. (20.. l0) or Eo : O(h2) (.the tluncationerror in Euler's methodis attributable to the remaining terms in the Taylor seriesexpansionthat were not included in Eq. it can be represented by a Taylor seriesexpansionabout a starting value (/. By cornparingEqs..3). to /...and the result is often represented as F ^  .[ 1 + P h 2+ ..3)into Eqs.484 INITIALVALUE PROBLEMS previous steps.5) and (20.h ' . According to Eq.ll). (4. If the solutionthat is..6) where/u : ti+t . realizethatthe differential equation being integratedwill be of the general fbrm of Eq.1) where E.yr). An alternativeform can be developedby substituting Eq.7) where f lies somewhere in the interval from /.respectively. (20. as in [recallEq.yr+r: )'i t f (ti.\r. 11. (20..7)to yield f'(tj'Yi) . Insight into the magnitudeand properties of the truncationerror can be gainedby deriving Euler's methoddirectly from the Taylor seriesexpansion. Additionally.20. and Ra : the remainderterm. (20. Subtracting Eq.The sum of the two is the total enor.(20. (20. .0t*l)191 (20..the higherorder terms in Eq. and t and y are the independent and the dependent variables. .f ).For example.9) are usually negligible. We thustruncate..For sufficientlysmall lz. a part of thetrue solution. : the approximate local truncationeffor.It can also be demonstratedthat the global truncationerror is O(/r)that is. definedas R. (20.) . we seethat the local error is proportionalto the square of the step size and the first derivative of the differential equation." *4n nl (20. where dyldt : y'. or leaveout.it can be seenthat Euler's rnethodconesponds to the Taylor seriesup to and including the term /(r.. : the true local truncationerror. It is referredto as the gbbal truncation error.13)]: t ln ]i+r: ti *yitt +*n'+ :! " +'r'i' 7.
ah is called an cunplifictttion The parenthetical foctor. ffrrr. the solutionwill grow in an unbounded fashion. . 1969). Further.e that we useEuler's methodto solve the sameproblernnumerically: Now suppose . That is. is anotherimportantconsideration The stability of a solutionrrrethod that must be conwhen solving ODEs. Euler's method is referredto as a firstorder method. tf.y i + l: l i + . This latter conclusionmakesintuitive sense Euler's methodusesstraightline because segments to approximatethe solution.t l 0 .we learnedthat the truncationerror of Euler's methoddepends on This is an accuracy the step size in a predictableway basedon the Taylor series.the numerical method. Euler's methodis said to be conditionall.2 EULER'S METHOD (Carnahan et al. 2.20.lyil + oo as i + oo. Insight into the step size required for stability can be exanined by studying a very simpleODE: dt' ay ." *fution startsat 1. at If r(0) .v stable. SrrchODEs are called illconditioned... Suhstitutin E gq . 485 The global error can be reducedby decreasing the stepsize.e.12) and asymptoticallyapproaches zero.So clearly. It should also be noted that this generalpatternholds for the higherolderonestep methodsdescribed in the following pages.2.) ' i + r : 1 . I 2 t g i v e s Yr+l: ji \'ih or . because for a straightline the second derivativewould be zero.the stability depends on the step size /r.That is.)0. issue. If its absolutevalue is greaterthan unity.Theseobservations lead to someusefulconclusions: 1. d)'. A nurnericalsolution is said to be unstableif enors grow sidered exponentiallyfor a problem for which there is a boundedsolution. Hence.a h ) (20. an nthordermethodwill yield perfect polynomial. i ( l. 20. Basedon this analysis.the local truncation resultsif the underlying solution is an rrthorder errorwill be O (h"+1) and the globalerror O (h').and the stepsize. Note that thereare certainODEs where errorsalways grow regardless of the method. The method wiil provide errorfreepredictionsif the underlying function (i.13) quantity 1 . if /z > 2la.2 Stobility of Euler'sMethod ln the precedingsection. .The stability of a particular applicationcan dependon three factors: the differential equation. the solution of the differential equation)is linear. calculuscan be usedto determinethe solutionas V : ltr€ '' (20.
end n = length(t). r s p o n . y ] . Now. let's developa rnore general. y for i = 1:nl ?implement Euler's method = y(i) y(i+1) + dydt(t(i).allpurposealgorithm.tf ' lLf>ri). n = n+1. . tspan. necessary.3 showsan Mfile that usesEuler's methodto computevaluesof thedependentvariabley over a rangeof valuesof theindependent variablet .vector of independent variable z y = vector of solution ior dependent variable if nargin. .vararqini:])*(t(i+1)t(i) end ). e u . .i o n It. p l .3 MATLAB Mfile Function: eulode We have alreadydevelopeda simple Mfile to implementEuler's method for the falling jumper problem in Chap. . h . y 0 .6'r.error = if q^ ri if t (ti:h:tf)'.We will retum to the topic when we discussstilTsystems in Chap.ower'l.l o d e r d y d t .6.y0. Figure 20.rarargin) f u n c r .486 INITIAL.4 z z if least 4 inpuu argumens reqL rq. {'Jpper lrmit musl be grearer Lhar' .VALUE PROBLEMS (a) bothrepreInaccuracyand instability are often confused. 20.end tspan(2).y] B eulode: EUIET ODE SOIVCT z [ r .an inaccurate methodcanbe very stable. The nameof thefunction holding the righthand side of the differential equationis passedinto the function as the FIGURE 20.3 AnMJile to implemeni Euler's method = eulode(dydt. . r ee F f i c i e n . .For example.However. that this function usedEuler's bungee nethod to compute the velocity after a given time of tiee fall. p l . . rh)l 1a". This is probablybecause sentsituationswhere the numericalsolutionbreaksdown and (b) both are affected by step size.4.p2.21. .2. = additional parameters used by dydt % output : z t . Recallfrom Section3. add an additional value of t ^^ac rIOm L ri tO Ct {'ar r (n)<tf t(n+1) = tf .elror = tspan(1).3. 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.Ir.they are distinct problems.y(i)..h. % p r e a L l o c a t e y L o i m p r o ' . end y = y0'ones{n. : method to integrate z uses Euler's an ODE % rnput: 96 dydt = name of the Mfile Chat evaluates the ODE z tspan = lti.
Also noticehow the time stepis automatues of the independent and dependent values ically calculated between adjacent in the vectort. 20.Yl ) with the result(compare with Table20. a vector of the dependent with n values of the initial condition to improve efficiency.0000 2.In Mfile is the only option.If this occurs.because they have very straightwe will presentthem prior to their forrnal derivationas forward graphicalinterpretations. The initial and llnal valuesof the desiredrangeof the independent variable is passed as a vector tspan. suchinstances.3 IMPROVEMENTS METHOD OF EULER'S 487 variabledydt.4422 25.IETHOD IMPROVEMENTS OF EULER'S A fundamentalsourceof error in Euler's methodis that the derivativeat tlre beginningof the interval is assumed to apply acrossthe entire interval.As will be dernonstrated both modifications(as well as Euler's methoditself) actuallybelongto a largerclassof solution techniques calledRungeKuttamethods. Euler'smethod(Eq.0000 1r. Two simple modificationsare in Section20. creatinga separate 20.For example. In addition. fbr the ODE from Example20.4400 3. .3 TI.However.5"yt The solutioncan then be generated as >> disP(tt.1): 0 1. availableto help circumventthis shorlcoming.In the eventthat the stepsizeis not evenly divisible into the range.0000 5.20.5)is implemented y(i+1) end = y(i) + dydt(t(i). V ' ) 4*exp (0. The initial value and the desiredstepsizearepassed asy0 and h.First.the final value is addedto r so that the seliesspansthe completerange.The length of the r vector is detervariabley is preallocated mined as n. the last value will fall short of the final value of the range. 3*t) o.4.000a 4.8493 Although using an anonymousfunction is feasiblefor the presentcase. the differentialequationcan The ODE being solvedcan be setup in several be definedasan anonymous funcfionobject.v(i))*(t(i+l)t(i).I : > > t i 1 ' c i t = G( t . Notice how a function is usedto generate a value for the derivativeat the appropriate valvariables.var). by a simpleloop: At this point.4132 55. based on the difference ways.0000 2. RunseKuttamethods. variable The lunction first generates a vector t over the desired rangeofthe dependent using an incrementof t'r. respectively.there will be more complex problemswherethe definition of the ODE requiresseverallines of code.
3.15)is calledaprcdictor equatiott. y i l ' . the two slopes[Eqs. .14)and (20.9+r) .) /l (20.1* c.r5) For the standard Euler methodwe would stopat this point.in Heun'srnethod the r'. ' 'l ) )rrl:)'ir.calledHeun'smethod.a l c u l a t e c l i n E q .1 Heun's Method One methodto improve the estimateof the slopeinvolves the determination of two derivativesfor the intervalone at the beginningand anotherat the end.y i) .(20. YP* r) : f(ti.is depictedgraphicallyin Fig. T h i s i s why we havedistinguished it with a superscript 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 . . ( 2 0 .to li+r using Euler's method: '. r7) which is called a corrector eguation. However.) )'1+r (20. 1 5 ) i s n o t t h ea t 'n in sa wl e r .f (ti.vi) is usedto extrapolate linearly to y111 : ))+r:litJ'Gi.4.f(ri* r. It providesan estimate that allowstlrecalculation of a slopeat the end of the interval: : . Equation(20.Yi)h (20.l'j!. l6) Thus.the slopeat the besinnins of an interryal )"i : .488 INITIALVALUE PROBLEMS 2O.f (rt*t.f(rr+r.t . *. r4) (20.Thisapproach. . FIGURE 20.r'r) 2 This averageslope l s then used to extrapolatelinearly frorn 1'. 20.. _ /(r.16)] can be combinedto obtainan average slope for the interval. . f ( r . l Q i .r'.The two derivatives are then averaged to obtain an improved estimateof the slopefor the entire interval.4 Grophicol depiciiono[ Heun'smelhod (o) Predictor ond (b) correctof = . Recall that in Euler's method.
conthat the iterative processdoes not necessarily respectively. is depicted The process of v. First. an improvedestimate in previoussections of the book.4b): y.the slopeat (16. r9) (for.it can be appliedin Eq.3 IMPROVEMENTS 489 FIGURE 20. r : 2 . 20.rg)is calculated ! ' o : 4 e " .f.0 .0. it can be expressed as concisely (Fig.19)haslial on both sidesof the eclual Note that because can be usedlepeatedlyto provide That is. * .OFEULER'S METHOD 20. r8) h l'(rt. E m p l o ya s t o p fromt :0 to4 witha step ping criterion of 0. demonstrated 20. .5.o*r: y j " * y/+r : l'i' I f (ti.20.:1.the predictor v9:2*3(t):5 .4a): Preclictor Corector (Fig. Use Heun's methocl .2""'m) sign. 1 * ' ' ) (20. 5 ( 2 ): 3 is usedto compute a valueat 1. 1 ' . in Fig. as Solution.0: Then.As just derived. The Heun method is a pretlic'tor. (20.(orrectorapproach.]'/ 4e0 .yi)h (20.v/*1 are the resulttiom the prior and the presentiterationof the corrector.2 Heun's EXAMPLE Method 8' with iterationto integrate Problem Stotement.5y s 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 .yi)+ / ( t .lt should be understood with a finite truncationerror. a termiAs with similar iterativemethodsdiscussed of the correctoris frrovidedby nation criterion for conversence tc t t#l"ouo" where1.as verge on the true answerbut will convergeon an estimate in the following example. .00001c/o to terminirtetlre correctoriterations.r and. an old estimate an iterativefashionas indicated. 20.5 of Heun'smethodto obloin cn improved of iteroting fhe corrector Grophicol representotion estimcte.11.
101082 ^ L This result can then be substituted into the corrector[Eq. 7 0 1 0 8 2 (: which represents a true percentrelative error of 8. 5 ( 6 . 4 0 2 1 6 1 which can be combined with the initial slope to yield an averageslope over the interval f r o n rf : 0 t o l : r': 3 + 6.uo Jtiur".70 r082 1 6.Thus.The true velluein Table 20.2758 1 l c . l ' ): 4 e 0 8 ( r ) .l l 6.0 .tB 994 la 46 t4. i : 2 + 4 . Without lterqlion Jt.18c/c.6. Now.1.'l t r t D.v.3 andan approximate enor 0f il .l . qnd with iierqtion TheHeunmethod wos imolemented bothwithout o[ thecorrector. TABTE20.1758 11  I :6. 2 7 5 8I1 whichrepresents relative a truepercent I percent errorof 1. we can alsocomputean approximate erroras l670roR)sl l 'r " . 7 0 1 0 8 2 ) I : 6 . I 9463 l4 84392 3 3.4e0E 0 .VH"un le. i t ht h e i n i t i oc l onditiot nh o t . 6.At this point.19)to give '> t. to improve the estimatefor .62 0 I '2 3 4 200000 6.28a/o. r . (20. 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.2 With lterotion .y : 2 o t t : O ..0 .: l . we usethe value.2 showsthat it corresponds to a percentrelativeerror of 19. the Heun methodwithout iterationof the correctorreduces the absolutevalue of the error by a f'actorof about2.l ('hl le.70r082 l^'"" Now the estimateof r'1 can be refined by substitutingthe new result back into the righthand sideof Eq. (20.102164 :4. 6 7 7 l7 75 33896 200000 5 00000 t 140216 2 5. i : .701082 l) t .490 INITIALVALUE PROBLEMS Comporison of trueond numericol volues of the integrol ol tr .l x 6.4as comparedwith Euler's method..l("/"1 l9 28 2319 24 24 2454 2 00000 6 7010B I6 31978 37 19925 83 33777 l€.T h en u m e r i c v oo l lues were computed usingthe Euler ond Heunmethods with o stepsize of 1.19)]to give the prediction at t l.. 5 1 'w .r'f'to predict the slopeat the end of the interval t . 5 ( 5 ): 6 .3 2 ' t 14"o .' r :.116(/t 1001 . / ( x r .1("/"1 B.
\.ltri)f [(ri. In the previousexample.20. l l ) l i s d e f i n e d as '' [" f ( t i ) + J Q i + r )..ll)].03voandan approximate error of 1.3 IMPROVEMENTS OF EULER'S METHOD The next iterationgives )l 491 )^  Lr . frndt (20. The connection between the two methods can be formally demonstrated by startingwith the ordinary differentialequation tl 1' : dt fut (20. ft'+t JI.22) .36087. 2 /.20) Notice the sirnilaritybetweenthe secondterrnon the righthandsideof Eq.r).which represents a rrue relative error of 2.t'.l8)] is not requiredand the corrector is appliedonly oncefbr eachireration.r . ' f ( r ) d t : 2 (20.25) . (20.'. ( l 7 .i+ / Ittlrlt (20. where the ODE is solely a function of the inclependent variable. (l7.2 showsresultsfor the remainderof the computafionalong with resultsfor Euler's methodand for the Heun methodwithout iterationof the corrector.the derivativeis a function of both the dependent variabley and the independent variable/.0.t J r. [(t\dt (20. I J dt:l f t.20)anc1 the trapezoidal rule [Eq. Insight into the local error of the Heun methodcan be gainedby recognizingthat it is relatedto the trapezoidal rule. ft'tl J. l : l'. The approxirnate error will keep droppingas the iterativeprocess convelges on a stable final result. + .666vc. For casessuchas polynomials. the result at r : I is 6.Table 20.24\ Now.At this point.isely as .2t) This equation can be solvedlbr.23) . the techniqueis expressed conc. the predictor step [Eq.r'by integration: f . . which yields . recall that the trapezoidal rule [Eq. For suchcases.687c. (20.275g1 3 a 4rrt'xrtt 1) I :6.t': . In this example..i+: ll.r'i. (20..after l2 iterationsthe approximateerror falls below the stoppingcriterion.5(6.382129 which represents a true error of 3.l'.
6 illustratesanothersimple modificationof Euler's method.29) (20.25)into Eq.(o) Predictor ond (b) corrector v = J(.28) FIGURE 20.2 The Midpoint Merhod Figure 20. (17. y  L Then. In acldition. 20. this predictedvalue is usedto calculatea slopeat the midpoint: i + r lz.20. r ' i + 1 .20).tr2 l ( t .26) which is equivalenttoEq. respectively.26) is a direct expression of the trapezoidal rule. decreasingthe step size decreases the error at a lasterrate than fbr Euler's rnethod. Thus.492 INITIAL VATUE PROBLEMS where/r : !i+1 .t (20. .:/ r ) ' i * J ' ( t . (20.20. Heun's methodis sometimes refemed t0 as the trapezoidal rule.14)l r J f " ( \t \r 1a ' ' I ' (20. Because Eq. . tl.y1.Called the nitlpoittt ntethod.For this reason.1 1 .24)yields .' i 1 1 1 2 ) . tr (20. )"t+r1 ti +t p) :.27 ) wheref is betweenti afidt.Therefbre.6 Grophico depictionof Heun'smethod.y af the miclpointof the interval(Fig. (. the local and global errors are o(h3) and o(lf).1i11 1 2). (20. (20.3.6a): . l(titl J/rt.re methodis secondorder because the second derivative of the ODE is zero when the true solution is a quaclratic.this techniqueusesEuler's method to predict a value of . : . the local truncation error is given by [r'ecall Eq.f (. Subsriruring Eq.4.
Recallfron Table 11.30).Usingthe nomenclature h e e x p r e s s e a d s it can r l t t t . .a iterativelyto improvethe solutionas was donewith Heun'smethod. 120.4 that the simplest NewtonCotes can as which is calied the nridpoint method. with the forward eror of O1h:1in comparison suchas F. where. r k t hI q u .) ' i * q r r k r l t ) k . Thus. f ' ( t . it utilizes a slopeestimate The midpoint methodis superiorto Euler's methodbecause prediction interval.t p z h .20.i* q . . .This slope is then usedto extrapolate r'r+t : )'i I f Q i* t . (20. the midpoint method gets its name from the underlyingintegrationformula on which it is based.34d) . Q : a r k tI a z k z where the a's are constants and the k's are k 1 : J ' Q .q. be rcpresented open integrationtbrmula. O Consequently.r1is the midpointof the interval(n.34b) (20. 20. .rh) (20.3. * p .4 RUNGE. the local and of Euler's method. As in our discussionof Heun's method.6b): interval.24)yields Eq.3:lc) (20. ' .t'ftia1P) I Jti Q0.the rnidpoint rnethodcan also be linked to NewtonCotes integration lbrmulas.. The incremerrt +'''' l a. r .t.4 RUNGEKUTTA RungeKutta (RK) methods achieve the accuracyof a Taylor series approachwithout Many variationsexist but all can be cast in requiring the calculationof higher derivatives.(20. : j ' ( t i . t ..4): the generirlized )r+r : )'i l. O(h2)..32) Substitutionof this formula into Eq.a)J'(r) (20. z k z h I * q.which has an en'orof approximation respectively. . D). *Ii q t t k t l t i q n k z h ) : : k .KUTTA METHODS 493 which is assumedto representa valid approximationof the averageslope for the entire linearly from /1 to /111(Fig. fbrm of Eq. (20.33) as a repl'esentative slope where@is called an incrcment function. t .29)has a local truncation (h).(b ..r0) Observethat because I is not on both sides. just as the Heun method can be called the trapezoidalrule.Qh (20. which can be interpreted function can be written in generalform as over the interval.. ) ' i+ t n ) l t (:0.4 that centered same approximation In the sense.34a) (20.k. f]tdrZh. (20. t h . .30)]cannotbe applied r'. Recall from discussion of numericaldifTerenour at tlre miclpointof the of derivafinite differences arebetterapproximations tiation in Section4.the corrector[Eq. Jt rh I J'trl dx =. t.31) fbr the presenl case. global erors of the midpoint method areO(h3) and METHODS 2O.i4) (20. : f ( t i * p 1 h .u:k.t ' i ) l . a cenlered tives thaneitherlbrward or backwardversions. .
they linear. thereare an infinitenumif the RK methods.Suppose fbr can be solvedsimultaneously ut:lo: (20.ln addition.because droppedduring the derivation.in Section20. and so forth. next.39) I /42 (20. Thesesecondorder are terms with /rr andhigher to the differentialequationis quadratic. Euler's method.Every versionwould yield exactlythe sameresults ber of secondorder reyield different However.33)equal to terms in a Taylor seriesexpansion. pt.494 PROBTEMS INITIALVALUE That is. (20. 20. q's areevaluatedby settingEq. the other three.36) through Then Eqs.and Thus.36) (20.33)is The secondorder .k1 +a2k)h (20. underdetermined.40) Because we can choosean infinite numberof valuesfor a2.the local truncationenor is O1ft31and the globalenoris for whichthe O (h2).4. and qy1 are evaluated the to evaluate be derived can Taylor series. most Three of the sults when (as is typically the case)the solution is more complicated.4. Chapraand Canale.Oncen is chosen. solutionto the ODE were quadratic. methodswill be exactif thesolution function with two terms (n : 2). in fact.) t pth.38) (20. a2.2.1 SecondOrder RungeKuttq Methods versionof Eq.this recurrencemakes RK methodsefficient calculations.37\ (20.38) aresaidto be Becausewe have three equationswith four unknowns.Beft1 appearsin the equationfor k2.li+l :li where ky : k2: J'Qi' 1t.p's. three equations secondorder are (see The three equations 2006. computer numVarious types of RungeKuttamethodscan be devisedby employing different RK bers of terms in the increment function as specifiedby n.1. therefore.secondorder the approach. four unknown constants a 1I c t 2 : l a z P r: : azQtr l12 I12 (20. (20. which appears for causeeach ft is a functional evaluation. commonlv usedand preferredversionsare presented . (20'35)equal The valuestbr ar. that we specify a value for a2.or a constant. li i qlhh) f Qi l(a.By doing this.theseequations to determine unknowns of one of the a value must assume We.4. relationships. ln Section 20.valuesfor the a's. in the equationfor ftr. Notice that the ft's arerecurrence wherethep's and 4's areconstants. fbr details).35) (20.35b\ to a by settingEq. of theorder the numberof termsn usuallyrepresents at leastfor the lowerorderversions. (20. RK methodsusean increment For example. Note that the firstorder methodwith n : I is.35c) (20.the fourthorderRK method (n : 4) is presented g l o b a lt r u n c a t i o e n r r o ri s O t f t l t .
40) can be solvedfor a1 : 1/2 ancJ pt : Qu : L Theseparameters. yield /t . .e t h e n a 1: 0 . n ' o r .35)becomes . : lf3 and 7r1 and For this version.(11 RK algorithrus. Ralston(1962)and Ralstonand Rabinowitz(1978)determined that cltoosingo? :213 provides a minimum bound on the truncationeror for :7tt:314.l'i+r: )'i'l k2h where / < 1: /(t. p r : 4 t r : l 1 2 .35)becomes : r'. .(20.42a) (20.t'i+[. Conseqrrently.42b) k2: f fti + h/2.2 ClqssicolFourthOrderRunge'KuttoMethod there approaches.As with the secondorder The following is the most commonly usedtbrm. lt a2isassumedtob l . the secondorder Eq.\'ijr:. (20. T h e M i d p o i n t M e t h o d( o z: and Eq.39) and (20. (20. \l1 . : 1 / 2 ) If a2 is assumed tobe ll2. v .) 11. . . : 2/3).ftr \z + ljt . t . / (20.44) .. J'i * krhl2) method.+ (]t. this secondorder RungeKutta nethod is actually Heun's technique without iferationof the corrector. r ' .. Eqs. r ' i ) t kth) k2: f ft.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 . +: i ) ' i* *2k:*2h*k+)h U(kr I (20.+r where f t 1: / ( .43b) k t : l ( t .4r ) where f t r: . (20.. +f. r. thereforecall it the classicalfourthorderRK ntetlrcd: . ) (20. / ( r . Thisis themidpoint Rqlston'sMethod lo.41a) (20.4.rr) n . r . (2042) (20.l+ r'l) 20.35). and we are an inlinite numberof versions.4 RUNGE.43) (20..* /1. The most popularRK methodsarefourth order.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.20.13a) (20.r'. when substitutedinto Eq. (20.
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 as Solution.Equation represents at the imProvedsloPe. 20.7.O r d eR y'= Problem Stqtement.!i) (20.7 FIGURE melhod RK thefourthorder comprising estimotes of theslope depiction C.+1. For this case.44c) (20.*)r.the fourthorderRK methodis simito comeup of the slope are developed lar to the Heun approachin that multiple estimates of theft's each Fig. 20. Employ the classicalfourthorder RK method to integrate Ifw i t h y ( 0 ) : 2 .44d) Notice that for ODEs that are a function of / alone.t.496 PROBLEMS INITIALVALUE i /' . a: a: a.: f (t.3 EXAMPLE r KM e t h o d C l o s s i c q lF o u r t h . the classical fourthorder RK method is similar to Simpson's1/3 rule.'.r.44b) kz: f ('.44a) (20.ot where k1: f fti.tI u: a'. with an improved averageslopefor the to arrive of these average a weighted (20. in As depicted interval.0 .2\ : 3 . 4 r 0 8 t.+hh) (20.*)r. '.y.k' tl+ \Alo \ 20. In addition.44) then represents a slope.i.!)' .the slopeat the beginningof the interval is computed : (0.n) ka:f(tith.. +!.pt.n) t. *'/*<l L l "l *.
0 .f (0. It is certainly possibleto developfifth.\'(1.2.and higherorderRK methods.4sb) (20.5(4. .0.45) t. noticethat six tunction evaluationsarerequired.]l a/ l) (20. 5 :) 4 . this rnethocl has a global truncationerror of O (h5). n flnction evaluations . \r. * ).5 5 ).92974) 4e08(t0 Finally.5 ( 3 .vand anotherslopeat the midpoint: : 4. :0.(7kr * llAr+ 12k1* l2As +7k)h (20.20.r. ) . + 2 r r ' n + r.45a) (20. 5 .'n hh) y(ti rr5: + ftr..45c) (20.5(5.91291 1) + 5.For example.) k+: . this siopeis usedto compute a valueof .211 ] : 4.5):2+3(0.912974):5.r(.+ jr'r + jr.211299(0.r'.' * Io. 103Vo).945611 [3 + 2(1.912914(1.5.0) : 6.the four slopeestimates This average arecombined to yield an average slope. * + )0.n) *ton..945677 ka: f (1.. and a slopeat the end of the interval.201031( which cornpares favorablywith the true solutionof 6.201031 . * h . Q: 2e9)+ 2(3.5) 5) . .4 RUNGEKUTTA METHODS This valueis usedto compute a valueof .3 .0 : ) 5. slope is thenusedto rnakethe final prediction at the end of the interval.4sd) (20. 2 1 1 2 9 9 k r : l ' ( 0 .19463 I (e.4sf ) I .. t i) r .r and a slopeat the midpoint: l'(0. Butcher's As expected.r. : /(.y(1.t.Recall thatup throughthefourthorder versions.5):3. : 2 + 3. . Although the fifthorder versionprovidesmore accuracy. .rn) Note the similaritybetween methodandBoole'srule in Table17.+) fiflhorder RK methodis written as )'i+r : )'i * where k 1: f G i . Butcher's (196.0.5): 2 + 1.201031 u 1.912974 k7 : .5.r. r o : f ( .n . i ) k:: .lr^o +]r.5 : ) 4 c 08 ( 0 497 This slope in turn is usedto computeanothervalue of . .912974 . : 3..t(.. 108649 r(0.4.108649) 1e08(0 108649) Next.n+ft*.0) ) 0.45e) (20.0) : 2 + 4..
498 PROBLEMS INITIAL.(1.oneor are requiredlbr an nthorderRK method. \. . this problem freefall For the this chapter. r . . acc0unt Because thefunctionevaluations necessary. fourthorder less than the efficient atively popularityof the fourthorderRK method. . for the most computation for the main reasons This is one of the versions.25kg/m. are function evaluations two additional relconsidered higher are usually five and methods of order time. . ) " : .the gravitational tion is 9.8I m/sr.) 4z : J : ( r .) t . the initial would be x(0) : u ( 0 ) : 0 . Solvefor the velocity andpositionof the freefallingbungee to / : l0switha Assumingthat at / : 0.t.. :!:! dt :.the procedurefor solving a system This to the next step.f.48) m If the stationaryplatform from which the jumper launchesis defined as r : 0. of simultaneous involve thousands of ODEs. and integrate usingEuler'smethod. . Engineeringapplicationscan the involves applying simply of equations each case. .Interestingly.VALUE for ordershigher than four.2. .1and 1. r ' . accelerastepsizeof 2 s. . .Suchsystems multaneous ordinary differentialequations generallyas be represented d)'t i : / 1 ( t . : . . r : u : 0..l t . .5 SYSTEMS OF EQUATIONS of sirequirethe solutionof a system and science Many practicalproblemsin engineering may ratherthan a singleequation.I kg with a dragcoefficientof 0. onestep techniquefor every equationat eachstepbeforeproceeding method. and thejumper hasa massof 68. r . . of at the beginning up amountsto solving the following systemof ODEs: dx ' dt du dt wat 6 ) (20. .4 EXAMPLE Method o f O D E sw i t h E u l e r ' s S o l v i n gS y s t e m s jumper Problem Stotement.47) (20. ) ) i ) (20. 20. r ' 1 .As was donepreviouslyin Examples1. ) . t. i.) .5. conditions 20. : .1 Euler'sMerhod to systems can be extended in this chapterfor single equations All the methodsdiscussed In equations. for Euler's is best illustratedby the following example 20. jumper's velocity and positionthatwe set An exampleis the calculationof the bungee portion of the jump. .46) The solution of such a systemrequiresthat n initial conditionsbe known at the stafting value of /.
(x) e.9)l: r.I n l c o s h { .4137 46 2983 50 I 802 5r 3t23 r 0 00 0 % 45 45% ?425% ]3 83% B72% 476% 9.7292 33 il tB 42 4762 469575 49.20.z ) This result can be substituted into Eq.3.25 .u" ur"u* rEul".81 dt 0. (20.+t.24 u: / 0.25 ::::(142:9. TABTE2O. The process can be repeated to computethe resultsat t : 4 as .t \ l Proceeding in a like mannergives the resultsdisplayedin Table 20. The ODEs can be usedto compute the slopes at / :0 dr :U dt i::^ t il ' Gr.i \V .8t . e. ll as du :::9.3 Distonce ond velocity of o freeJolling bungeeiumper os computed numericolly with Euler's method. (a) 0 2 4 6 B t0 0 1 91 6 6 3 7t 9304 147. .lo8 68. 8 1 ( 2:)1 9 .9462 2 3 75 ) 0 4 3 3 4) 7 8 2 00 t 8.5 SYSTEMS OFEAUATIONS Recallthat the analyrical solurion for velocityis tEq. uEul".86% 3 B3:d .\ 19. 6 2 The analytical solutions canbe computed as.4214 0 39. respectively. the percentrelativeeffors are 1007oand4.62)2 )z : 30. Soluiion.t(2) : 19.240A I t2 A674 2A4 664A 3 0 54 2 4 4 o r9 6200 36.lf' r 19.1567a.62+ f 9.97% 1003% 6.16629 and r'(2) : 18.62(2):39.r:0*0(2):0 u : 0 * 9 .81 68.1 Euler's methodis then usedto computethe valuesat t : 2 s. / a rl r l cl L \V. r ( I ) : . (1.r:0*19.72919. Thus. rt.41) which can be integratedto dererminean a n a l y t i c as l olution for distanca es m f t .(/ ) : l!!! 61nn( lE!!.\ V c.n'J) Use theseanalyticalsolutionsto conpute the true relativeerrorsof the results.
7 However.Theseare then employed predictionsat the end of the interval that are usedto developslopesat the end of the interval (the fta's). Use the fburthorderRK methodto solve fbr the sameproblemwe addressed in Example 20. develop is.r : 0. k1... 0) : 0 J'r(O.! :O+Oi : o r ' ( l ): r ( 0 ) 22 u ( r ): u ( o + ) r r .u ) : 1 . is the lth value of ft for theTth dependent first valuesof . t t .That 's) Theseslopes(a setof ft1 arethenusedto make slopesfbr all variablesat the initial vahre.1 the variable. x. large step size.. step iarge solverprovidesdecentresultsevenwith a relatively using a higherorder 20.soo PROBLEMS INITIAL. First.:u' tn Ct t . 8 1.5.Next. the fr's are combined into a set of increment functions [as in The fblEq.Thesemidpointvalpredictionsof the dependent ues are i1 turn usedto cornputea setof slopesat the midpoint (the ft2's). As described iteration.Using a much snrallerstepgreatlymitigatesthesedeficiencies. Finally.fzj. r t:]o + 9 .4. (20.Thesenew slopes that are then taken back to the startingpoint to rnakeanotherset of midpoint predictions make to leaclto 1ew slopepredictionsat the midpoint (the l1's). u) : I .9 J 1 t': {'t. (20. variableat the midpoint of the interval. equations.2 Runge'Kutfo Methods of RK methodsin this chaptercan be appliedto systems Note rhat any of the higherorder helpful in is Figure 20. size. 8 1 68. the resultsarenot very accurate systems . we first visualizing the proper way to do this for the fourthordermethod. . r . Solution. 8 1 ]: n . 0 .5 RK Method Solving Systems of ODEs with the FourthOrder Problem Stotement. : . we must calculate where ki.VALUE for how Euler'smethodcanbe implemented Although the fbregoingexampleillustrates In addition.41))rhat are brought back to the beginningto make the final predictions.: 2 1 2 ( 0 . because of the of ODEs. illustrates the approach..caremust be takenin determiningthe slopes.r does not changeuntil the second because the resultsfor distanceare a bit unsatisfying next.46)as dx E 0Il : f i ( t .l 2 : 9 . lowing exarnple EXAMPLE 20. it is convenientto expressthe ODEs in the functional lbrmat of Eq.r and u at the rnidpointof tlre first step: r f.0 ) : 9 .tr The first stepin obtainingthe solutionis to solve for all the slopesat the beginningofthe interval: kr.
oilpur.963412:19.4547 46. 9.:9. 9. 9 1 3 1 : )18. 9. 8 1 k2.2 t'. 8 1 ): 9 .1898 Tlrevalues to compute of t canthenbe used [Eq.0 .4 rt. 9. 8 1 0 0 : f 2 ( 1 .4817 6Proceedingin a like manner for the remaining stepsyields the values displayedin Table 20.. f t( 1 . ] 2 5 6 69 7. 8 1 0 0 9.fr(1. 8 1 0 6 u \ 2 ): 0 + . the fourthorderRK predictionsare much closerto the true values.o. 4 8 9 8 1 2 :1 8 . nonzerovalue is computedfor distance on the first step.9 . r ( l ): .0r 9% oo37% 0 051% a a49iL 0 038% 0 2 4 6 8 t0 0 1 91 6 6 3 71 9304 147.8loo . .4. Distonce of o freeJolling os computed ond velocity bungeeiumper numericolly with thefourthorder RKmethod.t : . :9. ! : o + 9 .4561 kt.9134 u(2): u(0) 1: 8 .r656 7 ) . 9 6 3 4 : : ka. ut. 0 .4567 (2) : 18..18. 18. . 9 6 3 4 + k3.9134. 9 3 )I t47 9s21 2 3 75 t O 4 3 3 41 6 2 6 0 I8.u" JnKr unx{ TABLE2O.4817 k3.9575 4 44 2 \ 4 0 19.4561) These areused to determine thepredictions at theendof theinterval: x(2) : r(0) + k3.Further.4811(2) wltichcanbe used to compute theendpoint slopes: : J t ( 2 . [ 9 . 8 1 0 0 + 2 ( 9 .4a)l: x ( 2 ): 0 + I +0 9 .t .1 8. u(l): u(0) + tz:i 0+9.2lt:0+9. In contrastto the resultsobtainedwith Euler's method.9634) 8. (u) 0. r ( 0 )* t .9634 kq.4567.1656 t 0+ 2 ( 9 .1 8 .8100.a highly accurate.(20.u" e. 4 5 6 1+ ) 18.. 9 .7256 330995 42.20.8100.9345 49.456't) : fi(1.004% 0 00r% 0 004% 0 000?{ 0 005% e.2 ) : 9.(x) 0.4561 Theseare usedto deternine the secondsetof midpoint predictions: 501 :1 .4427 .(2.5 SYSTEMS OF EQUATIONS which can be usedto computethe first set of midpoint slopes: k z : : .9462 2 3 75 1 0 4 3 3 41 7 8 2 0 )B7292 3318 42. 4 5 + +) 8 .0762 46..1h 0 + 9.4561 whichcanbeused.thesecond'set of midpoint slopes: : 9.
L = (ti:h:tf)'. ..p1. ' i ' ! N l b y 5 \ u l q L . n = n+1. (Continued) .tspan. \ F + 1.).. J . FIGURE 2O.3 MATLAB Mfile Funclion: rk4sys Figure 20.:) = y0.^ i .. l ..3) to solve a singleODE with Euler's method. ^ h f f ^ .tL.tf _ _ f = tspan(n) .yl Z a system of ODEs wit.h L . = additi"onal parameters used by dydt % % output: % tp = vector yp = vector % it r! h  of of independent variable solution for dependent variables redsL 4 .. hh = g(nP+1) ._O I a: /^r / nargin.fl. This code is similar in many ways to the functiondeveloped earlier (Fig. % times where solution output specific y0 = initial ? values of dependent variables % h = step size p1 . tfl. 20. . ^LrlJarrr c^rr\ landfh/f input 'rcn:r LJPcrr argumenrs requ ired' t.F . 1 h . I ^ ^ .For example.\ r r r r v o r a r v g rk4sys: fou:thorder RungeKurta for d system ol ODts = rk4sys(dydt. 4 y v ' L t t t ' .8 showsan Mfile called rk4 sys that usesthe fourthorderRungeKutta method to solve a systemof ODEs.8 AnMJile iheRK4 foro system lo implemenl meihod oltCDEs. . end not ascendi ng order') . initial times wiLh output. ? t.tspan.h.I J a r t ' .t(nP).it is passed the function name defining the ODEs throughits argument.' r . or = ltO t1 .502 INITIALVALUE PROBLEMS 20. and final generaLed ac interval Z of h. . end ti I F = tspan(1.error('ar arrJ I ).5. np = 1r tp(np) .p2.. ' ^ l t L I l ' y .y0.  . end else t . .l t a ' t .h fourthorder RK meEhod ? inpuL: ?' dydt = name of the Mfile that evaluaLes the ODES tspan = tti. end y(I .f t(n+1) = tf.4. yp(np..eqrates lt. while(1) Eend = t(np+1). € .) % : int. n = length(t).).:) i 1 .p2. if t (n) <t. tt = tii = y(1.
end while(1) i f ct+hh>tend.:) i f tt>=t f. the routine automatically times. Note older.0541 45. 9 . First. r656 1L. 20.1 526 0 tB. Veird=y(i. be in ascending We can employ rk4sys to solvethe sameproblemas in Example20j.:) .vararqrin{ : } )' .9344 49 .it has an additionalfeaturethat allows you to generate the As was casefbr Fig. 2 ' : / 6 8 . y) ^21. end end = Y{1. . t f I .93II !47.respectively./2. r ts ts  rlih .dydt (tt+hh. t 1 . = that in both can define r span i t 0 .:) FfGURE20.y1i. However.vararqin{ kl = dydt (tt.1) Y( t. .varargin{ phi = (k1+2*(k2+k3) +k4) /6.you at specific results you want to obtain intervalsh.we can developan Mfile to hold the ODEs: f unction cty = dydtsys (t.:) + k2*hrh/2.if at equalspaced must span values the L cases. YP(nP. h]i = h. \.yend.:) + kL.8 lContinued) output ln two ways. k4 = dydL(tL+hh.952r a?? (141 334.:) + phi*hh. if tt>=Lend. : ] )' .12a6 33.ymid.0 .you dependingon how the input variabletspan is specified.'end : })' . ymid = y(1.:) y(i+l. d y = l V ( 2 .0000 0 19./2..0000 6. times. } )' . = y(i.4427 . . 1 * Y ( 2 ) as wherey ( 1) : distance(:r)and y 12) : velocity (u). initial final and can set rspan = tri rf l. tp(nP) = tt. whereri arld tf are the theselimits values between generates output lf done in this way. end end np = np+1.varargini ymid = y(i. k3 .0995 42 . The solutioncan thenbe generated >> disp( [1' ]'(:.2))\ 0 2.0000 4. brea]:. break. . k2 = dydt . Alternatively.*hrh.0000 8.OF EAUATIONS 20.tt+hY\/2.5 SYSTEMS 503 i f hh>h.0000 10. B \ . +k3*hh. hh = tendtt. :).ymid.3.
An example of a simple nonlinear model basedon atmosphericfluid dynamicsis the Lorenz equations createdby the American meteorologist Edward Lorenz: dx ..6 x . thoseinvolving xy) are what make suchequations nonlinear. These equations are commonly called LotkaVolterra equations.50) where x and y .1626 42. Predatorprey models were developedindependentlyin the early part of the twentieth century by the Italian mathematician Vito Voltena and the American biologist Alfred Lotka.504 INITIAL.tt Lorenz developed theseequations to relate the intensity of atmospheric fluid motionr to tAmhArotrrra rrariotinnc 1r enrl 7 in thp qnA . and b andd : the ratescharacterizingthe effect of the predatorprey interactions on the prey death and the predator growth.9521 10.1) y(:.The multiplicative terms(that is. dz :bz*xy . For example.4021 PREDATOR.0000 334.respectively.The secondare equationsderived from tluid dynamics that are usedto simulate the atmosphere.The simplest version is the following pairs of ODEs: dx E:axoxY dv E'Y+axY (20. tems of nonlinear ordinary differential equations.A5t11 49. The first relates to predatorpreymodels that are used to study species interactions.VALUE PROBLEMS We can also use t span to generate results at specific values of the able. y(:. c : the predator deathrate.This case study focuseson two of these applications.o y E u dt rx)Jcl.the number of prey and predators.PREY MODELS AND CHAOS Engineers and scientistsdeal with a variety of problems involving sysBockgrcund.2))) 000 6.0000 141.a = the prey growthrate. >> tsPan=t0 >> disp(lt' 5 101. respectively.49) (20.
L s p a n .Y (: . In addition to displaying the solution as a timeseries plot (.9.plot (y ( : . a. c = 0 . First. b = 0 . h = 0 . .c. Hence.plot(t.6. In contrast. 20.v ( :. As in Fig. At a certain point. The time series (Fig. the increasedpredatorscausethe prey to decline.4 ) . c: 0.d) .d) y p = [ a * y ( 1 ) . ) subplot (2. we can develop a lunction to hold the differential equations: function yp = predprey {t. 2 .b.Note that the function eulersys was basedon modifyof suchan Mfile as a ing the rk4sys function (Fig.2 ) .t. c. the nonlinearitiesstem from the simple multiplicative terms: xz and xy.2. 20. c . the processrepeats.b :0. Employ initial conditions and integratefrom t: 0 to 30.Y {:. This decrease. The following parametervalues can be used for the predatorprey simulaSolution.9b).1) . using a step size of h:0.b * y ( 1 ) * y ( 2 ) .y(:. the script also generatesa plot ofy versus x. a . ofthe predators.r and y versus /).2. the expected' as that. This is plot (Fig.V (: .c " y ( 2 ) + d * y ( L J* y ( 2 ) ] . Such phaseplane plots are often useful in elucidating featuresof the model's underlying structure that may not be evident from the time series. 1).L) .2.6 CASE STUDY 505 coniinued with the predatorpreymodel. t s p a n = t 0 4 0 1. Plot the results to visualize how the dependentvariableschangetemporally. leads Eventually.y ( . In addition.L).8).1) .plot(t. of _r:2 andy : I tion: a : 1.'predaror' ) . h .2.Eventually.plot (y( :.20.y.3 ) . andd: 0.9c. 0 6 2 5 . the prey become so numerous that the predator population begins to grow' in turn. b . The following script employs this function to generate solutions with both the Euler and the fourthorder RK methods.8.2) .2). y 0 .We will leavethe development homework problem. d = 0 . B. 6 .2.tspan. a cyclical pattern emergesin time. . graph the dependent variables versus each other to seewhether any interestingpatternsemerge. t . 3 .'' (a) Euler tine plot' ) legend('prey'. 20.title(' subplot \2. '') title (' (c) RK4 rime plot') subplot \2.21  title (' (b) Euler phase plane plot') tt V) = rk4sys(GpredPreY.3.h. subplot (2. 20.y0.21 j title ( ' (d) RK4 phase plane plot') The solution obtained with Euler's method is shown at the top of Fig. theseresults indicate that the crude reinforced by the phaseplane Euler method would require a much smaller time step to obtain accurateresults.a. Use numerical methods to obtain solutions for these equations. the RK4 method yields good results with the same time step. because of its much smaller truncation error.Notice to a decrease . 20. Because the predator population is initially small. the prey grows exponentia\. d ) . y 0 = [ 2 i l i a = L .9a) indicates that the amplitudes of the oscillations are expanding. I t y l = e u l e r s y s ( @ p r e d p r e y .0625.b.
9Q indicates The phaseplane representation that the interaction between the predator and the prey amounts to a closed counterclockwise orbit. PROBTEMS INITIAL.ii* continued 10 20 30 ( a )E u l e t ri m ep l o t 5 10 ( b ) E u l e rp h a s ep l a n ep l o t I I I . a be and the ! /b. :i. will remain constant.20.9 Solution fortheLoikoVolterro model. The formeris the trivial result that if we start with neither predatorsnor prey. Euler's method ond{b)phoseplone plots.s06 ti. Also.VALUE . RK4 meihod timeseries ondld)phcseplone predator peak lags the prey. for the accurateRK4 solution (Fi9. 0) and (cld. Now. y) = (0.Thelatter is the more interesting outcome that if the initial conditions are set at x:cld and : populations the derivatives will zero.and r : 28.50) to steadystate (dy ldt : dx /dt : 0) and solving for (x. II I ll tl tl tl ll t t I I I I I I I I t\ r I lt tl . i +"t '.] I I . nothing will happen.b = 8I 3. (20. Interestingly. ond {o)fimeseries (cJ plots. The exact location of this point can be determined by setting Eqs. observe that the processhas a fixed periodthat is. there is a resting or critical point at the center of the orbit. it repeatsin a set time. alb).49) and (20. Employ initial conditions . 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. let's use the sameapproachto investigatethe trajectoriesof the Lorcnz equations with the following parameter values:a = 10.
20.: : 5 10 5 10 tc 20 FIGURE20. The results are quite different from the behavior of the LotkaVolterra equations.10.The term chaotic is used to describesuch solutions.the variable.Thus. 5 ) of"r:.r is perturbed condttion {5. However.Thedoshed slighily i s 0 0 r.r seems to be undergoing an almostrandompatternof oscillations. 5). 20.Forthiscase.IO Timedomoin representotion of x versus t for fheLorenz equotions. after about / : l5 they diverge significantly. even though the pattems seem random.)':z:5andintegratefrom/:0to20. 5.Although the solutionstrack on eachother for a time.6 CASE STUDY 507 confinued 20 E Lorenz model "{ versus r 15 5 .001). Thesolidtimeseries is for the initiol conditions lineis wheretheinitiol for. 5 .10. the frequency of the oscillation and the amplitudesseemfairly consistent.' . 20. bouncing around from negative values to positive values. An interestingfeatureof suchsolutionscan be illustratedby changingthe initial condition for "r slightly (from 5 to 5.In his original study.As in Fig.wewillusethefourthorder RK methodto obtain solutionswith a consrant time stepof h :0.The resultsare superimposed as the dashedline in Fig.03125. we can see that the Lorenz equationsare quite sensitiveto their initial conditions. this led Lorenz to the conclusionthat longrangeweatherforecastsmight be impossible! . 0 0 1r. The other variablesexhibit similar behavior.
intt res As was the casefor Fig.2) ) >> xlabel('x' ) . the sensitivity of chaotic systemsto initial conditions hasimplications for numericalcomputations. the threedimensional plot (Fig20.y(:. 21 are designedto demonstrate this issue.10. differentstepsizes or different algorithms (and in some cases. thesediscrepancies will eventuallylead to large deviations.rotions The sensitivityof a dynamicalsystemto small perturbations of its initial conditions is sometimescalled the butte(Iy ffict.z1abe} ('2.I I represeniot. we can generate projections.on Phoseplone for fhe Lorenz (oJrr. MATLAB's p1or3 function providesa vehicle to directly generate a threedimensional phasepiane plot: >> plot3 (y(: . As a final note.Someof the problemsin this chapterand in Chap.y. (a) (b) (c) (d) .ylabel('y' ) . 20.The idea is that rhe flapping of a butterfly'swings might induce tiny changesin the atmospherethat ultimately leads to a largescale weather phenomenon like a tornado.508 INITIAL. and the yz planes. Beyond the initial conditionsthemselves. Beyond the twovariableprojections. ond (cJyl proiections eqL. Although the timeseries plots are chaotic.11 showsprojectionsin the x. ) . 20.VALUE PROBLEMS continued (a) y versusr (b) z versusr 10 n 10 45 40 35 30 a25 (c) .2) .Figure 20. phaseplaneplots reveal an underlying structure. Thesepoints are called strangeattractorsin the jargon of mathematicians who study suchnonlinearsystems.L) . Because we are dealing with three independent variables.xz.12) depictstrajecrories cycling in a definite pattem arounda pair ofcritical points.y(:.In a similar fashion to Fig.Noticehowa structure is manifest when perceivedfrom the phaseplane perspective. versus! 45 40 JU 25 20 l5 1q 20 10 5 20 10 0 "r 10 10 5 40 20 0 ) 20 FIGURE 20.1i.r:.grid 20.even different computers)can introducesmall differencesin the solutions. (bl .The solutionforms orbits around what appearto be critical points.
\ r'r'v/ /i. Using Euler'smethod. :. Using the midpointmethodu'ith ft : 0.PROBLEMS 509 K. Using Heun's methodwithout iteration. dr' dt :\. 20.25 wherel (0) . UsineRalston's method. . PROBLEMS 20. Display all your resultson the sameglaph.1 Solve the following initial value problem over the interval frorn / : 0 to 2 where r'(0) : l Display all your resultson the samegraph.12 Threedimensionol phoseplone representoiion forlheLorenz equolions generoted with MATLABs p}ot 3 iunction.4 0 20 FIGURE 2O. (a) (b) (c) (d) Analytically./l l\.1r " r I I ).25. Usingthe fburthorder RK methodwith i : 0.5 and 0.l. Using the fburthorderRK method.5.l*lin conrinued t'\ 30 20 10 v0 l I I l\l tI lI I l \ _10 20 2n I rl tt_ t 40 . .2 Solve the following problem over the interval from r : 0 to I usinga stepsizeof 0.5. Using Euler'smethodwith /i : 0. (a) (b) (c) (d) (e) Analytically.
.t.kgtn:0. One of the simplest that a projectileis launchedupwardflom the 20.4 using a step size of 0.Display your simulation (/l' :21.O dtt . 20.5 wherey(0) : l. 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. Assumethat the only force actingon theobproportionalto the existing populationat any time /: ject is the downward force of gravity.. : the maximum growth rate under unlimited conditions.4. .1) holds.but can be formulatedas circuit that aroseback in the days of vacuumtubes: k.the growth rate approacheszero. solvethis equationfrom 1 : 0 to 10 using Euler's methodwith a step sizeof (a) 0.1 .1%. = p 4 4 5 4 4 8 5 0 5276 5686 6079 1400m/s.J The growth of populationsof or.. 20. at low popr"rlationdensity p {{ p^u.000million people.".The analytical solution to this modelis yP\l 2{). is sontetimes called the curning t'upucin..i.5ro INITIAL.'10.Note that 2n.. . pmrr : 12..it breaksdown when . (b) Heun's method with iteratingthe initial conditionsand parameter valuesfor your simulation: correctoruntil s. Using this growth rate fbrmulation. 20. (P20.7 Solve the lbllowing pair of ODEs over the interval (a) Assumingthat Eq. 3 7 z 1 0 6m ) ... .3 Solve the iollowing problem over the interval fi'om I :0 to 3 using a stepsizeof 0. rnd (b) the fourthorderRK Obtain your solutions with (a) Heun's method without method with a step size of 5 years.p/p.:0 dt .population.h 3 factors such as fbod shortages.0261yr.the growth rate is not a con20.9 Given the initial conditions. + kr. Employ the tbllowing iteratingthe conector.rr..r12 tlt where ftq : the growth rate. Obtain your solution with I 950 throughI97Oto estimate ks.and (d) Ralston's methc)d. 2A fb al Pr 2( Er rh 2( pe is b1 ta Si h: (l at' .r'') ."*. L The initial conditions are .Display your resultsasa plot has rrany en20.p f 1 t . : kr. and /max: the rnaximurn population... < 0.VALU E PROBLEMS This is referredto as the logistic model. ( l . pollution. and lack of space inhibit growth. d:" dt ot Pmax Po * (2nr"" Po)e kc^t Simulate the world's population from l950 to 2050 using (a) the analytical solution.r(l . The world population in milwhere u : upward velocity (m/s). dl 20. t : time (s). (b) Use the fburthorderRK method along with the results (a) Euler's methodand (b) the fourthorder RK method. Thus. Given the initial conditions.x: altitude (m) measured upward from the earth's surface.8rnAr).555 million people.5 Although the model in Prob.ganisms along with the datafiorn Prob..(0) : 4. r 1 9 5 0 r 955 I 960 r 965 lsTa 1 9 7 5 gravitationalacceleration 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 . solve the fbllowing initialvalueproblem from t : 0 to 4: r/l l lQ\. Display all your results on the samegraph. a fbrce balancecan be usedto derive dLt (P20.: kpp ul dt. gineeringand scicntiflc applications. =6. ) where t..t4et results alongrvith the dataon a plot. )(0): I and 1. k. ln such cases.g(0) : the at the earth'ssurface(? 9.4. 20. Disof (a) to stimulate the world population from 1950 to play your resultsas a plot. Plot both solutions on thesame grrph. and px (in 1950):2. f t.4. *t ) lions from 1950throush2000was for Pl so 20 m( pl( po 5' 2tJ mi of uIr 5.t2. use the data from frorr r : 0 to 0. .1) . As p approaches7r.6 Suppose rnodelsassumes that the rate ofchange ofthe population 7' is earth'ssurface.4 works adequately yawhen population growth is unlinrited.tt The van der Pol equation is a model of an electronic stant. R e c o g n i t i n g that dxldt: u. Under theseconditions. (c) the midpoint method. 2050 with a step sizeof 5 yeals. 1(0) : )'(0) : 1.2 ancl(b) 0. the rate of changeof populationcan be modeledas dp dt : * c . Kdlf)\o"'(R+. p .y(0) : 2 and .
< 0' l7o.Employ Prob.PROBLEMS and (b) the Obtainyour solutionswith (a) Euler's rr. Employ a step size of 5lt FIGURE P2O.In 19.14 Isle RoyaleNationalPark is a 2[0squaremile pelagocomposedof a sinele large islandand many snrall dx d2x lr. and islandsin Lake Superior'.Tesl your program by using to your model and the data for both the residuals between l.and c : the damping coefficient rn Sincethe late 1950s.13 Develop an Mfile to solve a systemof ODEs with (b) plot olyour solution.+k.12 Develop an Mfile to solve a single ODE with the the data using a tinrewith your simulation Compare RK method. archi. .Design the Mfile so that it createsa plot of the results. 2 3 . fourthorder Plot both solutionson the same graph along with the exact solution .t' : cos34.+t .Design the Mfile so that it creates tburthorder of the of the squares sum the plot determine series and it solve a plot of the results. 20. 0 1 3 3t. RK method. from equiliblium position(m)' t : wolves crossedan ice bridge from Ontario.6) liom 1960through2020 using the tbllowing coeflicient 5 years.Testyour prograrnby using it to solvefbr population as describedin Prob. 5 1'eiirs 20.5. 20. Employ a step size of and iteratethe correctol'until c.10 Developan Mfile 1tlsolvea singleODE with Heun's methodwith iteration.: : 0 ..19..11 Develop an Mtile lo solve a single ODE with the l plot method.entialequation: 20.Test your programby using it to solvefor population as describedin Prob.Test your programby using it 1osolveProb.15 20.i:0 Moose arrived around 1900.7 theresults. P20. a phaseplane Develop a plot ol Euler'smethod. LltdI vegeravaging rtpprouched.Designthe Mfile so that it creates miclpoint of theresults. :20kg (s). 0 : 0 .rethod step sizeoi 0.l5) is describedby the fbllowing oldiniuy difler$ith a stepsizeof 0. nrclose and the wolves. The damping coefficientc takes on three values (N havebeen tracked. 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. (Fig.25. ' 1 a 20. their population b1 l910. use a In both cases. size 0.193 1435 1467 2A 22 22 23 20 26 2B 26 22 22 l7 IB 20 23 24 3l I001 I028 9r0 863 872 932 I038 ll t5 ) t92 t268 r3 3 5 I 397 1216 4) 44 34 40 43 50 30 l4 23 24 22 2A t6 t2 t2 l5 t3r3 r590 lB79 l77A 2422 I 163 500 699 750 B5O 900 I 100 400 7 5Ct 544 450 2005 2046 12 l2 t3 l7 t6 22 24 l4 25 2a t9 t7 l9 29 30 30 .2.the numbersof the mooseand wolves time . mass. 20.I5 (a) tntegratethe LotkaVolterraequations (Section 20. a step of 20.i000. v a l u e sa : : 0 . s/m).Designthe Mtile so that it creates a damped springnrass system of motion The 20. I .5. ndd : 0. wherex : displacement tation. 20.0004.
CA 1 . and tlre initial displacement .und h: the depthof liquid in initial conditions of all the dependentvariablesarezero.0 gmoyl. CB2= concentration of B in the secondreactor. and k ofA to AnEii C Q. is 20 'C. The initial velocity is zero.r: produceB. 40 (critically damped).CB:l . 20. We assumethat at the time of death.: outflow (mr/st..lf C:Ao is equalto 20.s6nqsrtration ofA at the outlet of the first reactor (and inlet of the second).is the constantambi. and that two hours later.n A(h 20. I : CBtIkCAt . .^n+ ^f :^1:\T^r^ ]L^* +L^.512 INITIALVALUE PROBLEMS Suppose that the temperature of the corpsewhen it wasdiscoveredwas 29.The reactorsare well mixed but are not at steady massbalancefor eachstirredtank state.The unsteadystate reactoris shownbelow: ICA' I :(CAuCA:)kCAr .l0(T . Find theconcenperatureof the object at time /.the oa 1. A : the areaof the oril'ice tion. 20.5"C. nonisothermal batch reactor can be described by chapter to determine horvlong it will takefor the waterto flow the following equations: out of a 3m diametertank with an initial height of 2.tration and temperatureol' the reactor as a function of time. :K(T dt Calc Use I: 20.Supposethat at time I : 0 a corpse is discoveredand its temperature is nreasured to be I. The ambient temperature (a) DetermineK and the time of death. of 5 (underdamped)..5 "C. l2lmin and r : 5 nrin and assume thatthe (:9. (b) Solve the ODE nunerically and plot the results. . C : an empirically derived and B in both reactors during their first 10 minutes of opera(mr).r : I m. Solve this equationusing a numericalmethodover the time period 0 < / < 15 s.The flow rate through the hole can be estimatcdas dCB.4 where Qou. Use t : 0. atr FIGURE P2O.20) it is known that the surfacetemexperimeutalobservations.The spring constantk : 20 N/m.: dl I tCBt . E : modulus of elasticity. the reactor is at I 5 "C and has known as Newton's law of cooling.From the dT . This is perature of the reactor. in 20. lional constant the tank.55. : 1000e(r0/(r+273))C dI peratureof an object changes at a rateproportionalto the differencebetweenthe temperature of the object and that of the where C is the concentration of the reactant and I is thetemsurrounding environment or ambient temperature.it is 23.^^^ .3 : the gnrvitacocfficient.  11 ) tl:: where /(l) ?E I : wind force.19 A.2 Und diff( ! ( whe A(h gra\ outl area min h(0" h./ (a )  . if f(/) is the tem. aIt varl( dCA. lnitially.I6 A sphericol tonk.) flection of a sailboatmast subiectto a wind force: (1)' t ) where K > 0 is a constantof proportionality.2 gair enc sho this stru dev 7.18 The reaction A + B takes place in two reactors series.a concentrationof reactantC of 1.kCB: T where CA6 : concentration of A at the inlet of the first reactor.ssnssrtration ofA at the outlet of the secondreactor.. r : residence time : the rate constantfor reaction each fbr reactor.20The following equationcan be usedto modelthededT . Thus. ent temperature: 2(1. and i". CB r : concentration of B at the outlet of the first reactor(andinlet of the second).81 m/srt.15 m.and 200 (overdamped).16 A sphericaltank has a circular orifice in its bottom through which the liquid flows out (Fig. P20. Plot the displacement versustirne for eachof the three valuesof the dampingcoefficient on the sameplot. I j:(CAtCA)kCAt dt T J dCBt :. find the concentrations of.17 ln the investigationof a honricideor accidental dl it is often important to estimatethe time of death. I = ^A I ff^'.16).. CA2 . Use one of the numericalmethodsdescribed in this 20. dC _ _r\t)/(T+273)) g death. Note that the orifice hasa diameterof 3 cm and C: 0.
96. m2 6 I l7 . u.1  . : 0 to 20 s.g : (: 9.r1/th : I m/s. m1 m2 A'x3 /1t . d : 0 .22 ence of disturbancessuch as earthquakes. fbr a threestorybuilding. solve this diff'erentialequation to determine how long it takes tbr the pond to empty.rr) ]tl t. n: = 8. For shorvssuch a representation this case.the analysisis limited to horizontal motion of the canbe structure. 20. and all other initial values of displacementsand velocitiesare zero. ffr A(h).forcebalances UsingNewton'ssecond developedfor this systemas dtr Simulatethe dynamicsof this structurefrom .22 Engineers and scientists gain insight into the dynamics of structures under the influFigure P20. /q\tl Le) qT0\= 1 2 1 0 0 0k g /7rr k.\\)  )l) dt: t ) t tn: K1 .t) . phaseplane plot of In addition.05 for your computation.1. 2 5m . the fbllowing differential how depthchanges equationdescribes with time: ' 1A(h)' (nr). grar.22 k1 3000kN/m 543210 a97 A67 445 A32 01B 0 use massspring modelsto 20.6 for the Lorenz equations but use a value of r :99 .developa threedimensional the displacements.tt.. given that l . siven the initial condition that the velocity of the ground floor is d.21.+: = rrr Ilt t ^) :(.25 x 108. Basedon the following areadepth table. arrd / : 0. Undera numberof simplifying assumptions.6.6 for the Lorenz equations but generate the solutionswith the rnidpointmethod.000 kg ) . 2(1. / T.tu f.al: pipediarneter A\h): pond surf'ace areaas a functionoldepth (m2t. Ure paranreterl'alues of L :30..2I FIGURE varies with height accordingto " 200. E : 1.21A ponddrainsthrougha pipeas shownin Fig.81 m/s2).23 Repeattlre the sarnesirnulationsas in Section 20. law. e : I m . utt : \ . L]"il1\ = 10. ^l {. (r { 2 l. Presentyour results as plots of (a) displacements two timeseries and (b) velocities.'itational constant and c : depthof pipe outletbelow the pond bottom (m)..000 In2 kg ftr 1800kN/m C a l c u l a tte i f r ' : 0 a n dd v l d z : 0 hedeflection at z = 0. r: time (s). dl' .PROBLEMS 5r3 __t_ e d P2O.\r ltr (l * j('r: . ( 0 ) : 6 r n . Compare vour resultswith thoseobtainedin Section20.24Perfornl the samesimulationsas in Section 20.tt . 2400kN/m qTn'r FIGURE P2O. 20.here /i : depth (m). P20. dr h.
a much smallerstepsizethan to be applied to the entire computation.l 5 to 2.the solutionundergoes tical consequence of dealing with such functions is that a very small stepsizewouldbe algorithm required to accuratelycapturethe impulsive behavior.For most ofthe range. For this size. 21.1. Learninghow to passparameters for solving the differencebetweenonestep and multistepmethods Understanding ODEs. that are usecl difl'erentordersto provide error estimates Familiarizing yourselfwith the bLriltinMATLAB lunctions for solvingODEs. 2I. Learninghow to adjustthe optionsfor MATLAB's ODE solvers. of necessaryand.the srnallerstep size requiredfor the region of abruptchange As a consequence. localizedregion from t :  .the solutionchanges Suchbehaviol'suggests results.25. to MATLAB's ODE solvers.Adoptive Methods ond StiffSystems CHAPTER OBJECTIVES methods The primary objectiveof this chapteris to introduceyou to more advanced Specific for solving initialvalueproblemsfor ordinary differentialequations. can represent a selious limitation.many more calculationswould be wastedon the regions gradualchange. o of how the RungeKuttaFehlbergmethodsuse RK methods Understanding to adjustthe stepsize. . Understanding what is meantby stiffnessand its implicationsfor solvingODEs.However. for a that a fairly large step size could be employedto obtain adequate Thepracan abruptchange.supposethat we are integratingan ODE with a gradually.I METHODS ADAPTIVE RUNGE. we have presented problems.If a constantstepsize would have were employed. Fig. For a signiticant nuinber of solution depicted in of the type example. therefore.KUTTA methodstor solving ODEs that employ a constant step To this point. objectivesand topics coveredare r r o o .
lr'Urgiventhe initial conditiony(a) : 0. {^.1 ADAPTIVE RUNGEKUTTA METHODS 5r5 FIGURE 2I. . Betbre proceeding. the fbllowing techniques can be employed to efficiently evaluate definite integrals involving functions that are generally smoothbut exhibit regionsof abruptchange.I 21. Thus.o.^^o. Implementation of suchapproaches requiresthat an estimateof the local truncationerror be obtainedat each step.r<lmant hn< nrnnt ^l.e control.This error estimatecan then serveas a basisfor either shortening or lengthening the stepsize. . Algorithms that automatically adjustthe stepsizecan avoid suchoverkill and hencebe of great advantage. suchcoses. .I A^ exomo e o i o s o.Stephalving involves taking eachsteptwice. they are said to have adaptivestepsii. the methods describedin this chaptercan also be usedto evaluatedefinite integrals. once as a full stepand then as .The evaluationof the definite integral t: _t rb Ja I J$tdx is equivalentto solving the differentialequation dy /rr.r ' o r o f o n C D I t h a le x h i b i t s o n o b r u p tc h o n g e .A u t o m o i i c stepsize nli.r^^. There are two primary approaches to incorporate adaptivestepsize control into onestepmethods.we should mention that aside from solving ODEs. Because they "adapt" to the solution'strajectory.
r.calledetnbedded RK methods. Severalof theseare availableas builtin functionsin MATLAB. In the secondapproach.516 ADAPTIVE METHODS AND STIFF SYSTEMS two half steps.2) (2t. Thus.c 1) I * (*5kr t 6kz + 8k1.1) t1 : . These are cun'entlythe methodsof choicebectruse they ale more efficient than stephalving.nvented this problernby deriving a fifthorderRK methodthat employsmost of the samefunctionevaluations requiredfor un accompanying fourthorderRK rnethod. The error is estimated as Er+t : where k q : .) (2Lla) r.tb) ir") ( 21 .andfifthorder predictionamountsto a total of I 0 function evaluations per step[recallEqs. t r : f ( r .r. The differencein the two resultsrepresents an estimateof the localtruncation eror.Hence.:f(.gk+)h /l (71. The ode23 function usesthe BS23 algorithm (Bogacki and Shampine..they aresometimes refen'edto as RKFehlbergmethods. The embedded methodswere first developed by Fehlberg.*)r. the local truncation enor is estimatedasthe differencebetweentwo predictions usingdiff'erentorder RK methods.2a) Note that althoughthereappearto be fbur function evaluations..a fburth. ode23. r . other even betterapproaches havebeen developed. Fehibergcleveriy circur.l MATTABFunctions for Nonstiff Systems Since Fehlbergoriginally developed his approach. For example.45)1.* ' r r . thereare really only three because after the first step.the ideaof usingtwo predictions of different order might seemtoo computationally expensive.. The step size can then be adjusted basedon this error estimate.44) and 120.2h t 3kz 1. f ( t i + t .1994).r) (2t.kt)lt (2 r. the approach yields a predictionand error estimate basedon threeevaluations rather .which simultaneously usessecondand thirdorderRK fbrmulas to solve the ODE and make error estimates for stepsize adjustment. Shampine. the approach yieldedthe error estimate on the basis of only six functionevaluationsl 2l. At facevalue. The formulas to advance the solution are J'i+r : li t where I n(. 1989. Thus.the k1 for the presentstepwill be the ftafrom the previous step./(r.) ' i + t ) lr.l.(20.
the error is checkedto determinewhetherit is within a desiredtolerance. Note that .(dydt. the stepis repeated error satisfies E < n t a x ( R e l T ox l l y l .2I . It.y) 4*exp(0.The simplestapproach is Ir.8*L) 0.'1 variables and each wherey is the solutionarraywhereeachcolumn is one of the dependent vector the name the function row corresponds to a time in the column t. = ocle45(odefun.2.21 . the ODE is typically stored in its own Mfile when . The ode45 functionusesan algorithmdeveloped by Dormandand Prince(1990). .3390 As described in the following example.lA 4).a1 is accepted. if it is entered as a vectorof two numbers. ode4 5 can be usedto generate the sameresultnumericallyas >> dydt=G(t. odefun is of vector returninga column of the righthandsides of the difTerential equations. A b s T oIl (21.33896.y1=66"4!. Second.MATLAB recommendsthat ode45 is the best function to apply as a "first try" for most problems. which simultaneously usesfburth.to obtain solutionsat specific times t0.r.First. the integrationis perfornredfrom ti to tf.r": 4e08t. spancan be lbrmulatedin two ways. fspar Ltt tf). Recallfrom Example 20. odeL13. fiom /:0 to 4 with an initial conditionof y(0):2. After each step. Observethat the criteriafor the relativeeffor usesa fractionratherthan a percentrelativeerror as we havedone on many occasions prior to this point.0. and k+ becomes k1 lbr the next step. >> y(Length(tt ) 75. Representing the ODE as an anonymousfunction. tl tn (all increasing or all decreasing). Herc is an example of how ode45 can be used to solve a single ODE. It is useful for stringentemor tolerances or computationallyintensiveODE functions. the value of y.If the error with reducedstepsizesuntil the estimated is too large. t span specifies the integrationinterval.'d) t. The ode113 functionusesa variableorder AdamsBashforthMoulton solver. ode45.andfifthorder RK formulasto solvethe ODE and make error estimatesfbr stepsizeadjustment.and y0 : a vectorcontainingthe initial values. r.5. tspan.5*y.I ADAPTIVE RUNGEKUTTA METHODS 5r7 than the five that would ordinarily result tiom using second(two evaluations) and thirdorder (tlrreeevaluations) RK formulasjn tandem.I that the analyticalsolutionat t:4 is 75.If it is.3) j) (default: l0 where RelTol is the relativetolerance andAbsTol is the absolute tolerance (default : l06). that this is a multistep as we will describe subsequently Thesefunctionscan be calledin a numberof differentways. >:.Note n'rethod in Section21. use tqpal = ltA t1 tn).
1'2 0.:l.5r8 EXAMPLE 2l . spdn fn 20 .l ADAPTIVE METHODS AND STIFF SYSTEMS Using MATLABto Solve o Systemof oors ProblemStotement.6)rlz Solution.y) *y\2\ = l'1.y) whiclr yieldsFig.3*y(1) *y(2) I .2ytE: : 1 at 1 : 0.A. The results canbe displayed by typing simply >1 plot(t.6*y(1) . Next.m over the range definedby tspan usingthe initial conditions found in yo. Employ ode45 to solvethe following setof nonlinear ODEsfrom t:0to20: 0. you must createa functionto compute the righthandside of the ODEs. dt'' dv. The solvercan then be invoked by This command will then solve the differential equationsin predprey. 0. m. where .3y1y2 . 2 Solution of predotorprey moo'el withMATLAB . enter the following commandsto specify the integrationrange and the initial conditions: '.B"y(2)+0. >> ya = 12. 7l. F I G U R2 EI . We storedthis Mfile underthe name:predprey. One way to do this is to createan Mfile as in yp r uflCLi on yp = pr eopi e/ ( L.2*y lI) 0.r'1: 2 and. Suchequationsarerefered to aspredatorpre)'cquations.2. Before obtaininga solutionwith MATLAB.81'* . 21.
rnirialsrep' 'MaxSf ' Allows you to adjustthe relativetolerance.'par ' . !.METHODS RUNGEKUTTA 2I . tspan. In addition. The odeset function has the generalsyntax options = odeset ('parr' . To havecontroloverthese tions' parameters. it is also instructiveto generate versus each other by variables that is. tolerance Allows vou to adiustthe absolute the initial step. a plot of the dependent >> plot (y( :..This The maximum stepdefaultsto onetenth . areparameters turesof the solution. has the value vaf . 3 .This optionaldetermines automatically The solver your own... . 2 l .thereis alsono controloverthe differentialequaofthe integration.yl = ode45(odefun. .I ADAPTIVE 5r9 plota phaseplane In addition to a time seriesplot.v (:. 1). 3 wiihMATLAB model plotof predoforprey Sloie spoce ?.vaf ' .) par.2 1 oot ? y1 to control varthe MAILAB solverusesdefaultparameters As in the previousexample. ious aspects areincludedasin additionalarguments features. p2. you to set lows of the t span interval. andpJ . A completelisting of all the possibleparawhere the parameter meterscan be obtainedby merely enteringodeset at the commandprompt.vaf."') where options is a datastructurethat is createdwith the odeset function to control feathat you want to passinto odefun. F I G U R2 EI . options' p1' p2.2) ) w h i c h v i e l d sF i s .. yA. Some comare monly usedparameters 'RelTol ' 'AbsTol ' . lt.
( L .075  r 0.er'.2 ) / ( 2 * . 2l. MATLAB automaticallycreatesa plot of the resultsdisplayingcirclesat the valuesit hascomputed. F I G U R2 EI .5. As in Fig. 4 (b). Note that we have not setthe function equalto output variables tr . When we implement one of the ODE solversin this way. tA 4l . First. 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 e o l0 a: >> >> optlons=odeset ode23(@dydt. y) y P = 1 O * e x p( . 4). we can implementthe solverwithout settingthe options.Hencethe defaultvaluefor the relativeenor ( l0 3) is automatically used: >> ode23(@c1ydr. ' As in Fig.2 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 Options ProblemStotement.2 I * ( t .the solvertakesmore snall steps to attainthe increased erccuritcy. Use ode23 to solvethe followine ODE. Solution of CDEwithMATLAB For o smoiler relotive error toleronce isuseo monv ononence no'esteo:oerol.from I : 0 to 4: /t al 2 dt : 10e {r l) [2r0.4a. Ie4) .5). ( 'ReLTol t0.a .4b.6t where1(0) :0. 2 l . . A 7 5 ^ 2 ) ) 0 . 0. options). 0. Solution. noticehow ode2 3 takes relatively large stepsin the smoothregionsof the solufion whereasit takessmaller stepsin the region ol'rapid changearoundI : 2. y I . Obtainsolutions for the default(103)and for a more stringent (l0r) relative error tolerance.we will create an Mfile to computethe righthand sideof theODE: function yp = dydt (t. 6 * y t Then. ( b ) R e l T o l= 1 0 .5.520 ADAPTIVE METHODS AND STIFF SYSTEMS EXAMPLE 2I .
)+ f (t.))+r rule as a corector [Eq.2. In this section. 21. Consequently. initial prediction.+rrt a future point 1.17)]: and the trapezoidal . 21.) f (t.5a).4) y!*.Multistep methodsexploit this informationto solve ODEs.5b). n (21 . * (21.2 MULTISIEP METHODS 52r 21..we will presenta simple secondorder method that servesto demonstrate the generalcharacteristics of multistepapproaches. one way to imstep on the accuracy of the rector depends prove Heun'smethodis to developa predictor thathasa local errorof Othi).v)h .y.s) Thus.11(Fig. the predictorand the correctorhavelocal truncationerors of O(/rr) and O(h3).1 The NonSelfSforfing Heun Method Recallthat the Heun approach uses Euler'srnethod as apredictor[Eq.15)]: :)'i*fQi. Alternative approaches. This can be FIGUR2 EI . arebasedon the insightthat.1 ' .2 MUITISTEP METHODS The onestepmethodsdescribed in the previous sectionsutilize information at a single point tr to predict a value of the dependent variable. (20. (20.The curvatureof the lines connectingthesepreviousvaluesprovidesinformation regardingthe trajectoryof the solution. 5 ond{b)multlstep beiween drfference of thefundomentol Grophicol depiction {o)onestep for solvingODLs merhods . valuable infbrmation from previous points is at our command. This suggests that the predictoris the weak link in the methodbecause iterative corgreatest is of the This weakness signilicant because the efficiency the error.. calledmultistepmethods once the computation has begun.2I .*. it has respectively. (Fig. 1 g: J'. 21.y.
f (ti. (21.thederivative in Eq. r .t'i)2h (2r. F I G U R2 EI . 21. This centering inrproves the local errorof the predictor to O1h3 1.a s i n lj+r : l'rr * . 6 (o)The A grophicol depiction of lhenon Heun method midpolnt method thot isused selfsfirrling The tropezoidol rrlethot isemployed oso predictor osc corrector {b) S l o p e./ ( r .5) and (21. . r . Eqs. dent variable)rr. Sucha valuewould not be available in a typicalinitialvalue Becauseof this fact.522 ADAPTIVE METHODS AND STIFF SYSTEMS froma accomplished by usingEuler's methodand the slopeat. As depicted in Fig. (21.6) are called the nortselfsterting Heunmethod. * . ) EXA S4ri .6) is now located estimate at themidpoint ratherthan at the beginningof the interval over which the predictionis rnade.6) This fornrulaattainsO(h3) at the expeuse of employinga largerstepsize2h.'!. In addition. f * . and extrainformation previous p o i n t l ' r .. note that the equationis not selfstarting because it involvesa previousvalue' of thedepenproblem.6.
2.313749 r 6.8) Now. are the final results of the corrector The iterationsare terminatedbasedon an estimateof iterationsat the previoustirne steps. The predictortEq.313149  lx  r 0 0 ":/3 ".2.2 MULTISTEP METHODS Thenonselfstarting Heunmethod canbe summarized as (Fig. ( 2 1 .tl zT srtr.607005 is then usedto conpute the varlue: The conectorIEq.J7c .1) (21 .s49331 which represents a true percentrelativeerror of 5. (21.6a): f'j'*r: .That i.ltr 523 (2t.6b): 1 l/+r : _1'. l" Lr((ir ..54933 3 1 4.9): mined usingEq..0 .8) ' ) 't(t" ''t/'") J (r'''1"' ' /r (for.t . T h e u s eo f E q s .An approximate estimateof the error can be deter(21.5(2)] 2 : 5.139o(true value :6.54933 6. we multithe initial conditionat / 0 is t' However.8)] vi:2+ jlittr 1"0 0 .. I t il 'i+l lo t_1.313149 which represents an error of 1.. Eq. l" : : 2.s. the approximateerror.l e r ) x ' I ' . . 7 )t h r o u g h i s nd a n O D E d e r r r o n s t r a tie the followine examnle.1Solc incun'ed Heun.+ n " * ' . A s w i t h E x a m p l e integrate 20.""nr) where the superscripts denotethat the correctoris applied iteratively fiom 7 : I to la to obtain refined solutions.5(6. . the iterationsareterminated.7)lis usedto extrapolate linearlyfrorn r : l to 1: . o o z o o .21.l:1. canbe appliediteratively to irnprove the solution: l^ .'lr Predictor + t' (t.2I . Use the nonselfstarting Heun methodto performthe samecomputations peltbrmed previously were in as Example20. Note that r'j" and y'l_.0.t .2l .r : 0.194631).6.2 using Heun's method. yi)Zn Corrector (Ftg.3 NonSelfStortin H ge u n ' sM e t h o d Problem Stotement.3929953+ [4"'r'iu' .5 t f r o m / : 0 t o 4 w i t h a s t e ps i z eo f l .s t 2 I : 6.trrli . This in the selfstartins error is somewhat smallerthan the valueof 8.3929953 at / : 1. because arenow dealingwith a stepmethod.0. (2 1.we requirethe additionalinfbrmationthat1 is equalto 0. EXAMPLE 2l . 5 ( 2 )J . Solution. 9 ) t os o l v e p o i n t .T : m .'/+l r.927o. 9 ) When lenI is lessthana prespecified At this error tolerance e. 121.!i+ r I x l00lo ( 2r . s t s .o l) 1 I : 6.)'.( 2 1 .
1r.1ir *r.As was the casewith the Heun method (recall Example 20.097o).5(6.) : ht l"l€.13) ro yield (21.the predictoris sitt .36087(et : 2.2.falls belowa prespecified value ofe.r{.un b..1 7 r .524 ADAPTIVE METHODS AND STIFF SYSTEMS (21.1'.) (2I.0. 1 (2t.9+r 0: . the error estimatethen providesa criterionfbr changingthe sfepsize. 21./ 1r ' ..This errorestinrate 7r designates can be cornbinedwith the estimateof )'i+ r from the predictorstepto yield value:t.68%).and subsequent iterationsconvergeon the sameresultas was obtainedwith the selfstarting Heunmethod.ructed fromEq.. : ih ) ' t ' ( { . the iterationsconvelge on a value of 6. (t.4) t.8)canbe appliediteratively Equation until e. (21.its localtruncation erroris (Table17. 1 andf .36087)2: .r.il) By recognizingthat the correctoris equivalentto the trapezoidalrule.r.44346 €t :9...dividing Eq.y.10) where the subscript that this is the error of the predictor.14) { is now between by 5 andreanangingthe gives result ) . .(21..ub.0826(et : l87o) rhar was computedwith the original Heun method.2 Error Estimqtes Aside from providing increased efficiency. ) (21.43a/c wlrich is superior to the prediction of 12. :1. r ' ' " ' { € .. r i i ' : .The first correctoryields 15.(21.]'l': 2 * l4ett 13. . t : ..the rate of convergence 15.76693 (e. I 6 'r.15) .14) where r. For the secondstep.r (21. the rnultistepmethodconverges at a somewhatfasterrate. Hence.U. : 3. Now.2).the nonsellstarting Heurrcan alsobe used to estimate the local truncation ernrr. .As with the adaptive RK methods in Section 21.As with tlre previousstep.l t ' f " t € .8vc). .30224 of theconector is somewhat irrprovedbecause of the betterinitial prediction.tr.13) .r u r r r 1 6 ) < It (2r.1. a similarestimate of the local truncationerror lbr the correctoris (Table 17.6.(3){6.l2l Thiserrorestimate canbecombined with theconector result to give .ift3.1 Equation I) .t'. 9 +r.u. The error estintate can be derivedby recognizingthat the predictoris equivalent to the midpointrule.1r True + fir.because the initial predictorvalueis more accurate.11 True value: )'. However.2) ll E.
EXAMPLE 21.15) are identical.13.multistep methods are usually not the method of choice for most problemsroutinely confrontedin engineeringand science.84392. which compares .194631 0.3.360865.3 STTFFNESS Stiffnessis a specialproblem that can arisein the solution of ordinary differentialequatogetherwith slowly tions. (21.. Chapra and Canale.3 STIFFNESS 525 Notice that the righthandsidesof Eqs.194631and 14.with the exception of the argumentof the third derivative.the MATLAB function odel13 is a multistep method.the lefthandsidesshouldalso be equivalent.For example.6.2I .5.44346 can be usedto comDute F _ .45831.30224 : 0.That said. 2r. we have arrived at a relationshipthat can be usedto estimatethe perstep error on the basis of two quantitiesthat are routine byproductsof the computation:the predictor(1. Additional information can be found elsewhere(e.g. the predictorgives5.1662341 and the correctoryields 15. The foregoing has been a brief introduction to multistep methods. Use Eq.360865 : Et : 6.84392. Et : 14.16) to estimatethe perstep respectively.607005 into Eq. after which the solutionbecomes .If the third derivativedoesnot vary appreciably over the interval in question. ple 21.12) and (21. and therefore.9*r ) anAthe corrector("vlir) .) it . Although they still have their place for solving certain types of problems.30224.l6) to give Thesevaluescan be substituted F_ .0 _ rit 1'ri (l l.44346 15. they are still used. In some cases.we can assumethat the righthandsidesare equal. 2006).We have thereforeincluded this sectionto introduceyou to their basic principles. Note that the true valuesat / : I and 2 are6.. At /.360865 : 0. (21. the predictorgives 13.16) truncation Thus.as in E ' 5.11 :2.15.607005 6. A stiff systemis one involving rapidly changingcomponents changing ones.the rapidly varying componentsare ephemeraltransients dominatedby the slowly varying that die away quickly. and the comector Solution.a1 : l.4 Error Estimqte Truncotion of PerSteo truncationerror of ExamProblem Stotemenf . yields 6.150722 well with the exacteffor.30224: 0.which At /. (21.31176 which also compares favorably with the exact error.
005). (21.t 2  . this transient dies out and the solutionbesomes (l e / ). Both individual and systems of ODEs can be stilT.17) If I (0) : 0.l't+l :)'i alih .1 0 0 0 e ' (2r.)t). w h e n h e r e : p o n s e is viewed on the finertimescole in the insel. n dt Suhstitutin E gq .1 by the fast exponential term(ermr). calculuscan be usedto determinethe solutionas t' : 1'ue ttl Thus. 21. l 9 t g i v e s . Euler'smethodcan be usedto solvethe sameproblemnumerically: ]i+t :)'it dt.t 2. the analyticalsolutioncan be developed as .17): tlrehor. : .. p e ' c e pi b l e e n l ./ r' .This . 1 0 0 0 .18) As in Fig.the solutionis initially dominated governed After a shortperiod (t < 0.526 ADAPTIVE METHODS AND STIFF SYSTEMS D l o 'o r o .nogeneous aminirrg dt' dt EX (2t. 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.An exampleof a singlestitT is ODE . Although the transient existfor only a shortpart of theintegration interval. 7 phenomena components.they can dictatethe time steplbr the entire solution. . b y t h e s l o we x p o n e n t i a Insight into the stepsize requiredfor stability of such a solutioncan be gained by expart of Eq.oo2e' . . F I G U R2 EI .t9) If f'(0) .1 0 0 0 r '* . the solution startsat ).0and asymptoticallyapproaches zero.998el{){x (21.l : 3 0. there tronsienl octuo ly o fost ircnsieni from y : g 1e I thct occursin lessthnn the 0 OO5lime unil.
J. 'dt ll This is calledthe baclovutd.i+rf 3000 _ )eeegti+t1p .002would resulr in a totally unstablesolutionthat is. (a) Use theexplicitEulerwith stepsizes of 0. Usingft > 0. (a) For this problem.\'i+r t'i(l . UseboththeexplicitandrmplicitEulermethods ro solveEq.006.e. regardless of the size of the step. when the stepsizeis increased to a valuejustbelow the stability limit (ft : 0. 21. l9) yielcls .the solution manifests oscillations. T h u s .'l:\'. . I 8 ).4. the explicitEuler'smethocl is f (_1000]. For the fast transient part of Eq.11). Euler'smethotl. Such representations are called intplicit because the unknown appears on both sidesofthe equation. it would go infinite as the solutionprogressed. Solution.0015). (2 I . 2 / a .+t : )'i ali+th which can be solvedfbr \ij . Ratherthan usingexplicit approaches. In addition.a bounded thiscriterion maintains stability solution).yi* (_1000]. or inrltlicir. d. an evensmaller stepsize would be requiredto obtainan accurate solution.r+r .rould note that. wherer'(0) : 0..3 STIFFNESS 527 or : . Thus.ah\ e . l .we sl.0005 is displayed in Fig. Although it exhibitssometruncationelror.05 to solve fbr I' between 0 and0.i * 3000 _2000e ')h J.rr.'i  called unc on di t i o n al I v stubl e. In contrast.0005 and0.o o a si .0015to solvefor . EXAMPLE 2I 5 E x p l i c io t nd lmpliciE t uter ProblemStotemeni. y r l .\ll: I t(tn 1ll.002. it controls themaximumallowable stepsize.20) The stabilitl'of this formulaclearlydepends on the step sizeh. the approach is For this case.t'between/ : 0 and 0. the resultcaptures the generalshapeof the analytical scllution.r+r: . Hence.r. Substituting Eq.2I .An implicit forrn of Euler's method can be developedby evaluatingthe derivative at the future tirne: \'.2ll + 0 as r .althoughthe transient occurs1oronly a sntallfi'action of theintegration interval.(b) Use the implicit Euler with a stepsizeof 0. this criterioncan be usedto show that the step size to maintainstabilitymust be <2/1000:0.i f h : .r The result for h:0. implicit methocls of'teran alternative remecly.(21.> oo.)..8a along with the analyticalsolution. whereas (i.That is.. (2 I . l_r.a/zl mLrst be l e s st h a n l . (b) The implicit Euler'smerhodis : . ll .+ o o .r.
05 is displayed in Fig.11is isolated on the lefthandside: Li't ]i F 3000/r.E 2r..theexactsolutionis .3 0.s28 ADAPTIVE METHODS AND STIFF SYSTEMS .gge302.5 I 0. Systemsof ODEs can also be stifT.r1. An exampleis dyt __:__5r. : dr .83er.22h) For the initial conditions .2 (bl 0.1 0.v 1. 2l.22a) (2t.23b) .67 r302.g.e8eel + 65.4 FTGUR.96e3.8b alongwith the analytical solution.002 0 0.:(0) : 83.8 S o i u t i oo nf o s t i fC f D Ew i t h( o )t h ee x p l i c o i t n d ( b )i m p l i c E i t u l em r ethods Now because the ODE is linear.5 0 ) z 0.82.^ dr dv .e8eet : lJ .29and l. Notice that even though we have useda much bigger stepsize than the one that inducedinstability for the explicit Euler.we can rearrange this equation so that y.rr(0) : 52.01t)1t lz (21.{\10tt lt : 52.l0ly2 1001'1 (21. the numericalresult tracksnicely on the analyticalsolution.23a) (2t..)QQQlls ''t'r l+roooft The resultfor h :0.
3 STIFFNESS 529 Note that the exponents are negativeand dilTerby abouttwo ordersof magnitude. . This function is basedon a modified Rosenbrock formula of order 2. ode23tb. " . i +:tr ' 2 1 f ( 1 0 0 r ' 1 .althoughstabilityis gainedthroughimplicit irpproaches.Tltis is usedfor stiff problemsof low to medium accuracy. An implicit Euler's methodfbr systems can be formulatedfor the present exarnpleas . i + r ) / l (21. y 2 . For nonlinear ODEs. we can seethat the problem consistsof solving a set of simultaneous equationsfbr eachtime step.3.24a) (21. ode2 3 s. This is an implementation of an implicit RungeKutta formulawith a first stagethat is a trirpezoidal rule and a secondstagethat is a backwarddifferentiationformula of order2. 1 ' 1 . 1 ) / z Collecting terms gives ( l + .6.lt is a multistepsolverthat optionally usesthe Gear backwarddifferentiation forrnulas. it miry be more efficientthanode15s at crudetolerances. i + l: l r i * ( . \ ' r . 1 1 .. # p(r. The van der Pol equationis a model of an electroniccircuit that aroseback in the daysofvacuum tubes. systemof nonlinearsimultaneous equations Thus.2). .5 / z . a priceis paid in tlreform of addedsolutionconrplexity. 21. This function is an implementation of the trapezoidal rule with a "free" interpolant.the solutionbeconres evenrnoredilficult sinceit involvessolvinga (recallSection12. it is the largeexponents thatrespond rapiclly andareat the heartofthe system's stiffness. a n d ( b )f o r 1 t : 1 0 0 0 .. \ ' 2 . .r'1 1 11 ' .' * r3 ) ' : .This is usedfor moderately with low accuracy whereyou needa stiff problems solutionwithoutnumerical danrping. EXAMPLE 21 6 MATTAB for StiffODEs Problem Stotement. u s eo d e 4 5t o s o l v e f r o m / : 0 t o 2 0 .i: Thus.5 . .. It can solvesomekindsof stifTproblents betterthanodel5s.25a) (2L25b) 100. u s eo d e 2 3 st o solvefrom/:0to6000. .Theseare odel5s.24b) .3 0 1 . As with the singleequation.2I . t i a l c o n d i t i o n s.:o (E21. This function is a variableorder solverbasedon nume