Seite - 92 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 92 -
Text der Seite - 92 -
92 3 Computing Integrals
MinimizationofafunctionofN variables,E.b1;:: :;bN/ ismathematicallyper-
formedby requiringall thepartial derivatives tobezero:
@E
@b1 D0;
@E
@b2 D0;
:::
@E
@bN D0:
a) Compute the partial derivative @E=@b1 and generalize to the arbitrary case
@E=@bn,1 n N .
b) Showthat
bn D 1 Z
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
integralsR
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/ D 1 t as a Python function f(t) and call inte-
grate_coeffs(f, 3, 100) to seewhat theoptimalchoiceofb1;b2;b3 is.
f) Make a function plot_approx(f, N, M, filename)where you plot f(t)
togetherwith thebest approximationSN ascomputedabove,usingM intervals
fornumerical integration. Save theplot toafilewithnamefilename.
g) Runplot_approx(f, N, M, filename)forf.t/D 1 t forN D3;6;12;24.
Observehowtheapproximation improves.
h) Runplot_approx forf.t/ D e .t / andN D 100. Observe a fundamental
problem: regardlessofN ,SN. /D0, note2 535. (Thereareways tofix
this issue.)
Filename:autofit_sines.py.
Exercise3.13:Derive the trapezoidalrule foradouble integral
Use ideas in Sect. 3.7.1 to derive a formula for computing a double
integralRb
a Rd
c f.x;y/dydx by the trapezoidal rule. Implementand test this rule.
Filename:trapezoidal_double.py.
Exercise3.14:Compute theareaofa trianglebyMonteCarlo integration
UsetheMonteCarlomethodfromSect.3.7.3 tocompute theareaofa trianglewith
verticesat . 1;0/, .1;0/, and .3;0/.
Filename:MC_triangle.py.
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