<jupyter_start><jupyter_text>Demo: BasisChebyshev This script provides a few examples on using the BasisChebyshev class. Importing modules<jupyter_code>from demos.setup import demo, np, plt
from compecon import BasisChebyshev
%matplotlib inline<jupyter_output>Vendor:  Continuum Analytics, Inc.
Package: mkl
Message: trial mode expires in 26 days<jupyter_text>EXAMPLE 1:Using BasisChebyshev to interpolate a 1-D function with a Chebyshev basis**PROBLEM:** Interpolate the function $y = f(x) = 1 + sin(2x)$ on the domain $[0,\pi]$, using 5 Gaussian nodes. First, create the function f<jupyter_code>f = lambda x: 1 + np.sin(2 * x)<jupyter_output><empty_output><jupyter_text>and the Chebyshev basis B. If the type of nodes is unspecified, Gaussian is computed by default<jupyter_code>n, a, b = 5, 0, np.pi
B = BasisChebyshev(n, a, b, f=f)<jupyter_output><empty_output><jupyter_text>The basis nodes are:<jupyter_code>xnodes = B.nodes
print(xnodes)<jupyter_output>[[ 0.0769  0.6475  1.5708  2.4941  3.0647]]<jupyter_text>We can also plot the Chebyshev basis functions:<jupyter_code>B.plot()<jupyter_output><empty_output><jupyter_text>Fitting a function: set the interpolation coefficients c:<jupyter_code># c = np.mat(np.linalg.solve(B.Phi(), f(xnodes)))  
# notice that Phi is a dictionary, the matrix is indexed by 0<jupyter_output><empty_output><jupyter_text>Next plot the function f and its approximation. To evaluate the function defined by the basis B and coefficients c at values x we call the basis directly.<jupyter_code>x = np.linspace(a, b, 121)
f_approx = B(x)
f_true = f(x)<jupyter_output><empty_output><jupyter_text>We also plot the residuals, showing the residuals at the interpolating nodes (zero by construction)<jupyter_code>plt.figure()
demo.subplot(2, 1, 1, 'Chebyshev approximation with 5 nodes', '', '$f(x)$', [a, b])
plt.plot(x, np.c_[f_true, f_approx])
plt.legend(['$f = 1 + sin(2x)$', 'approx.'], loc=3)

demo.subplot(2, 1, 2, '', '$x$', 'Residuals', [a, b])
plt.plot(x.T, f_approx - f_true)<jupyter_output><empty_output><jupyter_text>Adjusting the number of nodes: to increase accuracy, we make a new basis with 25 nodes.<jupyter_code>B = BasisChebyshev(25, a, b, f=f)
plt.figure()
demo.subplot(2, 1, 1, 'Chebyshev approximation with 25 nodes', '', '$f(x)$', [a, b])
plt.plot(x, np.c_[f_true, B(x)])
plt.legend(['$f = 1 + sin(2x)$', 'approx.'], loc=3)

demo.subplot(2, 1, 2, '', '$x$', 'Residuals', [a, b])
plt.plot(x.T, B(x) - f_true)<jupyter_output><empty_output><jupyter_text>* With previous basis, now compute derivative.<jupyter_code>df = lambda x: 2 * np.cos(2 * x)
plt.figure()
demo.subplot(2, 1, 1, 'Approximating the derivative, 25 nodes', '', "$f'(x)$", [a, b])
plt.plot(x, np.c_[df(x), B(x, 1)])  # notice the 1 in B(x, 1) to compute first derivative
plt.legend(['df/dx = 2cos(2x)', 'approx.'], loc=3)

demo.subplot(2, 1, 2, '', '$x$', 'Residuals', [a, b])
plt.plot(x, B(x, 1) - df(x))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15643026806922294, 'fraction_numerical': 0.02816423481506617, 'mean_word_length': 6.069544364508393, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '381527', 'license': 'MIT', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1134, 'n_words': 317, 'path': 'notebooks/old notebooks/dem01_basis.ipynb', 'repo_name': 'snowdj/CompEcon-python', 'size': '2947'}