Docsity

Applied Numerical Methods with MATLAB, Notas de estudo de Eletrônica

Pré-visualização parcial do texto

Baixe Applied Numerical Methods with MATLAB e outras Notas de estudo em PDF para Eletrônica, somente na Docsity!

CONTENTS

About the Aufhor iv

Prefoce xiii

Guided Tour xvii

PanrOur Modeling,Computers,ond ErrorAnolysis I

l. I Motivofion I

1.2 Port Orgonizotion 2

CHAPTERI 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 o lM o d e l 5 1.2 ConservotionLowsin Engineeringond Science 12 1.3 NumericolMethodsCoveredin ThisBook (^) l P r o b l e m s1 7

CHAPTER 2 MATLAB Fundomeniqls (^20) 2.1 TheMATIABEnvironmenl

  1. 2 A s s i g n m e n t2 2 2.3 MothemoticolOperotions 27
  2. 4 U s eo f B u i l t l nF u n c t i o n s3 0
  3. 5 G r o o h i c s 3 3 2.6 (^) OtherResources 2.7 (^) CoseStudy:ExplorotoryDotoAnolysis 37 ^ r r o b l e m s l l J Y

CHAPTER 3

Progromming with MATTAB 42

3.1 M-Files 43

3.2 InputOutput 47

v I t! (^) CONIENTS

