Skip to content

Instantly share code, notes, and snippets.

@jgphilpott
Created January 29, 2021 21:40
Show Gist options
  • Save jgphilpott/d0d6bb911cf4ec4b26351bfce1f96ac7 to your computer and use it in GitHub Desktop.
Save jgphilpott/d0d6bb911cf4ec4b26351bfce1f96ac7 to your computer and use it in GitHub Desktop.
A collection of functions for performing differential and integral calculus in Python.
# Find the tangent of any polynomial function at x.
def find_tangent(x, coefficients):
slope = 0
intercept = coefficients[0]
for i, coefficient in enumerate(coefficients):
if i != 0:
slope += coefficient * i * pow(x, i - 1)
intercept += coefficient * pow(x, i)
return [intercept - slope * x, slope]
# Find the area under any polynomial function within bounds.
# This function works assuming your curve is either entirely above or entirely below the x axis.
# If your curve crosses the x axis within the given bounds you will need to first find the roots (x intercepts) and then calculate the segments above and below the axis separately.
def find_area(lower_limit, upper_limit, coefficients):
lower = 0
upper = 0
for i, coefficient in enumerate(coefficients):
lower += (coefficient * pow(lower_limit, i + 1)) / (i + 1)
upper += (coefficient * pow(upper_limit, i + 1)) / (i + 1)
return abs(upper - lower)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment