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

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

Image of the Page - 138 -

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

Text of the Page - 138 -

138 6 ComputingIntegralsandTestingCode where0.5*f(a) + 0.5*f(b) isusedinaninitializationofresult,beforeadding all the function evaluationsf(xi) in the loop. After the loop, the only remaining thing, is to multiply by h. In this second alternative, there are also a few other differences to note. Therange function is called with two parameters only, giving the (default) step of 1. In the loop, instead of computing x = a + i*h prior to calling f(x), we combine this into f(a + i*h), which first computes a + i*h and then makes the call to f. Also, the compact operators += and *= have been used. Using the General Implementation in a Session Having the trapezoidal function as the only content of a file trapezoidal.py automatically makes that filea module thatwe can importand test inan interactivesession: In [1]: from trapezoidal import trapezoidal In [2]: from math import exp In [3]: v = lambda t: 3*(t**2)*exp(t**3) In [4]: n = 4 In [5]: numerical = trapezoidal(v, 0, 1, n) In [6]: numerical Out[6]: 1.9227167504675762 Let us compute the exact expression and the error in the approximation. UsingV for theanti-derivative,weget: In [7]: V = lambda t: exp(t**3) In [8]: exact = V(1) - V(0) In [9]: abs(exact - numerical) # absolute value of error Out[9]: 0.20443492200853108 Since the sign of the error is irrelevant, we find the absolute value of the error. So, is thiserrorconvincing?We can trya largern: In [10]: numerical = trapezoidal(v, 0, 1, n=400) In [11]: abs(exact - numerical) Out[11]: 2.1236490512777095e-05 Fortunately,manymore trapezoidsgivea muchsmaller error. Using the General Implementation in a Program Instead of computing our integral in an interactive session, we can do it in a program. In that program, we need the (general) function definition of trapezoidal, and we need some code that specifies our particular integrand, as well as the other arguments required for callingtrapezoidal. This code might be placed in a main program. However, it couldalsobeplacedinafunctionthat iscalledfromthemainprogram.This iswhat we will do. A chunk of code doing a particular thing is always best isolated as a function, even if we do not see any future reason to call the functionseveral times, andeven if we haveno need forarguments to parameterizewhat goeson inside the
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