CONTENTS

 - 3 4 N e s t i n go n d I n d e n t o t i o n6 3 3 S h u c t u r e dP r o g r o m m i n g5 l - 3 5 P o s s i n gF u n c t i o n st o M - F i l e s - 3.6 CoseStudy:BungeeJumperVelocity - Problems 
  • CHAPTER - Roundoff qnd Truncotion Errors - 4 1 E r r o r s B - 4.2 RoundoffErrors - 4.3 TruncotionErrors - , l 4 4 T o t o lN u m e r i c o lE r r o r - 4 5 B l u n d e r s ,M o d e lE r r o r s ,o n d D o t oU n c e r t o i n t y1 - , l P r o b l e m s - .| PnnrTwo RootsondOptimizotion - 2.2 Port Orgonizotion I 2 1 O v e r v i e w t l l
  • CHAPTER - Roois: Brocketing Methods I l - 5 1 R o o t si n E n g i n e e r i n go n d S c i e n c e I l - 5 2 G r o p h i c o lM e t h o d s I l
    • 5 3 B r o c k e t i n gM e t h o d so n d I n i t i o lG u e s s e s
      • 5 4 B i s e c t i o n1
      • 5 5 F o l s eP o s i t i o n
      • 5.6 CoseStudv:GreenhouseGosesond Roinwoter - , l P r o b l e m s
  • CHAPTER - Roots: Open Methods |
    • 6 2 N e w t o n - R o p h s o n1 6 1 S i m p l eF i x e d - P o i n il t e r o t i o n
    • 6 3 S e c o n tM e t h o d s
      • 6 5 P o l v n o m l o l s1 6 4 M A T L A BF u n c t i o n :f z e r o - r
      • 6 6 C o s eS t u d y :P i p eF r i c t i o n
      • Problems
    • CHAPTER CONTENTS Yrt - Optimizofion - 7.1 Introduciionond Bockground - 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 i o n1 - 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 i o n1 - P r o b l e m s 7 4 C o s eS t u d y :E q u i l i b r i u mo n d M i n i m u mP o t e n t i o lE n e r g y l 8 l - ,| PrnrTxnrr LineorSystems
  • CHAPTER - 3.1 Overview - Lineqr Algebroic Equofions ond Motrices | 3.2 Port Orgonizotion l9l - 8.1 MotrixAlgebroOverview - 8.2 SolvingLineorAlgebroicEquotionswith MATLAB - 8.3 CoseStudy:Currentsond Voltogesin Circuits - Problems
  • CHAPTER - G o u s s E l i m i n o t i o n - 9 1 S o l v i n gS m o l lN u m b e r so f E q u o t i o ns - 9 2 N o i v eG o u s sE l i m i n o t i o n2 - 9 3 P i v o t i n g - 9 4 T r i d i o g o n o lS y s t e m s2 - 9.5 CoseStudy:Model of o HeotedRod - Problems - [U Foctorizotion CHAPTERIO - 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 - 1 0 2 G o u s sE l i m i n o t i o no s l U F o c t o r i z o t i o n2 - 1 0 3 C h o l e s k yF o c t o r i z o t i o n2 - 'l0. - MATLABLeftDivision - Problems - Motrix lnverse ond Condition CHAPTERI I - 1 i .l TheMotrix lnverse - I I .2 ErrorAnolysisond SystemCondition - I 1 3 C o s eS t u d y :I n d o o rA i r P o l l u t i o n2 - Problems
  • CHAPTERI - Iterotive Methods - l2.l LineorSystems:Gouss-Seidel - , l - N o n l i n e o rS y s t e m s2 - 1 2 3 C o s eS t u d v :C h e m i c o lR e o c t i o n s2 - P r o b l e m s2
    • PtrnrFoun CurveFitting
      • 4.1 Overview
      • 4.2 Port Orgonizotion
  • CHAPTERI - Lineor Regression - 1 3 I S t o t i s t i c sR e v i e w - 1 3 2 L i n e o rL e o s t - S q u o r e sR e g r e s s i o n2 - , l - L i n e o r i z o t i o no f N o n l i n e o rR e l o t i o n s h i p s3 - 1 3 4 C o m p u t e rA p p l i c o t i o n s3 - 1 3 5 C o s eS i u d y :E n z y m eK i n e t i c s
  • CHAPTERI rroDtems ^ l l 5 | z - Generol lineqr Leosf-Squores qnd Nonlineqr Regression - 1 4 .I P o l y n o m i o lR e g r e s s i o n3 - 1 4 3 G e n e r o lL i n e o rL e o s tS o u o r e s 1 4 2 M u l t i p l eL i n e o rR e g r e s s i o n3 2 O - 14.4 QR Foctorizotionond the BocksloshOoerotor - 1 4 5 N o n l i n e o rR e g r e s s i o n3 - 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 - 1 8 3 G o u s sQ u o d r o t u r e4 - I 8 4 A d o p t i v eQ u o d r o t u r e4 - I8.5 CoseStudy:Root-Meon-SquoreCurrent - Problems
    • CHAPTERI - Numericql Differentiqtion - 19.I Inhoductionond Bockground - 19.2 High-AccurocyDifferentiotionFormulos - I 9 3 R i c h o r d s o nE x t r o p o l o t i o n4 - ,l9. - Derivotivesof UnequollySpocedDoto - ,l9. - Derivotivesond lntegrolsfor Dofo with Errors - 19.6 PortiolDerivotives - I9.2 NumericolDifferentiotionwith MATLAB - I 9 8 C o s eS t u d y :V i s u o l i z i n gF i e l d s - Problems
      • Pnnr5x OrdinoryDifferentiolEquotions - 6.1 Overview - 6.2 Porl Orgonizofion
  • CHAPTER - Initiol-Volue Problems - 20.I Overview - 20.2 EuleisMethod - 20.3 lmprovemenlsof Euler'sMethod - 20.4 Runge-KutfoMethods - 20.5 Systemsof Equotions - Problems 20.6 CoseStudy:Predotory-PreyModelsond Choos 50A - Adopfive Merhods ond Stiff Systems CHAPTER2I - 21 .'l AdoptiveRunge-KuttoMethods - 2l .2 MultistepMethods - 2l .3 Stiffness - 2 l 4 M A T L A BA p p l i c o t i o n :B u n g e eJ u m p e rw i t hC o r d - 2l .5 CoseStudy:Pliny'slntermittentFountoin
  • CHAPTER CONTENTS xl
    • Boundory-Volue Problems
    • 22.1 lntrodvctionond Bockground
    • 22.2 lhe ShootingMethod
    • 22.3 Finite-DifferenceMethods
    • P r o b l e m s5
    • APPENDIXA: EIGENVALUES
    • APPENDIXB: MATLABBUILT-INFUNCTIONS
    • APPENDIX€: MATIAB M-FltE FUNCTIONS
    • BIBLIOGRAPHY
    • rNDEX

PARTI^ MODELING,COMPUTERS,AND ERRORANALYSIS

invariablyhave^ occasionto use^ commerciallyavailableprepackagedcomputerpro- grarnsthat involve numericalmethods.The intelligentuseof theseprogramsis greatly enhancedby an understandingof the basic^ theory^ underlyingthe methods.In the ab- senceof suchunderstanding,you will be left to treat suchpackagesas "black boxes" with little critical insightinto their inner workingsor the validity of the resultsthey produce.

  1. 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.
  2. Nr.rrnericalmethodsare an efficient^ vehiclefbr learningto usecomputers.Becausenu- mericalmethodsale 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 acknowl- edge and control the errors of approximationthat are part and parcel of large-scale numericalcalculations.
  3. Numericalmethodsprovidea vehiclefbr you to reinforceyour understandingof math- ernatics.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.

With thesereasonslls motivation.we can now set out to understandhow numerical methodsand digital computerswork in tandemto generatereliablesolutionsto mathemat- ical 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 numer- ical methods.Although it might be temptingto jump right into this material,Part One con- sistsof 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 end the chapterwith an overview of the major areersof numericalrnethodsthat we cover in 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 PARTORGANIZATION

Chapter-l shows how MATLAB's programmingmode providesa vehicle for assem- bling 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 exactly.The latter part addressestruncationerrctrsthat arisefiom using^ an approximation in place of an exactmathematicalprocedure.

I .I A SIMPTEMATHEMATICALMODEL

Upwardforce d u et o a i r resistance

t

til

til

v

Downward forcedue to gravrty

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 hr e s p e c tt o t i m e.

d u c d. d t ' ' m

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.rputer- orientednumericalor approximatesolution. Aside from showing you how the computercan be usedto solve this particularprob- lem, 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.rodelsfigure prominentlyin the way engineersand scientistsuse numericalmethodsin their work.

I. l A SIMPTEMATHEMATICATMODET

A motlrcnntical ntodelcan be broadly definedas a tbrmulation or equationthat expresses the essentialfeaturesof a physicalsystemor processin mathematicalterms.In a very gen- eral 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 (^) f o r c i n e \

.. '... , _ , : J^ [ '. , , (^). p u r a n ) e t e r s. v a n a o l e (^) \ v a n a D t e s l u n 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 ntath- ematicalexpression,or model,of the secondlaw is the well-known equation F : m a (^) ( t. 2 ) whereF is the net force actingon the body (N, or kg nls"), m is the n.rassof the object(kg), a n dc i i s i t s^ a c c e l e r a t i o n( r n / s : ).

flGUREl.l

Forcesociingon o lreeJollingbungee iumpet.

6 MATHEMATICALMODELING,NUMERICALMETHODS,AND PROBLEMSOLVING

The secondlaw can be recastin the format of Eq. ( 1.l) by merely dividing both sides by m to give F n

( 1. - 3 1

where a is the dependentvariablereflecting the system'sbehavior,F is the forcing func- tion, 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 characteristicsthat aretypical of mathematicalmodels of the physicalworld.

. (^) It describesa naturalprocessor systemin mathematicalterms. . (^) It representsan idealizationand sirnplificationof reality.That is. the model ignoresneg- ligible detailsof the naturalprocessand focuseson its essentialmanif'estations.Thus, the secondlaw doesnot include the effects of relativity that are of minimal importance when (^) appliedto objectsand forcesthat interacton or aboutthe earth'ssurfaceat veloc- ities and (^) on scalesvisible to humans. . (^) Finally, it yields reproducibleresultsand,consequently,can be usedfbr predictivepur- poses.For example,if the force on an object and its 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 d u F dt 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 oppos- ing forces: the downward pull of gravity Fp and the upward force of air resistanceFy ( F i g. 1. 1 ) : F : F o l F t i (^ 1. 5 ) If force in the downward direction is assigneda positive^ sign,^ the secondlaw can be u.sedto formulatethe force due to pravity as F o : m 8 whereg is the accelerationdue to gravity(9.81m/s2).

MATHEMATICALMODELING,NUMERICALMETHODSAND PROBTEMSOLVING

Solution. Insertingthe parametersinto Eq. (1.9)yields

which can be usedto compute

t, 3 u , m f s o I B 72, 3 3 I I t B 42 4762 4 6 9 5 7 5 4 S4 2 t 5 0 6 1 7 5 5 1 6 9 3 8

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.

Theonolyticolsolutionforthebungeeiumperproblemos compuredin ExompleI 1 Velociry

increoseswith time ond osympfoiicollyopproocheso terminolvelociiy.

B

t 0 1 2

] .I A SIMPLEMATHEMATICALMODEL 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 bal- ancewith the air resistance.Thus. the net force is zero and accelerationhas ceased.

Equation( I .9) is called an anabtical or closed-formsolution becauseit exactly satis- fies 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 the time tateof changeof velocitycan be approximatedby (Fig. 1.3):

d t t

d t^ -

A u N

d t t d t

  • : - : A u^ u ( / i + r )-^ u ( t i ) Lt ti+t -^ ti

( r. l r )

whereAu and At aredifferencesin 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 approximatebecauseAr is flnite.Rememberfrom calculusthat

  • (^) l i m A 1 + l )

