Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Page - 159 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 159 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 159 -

Image of the Page - 159 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 159 -

6.7 DoubleandTriple Integrals 159 Direct Derivation The formula (6.29) can also be derived directly in the two- dimensional case by applying the idea of the midpoint method. We divide the rectangle [a,b]Γ— [c,d] into nx Γ—ny equal-sized parts called cells. The idea of themidpointmethod is to approximatef bya constantovereachcell, andevaluate theconstantat themidpoint.Cell (i,j)occupies thearea [a+ ihx,a+(i+1)hx]Γ—[c+jhy,c+(j+1)hy], and themidpoint is (xi,yj)with xi =a+ ihx+ 1 2 hx, yj = c+jhy+ 1 2 hy . Theintegralover thecell is thereforehxhyf(xi,yj), and the totaldouble integral is thesumoverall cells,which isnothingbut formula(6.29). ProgrammingaDoubleSum Theformula(6.29) involvesadoublesum,which is normallyimplementedasadoublefor loop.APythonfunctionimplementing(6.29) maylook like def midpoint_double1(f, a, b, c, d, nx, ny): hx = (b - a)/nx hy = (d - c)/ny I = 0 for i in range(nx): for j in range(ny): xi = a + hx/2 + i*hx yj = c + hy/2 + j*hy I = I + hx*hy*f(xi, yj) return I If thisfunctionisstoredinamodulefilemidpoint_double.py,wecancompute someintegral,e.g., ∫3 2 ∫2 0 (2x+y)dxdy=9 inan interactiveshell anddemonstrate that the functioncomputes the rightnumber: In [1]: from midpoint_double import midpoint_double1 In [2]: def f(x, y): ...: return 2*x + y ...: In [3]: midpoint_double1(f, 0, 2, 2, 3, 5, 5) Out[3]: 9.000000000000005 Reusing Code for One-Dimensional Integrals It is very natural to write a two- dimensionalmidpointmethodas we did in functionmidpoint_double1when we have the formula (6.29). However, we could alternatively ask, much as we did in the mathematics, can we reuse a well-tested implementation for one-dimensional integrals tocomputedouble integrals?That is, canweuse functionmidpoint def midpoint(f, a, b, n): h = (b-a)/n f_sum = 0
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
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
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python