CONTENTS About the Aufhor Prefoce
iv
xiii
Guided Tour
xvii
PanrOur Modeling,Computers, ond ErrorAnolysis I l. I Mo...
1723 downloads
7109 Views
29MB Size
Report
This content was uploaded by our users and we assume good faith they have the permission to share this book. If you own the copyright to this book and it is wrongfully on our website, we offer a simple DMCA procedure to remove your content from our site. Start by pressing the button below!
Report copyright / DMCA form
CONTENTS About the Aufhor Prefoce
iv
xiii
Guided Tour
xvii
PanrOur Modeling,Computers, ond ErrorAnolysis I l. I Motivofion I 1.2 Port Orgonizotion
2
CHAPTER I Mothemqticol Modeling, Numericol Methods, ond Problem Solving 4 I . l A S i m p l eM o t h e m o t i c M o lo d e l 5 1.2 Conservotion Lowsin Engineering ond Science 12 1.3 NumericolMethodsCoveredin ThisBook l3 P r o b l e m s1 7
CHAPTER2 MATLAB Fundomeniqls 2.1 2.2 2.3 2.4 2.5 2.6 2.7 ^
20
TheMATIABEnvironmenl21 A s s i g n m e n t2 2 Mothemoticol Operotions 27 U s eo f B u i l t l nF u n c t i o n s3 0 Groohics 33 OtherResources36 CoseStudy:Explorotory DotoAnolysis 37
l l
rroblems JY
CHAPTER3 Progromming with MATTAB 42 3.1 M-Files43 3.2 InputOutput 47
CONTENTS
vl
3 . 3 S h u c t u r ePdr o g r o m m i n g5 l 3 . 4 N e s t i n go n d I n d e n t o t i o n6 3 g u n c t i o nt os M - F i l e s 6 6 3 . 5 P o s s i nF 3.6 CoseStudy:BungeeJumperVelocity 71 Problems 75
4 CHAPTER Roundoff qnd Truncotion Errors 79 4.1 ErrorsB0 4.2 RoundoffErrors 84 4.3 Truncotion Errors 92 ,l03 4 . 4 T o t oN l u m e r i c oEl r r o r 4 . 5 B l u n d e r sM, o d e lE r r o r so, n d D o t oU n c e r t o i n t y1 0 8 ,l09 Problems
.|11 PnnrTwo Roots ondOptimizotion 2.1 Overview tll 2.2 Port Orgonizotion
I 12
CHAPTER 5 Roois: Brocketing Methods
I l4
5 . 1 R o o t si n E n g i n e e r i nogn d S c i e n c e I l 5 5 . 2 G r o p h i c oM l ethods I l6 g e t h o d so n d I n i t i oG 5 . 3 B r o c k e t i nM l u e s s e s1 1 7 5 . 4 B i s e c t i o n1 2 2 5 . 5 F o l s eP o s i t i o n 1 2 8 5.6 CoseStudv:Greenhouse Gosesond Roinwoter 132 ,l35 Problems
CHAPTER 6 Roots: Open Methods
| 39
6 . 1 S i m p l eF i x e d - P o ilnt ei r o t i o n 1 4 0 6 . 2 N e w t o n - R o p h s o1n4 4 6 . 3 S e c o nM t ethods 149 6.4 MATLAB F u n c t i o nf :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 i1o7n0 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 i1o7n9 7 . 4 C o s eS t u d yE : q u i l i b r i uomn d M i n i m u mP o t e n t i oEln e r g y l 8 l P r o b l e m s1 8 3
,|89 PrnrTxnrr LineorSystems 3.1 Overview 189 3.2 Port Orgonizotion l9l
CHAPTER 8 Lineqr Algebroic Equofions ond Motrices
| 93
8.1 MotrixAlgebroOverview 194 with MATLAB 203 8.2 SolvingLineorAlgebroicEquotions ond Voltogesin Circuits 205 8.3 CoseStudy:Currents Problems 209
9 CHAPTER Gouss Eliminotion 212 9 . 1 S o l v i n gS m o l N l u m b e r so f E q u o t i osn 2 1 3 9 . 2 N o i v eG o u s sE l i m i n o t i o n2 1 8 9.3 Pivoting225 9 . 4 T r i d i o g o n oSly s t e m s2 2 7 9.5 CoseStudy:Model of o HeotedRod 229 Problems 233
IO CHAPTER [U Foctorizotion
236
l 0 . l O v e r v i e wo f l U F o c t o r i z o t i o n2 3 7 1 0 . 2 G o u s sE l i m i n o t i oons l U F o c t o r i z o t i o n2 3 8 1 0 . 3 C h o l e s kF y o c t o r i z o t i o n2 4 4 'l0.4 MATLABLeftDivision 246 Problems 247
CONIENTS
vIt!
CHAPTERI I Motrix lnverse ond Condition
249
1 i .l TheMotrix lnverse 249 I I .2 ErrorAnolysisond SystemCondition 253 I 1 . 3 C o s eS t u d yI:n d o o rA i r P o l l u t i o n2 5 8 Problems 261
CHAPTER I2 Iterotive Methods
264
l2.l LineorSystems: Gouss-Seidel264 ,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 oRl e o c t i o n s2 7 7 P r o b l e m s2 7 9
PtrnrFoun CurveFitting 281 4.1 Overview 281 4.2 Port Orgonizotion
283
CHAPTER I3 Lineor Regression 284 1 3 . I S t o t i s t i cRse v i e w 2 8 6 1 3 . 2 L i n e o Lr e o s t - S q u oR r eesg r e s s i o n2 9 2 ,l3.3 L i n e o r i z o t i o nf N o n l i n e oR r e l o t i o n s h i p3s0 0 1 3 . 4 C o m p u t eAr 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 : n z y m eK i n e t i c s 3 0 7 ^
l l
rroDtems 5 | z
CHAPTER I4 Generol lineqr Leosf-Squores qnd Nonlineqr Regression 316 1 4 .I P o l y n o m i o Rle g r e s s i o n3 16 1 4 . 2 M u l t i p l eL i n e oR r e g r e s s i o n3 2 O 1 4 . 3 G e n e r oLl i n e oLr e o sSt o u o r e s 3 2 2 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 : i t t i n gS i n u s o i d s3 2 8 h
l l
rrontcms
. 1 . 1/
CONIENIS
'I 5.'l lntroduction to Interpotofion336 I5.2 NewronInterpoloring polynomiol 33g polynomiol j: .j tosron9etnrerpoloring 347 rJ.4 tnversetnterpolotion 350 I J.J txkopolotion o n d O s c i l l o t i o n s3 5 1 Problems 355
t6 cHAPTER Splines ond piecewise Inferpofofion I 6 . I l n k o d u c t i otno S p l i n e s 359 l o . z L i n e aSr p l i n e s3 6 1 1 6 . 3 Q u o d r o i i cS p l i n e s 365 1 6 . 4 C u b i cS p l i n e s 3 6 8
359
inMATLAB l9: liTewiseInrerpotorion
374 i 6.6 Multidimensionol Interpolotion 37g l6 Z CoseStudy:HeotTronsfer 3g2 rrobtems 386
PnnrFvr Infegrotion ond Differentiotion3g9 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 Newton-Cotes Formutos 396 17.3 TheTropezoidol Rule 39g 1 7 . 4 S i m p s o n ,Rsu l e s 4 0 5 17.5 Higher-Order Newfon_Cotes Formulos 4j j l7 6 lntegration with UnequolSegments 412 17.7 OpenMerhods 416 I 7 8 M u h , p l eI n t e g r o l s 4j 6 CompuringWork wirh Numericol lnregrorion 4j9 ;lJ;:r";udy: t I
!
t I
i{.
Numericof Integrotion of Functions 'l 8.I Introducfion 426 1 8 . 2 R o m b e r Iqn i e q r o t i o n 427
CONTENTS 1 8 . 3 G o u s sQ u o d r o t u r e4 3 2 I 8 . 4 A d o p t i v eQ u o d r o t u r e4 3 9 I8.5 CoseStudy:Root-Meon-Squore Current 440 Problems 444
CHAPTER I9 Numericql Differentiqtion
448
19.I 19.2 I9.3 ,l9.4 ,l9.5
Inhoduction ond Bockground 449 High-Accurocy Differentiotion Formulos 452 R i c h o r d s oEnx t r o p o l o t i o n4 5 5 Derivotives of UnequollySpocedDoto 457 Derivotivesond lntegrolsfor Dofo with Errors 458 19.6 PortiolDerivotives 459 I9.2 NumericolDifferentiotion with MATLAB 460 I 9 . 8 C o s eS t u d yV : i s u o l i z i nFgi e l d s 4 6 5 Problems 467
Pnnr5x
OrdinoryDifferentiol Equotions473 6.1 Overview 473 6.2 Porl Orgonizofion 477
CHAPTER 20 Initiol-Volue Problems
479
20.I Overview 481 20.2 EuleisMethod 481 20.3 lmprovemenls of Euler'sMethod 487 20.4 Runge-Kutfo Methods 493 20.5 Systems of Equotions 498 20.6 CoseStudy:Predotory-Prey Modelsond Choos 50A Problems 509
CHAPTER 2I Adopfive Merhods ond Stiff Systems
514
21 .'l AdoptiveRunge-Kutto Methods 514 2l .2 MultistepMethods 521 2l .3 Stiffness525 2l .4 MATLAB A p p l i c o t i o nB: u n g e Je u m p ew r i t hC o r d 5 3 1 2l .5 CoseStudy:Pliny'slntermittent Fountoin 532 r r o D l e m s3 J /
CONTENTS
xl
22 CHAPTER Boundory-Volue Problems
540
22.1 lntrodvction ond Bockground 541 22.2 lhe ShootingMethod 545 22.3 Finite-Difference Methods 552 P r o b l e m s5 5 9
APPENDIXA: EIGENVALUES 565 APPENDIXB: MATLABBUILT-INFUNCTIONS 576 APPENDIX€: MATIAB M-FltE FUNCTIONS 578 BIBLIOGRAPHY579 rNDEX 580
Modqling,CoTpute''i, qnd Erior Anolysis
t.t
MoTtvATtoN What are numericalmethodsand why shouldyou stridythem? Numericalmethodsare techniquesby which mathematicalproblemsare formulatedso that they can be solved with arithmeticand logical operations.Becausedigital computers excel at perform.ingsuchoperations.numericalmethodsare sometimesreferredto as computer mathematics. In the pre-computerera, the time and drudgeryof implementingsuchcalculationsse.-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 foundationsin the other areasof mathematicsand science, we should also have a fundamentalunderstandingof numerical methods.In particular,we should have a solid appreciationof 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 handlinglarge systemsof equations.nonlineari, , d.l, and complicated geometriesthat are not uncommon in engineeringand scienceand that are often impossibleto solve analyticallywith standard calculus.As such"they greatlyenhanceyour problem-solving skills. 2. Numorical methods allow you to use "canned" so-ftwarewith insight. During your career,you will q*"
AND ERROR ANALYSIS PARTI MODELING,COMPUTERS,
3.
4.
5.
computerproinvariablyhave occasionto use commerciallyavailableprepackaged grarnsthat involve numericalmethods.The intelligentuseof theseprogramsis greatly enhancedby an understandingof the basic theory underlyingthe methods.In the absenceof suchunderstanding,you will be left to treat suchpackagesas "black boxes" with little critical insightinto their inner workingsor the validity of the resultsthey produce. Many problemscannotbe approachedusing cannedprograms.If you are conversant with numerical methods,and are adept at computer programming,you can design your own programsto solveproblemswithouthavingto buy or commissionexpensive software. Nr.rrnerical methodsare an efficient vehiclefbr learningto usecomputers.Becausenumericalmethodsale expresslydesignedfor computerimplementation,they areidealtbr illustratingthe conrputer'spowers and limitations.When you successfullyimplement numericalmethodson a computer,and then apply them to solve otherwiseintractable problenrs,you will be plovided with a dramaticdernonstrationof how computerscan serveyour professionaldevelopment.At the sarnelime, you rvilI alsolearnto acknowledge and control the errors of approximationthat are part and parcel of large-scale numericalcalculations. Numericalmethodsprovidea vehiclefbr you to reinforceyour understandingof mathernatics.Becauseone tunction of numericalmethodsis to reducehigher mathematics to basic arithmetic operations.they get at the "nuts and bolts" of some otherwise obscuretopics. Enhancedunderstandingand insight can result from this alternative perspective.
how numerical With thesereasonslls motivation.we can now set out to understand methodsand digital computerswork in tandemto generatereliablesolutionsto mathematical problems.The remainderof this book is devotedto this task.
1.2 PARTORGANIZATION This book is divided into six parts.The latter five partsfocus on the major areasof numerical methods.Although it might be temptingto jump right into this material,Part One consistsof four chaptersdealng with essentialbackgroundmaterial. Chapter 1 providesa concreteexampleof how a numericalmethodcan be employed to solve a real problem. To do this, we develop tt muthematicalmodel of a fiee-falling bungeejumper. The model, which is basedon Newton's secondlaw, resultsin an ordinary differentialequation.After first using calculusto developa closed-formsolution,we then show how a comparablesolution can be generatedwith a simple numerical method.We of numericalrnethodsthat we cover in end the chapterwith an overview of the major areers PartsTwo throughSir. Chapters2 and 3 provide an introduction to the MATLAB' software environment. Chapter2 dealswith the standardway of operatingMATLAB by enteringcommandsone at a time in the so-calledt'alculatornuttle.Thisinteractivemodeprovidesa straightforward meansto orient you kl the enviroumentand illustrateshow it is usedibr common opera-
I.2 PART ORGANIZATION Chapter-l shows how MATLAB's programming mode providesa vehicle for assembling individualcommandsinto algorithms.Thus,our intentis to illustratehow MATLAB servesas a convenientprogrammingenvironmentto developyour own software. 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 in place of an exactmathematicalprocedure.
{.1:i
ffilT**
i.t "
TFi +
i'i,r I r,if i
..,,lrfi;
U1
I
Mothemoticol Modeling, NumericolMethods, ond ProblemSolving
I
4 CHAPTER OBJECTIVES The prirnary objectiveof this chapteris to provide you with a concreteidea of what numericalmethodsare and how they relateto engineeringanclscientificproblem solving. Specificobjectivesand topicscoveredare o r o
r
Learning how mathematicalmodelscan be formulatedon the basisof scientific principlesto simulatethe behaviorof a simplephysicalsystem. Understandinghow numericalmethodsirlford a meansto generatesolutionsin a rnannerthat can be irnplementedon a digital computer. Understandingthe different typesof conservationlaws that lie beneaththe models usedin the variousengineeringdisciplinesand appreciatingthe diff'erence betweensteady-state irnd dynamic solutionsof thesemodels. Learning aboutthe difterent typesof numericalmethodswe will cover in this book.
YOU'VE GOT A PROBTEM upposethat a bungee-jumpingcompanyhires you. You're given the task of predicting the velocity of a jumper (Fig. l.l ) as a function of time during the free-fall part of thejump. This inlbrmationwill be usedaspart of a largeranalysisto determinethe length and requiredstrengthof the bungeecord for jumpers of differentmass You know from your studiesofphysics that the accelerationshouldbe equalto the ratio of the tbrceto the mass(Newton'ssecondlaw). Basedon this insightandyour knowledge
r
1
FIG For
f-^^ l I cc-
ium
I .I A SIMPTE MATHEMATICAL MODEL
Upwardforce d u et o a i r resistance
t
of fluid mechanics,you developthe following mathematicalmodel for the rate of change o l ' v e l o c i t yr . r ' i t rhe s p e ctto t i m e . ducd. dt''m
til
vtil
Downward forcedue to gravrty
where rr : vertical velocity (n/s). r : time (s), g : the accelerationdue to gravity (:9.81nls21, ca: a second-orderdrag coetficient (kg/m), and m: the jumper's mass(kg). Becausethis is a ditlerentialequation,you know that calculusmight be usedto obtain an analyticalor exactsolutionfor u as a function of /. However,in the following pages,we will illustrate an alternativesolution approach.This will involve developinga con.rputerorientednumericalor approximatesolution. Aside from showing you how the computercan be usedto solve this particularproblem, our more generalobjective will be to illustrate (a) what numericalmethodsare and (b) how they figure in engineeringand scientificproblen solving.In so doing, we will also show how mathematicaln.rodels figure prominentlyin the way engineersand scientistsuse numericalmethodsin their work.
flGUREl.l Forces ociingon o lreeJolling bungee iumpet.
I.l
A SIMPTEMATHEMATICAT MODET A motlrcnntical ntodelcan be broadly definedas a tbrmulation or equationthat expresses the essentialfeaturesof a physicalsystemor processin mathematicalterms.In a very general sense,it can be representedas a functionalrelationshipof the fonn D e o e n d e n ( - / i n d e' .n,e, n c l e n t forcine \ .v.a' .n.a. ,o_l e, : J [ v a n a D t e s. p u r a n ) e t el rusn.c t l o n s| \ ,f
(l.l)
where the de;tendentvariable is a characteristicthat usually reflectsthe behavioror state of the system:-the independetttvariablesare usually dimensions.such as time and space, along which the system'sbehavioris beingdetermined;theparametersareretlectiveof the system'sproperliesor composition;and thelbrring.functiottsareexternalintluencesacting upon it. The actual mathematicalexpressionof Eq. (1.1) can range from a sirnple algebraic relationshipto largecomplicatedsetsof diff-erentialequations.For example,on the basisof his observations,Newton formulatedhis secondlaw of motion, which statesthat the time rateof changeof momentumof a body is equalto the resultantforce actingon it. The ntathematicalexpression,or model,of the secondlaw is the well-known equation F:ma
(t.2)
whereF is the net force actingon the body (N, or kg nls"), m is the n.rass of the object(kg), a n dc i i s i t s a c c e l e r a t i o(nr n / s : ) .
6
MODELING,NUMERICAL METHODS, SOLVING MATHEMATICAL AND PROBLEM The secondlaw can be recastin the format of Eq. ( 1.l) by merely dividing both sides by m to give F
(1.-31
n1
where a is the dependentvariablereflecting the system'sbehavior,F is the forcing function, and nr is a parameter.Note that for this simple casethereis no independentvariable becausewe are not yet predictinghow accelerationvariesin time or space. Equation( 1.3)has a numberof characteristics that aretypical of mathematicalmodels of the physicalworld. . .
.
It describesa naturalprocessor systemin mathematicalterms. It represents an idealizationand sirnplificationof reality.That is. the model ignoresnegThus, ligible detailsof the naturalprocessand focuseson its essentialmanif'estations. 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 scalesvisible to humans. can be usedfbr predictivepurFinally, it yields reproducibleresultsand,consequently, its poses.For example,if the force on an object and massare known, Eq. ( 1.3)can be usedto compLlteacceleration.
Becauseof its simple algebraicform, the solution of Eq. (1.2) was obtainedeasily. However,other mathernaticalmodelsof physicalphenomenamay be much more complex. and either cannotbe solvedexactly or requiremore sophisticatedmathematicaltechniques than simple algebrafor their solution.To illustrate a more complex model of this kind, Newton's secondlaw can be usedto determinethe terminal velocity of a free-fallingbody nearthe earth'ssurface.Our falling body will be a bungeejumper (Fig. 1.1).For this case, a model can be derived by expressingthe accelerationas the time rate of changeof the r,'elocity(tluldr) and substitutingit into Eq. (1.3)to yield duF dt
(1.4)
nr
where u is velocity (in metersper second).Thus, the rate of changeof the velocity is equal to the net force acting on the body normalizedto its mass.If the net force is positive,the object will accelerate.Ifit is negative.the objectwill decelerate.Ifthe net force is zero,the object'svelocity will remain at a constantlevel. Next, we will expressthe net force in terms of measurablevariablesand parameters. For a body talling witlrin the vicinity of the earth,the net force is composedof two opposing forces: the downward pull of gravity Fp and the upward force of air resistanceFy (Fig.1.1): F:
Fol
Fti
(1 . 5 )
If force in the downward direction is assigneda positive sign, the secondlaw can be u.sedto formulatethe force due to pravity as Fo:m8 whereg is the acceleration due to gravity(9.81m/s2).
(1.6)
I .I A SIMPLE MATHEMATICAL MODEI.
7
Air resistancecan be fbrmulatedin i.ivariety of ways. Knowledge from the scienceof fluid ntechanics suggests thata gtrodfirst approxirrration wouliJbe to assumethat it is proportional to the squareof the velocitl,, - l
f U:
-cdr)'
(1.7)
where r',1is a proporticlnalitvconstantcalled the drag coefticient(kg/m). Thus. the greater the fall velocity, the greaterthe uprvardfbrce due to air resistance.The parameterc./ accountslbr propertiesofthe ialling object,suchas shapeor surfaceroughness,that affect air resistance.For the presentc&s€,c,7might be a function of the type of clothing or the orientationusedby thejumper duringfree tall. The rlet fbrce is the differencebetweenthe downward and upwi.rrdforce. Therefbre, E q s .1 1 . 4 1 t h r o u g h( 1 . 7 )c a nb e c o m b i n e dt o y i e l d dt: ,
-,5
dt
ctt t
(1.8)
tn
Ecluation(1.8) is a ntodelthat relatesthe accelerirtion of a falling objectto the tbrces actingon it. It is a tli/Jerentialequtttiortbecauseit is written in terntsof the differentialrate of change(d uI dt 1 of the variablethat we are interestedin predicting.However,in contrast to the solutionof Newton'ssecondlaw in Eq. ( 1.3),the exactsolutionof Eq. ( 1.8)for the velocity of the jumper cannot be obtirinedusing simple algebraicmanipulation.Rather, more adt'ancedtechniquessuchas thoseof calculusnrustbe appliedto obtainan exactor analyticalsolution.For example,if thejumper is initially at rest(r.,: 0 at / : 0), calculus can be usedto solveEq. ( 1.8)for
:r,ff*"n( u(/) ,8,) yilt
(1.9)
/
where tanh is the hyperbolic tangent that can be either computed directlyr or via the more elementary exponential function as in
tanh-t:
e' - e-' e . ,* e \
(1.10)
Note that Eq. ( 1.9)is castin the generalfbrm of Eq. (1.1)wheret,(/) is the dependent variable.t is the independentvariable, ctt andm areparameters, andg is theforcing function.
EXAMPLE I .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 eJ u m p e rP r o b l e m ProblemStotement. A bungeejumper with a massof 68.1kg leapsliom a stationaryhot air balloon.Use Eq. ( 1.9)to computevelocityfbr the first l2 s of fiee fall. Also deterr-nine the terminal velocity that will be attainedfbr an infinitely long corcl(or alternatively,the jumprrasteris havinga particuiarlybad dayl).Use a dragcoefticientof 0.25kg/m.
I MATLABG'allows d i r e c rc a l c u l a t i o no f t h e h y p c r b o l i ct a n g e n vt i a t h c b u i l t - i nf u n c t i o nr d n h ( r ) .
MATHEMATICAL MODELING,NUMERICAL METHODSAND PROBTEM SOLVING Solution. Insertingthe parameters into Eq. (1.9)yields
which can be usedto compute
t, 3
0 2 4 6 B t0 12
u,mfs
o I B 72,92 33I I tB 42 4762 46 9575 4 S4 2 t4 50 6175 516938
According to the model, the jumper acceleratesrapidly (Fig. 1.2). A velocity of 49.4214m/s (about 110mi/h) is attainedafter 10 s. Note also that after a sufficiently lons
FIGUREI.2 Theonolyticol solution forthebungee problem oscompured in Exomple I 1 Velociry iumper increoseswith time ond osympfoiicolly opproocheso terminolvelociiy.
] .I A SIMPLE MATHEMATICAL MODEL
9
time, a constantvelocity.called the terminol velocitt',of 51.6983m/s (115.6mi/h) is reached.This velocity is constantbecause,eventually,the force of gravity will be in balThus. the net force is zero and accelerationhas ceased. ancewith the air resistance.
Equation( I .9) is called an anabtical or closed-formsolution becauseit exactly satisfies the oliginal diffbrentialequation.Unfortunately,tlrereare mirny matlrematicalnrodels that cannotbe solved exactly.In many of thesecases,the only alternativeis to develop a numericalsolutionthat approximatesthe exact solution. Nttntericalntethodsare thosein which the mathemirticalproblerr is refbrmulatedso it can be solvedby arithmeticoperations.This can be illustratedfor Eq. ( 1.8) by realizingthat by (Fig. 1.3): the time tateof changeof velocitycan be approximated
u ( / i + r -) u ( t i )
- Au - : - : Lt dt
dtt
(r.lr)
ti+t - ti
whereAu and At aredifferences in velocityand time computedoverflnite intervals,u(r1) is velocity at an initial time ri, and u(ria;) is velocityat somelater time f11. Note that du ldt = Lu I Lt is approximate becauseAr is flnite.Rememberfrom calculusthat dtt
dt
-
lim A1+l)
Au N
Equation( l. I I ) represents the reverseprocess.
FIGURE I.3 io / Theuseof o finitedifference thefirsfderlvotive of u wifhrespect to opproximote
AU
Approximateslope -u(r,) Ar_u(1,*,) Lt
li+t - ti
to
MATHEMATICAL MODELING,NUMERICAL MEIHODS,AND PROBLEM SOLVING Equation( 1.1I ) is calleclaJinite-diJJeren.ce opprcrirnationof the derivativeJt Iirnc /, . It can be substituted into Eq. (1.8)to give r'(1,,1)-u(t,l tr+t * t;
:g
.J _ _ t . t / 7 ) )tn.
This equationcan then be rearrangecl to yielcl
u(/i+r ) : u(/i)-1_ - 9lrt,,ff(/,+r - /i) [r
( r .r 2 )
Notice that the tenn in bracketsis the right-handside of the diff'erentialequationitself tEq. (1.8)1.That is, it providesa meansto computethe rateof changeor slopeof u. Thus, the equat.ioncan be rewritten as
u i + :r r + ' ! u
(t.13)
{1t
wherethe nomenclature u; clesignates velocityattinle /i and At : ti+t - ti. We cannow seethat thedifferentialequationhasbeentransformedinto an equationthat canbe usedto determinethe velocityalgebraically at ri+l usingthe slopeandpreviousvaluesof u andt. If you aregivenan initial valuefor velocityat sometime l;, you caneasilycomputevelocityat a latertime f 11. This new valueof velocityat l;11canin tum be employedto extendthe cornputationto velocityat l;12 and so on. Thus at any time along the way, New valne: old vahle* slopex stepsize This approachis tbrnrally calledEuler's metlnd. We'll discussit in more detail when we turn to diff'erentialequationslater in this book.
E X A M P L E1 . 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 eJ u m p e rP r o b l e m P r o b l e mS t o i e m e n t . P e r f o r mt h es a m ec o r n p L l t a t i o an si n E x a m p l e1 . 1 b u tu s eE q . ( J . 1 3 ) to colnputevelocitywith Euler'smethod.Employ a stepsizeof 2 s fbr the calculation. Solution. At the startof the computation(/{):0), the velocity of the jumper is zero. Using this infbrmationand the parameter valuesfrom ExampleI . I , Eq. ( 1.13)can be used to corxputevelocityat 11- 2 s:
r,: o* fr.r' - H,o,rl x 2 : te.62rls 68.r L I For the next interval lfiom r : 2 b 4 sJ,the colnplrtationis repeated,with the result :
l-
0.2s
l ( ) . 6 2+ 1 9 . 8 1- , ( 1 9 . 6 2 t - l 68.t L
.1 r
" l:16.4117m/s
tt
I .1 A SIMPLE MATHEMATICAL MODEL
Terminalvelocity
F I G U R EI . 4 for thebungeeiumperproblem Compcrison of thenumericol ond onclyticol solutions The calculationis continuedin a similar fashionto obtain additionalvalues: ttS
0 2 4 6 B
r0 l2 oo
u, m/s 0 r9.6200 36.4137 46 2983 50 r802 5t 3123 5 r 6008 5r 6938
The resultsare plotted in Fig. 1.4 along with the exact solution.We can seethat the numerical methodcapturesthe essentialfeaturesof the exact solution.However,becausewe have employed straighfline segmentsto approximatea continuouslycurving function, there is some discrepancybetweenthe two results.One way to minimize such discrepanciesis to usea smallerstepsize.Forexample,applyingEq. (1.13)at 1-sintervalsresultsin a smaller error, as the straighrline segmentstrack closerto the true solution.Using hand calculations,the effort associatedwith using smaller and smaller step sizeswould make such numerical solutionsimpractical.However, with the aid of the computer,large numbers of calculationscan be performedeasily.Thus, you can accuratelymodel the velocity of thejumper without having to solve the differentialequationexactly.
r2
MATHEMATICAL MODELINGNUMERICAL METHODS, AND PROBLEM SOLVING As in Example 1.2,a cornputationalprice nrustbe paid for a nroreaccuratenumerical result.Each halving of the stepsize to attainmorc accuracyleadsto a doubling of the nurnber of computations.Thus, we seethat thereis a trade-offbetweenaccuracyand computational effort. Such trade-offsfigure prominently in numerical methodsand constitutean importantthemeof this book.
L/AWSlN ENGINEERING AND SCIENCE 1.2 CONSERVATION Aside from Newton's secondlaw. there are other major organizingprinciples in science and engineering.Among the most importantof theseare the conserv,ation lan:s.Although they form the basis for a variety of complicatedand powerful mathematicalmodels, the great conservationlaws of scienceand engineeringare conceptuallyeasy to understand. They all boil down to - decreases Change: increases
(1.r4)
This is preciselythe fbrmat that we empioyedwhen using Newton'slaw to developa force balancefor the bungeejumper tEq. ( 1.8)1. Although simple,Eq. (1.14)embodiesone of the most fundarnentalways in which conservation laws are used in engineering and science-that is. to predict changes with respect to time. We will give it a special name-the time-variable (or transient) computation. Aside from predictingchanges,anotherway in which conservationlaws are appliedis fbr caseswherechangeis nonexistent. If changeis zero,Eq. (I.14) becomes Change: 0 : increases decreases or Increases: decreases
(1.r5)
Thns, ifno changeoccurs,the increasesanddecreases nrustbe in balance.This case,which is also given a specialnarne-the stea(ly-state calculation-has many applicationsin engiincompressiblefluid flow in pipes,the neeringand science.For example,fbr steady-state flow into a junction musl be balancedby flow going out. as in Flow in : flow out For the junction in Fig. I .5, the balancecan be used to computethat the flow out of the fourth pipe must be 60. For the bungeejumper, the steady-state condition would correspondto the casewhere the net lbrce was zeroor [Eq. (1.8) with du ldt : 0l l1l$ :
f
6411-
(1.r6)
Thus. at steadystate,the downwardand upwardfbrcesarein balanceand Eq. ( 1.| 6) can be solvedfor the terminal velocity
AlthoughEqs.(1.14)and (1.15)might appeartriviallysimple,they embodythe two fundamentalways that conservationlaws areemployedin engineeringand science.As such,they will tbnn an importantpart of our et-fortsin subsequent chaptersto illustratethe connection betweennumericalmethodsand engineeringand science.
I.3 NUMERICAL METHODSCOVERED 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
F I G U R EI . 5 A f o w b o o n c ef o rs t e o d yi n c o m p r e s s ifbuei df l o t vo t t h ej u n c l i oonf p i p e s
Tablel. I summarizes somemodelsandassociated conservation lawsthatfigurepromrnently in engineering.Many chemical engineeringproblems involve mass balancesfor reactors.The massbalanceis derived from the conservationof mass.It specifiesthat the changeof mass of a chemical in the reactordependson the amount of mass flowing in minus the n.rassflowing out. Civil and mechanicalengineersoften focus on models developedfrom the conservation of momentum.Forcivil engineering,force balancesare utilized to analyzestructures suchas the simple trussin Table 1.1.The sameprinciplesare employedfor the mechanical engineeringcasestudiesto analyzethe transientup-and-downmotion or vibrations of an automobile. Finally.electricalengineeringstudiesen-rploy both currentandenergybalancesto model electriccircuits.The currentbalance,which resultsfrom the conservationof charge,is similar in spirit to the flow balancedepictedin Fig. 1.5.Justasflow mnstbalanceat thejunction of pipes,electriccurrentmust balanceat thejunction of electricwires.The energybalance specifiesthat the clrangesof voltagearoundany loop of the circuit must add up to zero. We shouldnotethattherearemanyotherbranches of engineeringbeyondchemical,civi,, electrical,andmechanical.Many of theseale relatedto the Big Four.For exalnple,chemical engineeringskills are usedextensivelyin areassuchas environmental,petroleum,and biornedicalengineering.Sirnilarly,aerospace engineeringhasmuch in cornmonwith mechanical engineering.We will endeavorto includeexamplesfrom theseareasin the coming pages.
I.3
NUMERICAT METHODSCOVERED IN THISBOOK We choseEuler's methodfor this introductorychapterbecauseit is typical of many other classesof numericalmethods.In essence, most consistof recastingmathematical operations into the simple kind of algebraicand logical operationscompatiblewith digital compllters.Figure 1.6 summarizesthe major areascoveredin this text.
l4 TABTEl.l
Field
MATHEMATICAT MODELING,NUMERICAT METHODS, AND PROBLEM SOLVING Devicesond typesof boloncesfhotore commonlyusedin ihe four moiororeosof engineering. For eochcose,lhe conservotion low on whichthe bolonceis bosedis specified. Device
Chemical engineering
OrganizingPrinciple
MathematicalExpression
Conservation of mass
Mass balance:
ffi
inort ff_--*
ourpur
Over a unit of time period Amass:inputs-outputs
I
Civil e n g in e e r ni g
Conservation of momentum
t structyz
ffi ?7fu.
+Fv
Forcebalance:
,,\
+ I
-Fn *
O+
\
+FH
I
V
,1m77,
-at/
At each node I horizontalforces (FH)= o I vertical forces (I'u) : 0
Conservation of momentum
Mechanical engineering
Forcebalance:
I Upward force I
lr=0 I
V Downward force m Li
Electrical engineering
ilH3
Conservation of charge
= downward force - upward force
C u r r e n tb a l a n c e :+ i , For each node I current (i) = 0
I'R'
Conservation of energy
Voltagebalance:
a{A&-l ,-, -R2,Jr * z- -Y
f
L--\A7\--J i:R: Around each loop I emf's - I voltage drops for resistors
>6->a:0
r5
I.3 NUMERICAL METHODSCOVERED IN THISBOOK
lal Part 2: Roots and optimization
f(xl
Roots: Solve for.r so thatfi-r) = 0 Optimization:Solvefor x so that/'(r) = 0
Optima
lbl Part 3: Linear algebraic equations Giventhe a's and the b's.solve for the.r's
f\x\
a ' , r xt a r 2 x . ab =, arrx, 1-a,x, = b2
lcl Part 4: Curve fitting
o
ldl Part 5: Integration and differentiation Integration:Find the area under the curve Differentiation:Find the slooe of the curve
lel Part 6: Differential equations Given dv Av : ,h N:flt'Yl solve for r as a function of r .,Ii+r= -]'i+ "f(ti,yJAr
FIGURE I.6 Summory of thenumericol methods covered inthisbook.
t6
MATHEMATICAL MODELING,NUMERICAT METHODS, AND PROBLEM SOLVING Part Two dealswith two relatedtopics:root finding and optimization.As depictedin Fig. 1.6a,root locotiorrinvolvessearchingfor the zerosof a function.In contrast,optimizarion involvesdetermininga valueor valuesof an independentvariablethat correspondto a "best" or optirnalvalue of a function.Thus, as in Fig. 1.6a, optimizationinvolvesidentifying maximir and minima. Although somewhatdifferent approachesare used,root location and optimizationboth typically arisein designcontexts. Part Three is devotedto solving systemsof simultaneouslinear algebraicequations (Fig. 1.6&).Such systernsare similar in spirit to l'ootsof equationsin the sensethat they are concemedwith values that satisfy equations.However, in contrastto satistying a single equatiou,a set of valuesis soughtthat simultaneously satisfiesa set of linear algebraic equations.Suchequationsarisein a varietyof problemcontextsand in all disciplinesof engineeriugand science.In particular,they originate in the mathenraticalmodeling of Jarge systemsof interconnected elementssuchas structures,electriccircuits.and fluid networks. However,they are also encounteredin other areasof numericalmethodssuchas curve titl i n g l r n dd i f f e r e n t i ael q u u t i o n s . As an engineeror scientist.you will often haveoccasionto fit curvesto datapoints.The techniquesdevelopedfor this pulposecanbe dividedinto two generalcategories:regression regressionis ernployedwherethere and interpolation.As describedin Part Four tFig. 1.6c'1, is a significantdegreeof error associirted with the data.Experimentalresultsareoften of this kind. For thesesituations.the strategyis to derivea singlecurve that representsthe general trend of the datawithout necessarilymatchingany individualpoints. In contrast,interpolutionis usedwherethe objectiveis to determineintermediatevalues betweenrelativelyerror-freedata points. Such is usually the casefor tabulatedinformation.The strategyin suchcasesis to flt a curve directly throughthe datapoints and r.rse the curve to predict the intermediatevalues. As depicted in Fig. 1.6d, Part Five is devoted to integlation and differentiation.A plrysical interpretationof ruurrcricctliltegratiott is tlre determinationof the area under a curve. Integrationhas many applicationsin engineeringand science,rangingfrom the determinationof the centroidsof oddly shapedobjectsto the calculationof total quantities In addition,nurnericalintegrationformulasplay an basedon setsof discretemeasurements. importtrntrole in the solution of diffbrentialequations.Part Five also covers methodsfor nume.ricaldifr'erentiation. As you know fiom your study of calculus,this involves the determinationof a function's slopeor its rate of change. equations(Fig. 1.6e). Finally.Part Si.xfocuseson the solutionof ordirro'vdi.fterential Such equationsare of greatsignificancein all areirsof engineeringand science.This is becausemany physicallaws arecouchedin termsof the rateof changeof a quantityratherthan rnodels the magnitudeof the quantity itself. Examplesrangefrom population-forecasting (rateofchangeof population)to tlreacceleration of a tallin-ebody (rateofchangeofvelocity). initial-valueandboundary-valueproblems. Two typesof problemsareaddressed:
t7
PROBLEMS
PROBTEMS l.l Usecalculusto velity that Eq. (1.9) is a solutionof E q (. 1 . 8 ) . 1.2The following infbrmation is availablc for a bank acc0unt: Dote
Deposits
Withdrowols
2 2 41 3
327.26
2t6B0
3 7 86 1
, r ( nr
b-
|2;
1; 6; 8; I0
l
or. by transposing a row vectorwith the ' operator, >> Lr =
|
2, 4
6
I
10
I'
Tlre resultin all threecaseswill be b= 1. 4 b S 1t)
A rnatrixof valuescan be assignedas lblkrws:
r'23 456 789 In addition.the Enterkey (carriagereturn)can be usedto separirte the rows. For example. in the followingcase,the Enterkey woulclbe struckafterthe 3. the 6 andthe I to assignthe matrix: .->A..
lr23 456 7 8 9l
ffi qA1, zf :5yst LIZ
zt -+
4
"L x jl':-
a_J---*=
_r -{
vI
Z] X+ , -
A
> rvho yariabl
Your
al
es
.rrc:
ans
b
x
or. with moredetail.cnterthc whos command: >;.whos f Jd r { i e
Sr ze
A
lx3
12
clouble
array
I x5
40
cloubLa
array
dns
LXL
l)
5xi '1 xl
x(comfiLex) Clrancl t-otal
is
Byte::
Class
tlouLr Lc: ;.Lrray
8
21
4Ct
il,trrb l e
i)i I'ay
I6
doubLe
arr.r.y
elenrrrnl.s; LrsinqJ 175
bytes
Note that subscriptnotationcan be usedto accessan individualelcmentof an array. For exarnple.the firurthelemento['thc columnvccfort) can be displayedas :>
b(4) .
,tlt si
8 F o r a n a r r a y , A ( n, rn ) s e l e c t s l h e c l e m c n t i n m t h r o w a n d t h c n t h c o l u r n n . F o r e x a m p l e , A(:,1) 6
Thsre are sevcralbuilt-in functionsthat can be used1ocreatcmatrices.For cxarnplc, the ones llnd zero:i l'unctionscreatcvcctol'sor matricesfilled with ones and zeros. respectively. Both havetwo argumcnts, thc first tbr the nulnberof rows and the secondtbr thc number-ofcolumns.For example.to creatca 2 x 3 matrixtl1'zeros: E :
7 , c I o s ; ( 2 ., - ) J
00 UU
Sintilarly, the or-resfLtnctioncan be used to create a row vector of oncs: u
= o n c : : ;( j ,
- l)
2.2.3 The Colon Operotor The colon opefatoris a powerfll tool fbr crertingand manipulatingarrays.If a colon is used to separatetw'o nunrbers,MATLAB generaleslhe n u m b e r sb c l w e e nl h e m u s i n ua n
-
ffial z$4 zl' -+
zf Ll 2
'|-A
I la-
;n-Zt*
5t
s
-i
Z] X+
L
vI
> A
.^
2
PressingEnter will perform the calculation again. But you can also edit this line. For example,changeit to the line below and then pressEnter. >> A
.". 3
1821 64 I25 343 5r2
216 129
Using the up-arrow key, you can go back to any command that you entered. Press the uparrow until you get back the line F * -
Alternatively,you can type b and pressthe up-arrow once and it will automaticallybring up the last commandbeginningwith the letter b. The up-arrow shortcutis a quick way to fix errorswithouthavingto retypethe entireline.
2.4
USEOF BUIIT.INFUNCTIONS MATLAB and its Toolboxeshavea rich collectionof built-infunctions.You can useonline help to find out nroreaboutthem. For example.if you want to learnaboutthe 1og function, type rn >> helP LOG
log' lrlatural
LOG(X)
is
Complex See
also
logarithm. the
nat-ural
results LOG2,
lograrithm produced
are
LOG10,
EXP,
if
of X
the is
elements
not
of
X.
posrtive.
LOGM.
For a list of all the elementaryfunctions,type '>
help
elfun
One of their importantpropertiesof MATLAB's built-in functionsis that they will operatedirectly on vectorand r.natrixquantities.For example,try >> log (A)
0 1.3863 1.9459
0.6931 1.6094 2.0194
1.0986 r.79'LB 2.1912
and you will see that the natural logarithm function is applied in array style, element by elenrent,to the matrix A. Most functions. such as sqr t, abs, sin, acos, f anh, and exp, operate in anay fashion. Certain functions, such as exponential and square root, have matrix definitions also. MATLAB will evaluate the matrix version when the letter m is appended to
'\--
zf+
ffi= qA+,
,"1{z
a eys\ -1
-l
Z] X+
"L 4
> sqr:tm(A)
A.4498 + 0.1623i 1.0185 + O.AB42i I.5813 - 0.594A1
0.5526 + 0.2068i 1.2515 + 0.A22Bi 1.9503 - 0.1611i
0.5555 I.4844 2.3134
0.3481l 0.0385i + 0.21I7i -
There are several functions for rounding. For example, suppose that we enter a vector: >> E =
-1.5
l-1.6
-I.4
I.4
1.5
I.6l;
The round function rounds the elements of E to the nearest integers: >> round(E) -2-2-1r22 (short fbr ceiling) function rounds to the nearestintegerstoward infinity:
The ceil
>> ceil
(E)
1
-1
2
2
2
function rounds down to the nearestintegers toward minus infinity:
The f loor >>
1
f loor
( F t)
22
2rl
There are also functionsthat perform specialactionson the elementsof matricesand arrays.For example,the sumfunctionreturnsthe sum of the elements: >> F = l3 5 4 6 Il; >> sum(F)
19 In a similar way, it should be pretty obvious what's happeningwith the following commands: >> min(F),max(F),mean(F),prod(F),sort
(F)
1
6
3.8000
360
13456
ffi
Z} x --Tl 4 -l-
&
32
MATTABFUNDAMENTALS A common useof functionsis to evaluatea fornrula for a seriesof arguments.Recall that the velocityof a free-fallingbungeejumper canbe computedwith [Eq. (1.9)]: lom
/
loe,
M, gft
\
, : , / a t a n h,l/ r - r I V c.r \V trt / due to gravity(9.81 m/s:),n is mass(kg), where u is velocity(m/s),g is the acceleration c,,is the drag coefficient(kg/m), and r is time (s). Createa columnvectort that containsvaluesfrom 0 to 20 in stepsof 2: ;'> l- =
2.5 G
Th bo
[0:2:20]'
t.0 2 4 6 B l0 t2 l4 15 1B 20 Check the number of items in the t array with the lerrgLh function:
Yor
>> I cnqtlr (L ) =
allS
1I
Assignvaluesto the parameters: =
>:.9
9.81;
in -
68.1;
crl
=
0.25;
M A T L A B a l l o w sy o u t o e v a l u a t ea f o r m u l as u c ha s r : . 1 ( r ) , w h e r et h e f b r r n u l ai s computedfbr each value of tl.rer array,and the result is assignedto a correspondingposition in the u array.For our case, -/ :
s q L t - ( g * n / c ' d ) * 1 - a n h ( s q r t ( g * c c l/ n ) * t
) i
( (
t) 1B.'t292 33.r118 42 .01 62 46 .951 a 49 .42r4 50.6175 51.1871 51.4560 51.5823 br.6416
I
-
ffir zl Llz
zl'+
.r1
-\
7f 5[ st -{
L
-l
Z] X+?
vI
x
>
til,re('Plot of ('Valucs
:':.' xlabel
, -> y l . L b c l
('Valucs
v
ver':jus
of
L')
ol
v')
t')
.:'> qrid
Plot of u versus f
60 50
a
: .' l
t
'!ry
l
l
i
40
I l
l
I
o
: 30
6
20
i
:
j
i
I
!
I,
i
.-..--- t. "- -
:
10
"-t--* l
I i
l
0
?,r'
ffi z\4+ ,"lqfjeTl
I
8 10 12 Valuesof t
14
16
18
a-* z,J ^:, x+
l
-l-
zl
3$&
34
MATLABFUNDAMENTALS TAB E 2.:2 Specifiers for colors,symbols,ond linetypes. Colors Bl,re Green Red Cyon A^agenio Yello'"v Blccl.
Symbols
Line Types
Poinl
Solid Dolied Dcshdot Doshed
Circle 'MO TK
Pu l s Slcr Squore Diomond T r i o n ge ( d o v r n ) Triongle{up) Triongie(leftJ T ri o n g l e { r i g h t ) Penlogronr nexogfcrn
m v k
The 1rlot- conlmandclisplays a solidline by default.If you wantto plot eachpoint with a symbol' you can includea specifierenclosedin singlequotesin the pior functio.. Table2'2 Iiststhe availablespecifiers. For example,if you want to useopencirclesenter r-:.l:jlot
(t,
v,
,o,)
MATLAB allowsy.ruto displaymorethanonedataset on the sameprot.For example, if you want [o connecteachclatarnarkerwirh a straight rineyou courdrype :'i.I,1ol,
(L,
yt
t,
\r,
,o')
It shouldbe Inentionedthat,by clefault,previousplots areerasedeverytirnethe plor commandis implernented. The hol d c,nconrmancl hoidsthecurrcntplot anclall axis propertiesso that additionalgraphingcommancls can be addedto the existingplot. The h.tit of f cornmandreturnsto the def'aultmocre.Fclr exampre,if we had typed the tbilowing cornmands, the flnal plot woukl only displaysyrnbols; :''
pIoL
>> plot
(1,
.r)
(t,
v,
,o,)
ln contrast,the f> x ^ 3; >>Y=B-x
.r'') +-9l
+fi6-qrlal
l,
Use subplor to genera (.r, .y) in the top pane and (x,.v,l) in the bottompane 2.15 Exactly what will t MATLAB commandsare I (a)>>x=z;
whercU: r,clocity(nr/s),S: channelslopc,n: roughncss LJsr'MATLAB to crcatc a plot displayingboth thc data (nr),and /1 : dcpth(rn).Thc lirllo',v- (usingdiantondsymbols)and thc llnction (usirrga dottcd cocfl'icient, Ii: yu;.1,1.r ing datais availablclbr I'ivechannels: l i n e ) .P l o tt h c l u n c t i o nl i r r u : 0 t o 9 ( )n r / s . 2.ll Thc loc_{locr lirnclionopcratcsin an idcnticalI'ashion to the lirnction cxccpt(hatlogarithnricscalcsarc uscd fil()'. n5BH lirr both thc,r ancl.r'axcs.Usc this lunctionto plot thc dau 0 035 0 0001 t0 2 and lunctionas dcsclihcdin Proh.2. I0. Explaintlrc rcsults. B I 0.020 0 0002 2.12 Thc Maclaurinscricscxpansiontirr thc sinc is
00r5 0 030 a a22
-ll
1 5t I
4l
PROBTEMS 2.13You contactthe jumpers used to generatethe data in Table2.1 and measure their frontal areas.The resulting values,which are ordered in thc same sequenceas the conesponding valuc-sin Table 2. l, are
2.16 The trajecbry of an object can be modeledas R. ):(tando).\--r'*)t, IU;COs.d0
where _v: height (m), d,, : initial angle (radians),r : horizontal distance (m), g = gravitationalacceleration ( : 9 . 8 1 m / s r ) ,u , , : i n i t i a lv e l o c i t y( m / s ) ,a n d y , , : i n i t i a l find the displacementfbr r',, : Q (a) Il the air densityis p : 1.225kg/mr, use MATLAB to height. Use MATLAB to r,,:30 nr/slbr initial angle-s rangingliom l5 to 75o in and drag coefficicrrt computevaluesof the dimensionless increrncnts of 1,5".Employ a rangeof horizontaldistances cD. from -t : 0 to 100m in incremcnts of 5 m. Thc resultsshould (b) Deternrine the average,minimunrand rnaxirlumol'thc in an array where thc first dimension(rows) be assernblcd resulting values. distances.and the seconddimension (c) Dcvelopa side-by-side plot ofA versusrr (lcft sidc)and correspondsto the (columns)correspondsto the difl'erentinitial angles.Use C.,versusrr (right side).lncludedescriptivcaxis liibels this matrix to gcncratea singlc plot of thc hcightsversus andtitlcson the plots. horizontaldistanceslbr eachof the initial angles.E,rnploya 2.11The lbllowing paramctricequllirrrrsgenerrlc-l hclix among the dill-crentcases.and scale legenclto di.stinguish r'xponentially as it cvolvcs thatcontriicts thc plot so that thc minirnumheightis zero usingthe azis .r:e ol'sinI commancl. \'=c CosI dcpendence ol'chcmicalreactionscan 2.17 The lcmperature wilh theArrhaniu.tt'tlutrtittn; bc ctrrnprrtetl
A,nt2 0.454 0 401 0 453 0 485 0 532 a 474 A 4B6
Usesubpl,rr to gcneratca two-dimcnsionallinc plot ot' (-r,i') in the top pane and a three-dirncnsional line plot ol' (,r,r, :) in thc bottom panc. 2.15Exactlywhal will be displaycd alier thc lirllowing MATLABcourmandsarc typcd'l -
2;
-
lt
q
.
. 1: 2 : 1 0 ;
>> r
.
f]
(a) -'> x :':' X' >-../ (b)t-
l; x
I
> > : i L l l r ( c 1 )*
zt +
4; r(2,
)
6
2.);
3)
,jF-\
k - Aa t'itRI,,1 (or tiewherck: rcactionratc(s r).A : thc prccxponential quency)lactor,E: activationenergy(J/mol),R - gasconstant [8.314J/(rnolc . K)], and l, : absolutetemperaturc ( K ) . A c o r n p o u n d h a s El :x l O s J / m oaln d A : 7 x 1 0 1 6 . Usc MATLAB to gcncratevalucsol reactionratcs1brtemperalurr-s rangin-u lrom 273 to 333 K. Usc subplot to gc'ngraphol'(a) /> freefallvel
gravity * cd,/
(kSi m)
m) * t);
. m. To invoke the function, return to the command window
\12, 68. 1, 0.25)
The result will be displayed as
50.6i75 One advantage of a firnction M-file is that it can be invoked repeatedly for diff'erent argument values. Suppose that you wanted to compute the velocity of a 100-kg jumper after 8 s: (8, 100, 0.2t)
>> freefallvel
53.187B To invoke the help comments type >;. hel.p freefallveL which results in the comments being displayed freef;r1lve1: bungee velocity with second-order drag .,'=freefallvel(t,m,ccl) compuLes the free fall veloclty of an object with second-order i nput- : t = time (s) m = nass (kS) (kglm) cd = second-order drag coefficient ourpu[: (m/s) v = downward velocrty
dr:ag
If at a later date, you tbrgot the narne of thjs functiorr, but remembered that it involved bungeejumping, you could enter >> lookfor-bungee and the following information would be displayed f r e e f a l .1 . m :
% f reef al1:
bunqee velocity
with
second-order
clr:agt
Note that, at the end of the previous example, if we had typed >>g
ffi= z$4+
zr+
ztf-ai3'" -\
= "L vI
3l F+
!' -
lB 5 1D 12' 6 1.5
3.2
4l;
m= 7.5000 S=
2 .873'1 BecausescriptM-files have limited utility. functionM-flles will be our prirnaryprogramming tool fbr the remainderof this book. Hence,we will ofien refer to function M-files as simply M-flles.
3.1.3 Subfunctions M-files, Functionscan call otherfirnctions.Althoughsuchfunctionscan exist as separate they rnay also be containedin a singleM-file. For example.the M-file in Example3.2 (without conrments)could have been split into two functionsand saved as a single M-file': rr = f reef al Lsubf unc (L, f unction = veL (t, m, cd);
n r,
ccl)
v
end function n
a
r . r t l
9
v
=
(t,
= vel
v
m,
cd)
al.
sqrt
*
(q
m ,z cd) *tanh(sqrt
(g
*
cd
/
m)
*
t);
end
I Note that althoughend statementsare t.rptionalin single-functionM-lilcs, we like to includethem whcn s u b f u n c t i o nasr 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 ebs e t w c e nt 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 .
ffi qA+, zt Ll 2
zl'+
LA
jt -l
ZN
z,J Lt€ - ?l 5t 3$8 Z] X+?
el' st
-l
< , ,. . . )
where form.: 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. A sirrrpleexample would be to display a value along with a message.For instance,suppose that the variable velocity has a value of -50.6175.To display the value using eight digits with four digits to the right of the decimal point along with a message,the statement along with the resulting output would be >> fprinLf
('T'he velocity
The velocit.7
is
%8.4f n/s\n',
velocity)
i s 50.6175 mr's
This example should make it clear how the format string works. MATLAB starts at the left end of the string and di:;plays the labels until it detectsone of the symbols: %or \. In our example, it first encounters a % and recognizes that the following text is a format code. As in Table 3.1 , the format codes allow you to specify whether numeric values are
ffi zl' +
zl Llz .-r J
ffii: :.t -l
"L x -,\'IN Y+
vI
a,- . [h
j-l € - sJ 5t I
3.2 INPUT.OUTPUT TABTE3.I
49
Commonlyusedformotond controlcodesemployed w i t ht h e f p r i n t f f u n c t i o n .
Formoi Code
Descripiion
zd
zf Zg
l n l e g e rl o r m c i S c i e n t i f l cf o r m o fw l t h l o w e r c o s ee S c i e n t i cf f o r m o tw i t h u p p e r c o s eE 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
Conirol Code
Description S l o r fn e w l i n e Tob
\Il
\t
displayedin integer,decimal,or scientificformat.After displayingthe valueof veLociry, MATLAB continuesdisplayingthe characterinformation(in our casethe units: m/ s) until it detectsthe symbol \. This tells MATLAB that the following text is a control code. As in Table 3.I,the control codesprovidea meansto perfbrm actionssuchas skippingto the next line. If we had omittedthe code \n in the previousexample,the comrnandprompt would appearatthe end of the labelm/s ratherthanon the next line as would typicallybe desired. The fprintf functioncan alsobe usedto displayseveralvaluesper line with diff'erent formats.For example, ('%5d %10.3f %8.5e\n',100,2*pi,pi)
>> fprintf 100
6.283
;
3.14159e+000
It can also be usedto display vectorsand matrices.Here is an M-file that enterstwo setsof valuesas vectors.Thesevectorsarethencombinedinto a matrix.which is thendisDlavedas a tablewith headines: fprrnt fdemo function '2 x = lI I 4 5l; 2A.4 12.6 r7.B BB.7 L2a.4l; x;yl j -
fprintf fprintf
(' y\n' ) x ('%5d %10.3f\n', z) ;
The resultof runningthis M-llle is >>
f pr:int X 1 2 3 4 5
--
z[+
Y 20.400 12 .6AA 17.800 88.700 120.400
ffi qAb :I
,/--5 ---"'-
z(\ Ll 2 -zl '-A
f de rno
--:> - ^, a I
e[st
x
> 9 = 9 . B 1 ; m = B Ot ;= 5 ; >> cd=[.25 .261 .245 .28 .213]'; >> v=sqrt (g"m ./cd) .*tanh(sqrt(g*cdlm) *t) ; 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, removeall variablesfrom the workspacewith the clear command, >>
p F
clear
u al
At this point, if you tried to display the velocitiesyou would ger rhe result: >>
3.3
v
??? Undefined function
or variable
'v'
However,you can recoverthem by entering >>
load
!
T m CI
veldrag
n
Now, the velocitiesare availableas can be verified by typing
a
>> who Your
variables
3 TI
are:
cdv
Although MAT-files are q,rite useful when working exclusivelywithin the MATLAB environment,a somewhatdifferent approachis requiredwhen interfacingMATLAB with otherprograms.In suchcases,a simpleapproachis to createtext files written in the widely accessibleASCII format.
ffi= qA+
,
zl -h
--a zl Llt ._r _l
5[ sl
-i
Z] X+
"L x z,J
:
simpmaLr ix 5192 zF,
= l9
Alternatively, you could use the load command as a function and assign its values to a variable as in >> A = loeid(simpmatrix.txt) The foregoing material covers but a small portion of MATLAB's file managementcapabilities. For example. a handy import wizard can be invoked with the menLrselections: File, lmport Doto. As an exercise,you can denronstratethe import wizards convenienceby u s i n g i t t o o p e n s i m p m a t r i x . t x t . I n a d d i t i o n ,y o u c a n a l w a y s c o n s u l th e l p t o l e a r nm o r e about this and other l'eatures.
3.3
STRUCTUREDPROGRAMMING The simplestof all M-files perform instructionssequentially.That is, the program statementsare executedline by line startingat the top of the function and moving down to the end. Becausea strict sequenceis highly limiting. all computerlanguagesinclude statepaths.Thesecan be classifiedas mentsallowingprogramsto takenonsequential . .
The branchingof t-lowbasedon a decision. Decisions (or Selection). Loolts (or Repetition).The looping of flow to allow statementsto be repeated.
3.3.1 Decisions The i r Siructure. This structureallows you to executea set of statementsif a logical conditionis true.Its generalsyntaxis if
condition statemenf-s
ffi z\4 ,"I LJ' zl'+
9t" 5\
-J
3l x+
"L 4
'7
d..
II
I
>0
&
2&
Substitute constants
t
i
I '('b'
7>'7
I
t Evaluatemathematical expressions
t
-2
>0
&2==2
&1
>7
&
I I
tl tl
ii
F&T
>'d')
II
-F \---YJ
T
I
Fl
&
Evaluaterelational expressions
t
Fl
\.-.........-.Y#
F
| _('b'
Evaluatecompound expressions
+
T
T FIGURE 3.I A slep'by'srep evollotion o{ o conrplexdecision.
elseif
condition-,
statementsr
else statements..l5.. end
EXAMPLE 3,4
i t Structures Problem Stotement. For a scalar,the built-in MATLAB sign function returnsthe sign of its argument(- I, 0, I ). Here's a MATLAB sessionthat illustrateshow it works; >> sign (25.6) 1 >> sign (-4.776) -1 >> sign(0) 0 Develop an M-file to perform the same function.
zt+
,"1{
ffi qA+,
zt\ aY
.
x
if
returns
1
if
x
gTreater
is
zero.
than
0 =
sgn
= nYSiQIn(x)
sqn
function
1;
end
This function can be run as >> mysign(25.6) 1
Although the function handles positive numbers correctly, if it is run with a negative or zero argument, nothing is displayed. To partially remedy this shortcoming, an if. . .e1se structure can be used to display -1 ifthe condition is false: sgtn = mysign (x) f unction 1 if x is greater than zero. returns % mysign(x) I if x i-s less than or equal Z if x > 0 sgn = 1; ^t
to
zero.
^-. _ -
-^-
- 9 r !
_t.
end
This function can be run as >> mysign(-0.176) -1 Although the positive and negative cases are now handled properly, -1 is erroneously structurecan be used to incorporate returned if azero argument is used.An rf . . . elseif case: this final funct-ion sgn = mysign(x) 1 if returns % mysign(x)
z z
x
is x
O if
x
is is
gtreater less eq-al
than than to
zero.
zero.
7.aro.
lf x > 0 sgn = 1; x < O elseif sgn = -1; else sgn = O; end
The function now handlesall possiblecases.For example, >> mysign(0) 0
ffi!3r qA+,
il{#ff: er'
"L x 4
zl xh
%8,
57
PROGRAMMING 3.3 STRUCTURED
T h e s r v i t - c hS t r u c t u r e . 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 . . . e 1 s e i f structure.However,ratherthan testingindividual conditions,the branchingis basedon the value of a single test expression.Dependingon its value,different blocks of code are implemented.In addition.an optionalblock is implementedif the expressiontakeson noneof the prescribedvalues.It has the generalsyntax I ..sf r.xprcss
sr.ricch case
I on
r z a Ju e ,
"-ctaaemencs. y a - 1u e . , case staIe.menrs.
oInerwrse s I a a emel]
f .sol lr..r rr:i,
end
As an example,hereis functionthatdisplaysa message dependingon the valueof the stringvariable,gra de. gracle
=
svrifch case drsp case
'R'; qracle: 'A' ( 'Excellent' 'B'
)
clisp('Good') 'C'
case
disp L r15ri
( 'Mc,-diocre' ) D
disp('Whoops') 'F' case drsp
( 'Wou1d
like
fries
wj th
your
order?'
)
otherw]se disp(,lluht
)
end
"Good" would be displayed. When this codewas executed,the message Vorioble Argument List. MATLAB allowsa variablenumberof argumentsto be passed to a function.Tlris featurecan come in handy for incorporatingdefaultvaluesinto your functions.A d{oult vulueis a numberthat is automaticallyassignedin the eventthat the userdoesnot passit to a function. As an example,recall that earlierin this chapter,we developeda function freef a 1 1ve i, which had threearguments: =
v
freefallvel
(t,m,cd)
Although a userwould obviously needto specifythe time and mass,they might not have a good idea of an appropriatedr;..9coefficient.Therefbre,it would be nice to have the program supply a value if they omitted it from the argumentlist. MATLAB hasa functioncallednarsin that providesthe numberof input arguments suppliedto a function by a user.It can be usedin conjunctionwith decisionstructureslike
ffi ,-TLJ' -b
b
Z-x N
z.J
3!h ,f.€ - 9f 5l la^,
l -
PROGRAMMING WITHMATLAB
58
the if or switch constructsto incorporatedefault valuesas well as error messagesinto your functions.The following code illustrateshow this can be done for fr^eefallvel: function ?
=
v
%
(t,
freefalLvelt
freefallvef
m,
cd)
with second-order bungee vefocity (t,m, cd) computes free-fa11 the
:
v=freefallvel
of
with
an object
% input: Z L = time (s) %m=mass(ks) (default = 0.21 cd = drag coefficient % % outpul-: (mrs) v = downward velocity % swi t ch nargrin case O enter trme and mass') error('Must ^--^
drag.
second-order
kg/m)
a
error case cd
en1-er nass ' )
( 'Must
2 = 0.21 ;
end A = v
drag velocity
% acceleration
9.81; = .sqrt(g
*
m,/
of
cd)*Lanh(sqrt(g
gravity * cd
/
m)
*
t);
Notice how we have used a switch structureto either display error messagesor set the default,dependingon the numberof argumentspassedby the user.Here is a command window sessionshowingthe results: >'> f reef allvc. l (12,68.I,
A .25)
50.61.75 >>
( 12,6B.l)
freefallvel
48 .81 41 >>
??'. Err(,r Must >>
(12 )
freefallvel
enter
mass
f reef al.lvel
??? Must
Error enter
f reefallv-l
us.ng
() ltrofallvol
*sing time
and
EXAMPLE 3.5
mass
Note that narsin behavesa little diff'erentlywhen it is invoked in the command window. In the commandwindow, it must include a string argumentspecifyingthe function and it returnsthe numberof argumentsin the function. For example, >> nargin
('freefallvel'
ffir
,fl1fu= -\
zt'--h"
)
"L x zJ 7) F+
>
f actor: (5 )
12a This loop will execute5 tirnes(fiom I to 5). At the end of the process,x will hold a value o f 5 l ( r n e a n i n g 5 f a c t o r i a l oI l x 2 x 3 x 4 x 5 : 1 2 0 ) . Notice what happensif n : 0. For this case,the f or loop would not execute,and we would get the desiredresult,0! : I
Vectorizotion. The for loop is easy to implement and understand.However, for MATLAB, it is not necessarilythe most efficient meansto repeatstatementsa specific numberof times.Becauseof MATLAB's ability to operatedirectly on arrays,vectorizotion providesa much rnoreefficient option. For example,the filllowing f or structure: i
n. t- = 0:0.42:5C
for r
=
y(i)
i
+ l; = cos(r);
end
can be representedin vectorizedform as t
= 0:0.02:50;
y
= cos(t);
It shouldbe noted that for morc complex code,it may not be obvious how to vectorizethe code.That said,whereverpossible,vectorization is recommended.
T Ir
Preollocotion of Memory. MATLAB automaticallyincreasesthe size of arays every tirne you add a new element.This can becometime consunringwhen you perform actions suchas addingnew valuesone at a time within a loop. For exarnple.hereis somecodethat
fiT=fiar qA+ zf Ll L
zl'+
.r'.
.{
A e[st -.1
Z] X+
"L x
sl SI
>
f1(3,4)
ans.=.
\ Asidefiom the variablesin its argumentlist, an anonymousfunctioncan includevariables that exist in the workspacewhere it is created.For example, we could createan a n o n y l n o ufsu n c t i o n / t l ) : - L l l u s >>
a
-
>>
4; = 2; f2=@ (x)
>>
f2 (3)
>> b
ans
a*x^b,'
= 36
Note that if subsequentlywe enter new valuesfor a and b, the anonymousfunction doesnot chanse: f 2 (3 ) ans
ffiar qAb zf Ll t zl -&
''l
-\
T -l
--c,lh -
5[st
3l x+
Y
j-l'€- eJ 5t 3$8
t t
FUNCTIONS 3.5 PASSING TOM.FILES
67
Thus, the function handleholds a snapshotof the function at the time it was created.If we want the variablesto take on new values,we must recreatethe function.For example,having changeda to 3, >>
f2=@x
a*x^b;
with the result >>
f2 (3)
ans
27
It shouldbe notedthat prior to MATLAB 7, inline functionspertbrmedthe same role as anonymousfirnctions.Forexample.the anonymousfunctiondevelopedabove,f 1, could be written as >>
+ y^2',
fl=rnline:('x^2
'x','y');
Although they arebeing phasedout in favor of anonymousfunction,somereadersmight be usingearlierversions.and so we thoughtit would be helpfulto mentionthem.MATLAB helpcan be consultedto learnmoreabouttheiruseand limitations.
3.5.2 FunctionFunctions Function functions are functionsthat operateon other functionswhich are passedto it as input arguments. The functionthat is passedto the functionfunctionis ref'erredto as the passed.function. A sirnpleexampleis the built-in function fp1oL, which plots the graphs of functions.A simplerepresentation of its syntaxis fplot\iun,lins) where fun is the tirnctionbeing plotted betweenthe -r-axislimits specifiedby 7 rms = [xmin xmax].For this case,f un is the passedfunction.This functionis "smart" in that it automaticallyanalyzesthe functionand decideshow many valuesto use so that the plot will exhibitall the function'sf'eatures. Here is an exampleof how fplot can be usedto plot the velocityof the free-falling bungeejumper.The f unctioncan be createdwith an anonymousfunction: .
vrl
'Jrrj
s q r t ( 9 . B 1 * 6 i l . I / 0 . 2 5 ) * L a n h ( s c l r t -( 9 . B 1 * A . 2 5 , / 6 8 1 . ) *t) ; We can then generatea plot from / : 0 to l2 as >>
fplot
(vel,
f2 I )
[0
The resultis displayedin Fig. 3.2. Notethatin theremainderofthisbook,wc,will havemanyoccasions to useMATLAB's builrin functionfunctions.As in the following example,we will also be developingour own.
ffi= ----
,"I LJ' zl+
--1t _-l
5[ s\
-
> t=linspace (A, I2) ; >> v=sqrf (9. 81"68. 1,/0 >> mean(v)
*tanh (sqrt (9. 81"0.25 / 68. 1 )
36.0870
lnspectionof a plot of the function (Fig. 3.2) showsthat this resultis a reasonableestimate of the curve's averageheieht.
ffir qAb
> funcavS
(0, 12,60)
36.4121 Now let's rewrite the M-file so that rather than being specific to f unc, it evaluates a nonspecific function name f that is passed in as an argument: [*nct.on 9
r
iavg
r nr"a.
%
{r,o,o,n)
fun-dvg
r'7^,
,aa
f rrrnl
favg-funcavg(f
:nr
,a,b,n)
:
%
hoiqhL
comput-es of
Z
input: f =
to
be
evaluated
%
a
=
lower
bound
of
range
%
b
-
upper
bound
of
range
%
n
= number
?
z
of
over
val.ue a
range
intervals
outpuc: l'aV9 = ,vQ) oQe
% x
function
average
function
=
Y = favg
volue
of
f une t ion
llnspace(a,b,n); f (x); = mean(Y) ;
Becausewe haveremovedthe subfunctionf unc, this versionis truly generic.It can be run fiom the commandwindow as >> vel-@ (t ) sqrt ( 9. B1* 68.I/ 0.25 ) *tanh >> funcavg (vel , A, 12 ,60)
ffi z$4 zt+
l{t
( sqrt
( 9. B1*4.25 / 68.1 ) *t ) ;
"L x
+,
3l x+
4
-a, lh
--T
,f\€ - 9f 5l
-l-
3$8
70
PROGRAMMING WITHMATLAB
36 . 0 r 2 ' 7 To demonstrateits genericnature,funcavg can easily be appliedto anothercaseby merely passingit a different function. For example,it could be usedto determinethe average value of the built-in sin function between0 and 2z as >> funcavg(Gsin, 0, 2*pi, 180) 6.3001e 017 Doesthis resultmakesense? We can seethat f uncavg is now designedto evaluateany valid MATLAB expression, We will do this on numerousoccasionsthroughoutthe remainderof this text in a numberof contextsrangingtiom nonlinearequationsolvingto the solutionof differentialequations.
3.5.3
Possing Porqmelers
Recall from Chap. I that the terms in mathematicalmodelscan be divided into dependent and fbrcing functions.For the bungeejumper and independentvariables,pararneters, (u) variable,the is the dependentvariable,time (t) is the independent model,the velocity (c,,) (rr) gravitational and the constant(g) is the rnass and dragcoefTicient areparameters, to investigatethe behaviorof sr.rchmodelsby perfbrcing function.lt is cornmonplace variablechanges ana\-sis.This involvesobservinghow thedependent fbrmingtr sensitivitlt as the parameters and forcingtunctionsarevaried. In Example3.7,we developeda functionfunction,f uncavg, and usedit to determine the averagevalue of the bungeejumper velocity fbr the casewherethe parameterswereset L\ttn :68. I and c,,- 0.25.Supposethatwe wantedto analyzethe samefunction,but with Of course,we could retypethc functiclnwith new valuesfbr each dif'ferentparameters. case,but it would be preferabletojust changethe parameters. into anonymous As we learnedin Sec.3.5.1,it is possibleto incorporateparameters functions.For example,ratherthan "wiring" the nurnericvalues,we could havedonethe fbllowing: >> m-b8.I;r:d-().2,5; >> -ze1=@(t,) sqrt (9.81*m/cd) >>
f uncavg
*Lanh(sqrt
(9.81*cdz'm)
*t)
;
(ve1, 0 ,1,2,6A)
36.A121
However,if we want the parameters to take on new values,we must recreatethe anonymous function. MATLAB offers a better alternativeby adding the term varargin as the function function'slast input argument.In addition,every tirne the passedfunction is invoked within the function function,the term vararsin{: } shouldbe addedto the end of its argumentlist (notethe curly brackets).Here is how both modificationscan be implemented for conciseness): fbr f uncavg' (clmittingcomnrent.s
ffi= ,t';rrA {
=1_h"
x = "L zJ
Zl F+z
I
3.6 CASESTUDY
7l
function favg = funcavg (f, a, b, n, varargin) x - linspace(a,b,n); y = f(x,varargin{:}); f avg = mean(Y) ; When the passed function is defined, the actual parameters should be added at the end of the argument list. If we used an anonymous function, this can be done as in .
vol=t?(r,m,edt
SqrL(q.Bl*m
edr*Lanh(sqrr_r9.Bl"cd
ml*Lr;
When all these changes have been made, analyzing different parameters becomes easy. To implement the case where m : 68.1 and c1 - 0.25, we could enter >> funcavg(ve1, 0,12, 64, 68. 1, 0.25) ans = 36 . Ar21 An altemative case, 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.28, could be rapidly generated by merely
>> funcavg(ve1, 4,f2, 60, 100, 0.28)
38.9345
BUNGEEJUMPER VELOCITY ) t I
Bockground. In this section,we will use MATLAB to solve the free-falling bungee jumper problem we posedat the beginningof this chapter.This involves obtaininga solution of
1
du cd .t -:8--UaIm
) e
Recall that, given an initial conditionfor time and velocity,the problem involved iteratively solving the formula, U i + l: u 1 *
v)n its ed
du' -At AI
Now also remember that to attain good accuracy,we would employ small steps.Therefore, we would probably want to apply the formula repeatedlyto step out from our initial time to attain the value at the final time. Consequently,an algorithm to solve the problem would be basedon a loop. Solution. Suppose that we started the computation at / = 0 and wanted to predict velocity at t : 12 s using a time stepof A/ : 0.5 s. We would thereforeneedto apply the iterative equation 24 times-that is,
t2
n---24 0.5
ffi q
,-ILJ' ?,tr+
b
{e[ --{
st -r
Z] X+
"L x 4
3$ 8,
72
PROGRAMMING WITHMATLAB
continued where n : the number of iterations of the loop. Becausethis result is exact (i.e., the ratio is an integer),we can usea for loop as the basisfor the algorithm.Here'san M-file to do this including a subfunction defining the differential equation: f uncLion vend = velocityl % % Z Z % % % Z % t % L 1,/
(dt,
t,i, rf , vi )
vel.ocityl: Iiuler solutj.on for bungee velocity vend = veloc-Ltyl (dt, ti, Lf , v1) [.uler uret]rod soluLion of bungerj u m p e ] r z eL o c i t y rnpuL: dL ,= t.rme step (s) ti = initi.al Lime (s) Lt = f,in.rl time (s) (rn/s) vi = initial val.ue of dependent vari.rble outDut: (n/s) v e n d = v e l . o c . iL v a t t f = Li; =
Vl,'
n =
(tf
Far
i
-
ti)
/
dL;
1.n
dvdt = deriv(v) ; v : v + dvdt * dt; t = t + dt; end Varnd = V,' end f unct ion d.; - deri-z (v) dv = 9.BL - (A.25 / 68.1) end
x v''2;
This function can be invoked from the command window with the result: >> vel,oci Lyl (0.5 ,0 , 1?,,l)) dns = 50.9259 Note that the true value obtained from the analytical solution is 50.6175 (Example 3.1). We can then try a much smaller value of dt to obtain a more accurate numerical result: > > v e . o c r i . r - y (10 . 0 0 1 , 0 , 1 2 , O )
50.6i.Bi
Althoughthis functionis certainlysimpleto program,it is not foolproof.In particular,it will not work if '.hecomputation intervalis not evenlydivisibleby thetime step. To cover such cases,a while . break loop can be substitutedin place of the shadedarea(notethatwe haveomittedthecomments for conciseness):
ffi= zt+
,f -\
= 4F+
\'r?+,
73
3.6 CASESTUDY
continued vend = velociLy2
function t = ti; ] - \
-
(dt,
t1,
t;
end
tf,
vi )
^ f .
while (1) if t + dt > tf, dvdt = deriv(v) v=v+dvdt*h; -
1-
t
r
if t >end vend = v; end
h = tf ;
-
h.
tf,
break,
end
f unct ion clv = deriv (v ) - 9.81. - (0.25 / 68.L) end
dv
*
v""2;
As soon as we enterthe while loop, we usea singleline if structureto test whether adding t + dt will take us beyondthe end of the interval.If not (which would usually be the caseat first), we do nothing. If so, we would shortenup the interval-that is, we set the variablesteph to the intervalremaining:tf * t. By doing this, we guaranteethat the last stepfalls exactly on t f . After we implementthis final step,the loop will terminatebecause the condition t >= tf will test true. Notice that before entering the loop, we assignthe value of the time step dt to another variable h. We create this dummy variable so that our routine does not change the given value of dt if and when we shortenthe time step.We do this in anticipationthat we might need to use the original value of dt somewhereelse in the event that this code were integrated within a larger program. If we run this new version,the result will be the sameas for the versionbasedon the for structure: ; ' > v e l o c i L y 2 ( 0 . 5 , 0 , l : 1, 0 )
5A.9 2\9 Further, we can use a dt that is not evenly divisible into tf :'-- velociLy2
-
ci:
(0. 35, A, 12, A)
50 - 834r1 We should note that the algorithm is still not foolproof. For example, the user could havemistakenlyenteredast3psizegreaterthanttrecalculationinterval(e.g., tf - ti = 5 and dt = 2 0). Thus, you might want to include error traps in your code to catch such errors
e
and then allow the user to correct the mistake.
ffi ,"I LJ' -b
ffii --I
\
x
a value.Designyour programso that it returnsboth thc rcsult ,, _,,,,, _ J I tt and the errur. Makc surc that it can cvaluatethc squareroot when -r < a I I o1'nunrbers thatarc equalto and lcssthanzero.For the latter versus casc,displaythe resultas an imaginarynumber.For examDevelop anM-filc thatcrcatcsa plot ol-displacemenl 21.Test your program distance alongthe beam,,r. Notc that,t : 0 at the leli end ol' plc, thc squareroot of -4 would retr.rrn b y e v a l u a t i nug- 0 , 2 , 4 , a n d- 9 f b r c : I x l 0 r . thebeam. .r/lr
^
ffi q A b
,"I LJ' zl'+
-._
tt
.\t'ltl
st
3l x+
Z} z,J I
PROGRAMMING WITHMATLAB
78
3,14 Piecewisefunctions are sometimesuseful when the relationshipbetweena dependentand an independentvariable cannot be adequatelyrepresentedby a single equation.For example,the velocity of a rocket might be describedby l1t2-5t ll00-5/ 50t+2(t-20)2 l52oe 0.2(t10) 0
0rba.se-8, representation. In the samesense, our fiiend the computeris like a two-fingcrcdanimalwho is limited to two states-either 0 or l. This relatesto the fact that the prirnarylogic unitsof digital computcrsare on/off electroniccomponents.Hence,numberson the computerare rcpresented with a birtary, system.Just as with the decirnalsystcm,quantitiescan be represented or ba"^e-2, using p o s i t i o n anl o t a t i o n F . o r e x a m p l c ,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 . 5 i n t h c d e c i m as ly s t e m . Integer Representotion. Now that we havereviewedhow base-10numberscan be representedin binarylorm, it is simplcto concciveof'how integersarerepresented on a cornputer.The most straightforwardapproach,callcd thc ,signadnngnitrule metfutd,employs the first bit of a word to indicatethe sign,with a 0 fbr positivcanda I firr negative.The remainingbits arc usedto storeths number.Forexample,thc integervalueof 173is represented i n b i n a r ya s l 0 l 0 l l 0 l : ( 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,thebinaryequivalentof - 173wouldbe storedon a l6-bit computer,asdepicted in Fig.4.2. If sucha schemeis cmployed,thereclearlyis a lirnitedrangeof integersthat can be represented. Again assurrring a l6-bit word size,if one bit is usedtbr the sign,the l5 remainingbits can represent binaryintegersliom 0 to I I I I I I I I I I I I I I I . The upperlimit can
FIGURE 4.2 T h eb i n o r yr e p r e s e n t o l i oonf 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 m o g n i l u d em e t h o d
t 1
Y
I
Sign
0
0
0
0
0
0
01110 Magnitude
1
0
1
0
1
86
ROUNDOFF AND TRUNCATION ERRORS be convertedto adecimalinreger,asin (1 * 2'o)+(1 x 2'3)+. . . + (l x 2') * (l x 20;= 32,161. Note that this valuecan be simply evaluaredas 2rs - l. Thus,a l6-bit computer word can storedecimalintegersrangingfrom -32,761 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 conventionally employedto representan additionalnegativenurrrber:-32.768, and the rangeis frorn -32.768 to 32,761. For an n-bit word,the rangewould be from -2" to 2" - l. Thus, 32-bit integerswould rangefrom -2,141.183.648 to +2,141,483,641. Note that,althoughit providesa nice way to illustrateour point, the signedmagnitude method is not actually usedto representintegersfor conventionalcomputers.A prefened approachcalled the 2s complemenltechniquedirectly incorporatesthe sign into the number's magnituderatherthanproviding a separatebit to representplus or minus.Regardless, the rangeof numbersis still the sameas for the signedmagnitudemethoddescribedabove, The foregoingservesto illustratehow all digital computersare limited in their capability to representintegers.That is, numbersaboveor below the rangecannotbe represented. A moreseriouslimitationis encountered in the storageandrnanipulation of fractit'rnal quantitiesas describednext. Flooting-PointRepresentotion. Fractionalquantitiesare typically represented in computersusing floating-point.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 onesignificantdigit is to thelefi of thedecirnalpoint.This is doneso cornpLlterrtlcmoryis not wustedcln storinguselessnonsignificantzeros.For example,a value like 0.00567ticould be represented in a wastefulmanneras 0.00-5671t x l0('.However,nort.ttalization would yield -5.678,. l0 I which eliminatesthe uselesszeroes. Beforedescribingthe base-2implementation usedon computers,we will first explore the fbndamentalirnplications of suchfloating-pointrepresentation. In particular,whatare the ramificationsof the fact that in order to be storedin the cornputer,both the mantissa and the exponentmustbe limited to a finite numberof bits?As in the nextexample,a nice way to do this is within the contextof our moretirmiliarbase-l0 decimalworld.
EXAMPLE 4.2
l m p l i c o t i o n so f F l o o t i n g - P o i nRte p r e s e n t o t i o n ProblemStotement. Supposethatwe hada hypothetical base-I0 computerwith a 5-digit word size.Assumethatonedigit is usedfbr the sign,two for the exponent,and two forthe mantissa.For simplicity,assumethatone of theexponentdigitsis usedfirr its sign,leaving a singledigit for its magnitude. Solution. A generalrepresentation of the numberfollowing normalizationwcluldbe s 1 d 1 . d x2 l O r o ' d )
tS
i"
:p
i
rq
F o
V
e) 0
of
pl TI or
VA
w( ca
* Th
fio
> formaL long >> sumdemo ans = 0.9999999999999r The f ormat long command lets us see the l5 significant-digit representationused by MATLAB. You would expect that sum would be equal to l. However, although 0.000 I is a nice round number in base-10, it cannot be expressedexactly in base-2. Thus, the sum comes out to be slightly diff'erent than l. We should note that MAILAB has features that are designed to minimize such e:'rors. For example, suppose that you form a vector as in >> format longr >> s = [0:0.0001:1]; For this case, rather than being equal to 0.99999999999991 , the last entry will be exactly one as verified by >> s (10001) 1
92
ROUNDOFF AND TRUNCAIIONERRORS
w e a d da s m a l l n u m b e r , 0 . 0 0 1t0o,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 computerwith the4-digit nrantissa andthe l-digir exponent.After modifying the smallernumberso that its exponentrnatchesthe larger, 0.4000x l0a 0.0000001x lOa 0.4000001x l0a which is choppedto 0.4000 x 104. Thus, we rright as well have not performeclthe addition! This type of errorcan occurin the computationof an infiniteselies.The initial terms in suchseriesareollen relativelylargein comparisonwith the laterterms.Thus.aftera few termshavebeenadded,we are in the situationof addinga smallquantityto a largequantity. One way to mitigatethis typeof erroris to sum the seriesin revelseorder.In thisway, to the accurnulatecl surn. eachnew ternrwill he of comparablernagnitude Smeoring. Smearingoccurswheneverthe individualterms in a summationare larger thanthe sunrmationitself.One casewherethis occursis in a seriesol'rnixedsicns. Inner Products. As shouldbe clearfrom the lastsections,someinfiniteseriesureparticthe calculationof seriesis not one o1'themore ularly proneto roundolTerror.Fortunately, A lll more ubiquitousmanipulationis thecalcommonoperationsin numericalnrethods. c u l u t i o rttr f i n n e rl ) r o d u e (uss i l l
: .trr,vr *,r:.v:*''' * x,,-)',, E.tr.t' linearalgebraic This operationis very comrron,particularlyin thesolutionol-simultaneous Suchsummations areproneto roundoll error.Cc'rnsequently. to equations. it is ofiendesirable in doubleprecisionns is doneautorraticallyin MATLAB. computesuchsumrnations
Trwrccttion(rrors are those that result fiom using an approximationin place ol- an exact procedure. For example.rn Chap.I we approximatedtheclerivative rnathematical 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 q u a t i o no l ' t h ef i r r n r[ E q .( l . l l ) l ity of a bungee dt
Lt
u(ri+r) u(t1) t;"t I -
ze
pr fu
do rel
po
4.3 TRUNCATIONERRORS
du-A,u
FI
T
(48)
idt
gu wt no
ti
A truncationerrorwas introducedinto the nurnericalscllutionbecause the difl'erence equation only approximates thetruevalueof thederivative(recallFig. 1.3).To gain insightinto thepropertiesof sucherrors,we now turn to a rnathematical tormulaticln thatis usedwidely fashion-the Taylol series. in numericaln'rethods to expressfurrctionsin an approxinrate
4.3.1 The Toylor Series Taylor'stheoremand its associated formula,theTaylorseries,is of greatvaluein thestudy of nurnericalrnethods.[n essence.the Tay'lorthertremstatesthat any smoothtirnctioncan be approxirnatedas a polynomial.The Tay-lor.renes then providesa meansto expressthis in a form thatcanbe usedto comeup with practicalresults. ideamathematically
Th
.f ar ber ilar
fac on tior
leg
93
ERRORS 4.3 TRUNCATION
Zero order Jk,n):fk)
\:[:*':d::-s
/(.r,.1)= ^xt - r'(,,th
\.'3"^
r;+t : 1
ri=0 h
FIGURE 4.6
T h e o p p r o x i m o l i o on { / ( - r ) : O l - r a- O l 5 - r r O 5 . r r - a 2 5 x * l 2 o t r : zero order,firstorder,ond secondorder Toylor seriesexponsions.
l by
A usel'ulway to gain insightinto the Taylor scriesis to build it term by terrn.A good problcrnc()ntexttbr this cxerciseis to predicta functionvalucat one point in ternrsof the firnctionvalueand its dcrivativesat anothcrpoint. Supposethat you ale blindfbldedancltakcn to a locationon thc side of a hill facing downslopc1Fig.4.6).We'll call your horizontallocationJriand your vcrticaldistancewith respectto thc baseof the hill l(.r;). You are givcn the task of pledictingthe heightat a p o s i t i o n, r ; 11 ,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 . At first, you areplacedon a platfbrmthat is completelyhorizontalso thatyou haveno idea that the hill is slopingclownaway fiorn yon. At this point,what would be your best guessat the heightat x;11'?If you think aboutit (remcmbcryou haveno ideawhatsoever what'sin front of you), the bestguesswould be the sameheightas whereyou're standing now! You c 0
PT2.I FIGURE the differencebelween rootsond oplimo A funciionof o singlevoriobleillustrciing
Suchhaphazardmethodsareobviouslyinefficientand inadequatefor the requirements of engineeringpractice.Numerical methodsrepresentalternativesthat are also approximate but employ systematicstrategiesto home in on the true root. As elaboratedin the following pages,the combinationof thesesystematicmethodsand computersrrrakesthe problemsa simple and efficient task. solutionof most appliedroots-of-equations Besiclesroots,anotherfeatureof lunctionsof interestto engineersand scientistsareits minimum and maximum values.The detenninationof suchoptimal valuesis ref'en-ed to as you As learned in calculus, solLltions be obtirined analyticalty optimi.ation. such can by deat which function f'lat; is, its derivative is zero. terminingthevalue the is that where Although such analyticalsolutionsare sometimesfeasible,most practicaloptimizationproblemsrequire nunrerical,computer solutions.From a numerical standpoint,such numerical optimizationmethodsaresinrilarin spirit to the root locationmethoclswejust discussed. Thatis, guessing fbr The involve and location on function. fundamental difference both searching a a betweenthe two types of problemsis illustratedin Figure PT2.1. Root location involves searchingfor the locationwherethe functionequalszero.In contrast.optimizationinvolves searchingfbr the functiorr'sextremepoints.
2.2
PARTORGANIZATION The first two chaptersin this part are devotedto root location.Chapter5 focuseson bracketing methodsfor finding roots.Thesemethodsstartwith guessesthat bracket.or contain, the root and then systematicallyreducethe width of the bracket.Two specificmethodsare covered:bisectionand.falseposition. Graphicalmethodsare usedto provide visual insight into the techniques.Error formulationsare developedto help you determinehow much computationaleffort is requiredto estimatethe root to a prespecifiedlevel of precision. Chaltter6 coversopenruetltods.Thesemethodsalso involve systematictrial-and-error iterationsbut do not requirethat the initial guessesbracketthe root. We will discoverthat thesemethodsare usuallymore computationallyefficientthan bracketingmethodsbut that they do not always work. We illustrate severalopen methods including theJixed-point iteration, Nev,ton-Raph.rolr, and secantmethods.
2.2 PART ORGANIZATION
S L '
K-
n, re nt )h or at at nt
rr3
Following the descriptionof theseindividual open methods,we then discussa hybrid approachcalledBrent's root-findingmethodthat exhibits the reliability of the bracketing rnethodswhile exploiting the speedof the open methods.As such, it fbrms the basis fbr MATLAB's root-findingfunction,f zero. After illustratinghow f zero canbe usedfor engineeringand scientificproblemssolving, Chap. 6 endswith a brief discussionof special methodsdevotedto finding the rootsof polynontials.In pafticular,we describeMATLAB's excellentbuilt-in capabilitiesfor this task. Chapter Z dealswith optimization.First,we describetwo bracketingmethods,goldensectionsearchandparabolic interpolation,for finding the optima of a function of a single variable.Then,we discussa robust.hybrid approachthat combinesgolden-section search and quadraticinterpolation.This approach,which again is attributeclto Brent, fbrms the basis for MAILAB's one-dimensionalroot-finding function:fminbnrl. After describing and illustrating fmlnbnd, the last part of the chapterprovidesa brief descriptionof optimization of multidimensionalfunctions.The emphasisis on describingand illustratingthe use of MAILAB's capability in this area:the fminsrearch function. Finally, the chapter endswith an exampleof how MATLAB can be employedto solveoptimizationproblems in engineeringand science.
.\
Roots:Brocketing Methods
N et
rf
5.r R
A ar dr rh
CHAPTEROBJECTIVES Thc primaryobjectiveof thischapteris to acquaintyou with bracketingmcthodsfbr findingthe rootof a singlenonlinearequation.Specificobjectivesandtopicscoveredare ' ' . . ' .
what rootsproblemsnrc and wherethey occurin cngineeringand Uncierstaniling science. Knowing how to determinea root graphically. Llnderstanding the incrementalsearchmethodand its shortcomings. Knowing how to solvea rootsproblemwith the bisecl.ion rnethod. Knowing horvto cstimatetheerrorol'biscctionanclwhy it difl'crsliom error estimatesfbr othcrtypesclf root locationalgorithms. Understanding lalsepositiclnand how it difl'ersfiom biscction.
e d i c a ls t u d i e sh a v ec s t a b l i s h ctdh a t a b u n g e ej u n r p c r ' sc h a n c e so f s u s t a i n i nag signilicantvertebraeinjury increasesignificantlyit'the liee-1allvelocityexceeds 36 m/s afier4 s of fiee fall. Your bossat the bungec-jumping compirnywantsyou to detet'minethe ntassat which this criterion is exccedeclgiven a drag coeliicientof 0.2-5kg/rn. You know f}om your previousstudiesthat the fbllclwinganalyticalsolutioncanbe usedto prcdictfall velocityas a functionof tirne: lntf^t r ' ( r ) : , / i - r i r n h' , l : - , |
V t,r
\V rtt /
ra tic pa
mi pe an ca
rt_B-lE s.l Fund3
YOU'VE GOT A PROBTEM
rsrr
Try as you might, you cannotntanipulatethis equationto explicitly solvefbr rn-that is, you cannotisolatethe masson the lefi sideof the equation.
tl4
at pr
Fundomentol Principle
De Vc
Heotbolonce Mossbolonce
T^.
Energybolonce
Cc of Mc ofl Ch
Newton'slows of motron Kirchhoff s lows
orl Cu
Forcebolonce
AC(
tt5
5 . I R O O T SI N E N G I N E E R I NAGN D S C I E N C E
An alternativeway of looking at the probleminvolvessubtractingu(t) from both sides to give a new function:
:,1*,un(,F') -,,', r(m)
(5 . 2 1
Now we can seethat the answerto the problem is the value of ru that makesthe function equalto zero.Hence.we call thisa "roots"problem.This chapterwill introduceyou to how the computeris usedas a tool to obtainsuchsolutions.
5.I
R O O T SI N EN GIN E E R INAGN D S CIENCE Although they arise in other problem contexts,roots of equationsfrequentlyoccur in the principlesthatareroutinelyusedin areaof design.Table5. I listsa numberof tundamental designwork. As introducedin Chap. 1, mathematical equationsor modelsderivedfiom theseprinciplesare employedto predict dependentvariablesas a function of independent rurrr,b\t:. torii.r,g{i,:r,:'j,cr,:.'s.dpN.rm'-t--rs.\ote \hrt \r.',rlh'i.rsi.'Jrt dtpendtrt. r'*i,ables ret'lectthe stateor pertbrmanceof the syster.n. whereasthe pararnetersrepresentits propertresor composition. An examp\e of such a mode\ is the equationfor the bungee\umper's ve\ocity. If the pa.'-'.::':r..|i:.-.-'.:.'.):l:.:,'
.i\'..'i:..-\.^\'::-^\j-'-::-'..'.:.-'-\-:.:':.).r:'-
) S
1 -:.i..
parameters. That is, it is isolatedon one sideof the eqLralsign. However, as posed at the start of the chapter,supposethat we had to determinethe ma.ssfbr a jumper with a given drag coefficientto attaina pre.scribedvelocirytin a .settinre period.AlthoughEq. (5.1) providesa mathematical representation of the interrelationship it cannotbe solvedexplicitlyfbr nrass.In such amongthe modelvariablesarrdparameters, cases./r, is said to be irrrnlicit.
5. t
principlesusedin designproblems Fundomentol Dependenl Vorioble
D0iOriCe bolonc-. 00lonce
Independent Vorioble
tenpeteioie C o n c e n l r o l i o no r q u o n t i t y of moss M c a n i t u d e o n d d r t e c lo n
ine
CtC
pOS,it)rl
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
. . ::] ewlonslorts ol )l of rchhoff s iorrys
p o t e n t l oe l nergy Acce/ercllion,veloci\', or ocotron Currenlsond vo/toges
Poromelers Itei,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 ,m o s sl r o n s f e r , syslem(leomelry 5 l r e r r g t lor l r n o l e r i o ls. l r u c i u r cpl r o p e r le s , s y s l e r n
qeometry T r m eo n d p o s r t i o n Lme
Alc-rss of molerrol syslemgeomefry, dissipoiive potcmerers F l o r t r l r' -a l n r - n o r l r a q r"f
induclonce)
/ro>
fm-G(rn)
>>
[mass
mclSS
(9.81*11.25lm)
*4)
Fr
36;
Fo
=
142.13'11 5.3450e
0Olr
pla q ' " ico
2.r Thus,a resultof nr : 142.1311kg is obtainedafter2l iterationswith an approximate relativeerrorol'€,,:0.0000-534-50/o.wecansubstitutetherootbackintothel'unctiontoverify that it yieldsarvaluecloseto zero:
is bis
EXAMPLE 5.5 Th
>:' fm(mass)
Prc
cal 4.6089c
007
So xu
5.5
FArSEPOSTTTON
Fir
Falseposition(alsocalledthe linearinterpolation method)is anotherwell-knownbracketing method.It is very similarto bisectionwith the exceptionthatit usesa diffbrentstrategy to come up with its new root estimate.Ratherthanbisectingthe interval,it locatestheroot . h e i n t e r s e c t i oonl ' t h i sl i n ew i t h b y j o i n i n g . l ( r r ) a n d . l ( , r , , )w i t h a s t r a i g hlti n e ( F i g . 5 . 8 ) T the r axis represents an improvedestimateof the root.Thus,the shapeof the functioninfluencesthe new root estimate.Using similartriangles,the intersection of the straightline with ther axis can be estimatedas (seeChapraand Canale,2002,for details), f (.r,,)(x1- r,,) .f Gt) - .f (.Y,,)
(5.7)
wh Se
5.5 FALSE POSITION
129
lre ror ir the )ed
ter. rity 3to
root rine 6;
F I G U R E5 . 8 F o l s ep o s i t i o n
This is the.fhl,sc-po,sition.litrmula. The vah-re of ,r,,computedwith Eq. (-5.7)then rep l a c c sw h i c h c v eor f t h et w o i n i t i a lg u c s s c s , . o r /r . r , , ,y i e l d sa l i r n c t i o nv a l L r w e i t h t h es a m e sign as l (.r,). In this way the valuesof .r7andr,, alwaysbracketthc true root.The proccss is repeateduntil the loot is estimatedadequately. Thc algorithmis identicaltclthe one for ( F i g .5 . 7 . w bisection 1 i t h t h r :c x c e p t i o n t h a rE q . ( 5 . 7 )i s u s e d .
relaerifY EXAMPLE 5 5
The Folse-Position Method Problem Stotement. Use lalse position1o solvc the sameproblernapproachecl graphic a l l ya n dw i t h b i s e c t i o ni n E x a n r p l e5s. 1 a n c -l 5 . 3 . S o l u t i o n . A s i n E x a m p l e- 5 . 3 ,i n i t i a t ct h e c o m p u t a t i o nw i t h l l u e s s e so f . r 1- 5 0 a n d .r,,: 200. F i r s ti t c l u t i o n :
rcketategy 3 root ) with )n inlt line
.rt : 50 'r,, : 200 .\,:200 -
./ (.ri) : -'4.519381 .f (.r,,) 0.860291 r(50- 2(n) 0.rJ6029
: l 76 . 2 1 l1 - 4 . 5 1 9 3 8 1- 0 . 8 6 0 2 9 1
which hasa true rclativeerrorof 23.5clo Seconditeration: (5.7)
. l ( . r r )f 6 , ) : - 2 . 5 9 2 1 3 2
t30
ROOTS: BRACKETING METHODS Therefbre,the root lies in the first subinterval,and r,. becomesthe upperlimit for the next iteration.r,, : 116.2713.
.f (rt) : -4.519381 .r, : 176.2113 t'Q,) :0.566114 0.566t74(50- t] 6.2113) : . r , : 1 1 6 . 2 7 7 3 -1519381 - 0.566174 t 62.3828 ,.-{n
which has true and approximaterelativeerrorsof 13.16a/c and 8.-56olc, respectively. Additional iterationscan be performedto refinethe estimates of the root.
Although firlse positionolien perfbrmsbetterthan bisection,there are other cases where it does not. As in the fbllowing exarnple,thereare certaincaseswhere bisection yieldssupericlrresults. E X A M P L E5 . 6
A C o s e W h e r e B i s e c t i o nl s P r e f e r o b l et o F o l s eP o s i t i o n ProblemStotement. Use bisectionand lalseposition(o locatethe root of 'l(r) :'r"'-l b e t w e e n- r : 0 r n d 1 . 3 . S o l u t i o n . U s i n sb i s e c t i o nt .h e r e s u l t sc a nb e s u n r m a r i z eads Iterotion l
2 ,J
4 5
xt
u
O (r5 0 975 o 975 o 975
X,,
3 ,J J
137 5 45625
r0 6.5 4 975 I 1375 ) 05625 I 0t5625
e" ("/"1
e, (o/ol
r 0 00 333 1 43 77 4A
35 25 t3B 56 6
Thus, alier l'ive iterations,the true error is reducedto lessthan 27r,.Frlr falseposition, a very diffbrentoutcomeis obtained: herotion I 2 3 4 5
xt
0 a a943O 0 tBtz6 o 26287 0 3 3 8 II
3 3 3 3 3
.r,
e. (Y"l
0 09430 a tBl76 o 26287 0338Ir a 40788
48I 309 223 l7 l
e,(7"1 906 BIB 737 662 592
After five iterations,the true error hasonly beenreducedto about59%. Insightinto theseresultscan be gainedby exarlininga plot of the l'unction.As in Fig. -5.9.thecurve violatesthe prerniseon which fhlsepositionwas based-that is, if .l(.rr) is muchcloserto
POSITION 5.5 FATSE
r3l
he next
r cases section
(;; ", 35 2.5 r3 8 56 t6
; r t l o n ,a
e, (7"1 906
Br 8 737 66.2 5q2
ght into .e curve loser to
F I G U R E5 . 9 P o to f . F ( x ): x l r t
n ethod { t h ef o l s ep o s i i i om ce 1 , i l l u s t r o t isnlgo wc o n v e r g e n o
zerothanf(x"), thentheroot is closerto x1thanto x,, (recallFig. 5.8).Becauseofthe shape of the presentfunction,the oppositeis true.
regardingroclt-location illustratesthatblanketgeneralizations The fbregoingexarrrple positiorris often supeas lalse method such possible. a Although rnethodsare usuallynot Therefbre, general conclusion. that violate this rior to bisection,thereare invariably cases the root substituting (5.5), checked by be the resultsshouldalways in additionto usingEq. zero. to the result is close whether estimateinto the originalequationand determining method:its oneThe examplealso illustratesa major weaknessof the false-position points will tend to bracketing of the one That is, as iterationsare proceeding, sidedness. significant fbr with particularly functions stay fixed. This can lead to pool'convergence, (Chapra and elsewhere available curvature.Possibleremediesfor this shortcomingare Canale.2002).
r32 'il
ROOTS: BRACKETING METHODS
{
'
"ii'i*,iry'ii"",.lf,iei{rl:
GASES AND RAINWATER GREENHOUSE ,"' ,,,,SY Bcckground. It is well documentedthat the atmosphericlevels of severalso-called "greenhouse"gaseshave been increasingover the past 50 years.For example,Fig.5.10 shows data for the pafiial pressureof carbon dioxide (COr) collected at Mauna Loa, Hawaii from 1958through2003.The trendin the datacanbe nicely fit with a quadraticpolynomial,3 p c o , : 0 . 0 1 1 8 2 5 ( r - 1 9 8 0 . 5 )+2 1 . 3 5 6 9 7 5-( t 1 9 8 0 . 5+) 3 3 9 where pgo, - COz partial pressure(ppm). The data indicate that levels have increased a little over 19Voover the period from 315 to 376 ppm. One questionthat we can addressis how this trend is affecting the pH of rainwater, Outsideof urban and industrialareas,it is well documentedthat carbondioxide is theprimary determinant of the pH of the rain. pH is the measureof the activity of hydrogenions and, therefore,its acidity or alkalinity.For dilute aqueoussolutions,it can be computedas pH:
-log,n[H+]
6.s)
w ul
tr
st
l(
rh
where [H+] is the molar concentrationof hydrogenions. The following five equationsgoverx the chemistryof rainwater:
fc
^ , ,I H * J" l H C O i I K r' - 1 0 " ' Knpco.
(5.9)
a
5r is pc
Sr
1960
1980
1990
2000
2010
Er
FIGURE 5.IO Averogeonnuol portiolpressures o{ otmosphericcorbon dioxide (ppm)meosuredol MounoLoo, Howoii.
A
tr AI I In Part Four, we will learn how to determinesuchpolynomials.
fa
5.6 CASESTUDY
continued
-
1ll:-
tH+llcol2l
tHcotl
(5.10)
K,,,: [H-][OH-]
( 5 . lr)
, r' : f u # +
(5.r2)
106
tHcotl11coJ2l
0: tHcoil + 2tco;21+toH-l - [H+]
( s .3 1)
where Ks : Henry's constant,and K1, K2, and K,, areequilibrium coefficients.The five bicarbonate,tCO;21 : carbonate. unknowns utec7 : total inorganiccarbon,tHCOtl: : hydroxyl ion. Notice how the partialpressureof CO2 [H*] : hydrogenion, and tOH-l showsup in Eqs.(5.9)and (5.12). Use these equations to compute the pH of rainwater given that K n = l 0 - 1 4 6 ,K 1 : l Q - 63 , K z - 1 0 - t o : . a n dK , : l Q - l a . C o m p a r et h er e s u l t si n 1 9 5 8w h e n the pgo. was 3 l5 and in 2003 when it was 375 ppm. When selectinga numericalmethod for your computation,considerthe following: . .
You know with certainty that the pH of rain in pristine areasalways falls between2 and 12. You also know that pH can only be measuredto two placesof decimalprecision.
Solution. There are a variety of ways to solve this systemof five equations.One way is to eliminate unknowns by combining them to producea single function that only dependson [H+1.To do this, first solveEqs.(5.9)and (5.10)for
tHcotl:-fu, Kupco,
(5.r4)
{#F rcor2l:
(5.I s)
S u b s t i t u tE e q . ( 5 . 1 4 )i n t o ( 5 . 1 5 )
: tco;21 ffi*rnro"
( 5 .I 6 )
alongwith Eq. (5.11)into Eq. (5.13)to give Equations(5.14)and(5.16)canbe substituted
o:
#*,
'z##I KuPcoz. - [H+] Ku.co'+ #
(s.l7)
Although it might not be immediatelyapparent,this result is a third-orderpolynomial in [H+]. Thus, its root can be usedto computethe pH of the rainwater. Now we must decidewhich numericalmethodto employ to obtainthe solution.There are two reasonswhy bisectionwould be a good choice.First, the fact that the pH always falls within the rangefrom 2 to 12, providesus with two good initial guesses.Second,becausethe pH can only be measuredto two decimalplacesof precision,we will be satisfied
t34
ROOTS: BRACKETING METHODS
continued with an absolute error of Ea,d : t0.005. Remember that given an initial bracket andthe desirederror,we can computethe numberof iterationa priori. Substitutingthe presentvaluesinto Eq. (5.6) gives r l . ' .- L ) . . ' ,: '
l:i.rrl-1-l.1ll)t); lri:'.'ll r,lr
'
rr-ll,t.'
I ( r . t l L , ' ri l
Eleven iterationsof bisectionwill producethe desiredprecision. Befbre implementingbisection,we must first expressEq. (5.17) as a function.Becauseit is relativelycomplicated,we will storeit as an M-file: lii|aij f.L.-l 1 l . . i l. r r ) ll
li)
I
i.l
,. irrrl I ',.:,;1,.
1l)l (ljil,iiir.
l
iLr. i;i';,t
ILI
i i
i i;
t,;
L,l i.,l;
ll'.{,*lll*llj*1.')l
lrl
)*l'i
(L,r,1li)ll.ll^i,l'i,ir
",ll
ll;
We can then use the M-file from Fig. 5.7 to obtain the solution.Notice how we have set the value of the desiredrelativeerror (r., : I x l0-8) at a very low level so thattheiteration limit (maxit) is reachedfirst so that exactly I I iterationsare implemented lrijlli,ir
I ilI't
,i
',I
|r1i1l,'',i.',
tt,rll
r,)
,
,1r ,- {).lr'(l
l-ilrli,l-l
ll
Thus, the pH is computedas 5.6279with a relativeerror of 0.0868%.We can be confident that the roundedresultof 5.63 is correctto two decimalplaces.This can be verifiedby performing anotherrun with more iterations.For example,settingmaxi t to 50 yields ll,lll'r iill"
,,
l 'i . i, rit i
'r. li,'lLl, tl
rr
I llrr
t,l
],. For 2003,theresultis
rl
il
l';.
r irilill,
,,'.,
,
i,
ll,)
PROBTE
5.1 Use bisectionto de so that an 65-kg bungee 4.5 s of freelall. Note:Tl Start with initial guesse ate until the approximat 5.2 Develop your own J ion to Fig. 5.7. Howev iterationsand Eq. (5.5) criterion. Make sureto r next highest integer.Ter using 8 ,,,7: 0.0001. 5.3 RepeatProb. 5. I, t obtain your solution. 5.4 Develop an M-file b y s o l v i n gP r o b . 5 . l .
r35
PROBLEMS
lii ;L;,1,ri{ , :ll ri. i'
tt'_,,,'
continued ll .0E74 i r ^,-
-
tL
Interestingly,the resultsindicatethat the l9%orise in atmosphericCO2 levels has produced only a 0.67Vodrop in pH. Although this is cenainly true, rememberthat the pH representsa logarithmic scaleas definedby Eq. (5.8).Consequently,a unit drop in pH representsa l0-fold increasein the hydrogen ion. The concentrationcan be computed as [H*l : lO-pHand its percentchangecan be calculatedas. I
clil:l
I
1112.,
I
r
. \
lr
p ] ]1 9 5 8 ) * 1 0 0
:
( r .o q l ( l Therefbre,the hydrogenion concentrationhas increasedabout.97o. There is quite a lot of controversyrelatedto the meaningof the greenhouse gastrends. Most of this debatefbcuseson whetherthe increasesare contributingto global warming. However,regardless of the ultimateimplications,it is soberingto realizethatsomethingas largeas our atmospherehaschangedso much over a relativelyshorttime period.This case study illustrateshow nunrericalmethodsand MATLAB can be employedto analyzeand interpretsuchtrends.Over the coming years,engineersand scientistscan hopefully usesuch tools to gain increasedunderstanding of suchphenomenaand help rationalizethe debate over their ramifications.
PROBTEMS l l U s eb 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 m t h aatn6 5 - k gb u n r c c . j u n r p c r h u s a v c l o c i t y o l ' 3 5 n r / s a l t c r '
soffrcelall. Notc:Thc accclcration ol'gravitvis 9.8I nr/sl initialgLrcsscs Stafiwith of',ri :0.2 and i,, :0.3 anclitcrthcapproxilnatc unlil rclativccrror lirllsbcl I theenors if lg'l < I. the errorsdecrease grow. Notice also that if the derivativeis positive,the enors will be positive,andhencethe errorswill havethe samesign (Fig.6.3aandc). If the derivativeis negative,theenorswill changesign on eachiteration(Fig. 6.3b andd).
6.2
NEWTON.RAPHSON Perhapsthe most widely usedof all root-locatingformulasis IlteNewtott-Raphsott method (Fig. 6.a). If the initial guessat the root is .r;, a tangentcan be extendedfrom the point The point wherethis tangentcrossesther axisusuallyrepresents an improved Iri, I (,,.;)1. e s l i m a t co l ' t h er o o t . The Newton-Raphsonmethod can be derived on the basisof this geometricalinterpretation.As in Fig. 6.4, the first derivativeat.r is equivalentto the slope: /('r't-o
/'('r't:
Ji -'ft+l
f
which can be rearranced to vield -
' r r + l: ' ,
.l Gi)
( .l
(66)
f o,
which is calledtlreNcwton-Raph.son lbnnulu. E X A M P L E6 . 2
N e w t o n - R o o h s oM n ethod Problem Siotement. Use thc Newton-Raphson rnethodto estimatethc root of /(x) = ? ' - .tremployingan initial guessol'.r1;- 0. Solution. The I'irstderivativeo1'thefunctioncan bc cvaluatcdas /'tt) :
- n c w L r . r p h ( - { , d y , 1 , 1t l , 0 . 0 0 0 0 1 ) \.\2.'tj]6
6.3 SECANTMETHODS As in Example6.4, a potentialproblemin implementingthe Newton-Raphson methodis the evaluationof the clerivative. Although this is not inconvenientfbr polynomialsand many other tunctions,thereare certainfunctionswhosederivativesmay be difficult or
rso
ROOTS:OPEN METHODS
6.
inconvenientto evaluate.For thesecases,the derivativecan be approximatedby a backward finite divided difference:
Se
f (xi-t) - .f(ri) xi-t
- xi
This approximation can be substitutedinto Eq. (6.6) to yield the following equation: xi+t : xi -
/(xi)(xi
t
-
xi)
./(xi-r) - .f(x,1
Th
(6.8)
Equation(6.8) is the fbrmula for the secantmethod.Notice that the approachrequirestwo initial estimatesof x. However,because.f (.r) is not requiredto changesignsbetweenthe estimates,it is not classifiedas a bracketingmethod. Rather than using two arbitrary values to estimatethe derivative,an alternativeapproach involves a fractionalperturbationof the independentvariableto estirnate./'(.r), .f
-./(,r,) ' { x i \ = .f (xi + 6.ri) dri
where 6 : a small perturbationfraction.This approximationcan be substituted into Eq. (6.6)to yield the following iterativeequation: ri+l
:
"tr
-
6x;.l'(x;) .f(xi+lxi)-
swa Eq.
(6.9) f(.ri)
der
We call this the modified secan.tnrcthod.As in the fbllowing example,it providesa nice meansto attain the efTiciencyof Newton-Raphsonwithout having to computederivatives,
E X A M P L E6 . 5
asi alw cha nifir rese met
M o d i f i e d S e c o n tM e t h o d Problem Stotement. Use the modified secantmethod to determinethe massof the bungeejumper with a drag coefficientof 0.25 kg/m to havea velocity of 36 m/s atter4 sof f r e e f a l l . N o t e : T h e a c c e l e r a t i ognr oa fv i t y i s g . 8 lm / s 2 . U s e a n i n i t i a l g u e s 5 so 0 fk g a n d a value of I 0 6 for the perturbationfraction. into Eq. (6.9)yields Solution. Insertingthe parameters First iteration: xrt : 50 .ro*dru:50.00005 Jl
- 5r)-
l'(xo) : -4.57938708 I l8 ,f (xo+ 6xo)- -4.579381
I 0-b(50)(-4.57938708) -4.57938rI r8 - (-4.57938708)
= 8 8 . 3 9 9 3 1 ( l:€ ,318 . l % ol e; o l : 4 3 . 4 V o )
6.4
Ml The but ofb fepr
whe Nott
whe
F U N C T I O Nf: z e r o 6.4 MATLAB
l5l
Seconditeration:
.f(.r-r): -1.69220111 ./(.rr+dxr) - -1.692203516
.tr : 88.39931 . r 1f d r 1 : 8 8 . 3 9 9 4 0 . r r : 8 8 . 3 9 9 3l :
10-6(88.3993 1)(- 1.6922017 t) -t.6922035r6 - (- 1.69220't1t)
je,| : 124.08910(
l3.l7c: lo,l :28.16a/")
The calculationcan be continuedto yield i 0 I
,2 3 4 5 ()
xi -500000 B B3 9 9 3 t24 ABS7 ) 4 A5 4 1 7 t 42 7A72 1 4 27 3 , 7 6 14 2 7 3 7 a
letl, "/" 64971 38064 I3064 t 53B o o2t 4 l x lO' 3 4 x l0 '
le.l, T" 43 438 28 762 II 746 I 5t7 a a2t 4 I x lO ''
The choiceof a propervaluefor 6 is not automatic.If J is too snrall,the methodcan be swampedby round-oft error causedby subtractivecancellationin the denominatorof Eq. (6.9).If it is too big, the techniquecan becorneinefTlcient arrdevendivergent.However. if chosencorrectly,it providesa nice alternativefor caseswhere evaluatingthe d e r i v a t i v ei s d i f f l c u l ta n dd e v e l o p i n tgw o i n i t i a lg u e s s eiss i n c o n v c n i e n t . generalsense,a univariatefunctionis rnerelyan entitythat returns Further,in its m> x = fzero(Glx)
x^2'.;,
4)
x3 If we warrt to find the positive root, use a guess that is near it: > > x = f z e r o ( ( 4( x )
-r^2 9,4)
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 , i t l i n d s t h e n e g a t i v er o o t >> x -
f :ero(tj (x)
x^2
9,0)
F C
x-
1
3 If we wanted to ensure that we fbund the positive root, we could enter twil guessesasin
rh >:.
1, -
f2c.1-6(t;l(1)
x'2
!),|0
Ii)
IS
x3 an errormessage is displayed Also, if a signchangedoesnot occurbctweenthetwo guesses, o{ >.: x
-
fzcro((4(x)
x-"'),9,1-4
| . , r : . : I 1 r u, ir,1 'lhe' f unct-ion val ues; .r1- Lhc clif fcr
in
4l)
ill
pa us c , ' t v . r1 e ' n c l p o i f l l . : j l i l u : i t
srgrr.
The f zero lunctionis a combinationof the relitble bisectionrnethoclwith two t'aster interpolation. lrn'erscquudruticinteralgorithnrs:the secanllnethodanclinversequaclratic poltttionis sirnilarin spiritto the secantmethod.As in Fig. 6.8rr,the secantrnethodisbased l'he intelsection of thisstraight on computinga straightline thatgoesthroughlwo guesses. line with the,r'axisrepresents the new root estimate.The inversequadraticinterpolation that usesa similarstrategybut is basedclncomputinga quadraticequation(i.e.,a parabola) goesthroughthreepoints(Fig. 6.8b). The f zcro functionworks as filllows. lf a singleinitial guessis passed,it firstpersearch forms a searchto identily a sign change.This searchdifl'ersfrom lhe incremental in thatthe searchstartsat the singleinitial guessandthentakes describedin Section5.-1.1, increasirrgly bigger stepsin boththe positiveandnegativedirectionsuntil a signchangeis detectecl. Thereafter,the fast methods(secantand inversequadraticinterpolation)areusedunresultoccurs(e.g..the root estimatefalls outsiclethe bracket). lf abad lessan unacceptable root is obtainedwith oneof resulthappens,bisectionis inrplemented until an acceptable
wl et( thi
EXAMPTE 6.6 Th
Pr< ,r l' pr(
So
F U N C T I O Nf: z e r o 6.4 MATLAB
J(x)
(al
FIGURE 6.8 N.o i e t h o tt h e C o m p o r i s o no 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 eq u o d r o f i ci n t e r p o l o t l o 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" e c o u s et h e q u c d r o t i cf u n c t i o ni s w r i l i e ni n . r ' r o l h etrh o n i n
thefastmethods.As might bc expected,bisectiontypicallydominatesat first but asthe root is approached, the techniqueshiftsto the tasterrrrcthods. A morecompletercplescntation of the f'zcrrosyntaxcan be writtenas Ix, fx]
i z , e r c :\ f u n c : Lj o n , x A , o l . t t i o n s , p l , p 2 , . . . J
-
where I xr , f ;- opl-ions = optim:,;e1,('l-olx', 1e-l); = tzcro(G (x) x^10 1,0.5,options) >) lx,l.xl
Di
{.s
the
1 0009 0.0090
6.5
POTYNOMTATS Polynomialsare a specialtype of nonlinearalgebraicequationof the generalfbrnr . f r ( . r ): d 1 , t "* a 2 x ' ' - t+ . . ' + o u t . y 2| u , , . !t a , , a 1
ertJ the MI tior foll
r55
6.5 POLYNOMIALS
where n is the order of the polynomial, and the a's are constantcoefficients.In many (but not all) cases,the coefficientswill be real.For suchcases,the rootscan be real and/orcomplex. In general,an nth order polynomial will haven roots. Polynomialshave many applicationsin engineeringand science.For example,they are used extensivelyin curve fitting. However,one of their most interestingand powerful applicationsis in characterizing dynamicsystems-and,in particular,linear systems. andelectricalcircuits. Examplesincludereactors,mechanicaldevices,structures, 6.5.1 MATLAB Function: roots If you are dealing with a problem where you must determinea single real root of a polymethodcanhaveutility. nomial,the techniques suchasbisectionandthe Newton-Raphson However,in many cases,engineersdesireto determineall the roots,both real and complex. like bisectionand Newton-Raphson arenot availablefbr simpletechniques Unfbrtunately, determiningall the rootsof higher-orderpolynomials.However,MATLAB has an excellent built-in capability,the roots function,fbr this task. The roots functionhasthe svntax. x..
roots(c)
wherex is a columnvectorcontainingthe rootsand c is a row vectorcontainingthe polynomial'scoefficients. So hclwdoesthe roots functionwork? MATLAB is very good at finding the eigenvaluesof a matrix.Consequently, the approachis to recastthe root evaluationtask as an eigenvalueproblem.Becausewe will be describingeigenvalueproblemsIaterin the book, we will merelyprovidean overviewhere. Supposewe havea polynomial ( 6 . 1| )
. / t . r 5+ o 2 x 1+ , r . , r t + a + x z* o 5 x+ a 6 : 0 yields Dividing by rrsand rearranging 5 I
:
-
(Il -f
I
-
(l I -l' (ll
dl
I
(14
.
At
(l\
(Irr
Ul
Al
(6.12)
A specialmatrix can be constructed by usingthe coefflcientsfiom the right-handsideas 's the first row and with I and 0's writtenfbr the otherrows as shown: -o2fu1
-u3fd1
10000 0t000 00r00 00010
-aafo1
-a5fu1
-aolar
(6.13)
matix. It has the usefulpropEquation(6. l3) is calledthe polynomial'sr:ompanion are the rootsof the polynomial.Thus, the algorithmunderlying erty that its eigenvalues the roots functionconsistsof merely settingup the companionmatrix and then using MATLAB's powerful eigenvalueevaluationfunction to determinethe roots. Its applicafunctions,aredescribedin the tion, alongwith someotherrelatedpolynomialmanipulation following example.
r56
ROOTS: OPENMETHODS
6.(
We shouldnote that roots has an inversefunctionc-alledpo1y, which whenpassed the valuesof the roots.will returnthe polynornial'scoefficients. Its syntaxis
cie
''
''
l'1Y(r)
Be )'>
where r is a column vector containingthe roots and c is a row vectorcontainingthepoly. nonliai'scclefficients.
wit fbu
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 n d D e t e r m i n eT h e i r R o o t s
X
;
Problem Siotement. Use the following equationto explorehow MATLAB canbeemployedto ntanipulate polynomials:
2. 1. 1.
i I
(E6.7.1) r 1.2-5 / ! ( x ) : - r ' ' - 3 . 5 . r '*1 2 . ' / 5 x t 1 2 . 1 2 5 , 1 2- 3 . u 7 5 . + Note that this polynomialhasthreereal rcots:0.5. - 1.0,and 2: and one pair ol complex r o o t s :I + 0 . 5 1 .
E X A M P L E6 . 2 '
r I i '
We
Solution. Polynomialsare enteredinto MATLAI) by storingthe coefflcientsas a row \/ector.Ftir example,enteringthe following line storesthe coefficientsin the vectora: -'' a = f I 3.5 2.15 2.'l'2,\ i.875 1.251;
We
We can then proceedto manipulatethe polynomial.For exarnplewe can evaluate it at .r : I, by ryping
X:
:>
(a, I )
p c )l y v a l
, w i t ht h er e s u l tl ,( l ) 5 - 3 . 5 ( l ) 1+ 2 . 1 5 ( t t + r 2 . 1 2 5 i l ) -2 3 . 8 7 5 0 + ) 1 . 2 -:5 - 0 . 2 5 : t).:tb0o
Fin
We ciin createa quadraticpolyn [q,r]
= deconv(a,b)
with the resultbeinga quotient(a third-orderpolynontial.,1)and a rerrrainder(r) cl-
1.0000 0
-4.000r1 0
Bqe man of l' rang
calle calcr
157
6.6 CASESTUDY
Becausethe polynomial is a perfect divisor, the remainderpolynomial has zero coefficients.Now, therootsof the quotientpolynomialcan be determinedas >>
X
-
rootS(q)
with the expectedresultthat the remainingrootsof the original polynomialEq. (E6.7.I ) are tbund: 2.0000 r.0000 + 0.5000i r.0000 0.5000i We can now multiply c1by b to colrL-up with the originalpolynomial: ->
if
-
conv
(q,
b)
,l:
1 . f .0r t )( l
-3.5011 0
?,.15A()
2.i2')A
-1.8'l!.0
1.2500
We can thendetermineall thc rootsof the originalpolynomialby .:
x
-
I-.Jal,S(.1)
_\: . 2.0000 r. rJ000 1.000t) + 0.5000i 0-!0t)(li t.00rlrr 0.5000 Finally, we can return to thc original polynomialagainby usingthe poLy function: _::
J
-
l).Jr-\1(X)
-l.500tr
2,.1,,A0
2.1),\O
3.87!0
1.2500
P IP E F R IC T ION Determiningfluid flow throughpipes and tubeshas great relevancein Bockground. many areasofengineeringand science.ln engineering,typical applicationsincludethe flow of liquids and gasesthroughpipelinesandcooling systems.Scientistsareinterestedin topics througha plant'svascularsystem. rangingfrom flow in bloodvesselsto nutrienttransmission The resistanceto flow in such conduitsis parameterizedbya dimensionlessnumber called the.lil t:tionJ'actor For turbulenttlow, the Colebrookequ.otionprovidesa meansto calculatethe friction factor:
o: +* 2orog (rr. U1#)
(6.14)
t58
ROOTS: OPENMETHODS
continued where e : the roughness(m), D : diameter(m), and Re = the Reynoldsnumber: oVD Re - :__ LL
where p : the fluid's density {kg/m3t, V: its velocity (m/s), and & : dynamic viscosiry (N.sim2). In addition to appearingin Eq. (6.l4), the Reynoldsnumber also servesasthe criterion for whetherflow is turbulent(Re > 4000). In this casestudy,we will illustratehow the numericalmethodscoveredin this partof the book can be employedto determine/for air flow througha smooth,thin tube.Forthis case,theparametersarep - 1.23kglm3,! : 1.79x I 0r N . slmz,D : 0.005m, V = 40nr/s and e :0.0015 mm. Note that friction factorsrangefrom about0.008 to 0.08. In addition, an explicit formulationcalledthe Swamee-Juin equationprovidesan approximateestimate:
1.325
f-
(6.1t
['"(t;.#)]'
1
The Reynoldsnumbercan be computedas
Solution.
o VD 1.23(40)0.005 : 13'743 R e- : - - : r . z st l o This value along with the other parameterscan be substitutedinto Eq. (6. l4) to give
Er gc 0.
r /0.0000015 2.st \ g(J'l: -+2.0loel=-+=" \ 3 . 7 ( 0 . 0 0 s ) 1 3 . 7 4 3 Jlf ) J.f Before detennining the root, it is advisableto plot the function to estimateinitial guessesand to anticipatepossibledifficulties.This can be done easily with MATLAB: Ilt,, liI .t :
l.
lJ;'l
I. I
1(1,"r;l)
1 l . i ) ( lr l t i - , , ] { ); ,
().f)0It)/'I(){)0;
I l l t , * \ , t * l ) , / [ l L t; I(I)
f lrl,rl
1
r t L i t 1 .( I )
(rr, f {).t)0:r
t l*ilLl
( i , : , - , * : : , , 1 r (t i ) ) ) ; ill (,.:i | 1,.,'"1.t t,..'tti i , i , > : t , r l r , ' i ( ' 1 ' ) , _ vl , r f r r i ( ' r r ( i ) )
0.1)il ,),rrr
As in Fig. 6.9, the root is locatedat about0.03. Becausewe are suppliedinitial guesses(x/ : 0.008 andx, : 0.08),eitherof thebrack eting methodsfrom Chap. 5 could be used.For example,the bisect function developed in Fig. 5.7 gives a value of/: 0.0289678with a percentrelativeerror of error of 5.926x l0{ in 22 iterations.False position yields a result of similar precision in 26 iterations. Thus, althoughthey producethe corect result,they are somewhatinefficient.This would not be importantfor a singleapplication,but could becomeprohibitiveif many evaluations were made.
Hr di
Ar gu fo
r59
6.6 CASESTUDY
c o n tn i ued
ty ne
2
\2
of tis t/s tn, [e:
1
i\
i
i'\-iiii
- \ i\-\
i
1
i
r
- - - - - - L - - - - - - - - - - - - - - - - - - l
H.
-2
5)
i
_?L!
" 0 . 0 1 0.02 0.03 0.04 0.05 0.06 0.07 0.08 f
FIGURE 6.9
We could try to attain improvedperformanceby turning to an open method.Because Eq.(6.14) is relatively straightfbrwardto differentiate,the Newton-Raphsonmethod is a good candidate.For example,using an initial guessat the lower end of the range (ro : quickly: 0.008),the newtraph functiondevelopedin Fig. 6.7 converges
)(, r.1., l(
ir.ill-1
. '
'
rr
r . , , 1r , L i r l l ( ( i ,
), t{I
j
':,:irlt i ll
( l i r i rl
il
I
l.l r
'liril ,fi l iilt,
ilir l
: rl
However,when the initial guessis setat the upperend of the range(xn: 0.08),the routine diverges, ,,,t
:i'
t i.Llir,
As can be seenby inspectingFig. 6.9, this occursbecausethe function'sslopeat the initial guesscausesthe first iterationto jump to a negativevalue. Further runs demonstratethat for this case,convergenceonly occurswhen the initial guessis below about0.066.
t60
ROOTS: OPENMETHODS
' continued So we can see that although the Newton-Raphson is very efficient, it requires good initial guesses.For the Colebrook equation, a good strategy might be to employ the Swamee-Jainequation(Eq. 6.15) to providethe initial guessas in '
J l l , i - l . l . l ' , ' l 6 1 1( t : r /{ 1 . / " D ) + 5 . 7 4 . / R c ' 0 . 9 ) ^ 2
I :l,I 1 j. i J : . ) , .1r it l, l ': .rr / l L . l b l , , , ' Li I i
II
i . l , , 1 , : a t r . - r p )1- ,i 1 , c 1 r 1 , l ! i . l )
r ). t ) . t f l ' l r{ fi j I 0 1 ' ' I 4 4 i t . r rI I I l j l ) . .1i . r r , t( r) 0 r , l ) ! f )I { ) ir ,., , Aside from our homemade functions, we can also use MATLAB's built-in f zero function. However, just as with the Newton-Raphson method, divergence also occurs when f zero function is used with a single guess. However, in this case, guessesat the lower end of the range cause problems. For example, .
I / ( r I ( r ( r . 1 (, 1 . ( J l l l - l )
l : : ,j 1 i l j ( . J L ' t t : t r , :
. r i r o r l i i r ( 1 r ; r r , r i( l f L l r l l
, i r , r r t r J , . 'r r c , c i rr l s { r
i omJtl r:r
. r n i n l . t , f ' . / ' ( i lr : o r r l , a i n i l q , , r r . t u- r :i r r q
( l , ' L r r ,l r , ) r r \ . / LI rr l , , , r t I 'lilll
'
,
. r : .) 0 0 t l l i \
si.gn
J L i n c' l i o n , , r . 1 i l r r - , r . r r c o i . r n L c r e d
t.,.,.
:rccl r (j1r.
i :;
4 . 9 2 A ' . )B ? , 1 ) .: 14 ; l I - i . ) r , , ' r t - . h . r r l I l . 1 | r c t r l , s l a r . 1 -L r L c ' ti , ' , . L . L u e .
f
,:
!
i!, r irj
If the iterationsare displayedusing optimset (recall Exanple 6.6), it is revealedthata negativevalue occursduring the searchphasebeforea sign changeis detectedand theroutine aborts.However,for singleinitial guessesaboveabout0.016,the routine worksnicely, For example,for the guessof 0.08 that causedproblemsfor Newton-Raphson,f zero does just fine: '
,:rIl!t
I
I
t,
.(
-
'
.
f ) - ( i : )i ' r ) t ) / ' . ' I 0 I
',1 ,1 i 4
As a final note,let's seewhetherconvergenceis possiblefor simple fixed-point iteration, The easiestand most straightforwardversioninvolves solving for the first./in Eq. (6.14): f...
-
I
t
0.25
('"'(#.*#))'
(6.16)
r f
o n u tl b
6.6 CASESTUDY
l6l
!t= s6)
0.01
0.04
0.08 "(
FIGURI6.IO
The two-curvedisplayof this functiondepictedindicatesa surprisingresult(Fig. 6. 10). Recallthat fixed-pointiterationconvergeswhen the)2 curve hasa relativelyflat slope(i.e., lg'({)l < l). As indicatedby Fig. 6.10, the fact that the y, curve is quite flat in rhe range from/: 0.008 to 0.08 meansthat not only doesfixed-pointiterationconverge,but it convergesfairly rapidly! In fact, for initial guessesanywherebetween0.008 and 0.08, fixedpoint iteration yields predictionswith percentrelative errors less than 0.0087oin six or fewer iterations!Thus, this simple approachthat requiresonly one guessand no derivative estimatesperformsreally well for this particularcase. The take-homemessagefiom this casestudy is that even great,professionallydeveloped software like MMLAB is not always foolproof. Further,there is usually no single methodthat works bestfbr all problems.Sophisticatedusersunderstandthe strengthsand weaknesses of the availablenumericaltechniques.In addition,they understandenoughof the underlying theory so that they can effectively deal with situationswhere a method breaksdown.
ROOTS: OPENMETHODS
162
PROBTEMS 6.1 Enrployfixed-pointitcrationto locatethc root ot . l ( - r ): : s i n ( v 4 ) - . ^ Usc an initial-{ucssof -rir: 0.-5lncl iterateuntil t',, < 0.Ola/a. 6.2 Use (a) fixed-point itefltion antl (b) tlrc Nc'ur'lonRaphsonmcthod to cletcrminca t'clotoi ./ (.t) : -.rl + : -5.Pcrtirlntthe conrputation l.8r * 2.-5using-i'11 unlil e,, is lcssthane, :0.05%. Also checkyor-rll'inaliutswer. 6.3 Dctcrrnincthc hishcst rcal root ol ./ (.f) : 0.9-5.t:t5 . 9 , r r +1 0 . 9 . r - 6 : (a) Graphically. (b) LIsin-cthrr Ncwton-Raphsonmcthod (thrcc itcrations, 'r;:3'5)' (c) Using thc sccantnrclhod(thrcc itcrations,-r; I - 2..5 i l n d . f ;: 3 5 ) . (d) Using thc rnodificd sccant rncllrod (thrc'c itcnttiotrs, . t i : 3 . 5 ., J: 0 . 0 1 ) . (c) Dctcrnrinctll thc rootswith MATLAB. 6.4 Dctcrrninc the l()wcst positivc r(x)t ol' ./ (-r) : 8 s i n ( . t ) r' - I : (a) Craphically. (lt) Using the Nervton-Ruphson ttrctl.tod(thrcc itctirtiotts, . t i : 0 . 3) . (c) LIsing the sccantmcthod (thrce itcrations..r';.1 - 0.5 and,ti: 0.'1. (d) Using tlrc rnodilicd scculrt nlethorl (llvc itcllti()n\, , r ; : ( ) . 3 ., l : 0 . 0 1) . nlethodand (b) lhc nrocli6.5 Usc (a) the Ncwton-Raphsorr (3 : 0.05) to clctclnrirtc ticclsecantnrcthocl a r-ootol ./ (,r'): , t 5- 1 6 . 0 5 . r*ru u . 7 - 5 . r -r l t ) 2 . 0 1 7 5 . r* r l l 6 . 3 5 r + 3 1 . 6 u 7 5 usirrg an initial gucss ot' ,t : 0.-5t125ancl t, : 0.(\l(tr,. Explain-yourrcsults. 6.6 Dcvclt>pan M-filc lirr thc sccantnrcthod.Along with thc (wo initial gucsscs,passthc l'unctiorrils an argunrctrt. T c s ti t b y s o l v i n gP r o b . 6 . 3 . 6.7 Dcvelop an M-l'ilc tir thc modificclsccant nrcth()d. Along with the initial gucssand the perturbationfiirclion. passthL-I'Lrnction asrn argurncnt. Tcstit by solvingProb.6.3. Eq. (86.4.l) l() gct Eq. (86.21.2). 6.ll DiUcrcrrtiate 6.9 Enrploy the Ncwton-Rlphsonnlcthod to dctenninc-a r c a lr t x r tf o r . / ( . i ) : - I + 6 . r - , l . r l | 0 . 5 , t 1 . u s i n ga n r n i aud usegraphical tial gucssof (a).1.-5. and (b) 4.43.DiscLrss to cxplairtany pcculiariticsin y'our and analyticalr.ucthods rcstrlts. 6.10 Thc "di"iclcand avcragc"tlctlrocl,un old-timcInctho(l lbr approxirratingthc :iquarcroot ol'any positivcrrulttbcrzr, can be firrrlulatcclas .ti luf .t1 1
Provc that this firnnula is bascd on the Newton-Raphson algorithm. 6,ll (a) Apply the Ncwton-Raphsonrnethodto thet'unction .l(r): tanh(,r2- 9) to evaluatc its known real rootat ,r : -1.Usean initial guessof .t1;: 3.2 andtakea minimum of thrcc itcrations.(b) Did thc nrcthodexhibit convergence on(o its r:al root?Sketchthe plot with thc rcsultsfor each iterationlabclcd. - 0.284"rr polynomial / (,t) : 0.0074.ra + 6. 12 Thc 3.3-5.5.rr-12.183.r-1--5has a rcal ro ) ' q - - - ' .i 8 1 ; v O - 5 5 ; n t = 8 0 ; r . : = 1 \ ; z r ) - I 0 l ) ; (zt)+rnlc*(vO+m*!.1.,'c)*(I erp( :=Q(L) r: > | x n i n , f rn i ri , .:ra , i L c r I = g o 1 ctn i n ( z , O , i', )
l6
>-
)7 29 7B il
xmin
c/m*1,) )
nr*q,i.*L)
;
=
3.8317 lnii n 192.86r) 9
th ne
u;h ne xF SC
6.9356c
(l0'-r
N o t i c eh o w b e c a u s et h i s i s a m u x i m i z a t i o nw, e h a v ee n t e r e dt h c n e g a t i v eo f E q . ( 7 . 1 ) . C o n s e q u e n t l [ym , i n c o r r e s p o n ct losa m a x i m u mh e i g h to l ' 1 9 2 . 8 6 0 9 . You nraybe wonderingwhy we havestressed the reduccdfunctiorrevaluations of thc goldcn-sectionsL-ilrch. Of course,lor solvirrga single optirnization,thc speedsavings would bc negligible.However,there are two importantcontextswhere mininrizingthe numberof functioncvaluationscan be important.Theseare l.
searchalgorithmmay be a Many evaluations.Therearc caseswherethe golden-section part of a much largercalculation.In suchcases.it may be calledmany times.Therefbre, kecpingfunctiortevaluations to a minimumcoulclpay greatdividendsfor suchcases.
176
OPTIMIZATION
r -n-r io-r Ix, fx,e-, irer l.goJdrin lf ,x',xu,es,maxic,varargin) % goldrrLin: mj-nimizaLior-r golclen section search % : fx,fx,ea,iterj=goldmin(f,xl,xu,es,maxit,pI,p2,...) % uses grolden sectjon search to find t.he minimum of f % inpr'rg; t = name of functior1 Z oo r'l , xl lower and Jppe' oLesses (dlefault - C.0001?) % es = de:;ired relative error (default = 561 % maxit = naximum allowable iterations p1 ,p2, , .. = additionaL parameters used by f % 9:
? ? ? ?
^rrrrirrts.
x = locaLj.on of mrnrmum fx = minimum function value ea - approxirnaLe relative error = number of iterations iter
(?)
FI
if rr.rrgin.3,error('at least 3 input argumenLs required'),end if nargin > g = 9 . B1 ; v 0 - 5 5 r m= BO; c = 1 5 ; z A- L 0 a ; >>
z=@(t)
(20+n/c*(v0+m"g/c)x
\1-exp(
c/m"t))-m*g/c*t)
;
X=
3.8317 f-192.8609 As with f zero, optional parameterscan be specified using optimser. we can display calculation details: options
= ofitimset
( 'display'
,
'iter')
For example,
;
fnr i
Func-count 1 2 -l
4 5 5 1 B 9 OptimizaLion the
current
f (x)
X
3.05573 4.94427 r.88854 3 .81544 3.85836 3.83332 3 .83r52 3.83166 3.83159
rB9.159 187.19 I/I,87I 792.851 -r92 .851 192 . B6r 792 . B6r 192. B(r1 792.861
Procedure inirial go 1den qolden parabo 1i cpirrabolic perrabolic parabolic P.,rrabolic parabo 1i c
ter.mrnated: x
OPTIC)NS.ToLX of
satisfies 1.000000e
the
termrnation
criteria
usinq
004
3 .83,r1 Thus, after three iterations.the nrethodswitclresf'romgolden to parabolic,and alier eight iterations.the minimum is determinedto a toleranceof 0.0001.
7.3 MUTTIDIMENSIONAT OPTIMIZATION Aside from one-dimensionalfunctions, optimization also deals with multidimensional functions.Recallfrom Fig.7 .3a that our visual imageof a one-dimensionalsearchwas like a roller coaster.For two-dimensionalcases,the image becomesthat of mountainsand valleys(Fie.1.3b).As in the lbllowing example,MAILAB's graphiccapabilitiesprovide a handy meansto visualizesuchfunctions.
r80 E X A M P L E7 . 4
7.4
OPTIMIZATION V i s u o l i z i n gq T w o - D i m e n s i o n qFlu n c t i o n Problem Stqtemeni. Use MATLAB's graphicalcapabilitiesto display the following function and visually estimateits minimum in the range-2 < xr < 0 and 0 < .r2 < 3: . f ( x t ' x ) : 2 i x t - x z - ' l -2 x l + 2 x r r z + x i Solution. The fbllowing scriptgenerates contourand meshplotsof the function: x - l i n s p a c e ( 2 , C), 4 A ) ; i, = l i n s p a c e ( 0 , 3 , 4 0 ) ; t ] : ' Y l = m e s h q t r i d( x ' Y ) ; z = . 2 + x - '+{ 2 * x . ^ 2 + 2* x . * Y + Y .' ' 2 ; subploL i'7,2,7); c ! ; = c o n t o l l r ( X , Y , Z ' ) ; ( -l :. r b e l ( c s ) ; xlabel ('r-1' ) ;\'l;rbel (':>
A',
-i
1 5 6
-3
.i6 27
Next we will createanother3 x 3 matrix on a row basis.First createthreerow vectors: >> x _ 1B 6 gJ; >> Y = t 5 8 1l; >> z -- t4 B 2l;
Then we can combinetheseto tbrn.rthe matrix >>
B =
lx;
y;
z)
p
-5
B
4B
9 1 2
We can add [A] and [Bl together: >>C=A+B
c= 9 212 1
1l 14
l5 3 9
MATRIXALGEBRA OVERVIEW Further,we can subtract[B] from [C] to anive back at [A]: >>C=C-B
1 'l
5 4
6 2
-3
6
7
Becausetheir inner dimensionsare equal,[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 element-by-element basis by including a period with the multiplication operator as in >> A. *B
B -35
30 32 48
12
54 2 14
A2 x 3 matrix can be set up >> D =
t1 4 3;5
B 1l;
If [A] is multiplied times [D], an error message will occur >> A*D .'?! LTta' usrnq Inner matrix dimensions
must agree.
However, if we reverse the order of multiplication so that the inner dimensions match, matrix multiplication works >> D*A
ro
20 58
53
35 53
The matrix inverse can be computed with the inv function: >>
l\1
lT
-
=
fnv(Al
4.2462 -a.8462 0.8308
0.01s4 0.3846 4.3231
-0.2154 0.6L54 -0.4769
202
LINEARALGEBRAIC EAUATIONSAND MATRICES To testthat this is the conect result,the inversecan be multiplied by the originalmatrixto give the identityrratrix: >>
AXAI -
a rl:;
-0.0(r00 1.0000 -0.0000
1.0000 0.0i100 0.0000
0.0000 0.rr000 1.0000
The eye functioncan be usedto generate an identitymatrix: 22
f
= e)re(3)
I=
0 1
0 0t)
Finally,matricescan be augmentedsirnplyas in >> Aug
=
tA
Il
Lrrn
t4:
010
361
Note that the climensionsof a matrix can be determined bv tlre size firnction:
3
8.I .3 RepresenlingLineor Algebroic Equotionsin Motrix Form It shoulclbe clear that r.natricespror,'idea concisenotation tbr representingsimultaneous linearequations. For example,a 3 x 3 setof linearequations, d r t r r * a 1 2 . t 2- f 0 t 1 . V :
bl
a 2 2 \ 2 1 o 2 1 . r 3:
b.
* a 3 2 . Y 2I { 1 3 1 1 3:
fi,
t t 2 1 x 1* 4:rrr
(8.4)
can be expressecl as [A]{r} : {b}
(8.5)
EQUATIONS WITHMATLAB ALGEBRAIC 8.2 SOLVINGLINEAR
where [A] is the matrix of coefficients:
otl ,,,-l l oo.t , t a:2 .,:: I I a): arrJ
--
tdl
ttrl
Lrl-rr
{D} is the columnvectorof constants: lblr :
lb1 bz
bt)
and {,,r}is the columnvectorof unknowns: {r}r :
[.r1 x2 r:]
Recall the definition of matrix rnultiplicationtEq. (8.3)l to convinceyourselfthat Also, realizethat Eq. (8.5)is a valid matrixmultiplicaEqs.(8.4)and (8.5)areequivalent. tion becausethe numberof columnsn of the first matrix [A] is equalto the numberof rows n of the secondmatrix {-r}. This part of the book is devotedto solvingEq. (8.5)lbr {x}. A formal way to obtaina solution using matrix algebrais to multiply eachside of the equationby the inverseof [A] to yield
tAl
r t A l { r }: t A l 1 i . b }
Because[A] '[A] equalsthe identitymatrix,the equationbecomes
tr) : tAl
r{b}
(8.6)
Therefbre,theequationhasbeensolvedfbr {r}. This is anotherexampleof how theinverse playsa role in matrix algebrathatis similarto division.lt shouldbe notedthatthis is not a very efficient way to solve a systemof equations.Thus, other approachesare employedin numericalalgorithms.However,as discussedin Section11.1.2,the matrix inverseitself hasgreatvaluein the engineeringanalyses of suchsystems. It shouldbe notedthatsystemswith moreequations(rows)thanunknowns(columns), regressionwhere A typical exampleis least-squares m > n, are said to be overdetermined. systemswith less an equationwith n coefficientsis fitto m datapoints(x,.y).Conversely, A typical example of equationsthan unknowns,,??< n, are said to be nndertletermined. underdetermined svstemsis numericaloDtimization.
EQUATIONSWITH MATTAB 8.2 SOTVINGTINEARATGEBRAIC MATLAB providestwo directwaysto solvesystemsof linearalgebraicequations. The mosl efficientway is to employthe backslash, or "left-division,"operatorasin >>r=A\b
T h e s e c o n di s t o u s em a t r i xi n r e r s i o n : >>
x
=
inv(,q)*b
204
LINEARALGEBRAIC EQUATIONS AND MATRICES As statedat the end of Section8.1.3,the matrix inversesolutionis lessefficient thanusing the backslash.Both optionsare illustratedin the following example.
E X A M P L E8 . 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 mw i t h M A T L A B ProblemStotement. UseMATLAB to solvethebungeejumperproblemdescribedatthe beginningof this chapter.The parametersfor the problem are
Moss (kg)
Jumper
Spring Constont (N/m)
6A 7A BO
Top(1) M i d d i e( 2 ) Bottom(3)
Unstretched Cord Length (m)
50 100 50
2A 2A 2A
Solution. Substitutingtheseparametervaluesinto Eq. (8.1)gives
-i:B-,8] : [-l;: {;t}{::::;l I L o -so so-l[', J Izs+.s Start up MATLAB and enterthe coefficientmatrix and the righrhand-sidevector: >> K = t150
100 0; 100 150
50;0
50 501
v-
150 100 rJ >> mq -
-100 150 -50 [588.6;
0 50 50 586.7;
184.81
588.6000 586.7000 784.8000 Employing left division yields >> x -
K\m9
4r.2020 55.9170 71.5130 Alternatively, multiplying the inverse of the coeflicient matrix by the right-hand-sidevec. t o r g i v e s t h e s a m er e s u l t :
8.3 CASESTUDY =
>> x
inv(K)*mg
4r.2A2A 55.9170 7I.613A Becausethe jumpers were connectedby 20-m cords,their initial positionsrelative to the platform is >> xl
= t20;40;501;
Thus, their final positionscan be calculatedas >> 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.Becauseit is subjectto the weight of two jumpers,one might expectthe secondcord to be extendedIonger than the third. However,becauseit 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 i n d i v i d u ocl o snnecied by bungeecords. (o)Unsiretched ond (b)stretched
CURRENTS AND VOLTAGES IN CIRCUITS Bockground. Recallthat in Chap. 1 (Table 1.1),we summarizedsomemodelsand associatedconservationlaws that figure prominently in engineering.As in Fig. 8.7, each model representsa systemof interactingelements.Consequently,steady-state balancesderived liom the conservationlaws yield systemsof simultaneousequations.In many cases, such systemsare linear and hencecan be expressedin 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 consideredpositive in sign. The current rule is an applicationof the principle of conservationof charge(recallTable 1.1).
EQUATIONS AND MATRICES LINEARALGEBRAIC
206
*_.r--*_ il
( a ) C h e m i c ael n g i n e e r i n g
f,,
( b ) C i v i le n g i n e e r i n g
(a)
ffi,ryl M Circuit (c) Electricalengineering
R,
--+AA---]+
WI
i,j
(b)
(d) Mechanicalengineering
FIGURE8.8 FIGURE8.7
Srhamntir
Engineeringsyslemswhich, ot steodyslote,con be modeledwilh lineorolgebroic equolrons.
ronro> A(:,1)= t-0.0I;0.61 ; -0.441 -0.0100 0.6700 -0.4400 >> x1=det X1
0.5200 1.0000 0.3000
1.0000 1.9000 0.5000
(A) /D
=
-14.9000 For more than three equations,Cramer's rule becomesimpractical because,as the number of equationsincreases,the determinantsare time consumingto evaluateby hand (or by computer).Consequently,more efficient alternativesare used.Someof thesealternativesarebasedon the last noncomputersolutiontechniquecoveredin Section9.1.3-the elimination of unknowns.
9.1.3 Eliminotionof Unknowns The elimination of unknownsby combiningequationsis an algebraicapproachthat can be illustratedfor a setof two equations:
(e.2) (e.3)
a 1 1 x 1* a 1 2 X 2 : 1 1 1 aZtxtla22x2:fi2
The basic strategyis to multiply the equationsby constantsso that one of the unknowns will be eliminatedwhen the two equationsare combined.The result is a single equation that can be solvedfor the remainingunknown.This valuecan thenbe substitutedinto either of the original equationsto computethe other variable. For example,Eq. (9.2)might be multipliedby a21andEq. (9.3)by all to give aztTltxt I
a 2 1 A 1 2 X :2 A Z l b t
a 1 1 a 2 1 X y{ a 1 1 a 2 2 x 2:
attbz
(e.4) (9.5)
SubtractingEq. (9.4) from Eq. (9.5) will, therefore,eliminatethe x1 term from the equations to yield a t t a 2 2 x-2 a y l a l 2 x z: a t t b Z- a y b t which can be solvedfor a 1 1 b 2-
a21b1
ii ti
GAUSSELIMINATION
218
Equation(9.6)can thenbe substituted into Eq. (9.2),which can be solvedfbr a 2 2 b 1- u p b 2 d 1 1 4 2 2-
02yAp
Noticethat Eqs.(9.6)and (9.7)follow directlyh'om Cramer'srule:
th' anl lb,
,._t-_-t_
aybl - a12lt2
attl
Il l, , , a n l 1O t t
'l lo" u bt lrr.'
1,.3
_.1____J___i_I
lQtr tl I c.lrr
0|tt)2 -
A2jal?
d 1 1 b 2-
a21b1
A))
I
I
1
an J tltt
I
The eliminationof unknownscan be extendedto systemswith rnorethan two or three eqLrations. However, the numerouscalculationsthat are requiredfor larger systemsmake the methodextremelytediousto irnplementby hand.However,as describedin Section9,2, the techniquecan be formalizedand readily programrnedlbr the conlputer.
9.2
NAIVE GAUSSELIMINATION In Section9.1.3,the eliminationof unknownswas usedto solve a pair of simultaneous equations. The procedureconsistedof two steps(Fig.9.3): 1. 2.
one of the unknownstiom the equations, The equationswere manipulatedto elin.rinate had one equationwith one unknown. of this elimination step was that we The result into could be solved directly andthe l'esultback-substituted Consequently, this equation solve for the remaining unknown. one of the original equationsto
This basicapproachcan be extendedto largesetsofequationsby developinga systemand to back-substitute. Gausselimination atic schemeor algorithrnto eliminateunkr.rowns is the most basicof theseschemes. This sectionincludesthe systematictechniquestor forward eliminationandbacksubfor areideallysuited stitutionthatcompliseGausselimination.Althoughthesetechniques implementationon computers,some modifications will be required to obtain a reliable algorithrn.In particular,the computerprogram must avoid division by zero. The tbllowing method is called "naive" Gausselimination beciiuseit does not avoid this problem. Section 9.3 will deal with the additional featuresrequired for an et'fectivecomputer progranl. The approachis designedto solve a generalsetof n equations: d1111 * q s z x z l 4 r - t x :* ' " ayll
* ( r 2 X 2I d z r j r : * '
I ay,,x,,: $t
(9.84)
. ' I a 2 , r x , :, S .
(9.8r)
1a,.-r- :
(9.8c)
::
2r9
9.2 NAIVEGAUSSELIMINATION
drz an [r,'
dz2 azt
lt"
att
a32
Lo!
I
o' ['"
,rrl
o','
,' ""
o',,'
| {
,,,4 (a) Forward
elimination
4rr
I
i h: xt:
b"z/a'\z
(b) Back substitution
(b'r- a|34lftt'22
-t, : (b, - dr:.r:- aptzlfa,
FIGURE 9.3 Theh.azo phoses (olforword of Gouss eliminotion: eliminotion ond(b)bocksubstitution
As was the casewith the solutionof two equations,the techniquefor n equationsconsists of two phases:eliminationof unknownsand solutionthroughback substiturion. Forword Eliminotion of Unknowns. The first phaseis designedto reducethe set of equationsto an uppertriangularsystem(Fig. 9.3a).The initial stepwill be to elininate the first unknownr1 from the secondthroughthe nth equations.To do this, multiply Eq. (9.8a) by a21f all to give a 2 1 x y* \ o p * z alt
*Lanxz+ atl
..'+
9or,*, att
-
eht br
(e.e)
Qtt
Now this equationcan be subtractedfrom Eq. (9.8b) to give
(,,, - 9 u ,r).,,+ .. + (,,,,- #,,,,) r , - b,- !) !6, or tt'rrxz+ . .. I a'"r-r,,: 6, where thc prime indicatesthat the elementshavebeenchangedfrom their original values. The procedureis then repeatedfor the remainingequations.For instance,Eq. (9.8a) can be multiplied by a3llayl and the result subtractedfrom the third equation.Repeating the procedurefor the remainingequationsresultsin the lbllowing modified system: ( t 1 1 x 1* c t p l 2 l
n 1 - 3 - t*3 . . . *
a1ux,,: $,
a \ t x t * a \ . x 3 a " ' * r t l , , x ,:, 6 \
(9.10a) rq InA\
220
GAUSSELIMINATION
c t r , x z | a 1 ] , r *3. . . * c t . . , , x , r : 6 \
a',rrx,,:6',,
a ' r ' r . rl ox u 2rr,r*...r
(9.10c)
(9.10d)
For the foregoingsteps,Eq. (9.8a)is calledthe pivot equationandall is calledthe pivot elentenr.Note that the processof multiplying the first row by u21f tr11is equivalent to to dividing itby all and multiplying it by a21. Sometimesthe division operationis refened as nonnalization.We makethis distinctionbecausea zeropivot elementcan interferewith normalizationby causinga division by zero.We will returnto this importantissueafterwe completeour descriptionof naiveGausselimination. The next step is to eliminatex2 from Eq. (9.10c) through (9.10d). To do this,multiply Eq. (9.10r) by aj2la\, and subtractthe result from Eq. (9. lOc). Perform a simila elimination for the remainingequationsto yield { 7 1 1 . \l '-1a 7 l t , f , tI d l r r . r r
"'LA1nX,,:lt,
tr\ar2+ aztu * .. . I a'r,,x,: 11', ai{tr...la'i,,x,:['!
:: ai,l4 1 ... t al,,x,: b',' where the double prime indicatesthat the elementshave beenmodified twice. The procedurecan be continuedusing the remaining pivot equations.The finalmanipulationin the sequenceis to usethe (n - I )th equationto eliminatefhe x,,-1 termfrom the rth equation.At this point, the systemwill have beentransformedto an uppertriangular system: a t t x t I a n x z I r z 1 3 J *3 . . . I a 1 r r , r : f i 1 a'zzrz* c/ar4 + . . . I a'r,rr, : 6, - 6\ a'1.,4* ... * a'1,,r,,
(9.1 ld)
(9.11r) (9.llc)
: l o ) , i it;'* , , : h " t t Bock Substitution. Equation(9.11d)can now be solvedfor x,,: -ht \1 n
t)
(r-l) ann
into the (n - l )th equationto solve for ir,,- 1. Thepn This result can be back-substituted by thefollowcedure,which is repeatedto evaluatethe remaining:r's, can be represented ins fbrmula: il
6ri tt _ 'r
\/-
i:i+l
, r 1 1 - -l-)J" , --t.l
fori:n-l,n-2,
E}
22r
9.2 NAIVEGAUSS ETIMINATION E X A M P L9E. 3
Noive GoussEliminotion Problem Stqtement. Use Gausseliminationto solve 3xr- 0.lrz -0.2x2: 7x2-0.34: 0.lxr* 0.3x1 0.2x2+ l0;t: -
(Ee.3.l)
7.85 -19.3
rFq ? ?r
(E9.3.3)
71.1
Solution. The first part of the procedureis fbrward elimination.Multiply Eq. (E9.3.1) by 0.1l3 and subtractthe resultfrom Eq. (89.3.2)to give -19.5617
7.00333x:- 0.293333n :
Then multiply Eq. (89.3.1)by 0.313and subtractit from Eq. (89.3.3).After theseoperat i o n s .t h e s e to f e q u a t i o niss 0 . 2 x 3:
7.85
- - 19.5617 - 0.293333x: 7.00333x: - 0.190000x2 * 10.0200x:: 70.6150
rFq 1 4) (89.3.5) (E9.3.6)
To completethe lbrward elimination,rr rrIUStbe removedfiom Eq. (E9.3.6).To accomplish this, multiply Eq. (E9.3.5)by -0.19000011.00333and subtractthe result from Eq. (E9.3.6).This eliminatesr? from the third equationand reducesthe systemto an upper triangularform, as in (E9,3.7) 7.85 3xr0 .l x z 0 . 2 x 31.00333x2- 0.293333xt: -19.561'7
(E9.3.8)
10.0120-tj: 70.0843
(E9.3.9)
We can now solve these equationsby back substitution.First, Eq. (89.3.9)can be solvedfor
70'0843 : 7.oooo3 10.0120 into Eq. (E9.3.8).which can then be solvedfor This result can be back-substituted
- t9.5617+ 0.293333(7.00003) : -2.50000 7.00333 Finally,"r:: 7.00003andx2 : -2.50000 canbe substituted backinto Eq. (E9.3.7),which can be solvedfor
7.85+ 0.1(-2.50000) + 0.2(7.00003) : 3.00000 Although thereis a slight round-off eror, the resultsare very closeto the exact solutionof rr : 3, ^z: -2.5, &I1d;rj : 7. This can be verified by substitutingthe resultsinto the original equationset: -r(-r)- o. I (- 2.-5) 0.2(7.00003): 7.81999= 7.85 0 . 1 ( 3 )+ 7 ( - 2 . 5 ) - 0 . 3 ( 7 . 0 0 0 0 3: ) - 1 9 . 3 0 0 0 0: - 1 9 . 3 0 . 3 ( 3 )- 0 . 2 ( - 2 . 5 ) + l 0 ( 7 . 0 0 0 0 3 :) 1 1 . 4 0 0 3= 1 1 . 4
222
GAUSSELIMINATION
function x = caussNaive(A,b) % GaussNaive: naive Gauss eliminatiorr % x = GaussNaive(A,b): Gauss elimination ? input: % A=coefficientmaLrix % b = rigirr hand side vector % outpuL; ? x=solutionvector lm,nl = size (A) ; if m-=n, error('Matrix nb - n+1; Aug - [A b];
A must be square');
withouL
pivoting.
end
? forward elimination for k = 1:n-1 for i = k+1:n - Aug(i,k) /Auq(k,k) ; factor = Aug(i,k:nb) -factor*Aug(k,k:nb) Aug(i,k:nb) ; end end ? back subsr'rue ion x = zeros{n,l); x(n) - Aug(n,nb)./Auq(n,n) ; ior i = n-1:-i:1 - (Au.J(i,nb)-Aug(i, i+1:n) *x(i+1:n) ) iAug(i,i) x(i)
;
end
F I G U R E9 . 4 A n M J i l ei o r m p i e m ennot i v eG o u s se l i m i n a t i o n .
9.2.I
MATTAB M-file! GaussNaive
An M-file that implementsnaive Gausselimination is listed in Fig. 9.4. Noticethatthe coefficient matrix A and the right-hand-sidevector b are combinedin the augmented matrix Aug. Thus, the operationsare performedon Auq ratherthan separatelyon A andb. Two nestedloops provide a conciserepresentation of the tbrward eliminationstep. Al oLtterloop movesdown the matrix from one pivot row to the next. The inner loopmoves below the pivot row to each of the subsequentrows where elimination is to takeplam. Finally, the actual elimination is representedby a single line that takes advantage of MATLAB's ability to perform matrix operations. The back-substitutionstep follows directly from Eqs. (9.12) and (9.13).Again, MATLAB's ability to perform matrix operationsallows Eq. (9.13) to be programmed asa sinsleline.
9.2.2 Operotion Counting The executiontine of Gausseliminationdependson the amountof .floating-point operatilv
9.2 NAIVE GAUSS ELIMINATION
223
Therefore,totalingup theseoperationsprovidesinsightinto which partsofthe algorithmare most tirneconsumingand how computationtime increasesas the systemgetslarger. Before analyzingnaive Causselirnination,we will first define somequantitiesthat facilitateoperationcounting: il1
il1
111
\-, r,;' -, \- r'ir /--' /-'"' ;
I
i
I
tn
;-t
i-l
\L - r
: I * I + | +...11:nt
\-;
:
L'
In
\ - r , ; r + o r ir -- \ - r , ; r r \ -, 'o, r i r //_.t
| +2 +3 +... +t1t
nr_ k+l
It: t n ( n r*
l)
(9.| 1a,b)
/_6", i-l
:
nr) I
(9.14cd)
(9.14e)
O(nt)
\-;r : l.+ 2,+3-+...+ ,rz-m(ml1)-(2mll) :!*o@2) lJ
(:e.t1.f )
6-i
where O(rl") means"terms of order tn" and lower." Now let us examinethe naiveGausseliminationalgorithm(Fig.9.4) in detail.We will first count the flops in the eliminationstage.On the first passthroughthe outer loop, k : I . Therefbre,the limits on the inner loop are from i: 2ro n. Accordingto Eq. (9.14d),this meansthat the numberof iterationsof the innerloop will be f/ - - t : t t _
2+l:n-
(9.I s)
I
i:2
For every one ofthese iterations,thereis one division to calculatethe factor.The next line then performs a multiplication and a subtractiontbr each column element from 2 to nb. Becausenb : n f 1, going fiom 2 to izb resultsin r multiplicationsand n subtractions. Togetherwith the singledivision,this amountsto fl + I multiplications/divisions and n pass iteration inner loop. The for the first addition/subtractionstor every of the total (n l) multiplication/divisions l)(n) throughthe outerloop is thereforeQt l)(n 1 and addition/subtractions. iterationsof the Similarreasoningcan be usedto estimatethe flopsfor the subsequent outer loop.Thesecan be summarizedas
'"r,t,tu."n";;;;;,.ion Outer Loop k
tr,r,",tolp
aatiri""Tsritr".tt"r
i
Flops
I 2
2,n 3, rt
{rr- I j(r) ln-2)ln-t)
Flops
{,r-llfu+1) l,'- 2)l,l
: k
rr- l
kll
n
ln
k ) l n+ I - k )
( r) ( 2 )
n, n
(ir
k)(n+2-k)
( r) { 3 )
flops for eliminationcan be computedas Therefbre,the total addition/subtraction ir
rr I
I
k ' t ( n +t - f r ) : f
k ( 2 n *t ) + / . 2 1
(9.16)
tl li
224
GAUSSELIMINATION
n ( nt t , i t - Q t *t t )f r + f t ' A:t k:t t-l Applying someof the relationshipsfrom Eq. (9.14) yields
l n 3+ o ( , ) ) - [ n 3+ o r n z t ] *[ ] , , t + o r n z ; l : | * o r r , J L.''
J
A similar analysislbr the multiplication/divisionflops yields
[ n 3+ o l f ) ] -
[ n 3+ o ( n ) ]+ [ ] , , t * o r r ' ; ] : | * o r r t , t)tl L_-
)
(9.r9)
'
Summingtheseresultsgives ln' ^ J
(9.201
lO(tt')
Thus, the total number of flops is equal to 2nt 13 plus an irdditionalcomponent proportional to terms of orclernr and lower.The result is written in this way becauseasn gets large,the O (n?) andlower termsbeconrenegligible.We are thereforejustifieclin concluding that for largen, the effort involved in forward eliminationconvergeson 2n3f 3. Becauseonly a singleloop is used,back substitutionis much simplerto evaluate. The numberof addition/subtraction flops is equalto n(n - l) 12. Becauseof the extradivision prior to the loop, the numberof multiplication/division flops is n(n -l l)12. Thesecanbe added to arrive at a total of n2+ o1ny
(9.2\
EXA
Thus, the total efTortin naive Gausseliminationcan be representedas
oUr',+ n2+o(i,) -ll-I1r\ T * Forward elimination
2!
* oCl
(9.221
Back substitution
Two usefulgeneralconclusionscanbe drawn ftom this analysis: l.
As the systemgets larger,the computationtime increasesgreatly.As in Table 9.1,the amountof flops increasesnearly three ordersof magnitudefor every order of magnitudeincreasein the numberof eouations.
T A B T E9 . 1
N u m b e ro f f l o p sf o r n o i v eG o u s se l i m i n o t i o n .
n
Eliminqfion
t0 r00 r000
705 6 7 15 5 0 667xlaE
Bock Substituiion
r00 r0000 I x lOo
Tolol Flops
805 6 8I 5 5 0 668 x IOB
2tt313
667 666667 6 . 6 7x I A B
Percenl Due to Eliminction
87.58% 9853% 99 85%
9.3 PIVOTING 2.
9.3
225
Most of the effort is incurredin the eliminationstep.Thus, effortsto make the method n.roreefficient shouldprobably focus on this step.
PTVOT|NG The primary reasonthat the foregoing techniqueis called "naive" is that during both the elimination and the back-substitutionphases,it is possiblethat a division by zero can occur.For examole.if we usenaive Gausseliminationto solve 2 - r 21 1 . 1 1 8 4rr * 6-ru*7.rt - -3 2x1 - 3x2 -f 613 5 the normalizationof the first row would involve division by ay1:0. Problemsmay also arisewhen the pivot elementis close,ratherthan exactlyequal,to zerobecauseif the magnitude of the pivot elementis small comparedto the other elements,then round-off errors can be introduced. to determinethe coefficient Therefore,beforeeachrow is nomralized,it is advantageous with the largestabsolutevalue in the column below the pivot element.The rows can thenbe switchedso that the largestelementis the pivot element.This is calledpartial pivoting. If columnsas well as rows are searchedfor the largestelementand then switched,the procedureis called contpletepivoting. Cornpletepivoting is rarely usedbecauseswitching columns changesthe order of the .r-'sand, consequently,adds signilicant and usually unjustified complexity to the computerprogram. The following example illustrates the advantagesof partial pivoting. Aside from avoiding division by zero,pivoting also minimizesround-off eror. As such,it also serves as a partial remedy fbr ill-conditioning.
9.4 EXAMPLE
Poriiol Pivoting ,
ProblemStotement. Use Gausseliminationto solve 0.0003xr+ 3.0000x2:2.0001 : 1.0000 1.0000xr* 1.0000.12
, ; r
Note that in this fom.rthe first pivot element,arr : 0.0003,is very closeto zero.Then repeat the computation,but partial pivot by reversingtl'reorder of the equations.The exact solutionis 'r1 : I /3 andxz : 213. Solution. Multiplyingthe first equationby 1/(0.0003)yields - r - fr 1 0 , 0 0 0 x 2 : 6 6 6 1
'
which can be usedto eliminater1 from the secondequation: -9999x2:
-6666
which can be solved fbr:r2 : 213.This result can be substitutedback into the first equation to evaluate"rr| : ^t-
2.0001- 3(2/3) 0.0003
(E9.4. r)
226
GAUSSELIMINATION Due to subtractivecancellation,the result is very sensitiveto the number of significant f i g u r e sc a n i e di n t h ec o m p u t a t i o n :
Significonr Figures 3 4 5 6 7
.rt
x2
0 667 0.6667 4 66667 0 666667 0.6666667
I
11
0 0000 0 30000 0 330000 0 3330000
AbsoluteVolue of PercenlRelotive Error for x,
r099 r00 t0 I 0t
Note how the solutionfor.r, is highly dependenton the numberof significantfigures.Thn is becausein Eq. (E9.4.1), we are subtractingtwo almost-equalnumbers. On the other hand,if the equationsare solvedin reverseorder,the row with thelarger pivot elementis norrnalized.The equationsare 1.0000xr* 1.0000x2: 1.0000 0.0003xr* 3.0000-tr:2.0001 Elimination and substitutionagain yields x3 :213. For different numbersof significant figures,rr can be computedfrorn the first equation,as in
| - (213) I This caseis much lesssensitiveto the numberof significantfiguresin the computation:
Significont Figures
3 4 5 6 7
xl
x2
4 667 4 6667 0.66667 4 666667 0.6666667
0 333 0 33-?-? 0 33333 0 333333 0 3333333
AbsoluteVolue of PercenlRelotive Error for x,
0l 00r 0 00t 0 000r 0.0000
Thus, a pivot strategyis rnuch rnore satisfactory.
9.3.1 MATLAB M'file3 GaussPivot An M-file that implementsGausselimination with partial pivoting is listed in Fig.9.5.It is identicalto the M-file for naive Gausselirninationpresentedprel'iouslyin Section9.2.1 with the exceptionof the bold porlion that implementspartial pivoting. Notice how the built-in MATLAB function max is usedto determinethe largestavailable 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:n-l eo partiaL pivoting t b i g , i l = m a x ( a b E( A u g ( k : n , k ) ) ) ; ipr=i+k-1; if ipr-=k AUS([k,ipr],: )=Aug( [ipr,k],: ); end for i = k+1:n factor=Augr (i, k) /Auq (k, k) ; Aug ( i, k : nb) =Aug( i, k : nb) - factor*Aug (k, k : nb) ; end ? back substitution x=zeroS(n,1); x ( n ) - 4 L 1 g( n , n b ) / A u g ( n , n ) ; for i = n-1:-1:1 x ( i ) = ( A u g ( i , n b ) - A u g r( i , i + 1 : n ) * x ( i + 1 : n ) ) / A u g ( i , i ) ; end
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 own i i h p o r i i o lp i v o t i n g
9.4 TRIDIAGONATSYSTEMS Certain matriceshave a particularstructurethat can be exploitedto developefficient solution schemes.For example,a bandedmatrix is a squarematrix that has all elementsequal to zero, with the exceptionof a bandcenteredon the main diagonal. A tridiagonal systemhas a bandwidthof 3 and can be expressedgenerallyas
fi8r e2 .fz 8z e3 ft
r1
rl
.{1
r.
-
41
(9.23)
€tt I
.f,,-l
8n-t
Xn-l
' n- |
GAUSSELIMINATION
Notice that we have changedour notationfor the coefficientsfrom a's and b's to e's,/s, ma,g's,and r's. This was doneto avoid storinglargenumbersof useiesszerosin thesquare modificationis advantageous becausethe resultingalgonthn trix of a's. This space-saving requireslesscomputerrnemory. An algorithmto solvesuchsystemscanbe directly patternedafterGausseliminationthat is, using forward elimination and back substitution.Howeveq becausemostof thc matrix elementsare already zero, much less effort is expendedthan for a full matrix.This efficiency is illustratedin the following exarnple. E X A M P L E9 . 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.Solve the following tridiagonalsystem: Problem
-, ll;tt_lTfl ':i -:l t:tI t,3;1-l
-l f2.04 2 .04 l-r
Solution. As with Gausselimination,the tirst step involves transformingthematrixh upper triangular form. This is done by multiplying the first equationby thef actore2lf ynl subtractingthe resultfrom the secondequation.This createsa zero in placeof e2andtransforms the other coefficientsto new values. f z : . f zt'j:
?a
T+t
:2.04 -
t ' )- ? , ' , : 0 . 8 f,
-l
1) : I '550 2JJ4e I t 4 0 . 8 t: 2 0 , 8 1.04
Notice that g, is unmodifiedbecausethe elementaboveit in the first row is zero. istranr After performinga similal calculationlbr the third andfourth rows,the system formed to the upper triangularform
,.lot-:os lI;i I:I - i33I [204 ,.i,Jl;;I I ift',';f I Now back substitutioncan be appliedto generatethe linal solution:
,o: 'io: '+#: t'3 - 83r-l
,r:
fa ,'z - 82x3
,fz rl ^l
-
8lJ2
.ft
r5e48o
14.22r- (-l)159.480 :124.538 1.395 - (- l) 124.538 20.800 : 93.718 1.550 40.800- (- l)93.778 :65.910 2.040
9.5 CASESTUDY
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
-
elimination ).r
= e(k) /f (k-1-); factor - factor*g{k-1); = f{k) f(k) - factor*r(k-1); = r(k) r(k) end % back substirurion (n) ; x(n) = r(n)/f for k = n-1:-1:1 x(k) = (r(k)-s(k)*x(k+1) )/f
(k) ;
end
FIGURE 9.6 AnMJileto soveo tridiogonol sysiern.
9.4.1
MATLAB M-file! rridiag
An M-file that solvesa tridiagonalsystemof equationsis listed in Fig. 9.6. Note that the algorithmdoesnot includepartialpivoting. Although pivoting is sometimesrequired,most tridiagonalsystemsroutinely solvedin engineeringand sciencedo not requirepivoting. Recall that the computationaleffort for Gauss elimination was proportional to n3. Becauseof its sparseness, the effort involved in solving tridiagonalsystemsis proportional to n. Consequently,the algorithm in Fig. 9.6 executesmuch, 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 steady-statecase,a differential equation basedon heat conservation can be written for such a system as d2T
_ +h'(7.- z):0
ax-
(9.24)
230
GAUSSELIMINATION
continued 7,,: 20
I
fo: 40
ti{it
-- Lx;'7,,:20
Is = 200
{ ,
,r=0
FIGURE9.7 twowollsof constont temperoture rodpositioned between butdifferent A noninsuloted uniform The {inite-d'flerence representolion employsf6ur inls1ie,66is5.
where I: temperature("C), x : distancealong the rod (m), h' : aheat transfercoefficient betweenthe rod and the surroundingair 1m-2), ztrdTo: the air temperature("C). 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.4523e-0 +
(9.2'
In Although it provided a solutionhere,calculusdoesnot work for all suchproblems. such instances,numerical methods provide a valuable alternative. In this case study,we will use finite differencesto transformthis differentialequationinto a tridiagonalsystem of linear algebraic equationswhich can be readily solved using the numerical methodsdescribed in this chapter. by Solution. Equation(9.24) canbe transformedinto a setof linear algebraicequations conceptualizingthe rod as consistingof a seriesof nodes.For example,the rod in Fig.9.7 is divided into six equispacednodes.Sincethe rod has a length of 10, the spacingbetween nodesis A,x :2. Calculus was necessaryto solve Eq. (9.20 becauseit includes a secondderivative. As we learnedin Sec.4.3.4,finite-differenceapproximationsprovide a meansto transfom derivatives into algebraic form. For example, the second derivative at each node canh approximated as dzT
Ti+t -ZTi *Ti-r
d-t
Lx2
where I designatesthe temperatureat node i. This approximation can be substitutedinto Eq. (9.2q to give Ti+r-2Tr * Ti*t n, -2
+h'(7"-4):0
9.5 CASESTUDY
continued Collecting terms and substitutingthe parametersgives -Ti*r*2.04Ti-
(e.26)
4+r:0.8
Thus, Eq. (9.24)has beentransformedfrom a differentialequationinto an algebraicequation. Equation(9.26) can now be appliedto eachof the interior nodes:
-To*2.04T1-4:0.8 - Tt * 2.0472- Z: : 0.8 - Tz 12.04\ - Z+: 0.8 - Tt -t 2.0474- Is : 0.8
(9.27)
The values of the fixed end temperatures,Io : 40 and Ts:200, can be substitutedand moved to the right-handside.The resultsarefour equationswith four unknownsexpressed in matrix form as
a; ffi' 'it;]{i^l:{}i}
(9.28)
So our original differentialequationhas been convertedinto an equivalentsystemof lineal algebraicequations.Consequently,we can usethe techniquesdescribedin this chapter to solve for the temperatures.For example, using MATLAB >> 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= 93.',i18a
65.9698
'r24.5382
r59.41 9a
A plot can also be developed comparing these results with the analytical solution obtained with Eq. (9.25), .
L
j'
i
-
t;
>> x=[0:2:101; >> xanal=f0:101; >> TT-0 (r) 13.452 -j*exp (0.1*xanal) (-0.1*xanal)+20; t-,r.rj=1:(Ydl--); >> plot it ,'l , 'o' , ranal,
-53.4523*exp
. . .
Tartal )
As in Fig. 9.8, the numericalresultsare quite closeto thoseobtainedwith calculus.
232
GAUSSELIMINATION
confinued
9.1 | the r (Fig.
Analytical (linel and numerical (pointsl solutions
220
9.2 | 200
4
180
Chec equa
160
9.3 ( 140 r-. 120
(a) S
100
rl (b) c
80
fi
(c) c 9.4 (
60
10
(a) C
FIGURE9.8 versus rod.Boihonolyticol ondnumericol A plotof temperoture distonce olongo heoted {line) lpoints)solutionsore disployed.
(b) u
(c) U tt
(d) s In addition to being a linear system,notice that Eq. (9.28) is also tridiagonal.Wecan use an efficient solutionschemelike the M-file in Fis. 9.6 to obtain the solution: >> e=10 -1 -1 -11; >> f=12.04 2.04 2.04 2.04); >> g=i-1 -1 -1 01. >> r= [40.8 0.8 0.8 200.8] ; >> TridiaS (e, f, q, r )
65.9598
93.7185
r2,4.5382
tc 9.5 C
c
l.(
r59 .4195
The system is tridiagonal because each node depends only on its adjacent Becausewe numberedthe nodes sequentially,the resulting equationsare tridiagonal. casesoften occur when solving differentialequationsbasedon conservationlaws.
(a) So (b) Co (c) On reg (d) Sol (e) Sol Intr
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
9.6). Use thegraphical methodto solve 8 . r t : 2 4 4.rt - { *r 6 , r : :
(b) Substituteyour results into the original equations to check your answL.t's. 9.7 Given the cquations 2r1-6x2-
-r-r:-38
-3rr-;r:*7.r.t:-34
31
your resultsby substitutingrhem back into the
*8rr +
- r 2 - 2 x 1: - 2 0
(a) Solvebv Gaussc.limination with partialpivoting.Show all stepsol the computation. - l . l r 1* (b) Sr"rbstitutc your rcsults into the original equationsto l0-r2: l16 checkyour answers. - 2xt+ ll .4.b : 174 9,8 Pcrforn.rthc samccalculationsasin Example9.5, but lbr graphicallyand check your resultsby substituting the tridiagonal Solve system: them buclinto thc equati(rn\. -0.4 0nthebasisof thegraphicalsolution,what do yor.rexpect Ios l tll . r I -c).4 0.8 - 0.a regarding theconditionof thc system) | | | L1 Compute thedeterminant. {).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 concentrationof the reactor :2 5 . t -12 - r 2 fronr which thc flow originates(c, with unirsof milligrarns Compute thedeterninant. per cubic metcr).If the systcmis at a steadystate,the transUseCramer'srule to solve fbr thc r's. f'cr into each reactorwill balancethe transferout. Develop UseGauss eliminationwi(h partialpil,otingto solvetor r.niiss-balarrce equations for lhe reactors and solve the the-r's. threc simultaneous linear algebraic equations for their Substitute your resultsback into the original equations conccntratlons. tocheck yoursolut.ion. Given theequations Given thesystemof equations
:{f,
0 . 5 ; -t 1 . r 2 : 1 . 0 2 -t 2 1x2:
9.-5 -18.8
Qnct
Solve graphically. the deterrninant. {b)Compute (c)0n the basis of (a) and (b), what would you expect regarding the system'scondition? by the elinrinationof unknowr.rs. {d)Solve (t)Solveagain.but with a,, modified slightly to 0.52. your results. Interpret 9,6Giventheequatiolrs 1 0 - *t 12 - r 2 - , r 3 :
200 mg/s Qz(z
Qr.: 120 Qrr: 40 Q,z= 90 Qz::60 Qyt:3o
27
- 3.t1- 6-t2* 2.r3: -61.5 . r 1* . r 2 * 5 . r : : - 2 1 . 5 (l) Solve bv naiveGuusselirnination.Shorr,' all stepsof the computatton.
FIGUREP9.9 Threereociors linkedby pipes Theroteof mosstrcnsfer through eochpipeis equo to theproductof f ow Q cnd concenirotion c of thereccforfromwhichthef ow orgrnoles.
234
GAUSS ELIMINATION
9.10 A civil engincer involved in constructionrcquires the lbllowin-edifl'crentialequationrcsultsfiom a steady-$ate .1800,5800, and 5700 rnr of sand, line gravel, and coarse massbalancefbr a chemicalin a onc-dimensional clnal: gravel,respectivcly,lbr a building plojcct. Thcre arc thrcc d-t llt: 0:D --U ,--kc pits from which thesematerialscan be obtaincd.The cornd.\' d.r pits of thcse is 1-rosition r.'u'hcre c : conccntri.ition, / : tinlc, r : distance,D = diffusion coclllcicnt. U - f-luiclvelocity. and li = a first-order dccay late. Convert this ciitilrcntial ccluationto an equiva. Fine Grovel Sond Coorse Grovel lent systcmof simultaneous algcbraicequations. GivenD = : 311and c(10) : 20. strivethese 2, U : l, k -- 0.2, r'(0.1 P iIr 30 t5 55 equationsfi'om.r : 0 to l0 and dcvelopaplot of concentfa' ?it2 45 25 30 tion velsusdistance. Pir3 2a 25 55 9.13 A stagecxtractionprocessis depictedin Fig.P9.13. In sr-rchsystems,a streamcontainirtga weight tiactionr',,,ofa Hou, nran-vcubic melels rxlrst be hauleclfiorn u'achpit in chemical enters fronr the lcft at a mass f'low late of F,. Siniultaneously, a solvcntcarryinga rvcighttiactionx,nof orderto meetthe cnginecr'snceds? the same entersflom the right at a flow rate0fF,. chemical 9.ll Arr clectricalengineersupervise's theploductionof three Thus, lbr nrassbalancccan be reprcsentcd stagc i, a as typcs of clectrical componcnts.Three kinds of rr-raterial' ) t
rnctal.plastic,anclrubber-are rctpriledtirl production.The amountsneededto prodLrcceachcomponentare
Componenf
Metol (g/ component)
Plosric (9/ component)
t5 l7 t9
03c o4a
I
2 J
Rubber (9/ component)
o 2
0 5-5
(P9.l 3n) At cach stage.an equilibriumis assumcdto be established bctween,r', andr, as in ''i K : {P9.ll}) wheleK is calleda distribution Equation(P9.13b) coefficient. can be solvedfbr -r,and substitutcdinto Eq. (P9.13a)to yield F, \ /F, \ rPg.ljir | (;=K Ir, ':g r- t' r a /) r ' , \ r t / lf Fr : 500 kg/h, .\'in: 0. 1, F.: 1000kg/h, rin = 0, and K : 4, delerminethe valuesof _r',,u, anclx.,,,,if a tlve-stage rcactoris uscd.Note that Eq. (P9.l3c) must be modified to accountfbr thc inflou, wei-shtliactions when appliedto the filst and laststages. r' ,-
If totalsof 3.89,0.09-5,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 algebraicequationscan arise in the solution of difl'erentialcquations.For example.
/ ('\
FIGUR
9.14 A viscous pipe se( mechan flows ir to obtai Q, I Qt) 3Qt
o,= Qt=
o.=
9 . 1 5A followi BC,AL
74k
P9.I3 FIGURE A stoqe extroclionprocess
> A -
l\
\/2
I/3;I
2/3 Ii2;L
3/4 3/51;
Then, the row-sum norm and conditionnumbercan be computedas > > n o r m (A , i n f )
2.3504 > > c o n d (A , r n f )
258
MATRIXINVERSE AND CONDITION These results con'espond to those that were calculated by hand in Example 11.3. (b) The condition numbers based on the Frobenius and spectral norms are >> cond (A, ' fro'
)
368.0866 >> cond (A)
366.3503
"'i
"i.;'j\';1
Il.
As the name implies, indoor air pollution deals with air contamination Bockground. in enclosed spacessuch as homes, offices, and work areas.Supposethat you are studying 'N Guzzle, a truck-stop restaurantlocatedadjacent the ventilation system for Bubba's Gas to an eight-lane freeway. As depicted in Fig. I L2, the restaurantserving areaconsistsof two rooms for smoken and kids and one elongatedroom. Room I and section 3 have sourcesof carbon monoxide from smokers and a faulty grill, respectively. In addition, rooms I and 2 gain carbon monoxide from air intakes that unfortunately arepositioned alongside the freeway.
F I G U R EI I . 2 Theone-woyorrowsrepresent volumetric oirflows, Overheodview of roomsin o restouront. whereosiheMo-woyorrowsrepresent diffusive mixing.Thesmoker ond grillloodsodd corbon monoxide mossfo thesystem butnegllgible oirflow. 0,.= 150m3/hri I Qt = 50 m3lhr
2 (Kids'section)
ct = 2 mglm3
Q":20Q m
Qa: 1oo
I 25 m3lht 4 c+ t %_l_-l El I oi I
3lhr 1 ( S m o k i n gs e c t i o n )
-3 cu = 2 mglm
Smoker load (1000mg/hr) )
>
25 m3/hr J
I G r i l ll o a d
CASESTUDY trli':'l);
. .
i
.l
. .
continued l
,
Write steady-statemass balancesfor each room and solve the resulting linear algebraic equationsfor the concentrationof carbonmonoxide in eachroom. In addition, generate the matrix inverse and use it to analyze how the various sourcesaffect the kids' room. For example,determinewhat percentof the calbon monoxidein the kids' sectionis due to (l) the smokers,(2) the grill, and (3) the intakevents.In addition,computethe improvementin the kids' sectionconcentrationif the carbonmonoxide load is decreasedby banning smoking and fixing the grill. Finally, analyzehow the concentrationin the kids' area would changeif a screenis constructedso that the mixing between areas2 and 4 is decreasedto 5 mj/hr. Steady-statemass balancescan be written for eachroom. For example.the Solution. balancefor the smoking section(room l) is QncoQocr *f13(ca-c1) (Load) *(lnflow) (Outflow)f (Mixing)
0:lVrmore.*
Similar balancescan be written for the other rooms: 0 : Qt,ct,+ (Q' - Qa)c'+- Q,cz'l E2a@a- c2) 0 : wern * Qnct I EnGt - c:) * Ey(!+ - cs) - Qoct 0 : Qoc3* Ez+Gt- c+) * Ezq(cz- c+)' Qoc+ Substitutingthe parametersyields the final systemof equation:
}:'t-l l:t*i;!i,,;r]{;i MATLAB can be used to generate the solution. First, we can compute the inverse. 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); 0 >>
A L=f
lT
-
nV
(A,j
0.0449962 0.0034483 0.0049555
I.432 6e-005 0.0062069 0.000r3193
^
n
nAtQ-)aC
nnn 6aaG6
0.00055L12 0.0034483 0.0049655 n n n t 9 ) , -6
0.00010728 0.0034483 0.00096552
260
$
MATRIXINVERSE AND CONDITION '' i''
.,;,0:j{;..., "..;S,'.
{ continued . ; " ,.i,15,j: The solution can then be generatedas >> b=11400 r00 2000 0l , ; >>
c=Af *b
c= 8.0996 1)
?/q,
L6.897 16.483 Thus, we get the surprising result that the smoking section has the lowest carbon monoxidelevels! The highestconcentrationsoccur in rooms 3 and4 with section2having an intermediate level. These results take place because(a) carbon monoxide is conservative and (b) the only air exhaustsare out of sections2 and 4 (Q, and 0a). Room 3 is sobad becausenot only does it get the load from the faulty grill, but it also receives the effluent from room l. Although the foregoing is interesting, the real power of linear systems comesfron using the elementsof the matrix inverse to understandhow the parts of the systeminteract. For example, 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.0034483(1000) : 3.4483 c2,smokers : 7o,moke,.
? 4/l'9.?
d
1L345
x l00vo : 2'7.93Vo
The grill: cz.snl: arytWsrin: 0.0034483(2000): 6.891 Vo";t:
6.897 12.345
x l00%o:55.877o
The intakes: : arl Q,c' + arrt Qt ct,: 0.0034483(200)2 + 0.0062069(50)2 c2,irrrakes : 1.37931* 0.62069: 2 ) V'"snrr r ' , , :' 12.345 The faulty grill is clearly the most significant source. The inverse can also be employed to determine the impact of proposedremediessuch as banning smoking and fixing the grill. Becausethe model is linear, superpositionholds and the results can be determined individually and summed: Lc2: a;tt Lw, ok",+ aur LW-*lr:0.0034483(-1000) + 0.0034483(-2000) : _ 3 . 4 4 8 3_ 6 . 8 9 6 6 : _ 1 0 . 3 4 5
il.I
I l0 -3
Check useaI
rl
ii
PROBTEMS
li i
continued Note that the samecomputation would be made in MATLAB as >> AI \2,I)* (-1000)+AI (2,3\* (-2000) -10.345 Implementingboth remedieswould reducethe concentrationby 10.345mg/m3.The result would bring the kids'room concentrationto 12.345 - 10.345 :2 mg/mt. This makes sense,becausein the absenceofthe smokerand grill loads,the only sourcesare the air intakeswhich areat 2 mg/m3. Becauseall the foregoingcalculationsinvolved changingthe fbrcing functions,it was not necessaryto recomputethe solution.However,if the mixing betweenthe kids' areaand zone 4 is decreased,the matrix is changed
-2s 0 f225 0 l tl J C l r 0 s r s s 0 0 | -22s 0 21s -s0 I I | -5 -250 0
L
2s5Jt
C4
:l {*}*l
The resultsfor this caseinvolve a new solution.Using MATLAB. the resultis Cl
c2
C4
l:{r*tr1
Therefore, this remedy would only improve the kids' area concentration by a paltry 0.265 mp/m3.
PROB[EMS D e t e r m i n e t h e n r a t r i x i n v e r . s e f o l tl o hw e ingsystem: ll.2 Detelnrinethematlixinversefbrthefollowingsystem l0.rr-F2.rz- -rj : 2'7 -3r1 -6.12*2r::-61.5 r 1* , r 2 * 5 x . j : - 2 1 . 5 yourresults by verifyingthat [A][A]-r : [1] Donot aplv0tlngstratcgy.
-8rr+
x2-2x3:-/Q 2x1-6x2- xr:-38 3r1 - -t2*'7x., : -34
ll.3 The following system of equationsis dcsignedto determine concentrations(the c's in g/m3) in a series of
262
MATRIXINVERSE AND CONDITION
(a) Dcterminethe condifionnumberbasedon therow-sum n o r m f o r t h ec a s ew h e r e . r l- 4 . . x z : 2 , a n d . r := 7 . (b] Usc MATLAB to cornputethc spcct.raland Frobenius l 5 c 1- 3 c 2cl :3800 conclitionnur.nbcrs. 3 c r+ l 8 c : 6 r ' .:11 2 6 9 11.10 Usc MATLAB to determine the spectralcondition -4.i t : * l 2 c - l: 2 3 5 0 nutnbertbr a I0-dinrcnsiontrlHilbclt nratrix.How manydig. (a) Dctcrnrincthe lnatri.\inverse. its ofprccisionareexpected to bc lostdueto ill-conditioningl Deterrr-rine thc solutionfor this systernfbr thc casewhere each {b) Usc the inverscto dcterminethc solution. (c) Detcrminchorv much the ratc of rnassinpLttto reactor3 elcnientofthe righrhand-side r,ector{b} consists ofthesum. in its low. In otlrerwords.solve lnust be increascdto inc[rcca l0 g/m3rise in the con- mationof the coefTicicnts for thc casewherc all thc unknownsshouldbe exactlyone.Com. c e n l r u t i ou n l r c a c t o rl . (d) How much will thc concentraticrnin reactor 3 be rc- pal'c thc resultins crrors with those cxpectcdbasedonthe duced if thc rate of mass input lo reactorsI and 2 is conditionnumbcr. l l . l l R e p e a tP r o b . l l . l 0 , b u t f b r t h e c a s e o f a s i x . reducedby 500 and 250 g/day,rcspeclively? ll..l Determinelhc rni"rtrixinversefbr thc systcnrdescribcd dinrensionalVandcnrondcmatrix {sce Prob. ll.9) wnere i n P r o b . 8 . 5 . 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 et h e -fr : 4. It = 2, -I: : 7.-r-+: 10.xs : 3. and"16:5. concentrationin reactor ,5 if the inflow concentrationsare ll.12 The l,owerColoradoRivcr consistsof a series oftbur rescrvoirsas shownin Fie. Pl l. 12. changedto c1l : 20 and ci;.1: -50. Mass balancescan be written fbr each reservoir. ll.5 Dcterminethe matrix invcrselbr the systemdc'scribed and the lbllowing setof sirnultancous in Prob. 8.6. Use the matrix inverscto dcterrninethe fbrce in Iinearalgehraic equalions thc thrcc nrembers(,Fr. & and F-r) if thc vertical load at resuIts: node I is doubledto F1.,,: -2000 lb and ahorizontalload t3.122 0 0 0 of F3.1,- -500 lb is applieclto nodc -3. t3.422 t2.252 0 0 ll.6 Dctelmine f o r a n d l l A l l r .l l A l l1 . llAllcouplcd reactorsas a function of thc antountof nrassinput tcr e-achreactor(the righrhand sidesin g/day):
z [t rAt: I -e I ls -l L
l.l
o 0
t2.252 0
12.3'71 0 12.377 n.797
i-l
:tl_
Belbre detcrmining thc norms, scale thc rnatrix by making the nraxirnumclementin eachrow equalto one. :[ll.7 Determincthe Frobeniusarrdrorv-sumnorms fitr the systemsin Probs.I 1.2and I 1.3. where the right-hand-sidcvector consistsof the loadingsof l l.ll Use MATLAB to deternlinethc spectfalconditionnurn- chloride to eacho1-thcfbr"rrlakesalld {rl. c2,r'r, andc',= fie ber for the fbllowin-csystem.Do not nornralizethe systcm: rcsulting chloriclecoltcentratiousfor Lakcs Powell,Mead, Mohave.and Havasu.respectively. 1 s 16 r,-l (a) Use the matrix invelse to solve tbr the concentrationsin [t e 16 2s 361 eachof thc fbur lakes. 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 concentrationof Lake Havasuto be75? l16 (c) Using the column-sumnorm. computethe condition L2.5 36 4e 61 8r .l nurnbcr and how many suspcctdigits would begenuCompute the condition rrumberbascclon the row-sur.nnorrn. atedby solvingthis systcnr. ll.9 Bcsidesthe Hilbert matrix. there arc olher matrices ll.l3 (a) Dcternrinethc rratrix invcrseandcondition num. that are inherentlyill-conditioned.Onc such case is the ber fbr thc fbllowing rnatrix: Vantlcnnotttlerzalnr, u'hich has thc fbllou,ing tbrm:
['i r 'I ll l-rl ,r'
l'1,. rl
[ ' 5t 6, ll 14 Lt8eJ
( b ) R c p e a(t a ) b u t c h a n c ea . , s l i s h t l vt o 9 . 1 .
ll.l4 Po unique (z Such poll f(x):
P
where th, way for r algebraic the coeff
PROBLEMS
Upper Colorado River
Lake Powell
Lake Havasu
F I G U R EP I I . I 2 TheLowerColorodoRiver
coefficientsof the fourth-orderpolynomialf(r) : p6" * pzx3t pzx2+ p$ + psthat passesthroughthe following five points:(200,0.746),(250,0.675),(300,0.616),(400,0.525), and (500, 0.457).Each of thesepairs can be substitutedinto (Pl 1.14) l p2x"-2+ . . + pn rx * pn Eq. (Pl1.l4) to yield a systemof five equationswith five thep's are constant coefficients.A straightforward unknowns(thep's). Use this approachto solve for the coeffor computingthe coefficients is to generate n linear ficients. In addition, determineand interpret the condition number. equationsthat we can solve simultaneouslyfor that we want to determine the coefficients. Suppose Polynomial interpolationconsistsof determiningthe (n - 1)th-orderpolynomial that fits n data points. polynomials havethe generalform,
,','"
t,
Iterotive Methods
CHAPTER OBJECTIVES The primary objectiveof this chapteris to acquaintyou with iterativernethodsfor solving simuitaneousequations.Specificobjectivesand topicscoveredare ' ' ' '
Understandingthe differencebetweenthe Gauss-Seideland Jacobimethods. Knowing how to assessdiagonaldominanceand knowing what it means. Recognizinghow relaxationcan be usedto improve the convergenceof iterative methods. Understandinghow to solve systemsof nonlinearequationswith successive substitutionand Newton-Raphson.
terative or approximatemethods provide an alternativeto the eliminationmethodi I describedto this point. Such approachesare similar to the techniqueswe developed to I I obtain the roots of a single equationin Chaps.5 and 6. Those approaches consistedof guessinga value and then using a systematicmethod to obtain a refined estimate 0f the root. Becausethe presentpart of the book dealswith a similar problem-obtaining thevaluesthat simultaneouslysatistya setol'equations-we might suspectthat suchapproximate methodscould be useful in this context. In this chapter,we will presentapproaches for solving both linear and nonlinearsimultaneousequations.
l2.l
IINEAR SYSTEMS: GAUSS-SEIDEL The Gauss-Seidelmethodis the most commonly used iterativemethod for solvinglineu algebraicequations.Assumethat we are given a setof il equations: lAl{"r} : {b} Supposethat for conciseness we limit ourselvesto a 3 x 3 setof equations.If thedragonal elementsare all nonzero,the first equationcan be solvedfor.r1, the secondfor x2,andthe
EXAM
12.1 LINEAR SYSTEMS: GAUSS-SEIDEL
265
third for x.r to yield
xi
, Dr-4ttX.
t:l
-clrr-{l ,-!
(t2.1a) atl
, i tl D) -AtqXi -d)t.t;
(12.llr)
a22
bt-ayrl-a:txl
(12.rc)
1133
whereT and j - I are the presentand previousiterations. To start the solution process,initial guessesmust be made for the x's. A simple approach is to assumethat they are all zero.Thesezeroscan be substitutedinto Eq. (12.1a). which can be used to calculatea new value for -r1 : bt latr. Then we substitutethis new valueof r1 alongwith the previousguessof zerofor-rr into Eq. (12.1b)to compute'anew value for x2. The processis repeatedfor Eq. (12.1c)to calculatea new estimatefor x3. Then we return to the first equationand repeatthe entire procedureuntil our solutionconverges closely enoughto the true values.Convergencecan be checkedusing the criterion that for .lli' Jri -r/'I e,,.': l--l lr;l :XAMPLE l2.l
x 1 0 0 %< r ,
rll rr
G o u s s - S e i d eMl e t h o d ProblemStqtemeni. Use the Gauss-Seidel methodto obtainthe solutionfor 3xr-0.1r2-0.2x3: 1.85 0.lxr* 7.r2-0.3xj: -19.3 0 . 3 1 1- 0 . 2 t 2 + l 0 r 3 71.4 Note that the solutionis ixlr :
|3
-2.5
i ).
Solution. First,solveeachof the equationsfor its unknownon the diagonal: 7.85+0.1,r2f0.1yl .f1
. !
'10 1
3 - 19.3- 0.lrr t 0.3.r,r 1 1 1.4- 0.3xr -l 0.2x2
:
By assuming that;r2andx3 arezero,Eq.(E12.1.1) canbe usedto compute ^t
-
7.85+0.r(0)+0.2(0)
:2.616661
( Er 2 r. . l ) (Er2.1.2) (E12.1.3)
266
METHODS ITERATIVE into Eq'(El2'l This value,alongwith the assumedvalueof -rr : 0, can be substituted to calculate
- 1 9 . 3- 0 . 1 ( 2 . 6 1 6 6 6 + 10). 3 ( 0 )-
,1
1qL\)L
The first iteration is completeclby substitutingthe calculatedvaluesfor 'rt and'r: into E q . ( E 1 2 . 1 . 3t )o y i e l d
1 1.4- 0.3(2.616661 ) + 0.2(-2.191524) : 7.005610 l0
a 1 -
For the seconditeration,the sameprocessis repeatedto compLlte
^l
-
I 0) + 0.2(.1'0056 7.85+ 0.r(-2.194524) :2.990551 3 - 1 9 . 3- 0 . 1 Q . 9 9 0 s 5+7 0) . 3 ( 7 . 0 0 s 6 1- 0 -) 1 4 q q 6 r 5 7 1 r.4 - 0.3(2.990s57 ) + 0.2(-2.499625) :1.000291 l0
couldh The method is, therefore,convergingon the true solution.Additional iterations th know applied to improve the answers.However, in an actualproblem, we would not theenorFr true answerapriori.Consequently,Eq. (12'2) providesa meansto estitnate for x1: example, ',
- 2'616661| .,00" : |2'990551
|
2.qe0ss7
:
12.5(/'
|
F o r x 2 a n c l ' r r , t h e e r r o r e s t i m a t o s i l t ' € € , , . 2 : l l 'o8tcl d/ oe ' ' 3 : A ' 0 J 6 c / cN' o t e t h a t ' a s w o (12'2) usu the casewhen determiningroots of a singleequation,fbrmulationssuchasEq' theyensun are met, when they Thus, ally provide a conservativeappraisalofconvergence. that the result is kttown to at leastthe tolerancespecifiedby e''
ud method,it is immediately As eachnew r value is computeclfor the Gauss-Seiclel is converging'th solution if the Thus, value. -r another in the next equationto cleter-n-rine bestavailableestimateswill be enrployed.An altemativeapproach,calledJttcobiiteratiwr' utilizes a somewhatdifferent tactic. Rather than using the latest availabler's, thistch u niqueusesEq. ( 12.I) to computea setof new .r's on the basisof a setof old x's.Thus, forthenefl retaiued are rather but used not immediately are new valuesaregenerated,they iteration. betweenthe Gauss-Seidelmethocland Jacobi iterationis depictedin The clifTerence Fig. 12.I . Although therearecefiaincaseswheretheJacobirnethodis useful,Gauss-Seidel! utilization of the bestavailableestimatesusuallymakesit the methodof preference'
267
G:A U S S - S E I D E L SYSTEMS I2.1 LINEAR
First iteration
x, :
I b2 - d: rrr
\:
(bz-
a 3 1 x . t-
- atzr2 *
arrx)/atr
a v . \ 1 \/ a . 2
- u27x.\fu'
ap:t1)/a1z
- ttyx2)/a1
Second iteration
i r : ( h r - n , . r , - c l , 1 r 1 ) / ,ir 1
xr:
(b2 - a2trt - ar.x.)faa,
,r. :
(bq -
a31r.t - ct.r.r2)/ajz
x, - (bt - azrxt - a.14)/azz
-rr - (b.r
tt.rx, - tt.ttr)f ar. (b)
@l
F I G U R EI 2 . I ond (b)theJocobiiferotive between(o)theGoussSeidel of thedifference C1opn.otdepicrron m e t h o dfso rs o l v l n sql m ui o n e o u si n e o ro l g e b r o iecq u o t i o n s
l2.l.l
Convergence ond Diogonol Dominonce
Note that the Gauss-Seidelmethodis similar in spirit to the techniqueof simplefixed-point iterationthat wls usedin Section6. I to solvelbr the roots of a singleequation.Recall that simple fixed-point iteration was sometimesnonconvergent.That is, as the iterationsprogressed,the answermoved fartherand fartherfrom the correctresult. methoclcan also diverge,becauseit is designedfor linear Although the Gauss-Seiclel is much more predictablethan for fixed-point iteration of converge to ability systems,its that if the following conditionholds,Gauss-Seidel shown lt be can nonlinearequations. will converge: II \i1t
lo;;l:> )
ul i-l
I
l0;;l
(r 2 . 3 )
i+i
That is, the absolutevalue of the diagonal coefficient in each of the equationsmust be in the equation.Such larger than the sum of the absolutevaluesof the other coelTicierrts systenisare said tobe tliugonalb dotninant.This cliterionis sufficientbut not necessary for convergence.That is, although the method may sometilneswork if Eq. (12.3) is not met, convergenceis guaranteedif the condition is satisfied.Fortunately.many englneering and scientific problems of practical imporlance fuhlll this requirement.Therefore. Gauss-Seidelrepresentsa feasibleapproachto solve many problemsin engineeringand science.
268
METHODS ITERATIVE 12.1.2 MATTAB M-file: Gaussseidel Before developing an algorithm, let us first recastGauss-Seidelin a form thiit is compltible with MATLAB's ability to pelfbrm matrix operations.This is done by expresstttg E q .( 1 2 . 1 a) s bt
_
b2
-
; : ..,i.* '
U.t AT
a12 , o-v )
Atl
Cltl
-
_
,,,;,0 all
4f | -.n.r'
ll2-l
;^,
^r^'
ntl
.-nl.l
xi,.* - !!*uc* rl33
a.l
conciselyin matrixform as Noticethatthesolutioncanbe expressed { : r }: { d } - l c l { : r } where
: tfl:';':) {,1} and
anlal 0 [ : l a z t l t t z z 0 tcl osz a,,t a.,t L f lctr
onlottf ay/a2 l 0 -l
Eq. (12.a)is listedin Fig. 12.2. An M-file to irnplernent | 2.1.3 Reloxolion Relaxationrepresentsa slight modificationof the Gauss-Seidelmethodthat is designedto usingEq. ( 12.1).thatvalueis After eachnew valueof r is compr"rted enhanceconvergence. averageof the resultsof the previousand the presentiterations: modifiedby a wei-ahted .r,l'*: lr,lt" + (l
),).rild
(12.5)
where ). is a weighting f'actorthat is assigneda value between0 and2. If ). : 1, (1 - i) is equalto 0 and the resultis unmodified.However.if I is setata value between0 and I . the result is a weightedaverageof the presentand tl.repreviousreIt is typicallyemployedto make Tlris type of modificationis calledwtderrela.ration. sr"rlts. by dampeningout oscillations. hasten conversence converge or to system a ltonconvergent For valuesof .]. from I to 2, extra weight is placed on the presentvalue. In this instance,thereis an implicit assumptionthat the new value is moving in the correctdirection toward the true solution but at too slow a rate.Thus, the addedweight of /. is intendedto improve the estimateby pushing it closer to the truth. Hence,this type of rnodification, which is calledot'errelaratiort,is designedto acceleratethe convelgenceof an alreadyconor SOR. vergentsystem.The approachis also calledsuccessiveot'errelaxcfiion,
LINEARSYSTEMS: GAUSS-SEIDEL
functiol-r x = GaussSeidel (A,b, es,maxtt) ? GaussSeidel: causs Seidel nethod eo x = Gaus:lSei.del (A, b) : causs SeicleI
vrithout % lnput: % A=coefficientnatrix Z b - rlghL hand side vector ( d e f a u l l - - u '.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.2,err.l r p . l t d i . g u m e n -s y o q u i r e d , r , F t . d if nargin> x=x ,l\f X.-
2.0360 2.'-]/139 Althoughwe couldcontinuethe iterationsin the commandmode,a niceralternative is to expressthe algorithmas an M-file. As in Figure 12.4,this routineis passedan M-file that computesthe fbnction valuesand the Jacobianat a given value of "r.It then calls this function and implernents Eq. (12.l7) in an iterativefashion.The routineiteratesuntil an upper limit of iterations(maxit) or a specifiedpercentrelativeenor (es) is reached. We should note that there are two shortcomingsto the foregoing approach.First. Eq. (12.15)is sometimesinconvenientto evaluate.Theretbre,variationsof the NewtonRaphson approachhave been developedto circunrventtlris dilemma. As might be expected.most alrebasedon using finite-differenceapproxir.nations fbr the partialderivatives that compriseIJ]. The secondshortcomingof the multiequationNewton-Raphsonmethod is that excellentinitial guessesare usuallyrequiredto ensureconvergence. Becausethese are -somefimes difTicult or inconvenientfo obta.in.alternafiveapproachesthat are slower
I i
276
ITERATIVE METHODS
f u n c L i - o r r l x , f , e a , i t e r l = n s . ' , ' 5 m ut 1( f L l n c , x 0 , e s , m a x i t , . z a r a r g i n ) % newtmult: Newton-Raphson root zeroes nonlinear systems ? [x, f,ea,iter]=nev/tmult(func,x0,es,maxit,p1,p2, ... ), % uses the Newton-Raphson method to find the roots of % a system of nonlinear equations ? input: % func = name of function that returns f and J Z x0 - initial guess % es = desired percent relative (default = 0.0001?) error a maxit = maximum allowable (default = 50) iterations p1,p2,... = additional parameters used by function ? ?r output: ? x = \/ector of roots Z f = vector of frrnctions evaluated ar roors 2 ea = approxinate percent- relative (Z) error = number of iterations % iter rf nargin
l
I
o,
-cLr
> hist(s)
3 6.4I4 6.452 6.49 6.528 6.566 6.604 6.642 6.68 6.718 6.7s5 The resultinghistogramdepictedin Fig. 13.4is similarto the one we generated by handin Fig. 13.3.Note that all the argumentsand outputswith the exceptionof y are optional.For example,his'* (y) without output argumentsjust producesa histogrambar plot with l0 bins determinedautomaticallybasedon the ranqeof valuesin r.'.
FIGUREI3.4 Hisfogrcmgeneroiedwith the MATLABhist funciion
6.5
6.55
6.6
6.65
6_7
6.75
6.8
6.85
292
LINEAR REGRESSION
13.2
TINEARTEAST.SQUARES REGRESSION Where substantialerror is associatedwith data,the best curve-fitting strategyis to derive 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. Although such"eyeball" calculaapproacheshave commonsenseappealand are valid for "back-ot'-the-envelope" tions, they are deficient becausethey are arbitrary.That is, unlessthe points defineaperfert would straightline (in which case,interpolationwould be appropriate),differentanalysts draw different lines. To removethis subjectivity.somecriterion must be devisedto establisha basisforthe fit. One way to do this is to derivea curve that minimizesthe discrepancybetweenthedata points and the curve. To do this, we must first quantify the discrepancy.The simplestexamp 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 opf a i r e d o b s e r v a t i o(nr sr ,:) l ) . ( x z , y ) , . . . , ( . r , , ) n ) , The mathematicalexpressionfor the straightline is (13.8)
y : a o* z t l x1 e
where a6 and a 1 are coefficients representingthe intercept and the slope,respectively, and e is the error, or rcsidual, between the model and the observations,which can be representedby rearrangingEq. (13.8)as (13.9)
e:!-as-a1x
Thus, the residualis the discrepancybetweenthe true valueof y andthe approximate value, a0 + atx, predictedby the linear equation.
I 3.2. 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.as in
i
L't r:l
",
:- i ,r, - as- a1x;) /-'rl
(l3.lo)
i:l
wheren : total numberof points.However,this is an inadequatecriterion,asillustratedby F i g . l 3 . 5 a , w h i c h d e p i c t s t h e f i t oaf s t r a i g h t l i n e t o t w o p o i n t s . O b v i o u s l y , t h e b e s t f i t i s t h e line connectingthe points.However.any straightline passingthroughthe midpointofthe connectingline (excepta perfectly vertical line) resultsin a minimum valueof Eq.(13,10) equal to zero becausepositive and negativeerors cancel. One way to remove the effect of the signs might be to minimize the sumof theab solutevaluesof the discrepancies. as in iL,t "' r,t, i=l
-:
i , ' , ' , - a o-
Lt.'t i:l
alxil
(l3ll)
Figure 13.5bdemonstrates why this criterionis also inadequate.For the four pointsshown, any straightline falling within the dashedlines will minimize the sum of the absolute values ofthe residuals.Thus, this criterion also doesnot yield a uniquebestfit.
il I 3.2 LINEARLEAST-SQUARES REGRESSION
293
1il lrl li
il
ttltt' -l,tt't7'j-'''--
oo2ote'
,l il
ii ,i
(c)
FIGUREI3.5 F x n m n l e s n [ s o n - e 6 1 1 t o s r{69 . b e s ' r i , t h o t o r c : n g l e n r r n t c f n r . e , t , e s s i o n l , - r lm ^ m ; z e s t h e s u m
o f t h er e s i d u o l(sb, )m i n i m i z et hs es u mo f t h eo b s o l u tveo l u e os f i h er e s i d u o losn, d ( c )m i n i m i z e s l h em o x i m u m e r r o ro f o n y i n d i v i d u o p lo i n t .
A third strategyfor fitting a bestline is theminitna-rcriterion.In this technique,the line is chosenthat minimizes the rnaximumdistancethat an individual point falls from the line. As depictedin Fig. 13.5c,this strategyis ill-suited forregression becauseit gives undue influence to an outlier-that is, a single point with a large error. It should be noted that the mininax principle is sometimeswell-suitedfor fitting a simple function to a complicatedfunction (Carnahan,Luther, and Wilkes, 1969). A strategythat overcomesthe shortcomingsof the aforementionedapproachesis to minimize the sum of the squaresof the residuals: iltI
: f- (v; - as- arxi)z S, : 5-"i L-t /--':' ;-1
;-l
(13.12)
,l il
294
TINEAR EGRESSION
This criterion, which is called leastsquares,has a numberof advantages, includingthatit yielclsa uniqueline lor a given setof data.Beforediscussing thesepr-o-pertles, we will presenta techniquefor cleterminingthe varuc-sof (h andat that minimize Eq. ( 13.r 2).
13.2.2 leost-SquoresFir of o Srroight Line To determine varuesfor .,s and at, Eq. (13.12)is differentiated with respectto each unknowncoeflicient: i)s'
: -? \- r,,
un -
tt 1x;)
ilLti
t't A^:
: - z2 \ - r r , , - ( / { ) - a 1 ' r , ) rl , Lll.\'l
Note that we have simplified the summationsymbols;unlessotherwiseindicated, all summationsare fiorn r : I to rl. Settingtheseclerivativesequalto zero will result in a minimurn S,..Ifthis is done,the equationscan be expressed as
o:f.n;-f,,u-f,,1,, - f ,n,.,-, - f ,,,*i o : f r;.r', .
L
Now, realizingthat I a0 : noy, we can expressthe equationsas a setof two simultaneous lirrearequationswith two unknowns(aoandnr).
tr
(1,, * (f
O \ s - . . , \t r , , + ( \ -
",)
,, : f .r,
'\
\LJ'ri)tt1
r-
:Lr,.t''
Thesearecalledthe nonnal equation.t. They canbe solvedsimultaneously for , _ ,rI.r,.r'; I.r, I.r.i
ltT.'? - (I ',)'
(13.r5)
This resultcan rhenbe usedin conjunctionwith Eq. (r3.r3) to solvefor ao: i - atx wherev andi arethe meansof .vand.r,respectively. E X A M P L E1 3 . 2
L i n e o rR e g r e s s i o n ProblemStotement. Fit a straightline ro rhevaluesin Table i3.1. Solution. In this application,fbrce is the clependent variable(,v) and velocityis the independentvariable (.r). The data can be set up in tabulirrfbrm and the necessarvsums
REGRESSION I 3.2 LINEARLEAST-SAUARES TABTE13.4
295
neededto compulethe best-fit linefor the doto Dotoond summotions
Toble, ]3,,I from xi
I 2 3 4 5 6 7 B
:
I
li
to 2A 30 4A 50 60 7A BO
25 7A 380 550 610 t 224 830 1, 4 5 0
q I ?
> vm-Ira(2) \ rrr
i:
-':'
aRral
.{S=\.1n*J
( 1 j
ks= 3 r :. 2 : t6 ( ) Thus, the best-fit model is ,,_
5.25701S1
86.2260 + [s]
Although the highralue of 12might lead you to believe rhat this result is acceptable, inspection of the coefficients might raise doubts. For example, the maximum velocity 15'2510)is much greaterthan the highestobservedvelociry tb.sol. In addition, the halfsaturation tate (86.2260) is much bigger than the maximum substrate concentration (9). The problem is underscoredwhen the fit is plotted along with the clata.Figure l3.I5ta shows the transformedversion.Although the straightline iollows the upward trend, the dataclearly appearsto be curved.When the original equationis plotted along with the data in the untransformedversion (Fig. 13.15b),the fit is obviously unacceptable. The data is clearly leveling off at about 0.36 or 0.37. If this is correct. an eyeball estimatewould suggestthat u^ shouldbe about 0.36, and ft, shouldbe in the rung. of 2 to 3. Beyondthe visual evidence,the poornessof the fit is alsoreflected by statisticslike the coefficient of determination.For the untransformedcase,a much less acceptableresult of I - 0.6406 is obtained. The foregoing analysiscan be repeatedfor the second_order model. Equation (13.28) can also be linearizedby inverting it to give I I k? I +j_ u D, u. [Sl2 The linreqr function from Fig. 13.13can againbe usedto determine the least-squares fit:
l9..l7iiO i:l
=
? .. 4 4 9 )
3to
LINEARREGRESSION
#i*,i ; (a) Transformed model
0.4
0.5
14,r1 (b) Originalmodel
0.8 0.6 a 0.4
o.2 0 isl I3.I5 FIGURE Theplotin modelolonqwithdoto(points}. fit{line) Plots of leosl-squores of theMichoelis-Menten fit,ond{blshowshowlhefit lookswhenviewedin iheuntronsformed, {o)showsthetionsformed oilgrnorrorm.
The model coefficients can then be calculated as >> vm=I/a(2) 0.4083 >> ks-sqrt(vm*a(1)) 2 . BI?,1 Substituting these values into Eq. (13.29) gives
0.4083ts12 u:7.91r+[s:p Although we know that a high I doesnot guaranteeof a good fit, the fact thatit is very high (0.9929) is promising. In addition, the parametersvalues also seemconsistentwiththe trends in the data; that is, the ft- is slightly greaterthan the highest observedvelocityand stthcfrqfc
nnnnanfrofinn
/o\
I3.5 CASE STUDY
3ll
ffi'-ru conrinued i,lt+F.$fii:{:ifl (a)Transformedmodel
t/$12 ( b ) O r i g i n a lm o d e l
0.4 0.3 . 0.2 0.1 0
tsl FIGURE T3.I6 Michoelis-Menfen modelolonowithdoto Plots fit(line) of thesecond-order of leost-squores fit,ondib)shows iheuntronsformed, originol form thetrqnsformed Theplotin (o)shows {points).
graphically.As in Fig. 13.16a,the transformed The adequacyof the fit can be assessed results appear linear. When the original equation is plotted along with the data in the untransformedversion (Fig. 13.16b),the fit nicely follows the trend in the measurements. Beyond the graphs, the goodnessof the fit is also reflected by the fact that the coefficient of determination for the untransformedcasecan be computed as I : 0.9896. Based on our analysis,we can conclude that the second-ordermodel provides a good fit of this data set. This might suggestthat we are dealing with an allosteric enzyme. Beyond this specific result, there are a few other generalconclusionsthat can be drawn from this case study. First, we should never solely rely on statistics such as I as the sole basisofassessinggoodnessoffit. Second,regressionequationsshouldalwaysbe assessed graphically. And for caseswhere transformations are employed, a graph of the untransformed model and data should always be inspected. Finally, although transformationsmay yield a decent fit of the transformed data, this does not always translateinto an acceptablefit in the original format. The reasonthat this might occur is that minimizing squaredresidualsof transformeddata is not the sameas for the untransformed data. Linear regression assumesthat the scatter of points around the
3r2
LINEARREGRESSION ":;'i:-,'; " ':, '
'";;
'i
Lineariz on the f( the data.
, continued
best-fit line follows a Gaussiandistribution, and that the standarddeviation is the sameat every value of the dependentvariable. Theseassumptionsare rarely true after transforming data. As a consequence of the last conclusion,someanalystssuggestthat ratherthanusing linear transformations,nonlinear regressionshould be employed to fit curvilinear data.In this approach, a best-fit curve is developed that directly minimizes the untransformed residuals.We will describehow this is done in Chap. 14.
l3.l Given the data
9.5 l0.l 10.4 9.5 9.5
9.8 9.2 7.9 8.9 9.6
9.4 11 . 3 10.4 8.8 10.2
That is, detelminethe slope that resultsin the least-squares fit for a straightline with a zero intercept.Fit the following data with this model and display the result graphically,
t0.0 9.4 9.8 10.6 x 2 4 6 7 y4565 8.9
Determine (a) the mean, (b) median, (c) mode, (d) range, (e) standarddeviation, (f) variance,and (g) coefficient of variation. lJ,2 Construct a histogram from the data from Prob. 13.I. Use a rangefrom 7.5 to 11.5with intervalsof 0.5. 13.-l Given the data
28.65 28.65 27.65 29.25
26.55 29.65 28.15 2'7.65
26.65 2'/.8s 28.65 28.6s
27.65 27.05 28.45 27.65
27.35 28.25 3 r .65 28.s5
28.35 28.85 26.35 27.65
26.85 26.75 27.'75 21.2s
Determine (a) the mean, (b) median, (c) mode. (d) range, (e) standarddeviation, (f) variance,and (g) coefficient of variation. (h) Constructa histogram.Use a range from 26 to 32 with incrementsof 0.-5. (i) Assuming that the distribution is normal, and that your estimateof the standarddeviation is valid, computethe range (that is, the lower and the upper values) that encompasses68Vofthe readings.Determinewhetherthis is a valid estimatefor the data in this problem. 13,.1Using the same approachas was empioyed to derive Eqs. (13.15)and (13.16),derivethe least-squales fit of the following model: \t-n1r'-lp
13.8 Fir naturalI 13.9 Tb areais n
f (hr) c (CFU/
PROBI.EMS
8.8 9.4 10.0 9.8 10.1
x 0 .1 y O.75
t0ill41720 876912
regressionto fit a straightlineto 13.5 Use least-squares
x02469 y56764
ll I
12 t5 710
l7 l2
19 t2
Along with the slopeand intercept,computethe standardenor of the estimate and the correlation coefficient. Plot thedata and the regressionline. Then repeatthe problem,but regress x versus}-that is, switch the variables.Interpretyour results. 13.6 The following datawas gatheredto determinetherelationship betweenpressureand temperatureof a fixedvolunteof I kg of nitrogcn.The volumeis l0 mr. T,'C p,Nlm2
-aO t 2A 0 40 B0 160 6 9 0 0 B1 0 0 9 3 0 0 1 0 , 5 0 0 I 1 , 2 0 0 t 2 9 0 0
Enrploy the ideal gas law pV - nRT to deterrninert onthe basis of this data. Note that for the 1aw,I must be expressed in kelvins. 13.7 Beyond the examples in Fig. l3.ll, there areother models that can be linearized usins transformations.FoI example, \) : a.^rep1r
The timr and the r estimate and (b) 2OOCF be cons impossi creases 13.10 F (Eq. l3 model:
When t results parame
(d').U
tion, de 13.11( related ber of (kg) git
w(kgl A (m')
Show t well. E
3r3
PROBLEMS this model and use it to estimatear and 6.r based thefollou,ingdata.Develop a plot of your fit along rvith data.
0 t 0 2 0 4 0 6 c )9 t 3 t5 17 tB a/5 125 145 125 085 055 035 028 0 rB
Fita powermodel to the datafiom Table 13.l, but use 13,8 logarithmsto perform the transformations. nafural 13,9Theconcentrationof E. c'oli bacteriain a swimnrins ueais monil.ored after a slorm:
4 ,{h4 B mL) ls9O I 320 c(CFU/l0O
t6 2A 24 900 650 560
Ihetimeis measuredin hoursfollowing the end of the storrn ardtheunit CFU is a "colony lbrrning unit." Use this data to (a) the concentrationat the end of the storm (t: 0) estimate and(b) the time at which the concentrationwill reach 200CFU/100mL. Note that yor.rrchoice of model should beconsistent with the fact that negativeconcenrationsale inpossible and that the bacteria concentrationah.r,aysdewith time. creases Rather than using the base-e exponential nrodel 13.10 { E qI.3 . 2 2 t u. c o m n l o na l t e r n a t i v ies to employ a base-10 model:
13.12 Determinean equationto pledict metabolismrateas a function of massbasedon the followinc data: Animol
Moss (kg)
Cow Humcn Sheep Hen Rcri Dove
400 7A 45 2 03 0 16
270 B2 50 4B 1 /
> S r = s u m( ( y - Z * a ) . ^ 2 )
3 .'t 466 Then r2 can be computed as > > 1 2 = I - S r / s u m( ( y - m e a n ( y ) ) . ^ 2 ) 0.998s and sr,7,.can be computed as >> syx -
sqrt (Sr/ (lenqth (x) -lenqth
(a) ) )
1.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. It also sets the stage for the next section where we will gain some insights into the preferred strategies for solving Eq. (1a.10). The matrix notation will also have relevance when we tum to nonlinear regression in Section 14.5.
,I4.4
AND THEBACKSLASH OPERATOR QR FACTORIZATION
32s
14.4 QR FACTORIZATION AND THEBACKSIASHOPERATOR Generatinga bestfit by solving the normal equationsis widely usedand certainlyadequate for many curve-fittingapplicationsin engineeringand science.It must be mentioned,however,tlrat the normal equationscanbe ill-conditionedandhencesensitiveto roundoff errors. Two more advancedmethods,QRfactorization andsingular valuedecomposition,are more robustin this regard.Although the descriptionof thesemethodsis beyondthe scope of this text, we mention them herebecausethey can be implementedwith MATLAB. Further.QR factorizationis automaticallyusedin two simple ways within MATLAB. First, for caseswhere you want to fit a polynomial, the built-in polyf it function automatically usesQR factorizationto obtain its results. problemcan be directly solvedwith the backSecond,the generallinear least-squares slashoperator.Recallthat the generalmodelis formulatedas Eq. (14.8) 1 y y: l Z l { a l
( 1 4I. l )
In Section10.4,we usedleft divisionwith thebackslashoperatorto solvesystemsof linearalgebraicequationswherethe numberof equationsequalsthe numberof unknowns(n : n). For Eq. ( 14.8)as derivedfiom generalIeastsquares,the numberof equationsis greaterthan the number of unknowns (n.> m).Such systemsare said to be overdetermined.When MATLAB sensesthat you want to solvesuchsystemswith left division,it automaticallyuses QR factorizationto obtainthe solution.The following exampleillustrateshow this is done. E X A M P L1E4 . 4
l m p l e m e n t i n gP o l y n o m i o R l e g r e s s i ow n i t h p o , y i j t o n d L e f tD i v i s i o n Problem Stotemeni. RepeatExample 14.3,but use the builrin polyf ir functionand left division to calculatethe coefficients. Solution. As in Example 14.3,the datacan be enteredand usedto createthe lZl matrix as in >> x = >> y = >> z = The potyf
3 4 5l'; lA I2 1 2. I 1 . 1 1 3 . 6 2 1 . 2 4 A . 9 6 I . I l ' [ones (size (x)) x x..2); it
;
function can be used to compute the coefficients:
>> a = polyf it \x,V,2) 1.8607
2 .3593
2.41 B6
The sameresult can also be calculatedusing the backslash: >>a=Z\y 2 .41 86 2.3593 1 8607 As just stated, both these results are obtained autornatically with QR factorization.
326
AND NONLINEARREGRESSION GENERAL LINEARLEAST-SAUARES
I4.5
NONTINEARREGRESSION There are many casesin engineeringand sciencewhere nonlinearmodels must be fit to data.In the presentcontext.thesemodelsaredefinedas thosethat have a nonlineardependenceon their oarameters.For example. -v:ao(l
-r-at"r)*e
( 1 4 .21)
This equationcannotbe manipulatedso that it conformsto the generalform of Eq. (14.7). As with linear least squares,nonlinearregressionis basedon determiningthe values of the parametersthat minimize the sum of the squaresof the residuals.However,for the nonlinearcase,the solution must proceedin an iterativefashion. There are techniquesexpresslydesignedfor nonlinearregression.For example,the Gauss-Newtonmethod uses a Taylor seriesexpansionto expressthe original nonlinear equationin an approximate,linear form. Then least-squares theory can be usedto obtain new estimatesof the parametersthat move in the directionof minimizing the residual.Details on this approachare providedelsewhere(Chapraand Canale,2002). An alternativeis to useoptimizationtechniquesto directly determinethe least-squares fit. For example,Eq. (la.l2) can be expressedas an objectivefunction to computethe sum of the squares: n
f ( a q ,a 1 ) :
)-
[t, - a o \ - , - a t r ' 1 1 2
(t4.r3)
An optimizationroutine can then be usedto determinethe valuesof ae and a1 that minimize the function. As describedpreviouslyin Sec.7.3.1.MATLAB's fminsearch functioncanbe used for this purpose.It has the generalsyntax lx,
fvaLl
= fmlnsearch(fun, xa, options,pl,p2,
...)
wherex = a vector of the valuesof the parametersthat minimize the function f un, f va7 = the value of the function at the minimum. x0 : a vector of the initial guessesfor the parameters,options: a structurecontainingvaluesof the optimizationparametersas created with the optimset function (recall Section6.4), and pl, p2, etc.: additionalarguments that are passedto the objectivefunction. Note that if options is omitted,MATLAB uses default valuesthat are reasonablefor most problems.If you would like to passadditional arguments(p1. p2,...). but do not want to setthe aptions, useempty brackets[] asa place holder. E X A M P L E1 4 . 5
N o n l i n e o rR e g r e s s i ow n ith MATLAB Problem Stotemeni. Recallthat in Example13.4,we fit the power model to datafrom Table l3.l by linearizationusing logarithms.This yielded the model: F : 0.2741ute\a2 Repeat this exercise,but use nonlinear regression.Employ initial guessesof I for the coefficients.
I4.5 NONLINEAR REGRESSION
327
Solution. First, an M-file function must be createdto computethe sum of the squares. The following file, called f ssR. m,is set up for the power equation: function f = fSSR(a,xm,ym) yp = a(1)*xm.^a(2); f = s u m (( y m - y p ) . ^ 2 ) ; In commandmode, the datacan be enteredas >> x = t10 20 30 40 50 60 7O B0l; >> y - 125 7A 380 550 610 1220 830 14501; The minimization of the function is then implementedby >> fminsearch (@fSSR,[1, 1] , 2.5384
tL
x, y)
r.4359
The besrfit model is therefore F : 2.5384u1a35e Both the original transformedfit and the presentversion are displayedi-li'Frg.14.4. Note that althoughthe model coefficientsarevery different,it is difficult to judge which fit is superiorbasedon inspectionof the plot. This example illustrateshow different best-fit equationsresult when fitting the same This model using nonlinearregressionversuslinear regressionemployingtransformations. is becausethe former minimizes the residualsof the orisinal datawhereasthe latter minimizes the residualsof the transformeddata.
FIGUREI4.4 velocity ondunlronsformed modelfitsforforceversus dctofrom Comporison of tronsformed ODIE
J
,
ta
a
z {
800
Untransformed
4 40 u, m/s
, aa a
328
LINEARLEAST-SAUARES AND NONLINEARREGRESSION GENERAL
F IT T INS GIN U S OIDs Bockground. Engineersand scientistsoften deal with systemsthat oscillateor vibrate.As might be expected,sinusoidalfunctions play a fundamentalrole in modelingsuch problem contexts. In this discussion,we will usethe term sinusoidto representany waveformthatcanh describedas a sine or cosine.There is no clear-cut convention for choosing either function, and in any case,the results will be identical. For this chapter,we will use the cosine,which is expressedgenerally as * d) f (t) : As * Cv cos(a-rst
(14.14)
Thus, four parametersserve to characterizethe sinusoid (Fig. 14.5a). The mean valued sets the average height above the abscissa.The amplitude C, specifies the height of the
F I G U R EI 4 . 5 . o r t h i s c o sA f uonl c t i o n } :( A r )o * C , c o s ( a r o r * 9 ) F en , =1.7,C,=), { o }A p l o t o ft h e s i n u s o i d ao: 2n/l 1.5 s),ond 0 : r/3 rodions(b)An olternotive expression of thesomecurveis in Thethreecomponents of thisfunction oredepicted * B,sin{arol). I(r) : Ao + A) + A,cos{coor} of thethreecurvesin (b)yieldsthesingle {b),whereA, : 0.5 ond B, : -0 866.Thesummotion curvein {o).
cl
t,s at, fad
I4.6 CASESTUDY
329
continued oscillation. The angular frequency o.4characterizeshow often the cycles occur. Finally, the phase angle, or phase shift 0 parameterizesthe extent to which the sinusoid is shifted horizontally. It can be measuredas the distancein radiansfrom / : 0 to the point at which the cosine function begins a new cycle. The angular frequency (in radians/time) is related to frequency/(in cycles/time) by oo :2r.f
(14.15)
and frequency in turn is related to period I (in units of time) by I
(14.r6)
r-T
Although Eq. (14.14) is an adequatemathematicalcharacterizationof a sinusoid, it is awkward to work with from the standpoint of curve fitting becausethe phase shift is included in the argument of the cosine function. This means that it is not in the form of the general linear least-squaresmodel (Eq. U.7), and we must use nonlinear regressionto estimate the coefficients. This deficiency can be overcome by invoking the trigonometric identity: sin(p)] cos(p)- sin(cosr) Cr cos(abl + d) : C1[cos(rr-rst)
(r4.r7)
SubstitutingEq. (14.17)into Eq. (14.14)and collectingtermsgives (Fig. 14.5b) f (t) : Ao * Ar cos(a-lol)* 81 sin(arer)
( 1 4l.8 )
where Ar : Ct cos(0)
Br : -Cr sin(9)
(14.19)
Dividing the two partsof Eq. (14.19)gives / B'\ 0 : arctan| --| \ Arl
(14.20)
where if A r < 0, add z to 9. Squaringand summingthe two partsof Eq. ( 1a.19) leadsto
(14.2r) Thus,Eq. (14.18)representsan alternativeformulationof Eq. (14.14)that still requires four parametersbut that is cast in the format of a general linear model (Eq. 4.7). Thus, it can be simply applied as the basis for a least-squaresfit. The average monthly maximum air temperaturesfor Tucson, Aizona, have been tabulated as Month T'"C
.J lB9
F 2 1 . 1 23.3
AM 27.8
32.2
A J 3 7 . 2 3 6 .I
S 1.1 ^
OND 2e.4 23.3
r8.9
330
G E N E R ALLI N E A R LEAST-SQUAR AE NS D NONTINEAREGRESSION
continued Observe that the July value is missing. Assuming each month is 30 days long, fit a sinusoid to this data. Use the resulting equation to predict the value in mid-July. Solution.
In a similar fashion to Example 14.3, we can enter the data and createthe
lZl matrix: >> >> >> >>
vrO=2*pi/360; t=t15 45 75 105 135 165 225 255 285 315 3451'; r=118.9 2r.r 23.3 27.8 32.2 31 .2 36.1, 34.4 29.4 z= [ones(size(t)) cos(w0*L) sin(w0*t)];
23.3 18.91';
The coefficients for the least-squares fit can then be computed as >> a= lZ'*Z ) \ (Z'*T)
28.381 B -9 .2s59 ,2 . BA02 The statistics can also be determined as >> Sr=sum((T-Z*a)
.^2)
6 .4398 >> r2=I-Sr
/ s u m( ( T - m e a n ( T ) ) . ^ 2 )
0.9862 >> syx=sqrt (Sr/ (length(t) syx
-lengch(a)
) )
= 0.8972
A plot can be developed as >> tp=[0:360]; >> Tp=a (1)+a (2 ) *cos (w0*tp)+a (3 ) *sin (w0*tp) ; >> plot (t,T,'o', tp,Tp) The fit, which is displayed in Fig. 14.6, generally describes the data trends. Equations (14.20) and (14.21) can be used to express the best-fit sinusoid in themore descriptive format of Eq. (14.14): >> theta=at.an2 (-a (3 ) , a (2J ) *360 i (2*pi ) theta = L63.r6'76
331
CASESTUDY
continued
15
300
350
FIGUREI4.6 Leost-squores fitof o sinusoid to temperoture dotoforTucson, Arizono >> C1=sert (a(2 ) ^2+a (3) ^2)
9 .67 A2 Note that because it is more physically meaningful, the phase shift is expressed in days rather than in radians. Therefore, the final model is
/2n \ R : 2 8 . 3 8 7 8+ 9 . 6 7 0 2 c o|s* - t r + 1 6 3 . 1 6 7|6 ) \Jbu
/
We can see that the mean is 28.3878, and the amplitude is 9.6702. Hence, the maximum monthly temperaturein Tucson rangesfrom about 18.7 to 38.1 'C. The phaseshift tells us that the peak temperatureoccurs about 163 days prior to the start of the year, which translates to about day 197 (July 17). The value in mid-July (/ : 195 d) can thereforebe computed as > > T p J u l = a ( 1 ) + C 1 * c o s( w 0 * ( 1 9 5 + t h e t a ) ) TpJul = 38.0530
332
G E N E R ALTI N E A R LEAST-SQUAR AE NS D NONLINEAR EGRESSION
Estim and C
PROBLEMS 14.1 Fit a parabolato the data lrom Table 13.1.Determine the r2 for the fit and comment on the efficacy of the result. 14.2 Using the same approachas was employed to derive Eqs. (13.15)and (13.16),derive the least-squares fit of the following model: _ \ ' : . t l i r+ a 2 . t 2 + e That is, determinethe coefficientsthat reslllts in the leastsquaresfit for a second-orderpolynomial with a zero intercept. Test the approach by using it 1(Jfit the data fiom T a b l e1 3 . 1 . 14.3 Fit a cubic polynornialto the following data:
.r34 16 J
578 44 34
36
2.2
9 28
t) 3B
)2 4.6
Along with the coefficients.determinerr and -s,.7_.. lzl.4 Develop an M-file to implement polvnomial regression. Passthe M-file two vectorsholding the -r and ) values along with the desiredorder rn. Test it by solving Prob. 14.3. 14.5 For the data from Table P14.5.use polynornialrellession to derive a predictive equation for dissolved oxygen concentrationas a function of temperaturelor the casewhere the chlorideconcentrationis equalto zero.Employ a polynomial that is of sufficiently high order that the predictions match the numberof significantdigits displayedin the table. 14.6 Use multiple linear regressionto derive a predictive equationfbr dissolvedoxygen concentrationas a function of temperatureand chloridebasedon the dataliom TableP14.5. Use the equationto estimatethe concentrationof dissolved oxygenfora chlorideconcentration of 15 glL atT : 12"C. TABTE P14.5
Note that the true value is 9.09 mg/L. Computethepercent relative error for vour prediction. Explain possiblecauses fot the discrepancy. 14.7 As comparedwith the rnodelsfiom Probs.14.5and 14.6, a somewhatmore sophisticatedmodel that accounts fbr the eflect of both temperatureand chloride on dissolved oxygen saturationcan be hypothesizedas beingof theform o:
xrO1)223344 I x2 0 2 y 151 t7.e t27
T, "C
c:OglL
0 5 t0
45 2B t3
r5
0r
20 25
909 B.26
c=lOg/L
12.9 It3 r0 t 903 B.l7 746 A e5
I 1 2 2 2 I 256 205 35 I 297 454442
Computethe coefTicients. the standarderror of the estimate. and the conelation coefticient. 14.9 Thc lbltowing datawas collectedfbr the steadyflowof u a(erin a concretccilcularpipe: Experiment
Diomeler, m
l 2 3 4 5 6 7 B I
03 06 0.9 03 06 O9 0.3 06 09
Dissolved Oxygen (mglt) for Iemperolure ('C) ond Concenlrotion of Chloride (g/t)
Slope, m/m
0 00t 0 001 0 00r 001 00r 0 0t 005 005 005
Flow, m3/s
aa4 4.24 069 0 13 a82 238 0 . 3r 195 566
c=20g/L
114 t0 3 B96 BOB 735 573 6)O
Use multiple linear regressionto fit the fbllowing modelto this data: Q
14.rr
solar r P.
hG) *./r(c)
That is, a third-orderpolynomial in temperature anda linear relationshipin chloride is assumedto yield superiorresults. Use the general linear least-.squares approachto fit tlis model to the data in TablePl4.5. Use the resultingequadon to estimatethe dissolvedoxygenconcentrationfor a chloride concentrationof 15 g/L aI T : 12 'C. Note that thetrue valuc is 9.09 rng/L. Compute the percentrelativeenorfor your prediction. 14.8 Use multiple linear regressionto fit
Dissolvedoxygen concentrotionin woier os o function of lemoerofure ("C) ond chloride concentrotion(g/L).
t p(t)
- a11D"'5"'
where Q : flow, D : diameter,and S: slope. l4.ll) Three disease-canyingorganisms decay exponentially in scawateraccordingto the following model:
where maxin radiati UtE n Iru, b"
r5( P9(
14.1 and fi
User follor .rU,
,a
14.1. teriz exan
U(
wher subs The
lsl, C C C t l t( 5(
rii ,ii
PROBLEMS
the initial concentrationof cach organisrn(A, B, E$imate C)siventhe followins mcaslrremcnts: and
r
05
, , I, \ A
l AA
'2 )-)
3 .t
f\t
4
5
6
7
9
2
1.9
1.7
1.4
Ll
(a) Use a transfbrnrationto linearizethe nrodeland evaluate thc paramcters.Display the data and the rnodelfit on a grapn. (b) Pcrforrnthc sanreevaluationas in (a) bul usenonlinear regrcsslon. 14.14 Givcn thc data
l4.ll Thefollowing model is uscdto representthe efl'cctol fadiation on the photosynthesis ratc of aquaticplants: r solar
y
I P=P,,, ,-1;*t I ,,,, where P : the photosynthesis rate 1mg m 3d-l), P,, : the photosynthcsisratc (mg m-id l.), 1 : solar maximum (lrE nr 2s l). and 1,,,/: optimal solar radiation radiation rs-l). Usc nonlinearrcgressionto evaluateP,, and lpEm based 1,u, on the fbllowing data: r
5a\
P99
80 r30 200 254 350 454 550 7AA )77 2A2 248 229 219 173 142 72
5 l7
l0 24
25 37
35 4A 45 40 40 42
30 37
50 4l
legressionto {lt (a) a straightlinc, (b) a usc least-squares power equation,(c) a saturation-growth-rate equirtion.and (d) a parabola.For (b) and (c), employ transfbnnationsto Iincarizcthe data.Plot the dataalongwith all the curves.Is arly one of fhe curve-ssuperior?If so,jLrstify. 14.15 The following datarepresentsthe bacterialgrorvthin a liquid cultureover of numberof days:
Doy
a
Amounl x to linearrze l{,12In Prob. I3.8 we used transtbrmations 106 fit thefollowing moclel: and )'
15 20 31 33
4
B
t2
t6
20
67 38 7467 8274 9l69 rot 6C l]2 58
- n1't g'-B ,r'
Find a best-flt equation to the data trend. Tr)r several nonlinearregressionto estimateaa arrdp.r basedon the possibilities-polynomial,logarithmic, and exponential. Use data.Developa plot ol your fit along with thc da1a. Determinethc bestequationto predict the amouut.of bactefollowing riu atier 30 days. Ii, a2 04 06 oe r3 r5 t7 IB 14.16 Derive the lcast-squares flt ol the fbllowing model:
1 4 . 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.The following is rn of a model that is usedto fit sr-rch reactions: example k,,,IS]3 ''-K+[.tll
.f': l]1,\:lazx2+e. That is, determinethe coefficientsthat resultsin the leasl squares tit tbr a second-orderpolyuomial with a zero intercept.Test thc approachby using it to fil the data fron.r T a b l e1 3 . 1 . 1 4 . 1 7 D y n a m i cv i s c o s i t yo f w a t e rp ( 1 0 3 N . s / m 2 ; i s l c I('C)in the ftrllowinglranner: lateclto telnperaturc
u,here u0: the initial rate of the reaction (M/s). [S] : tne concentration(M). and ft,,,and K are paramcters. T substrate Ihefollowingdata can be tit with this modcl: lL
lsl,M
cc r 005 0l 0-\ ,5
rc 50 r00
uo,M/s 6 O Z Bx l O ') 7.595x la 6 C ) 6 3r l 0 3 5ZBBxlOr' 1 . 7 3 7x l O ' 2423v.)a5 2430xlC' 2431 x la' 2 , 1 3 1. t O 5
0 5 i.787 I5t9
t0 I3Q7
20 I002
30 4a a 7,)75 0 6529
(a) Plot this data. (b) Use linearintcrpolatior.r to predictp at T :7 .5 "C.. (c) Usepolynornialregressionto lit a parabolato thc data in order lo make the si.rmeprediction. 14.18 Use the following setof pressure-volume datato find the bestpossiblcvirial constants(A, and Ar) fbr the following equationof state.R : 82.05rnL atm/gnlol K. and I :
-r03K. PV
At
At
I i It
334
P (orm)
V (mt)
GENERAL LINEARLEAST-SAUARES AND NONLINEARREGRESSION
O 9B5
2saoo
I t0B 22,204
I 363 rB000
collectedto quantify this relationship.Developbest-fitequa-
163t tions fbr both the thinking and braking components.Use r 5 0 0 0 these equationsto estimatethe total stoppingdistancefor a car travclingat ll0 km/h.
l.l.l9 Environmentalscientistsand engineersdealing with thc impacts of acid rain must detcrmine the value of the ion productof water K,, as a lunction of temperature.Scientists have sr.rggested the following equation to model this relationshio: - l o 9 1 6K .
a
0 t0 2A 30 4A
* D log,o 7,, I cT,, I d
t0 : l0 r5 l0-r5 t0-' t0- '
45 8.5 l2 3
60 75 90 ltt 145 )67 2 l . o 3 2 . 9 4 76
t2A 224 847
fime, h 0 2 4 5 7 I l2 t5 2A 2224 pH 76 72 7 6.5 7.5 72 8.99t BS 79 7
1.1.22The solar radiation fbr Tucson. Arizona. has been tabulatedas
K, I . 1 6 4x 2950x 6846x 1467x 2 . 9 2 9x
30 56 5.0
ll.2l The pH in a reactor varies over the courseof a day. Use least-squares regressionto fit a sinusoidto the following data. Use your fit to determinethe mean, amplitude,and time of maximumpH.
where I, : absolutetemperature(K), and a, b, c, and d are parameters.Employ the fbllowing data and rcgressionto estimate the parameters:
r (K)
Speed,km/h Thinking,m B r o k i n g ,m
Time, mo F OND I Rodiotion, 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,eachof which is a function of its speed.The fbllowing cxperimentaldata was
Assumingeach month is 30 days long, fit a sinusoidto this data. Use the resulting equationto predict the radiatronrn mid-August.
.'.: r"it'liii
Polynomiol Interpolotion
CHAPTEROBJECTIVES The primary objectiveof this chapteris to introduceyou to polynomial interpolation. Specific objectivesand topics coveredare ' ' ' ' ' ' '
Recognizingthat evaluatingpolynonrialcoefficientswith simultaneous equations is an ill-conditionedproblem. Knowing how to evaluatepolynomial coetficientsand interpolatewith 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 . Knowing how to pelfbrm an interpolationwith Newton's polynomial. Knowing how to pertbrm an interpolationwith a Lagrangepolynomial. Knowing how to solve an inverseinterpolationproblem by recastingit as a roots problem. Appreciatingthe dangersof extrapolation. Recognizingthat higher-orderpolynomialscan manif-estlargeoscillations.
YOU'VE GOT A PROBTEM f we want to improve the velocitypredictionfor the free-fallingbungeejumper, we might I expandour model to accountfor other fhctorsbeyondmassand the drag coefTicient.As I I was previouslymentionedin Section2.7,the drag coefficientcan itself be formulatedas a function of other factors such as the areaof the jumper and characteristicssuch as the air's density and viscosity. Air density and viscosity are commonly presentedin tabular form as a function of temperature.For example.Table 15.1is reprintedfrom a popularfluid mechanicstextbook (Whire, 1999). Supposethat you desiredthe densityat a temperaturenot includedin the table.In such a case,you would have to interpolate.That is, you would have to estimatethe value at the
336
POLYNOMIAL INTERPOLATION
TABTEl5.t T,'C /A
0 2A 50 100 150 200 254 300 400 500
Density(p),dynomicviscosily(p), ond kinemotic viscosity(u)os o functiono[ (f) ot I otm os reportedby White (1999). temperoiure 1t, N . s/m2
p,kg/m3
5t x 7l x B Ox 95 x 2 . 1 7x 2 . 3 8x 2 . 5 7x 2 75 X
52 29 2A 09 4.946 0 835 0 746 0 675 06r6 0 525 a.457
/
YJ
lO 5 lO 5 l0 5 lO r lO-5 l0 5 lO-r IU'
x l0 r X ]U
355 X
U'
-
v , m2ls 0.99x l0 r 1 33 x l0-r j l . 5 Ox l 0 l /9 x 105 230xl0j 2 85 x l0-5 3 . 4 5x l 0 - j 4 O Bx l O - j 4 75 x lA': 6 20 x lO-j 777 xla5
desiredtemperaturebasedon the densitiesthat bracketit. The simplestapproachis t0 determine the equationfor the straightlirre connectingthe two adjacentvaluesandusethls equation to estimatethe density at the desiredintennediatetemperature.Althoughsuch linear interpolationis perfectlyadequatein many cases,error can be introducedwhenthe data exhibits significantcurvature.In this chapter,we will explore a numberof different approachesfor obtainingadequateestimatesfor suchsituations.
I5.I
INTRODUCTION TO INTERPOTATION You will frequently have occasionto estimateintermediatevalues betweenprecisedata points. The most common method usedfor this purposeis polynonial interpolation. The generalformula for an (a - l)th-order polynomialcan be written as ,f(x):ar*azxla3x2
+..'+erx"t
(15.1)
For n datapoints,thereis one andonly onepolynomialof order (n - l) that passes through all the points. For example,there is only one straightline (i.e.. a first-orderpolynomial) that connectstwo points(Fig. 15.1a).Similarly,only one parabolaconnectsa setofthree points (Fig. l5.lr). Polltnomialinterpolationconsistsof determiningthe unique@ - l)thorder polynomial that fits n data points. This polynomial then provides a formulato computeintermediatevalues. Before proceeding.we should note that MATLAB reprcsentspolynomialcoefficients in a different mannerthan Eq. (15.1).Ratherthan usingincreasingpowersof .r, it usesdecreasingpowers as in I '+'.. f (x): plx" I p:.t'
1 1 t , ,1 t I p , ,
(15.4
To be consistentwith MATLAB. we will adontthis schemein the followins section.
EXA
15. I INTRODUCTION TO INTERPOLATION
337
FIGUREI5.I Exomplesof nierpolotingpolynomiols:(o) firsl-order (lineor)connecfingtwo poinis, { b ) s e c o r d - o r d e ' { o u o d ' o l o: 6' o o ' o b o l i c )c o n n e c r i n tgL r e ep o i n t s o n d { c )r h i r d - o r d e{ cr u b ' c l conneciinoiour ooints.
l5.l.l
Defermining
Polynomiol Coefficients
A straightforwardway for computingthe coefficientsof Eq. (15.2)is basedon the fact that n datapoints are requiredto determinethe n coefficients.As in the following example,this allows us to generaten linear algebraicequationsthat we can solve simultaneouslyfor the coefficients.
E X A M P L1E5 . . l
D e t e r m i n i n gP 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. Supposethat we want to determinethe coefficientsof the parabola, \ pl,thatpassesthroughthelastthreedensityvaluesfrom l 5T. la: b l e .f (x): pt.t21 p2-+
rr : 300 "f(rr) : 0.6t6 xz:400 f(x):0.525 . r : : 5 0 0 . f( x ) : 0 . 4 5 ' 7 Eachof thesepairscanbe substituted into Eq. (15.2)to yield a systemof threeequations; 0.616: pt(30q2 + p2Q00)+ p3 0.525: p1@00)z + p2(400)+ p3 0.451: pr(500)2-t p2600)-l pt or in matrixform:
3oo rl I p,| [ o.oro I oo.ooo I 1 6 0 . 0 0o 0ot l l n z | : l o . s z s ] 4 I p, 1250.000s00 lJ I
J
10.4s7|
Thus, the problem reducesto solving three simultaneouslinear algebraicequationsfor the three unknown coefficients.A simple MATLAB sessioncan be used to obtain the
338
INTERPOLATION POLYNOMIAL solution: >>
format long - 190000 3tl0 1;160000 404 b = [0.6r6 4.525 0.451 )';
>> A >>
I;250000
500
1l;
>>P=A\b
0.00000115000000 -0.00171s00000000 r.42100ct00000000 Thus, the parabolathat passesexactly throughthe threepoints is f ( . x ): 0 . 0 0 0 0 0 1 1 5 . r 2- 0 . 0 0 1 7 1 5 .+r 1 . 0 2 1 This polynomial then providesa meansto determineintermediatepoints.For example, the value of densityat a temperatureof 350 oC can be calculatedas / ( 3 5 0 ) : 0 . 0 0 0 0 0 1 1 5 ( 3 5 0 )- 2 0 . 0 0 1 7 t 5 ( 3 5 0+) 1 . 0 2 7: 0 . 5 6 1 6 2 5
Although the approachin Example 15.1 provides an easy way to performinterpolation, it has a seriousdeficiency.To understandthis flaw, notice that the coefficientmatrix in Examplel5.l has a decidedstructure.This can be seenclearlyby expressing it ingeneral terms:
l-"i -rr I I lr.'.tr) ll
t'.-l Lxi ,r.r I J
i\l:{ilii}
Coefficient rnatricesof this tbrm arc ref-eflrd to as Vctndennondenntrices. Suchmatrices are very ill-conditioned.That is, their solutionsarevery sensitiveto round-offenon. This can be illustratedby using MATLAB to computethe conditionnumberfor thecoefficient matrixfrom Examole15.1as >> cond (A)
5.8912e+a)0t-, This condition number, which is quite large for a 3 x 3 matrix, implies that about six digits of the solution would be questionable. The ill-conditioning becomes even worse asthe number of simultaneous equations becomes larger. As a consequence, there are altemative approaches that do not nranifbst rhis shortcoming. In this chapter, we will also describe two altematives tlrat are well-suited for computer implementation: the Newton and the Lagrange polynomials. Before doing this, however. we will first briefly review how the coefficients of the interpolating polynomial can be estimated directlv with MAILAB's builrin functions.
I 5.2 NEWTONINTERPOLATING POLYNOMIAL
339
15.1.2 MATTAB Functions: polyf it dlrd potyval Recall from Section 13.4.2,that the polyf it function can be usedto perform polynomial regression.In such applications,the number of data points is greaterthan the number of fit line doesnot necessarily coefficientsbeing estimated.Consequently,the least-squares passthrough any of the points,but ratherfollows the generaltrend of the data. For the casewherethe numberof datapointsequalsthe numberof coefficients,po ly f it performs interpolation.That is, it returnsthe coefficientsof the polynomial that pass directly through the datapoints. For example,it can be usedto determinethe coefficients of the parabolathat passesthroughthe last threedensityvaluesfrom Table 15.1: >>
format
longt
>> r: [300 400 500]; >> density = i0.616 0.525 0.451); >> p = polyfit
(T, density,2
)
O . O O O O O l l 5 O O O O I ] O_ O . O O 1 7 1 5 O O O O O O O OI . 0 2 ' I O O O O O T ] O O O O O We can then use the polyval >> d = polyval
function to perform an interpolation as in
(p, 350)
d,
0.56762500000000 These results agree with those obtainedpreviously in Example l5.l with simultaneous equations.
15.2 NEWTONINTERPOTATING POTYNOMIAT There are a variety of alternativeforms for expressingan interpolatingpolynomial beyond the familiar format of Eq. (15.2). Newton's interpolatingpolynomial is among the most popular and useful forms. Before presentingthe generalequation,we will introducethe first- and second-orderversionsbecauseof their simple visual interpretation. 15.2.1 lineor Interpolotion The simplestform of interpolationis to connecttwo datapoints with a straightline. This technique,called linear interpolation,is depictedgraphicallyin Fig. 15.2. Using similar triansles.
fi(r) - f (rt) ^
4l
f(x)-f(xt) x2-xl
( 15.,1)
which can be rearrangedto yield
fr?):f(x)+
f(xr)-
f(,rr)
x2*xl
(1 s . s )
which is the Newton linear-interpolation formula. The notation /r (-r) designatesthat this is a first-order interpolatingpolynomial. Notice that besidesrepresentingthe slope of the --rr) is a finite-difference line the the term
340
INTERPOLATION POLYNOMIAL
._*\\
f(xz) d
.li(r)
---_-------:-,/ I rii t
,'
,.4
-/
-/
./
"f(,xr)
T5.2 FIGURE used triongles theslmilor indiccrte oreos Theshoded of ineorinierpolofion. C.phicoldepiction i o d e r i v ei h e N e w t o n l l n e o ri n l e r p o l o l i ofno r m u l ol F q { 1 5 . 5 ) ]
approximationof the first derivative{recallEq. (a.20)1.In general,the smallertheinterval betweenthe data points, the better the approximation.This is due to the fact that,asthe interval decreases,a continuousfunction will be better approximatedby a straightline. This characteristicis demonstratedin the following example'
'I5.2 L i n e o rI n t e r p o l o t i o n EXAMPLE Problem Stotement. E,stimatethe naturallogarithmof 2 using linear intelpolation.First, Then, perform the computationby interpolatingbetweenln I :0 and ln 6: 1.791759. the Note that ( 4 I .386294). I ln ln to repeatthe procedure,but use a smallerinterval liom true valueof ln 2 is 0.6931412. Solution. We useEq. ( 15.5)from :r1: 1 . 7 9715 e- 0
.fi (2:o ) + ff
I to rz : 6 to give
(2- 1): 0' 358351e
w h i c h r e p r e s e n t s a n e r r o reo,f: 4 S . 3 V c . U s i n g t h e s m a l l e r i n t e r v a l f r o m x: l I t o ; r : = 4 yields - 0 t.-r86294
lre\ :0+
t) :0.4620e81
POLYNOMIAL I 5.2 NEWTONINTERPOLATING
341
J(.;r) 2
05r
FIGURE I5.3 Two lrneorinterpololions lo esiimoleln 2 Note how the smollerinlervolprovideso better esiimote.
Thus. usirrg the shorter interval reducesthe percentrelative error to €t :33.3Vo. B>
format
longr
>>T=t-40024541;
>> d = 1r.52 1.29 i.2 >>
densitY
o-ns
L a Q fr a n g e
t.091; (T, d, 15 )
i I \
I .22II2B
I5.4
-
41 22222 2
INVERSE INTERPOLATION As the nomenclatureimplies, the .f ('.r)and.t valuesin most interpolationcontextsarethe dependentand independentvariables,respectively.As a consequence,the valuesof the r's are typically unilbrmly spaced.A simple exampleis a table of valuesderivedfor the t u n c t i o nf ( r ) : l/x: x
f@)
2 05
3 0 3333
4 025
5 o2
6 0.1667
7 0 . t4 2 9
Now supposethat you must use the samedata,but you are given a value for ./(r) and must determinethe correspondingvalue of x. For instance,for the dataabove,suppose that you were askedto determinethe value of I that correspondedto l (-r) : 0.3. For thiscase, becausethe lunction is availableand easyto manipulate,the correctanswercan be determ i n e dd i r e c t l )& S- { : 1 1 0 . 3: 3 . 3 3 3 3 . Such a problem is called inverse interpolatiort For a rnore conrplicatedcase,you might be temptedto switchthe /(;r) and.r values[i.e..merelyplot x versus/(x)] anduse an approachlike Newton or Lagrangeinterpolationto determinethe result.Unfortunately, when you reversethe variables,thereis no guaranteethat the valuesalong the new abscissa [the /(.r)'s] will be evenly spaced.In fact. in many cases,the valueswill be "telescoped." That is, they will have the appearanceof a logarithmic scalewith some adjacentpoints bunchedtogetherand othersspreadout widely. For example,for /(r) : l lx theresultis
f (x) x
o t429 7
o 1667 6
02 54
425
03333 32
05
Suchnonunitbrm spacingon the abscissaoften leadsto oscillationsin the resultinginterpolatingpolynomial. This can occur even for lower-orderpolynomials.An alternative strategyis to fit an nth-orderinterpolatingpolynomial,/, (r), to the original data[i.e.,with f (r) versus.rl. In most cases,becausethe.r's are evenly spaced,this polynomialwill not be ill-conditioned.The answerto your problem then amountsto finding the valueof .rthat makesthis polynomial equalto the given /(,r). Thus. the interpolationproblemreduces to
I 5.5 EXTRAPOTATION AND OSCILLATIONS
351
For example,for the problernjust outlined,a simple approachwould be to fit a quadraticpolynomialto thethreepoints:(2, 0.5).(3, 0.3333),and(4, 0.25).The resr.rlt wouldbe / : ( r ) : 0 . 0 4 1 6 6 7 r r- 0 . 3 7 5 r+ 1 . 0 8 3 3 : l The answer to the inverse interpolation problem of finding the .r correspondingto l (.r) : 0.3 would therelbreinvol'r,edeterminingthe root of 0 . 3 : 0 . 0 4 1 6 ' 71 2 - 0 . 3 1 5 x+ 1. 0 8 3 3 3 For this simplecase,the quadraticfbrmulacan be usedto calculate
0.375+
(-0.37s)2- 4Q.041661)0.78333 : 5.704158 3.295842 2(0.041667)
Thus, the secondroot, 3.296,is a good approximationof the true value of 3.333.If additional accuracywere desired,a third- or tburth-orderpolynomial along with one of the root-locationmethodsfrom Chaps.5 or 6 couid be employed.
I5.5
EXTRAPOTATION AND OSCITLATIONS Before leaving this chapter,there are twcl issuesrelatedto polynornialinterpolationthat must be addressed. Theseare extrapolationand oscillations. 15.5.1 Extropolofion Ertapolation is the processof estimatinga value of ./ (,r) that lies outsidethe rangeof the k n o w n b a s ep o i n t s ,r r , - r 2 , . . . . . t , , . A s d e p i c t e di n F i g . 1 5 .1 0 , t h e o p e n - e n d enda t u r eo f
F I G U R IE5 . I O lllustrolion of thepossible divergence of on extropoloted prediction. Theextropolofion isbosed through on fittinq o porobolo thLfirstthreeknownpoinfs .f (x) ,.
I n t e r p o l a t i o n. ' E x t r a p o l a t i o n -
r, Extrapolation I 0 rof1 interpolati tnteroolattno Il .
polynomial
I I I I I I t>
l"r
3s2
POLYNOMIAL INTERPOLATION extrapolationrepresentsa step into the unknown becausethe processextendsthe curve beyondthe known region.As such,the true curve could easilydivergeliom the prediction. Extreme care should, therefore,be exercisedwhenever a case arises where one mu$ extrapolate.
E X A M P L E1 5 . 6
D o n g e r so f E x t r o p o l o l i o n Problem Stqtement. This exampleis patternedafteroneoriginallydevelopedby Forsythe, Malcolm, andMoler.rThe populationin millions of the United Statesfrom 1920to 2000can be tabulatedas Dote )S2O I 930 l94O I 950 I 960 l97O I 980 I 990 2000 Populalion 10646 123.08 13212 15227 tBA67 2A505 22723 24946 28142
Fit a seventh-orderpolynomial to the first 8 points (1920 to 1990).Use it to cornputethe population in 2000 by extrapolationand compareyour prediction with the actualresult. Solution. First,the datacanbe enteredas >> t = L\924:10:19901; >> pop = 1105.45 I23.08 132.I2 L52.27 180.61 205.05 227.23 2 4 a. r 6 l ; The polyf it function can be usedto computethe coefficients >> p - polyfrt
(t, pop,7 )
However,when this is implemented.the following messageis displayed: VJarnlncJ: Polynomial po'nt s or
is
badLy
condil-ioned.
rr2l conter ino
Remove repeated drid scdr jng ds desct ibeo
data in
HELD
POLYFIT.
We can follow MATLAB's suggestionby scalingand centeringthe datavaluesas in >> rs = (r - 1955)/35; Now po11,fit works without an errormessage: r . ' >p = p o l y f i t ( t s , p o p , 1 ) ; We can then use the polynomial coefficientsalong with the Fo1yr731function to predict fhe populationin 2000 as :'> polyval (p, (2000-f955)i 35) 175.0800 which is much lower that the true value of 281.42.Insight into the problem can be gained by generatinga plot of the dataand the polynomial, > > t t = l i n s p a c e ( I 9 2 A , 2 0 0 A ); >> pp = polyval (p, (tt-1955) /35); >,
nloi
(r.{)oD.,o,,tt,pp)
E}
I 5.5 EXTRAPOLATION AND OSCILLATIONS
353
r\
250
200
150
1920 1930 1940 1950 1960 1970 1980 1990 2000 F I G U R EI 5 . I I U s eo t o s e v e ^ t l ' o ' d e ' p o l y ^ o nrioortr o i . eo p r e d i c t i oon[ U . S .p o o u l oo n , " 2 O O O b o s e oo n d o t oi r o ml 9 2 Of h r o u q h 1990.
,,.*1;;?,[i;1]ii*:;:;*::TJ:il:1Jl:J,",'n::ilT::;: polynomialplungesto the erioneouspredictionin 2000. extrapolation,the seventh-order
| 5.5.2 Oscillotions Although "more is better" in many contexts,it is absolutelynot true for polynomial interpolation.Higher-orderpolynomialstend to be very ill-conditioned-that is, they tend to be highly sensitiveto round-off eror. The following exampleillustratesthis point nicely. E X A M P L lE5 . Z
D o n g e r so f H i g h e r - O r d e rP o l y n o m i o Il n t e r p o l o t i o n Problem Stotement. In 1901,Carl Rungepublisheda studyon the dangersof higherorder polynomial interpolation.He looked at the following simple-lookingfunction: I rt l l\t -: - '
l *25x2
(t5.24)
which is now called Runge'sfunction. He took equidistantlyspaceddata points from this function over the interval t-l, ll. He then used interpolatingpolynomials of increasing order and found that ashe took more points,the polynomialsandthe original curve differed considerably.Further,the situationdeterioratedgreatly as the order was increased.Duplicate Runge'sresult by using the polyf it and polyval functionsto fit fourth- and tenthorder polynomials to 5 and 1l equally spacedpoints generatedwith Eq. (15.2$. Create
354
POLYNOMIAL INTERPOLATION
Solution. The five equally spaceddatapointscan be generatedas in >> x
-
linspace(.-I,L,5);
>> Y
=
I./(I+25*x.^2);
Next, a more finally spacedvector of rx valuescan be computedso that we cancreate a snooth plot of the results: >> xx = linspace(-1,1); Recall that l inspace automaticallycreates100 points if the desirednumberof points is not specified.The po ly f r t function can be usedto generatethe coefficientsof thefounhorder polynomial, and the polval function can be usedto generatethe polynomialinterpolation at the finely spacedvaluesof xx: = po11'f it >> 1.4 = polyval >> p
(x,y , 4) ; (p, xx) ;
Finally. we can generatevaluesfor Runge'sfunction itself and plot them alongwiththe polynomial fit and the sampleddata: >> yr - I.i (1+25*xx."2); >> plot
(x,y,'
--'
o',xx,y4,xx,yt:,'
)
As in Fig. 15.12,the polynomialdoesa poorjob of followingRunge'stunction. Continuingwith the analysis.the tenth-orderpolynomial can be generatedandplotted with >>
X
=
>>
y
=
linspace( 1,1,11); I . / (I+25*x. ^2) ;
FIGURE I5.I2 funct]on Cornporison o[ Runges line)wiiho fourth-order polynomiol fitto 5 poinis {doshed sompleo lrom lne iunclron.
/t
r\
It
l! t;
|t ,t
i
r\ t\
r\ i\ ta\
t5.l (
tta
x "f(r) -0.2 -0.4 -1
- 0.5
(a) C al fc
PROBLEMS
355
a
-0.5 -1
-nF
0.5
F I G U R IE5 . I 3 Comporisonof,Runge'sfunctlon{doshedline)with o tenih-order polynomiolfit to I I points somoledfrom lhe lunction
>> p = polyfrt (x,y,10) ; > > y 1 0 = p o l y v a l (p , x x ) ; >> plot(x,y,'o',xx,y10,xx,yr,'
-'
)
As in Fig. 15.13,the fit has gotteneven worse,particularlyat the endsof the interval! Although theremay be certaincontextswherehigher-orderpolynomialsarenecessary, they are usually to be avoided.In most engineeringand scientific contexts,lower-order polynomials of the type describedin this chaptercan be used effectively to capturethe curvins trendsof datawithout sufferins from oscillations.
PROBTEMS l5.l Giventhe data
Giventhe data
2.5 7
3 65
5r o "f(r)
I 475
3 5.25
5 )9.75
6
36
Calculate/i3.4) usingNewton'sinterpolatingpolynomi- Calculate/(4) usingNewton's interpolatingpolynomialsof alsoforder I through3. Choosethe sequenceofthe points order I through 4. Chooseyour basepoints to attain good for your estimatesto attain the best possibleaccuracy. accuracy.What do your results indicate regarding the order - aL^ r ^_-^-_^ -^l.,nnminl ofthe usedto generatethe data in the table?
356
POLYNOMIAL INTERPOLATION
15.3 RepeatProb. 15.2 using the Lagrangepolynomial of best estimate?Employ this best estimateand inverseinten order I through 3. polation to determinethc conespondingtemperature. 15.,1TableP14.5lists valuesfor dissolvedoxygenconcentration in water as a function of temoeratureand chloride T ' K 204 254 300 350 404 450 concentratlon. Densiry \ 7 0 8 I 3 6 7 I r 3 9 0 . 9 6 1 0 8 5 4 0 / 5 9 (a) Use quadraticand cubic interpolationto determinethe kglm3 oxygen concentrationfor I : 12 'C and c:10 glL. (tl) Use linear interpolationto determinethe oxygen con15.9 Ohm's law statesthat the voltage drop V acrossan centrationfor I : 12 "C and c : 15 glL. ideal resistoris linearly proportionalto the currcnt I flowing (c) Repeat(b) but use quadraticinterpolation. : I R, where R is the resistance. 15.5 Employ inverseinterpolationusing a cubic interpolat- throughthe resisteras in V However, real resistors may not always obey Ohm's law. ing polynomial and bisectionto determinethe value of x that you performed Suppose that some very prcciseexperiments correspondsto /(-t) : 1.6 for the following tabulateddata: to measurethe voltagedrop and correspondingcurent for a rcsistor.The following resultssuggesta curvilinearrelation2,4( ship rather than the straightline representedby Ohm'slaw: .r12 67
f(x) 36
lB
12
09
072
I 5
A . 5 4t 2 9
*"; t
v 15.6 Employ inverseinterpolationto determinethe value of r that coresponds to l (-t) : 0.93 for the following tabulated data:
r01 J@) 0
0.s
23 0B
09
45 o 94lt76
0 9 6 t5 3 8
-2 637
-t 96 5
-05 -245
05 205
t 965
637
To quantify this relationship,a curve must be fit to thedata. Becauseof mcasurcmcnterror, regressionwould typically be the preferred method of curvc litting fbr analyzing such experimentaldata.However,the smoothnessof the relationship, as well as the precisionof the experimentalmethods, suggeststhat interpolationmight be appropriate.Usea fifthorder interpolatingpolynornialto fit the dataand computeV fori : 0.10. 15.10 Besselfunctionsoften arisein advancedengineering analysessuch as the study of electric fields. Here aresome selcctcdvalueslbr the zero-orderBesselfunction ofthe first kind
Note that the values in the table were gencratedwith the function f (x) : x2I 0 + x2). (a) Determinethe correctvalue analytically. (b) Use quadraticinterpolationand the quadraticformula to determinethe value numerically. (c) Use cubic interpolation and bisection to determinethc value numerically. x IB 20 2.2 24 26 15.7 Use the portion of the given steam table for super- , t ' ( r ) 0 . 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. I l8 with linear interpolation, (b) the same coffesponding entropy using qua- Estimate"/1(2. I ) using third-, and fourth-orderinterpolating dratic interpolation,and (c) the volume correspondingto an polynomials.Determinethe percentrelative enor for each casebasedon the true value, which can be determinedwith entropyof 6.45 using inverseinterpolation. MATLAB's builrin tunctionbessel j. 15.11 RepeatExample 15.6but using tirst-, second-,third-, u, m3/kg o to377 0 il 144 4 1 2 5 4 7 and fourth-ordcr interpolating polynomials to predictthe 64147 s, kJl(kg K) 6 5453 6 7664 populationin 2000basedon the most recentdata.Thatis,for the linear predictionusethe datafrom I 980 and I 990,for the quadraticpredictionusethe datafrom I 970, I 980, and I 990, l5.lt The following data for the densityof nitrogengas ver- and so on. Which approachyields the best result? sus temperaturecomesfrom a table that was measuredwith 15.12 The saturationconcentrationof dissolvedoxygenin high precision.Use firsl throughfifth-order polynomialsto water as a function of temperatureand chloride concentraestimatethe densityat a temperatureof 330 K. What is your tion is listedin TablePl5.l2. Use intemolationto estimate
TAB
T, "
o
5 t0 t5 20 25 30
the r
lO o
15. pola
SOIV
the < the r for l
r5.l
in s pres
T, "1 a, Jl
Dett
r5.l
lar a the r
(
Bec has
(
whe dim
PRoBLEMS
I I I f
nfU Pt5. l2 _
I
',
I r: I l; I I I
I
in woteros o functionof iemperoture Dissolved oxygenconcentrotion {"C)ond chloride c o n c e n t r o t i o( g n/ L ) . DissolvedOxygen (mglt) for Temperoture("C)ond Concentrotionof Chloride (g/Ll
I lt,"a
I
ssl
s=OglL
c=l}g/L
146
l?9
t? 3 '3A,
lJt 3?9
theclissolve,l oxygcnlevclfor ?': 18'C with chloride:
l--
I Iosrr-. I
'33u 93! 114
7 46 685
Bt6 756
25 ;0
c=2Og/L
inter15.13 Forthc datain TableP15.12,usepolynomial
673 62A
n
m-----lT
I EU:-',;-*':::.iliH;i:J^"Ti::#T?.,*"H"t; /"/ / i esrimare rhedi:solvcd o\yscn conce-nrrarirrn _ _+ / I ff;:ul.ro fo" |rheeaser,lhel.cch|or.idecollccntrationisequa|tol0g/L.UseJ,#|
rolunre ol'u :upcrlicrte(lsteanris listctl lS.U Thc.specil'ic
TI Forexampre ata I ;;:'.',ll:l*,iffiiil:temrcraturcs F r c u RPEr s . l s I /oo 72.) z*a 7(;a 7Ba I t "t I
r 6613 I u,lt3/lb- a aa-7 a l2t14 a l4060 a li5o9 c-1 ltabulatecI,aportionol'whichisgiveninTablePl5.15.Ifa: 1.6 andh : 14,usea third-orderinterpolatingpolynomiat to I ^ I at : 750 'F compufeo- at it depthI (i rl beiou' thc corner of a rectangu.r I .D.t:tj'Tltt \tre\s,"- under thc c.rncr of a rectangu15'15The lar footing that is sub.iectto a total ]oad of 100 t (metric .venl11t J l I r r a r e a : t r b j c c t e d | o l u n i | t r t' -. t: t' t' l r l"a' 't "J t r"| ' i n 1 c n s i t . r r 7 i r e t ti rr rernsb) r. E r p r e s s ) o u r . t n s $ c r i n ( o n n e s p e r s q u a r e n l c t c r . N ( ) t c L,'.-',-.'.-'-;:'"*"'."" o l B o u s : i n c s q 'esq' u a t i o n : I t h er o l u t i o n inat 4 ts cquatto Inc tolloper area. I
q I z*nJ.2 +-,7+ t r] + n2+2 J ; rI t r r : + n : i l + m 2 n 2 m 2 + n 2 + l L
-si.(#.+#)l Bccau.sc this cquationi.sincorrvenient fo solye nranually,, it hasbeenreformulatedas o- -- t1.f-\nt,n) where.f(rrr. n) is calledthe influenccvalut-.and lr and rr are dirnensionless ratios,with nt : al: and rr : b/; and a and b are defined in Fis. Pl5.l-5. The influcncevalue is then
TABIEPl5.l5
0l o2 03 a4 05 06 a7 OB
tt = 1.2
n=1.4
n=1.6
c 42a26 4.45733 0 08323 0 t063t c 12626 0 14309 0 I 5203 0 I 6843
0 a3aa7 0 a5Bq4 0 . 0 8 5 6r 0 t0941 0 13003 o . 1 4479 016199 0 rz3B9
0 03058 a a59q4 a aB709 0l]t35 a.13241 a.l5a27 0 1 6 5 51 4.17739
358
POLYNOMIAL INTERPOLATION
15.16 You measurethe voltagedrop Vacrossa resistorfor a number of ditl'erentvaluesof current L The resultsare
425 -o.45
475
1. 2 , 5 a70
t5 ]BB
20 60
TABTEP I 5. | 9
"y=O y =2 t=4 Use first- through fourth-orderpolynomial interpolationto t = 6 estimatethe voltagedrop tbr i: 1.15.Interpretyour results. J = 8
Temperotures ("C)ot voriouspoints on o squoreheotedplote.
x=O
x=2
x=4
x=6
10000 8 50 0 2000 s5.00 40.00
90 00 64.49 48.90 38.78 35.00
B0 00 53 . 5 0 3 84 3 30 39 3000
70.04 6000 48 I5 5 00 0 3 5 0 3 4000 2 7 . 0 7 3000 2 5 . 0 0 2000
x=8
| 5. I 7 The currentin a wire is measuredwith greatprecision as a function of time: l-i. 19 Temperaturesare measuredat various pointsona at 0 . 5 0 0 0 heated plate (Table P15.19). Estimate the temperature 0 0 0 0 0 ( a ) . r : 4 , ) ' : 3 . 2 , a n d( b ) r : 4 . 3 .y : 2 ; 7 . 15.20 Use the portion of the given stearntablefor superheated HrO at 200 MPa to (a) tind the conesponding D e t e r m i n ei a l t : 0 . 2 3 . entropys for a specificvolurne u of 0.108 mi/kg with linear l5.llJ The accelerationdue to gravity at an altitudey above interpolation,(b) find the samecorrespondingentropyusrng the surfaceof the earth is given by quadraticinterpolation,and (c) find the volumeconespond. ir-rgto an entropyof 6.6 using inverseinterpolation.
t t
a 0
01250 624
02500 775
03250 4.85
0 30,000 60,000 90 000 120,000 !, m g, m/s2 9 8100 9 7487 9.6879 9 6278 9 568'2 cornputeg ar-y: -55,000 m.
u (m3lkg) s (kJlke.K)
0 14377 6.4147
a.| 144 6.5453
0.r25; 6.7664
..:r,f i,. i'
'...i.i,,,, . ",,*i l'l .
.
Splinesond rrecewrse Interpolotion
CHAPTEROBJECTIVES The primary objectiveof this chapteris to introduceyou to splines.Specificobjectives and topics coveredare . . o . . o '
I6.I
Understandingthat splinesminimize oscillationsby fitting lower-order polynomialsto datain a piecewisefashion. Knowing how to developcode to perform a table lookup. Recognizingwhy cubic polynomialsare preferableto quadraticand higher-order splines. Understandingthe conditionsthat underliea cubic splinefit. Understandingthe differencesbetweennatural,clamped,and not-a-knotend conditions. Knowing how to fit a splineto datawith MATLAB's builrin functions. Understandinghow mr.rltidimensional interpolationis implementedwith MATLAB.
INTRODUCTION TO SPLINEs ln Chap. 15 (n - l)th-order polynomialswere used to interpolatebetweenn data points. For example, for eight points, we can derive a perfect seventh-orderpolynomial. This curve would captureall the meanderings(at leastup to and including seventhderivatives) suggestedby the points. However,thereare caseswhere thesefunctionscan lead to erroneous results becauseof round-off error and oscillations.An alternativeapproachis to apply lower-orderpolynomialsin a piecewisefashionto subsetsof datapoints. Such connectingpolynomialsare calledsplinefunctions. For example, third-order curves employed to connect each pair of data points are calledcubic spl.ines.Thesefunctionscan be constructedso that the connectionsbetween
360
INTERPOLATION AND PIECEWISE SPLINES
FIGUREI6.I interpoloting A visuolrepresentotion of o situotionwhere splinesore superiorto higher-order polynomiols.The funciionto be fit undergoeson obruptincreoseol -r : O. Porls(o) through(c) i n d i c o t et h o tt h e o b r u p tc h o n g ei n d u c e so s c i l l o t i o ni sn i n t e r p o l o i i npgo l y n o m l o l sl n. c o n t r o s t , connections,o lineorsp ine {d) provideso much more becouseit is lirnitedto strcigl-Lt-line occeolobJecoproximotron.
adjacentcubic equationsare visually smooth.On the surface,it would seemthat thethirdYou order approximationof the splineswould be inferior to the seventh-orderexpression. might wonder why a splinewould ever be preferable. Figure 16.1 illustratesa situationwhere a spline performs betterthan a higher-order polynomial.This is the casewhere a function is generallysmoothbut undergoesan abrupt changesomewherealong the region of interest.The stepincreasedepictedin Fig. 16.1is an extremeexampleof such a changeand servesto illustratethe point. Figure 16.la throughc illustrateshow higher-orderpolynomialstendto swingthrough wild oscillations in the vicinity of an abrupt change.In contrast, the spline also connects
1 6 . 2 L I N E AS RP L I N E S
36r
F I G U R EI 6 . 2 t e c h n i q uoef l s i n go s p l i n el o o r o w. -' n[ io' o hc l r u e .l l r o l g - o s e r i eosf p o n i s \ o t i c e T[eorclting h o w o l t e e n dp o i n l sl [ e s p l i n se t r o i g h - eonusi . i s< o le d o ' " o ' u r o l s p i n e
minimum. As such,the splineusuallyprovidesa superiorapproximationof the behaviorof functionsthat have local, abruptchanges. The conceptof the splineoriginatedfrom the drafting techniqueof using a thin, flexible strip (calleda spline) to draw smoothcurvesthrougha setof points.The processis depicted in Fig. I 6.2 for a seriesof five pins (datapoints).In this technique,the drafterplaces paperover a woodenboard and hammersnails or pins into the paper(and board) at the location of the datapoints.A smoothcubic curve resultsfrom interweavingthe strip between the pins. Hence,the name "cubic spline" hasbeenadoptedfor polynomialsof this type. In this chapter,simple linear functionswill first be usedto introducesomebasic conceptsandissuesassociated with splineinterpolation.Then we derivean algorithmfor fitting quadraticsplinesto data.This is followed by materialon the cubic spline,which is the most common and useful version in engineeringand science.Finally, we describeMATLAB's capabilitiesfor piecewiseinterpolationincluding its ability to generatesplines.
r6.2 T I N E A RS P T IN E S The notationusedfbr splinesis displayedin Fig. 16.3.For n datapoints (l : I, 2, . . . , n), there are n - I intervals. Each interval i has its own spline function, s;(x). For linear splines,eachfunction is merely the straightline connectingthe two points at eachend of the interval, which is formulatedas (16.r) si (r) : ar I bi(x - ;ri) where ai is the intercept,which is definedas ai:
Ji
and b; is the slopeof the straightline connectingthe points; Ji+r - Ji , ui--
(16.2)
(16.3)
362
SPTINES AND PIECEWISE INTERPOLATION
J(.tt
attttrr_
F I G U R EI 6 . 3 l \ o ' o ro n u t e dl o d e ' ' , es o l ' ^ e sNotice thot therecre n - I intervolsond n dcio points
where li is shorthandfor l(r;). SubstitutingEqs. (16.1)and (16.2)into Eq. (16.3)gives f..,
Ji (.r ) :
.l ;
_
-r L)----l' rirl-ri
f.
(.r - .ti )
(r6.4)
Theseequationscan be usedto evaluatethe function at any point betweenx1 ard.l,, is by first locating the interval within which the point lies. Then the appropriateequation usedto determinethe function valuewithin the interval.Inspectionof Eq. (16.4)indicates that the linear spline amountsto using Newton'sfirst-orderpolynomial[Eq. (15.5)]to interpolatewithin eachinterval.
E X A M P LI 6E. I
F i r s t - O r d eSr p l i n e s Problem Stotement. Fit the data in Table 16.I with first-ordersulines.Evaluatethe functionotx:5. TABIE l6. l
Dototo be lit with splinefunc]ions.
ix,f, I 2 3 A
30 45 7.4 90
25 I0 2.5 0.5
Solution. The data can be substitutedinto Eq. (16.4) to generatethe linearspline functions.For exarnple,for the secondintervalfrom x : 4.5 to x :7, the functionis sr(x - ) : 1.0 +
2 5-1.0 --::-------:t.r - 4.51 1.0 - 4..5
I6.2 TINEAR SPTINES
363
F I G U R ET 6 . 4 S p l i n ef i t so f o s e lo f f o u rp o i n i s{.o )L i n e osrp l i n e(,b )q u o d r o f iscp l i n eo, n d ( c )c u b i cs p l i n ew, i t h o c u b i ci n f e r p o l o l i pn og l y n o m i o l s op l o t t e d .
The equationsfor the other intervalscan be computed,and the resultingfirst-ordersplines a r ep l o t t e di n F i g . l 6 . 4 c tT h e v a l u ea t x : 5 i s 1 . 3 . 2 . 5- t . 0 -4.5): s-r ( x ) : 1 . 0 + - ( 5 1 . 0- 4 . . 5
l.-l
Visual inspectionof Fig. 16.4aindicatesthat the primary disadvantageof first-order splinesis that they are not smooth.In essence,at the data points where two splinesmeet (called a knot), the slopechangesabruptly.In formal terms,the first derivativeof the function is discontinuousat thesepoints. This deficiency is overcomeby using higher-order polynomial splinesthat ensuresmoothnessat the knots by equatingderivativesat these points,as will be discussedsubsequently. Beforedoing that,the following sectionprovides an applicationwhere linear splinesare useful.
364
ET E R P O L A T I O N S P L I N EASN D P I E C E W I SI N
16.2.1 Toble lookup A table lookup is a common task that is frequentlyencounteredin engineeringandscience computer applications.It is useful tbr performing repeatedinterpolationsfiorrr a tableof independentand dependentvariables.For example,supposethat you would like to setup an M-file that would use linear interpolationto determineair density at a particulartemperaturebasedon the datafrom Table 1-5.1.One way to do this would be to passtlreM-file the temperatureat which you want the interpolationto be performedalong with thetwoadjoining values.A more generalapproachwould be to passin vectorscontainingall thedata and have the M-file detenninethe bracket.This is called a table lookup. variThus, the M-file would perform two tasks.First, it would searchthe independent able vector to find the intervirl containingthe unknorvn.Then it would performthelineat interpolationusingone of the techniques describedin this chapteror in Chap.15. For ordereddata, there are two simple ways to find the interval. The first is calleda sequentialsearch.As the name implies.this methodinvolves comparingthe desiredvalue with each elementof the vector in seqnenceuntil the interval is located.For datain ascendingorder,this can be doneby testingwhetherthe unknownis lessthanthe valuebeing lf so, we know that the unknown falls betweenthis value and the previousone assessed. that we examined.lf not, we move to the next value and repeatthe comparison.Hereisa this objective: simpleM-file that accomplishes functior-i n if
yi
= Tablelook(; x(.n) €irror: ( 'InterI,olation outside
\',
xx)
=
zx
range')
end % binary
t;
(1)
rvh i le r.f
search iu
l;
rL
iU
.=
tL/L:'tY((tt rf
< x(iM) rT = rI{:
l,
break, u)
en-l
z);
/
xx
t:lse a:Ilal end % Linear yi 1i
inLerpolati.on ,, i l,
t'1"
/ (x(iL+1)-x(iL)
)*(xx
-
:.(rL)
) ;
The approachis akin to the bisectionmethodfbr root location.Justas in bisection,the index at the rridpoint r1.4is computedas the averageof the filst or "lower" index i L = 1 and the last or "upper" index iu : n. The unknownxx is thencomparedwith the valueof x at the midpoint r (iM) to assesswhetherit is in the lower half of the anay or in the upper half. Dependingon where it lies, either the lower or upperindex is redefinedas being the middle index.The processis repeateduntil the differencebetweenthe upperand the lower index is lessthan or equal to zero.At this point, the lower index lies at the lowel bound of the interval containingx:.:,the loop terminates,ilnd the linear interpolationis pertbrmed. Here is a MATLAB sessionillustratinghow the binary searchfunction can be applied to calculatethe air densityat 350 "C basedon the datafi'ornTable l-5.1.The sequential searchwould be similar. > - r = t 4 0 0 2 0 5 0 1 0 0 1 5 0 2 0 0 2 5 0 3 0 0 . 10 0 5 0 o l ; > > d e n s i t v = f 1 . 5 : 1 . 2 9 I . 2 I . Q 9 . 9 4 6 . ' , : r 3 1. ,' l 4 6 . 6 1 1 . 6 1 6 >> TableLoo]> yr >>
plot
= L./ (1+25*xx.^2); (x,y , 'o ' , xx, yy, xx, yr,
'
-
)
As in Fig. 16.6,the not-a-knotspline doesa nice job of following Runge'sfunction without exhibiting wild oscillationsbetweenthe points. (b) The clampedcondition can be implementedby creatinga new vector yc that has the desiredfirst derivativesas its first and last elements.The new vector can then be used to
I6.6 FIGURE Comporisonof Runge'sfunciion(doshedlrne)with o 9-poinlnoto-knotspllnefit generotedwilh MATLAB(solidline).
376
ET E R P O L A T I O N S P L I N EASN D P I E C E W I SI N
0.5 F I G U R EI 6 . 7 (doshedline)wiih o 9 pointclompedend splinefitgeneroted functlon Comporison of Runge's withMATLAB of I ond -4 orespecifred ct thJleltond isolid-ine)Notethotfirstderivoiives r i g h b' o u n d o . i e s ' e s p e c r ; , e l ,
generateand plot the splinefit: >> yc = [1 y 4]; >> yyc = spline(x,yc,xx); >> plot (x,y, 'o' ,xx /yljc,xx,yr,'--')
As in Fig. 16.7,the clampedsplinenow exhibitssomeoscillationsbecauseof the artificial slopesthat we have imposedat the boundaries.In other examples,where we haveknowledgeof the true first derivatives,the clampedsplinetendsto improve the fit.
16.5.2
MATTAB Function: interpl
The built-in function interpl providesa handy meansto implementa numberof different typesof piecewiseone-dimensionalinterpolation.It has the generalsyntax yi
=
interL, 1 (x,
1:, xi,
'rneLhad
t
w h e r e x a n d y - v e c t o r s c o n l a i n i n g v a l u e s t h a t a r e t o b e i n t e r p o l a t ye id: a , ,vectorc0nthe interpolation points taining results of the as evaluatedat the in the vector xj, and 'methocJ' : the desiredmethod.The variousmethodsare .
.
'nearest '-nearest nei-ehbor interpolation. This methodsetsthe valueof an interpolatedpoint to the value of the nearestexisting data point. Thus, the interpolation looks like a seriesof plateaus,which can be thoughtof as zero-orderpolynomials. ' I inear ' -linear interpolation.This methodusesstraightlines to connectthepoints.
EXA
I6.5 PIECEWISE INTERPOLATION IN MATTAB
377
.
'spL.ine'-piecewise cubic spline interpolation.This is identicalto the spline
.
iTItt"Ji
una ' cubic '-piecewise cubicHermiteinrerpolarion.
If the 'method' argunrentis omittecl.the defaultis linearinterpolation. The pchip option (short for "piecewisecubic llermite lnterpolation")merits more discussion.As with cubic splines,pchip usescubic polynomialsto connectdatapoints with continuousfirst derivatives.However,it ditTersfrorn cubic splinesin that the second derivativesarenot necessarilycontinuous.Further the first derivativesat the knots will not be the sarneas for cubic splines.Rather,they are expresslychosenso that the interpolation is "shape preserving."That is, the interpolatedvaluesdo not tend to overshootthe data pointsas can sometimeshappenwith cubic splines. Therefore,therearetrade-offsbetweenthe spline and the pchip options.The results of using spline will generallyappearsmootherbecausethe humaneye can detectdiscontinuitiesin the secondderivative.In addition.it will be moreaccurateif the dataare values of a smoothfunction. On the other hand.pchip has no overshootsand lessoscillation if the dataare not smooth.Thesetrade-ofI\.as well asthoseinvolving the otheroptions.are exploredin the following example. EXAMPLE 16.5
Trode-OffsUsing in; e,''1r l Problem Stotement. You perfom.ra test drive on an automobilewhere you alternately acceleratethe automobileand then hold it at a steadyvelocity. Note that you never decelerate during the experiment.The time seriesof spot nleasurementsof 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"rnctionto fit this datawith (a) linear interpolation,(b) nearest neighbor.(c) cubic splinewith not-a-knotend conditions.and (d) piecewisecubic Hermite interpolation. Solution. (a) The datacan be entered,fit with linearinterpolation, and plottedwith the following commands: >> r - [0 20 40 56 68 B0 34 95 t04 110]; >> v = l0 2A 20 38 B0 80 100 10A \25 7251; = finspace ( O, 110 ) ; >> tt = incerpl(t,v,r-t); >> vl >> plot (t,..',' o', tt,vl)
The results(Fig. 16.8c)are not smooth,but do not exhibit any overshoot. (b) The commandsto implementand plot the nearestneighborinterpolationare >> vn - interpl (t,v,tt,'nearest' >> nl ol- (t-,./,'
o' . tt,\-n)
);
378
ET E R P O L A T I O N S P L I N EASN D P I E C E W I SI N
140
't40
tzu
120
100
100
80
80
60
60
40
40
20
20 bu
IUU
120
{a} linear
140
140
120
120
100
100
80
80
60
bU
40
40
20
20 60 ( c )s p l i n e
100
'120
100 60 80 40 (b)nearestneighbor
120
100
120
FIGUREI6.8 Use of severoloptionsof the rnterpt toron ouiomobile.
funciionto performpiecewisepolynomiol nterpolciionon o velocilyiime serles
As in Fig. 16.8b.the resultslook like a seriesof plateaus.This option is neithera smooth nor an accuratedepictionof the underlyingprocess. (c) The commandsto implementthe cubic splineare >> vs >> plot
=
(t, v, tt, interpl ( t , - " - ,' o ' , t t , v s )
'spline'
) ;
Theseresults(Fig. 16.8c)are quite smooth.However,severeovershootoccursat several locations.This makes it appearthat the automobiledeceleratedseveraltimes duringthe
I 6.6 MULTIDIMENSIONAL INTERPOLATION
379
(d) The commandsto implementthe piecewisecubic Hermite interpolationare >>
vh
>> plot
=
interpl(t,\/,tt,'pchip'); (t,V,'o', tt,vh)
For this case,the results(Fig. 16.8d)arephysicallyrealistic.Becauseofits shape-preserving nature,the velocitiesincreasernclnotonicallyand never exhibit deceleration.Although the resultis not as smoothas for the cubic splines,continuityof the f irst derivativesat the knots makesthe transitionsbetweenpointsmore gradualand hencemore realistic.
I 6.6
INTERPOTATION MUTTIDIMENSIONAL The interpolationn.rethods for one-dimensionalproblemscan be extendedto multidimensionerlinterpolation. In this section,we will describethe simplestcaseof two-dimensional interpolationin Cartesiancoordinates.In addition,we will describeMATLAB's capabilities for rnultidimensionalinterpolation.
16.6.1 BilineorInterpolotion 'lnao-dintensional interpolctriondeals with determiningintermediatevalues for functions of two variablesz: .f (xi, ]r). As depictedin Fig. 16.9,we have valuesat four points: yz).Wewanttointerpolatebetweenthesepoints "f(xr,.r'r),,f(rz,.r'r),,f(rr,,r':),andl(.t:, F I G U R EI 6 . 9 G ' o p r ; c oole p c l i o n or {v o - d r r r e r s i oonr lor ln e o ' i n e p o l o ' i o r w l e e o n . l e ' n e o o e , o l- e" {dl i c i r c e )i s e s t i m c t ebdo s e do n f o u rg i v e nv o l u e {so p e nc i r c l e s )
f(xz, yt)
380
S P L I N EASN D P I E C E W I SI N ET E R P O L A T I O N
EX
FIGURE I6.IO Two dimensionolbilineorinlerpolctioncon be implemenledby f irstopplyinq one dir-nensioncl ' e o ^ l ^ ro c l o l i ^ no r o n q' n e . r o i m e n s , o r o d e - ^ r, r n e , o L e co - . r , h e - e , i o l e s c o r t ^ e nb e u s e d t o i n e c r l y i n t e r p o l o t e o l o n g l h e . r ' d i m e n s i o n t o y i e l d t hr ee sf iunl ot oi t - r , , , r , ,
to estimatethe value at an intermediatepoint / (-ri. _vi ) . If we use a linear function,there. sult is a plane connectingthe points as in Fig. 16.9.Suchfunctionsare calledbilinear. A simple approachfor developingthe bilinear tunction is depictedin Fig. 16.10.First, we can hold the v value fixed and apply one-dimensional lineu interpolation in the.rdirection.Usingthe Lagrangeform, the resultat (r;, .r1) is
. f( r i ,r ' r ): 3 f Jt
.fZ
, - r rJ, r )+ S f x2
.rt
t r : ,- v r )
(16.2s)
andat (,r;,rrr)is / ( x i , . r , : :)} . r r y- -rV.2, r : ) It
+ " t --{. ,!r l l ' ( , r r , , v : ) ,t2
16.10)
Thesepointscan then be usedto linearly interpolatealong theI'dimension to yieldthefinat result: -
lr
-
\''
. l { , r ; . . r ' ,:) - / ( x- ; . . r ' 1 )t }:
\'; -
\'r
) - -,f ' l.ri.J:) ,'t): .\'l
116.311
A single equation can be developedby substitutingEqs. (16.29) and (16.30) into Eq. (16.31) to give -.v: r'' -'t2 't '{i-xt '\'i .ft.r,.1-)f l , r , . r r ) f(x.. v,) rl - .\'2 )'r - ")Z X2 - Xt .t,r _ -r': (1634 -{i - rr ),i - lr ^, )'r ... , -ri -t: ]i .rl - rt ): -.r'r 12 -.{l )z _ ).r
I 6.6 MULTIDIMENSIONAL INTERPOLATION E X A M P L1E6 . 6
381
Bilineorlnterooloiion Problem Stotement. Supposeyou have measuredtemperaturesat a number of coordinateson the surfaceof a rectangularheatedplate:
T ( 2 , 1 :)6 0 T(2,6): 55
: 5l .5 T(:9,1) T(9,6):10
Usebilinearinterpolation to estimate thetemperature at.rr: 5.25and1',- 4.8. Solution. Substituting thesevaluesintoEq.(16.32)gives : "/ ( ) . j ) . _ { . 6 )
5.25-9 4 - o. 8 u- 6 2-9 l-6
5.25-2 4 - )./8. -- \6 + 9-2 t-6 s-}s-24R-l
5.25-94.8-l ss + -;; + -;; u-,
u-,
zo : 6r.2113
| 6.6.2 Multidimensionol Interpolotion in MATLAB piecewiseinterpolaMATLAB has two builrin functionsfor two- and three-dimensional tion: interp2 and interp3. As you might expectfrom their names,thesefunctionsoperate in a similar fashionto interpl (Section 16.5.2).For example,a simplerepresentation o f t h es y n t a xo f i n e o 2 i s zi
=
Lt\r-e(p2 lx,
y,
z,
xi,
yi,
'mechod'
)
where x and y - matrices containing the coordinatesof the points at which the values in the matrix z are given, zi : d matrix containingthe resultsof the interpolationas evaluated at the points in the matricesxr and yr, and method: the desiredmethod.Note that the methodsareidenticalto thoseusedby in*'erpi; that is, linear, nearesf , spline, a n dc u b i c . As with interpl, if the,nethod argumentis omitted,the defaultis linearinterpolation. For example,interp2 can be usedto make the sameevaluationas in Example 16.6as >> X= >> y= >>
z-
>>
lnl-
2 9); 1 6l; 6A 51 .5;55 701; erp2(x,y,2,5.25,4.8)
5r .2r43
382
AND PIECEWISE INTERPOLATION SPLINES
. ., H E A T R A N S F E R Bockground. Lakes in the temperatezone can becomethermally stratified duringthe summer.As depictedin Fig. 16.11,warm, buoyantwater near the surfaceoverliescolder, denserbottom water. Such stratification effectively divides the lake vertically into two layers: the epilitnnion and the hypolimnion, separatedby a plane called the thermocline. Thermal stratification has great significancefor environmentalengineersand scientists studying such systems.In particular,the thermoclinegreatly diminishesmixing betweenthe two layers.As a result, decompositionof organic matter can lead to severedepletionof oxygen in the isolated bottom waters. The location of the thermoclinecan be defined as the inflection point of the temperatur€d e p t hc u r v e - t h a t i s . t h e p o i n t a r w h i c h d 2 T / d z 2 : 0 . 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. The temperaturegradient is important in its own right becauseit can be usedin coniunction with Fourier's law to determinethe heatflux acrossthe thermocline: dT
J:-DpC,
(r6.33)
az
where "f : heat flux [call(cm' . s)], cv: an eddy diffusion coefficient (cm'ls), p : density (31 g/cm3),and C: specificheat[: I call(g . C)]. In this case study, natural cubic splines are employed to determinethe thermocline depth and temperaturegradientfor PlatteLake, Michigan (Table 16.3).The latter is also usedto determinethe heatflux for the casewherea = 0.01 cm2/s.
I6.I I FIGURE Temperoiure versus forPlotte depthduring summer Loke, Michigon
r ('c)
Epilimnion Thermocline
Hypolimnion
TABTE16.3 2,6 T,"C
Temperoture versusdepthduringsummerfor PlotteLoke,Michigon.
0 228
2.3 22.8
4.9 228
9 I 2A6
137 t3I
1 83 11.7
22.9 tl.t
27.2 ilt
] 6.2 CASESTUDY
383
" . . , . . : c. o n t i n u e d Solution. As just described,we want to usenaturalsplineend conditionsto performthis analysis.Unfortunately,becauseit usesnot-a-knotend conditions,the built-in MATLAB spl ine function doesnot meetour needs.Further,the sp1 ine functiondoesnot returnthe first and secondderivatives we require for our analysis. However, it is not difficult to develop our own M-file to implementa natural spline and retum the derivatives.Sucha codeis shownin Fig. 16.12.After somepreliminaryerror trapping,we setup and solveEq. (16.27)for the second-ordercoefficients(c). Notice how
FfGUREr6.12 MJile fo deiermineintermediolevoluesond de,"ivofives with o noiurolspline.Note fhot fhe oif f {unclionemployedfor errortroppingis describedin Seclion 19.7.1. -
l \ . / \ ,; . 1 - ;, , t : . ,=:
r r a r - : i p l i n e 1r , y , s;,1 ire
r,'dlii lrl
xx)
r " : i! : ] , r j i f l - - r e n t : . r L 1 ( , r l
spi.irrr,'(x,y,-xril:
uses
. l n . l t s - r - r r ; lc , r b i c
: a)n la.) f r f r d ua t t r:s
j.y, t-he varues oi the ::,r- yector The vecLcf xx.
in
l,
,
rl
j ndepenLlenL
;i
ricpenrlelt
der,vaLiv-:r
::=i:,1: ri aiirr i-iat
ir
tf
c l e ! ) € : n , - . l r t l : ' , ' - -i .- t h L e : :
;{){ r::
ai
anr,ly rrr:st be same L.r!{t-h' ) ; end x n o t s t : - i c " L y . r s c e n c r r : r r ') , e n d
II Il'
l;,-
ar, xr
a:
ii'e:
furr:Lion
slle.ttf ie :
. , ' ar i ; : b r e s . , ' a: : i a h . e s
€ f ilo l.ri e.f \.a l ues
iirsi
r
l...r.
/
i,i.-':';i r:e,J'ialt:es i.t
:pLtne
rtnderlving
-
'.fEL
- 1t(r, I 2 * r l r{ x , -.hi-,.
*
i
Lal-
h.
I
i); L) r
i
hlr:,
i));
i')
fci(j,
il
+
1l-rili
?solve
I')
1,
i,
x,
i-ol c i:oetficier,ts : " S O l . - j e J - c , r -a , b . - . n d
d
i
-
1,
v));
r:c-el-fi,:i+nj-s
ll;
(r lr
+ l, +-lj
t,
x, t,r) - h(x, i) cr{:L)) ,/ 3 / lilx,
/ i i);
*
(2 * c(:)
= c1i
-
1))
(continued)
384
SPLINES AND PIECEWISE INTERPOLATION
coniinued I - 1:m %perforn interpolaLions at desired values - Splinelnrerp(x, lyt'(r),dy{r),d.2(i)l n, a, b, c, d, xx(:.)); end enC frrrlctfon hti = 11*, i) hl'r = x(r + i) - x(i); for
end iun -l n i.C'J fdri, , x , , 1) - y(j)) fcld - (y(i) r' (..r(i) x(j)); enC function lyyy, dyy, d2yl -gp] inelnterp f nr
i
1 ,h
i
(x.
n,
a,
b,
c,
d,
xr )
1
, 0 . 0 0 U 0 0 1 & x r < _ , -x ( i i xi >= x(ri) + i) + 0.000001 yyy-a (1i) +b ( ii) * (xi-x (ri) ) +c (ii) * (x!,x (ir ) ) ^2+o( ii) . . . *(xi x(ri))^3; d y i , = 5 1i I ) + . 1* c ( i , i ) * ( x l - x ( i i ) ) + I * r l ( i i ) * ( x i - x { , i ) ) ^ 2 ; d2y-2*c (ii ) +5*d (ii) * (z.i-x (ii ) ) ;
if
br eak end end end
FIGUREt6.t2 lContinued)
we use two subfunctions, h and fd, to compute the required finite differences.Once Eq. (16.27) is set up, we solve for the c's with back division. A loop is thenemployed t0 generatethe other coefficients(a, b, and d). At this point, we have all we need to generate intermediate values with the cubic equation: f (x) : ai * bt(x - rr) + ct(x * x)2 * di@ - xi)3 We can also determine the lirst and second derivatives by differentiating this equation twice to give '(x) : bi * ci(x - x;) * 3diQ - x;)z .f"{x1 :2ci + 6di, - xi) f
As in Fig. 16.12, these equations can then be implemented in another subfunctign, splineTnterp, to determinethe values and the derivativesat the desiredintermediate values. Here is a script file that usesthe naLspl ine function to generatethe splineandcreats plots of the results: z = i0 2.3 4.9 9.1 13.7 IB.3 22.t) 27.21; r=122.8 22.8 22.8 20.6 13.9 17.7 I1 .l 17.L1; ?i
-
l inqnaeo(z(1\.zi
I6.7 CASESTUDY
"
continued -
natsptine \2,T, zzJ ; (T, z,' a',TT, zz) I ),Pioi ' l, ieqrei.i.ll'ciaca',' l " rcvc!-s /.9r-!v /
,pfor(ai,zzt c 1 z ') ,'reverse'),grid ,!rlot (d't2, zz)
As in Fig. \6.13, the thermoc\ine appearsto be \ocated at a depth of about 11.5 m. We can use root \oeation (zero second denrative) or optimization methods (minimum fust denvative) to refine this estimate.The result is that the thermocline is located at 11.35 m where the gradientis - 1.61'C/m.
FIGURE T6.I3 Plotsof {o) temperoture, depth (ml generotedwith {b} g'odient, ond (c} secondderivotiveuers,,s the cubic sprineprogro.n.The rhermocline islocoted ot the in[lecrionpoint o[ ihe temperoruredeplh curve. ( a )T
(cl dzTtdz2
\bl dr/dz
_L______
10
10
- - - -t-
- - - - - -
10
F_ I
\ t5
15
----F------
-r------
I I I I
t5
20
I
25
30L
10
20
1n
rI I L
I
20
zv
25
25
30 1 -2
30 -0.5
0.5
AND PIECEWISE SPTINES INTERPOLATION
386
cond cubi(
continued The gradientcan be usedto computethe heat flux acrossthe thermoclinewith Eq.(16.33): J:
8 6 ' 4 o- :0t s3 ' 9 . n 2c' aa-l -0.0rt*' * r4 * r-:"1^* (- t6rI) * .^l=- * g..L cmr m / l 0 o c m d s \
The foregoinganalysisdemonstrates how splineinterpolationcanbe usedfor engineeringandscientificproblemsolving.However,it alsois anexampleof numericaldifferfrom differentareascanbeused entiation.As such,it illustrateshow numericalapproaches in tandemfor problemsolving.We will be describingthetopicof numericaldifferentiation in detailin Chap.19,
x
f (x) x f (x't
In ea whic
f
16.5
depi x f (x\
PROBTEMS 16.l Given the data
xl2 f{x1
I
5
5 I
25 7
Fit this data with (a) a cubic spline with natural end conditions, (b) a cubic spline with not-a-knotend conditions,and (c) piecewisecubic Hermite interpolation. 16.2 A reactol is thermaily stratified as in the tbllowing table:
Depih, m Temperoture,'C
0 05 7A 7A
I 55
t5 22
2 25 13 l0
3 l0
to Use a clampedcubic spline tit with zero end derivatives determinethe thermoclinedepth.If k:0.01 cal/ (s . cm . "C) compute the tlux acrossthis interface. 16,3 The following is the built-in humps function that MATLAB uses to demonstrate some of its numerical clpabilities: -0
(x-0.3)2*0.01 (x-0.9)2*0.04
The humpsfunction exhibitsboth flat and steepregionsovet a relatively shon.r range. Here are some valuesthathave beengeneratedat intervalsof 0. I over the rangefrom.r = 0 to l:
r ftt) x
02 03 4.4 05 O 0l 5 . 1 7 6 1 5 4 7 t 45 887 96 500 47 448 19400 06
07
08
0.9
I
Based on these temperatures,the tank can be idealized as f $) ]t 692 12382 t7.846 2t 703 r6.000 two zones separatedby a strong temperaturegradient or thennocliue. The depth of the thermoclinecan be defined as the inflection point of the temperature-depth curve-that Fit this datawith a (a) cubic splinewith not-a-knotendconis, the point at which d2Tldzz:0. At this depth,the heat ditions and (b) piecewisecubic Hermite interpolation. In flux from the surface to the bottom layer can be computed both cases,create a plot comparing the fit with the exact with Fourier's law: humpsfunction. 16.4 Develop a plot of a cubic sptine fit of the following . . d r J:-K-data with (a) naturalend conditionsand (b) not-a-knotend az
Fir tl ditio tions case 16.6 natu cate t6.7 orde 15.4 x
t2
(a) F ditio (b) F slopt tiatir 16.8 and Thes evalr
PROBLEMS
387
conditions. In addition, develop a plot using (c) piecewise cubic Hermiteinterpolation.
mathematicaltables.For example,
lB
.r /(r)
0 0
ro0 0 82436
2aa I 00000
/(x)
600 0 4060l
800 0 t99r5
r000 009r58
Ineach case,colnpareyour plot with the following equation which wasusedto generatethe dala: I"{ , { ) :
x -e
200
16.5Thefollowing data is sarnpledfrom the step function depicted in Fig. 16.I :
/(r)
0
-02 0
02
0.o
I
0lrt
Fitthisdatawith a (a) cubic spline with not-a-knotend con(b) cubic spline with zero-slopeclampedend condiditions, and(c) piecewisecubic Hermite interpolation.[n each trons, createa plot comparing the fit with the step function. case, 16,6Developan M-file to cor.nputea cubic spline 11twith natural end conditions.Test your code by using it to dupliExampleI 6.3. cate 16.7The fbllowing data was generated with the liftlrpolynomial:.f(x): 0.018515- 0.411.14 order * 3.9125xr15.456i+ 27.069x - l4.l: r135679 1000
/(r)
2 172
22 0.556
24 A 5202
26 A 47OB
EstimateJl2.l). (a) using an interpolating polynomialand (b) using cubic splines.Note that the true value is 0.5683. 16.9 The following data defines the sea-levelconcentration of dissolved oxygen for fresh water as a function of tempelature:
T,'C 0 B t6 24 32 40 o,mg/L 1462) IlB43 IBZ0 B4lB 73A5 64)3
-r!r t')rr
-t
2 0 5767
0 5 8t 5 400 "Ir(J) 0 73576
4 22O
5 430
4.912
9.)24
(a)Fit this data with a cubic splinewith not-a-knotend conditions. Createa plot comparing the fit with the tunction. (b)Repeat(a) but useclampedend conditionswherethe end slopes are set at the exact values as determined by differenliating the function. 16,8Besselfunctions often arise in advancedengineering andscientificanalysessuch as the study of electric fields. These functionsare usually not amenableto straightforward evaluation and, therelbre, are olien compiled in standald
Use MATLAB to fit the data with (a) piecewise linear interpolation,(b) a fifth-order polynomial,and (c) a spline.Display the resultsgraphicallyanduseeachapproachto estilnate o(27). Note that the exactresultis 7.986 mg/L. 16.10 (a) Use MATLAB to fit a cubic splineto the followins data:
x
0 2A
v
2 2A
4 12
7 7
l0 6
l2 6
Determinethe value of 1,at "t : 1.5.(b) Repeat(a). but with zero first derivativesat the end knots. l6.ll Runge'sfunctionis writtenas I "
|*25rt
Generatefive equidistantlyspacedvalues of this function over the interval: [, 1]. Fit this data with (a) a fourth-order polynomial, (b) a linear spline, and (c) a cubic spline. Presentyour resultsgraphically. 16.12 Use MAILAB to generateeight points lrom the function / (/) : stn- t from r : 0 to 2n. Fit this data using (a) cubic spline with not-a-knotend conditions,(b) cubic splinewith derivative end conditionsequalto the exactvaluescalculatedwith differentiation,and (c) piecewisecubic hermite interpolation. Developplots of eachfit aswell asplots of the absoluteerror (8, : approximatioll- true) for each.
.: '.
d
t
b
,'i:';,
'ii,;*"'!.1
I
l;'#i$k*tffi#*ri
lntegrotion o nd Diff;rentiotion 5.1 ovERvtEw In high school or during your first year of college, you were introduced to differential and integral calculus.There you learnedtechniquesto obtain analytical er exact derivatives and integrals. Mathematically,the derivuriverepresentsthe rate o[ changeof a clependentvariable willr respectto an independent variable.For example.if we are given a functionXt) that specifiesan object'spositionas a functionof time.differentiationprovidesa meansto determineils velocity.as in: d u(l): -y(l) dt' As in Fig. PT5.1n,the derivativecan be visualizedas the slopeof a function. Infegrationis the inverse of differentiation.Juqt as differentiationusesdifferencesto quantify an instantaneous process,integrationinvolvessumminginstantaneousinformation to give a total result over an interval.Thus. iI-we are providedwith velocity as a function of time, integrationcan be usedto determine the distancelraveled:
: fr'u(t)dr
v(/)
As in Fig. PTs.lb, for functions lying above the abscissa,the integral can be visualized as the areaunder the curve of u(r) from 0 to r. Consequently,just as a clerivativecan be thoughtof as a slope,an integralcan be envisagedas lsummation. Becauseoflhe close relationshipbetweendifferentiation and integmtion, we have opted to devorethis part of lhe book to both processes.Among other
389
390
PART 5 INTEGRATION AND DIFFERENTIATION
0 ) 400
200
0
0 tal
FIGURE PT5.I ond {b) integrotion The confrostbehr;een(o) differentiotion
things,this will provide the opporlunityto highlight their similaritiesand differencesfrom a numericalperspective.ln addition,the materialwill haverelevanceto the next paft of the book where we will cover differentialequations. Although differentiationis taughtbeforeintegrationin calculus.we reversetheir order in the following clrapters.We do this for severalreasons.First, we havealreadyintroduced you to the basicsof numericaldifferenltiationin Chap.4. Second.in part becauseit is much less sensitiveto roundoff errors, integrationrepresentsa more highly developedareaof numericalmethods.Finally, althoughnumericaldifferentiationis not as widely employed, it does have great significancefor the solution of differentialequations.Hence,it makes senseto cover it as the last topic prior to describingdifferentialequationsin Part Six.
5.2
PARTORGANIZATION Chapter 17 is devoted to the most common approachesfor numerical integration-the Newton-Cotes formulas. Theserelationshipsarebasedon replacinga complicatedfunction tabulated or data with a simple polynomial that is easy to integrate.Three of the most widely usedNewton-Cotesformulasarediscussedin detail: the trapezoidalrule, Simpson's 1l3 rule, andSimpson's318rule. All theseformulasare designedfor caseswherethe data to be integratedis evenly spaced.In addition, we also include a discussionof numerical
ORGANIZATION 5.2 PART
39r
integrationof unequally spaceddata. This is a very important topic becausemany realworld applicationsdeal with datathat is in this forrn. All the above materialrelatesto closedintegratiott,where tlre function valuesat the ends of the limits of integrationare known. At the end of Chap. 17, we presentopen integrationfonnulas, wherethe integrationlimits extendbeyondthe rangeof the known data. Although they are not commonly usedfor definite integration.open integrationformulas are presentedhere becausethey are utilized in the solution of ordinary differential equations in Part Six. The formulationscoveredin Chap. 17 can be employedto analyzeboth tabulateddata and equations. Chapter18 dealswith two techniquesthat areexpresslydesignedto integrate equationsand functions:Rombergintegrationand Gaassquadrature.Computeralgorithms are providedfor both of thesemethods.In addition,adaptiveintegrationis discussed. ln Chap. 19, we presentadditionalintbrn.rationon numeric:aldi.fferentiation to supplement the introductorymaterialfrom Chap.4. Topicsincludehigh-accuracl, finite-dffirence formulas, Richardson extrapokttion, and the differentiation of unequally spaceddata. The effect of errorson both numericaldiff'erentiationand intesrationis also discussed.
:' ,
' ;,0;' :
NumericolIntegrotion Formulos
CHAPTER OBJECTIVES The primary objectiveof this chapteris to introduceyou to numericalintegration. Specific objectivesand topicscoveredare .
'
'
. . .
Recognizingthat Newton-Cotesintegrationformulas arebasedon the strategy of replacinga complicatedfunction or tabulateddatawith a polynomial that is easy to integrate. Knowing how to implementthe following singleapplicationNewton-Cotes formulas: Trapezoidalrule Simpson's1/3 rule Simpson's3/8 rule Knowing how to implementthe following compositeNewton-Cotesformulas: Trapezoidal rule Simpson's113 rule Recognizingthat even-segment-odd-point formulaslike Simpson's1/3 rule achievehigher than expectedaccuracy. Knowing how to usethe trapezoidalrule to integrateunequallyspaceddata. Understandingthe differencebetweenopen and closedintegrationformulas.
YOU'VE GOT A PROBTEM h ftf l\
ecall that the velocity of a free-falling bungeejumper as a function of time canbe computed as
( ,f8a'),) uu\: .14 ,unn V ,'.i \''l n
rrrrr
I Z.I INTRODUCTION ANDBACKGROUND
393
Supposethat we would like to know the vertical distancez the jumper has fallen after a certaintime r. This distancecan be evaluatedby integration: fl
:(r):
(1j.2)
/ t'(ndr .to
S u b s t i t u t i nE g q . 1 1 7 . 1i)n t o E q . ( 1 7 . 2 )g i v e s
z(t):I,',8""n(,l*,)0,
( I 7.-r.1
Thr.rs,integrationprovidesthe meansto deterrrrinethe distancefiom the velocity.Calculus can be usedto solveEq. (17.3)for
,E')l
f '"[.."'n(
(t7.4)
Although a closedform solutioncan be developedfor this case,thereare other functions that cannot be integratedanalytically.Furlher. supposethat there was some way to measurethejumper's velocityat varioustimesduringthe lall. Thesevelocitiesalongwith their associatedtimes could be assembledas a table of discretevalues.ln this situation,it would also be possibleto integratethe discretedatato determinethe distance.In both these instances, numericalintegrationmethodsareavailirbleto obtainsolutions.Chaptersl7 and l8 will introducevou to someof thesemethods.
I7.I
INTRODUCTION AND BACKGROUND 17.l.l
Whof ls Integrotion?
According to the dictionarydefinition, to integratemeans"to bring together,as parts,into a whole;to unite;to indicatethe total amount.. . ." Mathemafically, definiteinteglationis representedby
t:
a^ l,,of {.r)
(r 7 . 5 )
which standsfor the integralof the function f (x) with respectto the independentvariable .r. evaluatedbetweenthe limits x : a to ,r : b. As suggestedby the dictionarydefinition,the "meaning"of Eq. (17.5) is the roral value. or summation,of /("r) dx overthe rangex : a to b. In fact, the symbol / is actually a stylizedcapital S that is intendedto signify the closeconnectionbetweenintegration and summation. Figure 17.1 representsa graphicalmanifestationof the concept.For functions lying above the x axis, the integral expressedby Eq. (17.5) correspondsto the area under the curve of l (x) betweenr : a andb. Numerical integrationis sometimesreferredto as quadrature.This is an archaicterrn that originally meantthe constructionof a squarehaving the sameareaas somecurvilinear figure. Today, the term quadratureis generallytaken to be synonymouswith numerical definite integratior.r.
394
NUMERICAL INTEGRATION FORMULAS
I
.{ F I G U R EI 7 . I Grophicolrepresentotion of theintegroo1 l ( x ) b e t w e e nt h e l i m i t sr : equivoenl lo theoreounderlhecurve.
t t l o D T h e i n t e g r oi ls
nonl
17.1.2 Integrotionin Engineeringond Science Integrationhas so many engineeringand scientific applicationsthat you were requiredto take integral calculusin your first year at college.Many specificexamplesof suchapplicationscould be given in all fields of engineeringand science.A number of examples reIate directly to the idea of the integralas the areaunder a curve. Figure 17.2depictsa few caseswhere integrationis usedtbr this purpose. Other common applicationsrelateto the analogybetweenintegrationand summation. For example,a common applicationis to determinethe meanof a continuousfunction.Recall that the meanof discreteof n discretedatapointscan be calculatedby tEq. (i3.2)1.
i,,
14s2n- 5r t17.6) n where -yiare individual measurements. The determinationof the meanof discretepointsis depictedin Fig. 17.3a. In contrast,supposethat ,r is a continuousfunction of an independentvariable.r,as depictedinFig. 17.3b.For this case,thereare an infinite numberof valuesbetweena and b. Just as Eq. (17.6) can be appliedto detenninethe mean of the discretereadings, you might also be interestedin computingthe mean or averageof the continuousfunction .l : / (x) for the interval fron a to b. Integrationis usedfor this purpose,as specifiedby Mean:
l'! f t.utax ,"-
FIG Exo nee kno
(11.1)
D-A
This formula has hundredsof engineeringand scientific applications.For example,it is used to calculatethe center of gravity of irregular objectsin mechanicaland civil engineeringand to determinethe root-mean-square currentin electricalengineering.
I Z.I INTRODUCTION ANDBACKGROUND
395
i _l -l
(b)
(c)
I7.2 TIGURE (o)A surveyor of how integrotion is usedto evoluole oreosin engineering Exomples cnd scientific opplicotions. might to knowtheoreoof o fied boundedbv o meonderinq need ond hryoroods.{b)A hvdroloqist miqhtneedto streom k n o w i h e c r o s s s e col iroenooo fo r i v e ri c )A s t r u c t u reonl g i ' , e e r m i gnhete d i o d e t e r m i n e t h ef in, ce ei d J e t o o nonuniform wind blowingogo nsfthesideof c skyscroper.
4 (a)
r : "f(-r)
(b)
FIGUREI7.3 An illustrotion of themeonfor(o)discreie ond(b)continuous dotc
396
INTEGRATION NUMERICAL FORMUTAS
Integralsare also employedby engineersand scientiststo evaluatethe totalamount0r quantity ofa given physicalvariable.The integralmay be evaluatedover a line, anNea,sr a volume.For example.the total massof chemicalcontainedin a reactoris girenasth product of the concentrationof chemicaland the reactorvolume, or Mass : concrentration x volume where concentrationhas ur.ritsof mass per volume. However, supposethat concentration varies from location to location within the reactor.In this case,it is necessaryto sumthe productsof local concentrationsc; and correspondingelementalvolumes A Vi:
: Mass I.,on whererr is the numberof discretevolumes.For the continuouscase,wherec(x, y, t) isa known function andx, y, and : are independentvariablesdesignatingpositionin Cartesian coordinates,integrationcan be usedfor the samepurpose: Mass:
III,,-
t , .z ) d r d v d z
ttt
Mirss: lllrtvtdV JJJ v
which is referredto as a volume integrctL.Notice the strong analogybetweensummation and integration. Similarexamplescould be givenin otherfieldsof engineering and science. Forexample, the total rate of energy transferacrossa plane where the flux (in caloriesper square centimeterper second)is a lunctionof positionis givenby ??
Flux:llnu*ae " "r.
which is referredto as an areal integral. whereA : areo. Thesearejust a few of the applicationsof integrationthat you might taceregukuly in the pursuit of your profession.When the functionsto be analyzedare simple,you will normally chooseto evaluatethem analytically.However, it is often difficult or impossible whenthe functionis complicated, as is typicallythe casein morerealisticexamples. Inaddition, the underlyingfunction is often unknownand definedonly by measurement atdisvalues crete points. For both thesecases,you must have the ability to obtain approximate for integralsusing numericaltechniquesas describednext.
17.2
NEWTON-COTES FORMULAS The Newton-Cotes.formulasare the most common numerical integrationschemes, They are basedon the strategyof replacingn complicatedfunction or tabuiateddatawitha polynomial that is easyto integrate: 7b
7b
I:lf(r\dr=1f..(,r\tl.x
FORMULAS I 2.2 NEWTON-COTES
397
where ll, (,r) : a polynomial of'the form a,, rtr"I la,tx" .fr(..r):a0+alx +"'+
(17.9)
wheren is the order of the polynomial.For example,inFig. 17.4a, a first-orderpolynomial (a straightline) is usedas an approximation.In Fig. 17.4b,a parabolais employedfor the samepurpose. The integralcan also be approximatedusing a seriesof polynomialsappliedpiecewise to the function or data over segmentsof constantlength. For example,in Fig. 17.5,three
FIGURE I7.4 ineond(b)o porobolo bythecreounder(o)c stroight Theopproximction of on integrol "f(;r)
.f (tt)
FIGURE I7.5 three byfheoreounder stroightline segmenis Theopproximotion of on integro l(r)
398
FORMULAS NUMERICAL INTEGRATION
FIGUREI7.6 Thedifference behween ondib)openinlegrotion formulos {o)closed
straight-linesegmerltsare usedto approximatethe integral.Higher-orderpolynomialscan be utilized for the samepurpose. Closedand openforms of tlre Newton-Cotesformulas al'eavailable.Theclosedlorns are those where the data points at the beginning and end of the limits of integration ac known (Fig. 17.6a).The openfornrs have integrationlimits that extendbeyondtherangc m of the data (Fig. 17.6b).This chapteremphasizesthe closedforms. However,material open Newton-Cotesformulas is briefly introducedin Section17.7.
RUIE 17.3 THETRAPEZOIDAT The trapezoidalrul.eis the first of the Newton-Cotesclosedintegrationformulas. It con+ spondsto the casewherethe polynomialin Eq. (17.8)is first-order:
t:
I .f (b) - .f (a) (.r, , ")]d, D-0
l""lt^ 1-
T h e r e s u l to f t h e i n t e g r a t i oins l:(b
kl) + .[(b) - r r ) f' 2
(17.10
which is calledthe trapezoidalrule. Geometrically,the trapezoidalrule is equivalentto approximatingthe areaof fu trapezoidunderthe straightline connecting I O1 andJ @) inFig. l7 .7. Recallfromgeor etry that the formula for computingthe areaof a trapezoidis the heighttimestheaverage of the bases.In onrcase,the conceptis the sarriebut the trapezoidis on its side.Therefog the integralestimatecan be representedas / - width x ,verageheight
EXAM
17.3 THETRAPEZOIDAL RULE
399
FIGURE I7.7 Grophicol depicrion of thetropezoidol rule
I : (b - rr) x averageheight
(r7.r3)
where, fbr the trapezoidalrule, the averageheight is the averageof the function valuesat the end points,or l.f (o) + J @)112. All the Newton-cotes closed lormulas can be expressed in the generalfbrmat of Eq' ( 17'13)' That is, they difl'er only with respectto the formulationof the averageheight.
| 7.3.1 Error of the TropezoidolRule when we employ the integral under a straight-rine segmentto approximatethe integral undera curve,we obviouslycan incur .oo, that rnay be substantial (Fig. l 7.g). An esti_ mafe forthe local truncationenorof a "n singleapplicationof the trapezoidalrule is I
E,:--;f"tE)tl,
-ul.
t!
(17.11)
wheref lies somewherein the intervalfrom a to b. Equation(:i.14)indicatesthat if the tunction being integratedis linear,the trapezoidalrule wiil be exactbecausethe secondderivative of a straightline is zero. otherwise. for functions with second-and higher-order derivatives(i.e., with curvaturc),someerror can occur. 'lZ.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. Use Eq. (Ij.lD ro numerically integrate l ( r) : 0.2 _t 25x _ 200x2* 615x3_ 900ra f 40015 from a : 0 to D : 0'8. Note that the exactvalue of the integralcan be determinedanalytically to be 1.640533.
400
FORMULAS NUMERICAL INTEGRATION
lnteqralestimate
0.8 F I G U R EI 7 . 8 ruleto opproximote Grcphicoldepiction of theuseof o single-opplicotion of thetropezoidol i h e i n t e q rool l ( . r ) - - A . 2 + 2 5 x - 2 o a ^ - r * 6 7 5 r r - 9 O O - r + * 4 O O r ' t r o m ; t - : 0 t o 0 . 8 .
S o l u t i o n . T h e f u n c t i o nv a l u e sl ( 0 ) : 0 . 2 E q .( l 7 . t I ) t o y i e l d 1:{0.8-n,
a n d . f ( 0 . 8 ) : 0 . 2 3 2 c a nb e s u b s t i t u t e d i n b
0 . 1+ 0 . 2 3 1 :0.17:8 ,
which represents an effor of E, : 1.640533- 0.1128: 1.461133,whichconespondsh a percentrelatil,eerror of st :89.5c/c. The reasonfor this largeerror is evidentfron|b g r a p h i c adl e p i c t i o n i n F i g .l T . 8 . N o t i c e t h a t t h e a r e a u n d e r t h e s t r a i g h t l i n e n e g l e c t s a s i g nificant portion of the integrallying abovethe line. hr actual situations,we would have no for-eknowledge of the true value.Therefoq serord an approxirnateerror estimateis required.To obtain this estimate,the function's twicc derivativeover the interval can be computedby differentiatingthe originalfunction to give f" (x) : -400 + 4,050.t- 10.80012t 8,000x3 The averagevalueof the secorrdderivativecan be computedas lEq. (17.7)l
'' '^'f
/,.\
_
/1'* t-+oo * 4.050x - 10,800:rr* 8,00013)r/:r
0.8-o
which can be substituted into Eq. (17.14)to yield I E,,: - |(-60)(0.8)3 - 2.56 | ./,
RULE 17.3 THETRAPEZOIDAT
401
doesexist, which is of the sameorderof magrritudeand sign as the trueerror.A discrepancy' however,becauseof the fact that tbr an interval of this size,the averagesecondderivative is not necessarilyan accurateapproximationof l"(6). Thus, we denotethat the error is by using the notation E,,, ratherthan exactby using E,. approxin.rate
17.3.2 The CompositeTrqpezoidolRule One way to improve the accuracyof the trapezoidalrule is to divide the integrationinterval liom a to b into a numberof segmentserndapply the ntethodto eachsegment(Fig. 17.9). The areasof individual segmentscan then be addedto yield the integralfbr the entire inor muhiple-application,integration terval. Tlre resulting equationsare called compo,site, .fitrmulas. Figure 17.9 shows the generalfbrmat and nomenclaturewe will use to characterize compositeintegrals.Therearen * I equallyspacedbasepoints(ro,,rr, x2,..., -r,,).Consequently,there are ,l segmentsof equalwidth: la-
b-a
(l7.l-s)
-
t7
If a and b are designatedas ,rs and 1,,. respectively,the total integral can be representedas
r : L''.f(x)ru +1,,,' f ( t ) d x + . . ' +l . ' ' f , r r u , F I G U R EI 7 . 9 trcoezoidcrl rue Comoosite
, n :
b-ct ---il-
402
FORMULAS NUMERICAL INTEGRATION
Substitutingthe trapezoidalrule for eachintegralyields
,
, I(rn\* /trr) 22
-
, " [ ( r r ) . 1/ ( x : ]
t t -
or, grouplng terms: nl
,T
nt
I : : l / ' ( x o+ ) 2) .rl -L
i-l
I
I
I ( r i ) l - J ( x , , )|I J
or, usingEq. ( 17.l5) to expressF.q.(17.17) in the generalform of Eq. ( 17.l3): n-l
f (rn) r 2 - / r\ r-\ ' - I '/ t . 1 ,) r
r
r ( . r / i)
j-1
t:(b-a) \.....--
2rt
widlh
Avcragc hcight
Becausethe summationof the coefficientsof /(:r) in the numeratordividedby 2il is equal to to I , the averageheightrepresentsa weightedaverageof the function values.According Eq. ( 17.l8), the interior points are given twice the weight of the two end points/(.ro) and .f (x,). An error fbr the compositetrapezoidalrule can be obtainedby summingtheindividual errorsfor eachsegmentto give ,' lD
-
.1 tll'
E,: _,:;:: tln'
il -
) f- /,,re,
{r7re)
where /" (6i) t, ,n" ,".ond derivativeat a point {; locatedin segmenti. This resultcanbe simplified by estimatingthe mean or averagevalue of the secondderivativefor theentire interval as t1
\-
1'
f ' l r \l \. i ' , r
J
n
ThereforeL, .l'" (€,) = n f " and Eq. (.11.19) can be rewritten as ,l tD -
.1
ot'
-.
L , , - - - I
| 1,n'
Note Thus, if the number of segmentsis doubled,the truncationerror will be quarlered. t h a t E q .( 1 1. 2 1 )i s a n a p p r o x i m a t e e r r o r b e c a u s et hoef a p p r o x i m a t e n a t u r eEoqf . ( 1 7 , 2 0 ) . E X A M P L EI 2 . 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 Problem Stotement. Use the two-segmenttrapezoidalrule to estimatethe integralof -t * f (x) : 0.2 * 25x 200x2 675r.3 900x4 * 400x5 from a : 0 to D : 0.8. Employ F,q.(17.21) to estimatethe error.Recallthat theexactvalue of the intesralis 1.640533.
.I2.3
403
RULE THETRAPEZOIDAL
Solution. Forn : 2(h :0.4): /(0) : 0.2 1:0.8
f ( 0 . 4 ): 2 . 4 5 6
, f ( 0 . 8 ): 0 . 2 3 2
0 . 2 + 2 ( 2 . 4 s+60) . 2 3 2 4
:1.0688
E , : 1 . 6 4 0 5 3 3- 1 . 0 6 8:80 . 5 1 1 7 3
€t :34.97c
0.gr [ ,"n : - - 1 - ( - 6 0 ) : 9 . 6 4 t2(2\t where -60 is the averagesecondderivativedeterminedpreviouslyin Example 17.1.
rl o d
The resultsof the previousexample,along with three-through ten-segmentapplications of the trapezoidalrule, are surnmarizedin Table l7.l. Notice how the error decreases as the numberof segmentsincreases. However,alsonoticethat the rateof decreaseis gradual. This is becausethe error is inverselyrelatedto the squareof r [Eq. (1,1.21)).Therefore, doubling the number of segmentsquartersthe error. In subsequentsectionswe develop higher-orderfbrmulasthat iue more accurateand that convergemorequickly on the true integralas the segmentsareincreased.However,belbreinvestigatingtheseformulas,we will first discusshow MATLAB can be usedto implementthe trapezoidalrule.
| 7.3.3
MATTAB M-file: trap
A simple algorithm to implement the compositetrapezoidalrule can be written as in Fig. 17.l 0. The function to be integratedis passedinto the M-file along with the limits of integrationand the numberof segments.A loop is then employedto generatethe integral f o l l o w i n gE q . ( 1 7 . 1 8 ) .
TABLE| 7. I
hopezoidolruleto Resulis for thecomposite estimotethe integrolof /(.r'l :0.2 + 25.\200-t2+ 675x3- 9O0r++ 4OO,rs from - r : O t o 0 . 8 .T h ee x o c tv o l u ei s 1 . 6 4 0 5 3 3 . e, ("/"1
2 3 4 5 6 7 B I t0
0.4 a 2667 0.2 0 t6 0 .r 3 3 3 0l143 0t 0.0889 008
06BB 3695 484B 5399 5703 5B87 6008 609 I 6t50
349 t6.5 95 6l 4.3 32 2.4 1.9 t6
404
NUMERICAL INTEGRATION FORMULAS
function I = trap(func,a,b,n,varargin) % trap: composi.te tt:apezoidal rule quadraLure 'Z I - trap(f unc,a,b,n,pi,p2,... ): % composite trapezoidal rule % input: func = name of function ? Lo be integrated Z a, b = inteqration limits ? n - number of seg,ments (defaulL = 100) pl ,p2, . .. - additional parameters used by func ? Z output: I=integralestimate % if nargin > q u a d ( @ m y h u m p sO
11,0.3, 0.9, 6)
29.85812133214492 Notice that because we used a larger tolerance, the result is now only accurate to five significant digits. However, although it would not be apparent liorn a single application, fewer function evaluations were made and. hence, the computation executes f'aster.
Bockground. Becauseit resultsin efficient energytransmission,the currentin anAC circuit is often in the form of a sine wave: i : ipeatsin(arr) where i : the current (A : c/s), lo.ur: the peak current (A), ar : the angularfrequency (radians/s)and / : time (s). The angularfrequencyis relatedto the period r(s) by a =zrlT.
I8.5 CASESTUDY
441
continued The power generatedis relatedto the magnitude of the current. Integration can be used to determine the averagecurrent over one cycle: , :
I fr. i.",r. lpearsln(tr;/)dt - -!:= (- cos(2n) + cos(Q)):0 T Ju
Despite the fact that the averageis zero, such a cuffent is capable of generating power. Therefore, an alternative to the averagecuffent must be derived. To do this, electrical engineersand scientistsdetermine the root mean squarecurrent i*, (A), which is calculatedas
il"ssin?(att)dt
ipea.k
/;
(r8.2s)
Thus, as the nameimplies, the rms current is the squareroot of the mean of the squared current' Because1/ \/2 : 0.70707, l'.., is equal to about j}vo of the peak current for our assumed sinusoidal wave form. This quantity has meaning becauseit is directly related to the averagepower absorbed by an element in an AC circuit. To understandthis, 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.26)
whereP: the power (W: J/s),and y: voltage(V: J/C).For a resistor,Ohm,slqw states that the voltage is directly proportional to the currenr: V:iR
(18.27)
whereR : the resistance(Q : V/A: P:izR
J . s/Cz).SubstitutingEq (18.21)into (18.26)gives (18.28)
The averagepowercanbe determined by integrating Eq. (lg.2g) overa period with the result: r
p E -_ ;. 2 mcr\
Thus, the AC circuit generatesthe equivalent power as a DC circuit with a consrant current of 1*r. Now, although the simple sinusoid is widely employed, it is by no means the only waveform that is used.For some of theseforms, such as triangular or squzuewaves, the l*" can be evaluated analytically with closed-form integration. However, some wavefbrms must be analyzedwith numerical integration methods. In this case study, we will calculate the root-mean-squarecurrent of a non-sinusoidal wave form. We will use both the Newton-Cotes formulas from Chap. 17 as well as the -__--^^^L^^ )^^^;l^^A
i-
t}|ic
nhanfar
442
N U M E R I C AI L N T E G R A T I OONF F U N C T I O N S
continued Theintesralthatmustbe evaluated is
Solution.
,k,:
Io''
dr eoe-tsin2nt)2
(18.29)
For comparative purposes,the exact value of this integral to fifteen signilicant digitsis 15.41260804810169. Integral estimatesfor various applications of the trapezoidal rule and Simpson's1/3 rule are listed in Table 18.2.Notice that Simpson'srule is more accuratethan the trapezoidal rule. The value for the integralto sevensignificantdigits is obtainedusing a 128-segment trapezoidalrule or a 32-segmentSimpson'srule. The M-file we developed in Fig. 18.2 can be used to evaluate the integralwith Rombergintegration: >> format longi (10*exp(-t) >> i2-@(t)
.*sin(2*pi*t)
| .^2;
15 .4r'2608A4288977 1 . 4 8 0 0 5 8 7 8 7 3 2 6 94 6 e - 0 0 8 5 Thus, with the default stopping criterion of es : 1 x 10-6. we obtain a result that is correct to over nine significant figures in five iterations. We can obtain an even better resultif we impose a rnore stringent stopping criterion:
r5.4L260804810169 0 i ra-
-
l
Gaussquadraturecan also be used to make the sameestimate.First, a changein variable is performedby applying Eqs. (18.22)and (18.23)to yield 1lI t:-*-t.r 4 4"
dt:-dt,t " 4
Theserelationshipscan be substitutedinto Eq. (18.29)to yield t,1",:
lt -) zs+ozs',)sinzt(0.25 + 0.25q)l'0.25 dt / [l0e-,o -
J-t
(18.30)
I8.5 CASESTUDY iir' rh-T:'-
"
continued
TABTE18.2
Voluesfor the iniegrolcolculoted usingNewton-Cotes formulos.
Iechnique
Segmenls
I 2 4 B )b 32 o4 I?B
Tropezoidol rule
S i m p s o n1s/ 3 r u l e
TABTE18.3 Poinfs
2 3 ,4
5 6
2 4 B t6 32
Inlegrol
e,l"/ol
0.0 r00 0000 1s.163266493 ) 6178 15.401429095 0.A725 l 5 4 11 9 5 8 3 6 0 4 . 2 2x 1 O - 3 15.412568151 259x1O-a 15412605565 I6lxlO-s 15.412607893 lOl x 10-6 1 54 1 2 6 0 8 0 3 8 6 . 2 8x l O - b 20217688657 t 5 .480816629 r5415468il5 I5 412771415 l5 412618037
3\ 1763 4.4426 0.0r86 I 06x 1O-3 6 48 x l0-5
Results of usingvorious-point Goussquodroture formulosto opproximote the integrol. Estimote
t L9978243 15.65755A2 r5 4058023 I 5. 4 1 2 6 3 9 1 154126109
e, l"/"1 22.1 1.59 4 . 4 2x l 0 - 2 2 . 0 1x l 0 - a L B 2x l 0 - 5
Forthe two-point Gauss-Legendre formula, this function is evaluatedat t,t : -1 I J3 and, 1/V3. with the resultsbeing 7.684096and 4.313728,respectively.These values can be substitutedinto Eq. (18.17)to yield an integralestimateof 11.99182,which representsan error of €r : 22.1c/o. The three-pointformula is (Table 18.1) 1:0.5555556(1.237449)+0.8888889(15.16321)+0.5555556(2.684915):15.65755 which has €t: l.6Vo.The resultsof using the higher-pointforrnulasare summarizedin Table 18.3. Finally, the integral can be evaluatedwith the built-in MATLAB function quad and quadL: >> irms2-quad(i2, 0, . 5) i. rms:2 ,-, \ t r , . 4 I 2 , b 0 84C9 13 4 5 0 9
444
N U M E R I C AI N L T E G R A T I OONF F U N C T I O N S
18.4 Tt
continued >> irirs2=quad1 (i:,
erf(c
0, . 5)
irms2 = 1 5 . . 11 : 6 0 8 0 4 8 C 9 r 957 Both these results are very accurate, with quadt being a little better. We can now compute the i,n by merely taking the square root of the integral. For ex" arnple, using the result computed with quadl, we get >> i rrrs=sqrt- (irrns2 )
Use the formula error for termine
r8.5 Th followir 1;:
irms = 3.92588945948554 This result could then be employed to guide other aspects of the design and operationof the circuit such as power dissipation computations. As we did for the simple sinusoid in Eq. (18.25), an interesting calculation involves comparing this result with the peak current. Recognizing that this is an optimization prob lem, we can readily employ the fminbnd tunction to determine this value. Becausewe are looking for a maximum, we evaluate the negative of the function:
where ; of the n (a) Rom two-poir quad fu Iti.6 Th
I*^
ForT = 0 . 2 2 4 B 1 9 4 0 3 1,9 3 2 L inax = - 7 . B B 6 B 5 3 8 l3 9 3 2 5 A
t (r) i(I)
A maximum cuffent of 7.88685 A occurs at t : 0.2249 s. Hence, for this particular wave form, the root-mean-square value is about 49.87o of the maximum.
Evalual ance ol formulr 18.7 T represe
PROBTEMS l8.l Use Rombergintegrationto evaluate f:
/
1\-
1: I {:,- -),/r r,/ Jt \
GaussquadraturetbrmLrla,and (d) MATLAB quadfunction:
| :
,"8
I
- ().0547.rr -r 0.s6-t6.rr - -1.1562.i:
,to
] 6 . 2 9 1 7 xI 2 d . r to an accuracvof s. : 0.5a/..Your resultsshouldbe presented 18.3 Evaluatethe tbllowing integralwith (a) Romberg intein the tbnnat of Fig. 18.1.Use the analyticalsolutionof the eration (tr :0.5%), (b) the two-point Gaussquadrature integralto deterrninethepercentrelativeerrorof theresultobformula. and (c) MATLAB quad and quadl functions: tainedr.vithRombergintegration.Checkthate, is lessthane.. 73 18.2 Evaluate the following integral (a) analytically, t: I xe'd.t rk\ Dnmhprcinreorarion(F" :0.5%). (c) the three_point Ji
where the pip meter. using ( two-p( quad'l 18.8 l riod oi M
PROBLEMS
445
18.4Thereis no closedfcrrnrsolution fbr the error functiol
whereM = nlass(mg), rr : the initial time (min), r: : rhe llnal tirne (rnin), Q1r) : flow rare (m3/min), and c(r.) : a rd = :f (mg/rn3). The following functional repreconcentration sySqal ,.-' ,/,r | Jn Jt sentations define the temporal variations in flow and Usethe (a) two-point and (b) rhree-pointGauss-Legendre croncentration: formulas to estimateeri( 1.5).Determinethe percentrelarive QQ):9+,icos21o.4r1 enorforeach casebasedon the true value.which canbe dec ( t ) - 5 e - ( )' 5 /* Z e t t ' 1 s r rvith MATLAB's built-in tirnction +r:f . termined 18.5Theforce on a sailboatmastcan be representedby the Determine the mass transported between 1r : 2 and following function: l: : 8 min rvith (a) Romberg integrationto a toleranceof rH 0.1Vc: and(b) the MMLAB qu,:il function. ,, n F = I too{ ----, ),, d.18.9 Evaluatethe doubleinteglal \ / t--,/ Jo where;: the elevationabovethe deck and 11: the heighr of themast.Compute F fbr the case where I1 : -10using (a)Rombergintegrationto a toleranceof e. : 0.5olc.(b) the tu,o-point Gauss-Legendreformula. and (c) rhe MNLAB q:r;dI'unction. 18,6Theroot-mean-square cunent cln tre cornputedas
'rrr: I\nu, E /
(.r2 - 3-r,2* -r,r'3)r1.rr1-r,
(a) analyticallylnd (b) usinr the MATLAB dl.t cr,.r,:cr function. Usehetp to understandhor.vto implementthe tunction. 18.10 Computework as describedin Sec. j7.9, but usethe tbllowing equationsfor F(_r)and d1.r): F(.r):1.6x-0.045.12 d(t) : -0.000-5-5rr* 0.0123;12 * 0. l3.r
Forf : l. supposethat i (r) is definedas -
l_, l,'
r\
t ( r ): l o d - /' s i n ( Z r 7J
[or0> plot (xm,d,'o',):a,ya) As displayedin Fig. 19.8,the resultscomparelavorablyfor this case. Note that asidefrom evaluatingderivatives,the dif f function comesin handyasa progranming tool fbr testingcertaincharacteristics of vectors.For example,thefollowing statementdisplaysan error nlessageand terminatesan M-file if it determinesthata vector :i has unequalspacing: if
any (drf
f (drf
f (x) )-=91 ,
error
( 'ur.iequal
spacing'
) ,
end
Another common useis to detectwhethera vectoris in ascendingor descending order. For example,the following code rejectsa vector that is not in ascendingorder(ntonotoni""'t,t:t:::::*.f,.,.r.:,:-f, erro]-(,n.r
E)
I 9 . Z N U M E R I C ADLI F F E R E N T I A TW I OI N T HM A T L A B
19.7.2
MATTAB Function: sradienr
The gradienr- functionalso returnsdifferences. However.it doesso in a mannerthat is more compatible with evalr.ratingderivativesat the valuesthemselvesrather tharr in the intervalsbetweenvalues.A simple representation of its syntaxis 'x
-
9.,'.1 .'l
(''
where f : a one-dimensional\/ectorof lengthn, and fx is a vectorof lengthn containing differencesbasedon f. Just as with the dirf function, the flrst value returnedis the differencebetweenthe first and secondvalue. However, for the intermediatevalues,a centered dif'fbrencebasedon the adiacentvaluesis returned .l,tr - .fi r (19.28) dtlIi : 2 The last value is then computedas the differencebetweenthe final two values.Hence,the resultsare akin to using centereddifferencesfor all the intermediatevalues,with forward and backwarddiflbrencesat the ends. Note that the spacingbetweenpoints is assumedto be one. If the vector represents equally spaceddata,the following versiondivides all the resultsby the interval and hence returnsthe actualvaluesof the derivatives, f;E = gradient
( f,
h)
where h : the spacingbetweenpoints. ,l9.5 EXAMPLE
Usingu, .riii+rL for Differentiotion Problem Stotement. Use the qiradient tunctionto diflbrentiatethe samef'unctionthat we analvzedin Example 19.4with the di tf function. Solution. In the samefashionas Exanrple19.4,we can generatea seriesof equally variables: and dependent spacedvaluesof the independent ^ 2 + 6 1 > > f - @( x ) A . 2 + 2 5 * x - 2 0 O * x . 5 * x . ^ 3 9 0 0 * x . ^ 4 + 40 t J * x .^ 5 ; > > x = 0 : 0 . 1 : O .B ; Y I rl; We can then use the graclient function to determinethe derivativesas >> dv_sradient1y,0.1) ^., Columns 1 through 5 5.44C\0 10.89t)0 Columns 6 through 9 -4.8100 5.0400
1.5900
5.8400
16.1500
-21.3100
8.5900
As in Example 19.4, we can generate values for the analytical derivative and display both tlre numerical and analytical estimates on a plot: >> xa=O:.01:.8; > > ! a = 2 5 , 10 0 * x a + 3 * 6 7 5 * x a . ^ 2 r.r,ya) >' plot(x,dy,'o',
^4; 4*900*xa. ''3+5*400*xa.
464
NUMERICAL DIFFERENTIATION
25 20
10
FIGUREI9.9 Compcrisonof the exccf derivotive(line)wiih numericoleslimotes(circles)computedwith MATLABs qradient function.
As displayedin Fig. 19.9,the rcsults arc not as accurateas thoseobtainedwith the dif f function in Example 19.4.This is due to the f'actthat gradient ernploysintervals that aretwo times(0.2)as wide as for thoseusedfor dl rf (0.1).
vectors,the gradient functionis particularlywell suited Beyond one-dinrensional mafor determiningthe partialderivativesof matrices.For example,for a two-dimensional trix, f, the function can be invoked as I fx, fyl
= gradient (f,
h)
where fx correspondsto the differencesin the r (column) direction,and 4.corresponds to the ditferencesin the y (row) direction, and .h : the spacingbetweenpoints.If .nis ornitted,the spacingbetweenpoints in both dimensionsis assumedto be one.In thenext section,we will illustratehow gradient can be usedto visualizevectorfields.
I9.8 CASESTUDY
465
VISUALIZINGFIELDS Beyond the determinationof derivativesin one dimension,the sradienr Bockground. function is also quite useful for determining partial derivatives in two or more dimensions. In particular, it can be usedin conjunction with other MATLAB functions to produce visualizationsof vector fields. To understandhow this is done, we can returnto our discussionof partial derivatives at the end of Section 19.1.1.Recall that we usedmountain elevationas an exampleof a two-dimensional function. We can representsuch a function mathematically as
z: fk,y) where z : elevation, x : distance measuredalong the east-westaxis, and "y : distance measuredalong the north-south axis. For this example, the partial derivatives provide the slopes in the directions of the axes.However, if you were mountainclimbing, you would probably be much more interested in determining the direction of the maximum slope.If we think of the two partial derivatives as component vectors, the answer is provided very neatly by
af
af
dx
dv
vf:+i++j
where V/ is referred to as the gradient of I This vector, which representsthe steepest slope, has a magnitude
(#)'. (#)' and a direction _r / Af/Ay\ 0^ : tan-' \ffi ) where I : the angle measuredcounterclockwisefrom the r axis. Now supposethat we generatea grid of points in the x-y plane and used the foregoing equations to draw the gradient vector at each point. The result would be a field of arrows indicating the steepestroute to the peak from any point. Conversely,if we plotted the negative of the gradient, it would indicate how a ball would travel as it rolled downhill from any point. Such graphical representationsare so useful that MATLAB has a special function, called quiver, to createsuchplots.A simplerepresentation of its syntaxis quiver(x,y,u,v) where x and y are matrjces containing the position coordinates and u and v are matrices containing the partial derivatives.The following example demonsffatesthe use of quiver to visualizea field. Employ the srradient function to determineto partial derivativesfor the following two-dimensionalfunction: /fr
rr\ :
1,-
y -7xZ
-2.xt,
-
tZ
466
NUMERICAT DIFFERENTIATION
continued from : -2 to 2 andy : l to 3. Thenusequiver to superimpose 1 a vectorfieldonacontour plot of thefunction. Solution.
Wecanfirst express/(.t, y) asananonymous function
> > f = G( x , y ) y - x - 2 * x . ^ 2 - 2 . * x . * y _ y . ^ 2 ; A seriesof equally spacedvalues of the independent and dependentvariablescanbe generated as >>
z_f (x,y-);
The gradient
function can be emproyedto determinethe paftiar
derivatives:
19.1 mati mati
We can then developa contourplot of the results: >>
cs-contour(x,y,z
);clabel
(cs);ho1d
on
As a final step,the resultant of the partial derivatives can be superimposed -' -"-- asvectorsonthe r contourplot: >> quirzer(x,y,_fx,,fy);ho1d
:
oft
FIGUREI9.IO
ffil$?":rui5r*n'".ruj:lof
tt",
o twodimensionol function with rheresulionr oftheporriol
J
2.6
2.6
t9.2 first E'np
19.3 finit, seco diffe In ei inter 2L.t equi first eacl cate 19.t riva and initi
l9.r l9.l of -r ro= the diff 19.' redr -1.6
-1.4
-1.2
-0.6
-0.4
-0.2
l9.i
PROBLEMS
467
Note that we have displayed the negative of the resultants, in order that they point "downhill." The resultis shownin Fig. 19.10.The function'speakoccursat x : *1 and y : 1.5 and then drops away in all directions.As indicatedby the lengtheningarrows,the gradient drops off more steeply to the northeastand the southwest.
PROBTEMS 19.1Compute forward and backward difference approxiof O(h) ard O(h]), andcentralclif'furencc approrirnations mationsof O(h)\ and O(h1l for the flrst derivativeof . l ' = c o s - ra t - r : r / 4 u s i n ga v a l u co f h : r 1 1 2 . E s t i m a l e thetruepercentrelative error rr fbr eachapproximation. approrinrrtionsto estimatethe 19.2Usecentereddifl'erence firstand secondderivativesof -r - e' ar x : 2 fot' ft : 0. I . Employboth O(h?) andO(ft4) formulasfbr your cstimates. 19.j Use a Taylor series expansion to derivc a ccntercd approximationto the third clerivalivethat is finite-difference second-order accurate.'Ibdo this,you will have(o use four expansionsfbr the pointsli :. ri I . r'r+t . ilnd,r;+2. diff'erent In eachcase,the cxpansionwill be aroundthe point -r;. The intervalA,r will be usedin eachcaseof i - i and i * l, and 2A.r will bc used in each casc of i - 2 antli * 2. The four' must then be combined in a way to climinate the equations first and secondderil'atives. Carry enou-{hterms along ilt eachexpansionto evaluatethe first term that will be truncatedto determinethe orilcr oi the approximation. 19.4Use Richardsonextrapolationto estimatethc first deof r' : cos.r at t : n llusing stepsizesof h 1: n f I rivative andh. : r/6. Ernploy centereddifl'erencesof O(/rr) for the initialestimates. 19.5RepcatProb. 19.4.but fbr the first derivativeof ln.r at . r = 5 u s i n gh t : 2 a r u lh z - - | . 19.6Employ Eq. (19.2[) to determinethe first dcrivative o f r : 2 - r a - 6 - t 3 - l 2 . r - 8 a t r : 0 b a s e do n v a l u e sa 1 x o= - 0 . 5 , J r : 1 , a t t dx 2 : 2 . C o m p a r ct h i s r e s u l tr v i t h thetruevalue and with an estimateobtainedusing a centered differenccapproxiniationbasedon /i : l. 19.7Provc that fbr cquispaceddata points, Eq. (19.21) reducesto Eq. 6.25) at .r : -r| . 19.8 Develop an M-fllc to apply a Romberg algorithm to -...:----!-
iL
-
-l-,-:,,^+:-,,.
.-f
^
ft;r,dh
{',,r,-ti,rn
19.9 Develop an M-filc to obtain flrst-tlerivativeestimates for unequallyspaceddata.Tcstit with thc fbllowin-edata:
15 0.6 t6 x f ( x ) 0 9 0 3 6 4 . 3 7 3 4 0 . 32 6l
25 35 o o B 4 2 2 0 0 15 9 6
rvhcre .l (r) : 5z-2'.r. Cornpareyour rcsulls with the true derivatives. 19.19 Devclop an M-file function that computcsfirst and secondclerivativc estirnates of oltler O1lir1brsedon the fbrmulas in Figs. 19.3 through 19.5.Thc function'sflrst line shouldbe sct up as f unctiotr
l r l l ' d x , O r O O * 2 I = f l i f f r z q( ; , - . ' 1
wherc x and y are input vectorsof length n containingthe valuesof the indepcndent and dependeutvariables,rcspectively, and dydx and dy2d.;2 are output vectorsof lcngth il estimatesat containing the first- and second-derivative cach value of thc indcpendcntvariable.The function shoulcl generatea plot of c1y'dxarrd dy2dx2 versusx. Havc your M-file returnan error messlgeif (a) the input vectorsare not varithe samelength,or (b) the valueslor the independent able are not equally spaced.Test your programwith the data f i o m P r o b .1 9 . Il . 19.11The fbllowing data was collectcdfor the distance traveledversustime fbr a rocket:
It
S
J, km
25 3'2
50 5B
75 7B
t00 92
125 r00
Usc numericaldiff'erentiationto estimatethe rocket's velocilr
468
N U M E R I C ADLI F F E R E N T I A T I O N
19.12 A jet fighter'spositionon an aircraftcarier's runway was timed during landin-u:
t,s x,m
0 153
o:.l85
lAA 2aB
t 75 2 37 '249 261
325 271
383 273
where x is the distancefrom the end of the carricr.Estimate (a) velocity @.rldt') and (b) acccleration(duldrl using nunrericaldifferentiation. 19.13 Use the fbllowing datato find the velocitl'and accelerationat / : l0 seconds:
lime,t,s O 2 Position,x,m O a7
4 IB
6 34
B 5l
l0 63
l2 l4 16 7:3 B0 B4
19.17 The fbllowing data was generatedftom the normal distribution:
-l -s -05 x 2 I 0 (1 { l 0 , s 3 9 9 O ) 2 9 5 2 a.2atq7 352070 3e894 /(x)
r.5 r 05 l 2 f ( x \ 0 . 3 5 2 A 74 2 4 1 9 7 4 . 1 2 9 5 20 0 5 3 9 9
I
2
J
4
5
6
7
B
9
10
Use sccond-order correct (a) centered flnite-diftcrence, y 1 4 2 ) 3 . 3 4 B 6 B 6 6 8 . 6 7 5 8 . 9 1 0 9 t 0 (b) fbrward finite-difl'erence,and (c) backward finitemethods. clifTercnce 19,19 The objcctive of this problem is to comparesecondlg.l1 A plane is being trackcdby radar,and data is taken orclel accurate forwiucl, backward, and centeredfiniteeverysccondin polarcoordinates I and r ditfbrenceapproxirrationsof thc first clerivative of a function to the actr.ralvalue of the derivativc.This will be donefor
t, s 2AA 0,(rodl A75 t', tn 5l 20
2A2 244 246 2 0 8 2 t O A72 o 7 a 0 6 8 4 57 4.66 5374 5560 5800 6 0 3 0 6 2 4 a
lG):e-2'-x
(a) Use calculusto dctclmine the corrcctvalueof thedenvativeatr:2. (b) Develop an M-f ile function to evaluatethe centered fi nite-difl'erenceapproximations, startingwith .r = 0.5. At 206 seconds,use the centercdfinite-diflerence(secondThus, for the first evaluation,the .r valuesfor thecenorder correct) to find the vector cxpressionslor vclocity il tered differencc approximationwill be .r : 2 t 0.5or and accelerationrl. The velocitv and acceleratiorrgiven rn of 0.1 polar coordinatesale r : 1.5 and 2.5. Then. decreascin increments down to a nrinimumvalueof A-r = 0.01. i : i a , + t 0 4 0 a n d a : G - r 0 : ) 2 , ,I \ r 0 + 2 i 0 ) A e (c) Repeatpart (b) lbr the second-orderfbrwardandback(Notethatthesecanbedoneatthesame ward difTerences. 19.15 Use regrcssionto estimatc the accelerationat each time thatthe centereddifl'erenceis computedin theloop.) time fbr the following data with second-,third-, and fburth(d) Plot the resultsof (b) and (c) versr.rs r. Includetheexact orderoolvnomials.Plot the results: rcsullon the plot lol eomparison. 19.20 You have to measurethe flo$,rate of waterthrougha 1 2 3.25 45 678 B 5 9 3 t 0 surallpipe. In order to do it, 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 measurethe volumc in the bucketasa function of titnc as tabulatedbelow. Estimatethe flow rateat t = 7 s. 19.16 The nolrlal distributionis definedas - - r -I t - 1 z "f ( r ) J2tr Use MAILAB
. ,^
to determine the inflection points of this
Time, s Volume, cm3
0 0
T
::::: J,h U, ftl
Use MATLAB to estimatethe inflectionpointsof thisdata. 19.18 Usc the .tif f iy; commanclto developa MATLAB M-file tunction to computeflnite-difftrenceapproximations to the first and sccondderivativeat each-r valuein thetable below. Usc finitc-ditfbrenceapproximationsthataresecondordercorrcct.O (.r2):
,rO
Use i r (N/
B 164
19,21 The velocity rr (m/s) of air flowing pasta flat surface
t9.22 M
wher u n r t€ (cmz Ane centr derly
i:::
x, cn c, l(
Use t estirr conjr pollu (D= sedin the lr t9.24
'T:
t, mi v, lr
Calc order 19.2,
neer! temp
x
rrh T, "l
If thr
469
PROBLEMS
larl to determine the shear stress UseNewtott'sr:i.rczrsln' r (N/m:)at the surface(.v : 0),
19.25 Ths horizontalsurfaceareaA, (nr21of a lakeataparticulardepthcanbecomputedfionr volumeby difTerentiation:
in 4 . 1 ; 1:
L-lL-
aI
A s s u m e a v a l udeyonfa m i c v i s c o s i4t y: 1 . 8 ),h
0
u,m/s 0
0.002 0006 a2B7 0899
00r2 I915
x l0-5 N ' s/rn2.
0 0r B 3 048
A A?! 4 299
rvhere I./ : volur.ne(ml) and ; : tlepth (nr) as nreasured fiom the surfacedown to the bottom. The averageconcentlationof a substance thatvarieswith depth.- (g/m3),canbe computeclby integlatron:
.[,] r:Q)A,(z)ctz.
19'22pi711',e 71vstdiffir.sion/rnl stalesthat Massflux : -Dd' dr
(Pt9.22) whereZ:
where massflux : the quantity of massthat passesacrossa D : a dil-fusioncoefTicient unitareaper unit tirne (g/cmr/s.1. (g/cm3).and ,r : distance1cm). (cmr/s), c : concentration An environmentalengineer measuresthe fbllowing concentration of a pollutant in the pore watersof sedimentsunderlying a lake (.r : 0 at the sediment-waterinterfaceancl downward): increases
0 006
r, Cm c, l0-6 g/cm3
dV , (:) aa.
a32
1'de,s1az
the total depth (rr). Determinethe averageconcentralionbasedon the following data:
16 4 B 12 O z,n v, lo6m3 I8175 5 105I I 9635 Q3927 00000 lo.2 7.4 4.1 B5 5.2 c,9/m3 19.26 p;orr4^r's law characterizesthe r'oltagedrop across an inductor as
3 06
di Vt.: L; t7l
Usethebestnr,rmerical difl'erentiationtechniclueavailableto estimate the derivalive at "rr: 0. Ernploy this estimate in with Eq. (PI9.22) to colrlputethe massllux of corlunction pollutant out of the sedimentsand into the overlying walers Q - - 1 . 5 2 x 1 0 - 6 c m 2 / s )F. o r a l a k ew i t h 3 . 6 x 1 0 6r n 2o f sediments. how much pollutantu'ould be transportedinto thelakeover a year's time? 19.23fne fbllowing data was collectedwhen a large oil tankerwas loading: /, min I', 106 borrels
0 r 0 2 0 3 0 4 5 60 04 07 027 0BB r 05 \17
(:,
the flow rate Q (that is, tll/ ldt) fbr eachtirne to the Calculate orderof /rI . 19'24psudsp'slaw is usedroutinelyby architectural engineersto detennineheat flow through walls. The fbllowing are measuredfiorn the surface(.r : 0) into a temperatures
(i1 henrys: where V.: r,oltagedrop (V), L: indtrctance I H : I V. s/A), I : current(A), and I: time (s). Determine the voltagedrop as a functionof time from the fbllowing datafor an inductance ol4 H.
a 0
t i
0I 016
02 a32
03 056
05 084
0/ 2A
19.27 gxr.4 on Faraday'slaw 1Prob.19.26),usethe following voltagedatato estimatethe inductanceif a currentof 2 A is passerJ thloughthe inductorover'400milliseconrls. t, ms V, volts
0 t0 20 40 60 B0 12a lB0 280 400 35 26 15 7 a)829414946
l9.2tt 15srareof coolingof a body(Fig.P19.28) canbeexpressed as JT
klT
Tul
dT I,
lf,
T,'C
0 t9
008 l7
If theflux at x : 0 is 60 W/mr, cornputeft.
O.t U
whereI: temperatureof the body ("C), 4, : temperatureof conrnedium('C), andl:: a propoflionality the surrounding stant(per minute).Thus, this equation(calledNcruton's/aw of cooling) specifiesthat the rateof cooling is proportionalto
470
NUMERICAL DIFFERENTIATION madetbr air flowing over a flat plate where,r': distancenorrnal to thc surface:
0 900
Jr clll
T,K
I 480
35 270
dioxi deter lated and
L), the springforce 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 increasesand alwaysactst0 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 formulartinganotherdifferentialequation lbr distance: dx
(20.2)
dt
Thus, solving tbr the bungeejurrrper'svelocity amountsto soiving two ordinarydifferentialequationswhereone of the equationstakesdifferentforms dependingon thevalue
I Some computerlanguagesrepresentthe signum functionas sgr (x). As represented here,MATLAB usesthe n o m e n c l a t u r sei o n 1 x ) .
20.2 EULER'S METHOD
48r
of one of the dependentvariables.Chapters20 and21 exploremethodsfor solving this and similar problemsinvolvingODEs.
20.t ovERvrEw This chapteris devotedto solvine ordinarv diflerentialequationsof the form d.y
dr
: I (l' -Y)
(20.3)
In Chap. l, we developeda nnmericalmethodto solve suchan equationfor the velocity of the free-falling bungeejumper. Recall that the methodwas of the generalform New value : old value* slope x stepsize or, in mathematicalterms, .Vi+r: )'i*Qh
(20.4)
where the slope@is calledan incrcnzent Junction.Accordingto this equation,the slopeestimate of @is usedto extrapolateliom 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 the future. Suchapproaches are calledute-ste1tntethodsbecausethe value of the increment function is basedon information at a single point i. They are also refelred to as Rwrgewho first discussedthem in the early Kutta ntethodsafter the two appliedmathematicians 1900s.Another class of rnethodscalled multistepmethodsuse information from several previous points as the basisfor extrapolatingto a new value. We will describemultistep rnethodsbriefly in Chap.21. All one-stepmethodscan be expressedin the generalform of Eq. (20.4),with the only differencebeing the mannerin which the slopeis estimated.The sirnplestapproachis to use the differentialequationto estimatethe slopein the fbrm of the first derivativeat /;. ln other words, the slopeat the beginningof the interval is taken as an approximationof the averageslope over the whole interval.This approach,called Euler's method,is discussed next. This is followed by other one-stepmethodsthat employ alternativeslope estimates that result in more accuratepredictions.
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 differentialequationevaluatedat /; and _r'i.This estimatecan be substitutedinto Eq. (20.1t: )'i+r: t"i* fGi,v"t)h
(20.s)
This formula is ref-enedto as Euler's rnethod(or the Euler-Cauchyor point-slopemethod). A new value of -"-is predictedusing the slope (equalto the first derivativeat the original valtte of r) to extrapolatelinearly over the stepsize h (Fig.20.1).
482
INITIAL-VALUE PROBTEMS
FIGURE 20.I Euler s meihod.
E X A M P L E2 0 . . l
E u l e r ' sM e t h o d ProblemStotement. Use Euler'smethodto integrate!' - 4ro s' - 0.5.r'from r : 0 to 4 with a stepsizeof 1. The initial conditionat / : 0 is y : 2. Note that the exactsolutioncan be determinedanalvticallvas ^' * e o5r')12n-o5r ) : r r-lrtrut' Solution. Equation(20.5)can be usedto implementEuler'smethod: , ] , ' ( l ): ) , ( 0 )+ J ( 0 , 2 ) ( l ) where 1,(0) :2
andthe slopeestimateat / : 0 rs
. f ( 0 , 2 ) : 4 e o- 0 ' 5 ( 2 ) : 3 Therefbre, ](l):2+3(l):5 The true solutionat r : r :
4
,-,
1 is
r o * r tt tr ( e u s t - e 0 5 ( l ) )+ 2 e - 0 5 n \ : 6 . 1 9 4 6 3
Thus.the percentrelativeerroris t6.19463-51 F, : l-l x 1007o: l9.28Vo 6.19463 l For the secondstep: )(2) : y(l) * /(l. s)(l) : - 5+ f 4 e 0 8 ( r- t 0 . 5 r 5 t l 0 ) :
11.40216
483
METHOD 20.2 EULER'S TABLE20.l
voluesof the integrol of trueond numericol Comporison q1 o f . r ": 4 e ( t 8- t 0 . 5 , vw, i t ht h ei n i t i ocl o n d i t i o tnh o t y : 2 Euler's using computed volueswere r : O.Thenumericol methodwith o stePsizeof I ' Jt"u"
0 I 2 3 4
2 00000 6 19463 I4.84392 3 36 7 7 l 7 75 . 3 3 8 9 6
JEur".
lerl (%)
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 usingEulers solution wiiho numericol solurion of fhe.rrue Comporison -* !,]^"I59|.1 ?t 2 T h J l n i t i oclo n d i l i oont / : O i s i",\;,-- 6 i" f,.r ;:O;4
w 1 ho s i e ps i z eo f I o
1':
relative error is The true solution at t : 2.0 is 14.84392and, therefore,the true percent 1 and Fig' 20'2' 23.l97o.Thecomputationis repeated,and the resultscompiledin Table20' the error Note that althoughthe computationcapturesthe generaltrend of the true solution, using a by is considerable.As discussedin the next section,this error can be reduced smaller step size.
20.2.1 Error Anolysis for Euler'sMethod 4): The numericalsolutionof ODEs involves two typesof error (recallChap' employed l. Truncation,or discretization,errorscausedby the natureof the techniques to approximatevaluesof Y. canbe retained 2. Roundnfi'enofscausedby the limited numbersof significantdigits that 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
484
INITIAL-VALUE PROBLEMS previous steps.The sum of the two is the total enor. It is referredto as the gbbal truncation error. Insight into the magnitudeand propertiesof the truncationerror can be gainedby deriving Euler's methoddirectly from the Taylor seriesexpansion.To do this, realizethatthe differential equation being integratedwill be of the general fbrm of Eq. (20.3), where dyldt : -y', and t and y are the independentand the dependentvariables,respectively. If the solution-that is, the function describingthe behaviorof y-has continuousderivatives, it can be representedby a Taylor seriesexpansionabout a starting value (/;,.yr). as in [recallEq. (4.13)]: t
]i+r: ti *yitt +*n'+ :!
ln|
" + ' r ' i ' 7 , "* 4 n
(20.6)
nl
where/u : ti+t - /; and Ra : the remainderterm, definedas R,,' -
,,0t*l)191
j------::-/r"+l ( t z* 1 ) !
(20.7)
where f lies somewherein the interval from /; to /; 11. An alternativeform can be developedby substitutingEq. (20.3)into Eqs.(20.6)and (20.7)to yield f'(tj'-Yi) ,,2 * .yr+r: )'i t f (ti,l')h + where O(h'+l) specifiesthat the local truncationetror is proportional to the stepsize raisedto the (n * l)th power. By cornparingEqs. (20.5) and (20.8),it can be seenthat Euler's rnethodconesponds to the Taylor seriesup to and including the term /(r;,,yi)/r. Additionally, the comparison indicatesthat a truncationenor occursbecausewe approxirnatethe true solutionusinga finite numberof termsfrom the Taylor series.We thustruncate,or leaveout, a part of thetrue solution.For example,the tluncationerror in Euler's methodis attributableto the remaining terms in the Taylor seriesexpansionthat were not included in Eq. (20.5). Subtracting Eq. (20.5)from Eq. (20.8)yields
E ,- [ 1 + P h 2+ . . .+ o ( h , + Ii
t20.ql
where E, : the true local truncationerror.For sufficientlysmall lz,the higher-orderterms in Eq. (20.9) are usually negligible,and the result is often representedas
^
F
-
,\r,, .f ) , ), , r
- h '
(20.l0)
or Eo : O(h2)
(.20.1)
where E, : the approximatelocal truncationeffor. According to Eq. (20.ll), we seethat the local error is proportionalto the squareof the step size and the first derivative of the differential equation.It can also be demonstratedthat the global truncationerror is O(/r)-that is, it is proportionalto the stepsize
20.2 EULER'S METHOD
485
(Carnahanet al., 1969).Theseobservations lead to someusefulconclusions: 1. 2.
The global error can be reducedby decreasingthe stepsize. The method wiil provide error-freepredictionsif the underlying function (i.e., the solution of the differential equation)is linear, becausefor a straightline the second derivativewould be zero.
This latter conclusionmakesintuitive sensebecauseEuler's methodusesstraight-linesegments to approximatethe solution. Hence, Euler's method is referredto as a first-order method. It should also be noted that this generalpatternholds for the higher-olderone-step methodsdescribedin the following pages.That is, an nth-ordermethodwill yield perfect resultsif the underlying solution is an rrth-orderpolynomial. Further,the local truncation errorwill be O (h"+1) and the globalerror O (h').
20.2.2 Stobility of Euler'sMethod ln the precedingsection.we learnedthat the truncationerror of Euler's methoddependson the step size in a predictableway basedon the Taylor series.This is an accuracyissue. The stability of a solutionrrrethodis anotherimportantconsiderationthat must be consider-edwhen solving ODEs. A nurnericalsolution is said to be unstableif enors grow exponentiallyfor a problem for which there is a boundedsolution.The stability of a particular applicationcan dependon three f-actors:the differential equation,the numerical method,and the stepsize. Insight into the step size required for stability can be exanined by studying a very simpleODE: dt' -ay ; at If r(0) - )0, calculuscan be usedto determinethe solutionas V :
ltr€
(20.12)
-''
ffrrr, tf," *fution startsat 1,eand asymptoticallyapproacheszero. Now supposethat we useEuler's methodto solve the sameproblernnumerically: - y i + l: l i + ,
d)', ,
S u h s t i t u t i nEgq .t l 0 . I 2 t g i v e s -Yr+l:
ji -
\'ih
or - ) ' i + r : 1 , i ( l- a h )
(20.13)
The parentheticalquantity 1 - ah is called an cunplifictttion foctor. If its absolutevalue is greaterthan unity, the solutionwill grow in an unboundedfashion.So clearly,the stability dependson the step size /r. That is, if /z > 2la,lyil --+ oo as i -+ oo. Basedon this analysis, Euler's methodis said to be conditionall,vstable. Note that thereare certainODEs where errorsalways grow regardlessof the method. SrrchODEs are called ill-conditioned.
486
INITIAL.VALUE PROBLEMS Inaccuracyand instability are often confused.This is probablybecause(a) bothrepresentsituationswhere the numericalsolutionbreaksdown and (b) both are affectedby step size.However,they are distinct problems.For example.an inaccuratemethodcanbe very stable.We will retum to the topic when we discussstilTsystemsin Chap.21.
20.2.3
MATLAB M-file Function: eulode
We have alreadydevelopeda simple M-file to implementEuler's method for the falling bungeejumper problem in Chap.3. Recallfrom Section3.6, that this function usedEuler's nethod to compute the velocity after a given time of tiee fall. Now. let's developa rnore general,all-purposealgorithm. Figure 20.3 showsan M-file that usesEuler's methodto computevaluesof thedependentvariabley over a rangeof valuesof theindependentvariablet . The nameof thefunction holding the right-hand side of the differential equationis passedinto the function as the
FIGURE 20.3 AnMJileto implemeni Euler's method = eulode(dydt, tspan,y0,h,,rarargin) f u n c r ,i o n It,y] B eulode: EUIET O-DE SOIVCT z [ r , y ] . e u .l o d e r d y d t , r s p o n , y 0 , h , p l , p l , . . . , : method to integrate z uses Euler's an ODE % rnput: 96 dydt = name of the M-file Chat evaluates the ODE z tspan = lti, 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,p2, . . . = additional parameters used by dydt % output : z t - vector of independent variable z y = vector of solution ior dependent variable if
nargin.4,elror = tspan(1);tf ' lLf>ri),error
ri if
(ti:h:tf)';
t
=
z z
if
necessary,
q^
rh)l
1a"-.4
{'ar
least 4 inpuu argumen-s reqL rq.6'r,end tspan(2); {'Jpper lrmit musl be grearer Lhar' .ower'l,end n = length(t-); add an additional value of t -
^^ac
rIOm
L
-
ri
tO
Ct
r (n) > t i 1 ' c i t = G( t , V ' )
4*exp (0. 3*t)
o.5"yt
The solutioncan then be generatedas >> disP(tt,Yl
)
with the result(comparewith Table20.1): 0 1.0000 2.4400 3.000a 4.0000
2.0000 5.0000 1r.4422 25.4132 55.8493
Although using an anonymousfunction is feasiblefor the presentcase.there will be more complex problemswherethe definition of the ODE requiresseverallines of code.In suchinstances,creatinga separateM-file is the only option.
20.3
TI,IETHOD IMPROVEMENTS OF EULER'S A fundamentalsourceof error in Euler's methodis that the derivativeat tlre beginningof the interval is assumedto apply acrossthe entire interval. Two simple modificationsare availableto help circumventthis shorlcoming.As will be dernonstratedin Section20.4, both modifications(as well as Euler's methoditself) actuallybelongto a largerclassof solution techniquescalledRunge-Kuttamethods.However,becausethey have very straightforward graphicalinterpretations,we will presentthem prior to their forrnal derivationas Runse-Kuttamethods.
488
INITIAL-VALUE PROBLEMS
2O.3.1 Heun's Method One methodto improve the estimateof the slopeinvolves the determinationof two derivativesfor the interval-one at the beginningand anotherat the end.The two derivatives are then averagedto obtain an improved estimateof the slopefor the entire interval.Thisapproach,calledHeun'smethod,is depictedgraphicallyin Fig. 20.4. Recall that in Euler's method.the slopeat the besinnins of an interryal )"i : .f (ti,-vi)
(20.r4)
is usedto extrapolatelinearly to y111:
(20.r5)
))+r:litJ'Gi,Yi)h
For the standardEuler methodwe would stopat this point. However,in Heun'srnethod the r ' , 1 *c,a l c u l a t e c l i n E q . ( 2 0 . 1 5 ) i s n o t t h eat 'ni nsawl e r . 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 . T h i s i s why we havedistinguishedit with a superscript0. Equation(20.15)is calledaprcdictorequatiott. It providesan estimatethat allowstlrecalculationof a slopeat the end of the interval:
)'1+r: ,f (rt*t,l'j!,)
(20.l6)
Thus, the two slopes[Eqs.(20.14)and (20.16)] can be combinedto obtainan averageslope for the interval; - , _ / ( r , , - r ' r )* , f ( r r + r , r ' , 9 + r )
2 This averageslope l s then used to extrapolatelinearly frorn -1';to li+r using Euler's method: , f ( r , . y i l ' . l Q i - t . . , ' 'l )' , )rrl:)'ir-------
,)
/l
which is called a corrector eguation.
FIGURE20.4 Grophicol depiciiono[ Heun'smelhod (o) Predictorond (b) correctof
= .f(ri* r. YP*r)
: f(ti,y i)
(20.r7)
489
OFEULER'S METHOD 20.3 IMPROVEMENTS
FIGURE 20.5 of iterotingfhe correctorof Heun'smethodto obloin cn improved Grophicol representotion estimcte.
The Heun method is a pretlic'tor,(orrectorapproach.As just derived, it can be expressedconciselyas Preclictor(Fig.20.4a):
y,o*r: y j " *
Corector (Fig. 20.4b):
y/+r : l'i' I
(20.r8)
f (ti,yi)h
l'(rt,yi)+ / ( t , * , , 1 ' , 1 * ' ' )
h
(20.r9)
(for;:1.2""'m) Note that becauseEq. (20.19)haslial on both sidesof the eclualsign,it can be appliedin an iterativefashionas indicated.That is. an old estimatecan be usedlepeatedlyto provide The processis depictedin Fig. 20.5. an improvedestimateof -v;11. As with similar iterativemethodsdiscussedin previoussectionsof the book, a termination criterion for conversenceof the correctoris frrovidedby
tc
t-
t#l"ouo"
where1,f,r and.v/*1 are the resulttiom the prior and the presentiterationof the corrector, respectively.lt should be understoodthat the iterative processdoes not necessarilyconverge on the true answerbut will convergeon an estimatewith a finite truncationerror,as demonstratedin the following example.
20.2 Heun's EXAMPLE Method 8' Problem Stotement. Use Heun's methoclwith iterationto integrate.]'/ 4e0 - 0.5y f r o m t : 0 t o 4 w i t h a s t e ps 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 . r : 2 . E m p l o ya s t o p ping criterion of 0.00001c/o to terminirtetlre correctoriterations. Solution. First,the slopeat (16,,rg)is calculatedas ! ' o : 4 e " - 0 . 5 ( 2 ): 3 Then,the predictoris usedto computea valueat 1.0: v9:2*3(t):5
490
INITIAL-VALUE PROBLEMS
TABTE20.2
Comporison of trueond numericol voluesof the integrolol tr - 4e0E 0 . 5 1 'w , i t ht h e i n i t i ocl o n d i t i o tnh o t - y : 2 o t t : O .T h en u m e r i c vo ol l u e s were computedusingthe Eulerond Heunmethods with o stepsizeof 1. TheHeunmethodwos imolemented bothwithoutqnd with iierqtion o[ thecorrector. With lterotion
Without lterqlion Jt.uo
0 I '2 3 4
Jtiur".
200000 6. I 9463 l4 84392 3 3. 6 7 7l 7 75 33896
200000 5 00000 t 140216 2 5. 5 I3 2 1 56 84931
le,l("/"1 l9 28 2319 24 24 2454
l€,1("/"1 2 00000 6 7010B I6 31978 37 19925 83 33777
B.tB 994 la 46 t4.62
.VH"un
le,l ('hl
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 standardEuler method.The true velluein Table 20.2 showsthat it correspondsto a percentrelativeerror of 19.28a/o. Now, to improve the estimatefor .v;..1,we usethe value.r'f'to predict the slopeat the end of the interval t . i : . / ( x r , r , l ' ): 4 e 0 8 ( r ) - 0 . 5 ( 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.102164 :4.101082 ^ L
This result can then be substitutedinto the corrector[Eq. (20.19)]to give the predictionat t -
l,
t , i : 2 + 4 . 7 0 1 0 8 2 ( :l )6 . 7 0 1 0 8 2 which representsa true percentrelative error of -8.18c/c.Thus, the Heun methodwithout iterationof the correctorreducesthe absolutevalue of the error by a f'actorof about2.4as comparedwith Euler's method.At this point, we can alsocomputean approximateerroras l670roR)-sl l' r" .' r :- l l
6.70r082 l^'""
Now the estimateof r'1 can be refined by substitutingthe new result back into the right-handsideof Eq. (20.19)to give 3 -2 -- ' '> -t.t
.'l
14"o
t r t D- 0 . 5 ( 6 . 7 0 1 0 8 2 ) I : 6 . 2 7 5 8I1
whichrepresents a truepercentrelativeerrorof 1.3I percentandan approximate enor0f il - 6.70r0821 16.2758 l c .l ,: l - l x 6 . 1 7 5 811 | I
1 0 0 1: 6 . 1 1 6 ( / t
20.3 IMPROVEMENTS OF EULER'S METHOD
491
The next iterationgives )l
) ^-
L-r
- 0.5(6.275g1 3 a 4rrt'xrtt 1) I :6.382129
which representsa true error of 3.03voandan approximateerror of 1.666vc. The approxirnateerror will keep droppingas the iterativeprocessconvelgeson a stable final result. In this example,after l2 iterationsthe approximateerror falls below the stoppingcriterion.At this point, the result at r : I is 6.36087,which representsa rrue relative error of 2.687c.Table 20.2 showsresultsfor the remainderof the computafionalong with resultsfor Euler's methodand for the Heun methodwithout iterationof the corrector.
Insight into the local error of the Heun methodcan be gainedby recognizingthat it is relatedto the trapezoidalrule. In the previousexample,the derivativeis a function of both the dependentvariabley and the independent variable/. For casessuchas polynomials. where the ODE is solely a function of the inclependentvariable, the predictor step [Eq. (20.l8)] is not requiredand the correctoris appliedonly oncefbr eachireration.For suchcases,the techniqueis expressedconc.iselyas .l',
-l :
. .ltri)-f [(ri.r).
l', +
/,
2
(20.20)
Notice the sirnilaritybetweenthe secondterrnon the right-handsideof Eq. (20.20)anc1the trapezoidalrule [Eq. (l7.ll)]. The connectionbetweenthe two methodscan be formally demonstratedby startingwith the ordinary differentialequation tl 1'
-: dt
fut
(20.2t)
This equationcan be solvedlbr.r'by integration: f
,,',
dt:l
I J
f t,-t J r,
.t',
frndt
(20.22)
[(t\dt
(20.23)
Ittlrlt
(20.24\
which yields
. r ' i , r- . t ' :,
. \ . i + :l l . i +
ft'tl
J,, ft'+t
/
JI,
Now, recall that the trapezoidalrule [Eq. ( l 7 . l l ) l i s d e f i n e da s
'' ["
f ( t i ) + J Q i + r ), ' f ( r ) d t : ---2
(20.25)
492
INITIAL VATUE PROBLEMS where/r : !i+1 - 4. Subsriruring Eq. (20.25)into Eq. (20.24)yields . l(titl J/rt-.tr---2
l ( t ; ,-1 1 .
(20.26)
tr
which is equivalenttoEq. (20.20).For this reason,Heun's methodis sometimesrefemed t0 as the trapezoidalrule. BecauseEq. (.20.26)is a direct expressionof the trapezoidalrule, the local truncation error is given by [r'ecallEq. (17.14)l J
r
'
f " ( \t \r
'
'
I
(20.27 )
1a
wheref is betweenti afidt;y1. Thus, tl.remethodis secondorder becausethe secondderivative of the ODE is zero when the true solution is a quaclratic.In acldition.the local and global errors are o(h3) and o(lf), respectively.Therefbre, decreasingthe step size decreasesthe error at a lasterrate than fbr Euler's rnethod.
20.3.2 The Midpoint Merhod Figure 20.6 illustratesanothersimple modificationof Euler's method.Called the nitlpoittt ntethod,this techniqueusesEuler's method to predict a value of .y af the miclpointof the interval(Fig.20.6a): , r ' i + 1 ,:/ r ) ' i * J ' ( t , . y | L
(20.28)
Then, this predictedvalue is usedto calculatea slopeat the midpoint: )"t+r1:. : .f (.ti + r lz,t-i +t p)
(20.29)
FIGURE 20.6 Grophico depictionof Heun'smethod.(o) Predictorond (b) corrector v
=
J ( . 1 i 1 11 2),' i 1 1 1 2 )
493
20.4 RUNGE.KUTTA METHODS
which is assumedto representa valid approximationof the averageslope for the entire interval.This slope is then usedto extrapolatelinearly from /1 to /111(Fig. 20.6b): r'r+t :
)'i I
(:0.-r0)
f Q i* t , ' , , ) ' i+ t n ) l t
Observethat because-r';aI is not on both sides,the corrector[Eq. (20.30)]cannotbe applied iterativelyto improvethe solutionas was donewith Heun'smethod. As in our discussionof Heun's method,the rnidpoint rnethodcan also be linked to Newton-Cotesintegrationlbrmulas.Recallfron Table 11.4 that the simplestNewton-Cotes open integrationtbrmula, which is calied the nridpointmethod,can be rcpresentedas rh
t..
I
J'trl dx =- (b - a)J'(r)
(20.31)
Jt
fbr the presenlcase, where,r1is the midpointof the interval(n, D).Usingthe nomenclature h e e x p r e s s e a d s it can
|r l t t t . . f]tdrZh.t'ftia1P) I Jti
Q0.32)
Substitutionof this formula into Eq. 120.24)yields Eq. (20.30). Thus, just as the Heun method can be called the trapezoidalrule, the midpoint method gets its name from the underlyingintegrationformula on which it is based. The midpoint methodis superiorto Euler's methodbecauseit utilizes a slopeestimate at tlre miclpointof the predictioninterval.Recallfrom our discussionof numericaldifTerentiation in Section4.3.4 that centeredfinite differencesarebetterapproximationsof derivatives thaneitherlbrward or backwardversions.In the samesense.a cenleredapproximation suchas F.q.(20.29)has a local truncationeror of O1h:1in comparisonwith the forward approximationof Euler's method,which has an en'orof O (h). Consequently,the local and global erors of the midpoint method areO(h3) andO(h2), respectively.
METHODS 2O.4 RUNGE-KUTTA Runge-Kutta (RK) methods achieve the accuracyof a Taylor series approachwithout requiring the calculationof higher derivatives.Many variationsexist but all can be cast in the generirlizedfbrm of Eq. (20.4): (20.33)
)r+r : )'i l- Qh
slope where@is called an incrcmentfunction, which can be interpretedas a repl'esentative over the interval. The incremerrtfunction can be written in generalform as Q : a r k t I a z k z+ ' ' ' ' - l a , , k , ,
(20.i4)
where the a's are constantsand the k's are (20.34a)
k 1 : J ' Q ; ,t ' i ) l ; . : f ( t i * p 1 h .) ' i * q r r k r l t ) k . , : j ' ( t i - t p z h , - , *Ii q t t k t l t i q n k z h ) : : k , , f ' ( t ; * p , t h , t - i* q , r . r k t hI q u , t . z k z hI
(20.34b) (20.3:lc)
* q,-t.u-:k,,rh)
(20.34d)
494
PROBTEMS INITIAL-VALUE Thatis, wherethep's and 4's areconstants.Notice that the ft's arerecurrencerelationships. ft1 appearsin the equationfor k2, which appearsin the equationfor ftr, and so forth.Becauseeach ft is a functional evaluation,this recurrencemakes RK methodsefficientfor computercalculations. Various types of Runge-Kuttamethodscan be devisedby employing differentnumbers of terms in the increment function as specifiedby n. Note that the first-orderRK methodwith n : I is, in fact, Euler's method.Oncen is chosen,valuesfor the a's,p's,and Thus, q's areevaluatedby settingEq. (20.33)equal to terms in a Taylor seriesexpansion. theorderof at leastfor the lower-orderversions,the numberof termsn usuallyrepresents the approach.For example,in Section20.4.1,second-orderRK methodsusean increment function with two terms (n : 2). Thesesecond-ordermethodswill be exactif thesolution to the differentialequationis quadratic.ln addition,becauseterms with /rr andhigherare droppedduring the derivation,the local truncationenor is O1ft31and the globalenoris O (h2). ln Section 20.4.2,the fourth-orderRK method (n : 4) is presentedfor whichthe 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 .
20.4.1 Second-Order Runge-Kuttq Methods versionof Eq. (20.33)is The second-order .li+l :li
l(a.k1 +a2k)h
(20.35)
where ky : k2:
J'Qi' 1t;) -t pth, li i qlhh) f Qi
(20.35c) (20.35b\
The valuestbr ar, a2, pt, and qy1 are evaluatedby settingEq. (20'35)equalto a the second-orderTaylor series.By doing this, three equationscan be derivedto evaluate are (see The three equations 2006, fbr details). Chapraand Canale, four unknown constants a 1I c t 2 : l
(20.36)
a z P r:
l12
(20.37\
a z Q t r:
I12
(20.38)
Becausewe have three equationswith four unknowns,theseequationsaresaidto be We, therefore,must assumea valueof one of the unknownsto determine underdetermined. the other three.Supposethat we specify a value for a2. Then Eqs. (20.36)through(20.38) can be solvedsimultaneouslyfbr (20.39)
ut:l-o:
I
(20.40)
/42
Becausewe can choosean infinite numberof valuesfor a2, thereare an infinitenumber of second-orderRK methods.Every versionwould yield exactlythe sameresultsif the resolutionto the ODE were quadratic,linear,or a constant.However,they yield different most Three of the sults when (as is typically the case)the solution is more complicated. commonlv usedand preferredversionsare presentednext.
20.4 RUNGE.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 , : 1 / 2 ) If a2 is assumedtobe ll2, Eqs.(20.39) and (20.40) can be solvedfor a1 : 1/2 ancJ pt : Qu : L Theseparameters,when substitutedinto Eq. (20.35),yield
/t .\'ijr:.t'i+[;ftr \z
+ ljt , -\l1 , -
/
(20.4r )
where f t r: . / ( r , , , r ' i ) -t kth) k2: f ft;* /1,,r,;
(20.41a) (20.4tb)
N o t e t h a t f r il s t h e s l o p e a t t h e b e g i n n i n g o f t h e i n t e r v a l a n d k 2 i s t h e s l o p e a t t h e e n d o f t h e interval. Conseqr-rently, this second-orderRunge-Kutta nethod is actually Heun's technique without iferationof the corrector. T h e M i d p o i n t M e t h o d( o z: and Eq. (20.35)becomes
1 1 . l t a 2 i s a s s u m e d t o bl ,et h e n a 1: 0 , p r : 4 t r : l 1 2 ,
.l'i+r: )'i'l k2h
(20-42)
where / < 1:
/(t;, r,;)
k2: f fti + h/2, J'i* krhl2)
(20.42a) (20.42b)
Thisis themidpointmethod. Rqlston'sMethod lo, : 2/3). Ralston(1962)and Ralstonand Rabinowitz(1978)determined that cltoosingo? :213 provides a minimum bound on the truncationeror for and RK algorithrus.For this version,(11: lf3 and 7r1:7tt:314, the second-order Eq. (20.35)becomes
: r',+ (]t, +f,rr) n .r',+r
(20.43)
where f t 1: / ( , t ; , r ' ; )
(20.13a)
k t : l ( t , n ' o r , r , ,l+r ' l )
(20.43b)
20.4.2 ClqssicolFourth-OrderRunge'KuttoMethod there The most popularRK methodsarefourth order.As with the second-orderapproaches, are an inlinite numberof versions.The following is the most commonly usedtbrm, and we thereforecall it the classicalfourth-orderRK ntetlrcd: . v , + :i ) ' i *
I
*2k:*2h*k+)h U(kr
(20.44)
496
PROBLEMS INITIAL-VALUE
i
*'/-*tend, hh = tend-tt,'end : })' ; :),vararqin{ kl = dydt (tt.y1i, ymid = y(1,:) + kL.*hrh,./2; : ] )' ; k2 = dydt |.tt+hY\/2,ymid,varargini ymid = y(i,:) + k2*hrh/2; k3 - dydt (tt+hh,/2,ymid,vararqrin{ : } )' ; +k3*hh; Vei-rd=y(i,:) ; } )' ; k4 = dydL(tL+hh,yend,varargin{ phi = (k1+2*(k2+k3) +k4) /6; = y(i,:) y(i+l,:) + phi*hh; r ts
ts |
rlih
.
if tt>=Lend, brea]:, end end np = np+1; tp(nP) = tt; YP(nP,:) i f tt>=t f, break, end end
= Y{1,:)
FfGURE20.8 lContinued)
However,it has an additionalfeaturethat allows you to generateoutput ln two ways, dependingon how the input variabletspan is specified.As was the casefbr Fig. 20.3,you can set rspan = tri rf l, whereri arld tf are the initial and final times,respectively. lf done in this way, the routine automaticallygeneratesoutput valuesbetweentheselimits at equalspacedintervalsh. Alternatively,if you want to obtainresultsat specifictimes.you can define r span = i t 0 , t 1 , . . . , t f I . Note that in both cases,the L span valuesmust be in ascendingolder. We can employ rk4sys to solvethe sameproblemas in Example20j. First,we can developan M-file to hold the ODEs: f unction cty = dydtsys (t, y) ^21; d y = l V ( 2 , \; 9 . B \ - 0 . 2 ' : / 6 8 . 1 * Y ( 2 ) wherey ( 1) : distance(:r)and y 12) : velocity (u). The solutioncan thenbe generatedas
>>
disp(
[1-' ]'(:,1)
0 2.0000 4.0000 6.0000 8.0000 10.0000
Y( t,2))\
0 19. r656 1L.93II !47.952r a??
(141
334.1 526
0 tB.12a6 33.0995 42 .0541 45.9344 49 .4427
504
INITIAL.VALUE PROBLEMS
We can also use t span to generate results at specific values of the able. For example, >> tsPan=t0
5 101;
>> disp(lt'
y(:,1)
y(:,2)))
000 6.0000 141.9521 10.0000 334.1626
42.A5t11 49.4021
PREDATOR.PREY MODELSAND CHAOS Engineers and scientistsdeal with a variety of problems involving sysBockgrcund. tems of nonlinear ordinary differential equations.This case study focuseson two of these applications. The first relates to predator-preymodels that are used to study speciesinteractions.The secondare equationsderived from tluid dynamics that are usedto simulatethe atmosphere. Predator-prey models were developedindependentlyin the early part of the twentieth century by the Italian mathematician Vito Voltena and the American biologist Alfred Lotka. These equations are commonly called Lotka-Volterra equations.The simplestversion is the following pairs of ODEs: dx
(20.49)
E:ax-oxY dv
(20.50)
E--'Y+axY
where x and y - the number of prey and predators,respectively,a = the prey growthrate, c : the predator deathrate, and b andd : the ratescharacterizingthe effect of the predatorprey interactions on the prey death and the predator growth, respectively.The multiplicative terms(that is, thoseinvolving xy) are what make suchequationsnonlinear. An example of a simple nonlinear model basedon atmosphericfluid dynamicsis the Lorenz equations createdby the American meteorologist Edward Lorenz: dx - - 6 x - o y
E
u
--rx-)-Jcl.
dt
dz :-bz*xy ,tt Lorenz developed theseequations to relate the intensity of atmospheric fluid motionr to tAmhArotrrra
rrariotinnc
1r enrl
7 in
thp
qnA
20.6 CASESTUDY
505
coniinued with the predator-preymodel, the nonlinearitiesstem from the simple multiplicative terms: xz and xy. Use numerical methods to obtain solutions for these equations. Plot the results to visualize how the dependentvariableschangetemporally. In addition, graph the dependent variables versus each other to seewhether any interestingpatternsemerge. The following parametervalues can be used for the predator-prey simulaSolution. tion: a : 1.2,b :0.6, c: 0.8, andd: 0.3. Employ initial conditionsof _r:2 andy : I and integratefrom t: 0 to 30, using a step size of h:0.0625. First, we can develop a lunction to hold the differential equations: function yp = predprey {t,y, a,b. c,d) y p = [ a * y ( 1 ) - b * y ( 1 ) * y ( 2 ) ; - c " y ( 2 ) + d * y ( L J* y ( 2 ) ] ; The following script employs this function to generate solutions with both the Euler and the fourth-order RK methods.Note that the function eulersys was basedon modifying the rk4sys function (Fig. 20.8).We will leavethe developmentof suchan M-file as a homework problem. In addition to displaying the solution as a time-series plot (.r and y versus /), the script also generatesa plot ofy versus x. Such phase-plane plots are often useful in elucidating featuresof the model's underlying structure that may not be evident from the time series. h = 0 . 0 6 2 5 ; t s p a n = t 0 4 0 1; y 0 = [ 2 i l i a = L . 2 ; b = 0 . 6 ; c = 0 . B; d = 0 . 3 ; I t y l = e u l e r s y s ( @ p r e d p r e y ,L s p a n , y 0 , h , a , b , c , d ) ; ) subplot (2,2,L) ;plot(t,y(:,L),t,Y {:,2),'--' (a) Euler tine plot' ) legend('prey','predaror' ) ;title(' subplot \2,2,2) ;plot (y( :, 1),v ( :,21 | title (' (b) Euler phase plane plot') tt V) = rk4sys(GpredPreY,tspan,y0,h,a,b,c,d) ; subplot (2,2,3 ) ;plot(t,y ( , ,1) , t ,Y (: ,2 ) , '--') title (' (c) RK4 rime plot') subplot \2,2,4 ) ;plot (y ( : ,1) ,V (: ,21 j title ( ' (d) RK4 phase plane plot') The solution obtained with Euler's method is shown at the top of Fig. 20.9. The time series (Fig. 20.9a) indicates that the amplitudes of the oscillations are expanding. This is reinforced by the phase-planeplot (Fig. 20.9b). Hence, theseresults indicate that the crude Euler method would require a much smaller time step to obtain accurateresults. In contrast, because of its much smaller truncation error, the RK4 method yields good results with the same time step. As in Fig. 20.9c, a cyclical pattern emergesin time. Because the predator population is initially small, the prey grows exponentia\. At a certain point, the prey become so numerous that the predator population begins to grow' leads Eventually, the increasedpredatorscausethe prey to decline. This decrease,in turn, the expected' as that, to a decreaseofthe predators.Eventually, the processrepeats.Notice
PROBTEMS INITIAL.VALUE
s06 ti, :i,
.
i
-+"t '.ii*
continued
10 20 30 ( a )E u l e tri m ep l o t
, II
I I I
I ll tl
tl tl ll t t
I I I I I
I I I
t\
I
r I
,
lt tl
,] I I
I I I t
10 20 30 (c)RK4time plot
5 10 ( b ) E u l e rp h a s ep l a n ep l o t
I I I I
t I I I
40
24 (d) RK4phaseplaneplot
FIGURE2O.9 Solution fortheLoiko-Volterro model. Euler's method ond{b)phose-plone plots, ond {o)fime-series (cJtime-series plots. RK4meihod ondld)phcse-plone
predator peak lags the prey. Also, observe that the processhas a fixed period-that is, it repeatsin a set time. The phase-planerepresentationfor the accurateRK4 solution (Fi9.20.9Q indicatesthat the interaction between the predator and the prey amounts to a closed counterclockwise orbit. Interestingly, there is a resting or critical point at the center of the orbit. The exactlocation of this point can be determined by setting Eqs. (20.49) and (20.50) to steadystate (dy ldt : dx /dt : 0) and solving for (x, y) = (0, 0) and (cld, alb). The formeris the trivial result that if we start with neither predatorsnor prey, nothing will happen.Thelatter is the more interesting outcome that if the initial conditions are set at x:cld and : populations the derivatives will zero, will remain constant. a be and the ! /b, Now, let's use the sameapproachto investigatethe trajectoriesof the Lorcnz equations with the following parametervalues:a = 10,b = 8I 3,and r : 28. Employ initial conditions
20.6 CASESTUDY
507
confinued Lorenz model "{ versus r
20 E
15
5 . 0 0 1r,' - : : 5
10
-5 -10 -
tc
-20 FIGURE20.IO Time-domoin representotion of x versus t for fheLorenz equotions. Thesolidtimeseriesis for the initiolconditions for.r is perturbed {5, 5, 5).Thedoshedlineis wheretheinitiolcondttion slighily i s 0 0 r, 5 , 5 )
of"r:.)':z:5andintegratefrom/:0to20.Forthiscase,wewillusethefourth-order RK methodto obtain solutionswith a consranttime stepof h :0.03125. The results are quite different from the behavior of the Lotka-Volterra equations.As in Fig. 20.10,the variable.r seemsto be undergoingan almostrandompatternof oscillations, bouncing around from negative values to positive values. The other variablesexhibit similar behavior. However, even though the pattems seem random, the frequency of the oscillation and the amplitudesseemfairly consistent. An interestingfeatureof suchsolutionscan be illustratedby changingthe initial condition for "r slightly (from 5 to 5.001).The resultsare superimposedas the dashedline in Fig. 20.10.Although the solutionstrack on eachother for a time, after about / : l5 they diverge significantly.Thus, we can see that the Lorenz equationsare quite sensitiveto their initial conditions.The term chaotic is used to describesuch solutions.In his original study, this led Lorenz to the conclusionthat long-rangeweatherforecastsmight be impossible!
508
INITIAL.VALUE PROBLEMS
continued (a) y versusr
10 n
(b) z versus-r
45 40 35 30
JU
a25
25 20
-10 1q
-20
10 5 -20
(c) . versus!
45 40
l5
-10
0 "r
10
10 5 -40
-20
0 )
20
FIGURE20.I I represeniot;on Phose-plone for fhe LorenzeqL.rotions (oJrr, (bl .r:, ond (cJyl proiections
The sensitivityof a dynamicalsystemto small perturbationsof its initial conditions is sometimescalled the butte(Iy ffict.The idea is that rhe flapping of a butterfly'swings might induce tiny changesin the atmospherethat ultimately leads to a large-scaleweather phenomenonlike a tornado. Although the time-series plots are chaotic, phase-planeplots reveal an underlying structure. Because we are dealing with three independent variables, we can generate projections.Figure 20.11 showsprojectionsin the x.y,xz, and the yz planes.Noticehowa structure is manif-estwhen perceivedfrom the phase-planeperspective.The solutionforms orbits around what appearto be critical points. Thesepoints are called strangeattractorsin the jargon of mathematicianswho study suchnonlinearsystems. Beyond the two-variableprojections,MATLAB's p1or3 function providesa vehicle to directly generatea three-dimensional phase-pianeplot: >> plot3 (y(: ,L) ,y(:,2) ,y(:,2) ) >> xlabel('x' ) ;ylabel('y' ) ;z1abe} ('2, ) ;grid
As was the casefor Fig. 20.1i, the three-dimensional plot (Fig20.12) depictstrajecrories cycling in a definite pattem arounda pair ofcritical points. As a final note, the sensitivity of chaotic systemsto initial conditions hasimplications for numericalcomputations.Beyond the initial conditionsthemselves,differentstepsizes or different algorithms (and in some cases,even different computers)can introducesmall differencesin the solutions.In a similar fashion to Fig. 20.10, thesediscrepancies will eventuallylead to large deviations.Someof the problemsin this chapterand in Chap.21 are designedto demonstratethis issue.
20. intt res
(a) (b) (c) (d)
PROBLEMS
conrinued
,l*lin
K,
509
t'\
30
--l
20
\l
10
--l
I I
ltI l-I I
-\|
v0
tI
_10
rl
-t_ t-
-20 -2n
40
- 4 0 -20
FIGURE2O.12 Three-dimensionol phose-plone representoiion forlheLorenz equolions generoted withMATLABs p}ot 3 iunction.
PROBLEMS 20.1 Solve the following initial value problem over the interval frorn / : 0 to 2 where r'(0) : l Display all your resultson the samegraph. dr'
-:\,/--l
20.2 Solve the following problem over the interval from -r : 0 to I usinga stepsizeof 0.25 where-l (0) - l. Display all your resultson the sameglaph. -:-
l\.
-- " r I I r ' r) '. v\ /
/i;
,1r
dt
(a) (b) (c) (d)
Analytically. Using Euler'smethodwith /i : 0.5 and 0.25. Using the midpointmethodu'ith ft : 0.5. Usingthe fburth-orderRK methodwith i : 0.5.
(a) (b) (c) (d) (e)
Analytically. Using Euler'smethod. Using Heun's methodwithout iteration. UsineRalston'smethod. Using the fburth-orderRK method.
5ro
INITIAL.VALU E PROBLEMS
2{).3 Solve the iollowing problem over the interval fi'om I :0 to 3 using a stepsizeof 0.-5wherey(0) : l. Display all your resultson the samegraph.
This is referredto as the logistic model.The analyticalsolution to this modelis y-P\l
d:"
Pmax
Po * (2nr""
Po)e
kc^t
dt
Simulate the world's population from l950 to 2050 using (a) the analytical solution, rnd (b) the fourth-orderRK Obtain your solutions with (a) Heun's method without method with a step size of 5 years. Employ the tbllowing iteratingthe conector, (b) Heun's method with iteratingthe initial conditionsand parametervaluesfor your simulation: correctoruntil s, < 0.1%. (c) the midpoint method, and px (in 1950):2,55-5 million people,kgtn:0.0261yr,and (d) Ralston'smethc)d. pmrr : 12,000million people.Display your resultsasa plot has rrany en20.J The growth of populationsof or,ganisms along with the datafiorn Prob. 20.4. gineeringand scicntiflc applications.One of the simplest 20.6 Supposethat a projectileis launchedupwardflom the rnodelsassumesthat the rate ofchange ofthe population7' is earth'ssurface.Assumethat the only force actingon theobproportionalto the existing populationat any time /: ject is the downward force of gravity. Under theseconditions. a fbrce balancecan be usedto derive dLt (P20.4.1) - : kpp ul dt, K-dlf)\o"'(R+.r12 tlt where ftq : the growth rate. The world population in millions from 1950throush2000was
where u : upward velocity (m/s), t : time (s),x: altitude (m) measuredupward from the earth's surface,g(0) : the r 1 9 5 0 r 955 I 960 r 965 lsTa 1 9 7 5 gravitationalaccelerationat the earth'ssurface(? 9.8rnAr). 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 . 3 7 z 1 0 6m ) . R e c o g n i t i n g that dxldt: u. use Euler's method to deterrninethe I t 9 B 0 t 9 B 5 I 990 I 995 2000 maximum height that would be obtained if u(t = 0; = p 4 4 5 4 4 8 5 0 5276 5686 6079 1400m/s. 20,7 Solve the lbllowing pair of ODEs over the interval (a) Assumingthat Eq. (P20.4.1)holds, use the data from frorr r : 0 to 0.4 using a step size of 0. L The initial conditions are .y(0) : 2 and ;(0) : 4. Obtain your solutionwith I 950 throughI97Oto estimateks. (b) Use the fburth-orderRK method along with the results (a) Euler's methodand (b) the fourth-orderRK method.Disof (a) to stimulate the world population from 1950 to play your resultsas a plot. 2050 with a ste-psizeof 5 yeals.Display your simulation (/l' :-21.t4et resultsalongrvith the dataon a plot. dl 20.5 Although the model in Prob. 20.4 works adequately -yawhen population growth is unlinrited,it breaksdown when ,h 3 factors such as fbod shortages,pollution, and lack of space inhibit growth. ln such cases,the growth rate is not a con20.tt The van der Pol equation is a model of an electronic stant.but can be formulatedas circuit that aroseback in the days of vacuumtubes: k, : kr,r(l - p/p,,,,,,) where t,rr, : the maximum growth rate under unlimited conditions, p - population, and /max: the rnaximurn population.Note that 2n,", is sontetimescalled the curn-ing t'upucin. Thus, at low popr"rlationdensity p {{ p^u,, k, -+ kr,,. As p approaches7r,,,"*.the growth rate approacheszero. Using this growth rate fbrmulation, the rate of changeof populationcan be modeledas dp dt
: * c , , ( l - p f 1 t , , , , 1, *t )
.t2,,
---(l at'
.t,,
-.r'') . f t.:0 dt
Given the initial conditions,1(0) : )'(0) : 1, solvethis equationfrom 1 : 0 to 10 using Euler's methodwith a step sizeof (a) 0.2 ancl(b) 0.i. Plot both solutionson thesame grrph. =6, 20.9 Given the initial conditions,)(0): I and 1,'10; solve the fbllowing initial-valueproblem from t : 0 to 4: r/l l -lQ\.-O dtt
ot
for Pl so 20 m( pl( po 5' 2tJ mi of uIr 5. 2A fb al Pr 2( Er
rh
2( pe is b1
ta Si h:
5lt
PROBLEMS Obtainyour solutionswith (a) Euler's rr.rethodand (b) the fourth-orderRK method.In both cases,usea stepsizeoi 0. I . Plot both solutionson the same graph along with the exact solution.t' : cos34. 20.10 Developan M-file 1tlsolvea singleODE with Heun's methodwith iteration.Design the M-file so that it createsa plot of the results.Test your programby using it to solvefor populationas describedin Prob. 20.5. Employ a step size of 5 1'eiirsand iteratethe correctol'until c., < 0' l7o. 20.11 Develop an M-tile lo solve a single ODE with the miclpointmethod.Designthe M-file so that it createsl plot of theresults.Testyour prograrnby using it to solvefbr population as describedin Prob. 20.5. Employ a step size of
FIGURE P2O.I5
(a) tntegratethe Lotka-Volterraequations (Section 20.6) liom 1960through2020 using the tbllowing coeflicient 5 years. v a l u e sa: : 0 . 2 3 , 0 : 0 . 0 1 3 3t,: : 0 . ' 1 a, n d d : 0 . 0 0 0 4 . 20.12 Develop an M-file to solve a single ODE with the your simulation with the data using a tinreCompare tburth-orderRK method.Design the M-file so that it creates plot series and determinethe sum of the squaresof the it solve a plot of the results.Tesl your program by using to your model and the data for both the residuals between l. size 0. a step of Prob.20.2.Employ the wolves. nrclose and 20.13 Develop an M-file to solve a systemof ODEs with (b) Developa phase-plane plot olyour solution. Euler'smethod.Designthe M-tile so that it createsa plot ol a damped spring-nrass system of motion The 20,15 20.7 theresults.Test your programby using it 1osolveProb. (Fig. P20.l5) is describedby the fbllowing oldiniuy difl-er$ith a stepsizeof 0.25. archi- entialequation: 20.14 Isle RoyaleNationalPark is a 2[0-square-mile pelagocomposedof a sinele large islandand many snrall dx d2x lr-,;+t - +k.i:0 islandsin Lake Superior'.Moose arrived around 1900, and LltdI vegeravaging b1 l9-10.their populationrtpprouched.i000. (m)' t : tation.In 19.19,wolves crossedan ice bridge from Ontario. wherex : displacementfrom equiliblium position : coefficient :20-kg damping (s), c the and mass, rn Sincethe late 1950s,the numbersof the mooseand wolves time (N . s/m). The damping coefficientc takes on three values havebeen tracked.
Yeor
I 959 I 960 196l 1962 I963 1964 I 965 1966 1967 I968 1969 t97a ) 9 7l 1973 1974
Moose
563 610 628 639 663 747 733 765 9)2 la42 I 268 )2c)5 I 439 1,193 143-5 1467
Wolves
2A 22 22 23 20 26 2B 26 22 22 l7 IB 20 23 24 3l
Yeor 5 197 1976 1977 t97B 1979 I980 r 9 8I t9B2 I9 8 3 lsB4 I985 I986 l9B7 ]9BB I989 r 990
Moose
I001 I028 9r0 863 872 932 I038 ll t5 ) t92 t268 r3 3 5 I 397 1216
Wolves
4) 44 34 40 43 50 30 l4 23 24 22 2A t6 t2 t2 l5
Yeor l 991 1992 1993 1994 I 995 1996 1997 I 998 I 999 2000 200l 2AO2 2003 lvJ4
2005 2046
Moose
t3r3 r590 lB79 l77A 2422 I 163 500 699 750 B5O 900 I 100 400 7 5Ct 544 450
Wolves
12 l2 t3 l7 t6 22 24 l4 25 2a t9 t7 l9 29 30 30
INITIAL-VALUE PROBLEMS
512
Supposethat the temperatureof the corpsewhen it wasdiscoveredwas 29.5 "C, and that two hours later,it is 23.5"C. The ambient temperatureis 20 'C. (a) DetermineK and the time of death. (b) Solve the ODE nunerically and plot the results. 20.18 The reaction A --+ B takes place in two reactorsin series.The reactorsare well mixed but are not at steady massbalancefor eachstirredtank state.The unsteady-state reactoris shownbelow: ICA' I :-(CAu-CA:)-kCAr ; atr varl(
dCB, I : -CBtIkCAt ;
FIGUREP2O.I6 A sphericol tonk.
aIt
J
dCA, I --j:-(CAt-CA)-kCAt of 5 (underdamped),40 (critically damped),and 200 (overdamped).The spring constantk : 20 N/m. The initial velocity is zero, and tlre initial displacement,r : I m. Solve this equationusing a numericalmethodover the time period 0 < / < 15 s. Plot the displacementversustirne for eachof the three valuesof the dampingcoefficient on the sameplot. 20.16 A sphericaltank has a circular orifice in its bottom through which the liquid flows out (Fig. P20.16).The flow rate through the hole can be estimatcdas
dt
T
dCBt ----:-- :
I -tCBt - CB:-l - kCB:
dl
T
where CA6 : concentration of A at the inlet of the first reactor.CA 1 - s6nqsrtration ofA at the outlet of the first reactor (and inlet of the second),CA2 - ssnssrtrationofA at the outlet of the secondreactor. CB r : concentrationof B at the outlet of the first reactor(andinlet of the second), CB2= concentrationof B in the secondreactor,r : residence time : the rate constantfor reactionofA to each fbr reactor. and k AnEii C Q.,r: produceB.lf C:Aois equalto 20, find the concentrations of,4 where Qou,: outflow (mr/st. C : an empirically derived and B in both reactorsduring their first 10 minutesof operacocfficient.A : the areaof the oril'ice(mr).3 : the gnrvita- tion. Use t : 0. l2lmin and r : 5 nrin and assumethatthe lional constant(:9.81 m/srt.und h: the depthof liquid in initial conditions of all the dependentvariablesarezero. the tank. Use one of the numericalmethodsdescribedin this 20.19 A, nonisothermal batch reactor can be describedby chapterto determinehorvlong it will takefor the waterto flow the following equations: out of a 3-m diametertank with an initial height of 2.15 m. Note that the orifice hasa diameterof 3 cm and C: 0.55. dC _ --_r\-t)/(T+273)) g 20.17 ln the investigationof a honricideor accidentaldeath, dl it is often important to estimatethe time of death.From the dT -;experimeutalobservations,it is known that the surfacetem: 1 0 0 0 e ( - r 0 / ( r + 2 7 3 ) ) C- l 0 ( T - 2 0 ) dI peratureof an object changesat a rateproportionalto the differencebetweenthe temperatureof the object and that of the where C is the concentrationof the reactantand I is thetemsurrounding environment or ambient temperature.This is peratureof the reactor.lnitially, the reactoris at I 5 andhas "C known as Newton's law of cooling. Thus, if f(/) is the tem- a concentrationof reactantC of 1.0 gmoyl-. Find theconcenperatureof the object at time /, and i".,is the constantambi- tration and temperatureol' the reactor as a function of time. ent temperature: 2(1.20The following equationcan be usedto modelthededT . :-K(T
flection of a sailboatmast subiectto a wind force:
-7.)
dt
t )
. .
(1-)'
where K > 0 is a constantof proportionality.Supposethat at time I : 0 a corpse is discoveredand its temperatureis nreasuredto be I,. We assumethat at the time of death,the oa-
-
-
tl:: where /(l)
./ (a ) -
-1,
-
)
11-
?E I : wind force, E : modulus of elasticity, I = -^A
I
-
ff^'.^n+
^f
:-^-1:-
\T^r^
]L^*
+L^.^-^^
Calc Use I: 20.2 Und diff(
! (
whe A(h gra\ outl area min h(0" h,n A(h
20.2 gair enc sho this stru dev
5r3
PROBLEMS
-_-_t_ e
d
FIGUREP2O.2I varieswith height accordingto 200;
"
n: = 8,000 kg
) ,1,,
L--]"il1\
ftr
1800kN/m
k.
2400kN/m
k1
3000kN/m
-/ T.,
C a l c u l a tteh e d e f l e c t i o ni f r ' : 0 a n dd v l d z : 0 at z = 0. Ure paranreterl'alues of L :30, E : 1.2-5x 108, arrd / : 0.05 for your computation. 20.21A ponddrainsthrougha pipeas shownin Fig. P20.21. Undera numberof simplifying assumptions. the fbllowing differentialequationdescribeshow depthchangeswith time: .tt. utt -:
--\
-,t) .tu
h, ffr
6
A(h), m2
I l7
543210 a97
A67
445
A32 01B 0
20.22 Engineersand scientistsuse mass-springmodelsto gain insight into the dynamics of structuresunder the influence of disturbancessuch as earthquakes.Figure P20.22 shorvssuch a representationfbr a three-storybuilding. For this case,the analysisis limited to horizontal motion of the canbe structure. UsingNewton'ssecondlaw,forcebalances developedfor this systemas d--tr
ltr
-rrr
,+:
dl'
Ilt|
( r{ 2 l- 1.
t ^) ---:(.\r
-
=
dt: t ) t
A'x3 /1t-
tn: K1 . m1
qT0-\-/ 7 r r= 1 2 1 0 0 0k g
qTn'-r
f--., /-q\tl Le)
' 1A(h)' u,her-e /i : depth (m), r: time (s),al: pipediarneter(nr), A\h): pond surf'aceareaas a functionoldepth (m2t.g : grar.'itational constant(: 9.81 m/s2),and c : depthof pipe outletbelow the pond bottom (m). Basedon the following area-depthtable, solve this diff'erentialequation to determine how long it takes tbr the pond to empty, given that l , ( 0 ) : 6 r n , d : 0 . 2 5m , e : I m . dr
In2= 10,000kg
(l * j('r: -.rr)
]tl|
- -\\)-
t. ^l -{.tt
m2
-
)l)
FIGUREP2O.22 Simulatethe dynamicsof this structurefrom , : 0 to 20 s, siven the initial condition that the velocity of the ground floor is d.r1/th : I m/s, and all other initial values of displacementsand velocitiesare zero. Presentyour results as two time-seriesplots of (a) displacements and (b) velocities. phase-planeplot of In addition,developa three-dimensional the displacements. 20.23 Repeattlre the sarnesirnulationsas in Section 20.6 for the Lorenz equationsbut generatethe solutionswith the rnidpointmethod. 2(1.24Perfornl the samesimulationsas in Section 20.6 for the Lorenz equationsbut use a value of r :99 .96. Compare vour resultswith thoseobtainedin Section20.6.
AdoptiveMethods ond StiffSystems
CHAPTEROBJECTIVES The primary objectiveof this chapteris to introduceyou to more advancedmethods for solving initial-valueproblemsfor ordinary differentialequations.Specific objectivesand topics coveredare r r o o . o
2I.I
Understandinghow the Runge-KuttaFehlbergmethodsuse RK methodsof difl'erentordersto provide error estimatesthat are useclto adjustthe stepsize. Familiarizing yourselfwith the bLrilt-inMATLAB lunctions for solvingODEs. Learninghow to adjustthe optionsfor MATLAB's ODE solvers. Learninghow to passparametersto MATLAB's ODE solvers. Understandingthe differencebetweenone-stepand multistepmethodsfor solving ODEs. Understandingwhat is meantby stiffnessand its implicationsfor solvingODEs.
METHODS ADAPTIVERUNGE.KUTTA To this point, we have presentedmethodstor solving ODEs that employ a constantstep size. For a signiticant nuinber of problems,this can representa selious limitation.For in example,supposethat we are integratingan ODE with a solution of the type depicted gradually. Fig. 21.1.For most ofthe range,the solutionchanges Suchbehaviol'suggests that a fairly large step size could be employedto obtain adequateresults.However,for a localizedregion from t : | .l 5 to 2.25,the solutionundergoesan abruptchange.Thepractical consequenceof dealing with such functions is that a very small stepsizewouldbe required to accuratelycapturethe impulsive behavior.If a constantstep-sizealgorithm were employed,the srnallerstep size requiredfor the region of abruptchangewouldhave a much smallerstepsizethan to be applied to the entire computation.As a consequence. of necessary-and, therefore,many more calculations-would be wastedon the regions gradualchange.
I
21.1 ADAPTIVE RUNGE-KUTTA METHODS
5r5
F I G U R E2 I . I A ^ e x o m oe o i o s o- r ' o r o f o n C D I t h a le x h i b i t so n o b r u p tc h o n g e .A u t o m o i i cs t e p s i z e nli,r>
optlons=odeset
>>
ode23(@dydt,
( 'ReLTol t0,
4),
'
, Ie-4) ; 0.5, options);
As in Fig. 2l.4b.the solvertakesmore snall stepsto attainthe increasederccuritcy.
F I G U R 2EI . 4 Solution of CDEwithMATLAB For(b),o smoiler relotive errortoleronce isuseoononence monv no'esteo:oerol,er'.
( b ) R e l T o l= 1 0 - a
2I .2 MULTISIEP METHODS
21.2
52r
MUITISTEPMETHODS The one-stepmethodsdescribedin the previous sectionsutilize information at a single point tr to predict a value of the dependentvariable.y,+rrt a future point 1;11(Fig. 21.5a). Alternative approaches, calledmultistepmethods(Fig. 21.5b), arebasedon the insightthat. once the computation has begun, valuable infbrmation from previous points is at our command.The curvatureof the lines connectingthesepreviousvaluesprovidesinformation regardingthe trajectoryof the solution.Multistep methodsexploit this informationto solve ODEs. In this section,we will presenta simple second-ordermethod that servesto demonstratethe generalcharacteristics of multistepapproaches.
21.2.1 The Non-Self-SforfingHeun Method Recallthat the Heun approachusesEuler'srnethodas apredictor[Eq. (20.15)]: . ) ) + r: ) ' i * f Q i , v ) h
(21.4)
rule as a corector [Eq. (20.17)]: and the trapezoidal - 1 ' ; 1 g:
J'; *
f (t,.y,)+ f (t,*,,y!*,) n
( 2 1. s )
Thus, the predictorand the correctorhavelocal truncationerors of O(/rr) and O(h3), respectively.This suggeststhat the predictoris the weak link in the methodbecauseit has the greatesterror.This weaknessis signilicant becausethe efficiency of the iterativecorrector stepdependson the accuracyof the initial prediction.Consequently,one way to improve Heun'smethodis to developa predictorthathasa local errorof Othi). This can be
FIGUR2 EI . 5 ond{b)multlstep beiween drfference of thefundomentol Grophicol depiction {o)one-step merhodsfor solvingODLs
522
ADAPTIVE METHODSAND STIFFSYSTEMS accomplished by usingEuler's methodand the slopeat,'!;, and extrainformationfroma p r e v i o u sp o i n t l ' r , r , a s i n lj+r :
(2r.6)
-l'r-r * .f (ti,t'i)2h
This fornrulaattainsO(h3) at the expeuseof employinga largerstepsize2h. In addition, note that the equationis not self-startingbecauseit involvesa previousvalue'of thedependent variable)r-r. Sucha valuewould not be availablein a typicalinitial-valueproblem. Becauseof this fact, Eqs. (21.5) and (21.6) are called the nort-selfsterting Heunmethod. As depictedin Fig. 21.6,thederivativeestimatein Eq. (21.6) is now locatedat themidpoint ratherthan at the beginningof the interval over which the predictionis rnade.This centering inrprovesthe local errorof the predictorto O1h31.
F I G U R2 EI . 6 A grophicol depiction of lhenonself-sfirrling Heunmethod(o)Themidpolnt method thotisused rrlethotisemployed oso predictor osc corrector {b)Thetropezoidol S l o p e- / ( r , * , . r , f * , )
S4-ri
EXA
2I .2 MULTISTEP METHODS
523
Thenon-self-starting Heunmethodcanbe summarized as (Fig.21.6a): f'j'*r: .)';'lr+ t' (t,, yi)Zn Predictor '
Corrector (Ftg.2l .6b):
1 l/+r : _1',ltr
(2t.1)
J ( r ' ' ' 1 " -) ' t (' t " ' ' t / ' " )'
/r
( 2 1. 8 )
(for.l:1,2,""nr) where the superscriptsdenotethat the correctoris applied iteratively fiom 7 : I to la to obtain refined solutions. Note that -r'j" and y'l_., are the final results of the corrector iterationsat the previoustirne steps.The iterationsare terminatedbasedon an estimateof the approximateerror, I t i-l -'i+l lo t_1.'/+l r.trr-li .!i+ r I
( 2r . 9 )
x l00lo
When lenI is lessthana prespecifiederror tolerancee., the iterationsareterminated.At this p o i n t ,T : m . T h e u s eo f E q s .( 2 1 . 7 )t h r o u g h( 2 1 . 9 ) t os o l v ea n O D E i s d e r r r o n s t r a tiendt h e followine examnle. E X A M P L E2 l . 3
N o n - S e l f - S t o r t i nHge u n ' sM e t h o d Problem Stotement. Use the non-self-starting Heun methodto performthe samecomputationsas were peltbrmedpreviouslyin Example20.2 using Heun's method.That i.s, - + n " * ' - 0 . - 5 t f r o m / : 0 t o 4 w i t h a s t e ps i z eo f l . A s w i t h E x a m p l e2 0 . 2 , integrate l" the initial conditionat / : 0 is -t': 2. However,becausewe arenow dealingwith a multistepmethod,we requirethe additionalinfbrmationthat1 is equalto -0.3929953 at / : -1. Solution. The predictortEq. (2 1.7)lis usedto extrapolate linearlyfrorn r : -l to 1: .r| : -0.3929953+ [4"'r'iu' - 0.5(2)] 2 : 5.607005 The conectorIEq. 121.8)]is then usedto conpute the varlue:
vi:2+
- 1 " 0j l i t t -r 0 . 5 ( 2 )J - l e r ) x ' I ' - t - t . s t s . o o z o o - s t
I : 6.-s493-31
2
which representsa true percentrelativeerror of -5.139o(true value :6.194631). This error is somewhatsmallerthanthe valueof -8.1Solcincun'edin the self-startinsHeun. Now. Eq. (21.8)canbe appliediterativelyto irnprovethe solution:
3 1 4.,osrtr- 0.5(6.54933 l)
l^ .tl
-
z-T
1
I : 6.313149
which representsan error of -1.927o.An approximateestimateof the error can be determined usingEq. (21.9): l" Lr((ir-
- 6.54933 6.313749 r 6.313149 | lx |
r 0 0 ":/3" . J 7 c
524
ADAPTIVE METHODSAND STIFFSYSTEMS Equation(21.8)canbe appliediterativelyuntil e,,falls belowa prespecified valueofe..As was the casewith the Heun method (recall Example 20.2), the iterationsconvelgeon a value of 6.36087(et : -2.68%). However,becausethe initial predictorvalueis more accurate.the rnultistepmethodconvergesat a somewhatfasterrate. For the secondstep,the predictoris sitt - 0.5(6.36087)2: .]'l': 2 * l4ett
13.44346
€t :9.43a/c
wlrich is superior to the prediction of 12.0826(et : l87o) rhar was computedwith the original Heun method.The first correctoryields 15.76693(e, - 6.8vc),and subsequent iterationsconvergeon the sameresultas was obtainedwith the self-startingHeunmethod; 15.30224(t, : -3.097o).As with tlre previousstep.the rate of convergenceof theconector is somewhatirrprovedbecauseof the betterinitial prediction.
21.2.2 Error Estimqtes Aside from providing increasedefficiency,the non-sell-startingHeurrcan alsobe usedto estimatethe local truncationernrr.As with the adaptiveRK methodsin Section21.1,the error estimatethen providesa criterionfbr changingthe sfepsize. The error estintatecan be derivedby recognizingthat the predictoris equivalent to the midpointrule. Hence,its localtruncationerroris (Table17.4) t,, I 6 'r. :1-- /1r ' . r ' ' " ' { € , , t : - l t ' f " t € , , )
(21.10)
where the subscript7r designatesthat this is the error of the predictor.This errorestinrate can be cornbinedwith the estimateof )'i+ r from the predictorstepto yield
Truevalue:t.,1r+
fir,.u,,,r{,,r
(21.il)
By recognizingthat the correctoris equivalentto the trapezoidalrule, a similarestimate of the local truncationerror lbr the correctoris (Table 17.2) ll E, : -ih
) ' t ' ( { ,) :
--ht
l"l€, 1
(2t.l2l
Thiserrorestimate canbecombinedwith theconectorresult.1';11 to give Truevalue: )',1r- ift3.r.(3){6,)
(21.13)
(2I.1I) .un b. ,ub,ructed Equation fromEq.(21.13) ro yield
- r,9+r 0: .y,1ir
*r,tr,t',U,
(21.14)
where{ is now between r; 1 andf . Now,dividingEq.(21.14)by 5 andreanangingthe resultgives ) , 9 +-r. r i i ' : - 1 7 r - r u r r r 1 6 )
> plot(t,y(:,1))
F I G U R 2EI . 9 forvonderPol's formsolved withode45ond(b)stiffformsolved Soutions equolion. io)Nonstiff wilh otl.:21s.
?
-l
-z
it
' lt
I I
z 1
0 *1
{
tl I
_3 1 0
--. y2
fl
1 0
/l i it
2
{
*3 1
0
2l .4 MATLABAPPLICATION: BUNGEE IUMPERWITHCORD
53t
We have only displayedthe r1 componentbecausethe resultfor .yzhasa much larger scale. Notice how this solution(Fig.2l .9b)hasmuch sharperedgesthanis the caseinFig.21 .9a. This is a visual manifestationof the "stiffness"of the solution.
21.4
MATTABAPPLICATION: BUNGEEJUMPERWITH CORD In this section.we will use MATLAB to solve for the vertical dynamicsof a jumper connected to a stationaryplatform with a bungee cord. As developedat the beginning of Chap. 20, the problem consistedof solving two coupled ODEs fbr vertical position and velocity.The differentialequationfor positionis dr
---u
(2t.26)
dt
The diff'erentialequationfor velocityis differentdependingon whetherthejumper hasfallen to a distancewhere the cord is fully extendedand begins to stretch.Thus, if the distance fallen is lessthanthe cord length,thejumper is only subjectto gravitationaland dragfbrces, dtt
' : g - signlx)31rr
(2t.27a)
nt
dT
Once the cord beginsto stretch,the spring and dampeningfbrcesof the cord must also be included: dr t'.t - 1 : 8 - s i g n ( uj)u . atntmm
t
k
v
- -(,r - L) -:-u
(21.2'7b)
The following exampleshowshow MATLAB can be usedto solvethis problem. E X A M P L E2 l . 7
B u n g e eJ u m p e rw i t h C o r d Problem Stotement. Determine the position and v^elocityof a br.rngeejumper with t h e f o l l o w i n g p a r a m e t e r sI: : 3 0 m , g : 9 . 8 1 r t / s t , n r : 6 8 . 1 k g . . , r : 0 . 2 5 k g / m . k :40 N/m, and / : 8 N.s/rn. Perfom the computationfrom t :0 to 50 s and assume that the initial couditionsarer(0) : u(0) : 0. Solution. The following M-file canbe setup to computethe right-handsidesof the ODEs: f unct ion n
Q
dyclt - bunqee (t , y, L, cd, m,k, garnma )
Ql.
cord = 0; if y(1) > L %determine lf the cord exerts a force c o r d = k / m * ( y ( 1 ) - L ) + q a m m a , / m * y( 2 ) ; end - cordl; g - sign(y(2))*cdr'm*y(2)^2 dydt - ly(2li Notice that the derivatives are returned as a column vector because this is the format .^n,'irert hrr the MA'l'T,AR solvefs.
s32
METHODSAND STIFFSYSTEMS ADAPTIVE Becausetheseequationsarenot stiff, we can useode4 5 to obtainthe solutionsanddisplay them on a plot: >> plot(t,-y( >> leqend('x
:' )
:,1),'-',t,y(:,2),' (m) ','v (m/s)')
As in Fig. 2l.10, we havereversedthe sign of distancefor the plot so that negativedistance is in the downward direction.Notice how the simulationcapturesthe jumper's bouncing motion.
F I G U R E2 I . I O Plotof distonce ond ve ocilyof o bungeejumper 40 20 0 -20
l\,",''\' l"ta-rr
-ar
-i.
-r-
-40 -60
*too'
it
10
20
30
FOUNTAIN PLINY'SINTERMITTENT Bockground. The Roman natural philosopher,Pliny the Elder, pulportedly hadanintermittent fountain in his garden.As in Fig. 21. 11, water entersa cylindrical tank at a constant flow rate Qrnandfills until the water reaches)r,igr,.At this point, water siphonsoutof the tank through a circular dischargepipe, producing a fountain at the pipe's exit. Thefountain runs until the water level decreasesto )1oy,whereuponthe siphon fills with air andthe andthe fountain stops.The cycle then repeatsas the tank fills until the water reaches)rrigh, fountain flows again. When the siphon is running, the outflow Qo,, can be computed with the following formula basedon Torricelli's law: -eou, c J2gyn r2
(21.28)
2I.5 CASE STUDY
s33
continued
----------)=)high
F I G U R2 EI . I T An intermittent founioin Neglecting the volume of water in the pipe, compute and plot the level of the water in the tank as a function of time over 100 seconds.Assume an initial condition of an emptv tank )(0) - 0, and employ the following parametersfor your computation:
Rr: 0'05m
r:
0.007m
C:0.6
)r,ign:0'l m
.Iro*: 0.025m
g = 9.81m/sz
0,' : 50 x 10-6m3/s Solution. When the fountainis running,the rate of changein the tank's volume V(m3) is determinedby a simple balanceof inflow minus the outflow:
dv 4,
:
Qi"-
(2r.29)
Qou,
where V : volume (m3).Becausethe tank is cylindrical, y : r Rll.substituting this relationshipalong with Eq. (21.28)into Eq. (21.29)gives
d1, dt
Q,,- CJTfrrrz
(21.30)
1TR;
When the fountain is not running, the secondterm in the numerator goes to zero. We can incorporate this mechanismin the model by introducing a new dimensionlessvariable siphon that equals zero when the fountain is off and equals one when it is flowing; d1, dt
Qin-siphonxCJTfinrz TR?
(21.3 r)
In the presentcontext,siphon can be thoughtof as a switch that turns the fountainoff and on. Such two-state variables are called Boolean or loqical variables, where zero is equiva- r -_-_ :._ ^^.,:,,^t^-+
+^ +rra
534
ADAPTIVE METHODSAND STIFFSYSTEMS
rl
Next we must relate siphon to the dependent variable y. First, siphon is set to zero whenever the level falls below.)!,,.. Conversely, siphon is set to one whenever the level rises above yn,*n.The following M-file function follows this logic in computing the derivative: dy = Plinyode(L,y) function g i .o b a I s i p h o n Rt - 0.05; r = 0.007; yhr - 0.1; ylo = 0.025; g = 0.6; I = 9.81; Qin = 0.00005; if y(1) = yhi elseif sipiron = 1; end * pi * r * g * y(1)) Qout - siphon * C * sqrt(2 ^ 2); dy = (Qin - Qout) ,/ (pi * Rt
"'2;
Notice that because its value must be maintained between function calls, siphon is declared as a global variable. Although the use of global variables is not encouraged (particularly in larger programs), it is useful in the present context. The following script employs the built-in ode45 function to integrate Plinyode and generate a plot of the solution; globai siphor-i siphon = 0; y0 - 0; tspan = l0 f00l; ( G P l i l t 1 z e 6 s ,t s p a n , l / 0 ) ' = o d e 4 5 Itp,l'p] plot (tp,yp) (s)') xlabel('l.iine, (m) ') ylabel('water Le.rel in tank, As shown in Fig. 2I.12, the result is clearly incorrect. Except for the original filling period, the level seems to start emptying prior to reaching -vn,rn.Similarly, when it is draining, the siphon shuts off well before the level drops to,v1qp,. At this point, suspecting that the problem demands more firepower than the trusty ode4 5 routine, you might be tempted to use one of the other MATLAB ODE solvers such as ode2 3 s or ode2 3 tb . But if you did, you would discover that although these routines yield somewhat dift'erent results, they would still generate inconect solutions. The difficulty arises because the ODE is discontinuous at the point that the siphon switches on or off. For example, as the tank is filling, the derivative is dependent only on the constant inflow and for the present parameters has a constant value of 6.366 x l0-l m/s. However, as soon as the level reaches yn,*n,the outflow kicks in and the derivative abruptly drops to -1.013 x l0-2 m/s. Although ihe adaptive step-size routines used by MATLAB work marvelously for many problems, they often get heartbum when dealing with such discontinuities. Because they infer the behavior of the solution by comparing the results0f different steps, a discontinuity represents something akin to stepping into a deep potholeon q drrk
qtreet
s35
2I .5 CASESTUDY
continued u.tz
0.1
E 0.08 J
o
c) c)
0.06
q) G
0.04
0.02
U
80
010203040506070
90
100
T i m e( s )
21.12 FIGURE The level in Pliny'sfounloinversustime os simuloiedwith ode45.
At this point, your first inclinationmight be to just give up. After all, if it's too hard for MAILAB, no reasonablepersoncould expect you to come up with a solution. Because professionalengineersand scientistsrarely get away with suchexcuses,your only recourse is to develop a remedy basedon your knowledge of numerical methods. Because the problem results from adaptively stepping across a discontinuity, you might revert to a simpler approachand use a constant,small step size. If you think about it, that's precisely the approachyou would take if you were traversing a dark, pothole-filled street. We can implement this solution strategy by merely replacing ode45 with the constant-steprk4sys function from Chap.20 (Fig. 20.8).For the scriptoutlinedabove,the fourth line would be formulated as Itp,yp]
= r k 4 s y s ( @ P l i n y o d e ,t s p a n , y 0 , 0 ' 0 6 2 5) ;
As in Fig. 21.13, the solutionnow evolvesas expected.The tank fills to yn'rnand then empties until it reaches)1o*,when the cycle repeats. There are a two take-home messagesthat can be gleaned from this case study. First, although it's human nature to think the opposite, simpler is sometimesbetter.After all, to paraphraseEinstein,"Everything shouldbe as simpleas possible,but no simpler."Second, you shouldnever blindly believeevery result generatedby the computer.You've probably "sarbase in. out" in referenceto the impact of data quality
536
ADAPTIVE METHODSAND STIFFSYSTEMS
continued
2l.l Plin
0.1 0.09 0.08
thre 21.2 ane
?c 0.07
:
I
J € 0.06
: I
a o.os
:
0)
5 o.oc
whe the r rate. (a)
> 0.03
0.02 0.01 -0
10
20
30
40
50 60 T i m e( s )
70
80
90
100
FTGUR.E 2r.r3
(b)
Thelevelin Pliny's {ounloin timeos simuloted fie versus witho smoll,consiont stepsizeusing . 0.8) r k 4 s y s f u n c l i o(nF i g 2
on the validity of computer output. Unfortunately, some individuals think that regardlessof what went in (the data) and what's going on inside (the algorithm), it's always"gospeloul" Situationslike the one depictedin Fig. 21.12 areparticularlydangerous-thatis, althou$ the output is incorrect,it's not obviously wrong. That is, the simulationdoesnot go unst& ble or yield negative levels. In fact, the solution moves up and down in the mannerof an intermittent fountain, albeit incorrectly. Hopefully, this case study illustrates that even a great piece of software suchas MATLAB is not foolproof. Hence, sophisticatedengineersand scientistsalwaysexamine numerical output with a healthy skepticism based on their considerableexperiencead knowledgeof the problemsthey are solving.
; 21.3
inter
Use ofC v(2. Corr the e and. 2r.4 inter
! a
Use
PROBLEMS
537
PROBLEMS 2l,l Repeatthe the samesinrr.rlations as in Section21.-5for Pliny's fountain. but generatethe solutionswith c.i1e2j, ode23s, and ode113. Use :subplot to developa vertical plot of the time series. three-pane 21,2 The following ODEs havebeenproposedas a model of a ne p i d e m i c : dS - : -/r.sI at dt
, :aSI at
-rl
21.5 Given tly
;
: -1oo'ooo)' r 99.999e-'
(a) Estimate the step size requiled to maintain stability r"rsing the explicitEulermethod. (b) lf .v(0) : 0, use the implicit Euler to obtain a solution from / : 0 to 2 using a step size of 0. l. 21.6 Given z1t' , : 3 f l r s i n1 . r ) + 3 c 0 s 1 tlI
dR --:rl at whereS : the susceptibleindividuals,/ = the infected,R : the recovered,a : the int'ectionrate. and r: the recovery rate.A city has 10,000people,all of whonr are susceptible. (a) If a single infectious individual entersthe city at . : 0, computethe progressionof the epidenricuntil the number of infectedindividualsfalls below | 0. LIsethe following . week) and r : 0.15/d. parameters:a : 0.0021@erson Develop time-seriesplots of all the statevariables.Also generatea phase-planeplot of S versus1 versusR. (b) Supposethat after recovery,there is ir loss of immunity thatcausesrecoveredindividualsto hecomesusceptible. This reinfection mechanism can be computed as pR, where p : the reinf'ection rate. Modify the model to include this mechanismand repeatthe computationsin (a)usingP:0.03/d. 21.3 Solve the following initial-valueproblem over the intervalfiomr:2to3:
If f (0) : 0, use the implicit Euler to obtain a solutionfrom t : 0 lo 4 usinga stepsizeof 0.4. 21.7 Given cl.tt dt
:999xt -f 1999x2
dr: : -1000rr - 2000rr dr to 0.2 I f ; r 1 ( 0 ) : x u ( 0 ) : l . o b t a i na s o l u t i o nf r o m t : 0 using a stepsizeof 0.05 with the (a) explicit and (b) implicit Euler rnethods. 21.8 The fbllowing nonlinear.parasiticODE was suggested by Hornbeck(1975): dt' - :5(l'-
r'r
AT
If the initial condition is -r.'(0): 0.08. obtain a solutionfrom /:0to5: dv (a) Analytically. - - -0.5r' l-e-' (b) Using the fourth-orderRK methodwith a constantstep dt sizeof 0.03l2-5. Use the non-self-startingHeun method with a step size ( c ) U s i n gt h e M A T L A B f u n c t i o no c t e 4 . of 0.5 and initial conditions of -r,-(1.5):5.222138 and ( d ) U s i n gt h e M A T L A B l ' u n c l i o n ooe) : . .r(2.0) : 4.143883. Iterate the corrector to s. :0.17o. (e) Usingthe MATLAB functionode23tb. your resultsbasedon Computethe percentrelativeenors for Presentyour resultsin graphicalform. theexactsolutionsobtainedanalytically:v(2.5) : 1.27-1888 21.9 Recallfrom Example I7.5 that the following humps and -r'(3.0): 2.577988. function exhibitsboth flat and steepregionsover a relatively 21.4 Solve the following initial-value problem over the short.r range, interval from I : 0 to 0.5:
dy dt
-
-r'tZ
-
+
Use the fourth-orderRK methodto predict the first value at Heun method to t:0.25. Then use the non-self:starting *-r.^
rlr^ ^--Ji^r;,-n
-o
('I-0.3)?+0.01 (x-0.9)2f0.04
)
.t
| -
n 5
NTntp. rrtO)
-
I
Determinethe value of the deflnite integral of this function between.r:0 and I using(a) the quad and (b) the ode45
s38
ADAPTIVE METHODSAND STIFFSYSTEMS
21.10 The oscillationsof a swingingpendulumcan be sitnulated with the following nonlinearmodel: ,t)a
(a) 1
l (b) (
o
:: + I sind:0 tlt) I
1
2l.l' (Fie cant
wherc 0 : the angle of displacernent,.q : the gravitational constant,and / : the pendulum lcngth. For small angular displacements.the sin d is approxirnatelyequal to d and the model can be linearizedas L1 r, uo d12-
s, _,,
F I G U RP E2 I . I 3
t
radls.Solveusinganymethod Let fr(0):0 and A1O): CI.ZS studicd in this chapter.Plot the angle versustime andthe thesecondangularvelocity versustime. (Hint: Decompose order ODE.)
Use ode45 to solve fbr d as a function of time for both the linear and nonlinear models wherc I :0.6 m and g:9.81nr./s2. First, solve for thc case where the initial condition is fbr a small displaccment (d : :r/8 and d0 I dt :0). Then repcatthe calculationfbr a largedisplace- 21.14 Given the first-ordcrODE: ment (A : r 12 andd0 I dt : 0). For eachcase,plot the linear and nonlinearsimulationson the sameplot. dt 21.11 The following system is a classic cxample of stiff -r(t:0):4 ODEs that can occur in the solution of chemical reaction kinetics: Solve this stiff diff'erential equation using a numerical method over the time period 0 < r < 5. Also solveanalyti9:-o.ut,cr -looor:rc3 cally and plot the analytic and numericalsolutionfor both dt the tast transientand slow transitionphaseof thetimescale. dc.- -2-500crcr 21.L5 Solvc the following differential equationfrom dr t :0Io 2 -'* : dt
- 0 . 0 1 3 t . -r 1 0 0 0 r ' r c -r 2 5 0 0 c 2- rt
Solve theseequationsfrom l : 0 to 50 with initial conditions c1(0) : cr(0) : I and c.(0) : 0. If you have accessto MAILAB software,use both standard(e.g.,oce45) and stiff (e.g.,ode23s) functionsto obtainyour solutions. 21.12 The following second-orderODE is consideredto be stiff: 11
r
-a-. r_- t o o r ; - t o oor ox Solve this differential equation (a) analytically and (b) numerically fbr .r : 0 to 5. For (b) use an implicit apploach with /z : 0.5. Note that thc initial conditionsare .v(0) : 1 and 1'(0) : 0. Display both results graphically' 21.13 Consider the thin rod of length 1 moving in the x-,r'planeasshownin Fig. P2l .13.Thc rod is fixed with a pin on one end and a massat the other.Note that I : 9.81 m/sl and 1 : 0.5 m. This systcmcan be solvedusing ..e e-L0
-n
dt -l0r - : ot with the initial condition 1'(0): l. Use the followingtech(b) theexniquesto obtain your solutions:(a) analytically, For plicit Euler rnethod,and (c) the implicit Eulermethod. (b) and (c) use/r : 0. I and 0.2. Plot your results. 21.16 The Lotka-Volterra equations describedin Secthat tion 20.6 havebeenrefined to includeadditionalfactors impact predator-preydynamics. For example,over and above predation,prey population can be limitedby othet factors such as space.Spacelimitation can be incorporated into the model as a carrying capacity (recallthe logi$ic model describcdin Prob. 20.5) as in dx
-
r
,{r
:rr(l-V)x-bxt
dt
d), dr
: -61' f r/_ry
where K : the carrying capacity. Use the sameparameter valuesand initial conditionsas in Section20.6to integrate : 0 to 100usinsode45.
4( ! d
wher the ut pute r the fc
s39
PROBLEMS (a) Employ a very large value of ;g: 10" to validatethat you obtainthe sameresultsas in Section20.6. (b) Conrpare(a) with the more realisticcarryingcapacityof K : 2QO.Discussyour results. 21.17Two massesare attachedto a wall by linearsprinus tFig.P2l. I 7). Forcebalancesbasedon Newton'ssecondlaw canbe written as d--{I
: -If
,l,t d2.rt
dt'
mt -
k.
1 , r -, t . t ) , 1 ! ( r ,
----:(-t2 m2
n 1|
Ll
w1
L2
w2
- . \ 1- w ' - L 2 ) F I G U R EP 2 I . I 7
-.t1 -trrl -12)
t t ) , : y l . : 5 , a n dL t : L t : 2 . S e tt h ei n i t i a cl o n d i t i o nass : n-rass, wherek: the springconstants.,n L: the lengthof "\t: Lt tutdx, : L, I u, + L2+ 6. Perfbrrnthe simulation theunstretchedspring.and w : the width of the mass.Com- from / : 0 to 20. Constructtime-seriesplots of both the disputethe positionsof the massesas a functionof time usin-t placementsand the velocities.In adclition,producea phasethefbllowing parametervalues:t, : ft: : 5, mt : m):2, planeplot of .r, versusr,.
i"
:i; :li' i, ,'
Boundory-Voue Problems
CHAPTER OBJECTIVES The primary objectiveof this chapteris to introduceyou to solving boundary-value problemsfor ODEs. Specific objectivesand topicscoveredare . o . . . . . .
Understandingthe differencebetweeninitial-valueand boundary-valueproblems Knowin-qhow to expressan nth-orderODE as a systernof n first-orderODEs. Knowing how to implementthe shootingmethod1brlinear ODEs by usinglinear ''shots." interpolationto generateaccurate Understandinghow derivativeboundaryconditionsare incorporatedinto the shootingmethod. Knowing how to solve nonlinearODEs with the shootingmethodby usingroot locationto generateaccurate"shots." Knowing how to implementthe finite-differencemethod. Understandinghow derivativeboundaryconditionsare incorporatedinto the finite-differencemethod. Knowing how to solve nonlinearODEs with the finite-differencernethodby using root locationmethodsfor systens of nonlinearalgebraicequations.
YOU'VEGOT A PROBTEM o this point, we havebeencomputingthe velocity of a free-fallingbungeejumperby integratinga singleODE: tlu -; :8 atm
-
c,t t --:u-
tll.lt
Supposethat ratherthan velocity,you are askedto determinethe positionof thejumperas a firncrinrr
nf time
Onp
urev tn
22.'I INTRODUCTION ANDBACKGROUND
541
of distance: dr dr
(22.2)
Thus, by solvingthe systemof two ODEs representedbyEqs. (22.1) and(22.2),we can simultaneously deterrnine both the velocityand the position. However, becausewe are now integratingtwo ODEs, we require two conditionsto obtain the solution.We are aheadytamiliar rvith one way to do this for the casewhere we have varluesfor both positionand velocity at the initial time; x(r:0):r:, tr(l:0):ur Given such conditions,we can easily integratethe ODEs using the numericaltechniques describedin Chaps.20 and 2l . This is refened to ils an initial-valueproblem. But what if we do not know valueslbr both positionand velocity at / : 0? Let's say that we know the initial position but rather than having the initial velocity, we want the .jumperto be at a specifiedpositionat a later time. In other words: .t(l : 0) :.v; x(t:t1):ry Becausethe two conditionsare given at ditferentvaluesof the independentvariable,this is called a boundary-valueproblem. Such problemsrequire speciaisolution techniques.Son.reof theseare relatedto the methodsfor initial value problemsthat were describedin the previoustwo chapters.However, others emplov entirely different strategiesto obtain solutions.This chapteris designedto introduceyou to the more comnlon of thesemethods.
22.1
INTRODUCTION AND BACKGROUND 22.1.1 Whot Are Boundory-VolueProblems? An ordinary differentialequationis acconrpaniedby auxiliary conditions,which are used to evaluatethe constantsof integrationthat result during the solutionof the equation.For an rzth-orderequation,n conditionsare required.If all the conditionsare specifiedat the samevalue of the independentvariable,then we are dealingwith an ilritial-vaLueproblem (Fig.22.1a).To this point,the materialin PartSix (Chaps.20 and 21) hasbeendevotedto this type of problem. ln contrast.there are often caseswhen the conditionsare not knor.vnat a singlepoint but rather are given at different valuesof the independentvariabie.Becausethesevalues are often specifiedat the extremepoints or boundariesof a system,they are customarily pntblerns(Fig.22.lb). A varietyof significantengineeling referredIo as bounrlary--value applicationstall within this class.In this chapter,we discusssomeof the basicapproaches
542
BOUNDARY-VALUE PROBLEMS
4't : , I r ( r .] r ' , r ' : l [ dy. V
= lzQ':"v):)
w h e r e a t r - 0 . r I . - J r . oa n d \ ' 2 - ) . r ,
I n i t i a lc o n d i t i o n s
0 {al a-\
ox-
= lQ'Y)
w h e r e a t r : 0 , . y- _ ) o x:1")"=YL
)
Boundary condition
\ YL
Boundary *\l,o condition
(b)
FIGURE 22.I lnitiolvolue versus problems boundoryvolue problem where o llheconditions {o)Aninrtiol-volue
o r e s p e cl i e d c ' l I e s o r n ev o l u eo f t h e i n o e p e n o e nv' o ' i o o l e .( b )A o o r ^ d c . y - v o l upe' o b l e r where lhe conditionsore specifiedoi differentvoluesof the independentvorioble.
22.1.2 Boundory-VolueProblemsin Engineeringond Science At the beginningof this chapter,we showedhow the determinationof the positionandvelocity of a falling objectcould be formulatedas a boundary-valueproblem.For thatexample, a pair of ODEs was integratedin time. Although other time-variableexamplescanbe developed,boundary-valueproblemsarisemore naturallywhen integratingin space.This occursbecauseauxiliary conditionsare often specifiedat differentpositionsin space. A casein point is the simulationof the steady-state tenperaturedistributionfor a long, thin rod positionedbetweentwo constant-temperature walls (Fig. 22.2).The rod's crosssectionaldimensionsare small enough so that radial temperaturegradientsare minimal and, consequently,temperatureis a function exclusivelyof the axial coordinatex. Heatis transfeffedalong the rod's longitudinal axis by conductionand betweenthe rod andthe surroundinggas by convection.For this example,radiationis assumedto be negligible.r
lWe incornorate radiation into this nrohlem
22.I INTRODUCTION ANDBACKGROUND
s43
A
i++l
-r
,r + A-r
FIGURE22.2
A h e o to o o ' c e I o o d i f f e ' e n ' oe e ' n e 1 o f o h e o ' e d ' o ds - b ; e . t - oc o ' r dr c l i o ' o ^ o . o n \ e c i o ^
As depictedin Fig. 22.2, a heatbalancecan be taken arounda differentialelementof thicknessA,t as
Q: q(r')A,.- q(x + Ar)A,.+ hA,(Tn - T)
(22.3)
whereq(.r) : flux into the elementdue to conduction[J/tm:'s)]: q(r * Ax) : flux out area[m2] : 7T12, r : the of the elementdueto conductionU/(m2' s)]; A,. : cross-sectional ' K radius[m]; h : theconvectionheattransfercoefTicient [J/1m2 ' s)l: A' : theelement's surfacearea [m2] - 2n r Lr Z- : the temperatureof the surroundinggas [K]; and Z : the rod's temperature[Kl. Equation(22.3)canbe dividedby the element'svolume (1rr'A,x) to yield ./(r)-./(r + A'{) 0_ + 2 h t T __ T l r A-r Taking the limit Ax -> 0 gives da
2h
0:__;+_(T__T)
(22.4)
arr
The flux can be relatedto the temperaturegradientby Fourier's law: q:-K,
dT
(22.s)
ax y / ( s . m ' K ) l . E q u a t i o n ( 2 2 . 5 )c a n b e d i f w h e r e , t: t h e c o e f f i c i e n t o ft h e r m acl o n d u c t i v i tU (22.q, and the resultdivided by k to yield, ferentiaredwirh respectro x, substitutedinto Eq. ,!27
0:T+h'tT--Tl
(22.6)
dx-
whereft' : a bulk heat-transferparameterreflectingthe relativeimpactsof convectionand 2] : 2hl?k). conduction[Equation(22.6)representsa mathematicalmodel that can be usedto computethe temODE, two conditions
544
BOUNDARY.VALUE PROBLEMS arerequiredto obtain a solution.As depictedinFig.22.2, a comnroncaseis wherethetemperaturesat the endsofthe rod areheld at fixed values.Thesecan be expressed mathematically as T (0) : 7,, T(L) : T|' The fact that they physicallyrepresentthe conditionsat the rod's "boundaries"is theorigin of the terminology:boundaryconditions. Given theseconditions,the model representedbyEq. (22.6) can be solved.Because this particularODE is linear,an analyticalsolution is possibleas illustratedin thefollowing example.
EXAMP2 L2 E. I Anolyticol Solutionfor o Heqted Rod Problem Stotement. Use calculus to solve Eq. (22.6) fbr a 10-m rod with h'= 0 . 0 5 m - 2 [ / z : l J l ( m 2' K ' s ) , r : 0 . 2 m , k : 200 J/(s . m ' K)1, T* = 200K, andthe boundaryconditions:
z(0) : 300K
z(10): 4s0K
Solution. This ODE can be solvedin a numberof ways.A straightforward isto approach first expressthe equationas d2T --ltT:-17'ln Becausethis is a linear ODE with constantcoefflcients,the generalsolutioncanbereadily obtained by setting the right-hand side to zero and assuminga solutionof the form T : eL' . Substitutingthis solutionalong with its secondderivativeinto thehomogeneous form of the ODE yields 7 2 n L_xh , d , r : 0 which can be solvedfor L : +JA
Thus. the gener-alsolutionis
T:Ae^'+Be-i' whereA and B are constantsof integration.Using the methodof undetermined coefficients we can derive the particularsolution T : T-. Therefore,the total solutionis T :To
* Ae,' 1 Br' "
The constantscan be evaluatedby applying the boundaryconditions 7,,:Tn+A+B Tr,: Tn I Ad'L I Be-t'r Thesetwo equationscan be solvedsimultaneouslyfor
A-
(7,-Tn)e-^t
-(7,,-Tn\
e,Lr, _ d,L (To-T*)-(7.-Tn)/'L
22.2 IHESHOOTING METHOD
545
I.K
400
10 "r,m
FIGURE22.3 fortheheoted rod Anolytico solulion
Substitutingthe parametervalues from this problem gives A:20.461 1 and B: 19.5329.Therefbre.the final solutionis (22.1) r : 100 + 20.4611n. ttos' + lg .532ge ffi5' As can be seeninFig.223, the solutionis a smoothcurve connectingthe two boundary temperatures.The temperaturein the middle is depresseddue to the convectiveheat lossto the coolersunoundingsas. for solving the same In the following sections,we will illustratenumericalapproaclres problem we just solvedanalyticallyin Example22.l.The exactanalyticalsolutionwill be useful in assessingthe accuracyof the solutionsobtainedwith the approximate,numerical methods.
22.2
THESHOOTINGMETHOD The shootingmethod is basedon conveftingthe boundary-valueproblem into an equivalent initial-valueproblem.A trial-and-errorapproachis then implementedto developa solution for the initial-valueversiorlthat satisfiesthe given boundaryconditions. and nonlinearequations.it is Although the methodcan be employedfor hi-eher-order nicely illustratedfbr a second-order,linear ODE such as the heatedrod describedin the previoussection: 0:
d)T a_*.
alttTo-T)
(22.8\
subjectto the boundaryconditions
r (0) - f,, T(L):Tb We convertthis boundary-valueproblem into an initial-valueproblem by defining the rate of changeof temperature,or gradient,as dT
546
BOUNDARY-VALUE PROBLEMS andreexpressing Eq.(22.8)as )tt . ( 2 2l.0 )
dx
Thus, we have convertedthe singlesecond-order equation(Eq. 22.8) into a pair of firstorder ODEs (F.qs.22.9and22.10). If we had initial conditionsfor both Z and:. we could solvetheseequationsasan initialvalue problemwith the methodsdescribedin Chaps.20 and 21. However,because we only havean initial vafi"re tbr one of the variablesI (0) : 7.,we simply mtrkea guessfor theother :(0) : {.r ord then perforrnthe integration. After perfbrmingthe integration,we will have generateda value of I at theendof the interval,which we will call 4r. Unlesswe areincrediblylucky,this resultwill differfrom the desiredresult Zr,. Now, let's say that the value of f,1 is too high (Tr,t > Tt) , it would makesensethara lower value of the initial slope z(0) - ;(2 might result in a better prediction.Usingthis new guess,we can integrateagainto generatea secondresultat the end ofthe intervalf,,2. We could then continueguessingin a trial-and-errorfashionuntil we arrivedat a guessfor :(0) thatresultedin the corect valueof T (L) : 7r. At this point, the origin of the name.shootirtg methodshouldbe prettyclear.Justasyou would adjustthe angleof a cannonin orclerto hit a target,we are adjustingthe trajectoryof our solution by guessingvaluesof z(0) until we hit our targetT(L) : Ty. Although we could cefiainly keep guessing.a more efficient strategyis possiblefor linear ODEs. In suchcases,the trajectoryof the perf-ectshot 1, is linearly relatedto theresultsof our two erroneousshots(s,,1,4r) and (zoz,Tnz).Consequently, linearinterpolation can be ernployedto arrive at the requiredtrajectory: i,, : l,,r + ;f -I bal r 7 1 , T 1 ' 1 ) 1b2-
(22.1r)
The approachcan be illustratedby an example.
EXAMPLE 22.2
The Shooting Method for o LineorODE Problem Stotement. use the shootingmethod to solve Eq. (22.6) for the samecondit i o n s a s E x a r n p l e2 2 . 1 : L : l 0 m , h ' : 0 . 0 5 m - 2 . T - : 2 0 0 K , f(0):300K, and 7(10) : 400K. Solution. Equation(22.6)is first expressed as a pair of first-orderODEs: .IT
E
:,.
.tat^
_ : _0.05(20tJ_ T) ax Along with the initial value for temperarureZ(0) : 300 K, we arbitrarilyguessa valueof ia | : -5 K/m for the initial value for r (0). The solutionis thenobtainedby integrating the
:"9,:""11;Y::il,f^'Li:.y':l.r*LAB's ocre45 ru'-ion bvfirst l:.11-":_10u-'^';".1'l^:
22.2 IHE SHOOTING METHOD
547
L u - ] e r. o r . . i y = E x 2 l 0 2 rx,y I dy= ly (2) ; -0.05" (2oo-y (t) )l ; We can then generatethe solutionas >>
Tbl=Y (lenSth
(Y) )
569.1539
Thrrs.we obtaina value at the end of the intervalof 761: 569.7539(Fig. 22.4a), which differs fronl the desired boundary condition of T6:400. Therefore,we make another gu€ss in2 : -20 and perform the computation again. This time, the result of T t z : 2 5 9 . 5 1 3 1 i s o b t a i n e d( F i g .2 2 . 4 b ) .
FIGURE22.4 Temperofure cistonceim.)compuied wrththeshoctingmethod;io) thefirsr"shot," {K)versus {b)theseconC"shoi,"ond lc) thefinolexoci"hit."
548
PROBLEMS BOUNDARY-VALUE Now, becausethe original ODE is linear,we can useEq. (22.11)to determinethecorrect trajectoryto yield the perfectshot:
-20 - (-5) (4oo- 569.7539) : -13.2075 2 5 9 . 5 1 3- 1569.1539 This value can then be usedin conjunctionwith ode45 to generatethe conect solution,as depictedinFig.22.4c. Although it is not obvious from the graph, the analyticalsolution is also plottedon Fig.22.4c. Thus, the shootingmethodyields a solution that is virtually indistinguishable from the exactresult.
22.2.1 Derivqtive Boundory Conditions The fixed or Dirichlet boundaryconditiondiscussedto this point is but oneof severaltypes that are used in engineeringand science.A common alternativeis the casewherethederivative is given. This is commonly referredto as a Neumannboundarycondition. Becauseit is alreadysetup to computeboth the dependentvariableandits derivative, incorporating derivative boundary conditions into the shooting method is relatively straightforward. ODE Justas with the fixed-boundaryconditioncase,we first expressthe second-order as a pair of first-orderODEs. At this point, one of the requiredinitial conditions,whether the dependentvariableor its derivative,will be unknown. Basedon guessesfor themissing initial condition,we generatesolutionsto computethe given end condition.As withthe initial condition, this end condition can either be for the dependentvariableor its derivative. For linear ODEs, interpolationcan then be usedto determinethe valueof themissing initial condition requiredto generatethe final, perfect "shot" that hits the end condition.
EXAMPLE 22.3
T h e S h o o t i n gM e t h o d w i t h D e r i v o t i v eB o u n d o r yC o n d i t i o n s Problem Stotement. Use the shooting method to solve Eq. (22.6) for the rod in E x a m p l e2 2 . 1 :L : l 0 m . f t ' : 0 . 0 5 m - 2 [ f t : I J / ( m 2. K . s ) . r : 0 . 2 m . k : 2 0 0 J l (s ' m ' K)1, f- : 200 K, and 7(10) : 400 K. However,for this case,ratherthanhaving a fixed temperatureof 300 K, the left end is subjectto convectionas in Fig.22.5.For
FIGURE 22.5 oi theolher. A rod wiih o conveciiveboundoryconditionoi one end ond o fixed temperoture
Convection T-
22.2 TIIESHOOTING METHOD I
549
simplicity, we will assumethat the convectionheattransfercoefficientfor the end areais the sameas for the rod's surface. Solution. As in Example22.2,Eq. (22.6)is firsr expressed as dT dx n7
ia x : -0.0sQ00- 71 Although it might not be obvious,convectionthroughthe end is equivalentto specifying a gradientboundarycondition.In order to seethis, we must recognizethat because the systemis at steadystate,convectionmust equal conductionat the rod's left boundary (-r : 0). Using Fourier'slaw (8q.22.5) to representconduction,the heatbalanceat the end can be formulatedas
h A , ( T o - f ( 0 l): _ - f e , # 0 1
(22.12)
This equationcan be solvedfor the gradient dTh - 7 *) E (0 ): t(7 (0 )
(22.13)
If we guessa value for temperature,we can seethat this equationspecifiesthe gradient. The shootingmethod is implementedby arbitrarily guessinga value for z(0). If we choosea value of Z(0) :Tnt:300K, Eq. (22.13)then yields the initial value for the gradient z n r:
dT.^ ;(0)
1 : -(300
- 2 0 0 ): 0 . 5
The solution is obtainedby integratingthe pair of oDEs from r : 0 to 10.We can do this with MATLAB's ode45 function by first settingup an M-file to hold the differentialequations in the samefashionas in Example22.2.we can then generatethe solutionas > > [ t , y ] = o d e 4 5 ( @ E x 2 3 A 2 l, 0 1 0 1 , t 3 0 O , O . 5 l ) ; >> Tb1=y(lengrh(y) ) 6 B 3 .s 0 B B As expected,the value at the end of the interval of 71,1:683.5088K differs from the desiredboundaryconditionof T6 - 400. Therefore,we make anotherguessf,2 : 150 K, which correspondsto Znz: -O.25, and perfbrm the computationagain. >>
Tb2 =y (length
-47.1544
(y ) )
550
BOUNDARY.VALUE PROBLEMS
7'K 400
300
200
10 r,m
FIGURE 22.6 The solutionof o second-order CDE with o conveciiveboundorycondltionot one end ond o fixed lemoerolureot the other.
Linear interpolationcan then be employedto computethe correctinitial temperature 1 5 0- 3 0 0
4,:300f
- 683.5088) : 241.3643 -- 4 1 . 7 5 4 4 K - 6 8 3 . s 0 8@00 8'
which correspondsto a gradientof Zu - 0.2068.Using theseinitial conditions,ode45can be employedto generatethe conect solution,as depictedin Ftg. 22.6. Note that we can verify that our boundaryconditionhasbeensatisfiedby substituting the initial conditionsinto Eq. (22.12)to give |
JJK ir x(0.2m)'x(200K-241.3643K):-200 ---. rr xt0.2m)'x0.2068* mrKs mKs m
which can be evaluatedto yield *5.1980J/s: -5.1980J/s. Thus, conductionandconvectionareequalandtranSferheatoutoftheleftendoftherodatarateof
22.2.2 The Shooting Merhod for Nonlineor ODEs For nonlinearboundary-valueproblems,linear interpolationor extrapolationthroughtwo solutionpoints will not necessarilyresult in an accurateestimateof the requiredboundary condition to attain an exact solution.An alternativeis to perform threeapplications ofthe shooting method and use a quadratic interpolating polynomial to estimatethe proper boundarycondition. However,it is unlikely that such an approachwould yield theexact answer,and additionaliterationswould be necessaryto home in on the solution. Another approachfor a nonlinearprobleminvolvesrecastingit as a rootsproblem.Recall that the generalgoal of a rootsproblemis to find the value of r that makesthefunction f (x) :0. Now, let us usethe heatedrod problemto understandhow the shootingmethod can be recastin this form. First, recognizethat the solution of the pair of differentialequationsis alsoa "function" in the sensethat we guessa conditionat the left-handend of the rod zn, andtheintegration yields a predictionof the temperatureat the right-hand end 76.Thus, we canthink of the integrationas
Tu: f k,)
METHOD 22.2 THESHOOTING
551
That is, it representsa processwherebya guessof 2,, yields a predictionof 16. Viewed in this way, we can seethat what we desireis the value of ;,, that yields a specificvalue of 71,. I1',as in the example,we desireTo : 400. the problem can be posedas 400 : .l k.) By bringing the goal of 400 over to the right-handside of the equation,we generatea new function res(zn)that representsthe difference,or residual,betweenwhat we have, l (2,,), and what we want. 400. r e s ( 7 . , , ) J: ' k a ) - 4 0 0 If we drive this new function to zero, we will obtain the solution.The next exampleillustratesthe approach. The Shooting Method for Nonlineor ODEs
EXAMPLE22.4
Problem Stotement. Although it served our pu{posesfor illustratingthe shooting rnethod,F,q.(22.6) was not a completelyrealisticmodel for a heatedrod. For one thing, such a rod would lose heatby mechanismssuchas radiationthat are nonlinear. Supposethat the following nonlinearODE is usedto simulatethe temperatureof the heatedrod: (1:T
0 : = + h,(T&_ Tt + o,'q! _ ra1 dx' ' r ' ,
, ,
where o' : a bulk heat-transferparameterreflectingthe relative impactsof radiation and conduction:2.J x l0 e K 3 m-2. This equationcan serveto illustratehow the shooting method is used to solve a two-point nonlinear boundary-valueproblem. The remaining 2, problem conditions are as specified in Example 22.2: L : 10 r7t,h':0.05 m 7 - : 2 0 0 K , f ( 0 ) : 3 0 0 K , a n d7 ( 1 0 ) : 4 0 0 K . Solution. Just as with the linear ODE, the nonlinear second-orderequationis first exoressedas two first-orderODEs: tlT d: . ' : - 0 . 0 5 t 2 0 0 T ) - 2 . 1x l 0 u ( 1 . 6x l O q- 1 4 . ) tlx
An M-file can be developedto computethe right-handsidesof theseequations: dy=flydxn (x, Y) function (200-y(1,) (2) ;-0.05*
dy=ty
)
2.1e-9*
(1. 6e9 y(1)^
) I ;
Next, we can build a function to hold the residual that we will try to drive to zero as r=res (za) function fx,yl=ode45(@dydxn, t0 101, l3Aa zal); r=y (length (r), 1) -400 ;
BOUNDARYVALUE PROBLEMS
552
lK 400 300
10 r. rn
FIGURE22,7 Theresult to solve of usingfie shooting method a nonineorprobem
Notice how we use the ode45 function to solvethe two ODEs to generatethe temperature at the rod's end:y (length (x) , 1) . We can then find the root with the f zero function: >>
fzero
A1
(@res, -50)
?.41,1
T h u s , w e s e e t h a t iw f e s e t t h e i n i t i atlr a j e c t o r y z ( 0:) - 4 1 . 1 4 3 1 .t h e r e s i d u a l f u n c t i o n w i l l be driven to zero and the temperatureboundarycondition Z(10) :400 at the endof the rod should be satisfied.This can be verified by generatingthe entire solutionandplotting the temperaturesversusx: > > l x , y l = o d e 4 5 ( @ d y d x n ,t 0 1 0 1 , 1 3 0 0 f z e r o ( @ r e s , - 5 t l ) l ) ; >> plot(x,t'(:,1))
The resultis showninFig.22.7 alongwith the originallinearcasefionr Example22.2. As expected,the noniinearcaseis depressedlower than the linear model due to theadditional heatlost to the surroundinggas by radiation.
22.3
FINITE.DIFFERENCE METHODS The most colrmon altemativesto the shootingmethodarefinite-ditlerenceapproaches. In these techniques,tinite differences(Chap. l9) are substitutedfor the derivativesin the original equation.Tlrus, a linear differentialequationis transformedinto a setof simultaneousalgebraicequationsthat can be solvedusing the methodsfrorn PartThree. We can illustratethe approachfbr the heatedrod model (Eq.22.6): .l)r
o:;+h'(T--T)
(22.t4)
The solutiondomain is first divided into a seriesof nodes(Fig. 22.8).At eachnode,finitedifferenceapproximationscan be written fbr the derivativesin the equation.For example,
22.3 FINITE-DIFFERENCE METHODS
5s3
a-{
FIGURE22.8 Inorderto implement thefinitedifference opprooch, theheoted rodisdivided intoo series o{ nodes
at nodel. the secondderivativecan be represented by (Eq. 19.15):
d)T
Ti-t -2Ti *Ti+t
dx2
Ax2
(22.1s)
This approximationcan be substitutedinto Eq. (22.14)to give Ti r-2Ti aTi*1 A.rl
+ h ' ( T n* 4 ) : 0
Thus, the differential equationhas been convertedinto an algebraicequation.Collecting termsgives -Ti
t I Q + h' Lx2)Ti - Ti+r : h' L..r2Tn
(22.16)
This equationcan be written for eachof the rr - I interior nodesof the rod. The first and last nodesTs and 7,, respectively,are specifiedby the boundaryconditions.Therefore,the problem reducesto solving n - 1 simultaneouslinear algebraicequationsfor the n - I unknowns. Before providing an example,we should mention two nice featuresof Eq. (22.16). First, observethat since the nodes are numberedconsecutively,and since each equation consistsof a node(i) and its adjoiningneighbors(i - I andi + 1), the resultingsetoflinear algebraicequationswill be tridiagonal.As such.they can be solved with the efficient algorithmsthat areavailablefor suchsystems(recallSec.9.4). Ftrrtl.rer, inspectionof the coefficientson the left-handsideof Eq. (22.16)indicatesthat the systemof linearequationswill alsobe diagonallydominant.Hence,convergentsolutions can also be generated with iterativetechniques like the Gauss-Seidel method(Sec.12.l). E X A M P L E2 2 . 5
F i n i t e - D i f f e r e n cAep p r o x i m o t i o no f B o u n d o r y - V o l uP er o b l e m s Problem Stotement. Use the finite-differenceapproachto solve the sameproblem as in Examples22.1and22.2.Usefour interiornodeswith a segmentlengthof Lx : ) 111. Solution. Employing the parametersin Example 22.1 and Ax:2m, Eq.(ZZ. | 6) fbr eachof the rod's interior nodes.For example,for node l: -To l2.2Tr - Tz: 40 Substitutingthe boundaryconditionIt : 300 gives 2 . 2 7 1- T z : 3 4 0
we can write
554
BOUNDARY-VALUE PROBLEMS After writing Eq. (22.16) for the other interior nodes,the equationscan be assembled irr matrix form as
-r f2.2 .2 l-1 2 -r 0 | 0 L0
0 -r 2.2 -1
: :'lli{it}
Notice that the matrix is both tridiagonaland dia-qonallydominant. MATLAB can be usedto seneratethe solution:
4 0 4 4 r ) l' ; T= 283.265t) 283.1853 299 .14r6 336.2462
Table 22.1 provides a comparisonbetweenthe analyticalsolution (8q.22.1) andthe numericalsolutionsobtainedwith the shootingrnethod(Example22.2) andthe finitedifferencemethod (Example 22.5). Note that althoughthere are some discrcpancies, the numericalapproaches agreereasonablywell with the analyticalsolution.Further,thebiggest discrepancyoccursfor the finite-differencemethoddue to the coarsenode spacingwe used in Example22.5.Betteragreementwould occur if a f iner nodal spacinghad beenused. TABTE22. 1
Comporison of the exoctonolyticolsolutionfor temperoture with theresults
.:13:::-{"yl-f::k-:ns".::"ji stk*;"** Anolyticol Solution
(._) 2 4 6 IJ t0
300 2 8 28 6 3 4 2 8 25 7 7 5 '299 AB43 335 74A4 4AO
Shooting Method
Finite Difference
300 2 B 2B B B 9 2 8 25 t 5 8 2 9 91 2 5 4 J 3 5 . 7 )7B 404
300 283 2664 2 8 3I 8 5 3 299.7416 336 2462 4AA
22.3.1 Derivotive Boundory Conditions As mentionedin our discr,rssion of the shootingmethod,the fixed or Dirichlet boundan' cornlition is but one of severaltypes that are usedin ensineeringand science.A common
22.3 FINITE-DIFFERENCE METHODS
555
A-r
FIGURE22.9 A boundory nodeot theleftendof o heoied rod.Toopproxirnole thederivotive ot theboundory on imoginory nodeislocoted o drslonce Ax to theleftof therod'send.
altemative,called the Neumannboundary condition, is the case where the derivative is given. We can use the heatedrod introducedearlierin this chapterto demonstratehow a derivative boundaryconditioncan be incorporatedinto the finite-differenceapproach: 0:
d2T _ +/r'(f\-f) dr'
However, in contrastto our previousdiscussions,we will prescribea derivativeboundary condition at one end of the rod: dT
. (0): r:
dx T (L) : 7,,
Thus, we have a derivative boundarycondition at one end of the solution domain and a fixed boundarycondition at the other. Just as in the previous section,the rod is divided into a seriesof nodesand a finitedifferenceversionof the differentialequation(F,q.22.16)is appliedto eachinterior node. However.becauseits temperatureis not specified,the node at the left end rnustalso be included.Fig.22.9 depictsthe node (0) at the left edgeof a heatedplatefor which the derivative boundarycondition applies.Writing Eq. (22.16)for this node gives -T
r t (2 + h' L-r2)To* Tt : h' LxzTn
\22.11)
Notice that an imaginarynode(- I ) lying to the left of the rod's end is requiredfor this equation.Although this exterior point might seem to representa difficulty, it actually servesas the vehiclefor incorporatingthe derivativeboundaryconditioninto the problem. This is done by representingthe first derivativein the x dimensionat (0) by the centered difference(Eq. 4.25):
dT dr
Tr - T-t 2Lx
which can be solvedfor T-r:Tt
-2Ar',
dT dr
556
BOUNDARY-VALUE PROBLEMS Now we havea formula for I-1 that actuallyreflectsthe impact of the derivative.It canbe substituted into Eq. (.22.11) ro give
(2 + h' Lx2)7,* 2Tt: h' LxlTo - ro-#
(22.18)
Consequently,we have incorporatedthe derivativeinto the balance. A commot.texampleof a derivativeboundarycondition is the situationwheretheend of the rod is insulated.In this case,the derivativeis setto zero.This conclusionfollowsdirectly from Fourier's law (Eq. 22.5), becauseinsulatinga boundarymeansthat the heat flux (and consequentlythe gradient)must be zero.The tbllowing exampleillustrateshow the solutionis affectedby suchboundaryconditions. E X A M P L E2 2 . 6
I n c o r p o r o t i n gD e r i v o t i v eB o u n d o r yC o n d i t i o n s Problem Stotemeni. Generatethe finite-differencesolution fbr a 10-m rod with t.Tn:200K, Lx:2rl,h':0.05m a n d t h e b o u n d a r yc o n d i t i o n s 4: j : 0 a n d Tt : 400 K' Note that the first condition meansthat the slope of the solutionshouldapproach zero at the rod's left encl.Aside from this ca.se,also generatethe solutionfor dTldx: -20 at-v:0. Solufion. Equation(.22.1g) can be usedto represent node0 as 2 . 2 T 0 - 2 7 1: 4 9 We can write Eq. (22.16)for the interior nodes.For example.fbr node l. -To*2.2Tt-Tz:40 A similar approachcan be usedfor the remaininginterior nodes.The final systemof equations can be assembledin matrix form as
| / | - /t''| 1 2.2 l-r | -1 2.2 -r | -t 2.2 _r I | - 1 2 . 2IJ L Theseequationscan be solvedfor
lrtfit
To :243.0218 Tr : 241.3306 Tz:261.0991 Tt :281.0882 T+:330.4946 As displayedin Fig. 22.10, the solutionis flat at ,r : 0 due to rhe zeroderivativecondition and then curvesupward fo the fixed conditionof I : '100at x : 10.
22.3 FINITE-DIFFERENCE METHODS
557
200d FIGURE 22.1O Thesolution ofo second-order ODEwiiho derivotive boundorv condition ol oneendondo fixed boundory condilion Twocoses reflecting different ol fheother. oreshown derivotive volues ot-r: O For the casewherethe derivative&t,t : 0 is setto -20, the simultaneous equatibnsare 1t
a
-l
2.2 -t
To
,, 1 I
ll 1"{, T, rl
I
-1 .'t ^,
I
1
')a
whichcanbe soivedfor
T2 T3 T+
To: 328'2710 7r:301'0981 Tz:294'1448 Tt:306'0204 zr:339.100)
'
As in Fig.22.10,thesolutionatx : 0 nowcurvesdownwarddueto thenegative derivative we imposedat theboundary. 22.3.2 Finife-DiffercinceApprooches for Nonlineqr ODEs yieldsa systemof nonlinearsiFor nonlinearODEs,thesubstitution of finitedifferences multaneous equations. Thus,themostgeneralapproach to solvingsuchproblemsis to use root locationmethodsfor systems suchastheNewton-Raphson methoddeof equations is certainlyfeasible,an adaptation scribedin Sec.12.2.2.Althoughthis approach of sucprovidea simpleralternative. cessivesubstitution cansometimes Theheatedrod with convection andradiationintroduced in Example22.4providesa nicevehiclefor demonstrating thisapproach, ^
d:T
0 : ;; d
v a
., -.
+ h (I--
t .-t -, f) + o (T] -7")
.
558
BOUNDARYVALUE PROBLEMS We can convert this difTerentialequationinto algebraicform by writing it for a nodei and substitutingEq. tZZ.l5) for the seconcl derivarive: o-
T1 1-2TilTial
Ax2
+ h ' ( T n- 7 l )+ o " ( 7 1 - r : )
Collectingtermsgives -Ti-t I (2 + h'Lxz)Ti - Ti+) : lt' Lx2Tn o' tr2(r!. - r,a) + Noticethat althou-sh thereis a nonlineartermon the right-lrandside,tlreleti-handside is expressedin the form of a linear algebraicsystemthat is diagonallydominant.If we assumethat the unknownnonlinearterm on the right is eclualto its valuefrom the previous iteration,the equationcan be solvedfor , _
/ r ' A . r 2 r -+ o ' A r 2 ( {
-T,o)-tTi tlTi+t
(22.t9)
As in the Gauss-Seidel method,we can useEq. 122.19)to successively calculatethetemperatureof each node and iterateuntil the processconvergesto an acceptabletolerance. Although this approachwill not work fbr all cases,it convergesfbr many ODEs clerived from physicallybasedsystents.Hence,it can sometimesprove usefulfbr solvin-uproblems routinelyencountered in engineering and science. E X A M P L E2 2 . 7
T h e F i n i t e - D i f f e r e n cMee r h o d f o r N o n l i n e o rO D E s Problem Stqtement. Use the finite-difl'erenceapproachto simulatethe temperature of a heatedrod subjectto both convcctionanclradiation:
o: +
+ h , ( r n_ r ) + o ,e, l
14)
w h e r e o ': 2 . 1 x l O - e K - 3 l n - 2 ,L : l 0 m , l r , : 0 . 0 5 m . , Z _ : 2 0 0 K . I ( 0 ) : 3 0 0 K , and I ( l0) : 400 K. Usefour interiornodeswith a segmentlengthof Ar : 2 m. Recallthat we solvedtlre sanreproblemwith the shootingmethodin Example22.4. Solution. Using Eq. (22.11)) we can successively solvefor the temperatures of therod's interior nodes. As with the standardGauss-Seideltechnique,the initial values of the interior nodesarezero with the boundarynodes.setat the fixeclconclitionsof I, : 300and Z. :400. The resultsfor the first iterationare T.-
T.:
- 01)+ 300+ 0 0.05(2)2 200+ 2.7 x t}-e (2)2(2001 :159.2432 2 + 0.05Q)) :91.9614
559
PROBLEMS
Finitedifference
l0 x.m
22.11 FIGURE problem method lo solveo nonlineor Thef ledcircesoretheresu t of usingthefinite difference T h ei n e g e n e r o t e d w i t h t h e s h o o t i n g m e t h o d i n E x o m p e 2 2 . 4 i s s h o w n f o r c o m p c r i s o n - 01)+ 91.9614 200+ 2.1x t\-e'(2)2(2001 +0 'r- - 0.05(2)2 :70.4161 2 + 0.05(D2
Ta*
- 04)+ 10.4461 200+ 2.'/ x rc e'(2)2(2004 0.0-5(2)2 + 400: 2 2 6 . 8 1 0 4
2 + 0.0s(D2
until we converge on thefinal result: Theprocess canbe continued Zo: 300 Tr :250'4827 Tz:236'2962 Tt:215.7596 T',- 286.4921 I. :400 in Example22.4 in Fig. 22.11alongwith theresultgenerated Theseresultsaredisplayed with theshootingmethod.
PROBtEMS 22.1 A stcady-statehcat balance for a rod can bc rcprescntcdas
|a_-\'o . t 5 r : 0
22.2 RepeatProb. 22.1 but with the right end insulatedand the left end tempcratureflxed at 240. 22.3 Use the shootingmethodto solve ,t
, \.
d\-
a
I
v
-
ll
Y
: 8. and with the boundaryconditions.r.(0): 5 and,-v(20) Obtain a solution fbr a l0-m rod with 7(0):240 r(10) : 150(a) analytically,(b) with the shootingmethod, 22.4 Solve Prob. 22.3 with the finite-difl'crenceapproach usingAr : 2. and (c) usin-ethc finitc-ditl'erenceapproachwith Ax : l.
PROBLEMS BOUNDARY.VALUE
560
22.5 The tbllowing nonlinear difl'erential equation was solved in Examples22.4 and 22.'7.
n:
o*4
+ h'(T* - D + o' (Tl
Tl)
(P22.5)
Such equationsarc sometimeslinearized to obtain an approximate solution.This is done by employing a flrst-order Taylor sericscxpansionto linearizc thc quartic term in the equationas o'71 : o'Ta + 4o'T3C -T) where 7 is a basetcmpcratureabout which the tcrm is linearized.Substitutcthis relationshipinlo Eq. (P22.5),and thcn solve the resulting lincar cquation with the finitedifl'erenceapproach.Employ 7 : .r00. Ar : I m. and the paramctcrsfiom Exarnple22.21to obtain your solution.Plot your results along with those obtaincd fbr the nonlinear versionsin Exarnples22.4 and 22.1. 22.6 Dcvelop an M-file to implernentthe shootingme(hod for a linear second-orderODE. Tcst the program by duplicatingExamplc22.2. 22.7 Dcvelop an M-file 1o irnplementthe flnite-differencc approach fbr solving a linear sccond-order ODE with Dirichletboundaryconditions.Testit by duplicatingExample 22.5. 22.8 An insulatedheatedrod with a unifolm hcat sourcecan b e m o d e l c dw i t h t h e P u i t t o t t( u t t ( t t i ( t L '
an( be
a,,.
uk: 1l=
wh len bo
FIGUREP22.IO of the cone wall. Thc equationhas the boundaryconditions: u(,r:0):
Q
a(,r: l):
l
Solvc this equation for the tcmperaturedistributionusing finite-diff'erencemethods.Usc second-orderaccuratefinitediff'crenccfbrmulas for the derivatives.Write a computer versus program to obtain thc solution and plot temperature (l- I axial distancefbr variousvaluesof p : I 0. 20, 50, and100. : .l(r) u.x22.11 CompoundA diffuses through a 4-cm-longtubeand The equationgoverningdiffusionwith Given a hcat source/(,1 ) : 25 "Clm2and the boundarycon- reactsas it diffr.rses. rcaction is : : 2 0 0 ' ' C . : 0 ) : , 1 0 ' C ( , t 10) solve and f d i t i o n s7 ( - t fbr the temperaturedistributionwith (a) the shootingmcthod J)A D kA:0 and (b) the finite-difI'crcncemethod(A-r : 2). ax' 22.9 RepeatProb. 22.8, but fbr the following spatiallyvaryAt one end of the tube (,1: 0), there is a largesourceof .4 i n g h e a ts o u r c e.:l { r 1 : 0 . l 2 . r r 2 . 4 1 2- 1 2 . r . 22.10 The temperaturc distribution in a tapered conical that resultsin a fixed concentrationof 0.1 M. At the other cooling fin (Fig. P22.10)is desclibedby thc fbllowing dif'- end of the tubc thcreis a materialthat quickly absorbsanyA, makingtheconccntration 0 M. If D : 1.5 x 10-6cm2/sand f-erentialeouaiion.which has been nondimcnsionalized: I, : x s is ft 5 l0-o what thc concentrationofA asa func0-u tion of distancein the tube') ,t.t 22.12 The tbllowing diffcrcntial equation describesthe whcrc a : temperature(0 < a < l). .r: axial distancc stcady-stateconcentrationof a substancethat reactswith (0