CompEcon Toolbox:
DemApp08
Compute function inverse via collocation
Randall Romero Aguilar, PhD

This demo is based on the original Matlab demo accompanying the Computational Economics and Finance 2001 textbook by Mario Miranda and Paul Fackler.

Last updated: 2020-Sep-09

About

The function is defined implicitly by \begin{equation*} f(x)^{-2} + f(x)^{-5} - 2x = 0 \end{equation*}

Initial tasks

In [ ]:
if 'google.colab' in str(get_ipython()):
    print("This notebook is running on Google Colab. Installing the compecon package.")
    !pip install compecon
In [ ]:
import numpy as np
import matplotlib.pyplot as plt
from compecon import BasisChebyshev, NLP, demo

Approximation structure

In [ ]:
n, a, b = 31, 1, 5
F = BasisChebyshev(n, a, b, y=5*np.ones(31), labels=['f(x)'])  # define basis functions
x = F.nodes                  # compute standard nodes
In [ ]:
F.plot()

Residual function

In [ ]:
def resid(c):
    F.c = c  # update basis coefficients
    y = F(x) # interpolate at basis nodes x
    return y ** -2 + y ** -5 - 2 * x

Compute function inverse

In [ ]:
c0 = np.zeros(n)  # set initial guess for coeffs
c0[0] = 0.2
problem = NLP(resid)
F.c = problem.broyden(c0)  # compute coeff by Broyden's method

Plot function inverse

In [ ]:
n = 1000
x = np.linspace(a, b, n)
r = resid(F.c)

fig1, ax = plt.subplots()
ax.set(title='Implicit Function', 
       xlabel='x',
       ylabel='f(x)')
ax.plot(x, F(x));

Plot residual

In [ ]:
fig2, ax = plt.subplots()
ax.set(title='Functional Equation Residual',
         xlabel='x',
         ylabel='Residual')
ax.hlines(0, a, b, 'k', '--')
ax.plot(x, r);

Save all figures to disc

In [ ]:
#demo.savefig([fig1, fig2], name='demapp08')