Python Programming: An Introduction to Computer Science

(Nora) #1
10 CHAPTER1. COMPUTERSANDPROGRAMS

print x
x = 3.9 x (1 - x)
print x


Obviouslyusingtheloopinsteadsavestheprogrammera lotoftrouble.
Butwhatexactlydothesestatementsdo?Thefirstoneperformsa calculation.


x = 3.9 x (1 - x)


Thisiscalledanassignmentstatement.Thepartontherightsideofthe=isa mathematicalexpression.
Pythonusesthe*charactertoindicatemultiplication.Recallthatthevalueofxis 0 25 (fromtheinput
statement).Thecomputedvalueis 3 9



0 25 

1  0 25  or 0 73125.Oncethevalueontherighthandsideis
computed,it is storedback(orassigned) intothevariablethatappearsonthelefthandsideofthe=, inthis
casex. Thenew valueofx(073125)replacestheoldvalue(025).
Thesecondlineintheloopbodyis a typeofstatementwehave encounteredbefore,aprintstatement.


print x


WhenPythonexecutesthisstatementthecurrentvalueofxis displayedonthescreen.So,thefirstnumber
ofoutputis 0.73125.
Remembertheloopexecutes 10 times.Afterprintingthevalueofx, thetwo statementsoftheloopare
executedagain.


x = 3.9 x (1 - x)
print x


Ofcourse,nowxhasthevalue 0 73125,sotheformulacomputesa newvalueofxas 3 9



0 73125 

1 
0 73125 , whichis 076644140625.
Canyouseehow thecurrentvalueofxis usedtocomputea new valueeachtimearoundtheloop?That’s
wherethenumbersintheexampleruncamefrom.Youmighttryworkingthroughthestepsoftheprogram
yourselffora differentinputvalue(say 0 5).ThenruntheprogramusingPythonandseehow wellyoudid
impersonatinga computer.


1.8 ChaosandComputers.


I saidabove thatthechaosprogramillustratesaninterestingphenomenon.Whatcouldbeinterestingabout
a screenfullofnumbers?If youtryouttheprogramforyourself,you’ll findthat,nomatterwhatnumber
youstartwith,theresultsarealwayssimilar:theprogramspitsback 10 seeminglyrandomnumbersbetween
0 and1.Astheprogramruns,thevalueofxseemstojumparound,well,chaotically.
Thefunctioncomputedbythisprogramhasthegeneralform:k



x

1  x, wherekinthiscaseis3.9.
Thisis calleda logisticfunction.It modelscertainkindsofunstableelectroniccircuitsandis alsosometimes
usedtopredictpopulationunderlimitingconditions.Repeatedapplicationofthelogisticfunctioncanpro-
ducechaos.Althoughourprogramhasa welldefinedunderlyingbehavior, theoutputseemsunpredictable.
Aninterestingpropertyofchaoticfunctionsis thatverysmalldifferencesintheinitialvaluecanleadto
largedifferencesintheresultastheformulais repeatedlyapplied.Youcanseethisinthechaosprogramby
enteringnumbersthatdifferbyonlya smallamount.Hereis theoutputfroma modifiedprogramthatshows
theresultsforinitialvaluesof 0 25 and 0 26 sidebyside.


input 0.25 0.


0.731250 0.
0.766441 0.
0.698135 0.
0.821896 0.
0.570894 0.
0.955399 0.

Free download pdf