Seite - 165 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 165 -
Text der Seite - 165 -
6.7 DoubleandTriple Integrals 165
3. count the fractionq ofpoints that are insideΩ
4. approximateA(Ω)/A(R)byq, i.e., setA(Ω)=qA(R)
5. evaluate themeanoff , f¯ , at thepoints insideΩ
6. estimate the integralasA(Ω)f¯
Note that A(R) is trivial to compute since R is a rectangle, while A(Ω) is
unknown. However, if we assume that the fraction ofA(R) occupied byA(Ω) is
the same as the fraction of random points insideΩ, we get a simple estimate for
A(Ω).
To get an idea of the method, consider a circular domain Ω embedded in a
rectangle as shown below. A collection of random points is illustrated by black
dots.
Implementation APythonfunctionimplementing ∫
Ωf(x,y)dxdycanbewritten
like this:
import numpy as np
def MonteCarlo_double(f, g, x0, x1, y0, y1, n):
"""
Monte Carlo integration of f over a domain g>=0, embedded
in a rectangle [x0,x1]x[y0,y1]. n^2 is the number of
random points.
"""
# Draw n**2 random points in the rectangle
x = np.random.uniform(x0, x1, n)
y = np.random.uniform(y0, y1, n)
# Compute sum of f values inside the integration domain
f_mean = 0
num_inside = 0 # number of x,y points inside domain (g>=0)
for i in range(len(x)):
for j in range(len(y)):
if g(x[i], y[j]) >= 0:
num_inside = num_inside + 1
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