/
chap02.html
1367 lines (1207 loc) · 74 KB
/
chap02.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.18.1: http://docutils.sourceforge.net/" />
<title>Bike Share System — Modeling and Simulation in Python</title>
<script data-cfasync="false">
document.documentElement.dataset.mode = localStorage.getItem("mode") || "";
document.documentElement.dataset.theme = localStorage.getItem("theme") || "light";
</script>
<!-- Loaded before other Sphinx assets -->
<link href="_static/styles/theme.css?digest=e353d410970836974a52" rel="stylesheet" />
<link href="_static/styles/bootstrap.css?digest=e353d410970836974a52" rel="stylesheet" />
<link href="_static/styles/pydata-sphinx-theme.css?digest=e353d410970836974a52" rel="stylesheet" />
<link href="_static/vendor/fontawesome/6.1.2/css/all.min.css?digest=e353d410970836974a52" rel="stylesheet" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.1.2/webfonts/fa-solid-900.woff2" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.1.2/webfonts/fa-brands-400.woff2" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.1.2/webfonts/fa-regular-400.woff2" />
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
<link rel="stylesheet" href="_static/styles/sphinx-book-theme.css?digest=14f4ca6b54d191a8c7657f6c759bf11a5fb86285" type="text/css" />
<link rel="stylesheet" type="text/css" href="_static/togglebutton.css" />
<link rel="stylesheet" type="text/css" href="_static/copybutton.css" />
<link rel="stylesheet" type="text/css" href="_static/mystnb.4510f1fc1dee50b3e5859aac5469c37c29e427902b24a333a5f9fcb2f0b3ac41.css" />
<link rel="stylesheet" type="text/css" href="_static/sphinx-thebe.css" />
<link rel="stylesheet" type="text/css" href="_static/design-style.4045f2051d55cab465a707391d5b2007.min.css" />
<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="_static/scripts/bootstrap.js?digest=e353d410970836974a52" />
<link rel="preload" as="script" href="_static/scripts/pydata-sphinx-theme.js?digest=e353d410970836974a52" />
<script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/_sphinx_javascript_frameworks_compat.js"></script>
<script src="_static/doctools.js"></script>
<script src="_static/clipboard.min.js"></script>
<script src="_static/copybutton.js"></script>
<script src="_static/scripts/sphinx-book-theme.js?digest=5a5c038af52cf7bc1a1ec88eea08e6366ee68824"></script>
<script>let toggleHintShow = 'Click to show';</script>
<script>let toggleHintHide = 'Click to hide';</script>
<script>let toggleOpenOnPrint = 'true';</script>
<script src="_static/togglebutton.js"></script>
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown';</script>
<script src="_static/design-tabs.js"></script>
<script>const THEBE_JS_URL = "https://unpkg.com/thebe@0.8.2/lib/index.js"
const thebe_selector = ".thebe,.cell"
const thebe_selector_input = "pre"
const thebe_selector_output = ".output, .cell_output"
</script>
<script async="async" src="_static/sphinx-thebe.js"></script>
<script>window.MathJax = {"options": {"processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
<script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script>DOCUMENTATION_OPTIONS.pagename = 'chap02';</script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Iteration" href="chap03.html" />
<link rel="prev" title="Modeling" href="chap01.html" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="docsearch:language" content="en"/>
</head>
<body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="">
<a class="skip-link" href="#main-content">Skip to main content</a>
<input type="checkbox"
class="sidebar-toggle"
name="__primary"
id="__primary"/>
<label class="overlay overlay-primary" for="__primary"></label>
<input type="checkbox"
class="sidebar-toggle"
name="__secondary"
id="__secondary"/>
<label class="overlay overlay-secondary" for="__secondary"></label>
<div class="search-button__wrapper">
<div class="search-button__overlay"></div>
<div class="search-button__search-container">
<form class="bd-search d-flex align-items-center"
action="search.html"
method="get">
<i class="fa-solid fa-magnifying-glass"></i>
<input type="search"
class="form-control"
name="q"
id="search-input"
placeholder="Search this book..."
aria-label="Search this book..."
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"/>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span>
</form></div>
</div>
<nav class="bd-header navbar navbar-expand-lg bd-navbar">
</nav>
<div class="bd-container">
<div class="bd-container__inner bd-page-width">
<div class="bd-sidebar-primary bd-sidebar">
<div class="sidebar-header-items sidebar-primary__section">
</div>
<div class="sidebar-primary-items__start sidebar-primary__section">
<div class="sidebar-primary-item">
<a class="navbar-brand logo" href="index.html">
<p class="title logo__title">Modeling and Simulation in Python</p>
</a></div>
<div class="sidebar-primary-item"><nav class="bd-links" id="bd-docs-nav" aria-label="Main">
<div class="bd-toc-item navbar-nav active">
<ul class="nav bd-sidenav bd-sidenav__home-link">
<li class="toctree-l1">
<a class="reference internal" href="index.html">
Modeling and Simulation in Python
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Chapters</span></p>
<ul class="current nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="preface.html">Preface</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap01.html">Modeling</a></li>
<li class="toctree-l1 current active"><a class="current reference internal" href="#">Bike Share System</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap03.html">Iteration</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap04.html">Sweeping Parameters</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap05.html">World Population</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap06.html">Proportional Growth</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap07.html">Limits to Growth</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap08.html">Projecting Population Growth</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap09.html">Analysis of Population Growth</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap10.html">Case Studies Part 1</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap11.html">Epidemiology</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap12.html">Modeling Vaccination</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap13.html">Sweeping Parameters</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap14.html">Nondimensionalization</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap15.html">Cooling Coffee</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap16.html">Adding Milk</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap17.html">Pharmacokinetics</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap18.html">Glucose and Insulin</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap19.html">Case Studies Part 2</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap20.html">The Empire State Building Strikes Back</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap21.html">Drag</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap22.html">Baseball</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap23.html">Optimal Baseball</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap24.html">Rotation</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap25.html">Torque</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap26.html">Case Studies Part 3</a></li>
<li class="toctree-l1"><a class="reference internal" href="chap27.html">Under the Hood</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Examples</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="queue.html">One Queue or Two</a></li>
</ul>
</div>
</nav></div>
</div>
<div class="sidebar-primary-items__end sidebar-primary__section">
</div>
<div id="rtd-footer-container"></div>
</div>
<main id="main-content" class="bd-main">
<div class="sbt-scroll-pixel-helper"></div>
<div class="bd-content">
<div class="bd-article-container">
<div class="bd-header-article">
<div class="header-article-items header-article__inner">
<div class="header-article-items__start">
<div class="header-article-item"><label class="sidebar-toggle primary-toggle btn btn-sm" for="__primary" title="Toggle primary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="fa-solid fa-bars"></span>
</label></div>
</div>
<div class="header-article-items__end">
<div class="header-article-item">
<div class="article-header-buttons">
<a href="https://github.com/AllenDowney/ModSimPy" target="_blank"
class="btn btn-sm btn-source-repository-button"
title="Source repository"
data-bs-placement="bottom" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fab fa-github"></i>
</span>
</a>
<div class="dropdown dropdown-download-buttons">
<button class="btn dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false" aria-label="Download this page">
<i class="fas fa-download"></i>
</button>
<ul class="dropdown-menu">
<li><a href="_sources/chap02.ipynb" target="_blank"
class="btn btn-sm btn-download-source-button dropdown-item"
title="Download source file"
data-bs-placement="left" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fas fa-file"></i>
</span>
<span class="btn__text-container">.ipynb</span>
</a>
</li>
<li>
<button onclick="window.print()"
class="btn btn-sm btn-download-pdf-button dropdown-item"
title="Print to PDF"
data-bs-placement="left" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fas fa-file-pdf"></i>
</span>
<span class="btn__text-container">.pdf</span>
</button>
</li>
</ul>
</div>
<button onclick="toggleFullScreen()"
class="btn btn-sm btn-fullscreen-button"
title="Fullscreen mode"
data-bs-placement="bottom" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fas fa-expand"></i>
</span>
</button>
<script>
document.write(`
<button class="theme-switch-button btn btn-sm btn-outline-primary navbar-btn rounded-circle" title="light/dark" aria-label="light/dark" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="theme-switch" data-mode="light"><i class="fa-solid fa-sun"></i></span>
<span class="theme-switch" data-mode="dark"><i class="fa-solid fa-moon"></i></span>
<span class="theme-switch" data-mode="auto"><i class="fa-solid fa-circle-half-stroke"></i></span>
</button>
`);
</script>
<script>
document.write(`
<button class="btn btn-sm navbar-btn search-button search-button__button" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
</button>
`);
</script>
<label class="sidebar-toggle secondary-toggle btn btn-sm" for="__secondary"title="Toggle secondary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="fa-solid fa-list"></span>
</label>
</div></div>
</div>
</div>
</div>
<div id="jb-print-docs-body" class="onlyprint">
<h1>Bike Share System</h1>
<!-- Table of contents -->
<div id="print-main-content">
<div id="jb-print-toc">
<div>
<h2> Contents </h2>
</div>
<nav aria-label="Page">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#modeling-a-bike-share-system">Modeling a Bike Share System</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#defining-functions">Defining Functions</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#print-statements">Print Statements</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#if-statements">If Statements</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#parameters">Parameters</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#for-loops">For Loops</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#timeseries">TimeSeries</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#plotting">Plotting</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#summary">Summary</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exercises">Exercises</a><ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-1">Exercise 1</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-2">Exercise 2</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#exercise-3">Exercise 3</a></li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#under-the-hood">Under the Hood</a></li>
</ul>
</nav>
</div>
</div>
</div>
<div id="searchbox"></div>
<article class="bd-article" role="main">
<section class="tex2jax_ignore mathjax_ignore" id="bike-share-system">
<h1>Bike Share System<a class="headerlink" href="#bike-share-system" title="Permalink to this heading">#</a></h1>
<p>This chapter presents a simple model of a bike share system and
demonstrates the features of Python we’ll use to develop simulations of real-world systems.</p>
<p>Along the way, we’ll make decisions about how to model the system. In
the next chapter we’ll review these decisions and gradually improve the model.</p>
<p>This chapter is available as a Jupyter notebook where you can read the text, run the code, and work on the exercises.
Click here to access the notebooks: <a class="reference external" href="https://allendowney.github.io/ModSimPy/">https://allendowney.github.io/ModSimPy/</a>.</p>
<section id="modeling-a-bike-share-system">
<h2>Modeling a Bike Share System<a class="headerlink" href="#modeling-a-bike-share-system" title="Permalink to this heading">#</a></h2>
<p>Imagine a bike share system for students traveling between Olin College and Wellesley College, which are about three miles apart in eastern Massachusetts.</p>
<p>Suppose the system contains 12 bikes and two bike racks, one at Olin and one at Wellesley, each with the capacity to hold 12 bikes.</p>
<p>As students arrive, check out a bike, and ride to the other campus, the number of bikes in each location changes. In the simulation, we’ll need to keep track of where the bikes are. To do that, we’ll use a function called <code class="docutils literal notranslate"><span class="pre">State</span></code>, which is defined in the ModSim library.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">bikeshare</span> <span class="o">=</span> <span class="n">State</span><span class="p">(</span><span class="n">olin</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">wellesley</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<p>The equations in parentheses create two variables, <code class="docutils literal notranslate"><span class="pre">olin</span></code> and <code class="docutils literal notranslate"><span class="pre">wellesley</span></code>, and give them the values <code class="docutils literal notranslate"><span class="pre">10</span></code> and <code class="docutils literal notranslate"><span class="pre">2</span></code>.
The <code class="docutils literal notranslate"><span class="pre">State</span></code> function stores these variables and their values in a <code class="docutils literal notranslate"><span class="pre">State</span></code> object, which gets assigned to a new variable named <code class="docutils literal notranslate"><span class="pre">bikeshare</span></code>.</p>
<p>Variables stored inside a <code class="docutils literal notranslate"><span class="pre">State</span></code> object are called <em>state variables</em>.
In this example, the state variables represent the number of
bikes at each location. Their values indicate that there are 10 bikes at Olin and 2 at Wellesley.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">State</span></code> object is assigned to a new variable named <code class="docutils literal notranslate"><span class="pre">bikeshare</span></code>.
We can get the value of a variable in a <code class="docutils literal notranslate"><span class="pre">State</span></code> object using the <em>dot operator</em>, like this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">bikeshare</span><span class="o">.</span><span class="n">olin</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>10
</pre></div>
</div>
</div>
</div>
<p>And this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">bikeshare</span><span class="o">.</span><span class="n">wellesley</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>2
</pre></div>
</div>
</div>
</div>
<p>Or, to display all of the state variables and their values, you can enter just the name of the object:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">bikeshare</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>olin 10
wellesley 2
Name: state, dtype: int64
</pre></div>
</div>
</div>
</div>
<p>These values make up the <em>state</em> of the system.</p>
<p>We can update the state by assigning new values to the variables.
For example, if a student moves a bike from Olin to Wellesley, we can figure out the new values and assign them:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">bikeshare</span><span class="o">.</span><span class="n">olin</span> <span class="o">=</span> <span class="mi">9</span>
<span class="n">bikeshare</span><span class="o">.</span><span class="n">wellesley</span> <span class="o">=</span> <span class="mi">3</span>
</pre></div>
</div>
</div>
</div>
<p>Or we can use <em>update operators</em>, <code class="docutils literal notranslate"><span class="pre">-=</span></code> and <code class="docutils literal notranslate"><span class="pre">+=</span></code>, to subtract 1 from
<code class="docutils literal notranslate"><span class="pre">olin</span></code> and add 1 to <code class="docutils literal notranslate"><span class="pre">wellesley</span></code>:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">bikeshare</span><span class="o">.</span><span class="n">olin</span> <span class="o">-=</span> <span class="mi">1</span>
<span class="n">bikeshare</span><span class="o">.</span><span class="n">wellesley</span> <span class="o">+=</span> <span class="mi">1</span>
</pre></div>
</div>
</div>
</div>
<p>The result is the same either way.</p>
</section>
<section id="defining-functions">
<h2>Defining Functions<a class="headerlink" href="#defining-functions" title="Permalink to this heading">#</a></h2>
<p>So far we have used functions defined in NumPy and the ModSim library. Now we’re going to define our own functions.</p>
<p>When you are developing code in Jupyter, it is often efficient to write a few lines of code, test them to confirm they do what you intend, and then use them to define a new function. For example, these lines move a bike from Olin to Wellesley:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">bikeshare</span><span class="o">.</span><span class="n">olin</span> <span class="o">-=</span> <span class="mi">1</span>
<span class="n">bikeshare</span><span class="o">.</span><span class="n">wellesley</span> <span class="o">+=</span> <span class="mi">1</span>
</pre></div>
</div>
</div>
</div>
<p>Rather than repeat them every time a bike moves, we can define a new
function:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">bike_to_wellesley</span><span class="p">():</span>
<span class="n">bikeshare</span><span class="o">.</span><span class="n">olin</span> <span class="o">-=</span> <span class="mi">1</span>
<span class="n">bikeshare</span><span class="o">.</span><span class="n">wellesley</span> <span class="o">+=</span> <span class="mi">1</span>
</pre></div>
</div>
</div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">def</span></code> is a special word in Python that indicates we are defining a new
function. The name of the function is <code class="docutils literal notranslate"><span class="pre">bike_to_wellesley</span></code>. The empty
parentheses indicate that this function requires no additional
information when it runs. The colon indicates the beginning of an
indented <em>code block</em>.</p>
<p>The next two lines are the <em>body</em> of the function. They have to be
indented; by convention, the indentation is four spaces.</p>
<p>When you define a function, it has no immediate effect. The body of the
function doesn’t run until you <em>call</em> the function. Here’s how to call
this function:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">bike_to_wellesley</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<p>When you call the function, it runs the statements in the body, which
update the variables of the <code class="docutils literal notranslate"><span class="pre">bikeshare</span></code> object; you can check by
displaying the new state.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">show</span><span class="p">(</span><span class="n">bikeshare</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>state</th>
</tr>
</thead>
<tbody>
<tr>
<th>olin</th>
<td>6</td>
</tr>
<tr>
<th>wellesley</th>
<td>6</td>
</tr>
</tbody>
</table>
</div></div></div>
</div>
<p>When you call a function, you have to include the parentheses. If you
leave them out, you get this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">bike_to_wellesley</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span><function __main__.bike_to_wellesley()>
</pre></div>
</div>
</div>
</div>
<p>This result indicates that <code class="docutils literal notranslate"><span class="pre">bike_to_wellesley</span></code> is a function. You don’t have to know what <code class="docutils literal notranslate"><span class="pre">__main__</span></code> means, but if you see something like this, it probably means that you named a function but didn’t actually call it.
So don’t forget the parentheses.</p>
</section>
<section id="print-statements">
<h2>Print Statements<a class="headerlink" href="#print-statements" title="Permalink to this heading">#</a></h2>
<p>As you write more complicated programs, it is easy to lose track of what
is going on. One of the most useful tools for debugging is the <em>print statement</em>, which displays text in the Jupyter notebook.</p>
<p>Normally when Jupyter runs the code in a cell, it displays the value of
the last line of code. For example, if you run:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">bikeshare</span><span class="o">.</span><span class="n">olin</span>
<span class="n">bikeshare</span><span class="o">.</span><span class="n">wellesley</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>6
</pre></div>
</div>
</div>
</div>
<p>Jupyter runs both lines, but it only displays the value of the
second. If you want to display more than one value, you can use
print statements:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">bikeshare</span><span class="o">.</span><span class="n">olin</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">bikeshare</span><span class="o">.</span><span class="n">wellesley</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>6
6
</pre></div>
</div>
</div>
</div>
<p>When you call the <code class="docutils literal notranslate"><span class="pre">print</span></code> function, you can put a variable in
parentheses, as in the previous example, or you can provide a sequence
of variables separated by commas, like this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">bikeshare</span><span class="o">.</span><span class="n">olin</span><span class="p">,</span> <span class="n">bikeshare</span><span class="o">.</span><span class="n">wellesley</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>6 6
</pre></div>
</div>
</div>
</div>
<p>Python looks up the values of the variables and displays them; in this
example, it displays two values on the same line, with a space between
them.</p>
<p>Print statements are useful for debugging functions. For example, we can
add a print statement to <code class="docutils literal notranslate"><span class="pre">bike_to_wellesley</span></code>, like this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">bike_to_wellesley</span><span class="p">():</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'Moving a bike to Wellesley'</span><span class="p">)</span>
<span class="n">bikeshare</span><span class="o">.</span><span class="n">olin</span> <span class="o">-=</span> <span class="mi">1</span>
<span class="n">bikeshare</span><span class="o">.</span><span class="n">wellesley</span> <span class="o">+=</span> <span class="mi">1</span>
</pre></div>
</div>
</div>
</div>
<p>Each time we call this version of the function, it displays a message,
which can help us keep track of what the program is doing.
The message in this example is a <em>string</em>, which is a sequence of
letters and other symbols in quotes.</p>
<p>Just like <code class="docutils literal notranslate"><span class="pre">bike_to_wellesley</span></code>, we can define a function that moves a
bike from Wellesley to Olin:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">bike_to_olin</span><span class="p">():</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'Moving a bike to Olin'</span><span class="p">)</span>
<span class="n">bikeshare</span><span class="o">.</span><span class="n">wellesley</span> <span class="o">-=</span> <span class="mi">1</span>
<span class="n">bikeshare</span><span class="o">.</span><span class="n">olin</span> <span class="o">+=</span> <span class="mi">1</span>
</pre></div>
</div>
</div>
</div>
<p>And call it like this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">bike_to_olin</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Moving a bike to Olin
</pre></div>
</div>
</div>
</div>
<p>One benefit of defining functions is that you avoid repeating chunks of
code, which makes programs smaller. Another benefit is that the name you
give the function documents what it does, which makes programs more
readable.</p>
</section>
<section id="if-statements">
<h2>If Statements<a class="headerlink" href="#if-statements" title="Permalink to this heading">#</a></h2>
<p>At this point we have functions that simulate moving bikes; now let’s think about simulating customers. As a simple model of customer behavior, I will use a random number generator to determine when customers arrive at each station.</p>
<p>The ModSim library provides a function called <code class="docutils literal notranslate"><span class="pre">flip</span></code> that generates random “coin tosses”.
When you call it, you provide a probability between 0 and 1, like this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">flip</span><span class="p">(</span><span class="mf">0.7</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>True
</pre></div>
</div>
</div>
</div>
<p>The result is one of two values: <code class="docutils literal notranslate"><span class="pre">True</span></code> with probability 0.7 (in this example) or <code class="docutils literal notranslate"><span class="pre">False</span></code>
with probability 0.3. If you run <code class="docutils literal notranslate"><span class="pre">flip</span></code> like this 100 times, you should
get <code class="docutils literal notranslate"><span class="pre">True</span></code> about 70 times and <code class="docutils literal notranslate"><span class="pre">False</span></code> about 30 times. But the results
are random, so they might differ from these expectations.</p>
<p><code class="docutils literal notranslate"><span class="pre">True</span></code> and <code class="docutils literal notranslate"><span class="pre">False</span></code> are special values defined by Python.
They are called <em>boolean</em> values because they are
related to Boolean algebra (<a class="reference external" href="https://modsimpy.com/boolean">https://modsimpy.com/boolean</a>).</p>
<p>Note that they are not strings. There is a difference between <code class="docutils literal notranslate"><span class="pre">True</span></code>, which is a boolean value, and <code class="docutils literal notranslate"><span class="pre">'True'</span></code>, which is a string.</p>
<p>We can use boolean values to control the behavior of the program, using an <em>if statement</em>:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">flip</span><span class="p">(</span><span class="mf">0.5</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'heads'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<p>If the result from <code class="docutils literal notranslate"><span class="pre">flip</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the program displays the string
<code class="docutils literal notranslate"><span class="pre">'heads'</span></code>. Otherwise it does nothing.</p>
<p>The syntax for <code class="docutils literal notranslate"><span class="pre">if</span></code> statements is similar to the syntax for
function definitions: the first line has to end with a colon, and the
lines inside the <code class="docutils literal notranslate"><span class="pre">if</span></code> statement have to be indented.</p>
<p>Optionally, you can add an <em>else clause</em> to indicate what should
happen if the result is <code class="docutils literal notranslate"><span class="pre">False</span></code>:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">flip</span><span class="p">(</span><span class="mf">0.5</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'heads'</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'tails'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>heads
</pre></div>
</div>
</div>
</div>
<p>If you run the previous cell a few times, it should print <code class="docutils literal notranslate"><span class="pre">heads</span></code> about half the time, and <code class="docutils literal notranslate"><span class="pre">tails</span></code> about half the time.</p>
<p>Now we can use <code class="docutils literal notranslate"><span class="pre">flip</span></code> to simulate the arrival of customers who want to
borrow a bike. Suppose students arrive at the Olin station every two
minutes on average.
In that case, the chance of an arrival during any one-minute period is 50%, and we can simulate it like this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">flip</span><span class="p">(</span><span class="mf">0.5</span><span class="p">):</span>
<span class="n">bike_to_wellesley</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Moving a bike to Wellesley
</pre></div>
</div>
</div>
</div>
<p>If students arrive at the Wellesley station every three minutes, on average,
the chance of an arrival during any one-minute period is 33%, and we can
simulate it like this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">flip</span><span class="p">(</span><span class="mf">0.33</span><span class="p">):</span>
<span class="n">bike_to_olin</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<p>We can combine these snippets into a function that simulates a <em>time step</em>, which is an interval of time, in this case one minute:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">step</span><span class="p">():</span>
<span class="k">if</span> <span class="n">flip</span><span class="p">(</span><span class="mf">0.5</span><span class="p">):</span>
<span class="n">bike_to_wellesley</span><span class="p">()</span>
<span class="k">if</span> <span class="n">flip</span><span class="p">(</span><span class="mf">0.33</span><span class="p">):</span>
<span class="n">bike_to_olin</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<p>Then we can simulate a time step like this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">step</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<p>Depending on the results from <code class="docutils literal notranslate"><span class="pre">flip</span></code>, this function might move a bike to Olin, or to Wellesley, or neither, or both.</p>
</section>
<section id="parameters">
<h2>Parameters<a class="headerlink" href="#parameters" title="Permalink to this heading">#</a></h2>
<p>The previous version of <code class="docutils literal notranslate"><span class="pre">step</span></code> is fine if the arrival probabilities
never change, but in reality they vary over time.</p>
<p>So instead of putting the constant values 0.5 and 0.33 in <code class="docutils literal notranslate"><span class="pre">step</span></code>, we can replace them with <em>parameters</em>.
Parameters are variables whose values are set when a function is called.</p>
<p>Here’s a version of <code class="docutils literal notranslate"><span class="pre">step</span></code> that takes two parameters, <code class="docutils literal notranslate"><span class="pre">p1</span></code> and <code class="docutils literal notranslate"><span class="pre">p2</span></code>:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">step</span><span class="p">(</span><span class="n">p1</span><span class="p">,</span> <span class="n">p2</span><span class="p">):</span>
<span class="k">if</span> <span class="n">flip</span><span class="p">(</span><span class="n">p1</span><span class="p">):</span>
<span class="n">bike_to_wellesley</span><span class="p">()</span>
<span class="k">if</span> <span class="n">flip</span><span class="p">(</span><span class="n">p2</span><span class="p">):</span>
<span class="n">bike_to_olin</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<p>The values of <code class="docutils literal notranslate"><span class="pre">p1</span></code> and <code class="docutils literal notranslate"><span class="pre">p2</span></code> are not set inside this function; instead,
they are provided when the function is called, like this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">step</span><span class="p">(</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.33</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Moving a bike to Olin
</pre></div>
</div>
</div>
</div>
<p>The values you provide when you call the function are called
<em>arguments</em>. The arguments, <code class="docutils literal notranslate"><span class="pre">0.5</span></code> and <code class="docutils literal notranslate"><span class="pre">0.33</span></code> in this example, get
assigned to the parameters, <code class="docutils literal notranslate"><span class="pre">p1</span></code> and <code class="docutils literal notranslate"><span class="pre">p2</span></code>, in order. So running this
function has the same effect as:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">p1</span> <span class="o">=</span> <span class="mf">0.5</span>
<span class="n">p2</span> <span class="o">=</span> <span class="mf">0.33</span>
<span class="k">if</span> <span class="n">flip</span><span class="p">(</span><span class="n">p1</span><span class="p">):</span>
<span class="n">bike_to_wellesley</span><span class="p">()</span>
<span class="k">if</span> <span class="n">flip</span><span class="p">(</span><span class="n">p2</span><span class="p">):</span>
<span class="n">bike_to_olin</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Moving a bike to Wellesley
</pre></div>
</div>
</div>
</div>
<p>The advantage of using parameters is that you can call the same function many times, providing different arguments each time.</p>
<p>Adding parameters to a function is called <em>generalization</em>, because it makes the function more general; without parameters, the function always does the same thing; with parameters, it can do a range of things.</p>
</section>
<section id="for-loops">
<h2>For Loops<a class="headerlink" href="#for-loops" title="Permalink to this heading">#</a></h2>
<p>At some point you will get sick of running cells over and over.
Fortunately, there is an easy way to repeat a chunk of code, the <em>for loop</em>. Here’s an example:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="n">bike_to_wellesley</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>0
Moving a bike to Wellesley
1
Moving a bike to Wellesley
2
Moving a bike to Wellesley
</pre></div>
</div>
</div>
</div>
<p>The syntax here should look familiar; the first line ends with a
colon, and the lines inside the <code class="docutils literal notranslate"><span class="pre">for</span></code> loop are indented. The other
elements of the loop are:</p>
<ul class="simple">
<li><p>The words <code class="docutils literal notranslate"><span class="pre">for</span></code> and <code class="docutils literal notranslate"><span class="pre">in</span></code> are special words we have to use in a for
loop.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">range</span></code> is a Python function we use to control the number of times the loop runs.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">i</span></code> is a <em>loop variable</em> that gets created when the for loop runs.</p></li>
</ul>
<p>When this loop runs, it runs the statements inside the loop three times. The first time, the value of <code class="docutils literal notranslate"><span class="pre">i</span></code> is <code class="docutils literal notranslate"><span class="pre">0</span></code>; the second time, it is <code class="docutils literal notranslate"><span class="pre">1</span></code>; the third time, it is <code class="docutils literal notranslate"><span class="pre">2</span></code>.</p>
<p>Each time through the loop, it prints the value of <code class="docutils literal notranslate"><span class="pre">i</span></code> and moves one bike to Wellesley.</p>
</section>
<section id="timeseries">
<h2>TimeSeries<a class="headerlink" href="#timeseries" title="Permalink to this heading">#</a></h2>
<p>When we run a simulation, we often want to save the results for later analysis. The ModSim library provides a <code class="docutils literal notranslate"><span class="pre">TimeSeries</span></code> object for this purpose. A <code class="docutils literal notranslate"><span class="pre">TimeSeries</span></code> contains a sequence of timestamps and a
corresponding sequence of quantities.</p>
<p>In this example, the timestamps are integers representing minutes and the quantities are the number of bikes at one location.</p>
<p>Since we have moved a number of bikes around, let’s start again with a new <code class="docutils literal notranslate"><span class="pre">State</span></code> object.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">bikeshare</span> <span class="o">=</span> <span class="n">State</span><span class="p">(</span><span class="n">olin</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">wellesley</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<p>We can create a new, empty <code class="docutils literal notranslate"><span class="pre">TimeSeries</span></code> like this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">results</span> <span class="o">=</span> <span class="n">TimeSeries</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<p>And we can add a quantity like this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">results</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">bikeshare</span><span class="o">.</span><span class="n">olin</span>
</pre></div>
</div>
</div>
</div>
<p>The number in brackets is the timestamp, also called a <em>label</em>.</p>
<p>We can use a <code class="docutils literal notranslate"><span class="pre">TimeSeries</span></code> inside a for loop to store the results of the simulation:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="n">step</span><span class="p">(</span><span class="mf">0.6</span><span class="p">,</span> <span class="mf">0.6</span><span class="p">)</span>
<span class="n">results</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">bikeshare</span><span class="o">.</span><span class="n">olin</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>0
Moving a bike to Wellesley
1
Moving a bike to Olin
2
Moving a bike to Olin
</pre></div>
</div>
</div>
</div>
<p>Each time through the loop, we print the value of <code class="docutils literal notranslate"><span class="pre">i</span></code> and call <code class="docutils literal notranslate"><span class="pre">step</span></code>, which updates <code class="docutils literal notranslate"><span class="pre">bikeshare</span></code>.
Then we store the number of bikes at Olin in <code class="docutils literal notranslate"><span class="pre">results</span></code>.
We use the loop variable, <code class="docutils literal notranslate"><span class="pre">i</span></code>, to compute the timestamp, <code class="docutils literal notranslate"><span class="pre">i+1</span></code>.</p>
<p>The first time through the loop, the value of <code class="docutils literal notranslate"><span class="pre">i</span></code> is <code class="docutils literal notranslate"><span class="pre">0</span></code>, so the timestamp is <code class="docutils literal notranslate"><span class="pre">1</span></code>.
The last time, the value of <code class="docutils literal notranslate"><span class="pre">i</span></code> is <code class="docutils literal notranslate"><span class="pre">2</span></code>, so the timestamp is <code class="docutils literal notranslate"><span class="pre">3</span></code>.</p>
<p>When the loop exits, <code class="docutils literal notranslate"><span class="pre">results</span></code> contains 4 timestamps, from 0 through
3, and the number of bikes at Olin at the end of each time step.</p>
<p>We can display the <code class="docutils literal notranslate"><span class="pre">TimeSeries</span></code> like this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">show</span><span class="p">(</span><span class="n">results</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Quantity</th>
</tr>
<tr>
<th>Time</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>10</td>
</tr>
<tr>
<th>1</th>
<td>9</td>
</tr>
<tr>
<th>2</th>
<td>10</td>
</tr>
<tr>
<th>3</th>
<td>11</td>
</tr>
</tbody>
</table>
</div></div></div>
</div>
<p>The left column is the timestamps; the right column is the quantities.</p>
</section>
<section id="plotting">
<h2>Plotting<a class="headerlink" href="#plotting" title="Permalink to this heading">#</a></h2>
<p><code class="docutils literal notranslate"><span class="pre">results</span></code> provides a function called <code class="docutils literal notranslate"><span class="pre">plot</span></code> we can use to plot
the results, and the ModSim library provides <code class="docutils literal notranslate"><span class="pre">decorate</span></code>, which we can use to label the axes and give the figure a title:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">results</span><span class="o">.</span><span class="n">plot</span><span class="p">()</span>
<span class="n">decorate</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="s1">'Olin-Wellesley bikeshare'</span><span class="p">,</span>
<span class="n">xlabel</span><span class="o">=</span><span class="s1">'Time step (min)'</span><span class="p">,</span>
<span class="n">ylabel</span><span class="o">=</span><span class="s1">'Number of bikes'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/4f14cd8acc6e6a540637a5c77c8b75d12cc706e7f83a9f0ba4fa690b379f2647.png" src="_images/4f14cd8acc6e6a540637a5c77c8b75d12cc706e7f83a9f0ba4fa690b379f2647.png" />
</div>