Interpolate a function at the Chebyshev points of the first kind. Returns the Chebyshev series that interpolates `func` at the Chebyshev points of the first kind in the interval [-1, 1]. The interpolating series tends to a minmax approximation to `func` with increasing `deg` if the
(func, deg, args=())
| 1777 | |
| 1778 | |
| 1779 | def chebinterpolate(func, deg, args=()): |
| 1780 | """Interpolate a function at the Chebyshev points of the first kind. |
| 1781 | |
| 1782 | Returns the Chebyshev series that interpolates `func` at the Chebyshev |
| 1783 | points of the first kind in the interval [-1, 1]. The interpolating |
| 1784 | series tends to a minmax approximation to `func` with increasing `deg` |
| 1785 | if the function is continuous in the interval. |
| 1786 | |
| 1787 | Parameters |
| 1788 | ---------- |
| 1789 | func : function |
| 1790 | The function to be approximated. It must be a function of a single |
| 1791 | variable of the form ``f(x, a, b, c...)``, where ``a, b, c...`` are |
| 1792 | extra arguments passed in the `args` parameter. |
| 1793 | deg : int |
| 1794 | Degree of the interpolating polynomial |
| 1795 | args : tuple, optional |
| 1796 | Extra arguments to be used in the function call. Default is no extra |
| 1797 | arguments. |
| 1798 | |
| 1799 | Returns |
| 1800 | ------- |
| 1801 | coef : ndarray, shape (deg + 1,) |
| 1802 | Chebyshev coefficients of the interpolating series ordered from low to |
| 1803 | high. |
| 1804 | |
| 1805 | Examples |
| 1806 | -------- |
| 1807 | >>> import numpy.polynomial.chebyshev as C |
| 1808 | >>> C.chebinterpolate(lambda x: np.tanh(x) + 0.5, 8) |
| 1809 | array([ 5.00000000e-01, 8.11675684e-01, -9.86864911e-17, |
| 1810 | -5.42457905e-02, -2.71387850e-16, 4.51658839e-03, |
| 1811 | 2.46716228e-17, -3.79694221e-04, -3.26899002e-16]) |
| 1812 | |
| 1813 | Notes |
| 1814 | ----- |
| 1815 | The Chebyshev polynomials used in the interpolation are orthogonal when |
| 1816 | sampled at the Chebyshev points of the first kind. If it is desired to |
| 1817 | constrain some of the coefficients they can simply be set to the desired |
| 1818 | value after the interpolation, no new interpolation or fit is needed. This |
| 1819 | is especially useful if it is known apriori that some of coefficients are |
| 1820 | zero. For instance, if the function is even then the coefficients of the |
| 1821 | terms of odd degree in the result can be set to zero. |
| 1822 | |
| 1823 | """ |
| 1824 | deg = np.asarray(deg) |
| 1825 | |
| 1826 | # check arguments. |
| 1827 | if deg.ndim > 0 or deg.dtype.kind not in 'iu' or deg.size == 0: |
| 1828 | raise TypeError("deg must be an int") |
| 1829 | if deg < 0: |
| 1830 | raise ValueError("expected deg >= 0") |
| 1831 | |
| 1832 | order = deg + 1 |
| 1833 | xcheb = chebpts1(order) |
| 1834 | yfunc = func(xcheb, *args) |
| 1835 | m = chebvander(xcheb, deg) |
| 1836 | c = np.dot(m.T, yfunc) |
no test coverage detected
searching dependent graphs…