Seite - 173 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 173 -
Text der Seite - 173 -
6.8 Exercises 173
Minimization of a function of N variables, E(b1,.. .,bN) is mathematically
performedbyrequiringall thepartialderivatives to bezero:
∂E
∂b1 =0,
∂E
∂b2 =0,
...
∂E
∂bN =0 .
a) Compute the partial derivative ∂E/∂b1 and generalize to the arbitrary case
∂E/∂bn, 1≤n≤N.
b) Showthat
bn= 1
π ∫ π
−π f(t)sin(nt)dt .
c) Write a functionintegrate_coeffs(f, N, M) that computesb1,.. .,bN by
numerical integration,usingM intervals in the trapezoidal rule.
d) A remarkable property of the trapezoidal rule is that it is exact for inte-
grals ∫π
−π sinntdt (when subintervals are of equal size). Use this property
to create a function test_integrate_coeff to verify the implementation of
integrate_coeffs.
e) Implement the choice f(t) = 1
π t as a Python function f(t) and call
integrate_coeffs(f, 3, 100) to see what the optimal choice ofb1,b2,b3
is.
f) Make a function plot_approx(f, N, M, filename)where you plot f(t)
together with the best approximationSN as computed above, usingM intervals
fornumerical integration.Save theplot toa file withnamefilename.
g) Runplot_approx(f, N, M, filename)forf(t)= 1
π t forN =3,6,12,24.
Observehowtheapproximationimproves.
h) Run plot_approx forf(t) = e−(t−π) andN = 100. Observe a fundamental
problem: regardless ofN,SN(−π)= 0, note2π ≈ 535. (There are ways to fix
this issue.)
Filename:autofit_sines.py.
Exercise6.13:Derive the TrapezoidalRule foraDouble Integral
Use ideas in Sect.6.7.1 to derive a formula for computing a double
integral∫b
a ∫d
c f(x,y)dydxby the trapezoidal rule. Implementand test this rule.
Filename:trapezoidal_double.py.
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Band
- Second Edition
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2020
- Sprache
- englisch
- Lizenz
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 356
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik