How to calculate a limit of a function in python
To find the limit of a mathematical function in python, we can use limit() function of the sympy library.
limit(y,x,x0,s)
- The first argument (y) is the functionf (x) whose limit is to be calculated
- The second parameter (x) is the reference variable ( argument )
- The third parameter (x0) is the accumulation point
- oo = + infinity
- -oo = - infinity
- 0 = zero
- n = number
- The fourth parameter (s) is used to calculate the lateral limits of a point
- '+' =right limit
- '-' = left limit
The function limit() calculates the limit of the function f (x) as x approaches x0.
$$ \lim_{x \rightarrow x_0 } f(x) = l $$
What's a limit of a function? The limit of a function f(x) at X0 point in its domain, if it exists, is the value that the function f(x) approaches as its argument approaches X0. The notation of a limit is as follows: $$ \lim_{x \rightarrow x_0 } f(x) = l $$ We can read "the limit of f(x) as x approaches x0 is L".
The limit() function must be imported from the sympy library using the command from sympy import limit.
Examples
Example 1
This script calculates the limit of the function 1/x as x approaches zero.
from sympy import limit, Symbol
x = Symbol('x')
y=1/x
limit(y, x, 0)
The function returns to output
oo
The limit of the function 1 / x as x approaches zero is more infinite (oo).
$$ \lim_{x \rightarrow 0 } \frac{1}{x} = \infty $$
Example 2
This script calculates the limit of the 1/x function as x approaches + infinite.
from sympy import limit, oo, Symbol
x = Symbol('x')
y=1/x
limit(y, x, oo)
The oo symbol (+ infinity) must be imported from sympy.
The output of the function is
0
The limit of the function 1 / x for x tending to +∞ is zero.
$$ \lim_{x \rightarrow \infty } \frac{1}{x} = 0 $$
Example 3
This script calculates the limit of the function x2 as x approaches - infinite.
from sympy import limit, oo, Symbol
x = Symbol('x')
y=x**2
limit(y, x, -oo)
The output of the function is
oo
The limit of the function is + infinite.
$$ \lim_{x \rightarrow - \infty } x^2 = \infty $$
Example 4
This script calculates the limit of the x2 as x approaches 4.
from sympy import limit, oo, Symbol
x = Symbol('x')
y=x**2
limit(y, x, 4)
The output of the function is
16
The limit of the function for x tending to 4 is 16.
$$ \lim_{x \rightarrow 4 } x^2 = 16 $$
Example 5 (lateral limit)
This script calculates the lateral boundary of the 1 / x function with x tending towards zero from the left.
from sympy import limit, Symbol
x = Symbol('x')
y=1/x
limit(y, x, 0, '-')
The function returns in output
-oo
The limit of the function 1 / x as x tending to zero from the left is minus infinite (-oo).
$$ \lim_{x \rightarrow 0^- } \frac{1}{x} = - \infty $$