Let \(f(x)\) be a function that is \(n+1\) times differentiable on an interval \(I\) containing \(a\) and let \(P_n(x)\) be the \(n\)th degree Taylor polynomial for \(f(x)\) about \(a\). Then, there exists a number \(c\) between \(a\) and \(x\) such that: \[\begin{equation}
f(x)=P_n(x)+R_n(x),
\end{equation}\] where the remainder \(R_n(x)\) is given by: \[\begin{equation}
R_n(x)=\frac{f^{(n+1)}(c)}{(n+1)!}(x-a)^{n+1}.
\end{equation}\]
Define the function to be approximated
Code
import torchimport mathimport matplotlib.pyplot as pltimport numpy as np# Define the sine function to be approximateddef f(x):return torch.sin(x)x = torch.linspace(-3.14, 3.14, 100)y = f(x)plt.plot(x, y)plt.xlabel('x')plt.ylabel('y')plt.title('Sine Function')plt.show()
First order Taylor approximation for f(x) at x = 0