Seite - 216 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 216 -
Text der Seite - 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
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2016
- Sprache
- englisch
- Lizenz
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 248
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik