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 - 178 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

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

Bild der Seite - 178 -

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

Text der Seite - 178 -

178 7 SolvingNonlinearAlgebraicEquations Implementation Given some Python implementation f(x) of our mathematical function, a straightforward implementation of the above algorithm that quits after findingoneroot, looks like x = linspace(0, 4, 10001) y = f(x) root = None # Initialization for i in range(len(x)-1): if y[i]*y[i+1] < 0: root = x[i] - (x[i+1] - x[i])/(y[i+1] - y[i])*y[i] break # Jump out of loop elif y[i] == 0: root = x[i] break # Jump out of loop if root is None: print(’Could not find any root in [{:g}, {:g}]’.format(x[0], x[-1])) else: print(’Find (the first) root as x={:.17f}’.format(root)) (See thefilebrute_force_root_finder_flat.py.) Notetheniceuseofsettingroot toNone:wecansimplytestif root is None to see if we found a root and overwrote theNone value, or if we did not find any rootamongthe testedpoints. Running this programwith somefunction, sayf(x)= e−x2 cos(4x) (whichhas a solution at x = π8), gives the root 0.39269910538048097,which has an error of 2.4×10−8. Increasingthenumberofpointswitha factorof tengivesa rootwithan errorof2.9×10−10. After such a quick “flat” implementation of an algorithm, we should always try tooffer thealgorithmasaPythonfunction,applicable toaswideaproblemdomain aspossible.Thefunctionshould takef andanassociated interval [a,b]as input,as well asa numberofpoints (n), andreturna list of all the roots in [a,b].Here isour candidatefora goodimplementationof thebrute force rootfindingalgorithm: def brute_force_root_finder(f, a, b, n): from numpy import linspace x = linspace(a, b, n) y = f(x) roots = [] for i in range(n-1): if y[i]*y[i+1] < 0: root = x[i] - (x[i+1] - x[i])/(y[i+1] - y[i])*y[i] roots.append(root) elif y[i] == 0: root = x[i] roots.append(root) return roots (See thefilebrute_force_root_finder_function.py.) This timeweuseanothereleganttechniqueto indicate if rootswerefoundornot: roots is anempty list if the rootfindingwasunsuccessful,otherwise it containsall the roots.Applicationof the function to thepreviousexamplecanbecodedas
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