Jeffrey J. Heys-Chemical and Biomedical Engineering Calculations Using Python-Wiley (2017) — копия


Чтобы посмотреть этот PDF файл с форматированием и разметкой, скачайте его и откройте на своем компьютере.
ChemicalandBiomedicalEngineering
CalculationsUsingPython
ChemicalandBiomedicalEngineering
CalculationsUsingPython
JereyJ.Heys
MontanaStateUniversity
Bozeman,Montana,USA
iseditionrstpublished2017
2017JohnWiley&Sons,Inc
Allrightsreserved.Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,or
transmitted,inanyformorbyanymeans,electronic,mechanical,photocopying,recordingor
otherwise,exceptaspermittedbylaw.Adviceonhowtoobtainpermissiontoreusematerialfrom
thistitleisavailableathttp://www.wiley.com/go/permissions.
erightofJereyJ.Heystobeidentiedastheauthorofthisworkhasbeenassertedin
accordancewithlaw.
RegisteredOce
JohnWiley&Sons,Inc.,111RiverStreet,Hoboken,NJ07030,USA
EditorialOce
111RiverStreet,Hoboken,NJ07030,USA
Fordetailsofourglobaleditorialoces,customerservices,andmoreinformationaboutWiley
productsvisitusatwww.wiley.com.
Wileyalsopublishesitsbooksinavarietyofelectronicformatsandbyprint-on-demand.Some
contentthatappearsinstandardprintversionsofthisbookmaynotbeavailableinotherformats.
LimitofLiability/DisclaimerofWarranty
epublisherandtheauthorsmakenorepresentationsorwarrantieswithrespecttotheaccuracy
orcompletenessofthecontentsofthisworkandspecicallydisclaimallwarranties,including
withoutlimitationanyimpliedwarrantiesoftnessforaparticularpurpose.isworkissold
withtheunderstandingthatthepublisherisnotengagedinrenderingprofessionalservices.e
adviceandstrategiescontainedhereinmaynotbesuitableforeverysituation.Inviewofongoing
research,equipmentmodications,changesingovernmentalregulations,andtheconstantow
ofinformationrelatingtotheuseofexperimentalreagents,equipment,anddevices,thereaderis
urgedtoreviewandevaluatetheinformationprovidedinthepackageinsertorinstructionsfor
eachchemical,pieceofequipment,reagent,ordevicefor,amongotherthings,anychangesinthe
instructionsorindicationofusageandforaddedwarningsandprecautions.efactthatan
organizationorwebsiteisreferredtointhisworkasacitationand/orpotentialsourceoffurther
informationdoesnotmeanthattheauthororthepublisherendorsestheinformationthe
organizationorwebsitemayprovideorrecommendationsitmaymake.Further,readersshould
beawarethatwebsiteslistedinthisworkmayhavechangedordisappearedbetweenwhenthis
workswaswrittenandwhenitisread.Nowarrantymaybecreatedorextendedbyany
promotionalstatementsforthiswork.Neitherthepublishernortheauthorshallbeliableforany
damagesarisingherefrom.
LibraryofCongressCataloging-in-PublicationData
Names:Heys,JereyJ.,1974-author.
Title:ChemicalandBiomedicalEngineeringCalculationsUsingPython
/JereyJ.Heys.
Description:Hoboken,NJ:JohnWiley&Sons,2017.|Includes
bibliographicalreferencesandindex.
Identiers:LCCN2016039763|ISBN9781119267065(cloth)|ISBN9781119267072
(epub)
Subjects:LCSH:Engineeringmathematics.|Python(Computerprogramlanguage)
Classication:LCCTA330.H492017|DDC620.00285/5133dc23LCrecordavailableat
https://lccn.loc.gov/2016039763
CoverdesignbyWiley
Coverimage:lvcandy/GettyImages,Inc.
Setin10/12pt,WarnockbySPiGlobal,Chennai,India
PrintedinUnitedStates
10987654321
Contents
Preface
AbouttheCompanionWebsite
1ProblemSolvinginEngineering
1.1EquationIdenticationandCategorization
1.1.1AlgebraicversusDierentialEquations
1.1.2LinearversusNonlinearEquations
1.1.3OrdinaryversusPartialDierentialEquations
1.1.4InterpolationversusRegression
Problems
AdditionalResources
References
2ProgrammingwithPython
2.1WhyPython?
2.1.1CompiledversusInterpretedComputerLanguages
2.1.2ANoteonPythonVersions
2.2GettingPython
2.2.1InstallationofPython
2.2.2AlternativetoInstallation:SageMathCloud
2.3PythonVariablesandOperators
2.3.1UpdatingVariables
2.3.2Containers
2.4ExternalLibraries
2.4.1FindingDocumentation
Problems
AdditionalResources
References
3ProgrammingBasics
3.1ComparatorsandConditionals
3.2IteratorsandLoops
Contents
3.2.1IndentationStyle
3.3Functions
3.3.1PizzaExample
3.3.2PrintFunction
3.4DebuggingorFixingErrors
3.5Top10
PythonErrorMessages
Problems
AdditionalResources
References
4ExternalLibrariesforEngineering
4.1NumpyLibrary
4.1.1ArrayandVectorCreation
4.1.2ArrayOperations
4.1.3GettingHelpingwithNumpy
4.1.4NumpyMathematicalFunctions
4.1.5RandomVectorswithNumpy
4.1.6SortingandSearching
4.1.7Polynomials
4.1.8LoadingandSavingArrays
4.2MatplotlibLibrary
4.3Application:GillespieAlgorithm
Problems
AdditionalResources
References
5SymbolicMathematics
5.1Introduction
5.2SymbolicMathematicsPackages
5.3AnIntroductiontoSymPy
5.3.1MultipleEquations
5.4FactoringandExpandingFunctions
5.4.1EquilibriumKineticsExample
5.4.2PartialFractionDecomposition
5.5DerivativesandIntegrals
5.5.1ReactionExample
5.5.2SymbolicIntegration
5.5.3ReactorSizingExample
5.6Cryptography
Problems
References
6LinearSystems
6.1ExampleProblem
6.2ADirectSolutionMethod
Contents
6.2.1DistillationExample
6.2.2BloodFlowNetworkExample
6.2.3ComputationalCost
6.3IterativeSolutionMethods
6.3.1VectorNorms
6.3.2JacobiIteration
6.3.3GaussSeidelIteration
6.3.4RelaxationMethods
6.3.5ConvergenceofIterativeMethods
Problems
References
7Regression
7.1Motivation
7.2FittingVaporPressureData
7.3LinearRegression
7.3.1AlternativeDerivationoftheNormalEquations
7.4NonlinearRegression
7.4.1LunarDisintegration
7.5MultivariableRegression
7.5.1MachineLearning
Problems
References
8NonlinearEquations
8.1Introduction
8.2BisectionMethod
8.3NewtonsMethod
8.4BroydensMethod
8.5MultipleNonlinearEquations
8.5.1ePointInsideaSquare
Problems
9Statistics
9.1Introduction
9.2ReadingDatafromaFile
9.2.1NumpyLibrary
9.2.2CVSLibrary
9.2.3Pandas
9.2.4ParsinganArray
9.3StatisticalAnalysis
9.4AdvancedLinearRegression
9.5U.S.ElectricalRatesExample
Problems
References
Contents
10NumericalDierentiationandIntegration
10.1Introduction
10.2NumericalDierentiation
10.2.1FirstDerivativeApproximation
10.2.2SecondDerivativeApproximation
10.2.3ScipyDerivativeApproximation
10.3NumericalIntegration
10.3.1TrapezoidRule
10.3.2NumericalIntegrationUsingScipy
10.3.3ErrorFunction
Problems
References
11InitialValueProblems
11.1Introduction
11.2BiochemicalReactors
11.3ForwardEuler
11.4ModiedEulerMethod
11.5SystemsofEquations
11.5.1eLorenzSystemandChaoticSolutions
11.5.2Second-OrderInitialValueProblems
11.6StiDierentialEquations
Problems
References
12BoundaryValueProblems
12.1Introduction
12.2ShootingMethod
12.3FiniteDierenceMethod
12.3.1ReactionsinSphericalCatalysts
Problems
References
13PartialDierentialEquations
13.1FiniteDierenceMethodforSteady-State
PDEs
13.1.1Setup
13.1.2MatrixAssembly
13.1.3SolvingandPlotting
13.2Convection
13.3FiniteDierenceMethodforTransientPDEs
Problems
References
Contents
Preface
duringthetypicalundergraduateeducation.eironyofthissituationisthat
studentsoftendonotunderstandthecalculationbeingperformedbythe
softwaretheydonotknowthelimitationsofthemathematicalmodels,they
donotknowtheexpectedaccuracyoftheapproximatesolution,andthey
donotalwayshavetheintuitionnecessarytorecognizeahighlyincorrect
result.Anotherlossassociatedwiththeriseofspecializedsoftwaretoolsfor
engineersisthatitisoftenverydiculttondacomputationaltoolforanew
problem.esoftwareoftenworkswellforthelimitedrangeofproblemsfor
whichitwasdesigned,but,ifanengineerwishestoanalyzesomethingnewor
includesomechangethattakestheproblemjustbeyondtherangeofproblems
forwhichthesoftwarewasdesign,thatengineerisoftenoutofluckbecause
nocomputationaltoolisavailabletohelp.
Idonotadvocateabandoningmodernengineeringsoftware.Idonot
advocatereturningtotheuseofcustomFORTRANcomputercodesforevery
problem.Idoadvocatethatengineeringstudentsgetsomeexperiencewriting
shortcomputerprograms.isexperienceteachesonetothinkpreciselyas
computersarenotoriouslyunforgivingwhenwemakemistakesinourlogic.It
teachesonetodecomposeacomplexprocessdownintosmall,individualsteps.
isexperienceteachesonetodevelopauniquesolutionforanewproblem
thatisnothandledwellbyexistingsoftware.Finally,theexperienceofcreating
acomputeralgorithmhelpstodeveloparecognitionofwhencomputations
arelikelytobereliableandwhentheyarenotwhenthecomputational
solutionissucientlyaccurateandwhenitisnot.
egoalofthisbookistoprovidethereaderwithanunderstandingof
standardcomputationalmethodsforapproximatingthesolutiontocommon
problemsinChemicalandBiomedicalEngineering.ebookdoesnothavea
comprehensivecoverageofcomputationalmethods,butitisinsteadintended
toprovidetheintroductorycoveragenecessarytounderstandthemostcom-
monlyusedalgorithms.ecomputerlanguageusedtoexplorethedierent
computationalmethodsisPython.eadvantagesofusingPythoninclude
itswideandgrowingpopularity,largelibraryofexistingalgorithms,andits
licensingasfree,opensourcesoftware.enalandpossiblygreatestadvan-
tageinusingPythonisthatitiseasytolearntowritegeneralcomputational
algorithmsandmorespecializednumericalalgorithmsarealsoeasytowrite,
thankstotheNumPyandSciPylibraries.Bytheendofthisbook,thereader
shouldhaveasolidunderstandingofhowtowriteandusecomputational
algorithmsinPythontosolvecommonmathematicalproblemsinChemical
andBiomedicalEngineering.
ecoursethatmotivatedthecreationofthistextbookisonesemester
ofapproximately15weeks.Itismybeliefthatmostofthismaterialcanbe
coveredinthatlengthoftime.Eachchapterinthetextbookcoversadierent
topicandthebookwasconstructedsothatthematerialinthatchaptercould
becoveredinapproximately1week.ereare,ofcourse,someexceptions.
elargenumberoftopicsandshortamountoftimeassociatedwithasingle
Preface
semestermayencourageinstructorsusingthisbooktoconsideraslightly
dierentformatthanthetraditionallectureformat.Forexample,iftwoclass
timesperweekareavailable,aninstructormaywanttoconsiderrequiring
studentstoreadthebookorwatchanonlinelecturethatpresentsthematerial
tobecoveredbeforecomingtotherstclassmeetingtimeeachweek.e
twoclassperiodscouldthenbeusedtocoverexampleproblems(therst
classeachweek)andaworkingclasscouldbeusedforthesecondclass
meetingoftheweek.Whenstudentsaretryingtocompletethehomework,
theyoftenneedsupporttoovercomeadiculterrormessageorunexpected
andunphysicalnumericalanswerfromthecomputer,andallowingstudents
toworkonproblemsforoneclasstimeperweekisoftenverybenecial.
Suggestedhomeworkproblemsareincludedattheendofeachchapter.
Manyofthehomeworkproblemsarewrittensothatthepersonansweringthe
problemmustrespondtoarequestfromarealorhypotheticalorganization
suchasacompanyorgovernmentagency.eauthorofthisbooktypically
assignsoneortwoproblemsperweekandrequiresstudentstosubmittheir
solutionsintheformofamemototheorganizationthatposedtheproblem.
ememotypicallyisabout1pageoftextplus13guresforatotalof2or3
pagesforthemainbodyofthememo,andthePythoncodeisincludedbythe
studentinanappendixwiththememo.Requiringstudentstopracticetechni-
calwritingisabenetofusingthisapproach,andmanystudentsaremotivated
whentheproblemshavemoreofarealworldavorandarelessabstract.
Inclosing,Iwouldliketooermysincerestthanksandgratitudetothe
manyunnamedindividualsthathavecontributedtobuildingPythonand
makingscienticcomputingusingPythonsuchawonderfulreality.Tome,it
isreallyhumblingandencouragingtoseethegreatworkthattheseindividuals
havefreelygiventotheworld.Iwouldliketosingleouttwoindividualsby
namebecauseofthetransformativeimpactoftheirworkwithouttheir
work,IwouldneverhavestartedusingPythonasextensivelyasIdo,andthis
bookwouldneverhavebeenwritten.erstindividualisTravisOliphant,
theprimarycreatorofNumPyandthefounderofContinuumAnalytics,
whichproducestheAnacondaPythonDistribution.esecondindividualis
FernandoPerez,aphysicist,creatorofiPython,and,mostimportantlytome,
thepersonthatcameintomyoceattheUniversityofColoradoatBoulderand
toldmethatIshouldtrylearningPythonbecauseitmadeprogrammingfun!
Bozeman,Montana
JereyJ.Heys
July15,2016
AbouttheCompanionWebsite
isbookisaccompaniedbyacompanionwebsite:
www.wiley.com/go/heys/engineeringcalculations_python
ewebsiteincludes:
PythonComputerCodes.
1ProblemSolvinginEngineering
statement
diagram
equations
Figure1.1
Engineeringproblem-solvingprocess.
typicallynottakenalltherequiredmathematicscourses,and,asaresult,itis
diculttoteachacomputationalapproachtosolvingadierentialequation
whenastudentisnotyetfamiliarwithdierentialequationsortechniques
forsolvingthem.eseconddisadvantageisthatthestudenthasnottaken
coursesonseparations,kinetics,transport,andsooninwhichtheylearnto
deriveoridentifytheappropriatemathematicalequation(s)fortheirparticular
problem.Itis,ofcourse,diculttoteachacomputationalapproachto
solvinganequationwhentheimportanceorrelevanceofthatequationisnot
known.
Athirdapproachforaddressingthisdilemmaistosimplynotteacha
stand-alonecomputationalmethodscourseandinsteadcovertherelevant
computationalapproachesastheyareneededineachindividualcourse.We
willcontinueourlistingofthetoptwochallengesandidentifytwopotential
dicultieswiththisapproach.First,insteadoflearningandbecomingcom-
fortablewithtwoorthreecomputationaltools(i.e.,mathematicalsoftware
packages),studentsunderthisformatoftenneedtolearn4or5computational
toolsbecauseeveryoneoftheirinstructorsprefersadierenttool,andthe
studentsneverreallybecomeprocientwithanysingletool.esecond
dicultyisthatthereareafewimportantconceptsthatplayaroleinmany
ofthevariouscomputationalmethods,forexample,roundingerror,logical
operators,andaccuracy,thatmayneverbetaughtifthereisnotasinglecourse
focusedoncomputationalmethods.
istextbook,andthecoursethatitwasoriginallywrittentosupport,is
focusedonthesecondapproachacoursethatappearsintherstyearorearly
inthesecondyearofanengineeringcurriculum.emainreasonforadopting
thisapproachissimplythebeliefthatitiscriticalforstudentstounderstand
boththepotentialpowerandexibilityofcomputationalmethodsandalsothe
importantlimitationsofthesemethodsbeforeusingthemtosolveproblemsin
engineering.Forastudenttouseacomputationaltoolinacourseandblindly
trustthattoolbecausetheydonotunderstandthealgorithmsbehindthetool
isprobablymoredestructivethanneverlearningthetoolatall.Further,tolimit
astudenttoonlyproblemsthatcanbesolvedwithpaperandpencilformostof
theirundergraduateeducationissimilarlyunacceptable.Addressingthelim-
itationsassociatedwithteachingcomputationalmethodsbeforemostofthe
fundamentalengineeringandsomemathematicscoursesisdicult.ebasic
strategyemployedbythisbookistoteachstudentstorecognizethetypeof
1ProblemSolvinginEngineering
mathematicalequationtheyneedtosolve,and,oncetheyknowthetypeof
equation,theycantakeadvantageoftheappropriatecomputationalapproach
thatispresentedhere(or,morelikely,referbacktothisbookfortheappropriate
algorithmfortheirparticularequation).
ereisasecond,andpossiblymoreimportant,reasonforlearningthis
materialearlyintheengineeringeducationprocess.Itisrelatedtothefactthat
oneofthemostdicultskillsformanyscience,engineering,andmathematics
studentstomasteristheabilitytocombineanumberofsmall,simplepieces
togetherintoamorecomplexframework.Inmostscience,engineering,and
mathematicscoursesinhighschoolandearlyincollege,studentslearnto
ndtherightequationtosolvethequestiontheyareaskedtoanswer.Most
problemscanbecompletedin
oneortwo
steps.Problemsinlatercourses,on
theotherhand,canoftenrequire
ormorestepsandcanrequiremultiple
pagesofequationsandmathematicstosolve.istransitionfromsmall
problemsthatonlyrequireafewlinestolargeproblemsthatrequireafew
pagescanbeverychallengingformanyscience,engineering,andmathematics
students.Ibelievethatprogrammingingeneral,andnumericalcomputations,
inparticular,canbeagreatwaytodeveloptheskillsassociatedwithsolving
largerproblems.Programmingrequiresonetocombineanumberofsimple
logicalcommandsandvariablestogetherintoamorecomplexframework.
Programmingdevelopsthepartsofourbrainsthatallowustosynthesizea
numberofsmallerpiecesintoamuchlargerwhole.Agoodanalogyisbuilding
somethingcomplex(e.g.,theDeathStar)withLEGObricks.isprocess
requiresonetoproperlyandcarefullycombineanumberofsimplepiecesinto
amuchlargerstructure.eentireprocessrequiresonetosimultaneously
thinkonboththelargescale(Whatismydesignobjective?)andthesmall
scale(Willthesetwopiecesstayconnected?Aretheycompatible?).isskill
isnecessaryforbothprogrammingandengineering.Itisaskillthatalmost
everyoneiscapableofdeveloping,butittakespracticeso,wemightaswell
startearly!
istextbookadvocatesthatstudentsdevelopthefollowingskills:(1)rec-
1ProblemSolvinginEngineering
1.1EquationIdenticationandCategorization
Weidentiedtwocategoriesofskillsthatwewishtodevelopthroughoutthis
book:(1)recognizingthetypeofmathematicalequation(s)and(2)selecting
andimplementinganappropriatecomputationalmethod.erstskillwillbe
coveredinthischapterandthentheremainderofthebookisfordeveloping
thesecondsetofskills.
1.1.1AlgebraicversusDierentialEquations
edistinctionbetweenalgebraicanddierentialequationsistriviala
dierentialequationisarelationshipbetweenthederivativesofavariableand
somefunction.Dierentialequationsdescribedtherateofchangeofavariable;
typicallytherateofchangewithrespecttospaceortime.Equationscanhave
bothindependentanddependentvariables.Itisusuallysimplesttoidentifythe
dependentvariablesbecausetheirvaluedependsonthevalueofanothervari-
able.Forexample,inboth
isthedependent
variablebecauseitsvaluedependsonthevalueof
istheindependent
variable.erecanbemultipleindependentvariables,forexample,multiple
spatialdimensionsandtime,andthevalueofdependentvariablemaydepended
onthevalueofallindependentvariables.edensityofair,forexample,varies
withlocation:latitude,longitude,andelevationabovesealevel,aswellastime.
erefore,ifwehaveanequationthatdescribesthedensityofairasafunction
oflocationandtime,then,inthatequation,densityisthedependentvariable
andlocationandtimearetheindependentvariables.Similarly,theidealgas
lawcanbeusedtocalculatethedensityofair:
.Forthisequation,
isafunctionoftemperatureandpressure,so
isthedependentvariableand
aretheindependentvariables.Alternatively,thisequationcouldbe
seensuchthatpressure,
,isthedependentvariablethatdependsondensity,
,andtemperature,
,thatis,
Fordierentialequations,therearethreedierentnotationstylesthatare
commonlyusedforderivatives.
Leibniznotation
ederivativeofthefunction,
,withrespectto
writtenas
andthesecondderivativeiswritten:
partial
derivativeof
withrespectto
x
.
1.1EquationIdenticationandCategorization
Lagrangenotation
ederivativeofthefunction,
,withrespectto
written:
andthesecondderivativeiswritten:
enotationisnoteasilyextendedtopartialderivativesandthereisno
universalstandard,butonestylethatisusedistoswitchfromtheprime
mark,
,toasubscriptsothatthepartialderivativeof
withrespectto
Eulernotation
ederivativeofthefunction,
,withrespectto
iswritten:
andthesecondderivativeiswritten:
partial
derivativeof
withrespectto
Insummary,dierentialequationshaveatleastonederivativeandalgebraic
equationsdonot.epresenceofaderivativehasasignicantimpactonthe
computationalmethodusedforsolvingtheproblemofinterest.
1.1.2LinearversusNonlinearEquations
Alinearfunction,
,isonethatsatisesbothofthefollowingproperties:
additivity:
homogeneity:
Inpractice,thismeansthatthedependentvariablescannotappearin
polynomialsofdegreetwoorhigher(i.e.,
isnonlinearbecause
),innonlinearargumentswithinthefunction(i.e.,
isnonlinearbecausesin
),oras
productsofeachother(i.e.,
isnonlinear).
Foralgebraicequations,itistypicallystraightforwardtosolvelinearsystems
ofequations,evenverylargesystemsconsistingofmillionsofequationsand
millionsofunknowns.Twodierentmethodsforsolvinglinearsystemsof
equationswillbecoveredinChapter6.Nonlinearalgebraicequationscan
sometimesbesolvedexactlyusingtechniqueslearnedinalgebraorusing
symbolicmathematicsalgorithms,especiallywhenthereisonlyasingle
equation.However,ifwehavemorethanonenonlinearequationorevena
single,particularlycomplexnonlinearalgebraicequation(orifwearesimply
1ProblemSolvinginEngineering
feelingalittlelazy),wemayneedtotakeadvantageofacomputational
techniquetotryandndanapproximatesolution.Algorithmsforsolving
nonlinearalgebraicequationsaredescribedinChapter8.
Itisimportanttonotethatthedistinctionbetweenlinearandnonlinear
equationscanalsobeextendedtodierentialequationsandallofthe
sameprinciplesapply.Forexample,
4
c
2sin
arelinear
isnonlinear.Insomecases,thenonlinearitywillnotsignicantly
increasethecomputationalchallenge,but,inothercasesliketheNavierStokes
equations,thenonlinearitycansignicantlyincreasethedicultyinobtaining
evenanapproximatesolution.
LinearversusNonlinearExamples
Linear:
singlelinearequation:
linearsystemofequations:
Nonlinear:
singlenonlinearequation:

singlenonlinearequation:
nonlinearsystemofequations:
nonlinearsystemofequations:
log
1.1.3OrdinaryversusPartialDierentialEquations
Anordinarydierentialequation(ODE)hasasingleindependentvariable.For
example,ifadierentialequationonlyhasderivativeswithrespecttotime,
orasinglespatialdimension,
,itisanODE.Adierentialequationwithtwo
ormoreindependentvariablesisaPDE.efollowingareexamplesofODEs.
d
(linear,second-orderODE)
Ifyouhavenottakenadierentialequationscourse,thisequationmaylook
alittleintimidatingorconfusing.Tosolvethisequation,weneedtonda
1.1EquationIdenticationandCategorization
function
wheretherstderivativeofthefunction,multipliedby
,plusthe
secondderivativeofthefunctionisequaltosin
.Ifthatsoundsdicult,do
notworry,bytheendofthistextbook,youwillknowhowtogetanapproximate
solution,thatis,anumericalapproximationofthefunction
.Itisalsoimpor-
tanttoemphasizethatmultiplyingthedependentvariable
bytheindependent
variable
didnotmaketheequationnonlinear.Anonlinearityonlyarisesif,for
example,
ismultipliedbyitself.
3cos
(nonlinear,rst-orderODE)
Again,ifyouhavenothadadierentialequationscourse,solvingthis
equationrequiresndingafunction
thathasaderivativeequalto
plus3cos
.Donotworryifthatmakesyourheadspin,wewillalsocoverthe
solutionofthisclassofproblems.
SomeexamplesofPDEsareincludedbelow.
t
=


(linear,second-orderPDE)
isisanequationthatdescribesunsteady,conductiveheattransportinone
spatialdimension.Youcouldusethisequationtodescribe,forexample,the
warmingofthegroundwhenthesuncomesupinthemorning,amongmany
otherexamples.Solvingthisequationrequiresndingafunction
ofboth
andspace
wheretherstderivativewithrespecttotimeisequalto
timesthesecondderivativewithrespecttospace.
x
+

m
0(nonlinear,rst-orderPDE)
Bynowitisprobablyobviousthatthestandardmathematicalconventionis
touse
forderivativesinaPDEwhileODEsuse
.eorderoftheequation
isdeterminedbytheorderofthehighestderivative.
SolvingaDierentialEquation
Eventhoughyoumaynothavetakenadierentialequationscourse,youmight
beabletosolveasimpliedversionoftherstODEexample.Trytosolve
Noticethatwehaveeliminatedthediculttermwith
multipliedbytherst
derivative.Letusstartbyintegratingbothsidesoftheequationwithrespect

t
)
(Continued)
1ProblemSolvinginEngineering
SolvingaDierentialEquation(Continued)
cos
Thetwoconstantsofintegrationcansimplybecombinedintoasingle
constant,
,whichcanbeplacedontheright-handsidegiving:
cos
Itisalwaysagoodideatocheckthesolutiontoyourproblembysubstituting
backintotheoriginaldierentialequationandcheckingtomakesurethat
theleftside(i.e.,thesecondderivateof
)isequaltotheright-handside.
1.1.4InterpolationversusRegression
Withinengineering,itisoftennecessarytoobtainanequation,usuallya
polynomialequation,thattsagivensetofdata.Ifwewantanequation
thatexactlymatchesthedata,thenwemustinterpolatethedatasothatwe
obtainafunction(e.g.,apolynomial)thathasthesamevalueasthedatafor
agivenvalueoftheindependentvariable(Figure1.2).Inordertodetermine
aninterpolant,thenumberofadjustableparametersthatwedeterminein
theequationmustequalthenumberofdatapoints.Forexample,ifwewant
tointerpolatethreedatapoints,wemustuseanequationthathasthree
adjustableparameters,suchasaquadraticpolynomial,
Inpractice,itisactuallyprettyrarethatwewanttoexactlyinterpolatea
givensetofdatabecausewehopefullyhavealargeamountofdata(andwe
donotwanttouseaveryhigh-orderpolynomial)andthatdatacontainssome
amountoferror.Inmostcases,wewanttoapproximatelytourdatawithan
equationofsomeform(Figure1.3).Inordertodothis,wemustrstdecide
1.1EquationIdenticationandCategorization
Figure1.2
x
(a)(b)
x
Figure1.3
1ProblemSolvinginEngineering
Problems
Determinethetype(linearornonlinear)ofalgebraicequationassuming
,and
areunknownvariables:
Determinethetype(linearornonlinear;ordinaryorPDE)ofdierential
equationassumingthat
,and
areindependentvariablesand
,and
areknownparameters:
(Newtonsrstlaw)
t


C
z
z

C
Ifyouwanttodeterminethepolynomialthatinterpolates6datapoints,
whatistheminimumorderpolynomialthatisrequired?Writethepoly-
nomialwith
astheindependentvariableand
astheunknown
coecients.
Youareaskedtouseregressiontodeterminethebestlinearpolynomialt
foragivensetofdata.Acolleagueencouragesyoutodeterminethebest
tbyminimizingthesumofthedistancebetweeneachpointandtheline
insteadofminimizingthesumofthe
square
ofthedistance,whichisthe
standardpractice.ecolleagueclaimsthatthiswillreducetheinuence
ofafewoutlyingdatapoints.Isthecolleaguecorrect?
Youhavebeenhiredtoproduceanexactreplacementpartforaclassic
Porschebecausethepartisnolongeravailable.Anotherengineercollects
precisemeasurementsofthelocationofanumberofpointsonthesurface
ofthepart.Youneedtoproduceanewpartwithcorrespondingpointsat
thesamelocations.Beforemachiningthenewpart,youneedtodevelop
acontinuousfunctionthattsthemeasurementpointsbecausethecon-
tinuousfunctionwillprovidearepresentationofthesurfaceconnecting
thepoints.Shouldyoudevelopthecontinuousfunctionusingregression
orinterpolationbetweenthepreciselymeasuredlocationsonthesurface
ofthepart?Why?
Whilestudyingaparticularsystem,youcollectsomedataonameasur-
ablevariable(
)versusanadjustablevariable(
).Yournexttaskistouse
References
regressiontoapproximatelytthedatawithacontinuousmathemati-
calfunction.Mostengineerswouldstartbytryingtotthedatawitha
polynomial.Youarenotlikemostengineersbecauseyouwiselystartby
plottingthedata.Whileexaminingtheplot,younoticethatthedatahasa
patternthatisrepeatedastheadjustablevariableiscontinuouslychanged.
emeasuredvariableincreasesanddecreasesregularlyastheadjustable
variableisincreased.Shouldyoutthisdatawithapolynomial?Ifso,what
orderpolynomial?Ifnot,whatfunction(s)wouldyouuseinstead?
AdditionalResources
Anunderstandingofhowtosolvedierentialequationproblemsisnotrequired
forunderstandingthematerialinthisbook.However,anabilitytoclassifyor
recognizethetypeofequationthatoneistryingtosolveisrequired.Most
dierentialequationtextbooksincludeacomprehensivesetofdenitionsthat
enabletheclassicationofmathematicalequations.Somepopulardierential
equationtextbooksforengineersare:
DierentialEquationsforEngineersandScientistsbyengelandPalm[1]
AdvancedEngineeringMathematicsbyZillandCullen[2]
AdvancedEngineeringMathematicsbyKreyszig[3]
andahelpfulresourcefordataplottingandregressionusingMicrosoftExcelis:
EngineeringwithExcelbyLarsen[4].
References
engel,Y.andPalm,W.III(2013)
DierentialEquationsforEngineersand
Scientists
,McGraw-Hill,NewYork,NY,1stedn.
Zill,D.andCullen,M.(2006)
AdvancedEngineeringMathematics
,Jonesand
Barlett,Sudbury,MA,3rdedn.
Kreyszig,E.(2011)
AdvancedEngineeringMathematics
,JohnWileyandSons,
Inc.,Hoboken,NJ,10thedn.
Larsen,R.W.(2009)
EngineeringwithExcel
,PearsonPrenticeHall,Upper
SaddleRiver,NJ,3rdedn.
2.1WhyPython?
tousethesoftware.Italsomeansthatthestudentisunlikelytohaveaccessto
thesoftwareaftertheygraduate.
enaloptionforthecomputationalsolutionofengineeringproblems
istosimplywriteyourowncomputercodeinarelativelylow-levellanguage
suchasFORTRANorC
.Unfortunately,thisoptionrequiressignicant
specializedknowledgeknowledgethatisrarelyretainedbeyondthecourse
inwhichitistaught.Writinglow-levelcomputercodecanalsobeavery
frustratingexperiencewhensubtleerrorsinthecodearediculttoidentify
duetoobscureerrormessages.eresultisthatstudentsspendmostof
theirtimelookingforerrorsinthecomputercodeinsteadoflearningabout
computationsandalgorithmdevelopment.
ereisnotaperfectsolutiontothedilemmaofselectinganoptimal
computerenvironmentforlearningcomputationaltechniquesforsolving
engineeringproblems.However,thePythonprogramminglanguagehasmany
advantagesthatmakeittheplatformofchoicehere.eseadvantagesinclude
thefollowing:
1)Itisfreelyavailableandrunsonmostmajorcomputerplatformsincluding
Windows,MacOS,andLinux.
2)Ithasatremendousnumberofadditionallibrariesthatarealsofreeandadd
computationalmathematicscapabilities.Forexample,theNumpylibrary
providesPythonwithcapabilitiesthataresimilartothoseofMATLAB.
3)Itisaninterpretedlanguage(denedbelow)andiseasierandfasterfor
developingnewalgorithmsthancompiledlanguages.
4)Manylibrariesofpreviouslycompiledalgorithmscanbeimportedinto
Python,whichallowsforveryfastandecientcomputations.
5)Itisworthrepeatingitisfree!
2.1.1CompiledversusInterpretedComputerLanguages
ersthigh-levelprogramminglanguagesthatweredeveloped,suchas
FORTRANorC,werecompiledlanguages.ismeantthattheprogrammer
wouldtypesourcecodeintothecomputer,thiscodewascompiledinto
assemblercode,andthiswasultimatelylinkedtoproduceanalexecutable
le(Figure2.1).eadvantageofthisapproachisthattheexecutablethat
wasproducedwasrelativelyoptimizedandecientfortheplatformonwhich
itwasbuilt.Eventoday,mostnumericalsoftwarethatrequiressignicant
computations,forexample,meteorologicalsoftware,iswritteninacompiled
language.edisadvantageofthisapproachisthatsignicantexpertiseand
trainingarerequiredtowritecomputerprogramsinacompiledlanguage,
identifyingerrorsinthesourcecodeisoftenaverydicultandtimeconsum-
ingprocess,andtheresultingprogramcanonlyberunontheplatformor
operatingsystemforwhichitiscompiled.
esedisadvantagesassociatedwithcompiledprogramminglanguages
canlargelybeaddressedthroughtheuseofinterpretedlanguages.Common
2ProgrammingwithPython
code
(a)
(b)
code
CompilingLinking
code
representation
on many platforms)
(runtime)
Figure2.1
Inparticular,programswrittenforthePython3.xserieswouldnormallynot
runonthePython2.6andearlierseriesofvirtualmachines,andexisting
programswrittenforPython2.xvirtualmachineswouldnotrunonPython
3.xseriesvirtualmachines.Probably,thebiggestchangeimpactingthePython
codesinthisbookhasbeentotheprintfunctionnotation.InPython2.6and
earlierversions,theformatwas
HelloWorld"
andforPython3.0andlaterversions,theformatwas
(HelloWorld")
oseparenthesesmaylooklikeasmallchange,butthenewformatisnot
compatiblewithPython2.6andearlierversions,andtheoldformatisnotcom-
patiblewithPython3.0andlaterversions.Interestingly,Python2.7supports
bothversions.Itshouldalsobenotedthattherewereother,moresignicant
changesbeyondtheprintfunctionwhenthechangewasmadetoPython3,
butthosechangesrarelyimpactthecodesandtypesofalgorithmswrittenin
thisbook.
Asof2015,mostnumericalpythonlibrariesareavailableforPython2.7or
Python3.xvirtualmachines.eexamplesinthistextbookwerewrittenfor
aPython3.xseriesvirtualmachinebuthavealsobeentestedonaPython2.7
seriesvirtualmachine.ItisinevitablethatallPythoncomputationswilleven-
tuallytransitiontoPython3.xorlatervirtualmachines.Inthemeantime,itis
importanttorecognizetheversionofPythonthatyouareusingandselectthe
2ProgrammingwithPython
Matplotlib(www.matplotlib.org)providesthepyplotandpylabplotting
libraries
SymPy(www.sympy.org)symbolicmathematicslibrary(optional,used
primarilyinChapter5)
Pandas(http://pandas.pydata.org/)easytousedatastructuresanddata
analysistoolsincludingdataimport(optional,usedprimarilyinChapter9)
Itisalsorecommendedthatanintegrateddevelopmentenvironment(IDE)
beusedtofacilitatethewritingofPythonSourcecode.Oneparticularlygood
IDEiscalledSpyder(https://pythonhosted.org/spyder/).Figure2.2showsthe
basiclayoutoftheSpyderIDEinterface.einputwindowontheleftsideof
theSpyderprogramwindowshowsthePythonsourcecodethatiscurrently
beingedited.ecodeinthesourcewindowcanbeexecutedorrunby
selecting
Runfromthe
RunmenuorsimplypressingF5onmostplatforms.
eupperright-handscreenusuallyshowsdocumentationwhenitisavailable
fordierentfunctionsincludedwithPythonorimportedlibraries.elower
high-handscreenshowsaPythonconsoleorPythonprompt,
.Basically,
thePythonpromptisanactivelyrunningPythonvirtualmachineanddierent
Figure2.2
AscreenshotoftheSpyderIDEforPythonprogrammingincludingsourcecode
windowontheleftsize,documentationwindowontheupperrightsideandPython
consoleforrapidtestingandexecutingthesourcecodeinthelowerrightside.
2.2.1InstallationofPython
ForcomputersrunningWindows,threegoodoptionsforinstallingPython
includethefollowing:
AnacondaScienticPython(store.continuum.io/cshop/anaconda/)
pythonxy(code.google.com/p/pythonxy)
winpython(winpython.sourceforge.net)
AllofthesepackagesincludePythonplusalltherequiredlibrariessuchas
numpyandscipyplustheyincludetheSpyderIDE.Asof2015,onlyAnaconda
ScienticPythonsupportedPython3.Presumably,theotheroptionswill
eventuallysupportPython3,butcareshouldbetakenwheninstallingPython
toselectthedesiredPythonversion3.xor2.x.
ForcomputersrunningMacOS,itiseasiesttoinstalltheAnacondaScientic
Pythonpackage(https://store.continuum.io/cshop/anaconda/).
ForcomputersrunningaDebian-basedversionofLinux,thefollowing
commandwillinstallallrequiredlibraries:
FirstPythonCommands!
OpenaPythonconsoleoropentheSpyderIDEandmovethecursordownto
thelowerrightcorner.AtthePythonprompt,
type,
('HelloWorld')
andtheconsoleshouldprintHelloWorldbacktothescreen.Notethatyoudo
nottypethe
promptasitshouldautomaticallyappearwithintheopen
console.IfyouareusinganiPythonconsole,thepromptwilllooklike:
In[
where#isaninteger,andtheprogramabovewouldbe:
In[1]:
('HelloWorld')
Ingeneral,theregularPythonprompt:
andtheiPythonprompt:
In[
givethesomebehavioralthoughtheiPythonpromptsupportsmore
commands.
(Continued)
2ProgrammingwithPython
FirstPythonCommands!(Continued)
2.2.2AlternativetoInstallation:SageMathCloud
IfaWindows,Mac,orLinuxcomputerisnotavailableforinstallingPython
andtheimportantscienticlibraries,mostofthematerialcoveredinthis
book,includingexamples,exercises,andproblems,canbecompletedusing
SageMathCloud.SageMathCloudisaweb-basedcomputingplatformfor
computationalmathematics,anditispartoftheSageproject.Basically,
theSageMathCloudprojecthasinstalledalargenumberofsoftwarepack-
ages,includingPythonandthelibrariesusedinthisbook,oncomputers
connectedtotheinternet,andthentheyprovidedaweb-basedinterfaceto
thissoftware.eresultisthatuserscanvisittheSageMathCloudwebsite,
cloud.sagemath.com,createanaccount,andthenstartwritingscientic
softwareinPython(orotherlanguagesincludingJulia,R,andOctave)within
thewebpage.ewebsitecanbeusedfromalmostanyweb-browser,including
smartphones,tablets,andchromebook(ChromeOS)computers.
eweb-basedinterfaceisbasedontheJupyterproject.Usersstartby
creatinganewproject,thencreatinganewJupyternotebook.Anexampleofa
JupyternotebookisshowninFigure2.3.Jupyternotebooksconsistofcellsthat
containoneormorelinesofPythoncode.ecodewithinacellisexecuted
bypressingShift-Enter,andoncethecodeisexecuted,theresultsandother
outputaredisplayedbelowthecellandstoredinmemory(moreprecisely,
allpythonobjectsareretainedandcanbeusedwhenexecutingothercells).
Overall,thestyleofJupyternotebooksissimilartoMathematicaNotebooks
forindividualsthatarefamiliarwiththatsoftware.
WithallthebenetsassociatedwithSageMathCloud,includingtheability
towriteandexecutescienticPythoncodefromanywhere,includingatablet,
onemightaskwhyPythonshouldeverbeinstalledonacomputer?Whynot
alwaysuseSageMathCloud?ereareafewreasons.First,SageMathCloud
requiresaninternetconnectionandifthatconnectionislost,workcanbe
lost.Second,attimesofheavyuse,theinternet-connectedcomputersthatare
2.3PythonVariablesandOperators
Figure2.3
AscreenshotoftheJupyternotebookonSageMathCloud.Twodierentcellsare
populatedwithPythoncode,andthecellsareexecutedusingShift-Enter.Theresultsof
codeexecutionareshownbeloweachcell.
actuallyrunningthePythoncodeforSageMathCloudcanbecomeslowupto
afactorof10slowerthanamodernlaptoprunningPython.Finally,andthisis
thebiggestreason,itcanbedicultorimpossibletouseaPythonlibrarythat
isnotalreadyonSageMathCloud.Forexample,theniteelementlibraryFEn-
iCSthatiscoveredinthelastchapterofthisbookisnotavailableonSageMath-
Cloud.Ifyouareusingyourowncomputer,youcaninstallityourself,but,if
youareusingaweb-basedinterfaceintosomeothercomputer,thatmaynotbe
possible.
2.3PythonVariablesandOperators
Programmingfrequentlyrequiresustoassignavariabletoaspecicpieceof
data(orsomethingmorecomplex).Forexample,typing:
a="hello"
intotheconsoleoraPythonscriptleresultsinthevariableabeing
assigned
tocharacterstringhello.eword
assigned
isemphasizedherebecause
itbetterreectstherolebeingplayedbytheequalsign.WheneverPython
codecontains
,theobjectontherightisbeingassignedtothevariable
ontheleft.
2ProgrammingwithPython
VariableAssignment
InPython(andmostotherprogramminglanguages)weshouldsee:
hello
hello
eroleoftheassignmentoperatormayseemobvious,butmanynovice
programmershavestruggledwhenthefollowingcodedidnotwork:
���a=4
���a=b
Traceback(mostrecentcalllast):
File&#xstdi;&#xn000;"stdin",line1,
NameError:name'b'
isnot
enoviceprogrammermaybelievethatthesecondline(a
b)willresult
inbbeingsetto4sinceawaspreviouslysetto4.iswillnothappen,
and,instead,wegetanerrorbecausetheresultofexecutingthecodeisthat
aisassignedtosomethingthatisnotdened(thevariablebhasnotbeen
assigned).NoticethattheendofthePythonerrormessageistellingusthe
problem.
SimultaneousAssignment
Pythonalsoallowsthesimultaneousassignmentofmultiplevariabletothe
samevalue.Forexample,
isthesameas
andboth
areassignedthevalueof
Pythonuses
strong-typing
forvariables,whichmeansthateveryvariableis
aspecictype,forexample,aninteger,oatingpointnumber,andcharacter.
ereisabuilt-infunctioninPythoncalled
type()
,whichwillreturnthetype
foragivenvariable.Insomecases,itispossibletoconvertfromonevariable
typeintoanothervariabletypeasisillustratedintheexamplebelowwherea
string(str)variableisconvertedintoaninteger(int).
2.3PythonVariablesandOperators
���a=5
str�
���c=
int�
float�
ePythoncodeabovewasenteredintoaPythonconsole.Ifyoutrythis
exampleforyourself,donottypethe
prompt,itshouldbepartofthe
console.eoutputfromtheprintstatementmightvaryslightlydepending
ontheoperatingsystem,Pythonversion,andtypeofPythonconsoleyouare
using,buttheresultsshouldcontainstr,int,andoat.Youcanalsoenter
theabovecodeintoatextleorscript.Forexample,youcouldenterthecode:
intothelefthalfoftheSpyderIDEwindow(oranyotherprogramthatcanedit
textles),savethele,andthenrunitthroughthePythonvirtualmachine.e
outputshouldbethesameasthepreviousseriesofcommandsattheconsole:
str,int,andoat.Examplesofbothtypesofcodeentry:attheconsoleorin
ascript,areshowninFigure2.4.
2.3.1UpdatingVariables
Whenwritingacomputerprogram,itisoftennecessarytoupdatethevalueof
avariable.Forexample,wemaywanttocountsomethingbyinitiallysettinga
variabletozero,andthenaddingonetothevalueofthevariableaswecount,for
example,thenumberofwordsinaparagraph.Inordertoaddonetoavariable,
wecouldtype:
���w=w+1
where
isthevariablethatholdsthenumberofwordsaswecount.e
lineofPythoncodeaboveisclearlycompletelyinvalidfromamathematical
standpoint.Itlookslikeanequationwhere
isequaltoitselfplusonea
mathematicallyunsolvableequation(unlesszeroequalsone!).However,it
isNOTamathematicalequation,itisNOTastatementofequality,itISan
assignment:
isassignedavalueofthepreviousvalueof
plusone.isline
shouldbeviewedas:
new
istypeofvariableupdatewhereavariablesvalueisupdatedbyadding
somethingtoitissocommoninprogramming,thataspecialnotationis
2ProgrammingwithPython
Figure2.4
MathematicalOperations
Nowthatweknowhowtoassignvariables,weareinpositiontoexploreoper-
atorslike
and*,whichallowustoeectivelyusePythonlikeacalculator.
Thefollowingexampleillustratessomefeaturesofoperators.
ThefollowingcanbetypedintothePythonconsole:
���a=4
���b=2
2.3PythonVariablesandOperators
multiplicationanddivision:
exponent,oordivision,andremainder:
2.3.2Containers
Itisoftenusefulinprogrammingtocollectmultipleobjectstogetherintoa
singlecontainerandassignthemtoavariable.Pythonincludesanumberof
dierenttypesofcontainersincludingtuples,lists,anddictionaries.efocus
hereisonnumericalcomputationsandthemostusefultypeofcontainerfor
thesealgorithmsisalistcontainer.InPython,alisthasoneormoreobjects
(usuallynumbersfornumericalcomputations)separatedbycommasandsur-
roundedbysquarebrackets.Listsmaybeheterogeneouscontainingdierent
objectstypes,butinpractice,mostlistsonlycontainonetypeofvariable.Lists
shouldremindusofvectors.econstructionoflistsisillustratedbelow.
���myList=[1,2,3]
[1,2,3]
���secondList=['a','b','c',1,2,3]
['a','b','c',1,2,3]
etwolistscreatedabovewerestoredintwodierentvariables,myListand
secondList.Itiscommontostorelistsinvariables.Weoftenwishtomodify
liststhathavealreadybeencreated.Someexamplesoflistmodicationare
shownbelow.
���vec1=[2,3,5]
���vec2=[24,2,10]
���vec1.append([8,2])
[2,3,5,[8,2]]
2ProgrammingwithPython
���vec1[3]=87
[2,3,5,87]
���vec2.extend([9,3])
[24,2,10,9,3]
[2,3,5,87,24,2,10]
Traceback(mostrecentcalllast):
File&#xstdi;&#xn000;"stdin",line1,
TypeError:canonlyconcatenate
"str")to
Twolistsaredenedattheconsoleandassignedtothevariablesvec1and
vec2.Toaccessaniteminalist,usesquarebracketsafterthenameof
thevariable,forexample,vec1[3]accessesthefourthiteminthelist.Itis
importanttoemphasizethatinPython(andmanyothermodernprogramming
languages,therstitemisalisthastheindexzero.Toaccesstherstitem
invec1,usevec1[0].Countingfromzerocanbeawkwardatrst,butmost
experiencedprogrammersappreciatethesubtleadvantagesthatwillhopefully
becomeapparentlater.
Afterdeningthelists,anestedlististhenappendedontotheendofvec1
andthenthenestedlistisreplacedwith87(programmersliketosaythat
Pythonlistsaremutableandcanbechanged).Listscanalsobeextendedas
vec2isextended,orlistsofthesametypecanbeconcatenatedasvec1and
vec2arecombinedtogether.Alistcannotbeconcatenatedwithastringor
almostanythingotherthananotherlistasevidencedbytheerrormessageat
theend.
TuplesareanothertypeofcontainerinPythonthatare,inpractice,very
similartolists,theyareanorderedcollectionofobjectsthatmaybehetero-
geneous.emaindierenceisthattuplesmaynotbechanged.eyare
immutable.Asimpledemonstrationoftuplesisshownbelow:
���mytuple=('a',1,'c')
)c���(x,y,z)=mytuple
���(j,h)=mytuple
Traceback(mostrecentcalllast):
(j,h)=mytuple
ValueError:toomanyvaluestounpack(expected2)
2.4ExternalLibraries
Noticehowtheindividualobjectswithinthetuplearestillaccessedwith
squarebrackets.Inaddition,observethattheindividualobjectswithinthe
tuplecanbeassignedtoasetofindividualvariables,inthiscasenamedx,y,
andz.isisonlypracticalfortupleswithsmallnumbersofobjects,butit
canbeusefulaswewillseeinthenextchapter.Ofcourse,ifyoutrytoassign
threeindividualobjectstoonlytwovariables,theresultisanerror.
OnenaltypeofPythoncontainerthatisonlybrieymentionedhereisthe
dictionarycontainer.Withlistsandtuples,theobjectsinthecontainerwerein
aspecicorder,andwecouldaccesstheobjectsusinganintegercorresponding
tothelocationofthedesiredobjectwithinthatorder.erstandthirdobjects
inalistoratupleareaccessedusingname[0]andname[2],respectively,if
thelistortupleisstoredinvariablename.Dictionariesarecontainersthat
containapotentiallyheterogeneouscollectionofobjects,butdictionariesare
ordered.Instead,a
key
isassignedtoaccesstheobject.ekeycanbea
stringoranintegerorotherdescriptor,andwhenbuildingthedictionary,the
keyisfollowedbyacolon(:)andthentheobjectthatthekeyisreferencing.To
accesstheobjectreferencedbythekey,squarebracketscontainingthekeyare
used.eexamplebelowbuildstwodictionaries,oneusesstringsforthekeys
andtheotherusesintegersforthekeys:
���myDict={class:102,instructor:Heys}
)Heys���secondDict={0:zero,2:4}
)zeroNoticethatdictionariesareconstructedusingcurlybrackets,{and},and
everyentryisakey:objectpair.Usingintegersaskeysinthesecondexample
causesthedictionarytolookalittlelikeanorderedlistortuplebutthatisnot
thecases.Ifonetriedtoaccess
,forexample,anerrorwould
result.Dictionariesaremuchlesscommoninengineeringcomputationsand
arenotusedinthisbookforthealgorithmspresented.
2.4ExternalLibraries
Imaginethatwewanttocalculatesin
.IfwetrytypingthatintothePython
consoleorasimplepieceofsourcecode,thisiswhatwearelikelytosee:
���sin(1.2)
Traceback(mostrecentcalllast):
File&#xstdi;&#xn000;"stdin",line1,
NameError:namesin
isnot
Wegetanerrormessagebecausethesin
functionisnotabuilt-infunction
inPython.Inordertousethesin
function,weneedtoimportthemath
2ProgrammingwithPython
libraryintoPython.iscanbeaccomplishedtwodierentwaysandbothare
shownbelow.
Toimportalibrary,usethe
command
���math.sin(1.2)
isisthepreferredapproach.erstcommandimportstheentiremath
library,andanyfunctions,methods,ordatacontainedwithinthatlibrarycan
beaccessedbytyping:
where
isthe
nameofafunctioninthelibrary.Acompletelistoffunctionsandvalueswithin
themathlibrarycanbefoundatdocs.python.org/3/library/math.html.
eotherapproachtoimportingalibraryusesthe
command
���sin(1.2)
isapproachloadstheentiremathlibraryintotheglobalPythonnamespace,
whichcanbethoughtofasalistofreservedwordsthatarealreadydened.
Forexample,
isareservedwordthatispartoftheglobalPython
namespaceandweshouldneveruse
foranyotherpurpose.For
example,donottrytouseimportasavariablename.Wheneverweloada
libraryintothissameglobalnamespace,wegreatlyincreasethenumberof
globaltermsandinvitethepossibilityofconict.Forexample,ifwetriedto
loadtwolibrariesthatbothcontainthesin
function(boththemathlibrary
andtheNumpylibrary,whichweusefrequentlybothcontainasin
function),
Pythonwouldgiveusanerror.erearetimeswhenitiseasiertousethe
optionforloadinglibraries,butitisusuallybetter
touse
Ifyoutriedtorun
attheconsoleandweresuccessful,this
wasaresultofusinganIDEthatwassmartenoughtoloadthemathlibrary
foryou.
erearehundredsoflibrariesthathavebeenwrittenbyothersthatincrease
thepowerofPythonandsaveusfromhavingtorewritecodethathasalready
beenwrittenbyothers.Inthisbook,wewillusethe
numpy
scipy
,and
matplotlib
librariesextensively.Twolibrariesthatarenotcoveredingreat
depthinthisbookbutmaybehelpfulinfurtherdevelopingengineeringalgo-
rithmsarethesyslibraryandthetiminglibrary.esyslibraryprovides
informationandfunctionsforconnectingwiththecomputersoperating
system.Forexample,
willprintoutastringthat
identiestheunderlyingoperatingsystem.e
data
structurehasinformationaboutoatingpointprecisionandcomputational
roundoforthecomputercurrentlybeingused.etimelibrarycanbe
usefulformeasuringthetimerequiredtorunvariouspartsofanalgorithm.
2.4ExternalLibraries
efunction
returnsthecurrentprocessortime(inseconds),
andbystoringthetimeindierentvariablesandthencalculatingthedierence
betweenthosevariables,itispossibletodeterminetheprocessortimerequired
toexecuteaseriesofcommands.Forexample,thefollowingcode:
t0=time.clock()
...Pythoncodetobetimed...
(time.clock()-t0,"secondsprocesstime")
2ProgrammingwithPython
copysign,cos,cosh,degrees,e,erf,
erfc,exp,expm1,fabs,factorial,floor,
fmod,frexp,fsum,gamma,hypot,isinf,
isnan,ldexp,lgamma,log,log10,log1p,
modf,pi,pow,radians,sin,sinh,sqrt,
tan,tanh,trunc]
Notethatsomeoftheitemsinthemathlibraryarefunctions,likesqrtor
exp,andsomeoftheitemsareconstants,likepi.
Problems
UsingPython,calculate5/16,5.0/16,and0
.IncludeallPython
commandsandresults(usingacut-and-pasteapproachisrecom-
mended).Aretheresultscorrect.ForPythonversionsbeforePython
3.x,integerdivisionreturnedanintegerresult.Asaresult,5/16would
return0insteadoftheoatingpointanswer(0.3125).Wheneveryouuse
divisioninaPythonalgorithm,itcanbeimportanttomakesurethatone
ofthenumbersisaoatingpointnumberormultiplethenumeratorby
1.0toconvertitintoaoatingpointnumber.Alternative,inPython2.6
and2.7codes,youwilloftenseetheline:
togetthesamebehaviorasPython3.xwheretheresultis
alwaysreturnedasaoatingpointnumber.
WhenusingcontainersinPython,variableassignmentcangivesome
interestingbehavior.Beginbycreatingalistofintegersfrom1to5,and
storethelistinvariablea.Next,assignthevariablebtobeequaltoa.
Finally,changethevalueofthesecondentryinthelistofintegerstothe
number20.ecodebelowsummarizesthestepsnecessary.
���a=[1,2,3,4,5]
���b=a
���b[1]=20
Summarizetheoutputofthecodeaboveanddescribethebehaviorof
assigningvariablebequaltoa.
Forthesecondpartoftheproblem,repeatallthestepsinrstpart,except
assignbtoallthevalueswithinausing
���b=a[:]
Again,summarizetheoutputofthecodeanddescribetheassignment
behavior.
Problems
AnothertypeofPythoncontainerthatislesscommoninengineering
computationsisthetuple.Tuplesuseroundedbrackets:(and)
insteadofsquarebrackets,[and]likelists.emaindierence
betweentuplesandlistsisthattuplesareimmutable,thatis,theycannot
bechanged.Trytorepeattherstfewcommandsfromtheprevious
problemusingatupleinsteadofalist:
���a=(1,2,3,4,5)
)���b=a
���b[1]=20
Whatdidyouobserve?Canyouthinkofawaytogetanewtuplefrom
anprevioustuplewithanumberthatischanged?
Assignavariabletoastringthatisyourname.Forexample,Iwould
performthefollowingassignment:
WritethePythoncommandthatwillprintthesecondletter(e.g.,forme,
itshouldprintthelettere).Now,trytoreplacethesecondletterinyour
namewiththeletterz.Describeandexplainwhatyouobserve.
WhilewritingaPythonprogram,youdecidetoassignanewvariable
classtothevalueof1,2,3,or4dependingonthecurrentclassyearfora
groupofstudents.InPython,trytoassignthevariableclasstoavalue
of1.Describewhatyouobserve.Nexttrytoassignthevariablepassto
avalueofTrue.Describewhatyouobserve.
Tobetterunderstandwhatishappening,typehelp()atthePython
prompt(i.e.,the
prompt).isshouldbringupthehelpprompt
(i.e.,help
).Atthisprompt,typekeywordstogetalistofreserved
keywordsthatcannotbeusedasvariables.PrintthislistofPython
keywords.
AdditionalResources
BooksongeneralprogramminginPython:
LearningPythonbyLutz[1]
PythoninaNutshellbyMartelli[2]
inkPythonbyDowney[3].
2ProgrammingwithPython
References
Lutz,M.(2013)
LearningPython
,OReillyMedia,Inc.,Sebastopol,CA,
4thedn.
Martelli,A.(2009)
PythoninaNutshell
,OReillyMedia,Inc.,Sebastopol,
CA,2ndedn.
Downey,A.(2012)
inkPython
,OReillyMedia,Inc.,Sebastopol,CA,
1stedn.
3ProgrammingBasics
���a==b
���ab
���ba
Here,thecomparatorcomparestwostringstodeterminewhichisrstalpha-
betically.
Comparatorscanbeextremelyhelpfulinconstructingconditionalstate-
ments.Forexample,ifwewantablockofcodetoonlyexecutewhenacertain
conditionistrue,wecanusean
statement:
���a=4
or,intheformofascript:
wherethecodewill,ofcourse,printlargeruponexecution.Fromthesetwo
examples,wecanmakean
INCREDIBLYIMPORTANTOBSERVATION
(note
thatIwishthatIcouldmakethenextsentenceash).InPython,blocksof
codearedesignatedusingindentation.Every
statementhasacondition
followedbyacolon.Iftheconditionalis
thefollowingblockoftextis
executed,andthescopeorlengthoftheblockisdeterminedbythefactthat
alllinesofcodewithintheblockMUSTbeindentedEXACTLYtheSAME
amount.Iftherstlineintheblockisindentedfourspaces(andfourspacesis
thestandardPythonstyle),theneverylinemustbeindentedfourspace.Ifyou
messupandindentonelinewithonlythreespacesoratab,errormessages
andchaoswillfollow.MostpeoplendindentationatthePythonprompt
)awkwardanddicult.Anyalgorithmthatrequiresindentingablockof
codeisprobablysucientlongthatitshouldbedevelopedusingascriptand
notsimplyenteredatthePythonprompt.esameconsistentindentation
requirementforoneblockofcodefollowingaconditionalstatementalso
extendstonestedconditionalstatements,asshownintheexamplebelow.
(Enteraninteger(0-10):)
#converttheinputtoanintegerifpossible
("aislessthan5")
3.1ComparatorsandConditionals
("adding1")
a+=1
#thisisidenticaltoa=a+1
("aisstilllessthan4")
("adding1more")
a+=1
("a=",a)
egoalofthissimplecodeistoincreasethevalueenteredifitissmall.If
thevalueislessthan5,then1isrstaddedtotheoriginalinputvalue.Ifthe
valueisstilllessthan4,asecond1isadded.Uponexecutionwithaninputof
4,thiscodegenerates
lessthan5
adding1
a=5
andaninputof2generates
Enteraninteger:2
lessthan5
adding1
stilllessthan4
adding1more
a=4
Inthepreviousexample,wecanseeanexampleofacommentbeing
includewiththePythonscript.ecomment,
#thisisidentical
toa
,isincludedintheprogramtomakeitmorereadableandeasier
tounderstand.euseoffrequentanddescriptivecommentsishighly
recommended.Agoodruleofthumbisthatonecommentshouldbeincluded
foreverytwolinesofregularPythoncode.Anothergoodruleofthumbistouse
roughlyanorderofmagnitudemorecommentsinyourowncodecompared
towhatyouwillndinthisbook!InPython,acommentisinitiatedbythe
#characterandallfollowingcharactersarenotinterpretedorexecutedby
thePythonvirtualmachineitisasiftheydonotexist.Optionally,multiline
commentscanbeinitiatedusethreeconsecutivedoublequotesandended
usingthreeconsecutivedoublequotes.Onenalnote,thecommentcharacter,
#,canalsobehelpfulfortemporarilyremovingalineofcodefromexecution.
OnenewPythonfunctionusedinthepreviousexampleisthe
functionforgettinginputfromthekeyboard.einputfromthekeyboard
isstoredinthevariable
intheexample.Itis
always
agoodprogramming
practicetocheckthevalidityofinputeachandeverytime.Intheabove
example,itwouldbegoodtocheckthat
isbetweensomeminimumand
maximumintegersbeforeusingthevariableanyfurther.
Wheneveracomparatorisused,PythonreturnsaBoolean(i.e.,
inPython).eBooleancanbestoredinavariableasisillustratedina
simpleexample:
3ProgrammingBasics
andwenotethattheresultcanbeusedinaconditional
statement.e
outputofthisscriptshouldbe
becausethe
comparatorisidenticalto
inthisexample.
3.2IteratorsandLoops
Whencreatingcomputeralgorithms,weoftenneedtorepeataseriesof
commandsorinstructionanumberoftimes.Forexample,wemightwant
tocomparethevalueofavariabletoeachindividualinalistofvalues.In
numericalalgorithms,wemightneedtotakethesin
ofeachnumberinalist
ofnumbers.Forallthesesituations(andmanymore),weneedtouseiterators.
emostcommontypesofiterators(orloops)thatareusedinthisbookare
the
loopandthe
loop.
forLoops
Anextremelysimple
loopis
print(n)andtheoutputfromrunningthisloopis
Noticethatthestructureoftheforcommandis
wherethevariabletakesthevalueofeachiteminthelistinorder.Alsonotice
thatthe
commandendswithacolon(:)andtheblockofcodetobe
executedeachiterationthroughtheloopisindented.
esimpleexampleaboveillustratesthebasicelementsofa
loop,but
whatifyouwantedaloopthatrepeated50timesor50milliontimes?Would
weneedtotypeoutalistof50millionnumbers?eansweris,ofcourse,No!
3.2IteratorsandLoops
Pythonconvenientlyprovidesthe
functionforiterativelygenerating
listsofnumbersofanydesiredlength.Ifasinglenumberispassedintothe
function,itwilliterativelygeneratealistofintegersfrom0to
3ProgrammingBasics
Pythonmakesiteasytoiteratethroughanylistandperformvariousoperations
eachiteration.
Itisoftennecessarytonestaconditionalwithinaniterator.eouterloop
consistsofsomelistthatweareiteratingthrough,andtheinnerloopconsistsof
aconditionthatisexecutedwhenevertheconditionismet.Asimpleexample
ofthiswouldbetoiteratethroughalistofintegersandthenprintoutthevalue
ofintegersiftheyhave7asafactor(i.e.,theyareevenlydivisibleby7).e
simplePythoncodebelowperformsthistask.
inrange
i%7==0:
eoutputisalistofintegers,startingwithzeroandthencountingby7upto
98.Wecouldalsotrytheslightlymorecomplicatedtaskofdeterminingallthe
integersbetween0and150thathave5or7asfactors.ischangerequiresthe
useofalogical
inthe
statement.Wealsowishtocountthetotalnumber
ofintegersthatmeetthesecriteria.
counter=0
inrange
(i%7==0)
(i%5==0):
counter+=1
#incrementthecounter
(Total:,counter)
etotalnumberofintegersbetween0and150thathave5or7asfactorsis47,
whichisprintedoutatthelastlineinthescript.Averycommonmistakemade
bynoviceprogrammersistoindentthatlastline.Wheneveryouareiterating
andaccumulatingortotalingsomethingastheiterationsoccur,itiscriticalthat
noactionbetakenuntiltheiterationhascompleted.Ifthatlastlineisindented,
Pythondoesnotknowthattheprogrammerreallywantedtowaittilltheend
toprinttheresulttothescreen.
Nextisanexampleofa
loopthatcontinuesuntilaspeciccondition
ismet.Twowarningsregarding
loops:(1)makesurethatallvariables
inthestoppingconditionareinitializedtostartingvaluesbeforethewhile
statementand(2)makesurethattheloopcannotrepeatforaninnitenumber
oftimes.
x10.0:
y=2.0
x+=y
3.2IteratorsandLoops
Uponexecution,theoutputfromthiscodeshouldbe
(2.0,2.0)
(4.0,2.0)
(6.0,2.0)
(8.0,2.0)
(10.0,2.0)
Whileloopsareoftenusedinsituationswhereanunknownnumberof
iterationsisrequired.Itiscriticalinthesesituations,however,tohavesome
typeofstoppingconditiontopreventtheloopfromiteratingorexecuting
forever.Imaginethatwewantedtogeneraterandomnumbersbetween0.0
and1.0andthencountthetotalnumberofrandomvaluesgeneratedbefore
generatingarandomvaluethatexceedssomethreshold.esetypesof
calculationsarecommonwhendevelopingmathematicalmodelsofstochastic
orrandomprocesseslikechemicalkinetics.ePythoncodebelowallowsusto
countthenumberofrandomvaluesgeneratedbeforeexceedingasetthreshold.
maxIter=10
currentIter=1
val=random.random()
3ProgrammingBasics
numberofallowediterationsisreached,butitcananddoeshappen.Atypical
outputfromthecodeisshownbelow:
Iterationsrequired
arandomnumber(0-1)
greaterthan0.75?6
eoutputisdierenteverytimethecodeisexecutedbecausedierent
randomnumbersaregeneratedeachtime,althoughitispossibletomodify
thecodesothatthesamesetofrandomnumbersisgeneratedeachrun.In
addition,aswewillseeinthenextchapter,itisprobablymoreecientto
generatealistof10randomnumbersinasinglecalltoarandomnumber
generatorandthenloopthroughthelistuntilwereachonelargerthanthe
threshold(0.75fortheexampleabove).
Itispossibletouseiteratorstogeneratemorecomplexlists.Forexample,if
wewantalistofperfectsquares,thenwecanputaniteratorinsidethesquare
bracketsnormallyusedfordeningalist.Forexample,
x2=[x*x
inrange
willgeneratethelist
[0,1,4,9,16,25]
.esetypesofoperations
arecalled
listcomprehensions
.eycanalsobeusefulforgeneratingsublists
usingcomparators.Forexample,thelistoffactorsof3fromthefulllistofinte-
gers:
x2=[x
inrange
x%3==0]
willgeneratethelist
[0,3,6,9,12,15,18]
.Finally,thePython
keyword
maybeusedtocheckformembershipinalist.epreviousscript
canbeusedtocheckifspecicvaluesareintheresultinglist.
x2=[x
inrange
x%3==0]
escriptwillgenerate
andthen
Therange()Function
The
functioninPythonsupportsthefollowingarguments:
(start,stop,step)
.Ifonlyoneinputisgiven,itistreatedasthe
3.3Functions
aregiven,theyaretreatedasstartandstopvalues,andthreevaluesaretreated
asstart,stop,andstepsize.Usingthisinformation,trytoconstructaloopthat
printsouttheoddintegersfrom7to17,including17.
3.2.1IndentationStyle
Beforeleavingthetopicsofloopsandconditionals,itisworthrevisitingan
importantissuethatwasintroducedinthissectionindentation.InPython,
blocksofcodearedesignatedbyacommonlevelofindentation.Ifwehavemul-
tiplelinesofcodethatshouldbeexecutedaspartofa
looporaspartof
statement,thenthoselinesmusthaveauniformlevelofindentation.
3ProgrammingBasics
manysituations,itisveryhelpfultowriteourownfunctions.Advantagesof
writingfunctionsincludethefactthatitbecomeseasiertoreusethecode
youhavewrittenpreviously,andfunctionshelptobreakourprogramsup
intomanageablepieces,whichmakesprogrammingeasier.ekeyword
isusedtodeneafunctioninPython.iskeywordshouldbefollowed
bythenameofthefunctionandvariablenamesforanyinputs.esetof
instructionsthatmakeupthefunctionappearintheblockbelowthestline.
econstructionofafunctionisillustratedthroughthefollowingexamples.
Wewanttowriteafunctionthatwillprintouttheareaofatrianglegiventhe
sizeofthebaseandtheheight.
#twofloatingpointinputsrequired:
#lengthofthetrianglebase
#heightofthetriangle
area=0.5*base*height
3.3Functions
Build the function
def triangle():
Figure3.1
Avisualrepresentationoftheconstructionofthetrianglefunctionthatactsasa
3ProgrammingBasics
#nextlinegivesanerror
Uponexecution,thisprogramshouldrstdenethevariable
andthenitshouldload,butNOTexecutethefunctiontriangle.
en,thefunctiontriangleshouldbeexecutedwhenPythonreachestheline
wherethefunctionisrstcalled(
).ecode
shouldprint
and
tothescreenwhenthetwoprintstatementsare
executed.However,whenwetrytoexecute
weshouldget
anerrormessagetellingusthatthevariable
isnotdenedbecausethe
variableisonlydenedwithinthefunctionnotoutsidethefunction.
OneotherPythonfeaturetonoteinthisexampleistheuseofdefaultvalues
forthetwoinputparametersofthetrianglefunction.Bysettingdefaultvalues,
wecannowcallthefunctionwithorwithoutvaluesfor
Ifonevalueispassedintothefunction,thatvalueisusedforsettingtherst
variable,(
).Ifasecondvalueispassedintothefunction,bothofthe
defaultvaluesarereplacedwiththevaluespassedintothefunction.Default
valuesareveryhelpfulinsituationswhereparametersareunlikelytochange
inmostsituations.
equadraticformulaisusedtondtherootsofaquadraticpolynomialof
theform:
0where
,and
aregivenconstants.eroots
ofthepolynomialare
4

a

c
andtheremaybeuptotwouniquerootsduetothe
-sign.Further,iftheterm
undertheradicalsign(i.e.,squarerootsign)isnegative,therootsarecomplex.
WewouldliketodevelopaPythonfunctionthatwillcalculatestherootsofa
quadraticpolynomialgiven
,and
asinputs,assumingthattherootsare
realnumbers,anditshouldprintanerrormessageiftherootsarecomplex.An
examplefunctionisshownbelow.
myquad(Ain,Bin,Cin):
#checkforcomplexroots
Bin**24.0*Ain*Cin:
#ifcomplex,printerror
("Complexresultsnotallowed")
3.3Functions
x2=x2/(2.0*Ain
3ProgrammingBasics
circumference=16*math.pi
(numPeople1):
("Error:musthaveatleastoneperson")
3.5Top10
PythonErrorMessages
numberofdigitsbeforethedecimalpointand
isthenumberofdigitsafter
thedecimalpoint.Both
areoptional.
3.4DebuggingorFixingErrors
Probably,thesinglegreatestchallengethatanoviceprogrammerfacesis
correctingorxingerrorsintheirprograms.eprocessesofcorrecting
errorsincomputercodeiscommonlyreferredtoasdebugging.eorigin
ofthetermdebuggingisfrequentlyattributedtoGraceHopper,anearly
computerpioneerwhodiscoveredamothstuckinacomputerrelaythatwas
causingerrors.etopicofdebuggingisverybroadandalargenumberof
bookshavebeenwrittenthatarededicatedtothetopicofdebugging.Expe-
riencedcomputerprogrammersalmostalwaysusedebuggersoftwaretohelp
withtheprocessofndingerrors.Debuggingsoftwareallowsprogramstobe
executedoneorafewstepsatatimewhilethevalueofvariousvariablescanbe
continuouslytracked.isabilitytorunaprograminslowmotionandwith
fullvariableexposureisverypowerful.DebuggersforPythonareincludedwith
mostIDEsincludetheSpyderIDE.Whiletheuseofdebuggersisnotdescribed
here,afewimportantstrategiesforndingandcorrectingerrorsarelisted
below.
1)esinglemostcommonmistakemadebynoviceprogrammersistryingto
write10ormorenewlinesofcodewithouttestingthecode.Experienced
programmerstrytotesttheirprogramsafterwritingjustafew(2to5)new
linesofcode.Ideally,programmersliketostartwithasimilar,workingcode
thatcanbemodiedafewstepsatatimetoreachthedesiredcode.
TEST
OFTEN!!
2)Printoutthevalueofvariableswheneverpossibleduringtheinitialwriting
ofthecodetoensurethatthecodeisbehavingproperly.eseprint()
statementsareeasytoremovelater.
3)Ifapieceofcodeisnotworking,trytoaddacommentcharacter,#,before
asmanylinesaspossible.Hopefully,thiswillallowthecodetorun.en,
removethecommentcharactersonelineatatimetondthelinecausing
theproblem.
4)Usethedocumentationavailableforthesoftwarelibrariesyouareusingto
verifythatthecorrectvariablesarebeingpassedtofunctionsinthelibrary.
3.5Top10
PythonErrorMessages
Weendthischapterwithalistofthemostcommonlyencounterederror
messagesandsomecommoncausesforthosemessages.
3ProgrammingBasics
1)TypeErrorthiserroriscausedbytryingtouseavariableofonetypeina
situationthatrequiresadierenttype.Forexample,tryingtocombinean
intandastr.eTypeErrormessageisusuallyfollowedbytheactual
variabletypefollowedbytherequiredvariabletype.
2)IndexErrorthiserroriscausedbytryingtoaccesspartofalistorarray
thatisbeyondtheexistingrange.Afrequentcauseisforgettingthatalistor
arrayisindexedstartingwithzero.Ifanarray(e.g.,myarray)has5entries,
thenthe5thandnalentryisaccessedusinganindexof4(e.g.,myarray[4]).
Indiagnosingtheproblem,itisoftengoodtoprintthelengthofthearray
tothescreen.elengthofanlistorcontainercanbeobtainedusingthe
function.
3)SyntaxErrorthisiscausedbyaviolationofthePythonsyntaxor
formattingrequirements.emostcommoncauseisforgettingacolon
(:)attheendofalinethatrequiresone(e.g.,linesstartingwithif,
while,andfor)
4)SyntaxError:EOLwhilescanningstringliteralthisisaspecialsyntax
errorthatisusuallycausedbyforgettingaquotationmarkorusinga
mixtureofsingle()anddouble()quotationmarks.
5)NameErrorhappenswhenyoutrytouseavariablethathasnotbeen
dened.isfrequentlyoccurswhenweforgettoinitializeavariabletoa
value.
6)ZeroDivisionErrorprobably,theeasiesterrormessagetounderstand,but
itcanbediculttosolve.Itisalwaysagoodideatoprintthevaluesof
variablestothescreentobetterunderstandwhen/howavariableisbeing
settozeroinsteadofanonzerovalue.
7)IndentationErrorcausedbyinconsistentindentationinablockofcode
thatshouldhavebeenuniformlyindented.Visualinspectioncanoften
revealtheproblemunlesstheproblemiscausedbyamixtureofspaces
andtabs.esolutionistonotusetabsoruseaneditorthatconverts
tabsintospaces.
8)AttributeErrorhappenswhenwetrytocallafunctionthatdoesnotexist
(orwemisspellafunctionthatdoesexist)inalibrary(e.g.,math.sine()
insteadofmath.sin()).
9)KeyErroronlyoccurswithdictionarieswhenweuseakeythatdoesnot
exist.
Problems
Problems
evaporpressureofapureliquid,written
,isastrongfunctionof
temperature.Tocalculatethevaporpressureatagiventemperature,
,it
iscommontouseAntoinesequation:
log
where
,and
areconstantsthatcanbelookedupfordierentliquids.
Writeafunctionthathas
,and
C)asinputsandreturnsthe
vaporpressure,
.Hint:10
StartingwithAntoinesequation(seepreviousproblem),writeafunction
thathas
,and
(inmmHg)asinputsandreturnsthetemperature,
,forthegivenvaporpressure,
Writeafunctionthatcomparesitemssequentiallybetweentwolists,for
example,list
andlist
,andcalculatesthetotalnumberoftimesthatthe
iteminlist
islargerthantheiteminlist
.Ifthelistsareofdierent
length,thecomparisonshouldonlybeperformedforthetotalitemsin
theshorterlist(i.e.,ignoreitemsinthelongerlistthatarebeyondtheend
oftheshorterlist).ePythonfunction
isveryusefulforgetting
thelengthofalistasaninteger.
WriteafunctioncalledFtoC(T)thatreceivesatemperatureinFahrenheit
astheinputandreturnsthetemperatureinCelsiusasthereturnvalue.
WriteasecondfunctioncalledCtoF(T)thatdoestheoppositereceives
atemperatureinCelsiusastheinputandreturnsthetemperaturein
Fahrenheit.Demonstrateoneofthefunctionsbyinputtingthecurrent
temperatureatthelocationofyourbirthusingthestandardtemperature
measurementunitatthatlocationandprintoutthetemperatureinthe
othersystemofunits.Forexample,IwasborninBozeman,Montana,
USA,somyinputwouldbethecurrenttemperatureinFahrenheit,andI
wouldprintoutthetemperatureinCelsius.
WriteascriptthatstoreseachlineofthesongHappyBirthdayasa
separatestringinalist.en,inputfromtheuseranumbercorresponding
tothenumberoflinesthattheywouldlikeprintedtothescreen.Check
thenumbertodeterminethatitisvalidbeforeprintingthelinestothe
screen.
Writeafunctionthatreceivesasingleword(i.e.,astringoftext)asinput
andthenprintsoutthatwordinPigLatin.ForanyoneunfamiliarwithPig
Latin,thisrequiresmovingtherstlettertotheendandthenaddingay.
3ProgrammingBasics
Acouplehints:(1)specicletterswithinastringcanbeaccessedjustlike
avectorofnumbersforexample,ifmyString
je,thenmyString[0]
returnsjandmyString[2:3]returns,and(2)youcanconcatenate
stringswiththeplussign,somyString
heysgivesjeheys.Finally,
writethefunctionsothatitcanreceiveasentenceastheinputandthen
translateeachwordhint:usethestring.split()function.
Writeafunctionthatreceivesastringastheonlyinput.efunction
shouldtheniteratethrougheachletterinthestring(notethatthelength
ofthestringcanbeobtainedusing
andtheindividualcharacters
forstring
canbeaccessedusing
)andcountthenumberofeach
typeofvowel,a,e,i,o,oru.efunctionshouldalsocountthevowel
regardlessofcase:bothupperandlowercasevowelsshouldbecounted
(hint:character
orstring
canbeforcedtobelowercaseusing
whichmakescountingeasier).efunctionshouldreturnveintegers,
thenumberofvowelsofeachtype,a,e,i,o,andu.efunctionshouldbe
testedusingthestringAlphabetastheinput.
edecisiontreeshownbelow(Figure3.2)hasbeendevelopedbyChaste
Bankaftertheyanalyzedtheprobabilityofprepaymentoneachmortgage
theyhaveissuedoverthepastfewyears.YouhavebeenhiredbyChasteto
implementthedecisiontreeinPythonsothattheanswerstothequestions
canbeenteredbyabankrepresentativetodeterminetheprobabilityof
prepayments.ecodeshouldaskyesornoquestionsandacceptY,
y,N,ornastheanswer(i.e.,youwillwanttoconvertthestringto
8%
$75,000
$183,000
prepayment
2.6%
prepayment
6.4%
prepayment
13.9%
prepayment
36.0%
NO
YES
NO
Figure3.2
Adecisiontreeforassessingprepaymentriskonamortgage[3].
References
upper-orlowercasetoensurethatthecaseisknown).ecodeshould
thenprinttheprepaymentprobabilitytothescreen.
Youareplanningtopurchaseanewcarandyouaresuddenlyconcerned
abouttheannualoperatingcosts.Yourlistofcarchoiceshasbeennar-
roweddownto
Car(2015modelyear)mpg
ChevroletSilverado
wherethefueleciency(inmilespergallon)wasobtainedfromwww
.fueleconomy.gov/feg/pdfs/guides/FEG2015.pdf.CreateaPythonscript
thatstoresthemodelname(e.g.,Silverado)andfueleciencyforeach
vehicleintwoseparatelists.en,loopthroughthecarsandcalculate
theannualoperatingcostusingthecurrentpriceofgasatastationnear
youandassumingthatyouwilldrive10,000milesperyear.eprogram
shouldprinttheannualoperatingcostforeachvehicletothescreenas
wellasthemodelnameofthevehicle.
AdditionalResources
ReferencebooksonprogramminginPython:
PythonPocketReferencebyLutz[4]
PythonProgrammingbyZelle[5]
IntroductiontoComputingUsingPythonbyPerkovic[6]
BooksonnumericalprogramminginPython:
NumericalMethodsinEngineeringwithPythonbyKiusalaas[7]
PythonScriptingforComputationalSciencebyLangtangen[8]
References
vanRossum,G.,Warsaw,B.,andCoghlan,N.(2015)
StyleGuideforPython
Code
,http://www.python.org/dev/peps/pep-0008/.
Slatkin,B.(2015)
3ProgrammingBasics
Lutz,M.(2014)
4ExternalLibrariesforEngineering
andtheoutputis
[537]
[[2.3.]
[6.71.]]
Ifasingle,one-dimensionallistispassedintothe
method,aone-dimensionalvectoriscreated.Ifanestedlistispassedinto
the
method,atwo-dimensionaltensoriscreated.Every
numpyarrayhasadatatypeordtypeandthisparameterspeciesboththe
numericaltypeandprecisionofthevaluesstoredinthearray.Intheprevious
example,therstarray,myvector,wasconstructedusingalistofintegersso
numpyautomaticallysetthedtypeasint64or64-bitintegers.esecond
array,myarray,wasconstructedusingamixtureofintegersandoatingpoint
numberssothedtypewassetasoat64or64-bitoatingpointnumbers.
Inengineeringcalculations,wetypicallywanttoworkwithoatingpoint
numbersandoatingpointarithmatic,soitisdesirabletospecifythedata
typewhenitisrstcreated.iscanbeaccomplishedbysettingthedtype
parameteratconstruction.econstructionofmyvectorintheprevious
examplecanbeslightlymodiedtospecifythedesireddtype:
myvector=numpy.array([5,3,7],dtype=numpy.float64)
andnowtheoutputis:
[5.3.7.]
Notethedecimalpointsafterthevaluesinthevector,whichdenoteoating
pointvaluesinsteadofintegers.eintegersinthelistusedforconstruction
wereconvertedtooatingpointvalues.
Usingalistforarrayconstructionworksgreatforverysmallarrayswhere
wealreadyknowthevalues.Inpractice,however,wewillusuallyconstructan
arrayofzerosofthesizewewantandthenuseloopstoinsertthedesiredvalues
intothearray.isapproachisillustratedinthefollowingexample:
size=3
myarray=numpy.zeros((size,size))
inrange
inrange
4.1NumpyLibrary
eprogramconstructsasquare,two-dimensionalarraytothesizespecied
bytherstargumentpassedintothe
function.Ifavector
orone-dimensionalarrayofzerosisdesired,thenthefunctioncallcanbe
.Ifatwo-orhigher-dimensionalarrayisdesired,a
tupleshouldbepassedintothe
functionasisdoneinthe
exampleabove.ezeroswithinthearrayarethenreplacedbythevalues
calculatedinsidethenestedloop.isapproachtorstallocatingspaceforan
arrayandthenoverwritingtheinitialvaluesistypicallymorecomputationally
ecientthanrstconstructinganemptyarrayandthenappendingvalues.
Weneedtomakeaveryimportantobservationfromthepreviousexample:
4ExternalLibrariesforEngineering
lin=numpy.linspace(1.0,3.0,6)
logger=numpy.logspace(1.0,3.0,num=5)
functionrequiresaminimumoftwonumbersasinputs:a
startingvalueandastoppingvalue.efunctioncreatesanumpyvectorwith
astartingvalueastherstnumberandanalstoppingvalueasthesecond
number.eentriesinthevectorbetweenthestartingandstoppingvalues
arelinearly(orevenly)spacedandthetotalnumberofentriesinthevector
canbespeciedusingathirdnumberpassedtothefunction(thedefaultisa
vectoroflength50).evectorconstructedbythe
linspace(1.0,3.0,
functionaboveis
[1.1.41.82.22.63.]
.e
functionisverysimilar,butinsteadofspecifyingastartingandastopping
value,theexponentforthestartingandstoppingvaluesarespecied.e
defaultbaseis10,sothe
logspace(1.0,3.0,num
callabovecreates
avectorstartingat10
andendingat10
withlength5.eintermediate
entriesinthevectorarenotlinearlyspacedbutarebasedonlinearlyspaced
exponents.Asaresult,the
functionabovecreatesthenumpy
vector:
[10.31.62100.316.21000.]
FibonacciSequence
TheFibonaccisequenceisanimportantmathematicalseriesthatisfrequently
foundinnature(e.g.,thearrangementofsunowerseeds).Thersttwovalues
inthesequencearedenedas0and1.Additionaltermsinthesequenceare
calculatedbysummingtheprevioustwotermsinthesequence.Thefunction
belowstorestheFibonaccisequenceinanumpyarray.
seqLength=10
seq=numpy.zeros(seqLength,dtype=numpy.int32)
seq[1]=1foriinrange
print("Finalsequence:",seq)
4.1NumpyLibrary
4.1.2ArrayOperations
Toreview,wehavediscussedtheconstructionofnumpyvectorsandarrays,
andwehavediscussedhowtoaccessthevariousentrieswithinthevectorsand
arrays.Nowletusexploreafewdierentoperationsthatcanbeperformedon
matricesandvectorswiththefollowingexample.
Tryrunningthecodebelow:
myarray=numpy.arange(5)
#output:[01234]
#output:(5,)
myarray=myarray*4
#output:[0481216]
yourarray=numpy.ones(5)
theirarray=myarray-3*yourarray
#output:[-3.1.5.9.13.]
#360.0
itsarray=numpy.outer(myarray,theirarray)
#output:(5,5)
iscodesegmentbeginsbybuildingasequentialvectoroflength5.e
shapepropertyofthatvectorshouldreturn(5,),whichtellsusthatthearray
has5rowsandnoadditionalcolumns.isinitialvectoristhenmultipliedby
4andasecondandathirdarrayarebuilt.ethirdarray,calledtheirarray,is
actuallybuiltbytakingtherstarray,myarray,andsubtractinganarrayof3s
fromit.Notethataddingorsubtractingonearrayfromanotherrequiresthat
thearrayshavethesamesizeandshapethatiswhyitisoftenagoodideato
printthevalueofthesizeandshapetothescreensothatyoucanconrmthat
thesizesarethesame.eexamplecontinuesbytakingadotproductoftwo
vectors(theymusthavethesamelength)andanouterproduct.
isexamplebarelyscratchedthesurfaceofwhatispossiblewithnumpy.For
example,wehavenotyetcoveredmatrixinversionoreigenvaluecalculations,
butmanyofthesetopicswillbediscussedinlaterchapters.
4ExternalLibrariesforEngineering
logspace(start,stop,num=50,endpoint=True,base=10.0)
4.1NumpyLibrary
4.1.5RandomVectorswithNumpy
enumpylibrarycontainsfunctionsforgeneratingarraysofrandomnum-
bers.esefunctionsarein
,therandomsamplingpartofthe
library.Someofthemostusefulfunctionsaredemonstratedbelow.
numpyasnp
a=np.random.randint(0,10,size=5)
b=np.random.random(size=(2,2))
functionreturnsa
numpyarrayofthespeciedsize(avectoroflength5fortheexampleabove)
withtherandomintegersdrawnfrombetweenthelowand(high1),that
is,alltherandomintegerswillbegreaterthanorequaltothelowvalueand
strictlylessthanthehighvalue.Randomintegerscananddorepeat.For
example,thefunctionabovegenerated:
[30098]
ononeoccasion.
functiongeneratesrandomoating
pointnumbersgreaterthanorequaltozeroandstrictlylessthan1.0.e
exampleabovegeneratesa2
2arrayofrandomoatingpointnumbers.If
randomoatingpointnumberslargerorsmallerthan1.0aredesired,they
canbeobtainedbymultiplyingarandomarraybyascaler.Intheexample
above,randomoatingpointnumbersbetween
0,10.0)areobtainedsimply
bymultiplyingthearrayby10.0.enalfunctiondiscussedhereistheshue
function.e
functionwillsimplyshuethe
locationsofvaluesinanumpyvectororarray.Notethatanewarrayisnot
returned,but,instead,theoriginalarraypassedintothefunctionisforever
shued.Intheexampleabove,asequentialarrayofintegersisshued.e
originalarrayof
[01234]
wasshuedinto
[04213]
,inone
instance.
4.1.6SortingandSearching
esortingofnumpyvectorscanbeperformedusingthe
function.isisdemonstratedinthescriptbelowwhereavectorofrandom
oatingpointnumbersisgeneratedandthensorted.
numpyasnp
a=np.random.random()
b=np.sort(a)
4ExternalLibrariesforEngineering
eresultsofrunningthisscriptaredierenteverytimeitisrunduetothe
generationofrandomnumbers,buttheoutputfromoneexamplerunisshown
below.
[0.461182780.005778880.955398350.652181]
[0.005778880.461182780.6521810.95539835]
Sortingtwoorhigherdimensionalarraysisalsopossible.Typically,theaxisfor
sortingisspeciedwhenthe
functioniscalled.
Inengineering,weareofteninterestedinthemaximumorminimum
valuefromalistofvalues.Forexample,wemightwanttoknowthemaxi-
mumstressinauidorstructuralbeambecausethatvalueoftenneedsto
remainbelowsomethreshold.Twodierentpairsoffunctionareusefulfor
locatingandobtainingextremevalues.erstpair:
returnthemaximumvalueinanumpyvectorandthe
indexorlocationofthatmaximumvalue,respectively.esetwofunctions
areusedintheexamplebelowtondthemaximumvalueanditsindexfora
vectorofrandomvalues.
numpyasnp
a=np.random.random(4)
andtheoutputfromonerandomrunwas
[0.759924210.478085630.90041580.26679892]
Sincenumpyvectorsareindexedstartingwith0,themaximumvalue
4.1NumpyLibrary
othercomputationaltoolsthatsupportpolynomials),thepolynomialmust
bewrittenwiththezero-ordertermrstandthenprogressingsequentiallyto
higher-orderterms.Forexample,ifwewanttousethepolynomial:
wemustbeginbyreorderingthetermsinthepolynomialas
ispolynomialcanberepresentedinnumpyusingthe
package.euseofthispackagetobuildandevaluatetheexamplepolynomial
fromaboveisdemonstratedbelow.
numpy.polynomialasnp
f=np.Polynomial([-3.,-2.,1.])
(x,y)=f.linspace(8,domain=[-2,5])
evariable
holdsthecoecientsofthepolynomial,startingwiththe
zero-orderterm.epolynomialpackageincludesa
function
thatreturnstherootsofthepolynomial(inthiscase,thefunctionreturns
[-1.3.]
).Finally,the
functionevaluatesthepolynomial
at8pointsinthedomain
[
.e
functionreturns
twoarrays,onecontainingthe
-valuesandtheothercontainingthe
-values
resultingfromtheevaluationofthepolynomial.esearrayscanbeplottedto
visualizethepolynomialasshowninFigure4.1anddescribedinSection4.2.
4.1.8LoadingandSavingArrays
Numpyincludesextensivesupportforwritingvectorsandarraysintolesand
thenloadingthoselesatanothertime.Here,weareonlygoingtoexplorethe
readingandwritingoflesusingnumpysbinaryformatandthetraditional
ASCIItextformat.euseofbothformatsisillustratedinthescriptbelow.
Figure4.1
Agureofthe
Polynomialevaluationused
thenumpypolynomial
package,andtheplotwas
generatedusingmatplotlib.
21012345
4ExternalLibrariesforEngineering
numpyasnp
x=np.arange(5)
#.npyextensionautoadded
#binaryfileload
4.2MatplotlibLibrary
1.0
1.0
1.0
1.0
0246810024
6810
Figure4.2
Agureofacos
-wavegeneratedusingpylabwith(a)asolidline(and(b)circles.
matplotlib.pyplotasplt
x=numpy.linspace(0,10,num=100)
y=numpy.cos(x)
eresultinggureisshowninFigure4.2(a).enallineofexamplecode
)isrequiredbecauseitcausestheguretopersistonthescreen
andbecomeinteractive.Ifthislineisomitted,theguresmayneverbeplotted
tothescreenorisplottedforafractionofasecondbeforeitdisappearsonsome
operatingsystems.inkofthe
functionascausingtheprogram
topauseandwaitfortheusertodecidedwhattheywanttodowiththeplot.
functionisnotrequiredonalloperatingsystems.
Ifthefunctioncallto
isreplacedwith
,theplotisconstructedwithcirclesinsteadofasolidline,asshownin
Figure4.2(b).
Itisalsopossibletoplacemultiplecurvesonthesameplotandincludeaxis
labelsandguretitlesasillustratedinthefollowingexampleofpolynomial
curves.
matplotlib.pyplotasplt
numpyasnp
x=np.linspace(0,2,200)
plt.plot(x,x,label=linear)
plt.plot(x,x**2,.,label=quadratic)
plt.plot(x,x**3,--,label=cubic)
plt.xlabel(x-axislabel)
plt.ylabel(y-axislabel)
4ExternalLibrariesforEngineering
isexampleillustrateshoweverycalltofunctionsinthematplotliblibrary
areappliedtothecurrent,activegure.isbehaviorissimilartoMatlab.
Noticethataxislabelsareaddedtothegureusingthe
functioncallswiththedesiredstringoftextbeingpassedinto
thefunction.Atitleisaddedtothegureusing
functioncall,
but,incommonengineeringpractice,titlesarenotincludedforgures.Inthe
future,wewilluse
inthematplotliblibrarytogenerateaddi-
tionalguresandavoidhavingeverythingonthesamegure.eresultof
runningtheexamplecodeisshowninFigure4.3.
Polynomials
0.51.0
1.52.0
Quadratic
Figure4.3
Aplotshowingalinear,quadratic,andcubicpolynomialallonthesameplot
withalegendidentifyingeachcurve.
Beforeendingthissection,thepowerofMatplotlibisillustratedthrougha
slightlymorecomplexexamplethatshowsacontourplotofafunctionwith
twoindependentvariables.
4.3Application:GillespieAlgorithm
C=pylab.contour(X,Y,f(X,Y),8)
efunctionthatisplottedisdenedbythefunction
.einde-
pendentvariables,
(
(
,arestoredinvectorscreated
usingnumpys
function.enumpymeshgridfunctionextends
theone-dimensionalvectorsoveratwo-dimensionalarray.econtourplot
consistsofeightcontourlines,whicharelabeled,andacolorbarisaddedto
therightoftheplot.Colorbarsarelargelyunnecessaryandunattractivefor
thisstyleofcontourplot,butoneisincludedheretoillustratethesimplicity
withwhichitcanbeadded.eresultinggureisshowninFigure4.4.
1.0
210
1.000
Figure4.4
Acontourplotofafunction,
for
(
(
Matplotlibisacomprehensiveplottinglibrary,largeenoughthatanentire
bookhasbeenwrittentodocumentallofthemanydierentstyleofgures
andoptions.Formoreinformationaboutthelibraryaswellasdocumentation
describingtheinterfacesintothelibrary,seethetutorialspostedonthe
Matplotliblibrarywebsite:matplotlib.org.
4.3Application:GillespieAlgorithm
ischapterintroducedtwoimportantlibrariesforsolvingchemicaland
biologicalengineeringproblemsusingPython.Beforeendingthischapter,let
usexploretheuseofthesetwolibrarieswhenimplementinganimportant
algorithminmodelingbiochemicalreactions:theGillespiealgorithm.
4ExternalLibrariesforEngineering
Classicalchemicalreactionkineticmodelsarederivedbyassumingthata
systemhas10
ormorewell-mixedmolecules.Whenoneremembersthat
onemoleofamaterialhasmorethan10
molecules,theassumptionthata
reactorcontainsmorethan10
moleculesisalmostalwaysvalid.However,
ifwewanttomodelbiochemicalreactionsinasinglecell,thenumberof
reactingmoleculespresentisoftenontheorderof100or1000molecules.
Hence,usingclassicalchemicalreactionkineticstomodelreactionswithina
cellmaynotbevalid.eGillespiealgorithm,incontrast,isnotbasedonthe
assumptionofalargenumberofmolecules.Instead,thealgorithmisstochastic
andbasedontrackingadiscretenumberofmolecules.Briey,thealgorithm
isbasedonthegenerationofrandomnumbers(i.e.,analogoustorollinga
dice)fortwocalculations:(1)usingthereactionrateandarandomnumber,
thealgorithmdetermineswhetherornotareactionoccurredforarandom
molecularcollisionand(2)usingarandomnumbertodiscretelyapproximate
thetimeuntilthenextcollision.Detailedderivationanddescriptionofthe
algorithmarebeyondthescopeofthisbriefpresentation,buttheinterested
readerisreferredtotheoriginalGillespiepaper[1]oranyofthethousandsof
papersonthealgorithmwritteninthepastfewdecades.APythonlibraryfor
modelingdiscrete,stochasticreactionsusinganumberofdierentalgorithms,
includingtheGillespiealgorithm,isStochPy(stochpy.sourceforge.net).
ToillustratetheGillespiealgorithm,considerthechemicalreaction
where
istheforwardrateconstant(unitsarepertime)and
thebackward
rateconstant(sameunits).Classicalchemicalreactionkineticswouldpredict
thatthisreactionwillproceedtowardequilibriumandbehavelikearst-order
reaction.Alternatively,theGillespiealgorithmforthisreactionis:
matplotlib.pyplotasplt
k1=1.0
#forwardrateconstant
k2=0.1
#reverserateconstant
maxReact=1000
#maximumnumberofreactions
numMol=numpy.zeros((2,maxReact),dtype=numpy.
timePt=numpy.zeros(maxReact,dtype=numpy.
numMol[0,0]=175
#initialnumberofAs
numMol[1,0]=25
#initialnumberofBs
timePt[0]=0.0
#initialtime
rands=numpy.random.rand(2,maxReact)
inrange
4.3Application:GillespieAlgorithm
proB=k1*numMol[0,i]
#probabilityofformingB
proA=k2*numMol[1,i]
#probabilityofformingA
#calculatetimetillnextreaction
dt=-numpy.log(rands[0,i])/(proB+proA)
timePt[i+1]=timePt[i]+dt
rands[1,i](proB/(proA+proB)):
#formB?
numMol[0,i+1]=numMol[0,i]-1.0
numMol[1,i+1]=numMol[1,i]+1.0
#elseweformA
numMol[0,i+1]=numMol[0,i]+1.0
numMol[1,i+1]=numMol[1,i]-1.0
plt.plot(timePt,numMol[0,:],label="A")
plt.plot(timePt,numMol[1,:],label="B")
plt.ylabel('numberofmolecules')
erst20linesofthealgorithmaresettinguptheproblemtobesolved.e
scriptbeginsbyloadingthenumpyandmatplotliblibrariesandthentherate
constantsforthereactionarespecied.Notethattheforwardrateismuch
fasterthanthereverseratesoweshouldhavemore
than
atequilibrium.
eGillespiealgorithmisbasedoncountingtheexactnumberofmoleculesin
thesystemforapresetnumberofreactions.Inthiscase,wechoosetosimulate
1000reactionsandanemptynumpyarray,
,isconstructedtolater
storethenumberofmoleculesof
(column0)andmoleculesof
(column1)
aftereachofthe1000reactions.Asecondemptynumpyarrayisconstructed
tostorethetimeatwhicheachreactionoccurs.Recallthatthetimebetween
reactionsisstochasticthatis,aroleofthedice.esetupphaseendswith
theconstructionofanumpyarrayfullofrandomnumbers.earrayhastwo
columns,oneforeachreaction.
Afterthesetupphase,themainpartofthealgorithmisaniterative
loop
thatcontainsthecalculationsforeachofthe1000reactions.Eachiteration
beginsbycalculatingtheprobabilityoftheforwardreaction(forming
)and
thereversereactionprobability(forming
).etimetillthenextreactionis
alsocalculatedandusingarandomvalueandthereactionprobabilities,that
is,smallerprobabilitiesimplymoretimetillthenextpossiblereaction.Onthe
basisoftheprobabilitiesofforming
,thenumberofmoleculesof
isupdatedinthe
array.Finally,afterstochasticallysimulating1000
reactions,matplotlibcanbeusedtoplotthenumberofmoleculesof
inthesystemasafunctionoftime.Whileeverysimulationresultisdierent
duetothestochasticnatureofthealgorithm,anexampleofasinglesimula-
tionisshowninFigure4.5.ecurveresemblesanoisyversionofaclassical
rst-orderreaction.
4ExternalLibrariesforEngineering
050100150
Time
250300
150
100
Figure4.5
Numberofmoleculesof
forarst-orderequilibriumreactionsimulation
usingtheGillespiealgorithm.Forwardrateis
0andthereverserateis
1(both
pertime).
Problems
YouhavebeenhiredbyNASAtodevelopashortPythonscriptthat
whenexecuted,askstheusertoinputtheirweightonearthandthen
selecttheplanetthattheyarecurrentlyoccupying.eprogramshould
thencalculateandprintouttothescreentheindividualsweightontheir
currentplanet.Usearelativeweighttablesuchasthatreproducedbelow
tosimplifythecalculation.reenotes:(1)theinput()functioninPython
returnsastring,whichyouwillneedtoconvertintoanintegerusingthe
int()function,(2)itiseasiesttoasktheusertoinputanumberforthe
planetselection(i.e.,enter1forMercury,2forVenus,etc.),and(3)you
shouldchecktheplanetselectionusinganifstatementtoensurethatit
isvalid.
Mercury0.38
Venus0.91
Earth1.0
Mars0.38
Jupiter2.34
Saturn1.06
Uranus0.92
Neptune1.19
Problems
Youhavebeenhiredasaconsultingengineeringtoansweraquestion
fromtheEngineeringDepartmentatMosureUniversity.edepartment
wouldliketodeterminetheprobabilityoftwostudentsinthesameclass
havingthesamebirthdate.Weareonlyconsideringthedayoftheyear
(e.g.,February3)andnottheyearofbirth.Warning,youprobablywillnot
believetheresult.
Becauseweareengineersandnotstatisticians,youarebeinghiredto
developacomputerprogramthatiscapableofgeneratingstudentswith
randombirthdatesforaspeciedclasssize.eprogramwillsimulate
aspeciednumberofclassesanddeterminethefractionofclassesthat
containatleasttwostudentswiththesamebirthday.
eprogramshouldhavethefollowingattributes:
a)eusercansetavariableNumTrialsequaltothenumberofvirtual
classestheywishtosimulate.Iinitiallysetthisvariableto1000.
b)eusercansetavariableClassSizeequaltothesizeoftheclass
(e.g.,40,60,80,and200).
c)Youshouldwriteafunctionthatreceivesasinputanumpyvectorof
lengthClassSizethatcontainsthebirthdayforeverymemberofthe
class.efunctionshouldthendetermineifanytwostudentshavethe
samebirthday(andreturn1orTrueinthiscase)ornot(andreturn0
orFalseinthiscase).
d)eprogramshouldrunmultipletrials.Foreachtrial,youshould
constructanumpyvectoroflengthClassSizecontainingrandomly
assignedbirthdays.Irecommendusinganumberbetween0
and364torepresentthebirthday,andIrecommendusingthe
numpy.random.randint()functiontoconstructthevectorofrandom
birthdays.
Yournalreporttothedepartmentasaconsultantshouldconsist
ofadiscussionofyourndingsand,mostimportantly,aplotofthe
probabilityofaclasscontainingtwostudentswiththesamebirthday
versusclasssizeforclassesbetween20and200students.Notethatyou
shouldnotsimulateeverypossibleclasssizebetween20and200,just
pick5or6classsizesoverthatrange.ereportshouldbeintheform
ofamemotothedepartmentfromyou.Hint:inaclassof40students,
thereisa89%probabilityoftwostudentshavingthesamebirthdaysee,
Itoldyouthatyouwouldnotbelievetheresult.
Onenalwarningyouwillneedtocalculatetheprobability,thatis,
thefractionoftrialswheretwostudentshadthesamebirthdaydivided
bythetotalnumberoftrials.Calculatingthisfractioniseasy,Iused
successes/NumTrials.However,bothofthesevariableswereintegers
(e.g.,891/1000),andPython2.xreportstheresultasaninteger(i.e.,0
or1)insteadofafraction(i.e.,0.891).Toensureaoatingpointresult,I
used(1.0*successes)/NumTrials.
4ExternalLibrariesforEngineering
eMontyHallProblemposesfollowingchallenge:imagineyouareon
agameshowandarefacedwiththreedoors.Behindoneofthedoorsis
agreatprize,andbehindtheothertwodoorsissomethingoflittlevalue.
egameshowhostasksyoutopickoneofthedoors,whichyoudo.On
thebasisofyourchoice,thegameshowhostopenoneoftheothertwo
doorsandrevealssomethingoflittlevalue.Atthispoint,twodoorsremain
closed,oneofthemconcealingtheprize.egameshowhostoerstolet
youswitchyourchoiceofdoors.Youfearthatthehostistryingtotrick
you.Shouldyouswitch.
DeterminetheanswertothisquestionbywritingaPythonalgorithmthat
cansimulatealargenumberofvirtualgames(e.g.,
10,000games).
Createanumpyarraythatholdsarandominteger:1,2,or3,correspond-
ingtothewinningdoorforeachgame.Aneasywaytobuildthisarray
isthefunction:
.Withoutlossof
generality,itispossibletohavethecontestantselectthesamedoorevery
gameforall
games.Next,allowfortheselectionofwhethertoswitch
thedoorselectedornot.Onthebasisofthedoorselectionandthe
switchselection(yesorno),thealgorithmshouldbeabletoiterate
throughthe
gamesanddeterminethefractionwon.Doesthefraction
wondependonwhetheryouswitchornot?
AdditionalResources
Recommendedbooksontheexternallibrariescoveredhere:
MatplotlibforPythonDevelopersbyTosi[2]
LearningSciPyforNumericalandScieticComputing[3]
High-PerformancePythonbyGorelickandOzsvald[4]
Recommendedbookonlinearalgebra:
IntroductiontoLinearAlgebrabyStrang[5]
References
Gillesphie,D.T.(1977)Exactstochasticsimulationofcoupledchemical
reactions.
J.Phys.Chem.
,23402361.
Tosi,S.(2009)
MatplotlibforPythonDevelopers
,PacktPublishing,
Birmingham,UK.
References
Rojas,S.J.,Christensen,E.A.,andBlanco-Silva,F.J.(2015)
LearningSciPyfor
NumericalandScienticComputing
,PacktPublishingLtd.,Birmingham,UK,
2ndedn.
Gorelick,M.andOzsvald,I.(2014)
HighPerformancePython:Practical
PerformantProgrammingforHumans
,OReillyMedia,Inc.,Sebastopol,CA,
1stedn.
Strang,G.(2009)
IntroductiontoLinearAlgebra
,Wellesley-CambridgePress,
Wellesley,MA,4thedn.
5.2SymbolicMathematicsPackages
approach,suchastheonedescribedinthischapterjusttocheck-and-seeifa
symbolicsolutioniseasilyavailable.ischapteronsymboliccomputations
alsoprovidesagoodreviewofsomePythonprinciplesthatwerecovered
previously,includingtheuseofexternallibraries.
5.2SymbolicMathematicsPackages
Alargenumberofsoftwarepackageshavebeendevelopedforsymbolic
mathematics,andthecapabilitiesofthevariouspackagesarenotthesame.
Asof2014,Wikipedialistedover30dierentsoftwarepackagesforsymbolic
mathematics.epackageslistedbelowareallcommercialsoftware,butthey
areamongthemostpopularandsitelicensesareavailableonmanyuniversity
campuses.
Maple
Oneoftheoldestsoftwarepackagesforsymbolicmathematics,andit
wasoriginallywrittenattheUniversityofWaterloointheearly1980s.e
nameisareferencetoMaplesCanadianheritage.Whileitwasquitepopular
before1995,itspopularitydeclinedduetoauserinterfacethatwasdicult
touse.enewuserinterface,introducedin2005,issignicantlybetterand
similartotheotherpackagesavailableforsymbolicmathematics.
Mathcad
Oneoftherstmathematicssoftwarepackageswithagraphicaluser
interfaceandsupportforSIunits.esoftwareispopularforproducing
reportsanddocumentationthatincludemathematicalcalculations.e
symbolicmathematicscapabilitiesaresucientformostpurposes,butnot
asstrongassomeoftheotherpackageslistedhere.
Mathematica
Initiallyreleasedin1988,Mathematicawasoneofthe
rstsymbolicmathematicspackageswithagraphicaluserinterface,
whichisreferredtoasthefrontend.Whilethecreationofcustom
algorithmsremainsdicultinMathematica,itisstilloneofthemost
popularplatformsforcomputationalmathematics.Someofthefunc-
tionalityisavailablefree-of-chargethroughtheWolframAlphawebsite
(https://www.wolframalpha.com/).
MATLABsSymbolicMathematicsToolbox
MATLABisprimarilyusedfor
numericalcomputing,buttheSymbolicMathematicsToolboxprovides
somesymboliccapabilities.Dependingonthetypeoflicense,thisisoneof
themostexpensiveoptionslistedhere.
OneadditionalpackagethatshouldbehighlightedhereistheSage
(previouslySAGE,SystemforAlgebraandGeometryExperimentation)
mathematicssoftware,whichisfreeandlicensedunderaGNUGeneralPublic
License.OfparticularinteresthereisthefactthatSageusesthePython
programminglanguage,soindividualsfamiliarwithPythonwillhaveamore
modestlearningcurve.SagemakesextensiveuseofPythonlibraries,including
NumPy,SciPy,andSymPy,inordertoavoidhavingtoreimplementlarge
5SymbolicMathematics
amountsofexistingcode.WhileSageisanexcellentresourceformathematical
computing,itisnotcoveredindetailinthisbookbecauseofthelargesizeof
theplatform.ecuriousreaderisencouragedtoexploretheSagesoftware
anditsfeatures.ebrowser-basednotebookinterfaceavailableforSage
(SageMathCloud)maybeofparticularinterestandwasbrieydiscussedin
Chapter2.
efocusinthischapterisontheuseoftheSymPylibrary,whichadds
supportforsymbolicmathematicstoPython[2].SymPyiswrittenentirely
inPythonanddoesnotrequireanyexternallibraries.SymPyisincluded
withmanydistributionsofPythonthatarefocusedonscientistsorengineers
includingtheAnacondaPythondistributionandPythonxy.Installationon
Linuxsystemsisalsostraightforward.Furtherinformationrelatedtodown-
loadingandinstallingSymPyaswellascomprehensivedocumentationis
availableontheSymPywebsite:www.sympy.org.
5.3AnIntroductiontoSymPy
eSymPylibraryisimportedintoanyPythonprogramthatwewriteusing
thecommand:
.Asaresult,allmethodsassociatedwiththe
libraryareaccessedusingstandard
4
ederivationofthequadraticequationonlyrequiresstraightforwardalge-
braicmanipulationofthequadraticpolynomialtosolvefor
.equadratic
equation,thatis,therootsofthequadraticpolynomial,canalsobederived
usingSymPyasisillustratedintheexamplebelow.
5.3AnIntroductiontoSymPy
Determinetherootsofthequadraticpolynomial,
0,using
SymPy:
a,b,c,x=sympy.symbols(abcx)
expr=a*x**2+b*x+c
equadraticpolynomialisstoredinthevariable
,andusingthe
functionintheSymPylibraryallowsforthedeterminationofthe
rootsofthepolynomial.eoutputfromtheexamplecodeshouldbe
[(-b+sqrt(-4*a*c+b**2))/(2*a),-(b+sqrt(-4*a*c+
whichisaPythonlistcontainingthetworootsofthepolynomial.
Ifthevaluesof
,and
areknown,thentheSymPylibrarymaystillbeused.
Forexample,thefollowingmodicationtothecodeabovesolvesfortheroots
of3
x=sympy.symbols(x)
expr=3*x**2+4*x+5
Inthiscase,theoutputshouldbe
2/3sqrt(11)*I/3,2/3+sqrt(11)*I/3]where
1.Notethatthe
function
assumesthatalltermsintheequationhavebeenmovedtoonesideofthe
equalsign.Iftheequationinthefollowingexamplehadinitiallybeenwritten:
,itwouldhavebeennecessarytorstrearrangethetermssothat
theywereallonthesamesideoftheequalsign.
AnotherveryhelpfulfunctioninSymPyisthe
method,whichsub-
stitutesoneexpressionforanother.Asimpleexampleofthisisreplacing
ina
polynomialwithaspecicvalueasillustratedbelow.
x=sympy.symbols(x)
expr=3*x**2+4*x+5
Here,2.0issubstitutedfor
intheexpressionandtheresultissimpliedto
25.000.econversionofexpressionsintooatingpointnumberscanuse
,andthedesiredprecisioncanbepassedintothefunction.For
example,replacingthelastlineinthepreviousexamplewith
evaluatestheresultaftersubstitutionto16
digitsofprecision.
5SymbolicMathematics
functionwaspreviouslyusedforsolvinganalgebraic
equation.esyntaxofthisfunctionis
where
maybeasingleequationoralistofequations(alistin
enclosedinsquarebrackets,[item,item,item]).enumberofvariables
listed,ofcourse,mustequalthenumberofequations.Intheexamplebelow,
the
functionisusedtosolveacommonproblemindescribingthe
behaviorofgases.
vanderWaals
equationofstateisacommonequationforrelatingthe
temperature(
),pressure(
),andspecicvolume(
)ofanonidealgas.e
equationmaybewrittenas
V

b

a
where
b
=
atm/(mol
arethecriticaltemperatureandpressureofthegas,respectively.
Ourgoalistocalculatethespecicvolumeofammonia(
5Kand
3atm)at
420Kand
4atm.BeforeusingSymPytosolveforthe
specicvolume,weneedtorewriteourequation(s)sothatalltermsareonone
sideofthe
sign.us,wewillwritethevanderWaalsequationas
V

b
+
a
Wearenowreadytosolveforthespecicvolume.
R=0.08206
#Latm/(molK)
P=43.4
#atm
T=420.0
Tc=405.5
Pc=111.3
#atm
a=27*(R**2*Tc**2/Pc)/64
b=R*Tc/(8*Pc)
V=sympy.symbols(V)
f=P-R*T/(V-b)+a/(V**2)
5.3AnIntroductiontoSymPy
eoutputfromthisexampleis
[0.70088,0.06531-0.02985*I,0.06531+0.02985*I]
eequationiscubicwithrespectto
soweshouldnotbesurprisedbygetting
threesolutions(i.e.,threeroots).Inthiscase,itissimpletodeterminethe
correctsolutionastwoofthesolutionsarecomplexandobviouslynotphysical.
WeshouldalsonotethatneitherPythonnorSymPysupportsunits,sotheonus
5SymbolicMathematics
2.01.51.00.50.00.51.01.52.0
Figure5.1
-diagramofthetwoequationsthatweresimultaneouslysolvedinthe
multipleequationsexample.
5.4FactoringandExpandingFunctions
Oneofthemoretediousanderror-proneroutinetasksinmathematics
isexpandingandfactoringpolynomialequations.SymPycanusuallyper-
formthistaskautomatically.WecanuseSymPytoexpandthefunction
3usingthefollowingcode.
x=sympy.symbols(x)
f=(x+2)**3+3
Runningthisshortprogramgivesustheexpandedpolynomial:
11.isexpansioncanalsobeperformedbyhumans,buttheerrorrate
andtimerequiredarebothhigh.
AnevenmoreusefulfeatureofSymPyisitsabilitytofactorpolynomials.
Astudentcouldspendhourstryingtofactorthepolynomial27
125,butSymPycanfactoritinafewsecondsusingthefollowingcode.
x=sympy.symbols(x)
f=27*x**3+135*x**2+225*x+125
Runningthiscodeblockprintsthefactorization,
tothestandard
output.UsingSymPytoexpandandfactorpolynomialsissimpleenoughthat
5.4FactoringandExpandingFunctions
itcanoftenbedonestraightfromthePythonpromptwithouttheconstruction
ofacompletescript.
(isexampleisadaptedfromanexampleinFelderandRousseau[3].)Ifthe
feedtothisreactoris2.0molofCOand2.0molofH
Oandtheextentthat
thisreactionproceedstotheright(i.e.,theextentofreaction)is
,thenwecan
writethemolefractionofeachspeciesas
andtheequilibriumequationas
egoalistodeterminetheextentofreaction,
,whichisrelativelystraightfor-
wardbutrequiresexpandingoutthepolynomial.efollowingcodeillustrates
theuseofSymPytoeitherhelpusexpandthepolynomialorsolvefor
xi=sympy.symbols(xi)
f=xi**2/((2-xi)*(2-xi))-1.4
g=xi**2-1.4*(2-xi)*(2-xi)
erstthreelinesprintedbytheprogramallgeneratethesimplied
quadraticpolynomialassociatedwiththeequationfor
,specically,theygive
6,whichcanbesolvedfor
usingthequadraticequation.
5SymbolicMathematics
elastlineprintsoutthesolution,1.084or12.92.Sincewestartwithonly
2.0molsofCOandH
O,theonlyphysicallypossiblesolutionis1.084mol.
5.4.2PartialFractionDecomposition
AchallengingalgebraicexercisethatarisesfrequentlyinProcessControls(the
areaofChemicalandBiologicalEngineeringthatstudiestheautomatedcontrol
ofprocessoperations,includingtheuseofsensorsandvalves)isthepartial
fractiondecompositionofrationalfunctions,whichhavetheform:
where
arepolynomialswith
beingthehigherdegreepoly-
nomial.eobjectiveofpartialfractiondecompositionistodecomposethis
rationalfunctionintooneoftheform:
(
x
)
=
A

k
B
bydeterminingtherootsof
,whichgives
,andsoon,andthevalues
for
,andsoon.eserationalfunctionsarecommonwhenanalyzing
feedbackcontrolloopsusingLaplacetransformsinprocesscontrols.e
partialfractiondecompositionisrequiredfortransformingtheanalysisfrom
theLaplacedomainintothereal-worldtimedomain.
efunction
canautomaticallyperformmanypartial
fractiondecompositions,andtheuseofthisfunctionisdemonstratedinthe
examplebelow,whichdecomposestherationalfunction
s=sympy.symbols(s)
f=1/(s*(s+0.5))
eoutputfromthisexampleis
-2.0/(1.0*s+0.5)+2.0/s
andtheinterestedreadercanconrmthat

(
s
+
0
.
5
)
=

2
+
0
.
5
+
2
Inthisexample,
2,and
5.5DerivativesandIntegrals
Itisalsopossible,andoftenveryhelpful,tousesymbolicmathematicssoftware
whentakingderivativesandintegrals.Symbolicderivativescanbeobtained
5.5DerivativesandIntegrals
usingthe
function.Letusbeginbytakingthederivativeofa
function.
x=sympy.symbols(x)
eoutputfromthiscodeiscos
,asexpected.Passinginadditional
symbols(i.e.,variables)intothe
functioncausesadditional
derivativestobetaken.Forexample,ifthelastlineinthepreviouscode
blockisreplacedwith
,the
secondderivativewithrespectto
isdeterminedandtheoutputis
Alternatively,addingytothesymbolslistandaskingforthederivativewith
respecttoy,
,givesthe
expectedresultofzero.
SymPyisespeciallyhelpfulwhentakingderivativesofmorecomplex
functionsbecausehumansaremorelikelytomakeanerrorasthenumber
ofalgebraicstepsincreases.escriptbelowisusedtotakethederivativeof
tan
log
x=sympy.symbols(x)
f=x**2*sympy.tan(x)+x*sympy.log(x)
andtheoutputisthederivative:
x**2*(tan(x)**2+1)+2*x*tan(x)+log(x)+1
NotethatinSymPy(andPython,ingeneral),log
isthenaturallogarithm
function.
5.5.1ReactionExample
Whendesigningachemicalreactor,wesometimeshaveamathematical
expressionthatrelatestheconcentrationofaspeciesinthereactortotime.
Forexample,assumethatweknowthat
where
istheconcentrationofspecies
theinitialconcentrationof
aconstant,and
time.Wewouldliketotakethederivativeof
withrespect
totimetodeterminetherateofthereaction.efollowingPythoncodewill
determinethederivative:
Ca0,k,t=sympy.symbols(Ca0kt)
5SymbolicMathematics
Ca=Ca0*sympy.exp(-k*t)
(sympy.diff(Ca,t))
eoutputfromthescriptis
,whichtheobservant
readerwillrecognizecanbesimpliedto
5.5.2SymbolicIntegration
Symbolicintegrationtendstobeevenmorehelpfulthansymbolicdier-
entiation,probablybecauseintegrationbyhandisoftenmoredicultthan
dierentiation.e
functionisusedforsymbolic
integration.efollowingcodeblockdemonstratesboththesingleanddouble
integrationsofasimplesin
function.
x,y=sympy.symbols(xy)
))print(sympy.integrate(sympy.sin(x),x,x))print(sympy.integrate(sympy.sin(x),x,y))Uponexecution,thecodeoutputstheexpectedresultof
cos
forthe
functioncall.esecond
functioncallisnearly
identicaltotherst,butthistimealistisgivenforthesecondargument.e
listcontainsthesymbolicvariabletobeintegratedandtheboundsonthat
variable.eresultisadeniteintegral,andtheoutputis0.4597.ethird
functioncallresultsinsin
beingintegratedtwicewithrespect
,thatis,thisisadoubleintegral,andoutputstheexpectedresult:
Whatshouldweexpectfromthenalprintstatement,whichintegratessin
rstagainst
andthenagainst
?Well,therstintegrationwillyield
cos
andthesecondintegrationwilltreatanyfunctionof
asaconstantand,asa
result,integrationagainst
willgive
cos
5.5.3ReactorSizingExample
Asstudentslearninareactordesigncourse,thesizingofabatchreactor
containinganirreversiblesecond-orderreaction
requiresthatwe
evaluatetheintegral:
Whilethisintegralisstillrelativelysimple,itneverhurtstocheckourwork
withasymbolicmathematicsprogram.efollowingscriptwillevaluatethis
integralusingSymPy.
5.6Cryptography
X=sympy.symbols(X)
t=sympy.integrate(1/((1-X)**2),(X,0,X))
Acoupleinterestingobservationscanbemadefromthisblockofcode.
First,noticethefunctioninterfacefordeniteintegrals:
lower_bound,upper_bound)
,which,fortheproblemofinteresthere
.Second,theresultofsymbolicintegrationisinitially(i.e.,therst
printstatement):
,whichiscorrectbutnotthesimplestformpossible.
Toobtainthemorecommon,andsimplerresult,the
functionisusedtogetthestandardresult:
5.6Cryptography
libraryhasabasicsetofciphersthatallowforagentle
introductiontosomeclassiccryptography.eonemajorconstraintthatthe
userofthislibraryneedstobeawareofisthatthelibraryonlysupportsupper-
casestringswithoutspaces.us,beforeusinganyoftheprovidedciphers
onasecretmessage,itisnecessarytoconvertthestringintouppercase
letters(using,e.g.,the
function)andremovinganyspaces(using,
forexample,the
replace("","")
functionwheretherstsetofquotes
enclosesasinglespaceandthesecondsetofquotesisemptyandwithoutany
spaces).
erstandsimplestcipheristheshiftcipherorCaesarcipher,afterJulius
Caeser,apurporteduserofthecipher.Withthiscipher,allthelettersinthe
messageareshifted
lettersforwardinthealphabet.If
2,forexample,then
AisreplacedwithCandBisreplacedwithD.ekeytoreversingor
decipheringthemessageistoreversetheshift,whichonlyrequiresknowing
.ekeyforencodinganddecodingshiftcyphersisasingleinteger.e
Pythonalgorithmbelowdemonstratestheuseoftheshiftcipher.
sympy.crypto.cryptoascipher
5SymbolicMathematics
Itiseasytoconrmthateveryletterinthesecretcodewasshiftedtothenext
letterinthealphabet,andthekeyforthisexampleis1.Dierentkeyscanbe
speciedinthecalltothe
function.
Aslightlymorecomplexcipheristheanecipher,whichrequirestwo
integersforthekeythatisusedinencryptinganddecryptingmessages.e
twointegers,
,areusedtomapeveryletter,
,representedasaninteger
(025)toanewletter,
,alsorepresentedasaninteger(025)usingthelinear
function:
mod26
1and
1,then
1,whichmeansthateveryletterisreplaced
withthesameletterplusone,thatis,thisisidenticaltoashiftcipherwithashift
of1.If
2and
2then
2,andtheletterE,
4isreplaced
with
10ortheletterK.euseoftheanecipherisdemonstratedwith
thescriptbelow.
sympy.crypto.cryptoascipher
Problems
eoutputfromthescriptis:
.
0
,where
0and
0.efollowingPythonalgo-
rithmplotstheshapeoftheSquarechip.
x,y=sympy.symbols(xy)
z=x**2/1.0-y**2/2.0
#Notethatsympy.plotting.plot3d()calls
Youhavebeenhiredasaconsultanttoanswertwoquestionsaboutthe
newSquarechipbytheMountainChipcompany.
a)ecompanybelievesthattheSquarechipismostlikelytobreakwhere
thecurvatureisgreatest,andtheywouldlikeyoutodeterminethe
location(s)withthegreatestcurvature.Curvatureisapproximatedby
x

+


.Determinethelocation(s)andmagnitudeofthe
greatestcurvature.ecompanymadeitveryclearthattheydonot
trusthumancalculationsandtheyrequireananswerfromcomputer
usingsymbolicmathematicssoftware.
b)Inpursuitofevergreaterpackingeciency,thecompanyisconcerned
aboutthevolumeofairbelowthelowestchipinthestackofchipsinthe
5SymbolicMathematics
box.Youneedtodeterminethevolumeofairbelowthechip(i.e.,the
volumebetweenthechipandaatsurface)byintegratingthefunction
describingtheshapeofthechipoverthedomain
0and
0.Itisprobablynecessarytomodifythefunctionsothat
theminimumvalueofthefunction
overthedomain
0and
0iszero.ismodiedfunctionwillrepresent
thechiprestingonaatsurface(theatsurfaceis
0).Ifyou
runtheplottingfunctionintheboxabove,youwillseethatthecurrent
functionappearstopossiblybelessthanzeroforsomevaluesof
0and
Youhavebeenhiredbyaspecialtychemicalcompanythathasbeen
researchingthephysicalpropertiesofacetone(acommonchemical
forremovingnailpolish).ecompanyobservedthatatanunknown
temperature,acetoneexhibitedthesameheatcapacityaswater.e
companyfoundthefollowingquadraticequationfortheheatcapacityof
acetoneasafunctionoftemperature[4]:
Youhavebeenhiredtodeterminethetemperatureatwhichacetonehas
thesameheatcapacityaswater(assumethatwaterhasaconstantheat
capacityof75.6J/(mol
K))onapermolbasis.Finally,thecompanyhas
twoadditionalrequirements:(1)youneedtodetermineanequationfor
thechangeinheatcapacityasafunctionoftemperature,thatis,
,and
(2)plottheheatcapacityasafunctionoftemperatureoverthefullrangeof
temperatureswheretheheatcapacityofacetonemightequaltheconstant
headcapacityofwater.ecompanyisnotoriouslyskepticalofpeople
thatperformmathematicalanalysisbyhandandisrequiringthatyou
performallcalculationsusingsymbolicmathematicssoftware.
YouhavebeenhiredbytherecentlyreformedLaVieClairecyclingteam
(thisistheteamthatGregLeMondwontheTourdeFrancewithin
1986seetheESPN30for30documentary,SlayingtheBadger).e
newteammanagerknowsalittlephysicsandmadeaninterestingobser-
vationwhilestudyingthestandarduiddynamicsequationdescribing
thedragforceonabodymovingthroughair(oranyNewtonianuid):
where
,thedragforce,isapproximatelyequaltotheforcetherideris
applyingtothepedals(neglectingmechanicalresistance)whenridingon
aatsurface.eteammanagerclaimedthatforagivenforcefromthe
rider,theequationshouldhavetwosolutionsforthevelocity,
,because
itisquadratic.emanagerfurtherassertedthatifriderscouldchange
Problems
theirvelocitysomehow,theycouldshifttheirvelocitytotheother,faster
solutiontotheequationwithouthavingtochangetheforceonthepedals.
Youhavebeenhiredbytheteamownertoinvestigatethisclaim.Using
symbolicmathematicssoftware,showthatthereisonlyonepositive
velocitysolutiontotheequationif
,theforce,
(thedragcoecient
multipliedbytheridersfrontalorcross-sectionalarea),and
(the
densityofair,1
0kg
)areallpositive.
esecondhalfoftheownersrequestisthatyoucalculatethecyclists
velocity(inmeterspersecondandmilesperhour)usingthefollowing
assumptions:
Professionalcyclistsperform10,000kJ/dayofwork
Inamajorrace,cyclistsride250,000m/day
Workisforcetimesdistance(i.e.,forceisworkoverdistance)
foracyclistsaloneontheroadis0
Finally,estimatethevelocityofthesamecyclistinagroupwhereeach
cyclistcandraftothepersoninfrontofthem,thusreducing
Haveyoueverlookedatthatlittleholeatthebottomofwindowson
commercialaircraftsandwondered,Whyisitthere?(answer:search
holesinairplanewindowsonhttp://www.slate.com).Commercial
airplaneshavethreelayersforeachwindow:theinnerlayertocatchsnot
fromsneezes,amiddlelayerthatcontainsthetinyholeatthebottom,and
anouterlayer.Itturnsoutthattheholeinthemiddlelayerisdesignedto
belargeenoughtokeepmoisturefromaccumulatingbetweenthetwo
outerlayerswhileatthesametimebeingsmallenoughtopreventatotal
lossofcabinpressureintheeventthattheouterwindowlayercompletely
fails.
Youhavebeenhiredbyamajorairplanemanufacturertoestimatethe
owrateofairthroughthesmallholeintheeventthattheoutermost
layerofglassfails.emanufacturerneedstobesurethattheairplane
cabinpressurizationsystemhastheabilitytopreventthetotallossof
cabinpressure.
eowthroughthesmall,cylindricalholeshouldbeestimatedby
assumingPoiseuilleow:
where
60kPaisthepressuredierencebetweentheinsideand
outsideoftheairplane,
theviscosityofair,
0mmthelengthofthecylindricalhole,and
5mmtheradius
ofthehole.evelocityofairthroughthehole,
,isafunctionofthe
distance,
fromthecenterofthehole.Hence,thevelocityismaximum
alongthecenteroftheholeanditdecreasesclosertotheedgesofthe
5SymbolicMathematics
hole.Attheedgeofthehole,
andthevelocityiszero.eairnear
theedgeissloweddownbyfrictionwiththeglassofthewindow.
erstpartofyourcontractwiththeairplanemanufactureristouse
symbolicmathematicssoftwaretoobtainanequationforthetotalow
throughthehole,
,byintegratingthevelocityacrossthecrosssectionof
thehole:
en,determinethetotalowrateinm
sandm
husingtheproperties
givenabove.
IntheeldofProcessControls,itissometimesnecessarytoperformwhat
iscalledapartialfractiondecomposition.Considertheequation
eprocessofpartialfractiondecompositionrequiresthatwedetermine
theconstants
,and
suchthatthefollowingequationissatised:
s
+
2
)
=
c
s
c
(
s
+
2
)
+
c
Fortunately,SymPyincludesthefunction
thatcan
usuallytakeapartialfractiondecompositionautomatically.Writea
scriptthatdeterminesthevalueof
usingapartialfraction
decomposition.
References
Livio,M.(2006)
eEquationthatCouldntbeSolved:HowMathematical
GeniusDiscoveredtheLanguageofSymmetry
,Simon&Schuster,NewYork,
NY.
Team,S.D.(2014)
SymPy:Pythonlibraryforsymbolicmathematics
,http://
www.sympy.org.
Felder,R.M.andRousseau,R.W.(2005)
ElementaryPrinciplesofChemical
Processes
,JohnWiley&Sons,Inc.,Hoboken,NJ,3rdedn.
Fogler,H.S.andGurmen,M.H.(2015)
ElementsofChemicalReactionEngi-
neering.CompanionCD
,http://umich.edu/~elements/.
6LinearSystems
NoteonNotation
Throughoutthisbook,aboldlowercasevariable(e.g.,
)isusedtorepresenta
vector.Abolduppercasevariablerepresentsamatrix(e.g.,
6.1ExampleProblem
Distillationcolumnsareusedtoseparatemixturesofcompoundsbasedon
dierencesinboilingpoints.edevelopmentofamathematicalmodelof
adistillationcolumntypicallyresultsinhundredsorthousandsoflinearand
nonlinearequations.Letusexploreasimpliedmathematicalmodelfora
distillationcolumnwheretheinputisknown:30kg/sofmethane,25kg/s
ofethane,and10kg/sofpropane.einputmixtureisseparatedintothree
outowstreams:aoverheadstreamthatisrichinmethane(90%)anddoes
notcontainanypropane,amiddlestreamthatisrichinethane(50%)and
abottomstreamthatisrichinpropane(70%).Propaneistheleastvolatile
ofthethreecomponentsinthedistillationcolumnand,hence,isthemost
likelytobeseparatedintothebottomstream.Figure6.1containsadditional
informationonthecompositionoftheoutowstreamsnotethat
usedformassfractions(i.e.,thefractionofatotalstreamthatisaspecic
compound)and
isusedformassowrates(inkg/s).Subscriptsdenote
speciccompoundsmethane(
),ethane(
),andpropane(
),ornumerical
subscriptsrepresentdierentstreamnumbersso
isthetotalmassowrate
oftheentirestream1.
Ideally,distillationcolumnsareoperatedatsteadystate,andeverykilogram
ofeachcompoundthatentersthecolumnismatchedbyakilogramofthat
samecompoundleavingthecolumn.ismustbetrueduetotheconservation
ofmass.Usingthisprinciple,anequationthatequatesthemassowrateof
methaneintothecolumntothemassowrateofmethaneleavingthecolumn
canbewritten.
30kg/s
esecondequationutilizesthefactthatthemassowrateofmethanein
stream1mustequalthetotalmassowrateofthatstream(
)multiplied
bythefractionofthestreamthatismethane(
9).Sincethereare
threeoutowstreams,themassowrateintothecolumnmustequalthe
combinedmassowratefromeachofthethreeoutowstreamsmassmust
beconserved!
Similarly,wecanwritemassconservationequationsonethaneandpropane
also:
25kg/s
6.1ExampleProblem
10kg/s
Figure6.1
Diagramofa
distillationcolumnforthe
m
m
x
x
m
x
x
x
x
x
m
Notethatthereisnopropaneinstream1,butwecanstillincludestream1
inthemassbalanceforpropane(equation6.4)bysettingthefractionofthe
streamthatispropaneto0.0.eresultisanalsystemofthreelinear,alge-
braicequationswiththesamethreeunknowns.Forsimplicity,thesystemof
equations(6.26.4)canbewritteninmatrixformas
Forreview,letususeSymPytosolvefortheowratesofthethreeoutow
streams.
m1,m2,m3=sympy.symbols('m1m2m3')
eq1=0.9*m1+0.3*m2+0.1*m3-30
eq2=0.1*m1+0.5*m2+0.2*m3-25
eq3=0.2*m2+0.7*m3-10
6LinearSystems
SymPysymbolicallysolvesthissmallsystemofequationsandgivesasolution
9kg/s,
0kg/s,and
2kg/s.Itisalwaysagoodideato
checkasolutiontomakesurethattheoriginalequationsareindeedsatised.
Insteadofsymbolicallysolvingthissystem,whichissomethingthatdoes
notscalewelltolargersystemsofequations,letusinsteadsolvethesystem
numerically.WewillusetheNumpylibrary(www.numpy.org)tobuildthe
requiredmatricesandvectorsforthisprocess.erststepistobuildthe
matrix
,andtheright-handside,
,andstorethemasnumpyarrays.
A=numpy.array([[0.9,0.3,0.1],[0.1,0.5,0.2],
)f=numpy.array([30.0,25.0,10.0])
Noticethattheentire
matrixiscontainedinalist,andtheindividualrows
ofthematrixaresublists(ornestedlists)withinthelargerlist.
LineBreaksinPython
Tosolvethissystemofequationsnumerically,weneedtoimportan
additionallibrarythatcontainscommonlinearalgebrafunctions.ere
areanumberoflinearalgebralibrariesavailableforPython,butone
easytouselibraryisdistributedwithnumpyandcanbeimportedusing
.Notethatthislibraryisnotautomaticallyloaded
whenwe
andmustbeimportedseparately.Mostpeopletire
oftyping
over-and-over,soitiscommonto
importthislibraryusingashorternamesuchasnl.isispossibleusing
thecommand
numpy.linalgasnl
.efollowingcodeuses
the
functiontosolvethematrixproblemanddeterminethe
unknownowrates.
numpy.linalgasnl
A=numpy.array([[0.9,0.3,0.1],[0.1,0.5,0.2],
)f=numpy.array([30.0,25.0,10.0])
x=nl.solve(A,f)
eoutputisavectorcontainingthethreeunknownowrates:
45.961.15]
functioncomputestheexactsolutionofawell-
determinedlinearmatrixequation,
.etermexactisinquotes
becausethesolutionisonlyexactuptocomputerround-oerror.Inother
words,thesolutionwilltypicallyhave812digitsofaccuracydependingon
theconditionofthematrix,typeofcomputer,andotherfactors.Methods
thatcomputeanexactsolutiontoalinearmatrixequationarecalled
direct
methods.Inthenextsection,wewillexaminesomeoftheprinciplesbehind
directmethodsanddiscusstheirscaling.Directmethodsareagoodchoice
forsystemsof2to10,000equations(althoughthisrangechangeswith
availablecomputationalpower).ecomputationalalgorithmusedbythe
functionisacommonLAPACKroutinethat
iswritteninFORTRAN.Becausetheunderlyingalgorithmiswrittenina
compiledlanguageinsteadofPython,itismorecomputationallyecientand
scalable.
6LinearSystems
erstfunctionthatwillbewrittenimplementsGaussianelimination.To
illustratetheprocessofGaussianelimination,recalltheexamplematrixprob-
lemfromthedistillationexamplethatwasderivedpreviously.
Inthelinearequationsystem,eachrowof
representsanequation
equality
.Itisperfectlymathematicallyreasonabletomultiplyanentire
equationbyaconstantoradd/subtractoneequationfromanotherwithout
changingthesolution.Ourgoalistomultiplyequationsbyaconstantandthen
addorsubtractequationsfromeachothersothatthelowertriangularpartof
thematrixiszeroinotherwords,wewantamatrixthatisallzerosbelow
themaindiagonal.Forthematrixinequation6.6,themaindiagonalhasthe
values:0.9,0.5,and0.7.Letusstartbyeliminatingthevalueintherstcolumn
thatisdirectlybelowthemaindiagonalthevalueiscurrently0.1.Observe
thatifwemultipletherstequation(row1)by
andthensubtractingthe
resultingequationfromthesecondrow,wewilleliminatethe0.1valueinthe
rstcolumnanddirectlybelowthemaindiagonal.Specically,if
representrows1and2,respectively,thenreplacing
with
gives
46670
Noticethatrow1(representingequation6.6)didnotchangeatall;theonly
changewastorow2.isprocesscannowberepeatedforallnonzeroterms
belowthemaindiagonalaprocesscalledGaussianelimination.esecond
stepwouldnormallybetoeliminatethetermincolumn1,row3,butthatterm
isalready0.0inthisexamplesonoactionisrequired.ethirdandnalstep
forthisexampleistoeliminatethetermincolumn2,row3,whichcurrently
hasavalueof0.2.istermiseliminatedbymultiplyingrow2by
replacingrow3byrow3minusrow2timesthisvalue(i.e.,replacing
with
).Notethatrow1isnotusedinthiseliminationstepbecauseits
usewouldintroduceanonzerovalueintocolumn1,row3ofthematrixa
termthatwasjusteliminatedinstep2.InGaussianelimination,thenonzero
termsbelowthemaindiagonalareeliminatedusingsamerowasthecolumn
wheretermsarebeingeliminated.Forexample,thenonzerotermsbelowthe
maindiagonalincolumn2areeliminatedusingrow2.
AsimilarprocessiscalledLU-decomposition,whichreferstothe
decompositionofamatrixintoalowertriangularmatrix(L)andan
uppertriangularmatrix(U).etermsGaussianeliminationand
LU-decompositionarefrequentlyusedinterchangeably,althoughthey
arenotexactlythesamealgorithm.Forthecurrentexampleproblem,the
resultafterGaussianeliminationis
46670
Examinationofthelinearmatrixsystem6.8showsthatsolvingforthe
vectorofunknowns,
m1,m2,m3],isnowrelativelytrivial.Startingwith
thelastequation,whichisnow0
714,wecaneasilysolve
for
15.Once
isdetermined,itbecomestrivialtosolvefor
4667.isprocessofsolvingforthenalsolution
afterGaussianeliminationisreferredtoasbackwardsubstitution.
PythoncodeforasimpleGaussianeliminationfunction,calledbobcatLU,is
givenbelow.
n=f.size
#checkforcompatiblematrixandrhssizes
(A.shape[0]!=n
A.shape[1]!=n):
("Error!Incompatibleinputsizes.")
6LinearSystems
Backwardsubstitutionisanalgorithmofsimilarcomplexityandisgiven
below.
n=f.size
#Checkforcompatiblematrixandrhssizes
(A.shape[0]!=n
A.shape[1]!=n):
("Error!Incompatibleinputsizes.")
isrequiredatthestartofthele.eresultingleiscalleda
module
inPython
programming,anditcanbeimportedandusedwithothercode.isisa
verysimpleandecientmechanismforrecyclingcode.Asanexample,if
the
algorithmsaresavedinalecalled
,thenthealgorithmscanbeusedtosolvetheprevious
distillationcolumnexampleinastraightforwardmannerasillustratedinthe
examplebelow.
6.2.1DistillationExample
Usethe
functionstosolvethedistillation
columnexampleproblem.
numpyasnp
bobcatSolveasbS
A=np.array([[0.9,0.3,0.1],[0.1,0.5,0.2],
)f=np.array([30.0,25.0,10.0])
A,f=bS.bobcatLU(A,f)
x=bS.bobcatBS(A,f)
esolutionshouldbethesameasobtainedusingthe
function:
[17.9,46.0,1.2]
where
isthepressuredecrease,
theviscosityofblood(4dyn
s/cm
thelengthofthevessel,and
thediameterofthevessel.
6LinearSystems
Figure6.2
Similarly,thePoiseuilleowequationfortheupperandlowerbranchescan
besimpliedto

d
2
Q
Q
P
0
,


L
Table6.1
Propertiesofthefemoralarteryand
upperandlowerbranches.
1(femoralartery)0.410
2(upperbranch)0.288
3(lowerbranch)0.288
Examiningthethreeequations(6.106.12),weshouldnotethattherearefour
unknowns:
,and
,soweneedoneadditionalequationtohavea
solvablesystemoflinearequations.enalequationrecognizesthattheow
throughthefemoralarterymustequalthesumoftheowthroughthetwo
branches,thatis,
,usingtheassumptionthatbloodinthissystem
hasconstantdensity.
esystemofequationsgivenabovefordeterminingbloodowinthethree
vesselscanbewrittenasamatrix,avectorofunknowns,andaright-handside.
Itis
critical
thattheequationsaboveberearrangedslightlybymovingallterms
withunknownstoonesideoftheequalsign.Hence,the
termintherst
threeequationsmustbemovedtotheleftside,andthe
termsinthe
fourthequationmustbemovedtotheleftside.
6370
021210
02121
ePythoncodebelowsolvesthelinearsystemofequationsusingboth
thebobcatLU()functionandthe
function.e
readershouldnotethateventhoughtheoriginalmatrixhasatermonthe
maindiagonalthatisequaltozero,thetermbecomesnonzeroduringthe
GaussianeliminationprocessandthebobcatLU()functiondoesnotgivean
error.
bobcatSolveasbS
numpy.linalgasnl
P0=5000.0
#dynes/cm
L1=10.0
#cm
L2=L3=8.0
#cm
d1=0.4
#cm
d2=d3=0.28
#cm
mu=0.04
#dyn*s/cm
R1=(128*mu*L1)/(numpy.pi*d1**4)
R2=(128*mu*L2)/(numpy.pi*d2**4)
R3=(128*mu*L3)/(numpy.pi*d3**4)
#Unknowns:Q1,Q2,Q3,P1withQinmL/s
A=numpy.zeros((4,4),dtype=numpy.
A[0,0]=R1
A[0,3]=1.0
A[1,1]=R2
A[1,3]=-1.0
A[2,2]=R3
A[2,3]=-1.0
6LinearSystems
A[3,0]=1.0
A[3,1]=A[3,2]=-1.0
f=numpy.array([P0,0.0,0.0,0.0])
A,f=bS.bobcatLU(A,f)
x=bS.bobcatBS(A,f)
esolutionaftersolvingfortheunknownsis
9mL/s,
5mL/sand
3100dyn
s/cm
.esevaluesareconsistentwith
experimentalmeasurements[2].
6.2.3ComputationalCost
Gaussianeliminationandbackwardsubstitutionaremuchmorecomputa-
tionallyecientthansymboliccomputing,butthecomputationalscalability
isstillnotoptimal.Averyroughapproximationofthecomputationalcostcan
bemadebyexaminingtheGaussianeliminationalgorithm.eelimination
ofallnonzerotermsbelowthemaindiagonalrequiresloopingthroughon
theorderof
-columnsand
-rows.Foreachentry,thereareapproximately
-multiplications,sothetotalcomputationalcostisontheorderof
opera-
tions.Itiscommontousetheshorthandnotation:
forsomethingthatis
ontheorderof
Totestthisestimateofcomputationalcost,thecodebelowwasusedto
measurethecomputationaltimeforsolvinganincreasingnumberoflinear
equations.ecodeusesthePythontimelibrarytodeterminethesolve
timebycalculatingthedierencebetweenthestarttimeandstoptimeofa
calculation.eproblemisbasedonadensematrixofrandomnumbers,and
arandomright-hand-sidevector,andthesmallestproblemis100equations
andthelargestis6400equations.
numpy.linalgasnl
mag=4
cputime=numpy.zeros((mag,1))
cpusize=numpy.zeros((mag,1))
inrange
A=numpy.random.random((n,n))
b=numpy.random.random((n,1))
start=time.clock()
x=nl.solve(A,b)
stop=time.clock()
cpusize[i]=nn=n*4
pylab.xlabel('NumberofEquations')
pylab.ylabel('CPUtime(sec.)')
eCPUtimemeasurementsversusnumberofequationsaresummarized
inFigure6.3,whichwasobtainedonaDelllaptopwithaCorei5CPU.e
smallestproblemsize(100equations)requiredonly0.0006s.Assumingthat
thescalingofthealgorithmis
,increasingtheproblemsizebyafactorof4
shouldincreasethecomputationaltimebyafactorof4
64.eobserved
CPUtimeincreaseisclosertoafactorof36whengoingfrom100to400
equations,buttheobservedincreaseisexactlyafactorof64whengoingfrom
1600to6400equations.Forthisparticularcomputer,6400equationsrequired
about1min,whichiswhydirectmethodsarerarelyusedforproblemslarger
thanapproximately10,000equations(unlessthematrixissparse,thatis,
containsmostlyzeros).
scalingofdirectmethodsmotivatesthedevelopmentofalternative
approachesthatgiveupthegoalofobtaininganexactsolutioninexchange
forimprovedscaling.Inthenalsectionofthischapter,iterativemethodsthat
Figure6.3
TheCPUtimerequiredtosolveadensesystemoflinearequationsusing
.If
isthenumberofequations,theCPUtimescaleswith
6LinearSystems
can,insomecases,improvethescalingofCPUtimerelativetoproblemsize
willbebrieyexamined.
-normofaNumpyvector,
,iscalculatedusing
wherethe2denotesthe
-norm.Oneothernormofnotablerelevancetothe
calculationsofinterestinthisbookistheinnitynorm,whichiscalculatedby
ndingtheterminavectorwiththelargestabsolutevalue.einnitynorm
ofaNumpyvectoriscalculatedusing
6.3.2JacobiIteration
Toillustrateourrstiterativemethod,letusreturntothesystemofequations
thatrepresentmassbalancesaroundadistillationcolumn.
30kg/s
25kg/s
10kg/s
Oneapproachtodeterminevaluesfortheunknowns:
,wouldbe
tomakeaninitialguess,forexample,that
20kg/s,
20kg/s,and
20kg/s.isisnotthemostreasonableofguessessincetotalmassisnot
conserved(i.e.,moremassisowingintothecolumnthanoutbasedonour
crudeguess),butthegoalistoillustratethatourguessdoesnotnecessarily
needtobe
really
closetotheactualsolution.Now,letussolvetherstequation
for
,usingourguessforthevaluesof
.Itistrivialtocalculate
anewguessfor
new
44.Repeatingthis
processandsolvingforanewguessfor
usingthesecondequationandthe
oldguessforboth
resultsin
new
38and,nally,
new
usingthethirdequation.Noticethatthenewguessisindeedclosertothe
solutiondeterminedpreviouslyinthischapterthanourinitialguessof20kg/s
foreverystream.Ifwerepeatedthisprocessafewmoretimes,alwaysusing
ourimprovedguess,wemightconvergetowardtheexactsolution.
ePythonscriptbelowwillhelpustoperformthesecalculationsquickly
andautomatically(andwewilllearnsomenewPythonprogrammingpractices
aswell!).
numpy.linalgasnl
jacobi(A,f,x,maxIter=100,tol=1.0e-4):
#inputs
#Aisanxnmatrix
#fisaright-hand-sidevectoroflengthn
#xisinitialguessatthesolutiontoAx=f
#maxIter(optional)ismaximumiterations
#tol(optional)isdesiredaccuracyinterms
#oftheL2-normoftheresidual(=f-Ax)
n=f.size
#Beginbycheckingforcompatiblesizes
(A.shape[0]!=n
A.shape[1]!=n):
("Error!Incompatiblesizes.")
6LinearSystems
x=numpy.copy(xnew)
(Failedtoconvergeafter,
iterations)
dividedbythecoecientalongthediagonal.ereaderisencouragedto
revisittheprocessdescribedaboveforsolvingforoneunknownforeachofthe
massbalanceequationsandtoobservetheconnectiontotheJacobiiteration
inthePythonscript.Ifthedesiredsolutiontoleranceisnotachievedafterthe
maximumnumberofiterationshasbeenreached,thefunctionprintsanerror
messageandsimplyreturnsthe(incorrect)vector
afterthenaliteration.
TestingtheJacobiiterativemethodonthedistillationcolumnmassbalances
resultsin14iterationsbeingrequiredtoachievethedefaulttoleranceforthe
-normoftheresidual.Itisinterestingtotestthemethodwithdierentinitial
guessesforthesolution.Forexample,ifourinitialguesshadbeen
x=numpy.array([10.0,10.0,10.0])
thealgorithmwouldhaverequired16iterationstoachieveasolutionsatisfying
thesametolerance.
6.3.3GaussSeidelIteration
eJacobiiterationcalculatesanewguessforthevector
based
only
thepreviousguess.Itisthereforepossibletocomputeeachentryinthenew
guessvectorsimultaneously.Anobviousalternativetothisapproachisto
calculateanewvaluefortherstentryintheunknownvector
butthenuse
thisnewvalueforcalculatingthesecondentryinthevector
.Continuingin
thismanner,eachnewvaluein
isalwayscalculatedusingthemostrecent
informationavailable.Fortherstiterationofthedistillationcolumnexample,
thecalculationof
4wouldbeidenticaltotheJacobiiteration,butthe
calculationof
usingthenewvaluefor
wouldresultin
11instead
eimplementationoftheGaussSeideliterationisnearlyidenticaltothe
implementationoftheJacobiiteration,exceptthevector
isnolonger
requiredsinceallcalculationsinvolveonlythemostrecentinformationthat
isalreadystoredin
.ePythonscriptthatimplementstheGaussSeidel
iterationforthedistillationcolumnmassbalancesisgivenbelow.
6LinearSystems
numpy.linalgasnl
gaussSeidel(A,f,x,maxIter=100,tol=1.0e-4):
#inputs:
#Aisanxnmatrix
#fisaright-hand-sidevectoroflengthn
#xisinitialguessatthesolutiontoAx=f
#maxIter(optional)ismaximumiterations
#tol(optional)isthedesiredaccuracyinterms
#oftheL2-normoftheresidual(=f-Ax)
n=f.size
#Beginbycheckingforcompatiblesizes
(A.shape[0]!=n
A.shape[1]!=n):
("Error!Incompatiblesizes.")
wererequired).eGaussSeideliterativemethodtypicallyconvergeswith
signicantlyfeweriterationsandoveragreaterrangeofinitialguessesthan
theJacobiiterativemethod.eonlyreasontouseJacobiiterationsinstead
ofGaussSeideliterationsisthatcalculationsinvolvedintheJacobiiteration
maybeexecutedinparallel,whichmayresultinshortercomputationaltimes
onsomecomputerarchitecturesevenwithalargertotalnumberofiterations.
calculation:
=(f[i]sum)/A[i,i].Informally,thisequation
tellsusanewvaluefor
thatis(hopefully)betterthanthepreviousguess.
Forsomeproblems,thisupdatedvaluefor
mightmovetheunknown
toofarandourapproximatesolutionmightstarttodivergefromthecorrect
solution.Inthiscase,thefollowinglineofcodeinthealgorithmforcalculating
anewvaluefor
mightprovidegreaterstability:
x[i]=(1.0-omega)*x[i]+\
omega*(f[i]-
where
issettoavaluebetween0.0and1.0.ischangetothe
GaussSeideliterationresultsinanewguessat
thatisequaltoa
weightedaverageoftheoldguessplusanupdatedguess.isapproachis
calledunder-relaxation,anditcanhelpwithstabilityatthecostofpotentially
slowingconverge.Sometimes,theconvergenceisdramaticallyslowerand
23timesasmanyiterationsarerequired.
Similarly,onecanimaginesituationswherewewishtotrytomovefaster
towardthesolution.Ifthenewguessfor
isreallyamuchbetter
guess,maybeweshouldtrytomoveevenfurtherinthatsamedirection.
Usingthesameupdatedlineofthealgorithmasunder-relaxationused,
6LinearSystems
fromtheright-handside(notethat
istheidentitymatrix,which
isofthesamesizeas
butjusthasonesonthediagonalandzeroseverywhere
else),giving:
.isequationcanberearrangedtogive
apotentialiterativemethod:
new
.Itturnsoutthatthisisa
reallyslowiterativemethodthatshouldneverbeused.
WecanusethissameframeworktowritedowntheJacobiiteration.Webegin
bydecomposing
intoamatrix
thatjusthasthemaindiagonaltermsfrom
withzeroeverywhereelse,amatrix
thatjusthasthevaluesfrom
thatare
inthelowertriangularsectionstrictlybelowthemaindiagonal,andamatrix
thatcontainsthevaluesfrom
thatareabovethemaindiagonal.Withthese
newmatrices,wecanrewrite
.Anexampleofthisdecomposi-
tionforourdistillationexampleis
RecallingthatintheJacobiiteration,alltheodiagonaltermsin
were
eectivelymovedtotherightsideoftheequationandwethendividedbythe
diagonaltermsof
,theJacobiiterationcanbewrittenas
new
Usingthesamestrategy,theGaussSeideliterationcanbewrittenas
new
Ineithercase,wearerequiredtocalculatetheinverseofamatrix,
,whichisnormallythesamecomputationalcostasGaussEliminate
(i.e.,order
operations)butisveryinexpensiveforthetwomatriceslisted
herebecausetheyarestrictlydiagonalorlowertriangular(i.e.,thesamecostas
backwardsubstitution,order
orless).Asaresult,eachiterationisrelatively
inexpensivefromacomputationalstandpoint.
erateatwhichtheseiterativemethodsconvergedependsonhowwell
the
preconditioner
forJacobiandGaussSeidel,respectively,
approximates
.Ifthediagonalmatrix,
,containsthelargesttermsin
,then
isagoodpreconditionerandconvergenceisrapid.Ifthelargest
Problems
magnitudetermsarenotalongthemaindiagonal,thatis,notcontainedin
thenitisapoorpreconditionerandconvergenceisunlikely.
eeldofiterativemethodsforsystemsoflinearequationsisverybroad,
andthepresentationofconvergenceratehereisverysimplied.erearea
numberofiterativemethodssuchastheconjugategradientmethod,Krylov
methods,andmultigridmethodsthatarebeyondthescopeofthisbook.
However,inallcases,theavailabilityorabsenceofagoodandinexpensive
preconditionerhasasignicantimpactontheperformanceofthemethod.
Interestingly,onecommonpreconditionistouseGaussianeliminationbut
tothrowawayanysmalltermsthatariseduringthecomputations.isis
referredtoasincompleteeliminationanditprovidesarobustandinexpensive
preconditionerforsomeproblems.Formoreinformationoniterativemethods,
theinterestedreaderisencouragedtoreadthefollowing:
NumericalAnalysisbyBurdenandFaires[3]
IterativeMethodsforSolvingLinearSystemsbyGreenbaum[4]
AMultigridTutorialbyBriggs
year
)bytheconcentration(in
Lookingatthediagrambelow,wewriteabalance(
)oneachlake.
ForLakeSuperior,
year
year
year
year
year
year
Beginbyderivingthevebalancesonthevelakes(note,twoofthem
appearabove).en,solvethesystemofvelinearequations(notby
hand,butusingoneofthemethodscoveredinthischapter)fortheve
unknowns(
,and
).Notethatyouwillneedtorearrange
theequationssothatthetermswithunknownsallappearononesideof
theequation.ReporttotheEPAtheconcentrationsitisseeking.
6LinearSystems
Superior
C
C
C
Q
Q
Q
Q
Q
C
eEPAisalsoconsideringabypassstreamthatwouldgodirectlyfrom
LakeMichigantoLakeOntariowithaowrateof20
year
inorderto
reducetheconcentrationofPCBsinLakeMichigan.isbypassdoes
notchangeanyoftheexistingowrates,itwouldjustbeanadditional
owoutofLakeMichiganandintoLakeOntario.Reportthepotential
impactofthebypass.
Anewtypeofchairforaskilifthasbeendeveloped,andthemanufacture
hasdesignedasimpliedmodel(Figure6.4)ofthechairsbehaviorupon
loadingagroupofindividuals.Recallthatthebasicspringequationis
where
istheweightorforceappliedtothespring,
thespringconstant,
thedisplacement(orstretch)ofthespring.
Youhavebeenhiredtocalculatethetotaldisplacementofthesystemof
springsandweightsshowninFigure6.4.epropertiesofthesystemare
asfollows:
10,000.0N
5,000.0N
8,000.0N
3,500.0N
4,500.0N
500.0N
1,000.0N
1,000.0N
Problems
Figure6.4
Asystemofweightsandlinear
springsthatmodelanewchairliftdesignfor
skiresorts.
erststepistoderiveaforcebalanceoneachweight.Fortherst
weight,
,youneedtoconsidereveryspring
touchingtheweight
includingthedisplacementanddirectionofforce:
andforthesecondweight:
Afterderivingtheforcebalanceforthethirdweight,thesystemof
equationscanbewrittenasalinearmatrixproblem:
???
Ifthelinearmatrixhasbeenderivedcorrectly,thetermsalongthemain
diagonalwillallbepositive,o-diagonaltermswillbenegative,andthe
matrixwillbesymmetric.Solvethelinearmatrixproblemanddetermine
thedisplacementofeachweight(i.e.,eachskier)in
6LinearSystems
= 20 g
= 0 g
Figure6.5
Athree-stage,countercurrentcascadewhereallthelowerstreamshaveaow
rateof10m
minandallupperstreamshaveaowrateof20m
min.Thefeedstream
withthecompoundofinterestisfedintotherst(left)stageataconcentrationof20g
CountercurrentcascadesarecommoninChemicalEngineeringfor
theextractionofasubstancefromonestreamintoanotherstream.A
three-stagecountercurrentcascadeisshowninFigure6.5.Eachstage
inthecascadeisbasicallyamixingtank,andwewillassumethatboth
streamsleavingastagehavethesameconcentration(i.e.,boththeupper
andlowerstream
leaving
stage1haveaconcentrationof
Youhavebeenhiredbyalocalenvironmentalengineeringrmtoanalyze
theeectivenessofthecascadeshownbelow.Youshouldbeginbywriting
amassbalanceoneachstagebecauseforeachstagethetotalmassofthe
contaminantcoming
mustequalthemassofcontaminantgoing
.In
otherwords,gramsperminofcontaminantcominginequalsgramsper
minuteofcontaminantgoingout.Lookingatstage1,therearetwoinput
streams,sothetotalcontaminantcominginis
Total
20m
Ifweincludethetotalgoingouttothisequationandreplacetheknown
variables,weget
10m
20g/m
20m

inputsing/min
10m
20m

outputsing/min
andforthesecondstagebalance,weget
10m
20m
10m
20m
Problems
Afterderivingthemassbalanceforthethirdstage,thesystemofequations
canbewrittenasalinearmatrixproblembymovingalltermscontaining
anunknown,
totheleftsideoftheequalsign,andalltermswithoutan
unknowntotherightside,giving:
20200
2020
???
Ifthelinearmatrixhasbeenderivedcorrectly,thetermsalongthemain
diagonalwillallbenegativeando-diagonaltermswillbepositive.Solve
thelinearmatrixproblemanddeterminetheconcentrationineachstage
ofthecascadeing
Usethe
functionstosolvethelinear
matrixsystem:
Further,comparethesolutiontothesolutionobtainedusing
.Inbothcases,thesolutionshouldbe
1.0,1.0,3.0].6.5Usethe
Gaussianeliminationalgorithmonthefollowing
linearmatrixsystem:
esimpleGaussianeliminationalgorithmimplementedin
failswiththismatrixbecauseduringtheeliminationprocess,azerois
producedonthemaindiagonal,whichinthebestcasetriggersanerror
messagestatingthatpivotingisrequired,andintheworstcasecausesa
programcrash.
Rewritethe
algorithmtousepivotingtoavoidsuch
diculties.Adescriptionofapivotingalgorithmcanbefoundina
numberofnumericalmethodsbooksincludingthebookbyBurdenand
Faires[3].
6LinearSystems
References
Zamir,M.(2016)
Hemo-Dynamics,
BiologicalandMedicalPhysics,
BiomedicalEngineering,SpringerInternationalPublishing,Heidelberg.
Fung,Y.(1984)
Biodynamics:Circulation
,Springer-Verlag,NewYork.
Burden,R.andFaires,J.(2001)
NumericalAnalysis
,Brooks/Cole,Pine
Grove,CA,7thedn.
Greenbaum,A.(1997)
7Regression
7600
(a)(b)
8102030405060708090
Figure7.1
where
areconstantsthatmustbedeterminedfromexperimentalmea-
surements.LookingatthedatashowinFigure7.1(b)itmaynotbeapparentthat
wecouldeveruselinearregressiontotthedata,buttheClausiusClapeyron
relationshipshowsustheway.Recallingthattheequationforalineis
wenoticethatifweplotln
(insteadofjust
)andifweplot
insteadof
,thedatashouldbeapproximatelylinear,allowingustotthedatawitha
7.3LinearRegression
0.00300.0032
1/temperature (K
0.00340.0036
Figure7.2
Whenthenaturallogofvaporpressure,ln
,isplottedagainsttheinverse
temperature,1
,thenthepointsfallonastraightlineandlinearregressioncanbeusedto
tthedata.
lineanddetermine
.Whenvaporpressuredata,suchasthatshown
inFigure7.1(b),isreplottedaftertakingthenaturallogofthevaporpressure
andtheinverseoftemperature,thedatafallsonanapproximatelystraight
lineasshowninFigure7.2.Oncetheconstantsareknown,wecanusethe
ClausiusClapeyronrelationshiptodeterminethevaporpressureatany
temperaturewedesireor,equivalently,todeterminethetemperatureatwhich
adesiredvaporpressurecanbeachieved.
7.3LinearRegression
Tobegintheregressionprocess,weneeddata.Fornow,assumethatwehave
datapointsthatconsistofoneindependentvariable,
,andonedependent
variable,
,sothereare
datapoints:
.egoalistoobtain
anequationforapolynomial,
,thatapproximatelymatchesthedata.
erststepistheselectionofameasurethattellsushowwellthepolynomial
matchesthedata.Forexample,wemaywanttominimizetheabsolutionvalue
ofthedistancebetweenthepolynomial,
,andthedata,
.Ifthisisthegoal,
thenweneedtominimize:
abs
.(7.1)
7Regression
Inpractice,itisdiculttominimize
becausethederivativeoftheabsolute
valuefunctionisnotdenedattheorigin.Least-squaresregressionisaneasier
andmuchmorecommonchoice.Itisbasedonminimizing:
.(7.2)
Inordertounderstandtheprocessofminimizing
,letusassumeforsimplic-
itythatweareinterestedinlinearpolynomials,thatis,
.(Itis
relativelystraightforwardtoextendthisanalysistohigher-orderpolynomials,
butthisisoftenunnecessarybecausehelpfulsoftwarehasbeendevelopedto
automatetheprocess.)
egoaloflinearregressionanalysisistodeterminethetwounknownsin
theequationfortheline:
thatminimize
.Asweprobablylearnedin
oneofourcalculuscourses,thelocalminimumofafunctionoccurswhenthe
derivativeisequaltozero.Further,since
isaquadraticfunction,thelocal
minimumisalsotheglobalminimum.Takingthederivativeof
withrespect
tothetwounknownsgives
c
=
1
y
c
c
E
Simplifyingtheseequations(notethataconstantcanbefactoredoutofasum)
gives
,(7.3)
,(7.4)
whicharecommonlyreferredtoasthenormalequations.Recallthat
are
known
orgivenbythedata.eonlyunknownsare
.Intermsof
theseunknowns,thenormalequationsarealinearsystemofequationsandcan
berewrittenasamatrixproblem:
.(7.5)
Fortunately,welearnedhowtosolvelinearmatrixproblemsintheprevious
chapter.
APythonscriptthatperformslinearregressiononeightdatapoints,which
aregivennearthestartofthescript,isshownbelow.
7.3LinearRegression
numpyasnp
numpy.linalgasnl
x=np.array([0.1,1.43,2.86,4.29,5.71,
7.14,8.57,9.95])
y=np.array([2.33,2.81,3.84,4.41,4.31,
5.65,5.68,6.80])
(n!=y.size)):
("Error:inconsistentnumberofdatapoints")
Xsum=np.
Ysum=np.
XYsum=np.
X2sum=np.
A=np.array([[n,Xsum],[Xsum,X2sum]])
f=np.array([Ysum,XYsum])
c=nl.solve(A,f)
yLR=c[0]+c[1]*x
escriptbeginsbyconstructingtwonumpyarraysthatcontainthedata.One
array,
,containstheindependentvariableandtheotherarray,
,containsthe
dependentvariabledata.escriptthencheckstomakesurethatthearrays
arethesamesize.Ausefulnumpyfunction,
,isusedtosumthe
entriesinavector.esesumsarestoredindierentvariablesforuselaterin
constructingthematrix,
,andtheright-handside,
asdenedinequation7.5.
elinearsystemissolvedusingthesolverincludedwithnumpy.Inorder
toplottheregressionline,thesolutiontothelinearproblem,whichcontains
thecoecientsforthelinearpolynomialttothedata,isusedtocalculate
theapproximatevalueofthedependentvariableateveryindependentvari-
ablepoint.eoriginaldatafortheexampleproblemisshowninFigure7.3(a),
andthedatawiththelinearregressioncurveisshowninFigure7.3(b).Plotting
dataasseparatepointsandpolynomialtstothedataasasolidlineisstandard
practiceandhighlyrecommended.
edataforthepreviousexamplewasobtainedbyrstselectingevenly
distributedpointsfromastraightline:
5andthenaddingrandom
noisetothedatasothatitdidnotallfallonastraightline.Interestingly,
7Regression
6810024
(a)(b)
6810
Figure7.3
(a)Sampledatausedinregressionanalysisand(b)thesamedatawiththe
calculatedregressionline.
theregressionlineobtainedusingleast-squareslinearregressionwas
33.Becauseonlyeightpointswereusedandtherandomnoise
wasnotevenlydistributedoversuchasmallnumberofpoints,theoriginalline
wasnotcompletelyrecovered.Additionaltestingshowedthattheslopecould
bereliablyrecovered(approximatelytwosignicantdigits)withabout20data
points,butrecoveringanaccuratevaluefortheinterceptwasmoredicult
andrequiredatleast100datapointstoobtainapproximatelytwosignicant
digits.
QuadraticRegression
Repeatingthederivationofthenormalequationsandlinearmatrixprob-
lemassociatedwiththeleast-squaresregressionofaquadraticpolynomial:
gives
7.3.1AlternativeDerivationoftheNormalEquations
Recallthatthegoalinpolynomialregressionanalysisistodeterminethe
polynomialcoecientsthatgivetheoptimaltto
datapoints.isrequires
thatthenumberofdatapointsbegreaterthanthenumberofcoecientsin
7.4NonlinearRegression
thepolynomial.epolynomialwillhavetheform:
Since
,wecanthinkofthispolynomialateachdatapointbeing
writtenasalinearmatrixproblem:
where
Noticethat
arebothgivenbythedatathatisbeingt.Inregression,
thegoalistominimize
,(7.8)
whichisthe
-normoftheresidualsquared.
isminimizedwhenthederiva-
tiveiszero,whichleadsto
Uponrearrangement[1],weobtainthenormalequationsinmatrixform:
.(7.9)
eresultisthesamelinearmatrixproblemthatwasderivedpreviouslyusing
themorecommonapproach.
7.4NonlinearRegression
ettingofdatabyanonlinearfunctionthroughaleast-squaresminimiza-
tionprocesscanbedicultbecausetheprocessleadstoasystemofnonlinear
equationsthatmustbesolved.esolvingofnonlinearequationsisthefocus
ofthenextchapter,butafewwordsontheprocessareimportanthere.First,
nonlinearregressionrequiresthattheusersupplyaguessforthevaluesofthe
unknownparametersinthefunctionthatisbeingttothedata.Ifdataisbeing
ttoafunctionlike
,where
istheindependentvariable,thenthe
usermustsupplyaninitialguessfortheamplitude,
,andtheperiod,
,ofthe
data.enonlinearsolutionprocesstendstobemuchmorelikelytoconverge
toafunctionthatoptimallytsthedatainaleast-squaresnorm(equation7.1)
iftheinitialguessesforthefunctionparametersarerelativelyclose.esecond
factorthatimpactsthesolutionprocessistheaccuracyofthedata.Datawith
signicantlevelsofnoiseisunlikelytoleadtoanonlinearregressionsolution
forhighlynonlinearfunctions.
Arelativelyrobustnonlinearleast-squaresregressionroutineisincluded
withtheScipylibrary,itisthe
function.
7Regression
isfunctionisbasedontheLevenbergMarquardtalgorithm[2]for
nonlinearregression.ecalltothisfunctionhastheform:
scipy.optimize.curve_fit(func,xdata,ydata,p0,sigma)
andthefollowingparametersarepassedintothisfunctionwhenitiscalled:
func
:isisauser-denedfunctionthatisdeclaredbeforecurvetiscalled.
efunctionmusttaketheindependentdataasinput(i.e.,xdata)andtheval-
uesforthedierentunknownparametersthatarebeingdeterminedinthe
ttingprocess.efunctionmustreturnavector,
,containingthedepen-
dentvariable.ecurve_tfunctionchangesthevaluesoftheunknown
parameterstominimizethedierencebetweenthefunctionreturnvalues
andthedependentvariabledata,ydata.
xdata
:avectoroftheindependentvariabledatapoints.
ydata
:avectorofthedependentvariabledatapoints.e
-valuesreturnedby
thefunction(func)arecomparedtothisdatausingtheleast-squarenorm
(equation7.1).
:aPythonlistcontainingguessesfortheunknownfunctionparameters.
Adefaultvalueof1isusedforallparametersifnoinitialguessisprovided.
Useofthedefaultvalueisdiscouraged.
sigma
:an
optional
vectorthatisusedtoproviderelativeweightsforthe
least-squaresprocesses.Ifthegoalistotsomeofthedatapointsmore
closelythanotherdata,alargerweightcanbeappliedtothosedatapoints.
isvectorisrarelyprovided.
euseofthe
functionisillustratedthroughthePython
scriptbelow.
n=20
#AntoinecoefficientsforwaterfromWikipedia
A=8.07131
B=1730.63
C=233.426
#Buildsomefakedata:temperature,x,versus
#vaporpressure,y,data
x=numpy.linspace(20,90,num=n)
error=numpy.random.rand(n)
y=numpy.zeros(n)
inrange
7.4NonlinearRegression
y[i]=A-B/(x[i]+C)
y[i]=(10**y[i])+50*(error[i]-0.5)
#FunctionforAntoine'sequationusedincall
#tocurvefitbelow.
antoine(temp,a,b,c):
n=temp.size
p=numpy.zeros(n)
inrange
p[i]=10**(a-b/(temp[i]+c))
curve)thatoptimallytsthedatarequiresasetof
valuesthatfallon
thecurve.Intheexampleabove,the
-dataisalsousedforplottingthecurve,
7Regression
2030406080
(a)(b)
708090
2030406080
708090
Figure7.4
(a)Sampledatausedinnonlinearregressionanalysisand(b)thesamedatawith
thenonlinearregressionlinefrom
butnew
-dataisobtainedusingtheoptimalparametervaluesfromthe
vectorandthefunctionthatwasusedinthetting(
AsampleresultusingthescriptaboveisshowninFigure7.4.Repeated
experimentationwiththescriptanddierentsetsoffakedataprovidedsome
interestingobservations.First,theAntoineparametersfrom
oftendieredfromtheactualparametersby10%ormorewheneversignicant
quantitiesofnoisewereaddedtothefakedata.Second,the
functionoftenfailedtoconvergetoaleast-squaresminimizeriftheerror
levelinthedataexceededroughly10%.Addingsmallamounts(0.1%)of
noisetothedatagaveaccuratevaluesfortheAntoineparametersand
alwaysledtoconvergenceby
,butlargeramountsofnoise
gavepoorresultsornoresults.Finally,largequantitiesofdata(50or
morepoints)gavebetterresultsthansmallquantitiesofdata(10orfewer
points).
7.4.1LunarDisintegration
ebookSevenevesbyNealStephensonisasciencectionnovelthatbegins
withanevent,causedbyanunknownagent,thatcausesthemoontobreak
apartintosevenlargefragments.Relativelyquickly,scientistsinthebookrec-
ognizethatthesevenlargefragementswillcollidewitheachotherandcause
thelargefragmentstobreakapartintosmallerfragments.Asmorefragments
areformed,thepotentialforcollisionandfurtherfragmentationincrease.is
systemexhibitsaclassicexponentialgrowthcurvegrowth(anincreaseinthe
numberoffragments)inonegenerationcausesfastergrowth(afasterrateof
newfragmentformation)forthenextgeneration.Inthebook,scientistrecog-
nizethatthisfragmentationprocesswouldcontinueuntiltheoriginalmoon
wasbrokenapartintoanincrediblylargenumberofsmallfragments.Someof
7.4NonlinearRegression
Table7.1
Estimatesofthenumberoffragmentsoftheformermoonasa
functionofthenumberofdayssincetheinitialevents.
Day:0728100200300
Fragments:782035012,000500,000
thefragmentswouldreachearthduetotheearthsgravity,andthesefragments
wouldimpactthesurfaceorburnupintheatmosphere.Duetotheincredibly
largenumberoffragmentsthattheformermoonwouldproduce,theearths
surfaceandatmospherewouldheatupbeyondthepointwherelifeofanyform
wouldsurvive.
etextofthebookdoesnotclearlystatetheexactnumberoffragments
thatthemoonhasbeenreducedtoasafunctionoftime,butthroughacare-
fulanalysisofthetext,thedatainTable7.1hasbeenestimatedandincludes
thenumberoffragmentsasafunctionofthenumberofdaysfromtheinitial
event(day0).Ourgoalistotthefragmentnumberversusdaydatawithan
exponentialcurveoftheform:
where
isthenumberoffragments,
istheday,and
areunknown
parameters.
ePythonscriptbelowusesthe
functiontodeterminethe
valuesfor
thatresultintheexponentialcurvetobesttthefragmenta-
tiondata.
matplotlib.pyplotasplt
day=numpy.array([0,7,28,100,200,300])
chunks=numpy.array([7,8,20,350,12000,500000])
#FunctionforBreakupequation
#usedincalltocurve_fitbelow.
breakup(x,a,tau):
f=a*numpy.exp(x/tau)
7Regression
#andcovarianceofparams,ignoredwith_
fit,_=curve_fit(breakup,day,chunks,p0=params)
moreDays=numpy.arange(0,700)
yfit=breakup(moreDays,fit[0],fit[1])
#plotdataaspointsandfitasaline
plt.semilogy(day,chunks,'o',moreDays,yfit)
plt.xlabel('Dayaftertheevent')
plt.ylabel('Numberofchunks')
plt.plot(day,chunks,'o',moreDays,yfit)
plt.xlabel('Dayaftertheevent')
escriptbeginsbystoringthefragmentationanddaydatainnumpyarrays
andthentheexponentialfunctionthatistotthedataisdened.e
functioniscalledby
fewlinesbelow,andthe
functionneedstobewrittentoreceive
three
arguments:anumpy
vector
,andtwoscalarparameterstobet,
.Apossiblymorerobustapproachtowritingthe
functionistowritea
looptoiteratethroughthevaluesin
,butthemore
compactformshownhereispossibleduetotheuseofthe
function.
Asdescribedearlier,the
functionreturnstwodatastruc-
tures:(1)theoptimalvaluesofthettingparameters,storedinthevariable
above,and(2)covariancevaluesfortheparameters.Forthisexample,we
arenotinterestedinthecovariancevaluesfortheparameters,sowetemporar-
ilystoretheminthevariable_,whichisastandardvariableinPythonfor
temporarystoringsomethingthatyoudonotplantouse.eresultoftting
thefragmentationdatawithanexponentialcurveisshowninFigure7.5.e
exponentialfunctionttingthedatais
Notethata
plotisusedduetothelargevariationinthenumber
offragmentsovertime.
enalsectionofthecodegeneratesaplotthatconsistsoftwosubplots,
andinbothcases,theexponentialfragmentationcurveisextrapolatedbeyond
thedatathatisavailable.edataendsatday300,butthecurvesshownin
Figure7.6extrapolatethecurveouttoday700.Extrapolationrequiresthatwe
constructanewvectorcontainingdaysoutto700,andthisisachievedusing
the
function.e
functionisthenusedto
7.4NonlinearRegression
100150
Day after the event
200250300
Figure7.5
Fragmentationversusdayaftertheeventdataisshownasdescretepoints,
andtheoptimalexponentialcurvettingthedataisshownasasolidline.
0100200300400
Day after the event
5006007000100200300400
Day after the event
(a)(b)
500600700
1e12
1.6
1.4
1.2
1.0
Figure7.6
Fragmentationversusdayaftertheeventdataisshownasdescretepoints,and
theoptimalexponentialcurvettingthedataisextrapolatedoutto700daysandshownas
asolidline.
generateaplotwithtwosubplots,thesubplotontheleftusesalog-scalefor
the
-axis(using
)andthesubplotontherightusesaregular,linear
-axistoemphasizetheexponentialbendinthenumberoffragmentsasday
700isapproached.
7Regression
7.5MultivariableRegression
elinearandnonlinearregressionproblemsthathavebeenexaminedthus
farinthechapterallhaveasingleindependentvariablethatischanging.
Forexample,thevaporpressurewasonlyafunctionoftemperature.e
numberoflunarfragmentswasonlyafunctionoftime(ordays)sincethe
initialfragmentationevent.Inmostengineeringproblems,ourgoalisa
fundamentalunderstandingofasystemandthatmeanstryingtochangea
singlevariableandobservingtheresponseofthedependentvariable.However,
sometimesthisisimpossibleandtherearemultiplevariablesindependently
changing.Forexample,myheartrateisafunctionofactivityleveland
caeineconsumptionanditmaynotbepossibletovaryactivityandcaeine
consumptionseparately.efocusofthisnalsectiononregressionistting,
forexample,anoutputvariable,
,thatdependsonmultipleinputvariables,
,withafunctionoftheform:
.isexamplefunction
hastwoindependentvariablesanditis
linear
7.5MultivariableRegression
150,160,170,180])
conc=numpy.array([40.,40.,40.,40.,
50.,50.,50.,50.,
60.,60.,60.,60.,
70.,70.,70.,70.])
x=numpy.stack((temp,conc),axis=1)
fracYield=numpy.array([70.,72.,74.,77.,
64.,66.,69.,71.,
49.,55.,57.,58.,
46.,48.,53.,55.])
rxnFit(x,a,b,d):
f=a*x[:,0]+b*x[:,1]+d
7Regression
0246810121416
Figure7.7
Abarchartshowingpairsofbarsforeachexperimentalcondition(temperature
andpressure).Thedarkerbarontheleftistheyieldfromtheoriginaldata,andthelighter
barontherightistheleast-squaresregressiontatthesameconditions.
thatholdstheconditions(temperature,pressure,valveposition,concen-
tration,opacity,humidity,etc.)atalargenumberoflocationsthroughouta
chemicalprocessingfacility.Anotherexampleiscompleterecordsofeach
consumerspurchasesatagrocerystoreoveramultiyearperiod.eeld
ofmachinelearningisfocusedonthedevelopmentofalgorithmsthatcan
ttheselargedatasetsinordertopredictoutputs.Forthechemicalpro-
cessingexample,theoutputmightbethepurityofthenalproduct,and
themachinelearningalgorithmcanpredictpuritybasedonafullsetof
inputconditions,includingtheidenticationofthemostimportantinput
conditions.
Probably,themostfundamentaltoolusedinmachinelearningismultivari-
ableregression.Intheeldofmachinelearning,thetermfeatureisused
insteadofvariable,andthetermweightisusedinsteadofparameter,but
manyofthebasicideasarethesameasthemultivariableregressionproblem
solvedabove.Toillustratetheconnectionbetweenmachinelearningandmulti-
variableregression,letusconsideroneofthemostfamousproblemsinmachine
learning:numericalcharacterrecognition.isprobleminvolvesscanninga
singledigit(09)fromahand-writtennumber,thusconvertingitintoanimage
orsetofpixels.en,basedonthecolor(white,gray,orblack)ofthepixels,
determinewhatnumber(09)isshownintheimage.isisaprettysimpletask
forahumanbutverychallengingforacomputer.eapproachusedinmachine
Problems
learningistostartwithalargecollection(
1000)ofimagesshowingdigits
(09)thathavealreadybeenidentiedbyaperson.ecolorofeachpixelinthe
imageisrepresentedusingavectorofnumbers,
,andmachinelearningalgo-
rithmsuseregressiontondavectorofweights,
suchthat
where
isthepreviouslyidentiednumber.isisjustalinearregressionproblem.
Wenormallythinkoflinearregressionas:givenasetofpoints,
ndthe
parameters
ofalinesothattheline
isagoodtfordeter-
foranygiven
.echaracterrecognitionproblemisverysimilar:nd
sothat
foranygivenvectorofpixelcolors,
Currentchallengesinmachinelearningincludethechallengeofgettinga
computertoansweraquestion.isproblemusuallystartswiththeprocess
ofconvertingeverywordinalanguageintoanumber.en,wewishtond
thesetofweights,
suchthat
where
isthevectorofnumbersthat
representthewordsinthequestionand
istheanswer!Ifwehavealargeset
ofquestionsandanswers(i.e.,alargesetofquestions:
andanswers
),itmay
bepossibletondthedesiredweights(
).Finally,itisimportanttonotethat
theeldofmachinelearningsignicantlyextendsbeyondmultivariablelinear
regression.Fortheinterestedindividual,theMachineLearningcoursethrough
Coursera(https://www.coursera.org/learn/machine-learning)isoneopportu-
nitytolearnmore.
Problems
Lookuptheaveragemonthlyhightemperatureforyourhometown.Start-
ingwithApril,listtheaveragemonthlyhightemperatureforthenext12
months.en,tthisdatawithafunctionoftheform:
where
isthemonth(startingwithAprilaszeroandcontinuingtoMarch,
month11)and
,and
areunknownparametersthataretobedeter-
minedtottheaveragemonthlyhighinaleast-squaresminimization.
YouhavebeenhiredbyNASAtoperformaregressionanalysisonthe
GLOBALTemperatureIndexdataset(originaldata:http://data.giss.nasa
.gov/gistemp/tabledata_v3/GLB.Ts+dSST.txt).Usingthedatainthetable
below,completealinear(
),quadratic(
andexponential(
)regressionanalysisoftheaverageglobaltem-
perature(
)versusyear(
).Useofthe
functionfromthe
ScipylibraryisacceptabletoNASA.Commentonthequalityofthetfor
thevariousregressionoptionsconsidered.
7Regression
Year
Averageglobal
temperature(
Approximatenumber
ofpirates
189013.820,000
190013.718,000
191013.716,000
192013.615,000
193013.89000
194013.95000
195014.03000
196014.02000
197014.01100
198014.0400
199014.220
200014.417
201014.615
Ithasbeensuggestedthattheriseinglobalaveragetemperatureisdueto
thedeclineinthenumberofpirates.Youhavealsobeenaskedtoperform
alinearandquadraticregressionanalysisontheapproximatenumberof
piratesintheworld(
)versusyear(
).Finally,commentonthecorrela-
tionbetweentheapproximatenumberofpiratesintheworldversusthe
averageglobaltemperaturedata.
YouhavebeenhiredbythepublisherofaChemicalEngineeringrefer-
encemanualtoperformaregressionanalysisonHeatCapacityofsolid
HydrogenBromidedata[3].Usingthedatainthetablebelow,complete
alinear(
),quadratic(
),andcubic(
)regressionanalysisoftheHeatCapacity(
)ver-
sustemperature(
).Useofthe
functionfromtheScipy
libraryisacceptabletothepublisher.Commentonthequalityofthet
forthevariousregressioncurves.
Temperature(K)
Heatcapacity
K))
118.9910.79
120.7610.8
122.7110.86
125.4810.93
127.3110.99
Problems
Temperature(K)
Heatcapacity
K))
130.0610.96
132.4110.98
135.8911.03
139.0211.08
140.2511.1
145.6111.19
153.4511.25
158.0311.4
162.7211.61
167.6711.69
172.8611.91
177.5212.07
182.0912.32
enalreporttothepublishershouldincludeguresshowingeachof
theregressioncurvets,theequationforthettingcurve,andyourrec-
ommendationforthettingequationthatbestbalancesqualityoftwith
simplicity(i.e.,fewerttingparameters).
ereismuchdebateabouttheproperapproachforcharacterizingthe
rheologyofblood.Aparticularlyimportantaspectofthisproblemis
establishingarelationshipbetweenthe
shearstress
strainrate
for
blood.eshearstressisameasureoftheshearforceperareaapplied
toblood.Imagineapersonslidingonawaterslide,theshearstressisthe
forceperareathatthepersonisapplyingtothewaterunderneaththem.
estrainraterepresentstherate(ortimeperiod)atwhichtheuid
deforms.emagnitudeofthedeformationisusuallynormalizedbythe
originalsize(orthickness)oftheuidsothatthestrainratehasunitsof
pertime.
YouhavebeenhiredbytheRedCrosstotstrainrateandshearstressdata
fromabloodsampletotwodierentmodels,andthenassessthequality
ofthemodelsforttingthedata.erheologicaldataisfrom[4].
Strainrate(1/s)Shearstress(dynes/cm
1.512.5
2.016.0
3.225.2
7Regression
Strainrate(1/s)Shearstress(dynes/cm
6.540.0
11.562.0
16.080.5
25.0120
50.0240
100475
erstmodelproposedbytheRedCrossisthelinear(orNewtonian)
model:
where
istheshearstressand
thestrainrate.esecondmodelisthe
powerlawmodel:
Useleast-squaresregression(e.g.,the
functioninScipy)
todeterminetheoptimalvalue(s)fortheunknownparameter(s)ineach
model(i.e.,
inthelinearmodelor
inthepowerlawmodel).
Generateaplotshowingthedataandtheoptimalregressioncurvefor
eachmodel.Finally,assessthequalityofthetinyourreporttotheRed
Cross.
Possiblythemostfundamentalrelationshipinecologyisthatthenumber
ofuniquespecies,
,increasesasthearea,
,ofaregionincreases.is
relationshiphasbeenquantitativelyobservedinnumerousstudiesover
thepast200years.erelationshipisnormallyoftheform:
where
areunknownconstantsthatdependontheecologicalsys-
tembeingstudiesandtheymustbedeterminedfromexperimentaldata.
evaluefor
isapositiverealnumberlessthan1.0.
YouhavebeenhiredasaconsultantbytheStateofCaliforniatostudythe
speciesarearelationshipforendemicvascularplantspeciesforpartially
isolatedsubregionsofthestate.Dataonthenumberofspeciesineach
areawascollectedandpublishedpreviously[5].
LocationArea(mi
)Species
TiburonPeninsula5.9370
SanFrancisco45640
Problems
LocationArea(mi
)Species
SantaBarbaraarea110680
SantaMonicaMountains320640
MarinCounty5291060
SantaCruzMountains13861200
MontereyCounty33241400
SanDiegoCounty42601450
CaliforniaCoast24,5202525
Fitthespeciesversusareadatatothepowerlawrelationshipgivenabove
usingregressiontodetermine
forthisparticularsystem.Anexam-
inationofsimilarstudiesshowsthat
isoftenoftheorder101000and
isfrequentlybetween0.2and0.4.
Onthebasisoftheregressionanalysis,theStateofCaliforniahasasked
youtoanalyzetheimpactofthegrowthofitsthreemajorcities:LosAnge-
les,SanDiego,andSanFrancisco.Specically,ifCalifornialoses1%ofthe
totallandinthestatetothedevelopmentofthecitieseveryyear,approx-
imatelyhowmanyspecieswillbecomeextinctnextyear?
Hint:yoursupervisorrecommendsplottingthespeciesversusareadata
andcurvetusingasemilogx()plotinmatplotlib.
eWorldHealthOrganization(WHO)publishesdataonaverageheight
versusageforbothfemalesandmales.Selectdataissummarizedinthe
tablebelow(ageisgiveninmonthandheightisgivenincm).
Month:012366096144168192216
Female:49.174.095.1109.4126.6151.2159.8162.5163.1
Male:49.875.896.1110.0127.3149.1163.2172.9176.1
YouhavebeencontractedbyWHOtotthedataforfemalestothe
followinggrowthfunction:
where
istheheightoffemalesincm,
theageinmonths,and
,and
theunknownparameterstobedeterminedusingleast-squaresregression
onthisnonlinearfunction.YourreporttoWHOshouldincludeaplotof
theheightversusagedataalongwiththeoptimalgrowthfunctiontot
thedata.
7Regression
Finally,WHOisconsideringthepotentialforasinglelinearfunctionto
tthedataforbothboysandgirls,usingafunctionoftheform:
where
istheheightoftheindividual,
thegender(youneedtopropose
anconvervsionfromgender,maleorfemale,intoanumber),and
,and
unknownparameterstobedeterminedusingleast-squaresregression.
Youshouldcommentonthequalityofthetforthisfunctionrelativeto
thepreviousgrowthfunctionforthedataprovided.
References
Anton,H.andRorres,C.(2005)
ElementaryLinearAlgebra
,JohnWiley&
Sons,Inc.,NewYork,9thedn.
Mor,J.(1978)eLevenberg-Marquardtalgorithm:implementationand
theory,in
NumericalAnalysis
LectureNotesinMathematics
,vol.630(ed.
G.Watson),Springer-Verlag,BerlinHeidelberg,pp.105116.
Giaugue,W.F.andWiebe,R.(1928)eheatcapacityofhydrogenbromide
from15
toitsboilingpointanditsheatofvaporization.eentropyfrom
spctroscopicdata.
J.Am.Chem.Soc.
,21932202.
Palladino,J.L.andDavis,R.B.III(2012)Biomechanics,in
Introductionto
BiomedicalEngineering
(edsJ.D.EnderleandJ.D.Bronzino),AcademicPress,
Burlington,MA,pp.134218,3rdedn.
Johnson,M.P.,Mason,L.G.,andRaven,P.H.(1968)Ecologicalparameters
andplantspeciesdiversity.
Am.Nat.
,297306.
V

b


a
=
0
.
8NonlinearEquations
Ourgoalinthersthalfofthischapteristoexaminetwodierentmethodsfor
ndingsolutionstononlinear,algebraicequationsliketheSRKequationwhen
isunknown.erstmethod,bisection,isslowbutveryrobust,whilethe
secondmethod,Newtonsmethod,isfastbutdoesnotalwaysconvergetothe
solution.
BeforeexaminingbisectionandNewtonsmethodindetail,wecanuse
somefunctionsincludedwiththeScipylibrarytosolvetheSRKproblem
describedabove.Itis,ofcourse,dangeroustouseanalgorithmthatwedo
notunderstandsoconsiderthisapproachwithskepticism,buthavinga
solutiontoourexampleproblemwillbehelpfulgoingforward.eScipy
libraryincludesanumberoffunctionsforsolvingnonlinearequations,and
thesefunctionsarecontainedintheoptimizationsectionofthelibrary.
Someofthenonlinearsolversaredesignedforasinglenonlinearequations
andothersolvers,examinedlaterinthischapter,aredesignedforlarge
systemsofnonlinearequations.Here,wewillusethebroyden1function
)tosolvetheSRKequationfor
.e
useofthebroyden1function(andallothernonlinearsolvers)requiresatleast
twoinputs:(1)thenameofafunctioncontainingthenonlinearequationand
(2)aguessforthesolution.Further,thenonlinearfunctionmustberearranged
sothatallthetermsareononesideoftheequalsignanditreturnszeroatthe
solution.Hence,ifwehaveageneralnonlinearequationliketheSRKequation
thatcanbewrittenas
andwearesearchingfor
thatsatisesthis
equality,wemustrewritethefunctionas
0sothatthefunction
returnszerowhen
isfound.
NonlinearSolutionAlgorithms
Formostalgorithmsthatndsolutionstoanonlinearequation,thenonlinear
equationmustberearrangedsothatitisequaltozeroatthesolution.
eSRKequationshouldberewrittenas
V

b
+

a
0.(8.2)
ePythonscriptbelowbeginswiththedenitionoftheSRKfunction,which
ispassedaguessfor
astheonlyinput,andthenusingthatguessandthe
appropriateconstantsforthesubstanceofinterest(carbonmonoxideinthis
case),teststoseeiftheSRKequationissatised.Ifthefunctionreturnszero,
thentheestimatedvaluefor
wascorrectandtheoriginalfunctionevaluates
tozero,thatis,itisarootofthefunction.
#PropertiesofCarbonMonoxide
T=300
P=10
#atm
Tc=133.0
Pc=34.5
#atm
w=0.049
#AcentricfactorforCO
R=0.08206
#Latm/(molK)
a=0.42747*(R*Tc)**2/(Pc)
b=0.08664*(R*Tc/Pc)
m=0.48508+1.55171*w-0.1561*w**2
alpha=(1+m*(1-math.sqrt(T/Tc)))**2
term1=R*T/(V-b)
term2=alpha*a/(V*(V+b))
8NonlinearEquations
f(x)
b
f
(
b
)
f
(
a
)
)/2
Figure8.1
Thebisection
P=10
#atm
Tc=133.0
Pc=34.5
#atm
w=0.049
#AcentricfactorforCO
R=0.08206
#Latm/(molK)
a=0.42747*(R*Tc)**2/(Pc)
b=0.08664*(R*Tc/Pc)
m=0.48508+1.55171*w-0.1561*w**2
alpha=(1+m*(1-math.sqrt(T/Tc)))**2
term1=R*T/(V-b)
term2=alpha*a/(V*(V+b))
8NonlinearEquations
stop==False:
("ReachedmaxIter.Currentlyat",c)
Whenthisscriptisrun,itprintsoutthemidpoint,
,and
everyiteration.
Dependingontheinitialbounds,1520iterationsarerequiredforconvergence
tothetoleranceof1
.Withnonidealgasproblemslikethis,itispossible
toobtainagoodinitialestimateforthesolutionusingtheidealgaslaw,
.Forcarbonmonoxideundertheconditionslistedabove,theidealgaslaw
gives
46L/mol,whichisalmostidenticaltothesolutionobtainedusing
theSRKequation.Ifthepressureincreasesorthetemperaturedecreases,the
idealgaslawbecomeslessaccurate.
DebuggingPractice
Anoviceprogrammerrewroteafewlinesofthebisectionscriptasshownbelow:
.(8.3)
Typically,
ischosentobeapointwhereinformationaboutthefunction
isknown.elasttermtellsusthatthemagnitudeofthetermisontheorder
,buttheexacttermisnotgiven.Ifthedistancebetween
small(i.e.,
1),then
oreven
isverysmall.
InNewtonsmethod,wearetryingtond
suchthat
0.Assuming
thatwehaveaguess,called
,forthevalueof
andsetting
0,the
Taylorpolynomial,ignoringthelasttwotermsgivenabovebecausetheyare
(hopefully)small,becomes
Rearrangingandsolvingfortheunknown
gives
.(8.4)
Figure8.2illustrateshowthisequationhelpsustondtherootofinterest.
NoticingthatweonlyretainedthelineartermfromtheTaylorpolynomial,
Newtonsmethodapproximatesthenonlinearfunctionwithalineatcurrent
estimateforthelocationoftheroot,
,andthatlineisthenusedtocalculate
anewandhopefullybetterestimateforthelocationoftheroot.AnewTaylor
polynomialisusedtoapproximatethenonlinearfunctionwithalineatthis
newlocation,andtheprocessisrepeatuntiltherootisfound(inthecaseof
convergence)oraniterationlimitisreached(failure).
Figure8.2
root(i.e.,theintersectionofthe
nonlinearfunctionwiththe
-axis)ofthenonlinearfunction.
f
(
x
)
approximation
8NonlinearEquations
Newtonsmethodisusedtosolvefortherootsoftheequation
inthePythonscriptbelow.
TOL=1.0e-6
MAX_ITER=10
#function
Figure8.3
Thenonlinear
function
withrootsat
0and
1.4
1.2
1.0
0.60.81.01.21.41.6
Newtonsmethodalsohastwodisadvantagescomparedtothebisection
method:
1)Weneedtohaveafunctionthatcalculatesthederivative,
,atapoint,
.islimitation,however,issomewhatovercomeinthenextsectionon
Broydensmethod.
2)Iftheinitialguessforthelocationoftherootisnotsucientlyaccurate,the
iterationscandivergeandasolutionwillnotbefound.isbehaviorwas
seenpreviouslyusingBroydensmethodwhentheinitialestimateof
lessthanzero.
,(8.5)
andnoticethatifthehigherordertermsarediscarded(i.e.,thelasttwoterms
ontherightside),itcanberearrangedtoprovideanestimateforthederivative:
IfwesubstitutethisdirectlyintotheNewtoniteration,everythingwillcancel
outbecausewejustusedthesameequationtwice.Insteadofusingthecurrent
8NonlinearEquations
guess,
,andthenextguess,
,toapproximatethederivative,wecanusethe
previoustwoguesses:
.(8.6)
Noticethatthecurrentguessisnow
andthepreviousguesswas
.Substi-
tutingthisapproximationforthederivativeintoNewtonsmethodgives
new
.(8.7)
isapproachisalsofrequentlycalledtheSecantmethodbecausethe
nonlinearfunctionisapproximatedusingasecantlineinsteadofatangent
ePythonscriptbelowusesBroydensmethod(equation8.7)tondthe
solutiontotheSRKmodelproblemthatwassolvedpreviously.
#PropertiesofCarbonMonoxide
T=300
P=10
#atm
Tc=133.0
Pc=34.5
#atm
w=0.049
#AcentricfactorforCO
R=0.08206
#Latm/(molK)
a=0.42747*(R*Tc)**2/(Pc)
b=0.08664*(R*Tc/Pc)
m=0.48508+1.55171*w-0.1561*w**2
alpha=(1+m*(1-math.sqrt(T/Tc)))**2
term1=R*T/(V-b)
term2=alpha*a/(V*(V+b))
("xNew=",xNew,"andfxNew=",fxNew)
(math.fabs(fxNew)TOL):
("Rootfoundat",xNew)
x0=x1
fx0=fx1
x1=xNew
fx1=fxNew
(math.fabs(fxNew)�TOL):
("ReachedmaxIter.Currentestimateat",xNew)
UsingBroydensmethod,thesolution,
46,isobtainedinabout
seveniterations,dependingontheinitialguess.eiterationsareespe-
ciallyrapidnearthesolutionwith
00016aftersixiterationsand
afterseveniterations.Noticethatthevalueof
isconverg-
ingtozeroquadratically(ornearlyquadraticallysinceweareapproximating
thederivative.
Usinganinitialguessof
0onceagainleadstoanerrormessage(in
thiscasedivisionbyzero)andfailuretoconverge.Nowthatweunderstandthe
principlebehindNewtonsmethodandBroydensmethod,wecanexaminewhy
thisoccurs.Figure8.4showsthenonlinearSRKfunctionwithasinglerootat
46.Foranyguessnearthatroot,ifweapproximatetheSRKfunctionwith
astraightlineandthennoticewherethatlinecrossthe
-axis,itwillprobably
0
40

20
0
20

5

10

Figure8.4
ThenonlinearSRKfunctionwithasinglerootat
8NonlinearEquations
beapointclosertothesolution,andtheiterationswillconverge.Ifourinitial
guessisnegative,however,anylinearapproximationtothefunctionisgoingto
predictthattherootliesfurthertotheleft.Asaresult,eachiterationwilltake
usfurtherandfurthertoward
8.5MultipleNonlinearEquations
Problemsthathavemultiplenonlinearequationsthatmustallbesolvedsimul-
taneouslyariseinproblemsasdiverseasuidpipingnetworks(i.e.,waterdistri-
butionsystems)tostageddistillationcolumns.Inordertosolvethesesystems
ofnonlinearequations,weneedtousethetoolswehavealreadylearnedfor
linearsystems(Chapter6)andsinglenonlinearequations(thischapter)and
combinethemtogether.erststepistorecallNewtonsmethod:
new
,(8.8)
whichenablestheiterativedeterminationof
suchthat
0.Nowweare
interestedinthecasewherewehave
nonlinearequations(somepreferto
thinkofthisasa
vector
ofequations)written
andwearetryingto
determinethevectorofunknowns,
,thatsatisesthissystemofequations.
Formultipleequations,Newtonsmethodcanberewritten:
new
,(8.9)
where
x
f
x
f
x
f
.(8.11)
Allofthepreviousadvantages(fast,singleinitialguess)anddisadvantages(cal-
culatederivatives,maynotconverge)applywhenNewtonsmethodisapplied
toasystemofnonlinearequationsinsteadofasingleequation.Itiscommon
tocall
the
residual
orresidualvectorand
isthe
Jacobian
orJaco-
bianmatrix.CalculatingtheresidualdividedbytheJacobianrequiressolving
alinearmatrixproblem:
for
.Once
isdetermined,then
8.5MultipleNonlinearEquations
1.0
Zero contour for the two functions
1.0
1.0
1.0
Figure8.5
Aplotofthezerocontourlines(i.e.,thelineswhere
equalzero)forthe
examplefunctions.Thethickercontourlineis
.Thetwopointswherebothfunctionsare
zeroaresolutionstothisnonlinearsystem,andtwodierentsolutionvectorsareseento
existinthisplot.
new
.isiswrappedinaniterationandrepeateduntil
TOL
.Inotherwords,if
isclosetothesolutionvector,theneachequationin
theresidualshouldbeasmallnumberandthe
-normoftheresidualshould
besmall.
Asanexample,considerthesystemoftwononlinearequationswithtwo
unknowns:
cos
Forasystemthissmall,itispossibletouseacontourplottoexploretheproper-
tiesofthesolution.Puttingbothoftheunknowns,
,intoasinglevector:
0]=xandx[1]=yandplottingthecontourswhereeachfunctionisequalto
zerogeneratestheplotshowninFigure8.5.
APythonscriptthatusesNewtonsmethodtosolvethissmallsystemoftwo
nonlinearequationsisshownbelow.
numpy.linalgasnl
n=x.size
8NonlinearEquations
f=numpy.zeros(n,dtype=numpy.float64)
f[0]=2*x[0]**2-x[1]**2
f[1]=x[1]-0.5*(math.sin(x[0])+math.cos(x[1]))
8.5MultipleNonlinearEquations
dierentalgorithmsavailablein
forsolvinglinearsystems
ofequations.
n=x.size
f=numpy.zeros(n,dtype=numpy.float64)
f[0]=2*x[0]**2-x[1]**2
f[1]=x[1]-0.5*(math.sin(x[0])+math.cos(x[1]))
8NonlinearEquations
6
4
a
b
c
d
Figure8.6
thateachsideofthesquaremusthavethesamelength,so
Nowwehaveanonlinearsystemoffourequationswithfourunknowns.Before
usingBroydensmethodtosolveforthefourunknowns,itisnecessaryto
rewritetheequationssothatallthetermsineveryequationareononesideof
theequalsign.
ePythonscriptbelowuses
toapprox-
imatethesolutiontothissystemofequations.
a,b,c,d=vals
eq1=a+b-c-d
eq2=b**2+d**2-36
Problems
eq3=a**2+c**2-9
eq4=c**2+b**2-16
8NonlinearEquations
edewpointtemperatureisthetemperatureatwhichthefollowing
equationsissatised:
750mmHg
where
isthemolefractionofmethaneinthenaturalgas,
the
molefractionofethaneinthenaturalgas,and
thevaporpressureof
methaneorethane,dependingonthesubscript.eabovementioned
equationassumesthatatmosphericpressure(orbarometricpressure)in
NewYorkis750mmHg.evaporpressuresofmethaneandethane
dependonthetemperature,andtheyarecalculatedusingAntoines
equation:
where
isthetemperaturein
thevaporpressureinmmHg,and
,and
thespecies-specicconstantsgiveninthetablebelow.Note
thatthetermafterthenumber10isanexponent.
6.611846.80266
389.93656.4
266.0256.0
ConEdisontypicallyhasnaturalgasthathas
90(i.e.,90mol%
methane)and
10(i.e.,10mol%ethane).Determinethedew
pointtemperaturebysolvingthenonlinearequationabove.ConEdison
wouldalsoliketoknowhowthedewpointtemperaturechangesif
thecompositionofthenaturalgaschangesto70mol%methaneand
30mol%ethane.Shouldtheybeworriedaboutcondensationoccurringin
NewYorkfortheworstcasescenarioofArcticcoldfront?
Anewlargebackyardgasrepithasrecentlybeendevelopedthatisfueled
bypropane.Unfortunately,thepropanetankwaspaintedblacksothatit
wouldbeeasiertohideinabackyard,buttheblackcolorcausesthetank
contentstoheatupinthehotsunandsometankshaveruptured.You
havebeenhiredbythemanufacturertocalculatethequantityofpropane
inthetankatthetimeofrupture.emanufacturersbelievethatvander
Waalsequationofstateisanaccurateequationfortheseconditions.e
equationis
V
(

V

b
Problems
where

R
c
,
b
=

1


efollowingpropertiesforpropaneandotherconstantwereprovided
toyou:
384K
3kPa
tankvolumeis0.15m
15m
where
ismolesofpropanein
thetank
9K(criticaltemperatureofpropane)
6kPa(criticalpressureofpropane)
gasconstant:
008314m
kPa/(mol
epressureandtemperatureprovidedtoyouarethelastpressureand
temperaturereadingsonthetankjustbeforerupture.Solvevander
Waalsequationforthenumberofmolesinthe0.15m
tankatthetime
ofrupture.Notethatthetemperatureandpressurebothexceedthe
criticalvaluessotheuidinthetankisnotaliquidoragasbutisasuper
criticaluid.Youshouldreportthequantityofsupercriticalpropanein
thetankintermsofmolesandmass(kg).Youshouldalsogenerateaplot
ofthevalueoftheleftsideofvanderWaalsequationfordierentvalues
,thenumberofmolesinthetank,toconvincethemanufacturethat
thereisonlyasinglephysicallypossiblesolutiontotheequation.
etrajectoryofanyprojectileobject(undercertainassumptions)canbe
determinedusingNewtonslawsofmotion,
.Afterafootball
hasbeenreleasedbyaquarterback,theprimaryforceactingonthefoot-
ballisgravity,thus,theacceleration,
,ofthefootballcanbedescribedby
where
isaccelerationinthe
-orhorizontaldirection,
theaccel-
erationinthe
-orverticaldirection,
themassofthefootball,and
806m/s
.Sinceacceleration,
,equalsthederivativeofvelocitywith
respecttotime,
,theaccelerationequationscanbeintegratedtogive
equationsforvelocity:
cos
8NonlinearEquations
where
istheinitialvelocityofthefootballand
istheinitialupward
angleofthethrow.Sincevelocity,
,equalsthederivativeoflocation,
,withrespecttotime,thevelocityequationscanbeintegratedto
giveequationsforlocation:
cos
where
istheinitiallocationofthequarterback.Solvingthe
loca-
tionequationfortimegives
cos
andsubstitutingthisequationfortimeintothe
locationequation(and
movingeverythingtoonesideoftheequalsign)gives
tan
cos
Youhavebeenhiredbythequarterbackofthelocalprofessional
(American)footballteamtodeterminetheangle,
,forthrowingthe
footballsothatittravelsdowneldfor50mandcanbecaught2m
abovetheground.Usethefollowingdata(obtainfromtheNFLScouting
Combine)inyouranalysis:
Quarterbacklocation
Quarterbackarmstrengthgivesaninitialvelocityof25.0m/s
0102030405060
Figure8.7
Trajectoryofthefootballinthe
plane.
Problems
Targetlocation
inm
BroydensmethodasimplementedinScipyisrecommended.
Yournalreporttothequarterbackshouldincludetheinitialangle,
for
thethrowinradiansanddegrees,andyoushouldhaveaplotoftheball
location,
overtime(Figure8.7).Usethisplottoestimatethetimeto
targettothenearestsecond.
9.2ReadingDatafromaFile
Figure9.1
AscreencaptureshowingtherstfewlinesoftheleDOdata.csv.Thele
containsaheaderrowfollowedbythedata.
contentsofacsvtextleintoanumpyarrayforlaterstatisticalanalysisand
plotting.
erstdatasetthatwillbeimportedcontainsmultiplepH,dissolvedoxygen
(DO),andtemperaturedatafromsixdierentlocationsinariver.elocations
aredistinguishedonlybyanumber(1through6).Figure9.1showsascreen
captureofthecontentsoftherstpartofthedatale(DOdata.csv).ele
containsaheaderrowthatcontainsthenamesofthevariouscolumnsofdata
andtheactualdatafollowsbelow.
9.2.1NumpyLibrary
esimplestmethodforimportingdatafromaleintoanumpyarrayisto
usethefunction
.isfunctionispassedasinputs:
(1)thenameofthele;(2)thedelimiter(i.e.,thecharacterseparatingthe
uniquedatapoints),whichistypicallyacommaoratabcharacter;and(3)the
numberofheaderrowstoskip.enumpydocumentationincludesdescrip-
tionsforother,optionalinputsforthe
function.ePython
scriptbelowillustratestheuseofthisfunctionforimportingdatafromacsvle.
data=numpy.genfromtxt("DOdataNoHeader.csv",\
numRows,numCols=data.shape
9Statistics
Onedrawbacktothisapproachisthatifthelecontainsaheaderrow,such
astheexampledatashowninFigure9.1,theheadertextisskippedandisnot
readilyavailableforuselaterinthecode.Anotherdrawbackisthatallthedata
intheleshouldbethesamedatatype.Ifonecolumncontains,forexample,
textdataormissingdata,theimportfunctionreturnsanerror.Ifthisapproach
isused,thecsvleshouldbecarefullycheckedthatitonlycontainsnumerical
dataandanyheadertextshouldberecordedseparatelyifitisneeded.
Asecondapproachforimportingdatafromaleusingthenumpylibrary
startsbyusingsomestandardPythonfunctionsforopeningaleandthenread-
ingthelecontentsonerowatatime.Everytimeanewrowisreadinfromthe
le,itisloadedintothenumpyarrayusingthe
func-
tion.isapproachrequirestheconstructionofanemptynumpyarraytohold
newdataasitisreadinfromthele.eadvantageofthisapproachisthatit
allowsonetoloadtheheaderinformationfromtheleandstoreitinaseparate
variablefromthenumpyarray.ePythonscriptbelowimplementsthismore
manualapproachforreadingindatafromacvsle.
#openfilecontainingdata
dataFile=
#readfirstlinewithheadersandsplitthemup
header=dataFile.readline()
dataHeaders=header.split(',')
numCols=
9.2ReadingDatafromaFile
9.2.2CVSLibrary
Anotherapproachforloadingdatafromacvsletakesadvantageofthecvs
librarythatisastandardpartofthePythonlanguage.isapproachstillallows
specialhandlingoftheheaderrow,anditalsorequiresthecreationofanempty
numpyarrayforstoringthedataasitisreadfromthele.Wheneachrowis
readfromthele,itisimportedasalistofstrings,thatis,eachnumberisa
uniquestring.esestringsareconvertedintooatingpointnumbersusing
thefunction
.Oncealltherowsarereadfromthe
le,thenumpyarraycanberesizedtotruncateanyemptyrowsfromtheendof
thearraythatwasoriginallycreated.ePythonscriptbelowimplementsthis
thirdapproach.
('DOdata.csv','r')asdataFile:
reader=csv.reader(dataFile)
numCols=len(header)
maxRows=256
data=numpy.zeros((maxRows,numCols))
rowCount=0
data[rowCount,:]=numpy.array(row).astype(
rowCount+=1
(rowCount�=maxRows):
data=numpy.resize(data,(rowCount,numCols))
9.2.3Pandas
enalmethodforimportingdatafromaleistousethepandaslibrary
(pandas.pydata.org).epandaslibrarywasdevelopedfromtheverybeginning
toenabletheanalysisoflargedatasetsinthemostsimpleandstraightfor-
wardmannerpossible.Asaresult,pandasincludesfunctionsforimporting
datafromawidevarietyofletypesincludingcsvles,Excelles,andmany
others.Pandascanhandleheterogeneousdatasetsthatincludecolumnsof
oatingpointnumbersnexttocolumnsofstringdata.Further,pandascan
importdatasetswithmissingdata,anditincludesfunctionsthatcanattempt
toll-inthemissingdatausinganumberofstandardapproachesifthatis
necessary.epandaslibraryisoneofthemostactivelydevelopedlibraries
fordataanalysisusingPython,andfurtherexplorationofthelibraryandits
featuresishighlyencouragedforanyonethatroutinelyperformsdataanalysis
tasks[1].
9Statistics
euseofpandastoimporttheDOdatasetusedinthischapterisexplored
throughabriefexample.Pandashasanumberofbuilt-infunctionsforimport-
ingandexportingdatasetstolesincluding
,and
.efulldescriptionofinput/outputfunc-
tionscanbefoundonthepandaswebsite(http://pandas.pydata.org/pandas-
docs/stable/io.html).ePythonscriptbelowillustratesthe
functionforimportingtheDOdataset:
matplotlib.pyplotasplt
data=pandas.read_csv("../../data/DOdata.csv")
isisremarkablysimplewhencomparedtotheothermethodswehave
examinedforimportingdata.efunctionautomaticallyrecognizestheheader
informationanditautomaticallyrecognizestheuseofcommastoseparatethe
data.einformationintheleisstoredinthevariabledata,whichistechni-
callyapandasDataFrameobject.Asexpected,itisrelativelysimpletoaccess
thedatawithintheDataFrame.ecodebelowisacontinuationoftheprevi-
ousPythonscript,anditillustrateshowtoaccess,modify,andanalyzethedata
thatwasreadfromthele.
(data.Temp�26.0)
erstprintfunctioncalloutputsthenamesofthecolumnsfromthele,
andtheoutputis:
Index(['Location','pH','DO','Temp'],dtype='object')
esecondprintfunctionoutputsthevaluesfromtheDataFrameasanumpy
array.Pandasistightlycoupledtonumpyandusesnumpyarraysextensively.
ethirdprintfunctioncallusingpandas
functiontocalculate
statisticsforeachcolumnofdata.eoutputfromthisfunctionis:
LocationpHDOTemp
count33.00000033.00000033.00000033.000000
mean3.6060618.2978799.47575826.274545
std1.8530280.1438910.9912930.418510
1.0000007.8800007.78000025.780000
25%2.0000008.2400008.75000026.010000
50%4.0000008.3000009.52000026.190000
75%5.0000008.3500009.89000026.420000
6.0000008.65000012.13000027.830000
9.2ReadingDatafromaFile
Weseetheaverage,standarddeviation,andotherstatisticalinformationfor
eachcolumnofdata.elasttwoprintfunctionsdemonstratepandasabil-
itytoquicklyanalyzeandsortdata.e
functionreorders
therowsofdatabasedonsortingaspeciedcolumn.enalprintfunction
outputsaBooleanvectorthatisTrueforeveryrowwithatemperaturevalue
over26.0.
Oneoftheespeciallyusefulfeaturesofpandasisthatitallowsustoaccess
datausinglabelsinsteadofalwaysneedingtoremembercolumnorrownum-
bers.IftheDOdatasetisimportedintoanumpyarrayusinggenfromtxt()or
oneoftheothermethodsthatwereconsideredbeforepandas,thenweneed
torememberthatthetemperaturedataisinthefourthcolumnandcanbe
accessedusing
orsimilarnotation.Ifthedataisstoredinapandas
DataFrame,wecanaccessanumpyarrayholdingthetemperaturedatausing
.isisillustratedinthecodebelow,whichgeneratesahistogram
ofthetemperaturedata.
27.0
Temperature (C)
Frequency
27.5
Distribution of temperature measurements
Figure9.2
Ahistogramof33temperaturemeasurements.Thedatadoesnotappeartobe
normallydistributed.
matplotlib.pyplotasplt
data=pandas.read_csv("../../data/DOdata.csv")
#Plotahistogramoftemperatures
9Statistics
plt.title("DistributionofTemperatureMeasurements")
plt.xlabel("Temperature($
eresultisshowninFigure9.2.
9.2.4ParsinganArray
Onceallthedatafromtheleisimportedintoanumpyarray,itissome-
timesdesirabletoextractoutasubsetofthisdata.Forexample,thedataset
thathasbeenusedinthischaptercontainsmultiplepH,DO,andtempera-
turemeasurementsatsixdierentlocations.Ifwewanttocomparetheaver-
ageDOatlocation1totheaverageDOatlocation2,itishelpfultoparseor
extractthatdatafromthelargerarray.isprocessoftenrequiresthatwerst
counttheamountofdatathatisgoingtobeextracted,thenallocatespaceinan
emptyarrayforthedata,andnallyreadingandcopyingthedataintothenew
array.
ForthedatasetshowninFigure9.1,asimplefunctioncanbewrittenthat
parsesthelargerarrayandreturnsasmallarraywithonlythelocationdataof
interest.
9.3StatisticalAnalysis
functionforgeneratingahistogramplotfromavectorcontainingthedata
ofinterest.eplottingfunctioniscalledwith:
)orpylab.hist(data[:,3],9)wheretherst
argumentisavectorwiththedataofinterest(inthiscaseweareaskingfor
ahistogramofthetemperaturedataacrossalllocations)andthesecond,
optionalargumentisnumberofbins(i.e.,thenumberoftemperatureranges).
ehistogramofthetemperaturedataisshowninFigure9.2andthe
-axis
indicatesthetemperaturerangesforeachbinandthe
-axisisthefrequency
ofdatapointswithineachrange.Someofthestatisticalanalysisthatis
coveredinthissectionreliesondatathatisnormally(orGaussian)distributed.
Examinationofthehistogramcanprovidesomeinsightintowhetherornota
particulardatasetisnormallydistributed.
Numpyhasbuilt-infunctionsforcalculatingthemeanandstandarddevia-
tionforanarray.Usingthe
9Statistics
thetwomeansarethesame.If
05,thenwecansaywith95%con-
dencethatthetwomeansaredierent.If
01thenwecanbe99%
condentthatthemeansaredierent.esearethetwothresholdscommonly
usedinstatisticallycomparingtwodatasets.Forthedataexaminedhere,prob
equals0.37sowecannotsaythatthetwomeansaredierentandnostatisti-
calconclusionshouldbemade.emeansmightbethesameortheymight
bedierent,wesimplydonothaveenoughdatatohavecondenceineither
result.
Insomecases,thedatathatwecollectandwanttoanalyzeissubjective.For
example,imagineifweasked100peopletoscore20dierentmoviesfrom
thispastyearonascaleof110.Somepeopleareeasygradersandwould
givescorescloserto10evenwhentheydidnotreallylikethemovie.Other
peoplearetoughgradersandtheynevergiveascoreover8.Further,some
peoplewouldusethefullrangeofpossiblevalues,givingtheworstmoviea1
andthebestmoviea10whileotherpeoplewoulduseasmall,clusteredrangeof
scores(i.e.,theworstmoviegetsa6andthebestmoviegetsa9).Wheneverwe
havesubjectivescoreslikethiswithvariabledistributions,itishelpfultonor-
malizethescoresusinga
-score,whichisalsoknownasthestandardscore.
-scoreisdenedas
where
istheaveragescoreand
isthestandarddeviationforonesetof
scores.us,eachpersons
-scoresforthe20moviesarecenteredaroundzero
(goodmovieshavea
-scoregreaterthanzeroandbelowaveragemovieshave
anegative
-score)andthe
-scorereectshowmanystandarddeviationsbet-
terorworsethantheaverageamovieendsupbeingratedbyeachindividual.
-scoreof2.0reectsamoviethatis2standarddeviationsbetterthanaverage
(i.e.,thepersonrankedthemovieintheirpersonaltop5%ofmovies).
Adatasetcanbeautomaticallytranslatedinto
-scoresusingthefunction:
.einputintothefunc-
tionistheoriginaldata,andthefunctionreturnsthenormalizedscoresor
-scores.elengthofthe
zscore
vectorshouldbethesameasthevectorcon-
tainingtheoriginaldata,andthe
-scoresshouldaveragezero.e
-scoresfor
theDOdataatlocation1areasfollows:
65,1.16,
51,0.85,and
0.77.egreatestdeviationfromtheaveragefortheDOdatais
6standard
deviationsbelowaverage.
9.4AdvancedLinearRegression
Linearregressionwasdiscussedpreviously,butthe
librarypro-
videsalinearregressionfunctionthatreturnsadditionalinformationthatcan
behelpfulininterpretingtheresults.Toexplorethelinearregressionfunction
9.4AdvancedLinearRegression
,anewdatasetthatcontainsDOandtemperaturemeasure-
mentsasafunctionoftimeisused.Further,themeasurementswererepeatedso
twodierenttime-dependentdatasetsareavailable.edataisstoredintwo
separatecsvles:DOdepletion1.csvandDOdepletion2.csv.Wheneversucha
situationarises,itishelpfultowriteaPythonscripttoopenaleandimport
thedataforaspecictrialnumbertrial1ortrial2inthiscase.ePython
scriptbelowispassedatrialnumberanditthenbuildsthelenameusingthe
trialnumberandstringconcatenation.
where
istherateconstantand
thedissolvedoxygenconcentrationin
mg/L.Forarst-orderprocess,therateofdepletionisproportionaltothecon-
centrationandafasterdepletionrateisobservedathigherconcentrations.Ina
dierentialequationscourseorakineticscourse,thesolutiontothisequation
isderived.esolutionisjustpresentedhere:
9Statistics
0510
Time (min)
Concentration (mg/L)
152025
Figure9.3
Dissolvedoxygen(mg/L)measurementsasafunctionoftimeforanisolated
andthesolutiontothisequationisgivenby
1
FortheDOmeasurementsplottedinFigure9.3,wewishtodetermineifthe
processisrstorsecondorderusinglinearregressiontotthedatatoboth
ratelawsanddeterminingwhichprovidesthebettert.
Inordertotthedatawitharst-ordermodel,weneedtoperformlinear
regressiononln
versus
.Since
theslopefromregressionshouldbeanestimatefor
andtheinterceptshould
beanestimateforln
.e
func-
tionmustbepassedatleasttwovectorsasinputs:(1)theindependentvariable
data,
,andthedependentvariabledata,
.Fortherst-orderdepletion
model,theindependentvariableistime,
,andthatdataisintherstcolumn
oftheDOdataarray,andthedependentvariableisln
,whichcanbe
obtainedbytakingthenaturallogarithmofthesecondcolumnofDOdata.
9.4AdvancedLinearRegression
functioncanbecalledandpassedthe
timeandnaturallogofDOdatausing
slope,intercept,r_value,p_value,std_err=\
versus
.eresultingslopefromlinearregression
shouldcorrespondto
,andtheinterceptshouldbeequalto
.e
functioniscalledusing:
slope,intercept,r_value,p_value,std_err=\
0510
Time (min)
Concentration (mg/L)
152025
Figure9.4
Dissolvedoxygen(mg/L)measurementsasafunctionoftimeandthebestt
9Statistics
0510
Time (min)
Concentration (mg/L)
152025
Figure9.5
Dissolvedoxygen(mg/L)measurementsasafunctionoftimeandthebestt
9.5U.S.ElectricalRatesExample
erststepindevelopingaPythonscriptforexploringandanalyzingthis
dataistoimportthelibrariesthatwillbeusedandthenreadinthecvsle.e
examplebelowusesthePandaslibraryforreadingtheleandstoringthedata.
pandasaspd
matplotlib.pyplotasplt
rates=pd.read_csv('iouzipcodes2011.csv')
edatasetcontainsninecolumns:
['zip','eiaid','utility_name','state',
'service_type','ownership',
'comm_rate','ind_rate','res_rate']
Togetamorecompletepictureofthedatathatisinthele,itisusefultonext
usethefunctioncall:
.Someoftheinforma-
tionoutputbythisfunctionincludesthefollowing:
ereare37,791rowsofdatainthele,indicatingthatthereare37,791
uniquezipcodes.
eaveragecommercialrate(
)is$0.084/kWh.
eaverageindustrialrate(
)is$0.063/kWh.
eaverageresidentialrate(
)is$0.103/kWh.
estandarddeviationforallratelevelsisapproximately$0.04/kWh.
emaximumresidentialrateis$0.85/kWh.
everyhighmaximumratesforeachoftheratecategoriesareinteresting.
Generatingaquickscatterplotoftheresidentialrateversuszipcodeprovides
someinsight.
plt.xlabel('zipcode')
plt.ylabel('residentialrate')
('ZipCodewithhighestrate:',
())eplotthatisgeneratedisshowninFigure9.6.Mostoftheresidentialelec-
tricratesfortheUnitedStatesarelessthan$0.20/kWh,butthereareafew
extremeoutliers.elastlineofthePythoncodefromthesectionshownabove
usesthe
functiontogettherownumberwiththelargest
valuefortheresidentialrateandthenitprintsoutthezipcodeassociatedwith
thatrownumber.ezipcodeis99634,andaquicksearchshowsthatthisis
thezipcodeforNapakiak,Alaska.
9Statistics
020,00040,00060,00080,000100,000
Residental rate (
/kWh)
Figure9.6
Scatterplotshowingtheresidentialelectricrate($/kWh)foreveryU.S.zipcode.
isisaverylargedatasetthatcoverstheentireUnitedStates,butwemight
wanttofocusinonaspecicstate.enextsectionofthePythonscriptcounts
upthenumberofrowsinthedatasetassociatedwiththestateofTexasandthen
buildsnumpyarraysthatholdtheresidentialandcommercialelectricratesfor
Texasonly.
TXcount=numpy.count_nonzero(rates.state=='TX')
TXcomm=numpy.zeros(TXcount)
TXres=numpy.zeros(TXcount)
count=0
numpy.nditer(numpy.nonzero(rates.state=='TX')):
TXcomm[count]=rates.comm_rate.data[i]
TXres[count]=rates.res_rate.data[i]
count+=1
Anumpyfunctionthathasnotbeenusedbeforeappearsinthecodeabove.
functionconvertsanumpyarrayintoaPythonobject
thatcanbeiteratedover,allowinga
looptoloopoveralltherowsforthe
stateofTexas.SelectdatainthoserowsforTexasisthencopiedintosmaller,
Texas-specicarrays.
Finally,thenalsectionofthePythonscript(below)generatesahistogram
ofthevariousresidentialelectricratesinTexas:
Onequestionconcerningelectricalratesmightbewhetherthereisacorre-
lationbetweentheresidentialrateandthecommercialrate.Figure9.7shows
thecommercialrate(
-axis)versusresidentialrate(
-axis)forthezipcodes
inTexas.EventhoughtherearealargenumberofzipcodesforTexas(456),
thereareamuchsmallernumberofuniqueresidentialandcommercialrates.
ItisalsoclearfromFigure9.7thatthereisastrong,butnotperfect,correlation
9.5U.S.ElectricalRatesExample
0.120
0.120
0.110
0.105
0.100
0.0600.0650.0700.0750.0800.0850.0900.0950.1000.105
Texas commercial electric rate (
/kWh)
Texas residential electrical rate (
/kWh)
Figure9.7
Scatterplotshowingthecommercialelectricrate($/kWh)versustheresidential
electricrate($/kWh)foreveryTexaszipcode.
160
140
120
100
0.0750.080
Texas residential electrical rate (/kWh)
0.0850.0900.095
0.1000.1050.1100.1150.120
Figure9.8
Histogramshowingthefrequencyofzipcodeswithvariousresidentialelectric
rates($/kWh).
betweenthetworates.Wecangoonestepfurtherbycalculatingthe
value
foralinearregressionfunctionthroughthisdata.Using
slope,intercept,r_value,p_value,std_err=\
itcanbefoundthattheresidentialrateisequalto0.87timesthecommercial
rateplus$0.03)andthe
98,indicatingastrongcorrelation.
9Statistics
plt.xlabel('TexasRes.Elec.rate')
plt.ylabel('numberofzipcodes')
andtheresultinggureisshowninFigure9.8.Interestingly,thehighestratein
Texas($0.12/kWh)isalsothemostfrequentratebasedonzipcode.
Problems
Youhavebeenhiredtostatisticallyanalyzethesnowpackforone
basin(orarea)inMontanaoverthepastdecade.Tobegin,goto
theNaturalResourcesConservationServicessnowdata(SNOTEL)
websitecontainingtheMonthlyHistoricSnow(SNOTEL)Datafor
variousbasinsinMontana:http://www.wcc.nrcs.usda.gov/nwcc/rgrpt?
report=snowcourse&state=MT
Youremployerhasmadetheunusualrequestofaskingyoutoanalyze
theannualSnowDatafromarandomsiteofyourchoosing.ewebsite
willallowyoutodownloadatextlecontainingthesnowpackdatafor
variousmonthsgoingbackdierentlengthsintimedependingonthe
year.etextlefollowsthecommaseparatedvalue(*.csv)formatwith
theexceptionoftheheaderinformation.Irecommenddownloadingthe
data,changingtheleextensiontocsvandopeningtheleinExcelso
thatyoucandeleteanyunwantedinformation.Ifyoudoopenthelein
Excel,youshouldbesuretoresavetheleasa*.csvle.
Selectaparticulartimeoftheyear(e.g.,March),andploteithertheSnow
DepthorSnowWaterEquivalentasafunctionofyearforatleastthelast
10years.AddalinetotheplotshowingtheaverageSnowDepthorSnow
WaterEquivalentovertheentiretimeperiodbeingplotted,andaddstan-
darddeviationlinesifpossible.Writeabriefreportsummarizingyour
ndingsregardingtheaveragesnowdepthorsnowwaterequivalentand
thevariingsabilityfromyeartoyear.
Youhavebeenhiredbyamajorclothingmanufacturingtoprovidesome
analysisandgures(plots)onhistoricalclothingsalestrendsandclothing
advertisingspending.Beginbydownloadingpublicallyavailablecloth-
ingexpendituredatafromhttp://www.stat.u.edu/winner/data/clotthes_
expend.csvandthedescriptionofthedata(optional):http://www.stat.u
.edu/winner/data/clothes_expend.txt.ecsvdatalecontainsacolumn
ofdataontherealUSGDPinbillionsofdollarsthatincludequotation
marksaroundthenumber.Itisrecommendedthatthiscolumnbedeleted
byopeningtheleinExcelbeforeimportingtheleintoaPythonarray.
AfterimportingthedataintoPython,youareaskedtogenerateaplotof
clothingsales(inbillionsofdollars,secondcolumnofdata)foreveryyear
thatdataisavailable.Youarealsoaskedtogenerateasecondplotshowing
Problems
expendituresonadvertisingasapercentofGDP(lastcolumn)alongwith
theaveragepercentageandstandarddeviationoverthefulltimeperiod.
Finally,returningtotheoriginalclothingsales(inbillionsofdollars,
secondcolumnofdata)peryeardata,uselinearregressiontodetermine
theslopeofthecurveoverthe
mostrecent10yearsofdata
.eslope
willrepresenttheaverageincreaseinclothingsalesperyearforthe
last10years.Writeabriefmemototheclothingmanufacturerthat
summarizesyourndingsonthechangesinsales,salestrends,and
averageadvertisingspendingasapercentageofGDP.
In2015,theNewEnglandPatriotswereaccusedofillegallydeatingthe
footballsthattheyusedonoenseduringtheAFCChampionshipgame.
WellsReport
wasreleasedinMay2015,andcontainsthendingsofa
specialcommissionhiredbytheNFLtoinvestigatetheaccusations.e
primaryndingofthecommissionwas,itismoreprobablethannotthat
NewEnglandPatriotspersonnelparticipatedinviolationsofthePlaying
Rulesandwereinvolvedinadeliberateeorttocircumventtherules.
Inotherwords,itisstatisticallyprobablethatthePatriotsdeatedtheir
footballsbelowthe12.5psigminimumallowedbythePlayingRules.
eprimarypressuredatausedtomakethisdeterminationisfromthe
pressuremeasurementsoftwodierentrefereesusingtwodierentpres-
suregaugesathalftime.ItisassumedbythereportthatthePatriotsfoot-
ballswereallinatedto12.5psigatthestartofthegameandbefore
tampering.eColtsfootballswereallinatedto13.0psigatthestart
ofthegame.(Itisunclearwhichpressuregaugewasusedatthestartof
thegame.)ehalftimepressuredatathatwascollectedissummarized
PatriotsBallBlakemanPrioleau
111.5011.80
210.8511.20
311.1511.50
410.7011.00
511.1011.45
611.6011.95
711.8512.30
811.1011.55
910.9511.35
1010.5010.90
1110.9011.35
9Statistics
ColtsBallBlakemanPrioleau
112.7012.35
212.7512.30
312.5012.95
412.5512.15
YouhavebeenhiredbytheNewEnglandPatriotstorepeatsomeofthe
statisticalanalysispresentedintheWellsReportandthensubmityour
ownexpertopinion.Specically,calculateaveragepressure
decrease
standarddeviation)forthePatriotsandColtsballsbasedonthemea-
surementsofeachpressuregauge.A
-testisusedtocomparetwomeans
(ortwoaverage)anddeterminetheprobabilitythattheyarethesame.Use
-test(
isrecommended)todetermine
theprobabilitythattheaveragepressuredropofthePatriotsfootballsis
dierentfromthepressuredropoftheColtsfootballsforeachsetofmea-
surements.(ExtraCredit)ePatriotshaveoeredyouabonuspayment
ifyouareabletogenerateabarchartshowingtheaveragepressuredrop
andstandarddeviationforthetwodierentsetsofmeasurementsfortheir
footballs.
Finally,thePatriotshaveaskedyoutosummarizeyourownexpert
opinionbasedonthedataandfactspresentedhere.Doyouthinkthereis
strongevidenceofdeationinviolationofthePlayingRules.Donotcon-
siderfactsbeyondthepressuredata(e.g.,textmessagesincludedinthe
report).
Youhavebeenhiredbyalocallawyerthatisdefendinganindividual
accusedofdrivingundertheinuenceofalcohol.edefendants
argumentisthatthebreathalyzertestusedtomeasuretheindividuals
bloodalcoholconcentration(BAC)isnotreliableorconsistent.e
lawyerisrelyingondatafromapreviouslypublishedstudybyGullberg
[2].Inthisstudy,10breathalcoholsamplesweretakenfromdierent
subjectsapproximately20sapart.edefenseattorneysargumentis
thatareliabletestshouldgivethesamemeasurementforall10samples,
butthisstudyshowedsomevariability.
Yourspecictaskasaconsultantistogenerateguresthatthelawyercan
useduringthetrial.eguresshouldshowtheBACmeasurementfor
eachofthe10samplesfromasinglesubjectaswellastheaverageand
standarddeviationlinesforthesamplesfromtheindividual.elawyer
hasrequestedguresforatleasttwodierentsubjects.Acsvlecontain-
ingthedatafortwodierentsubjectsisavailableinthepaperreferenced
aboveorhttp://www.stat.u.edu/winner/data/breath_reg.dat.
References
Tohelpyougenerateexactlytheplotdesired,thelawyerdrewasketch
onanapkinshownbelow.InadditiontotheBACgures,thelawyeralso
needsyourreporttoincludetheaveragesandstandarddeviationsforthe
datafromthetwosubjects(Figure9.9).
0.125
0.120
0.115
0.110
0.105
0.100
Sample
BAC
810
Figure9.9
10.2NumericalDierentiation
distance
awayfrom
h
d
(
x
(
x
h
(
x
x
where
representsatermwithasizeontheorderof
.Equation10.2
canbeseentobeequivalenttothepreviousTaylorpolynomialequation8.3by
replacing
with
10.2.1FirstDerivativeApproximation
Ourgoalinthissectionistoapproximate
,andwecanrearrangetheTaylor
polynomial(equation10.2)togive
f
(
x
h
(
x
+
O
(
h
)
x
f
(
x
h
(
x
isapproximationistypicallyreferredtoastheforwardapproximationofthe
rstderivative,andtheerrorassociatedwiththeapproximationisoforder
Aswewillseeinthenextfewchapters,numericalapproximationofderivatives
isoftenperformedonasequenceofpointsthatareseparatedbyaxeddistance
.Hence,itiscommontosimplifythisnotationslightlybyreplacing
with
,seeFigure10.1,sothatitclearlyreferstothenextpointin
asequenceofpoints.Usingthisnotation,theforwarddierenceapproximation
ofthederivativebecomes
f
+
1
f
euseofthisapproximationisshowninFigure10.1(b).Clearly,theapprox-
imationoftheslopebecomesmoreaccurateasthedistancebetweenthetwo
pointsshrinkstowardzero.
eTaylorpolynomialat
andusingthesamederivationasabove,leadstothebackwarddierence
approximationofthederivative:
f
f

1
whichhasthesame
accuracyastheforwarddierenceapproximation.
euseofthebackwarddierenceapproximationisshowninFigure10.1(a).
10NumericalDierentiationandIntegration
f
(
x
)

1
+1

1
f
(
x
)

1
+1
+1

1
f
(
x
)

1
+1
+1

1
(a)(b)
Figure10.1
Threedierentnitedierenceapproximationsforthederivativeat
(a)backwarddierence,(b)forwarddierence,and(c)centereddierence.
enalmethodforapproximatingthederivativeof
isbysubtract-
ingtheTaylorpolynomialfor
(equation10.6)fromthepolynomialfor
(equation10.2).Noticethatthe
termsareeliminatedand,more
importantly,itcanbeshownthatthe
termsareeliminatedleavinga
term.eresultingcentereddierenceapproximationoftherstderivativeis
f
+
1
f

1
whichis
accurate.Asaresult,as
0,thecentereddierence
approximationismuchmoreaccuratethaneithertheforwardorbackward
10.2NumericalDierentiation
dierenceapproximation.ecentereddierenceapproximationisillustrated
inFigure10.1(c).
Toillustrateboththeimplementationandaccuracyofnumericalapproxi-
mationsfortherstderivative,thePythoncodebelowwascreated.ecode
hasthefunction
declarednearthebeginning,andthecode
hasasecondfunctionthatcalculatestheexactderivative.ederivativeat
0isapproximatedusingthethreedierentapproximationsdescribed
above(equations10.5,10.7,10.8),andtheaccuracyofeachapproximationis
calculated.
10NumericalDierentiationandIntegration
Table10.1
Accuracyofdierentnumericalapproximationsoftherstderivative
Forwarddierence
error
Backwarddierence
error
Centereddierence
error
0.16
0.011
0.0011
0.00011
10.2.2SecondDerivativeApproximation
esecondderivativeofafunctioncanalsobenumericallyapproximated,and,
onceagain,weturntotheTaylorpolynomialtoderiveanequationforthe
approximation.Recallingthat
h
(
x
O
(
h
(
x
h
(
x
x
h
(
x
wecanaddthesetwoequations(10.9and10.10)together(notethattherst
derivativetermscancelandthe
termscancel)giving
isequationcanberearrangedtosolveforthesecondderivativeandthe
notationsimpliedtoyield
f
+
1
2
f
f

1
whichis
accurate.isapproximationisalsocalledthecentereddier-
enceapproximationforthesecondderivative.ereareotherapproximations
thathavebeenderivedforthesecondderivative,butthisoneapproximationis
usedinthevastmajorofengineeringalgorithmsintheauthorsexperience.
eaccuracyofthecentereddierenceapproximation(equation10.12)of
thesecondderivativeisexaminedusingthePythonscriptbelow.
10.2NumericalDierentiation
exactSecDeriv(x):
10NumericalDierentiationandIntegration
10.3NumericalIntegration
("Scipyderiverroris%.4e"%
-exact))
("Scipysecondderiverroris%.4e"%
dx=0.0001,n=2)-exactSec))
resultsintheerrorbeing:
Scipyderiverror
Scipysecondderiverror
andthisisidenticaltothecentereddierenceapproximationspresentedhere.
10.3NumericalIntegration
ecalculationofthedeniteintegralofafunctioninonedimensionisidenti-
caltocalculatingtheareabetweenthefunctionandthe
-axis.Ifwewerenotat
allconcernedwithaccuracy,wecouldapproximatethefunctionwithastraight
linebetweentheboundsonthedeniteintegral,
.eresultingpolygon
wouldbeatrapezoidandwecouldeasilyapproximatetheareawith
f(a)+f(b)].eerrorassociatedwiththisapproximationdependsonthenonlinearityof
thefunction(i.e.,roughly,thesecondderivative)butformostproblems,the
useofasingletrapezoidisnotsucientlyaccurateandmuchbetteraccuracy
ispossibleatamodestcomputationalcost.
Iftheregionunderthefunctionissubdividedinto
intervalsofwidth
,thenarbitrarilyhighaccuracyispossiblebyincreasing
Approachesthatusemultiplepolygonstoapproximatetheareaunderthe
curvearecalledcompositemethod.esimplestcompositemethodisto
dividetheregionunderthefunctionintorectangles.Eachrectangletypically
hasthewidth,
,andtheheightofeachrectangleisdeterminedbyevaluating
thefunctionatthemidpointoftherectangleswidth(i.e.,themidpointofthe
rstrectangleis
2).isapproachiscalledthecompositemidpointrule
becausethemidpointisusedtodeterminetheareaofeachrectangle.e
midpointruleprocessisillustratedinFigure10.2.eequationdescribingthis
approximateintegralis
where
isatthemidpointofeachsubdomain.eerrorassociatedwiththis
approximationis
,whichmeansthatusingtwiceasmanysubdomains
(i.e.,reducing
inhalf)resultsinafactorof4reductionintheerror.
10NumericalDierentiationandIntegration
(
x
)
x
Figure10.2
Thefundamentalideabehindthenumericalapproximationofadenite
integral(from
)istoestimatetheareaunderthecurveusingsimplesubdomainswith
areasthatareeasytocalculate.Thecompositemidpointruleisillustratedhere.
Atestproblemusingthemidpointrule(equation10.13)toapproximate
issolvedbythePythonscriptbelow.
10.3NumericalIntegration
inrange
area=area+h*(fun(x[i]))
("Midpointruleerror:%.4e"
%math.fabs(exact-area))
eonelineinthisPythoncodethatissomewhatmorecomplexthantheoth-
ersistheconstructionofthevector,
,thatholdsthemidpoints.ekeyin
constructingthisvectoristostartatthemidpointoftherstinterval(
andlinearlyspacethepointstothemidpointofthelastinterval(
2).Using
100subinterval(or100rectangles)themidpointruleresultsinanerrorof
approximately1
,whichissucientlyaccurateformostengineering
problems.
10.3.1TrapezoidRule
Insteadofapproximatingtheareaunderthefunctionwithasequenceofrect-
angles,asequenceoftrapezoidscouldbeusedinstead.eprocessisillustrated
inFigure10.3.eequationdescribingthisprocessis
Interestingly,theaccuracyofthecompositetrapezoidruleisthesameasthe
compositemidpointrule,
,butthetrapezoidrulehasthesameoverall
(
x
)
x
Figure10.3
Theareaunderafunction(i.e.,thedeniteintegralofafunction)canbe
estimatedbysubdividingtheareaintoasequenceoftrapezoids.
10NumericalDierentiationandIntegration
computationalcostandissometimesmoreaccuratebyafactorof2.Whenever
wereadthattheorderofaccuracyis
),wecanthinkofthisassay-
ingthattheerrorisequalto
where
isaconstant,orwecanthinkofthis
assayingthattheerrorisproportionalto
.Iftheerrorofthecompositemid-
pointruleis
,thentheerrorassociatedwiththecompositetrapezoidrule
forthatsameproblemsis
.Becausethecompositetrapezoidruleprovides
twicetheaccuracyforthesamecomputationalcost,itisgenerallypreferred
overthecompositemidpointrule.
ecompositetrapezoidruleisstraightforwardtoimplementinPython,and
an(inecient)implementationisgiveninthescriptbelow.isimplementa-
tionisinecientbecausethefunctionbeingintegratedisevaluatedtwiceatthe
samelocation,
.isisobviouslyawaste,butthealgorithmissofastthatthis
littleineciencydoesnotreallymatter.
10.3NumericalIntegration
midpointofasubdomainortheendpointsofasubdomain.Higheraccuracy
canbeachievedbyevaluatingthefunctionbeingintegratedatstrategically
placedpointsthatminimizetheerrorassociatewiththeapproximation.
eseoptimalpointsarenotattheendsofthesubdomain,andtheyare
notevenlyspacedthroughoutthesubdomain.Fortunately,mathematicians
havepreviouslydeterminedthelocationsoftheseoptimalpoints,theyare
called
Gausspoints
,andthenumericalintegrationmethodiscalled
Gaussian
quadrature
AnalgorithmthatimplementstheGaussianquadratureapproachfornumer-
icallyapproximatingintegralsisincludedinthe
library.
euseofthisalgorithmisillustratedinthePythonscriptbelow.
scipy.integrate.quadratureasquad
10NumericalDierentiationandIntegration
isanelementaryfunction.Oneexampleis
isintegrandarisesfrequentlywhensolvingheatconductionordiusion
problemsonsemi-innitedomains.Forexample,modelingthediusionof
adrugfromaskinpatchintothetissuebelowmayresultinthisintegrand.
Anotherexampleisthemodelingofsoiltemperaturesnearthesurface,which
oscillatesduetosolarheatingbuttheoscillationsdecaywithdepth(i.e.,the
temperaturedoesnotchangeafewfeetbelowthesurface).
isintegrandarisessofrequently,thataspecialfunction(i.e.,nonelemen-
taryfunction)hasbeendened.eerrorfunctionisdenedas
erf
evalueofthisfunctioncanbeobtainedusingtablesormanysoftware
librarieshavingfunctionsthatprovidehighlyaccurateapproximations.In
Python,the
functionisavailableinthe
library.ePythonscriptbelowapproximatestheintegralto
usingtheMidpointrule(equation10.13),thetrapezoidrule(equation10.14),
andGaussianquadrature.eseapproximationsarethencomparedtothe
hopefullyhighlyaccurateapproximationavailableusing
scipy.integrate.quadratureasquad
scipy.specialasss
10.3NumericalIntegration
#GaussianQuadrature
estimate=quad(fun,a,b)
("GaussianQuadratureerror:%.4e"
%math.fabs(exact-estimate[0]))
#MidpointRule
x=numpy.linspace(a+h/2,b-h/2,num=n)
area=0
inrange
area=area+h*(fun(x[i]))
("Midpointruleerror:%.4e"
%math.fabs(exact-area))
#TrapazoidRule
x=numpy.linspace(a,b,num=n+1)
area=0
inrange
area=area+h*(fun(x[i])+fun(x[i+1]))/2.0
("Trapazoidruleerror:%.4e"
%math.fabs(exact-area))
eexactintegralisassumedtobethehighlyaccurateapproximationfrom
,andthedierencebetweenthisexactintegral
andGaussianquadratureusing
.Usingnumericalintegrationwiththemidpointruleandthe
trapezoidruleandasfewas10intervalsalsoprovidesarelativelyaccurate
solutionassummarizedinTable10.2.
Table10.2
Accuracyofthemidpointandtrapezoid
ruleforintegrating
withdierent
numbers,
ofsubintervals
MidpointerrorTrapezoiderror
101
1001
10001
10NumericalDierentiationandIntegration
Problems
functionhasanoptionalinput
argument
thatspeciesthenumberofpointstouseinthedier-
enceapproximationandmustbeanoddnumber.edefaultvaluefor
thisfunctionis3,andthecentereddierenceapproximationsderived
atthestartofthischapterwerealsobasedonthreepoints.Evaluate
theimpactofsetting
to5and7whilechanging
to0.01,
0.001,and0.0001forthefunctionusedpreviously,
.What
doyouobserve?Describethepotentialadvantagesanddisadvantages
associatingwithusingahigherorderapproximation.
Youhavebeenhiredbyamathematicssoftwarecompanythathaspre-
viouslybeenusingthecompositetrapezoidruletonumericallyapprox-
imatetheintegralsofvariousmathematicalfunctionsoftheform:
Forthecompositetrapezoidrule,theintegrationinterval,
a,b],is
dividedintosmallerpiecesandthentheintegralofeachpiece
isapproximatedusing
f(xi)+f(xi+1)].esoftwarecompanyhashiredyoutoimplementSimpsonsrule,
whichissimilartothetrapezoidruleexceptthatinsteadofapproxi-
matingtheintegralofeachpiecewithatrapezoid,theintegralofeach
pieceisapproximatedusingaquadraticpolynomial.us,theintegral
ofeachpieceoftheintervalisapproximatedusing
f(xi)+4f(xi+1)+f(xx+2)].isapproximationisillustratedinFigure10.4.Simpsonsruleshould
beimplementedandtestedbyintegrating
foraspecied
numberofpoints,
.Notethateachintervalconsistsof3points,which
meansthatthereareonly
intervals(and,
mustbeoddsothat
5pointscorrespondsto2intervals)andaloopthroughtheintervals
shouldhaveastepsizeof2.EvaluatetheaccuracyofSimpsonsrulefor
dierentvaluesof
thedistancebetweenthepoints.IsSimpsons
rulemoreaccuratethanthetrapezoidruleforagiven
ecalculationoftheamountofenergyrequiredtochangethetemper-
atureofamaterialisperformedfrequentlyinprocessengineering.For
example,ifwehaveacubicmeterofnitrogenorakilogramofcarbon,
Problems
(
x
)
a
(
a
+
b
)/2

b
x
Figure10.4
IllustrationofSimpsonsruleforintegrationof
from
howmuchenergyisrequiredtoraisethetemperatureby1or100
ecalculationfrequentlyrequiresintegratingapolynomialbetween
thestartingandendingtemperatures.Areneryhashiredyoutocreate
aPythonprogramthatbothautomatesthesecalculationsandcompares
twodierentnumericalmethodsfortheapproximationofintegrals.
Assumingaconstantpressureprocess,theenthalpychangeassociated
withatemperaturechangefrom
where
istheheatcapacityasafunctionoftemperatureatconstant
pressure[1].eheatcapacityofamaterialasafunctionoftempera-
tureisfrequentlygiventhroughapolynomialrelationship.erenery
isparticularlyinterestedintwomaterials:(1)nitrogengasand(2)carbon
solid.eheatcapacityofnitrogengasinkJ/(mol
C)isgivenby
where
shouldbein
C.eheatcapacityofcarbonsolidinkJ/(mol
isgivenby
where
shouldbein
Yourcontractwiththereneryasksyoutocalculatetheenthalpy
changeforeachmaterial(nitrogengasandcarbonsolid)startingat
atemperatureof20
Candraisingthetemperatureto100
C.Report
10NumericalDierentiationandIntegration
theenthalpychangeinkJ/mol.Youshouldnumericallyapproximate
theintegralusingtheGaussianquadraturealgorithmincludedin
Scipy(
).Inaddition,usethe
Trapazoidrulewith10intervalstoapproximatetheenthalpychangeof
nitrogengasandreportthedierencebetweenthetrapazoidruleand
Gaussianquadrature.
Reference
Felder,R.M.andRousseau,R.W.(2005)
ElementaryPrinciplesofChemical
Processes
,JohnWiley&Sons,Inc.,Hoboken,NJ,3rdedn.
11InitialValueProblems
thoseelds,ifaprocessiscontrolledbytheabovesetofreactions,thenwecan
describethechangeintheconcentrationofsubstrateinaclosesystem(i.e.,a
systemwithnoinowandoutowlikeasealedwinebottle)withtheequation:
max
where
istheconcentrationofsubstrate,
max
isaparameterthatdescribes
themaximumreactionrate(i.e.,fullutilizationofallenzymesbecausehigh
concentrationof
ispresent)and
isthesubstrateconcentrationatwhich
thereactionrateishalfof
max
.Inordertosolveaninitialvalueproblem,we
seektodeterminethesubstrateconcentration,
,thatsatisedtheequation
above,thatis,therateofchangeofthesubstrateisequaltoafunctionthat
dependsonthecurrentconcentration.
eequationfor
describesthelossorconsumptionof
.erateatwhich
isconsumedmustbeexactlyequaltotherateatwhich
isproduced(i.e.,
everyethanolmoleculethatisreactedawayformsanaceticacidmolecule)so
wecanalsowriteanequationfortheformationof
max
S
1.0
0.00.51.01.5
Substrate
Product
Time (dimensionless)
Concentration (dimensionless)
Figure11.1
Substrateconsumption(solidcurve)andproductformation(dashedcurve)for
11.3ForwardEuler
initialconcentrationof1.0(dimensionless)forthesubstrateandaconcentra-
tionof0.0fortheproduct.eordinarydierentialequations(ODEs)were
solvedusing
,whichisdescribedlaterin
thischapter.
11.3ForwardEuler
Inthissection,theforwardEulermethodisdescribed,anditisthesimplest
approachforsolvinganinitialvalueproblem.Anyinitialvalueproblemcanbe
writtenas
andtheinitialcondition
mustbeincluded.ekeytounderstand-
ingtheprocessforapproximatelysolvingtheinitialvalueproblemistosimply
recognizethattheinitialvalueproblemgivesus(1)astartingpoint,
,and(2)a
slope(
)!Wecanusethesetwopiecesofinformationtomakeanestimateasto
wheretheunknownfunction,
,isgoinginthenearfuture.Takingasmallstep
fromtheinitialvalueinthedirectiongivenbytheslopeleadstoanewvalue
for
atanewtimepointthatisslightlydierentfromwherewestarted.At
thisnewlocation,wecanagaincalculatetheslopeandtakeasmallstepinthe
directiongivenbytheslope.isprocessisillustratedinFigure11.2.epro-
cessdescribedhereestimatesthefuturebasedONLYonthecurrentconditions.
iscategoryoftime-steppingmethodsforsolvinginitialvalueproblemsare
called
explicit
methodsbecausetheestimatesofthefuturearebasedexplicitly
onthepresent.
y
Slope=
=
f
(
t
Figure11.2
Anapproximatesolutiontoaninitialvalueproblemcanbeobtainedby
calculatingaslopefromtheODEequationbasedonthecurrentvaluesfor
,andthen
11InitialValueProblems
econstructionofanalgorithmthatimplementstheprocessdescribed
abovebeginswiththecreationofafunctionthatcontainstheODEand
returnstheslope(i.e.,thetimederivative)basedonagiventime,
,andvalue
forthedependentvariable,
.eforwardEulerprocessisbasedonaniteration
wheretheslopeiscalculatedandthen,basedonthesizeofthetimestep,
newvaluefor
iscalculated.RecallfromChapter10thattherstderivativeof
afunctioncanbeapproximatedwithanitedierenceapproximation:
y
y

1
ecurrentvalueofthedependentvariable,
,andtime,
,isusedtocal-
culatetheslopeandsolveforthenewvalueofthedependentvariable,
.e
previousequationcanberearrangedtogive
whichistheforwardEulermethod.ePythonscriptbelowimplementsthe
forwardEulermethodtopredictthechangeinsubstrateconcentrationforthe
MichaelisMentenreaction.
11.3ForwardEuler
erstsectionofthisscriptcontainstheODEfunctionwithinaseparate,
callablefunction.einputstothisfunctionarethecurrentvalueforthe
dependentvariable(substrateconcentration)andthecurrenttime.efunc-
tionreturnstheslope,
.eforwardEuleralgorithmrequiresthattimebe
discretizedintosmallsegments,and,tofacilitatethisprocess,thealgorithm
buildsavectorthatholdsallthetimepointsusingthe
function.einitialconditionandstoragespaceforthenalsolutionarethen
set.eforwardEuleriterationisverysimplebecauseitcalculatesanewvalue
forthedependentvariablebasedontheslopeandthetimestepsize.
Figure11.3showsaplotofthesubstrateconcentrationversustimeusingthe
forwardEulermethodwithtwodierenttimestepsizes.esolidcurveis
basedon100timesteps(
02)andthedashedcurveisbasedon4time
steps(
5).elargetimestepsusedforthedashedcurveshowhowthe
slopeoftheapproximationisequaltotheslopeatthebeginningofthetime
step.Duringthetimestep,theslopedecreasesasthesubstrateconcentration
isreducedsothereisasignicanterrorassociatedwithusingthelargetime
step.eaccuracyoftheforwardEulermethodisofthesameorderasthefor-
wardnitedierenceapproximationofthederivative,thatis,theerroris
Ifthelengthofatimestepiscutinhalf,theerrorisalsohalved.Fortunately,the
forwardEulermethodhasarelativelylowcomputationalcostsotakingalarge
numberoftimestepsisfeasiblehere,butifweneedtosimulateamuchlonger
periodoftimeorifwearesolvingmanyinitialvalueproblemssimultaneously,
theverysmalltimesteprequirementquicklybecomesanissue.Tobeginto
addressthisaccuracylimitation,wenowturnourattentiontoimprovingthe
orderofaccuracy.
1.0
0.51.01.52.0
Time (dimensionless)
Concentration (dimensionless)
Figure11.3
SubstrateconcentrationgovernedbyaMichaelisMentenreactionismodeled
11InitialValueProblems
Wenowhavetwoestimatesfortheslope,oneat
andoneat
.Usingan
averageofthesetwoestimatesgivesusabetterestimateoftheslopeoverthe
timespanofinterest.Usingthisprinciple,themodiedEulermethodcalculates
anewvalueforthedependentvariableusing:
where
iscalculatedusingequation11.4(i.e.,forwardEuler).eaccuracy
ofthemodiedEulerapproachis
,whichleadstosignicantlysmaller
errorsforsmalltimestepsizes.
APythonscriptthatutilizesthemodiedEulermethodforthe
MichaelisMentenkineticsproblemisgivenbelow.
11.5SystemsofEquations
#Iterationovertimesteps
slope=df(sol[i-1],t[i-1])
est=sol[i-1]+dt*slope
sol[i]=sol[i-1]+0.5*dt*(slope+df(est,t[i]))
#Plotfinalsolution
pylab.xlabel("time(dimensionless)")
pylab.ylabel("concentration(dimensionless)")
Overall,thealgorithmisverysimilartotheforwardEulermethod,butithasan
extrastepintheiterationlooptoestimatethevalueofthedependentvariable
attheendofthetimestep.
11.5SystemsofEquations
eenzymaticdegradationofasubstratetoaproductinvolveschangesin
concentrationtoboththesubstrateandtheproduct.Inthisandmanyother
systems,achangeinonesystemparameterhasanimpactonmanyother
systemparameters.Fortheseproblems,multipleinitialvalueproblemsmust
besolvedsimultaneously.isimpliesthatthereisavector,
,ofthedependent
variableandavectoroftheODEs:
Fortunately,theextensionofthemethodsdescribedabovetoasystemofODEs
istrivial.eODEdenitionfunctionneedstobemodiedslightlysothat
itcanreceiveavectorofdependentvariablesanditmustreturnavectorof
derivatives(orslopes),butotherwisethereislittlechange.
Aswemovetowardsystemsofequations,thecomputationalcostscan
increasequicklyanditisoftenadvantageoustoutilizetheinitialvalueproblem
algorithmsavailableinthe
library.esealgorithms
haveanumberofhelpfuladvantages:
1)highaccuracyoftenfourthorder(
)orhigher,
2)errorcheckingcomparingthepredictionofafourth-andfth-order
method(or,ingeneral,comparingthepredictionoftwomethodswith
dierentordersofaccuracy)eachtimestepallowsthealgorithmtoadjust
thetimestepsizeinordertomaintainadesiredlevelofaccuracy,and
3)fastexecutionthealgorithmsareoftenwritteninFORTRANorCand
executefasterthanapurelyPythonalgorithm.
esimplestinitialvalueproblemsolverintheScipylibraryis
.isfunctionmustbegiventhenameofafunction
11InitialValueProblems
containingtheODEs,theinitialcondition(s),andthetimespanforintegra-
tion.euseofthisfunctionformodelingboththesubstrateandproduct
concentrationforaMichaelisMentenkineticsproblemisillustratedinthe
Pythonscriptbelow.

(
y

x
)
11.5SystemsofEquations
x
(


z
whereLorenzset
3,and
28.ePythonscriptbelowsolves
thissystemofequationsusingthesameparametersthatLorenzderived,andit
usesrandominitialvaluesfor
,and
thatarebetween
2and
numpyasnp
scipy.integrateassint
matplotlib.pyplotasplt
sigma=10.0
11InitialValueProblems
eLorenzequationsarepossiblythemostfamousinitialvalueproblemof
thepast50years,buttheywouldbecompletelyforgottenifitwerenotforone
veryinterestingobservation.BeforetheLorenzequationswerenumerically
solvedonanearlycomputer,thecommonassumptionwasthatthesolution
toaninitialvalueproblemwasnotoverlysensitivetotheinitialconditions
used.Smallchangesininitialconditionwereassumedtobeunlikelytocause
alargechangeinthenalsolution.Forexample,Figure11.4showstwo
dierentsolutionstotheLorenzsystemthathaveslightlydierentinitial
values.etwosolutiontrackclosetoeachotherandultimateoscillateinvery
similartrajectories.iswastheexpectedbehaviorforanytwosimilarinitial
conditions.
Fortunately,whenLorenzenteredtheinitialconditionthathedesiredfor
hismodelintothecomputer,hemadeamistakeandchangedtheinitial
conditionsslightly.esolutionthatheobservedwascompletelydierent
fromwhatheexpected.IttooktimeforLorenztondhisinitialcondition
mistake,but,whenhedid,herecognizedtheimplicationsofwhathehad
found:
thesolutiontothissystemofequationswashighlysensitivetothe
initialconditions
.Figure11.5showsanexampleofthisbehavior.etwo
initialconditionsaresimilar,butthenalsolutionsareverydierent.e
implicationofthisresultiscritical:forsomesystemssuchasatmospheric
dynamics(i.e.,weatherprediction),wemusthavehighlyaccurateinitial
conditionsbecauseslighterrorsininitialconditionscouldleadtovery
inaccuratepredictions.isobservationledtothedevelopmentoftheeld
ofchaos[1].efundamentalbehaviorofchaoticsystemsisthattheyare
unpredictablebecauseweneverknowtheinitialconditionsexactly.For
example,ifabutteryapsitswingsothecoastofSouthAmerica,andwe
donotknowit,wemightmispredicttheweatherinFloridainthecoming
weeks.
15
10
10
10
Figure11.4
Twodierent
solutionstotheLorenzsystem
ofequationsusingslightly
dierentinitialconditions.The
threeaxesrepresentthethree
solutionsvariables,
,and
andtimeisnotshownother
thanthevariablesoscillateina
cyclelongterm.
11.6StiDierentialEquations
Figure11.5
Twodierent
solutionstotheLorenzsystemof
equationsusingslightly
dierentinitialconditions.The
nalsolutionsareverydierent
eventhoughtheinitial
conditionsaresimilar.
10
10
15
11.5.2Second-OrderInitialValueProblems
Althoughtheyarerareinsomeengineeringelds,forexample,chemicalor
biologicalengineering,someinitialvalueproblemsinvolvesecond-orderODEs
andtwoinitialconditions.eclassicprobleminthiscategoryisthemotionof
abodyundergravitationalforce.If
isthepositionofthebody,thenNewtons
rstlawstates:
where
isgravitationalacceleration.Solvingthisequationrequirestwoinitial
conditions:aninitialposition,
,andaninitialvelocity,
Solvingsecond-orderinitialvalueproblemsisrelativelystraightforward
usingthealgorithmsalreadydescribedbecausesecond-order(orhigher-order)
problemscanberewrittenassystemsofrst-orderequationsbydening
newvariables.Fortheproblemabove(equation11.6),dening
allows
thesecond-orderproblemtoberewrittenasasystemoftworst-order
equations:
andnoticethatoneinitialconditioncanbeusedforeachequation:
11.6StiDierentialEquations
eforwardEulermethodandotherexplicittime-steppingmethodsarevery
simpleandcomputationallyecientforsolvinginitialvalueproblems,butthey
11InitialValueProblems
haveanimportantlimitation.UsetheforwardEulermethodtosolvetheinitial
valueproblem:
25sin
cos
with10timesteps(
2).esolutionshowninFigure11.6wasobtained
whenattemptingthisapproach.isisvery,veryfarfromthecorrectsolution.
Infact,theerrorassociatedwiththisapproximationisgrowingexponentially,
andifweintegratebeyond
0,theapproximationonlybecomesworse.
TohelpusunderstandwhytheforwardEulermethodfailedsobadlyfor
thisproblem,itishelpfultolookattheplotoftheexactsolutionshownin
Figure11.7.eimportantthingtorecognizeaboutthesolution(
)isthattherearetwodierenttimescalespresentthereisaveryfast
timescalethatcausesarapid,initialdecreaseinthesolution,andthenthereis
aslowertimescaleassociatedwiththeoscillationsfromthesin
function.
deningcharacteristicofstidierentialequationsistwoormoretimescales
Ifonlythefasttimescaleexisted,thesolutionwouldquicklyreachsteadystate
andthesimulationcouldfocusonthebriefperiodwhenallthechangesoccur.
Ifonlytheslowtimescaleexisted,longertimestepscouldbeusedinobtaining
anapproximatesolution.Withstidierentialequations,thetimestepmustbe
smallenoughtocapturethefasttimescaleevents,butthosesmalltimesteps
resultinlargecomputationalcostassociatedwithsimulatingtheslower,longer
timescale.
1.0
Time
10,000
15,000
1.52.0
Figure11.6
25sin
cos
0and
2.The
approximationerrorislargeandgrowingexponentially.
11.6StiDierentialEquations
1.0
0.00.51.0
Time
1.52.0
Figure11.7
Theexactsolutionandthesolutionresultingfromusing
ontheinitialvalueproblem
25sin
cos
0and
2.Thetwocurvesoverlapone
another.
euseofexplicittime-steppingmethodsonastiODErevealsthemajor
weaknessofthesemethods.Recallthatexplicitmethodspredictthefuture
directionofthesolutionusingonlycurrentlyavailableinformation.Ananalogy
wouldbetowalkaroundwatchingonlythegroundexactlyatyourfeetbased
onthetopologyatyourfeet,youtakeastepanticipatingthatthetopologyofthe
groundisnotgoingtochangedramaticallyoverthatstep.Forproblemswith
onlyonetimescale,thatassumptionholds.StiODEs,however,haveaslow
changingtopology(theslowtimescale)andacliorfastchangingtopology.
Whenanexplicitmethodhitsthefasttimescale,itisanalogoustosteppingo
acliandexponentialerrorincreasesresult.
Anumberofdierentalgorithmshavebeendevelopedtoaddressthechal-
lengeofstiODEs.ealgorithmusedby
isonesuchalgorithm[2],andifthisfunctionisusedtosolvethestiexample
problemabove,thesolutionisindistinguishablefromtheexactsolution.
Figure11.7actuallycontainstwocurvestheexactsolutionandtheapproxi-
matesolutionfrom
.Algorithmsdesigned
forstiODEsutilizetwostrategiestohandlethemultipletimescaleswithin
theproblem.erststrategyistocontinuouslychecktheaccuracyofa
predictedsolutionforeachtimestepandthencontinuouslyadaptthesize
ofthetimesteptothesizerequiredtomaintainaccuracy.Byadjustingthe
timestepsize,thesealgorithmscantakesmallertimestepswheneverfaster
timescalesarecausingrapidchangesandtakelargertimestepswheneverthe
changesareslow.isrststrategyishelpfultominimizethecomputational
11InitialValueProblems
costsassociatedwithstiODEs,but,byitself,thisstrategyisnotsucient
andasecond,criticaltechniqueisrequired.
Explicittime-steppingmethodsforsolvinginitialvalueproblemsallhavethe
basicform:
wherethepredictedsolutionisonlybasedoncurrentvalues(orestimatesof
thefuturethatarestill,ultimately,basedoncurrentvaluesasisthecaseofthe
modiedEulermethodorthepopular,explicitRungeKuttamethods.)e
alternativetoexplicittimesteppingisimplicittimesteppingwheretheslopeor
changeinthedependentvariablesoverthenexttimestepisnotsimplybased
oncurrentvaluesbutalsobasedonfuturevalues.Implicitmethodshavethe
basicform:
Forexample,thesimplestimplicitmethodisthebackwardEulermethod,and
ithastheform:
forsolvinganinitialvalueproblemoftheform:
.Noticethatthe
unknownvariable,
,nowappearsinmultipleplaceswithintheequation
(11.11)andsolvingfortheunknownlikelyrequiressolvinga
nonlinear
equation.FormultipleODEs,wehavetosolveasystemofnonlinearequations
everytimestep.edevelopmentofalgorithmsthatuseimplicittimestepping
requiresutilizationofthemethodscoveredinChapter8onnonlinearequat-
ionstypically,thesealgorithmsuseNewtonsmethodtosolvethenonlinear
equations.Fortunately,robustalgorithmshavebeendevelopedbyothers
andshouldbeutilizedwheneverpossible.Anumberofdierentalgorithms
thatshouldmeetmostrequirementsareavailablethroughthe
function(http://docs.scipy.org/doc/scipy/reference/generated/
scipy.integrate.ode.html).
Problems
Oneofthemostfamousinitialvalueproblemsisthepredatorprey
problem.If
isthepopulationofprey,itistypicaltoassumethatthe
populationchangeisgovernedby
where
isthebirthrateperunitof
thedeathrateduetothepreda-
torwithpopulation
.epopulationofpredatoristypicallyassumedto
begovernedby
cxy
Problems
where
isthegrowthratefromtheconsumptionofpreyand
isthe
deathratefromoverpopulationorage.
Solvethepredatorpreymodelequationswith
5,and
2,andwithaninitialpopulationof1.0forboththepredatorandthe
prey.Plotthepopulationoveratleast10timeunits.
Modeltheconcentrationofreactant
inastirredtankreactorwithtwo
inputs.eonlyreactionis
isreactioncanbedescribedbytherst-orderreactionequation
where
istherateatwhich
isconsumedinmolA/(L
theconcentrationof
inmol/Land
35s
istherateconstant.
erearetwoinputstreamsintothereactor:therstinputhasaow
rateof
10L/minand
2molA/Landthesecondinputstream
isturnedonat
0andhasaowrateof
8L/minandaconcentra-
tionof
5molA/L.econcentrationof
inthetankisgoverned
bytheequation:
CA,1Q1+CA,2Q2(Q1+Q2)CA]VkCA,where
50Listhevolumeofthereactor.Before
0,thereactoris
operatingatsteadystate,thatis,
0,and
0sotheabove
equationsimpliesto
whichcanbesolvedtoestablishthatat
73mol/L.isis
theinitialconditionthatshouldbeusedtomodeltheconcentrationof
inthereactorafter
0.WriteaPythonscripttomodelthisprocess
andplot
asafunctionoftime.
YouhavebeenhiredbytheCityHealthDepartmenttoestimatethe
impactonanEbolaoutbreakonacityof50,000people.Youhavebeen
askedtousearelativelysimpleinitialvalueproblemmodelofEbola.Let
bethenumberofhealthy,butsusceptiblepeopleleavinginthecity
(initialvaluefor
is50,000),let
bethenumberofpeopleinfectedby
Ebola(initialvaluefor
is2.0),andlet
bethenumberofpeoplethat
recoveredfromEbola.emodelequationsareasfollows:

S

I
C

S

I

I

d

I
I
,
11InitialValueProblems
where
istherateofcontactbetweensickandhealthypeople
(unknown,butestimatesof
are2.0/50,000to10.0/50,000)and
the
rateofdeathrelativetorecovery(estimatetobe0.5,implying1person
diesforevery2thatrecover).
SolvethismodelofEbolausing
for
amodelperiodof30daysandfor3dierentcontactratesandwritea
reportontheresultstotheCityHealthDepartment.
AdaptedfromAstronomyProjectsforCalculusandDierentialEqua-
tionsbyFarshadBarman,PortlandCommunityCollege,2012
YouhavebeenhiredbyNASAtodeterminetheminimumdistance
betweenMarsandEarthatanytimeinthenext10years.Assuminga
Cartesiancoordinatesystemwiththesunat
andboththeplanets
orbitinginthe
-plane,thelocation,
,andvelocity
eitherplanetisdeterminedusingtheinitialconditionsandsolvingthe
followingsystemofequationsfromNewton:




M

x

y


M

y

where
istheuniversalgravitationalconstant,6
.
year
,and
themassofthesun,2
Becauseofthelargedistanceandtimespansbeingsimulated,itis
recommendedthattheproblembesolvedusingastronomicalunits
(AU)astheunitsofdistanceandyearsastheunitsfortime.ereare
m/AUand3
s/year.
einitialconditionsfortheearthareasfollows:
AU,
88106AU,
71113AU/year,and
80924AU/year.einitialconditionsfortheMarsareasfollows:
81449AU,
41483AU,
23729AU/year,
11473AU/year.
Yourrstprojectrequirementistosolvetheinitialvalueproblemfor
eachplanetanddeterminethelocationandvelocityofeachplanetfor
thenext10yearperiod.NASArequestsaplotoftheorbitsoverthat
period.esecondprojectrequirementistoloopthrougheachtime
Problems
pointfromthe10-yearsolutionsanddeterminetheminimumdistance
betweenthetwoplanetsatanytimeduringthatperiod.NASArecom-
mendsobtainingthesolutionfor10,000timepointsduringthe10-year
period.ReporttheminimumdistanceinAU.
Halocarbonsaremoleculescontainingcarbonandatleastoneatomof
chlorine,bromine,oriodine.emostcommonhalocarbonsarechlo-
rouorocarbons(CFCs),whichwerecommonlyusedgasesupuntilthe
late1980s.CFCsarerelativelystable,long-livedmoleculesthat,regard-
lessoftheirpurpose,inevitablyescapeintothetroposphere(thelowest
layeroftheearthsatmosphere).ecompoundsthencyclebetweenthe
troposphereandthestratosphere.Whileinthestratosphere,aportionof
themolecularcompound(thechlorineandbromineatoms)canbedisso-
ciated,andtheseatomscatalyzetheremovalofozone.eatomsremain
inthestratosphereonaverage3yearsbeforetheyaretransportedback
tothetropospherewheretheyareremovedbyrainorsurfacedeposition.
AmathematicalmodelofthisprocesswaspublishedbyKo
H
f
+
H
where
aretheamountofhalocarboninthetroposphereand
thestratosphere,respectively.ersttermontherightrepresentshalo-
carbonsthatchemicallydegrade,thesecondtermishalocarbonstrans-
portedfromthetroposphereintothestratosphere,andthethirdtermis
halocarbonsthatreturntothetropospherefromthestratosphere.e
timescalesfortheseevents,basedonexperimentalmeasurements,are
1000yearsand
3years.esecondmassbalanceisonthe
quantityofhalocarbonsinthestratosphere:
H
+
H
f
where
5yearsbecausehydrocarbonsaredisassociatedmorerapidly
inthestratosphere.enalbalanceisonthequantityoffreechlorine
inthestratosphere:
+
H
Youhavebeenhiredasaconsultanttoindependentlysolvethese
equationsusinganumericalsolver.Youshouldassumethat15%ofthe
atmosphericmassisinthestratosphereso
18.einitial
conditionsthatyoushoulduseare
1kgand
0.e
keyresultwillbeaplotoftheconcentrationofeachofthethreespecies
11InitialValueProblems
asafunctionoftime.Youshouldsimulateasucienttimeperiodthat
allthreequantitiesarepracticallyzero(maybe100years?).
Bonus:youareoeredabonusifyoucanintegratethe
concentration
overthat100-yearperiodusingthenumericalintegrationapproaches
fromthepreviouschapter.etotalamountofozonelossduetothis
1kgofCFCsdependson
Cdt
References
Gleick,J.(1987)
Chaos:MakingaNewScience
,OpenRoadIntegratedMedia,
NewYork.
Hindmarsh,A.(1983)ODEPACK,asystematizedcollectionofODEsolvers,
ScienticComputing
(ed.R.Stepleman),Elsevier,North-Holland,Amster-
dam.
Ko,M.,Sze,N.D.,andPrather,M.(1994)Betterprotectionoftheozone
layer.
Nature
,505508.
a
(
x
)
12BoundaryValueProblems
forbothendsofthedomain,thatis,
isalsoknown,thenanapproximate
solutioncanbedetermined.eothercommonpossibilityisthat
isknown
atoneorbothendsofthedomain.istypeofboundaryconditionisknown
asaNeumannboundarycondition.
BVPsarisewhendescribingdiusionprocesses,whenmodelingconductive
heattransport,andwhencalculatingthevelocityofviscousuidow.ere
aremanyothersettingsinwhichthesetypesofequationscanpotentially
arise,butproblemsinvolvingconservationofmass,energy,andmomentum
arethemostcommoninengineering.Inthischapter,twodierentnumerical
approacheswillbeexaminedforsolvingBVPstheshootingmethodand
thenitedierencemethod.Inboththecases,wewillutilizenumerical
techniquesthatwerecoveredpreviously,sobepreparedtoreviewmaterial
fromearlychaptersasneeded.
).eequationdescribingthemotionoftheprojectilewas
asecond-orderODE,buttheproblemfellintothecategoryofinitialvalue
problemsbecauseitincludedtwoconditionsatthesameboundary(i.e.,
the
0boundary)insteadofoneconditionateachboundary.BVPshave
aconditionateachboundary,analogoustosolvingtheprojectilemotion
problemgivenastartinglocationandanendinglocationbutnoinitialvelocity.
Withouttwoinitialconditions,wecannotsimplyreusethemethodscovered
inthepreviouschapter,but,withalittlecreativity,wecanrecyclemuchof
whatwasdevelopedpreviouslyandadaptittoBVPs.
Imagingthatwearesolvingtheprojectilemotionproblem,givenaninitial
locationandatargetlocation,andweneedtodetermineaninitialvelocity
aswellastheparticlelocationandvelocitybetweenthelaunchingandtarget
locations.Wecouldguessaninitialvelocity,takeashot,anddeterminethe
naldistancefromthegiventarget.Wecouldthentakeasecondshotwith
adierentinitialvelocity,andonceagainmeasurethedistancetothetarget.
Usingthesetwoshotsasreferencepoints,wecouldtheninterpolate(or
extrapolate)todetermineabetterestimatefortherequiredinitialvelocityto
hitthetarget.Repeatingthisprocessforthreeorfourshotswouldhopefully
leadtoushittingthetarget.isprocessiseectivelytheshootingmethod
forBVPs.Weguessoneinitialvalue,usealltheinitialvaluemethodsfrom
thelastchapter,checktoseeifwematchedtheotherboundarycondition,and
repeattheprocessuntilourguessattheunknowninitialvalueresultsinus
matchingthesecondboundarycondition.
Itiseasiesttoexaminethealgorithmfortheshootingmethodthroughan
exampleproblem.WewanttosolvetheBVP:
onthedomain,0
1,andwiththeboundaryconditions,
0and
2.erststepistorewriteequation12.2asasystemofrst-order
equationsbyintroducinganewvariable:
y
,(12.4)
whereaninitialconditionisavailablefortherstequation(
0)butnot
thesecondequation,thatis,
isunknown.eshootingmethodrequiresa
guessforthesecondboundarycondition,andthenusingtheguess,wecansolve
thesystemofequations(12.3and12.4)usinganyofthealgorithmpresented
intheinitialvalueproblemchapter(the
functionisrecommended)andchecktoseeiftheotherboundarycondition
2)issatised.Figure12.1showsthesolutionusingaguessof
Withthisguess,thetargetof2.0ismissedand
0ishitinstead.
Forthesecondshot,aninitialguessof
0isused,andtheapprox-
imatesolutionusingthisguessisshowninFigure12.2.Inthiscase,thetarget
ismissedbyagreateramountas
Nowthattwoshotshavebeentakenandthetwomisseshavebeenmeasured,
weneedamethodfordeterminingabetterguessforthesecondboundary
0.60.81.0
1.0
Figure12.1
ApproximatesolutiontotheBPV,
usingaguessof
y
0
.
12BoundaryValueProblems
0.00.20.40.60.81.0
Figure12.2
ApproximatesolutiontotheBPV,
usingaguessof
conditionon
.esimplestapproachistotthetwopreviousresults
withalineandextrapolatetodetermineabetterguess.If
istherstguess
attheboundarycondition,
isthesecondguess,and
.(12.5)
Aniterativeprocessisusedwheretheimprovedestimatefortheboundary
condition,
,replacestheolderguess,
,andtheprocessisrepeated.
APythonscriptthatusestheshootingmethodtosolvetheexampleproblem
isgivenbelow.
#Splity''=4*(y-t)into
#y0'=y1and
#y1'=4*(y-t)
dfdt(y,t):
dy0dt=y[1]
dy1dt=4.0*(y[0]-t)
coeff=0.13786
12BoundaryValueProblems
1.5
1.0
0.00.20.40.60.81.0
Figure12.3
ApproximatesolutiontotheBPV,
usingaguessof
cancalculatethedistancetothetarget,
.Figure12.3showsboththenal
resultfromtheshootingmethodandtheexactsolution.elinesaresoclose
thattheyareindistinguishablefromeachother.ForlinearBVPslikethis,only
threeiterationsshouldberequiredbecausethelinearextrapolationusedto
determineanimprovedguess,
,shouldyieldtheexactvaluetouse.Nonlinear
BVPsmayrequirefourorveiterationstodetermineanacceptablevaluefor
eshootingmethodisacommonchoiceforBVPswhenanecientand
familiarODEinitialvalueproblemsolverisavailableandDirichletboundary
conditionsaregiven.Ingeneral,however,itisnotthemostcommonchoice
forBVPsintheauthorsexperience.Amoreintuitiveandexibleapproach,
capableofsolvingproblemswithoneortwoNeumannboundaryconditions,is
presentedinthefollowingsection(andthenextchapter):thenitedierence
method.
a
(
x
)
,(12.6)
onthedomain
withboundaryconditiongivenat
.enite
dierencemethodbeginsbydividingthedomain,
,intoasequence
+1

1
(
x
y
(
x
+1
y
(
x

1
a
b
x
(
a
)
y
(
b
)
Figure12.4
y
+
1
2
y
y

1
,(12.7)
where
y
+
1
y

1
.(12.8)
Usingthesetwoapproximationsforthederivatives,theoriginalBVPequation
canbereplacedwithanalgebraicapproximationateverynodeinthedomain
(i.e.,wearereplacingadierentialequationwith
1algebraicequations).
ealgebraicapproximationis
+
a
(
x

y
+
1
y

1
Noticethatwehavetransformedtheproblemfromadierentialequationinto
alargesystemoflinearalgebraicequations.eunknownsarethevaluesof
ateverynode.
enitedierencealgorithmforlinearBVPshasthreesections:
1)esetupphaseinvolvesspecifyingthenumberofintervalsbetweennodes,
,whichtranslatesinto
1nodesnumberedfrom0to
,thesizeofthe
12BoundaryValueProblems
domainbyspecifying
,andcalculatingthenodespacing,
.esetup
phasealsotypicallyincludesbuildingavectorcontainingthelocationsofthe
nodes,andallocatingspaceforlaterstoringthematrixandright-handside
associatedwiththelinearsystemofequations.
2)emiddlesectionofthecodeisaloopthrougheachnodeandadding
theappropriatecoecientsintothematrixandright-handside.edetails
ofthissteparesummarizedbelow,butitisimportanttonotethatnodes
locatedattheboundaryhaveboundaryconditionsthatneedtobehandled
separately.
3)elastsectionofthealgorithminvolvessolvingthelinearmatrixproblem
(using,e.g.,
)andplottingthesolution.
enitedierenceequationateverynodehastheform:
+
a
(
x

y
+
1
y

1
whichistypicallyrewrittenas
.(12.10)
isequationexistsateverynode,andtheresultingsystemofequationscan
bewrittenasamatrixproblemwiththefollowingform:
.
0

x
2
+
h
(
x
1
.
0
+
x
0

x
2
+
h
(
x


0

x
ItisimportanttonotethattheabovelinearmatrixsystemdoesNOTinclude
boundaryconditions(oranequationfornode0)andisonlyintendedtogive
thebasicstructureofthelinearsystem.Iftheboundarycondition
isgiven,theninsteadofhavinganitedierenceequationfortherstnode,
whichcorrespondstotherstrowinthematrixequation,theboundary
conditionequationwouldbeusedinsteadandthematrixproblemwould
become
000
2
+
h
(
x
1
.
0
+
x
0

x
2
+
h
(
x


0

x
enitedierencemethodisusedbelowtosolvetheBVP:
onthedomain0
2withtheDirichletboundaryconditions
0.enitedierenceequationateachnodeforthisequation
0.(12.13)
ePythonscriptisbelow.
#numberofintervals
x=numpy.linspace(0,numpy.pi/2.0,N+1)
h=x[1]-x[0]
#Allocatespace
b=numpy.zeros(N+1)
#Boundaryconditionatx=0
A[0,0]=1.0
b[0]=1.0
inrange
A[i,i-1]=1.0
A[i,i]=-2.0+h**2
A[i,i+1]=1.0
b[i]=0
12BoundaryValueProblems
#Boundaryconditionatx=pi/2.0
A[N,N]=1.0
b[N]=1.0
y=solve(A,b)
pylab.plot(x,numpy.cos(x)+numpy.sin(x))
eequationsassociatedwiththeboundaryconditions(i.e.,theequations
associatedwithnodesattheboundary)receivespecialhandlinginthisalgo-
rithm,butalltheothernitedierenceequations(12.13)arehandledwithan
iterativeloop.Forthisparticularproblem,anexactsolutionisavailableandthis
solutionisplottedonthesamegureastheapproximatesolution.Evenwith
only10nodes,thenitedierenceapproximationisalmostindistinguishable
fromtheexactsolutionasshowninFigure12.5.
12.3.1ReactionsinSphericalCatalysts
Manychemicalandbiochemicalreactions(
)arefacilitatedbycatalysts,
andthesecatalystsareoftensolidparticlesthatthereactantsdiuseintountil
thereactionoccursatacatalyticsite.Microbesthatareusedinbiological
1.45
1.40
1.35
1.30
1.25
1.20
1.15
1.10
1.05
1.00
0.00.20.40.60.8
1.01.21.41.6
Figure12.5
ApproximatesolutiontotheBPV,
0usingthenitedierence
processestofacilitatevariousreactionsareoftenfoundinocs,whichare
biolmparticlesthataresimilartothesolidcatalystparticlesoftraditional
chemicalprocesses.Ineitherscenario,thephysicalprocessesofinterest
include(1)diusionintothecatalystparticleand(2)reactionwithinthe
particle.Amaterialbalanceonasphericalcatalystparticlegivesthefollowing
equationthatincludesbothphysicalprocesses:
r
2

c
0,(12.14)
where
istheradialdistancefromthecenterofthesphere,
istheconcentra-
tionofthereactant,and
istheielemodulus,adimensionlessparameter
thatdescribestherelationshipbetweenthereactionrateandthediusion
rate.etwoleftmosttermsinthematerialbalancecapturediusion,andthe
termwiththeielemoduluscapturesthereaction.Iftheielemodulusis
large,thenthereactionismuchfasterthandiusionandmostofthereaction
happensnearthesurfaceandtheconcentrationofreactantiszeroovermost
oftheparticlebecausethereactantisconsumedbeforeitcandiuseinto
theparticle.Iftheielemodulusissmall,thenthereactionisslowandthe
particlehasahighconcentrationofreactanteverywhere.
erstderivativeterminequation12.14mustalsobeapproximatedusing
anitedierenceapproximation.Usingthestandardcentereddierence
approximation,therst-orderderivativetermwillbereplacedwith

c
r

1
c
+
1
c

1
Notethatthe2inthenumeratorcancelswiththe2thatappearsinthe
denominatorofthecentereddierenceapproximation(equation10.8).Asa
resultofthisadditionalrstderivativeterm,thetermsothemaindiagonal
ofournitedierencematrixaregoingtohavetheform:
aftermultiplyingeachequationby
.Itmaybehelpfultocomparethe
o-diagonalterminequation12.15withthatinequation12.12,andnotethat
the
termfromearlierissetto
here.Further,thetermsalongthemain
diagonalaregoingtohavetheform
duetothereactionterm.
Typicalboundaryconditionsforthereactioninasphericalcatalyst
equation12.14are
0at
whichbasicallysaysthatiftheconcentrationisthesameonallsurfacesofthe
catalyst,thentheconcentrationhastobesymmetricinsidethecatalyst.is
12BoundaryValueProblems
boundaryconditioncanberewrittenusinganitedierenceapproximation
fortherstderivativeas
where
istheconcentrationatthecenter(
0)and
istheconcentration
atthepointnearestthecenter(
).isequationcanbesimpliedto
,andthisequationwillbeusedastherstequationinthelinearsystemof
equations.esecondboundaryconditionisthattheconcentrationmustbe
knownonthesurfaceofthecatalystparticle.Forexample,ifthebulkconcen-
trationofthereactantis2.0(dimensionless),thentheboundaryconditionis
0at
assumingthattheradiusoftheparticleis1.0(nondimensionalized).
egoverningequation12.14andboundaryconditionsaresolvedusingthe
nitedierencemethodinthescriptbelow.
matplotlib.pyplotasplt
N=99
#numberofintervals
phi=5
#finitedifferencepointspacing
r=numpy.linspace(0,1.0,N+1)
h=r[1]-r[0]
#Allocatespace
A=numpy.zeros((N+1,N+1))
b=numpy.zeros(N+1)
plt.title('$\phi=5$')
eapproximatesolutiontoequation12.14isshowninFigure12.6for
twodierentvaluesof
.Notethattheleftedgeoftheguresshowsthe
predictedconcentrationatthecenteroftheparticleandtherightedgeof
theguresshowstheconcentrationatthesurface,whichshouldalwaysbe
2.0.Figure12.6(a)shows
0,whichrepresentsareactionratethatis
approximatelyequaltotherateofdiusion,andFigure12.6(b)shows
whichrepresentsareactionratethatis5timestherateofdiusioncausing
muchlowerconcentrationsnearthecenterofthesphericalcatalystparticle.
ePythonscriptaboveusesthefactthatthematplotliblibrarysupports
advancedmathematicalequationeditingwhenplacingatitleonaplot.e
$symbolsareusedtoindicateamathematicalequation,andeverything
appearingbetweenthe$signsisprocessedusingLaTeX.Oneparticularly
Figure12.6
Concentration
insideasphericalcatalyst
wherearst-orderreactionis
occurring.Thecenterofthe
particleis
0,andthe
surfaceisat
1.The
concentrationatthesurfaceis
0.(a)
1and(b)
5,whichrepresentsa
muchfasterreactionrate.
1.95
1.90
1.85
1.80
1.75
1.70
0.00.20.40.60.81.0
Radius
0.00.20.40.60.81.0
Radius
Concentration
Concentration
1.5
1.0
12BoundaryValueProblems
helpfulfeaturethatisusedaboveisthatGreekletterscanbeobtainedby
placinga
infrontofthenameoftheletter.InthePythonscriptabove,
isconvertedintotheGreekletter,
.Formoreinformationregardingusing
LaTeXtoformatmathematicalequations,see[1]oranyotherLaTeXreference.
Problems
UsetheshootingmethodtosolvetheBVP:
onthedomain0
with
1and
UsethenitedierencemethodtosolvetheBVP:
cos
onthedomain0
2andwiththeboundaryconditions
0.Notethatthisproblemhasthesolution
cos
eonecomplicationwiththisproblemistheNeumannboundary
conditionat
2.ederivativeinthisboundaryconditioncanbe
approximatedas
isequationbecomesthelastrowinthelinearmatrixproblem,which
hastheform:
1
.
0

x

1
2
+
h
(
x

1
1
.
0
+
x

1
YouhavebeenhiredbyFisserPharmaceuticalstoanalyzethepotential
ofanewdrugfortreatingvitreoushemorrhageintheeye.enew
drugwouldbedeliveredtopicallytothecorneaviaeyedrops.edrug
woulddiusetowardthebackoftheeye(i.e.,totheretina),andany
drugreachingtheretinainthebackoftheeyewouldimmediatelybe
takenawaybythebloodowintheretina.echallengeisthatthedrug
Problems
naturallydecaysrelativelyquicklyasitdiusesawayfromthecornea
towardtheretina.
ediusionanddecayofthedrugaredescribedbytheequation:
wherethersttermcapturesdiusionandthesecondtermisdecay.e
boundaryconditionsare
0(dimensionless)at
0(thecornea)
0(dimensionless)at
0cm(theretina).Forthecurrent
versionofthedrug,thedecayconstantis
20cm
erstquestionFisserwouldlikeyoutoansweris:Whatisthe
concentrationatthevitreoushemorrhagesite,whichisatthecenter
ofthevitreousat
0cm?Istheconcentrationgreaterthan0.2
(dimensionless),theminimumeectiveconcentration?
esecondquestioniswhatdecayrate,
,wouldapproximatelygivea
concentrationof0.2(dimensionless)at
0cm?
Youhavebeenhiredasaconsultantbyacompanythatmanufactures
tubingforblooddonations.etubingtransportsbloodfromthe
needleattheinjectionsitetothecollectionbag.einnerwallofthe
tubeis1cmfromthecenterofthetube(i.e.,theinnerdiameterofthe
tubeis2cm)andisatbloodorbodytemperature(38
C).eouter
wallofthetubeisatroomtemperature(23
C)andis1.3or1.8cmfrom
thecenterofthetube(Figure12.7).etemperatureofthetubingwall
materialvariesbetweentheinnerwalltemperatureandtheouterwall
temperatureandisgovernedbytheequation:
1
eapproximatesolutiontothisequationcanbefoundbydividingthe
domain,1
3or1
8intodiscretepointsandsolving
thenitedierenceapproximationequationateachpoint:
+
1
T
+
1
T

1
where
isthedistancebetweenthediscretepointsintheradialdirec-
thetemperatureofpoint
,and
theradiallocationofpoint
ecompanyisdebatingbetweenthetubingwithanouterwalldiameter
of1.3and1.8cm.Onefactorinchoosingthebestdiametertubeisthat
thereissomeconcernthatlargetemperaturegradients(approximatedas
)willcausethetubingtofractureandfail.Yourconsulting
contractrequiresyoutosubmitabriefreportwithyourrecommenda-
tionregardingtubingthicknessselection.ereportshouldsummarize
yourndingsandincludeguresshowingtemperatureversusradiusfor
12BoundaryValueProblems
23
Figure12.7
Athickcylindricaltubefor
transportingbloodfromanindividualintoa
collectionbag.Theinnerwall(1.0cmfromthe
center)ofthetubeisat38
Candtheouterwall
(1.31.8cmfromthecenter)isat23
thetwodierenttubethicknessesandreportsthetemperaturegradient
atsomelocationforeachtubethickness(Figure12.7).
Reference
Lamport,L.(1994)
LaTeX:ADocumentPreparationSystem
,Addison-Wesley,
UpperSaddleRiver,NJ,2ndedn.
13PartialDierentialEquations
Laplacesequationintwodimensions:
x

.(13.1)
Solvingthisequationrequiresthedeterminationofthefunction
suchthat
itssecondderivativewithrespectto
plusitssecondderivativewithrespect
isequalto
,and
mustsatisfytherequiredboundaryconditions.
isequationdescribesdiusionthroughsolidsandbiologicaltissues,andit
describesconductiveheattransportthroughastationarymedium.Attheend
ofthissection,asecondexampleispresentedthatincludesaconvectiveux
term.
+1

1
+1

1
y
Figure13.1
erstsectionofPythoncodeforthetwo-dimensionalLaplaceproblem
usingnitedierencesisshownbelow.
M=15
#intervalsinx-direction
N=15
#intervalsiny-direction
west=0.0
#edgelocations
east=1.0
south=0.0
north=1.0
#nodelocationsandspacing
x=numpy.linspace(west,east,M+1)
y=numpy.linspace(south,north,N+1)
h=x[1]-x[0]
k=y[1]-y[0]
h2k2=h**2/k**2
coeff=2*(h2k2+1)
#plotthemesh/grid
X,Y=numpy.meshgrid(x,y)
#Allocatespace
totalNodes=(M+1)*(N+1)
b=numpy.zeros(totalNodes)
functionisimportedbecauseitisusedlater
forsolvingthelinearmatrixproblemthatistheresultofthenitedierence
approximation.evariables
aresettothenumberofintervals
betweennodesinthe
directions,respectively.Largervaluesfor
willgiveamoreaccurateapproximatesolution,butvalueslargerthanabout
100willhaveasignicantcomputationalcost.etotalnumberofnodesinthe
domainis
100and
100willresultinover10,000
nodesandalinearmatrixproblemwithover10,000unknowns.isisabout
thelargestacceptablematrixsizeforasolverbasedonGaussianelimination.
evariables
east,west,south,north
areusedtospecifythe
locationoftheboundariesoftherectangulardomainoftheproblem.Some
authorsofnumericalalgorithmsndthosevariablenamestobeintuitivewhile
13PartialDierentialEquations
othersdislike(hate)thosevariablenamesbecausetheycannotremember
whichdirectioniseastandwhichiswest,forexample.Dierentvariable
namesare,ofcourse,acceptable.Forexample,somealgorithmwritersusethe
variables
left,right,bottom,top
insteadoreven
a,b,c,d
enextstageinthesetupphaseofthisalgorithmistobuildvectorsholding
thelocationsofthenodesinthe
directions.Onthebasisofthese
nodelocations,thespacingbetweenthenodes,
,canbecalculated.
evariables
areneededlaterinthealgorithm.e
functionconvertsthevectors
into2Darrays
thatholdthe
locationsofeverynode.esearraysareonlyneededfor
plottingthesolutionattheendofthealgorithmalthoughtheycanbeused
heretogenerateaplotwiththelocationsofthenodesincluded(thislineis
currentlycommentedout).enalstepinthesetupphaseistocalculatethe
totalnumberofnodesand,hence,thetotalnumberofunknownsinthelinear
matrixproblem.Oncethisisknown,spaceforthematrixandright-hand-side
vectorcanbeallocated.
13.1.2MatrixAssembly
emodelproblemthatisusedhereis
x

0(13.2)
onthedomain0
0and0
0and
0onallboundariesexcept
forthewest(left)boundary,whichhas
.Approximatingthederiva-
tiveswithcenterednitedierenceequations(10.12)leadstothealgebraic
equation
c
,
j
+
1
2
c
,
j
c
,
j

1
0(13.3)
andmultiplyingthisequationby
leadsto

1

c
,
j
c
+
1
,
j
c

1
,
j

h
isalgebraic,nitedierenceapproximationoftheoriginalPDEexistsat
eachnodeandmakesuponerow(oroneequation)inthelinearmatrix
problem.Equation13.4includescoecientsforveunknowns,reectingthat
eachequationconnectsacenternode,
,toitsfournearestneighbors(north,
east,south,andwest).
Assemblyofthelinearmatrixproblembeginswithanouterloopthrough
eachrowofnodesinthe
-directionfollowedbyaninnerloopthrougheach
columnofnodesinthe
-direction.ecombinationofthesetwoloopsis
thateachnodeinthedomainisvisitedoncestartinginthelowerleftcorner
(southwestcorner),proceedingtotherightacrosstherstrow,thenupto
thenextrow,andthennextuntilweendintheupperrightcorner(northeast
corner).Ateachnode,theappropriatecoecientsfromequation13.4are
addedtothematrix,unlessthenodeislocatedonaboundaryandthen
thecorrectboundaryconditionmustbeaddedtothelinearsystemof
equations.
Beforeexaminingthealgorithmforassemblingthematrix,itisimportantto
recognizethatweneedamechanismformappingnode
toauniquerowin
thematrix(i.e.,toauniqueunknownornodenumber).Inthepreviouschapter,
whereweexaminedone-dimensionalproblems,thiswastrivialbecausenode
correspondedtothe
thunknownand
throwofthematrix.Now,however,we
havenode
duetothetwo-dimensionalnatureoftheproblem.(Atthispoint,
somestudentssuggestusingathree-dimensionalmatrix,butthisisnotthe
correctsolution.)Inordertouniquelymapnode
toaparticularunknown
number,wewillusetheequation
node
,(13.5)
where
.erefore,fornode
,weget
node
0;fornode
,weget
node
1;andfornode
,weget
node
1.issameequationcanbe
usedtocalculatetheuniquenodenumberoftheneighboringnodestothe
north,east,south,andwest.
esectionofPythoncodebelowassemblesthematrixforthetestproblem.
node=j*(M+1)+i
Enode=j*(M+1)+i+1
#nodetotheeast
Wnode=j*(M+1)+i-1
#nodetothewest
Snode=(j-1)*(M+1)+i
#nodetothesouth
Nnode=(j+1)*(M+1)+i
#nodetothenorth
(i==0):
#checkforwestboundary
A[node,node]=1.0
b[node]=numpy.sin(numpy.pi*y[j])
(i==M):
#checkforeastboundary
A[node,node]=1.0
b[node]=0.0
(j==0):
#checkforsouthboundary
A[node,node]=1.0
b[node]=0.0
(j==N):
#checkfornorthboundary
A[node,node]=1.0
b[node]=0.0
13PartialDierentialEquations
A[node,node]=-coeff
A[node,Enode]=1.0
A[node,Wnode]=1.0
A[node,Snode]=h2k2
A[node,Nnode]=h2k2
b[node]=h**2*0.0
issectionofcodeconsistsoftwonestedloopsthatcauseustoultimatelyloop
througheverynodeinthedomain.enumbersofthenodeanditsneighbors
arecalculatedrst.en,thealgorithmcheckstoseeifanodeisonthebound-
ary.Itisimportanttodothisrstbecauseifanodeisontheboundary,thenone
ortwoofitsneighborsdoesnotexistandtryingtowriteentriesintothematrix
fornodesthatdonotexistwillonlyleadtocrashesanderrormessages.Ifthe
nodeisontheboundary,theappropriateboundaryconditioncanbeapplied
(inthiscase,allboundaryconditionsareDirichletconditionsandallarezero
exceptfortheeastboundary).Finally,ifthenodeisnotontheboundary,the
appropriatevalues,basedonthenitedierenceequation13.4,areaddedinto
thematrixandtheright-handside.
13.1.3SolvingandPlotting
enalcodesegmentsolvesthelinearmatrixproblem(
isrecommended),andtheresultisreturnedasaone-dimensional
vector.econtourplottingfunctioninMatplotlibtypicallyrequiresthatthe
solutiondatabeinanarraywiththesameshapeasthearrays(
)that
holdthelocationsofthenodesforthetwo-dimensionalnitedierencemesh.
Eachrowinthearraycorrespondstoarowofnodesinthedomain.esolu-
tionvectorcanbereshapedusingthe
functionbefore
theplottingfunctioniscalled.Dependingontheplottingroutinethatisused,
additionallabelsandcolorbarsmaybehelpful.ePythoncodesegmentbelow
nalizestheprocessofsolvingtheLaplaceproblemintwodimensionsusing
nitedierences.eresultingcontourplotisshowninFigure13.2
z=solve(A,b)
Z=z.reshape(M+1,N+1)
#Plotting
CT=pylab.contour(X,Y,Z)
13.2Convection
0.00.20.40.60.8
0.150
1.0
1.0
Figure13.2
Contoursofthesolutionforthemodelproblem.
13.2Convection
eLaplaceproblemexaminedintherstpartofthischapterisusedto
modeldiusionorheatconductioninstagnantdomains.Ifthereisalsouid
movementorconvectioninadditiontothediusionorconduction,then
anadditionaltermisrequiredinthemodelequation.Tomodelsteady-state
diusionandconvectionintwodimensionswiththeuidvelocitybeinggiven
bythevector
,thefollowingequationisused:
x

y
u


c
x




c
0.(13.6)
Usingthesameboundaryconditionsanddomainasbefore,andapproximating
thesecond-orderderivativeswithcenterednitedierenceequations(10.12)
andtherst-orderderivativeswithbackwardnitedierenceequations(10.7)
leadstothealgebraicequation
c
,
j
+
1
2
c
,
j
c
,
j

1
u

c
,
j
c

1
,
j



c
,
j
c
,
j

1
=
0
.
13PartialDierentialEquations
Itisveryimportanttorecognizethatlessaccurate,backwarddier-
enceequationswereusedhereinsteadofthemoreaccuratecentered
dierenceequations(10.8)forrst-orderderivatives.Whenapplyinganite
dierenceapproximationtotheconvectiveterm,itisimportantthatthe
approximationofthederivativebedoneinsuchawayastoincludeonlythe
nodesthatareupwindofthenodeofinterest.emathematicaljustication
forthischoiceisavailableinanumberofexcellentbooks[1],butherewewill
simplystatethatnotusinganupwinddierenceapproximationusuallyleads
tonumericalinstabilityandsignicanterrorintheapproximatesolution.An
analogyofquestionableaccuracyisthatitisdiculttodetectchangesinsmell
(concentration)whenfacingdownwindbutitisrelativelysimplewhenfacing
upwind.enitedierenceequationaboveisbasedontheassumptionthat
bothcomponentsofthewindvelocity,
,arepositive.Ifoneorbothofthese
velocitiesisnegative,thenthenitedierenceequationmustbemodiedto
usetheforwardrst-derivativeapproximations(equation10.5)intheupwind
direction.
Addingconvectiontotheprevioustwo-dimensionalnitedierencecodeis
relativelysimpleandonlyrequires(1)addingawindvelocityvariableand(2)
addingadditionaltermstothematrixfortheconvectivepartoftheequation.
ePythonscriptforthetestproblemwithauidvelocityvectorof
isreproducedcompletelybelow.
M=15
#intervalsinx-direction
N=15
#intervalsiny-direction
west=0.0
#edgelocations
east=1.0
south=0.0
north=1.0
#nodelocations
x=numpy.linspace(west,east,M+1)
y=numpy.linspace(south,north,N+1)
h=x[1]-x[0]
k=y[1]-y[0]
h2k2=h**2/k**2
coeff=2*(h2k2+1)
#plottingthegrid/mesh
X,Y=numpy.meshgrid(x,y)
13.2Convection
#Wind
#bothMUSTbepositiveduetodifferencing
wind=numpy.array([5.0,5.0])
#Allocatespace
totalNodes=(M+1)*(N+1)
b=numpy.zeros(totalNodes)
node=j*(M+1)+i
Enode=j*(M+1)+i+1
#nodetotheeast
Wnode=j*(M+1)+i-1
#nodetothewest
Nnode=(j+1)*(M+1)+i
#nodetothenorth
Snode=(j-1)*(M+1)+i
#nodetothesouth
(i==0):
#checkforwestboundary
A[node,node]=1.0
b[node]=numpy.sin(numpy.pi*y[j])
(i==M):
#checkforeastboundary
A[node,node]=1.0
b[node]=0.0
(j==0):
#checkforsouthboundary
A[node,node]=1.0
b[node]=0.0
(j==N):
#checkfornorthboundary
A[node,node]=1.0
b[node]=0.0
A[node,node]=\
-coeff-h*wind[0]-h**2*wind[1]/k
A[node,Enode]=1.0
A[node,Wnode]=1.0+h*wind[0]
A[node,Nnode]=h2k2
A[node,Snode]=h2k2+h**2*wind[1]/k
b[node]=h**2*0.0
z=solve(A,b)
Z=z.reshape(M+1,N+1)
#Plottingsolution
CT=pylab.contour(X,Y,Z)
13PartialDierentialEquations
0.150
0.20.40.60.8
1.0
1.0
Figure13.3
Contoursofthesolutionforthemodelproblemwithaconvectiveowof
,thatis,towardthenortheastcorner.
econvectivetermcanhaveasignicantimpactonthesolutiontothe
modelproblem,dependingonthemagnitudeofthewind.econtours
associatedwithauidvelocityof
areshowninFigure13.3.Oncethe
magnitudeoftheuidvelocityexceedsabout100,theproblemquicklybecomes
morenumericallydemandingandamuchnermesh(i.e.,morenodes)is
likelytoberequiredtogetanaccurateresult.Aninaccurateresultisalmost
alwaysinexpensivefromacomputationalstandpoint,butaccuracycanbe
costly.
eseequationsdescribedchangesintwoormorespatialdirections.eother
possibilityisthatoneoftheindependentvariablesistime.enalpartofthis
chapterexaminesproblemswheretherearederivativeswithrespecttotime
andspaceinthesameequation.isclassofPDEsisoftencalledparabolic
PDEs.BeforeexamininganitedierencealgorithmtosolveaparabolicPDE
problem,letusbrieydiscusscontrolleddrugrelease.
Researchersareincreasinglydevelopingdevicesthatgraduallyreleaseaphar-
maceuticaldrugovertime.Inmostcases,agradualreleaseispreferredtothe

d
,(13.8)
where
isthediusivityand
theconcentrationwithinthedevice.We
assumethatthedistancefromthesurfaceofthedevice,
0,tothecenter
is1.0(dimensionless).Asaresult,thespatialdomainis0
0.Further,
weassumethattheconcentrationatthesurfaceiszero,thatis,
implyingtheanydrugthatdiusestothesurfaceisimmediatelysweptaway,
andthedeviceisassumedsymmetricaboutthecenter,thatis,
Finally,weassumethattheinitialconcentrationis1.0(dimensionless)every-
whereexceptthesurface,thatis,
WehavepreviouslydiscussedhowtoreplacethederivativesinthePDEwith
nitedierenceapproximations.Forthisproblem,thetimethatwewishto
simulateisgoingtobedividedinto
discretestepsandwearegoingtoapprox-
imatethetimederivativewithaforwarddierenceapproximation(i.e.,usethe
forwardEulermethod,equation11.3).eonespatialdimensionisgoingto
bedividedinto
intervals(or
1nodes)thatspanfrom0.0to1.0,andthe
secondderivativeinspacewillbereplacedwithacentereddierenceapprox-
imation(10.12).eresultofthenitedierenceapproximationisthatthe
originalPDEisreplacedwiththenitedierenceequation:
new
t
=

c
+
1
,
2
c
,
c

1
,
,(13.9)
where
istheindexforthespatiallocationofthenode,
referstotheconcen-
trationatnode
attheprevioustimestep,and
new
referstotheconcentration
13PartialDierentialEquations
atthenexttimestep(i.e.,theunknowns).isequationisusuallyrearranged
togive
new
ci+1,old2ci,old+ci1,old].(13.10)
Noticethatexplicittimesteppingisusedsowecancalculatethenewconcen-
trationusingonlyconcentrationsfromtheprevioustimestep.isequation
alsoshowshowthenewconcentrationateachnodedependsonthreeconcen-
trationsfromtheprevioustimesteptheconcentrationatthesamenodeand
thetwoneighboringnodes.Figure13.4summarizestheconnectionbetween
theconcentrationatanodeanditsneighboringnodesfromtheprevioustime
step.
APythonscriptthatutilizesthenitedierenceapproximationtosolvethe
transientdiusionproblem,thatis,thedrugreleaseproblem,isshownbelow.
ealgorithmissimilartotheothersshowninthischapterinthattherst
sectionofthealgorithmperformssomebasicsetupoperations.enumberof
timeandspaceintervalsisspecied,thesizeofthedomainanddurationofthe
simulationareset,andsomevectorscontainingthespatialnodelocationsand
timepointsareconstructed.Oneadditionalandimportantstepinthesetup
phaseisthatavectormustbeconstructedthatcontainstheinitialconditions.
M=15
#intervalsinx-direction
N=200
#totalnumberoftimesteps
+1

1
+1
t
Figure13.4
DiagramillustratingthenitedierenceapproximationforatransientPDEwith
onespatialdimension.Timeisshownonthe
-axisandspaceisonthe
-axis.Whensolving
fortheconcentrationatanodeforthenexttimestep,theconcentrationwilldependon
threeadjacentnodesfromtheprevioustimestep.
left=0.0
#edgelocations
right=1.0
start=0.0
stop=20.0
diffh2=5.0
#nodelocations
x=numpy.linspace(left,right,M+1)
t=numpy.linspace(start,stop,N+1)
h=x[1]-x[0]
dt=t[1]-t[0]
#Initialconditions
c_old=numpy.ones(M+1)
c_old[0]=0.0
c_new=numpy.zeros(M+1)
#Plotinitialconditions
#timeloop
c_new[0]=0.0
#spatialloop
tmp=c_old[i-1]-2*c_old[i]+c_old[i+1]
c_new[i]=c_old[i]+dt*diffh2*(tmp)
c_new[M]=c_new[M-1]
c_old=c_new.copy()
#Plotevery20thtimestep
j%20==0:
emainsectionofthealgorithmconsistsoftwoloops.eouterloopisaloop
throughthetimesteps,andtheinnerloopcyclesthroughthespatialnodes.e
algorithmoperatesbycalculatingnewconcentrationsateveryspatiallocation
beforemovingontothenexttimestep.Caremustbetakentosettheboundary
conditions.eDirichletboundaryconditionat
0issetbeforeproceeding
throughtheinnerloopthroughthespatialnodes.eNeumannboundarycon-
ditionat
1isenforcedbysettingtheconcentrationoftheedgenodetobe
equaltoitsnearestneighbor.ejusticationfordoingthiscomesfromthe
boundaryconditionequation:
13PartialDierentialEquations

=
1
c
c

1
issimulationusesalargenumberoftimesteps(200)andplottingthesolution
ateverytimestepcreatesaverybusygure.Tosimplifythegureshowingthe
results,thesolutionisonlyplottedevery20thtimestepbycheckingtoseeif
theremainderofdividingthetimestepnumberby20iszero.eresultsfrom
runningthealgorithmareshowninFigure13.5.
Observantreadersmaybesurprisedthatthealgorithmaboveused200time
stepsandonly16spatialnodes.ischoicecanbejustiedinpartbyobserv-
ingthattheapproximationofthetimederivativeisrst-order(temporalerror
andthespatialderivativeapproximationissecond-order(
)sowe
shouldexpecttoneedahighertemporalresolution.Analternativejustication
canbeprovidedbysimplyrerunningthealgorithmwithonly40timesteps.e
resultofthisnumericalexperimentisshowninFigure13.6.Inthiscase,the
approximationerrorisseentogrownexponentiallyaclearsymptomofasti
dierentialequation.Recallingthatstidierentialequationsshouldbesolved
usinganimplicittime-steppingmethod,wesimplystateherethatswitching
toanimplicittime-steppingmethodlikebackwardEulerwillavoidtheexpo-
nentialerrorgrowthseenhere.edisadvantageofswitchingtoimplicittime
steppingisthatalinear(ornonlinear)matrixproblemmustbesolvedevery
timestep.Forthisreason,manyalgorithmsuseasucientlysmallstepsize
Increasing time
0.81.0
1.0
Figure13.5
Plotoftheconcentrationforthemodelproblemshownevery20thtimestep.
Theinitialconcentrationisthehighest,andastimeincreases,theconcentrationdecreases.
Problems
1.0
0.00.2
0.40.60.81.0
Figure13.6
Plotoftheconcentrationforthemodelproblemusingonly40timesteps.The
13PartialDierentialEquations
YouhavebeenhiredbytheDepartmentofHomelandSecurityto
studythepotentialforCanadatoattackMontanawithanoxiousodor.
econcernisthatCanadawillreleaseanoxiousodorfromeither
LethbridgeorMedicineHat(bothtownsarejustnorthofthenorthern
borderofMontana)andthenoxiousodorwilldiuseinalldirections,
includingintoMontana.eodorwilldrivepeopleoutofMontana
andthentheCanadianMounties(see,Do-Right,Dudly)willbeableto
moveinandoccupyMontana.
ediusionofthenoxiousodorisgovernedbythetwo-dimensional
Laplaceequation:
x

wherethedomainofinterestisthestateofMontana,whichshouldbe
approximatedasarectanglethatis500mileswest-to-eastand250miles
south-to-north.IfthenoxiousodorisreleasedfromMedicineHat,then
theconcentrationoftheodoralongthenorthbordershouldbeapprox-
imatedas
becauseMedicineHatisnorthofcentralMontana.econcentration
alongallotherborders(west,south,andeast)shouldbeapproximated
aszero.IfthenoxiousorderisreleasefromLethbridge,whichisnorthof
westernMontana,thentheconcentrationalongthenorthbordershould
beapproximated:
where
isalwaysthedistancefromthewesternborderofMontana.
Inboththecases,thepeakconcentrationalongtheborderis1.0(dimen-
sionless).YoushouldplottheconcentrationeverywhereinMontana
and,inparticular,determinetheconcentrationinBozeman,Montana,
whichcanbeapproximatedas100milesfromthewesternborderand
50milesfromthesouthernborder.ecitizensofBozemanwillbeable
tostayanddefendthestateiftheconcentrationislessthan0.10(dimen-
sionless).
enalconcernfromtheDepartmentofHomelandisthatthenoxious
odorwillbereleaseduringapolarvortex,whichwillgenerateawind
fromNorthtoSouthwithavelocityof
0(dimensionless).Inthiscase,
thetransportofthenoxiousodorwillbegovernedbytheequation:
Problems
x

y
.
0


c
Repeatthepreviousanalysisunderconditionsofthepolarvortex.
Nicotinepatchesareplacedontheskinofanindividualandthey
diusivelydelivernicotineacrosstheskinandothertissues.ey
areprimarilyusedbyindividualsthataretryingtostopconsuming
cigarettesandprovideanalternativenicotinesourceduringthe
transitionperiodawayfromcigarettes.Youhavebeenhiredbyaman-
ufacturerofnicotinepatchestopredicttheconcentrationofnicotine
inthetissuenearthepatchovera24-htimeperiod.
econcentrationofnicotineinthetissuecanbeaccuratelydescribed
bytheequation:
t
=
D

where
istheconcentrationinmg/cm
ci+1,old2ci,old+ci1,old]tkci,oldwithaninitialconcentrationofzeroeverywhereexcepttheskinsurface
adjacenttothepatch.eskinsurfacenexttothepatchshouldmaintain
axedconcentrationof1.0mg/cm
foratleastthe24-hperiodyouhave
beenaskedtomodel.esofttissuebeneaththepatchis2-cmthickand
thenanimpermeablebonepreventsfurtherdiusionofthenicotine.
Asaresult,themodelshouldhaveano-uxboundarycondition(i.e.,
max
max
)attheotherendofthespatialdomain,2cmawayfromthe
skinsurface.
erateatwhichnicotinedecaysinthetissuevariessignicantlyfrom
oneindividualtoanother.Atoneextreme,someindividualslackthe
proteinsthatnormallybreakdownthenicotineso
iseectivelyzero.
Attheotherextreme,someindividualsrapidlybreakdownnicotineso
canbeashighas1.0h
Yourreporttothecompanyshouldcontainpredictions(i.e.,gures)of
nicotineconcentrationatdierentpointsbetweentheskinandthebone
atdierenttimepointsforatotaltimeperiodof24h.epredictions
shouldincludebothextremecases,
.Itisalso
suggestedthatyouvarythenumberofspatialpoints/intervalsandthe
numberoftimestepstoconvinceyourselfandthecompanythathas
13PartialDierentialEquations
hiredyouthattheapproximatesolutionisagoodapproximationtothe
originalequation(hint:agoodapproximationshouldchangelittlewith
changesinthenumberoftemporalorspatialintervals).
Reference
Donea,J.andHuerta,A.(2003)
14.3LaplacesEquation
var: mesh
0.20.40.60.8
x-axis
y-axis
Figure14.1
Triangularmeshfromthe
function.
UnitSquareMesh(32,32)
dividesaunitsquareintotriangles
with32trianglesineachdirection.eresultingmeshisshowninFigure14.1.
Formorecomplexshapes,anumberofsoftwarepackagesareavailablethatpro-
videaCAD-likeinterfacefordeningthedomain(e.g.,ourdomainmightbea
mountainbike)andthendividethedomainintosmallpolyhedralshapesofa
desiredtypeandsize.eCubitsoftwarefromSandiaNationalLaboratoryis
oneexample.
d
f
(
x
,
y
)
14.4PatternFormation
Max: 1,000
Min: 0.000
Contour
Var: f_2
0.8182
0.1818
0.40.60.8
x-axis
Figure14.2
FEMsolutiontothemodelproblem(Laplacesequation)usingasecond-order
Lagrangefunctionspace.ThesolutionwasvisualizedusingVisit.
activatortobeproduced.Inapositivefeedbackloop,regionsthathadslightly
higheractivatorconcentrationsintheinitial,randompattern,theseareas
quicklyachieveevenhigheractivatorconcentrations(Figure14.3,topright).
However,becausetheactivatormovesmoreslowly,theincreasesinactivator
concentrationareverylocal.einhibitor,ontheotherhand,movesover
greaterdistancesandtheregionsbetweenthehighactivatorpeaksquickly
becomesuppressedbytheinhibitor(Figure14.3,lowerright).eresultisa
stable,predictablepatternofpeaksandvalues.enumberofpeaksdepends
ontherelativediusionormovementratesofthetwomoleculesandthespeed
oftheactivationandinhibitionreactions.
Onesystemofequationsthatdescribesthereaction-diusionsystemof
Turingandothersis
t
=


x

y
+
A
H

A
H
t
=


x

1,(14.5)
14.4PatternFormation
Randominitialactivator
Shortrangeactivator
concentrationincreases
Longerrangeinhibitor
reducesconcentration
StableTuring
pattern
Figure14.3
Patternformationbasedonthereaction-diusionsystemofTuringbeginswith
arandomconcentrationofactivator(showninupperleft)andinhibitor(oppositeof
activator).Regionsofgreaterinitialactivatorconcentrationstarttogrowduetoapositive
AdditionalResources
-dt*inner(Constant(1.0),p)*dx
L=L0+L1
a=derivative(L,w,du)
#Createfilesforstoringsolution
ufile=File("results/pattern.pvd")
#CreatenonlinearproblemandNewtonsolver
problem=TuringPattern(a,L)
solver=NewtonSolver()
#Time-stepping
t=dt
tT+DOLFIN_EPS:
"t=",t
w0.vector()[:]=w.vector()
solver.solve(problem,w.vector())
#Savetofile
ufilew.split()[0]
#Movetonexttimestep
t+=dt
Mostofthefunctionsinthisscriptarethesameasthosepreviouslydescribed
whenLaplacesequationwassolved.eTuringPatternobjectatthetop
isderivedfrombuiltinfunctionalityinFEniCSforautomaticallysolving
nonlinearproblemsusingaNewtonsolver.Ateachtimestep,theactivator
concentrationissavedtoale.Figure14.4showsthepatternthatresults
fromthescriptabove.Bychangingtherelativesizesofthevariousterms
inequations14.4and14.5,dierentpatterns(ornopatternatall)canbe
achieved.
AdditionalResources
efollowingbooksmaybeusefulforlearningmoreabouttheFEM.
AnIntroductiontotheFiniteElementMethodbyReddy[3]
FiniteElementMethodsforFlowProblemsbyDoneaandHuerta[4]
eFiniteElementMethodinHeatTransferandFluidDynamicsbyReddy
andGartling[5]
eMathematicaleoryofFiniteElementMethodsbyBrennerand
Scott[6]
0.20.40.60.8
x-axis
y-axis
Figure14.4
ActivatorconcentrationresultingfromaTuringpatternsimulation.
References
Logg,A.,Mardal,K.A.,andWells,G.N.(2012)
AutomatedSolutionofDif-
factoringandexpandingfunctions,SymPy
equilibriumkinetics7778
partialfractiondecomposition78
FEniCS17
implementation248249
FEniCSDockerpage245
Fibonaccisequence54
nitedierenceapproximations
backwarddierence177178
centereddierence178179
forwarddierence177178
nitedierencemethod
Dirichletboundaryconditions
linearBVP216218
matrixproblem218,219
nodes217,218
sphericalcatalysts,reactionsin220224
niteelementmethod(FEM)
Laplacesequation
discretization247248
FEniCSimplementation248249
mesh246247
patternformation249253
uses246
warning245
rstderivativeapproximation177180
rst-orderequilibriumreaction66
rst-orderdepletionmodel166,167
rst-ordermodel166,167
oatingpointnumbers31,43
FORTRAN91
forwarddierenceapproximation177
forwardEulerprocess195197
functionspace247
Gaussianeliminationfunction9193,97,229
Gaussianquadrature187
Gausspoints187
GaussSeideliteration103105
genfromtxt()161
GettingPython1519
GeometryExperimentation71
Gillespiealgorithm6366
HallHeroultprocess168
histogram161
IBMSPSSStatistics156
see
integrateddevelopmentenvironment
idealgaslaw140
ifstatements32
implicittime-steppingmethod
importbobcatSolve91
importnumpy.linalg.90
indentation32
IndentationError46
independentvariabledata120
IndexError46
initialconditions193
initialvalueproblems
biochemicalreactors193195
equationssystems
Lorenzsystemandchaoticsolutions
second-orderinitialvalueproblems203
forwardEuler195197
modiedEulermethod198199
stidierentialequations203206
inputfunction33
integrateddevelopmentenvironment(IDE)
interpolation
regression89
interpretedprogramminglanguages1314
iterativemethods99,105
iterativesolutionmethods
convergenceof106107
GaussSeideliteration103105
Jacobiiteration100103
numpy.norm100
relaxationmethods105
robustnessesof103
vectornorms100
Jacobi102
Jacobianmatrix146,148
Jacobiiteration100103
just-in-time(JIT)compilation14
KeyError46
Krylovmethods107
Lagrangepolynomials247
LAPACK91
Laplacesequation228
discretization247248
FEniCSimplementation248249
mesh246247
least-squares
norm119
regression9,118
Leibniznotation4
LevenbergMarquardtalgorithm120
linearmatrixequation87,91,99
linearmatrixproblem230231,249
linearregression164168
curve117
normalequations116,118119
linearsystems
directmethods91100
iterativesolutionmethods100107
nonlinearequations56
linspace()function59,60,63
Linux17
logspace()function54
Lorenzsystemandchaoticsolutions
low-levellanguage188
LU-decomposition92
Maple71
massowrate88,89
MathCAD12,71
Mathematica12,71
mathematicalequation3
mathlibrary26
MATLAB12,13
Matplotlab162163
Matplotliblibrary6063,223
matplotlib.pyplot163
matrixassembly230232
mesh246247
MichaelisMentenkineticsmodel194,196
modiedEulermethod198199
MontyHallProblem68
multigridmethods107
multiplenonlinearequations146151
multiplereturnvariables94
multivariableregression
datasets128
machinelearning127129
numpy.stack()function126
myquad()function43
NameError46
NationalRenewableEnergyLaboratory
(NREL)168
nestedconditionalstatements32
Neumannboundarycondition212
Newtoniteration148
Newtonsmethod
advantages142
disadvantages143
nonlinearfunction141
Taylorpolynomial140
nodes217
nonlinearequations
bisectionmethod137140
Broydensmethod143146
multiplenonlinearequations146151
Newtonsmethod140143
nonlinearregression113
Antoinesequation121
covariancematrix121
exponentialcurve123
least-squaresnorm119
LevenbergMarquardtalgorithm120
lunardisintegration122125
parametersof120
Pythonscript123
semilogy125
normalequations116
alternativederivationof118119
linearequations116
numericaldierentiation
rstderivativeapproximation177180
scipyderivativeapproximation181183
secondderivativeapproximation180181
numericalintegration
errorfunction187189
scipy186187
trapezoidrule185186
numpy.array()method5152
numpylibrary5160,157158
advantage158
arrayandvectorcreation5154
arrayoperations55
drawback158
help()function5556
inputs157
loading5960
mathematicalfunctions56
polynomials5859
randomvectors57
savingarrays5960
searching5758
sorting5758
numpy.linalg.function()90
numpy.nonzero()function58
numpy.norm100
numpy.polynomial59
numpy.random57
numpy.save()function60
numpy.savetxt()function60
numpy.sum()117
numpy.zeros()function53
ordinarydierentialequations(ODEs)68,
195,196,199,211
ordinary
partialdierentialequations68
pandaslibrary159162,169
parabolicPDEs237
partialdierentialequations(PDEs)3,68
convection233236
nitedierencemethod
matrixassembly230232
setup228230
partialdierentialequations(PDEs)(
solvingandplotting232233
fortransientPDEs236241
partialfractiondecomposition78
patternformation249253
Poiseuilleowequation85,95,96
polynomialcoecients118
powerlawmodel132
predatorpreymodelequations207
printfunction15,4445
problemsolving,engineering
equationidenticationandcategorization
pylab61
Pythagoreantheorem149150
Python
commands16
comparatorsandconditionals3134
debugging/xingerrors45
errormessages4546
externallibraries
docstrings27
ndingdocumentation2528
IDE26
functions3945
iteratorsandloops3439
module95
printfunction4445
programming12
prompt16
syntax46
type()20
variablesandoperators
containers2325
SpyderIDE21
strong-typing20
updatingvariables2123
Pythonscript136137
Broydensmethod144
convection234235
CVSlibrary159
FEniCSimplementation248249
nitedierenceapproximation238239
forwardEulerprocess195197
linearregression164168
Lorenzsystem201
matrixproblem231232
modiedEulermethod198199
numpylibrary158
shootingmethod214216
SRKequation138
quadraticpolynomial42
quadraticregression118
randomarrays57
randomoatingpointnumbers57
randomvectors,numpy57
range()function35
rangefunction38
recyclingcode95
regression
ClausiusClayperonequation114
function9
linearregression115119
multivariableregression126129
nonlinearregression119125
quadraticregression118
regressionline118
relaxationmethods105
SAGE71,72
SageMathCloud18
SAS156
scipyderivativeapproximation181183
scipylibrary51,136,181
scipy.optimize.curve_t()124
Secantmethod
see
Broydensmethod
secondderivativeapproximation180181
second-orderdepletionmodel168
semilogy125
shootingmethod212216
BVP213
guess213214
initiallocation212
initialvelocity212
projectilemotion212
Pythonscript214216
singlealgebraicequation75
SoaveRedlichKwong(SRK)nonideal
equation135136
SOR
see
successiveover-relaxation(SOR)
sort()function58
spatialvariation227
SpyderIDE16,17
SRKequationofstate138
SRKmodel144
standarddeviation163
standardscore164
statistics
advancedlinearregression164168
analysis162164
readingdata
array,parsingan162
CVSlibrary159
numpylibrary157158
pandas159162
U.S.electricutilitycompaniesandrates
steady-statePDEs,nitedierencemethodfor
Stidierentialequations203206
stochasticreactions64
successiveover-relaxation(SOR)105
sumofsquare9
symbolicderivatives7879
symbolicintegration80
symbolicmathematicspackages7172
SymPylibrary
derivativesandintegrals7881
factoringandexpandingfunctions7678
multipleequations7576
solve()function74
sympy.method()format72
sympy.solve()function73
SymPySymbolclass72
SyntaxError46
SystemforAlgebra71
tangentline144
Taylorpolynomial140,143,176177
ielemodulus221
transientPDEs236241
trapezoidrule185186
-test163
TypeError46
UbuntuLinux245
under-relaxation105
upwinddierenceapproximation234
U.S.electricutilitycompaniesandrates
user-denedfunction120
ValueError46
vanderWaalsequation74
vaporpressure113
vectornorms100
Vigenrecipher82
virtualmachine14
water-gasshiftreaction77
weakform248
WolframAlpha71
xdata120
ydata120
ZeroDivisionError46
zscore164
Go to www.wiley.com/go/eula to access Wiley’s ebook EULA.

Приложенные файлы

  • pdf 11180076
    Размер файла: 4 MB Загрузок: 0

Добавить комментарий