import numpy as np
import matplotlib.pylab as plt

def forward_difference(f, x):
	h = 1e-4 # 0.001
	return (f(x+h) - f(x)) / h

def central_difference(f, x):
	h = 1e-4 # 0.0001
	return (f(x+h) - f(x-h)) / (2*h)

def function_1(x):
	return 0.01*x**2 + 0.1*x

# it returns a tangent line of function f where x-point is x
def tangent_line(f, x):
	d = central_difference(f, x)	# df(x)/dx
	y = f(x) - d*x	# tangent point of y-axis
	return lambda t: d*t + y	# return a tangent line

def main():
	x = np.arange(0.0, 20.0, 0.1)
	y = function_1(x)
	plt.xlabel("x")
	plt.ylabel("f(x)")

	tf = tangent_line(function_1, 5)
	y2 = tf(x)

	plt.plot(x, y)
	plt.plot(x, y2)
	plt.show()

if __name__ == '__main__':
	main()
