Suppose that you write something like this for your students. This is just the beginning of something I would like to write to help them with the formal definition of a limit, but I am puzzled by the first output.

formalLimit[expr_, a_, L_, ϵ_] := Module[{f},
  f[x_] := expr;

So, my first question is, when I enter

formalLimit[x^2, 2, 4, .1]

Why do I get the output $x^2$?

Once I get past this problem, my next question is, suppose a student enters an expression $\sqrt{4-t^2}$, not using my choice of $x$ as the independent variable. How can I identify their choice of independent variable in the expr_, then change it to $x$ when using f[x_]:=expr ?

Update: I'd like to thank all of my colleagues for their kind responses. Here is what I was able to do thanks to your help. Students will get a question (yes, it can be answered much more quickly using Mathematica techniques such as Reduce, etc, but I want a visual introduction to the formal definition of a limit) such as "Use a graph to find a number $\delta$ such that


As an example, find a $\delta$ such that


I've written this function:

formalLimit[expr_, var_, a_, L_, ϵ_] := 
  DynamicModule[{f, δ},
    f = (expr /. var -> #) &;
      δ = Min[Abs[p[[1]] - a], Abs[q[[1]] - a]];
        Plot[f[x], {x, a - zoom, a + zoom}, 
          PlotRange -> {{a - zoom, a + zoom}, {L - 2 ϵ, L + 2 ϵ}},
          Epilog -> 
             Arrow[{{a, L - 2 ϵ}, {a, f[a]}, {a - 1.5 δ, L}}],
             {Red, Dashed,
               InfiniteLine[{{p[[1]], L - ϵ}, {p[[1]], L + ϵ}}],
               InfiniteLine[{{q[[1]], L - ϵ}, {q[[1]], L + ϵ}}]}},
          AxesLabel -> {ToString[var], "y"},
          PlotLabel -> "δ = " <> ToString[δ],
          AxesOrigin -> {a - 1.5 δ, L - 2 ϵ},
          Ticks -> {{p[[1]], a, q[[1]]}, {L - ϵ, L, L + ϵ}}],
        Plot[{L - ϵ, L + ϵ}, {x, a - zoom, a + zoom},
          PlotStyle -> Directive[Dashed, GrayLevel[0.8]],
          Filling -> {1 -> {2}}]],
     {{zoom, 3}, 0.0001, 3, Appearance -> "Labeled"},
     {{p, {a - 1, L - ϵ}}, Locator, Appearance -> None},
     {{q, {a + 1, L + ϵ}}, Locator, Appearance -> None}]]

Then the students can enter:

formalLimit[Sqrt[x], x, 1, 1, .5]

And they get this image, where they can use their mouse to drag the dashed vertical lines to help determine $\delta$. There is also a slider for some zooming to adjust the horizontal size of the window.

enter image description here

This function has only been slightly tested, so I wouldn't mind hearing warnings and suggestions.


marked as duplicate by Jens, m_goldberg, user9660, MarcoB, Kuba Jan 3 '16 at 11:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ It's an issue of scoping: the x in expr is not the same as the locally scoped pattern variable in f[x_]. The result from Trace[formalLimit[x^2, 2, 4, .1]] should help in understanding this. $\endgroup$ – Daniel Lichtblau Jan 2 '16 at 20:20
  • $\begingroup$ @DanielLichtblau The Trace command returns a bunch of stuff I don't understand, but I focused on the part Module[{f$}, f$[x$_] := x^2, which I think you are referring to. $\endgroup$ – David Jan 2 '16 at 22:42
  • 1
    $\begingroup$ Yes, it was exactly that "variable renaming" that I had meant. It shows that there is alocalized scoping of the pattern variable. $\endgroup$ – Daniel Lichtblau Jan 2 '16 at 23:40
  • 1
    $\begingroup$ @Jens So I've been trying to figure out exactly why all the answers here are so different from the great answer you gave me to the question you referenced in your comment above. $\endgroup$ – rogerl Jan 3 '16 at 1:29
  • 1
    $\begingroup$ Scoping issues aside, I will say I like this means of illustrating limits. Also there may be some very good approaches in Wolfram Demonstrations. Okay, I think I got that mandatory advertising itch out of my system. $\endgroup$ – Daniel Lichtblau Jan 3 '16 at 3:53

As Daniel Lichtblau points out in the comments, it's a scoping problem. From the documentation:

The named formal parameters xi in Function[{x1,...}, body] are treated as local, and are renamed xi$ when necessary to avoid confusion with actual arguments supplied to the function.

You can get around this in several ways:

  1. Although I don't recommend it, you can explicitly supply x$ as the variable in expr:

    formalLimit[x$^2, 2, 4, .1]


    This will not solve your second problem.

  2. A better way is to rewrite formalLimit to take an extra argument specifying the variable for which the limit is being taken:

    formalLimitMk2[expr_, var_, a_, L_, \[Epsilon]_] := Module[{f},
      f = (expr /. var -> Slot[]) &;
    formalLimitMk2[x^2, x, 2, 4, .1]


  3. If your functions will all have only one variable, you can use Variables to extract it:

    formalLimitMk3[expr_, a_, L_, \[Epsilon]_] := Module[{f},
      f = (expr /. First@Variables@expr -> Slot[]) &;
    formalLimitMk3[Sqrt[4 - t^2], x, 2, 4, .1]


    Variables is described in the documentation as working for polynomials.


Use an optional argument for the variable with a default of x


formalLimit[expr_, a_, L_, ϵ_, sym : _Symbol : x] :=
 Module[{f}, f[y_] := expr /. sym -> y;

formalLimit[x^2, 2, 4, .1]

(*  4  *)

formalLimit[z^2, 2, 4, .1, z]

(*  4  *)
  • $\begingroup$ That default for the formal argument sym is dangerous. Consider With[{x = 42}, formalLimit[x^2, 2, 4, .1]]; it does not return 4. $\endgroup$ – m_goldberg Jan 2 '16 at 21:41

I would approach of problem of giving students a formal limit evaluator from a different point of view. I would write a multivariate formal limit evaluator and specialize for the single variable case. Since I have a version of Mathematica that is later than V10.1, I would make use of the fairly new ContainsAll function.

formalLimit[expr_, x_Symbol -> val_] := formalLimit[expr, {x -> val}]
formalLimit[expr_, rules : {(_Symbol -> _) ..}] :=
  (expr /. rules) /; ContainsAll[Cases[expr, _, ∞], rules[[All, 1]]]

With these definitions, the following evaluations succeed.

formalLimit[1/x, x -> ∞]


formalLimit[Sqrt[x^2 + y^2], {x -> 3, y -> 4}]


But these evaluations fail as they should,

formalLimit[Sqrt[x^2 + z^2], {x -> 3, y -> 4}]

formalLimit[Sqrt[x^2 + z^2], {x -> 3, y -> 4}]

formalLimit[Sqrt[x^2 + y^2], {x -> 3, z -> 4}]

formalLimit[Sqrt[x^2 + y^2], {x -> 3, z -> 4}]

With[{x = 42}, formalLimit[Sqrt[x^2 + y^2], {x -> 3, y -> 4}]]

formalLimit[Sqrt[1764 + y^2], {42 -> 3, y -> 4}]

  • $\begingroup$ A problem with multivariate limit handling is that you have to be clear as to what exactly is the limit you are taking. (Something like "y before z except after c"?) $\endgroup$ – Daniel Lichtblau Jan 3 '16 at 0:04
  • $\begingroup$ @DanielLichtblau. I don't think David (the OP) is really trying to take real limits, but your point it well-taken. $\endgroup$ – m_goldberg Jan 3 '16 at 0:17

This solution works for any variable identifier consisting of one character

formalLimit[expr_, a_, L_, eps_] :=
  Block[{f, x$, p},
    p = Position[expr, s_Symbol /; StringLength@ToString@s == 1];
    f[x_] := ReplacePart[expr, p -> x$];

formalLimit[Sqrt[4 - t^2], 2, 4, .1]


  • $\begingroup$ Exactly, thanks for noticing :) $\endgroup$ – eldo Jan 2 '16 at 21:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.