Page - 216 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 216 -
Text of the Page - 216 -
216 A GettingAccesstoPython
A.5.1 ASimpleProgramintheNotebook
Start the IPythonnotebook locally by the commandipython notebookor go to
SageMathCloudorWakari as described above. Thedefault input area is a cell for
Pythoncode. Type
g = 9.81
v0 = 5
t = 0.6
y = v0*t - 0.5*g*t**2
in a cell and run the cellbyclickingonRun Selected (notebookrunning locallyon
yourmachine)oron theβplayβbutton(notebookrunningin thecloud). Thisaction
will execute the Python code and initialize the variablesg, v0, t, andy. You can
thenwriteprint y in anewcell, execute that cell, andsee theoutputof this state-
ment in thebrowser. It is easy togoback toacell, edit thecode, and re-execute it.
To download the notebook to your computer, choose the File β Download as
menu and select the type of file to be downloaded: the original notebook format
(.ipynbfileextension)oraplainPythonprogramversionof thenotebook(.pyfile
extension).
A.5.2 MixingText,Mathematics,Code,andGraphics
The real strength of IPython notebooks ariseswhen youwant towrite a report to
document how a problem can be explored and solved. As a teaser, open a new
notebook,click in thefirst cell, andchooseMarkdownas format(notebookrunning
locally) or switch fromCode to Markdown in the pull-downmenu (notebook in
the cloud). The cell is nowa textfieldwhereyoucanwrite textwithMarkdown17
syntax.MathematicscanbeenteredasLATEXcode. Trysometextwith inlinemath-
ematicsandanequationonaseparate line:
Plot the curve $y=f(x)$, where
$$
f(x) = e^{-x}\sin (2\pi x),\quad x\in [0, 4]
$$
Execute thecell andyouwill seenicely typesetmathematics in thebrowser. In the
newcell, addsomecode toplotf.x/:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline # make plots inline in the notebook
x = np.linspace(0, 4, 101)
y = np.exp(-x)*np.sin(2*pi*x)
plt.plot(x, y, βb-β)
plt.xlabel(βxβ); plt.ylabel(βyβ)
17http://daringfireball.net/projects/markdown/syntax
back to the
book Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations β Python
A Gentle Introduction to Numerical Simulations with Python
- Title
- Programming for Computations β Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2016
- Language
- English
- License
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 248
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik