Page - 173 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 173 -
Text of the Page - 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, 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