Page - 133 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 133 -
Text of the Page - 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:
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