<jupyter_start><jupyter_text>First and second order Taylor series approximation<jupyter_code>%matplotlib notebook 
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 100)
fx = np.sin(x)
plt.plot(x, fx)<jupyter_output><empty_output><jupyter_text>The first order Taylor series of the function at point $x_0$ is given by $f(x_0+\Delta x) = f(x_0) + \left. \frac{df}{dx}\right |_{x=x_0}\Delta x$Defining $u=\Delta x$ we have $f(x_0+u) = f(x_0) + \left. \frac{df}{dx}\right |_{x=x_0}u$<jupyter_code>x0 = np.pi/4
u = np.linspace(-np.pi/4, np.pi/4, 50)

fx0 = np.sin(x0)
dfdx_x0 = np.cos(x0)
f_approx = fx0 + dfdx_x0*u

plt.figure()
plt.plot(x, fx)
plt.plot(x0+u, f_approx)<jupyter_output><empty_output><jupyter_text>For instance, if we want to find the value of the function at point $x=\pi/4+0.1$<jupyter_code>u_eval = 0.5
f_approx_u_eval = fx0 + dfdx_x0*u_eval  # Approximation
f_exact = np.sin(x0+u_eval)             # Actual value of the function

plt.figure()
plt.plot(x, fx)
plt.plot(x0+u, f_approx)
plt.scatter([x0+u_eval, x0+u_eval], [f_exact, f_approx_u_eval], s=20, c='red', zorder=10)<jupyter_output><empty_output><jupyter_text>The second order Taylor series of the function at point $x_0$ is given by $f(x_0+\Delta x) = f(x_0) + \left. \frac{df}{dx}\right |_{x=x_0}\Delta x + \frac{1}{2}\left. \frac{d^2f}{dx^2}\right |_{x=x_0}\Delta x^2$Defining $u=\Delta x$ we have $f(x_0+u) = f(x_0) + \left. \frac{df}{dx}\right |_{x=x_0}u + \frac{1}{2}\left. \frac{d^2f}{dx^2}\right |_{x=x_0}u^2$<jupyter_code>x0 = np.pi/4
u = np.linspace(-np.pi/4, np.pi/4, 50)

fx0 = np.sin(x0)
dfdx_x0 = np.cos(x0)      # First derivative
d2fdx2_x0 = -np.sin(x0)   # Second derivative
f_approx = fx0 + dfdx_x0*u + d2fdx2_x0*u**2/2

plt.figure()
plt.plot(x, fx)
plt.plot(x0+u, f_approx)<jupyter_output><empty_output><jupyter_text>The Taylor expansion for the sin function is not that useful, but suppose we have some weird function:<jupyter_code>def weird_function(x):
    return -4*x**4+50*x**3-5*x**2+x

x = np.linspace(0, 13)
f = weird_function(x)

plt.figure()
plt.plot(x, f)<jupyter_output><empty_output><jupyter_text>and we want to know how does this function looks like at point $x_0=8$<jupyter_code>x0 = 8
dx = 0.001
u = np.linspace(-2, 2, 50)

fx0 = weird_function(x0)
fxe = weird_function(x0-dx)
fxd = weird_function(x0+dx)
dfdx_x0 = (fxd-fx0)/dx
d2fdx2_x0 = (fxe-2*fx0+fxd)/dx**2
f_approx = fx0 + dfdx_x0*u + d2fdx2_x0*u**2/2

plt.figure()
plt.plot(x, f)
plt.plot(x0+u, f_approx)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1741195092995647, 'fraction_numerical': 0.04867431737237832, 'mean_word_length': 5.180929095354523, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '612061', 'license': 'MIT', 'n_tokens_mistral': 1233, 'n_tokens_neox': 1194, 'n_words': 235, 'path': 'Lecture 07 -Detecting salient points/Taylor series approximation.ipynb', 'repo_name': 'chcomin/disciplina-visao-computacional-01-2019', 'size': '2527'}