How to compute the limit of a function (in Python, using SymPy)
Task
In mathematics, we write
to refer to the value that
How can we use software to compute such limits?
Solution
This answer assumes you have imported SymPy as follows.
1
2
from sympy import * # load all math functions
init_printing( use_latex='mathjax' ) # use pretty math output
Here we define a simple mathematical formula, limit
function, which takes the formula
1
2
3
var( 'x' )
formula = ( x**2 - x - 2 ) / ( x - 2 )
limit( formula, x, 2 )
You can also compute one-sided limits. For instance,
the limit of
1
limit( abs(x)/x, x, 0, "-" ) # "-" means from the left
1
limit( abs(x)/x, x, 0, "+" ) # "+" means from the right
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Contributed by Nathan Carter (ncarter@bentley.edu)