CompEcon Toolbox:
DemApp09
Linear Spline Approximation
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

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 BasisSpline, demo
In [ ]:
def f(x):
    return 50 - np.cos(x**2 / 8) * (x - np.pi + .5)**2
In [ ]:
xmin, xmax = 0.0, 1.5*np.pi
off = 0.05
xlims = [xmin - off, xmax + off]
n = 401
x = np.linspace(xmin, xmax, n)
y = f(x)
ymin, ymax = y.min(), y.max()
ywid = ymax - ymin
ylims = [ymin - 0.5*ywid, ymax + 0.1*ywid]
In [ ]:
figs = []
for nnode in 3, 5, 9:
    F = BasisSpline(nnode, xmin, xmax, k=1, f=f)
    xnodes = F.nodes[0]

    xx = np.r_[x, xnodes]
    xx.sort()

    fig, ax= plt.subplots(figsize=[10,5])
    ax.set(title = f'Linear Spline with {nnode} nodes',
           #xlabel='', ylabel='', 
           xlim=xlims, ylim=ylims)
    
    ax.plot(xx, f(xx), lw=3)  # true function
    ax.plot(xx, F(xx), 'r', lw=1) # approximation
    ax.set_yticks(ylims, ['', ''])
    xe = ['$x_{%d}$' % k for k in range(nnode)]
    xe[0], xe[-1] = '$x_0=a$', '$x_{%d}=b$' % (nnode-1)
    ax.set_xticks(xnodes)
    ax.set_xticklabels(xe, fontsize=18)
    for i, xi in enumerate(xnodes):
        ax.vlines(xi, ylims[0], F(xi), 'gray','--')
        
    figs.append(fig)    

Save all figures to disc

In [ ]:
#demo.savefig(figs, name='demapp09')