CompEcon Toolbox:
DemQua07
Illustrates integration using Trapezoidal rule
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-10

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 [ ]:
from numpy import poly1d, linspace
from compecon import qnwtrap, demo
import matplotlib.pyplot as plt
In [ ]:
n = 1001
xmin, xmax = -1, 1
xwid = xmax-xmin
x = linspace(xmin, xmax, n)
In [ ]:
f = poly1d([2.0, -1.0, 0.5, 5.0])
In [ ]:
def plot_trap(n):
    xi, wi = qnwtrap(n+1, xmin, xmax)
    
    fig, ax = plt.subplots()
    ax.fill_between(xi, f(xi), alpha=0.35, color='LightSkyBlue')
    ax.plot(x, f(x), linewidth=3, label=r'$f(x)$')
    ax.plot(xi, f(xi), color='Tab:Orange', linestyle='--', label=f'$\\tilde{{f}}_{n+1}(x)$')
    ax.vlines(xi, 0, f(xi),color='Tab:Orange', linestyle=':')
    ax.axhline(0,color='k',linewidth=2)
    
    xtl = [f'$x_{i}$' for i in range(n+1)]
    xtl[0] += '=a'
    xtl[n] += '=b'
    ax.set(
        xlim=[xmin-0.1, xmax+0.1],
        xticks=xi, 
        xticklabels=xtl,
        yticks=[0],
        yticklabels=['0'])
    ax.legend()
    return fig

figs = [plot_trap(n) for n in [2, 4, 8]]
#demo.savefig(figs, name='demqua07')