Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Approximate a function with a polynomial
ResourceFunction["PolynomialApproximation"][f,{x,xmin,xmax},n, "type"] finds an nth order polynomial of a given type that approximates the function f for x∈[xmin,xmax]. |
"Taylor" | Taylor expansion around (xmin+xmax)/2 |
"Equispaced" | Polynomial going through n+1 points equispaced between xmin and xmax |
"Chebyshev" | Polynomial going through n+1 Chebyshev nodes |
"Remez" | Iteratively optimize the nodes starting with Chebyshev nodes |
{“Remez”, m} | Remez with m iterations |
Find the second-order Taylor polynomial approximating the sine function:
In[1]:= |
Out[1]= |
Compare with the original function:
In[2]:= |
Out[2]= |
Use equispaced points to get a better approximation:
In[3]:= |
Out[5]= |
Use Chebyshev points to get an even better approximation:
In[6]:= |
Out[8]= |
The polynomial produced by the Remez method yields a nearly-optimal approximation:
In[9]:= |
Out[11]= |
Perform 6 iterations for the Remez method, and then compare the error:
In[12]:= |
Out[14]= |
Find a function that can approximate the cosine function for angles up to :
In[15]:= |
Out[16]= |
Calculate several orders and compare the maximum absolute error:
In[17]:= |
Out[18]= |
The Remez method does not always converge:
In[19]:= |
Out[21]= |
Higher order Remez might not converge to a good solution:
In[22]:= |
Out[24]= |
Higher order Remez might be prone to numerical errors:
In[25]:= |
Out[25]= |
Functions that are not continuous can not be approximated very well:
In[26]:= |
Out[28]= |
Find a function that approximates a normal distribution:
In[29]:= |
Out[31]= |
Find the maximum error:
In[32]:= |
Out[32]= |
Compare that to the Chebyshev case:
In[33]:= |
Out[34]= |
In[35]:= |
Out[35]= |
Wolfram Language 13.2 (December 2022) or above
This work is licensed under a Creative Commons Attribution 4.0 International License