Find local and global extrema of a function. Extreme points of a function f(x) are normally found by solving f ′ (x) = 0. A much simpler method is to evaluate f(x) for a set of discrete points in the...


Find local and global extrema of a function. Extreme points of a function f(x) are normally found by solving f ′ (x) = 0. A much simpler method is to evaluate f(x) for a set of discrete points in the interval [a, b] and look for local minima and maxima among these points. We work with n equally spaced points a = x0
1
<>n−1
= b, xi
= a + ih, h = (b − a)/(n − 1).


1. First we find all local extreme points in the interior of the domain. Local minima are recognized by


f(xi−1) > f(xi)
i+1), i = 1, . . . , n − 2 .


Similarly, at a local maximum point xi
we have


f(xi−1)
i) > f(xi+1), i = 1, . . . , n − 2 .


We let Pmin
be the set of x values for local minima and Fmin
the set of the corresponding f(x) values at these minimum points. Two sets Pmax
and Fmax
are defined correspondingly, containing the maximum points and their values.


2. The boundary points x = a and x = b are for algorithmic simplicity also defined as local extreme points: x = a is a local minimum if f(a)
1), and a local maximum otherwise. Similarly, x = b is a local minimum if f(b)
n−2), and a local maximum otherwise. The end points a and b and the corresponding function values must be added to the sets Pmin, Pmax, Fmin, Fmax.


3. The global maximum point is defined as the x value corresponding to the maximum value in Fmax. The global minimum point is the x value corresponding to the minimum value in Fmin.


Make a class MinMax with the following functionality:


ˆ The constructor takes f(x), a, b, and n as arguments, and calls


a method _find_extrema to compute the local and global extreme


points.


ˆ The method _find_extrema implements the algorithm above


for finding local and global extreme points, and stores the sets


Pmin, Pmax, Fmin, Fmax as list attributes in the (self) instance.


ˆ The method get_global_minimum returns the global minimum point


(x).


ˆ The method get_global_maximum returns the global maximum point


(x).


ˆ The method get_all_minima returns a list or array of all minimum


points.


ˆ The method get_all_maxima returns a list or array of all maximum


points.


ˆ The method __str__ returns a string where all the min/max points


are listed, plus the global extreme points.


Here is a sample code using class MinMax:


def f(x):


return x**2*exp(-0.2*x)*sin(2*pi*x)


m = MinMax(f, 0, 4)


print m


 The output becomes


All minima: 0.8056, 1.7736, 2.7632, 3.7584, 0


All maxima: 0.3616, 1.284, 2.2672, 3.2608, 4


Global minimum: 3.7584


Global maximum: 3.2608


Make sure that the program also works for functions without local extrema, e.g., linear functions f(x) = px + q. Name of program file: minmaxf.py.

Nov 23, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here