Illustrates integration using Trapezoidal rule

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.

Original (Matlab) CompEcon file: demqua07.m

Running this file requires the Python version of CompEcon. This can be installed with pip by running

!pip install compecon --upgrade

Last updated: 2022-Oct-23


Initial tasks

from numpy import poly1d, linspace
from compecon import qnwtrap
import matplotlib.pyplot as plt
n = 1001
xmin, xmax = -1, 1
xwid = xmax-xmin
x = linspace(xmin, xmax, n)
f = poly1d([2.0, -1.0, 0.5, 5.0])
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]]
../../_images/07 Illustrates integration using Trapezoidal rule_5_0.png ../../_images/07 Illustrates integration using Trapezoidal rule_5_1.png ../../_images/07 Illustrates integration using Trapezoidal rule_5_2.png