The concept of limits is fundamental to the understanding of differential and integral calculus.
By calculating the limit, the slope of the tangent (first derivative) can be calculated in general from the secant slope of a function. Many antiderivatives (integrals) can also be calculated from the limits for the upper and lower sums. SymPy calculates the limit of a sequence or function using the limit(f,x,g) method. The f object stands for a mathematical sequence or function; x is the independent variable; and, for g, the limit can be specified.
Limits of Sequences
Let’s use SymPy to calculate the limits of the five sequences shown in this table.
The sequences listed in the table are in accordance with the following formation laws:
An exception is the last sequence:
For this sequence, the formation law cannot be read from the value table. The limit of this sequence for n → ∞ is the Eulerian number e.
Below calculates the limits of the following sequences: an, bn, cn, dn, and en.
01 #18_limit1.py
02 from sympy import *
03 n=symbols("n")
04 #Sequences
05 a1=2*n
06 a2=n**2
07 a3=1/n
08 a4=(2*n**3+2)/(n**3+n**2-5)
09 a5=(1+1/n)**n
10 #Output
11 print("Limits for n towards ∞")
12 print("Limit of %s is: %s" %(a1,limit(a1,n,oo)))
13 print("Limit of %s is: %s" %(a2,limit(a2,n,oo)))
14 print("Limit of %s is: %s" %(a3,limit(a3,n,oo)))
15 print("Limit of %s is: %s" %(a4,limit(a4,n,oo)))
16 print("Limit of %s is: %s" %(a5,limit(a5,n,oo)))
Output
Limits for n towards ∞
Limit of 2*n is: oo
Limit of n**2 is: oo
Limit of 1/n is: 0
Limit of (2*n**3 + 2)/(n**3 + n**2 - 5) is: 2
Limit of (1 + 1/n)**n is: E
Analysis
In lines 12 to 16, the limits of the sequences defined in lines 05 to 09 are calculated and output using the limit(a1,n,oo) method. As a first parameter, this method expects the name of the sequence, the second parameter specifies the variable of the limit, and the third parameter specifies against which limit the sequence should strive. The outputs confirm the expected results.
Limits of Functions
SymPy can also calculate the limits of functions. Now, let’s calculate the limits for the following functions:
Below calculates the limits for the listed functions.
01 #19_limit2.py
02 from sympy import *
03 a,x=symbols("a x")
04 #Functions
05 y1=sin(x)/x
06 y2=tan(x)/x
07 y3=a*(1-exp(-x))
08 y4=1/x+2
09 y5=(x**2-1)/(x-1)
10 #Output
11 print("Limit of %s toward 0 is: %s" %(y1,limit(y1,x,0)))
12 print("Limit of %s toward 0 is: %s" %(y2,limit(y2,x,0)))
13 print("Limit of %s toward ∞ is: %s" %(y3,limit(y3,x,oo)))
14 print("Limit of %s toward ∞ is: %s" %(y4,limit(y4,x,oo)))
15 print("Limit of %s toward 1 is: %s" %(y5,limit(y5,x,1)))
Output
Limit of sin(x)/x toward 0 is: 1
Limit of tan(x)/x toward 0 is: 1
Limit of a*(1 - exp(-x)) toward ∞ is: a
Limit of 2 + 1/x toward ∞ is: 2
Limit of (x**2 - 1)/(x - 1) toward 1 is: 2
Analysis
In lines 05 to 09, those functions are defined whose limits must be calculated using the limit() method. The arguments are passed in the same order as the sequences. The outputs in lines 11 to 15 again confirm the expected results.
Differential Quotient
For the following functions, we’ll use SymPy to calculate the differential quotient by computing the limits from the difference quotient:
Below shows the implementation.
01 #20_limit3.py
02 from sympy import *
03 a,x,h,n=symbols('a x h n')
04 f1_1=((x+h)**n-x**n)/h #power rule
05 f1_2=(a**(x+h)-a**x)/h #exponential function
06 f1_3=(sin(x+h)-sin(x))/h #trigonometric function
07 f1_4=(sinh(x+h)-sinh(x))/h #hyberbolic function
08 f1_5=(sin(x+h)*cos(x+h)-sin(x)*cos(x))/h #product rule
09 #Output
10 print("Limits for h toward zero")
11 print("limes",f1_1," = ",simplify(limit(f1_1,h,0)))
12 print("limes",f1_2," = ",simplify(limit(f1_2,h,0)))
13 print("limes",f1_3," = ",simplify(limit(f1_3,h,0)))
14 print("limes",f1_4," = ",simplify(limit(f1_4,h,0)))
15 print("limes",f1_5," = ",simplify(limit(f1_5,h,0)))
Output
limes (-x**n + (h + x)**n)/h = n*x**(n - 1)
limes (-a**x + a**(h + x))/h = a**x*log(a)
limes (-sin(x) + sin(h + x))/h = cos(x)
limes (-sinh(x) + sinh(h + x))/h = cosh(x)
limes (-sin(x)*cos(x) + sin(h + x)*cos(h + x))/h = cos(2*x)
Analysis
Lines 04 to 08 define five difference quotients known from school math. The limit() method calculates the limits of these difference quotients for h toward zero in lines 11 to 15. These outputs have been simplified using simplify() but confirm the expected results.
Editor’s note: This post has been adapted from a section of the book Python for Engineering and Scientific Computing by Veit Steinkamp. Dr. Steinkamp studied electrical engineering and German to become a teacher and pass on his knowledge at vocational schools and technical colleges. He teaches electrical engineering, application development, and mechanical engineering technology. He has also taught theoretical electrical engineering and the fundamentals of electrical engineering.
This post was originally published 6/2025.
Comments