Page - 180 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 180 -
Text of the Page - 180 -
180 7 SolvingNonlinearAlgebraicEquations
# Return x and y values
return [(x[i], y[i]) for i in minima], \
[(x[i], y[i]) for i in maxima]
Themaxandmin functionsare standardPythonfunctionsforfinding themaximum
andminimumelementofa listoranobject thatonecan iterateoverwitha for loop.
Anapplication tof(x)= e−x2 cos(4x) looks like
def demo():
from numpy import exp, cos
minima, maxima = brute_force_optimizer(
lambda x: exp(-x**2)*cos(4*x), 0, 4, 1001)
print(’Minima:\n’, minima)
print(’Maxima:\n’, maxima)
Runningtheprogramgives
Minima:
[(0.70000000000000007, -0.5772302750838405), (2.1520000000000001,
-0.0066704807422565023), (3.6600000000000001, -7.3338267339366542e-07)]
Maxima:
[(1.4159999999999999, 0.10965467991643564), (2.8999999999999999,
0.00012651823896373234), (0.0, 1.0)]
7.1.3 ModelProblemforAlgebraicEquations
We shall consider the very simple problem of finding the square root of 9. That is,
we want to solve x2 = 9, but will (for simplicity) seek only the positive solution.
Knowingthesolutionbeforehand,allowsus toeasily investigatehowthenumerical
method (and the implementation of it) performs in the search for a solution. The
f(x) function thatcorrespondsto the equationx2 =9 is
f(x)=x2−9 .
Ourintervalof interest forsolutionswillbe [0,1000](theupperlimithereischosen
somewhatarbitrarily).
In the following,wewillpresent severalefficientandaccuratemethodsfor solv-
ing nonlinear algebraic equations, both single equation and systems of equations.
The methods all have in common that they search for approximate solutions. The
methodsdiffer,however, in the way theyperformthesearch for solutions.The idea
for the search influences the efficiency of the search and the reliability of actually
findingasolution.Forexample,Newton’smethodisveryfast,butnotreliable,while
thebisectionmethod is the slowest, butabsolutely reliable.Nomethod isbest at all
problems,so weneeddifferentmethodsfordifferentproblems.
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