.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_multivariate_piecewise_polynomial_interpolation.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_multivariate_piecewise_polynomial_interpolation.py: Multivariate Piecewise Polynomial Interpolation ----------------------------------------------- Piecewise polynomial interpolation can efficiently approximation functions with low smoothness, e.g. piecewise continuous functions. Here we will use piecewise quadratic polynomials to interpolate the discontinuous Genz benchmark. The function interpolates functions on tensor-products of 1D equidistant grids. The number of points in the grid doubles with each level. Here levels specifies the level of each 1D grid .. GENERATED FROM PYTHON SOURCE LINES 12-30 .. code-block:: default import numpy as np from functools import partial from pyapprox.benchmarks import setup_benchmark from pyapprox.analysis import visualize from pyapprox import surrogates benchmark = setup_benchmark("genz", test_name="discontinuous", nvars=2) levels = [5, 5] interp_fun = partial( surrogates.tensor_product_piecewise_polynomial_interpolation, levels=levels, fun=benchmark.fun, basis_type="quadratic") X, Y, Z = visualize.get_meshgrid_function_data_from_variable( interp_fun, benchmark.variable, 50) fig, axs = visualize.plt.subplots(1, 2, figsize=(2*8, 6)) axs[0].contourf(X, Y, Z, levels=np.linspace(Z.min(), Z.max(), 20)) .. image-sg:: /auto_examples/images/sphx_glr_plot_multivariate_piecewise_polynomial_interpolation_001.png :alt: plot multivariate piecewise polynomial interpolation :srcset: /auto_examples/images/sphx_glr_plot_multivariate_piecewise_polynomial_interpolation_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 31-32 To plot the difference between the interpolant and the target function use .. GENERATED FROM PYTHON SOURCE LINES 32-36 .. code-block:: default X, Y, Z = visualize.get_meshgrid_function_data_from_variable( lambda x: interp_fun(x)-benchmark.fun(x), benchmark.variable, 50) axs[1].contourf(X, Y, Z, levels=np.linspace(Z.min(), Z.max(), 20)) visualize.plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.093 seconds) .. _sphx_glr_download_auto_examples_plot_multivariate_piecewise_polynomial_interpolation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_multivariate_piecewise_polynomial_interpolation.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_multivariate_piecewise_polynomial_interpolation.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_