Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Seite - 137 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

Seite - 137 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Bild der Seite - 137 -

Bild der Seite - 137 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Text der Seite - 137 -

6.2 TheCompositeTrapezoidalRule 137 The Programmer’sDilemma 1. Specific implementation: Should we write a special program for the particular integral, using the ideas from the general rule (6.17), but replacingf byv,x by t, andhbyΔt? 2. General implementation: Should we implement the general method (6.17), as written, in a general function trapezoidal(f, a, b, n) and solve the particular integral by a specialized call to this function? Ageneralimplementationisalwaysthebestchoice,notonlyforintegrals, but when programmingin general! The first alternative in the box above sounds less abstract and therefore more attractive to many. Nevertheless, as we hope will be evident from the following, the second alternative is actually the simplest and most reliable from both a mathematicalanda programmingpointofview. These authors will claim that the second alternative is the essence of the power of mathematics, while the first alternative is the source of much confusion about mathematics! General Implementation For the integral ∫b a f(x)dx, computed by the for- mula (6.17), we want a corresponding Python functiontrapezoidal to take any f ,a,b, andnas inputand return theapproximationto the integral. We writetrapezoidalas close as possible to the formula (6.17), making sure variablenamescorrespondto themathematicalnotation: def trapezoidal(f, a, b, n): h = (b-a)/n f_sum = 0 for i in range(1, n, 1): x = a + i*h f_sum = f_sum + f(x) return h*(0.5*f(a) + f_sum + 0.5*f(b)) Observe how the for loop takes care of (only) the sum over f(xi), and that the x values start with x = a+h (when i is 1), increases with h for each iteration, before ending with x = a+ (n−1)h. This is consistent with the x values of the sum in (6.17). After the loop, we finalize the computation and return the result. This will be our implementation of choice for the trapezoidal function, even though, typically for programming, it could have been implemented in different ways. Which implementation to choose, is sometimes just a matter of personal taste. Onealternativeversioncouldbe: def trapezoidal(f, a, b, n): h = (b-a)/n result = 0.5*f(a) + 0.5*f(b) for i in range(1, n): result += f(a + i*h) result *= h return result
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition"
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
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python