<jupyter_start><jupyter_text>Taylor Approximations**<NAME>, PhD**This demo is based on the original Matlab demo accompanying the  Computational Economics and Finance 2001 textbook by <NAME> and <NAME>.Original (Matlab) CompEcon file: **demmath01.m**Running this file requires the Python version of CompEcon. This can be installed with pip by running    !pip install compecon --upgradeLast updated: 2021-Oct-01<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from compecon.tools import nodeunif
from matplotlib import cm<jupyter_output><empty_output><jupyter_text>Univariate Taylor approximation For the function $$f(x) = (x+1)\exp(2x) $$the first-order Taylor approximation is$$\hat{f}(x) = 1+3x $$and the second-order approximation is $$\hat{f}(x) = 1+3x + 8x^2 $$<jupyter_code>x = np.linspace(-1, 1, 100)
y = (x + 1) * np.exp(2 * x)
y1 = 1 + 3 * x
y2 = 1 + 3 * x + 8 * x ** 2

plt.figure(figsize=[8, 5])
plt.plot(x, y, 'k', linewidth=3, label='Function')
plt.plot(x, y1, 'b', linewidth=3, label='1st order approximation')
plt.plot(x, y2, 'r', linewidth=3, label='2nd order approximation')
plt.legend()
plt.xticks([-1, 0, 1])
plt.show()<jupyter_output><empty_output><jupyter_text>Bivariate Taylor approximation For the function $$g(x) = x_1^2\exp(-x_2) $$the first-order Taylor approximation around $(1,0)$ is$$\hat{g}(x) = 2x_1 -x_2 - 1$$and the second-order approximation is $$\hat{g}(x) = x_1^2 - 2x_1 x_2 +0.5x_2^2 + x_2$$<jupyter_code>nplot = [101, 101]
a = [0, -1]
b = [2, 1]
x1, x2 = nodeunif(nplot, a, b)
x1.shape = nplot
x2.shape = nplot

y = np.exp(x2) * x1 ** 2
y1 = 2 * x1 - x2 - 1
y2 = x1 ** 2 - 2 * x1 * x2 + 0.5 * x2 ** 2 + x2

def newPlot(title, Y, k):
    ax = fig.add_subplot(1, 3, k, projection='3d')
    ax.plot_surface(x1, x2, Y, rstride=1, cstride=1, cmap=cm.coolwarm,
                    linewidth=0, antialiased=False)
    ax.set_xlabel('$x_1$')
    ax.set_ylabel('$x_2$')
    ax.set_zlabel('$y$')
    plt.title(title)

fig = plt.figure(figsize=[15, 6])
newPlot('Original function', y, 1)
newPlot('First-order approximation error', y1-y, 2)
newPlot('Second-order approximation error', y2-y, 3)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16597701149425287, 'fraction_numerical': 0.05379310344827586, 'mean_word_length': 4.593830334190232, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '44814', 'license': 'MIT', 'n_tokens_mistral': 935, 'n_tokens_neox': 859, 'n_words': 233, 'path': '_build/jupyter_execute/notebooks/math/01 Taylor Approximations.ipynb', 'repo_name': 'randall-romero/CompEcon-python', 'size': '2204'}