<jupyter_start><jupyter_text>One-dimensional function interpolation<jupyter_code>import minterpy as mp
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Setup Create a test function:<jupyter_code>fx = lambda x: x * np.sin(12*x)
x = np.linspace(-1, 1, 300)
y = fx(x)<jupyter_output><empty_output><jupyter_text>The plot:<jupyter_code>plt.plot(x, y);<jupyter_output><empty_output><jupyter_text>Create an interpolantYou can create an interpolant using the [interpolate](../api/interpolation/interpolate.rst) function by specifying:- The function to interpolate- The number of dimensions- The degree of the polynomial interpolant<jupyter_code>fx_interp = mp.interpolate(fx, spatial_dimension=1, poly_degree=4)<jupyter_output><empty_output><jupyter_text>The interpolant can be evaluated on the test points:<jupyter_code>y_interp = fx_interp(x)
plt.plot(x, y, label="true function");
plt.plot(x, y_interp, label="interpolant");
plt.legend();<jupyter_output><empty_output><jupyter_text>That shows that degree 4 polynomial is not accurate enough to approximate the true function.The infinity norm provides a measure of the greatest error of the interpolant over the whole domain.The norm is defined as:$$||f(x) - \hat{f}(x)||_\infty = \sup_{-1\leq x \leq 1} |f(x) - \hat{f}(x)|$$The infinity norm of the degree 4 polynomial is:<jupyter_code>np.max(np.abs(y - y_interp))<jupyter_output><empty_output><jupyter_text>hardly a numerical convergence.  Interpolants of increasing degrees Let's create interpolant of increasing degrees and evaluate them on the test function:<jupyter_code>degrees = [4, 8, 16, 32, 64]
y_interp = np.empty((x.size, len(degrees)))
for i,p in enumerate(degrees):
    fx_interp = mp.interpolate(fx, spatial_dimension=1, poly_degree=p)
    y_interp[:,i] = fx_interp(x)
plt.plot(x, y);
for i,p in enumerate(degrees):
    plt.plot(x, y_interp[:,i], label="degree = {}".format(p));
plt.legend();<jupyter_output><empty_output><jupyter_text>The numerical convergence is shown below:<jupyter_code>errors = np.max(np.abs(y[:,np.newaxis] - y_interp),axis=0)
plt.plot(degrees,errors,'-x');
plt.yscale("log");<jupyter_output><empty_output><jupyter_text>The absolute error of the degree 64 polynomials in the domain is shown below:<jupyter_code>plt.plot(x, np.abs(y - y_interp[:,-1]));
plt.ylim(1e-18,1);
plt.yscale("log");<jupyter_output><empty_output><jupyter_text>The seemingly random behavior of the (very small) absolute error indicates that machine precision has been reached. Compare it with the absolute error of degree 4 polynomials:<jupyter_code>plt.plot(x, np.abs(y - y_interp[:,0]));
plt.yscale("log");<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1307237813884786, 'fraction_numerical': 0.011816838995568686, 'mean_word_length': 7.335384615384616, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, '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': '532320', 'license': 'MIT', 'n_tokens_mistral': 1004, 'n_tokens_neox': 953, 'n_words': 232, 'path': 'docs/getting-started/one-dimensional-function-interpolation.ipynb', 'repo_name': 'karanprime/minterpy', 'size': '2708'}