Make a calculus calculator class.
Given a function f(x) defined on a domain [a, b], the purpose of many mathematical exercises is to sketch the function curve y = f(x), compute the derivative f ′ (x), find local and global extreme points, and compute the integral f
b
a
f(x)dx. Make a class CalculusCalculator which can perform all these actions for any function f(x) using numerical differentiation and integration, and the method explained in Exercise 7.36 or 7.37 for finding extrema.
Here is an interactive session with the class where we analyze f(x) = x
2
e
−0.2x
sin(2πx) on [0, 6] with a grid (set of x coordinates) of 700 points:
>>> from CalculusCalculator import *
>>> def f(x):
... return x**2*exp(-0.2*x)*sin(2*pi*x)...
>>> c = CalculusCalculator(f, 0, 6, resolution=700)
>>> c.plot() # plot f
>>> c.plot_derivative() # plot f’
>>> c.extreme_points()
All minima: 0.8052, 1.7736, 2.7636, 3.7584, 4.7556, 5.754, 0
All maxima: 0.3624, 1.284, 2.2668, 3.2604, 4.2564, 5.2548, 6
Global minimum: 5.754
Global maximum: 5.2548
>>> c.integral
-1.7353776102348935
>>> c.df(2.51) # c.df(x) is the derivative of f
-24.056988888465636
>>> c.set_differentiation_method(Central4)
>>> c.df(2.51)
-24.056988832723189
>>> c.set_integration_method(Simpson) # more accurate integration
>>> c.integral
-1.7353857856973565
Design the class such that the above session can be carried out.
Hint: Use classes from the Diff and Integrator hierarchies (Chapters 9.2 and 9.3) for numerical differentiation and integration (with, e.g., Central2 and Trapezoidal as default methods for differentiation and integration). The method set_differentiation_method takes a subclass name in the Diff hierarchy as argument, and makes an attribute df that holds a subclass instance for computing derivatives. With set_integration_method we can similarily set the integration method as a subclass name in the Integrator hierarchy, and then compute the integral R b a f(x)dx and store the value in the attribute integral. The extreme_points method performs a print on a MinMax instance, which is stored as an attribute in the calculator class. Name of program file: CalculusCalculator.py.
Exercise 7.36
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.