Skip to content

Instantly share code, notes, and snippets.

@MichaelSDavid
Created July 28, 2021 06:17
Show Gist options
  • Save MichaelSDavid/cb02af79d2fe564ba42c26d240166d67 to your computer and use it in GitHub Desktop.
Save MichaelSDavid/cb02af79d2fe564ba42c26d240166d67 to your computer and use it in GitHub Desktop.
Calculates the derivative of a function at a given point, finds the equation of the tangent line, and plots this on the plane with matplotkib.
from matplotlib import pyplot as plt
def function(x):
# Enter any function (example given by default)
return x**2 + 4
def graph_function():
y_points = []
x_points = []
for i in range(31):
y_points.append(function(i))
x_points.append(i)
plt.plot(x_points, y_points)
def differentiate(x):
y_points = []
x_points = []
h = 1
for i in range(1, 16):
h /= i
derivative = (function(x+h) - function(x))/h
print(derivative)
for j in range(31):
# Slope equation for the tangent line
y_points.append(derivative*(j - x) + function(x))
x_points.append(j)
plt.plot(x_points, y_points)
plt.show()
graph_function()
# Enter input point (example given by default)
differentiate(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment