Link Search Menu Expand Document (external link)

How to compute the limit of a function


In mathematics, we write

\[\lim_{x\to a} f(x)\]

to refer to the value that $f$ approaches as $x$ gets close to $a$, called “the limit of $f(x)$ as $x$ approaches $a$.”

How can we use software to compute such limits?

Using SymPy, in Python

View this solution alone.

This answer assumes you have imported SymPy as follows.

from sympy import *                   # load all math functions
init_printing( use_latex='mathjax' )  # use pretty math output

Here we define a simple mathematical formula, $\frac{x^2-x-2}{x-2}$, and compute the limit as $x$ approaches 2. We use SymPy’s built-in limit function, which takes the formula $f(x)$, the variable $x$, and the value $a$.

var( 'x' )
formula = ( x**2 - x - 2 ) / ( x - 2 )
limit( formula, x, 2 )

$\displaystyle 3$

You can also compute one-sided limits. For instance, the limit of $\frac{\vert x\vert}{x}$ is $1$ as $x$ approaches 0 from the right, but it is $-1$ as $x$ approaches 0 from the left.

limit( abs(x)/x, x, 0, "-" )  # "-" means from the left

$\displaystyle -1$

limit( abs(x)/x, x, 0, "+" )  # "+" means from the right

$\displaystyle 1$

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Topics that include this task


This website does not yet contain a solution for this task in any of the following software packages.

  • R
  • Excel
  • Julia

If you can contribute a solution using any of these pieces of software, see our Contributing page for how to help extend this website.