Uploaded by Chan Cheese

Applied Numerical Methods with MATLAB for Engineers and Scientists

advertisement
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 <
r 0 68 0
t 2 7. 3 1
3 5 06 r
5ir
Bolonce
I 5 I 2 .3 3
6/l
7/l
Decay rate : -tc
whereft is a constantwith units of day I. Thercfore,according to Eq. (1.14),a rrass balancefbr the reactorcan be
wntten as
dc
Bit
9ll
Usetheconservation
of cashto computethe balanceon 6/ l,
111.811,
and9/1. Showeachstcpin thc computation.
ls this
a steady-state
or a transientcomputation?
1,3Repeat
Example1.2.Computethc velocityto t: l2 s,
witha stepsize of (a) I and (b) 0.5 s. Can you make any
statement
regardingthe crrorsof thc calculationbasedon the
results?
1.4Ratherthanthe nonlinearrclationshipof Eq. ( 1.7),you
mightchooseto model the upward fbrce on the bungee
j u m p car sa l i n e a r e l a t i o n r h i p :
rl
f tt :
1.7 Thc amountof a uniformly distributedradioactivecontaminant containedin a closed reactor is measuredby its
concentrationc (becquerel/literor Bq/L). The contaminant
decrcasesa1 a decay rate proportionalto its concentration;
that is
C U
wherer'': a first-orderdrag coefTicient(kg/s).
(a) Usingcalculus,obtain the closed-fbnn solution fbr thc
casewherethejurnperis initiallyat rcst (u : 0 at 1: 0).
(b) Repeatthe numericalcalculationin Example 1.2 with
the sameinitial condition and oarametervalues.Use a
valueof 12.5kg/s fbr c'.
1.5For the free-talling bungee jumper with linear drag
(Prob.I .4).assumea first jumper is 70 kg and hasa drag coefficient
of l2 kg/s. If a secondjurnperhasa drag coefficient
of 15kg/sand a massof 75 kg, how long will it take her to
reach
thesamevelocityjumpcr I rcachedin l0 s'l
1.6 For the fiee-falling bungce jumper with linear drag
(Prob.
1.4),compulethe velocityof a free-tallingparachutist
usrng
Er-rler's
methodfbr thc cascwhcre rr : 80 kg and c' :
l0 kg/s.Perfbrm thc calculation from / : 0 to 20 s with a
stepsizeof I s. Use an initial conditionthat the parachutist
hasanupwardvclocity of 20 m/s at /: 0. At r: l0 s, assume
thatthe chuteis instantaneously
deployedso that thc
dragcocllicientjumps to 50 kg/s.
dt
/ changc\
t .i"ntm: ta,rls
\
/
kt
/ decrease
\
U
l
d
e
c
a
S
\
/
(a) Use Euler's mcthod to solve this equationfrom t : 0 to
I d w i t h k : 0 . 2 d r . E m p l o y a s t e p s i z eo f A r : 0 . I d .
The concentration
at /:0 is l0 Bq/L.
(b) Plot the solution on a semilog graph (i.e., ln c versus/)
and detenninethe slopc.Intcrpretyour results.
l ll A storagetank (Fig. Pl.8) containsa liquid at depth)
where,r': 0 when the tank is half full. Liquid is withdrawn
at a constantflow rate Q to meetdemands.The contentsare
resuppliedat a sinusoidalrate3Q sin2(t).Equation(1.14)
can be written fbr this systcrnas
-d(+Av'\
AT
/ c h a n e ei n \
|
,'
l:
\ votume /
F I G U RP
EI . 8
: 3 0 s i n ' ( r )-
O
tinflowt - (outflow)
r8
MATHEMATICAL
MODELING,NUMERICAL
METHODS,
AND PROBLEM
SOTVING
or, sincethe surfaceareaA is constant
r/t'O.O
: :3:
s i n - f t )_ :
drAA
- rr)
o : - Ir': c,dr : nrc,,(rz
Use Euler's method to solve for the depth _vfron-rr : 0 to
I0 d with a step size of 0.5 d. The parametervaluesareA :
1200m2 and p : 500 m3/d.Assumethat the initial condition
i s . y: 0 .
1.9 For the same storagetank describedin Prob. 1.8, suppose that the outflow is not constantbut rather dependson
the depth. For this case,the differential equationfbr depth
can be written as
dl'
dt
nr the heat capacity,and thc changein temperatureby the
followingrelationship:
a(ltr')r
O .
= . 1 ; s i n - (trA
Use Euler's method to solve for the depth )' fiom t : 0 to
10 d with a step siz-eof 0.5 d. The parametervaluesareA :
1200m2,O:500 mr/d, and cv: -300.Assurnethat the initial condition is _r: 0.
1.10 The volume flow rate through a pipe is given by Q :
rA, whele u is the average velocity and A is the crosssectionalarea.Use volume-continuityto solvefor the required
areain pipe3 of Fig. P I . 10.
l.ll A group of 30 studentsattenda classin a room which
lneasuresl0 m by 8 m by 3 m. Each studenttakesup about
0.075 mr and gives out about 80 W of heat (l W = I J/s).
Calculatethe air temperaturerise during the first l5 minutes
of the class if the room is completely sealedand insulated.
Assume the heat capacityC,. tbr air is 0.7 18 kJ/(kg K). Assume air is an ideal gas at 20 "C and 101.325kPa. Note that
the heatabsorbedby the air O is relatedto the massof the air
The massof air can be obtainedliom the ideal sas larv:
Plt :
nt
-P7
Mwt
where P is the gaspressure,V is lhe volume of the gas,Mwt
is the molecularweight of the gas (1br air 28.97 kg/krnol),
and rRis the ideal gas constant[8.31,1kPa m]/(kmol K)].
1.12 FigureP1.12depictsthe variousways in which an averilgeman gainsandloseswaterin oneday.One liter is ingested
as food, and the body metabolicallyproduces0.3 liters. In
breathingair, the exchangeis 0.05 liters while inhaling, and
0.4 literswhile exhalingover a one-dayperiod.The body will
also lose 0.2, 1.4.0.2.and 0.35 litersthroughsweat,urine,
feces,and throughthe skin, respectively.To maintainsteady
state,how much water must be drunk per day?
l.13 In our example of the fiee-falling parachutist,we assumed that the accelerationdue to gravity was a constant
value of 9.8 m/s2.Although this is a decentapproxinration
when we are examining falling objectsnear the surtaceof
the earth, the gravitational lbrce decreasesas we lrove
above sea level. A rnore general representationbased on
Newton's inversesquarelaw of gravitationalattractioncan
be written as
R:
s ( x ) : g ( 0 ) ( l-( + . r ) '
Skin
Qr.in=
Qz'rt: 20 m3/s
Food
Drink
u.,ou, = 6 m/s
A:=?
F I G U R EP I . I O
Metabolism
F I G U RPEI . I 2
r9
PROBLEMS
whereg(r) : gravitationalaccelerationat altitude.r (in m)
measured
upward fronr thc earth'ssurfacetm/s2).gtO) :
gravitational
accelerationat the earth'ssurface(! 9.8 rn/sr),
a n dR : t h ec a r t h ' sr a d i u s( = 6 . 3 7 x 1 0 6m t .
(a) In a fashionsimilar to the derivationof Eq. (1.8), use a
forcebalanceto derive a ditlerential equationfor velocity as a function of time that utilizes this more complete
representation
of gravitation.However. lbr this derivation, assumethat upward velocity is positive.
(b.1For the casewhere drag is negligible,use the chain rule
to expressthe differentialequationas a function of altituderatherthan time. Recall that the chain rule is
du
du d-r
dt
d.r dt
(c) Use calculusto obtain the closed form solution where
u = u,,at.r : 0.
(d) UseEuler's rnethodto obtain a numericalsolutionfrom
r : 0 to 100,000m using a stepof 10,000m where the
initial velocity is 1400m/s upward.Compareyor,rrresult
u ith the analyticalsolr"rtion.
l. l{ Supposethat a sphericaldroplet of liquid evaporatesat
a ratethat is proportionalto its surfacearea.
dV
::
dt
-kA
rvhereV: volume 1mm3),t : time (hr), k : the evapol'ation
rate(mm/hr), and A : surface area 1mmr). Use Euler's
methodto conrputethe volume of the droplet from I : 0 to
l0 min using a step size of 0.2,5min. Assume that ft :
0.1mm/min andthat thedropletinitially hasa radiusof 3 mm.
Assess
the validity oi your resultsby determiningthe radius
of your final computedvolume and verifying that it rs consistentwith the evaporationrate.
l.l-5 Newton's law oicooling saysthat the temperatureof a
body changesat a rateproportionalto the differencebetween
its temperatureand that of the surroundingmedium (the ambient temperature).
d
-; T : - k ( T - T " )
at
where Z: the temperatureof the body ("C), r : time (rnin),
k : the proportionalityconstant(per minute), and 7, : ths
arnbienttemperature("C). Supposethat a cup of coft-eeoriginally has a temperatureof 68 'C. Use Euler's method to
computethe temperaturefrom I : 0 to l0 min using a step
sizeof I min if I.. : 2l "C and ft : 0.017/min.
|.16 Afluid is pumpedinto the networkshownin Fig. P1.16.
lf 0, : 0.6.O.,: 0.4.Qt:O.2. andQo: 0..1mr/s.determine
the other flows.
ilr*?-i=-i--I
iiri:;$
bt'ili o,l "i "i "'i
i-.er-:--s...;-*-*L*-i
FIGURE
PI.I6
\
iiEi;4+'"
,l
i.
MATLABFundomentols
2.1
CHAPTER
OBJECTIVES
and overviewof
The primaryobjectiveo1'thischapteris to providean introductiorr
how MATLAB's calculatormodeis usedto implcmentinteractivecornputations.
Specificobjectivesand topicscoveredare
'
.
.
.
.
Lcarninghow realanclcornplexnurrrbcrs
areassignedto variables
Learninghow vectorsand matricesareassignedvaluesusingsinrpleussignrnent.
.l
l'ttnctitlns.
the colon ope'rator',
and thc irs;p.r,:cand 1oq1,;piic:,--thc priority rulcsfirr constructingmathernatical
cxpre-ssions.
Llnderstanding
ol'built-in lr-rnctions
and how you can lcarntnore
Gaininga gencralundcrstanding
a b o u t h e mw i t h M A ' I ' L A B ' sH e l p f a c i l i t i e s .
Learninghow to usc vectorsto crcirtca sinrplclinc plot baseclon an equation.
YOU'VE GOT A PROBLEM
n Chap. l. we usctlir firrcebalanceto detcrnrinethe tcrminalvelocityof a fiec-falling
I
o h j e c ll i k c i r h L r r r g cj uer n p c r .
I
T
tsi
V t,t
(m/s').m : mass(kg),
wher-er.,,: ternrinalvelocity(nr/s),,q: gravilationalaccelerertion
and t',,: a dragcoefl'icient(kg/m).Asidefrom prcdictingthe terminalvelocity,this equato computethe dragcoefficicnt
tion can alsobe rearranged
m8
( 2 .I ;
ut
20
ffi=?N
,f
zr'+
4_
%. uE-1 5t
%I
zl x+
=
2r
MAT4gllyllerynENl
2 r THE
TABTE2.I
m,kg
u,,ml s
Dotoforthemossondossociotedtermino|velociliesofonumberofiumpers
BO9
653
gll
929
721
6A2
5l l
836
4
7
7
5
4
557
la \
509
(1,,1
of knclwnmass,this
velocityof a numberol .iumpers
terminal
the
nleasure
we
Thus, if
The clatain Table2' I werecolestimatethe drag coeificient'
tt.l
*t-'
o
provides
equation
l e c t u ' ldo r l h i s p u r p o s e '
^ m , ^ n ^ ^ ^ , . ^ , , . n r rl n analyze
r n : r l v . ztr ' s r rdclht u . B e y o n d
how MATLAB canbe uscdto
leartr
will
we
chapter,
In this
showinghowMATLABcanbeenlployedtocolnputeqtrantitieslikeclragcoetTicients'we
willalsoillustratchowttsgraphicalcapabilitiesp,*i.t.adtlititlnalinsightintosuchanalyses.
2.r
M A T L A B i s a c o m p u t e r p r o g r a l x t h a t p r o v i < l c s . t hiteprtlvides
u s e r w i ta
h avery
c o nnice
v e l rtool
i e t r tto
e ninrple.
virontlrentfclr
ln particular,
typcs.ol.ctilculaticrns.
many
perfornirrg
* *' 1P,
rute
" "!i:H:i:iilll1l;|;"vtoope
:',:,'^
::::::::::):i:I;:;: ii' ffi:lr
ngprots n
ati
crc
u'T*.,ln
;::'#
":1
"'"
ilJffill'.":ili:
ff.:ll,'Ji:'.:il{;
"'to createMAft-AB progralns'
commandscan be usecl
Chap.3, we show how sr'rch
o n e f u r t h e r . n o t c ' . [ h i s c h a p t e r h a s b e e n w r i tThe
t e n a s a l relficient
a n c l s - t way
l r r c xtoc lbeconle
.cise.Thatis,yott
o1'youlcornpLrtcr. m.st
fiont
in
,,tring
while
it
shoultlrcad
oroficientist'oacttrallyinl;llctlentthcctltntrratrclsonMATLABasyouprocccdthrotrghthe
following material'
witrclows:
fUeif-A,B usesthreeprimary
anddata'
winclow'Uscdto entercommancls
.
Conrtnancl
graphs'
and
plots
display
.
C."pt,i.* windtlw' Usedkr
and edit M-filcs'
creute
tt'r
gaii
Usecl
.
winclow'
wind.ws' ln chap' 3 we
usc of thc c.mmand anclgraphics
In this chapter,we wilr r.rake
crcatcM-iiles'
prompt
*iii ut. the edit window to
openwith thc commancl
tht tu'ntunti window will
Afier startingMd;;;'
bcingdisPlaYcd
y o L lt y p c i n c o n t lrI a s c q u c n t i i tl ll t s h i t r na s
oper''rtcs
o1'MATLAB
nlode
The calculator
you can think of it as operyou get'r-result
cotnurancl'
each
For
line'
by
Lh::s'
manclsline
tn
For exanlplc'if you typc
^ii"g f if,. a uery l'ancycalcttlator'
-_-,
51
Lo
rcsult'
MATL.AB will disPlaYthc
a,ttt-,=,,
1c'i':'r
' M A | l , A B s k r p s a l i n c b c t w c c l l t | - r | a h e l ( l , r r l . ) l t n d t h c n u n l b c r . ( - ] ) . H( e) rr: rr r- '.1$i(1..i (l L, m
l r firtis: 1lAl cuhd b! .l rl :n' rk' 'l j n c s t o r
I
l i n e ' sa r e i n c l u d c t l w i t h t h c
a u n , , a r t. u h . i t h e rr r t a n t
co.ciscness.varuaun
comlrtnds.
ffi
qA
,.-ILJ'
8
b
zt\
4x
4
AL
^r-
?t 5t
!8
22
MATLABFUNDAMENTALS
Notice that MATLAB has automaticallyassignedthe answerto a variable,ans. Thus, you
could now use ans in a subseouentcalculation:
'-
ars
+
II
with the result
50
MATLAB assignsthe resultto ans wheneveryou do not explicitlyassignthe calculation
to a variableof your own choosing.
2.2 ASSIGNMENT
Assignmentrefersto assigningvaluesto variablenames.This resultsin the storageof the
valuesin the memorylocationcorresponding
to the variablename.
2.2.1 Scolors
The assignment
of valuesto scalarvariables
is sirnilarto otherconlputerlanguages.
Try typing
1 _ A
Note how the assignment
echoprintsto confirmwhat you havedone:
4
Echoprintingis a charactelistic
of MATLAB. lt carrbe suppressed
by terminatingthecommand line with the semicolon(; ) character.
Try typing
>> A
=
5;
You can type severalcommandson the sameline by separating
them with comnlasor
semicolons.If you separatethem
with commas,they will be displayed,and if you usethe
semicolon,they will not.For example,
>> a -
4,A = 6;x
= . 1;
twe
4
MATLAB treatsnames in a case-sensitivernanner-that is. the nan)e a is not the same
as the name a. To illustrate this. enter
>>
short
Iong
short e
long e
long
eh^rt
and then enter
long
>>A
bank
Seehow their valuesare distinct.Thev are distinct names.
frT-Tbl
"L x jj€-
zfTtr ='1.t- st
4
zl-+
Z] X+?
.-f
J
g
a
_A
-z th
-r
15/
I
end
eng
23
2.2 ASSIGNMENT
We can assigncomplex valuesto variables.since MATLAB handlescomplex arithmetic automatically.The unit irnaginary number
ir preassignedto the variable i.
"/J
Consequently,
a complexvaluecan be assignedsimply as in
=
>> x
2+i*4
2.0000 + 4.0000i
It shouldbe notedthat MATLAB allows the symbol j to be usedto representthe unit imaginary numberfor input.However,it alwaysusesan i for display.For example,
= 2+1*4
>> x
2.0000 + 4.0000i
Thereare severalpredefinedvariables.fbr example.pi.
pi
>-
3 . r4r6
Noticehow MATLAB displaysfbur decinralplaces.If you desireadditionalprecision,
enterthe fbllowing:
>>
format
long
Now whenpi is enteledthe resultis displayedto l5 signilicantfigures:
>> pi
1 . 1 4 1 5 9 2 b 5 - 15 8 9 7 9
To return to the four decimal version, type
>> format
short
The f<rllowing is a summary of the format commands you will employ routinely in engincering and.scientific calculations.They all have the syntax: f ormat typr...
twe
Result
Exomple
- c h o ]t
S c o e d f i x e dp o i n t f o r m c tw i t h 5 d i g i t s
S c c r i e df r x e d - p o i nfto r m o iw i t h 1 5 d i g l h f o r d o u b l e o n d Z d i g i t sf o r s i n g l e
F l o o i i n g ' p o i nfto r m c tw i i h 5 d i q i t s
F l o o i i n gp o i n t l o r m o tw i f h l 5 d i g i i s f o r d o u b l e o n d / d l g i t sl o r s i n g l e
B e s lo f f i x e d o r f l o o f i n g - p o i nl ol r m o tw i t h 5 d i c t l t s
B e s to [ [ i x e d o r f l o o t i n gp o i n l f o r m o fw i t h 1 5 d l g i t sf o r d o u b l e
andTaglsforsingie
E n g i n e e r l n gf o r m o fw i t h o l l e c s l5 d i g i t so n d o p o w e r i h o t i s c r r n u t l p l eo f 3
Engineen
r g f o r m o tw i i h e x c c l l y 1 6 s i g n i f i c o ndt i g i t so n d o p o w e r
t h o t s o m ut i p l eo f 3
F i x e dd o l l o r so n d c e n t s
3. r416
i.1415q1b5J58979
l.1415e+0tr0
3 . 1 4l 5 9 2 6 5 3 5 8 9 7 9 3 e + 0 0 0
3.r416
3 . 1 4 1 5 9 2 . 6 5 ] , 5 8 997
Lcrng
srrort
e
lonlJ e
shorl: g
lonq g
short
eng
long eng
bank
ffiE\
z\
-_-
el +
a --:-----;:a,
+
.__..__i
. I
'"1F{'J
Z} X+
1.1416er000
3 . 1 4 1 5 9 26 5 3 5 8 9 79 e + 0 0 0
1
1A
-a.[h
--T a-*!1,€
4
el 5t
I
24
MATLABFUNDAMENTALS
2.2.2 Arroys, Vectors qnd Mqtrices
An arrat' is a collection of values that are representedby a single variable name. Onedincnsirnalarral't arc callcd tu'/ort and two-dinensional anavs are c'allednrutnce:. The
scalarsusedin Section2.2.1 areactuallya matrix with one low andone column.
Bracketsare usedto enterarraysin the comnranilmode.For exan.rple,
a row vectorcan
be assisnedas fbiiows:
123451
Note that this assignment
overridesthe previousassignment
of a : 4.
practice,
In
row vectorsare rarely usedto solve rnathematical
problerns.When we
speakof vectors.we usuallyret-erto column vectors,which are more contmonlyused.A
column vectorcan be enteredin severalways.Try them.
or
_-..>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
<, lh
-r
?l 5t
I
2.2 ASSIGNMENT
25
At any point in a session,a list of all currentvariablescan be obtainedby enteringthe
v;hocommand:
> > 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
<,
- [h
Y
t('€ - ?'' 5l
I
26
MATLABFUNDAMENTALS
l.J
incrementof one:
whi
For
-:
l-
-
1.(
I
If colonsare usedto separatethreenumbers.MAILAB generatesthe numbersbetweenthe
first and third numbersusing an incrementequal to the secondnumber:
>> t = L:0.5:3
2.3 rnr
f-
1.0000
2.0000
1.5000
2.5000
ope
3.0000
com
Note that negativeincrementscan also be used
>-" f
=
Ifn
i0:-1:5
t0
9
B
1
6
5
Aside from creatingseriesof numbers,the colon can also be usedas a wildcard to select the individualrows and columnsof a matrix.When a colon is usedin placeof a specific subscript,the colon reprcsentsthe entire row or column.For example,the secondrow
of the matrix A can be selectedas in
>> A(2,:)
We can also use the colon notation to selectivelyextract a seriesof elementsfrom
within an array.For example,basedon the previousdefinition of the vector t :
Alsc
> > ' 1 -( 2 : 4 )
981
Thus, the second through the fourth elements are returned.
Res
2.2.4 The rlnspace ond losspace Functions
plv
The iinspacre and logspace functionsprovideotherhandytoolsto generatevectorsof
spacedpoints.The 1 inspace functiongenerates
a row vectorofequally spacedpoints.It
has the form
thes
ing
I i n s p a c e (x l . x 2 , n )
which generatesn points betweenxl and x2. For example
'> linspace(0,1,6)
Th
AS
0
0.2000
0.4000
0.6000
o.BOOO
1.0000
lf the n is omitted,the function alromatically generates100points.
The logspace l-unctiongenerates
a row vectorthatis logarithmicallyequallyspaced.
It has the form
logspace(x1,
x2,
ffi=
?l -+
Ll 2. *
J
).
-t
n)
Zl eNst
"L x
ZI X+?
vI
-tlh
2-
3l
=$8
2.3 MATHEMATICAL
OPERATIONS
27
which generatesn logarithmicallyequally spacedpoints betweendecadesl0'r and 10":
For examp)e,
>>
e (-I
logspac
0.1000
,2 , 4)
1.0000
10.0000
100.0000
If n is omitted,it automaticallygenerates50 points.
2.3
MATHEMATICAL
OPERATIONS
Operationswith scalarquantitiesare handledin a straightforwardmanner,similar to other
computerlanguages.The common operators.in order of priority, are
Exponenliolion
Negotion
M u l t i p l i c o t i o no n d d i v i s i o n
L e f td i v i s i o n ' l
A d d i t i o no n d s u b l r o c t i o n
These operators will work in calculator fashion. Try
,r
2*pt
6 .2,832
Also, scalar real variables can be included:
>> Y - Pt/4;
>> y ^ 2.45
0.5533
Results of calculations can be assigned to a variable, as in the next-to-last example, or simply displayed, as in the Iast example.
As with other computer calculation, the priority order can be overridden with parentheses. For example, because exponentiation has higher priority then negation, the following result would be obtained:
>> Y =
4 ^ 2
y-
_r6
Thus,4 is first squaredand then negated.Parentheses
can be usedto overridethe priorities
as in
>> Y = \-4)
L6
: Lefi division appliesto matrix algebra.It will be discussedin detail later in this book.
-----
zl'+
ffi=
r\ a +
,"1lJ t
-
-=-:-T
*-6 -l
__..-..-..i,
- "L x
.l
5[ s\
-{
Z] X+
4
<, [h
6re
%&
2a
MATTABFUNDAMENTAIS
Calculations
can alsoinvolvecomplexquantities.
Herearesomeexamplesthatusethe
valuesof x (2 + 4i) andv ( I 6) definedpreviously:
'>,'3
* x
ans 5.0004
>>'1
+I2.0000i
/;<
0 . 1 0 1 10
0.2000r
^ ,2
>> x
ans -12.0000
+1r,.0000-r
>> x + y
an!; lB.O0tJ0 + 4.00001
The real power of MATLAB is illustrated in its ability to carry out vector-rnatrix
c a l c u l a t i o n sA
. l t h o u g h w e w i l l d e s c r i b es u c h c a l c u l a t i o n si n d e t a i l i n C h a p . 8 . i t i s w o r t h
introducing sorne of those manipulations here.
The irtner product of two vectors (dot product) can be calculated using the * operator,
:.:,d*b
r10
and likewise, the outer pnrluct
>:, b * -
allS
2
4
6
8
10
4
8
12
16
2t)
b
12
18
24
l0
8
16
24
3'2
4L)
I0
20
30
4t)
50
To further illustrate vector-matrix mLrltiplication,llrst redefine a and l:,:
.
,
ti
2
l;
and
>> b =
f4 5 tjl';
Now. try
>>
a
*
l0
A
t6
4?.
Z
ffir
qAb
zl' -&
LJt
ffi
Z
uN
x
A
i-
x+
<,
- [h
z_! 1J'€- sJ 5t
3$&
29
2.3 MATHEMATICAL
OPERATIONS
>>A*b
32
71
r22
Matrices cannot be multiplied if the inner dimensions are unequal. Here is what happens
when the dimensions are not those required by the operations. Try
>> A * a
MATLAB
automatically displays the error message:
??? ETrar using =-> mtimes
Inner matrix dimensi.ons must aQfree.
Matrix-rnatrix multiplication is carried out in likewise fashion:
>> A * A
30
66
102
35
B1
126
42
96
150
Mixcd operations with scalarsare also possible:
>> A/pi
ans =
0.3183
'I
.2132
?,.2,?,82
0.5366
I.5915
2,.\46\
0.9549
1.9099
2,.8648
We must always remember that MATLAB will apply the simple arithmetic operators
in vector-matrix fashion if possible.At times, you will want to carry out calculations item
by item in a matrix or vector. MATLAB provides for that too. For example,
30
66
r02
35
81
t2,6
4?,
9tr
1 50
resultsin matrix multiplicationof a with itself.
What if you want to squareeachelementof e? That can be donewith
>>
anS
A
.^
2
=
r49
16
49
2a
36
64
81
^
The . precedingthe operatorsignifiesthat the operationis to be carriedout elementby
element.The MATLAB manualcalls thesearray operatlons.They are also often referred
Loas element-hy-cIemen t t tparutit tn.s.
ffi
z\4
,"I LJ'
-2.(-h
Zl- gt st
-l
z_|lh
"L
4
<, lh
x _-T - 9f 5l
,t\€
-l-
I
30
MATLABFUNDAMENTALS
MATLAB containsa helpful shortcutfor performingcalculationsthat you've already
done.Pressthe up-arrowkey. You shouldget back the last line you typed in.
>> 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
<'- [h
Y
ll'€
- eJ 5t
&
FUNCTIONS
2,4 USEOF BUILT-IN
3l
the function name.Try
>> 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 --T<, lh
,f\€ - 9f >l
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
<,
- [h
j_f€ - sJ 5t
33
2.5 GRAPHICS
2.5 GRAPHICS
createa
MMLAB allows graphsto be createdquickly and conveniently.For example,to
enter
above,
data
the
graphof the t and v arraysfrom
(t,
:,., nlol
./)
lne gfapn appears ln tne grapnlcs wrnoow ano can De pnnteo or translerreo vra tne clrpboard to other programs.
60
50
40
30
20
10
0
8
1214
10
16
18
You can customizethe grapha bit with commandssuchas the following:
>>
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+
<, lh
--4
l,f.€- ef >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<lilowingcommandswould
resurt in both lines and symborsbeing
displayed:
':'
'-.
>-
ploL
(t,
lLoLd on
(t,
Plot-
-,.)
v,
'o')
::-, ho1c1 off
In additionto ho1.l, anotherhanrlyfunctionis subplor,
which alJowsyou lo split the
graphwindow into subwindows()rp(ute,r.It has
the svntax
subploL (n, n, pJ
This cornrnandbreaksthe graphwindow into
an m-by-nmatrixof small axes,and selects
the p-th axesfor the currentplot.
-
ffi
aAI
=,:st
--7i
5[
7l
Lt r
J-l
J'-l
zt'-+
Z-} X+
z-t\
-x
v!
a.
- [h
rf € - 1 5t
2.5 GRAPHICS
35
We can demonstratesubplot by examiningMATLAB's capabilityto generatethreedimensionalplots. The simplest manifestationof this capability is the p1or3 command
which has the syntax
p1ot3 \x, y,
z)
wherex, y, and z arethreevectorsof the samelength.The resultis a line in three-dimensional
spacethroughthe pointswhosecoordinatesarethe elementsof x, y, anrJz.
Plotting a helix providesa nice exampleto illustrateits utility. First, let's grapha circle
with the two-dimensional
plot function using the parametricrepresentation:
-r: sin(t)
cos(r).
and.y
We employ the subplot commandso we can subsequently
add the three-dimensionalplot.
>> l- = 0:pi/50:10*pi;
> > s u b p l o r -( 1 , 2 , I ) ; p 1 o t ( s j n ( L ) , c o s ( r ) )
>> axis squaLe
>> r.irle (' (a) ')
As in Fig. 2.la,the result is a circle. Note that the circle would have been distorted if we
had not used the axis sou.er e command.
F I G U R E2 . I
A twoponeplotof (o)o two-dimensionol
circleond (b)o three-dimensionoJ
helix
(b)
ffi=b
z['+
,"I LJ'
5t(st
1t
-l
Z] X+
"L Y
vI
<, lh
_-T
j} € - eJ 5t
3$8
36
MATLABFUNDAMENTALS
Now, let's add the helix to the graph'sright pane.To do this, we againemploy a parametl'icrepresentation:
x: sin(/),.)': cos(1),andu : /
' > s u b p L o t (- I , 2 , 2 ) ; p 1 o t 3 ( s i n ( L ) , c o s ( 1 . ) , L ) ;
,)
>> rirle('(b)
The resultis shownin Fig. 2.l b. Can you visualizewhat'sgoingon?As time evolves.
the .r and .y coordinatessketchout the circumferenceof the circle in the r-r' plane in the
plot. However.simultaneously.
the curverisesvertisamefashionas the two-dimensional
cally as the : coordinateincreaseslinearlywith time. The net resultis the characteristic
springor spiralstaircase
shapeof the helix.
of graphicsthat are useful-fbr example.plotting obiects
Thele ale other f'eatr.rres
insteadof lines,familiesol curvesplots,plottingon the complexplane,log-logor semilog
plots,three-dimensional
meshplots,and contourplots.As describednext,a varietyof resolll.ces
areavailableto learnabouttheseas well as otherMATL,AB capabilities.
2.6
OTHERRESOURCES
ol'MATLAB that we will be using
The fbregoingwas designedto lbcus on thosef'eatures
overviewof all
in the remainderof this book.As such.it is obviouslynot a comprehensive
in learningrnore,you shouldconsultone
If you are interested
o1'MATLAB's capabilities.
of the excellentbooksdevotedto MAILAB (e.g..Palrn.2005;Hanselmanand Littlefield.
2 0 0 5 :a n dM o o r e . 2 0 0 7 ) .
by
an extensiveHelp lacility thatcan be accessed
Further,the packageitsell'includes
clicking on the Help nrenuin the comnrandwindow.Thi.swill provideyou with a nutnber
of ditl'erentoptionsfbr exploringand searchingthloughMATLAB's Help material.In addition, it prclvidesaccessto a numberof instructivedetnos.
As describedin this chiLpter.
help is also availablein interactivernodeby typing the
lrclp conrmandfbllowedby the narneo1'acommandor function.
If you do not know the narne,you can use the lookfor commandto searchthe
of text.Fol example,supposethatyou wantto find all
MATLAB Help files lbr occurrences
you couldenter
and functionsthat relateto logarithrns,
the cornrnands
l-,kt
,r
..,'t
irhn
anclMATLAB will displayall ret'erences
that includethe word loq.rri t-hnr.
Finally.you can obtainhelp tionr The MathWorks.lnc., websiteat www.mathworks
books,and technical
.corn.Thereyou will l'indlinks to productinfbrmation,newsgroups,
uselitl
well
ofother
resources.
supportas
as a variety
ffiar
--=-al'
z[ Ll 2
zl'+
L1
-l_t
r
5y 5t
-r
zl ,x+
"L x
4
a,
- [h
1.|€- 1 5t
=$8
37
2.7 CASESTUDY
EXPLORATORY
DATAANALYSIS
Your textbooks are filled with formulas developed in the past by
Bockground.
renowned scientistsand engineers.Although theseare of great utility, engineersand scientists often must supplementtheserelationshipsby collectingand analyzingtheir own data.
Sometimesthis leads to a new forrnula. However, prior to arriving at a final predictive equation, we usually "play" with the data by per{ormingcalculationsand developingplots. ln
most cases,our intent is to gain insight into the pattemsand mechanismshiddenin the data.
In this casestudy,we will illustratehow MAILAB facilitatessuch exploratorydata
analysis.We will do this by estimatingthe drag coefficientof a free-falling human based
on Eq. (2.1) and the data from Table 2. 1. However, beyond merely computing the drag
coefficient, we will use MATLAB's graphicalcapabilitiesto discernpatternsin the data.
canbe enteredas
The datafrom Table2.1 alongwith gravitationalacceleration
Solution.
,
9)..9 (tir..l 8t:1.91;
tr-jrr1.o
f,0.ll 7.1.l 91.1
'rl..ll;
:;'rrt::l!-1
!,5.i
!,1 4l.l
.4 4|J.1 !0.9
,--. r; 9.fl1;
The drag coefficientscan then be computedwith Eq. (2. l). Becausewe are performing
operationson vectors,we must include periodsprior to the operators:
element-by-element
(r. '.1, r
1)..tt,il')
l).')t) lr)
We can now use someof MATLAB's built-in functionsto ceneratesome statisticsfor the
results:
- . . - 1 , J , 1 r 7 q = 1 1 r ( ' . (l (a i l l
';1,1 ,.
i Li,-.1
l i , i i r = t i ri r r (
.l r
, n , . ,r ,
, , 1 , 'q1 ( r ' r l )
l) . .l llr;4
r l c l r r ri r r
-
i)
r ' c l n t . r: .
0. J12!
Thus, the averagevalue is 0.2854 with a rangettom 0.25l l to 0.3125kg/m.
Now, let's start to play with this data by using Eq. (2 l) to makea predictionof the
teuninal velocity basedon the averagedrag:
',..
v l ) ] , . ' a l . , : l a l r L ( g * r l t r t r , l , r ' " ' ). 1
Vpl t r:Ci
'rl-t,(lL'r
'.1),.i
1i,..iii,)/
4'1. /!.1l'-r'r.ll1r',tr:,
] ,) f "
Notice that we do not have to use periodsprior to the operatorsin this formula? Do you
understandwhy?
We can plot thesevaluesversusthe actualmeasuredterminal velocities.We will also
superimposea line indicating exact predictions(the l;1 line) to help assessthe results.
ffi=
qA+,
z(r Ll 2
,e[r
+
tr4
-\
e[ st
2-{
Z] X+?
x
-z [h
/_l€
- eJ 5t
?$
I
38
MATLABFUNDAMENTALS
Plot of predictedversusmeasuredterminal velocities
3ss
€0)
o-' 50
45L
50
49
48
56
54
53
52
51
Measured
Plot of drag coefficientversusmass
0.35
o)F
o<.
o
q)
t!
U.J
o
o
o.Y
c,)
O
oo
r!o
0 . 2|
60
65
70
85
80
75
Mass (kg)
95
PROBTE
FIGURE2.2
withMATIAB
Twoololscreoted
2.1 A simple electricc
pacitor, and an inductor
on the capacitor4(l) as
as
Becausewe are going to eventually generatea secondplot, we employ the subplot
command:
' ' : : u l . , y r l o L( 2 , ) , 1 ) ; p l o t
x I a b , - l ( ' l r , : , , t : ; L r r i : c l)'
( " / t - , v p r ( . 1 ( ,l ' . ) ' , ' , ' 1 . , v l - )
q(t):
.,
('Irr i.:rlic lL-'tl')
'l'l ol
ol
I i t lc] (
I)t ('(l i ( t..crd
q t ) g - R t / ( 2s1
I i.rlrt'I
Lrurar(l l,
wherer:time,qo:tl
L: inductance,and (
generatea plot of this fi
As in the top plot of Fig.2.2,becausethe predictionsgenerallyfollow the l: I line, you
might initially concludethat the averagedrag coefficientyields decentresults.However,
notice how the model tends to underpredictthe low velocitiesand overpredictthe high.
This suggeststhat ratherthanbeing constant,theremight be a trendin the dragcoefficients.
This can be seenby plotting the estimateddrag coefficientsversusmass:
x l ; r i r e I ( ' n r , r : r f , r( k q r ' t
' , ' l , i ' l : rl, ( ' , - ' : : 1 i t n a l ' t , r l L l r , t g r : t , [ ' l ' I i c t l l r l
( ] i q / 1 l l)
L i 1 . L e 1 ' ; ; L i r l - O I c 1 L . i . - l] r o l r i I i . i c t l L \ " r r : . i 1 1 l rf li l , l
4o:12,R:50'L-j
2.2 The standard norrr
bell-shapedcurve that c
,)
Use MATLAB to ger
z: -4 to 4. Labelth
scissaasz.
The resulting plot, which is the bottom graph in Fig. 2.2' suggeststhat rather than
being constant, the drag coefficient seems to be increasing as the mass of the jumper
ffi=
llAb
zl'+
:
:
"L x
Z] X+?
vI
<,
- [h
rtE-:l
5
I
39
PROBLEMS
continued
increases.Basedon this result,you might concludethat your model needsto be improved.
At the least,it rnight motivateyou to conductfurther experimentswith a larger numberof
jumpers to confirm your preliminaryfinding.
In addition, the result might also stimulateyou to go to the fluid mechanicsliteratureand
leam more aboutthe scienceof drag.If you did this. you would discoverthat the parameter
c.,is actually a lumped drag coefficient that along with the true drag includes other factors
such as thejumper's frontal areaand air density:
CoPA
(2.2)
where C, : a dimensionlessdrag coefficient,p : tir density (kg/mr), and A : frontal
area (m2), which is the areaprojectedon a plane normal to the direction of the velocity.
Assuming that the densitieswere relatively constantduring data collection (a pretty
good assumptionif thejumpersall took off from the sameheighton the sameday),F,q.(2.2)
suggeststhat heavierjumpersmight have largerareas.This hypothesiscould be substantiatedby rneasuringthe frontal areasof individualsof varying masses.
PROBTEMS
2.1A simplcelcctlic circuit consistingol' a rcsistur,a capacitor,
andan induc(oris dcpictcdin Fig. P2.l. T'hcchargc
onthecapacitor
ri(t) as a lirnctionol'limc can bc cor.nputcd
as
2 . f U s c t h c I i r : r p , r c e l ' u n c t i o nt o c r c a t c v c c t o r s i d c n t i c a l t o
t h c l i r l l o w i n g c r c a t c c lw i t h c o k l n n o l a t i o n :
(a) t : !:{r:-JlJ
(b) r:
t,:,.1
2.4 Usc colon notution lo crcatc vectors idcntical to the
l i r l l o w i n g c r c u t c dw i t h t h c I i n : , [ ) , ] .r : l i r n c t i o n :
( a ) r ' : I i r s l ) . r c ' { Ir l , I , t t
( b ) | : 1 r n s l r . r c e ( r ,0 , I I )
(#)'
,]
whcre
I = time,r7o: thc initial chargc.1l : the rcsistancc,
I = inductance,
and C : capacitancc.Usc MATLAB to
generate
r plot o1'thislirnctionliorn / - 0 Io 0.7, givcn that
q o - 1 2R
. : 5 0 ,L : 5 , a n d C : l 0 r .
2.2Thestandardnorrnalprobabilitydensitylirnction is a
bell-shaped
curvethat can be rcprescntcdas
2 . 5 I l ' a l i r r c c I - ( N ) i s a p p l i c d t o c o n r p r c s sa s p r i n g , i t s d i s placcmcnt,r (rn) can ol'tcn bc rnodclcd by Hookc's law:
Ij:lr
Inductor
It-\-
-
/
-,
l-
:-'-
\ ! /
/ ; - .
\/ llt
Resistor
UseMATLAB to gencrate a plot ol' this fbnction liom
;=-,1 to.1. Labcl thc ordinateas lrcquencyand the abscissa
as:.
F I G U R EP 2 . I
ffir
qA+,
zt'+
,"1{t
6 e[st
-l
-l
Z] X+
x
<,
- [h
1-l€- eJ 5t
8,
40
P
MATLABFUNDAMENTALS
where k : the spring constant(N/m). The potentialenergy
storedin the spring U (J) can then be computedas
l-
U:
-(.r2
Five springsare testcdand the following data compiled:
F,N
ll
r,rr
0013
)2
0020
l5
9
0C09
0010
2.13 You conractthe jur
Table 2.1 and measure
values, which are order
conespondingvaluesin T
2.8 lt is general practice in enginceringand sciencc that
equationsbe plotted as lines and discretedata as syrnbols.
Here is somcdatafbr conccntration({) \'elsustimc (l) for the
photodegradation
of aqueousbromine:
t, min
c, PPm
t0
414
2A
26
3C
t6
40
t3
50
i0
A,m2 o45A O4OlO
60
c5
t2
0012
(a) If the air density is p
compute values of tl
cD.
(b) Determine the averag
resultingvalues.
(c) Develop a side-by-si
C, versusrr (right sic
and titles on the plots
2.14 The following para
that contractsexponentia
This datacan bc dcscribedby the lbllowing function:
LlseMATLAB to storeF and,t as vectorsand thcn conrputc
vcctorsof the spring constantsand the potentialenergics.
Use thc max l-unctionto dcterminethe maxirnumpotential
cncruy.
2.6 The densityoi treshwater
canhe computedasa lunction
ol'tcmperaturc
with the firllowinecubiccquation:
p:
- 5 . - 5 2 8x9 l 0 - 8 f ( l - 8 . - 5 0 1 x6 t o ' - 6 7 < ?
s
i 6.5622x l0 11 + 0.99987
(._ 4.g4.,.00r.1/
Usc MATLAB to crci\tc a plot displayirrgboth the data
(r-rsingsquarcsymbols)and the function (using a dotted
l i n e ) .P l o tt h c l u n c t i o nl b r r : 0 t o 7 5 r n i n .
2.9 The senii 1o9i, lunctiont)pcratcs
in an idcnticalfashion
to thc frlot, lirnction cxccpt that a logarithrnic(basc-10)
scale is used lirr thc l axis. Usc this l'unctionto plot the
data ancl l'unctionas dcscribcdirr Prob. 2.t1.Explain thc
result s.
2.1() Hcrc is sorncwirrd tunncl data lirr lirrcc (I-) vcrsus
v e l o c i t y( r ' ) :
- srnI
nt ,
cosl
.v=e
.r:c
('C). Usc
whcre p : dcnsity (g/cnr31and 16' : tcmperat.urc
MATLAB to gencratea vcctor of tcrnpcraturesrangingliorn
32 "F to 82.-1'F usingincrcrncnts
of 3.6 oF.Convcltthrsvcctor tclclcgrecs
Celsiusanclthe'ncomputca vcclorof dcrrsitics
-i"1.. u , m l s 1 0 ? . 0 3 0
4A 50
60
7A
B0
hascdon the cubic lbrmula. Crcatea plot ol'p vcrsus
't5 7A 3BO
N
t
2
2
A
8
3
0
1
450
I
t
,
5
5
0
6
r
0
:
519(7'F 32).
Rccallthat T(
2.7 Manning'scquationcanbc uscdto compulcthc vclocity
01'wirtcrin a rectanglllar
opcn channcl:
This clatacan bc describcdhy thc lirllowingl'ur.rction:
BH \t'
/s/
.
I" : 0.2711ttt')x+t
rt \B+2H/
oo0to
0 0008
0 0c103
t5
3
'25
l9
'24
t5
Storethesevaluesin a matrix whereeachrow reprcscntsone
oi'thc channelsandcachcolumn represents
one of the paranrctcrs.Writc a single-lineMATLAB statcmentto conputc a
column vectorcontainingthe vclocitiesbasedon the virlues
in the parameter
matrix.
.r5
.r'l
.
Sln,I=-t-+
.r!
51
,r7
71
(b) rt
q = 4:2:L0;
- 17 B 4; 3
>> sum(q) * r(2,
>> r
Use MATLAB to creatca plot o1'thcsine(solid linc) along
r.vitha plot o1'thcscriescxpansion(dashedline) up ttr andincluding the terni -r7171.Usc the Lruilt-intirnctionf .rct1)r
ial in conrputingthe scriesexpansion.Make the rangeof
the abscissali'onr,r : O to 3tr 12.
a. th
4-t
-----j
zl 4+
7l Llz *2f
z['+*
-r
"
5[st
::-
;:,
.l
Z] X+?
jlg-
.
>> 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) /<vcrsusI,, and (b) logr{)ft
crater sidc-by-side
vcrsus l/7,,. Enrploythc semi l.rqy functionto crcatc(b).
Interpretyour rcsults.
ffi=
z\4+
zl
:
F+
"L
x
z,_!
<, lh
u=4
3$8
43
3,I M-FITES
We also learnedthat a numericalsolutionof this eouationcould be obtainedwith Euler's
method:
Ui+l: ui +
dut
dt
Lt
This equation can be implementedrepeatedlyto compute velocity as a function of
time. However, to obtain good accuracy,many small stepsmust be taken.This would be
extremelylaboriousand time consumingto implementby hand.However,with the aid of
MATLAB, suchcalculationscan be performedeasily.
So our problem now is to figure out how to do this. This chapterwill introduceyou to
how MATLAB M-files can be usedto obtainsuchsolutions.
3. I
M-FIEs
The most common way to operateMATLAB is by enteringcommandsone at a time in the
command window. M-files provide an alternative way of performing operationsthat
greatly expand MATLAB's problem-solvingcapabilities.An M-file containsa seriesof
statementsthat can be run all at once.Note that the nomenclature"M-flle" comesliom the
fact that suchfiles are storedwith a .m extension.M-files comein lwo flavors:scrintfiles
and functionfiles.
3.l. t ScriptFiles
A scriptfile is merely a seriesof MMLAB comnrandsthat are savedon a file. They are
useful for retaininga seriesof commandsthat you want to executeon more than one occasion.The scriptcan be executedby typing the file name in the commandwindow or by
invokingthe nrenuselections
in the edit window: Debug,Run.
EXAMPTE
3.I
S c r i p tF i l e
Problem Stotement. Developa script lile to computethe velocity of the lree-falling
bungeejumper.
Solution. Openthe editorwith the menuselection:File,New, M-file.Type in the fbllowing statenrents
to computethe velocityof the fiee-tallingbungeejunrperat a specifictime
(
1
.
9
)]:
[recalE
l q.
9 = 9.81; m = 68.1; L - 12; cd = 0.25;
v = sqrt(g * m / cd) * tanh(sqrt-(g * cd / m) * L)
Savethe file as scriptdemo . rn.Returnto the commandwindow and typc
>>scriptdemo
The result will be displayedas
50.6175
just as if you had typedeachof its linesin the comntandwinclow.
Thus,the scriptexecutes
ffi
---_
--At
zf Lt.
t
-{
-l
zl'+
5[ st
^l
zf x+
L
zJ
x
<,
- [h
lj€-
eJ 5
8,
PROGRAMMING
WITHMATLAB
44
As a final step,determinethe value of s by typing
>>g
9. BlOO
So you can seethat even though q was definedwithin the script.it retainsits value back in
the commandworkspace.
As we will seein the following section,this is an importantdist i n c t i o nb e t w e e ns c r i p t sa u d f u n c t i o n s .
3.1.2 FunctionFiles
FunctionJilesareM-files thatstartwith thewclrdf unction. In contrastto scriptfiles,they
can acceptinput argumentsand returnoutputs.Hencethey are analogousto user-deflned
functionsin programminglanguages
suchas Fortran,VisualBasicor C.
generallyas
The syntaxfor the functionfile can be represe'nted
funcLion
outr'.rr-
=
funclanlc
( . : r - g l Ji ^ . - - t)
Z he)pconme.nLs
s L.l t emell L.s
auLva r- - v'al ue;
where outvar'= the name of the output variable,furcr.rme = the function's name,
valuesthat are passedinto
argt)st = the function'sargumentlist (i.e.,comma-delirnited
the function.l,?reTpcornnerts= tcxt thatprovidestlreuserwith infbrmationregardingthe
function(lhesecan be invokedby typing t1elp f uncnane in the cornmandwindow),and
thitt is assignedto ourvar.
thatcomputethe r,.r-/uer
srare.mL-lLs= MATLAB statements
Beyondits role in describingthe function,the first line ol'the he/pcorrurrcnts,called
by the lookf or command(recallSec.2.6).Thus,
the Hl line, is the line that is searched
you shoulclincludekey descriptivewclrdsrelatedto the file on this line.
The functioncan thenbe run by typing
The M-flle shouldbe savedas furrcnarne.m.
f uncnamein thecommandwindow asillustratedin the fbllowingexarnple.Note thateven
your computer'soperatingsystem may not be.
though MATLAB is case-sensitivc.
W h e r e a sM A T L A B w o u l dt r e a tf u n c t i o nn a r n e sl i k e f r : e el a l l v e i a n d F ' r e c t r a l l v e l a s
two diff'erentvariables,your operatingsystemmight not.
EXAMPLE
3.2
r.l
r u n c l t o nr i l e
ProblemStotement. As in Example3. l, compufethe velocityof the free-fallingbungee
jumper,but now we will usea functiorrfile for the task.
Solution. Type the following statements
in the file eclitor:
f rrnction
v
=
% f reef al1vel:
f reef aLLvel- (L, m,
bungee ',-elocitiz
cd)
rviLh
% v=freefallrrel(L,m,cd)
conputes
Z
c:f an
sjecond-order
the
objecL
frc-e-faL1
wiLh
drag
velocity
second*order
drag
input:
l-ir_,
=
ffi
--l
zl 4+
zt\
?JLJ'
zl'+
!t'i 5t
-jy
-l
^l
af xh
"L Y
a. [h
-r
VT jjg-:l
5t
3$8
45
3.I M-FILES
%
r = rime (s)
(ks)
m=mass
%
2
cd = second-order drag coef ficrent
% output:
(m/s )
%
v = dovrnward velocity
9 = 9.81;
v = sert(g
of
? acceleration
* m / cd)*tanh(sqrt(g
Save the file as f reef al lvel
and type in
>> 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+
<,- - lh
Y
jtE-
?l 5t
I
WITHMATLAB
PROGRAMMING
46
the following messagewould have beendisplayed
or variable
??? Undefined function
'S' .
So even thoughs had a value of 9.81 within the M-file, it would not have a value in the
command workspace.As noted previously at the end of Example 3.1, this is an important
distinction betweenfunctions and scripts.The variableswithin a function are said to be
local and are erasedafter the function is executed.In contrast,the variablesin a script
retain their existenceafter the script is executed.
Function M-files can retum more than one l'esult.In suchcases.the variablescontaining the resultsare comma-delimitedand enclosedin brackets.For example,the following
function,staf s.m, computesthe meanand the standarddeviirtionof a vector:
Imean, stdev] = stats (x)
function
n = length(x);
mean = sum(x) /n;
stdev .. sqr t (sum( (x-mean) .^2/ (n-1) ) ) ;
Here is an example of how it can be applied:
'>
!' -
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
<,
- th
"Y
3.2 INPUT.OUTPUT
47
This M-file would be savedas f reefallsubfunc.m. In suchcases,the first functionis
called the main or primary function. It is the only function that is accessibleto the command window and otherfunctionsand scripts.Al1 the otherfunctions(in this case,ve1) are
referred to as subfunctions.
A subtunctionis only accessibleto the main firnction and other subfunctionswithin
the M-file in which it resides.If we run f reef al lsubf unc from the commandwindow
the result is identicalto Example3.2:
;'> freefa]lsubfunc (12, 68.!, 0.25)
50.6175
However,if we atternptto run the subfunctionvel, an errormessage
occurs:
>>
vel(I2,68.1,.25)
?? ? Undefined
3.2
command/ funct
ion
've1 '
INPUT.OUTPUT
As in Section3.1, informationis passedinto the functionvia the argumentlist and is output via the function'sname.Two otherfunctionsprovideways to enterand displayinformationdirectlyusingthe commandwindow.
T h e r r , l ' r r l F u n c t i o n . T h i s f u n c t i o na l l o w sy o u t o p r o m p tt h e u s e rf o r v a l u e sd i r e c t l y
from the commandwindow.lts syntaxis
,r = input \'promptstring')
The lunctiondisplaysIhepronptstring,
value fiom the keyboard.For example,
waits for keyboardinput,and then returnsthe
m = input ('Mass (kS)r ')
When this line is executed,the useris promptedwith the message
Mass
(kg):
If the userentersa value,it would thenbe assigned
to the variablen.
The inpur funclion can also returnuserinput as a string.To do this, an ,s, is appendedto the function's argunlentlist. For example,
name = input ( 'Enter your natne: ' , ,s , )
The ,ti. 1,Function. This functionprovidesa handyway to displaya value.Its syntaxis
disp(rza-Zue)
where yalue = the valueyort would like to display.If can be a numericconstantor variable,or a string messageenclosedin hyphens.Its applicationis illustratedin the following
example.
zt+
'"1-r
ffiE\
z\4+
zf\
-x
:
zl x+
4
<, [h
--T
,t€ - ef >t
-l-
I
48
PROGRAMMI
NG WITHMATLAB
EXAMPLE
3.3
A n I n t e r o c t i v eM - F i l eF u n c t i o n
ProblemStqtement. As in Example3.2,computethe velocityof the free-fallingbungee
jumper, but now usethe inprut and disp functionsfor input/output.
Solution. Type the fbllowing statementsin the file editor:
runct ion free[-, L:
% f reef a1li: int.eracti're bungee velocity
?
Z
eo
freefalli
inter.active
computatjon of the
free fal1 rrelocity
of an object
\ r itt h s e c o n d - o r : d e r d r a g ,
% acceleration
of grraviLy
9 - 9. B1;
( l < g ): ' ) ;
m = input('Mass
(kqlm) : ') ;
cd = input ('Drag coefficient
'
)
;
(s):
t =:-nput('Time
disp('')
(m/s) :' )
drsp('Velocity
* m / cd)*tanh(sqrt(g
drsp(sqrt(g
* cd / m) * t))
Save the file as f reef a11 i . m.To irrvoke the function. return to the command window and
type
>> f reef .rl I i
Mcrss (kg): 6g.l
Drag coeffrcierrt
Time (s): 12.
(kg/m) :
0 .2 5
Velocit,lr (nr,/s):
5 0 . 6 1 ' 15
T h e t 1 , r r r r l 1 F u n c t i o n . T h i s f u n c t i o n p r o v i d e s a d d i t i o n a lc o n t r o l o v e r t h e d i s p l a y o f
infbrmation. A simple representationof its syntax ts
fprinLf
('formaL',
> < , ,. . . )
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
<,
- lh
rf €- sJ 5t
_.1
Z} Fh
I
50
PROGRAMMING
WITHMATLAB
3.2. I Creoting ond AccessingFiles
MATLAB has the capability to both read and write data files. The simplestapproachinvolves a special type of binary file, called a MAT-fi\e, which is expresslydesignedtbr
implementationwithin MATLAB. Such files are createdand accessedwith the save and
load commands.
The save commandcan be usedto generatea MAT-file holding eitherthe entire workspaceor a few selectedvariables.A simple representation
of its syntaxis
save fifename varT var2 ...
varn
This commandcreatesa MAT-file named fi-tename.mar that holds the variablesyarl
through varn. If the variablesare omitted,all the workspacevariablesaresaved.The l oad
commandcan subsequentlybe usedto retrievethe file:
load
fr lename
varl
var2
...
varrl
which retrievesthe variablesrzar-1throughvarn from f ilenante.mat. As was the case
with save, if the variablesare omitted. all the variablesare retrieved.
For example,supposethat you use Eq. ( 1.9) to generatevelocitiesfor a set of drag
coefficients:
> > 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
<, [h
-r
j.l'€- ?l 5t
&
PROGRAMMING
3.3 STRUCTURED
5l
ASCII files can be generatedin MATLAB by appending ascii to the save command. In contrastto MAl-files where you might want to savethe entire workspace,you
would typically savea singlerectangularmatrix of values.For example,
>> A=15 I
>>
save
9 2;3 6 3 9l;
simpmatrix.txt
ascii
In this case,the save commandstoresthe valuesin A in 8-digit ASCII form. If you want
to storethe numbersin doubleprecision,justappend-ascii -doubLe. In eithercase,the
file can be irccessedby other proglams such as spreadsheets
or word processors.For
example,if you openthis file with a text editor,you will see
5 . 0 0 0O O 0 O e + O 0 O7 . 0 00 0 0 0 0 e + 0 0 9 . 0 0 0 0O0 O e + 0 0 0 2 . 0 0 0 00 0 O e + 0 0 0
3.0000000e+000 6.0000000e+000 3.0000000e+000 9.0000000e+000
Alternatively,you can read the valuesback into MATLAB with the load command,
>>
load
sirnpmatrix.txt
BecausesimpmaLrix. txt is not a MAT-file,MATLAB createsa doubleprecisionarray
namedafter the fi l ename..'
'
,;irpt-rt. ' i>:
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
<, lh
x --4 - 9f 5l
,f\€
-l-
&
52
PROGRAMMING
WITHMATLAB
wherecondr tron is a logicalexpression
that is eithertrueor false.For example,hereis a
simpleM-tile to evaluatewhethera gradeis passing:
gldder(g
oder
(grade ) :
determines
whether
Iuncrion
% grader
%
% input:
r"
grade
= numerical-
qrade
value
is
of
passing
grade
(0-100 )
% output:
%
if
drsplayed
message
grade
>= 50
di sp ( 'pass rr-rg grrade ' )
end
The following illustratesthe result
>> glader(95.6)
passingr grade
For cases where only one statementis executed, it is ofien convenient to implenrent
the i f structure as a single line,
if
grade
> 60,
djsp('passing
gr.rdc'),
end
This structure is called a single-lirrc rf For cases where rnore than one statel.nentis irnplemented, the multiline if structure is usually preferable because it is easier to read.
E r r o r F u n c t i o n . A n i c e e x a m p l eo
r l ' t h e u t i l i t y o f a s i n g l e - l i n ei f i s t o e m p l o y i t f o r r u d i rnentary error trapping. This involves using the cr:ror: function which has the syntax,
er ror (msgr)
When this firnction is encountered,it displays the text message,.rsEr
and causesthe M-tile
to terminate and return to the conrmand window.
An example of its r"rsewould be where we might want to terminate an M-file to avoid
a division by zero. The fbllowing M-file illr"rstrates
how this could be done:
function
f = errortest(x)
11 1 == 0 , er ror (' 'zcrc:value
f = I/x.;
encountered'
) , cnd
If a nonzero argument is used, the division would be implemented successfully as in
>> errotLest(l0)
0.1000
Howevel fbr a zero argument! the function would ternrinate prior to the division and the
error messagewould be displayed in red typef'ace:
>> error test (L ;
??? ErroL usit'tg ==> err-or:test
zero value encountered
ffi=
zr+
'"1
"L x
:
zl x+
4
<,
- lh
Lrt9 - 9n 5
-tl
-l-
I
53
PROGRAMMING
3.3 STRUCTURED
TABTE3.2
Summoryof relotionoloperoiorsin MATLAB
Operotor
Exomple
unit -=
a< 0
Relotionship
Equol
Not equcl
Lesslhon
G r e o l e rl h o n
L e s st h o n o r e q u o l l o
G r e o i e rl h o n o r e q u o l t o
'r
s>t
3.9 .:= 3/3
r: >= 0
Logicol Conditions. The simplestform of the condr Lron is a singlerelationalexpression that comparesfwo valuesas in
vaf ue.
relaL
i,on vaf ue,
variables,orexpressions
wherethe yaf ue-scan be constants,
and the relaLion is one of
Table
the relationaloperatorslisted in
3.2.
MATLAB alsoallowstestingof morethanone logicalconditionby cmployinglogical
We will ernphasize
the fbllowing:
operators.
.
- lNot).Usedto perfbrmlogicalnegationon an expression.
- exp res-s r o,t
If the expre.sr;ion is true,the resultis false.Converscly,if the expressjon is false,
the resultis true.
* (And). Usedto perlbnn a logicalconjunctionon two expressions.
.
+;1p7 essr.,r
d
pxp/
| .';S,rOn
If both e)<pre.ssrorsevaluateto true, the resultis true. If eitheror both expre-ssrons evaluatesto false,the resultis false.
(Or).Used to performa logicaldisjunctionon two expressions.
.
FXp/
ps.-;'on
I
PxLrt r-. :'io"1
If either or both express,-i on!r evaluateto true, the result is true.
lhble 3.3 summarizesall possibleoutcomeslor eachof theseoperators.Just as fbr
arithmeticoperations,there is a priority order lbr evalualinglogical operations.These
TABTE3.3
for logicoloperotors
the possibleoutcomes
A truthtoblesummorizing
Theorderof priorityof ihe operotorsis shownol
employedin MATLAB.
the loo of thetoble.
Highesr
Lowesl
xly
x&y
ryx
T
T
T
F
I
I
F
F
"L x
4
<Ah
1-1,€
5t
8,
PROGRAMMING
WITHMATLAB
54
are from highest to lowest: -, a and I . In choosingbetweenoperatorsof equal priority,
MATLAB evaluatesthem from left to right. Finally, as with arithmetic operators,parenthesescan be used to override the priority order.
Let's investigatehow the computeremploysthe prioritiesto evaluatea logical expres=t,andy='b',evaluatewhetherthefollowingistrueorfalse:
s i o n I. f a = - 1 , b - 2 , x
a * b > 0 & b == 2 & x > 7 | .'(y > ,d )
To make it easierto evaluate,substitutethe valuesfor the variables:
I * 2 > 0 & 2 =_ 2 & 1 > 7 | _(,b, > ,d')
The first thing thatMMLAB doesis to evaluateany mathematical
expressions.
ln this
example,thereis only one: -7 x 2,
-2
>
A & 2
==
2
t''I
> 1
-('b'
>
'd')
Next, evaluateall the relationalexpressions
-2 > A & 2 -= 2 & I > I
T
F
&
&
tr
1 -('b'
l-
;' 'd')
F
At this point, the logical operatorsare evaluatedin priority order.Sincethe - hashighest
priority,the lastexpression(-r) is evaluatedfirst to give
F&T&F
T
The a operatoris evaluatednext. Since there are two, the left-to-right rule is appliedand
the first expression(p r r) is evaluated:
F&FIT
The r againhashighestpriority
FIT
Finally,the I is evaluatedas true.The entireprocessis depictedin Fig. 3.1.
The j r
. r ' t : : , , S t r u c t u r e . T h i s s t l u c t u r ea l l o w sy o u t o e x e c u t ea s e to f s t a t e m e n itfs
a logical condition is true and to executea secondset if the condition is firlse.Its general
syntaxis
if
r:ondjtion
stat€ment:,-,
else
Stdae'nlenLS..,
end
T h e i r . . { , t i ; r l t S t r u c t u r e . I t o f i e n h a p p e n s t h a t t h e t a l s e o p t i zo rnnoi f . . . e l s e
structureis anotherdecision.This type of structureoften occurswhen we have more than
two options lbr a par-ticularproblem setting.For such cases,a special form of decision
structure,the if. . . efseif hasbeendeveloped.It hasthe Seneralsyntax
if
condition,
statements,
elseif
conditron..
statements,
ffi=
qA+,
z(| Ll 2
zl'+
LA
_\
7[e1.s\
-l
^l
zl /+?
"L x
4
<,
- [h
ll'€- eJ 5t
3$8
EXAMPLE
3.4
PROGRAMMING
3.3 STRUCTURED
a*b
1^, --
>u&
ll
-r*2
Z
55
| -( Y '
x>'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
.
<o --zl-.! --I9l s\
Z]
x+
4
a[h
I
_-T
,f\t - ef
-l-
3$ 8,
56
WITHMATLAB
PROGRAMMING
Solution. First, an i f structurecan be usedto return 1 if the argumentis positive:
% mysign(x)
>
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+
<.
- [h.
il'€
-
- :l 5
I
3.3 STRUCTURED
PROGRAMMING
59
3.3.2 Loops
As the name implies, loops perform operationsrepetitively.There are two types of loops,
dependingon how the repetitionsare terminated.A for loop endsafter a specifiednumber
of repetitions. A whi I e loop endson the basisof a logical condition.
The ror . . . encl Structure. A f or loop repeatsstatementsa specificnumberof times.Its
g e n e r asl y n t a xi s
f o r i n d e x - s t a r t : s t e D : .f i n i s h
".1-uteme',rs
The f or loop operatesas follows. The rndex is a variable that is set at an initial value,
start. The programthencomparesthe rndex with a desiredfinal value,fin jsh. If the
index is lessthanor equalto the frnish, the programexecutesthe sLatemenrs. When
it reachesthe end line that marks the end of the loop, the index variableis increasedby
the srep and the programloopsbackup to the for statement.
The processcontinuesuntil
the index becomesgreaterthan the f ini sh value.At this point, the loop terminatesas the
programskipsdown to the line immediatelyfollowing the end statement.
Note that if an incrementof 1 is desired(asis ofien the case),the s t ep can be dropped.
For example,
for
i
= 1:5
djsp(i)
end
When this executes,MATLAB would displayin succession,
1 , 2 , l, 4, 5. In other
words,the def-aultstep is 1.
The size of the srep can be changedfrom the defaultof 1 to any other numeric value.
It doesnot haveto be an integer,nor doesit haveto be positive.For example,stepsizesof
A . 2 , - I , o r - 5 , a r ea l l a c c e p t a b l e .
If a negativestep is used,the loop will "countdown"in reverse.For suchcases,the
loop's logic is reversed.
Thus,the f .inj sh is lessthanthe start and the loop terminates
when the index is lessthan the f inish. For example,
for i = l0:
disp rj r
1:1
end
When this executes,MMLAB would displaythe classic"countdown"sequence:
10 , 9 ,
B, 1, 6, 5, 4,3,2,
1.
E X A M P L3E. 5
U s i n g o 1 . r L o o p t o C o m p u i et h e F o c t o r i o l
,
ProblemStotement. Developan M-file to computethe factorial.2
0!: I
ll-
I
I v
) -')
2 Note that MATLAB has a builrin function f acLoriat
zl'
,"1{t
-&
that oerformsthis comnutation
ffi=
z$4 b
st
71et
I
^
ZI\ x _-T<,- lh
9l
,f\€
4
>l
-t-
z I x+
/
&
WITHMATLAB
PROGRAMMING
60
3l:lx2x3:6
4l:lx2x3x4:24
5!:1x2x3x4x5:120
A simple function to implement this calculation can be developed as
Solution.
fout = factor (n)
function
(
n
) :
% factor
Computes the producc of
%
-
--
for
x
all
Lhe integiers
from
I
to
n'
I
t.
I
i = l:n
= x * i;
end
= x;
fout
end
which can be run as
>>
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
<, Ih
VT jl'€-
-r
sJ 5r
I
3.3 STRUCTURED
PROGRAMMING
6t
setsvalue of elementsofy dependingon whether
or not valuesof t are greaterthan one:
L
-
U:.UI:);
F^LOr
l
I
l:lerg.hrL/
t ( i ) >l
= I/t-(r);
Y(i)
else
r f
y(j)
end
end
For this case,MATLAB rnustresizey every
time a new value is determined.The follow_
ing code preallocatesthe proper amount
oi -".o.y by using a vectorizeclstatement
to
assignonesto y prior to enteringthe loop.
1, - 0:.01:5;
y - one-q(si:e(t));
for i = l:lengLh(t)
rf r (i )>l
y(i)
- Iir(i);
encj
end
Thus, the anay is onry sized once. In
addition, preailocation herps reduce memory
frag_
mentation, which also enhancesefficiency.
The r.;hLt L:,Structure.
general syntax is
A while
lclop repeats as long as a logical condition
is true. Its
vrh,l. e^ndirian
st;tte,rnert,s
end
The statemencs betweenthe whiLe
anclthe end are repeatedas long as the condr jon
t
is
t r u e .A s i m p l ee x a n r p l ei s
vO
while
disp
x
>
0
(x)
encl
When this cocleis run. the resultis
E
5
2
-1
T h e r r ' i l r : ' ' ' 1 r r r - : ' r sk t r u c t u r e '
A l t h o u g ht h ew h i l e s t r u c t u r ies e x t r e m e r u
y s e f u rt,h e
f'act that it always exits at the beginning
lf the structureon a false result is somewhat
constraining'For this reason'languagessuch
as Fortran90 and visual Basic have special
structuresfhat allow loop terminationon a
true condition anywherein the loop. Although
suchstruduresarecurrenrlynor availablein
MATLAB, tt er. iun.tion;ii;.""
be mimicked
-WT-Tbl
zl
zt -+
-tA-\
aA+,
a s[sl
Ll 2
-{
L
4
af x+?
x
<. lh
lj€
- eJ 5t
%8
62
PROGRAMMING
WITHMATTAB
by a specialversionof the while loop. The syntaxof this version,called a while...
break structure,can be written as
r,vhile
(1)
stafements
if
conditron,
break,
end
.'rXtutements
wherebreak- terminatesexecutionof the loop.Thus. a singleline i f is usedto exit the loop
if the conditionteststrue.Note that as shown,the break can be placedin the middle of the
loop (i.e., with statementsbeforeand atier it). Sucha structureis calledarnicltestloop.
If the problem requiredit. we could placethe break at the very beginningto createa
prel(.stloop.An exampleis
(1)
while
If
<
x
-;
0,
-
w
break,
end
l.
end
Notice how 5 is subtractedfrom x on eachiteration.This representsa rnechanismso that
the loop eventuallyterminates.
Every decisionloop must havesucha mechanism.Otherwise it would becornea so-calledinfinite loop that would neverstop.
at the very end and creAlternatively.we couldalsoplacethe i f . . . break statement
ate a posttest lo)P,
3
(1)
whrle
q.
if
x
<
(.), break,
end
end
EXAMPLE
3
It shouldbe clear that, in fact, all threestructuresare really the same.That is, depending on wherewe put the exit (beginning,middle,or end)dictateswhetherwe havea pre-,
nrid- or posttest.It is fhis simplicity that led the computerscientistswho developed
Fortran 90 and Visual Basic to favor this structureover ofher forms of the decision looo
suchas the conventionalv;hile structure.
The 1,,,,,',,-.
Commond. Thereare often timeswhen you might want a programto temporarily halt.The commandpause causesa procedureto stopand wait until any key is hit.
A nice exampleinvolvescreatinga sequence
of plots that a userrnight wanf to leisurely
perusebefbremoving on to the next.The following codeemploysa f or loop to createa
sequenceof interestingplots that can be viewed in this manner:
for n = 3:10
mesh (magic (n) )
pause
end
The pause canalsobe formulatedaspause (n ), in which casethe procedurewill halt
fbr n seconds.This featurecan be demonstratedby irnplementingit in conjunction with
severalother usefulMATLAB functions.The beep commandcausesthe computerto emit
a beep sound.Two other functions.t ic and toc, work togetherto measureelapsedtime.
ffiat
-
=L
,r,
4
1+%-q'tr
-zt+
zl ,N17
x
<,
- [h
j]\€ - ?l 5t
I
63
3.4 NESTING
ANDINDENTATION
The tic command saves the cuffent time that toc later employs to display the elapsed
time. The following code then confirms that pause (n)works as advertisedcomplete with
sound effects:
tic
beep
pause (5)
beep
toc
When this code is run, the computer will beep. Five seconds later it will beep again and display the following message:
ELapsed time
is
5.006306 seconds.
By the way, if you ever have the urge to use the command pause ( inf ), MATLAB will go
into an infinite loop. In such cases, you can return to the command prompt by typing
Ctrl*C or Ctrl+Break.
Although the foregoing examplesmight seema tad frivolous, the commandscan be
quite useful.For instance,tic and toc can be employedto identify the partsof an algorithm that consumethe most executiontime. Further,the Ctrl*C or Ctrl+Break key
combinations
comein real handyin the eventthatyou inadvertently
createan infiniteloop
i n o n eo f y o u r M - f i l e s .
3.4
NESTINGAND INDENTATION
We needto understandthat structurescan be "nested"within eachother.Nc.sllrrcref-ersto
placingstructureswithin other structures.
The following exampleillustrtrtes
the concept.
E X A M P L3E. 6
N e s t i n gS t r u c t u r e s
ProblemStqtement. The rootsof a quadraticeqLration
/(x):uxl+lrx+r'
can be determinedwith the quadraticformula
-b +.,t8-- 4aa
LA
Developa functionto implementthis fbrmulagivenvaluesof the coeff'cients.
v
Solution. ktp-down r/e.rlgnprovidesa nice approachfor designingan algorithm to compute the roots.This involvesdevelopingthe generalstructurewithout detailsand then
refining the algorithm.To start,we first recognizethat dependingon whetherthe parameter
a is zero,we will eitherhave"special"cases(e.g.,singlerootsor trivial values)or conventionalcasesusingthe quadraticfbrmula.This "big-picture"versioncan be programmedas
a
f - u n c t i . o nq u a d r o o t s ( a , b , c )
% quadroots:
llt
%
Lth
nit
]e.
z
quadroots
roots
of
(a, b, c) :
quadratic
real
and
o'quod'ot
equation
complex
j-
aqua'
rool-s
jon
% input:
9o
o
ffi
z$4
?JLI'
zl+
lo.ond
J
J
"t
otdet
coefficient
:I 5\
Z] Xh
"L x
4
<, [h
--4
1.1':- sl 5t
I
64
PROGRAMMING
WITHMATLAB
Z
b = fir-st-order
Z
c = zera order
% output:
Z
rI = real part
Z
i1 = imagrinary
Z
12 = real part
%
i2 = imaqinary
1t
==
d
coefflcient
coefficient
of first
root
part of flrsL
root
of second root
part of second root
u
%special
cases
els<:
"ir'r
'^rl-L1a
e
'dr,tl
end
Next, we developrefined codeto handlethe "special" cases:
%special
cases
if
b -'= 0
e;srngle
root
11
|
V,
else?tr i vial
sol ut ior-r
error('Trivial
solutron.
Try
again'
)
end
And we can developrefinedcodeto handlethe quadraticformulacases:
%qu.rdrat ic
- b ^ 2
if
cl >- O
f or:mul a
4 * a *
d
c;
rooLs
%real
r1 = ( b + sqrt(d)
)
( b - sqrt.(d))
t'2 -
/
/
12 *
(), *
a)
a)
el se
%complex roots
=
rl
b /
\2 * a)
i1 = serL(.rbs(d))
r)
/
(2
*
a)
r-l
end
We can thenmerelysubstitutetheseblocksback into the simple"big-picture"framework to give the final result:
quadrooLs
functron
?
quadroots:
quadroots
%
(a,
roots
of
(a ,b, c) :
%
b, c)
quadratic
real
of
input:
%
a =
Z
b -
equafion
and compl-ex roots
quadratic
equation
Z
%
c
second-order
first
-
zero-aYder
% ouLput:
%
rI
Z
i1
order
=
real
part-
imaginary
coefficient
coefficient
coefficient
of first
part- of
roo[
first
ffi=
zl+
,"1{z
5fi st
7t
l-l
3l x+
root
"L x
4
<, [h
Lrtg -
:.I
9r\
5
-J
I
3.4 NESTING
AND INDENTATION
%
Z
ai Fr
a
65
12 = r.eal- part of second
root
12 = imag,inary part of
second root
==
u
Sspecial cases
ifb-=0
?single
roor
1L--c/b
else
?trivial
solution
( ' ?rivial
solut ion.
g^l *
l u: r . o .
?ry
agaln , )
e-Lse
Sguadratic
formula
d=b
* c;
4 *a
Ediscriminant
rf d >= 0
?rea1 roois
ri- = (-b + sqrt(d))
/ (2 * a)
12=
(-b-sqrr(d))
/ (2*a)
else
Scomplex roots
rl-=-b/(2*al
i1 = gqy; (abs (d) ) / (2 *
a)
r)
-
v1
l .
! .
-
- f
a
end
end
o.r highrightedby the shading,notice
how indentationhelps to make the
, .
underlying
logical structurecrear.Also nodc;how "modular,,the
structuresare. Here rs a command
window sessionillustratinghow the
function f"rfo.rs
>> quadroots(1,1,1)
17 =
-0.5000
=
i1
0.8660
12_0.5000
-0.8660
>> quadroots (l, 5, 1
)
-0.2087
4.7913
>> quaCroots (0,5,1)
-0.2000
ml
zI
zl Llz
zl' +
,
t-r
_\
+
4-{
5[st
Z] X+
"L x
a,- . [h
VT Lf €- 1 5t
'$8
66
PROGRAMMING
WITHMATLAB
j
>> quadroots (0, 0, 0 )
??? Error using ==> quadroots
TriviaL
solution.
Try again
PASSINGFUNCTIONS
TO M.FIIES
3.5
Much of the remainderof the book involves developingfunctionsto numericallyevaluate
other functions.Although a customizedfunction could be developedfor every new equation we analyzed,a betteralternativeis to designa genericfunction and passthe particular
equationwe wish to analyzeas an argument.In the parlanceof MATLAB, thesefunctions
are given a special name:function.functictns.Before describinghow they work, we will
first introduceanonymousfunctions,which provide a handy meansto define simple userdefinedfunctionswithout developinga full-blown M-file.
3.5.1 Anonymous Functions
Anonyntous.functionsallow you to createa simple function without creating an M-file.
They can be definedwithin the comrnandwindow with the following syntax:
ihandLe = @(argJist) expressro,r
where f.harrdle - the functionhandleyou can use to invoke the function,argTist = a
comma separatedlist of input argumentsto be passedto the function.and express l on =
any singlevalid MATLAB expression.
For example,
>>
fl-@(x'Y)
x^2
+ Y^2;
Once thesefunctionsare defined in the commandwindow, they can be usedjust as other
functions:
>>
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\
-
<, [h
x --T - 91 5l
,r\€
"L
z,J
-l-
-l
Z] X+
I
68
PROGRAMMING
WITHMATLAB
FIGURE3.2
A ploio{velocily
generoted
versus
trrne
wilhthefplor funclion
E X A M P L3E. 2
n gF u n c t i oFnu n c t i o n
B u i l d i no
gn d l m p l e m e n t io
ProblemStqtement.DevelopanM-filefunctionfunctionto determine
value
theaverage
jumpervelocityovertherange
of a functionovera range.lllustrate
its usefbr thebungee
f t o mr : 0 t o l 2 s :
u(r):
ffo"n(rH,)
whereg:9.81,m:68
Solution. The averagevalue of the function can be comnutedwith standardMATLAB
commandsas
>> 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
<, lh
-r
-
z[ Ll 2 --zl
?,1+
t"l
"L x j.l'€ -
5[ st
-r,ar
-l
Z] X+
4
eJ 5t
I
3.5 PASSING
FUNCTIONS
TOM.FILES
69
We can write an M-file to perform the samecomputation:
funcr
o
ion
e"
I uncavg {o, b, n )
lavg
^.
f ',-^r-
f-vg
u
v
L
!
s Y
f rra.l:.
ro
n
funcavg(o,b,n):
l-,ohL
compuLes average
of function
over
%
% input:
2
a = lower bound of ranqe
%
b = upper bound of range
%
n - number of intervals
% output:
%
favg = average value of functron
x = linspace(a,b,n);
y = func(x);
favg - mean(y) ;
end
value
d range
function
f = func(t)
f-sqrt (9. B1*68.I / 0. 25 ) *tanh (sqrt (9. B1*4.25 / 68. 1 ) *t ) ;
end
The main function first uses linspace to generate equally spaced -r values across
the range. These values are then passed to a subfunction func in order to generate the corresponding y values. Finally, the average value is computed. The function can be run from
the command window as
>> 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
<. [h
^r-
sJ 5t
^l
Z] X+
3$&
74
PROGRAMMI
NG WITHMATLAB
PROB
continued
As a final note, we should recognize that the foregoing code is not generic. That is, we
have designed it to solve the specific problem of the velocity of the bungee jumper. A more
generic version can be developed as
f L r n c - ' t i o ny e n d = , o d e s i r n p ( d y d r , C t ,
y = yi.; h = dt;
L - ti;
(1)
whiIe
j 1' t + c]t- ; Lf, h =, t-f
L; t.rrd
.t -./
+ dyclL(1) * I'ii
f
ts
-
t- '=
i{
t i,
t,f , y j )
break,
cos"r: I -
Create an M-file to ir
putes and displays thr
series is added. In o
sequencethe valuesfc
'l
COS.r:
x2
cos-r: I -
h.
tf,
3.1 The cosine funct
infinite series:
r:nd
t
c-nrl
= Y'
Yenrl
-2
COS,f :
Notice how we have strippedout the parts of the algorithm that were specific to the
bungeeexample(including the subfunctiondefining the differentialequation)while keeping the essentialfeaturesof the solutiontechnique.We can thenusethis routineto solvethe
bungeejumper example,by specifyingthe differentialequationwith an anonymousfunction and passingits function handleto odesimp to generatethe solution
- - . r l . r < . l t i. a ( v )
q . I l . - \ ( . ) .2 \ / 6 f . r . L ) * u " " - ' .
o r , l e : r 1m p ( c 1 v c 1 l ,(,r . l , a r , l : l . , l l )
,.:
,lI
We could then analyzea different function without having to go in and modify the
M-file. For example,if .y: lQ al t :0, the differentialequationdy/dt : -0. l.y hasthe analytical solutionjy : l0e-0 r'. Therefore,the solutionat t : 5 would be y(5) : l0e*0 r('5)
6.0653.We can use oclesimp to obtainthe sameresultnumericallyas in
' ' : . o d . : i i n r r ,( 0 ( y
)
-0.
11.
up to the order term o
ceding, compute and d
true - se
-
'r(l-92b'r
l * y , l l . i J { . ) ' :(,1 , ! , . 1{ r )
;*
:
"/oeffOr
-
ll
I -
As a test case,employ
up to and including e
x t o1 1 4 r . .
3.2 An amountof mon
interest is compounde
worth F yielded at an
determined from the fo
F:P(l*i)"
Write an M-file that wi
vestment for each year
function should includt
rate i (as a decimal),an
future worth is to be ca
a table with headingsa
gramforP:$100,00(
3.3 Economic formulr
payments for loans. Su
money P and agree to
interest rate of i. The
ment A is
b.0i,4'l
i/ | a;\r
A-
ffi
,*ufleqqte
zr+
--
zl F+
x
-a,th
-r
jl€ - :l 5t
3$ &
P
"'
"t
(1+i),-t
PROBLEMS
75
PROBtEMS
we
Dre
3.1 The cosinefunction can be evaluatedby the following
rnfinite
series:
x2
.t4
c o s . r : I - -) t + | |
Createan M-file to irnplementthis folrrrula so that it computesand displaysthe values of cos x as each term in the
seriesis added. In other words, compute and display in
sequence
the valuestbr
C O s - r :I
cos-r: I cos.r: I -
the
y the
) anal5t
+-
where Z,u""u: the averageannual temperaturc.In.,,s: thc
peaktcmpelaturc,o : thc frequencyof thc annualvariation
(:2n1365). and tn"^r.: day ol' the pcak tempcrature
(=205 d). Paramcters
fbr someU.S. towns are listcd here:
City
4l
upto thc orderterm ol'youl choosing.For eachol-thc preceding,computeand display the perccntrelativeerror as
T.error:
7 : 4."^n * (Q"ar.-- I."nn) cos((,(/ - lp"ol))
x4
,
:
-"Y
) the
[nc-
2l
Write an M-file to compLrte
A. Test it with P : $55,000and
an interestrateof 6.6% (l : 0.066).Conlpu(eresultsftrr l :
1 , 2 , 3 , 4 , a n d5 a n dd i s p l a yt h er e s u l t sa s a t a b l ew i t h h e a d ings and columnsfbr n and A.
3.4 The averagedaily temperaturefor an area can be approxirnaledby the followingfunction:
T-.on ("C)
T"""r ("C)
221
231
52
t0 6
la7
283
336
221
)7.6
229
AAiomi,
FI
Yumo,AZ
B i s m o r cN
k ,D
Seoiile,WA
Boston,MA
lrue - senesapproxrmatlon
^ 1007,
true
Dcvelop an M-file that cornputesthe averagctcmperature
two claysol' thc year {irr a particularcity. Tcst it
hetrvcen
As a testcase.employ the prograrnto corllputecos(L5) fbr
(a)
in Miarni. FL 1l : 0 to -59)and
lirr
January-Fcbruary
is,
tcrm
up to and including eight tcnrs-that
up to thc
(b) JLrlyAugusttcnrpcraturc
in Boston,MA (t: 180to 242).
r'* f141..
3.2 An amountol rnoneyP is investcdin an accountwherc 3.5 Figurc P3.-5shows a cylindrical tank with a conical
inlerest
is conrpoundcd
at thc cnd ol'thc pcriod.Tlrc luturc' basc.lf'the liquid levcl is quitc low, in the conicalpart.thc
yielded
worthF
at an intercsl ratc I aficr n periodsrnay be v o l u m ei s s i m p l yt h e c o n i c a vl o l u m eo l ' l i q u i d .l f t h e l i q u i d
level is rnidrangcin the cylindricalpart,thc totrl volLrncol'
liom thc lbllowing fbrmula:
deternrined
liqLridincludcsthc fillcclconicalpart and thc partiallytillcd
F=P(l+i)"
c y l i n d r i c apl a r t .
Usc dccisionalstructures
to writc an M-f ile to compule
WriteanM-filc that will calculatethe futurcworth of an inthe tank'svolumc as a flnction ol'given valucsof'R and 11.
vestment
lbr each ycar lrorn I through rr. The input t0 the
Desisnthc tunctionso thatit rcturnsthe volurncfirr all cascs
function
shouldincludethe initial investmentP, thc interest
rateI (asa decinral),and the numbcrof yearsn firr which the
worthis to be calculatcd.The output shouldconsistof
future
atablervithhcadingsand columnsfirr n and F. Run the progtamfor
P : $100,000,t : 0.06,and n : 7 years.
3.3Economictbrmulas are available to compute-annual
payments
for loans.Supposethat you borrow an amountof
money
P and agreeto repay it in n annrralpaymentsat an
inlerest
rateol l. The tblmula to computc the annual payment
A is
NT
t-p
W
iil + l)"
(l+i),-l
FIGUREP3.5
ffiE\
zJLJt
-&
+,
<Ah
ZN
4Y
-1 st
4
fi:-
sp 5t
l -
3$8
76
PROGRAMMING
WITHMATLAB
where the depth is less than 3R. Return an error message The difliculty arisesfor the other cases.The following table
("Overtop") iI you overtopthe tank-that is, d > 3R. Test it
summarizesthe possibilities:
with the lbllowine data:
xy0
R
d
1.5
I
t5
2
t5
45
t<
<0
<0
<0
:0
:0
:0
>0
<0
:0
>0
<0
:0
ton l"-,zxl
* r
loo lr,/.rJ- 1T
T
rl2
-n /2
0
Note that the tank's radiusis R.
3.6 Two distancesare required to specify the location of a
point relative to an origin in two-dimensionalspace
(Fig. P3.6):
Write a well-structuredM-file to calculater and 0 as a func. The horizontal and vertical distances(-r, y) in Cartesian tion of -r and_r'.Expressthe final resultsfor d in dcgrees.Test
your programby evaluatingthc fbllowingcases:
coordinates.
. Thc radiusand angle(r, d) in polarcoordinates.
xJr|
It is relatively straightfbrwardto computeCartesiancoordinates(-r,.y) on the basis of polar coordinatcs(r,6). The
reverseprocessis not.sosirnple.The radiuscan be cornputcd
by the fbllowing lbrmula:
where U: velocity(m
cocfficient,B: width
ing data is availablefo
0.035
0 020
o 0t5
0 030
o 422
,:l*?-+v,
If the coorilinateslic within the first and lburth coordinates(i.e.,x > 0), then a sirnplelbrmula can bc uscd to
compute9:
rl : tan I (I)
\_r,/
0
0
0.0
OO
00t
0.0
00(
I
3.7 Dcvelop an M-filcr to detcrmine polar coordinatesas
descrihedin Prob. 3.6. However, rathcr lhan designingthe
function to cvaluatea single casg,passvectorsof ir and).
Havethe f unctiondisplaythe resultsas a tablewith columns
forx,.y, r, and().Testthe prograrnfbr the casesoutlinedin
Prob.3.6.
3.11Dcvelop an M-lllc function that is passeda numenc
grade liom 0 to 100 and returnsa lettcr grade accordingto
the schcmc:
Write an M-file that cor
channels.Enter thesev
umn represents
a param
nel. Have the M-file di
computedvelocityin ta
colurnn.Includeheadin
3.10 A simply suppo
F i g . P 3 . 1 0 .U s i n g s i n g
along the heamcan be e
-0
Criterio
A
B
C
D
+ 7 5 ( x- 7
90 <
BO 1
/O .
60 <
n u m e r i co r o d e < l 0 C
n u m e r i cg r o d e < 9 0
n u m e r l cq r o d e . B O
n u m e r i cc r o d e < / 0
n u m e r i cg r o d e < 6 0
F
By dcfinition,the sing
follows:
(x-tt)":lt*-o
l0
to computethe velocity
3.9 Manning'scquationcan be r.rsed
of water in a rectangularopen channel:
FIGUREP3.6
Jsr
DevelopanM-filethat<
distance
alongthebeam
the beam.
BH \2'
il \.8+2H/
ffi=
n
=
,f 4+-4-n-vI
-\
?,t'+
+Y
3l x+
<,
- [h
jl€ - sJ 5t
I
77
PROBLEMS
20 kips/ft
:-c
F I G U RP
E3 . I O
where
cylinder
U: vclocity(rn/s),S: channclslope,n - roughness 3.1l ThcvolumcV of liquidin a hollowhorizontal
r andlcngthL is related
to thedeptho1'theliquid
B: width (m), and H : dcpth(rn).The tbllow- ol'radius
coefficient.
hbv
lbr llve channels:
ingdatais availahle
l-,
,/r
V-lt-cor'[L
\r
n
0035
0020
00r5
0030
0422
r0
0 000t
0 0002
0 0010
0 000/
0 0003
B
20
24
t5
hr
|
/
rr
--l
htr2
/ rlt h,IlI
Dcvclopan M-fllc to cl'ciltea plot ol'volume versusdepth.
Tcst thc program l-orr - 2 rn and L: -5m.
3.12 Dcvclop a vectorizedvclsion ol'thc lbllowing codc:
1,:ildrL=U;
tc,ncl-ilfl; rri
5;
L ( L) -ts1,.t1L;
Write
anM-filcthatconrputcsthc vclocitylirl cachol'thcse
Enterthesevaluesinto a matrix whcrc cach colchannels.
umn
reprcscnts
a prramctcrand cach row rcprcscntsa channel.
Havethc M-l'ilcdisplaythc input data along with thc c l r t ( l
vclocityin tabularfirm whcrc velocity is the l'ilth
computed
3.ll Thc "dividc and average"method,an old-timcmcthod
lncludchcadingson thc tablcto lahcl thc colurnns.
column.
lirr approximating
thc squareroot ol'any positivcnumbcra,
3,10A simply supportcdbcant is loadcd as shown in
can bc firrrnulatcdas
Fig.P3.10.Using singularityf'unctions.thc displacerrncnt
.r + ulx
along
thebcamcan bc cxprcsscdhy thc ccpration:
2
-s
t5
'
Writc a wcll-structurcd M-l'ilc lunction bascd on a
:'*l . . (.r x)
lrr.r)--l'r-0)'-(r
oo
w h i l - - . . . b | e a k l o o p s t r u c t u r ct o i r n p l c m c n t h i s a l g o 57 r rithur.Use ploper indentationso that thc structnrcis clcar.At
'\ 23tt.25
t
* ? 5 ( r 7 ) r+
clrch\tcp c\tinrirtcthc crrrrrin y,rurirpprtrrillluli()n
irs
6
t._,,,1
Bydetinition,
thc singularitylunctioncan be cxprcsscdas
" - t -I t r n r u '
l
follows:
Rcpeatthe loop until a is lessthan or cqual to a spccified
(.
rtl"
when,r > 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
0<t<10
10<t<20
20<r<30
/ > 30
w h e r em o : t h e m o n t h ( l - 1 2 ) , d a : t h e d a y ( l - 3 1 ) , a n d
leap : (0 for non-leap year and I for leap year).Test it for
Januaryl, 1999,February29,2000,March 1, 2001,June21,
2002, and December31, 2004. Hint: A nice way to do this
combinesthe for and the switch structures.
3.17 Develop an M-file function to determinc the elapsed
days in a year.The first line of the function should be set
up as
function nd = days(mo, da, year)
othcrwise
w h e r ei r o : t h e m o n t h( l - 1 2 ) , d a : t h e d a y ( l - 3 l ) , a n d
year : the year.Test it for January l,1999, February29,
2000,March l, 2001,June21,2002,andDecember31,2004.
3.18 Developa lunctionfunctionM-flle thatreturnsthe differencebetweenthc passcdiunction's maximum and minimum value givcn a range of the independentvariable. In
addition,havethe lunctiongenerate
a plot of the functionfbr
the range.Test it for the following cases:
function xr = rounder(x, n)
( a ) . l ( 0 : l 0 e - 0 2 s ' s i n- ( l4 ) f r o mt : o t o 6 T .
Testthe prograrnby roundingcachof thc fbllowing to 2 dec- (b)
/(x) : es'sin(l/x) liom -r : 0.01 to 0.2.
-0. 125,
inraldigits:r : 467.9587, 467.958'7,0.125,
0. 13,5,
(c) The built-inhumpslunclionliom x : 0 to 3.
a n d- 0 . 1 3 5 .
3.19 Modily the functionfunction odesimp developedat
-1.16 Develop an M-filc function to dctcrmine the elapsed the end ol'Sec. 3.6 so that it can bc passcdthc argumcntsof
days in a year. The f'irst linc ol' thc function should be set the passedlunction.Tcstit lbr thc lbllowing casc:
up as
Develop an M-flle function to compute u as a function of /.
Then, use this lunction to gcneratea plot of u versus/ fbr
t5to50.
3.15 Developan M-file lunctioncallcdrounder to rounda
number-r to a specifiednumberof dccimal digits,n. The first
line of the f'unctionshouldbe setup as
(c<1/m)*v^2;
d v c l t - @ ( v , m , c : c 1) 9 . B L
:':' oclesirnp (dvtlt , 0. 5, 0, L2 ,0 , tt9.I, A .25)
>>
f uncrt i.rn
nd
=
days
(mo,
_q
zt'+
,"1{t
cla,
le.rp)
ffir
+,
71--syst
-.t
^l
Z] X+
3$8
.
;.1ffi
""j
'rt6;"',,1li,ii
: i irl:r 1,,1
."lii-: i
i 1 ) ,a n d
:st it for
I u n e2 1 ,
do this
Roundoff
Errors
ond Truncotion
elapsed
J be set
ll), and
nry 29,
t,2001.
; the difrd miniiable. In
ction for
CHAPTER
OBJECTIVES
The primaryobjectiveof this chapteris to acquaintyou with the major sourcesof
errorsinvolvedin nurnericalrnethods.
Speciticobjectivesand topicscoveredare
:loped at
mentsof
'
'
'
'
r)
'
'
'
t
'
Understanding
the distinctionbetweenaccuracyand precision.
Learninghow to quantityerror.
Learninghow errorcstimatescan be usedto decidewhen to terminatean iterrti','e
calculation.
Understanding
how roundofferrorsoccllrbccausedigitalcornputers
havea
limitedahility to represent
nunrbers.
Understanding
why lloating-pointnumbershave[irnitson their rangeand
precision.
Recognizingthat truncationerrorsoccurwhenexactmathematical
fbrmulations
ilrc represcnlcd
by approxirnations.
Knowing how to uscthe Taylorseliesto estimatetruncationerrors.
Understanding
how to write lbrwarcl,backward.and ccnteredfinite-difTerence
approximations
of first and seconcl
derivatives.
R e c o g n i z i ntgh a te l ' f i r r tlso r n i n i m i z el " r u n c r r t i e
o rnr o r sc a ns o m e l i m eisn c r c a s e
roundofTerrors.
YOU'VE GOT A PROBLEM
n Chap. I, you developeda numericalmodel for the velocity of a bungeejumper.To
solve the problemwith a computer,you had to approximatethe derivativeof velocity
with a llnite difl-erence.
!]=!!dr
Lt
u(ti+r)-u(ri)
t,+t - ti
79
80
R O U N D O FA
F N D T R U N C A T I OENR R O R S
Thus, the resultingsolution is not exact-that is, it has error.
In addition,the computeryou use to obtain the solutionis also an imperfecttool. Bethe magnitudes
causeit is a digitaldevice,the computeris limitedin its ability to represent
precision
yields
Consequently,
the
itself
and
of numbers.
machine
resultsthatcontainenor.
your
your
both
mathenlatical
digital
So
approxir.nation
and
computercauseyour lrprediction
problem
How
rnodel
to
Your
is:
sulting
be uncertain.
do you deal with suchunyou
introduces
to
sonle
and
certainty'?This chapter
approaches conceptsthat engineersand
with
this
scientistsuseto deal
dilemma.
4.I
ERRORS
constantlyfind themselves
havingto arccomplish
objectivesbased i
Engineersand scientists
goal,
perf'ection
rarely
if ever atis
a
laudable
it
is
on uncertaininformation.Although
For
tact
that
the
developed
liom
Newton's
second
law
tained.
rnodel
example,despitethe
jumper's
never
practice
predict
fall.
is an excellentapproxirnation,
it would
in
cxactly
the
would resultin deA varietyof factorssuchas windsand slightvariationsin air rcsistance
high or low, thenwe
viationsftom the prediction.If thesedeviationsare systematically
new
rzrndomly
might needto devclopa
modcl.However,il-theyare
distributedandtightly
groupedaroundthe prediction,thenthe deviationsrnightbe considered
negligibleandthe
Numerical
introduce
similar
discrepancies
model deemedadequate.
approxirlationsalso
i n t o t h ea n a l y s i s .
quantification,
This chaptercovcrsbasictopicsrclatedto tlreidentification,
and rninimizaticlnof theseerrors.Ccneralinformationconcernedwith the quantificationol'erroris
reviewedin this section.l-his is fbllowed by Sections1.2 and4.3, dealingwith the two
(dueto computerapproximations)
andtrunmajorfirrmsof numericalerror:rclundof'l'crror
(due
We alsodescribehow strategies
to recationerlor
to mathenraticalapproximations).
roundolT.
we
increase
Finally,
briefly discusscrrorsnot
duce truncationerror sometinres
Thess
includeblundcrs,rnodel
directlyconnectedwith the nurnericalnrethodsthemselves.
errors.and datauncertaintv.
I
I
i
I
4.1.1 Accurocy ond Precision
with
The errorsassociated
with both calculations
can be characterized
arrdmeasuremenls
rcl'ersto how closelya corlputedor mearegardto theiraccuracyand precision.Att'unrc'.v
ref'ersto how closelyindividuitlcomputed
suredvalueagreeswith thetruevalue.Prct'1.\'iorr
or measuredvaluesagreewith sachother.
Theseconceptscan be illustratedgraphicallyusing an analogyfiom targetpractice.
The bulletholeson eachtargetin Fig.4. l can be thoughtof as the predictionsof a numerical technique,whereasthe bull's-eyerepresents
the truth.Inaccurucy(alsocalledbirr.i)is
defirreclas systematicdeviationfrom the truth.Thus, althoughthe shotsin Fig. 4.lc are
more tightly groupedthan in Fig.4.la, thc two casesarc equallybiasedbecausetheyate
both cerrteredon the upperleft quadrantof thc target.Imltrecision(alsocalledurrc:ertaintl),
on the other hand,ref'ersto the magnitudeo1'thescatter.Therefore,althoughFig. 4. lD and
d are eqLrallyaccurate(i.e..centeredon thc bull's-eye),the latteris more precisebecause
the shotsaretightly grouped.
I
e
a
to
w
L(
rh
8r
4.I ERRORS
Increasingaccuracy
ol.Beritudes
n errol'.
our rerch unersand
o
0)
a
s based
)ver atrnd law
:r'sfall.
It in delhen we
I tightly
and the
:pancies
r c lm i n i 'error
is
the two
s to reors not
. rlodel
o)
FIGURE
4.I
i l l u s t r o t i nt hg e c o n c e p l so l o c c u r o c yo n d p r e c i so n .
frommorksmonship
Anexomple
( o ) i n o c c u r o t cen d r m p r e c i s e( ,b l o c c u r o t eo n c ii n r p r e c i s e( c, Ji n c c c u r o t o
end precise,
n - d { r J )o ( ' L r o ' ee r ', J p e c : s e .
r\umerrcalmethoclssnoulcloe sufiicrenrlyaccura("e
or unorused("omeet irc requtreparticular
problem.They also shouldbe preciseenoughfbr adequatedesign.
nrentsof a
In tlris book, we will use thc collectiveterm errur to representboth the inaccuracyand
implccision
t l l 'o u r p r e d i ci to n s .
4.1.2 Error Definitions
ed with
or rneamputed
rractice.
numerbia.s)is
4.I c' are
they are
rtainty),
l.l b and
because
Numericalerrorsariscfrom the useof approximations
to rL'present
exactmathematical
operationsand quantities.For sucherrors,the relationshipbetweenthe exact,or true,result
and thc approximation
carrbe lbrmulatedas
True value: approximation
f error
( 4 .l )
By rearrangingEq.(4. l), we find that the numericalerror is equal to the discrepancy
b e t w e e nt h el r u t hi t n dt h eu p p r o x i m a l i o n
a .s i n
6 r : t r u ev a l u e- u p p r o x i m a t i o n
(4.21
whereCr is usedto designate
theexactvalueof theerror.The subscriptt is includedto designatethat this is the "true" error.This is in contrastto other cases,as describedshortly.
wherean "approximate"estimateof the enor must be employed.Note that the true error is
commonly expressedas an absolutevalue and referredto as the absoLuteerror.
A shortcomingof this definitionis that it takesno accrount
of the order of magnitudeof
thevalueunderexarnination.
For example,an errorof a centimeter
is muchrnoresignificant
82
ERRORS
ROUNDOFF
AND TRUNCATION
4.
if we are measuringa rivet than a bridge. One way to accountfor the magnitudesof the
quantitiesbeing evaluatedis to normalizethe error to the true value,as ir-
wl
be
pe
to
True fractionalrelativeerror :
true value - approxlmatlon
true value
The relativeerrorcan alsobe multipliedbv 100%,toexpressit as
Th
to
aln
true value - approximation
cl
-
true value
1004/t
where e, designatesthe true percentrelativeerror.
For example,supposethat you havethe taskof measuringthe lengthsof a bridgeand
If the true valuesare 10.000and
rivet
and come up with 9999 and 9 cm, respectively.
a
10 cm, respectively,
the enor in both casesis I cm. However.their percentrelativeerrors
Thus,althoughbothnteacanbe computedusingEq. (4.3) as0.0I 7oand l0olo,respectively.
for
rivet is much greater.We
the
1
cm,
the
relative
error
error
of
surcmentshavean absolute
job
measuringthe bridge.
of
probably
that
we
have
done
an
adequate
wor.rlcl
conclude
to
be
desired.
for
rive(
leaves
sornetl.ring
whereasour estimate the
with a 1 to signifythatthe
Noticethat for Eqs.(4.2)and (4.3),t and e aresubscripted
rivet
For
exanrple
of
the
and
the bridge,we wercprothe
value.
the
error is basedon
true
such
infbrrnation
is rarcly available.
in
situations
with
this
value.
Howcvcr,
actual
vided
wlren
we
deal
with functionsthat
value
will
only
be
known
nrethods,
the
true
For nurnerical
when
we
investigate
the theothe
will
typically
bc
case
can be solvedanalytically.Such
in
real-world
apHowever,
particular
fbr
simple
systerns.
of a
techniqLre
reticalbehavi<,rr
plications,we will obviouslynot know the true answcra priori. For thesesiturtions.an
alternativeis to norrnalizetheerrorusirrgthebestavailableestimateof thetrue value-that
i s , t o t h ea p p r o x i m a t i oint s e l f ,a s i n
error'
approxrmate
l00a/(
approximation
wherethe subscripta signifiesthat the error is norrnalizedto an approximatevalue.Note
Eq. (4.2)cannotbe usedto !:alculatetheerrttrtermin
alsothatfbr real-worldapplications,
the nunteratorof Eq. (4.4).One of the challengesof numericalrnethodsis to determine
crror estimatesin the absenceof knowledgeregardiugthe true value.For example,certain
nunrericalmethodsuseiteratiotrto computcanswers.In suchcases,a presentapproximaThis processis performedrepeattion is rnadeon the basisof a previousapproximation.
compute(hopefully)betterand betterapproximations.
edly,or iteratively,to successively
For suchcases.the error is often estimatedas the dil'lerencebetweenthe previousandpreto
Thus,percentrelativeerror is determinedaccorcling
sentapproximations.
- previousapproximation
presentapproximation
l\OC/t
presentapproxrmatlon
( 45 )
This and other approachesfbr expressingerrors is elaboratedon in subsequentchapters.
The signsof Eqs.(4.2) through(4.5) may be either positiveor negative.lf the approx'
is greaterthanthecurim:rtionis greaterthanthe truevalue(or thepreviousapproximation
rent approximation),the error is negative:if the approximationis lessthan the true value,
may be lessthanzero,
the error is positive.Also, for Eqs.(4.3) to (4.-5),the denonrinator
pro
can
EXAMPLE
4.1
Err
Pro
ries
Thu
esti
^0.5
c
.1
:
'
with
term
fied t
JOIU
resu
I
Thui
tima
or fc
Thir
4.I ERRORS
which can also lead to a negativeerror. Often, when performing computations.we may not
be concemedwith the sign of the eror but areinterestedin whetherthe absolutevalueof the
percentrelativeerror is lower than a prespecifiedtoleruncee.. Theretbre,it is often useful
to employ the absolutevalue of Eq. (4.5). For suchcases,the computationis repeateduntil
(4.6)
I r , ,I < € . ,
This relationshipis refered Io as a stop1'ting
criterion.If it is satisfied.
our resultis assumed
to be within the prespecifiedacceptablelevel o.. Note that for the remainderof this text, we
almostalways employ absolutevalueswhen using relativeerrors.
It is alsoconvenientto relatetheseerrorsto the numberof significantfiguresin the approximation.It can be shown(Scarborough.
1966)thatif the followingcriterionis met,we
can be assuredthatthe resultis correctto at leastrr significantfigures.
(4.7)
e.,:(0.5 xl02-"1%o
EXAMPLE
4
E r r o rE s t i m o t e fso r l t e r o t i v eM e t h o d s
Problem Stotement. In mathematics,
functionscan otien be represented
by infinite series.For exanrple,the exponentialfunctioncan be computedusing
e':l+.r+-'
,\'l
2
.rl
F:+..
-l!
1
.r"
(E4.l.l)
nt
'Ihus,
as more terms are addedin sequence,the apploxirnationbecomesa betterand better
c'slimateof the true valueof e'. Equation(E4.L l) is calleda Moclourinserit,,s
a.rput,sion.
Startingwith the shnplestversion.e' : l, add termsone at a time in orderto estimate
e" 5.After eachnew term is added,computethe true and approximatepercentrelativeerrors
with Eqs.(4.3)and (4.-5),
respectively.
Notethatthe truevalueis e" 5 - |.648121. . . . Add
termsuntil the absolutevalueof the approximateen'orestimatec,, falls below a prespecified errorcriterions.rcontbrmingto threesignil'icant
figures.
S o l u t i o n . F i r s t , E q .( . 4 . 1 ) c a n b e e m p l o y e d t o d e t e r m i n e t h e e r r o r c r i t e r i o n t h a t e n s u r e s a
resultthat is correctto at leastthreesignificantfigures:
€. : (0.-5x 102 t)"/n:0.05a/,,
Thus,we will add termsto the seriesuntil e,,falls belowthis level.
The first cstimateis simplyequalto Eq. (E4.| . l) with a singleterm.Thus,the first est i r t i a t e i s e q utaclll . T h c s e c o n d e s t i l n a l . e i s t h e n g e n e l a t e d b y a d d i n g t h e s e c o n d t e r m a s i n
et:
l*x
or fbr,r : 0.5
e o 5- I * 0 . 5 :
1.5
This represents
a true percentrelativeenor of tEq. (a.3)l
.^_l
' _
|
r . 6 " 1 8 7-2 1t . . sI
I x 1001,=9.02(/t
t.64812t I
ERRORS
ROUNDOFF
AND TRUNCATION
84
Equation(4.5) can be usedto determinean approximateestimateof the error, as in
I
b
t15-ll
:.1.1.3(Z
r., : l-j_=_-l x 100(/o
I
l.)
|
S
n
by
Becauseen is not lessthanthe requiredvalueof e,, we would continuethe computation
adding anotherterm,x212r.,and repeatingthe error calculations.The processis continued
until le,,l < e,. The entire computationcan be summarizedas
Ierms
I
2
3
4
5
6
Result
5
625
645833333
648437544
6486979)7
393
942
I44
a t75
a at72
000142
3 33
769
I27
0 t58
00t58
andthe
errorfalls below e, : 0.05olo,
Thus, after six termsare includcd,the approxirnate
computationis terminated.Howcver,noticethat,ratherthan threesignificantfigures,the
tbr this case,both Eqs.(4.5) and(4.1) areconresultis accurateto five! This is because,
servative.That is, they ensurethat the resultis at leastas good as they specify.Although,
this is not alwaysthe casclbr Eq. (4.-5),it is true mostof thc time.
4.2
ROUNDOFFERRORS
Rountlo.ff'errur,s
arisebecausedigital computerscannotrcpresentsomequantitiesexactly.
and scicntificproblernsolvingbecausethey canleadto
They are irnportantto engineering
erroneousresults.In ccrtaincases,they can actuallylcad to a calculationgoing unstable
are saidto be ill-conditioned,
and yiefdingobviouslyerroneousresults.Suchcalculations
thataredifTicultto detect.
Worsestill, thcy can lcad to subtlerdiscrepancies
Thcreare two major facetsof nrundoll'crrorsinvolvedin numericalcalculations:
1.
2.
D,
nl
di
nl
tu
numben.
Digital computershavesizeandprecisionlimits on theirability to represent
are highly sensitiveto roundofTerrors.This canreCertainnumericalmanipulations
as well as fiorn the way in which computsult liorn both rnathematical
considerations
e'rsperl'ormarithmeticoperilti()ns.
4.2.1 Computer Number Representotion
Numerical roundofTerrorsare directly relatedto the mannerin which numbersarestoled
in a computer.The fundamentalunit wherebyinfbrmation is representedis calleda word,
This is an entity that consistscf a string of binary digil.l, or bits. Numbers are typically
storedin one or more words.To understandhow this is accomplished,we must first revierry
somematerialrelatedto numbersystems.
TI
aft
w(
ou
0c
ele
or
po
(0
Int
res
put
the
mai
sen
The
inF
repr
mair
FIGI
Thet
mog
rT
lll
IL
I
Sign
4.2 ROUNDOFF
ERRORS
85
Anumber systemis merely a conventionfor representingquantities.Becausewe have
l0 fingers and l0 toes,the numbersystemthat we are most familiar with is the decimal,or
base-10,number system.A baseis the number used as the ref-erence
fbr constructingthe
s y s t e m . T h e b a s e - l 0 s y s t e m u s els0tdhieg i t s - O , 1 , 2 , 3 , 4 , 5 , 6 , 1 , 8 , a n d 9 - t o r e p r e s e n t
numbers.By themselves,thesedigits are satisfactoryfor countingfrom 0 to 9.
For largerquantities,combinations
of thesebasicdigits are used,with the positionor
place vuluespecifyingthe magnitude.The rightmostdigit in a whole numberrepresents
a
numberfrom 0 to 9. The seconddigit fiom the right representsa multiple of 10.The third
digit from the right represents
a multipleof 100and so on. For example,if we have the
number8642.9,thenwe haveeightgroupsof 1000,six groupsof 100,four groupsof 10,
two groupsof l, and nine groupsof 0. l, or
( 8 x 1 0 3 ) + ( 6 x 1 0 2 ) + ( , 1x l 0 r ) + ( 2 x
100)+(9x l0 ry:9642.9
This type of representationis calledpositionol notatktn.
Now, becausethe decimalsystemis so familiar,it is not commonlyrealizedthal there
Forexample,if humanbeingshappenedto havceightfingersand toeswe
arealternatives.
would undoubtedlyhavedevelopedan ot'tul,t>rba.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<
4.2 ROUNDOFF
ERRORS
)r
nt
nis
1S,
de
ed
mrSS,
ve.
riled.
an-
)mtific
. the
:imal
ne so
rle, a
{owplore
at are
ntissa
a nice
87
w h e r e . r o a n d -. st h, e s i g n s ,d , : t h e r n a g n i t u d e o
t hf e e x p o n e n t . a d
n rda n d d , : t h e m a g nitudeof the significanddigits.
Now, let's play with this system.First, what is the largestpossiblepositive quantity
that can be represented?
Clearly,it would correspondto both signs being positive and all
nragnitudedigits setto the largestpossiblevaluein base-10,that is. 9:
Largestvalue: +9.9 x l0+Y
So the largestpossiblenumberwould be a little lessthan l0 billion. Althoughthis might
seemlike a big nunrber,it's reallynot that big. For example,this computerwould be incapableof representing
a commonlyusedconstantlike Avogadro'snumber16.022x l0rr.1.
In the samesensc,the smallestpossiblepositivenumberwould be
''
S m a l l e svt a l u e: * 1 . 0 x l 0
Again, althoughthis value rnighf seempretty snrall,you could nol use it trl representa
q u a n t i t yl i k e P l a n c k ' sc o n s t a n(t6 . 6 2 6x l ( ) - r r J . s ) .
Similarnegativevaluescouldalsobe developed.
The resultingrangesaredisplayedin
Fig.4.3. Largepositiveand negativenumbersthat fall outsidethe rangewould causean
overflowerrcr. ln a similar sense,for very small quantitiesthereis a "hole" at zero.and
very smallquantitieswould usuallybe convertedto zero.
Recognizethat the exponentoverwhelrningly
determinestheserangelimitations.For
exarlple,if we increasethe manfissaby onedigit. the maxirnumvalueincreases
slightlyto
9.99 x l0'. In contrast,
a one-digitincrease
in theexponentraisesthemaximurnby 90 orders
of magnitudeto 9.9 x l0e'r!
When it comesto precision,however,thesituationis reversed.
Whereasthe signil'icand
playsa minor role in detiningtherange,it hasa profoundetl'ecton specifyingthe precision.
This is dramaticallyillustratedfbr this exanrplewherewe have limited the significandto
only 2 digits.As in Fig. 4..1,just as thereis a "hole" at zero,thercarealso"holes"between
values.
Forexample.a simplerationalnumberwith a finitenurnberof cligitslike 2-5 : 0.0-l125
would haveto bc storedas -1.I x l0- r or 0.03I . Thus.'t rountlofferror is introduced.Fclrthis
case,it represcnts
a relativeerrorof
0 . 0 3 1 2 --5 0 . 0 3|
0 . 0 31 2 5
: 0.00t1
FIGURE
4.3
5-digit
for the
eaving
be
T h e n u m b e rl i n es h o w i n gt h e p o s s i b er o n g e sc o r r e s p o n d i ntgo t h e h y p o t h e t i c ob o s e l O
f l o o t i n gp o i n ls c h e m ed e s c r i b e di n E x o m p l e4 2 .
Minimum
l,,\l
9 . 9x 1 0 s
Overflowl--{
Smallest
- 1 . 0x 1 0 s
1 . 0x 1 0 s
,Underflow|-{Overflow
" H o l e "a t z e r o
Maximum
9 . 9x 1 0 s
88
ROUNDOFF
AND TRUNCATION
ERRORS
0.01
0.980.99 1
1. 1
1.2
FIGURE4.4
A smollporiionof thenumberlinecorresponding
to thehypotheticol
boselO flootingpoinl
s c h e m de e s c r i b eidn E x o m p l4e. 2 . I h e n u m b e ri sn d i c o fveo l u e tsh o tc o n b e r e p r e s e n t e d
All otherquonlilies
follingrnthe"holes"belvreen
ihesevolueswouldexhibitsome
exoctly.
t'rr
rounootterror.
While we could storea numberlike 0.03125 exactlyby expandingthe digits of the
significand,quantitieswith infinite digits must alwaysbe approximated.
For example,
a
c o m m o n l y u s e d c o n s t a n t s u c h a s ; r ( : 3 . 1 4 1 - 5 9 . . .h) a
wvoeutlocbl e r e p r c s e n t e d a xs 3l 0
. lr )
or 3. l. For this case,it represented
a relativeerrorof
- 93 .r
3.1415
3 .l 4 I . 5 9
I
(
F
T
p
"I
fl
b
: 0 . 0312
e/
F
Although adding significanddigits can improve the approximation,such quantitieswill
alwayshavesomeroundofTerrorwhen storedin a computer.
Anotherrnoresubtleef'fbctof'f'loating-point
representation
is illustratedby Fig.4.4.
Notice how the intervalbetweennumbersincreases
as we move befweenordersof magnitude.Iror nurnberswith an exponentof - I (that is, between0.1 and I ;, the spacing
is
0.01. Once we crossover into the rangefiom I to 10,the spacingincreasesto 0. l. This
meansthat the roundofl-errorof a nurnberwill be proportionalto its rnagnitude.In addrtion, it nreansthat the relativeerrorwill havean upperbound.For tlrisexample.themaxinrum relativeerror would be 0.05.This value is calledthe nuc'hineepsilon(or machine
precision).
As illustratedin Exanrple1.2,thefact thatboththeexponentand significandarefinite
meansthaf thereareboth rangeand precisionlimits on lloating-pointrepresentation.
Now,
let us examinehow floating-pointquilntitiesare actuallyrepresented
in a real computer
usingbase-2or binarynumbers.
First,let's look at normalization.
Sincebinarynumbersconsistexclusivelyof 0s and
ls, a bonusoccurswhen they are nornralized.
That is, the bit to the left clf the binarypoint
will alwaysbe onel This meansthat this leadingbit doesnot have to be stored.Hence,
nonzerobinaryfloating-pointnumberscan be expressed
as
X(l-tf)x2''
wheref': thennntisso(i.e.,the fractionalpartof the significand).
For example.if wenormalizedthebinarynumbell l 0 l. l, theresultwouldbe l. l0 l l x (2)'r or ( l + 0. l0 l l ) x 2-3.
p
S
R
tl
b
u
tl
tI
T
P
"l
4.2 ROUNDOFF
ERRORS
89
Signed
exponen
Mantissa
1 1b i t s
52 bits
Sign
(1 brt)
FIGURE
4.5
Themonner
inwhicho flooting-point
number
isstored
inon B-byte
wordin IEEE
double
prec s on formol.
Thus.althoughthe originalnumberhasfive signilicantbits,we only haveto storethe four
fractionalbits:0. l0l I .
By default, MATLAB has adoptedthe IEEE double-precision.fbnncrt
in which eight
bytes(64 bits) are usedto representfloating-pointnumbers.As in Fig. 4.5, one bit is reservecj
fitr the nurnber'ssign.In a similarspiritto the way in which integersare stored,the
expollL'nt
and its sign are storcdin ll bits. Finally.-52bits are set asidefor the mantissa.
Howevcr.becauscof normalization,
53 significandbits can be stored.
Now,just as in Exarnple.i.2,this nreansthatthenunrberswill havea lirnitedrangeand
precision.However,becausethe IEEE firrnratuseslnany more bits. the resultingnumber
systemcan be usedtbr practicalpurposes.
Ronge. In a tashionsimilarto the way in which integersare stored,the ll bits usedfor
the exponenttranslatcsinto a rangefrom - I 022 to | 023.The largestpositivenumbercan
be reprcscnted
in binaryas
l - a r g e sv ta l u e : + l . l l l l
... llll
x ?+10]'t
wherethe52 bits in the nrantissa
areall l. Sinccthesignificandis approximately
2 (it is ac- l.i9ii x 10108.
tually 2 - 2 t'), the largestvalueis therelbre2r0-rr
In a similarfashion,
the smallestpositivenumbercan be reprcsentcd
as
S r r a l l e svt a l u e: + 1 . 0 0 0 0 . . . 0 0 0 0 * 2 - t t \ 1 2
T h i s v a l u ec a nb e t r a n s l a t eidn t oa b a s e - 1 0
v a l u eo f 7 r { ) 1-1 2 . 2 2 5 1x l 0 r t ) 8 .
P r e c i s i o n . T h e 5 2 b i t su s e df b r t h c m a n t i s scao l r e s p o ntdo a b o u tl 5 t o l 6 b a s e - l 0d i g i t s .
Thus,z would be expressed
as
-'
formaL
Iong
"pl
.1ll S
-
I. I4I592ri53a891.)
Note thatthe machineepsilonis 2 sr : 2.2204x l0 16.
90
ROUNDOFF
AND TRUNCATION
ERRORS
4.
MATLAB has a numberof built-in functionsrelatedto its internalnumberrepresentation. For example,the rea Zmaxfunction displaysthe largestpositivereal number:
th
CI
nr
>>
format
>>
realmax
Anq
long
=
t.
9
-o9l-I
lo02J.oe-Jtru
Numbersoccurringin computationsthat exceedthis valuecreatean overflow.In MATLAB
they are set to infinity, inf . The te-aLmitTfunctionclisplaysthe smallestpositivereal
number:
rt
L
n
? . . 2 2 . 50 7 3 8 5 8 5 4 i - 2O i e - 3 0 8
>>
IL
n
>> realmirr
Nrlmbers that are smaller than this value create an underflow and, in MATLAB,
zero. Finally, the e.p-sfunction displays the machine epsilon:
\4
are set t0
eps
d
S
o
n
il
2 . ?.2 A ,!.4 6 0 4 9 2 5 0 3 I I e - C I tj
4.2.2 Arithmetic Monipulotions of Computer Numbers
Aside frornthe lirnitationsof a cclmputer's
numbersysteul,the actualarithmeticmanipulations involving thesenunrberscan also resultin roundofTerror.To understandhow this
occurs.let's look at how the computerperformssimpleadditionanclsubtraction.
Becauseof their familiarity,normalizedbase-10numberswill be ernployedto illustratethe effectof roundofTerrorson simpleadditionand subtraction.
Other numberbases
would behavein a sirrrilarfashion.?l simplify the cliscussion,
we will ernploya hypothetical decimalcomputerwith a 4-digit mantissaand a l-digit exponent.
When two floating-pointnumbersare addecl,the numbersare first expressedso that
they havethe sane exponents.
For example,if we want to add | .5-57+ 0.0434l, thecomputer would expressthe nurnbersas 0. 1557x l0r + 0.00+3-ll x l()r.'fhen the nrantissas
are addedto give 0. 16004I x t0r . Now. because
this hypothetical
computeronly carries
a
4 - d i g i t m a n t i s s a . t h e e x c e s s n u m b e r o f d i g i t s g e t c h o p p e d o f f a n d t h e r e s u l lt 0i sl .0 . l 6 0 0 x
Notice how the last two digits of the secondnumber(41) that were shifiedto the right have
essentiallybeenlost fiom the computation.
Subtractionis perfbrmedidenticallyto additionexceptthatthe sign of the subtrahend
is rcversed.Forexample.supposethat we are subtracting
26.86frorn 36.41.That is,
0 . - 1 6 4 x1 1 0 2
-0.2686 x 102
0.095-5x l0'
For this casethe resultmust be nornralizedbecausethe leadingzero is unnecessary.
S0
we mustshift thedecirnalone placeto theright to give0.9-550x l0' : 9.-550.Noticethat
I
I
(
4.2 ROUNDOFF
ERRORS
9l
the zero addedto the end of the mantissais not significantbut is merely appendedto fill the
empty spacecreatedby the shift. Even more dramaticresultswould be obtainedwhen the
numbersare very closeas in
0.1642 x 103
-0.7641 x 103
0.0001x lOj
which would be convertedto 0. 1000 x 100: 0. 1000.Thus, for this case,threenonsignificant zerosare appended.
The subtractingof two nearly equal numbersis called subtractivecancellation.lt ts
the classicexampleof how the mannerin which computershandlemathematicscan leadto
numericalproblems.Other calculationsthat can causeproblemsinclude:
Lorge Computotions. Certainmethodsrequireextremelylargenumbersof arithmetic
manipulationsto arrive at their final results.In addition,thesecomputationsareoften interdependent.That is, the later calculationsare dependenton the resultsof earlierones.Consequently,
eventhoughan individualroundofferrorcould be small,the cumulativeeffect
A very simplecaseinvolvessumoverthe courseof a largecomputationcanbe significant.
ming a roundbase-l0 numberthatis not roundin base-2.SupposethatthefollowingM-file
is constructed:
function
-
sout = sumdemo()
n.
for i - 1:10000
s = s + 0.0001;
end
sout = s;
When this tunction is executed.the result is
>> formaL long
>> sumdemo
ans =
0.9999999999999r
The f ormat long command lets us see the l5 significant-digit 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<lulcl
expressthis precliction
rnathcmatically
as
l ( , r i + r )] . 1 ( x r )
G.9)
This rclationship,
which is calledthe7.ero-onler
uppntximatiorz,
indicatesthat the valueof
I at the new point is the sameas the valucat the old point.This resultmakesintuitivesense
becauseif r, andr;11 erecloseto eachother,it is likely thatthe new valueis probablysimi l a r t o t h eo l d v a l n e .
is, in
Equation(4.9; proviclesa perf'ectestinrateif the functionbeing approxirnated
f'act,a constant.For our problcm,you would be right only if you happenedto be standing
on a pcrfectlyflat plateau.However,if thc functionchangesat all over the interval,additionaltermsof theTaylorseriesarerequiredto providea betterestimate.
So now you are allowcdto get ofTthe plattbrrnand standon the hill surfacewith one
leg positionecl
in tiont of you anclthe otherbehind.You immediatelysensethat the front
94
ERRORS
AND TRUNCATION
ROUNDOFF
^2
foot is lower than the back foot. In fact. you're allowedto obtain a quantitativeestimateof
the slope by measuringthe differencein elevationand dividing it by the distancebetween
your feet.
With this additional information. you're clearly in a better position to predict the
you use the slopeestimateto projecta straightline out t0
heightat .f (xi+).In essence,
.r;.,1.You can expressthis predictionmathematicallyby
No1
sig
acc
(4.10)
f (.,ri+r)= "f(.ri) *.f'(.u)h
This is calleda.firsr-order approximationbecausethe additionalllrst-orderterm consistsof
is
a slope/'(.r;) multipliedby /r. the distancebetween.;r;and x;11.Thus, the expression
now in the form of a straightline that is capableofpredicting an increaseor decreaseofthe
functionbetweenJ; and-t;11.
Although Eq. (,1.l0) can predict a change,it is only exactfor a straight-line, or linear,
trend.To get a betterprediction,we needto add more terms to our equation.So now you
First, you measurethe
are allowed to standon the hill surfaceand take two measurements.
slopebehindyou by keepingone foot plantedat x; and moving the otherone back a dist a n c e A , L . L e t ' s c a l l t h i s s l of lp, e
Qi).Thenyoumeasuretheslopeinfrontofyoubykeeping one fbot planted at x; and moving the other one forward Ax. Let's call this slope
.fi(.r). You immediatelyrecognizethat the slopebehindis milder than the one in front,
Clearly the drop in height is "accelerating"in l'rontof you. Thus, the odds are that .f (;rr)is
even lower than your previouslinear prediction.
term to your equation
As you rnightexpecf,you're now going to add a second-order
and make it into a parabola.The Taylor seriesprovidesthe correctway to do this as in
. l ' G i + ) = . t ' ' G i ) +/ ' ( r , , , *
J"lr'l
wht
and
app
ide
non
SOIC
con
den
yie
inn
eno
req
Thi
SOIT
(o.t')
,'
(n-
L :
To makeuseof this fbrmula,you needan estimateof the secondderivative.You canusethe
lasttwo slopesyou determinedto estitlateit as
. f" ( r i + ) 7
.fiQt) - .fi,Qi)
JQ
con
CITC
(4.r2)
Ax
Thus, the secondderivativeis merelya derivativeof a derivative;in this case,the rateof
changeof the slope.
Before proceeding,let's look carefullyat Eq. (4.I l). Recognizethat all the values
subscriptedr representvaluesthat you have estimated.That is, they are numbers.Conse'
quently,the only unknownsare the valuesat the predictionpositionx;a1.Consequently,it
is a quadraticequationof the tbrm
-r
. f ( h ) = a 2 h 2I a 1 h a 1 y
Taylorseriesapproximatesthefunctionwith a second'
Thus,we can seethatthesecond-order
orderpolynomial.
Clearly,we could keep addingmore derivativesto capturemore of the function'scut'
vature.Thus,we arive at the completeTaylorseriesexpansion
l - j J . n ' * " f ' ' - 1 t ' l r , ' + " * f " ) t t i t r ' + R , ,{ 4 . 1 3 1
f ( r , . r ): f ( r ; + / ' ( . rn; +
-----c--
wer
ber
wht
The
this
plv
bas
wil
ter
oft
lou
onl
trat
ERRORS
4.3 TRUNCATION
Note that becauseEq. (4.13)is an infinite series.an equalsign replaccsthe approximate
sign that was used in Eqs. (4.9) through (4.I l). A remainclerterrn is also includedto
accountfor all terms fror.nn * I to infinitv:
/ t / / + ll l i \
R , ,- J -
l'-7,,,+l
g.l1)
wherethe subscriptn connotesthat this is the rernainderfbr the nth-olderapproximation
and,.eis a valueof -t'thatlies somewhcrebetween.t';lifldr;+ r .
Thus,we can now seewhy the Taylortheoremstatesthat any smoothfunctioncan be
approximated
as a polynornialand that thc Taylor seriesprovidesa meansto expressthis
ideamathematically.
Tiiylor seriesexpansionwill be exactfirr arrrth-ordcr polyIn general,the nth-orcler
and sinuand continuousfunctions,suchas exponentials
norlial. For otherclifl'ererrtiablc
soids,a finite nunrbero1'termswill not yicld an exactestimate.Eachadditionalterm will
r ill be
s o m ci m p r o v e m e nht ,o w e v c rs l i g h t ,t o t h ea p p r o x i r n a t i oTnh. i s b e h a v i ow
contribute
in Example.1.3.Only if an infinitenurrbcrof terrnsalc addcdwill thc serics
demonstrateci
y i e l da n e x a c tr c s u l t .
is that,
Althoughthe tbregoingis true,the practicalvalueof Taylorscriesexpansions
i n r n o s tc a s e st,h ei n c l u s i o no l ' o n l ya f ' e r vt e r n . rws i l l r e s u l ti n a n a p p r o x i r n a t i ot hna ti s c l o s e
ol how many tL'nxsare
cnollghto the true valuc lilr placl.icalpurposcs.The assessmcnt
requiredto get "closeenclugh"is bascdon the remaindertcrrnof the expansion(Eq. 4.l4).
This relationshiphastwo major drawbacks.First.{ is not known cxactlybut melely lics
s o m e w h e rbc e t w e e nr ; a n d . \ ; 1t . S c c o n dt.o c v a l u a t eE q . ( 4 .l - l ) . w c n c c dt o d e t e r m i n tch c
( r r+ l ) t h d e r i v a t i v eo l ' / ( r ) . T o d o t h i s ,w e n e e dt o k n o w . / ( , r ) . H o w c v e r ,i f r v e k n c w
/(,r), llrere would be no neeclto pcrlirrrnthc Tirylor scrics expansionin the present
contexf!
. q . ( 4 .1 . 1 )i s s t i l l u s e f u lt i r r g a i n i n gi n s i g h ti r r t ot r u n c a t i o n
D e s p i t et h i s d i l c n r n r aE
errols.This is becausewe rlo havecontrnlovcr lhc term ir in thc cquation.In otherwords.
we canchoosehow lar awa-vfiorn.r'wc wiurlto evaluatel (r ), andwe cilltcontrolIhenttntb e r < r f ' r e r nw
r sc i n c l u d er n t h ee x p a n s r o rCr .o n s e q u e n i yE, q . ( . 1 , 1 ) i s u s u a l l ye x p t e s . s e d a . s
R , ,: O ( l t " ' ' )
e ( 1 , ' * ' ) m e a r t st h a tt h e t r u n c a t i o cnr r o r i s o f t h c o r d e ro l / t " * i .
w h e r ct h e n o m e n c l a t u rO
That is, the ellol is proportionalto thc stepsize/r raiscdto the (rr * l)th power'.Although
this approximationimplics nothingregardingthe magnitudco1'thederivativcsthat multiply firr+t, it is extremclyLrsefulin.judgingthe comparativeerror of nurncricalrnethods
Fur cxample,if the crror is O(fi), halvingthe stepsizc
basedon Taylor scriesexparrsions.
w i l l h a l v ct h ee r m r rO. n t h c o t h e rh a n d .i f t h ec r l r r ri s 0 ( f i r ) .h a l v i n gt h r -s t e ps i z ew i l l q u a r ter the error.
by the addition
In general,we can usuallyassumethatthe truncationerroris decreased
sn'rall.the first- anclother
of tcrnrsto the Taylor series.In many cases,il'fi is sLrfficiently
high percentof the error'.Thus,
ternrsusuallyaccountlbr a dispropoltionately
lower'-order
This propertyis illusapproxinration.
only a t-ewtermsare requiredto obtainan adequate
tratedb_\,
the following exanrple.
4.
ERRORS
AND TRUNCATION
ROUNDOFF
96
E X A M P L E4 . 3
A p p r o x i m o t i o no f o F u n c t i o nw i t h o T o y l o rS e r i e sE x p o n s i o n
: 0 to 6 to approxirnate
Problem Stotement. Use Taylor seriesexpansionswith n
,f(-r):cosxat'rr+t:rl3onthebasisofthevalueof/(x)anditsderivativesat
*
x i : t l 4 ' N o t et h a t t h i sm e a n st h a t h : r 1 3 n l 4 : T l 1 2 '
4
B
si
the correct
Solution. Our knowledgeof the true tunction meansthat we can determine
va|uef(r13):0.5.Thezero-orderapproxinrationis[Eq.(4.9)l
t ( i ) = ' " - ( i ) : o' o ' r 0 6 7 8 l
er
se
which representsa percentrelativeerror tlf
.',: lu_L9#Sl'or* :rt o*
It
sll
term where.f'(x) : -sinxl
we adtJthe first clerivative
approximatit'rn,
For the fir-st-order
AI
+ l) : 0 . s 2 r e 8 6 6 s s
'r\/r1/ -\ -= . . , / " ) - , i n f\ 44/)\(1- 2
\-rl
(jfl
we add the secondderivaapproximatitln,
which has le,l:4.400/o. For the seconcl-ttr<ler
:
"('r
r:
cos
)
tive terln where/
/z\
/r\
/r\/r\
t(i):c.'s(;)--'"|';)\i)-
cos(]r/4(l\':0.4()1154491
2
\r2l
---"'
ir.ran improvedestiwith le,l:0.44g%,. Thus, the inclusionof additionalterms results
nrate.Thepfocesscanbectrntinueclandtheresultslistedasin
Order n
0
I
2
3
4
5
6
fr"(x)
cos,r
-sin -r
cos.t
s i n. r
COS -Y
-sirt -r
-cos -r
ca
ap
l'0r/t1
o 7 a 7l 0 6 / 8 l
0 5 2 19 B 6 e ' - 5 9
o 497754491
c )4 9 9 8 6 9 1 4 7
0 50000/551
0 500000304
0 499999988
le,l
414
440
a.449
)
262x lA
l 5l x lO r
608 x lO 5
244xlO"
for a polynornial'
Notice that the derivatrvesnevergo to zero as would be the case
estimate'Howevet'
Therefbre,eachadditionalterm resultsin some improvementin the
For
thiscase'bythe
alsonoticehow mostof the improvementcomeswith the initial terms'
meansthat
which
tinre we have acldedthe third-orderterm, the error is reducedto 0.0267a'
of the true value.Consequently,althoughthe additionof more
we have attained99.974o/a
becomesnegligible'
terms will reducethe enor turther,the irr.rprovement
*
Gr
ERRORS
4.3 TRUNCATION
97
4.3.2 The Remoinderfor the Toylor SeriesExponsion
Before demonstratinghow the Taylor seriesis actuallyusedto estimatenumericalerrors,
we must explainwhy we includedthe ar-{ument
f in Eq. (4.14).To do this, we will use a
simple,visuallybasedexplanation.
Supposethat we truncatedthe Taylor seriesexpansion[Eq. (4. I 3)] after the zero-order
term to yield
. / ( . r , + r )] . / ( - t i )
A visual depictionof this zero-orderpredictionis shown in Fig. 4.7. The remainder,or
error. of this prediction,which is also shown in the iliustration,consistsof the iniinite
seriesof terms that were truncated
.
f"tv
t
R r:r, / ' ( . \ it h+ + h 2 + '
f{l'tt
t
.l^''h3+...
It is obviouslyin.onu"Ji.nt to deallith the remainderin this inflnite seriesformat.One
simplificationmight be to truncatethe remainderitself,as in
R 1 1= . [ ' ( x 1 ) l t
(4.1s)
Although,as statedin the previoussection,lower-orderderivativesusuallyaccountfor a
greatershaleof the remainderthan the lrigher-order
terms,this resultis still inexactbeis impliedby the
causeof the neglectedsecond-andhigher-olderterms.This "inexactness"
a p p r o x i m a teeq u a l i t ys y m b o l( : ) e m p l o y e di n E q . ( 4 . l 5 ) .
into an equivalence
is
An alternativesimpliticationthattransfbrmsthe approximation
basedon a grnphicalinsight.As in Fig. 4.8, the derivativemenn-valuethertremstatesthat
FIGURE
4.7
GrophicoLdepiclicn cf o , r o r . , . t d - T o . L l s e i p s 6 r c f l . r ' r o ' ) . : r , dr e n o r . d e t
l (xl
Ll.
)t,
1e
at
re
98
R O U N D O FA
F N D T R U N C A T I OE
NR R O R S
n
R
o1
,f{x}
Nr
Eq
h
Tht
the
we
tlor
FIGURE
4.8
G r o p h i c o ld e p i c t i o no [ t h e d e r ] v o t i v m
e e o n - v o l uleh e o r e m
if a functionl(r) and its first derivativearecontinuousover an intervalfrorn.:r,
to.ri+1,
then
thereexistsat leaslone point on the functionthat hasa slope,designated
by /'(€). thatis
parallelto the line joining,l{.r,)and,l(,r,*,).The parameter
{ marksthe r valuewherethis
slopeoccurs(Fig. 4.8).A physicalillustrationof this theoremis that,il'you travelbetween
two pointswith an averagevelocity,therewill be at leastone momentduringthecourseof
the trip when you will be moving at that averagevelocity.
By invokingthis theorern,it is simpletu realizethat,as illustratedin Fig.4.8.theslope
.l'(6) is equalto the riseR,,dividedby the run h, or
R.,
,
f'(t): +
whichcanb. ilurr.ng.oto -{ive
Ro-- .t'''(.t)h
(416)
versions
f'hus,we havederivedthe zero-order
versionof Eq. (4.14).The higher-order
aremerelya logicalexfension
ol'thereasoning
usedto deriveEq. (4.16).Thefirst-order
version
is
I"(F)
R1 : :--------7,
Thu
tion
shou
expe
4.3
Equ
ence
r4l7r
Li
For this case,the value of { confbrmsto the r- valuecorrespondingto the secondderivative
thatmakesEq. (4.l7) exact.Similarhigher-order
versionscanbe developed
from Eq.(a.la)
4.3.3 Using lhe Toylor Seriesto EstimqteTruncotionErrors
AlthoughtheTaylorserieswill be extremelyusefulin estimatingtruncationenorsthrough'
out this book, it may not be clear to you how the expansioncan actuallybe applied
to
wher,
matic
and i
99
ERRORS
4.3 TRUNCATION
numericalmethods.In fact, we have alreadydone so in our exampleof the bungeejumper.
Recall that the objectiveof both Examplesl. I and I .2 was to predictvelocity as a function
of time. That is, we were interestedin deterrniningu(/). As specifiedby Eq.(4. 13), u(l)
can be expandedin a Taylor series:
, 1 1 ' *- , I , ) * { f
r , ( / i + r:) u ( t ) * u ' { / 1
LI
, r , * , - t i ) z+ " ' - F
R,,
Now let us truncatethe seriesafter the first derivativeterm:
u ( t i + r ): u ( t i ) f u ' ( / r ) ( t r * -r / i ) * R r
( 4 .l 8 )
Equation(4.l8) can be solvedfbr
t . .|
rl I I
R1
u(ri+r)-u(ti)
-
{i+l -
-
ri+l -
{i
ri
( 4 r. 9 )
Truncatiorl
e[ot
First-ordcr
appfoxrnra(ron
The first part of Eq.(4.19,)is exactlythe samerelationshipthat was usedto approximate
thederivafiveinExamp1
l e. 2 [ E q . ( l . l l ) l . H o w e v e r , b e c a u s e o f t h e T a y l o r s e r i e s a p p r o a c h ,
with this approximawe havenow obtainedan estimateof the trtrncationerror associated
tion of the derivative.Using Eqs.(4.l4) and (4.l9) yields
rRr
ti+t -
li
:
u"(6).
-
-(ti+t
2!
ti)
or
R'
ti+t- Ii
:O(tirr-l,)
T h u s ,t h e e s t i m a t e
o f t h e d e r i v a t i v e[ E q .( l . l l ) o r t h e l l r s t p a l t o f E q . ( 4 .l 9 ) ] h a sa t r u n c a tion error of order ti+r - ti. In other words, the error of our derivativeapproximation
shouldbe proportionalto the stepsize.Consequently,
if we halvethe stepsize,we would
expectto halvethe errorof the derivative.
4.3.4
Numericql
Differentiotion
Equation(4.f 9) is given a formal label in numericalmethods-it is calleda.finitedifJ'erence.lt can be representedgenerallyas
f
'(.ri\ :
f
'(ri) :
.f\ri+t)
-
/(.fi)
Ji+t -r,
. l ( . r i + r )- . f ( r i )
f
O(-ri+t - ri)
+ o( h)
(1.20)
(4.21)
where /r is called the step size-that is, the length of the interval over which the approximation is rnade,.ri+t- x;. It is termeda "forward" differencebecauseit utilizesdataat i
and i * I to estimatethe derivative(Fig.4.9a).
too
ERRORS
ROUNDOFF
AND TRUNCATION
S
e
d
tt
e5
b1
al
br
Bc
CX
Trr
wht
Cer
mat
to yi
whic
tc)
FIGURE
4.9
finite-difference
depiclion
Grophicol
of (o)forword,
ond{c)centered
{b)bockword,
c n n r o < i m n t i o n so f r h e f i r q rd e r i r o t i v e
Noti
appr
the tr
the r
war(
tral r
4.3 TRUNCATION
ERRORS
rol
'I'his
forward differenceis but one of many that can be developedfrom the Taylor
seriesto approximatederivativesnumerically.For example,backwardand centereddifference approximationsof the llrst derivative can be developedin a fashion similar to the
derivationof Eq. (4.19). The former utilizesvaluesat r;-1 and.r; (Fig. 4.9b), whereas
the latter uses valuesthat are equally spacedaround the point at which the derivative is
estimated(Fig. 4.9c).More accurateapproximationsof the first derivativecan be developed
by including higher-ordertermsof the Taylor series.Finally, all the foregoingversionscan
also be developedfor second.third, and higherderivatives.The following sectionsprovide
brief summariesillustratinghow someof thesecasesare derived.
Bockword DifferenceApproximotion of the FirstDerivotive. The Taylorseriescan be
expandedbackwardto calculatea previousvalueon the basisof a presentvalr.re.
as in
f
h
f t x i - r t : J ' ( x t t- l ' ( x i f t +
::'t
(4.22)
Truncatingthis equationafter tlre first derivativeand rearrangingyields
1 ' G i )=
J (ri) - .f(xi-r)
(4.23)
wlrerethe error is O(/z).SeeFig.4.9b for a graphicalrepresentation.
Centered DifferenceApproximotion of the First Derivotive. A third way to approximatethe first derivativeis to subtractEq.(4.22)from the fbrwardTaylorseriesexpansion:
J ' ( x i - t- ) . l ' ( r i*) l ' ( . r*i + f f n ' + ' . '
(1.21)
to yield
J'^l(*'\
rt *
. l ( x i + r ): . f ( x i ) - t 2 l ' ( x , r , *
whichcanbesolvcdfor
. l ( - r i + r )- . f G i t )
.l''(x) :
2h
t't".\ -
*
" f ( . r i + r ) l ( . r i _ r )2h
.f'r)(",),,
b
o(t)
(4.25)
Equation (4.25) is a centercdfinire tlifJbrencerepresentationof the first derivative.
Notice that the truncationerror is of the order of ft2in contrastto the forward and backward
approximationsthat were of the order of /2.Consequently,the Taylor seriesanalysisyields
the practical information that the centereddifferenceis a more accuraterepresentationof
fhe derivative(Fig. 4.9c). For exarnple,if we halve the stepsize using a forward or backward difference,we would approximatelyhalve the truncationerror, whereasfor the central difference,the error would be quartered.
r02
E X A M P L E4 . 4
ERRORS
AND TRUNCATION
ROUNDOFF
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 n so f D e r i v o t i v e s
problem Stotement. Use forward anclbackwarclclifferenceapproximationsof o1/r1and
derivativeof
a centereddifferenceapproximation of O(h2)to estimatethe first
- 0 . l . r a - 0 . l 5 . t r - 0 . 5 . r r- 0 ' 2 5 ' t- t 1 ' 2
J (,r):
usingh:0-25. Note thatthe
ar -r : 0.5 usinga stepsizeft : 0.5. Repeatthe comprrtation
clerivativecan be calculateddirectly its
- 0 . 4 ' t 3 - 0 . 4 5 x 2- 1 ' 0 x- 0 ' 2 5
f'(r):
andcanbeusedtocomputethetruevalueas/,(0.5):-o.9|25.
So|ution. For h:0.5,
ri t:0
.t; : 0.5
r r i + r:
1.0
the functioncanbe employedto determine
f (xi-t): l'2
.f @) :0.925
l'(-ri+t):0'2
(a'21)J'
Thesevaluescan be usedto computethe forwarddifference[Eq'
o'2 - 0'925 -l
:
'15
l e , l: 5 8 ' 9 %
l ' ' ( 0 . - 5 )=
*
the backwarddifferencelEq. (a.23)1.
/'(05)
- 0'925- l'2:-0.55
0.5
le,l:3g"7o/o
IEq. (a'2-5)l'
and the centeredclifl'erence
0.2 1.1
.l''(0.5)=
ff
--1.0
le,l:9.6a/<,
Forft : 0.25,
.l(ri-r): l'10351563
J'Qi): 0'gZS
. l ( r i * r ): 0 ' 6 3 6 3 2 l83
whichcanbe useclto computethefbrwarddif'ference'
3 - 0.925
0.6363281
-r;I :0.25
,r; : 0.5
r ; 1 1: 0 . 7 5
l''(0.5)=
:-l.l-55
0.25
T
EI
lt,l:26.5"/o
the backwarddiff'erence'
0 . 9 2 5- 1 . 1 0 3 5 1 5 :6 3-0.1 11
=
l''(o.s)
0.25
le,l : 21.'|Eo
diff-erence'
andthecentered
5 5 6 3 -0.934
0 . 6 3 6 3 2 8 -1 31 .1 0 3 1
:
f /' ( o
O S{ \\ : =
0.5
V , \: 2 ' 4 % '
4.4 I
T
rh
o
ca
E
si
c(
r 03
4,4 IOIAL NUMERICAL
ERROR
For both step sizes,the centereddifferenceapproximationis more accuratethan forward or backwarddifl'erences.
Also, as predictedby the Taylor seriesanalysis,halving the
stepsize approximatelyhalvesthe error of the backwardand forward diff-erences
and quarters the error of the centereddifference.
Finite-Difference
Approximotionsof Higher Derivotives. Besidesfirst derivatives,
the
Taylorseriesexpansioncan be usedto derivenumericalestirnates
of higherderivatives.
To
d o t h i s , w e w r i t e a f b r w a r d T a y l o r s e r i e s e x p a n s i o n f o r . f ( . . ri ni +t e) r m s o f . / ( - r i ) :
. / ' ( r , + r ): . l ' ( r i ) I
J ' l x i ) t 2 h l+
'")r'''
,r,rrt*
"
(4.26)
Equation(4.24)canbe multipliedby 2 and subtracted
from Eq. (4.26)to give
. l ' Q i + ) - 2 . 1 ' ( r ; * 1 ):
. [ ( r i ) + . 7 ' " 1 t 1 1+h.2. .
which can be solvedfbr
./"(r,):
I ( r i + : ) - 2 . f ( . r i + t )* . / ' ( . r i )
h2
+ o(h)
(1.27)
This relationshipis called thc second.font,unl.finite diflerenca.S i r n i l a rm a n i p u l a t i o ncsa n
bc cmployedto derivea backwirrdversion
,.,
. / { . t , ) - 2 . / ' ( - t r, ) * . 1 ( r i 3 )
.1"(r,):ff+o(h)
-
and a centeredversion
. l ( . t , r ) - 2 / ( . t il I l ( . r i r )
.,/. ", ,\ . r i ) : f f + o f t 2 )
-
As wasthccascwith thefirst-derivativc
approximations,
thecentered
caseis moreaccurate.
Noticealsothatthecentercdvcrsioncanbe altemativelvcxnrsssed
as
l ( , r i + r ) - . 1 ' ( . r i )_ I G i ) - . / ' ( x i t )
hh
f ' "( r i ) 1
Thus,just as the secondderivativcis a derivativeofa derivative,the secondfinite dilfbrenceapproximationis a differcnceof two first finite differcncesIrecallEq. (a.l2)1.
4,4 TOTATNUMERICAT
ERROR
The total nnnerit:aLerror is the summationof the truncationand ror-rndofTerrors.
In general,
the only way to minimizeroundofTerrorsis to increasethe numberof significantfigures
of the computer.Further,we havenotedthat roundoff error may int:reasedue to subtractive
cancellation
or dueto an increasein the numberof computations
in an analysis.In contrast,
Example4.4 demonstratedthat the truncationerror can be reducedby decreasingthe step
size.Becausea decreasein stepsizecan lead to subtractivecancellationor t.oarl increasein
computations.the truncartion
errors aredecreasedas the roundofTerrorsareincreasetl.
r04
ERRORS
ROUNDOFF
AND TRUNCATION
Point of
diminishing
returns
o
J
Log step size
F I G U R 4E. I O
errorthotsomelimes
A croohlcodeoiction
of thelrodeoffbetweenroundoff
ond iruncotion
relurns
is
coiresintoploy in thecourseol o numericomethodThepointof diminishing
of step-size
reduclion.
shown,whereroundoff
errorbeginsto negolelhe benefits
t
i
t
Therefore,we are facedby the following dilemma:Thc strategyfbr decreasing
one
of the othercomponent.ln a cornputation,
componentof the totalerrorleadsto an increase
we couldconceivablyclecrease
thc stcpsizeto minimizetruncationerrorsonly to discover
that in doing so, the roundofferror beginsto dominateths solutionand the total error
grows!Thus,our remedybecomesour problem(Fig. 4. l0). One challengethat we laceis
stepsize1bra particularcomputation.
Wc'would like to choose
to determinean appropriate
the amountof calcr,rlations
and roundoffcrrorswithout incur
a largestepsizeto decrease
ring the penaltyof a largetruncationerror.If the total error is as shown in Fig. 4. 10,the
challengeis to identily the point of dinrirrishingreturnswhere roundofTerror beginsto
negatethe benefltsof step-sizereduction.
WhenusingMATLAB, suchsituations
of its l5- to I6arerelativelyuncommonbccause
they sometimes
digit precision.Nevcrtheless,
do occurandsuggcsta sortof "numericaluncertaintyprinciple"thatplacesan absolutelimit on theaccuracythat rnaybe obtainedusing
certaincomputerized
numericalmethods.We erplore sucha caseirr thc lollowing section.
EXAMPLE
4.5
R
P
c
P
b
R
4.4.1 Error Anolysis of Numericol Differentiotion
As describedin Sec.4.3.4,a centereddifl'erence
approximation
of the first derivativecan
be writtenas (Eq. 4.25)
l
ire
,f'(.r,):
True
value
J(ri+tl
-.1 (.ti r)
2h
Finite-difference
approximation
./'(t)(6), .'
---116
f
fr
(4.28)
d
Truncation
error
h,
H
Thus. if the two functionvaluesin the numeratorof the finite-difference
approximation
have no roundoff error,the only error is due to truncation.
D
E
-I
r05
ERROR
4.4 IOIAL NUMERICAL
However, becausewe are using digital computers,the function values do include
roundoff error as in
-t ei-t
.f(xi t) : f @i- )
, f ( - r i + r ): . f ( x i + ) I e i + r
where the ;'r a.e the rounded function values and the e's are the associatedroundoff
errors.Substitutingthesevaluesinto Eq. (4.28)gives
/(.ri t)
Pi t
-
, t',rl
?t.
.l lxit:
f(*,,r1A,
2h
True
value
Finite-difference
approximation
RoundofT
error
-
,/'l'(€),-:
"
6
Truncation
error
We can seethat the total error of the finite-differenceapproximationconsistsof a roundoff
with stepsize.
error that increaseswith stepsize and a truncationerror that decreases
error has an
roundoff
of
the
Assuming that the absolutevalue of each component
2e. Further,
€iwill
be
€i+rupperboundof e, themaximumpossiblevalueof thedifl'erence
boundon
M.
An
upper
value
of
assumethtrtthe third derivativehasa maximumabsolute
as
the absolutevalue of the total error can therefbrebe represcnted
t-
T o t uel r r o r :
e h2M
| .,
_ . [ 1 r , r, ) . / ( x r r ) < _ + _
h6
l./'t_r.r,
n
I
G.29\
An optimal step size can be determinedby diffbrentiatingEq. (4.29),settingthe result
equalto zeroand solvingfbr
('1.30)
E X A M P4L.E5
R o u n d o f fo n d T r u n c o t i o nE r r o r si n N u m e r i c o lD i f f e r e n t i o t i o n
Problem Stotement. In Example4.4, we useda centereddiffbrenceapproximationof
O(h]) to estimatethe first derivativeof the fbllowing functionat,r : 0.-5,
-l
t ' G ) : - 0 . l , r a - 0 . l 5 , r r - 0 . 5 1 2- o . 2 5 x 1 . 2
Peribrm the samecomputationstartingwith ft : l. Then progressivelydivide the stepsize
how roundoffbecomesdominantasthe stepsizeis reduced.
by a f'actorof I 0 to demonstrate
(4.30).
Recallthatthe truevalueof the derivativeis -0.9 125.
Eq.
your
results
to
Relate
and plot the
Solution. We can developthe following M-tlle to perfbrmthe computations
as
arguments:
derivative
its
analytical
function
and
pass
the
we
both
that
results.Notice
funcLion
di ffex
( func,
cLfunc, x, n)
1ong
format
df t r.ue=df unc (x ) ;
h-1;
H ( I ) =h;
D(1) = ( func (x+h) -func
E (1 ) =abs (dftrue-D(1)
(x-h)
) ;
) / (2*h)
;
r0 6
AND TRUNCATION
ERRORS
ROUNDOFF
i - )
i rr
. n
h = h , 1 r j;
H(r)=5;
D(r1= (func(x+h)
(.)
o.l(il
func(x
|
uF
h) ) / (2*h\ ;
));
end
L)' E' I ' ;
fprincl('
finite
diflerenr-e
srt-rp siz.:
t1.u.
('%14.10f
lprintf
%]5.14f
\16.lif\n',L)
;
l ogLog (H, E) , xl abel ( 'SLcp Si zc' ) ,y Label ( 'Error'
)
1 , =[ l |
!,r1-1e('Fla'!
IormaL
rt
l , . r . r . o r \ / c r . i r , l . . rS L e p
errol'\n');
Stz.')
sholf
The M-flle can thenbc run usingthe tbllowingcommallcls:
.>
f f=G(x)
L l. 1 * x ^ . 1
->
c 1 f = ( ; (r x )
0.4*x^
i
0.15*r^3-0.!*x^2
0.45*_r^2 x (1.2b;
0.25*x+1
.2;
> : ' c l i f I r - - - . x (f f , r - 1
f ,0.'1 ,ll)
:tr--p :;izc
f inite
c ii 1 f e t , - n c c
t.r'uLr error
I . 0 0 0 ( 10 0 0 f { l ( l
I . 2 . t t 2!.r 0 0 ( .rr)( 10 0 0 t r r - 1 0 . I r i - )0 0 1 :0) ( r i l0 t l 0 0
0 . I t l f l r r ( l 0 L l 0 r ) ( l 1 . ) !. 1 r . , 0 0 ( ) 0 ( ) ( l ( ) l l 0 r ) | 0 . 0 ( j t ' r ( ) t l L l t ) u t . l l l t ) ( )
0 . r _1l 0 0 0 0 0 0 0 0
0. !ll2 5J i00000011(r
ll.0il(l0l 5000000t)
) Ol!t)000t)
0. U(r0t0
0.0(l 10000{)(r0
A.')12 500llr00000I
i l . ( r ( ) 0I ( l o L r ( ) i ) r l l ) . ! ) l : 5 ( ) L r l r { ll . j 9 ! r t r ' ,
0.0000
10 0 r ) r J 0
0 . 0 0 0 0 0 10 0 | 0
( l . [ )( ) { ) 0 0 U1 ( )( ] i )
0 . 0 ( ) ( l l r 0 0 0 L( l o
0.0f)000000
t 0
0 . O ( r ( ) ( ) 0 0 0 ( l (I )
0 . { i ( | ) 1 ) { ) ( ) ( )3t )1 i , r 9 3
0. t l 21000rJ00 l l I !
0.00utJ000000112
0.91 2!t)000000b4..
0 . r l L - l4 9 t j c ) ' )q 4 5 ( r l I
0 . q 1 2 5 O ( t r ) ( Il i 1 6 0 ' r
0.0r100000000054
0 . 0 r ) [ )( ) 0 t )0 U 0 1 , 4 9 /
0 . 1 1 0 1 1 6 6 3 0 ( ) 1l 3 6 1
a .912,500u t 9 98q44
U.9 I 2!00(l i 'rb(10!tl
0.0000000199894
0.0.J0000075ir006
Q.,
As clepicted
in Fig.4.I I. thc rcsLrlts
arcasexpcctcd.
At l'irst,foundol'fis minimalandthe
Hcnce,as in Eq. (4.29).the totalerrordropsbya
r'stimatcis dclrninnted
by truncationL-rror.
t a c t o r o l ' I 0 0 e a c h t i n i e w c d i v i d csttheep b yI 0 . H o w ' c v e r , s t a r t i n g a t / z : 0 . 0 0 0 l , w e s e e
roundol'f'error
bcginto crcepin andcrodethc rateat whichthecrrordirninishes.
A rninimum
t'.
crror is reachcdat /r: l0 Bcyondthis point.the crrol irrcrcascs
as roundofTclominates.
Becuu.sc
w'eafc dcalingw'ithan easilyclittbrcntitblel'unction,wc czuralso investigate
whctherthescresultsarcconsistent
with Eq. t4.30).First,we canestimatcM by evaluating
t h e l ' u n e l i o n 'tsh i r t ld c r i v l r l i v cu s
-0.():2.
M : I l ' t ' t ( 1' -..-. ,5I ) l - l - 2 . 2 1 1 0 .1. 5
1
B e c a u sM
c A T L A B h a sa p r c c i s i o n
0 1 ' a b o ult5 t o l 6 b a s c - l 0 d i g i t s a
, rrouge
h s t i m a t e ot hf e
upperboundorl rounclofl'wouldbc abollt€r:0.-5 x l0 r('.Sr-rbstituting
thescvaluesinto
E q . ( 4 . 3 0 )g i v e s
l0r(')
:4.3 x l0 ('
w h i c h i s o n t h es a m eo r d e ra s t h er e s u l o
t l I x l0
('ohtuined
withMATLAB.
For
Thr
apt
mu
pro
eng
gra.
equ
you
pos
and
first
usir
erro
lem,
r07
4.4 TOTATNUMERICAL
ERROR
FIGURE
4.I I
Plot of error versus step saze
100
1o-2
10 - 4
10 - 6
t!
10 - 8
1 0 -1 0
10 8
10-6
10 - 4
Step size
4.4.2 Control of Numericol Errors
rd the
;bya
/e see
imum
rtes.
itigate
uating
: of the
es into
Forrrrostpracticalcases,we do not know tlreexacterrorassociated
with numericalrnethods.
The exception,of course,is whenwe know theexactsolution,which makesour numerical
Theretbre,
approxitnations
unnecessary.
for rnostengineering
andscientificapplications
we
mustsettlefbr someestimateo1'theerrorin our calculations.
and generalapproaches
Thereare no systematic
to evaluatingnumericalerrorslor all
problems.In many ciiseserror estimatesale basedon the expericnceandjuclgnrento1'the
engiueeror scientist.
Although error analysisis to a certain extent an art, there are severalpractical programmingguidelineswe can suggest.First and fbremost,avoid subtractingtwo nearly
equalnurnbers.Loss of significancealmostalwaysoccurswhen this is done.Sometimes
you canrearrange
or refbrmulatetheproblenrto avoidsubtractive
cancellation.
If this is not
possible,you may wirnt to use extended-precision
arithnretic.Furthermore.when adding
numbers.it is bestto sortthe numbersand work with the srnallestnumbers
andsubtracting
first.Thrsavoidslossof significance.
Beyond thesecomputationalhints, one can attemptto predict total numericalerrors
using theoreticalformulations.The Taylor seriesis our primary tool fbr analysisof such
errors.Predictionof total numericalerroris very complicatedforeven rnoderatelysizedproblemsandtendsto be pessirristic.
Theretbre,
for only srlall-scaletasks.
it is usuallyattempted
t08
4.5
ERRORS
AND TRUNCATION
ROUNDOFF
PR
The tendencyis to pnsh forward with the numericalcomputationsand try to estimate
the accuracyof your results.This can sometimesbe done by seeingif the resultssatisfy
someconditionor equationas a check.Or it n'raybe possibleto substitutethe resultsback
into the originalequationto checkthat it is actuallysatisfied.
Finally you shouldbe preparedto pertbrm numericalexperimentsto increaseyour
awarenessof cor.nputatiorral
errorsand possibleill-conditionedproblols. Such experimentsmay involve repeatingthe computationswith a difl'erentstep size or methodand
comparingthe results.We may employsensitivityanalysisto seehow our solutionchanges
when we changernodel parametersor input values.We may want to try different numeriarc basedon dit'terentcomputacal algorithmsthat havedif'tbrenttheoreticalfoundations,
propcl'ties
and stabilitycharacteristics.
tional strategies,
or haveditferentconvergence
When the resultsof numericalcomputationsare extrcmelycritical and may involve
it is appropriateto takespecial
ramiflcations,
lossof humanlit'eor havescverec.conornic
groupsto solvethesame
precautions.
This may involvethe ussof two or morc indepcndent
problenrso that theirresultscanbe compared.
Thc rolesof errorswill be a topicof cclncernanclanalysisirr all sectionsof thisbook.
to spccificsections.
Wc will leavethcseinvestigations
be
AND DATAUNCERTAINTY
BI.UNDERS,
MODELERRORS,
Ju
as
ca
ca
w(
4.
Er
AI
i, n
. . tt
lle
th
hit
es
ifr
Ltl\
Although the following sourcesol'error are not dircctly connectedwith most of thenuofa
mericalmcthodsin this book, they can sometimcshavegreatimpacton the success
modelingeflbrt. Thus, they must alwaysbe kepl in mind when applyingnunrcricaltechnic;uesin thc contextof real-worldproblcms.
ist
lo
As
th
4.5.1 Blunders
ntc
ol.
er
tn
We areall f'amiliarwith grosserrors,or blundcrs.In theearlyyearsol'conrputcrs,
erroneous
numericalresultscor-rldsornetimesbe attributcdto rnalfunctionsol'the cornputeritself,
Today.thissourceol'ernlris highly unlikcly.andmostblundcrsrnustbc attributcdto humar
impert'ection.
Blunderscan occur at any stageof the rnat.hernltical
rnodelingprocessand canconThey can be rvoicled<-rnly
by soundknowledge
tributeto all the otherconlponcnts
of err<lr.
principlesand by the carewith which you approachand designyoursoluof fundarnental
tion to a problenr.
Blundersareusuallydisr-egarded
in discussions
ol'rrurnerical
rlethods.This is nodoubt
areto a certainextentunavoidable.
Horvever,we
dueto thef actthat,try aswe may,rnistakes
canbc nrinimizecl.
In parbelievethattherearea numberof waysin which thcir occurrcnce
ticular.thegoodplogramnringlrabitstlratwereoutlinedin Chap.3 areextrcmelyusetulfor
mitigatingprogramnringblundcr.s.ln addition.therearc usually sirnpleways to check
whethcra particularnunrericalmethodis workingproperly.Throughoutlhis book,wc discusswaysto chcckthe resultsof nurrericalcalculations.
4.5.2 Model Errors
Model error.srelatcto biasthat can be ascribedto incorrrpletemathu'matical
rnodels.An exampleof a negligiblemoclelerroris thef-actthatNewton'ssecondlaw does not accountfbr
relativisticeff'ects.
This doesnot detractfiorrrthe adequacyof the solutionin Exanrplel.l
PROBTE
4.1 Convert thc lirllowi
l0l l00l and| 10.(X)10
4.2 Convcrt the lirlloui
7 I 563 and 3. I zl.
4.3 For computcrs.thc
thought ol' as lhc srnirllc
givcs a numbcI grcrtcr th
idtr ca1\.t drr r\o\'td .r.
S t e pl : S e t r : I .
Step 2: Il' I + e is lessth
Otherwiscgo to I
Step 3: r' : e/2
Step4: Rcturrrto Stcp2
S t e p 5 :a - 1 x €
PROBLEMS
r 09
with the br-rngee
becausetheseerrorsare rninimalon the timc and sDacescalesassociatcd
jumper problern.
However,suppose
thatairresistance
is not proportional
to thesquareofthe fall velocity.
as in Eq. ( 1.7),but is relatedto velocity and otherfactorsin a diffbrentway. If suchwere the
case,boththeanalyticalandnumericalsolutionsobtainedin Chap.I wouldbe erroneous
becauseofmodel error.You shouldbe cognizantofthis typeoferror andrealizcthat,ifyou are
rcsults.
workingwith a poorlyconceivedmodel,no numericalmethodwill provideadcquatc
4.5.3
Dofq Uncertointy
in thephysicaldataon which
Errorssomctimes
enterinto an analysisbecause
of uncertainty
modelby hava rnodelis based.For instance,
suppose
we wantedto testthe bungeejr"rmper
jurrrpsandthenrneasuring
his or hcr velocityalicr a spccring an individualrnakerepeated
fied time interval.Uncertainty
wouldundoubtcdly
with thcscmcasllrcmcnts,
ns
bc associatcd
would flll tasterduringsomejumps thanduringothcrs.Thcsccrrorscancxthe parachutist
hibit bothinaccuracy
andirnpr-ecision.
If our instmments
consistently
underestirnate
or overor hiascd,dcvicc.On thc otherhand,
estimatethevelocity,wc arcdcalingwith an inaccuratc.
if the measurements
ol'prccision.
areranclomlyhigh andlow, we arcdcalingwith a qr-rcstion
Measurernent
by sr-rnrnrarizing
thc datawillr oneor morewellerrorscan bc quarrtil'icd
choscns(atistics
lhat convcyas much inlbrrnationas possibleregardingspecil'iccharacteristicsol' thc data.Thcscdcscriptivcstatisticsarc most olien selectedto represent( I) the
l o c a t i o no f ' t h cc c n t c ro l ' t h cd i s t r i h u t i oonl ' ( h cd a t aa n d( 2 ) t h ed e g r e eo f s p r e a do l ' t h ed a t a .
As such.theyprovidca rnsasurc
ol'Lhcbiasandirnprccision,
rcspcctivcly.
Wc will retLrrn
to
(hctopicol'charactcrizing
whenwe discussregression
in PartFour.
dalar-rncertainty
Althoughyou mr-lst
hc cognizanto1'blundcrs,
r-rroclcl
errors,anduncertainclata,the nurncricalrncthodsuscdfirr bLrilcling
nroclels
can bc stndicd,lilr the nrostpilrt,indepcndently
thatwc havcnot lnadcgross
ol'thcscerrors.Thcrelilrc,ftrrrlost of thishook,we will assr"rmc
Undcr
en'ors,we have a sounclnrodel,and wc are dcalirrgwith crror-ll'ccrncusurcnrcnts.
thescconditir)ns,
wc can studynunrcricalerrorswitl'toulcrlrlplicatingl'actors.
PROBTEMS
Writc your own M l'ilcbascdon this al-uorithrn
to dctclnrirrc
thc nrachincepsilon.Validatcthc lcsLrlthy comparingit with
t h c v a l u cc o r n p u t c w
d i t h t h c b L r i l t - i ln' u r t c t i ort,t1 r r .
4 . 4 I n a l n s h i o r sr i n r i l a lt o P r o b . , 1 . 3c, l o v c k r py o u r o w n
positivcrcal nunrhelusedin
M l'ilcto dctclnrincthc srnallest
MAf LAts. Bascyour algorithrnon the notionthatyoLrrcornol as thc smallcst nurnbcl that whcn addcd to onc
grcatcrthan l. An algorithrnhuscdon this putcrwill hc unablcto rcliablydistinguishbctwccnz.crornd
a nurnbcr
. otc thal thc
c a nh c d c v c l r r p c da s
a q u a n t i t yt h a l i s s r n a l l c tl h a n t h i s n u n r b e rN
rcsultyou obtain will clil'l'crll'onrthc valLrcconrpLrtcd
with
l:Sets: l.
r r , r l i r i n . C h a l l c n g eq u c s t i o n :I n v c s t i g a t e
t h c r c s u l t sb y
gcneratcdby your'
2:Il'I * e is lesstharror eqrralto l. thcn go to Stcp.5. takingthc base-2 logalitlrrnol'thenun.rhcr
go to Stcp3.
Olherwisc
c o d ca n c tl h o s co h t a i n c dw i t h r r . r I m i r ; .
zl.5 Although it is not conrrnonlyuscd,MATLAB allows
3:t = t:12
1:Returnto Stcp2
nuntben lo bc cxpresscdin single prccisiolr.Each valuc
5 : t= 2 x t
is storcdin :1 hytcs with I bit lor thc sign, 23 hits li)r thc
Convcrt
thc lirllowing basc-2 nurnbcrsto basc l0:
l 0 0 rl n dI 1 0 . ( X ) 1 0 1 .
Convert
the lirllowing basc-l{ nurnhcls to basc l0:
and3.1,1.
Forcomputcrs,
thc rnachinccpsilon c can also hc
tro
ERRORS
ROUNDOFF
AND TRUNCATION
mantissa,and 8 bits fbr the signedexponent.Determinethe
smallcstand largcstpositivefloating-pointnumbcrsas u,ell
as the machine epsilon lor single precision represcntation.
Note that the exponcntsrangefrom - 126to 127.
.1.6 For the hypotheticalbase-10computcrin Exarnplc4.2,
provethatthe rnachincepsilonis 0.05.
.1.7Thc derivativeot l(.r) = l/1 I - 3xr): is givenby
4.12 Use zero- throughthird-ordcrTaylor sericsexpanslons
to pre'dic:t.l(3)
firr
I (;r) : 25-1r- 6-12+ 7x - 88
usinga bascpointat.r: l. Computethe truepercentrelative
error c, for eachapproximation.
zl.l.l ProvethatEq.(4.I I I is exactfbr all valucsof.r if'l(-r)=
urz+hr+c.
6-r
4. l,l Usezero-throughfourth-ordcrTaylorsericsexpansrons
(I - 3rt),
to predictl(2) fbr f(-r) : In "r'usinga basepoint at,r = l.
Do you cxpectto havc diff iculticsevaluatingtlris Ir-rnction Computc the true percenlrclativc cror r, firl each appsoxiat ,r : 0.5771 Try it using 3- and ,l-digit arithmcticwith mation.Discussthc rneaningoI thc results.
,1.15 Usc lirrward rnd backwarcldillirencc approximations
chopping.
ctfO(h) and a ccntercddiff'ercnceapprtrximationof O(ft2)to
l.tt (a) Evaluatethc polynornial
cstimalc thc l'irst dcrivativc ol' the firnctioncxamincdin
I:,rr-7-r:+u,r-o.-15
Prob.4. 12.Evaluatothe clcrivativcat.r.: 2 usinga stcpsize
h : 0.2. CompareyoLrrrcsultswith the true valueof the
c>l'
irt .r.: 1.37.Usc 3-digit arithrnclicwith r:hopping.Evaluatc
Interprctyour resultson the hasisol'thc remaindcrivativc.
thc perccntrclativc crror.
ol'lhc
term
Til) ltrr serir'scxpiln)ii{)r).
dcr
(b) Rcpeat(a) hut cxplcssI'as
.f.16 Usc a ccntcrcddillercnccapproxirnation
of 0(h2)to
in
cstimatethe seconddcrivativcof'thc lunctionexamined
,r': ((,r - 7).r * 8).r 0.35
Prob.-1.12.Pcrlirrnrthc evirluationat .\': 2 usingslepsizes
Evaluutcthe clror and cornparewith part (a).
ol'/r - 0.25 and0. 125.Curnparcyour cstimatcswith thctrue
'1.9 Thc lirllowing infinitc scricscan hc used to approxivaluc ol'thr' sccontldclivativc.Intcrprctyoul rcsultson the
n l a t cd ' :
basisol'thc rcrnaindcrtcnr ol'thc Taylol scricscxllunsion.
1 . 1 7 l l l . r l < I i t i s k n o w nt h a t
-rl
.t l
.{
r"-Ii-tl-r
l.
I
.lI
n'.
(a) Provc that this MaclaLtrirr
\criL'\ rr\piln\iruris a spucial
cascol'thc Taylor sclicscxpansiun(Eq. 4. l3) with .r,0 ancllr : .r.
( b ) U s c t h c 1 ' a y l o sl c r i c st o c s t i n r a t c . l ' ( . r )c: ' a t , r i * r : I
lirr.r,: 0.2. Enrploythc zcro-.l'irst-,sccond-,and thirdortlcrvelsionsand contputcthc lr:rI lirr cachcasc-1.10The Maclaurinscricscxparrsion
lirr cos-r is
ll
RcpcatProb.zl.l0 lirr this scricsli)r -r : 0. I .
,l.llJ To calculatca planct'sspacccoordirrates,
wc havet0
s o l v ct h c l u n c t i o n
l(r'):
t - I-0.5sin.^
Lct thc bascpoint bc u: xi : rf2 <tnthc intcrval[0,r],
Dctclnrinclhc highcstolclcr'l-aylorscricscxpansionresulf
.rl
,rf
-r(' ,rS
in a rnaximutncrnrr ol'0.0 15 on thc specil'icdintcrval.
ing
C(r\\-l
I
I
)
4'. 6!
ttl
Thc error is cc;ualto thc absolutcvrluc ol'thc dill'erence
thc givcn lunction and thc specilicTaylor series
S t a r t i n gw i t h t h c s i m p l c svt c r s i o nc. o s , r : l , a d dt e r n r so n e bctwecn
c
x
p
u
n
s
i
o n( .H i n t :S o l v cg r a p h i c a l l y . )
at a tinrcto estirnate
cos(z/3).Altcr cachncw lcrm is acldcd,
4.
19
Consitlcr
thc lirnction l'(-r): -rr - 2.r -l- 4 on the intercornpLrtethc truc and appr0xirnatcpcrccnt rclltivc crrors.
v
a
l
2
,
2
1
w
i
t
h l : 0 . 2 5 . U s et h c l i r r w a r db, a c k w a r d , a n d
[
Usc _vourpockr.tcalculnloror MATLAB to dc-lcrrninc
thc
llnitc
centcrcd
dil'lcrcnceapproximationslirr lhc firstand
trLrcvaluc.Add tcrnrsuntil thc ahsolutcvalucol'thc approxsccond
so as to graphicallyillustratcwhichapdcrivativcs
imater-rrorcstimatcI'allsbclow an crror critcrionconlirrmproxinrationis mostilccrrlltc.Craphall thrccI'ilst-derivafive
ing to two significantI'igures.
.l.ll Perlbrm thc sarncconrputationas in Prob. 4. 10, but finitc di{l-ercnccapproximationsalong with the theoretical,
anddo thc samelix lhe sccondclcrivativcas wcll.
use thc Maclaurinscriescxpansionlbr the sin -r to cstirnatc
4.2(l Dcrivc Eq. (4.30).
s i n ( z / 3) .
4.21 RepcatExanrple4.5. but lbr l( r) : cos(.r)at .\ = n 16.
151
,r
_rr'
4.22 RepcatExarnple4.5, but lirr thc torwilrd divideddif+
+
:
Sln.f
-\
(Eq..1.21
3!
5!
7t
l'crence
).
R
r:
t
i'
2.1 o\
Yea
to s
The
sen
time
Rootsond Optimizstion
2.1 ovERvtEw
Yearsago, you leamed to use the quadratic formula
*b+4F-4ai
(PT2.1)
LU
to solve
:.:,,
(Yrz.2)
fk):qf+bx+c:o
The valuescalculatedwith Eq. (PT2.1) are called the "roots" of Eq. (PT2.2).They represenl the valuesolx that make Eq. tPT2.2'lequalto zero.For this reason.rootsare sometimescalled lhez.eros
of the equation.
Although the quadraticformula is handy for solving Eq. (W2.2), there are many other functions for
which the root cannot be determinedso easily. Before
the adventof digital computers,therewere a numberof
ways to solve for the roots of suchequations.For some
cases,the roots could be obtainedby direcf methods,as
with Eq. (PT2.l). Althoughtherewere eQuations
like
this that could be solved directly.there were,many
more that could nol. ln suchinstances.
the onlv alternative is an approximatesolution technique.
One method to obtain an approximate solution is
to plot the function and determinl where it crossesthe
r axis. This point, which representsthe r value for
which/(x) : 0, is the root. Although graphicalmethods are useful for obtainingrough estimatesof roots,
they are limited becauseof their lack of precision.An
alternativeapproachis to use trial and error. This
"technique"consistsof guessinga value of x and evaluating whether/(r) is zero.If not (as is almost always
the case),anotherguessis made,and/("r) is again evaluated to determine whether the new value provides a
betterestimateof the root. The processis repeateduntil
a guessresultsin anfir) that is closeto zero.
ltl
'
:,.;.
rt2
PART
2 ROOTSAND OPTIMIZATION
l'(;r) = o
,f"(r) < o
f(x) = o
.l'('r) = 0
f"G) > 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<r<fnnr a
opollronra
tt6
5.3
ROOTS:
BRACKETING
METHODS
This representsa real dilemma,becausemany designproblemsinvolve specifyrngthe
propertiesol cornpositionof a system(as represented
by it.spararneters)
to ensurethatit
perfornrsin a desiredmanner(as represented
by its variables).Thus, theseproblemsoften
reqr-rile
the determination
of implicit parameters.
The solutionto thc dilemmais providedby numericalmethodstbr rootsof equations.
To solvethe problemusingnumericalmethods,it is conventional
to reexpress
Eq. (5.1)by
The
subtractingthedcpendentvariableu frornboth sidesof the equationto give Eq. (-5.2).
valueof'nithatrnakcs.f(m):0is,thercfbre,therootoftheequation.Thisvaluealsorepresentsthc rnassthat solVesthe clesignproblem.
The lbllclwingpagesdealwith a varietyof numericaland graphicalmethodsfbr deterrniningrootsof relationships
suclras Eq. (5.2).Thesetechniclues
can be appliedto many
in cnginecringand science.
otherproblcn.rs
conl'ronted
ror-rtincly
5.2
pro
cal t
ans
whi
par
GRAPHICATMETHODS
wh
A sinrplenrcthodlbrobtainingan estir.nate
of the root of the ecluation
./(r) :0 is to make
a plot ol'thc firnctionant'lobservcwhercitcrossesthc.raxis.This point,which represents
t h e . rv a l u el b r w h i c h . l ( r l : 0 . p r o v i d c sa r o u g ha p p r o x i r . n a t ioofnt h er o o t .
E X A M P5
L .EI
Ho
ma
TheGrophicolApprooch
Problem Stotement. Use thc graphicalapproachto dcternrinethe nrassof the bungee
jurrper with a drag coel'l'icicnt
ol'0.25 kg/rn to havca velocityol'36 m/s after4 s of free
1 a l l .N o t c :T h e a
r c c e l c r a t i oonl ' u r i r v i t vi s 9 . 8 1n r / s r .
un
me
ab
pl(
s c t su p a p l o t o l ' E q .( 5 . 2 )v e r s u sm a s s :
S o l u t i o n . T h c l i r l k r w i n gM A T L A B s c s s i o n
Cv
''
r'ril =
nlll ''
(.).2,\; q
c ). l l 1 ;
I = 4;
v
';
'
1 r.. 1 ,
,
I f ) = r i q f L ( r 1 * m 1 r c, : c 1 ). * t . a n l r ( : ; q r 1 ( . J * c d . / n l r ) * L )
plot (rrr;r,11-r),qrr11
'
::'
-
ro
ar
ha
'\6;
!'
Fc
tr
rh
te
-1
pr
u
ri
-z
5.3
"50
I
I]
e
150
tt7
METHODSAND INITIALGUESSES
5.3 BRACKETING
The functioncrossesthc llr axisbetweenlrl0 and l50 kg. Visualinspectionof the plot
providesa roughestimateof the root of 145kg (about320 lb). The validity of the graphical estimatecan be checkedby substitutingit into Eq. (5.2)to yield
>> sqrt (g*145/ccL)*r,anh(::qrt,(g*cd/1,i5)*t,)
v
0.0456
which is close to zero. It can also be checked by substitutingit into Eq. (S.l ) along with the
parametcr values liorr this exarnple to give
> > - s q r t , ( q * 1 4 f i l c o ) * t , a n h ( s c T r t -( q * c d / r 4 5 ) * t )
35.0456
which is close to the desired fall velocity ol-36 rr/s.
are ilf limited practicalvaluebeciiusethcy arc not vcly precise.
Craphicaltechniques
Howevcr,graphicalmethodscan be utilizedto obtainroughcstimatcsol'roots.Theseestiin this chapter.
matescan be ernployedas startinggucsscslor numcricalmcthodsdiscussed
ol'tlreroot,graphiculinterpretations
areusefirlfirr
Asidefiorn providingroughestimatcs
understanding
the plopertiesol'the lirnctionsand anticipatingthe pitlallsof the nurnerical
rrethods.For cxample,Fig. -5.1showsa numberof'ways in which rootscan occur (or be
by a lowcr bound,r1andan uppcrbound.r,,.Figurc-5.lb dcabsent)in an intervalprescribcd
pictsthecasewherea singlcroot is brackctcdby ncgativeandpositivevalueso1'./(r). Howe v e r , F i g . 5 . l r l , w h e r/ (c- r r ) a n d . / ' ( - r , , ) a r e a l s o o n o p p o s i t e s itdhces.or af x i s , s h o w s t h r c c
rootsoccurringwithin the interval.In general,if / (.rr)and ./ (-r,,)havcoppositcsigns,thcrc
arean odd numberof rootsin the interval.As indicatcdby Fig.-5.la anclr', il' l (rr ) and.f (.r,,)
havethe sarncsign,thereareeitherno rootsor an evennunrbcrol'rootsbctwecnthc values.
arcusuallylrue,lherearecaseswherethcy do not holcl.
Althoughthcsegcncralizalions
F o r c x a r n p l et ,i r n c t i o n tsh a ta r ct a n g c n t i at lo t h c , r a x i s( F i g .- 5 . 2 aa) n dd i s c o n t i n u o ul 'su n c t i o n s( F i g .5 . 2 b )c a nv i o l a t et h e s cp r i n c i p l c sA. n c x a r n p l co l ' a I ' u n c t i o tnh a ti s t a n g e n t i at tl l
t h ca x i si s t h ec u b i ce q u a t i o n. / ( - r ' ): ( r - 2 ) ( . r-' 2 ) ( x - 4 ) . N o t i c ct h a t , r: 2 t n a k c st w t r
t e r m si n t h i s p o l y n o m i acl q u a lt o z e r o .M a t h e r n a t i c a l l y , . r : 2 i s c a l l e d' . tt r r u l t i l t l cn r t t .
that arc
Although they are bcyond the scopeof this book, thereare specialtcchniqr"rcs
expresslydesignedto locaternultipleroots(Chapraand Canale.2002).
The cxistenccof casesof thc typedepictcdin Fig.-5.2nrakesit difiicult to dcveloptirolproof computeralgorithmsguaranteed
to locateall the rootsin an interval.Howevcr,whcn
the rnethods
describedin thc lirlkrwingsccusedin conjunctionwith graphicalapproaches,
tions are extrerrelyusefulfor solvingrnanyproblernsconfl'ontedroutinelyby cnginccrs,
scientists,
andappliedrnathematicians.
METHODSAND INITIATGUESSES
5.3 BRACKETING
If you hada rootsproblemin thedaysbefbrccomputing,you'd oftenbe told to use"trial and
until the function
error" to come up with the root.Thiit is, you'd repcatcdlymakeguesses
was sufTiciently
closeto zero.The processwasgrea(lylacilitatedby the adventof sofiware
tt8
ROOTS:
BRACKETING
METHODS
xl
xu
(bl
(d)
F I G U R E5 . I
I lustrolion
of crnumberof generolwoysthoio roolmoy
prescribed
by o lowerbound.r,ond
occurrnon intervol
on upperboundx,,. Poris(oJond (c)indicoiethctif both
,/ (rr) ond ./(r,,) hovethesomesign,eithertherewili
be no rootsor therewill be on evennumberof rools
w i t h i nt h ei n t e r v oP
l o r t (sb )o n d ( d )i n d i c o tteh o tl f l h e
f u n c l i ohno sd i f f e r e ns ti g n so i t h ee n d p o i n t si ,h e r ew i l l
be on odd numberof roolsin theintervol.
FIGURE
5.2
lllustrotion
of someexceptionslo lhe generolcoses
depicted in Fig. 5. 1 (o) Multiplerootsfhot,occurwhen
the functionis tongentic to the -r oxis. For thiscose,
ol'h., 'nh lnc e"d
nninls nr'p rf nnno.ile
s;.n<
iho'c ore
for the inlervol.
on even numberof oxis inlercepiions
(b) Discontinuous
funciionswhere end pointsof opposiie
s i g ^ o ' o c [ e t o n e v e nn u r r b e r , or[o o ' s . , S p ei oc l s t r o ' e g i e s
^ro
,o^,
",ol
lnr nolarmrn,nn
Jho ,^^r.
[^,
thoa^
.-.o.
5.3 BRACKETING
METHODSAND INITIALGUESSES
lr9
tools suchas spreadsheets.
By allowing you to make many guessesrapidly, suchtools can
actuallymake the trial-and-errorapproachattractivefbr someproblems.
But, for many other problems,it is preferableto have methodsthat come up with the
correctanswerautomatically.lnterestingly,as with trial and error,theseapproachesrequire
an initial "guess" to get started.Then they systernaticallyhome in on the loot in an iterative fashion.
The two major classesof methodsavailableare distinguished
by the type of initial
guess.They are
.
.
As the namc implies, thescare basedon two initial guessesthat
Brttcketingmethod.s.
"bracket" the root-that is. are on either sidc of the root.
but thereis no
Opertmethotls.Thesemethodscan involveone or moreinitial guesses,
needfor them to bracketthe root.
For well-posedproblems,the bracketingmethodsalwayswrlrk but convergeslclwly
(i.e.,they typically take more iterationsto home in on the answer).In contrast,the open
rnethodsdo not alwayswork (i.e.,they can divergc),but when they do they usuallyconvergequicker.
arerequired.Thesemay naturallyariseliom the physical
In bothcases,initial guesses
contextyou are analyzing.However,in othercases,good initial guessesmay not be obvious.In suchcases,automated
approaches
to obtainguesses
would be useful.The following
sectiondescribes
one suchapproach,the incremental
search.
5.3.1 Incremenlol Seorch
When applyingthe graphicaltechniquein Example-5.1,you observedthat /'(.r) changed
sign on oppositesidesof the root. In general,if ./(x) is real and continut'rus
in the interval
from x7 to,r,, and .l (,r7)and f (x,,) haveoppositcsigns,that is,
/ ( - r 1 ) / ' ( x , , )< 0
rre
(5.3)
thenthereis at leastone real root between.r1afld,r,,.
Ircrenrentalseun:hmethodscapitalizeon this observationby locatingan intcrval
where the functionchangessign.A potentialproblcm with an incrementalscarchis the
cl.roice
ol'the incrcmentlength.If the lengthis too srnall,the searchcan be very time consuming.On tho other hand, if the length is too great,there is a possibilitythat closely
by the possibleexisspacedrootsmight be misscd(Fig. 5.3).The problemis compounded
tcnceof multipleroots.
An M-file can be developedrthatimplementsan incremental
searchto locatethe roots
of a firnctionf L.rncwithin the rangefrom xmin to xmax (Fig. 5.4).An optionalargument
ns allowsthe userto specifythe numberof intervalswithin the range.lf ns is ornitted,it
is automatically
setto 50.A f or: loop is usedto stepthrougheachinterval.In the eventthat
a sign changeoccurs!the upperand lower boundsarestoredin an arrayxb.
site
lies
' This lirnction rs a rnoditledversionof an M-lilc originally prcscntcdby Recktenwald(2000).
5.2
EXAMPLE
FIGURE
5.3
theincremenlol
lengthof lhe seorchprocedureis
becouse
Coseswhererootscouldbe missed
o n d w o u i db e missedregordlessof the
t o o l o r g e N o t et h o t h el o s tr o o to n l h er i g h it s m u l t i p e
i n c r e m elneln g l h .
xb = incsearch(func,xmin,xmax,ns)
function
incremental
search root fo.-aLor
? incsearch:
xb - incsearch (func, xmin, xmax,ns) :
%
finds brackets of x that contain sign chattges
%
on an interval.
of a function
?
? inpuL:
furLc - name of funct-iorr
%
Y
xni -, xmax
cndpoitr(s oI inrerva]
(default
= 50)
?
ns = number of subintervals
% outpuL:
xb(k,1)
is the lower bound of Lhe kth
%
2
xb (k,2 ) is tire upper bound of t-he kth
If no brackef.s found, xb - Ll.
%
if
nargin
< 4,
ns = 50;
encl %rf ns blank
sigtn chatrge
sign change
set
to
50
search
Z Increm€ntal
x = I i n s p a c e ( x m r - n ,x m a x , n s ) ;
f - func (x) ;
unless sign c|ranqe det ec t ed
nb = 0r :{b - i I ; e.xb is: null
for k = 1; length (x) -1
if sign(f (k)) ^-- sign(f (k+-L)) licheck for sign chanqe
nb = nb + 1;
. x{kl;
xbrnl,l)
xb(t'tb,2) = x (k+1);
end
end
jf isempt),,(xb)
?displzr-v that no brackets
disp ( 'no brackets forind' )
or increase ns' )
disp ( 'check rnterval
disp('nr:mber
disp (nb)
end
ol
brackets:')
%drspLay number of
FIGURE5.4
seorch
An M-file fo irnplemenlon incrernelrtcl
r20
were
f ound
brackets
5.3 BRACKETING
METHODS
AND INITIALGUESSES
t2l
EXAMPLE
5.2 Incrementql
Seorch
ProblemStotement. UsetheM-flle i ncsearch (Fig.5.a)to identifybracketswithin the
interval[3, 6] fbr the function:
(s.4)
J G ) : s i n (l 0 . r ) * c o s ( 3 . r )
Solution. The MATLAB sessionusingthe detaultnumberof intervals(50) is
>>
incscarch(@r
numbcr
sin(10*x)
possiblc
of
+cos (3*x),3,
6)
rooLs:
5
afnS 3 . 2 1 , 49
t.3061
3.3061
3.36'.t3
3. /34-t
3. t9\9
4.6531
4.'-t14j,
\.{:i2
l
a.6939
A plot ol'Eq. (5.4)alongwith thc root Iocationsis shownhere.
4.5
?E
J
5.5
Althoughfive sign changesare dctccted,becausethe subintervals
are too wide, the function rnissespossiblerootsat .r j 4.25and-5.2.Thesepossiblemots look like they might be
doLrblenrots.However,by usingthe zoom in tool, it is clearthat eachrepresents
two real
rootsthal are very closc together.The f'unctioncan be run againwith more subintervals
with thc resultthat all nine sign changcsarc located
> : . i n c s e a r c h ( t l x s i n ( ' lO * r ) + c o s ( 3 * x ) , 3 ,
number of
9
3 .2 424
3.3536
possiblc
rooLs:
3 .212.7
3.3939
b,100)
122
ROOTS:
BRACKETING
METHODS
3.-t213
4.2121
4.2424
4.5910
,r.1515
5.1318
5 . 566J
3.t57ia
4 .2424
4.2',t21
4 . 1 ? , 13
5.1B1B
5.2r2).
':.591 A
2
The lbregoingexampleillustrates
that brute-lbrcenrethodssuchas incremental
search
are not lbolproof.You would be wise to supplementsuchautomatictechniqueswith any
otherinlbrnrationthatprovidesinsightinto the locationol'the roots.Suchintbrrnation
can
be fbunclby pkrttinglhe functionand throughLrnderstandinc
the physicalproblernfrorn
w h i c ht h ee q u a t i o no r i g i n a t e d .
5.4
FIG
Ac
f,oi
Ex
plc
let
Th
BtSECT|ON
The bisectionntctlu)dis a variationof the incremental
searchrnethodin which the interval
is alwaysdivideclin hali. If a functionchangessign over itn interval,the firnctionvalueat
the rnidpointis evaluated.
The locationof the root is thendeterminedas lying within the
subintervalwherethe sign changeoccurs.The subintervalthen beconresthe intervalfix
the next iteration.'flreprocessis lepeateduntil the root is known to the reqr"rired
precision,
A graphicaldepictionof the methodis provicledin Fig. 5.5.The lbllorvingexamplegoes
throughthe actualcornputations
involvedin the method.
E X A M P L E5 . 3
<1
T h e B i s e c i i o nM e t h o d
ProblemStotement. Usebisectionto solvethe sarneproblenrapproached
graphically
in
E x a r n p l5e. l .
Solution. The first stepin bisectionis to guesstwo valuesof the unknown(in the present
problenr.rr) thatgive valuesl'or t 0n') with dillelent sisns.Frornthe graphicalsolutionin
Nc
lat
Nr
wl
thr
,, t,
r23
5.4 BISECTION
.f(nt)
2
U
-2
-6
First iteration
I
V
"r/
xl
#
Second iteration
I
t
xt
Third iteration
)(f
x,,
xt.\r
xu
ffi
I
V
Fo_.t
Fourth iteration
FIGURE
5.5
A grophicoldepiclionof the bisectionmethod Thisploi correspondsto the firsifour iterotions
trom Er.omple5 3
Example5. l, wc can seethatthe t'unctionchangessignbetweenvaluesclf-50and 200.Thc
p l o t o b v i o u s l y s u g g e s t s b e t t e r i n igt u
i ael s s e s , s al y4 0 a n d I 5 0 , b u t t i l r i l l u s t r a t i ' u ' t ' p u r p o s r - ' \
lct's assumewe clon'thave the bencfitof the plcltand havc madeconservativeguesses.
Thcrcfble,the initial estimatcof the rtx>t.r,.lies at thc midpointof the interval
.50+ l(x)
_
| -)<
./.
Notethattlreexactvalueof tlrerootis 142.1316.
Thismeans
thatthc valueof 125calculatedherehasa trueDercent
errorof
relative
- r2-sI
,.,-l 142.1316
lx100?4:12.434/,
r,/r-
i
112.7316 I
Next wc computethe productof the functionvalueat the lower boundandat the midpoint:
I ( 5 0 )I ( 1 2 - 5:1 - 4 . 5 1 9 ( - 0 . 4 0 9 1: 1 . 3 7 1
which is greaterthanzero,and henceno sign changeoccursbetweenthe lower boundand
the midpoint.Consequently,
in the upperinterval betweerr125and
theroot mustbe locatecl
200.Therefbre,we createa new intervalby redefiningthe lower boundas 125.
124
BRACKETING
METHODS
ROOTS:
At this point,the new intervalextendsfronrxl :
rnatecau thenbe calculatedas
l2-5to r, :200. A revisedl'ootesti-
125+ 20t)
2
r v h i c h r e p r e s e n t s a t r u e p e r c e n t e rlreo,lr:o1f3 . 8 5 % . T h e p r o c e s s c a n b e r e p e a t e d t o o b tain refineilestinrates.
For example,
- 0 . 4 0 9 ( 0 . 3 . 5 9: ) - ( t . | 1 l
. f ( t t s l . 11 16 2 . - 5:;
Therelbre.the roo( is now irr the lower intervalbetween125and 162.-5.
The upperbound
is rede[inedas l 62.5.anclthe root cstinratetirr the third iterationis calculatedas
1 2 5* 1 6 2 . 5
: | 43.7-5
)
,
i
The nrethodcan be repeated
until
which represents
a percentrelativeelrur of a, : 0.101)()(,.
the resultis accurafeenoughto satistyyour needs.
We endedE,xarnple
5.3 with the statement
thatthe rnethodcouldbe continuedto obtain
of therrxrt.We rnustnow developan olr.jective
criterionlirr decidingwhen
a lel'inedr'stimate
to terminatethe rnethod.
rnightbe to end the calculationwhen the errrlrl'allsbelow some
An initial suggestion
p l c s p e ci e
f d l e v e l .F o r i n s t a n c ei n. E x a r n p l e
5 . 3 .t h c t r L r e
r e l a t i v ee r r o r d r o p p e fdr o m l 2 . 4 3
t<t0.J09(,4
duringthecoulseof'thecornputttion.We rnightclecidethtt we shouldternrinate
the errorestlmates
when the errordropsbelow.say.0.-5%,.
This strategyis llawed because
in theexanrplewer-ebasedtlrrknowledgeol'thetruer(x)tol'the firnction.This woulclnotbe
t h e c a s ci r r a n a c t u u ls i ( r , r a t i obne c r u s ct h e r ew o u l d b c n o p o i n ti n u s i n gt h e r n c t h o di f w e
tulrcadyknew the roo(.
thal is not contingerrt
on lbreknowledge
ol'the
Therelirre.wc recluirean errorestirnate
r o o 1O
. n e w a y ( o c l ot h i si s b y r ' s t i r n r L t iunrgrl p p r o x i r n r r tpee r c e nlte l a t i v ee r r o l ' i l si n I r e c a l l
E q . ( 4 . 5 )|
1_r1c'_ ..y.'lil
1
T]
te
to
T}
on
Fl
Er
of
(55)
lo','l:lff]l(x)%'
whc.re.r]1""is the loo{ lirr the present iteration and -rf'l'lis the rool lionr the plevious itemt i o n . W h e n c , , b e c o r n c sl e s s t h a n a p r e s p e c i l ' i e d
s t o p p i n gc r i t e r i o n e , , t h e c o n r p u t u t i o ni s
Icillriilulc(1.
E r r o rE s t i m q t e fso r B i s e c t i o n
E X A M P L E5 . 4
P r o b l e mS t o t e m e n t . C o n t i n u eE x a r n p l e5 . 3 u n t i l t h e a p p l o x i r n a teer r o r t a l l s b e l o wa
s ( o p p i n c r i t e r i o rot l ' € . ,: 0 . - 5 % . U s eE q . ( - 5 . - 5
t o) c o n r [ ] u t teh ee r r o r s .
S o l u t i o n . T h e r e s u l t s o l ' t h e f i r s t t w o i t e r a t i o n s l b r E x a r r p l e 5 .l 32 w
5 ae nr ed l 6 2 . 5 . S u b s t i t u t i n gt h e s ev a l u e si n t o E q .1 - 5 . -y5i1e l d s
,
l u rl,:,
lrAl 5-r?sl
#ll\l0o/,,:23.08c/o
I6t.5 I
Fig
exh
---__--*---+
5.4 BISECTION
125
Recallthat the true percentrelativeeror for the root estimateof 162.5was 13.85%.Therefore, le,,I is greaterthan le, I. This behavioris manifestedfor the other iterations:
Iterolion
xt
50
125
125
125
1 3 43 7 5
t39 a625
l4t 4463
t 4 25 7 B l
In
)n
1e
+-1
xu
204
204
t 6 25
I43.7
5
143.75
1 4 37 5
1 4 37 5
1 4 37 5
xl
125
t 6 25
1 4 37 5
1 3 43 7 5
r3 9 0 6 2 5
14t 4063
|42 578t
|43t64)
le,l ("/"1
2 30 B
1 34 4
698
337
)66
o82
o4l
le,l l7"l
1 24 3
t385
a7l
586
258
093
0il
030
Thus aftereight iterationsle,,l finally falls below €.,:0.5c/o, and the computationcan be
terminated.
Theseresultsare summarizedin Fig. 5.6.The "ragged"natureof the true error is due
to the fact that,fbr bisection,the true root can lie anywherewithin the bracketinginterval.
The true and approximateerrors are far apart when the interval happensto be centered
on the true root. They are close when the true root firlls at either end of the interval.
F I G U R E5 . 6
Errors
for lhe bisection
meihod.Trueond opproximcte
errorsore piottedversus
thenumber
of iterolions.
CS
be
VC
he
all
Approximateerror, le,,l
o
1n
.z
o
rais
(t)
rl
or
Although the approximateerror does not provide an exact estimateof the true error,
Fig. -5.6suggests
that la,,l capturesthe generaldownwardtrendof le,l. In addition,the plot
exhibitsthe extremelyattractivecharacteristic
that le,,l is alwaysgreaterthan le,l. Thus,
126
ROOTS:
BRACKETING
METHODS
when le,,I falls below s' the computationcould be terminatedwith confidencethat theroot
is known to be at leastas accurateas the prespecifiedacceptablelevel.
While it is dangerousto draw generalconclusionsfrom a single example,it can be
demonstratedthat le,,l will alwaysbe greaterthan le,l for bisection.This is due to the fact
that eachtime an approximateroot is locatedusingbisectiofloS.r,.: (xt * x) /2, we know
that the trlle root lies somewherewithin an interval of A-r : xu - xt. Therelbre,the root
must lie within *Ax/2 of our estimate.For instance,when Example5.4 was terminated,
we could rnakethe definitive staternentthat
x, : 143.1641
+
- 142.5781
1.13.7500
: I ' 1 3 . l 6 4+l 0 . 5 8 5 9
In essence,
Eq. (5.5) providesan upperboundon the true error.For this boundto be
exceeded,the true root would haveto fall outsidethe bracketinginterval,which by definition could nevetoccur fbr bisection.Otherroollocatingtechniques
do not alwaysbehave
as nicely.Although bisectionis generallyslowerthan other methods,the neatness
of its
error analysisis a positivefeaturethat nrakesit attractivefor certainengineeringand
scientificapplications.
Another benefitof the bisectionmethodis that the numberof iterationsrequiredto attain an absoluteenor can be computedu priori-that is, before startingthe computation.
This can be seenby recognizingthat beforestartingthe technique,the absoluteerroris
sll:r!l-rro:AxO
where the superscriptdesignatestlre iteration.Hence,beforestartingthe methodwe areat
the "zero iteration."After the first iteration.the errorbecomes
,
"2
A.r"
F'-_
Becauseeachsucceedingiterationhalvesthe error,a generalfbrmula relatingthe errorand
t h e n u r n b eor l ' i t e r a t i o nrs i s
E (" - -
A-rt'
.\D
L
If C,,.,1
is the desirederror,this ecluation
can be solvedfbri
:,"r,(#)
log(L.r\tf E,,.,1)
log 2
(5.6)
Let'stesttheformula.For Example-5.4,theinitialintervalwasA.ro : 200 - 50 : 150.
After eisht iterations.
the absoluteerrorwAS
tr-
- 142.518t1
1r43.7s00
=
0.58-59
Wecansubstitr-rte
thesevaluesintoEq.(5.6)to give
n: log:(150/0.5859):8
I MATLAB providesthe I
oq2 function to evaluatethc base-2logarithmdirectly.lf the pocket calculatoror
computerlanguageyou are usinl doesnot includethe base-2logarithm as an intrinsic function.this etluation
showsa handy way to computc it. ln general,log,,(,r)= log(_r)/log(b).
-.------
5.4 BISECTION
127
Thus, if we knew beforehandthat an error of lessthan 0.5859was acceptable,the formula
tells us that eight iterationswould yield the desiredresult.
Although we haveemphasizedthe useof relativeenors for obviousreasons,therewill
be caseswhere (usually through knowledgeof the problem context) you will be able to
specifyan absoluteerror.For thesecases,bisectionalongwith Eq. (5.6)can providea useful root locationalgorithm.
5.4.1 MATTAB M-file! bisecr
An M-file to implementbisectionis displayedin Fig. 5.7. It is passedthe function(func)
alongwith lower(x1) andupper(xu) guesses.
ln addition,anoptionalstoppingcriterion(es)
F I G U R E5 . 7
A n M J r l et o i m p l e m etnhteb i s e c t i om
nethod.
f unct ion Iroot, ea, it.er I -bi secr (f unc, xl, xu, es, maxit, varargin)
% bis;ect: r:oot Iocation
zer.oes
?
. .. ) :
lroot,ea, iterl=bisec1,(func,xl,xu,es,maxit,p1,p2,
%
uses bisect-ion rnethod to find the root of func
? inpuL:
func : name of function
%
ts
xl , x r
louer orrd .pl,Fr gues:-s
= 0.0001%)
es = desired relative
%
erroi: (default
(defaul t = s0)
%
maxiL = maximum allowabl e iterations
pl,p?,, ... = additional
parameters used by func
%
% output:
?
r oot ,. rea L root
?
ea = approximate
: rlumbef of
?
rter
relative
er:ror
iterations
(t)
if nargin<3,error('at
least 3 input argurnents requi red'),end
test - tunc (xl.,varargin{ : } ) *func(xu,varargin{
: }) ;
i f test>0 / €rror ( 'no si gn change' ) , end
if nargin<1 I i sempty(es), es-0.0001.;end
:lf nargin<5 i lsempty(maxit),
maxit=50;end
itcr- - 0; xr = xl;
(l)
while
xr:old = xr';
(x1 +.xu)/2;
xr'iLer = iLer + 1;
* 1 . 0 0 ;e n d
i f xr -= 0, ea = abs ( (xr , xrold) /xr)
test = func (xl,varargin{:
} ) *func (xr,varargin{:
} );
if
< 0
test
XU
=
XI;
-
xr;
=
0;
elseif
xf
test
>'0
el se
ea
end
i f
ea
<=
end
l:OOt
=
Xl-;
es
iter
>=
maxit,break,end
r28
ROOTS:
BRACKETING
METHODS
5
and maximum iterations(maxit) can be entered.The function first checkswhetherthere
are sufficient argumentsand if the initial guessesbracket a sign change.If not, an error
message
is displayedandthe functionis terminated.
It alsoassignsdetaultvaluesif maxit
a n d e s a r e n o t s u p p l i e dT. h e n a w h i l e . . . b r e a k l o o p i s e r n p l o y e dt o i m p l e m e ntth e
bisectionalgorithmuntil the approximateerror falls below es or the iterationsexceed
m a x it .
We can employ this functionto solvethe problemposedat the beginningof the chapter.
Recall that you need to determinethe massat which a bungeejumper's fiee-ftrll velocity
exceeds36 rrls after4 s of free fall given a drag coefficientof 0.25 kg/rn.Thus, you havet0
find the root of
9.81m
f (m): \,1025 ,"n (
@,, -) - 3 6
In Example5.1 we generated
a plot of this functionversusrltlssandestimatedthattheroot
fell between140and 150kg. The bi sect f unctionfrom Fig. 5.7 can be usedto determine
the root as
*tarrh(sqrt,
sqrL (9.81*m/0.25)
ea i ter] =brsect ( fm, 40,200 )
>>
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<tw2()tr,.
yourown M-l'ilefirr biscctionin a sirnilarlashDevclop
toFi.e.
-5.7.Howcvcr,rathcr than usin_r:l
thc rnaxir.nrrrn
andEq. (-5.-5),
crnployEq. (5.6) as yoLrrstopping
. Makcsureto roundthc rcsultol'Eq. (5.6)up to thc
highcst
integer.
Testyour llnction by solvingProb.5. I
5.5 A bcantis loadcdas shownin Fig. P5.-5.Usc thc biscction rncthoclto solvc lir thc 1-rosition
insiclcthc bcantwhcrc
thclc is no rnolllcn(.
100 lb/ft
8,.,r= 0'000I
Prob.5.1, but nsc thc falsc-positionIncthodto
Repeat
yoursolution.
Develop
an M-filc fbl thc lnlsc-position
rnethod.Tcstit
solvine
Prob.5. I
F I G U R EP 5 . 5
1 0 0t b
r36
ROOTS:
BRACKETING
METHODS
5.6 (a) Dctermine the roots of .l (x) : - 14 - 20r *
l9-{2 - 3i3 graphically.In adclition,determinethe flrst root
of the lunction with (b) bisectionand (c) falscposition.For
(b) and (c) use initial -guesses
of ;r7: -l and.rr :0 and a
stoppingcriterion of I o/c.
j
5.7 l,ocatethe first nontrivialfu()tof sin(.r) : .r w hcre,rrs
in radians.Use a graphicaltcchniqueand biscctionwith the
initial intervallrom 0.5 to l. Perfblm the computationuntil
e,, is lessthan o, : 2c/,.
5.tJ Detcrminethe positivc rcal rrrtr(of ln(,rl) : ().7 (a)
gfaphically, (b) using threc iterations of the biseetion
nrethod,with initial gucssesof -r1: 0.5 and -{r,: 2, and
(c) using threciterationsof thc talse-position
rnethod,with
the sameinitial gucssesas in (b).
5.9 Thc salurationconccntrationof clissolvedoxygcn in
licshwatcrcan be calculatcdwith thc etluation
tnr,1:
t3().3441
+lj]I01"]o'
1,,
6.64230Ux 107
-l-
r,i
t i . ( r 21 9 4 9x 1 0 "
T,l
l.2z1.ltl00
x l0r(l
r,l
P
case,the width and the cross-scctionalareacirnbe related
to
dcpth -r'by
I*-
B: 3*.r,
,tr-
ano
A , .: 3 r , *
_f-
2
Solve lbr the criticaldepthusing (a) the graphicalmethod,
(b) bisection.and (c) falsc position.For (b) and (c) use
initialgucsscstll'ri : 0.-5iind-r,,: 2.-5,anclitcrateuntilthe
approximatc
errorljrllsbelorvl% or thc numhcrof iterations
exccccls10.Discr.rss
yonr results.
5.ll Thc Michaelis-Mcntcn
rnocleldcscribcsthc kineticsof
cnr)/nlemccliatcd
rstctiolls:
tlS
tlr
.S
"1,+S
whcrc .! = suhstrateconccntration
(rnolcs/L),tl,r = Inrxi.
rlum uptakcrltc (rnolcs/L/d).anclA. : thc hall-saturarion
constan[,which is thc s{.{bstratc
lcveI at rvhichuptakeishalf
ol Lhcrnaximumlrnolcs/Ll. Il' thc initial substritlc
levelat
/ : 0 is So,this dil'f'crcntial
cquiltioncan bc solvcdfbr
I
\a
FIGURE
P5.T3
.S: Sir- tt,,,/* ft, In(Sg/S)
Devclopan M-l'ilc to gcncratca plot ol'S vcrsusr lbr the
whcrc o.7 : thc saturationconccntration
ot'dissolvcdoxycasc whctc .Sp: l0 ntolcs/L, 1,, : ().J lrolcs/L/d,and
r):
gen in licshwatcrat I atnr (ntg L
and 4, : ahsolutc
*., : 2 rlttlcs/L.
lcmpcrature(K). Rcmcmbcrthat f, : T +213.1 5. wherc
5.12 A rcvcrsilrlcchcnticalrcaction
("C).Accordingto thiscquation,satLlrilti()n
r : tcmperature
dccrcascswith incrcasingtempcraturc.For typical rratural
24+B-c
u'atcrsin tcnipcrateclimatc.s,
thecquationcan hc usc.cl
to dclclrnincthatoxygcnconccntration
rangcsli'orl 14.62| ntg/l, can bc churactcrizcd
by thc cqLrilibriunt
lclationship
at 0 "C to 6.4I 3 nig/L at 40 ''C.Gir,cn a valucol oxygcnconK:
ccntration,this lirlrnr"rla
and the biscctiontncthodcan hc
uscdto solvc lirr tcmpcratulcin "C.
(i l'cptcscnts
lhc rrorlcnclaltrrc
lhc conccntrationof
(a) Il'thc initial gucsscsalc sct as 0 and 40 "C, how rlany r,,'hcre
constitucntl. Supposclhat wc rlcl'inca variublc.{asrepre.
biscctionitcrationswould bc rcquircdtu dctcrniincternscntingthc nurnbcrol'rnolcsol'C thatarc ploduccd.Conser.
pcralurcto an absolulccrror of'0.05'C'l
vutior)ol' nlasscirt bc uscd kr lclirrrnulatcthc cquilibrium
on (a),dcvclopandtcsta biscctionM-l'ilcl'unelion
{b) Basecl
r c l a t i o n s h iaps
to dcternrine7 as a lunctionol a givcn oxygcncor)ccn( t , . . r*r. I )
trction.Tc.styour lunclionlirro,T : lJ. l0lnd l2 rng/LA'Checkyoul results.
Q ' , , . n 2 x ) ) ( t 1 , . 0- . r )
-5.10Watcr is llowing in a trapezoidalchannclat a ratc ol'
whcrc thc subscript0 dcsicnatcsthc initial concentrution
Q : 20 mr/s.The critical clepth.r'tirr suchI channelrnust ol cachconstitucnt.Il K - 0.016,(,.o: 42, (i,.0:
28,and
satisfvthe cquation
(,.0: -1,dctcrntincthc valucol'.r'.
(a) Obtainthc solLrtion
graphically.
O2
0:I
Lt r An.
(b) On thc basisol (a), soh,clbl theroorwith inirialguesu
^,^1
of'-t,: 0 rnd -r,,: 20 to r, :0.5%,. Choosccitherbisec
tvherc g : 9.81 rll/s1. ,4,.: the cross-scctionillarca (rrr).
tion or' lulsc position to ohtaiu your solution.Justif,v
and R : thc width ol'thc channel lt the surtacc (m). Fol this
your choicc.
5.13 Figure P5.l3a show
early increasing distributc
i n g e l a s t i cc u r v c i s r s t c F i g
'v : - t l L - r - r s + l '
l20EIt.
Use bisectionto dctcrminc
(thatis, thc valueof _rwhcn
valueinto Eq. (P5.l.l) ro dcr
d e f l e c t i o nU
. s ct h c l i r l l q y *
p u t a t i o nL:: 6 ( X ) c m , E :
ond u.r,,: 2.5 kN/crn.
5.14 You huy a $25,000
1
down at $5,500 pcr ycar Ic
you paying'?The firrmular
paymentsA, nr-rmbcr
ol'yea
A:
i(l,L;\t]
"'
P-"
(t+i),_l
5.15 Many ficlds ol'engine
estimates.F-orexamplc, trar
it necessaryto determinc s
trends of a city and adjacer
urbanareais dccliningwith
PuQ):Pu.n^^e-k"'aI
r37
PROBLEMS
rvhilethe suburbanpopulationis growing,as in
rD \ . i l t l t
P rrt -
I + L P , . m a x / P o- l l s - t ' r
$'hcrc P,,,,,,.,*,
tu, P,,u,"*,P,,,and k, : ernpiricallyderivcd pavalucsof
ralnctcrs.Determincthe timc and con'esponding
'nt'c2()%largerthan the city.
P,,(t)andP,(/) when thc sLrburbs
The pararnctcrvalues arc P,,.n,",: 75,000, k,, : 0.045/yr,
P,.u,iu: 100.000people, P,.,'.,x: 100'000 people, P0 :
10.000people,and t, : 0.08/yr.To obtain your solutions,
mcthods.
usc (a) graphical,and (b) false-position
-i.16 Thc rcsistivily p ol' doped silicon is based on thc
chargcr7on an clcctron,thc clectrondcnsity n, anclthc electrorrrrobility ll. Thc electrondcnsity is givcn in terms of
thc dopingdcnsityN iinclthc intrinsiccarricrdensityn,.The
clcctronrnobilityis describcdby the tempcrature
I thc rcfThe
crcncc tempcraturcI,,, and thc referenccmobility pr1y.
cqLlntions
rcquircdto computcthe rcsistivityare
(a)
(.r=0,t=0)
I
iIGURE
P5.I 3
qnlr
\4',ncfc
5'13
Figure
P5.l3rrshowsir unilirlnrbcarnsubjcclto a lindistlihutcdloacl.Thc ccluatiorr
lirr thc rcsultatlyrncreasing
hgelastic
cun'cis (sccFig. P5.l3r)
r = -l_l.t
120F.
IL
..t + 21-r.rr Lr r )
(p5.13)
,,:j(r*
:
DctcrnrincN, givcn 7ir : 300 K. f : 1000 K. 1111
( Vr s ) r ,4 : 1 . 7 x l 0 r ' )C . n , : 6 . 2 1 x l 0 ' ) c r n3 ,
l3-50crn
and a dcsircdlt : 6.5 x l0t' V s cm/C. Usc (a) biscctionand
(b) the lllsc positionnrcthod.
-5.l7 A totalchargcB is unilirrrnlydistrihutedarounda ringshapcclconductorwith rldius a. A chlrgc q is l()catcdat a
The lirrcc
-r lhrrn thc ccntcrol'thc ring ( Fig. P-5.17).
drstancc
cxcrtcdon thc chargcby the ring is given by
Usebisectron
ttrdctcrr.ninc
thc noint ol'rnaxirnurn
dcf'lcctitrrr
valueol'.rwhcrctlt'ld.r'- 0). Then sr.rbstitulc
ts,thc
this
intoEq.tP5.l3)to dctcrmincthc valucol'thcrnaxirnurn
. Uscthclirllowingprrarnctcrvalucsin y()r.r'corlr
ion:L - (r(X)crn.E : -50.(XX)
kN/crn'. 1 : 30.000cm*.
I
u0.t
'r : ub=25kN/ctn.
1n co (.rl + a2yr,':
Youbuy a $2-5.000
;riccc ol' cqr:ipnrcntlil' nothing
at$5.500Dcl'voar lor' 6 vcars.What intcrcst ratc arc w h c r c c , , : t 1 . 8 5x 1 0 ' t C t ( N m l y . F i n d t h c c l i s t a n c c x w h c r c
5
pupaying'l
Thc lirrnrularclatingpfcscntworth P. ittrttual t h c l o r c c i s 1 . 2 5 N i l ' 1 1a n d Q a r c 2 x l 0 C l i ) r a r i n g w i t h a
r
n
.
r
a
d
i
u
s
o
l
'
0
.
9
pyments
A. nurnber
ol'ycirrsrr,lnd intercstratci is
i(l+ l)"
A =P
i l + l ) , r- I
rcquireaccuratcpopulation
Manylieldsof'cnginccring
Forcxarnplc.
transportation
cngincclsrnightlind
necessary
to dctcrnrincscparatclythc populationgrttwth
of a city andad.lace
nt suhurh.Thc populatiorto{' (hc
tcr
isdecliningwith timc accorcling
area
P , ,u , " * t ,
l"'
F I G U R EP 5 . 1 7
*
Prr.rrin
r38
ROOTS:
BRACKETING
METHODS
5.lti For tluid flow in pipcs, friction is dcscribedby a dimerrsionlessnumber.the F unn i ng .fr i c't i on .fot:to r .1.The Fanning liiction factor is dependcnton a nurnberof paramctcrs
related to the sizc of the pipe and the fluid, which can
all be representcdby anotlrcrdimcnsionlcssquantity.the
Revnoldsttumher Re. A firrmula that prcdicts.l given Rc is
the r.,onKurnuul etlutttion:
Ir-\
::4toc,,(nc.
Jf
l)
heatof dry air r1,kJ/(kg K) to tcmperaturc(K):
r ; , : 0 . 9 9 4 0 3 + 1 . 6 1| x l 0 1 I + 9 . 7 2 1 5x l 0 8 r :
- 9 . - 5 8 3 8x l 0 r r T 3+ 1 . 9 5 2 0x l 0 l ' 1 r l
R
Detcrminethetcmperaturc
heat
thatconesponds
to a specilic
o i l . l k J / ( k gK ) .
5.20 The upwardvclocity of a rockct can be computcd
by
thc tbllowinglblnrula:
u.+
Typical valucstbr thc Reynoldsnumbcrtbr turbulentI'low
are 10,000to 500,000anclfbr thc Fanningfiiction tactor arc
f ) . 0 0 It o 0 . 0 1 .D e v c l o p al i r n c t i o nt h i l tu s e sb i s c c t i o tno s o l v c
lor./ given a uscr-sr.rpplicd
valuc of Rc bctween2.500and
1,000,000.
Designthe lunctionso that it cnsuresthatthe absolutecrror in thc'rcsulti, 6,,.,,. 0.00000-5.
5.19 Mcchanicalcnginccrs,as wcll as mostothercnginecrs,
uscthernrodynamics
cxtcnsivclyin thcirwork.Thc lirllowing
polynornialcan bc uscd to rclatc thc zelu-prcssurc
spccilic
l' :
,.{ lfl --
llln
ttto - qt
-
,qI
whereu : upwardvclocity,u : thc vclocityat whichfuelis
: thc initial massofthe
expcllcdrelrtivc tt>thc rosket./??o
rockctat tinlc/ :0, q : thc l'uclconsumption
riite,irndg = ths
dorvnwat'daccelera(ionol' gravitv (assumcd constant=
t).lJI rn/sr).If r.r: 2000 m/s, rt,, : 150.(X)0
kg. and4 =
2700 kg/s, computethc tirne at which u = 7-50rn/s.(Hint:t
is somcwhcrcbctwccnl0 and.50s.)Dcternrincycurrresults0
thutit is w'ithinl% ol'thc trr,re
value. Chcckyoul answer.
;^
F
Senl
valu
time
Hov
quic
a sir
ing I
il
Roots:Open Methods
CHAPTEROBJECTIVES
The primary objectiveof this chapteris to acquaintyou with open methodslilr finding
the root of a singlenonlinearequation.Specificobjectivesandtopicscovereclare
.
o
.
'
.
.
Recognizingthe diff'erence
betweenbracketingarrdopenmethodslbr root
location.
the fixed-pointiterationrlethodand how you can cvaluatcits
Understanding
c o n v e r g e n ccch a l u c l c r i iscts .
Knowing how to solvea rootsproblernwith the Ncwton-Raphson
methodand
the conceptof quadraticconvergence.
appreciating
Knowing how to irnplementboththe secantand the modil'iedsecantrnetlrods.
Knowing how to useMATLAB's f zero lunctionto estimateroots.
Learninghow tclrnanipulate
anddctcrminethe rootsof polynomialswitlr
MATLAB.
or the brackctingrnethodsin Chap.5, the root is locatedwithin an intcrvalprescribed
by a lower and an upperbound.Repeated
applicationof thesemcthodsalwaysresults
in closerestimatesof lhe true valuc ol'thc root. Suchmethodsare saiil lo be conver(Fig. 6. la).
progresses
,genlbecausethey moveclosertclthe truthas the cornputation
In contrast,the open ntethotlsdescribedin this chapterrequireonly a singlestarting
valueor two startingvaluesthat do not necessarily
bracketthe root.As such,they sometimes divergeor move away from the true root as the computationprogresses(Fig. 6. lb).
However,when the open methodsconverge1Fig.6.lc) they usuallydo so ntuch more
quickly thanthe bracketingmethods.We will beginour discussion
with
of opentechniques
a simpleapproachthat is usefulfor illustratingtheirgeneralfbrm and alsoltr demonstrating the conceptofconvergence.
r39
t40
ROOTS:
OPENMETHODS
EXAMPLE
6.
|J
ri
r,
^1
^u
#
xt
-lx
lH
-tt-ru
lol
FIGURE6.I
Grophicol deplcfionof lhe fundcmentoldjfferencebetweenthe (o) brocketingond (b) ond {c)
open rnelhodsfor root loccrlionln (crl,which is bisection,the rooi is consircrinecl
wilhinlheinfef
vol pr-^scribed
by.r1 ond -r,,.]n controst,for ihe open methoddepictedin (b) ond (c),whLchs
Newlon Rophson,cr lorrnulois used fo proiectfrorn,r; io -r;, I in cn itercrtive
foslrion.Thusthe
thr:shope of the frnc-rion
methodccrneilher(bJclivergeor (c) convergerop,id1y,
dependingc-,rr
c n d t h e v o l u eo f t h - ^i n i l i o l( t L r e s s
TI
6.I
SIMPLEFIXED-POINTITERATION
As just mentioned,opennrcthodsemploya fbrnrulato predictthe root.Sucha fbrmulaca
be developedfbr sirnple.li,red-poitrt
itcration(or, as it is alsocalled,one-pointiterirtionor
s u c c e s s i vseu b s t i t u t i o nb)y r c a r r a n g i ntgh c ' f ' u n c t i o n . l ( . r: 0
) s o t h a t . ri s o n t h el e f t - h a n d
sideof the equation:
-r:.q(x)
(61)
This tlansfbrnlationcan be accourpli.shed
eitherby algebraicmanipulafionor by simpJy
addingr 1oboth sidesof the originalequation.
The utility of Eq. (6. l) is that it providesa tbrmula to predicta new valueof .rasa
f u n c t i o no f a n o l d v a l u eo f r . T h u s ,g i v e na n i n i t i a lg u e s sa t t h e r o o t - r i , E Q . ( 6 l.) c a n h
usedto computea new estimate.r;11eSexprcssed
by the iterativefbrnrula
ri+t : g(ri)
(6.:l
pr
pr
sit
gri
be
alt
Tt
an
t4l
6 . 1 S I M P LF
EI X E D
P O I N TI T E R A T I O N
As with many other iterativeformulasin this book, the approximateerror for this equation
can be determinedusing the error estimator:
'":lfflrcou"
(6.3)
lterotion
EXAMPLE
6.1 SimpleFixed-Point
Problem
Stotement.Usesimplefixed-point
iteration
to locate
therootof /(r) : e '\ - x.
Solution. The function can be separated
directly and expressedin the form of Eq. (6.2) as
,fi+l
:
C-"
:0, this iterativeequationcan be appliedto computc:
Startingwith an initial gr-ress
of -r11
rt
U
I
2
3
4
-5
6
7
B
9
r0
0 0000
r 0000
4 3675
0 6922
0 5005
o 6462
a 5454
4 5796
0 560r
o 5/II
4 5649
le ol, "/"
r00 000
t7l B2B
46 854
38 309
)7447
| 157
5 903
3 4Bt
t93I
t r09
le,l, "h
t 00 000
76322
35 I 3-5
22 450
t) 755
6 894
3 835
2 tsg
) 239
o 7a5
0 399
le,l,/le'1,-t
4 763
a 46A
a 628
0 533
0 586
0 556
o 573
4 564
0 569
4 566
Thus. eachiterationbrinssthe estimatccloscrto the true valueof the root: 0.56714329.
Noticethat the true percentrelativcerrorlbr eachiterationof Examplc6. I is roughly
proportional(by a firctorof about0.-5to 0.6) to thc errorfiom the previousiteration.This
propcrty,calledIinearconvergen(e.
is charactcristic
ol'fixed-pointiteratron.
Aside liom the "rate" of convergence,
we mustcommcntat this point aboutthe "possibility" ol'convergence.The conceptsof convcrgenceand divergencecan be depictcd
graphically.Rccallthat in Scction5.2, we grapheda functionto visualizeits structureand
bchavior.Suchan approachis ernployedin Fig. 6.2a for the f unction.l (r ) : e ' - ,r. An
a l t c r r t u t i v r ' g r u p h i ca ap lp r o u c h
i s t o s e p a r u ttch cc q u a l i o ni n l o t w o c o m p o n e npt a r t s a
. sin
.ftG): JzG)
Thenthetwoequations
,vr- .ll(.r)
(6.4)
.t'::./:(-r)
(6..5
)
and
142
OPENMETHODS
ROOTS:
F I G U R E6 . 2
'h'
:-:^:-Jr
theroot'l
fordererminins
methods,
i;;il;;;e
r v v uu " E '
"'
srophiccrl
l9l
!1ll::'
i
u
n
c
t
i
o
n
s
i
h
"
.
o
*
p
o
n
"
n
t
o
f
l
n
i
e
r
s
e
c
t
i
o
n
t
h
e
lhe,
Ot ,".tot
ooiniwhereit crosses
""li;
6.1
FIG
Gro
ilnrn
osc
canbeplottedseparately(Fig.(l'2b).The-rvalucscorrespondingtotheintersectionsof
the rclotsof /('r) : 0'
thesefunctionsrepresent
and divergence
'l'he
twcl-curvemethodcan now be usedto illustratethe convergence
=r
equiltions'\'l
pair
of
a
as
of fixed-pointiteration.First, Eq. (6.l) can he rcexpressed
with
case
was
the
As
separately'
ancl.y2:g(r). Thesetwo equalionscan then be plotted
Eqs.(6.4)and(6'5),therootsoll'(x):0correspondtotheabscissavalueattheintersec'
shapeslbr.v2 : g('r) ate
tion of the two cufves. The function .)'t : 't and tbur diflbrent
plottedin Fig. 6.3.
theconesponding
For the first case(Fig. 6.3a),the initial guessof r11is usedto <letermine
to
horizontally
lefl
moving
by
pointon rhe.yzcurvel.{(; g(r0) l. The point [xr , .rr] is located
method:
fixed-point
of
the
the -yrcurve.Thesemovementsareequivalentto the first itcration
- r 1: g ( x s )
,re is usedto obtainanestiThus, in both the equationand in the plot, a startingvalueof
g(,rr)l
and then to [x2,:r2].This
mateof ,rr. The next iterationconsistsof moving to [.,r1,
iteri
roo
Fig
Cha
por
slop
r 43
ITERATION
6.I SIMPLE
FIXED-POINT
J0
(cl
(d)
FIGURE6.3
of simpefixedpoinl
G r o p h i c o ld e p i c t i o no f ( o )o n d ( b ) c o n v e r g e n coen d ( c )c n d i d ) d i v e r g e n c e
iterotion.Grophs (o) ond (c) ore co led monotonepotlernswhereos(b) ond (c) ore colled
o s c il o l i n go r s p i r o p o t t e r n sN. o t e t h o tc o n v e r g e n coec c u r sw h e n g ' ( . r ) l - < 1 .
iterationis equivalentto the equation
12: s(,rt,
The solutionin Fig. 6.3a is convergentbecausethe estimatesof -r move closerto the
root with eachiteration.The sameis true for Fig. 6.3b. However,this is not the casefbr
Fig. 6.3c and d, wherethe iterationsdivergefrom the root.
A theoreticalderivationcan be usedto gain insightinto the process.As describedin
Chapraand Canale(2002), it can be shown that the error for any iterationis linearly proportional to the error from the previous iterationmultiplied by the absolutevalue of the
slopeof g:
Ei+r :
g'G)Ei
ROOTS:
OPENMETHODS
144
Consequer-rtly,
with eachiteration.For lg'l > 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) :
-<"
T
I
which can be substituted
alongwith the originall'unctioninto Eq. (6.6)to give
'\','1 -
\',
it
tt
t/ ,
z,
.r1
^.
e-',i-l
Startingwith an initial gucssof ,re : 0, this iterativeequationcan be appliedto compute
le,l, "/"
0
I
2
3
4
0
0 -500000000
056631t003
o 56/143165
4 . 5 6 71 4 3 2 ( ) 0
IOO
llB
a )47
0 0000220
.10 8
Thus, the approachrapidly convergeson the true root. Notice that the true percentrelative
el'rorat each iteratiorrclecreases
much faslerthan il doesin simple fixed-nointiteration
(comparewith Example6. l).
EXAMPLE
6.3
P
R
6.2 NEWTON-RAPHSON
t45
l(r)
F I G U R E6 . 4
Grophicoldepiction
of theNewton-Rcrphson
method.A tongentto ihefunction
of ,r; fthotis,
of ihe rootot .r;11.
/'(.r)l is exlropooteddown to the,r oxislo provideon estimote
As with otherroot-locationmcthods,Eq. (6.3)can be usedas a terminationcrite.rion.
ln addition,a thcorcticalanalysis(ChapraandCanale,2002)providesinsightregardingthe
rateof'convergence
as expressed
by
(6.7)
Thus,thc crror shouldbc roughlyproportionalto the squareol'the prcviouserror.In other
words, the numbcr of' significantfiguresof accuracyapproxirnately
doubleswith each
iteraticrn.
This behavioris calledqruulrilic (onvcrgcn(eandis oneo1'the rra.jorreasonsfbr
thc popularityol'thc rnethod.
Although thc Ncwton-Raphson
method is ofien very efl-icient,thcre irrc situations
whereit pcrlbrmspoorly.A specialcase-multiple roots-is discussed
elscwhcre(Chapra
and Canale,2002). However,cven whcn dcalingwith simple roots,difficulliescan also
a r i s ea
. s i n t h ef b l l o w i n se x a m o l .e
E X A M P6L.E
3
A S l o w l y C o n v e r g i n gF u n c t i o nw i i h N e w t o n ' R o p h s o n
Problem Stotement. Determinelhe positiveroot of .l(l) : ,rr0Raphsor.r
methodand an initial guessof .r : 0.-5.
Solution. The Newton-Raphson
tbrmulafbr this caseis
.t .i r - rr : . t l
-t -. tl { ) -
|
l,)rI:
which can be usedto compute
I using the Newton-
146
ROOTS:
OPENMETHODS
xi
0
l
2
3
4
0.5
5 16 5
46.485
4t 8365
37.65285
4A
4l
42
0 0 2 3I 6
aaoa24
leol, "/"
99.432
2130
o 22g
0 002
Thus, afterthe first poor prediction,the techniqueis convergingon the true root of I, but
at a very slow rate.
is
Why doesthis happen?As shownin Fig. 6.5,a simpleplot of the first few iterations
helpfulin providinginsight.Noticehow thelirst guessis in a regionwherethe slopeisneat
zero.Thus, the first iterationflings the solutiontar away from the initial guessto a new
where./(x)has an extremelyhigh value.The solutionthcn plodsalong
value(,r: -51.6-5)
fbr over 40 iterationsuntil convcrgingon the root with adequate
accuracy.
F I G U R E6 . 5
methodfor o cosewilh s ow convergence
The
Grophicoldepiction
of theNewtonRophson
shoots
thesolution
for fromtherool Thereofter,
insetshowshow o neorzeroslopeinitiolly
thesolulion
veryslowlyconverqes
on lhe root
l(x)
2E+17
1E+17
FI
T
Aside from slow convergencedue to the natureof the function, other difficultiescan
arise,as illustratedin Fig. 6.6. For example,Fig.6.6a depictsthe casewherean inflection
6.2 NEWTON-RAPHSON
F I G U R E6 . 6
poorconvergence
methodexhibits
FourcoseswheretheNewton-Rophson
r48
ROOTS:
OPENMETHODS
'
point (i.e..f (x) : 0) occursirr the vicinity of a root. Notice that iterationsbeginningat.re
progressivelydiverget'rorntheloot. Fig.6.6b illustratesthetendencyof theNewton-Raphson
techniqueto oscillatearounda localmaximumor minimunl.Suchoscillationsmaypersist,
or,
asin Fig. 6.6b,a near-zeroslopeis reachedwhereuponthesolutionis sentf'arfrom thearea
of
interest.Figure6.6c showshow an initial guessthatis closeto one root canjump to a location
severalrootsaway.This tendencyto move awayfronr thearcaof interestis dueto thefactthat
near-zeroslopesareencountered.
Obviously,a zeroslope[./'(.r) : 0l is a rcaldisasterbecauseit causesdivisionby zeroin thetr-ewton-Raphson
fornrulaIEq.(6.6)1.As in Fig.6,6d,
it meansthatthesolutionshootsoff horizontallyandneverhitsthex axis.
Thus,thereis no generalconvergence
criterionfor Newton-Raphson.
Its convergence
dependson the natureof the lunction and on the accuracyof the initial guess.Theonly
remedyis to havean initial guessthat is "sufficiently" closeto the root. And for somefunctions,no guesswill workl Good guesses
areusuallypredicated
on knowledgeof thephysical problemsettingoron devicessuchas graphsthat provideinsightinto thebehaviorof
the solution.It also suggeststhat good cornputersoliware shouldhe designedto recognize
slow convergence
or divergence.
6.2.1
I ATLAB M-file! newtraph
An algorithmlbr the Newton-Raphson
methodcanbe easilydeveloped(Fig.6.7).Notethat
theprogrammusthaveaccessto thefunction(f unc) andits first derivative(ofunc).These
can be simply accomplished
by the inclusionof user-dcfined
functionsto compute
these
quantities.Alternatively.as in the algorithmin Fig. 6.7, they can be passedto thefunction
as arguments.
Alier the M-file is enterecland saved.it can be invokedto solve tbr root. Forexamole.
lor the simplcfunction.r2 - 9, thc root cun he dcterminecl
as in
: ' j ' n e \ ^ , / L r a I J n ( G ( x ) - " , ^ : l ! 1, ( 4 ( x )
2,"x.,5)
Ft
An
.'
Newton-RophsoB
n u n g e eJ u m p e rP r o b l e m
E X A M P L E6 4
'
Problem Stotement. Usc the M-file functiontiom Fig. 6.7 to detcrntinethe rnass
ofthc
bungeejumper with a dragcoefTicient
of 0.25kg/rnto havea velocityol-36rn/safter4sof
fice fall. The acceleration
of grlvity is 9.81 nr/sr.
Solution. Thc firnctionto bc evaluatedis
.f ltnl
:rff""n(
grii,
nl
)
/
- r,'(l;
(E6.41)
To apply thc Newton-Raphsonmethod,the derivativeof this function rnustbeevaluirtedwith resDectto the unknown.n:
'+#:irF-""^(,H')
-fir r..rrt(#;)
6.3 SE
As
the
ma
6.3 SECANT
MEIHODS
f u n c c i o n I r o o u , e a , i r e r J- n e w L ' d p h r f u n - , d i r n c , y r , F S ,m a x ' L , v o ' d r g i n )
% newtraph:
Newton-Raphson root Location
zeroes
% lroot, ea, it:erl=newtraph (f unc, df unc, xr, es,maxit,pl ,p2, . . . ) :
?
uses Newton Raphson nethod to find the root of func
% input:
%
func = name of function
?
df unc = nane of derir,zat irze of f unct lon
?
xr = init ia1 guess
- 0.0001?)
(default
%
es = desired relative
error
(default
= 50)
%
maxit = maximum allowable
iterations
- additional
p1,p2,..,
parameLers usted by function
%
% output:
%
root = real root
(?)
%
ea - appr.orimate relative
error
= number of iterations
%
iter
if
if
if
itoz
nargin<3, error ( 'at least 3 input arguments required'
nargin<4 isernpt)'(es),es=0.0001;end
nargin<5 | isempty (maxit ), naxit,-50 ; end
-
) , end
O.
(1)
while
= xr ;
xrold
xr = xr - f u;rc(xr)/df unc (xr) ;
. 1,
iLer
ir<r
* 100;
if xr -= 0, ea = abs( (xr - xrol.d) /xr)
if ea <= es i iter
>= maxit, break, end
end
root : xr;
end
FIGURE
6.7
A n M l i l e t o i m p e m e n if h e N e w l o n R c p h s o nm e t h o d
W e s h o u l dn r e n t i o nt l r a ta l t h o u g ht h i sd e r i v a t i v ci s n o t d i f T i c u l[to e v a l u a t ei n p r i n c i p l ei,t
involvcsa bit of concentration
andeflbrt to arriveat thc l-inalrcsult.
The two fbrntulascan now be used in cilnjunctionwith the function n, wtr nLih [o
evaluatcthe root:
>> y = Gn sqrt ('j.81*m/0.25)*l,anh(sqrl, (9.81*0.25lm)*4)
l6;
> > d y = G n I / 2 * : - r c 1 r l1, 9 . ' E I / ( m * 0 . 2 5 ) ) * t a n h ( ( 9 . 8 - l * 0 . 2 5 l m )
^ (1
/ 2 , 1 * 4 ) 9 . r 3 1/ ( 2 * p ; * s c c h ( s q r l ( ( 1. B l * 0 . 2 5 l m ) * 4 ) ^ 2 , ;
r " > 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<-rst
a single value in return for valuessent to it. Perceivedin this scnsc.functionsare not
alwayssimpletbrrnulaslike theone-lineequationssolvedin theprecedingexamplesin this
chapter.For example,a f unctionmight consistof nranylinesof coclethatccluldtakea significantamountof executiontime to evaluate.In sornecases,the functionmight evenrepresentan independent
computerprogram.For suchcases,the secantand modifledsecant
methocls
are valuable.
6.4 MATLAB FUNCTIONz fzero
The rnethodswe havedescribedto this pointareeithcrreliablebut slow (bracketing)
or f-ast
but possiblyunreliable(open).The MMLAB f zerc-rfunctionplovidcsthe bestclualities
of both.The f zero functionis designedto find the realroot of a singleequation.A sirnple
representation
of its syntaxis
f z , e r o( f u n c t i o n , x a J
where funcrjon is the nameof the functionbeingevaluated,
and x0 is the initial guess.
Note that two guessesthat bracketthe root can be passedas a vector:
f zero (funcLion, lxa xl ) )
where x0 and x1 are guessesthat bracketa sign change.
r52
ROOTS:
OPENMETHODS
- 9
Hereis a simpleMATLAB sessionthatsolvesfor theroot of a simplequadratic:.f:
-3
Clearlytwo rootsexistat
and 3. To find the negativeroot:
>> 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 ;<] . a vector containing the root x and the function evaluatcd at the root f-x,
optron.s is a data structure created by the opt- lmseL function, and p1 , ;:2. . . are any
parermetersthat the function requires. Notc that il'you dcsirc to pass in parametersbut not
u s e t h e o p t , i o n s , p a s sa n e m p t y v e c t o r I I i n i t s p l a c c .
The opt imsct functicln has the syntax
optiors
-
o f i L r m s c t .( ' p . l r ' , ' , v c t l , , ' p a r
'
,v,tl ,, ...J
w h e r e t h e p a r a m e t e r p a r -h a s t h c v a l u e v a l , . A c c l m p l e t e l i s t i n g o f a l lt h e p o s s i b l e p a r a m eters can be obtained by rnerely entering opt- i rnsct, at the command prornpt. The parameters
that are cornmonly used with the Izer o function are
clisplay, Whensetto'ir-er'displaysadetailcdrecordol'all theiterltions.
Lolx: A positive scalar that sets a termination tolerancs on x.
EXAMPLE
6.6 The r
toofld,,1
rl iir
F un c t i o n s
P r o b l e mS t o t e m e n t . R e c a l lt h a t i n E x a m p l e6 . 3 , w e f b u n dt h e p o s i t i v er o o t o f . l ' , x ) :
rl0 - I using the Newton-Raphson
mcthodwith an initial guessol'0.5. Solve the same
p r o b l e mw i t h o p l i m s e t a n d f z e r o .
Solution. An interactiveMATLAB sessioncan be implementedas fbllows:
>>
opLions
= op1-imset ('display',
'ite
r');
154
ROOTS:
OPENMETHODS
Func-count
L
2
5
6
f (x)
-0.999023
-4.9q9267
r-L6
0.999454
0 .95254E
0.14
I.I4
-0.385007
-1
searcll
2.14122
search
l-he interrral
t 0.14, 1.141
4.47I
23
24
25
Looking
26
21
2B
'2
x
0.5
c.485358
4.5r4r42
0.48
0.52
.Q
30
31
3',.,
for
a
zero
a .24\21 :,
a.61 2636
O.90tr318
1.0:3itsJ
0.98q128
0.998894
1.00001
3i
34
l1
Zero
I
found
ir'l-
0.998709
-0.9993s1
-0.998554
in
bisectron
If
n(
H
U
de
let
bisecl-ior-r
interpo
laf,ion
interpolaLion
inl-erpolatlorr
rnterlrolaLiorr
I.324 5e D11
0
4,
6
br.sect i on
7.6BlB5e-005
-1. B306ie 007
.
a
fr
E
interpolati.orr
0.9P,1442.
a .626056
o .2572t I
-0.103511
,0 . 01Laarl
--/.
n
r
search
-1
it'-rv.rl:
v
n
Procedure
initial
search
search
search
search
search
i nterpolat
i orr
i ni erpoIat
i on
L 141.
wl
no
VA
eig
we
0
Thus.after25 iterationsof searching,fzero finds a signchange.It thenusesinterpolationandbisectionuntil it getscloseenoughto theroot so that interpolation
takesoverand
rapidlyconvergeson the root.
Supposethatwe would like to usea lessstringenttolerance.
We canusethe opt imset
functionltl set a low maxinrumtoleranceand a lessaccurateestimateol'the rootresults:
>- 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<lmialthat has roots con'esponding
to two of the
o r i g i n a lr o o t so l ' E q . t B 0 . Z . l ) : 0 . - 5a n d - 1 . T h i s q u a d r a t i ci s ( , r - 0 . 5 ) ( . r* l ) = . r r *
0.5.r- 0.5. It can be enteredinto MATLAB as the vectorb:
.,.. ir -
.5 ,... 1
i i
la)
1.0000
0.5()00
(r.5000
Note that the pc;1y function can bc useclto pellirrrn tlre satle task as in
-:
b = poly([0.5
1])
D-
1.0000
0.5000
0.500t)
We can divide this polynomialinto the originalpolynornialby
r:> [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<;tbctwcenl-5and20,
u)cthodto thisfunc(ionusingan
Apply thc Ncwton-Raphson
i n i t i a ls u c s so f - r 1-; 1 6 .1 5 .E x p l a i ny o u r r c s u l t s .
6.13 In a chcmicalcnginccringproccss,wiitelvapor(H:0)
thatn significant
is hcatcdto sLrlTiciently
high tcrnpcraturcs
portion of thc watcr dissociatcs.o| splits irpart.to filnnoxygcn (O1)anclhydrogcn(H:):
H:OP H: * lO:
the
If it is assLrnrcd
thut this is the only rcactioninvolved,
nrolctiaction-t of HlO thatclissociates
canbc Rrpresentedby
.\' Tr\
,/^r
,,
A:,
r -
f Y l
(P6.13.1)
1-.\'
whcr-cK is thc rcaction'sccluilibriumconstultandp, isthe
totalprcssurcof thc rnixturc.ll'7r, : l.J atnrandK - 0.M,
dctcrnrincthc valr.rc
of ,r thatsatisl'ies
Eq.(P6.I3.1.1.
ccluation
ol statcis givenby
6.14 Thc Rccllich-Kwong
R7
''-
t-lt
r:(t:I lt)JT
whcrc R : tlre uniycrs:rlgas constrntl:0-5ltt kJ(kgK)1,
(kPa),
(K), / : absolutepressurc
7 : afrsolute
tenrpcrnturc
lunclir : thc volurncol a kg ol'gas {rrr/kg). Thc parameten
tr irntllt arc calculatcclby
,r -- 0.4)7
R r r ': ' 5
l),.
t.
/r -- 0.0u66Rl),.
engi.
whcrc 7r,.: 45tt0kPa arrd7, = l9l K. As a chcrnical
fuel
ncer,you arc askcdto dctcrnrint:thc ilmountof methane
that can bc hcld in a 3-rnt tilnk a( a tcmperirture
of -50'C
with a prcssurcol'65.0(X)kPa.Usea rtxrtlocatingmethodof
your choicc to calculatcl and the-ndctcrnrinethe mass
of
rncthilnccontaincdin the tank.
cylindu
6.15 Thc volurncof liquid V in a hollow horizontal
o1'r'adius
land lcngthL is relatcdto thedepthof thcliquid/tby
T
,,.
,.,
I
6.16 A catenary cable i
points not in the sarr
Fig. P6.l6a, it is subje
weight. Thus, its weigh
length along the cable u
scction AB is depicted in
the tension forces at the e
cal force balances, the fol
ofthe cable can be derive
d'y
--::-
w
dxz
TA
,rc
Calculus can be employ
height of the cable y as a
Ta
./w
\
y : _.o.nl\
*/
.o
(a) Use a numerical me
parameter ?".rgiven
and Y6 : 6, suchthat
-t:50.
(b) Develop a plor of v
)
6.17 An oscillatingcurre
bY I :9e-t cos(2tt), w
valuesof/ such that 1 :
6.18 Figure P6.18showr
tor, and a capacitor in par
to expressthe impedance
-1
- , , - / i t ' ,} / r - h : l L
V = l l r c , , s{ ( ' - 1 I
L
\T
/
J
/r givenr : 2 nr,L : 5 rnr,andl': 8.5mr.
Dctelntine
+(,c-
r 63
PROBLEMS
(b)
F I G U R EP 6 . I 6
cable is one which is hung between tw<r
116A catenary
pintsnot in the same vertical line. As depicted rn
P6,l6u,
it is subjectto no loads other than its own
Ihus,its weight acts as a unilblm load per unit
alongthe cable ur (N/m). A lree-body diagrarnol' a
lcngth
ionAB is depictedin Fig. P6.l6D. where T1 and Ts are
tension
lbrcesat the end. Basedon horizontaland vcrtiforce
balances,
the fbllowingdif l-erential
cquationrnodeI
FIGUREP6.I8
cablecan be derived,
#=;,FfS
whereZ: impedance(Q). and roris the angularlreguenc_v
Frnd the ar that resultsrn an rmpedance
ot /-i (/ usrngthe
tzero lunction with initial guessesof I and 1000for the
lirllowing parameters:
R :225 A, C' - 0.6 , l0-r' F, and
L : 0.-5H.
(r.19 Real mechanicalsyslemsmay involve the deflection
o l ' n o n l i n e a rs p r i n g s I. n F i g . P 6 . 1 9 ,a b l o c k o f m a s sr i i s
canbe employedto solvc this equationtbr the
ofthecablel asa lunctionof distance,r:
7,1
- cosn
ur
Usea numerical
methodto calculatea value lbr the
parameter
I,q given values for the parametersut : 12
and
r,s= $, suchthat the cablehas a height of ,r': I 5 at
r=50.
Drvelopaplot
of,y versus-rfbr,r :
50 to 100.
Anoscillating
currentin an electriccircuitis described
l=9e-' cos(2rt).where/ is in seconds.Deterrrrine
all
oftsuchthatl:3
Figure
P6.18showsa circuit with a resistor,an inducacaoacitor
in oarallel.Kircfihofl-'sru/escan be used
theimpedance
o1'thesystemas
I
ll p:
+(.c-
@t
FIGUREP6.19
r64
ROOTS:
OPENMETHODS
P
fixed-point iteration sch
givenQ:5.S:0.0002
your schemeconvergesf<
equal to zero.
6.25 Seeifyou can deve
the friction factor base
describedin Sec. 6.6. yor
result for Reynolds numbe
e/ D ranging from 0.0000
6.26 Use rhe Newton-Ra
f (x) :
FIGUREP6.2O
releaseda distanceI above a nonlincarspring.The resistancefbrce F of the spring is given by
F--\krt!
Employinitialguesses
of (
results.
5.27 Given
f (x) : -2xb - 1.5
FIGURE
P6.2I
*k.d3i'1
infbrmation:(a) For hracketingmethods,initialguesses
of0
and R will bracket a single root ti)r this cxample.(b) Fot
2krtl5l2 I
open mcthods,an initial guessof R will alwaysconverge.
{):
+ ; k 1 t l '"- n t g < l - t t t q l t
tl
6.22 Pertbrm the identical MATLAII operationsas thost
Solvc fbr z/, given thr: fbllowing parametervaluc-s:ft1 :
in Example6.7 to manipulatcand iind all the rootsof the
5 0 . 0 0 0 g / s 2f .t z : 4 0 g / ( s 2 n r s ) .n : 9 0 8 . . ( : 9 . 8 1 m / s 2 , polynomial
and /r - 0.4-5m.
. l s ( x ) : ( - r | 2 ) ( . r* 5 ) ( x - l ) ( x - 4 ) ( . r- 7 )
6.20 Aerospacc enginc'erssomctimes computc the trajcctories of projcctilcssuch as rockcts.A relateclproblemdcals 6.2-1 ln control systcrns analysis, transfer tunctionsae
u,ith the traicctory of a thrown ball. Thc traje-ctoryo1'a ball devr'loped
rclatethc dynamicsof asys.
that mathematically
thrown by a right ficlder is clefinedby thc (x,.1,)coordinatcs tem's input to its output. A transf'erfunction for a robotic
as displayedin Fi-e.P6.20.Thc trdecbry can be modelecl
as p o s i t i o n i nsgy s t c nils g i v c nh y
Conscrvation
ofcnergl'can bc uscdttr show that
Rt
v - (tan0o)-r- -j5r'
Ltrai
cos-u0
*.r'1y
F-indthe appropriateinitial angledo,if urr:20 m/s, and the
distanceto the catcheris 3-5m. Note that thc throw lcavcsthc
right ficlder's hand at an elcvationof 2 m and the catclrcr
rcceivcsit at I m.
6.2| You arc designinga sphcricaltank (Fig. P6.2I ) to hold
water fbr a small villagc in a devclopingcountry.The volume of liquid it can hold can bc computedas
l /
-
n
,llR-rl
l t ' -
3
wherc'V: volunreIm]1,/r : depthof waterin tank Im], and
R : t h e t a n kr a d i u sl m l .
If R : -l m, what depth rnustthe tank be filled to so that
it holds 30 m3? Use three iterationsof the most efllcient
nurnericalmethodpossibleto determineyour answer.Determine the approximate relatir"e cror after cach iteration.
Also, providejustification fbr your choice of method.Extra
C(.tt-
c(s)
rt+ l2.5sl*-50.5,r*66
= N(.t)
r'u +
l 9 s r+ 1 2 2 s+22 9 6 s- l 1 9 2
where C(s) : system gain, C(s) : systemoutput,N(s)=
systcminput, and s : Laplacetransfbrmcomplexfrequency,
Use MATLAB to find thc roots of thc numeratorandde.
nominatorand t:rctorthescinto the form
G ( , r )=
( . s* a r ) ( . s + d 2 ) ( . r+ . r 3 )
( s * D r ) ( s* b : ) ( . r* h ) ( s * l t t )
wherc zri andb1 : the rootsof thc nume-rator
anddenomina.
tor, fespectively.
6.2-l The Manning equationcan bc wriften fbr a rectangulat
openchannelas
(=^r :
- x
" - n s ^1 4
- .,5(sH)'''
rt(B i 2H Jtlt
where B : flow (m3/s),S : slope (mlm), H = depth(m),
l
and n : the Manning roughncsscoefficient. Develop
Usea rootlocationtechn
this function.Performiter
tive error falls below5Zo
r65
PROBLEMS
fixed-point
iterationscheme to solve this equation for H
given
0 = 5, S : 0.0002.B :20, and r?: 0.03.Provethat
your
scheme
convergesfor all initial guessesgreaterthan or
equal
t0 zero.
6,25Seeif youcandevelopa lbolproof function to compute
fie frictionlactor based on the Colebrook equation as
described
in Sec.6.6. Yor.rrfunction should return a precise
rcsult
forReynoldsnumberranging from .1000to | 07and lbr
e/Dranging
fiom 0.00001to 0.05.
6,26Usethe Nervton-Raphson
method to find the root of
:'@_
x
)
2
.f(.r)-.-tr
Employ
initialgucsses
of (a) 2, (b) 6, and (c) 8. Explainyour
rcsults.
6J?Given
- Z r b - l . - 5 . r 4* l o x * 2
f (t) =
0
JT
ie
le
re
S-
ic
v.
),
a
Use
arootlocationtechniqueto dctt'rminc(hemaxirnumof
this
function.
Perlbrmiterationsuntil the approximaterelativeenorialls below 5%,.If you usc a br'ackctingmcthod,
use initial guessesof x1 - 0 and -r, : 1. If you use the
Newton-Raphsonor the modified sccantnrethod,use an initial guessof;r; : l. Ifyou usethe sccantmethod,useinitial
guessesof x; I : 0 and Xi : l. Assumingthxt convergenceis not an issue,choosethe techniquethat is bestsuited
to this problem.Justify your choice.
6.211You must determinethe root of the following easily
di fferentiablef unction:
n <,.
?"-^ =
)-l-r
justify your choice,and
Pick the bestnunrcricaltcchnique,
use
technique
determine
then
thal
to
the root.Note that it is
known thatfi:rrpositivcinitial guesscs,
except
all techniques
fixed-point iterationwill cvcntually converge.Perfbrm iterrelativeerror fallsbclow 2olc.If
ationsuntil the approximate
you use a bracketingmethod,usc initial guessesof r1 : [)
and.r,,: 2. If you usethc Newton-Raphson
or the modified
secantmcthod,use an initial guessof .r; - 0.7. If you usc
thc secantmethod, Lrseinitial guesscsof ,r; 1 :0 and
Opti mizqtion
CHAPTER
OBJECTIVES
The primaryobjectiveof the presentchapteris to introduceyou to how optinrization
can be usedtcldetermineminirrraand rnirximaof bothone-dimensional
and
multidimensional
functions.Specificobjectivesand topicscoveredare
'
t
'
'
'
'
'
'
'
t
Understanding
why and whereoptirnizafionoccursin engineering
and scientific
problemsolving.
Recognizingthe differencebetwecnone-dimensional
and multidimensional
optirnization.
D i s t i n g u i s h i nbge t w c e ng l o b a la n dl o c a lo p t i m u .
problemso thatit can bc solvedwith a
Knowing how to recasta nraximization
r n i n i r n i z i nagl g o r i t h n r .
Bcing ableto definethe goldenrati<land understand
why it makesonedinrensionaloptitnizatione'flicient.
Localingthe optinrumclf a single-vnriahlc
functionwith thegolden-section
search.
Locatingthe optirnumof a singlc-variable
f'unctionwith parabolicinterpolation.
Knowing how to apply the f mi n brid f unctionto deterrnine
the minimumof a
l'u
l n ci to r t .
o n e - driu e n s i o r t a
Beingableto developMATLAB contourand surlhceplotsto visualizetwodimensionalfunctions.
Knowing how tclapply the f minse.rrch lirnctionto clctermine
the minimumof a
m u l t i d i n t e n s i o nIa' ul n c t i o n .
7.1
YOU'VEGOT A PROBLEM
jurrper can be projectedupwarrdat a specifiedvelocity.
n object like a bLrngee
If
is subjectto Iineardrag,its altitudeas a functionof time can be computed
as
t66
t--
-ry,
* T) tr - e I(tn'I)t)
Z: zr* 1 (,,.,
I
(
f
I
(,
Z,I INTRODUCTION
AND BACKGROUND
Maximum
elevation
F'GURE7.1
E l e v a l t a no t q f u l l a l i ; t r ' o
t f t i m e f o r o n o b f t r c l i r r r i l o / / i p r o f e c l e t l u S t , . " , ' o ,rcvli l h i r n i r t t r o l r t , - l r tr<t t /
w h e r e : : a l l i t u c l (er n )a h i l v ct h cc a r t h ' ss u d : r c e( d c t ] n c da s : : 0 ) . l o : t h c i n i t i a lt l t i t u c l e
( m ) ,l r : r n i l s s( k g ) ,( : a l i n c a r d r a gc o e l ' l ' i c i c(nktg / s ) ,q r : i n i t i ' r lv c l o c i t y( r n / s )a, n dI :
tinrc(s). No(c that (irr tlris liurnulation"positivcvckrcityis corrsidcrcd
to bc in thc rrpwlrci
d i r c c t i o n . G i v c n t h e l b l l o w i n g p a r a r n c l c r v,t\l:u9c. s1 :{ l r r . r / s r . - ':, , l O 0 m , 1 1: r- 5 . 5 r n / r ,
r i : t l Ok g , a n d r ' : l 5 k g / s ,E c 1(.7 .I ) c a n b c u s c dt o c u l c u l a t ct h c j r r r n pre' s a l t i t u c l cA. s
d i s p l a y c d i n F i g . T . l , t h c j u r n p c r r i s c s t o a p c a k c l c v a t i o n1o9l '0i rnbroaut al h o u t l : - 1s .
S u p p o s tch a ty o u a r cg i v c nt h c - j o ho l ' d c t c r r r i n i ntgh ce x l c t l i r n eo l ' t h cp c a kc l c v a l i o n .
T h c d e t c r m i n a t i oonl ' s u c hc x l r c m cv a l u e si s r c l c r r c dt o l s o p l i r n i z a t i o nT.h i s c h a p t c rw i l l
introduccy()uto how the cornputcris uscdto nrakcsuchdetcrn'riniltions.
7 . 1 INTRODUCTION
AND BACKGROUND
In thc nrostgcneralscnsc,optinri:/.ati()rl
is thc ploccsso1'crcatirrgsonrcthingthat is as
c f f b c t i v ca s p o s s i b l eA. s c r r g i u c e r w
t o n t i n u < l u sdl cy s i g nd c v i c c sa n c pl r o d u c t tsh a (
s .c n r u s e
p e r l i r r mt a s k si n a n c f f i c i c n lI a s h i o nl i l r t h c l c a s tc o s t .T h L r sc, n g i n c e r sa r c a l w a y sc o n frontingoptirnizittionpnrblerls that uttcnrptto balarrccpellirrnranccand linritatiorrs.In
adclition.
scientistsIravcintcreslirr optirnalphenorncna
firlnr thc pcukclcvationo1'
rurnging
projectilesto the minirnunrli'eccncrgy.
perspective.
Frour a nrathcnraticirl
optinriz.ation
dcirlswith l'indingthe nraxinraand
rninimaof a l'unctiorr
that dcpendson one or m()revariablcs.The goal is to detcrnlincthe
valuesof tlre variablesthat yield rnaximaor minirnalbr the I'unction.Thcsecan then be
substituted
back into thc functionto conrputcits optinralvalucs.
A l t h o u g ht h e s c s o l u t i o n sc a n s o r n c t i r n cbsc o b t a i n c da n a l y t i c a l l yn, r o s tp r a c t i c a l
optimizationproblenrsrequirc nurnerical,cornputcrsolutions.From a numcricalstandpoint, optirniz.atit'rn
is sinrilar in spirit to lhe root locationrncthodswc just covercdin
Chaps.5 and6. That is. both involveguessinganclsearching
tilr a pointon a function.The
fundarne'ntal
dif'lbrcnccbetweenthc two typesol'problernsis illustratedin Fig.7.2. Root
Iocationinvolvessearchingfor thc locationwherethe I'unctioncqualszcro. ln contrast,
involvessearchingfor the function'scxtrenrep<lints.
optimiz-ation
168
OPTIMIZATION
T
m
l'('t) : o
l"(r) < o
l(x) : o
/'("t) = o
>
"f"(,r) 0
TT
tht
gri
F I G U R E7 . 2
A f u n c t i oonf o s i n g l ev o r i o b l iel l u s t r c f itnhqed i f f e r e n cbee l w e e n
r o o f so n do p l r r n o
As can be seenin Fig. 7.2,theoptirnunrs
arethepointswhcrethccurveis flat.In mathenrirticalterms,this correspondsto the .r value whcre thc derivative / '(.r) is equalt0 zelo.
Additionally,thc secondderivative,.1"(,r), indicateswhethertheoptimurnis a rninimumor
a maximum:il' .t'"(x) < 0, the point is a maximLlm;if .l'"(.r) > 0, the pointis a minimun
Now, understanding
thc relationship
betwecnrootsandoptimawouldsuggest
a posible strategyfor finding the latter.That is, you can diff-crentiate
thc functionandlocateth
root (thatis, the zero)of the ncw tunction.ln fhct.someoptirnizationrnethods
dojustthis
by solvingthe root problenr:.f'(.r1: 11.
E X A M P L EZ . l
sat
AS
t__
ofr
on(
prc
sea
me
D e t e r m i n i n gt h e O p t i m u m A n o l y t i c o l l yb y R o o tL o c o t i o n
Problem Stotement. Deterrninethe tirnc and magnitudeil1'thepeakelevation
basedon
E q . 0 . 1 ) . U s e t h e l b l l o w i n g p a r a n r e t evr a l u e sf o r y o u r c a l c u l a l i o n.:g : 9 . 8 1 m / s t ,
;o : 100m, u0 - -55m/s, nt : u0 kg, andr: : l-5kg/c.
*
{oJ(
PqU
S o l u t i o n . E q u a t i o n( 7 . 1 )c a nb e d i l f ' e r e n t i a t e
t odg i v e
rh;
ltou
dz
n
:
\ ( / t t t) t -
D'e
tn8
(;
(l -
e-
i'l"'t';
Note that because1)- Ll-.ldt, this is actLrally
thc ccluation
fbr the vclocity.Themaximun
elevationoccursat the value ol I that drives this gquationto zero.Thus,the problen
anrountsto deterrnining
the root.Forthis case.this can be accomplished
by settingthedcrivativeto zeroand solvingEq. (E7.L l.) analyticallylbr
r :'!rn (r + !a )
tng/
r'
\
r ivcs
S u b s t i t u t i n gt h e p i l r i i r n e l e r g
,:
l5ts5t
8o
r n/ r r
\ :3.8.r166s
80r9.81)/
l.s \
min
INTRODUCTION
AND BACKGROUND
This value along with the parameterscan then be substitutedinto Eq. (7.1) to computethe
maximum elevation as
z:
80 /
8 0 r QR l r \
8 0 r, 9= ."8, ,1r r- r . 8 3 t 6 6 ) :
1 0 0 + : ; | 5 0 + " " . ; ; " , , ) ( t _ . _ , 1 58 0 , 3 8 3 1 6 "6"1, _
1 9 2 . g 6 0m9
15\
rs /'
ls
We can verify that the resultis a maximum by differentiatingEq. (E7.1.1)to obtainthe
secondderivative
d2z
-;;:
(ll-
c
- - u t ) €- . t ,/ , , t t - , n - ( t-/(' .' t/t, n \ t "ntlt
ln
- m
- 9^. 8-1 ;
The fhct that the secondderivativeis negativetells us that we have a maximum. Further,
the result makesphysicalsensesincethe accelerationshouldbe solely equalto the force of
gravity at the maximum when the vertical velocity (and hencedrag) is zero.
Althoughan analyticalsolutionwaspossiblefbr this case,we couldhaveobtainedthe
sameresultusingthe root locationmethodsdescribedin Chaps.5 and 6. This will be left
as a homeworkexercise.
Althoughit is certainlypossibleto approachoptimizationas a rootsproblem,a variety
of directnumericaloptimizationmethodsareavailable.
Thesemethodsareavailablefbr both
problems.As the name implies,one-dimensional
one-dimensional
and multidimensiontrl
problemsinvolvefunctionsthertdependon a singledependent
variable.As in Fig. L3a, the
searchthenconsistsof climbingor desccnding
peaksandvalleys.Multidione-dimensional
mensionalproblemsinvolve lunctionsthat dependon two or more dependentvariables.
FIGURE
7.3
( o )C n e ' d i m e n s i o n o lp t i m i z o t i o nT h i sf l g u r eo l s o i l l u s t r o t ehso w m i n i m i z c i i oonf 1 1 l r 1i s
i ot t h e m o r i m i z o l i o n
equivcrlen
o f - . / ( , r ) ( b )T w o - d i m e n s i o noopl l i m i z o t i o nN. o t e t h o tt h i s
l r g u r ec o n b e t o l e n t o r e p r e s e nei i t h e ro m o x i m i z o i i o(nc o n t o u risn c r e o s ei n e l e v o l i o nu p t o
lhe moximumiike o mountcrin)
or c minimizotionicontoursdecreosein eJevotion
down to the
m i n i m u ml i k eo v o l l e v )
Optimum/ir'..u'r
Minimum.l("r)
r70
OPTIMIZATION
7.
ln the samespirit,a two-dimensional
optimizalioncan againbe visualizedassearchingout
peirksand valleys(Fig.7 .3b).However,just as in realhiking, we arenot constrained
towalk
a singledirection;insteadthe topographyis examinedto efTicientlyreachthegoal.
Finally,the processof findrng a maximum versusfinding a minimumis essentially
identicalbecausethe samevalue x* both minimizes/(x) and maximizes-/(,r). This
equivalenceis illustratedgraphicallyfor a one-dirnensional
ftrnctionin Fi_c.
7.3d.
In the next section,we will describesome of the mole common approaches
for onedimensionaloptimization.Then we will providea brief descriptionof how MATLABcan
be employedto determineoptimafor multidinrensional
functions.
7.2 ONE-DIMENSIONALOPTIMIZATION
This sectionwill describetechniques
to find the mrnrmnmor maxrmumof a function
ofa
s i n g l e v a r i a b l e . l ( . . ) . A u s e f u l i m a g e i n t h i s r e g a r di s t h e o n e - d i n r e n s i o n" raol l l e r
coaster"-like lirnction dcpicteclin Fig. 7.4. Recall liorn Clraps.-5ancl6 that root location
was complicatedby the fact that severalrootscan occur tbr a singlefunction.Similarly,
h o t hl o c a lu n dg l o h u lo p t i m i rc u r o c c u ri n t r p t i n r i z a t i o n .
A gkhul olttittrtrmreprescntsthe very best solution.A local optinrunr,thoughnotth
very best.is betterthunits inrmecliate
neighbols.Casesthat includelocaloptimaarecalld
multimodol.lnsuchcases,we will almostalwaysbe intercstcd
in findingtheglobaloptimunl
In addition,wc mustbe concerned
aboutmistakinga localrcsulttor theglobaloptimum.
Justas in rool location.optimizrtionin one dimensioncan be dividc'dintobracketing
andoperrmethods.As dcscribedin thenextsection,thc golden-section
search
is anexamplc
of a bracketingnrethodthatis very sirnilarin spiritto thc hisectionnlethodlbrrootlocation
This is firllowcdby a somcwhatmoresophisticated
inter.
brackctingapproach-parabolic
polation.We will then show how thcsctwo nrethodsarecornbinedand irnplemented
wit
M A T L A B ' s I r ni n b n d f u n c t i o n .
FIGURE 7.4
A {unctionthirl osymptoticolly
opproocheszero ot p us ond minLrs
oc ond hcrstrvo moximum
ond
two rninimum
p o n l s i n t h - -v i c i n i t yo f l h e o r i q i n .T h e l w o p o i r l s1 ot h - -r i g h tr r r e o c o o p t i m o ,
w h e r e c sf h et w o t o t h e l e f to r e q o b o l .
7.
In
aft
the
pr(
ofi
the
lett
rolr
Euc
whe
Mul
wher
q
'l
In ad
provir
termir
T
roots i
guess
tween
The rc
xr
Global
maxtmum
FIGUR
Euclid
s
rotioof
smoile
Global
F-
1-
2.2 ONE.DIMENSIONAL
OPTIMIZATION
l7l
7.2.1 Golden-SectionSeorch
In manycultures,certainnumbersareascribedrnagicaiqualities.For example,we in theWest
quantities.
areall familiarwith "lucky 7" and "Fridaythe 13th."Beyondsuchsuperstitious
thereareseveralwell-known numbersthat havesuchinterestingandpowerful mathematical
propertiesthat they could truly be called"rnagical."The mostcomnlonof thesearethe ratio
of a circle'scircumferenceto its diameterz and the baseof the natLlrallogarithme.
Although not as widely known, the golden ratio shouldsurelybe includedin the pantheonof remarkablenumbers.This quantity,which is typicallyreprescnted
by the Greek
leuer Q (pronounced;fee), was originally definedby ELrclid(ca. 300 BCE) becauseof its
role in the constructionof the pentagramor five-pointedstar.As depictedin Fig. 7.-5.
Euclid'sdefinitionreads:"A straightIine is saidto havebeencut in extremeandmeanratio
when,as the whole line is to the greatersegment,so is the greaterto the lesser."
The actr"ral
valueof the golclenratiocan be derivedby expressing
Euclicl'sdefinitionas
t1IL2
Ir
lr
(':
(7.2)
Multiplying by t 1/(.2and collectingtermsyields
(7.3)
o'-Q-I:0
where@ : L r / ( z. The positiveroot of this equationis the goldenlatit'r
4,:
l+\,6
:
(7.1)
1 . 6t 8 0 . r - r 9 8 8 7 4 9 8. 9 .
pleasingin Westerncultures.
The goldenratio has long beenconsideredaesthctically
In addition,it arisesin a varietyof ilthcr contextsincludingbiology.For our pulposes,it
providesthe basisfbr thc golden-sectiorl
rncthoclfbl dcscarch,a sirnple,general-purpose
function.
terminingthe optimumof a single-variablc
searchis sirnilarin spirit to the'bisectionapproachlbl locating
The golden-section
by a lorver
rootsin Chap.-5.Recallthat bisectionhingedon definingan interval,specil'ied
guess(-r,)and an upperguess({,) that bracketeda singleroot.The prcsenceof a root bethat.l'(.r,)and.l(.r,,)had difl'ercntsigns.
tweenthesebounclswas veriflcclby deterrninimg
The root was thcn estimatedas the midoointof this intcrval:
'.,
'tt I x,,
(7.51
FIGURE7.5
so lhot fhe
Euclidsdefinitionof the golden roiio is bo-sedon dividing o iine i.rfolwo segmc-nls
roiio of ihe whole line io the lorgersegmentis eguol lo the roiroof the lcrrgersegrnenlio ihe
smo//ersegmeni Ihis roiio is co//edthe go/den rofio
(t
,
t-+l-,
_
(2-
172
OPTIMIZATION
The final stepin a bisectioniteratjoninvolveddetennininga new smallerbracket.Thiswas
done by replacingwhicheverof the bounds-r1or-r,/had a functiorrvalue with the samesign
as-l(r,.).A kev advantageof this approachwas that the new valner, replacedoneof theold
bounds.
Now supposethat insteadof a root. we were interestedin detenniningthe minimurn
of
function.As with bisection,we can startby dcfining an intervalthat
a one-dimensional
andhence
containsa singleanswer.That is, the intervalshouldcontaina singlenrinirnurrr.
as for bisection.where.l,and4
is calleduninrodal.We can adoptthe samenomenclature
definedthe lower and upperbounds,respcctively.
of suchan interval.However,in conlrasl
to bisection,we needa new strategyfor findinga rninimumwithin the interval.Ratherthan
value(which is sufflcientto detecta sign change,andhencea
usinga singleintermediate
zero), we would need two intermediatcfunction valuesto detcct whcthera minimum
occun'ed.
points,
The key to makingthis approachetficicntis thc rvisechoiceof the intermediate
As in bisection,the goal is to nrininrizelirrrctionevaluations
by rcplacingold valueswith
nerv values.For bisection.this was accornplished
by choosingthe midpoint.Forthe
golclen-section
sc'arch,the two intcrnediirtepoints are chosenaccordingto the golden
ratio:
.Yt:.\/ +d
(76)
.r):xu-d
(1.1)
where
d : ( ( h - l ) ( : r , -, , r r )
(7.81
Thc functionis evaluatcdat thcsetwo inleriolnoints.Two resultscan occur:
l.
)
I t ' , a s i n F i g . 7 . 6 a , . l ( x.rll( - r : ) , t h e n . l ( . r , ) i s t h c r . n i n i n r u n r , a n d t h e d o m a i n o f . r t o t h e
For
leli cll'x,,liorn -r,to r), clurbe clintinatcdbecauscit doesnot containthc rninirnum.
the new .r,lirr thc next round.
this casc,.r, bscclmes
If I ( v:) . ./ (,rr), then/ (.r,)is the rrinimum andthc donrainol' r to thc rightof .r,,fiom
.rr to ,r,,would bc eliminatcd.Fclrthis case,.\r bccotnesthc new .r,,lirr thc ncxtround.
Now. hereis thc neirlhcnelit {l'clmthc uscol'thc goldenratio.Becallscthe oliginal.r,
and r, u,crechosenusingthe goldenlatio, we do not havc to recalculatcall thc function
valuesfor thc next iteration.For example.tbr thc caseillustratedin Fig.7.6,theold"r,becomesthe new rr. This meirnsthat we alrcadyhavethe valuelbr the new.l(r.), sinceitis
the sameas the l'unctionvalueat the old.r,.
To cornpletethc algorithrn,we needonly detcrminethe new.r,. This is donewith
Eq. (7.6) with r/cornputedwith Eq. (7.8) basedon the new valueso1',r,rud.r,,.Asimila
approachwould lre useclfbr thc alternatecascwherethe optirlLrnrf-cllin the leli subinterval.
For this case.the new.rrwould be computedwith Eq. (7.7).
rapidly.
As the iterationsarerepeated,
the intervalcontainingthe extremumis reduced
In fact,eachroundthe intervalis reducedby a 1'actor
of d - I (about61.87c).Thatnleans
that after l0 rounds,the intervalis shrunkto about0.6l8r{)or 0.008or 0.8% of itsinitiai
length.Afier 20 rounds,it is about0.0066%. This is not quite as good as the reduction
achicvedwith bisection(50Vo\.birt this is a harclerDroblem.
EXAMPLE
7.2
Gol<
Prob
50lu
2.2 ONE-DIMENSIONAL
OPTIMIZATION
r73
S
n
d
)f
rt
e
f,
rl-d+xl
St
.n
(al
n
S.
rh
le
:n
x2
-t1
t
I
I
New
(b)
ne
or
FIGURE 7.6
(o) The initiclstep of the golden-section
seorcholgorithminvolveschoosingiwo inleriorpornts
occordingto the golden rotio (b) The secondstep involvesdeflnlngo new intervolthot
e n c o m o o s s el n
s eoollmum.
)m
rd.
.rl
on
)eis
EXAMPLE
2.2 G o l d e n - S e c t i o Sn e o r c h
ProblemStotement. Use the golden-section
searchto find the minimum of
1
irh
lar
al.
Llv.
tns
:ial
ron
"f ( x t : - - 2l 0s i n r
within the interval from -r,: 0 to jr,,: 4.
Solution. First,the goldenratio is usedto createthe two interiorpoints:
d : 0.61803(4 - 0) : 2.4'.721
rr :0 +2.4121:2.4121
xz:4-2.4121 :1.5279
174
OPTIMIZATION
The function can be evaluatedat the interior ooints:
l(x:):
1.52192
- 2 s i n ( l. 5 2 1 9 ) : - 1 . 1 6 4 7
l0
I (rr) :
2.412t2
- 2 s i n ( 2 . 4 1 2 1:) - 0 . 6 3 0 0
t0
Because.[(xt1 ..1 (,r1),our best estimateof the minimum at this point is thatit n
focatedat r : | .5219with a valueof ,f (r) : -l .7641.ln addition,we alsoknow thatthe
minimum is in the intervaldefinedby -r,,.rr,and.r,. Thus,for the next iteration.thelower
b o u n dr e n r a i r l s . :t / 0 , a n dx , b e c o n r etsh e u p p e rb o u n d ,t h a ti s ,. r , , : 2 . 1 7 2 1. I n a d d i t i o n ,
the fbrmer-r. valuebecomesthe new .rr, thatis,x, - | .5279.ln adclition,we do nothave
to
recalculate/(x1), it was determinedon the previousiterationas l (.5219) : -1.1641,
All that remainsis to use Eqs.(7.8) and(7.7)to computethe new valueof r/andr,:
d : 0 . 6 1 8 0 3 ( 2 . 1 1 2 1 0- ) :
or
es
1.5219
or
the
r z : 2 . 4 J 2 1 - 1 . 5 2 7 9: 0 . 9 4 4 3
T h e f u n c t i o n e v a l u a t i o n a t ./r(.0i s. 9 9 4 3 ):
l.,53l0.Sincethisvalueislcssthanthe
f u n c t i o nv a l u ea t . r , . t h e m i n i m u mi s l ( 1 . 5 2 1 9 ) : - 1 . 7 6 1 1, a n d i t i s i n t h e i n t c r v apl r e scribedby rr, -r,. and,r,,.The processcan be repcatcd,with the resultstatrulated
hcre:
Th
xt
I
2
3
4
5
6
7
B
0
0
o 9443
4 s443
3050
30,sc)
3050
3 9 0I
J'$,)
0
0
_53r0
5 3t 0
7595
7595
7595
7742
x2
I 5279
o 944'3
5279
3050
5279
4427
3 9 0I
4427
f (xr)
7647
-5310
7647
7595
7647
7755
7742
7755
xt
24721
I 5279
I.BB85
) 5279
1 66-s6
I 5279
I 4427
I 4752
l&)
--0 6300
*1 7647
, t 5432
--17647
t 7136
t 7647
-1 7755
-t 7732
xu
J'(x,,)
40000
:tIt36
-o 6.100
2 4721
(l c,,lOO
2 4721
B8B5
t.5432
BBB-5
) 5432
6656
I 7136
5279
t 7647
-t 7647
5279
a
2 A/21
) 5279
0 9443
0 5836
0 360/
a 2229
0 I3l8
0 0B5r
Note that the current minimum is highlightedtbr cvery iteration.Alier the eighth
i t e r a t i o nt,h e m i n i m u r no c c u r sa t x : 1 . 4 4 2 1 w i t h a l i r n c t i o nv a l u c c t l ' - ] t . 1 1 5 5T. h u s . t h e
r e s u l ti s c c r n v e r g i nogn t h e t r u ev a l u eo f - 1 . 1 1 5 7 a t . r : 1 . 1 2 1 6 .
Recallthat for bisection(Sec.5.4), an exactupperboundfor the crror can be calculatedat eachiteration.Using similarreasoning.
an upperboundfbr golden-section
search
canbe derivedaslbllows:Oncean iterationis complete,theoptimumwill eitherfall inone
of two intervals.lf the optimumfunctionvalueis at,rr, it will be in the lower interval
(.r,,
.r,..r,).If optimumfirnctionvalueis at -r,,it will be in the upperinterval(.r...r,..r,,).Because
the interiorpointsale symmetrical,eithercasecan be usedto definetheerror.
Fit
ap
No
Co
gol
wo
nuI
l.
Z.2 ONE-DIMENSIONAL
OPTIMIZATION
Lookingat the upperinterval(xr,r,, :r,,).if the true valuewereat the fiu'left,the maximum distancefrom the estimatewould be
Ar.:-TI -,r2
: . r t I ( Q - D Q , - r r ) - x , ,+ ( Q - l ) ( x , ,- x i )
: (rr - .t,,)+ 2(0 - l)(.r,, - ,r1)
: (20 - 3)(.r,,- .rr)
or 0.2361(-r,,-.r',). If the true vaiuewereat the far right, the maximumilistancefiorn the
estimatewould be
A,r1, :
:
-{,, -'f
,{rr
I
.t't -
(Q -
l)(x,, - -ri)
: (r,, - rr) - (Q l)(.r,,- .r1)
: ( 2 - d ) ( . r u- . r r )
Therefbre,thiscasewould represent
themaximumerror.This resultcan
or 0.3820(,r,,- ,r1).
thenbe normalizecl
to thc optimalvaluefcrrthat iteraticln
.r,,n,
to yicld
le
t._..1
t , , : ( 2 - O ) l ' : ! - - - - : J lx t 0 0 %
e.sl
I r,'nr I
;
79
This cstimateprovidesa basisfbr tcrminatingthe iterations.
searchfor minimizationis presentcdin
An M-file f'unctionfbr the golden-section
returnsthe locationof the rninimum,the valueof the function,the
Fig.7.7.The l'uncticln
approximate
crror,artclthe numberof iterations.
The M-file can bc uscdto solvethe problenrfrorn Example7.1.
1?
> ) ' 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<4 rsempty(es),
es=0.0001;end
if nargin<5 isempt-y(maxit), maxit=50;end
p h i . ( 1 + s q r r -( 5 ) ) / 2 ;
iter-0;
while(1)
xI);
d = (phi.-1)* (xu
xI = xl r cl;
x2 = xu - d;
if t(x7,\tararqirr{:})
< f (x2,vararcJin{:})
xopt = x1;
x1 = 7);
xopt
-
ul
7.
Pa
pr,
co
ca
SO
x2,.
tic
vl.
end
iter=iter+1;
if xopt---0, ea = (2 - phi) * abs( (xu i-f ea <= es I iter
>= maxit,br:eak, end
end
x-xol';[x.i
x 'pL,'/a'or:in
:] ;
xl)
/
xopt)
*
100;end
F I G U R E7 . 7
A " A / i ; l e 1 3 j p r 5 . r m i n p t h e m r n i m u m o f o l u n c i i o n w i t h t h e o o l d e n - . s e c l i o ns e o r c f r
7
Time-consunringevaluation.For pedagogicalreasons,we use simple functionsil
most of our examples.You shouldunderstandthat a functioncan be very compler
and tinre-consunling
to evaluate.For exarnple,optimizationcan be usedto estimatr
the parameters
of a modelconsistingof a systemo1'diff'erentialequations.Forsucl
cases,fhe "function" involvestime-consumingmodel integration.Any method
thal
m i n i r n i z e s u c he v a l u a t i o nw
s o u l db e a d v a n t a s e o u s .
wl
op
7.3
$(AAAPLE
Po
Pr
Sr
2.2 ONE-DIMENSIONAL
OPTIMIZATION
177
Parabolic
approximation
of maximum
Truemaximum
./(.r) True function
Parabolic
function
F I G U R E7 . 8
G r o p h i c odl- - p i c t i oonf p o r o b oi c i n t e r p o l o t i o n
7.2.2
Porobolic
Interpolotion
Parabolicinterpolationtltkesadvantagcol'the lirct that a sccor.rd-ordcr
polynornialolien
providcsa good approxirnation
to the shapcof l(.r) ncaran optimum(Fig. 7.g).
.fustits lhcrc is tlnll'ttnc sl.raight
linc corrnccting
two points,thereis only 9nc parabola
c o n n e c t i ntgh r e ep o i n t sT
. h u s .i 1 ' w ch a v ct h r c cp o i n t st h a t . y o i n t h
l yr a c k eat n o p t i m u m w
. ccltnl.ita parabolato the points.Thcn wc candil'lcrcntiatcit, sctthc rcsultcqualto zcro,ancl
solvelbr atr estitnaleo1'lhcoptimal-r. It can bc shownlhloughsontcalgcbraicrlanipulat i o n st h a tt h c r c s u l ti s
rl:.rl-
(7.l0l
where-r', -r., ancl,r, iirc thc initial gucsses,
and.rr is the valueo1'.rthat corresporrds
tq the
o p t i r r r u nvta l u cu l ' t h cp a r . a h o l fi ci t t o t h eg u e s s e s .
E X A M P2L. E
3
P o r o b o l i cI n t e r p o l o t i o n
ProblemStotement. Use parabolicinterpolationto approxirnatc
thc rnipirnurnol'
/(r) :
in
Lex
ate
rch
lat
rl
lo
2sin'r
r v i t hi n i t i a lg u e s s eosf , r , - 0 . . r , :
l , a n d _ r . . :- 1 .
S o l u t i o n . T h c l u n c t i o nv a l u e sa t t h c t h r e eg u e s s ecsa n b e e v a l u a t e d :
r
-
r'-l
.
-A
f)
.l(r,):0
l(r,): -1.-s829
/(r',):3.1136
r78
OPTIMIZATION
7.3
into Eq. (7.10)to give
and substituted
r'. -
* 3.11361
- 0 - 4 ) 2[ - r . 5 8 2 9
- 01
l (l - 0)2L-1.5829
2 ( 1 - 0 ) [ - r . 5 8 2 9- 3 . i l 3 6 1- ( l - - + [)- 1 . s 8 2 9- 0 ]
I
whe
func
which frasa functionvalueof /(1.50-55): -1.1691.
Next, a strategysirrrilarto the golden-sectionsearchcan be employedto determine
which point should be discarded.Becausethe function value for the new point is lower
than for the interrnediatepoint (.r:) and the new x value is to the right of the intermediate
point.the lower guess(x,) is discarded.
Therefore,lbr the next iteration:
rr :l
r: : I.-50-5-5
xl: 4
Exar
.li.r'): -1.5829
.l'Q): -1.76e1
./k.):3'1136
r v h i c hc a nb e s u b s t i t u t ei dn t o E q . ( 7 . l 0 ) t o g i v e
ra = 1.50.55
:
I ( r . 5 0 - 5-5 I ) 2[ - I . 7 6 9 t- . ] . 1 l 3 6 l
2 ( 1 . - s 0 5 -r5) t- - 1 . 7 6 9-t 3 . l l 3 6 l - ( t . 5 0 5 s 1 ) l - t . 7 6 9 1
1.4903
which lrasa functionvalueofl( I .4903): -l .1114.The processcan be repeated,
withthc
r-e.suIts
tabulatedher-e:
.rl
I
2
3
4
5
J@,)
0 000cr 0 0000
I0000
r 5829
I0000 ,t 5B2a
r o o o o - t 5829
1 4 2 5 6 - t 7 75 7
x2
0000
5055
4903
4256
4266
f (x2)
r.t
f k,l
- r 5 8 2 9 4 0000
3 r 36
I 7 6 9 1 4 0000
J I I 36
t 7714
5055 - t 7691
-1 7757
4qa:J - t /714
-) 7757
49A3
I 7714
X,
1 .5055
t 4903
I 4256
I 4266
14 2 7 5
"f(rJ
-1 /691
-1 7711
-) 7757
-t 7757
-)7757
Thus, within five iterations,the resultis convergingrapidlyon the true valueof -1.7757
a t . r: 1 . 4 2 J 6 .
7.2.3
Thu
itera
MATTAB Function: rminbnd
Recallthat in Sec.6.4 we describedthe built-in MATLAB functionf zero. Thisfunction
combined severalroot-finding methodsinto a single algorithm that balancedreliability
with efliciency.
The fminbn.f function provides a sin.rilarapproachfor one-dimensionalminimiza.
tion. It combinesthe slow. dependablegolden-sectionsearchwith the faster,butpossibly
unreliable.parabolicinterpolation.It first attemptsparabolicinterpolationandkeeps
ap
plying it as long as acceptable
resultsareobtained.If not,it usesthe golden-section
search
to set mattersin hand.
7.3
MU
Asid
func
a rol
valle
a har
2.3 MULTIDIMENSIONAL
OPTIMIZATION
A simple expressionof its syntaxis
= fminbnd(functian,xl,x2)
fxmrn, fvafl
wherex and f va1 arethe locationand valueof the minimum, function is the nameof the
function being evaluated,and xl and x2 arethe boundsof the interval being searched.
Here is a simple MATLAB sessionthat uses fminbnd to solve the problem from
Example7.l.
> > 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 (':<-2' ) ;
(a) Contour ploL' ) ;,Jrid;
trtle('
subplol \I,2,2 t;
cs=surf c lX,'{,Z);
zmin=f l,ror(min (Z));
z m a x - r - - e iI ( m . r x( Z ) ) ;
x l a b c l { ' y _ 1 ' ) ; : r I a b e l ( ' : ' , . _ ?), '; z l a b e l
title('(b)
M es l - r t r ' l o t ' ) ;
nLtn
on
{fAt
uat
funr
7.i
Sta
mu
dire
han
whr
the
(' f (x_I,:<._2)' ) ;
the
As displayed in lrig. 7.9, both plots indicatc that function has a minimurn value of about
-l and.r, : l.-5.
. f t . r ' , " r 2 ) : 0 t o I l o c a t e da t a b o u t r l :
FIGURE7.9
(o) Confourond (b) rneshplc,lsof o Mo-dimensionolfunctron
Bo
wh
forc
fere
*
lo) |
the
(a)
{b)
7.4 CASESTUDY
t8l
Techniques
for multidimensionalunconstrained
optimizationcan be classifiedin a
numberof ways.For purposesof the presentdiscussion,we will divide them depending
on whetherthey require derivative evaluation.Those that require derivativesare called
gradient,or descent(or ascent),methods.The approaches
that do not requirederivativeevaluationare callednongradient, or direct, methods.As describednext. the built-in MATLAB
functionfniinsearch is a directmethod.
7.3.1 IVIATLAB Functiohs fminsearch
StandardMMLAB has a function fminsearch that can be usedto determinethe minilnum of a rnultidimensionalfunction. It is basedon the Nelder-Meadmethod,which is a
direct-searchmethod that uses <lnly function values (does not require derivatives)and
handlesnon-snrooth
obiectivefunctions.A simpleexpression
of its syntaxis
fxnrr,
fvall
= fminsearch( function,xl,x2)
wherexmrn and frzal arethe locationandvalueof theminimum,fttnction is thenameof
the tunctionbeing evaluated,and xl and x2 are the boundsof the intervalbeing searched.
Hereis a simpleMATLAB sessionthat usesf rninsearch to determinerninimumfor
thefunctionwe just graphedin Example7.4:
: . > f = @( x )
2+x\1 ) -x(2) +2*xlI)
-1. OO0O
=
^ 2 + 2 * x ( l * x ( 2 ) + x ( t : , )^ 2 ;
)
1.5001)
f v e ri
0.7500
ENERGY
EAUILIBRIUM
AND MINIMUM POTENTIAL
Bockground.
As in Fig. 7.70a,an unloadedspring can be attachedto a wall mount.
When a horizontalforce is applied,the spring stretches.The displacementis relatedto the
force by Hookes law, F : kx. The potential energyof the deformed stateconsistsof the differencebetweenthe strainenergyofthe springand the work doneby the force:
PE(i :0.5kx2 * Fx
(7.11)
7.IO
FIGURE
forcesiretches
spring
oitoched
to o wollmount.
of o horizoniol
{b}Applicotion
{o)An unlooded
isdescribed
thespring
forceonddisplocement
by Hookes
low
wheretherelotionship
beiween
OPTIMIZATION
it,I+:l.S-,1,,i
l
,t
'i.,,|.
continued
Equation(7.11)definesa parabola.Sincethe potentialenergywill beat a
equilibrium, the solution for displacementcan be viewed as a one-dimensional
tion problem. Becausethis equation is so easy to differentiate,we can solvefor
p l a c e m e nat s . r : F / k . F o r e x a m p l ei,f k : 2 N / c m a n d F : 5 N , r = 5 N / ( 2
2.5 cm.
A more interestingtwo-dimensionalcaseis shown in Fig. 7.11.In this
two
are
degreesof freedom in that the system can move both horizontallyand
In the same way that we approachedthe one-dimensionalsystem,the equilibrium
mationsare the valuesof x, and x, that minimize the potentialenergy:
5€
The
12
^_l2
+ (L,,- x ) 2 * L")
+ o.skb(
Thu
loca
If the parametersare ko: 9 N/cm, k, : 2 N/cm, Lo: 70 cnt Lo: l0 cm,F, = 2
Fz = 4 N, use MATLAB to solve for the displacementsand the potentialenergy.
7.I I
FIGURE
(o)unlooded
A 1"wo-spring
system:
ond(bllocded.
PROBTEM
three iterations
the root of Eq. t
Example7.1 along
the formula
=-x2*8x-12
the maximum
this function analytir
thatEq. (7.10)yi
guesses
ofr, - 0,
the following f
=3+6.r+5x2+3
6e minimum by findi
Use bisection r
-1.5x6+2r4+1
fiurction.
methods to
fu dl values of r.
PROBLEMS
continued
Solution.
An M-file can be developedto hold the potentialenergyfunction:
p = P H ( x , k a , l . : b ,L a , L b , F ' I , l -2 )
function
^ . 1) - L a ' 2 ;
P E a =0 . 5 * k a * ( s q r t ( x ( 1 ) ^ 2 + ( L a - r ( 2 ) )
)
^
2
'
2
+
(
s
q
r
t
(
x
(
l
,
b
+
x
( . 1)
PEb=0.5*kb*
\'2))
) Lb)'l;
W - F l * x ( 1 ) + F 2* x ( 2 ) ;
p - P F l a +P E b- W ;
The solution can be obtained with the fminsearch
function:
k a = 9 ; k b - 2 ; L a = 1 0 ; t , l ; =i 0 ; F 1 , ,: , ;. E " t , - , ]
I x , f ] - f r n i n s e a r . c h ( B P E , i - " 0 .! r , 0 . 5 l
4 .9\2 i
I .2'.0
l 9
f-
- 9 . (,42,:
Thus, at equilibrium,the potentialenergyis -9.6422 N.cm. The connectingpoint is
located4.9523 cm to the right and 1.2'759cm aboveits original position.
PROBtEMS
(c) Dil-l-erentiatc'
the lirnction and then Lrsea root-location
methodIo solve lor the maxirlurn.l(.r)and the correspondingvalueul'r.
7.5 Solve for the value of -xthat rnuximizes.f(,r) in Prob.7.4
search.Employ initial guessesof
using the golden-section
12
J ( r=) - r r + 8 r
,ti : 0 and-r,,: 2, and perfbrrnthree iterations.
themaxinrurnand the colrespondingvalueol' 7.(r RepeatProb. 7.5, except use parabolicintcrpolation.
Delermine
ly ( i.e..usingdill-erentiation). Enrplo-v
rforlhisfunctirxanalvtical
initialguessesof ,r) : 0.,r, : I. andxr : 2. rnd perVedfy
thatEq. (7.10)yields the sanreresultsbasedon lbrm threeiterations.
of,t, : fl, .v. - 1. xp1l.v..: $.
initialguesses
7.7 Enploy the l'o|l<'tringnlethodsto t'ind the nrarirnum olthelbllowinglunction:
Consider
I ( r ) : 4 x - l . u x 2* 1 . 2 - 1 3 0 . 3 - r a
'(r)
r
= 3 { 6-r* -5-r
+ 3xr +,l-ra
(a) Golden-section
search(.rr: -2, -r, : 4, 8, : l(n.r.
by findingthe root ol'the derivativeol'
thenrinimunr
(b) Parabolicinterpolation(,tr : 1.7-5,r'z : 2. ir : 2.-5,
2
Usebisectionwith initial [uessesof -r,:
function.
iterations: -5).
r r =l '
7.ll Considerthe fbllowing function:
Given
r 1 y=)- 1 . 5 - t n
.f (xl : ro + 2-tr + 8x2 + 5r
+2x" +12l
threeiterationsof the Newton-Riiphsonmethod
Perform
valfherootof Eq. (E7.| .l). Usethe parameter
E x a m p7l e. 1a l o n gw i t h a n i n i t i a lg u e s so f r : 3 s
thefonnula
Given
thefunction.
Plot
methodsto prove that the function is conUseanalytical
forall valuesof -r.
cave
Use analyticaland graphicalmethodsto show the lirnction
has a minirnum fbr some value ol' x in the range
*2 < x < l.
r84
OPTIMIZATION
7,9 Employ the fbllowing mcthodsto fincl the minitnurn of
thc function fiorn Prob. 7.8:
(a) Golden-section
search(,r,: -2, -v,,: I, r. -- lc/c).
(b) Parabolicinterpolation(,rr : -2, -y:: -1. .r., : l,
iterations:5).
thcfbllowingtr.rnction:
7.1{)Considcr
3
.l (.r) : 2.r * -
P
constant: 9.8 I rn/s:.attd,t'n- the initial height(rn).Usethe
golclen-sectionsearch to determine the nraximum height
givcn -t'u: I nt, u' : 25 rn/s,and0,,: 50". Iterateuntilthe
approxirnate
ertor falls below €,: lc/ousinginitialguesses
of ,r,: g and-r,,: $Qp.
7.16 The dcflection of a unifbrm bearnsubjectto a linearly
distributedloadcan be computedas
increasing
0.4
I
( d - 1 )o . z
0
J
,= _5(-ys+2L)r'-Lrrl
Perform IO itcrationsof parabolicinterpolationto locate
I)()EIL
the minimum.Commenton tlreconvclgcnceof yttur rcsults
GiverrthatL: 600 crn,C = 50,000kN/cnr:,1: 30,000cm{,
(.rr: 0.1. ,r: : 0.-5,-rr : 5)
7.ll Devclopan M-filc that is expresslydcsignedto locate ahd ur,,: 2.5 kN/cm, detcrtnincthe point of maximumde(b) usingthe golden-scction
sealch
scarch.[n othcr words, {lection(a) graphically',
a rnaxirnumwith thc golden-scction
sct if up so that it dircctly finds the traxitnum ratherthan until tlie approximatcerror falls below e, : l% with initial
finding thc rninimunrof -/'(.r). Thc subroutineshouldhavc guesscsof -r,: 0 ancl.r,, L.
7.17 A ob.icctwith a ntassof 100 kg is prolectedupward
thc tblloia,ingf'eaturcs:
liom tlre surluccol'thc carth at a vclocity of 50 rn/s.[f the
.
Iteratcuntil thc rclativcclror talls bclow a sk)ppingcri- ob.icctis subjcctto lincaldrag(r' : l5 kg/s),usethegoldentcrionor cxcecdsa ntaxinruntnunrbclol'itcration..
scctionscarchto detcrrnincthc rnaxirnumheighttheobjeo
.
Rcturnboth thc optinral"t and./(.r).
attains.
curvedefinedby
is a bcll-shapcd
Test y()ur prograrnwith thc sarncproblctnas Exanrplc7.1. 7.Itl The nornralclistribution
FIGUREP7.22
lne,specificgrow,h .sls
.
onlibioiic versusthe fooo
t
7.23 A compounclA will I
tank reactor. The product
B
separatlon unit. Unreacted
processengineer has fbund
is a function of the convers
will result in the lowest
cos
constant.
7.12 Dcvclop arr M-f ilc to krcittc l ntinimum with thc
goldcn-scction
search.Ralhcrthanusingthc Inaxitrtuuitcrations und L,q. (7.9) as thc stoppingcritcria,dctcnninc(hc
of
scarchto clc(crttrirrc
the location
Usc thc goldcn-scction
numbcrof itcralionsnccdcdt()attaina dcsircdtolcrance.'fcst the inflcctionpoint of this curvc firr positivc.r.
y o u r f u n c t i o nb y s o l v i n gE x r n t p l c7 . 2 u s i n gL ' , , ,: 0 . ( X X ) 1 . 7 . 1 9 l J s c t h c f r l i n r ; r r , r I r : l rl u n c t i o n t o d c t c r m i n teh e
7.1-1t)cvelop an M-filc (o irnplcntcntparabolicintcrpoltl- rlinimrnl ot'
tion (o krcatca nrininturn.Thc subroutincshouldhavc thc
/ ( - t .. r , ): 2 . v ) , 2 . 2 5 . r . l '- 1 . 7 - 5+, r .1 . 5 - r 2
lirllowingl-caturcs:
the
r t'lr ltlnction to detcrrnine
7.20 Usc tllc f nriri,i{r.r
.
andhavcthc progllut gcuBascit on tu,oinitialgucsscs,
ofl.ttitxirtrunr
cratcthc third initiirlvalucat thc rnidpoint()f thc intcrlal.
.
Check whethclthc gtrcsscsbrackctit tlaxitrturn.Il ntlt,
/ 1 , r . . r ' ): 4 - r * 2 . r ' - 1 - - 1 . 12 . r r + 2 - r r . ' - 3 r 2
but
thc subroutincshouldnot inrplcntcntthc algorithrn,
7 . 2 1 C i v e nt l r cl i r l l o r v i n gl u n c t i o n :
shouldrcturnirncrr()rnrcssagc.
.
Itcratcuntil thc rclativccrror lllls bckrrva stoppinScri./ (,r. .r'): -tl-r * -rl + l2r' + 4r'l 2,rr'
tcriorior cxcccdsl rnaxintuntttutnhcltll'itcrationr.
.
Rcturnboth thc optinral,r and./(,r).
Dctcnnine thc rninirnuttr(a) glaphically.(b) nurnetically
theresult
with (hc lrLj rr;r,.Li,:li l'unctitln.ancl(c) suhstittrte
Tcst 1,pprpfogramrvith thc samcptoblctrtas Exanrplc7.3.
(b) back into thc l'unutionio clcternrinctlic rninimum
ol'
arc takcn at ccrtain poiltts
7.14 PrcssurcmcasurL'nlcnts
(.r, i').
bchind an airlbil ovcr tiuic. Thc data bcst l'its thc curvc I
7.22
Thc spccilic gt'owthratc of a vcast that producesan
_r: 6 cos-r I.5 sin.t l'rom.r: 0 to 6 s. Llsclilur iterations
(.:
is a lirnctionol'thc lixld conccntr.ttiot'l
antibiotic
rncthodto l'indthc nrininrunrprcssurc.
o1'thcgoldcn-search
Sctr,:2and,r,,:'1.
2t
with
7.15 Thc trajcctoryo1'aball can bc cot.nputcd
4+0.8r+(l+0.2(]
r''*rir
r : ( t i u l H { ) ) . -t - = j ,
2t ,j crrs'11"
As dcpictedin Fig. P7.22,growth gocs to zc() at veryl0$
conccntrations
clucto lcxrdlirnitation.It alsogoesto zelo8l
due to toxicitycitects.Findthevalueof
whcre ,r': the height(m). d,, : thc initial angle(radiansl, high conccntri.itions
u. : the initial vclocity (rn/s), .g : thc gravitational r u ( r l r i eh g r o ut h i s u r t r : r x i n r u m .
0
cosr:.
f(,,___
7.24 A finite-element mode
loading and monrenrsrF.ig
f(x,y)--5.r2-5xy*2
where-r : end displacernen
v a l u e so f r . a n d
1 . t h a tn t i n u n
7.25 The Streeter-phelp.t mo,
dissolvedoxygen .on."ntrrn,
chargeof sewage (Fig. p7.25
o: o.r- ;*!L
p
k a * k . - k , ,.
S/,
-;(t
- e - ^ , ,, ,
I
r---r__'
I
FIGUREP7.24
A contilever
beom
r85
PROBLEMS
rt
)S
0,4
8
ld_1)0.2
(mg/L)
ly
or'
0
0510
c (mg/L)
lc.
14,
ch
ial
rrd
.he
:n-
by
rof
the
IIGURE
P7.22
FIGURE
P7.25
Thespecific
growthrcle of o yeosf thot producescn
onlibiol
c vefsus
the food concentrotion.
A dissolvedcxygen "sog" below o point dischorgeof
sewoge Inroo rver.
whcre o : dissolvedoxygen conccnlration(mg/L). o, :
(mg/L),r : lraveltirne(d),
oxygensaturatiorl
conccntrrti()n
L,, : biochemicaloxygendenrand(BOD) concentralion
irt
thc rnixing point (rng/L), kz : rate ol'decompositionol'
d I ) , f r ,:, r e a e r a t i o n
B O D ( d r ) .t , : r a t e o l ' s e t t l i n g o l ' B O( D
r).
ratL'(d arrd5,,: sediurent
oxygendemand(mg/L/d).
As indicatcdin Fig. P7.2-5,F.r1.(P7.25)producesln
levela,, srrme
oxygen"sag" thal reachesa criticalr.nininrunr
l,
I
r""
r |r""l
traveltirnet, bclow thc point discharge.
This point is called
'
6
(
'
l
Cost=clt-l
I
\.\1,r
"cri(icirl"hecauseit rcpresents
the lrrcationwherebiotuthat
L\{l-.rl)-/
I
(likc I'ish)would be thc most stressed.
depend
on
oxygcn
rnodelof a cantileverbearnsubjectto
?J4A finite-element
r:ivcn
(Fig. P7.24)is civen by optinrizing Deternrinethc critical tlavcl tirrre'and concentriltiun,
hoding
andmonrents
t h c l b l l o w i n gv a l u e s :
rl = .r.f - ).r.\'+ i..).\'- .\' - | ..\.\'
J{.r.
( , ,: 0 . 6 t l I
r , ,:, l ( ) u r g / L
k,r:0. t d |
7J3A compound
A will be converted into B in a stirred
hnkreactor.
TheproductB and unreactedA ure-purified in a
loparation
unit. UnreactedA is recycled to thc reactor.A
pocess
engineer
hasfoundthat the initial costol'thc syslcm
ilafunction
ol'theconversionr.,. Find the conversionthat
rillresult
in thelowestc()stsystenr.C is a proportionality
on$ant.
r = enddisplacenrent
and .y : end mol.nent.Find the
of.randl thatrninimizc11.r,r').
TheStreeter-Phclps
mtxlel can bc usedto cornputethe
oxygen
concentration
in a river helowa pnin(clis(Fig.P7.25),
ofsewage
:ally
)sult
num
o = 0' ', - - J t t ! ( c t ' , t- d - ( i , i + t ' ) / )
li,r*k,-ft,,'
Sr
-p(l-e-^"t
t.:0.05d
I
L,,:-50rng/L
5r,: I rng/L/d
distributionol' ptrllutantconcen7.26 The two-dirnensional
trationin u channclcan be describedby
r ' ( r . r ) : 7 . 9 t O . l 3 . r* 0 . 2 1 . t ' -0 . 0 5 , r :
- 0 . 0 1 6 r ' r- 0 . ( X ) 7 - r I
ej.25\
Determinetheexacrt
Iocationol'thepeakconcentration
given
t h e l u n c t i o na n dt h e k r r o w l e c l g c ' l hl haet p e u kl i e sr v i t h i nt h e
-
,(,\.i
{1(,.,r./..
ttt,tl'rltt
-
I
---rr.
7.27 A total chiirgeQ is unitbrnrlydistributedarounda t'ing-
:s an
sh',rned cr-rnductrq rr\th rrli\rl. r \ thrr.:c r1 i- \r':q11-{ .-t 1
o\:\mc€ -\ lr()m tnt center ()1Lr\er\ng tFrg. P1 .21 t.The \orce
exerted on thc charge by the ring is given by
)'
low
ro at
le of
RGURE
P7.24
Aconli
everbeom
|
'I :
-
-
', rt xO' r
4ten 1t2 1 47131t
rrCr/(Nmr;,r7:
l0 iC'
w h e r e a , , : 8 . t 1x5 l 0
Q:2x
and d : 0.9 m. Deterrninethe distancex whet'ethe force is a
mlxir.t'turtr.
r86
PR
OPTIMIZATION
ro
r_
D
20,000
(r
10,000
0
FIGURE
P7.27
motoris a flnc7.2t1The torquctransrlittedto an indr"rction
tion of the slip betwccnthe rotationof the statorfield and thc
rotrrrspced.r,whele slip is detincdas
tl
FIGUREP7.31
Twofrictionless
mosses
cor
lineorelostic
springs.
FIGUREP7.29
Plotof drcg versus
velocity
for cn oirfoil
tlR
n
The combination of the two f'actorsleads to a minimum
clrag
( a ) lf o :0.6 and W : 16.000,dcterrninethe minimun
drag anclthc velocity at which it occurs.
( b ) In adclition,devclopa sensitivityanllysisto determim
d
how this optirrum varics in responseto a range
l5s(l- s)
,' w: 12,000to 20,(xx)with o : 0.6.
( I - . r ) ( 4 , s r- 3 s * z l )
7.-10 Rollcr bcaringsarc sub.iectto fatiguetailure
FigureP7.28showstlris function.Usc u numcricalmcthod lirrgc contrct loacls/i tFig. P7.-10).Thc problenrof
to dctcrminethc slip at which tlrc rlaximum torqueoccurs. thc location ol' the maxirnum stressakrngthc -r axiscan
7.29 Thc total drag on an airtiril can bc cstirnatcdby
shownto bc cquivulcnt(o maxirnizingthefunction:
whcre rr : rcvolutionspcr seconclof rotatingstatorspccd
and ri,,: rotor spced.KirchhotT'slaws can bc usedto show
fbrm) artd slip
that the torquc (cxpressedin dirncnsionlcss
arc rclatcdby
D : 1 1 . 1 1 1 o Y ' +( # ) '
:T
F-r'iction
Liit
0..1
.',4+'F
Torclueironsmifted
to on inductoros o functionof slip
__/
rr'Ir
Find thc .r that rraxirnizcs/(.r').
whcrc D : drag,o : ratio of air densitybctweenthc flight
As scen
altitudcandscalevcl, lV: wcight.and V: r,clocity.
in Fig. P7.29,tlrc two l'actorscontributingto drag alc i'rflected difl'ercntlyas velocity increases.Whcreasfrietion
drag increascswith velocity,thc drag due to lifi dccreascs.
FIGURE
P7.28
-/t
FIGURE
P7.3O
Roller
beorings.
\
04
\
---_]+.r
l+r-l
7.31 In a similar fashionr
Sec. 7.4, develop the poten
tem depicted in Fig. P7.3L
plots in MATLAB. Minimiz
to determine the equilibrium
ltc forcing function F: l0(
Jd t6 - l5 N/m.
732 As an agricultural engi
open channel to carr)
ine the optimal dime
perimeterfor a cross-section
dimensionsuniversal'l
733 Use the funcrion rmins
ofthe shortest ladder that rea
ftnce to the building's wall (l
where
?34 The length of rhe long
6e corner depicted in Fig.
P7.32
r87
PROBLEMS
P7.3|
hictionless
mosses
fo o woll by o poirof
connected
e
- -n"s,f 'i -. r < . . i n,^Jq-
FIGUREP7.33
um
ine
of
by
ing
be
ln a similarlashionto the case study describedin
?.4,derelop
the poterrtialenergyf'unctionfol the sysin Fig. P7..il. Developcontour and surflce
depicted
tnMATLAB.Minimizethc potcntialcnergyl'unction
letermine
theequilibriurndisplacenrents
.r, and -r. given
F : 100N und the paral)rcters
forcing
tunction
k,,:20
tr,= l5 N/rn.
Asanagricultural
engineer.you rnustdesigna trapeopen
channel
to carry irrigationwater (Fig. P7.32)
netheoptinrilldinrensionsto rninimizethe wettecl
fbrr cross-sectit'rnal
areaof 50 rn'. Are the relativc
l
universal
Usethelunctiontm i ri:;e:a
r ch to deternrine
the length
shortest
ladderthat reachesliorrr the ground over the
tolhebuilding's
wall (Fig. P7.33).Testit lirr the case
L _ .t -
A l o d d e rl e o n i n go g o i n s o
l f e n c eo n d i u s tt o u c h i n go w o l l
computing the value of e that minimizes the fbllowing
function:
L(o): Ll- + -.
sfnfi
"''
stn(ft-a-H)
For the casewhere ur1 : r.lr2: 2 m, usea numericalrnethod
describedin this chapter (including MATLAB's built-in
capabilities)
to developa plot ol'L versusa rangeola's from
4 - 5t o 1 3 5 " .
,t ^-
Thelengthof the longestladder that can negotiate
comer
depictedin Fig. P7.,1,1
can be determinedby
P7.32
_-."',
'..
FIGURE
P7.34
A ioddernegolioting
formed
o corner
bytrrohollwoys
:l!:li;
fi
,i
Lineor Syrtems
ii
3. I
ovERVtEW
Whof Are Lineqr Algebroic Equotions?
In Part Two, we O"rrrrn::;.1|,Te value
xrhar satisfied a single equation,f
r) : 0. Now, we
deal with the caseof determining the
values* ri;*r, . . . , x, thatsimultaneously
satisfy a set
of equations:
f,lx,.xr-....x,):0
fik,y,
,i:o
:
xr,.
fJ.xp
:
.,,
,,
',,''.'
,xr) : 0
Such systemsare either linear or
nonlinear.In part
,*:..*.
deal with lincar algebraic equarions
that
are of the generalform
a | x t * a n x z + . . . + a l o x n: [ ,
aztxt * azzxz* .. . * a2,,x,,: fi,
(PT3.t)
-:
a 1 , 1 x 1* a n Z X ZI
...*
ar,,x, :
12,
wherethe rz'sareconstantcoefficients.
the b.s arecon_
stanrs..
rhe
* unknowns.and n is ,f,. nrri.,
of
1.:
equatrons.
All otheralgebraicequationsarenonlinear.
linear Algebroic Equotionsin
Engineeringond Science
fundamenlat
equarions
of engineering
::iy^,:l
T are based
ano.
scrence
on conservationlaws. Sometamllar quantitiesthat conformto such
laws are mass,
energy..anO
momenturn.
In mathematical
terms,these
teadto balanceor continuiryequarions
rhar
llT'p,":
.relare
systembehavioras represented
by ifre levels or
r89
t90
PART3 LINEARSYSTEMS
Feed
@t
FIGURE
PT3.I
Two lpes of syslemsthot ccn be modeledusinglineorolgebrclcequotions.(ol lumpedvorioble
systemthot involvescoupled linitecomponentsond (b) distributedvcrioble systemlhot involves
o confinuum.
responseof the quantitybeing modeledto the propertiesor characteristics
of the system
alrdthe externalstimuli or fbrcingfunctionsactingon the system.
As an example,the principleof massconservation
can be usedto formulatea model
for a seriesof chemicalreactors(Fig.PT3.l a). For this case,the quantitybeingrnodeled
is
the massof the chemicalin eachreactor.The systempropertiesare the reactioncharacteristics of the chernicaland the reactors'sizesand f-lowrates.The forcing lunctions arethe
feed ratesof the chemicalinto the system.
When we studieclrootsof equations,you saw how single-component
systemsresultin
a single equationthat can be solved using root-locationtechniques.Multicolnponentsystems resultin a coupledset of mathematicalequationsthat r.nustbe solveclsinrultaneously.
The equationsarecoupledbecausethe individualpartsofthe systerrale influencedby other
parts.For example,in Fig. PT3.lc, reactor4 receiveschemicalinputsfiom reactors2 and3,
Consequently,its responseis dependenton the quantityofchernical in theseotherreactors.
When thesedependenciesare expressedrnathematically,the resLrltingequationsare
often of the linear algebraicfolm of Eq. (PT3.l ). The,t's areusuallyrneasures
of the magnitudesof the responses
of the individualcomponents.
Using Fig. PT3.ln as an example,
"r1
might quantifythe amountof chernicalrnassin thefirst reactor,.r2
rnightquantifytheamount
in the second,and so lbrth. The a's typically representthe propertiesandcharacteristics
that
bearon the interactionsbetweencomponents.For instance,thea's for Fig. PT3.la mightbe
reflectiveof the flow ratesof massbetweenthereactors.Finally.theb's usuallyrepresent
the
forcing functionsactingon the system,suchasthe feedrate.
Multicomponentproblemsof thesetypes arisefiom both lurnped(macro-)or distributed (nricro-) variable mathematicalmodels.Lrrmpedvoriable probLemsinvolve coupled
3.2 PARTORGANIZATION
t9r
finite components.Examples include trusses,reactors,and electric circuits. The three
bungeejumpersat the beginningof Chap.8 area lumpedsystem.
Conversely,distributed variable problems attempt to describethe spatial detail of
systernson a continuousor semicontinuous
basis.The distributionof chemicalsalongthe
lengthof an elonqated,rectangularreactor(Fig. PT3.lb) is an exantpleof a continuous
variablemodel. Diffbrential equationsderivedfiom conservationlaws specify the distribution of the dependentvariable fbr such systems.These differentiai equationscan be
solved numericallyby convertingthem to an equivalentsystemof simultaneousalgebraic
equations.
The solutionof suchsetsof equationsleplesentsa major applicationareatbr the methcldsin the fbllowing chapters.TheseeqLrations
arecoupledbecausethe variablesat one location are dependenton the variablesin acljoiningregions.For example,the concentrationat
the middle of the reactor in Fig. PT3.11,is a function of the concentrationin adjoining
regions.Similar examplescould be developedfor the spatialdistributionof temperature,
m o n r e n l u mo.r e l e c t r i c i t l .
Aside fiom phvsicalsystems.simultiineous
linearal-uebraic
equationsalso arisein a
varietyof mathernatical
problemcontexts.Theseresultwhen mathematical
functionsare
requiredto satisfyseveralconditionssirnultaneously.
Eachconditionresultsin an equation
that containsknown coefflcientsand uuknownvariables.The techniques
discussed
in this
part can be used to solve fbr the unknownswhen the equationsare linear and algebraic.
Somervidely usednunrericaltechniquesthat employ simultaneous
equationsare legression analysisand splineintel'polation.
3.2
PARTORGANIZATION
Due to its importancein fbrmulating and solving linear alsebraicequations.Chap. 8 provides a brief overviewof tnotrir algebru.Aside from coveling the rudimentsof matrix
representationanclrnanipulation,the cl.rapteralso describeshow man'icesare handledin
MATLAB.
Chttpter 9 is devotedto the most fundamentaltechniquefor solving linear algebraic
systems:GausseLimittolion.Before
launchinginto a detaileddiscussionof this technique,
a pleliminary sectiondealswith simple methodsfor solving small systerns.These approachesarepresented
oneoftlre rrrethodsto provideyou witlr visualinsightandbecause
the eliminationof unknowns-represents
the basislbr Gausselimination.
After this prelirr.rinary
material,"naive" Gausseliminationis discussed.We starl with
this "stripped-down"versionbecauseit allows the fundarnentaltechniqueto be elaborated
on without complicatingdetails.Then,in subsequent
sections,we discusspotentialproblems of the naive appr-oirch
and presenta number of mocliflcationsto mjnirlize and circunlventtheseproblenrs.The fbcus of this discussionwill be the processof switching
rows,orpdrlial pit'oting.The chapterendswith a brief descriptionof eflicientmethodsfbr
solving tridiagonal ntatrrces.
Chapter10 illustrateshow Gausseliminationcanbe lbrmulatedas an LI) factoriZation.
Such solutiontechniquesare valuable1br caseswhere many right-hand-sidevectorsneed
to be evaluated.The chaoterends witlr a brief outline of how MATLAB solveslinear
s\/stet)-ls.
r92
PART3 LINEARSYSTEMS
Chapter -1-1
startswith a descriptionof how LU factorizationcan be employedto efficiently calculatethe mati.r inverse,which has tremendousutility in analyzingstimulusresponserelationshipsof physicalsystems.The remainderof the chapteris devotedto the
importantconceptof matrix condition.The conditionnumberis introducedas a measureof
the roundoff errorsthat can result when solving ill-conditionedmatrices.
Chapter I2 dealswith iterative solutiontechniques.which are similar in spirit to the
approximatemethodsfor rootsof equationsdiscussedin Chap.6. That is. they involve guessing a solutionand then iteratingto obtaina refinedestimate.The emphasisis on the GaussSeidel method, although a description is provided of an alternative approach, the Jacobi
method.The chapterendswith a brief descriptionof how nonlinearsimultaneousequations
canbe solved.
nrl
effiuluso the
[e of
o the
UESS-
,.I. t
l"
LineorAlgebroicEqrotions
ond Motrices
AUSS-
Tcobi
tions
CHAPTEROBJECTIVES
The primary objectiveof this chapteris to acquaintyou with linear algebraicequations
and their relationshipto matricesand matrix algebra.Specificobjectivesand topics
coveredare
e
.
'
.
.
U n d e r s t a n d i nmga t r i xn o t a t i o n .
Being able to identify the following typesof matrices:identity,diagonal,
symmetric,triangular,and tridiagonal.
Knowing how to perlbrm matrix multiplication and being able to assesswhen it is
feasible.
Knowing how to representa systemof linear algebraicequationsin matrix form.
Knowing how to solve linear algebraicequationswith left division and matrix
inversionin MNLAII.
\OU'\E
GO\ A.PROBLEN\
upposerhat three jumpers are connectedby bungeecords.Figure 8.ln shows them
It
U.ing held in placeverticerllyso that eachcord is fully extendedbut unstretched.We
\
crn define threedistances..t'1.,t'2.and .\. as measureddovnv'ard from eachof their
9/
grar it1 takeshold and thejumpers s ill evenunstretchedpo:itions. ,.l.tierrhel are reJeased.
positionsshownin Fig. 8.1b.
tually cometo the equilibriun-r
Supposethat you are askedto computethe displacernentof eachof thejumpers.If we
assumethat eachcord behavesas a linear spring and follows Hooke's law, free-bodydiagramscan be developedfor eachjumper as depictedin Fig. 8.2.
forcebalancecanbe writtenforeachjumper:
Using Newton'ssecondlaw, a steady-state
m 1 gI k z ( x z- 1 1 )- f t 1 r 1: Q
t 1 t l g* k r ( , r : - x ) - k z ( x z- . r r ) : 0
tntS - k: (.r: - irz) : 0
r94
LINEAR
ATGEBRAIC
EQUATIONS
AND MATRICES
ffi
",:Ol?r
- nu 1
ffi
Iri
.:
n
ffi
z'^r
- ni l )
li:
?r
ffi
il
t:tl
ffi
lr)
_H
Arxr
I'l
I
n
tr
?r
t2(r, - 11)
I
i
ffi
tl
lr)
11
tl
tr
(a) Unstretched
tt
( b ) Stretched
nrrg k.(x2- x)
FIGURE
8.I
Threeindividuos conneciedbv bunqeecords
ill
m tg t.(.r1 - "r2)
m$
FIGURE
8.2
Free-body
diogroms.
wherenr; : the massof jumper r (kg), A; : the springconstantfor cordT(N/m),-ri : the
displacernentof jumper I measureddownwardliom the equiiibrium position (m), andg =
gravitationalacceleration
19.81nr/sr).Collectingtermsgives
( k 1l k ) x 1
-kzxz
:mtg
-ftz.tr * (kz I kz)xz- k:-rr : mzg
(8.1)
-ktxzlk3xl-7n19
Thus. the problem reducesto solving a systelnof three simultaneousequationsfor
the threeunknown displacements.Becausewe have useda linear law for the cords,these
equationsare linear algebraicequations.Chapters8 through l2 will introduceyou to how
MATLAB is used to solve such systemsof equations.
8.I
MATRIXALGEBRAOVERVIEW
Knowledgeof matricesis essentialfor understandingthe solutionof linear algebraicequations. The tbllowing sectionsoutline how matricesprovide a conciseway to representand
maninulale
l i n e r ra l q e h r a i c
II
MATRIX
ALGEBRA
OVERVIEW
ll
iiil
C o l u m n3
I
I
I
at3
a:t
at2
azt
azz .t..:&it:'',
azu
onl
o.)
a,r,'
Qt,
I
-Row2
-
t^t
a,n3
FIGURE
8.3
A moirix.
8.1.1 Mqfrix
Nototion
A matrix consistsof a rectangulararray of elementsrepresentedby a single symbol. As
depictedin Fig. 8.3, [A] is the shorthandnotationfor the matrix andai 1 designatesan individual element of the matrix.
A horizontalset of elementsis called a row and a vertical set is called a column.The
first subscriptI alwaysdesignatesthe numberof the row in which the elementlies.The second subscriptTdesignatesthe column.For example,elementa23is in row 2 and column 3.
The matrix in Fig. 8.3 has m rows and n columns and is said to have a dimensionof
m by n (or m x n). It is referredto as an m by n matrix.
Matrices with row dimensionm : l. suchas
[b]:
[bl
b2
brl
are called row yectors. Note that for simplicity, the first subscriptof each element is
dropped.Also, it shouldbe mentionedthat thereare times when it is desirableto employ a
specialshorthandnotation to distinguisha row matrix from other types of matrices.One
way to accomplishthis is to employ specialopen-toppedbrackets,as in fbl .l
Matrices with column dimensionn : l. suchas
lnl j
tL
-
L:
(8.2)
are ref'erredto as column vectors. For simplicity, the secondsubscriptis dropped.As with the
row vector,thereare occasionswhen it is desirableto employ a specialshorthandnotation
to distinguisha column matrix from other typesof matrices.One way to accomplishthis is
to employ specialbrackets,as in {c}.
I In addition to specialbrackets,we will use caseto distinguishbetweenvectors(lowercase)and matrices
(uppercase).
r96
LINEARALGEBRAIC
EAUATIONSAND MATRICES
Matrices where rn :
tttl
l-r,,
n ate called .square matrices. For example. a 3 x 3 matrix is
atrl
I A l : I a r , a ) - ) . r r ,I
L,;,";: ,;,1
The diagonalconsistingof the elementsas, a22,and a.j3is termed theprincipal or maitt
diagonal of the matrix.
Squarematricesare particularly important when solving setsof simultaneouslinear
equations.For such systems,the number of equations(correspondingto rows) and the
numberof unknowns(correspondingto columns)must be equalfor a uniquesolutionto be
possible'Consequently,squarematricesof coefTicients
are encounteredwhen dealinewith
suchsystems
There are a number of specialforms of squarematricesthat are important and should
be noted:
A symmetricmatrix is one wherethe rows equalthe columns-that is, a; : aii for all
i
i's and.l's.For example,
[st2f
tAl:ll3
7l
L2 7 8_l
i s a 3 x 3 s y n r m e t r inc r a t r i x .
A diagonal nmtrix is a square matrix where all elements off the main diagonal are
equal to zero, as in
fo,,
I
lAl=l
a-) )
|
orr)
L
Note that wherelarge blocks of elementsate zero.they are left blank.
An identi4' matrix is a diagonalmatrix where all elementson the main diagonalare
equalto 1, as in
[rl
tAl:l
I
I
LlJ
The symbol Ul is usedto denotethe identity matrix. The identity matrix haspropertiessimilar to unity. That is,
[A]Ul: [/][A]: tAl
An upper triangular malrx is one whereall the elementsbelow the main diagonalare
zero. as in
[,4]:
['''
rl12
o,rl
422
ott
I
ctstJ
8.I MATRIXALGEBRA
OVERVIEW
197
A lower triangular malrrx is one whereall elementsabovethe main diagonalarezero,
as ln
I
f o,,
tAl
:
|
i
i
o11
1,,;,,; ,,,]
"',
I
i
A bandedntotix has all elernentsequalto zero,with the exceptionof a band centered
on the main diagonal:
ln l:
lI
f al
at)
I
a:z
dt.r
,,,
Or,
I
aat aqc)
t .a Z t
I
|
L
(/tl
Now that we have specifiedwhat we meanby a matrix, we can definesomeoperatingrules
that govern its use.Two nr by n natrices are equalif, and only if, every elementin the first
is equal to every elementin the second-that is, [A] : [B] if ai1 : bi1 for all i andj.
Addition of two matrices,say, [Al and [B|, is accomplishedby addingcorresponding
terms in eachmatrix. The elementsof the resultingmatrix [C] are computedas
c'i1: ct,i1b,i
f o r i : 1 , 2 , . . . , m a n dj : 1 , 2 , . . . , n . S i m i l a r l yt,h e s u b t r a c t i oonf t w o m a t r i c e ss.a y .
[E] ninus [F], is obtainedby subtractingcorrespondingterms,as in
-fij
forl: l,2,...,mand j:l,2....,n.ltfollowsdirectlyfromtheprecedingdefinitions
that addition and subtractioncan be performedonly betweenmatriceshaving the same
dimensions.
Both addition and subtractionare commutative;
t A l + t B l: [ B ] +[ A ]
and associative:
( t A l +t B l )+ [ c ] : [ A l + ( t B ]+ t c l )
The multiplication of a matrix [A] by a scalarg is obtainedby multiplying every element of [Alby S. For example,for a 3 x 3 matrix:
fPo,,
I on.,
Rart Pat,,f
i o , , i o r ,I
efltt
I
I
I
j
II
8.1.2 Motrix Operoting Rules
I D I: s l A l: l i o l
I
i
I
The precedingmatrix has a bandwidthof 3 and is given a specialname-the tridiagonal
matri.r.
dij:eij
I
,n,,
I
I
I
i
I
i
I
r98
LINEARALGEBRAIC
EAUATIONSAND MATRICES
q-l
[A'l^"n
F,'I
t,
rl ->
[::]Ll
[Blnt
=
[C],"t
;)
Interiordimensions
are equal.
multiplication
is possible
[sxs-1x7-22
II
Exteriordimensionsdefine
t h e d i m e n s i o n so f t h e r e s u l t
FIGURE
8.4
Visuol
depiction
of howtherowsondcolumns
lineupin
FIGURE
8.5
Motrixmultlplicotion
conbe per{ormed
onlyif
m o t r i xm u l t i p l i c o i i o n .
l h e i n n e rd i m e n s i o nosr e e q u o l .
The productof two matricesis represented
as [C] : tAltBl, wherethe elementsof [C]
are dellned as
{ 1i :
+
) .oislt11
t:l
{8.3)
where n : the column dimensionof [A] and the row dimensionof [B]. That is, the c;, elenent is obtainedby addingthe productof individual elenents from the lth row of thefir$
matrix, in this case[A], by theTthcolumn of the secondmatrix [B]. Figure 8.4 depictshow
the rows and columnsline up in matrix multiplication.
According to this definition, matrix multiplication can be perfbrmedonly if the first
matrix has as many columnsas the nunber of rows in the secondmatrix. Thus, if [A] is an
m by n matrix, [B] could be an n by / matrix. For this case,the resulting[C] matrix would
have the dimensionof m by /. However, if [B] were an m by i matrix, the multiplication
could not be performed.Figure 8.5 provides an easy way to check whethertwo matrices
can be multiplied.
If the dimensionsof the matricesare suitable.matrix rnultiolicationis associative:
(tA JtB[c]
l ) : tA l (tB l tcl )
'aLnd
distributive:
t A l ( t B l +[ c ] ) : [ A l [ B ] +t A l t c l
or
( [ A ] + t B l ) l c l : [ A ] [ c ]+ l B l l c l
However,multiplicationis not generallycommutative:
tAttBl+ lBltAl
Thot ic rh,' order nf mrrltinlic:rtion is
B.'I MATRIXALGEBRA
OVERVIEW
Althoughmultiplicationis possible,matrix divisionis not a definedoperation.However.if a matrix [A] is squareand nonsingular.thereis anothermatrix [Al r, cailed the
inver.seof [Aj, fbr which
tAltAl
' :
1 J e ]: 1 1 1
[A]
Thus,the multiplicationof a matrixby theinverseis analogous
to division,in the sensethat
a numberdividedby itself is equalto l. That is, nultiplicationof a matrix by its inverse
l e a d st o t h e i d e n t i t ym a t r i x .
The inverseof a 2 x 2 matrix can be representedsimply by
lAl
':
ail422 -
anq2l
I
o" -n'tl
L-at
itrrJ
Similarfbrmulastbr higher-dimensional
matricesaremuchmoreinvolved.ChapterI I will
deal rvith techniqueslbr using numericalmethodsand the computerto calculatethe inverse
lbr suchsystems.
Two othermatrix manipulationsthirtwill have Lrtilityin our discussionare the transposeand the augmentationof a matrix. The trartslto,se
of a matrix involvestransformingits
rows into columnsand its columnsinto rows. For examole.for the 3 x 3 ntatrix:
0t)
f u,,
a,,l
l A l : l , I . ; ( t ) - , , i ,I
Lr;; 't2 r,.,J
t h e t n r n s p o s ed .e s i g n a t eld, e 1 ri.s d e l i n e da s
(t,t a.,l
l - , , ,'
t A l r: I , , ; ' u . , u , .I
L,. ,;. ",;l
In otherwords,
element of
the
rr;; the transpose
is equalto the zu
7; elementof the original
matl'lx.
The transposehas a variety of functionsin matrix algebra.One simple advantageis
that it allows a column vector to be written as a row and vice versa.For example.if
J , -r l -r.
['' l
, -. t
l.r I
[., J
tnen
{c}r :
lc1 c2 c:l
In addition,the transposehas numerousmathematicalapplications.
The final matrixmanipulationthat will haveutility in our discussionis uuqmenratiott.
A tnartrixis augrnentedby the additionof a column (or colunurs)to the original matrix. For
example,supposewe have a 3 x 3 matrix of coefficients.We might wish to augmentthis
matrix [A] with a 3 x 3 identity matrix to yield a 3-by-6-dimensionalmatrix:
[,t,,
ott
I
LaSt
a1t
d1
l ool
a2t
a3t
021
{41
0l0l
0 0 r_j
200
LINEARALGEBRAIC
EAUATIONSAND MATRICES
onthe
Such an expressionhas utility when we must perform a set of identicaloperations
matrix
rows of two matrices.Thus. we can perlbrm the operationson the singleaugmented
ratherthan on the two individual matrices.
EXAMPTE
8.1
M A T L A BM o t r i x M o n i o u l o t i o n s
Problem Stotement. The following exampleillustrateshow a variety of matrixmanipulationsareimplementedwith MATLAB. It is bestapproachedas a hands-onexercise
onthe
computer.
S o l u t i o n . C r e a t e a 3x 3 m a t r i x :
>>
=
A
[1
5
b;7
4
2;-_t
61]
6
2
,-4
-3
6
The transposeof [A] can be obtainedusing the ' operator:
>>
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<anlntinnq
rule
of (o)Kirchhoff's
current
o n d ( b ) C h m ' lso w .
The voltage (or loop) rule specifiesthat the algebraic sum of the potential differences
(that is, voltage changes)in any loop must equal zero. For a resistor circuit, thisis
expressedas
fc-I^:o
(8.s)
of
where f is the emf (electromotive force) of the voltage sources,and R is the resistance
any resistors on the loop. Note that the secondterm derives from Ohm's law (Fig. 8.8b),
which statesthat the voltage drop across an ideal resistor is equal to the product of the
cunent and the resistance.Kirchhoff's voltage rule is an expressionof the consentationol
enerSy.
Application of theserules results in systemsof simultaneouslinear algebraic
Solution.
equationsbecausethe various loops within a circuit are interconnected.For example,consider the circuit shown in Fig. 8.9. The currents associatedwith this circuit are unknown
onesimply
no greatdifficultybecause
anddirection.This presents
bothin magnitude
assumesa direction for each current. If the resultant solution from Kirchhoff's lawsis
negative,then the assumeddirection was incorrect.For example,Fig. 8.10 showssone
assumedcurrents.
8.3 CASESTUDY
ili
continued
,il
'll
I
!l
R:5O
Vt:200Y
7o:0V
a n =i s o
R:20f)
A resistor
circuitlo be solvedusingsimultoneous
FIGURE
8.IO
i;n"^' ^l^.h.^;.
AssumedcLruenr
direction5
an, rnf innc
Given these assumptions, Kirchhoff's current rule is applied at each node to yield
in*isz*jrr:0
ios* isz- ls+ :0
i a 3- 1 2 :
Q
i5a-iaj:Q
Application of the voltage rule to each of the two loops gives
ios-isz-ls+:0
-is,rRs+- i+-tR+:* i12Ri2* l52R52: Q
-iosRos - iszR-srI ieRe - 200 : 0
or, substituting the resistances from Fig. 8.9 and bringing constants to the right-hand side,
*151s+- 5ia3- 10i32* 10i52: Q
-20ios-10is:f5ip:200
Therefore, the problem amounts to solving six equations with six unknown currents. These
equationscan be expressedin matrix form as
[r
l0
l0
lo
t0
l
r
-l
0
0
I
0
o
l0
Ls -ro
0
-l
-1
o
0
o
o
_20 o
-t0
0
0
I
-15
0l
l12
0 |
r52
r
-1 I
|
--sl
ol
t3z
.oJ
r)4
iqz
:{,L
208
EQUATIONS
AND MATRICES
LINEARALGEBRAIC
continued
Although impractical to solve by hand, this system is easily handled by
The solution is
>>A=t111000
0 -1 0 1 *1 0
0 0 -1 0 0 1
0 0 0 0 1 -1
0 10 -10 0 -15 *5
5 *10 A -20 0 ol;
>> b=t0 0 0 0 a 2001,;
>> current=A\b
=
current
6.1538
-4.6L54
-1.5385
-6.1538
*1.5385
-1.5385
8.1 ci
comm€
the oril
E.2 At
tAl
rcI
LE
tFl
Thus, with proper interpretation of the signs of the result, the circuit curents and
ages are as shown in Fig. 8. 1L The advantages of using MATLAB for problems of this
should be evident.
Answe
(a) WI
(b) Ide
(c) Wl
J t2'
(d) Pe
(l
FIGURE8.I I
obtoined
using
MATLAB
Thesolution
forcurrents
ondvoltoges
V:
169.23
(4
PROBLEMS
PROBTEMS
Givena squarematrix [A], writc a single line MATLAB
thatwill createa new matrix [Ang] that consistsof
original
matrix[AJ augnrentedby an identity matrix
[11.
A numberof mafices are definedas
[137f
lr 2 7l
L2 0 4J
il pt:l:--,i ,'l
l r t, -_
(\
[r s 8l
r r l = 1 7t r l
L4 0 6_l
1-l
r r r = f ro
7 .11
Ll
LG):17
6 4J
thefbllowingquestionsregardingthcse matrices:
Whatarethe dimensionsof the matrices,l
Identifythe square,coluntn, and row matrices.
Whatarethe values of the clcments..ar,. b.1, dzt,
ezz,
/r:,8r:?
Perform
thefollowing operations:
(r)trt + tBl
(1)7 x [B]
(2)IAI+ lFl
1-s1
[A] x tBl
FIGUREP8.5
(3)tBl _ tEl
(6)(C],
(7) [Bl x [Al
( 1 0 )[ 1 ] x t 8 l
(8) [D]7
r n 1 [ E l rx [ E l
(e) tAl x { C}
( t 2 ){ C } rx { C }
8.3 Writethefollowingsetof equations
in rnatrixforrn:
50:5,r:-7.r:
4rz+7.\*30:o
rr - 7.r.r: ^10, 3xzf 5;:r
UseMATLAB to solvelbr theunknorvns.
In addition,usert
to computethetranspose
andthe inverseof thecoefficient
matrix.
8.4 Threematrices
aredefinedas
-,'l
,
,",:ffi l'],",=
l],.,:;
[,i,
(a) Perfbrm all possible nrultiplicationslhat
can be com_
puted betweenpairs of thesematrices.
(b) Justify why the remainirigpairs cannot
be rnultiplied.
(c) Use the resultsof (a) to illustratewhy the order
of multiplication is important.
8.5 Five reactorslinked by pipes are shown in
Fie. pg.5.
The rate of massflow througheachpipe is computed
as the
prodr"rctof flow (Q) and concentration{r.). At steady
state,
the mass llow irrto and out of each reactor must be equal.
2ro
EAUATIONSAND MATRICES
TINEAR
ALGEBRAIC
For example, for the first reactor, a mass brilance can be
written as
0 o r r : ofr Q t t ' t :
/-'
cos]0 -Fjcos60 *F1.71
-Fr sin -10
F.rsin 60 * Fr ,
\
nr :0:
F : I F 1c o s 3 0 I
F z nI H z
F rs i n 3 01 F 2 . ,I V :
for node 3:
/r'n
algebraicequationsin matrix form and then use MATLAB
to solve fbr the unknowns.
8.7 Considerthe threemass-fourspringsystemin Fig.P8.?,
Determining the equationsof motion from XF' : ma,for
eachmassusing its free-bodydiagramresultsin thefollow'
ing differentialequations:
/kr+ k"\
:0:
5
/ --' n 'u
\-
P8.7
TIGURE
rr*lrlrr-l-lr::0
for node 2:
)/--
I
Q r s c tI Q n c t
Write massbalancesfor the remainingreactorsin Fig. P8.5
and expressthe equationsin matrix fbrm. Then use MATLAB
to solve for the concentrationsin eachreactor.
8.6 An importantproblem in sffucturalengineeringis that of
finding the forcesin a staticallydeterminatetruss(Fig. P8.6).
This type of structurecan be describedasa systemof coupled
linear algcbraicequationsderived from force balances.The
sum of the forces in both horizontal and vertical directions
must be zero at each node, becausethe system is at rest.
Therefore,for node 1:
Lf,:0:-Fr
\- n'' : 0 :
+"x?
r - :- 0 :"
\m:/
-F, - F.rcos60-l Fr.r,
:
D a u o : F : s i n 6 o ' *F : , ,r v :
where F,.,,is the externalhorizontal force applied to node I
(wherea positiveforce is from left to right) and {.,. is the external vertical force appliedto node i (where a positiveforce
is upward). Thus, in this problem, the 1000-lb downward
fbrce on node I correspondsto F,.,: - 1000.For this casc,
all other F,.,'s and {,,,'s are zero. Expressthis set of linear
/kr\
\mt/
/l'\
/,t'\
/k,*t,\
tr (31'' -(
;,-
rzr /
\
,t:
\m2 l
/
/k.\
/k,'" \
( a ) * r +-{ ^ r r ^ 4 } x , : o
\.n.r/
\
m3 /
andmr=mt=
w h e r e f t -, k t : 1 0 N / m , k . : f t r : 3 0 N / m ,
mz: I kg. The three equationscan be written in matrixform:
Q : {Accelerationvector}
+ lk I m matrixl{displacementvectorx }
At a specifictime where:r, : 0.05 m, r: : 0.04 m, andl,=
0.03 m, this forms a tridiagonal matrix. Use MATLABto
solve for the accelerationof eachmass.
8.8 Solve
[ 3 + 2 i + ' lJ : r [ _- l I 2 + , I
i I
I i r . l[ z : f
8.9 Perform the same computationas in Example8.2,but
use five parachutistswith the following characteristics
F I G U R EP 8 . 6
Moss
Jumper
(ks)
Spring
Conslont
(N/m)
I
2
3
4
5
55
75
60
75
90
BO
50
70
r00
20
urltr"*ffi
Cord
tength (m)
t0
10
t0
r0
t0
8.10 Tl
identica
the bott
2.5kg,t
displac
8.11 Pe
the circ
8.12 Pi
the circ
8.13 D
two mal
lZl is n
multipli
the prol
8.14 D
the tran
plemen
PROBLEMS
2ll
Threemassesare suspendedvertically by a seriesof
springswheremass I is at the top and mass3 is at
bottom.
lf g : 9.81 m/s', ffir : 2 kg, m2: 3 kg, 2, :
kg,andtheft's: l0 kg/s2,useMATLAB to solve for the
ntsr
Perform
the samecomputationas in Sec. 8.3, but for
circuit
in Fig. P8.I l.
Perform
the samecomputationas in Sec. 8.3, but for
circuit
in Fig.P8.12.
Develop,
debug,and test your own M-file to multiply
matrices-thatis, [X] : lYl[Zl, where lY] is m by n and
isn by p. Employ f or. . . end loops to implement the
ion andinclude error trapsto flag bad cases.Test
program
usingthe matrices from Prob. 8.4
Develop,
debug,and test your own M-file to generate
tmnspose
of a matrix. Employ f or. . . end loops to imthetranspose.
Testit on the matricesfiom Prob. 8.4.
R=30O
,
R:20c)
R:10O
Vr : 150volts
R:2O
R=5O
Vo: 0 volts
F I G U R EP 8 . I I
R=35O
1
Vr : 10 volts
R:8f)
Vo= 150volts
F I G U RP
E8 . I 2
ti
. ...
GoussEliminotion
CHAPTEROBJECTIVES
The primary objectiveof this chapteris to describethe Gausseliminationalgorithm
fbr solving linear algebraicequations.Specificobjectivesand topics coveredare
'
.
'
.
.
'
Knowing how to solve small setsof linear equationswith the graphicalmethod
and Cramer'srule.
Understandinghow to implementfbrward eliminationand back substitutionasin
Gausselimination.
Understandinghow to count flops to evaluatethe efficiency of an algorithm.
Understanding
the conceptsof singularityand ill-condition.
Understandinghow partial pivoting is implementedand how it differs from
completepivoting.
Recognizinghow the bandedstructureof a tridiagonalsystemcan be exploited
to obtain extremelvef1icientsolutions.
t the end of Chap. 8, we statedthat MATLAB provides two simple and direct
left-division,
methodsfbr solvingsystemsof linearalgebraicequations:
>>
x
=
A\D
and matrix inversion,
>> x
=
inv (A) *b
Chapters9 and l0 provide backgroundon how such solutionsare obtained.Thismaterial is includedto provide insight into how MATLAB operates.In addition,it is intended
to show how you can build your own solution algorithmsin computationalenvironments
that do not haveMATLAB's built-incapabilities.
9.I SOLVINGSMALLNUMBERS
OF EQUATIONS
2r3
The techniquedescribedin thischapteris cllleclGausseliminationbecause
it involves
combiningequafionsto climinateunknowns.Althoughit is clneof the eirrliestrnethodsfbr
solving simultaneous
equctions,it remainsarnongthe most importantalgorithnrsin use
today and is the basisfur linear equationsolving orl many popular sotiwarepackagesincluclingMA|LAB.
9 . 1 SOIVING StylAtt NUMBERSOF EQUATIONS
Betbreproceediugto Gausselinrination,we will describeseveralmethodsthat are appropriate fbr solving small (n < 3) setsof simultaneousequationsand that do not recluirea
computer.Theseare the graphicalmethocl,Cramer'srule. anclthe eliminationof unknowns.
9.1.1 fhe GrophicolMethod
A graphicalsolutionis obtainablefor two linearequationsby plottingthen on Cartesian
coordinateswith one axis colresponding
to -r1and the othel to -r2.Becausethe equations
arelinear.eachequationwill plot as a straightline. For example,supposethat we havethe
tbllowing equations:
3 - r r* 2 . r r :
-,-.
--L l,^
-
l8
)
I f v e e . \ u n c r h e t . ir>, t h e z b : c i l : t a , { e a a n . > o l r c c a c h t t l t h t s e t e u i l U o i l s r o r - r t !
3
-rl:--.tl+9
-t-t:
I
-f I
t-rl
The equationsare now in the forrn of straightlines-that is..t2 : (slope)-rr * intercept. When theseequationsare graphed,the valuesof r1 and x2 at the intersectionof the
lines representthe solution (Fig. 9. l ). For this case,the solutionis ,r1 : 4 andxz : 3 .
by a planein a
For threesimultaneous
equations.
eachequationwould be represented
three-dimensional
coordinatesystem.The pointwherethe threeplanesintersectwould representthe solution. Beyond three equations,graphical methodsbreak down and. consequently,have little practical value fbr solving simultaneousequations.However,they are
usefulin visualizingpropertiesof the solutions.
For example.Fig. 9.2 depictsthreecasesthatcan poseproblemswhen solvingsetsof
parallellines.
linearequations.
Fig. 9.2a showsthe casewherethe two equationsrepresent
For suchsituations,thereis no solutionbecausethe lines nevercross.Figure9.2b depicts
the casewhele the two linesarecoincident.For suchsituationsthereis an inllnite number
of sollrtions.Both typesof systemsaresaidto be singular.
In addition,systernsthat are very closeto being singular(Fig. 9.2c) can also cause
problems.Thesesysternsare said tobe ill-conditirnecl.Graphically,this conespondsto the
fact thatit is diiTicultto identifythe exactpoint at which the linesintelsect.Ill-conditioned
systemswill alsoposeproblenrswhen they areencountered
duringthe numericalsolution
This is becausethey will be extremelysensitiveto roundofTerror.
of linearequations.
il
2r4
GAUSSELIMINATION
6rt
9.I
FIGURE
ofthe
Theintersection
lineorolgebroicequotions.
of o setof Mo simultoneous
solutlon
Grcphicol
the solution.
linesrepresenls
EX
9.2
FIGURE
(o)nosoluiion,
ond
solutions,
systems:
ondilfconditioned
of sinqulor
depiction
{b)lnflnlte
Grophicol
visuolly
to detecl
isdifficuh
oresoclosetholihepointoI intersection
(c)ilicondiiioned
theslopes
system
wtrere
9.1.2
Determinqnls qnd Cromer's Rule
Cramer's rule is anothersolution techniquethat is best suitedto small numbersof equa'
tions. Before describing this method, we will briefly review the conceptof the determinant,
which is usedto implementCramer'srule. In addition,the determinanthasrelevancet0the
evaluationof the ill-conditionine of a matrix.
SOLVINGSMALLNUMBERS
OF EAUATIONS
2r5
Determinonts. The determinantcan be illustratedfor a set of threeequations:
[A]{,t} : {b}
where [A] is the coefficientmatrix
[Al:
ort an anf
f
I a21 a22 azz I
tl
LA3t
432
AT J
The determinantof this systemis formed from the coefficientsof [Aj and is represented
as
D :
latt
I u.,
i" ;;
at2
Ltt
at3
o)1
o .,
" .;
Although the determinantD and the coefficientmatrix [Al are composedof the same
elements,they are completelydifferent mathematicalconcepts.That is why they are distinguishedvisually by using bracketsto enclosethe matrix and straightlines to enclosethe
determinant.In contrastto a matrix, the determinantis a single number.For example,the
value of the determinantfor two simultaneousecuations
ott
D : latr
I
I A)t
A)) |
- - l
is calculatedby
D:atla22-an(7zt
For the third-ordercase,the determinantcan be computedas
ott o"l
ot'l
D : a ,' ,l al as !z
| o ,-rl al o: rt t a . r : l* o ,-.l ,al:or " a J 2
.?.r.rl
l
(e.1)
where the 2 by 2 determinantsare called minors.
Determinonts
Problem Stotement. Computevaluesfor the determinantsof the systemsrepresentedin
Figs.9.l and9.2.
S o l u t i o n . F o r F i g . 9 . 1:
D-
JL
-1
2
:3(:2)-2(-1):8
ForFig.9 2a:
L
_ !
D-
|
2--l
1
: - ; ( ll ) - t l ; l :/0- 1 \
L
\L,/
ForFig.9.2b:
D:l
l-1
2
-r : - t I( : 2 )1(-l):0
l-r
z
216
GAUSSELIMINATION
For Fig. 9.2c:
tr
D_
I
)'
,',
_:l
:-
I
/-?
2
\s/
?\
___1)_ll_l:_0.04
In the foregoing example,the singular systemshad zero determinants.Additionally,
the resultssuggestthat the systemthartis ahnostsingular'(Fig. 9.2c) has a detenninant
that
is close to zero. These ideas will be pursuedfurther in our subsequentdiscussionof illconditioningin Chap.I l.
equaCromer's Rule. This nrle statesthat eachunknownin a systemof linearalgebraic
tions may be expressedas a fraction of two determinantswith denorninatorD andwiththe
numeratorobtained from D by replacing the colurnn of coefficientsof the unknownin
q u e s t i o nb y t h e c o n s t a n tbs r . b 2 , . . . . b , , .F o r e x a m p l ef,o r t h r e ee q u a t i o n sx,1 w o u l d b e
computedas
.,,:
lbr oe 4r:i
I bz azz azt I
I tr^ a1. 4.. l
,
EXAMPLE
9.2 Cromer's
Rule
ProblemStotement. Use Cramer'srule to solve
0 . 3 ; r r+ 0 . 5 2 x 21
0 . 5 . r r+
r::
-0.01
x2f l.9rj:
0.67
0 . 1 r r + 0 . 3 x 2 * 0 . 5 1 3: - 0 . 4 4
Solution. The determinantDcanbe evaluatedas [Eq. (9.1)]:
a:o:lol:
l?l-o"l3i
A:31.'l3i
o:l:-ooo22
The solutioncan be calculatedas
.r I -
-0.0r 0.52 I
0.67 r
1.9
-0.44 0.3 0.5
-4.0022
10.3 -0.01
-0.0022
I I
i o.r 0.67 r.eI
| 0.I
0.03278 - 1 4 . 9
:
-0.44 0.5|
-0.0022
0.0649 _
_)a <
-0.0022
-0.01
10.3 0.52
I o.s r
0.6l
I o.r 0.3 *0.44
-0.043s6
,0.0022
-0.0022
: 19.8
SOLVINGSMALTNUMBERS
OF EAUATIONS
217
The ae-' Function. The determinantcan be computeddirectly in MATLAB with the det
function. For example,using the systemfrom the previousexample:
>> A-10.3 0.52 I;0.5
>>
1 1.9;0.1
0.3 0.51;
D=det (A)
D=
0 .0422
Cramer'srule can be appliedto compute.{i as in
>> 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
<_FIOW:
A_
-Flow:
F,+
FIGUI
PROBLEMS
235
-lJ - CD
-AD r DE
A,+AD:o
A,*AB:0
74+ BC+(3/s)BD:0
AB - G/s)BD:0
A / 5 t CE : 0
(3l5tBD:0
CD+ (415)BD:O
- D E - ( 3 / s ) C E: 0
-BC+(315)CE:0
E \ . 1( 4 l s ) C E - 0
9.1(t A pentodiagonal
systemwitlr a bandwidth
of flve can
generally
beexprcssed
as
.ft ,9r hr
e2 .fz 8z h2
P9.r4
dt
e1
h
ht
Br
pump deliversa unit llow (Q, ) of a highly
9.11A peristaltic
viscous
tluid.The network is depictedin Fig. P9.14. Every
section
hasthe samelength and dianreter.Thc massand
I energybalancecan be simplitied to obtain thc
in everypipc. Solve the following systemof equations
in theflow in every stream.
d,,
t
€tt I
d,
Q z + 2 Q t -2 Q : : O
Q s t 2 Q o- 2 Q r : 0
€rt
8n
I
.f,,
rl
r2
r3
,{l
3 Q .- 2 Q ( , : 0
Jr-t
.rl
0,=0,+O,
Qt= Qti
Qs
Qs=Qo* Q,
fttl
15A trussis loaded as shown in Fig P9.15. Using the
in-esetof equations,solve for the l0 unknowns.AB.
,AD.BD.CD. DE, CE, A,. A,, and E,
24 kN
xrt
Developan M-file to etficientlvsolvesuchsystemswithor,rt
pivotingin a similar fashionto the algorithmusedfbr tridiagonal matrices in Sec.9.4.1.Tcst it fbr the following case:
-2
lo
Lo
P9.r5
-l
9 -4
3 al
1
,1
0 -7
t_"
t_;
0
I
lf
-3
:ltiitil
..
LU Foctorizotion
OBJECTIVES
CHAPTER
The primary objective of this chapter is to acquaint you with LIJ factorizationt.
Specific objectivesand topics coveredare
.
'
.
.
.
Understandingthat LIJ factortzationinvolves decomposingthe coetlcient matrix
into two triangularmatricesthat can then be usedto efficiently evaluatedifferent
right-hand-sidevectors.
Knowing how to expressGausseliminationas an LU factortzation.
Given an LIJ factorization.knowing how to evaluatemultiple right-hand-side
vectors.
a
Recognizingthat Cholesky'smethodprovidesan efficient way to decompose
symmetricmatrix and that the resultingtriangularmatrix and its transposecanbe
usedto evaluateright-hand-sidevectorsefficiently.
Understandingin generalterms what happenswhen MATLAB's backslash
operatoris usedto solvelinearsystems.
s describedin Chap. 9, Gausselimination is designedto solve systemsof linear
algebraicequations:
[A]{r}: {b}
(i0.1)
Although it certainly representsa soundway to solve such systems,it becomesinefficienl
when solving equationswith the sarnecoefficients[A], but with different right-hand'side
constants{b}.
| [n the parlanceof numericalmethods,the tenns "factorization"and "decotttposition"are synonyrnous.
Tobe
consistentwith the MATLAB doculnentation,we havechosento employ the terminology LU factori:ationlor
the subiectof this chapter.Note that LLl tlecompositionis very comntonly usedto describethe sameapproach
OVERVIEW
OF [U FACTORIZAIION
Recallthat Gausseliminationinvolvestwo steps:fbrwardeliminationand back substitution(Fig. 9.3).As we learnedin Section9.2.2,the forwald-elimination
stepcompriscs
the bulk of the conrputational
etlbrt.This is particularlytruefor largesystemsof equations.
LU factoriz.ationmethodsseparatethe time-consumingelimination of the matrix [A]
from the rnanipulationsof the right-handside {b}. Thus, once [A] has been "factored" or
"decornposed,"
multipleright-hand-side
vectorscan be evaluatedin an efTicientmanncr.
Inter-estingly.
Gausseliminationitselfcanbe cxpressed
as an LU facrorizttlion.
Bcfole
shclwinghow this can be done.let us lirst providea mathernatical
oven,iewof the factorizationstrategy.
I O.I
OVERVIEWOF LU FACTORIZATION
Just as was the casewith Gausselimination.LU factorizationrequirespivotingto avoid
divisionby zero.However,to simplifythe followingdescription,
we will omit pivoting.In
addition,the fbllowing explanationis limited to a setof threesimultaneous
equations.
The
resultscan be directlyextencled
to ir-dimensional
systems.
Equation( 10.I I can be rearranged
to give
l A l { r } - { b }: 0
( 1 0 . 2)
Supposethat Eq. ( 10.2)could be expressed
as au uppertriangr"rlar
system.For example,fbr
a3x3svstenr:
['l
Ll12 ,r':
Lltt
0
l/ rr
I
:1t_
I
,r,.-l
..,f-
d1
dt
dz
t 0.3)
Recognizethat this is similar to the manipLrlation
that occursin the first step of Gauss
elimination. That is, elimination is used to reducethe system to upper triangular form.
Equation(10.3)can alsobe expressed
in rnatrixnotationancirearranged
give
11'r
t u l { . r -} { d }: 0
(r 0 . r )
Now assumethat thereis a lower diagonalnatrix witl.rl's on the diagonal,
t/l-
tLt
Ir
-
0 0l
li.,
I
0l
( r0 . 5 )
Ll, /r: l l
that has the propertythat when Eq. (10.a)is premultipliedby it, Eq. (10.2)is the resulr.
That is.
t t l I t u ] { , r-} { c t l l : t A l { r } - [ b ]
(10.6)
If this equationholds,it follows fiom the rulesfor matrixnultiplicationthat
lLllul: lAl
(r 0 . 7 )
ILlldj : lbl
(10.8)
and
238
IU FACTORIZATION
tAl {.r}: {ll}
tut f..torirution
A
{ lul
lLl
I
tLl
._-
{d}: {b}
I
V
{d}
I
.lul
Substitution
V
ir) : trl)
,".0
I
t
{ri
|,o
F I G U R EI O . I
ihe stepsin 1Uloctorizotion.
A two-stepstrategy(seeFig. I 0. I ) for obtainingsolutionscan be basedon Eqs.( 10.3),
( 1 0 . 7 )a, n d( 1 0 . 8 ) :
1.
2.
LU factorizationstep. [A] is factoredor "decomposed"into lower [L] and upper[U]
triangularmatrices.
Substitutionstep.[t] and [U] areusedto determinea solution{x } for a right-hand
side
aninter{b}. This stepitselfconsistsof two steps.First,Eq. (10.8)is usedto gerrerate
mediatevector{z/}by forward substitution.Then,the resultis substitutedinto Eq.(10.3)
which can be solvedby back substitutionfbr {x }.
Now let us shorvhow Gansselinrinationcanbe imnlementedin this wav.
I o.2 GAUSSETIMINATIONAS [U FACTORIZATION
Although it might appearat face value to be unrelatedto LU factorization,Gausselimination can be usedto decompose[Al into [Ll and IU]. This can be easilyseenfor [U], which
is a direct product of the fbrward elimination.Recall that the forward-eliminationstepis
intendedto reducethe original coefficientmatrix [A] to the form
ot' ac 4rr -l
f
,I
a i , ar.,
lUl:10
I
t--
L0
;;l
0 ass
J
whiclr is in the desireduppertriangularformaf.
ri
AS IU FACTORIZATION
I O.2 GAUSSELIMINATION
Though it might not be as apparent,the matrix [L] is also producedduring the step.
This can be readily illustratedfor a three-equationsystem,
f a r r o t z . 7 1I3 -rl
I o., a22 ott I r r
1o.; aj2 ar )
_ti.:
l;:
The first stepin Gausselimination is to multiply row I by the f'actor[recall Eq. (9.9)]
0il
Jzt:-
0)l
and subtractthe result from the secondrow to eliminatea.,. Similarly,row I is multiplied by
A2t
| 1t Ltll
and the result subtractedfiom the third row to eliminatea,,. The final stepis to multiply
the modified secondrow by
..
t..-| 1: -
a\,
azz
and subtractthe result from the third row to eliminatea-,,.
Now supposethat we merely perfbrm all these manipulationson the matrix [A].
Clearly,if we do not want to changethe equations,we alsohaveto do the sameto the righthand side {b}. But thereis absolutelyno reasonthat we have to perform the manipulations
Thus,we could savethe/'s and manipulate{b} later.
simultaneously.
Recall that the whole idea behind the
Where do we store the factors/,,,J.r, andf...r'?
elirninationwas to createzerosin at1,a,,t,and a3,.Thus,we can storerr in a.,,f., in a.r,
andl,, in a.,,.After elimination,the [A] matrix can therefbrebe written as
l - . , ',
If,
{/lt apl
ur or,I
(r0.10)
L,t , .fp ai:,)
This matrix, in fact, representsan efficient storageof the LU factorizationof [Al,
lAl -
(10.1l)
tLltul
where
f
lUl :10
on
a12 n',
T
a ' r , a \ , ,1
(r0.r2)
0 0l
1 0l
0 0 .r 3 )
L o t o;1.)
and
[r
7rl:lft
L .fsr .f:z 1I
The following exampleconfirms that [A] : tLl[n.
I.UFACTORIZATION
E X A M P L El 0 . l
l U F o c t o r i z o t i o nw i t h G o u s s E l i m i n o t i o n
Problem Stqtement. Derive an LU factorizationbasedon the Gauss eliminationoerformed previously in Example9.3.
Solution. ln Example 9.3, we used Gausselimination to solve a set of linear algebraic
equationsthat had the following coefficientmatrix:
-0.1 -0.21
[ 3
-0.3
7
lAl:10.1
1
0
.
2
r0 I
10.3
After forward elimination,the fbllowing uppertriangularmatrix was obtained:
-0.r
-0.2
[3
I
0
:
1
0
7 . 0 0 3 3 3 . 2 9 3 3 313
tul
1 0 . 0 1 2I0
0
L0
The f'actorsemployedto obtain the uppertriangularmatrix can be assembledinto a lower
triangularmatrix. The elementsd21and d11were eliminatedby using the factors
. f u: ,
0.1
:0.0333333 /,, :,
0.3
:0.1000000
and the elementd12woSeliminatedby using the factor
-0.19
.f., :
7J0333
: -0.0271.100
Thus. the lower triangularmatrix is
1
0l
0
[
r
0l
tll :10.0333333
L 0.100000-0.0271300r J
Consequentlv. the LU factorizationis
tI -
I
0
otT3
tt
-0.1
-0.2 I
I
r
0 | l 0 7 . 0 0 3 3 3- 0 . 2 9 3 3 3| 3
[A):[LilU):10.033333]
1 0 . 0 1 2 I0
0
1 0 . 1 0 0 0 0 0 - 0 . 0 2 7 1 3 0 0l l L 0
Thisresultcanbe verifiedby performingthemultiplication
of [L][L/]to give
[
3
L
0.3
-0.1
lLltul:l}.jeeeeee 7
-0.2
-0.3 1
I
-0.2 9.99996
)
wheretheminordiscreoancies
aredueto roundoff.
Afier the matrix is decomposed.
a solutioncan be generatedfor a panicularright-handside vector {b}. This is done in two steps.First, a forward-substitutionstepis executedby
solving Eq. (10.8)for {d}. It is importantto recognizethat this merely amountsto performside
ing the eliminationmanipulationson {bl. Thus, at the end of this step,the right-hand
EXAA
IO.2 GAUSSELIMINATION
AS TUFACTORIZATION
will be in the samestatethat it would have beenhad we performedforward manipulation
on [,4]and {}} sirnultaneously.
The fbrward-substitutionstepcan be representedconciselyas
i-l
s-, ,
tl,:fi, Ll,ibi
fori :
1.2.....n
j:l
The second step then merely amounts to irnplementingback substitutionto solve
Eq. ( 10.3).Again, it is importantto recognizethat this is identicalto the back-substitution
phaseof conventionalGausseliminarionlcomparewith Eqs.(9.l2) and (9.l3)l;
t,
:
drf Q,,,
il
d; -
u11x1
|
j:i+1
'fl:-uii
,l0.2
EXAMPLE
T h e S u b s t i t u t i oS
n teps
Problem Stotement' Completethe probleminitiateciin Example10.1 by generatingthe
final solution with forward and back substitution.
Soluiion. As just stated,the intent of forward substitutionis to imposethe elimination
manipulationsthat we had formerly appliedto [A] on the right-hand-sidevector {b}. Recall
that the systembeingsolvedis
-0.r -92lJ,yr
| - 3|. 7 - 0 . 3 . , I [ 7 . 8 s I
l0 -0.2
l l l : l - l e . . rI
Lo.:
r o I [ * r | | t r . +J
andthattheforward-elimination
phaseof conventional
Gausseliminationresultedin
T3
-0.1
-0.2
(
7.85
tfxr I
-02e3333
: | -'i?i'tI
7oo,i3
I.
|
Ilj
l
0
t0.0lt0 lrr
L0
zo.os+:
-J
l
I
J
The forward-subsriturion
phaseis implementedby applyingEq. (10.g):
o,J..,..,
? 3l{:l
fo
L 0 . 1 0 0 0 0 0 - 0 . 0 2 7 1 3 0 0I J [ , 1 r
7.85
-19.3
1t.4
or multiplying out the left-handside:
:
d1
7.85
0.0333333dr+
:-19.3
d2
0.100000dr-- 0.0271300d2+ d3 11.4
We cair solve the tirst equation for d, : 7.85, which can be substitutedinto the second
equationto solve for
d : .:
-19.3 * 0.0333333(7.85
: ) -19.5617
242
TUFACTORIZATION
Bothd, andd, canbe substituted
into thethirdequation
to give
: 70.0843
dt : 7 1.4- 0.1(7.85)+ 0.02713(-19.,5617)
Thus.
: {;r*y
{,/}
This resultcan thenbe substituted
into Eq. (10.3),IU]{.r} : {di:
-0 .r
13
-0 .2
I
xl
0 7.00333 -0.2e3333
|
|
0
1 0 . 0 1 2 0)
L0
;#*vl
whicl.rcanb e s o l v e db 1 h a c ks u b s t i t u t i o(see
n Example9.3 for details)for the final solution:
I :. I
{d\ :
I t o,io''..
I
The LU factorizatioualgorithmrequiresthe sametotal flops as for Gausselimination.
The only differenceis that a little lesseftbrt is expendedin the factorizationphasesincethe
operationsare not appliedto the riglrt-handside.Conversely,the substitutionphasetakesa
little more eflbrt.
lO.2.l MATTABFunction:ru
MMLAB
syntax:
has a built-in function lu that generatestheLU factorization.It has the general
lL,t-t)
=
lu (X)
where I and u are the lower triangularand uppertriangularmatrices,respectively,derived
fron-rthe LIJ factorizalionof the matrix -x.Note that this function usespartial pivotingt0
avoid division by zero.The following exampleshowshow it can be employedto generate
both the factorization and a solution fbr the same problem that was solved in Examp l e s 1 0 . 1a n d 1 0 . 2 .
E X A M P L E1 0 . 3
t U F o c t o r i z o t i o nw i t h M A T L A B
Problem Stotement. Use MATLAB to compute LheL(l factorization and find the
solutionfor the samelinear systemanalyzedin Examples10.I and 10.2:
-0r
f .r
I or
Inr
-_02llri
1 2 . 8 s1
7 -o.ill,, |l: l-ro.:
I
_o1 ro ll*,
I
ltt.ql
GAUSSELIMINATION
AS IU FACTORIZATION
Solution. The coetlicientmatrix and the right-hand-side
vectorcan be enteredin standard fashion as
>> A = t3 -.1
.2;.I
>r b = 17.85; -19.3;
1 .3;.3
'lI.4l;
-.2
I0);
Next, the LU factorizationcan be computedwith
L-
1.0000
0.0333
0 . 1 ( 10 0
0
1.0000
-t.r.a27I
0
0
1.0000
3.01r00
a
0
0.1000
1.0033
0
-0.2000
0.2933
t].ai2a
This is the sameresultthat we oblainedby hand in Example 10.1. We can testthat it is correct by computingthe original matrix as
>> L*LT
3. C)001r' -0. iL'00
1.0000
0.1004
0.2000
0.3000
-0.2000
0.3000
10.0000
To generate the solution. we flrst cornpute
>> d = L\b
d=
?.8500
19 . 5..11
70.0843
And then Llsethis result to compute the solution
>> x = U\d
3.0000
2.5000
7.0000
These results conlbrm to those obtained by hand in Example 10.2.
244
IU FACTORIZATION
FACTORIZATION
I O.3 CHOTESKY
Recall from Chap. 8 tlrirta s.vmmetricnratrix is one whereeij : aji for all i andj. Inother
rvords,[Al : IA)r. Such systemsoccLlrcommonly in hoth mathematicaland engineering/
scienceproblemcontexts.
Specialsolution techniquesare availablefbr such systems.They offer computational
advantagesbecauseonly half the storageis neededand only half the computation
timeis
requiredfor their solution.
One crf the most popular approachesinvolves Choleskt .factoriz.ntion(alsocalle/
Choleskydecornposition).
This algorithmis basedon the fact thata syrnmc'tric
matrix
can
be decomposed.
as in
tr o.u)
l A l :l u fw l
That is. the resultingtriirn-eular
factorsare the transposeof eachother.
The termsof Eq. ( 10.l4) ciln be rnultipliedout and setequalto eachother.Thefactotization can be generatted
efTicientlyby recurrencerelations.For the lth row:
(l0.rt
1l
ai1 -
upltl11
|
1._l
forj-i+1,...,n
'
EXAMPLE
10.4
(r0.r6)
tlii
CholeskyFoctorizotion
ProblemStotement.Compute
theCholesky
factorization
for thesymmetric
matrix
I A t -l?rt
1.5 s5
I
5.5 22s
I
1 5 5 225 919)
Ib
I r-5
Solution. Forthefirstrow(i : l). Eq.(10.15)
is ernployedtocompute
u11:Jan:16:2.44949
Then.Eq.(10.l6) canbeusedto deten.nine
otz
q
tt
-
Llll
at3
.r l.r -
L ll l
l-5
:6.123124
2.44949
5-5
:22.45366
2.44949
For-thesecondrow (l : 2):
: ,f a.. - rrl, : ,/55
1r.2
: 4.1833
(6.12372q2
EXAM
FACTORIZATION
I 0.3 CHOLESKY
423 -
245
225- 6.| 23124(22.15366)
:10.9165
4 .I 8 3 3
Ul2ut3
Ll22
F o r t h e t h i r dr o w ( i : 3 ) :
u . r : , fo . .- , i t * u l . : @ : 6 . 1 l o l o l
Thus, the Choleskyfactorizationyields
tul :
r 1 JJq4q
t-
|
L
6 I 11711
4.1833
11 457661
|
2 0 . 9 1 6 5|
6 .I l 0 l 0 l I
The validity ofthis factorizationcanbe verified by substitutingit and its transposeinto
Eq. (10.l4) to seeif theirproductyieldsthe originalmatrix [Al. This is leflforan exercise.
After obtaining the factorization,it can be used to determinea solution fbr a righthand-sidevector {b} in a mannersimilar to LU factorization.First, an intermediatevector
{d} is createdby solving
IUlr {d} : {b}
(10.17)
Then,the final solutioncan be obtainedby solving
l u l { x }: l d }
lO.3.l
(r0.r8)
MATTAB Funcfion: cuot
MATLAB has a built-in function chol that generatesthe Choleskyfactorization.It hasthe
generalsyntax,
t/ - chol (x)
where y is an uppertriangularmatrix so that t/' * t/ = r. The following exampleshowshow
it can be employedto generateboth the factorizationand a solutionfor the samematrix that
we looked at in the previousexample.
EXAMPLE
I O.5 Cholesky Foctorizotionwith MATLAB
Problem Stotement. Use MATLAB to computethe Choleskyfactorizationfor the same
matrix we analyzedin Example10.4.
[6
t A l: l l s
155
ls ssl
ss 22sl
2 2 5 9 1 9J
Also obtain a solutionfor a right-hand-sidevector that is the sum of the rows of [A]. Note
that for this case.the answerwill be a vector of ones.
246
TUFACTORIZATION
The matrix is entered in standard fashion as
Solution.
:'> A =
f 6 l5
55;
15 55 225;
55 225 919);
A right-hand-side vector that is the sum of the rows of [A] can be generatedas
>> b =
fsum(A(1, :))/'
sum\tt(2,:))
;
sum(A(3,:)ll
16
295
7259
Next, the Cholesky tactorization can be computed with
>> U = chol(A)
u=
6 . r23t
4.1813
0
2 .4495
0
0
22 .45,31
2[).91{,.5
6.1101
We can test that this is correct by computing the original matrix as
>> U' *IJ
6.0000
15.0000
55.0000
15.0000
55.0000
225.0000
55.0000
225.0000
979.0000
To generate the solution. we flrst conrpute
>> d = A'\b
d=
3r.0269
25.0998
6.1101
And then use this result ti'r compute the solution
l0.l Dete
of equatio
tion, and(
versionof
10.2 Use
E q s (. 1 0 .
10.3 Use
systemac
10.r1-3xr xrj
>>x=A\y
Then,mul
minethat I
10.4Use
Prob.10.3
thesystem
r.0000
1.0000
1.0000
lblr :
I O.4 MATTABIEFTDIVISION
We previouslyintroducedleft division without any explanationof how it works.Nowthu
we have some backgroundon matrix solution techniques,we can provide a simplifid
-t^.,^-:^ti
^^
^f
:*-
^-^-^ri.,-
10.5Solv
factorizat
2xr -3xr -
247
PROBLEMS
When we implement left division with the backslashoperator,MATLAB invokes a
highly sophisticatedalgorithm to obtain a solution. ln essence,MATLAB examinesthe
structureof the coefficient matrix and then implementsan optimal method to obtain the
solution.Although the detailsof the algorithmarebeyondour scope,a simplified overview
can be outlined.
First, MAILAB checks to see whether [A] is in a format where a solution can be
obtained without full Gausselimination. These include systemsthat are (a) sparseand
banded,(b) triangutar(or easily transformedinto triangularform), or (c) symmetric.If any
of thesecasesare detected,the solution is obtainedwith the efTicienttechniquesthat are
availablefbr such systems.Some of the techniquesinclude bandedsolvers,back and forward substitution,and Choleskyfactorization.
If none of thesesimplified solutionsare possibleand the matrix is square.'a general
triangular factorizationis computed by Gausselimination with partial pivoting and the
solution obtainedwith substitution.
2 It should be noted that in the event that
solutionis obtainedwith an approach
[Al is not square,a least-squares
c allecJQR .fac t or i zati on.
PROBTEMS
the total flops as a function of the number
l0,l Determine
equations
n for the (a) factorization,(b) forward substituand(c)back substitutionphasesof the lU factorization
of Gausselimination
Usethe rules of matrix multiplication to prove that
(10.7)
and(10.8)follow from Eq. (10.6).
Usenaive Gauss elimination to factor the following
to the descriptionin Section 1 0 . 2 :
according
27
l2rz- xt:
- 6rz + 2x',: -61 .5
I x z + 5 a : . : - 2 1. 5
multiplythe resulting [L] and [{l matricesto deterthat[A] is produced.
UselU factorizationto solvethe systemof equationsin
10.3.Showall the stepsin the computation.Also solve
svstem
for an alternativerisht-hand-side vector
Ibf = Lt2 18 - 6)
Solvethe following system of equations using LU
with partial pivoting:
2x1-6x2- 13:-38
-3.r1- -r2*7x3 : -34
-Rr, -L
-)v.
-
-)O
10.6 Developyour own M-file to determinerheLU factoization of a squarematrix without pafiial pivoting. That is, develop a function that is passedthe squarematrix and returns
the triangular matrices [Ll and [U]. Test your function by
using it to solve the systemin Prob. 10.3.Conlirm that your
f'unctionis working properlyby verifying that [L][Ul : tA]
and by using the builrin function 1u.
10.7 Confirm the validity of the Cholesky factorizationof
the resultsinto Eq. (10.14)to
Example10.4by substituting
verify that the productof l L/17and l Ll yieldslA l.
l0.tl (a) Perform a Choleskyfactorizationof the fbllowing
symmetricsystemb1 hand:
tl ilill{i:}:t*l
(b) Verify your hand calculation with the built-in cnol
function. (c) Employ the resultsof the factorizationIL4 to
determinethe solutionfor the right-hand-sidevector.
10.9 Develop your own M-file to determinethe Cholesky
factorizationof a symmetric matrix without pivoting. That
is, develop a function that is passedthe symmetric matrix
and returnsthe matrix [tIl. Test your function by using it to
solve the systemin Prob. 10.8 and use the builrin function
is working
248
IU FACTORIZATION
10.10 Solve the following set of equationswith LU factorization:
3 x 1- 2 x 2 * . r r : - 1 0
Z.rt]_6xz-4x7:
factorizationto determineIU] sothat
10.13UseCholesky
LA): W)rlul:
44
[2
l-r
-1 0l
2 ,tl
Lo -r
2)
- - x 1- 2 t 2 * 5 . 1 3 : - 2 6
1().14 Computethe Choleskyfactorizationof
10.U (a) Determine the LU factorizationwithout pivoting
by hand for the fbllowing matrix and check your resultsby
validatingthat [L][U] : [A|.
1l
[8 2
7 2l
l3
L2 -r e_.j
0l
Lo o 4-l
Do your results make sensein terms of Eqs. (10.15)and
0 0 . 16 ) ?
(b) Employ the result of (a) to compute the determinant.
(c) Repeat(a) and (b) using MAILAB.
10,12 Use the following LU factorizalion to (a) compute
tl.redeterminantand (b) solve [Al{r} : {b} with {b]r:
L- r0 44 ,26).
lAl:tLlfUl:
[ e 2o, s o l
IAl:10
[1
|I 0 . 6 6 6 7
I
L -0.3333 - 0 . 3 6 3 6 , ]
'
[3
L
-2
-^.lrr*l
1.3333
3.6364)
Motrix Inverseond Condition
CHAPTEROBJECTIVES
The primary objectiveof this chapteris to show how to computethe matrix inverse
and to illustratehow it can be usedto analyzecomplex linear systemsthat occur in
engineeringand science.In addition,a methodto assessa matrix solution'ssensitivity
to roundofTerror is described.Specificobjectivesand topicscoveredare
o
r
r
o
r
t.l
Knowing how to determinethe matrix inversein an efficient mannerbasedon lU
factorization.
Understandinghow the matrix inversecan be usedto assessstimulus-response
characteristicsof engineeringsystems.
Understandingthe meaningof matrix andvectornormsandhow they arecomputed.
Knowing how to use norms to computethe matrix conditionnumber.
Understandinghow the magnitudeof the conditionnumbercan be usedto
estimatethe precisionof solutionsof linear algebraicequations.
THEMATRIXINVERSE
In our discussionof matrix operations(Section8.1.2), we introducedthe notion that if a
matrix [A] is square,thereis anothermatrix [A]-r , called the inverseof [A], for which
t A l t A l - ': [ A ] - r t A l: t / l
(r1.1)
Now we will focus on how the inversecan be computednumerically.Then we will explore
how it can be usedfor engineeringanalysis.
I l.l.l
Colculoting the Inverse
The inversecan be computedin a column-by-columnfashionby generatingsolutionswith
unit vectorsas the right-hand-sideconstants.For example,if the right-hand-sideconstant
250
MATRIXINVERSE
AND CONDITION
has a I in the tirst positionand zeroselsewhere,
tt .h' t\ --
s)
the resultingsolutionwill be the first column of the matrix inverse.Similarly,if a unitvector with a I at the secondrow'is used
[b]:{
f0l
l}
{il,1}
tol
the resultwill be the secondcolumnof the matrix inverse.
The bestway to implementsucha calculationis with LU factorization.Recallthatone
of the greatstrengthsof LU factorizationis that it providesa very efficientmeansto evaluate multiple righrhand-sidevectors.Thus, it is ideal fbr evaluatingthe multiple unitvecton
neededto computethe inverse.
E X A M P L EI L l
MqtrixInversion
Problem Stotement. Employ LU factorizationto determinethe matrix inversefor the
systemfrom Exaurple10.l:
-or
-o)l
I l
t-""-l
A l : 1 0 . 1 7 _ 0 . 31
10.3 -0.2 10 _.1
Recall that the lactorizationresultedin the following lower and uppertriangularmatrices:
[r
-o.r
-0.2 I
I
t
-0.293333
7.00333
tul:10
1
1 0 . 0 1 2_0j
0
L0
o
o'l
1
0l
ttl:10.0333333
L 0 . 1 0 0 0 0 0- 0 . 0 2 7 1 3 0l J0
Solution. The tirst column of the matrix inversecan be determinedby perfbrmingthe
forward-substitutionsolution procedurewith a unit vector (with I in the first row)asthe
right-hand-sidevector.Thus, the lower triangularsystemcan be setup as (recallEq.[10,8])
[r*-0.1,,.ui]
{l}:{i}
-0.03333 -0.10091. Thisvector
and solvedwith forward substitr.rtion
for {d}r : ll
can then be used as the right-handside of the upper triangularsystem(recallEq. [0.3]):
[:
l0
lo
-or
-02
l[.]
7 . 0 0 3 3 . -10 . 2 e 3 3 3l.l1. : l : l
o
r o . o r 2I [o' , J
[
r
I
0.0.133
13
[-orooeJ
which can be solvedby back substitutionfor {.r}r : 10.-1-1249-0.00518 -0.01008j
which is the first column of the u.ratrixinverse:
f o.zzz+so o-l
l A l ' : l - o . o o s r so o l
I
nntooR
n
n I
THEMATRIXINVERSE
'lllil:{:}
To determinethe secondcolumn, Eq. (10.8) is formulatedas
[ro
l
| 0.0333333
L 0.100000 -0.027r 300
This can be solvedfor irl), and the resultsare usedwith Eq. (10.3)to determine{r}' :
10.004944 0.142903 0.002111,which is the secondcolumn of the matrix inverse:
[Al
':
ol
o.zzz+e
o.oo4s44
f -0.00sr8
0.142e030
|
|
L -0.010080.0027r00_j
Finally, the sameprocedurescan be implementedwith {b}/: l0 0 1l to solve fbr
, which is thefinal columnof thematrixinverse:
{r }7 : 10.006798 0.004l83 0.099881
t
03324so.oo4s44
0.006798l
[A]-' : | -0.00s18 0.1429030.004183
|
L -0.01008 0.0021100.0998801
Thevalidityof thisresultcanbechecked
by verifyingthat[A][A] ' : Ul.
| 1.t.2 Stimulus-Response
Computotions
As discussedin PT 3.1, many of the linear systemsof equationsarisingin engineeringand
scienceare derived from conservationlaws. The mathematicalexpressionof these laws
is some form of balanceequationto ensurethat a particularproperty-mass, force, heat,
momentum,electrostaticpotential-is conserved.For a force balanceon a structure,the
propertiesmight be horizontalor vertical componentsof the forcesacting on eachnode of
the structure.For a mass balance,the propertiesmight be the mass in each reactor of a
chemicalprocess.Other fields of engineeringand sciencewould yield similar examples.
A singlebalanceequationcan be written for eachpart of the system,resultingin a set
of equationsdefining the behavior of the property for the entire system.Theseequations
are interrelated,or coupled,in that eachequationmay include one or more of the variables
from the other equations.For many cases,thesesystemsare linear and, therefore,of the
exactform dealt with in this chapter:
[A]{x}: {b}
(11.4)
Now, fbr balanceequations,the terms of Eq. ( I 1.4) have a definite physical interpretation. For example,the elementsof {x} are the levels of the property being balancedfor
eachpart of the system.In a fbrce balanceof a structure,they representthe horizontaland
vertical forcesin eachmember.For the massbalance,they arethe massof chemicalin each
reactor.In either case,they representthe system'sstateor response,which we are trying to
determine.
The right-hand-sidevector {b} containsthose elementsof the balancethat are independentof behavior of the system-that is, they are constants.In many problems,they
representthe
or externalstimuli that drive the system.
252
MATRIXINVERSE
AND CONDITION
Finally, the nratrix of coeflicients [lJ usui,illycontains the ;xrrameter.rthatexpres
how the parts of fhe systernirte ract or are coupled. Consequently,Eq. ( I I .4) mi-qht
h
reexpressed
as
I I n t e r a c t i o n s l { r e s p o n: s e{ }s t i m u l i }
As we know from plevious chapters,there are a variety of ways to solveEq.(11.4).
However.using the matrix inverseyielclsatparticularlyinterestingresult.The fbrmalsolution can be expressed
as
h : l: [ A l
]{1r}
or (r'ecallingour detlnitionof ntatrixmultiplicationfrom Section8.I .2)
.\'t :
L
t,
L
{lt I /)l -f {/l, D. -f rlt l /)r
.\'J :
I
(l)t l)l
. \ ' . r:
lr
l/rl l)t +
f
l,
r,
rr.. lr. -f rt.. l.r1
l,
{l{. lrl +
L
(lr-t /tt
Thus, we lind that the invertedmatrix itself',asidetiom providing a solution,hasextrernelyuseful properties.That is, eachof its elementsrepresentsthe responseof asingle
part of the systemto a unit stimulusof any otherpart of the system.
Notice that theselbrmulationsare Iinearand, theretbre,superpositionandproportionality hold. Supe.rltositirnmeansthat if a systemis subjectto severaldifTerentstimuli(the
b's), the responses
individuallyand the resultssummedto obtainatotal
can be corr-rputed
response.P roportionalin' meansthat multiplying the stimuli by a quantityresultsin there.
sponseto thosestimuli beingmultipliedby the sarnequantity.ThLrs,the coefficientour
is
a p r o p o r t i o n a l i t y c o r l s t a n t t h a t g i v e s t h e v a l udeuoef .tro-a1u n i t l e v e l o f l r s . T h i s r e s u l t r s
independentof the eft-ectsof b2 and 6: on ,r1, which are retlected in the coefficients
no' trnda,.', respectively.
Therefore,we can draw the generalconclusionthattheelement
a,,toftheinvertedmatrixrepresentsthevalueofx,cluetoaunitquantityofb,.
Usin-ethe exanrpleof the structure,elementrr,l' of the matrix inversewouldrepresent
the fbrce in member i due to a unit externalforce at node1. Even for sntal.lsystems,
such
behaviorof individualstimulus-response
interactions
would not be intuitivelyobvious.
As
such.the matrix inverseprovidesa powerful technique1brunderstandingthe interrelationshipsof componentpartsof complicatedsystems.
E X A M P LI E
I.2
A n o l y z i n g t h e B u n g e eJ u m p e rP r o b l e m
Problem Stotement. At the beginningof chap. 8, we set up a probleminvolvingrhree
individualssuspendedvertically connectedby bungeecords.We deriveda s)'stemof linear
algebraicequationsbaseclon fbrce balancestilr eachjuniper,
-roo o
I - rso
r 0 0 r s O - s 0I
|
L 0
--50 s0 I |
I,I _
-ss8.6
I
;;I- etiil
In Example 8.2, we used MATLAB to solve this system for the vertical positionsof the
jumpers(ther's). In the presentexample,useMATLAB to computethe matrixinverseand
I I . 2 E R R OA
RN A L Y S IASN D S Y S T E M
CONDITION
253
Solution. Start up MAILAB and enterthe coefficienrmatrix:
t150 -100 0; 100 150
>> K -
50;O -50 501;
The inversecan then be computedas
>>
Kf
KI
=
=
inv(K)
0.0200
0.0200
0.0200
0.0200
0.0300
0.0300
0.0200
0.0300
0.0500
Each elementof the inverse,k, I of the invertedmatrix representsthe vertical change
in position (in meters)of jumper I due to a unit changein fbrce (in Newtons) applied to
Jumper/.
First, observethat the numbersin the first column ("1: l) indicatethat the positionof
all threejumpers would increaseby 0.02 m if the force on the first jumper was increased
by 1 N. This makessense,becausethe additionalforce would only elongatethe first cord
by that amount.
ln contrast,the numbersin the secondcolumn (j :2) indicatethat applying a force
of I N to the secondjumper would move the first jumper down by 0.02 m, but the second
and third by 0.03 m. The 0.02-m elongationof the first jumper makes sensebecausethe
first cord is subjectto an extra I N regardlessof whetherthe force is appliedto the first or
secondjumper. However, for the secondjumper the elongationis now 0.03 m because
along with the first cord, the secondcord alsoelongatesdue to the additionalforce.And of
course,the third jumper showsthe identicaltranslationas the secondjumper as thereis no
additionalforce on the third cord that connectsthem.
As expected,the third column (..j:3) indicatesthat applying a force of I N to the
third jumper resultsin the first and secondjumpers moving the samedistancesas occured
when the force was appliedto the secondjumper. However,now becauseof the additional
elongationof the third cord, the third jumper is moved fartherdownward.
Superpositionand proportionalitycan be demonstratedby using the inverseto determine how much fartherthe third jumper would move downwardif additionalforcesof 10,
50, and 20 N were appliedto the first, second,and third jumpers,respectively.This can be
done simply by using the appropriateelementsof the third row of the inverseto compute,
A r - j : / . 3 r r A F r+ k r j a f ' 2 + k ; r A F r : 0 . 0 2 ( 1 0 )+ 0 . 0 3 ( 5 0 )+ 0 . 0 5 ( 2 0 ): 2 . J m
II,2
ERRORANALYSISAND SYSTEMCONDITION
Aside tiom its engineeringand scientificapplications,the inversealsoprovidesa meansto
discernwhether systemsare ill-conditioned.Three direct methodscan be devisedfor this
purpose:
l.
Scalethe matrix of coefficients[A] so that the largestelementin eachrow is l. Invert
the scaledmatrix and if there are elementsof [A] | that are severalordersof magniq\/{fcm
254
MATRIXINVERSE
AND CONDITION
2.
3.
Multiply the inverseby the original coefficientmatrix and assesswhethertheresultis
closeto the identity matrix. If not, it indicatesill-conditioning.
Invert the invertedmatrix and assesswhetherthe resultis sufficientlycloseto theorig.
inal coefficientmatrix. If not, it againindicatesthat the systemis ill-conditioned.
Although thesemethodscan indicateill-conditioning,it would be pret'erable
to obtail
a singlenumberthat could serveas an indicatorof the problem.Attemptsto formulate
such
a matrix condition numberare basedon the mathematicalconceDtof the norm.
| 1.2.1 Vector qnd Motrix
Norms
Anonn is a real-valuedfunction that providesa measureof the size or "length"of multi.
componentmathenaticalentitiessuchas vectorsand matrices.
A simple exampleis a vectorin three-dimensional
Euclideanspace(Fig. l1.l) thatcan
be representedas
b
lFl:la
cl
where c, b, and c are the distancesalong the r, 1,,and z axes,respectively.The lengthof
this vector-that is, the distancefrom the coordinate(0, 0, 0) to (a, b, c)-can besimpty
computedas
lFll. : {/o\
h, + f
where the nomenclaturell F ll,, indicatet that this length is referred to as the Euclideannorn
of Ln.
Similarly, for an n-dimensionalvector lX,l :
would be computedas
t.
llXll": if
\
i
i:r
F I G U R EI I . I
Grophicoldepictton
of o vectorin Euclideon
spoce
l"r1 x2
.rnj. a Euclideannorm
ERROR
ANALYSIS
AND SYSTEM
CONDITION
The conceptcan be extendedfurther to a matrix [A], as in
ll.{l
tinll/
\- \-
-
/-J
/r--t
"? I
(il.s)
which is given a specialname-the Frobenius notm. As with the other vector norms, it
providesa singlevalueto quantifythe "size" of [A].
It slrouldbe notedthat therearealternativesto the EuclideanandFrobeniusnorms.For
vectors,thereale alternativescalledp norms that can be representedgenerallyby
/
,,
\ll
lt /iX
' t lt ll t - :l L ' 'l,\t -
",
1/'l
\,-l
I
/
We can seethat the Euclideannorm and the 2 norm,llX ll, , areidenticalfor vectors.
Otherimportantexamplesare (p : 1)
rYr
- \-r..r
i= |
whichrepresents
thenormasthesumof theabsolute
valuesof theelements.
Anotheris the
maximum-magnitude
or uniform-vector
norm(p : oo).
llXll-: max,l.ril
whichdefinesthe normastheelementwith thelargestetbsolute
vallle.
Usinga similarapproach,
normscanbe developed
for matrices.
For example.
llAllr : .maxu)-- .r;,1
l - i '- , 1
i-l
That is, a summationof the absolutevaluesof the coetlicientsis performedfor eachcolumn, and the largestof thesesummationsis taken as the norm. This is called the columnsum ,tot'tn.
A similar determinationcan be made for the rows. resultine in a unifbrm-matrix or
row-sumnorm:
l l A l l . : , l.aut a^n f
-
l,,,7l
1:1
It should be noted that, in contrastto vectors,the 2 norm and the Frobeniusnorm for
a matrix are not the same.Whereasthe Frobeniusnorm llAllf can be easily deterrninedby
Eq. ( t I .5),the matrix 2 norm llA llz is calculatedas
l lA l l : : ( / r n ' " * ) ' ' "
where4,,,"*is the largesteigenvalueof [A]r[A]. In AppendixA, we will learnmore about
eigenvalues.
For the time being,the importantpoint is that the llAll2. or spectralnorm, is
the mirrimum nonl and,therefore.providesthe tightestlreAsureof size (Ortega,1972).
11.2.2 Motrix ConditionNumber
Now that we have introducedthe conceptof the norm, we can use it to define
C o n d l A l: l l A l l ' l l l - ' l l
MATRIXINVERSE
AND CONDITION
where Cond[A] is called the nntix conditiort number Note that for a matrix [A], this
numberwill be greaterthan or equalto l. It can be shown (Ralstonand Rabinowitz,1978:
Gerald and Wheatlev.1989)that
l^xll
,.. .ll^Ail
= condlAl
il"il
Mil
That is, the relative error of the norm of the computedsolutioncan be as large astherelative error of the norm of the coefficientsof [A] multiplied by the conditionnumber.Forexarnple,if the coefficientsof [A] are known to t-digit precision(i.e.,roundingerrorsare0n
the order of 10-r) and Cond[Al : 10', the solution[X] may be valid to only r - cdigits
(roundingelrorsry l0' -' ).
E X A M P L EI 1 . 3
M o i r i x C o n d i t i o nE v o l u o t i o n
Problem Stotement. The Hilbert matrix,which is notoriouslyill-conditioned,canberepresentedgenerallyas
[r
I
+
1
'i l
lt l,
I
4
,+r I
;
l,
I
ll l: : : : l1
l -
i
-
Lfl
I
-
nrl
- l
nll
ltl
I
I
tJ
Use the row-surnnorm to estimatethe matrix conditionnumberfor the 3 x 3 Hilbertmatrix;
t-,1
lr i i'
t A' l : l l{t
tl
L3
I. \ rI II
I
I
ll
5l
Solution. First.the matrix canbe normalizedso thatthe maximumelementin eachrowis1:
rAl:
il
iJ
fr1
|
Ll
Summing each of the rows gives L833, 2.1667, and 2.35. Thus, the third row hasthe
Iargestsurnand therow-sumnorm is
31
ll.4ll-:l+;+;:2.35
+i
The inverseof the scaledmatrix can be computedas
I g
-18 10l
l
r) b - b u l
| _ . -e0
60 -l
L 30
lAl-l:l-tr'
Note that the elementsof this natrix are larger than the original matrix. This is also reflected in its row-sum norrn.which is comoutedas
EXAM
AND SYSTEM
CONDITION
ANALYSIS
I 1.2 ERROR
257
Thus. the condition numbercan be calculatedas
Cond[,A]: 2.35(192\: 451.2
The fact that the condition number is much greaterthan unity suggeststhat the system
is ill-conditioned.The extentofthe ill-conditioningcan be quantifiedby calculatingc log
rounding
could
exhibit
of
the
solution
:2.65.
digits
Hence,the last threesignificant
451.2
errors. Note that such estimatesaln-rostalways overpredict the actual error. However, they
are usefulin alertingyou to the possibilitythat roundoff elrors may be significant.
| 1.2.3 Norms ond Condition Number in MATLAB
MATLAB has built-in functionsto computeboth norms and conditionnumbers:
>> norm (X, p)
ano
._ cond{X,p)
where x is the vector or matrix and p designatesthe type of norm or condition number(1 , z ,
inf , or ' f ro'). Notethatthe cond functionis equivalentto
>> norm(X,p)
*
norm(inv(X)
,P)
setto 2'
Also,notethatif p is omitted,it is automatically
I 1.4 MotrixConditionEvoluotion
with MATLAB
EXAMPTE
Problem Siotement. Use MAILAB to evaluateboth the norms and condition numbers
for the scaledHilbert matrix previously analyzedin Example 11.3:
i2 r. rl l
['
l^
tAr:lr I + |
t_-l
lr
L'
I
4
tl
sl
(c) As in Example I 1.3, first computethe row-sum versions(p : inf ). (b) Also compute
the Frobenius(p :' fro' ) andthe spectral(p : 2)conditionnumbers.
Solution: (a) First,enterthe matrix:
>> 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<4 | isernpty (maxit ) , maxit=50; end
if nargin<3 | isempty(es), es=0. 011001;erLd
In,nl - si.ze(A) ;
if m-=n, error('Matrix
A nust be scluare'); end
for i = 1:n
= 0;
c(i,i)
= 0i
x(i)
end
x=x';
Fnr
i
-
l.n
= C(1,1;n)iA(i,i)
C(i,1:n)
end
for i = 1:n
= b(i)/A(i,i);
d(i)
end
i f ar
n.
while
(1)
-
xold
F^-
I
;
x;
_
1,-
= d(i)-C(i,:)*x;
x(i)
-- 0
if x(i)
- al.s((x(l)
ea(i)
encl
end
= rter+1;
rter
if max(ea)<-gS
iter
end
- xold(i))/x(i))
*
>= maxit,
end
break,
1-00;
I2.2
FIGURE
Mfilefo implemenl
MATLAB
Gouss-Seidel
The choice of a proper value for ). is highly problenl-specificanclis often detern.rined
empirically.For a singlesolutionof a setof equationsit is oftenunnecessary.
However,if
the system under study is to be solved repeatedly,the efficiency introdr-rced
by a wise
choiceof ), can be extremelyimportant.Good examplesarethe very largesystemsof linear
algebraicequationsthat can occur when solvillg partialdifferentialequationsin a variety of
engineeringand scientificproblemcontexts.
270
ITERATIVE
METHODS
12.2
EXAM
NONTINEARSYSTEMS
The following is a setof two sinrultalleous
nonlinearequationswith two unknowns:
"f+"'"2:lg
. r l + J . r t . r ; : - )/
(12.to)
(r26r)
In contrastto linear systemswhich plot as straightlines (recallFig. 9.l), theseequations
plot as curvesoD an .r2 VeTSUS
.r1graph.As in Fig. 12.3,the solutionis the inte$ection0f
the curves.
Justas we did when we determinedrootsfor singlenonlinearequations,
suchsystems
of equationscan be expressed
generallyas
. l i ( , t 1-,r 2 . . . . r , ) : 0
./:(xr,r:,.....r,,) O
-
(12,7)
J , ( r t , . r 2 . . . . .{ , ) : 0
Therefbre,the solutionare the valuesof the r's that make the equationsequalto zero.
12.2.1 SuccessiveSubstitution
A simple approachfbr solving Eq. (12.7) is to usethe samestrategythat was employed
for
fixed-point iterationand the Gauss-Seidelmethod.That is, eachone of the nonlinear
equa.
tions can be solved for one of the unknowns.Theseequatiollscan then be implemenled
iteratively to computenew valueswhich (hopefully) will convergeon the solutions.
Thil
irpproach,which is calledsrcce.rsive
substitution,is illustratedin tlre lbllowingexample.
FIGUREI2.3
Grcphicol depictionof the soluiic-,n
of irvo simultoneous
nor,lineorequoiions
. t 1+ J J t . \ ; = 5 /
I2.2 NONLINEAR
SYSTEMS
E X A M P L1 E
2.2
27r
s u c c e s s i v es u b s t i t u t i o nf o r q N o n l i n e o r s y s f e m
Problem Stotement. Use successive
substitutionto determinethe roots of Eq. (12.6).
Note that a correctpair of roots is.rl _ 2 ancl,rr : 3 Initiate
the computafionwith guesses
o f , r 1: 1 . 5a n d . f , z: 3 . 5 .
Solution. Equation(12.6a)can be solvedfbr
, t r l: -
l0 - "rri
( Er 2 . :I.)
xz
and Eq. (12.6b)can be solvedfor
12 : )/ - J.rl.yi
^ l
(E12.2.2)
On the basisof the initial guesses,Eq. (E12.2.1)can be
usedto determinea new value
ofxl:
1 0- ( 1 . - s ) 2
:2.21429
3.5
This resultand the initial valueof x2 - 3.5 can be substituted
into Eq. (F,12.2.2)todeter_
mine a new valueof 't2:
.yz: 5'7- 3(2.21429)(3.5)2
: -24.3i 516
Thus' the approachseemsto be diverging.This behavioris
even more pronouncedon the
seconditeration:
I0 - 12.21429t2
Lt
-
-0.20910
- 2 4 . 3 75 1 6 :
rz : 57 - 3( -0.20910)(-24.37 5 tO2 :
429.709
ObvioLrsly,the approachis deteriorating.
Now we will repeatthe conlputationbut with the original
equationsset up in a diff'erent fbrmat. For example,an alternativesolutionof Eq. ( I 2.6a)
is
.t' : ./iblT'r2
and of Eq. (12.6b)is
Now the resultsare more satistactory:
r, : yTo - 1.5t35 :2.1:,915
5 7- 3 . 5
/--:2.86051
y 3(2.17e4s)
272
ITERATIVE
METHODS
tr:@:1.94053
51 - 2.86051
: 3.04955
3(1.%0s)
Thus, the approachis convergingon the true valuesof "r1: 2 andxz : 3.
The previous example illustrates the most serious shortcoming of successive
substitution-that is, convergenceoften dependson the mannerin which the equations
are
formulated.Additionally, even in those instanceswhere convergenceis possible,divergencecan occur if the initial guessesare insufficiently close to the true solution.These
criteria are so restrictivethat fixed-point iterationhas limited utility for solving nonlinear
systems.
12.2.2 Newton-Rophson
Justas fixed-pointiterationcan be usedto solve systemsof nonlinearequations,otheropen
root location methodssuchas the Newton-Raphsonmethodcan be usedfor the sanepurpose.Recall that the Newton-Raphsonmethodwas predicatedon employingthe derivative
(i.e., the slope) of a function to estimateits intercept with the axis of the independent
variable-that is, the root. In Chap.6. we useda graphicalderivationto computethisestimate.An alternativeis to derive it fiom a first-orderTavlor seriesexpansion:
( x r * r ) : f ( x i ) * ( x i + r- x , \ f ' \ x i )
(128)
"f
wherex; is the initial guessat the root andx;11 is the point at which the slopeintercepts
the
.r axis.At this intercept,I (.ri*r) by definitionequalszeroandEq. (12.8)canbe reananged
to yield
l;-1 :
ri -
f (x')
tl29t
^l \xt )
which is the single-equationform of the Newton-Raphsonmethod.
The multiequationfonn is derived in an identical fashion.However, a multivariable
Taylor seriesmust be usedto accountfor the fact that more than one independentvariable
contributesto the determinationof the root. For the two-variablecase,a first-orderTaylor
seriescan be written for eachnonlinearequationas
f t . iv t : . f i . i - | ( . t 1 , ; * ,-
",,,1H
f z . i + t : , f 2 . , * ( x r . i + r- * r . , l Y f
0 f u' I ( x 2 . 1 . ,-1 - r' "r ; )
?xz
( x z . i + -r
dlt;
-\'r;) "-"
Exz
(12.l}a)
( 12.10i)
Justas for the single-equationversion,the root estimatecorrespondsto the valuesof .r1and
.d2,where/y .;11and .[.111eQualzero.For this situation,Eq. ( 12.l0) canberearranged
togive
(12.ltal
(t2.ttu
EXAA
Ir
NONLINEARSYSTEMS
I
I
's
Becauseall valuessubscriptedwith i areknown (they correspondto the latestguessor ap. 2 . 1 1 )i s a s e t o f t w o
p r o x i m a t i o n ) , t h e o n l y u n k n o w n s o r e " r l , ;a
- 1n-d1x 2 , i a 1 . T h u s , E q( 1
linear equations with two unknowns. Consequently,algebraic manipulations (e.g.,
Cramer'srule) can be employedto solvefor
"
3f2., -
t, .J).t
n
.{t.i+t :
-tl.i -
OX2
\J\,i
a
dx2
(12.12a1
\fi,i \fz,i _ \fi,i \fzl
0x10x2 3x2'dx1
3f ,,,
]fz,i
"
"
t^._
Jl.t
x2,i+t : x2,i -
"
t^,JZ,r
_
n
dXt
t,,_
Jt,r
q
r,Xt
(12.12b)
3fi,i 0fzl * 3fi,i 3fz.i
\xt
\xz
3x2 \xy
The denominator of eachof theseequationsis formally referred to as the determinantof the
Jacobian of the system.
Equation (12.12) is the two-equationversion of the Newton-Raphsonmethod.As in
the following example,it can be employediterativelyto home in on the rootsof two simultaneousequations.
E X A M P 1L 2E. 3
N e w t o n - R o p h s o fno r o N o n l i n e q rS y s t e m
Problem Stotement. Use the multiple-equationNewton-Raphsonmethod to determine
of x1 : 1.5 andxz:3.5.
rootsof Eq. (12.6).Initiatethe computationwith guesses
Solution. First computethe partial derivativesand evaluatethem at the initial guessesof
-r and y:
#
: 2 x r+ x z : 2 ( 1 . 5 t *3 . 5: 6 . 5
ofz'o -3xi
2 : 36.i5
: 3(3.5)
3xr
W
#
:rr : 1.5
: 32.5
: | * 6xtxz:I + 6( r .5) ( 3.5)
for thefirst iterationis
Thus.thedeterminant
of theJacobian
- 1.5(36.75)
: 156.125
6.5(32.s)
as
at theinitialguesses
Thevaluesofthe functionscanbe evaluated
2
.
5
. f r . o: ( l . S ) 2+ 1 . 5 ( 3 . 5 ) 1 0:
. f z . :, . t3 . 5+ 3 (1 . 5 ) ( 3 . 5-) 25 l : 1 . 6 2 5
into Eq. (12.12)to give
Thesevaluescanbe substituted
- r.62s
-2.s(32.s)
(r.s)
156.125
-^-?<_
2.03603
- (-2.s)(36.7
s) :
r.62s(6.s)
156.t25
2.84388
Thus, the resultsare convergingto the true valuesofxl : 2 and12 : 3. The computation
can be repeateduntil an acceptableaccuracyis obtained.
274
ITERATIVE
METHODS
When the multiequationNewton-Raphsonworks,it exhibitsthe samespeedyquadratic
convergenceas the single-equationversion.However,just as with successivesubsfitution.
it can diverge if the initial guessesare not sufficiently close to the true roots.Whereas
graphicalmethodscould be employedto derive good guessesfor the single-equation
case,
no such simple procedureis availablefbr the multiequationversion.Although thereare
for obtainingacceptablefirst estimates,often the initial guesses
someadvancedapproaches
must be obtainedon the basisof trial and error and knowledgeof the physicalsystembeing
modeled.
The two-equationNewton-Raphsonapproachcan be generalizedto solve n simultaneousequations.To do this, Eq. ( I 2.I I ) can be written for the ftth equationas
frf
- ; - *.,
.\'t.r,
dxt
I t
, d.l'r,i
l.--r'l.i+t
dx2
ih.i
''f
+'
;--lrr.i
dxil
rt :
- J (". 1 f
t
"'
t
itf r,,i
.tl.t-::- f
<JIt
Jn.i
itJr.i
'l':.t-;t/J2
ofi.i
oxil
where the first subscriptk representsthe equationor unknown and the secondsubscript
denoteswhetherthe valueor function in questionis at the presentvalue(l) or at thenextvalue
(t + 1). Noticethat the only unknownsin Eq. (12.13) arethe,rr.i+rtermson theleft-hand
side. All other quantitiesare locatedat the presentvalue (i ) and, thus, are knownatany
iteration.Consequently,the setof equationsgenerallyrepresented
by Eq. ( 12.l3) (i.e.,with
k:1,2,...,n)
c o n s t i t u t e as s e t o f l i n e a r s i m u l t a n e o uesq u a t i o n st h a t c a n b e s o l v e d
numericallyby the elimination methodselaboratedin previouschapters.
Matrix notationcan be employedto expressEq. (12.13)conciselyas
'(r2.
t4)
l J l [ r i _ r ]: - t l l + l J l { x i }
wherethe partial derivativesevaluatedat i are written as theJacobianmatri.rconsisting
of
the partial derivatives:
3ft.,
()X1
UI:
0Jt.,
Dfi.i
0r,
OX?
3f2., ofzl
8-r;1 Dxz
oIz.i
::
0.fn,i
iJxr
:
0f,,i
d"h
3f, .i
}xz
3xn
The initial and final valuesare expressedin vectorforrr as
{x;}r:
l . r 1;
xz.i
x n .)i
and
{.t,*r)r:L,rr.r+r
.x2.i+r
) r a , , +l t
Finally, the function valuesat I can be expressedas
{f }r : 1.fi., .fz.i
f,,i )
275
I2.2 NONLINEAR
SYSTEMS
Equation(12.14)cirn be solvedusing a techniquesuch as Gausselimination.This
processcan be repeatediteratively to obtaiu refined estirnatesin a f'ashionsimilar to the
two-equationcasein Example12.3.
Insightinto the solutioncan be obtainedby solvingEq. (12.l4) with nratrixinvelsion.
Recall that the single-equationversionof the Newton-Raphsonmethodis
^/+l
-
^/
./('ri )
(12.16)
'f \x1l
If Eq. ( 12.14)is solvedby multiplyingit by the inverseof the Jacobian,the resultis
{.ri*r}: {t,}- [.1]
'{.lr
12.11)
Clomparisonof Eqs. (12.16) and (12.17) clearly illustratesthe parallelsbetweenthe two
equations.
In essence,
theJacobianis analogous
to thederilativeofa rnultir"ariate
tunction.
Such matrix calculationscan be implernentedvery efficiently in MATLAB. We can
ilhrstratethis by using MATLAB to duplicatethe calculationsfror.nExalr.rple
12.3.After
defining the initial guesses!we can computethe Jacobianand the function valuesas
>> x=[1.5;3.5];
>>
J=[2*x(1)+x(2)
x(1)
;3"x(2)^2
I+6*x(1)*x(2)
I
T-
6 . 5 t 10 0
36.7500
>> f=tx(1)
1.5000
32.s000
^2+x(1) *x(2) -10
;x(2) +3*x(1) *x \2)'2-51
I
f2.5u00
1.6250
Then, we can implementEq. (12.17) to yield the improved estimates
>> 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<2,erra]. ('at least 2 input arguments required'),end
if nargin<3 |iserLpty(es),es=0.0001;end
if nargin<4 |isempty(maxit) ,maxit=50;end
icer = 0;
x=x0;
while (1)
IJ, f] =func (x, varargin{ : } ) ;
dx=J\ f ;
x=x-dx;
iter = irer - l;
e a = 1 0 0 * m a x( a b s ( d x . / x ) ) ;
if iter>=nrax.it lea<=es, break,
end
end
FIGUREI2.4
MATIAB
Mljleto implement
Newton
Rophson
method
fornoniineor
syslems
of equoiions
than Newton-Raphsonbut which have betterconvergencebehaviorhave beendeveloped
One approachis to refbrrrruiatethe nonlinearsysteu.las a singlefunction:
_ \LF(x) _
l . f i G t , x z ,. . . . i r , , ) 1 2
;-l
whereI (x1,,{2,...,.r,,) is the ith rnemberof the originalsystemof Eq. (12.7).Thevalues
of r that nlinimize this ftrnctionalso reprcserltthe solutionof the nonlinearsystem.
Thfle.
fore, nonlinearoptimizationtechniquescan be employedto obtain solutions.
I2.3 CASESTUDY
277
CHEMICAL
REACTIONS
Bockground'
Nonlinearsystemsof equationsoccur frequently
in the characterization
of chemicalreactions.For example,the following
chemicalreactionstakeplacein a closed
sysrem:
2 A+ B
--->
C
<-_
(12.r8)
A+D*c
(12.19)
At equilibrium,theycanbe characterized
by
Kl :3cicu
(12.20)
Kz- 3-
(12.2t\
cac,t
where the nomenclatureci representsthe concentration
of constituent i. If x, and x" are the
number of molesof C that are produceddue to
the first and secondreactions,respectively,
formulatethe equilibrium,relationships
as a pair of two simultaneousnonlinearequations.
tt : 4_x
K, = 3:7.x lo-2,
50, c6.s: 20,c,,o: 5, and cr,6: 10, employ the
ll
!o-4,
.co.o:
Newton-Raphsonmethodto solve theie
equations.
Sofution. Usingrhe stoichiometry
of Eqs.(t2.lg) and(12.19),the concenrrationsof
eachconstituentcanbe represented
in termsof -r.,and_r,as
ca:cu.o-2x1
-
12
(r2.22)
cb:cb,\-xI
(12.23)
cc=cc,o*-tt*.rz
(12.24)
Cd=Cd,\-Xz
(r2.25)
where the subscript0 designatesthe initial concentration
ofeach constituent.Thesevalues
can be substirutedinro Eqs. (12.20) and,(12.21)ro
sive
Kr:
(c..0*xL+x2)
(co.o- 2x1 - x)2{c6,0 - xr)
(c",0+xr*xz)
(c",0* 2x1- x2)@a,o
* x:)
Given the parametervalues,theseare two nonlinear
equationswith two unknowns.Thus,
the solution to this problem involves determining
the roots of
f1@1,x) f 2 @ 1 ,x 2 ):
5*xr+xz
(50-2xr-x)2(20-x)
-4x10-a
(50-2xt-x)(10-xz)
- 3.7 x 10-2
(12.26)
(12.21)
278
ITERATIVE
METHODS
iiir";illr ir.1
.,.''
continued
In order to use Newton-Raphson,we must determinethe Jacobianby taking thepartial
derivativesof Eqs. (12.26) and (12.27).Although this is certainly possible,evaluating
the
derivativesis time consuming.An alternativeis to representthem by finite diff'erences
inr
fashionsinilar to the approachusedlbr the modified secantmethodin Sec.6.3.Forexanple, the partial derivativescomprisingthe Jacobiancan be evaluatedas
dxt
.fi(xt + 6x1,x2)* fi(xr, xz)
3xr
-ofr
:
3xz
.fi(xr,xz * 6x2)- J\$r, xz)
3.uz
3fz
o.rr
x2)- .ft(xt,.rz)
fz(xr + E.r1,
6xr
3fz
oxz
fz(xt, xz + d,r2)- .fz\r, xz)
3x,
3fr
These relationships can then be expressedas an M-file to compute both the function
valuesand the Jacobianas
f unction
lJ,lr =jf r-c;rct{x, var.rrgin)
del=0.000001;
c l f l d x l - ( r r( x ( I ) + c l e l * x ( I )
, x (2) )-rr (x (1). x (2 ))
dfldx2= (r (x(1) ,x(2) +del"x(2) ) -u(x(1 ) ,x(2) )
c l f 2 d x 1 = ( . . . ( x( 1 ) + d e l * x ( l - ) , x ( 2 ) ) - . u ( x ( 1 ) , x ( 2 ) )
( r . (' x ( L ) , x ( 2 ) + d e l * x ( 2 ) ) - v ( x ( f ) , x ( 2 ) )
df 2c1::2=
J = f c l f 1 d x 1 d f L d x 2 ; d f 2 c i x i d f 2 r . l x ? , ;1
f 1=u(x(1),x{lt));
f2='1(x(1.),x(l));
t2.t (^)
) ,' (de1*x t 1) ) ;
) / (del.*x(2));
) ,r(rlel*x(1,));
) / (ciel*x(2));
(50
-
,a x x
-'!)
^
'2
i
120
O.
T
I
t-o
I
L
(b) Rep
12.2 Us<
systemu
a-f f 1.f ?t
f unct ion f =u (x, !')
(5 + x + y)
f ,/
system ul
x) - 0.0004;
| 0-r1
-3xt
Xl
funct ion
f-v (x, y )
f = (i. + x + .,') I
(50
*
-
*
':<,.../)
i
(1Ll -.71
a . a : , - i:
The function nevrtmul t (Fig. 12.4)can thenbe employedto determinethe rootsgiveninit i a l g u e s s eosf x , : x z : 3 :
--> forii-.ai, shorL
3.3,r66c+000
2.a,-f 2l+000
f=
-'l .I2B5e-01.7
8.5?7-r€,-01'1
a.223 7e 010
i Ler
=
4
e
12.3 Re1
12.4 Tht
termine(
reactors
reactor(1
Solve tl
E s: 5 V o
12.5 Ust
and (b) v
tem to a
equation
PROBLEMS
continued
a solutionof x, - 3.3366&Ddx, : 2.6772is obtained.
Thesevalues
After four iterations,
into Eq. (12.22)through(12.25)to computethe equilibriumconcanthenbe substituted
of thefourconstituents:
centrations
ca : 50 - 2(3.3366)- 2.6772: 40.6496
cn:20 - 3.3366: 16.6634
cc : 5 + 3.3366
+ 2.6112: 11.0138
:
ca lO 2.6772:7.3228
PROBTEMS
methodto solvethe followine
I (a)UsetheGauss-Seidel
untilthepercentrelativeerror falls below s, : 5olc:
i1
I 4i;T]{i:
4l
25
l0-s
12.6 Of the following threesetsof linearequations,identify
the set(s)that you could not solve using an iterativemethod
such as Gauss-Seidel.Show using any number of iterations
that is necessarythat your solution does not converge.
Clearly stateyour convergencecriteria (how you know ir is
n()1converglng).
(a) but use overrelaxationwith I : 1.2
Repeat
Usethe Gauss-Seidelmethod to solve the followine
untilthe percentrelative error falls below e" - 57o:
Jrr+2;r:- jrj:
27
3 r t- 6 x 2 1 } s l : - 6 1 . 5
. r r* - t t * 5 x - r : - 2 1 . 5
Repeat
Prob. 12.2but useJacobiiteration.
Thefollowing systemof equationsis designedto de(the c's in g/m3)in a seriesof coupled
ineconcentrations
asa function of the amount of mass inout to each
Itheright-handsidesin g/day):
Set One
Set lwo
Set Three
9;* 3y*:: l3
.r*_y*6:: B -3.r*4y*5::6
6;r+B::2
r*5.y ::5 -2:+2t--3::-3
?r -'-- l
2 r + 5 r -- : : 6 A . rI 2 y - 2 z : 4
12.7 Determine the solution of the simultaneousnonlinear
equatlons
l':--l-+x+u./)
r'::3800
t- 3c:'r * l8r2 - 6cr : 1200
c2't l2ca: 2350
Ithis problem with the Gauss-Seidel method to
Usethe Gauss-Seidelmethod (a) without relaxation
(b)withrelaxation(,)": 1.2) to solve the following syst0 a toleranceof s. : 57o. If necessary,reanangethe
to achievec'onvergence
2 " t -16 . r ; - " r 3 : - 3 8
- 3 r 1- . t : l ' 7 t . , : - 3 4
-Rr,t
r"-)r.--)O
)*5x-r':-r2
Use the Newton-Raphsonmethodand ernployinitial guesses
of .r : _t': 1.2.
12.8 Determinethe solution of the simultaneousnonlinear
equatlons:
^
)-)
-J-!
)il:,r2
(a) Graphically.
(b) Successivesubstitutionusing initial guessesof r :
(c)
puesses
: r,r:
ITERATIVE
METHODS
C
Tcct-
>
l
I
o,
-cLr<
ccz-
2Cc:-
l
I
-ctzl
I
cc,t -
F
cc.s -
I
l
l
+' + +
-Cn<
-cu<
-cl-s<
cL6
Qt
L
FIGUREP12.9
12.9 Figure P12.9 depicts a chemical exchange process
consistingof a seriesof reactorsin which a gasflowing from
left to right is passedover a liquid flowing from right to left.
The transferof a chemicalfrom the gasinto the liquid occurs
at a ratethat is proportionalto the differencebetweenthe gas
and liquid concentrationsin each reactor.At steadystate,a
massbalancefor the first reactor can be written for the sas as
derivatives,which result in a system of linear algebraic
equations.Use the Causs-Seidelmethod to solve for the
temperaturesof the nodesin Fig. Pl2.10.
25"C
Qccco- Qcccr+ D(cu-
25"C
ccr):0
and for the liquid as
Qrctz - Qrcu I D(ccr - crr) : 0
Tn
r2)
T,,,
Tl
100'c
where Q" and Q, are the gas and liquid flow rates, respectively, andD : the gas-liquidexchangerate.Similar balances
can be written for the other reactors.Solvefor the concentrationsgiventhefollowingvalues:Q6 :2, Q t:
l, D : 0.8,
c c o : 1 0 0 c, m : 7 0 .
12.10 The steady-statedistribution of temperatureon a
heated plate can be modeled by the Laplace equation:
o:***
dx'
100"c
dy-
If the plate is representedby a seriesof nodes(Fig. P I 2. I 0),
centered finite differences can be substituted for the second
75.C
F I G U R EP I 2 . I O
/5
L
t
&
&,
Curve Fitting
braic
r the
4.1 oVERVIEW
WhEr ls Curve Fiffing?
Darais oftengivenfor discrere
varues
arong
mayrequire
esrimalesat pointsbetweenthe discretevalues. " "11111111;
chapters
"o*,.1.1you
13 througtrto oescribe-techniques
to
fit curves to such data to
esti-ates. In addi-rion,you rnay require a sirn.,
ri:"
lftaln ilterm"diute
plified version of a complicared
funcrion. One *uy to ooil;;;;oure
values of the : ,::.,
funct'ionat a numberof discretevalues alon^g
.
the range.
of inr"r"r,. rn.n. a simplerfunction
i:
may be derived to fir thesevalues.Both
of theseapplicationsare known.ascurre
trtting. ,,;;
other'onthebasisof rhearnountof errorassqciared
;rh ;;;.
;ii;;;ffi;;;;,.,*'r,,.
exhibits
a significant
degree
of erroror"scatter,,,
,t. ,t u,"gfi, t."j.rr". , singre
curvethat
representsthe generaltrend of the data.
Becauseany
individualdadpoint *"V l. i"."r.cr. we
make no
efiort to ln^tersect
every point. Rather.the curve is de_
srgnedto follow the pattern of the points
.takenas a
'
,!!
l\l
s.\,
e
tJi,i';,"):,::!::1'j-"H',narureis"uudt,o,t'
squares
,rgrriiion (Fig.pT4. tar).rluuru! wucrs the
rue oaul
datals
is Known
knownto
ro be very
very Dre_
pre_
. fc1nd,. where
cise.
the_
basicapproachis to fit.a curve or u ,".1.,
of
curves rhat passdirectly through each of
the points.
such data usually orii,inatesfrom tables.
Examples
are valuesForthe density of water or for
the heaica_
pacity of gasesas a function of temperature.
The esti_
mation of valuesbetweenwell-known discretepoints
is called inrerpolarion(Fig. pT4. lb and
e.
Curve Fittingond Engineeringond Science. your
ftrst exposure[o curve fitting may have been
to deter_
mtne lntermediatevalues from mbulated
data_for
instance, from interest tables for *";;;;
;;:
or from steam fables for thermodynamics.
Tri.r.
the remainder of your career,you will
,rnroughout
irequentoccasionto estimateintermediate
values
fave
trom such tabJes.
282
4 CURVEFITTING
PART
f(x)
(c)
F I G U R EP T 4 . I
(b)lineor
regression,
fivedoto points:io) leosl-squores
fo fit o "best"curvethrough
Threeottempts
i n l e r p oo i o n o n o ( c ) c - , r
f l e o f r r t e t o oo l o n .
Although many of the widely usedengineeringand scientificpropertieshavebeentab'
ulated,there are a greatmany more that are not availablein this convenientform. Special
casesand new problemcontextsoften requirethat you measureyour own dataand develop
your own predictive relationships.Two types of applicationsare generally encountered
when fitting experimentaldata:trend analysisand hypothesistesting.
Trend analysis representsthe processof using the pattern of the data to make predictions. For caseswherethe datais measuredwith high precision,you might utilize interporegression.
lating polynomials.Imprecisedatais often analyzedwith least-squares
variable.
dependent
values
of
the
Trend analyslsmay be used to predict or forecast
or
interpolation
observed
data
This can involve extrapolationbeyond the limits of the
within the range of the data. All fields of engineeringand scienceinvolve problemsof
this type.
A second application of experimentalcurve fitting is hypothesistesting. Here,an
existingmathematicalmodel is comparedwith measureddata.If the model coefficientsare
4.2 PARIORGANIZATION
to determinevaluesthat bestflt the observeddata.On the
ulrknown,it nray be necessary
are alreadyavailable,it may be appropriother hand,if estimatesof the model coetTicients
ate to comparepledicteclvaluesof the nrodelwith observedvaluesto testthe adequacyof
the model. Ofien. alternativenodels are comparedand the "best" one is selectedon the
basisof empiricalobservations.
curvefitting is irnIn additionto the foregoingengineering
and scientificapplications,
portantin other nurnericalmethodssuchas integrationand the approxirnatesolutionof dil'Finally,culve-fittingtechniqtres
ferentialequations.
canbe usedto derivesimplefunctions
functioDs.
to approximatecornplicated
4.2
PARTORGANIZATION
that is. hol'r'to deAfter a brief review of statistics.Chap. l3 focuseson linear regre.ssion;
terminethe "best" straightline througha setof uncertaindatapoints.Besidesdiscussing
how to calculatethe slopeand interceptof this straightline. we also presentquantitativc
and visualmethodsfbr evaluatingthe validity of the lesults.In acldition,we describeser'fbr the linearizationof nonlinearequations.
eral approaches
of polynomialandmultiplelinearreglessiou.
Chapter/4 beginswith brief discussions
Pctlynomialregres.sirtn
dealswith developinga bc'stfit clf parabolas.cubics,or higher-order
polynomials.This is followed by a clescriptionof multiple linear regression,which is designedtbr the casewhere the dependentvariable), is a linear functionof two or more
variablesr1, 12,.
irrdependent
, xtn.This approachhas specialutility for evaluatingexon a nunlberof differentfactors.
perimentaldatawherethe variableof interestis dependent
we illustratehow polynomialand multiple regressionare
After multiple regression,
model.Among otherthings,this will allow
both subsetsof a generullinear least-squares
ofregressionand discussits generalstatisus to introducea concisematrir representation
tical properties.Finally.the last sectionsof Chap. l4 ale del'otedtononlinearregression.
fit of a nonlinearequaticlnto data.
This approachis designedto computea least-squares
In Clnp. /5, the alternativeclrrve-tittillg techniquecaTledinterpolutionis describecl.
valuesbetween
As discussedpreviously,interpolationis usedfbr estimatingintermediate
precisedatapclints.ln Chap.15,polynomialsarederivedfor this purpose.We introducethe
to connect
basicconceptof polynomialinterpolationby usingstraightlinesand parabolas
procedureior fitting an rrth-ordelpolynomill.Two
points.Then.we developa generalized
thesepolynomialsin equationfbrm. The first, called
fbrmatsare presentcdfbr expressing
orderof the polynopolt'nontial,is preferablewhenthe appropriate
Newton'sinterpolotirrg
mial is unknown. The second,called the Lagrange interpolotingpoltrutmial, has advantagesrvhenthe properolder is known befbrehand.
Finally, Chup. l6 presentsan alternativetechniquefor fitting precisedatapo.ints.This
lits polynomialsto databut in a piecewisefashion.
calleclsplineirrterytolation,
techniclrre,
As such.it is particularlywell suitedfbr fitting datathat is genelallysmoothbut exhibits
The chapterenclswith an overviewof how piecewiseinterpolationis
abruptlocalclranges.
imolementedin MATLAB.
i
!
':.
Lineor R"gression
CHAPTEROBJECTIVES
The primary objectiveof this chapteris to introduceyou to how least-squares
regressioncan be usedto fit a straightline to measureddata.Specificobjectivesand
topics coveredare
'
.
'
.
'
Familiarizing yourselfwith somebasicdescriptivestatisticsand the normal
distribution.
Knowing how to computethe slopeand interceptof a best-fit straightline with
linear regression.
Knowing how to computeand understandthe meaningof the coefficientof
determinationand the standarderror of the estimate.
Understandinghow to usetransformationsto linearizenonlinearequationssothat
they can be fit with linear regression.
Knowing how to implementlinear regressionwith MATLAB.
YOU'VE GOT A PROBTEM
| we noted thaf a free-falling object such as a bungeejumper is subjecttothe
I n Chap. ,
upward tbrce of air resistance.As a first approximation,we assumedthat this forcewas
I
I p r o p o r t i o n at ol t h es q u a r eo f v e l o c i t ya s i n
Fu : cdu2
(13l)
where Fy: the upward fbrce of air resistance[N : kg m/sr]. c,t : a drag coefficient
(kg/m),and u : velocity[m/si.
Expressionssuchas Eq. ( I 3.1) come from the lield of fluid mechanics.Althoughsuch
relationihipsderive in part fl-orntheor,v.experimentsplay a critical role in their formuiainawind
is susperrded
riorr.One suchexperinrentis depictcdin Fig. /j. l. An irrcJiridual
285
REGRESSION
LINEAR
*
{\'
----+
FIGURE
I3.I
Wind tunnelexperimenlfo mecsurehow the force of o r reslstonce
dependson veocily
1600
1200
z
t\
800
40
u,m/s
F I G U R EI 3 . 2
Plotof forceversus
wind velocifu
for on obieclsusoended
in o w nd lunnel
TABTEl3.t
u, m/s
F,N
Experimentol
doto for force(N) ond velocity(m/s)fromo wind tunnel
exoeriment.
to
25
2A
7A
30
380
4A
550
50
610
60
l22A
70
830
BO
I 450
tunnel (any volunteers?)and the force measuredfor various levels of wind velocity. The
resultmight be as listedin Table l3.l .
The relationshipcan be visualizedby plotting force versusvelocity.As in Fig. 13.2,
severalfeaturesof the relationshipbear mention. First, the points indicate that the force
increasesas velocity increases.Second,the points do not increasesmoothly,but exhibit
rather significant scatter,particularlyat the higher velocities.Finally, althoughit may not
be obvious,the relationshipbetweenforce and velocity may not be linear.This conclusion
becomesmore apparentif we assumethat force is zero for zero velocity.
ri
286
LINEAR
REGRESSION
In Chaps.l3 and 14,we will explorehow to flt a "besf' line or curveto suchdata.In
so cloing,we will illustratehow relationships
like Eq. ( 13.I ) arisefrom experimental
data.
r 3.r
STATISTICS
REVIEW
Before describingleast-squares
regression,we will first review some basic concepts
frorn the field of statistics.These include the mean. standarddeviation,residualsumof
the squares,and the normal distribution.In addition.we describehow simple descriptive
statisticsand distributionscan be generatedin MATLAB. If you are familiar with these
subjects,feel tiee to skip the following pagesand proceeddirectly to Sec. 13.2.If you
are unf'amiliarwith theseconceptsor rrrein need of a review, the fbllowing materialis
designedas a brief introduction.
I 3. t .l DescriptiveStotistics
Supposethat in the courseof an engineeringstudy,severalmeasurements
were madeof a
particularquantity.For example,Table | 3.2 contains24 readingsof the coefllcientof thermal expansionofa structuralsteel.Takenat facevalue,the dataprovidesa linritedamount
of information-that is, that the valuesrangefrom a minimum of 6.395 to a maximumof
6.775. Additional insight can be gained by summarizingthe data in one or more wellchosenstatisticsthat convey as much informationas possibleaboutspecificcharacteristics
( l) thelocaof the dataset.Thesedescriptivestatistics
aremostotien selectedto l'epresent
tion of the centerof the distributionof the dataand (2) the degreeof spreadof the dataset.
Meosure of Locotion. The mostcomlron measureof centraltendencyis the arithmetic
mean.The urithntetic ntean (l) of a sampleis defined as the sunr of the individualdata
points(,r;) dividedby the numberof points(n), or
\-...
(1 3 . 2 )
n
wherethe summation(and all the sr.rcceeding
summaticlns
in this section)is from I = I
throughn.
There are severalalternativesto the arithmeticrnean.The medianis the midpointof a
groupofdata. It is calculatedby first puttingthe datain ascending
order.Ifthe numberof
measurements
is odd.the rnedianis the middlevalue.If the numberis even.it is thearithmetic mean of the two middle values.The medianis sometimescalled the 50th percentile.
The mode is the value that occursmost fiequenlly.The conceptusually hasdirectutility only when dealingwith discreteor coalselyroundeddata.For continuousvariablessuch
as the data in Table 13.2.the conceptis not very practical.For example,thereare actually
TABLE13.2
6 495
6 665
6755
6 56_s
Meosuremenis
of the coefficient
of thermolexponsionof siructurol
steel
6 )i n l { i n . ' ' F ) ] .
[(x to
6 595
6 505
6 625
65r5
6 615
6 435
6 . 7 15
6 555
o 635
6 625
6 575
6 395
6 485
6715
6.655
6775
6 55;
6655
6 605
668_5
ii
I3.I STATISTICS
REVIEW
287
four modesfor this data:6.555.6.625,6.655,and6.715,which all occurtwice.If the numbers had not beenroundedto 3 decimal digits, it would be unlikely that any of the values
would even have repeatedtwice. However,if continuousdataare groupedinto equispaced
intervals.it can be an informative statistic.We will return to the mode when we describe
h i s t o g r a mlsa t e ri n t h i ss e c t i o n .
Meosures of Spreod. The simplestmeasureof spreadis the range, the differencebetweenthe largestand the smallestvalue.Although it is certainlyeasyto determine,it is not
considereda very reliable measurebecauseit is highly sensitiveto the samplesize and is
v e r y s e n s i t i v teo e x t r e l n ev a l u e s .
The rnostcomlnon measureof spreadfor a sampleis thestandarddeviation(s,,) about
the mean:
( 1 3 .)3
where S' is the total sum of the squaresof the residualsbetweenthe data points and the
mean,or
$: Itr', - ir2
(13.4)
Thus, if the individual measurements
are spreadout widely aroundthe mean,s, (and,consequently.s, ) will be large.If they aregroupedtightly, the standarddeviationwill be small.
The spreadcan also be representedby the squareof the standarddeviation,which is called
the variance:
.
r r '
frv; - \:)l
------------l-
n-
|
(1 3 . 5 )
Note that the denominatorin borhEqs.(13.3)and (13.5)is ru- 1. The quantityru- I
is ref'enedto as the tlegreesof freedont.Hences, and s,, are said to be basedon rr - I degreesof fieedom. This nomenclaturederivesfrom the fact that the sum of the quantities
uponwhichgisbased(i.e.,f-yt,y-)2,...,,)r-y,)iszero.Consequently,ifyis
known and rr - I of the valuesare specified,the remainingvalueis fixed. Thus, only n - 1
of the valuesare said to be freely determined.Anotherjustification for diviclingby n - 1 is
the fact that there is no suchthing as the spreadof a singledatapoint. For the casewhere
n : 7, Eqs.(13.3)and (13.5)yield a meaningless
resulrof infinity.
We should note that an alternative,more convenientforrnula is availableto compute
the variance:
-2
,)r,:
- (Dt',)' l,
f u,?
n-l
(I3.6)
This versiondoesnot requireprecomputationof ! andyieldsan identicalresultasEq. ( 13.5).
A final statisticthat has utilrty in quantifying the spreadof data is the coefficient of
variation (c.v.).This statisticis the ratio of the standarddeviationto the mean.As such,it
providesa normalizedmeasureof the spread.It is often multiplied by 100 so that it can be
expressed
in the form of a percent:
s).
x 1007o
(1 3 . 7 )
t
ll
t
288
EXAMPLE
l3.i
LINEARREGRESSION
S i m p l eS t o t i s t i cosf o S o m p l e
Problem Stotement. Computethemean,median,variance,standarddeviation,andcoefficient of variation for the datain Table 13.2.
Solution. The data can be assembledin tabularform and the necessarysumscomputed
""HJt;Jl3"un t" compured
as[Eq.(13.2)],
t -:
158'4
:6.6
24
Becausethere are an even number of values,the median is computed as the arithmetic
meanof the middletwo values:(6.605+ 6.615)12:6.61.
As in Table 13.3,the sum of the squaresof the residualsis 0.217000,which canbe
usedto computethe standarddeviation [Eq. (13.3)]:
sr, :
0.217000
*z + - L : 0 . 0 9 7 1 3 3
TABTE13.3
Dotoond summotions
for compuiingsimpledescriptive
stotistics
forthe
,l3.2.
coefficients
of thermolexoonsionfromToble
(Jl -.yr
I
2
3
4
5
6
7
8
9
t0
tl
)2
t3
l4
t5
t6
l7
IB
t9
2A
2l
22
23
aA
T
6 395
6 435
6 485
6 495
6 505
6 515
6 555
6.555
6 565
6 575
5 595
6 605
6.61
5
6 625
6 625
6.635
6.655
6 655
6 665
6.685
6.715
6 . 7 15
6755
6.775
I s8zdo
o 04243
4.02723
0 0t323
0.01103
0 00903
o.oo723
0.00203
0.00203
0 0 0 12 3
0 00063
0 00003
0.00002
0.oao22
0.00062
0.00062
o.oat22
0 00302
0.00302
a 00422
a.ao722
o 41322
a o)322
o.02402
0 03062
67Tn6
40 896
41 449
42 055
42.)85
42.315
42.445
42568
42968
43 099
43.23)
43 494
43.62b
43 758
4 38 9 1
4 3B 9 l
44 023
44 285
44.289
44422
44.689
45091
45091
45630
45901
u4) 0J/
I3.I STATISTICS
REVIEW
289
the variance[Eq. (13.5)];
s,?: (0.097133)2:0.009435
and the coefficientof variation[Eq. (13.7)]:
c.v.:_
0.097133
x1007a:1.47Vo
6.6
The validity of Eq. (13.6)can alsobe verifiedby computing
a
\:
- (158.400)'
1045.651
/24 :
0.009435
24-l
13.1.2 The Normol Distribution
Another characteristicthat bearson the presentdiscussionis the datadistribution-that is,
the shapewith which the data is spreadaroundthe mean.A histogramprovidesa simple
visual representationof the distribution.A histograntis constructedby sorting the measurenrentsinto intervals,or bins. The units of measurementareplotted on the abscissaand
the frequencyof occurrenceof eachinterval is plotted on the ordinate.
As an example,a histogramcan be createdfor the data from Table 13.2.The result
(Fig. l3.a) suggeststhat most of the datais groupedcloseto the meanvalue of 6.6. Notice
also,that now that we have groupedthe data,we can seethat the bin with the most values
is from 6.6 to 6.64.Although we could say that the mode is the midpoint of this b|n,6.62,
it is more common to report the most frequent range as the modal class interval.
If we have a very large set of data, the histogramoften can be approximatedby a
smooth curve. The symmetric,bell-shapedcurve superimposedon Fig. 13.3 is one such
characteristicshape-the normal distribution.Given enoughadditionalmeasurements,
the
histogramfor this particularcasecould eventuallyapproachthe normal distribution.
FIGUREI3.3
A histogromused to depict the distribution
of doto. a. rh^ ^"-h^, ^{ l^r^ ^^inis increcses,ihe
n:srr-a,i- ofronnnn.rn.fpq 1l^pq-66+\ hcll..t-cned.r',"" .i rli 'i"ll]r[io
,r. br.ion.
OJ
0)
L2
290
LINEAR
REGRESSION
The conceptsof the rrrean,standarddeviation,residualsum of the squares,andnormal distribution all have greatrelevanceto engineeringand science.A very simpleexample is their useto quantify the confidencethat can be ascribedto a particularmeasurement.
If a quantity is norrnallydistributed,the rangedefinedby.v - s., to y * .1, will encompass
approximately68Vcof the total rneasurements.
Similarly, the rangedefined by tr - 2s, to
)r + 2.s, will encornpassapproximately95Vo.
For example,for the datain Table 13.2,we calculatedin Example13.1that) = 6.6
and s., :0.097133. Basedon our analysis.we can tentativelymake the statement
that
approximately95Voof thereadingsshouldfall between6.405'734and6.794266.Becauseit
is so far outsidethesebounds,ifsomeonetold us that they had rneasureda valueof7.35,we
would suspectthat the measurementmight be erroneous.
13.1.3 DescriptiveStotisticsin MATLAB
StandardMATLAB has severalfunctionsto computedescriptivestatistics.rFor example,
the arithmeticmeanis computedasmean(x). If x is a vector,the function returnsthemean
of the vector's values.If it is a matrix, it returns a row vector containingthe arithmetic
mean of eachcolumn of >r.The following is the result of using meanand the otherstatistical functionsto analyzea column vector s that holds the datafrom Table 13.2:
g
>> format
short
(s),mode
>> mean (s),median
(s)
h h
6.61,
6.555
>> min(s),max(s)
:ne
6.395
o,
t/a
>> range=max(s)
min(s)
O.3B
>> var(s),std(s)
:nc
0.0094348
anc
-
0.097133
I MATI-AB also ofiers a Statistics Toolbox
that provides a wide range of common statistical tasks,from random
rrrrmher
oeneration
fn .'rrrve fiffinq
fo desiqn
29r
I3.I STATISTICS
REVIEW
Theseresultsare consistentwith thoseobtainedpreviouslyin Example 13.1.Note that
althoughthere are four valuesthat occur twice, the mode function only returnsone of the
values:6.55,5.
MATLAB can also be usedto generatea histogrambasedon the hist function. The
hist functionhasthe syntax
ln, xl
= hisL (Y, x)
where n : the number of elementsin each bin, x : a vector specifyingthe midpoint of
eachbin, and y is the vector being analyzed.For the datafiom TabIe 13.2,the result is
>> 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 ?<
JOU
x!
r00
4AO
900
r,600
2 500
3,600
4,904
6,404
2a 404
xJi
254
t,400
I 1,400
22AAO
30500
73 204
5 8 ,r 0 0
I 16,000
3r2850
The meanscan be computedas
360
_
i-_-45
8
_
}:
5,135
: 641.815
g
withEqs.(13.15)
and(13.16)
as
The slopeand the interceptcan then becalculated
o' :
8 ( 3 1 2 , 8 s- 03) 6 0 ( sr 3. 5 )
:19.41024
8(20.400)-(360P
* 19.47024(45)
: -234.2857
ao : 641.875
fit is
Usingforceandvelocityin placeofy and;, theleast-squares
F : -2342851-t 19.41024u
Theline,alongwiththedata,is shownin Fig. 13.6.
FIGUREI3.6
ineto thedotofromToble1 3 t
leosf-squores
fitof o strclght
1600
1200
z
800
296
LINEAR
REGRESSION
Notice that althoughthe line fits the datawell, the zero interceptmeansthat theequa"
tion predicts physically unrealisticnegativeforces at low velocities.In Section 13.3,we
will show how transformationscan be employedto derivean altemativebesrfit line thatis
more physically realistic.
| 3.2.3 Quontificotion of Error of lineor Regression
Any line other than the one computedin Example 13.2resultsin a largersurnof thesquares
of the residuals.Thus, the line is uniqueand in termsof our chosencriterion is a "best"line
through the points. A number of additional propertiesof this fit can be elucidated
by
examiningmore closely the way in which residualswere computed.Recall that thesumof
the squaresis definedas [Eq. (13.12))
n
,t,: f (li -ao-arxi)2
-i:t
(13.17)
Notice the similarity betweenthis equationand Eq. (13.4)
s,:ftlr-l)2
(13.18)
In Eq. (13.18),the squareofthe residualrepresented
the squareofthe discrepancybetween
the dataand a singleestimateof the measureof centraltendency-the mean.In Eq.(13.17),
the squareof the residualrepresentsthe squareof the verticaldistancebetweenthedataand
anothermeasureof centraltendency-the straightline (Fig. 13.7).
The analogy can be extendedfurther for caseswhere (1) the spreadof the points
around the line is of similar magnitude along the entire range of the data and (2) thedistribution of thesepoints about the line is normal. It can be demonstratedthat if thesecriteria
FIGURE
I3.7
Theresiduol
in lineorregression
represents
theverticol
dlstonce
o dotopointondihe
between
s t r o i g hlti n e .
Measurement
-------*o
a0+ afii
I 3.2 LINEARLEAST-SQUARES
REGRESSION
297
are met, least-squares
regressionwill provide the best (i.e.,the most likely) estimatesof ag
and a1 (Draperand Smith, 1981).This is called the maximumlikelihoodprinciple in statistics. In addition,ifthese criteria are met, a "standarddeviation"for the regressionline can
be determinedas [comparewith Eq. (13.3)]
J],/r
-
s,
(13.19)
n-2
where s_"7,is called the standard error of the estimate.The subscriptnotation "y /x" designates that the error is for a predicted value of y correspondingto a particular value of x.
Also, noticethat we now divide by n - 2 becausetwo data-derivedestimates----as
anda1were used to compute S,; thus, we have lost two degreesof freedom.As with our discussion of the standarddeviation,anotherjustificationfor dividing by n - 2 is that thereis no
such thing as the "spreadof data" arounda straightline connectingtwo points.Thus, for
the casewheren : 2,F,q.(13.19)yieldsa meaningless
resultof infinity.
Just as was the case with the standarddeviation, the standarderror of the estimate
quantifies the spreadof the data.However, syTxQuantifiesthe spreadaround the regression
line as shown in Fig. 13.8b in contrastto the standarddeviation s,. that quantified the
spreadaround the mean (Fig. 13.8a).
Theseconceptscan be usedto quantify the "goodness"of our fit. This is particularly
useful for comparisonof severalregressions(Fig. 13.9).To do this, we returnto the original data and determine the total sum of the squaresaround the mean for the dependent
variable(in our case,y). As was the casefor Eq. ( 13.18),this quantityis designatedg . This
is the magnitude of the residual error associatedwith the dependent variable prior to
regression.After performing the regression,we can compute Sr, the sum of the squaresof
the residualsaround the regressionline with Eq. (13.17).This characterizes
the residual
F I G U R EI 3 . 8
Regression
dotoshowing(o)thespreodof thedotooroundthemeonof thedependent
vorioble
ond (b)lhespreodof thedotooroundthebest-fit
line.Thereduction
in thespreodin goingfrom
(o)to (b),os ind;cored
by t[e be'lsl^'oped
cu'uesot therigl^t,represenls
theimprovement
dueto
linecregression.
tbl
298
LINEARREGRESSION
o
o
I3.9
FIGURE
E r o r ' . p l eos f i n e o e g r e s s i o w
n r t n ( o )s m o l 'o n d { b )
'orge
residroe
l 'rors
error that remains after the regression.It is, therefore,sometimescalled the unexplained
sum of the squares.The differencebetweenthe two quantities,S, - $ , quantifiestheimprovement or error reduction due to describing the data in terms of a straight line rather
than as an averagevalue. Becausethe magnitudeof this quantity is scale-dependent,
the
differenceis normalizedto S, to vield
,'2 :
S'-S,
s,
where 12 is called the coefficient of determination and r is the correlation coefficient
.
/-.
\: J r)). For a peri-ectfit, t. : 0 and 12 : l, signifying that the line explains100%ofthe
An
variability of the data. For 12 : 0, S, : Sr and the fit representsno improvement,
alternativeformulation for r that is more convenientfor computerimolementationis
r:
nf(xiri) - (Ir')(Iy,)
- (I r,)'
(L,,)'J,I r,?
EXAMPLE
13.3 Estimotion
of Errorsfor theLineorLeqsl-Souores
Fit
ProblemStqtement. Computethetotalstandard
deviation.
thestandard
errorof theestimate,andthecorrelation
coefficientfor thefit in Example13.2.
lrlyl'?: .
Thedatacanbe setup in tabularform andthenecessary
sumscomputed
asin
299
I 3.2 LINEARLEAST-SQUARES
REGRESSION
TABTE13.5
Dotoond summotions
neededto computethe goodness-of-fit
stotistics
f o r t h ed o t of r o mT o b l e1 3 . , ] .
li
l
2
3
4
5
6
7
B
t0
20
30
4A
50
60
7A
BO
25
7A
380
550
610
t 224
830
I ,454
eo i atxi
-39 58
t55t2
349 82
544.52
735.23
9 3 39 3
I,t2863
1 , 3 23 3
JOU
L
(yi - t)2
380,535
327,441
68,579
8,441
1,0r6
3 3 42 2 9
3 53 9 r
6 53 , 0 6 6
t ,808,297
(!i - ao - a$)2
4,171
7,245
9il
30
I6,699
B ], 8 3 7
8 9 ,I B O
16,444
216,1tB
The standarddeviationis [Eq. (13.3)]
: 508.26
and the standarderrorof the estimateis [Eq. (13.19)]
: 189.79
Thus, becausesr,/.r( s,,,the linear regressionmodel has merit. The extentof the improvement is quantifiedby [Eq. ( 13.20)l
I
1 . 8 0 8 , 2 -9 72 1 6 . 1 1 8
: 0.880-5
l . 808.297
or r : \/0.88[5 : 0.9383. Theseresultsindicatethat 88.05clcof the orisinal uncertaintv
hasbeenexplainedby the linearmodel.
Before proceeding,a word of cautionis in order.Although the coefficientof determiyou should be careful not to ascribe
nation provides a handy measureof goodness-of'-fit,
more meaningto it than is warranted.Justbecauser'2 is "close" to I doesnot meanthat the
"good." For example,it is possibleto obtaina relativelyhigh valueof r'l
fit is necessarily
when the underlying relationshipbetween-y and ;r is not even linear. Draper and Smith
( 198l) provide guidanceand additionalmaterialregardingassessment
of resultsfor linear
regression.ln addition,at the rninimum,you shouldalwaysinspecta plot of the dataalong
with your regressioncurve.
A n i c e e x a r n p l e w a s d e v e l o p e d b y A n s c o m b e ( 1 9 7 3 ) . A1s3i n. lF0i,gh.e c a m e u p w i t h
four datasetsconsistingof I I datapointseach.Although their graphsare very different,all
have the samebest-fit equation,.)': 3 + 0.5r, and the samecoefficientof determination,
300
LINEAR
REGRESSION
t3
t:)
10
10
10
F T G U Rt E
3.lO
Anscombe's
fourdotosets
olongwithihebestfit
Jine,
,y: 3 + O.5x
I3.3
TINEARIZATION
OF NONTINEARRETATIONSHIPS
Linear regressionprovidesa powerful techniquefor fitting a bestIine to data.However,it
is predicatedon the fact that the relationshipbetweenthe dependentand independentvariables is linear. This is not always the case,and the first step in any regressionanalysis
shouldbe to plot and visually inspectthe datato ascertainwhethera linear model applies.
In some cases,techniquessuch as polynomial regression,which is describedin Chap.14,
are appropriate.For others,transformationscan be usedto expressthe datain a form that
w i t h l i n e a rr e g r e s s i o n .
iscompatible
One exampleis the exltonentialmodel:
! : olreP"
(l3.22)
where a1 and B1 are constants.This model is usedin many lields of engineeringandscienceto characterizequantitiesthat increase(positiveB1) or decrease(negativeB1) at a rate
that is directly proportionalto their own magnitude.For example,populationgrowthor
radioactivedecaycan exhibit suchbehavior.As depictedin Fig. 13.11a,the equationrepresentsa nonlinearrelationship(for fu l0) between,vand x.
Another exampleof a nonlinearmodel is the simplepower equation:
)- :
a2xP)
( 13.23)
wherea2 andB2 areconstantcoefficients.This model haswide applicabilityin all fieldsof
engineeringand science.It is very frequently used to fit experimentaldata when the
underlyingmodel is not known. As depictedin Fig. 13.11b,the equation(for B2 l0)is
nonlinear.
FIGU
{o)Th
(fJore
I 3.3 LINEARIZATIO
ON
F NONLINEAR ELATIONSHIPS
:-:
(dl
GI
301
azxQl
(f)
HGURE
r3.r I
(b)thepowerequolion,ond (c)thesoiurotron-growth-role
Ports(d),{e),ond
equotion.
equotion,
lolTheerponentiol
versions
lhctresui tromsimpletronstormotions.
oi theseequolions
lllorelineorized
A third exampleof a nonlinearmodel is the saturation-growth-rateequation:
J
.\':0.r_
D r+ , r
(1.1.2+)
where a3 and B3 are constantcoefficients.This model, which is particularly well-suited
for characterizingpopulation growth rate under limiting conditions, also representsa
nonlinearrelationshipbetween-y and r (Fig. l3.1lc) that levelsoff, or "saturates,"as x
increases.It has many applications,particularlyin biologically relatedareasof both engineeringand science.
Nonlinear regressiontechniquesare availableto fit theseequationsto experimental
datadirectly.However,a simpleralternativeis to usemathematicalmanipulationsto transform the equationsinto a linear form. Then linear regressioncan be employed to fit the
equationsto data.
302
LINEARREGRESSION
For example,Eq. (13.22)can be linearizedby taking its naturallogarithmto yield
(13.2t
l n y : l n c v r* f r x
of
Thus, a plot of ln y versusr will yield a straightline with a slopeof B1 and an intercept
ln o1 (Fig. 13.|ld).
Equation(13.23) is linearizedby taking its base-l0logarithm to give
(r3.26)
logy:loga2*B2logx
Thus, a plot of log y versuslog x will yield a straightline with a slope of B2 andaninter'
cept of log cv2fig. l3.Ile). Note that any base logarithm can be used to linearizethis
model. However, as done here,the base-10logarithmis most commonly employed.
Equation(13.24)is linearizedby inverting it to give
I
1
I
qS
ful
+'--
(13.21)
a3x
Thus, a plot of 1/y versusl/x will be linear,with a slopeof Bzluz and an interceptof 1/o3
( F i e .1 3 . 1 l / ) .
In their transformedforms, thesemodels can be fit with linear regressionto evaluate
the constantcoefficients. They can then be transformedback to their original stateandused
for predictivepurposes.The following illustratesthis procedurefor the power model.
13.4
EXAMPLE
FittingDotowith the PowerEquotion
transProblemStqtement. Fit Eq. (13.23)to thedatain Table13.1usinga logarithmic
formation.
Solution. The datacan be set up in tabularform and the necessarysumscomputedasin
Table 13.6.
The meanscan be computedas
t: ?Tr :2'5644
,:Yy:1.5i5't
TABTE| 3.6
neededto fit the powermodelto thedotofrom
Dotoond summotions
r l l
l o b l el 3 . l
xi
I
220
330
440
550
660
774
BBO
I
t0
li
25
7A
380
550
610
l22A
830
I 450
log ri
IogJi
000
301
477
642
699
L778
I .845
L903
1 398
t B45
2.584
2.744
2.785
3.086
2.919
3.t6r
tlouo
lu)
)
(log r;)2
I 000
I 693
2.182
2 567
2.886
3.162
3.404
3.622
/0 ) 1a
log x; logy;
r 398
2.AAl
3Bll
4 390
4.732
5.488
5 386
6016
ilbn
I3.3 TINEARIZATION
OF NONTINEARRELATIONSHIPS
303
log v
1000
100 log.r
)
1600
1200
800
400
0
60
40
80
(b)
F I G U R IE3 . I 2
firof c po*ermodello thedotofromToblel3 I {oJlhe fitof theli'onsfoi'med
Leost
squores
doto
(b)Thepowerequotion
fitolongwiththedolo.
The slopeand the interceptcan then be c a l c u l a t ew
d i t h E q s .( 1 3 . 1 5 ) a n d( 1 3 . 1 6 )a s
al :
8 ( 3 - r . 6 2 2-) I 2 . 6 0 6 1 2 0 l. 5 )
8 ( 1 0 . 5 1-6 r( 1 2 . 6 0 6 ) 2
:1.9842
c t l : 2 . 5 6 4 4- 1 . 9 8 1 2 ( 1 . 5 1 5 :1 ) - 0 . 5 6 2 0
The least-sqLrares
fit is
l o g r ' : - 0 . 5 6 2 0 t l . 9 8 4 2 l o gr
The fit along with the datais shown in Fig. 13.12a.
ri
304
LINEAR
REGRESSION
We can also display the fit usingthe untransformedcoordinates.To do this,thecoeffi:0.2741 andB. =1.9942.
cierrtsof the power nrodelare determinedoScy2: 19 05610
Using fbrceand velocityin placeof ,r'andr, the least-squares
fit is
e8a2
F :0.2J4lut
This equation,alongwith the data,is shownin Fig. 13.12b.
The fits in Example 13.4 (Fig. 13.12)should be comparedwith the one obtained
previouslyin Example13.2(Fig. 13.6)usinglinearregression
on the untransformeddata
Although both resultswould appearto be acceptable,the transformedresulthastheadvantage that it doesnot yield negativeforce predictionsat low velocities.Further,it is known
from the discipline of fluid mechanicsthat the drag force on an object moving through
a
flLridis often well describedby a model with velocity squared.Thus, knowledgefromthe
field you are studying often has a large bearingon the choice of the appropriate
mode,
e q u a t i o ny o u u s ef o r c u r v eI ' i t t i n g .
t3.3.1 GenercrlCommenlson Lineor Regression
Beforeproceedingto curvilinearand multiple linear regression,we mr.rsternphasize
theintroductory nature of the foregoing materialon linear regression.We have focusedonthe
simple derivationand practicaluse of equationsto fit data.You shouldbe cognizantofthe
fact that there are theoreticalaspectsof regressionthat are of practicalimportancebutare
beyondthe scopeof this book. For example,somestatisticalassumptionsthat areinherent
proceduresare
in the linear least-squares
l.
2.
3.
Eachx has a fixed valuel it is not randomand is known without error.
The -yvaluesare independentrandomvariablesand all have the samevariance.
The I' vslusr for a given x must be normally distributed.
Such assumptionsare relevant to the proper derivirtion and use of regression.
For
example,the first assumptionrneirnsthat (l) the "r valuesmust be en'or-tieeand(2)the
regressionof -t'versusx is not the sameas r versus1. You are urgedto consultotherreferencessuchas Draperand Smith ( 1981)to appreciateaspectsand nuancesof regression
that
are beyondthe scopeof this book.
I3.4
C OMP U T E R A P P L IC A TIONS
Linear regressionis so commonplacethat it can be implementedon most pocketcalculators. In this section,we will show how a simple M-file can be developedfo determine
the
slope and interceptas well as to createa plot of the dataand the best-fit line. We will also
show lrow linearregression
can be implementedwith the built-in po ly f i t f unction.
305
I 3.4 COMPUTER
APPLICATIONS
13.4.1 MATLAB M-file: linresr
An algorithm for linear regressioncan be easily developed(Fig. 13.13).The required
summationsare readily computedwith MATLAB's sum function. Theseare then used to
computethe slopeandthe interceptwith Eqs.(13.15)and(13.16).The routinedisplaysthe
interceptand slope,the coefficient of determination,and a plot of the best-fit line along
with the measurements.
A simple exampleof the use of this M-file would be to fit the force-velocitydatathat
was analyzedin Example13.2:
>> x = 110 20 30 40 50 60 70 B0l;
> > y = 1 2 5 1 A 3 8 0 5 5 0 6 1 0 1 2 2 0 8 3 0 1 4 5 0 1t
>>
(x,y)
linregr
0. BB05
r 9 . 4 1A 2 - 2 3 4 . 2 8 5 1
1600
1400
1200
1000
800
600
400
200
-200
10
80
It can just as easily be used to fit the power model (Exarnple 13.4)by applyingthe
Iosl0 function to the data as in
>> linregr
(log10 (x),1oql0
0.9481
7.9842
-0.5624
(y) )
306
LINEAR
REGRESSION
3.5
2.5
1.5
t.t
1.4
t.o
1.8
FIGURE
I3.I3
AnM{ilelo implement
lineor
regression.
function
Ia, 12 ] = linreqr (x,y)
% linregr:
linear
regression
curve fitting
%
1., 12l = linregr(x,y):
Least squares fit
of straight
?
line
to data by solving
the normal equations
Z input:
?
x = independent
variable
y=
Z
dependentvariable
Z output:
Z
a = vector of slope, a(1), and incercept,
%
12 = coefficient
of determination
a(2)
n = length(x);
if length(y)-=n,
error(,x
and y musf be same length,);
end
x = x(:);
y = y(:);
? convert i_o column vectors
S X = 3 s 1 1( tx ) ; S y = S u m ( y ) ;
sx2 = srtn(x.*x);
sxy - Sum(x.*y); sy2 = Sum(y.*y);
a (1) = (n*sxy-sx*sy) / (n*sx2-sx,'2) ;
a(2) = sy/n-a(1)*sx/n;
T2 = ((n*sxy-sx*sy) /sqrt (n*sx2-sx^2) /sqrt (n*sy2- sy^2) ^Z
)
;
% create plot
of data and best fit
lrne
xp = lanspace (min (x) ,max\x\ ,2) ;
yF
a(l)*xp-a(2);
plot(x,y,,o,,
xp,yp)
g r r - id o n
I3.5 CASESTUDY
307
13.4.2 MATTAB Functions: polyf ir ond polwal
MAILAB has a built-in function polyf it that fits a least-squares
nth-orderpolynomial to
data.It can be applicdas in
>> p
= polyfit
(x,
y,
n)
wherex andy arc the vectorsof the independentand the dependentvariables,respectively,
olld n : the order of the polynomial.The function returnsa vectorp containingthe polynomial's coefficients.We should note that it representsthe polynomial using decreasing
powers of -r as in the following representation:
f ( x ) : I t t x " * p 2 x " - t+ . . . +
p n x* p n , r 1
Becausea straightline is a first-orderpolynomial,potyf ir (x,y , t) will returnthe
slope and the interceptofthe best-fit straightline.
t10 20 30 40 50 60 70 B0l;
125 7A 380 550 610 1220 830 14501;
polyf it (x,y,1)
>>x
>>y
>>a
I9 . 4702 -23 4 .2857
Thus, the slopeis 19.4702and the inrercepris -234.2857.
Another function, polyval, can then be used to compute a value using the coefficients.It has the seneralfbrmat:
>> y
= polyval
(p,
x)
wherep : the polynomial coefficients,and y:
>> y
the best-fit value at x. For example,
= polyval(a,45)
6 4 7 . B 75 A
, it}fS9..,."fl,.!l;q,"1'
1
rr ,
, . : d . .. . -
E N Z Y ME
K IN E T IC S
Bockground.
Enzymesact as catalyststo speedup the rateof chemicalreactionsin
living cells.In mostcases,theyconvertonechemical,the substrate,into another,theproduct.TheMichaelis-Menten
eqvationis commonlyusedto describesuchreactions:
u--
u_[,s]
k"+ [s]
(13.28)
where u : the initial reaction velocity, u-: the maximum initial reaction velocity, [.i] :
substrateconcentration, and ft" : a half-saturation constant.As in Fig. 13.14, the equation
describes a saturating relationship which levels off with increasing [.S]. The graph also
illustrates that the half-saturation constant correspondsto the substrateconcentration at
which the velocity is half the maximum.
308
LINEAR
REGRESSION
continued
Second-order
Michaelis-Menten
model
0.5u,,,
k"
FIGURE
I3.T4
modelof enzyme
kinetics.
Twoversions
of theMichoelis-Menien
Although the Michaelis-Menten model provides a nice starting point, it has beenrefined and extended to incorporate additional features of enzyme kinetics. One simple
extension involves so-calledallosteic enzymes,where the binding of a substratemolecule
at one site leads to enhancedbinding of subsequentmolecules at other sites.For caseswith
two interacting bonding sites, the following second-orderversion often results in a bettu
fit:
't):
-
u- [s]2
ft"2+ [S]2
(t3.29)
-
This model also describesa saturatingcurve but, as depictedin Fig. 13.14,the squared
concentrationstend to make the shapemore sigmoid, or S-shaped.
Supposethat you are provided with the following data:
tsl
U
IB
0 . 13
1.3
o.a7
3
0.22
d
a7 F.
6
0 . 33 5
B9
0.35
036
Employ linear regressionto fit this data with linearized versions of Eqs. (13.28)and
(13.29). Aside from estimating the model parameters,assessthe validity of the fits with
both statistical measuresand graphs.
Solution.
model
Equation(13.28),which is in the format of the saturation-growth-rate
(Eq. 8.2D, can be linearizedby inverting it to give (recallEq. 13.27)
k,
I
I3.5 CASESTUDY
309
;ryifiru::ri
x.,. ii{. .i:.
continued
:i
The linreqr
function from Fig. 13.13 can then be used to determine the
least-squaresfit:
>> S=11.3 1.8 3 4.5 G 8 9l;
>> v=f 0.07 0.Ll 0.2?, O.2ja A. j.l 5 0..iir
0.3t, 1;
> > L a , r ' 2 l = L i n r e g r ( 1 . r . . - j.,1. , / v )
16.4A22
0.19u2
12=
tl.9t4l
The model coefficients can then be calculated as
>> 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 encompasses68Vo<>fthe 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 /<
497
l-1.1-1Fit an exponentialmodei to
04
800
x
v
08
975
I',2
I 500
1.6
I 950
2
2900
23
3600
Plot the data and the equationon both standardand semilogarithmicgraphpaper.
13.14 An investigatorhasreportedthe datatabulatedbelow
fbr an experimentto determinethe growth rate of bacteria
ft (per d) as a function of oxygen concentrationc (mg/L). It
is known that such data can be modeled by the fbllowing
equatiou:
t a
,
/< :
Krnaxc---;
cr+c'
where c, and ftnra\are parameters. Use a transformation ro
linearize this equation. Then use linear regression to estimate c. and ftn,.,xand predict the growth rate at c :2 mg/L,.
c
Whenusedfbr curve iitting, this equatior'lyields identical
results
to the base-r,version, but the value of thL-exponent
(lr) will diff'erfrom that estirnated
parameter
with Eq.13.22
(0,).Usethe base-IOversionto solve Prob. 13.9.In addidevelopa formulation to relateut ro frs
tion,
l3,ll On average.the surface area A of human beings is
rclated
to weight W and height H. Measurementson a nunberof individualsof height 180 cm and dil-lerentweishts
(kg)-eivevaluesof A (m2) in the lbllowing rable:
Metqbolism (wofts)
k
0.5
tt
OB
2.4
t5
53
25
76
4
B9
13.15 Developan M-file function to computedescriptive
stafisticsfor a vector of values.Have the function detennine
and displaynumberof values.mean,median.mode.range,
standarddeviation,r'ariiince.and coefficientof variation.In
addition,
huvcit gencralc
.rhisloglinr.
Trrslil wilhlhcrl;tl;r
lromProb.13.3.
13.16 Modify the lrnresr funcrionin Fig. 13.l3 so that it
(a) computesand returnsthe standarderror of the estimate,
and (b) usesthe sDbptor functionto alsodisplaya plot of
W(kS)7A 75 77
BO 82 84 87 90
the resicluals(the predictedminLrsthe measured-r.)versus-r-.
A ( r n 22l 1 0 2 t 2 2 t 5 2 2 A 2 2 2 2 2 3 2 2 6 2 3 0 13.17 Develop
an M-file function to fit a power model.
Have the function return the best-fit coefficient a2 and
powerB, along with the 12fbr the untransfbrmedmodel. In
Show
that a power law A : oil' fits thesedata reasonablv addition,usethe .jubp l ot function
to displaygraphsof both
well.Evaluatethe conslantsa and b. and predict what the the transfbrmedand
untransformedequationsalong with the
orrf.^a.rpr
ic fnr r Q5_Lo nersrrn
data.Test with
3r4
LINEARREGRESSION
l3.ltl The tbllowing data shows the relationshipbetween
the viscosityof SAE 70 oil and temperature.
After taking the
log of the data,use linear regressionto find the equationof
the line thrt bestfits the dataand the r2 value.
are shown beloll,.Which model bestdescribesthedata(sl&
tistically)?Explain your chorce.
T e m p e r o t u r e ,' €
Viscosiry p,N.s/m2
s"
26.67 93 33
I 35 O0B5
I48 Bq 3I5.56
0 012 OOoO/5
13.19 You perform experimentsand determinethe fbllowing valuesoi heat capacityc at varioustemperaturesZ for a
gas:
T50-30060
l25O I 280
c
I 350
I480
90
I 580
ilO
l70A
Use regressionto determinea nrodel to predict c as a function of I.
13.20 lt is known that the tensile strengthof a plastic increasesas a tunction of the time it is heat treated.The following data is collected:
Time
l0 )5 2A 25 40 50 55 60 Z5
TensifeStrength 5 2A I B 40 33 54 70 6A 78
Model A
135
Number of Model
Porometers Fif
2
Model B
r05
ModelC
i00
3
13.23 Below is data taken from a batch reactorofbactenal
growth (after lag phasewas over). The bacteriaareallowed
to grow as fast as possiblefor the first 2.5 hours,andthen
they are inducedto producea recombinantprotein,thepro
duction of which slows the bacterial growth significantly.
The theoreticalgrowth of bacteriacan be described
by
'4:
dt
r,x
where X is the number of bacteria,and p is the specrfic
growth rateof the bacteriaduring exponentialgrowth.Basd
on tbe data,estimatethe specificgrowth rate of thebacteria
duling the tirst 2 hoursof growth and during thenext4 houm
of erowth.
Time,
(a) Fit a straight line to this data and use the equation to
h0t23
determinethe tensilestrengthat a time of 32 min.
ICells],
(b) Repeat tl.reanalysis but for a straight line with a zercr
S l L 0 1 0 0 0 3 3 2 t t 0 2 I 6 4 4 2 453 3 660 5 460
intercept.
13.21 The following data was taken from a stirred tank re13.24 A transportationcngineeringstudy was conductedto
actor for the reactionA - B. Use the data to determinethe
dctL'rminethe properdesignof bike lanes.Datawasgathered
best possible estimatesfor ft,,, and E, fbr the following
on bike-lanewidths and averagedistancebetweenbikesand
kineticmodel:
passingcars.The clatafrorn I I streetsis
dA
- ---:- :
AT
A0le
A
where R is the gas constantand equals0.00198kcal/mol/K.
-dAldt (moles/Us)
A (moles/t)
r (K)
46A 960 2485 l600
200 150 50
20
2BA 320 45O 500
14.4<
Dislonce,m 24l524IB1.82.5
l2 312
l o n e W i d r hm
, 2 9 2 . 1 2 . 3 2 1 I B 2 7 1 . 52 9 1 5
(a) Plot the clata.
(b) Fit a straightline to the data with linearregression.
Add
this line to the plot.
(c) If the minimum safeaveragedistancebetweenbikesand
passingcars is consideredto be 2 m, determinethecor.
points
13.22 Concentrationdatawas takenat l5 time
for the
minimum lanewidth.
responding
polyrnerizationreaction:
11.25 In water-resourccsengineering,the sizing of reserxA*,tB - A,B:.
voirs dependson accurateestimatesof water flow in the
We assumethe reaction occurs via a complex mechanism river that is being impounded.For some rivers, long-term
consistingof many steps.Severalmodelshavebeenhypoth- historicalrecordso1'suchflow data are difficult to obtain.In
esized,and the sum of the squaresof the residualshad been contrast,meteorolo-vical
data on precipitationis often avail^^l^,,1^r^J
f^-
.L^
f:r^
^f
rL.-
'-..'J,-1.
^f
rli^
,l^.-
TL,.
t0
550
-,,-,,1..
determir
This rela
when or
followin
Procipifqtion,
Flow,
m3/s
(a) Plot
(b) Fit r
Supt
(c) Use
the 1
(d) rf th
of th
ratio
use.
13.26 "t)
10.65cn
alloy. Te
tween stl
Slroin,
cm/cm
Stress,
N/<m2
The stres
force in r
value car
the mast
mast's le
estimate
13.27 Tl
that mee
voltages
V,V
irA
(a) On
cur
an{
(b) Re
PROBLEMS
315
determine
a relationshipbetween flow and precipitation. 13.28 An experiment is performed to determine the Voelonthisrelationship
can then be usedto estimateflows for years gation of electricalconductingmaterialas a function of temwhen
only precipitation measurementswere made. The perature.The resulting data is listed below. Predict the 7o
following
datais availabletbr a river that is to be dammed: elongationfor a temperatureof 400 'C.
?ncipit o t i o nc, m B B9 l 0 B 5 1 0 4 I
lbw
m3/s
1 4 6 1 6 7 1 53
1 3 9 . 71 2 7 9 4 r 1 6 B 9 9 1
232 195 )61 II I
*"
cm/cm 0 0 0 3 2 0 0 0 4 5 0 0 0 5 5 0 0 0 r 6 0 0 0 8 5 0 0 0 0 5
5lrcss,
N/cm2 4970
5170 .5500 3590 6900 t24A
thestress
causedby wind can be computedasF/A, whereF :
in themast and /. : mast'. cross-sectionalarea.This
force
value
canthenbe substitutedinto Hooke's law to determine
themast'sdeflection, Al- : strain x l, where L : the
mast's
length.If the wind force is 25,000 N, use the data to
estimate
thedeflectionof a 9-m mast.
The following data was taken from an experiment
13,27
thatmeasuredthe cunent in a wire for various imposed
voltages:
i,A
2
52
3
7B
4
lo7
5
t3
7la
1 93
475 600
127 153
16.6
(a)Plotthedata.
0) Fit a sffaight line to the data with linear regression.
Superimpose
this line on your plot.
(c)Usethe besffit line to predict the annual water flow if
theprecipitation
is 120cnr.
(d)If rhedrarnage
areais I 100km:. estirnatewhat fraction
of theprecipitationis lost via processessuch as evaporation,deep groundwaterinfiltration, and consumptive
use.
The mast of a sailboat has a cross-sectionalarea of
13,26
cmr and is constructedof an experimentalaluminum
10.65
Testswere performed to define the relationshipbeallo1.
tween
stressand strain.The test resultsare
v,v
Temperofure,oC 200 25a 300 375 425
T"Elongotion 75 86 87
lO ll3
27.5
(a)0n thebasisof a linear regressionof this data,determine
cunentfor a voltage of 3.5 V. Plot the line and the data
andevaluatethe fit.
(b)Redothe regressionand force the interceptto be zero.
11.29 The populationp of a small community on the outskirts of a city grows rapidly over a 2O-yearperiod:
ta
p
1OO
to
450
5
204
t5
950
20
2000
As an engineer working for a utility company, you must
forecast the population -5years into the future in order to anticipate the demand for power. Employ an exponential
model and linear regressionto make this prediction.
13,30 The velocity r of air flowing past a flat surface is
measuredat severaldistancesl away from the surface.Fit a
curve to this data assumingthat the velocity is zero at the
surface(y : 0). Use your resultto determinethe shearstress
(du/dy) at the surface.
J,h
u, mls
0.002
A 2B7
0006
0 899
o0l2
r9r5
00rB
3048
oo24
4299
13,31 Andrade'secluationhas beenproposedas a model of
the elfect of temperatureon viscosity:
P:
PsB/r"
where p : dynamic viscosity of water ( l0-3 N's/m2), 4 :
absolutetemperature(K), and D and B are parameters.Fit
this model to the following data for water:
705r0203040
p
I 7B7
I .519
I 3A7
I 002
O7 9 7 5
A.6529
" l
rol-,l,,
GenerolLineorLeost-Squores
ond
NonlineorR"gression
CHAPTEROBJECTIVES
This chaptertakesthe conceptoffitting a straightline and extendsit to (d) fitting a
polynomialand (D)fitting a variablethat is a linear function of two or more independent
variables.We will then show how suchapplicationscan be generalizedand appliedto a
broadergroup of problerns.Finally, we will illustratehow optimizationtechniquescanbe
usedto implementnonlinearregression.Specilicobjectivesand topicscoveredare
o
o
o
o
.
I4.I
Knowing how to implementpolynomial regression.
Knowing how to implementmultiple linear regression.
Understandingthe formulation of the generallinear least-squares
model.
Understandinghow the generallinear least-squares
model can be solvedwith
MATLAB usingeitherthe normalequationsor left division.
Understandinghow to implementnonlinearregressionwith optimization
techniques.
POTYNOMIAL
REGRESSION
In Chap.13, a procedurewas developedto derive the equationof a straightline usingthe
least-squares
criterion. Some data, although exhibiting a marked pattern such as seenin
Fig.lrt.f.ispoorlyrepresentedbyastraightline.Forthesecases,acurvewouldbebetter
suited to fit the data.As discussedin Chap. 13, one method to acconrplishthis objective
is to use transformations.Another alternativeis to fit polynomialsto the datausingpolynotnial regression.
procedurecan be readily extendedto fit the data to a higher-order
The least-squares
polynomial. For example,supposethat we fit a second-orderpolynomial or quadratic:
,y:uotal.r+
a z r :+ e
(14.1)
317
REGRESSION
POLYNOMIAL
(b)
I4.I
FIGURE
( b )l n d i c o i i otnh o io p o r o b o l o
is
regression
forlineor
leost-squores
(o)Dotoihotis ill-suited
preferoble.
For this casethe sum of the squaresof the residualsis
II
(y, - nu- atxi - orri)t
s, : t
\14.2)
i-l
fit, we take the derivativeof Eq. (14.2) with respectto
To generatethe least-squares
of
the polynomial, as in
coefficients
eachof the unknown
as,
d0t
:
at
:
;d0t
aS,
:--
rtu.
:
-/'
.\-r
)
\.\',
-c'ltt -a,'t,
-t'1*;)
- us- d1x;-t^sa
) .xi ()l
-L r
\/
. . 2l -
rt
\-\r
a2xl)
- a0 - a1x1 - u7.tl)
These equationscan be set equal to zero and reananged to develop the following set of
normal equations:
z )z : L v i
( n ) a o +( I t ' ) a r + ( f - t ' ' a
(I r') oo+ (I-t,t) n' + (t ti) a2: lxiYi
(Ir,?) oo+ (T.",1)"' + (I-'1) tL2: lx!)'i
318
AND NONLINEARREGRESSION
GENERAL
LINEARTEAST.SQUARES
where all summationsare from i : I through n. Note that the precedingthreeequations
are
linear and have three unknowns:a0, ctt, anda2. The coefficientsof the unknownscanbe
calculateddirectly from the observeddata.
For this case.we see that the problem of determininga least-squares
second-order
polynomial is equivalentto solving a systemof three simultaneouslinear equations.
The
two-dimensionalcasecan be easily extendedto an mth-orderpolynomial as in
! : a o t d l x f a 2 x 2+ . . . a a . x ' ' I e
The foregoinganalysiscan be easily extendedto this more generalcase.Thus,wecan
to
recognize that determining the coefficients of an mth-order polynomial is equivalent
enot
solving a systemof m I I simultaneouslinear equations.For this case,the standard
is formulatedas
s,
(14.3)
n - (m -l1)
This quantity is divided by n - (m I l) because(m * l) data-derivedcoefficientso0, at,..., an-were usedto computeSr; thus,we have lost z * I degreesof freedom.
fot
In addition to the standarderror, a coefficient of determination can also be computed
polynomialregression
with Eq. (13.20).
.l4.1
EXAMPLE
Polynomiol
Regression
polynomialto thedatain thefirst twocolumns
ProblemStotement. Fit a second-order
ofTable14.1.
TABTEl4.l
fif.
for on erroronolysisof the quodroticleost-sguores
Compuiotions
(Ji -J)-
v,
xi
0
I
2
2l
77
t3.6
272
40.9
6t t
152.6
j?
4
5
I
544 44
314.47
1 4 00 3
3.12
239.22
1272.1)
2 5 r 33 9
Soluiion. The following can be computedfrom the data:
\'-. - r<
n:6
I l'i : 152.6
Lx!:9tg
I",)t :585'6
;-r<
\-'l-ss
L*?Y,: 2488'8
r-)5477
\L '-- 't J - l l s
-
/r ^t
r J
tg,- ao-arx,-aS)r
0.t 4332
r 00286
r 08r60
0 80487
0 6 19 5 9
0 09434
3.74657
]4, 1 POIYNOMIAL
REGRESSION
319
'i,
tr iil'i,l:hfx
Therefore,the simultaneouslinear equationsare
These equations can be solved to evaluate the coefficients. For example.
using MMLAB:
>> N = l6 15 55;15 55 225;55 225 9j9);
>> r = 1152.6 535.6 24BB.Bl;
>> a = l.i\r
2 .41 B6
2.3593
1.8507
Therefore. the least-squaresquadratic equation for this case is
! :
2 . 4 1 8 6* 2 . 3 5 9 3 r * 1 . 8 6 0 7 1 2
The standard error of the estimate based on the regression polynomial is
tEq. (14.3)l
|
1,.,.:
/
_1./+b) /
!o-rz+ l)
:lll75
""'-
The coefficientof determinationis
r-
-
- 3.74651
2513.39
:0.99851
2513.39
and the comelationcoefficientis r :0.99925.
F I G U R EI 4 . 2
Filof o secondorderpolvnomiol
320
LINEARLEAST-SAUARES
GENERAL
AND NONLINEARREGRESSION
These results indicate that 99.851 percent of the original uncertaintyhas beenexplainedby the model.This resultsupportsthe conclusionthat the quadraticequation
representsan excellentfit, as is alsoevidentfrom Fig. 14.2.
14.2 'T4UITIPIE
TINEARREGRESSION
Another useful extensionof linearregressionis the casewheref is a linear functionof two
or more independentvariables.For example,1 might be a linear function of r, andxr. asin
. y : r r o { c t 1 - t 1l a 2 x z a s
Such an equationis particularly useful when fitting experimentaldata where the variable
being studiedis often a function of two other variables.For this two-dimensionalcase.the
regression"line" becomesa "plane"(Fig. 14.3).
As with the previous cases,the "best" values of the coefficientsare determinedby
formulating the sum of the squaresof the residuals:
$ : i
()'i - ao-(r-tl.i - az.rt.i)?
and differentiatingwith respectto eachof the unknown coefficients:
aS,
;-:-l)
dao
as,
,:- :
d L ll
as,:
;-
OQ)
_\-r. ( l ' i
- 2^
11
)
- t t 1 1 - t 1 1 . x 1 . -1o 2 x 2 . i l
xt.i()i - (t0-
^ \--
-2
) .r2.,0"i
a1xl.i - azxz.i)
- ao - cttxt.i * azxz.i)
FIGUREI4.3
Grophicol
depiction
y iso lineor
of multiple
lineor
regression
where
function
of.r,ondrr.
_-__---!-_
//
(r4..1)
EXA
I4.2 MULTIPLE
LINEARREGRESSION
32r
The coefficientsyielding the minimum sum of the squaresof the residualsare obtainedby
settingthe partial derivativesequal to zero and expressingthe resultin matrix form as
I*'.,
Irr., Ifoo
f n
I
"
'
t
.
,
I
"
t
i
.
'
I
x
r . i r z . iI l a r
I
L I tr.r Ir,.,.rr.,
I .rt., _.1[ nt
l
;?;r:
(14.5)
EXAMPLE
14.2 Multiple
Lineor
Regression
Problem Sfotement. The following data was calculated from the equation y : 5 +
4x1- 3r2'.
.rt
x2
00s
2
2.525
r30
I
t0
463
7227
Use multiple linear regressionto fit this data.
Solution. The summationsrequired to develop Eq. (1a.5) are computedin Table 14.2.
Substitutingthem into Eq. (14.5)gives
16.5 rll
[ 6
1
6
.
5
1
6 . 2 s4 8 |
I
48 54)
L t4
a0
Al
A1
54
243.5
100
(14.6)
which can be solvedfor
(10:5
at:4
aZ:-3
which is consistentwith the original equationfrom which the datawas derived.
The foregoing two-dimensionalcasecan be easily extendedto m dimensions,as in
) : a oI a 1 x 1* a 2 x 2 + . " + a r x ^ * e
TABTE
I o:? c:Tp':olio1'.:"".:'i':ddey3lon '::::l
fo1Elomele
1o
li:.
::q,,oti:,i:
la'2
!
50
t0
9
0t
34
277
-a4
xr
2
25
0)
xz
0
I
2
3
6
2
t4
x!
xl
xrx2
.rilr
0
4
6.25
I
t6
49
0
l
4
I
36
4
o
2
5
3
24
l4
,/a.l)
J4
T6
0
20
225
0
l2
t89
)77-
x2y
0
t0
IB
0
]B
\,4
lao
322
LINEARLEAST-SQUARES
AND NONLINEARREGRESSION
GENERAL
where the standarderror is formulatedas
s
n-(nt+D
and the coefficientof determinationis computedas in Eq. ( 13.20).
Aithough therernarybe certaincaseswherea variableis linearly relatedto two or mole
other variables,multiple linear regressionhas additionalutility in the derivationof power
equationsof the generalform
x;t .' . x::i,"
)' : ctox';t
SuchequationsAreextremelyusefulwhen fitting experimentaldata.To usemultiplelineat
regression,the equationis transformedby taking its logarithm to yield
l o g , y: l o g a 0* a 1l o g x l * a 2 l o g x 2 + ' ' ' + a n l o g t .
I4.3
GENERATTINEARLEASTSQUARES
In the preceding pa-qes,we have introduced three types of regression:simple linear,
polynomial, and multiple linear. In fact, all three belong to the following generallinear
model:
least-squares
-l'''
) ' : a o z oI a r z t * a y Z z
I a n , z mI €
(14.?)
where :0, zl,..., z.marem * I basisfunctions.It can easilybe seenhow simplelinear
and multiple linear regressionfall within this model-that is, ;s: l, zr :.rl,t2=
is alsoincludedif the basisfunctionsate
x2,. .., Znt:.xm. Further,polynomialregression
:
:
:16m.
:
l,;r
x,.tt
x2,..., Zp1
s i m p f em o n o m i a lassi n : o
"linear"
to
the model's dependenceon its
refers only
Note that the terminology
polynomial
regression,the functionsthem'
parameters-that is, the a's. As in the caseof
selvescan be highly nonlinear.For example,the :'s can be sinusoids.as in
y : ao * a1 cos(a;r) I azsin(o_tx)
Such a format is the basisof Fourier analysis.
On the other hand, a simple-lookingmodel suchas
):
do(l -
e-ol')
is truly nonlinearbecauseit cannotbe manipulatedinto the format of Eq. ( 14.7).
Equation(14.7) canbe expressedin matrix notationas
(14.8)
[y]:fzl{a}+lel
values
where[Zl is a matrix of the calculatedvaluesof the basisfunctionsat the measured
of the independentvariables:
zll
t7l t-t-
.
[:::
:*t-f
a,:,,l
EXA
14.3 GENERAL
LINEARLEAST
SAUARES
323
where m is the number of variablesin the model and n is the number of data points.
Becattsen > m * 1, you shouldrecognizethat most of the time, [Z] is not a squaremarrix.
The column vector {y} containsthe observedvaluesofthe dependentvariable:
{-v}r :
L,vr !2.
-}nl
The column vector ia] containsthe unknown coefficients:
{a)r :
la11 at
a , ,)
and the column vector {e} containsthe residuals:
{e}r :
le1
e2
e u)
The sum of the squaresof the residualsfor this model can be definedas
n
s,:Ilt,
i-l
/
.
\2
-lo1r1,l
\
j=tt
(14.9)
/
This quantity can be minimized by taking its partial derivativewith respectto eachof the
coefficientsand settingthe resultingequationequalto zero.The outcomeof this processis
the normal equationsthat can be expressedconciselyin matrix form as
[ z ] r V \ { a } : { t z l r{ y } )
( 1 4r.0 )
It can be shownthat Eq. (14.l0) is, in fact,equivalenfto the normalequationsdeveloped
previouslyfor simple linear,polynomial, and multiple linear regression.
The coefficientof determinationand the standarderror can alsobe formulatedin terms
of matrix algebra.Recallthat r: is definedas
t
S,-S, -
s,$
| _ _S ,
Substitutingthe definitionsof S, and S, gives
r--l-
I(vr - -urt'
I(),r
- .ti t2
where! : the predictionof the least-squares
fit. The residualsbetweenthe best-fit curve
and the data,y; - j, can be expressedin vector form as
{y} - f"zlla}
Matrix algebracan then be usedto manipulatethis vector to computeboth the coefficient of
determinationand the standarderror of the estimateas illustratedin the following example.
EXAMPLE
I4.3
P o l y n o m i oR
l egressiow
n ith MATLAB
ProblemSiotement. RepeatExample14.1,but usematrixoperations
asdescribedin this
section.
Solution. First,enterthe datato be fit
>> x - t0 12 3 4 5l';
> > y = 1 2 . I 1 . 1 1 3 . 6 2 ' 7. 2 4 A . 9 6 1 . 1 1 ' ;
324
GENERATL\NEARIEASI.SQUARESAND NONI\NEAR RIGRESS\ON
Next, create the [Z] matnx:
>>
Z
=
[ones(slze(x)
)
x
x.^2]
100
111
1,24
139
7416
1
5
25
We can verify that LZf lzl resultsin the coefficientmatrix for the normal equations:
>> z'*z
15
ss
225
6
15
55
55
225
979
This is the sameresultwe obtainedwith summationsin Example 14.I . We can solvefor the
quadraticby implementingEq. (14.l0):
coefficientsof the least-squares
>> a =
\Z'*Z) \(Z'*y)
2 .47 B6
2.3593
1.8607
EX
In order to compute 12 and s"7-,, first compute the sum of the squares of the residuals:
> > 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<tth
interpolations
are shownin Fig. 15.3,alongwith the true function.
| 5.2.2 GluqdroticInterpolotion
The error in Exaniple 15.2resultedfrom approximatinga curve with a straightline. Consequently,a strategyfor improving the estimateis to introducesomecurvatureinto the line
with a
connectingthe points.If threedatapointsare available,this can be accomplished
second-orderpolynomial (alsocalled a quadraticpolynomial or a parabola).A particularly
c o n v c n i e nltb r r nl b r t l r i sp u l p o s ei s
l : ( x ) : b t * b z \ - : r r ) - | D : ( . t- . r r ) ( x - : ; z )
(15.6)
A simple procedurecan be used to determinethe values of the coefTicients.For b1,
Eq. (15.6.twith -r :.rl can be usedto conrpLlte
b1: .f (.xy)
(15.7)
Equation(15.7)can be substitutedinto Eq. (15.6),which can be evaluatedat r : -r2 for
.f (x) - l'(,tr)
,
(15.8)
br:"{2
- .\'l
into Eq. (15.6),which can be evaluatedat
Finally,Eqs.(15.7)and (15.8)can be substituted
for
-t : -r-.rand solved(after some algebraicrrranipulation.s)
.f(-t:)- /(-r:)
h--
1l
-
f(.rz)- l(.rr)
.{2
{.1 - .rt
(I 5.9)
342
POLYNOMIAL
INTERPOLATION
Notice that, as was the casewith linear interpolation,b2 still representsthe slopeofthe
line connectingpoints xy and "r2.Thus, the first two terns of Eq. (15.6) are equivalent
to
linear interpolationbetweenx, and rr, as specifiedpreviouslyin Eq. (15.5).The lastterm,
b:(-r - xr)(.r -.r:2), introducesthe second-ordercurvature
into the formula.
Before illustrating how to use Eq. (15.6), we should examinethe form of the coefficient &.,.It is very similar to the finite-differenceapproximationof the secondderivative
introducedpreviously inEq. (4.27').Thus, Eq. (15.6) is beginningto manifesta structure
that is very similar to the Taylor seriesexpansion.That is, terms are addedsequentially
t0
captureincreasinglyhigher-ordercurvature.
.l5.3
EXAMPLE
Q u o d r o t i cI n i e r p o l o t i o n
Problem Stotement. Employ a second-orcler
Newton polynomial to estimateln 2 with
the samethreeooints usedin Example 15.2:
rl:1
"/(xt):0
.\z:4
J$):1.38629.1
r-r : 6
-f(.r:) : l.'791159
S o l u t i o n . A p p l y i n gE q . ( 1 5 . 7 )y i e l d s
bt:o
Equation( 15.8)gives
b 1:
|.386294- 0
_:0.4610981
+- |
F I G U R EI 5 . 4
T h eu s eo f q u o d r o t ircn t e r p o o t i o
l one s tm o l eI n 2 . T h el i n e o irn l e r p o l o i ifor no m , r : I t o4 i s
o l s oi n c l u d efdo rc o m p o r i s o n .
| \.r)
2
fl.r) : In.t
Ouadraticestimate
Linearestimate
343
I 5.2 NEWTONINTERPOLATING
POLYNOMIAL
a n d E q . ( 1 5 . 9 )y i e l d s
- 1.386294
1.791759
6-4
6- I
- 0.462098r
: -0.0518731
Substitutingthesevaluesinto Eq. (15.6)yieldsthe quadraticformula
. f z @ ): 0
+ 0 . 4 6 2 0 9 8 1 ( -r l ) - 0 . 0 - 5 1 8 7 3 1 -( x l ) ( : r - 4 )
which can be evaluated At ,r : 2 for fz(2) : 0.5658444, which representsa relative
error of e, : 18.47o.Thus, the curvatureintroducedby the quadraticformuia (Fig. 15.4)
improves the interpolation cornparedwith the result obtained using straight lines in
Example15.2and Fig. 15.3.
| 5.2.3 Generol
Form of Newfon's
Interpolofing
Pofynomiqfs
The precedinganalysiscan be generalizedto fit an (zz- l)th-order polynornialto n data
points.The (n - I )th-orderpolynomialis
. f " _ t f ) : b r 1 -b z j - x r ) * . . . * b u \ - ; r r ) ( x - x ) . . . ( x * - t , _ r )
(15.10)
As was done previously with linear and quadraticinterpoliition,datapointscan be usedto
e v a l u a t et h e c o e f f i c i e n tbs1 . b 2 , . . . , b , . F o r a n ( n - 1 ) t h - o r d epr o l y n o m i a ln, d a t ap o i n t s
a r e r e q u i r e dl r:r , l ' ( - r r ) 1 , l x z , J Q ) 1 , . . . , [ - r ,f ,( x , 1 1 .W e u s e t h e s e d a t a p o i n t s a n d t h e
following eqnationsto evaluatethe coefficients:
b1:fQ1)
( 1 5 .l r)
6' : .f'[r2,.r]
(1s.12)
f u : f f v , x z ,x t l
:
(r5.r3)
b , ,: f l x , r ,x n - 1 ,. . . , " r z , , r r ]
(r-5.14)
where the bracketedtunction evaluationsare finite divided differences.For exanrole.tlre
first finite divided differenceis representedgenerallyas
r,
,
J l ^ i . r"i l :
-I k i t - f ( r l )
-t, _ _yl
( 1 s1. 5 )
The secondfinite divided difference.whichrepresents
the difference
of two first divided
differences,is expressedgenerallyas
f lx;, .q, x1,l:
f [ x ; ,. q J- f [ r i . x r l
(15.16)
ri - .rt
Similarlv. the nth finite divided differenceis
.\n -
( rs . 1 7 )
"Yl
344
POLYNOMIAL
INTERPOLATION
xi
f(x)
First
Second
';:rl it::ti=;[] .:..l
ii,j.':^:),a4t;'^'
xl
Third
r flxo, xt,x2,xl
FIGURE
I5.5
Grophicol
depiction
is
of iherecursive
noture
of finitedivided
Thisrepresentotion
differences.
,eferred
to oso divided
difference
toble.
Thesedifferencescan be usedto evaluatethe coefficientsin Eqs.(15.11)through(15.14),
which can then be substitutedinto Eq. (15.10)to yield the generalform of Newton'sinterpolating polynomial:
f,-rG):.f
( x t ) + ( r - x t ) f l x z . . r r l * ( " r- x r ) ( x - x ) f l . 4 , x 2 , x 1 l
+ . " + ( x - x r ) ( x - . r z ) . . . ( r - x n - t ) . f l x n , t r n - t , . . . , x z , x r l( 1 5 . 1 8 )
We should note that it is not necessarythat the data points used in Eq. (15.18)
be
equally spacedor that the abscissavaluesnecessarilybe in ascendingorder,asillustrated
in the following exarnple.Also. noticehow Eqs. (15.15)through(15.17)arerecursivethat is, higher-order differences are computed by taking differences of lower-orderdifferences(Fig. 15.5).This property will be exploitedwhen we developan efficientM-filein
Section15.2.4to implementthe method.
E X A M P L E1 5 . 4
N e w t o n I n t e r p o l o t i n gP o l y n o m i o l
Problem Siotement. In Example 15.3,data pointsat xt : l, xz:4, and,r3= ( 1ry91s
=
used to estimate ln 2 with a parabola. Now, adding a fourth point [xa : 5; ,f (.r,{)
L6094381,estimateln 2 with a third-orderNewton's interpolatingpolynomial.
Solution. The third-orderpolynomial,Eq. (15.10)with n : 4, is
f z @ ) : b r * b z @- x r ) * b z ? - x r ) ( , r - x ) * b q ( x- , r r ) ( x - x 2 ) ( x - x 3 )
The first divided differencesfor the problem ale [Eq. (15.15)]
1.386294- O
- :0.4620981
: -"f["r:.rr]
4 _,
f [4, x2]:
fr...
.,/ L^41
- _ l --
^-{l
- r.386294
r.791159
:0.2027326
6-4
- 1.19r'759
1.609438
:0.1823216
5-6
I 5.2 NEWTONINTERPOLATING
POLYNOMIAL
345
The seconddivideddifferences
are [Eq. (15.16)]
/[.t3, .t2, -r1] :
. f f x a , x 3 ,x 2 ) :
- 0.4620981
o.2021326
: - 0 . 0 5 1 8 7 3I 1
6-l
0.1823216- 0.2027326
: -0.02041100
5-4
The third divideddiff'erence
is [Eq. (15.17)with n :4]
J ' [ x + , r t . - t 2 ,x 1 ] :
-0.02041
r00- (-0.051873
1l)
J -
: 0.007865529
|
Thus. the divided differencetable is
f (xi)
j
0
386294
791759
609438
4
6
5
t)
e
d
n
a 462A9Bt
0.2427326
o l823216
-cc5lBZ3il
- 0 0 2 0 4 1r 0 0
00a786552a
T h e r e s u l t sf o r / ( x r ) , f [ x z , x t ] , . f [ x t , x z , x 1 ] , a n d f l x q , x z , x 2 , . t l l r e p r e s e ntth e
coefficientsb1,b2,b3, andb',, respectively,
of Eq. (l-5.10).Thus,the interpolatingcubicis
-f:(r) :0
+ 0 . 4 6 2 0 9 8 1 (-x l ) - 0 . 0 5 1 8 7 3 1 l (-" t l ) ( x - 4 )
+ 0.007865529(x- l)(x - 4)(r - 6)
which can be used to evaluate.fzQ) :0.6287686, which representsa relative error of
€t :9.3Vo. The completecubicpolynomialis shownin Fig. 15.6.
FIGURE
I5.6
The use of cubic interoolofion
lo estimoten 2
f(x)
z
346
INTERPOLATION
POLYNOMIAL
15,2.4
MATTAB M-file: Newtinr
It is straightforwardto developan M-file to implementNewtoninterpolation.As in Fig.15.7,
the first step is to compute the finite divided differencesand storethem in an array.Thedifferencesare then usedin conjunctionwith Eq. (15.18)to per-formthe interpolation.
An exampleof a sessionusing the function would be to duplicatethe calculation
we
just performedin Exarnple15.3:
>>
form.et
= tl
>> x
Longt
4
6
5l
FIGUREI5.7
An M fl e lo imp ement Newton inferpolotion.
= Newtint(x,y,xx)
function yint
polynomial
I'Jewton interpolal-ing
% Newtint:
= llewtrnt(x,y,xx):
Uses an (n - 1)-order Newton
? yint
,x, yl
polynrmial
inr erpofdtrng
"o
bosed or n dala poinrs
lyrnt)
%
to determine a vafue of the dependent variable
at a given value of the independenc variable,
xx.
%
% input:
:-.lanor
F
va ' ' rh'c
1Fr
x%
z
v-
%
xx=
r.lanonrlanf
value
z
Intrlyn^
? output
yint
%
rr:ri
:h-l
o
independent
of
;-
a-'o-
fdf
variable
at
which
C-IaLeO
i
=
inierpolated
!} .omnrl e rlre finir-e
value
divided
of
dependent
differences
variable
in
the
form of
a
% difference
table
n = length(x);
: r
1Fr
ifnlv,-:-
Fr-^1
!
| s
rli
z
mr
c
ho
camo
etnt
hri:
6ncl
b = zeros (n,n) ;
? assign dependenL variables
to the first
column of b.
= y(:);
b(:,1)
% the (:) ensur:es that y is a column vector
for j - 2,n
for i = 1:n j+1- (b(i+1,j-1)-b(i,j-1))/(x(i+j-r)-x(i))
b(i,j)
;
end
end
? use Lhe finite
1.ts
,
1
divided
-
= b(1,i);
for j = 1;n-1
xr
xt"{xx
x(jr);
= yrnt+b(1, j+1) *xt;
yint
yint
end
differences
to
inLerpolate
I 5.3 LAGRANGE
INTERPOLATING
POLYNOMIA.
347
> > y = t o S f( x ) ;
>> Newtint(x,y,2)
0.6287685"t890841
I5.3
TAGRANGEINTERPOTATING
POTYNOMIAT
Supposewe formulatea linear interpolatingpolynomialas the weightedaverageof the two
valuesthat we are connectingby a straightline:
./(.r): Lt J @r)-t Lzf Qz)
(rs.r9)
where the Z's are the weightingcoefficients.It is logical that the first weightingcoefficient
is the straightline that is equal to I at x1 and 0 at x2:
xt-x2
Similarly,the secondcoefficientis the straightline that is equalto I at "r2and0 at,r1:
r
tr-trI
12 -
.rl
Substitutingthesecoefticientsinto Eq. 15.19yields the straightline that connectsthe
p o i n t s( F i g . 1 5 . 8 ) :
..
:
"/r(x)
.r-X2'
xl-X2
^
J 6) -l
.\'-.Xr
X2-Xt
(1s.20)
J',6)
where the nomenclatnrefi(l) designatesthat this is a iirst-order polynomial. Equation (15.20) is refered to as the LinearLagrangeinterpolatingpolynomial.
The samestrategycan be employedto fit a parabolathroughthreepoints.For this case
threeparabolaswould be usedwith eachone passingthroughone of the points and equaling zero at the othertwo. Their sum would thenrepresentthe uniqueparabolathat connects
the threepoints. Such a second-orderLagrangeinterpolatingpolynomial can be written as
At.t
-
(.r-rr)(.r-xr)
( x r x z.). (- -- r l- x : ). J r i l r - r
+
(.t - . \ ' r ) ( r - . 1 r )
--.,-=1./
(r3-x1)(xj-r2)
/,r r-.l , \
-
(x-x1)(x-x3)
( . { : - x r ) ( . r :- - r t )
j
tt2t
( r5 . 2 1 )
N o t i c e l r o w t h e f l r s t t e r n r i s e q u a l t o / ( . r 1 ) aat n
1d
1isequaltozeroat.r2and.r-l.Theother
terms work in a similar fashion.
Both the first- and second-orderversionsas well as higher-orderLagrangepolynomials can be representedconciselyas
(1s.22)
348
INTERPOLATION
POLYNOMIAL
LtfU)
FIGUREI5.8
A v i s u o ld e p i c t i o no f t h e r o f i o n oe b e h l n dL o g r o n g ei n t e r p o l o i i npgo l y n o m i o l sT.h ef i g u r es h o w s
t h e f i r soi r d e r c o s e E o c h o ft h e t r " o l e r m s oE
[ q ( 1 5 2 0 ) p o s s e s t h r o u o h o n elohfe p o i n t s o n d
line
is zero ot the other.The summoiionof ihe two fermsmusi,iherefore,b"ethe uniquestroiqhl
thot connectsihe h^/opornts.
where
tl
_-
!
I
j=l
y.
^1
r
L;(.r): | | '
-fl
(1523)
_
| .
r/
.IFI
where ,r : the numberof datapoints and fl designatesthe "product of."
,|5.5
Interpoloting
Polynomiol
EXAMPLE
Logronge
polynomialof thefirst andsecond
ProblemStotement. Use a Lagrangeinterpolating
data:
orderto evaluate
thedensityof unusedmotoroil at T : 15'C basedonthefollowing
rr :0
f(xr):3.85
r::20
l(.r:):0.gtlO
.{::40
f(rt):0.212
polynomial
Solution. Theflrst-order
tEq.(15.20)lcanbe usedto obtaintheestimateal
,r: I5:
t 5 - 2-0
t 5 - o" 0 . 8 0 0 : 1 . 5 6 2 5
-1.85+'"
'f r( . r ) : ' "
0-20
t0-0
349
POLYNOMIA.
I 5.3 LAGRANGEINTERPOLATING
polynomial
is developed
as[Eq.( 15.21)]
In a similarfashion,
thesecond-order
-
/.{r):
(15-20)tl5-40.r^^_ (15-0)(15-40)^^^^
- i . 6 1+ - t ] . 6 t . , U
(20- 0)(20- 40)
(0 - 20)(0- 40)
+
(l5-O)fl5-20).,_,_
U.212:1.J316875
(40-0)(40-20)
| 5.3.1 MATTAB M-file: Lasranse
It is straightforward
to developan M-file basedon Eqs.(15.22)and (15.23).As in
(x) and the
Fig. 15.9,the functionis passedtwo vectorscontainingthe independent
(y) variables.
variablewhereyou
It is alsopassed
thevalueof theindependent
dependent
(xx). Theorderof thepolynomialis basedon thelengthof thex vector
wantto interpolate
If n valuesarepassed,
an (n - 1)thorderpolynomialis fit.
thatis passed.
FIGUREI5.9
inlerpolction
AnMJilelo implement
Logronge
y nL - Lag"6ngerx,\,xxl
trrccion
polynomial
Z Lagrange:
Lagrange interpolatingr
= Lagr:ange(x,y,xx) : Uses an (n - 1)-order
yint
2
polynomial based on n data points
?
Lagrange inLerpolating
(yint)
at
to determine a value of the dependent variable
%
xx.
Z
a given value of the independent variable,
Z input:
x - independent
variable
Z
y=
%
dePendentvariable
xx = value of independent variable
at whlch the
%
is calculated
%
inter:polation
? output:
= interpolated
yint
value of dependenc variable
?
n = lenqth(x);
if lenqth(y)-=n,
-
_
error('x
and y must be same length');
n.
= 1:n
;
Produce - I'i
for j = 1;n
if i -= i
product = product* (xx-x(:)
for: i
end
end
s = s+prc,duct;
end
= s;
yint
) / (x(i) -x(j)
) ;
end
3so
POLYNOMIAIINTERPOLATION
An exampleof a sessionusing the function would be to predict the densityof airat
I atm pressureat a temperatureof 15 "C basedon the first lour values from Table15.1.
Becausefour valuesare passedto the function, a third-orderpolynomial would be implementedby the r-aqrange functionto gile:
>>
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(;<,
y,
rr)
cngrn'X);
xx
error
<
r(1)
xx > x(n)
(' Inter1;,olation
outside
end
13 seclLrential
range'
)
search
i - r;
while (1)
if xx .= x(i + 1), break, end
i = i + 1;
end
? linear .interpolation
yr = 1.(i) + (y(i+1) y(i))/(x(i+1)
x(i))*(xx
x(i));
The table's independentvariablesare storedin ascendingorder in the arrayx andthe
dependentvariablesstoredin the arrayy. Before searching,an error trap is includedtoen. breakloop
s u r e t h a t t h e d e s i r e d v a l u e x x f a l l s w i t h i n t h e r a ntgheeox f' s . A r r r [ ] l s .
comparesthe value at which the interpolationis desired,xx, to determinewhetherit isless
thanthevalueatthetopoftheinterval,x(i+1).Forcaseswherexxisinthesecondintetval or higher,this will not testtrue at first. In this casethe counter i is incremented
byone
so that on the next iteration,xx is comparedwith the value at the top of the secondintetval. The loop is repeateduntil the xx is lessthan or equalto the interval'supperbound,in
which casethe loop is exited.At this point, the inter-polationcan be performedsimplyas
shown.
because
For situationsfor which thereare lots of data.the sequentialsort is inetTicient
it must searchthrough all the precedingpoints to find values. In thesecases,a simple
alternativeisthe binan- search.Here is an M-file that performsa binary searchfollowed
365
I 6 . 3 Q U A D R A T IS
CP L I N E S
by linear interpolation:
yi
f unction
n
tf
= T,rbleLookBrn
(x,
Lenql-h (x ) ;
< x(1)
| zx > 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]<Bin (T,densit\',
- l5 O l
0.5705
This result can be verifiecl by the hand calculation:
os?5-o615
: 0.616
*
I (350)
1ffi(3s0
I6.3
- 300): 0.s70s
SPTINES
QUADRATIC
To ensurethat the nth derivativesare contiDuousat the knots.a splineof at leastn * I
order r.nustbe used.Third-orderpolynomialsor cubic splinesthat ensurecontinuousllrst
and secondderivativesare most frequently used in practice.Although third and higher
366
SPLINES
AND PIECEWISE
INTERPOLATION
when usingcubicsplines,they usuallycannotbedetected
derivativescanbe discontinuous
visuallyand consequently
areignored.
Becausethe derivationof cubic splinesis somewhatirrvolved,we havedecidedtofirst
illustratethe conceptof splineinterpolationusing second-orderpolynomials.These"quadratic splines"havecontinuousfirst derivativesat the knots.Although quadraticsplines
are
not of practicalin.rportance,
they servenicely to demonstratethe generalapproach
fordevelopinghi gher-ordersplines.
The objectivein quadraticsplinesis to derivea secoud-order
polynomialfor eachinterval betweendata points.The polynonial for each interval can be represented
generally
as
s i ( r ) : a i t b t Q - x i ) * c ; 1 x- r i ) 2
(16.5)
wherethe notationis as in Fig. 16.3.For n datapoints (i : 1,2, . . . , n), therearen - 1
(thed's. b's, andc's)to evaluate.
intervalsand,consequently,
3(n - l) unknownconstants
Therefore.3rrequationsor conditionsarerequiredto evaluatethe unknowns.Thesecanbe
developedas fbllows:
l.
The function must passthrough irll the points.This is called a continuitycondition.It
can be expressedmathematicallyits
.fi : ai I b,(.t;- xi) * c;(ri - x;)l
which sinrplifiesto
ai : Ji
EX
(16.6)
Therefore,the constantin eachquadraticmust be equal to the value of the dependent
variableat the beginningof the interval.This resultcan be incorporatedinto Eq.(16.5):
.r;(r ) :
2.
l, t- lt;\.t - .r;) f ri (.r - -r;)l
Note that becausewe have detenninedone of the coefficients.the nurnberof conditionsto be evaluatedhasnow beenreducedto 2(n - l).
The function valuesof adjacentpolynomialsmust be equalat the knots.This condition
can be writterrfbr knot i -F I as
.filbi\i+r
- t i ) f c i ( r i + r - x i ) 2 : . f i + r * b i + r ( x i + r - - r i + r ) f c ; a 1 ( - r ; -1-1t i + r ) 2
(1 6 . 7 )
This equationcan be simplified nrathematicallyby defining the width of the ith interval as
hi :xi+t-xi
Thus,Eq. ( 16.7)simplifiesto
.fi I bihl -t c;hl : fi+t
(16.8)
T h i s e q u a t i o cn a nb e w r i t t e nf o r t h e n o d e si,: 1 , . . . . n l . S i n c et h i sa m o u n t s t o
n-l
c o n d i t i o n si ,t m e a n st h a t t h e r ea r e 2 ( n - 1 ) - ( n - 7 ' ) : n 1 remaining
conditions.
I6.3 AUADRATIC
SPLINES
3.
367
The first derivativesat the interior noclesmust be equal.This is an importantcondition,
becauseit meansthat adjacentsplineswill bejoined smoothly,ratherthanin thejagged
fashionthat we saw for the linear splines.Equation( 16.5)can be ditferentiatedto yield
s j ( . r 1: b i l 2 c i i . r - x i )
The equivalenceofthe derivativesat an interiornode,i f I can thereforebe written as
bi *2ciht:bi+r
4.
116.v.1
Writing this equation for all the interior nodes amounts to ,?- 2 conditions.This
meansthat thereis n - | - (n - 2) : I remainingcondition.Unlesswe have some
additionalintbrmation regardingthe functionsor their derivatives,we must make an
arbitrarychoiceto successfullycomputethe constants.Although thereare a numberof
different choicesthat can be made,we selectthe fbllowing condition.
Assumethat the secondderivativeis zero at the first point. Becausethe secondderivativeof Eq. (16.5)is 2c;, this conditioncanbe expressed
mathematically
as
ct :0
The visual interpretationof this conditionis that the first two points will be connected
by a straightline.
E X A M P L1E6 . 2
Q u q d r o i i cS p l i n e s
Problem Stotement. Fit quadraticsplinesto the samedataemployedin Example16.I
(Table16.l). Use the resultsto estimatethe valueat,r : 5.
Solution. For the presentproblem,we havefourdata pointsand rz: 3 intervals.Therefore, after applying the continuity condition and the zero second-derivative
condition,this
meansthat2(4-1)-l:5conditionsarerequired.Equation(16.8)iswrittenfbri:1
through3 (with cr : 0) to give
.filbrlt1 :7,
l ' z * b 2 h 2- l c 2 h i : f t
-)
Jt lb,h,lcfi\:7.,
Continuityof derivatives,Eq. (16.9),creirtesan additional3 - I :2
recallthat cr : 0):
br:bz
b z* 2 c z h z : b t
The necessaryfurrctionand interval width valuesare
l?,
:4
.\ -
{
ll:
I
'\
J z: 1 . 0
h t : 1 . 0 - 4 . 5: 2 . 5
l.-?s
14:9.9-1.0:2.0
l,:o5
conditions(again,
368
SPLINES
AND PIECEWISE
INTERPOTATION
These valuescan be substitutedinto the conditionswhich can be expressedin matnx
form as
j
[Ii'i' ll{l}fl
Theseequationscan be solvedusingMATLAB with the resulrs:
br:-l
b:. : -1
,._ -
h,-))
r. . ,J . -- - 1
A A,l
6
These results, along with the values for the a's (Eq. 16.6), can be substitutedinto the
original quadraticequationsto developthe following quadraticsplinesfor eachinterval:
sr(x)-2.5-(x-3)
s ; ( . r ): 1 . 0- r - r- - 1 . 5+t 0 . 6 4 ( x - + . 5 ) r
. r . 1 ( x:) 2 . 5 + 2 . 2 ( . r- 7 . 0 ) - 1 . 6 ( r - 7 . 0 ) r
Becausex : -5lies in the secondinterval,we use.r2to make the prediction,
. r 1 ( 5 ): 1 . 0- ( - 5- 4 . . s )+ 0 . 6 . 1 ( -- s 4 . 5 1 2: 9 . 6 6
The total quadraticsplinelit is depictedin Fig. 16.4b.Notice that therearetwo shortcomingstlratdetractfrom the fit: (l) the straightline connectingthe first two pointsand
(2) the spline fbr the last interval seemsto swing too high. The cubic splinesin thenext
section do not exhibit these shortcomingsand, as a consequence,
are bettermethods
for
splineinterpolation.
16.4 CUBTC
SPL|NES
As statedat the beginningof the previoussection,cubic splinesare most frequentlyused
in practice.The shortcomingsof Iinearand qr"radratic
splineshave alreadybeendiscussed.
Quarlic ol higher-ordersplinesare not usedbecausethey tend to exhibit the instabilities
inherentin higher-orderpolynomials.Cubic splinesareprefered becausethey providethe
simplestrepresentation
that exhibitsthe desiredappearance
of smoothness.
Tlre objective in cubic splinesis to derive a third-orderpolynomial for eachinterval
betweenknots as representedgenerallyby
s , ( , r ) : a i ' l b , ( . r - . r , ) * c i ( . r - , r i ) 2 + r / , ( . r- . r i ) 3
(r6.r0)
T h u s , f o r n d a t ap o i n t s ( i : 1 , 2 . . . . , 1 ) ,
t h e r ea r e / , - I i n t e r v a l sa n d 4 ( n - l ) u n known coefficientsto evaluate.Consequently,4(,- l) conditionsare requiredfor their
evaluation.
I 6 . 4 C U B I CS P L I N E S
369
The tirst conditionsare identicalto thoseusedlbr the quadraticcase.Tlrat is, they are
set up so that the functionspassthroughthe pointsand that the first derivativesat the knots
are equal. In additronto these,conditionsare developedto ensurethat the secondderivatives at the knots are also equal.This greatlyenhancesthe fit's smoothness.
After theseconditionsare developed.two additionalconditionsare requiredto obtain
the solution.This is a much nicer outcomethan occurredfor quadraticsplineswhere rve
neededto specify a singlecondition.In that case,we had to arbitrarily specily a zero second derivativefor the first interval,hencemaking the resultasymmetric.For cubic splines,
we are in the advantageous
position of needingtwo additionalconditionsand can, therefore, apply them evenhandedlyat both ends.
For cubic splines,these last two conditions can be formulated in severaldifferent
ways.A very common approacl.r
is to assurnethat the secondderivativesat the first and last
knots areequalto zero.The visual interpretationofthese conditionsis that the function becomesa straightline at the endnodes.Specification
of suchan endconditionleadsto what
is termed a "natural" spline. It is given this name becausethe drafting spline naturally
behavesin this fashion(Fig. 16.2).
There are a variety of other end conditionsthat can be specified.Two of the more popular are the clampedcondition anclthe not-a-knotconditions.We will describetheseoptions in Section 16.4.2.For the following derivation.we will lirnit ourselvesto natural
splines.
Once the additionalend conditionsare specitied,we would havethe 4(n - 1) conditions neededto evaluatethe4(n - I ) unknowncoefficients.Whereasit is certainlypossible
to developcubic splinesin this fashion,we will presentan altenrativeapproachthat requires
the solutionof only n - I equations.Further,the simultaneousequationswill be tridiagonal
and hencecan be solved very efficiently.Although the derivationof this approachis less
straightforwardthan for quadraticsplines,the gain in efficiencyis well worth the effort.
16.4.1 Derivqtionof CubicSplines
As was the case with quadratic splines,the lirst condition is that the spline must pass
through all the datapoints.
J'i: ai I bi6i-
x i ) * c i ( , r r- . t i ) 2 * d i ( x r- x , ) l
which simplifies to
ai : .fi
(16.11)
Therefore,the constantin eachcubic must be equal to the value of the dependentvariable
at the beginningofthe interval.This resultcan be incorporated
into Eq. (16.10):
s ; ( - v ):
-l
-,ti)
J^i bi(r
* ci(x -
xi)2* di(-r -.r;;3
(t6.12)
Next, we will apply the conditionthat eachof the cubicsmustjoin at the knots.For
knot i * 1, this can be representedas
J ' i I b i h i - l c y h i - t t ) 1 h l: . f i + r
(16.13)
370
SPLINES
AND PIECEWISE
INTERPOLATION
where
hi:xi+t-xr
The first derivativesat the interior nodesmust be equal.Equation(16.12)is dift'erentiated to yield
sj(x) : bi {2c,(x - rr) * 3tli7 - xi)2
(r6.r4)
The equivalenceof the derivativesat an interior node,I * I can therefore be written as
bi l2ciht +3dihl : bi+l
(16.15)
The secondderivativesat the interiot'nodes
must alsobe equal.Equation(16.14)can
be difTerentiated
to yield
sj'(-r) : 2ci * 6di(x - xi)
( I6 . 1 6 )
The equivalenceof the second derivativesat an interior node, I f 1 can thereforebe
wntten as
ci l3dihi : ci+t
(1 6 . 1 7 )
Next, we can solveEq. ( 16.17)fbr d1:
t
u;:-
ci+l -
ci
(r 6r 8 )
J11;
This can be substituted
into Eq. (16.i3) ro give
h?
Ii I bihi -t lt2ci*
-t
c ; - 1) : f i * t
(r6.r9)
E q u a t i o n( 1 6 . 1 8 )c a na l s ob e s u b s r i r u r ei ndt o E q . ( 1 6 . 1 5 )r o g i v e
b i + t: b i I h i k i i
ci+r)
(r6.20)
Equation(t6.19) can be solvedfor
o, , :
f, ,r-ti
h,
hi
- i l / - c. ^i
(r6.21)
tc;r1)
The inder of this equationcanbe reducedbl' |;
,
l),-1 :
f, - f'-r
lli-1
hi- t .^
--llC;
(16.72)
trC;'l
-,
The index of Eq. (16.20)can alsobe reducedby l:
b i : b i - r * f t r r ( c i - r * c ,)
( 16.23)
Equations(16.21)and (16.22)can be sr"rbstituted
into Eq. (16.23)and the resultsimplified
to yield
fffr
I t 1 - 1 c 1 --1t 2 ( h i r - h i ) c i * h ; c ; , r : 3 1 I } - J '
h.;
* rJi - Jr-l
-t
( 16.21)
37r
I 6 . 4 C U B I CS P L I N E S
This equationcan be made a little more conciseby recognizingthat the terms on the
right-handsidearefinite differences(recallEq. 15.15):
/ t r i , . r i ]'
f.-f.
Jt
rl
-
ri-rl
Therefore,Eq. (16.2a) can be written as
/ r 1 - 1 c i - 1* 2 \ h r - r
3 ( . 1[ - " - i + r , . t i-l
- h)ci I hici+t:
ri
"f[.ri.
r])
( 16.2s)
- 2' which
Equation (16.25) can be written for the interior knots. i :2.3, '.. 'n
I unknown coefficients.
results in n - 3 sinrultaneoustridiagonal equations with n
cr, c2. ..., (-,r-r.Therefore,r'fwe havetwo additionalconditions,we can solvefor the c's.
Once this is done,Eqs.( 16.21)and (16.18)can be usedto determinethe remainingcoefficients.b and d.
As statedpreviously,the two additionalend conditionscan be torrnulatedin a number
of ways. One common approach,the naturalspline,assumesthat the secondderivativesat
the end knots are equal to zero. To see how these can be integratedinto the solution
s c h e r n e , t h e s e c o n d d e r i v a t i v e a t t h e f i r s t n o d1e6(.lE
6q
) c. a n b e s e t t o z e r o a s i n
s i ' ( , r r ): 0 : 2 c t
* 6 d 1( x 1- . r 1 )
Thus, this condition amountsto settingc1 eQualto zero.
The sameevaluationcanbe madeat the last node:
s j j - , ( x , ,:)0
:2c,-t
(16.26)
t 6d, tlt,-t
Recalling Eq. (16.17), we can convenientlydefine an extraneousparameterc,,, in which
caseEq. (16.26) becomes
c u - t * 3 d , , - l t r - 1 : c r r: 0
Thus, to impose a zero secondderivativeat the last node,we set ('/?- 0.
The final equationscan now be written in matrix form as
[1
I ht
I
II
I
L
Cl
2 ( h t 1 -h )
hz
h,-t
C2
2(.h,-zI h,,-t1
Cn-l
cil
I
,r,l
0
- /[.r2.,rr])
3(,f[x:,.tz.l
3(flx,,rn rl - flx,-t,x,
0
fhc srrqtem is tridironnal
and
(16.2'7)
qolve
372
.l6.3
EXAMPLE
S P L I N EASN D P I E C E W I SI N
ETERPOLATION
NoturolCubic Solines
Problem Stotement. Fit cubic splinesto the samedatausedin Examples16.1and16,2
( T a b l e1 6 . 1 ) U
. t i l i z et h er e s u l t st o e s t i m a t e
t h ev a l u ea t . r : 5 .
Solution. The first stepis to employ Eq. (16.27) to generatetl.resetof simultaneous
equations that will be utilized to determinethe c cocfficients:
hz)
- l t / l r r.., l o / 1 . , . * , 1 ,
| ;, 20,,.t
'llI I:l I -: I 3 t / l r r .r r l - / I . r r . x 3I l r
o' 2tht-rhtt
I
l:l I I
I
The necessary
lunctionand intervalwidth valuesare
1t :2'5
fz : 1.0
h 1: ! . J - 3 . 0 : 1 . 5
hz:7.0-1.5:2.5
ht:9.0-1.0:2.0
f--)\
l+ : O.-s
Thesecan be substituted
to yield
Ir
8
lr-5
2.5
,r e
L
,l{ii}1'l
rt. t
Theseequationscan be solvedusingMATLAB witlr the results:
cr :0
ct : -0.166539924
cz:0.831)543726
c+ : 0
E q u a t i o n (s1 6 . 2 1 )a n d( 1 6 . 1 8 )c a nb e u s e dt o c o m p u t et h e b ' s a n dd ' s
b1:
b2 :
-1.119711863
-1).160456274
h :0.022053232
dt :0.186565212
dz : -0.214144181
dt :0.121156654
These results,along with the valuesfbr the a's lEq. (16.1l)1. can be substituted
into
Eq. (16.l0) to developthe following cubic splinesfor eachinterval:
s r ( , r )- 2 . 5 - 1 . 4 1 9 7 7 1 8 6 3 ( x 3 ) + 0 . 1 8 6 5 6 5 2 7 2 1-r - r . 1 1
s 2 ( x ):
1 . 0- 0 . 1 6 0 4 5 6 2 1 4 (-r 4 . - s )+ 0 . 8 3 9 - 5 + 3 7 2 6-1 r- 1 . - 5 , t r
0.2 Il l-l-l-167t.r -1..5)r
- 7.0)r
sj(,r) : 2.5 +0.022053232(x- 7.0) - 0.166539924(.r
t 0 . 1 2 7 7 . s 6 6 - rs - l ( 7 . 0 ) '
Tlre threeequationscan then be employedfo computevaluesrvithin eachinterval.
For
example,the valueat r : 5, which talls within the secondinterval,is calculated
as
s 2 ( 5 ) : 1 . 0- 0 . t 6 0 4 5 6 2 1 4 ( -54 . - 5 ) + 0 . 8 3 9 5 4 3 1 2 6 (-5 4 . 5 ) 2- 0 . 2 1 4 t 4 4 4 8 7 ( 5- 4 . 5 ) 3
: L102889734.
The total cubic splinefit is depictedin Fig. 16.4c.
373
I 6 . 4 C U B I CS P L I N E S
T h e r e s u l t s oE
f x a m p l e sl 6 . l t h r o u g hl 6 . 3 a r e s u m m a r i z e d i n F i lg6. . 4 . N o t i c e t h e p r o gressiveimprovementof the flt as we move from linear to quaclraticto cubic splines.We
a cubic interpolatingpolynomialou Fig. 16.4c.Although the cubic
have also superirnposed
spline consistsof a seriesof third-ordercurves,the resultingfit differs fronr that obtained
usingthe third-orderpolynomial.This is due to the fact that the naturalsplinerequireszerir
secondderivativesat the end knots. whereasthe cubic polvnomial has no such constraint.
16.4.2 End Conditions
Althoughits graphicalbasisis appealing,the naturalsplineis only one of severalend conditionsthat can be specifiedfor splines.Two of the mostpopularare
.
.
Clunlted End Condition.This option involvesspecifyingthe first derivativesat tlrefirst
and lastnodes.This is sometimescalleda "clamped"splinebecauseit is what occurs
when you clamp the end of a draftingsplineso that it hasa desiredslope.For example,
if zerolirst derivativesarespecilied,the splinewill level off or becomehorizontalat the
ends.
" Not-a-Knot" End Condition.A third alternativeis to fbrce continuity of the third deknots.Sincethe splinealreadyspecifiesthat
rivativeat the secondandthe next-to-last
the tunction value and its first and secondderivativesare equal at theseknots. specifying continuousthird derivalivesmeansthat the samecubic functionswill apply to
Sincethe first internalknotsno longer
eachofthe first and iasttwo adjacentsegments.
representthejunction of two different cubic functions,they are no longer true knots.
Hence.this caseis refenedto as the "not-a-knot" contlition.It hasthe additionalproperty thatfor firur points,it yieldsthe sameresultasis obtainedusingan ordinarycubic
interpolatingpolynomialof the sortdescribedin Chap. 15.
These conditionscan be readily applied by using Eq. tt6.25t fbr the interior knots.
i:2,3,...,n2 , a n d u s i n g f i r s t ( l ) a n d l a s t e q u a t(ino-n1s) a s w r i t t e n i n T a b1l e6 . 2 .
Figure 16.5showsa comparisonof the threeendconditionsasappliedfo tit the datafrom
?rble 16.l. The clampedcaseis setup so that the derivativesat the endsareequalto zero.
As expected,the spline fit for the clarnpedcaselevels off at the ends.In contrast,the
natul'aland not-a-knotcasesfollow the trend of the data points nrore closely.Notice how
the natural spline tendsto straightenout as would be expectedbecausethe secondderivatives go to zero at the ends.Becauseit hasnonzerosecondderivativesat the ends,the nota-knotexhibitsmore curvature.
TABTE16.2
Thefirstond lostequotionsneededto specifysomecommonlyusedend
c o n d i t i o nfso r c u b i cs o l i n e s .
Condition
Firsi ond lost Equotions
Noiuro
r : 1: Q , 6 , , : Q
Cicmped(rvhere
.li cnd 7;lore thespeciledfirst
dt--rlvotlves
vely)
ct thefirstond iostnodes,respeci
2 h 1 t 11 ' f t 1 r ' -2 i 7 J , . . . , t r l - 3 . / i
h r = 1 c , 1 a 2 h , , 1 t , , , : 3 l , ' ,- 3 . l l - , r , , . x1, ,l
lloto knol
h : t t - { l t t * h : l c z * / z r r ' :: O
hn tcn)-0t,:I11,
i<r,tIhu
2c',:Q
AND PIECEWISE
INTERPOLATION
SPLINES
374
f(x)
2
t
1
0
I6.5
FIGURE
nof-o-knot,
ond noturolsplinesfor lhe
Comporisonof the clomped (wilh zero firslderivotives),
doto from Toble 16 l
I6.5
PIECEWISE
INTERPOTATION
IN MATTAB
MATLAB hasseveralbuilt-in functionsto implementpiecewiseinterpolation.The spline
function performscubic splineinterpolationas describedin this chapter.The pchip function implementspiecewisecubic Hermite interpolation.The interpl function canalso
implement spline and Hermite interpolation,but can also perform a numberof othertypes
of piecewise interpolation.
16.5.1 MATTAB Function: spline
Cubic splinescan be easily computedwith the built-in MATLAB function, spli-ne. Ithas
the generalsyntax,
yy
= spl ine (x,
y,
)<x)
(16.28)
where x and y : vectorscontaining the valuesthat are to be interpolated,andyy = a vector
containingthe resultsof the splineinterpolationas evaluatedat the pointsin the vectorxx.
By default,splir.re usesthe not-a-knotcondition.However,if y containstwo more
valuesthan x has entries.then the first and last value in y areusedas the derivativesatthe
end points. Consequently,this option providesthe meansto implement the clamped-end
condition.
E X A M P L E1 6 . 4
S p l i n e si n M A T L A B
Problem Stqtement. Runge'sfunctionis a notorious example of a function that cannot
be fit well with polynomials(recallExample r 5.7) :
f(x):
I
| + 25x2
IN MATTAB
I6.5 PIECEWISE
INTERPOLATION
375
Use MATLAB to fit nine equally spaceddata points sampledfrom this function in the
interval[-1, l].Employ (a) a not-a-knotsplineand (b) a clampedsplinewith end slopes
-4.
o f f i : 1 a n df i - , :
Solution. (a) The nine equally spaceddatapoints can be generatedas in
>> x =
>> ]1 -
linspace(-I,I,9);
(i+25*x."2)
L.,
;
Next, a more finely spacedvectorof valuescan be generaredso tFlatwe can crearca sn?ooai
plot of the resultsas generatedwith the spline function:
xx
=
>> YY
-
>>
1,1);
linspace(
(x,Y,xx)
;
sPLine
Recall that I inspace automaticallycreates100 points if the desirednumberof points are
not specified.Finally, we cau generatevaluesfor Runge'sfunction itself and display them
along with the splinefit and the original data:
>> 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<3,error('at
least 3 input arguments required'),end
if -'(b>a),error('upper
bound must be greater than lower,),end
r f narqin-4 risempLylnt,n-100;end
x = a; h = (f - a)/n;
S-func (q,vdrarg i -,{:' ) ;
fori=1;n-1
X = X + h;
s = s + 2*func(x,vararg:in{:});
end
s - s + func(b,varargin{:});
I = (b - a) * s/(2*n);
F I G U R IE7 . I O
M-f le -o imp enr-cnl'he conpo:iie rope./ooo 'J e.
An applicationof the M-file can be developedto determinethe distancefallenby
the free-falling bungeejurnper in the first 3 s by evaluatingthe integralof Eq. ( 17.3),For
this exarnple,assumethe following parametervalues:g : 9.81 mlsz,m : 68.1kg, and
ca :0.25 kg/m. Note that the exact value of the integralcan be computedwith Eq.(17.4)
a s4 1 . 9 4 8 0 5 .
The function to be integratedcan be developedas an M-file or with an anonymous
function,
>> v=@(t) sqrt (9.81*68.L/0.25) *tanh(sqrt (9.81*A.25i68.1)*t)
C ( t ) s q r i ( 9 . 3 1 * 6 8 . 1 , 't ). 2 5 ) * t a n h ( s q r t ( 9 . B 1* A . 2 5 / 6 8 . 1 ) * t )
First, let's evaluatethe integralwith a crudefive-segmentapproximation:
formaL
long
>;' tt ap (v, 0, 3, 5 )
o, ..6oo2o
:,ar" )-1>5
IZ.4 SIMPSON'S
RUTES
405
As would be expected.this resulthasa relativelyhigh true enor of 18.67o.To obtaina more
accurateresult,we can use a very fine approximationbasedon 10,000segments:
> > t . r a p ( r r ,O , 3 , 1 0 t 1 0 0 )
4!.948049999r1528
which is very closeto the true value.
17.4 SIMPSON'SRULES
Aside from applyin-qthe trapezoidalrule with finer segmentation,anotherway to obtain a
more accurateestimateof an integral is to use higher-orderpolynomials to connectthe
p o i n t s . F o r e x a m p l e ,tihf e r e i s a n e x t r a p o i n t m i d w a y b e t w ef (ean) a n d / ( b ) , t h e t h r e e
points can be connectedwith a parabola(Fig. 17.11a).lf there are two points equally
spacedbetween.f (a) and f (b), the tbur points can be connectedwith a third-orderpolynomial (Fig. l7.l1b). The formulasthat resultfrom takingthe integralsunderthesepolynonrialsare called Sintpson'srules.
17.4.1 Simpson'sl/3 Rule
Simpson'sI /3 rule corespondsto the casewherethe polynomialin Eq. ( 17.8)is secondorder:
(x-xr)(;-rz)
(x - xo)1.r- rz)
,: I:,'l
. / (rtt)
( x 1 y- x y ) ( x s - x 2 )
+
+ (xr -
x e ) ( x 1-
:
l(xr)
(,r - xo) ("t - :rr)
4 rr',rla,
I
(x2 - xe)(.r2 - Y t )
F I G U R EI 7 . I I
( o )G r o p h l c odl e p i c l i oonf S i m p s o n1s/ 3 r u i e l:t c o n s i sol sf t o k i n gt h eo r e ou n d eor p c r o b o l o
c o n n e c i i nt g
h r e ep o i n t s(.b )G r c p h i c od e p i c f i oonf S i m p s o n3' s/ 6 r u l e l: t c o n s i soi sf t o k i n gt h e
^rA^,
n/l^r
1(/.)
' g 'I O J r p O t n ' 5 .
1 - - , O nC O ' n e c l
^-^ . ^ r i
f(x)
406
NUMERTCAL
tNIEGRAT|ONFORMULAS
wherea andb arc designated&s-:16
and 12, respectively.The result of the intesration
is
lt
-
/ :
-r
1 [/(xo) 4.f(x) + J-(.y)l
where,forthiscase,/z
-(!srems
(n
a ) / 2 . T h i s e q u a t i o n i s k n o wa
ns S i n t p s o n , s
I/3rule.T'e
fromthe
r r w rfact
4 v r that
r r 1 4 r //r
t is
l s atvided
u t v l ( j e o by
Dy 3
l n Eq. r7.22).
J in
l2t. Si
Simpson'sl/3 rulecan
:?::,,^t1]_
also
be expressed
usingthe formatof Eq. (17.l3):
.l (:to)+ 4f (r) * .f (xzl
-
I-(b-a
f)
wherea :
xo,b:.r2,
'fld.rr : the point midway betweena
andb,which is
rs glven
si
by
that,accordingto Eq. (17.23),the middlepoiint
is weightedby two,"otice
::,:!11and rhe rwo
thirds
end points by one_sixih.
It can be shown that a single_segment
applicati
ication of Sin npson'sl/3 rulehasa trunca.
tion enor of
I
E ,' :
-
90
h 5 r/ ' f r t '. \l t'
or,becauseh:(b-a)12:
-
E ,' - - '
( b - u-t"s
f t 1 , t b /,
2990 "\
where f lies somewherein the interval from
a to b. Thus, Simpson's1/3 ruleismoreaccuratethan the trapezoidalrule' However,
comparisonwith Eq. (17.] )indicatesthatit is
nore accuratethan expected'Rather than
being proportionalto the third denvative,
the
etror is proportional to the fourth derivative.
consequently,Simpson,sl/3 rule is thirdorder accurateeven though it is basedon only
threepoints. In other words,it yieldsexact
resultsfor cubic porynomialseven thoughit
is derivedfrom a parabora!
E X A M P L E1 7 . 3
S i n g l eA p p l i c o r i o no f S i m p s o n , sl / 3 R u l e
ProblemSiotement. Use Eq. (17.23)
ro inregrate
f ( . x ' ) : 0 . 2 * 2 5 " u - 2 0 0 1 2* 6 i 5 x 3 _ 9 0 0 x a
*400rs
frorn.l
0 b : 0'8' Employ Es. o7.24) to esrimare
rhe enor. Recall that theexacrin!9
regratis 1.640-533.
S o l u t i o n .n : 2 l h :
J'(0) : 0.2
1 :0.8
0.4);
f ( 0 . 4 ): 2 . 4 5 6
/(0.8) : 0.232
0.2+4(2.456)+0.232
= 1.367467
- 1.36746j: 0.2730667
E, : 1.640533
€t:16.67o
407
I 2 . 4 S I M P S O N 'R
SULES
which is approximatelyfive times more accuratethan for a singleapplicationof the trapezoiclalrule (Example17.I ). The approximate
errorcan be estimatedas
oRs
E ", , : - l t - 2 4 0 0 )
1880
: 0.17-10667
where -2400 is the averagefourth derivativelbr the interval.As was the casein Example 17.1,the error is approximate(E,,) becausethe averagefourth derivativeis generally
not an exactestimateof /'+rt{ ). Hower,'er,
becausethis casedealswith a fifth-olderpolynomial, the result matchesexactly.
17.4.2 The CompositeSimpson/sl/3 Rule
Just as with the trapezoidalrule. Simpson'srule can be improvedby dividing the integration interval into a number of segmentsof equal width (Fig. I7 .12).The total integralcan
be representedas
r:
[^,,'f{*)ar+
l,'.f
(x)ttx+. . ' +
o,
1."'
.rU,
( 17.2-s)
FIGURE
17.12
lim^.^n'<
1/3 rule.The relotiverveightsore depictedcbove the funclionvclues
Not-^thoi the methodcon be employedonly if the numberof segmenlsis even.
fnrrnn<lfo
408
NUMERICAL
INTEGRATION
FORMUTAS
SubstitutingSimpson'sI /3 rule fbr eachintegralyields
I (xo)+
I :2lt
+..'+2h
66
(r,,
z) * 4J'Q,, r) + l (r,,)
J
6
or, groupingtermsand usingE q .( 1 7 . 1 - 5 ) :
n
l(,ro)+4I
I
n2
fQi)+2 L
j:2,4.6
i- 1.3.5
J:(b-tt)
fG)+f(r,,)
3n
Notice that, as illustratedin Fig. 17.12,an even numberof segmentsmustbe utilized
to implement the method. In addition, the coefficients"4" and "2" in F,q.(17.26)might
seempeculialat first glance.However.they follow natul'allyfrom Simpson'sl/3 rule.As
illustratedin Fig. 17.12,the odd pointsrepresent
the rniddleterm tbreach applicationand
hencecarry the weight of four from Eq. (11.23).The even points are commonto adjacent
applicationsand henceare countedtwice.
An error estimatefor the compositeSimpson'srule is obtainedin the samefashionas
tor the trapezoidalrule by summing the individual errors for the segnlentsandaveraging
the derivativeto vield
rh -
t ,", : - " ,
'' ,' 1 5
! 'I ' o '
\1j.21)
l80na
where f
EXAMPLE
I2.4
(l)
is the averasetourthderivativetbr the interval.
C o m p o s i t eS i m p s o n ' sI / 3 R u l e
ProblemSiotement. UseEq. (17.26\ with n : 4 to estimatethe integralof
f (.x) : 0.2 * 25r - 200x2-| 675;;3- 900x4* 400x5
from a : 0 to b : 0.8. Employ Eq. (11.27) to estinlatethe error. Recall that theexact
integralis 1.6.10533.
Solution.n :4(h:
.f(o'): o.z
(
I ) . 1 ): 2 . 4 5 6
l (0.8): 0.232
0.2):
f \0.2): 1.288
. f( 0 . 6:)3 . 4 6 4
From Eq. (11.26):
/ -nrl
Et :
0.2+ 4( 1.288
+ 3.464)+ 2(2.456)
+ 0.232
: 1.623467
I2
1 . 6 4 0 5 3 -- l 1 . 6 2 3 4 6 :10 . 0 1 7 0 6 1
€t :
I.04Vo
RULES
IZ.4 SIMPSON'S
The estimatederror 1Eq.17.27';is
(o'8)5
E', , : - . _
t-2100) : ().U17067
l t t 0 ( 4) +
which is exact(aswas alsothe casefor Example17.3).
As in Example 17.4, the compositeversionof Simpson's1/3 rule is consideredsuperior to the trapezoidalrule fbr most applications.However,as mentionedpreviously,it is
Iimitedto caseswherethe valuesareequispaced.
Further,it is limited to situationswhere
thereare an evennumberof segments
and an odd numberof points.Consequently,
as discussedin Section 17.4.3,an odd-segment-even-point
formula known as Simpson's3/8
rule can be usedin conjunctionwith the I /3 rule to permit evaluationo1'bothevenand odd
numbersof equispaced
segments.
17.4.3 Simpson's378 Rule
In a similar mannerto the derivationof the trapezoidaland Simpson'sl/3 rule, a fhirdorder Lagrangepolynomial can be fit to four points and integratedto yield
f :
3h
-t3f(x) +.f(.fr)l
o If(.rr1)*-]l(.rr)
where/r:(b- a)/3.ThisequationisknownasSimpsons3I8rulebecause/rismultiplied
by 3l8.It is the third Newton-Cotes
closedintegrationformula.The 3/8 rule can alsobe
expressed
in thefbrmof Eq.(17.13):
i:\U-UrT
- f( x r , ) * 3 / ( r r ) * 3 . f ( . x )+ . / ( - r : )
(11.28)
Thus, the two interior pointsare given weightsof three-eighths,
whereasthe end pointsare
weightedwith one-eighth.Simpson's3/8 rule hasan emorof
E, : -]/,'f
6t/
't'te
t
or, becauseh : (l) - a) 13:
, , : - ' b . , . i L l ' . / 'r' ' r {
64rJ0
(1.29)
Becausethe denor.nina.tor
of Eq. (11.29)is largerthanfor Eq. (17.24),the 3/8 rule is somewhat more accuratethanthe 1/3 rule.
Simpson'sl/3 rule is usuailytlre rnethodof pref-erence
becauseit attainsthird-order
accuracywith three points ratherthan tlre tour points requiredtbr the 3/8 version.However,the 3/8 rule has utility when the nunrberof segmentsis odd. For instance,in Example 17.4 we usedSimpson'srule to integratethe function for four segments.Supposethat
you desiredan estimatefor five segments.One option would be to usea compositeversion
of the trapezoidalrule as was done in Example 11.2. Thts may not be advisable,however,
becauseof the large truncationerror associatedwith this method.An alternativewould be
to applv Simpson'sl/3 rule to the first two seqments Simoson's3/8 nrle to the last
4ro
NUMERICAL
INTEGRATION
FORMULAS
0
0.2
0.4
#
1/3 rule
0.6
0.8
3/8 rule
FIGURE
17.13
jntondem
lllusirotion
of howSimpsons
1/3cnd 3/Brules
conbeopplied
1ohondle
muriple
o p p l i c o l i o nw
s i t h o d d n u m b e r so f j n t e r v o l s .
three(Fig. ll .13).In thisway,wecouldobtainanestimate
with third-order
accuracy
across
theentireinterval.
EXAMPLE
12.5
S i m p s o n3' s7 8 R u l e
ProblemStqtement.(a) UseSimpson's
3/8 ruleto integrare
TABTEI
-l400x5
./(r) : 0.2r 25x- 20012* 675x'r- 900;r+
froma :0 to b :0.8. (b) Useit in conjunction
with Simpson's
l/3 ruleto integratethe
samefunctiontbr five segnlents.
Soluiron. (c) A singleapplication
of Simpson's
3/8 rule requires
four equally
spaced
points:
/(0):0.2
:3.4811'71
/(0.,5333)
f (0.266j):t.432j24
,f (0.8):0.232
UsingEq.(17.28):
+ 3(t .432724
+ 3.487117)
+ 0.232
l,: t ) .^l J-0.2
:1.51970
Segmentr
(n)
HIGHER-ORDER
NEWTON.COTES
FORMULAS
4tl
(b) Thedataneededfor a five-segment
appI ication(/z: 0.16) is
.f (0) : 0.2
./ (0.16): | .296919
f ( 0 . 3 2 1: 1 . 7 4 3 3 9 3 l ( 0 . 4 8 ): 3 . 1 8 6 0 1 5
f Q.A+1: 3.181929 ,f (l.Stl;:0.232
Theintegralfor thefirst two segnrents
is obtainedusingSimpson's1/3 rule:
I :0.32
0.2+ 4(.1.2969t9)+ t.143393
:0.3803237
0
For the last threesegments,the 3/8 rule can be usedto obtain
1:0.48
1 . 1 4 3 3 9+3 3 ( 3 . 1 8 6 0 1+5 3 . r 8 r 9 2 9 )+ 0 . 2 3 2
:1.264154
g
The total integralis computedby summingthe two results:
: 1.645017
1 : 0.3803237
+ 1.264754
17.5 HIGHER-ORDER
FORMUTAS
NEWTON.COTES
As noted previously,the trapezoidalrule and both of Simpson'srules are menbers of a
family of integratingequationsknown as the Newton-Cotesclosedintegrationformulas.
Some of the formulas are summarizedin Table 11.2. along with their truncation-eror
estimates.
Notice that, as was the casewith Simpson'sI l3 and 3/8 rules,the five- and six-point
formulas have the sameorder error. This generalcharacteristicholds for the higher-point
fornrulasand leadsto the resultthat the even-segment-odd-point
fonnulas(e.g.,l/3 rule
preference.
and Boole's rule) are usually the methodsof
IABLE
17,2 Newton-Cotes
closedintegrotion
formulos.
Theformulosore presented
in theformotof Eq. {l Z.I 3)
the overogeheightis opporent.Thestepsizeis
so thoi theweightingof thedoto pointsto estimote
givenbyh:(b-a)ln.
Poinls
Formulo
Nome
Trcpezoidol
rule
.,
\t)
./(ro)+l(-rr)
n,
)
S i m p s o r '1: 3 ' r - ^
.,
. , f ( r o ) + 4 . f ( - r r )+ f ( - r : )
\p - r,)-
S m p s o n3s/ 8 r u e . ," . . , 8
5
Bccles rr,le
\ 1 ,- u )
\b-0)
Truncolion Error
-ll /I2lhtl"tI)
- l 1, ' 9 O 1 t r1:' r ' t , ,
/ { . \ , , r - ' . / { . r 1 r -3 t t . r ' , - 7 r . t . r
7 . 16 0 ) + 3 2 1 ( x t ) + l 2 l ( x l + 3 2 . (1r t ) + 7 J \ t 1 )
- 1 3 l 8 0 ) 1 5f 1 1 ) ( 6 )
Q 4 5 ) l i r . ; r1( '6r 1
182
90
I 9 l (-rx)
+ Z5.l(.rr) + 501(.r:)+ 501(,{r)+ 75l (xa)+ I 9 l (r5) -127
5 / I 2,a9dnt .1rt':16,
288
412
N U M E R I C AI LN T E G R A T I OFNO R M U T A S
However,it must alsobe stressedthat, in engineeringand sciencepractice,thehigherorder (i.e., greaterthan four-point) lbrrnulasare not commonly used.Sirnpson'srulesare
sufficientfor most applications.Accuracycan be improvedby usingthe compositeversion.
Furthermore,when the function is known and high accuracyis required,methodssuchas
Rombergintegrationor Gaussquadrature,describedin Chap. 18, offer viableandattractive altematives.
17.6 INTEGRATION
WITH UNEQUALSEGMENTS
To this point, all lbrrrrulasfor numericalintegrationlrave been basedon equispaced
data
points. In practice,there are ntany situittionswherethis assurnptiondoesnot holdandwe
r.nustdeal with unequal-sizedseqments.For example,experimentallyderiveddatais often
of this type. For thesecases,one method is to apply the trapezoidalrule to eachsegment
and sum the results:
I -ht
.f(.ril+f@r)
lhz
f (u) + f (r.) * " . r h ,.l, G, t) * .1(x,,)
whereft; : the width of segmenti. Note that this was the sameapproachusedfor thecompositetrapezoidalrule.The only differencebetweenEqs.( 17.16)and( 17.30)is thattheft's
in the formel are constant.
E X A M P L EI 2 . 6
T r o p e z o i d o lR u l ew i t h l ' l 1 s ^ " ^ l s a a m a n l c
Problem Stotemeni. The informationin Table 17.3was generated
usingthesamepolynomialemployedin Example17.1.Use Eq. ( 17.30)to determinethe integralfor thisdata.
Recallthat the correctansweris 1.640533.
TABTE 17.3
Doto for "f(.r) : 0.2 + 25x - 200x2I 67513- 9O0ra+ 400"t',
with uneguollyspocedvoluesof :r.
f (-x)
000
0t2
o22
o32
036
040
0 200000
1 .ic9729
t 345241
) 7,13393
2 074943
2.456040
f {x1
044
o5a
064
o70
080
S o l u t i o n . A p p l y i n gE q . ( 1 7 . 3 0 y) i e l d s
I :0.12
1.309129
0.2+ r.309129
+ 1.305241
+ 0.10
2 . 1 6 1+ 0 2 1 2
+...+Ot0:_::'lj:_:::=t.-s9-1801
which represents
an absoluteperceutrelativeerrorof €t :2.8Vc.
2 842985
3 547297
3 rBr929
2 363000
0 232AAA
413
12.6 INTEGRATION
WITHUNEOUALSEGMENTS
17.6.1
MATLAB M-file: trapuneq
A simple algorithm to implement the trapezoidalrule for unequally spaceddata can be
written as in Fig. 17.14.Two vectors,x andy, holding the independentand dependentvariablesarepassedinto the M-file. Two error trapsareincludedto ensurethat (a) the two vectors are of the samelength and (b) the x's are in ascendingorder.rA loop is employedto
generatethe integral.Notice that we havemodified the subscriptsfrom thoseof Eq. (17.30)
to accountfor the fact that MATLAB doesnot allow z.erosubscriptsin anays.
An applicationof the M-file can be developedfor the sameproblemthat was solvedin
E x a m p l e1 7 . 6 :
>> x - l0 .I2 .22 .32
>> v - 4.2+25*x-200*x.
> > l -r a p L l n e q ( x , y )
:
(o
.Bl;
.36 .4.44.54
.64.1
' 2 + 5 15 * x . ^ 3 - 9 0 0 " x . ^ 4 + 4 0 0 * x . " 5
;
t!
which is identicalto the resultobtainedin Example17.6.
FIGURE
17.14
thetropezoidol
rulelorunequolly
spoced
dofo.
M{ileio lmplement
function
o.
o
i r _ nJ r . u
f
\y.
= trapuneq(x,y)
J,--4)a
L
.-r-.i,.
>lJdLFu
L r a p s T u ', v. (: ,d r_ l
, t rt - L o
dlt:alt_ftte
YJ
T = trapuneq (x, y) :
Z
rule to determine the integral
?
Applies the trapezoidal
(x, y) where x and y must be of t,he
*
for n daLa points
ascending
%
same lengrth and x must be monotonically
% input:
x = ./ector of independent var:rables
%
y = vector
of dependenE variai:les
?
% output:
2
I=inteqralestimate
least 2 input arguments resuired'),end
if nargin<2,error('at
not monotonically
ascendrnq' ),end
if any(diff (x)<0),error('x
n = lengrth(x);
if lengch (y) -=n, error ( 'x and y must be same length' ) ; end
s = 0;
for k = 1;n-1
+y\k+\\) /2;
s : s + (x(k+1)-x(k))-(y(k)
end
T
' The
-
c '
r : l u n c t i o ni s J e s e r i h eiJn S c c t i o r1r 9 . 7I.
414
FORMULAS
NUMERICAL
INTEGRATION
17.6.2
MATTAB Functions: trapz
Qrd
cumcrapz
MATLAB has a built-in function that evaluatesintegralsfor datain the samefashion
asthc
M-file we just presented
in Fig. i7.14. It hasthe generalsyntax
z
-
t rapz
(x,
l')
wherethe two vectors,x &od-r,.hold the independentanddependentvariables,
respectively.
Here is a simple MATLAB sessionthat uses this function to integratethe datafrom
T a b l e1 7 . 3 :
>> x = t0 .I2 .22 .32 .3,h .4 .44 .54 .64 .7 .81;
> > y = 0 . 2 + 2 5 * x - 2 0 0 " x . " ' 2 + 6 15 * x . ^ 3 - 9 0 0 * x . ^ 4 + 4 0 0 * x .^ 5 ;
>> trapz (x,y)
1.5948
In addition, MAILAB has another function, cumr rapz. that computes the cumulativc
inte_ural.
A sirnple representationof its syntax is
c u m t r a p z ( x. ,
z -
y.)
where the two vectors, x and y, hold the independent and dependent variables,respectively,
a n d z : a v e c t o r w h o s e e l e n r e n t sz ( k ) h o l d t h e i n t e g r a lf r o r n x ( 1 ) t o x ( k ) .
EXAMPLE
l7.7
UsingNumericolIntegrotion
to ComputeDistonce
fromVelociiy
ProblemStotement. As described
at the beginningof this chapter,
a niceapplication
of integrationis to computethe distancez(r)of an objectbasedon its velocityu(t)asin
(recallEq. 17.2):
I u \ t ld t
:trl:
J(\
Supposethat we had measurements
of velocityat a seriesof discreteunequallyspaced
tirno
during free fall. Use Eq. (17.2) to syntheticallygeneratesuch inforrnationfor a 70-kg
jumper with a drag coefficient of 0.275 kg/m. Incorporate some randomerrorby rounding
the velocitiesto the nearestinteger.Then usecum1,
rapz to determinethedistance
fallen
and
comparethe resultsto the analyticalsolution(Eq. 17.a).In addition,developa plotofth
analyticalar.rdcornputeddistancesalong with velocity on the samegraph.
Solution. Someunequallyspacedtimesandroundedvelocitiescanbe generated
as
'. lornot
shu'r (J
;';5=[Q
1 L.4 2 3 4.3 5 5.1 B);
> > 9 - 9 . 8 L ; m = 7 0 ; c c t = O. 2 ' 1 5;
> > v = r o u n d ( s q r t ( g * m , / c d )* t a n h ( s q r t ( g * c d / m ) * t ) ) ;
The distances can then be comouted as
/
J^Ltopt(
o
5
9.6
,vJ
1-9.2
4L1
80.7
I44.45
173.85
23r.t
I2.6 INTEGRATION
WITHUNEQUAL
SEGMENTS
the
)ly.
om
4t5
Thus, after 8 seconds,thejumper hasfallen 231.7m. This result is reasonablycloseto the
analyticalsolution(Eq. 11.4):
A graph of the numericaland analyticalsolutionsalong with both the exact and rounded
velocitiescan be generatedwith the following commands:
(t (1) , t (length(r)
) ) ;
za=n/ cd*1og (cosh (sqrt (qr*cd/m) *t,a) ) ;
>> ploL (La, za,t, z,' a' )
>>
ra=linspace
>>
>>
title('Distance
t-1me')
versus
('t
(s) ') ,ylabel
('x
(m) ')
'numerical
Legend ( 'analyticaL'
,
t
>> xlabel
>>
rve
,ly,
As in Fig. 17.15,the numericaland analyticalresultsmatchfairly well.
FIGURE
17.15
Ploiof distonce
timeThelinewoscompufed
versus
withtheonolyticol
rne
solufion,
wnereos
p o i ' r ' sw e e d e l e ' r i r e d ^ l n e c o l , , , r- [ .- h e u r
on
in
Distanceversustime
O
LCS
kg
ng
nd
he
-. , fu^clio..
a nalytical
numerical
416
NUMERICAL
INTEGRATION
FORMULAS
TABTE| 7.4
Newton-Cotes
open integrotion
formu/os.
Theformulosore presenied
in the formotof Eq.{17.l3)
so thottheweightingof the doto pointsto estimote
theoverogeheightis opporent.Thesiepsize
i s g i v e nb y h : ( b - u ) l n .
Segmenls
(n\
Poinls
2
I
3
2
Nome
Formulq
M dpolf t rn-.1hod
( b - a ) . f( . r t )
. . - . . f ( x t )+ l ( - r 2 . 1
\D
a)-
Truncotion Enor
| /3V13
f"G)
t,314)h3
f"G)
2 J ( r t ) - . 1( r : ) I I I ( r r )
(l4rJ5ll5.;iar161
3
I l . / ( . t)r + . 1( f r ) * . f ( , r r )* 1l . l ( - r 1 )
(1)(6)
(95/l44lr5.f
d)
lD
2A
i l . l ( - t r -) l : 1 1 ( f r +) 2 b l ( x t )- 1 4 .(f x r ,*) I 1 . 1 ' 1 . r r 1
(b-u)
l+t,l14A)h1
f\6)Gl
2A
(b
a\
17.7 OPENMETHODS
Recalltiorn Fig. 11.6b that openintegrationformulashavelimits that extendbeyond
the
rangeof the data.Table 17.4summarizesthe Newton-CotesopeninteT4rotion.fbnnulas.The
fbrmulasare expressedin the tbmr of Eq. ( 17.I 3 ) so that the weightingfactorsareevident.
As with the closed versions,successivepairs of the formulas have the same-order
enor.
The even-segment-odd-point
fbrmulas are usuallythe methodsof pref'erence
because
they
require f'ewerpoints to attainthe sameaccuracyas the odd-segment-even-point
fbrmulas.
The openformulasarenot ofien usedfor definiteintegration.However,theyhaveutility for analyzingimproperintegrals.In addition,they will haverelevanceto our discussion
of methodsfor solving ordinary differentialequationsin Chaps.20 and 21.
17.8 MULTIPLE
INTEGRATS
Multiple integralsare widely used in engineeringand science.For example,a general
equatiorlto compute the averageof a two-dimensionalfunction can be written as lrecall
E q .( t 1 . 1 ) l
l . ' '( I i l t r , 1 ' ) t 1 x )
(d-c)(b-a)
(r7.3
r)
The numeratoris called a double integntl.
The techniquesdiscussedin this chapter(and Chap. l8) can be leadily employedto
evaluatemultiple integrals.A simpleexarnplewould be to take the doubleintegralof a
functionover a rectangular
area(Fig. 17.16).
Recallfrom calculusthat suchintegralscan be computedas iteratedintegrals:
a,)at: L' (1,'/(r'
r)or)
L' (1,';(.r,,v)
(17.32)
Thus, the integral in one of the dimensionsis evaluatedfirst. The result of this firstintegration is integratedin the seconddimension.Equation(11.32) statesthat the orderofin-
EXA
I 2.8 MULTIPLE
INTEGRATS
417
FIGURE17.16
Double
infegrol
ostheoreounder
thefunction
surfoce
A numericaldouble integralwould be basedon the sameidea. First, methodssuchas
the cornpositetrapezoidalor Simpson'srule would be applied in the first climensionwith
eachvaltteof the seconddimensionheld constant.Then the methodwould be appliedto integratethe seconddimension.The approachis illustratedin the following example.
EXAMPLE
12.8 U s i n g D o u b l e I n t e g r o lt o D e i e r m i n eA v e r o g eT e m p e r o t u r e
Problem Stotement. Supposethat the temperatureof a rectangularheateclplate is describedby the following function:
I ( t . - r , ): 2 x ) * 2 r - x 2 - 2 t ' 2+ 1 2
If the plate is 8 m long (;r dimension)and 6 m wide (,y dimension),computethe average
temperature.
Solution. First,let us merelyusetwo-segment
applications
of thetrapezoiclal
rulein each
dirnension.The temperatures
at the necessary,rand y valuesare depictedin Fig. 17.17.
Note that a simple averageof thesevaluesis 47.33.The function can also be evaluatedanalyticallyto yield a resultof 58.66667.
To make the same evaluationnumerically,the trapezoidalrule is first implemented
along the ,r climensionfor each-r'value. Thesevaluesare then integratedalong the I' dimensionto give the final result of 2688. Dividing this by the areayields the averagetemperature
a s2 6 8 8 / ( 6 x 8 ) : 5 6 .
Now we can applya single-segment
Simpson'sl/3 rule in thesamefashion.This results
in an integralof 28 l6 andan averageof 58.66667,which is exact.Why doesthisoccur?Recall thatSimpson's| /3 ruleyieldedperfectresultsfor cubicpolynomials.Sincethehighest-
4t8
NUMERICAL
INTEGRATION
FORMULAS
Iftiit
U
40
54
70
72
64
48
( u - u^ ). 0 + 2 { 4 0 ) + 4 8 + 2 5 6
4
24
( 8 - 0 ) 5 4 + 2 | \ 7 0 ]+. 5 4
4-+
496
8_u7?:3gej4+
448
X
I
(6-0) 256+?(4961+448
FIGURE
17.17
Nurnericol
evoluotion
of c doubleintegrol
usingthetwo-segmenl
rule
tropezoidol
For higher-ordelalgebraicfunctionsas well as transcendental
functions,it wouldbe
necessaryto use compositeapplicationsto attain accul'ateintegralestimates.
In addition,
Chap. l8 introducestechniquesthat are more efficient thanthe Newton-Cotes
formulasfr
evaluatingintegralsof given f-unctions.
Theseoften providea superiormeansto implement
the numericalintegrationsfor multiple integrals.
17.8.1
MATTAB Functions: dblquad ond triplequad
MATLAB has functions to implernentboth double (dnlquad) and triple (rriptequad)
integration.A simple representationof the syntaxtbr dbtquad is
q = d b l q u a d ( f u n, x n t i n , x m a : . ,y m i n , y m a x , t o l )
where q is the double integralof the function fun over the rangesfrom xmrn to xmaxad
to ymax.If rol is not specified,a defaulttoleranceof I x 10-6is used.
,vm,zn
Here is an exampleof how this function can be usedto computethe doubleintegral
evaluatedin Example17.7:
>> q - dblquad(@(x,y) 2*x*y+2xx x. "2-2*y.''2+72,A,8,0,6)
2816
CASESTUDY
419
Bockground.
The calculation of work is an important component of many areasof
engineering and science.The general formula is
Work:force
x distance
When you were introducedto this conceptin high schoolphysics,sirnpleapplicationswere
presentedusing forces that remained constant throughout the displacement.For example,
if a force of 10 N was used to pull a block a distanceof 5 m, the work would be calculated
as50J(ljoule:lN.m).
Although such a simple computationis useful for introducing the concept,realistic
problem settingsareusually morecomplex.For example,supposethat the force variesduring the courseofthe calculation.In suchcases,the work equationis reexpressed
as
w:
F(x)dx
1.,,'
( 17.33)
where W: work (J), xo and x, : the initial and final positions (m), respectively,and F(x) :
a force that varies as a function of position (N). ff f(-r) is easyto integrate,Eq. (17.33) can
be evaluatedanalytically. However, in a realistic problem setting,the force might not be expressedin such a manner.In fact, when analyzing measureddata, the force might be available only in tabular form. For such cases,numerical integration is the only viable option for
the evaluation.
Further complexity is introduced if the angle between the force and the direction of
movement also varies as a function of position (Fig. 17.18).The work equationcan be
modified further to account for this effect. as in
w:1""
F(,r) cos[9(x)] dx
(r7.34)
Again, if F(-r) and e @) arc simplefunctions,W 07.34) might be solvedanalytically.However, as in Fig. 17.18,it is more likely that the functionalrelationshipis complicated.For
this sifuation, numerical methodsprovide the only alternativefor determining the integral.
Suppose that you have to perform the computation for the situation depicted in
Fig. 17.18.Although the figure showsthe continuousvaluesfor F(x) and d(x), assumethat,
becauseof experimentalconstraints,you are providedwith only discretemeasurements
at
x : 5-m intervals(Table 17.5).Use single- and multiple-applicationversionsof the trapezoidal rule and Simpson's1/3 and 3/8 rules to computework for this data.
Solution.
The resultsof the analysisare summarizedin Table 17.6.A percentrelative
error r/ was computed in referenceto a true value of the integral of 129.52 that was estimatedon the basisof valuestakenfrom Fig. 17.18at 1-m intervals.
The results are interesting becausethe most accurateoutcome occurs for the simple
two-segment trapezoidal rule. More refined estimates using more segments,as well as
Simpson'srules, yield lessaccurateresults.
The reason for this apparently counterintuitive result is that the coarse spacing of the
points is not adequateto capturethe variations of the forces and angles.This is particularly
420
FORMULAS
NUMERICAL
INTEGRATION
continued
0
,{, m
FIGURE17.18
forceoctingono block.Forlhiscosetheongle,oswellosihe
Thecoseof o vorioble
mognitude,
of theforcevories.
TABLE t 7.5
Doto for forceF("r)ond ongle 0(x) os o functionof
position-r.
F(r), N
0
5
t0
t5
20
25
30
0.0
9.0
r3.0
l4.o
105
12.0
5.0
0, rod
050
1.40
0.75
090
r30
I .48
r.50
F(x) cos d
0.0000
1.5297
9 . 5r 2 0
8.7025
2.8082
1. 0 8 8 1
0.3532
I2.9 CASESTUDY
421
continued
TABIE 17.6
Estimoies
of work colculotedusingthe tropezoidolruleond
Simpson's
rules.Thepercentrelotiveerror€, os computedin
referenceto o truevolueof the integrol1129.52Po)thot wos
estimoted
on the bosisof voluesot l-m intervols.
Technique
Trnnpzni.lnl
Segmenls
r,'lo
Simpson!1/3 rule
Simpson's
3/B rule
I
2
3
6
2
6
3
o/o
Work
e[
5 3t
1 3 3r 9
124.98
r19.09
175.82
I l 7. 1 3
r39.93
95.9
2.84
35 r
8.05
35 75
L57
804
5
5
F|GURE17.19
A continuous
plotof F(x)cos[d(r)]versus
posilion
withtheseven
discrete
pointsusedto develop
ihe numericolintegroiionestimotesin Toble 17.6 Notice how the use of sevenpointsto
chorccterizethisconfinuously
voryingfunctionmissestwo peoksot x :2.5 ond 12.5 m.
evident in Fig. 17.19, where we have plotted the continuous curve for the product of F(.r)
and cos [0(x)]. Notice how the useof sevenpointsto characterizethe continuouslyvarying
function missesthe two peaks at r :2.5 and 12.5 m. The omission of thesetwo points effectively limits the accuracyof the numerical integration estimatesin Table 17.6. The fact
that the two-segment trapezoidal rule yields the most accurateresult is due to the chance
positioning of the points for this particular problem (Fig. 17.20).
The conclusion to be drawn from Fig. 17.20 is that an adequatenumber of measurements must be made to accurately compute integrals. For the present case, if data were
^.'^ir4rrr6 af E"/t {\ onc tA(1 5\l -
?
422
NUMERICAL
INTEGRATION
FORMULAS
(a) anal
(c) coml
applicat
| /3 rule
the num
cent rela
17.4 Ev
continued
1a
I tt
a.
6
(a) analy
(c) coml
applicat
and (f) E
through
17.5 Th
f (x)
,t, ft
FfGURE17.20
Grophicol
depiction
of whvthetwo-seqment
lropezoidol
rulevieldso ooodestimote
ofthe
forthisporiictrlor
integrol
cose.Bychoice,theuseof trrotro[ezoids
h"oppens
to leodtoon
even bolonce between posifiveond negotive errors.
can be r
spacedd
rO
f(x)
determine an improved integral estimate.For example, using the MAILAB trapz
tion, we could compute
>> x=t0
>> y=[0
2.5 5 r0 L2.5 15 20 25 301;
3.9007- L.5291 9.5t"20 11.3940 8.1025 2.8087 ...
1.0881 0.35371;
>> trapz (x, y )
2
Evaluate
lytical rn
of the tri
attain thr
cent rela
17.6 Ev
el
I
J-t J
r32 .6458
Including the two additional points yields an improved integral estimateof I
(e,:2.l6Eo). Thus, the inclusion of the additionaldata incorporatesthe peaksthat
missed previously and, as a consequence,lead to better results.
PROBtEMS
17.1 DeriveEq. ( 17.4)by integratingEq. (17.3).
17.2 Evalnte the lbllowing integral:
r4
I
JO
tt - e-2'1tlx
applicationof Simpson'sl/3 rule, (e) composite
Simpson\
1/3 rule with n : 4, and (f) Simpson's3/8 rule.Foreachof
the numerical estimates(b) through (f), determinetheprcent relativeerror basedon (a).
17.3 Evaluatethe following integral:
rn /2
(a) anal
with n =
1/3 rule
423
PROBLEMS
(a)analytically,
(b) singlc applicationof the trapezoidalrule, 17.7 Evaluatethe triplc integral
(c)conrposite
tlapezoidal rule with n :2 iutd 4, (d) single
n)
n)
rl
application
of Sirnpson'sl/3 rule, (e) compositeSimpson's
1 . r 3- 3 r ' : ) d t d t ' d :
1/3rulewith n : 4. and (f) Simpson's3/8 rule. For eacho1'
thenumericalestimates(b) through (f), determinethe per(a) analytically.and (b) using single applicationsof
cerrt
relltirc erlor hasedon 1a.1.
Simpson'sl/3 rule. For (b), computethe percentrelative
17.,1
Evaluatethe lirllowins inte-eral:
efror.
17.8 Detcrmine the distancetravelcd tionr the follouinc
+ 2xs)ri-r
velocitydata:
I .J,,
J^
(a)analytically,
(b) singlc applicationof thc trapezoidalrule,
(c)compositetrapezoidalrule with n : 2 antl .1,(d) single
l/3 rule, (e) Simpson's3/8 rulc.
application
of Sirr.rpson's
and(f) Boole'srule. For cach of the numericalestimates (b)
(f), determinethc percentrelativeerror basedon (a).
through
17.5The firnction
,f(-r): 2e
r 5'
canbe useclto generatethe following table of unequally
spaced
data:
tl2
u56
325
55
45
7
6
B5
7
B
B
6
B5
7
93
t0
5
(a) Use the tlapezoidalrule.
(b) Fit the data with a cubic equationusine polynomial
regression.Integratethe cLrbicequation1odeterminethe
distance.
I 7.9 Walcr exertspressureon thc upstreamface of a dam as
by
shown in Fig. P17.9.The prcssurecan be characterized
pQ): pg(D- z)
0o
where p(;) - pressurein pascals(or N/ml) exertedat an
0s308 08131
elevationi mctersabovethe rescrvoirbottorn;p : densityol
to be a constant
water, which fbr thi.sproblem is a.ssunred
Evaluate
the inlc-eralfrom rr :0 to b :0.6 using (a) ana_ 103kg/rn3;.9 : acceleration
due to -eravityt9.81m/s2):antl
rule,and (c) a combinltion D : elevation(in m) ofthe watersurfaccabovethe reservoir
lytical
means,(b) the trapezoidal
ofthetrapezoidaland Simpsou'srules whereverpossibleto bottom.Accordingto Eq. (P17.9),pressureincreaseslinearly
aurinthehighestaccuracy.For (b) and (c), computethe per- with dcpth,as depictedin Fig. Pl7.9rz.Ornittingatmospheric
centrelativeerror'.
prcssure(becauseit works againstboth sidesof the dam facc
17.6Evaluatethe double intesral
and essentiallycancelsout). the total force ./, can be dctermined by multiplying pressuretimes the areaof the dam tace
(as shown in Fig. P17.9b).Becauseboth pressurcand area
vary with elevation,thc total force is obtainedby evaluating
(a)analytically,(b) using thc composite trapezoidalrule
r I)
withir : 2. and (c) using singleapplicationsof Simpson's
f, : I psw(z)(D-i,)(1.
JO
l/3 rule.For (b) and (c). compute the percentrelativeeror.
425
O OO5 015
03-5
f ( x ) 2 1 8 5 5 - 51 5 9 7 0 1 3 7 4 6 l l 8 3 l
r
A.{5
OU
40
zu
200
190
175
160
(b)
FIGURE
P17.9
foce of o
Woterexerting
pressure
on lheupslrecm
dom { o J: ; d e r i e w s L o * r n gf o ' t e . ^ r r e o s , n g
424
INTEGRATION
FORMULAS
NUMERICAL
where u(l):
width of the dam face (m) at elevation.:
(Fig. Pl7.9b). The line of action can also be obtainedby
evaluating
IoDpgzu:(z)(D z.)tJz,
p,su(:)(D ;) t1:
ft
71.
Use Simpson'srule to compute J, andcl.
17.10 The tbrce on a sailboatmastcan bc reprcscntcdby the
following function:
/ - \
Jr.-r:200(.,.),:s'a
\/T'l
where: : the elevationabovethe deck and H : thc height
of the mast.The total force F exertedon the mast can be dctermined by integratingthis function over the height of the
mast:
O:J,,I k.) dz
The linc of action can also be deterrninedby integratron:
r
rH
J0 :.It:ta:
-u
J,,'.fr:\ 'l:
(a) Use the compositetrapezoidalrule to compute F rnd d
for the casewhcre 11 : 30 (n : 6).
(b) Repeat(a), but use the compositeSimpson'sl/3 rule.
l7.ll A wind fbrce distributedagainstthe side of a skyscraperis measuredas
Height /, m
Force, F(/), N/m
0
0
Height l, m
Force, F(/), N/m
I50
3r00
30
340
If M,,iszero and.r : I l, calculateM using(a) analyticalintcgration,(b) multiple-applicationtrapezoidalrulc, and(c)
multiplc-applicationSimpson'srules.For (b) and (c) usel-m
lncrements.
17.13 The total mass of a variable density rod is givenbi-
60
l2aa
90
t2a
t600 27aa
IB0 2)A 240
3200 3500 3800
,r:
pG)A,.(r)dx
I
Jt)
p(,r) : dcnsity,A,(,r): cross-sectional
whererz : filoSS,
area,
-r : distancealongthe rod and L : the total lengthoftherod,
The following datahasbeenmeasuredfor a l0-m lengthrod.
Determinethe massin grams to the best possibleaccurac),
x,m
a
2
4
l0
3
6
B
p,glcm34ao 395 389 3Bo 3.60 34t 33l
A,, cm2 loo lo3 l06 I lo )2a I33 lsc
17,14 A transportationengineeringstudy requiresthatyou
determinethe numbcr of cars that passthroughan intersection traveling during morning rush hour. You standat the
sideol the road and count the numberof carsthatpassevery
4 minutesat severaltimes as tabulatedbelow. Use thebest
numericalmethodto determine(a) the total numberof can
that passbetwecn7:30 and 9:15, and (b) the rateofcan
going throughthe interscctionpcr minute. (Hint: Be careful
withunits.)
Time (hr)
73A
Rote (cors
per 4 min) I B
7 45
B:OO B:15
24
l4
24
B : 4 5 9 :i 5
21
9
17.15 Determinetheaveragevaluefor thedatain Fig.PI 7.15.
Perfbrm thc intcgral necdedtbr thc avcragcin theordershown
by the following equation:
r, f r,-
l- I ll
J,.
I
/(\.r),./.rl,1r
L/",
.l
Compute the net fbrce and thc line of action due to this diswind.
tributed
17.16 Integrationprovides a meansto computehow much
17.12 An I l-m beam is subjectedto a load, and the shear massentersor leavesa reactorover a specifiedtimeperiod.
force follows the equation
a si n
V(-r):5+0.2512
where V is the shearforce, and,r is length in distancealong
the beam.We know that V : dM ldx, and M is the bending
moment. Integrationyields the relationship
rI
M:M.,+
I Vttt'
.l,t
ft)
M:
I
et tlr
Jt
where I, and t2 - the initial and final times, respectively,
This formula makesintuitive senseif you rccall thc analogy
betweenintegrationand summation.Thus, the integralreprcscntsthe summationof the product of flow times concenqivo
Use numerica
data listed bel
t, mln
Q, m3/mi;r
c, fng/Jn"
17.17 The c
Putedas
A,: I
./0
where R : t
and,r : dist
averagetlov
o:
1
w h e r eU :
a numerica
data:
)rfi
I;1, ffi
U, mls
-
PROBTEMS
425
-u
-8
-4
-3
-z
1
4
7
I
10
0
12
F I G U R EP I 7 . I 5
Usenumericalinte_uration
to evaluatethis equationfbr the
datalistedbclow:
l, min
Q, m3/min
c, mg/m3
0
4
t0
t0 2a 30
4B 52 50
3_s .-55 52
35 4A 45
4.6 43 43
4A 37 32
50
5.0
34
17,17The cross-sectional
area of a channelcan bc computedas
J0
whercB : the total channelwidth (rn), H : rhedepth(m),
andr' : distanceliom the bank (m ). In a similarfashion,thc
l1ou,'0 (mr/s) can be contputcclas
average
rl]
I U{.r.lrrlr,r.)z/r.
Jo
whereU : watervelocity'(m/s).Use theserelationships
and
a nurnericalmethodto determineA,.and B fbr the fbllorving
data:
J,m
H, ftr
U, m/s
n)/1
05
003
r3
006
(-:
,t'(,+,da,
wherc Z : the total depth (ni). Determinethe averageconcenlrationbasedon thc followins data:
O
4
z,n
B
12
)6
A , l a 6 m 2 9 8 1 7 5 5 t 0 5 t 1 . 9 6 3 5A 3 5 2 7 O O O O O
c,g/m3
)a'2
85
7.4
52
4l
rll
A,= I H(r')r1r.
0 :
l7.lll The averageconcentfationofa substance-(g/rn3)in a
lake u,hercthe arcaA.1m:) varieswith depth l(m) can be
computcdby integration:
56
1.25 l7
005
412
l
0Ir
a.25
oa2
17.19 As wasdonein Section17.9.cletermine
the work perfbrmed ifa constantforce of I N appliedat an anglegresults
in the iollowing displacernents.
Use the curnLrcipzfunction
1odetcrminethe cunrulativcwork andplot the resultversusg.
r,h
0,rad
0
0
I
30
2.7
60
38
90
37
l2O
3
150
1.4
lB0
if*
.,
NumericolIntegrotion
of Functions
CHAPTER
OBJECTIVES
The primarv objectiveof this chapteris to introduceyou to numericalmethodsfor
integratinggiven firnctions.Specificobjectivesand topicscovereclare
'
.
o
I 8.I
Understandinghow Richardsonextrapolationprovidesa meansto createa more
accurateintegralestimateby combiningtwo lessaccurateestimates.
Understandinghow Gaussquadratureprovidessuperiorintegralestimates
by
picking optimal abscissasat which to evaluatethe function.
Knowing how to useMATLAB's built-infunctionsq,-r.:dand r-1uadlto integrate
functions.
INTRODUCTION
In Chap. 17, we noted that functionsto be integratednumericallywill typicallybeoftno
lbrms: a table of valuesor a tunction. The form of the datahas an importantinfluenceon
the approachesthat can be usedto evaluatethe integral.For tabulatedinformation,
youare
limited by the numberof points that are given. In contrast.if the fuuctionis available,
yut
can generateas many valuesof.l (r) as are requiredto attainacceptable
accuracy.
At tacevalue.thecompositeSirnpson'sl/3 rulemight seemto be a reasonable
toolfa
such problems.Although it is certainly adequatefor many problems,therearemoreefficient methodsthat are available.This chapteris devotedto three suchtechniques.
Bolt
capitalize on the ability to generatefunetion values tcl develop efflcient schemes
fr
numericalintegration.
The flrst technique is based on Richardson extr(tpolatiorz,which is a method
fa
combining two nunrericalintegral estimatesto obtain a thild, more accllratevalue.
Tlc
computationalalgorithm lbr implementingRichardsonextrapolationin a highlyefficied
rnanneris called Rombergintegration.This techniquecan be usedto generate
anintegnl
esrimatewifhin a nresnecified
eror
ROMBERG
INTEGRATION
The secondmethod is called Gcuss qundrnfure.Recall that, in Chap. 17, values of
/(x) for the Newton-Cotesformulas were determinedat specifiedvaluesof ,r. For example, if we usedthe trapezoidalrule to determinean integral,we were constrainedto takethe
weightedaverageof /(x) at the endsof the interval.Gauss-quadrature
formulasemploy,r
valuesthat arepositionedbetweenthe integrationlimits in sucha mannerthat a much more
accurateintegralestimateresults.
The third approachis called adaptivequadrature.This techniquesappliescomposite
Simpson's I /3 rule to subintervalsof the integrationrangein a way that allows error estimates to be computed.These error estimatesare then used to determinewhether more
refined estimatesare required for a subinterval.In this way, more refined segmentation
is only usedwhereit is necessary.
Two built-in MATLAB functionsthat useadaptivequadratureare illustrated.
18.2 ROMBERGINTEGRATION
Rombergintegrationis one techniquethat is designedto attainefficientnumericalintegrals
of functions.It is quite similar to the techniquesdiscussedin Chap. 17 in the sensethat it
is basedon successiveapplicationof the trapezoidalrule. However,throughmathematical
manipulations,superiorresultsare attainedfor lesseffort.
18.2.1 RichordsonExtropolotion
Techniquesare availableto improve the resultsof numericalintegrationon the basisof the
integral estimatesthemselves.Generally calledRichardsonextrapolallon,thesemethods
use two estimatesof an integralto computea third, rnore accurateapproximation.
The estimateand the error associatedwith the compositetrapezoidalrule can be representedgenerallyas
r:t(h)+E(h)
where 1 : the exact value of the integral, I (h) : the approximationfrom an n-segment
applicationof the trapezoidalrule with stepsizeh : (b - a) I n, andE (h) : the truncation
error. If we make two separateestimatesusing stepsizesof h1 andh2 andhave exact values for the error:
I (hr)-t E(ht) : I (h) -f E(hz)
(1 8 . 1 )
Now recall that the error of the compositetrapezoidalrule can be representedapproxlm a t e l yb y E q . ( 1 7 . 2 1 )[ w i t h n : ( b - a ) l h ) :
b-u
E-__htf"
12
.
(18.2)
lf rtis assumedthat f" is constantregarcl\ess of stcp sizc,Eq. (18.2) canbc uscdto <lctcrmine that the ratio of the two errors will be
E(ht)
E(hz)
_
- hh! i
(1 8 . 3 )
This calculationhas the importanteffect of removing the term 1" from the computation.
In so doing, we have made it possibleto utilize the information embodiedby Eq. (18.2)
428
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
without prior knowledge of the function's secondderivative.To do this, we reanange
E q . ( 1 8 . 3 )t o g i v e
/ h, \:
E ( h t t= E ( h : t l ;
\n2/
I
which can be substituted
into Eq. (18.1):
/h,\2
I(h1tLEthrtl , | -I(h2tlE(htl
\tt2 /
which can be solvedfor
p
E\ (" /1r/ ' ) :
Itlttl- I\hzl
1-(htlh)2
Thus, we have developedan estimateof the truncationelror in tenns of the integral
estimatesand their step sizes.This estimatecan then be substitutedinto
I : I(hz) _t E]tz)
to yield an improvedestimateof the integral:
I : l(hz) + -
- 1 - t - -- .
\tt1/h2)'
|
ll ttt:l -
ttttrtl
fl8.4)
It can be shown (Ralston and Rabinowitz, 1978) that the error of this estimate
is
we havecombinedtwo trapezoidal
of O(h2) to yielda newesO(h4). Thr-rs,
ruleestimates
timateof O(h\. For the specialcasewherethe intervalis halved(h2: hr12),thisequation becomes
' :
4l
a'(")
-
(185)
lt(nt;
EXA
Extropolotion
E X A M P1
LB
E . I Richordson
ProblemStotement.UseRichardson
to evaluate
extrapolation
theintegral
of /(.r)=
:
:
0.2t 25x 200x2t 675x-r 900r+*400x5 tioma 0 to b 0.8.
Solution. Singleandcomposite
rulecanbeused
toevaluapplications
of thetrapezoidal
atetheintegral:
Segments
h
I
2
4
0B
a4
02
Integrol
o t72B
I 0688
t 4B4B
E
89.5%
349%
5.5%
Richardsonextrapolationcan be usedto combinetheseresultsto obtainimprovedestimates
of the intesral. For example.the estimatesfor one ancltwo sesmentscan be combined
429
INTEGRATION
I 8.2 ROMBERG
to yield
t :lt.oazsr- lro.rzz8):1.36146i
3'
3'
: 0.2130(t1
(st : 76.6c/r:),
is Et : 1.640533 1.361461
improvedintegral
Theerrorofthe
which
it
was
based.
estirnates
upon
whichis superiorto the
for two andfour segments
canbecombinedto give
In thesamemanner,theestimates
41
I : - ( 1 . 4 8 4 8- ); ( 1 . 0 6 8 8 ) : 1 . 6 2 3 4 6 1
-53
an errorof E, :
which represents
1.640533 1.623461:0.011061 (et :
l.ja/c).
Equation(18.4)providesa way to combinetwo applicationsof the trapezoidalrule
with error O (h2) to computea third estimatewith error O (ha1. This approachis a subsetof
a more generalmethodfor combiningintegralsto obtainirnprovedestimates.For instance,
in ExampleI 8. l, we computedtwo improvedintegralsof O (ha) on thebasisof threetrapeThesetwo improvedintegralscan,in turn, be combinedto yield an
zoidalrule estimates.
estimates
evenbettervaluewith Oth6l. For the specialcasewherethe originaltrapezoidal
fbr
is
halving
the
step
size,
the
equation
used
O(li6)
accuracy
of
are basedon successive
I :
r
16
(r 8 . 6 )
-lt
I-5
-1,,,
1.5
where f,, and 11 arc the more and less accurateestimates,respectively.Similarly, two
O(ft6) resultscan be combineclto computean integralthat is O(/r8)using
[ :
,l8.2
:XAMPLE
64
-1,,,
63"'
-
I
-ll
61
'
( 18 . 7 )
Higher-OrderCorrections
ProblemStotement. In Example18.1,we usedRichardson
extrapolation
to computetwo
integralestimatesof Othl). Utilize Eq. (18.6)to combinetheseestimatesto computean
i n t e g r awl i t h O t l r 6 t .
Solution. The two integralestimates
of O(h1) obtainedin Examplel8.l were 1.361461
and 1.623461.Thesevaluescan be substitutedinto Eq. ( I 8.6) to yield
r : ! l . en 4 fl ) - !t.ru ro u r): r .640533
t5'
t5
which is the exactvalueof the intesral.
18.2.2 The Romberg Infegrotion Algorirhm
Notice that the coefficientsin eachof the extrapolation
equations[Eqs.(18.5),(18.6),and
(18.7)l add up to l. Thus. thev reoresentweichtinefactorsthat. as accuracvincreases.
430
INTEGRATION
OF FUNCTIONS
NUMERICAL
placerelatively greaterweight on the superiorintegralestimate.Theseformulationscanbe
expressedin a generalfbrm that is well suitedfor computerimplementation:
I..
-
4r tlj*r.r 1-11.r<,1
4i-l - I
where 17-.1.1-1
and I1.r r : the more and less accurateintegrals,respectively,andllt =
the improved integral.The index ft signifiesthe level of the integration,whereft : I cotrespondsto the original trapezoidalrule estimates,k:2 conespondsto the O(ft4)e$imates,ft : 3 to the O (ho).and so forth. The indexj is usedto distinguishbetweenthemore
U + l) and the less (l) accurateestimates.For example,for ft : 2 and j : 1, Eq.(18.8)
becomes
tr.t:!4
J
which is equivalentto Eq. ( I 8.-5).
The generalform representedby Eq. ( 18.8)is attributedto Romberg,andits systematic applicationto evaluateintegralsis known as Rombergintegratiut. Figure18.1isa
graphical depiction of the sequenceof integral estimatesgeneratedusing this approach.
rule
Each matrix correspondsto a singleiteration.The first column containsthetrapezoidal
($ep
application
evaluationsthat are designated1,.r, where : I is for a single-segment
"r
sizeisb-a),j:2isforatwo-segmentapplication[stepsizeis(b-d)12],j=3isfor
a four-segmentapplication[stepsize is (b - a) l4], andso forth. The othercolumnsofthe
better
matrix are generatedby systematicallyapplying Eq. (18.8) to obtain successively
estimatesof the integral.
For example, the first iteration (Fig. 18.1a) involves computing the one-andtwos e g m e n t t r a p e z o i d a l r u l e e s t i m a t e1sa( 1n1d 1 21 ) . E q u a t i o n ( 1 8 . 8 ) i s t h e n u s e d t o c o m p u t e
the elementIt.z: 1.361461
, which hasan errorof O(h41.
FIGURE
I8.I
G ' o p n i c o d e p i q f i e no f t l . es e q , e ' c e o f i n e g r o Je c i m o l e 5g e n e ' o l e dr s i n gR o r n [ 6 1i9n i e g r o t i o n
(o) Firstiterotion.ib) Second iferotion.(c)Third iterotion.
o(h2)
o(h4)
o@8)
o(h6)
t'r ?:163333-1s67467
0j72800
1.367467---------?
F 1.640533
,r, l:13?333-1623467
0.172800
1.068800
1.367467
1.623467--..*
," 1:333333---163s467
1.640533----*
1.640533
'
1.640533
I 8.2 ROMBERG
INTEGRATION
th
8.8)
t:
:orstiore
i.8)
m;a
)h.
lle
ep
br
le
er
)re
Now, we must check to determinewhetherthis result is adequatefbr our needs.As in
othe-rapproxirnate
rnethclds
in thi.sbook,a lernrinrrtion.
e15lpppin!.criterionis requiredto
assessthe accuracyof the results.One methodthat can be employedfor the presentpurposesis
1,,,r:lu;;-lxroo"/.
(18.e)
wheret,, : an estinrate
of the percentrelativeerror.Thus,as was donepreviouslyin other
iterativeprocesses.
we conrparethe new estimatewith a previousvalue.For Eq. (18.9),the
previousvalue is the most accllrateestimatefiom the previouslevel of integration(i.e., the
fr - I level of integrationwith j :2). When the changebetweenthe old auclnew values
as represented
by r:,,is below a prespecified
error criterion0,. the computationis terminated.For Fig. 18.1a,this evaluationindicatesthe following percentchangeover the
colrrseof the first iteration;
lp.,':l
I r.36i461 - | .068800|
lx1001 :21.8q
t..167-167
I
I
The objcctof the seconditeration(Fig. 18.10)is to obtairrthe O(h6l estirnate-Ir..r.
To do this, a four-segmenttrapezoidalrule estimate,1:.r : 1.4848,is determined.Then it
is combinedwith 11I usingEq. (18.8)to generateI:t : |.623461. The resultis, in turn,
cornbinedwith /1,1to yielcl1r : : 1.640-533.
Equationi18.9) can be appliedto determine
that this result representsa changeof 1.}c/r.when comparedwith the previousresult 12.2.
The third iteration(Fig. l8.lc) continuesthe processin tlresamefashion.In this case,
an eight-segment
trapezoidalestimateis addedto the first column.and then Eq. (18.8)is
appliedto computesuccessively
more accurateintegralsalong the lower diagonal.Afier
only three iterations,becausewe are evaluatinga filih-order polynomial,the resr-rlt
( l t t : 1 . 6 4 0 - 5 3 i3s)e x a c t .
Romber-e
integlationis more efficientthan the trapezoidalrule and Sinrpson'srules.
For example,fbr determination
of the integralas shownin Fig. 18.l, Sirnpson'sl/3 rulc'
would requireabouta 2lS-segment
applicationin doubleprccisionto yield an estimateof
the integralto sevensignificantdi-sits:1.640533.ln contrast,Rombergintegrationprodr-rces
the sameresult baseclon cornbiningone-,two-, fbur-. and eight-segnrent
trapezoidal
rules-that is, with only l5 functionevaluations!
Figure18.2presents
an M-file lbr Rombergintegration.
By usingloops.this algorithm
implementsthe methodin an eficient manner.Note that the function usesanotherfunction
(recallFig. 17.10).Here is
trrep to implementthe compositetl'apezoidal
rule evalutrtions
a MAILAB sessior.r
showinghow it canbe usedto determinetheintegralof thepolynomial
fiom Examole18.l:
>>
>>
f =(i lx)
0.2+25*x
rombcrg(f,0,0.8)
1.5405
2 0i]*t'-+b75*x
432
INTEGRATION
NUMERICAL
OF FUNCTIONS
function
le, ea, i terl'romberg ( func,a,b,es,maxjr,varargin)
quadrature
? romberg: Romberg integration
q = romberg(func,a,b,es,maxit.,pI,p2,...)
%
:
Romhero inteoration.
%
% input:
?
func - name of function
Co be integrated
%
a, b = int.egration
lirnits
(default
= 0.000001?)
%
es = desired relative
error
(default. = 30)
?
maxit - maximum allowable
it.erations
pl,p2,...
= additional
parameters used by func
%
% output:
q-integralestimate
%
(%)
Z
ea = approximate relative
error
= number of iterations
?
iter
if narqin. l,errot ('dL leasu : inpuc argumenrs requireo'),end
rt nargin<4 isempty (es ) , es-0 .000001; end
i I nargin-5tis-mpfy(maxlr. ), maxit=50;end
r
-
1.
= trap(func,a,b,n,varargin{:
I(1,1)
}) ;
= 0;
iter
while
iter<maxit.
= iter+l;
iter
n = 2^iter;
I (iter+1, 1) - trap(func,a,b,n,varargin{
: } ) ;
for k = 2:iter+l
j = 2+iter-k;
I (j , k ) = (4 ^ (k - 1 ) * I (j + 1 , k - 1 ) - r (j , k - 1 ) | / ( 4 ^ (k - 1 ) - 1 ) ;
end
-T(2,iter)
ea = abs( (I(1,iter+l)
) /I(1,iter+l)
) "100;
if ea<-es, break; end
end
q = I(1,iter+l);
FIGUREI8.2
MJileto implement
Romberg
integrotion.
I8.3
GAUSSQUADRATURE
In Chap. 17, we employedthe Newton-Cotesequations.A characteristicof theseformulas
(with the exceptionof the specialcaseof unequallyspaceddata)was that the integralestimate was basedon evenly spacedfunction values.Consequently,the location of thebase
points usedin theseequationswas predeterminedor fixed.
For example,as depictedin Fig. 18.3a,the trapezoidalrule is basedon takingthearea
underthe straightline connectingthe function valuesat the endsofthe integrationinterval.
The formula that is usedto computethis areais
= (h -
+ f (u)
I8.3 GAUSS
AUADRATURE
433
"f("r)
(b)
F I G U R EI 8 . 3
of thelropezoidol
ruleos fheoreounderthestroight
{o)Grophicoldepiction
lineloininofixed
end points{b)An improved
integrol
eslimofe
obtoinedby tokingtheoreounderthestroight
line
possingthroughtwo intermediote
points,By positioning
thesepointswisely,thepositive
ond
negcliveerrorsore bellerbolonced,ond on improved
integrol
estimote
resulls
whereaandb:thelimitsofintegrationandb-a:thewidthoftheintegrationinterval.
Becausethe trapezoidalrule must pass through the end points, there are casessuch as
Fig. 18.3awhere the fbrmula resultsin a large error.
Now, supposethat the constraintof tixed basepointswas removedand we were free to
evaluatethe areaunder a straightline joining any two points on the curve. By positioning
thesepointswisely, we could definea straightline that woLlldbllancc thc positil'c tnd ncgativeerrors.Hence,asin Fig. 18.3b,we would arriveat an improvedestimateof the integral.
Gttussquadratureis the name for a classof techniquesto implementsuch a strategy.
The particular Gauss quadratureformulas describedin this section are called GaussLegendreformulas.Before describingthe approach,we will show how numericalintegration formulas suchas the trapezoidalrule can be derivedusingthe methodof undetermined
coefficients.This methodwill then be employedto developthe Gauss-Legendre
formulas.
18.3.1 Method of UndeterminedCoefficients
In Chap. 17,we derivedthe trapezoidalrule by integratinga linearinterpolatingpolynomial
and by geometricalreasoning.The methodof undetermined
434
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
FIGURE
I8.4
Twointegrols
thctshould
beevoluoted
exocfly
by l h a ' r 6 p " r o , 6 o lr u l ^ .{ o )o
(b)c stroight
line
Lonsio"rro'ro
To illustratethe approach,
Eq. (18.10)is expressed
as
I = c g J @ )+ c t f ( b )
(r8.il)
where the c's : constants.Now realizethat the trapezoidalrule shouldyield exactresults
when the function being integratedis a constantor a straightline. Two simple equations
that representthesecasesare J : I and ) : -r (Fig. 18.4).Thus, the following equalities
shouldhold:
:l
{i;l-t;
r(h-u\ 12
I
ldx
J -(b-u)12
and
b-a
2
o,
*,,+ : I',0,,_,",'j,,
I8.3 GAUSS
QUADRATURE
435
or, evaluatingthe integra.ls,
co*ct:b-a
and
-cu
b-a
b-o
2
2
n
Theseare two equationswith two unklrownsthat can be solvedfor
b-a
L() -
Ll
-
^
z
which, when substituted
back into Eq. (18.11),gives
.
I:
b-a^
- J(It+
zz
b-u^.
^ J(bl
which is equivalentto the trapezoidalrule.
| 8.3.2 Derivqtion of the Two-PointGouss-legendreFormulo
Just as was the casefor the previousderivationof the trapezoidalrule, the object of Gauss
quadratureis to detenninethe coefficientsof an equationof the lbrm
I = cof 6il + clf {.rt)
rrg.rz)
wherethe c's : the unknowncoefficients.However,in contrastto the trapezoidalrule that
usedfixed end points a and b, the function arguments16 and r1 &rerot fixed at the end
points, but are unknowns (Fig. 18.5).Thus, we now have a total of four unknowns that
must be evaluated,andconsequently,
we requirefour conditionsto determinethem exactly.
Just as for the trapezoidalrule, we cirn obtain two of theseconditionsby assuming
that Eq. ( I 8. 12)fits the integralof a constantiurda linearfunctionexactly.Then,to anive at
the other two conditions,we merely extendthis reasoningby assumingthat it also fits the
i n t e g r aol f a p a r l b o l i c( ) : x 2 ) a n da c u b i c( , y: x r ) l - u n c t i o nt s. yd o i n gt h i s .w e d e t e r m i n e
F I G U R EI 8 . 5
Grophicoldepiction
of theunknown
voriobles.rs
ond r1 for integrotion
by Goussquodrolure
436
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
all four unknownsand in the bargainderivea lineartwo-pointintegrationformulathatis
exactfor cubics.The fbur equationsto be solvedare
fl
crtltt:
I
(r8.13)
ldr:2
J -l
r i r . \ r r- f ( t . r t :
fl
|
Jl
(18.14)
.t d,r :0
cut; t.1*i :
rt
2
/ 1 2 r l r : =3
lt
c , r x +i l c l x .f :
/
(r8.15)
pl
(r8.r6)
.rr,/x:o
Jt
E q u a t i o n s( 1 8 . 1 3 )t h r o u g h( 1 8 . 1 6 )c a n b e s o l v e ds i n r u l t a n e o u sfloy r t h e l b u r u n k n o w n s . F i r s t . s o l v e E q . ( 1 8 . 1 a ) f oarncdr s u b s t i t u t e t h e r e s u l t i n t o E q . ( 1 8 . 1 6 ) , w h i c h c a n
be solvedfor
EX
-.r2) _- - 2^ |
Sincex6 and 11 cannotbe equal,this rneansthat.re - -.r1. Substitutingthis resultinto
E q . ( 1 8 . 1 4 )y i e l d sc s : c r . C o n s e q u e n tfl iyo m E q . ( 1 8 . 1 3 )i t t b l l o w st h a t
ca :61
:
I
Substitutingtheseresultsinto Eq. (18.15)gives
, f r r:
I
--
:
-0.5173503...
/a
VJ
.r':]:0.5773503...
V3
fbrmulais
Therefbre,
thetrvo-pointGauss-Legendre
t:r(i)*/f+)
" \,./3/ '\'/3/
(r8.r7)
at
result that the simple addition of the functionvalues
Thus, we arrive at the_interesting
.\ : - 1/ J3 andl/V3 yields an integrirlestimatethat is third-orderaccurate.
Noticethattheintegrationlirnitsin Eqs.( 18.13)through( I8.I6) arefrom - I to l. This
was done to simplify the mathematjcsand to make the fbmrulation as generalaspossible.
A simple changeof variable can be used to translateother limits of integrationintothis
form. This is accomplishedby assumingthat a new variablex,7is relatedto the original
variable-r in a linear fashion.as in
-r:al
(18.r8)
+azxd
to-r,/:
If tlre lower limit,.l : r.r,corresponds
E q . ( 1 8 . 1 8r)o y i e l d
-1, thesevaluescan be substituted
into
a : at + a2el)
Similarly, the upperlimit, r : b, correspondsto x4 : 1, to give
(18.19)
437
QUADRATURE
18.3GAUSS
E q u a t i o n(s1 8 . 1 9 )a n d( I 8 . 2 0 tc a nb e s o l v e ds i m u l t a n e o u sfloyr
ot :
blcr
2
antl
a::
b-a
(18.21)
2
into Eq. (18.18)to yield
which can be substituted
(b + rr) 'l (b - a)r,1
2
rl8.llr
This equationcan be differentiatedto give
b-u
dx - _
dr.l
2'
(r 8 . 2 3 )
in theeqtration
fbr,r andd,r, respectively,
Equarigns(18.22)and ( 18.23)can be substituted
without
integration
interval
the
transform
to be integrated.Thesesubstitutionseff'ectively
is
how
this
done in
illustrates
example
changingthe value of the integral.The fbllowing
practice.
E X A M PIL8E. 3 Two-PointGouss-LegendreFormulo
ProblemStotement. Use Eq. (18.17)to evaluatethe integralof
,f(r) : 0.2 * 25r 200.rr+ 675-rr 900"ra+ 400.15
betweenthe limits r : 0 to 0.8. The exactvalueof the integralis 1.640533.
Solution. Before integratingthe function, we must perform a changeof variableso that
rz : 0 andb : 0.8 into Eqs.(18.22)
the limits arefiom - I to +1. To do this,we substitute
a n d( 1 8 . 2 3t)o y i e l d
x : 0.4* 0.4x7
and
dx :0.4dxd
into the originalequationto yield
Both of thesecan be substituted
r 0.8
|I
Jt)
:
n2
" " - 200:12
+ 6j5x3 - 900x4+ 400x5)dx
' " - +' -25r
I
- 200(0.4
10.4r7)3
+ 675(0.4
* 0.4x.1;2
[0.2+ 25(0.4* 0.4x,1)
J_l
- 900(0.4* 0.4-t.1)a
+ 400(0.4+ 0.4xi5l0.4dra
Therefore,the right-hand side is in the form that is suitablefor evaluationusing Gauss
quailrature.The transtormedfunction can be evaluatedatxT : - | I \4 as 0.5167ttl and at
iu:1/J1 asl.305S3T.Therefore,theintegralaccordingtoEq.(18.17)is0.516741*
a percentrelativeeror of -11.17c.This resultis
1.305837: 1.822578,which r-epresents
applicationof the trapezoidalrule or a single
a
in
to
four-segment
magnitude
comparable
Tlris latterresultis to be expectedbecause
113
3/8
rules.
and
ol'Simpson's
application
accurate.
However, becauseof the clever choice of
rules
are
also
third-order
Sirnpson's
quadrafure
on the basis of only two function
this
accuracy
poinfs,
attains
Gauss
base
evaluarions.
436
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
all fbur unknownsand in the bargainderive a linear two-point integrationlbrmulathatis
exactfor cubics.The fbur equationsto be solvedare
c a* c 1 :
l',rrtx:2
pt
(ir-ruT(l,rl :
|
JI
.rJx:0
pt
. i , t , j' , 1 r i
:
)
/ *l,1t:=
-l
J-,
rl
c r r x i*]c 1 x f :
|
(18.16)
,f ax:O
J "-1
for the fourunEquations(18.13) through (18.16) can be solved sir.nultaneously
knownsF
. i r s t ,s o l v eE q . ( 1 8 . 1 4 )f o r c r a n ds u b s t i t u tteh er e s u l ti n t oE q . ( 1 8 . 1 6 )w, h i c h c a n
be solvedfor
^- to --
EXA
'-1l
Since xe and x1 cannot be equal, this r.neansthat.re - --.r1. Substitutingthis resultinto
from Eq. ( | 8.l3) it follows that
Eq. ( 18.14)yieldsc1;: cr . Consequently
c r t: c t - l
Substitutingtheseresultsinto Eq. ( 18. t 5) gives
. \ ' r:) xt:
I
- 0..5773503...
^. :
v')
I
/::0.-5773503...
v --)
Therefore,the two-point Gauss-Legendre
formula is
/.t\
t : f l " \ . ' / 3^/l + f l
r
l\
(18.17)
-l
\'/3/
result that the simple addition of the functionvalues
at
Thus, we affive at the_interesting
r : - | l./3 and I /V3 yields an integralestimatethat is third-orderaccurate.
Notice that the integrationlimits in Eqs.( 18.I 3) through( 18.16) arefrom - 1 to 1.This
was done to simplify the mathematicsand to make the formulation as generalaspossible.
A simple changeof variablecan be usedto translateother limits of integrationintothis
form. Tlris is accomplished
by assumingthat a new variable.r7 is relatedto theoriginal
variablex in a lineartashion.as ir.r
" x: a t I a z x a
If the lower limit, x : d, corresponds
to xt:
E q . 11 8 . 1 8t)o y i e l d
(18.18)
-7, thesevaluescan be substituted
into
(18.19)
0:a1*a:(-l)
Similarly,the upperlimit, .t : b, corresponds
to x,7:
1. to give
437
QUADRATURE
18.3GAUSS
for
Equations(18.19)and (18.20)can be solvedsimultaneously
at :
b'ta
-;:_
b-a
(tZ: -;
and
(i8.21)
into Eq. ( I 8.I 8) to yield
which can be substituted
(b*a)l(b-tt)x,r
.t
(18.22)
----
:
2
This equationcan be diffbrentiatedto give
dx :
b-n
d.t,t
2'
(18.2.1)
in theequation
for-r-anddx, respectively,
Equations\18.22)and (18.23)canbe sLrbstituted
to be integrated.Thesesubstitutionseffectively transformthe integrationinterval without
changingthe value of the integral.The following exampleillustrateshow this is done in
practice.
E X A M P1L8E. 3 Two-PointGouss-LegendreFormulo
ProblemSlotement. Use Eq. (18.17)to evaluatethe integralof
.f (.x) :0.2 * 25.r - 200x2* 67513 900.14+ 400x5
betweenthe limits x : 0 to 0.8. The exactvalueof the integralis 1.640533.
Solution. Before integratingthe function, we must perform a changeof variableso that
thelimitsaretiom-lto+l.Todothis,wesubstitutea:0andb:0.8intoEqs.(18.22)
and ( I 8.231to yield
r : 0.4 * 0.4,r,i
and
th :O.4elxd
Both of thesecan be substitutedinto the otiginal equationto yield
n 0.u
,I
Jo
rc.2
* 400x5)r/r
' " ' - '*- ' '25.r- 20012+ 6i5r3 - 900,11
nl
:
I
-lLv'!'-"\
n.2+25rc.4i_0.4x,;- 200(0.4t0.4x,)2+675Q.4*0.4-r.7)l
J _l
- 900(0.4* 0.1x,)a + 400(0.4+ 0.4.r41510.4dx,1
Therefore,the right-hand side is in the form that is suitablefor evaluationusing Gauss
quaclrature.
The transformedfunction can be evaluatedat x.1: - | I J1 as 0.5I 674 I and at
l c c o r d i n gt o E q . ( 1 8 . 1 7 .i )s 0 . 5 1 6 7 4 1 +
, { , / : I l " E a s 1 . 3 0 5 8 3 7T. h e r e f b r et,h e i n t e g r a a
a percentrelativeerrorof -lI.l7o. This resultis
1.305837: 1.822518,which represents
compa'ablein magnitudeto a four-segmentapplicationof the trapezoidalrule or a single
applicationof Simpson'sl/3 and 3/8 rules.This latterresultis to be expectedbecause
Simpson'srules are also third-orderaccurate.However, becauseof the clever choice of
base points, Gauss quadratureattains this accuracyon the basis of only two function
evaluations.
438
NUMERICAL
INTEGRATION
OF FUNCTIONS
TABTE| 8.t
Weightingfoctorsond functionorguments
usedin Gouss-Legendre
formulos.
Weighting
Foclors
Points
I
co:2
2
co:
I
l
Function
Argumenls
; 1 6: 0 . 0
-
-ro:
=
-tl :
I lJ3
r11: -nl3/5
;rt : 0.0
c6:{lB-"tr6ltso
c j: ( 1 8 + ' t r d t s a
x o: * , / 5 2 5+ 7 0 \ / 3 0 1 3 5
r r : - v 5 2 5- 7 O \ / 3 0 1 3 5
x2: y'\15 -7sJfr135
X2:
/JO
c.r:{tB-,trotza
q1:1322- t3.na)PO]
c 1: 1 3 2 2 + ) 3 y r y 1p)A j
c2: 128/225
q : 1 3 2 2 + ) 3 ! r y op A O
c a: 1 3 2 2 - l 3 ! r y d / 9 O A
rx:0171324492379174
t ' t : O 3 6 0 7 6 15 7 3 0 4 8 1 3 9
c z: 0 . 4 6 7 9 1 3 9 3 4 5 7 2 6 9 1
c z: 0 . 4 6 7 9 31 9 3 4 5 7 2 6I9
c t : 0 . 3 6 4 7 6 1 5 2 3 0 4I83 l
r - :: 0 1 7 1 3 2 4 4 9 2 3 7 9 1 7 A
1t2){{)
7t1)G)
l/VJ
co : 5/9
(t : B/9
cz -* 5/Q
c::lu+V.,Ul
Truncolion
Error
=./to'({)
\/ J/)
'. : Jili +7aJi6t35
-
ro:-t/245+14\/7Al2l
,y : -li4514Jfi121
.r,:: 0.0
= 1t8){{)
-
/{to)16,
,.:n/1[i-14ffip1
u: '/ltii1tJfrlzt
ro : -a 9324695142031
52
1{tzr16,
-0.66
,rr :
I 249386466265
, r z: - 0 2 3 8 6 1 98l 6 0 8 3I 9 Z
x::O2386l9l86083l97
-ra= 0 661209386466265
xs:0.9324695142A3152
I 8.3.3 Higher-PointFormulos
Beyond the two-point formula describedin the previoussection,higher-pointversions
can
be developedin the generalform
I = c s f ( x 0 )+ c l l ( x r ) + . . . +
cu-rf(xn-t)
(18.24)
wherer?: the numberof points.Valuesfor c's andx's for up to and includingthe six-point
fonnula are summarizedin Table 18.I .
E X A M P LI 8E. 4 T h r e e - P o i nGt o u s s - L e g e n d rFeo r m u l o
Problem Siotement. Use the three-pointformula from Table 18.1to estimatetheintegral
for the samefunctionas in Example18.3.
Solution. According to Table 18.1, the three-pointformula is
1 : 0.5555556f(-0.114s967) + 0.88888891'(0)
+ 0.555s556
f (0.7'74s961)
which is equalto
/ : 0.28t3013 + 0.8132444+ 0.4859876: 1.640533
which is exact.
439
I 8.4 ADAPTIVE
QUADRATURE
BecauseGaussquadraturerequirestunctionevaluationsat nonuniformlyspacedpoints
within the integrationinterval,it is not appropriatefor caseswherethe functionis unknown.
problemsthatdealwith tabulated
data.However.wlrere
Thus,it is not suitedforengineering
This
is particularlytrue
the function is known, its efficiency can be a decidedadvantage.
performed.
when numerousintegralevaluationsmust be
I8.4
ADAPTIVEQUADRATURE
Althoughthe compositeSimpson'sI /3 rule can certainlybe usedto estimatethe integral
thatit usesequallyspacedpoints.This constraint
of givenfunctions,it hasthedisadvantage
doesnot take into accountthat somefunctionshaveregionsof relativelyabruptchanges
where more refined spacingmight be required.Hence,tcl achievea desiredaccuracy,the
fine spacingmust be appliedeverywhereeven though it is only neededfor the regionsof
sharpchange.Adaptive quadraturemethodsremedythis situationby automaticallyadjusting the stepsize so that small stepsare takenin regionsof sharpvariationsand largersteps
are taken where the function changesgradually.
the corrrposite
Simpson'sl/3 rule to
are basedon applyin-u
Most of thesetechniques
subintervalsin a mannersimilal to how the trapezoidalrule was usedin Rombergintegration. That is, the 1/3 rule is appliedat two levelsof reflnementand the differencebetween
thesetwo levelsis usedto estimatethe truncationerror.If the truncationerror is acceptable,
no fi,rrtherrefinementis required and the integral estimatefbr the subintervalis deemed
acceptable.If the error estimateis too large,the stepsizeis r-efinedand the processrepeated
levels.
until the enor falls to acceptable
MMLAB includes two built-in functions to implerrrentadaptivequadrature:cluad
how they can be applied.
and quad1.The following sectiondescribes
18.4.1
MATTAB Functions: quad ond quadl
MATLAB has two functions.botlr basedon algorithrnsdevelopedby Canderand Gautschi
(2000), for irnplementingadaptivequadrature:
.
.
quad. This function usesadaptiveSimpsonquadrature.lt may be more efficient fbr
low accuraciesor nonsmoothfunctions.
quadl. This function useswhat is calledLobotto quadrature.It may be more efficient
for high accuraciesand smoothfunctions.
The following function syntax for the quacl function is the same for the qr-Ladl
function:
g
= qu.ad(fun,
a,
b,
taf ,
t.race,
pl,
p2,
.)
where fun is the functionto be integrated.a and b: the integrationbounds,rol : the
desiredabsoluteerror tolerance(default: l0 6). rra.-e is a variablethat when set to a
nonzerovalue causesadditionalcomputationaldetail to be displayed,and pt, p2,
are parametersthat yor-rwant to passto f rrn.It shouldbe noted that array operators. * . . /
and .^ shouldbe usedin the definitionof fun. ln addition.passemptymatricesfbr rol or
tt ace to use the def-aultvalues.
440
NUMERICAL
INTEGRATION
OF FUNCTIONS
,l8.5
AdoptiveQuodroiure
EXAMPLE
ProblemStotement. Usequadto integrate
thefollowingfunction:
(, - q)2+ 0.01
-s
(x - r)2 + 0.04
belweenthelimitsr:0tol.NotethatforQ:0.3,r:0.9,ands:6,thisisthebuiltin humps function that MATLAB usesto demonstratesome of its numericalcapabilities.
The humps t-unctionexhibits both flat and steepregions over a relatively short.r range.
Hence,it is uselulfor demonstrating
and testin-s
routineslike quad and quadl. Notethat
the t.r,-rmps
functioncan be integratedanalyticallybetweenthe given limits to yield anexact
integralof 29.85832539549861.
Solution. First, let's evaluatethe integralin the simplestway possible,usingthebuilt-in
version of humps along with the defaulttolerance:
>> format lonEt
> > q u a d ( @ h u m p s ,0 , 1 )
29.85832612842164
Thirs. the .solution i.scorrect to .seven-si-clnificantdigits.
Next, we can sojve the sarneproblem, but using a looser tolerance and passingq, r, and
.r as parameters. First, we can develop an M-file for the function:
functi-on y
Y - 1./((x
= m"yhumps(x,q,r,s)
cl).^Z + O.01) + l./
((x
r).^2+0.04)
-
si
Then. we can integrate it with an error tolerance of l0-a as in
, ,1, le-4,
> > 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<t <T12
l ( t ): 0
forTl2<t<T
The force is in new,tonsanclthe angle is in radians.perfbrm
the integrationf}onr -r : 0 to 30 m.
l8.ll Perfbrnrthe samecomputarion
as in Sec.18.5,but fbr
the curlent as specifiedby
i(i1:5r'ls'sin2zr
<t <Tl2
ft'trO
Evaluate
the 1o-5 using (a) Rornbergintegrationto a toleri(t) :0
for Tl2 < t < T
anceof 0. I 7o.(b) the two- and three-pointGauss-Legendre
1= I s.
formulas.
and (c) the MATLAB quac function.
"vhere
18,7The velocity profile of a fluid in ir circular pipe can be 18.12 Computethe power absorbedby an elementin a circuit as describedin Sec. 18.5,but for a simple sinusoidal
represented
as
currenti : sin(2trt/T')where I = I s.
(a) Assumethat Ohm's law holdsand R : -5e.
, = r o ( r- ; ) " "
(b) AssLrrne
that Ohm's law doesnot hold and that voltage
and currentare relatedby the following nonlinear.relawhereu : velocity, r : radial distancemeasuredout front
tionship:I/: (5i - I.25i3).
theprpescenterline,r0 : the pipe's radius,and n : a paraItt.l3 Supposethat the currenrthrough a resistoris demeter.
Determinethe f'low in the pipe if r() = Q.75 andn = J
scribedby the function
(a) Romber_e
usin-e
integlarionto a toleranceof 0. I %. (b) the
Ilo-poinl GaLrss-Legendre
fbunula, ancl (c) the MATLAB
i ( r ) = ( 6 0- / ) 2+ ( 6 0- r i . i n ( " 4 )
qu:r,itunction.Note that flow is eqLralto velocity times area.
l8,ll The alrount of nrasstransportedvia a pipe over a pe- and the resistanceis a function of the current:
riodof tirne can be cornputedas
R : l O i+ 2 i 2 / 3
n,=L,'
Qft)c(t) dt
Compute the averagevoltage over / :
multiple-segment
Simpson's1/3 rule.
0 to 60 using the
446
N T E G R A T I OONF F U N C T I O N S
N U M E R I C AI L
where
center
0.02 40.0
0.05
0 . 1 0 43.0
0 . 1 5 52.0
0 . 2 0 60.0
0.25 55.0
Rupture
where
using
result
18.18
stretcl
tor:
M o d u l u so j
t o u g hn e s s
F,IQ
x, fn
F, l(
rrfi
(al
F I G U R EP I 8 . I 6
( o )A r o du n d eor x i o lo o d i n go n d ( b )t h er e s u l i nsgt r e s s s l r coui nr v ew, h e r e
i sd l m e n s i o n l e s s
i n c hi l 0 r l b / i n ' 7 o) ,n ds t r o i n
s t r e si s i n k i p sp e rs q u o r e
18.1
the vr
U
u
U
The areaunderthe curve from zero stressout to thepoint0f
It
ruptureis calledthe modulusof toughnessof the material.
providesa measureof the energyper unit volumerequired
to
lfr
ol
causethe materialto rupture.As such,it is representative
Vttt:;
I ittllt
LJo
the material'sability to withstand an impact load.Usenufol
If C: l0 s farad,use the fbllowing currentdata to develop mericalintegrationto computethe modulusof toughness
curve seenin Fig. Pl 8. I 6b.
the stress-strain
a plot of voltageversustime:
Itt.l7 If the velocity distributionof a fluid flowing through
pipe is known (Fig. Pl8.l7), the flow rate Q (thatis,the
a
04
06
t'3
0
a2
volume
of water passingthroughthe pipe per unit time)can
03683
038t9
0.2282
i, Io-3 A
o2
be computedby Q : J u tl A, where u is the velocity,andA
ol
is the pipe's cross-sectionalarea.(To graspthe meaning
)
12
t,s
0B
bethis relationshipphysically,recall the close connection
i, Io-3 A
0 0486
0.0082 0 )441
tween summation and integration.) For a circular pipe,
A: r12 anddA:2rr dr. Therefore.
Itl.l5 The work doneon an object is equalto the fbrce times
rl
the distancemoved in the direction of the force. The velocO -- I u(2rr)dr
ity of an object in the dircction of a fbrce is given by
JIJ
llt.l{ If a capacitorinitially holds no charge,the voltage
acrossit as a function of time can be computedas
u:4t
0<t<4
u:16*(4-r)t
4<t<14
I
Al
where r is in m/s. Determinethe work if a constantforce of
200 N is appliedfor all r.
llt.16 A rod subjectto an axial load (Fig. Pl8.i6n) will be
curvein Fi P I 8. I 60.
deformed.as shownin the stress-strain
I
FIGURE
PI8.I7
r
PROBLEMS
447
r is the radial distancemeasuredoutward from the
where
ol thepipe.If the velocilydistributionis given by
center
/
r,=lIl-]l
\
,
/ -.. \
r , - r r l n { "-' ' - ) - s ,
\l'6
\.tno qt /
ltt,/
(in this case,3 cm), conrpute0
where
r,,is the total ladir"rs
the multiple-applicationtrapezoidalrule. Discussthe
using
results.
18,18Usingthe following data,calculatethe work done by
a spling that has a springconstantof ft : 300 N/nr
slretchrng
to;r: 0.35m:
0
0
F ,l 0 3 .N
rrf,
4 t 0 3 .N
rrf,
18.20 The upward velocity of a rocket can be computedby
the fclllt>win-e
tomula:
0 063
020
00r
005
O C)82
025
0 028
o l0
0 046
0 15
0ll
0.30
0 13
035
18.19Evaluatethe vertical dislancetraveledby a rocket if
theverticalvelocityis given by
r':lll---\1
0<l<10
r ,= 1 1 0 0 - 5 r
l0 < r < 20
r'=-50I+2(t-20))
2O<t<30
whererr: upwardvelocity,rr : velocityat which fuel is expelled relativeto the rocket,rno- initial massof the rocket
al time / :0, q : fuel consumption
rate.andg : downward
accelerationof gravity (assumedconslant: 9.8 m/sr).If I :
1800m/s. n,, : 160.000kg. and q : 2500 kg/s,determine
how high the rocket will f1y in 30 s.
18.21 ]'he not'rraldistributionis defineclas
. / t - rt :
t,^
--E:e-\-t2
\/ L]T
(a) Use MATLAB to integratethis function fiom.r : - I to
I and frorn -2 to 2.
(b) Use MATLAB to determinethe inflection points of this
function.
18,22 Use Rornbergintegrationto evaluate
^)/ - €'sln-\
I
-d,
Jo l +.\to an accuracyof e, : 0.57c.Your results should be presentedin the form of Fig. I 8. L
NumericolDifferentiotion
CHAPTEROBJECTIVES
The primary objectiveof this chapteris to introduceyou to numericaldiftbrentiation.
Specific objectivesand topicscoveredare
'
'
'
'
'
'
Understandingthe applicationof hish-accuracynumericaldifferentiation
lbrmulas for equispaceddata.
Knowing hclwto evaluatederivativestbr unequallyspaceddata.
Understandinghow Richardsonextrapolationis appliedfbr numerical
differentiation.
Recognizingthe sensitivityof numericaldifferentiationto dataerror.
Knowing how to evaluatederivativesin MATLAB with the dif f and qlradient
functions.
Knowing how to generatecol.rtourplots and vector fields with MATLAB.
YOU'VE GOT A PROBTE'I/I
velocity of a free-talling bungeejurnper as a function of time canbe
R::*,*::TJ
u ( r)
At the beginningof Chap. 17, we usedcalculusto integratethis equationto determine
the
vertical distancez the jumper has fallen after a time /.
z(t.)
(19.2)
I9.I INTRODUCTION
ANDBACKGROUND
449
Now supposethat you were given the reverseproblem.That is, you were askedto determine velocity basedon the jumper's positionas a function of time. Becauseit is the inverseof intergration.
differentiationcould be usedto make the determination:
dz0)
,
at
r'(l):
119.3)
Eq. (19.2)into Eq. (19.3)anddifferentiating
wouldbring us backto Eq. ( 19.1).
Substituting
Beyond velocity, you might also be askedto computethe jumper's acceleration.To
do this, we could either take the first derivativeof velocity, or the secondderivative of
displacement:
du(t\
.t\i/:-----:-:
dt
d 2 z t tl
dt2
(19.4\
In eithercase.the resultwould be
a(t) :S
,".ftt (
#,)
(1 9 . 5 )
Although a closed-formsolutioncan be developedfor this case,thereare other functions that may be difficult or irnpossibleto differentiateanalytically.Further,supposethat
there was some way to measurethe jumper's position at various times during the fall.
Thesedistancesalong with their associatedtimes could be assembledas a table of discrete
values.In this situation,it would be usefulto differentiatethe discretedatato determinethe
velocity and the acceleration.In both theseinstances,numericaldifTerentiationmethods
are availableto obtain solutions.This chapterwill introducevou to someof thesemethods.
I9.I
INTRODUCTION
AND BACKGROUND
| 9. | . I Whor ls Differentiorion?
Calculusis the mathematicsof change.Becauseengineersand scientistsmust continuously
deal with systemsand processesthat clrange.calculusis an essentialtool of our profession.
Standingat the heartof calculusis the mathematicalconceptof differentiation.
According to the dictionary definition, to dffirentiate means"to mark off by differences;distinguish;. . . to perceivethe differencein or between."Mathematically,the derivettive, which servesasthe fundamentalvehicle for diff-erentiation,representsthe rate of change
of a dependentvariablewith respectto an independentvariable.As depictedin Fig. 19.l, the
mathematicaldefinitionof the derivativebeginswith a differenceapproximation:
f (xi + Ar) - /(.t;)
Ar
A.y
A.r
(1 9 . 6 )
where -y and /(:r) are alternativerepresentatives
for the dependentvariable and x is the
independentvariable.If A:r is allowedto approachzero,asoccursin moving from Fig. 19.1a
to c, the differencebecomesa derivative:
-'
dr
-
lim
{r+0
.f (xi + Ar) - /("r;)
A.r
(t9.7)
450
NUMERICAL
DIFFERENTIATION
.l(.r-,+ Ar;
f(x)
r,
.r, + ar
AI
(b)
(c)
F I G U R EI 9 . I
Thegrophicoldefrnition
zeroin goingfrom(o)to (c),thedifference
opprorimolion
of o derivotive:
os Ax opprooches
becomes
o derivotive.
whereh ldx [which can erlsobe designatedoS.l/ o. .f '(.ri) ]' is the first derivativeof .r'with
r e s p e c t t o - r e v a l u a t e d a t r , . A s s e e n i n t h e v i s u a l d e p i c tFi oi gn.ol 9f . l c , t h e d e r i v a t i v e i s
the slopeof the tangentto the curve at,rr.
The secondderivativereDresents
the derivativeof the first derivative.
#:*(#)
( 19.8)
Thus, the secondderivativetells us how fast the slopeis changing.It is commonlyrefened
to as the cun,ature,becausea high value for the secondderivativemeanshigh curvature.
Finally.partialderivativesareusedfor lunctionsthat dependon morethanonevariable.
Partialderivativescan be thoughtof as taking the derivativeof the functionat a pointwith
all but one variableheld constant.For example,given a function/ that dependson both.r
and y, the partial derivativeof/with respectto.{ at an arbitrarypoint (x, 1,)is detinedas
./(r * A.r,t') - ./(;r.r')
af
3;r
A,r+o
Ax
( 19.9)
Similarly, the partial derivativeof./with respectto y is definedas
a" f- l i m
3y
Ar-o
/ ( ; r , - r ' * A . r ' )- / ( x , l ' )
Ay
(19.r0)
To get an intuitive graspof partial derivatives,recognizethat a function that depends
on
two variablesis a surfaceratherthan a curve.Supposeyou aremountainclimbingandhave
accessto a function.f that yields elevationas a function of longitude(the east-west
oriented
'The fbrm dt'ldx was devisedby Leibnitz, whereasy' is attributedto Lagrange.Note that Newton usedthe
so-calleddot notation: i. Todav.the dot notationis usuallv usedfbr time derivatives.
I9.I INTRODUCTION
ANDBACKGROUND
451
.r axis) and latitude(the north-southoriented-r'axis).If you stopat a particularpoint (,rn,-r'e),
the slopeto the eastwould be 0f(xu,y,) I 0.r,andthe slopeto the noth would be 8/(xo,y6)/3"r.
19.1.2
Differentiotion
in Engineering
ond Science
The differentiationof a function has so rnany engineeringand scientific applicationsthat
you wererequiredto takedifferentialcalculusin your first yearat college.Many specificexamplesof suchapplicationscould be given in all fields of engineeringand science.Differentiationis commonplacein engineeringand sciencebecauseso much of our work involves
characterizingtlrechangesof variablesin both time and space.In fact,many of the laws and
other generalizationsthat figure so prominentlyin our work are basedon the predictable
ways in which changemanifestsitself in the physicalworld. A prime exampleis Newton's
secondlaw, which is not couchedin termsof thepositionof an objectbut ratherin its change
with respectto time.
Aside from suchtemporalexamples,numerouslaws involving the spatialbehaviorof
variablesare expressedin terms of derivatives.Among the most colnmon of thesearc the
cortstitutive/au's that define how potentialsor gradientsinfluencephysicalprocesses.
For
example,Fourier's law oJ heat conductionquantifiesthe observationthat heatflows from
regions of high to low temperature.For the one-dimensionalcase,this can be expressed
mathematically
as
Q:
- K, d T
,'
ax
( 1 9rr. )
where q (.r) : heat flux (Wm2), k : coefficientof thermalconductivitytW(m . K)1, f :
temperature(K), andx : distance(m). Thus,the derivative, or gradient,providesa measure
of theintensityofthespatial
temperaturechange,whichdrivesthetransferofheat(Fig.
19.2).
F I G U R EI 9 . 2
Grophlcoldepiciionof o temperoture
grooient.Becouse
heolmoves"downhill"
fromhighlo low
lemperoture,
ihuflo* in {o)is fromleftiorlght However,
dueto theorientotion
of Corteiion
theslopeis negotive
coordinoies,
for thiscose.Thus,o negolive
grodientleodsto c positive
flow.Thisis iheoriginof lhe"minus
low of XeotcJnduction.
Thereverse
signin Fourier's
coseis
depiciedin {b),wherethepositrve
grodlenlleodslo o negotive
heotflow fromrightto left
Directionof
heatflow
452
DIFFERENTIATION
NUMERICAL
TABTE| 9. I
Low
Fourier's
lor,v
Theone-dimensionol
formsof someconstitutive
lowscommonly
usedin
e n g i n e e r i nogn d s c i e n c e .
Equotion
q : - K *,
d7'
dc
lcw
Fick's
a.r
,dh
D'Arcy'sow
dT
Ohm'slow
I -
Newion's
viscosily
low
| -t^
flooke'sow
o :
_^
d
v
_
d.r
du
,
ax
EL-!
L
PhysicolAreo
!eot conduclion
Grodient
-[:mperoture
Flux
Proportionolily
Heotfiux
Thermoi
Conductivily
Mossdiffusion
Cor,e,rorio1
Mc.. Iu
Drru:iu]
Flowthrough
porousmedicr
Heod
For'vflux
ic
Hydrou
Conductivi\
C u r r e n ft l u x
Elecfricol
ConductivilY
flow
Currenl
Fluids
Velocity
Sheor
Sfress
Dynomic
Viscosily
Eosticily
Deformction
Siress
Young
s
Modulus
Sirnilar laws prcvide workablemodelsin nranyotherareasof engineeringandscience,
elecincluding the modeling of lluid dynurmics,masstransf'er.chernicalreactionkinetics.
isan
tricity, and solid mechanics(Table19.1).The ability to accuratelyestimatederivatives
importantfacet of our capabilityto work effectivelyin theseareas.
Beyond direct engineeringand scientificapplications,numericaldifferentiation
isalso
importantin a variety of generalmathernaticalcontextsincluding otherareasof numerjcal
methods.For example,recall that in Chap. 6 the secantmethod was basedon a finitedifferenceapproximationof the derivative.ln addition,probablythe mostimportant
appliWehave
cation of numericaldifferentiationinvolvesthe solutionof differentialequations.
alreadyseenan examplein the fbrm of Euler's methodin Chap. 1. In Chap.22,wewillinvestigatehow numerical differentiationprovides the basis for solving boundary-value
problemsof ordinary differentialequations.
These arejust a few of the applicationsof differentiation that you might faceregularly
in the pursuitof your profession.When the functionsto be analyzedaresimple,youwillnormally chooseto evaluatethem analytically.However,it is oftendifficult or impossible
when
the function is complicated.In addition,the underlyingfunction is often unknownanddefined only by measurement
at discretepoints.For both thesecases,you rnusthavetheability
next.
to obtain approximatevaluesfor derivatives,usingnumericaltechniquesasdescribed
r9.2 HIGH.ACCURACY
DIFFERENTIATION
FORMUTAS
We have already introduced the notion of numerical differentiation in Chap.4. Recalltha
we employedTaylor seriesexpansionsto derivefinite-differenceapproximations
of derivatives.In Chap.4. we developedforward,backward,andcentereddifferenceapproximations
of tirst and higher derivatives.Remernberthat,at best.theseestimateshaderrorsthatwere
O(h2)-that is, their enors were proportionalto the squareof the stepsize.Thislevelof
EXA
I 9.2 HIGH-ACCURACY
DIFFERENTIATION
FORMULAS
derivationof thesefbrnrulas.We wrll now illustratehow high-accuracyfinite-differenceformulascan be generatedby includingadditionaltermsfrom the Taylor seriesexpansion.
For example,the forward Taylor seriesexpansioncan be written as [recall Eq. (a.13)]
f"!:t)
^z *...
f (ri+t) : f (xi)+ .f'(x)h *
(1e.12)
which can be solvedfor
f'(r): {!tP
-
o(h2)
+r+
(19.13)
In Chap. 4, we truncatedthis result by excludingthe second-and higher-derivativetenns
and were thus left with a fbrward-differenceformula:
f'(,,): tSt#
+ o(h)
(r9.r4)
In contrastto this approach,we now retain the second-derivative
term by substituting
the following forward-differenceapproximationof the secondderivative[recallEq. (4.27)]:
.f,,Gi):W+o&)
(19.1-5)
i n t o E q . ( 1 9 . 1 3 )t o y i e l d
J l x i + z t- 2 f ( r ; - 1 ) + . / t x ; ) .
h+Otlr)
;;;
zn'
( l s .1 6 )
or, by collectingterms:
.f'(.x):-ry+o(h2)
(r9.r7)
term hasimprovedthe accuracyto O\h21.
Notice that incl-rsionof the second-derivative
Similar irnprovedversionscan be developedfor the backward and centeredformulas as
well as for the approximationsof higher-orderderivatives.The formulasare summarizedin
Fig. 19.-lthroughFig. 19.5along with the lower-orderversionsfrom Chap.4. The following exampleillustratesthe utility of theseformulasfor estimatingderivatives.
H i g h - A c c u r o c yD i f f e r e n t i o t i o F
normulos
E X A M P TlE9 . l
Problem Stotement. Recall that in Exarnnle4.4 we estimatedthe derivativeof
t.
I
f ( r ) : - 0 . 1 , r *- 0 . 1 5 x ' - 0 . 5 , r ' 0 . 2 5 x* 1 . 2
at -r : 0.5 using finite-differencesand a step sizeof h: 0.25. The resultsare summarized
in the following table. Note that the errors are based on the true value of
/'(0 5) : -0'9125'
Centered
Forword
o(h)
o(h\
o(h)
-4 714
-a 934
Bockword
Estimote
a/a
Renearfhis comnutation-hut emolov
-t 155
a^
qa/
formulasfrom Fis. 19.3throueh
454
NUMERICAL
DIFFERENTIATION
Enor
F i r s lD e r i v o l i v e
f ( xi + t t f l x , l
-
|
0(h\
\xil:
-f (r,*21+ 4f (xi+) - 3f (x)
2h
oftll
Second Derivotive
-f (,rr*J - 2f (x,*,) * f (.r,)
o(h)
h2
--f (xin:)+ 4.f (xi+) - 5"f (.t,*,)+ 2f (x,)
o(h2)
h2
ThirdDerivotive
(ti*z) * 3/(;,*') - /(.r,)
(x,)= f @i*) 3f
;1"'
0(h)
h3
-31 (1.,+1)
-l 14f (x,n.1- 24f $i+2)+ iB.f(r.+r)- 5,f (r,)
o(h2)
ln'
FourthDerivoiive
f (x,*) - 4f (t,+r)* 6/('r;*r) - 4l'6i+) + f (ri)
f"" (x,)=
o(h)
h1
f "" (r,) =
- 2 . f ( x , * ) + l l / ( x , + + )- 2 4 f ( . x i * ) * 2 6 7 1 . r n 1 * 1 4 . f( x , * , )+ 3 / ( . r , )
0T'?)
Ita
FIGURE
I9.3
Forwordf inite-difference
formulos:hryoversionsore presentedfor eoch derivotive.
Thelotier
version
incorporclesmore termsof the icyior seriesexponsionond rs,consequenlly,
moreoccurole.
Solution. The data neededfor this exampleare
xi-2:0
f(ti-):1.2
xi,r :0.25
.ri : 0.5
f(xi-t): 1.1035156
J'Gi) :0.925
ri+r :0'75
, f ( x i + r ): 0 . 6 3 6 3 2 8 1
li+l
:
I
f ( . r i + ): 0 . 2
Theforwarddifference
of accuracyO (h2)is computed
as(Fig. 19.3)
/ ( 0 . 5 ):
-0.2 + 4(0.636328
r) - 3(0.9t5) - 0 . 8 5 9 3 7 5
:
€ r: 5 . 8 2 V t
2(0j,
Thebackward
difference
of accuracy
O \h2I iscornputecl
as(Fig.19.4)
/ t t . l . -:s )
l ( 0 . 9 2 5-) 4 ( 1 1. 0 3 15 5 6+) 1 . 2
: -0.878125 €,:3.llVt
2(025)
Thecentered
difference
of accuracy
Otha;is computed
as(Fig.19.5)
- 0 . 2 + 8 ( 0 . 6 3 6 3 2 8- 18) ( 1 . 1 0 3 s 1 s+6 1
) .2
19.3 RICHARDSON
EXTRAPOLATION
Firsl Derlvotive
+rr\ J
\
f
i'
Error
frY
J '4i
-
\
t,
o(h)
n
' ( - t , ) 3J(.\i)- 4f (r,-J + J(.\i 1)
:
o(hr)
Lt1
Second Derivolive
.f (r) - 2f (,r, ,) * f (x,'r)
f"(xi)=
o(h)
h2
.f"(r') :
2f (x) - 5/[r'-r) * 4f (x,-r)- -f (.r, ,)
o(h?)
h7
Ihird Derivoiive
3,f (xir,) + 3f (xi,.2) - l (r;-:)
o(h)
hl
f"'( r',)=
5.f(il - 18f(x,_)+ 24f (.t,_r)- 14f (.r,_)-t 3f (.r,_,)
o(.h'1)
2h3
Fourth
Derivotive
f lt,t - 4f rr,_,t+ 6f (r,_r)- 4.f (xi_t)* /(r:,_r)
lr'
3"f(.r;)- I 4 f (x,-) + 26 f @-.) - 24f (ti ) + I I /(.{,,4) - 2f(x,-s)
1""(t) =
o(h)
o(h2)
FIGUREI9.4
formulos.h.rzo
versionscre r.,resenled
for eoch derivotive.Thelotier
Bockwordfinlte-diiference
moreoccurote
versionincorpoTotes
more fermsof ihe Toylorserresexponsionond is, corrsequently,
As expected,the errors for the forward and backwarddifferencesare considerably
more accuratethan the resultsffom Exanrple4.4. However,surprisingly,the centereddifferenceyields the exact derivative at .v : 0.5. This is becausethe tbrmula basedon the
Taylor seriesis equivalentto passinga fourth-orderpolynomialthroughthe datapoints.
I9.3
RICHARDSONEXTRAPOTATION
To this point, we have seenthat there arc two ways to improve derivativeestimateswhen
employing llnite diff'erences:(l) decreasethe step size or (2) use a higher-orderforrnula
that eniploys more poiuts.A third approach,basedon Richardsonextrapolation,usestwo
derivativeestimatesto computea third, more accurate.approximation.
provideda meansto obtainan
Recallfrom Sec. 18.2.i that Richardsonextrapolation
(
improved integralestimateby the formula [Eq. I 8.4)]
I : I ( r r : )* - - - - l
-
\.ttt/tt))- -
l
l r t t t l , t- r t h l \ l
(te.r8)
wlrere/(ft1) andI(h) areintegralestimatesusingtwo stepsizes:/r1 and /22.Becauseof
its conveniencewhen expressedas a computeralgorithm, this formula is usually written
for the casewherehz : ht 12, as in
4
456
DIFFERENTIATION
NUMERICAL
FirslDerivoiive
f ( x , r , )- f
-------;-;-
Error
(.xi-)
o(h'1\
ln
-f (x,*2) + B/(iri+r) - Bf (.r,-,) + l (.t,-1)
o(h')
I ltr
Second Derivotive
f t.t,+t)- 2f $,) + f \x, )
0(h')
h2
f" (x):
-f (xi*) + l6/(.r,*,) * 3O/(ri) + 16/(:r,_,)- f (x, z)
o(hn\
l2tt2
Third Derivotive
-f (r,*r) * 2.f (t+) + 2f (xi r) -./(.r,,)
f "'(x,)=
oft'1)
f"'(x) =
0(hn)
2hl
- . f ( r i + r ) + B / ( x i + 2 )- 1 3 1 1 " r , * , +
. ) l 3 / ( r r - r ) - B . f( x i _ z ) * f ( x ; _ )
8ft3
FourthDerivotive
f (r,*) - 4f {x,n) + 6f (r,) - 4f (xi_)-l .f (xi z)
f"" (x) =
oftI)
h4
f"" (x,1:
-_f (t,o.,)+ 12f (x.+2i\
+ 39/(.rr+r)+ 56f (x,)- 39/(r,-r) + 12.f(x_r) 1-f (xi_z)
0(ho)
6h1
TIGUREI9.5
rvvgve s onso'e oresetred
fo'rnulos:
Cenie'ed['^rle-d'{Ie'ence
for eochderivotive.
Thelofier
^..1 ia a^nao^
'anllrr mnro
versionincorporotes
moretermsof theToylorseries
exponsion
occurote.
In a similarfashion.Eq. (19.19)can be written fbr derivativesas
4l
D : : D ( h : t _ : D \ h1 t )
( I9.20)
-)J
For centereddifferenceapproximutions
with O\h2), the applicationof this formulawill
yield a new derivativeestimateof O(h+).
E X A M P L E1 9 . 2
R i c h o r d s o nE x t r o p o l o t i o n
thefirstdeProblem Stqtement. Using the samefunctionas in Example19.1.estimate
r i v a t i v ea t . r : 0 . - 5e m p l o y i n gs t e ps i z e so f h 1 - 0 . 5 a n dh z : 0 . 2 5 . T h e nu s eE q ,( 1 9 . 2 0 )
to computean improvedestimatewith Richardsonextrapolation.Recallthatthetruevalue
is-0.9125.
can be computed with centereddifferences
as
estimates
Solution. The first-derivative
0 . 2- 1 . 2
D(0.5): -----:- : *1.0
tt : *9.67c
I
and
D(0.25):
0 . 6 3 6 3 2 8- 1r . 1 0 3 5 t 6 -0.934375
:
0.5
€r : -2.47a
I9.4 DERIVATIVES
OF UNEQUALLY
SPACED
DATA
457
The improvedestirnate
can be determinedby applyingEq. ( 19.20)to give
4I
D:4(-0.934315) -
-0.9125
J(-l):
w h i c h l ' o rt h ep r e s e nct a s ei s e x a c t .
The previousexampleyielded an exactresultbecausethe function being analyzedwas
a fourth-orderpolynomial.The exactoutcomewas due to the fact that Richardsonextrapolation is actuallyequivalentto litting a higher-orderpolynomialthroughthe dataand then
evaluatingthe derivativesby centereddivided differences.Thus, the presentcasematched
polynomialprecisely.For most otherfunctions,of course,
the derivativeof the foLrrth-order
this would not occur,and our derivativeestirnatewould be irnprovedbut not exact.Consequently,as was the casefor the applicationof Richardsonextrapolation.the approachcan
be appliediteratively using a Rombergalgorithmuntil the resultfalls below an acceptable
en'or criterion.
19.4
DERIVATIVES
SPACEDDATA
OF UNEGIUATTY
The approachesdiscussedto this point areprimarily designedto determinethe derivativeof
a given function. For the finite-differenceapproximationsof Sec. 19.2,the datahad to be
l 9o.f3S, tehce. d a t a a l s o h a d t o
evenlyspaced.FortheRichardsonextrapolationtechnique
halvedintervals.Suchcontrolofdata spacbe evenly spacedand generatedfor successively
ing is usuallyavailableonly in caseswherewe canusea functionto generatea tableof values.
In contrast,ernpiricallyderived information-that is. data from experimentsor fic-ld
studies-are often collected at unequal intervals.Such intormation cannot be analyzed
with the techniques
discussed
to this point.
One way to l-randlenonequispaced
data is to fit a Lagrangeinterpolatingpolynomial
[recallEq. (15.21)Jto a setofadjacentpointsthat bracketthe locationvalueat which you
want to evaluatethe derivative.Remernberthat this polynomial does not require that the
points be equispaced.The polynomial can thenbe diff'erentiated
analyticallyto yield a formula that canbe usedto estimatethe derivative.
For example.you can fit a second-orderLagrangepolynomialto threeadjacentpoints
( - t o , . r t i )(,. r i . I r ) . a n d ( . r u , . r . ' zD)i.f f e r e n t i a t i ntgh ep o l y n o m i ayl i e l d s :
2.r-.r1 -.t2
l'(x) : l'(-re)(xo -
xr )(xe - x;)
-t .f (rr), 2 x - . r g - . r 2
( x r- r s ) ( x 1 -
2x-rg-x1
* lGz)
( , r :- , r o ) ( - l r-
rr)
12)
(le.21)
where-r is the value at which you want to estimatethe derivative.Although this equationis
certainly more complicatedthan the first-derivativeapproximationfrorn Fig. 19.3through
Fig. 19.5, it has some importantadvantages.
First, it can provide estimatesanywhere
within the rangeprescribedby the threepoints.Second,the points thernselvesdo not have
to be equally spaced.Third. the derivativeestimateis of the sameaccuracyas the centered
points,Eq. (19.21)evaluatedat t : xt redifferencet&q.9.25)1. In fact, for equispaced
ducesto Eo.4.25\.
458
NUMERICAL
DIFFERENTIATION
EXAMPLE
I9.3 DifferentiotingUnequolly Spoced Doto
Problem Stotement. As in Fig. 19.6,a temperaturegraclientcanbe measureddownintothe
soil. The heatflux at the soil-airintert'acecan be computedwith Fourier'slaw (Tablel9.l):
q(::0t:-ft,
drl
I
a_
l.=u
whereq (r) : heatflux 1W/m2).k : coetllcientof thennal conductivityfbr soil [: 0.5W
(m . K)1, I : temperature(K), &rd 1 : distancemeasureddown fron the surfaceintothe
soil (m). Note that a positive valuefor f'lux rneansthat heatis transf-erred
frorntheair tothe
soil. Use numericalditTerentiationto evaluatethe gradientat the soil-air interfaceandenploy this estimateto determinethe heatflux into the ground.
Solution. Equation(19.21)canbeusedtocalculatethederivativeattheair-soilinterfaceas
2(0)- 0.0r2s- 0.037-5
/ ' ( 0 ) : 1 3 . 5(0 -
+ t2
- 0.037-5)
0.0125.)(0
ffi
z(0)-0-0.0375
2(0)-0-0.0t25
- 0)(0.0375
- 0.012s)
(0.037s
: __1140
: - 133.333
K/m
+ 1440- 133.333
+10
whichcanbe usedto compute
w/
K\
w
q ( z: 0 ) : - 0 . 5 ,
: 6 6 . 6 0-1
K ( - 1 3 3 . - l - 1_3J
FIGUREI9.6
Tempercfure
versusdepth info the soii
I9.5
DERIVATIVES
AND INTEGRATS
FORDATAWITH ERRORS
Aside fronr unequalspacing,anotherproblernrelateclto diff'erentiatingernpiricaldatais
that it usuallv includesmeasurementerror.A shortcomingof numericalcliff'erentiation
I 9.6 PARTIAL
DERIVATIVES
4
dr
459
4I
dl
F I G U R EI 9 . 7
(o)ootc with no
lllustrolion
of how smcrLl
dotoerrorscre omplified
by numericol
difiereniioiion.
numericol
differenfiofion
error,(b)theresuhing
of curve{o),(cJdotc modifiedslightly,
ond (d) the
resrlting
differenfiotion
of curve(cJmcnifesting
irrcrecsed
voricbilityln controst,
thereverse
operoliono,fintegrotion
Imovingfrorn(dJto (c)by tokingtheoreounder(d)]tendsto ottenuofe
or srnooTn
oolo errors.
Fig. 19.7ttshows smooth,error-fieedata that wlren numericallydilferentiatedyields
a smoothresult(Fig. 19.7b).Incontrast,Fig. 19.7cusesthe samedata,but with alternating points raised and lowered slightly. This minor modification is barely apparentfiom
Fig. 19.7c.However,the resultinget-fbctin Fig. 19.7tlis significant.
The error arnplificationoccursbecausedifferentiationis sr-rbtractive.
Hence,random
positiveanclnegirtiveerrorstetrdto add.In contrast,the fact thatintegrationib a sumrring
processmakesit very forgiving with regardto uncertaindata.In essence.as points lre
summedto fbrm an integral,randompositive and negativeerrorscancelout.
As might be expected.the primary approachfbr deterrniningderivativestbr imprecise
regression
datais to useleast-squares
to fit a smooth.ditferentiable
functionto the data.In
the absenceof any other information,a lower-orderpolynomialregressionmight be a good
first choice.Obviously,if the truefunctionalrelationship
betweenthe dependent
and independentvariableis known, this relationshipshouldlbrnr the basisfor the least-squares
fit.
r9.6 PARTIATDERIVATIVES
Partialderivativesalouga singledimensionarecomputedin the samefashionas ordinary
derivatives.For example,supposethat we want to determineto partialderivativesfor a
I
460
N U M E R I C ADLI F F E R E N T I A T I O N
EX
approximatedwith centereddiff'erences:
Af
i.).r
-
Af
l(r-A"..u)
2A.r
(.r, ,v* A.y) - ./(x,,t' - A,t')
"f
/(r*Ar,.r)-
2Ar
dl'
(19.22\
(19.23)
All the other fbrmulas and approachesdiscussedto this point can be appliedto evaluate
partial derivativesin a similar fashion.
t0
For higher-orclerderivatives.we mi-ehtwant to differentiarte
a function with respect
two or rnore ditl'erentvariables.The result is called a ntixedpartiaLderivcttive.Forexample, we rnight want to take the partialderivativeof/(-r,,r) with respectto both independent
variables
#:*(#)
( l 9.21)
To developa finite-ditferenceapproximation,we can first form a diff'erencein .r of thepartial derivativesin r':
( t -. [
d-rrd I'
ja( tf
;) \'
I Ar..r') -
af
- A - l. r ' )
-1 (r
i, r'
2Lx
(1 9 . 2 5 )
Then, we can usetlnite differcncesto evaluateeachof the partialsin r':
a2.f
- A . y ) l ( r - A x , . v * A y ) - / ( x - A - r . , r '- A y )
l(x * Ax, I * At') l(:r f Ax, I
_
2Ar
lAr'
(i-r d 1'
2L.r
(19.26)
Collectingtermsyieldsthefinal result
atJ
0x01
/(r * Ax,,r'* A1')- f(r + A,r,.y- Af') - "/("r- A.r,l' * A.r')* /(,t - A . r , r ' - A r ' )
4A;rAy
(19.27)
WITH MATTAB
19.7 NUMERICAT
DIFFERENTIATION
MATLAB softwarehasthe ability to cleterminethe derivativesof databasedon twobuilti n f u n c t i o n sd: i f f a n dg r r a d i e n t .
19.7.1 MATTABFunction:ai.tr
When it is passeda one-dimensionalvectorof lengthn, the di f f function retunlsa vector
in the
of length zr - I containingthe differencesbetweenadjacentelements.As described
approxirnafbllowing example,thesecan then he employedto determinefinite-dift-erence
t i o n so f f i r s td e r i v a t i v e s
.I9,2
E X A M P L1E9 . 4
461
WITH MATLAB
DIFFERENTIATION
NUMERICAL
Usingit i r i for Differentiotion
ProblemStqtemenf. Explorehow the MAILAB dif f functioncan be employedto differentiate the function
- 900x4 + 400r-5
l (x) : 0.2 -t 25x 20012l61513
from x : 0 to 0.8. Compareyour resultswith the exact solution:
f
'(x) :
25 - 400x2+ 202512- 3600"t3* 2000.14
Solution. We can first express./('T) as an anonymousfunction:
> > f = G ( x ) a . 2 + 2 5 * x - 2 0 0 * x . " 2 + 6 15 * x . ^ 3 9 0 0 * x . ^ 4 + 4 0 0 * x . ^ 5 ;
We can then generatea seriesof equally spacedvaluesof the independentand dependent
variables:
>> x=0:0.1 :0.8;
>> Y-f
(r) ;
The dif f function can be usedto determinethe differencesbetweenadjacentelementsof
each vector.For examPle,
>>
diff
(x)
l
Columns
0.1000
througth
5
0.1000
0.1000
Columns 6 through B
0.1000
0.1000
0.1000
0.1000
0.1000
As expected,the result representsthe differencesbetweeneachpair of elementsof x. To
computedivided-differenceapproximationsof the derivative,we merely perform a vector
division of the y differencesby the x differencesby entering
>>
d=dif
f (y) . /drf
through
-0.0100
5
6 through
-11.010A
1.3900
B
Columns
1
f (x)
10.8900
Columns
3.1900
8.4900
8.6900
-21 .31"00
Note that becausewe are using equally spacedvalues,after generatingthe x values,we
could have simply performedthe abovecomputationconciselyas
>>
I
d=diff(f(r))i0.1;
The vector d now containsderivative estimatescorrespondingto the midpoint between adjacentelements.Therefbre,in order to developa plot of our results,we must first
generatea vectorholding the x valuesfor the midpoint of eachinterval:
>> n=Length
>> xm=(x(1:n
(r) ;
1) +x(2:n)
J . /2;
462
N U M E R I C ADLI F F E R E N T I A T I O N
FIGURE
I9.8
Comporisonof the exoct derivotive(line)wiih nunrericolesiimofes(circles)
compufedwith
MATIAB'sdif f function.
As a final step,we can computevaluesfor the analyticalderivativeat a finer levelof resolution to includeon the plot for comparison.
r - ' >X a = O :. C l : . E ;
> - i . ' a = 2 5 4 0 0 * x a + 3 * 5 7 5 * x . a . ^ 24 * 9 0 0 * r , r . ^ 3 + 5 * 4 O O * x a^. 4 ;
A plot of the nurnericaland analyticalestimates
canbe generated
with
>> 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 <
2t0
T'K
FIGURE
PI9.28
the ditlcrencein thc tempcraturesol the body and of thc surrounding medium. If a metal ball heatedto 80 'C is dropped
into water that is held constantat f,, : 20'C, the temperature
of the ball changes,as in
Time, min
r, "c
0
80
5
r0
,145 300
t5
24t
20
2t7
25
2A7
If thc plate's dimensionsare 200 cm long and 50 cm wide,
and I : 0.028J/(s . Irr . K), (a) deterrninethe flux at thesurface.and (b) the heattransf'erin watts.Note that I J = 1W.s.
I 9.3I The pressuregradientfor larninarflow througha constantradiustube is given by
dn
8uQ
d.r
ft14
224
22
r35
tl
090
O68
o6t
a54
250
47
2,\
I49
t2
099
475
4 . 6 75
06
282.5
523
27
I 5.5
I ).4
I03
a78
4.7
462
T, K
C,, kll(kmol
300
4AO
500
600
82 BBB Il2 136 136933 157744
700
800
900
' K) I Z5 036 ) 89 273 20A 9n
1000
210450
19.33 Tllg specificheat at constantpressureq, tJ/(kg. K)l
of an ideal gas is relatedto enthalpyby
dh
19.30 p61 fluid tlorv over a surface.the heat l]ux to the surface can be computedwith Fourier's law: ,y : distancenormal to the surf-acc(m). The tbllowing measurementsare
,,
reacti
-a
where p = pressureiN/rn:), -r : distancealongthetube\
centerline(m), fr : dynamicriscosity{N.s/nrr;,0 = flow
(mr/s)anclr: radius(m).
(a) Detcrminc the pressuredrop for a l0-cm lengthtuh
= p=
for a viscousliquid (p : 0.005N . s/mr,density
I x I03 kg/mr) with a flow of l0 x 10-6 m3/sandthe
fbllowing varying radii along its length:
T, K
Cr,kl/lkmol.K)
19.3
react
d,
Utilize nunrericaldiff'erentiationto determinedTldr ateach
value of tirnc. Plot dT f dt versw T - 7,, andemploy lir"rear
regression
to evaluatefr.
r,cm
O
2
4
5
710
6
19'29 Tl1senthalpyof a real gas is a function of pressureas
l58
r,mm 2
135 1.34 l6
142 2
dcscribcclbelow. The data was takcn lbr a real fluid. Estimatethe enthalpyof the f'luid at zl00K and 50 atm (evaluate
(b) Comparcyour result with the pressuredrop thatwould
thc integral frorn 0. I atm to 50 atm).
have occured if thc tubc had a constantradiuseoualto
7P
.;lt/ ')
the averagcradius.
H : I (\ r ' r (\ 1
' ),rp
dT/pl
(c) Dctelminc the averageReynoldsnuntberfor thetubeto
Ju
verit'y that flow is truly laminar (Re : pu D I 1t < 21N
whercu: velocity).
19.32 the following data for the specificheatof benzene
V,L
was generatedwith an /rth-orderpolynomial.Usenumerical
P, otm
r=350K
r=400K
T=450K
difl'erentiationto dcterminen.
0t
5
t0
20
25
30
4A
45
50
!
dT
whcre ft : enthalpy (kJ/kg), and I : absolutelemperature (K). The fbllowing enthalpiesare providedfor carbon
wher,
reacti
(minevalu
logar
lo
Thert
log(slope
meth,
follor
urea:
t, mi
c, ml
19.31
El(m:
PROBLEMS
471
dioxide(COr) at severaltemperatures.Use these valuesto
determine
the specificheat in J/(kg . K) fbr eachof the tabulatedtemperatures.
Note that the atomic weights of carbon
g/mol, r'espectively
andoxygenare 12.01I and I -5.9994
Lid
I
I
Water
T,K
h,kt/kmol
754
29 629
800
32,179
H
1000
900
37,445 42,769
Sediments
19.34An nth-orderrate law is often usedto model chemical
reactions
that solely dependon the concentrationof a single
reactant:
dc
--
F I G U R EP I 9 . 3 5
: *kc"
dl
wherec : concentration (mole), I : time (rnin), n :
reactionorder (dimensionless), and ft : reaction rate
(min I moler "). The di.fferentialmethod can be used to
evaluatethe parametersk and rt. This involves applying a
logarithmictransformto the rate law to yield,
. / - rlcr
I o- g\ { , } = l o g k
dt /
L,?log(
Therefore,if the nth-order rate law holds, a plot of the
log(-dcldf) versuslog c should yield a straightline with a
slopeof n and an intercept of logfr. Use the dilferential
methodand linear regressionto deternrinet and n for the
following data fbr the conversionof arnmoniumcyanateto
urea:
l, min
c,mole
0
A750
5
o 594
l5
a 420
30
a 29t
dissolvedoxygencontentof a naturalwater.11is measured
by placing a sediment core in a cylindrical conniner
(Fig. P I 9.35).After carefullyintroducinga layer of distilled,
oxygenatedwater abovethe sedinrents,
the containeris coveredto preventgas transfer.A stirel is usedto rnix the water
gently,and an oxygen probetracks how the water's oxygen
concentrationdecreasesover time. The SOD can then be
computedas
SOD: -I1:
do
at
where11: the depthof water (m). o : oxygenconcentration
(g/mr),and t: time (d).
Based on the following data and H : O.l m, use numerical differentiationto generateplots of (a) SOD versus
lime and (b) SOD versusoxygenconcentralioll:
45
0.223 t , d
0 0125 425 0375 05 0625 475
o , m g / L 1 o 7 . l l 4.59 2 57 r r s 0 3 3 0 . 0 3
19.35The sedirnent oxygen demand [SOD in units of
g/1mr. d.11is an important parametel in determining the
&
t
f,
Ordinory Differentiol
l-
r'
Equolrons
6.r ovERvrEw
The fundamental laws of physics. mechanics,electricity, and thermodynamicsare usually'
basedon empirical observationsthat explain variations in physical properties and statesof
i'
systems.Rather than describing the stateof physical systemsdirectly, the laws are usually_ .r" *F
'*
,
couched in terms of spatial and temporal changes. These laws define mechallt**
.oj ., momentum,
differenliol
change.When combined with continuity laws for enorgy,mass,or
integrationof thesedifferentialequationsresultsin matheequationsresult.Subsequent
matical functionsthat describethe spatialand temporalstateof a syslemin termsof energy, -, "
'"
mass,or velocity variations.As in Fig. FT6,1, the integrationcan be implementedanalyti*
l,,
cally with calculus or numericalli with the computer
jumper
introproblem
The free-fallingbungee
ducedin Chap. I is an exampleof the derivationof a
ditferential equationfrom a fundamentallaw. Recall
that Newton's second law was used to develop an
ODE describingthe rate of changeof velocity of a'"
falling bungeejumper:
c,t t
du
i:r--:-u-
(PT6'l)
where g is the gravitationa.lconstant.rr is the mass,
and c, is a drag coefficient.Suchequations,which are
composedof an unknown functionand its derivatives.
are called dffirential equntions,,:TheY
ate sometimes
referred to as rate equationsbecausethey expressthe
rate of change of a variable as'a"'functionof variables
and parameters.
u
In Eq. 1PT6.l), the quantitybeingdifferentiated
is called the dependent variable. The quantity with
respectto which u is differentiated t is calledthe independent variable. When the function involves one independentvariable, the equation is called an ordinary
'
,.::.:..1.r..
, r,i:,ir.,.,,
t.,..
473
474
PART
6 ORDINARY
DIFFERENTIAL
EQUATIONS
Physicallaw
F=ma
II
I
I
f
du -^
-6
ODE
I
cJ..2
,
aIm
n
Analytical
(calculus)
I
/
V
Solution
l o m
\
/
l o ^ ,
\
u - . / " " ' t a n h[ , / r y r ]
\
t,j
Numerical
(computer)
\V
/n
/
u r + r= u i + ( s - ' ! r ' )
n,
FIGUREPT6.I
i I e s e o u e n r eo t e v e n t si n r h ed e u e l o p r r e no' ^ d s o l - l l o ^o l O D E sf o e n g - e e r i n go n d s c i e n c e
Ihe exompleshown is for the velocityof the freefcliingbungee iumper.
dffirential equation (or ODE). This is in contrastto a partial dffirentiul equation (or
PDE) that involves two or more independentvariables.
Differentialequationsare also classifiedas to their orderFor example.Eq. (PT6.l) is
called afirst-order equatio,?becausethe highestderivativeis a lirst derivative.Asecondorder equationwould includea secondderivative.For example,the equationdescribingthe
positionx of an unforcedmass-springsystemwith damping is the second-orderequation:
,)
,
d'x
m -*t.
dt'
dx
dt
*A-x:0
(PT6.2)
where ,fl is mass,c is a damping coefficient,and k is a spring constant.Similarly, an nthorder equationwould include an nth derivative.
Higher-orderdifferentialequationscan be reducedto a systemof first-orderequations.
This is accomplishedby defining the first derivativeof the dependentvariable as a new
variable.For Eq. (PT6.2),this is doneby creatinga new variableu as the first derivativeof
disolacement
dx
dt
(PT6.3)
where u is velocity.This equationcan itself be differentiatedto yield
(l t'
o'-l
dt
dtz
(PT6.4)
Equations(PT6.3)and (PT6.4)can be substitutedinto Eq. (PT6.2)to convertit into a firstorder equation:
du
m7-tcu+kr:0
(PT6.s)
6.I OVERVIEW
475
As a final step,we can expressEqs.(PT6.3)and (PT6.5)as rateequations:
dx
(Pr6.6)
l-
ut
tlu
'
dT
ck
tn
nt
(PT6.t)
Thus, Eqs. (PT6.6) and (PT6.7) are a pair of first-order equationsthat arreequivalent
to the original second-orderequation (Eq. PT6.2). Becauseother nth-order ciiff'erential
equationscan be similarlyreduced,this part of our book fbcuseson the solutionof firstorderequations.
A solutionof an ordinarydifl'erentialequationis a specificfunctionof the independent
variable and parametersthat satisfiesthe original differential equation.To illustrate this
concept,let us startwith a simple fourth-orderpolynomial,
. t ,: - 0 . 5 1 4 + 4 . r 3 - l 0 x 2 + 8 . 5 . r* I
(Pr6.8)
Now. if we differentiateEq. (PT6.8).we obtainan ODE:
d.r - _2r3
* l2,rr - 20r * 8.5
dx
(Pr6.e)
This equationalso describesthe behaviorof the polynomial,but in a mannerdifferentfrom
Eq. (PT6.8). Rather than explicitly representingthe values of ,r for each vahre of .r,
Eq. (PT6.9)givesthe rateof changeof -ywith respectto.r (thatis, the slope)at everyvallle
of .r. FigurePT6.2showsboth the functionand the deriv'atileplottedverslls.{.Noticehow
the zero valuesof the derivativescorrespondto the point at which the original function is
flat-that is, where it hiis a zero slope.Also, the maximum absolutevaluesof the derivativesale at the endsofthe intervalwherethe slopesofthe functionaregreatest.
we can determinea differentialequationgiven the
Altlrou-eh,as just denronstrated,
originalfunction,the objecthereis tt.rdeterminethe originalfunctiongiventhedifferential
eqr.ration.
The original function then representsthe solution.
Withoutcomputers,ODEs areusuallysolvedanalyticallywith calculus.For example,
Eq. (PT6.9)could be multiplied by dx andintegratedto yield
t:
lr-ztt
1 l2.r'r- 2ox* 8.5)r/.r-
(PT6.I 0)
The righrhand sideof this equationis calledan indefiniteintegraLbecausethe limits of integrationare unspecified.This is in contrastto the definite integralsdiscussedpreviously
in PartFive [compareEq. (PT6.l0) with Eq. (17.5)].
An analyticalsolutionfor Eq. (PT6.l0) is obtainedif the irrdefiniteintegralcan be evaluatedexactlyin equationfblnr. For this sinrplecase,it is possibleto do this witlr the result:
r ' : - 0 . 5 1 4 * 4 . t 1 - 1 0 . 1+2 8 . 5 . r* C
(PT6.
r r)
which is identicalto the original function with one notableexception.In the courseof differentiatingand then integrating,we lost the constantvalue of I in the original equation
and gainedthe value C. This C is called a u)nstantof integrorion The f'actthat suchan arbitraryconstantappearsindicatesthat the solutionis not uniclue.In fact,it is but one ofan
476
PART
6 ORDINARYDIFFERENTIAL
EAUATIONS
FIGUREPT6.2
P l o t so f ( o )\ ' v e r s u sr o n d l b )d - r . / r hv e r s u s r f o r t h e f u n c t i o n l , :- O 5 r a * 4 x 3- l O ; + B 5 j r + I
infinite numberof possiblefunctions(con'esponding
to an infinitenumberof possiblevalues of C) that satisfythe differentialequation.For example,Fig. PT6.3 showssix possible
functionsthat satisfyEq. (PT6.l I ).
Therefore,to specifythe solutioncompletely,a ditTerentialequationis usuallyaccompanied by auxiliary conditions.For first-orderODEs, a type of auxiliary condition called
ln initial value is requiredto determirrethe constantand obtain a uniquesolution.For
example,the original diff'erentialequationcould be accompaniedby the initial condition
thatat r:
0 . . 1 ' : l . T h e s ev a l u e sc o u l d b e s u b s t i t u t eidn t o E q . ( P T 6 l. l ) t o d e t e r m i n e
C : l. Therefore,the unique solution that satisfiesboth the differentialequationand the
s p e c i l ' i eidn i t i a lc o n d i t i o ni s
-r': -0.5x1 f 4rr - l0rr -r-8.5r * I
Thus,we have"pinneddown" Eq. (PT6.l1) by forcingit to passthroughthe initial condition, and in so doing, we havedevelopeda uniquesolutionto the ODE and havecornefull
circle to the originalfunction[Eq. (PT6.8)].
Initial conditionsusually have very tangibleinterpretationsfor diff'erentialequations
derived from physical problem settings.For example.in the bungeejumper problem, the
ORGANIZATION
6.2 PART
477
C=3
( =Z
C=1
C=0
FIGUREPT6.3
of -2-r'rI l2x2- 2Or* 8.5.Eochconforms
to o differenl
Sixpossibe
solutions
fortheiniegrol
vo ue of lhe constontof inteqrolionC.
initial conditionwas reflectiveof the physicallact that at time zero the verticalvelocity
was zero.If the bungeejumperhad alreadybeenin verticalnlotiorlat time zero,the solution would havebeenmodified to accoLllltfor this initial velocity.
When dealingwith an nth-orderdifferentialequation,n conditionsare requiredto obtain a unique solution.If all conditionsare specifiedat the samevalue of the independent
variable (fbr example,at,v or t : 0), then the problem is called an initial-valueproblem.
This is in contlastto boundary-valueproblemswhere specificationof conditionsoccursat
difTerentvaluesof the independentvariable.Chapters20 and 2l will fbcus on initial-value
problems.Boundary-value
problemsarecoveledtn Chap.22.
6.2
PARTORGANIZATION
Chapter 20 is devotedto one-stepmethodsfor solving initial-value ODEs. As the name
suggests,one-stepmethodscomputea futurepredictiony;a1, basedonly on information at
a single point r; and no other previous information.This is in contrastto multistepapproachesthat useinlbrmation from severalpreviouspointsas the basisfor extrapolatingto
a newvalue.
With all but a rninorexception,the one-stepmethodspresented
in Chap.20 belongto
wh:rt are called,Runge-Kuttatechni4lle.s.
Although the chapterrnight havebeenorganized
aroundthis theoreticalnotion, we haveoptedfor a more graphical.intuitive approachto introduce the rnethods.Thus, we begin the chapterwrth Euler's ntethod,which has a very
straightforwardgraphicalinterpretation.In addition,becausewe have alreadyintroduced
Euler's methodin Chap. l, our emphasishereis on quantifyingits truncationeror and describineits stabilitv.
478
PART
6 ORDINARY
DIFFERENTIAL
EQUATIONS
Next, we usevisually orientedargumentsto developtwo improvedversionsof Euler's
nretlrod-tlre Heun and the midpoint techniques.After this introduction,we formally develop the conceptof Runge-Kutta(or RK) approachesand demonstratehow the foregoing
techniquesare actuallyfirst- and second-orderRK methods.This is followed by a discussion of the higher-orderRK fbrmulations that are fi'equentlyused fbr engineeringand
scientific problem solving. In addition, we cover the applicationof one-stepmethodsto
.t-y.r/erns
oJ ODEs.Note that all the applicationsin Chap.20 are limited to caseswith a fixed
stepsize.
ln Chop. 21, we cover more advancedapproachesfor solving initial-valueproblems.
First. we describeadaptiveRK ntethodsthat automaticallyadjustthe stepsize in response
to the truncationerror of the computation.Thesemethodsare especiallypertinentas they
are employedby MATLAB to solve ODEs.
Next, we discussnrulti,stepmethods.As mentionedabove,thesealgorithmsretain infbrn'rationof previousstepsto more ell'ectivelycapturethe trajectoryof the solution.They
alsoyield the truncationerror estimatesthat canbe usedto implementstep-sizecontrol.We
describea sinrplemethod-the non-self-startingHeun method-to introducethe essential
f-eatures
of the multistepapproaches.
Finally, the chapterends with a descriptionof ,stiffODEs. Theseare both individual
and systemsof ODEs thathaveboth fastand slow components
to their solution.As a consequence,they require specialsolution approaches.We introducethe idea of an implicit
solution techniqueas one commonly usedremedy.We also describeMMLAB's built-in
functionsfor solving stiff ODEs.
ln Cha1t.22, we focus on two approachesfor obtaining solutionsto boundary'-value
problems:theshootingandJinite-dffirencemethods.Aside from demonstratinghow these
techniquesare implemented,we illustratehow they handleclerivatiyeboundorvconditions
and nonlinear ODEs.
ti'.iili,
'l
'';
'j:''
':
'ii.
l:io
":i'
*l-\,
Initiol-Volue
Problems
CHAPTEROBJECTIVES
The primary objectiveof this chapteris to introduceyou to solving initial-value
problemsfbr ODEs (ordinarydifferentialequations).Specificobjectivesand topics
coveredare
t
.
.
.
Understandingthe meaningof local and global truncationerrorsand their
relationshipto stepsize for one-stepmethodsfor solving ODEs.
Knowing how to implementthe following Runge-Kutta(RK) methodsfor
a singleODE:
Euler
Heun
Midpoint
Fourth-olderRK
Knowing how to iteratethe coffectorof Heun'smethod.
Knowing how to irnplementthe followingRunge-Kuttamethodsfbr systems
of ODEs:
Euler
Fourth-orderRK
YOU'VE GOT A PROBLEM
e startedthis book with the problemof simulatingthe velocity of a free-falling
bungeejumper.This problemamountedto formulatingand solving an ordinary
diffbrentialequation,the topic of this chapter.Now let's return to this problem
and make it more interestingby computingwhat happenswhen thejumper reachesthe end
of the bunseecord.
480
INITIAL-VALU
E PROBLEMS
To do this, we should recognizethat the jumper will experiencedifferentforcesdependingon whetherthe cord is slack or stretched.If it is slack,the situationis thatoffree
fall where the only forces are gravity and drag. However, becausethe jumpel cannow
move up as well as down, the sign of the dragforce mustbe nrodifiedso that it alwaystends
to retardvelocity,
du g - srgn(r,)?u2
dt
(20.1a)
where u is velocity (m/s), t is time (s), g is the accelerationdue to gravity (9.81m/s'),c7is
the drag coefficient(kg/m), and m is mass(kg). The .signum
Jilndion.t sign,returnsa - I or
a I dependingon whetherits argumentis negativeor positive,respectively.
Thus,whenthe
jumper is falling downward (positive velocity, sign - l), the drag fbrce will be negative
and hence will act to reduce velocity. In contrast.when the jumper is moving upward
(negativevelocity.sign: -l), the dragfbrce will be positiveso that it againreduces
the
velocity.
Once the cord beginsto stretch,it obviouslyexefis an upward force on thejumper.As
donepreviouslyin Chap.8. Hooke's law can be usedas a first approximationof thisforce.
In addition,a dampeningforce shouldalso be includedto accountfor frictional effectsas
the cord stretchesand contracts.Thesef'actorscan be incorporatedalong with gravityand
drag into a secondfbrce balancethat applieswhen the cord is stretched.The resultis the
f o ll o w i n ed i f f e r e n t i ael o u a t i o n :
du
al
:,9
-
c,t
)
Slgn(u)-U- tn
k
-(r
ilt
-
I.l -
y
-U
m
(20,\b)
wherek is the cord's springconstant(N/m), x is verticaldistancemeasureddownwardfrom
the bungeejump platform (m). L is the lengthof the unstretchedcord (m), and 7 is a dampeningcoefticient(N . s/m).
BecauseEq. 120.1b)only holds when the cord is stretched(x > 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)<tf
t(n+1) = tf ;
n = n+1;
end
y = y0'ones{n,Ir;
% p r e a L l o c a t e y L o i m p r o ' . r ee F f i c i e n . y
for i = 1:n-l
?implement Euler's
method
= y(i)
y(i+1)
+ dydt(t(i),y(i),vararqini:])*(t(i+1)-t(i)
if
end
);
20.3 IMPROVEMENTS
METHOD
OF EULER'S
487
variabledydt. The initial and llnal valuesof the desiredrangeof the independentvariable
is passedas a vector tspan. The initial value and the desiredstepsizearepassedasy0 and
h, respectively.
The lunction first generatesa vector t over the desiredrangeofthe dependentvariable
using an incrementof t'r.In the eventthat the stepsizeis not evenly divisible into the range,
the last value will fall short of the final value of the range.If this occurs,the final value is
addedto r so that the seliesspansthe completerange.The length of the r vector is determined as n. In addition, a vector of the dependentvariabley is preallocatedwith n values
of the initial condition to improve efficiency.
At this point, Euler'smethod(Eq. 20.5)is implementedby a simpleloop:
y(i+1)
= y(i)
+ dydt(t(i),v(i))*(t(i+l)-t(i),var);
end
Notice how a function is usedto generatea value for the derivativeat the appropriatevalues of the independentand dependentvariables.Also noticehow the time stepis automatically calculatedbasedon the differencebetweenadjacentvaluesin the vectort.
The ODE being solvedcan be setup in severalways.First, the differentialequationcan
be definedasan anonymousfuncfionobject.For example,fbr the ODE from Example20.I :
> > 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
*'/-*<l
L l
a:
a:
a.
u:
a'.
/'
"l
,!-)'
,tI
'.k'
*,
-t-l+
\Alo
\
FIGURE20.7
RKmelhod
thefourth-order
comprising
of theslopeestimotes
C.pt-,i.otdepiction
where
k1: f fti,!i)
(20.44a)
kz: f (',*)r,t, +!;',n)
(20.44b)
t.: f (t,*)r,r,+1,,n)
(20.44c)
ka:f(tith,y,+hh)
(20.44d)
Notice that for ODEs that are a function of / alone, the classical fourth-order RK
method is similar to Simpson's1/3 rule. In addition,the fourth-orderRK methodis similar to the Heun approachin that multiple estimatesof the slope are developedto comeup
with an improved averageslopefor the interval.As depictedin Fig. 20.7, eachof theft's
representsa slope.Equation(20.44) then representsa weightedaverageof theseto arrive
at the imProvedsloPe.
E X A M P L E2 0 . 3
r KM e t h o d
C l o s s i c q lF o u r t h - O r d eR
Problem Stqtement. Employ the classicalfourth-order RK method to integratey'=
4 r 0 8 t- 0 . 5 y f r o m / : 0 t o l u s i n g a s t e p s i z e oI f w i t h y ( 0 ) : 2 .
Solution. For this case,the slopeat the beginningof the interval is computedas
:
(0.2\ -
-
: 3
20.4 RUNGE-KUTTA
METHODS
497
This valueis usedto computea valueof .r and a slopeat the midpoint:
l'(0.5):2+3(0.5):3.5
k r : l ' ( 0 . 5 .3 . - 5 :) 4 c 08 ( 05 )- 0 . - 5 ( 3 . 5: ) 4 . 2 1 1 2 9 9
This slope in turn is usedto computeanothervalue of ,vand anotherslopeat the midpoint:
r(0..5): 2 + 1.211299(0.5): 4. 108649
5) - 0.-5(4.108649): 3.912974
k7 : .f (0.5,4.108649)- 1e08(0
Next. this siopeis usedto computea valueof .r,and a slopeat the end of the interval;
, y ( 1 . 0 ): 2 + 3 . 9 1 2 9 1 4 ( 1 . 0: ) 5 . 9 1 2 9 7 4
k a : f ( 1 . 0 , 5 . 9 | 2 9 7 4- ) 4 e 0 8 ( t 0-)0 . 5 ( 5 . 9 1 2 9 7 4 ) : 5 . 9 4 5 6 7 7
Finally,the four slopeestimates
arecombinedto yield an averageslope.This averageslope
is thenusedto rnakethe final predictionat the end of the interval.
Q:
u
[3 + 2(1.2112e9)+ 2(3.912911) + 5.945611] : 4.201031
.\'(1.0) : 2 + 4.201031(1.0) : 6.201031
which cornpares
favorablywith the true solutionof 6.19463I (e, :0. 103Vo).
It is certainly possibleto developfifth- and higher-orderRK methods.For example,
Butcher's (196,+)fiflh-order RK methodis written as
)'i+r : )'i *
I
,(7kr
* - l l A r + 1 2 k 1 * - l 2 A s+ 7 k ) h
(20.45)
where
k 1: f G i , ) , i )
k::
.t(,, * ),,-r, - ]l
a/
(20.45a)
l)
(20.4sb)
t, : /(,' * Io,r',+
jr'r + jr,r,)
(20.45c)
k+: .r(,, *
+
)0,t, \r,'n hh)
(20.4sd)
rr5:y(ti*ton.,.,
+ ftr,n+ft*.n)
r o : f ( , , * h , t --i) r , , + 2 r r ' n
+ |r.n - lr^o +]r,rn)
(20.45e)
(20.4sf)
Note the similaritybetweenButcher'smethodandBoole'srule in Table17.2.As expected,
this rnethoclhas a global truncationerror of O (h5).
Although the fifth-order versionprovidesmore accuracy,noticethat six tunction evaluationsarerequired.Recall thatup throughthefourth-orderversions,n flnction evaluations
PROBLEMS
INITIAL.VALUE
498
are requiredlbr an nth-orderRK method.Interestingly,for ordershigher than four,oneor
Becausethefunctionevaluationsacc0unt
two additionalfunction evaluationsarenecessary.
relfive
and higherare usuallyconsidered
methods
of
order
time,
for the most computation
for
the
main
reasons
This
is
one
of
the
versions.
fourth-order
less
than
the
efficient
atively
popularityof the fourth-orderRK method.
20,5 SYSTEMS
OF EQUATIONS
Many practicalproblemsin engineeringand sciencerequirethe solutionof a systemof simay
multaneousordinary differentialequationsratherthan a singleequation.Suchsystems
be representedgenerallyas
d)'t
i
: / 1 ( t . . r ' 1 . . r ' . . . \. t. i.)
4z :
J : ( r .l t . ) . : . . . . .) t , )
(20.46)
"
:
,t,,
:!:!
dt
:.f,,(1.
. r . r .) . : . . . . . ) ) i )
The solution of such a systemrequiresthat n initial conditionsbe known at the stafting
value of /.
An exampleis the calculationof the bungeejumper's velocity and positionthatwe set
at
the beginning of this chapter.For the free-fall portion of the jump, this problem
up
amountsto solving the following systemof ODEs:
dx
'
dt
du
dt
(20.47)
wat
-6
)
(20.48)
m
If the stationaryplatform from which the jumper launchesis defined as r : 0, the initial
conditionswould be x(0) : u ( 0 ) : 0 .
20.5.1 Euler'sMerhod
All the methodsdiscussedin this chapterfor single equationscan be extendedto systems
In
of ODEs. Engineeringapplicationscan involve thousandsof simultaneousequations.
the
involves
applying
simply
of
equations
each case,the procedurefor solving a system
one-steptechniquefor every equationat eachstepbeforeproceedingto the next step.This
is best illustratedby the following examplefor Euler's method.
E X A M P L E2 0 . 4
S o l v i n gS y s t e m so f O D E s w i t h E u l e r ' sM e t h o d
Problem Stotement. Solvefor the velocity andpositionof the free-fallingbungeejumper
usingEuler'smethod.Assumingthat at / : 0, r : u : 0, and integrateto / : l0switha
stepsizeof 2 s.As was donepreviouslyin Examples1.1and 1.2,the gravitationalacceleration is 9.8I m/sr, and thejumper hasa massof 68.I kg with a dragcoefficientof 0.25kg/m.
20.5 SYSTEMS
OFEAUATIONS
Recallthat the analyricalsolurionfor velocityis tEq. (1.9)l:
i::^
t
i-l-
'
r,(/) : l!!! 61nn(lE!!,\
V c.i
\V
,z )
This result can be substitutedinto Eq. (20.41) which can be integratedto dererminean
a n a l y t i c asl o l u t i o nf o r d i s t a n c a
es
Gr, ll
m f
t
. r ( I ) : - I n l c o s h { . / a rl rl
cl L
\V,n'J)
Use theseanalyticalsolutionsto conpute the true relativeerrorsof the results.
Soluiion. The ODEs can be usedto computethe slopesat / :0
as
dr
-:U
dt
du
:::9.81
dt
0.25
-::::(142:9.81
68.1
Euler's methodis then usedto computethe valuesat t : 2 s,
.r:0*0(2):0
u : 0 * 9 . 8 1 ( 2:)1 9 . 6 2
The analyticalsolutionscanbe computedas.t(2) : 19.16629and r'(2) : 18.72919.
Thus,
the percentrelativeeffors are 1007oand4.1567a,respectively.
The processcan be repeatedto computethe resultsat t : 4 as
,r:0*19.62(2):39.24
u:
/
0.25
.\
19.62+ f 9.8t - lf' r 19.62)2
)z : 30.+t.lo8
68.t
\
l
Proceedingin a like mannergives the resultsdisplayedin Table 20.3.
TABTE2O.3 Distonce
ond velocityof o freeJolling
bungeeiumperos computed
numericolly
with Euler'smethod.
rt.u"
0
2
4
6
B
t0
0
1 91 6 6 3
7t 9304
147.9462
2 3 75 ) 0 4
3 3 4) 7 8 2
ur"u*
00
t 8.7292
33 il tB
42 4762
469575
49.4214
rEul".
0
39.240A
I t2 A674
2A4 664A
3 0 54 2 4 4
uEul".
o
r9 6200
36.4137
46 2983
50 I 802
5r 3t23
e, (x)
r 0 00 0 %
45 45%
?425%
]3 83%
B72%
e, (a)
476%
9.97%
1003%
6.86%
3 B3:d
soo
INITIAL.VALUEPROBLEMS
for
Although the fbregoingexampleillustrateshow Euler'smethodcanbe implemented
In
addition,
large
step
size.
systemsof ODEs, the resultsarenot very accuratebecauseof the
the resultsfor distanceare a bit unsatisfyingbecause;r does not changeuntil the second
iteration.Using a much snrallerstepgreatlymitigatesthesedeficiencies.As describednext,
using a higher-ordersolverprovidesdecentresultsevenwith a relativelyiargestepsize.
20.5.2 Runge'Kutfo Methods
of
Note rhat any of the higher-orderRK methodsin this chaptercan be appliedto systems
helpful
in
is
equations.However.caremust be takenin determiningthe slopes.Figure 20.7
develop
is,
we
first
visualizing the proper way to do this for the fourth-ordermethod.That
's)
Theseslopes(a setof ft1 arethenusedto make
slopesfbr all variablesat the initial vah-re.
predictionsof the dependentvariableat the midpoint of the interval.Thesemidpointvalues are i1 turn usedto cornputea setof slopesat the midpoint (the ft2's).Thesenew slopes
are then taken back to the startingpoint to rnakeanotherset of midpoint predictionsthat
leaclto 1ew slopepredictionsat the midpoint (the l-1's).Theseare then employedto make
predictionsat the end of the interval that are usedto developslopesat the end of the interval (the fta's). Finally, the fr's are combined into a set of increment functions [as in
Eq. (20.41))rhat are brought back to the beginningto make the final predictions.The fbllowing exarnpleillustratesthe approach.
EXAMPLE20.5
Solving Systemsof ODEs with the Fourth-OrderRK Method
Problem Stotement. Use the fburth-orderRK methodto solve fbr the sameproblemwe
addressedin Example 20.4.
Solution. First, it is convenientto expressthe ODEs in the functional lbrmat of
Eq. (20.46)as
dx
E
0Il
,tr
: f i ( t , , r ,u ) : 1 ,
Ct
: .fzj, x, u) : I - :u'
tn
t
The first stepin obtainingthe solutionis to solve for all the slopesat the beginningofthe
interval:
kr.r :
0. 0) : 0
J'r(O,
k 1 . :2 1 2 ( 0 . 0 ,0 ) : 9 . 8 1- 9 J 1 t': {'t,l 2 : 9 . 8 1
68.1
where ki..; is the lth value of ft for theTth dependentvariable.Next, we must calculatethe
first valuesof .r and u at the rnidpointof tlre first step:
r ' ( l ): r ( 0 )r f , . , ! : O + O i : o
22
u ( r ): u ( o +
) r r . r t:]o + 9 . 8 1 ]: n . t t
20.5 SYSTEMS
OF EQUATIONS
501
which can be usedto computethe first set of midpoint slopes:
k z : : . f t( 1 ,0 , 9 . 8 1 ): 9 . 8 1 0 0
k 2 . 2: f 2 ( 1 . 0 ,9 . 8 1) : 9 . 4 5 6 1
Theseare usedto deternine the secondsetof midpoint predictions:
: 19 . 8 l o o
. r ( l ): . r ( 0 )* t . . , ! : o + 9 . 8 1 0 0
;;
u ( l ) : u ( 0 )+ t z : i
0+9.4567;:9.4561
whichcanbeused,o.oilpur.thesecond'set
of midpointslopes:
: 9.4561
kt.t : .fr(1.9.8100,
9.456't)
:9.4817
k3..: fi(1, 9.8100,
9.4561)
Theseareusedto determine
thepredictions
at theendof theinterval:
x(2) : r(0) + k3.1h 0 + 9.4567
(2) : 18.9134
u ( 2 ) : u ( 0 )+ k 3 . 2 l t : 0 + 9 . 4 8 1 1 ( 2 )1: 8 . 9 6 3 4
wltichcanbe usedto computetheendpointslopes:
: )18.9634
k q . t: J t ( 2 ,1 8 . 9 1 3 1. 18,. 9 6 3 4
:
:
ka.2 t'.(2,18.9134,
18.9634) 8.,1898
Tlrevaluesof t canthenbe usedto compute[Eq.(20.4a)l:
x ( 2 ): 0 +
I
t 0 + 2 ( 9 . 8 1 0+09 . 4 5 6 1+) 1 8 . 9 6 3 4 1 2 : 1 9 . 1 6 5 6
6
u \ 2 ): 0 + ; [ 9 . 8 1 0 0 + 2 ( 9 . 4 5+6 9
7 . 4 8 1 7+) 8 . 4 8 9 8 1 2 :1 8 . ] 2 5 6
6Proceedingin a like manner for the remaining stepsyields the values displayedin
Table 20.4. In contrastto the resultsobtainedwith Euler's method, the fourth-orderRK
predictionsare much closerto the true values.Further,a highly accurate,nonzerovalue is
computedfor distanceon the first step.
TABLE2O.4
Distonce
ond velocityof o freeJolling
bungeeiumperos computed
numericolly
with thefourth-order
RKmethod.
rt.u"
0
2
4
6
8
t0
0
1 91 6 6 3
71 9304
147.9462
2 3 75 1 0 4
3 3 41 7 8 2
ut.u"
0
)B7292
331|8
42.0762
46.9575
4 44 2 \ 4
JnKr
0
19.r656
7 ) . 9 3 )I
t47 9s21
2 3 75 t O 4
3 3 41 6 2 6
unx{
0
I8.7256
330995
42.4547
46.9345
49.4427
e,(x)
e, (u)
0.004%
0 00r%
0 004%
0 000?{
0 005%
0.0r 9%
oo37%
0 051%
a a49iL
0 038%
502
INITIAL-VALUE
PROBLEMS
20.5.3
MATLAB M-file Funclion: rk4sys
Figure 20.8 showsan M-file called rk4 sys that usesthe fourth-orderRunge-Kuttamethod
to solve a systemof ODEs. This code is similar in many ways to the functiondeveloped
earlier (Fig. 20.3) to solve a singleODE with Euler's method.For example,it is passed
the
function name defining the ODEs throughits argument.
FIGURE2O.8
AnMJilelo implemenl
iheRK4meihod
foro system
oltCDEs.
€ , 1 h - F ; ^ h
--
f f ^
. ' ^ l
t L I l ' y . | , l
I ^ ^ - h
L - I J a r t '
- l t a ' t - , J . ' i '
! N l b y 5 \ u l q L ,
. , 4
y v '
L
t t t
' . - '
r - ^ i - \
r r r r
v o r a r v
g rk4sys:
fou:-th-order Runge-Kurta for d system ol ODts
= rk4sys(dydt,tspan,y0,h,p1,p2,...)
%
: int.eqrates
lt,yl
Z
a system of ODEs wit.h fourth-order
RK meEhod
? inpuL:
?'
dydt = name of the M-file
that evaluaLes the ODES
tspan = tti,
?
t.fl;
initial
times wiLh output.
and final
generaLed ac interval
Z
of h, or
= ltO t1 ... tfl;
%
times where solution
output
specific
y0 = initial
?
values of dependent variables
%
h = step size
p1 ,p2, . .. = additi"onal parameters used by dydt
%
% output:
%
tp = vector
yp = vector
%
of
of
it
nargin.4,error('ar
r!
arrJ
-
h
landfh/f
\
F + 1, -^-LrlJarrr
F
-
_ _
redsL 4
- _O
I
a:
/^r
/
input
'rcn:r
LJPcrr
= tspan(n) ;
f
L = (ti:h:tf)';
n = length(t);
if t (n) <t.f
t(n+1) = tf;
n = n+1;
end
else
t - tspan;
end
y(I ,:) = y0;
tt = tii
= y(1,
np = 1r tp(np) - tL; yp(np,:)
i -1
argumenrs requ ired' t, end
not ascendi ng order') , end
c^rr\
= tspan(1,);tf
ti
I
I );
independent variable
solution
for dependent variables
;);
.
while(1)
Eend = t(np+1);
hh = g(nP+1) - t(nP);
(Continued)
503
OF EAUATIONS
20.5 SYSTEMS
i f hh>h, h]-i = h; end
while(1)
i f ct+hh>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<lmant
hn< nrnnt
^l,r^^,^^o.
{^,
suchcoses.
Algorithms that automaticallyadjustthe stepsizecan avoid suchoverkill and hencebe
of great advantage.Becausethey "adapt" to the solution'strajectory,they are said to have
adaptivestep-sii.econtrol. Implementationof suchapproachesrequiresthat an estimateof
the local truncationerror be obtainedat each step.This error estimatecan then serveas a
basisfor either shorteningor lengtheningthe stepsize.
Betbre proceeding,we should mention that aside from solving ODEs, the methods
describedin this chaptercan also be usedto evaluatedefinite integrals.The evaluationof
the definite integral
rb
_t
t:
I
J$tdx
Ja
is equivalentto solving the differentialequation
dy -/rr,
,
,o, ,lr'Urgiventhe initial conditiony(a) : 0. Thus, the fbllowing techniques
can be employed to efficiently evaluate definite integrals involving functions that are generally
smoothbut exhibit regionsof abruptchange.
There are two primary approachesto incorporateadaptivestep-sizecontrol into onestepmethods.Stephalving involves taking eachsteptwice, once as a full stepand then as
516
ADAPTIVE
METHODSAND STIFF
SYSTEMS
two half steps.The differencein the two resultsrepresentsan estimateof the localtruncation eror. The step size can then be adjustedbasedon this error estimate.
In the secondapproach.calledetnbeddedRK methods,the local truncationenor is estimatedasthe differencebetweentwo predictionsusingdiff'erent-order
RK methods.These
are cun'entlythe methodsof choicebectrusethey ale more efficient than stephalving.
The embeddedmethodswere first developedby Fehlberg.Hence.they aresometimes
refen'edto as RK-Fehlbergmethods.At facevalue,the ideaof usingtwo predictionsof different order might seemtoo computationallyexpensive.For example,a fburth- andfifthorder predictionamountsto a total of I 0 function evaluationsper step[recallEqs.(20.44)
and 120.45)1.Fehibergcleveriy circur.nvented
this problernby deriving a fifth-orderRK
methodthat employsmost of the samefunctionevaluationsrequiredfor un accompanying
fourth-orderRK rnethod.Thus, the approachyieldedthe error estimateon the basisof only
six functionevaluationsl
2l.l.l
MATTABFunctionsfor Nonstiff Systems
Since Fehlbergoriginally developedhis approach.other even betterapproaches
havebeen
developed.Severalof theseare availableas built-in functionsin MATLAB.
ode23. The ode23 function usesthe BS23 algorithm (Bogacki and Shampine,1989;
Shampine,1994),which simultaneouslyusessecond-and third-orderRK fbrmulasto solve
the ODE and make error estimatesfor step-sizeadjustment.The formulas to advancethe
solution are
J'i+r : li -t
I
n(.2h
-t 3kz 1- kt)lt
( 2r . 1 )
where
t1 : ./(r;,,r;)
(2Lla)
r,:f(,,*)r,r,
lr,r)
(2t.tb)
t r : f ( r ,* ' r r , r ,
ir")
( 21 .c1)
The error is estimatedas
Er+t :
I
* (*5kr -t 6kz + 8k1- gk+)h
(71.2)
/l
where
k q : . f ( t i + t ,) ' i + t )
(2t.2a)
Note that althoughthereappearto be fbur function evaluations,thereare really only three
becauseafter the first step,the k1 for the presentstepwill be the ftafrom the previousstep.
Thus, the approachyields a predictionand error estimatebasedon threeevaluations
rather
2I .I ADAPTIVE
RUNGE-KUTTA
METHODS
5r7
than the five that would ordinarily result tiom using second-(two evaluations)and thirdorder (tlrreeevaluations)RK formulasjn tandem.
After each step,the error is checkedto determinewhetherit is within a desiredtolerance.If it is, the value of y;a1 is accepted,and k+ becomesk1 lbr the next step.If the error
is too large. the stepis repeatedwith reducedstepsizesuntil the estimatederror satisfies
E < n t a x ( R e l T oxl l y l . A b s T oIl
(21.3)
j)
where RelTol is the relativetolerance(default: l0
andAbsTol is the absolutetolerance
(default : l0-6). Observethat the criteriafor the relativeeffor usesa fractionratherthan a
percentrelativeerror as we havedone on many occasionsprior to this point.
ode45. The ode45 functionusesan algorithmdevelopedby Dormandand Prince(1990),
which simultaneouslyusesfburth- andfifth-order RK formulasto solvethe ODE and make
error estimatesfbr step-sizeadjustment.MATLAB recommendsthat ode45 is the best
function to apply as a "first try" for most problems.
odeL13. The ode113 functionusesa variable-order
Adams-Bashforth-Moulton
solver.It
is useful for stringentemor tolerancesor computationallyintensiveODE functions.Note
in Section21.2.
that this is a multistepn'rethod
as we will describesubsequently
Thesefunctionscan be calledin a numberof differentways.The simplestapproachis
Ir,
- t . ' 1= o c l e 4 5 ( o d e f u n , t - s p a n , - r . ' d )
wherey is the solutionarraywhereeachcolumn is one of the dependentvariablesand each
row corresponds
to a time in the column vector t, odefun is the name of the function
returninga column vectorof the right-hand-sides
of the difTerentialequations,t span specifies the integrationinterval,and y0 : a vectorcontainingthe initial values.
Note that . s-pancan be lbrmulatedin two ways.First. if it is enteredas a vectorof two
numbers.
f-spar -
Ltt
tf);
the integrationis perfornredfrom ti to tf. Second.to obtain solutionsat specific times
t0, tl
tn (all increasingor all decreasing),
use
t-qpal
=
ltA
t1
tn);
Herc is an example of how ode45 can be used to solve a single ODE. .r":
4e08t- 0.-5.r,
fiom /:0
to 4 with an initial conditionof y(0):2. Recallfrom Example 20.I that the analyticalsolutionat t:4
is 75.33896.Representing
the ODE as an
anonymousfunction, ode4 5 can be usedto generatethe sameresultnumericallyas
>> dydt=G(t,y) 4*exp(0.8*L) -0.5*y;
>:, It,y1=66"4!.(dydt,lA 4),21 ;
>> y(Length(tt
)
75.3390
As described in the following example, the ODE is typically stored in its own M-file when
5r8
EXAMPLE2l .l
ADAPTIVE
METHODSAND STIFF
SYSTEMS
Using MATLABto Solve o Systemof oors
ProblemStotement. Employ ode45 to solvethe following setof nonlinearODEsfrom
t:0to20:
dt''
dv,
- 0 . 8 1 ' *, 0 . 3 y 1 y 2
;:l.2ytE:
where .r'1: 2 and.1'2: 1 at 1 : 0. Suchequationsarerefered to aspredator-pre)'cquations.
0.6)rlz
Solution. Before obtaininga solutionwith MATLAB, you must createa functionto compute the right-handside of the ODEs. One way to do this is to createan M-file as in
r uflCLi on yp = pr eopi e/ ( L,y)
*y\2\
= l'1.2*y lI)
0.6*y(1)
yp
;-A.B"y(2)+0.3*y(1)
*y(2)
I ;
We storedthis M-file underthe name:predprey. m.
Next, enter the following commandsto specify the integrationrange and the initial
conditions:
'.
spdn
fn 20 ;
>> ya = 12, 7l;
The solvercan then be invoked by
This command will then solve the differential equationsin predprey.m over the range
definedby tspan usingthe initial conditionsfound in yo. The resultscanbe displayed
by
simply typing
>1
plot(t,y)
whiclr yieldsFig. 21.2.
F I G U R 2EI . 2
Solutionof predotor-prey
moo'elwithMATLAB
5r9
METHODS
RUNGE-KUTTA
2I .I ADAPTIVE
In addition to a time seriesplot, it is also instructiveto generatea phase-planeplotthat is, a plot of the dependentvariablesversuseachother by
>> plot (y( :, 1),v (:,2) )
w h i c h v i e l d sF i s . 2 l . 3 .
F I G U R2
EI . 3
modelwiihMATLAB
Sloiespoceplotof predofor-prey
?.
!.2
1
oot
?
y1
to control varAs in the previousexample,the MAILAB solverusesdefaultparameters
ious aspectsofthe integration.In addition,thereis alsono controloverthe differentialequaTo havecontroloverthesefeatures,additionalargumentsareincludedasin
tions' parameters.
= ode45(odefun, tspan, yA, options'
lt,yl
p1' p2,"')
where options is a datastructurethat is createdwith the odeset function to control feathat you want to passinto odefun.
turesof the solution,andpJ , p2, .. . areparameters
The odeset function has the generalsyntax
options
= odeset
('parr'
,vaf,,'par
'
,vaf
' - -.)
where the parameterpar, has the value vaf .. A completelisting of all the possibleparameterscan be obtainedby merely enteringodeset at the commandprompt. Some commonly usedparametersare
'RelTol
'
'AbsTol '
,rnirialsrep'
'MaxSf
'
Allows you to adjustthe relativetolerance.
Allows vou to adiustthe absolutetolerance
the initial step.This optionaldetermines
The solverautomatically
your
own.
you
to
set
lows
The maximum stepdefaultsto one-tenthof the t span interval.This
520
EXAMPLE
2I .2
ADAPTIVE
METHODSAND STIFF
SYSTEMS
U s i n g o d e s e t t o C o n i r o l I n t e g r o t i o nO p t i o n s
ProblemStotement. Use ode23 to solvethe followine ODE,from I : 0 to 4:
/t al
2
dt
:
10e
{r l)
[ 2 r 0 . 0 7 5| r-
0.6t
where1(0) :0.5. Obtainsolutionsfor the default(10-3)and for a more stringent(l0r)
relative error tolerance.
Solution. First,we will createan M-file to computethe right-handsideof theODE:
function yp = dydt (t, y)
y P = 1 O * e x p( - ( L - 2 I * ( t - 2 ) / ( 2 * . A 7 5 ^ 2 ) ) 0 . 6 * y t
Then, we can implementthe solverwithout settingthe options.Hencethe defaultvaluefor
the relativeenor ( l0 3) is automatically
used:
> > o d e 2 3 ( @ c 1 y d r ,t A 4 l , 0 . 5 ) ;
Note that we have not setthe function equalto output variables tr , y I . When we implement one of the ODE solversin this way, MATLAB automaticallycreatesa plot of the
resultsdisplayingcirclesat the valuesit hascomputed.As in Fig. 2 l .4a, noticehow ode23
takes relatively large stepsin the smoothregionsof the solufion whereasit takessmaller
stepsin the region ol'rapid changearoundI : 2.
We can obtailt a more accuratesolutionby using the ocleser function to settherelat i v e e r r o rt o l e r a n c teo l 0 a :
>>
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 )
<
It
(2r.15)
525
2I .3 STIFFNESS
Notice that the right-handsidesof Eqs. (21.12) and (21.15) are identical,with the exception of the argumentof the third derivative.If the third derivativedoesnot vary appreciably over the interval in question,we can assumethat the right-handsidesare equal, and
therefore,the left-handsidesshouldalso be equivalent,as in
E ' 5-
.,0
- ) i-t
_
1'ri
ri-t
(l l.16)
Thus, we have arrived at a relationshipthat can be usedto estimatethe per-steptruncation
error on the basis of two quantitiesthat are routine by-productsof the computation:the
predictor(1,9*r) anAthe corrector("vlir) .
EXAMPLE
21.4
Error
Estimqte
Truncotion
of Per-Steo
Problem Stotemenf. Use Eq. (21.16) to estimatethe per-steptruncationerror of Example 21.3. Note that the true valuesat / : I and 2 are6.194631and 14.84392,respectively.
Solution. At /;a1 : l, the predictorgives5.607005and the comectoryields 6.360865.
Thesevaluescan be substitutedinto Eq. (21.l6) to give
- 5.607005
6.360865
: -0.150722
F_
which compareswell with the exacteffor,
Et : 6.194631- 6.360865:
-0.1662341
At /;11 :2, the predictorgives 13.44346and the correctoryields 15.30224,which
can be usedto comDute
F
-_
- 13.44346
15.30224
: -0.31176
which also compares favorably with the exact error, Et : 14.84392- 15.30224:
-0.45831.
The foregoing has been a brief introduction to multistep methods. Additional
information can be found elsewhere(e.g., Chapra and Canale, 2006). Although they
still have their place for solving certain types of problems,multistep methods are usually not the method of choice for most problemsroutinely confrontedin engineeringand
science.That said, they are still used.For example,the MATLAB function odel13 is a
multistep method.We have thereforeincluded this sectionto introduceyou to their basic
principles.
2r.3
STTFFNESS
Stiffnessis a specialproblem that can arisein the solution of ordinary differentialequations. A stiff systemis one involving rapidly changingcomponentstogetherwith slowly
changing ones. In some cases,the rapidly varying componentsare ephemeraltransients
that die away quickly, after which the solutionbecomesdominatedby the slowly varying
526
ADAPTIVE
METHODSAND STIFF
SYSTEMS
F I G U R 2EI . 7
D l o 'o r o , l r [ s o i t ' o n o r o r i n g l eC D L Althoughlhe solutionoppeorsto slorlot l, thereis
octuo ly o fost ircnsienifrom y : g 1e I thct occursin lessthnn the 0 OO5lime unil.Thistronsienl
. p e ' c e pi b l e e n l , w h e n h e r e : p o n s eis viewed on the finertimescolein the insel.
components.Although the transientphenomenaexistfor only a shortpart of theintegration
interval,they can dictatethe time steplbr the entire solution.
Both individual and systemsof ODEs can be stilT.An exampleof a singlestitTODEis
,/ r'
;
:
- 1 0 0 0 r '* . 1 0 0 0 - 1 0 0 0 e '
If I (0) : 0. the analyticalsolutioncan be developedas
. l : 3 0 . 9 9 8 e - l { ) { x- t 2 . o o 2 e '
(2r.17)
(21.18)
As in Fig. 21.1,the solutionis initially dominatedby the fast exponential
term(e-rmr).
After a shortperiod (t < 0.005), this transientdies out and the solutionbesomesgoverned
b y t h e s l o we x p o n e n t i a( le / ) .
Insight into the stepsize requiredfor stability of such a solutioncan be gainedby expart of Eq. (21.17):
aminirrgtlrehor.nogeneous
dt'
(2t.t9)
dt
If f'(0) - )t), calculuscan be usedto determinethe solutionas
-t' : -1'ue
ttl
Thus, the solution startsat ).0and asymptoticallyapproacheszero.
Euler'smethodcan be usedto solvethe sameproblemnumerically:
-]i+t :)'it
dt,,
, n
dt
S u h s t i t u t i nEgq .t 2 | . l 9 t g i v e s
.l't+l :)'i
-alih
EX
2I .3 STIFFNESS
527
or
. \ ' i + r:
t'i(l - ah\
e| .20)
The stabilitl'of this formulaclearlydependson the step sizeh.That is. ll - a/zl mLrstbe
l e s st h a n l . T h u s ,i f h : , 2 / a , l . y r l - - o o a si - + o o .
For the fast transientpart of Eq. (2 I . I 8 ), this criterioncan be usedto show that the step
size to maintainstabilitymust be <2/1000:0.002. In addition.we sl.rouldnote that,
whereasthiscriterionmaintainsstability(i.e.,a boundedsolution).an evensmallerstepsize
would be requiredto obtainan accuratesolution.Thus,althoughthe transientoccurs1oronly
a sntallfi'actionof theintegrationinterval,it controlsthemaximumallowablestepsize.
Ratherthan usingexplicit approaches,
implicit methoclsof'teran alternativeremecly.
Such representations
are called intplicit becausethe unknown appearson both sidesofthe
equation.An implicit forrn of Euler's method can be developedby evaluatingthe derivative at the future tirne:
-\','l:\';-|-
. d.r.;rr,
'dt
ll
This is calledthe baclovutd,or inrltlicir,Euler'smethotl.Substituting
Eq. (2 I . l9) yielcls
,),+t :
)'i
-ali+th
which can be solvedfbr
\ij
.\l-l:-
I t(tn
1ll.2ll
-+ 0 as r ,> oo. Hence, the approach is
For this case, regardless of the size of the step, l_r,'i
|
called unc on di t i o n al I v stubl e.
EXAMPLE
2I 5
E x p l i c i ot n d l m p l i c i E
t uter
ProblemStotemeni. UseboththeexplicitandrmplicitEulermethods
ro solveEq.(21.11),
wherer'(0) : 0. (a) Use theexplicitEulerwith stepsizesof 0.000-5
and0.0015to solvefor
,t'between/ : 0 and 0.006.(b) Use the implicit Euler with a stepsizeof 0.05 to solve fbr I'
between0 and0.4.
Solution. (a) For this problem,the explicitEuler'smethoclis
J,r+r: .r,rf (_1000],i * 3000 _2000e ')h
The result for h:0.000-5 is displayedin Fig. 21.8a along with the analyticalsolution.
Although it exhibitssometruncationelror, the resultcapturesthe generalshapeof the analytical scllution.
In contrast,when the stepsizeis increased
to a valuejustbelow the stability limit (ft : 0.0015).the solutionmanifestsoscillations.
Usingft > 0.002would resulrin
a totally unstablesolution-that is, it would go infinite as the solutionprogressed.
(b) The implicit Euler'smerhodis
.J,r+r: .yi* (_1000],i+rf 3000 _ )eeeg-ti+t1p
s28
ADAPTIVE
METHODSAND STIFFSYSTEMS
.v
1.5
I
0.5
0
0.002
)
z
0
0.1
0.2
(bl
0.3
0.4
FTGUR.E
2r.8
S o i u t i oonf o s t i fC
f D Ew i t h( o )t h ee x p l i c o
i t n d ( b )i m p l i c E
i t u l em
r ethods
Now becausethe ODE is linear.we can rearrangethis equationso that y;11is isolated
on
the left-handside:
Li't
]i -F 3000/r- )QQQlls
''t'r-
l+roooft
The resultfor h :0.05 is displayedin Fig. 2l.8b alongwith the analyticalsolution.Notice
that even though we have useda much bigger stepsize than the one that inducedinstability for the explicit Euler, the numericalresult tracksnicely on the analyticalsolution.
Systemsof ODEs can also be stifT.An exampleis
dyt
__:_-_5r,.r1.,^
dr
dv
-; :
dr
- -l0ly2
1001'1
(21.22a)
(2t.22h)
For the initial conditions.rr(0) : 52.29and l,:(0) : 83.82,theexactsolutionis
- g.67r-302.{\10tt
lt : 52.96e-3.e8eet
+ 65.gge-302.01t)1t
lz : lJ .83e--r.e8eel
(21.23a)
(2t.23b)
2I .3 STIFFNESS
529
Note that the exponentsare negativeand dilTerby abouttwo ordersof magnitude.As with
the singleequation,it is the largeexponentsthatrespondrapicllyandareat the heartofthe
system'sstiffness.
An implicit Euler's methodfbr systemscan be formulatedfor the presentexarnpleas
, \ ' r . i + l:
l r i * ( - - 5 , t ' ri + r f 3 . v z . i + r ) / l
. \ ' 2 . i +:tr ' 2 1 f ( 1 0 0 r ' 1 ; 1 1 - 3 0 1 . y 2 . ; " . 1 ) / z
(21.24a)
(21.24b)
Collecting terms gives
( l + - 5 / z ; . 1 ' 1 . -' * r3 ) ' : . i + t :
)t i
- 1 0 0 , r ' 1; 1 r * ( l t 3 0 1 / z ) 1 ' 1 . i :1 1
1'.,;
(21.25a)
(2L25b)
Thus. we can seethat the problem consistsof solving a set of simultaneousequationsfbr
eachtime step.
For nonlinearODEs,the solutionbeconresevenrnoredilficult sinceit involvessolvinga
systemof nonlinearsimultaneous
equations(recallSection12.2).Thus,althoughstabilityis
gainedthroughimplicit irpproaches.
a priceis paid in tlreform of addedsolutionconrplexity.
21.3.1 MATTABFunclionsfor Stiff Systems
MMLAB
hasa numberof built-infunctionstbr solvingstiff systemsof ODEs.Theseare
odel-5s. This function is a variable-ordersolverbasedon numericaldifferentiation
formulas.lt is a multistepsolverthat optionally usesthe Gear backwarddifferentiation
forrnulas.Tltis is usedfor stiff problemsof low to medium accuracy.
ode2 3 s. This function is basedon a modified Rosenbrockformula of order 2. Becauseit
is a one-stepsolver,it miry be more efficientthanode15s at crudetolerances.
It can
solvesomekindsof stifTproblents
betterthanodel5s.
ode23t. This function is an implementationof the trapezoidalrule with a "free" interpolant.This is usedfor moderatelystiff problemswith low accuracywhereyou needa
solutionwithoutnumericaldanrping.
ode23tb. This is an implementation
of an implicit Runge-Kuttaformulawith a first
stagethat is a trirpezoidalrule and a secondstagethat is a backwarddifferentiationformula of order2. This solvermay alsobe more efficientthanode15s at crudetolerances.
EXAMPLE
21 6
MATTAB
for StiffODEs
Problem Stotement. The van der Pol equationis a model of an electroniccircuit that
aroseback in the daysofvacuum tubes,
#
-p(r- li)! *,-,:o
(E21.6.1)
The solutionto this equationbecomesprogressively
stifferas 1r getslarge.Given the init i a l c o n d i t i o n s.,r ' r ( 0 ): h ) l l d t : 1 ,
u s e M A T L A B t o s o l v et h e l b l l o w i n g t w o c a s e s :
( a ) f b r I L : l . u s eo d e 4 5t o s o l v e f r o m / : 0 t o 2 0 ; a n d ( b )f o r 1 t : 1 0 0 0 , u s eo d e 2 3 st o
solvefrom/:0to6000.
s30
ADAPTIVE
METHODSAND STIFFSYSTEMS
Solution. (a) The first stepis to convertthe second-orderODE into a pair of first-order
ODEs by defining
1/l'r
-
,
dT
-f I
Using this equation.Eq. (E2l .6.1)can ['rewrittenirs
d)':
: s ( l - r ' i ) . u- .. \ , r: 0
dt
An M-file can now be createdto hold tlris pair of diff'erentialequations:
un=
yp
, v.nd-rpo
r ,\',1u,
ion )p
^2,)*y (2) --{ (r)
ly (2) ;mu* (1-y(1)
I ;
N o t i c e h o w t h e v a r l u e1o.firs p a s s e d a s a p a r a m e t e r ' . A s i n E x a m p l e 2 l . l ,5ocdaen,b1e i n voked and the resultsplotted:
'
>> piof, (t,y(:,1),
>>
'
,i-,y (:,2)
,'
')
lesend('yl','y2');
Observethat becausewe are not specityingany options,we must useopenbracketsll as
a place holder.Tlre srnoothnatureof the plot (Fig. 21.9a)suggeststhat the van derPol
equationwith p : I is not il stiff system.
(b) If a standardsolverlike oc1e45is usedfor the stiff case(p : 1000), it will fail miserably (try it, if you like). However,ocle,tI s doesan efficientjob:
>> 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) <= y1o
srphon = O;
y (f; >= 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 <,r < l). and p is a nondimensional
parameterthat de- first-ordcrkineticsin an axially dispersedplug-flowreactor
(Fig.P22.12):
gcomctrv:
scribcsthe heat transf-erand
.(:)(# P,):
NL
T
rJ
U'- 2,,,]
where /z : a heat transfercocfficicnt, k: thermal conductivity, L : the lengthor height of thc cone, andm - the slopc
t a t
(1"(
A(
D__U__kt.:O
drr
d,r
where 1) : the dispersioncoefficient 1m2/hr),c : concentration (mol/L), ,r : distance(m), U: the velocity(m/hr),
tra
ter
an
ap
Cc
wl
22
de
Ifr
rea
us
PROBTEMS
561
FIGUREP22.12
An oxiolly
dlspersed
plugflowreoctor
andt : the reactionrate (/hr). The boundaryconditionscan
beformr-rlatcd
as
Usc thc finite-differenceapproachto solvc fbr the concentration of cach reactantas a tirnction of clistancegiven: D :
0 .I m 2 / n r i nU, : I m / r n i n ,t r : 3 / r n i n , k r : l l m i n , L :
tlc
: 0)
U c i n* U c ( . r : 0 ) - D - ( r
0.5 m. c,,',,: l0 rnol/L. Enrploycenteredfinite-diltcrence
approximirtionswith z\-r : 0.05 m to obtain your solutious
dc
-(r:/_):0
and
assumeDanckwertsboundaryconditionsasdescribedin
dI
Prob. 22.12. Also, computr-the sum o1'the reactantsas a
where.in: theconcentration
in the inf]ow (mol/L), l : the tunctionof distance.Do your resultsmakesensc''J
iengthof the reactor (m). Thcse are callcd Dant'kverts 22.14 Ahiolllnr with a thicknessL, (crn).grows on the surboundun'conditions.
face of a solid {Fig. P22.14).After traversinga dillusion
Usethe tinite-difl-erenceapproachto solve tbr concen- layer of thickness| (cm), a chemicalcompoundA diffuscs
tration
asa functionof distancegiven the tbllowingparame- into thc biofllrn where it is subjcctto an irrevcrsiblefirstrers:
D: 5000mr/hr, U : 100rn/hr.li :2/hr. | : t00 rn, ordcr reactionthat convertsit to a productB.
nndc'n : 100 mol/L. Employ centeredflnite-diffbrence
Steady-statcmass balancescan be used to derive the
approximations
with A.r : l0 rr to obtainyour solutions. fbllowing ordinary diltbrcntial equationsfor cornpoundA:
your numericalrcsultswith the analyticalsolution:
Compale
tl) c,,
D +:0
0<r<L
u cin
ur'
tU -
DXt)).\e):t-
\U
,
D).)L.e) tl
lL,t'":L/
'' -
,1).
),rr')'t r'i
,
where
,\:#(-'/;-#)
22,13A seriesof first-ordcr,liquid-phasereactionscreatea
product(B) and an undcsirablcbyproduct(C):
desirable
Dr!-frc.,,:0
ur'
< Ll
Lt
wherel) : the diftusion coefficientin the diffusion layer :
0.8 cm /d, D1 : ths ditTusioncoetTicient
in the biofilm :
0.611
cmr/d, and A : thc flrst-ordcrlate for the conversionol
A to B :0. l/d. The tbllowing boundalyconditionshold:
(rt :
Ll. ,
(ttl)
--r:0
tlx
n\s5c
l. <x
at -f :
0
ats=L-Lt
If thereactionstakeplacein an axially dispcrsedplug-flr>w where r',,e: the concentrationof A in the bulk liquid :
100rnol/L. Lr.sethc' finitc-difl-erencc
nrethodfo cornp.rtethc
reactor(Fig. P22.)2). .stead),-sIare
nrass balance.scan bc
: 0 to L * Lr. where
steady-state
fiom
distribution
of
A
.r
usedto dcvelol'rthe following sL-cond-ordel'ODEs:
f, : 0.008 cm and L, = 0.001 cm. Employ centeredfinite
tl-L',
al(,
ditlerenceswith A.r : 0.001crn.
D l1r'.,:()
U;
d-t'
d-t
22.15 A cable is hangingfiom two supportsat A and B
(Fig. P22.I5). The cable is loadedwith a distributedload
c/lr'r
dr'^
D lrr',, -(-ri,-0
Download