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 - 137 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

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

Image of the Page - 137 -

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

Text of the Page - 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
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