This demo illustrates how to use CompEcon Toolbox routines to construct and operate with an approximant for a function defined on an interval of the real line.

In particular, we construct an approximant for $f(x)=\exp(-x)$ on the interval $[-1,1]$. The function used in this illustration posseses a closed-form, which will allow us to measure approximation error precisely. Of course, in practical applications, the function to be approximated will not possess a known closed-form.

In order to carry out the exercise, one must first code the function to be approximated at arbitrary points. Let's begin:

In [1]:

```
from demos.setup import np, plt, demofigure
from compecon import BasisChebyshev, BasisSpline, nodeunif
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
%matplotlib inline
```

Function to be approximated

In [2]:

```
def f(x): return np.exp(-x)
```

Set the endpoints of approximation interval:

In [3]:

```
a = -1 # left endpoint
b = 1 # right endpoint
```

Choose an approximation scheme.

In this case, let us use an order 10 Chebychev approximation scheme:

In [4]:

```
n = 10 # order of approximation
F = BasisChebyshev(n, a, b, f=f)
```

One may now evaluate the approximant at any point x calling the basis:

In [5]:

```
x = 0
y = F(x)
print('\nThe approximate value of exp(-x) at x=0 is {:.15f}'.format(y))
print("The 'exact' value of exp(-x) at x=0 is {:.15f}".format(1))
```

... one may also evaluate the approximant's first and second derivatives at x:

In [6]:

```
d1 = F(x, 1)
d2 = F(x, 2)
print('\nThe approximate first derivative of exp(-x) at x=0 is {:.15f}'.format(d1))
print("The 'exact' first derivative of exp(-x) at x=0 is {:.15f}".format(-1))
print('\nThe approximate second derivative of exp(-x) at x=0 is {:.15f}'.format(d2))
print("The 'exact' second derivative of exp(-x) at x=0 is {:.15f}".format(1))
```

... and one may even evaluate the approximant's definite integral between the left endpoint a and x:

In [7]:

```
int = F(x, -1)
print('\nThe approximate integral of exp(-x) between x=-1 and x=0 is {:.15f}'.format(int))
print("The 'exact' integral of exp(-x) between x=-1 and x=0 is {:.15f}".format(np.exp(1) - 1))
```

In [8]:

```
ngrid = 5001 # number of grid nodes
xgrid = np.linspace(a, b, ngrid) # generate refined grid for plotting
yapp = F(xgrid) # approximant values at grid nodes
yact = f(xgrid) # actual function values at grid points
```

Plot approximation error

In [9]:

```
demofigure('Chebychev Approximation Error for exp(-x)', 'x', 'Error')
plt.plot(xgrid, yapp - yact)
plt.plot(xgrid, np.zeros_like(xgrid), 'k--', linewidth=2)
```

Out[9]:

The plot indicates that an order 10 Chebychev approximation scheme, produces approximation errors no bigger in magnitude than 6x10^-10. The approximation error exhibits the "Chebychev equioscillation property", oscilating relatively uniformly throughout the approximation domain.

This commonly occurs when function being approximated is very smooth, as is the case here but should not be expected when the function is not smooth. Further notice how the approximation error is exactly 0 at the approximation nodes --- which is true by contruction.

In [10]:

```
n = 21 # order of approximation
S = BasisSpline(n, a, b, f=f) # define basis
yapp = S(xgrid) # approximant values at grid nodes
# plot approximation error
demofigure('Cubic Spline Approximation Error for exp(-x)', 'x', 'Error')
plt.plot(xgrid, yapp - yact)
plt.plot(xgrid, np.zeros_like(xgrid), 'k--', linewidth=2)
```

Out[10]:

In [11]:

```
n = 31 # order of approximation
L = BasisSpline(n, a, b, k=1, f=f) # define basis functions
yapp = L(xgrid) # fitted values at grid nodes
# plot approximation error
demofigure('Linear Spline Approximation Error for exp(-x)', 'x', 'Error')
plt.plot(xgrid, yapp - yact)
plt.plot(xgrid, np.zeros_like(xgrid), 'k--', linewidth=2)
```

Out[11]: