Seite - 133 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 133 -
Text der Seite - 133 -
4.3 OscillatingOne-DimensionalSystems 133
Fig.4.21 Simulationof10periodsofoscillationsbyHeun’smethod
import odespy
def f(u, t):
return u
method = odespy.Heun # or, e.g., odespy.ForwardEuler
solver = method(f)
solver.set_initial_condition(2)
time_points = np.linspace(0, 4, 101)
u, t = solver.solve(time_points)
In other words, you define your right-hand side function f(u, t), initialize an
Odespysolverobject, set the initialcondition,computeacollectionof timepoints
whereyouwant the solution, andask for the solution. The returnedarraysu andt
canbeplotteddirectly:plot(t, u).
A nice feature of Odespy is that problem parameters can be arguments to the
user’sf(u, t) function. Forexample, ifourODEproblemisu0 D auCb,with
twoproblemparametersa andb,wemaywriteourf functionas
def f(u, t, a, b):
return -a*u + b
Theextra,problem-dependentargumentsaandbcanbe transferred to this function
ifwecollect their values in a list or tuplewhencreating theOdespy solver anduse
thef_argsargument:
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