Equation( l. I I ) representsthe reverseprocess.

F I G U R EI. 3

Theuseof o finitedifferenceto opproximotethefirsfderlvotiveof u wifhrespectio /

A U

Approximateslope A r _ u ( 1 , * , )- u ( r , ) L t l i + t -^ t i

I .1 A SIMPLEMATHEMATICALMODEL t t

Terminalvelocity

F I G U R EI. 4

Compcrisonof thenumericolond onclyticolsolutionsfor^ thebungeeiumperproblem

The calculationis continuedin a similar fashionto obtain additionalvalues:

u, m/s 0 r 9. 6 2 0 0 3 6. 4 1 3 7 46 2983 5 0 r 8 0 2 5 t 3 1 2 3 5 r 6008 5 r 6 9 3 8

The resultsare plotted in Fig. 1.4 along with the exact solution.We can seethat the nu- merical 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 discrepan- ciesis 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 num- bers of calculationscan be performedeasily.Thus, you can accuratelymodel the velocity of thejumper without having (^) to solve the differentialequationexactly.

t t S 0 2 4 6 B r 0 l 2 oo

r 2 MATHEMATICALMODELINGNUMERICALMETHODS,AND PROBLEMSOLVING

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 nurn- ber of computations.Thus, we seethat thereis a trade-offbetweenaccuracyand computa- tional effort. Such trade-offsfigure prominently in numerical methodsand constitutean importantthemeof this book.

1.2 CONSERVATIONL/AWSlN ENGINEERINGAND SCIENCE Aside from Newton's secondlaw. there are other major organizingprinciples in science and engineering.Among the most importantof theseare the conserv,ationlan: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 Change:^ increases-^ decreases ( 1. r 4 ) 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. r 5 )

Thns, ifno changeoccurs,the increasesanddecreasesnrustbe in balance.This case,which is also given a specialnarne-the stea(ly-statecalculation-has many applicationsin engi- neeringand science.For example,fbr steady-stateincompressiblefluid flow in pipes,the 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-statecondition would correspondto the casewhere the net lbrce was zeroor [Eq. (1.8)^ with du (^) ldt :^ 0l l 1 l $ : f

6 4 1 1 - ( 1. r 6 )

Thus. at steadystate,the downwardand upwardfbrcesarein balanceand Eq. ( 1.| 6) can be solvedfor the terminal velocity

AlthoughEqs.(1.14)and (1.15)might appeartriviallysimple,theyembodythe two funda- mentalways that conservationlaws areemployedin engineeringand science.As such,they will tbnn an importantpart of our et-fortsin subsequentchaptersto illustratethe connection betweennumericalmethodsand engineeringand science.