Scroll to Top

Virtual Math Learning Center

Virtual Math Learning Center Texas A&M University Virtual Math Learning Center

List Comprehension and Patterns in Higher Derivatives in Python

Author: David Manuel

In this video, it is shown how to use Python to quickly find several derivatives of a function using Python's list comprehension tool. We can then find the patterns in the results allowing us to write a formula for all higher order derivatives of the function.

Transcript 14

Problem: Find the first several derivatives of \(f(x)=\frac{1}{x}\) and generalize a pattern for the nth derivative.

Python Code: 
from sympy import *

# List Comprehension
x=symbols('x')
f=1/x
# 1st 6 derivatives IN ONE LINE OF CODE!
fderivs=[diff(f,x,i) for i in range(1,7)]
#range(a,b)-> list of integers from a INCLUSIVE to b EXCLUSIVE
print(fderivs)

# exponent is 1 bigger than the derivative we took: x^(n+1)
# signs alternate (-1)^n or (-1)^(n+1). n=1 is negative, so (-1)^n
#factorial numbers in the numerator: n! 
print('The formula for the nth derivative of f is (-1)^n*n!/x^(n+1)')

Open Python Notebook File

See more videos from this section