Use classes for computing inverse functions. Appendix A.1.10 describes a method and implementation for computing the inverse function of a given function. The purpose of the present exercise is to...


Use classes for computing inverse functions.


Appendix A.1.10 describes a method and implementation for computing the inverse function of a given function. The purpose of the present exercise is to improve the implementation in Appendix A.1.10 by introducing classes. This results in software that is more flexible with respect to the way we can specify the function to be inverted.


Implement the F and dFdx functions from Appendix A.1.10 as classes to avoid relying on global variables for h, xi, etc. Also introduce a class InverseFunction to run the complete algorithm and store the g array (from Appendix A.1.10) as an array attribute values. Here is a typical use of class InverseFunction:


>>> from InverseFunction import InverseFunction as I


>>> from scitools.std import *


>>> def f(x):


... return log(x)


...


>>> x = linspace(1, 5, 101)


>>> f_inv = I(f, x)


>>> plot(x, f(x), x, f_inv.values)


Check, in the constructor, that f is monotonically increasing or decreasing over the set of coordinates (x). Errors may occur in the computations because Newton’s method might divide by zero or diverge. Make sure sensible error messages are reported in those cases.


A __call__ method in class InverseFunction should evaluate the inverse function at an arbitrary point x. This is somewhat challenging since we only have the inverse function at discrete points along its curve. With aid of a function wrap2callable from scitools.std one can turn (x, y) points on a curve, stored in arrays x and y, into a (piecewise) continuous Python function q(x) by:


q = wrap2callable((x, y))


In a sense, the wrap2callable call draws lines between the discrete points to form the resulting continuous function. Use wrap2callable to make the __call__ method evaluate the inverse function at any point in the interval from x[0] to x[-1]. Name of program file: InverseFunction.py.

Nov 22, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here