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

Page - 61 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Image of the Page - 61 -

Image of the Page - 61 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text of the Page - 61 -

3.2 TheCompositeTrapezoidalRule 61 alternative is the essenceof thepowerofmathematics,while thefirst alternative is the sourceofmuchconfusionaboutmathematics! Implementationwithfunctions For the integral Rb a f.x/dx computedbythe for- mula (3.17)wewant thecorrespondingPythonfunctiontrapezoid to takeanyf , a,b, andnas inputand return theapproximation to the integral. Wewrite a Python function trapezoidal in a file trapezoidal.pyas close as possible to the formula (3.17), making sure variable names correspond to the mathematicalnotation: def trapezoidal(f, a, b, n): h = float(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 Solving our specific problem in a session Just having the trapezoidal func- tion as the only content of a file trapezoidal.py automaticallymakes that file amodule thatwecan import and test in an interactivesession: >>> from trapezoidal import trapezoidal >>> from math import exp >>> v = lambda t: 3*(t**2)*exp(t**3) >>> n = 4 >>> numerical = trapezoidal(v, 0, 1, n) >>> numerical 1.9227167504675762 Letuscompute theexactexpressionand theerror in theapproximation: >>> V = lambda t: exp(t**3) >>> exact = V(1) - V(0) >>> exact - numerical -0.20443492200853108 Is this errorconvincing?Wecan trya largern: >>> numerical = trapezoidal(v, 0, 1, n=400) >>> exact - numerical -2.1236490512777095e-05 Fortunately,manymore trapezoidsgiveamuchsmaller error. Solving our specific problem in a program Instead of computing our special problem in an interactive session, we can do it in a program. As always, a chunk of codedoingaparticular thing is best isolated as a functioneven ifwedonot see anyfuture reason tocall the functionseveral timesandeven ifwehavenoneed for arguments to parameterizewhat goes on inside the function. In the present case,
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2016
Language
English
License
CC BY-NC 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
248
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