Page - 282 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 282 -
Text of the Page - 282 -
282 8 SolvingOrdinaryDifferentialEquations
Exercise8.18:The Three-StepAdams-BashforthMethod
ThisexercisebuildsonExercise8.17,soyoubetterdothatonefirst.Anothermulti-
stepmethod,is the three-stepAdams-Bashforthmethod.This isa thirdordermethod
witha computationalscheme that reads:
un+1 =un+Ξt
12 (
23f(un,tn)β16f(unβ1,tnβ1)+5f(unβ2,tnβ2) )
.
forn=2,3,.. .,Nt β1,withu0 =U0.
a) Assumethat someoneimplemented theschemeas follows:
def adams_bashforth_3(f, U_0, dt, T):
"""Third-order Adams-Bashforth scheme for solving first order ODE"""
N_t = int(round(T/dt))
u = np.zeros(N_t+1)
t = np.linspace(0, N_t*dt, len(u))
u[0] = U_0
# Compute missing starting values
u[1] = 100*np.exp(0.1*dt)
u[2] = 100*np.exp(0.1*(2*dt))
for n in range(1, N_t, 1):
u[n+1] = u[n] + (dt/12)*(23*f(u[n], t[n]) - \
16*f(u[n-1], t[n-1]) +\
5*f(u[n-2], t[n-2]))
return u, t
There is one (known!)bug here,find it! Try first by simply reading the code. If not
successful,youmaytry to run it anddosometesting onyourcomputer.
Also, what would you say about the way that missing starting values are
computed?
b) RepeatExercise8.17,using thegiven three-stepmethod in steadof the two-step
method.
Note that with the three-step method, you need 3 starting values. Use the
Runge-Kutta third order scheme for this purpose. However, check also the
convergencerate of theschemewhenmissingstartingvaluesarecomputedwith
ForwardEuler in stead.
Filename:Adams_Bashforth_3.py.
Exercise8.19:Use aBackwardEuler SchemeforOscillations
Consider(8.43)β(8.44)modelinganoscillatingengineeringsystem.This2Γ2ODE
systemcanbesolvedbytheBackwardEulerscheme,which isbasedondiscretizing
derivatives by collecting information backward in time. More specifically,uβ²(t) is
approximatedas
uβ²(t)β u(t)βu(tβΞt)
Ξt .
Programming for Computations β Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
- Title
- Programming for Computations β Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Volume
- Second Edition
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2020
- Language
- English
- License
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 356
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik