Seite - 142 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 142 -
Text der Seite - 142 -
142 6 ComputingIntegralsandTestingCode
Lookingbackon the twodifferentsolutions, thespecific implementationand the
general implementation,youshould realize that implementingageneralmathemat-
ical algorithm in a general function requires somewhat more abstract thinking,but
the resulting code can be used over and over again! Essentially, if you apply the
special-purpose style, you have to retest the implementation of the algorithm after
everychangeof theprogram.
The present integral problems result in short code. In more challenging en-
gineering problems, the code quickly grows to hundreds and thousands of lines.
Without abstractions, in terms of general algorithms in general reusable functions,
the complexity of the program grows so fast that it will be extremely difficult to
makesure that theprogramworksproperly.
Another advantage of packagingmathematical algorithms in functions, is that a
functioncanbereusedbyanyonetosolveaproblembyjustcallingthefunctionwith
a proper set of arguments. Understanding the function’s inner details is strictly not
necessary to compute a new integral. Similarly, you can find libraries of functions
on the Internet and use these functions to solve your problems without specific
knowledgeofeverymathematicaldetail in the functions.
This desirable feature has its downside, of course: the user of a function
may misuse it, and the function may contain programming errors and lead to
wrong answers. Testing the output of downloaded functions is therefore extremely
importantbefore relyingon the results.
6.3 TheCompositeMidpointMethod
The Idea Rather than approximating the area undera curveby trapezoids,we can
useplainrectangles. Itmaysoundlessaccurate tousehorizontal linesandnotskew
lines following the function to be integrated, but an integration method based on
rectangles (the midpoint method) is in fact slightly more accurate than the one
based on trapezoids! In the midpoint method, we construct a rectangle for every
sub-interval where the height equals the integrand f at the midpoint of the sub-
interval.
For the sake of comparison, we may repeat the hand calculation of ∫1
0 v(t)dt
in (6.7),but this time with themidpointmethod.With four rectangles (Fig.6.3)and
thesamesub-intervalsthatweusedwith the trapezoidalmethod,[0,0.2), [0.2,0.6),
[0.6,0.8), and [0.8,1.0],we get
∫ 1
0 v(t)dt≈h1v ( 0+0.2
2 )
+h2v ( 0.2+0.6
2 )
+h3v ( 0.6+0.8
2 )
+h4v ( 0.8+1.0
2 )
, (6.18)
whereh1,h2,h3, andh4 are the widths of the sub-intervals, used previously with
the trapezoidalmethodanddefined in (6.10)–(6.13).
Withv(t)= 3t2et3, the approximationbecomes 1.632. Compared with the true
answer (1.718), this is about 5% too small, but it is better than what we got with
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