Last active
October 27, 2019 09:14
-
-
Save Rowing0914/f294baa0831d7eb57bbdff1788793aab to your computer and use it in GitHub Desktop.
First/Second/Third order Taylor Approximation of f(x) = x ** 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from autograd import grad | |
import matplotlib.pyplot as plt | |
def f(x): | |
return x ** 3 | |
def f_first(x): | |
return 3 * x ** 2 | |
def f_second(x): | |
return 6 * x | |
def _test(): | |
""" test phase """ | |
for x in np.arange(1, 6, dtype=np.float32): | |
# y = f(x) | |
# print("y: {}".format(y)) | |
grad_f = grad(f) | |
y1 = grad_f(x) | |
y2 = f_first(x) | |
print("First Order Gradient| autograd: {}, actual: {}".format(y1, y2)) | |
grad_f = grad(grad(f)) | |
y1 = grad_f(x) | |
y2 = f_second(x) | |
print("Second Order Gradient| autograd: {}, actual: {}".format(y1, y2)) | |
def taylor_approx(): | |
num_data = 1000 | |
# specify the point at which we approximate the graph f | |
point = 2.0 | |
# get the correct answer | |
x = np.linspace(-10, 10, num_data, dtype=np.float32) | |
ans = f(x) | |
# first order taylor approximation | |
grad_first = grad(f) | |
approx_first = f(point) + grad_first(point)*(x - point) | |
# second order taylor approximation | |
grad_second = grad(grad_first) | |
approx_second = f(point) + grad_first(point)*(x - point) + 0.5*grad_second(point)*(x - point)**2 | |
# third order taylor approximation | |
grad_third = grad(grad_second) | |
approx_third = f(point) + grad_first(point)*(x - point) + 0.5*grad_second(point)*(x - point)**2 + (1/6)*grad_third(point)*(x - point)**3 | |
# visualise the result | |
plt.plot(x, ans, label="f(x)") | |
plt.plot(x, approx_first, label="approx-first") | |
plt.plot(x, approx_second, label="approx-second") | |
plt.plot(x, approx_third, label="approx-third") | |
plt.grid() | |
plt.legend() | |
plt.show() | |
if __name__ == '__main__': | |
# _test() | |
taylor_approx() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just to play with
autograd