# Limits


{{< include ../_common_code.qmd >}}

This section uses the following add-on packages:


```{julia}
using CalculusWithJulia
using Plots
plotly()
using Richardson  # for numeric limits
using SymPy       # for symbolic limits
```


---


A historic problem in the history of math was to find the area under the graph of $f(x)=x^2$ between $[0,1]$.


There wasn't a ready-made formula for the area of this shape, as was known for a triangle or a square. However, [Archimedes](http://en.wikipedia.org/wiki/The_Quadrature_of_the_Parabola) found a method to compute areas enclosed by a parabola and line segments that cross the parabola.

::: {#fig-make-archimedes-triangle-graph}
```{julia}
#| hold: true
#| echo: false
#| cache: true
###{{{archimedes_parabola}}}
gr()
f(x) = x^2
colors = [:black, :blue, :orange, :red, :green, :orange, :purple]

## Area of parabola
function make_triangle_graph(n)
    title = "Area of parabolic cup ..."
    n==1 && (title = L"Area $= 1/2$")
    n==2 && (title = L"Area $=$ previous $+\; \frac{1}{8}$")
    n==3 && (title = L"Area $=$ previous $+\; 2\cdot\frac{1}{8^2}$")
    n==4 && (title = L"Area $=$ previous $+\; 4\cdot\frac{1}{8^3}$")
    n==5 && (title = L"Area $=$ previous $+\; 8\cdot\frac{1}{8^4}$")
    n==6 && (title = L"Area $=$ previous $+\; 16\cdot\frac{1}{8^5}$")
    n==7 && (title = L"Area $=$ previous $+\; 32\cdot\frac{1}{8^6}$")



    plt = plot(f, 0, 1;
			   legend=false,
			   size = fig_size,
			   linewidth=2)
    annotate!(plt, [
		(0.05, 0.9, text(title,:left))
	])  # if in title, it grows funny with gr
    n >= 1 && plot!(plt, [1,0,0,1, 0], [1,1,0,1,1];
					color=colors[1], linetype=:polygon,
					fill=colors[1], alpha=.2)
    n == 1 && plot!(plt, [1,0,0,1, 0], [1,1,0,1,1];
					color=colors[1], linewidth=2)
    for k in 2:n
        xs = range(0, stop=1, length=1+2^(k-1))
        ys = f.(xs)
        k < n && plot!(plt, xs, ys;
					   linetype=:polygon, fill=:black, alpha=.2)
        if k == n
            plot!(plt, xs, ys;
				  color=colors[k], linetype=:polygon, fill=:black, alpha=.2)
            plot!(plt, xs, ys;
				  color=:black, linewidth=2)
        end
    end
    plt
end


n = 7
anim = @animate for i=1:n
    make_triangle_graph(i)
end

imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 1)


caption = L"""
"""

plotly()
ImageFile(imgfile, caption)
```

The first triangle has area $1/2$, the second has area $1/8$, then $2$ have area $(1/8)^2$, $4$ have area $(1/8)^3$, ... With some algebra, the total area then should be $1/2 \cdot (1 + (1/4) + (1/4)^2 + \cdots) = 2/3$.
:::


@fig-make-archimedes-triangle-graph illustrates a means to compute the area bounded by the parabola, the line $y=1$ and the line $x=0$ using triangles. It suggests that this area can be found by adding the following sum


$$
A = 1/2 + 1/8 + 2 \cdot (1/8)^2 + 4 \cdot (1/8)^3 + \cdots
$$

This value is $2/3$, so the area under the curve would be $1/3$. Forget about this specific value---which through more modern machinery becomes uneventful---and focus for a minute on the method: a problem is solved by a suggestion of an infinite process, in this case the creation of more triangles to approximate the unaccounted for area. This is the so-called method of [exhaustion](http://en.wikipedia.org/wiki/Method_of_exhaustion), a method known since the 5th century BC.


Archimedes used this method to solve a wide range of area problems related to basic geometric shapes, including a more general statement of what we described above.


The $\cdots$ in the sum expression  indicate that this process continues and that the answer is at the end of an *infinite* process. To make this line of reasoning rigorous requires the concept of a limit. The concept of a limit is then an old one, but it wasn't until the age of calculus that it was formalized.


Next, we illustrate how Archimedes approximated $\pi$ – the ratio of the circumference of a circle to its diameter – using interior and exterior $n$-gons whose perimeters could be computed.

::: {#fig-archimedes-pi-approximation}
```{julia}
#| hold: true
#| echo: false
## Archimedes approximation for pi
gr()
blue, green, purple, red = :royalblue, :forestgreen, :mediumorchid3, :brown3


function archimedes!(p, n, xy=(0,0), radius=1; color=blue)

    x₀,y₀=xy
    ts = range(0, 2pi, length=100)



    plot!(p, x₀ .+ sin.(ts), y₀ .+ cos.(ts), linewidth=2)

    α = ((2π)/n)/2
    αs = (-pi/2 + α):2α:(3pi/2 + α)
    r = radius/cos(α)

    xs = x₀ .+ r*cos.(αs)
    ys = y₀ .+ r*sin.(αs)

    plot!(p, xs, ys, linewidth=2, alpha=0.6)
    plot!(p, xs, ys,
          fill=true,
          fillcolor=color,
          alpha=0.4)

    r = radius
    xs = x₀ .+ r*cos.(αs)
    ys = y₀ .+ r*sin.(αs)

    plot!(p, xs, ys, linewidth=2, alpha=0.6)
    plot!(p, xs, ys,
          fill=true,
          fillcolor=color,
          alpha=0.8)

    p
end

ns = [4,5,7,9]
xs = [1, 3.5, 1, 3.5]
ys = [3.5, 3.5, 1, 1]
p = plot(;xlims=(-.25, 4.75), ylims=(-0.25, 4.75),
         axis=nothing,
         xaxis=false,
         yaxis=false,
         legend=false,
         padding = (0.0, 0.0),
             background_color = :transparent,
         foreground_color = :black,
         aspect_ratio=:equal)

for (x, y, n, col) ∈ zip(xs, ys, ns, (blue, green, purple, red))
    archimedes!(p, n, (x, y), color=col)
end

caption = L"""
"""
plotly()
ImageFile(p, caption)
```

The ratio of the circumference of a circle to its diameter, $\pi$, can be approximated from above and below by computing the perimeters of the inscribed $n$-gons. Archimedes computed the perimeters for $n$ being $12$, $24$, $48$, and $96$ to determine that $3~10/71 \leq \pi \leq 3~1/7$.
:::

With @fig-archimedes-pi-approximation Archimedes uses *bounds* to constrain an unknown value. Had he been able to compute these bounds for larger and larger $n$ the value of $\pi$ could be more accurately determined. In a "limit" it would be squeezed in to have a specific value, which we now know is an irrational number.


Continuing these concepts, [Fermat](http://en.wikipedia.org/wiki/Adequality) in the 1600s essentially took a limit to find the slope of a tangent line to a polynomial curve. Newton in the late 1600s, exploited the idea in his development of calculus (as did Leibniz). Yet it wasn't until the 1800s that [Bolzano](http://en.wikipedia.org/wiki/Limit_of_a_function#History), Cauchy and Weierstrass put the idea on a firm footing.


To make things more precise, we begin by discussing the limit of a univariate function as $x$ approaches $c$.


Informally, if a limit exists it is the value that $f(x)$ gets close to as $x$ gets close to---but not equal to---$c$.


The modern formulation is due to Weierstrass:

::: {.definition title="Definition: a limit"}
The $\epsilon-\delta$ Definition of a limit of $f(x)$ defines
the limit of $f(x)$ as $x$ approaches $c$ as $L$, written,

$$
\lim_{x \rightarrow c} f(x) = L,
$$

by if for every real $\epsilon > 0$, there exists a real $\delta > 0$ such that for all real $x$, if $0 < \lvert x − c \rvert < \delta$ then $\lvert f(x) − L \rvert < \epsilon$.

:::


We comment on this later.


Cauchy begins his incredibly influential [treatise](http://gallica.bnf.fr/ark:/12148/bpt6k90196z/f17.image) on calculus considering two examples, the limit as $x$ goes to $0$ of


$$
\frac{\sin(x)}{x} \quad\text{and}\quad (1 + x)^{1/x}.
$$

These take the indeterminate forms $0/0$ and $1^\infty$, which are found by just putting $0$ in for $x$. To discuss a limit an expression does not need to be defined at $c$, as these two aren't at $c=0$. Cauchy illustrates two methods to approach the questions above.

The first is to pull out an inequality:


$$
\frac{\sin(x)}{\sin(x)} > \frac{\sin(x)}{x} > \frac{\sin(x)}{\tan(x)}
$$

which is equivalent to:


$$
1 > \frac{\sin(x)}{x} > \cos(x)
$$

This bounds the expression $\sin(x)/x$ between $1$ and $\cos(x)$ and as $x$ gets close to $0$, the value of $\cos(x)$ "clearly" goes to $1$, hence $L$ must be $1$. This is an application of the squeeze theorem, the same idea Archimedes implied when bounding the value for $\pi$ above and below.


The above bound comes from a picture like that in @fig-sin-cos-bound, for small $\theta > 0$:


::: {#fig-sin-cos-bound}

```{julia}
#| hold: true
#| echo: false
plt = let
    gr()

    empty_style = (xaxis=([], false),
                    yaxis=([], false),
                    framestyle=:origin,
                    legend=false)
	axis_style = (arrow=true, side=:head, line=(:gray, 1))
	text_style = (10,)
	fn_style =  (;line=(:black, 3))
	fn2_style =  (;line=(:red, 4))
	mark_style = (;line=(:gray, 1, :dot))
	domain_style = (;fill=(:orange, 0.35), line=nothing)
	range_style = (; fill=(:blue, 0.35), line=nothing)

	plot(; empty_style..., aspect_ratio=:equal)
    plot!(x -> sqrt(1 - x^2), 0, 1; line=(:black, 2))


    θ = π/6
    y,x = sincos(θ)
    col=RGBA(0.0,0.0,1.0, 0.25)

    plot!(range(0,x, length=2), zero, fillrange=u->y/x*u, color=col)
    plot!(range(x, 1, length=50), zero, fillrange = u -> sqrt(1 - u^2), color=col)

	plot!([x,x],[0,y], line=(:dash, 2, :black))
    plot!([x,1],[y,0], line=(:dot, 2, :black))
    plot!([1,1], [0,y/x], line=(2, :black))
    plot!([0,1], [0,y/x], line=(2, :black))
    plot!([0,1], [0,0], line=(2, :black))
    Δ = 0.05
    annotate!([(0,0+Δ, text(L"A", 10)),
               (x-Δ,y+Δ/4, text(L"B",10)),
               (1+Δ/2,y/x, text(L"C", 10)),
               (1+Δ/2,0+Δ/2, text(L"D", 10)),
              (0.2*cos(θ/2), 0.2*sin(θ/2), text(L"\theta", 12))
              ])
    current()
end
plt
```

```{julia}
#| echo: false
plotly()
nothing
```

Triangle $\triangle ABD$ has less area than the shaded wedge, which has less area than triangle $\triangle ACD$. Their respective areas are $(1/2)\sin(\theta)$, $(1/2)\theta$, and $(1/2)\tan(\theta)$. The inequality used to show $\sin(\theta)/\theta$ is bounded below by $\cos(\theta)$ and above by $1$ comes from a division by $(1/2) \sin(\theta)$ and taking reciprocals.

:::

To discuss the case of $(1+x)^{1/x}$ it proved convenient to assume $x = 1/m$ for integer values of $m$. At the time of Cauchy, log tables were available to identify the approximate value of the limit. Cauchy computed the following value from logarithm tables:


```{julia}
#| hold: true
x = 1/10000
(1 + x)^(1/x)
```

A table can show the progression to this value:


```{julia}
#| hold: true
f(x) = (1 + x)^(1/x)
xs = [1/10^i for i in 1:5]
[xs f.(xs)]
```

This progression can be seen to be increasing. Cauchy, in his treatise, saw this through:


$$
\begin{align*}
(1 + \frac{1}{m})^m &= 1 + \frac{1}{1} + \frac{1}{1\cdot 2}(1 - \frac{1}{m}) +  \\
& \frac{1}{1\cdot 2\cdot 3}(1 - \frac{1}{m})(1 -  \frac{2}{m}) + \cdots \\
&+
\frac{1}{1 \cdot 2 \cdot \cdots \cdot m}(1 - \frac{1}{m}) \cdot \cdots \cdot (1 - \frac{m-1}{m}).
\end{align*}
$$

These values are clearly increasing as $m$ increases. Cauchy showed the value was bounded between $2$ and $3$ and had the approximate value above. Then he showed the restriction to integers was not necessary. Later we will use this definition for the exponential function:


$$
e^x = \lim_{n \rightarrow \infty} \left(1 + \frac{x}{n}\right)^n,
$$

with a suitably defined limit.


These two cases illustrate that though the definition of the limit exists, the computation of a limit is generally found by other means and the intuition of the value of the limit can be gained numerically.


### Indeterminate forms


First it should be noted that for most of the functions encountered, the concepts of a limit at a typical point $c$ is nothing more than just function evaluation at $c$. This is because, at a typical point, the functions are nicely behaved (what we will soon call "*continuous*"). However, most questions asked about limits find points that are not typical. For these, the result of evaluating the function at $c$ is typically undefined, and the value comes in one of several *indeterminate forms*: $0/0$, $\infty/\infty$, $0 \cdot \infty$, $\infty - \infty$, $0^0$, $1^\infty$, and $\infty^0$.


`Julia` can help---at times---identify these indeterminate forms, as many such operations produce `NaN`. For example:


```{julia}
0/0, Inf/Inf, 0 * Inf, Inf - Inf
```

However, the values with powers generally do not look indeterminate, as the IEEE standard has `0^0` evaluating to 1:


```{julia}
0^0, 1^Inf, Inf^0
```

However, this can be unreliable, as floating point issues may mask the true evaluation. However, as a cheap trick it can work. So, the limit as $x$ goes to $1$ of $\sin(x)/x$ is simply found by evaluation:


```{julia}
#| hold: true
x = 1
sin(x) / x
```

But at $x=0$ we get an indicator that there is an issue with just evaluating the function:


```{julia}
#| hold: true
x = 0
sin(x) / x
```

The above is really just a heuristic. For some functions this is just not true. For example, the $f(x) = \sqrt{x}$ is only defined on $[0, \infty)$. There is technically no limit at $0$, per se, as the function is not defined around $0$. Other functions jump at values, and will not have a limit, despite having well defined values. The `floor` function is the function that rounds down to the nearest integer. At integer values there will be a jump (and hence have no limit), even though the function is defined.


## Graphical approaches to limits


Let's return to the function $f(x) = \sin(x)/x$. This function was studied by Euler as part of his solution to the [Basel](http://en.wikipedia.org/wiki/Basel_problem) problem. He knew that near $0$, $\sin(x) \approx x$, so the ratio is close to $1$ if $x$ is near $0$. Hence, the intuition is $\lim_{x \rightarrow 0} \sin(x)/x = 1$, as Cauchy wrote. We can verify this limit graphically two ways. First, a graph along with the points identified to produce it shows no issue at $0$. The $y$ values of @fig-sinx-over-x-minuspi-over-2-to-pi-over-2 seem to go to $1$ as the $x$ values get close to $0$. (That the graph looks defined at $0$ is due to the fact that the points sampled to graph do not include $0$.)


::: {#fig-sinx-over-x-minuspi-over-2-to-pi-over-2}
```{julia}
#| hold: true
#| echo: false
f(x) = sin(x)/x
plot(f, -pi/2, pi/2;
     seriestype=[:scatter, :line], # show points and line segments
     legend=false)
```

Plot of $f(x) = \sin(x)/x$ over $[-\pi/2, \pi2/]$. The graph made by `plot(f, -pi/2, pi/2`)` does not show the issue at $0$ and so suggests a limit of $1$.
:::



We can also verify Euler's intuition through a graph (@fig-plot-sin-and-x-over-minus-pi-over-2-to-pi-over-2).

::: {#fig-plot-sin-and-x-over-minus-pi-over-2-to-pi-over-2}
```{julia}
#| hold: true
plot(sin, -pi/2,  pi/2; label="f(x)=sin(x)")
plot!(identity; label="f(x)=x")    # the function y = x, like how zero is y = 0
```

Plot of both $f(x) = \sin(x)$ and the line $y=x$ over $[-\pi/2, \pi/2]$ showing there similar behaviour near $0$
:::

That the two graphs are indistinguishable near $0$ makes it easy to see that their ratio should be going towards $1$.


A parametric plot shows the same, we see below the slope at $(0,0)$ is *basically* $1$, because the two functions are varying at the same rate when they are each near $0$. In @fig-parametric-plot-of-sin-and-y-equal-x the line $y=x$ is added for easy identification of the slope.

::: {#fig-parametric-plot-of-sin-and-y-equal-x}
```{julia}
#| hold: true
plot(sin, identity, -pi/2, pi/2; label="sin")  # parametric plot
plot!(identity; line=(:dash, :gray25), label="y=x" ) # add y-x line
```

Parametric plot of $(\sin(t), t)$ for $t$ in $[-\pi/2, \pi/2]$ along with the line $y=x$. That the two graphs are similar at $(0,0)$ suggests that the limit of the ratio is $1$.
:::

The graphical approach to limits---plotting $f(x)$ around $c$ and observing if the $y$ values seem to converge to an $L$ value when $x$ get close to $c$---allows us to gather quickly if a function seems to have a limit at $c$, though the precise value of $L$ may be hard to identify.


##### Example


This example illustrates the same limit a different way. Sliding the $x$ value towards $0$ in @fig-jsxgraph-sinx-over-x shows $f(x) = \sin(x)/x$ approaches a value of $1$.

::: {#fig-jsxgraph-sinx-over-x}

```{=html}
<div id="jsxgraph" style="width: 500px; height: 500px;"></div>
```

```{ojs}
//| echo: false
//| output: false

JXG = require("jsxgraph")

b = JXG.JSXGraph.initBoard('jsxgraph', {
    boundingbox: [-6, 1.2, 6,-1.2], axis:true
});

f = function(x) {return Math.sin(x) / x;};
graph = b.create("functiongraph", [f, -6, 6])
seg = b.create("line", [[-6,0], [6,0]], {fixed:true});

X = b.create("glider", [2, 0, seg], {name:"x", size:4});
P = b.create("point", [function() {return X.X()}, function() {return f(X.X())}], {name:""});
Q = b.create("point", [0, function() {return P.Y();}], {name:"f(x)"});

segup = b.create("segment", [P,X], {dash:2});
segover = b.create("segment", [P, [0, function() {return P.Y()}]], {dash:2});


txt = b.create('text', [2, 1, function() {
    return "x = " + X.X().toFixed(4) + ", f(x) = " + P.Y().toFixed(4);
}]);
```

Interactive animation suggesting a limit for $\sin(x)/x$ at $x=0$
:::

##### Example


Consider now the following limit


$$
\lim_{x \rightarrow 2} \frac{x^2 - 5x + 6}{x^2 +x - 6}
$$

Noting that this is a ratio of nice polynomial functions, we first check whether there is anything to do:


```{julia}
#| hold: true
f(x) = (x^2 - 5x + 6) / (x^2 + x - 6)
c = 2
f(c)
```

The `NaN` indicates that this function is indeterminate at $c=2$. A quick plot (@fig-quick-plot-of-rational-function-over-1-to-3) gives us an idea that the limit exists and is roughly $-0.2$. The graph looks "continuous." In fact, the value $c=2$ is termed a *removable singularity* as redefining $f(x)$ to be $-0.2$ when $x=2$ results in a "continuous" function.


::: {#fig-quick-plot-of-rational-function-over-1-to-3}
```{julia}
#| hold: true
c, delta = 2, 1
plot(f, c - delta, c + delta)
```

A quick plot of the rational function $f(x)$ over $[2-1, 2+1]$ (centered about $2$)
:::


As an aside, we can redefine `f` using the "ternary operator":


```{julia}
#| eval: false
f(x) = x == 2.0 ? -0.2 :  (x^2 - 5x + 6) / (x^2 + x - 6)
```

This particular case is a textbook example---one can easily factor $f(x)$ to get:


$$
f(x) = \frac{(x-2)(x-3)}{(x-2)(x+3)}
$$

Written in this form, we clearly see that this is the same function as $g(x) = (x-3)/(x+3)$ when $x \neq 2$. The function $g(x)$ is "continuous" at $x=2$. So were one to redefine $f(x)$ at $x=2$ to be $g(2) = (2 - 3)/(2 + 3) = -0.2$ it would be made continuous, hence the term removable singularity.


## Numerical approaches to limits


The investigation of $\lim_{x \rightarrow 0}(1 + x)^{1/x}$ by evaluating the function at $1/10000$ by Cauchy can be done much more easily nowadays. As does a graphical approach, a numerical approach can give insight into a limit and often a good numeric estimate.


The basic idea is to create a sequence of $x$ values going towards $c$ and then investigate if the corresponding $y$ values are eventually near some $L$.


Best, to see by example. Suppose we are asked to investigate


$$
\lim_{x \rightarrow 25} \frac{\sqrt{x} - 5}{\sqrt{x - 16} - 3}.
$$

We first define a function and check if there are issues at $25$:


```{julia}
f(x) = (sqrt(x) - 5) / (sqrt(x-16) - 3)
```

```{julia}
c = 25
f(c)
```

So yes, an issue of the indeterminate form $0/0$. We investigate numerically by making a set of numbers getting close to $c$. This is most easily done making numbers getting close to $0$ and adding them to or subtracting them from $c$. Some natural candidates are negative powers of $10$:


```{julia}
hs = [1/10^i for i in 1:8]
```

We can add these to $c$ and then evaluate:


```{julia}
xs = c .+ hs
ys = f.(xs)
```

To visualize, we can put in a table using `[xs ys]` notation:


```{julia}
[xs ys]
```

The $y$-values seem to be getting near $0.6$.


Since limits are defined by the expression $0 < \lvert x-c\rvert < \delta$, we should also look at values smaller than $c$. There isn't much difference (note the `.-` sign in `c .- hs`):


```{julia}
#| hold: true
xs = c .- hs
ys = f.(xs)
[xs ys]
```

Same story. The numeric evidence supports a limit of $L=0.6$.

::: {.callout-note}
### The `lim` function

The `CalculusWithJulia` package provides a convenience function `lim(f, c)` to create tables to showcase limits.^[The `limit` function, by default, only takes *right* limits; to be defined in the next section.  The `dir` keyword argument can change this with `dir="+-"` for the two-sided limits discussed here, `dir="-"` for left limits, and the default `dir="+"` for right limits.]

For example:

```{julia}
lim(f, c)
```

The numbers evaluated by `f` are displayed in decreasing order  so the values on the left side of $c$ are read from bottom to top.

:::

##### Example: the secant line


Let $f(x) = x^x$ and consider the ratio:


$$
\frac{f(c + h) - f(c)}{h}
$$

As $h$ goes to $0$, this will take the form $0/0$ in most cases, and in the particular case of $f(x) = x^x$ and $c=1$ it will be. The expression has a geometric interpretation of being the slope of the secant line connecting the two points $(c,f(c))$ and $(c+h, f(c+h))$.


To look at the limit in this example, we have (recycling the values in `hs`):


```{julia}
#| hold: true
c = 1
f(x) = x^x
sec_line_slope(h) = (f(c+h) - f(c)) / h
lim(sec_line_slope, 0)
```

The limit looks like $L=1$.


### Issues with the numeric approach


The numeric approach often gives a good intuition as to the existence of a limit and its value. However, it can be misleading. Consider this limit question:


$$
\lim_{x \rightarrow 0} \frac{1 - \cos(x)}{x^2}.
$$

We can see that it is indeterminate of the form $0/0$:


```{julia}
g(x) = (1 - cos(x)) / x^2
g(0)
```

What is the value of $L$, if it exists? Getting closer to $0$ numerically than the default yields:


```{julia}
lim(g, 0; n=9)
```

Hmm, the values in `ys` appear to be going to $0.5$, but then end up at $0$. Is the limit $0$ or $1/2$? The answer is $1/2$. The last $0$ is an artifact of floating point arithmetic and the last few deviations from `0.5` due to loss of precision in subtraction. To investigate, we look more carefully at the two ratios:


```{julia}
xs = [1/10^i for i in 1:8]
y1s = [1 - cos(x) for x in xs]
y2s = [x^2 for x in xs]
[xs y1s y2s]
```

Looking at the bottom of the second column reveals the error. The value of `1 - cos(1.0e-8)` is `0` and not a value around `5e-17`, as would be expected from the pattern above it. This is because the smallest floating point value less than `1.0` is more than `5e-17` units away, so `cos(1e-8)` is evaluated to be `1.0`. There just isn't enough granularity to get this close to $0$.


Not that we needed to. The answer would have been clear if we had stopped with `x=1e-6` (with `n=6`) say.


In general, some functions will frustrate the numeric approach. It is best to be wary of results. At a minimum they should confirm what a quick graph shows, though even that isn't always enough.



### Richardson extrapolation

The  [`Richardson`](https://github.com/JuliaMath/Richardson.jl)  package can numerically compute limits of a wide class of functions in a way that avoids numeric issues, as above.


```{julia}
using Richardson
f(x) = (1 - cos(x)) / x^2
c, h0 = 0, 1
extrapolate(f, h0; x0=c)
```

The value `h0=1` in this case is just a starting point of the algorithm and is near `x0` but otherwise need not be worried about. (It is like the value in the left column of the first line output by `lim` for a right-hand limit, not the "going to" part.) In the answer the first value is the estimated limit, the second an error that is used to track when the computed values differ from the mathematically expected values.

Let $f_0(x) = f(x)$. The basic [algorithm](https://en.wikipedia.org/wiki/Richardson_extrapolation) assumes we can express the value for $L$ in a form such as:


$$
L = f_0(h) + a_0 h^{k_0} + a_1 h^{k_1} + a_2 h^{k_2} + \text{error}.
$$

Such a form is known as a series expansion; later the Taylor series expansion will be presented.
The error is understood to depend on $h^{k_3}$ in a more precise way than we describe here. But the key is that the $L - f_0(h)$ is not zero and the most important term in how far from zero is $a_0 h^{k_0}$. The trick of Richardson is to evaluate the above at $h/t$ for some $t$ to get another estimate for $L$, then multiply this new estimate by $t^{k_0}$, and then subtract the old estimate to get:

$$
\begin{align*}
(t^{k_0} - 1) L &= (t^{k_0} f_0\left(\frac{h}{t}\right) - f_0(h)) \\
&+ (t^{k_0} a_0 \left(\frac{h}{t}\right)^{k_0} - a_0 h^{k_0}) \\
&+ (t^{k_0} a_1 \left(\frac{h}{t}\right)^{k_1} - a_1 h^{k_1}) \\
&+ (t^{k_0} a_2 \left(\frac{h}{t}\right)^{k_2} - a_2 h^{k_2}) + \text{error}
\end{align*}
$$

The rationale for this choice is that the second line, with $a_0$, will cancel out leaving a new expression for $L$:

$$
L = f_1(h) + \tilde{a}_1 h^{k_1} + \tilde{a}_2 h^{k_2} + \text{error}
$$

where

$$
f_1(h) = \frac{t^{k_0} f_0\left(\frac{h}{t}\right) - f_0(h)}{t^{k_0} - 1}.
$$

This changes the important term in the difference between $L$ and $f_1(h)$ to an expression in $h^{k_1}$, a presumably smaller term. This process can be repeated to produce $f_2$, $f_3$, etc. However, as seen, even if mathematically this can get better an better, it may not be the case computationally. But these differences between the computational and
their mathematical expectation can be evaluated so that the `extrapolate` function can stop its work before issues arise.

##### Example


Some problems are beyond a numeric approach. Let $h(x)$ be defined by


$$
h(x) = x^2 + 1 + \log(\lvert 11 \cdot x - 15 \rvert)/99.
$$

The question is to investigate


$$
\lim_{x \rightarrow 15/11} h(x)
$$

The plot in @fig-k-obryants-sneaky-function shows the answer appears to be straightforward.

::: {#fig-k-obryants-sneaky-function}
```{julia}
h(x) = x^2 + 1 + log(abs(11*x - 15))/99
plot(h, 15/11 - 1, 15/11 + 1)
```

Plot of a function with the property that it has a vertical asymptote with  left and right limits of $-\infty$ and yet the function is never negative when evaluated at a 64-bit floating point number
:::

Taking values near $15/11$ shows nothing perhaps too unusual:


```{julia}
#| hold: true
c = 15/11
lim(h, c; n = 16)
```

(Though the graph and table do hint at something a bit odd---the graph shows a blip, the table doesn't show values in the second column going towards a specific value.)


However the limit in this case is $-\infty$ (or DNE), as there is an asymptote at $c=15/11$. The problem is the asymptote due to the logarithm is extremely narrow and happens between floating point values to the left and right of $15/11$.


## Symbolic approach to limits


The `SymPy` package provides a `limit` function for finding the limit of an expression in a given variable. It must be loaded, as was done initially. The `limit` function's use requires the expression, the variable and a value for $c$. (Similar to the three things in the notation $\lim_{x \rightarrow c}f(x)$.)


For example, the limit at $0$ of $(1-\cos(x))/x^2$ is easily handled:


```{julia}
@syms x::real
limit((1 - cos(x)) / x^2, x => 0)
```

The pair notation (`x => 0`) is used to indicate the variable and the value it is going to. In the next section we introduce the `dir` keyword argument.


##### Example


We look again at the function which despite having a vertical asymptote at $x=15/11$ has the property that it is positive for all floating point values, making both a numeric and graphical approach impossible:


$$
h(x) = x^2 + 1 + \log(\lvert 11 \cdot x - 15 \rvert)/99.
$$

We find the limit symbolically at $c=15/11$ as follows, taking care to use the exact value `15//11` and not the *floating point* approximation returned by `15/11`:


```{julia}
#| hold: true
h(x) = x^2 + 1 + log(abs(11x - 15))/99
limit(h(x), x => 15 // 11)
```

##### Example


Find the [limits](http://en.wikipedia.org/wiki/L%27H%C3%B4pital%27s_rule):


$$
\lim_{x \rightarrow 0} \frac{2\sin(x) - \sin(2x)}{x - \sin(x)}, \quad
\lim_{x \rightarrow 0} \frac{e^x - 1 - x}{x^2}, \quad
\lim_{\rho \rightarrow 1} \frac{x^{1-\rho} - 1}{1 - \rho}.
$$

We have for the first:^[This and the next two `limit` calls should have `dir="+-"` specified, as the default of SymPy is to only compute a limit from the right side of `c`.]



```{julia}
limit( (2sin(x) - sin(2x)) / (x - sin(x)), x => 0)
```



The second is similarly done, though here we define a function for variety:


```{julia}
#| hold: true
f(x) = (exp(x) - 1 - x) / x^2
limit(f(x), x => 0)
```

Finally, for the third we define a new variable and proceed:


```{julia}
@syms rho::real
limit( (x^(1-rho) - 1) / (1 - rho), rho => 1)
```

This last limit demonstrates that the `limit` function of `SymPy` can readily evaluate limits that involve parameters, though at times some assumptions on the parameters may be needed, as was done through `rho::real`.


However, for some cases, the assumptions will not be enough, as they are broad. (E.g., something might be true for some values of the parameter and not others and these values aren't captured in the assumptions.)  So the user must be mindful that when parameters are involved, the answer may not reflect all possible cases.


##### Example: floating point conversion issues


The Gruntz [algorithm](http://www.cybertester.com/data/gruntz.pdf) implemented in `SymPy` for symbolic limits is quite powerful. However, some care must be exercised to avoid undesirable conversions from exact values to floating point values.


In a previous example, we used `15//11` and not `15/11`, as the former converts to an *exact* symbolic value for use in `SymPy`, but the latter would be approximated in floating point *before* this conversion so the exactness would be lost.


To illustrate further, let's look at the limit as $x$ goes to $\pi/2$ of $j(x) = \cos(x) / (x - \pi/2)$. We follow our past practice:


```{julia}
j(x) = cos(x) / (x - pi/2)
j(pi/2)
```

The value is not `NaN`, rather `Inf`. This is because `cos(pi/2)` is not exactly $0$ as it should be mathematically, as `pi/2` is rounded to a floating point number. This minor difference is important. If we try and correct for this by using `PI` we have:


```{julia}
limit(j(x), x => PI/2)
```

The value is not right, as this simple graph (@fig-simple-graph-cosx-over-x-minus-pi-over-2) suggests the limit is in fact $-1$:

::: {#fig-simple-graph-cosx-over-x-minus-pi-over-2}

```{julia}
#| echo: false
plot(j,  pi/4, 3pi/4)
```

Plot of $f(x) = \cos(x) / (x - \pi/2)$ over $[-\pi/4, 3\pi/4]$
:::

The difference between `pi` and `PI` can be significant, and though usually `pi` is silently converted to `PI`, it doesn't happen here as the division by `2` happens first, which turns the symbol into an approximate floating point number. Hence, `SymPy` is giving the correct answer for the problem it is given, it just isn't the problem we wanted to look at.


Trying again, being more aware of how `pi` and `PI` differ, we have:


```{julia}
#| hold: true
j(x) = cos(x) / (x - PI/2)
limit(j(x), x => PI/2)
```



## Rules for limits


The `limit` function doesn't compute limits from the definition, rather it applies some known facts about functions within a set of rules. Some of these rules are the following.

::: {.relationship title="Rules for limits"}

Suppose the individual limits of $f$ and $g$  exist (and are finite) on the right-hand sides below. Then:

$$
\begin{align*}
\lim_{x \rightarrow c} (a \cdot f(x) + b \cdot g(x)) &= a \cdot
  \lim_{x \rightarrow c} f(x) + b \cdot \lim_{x \rightarrow c} g(x)
  &\\
%%
\lim_{x \rightarrow c} f(x) \cdot g(x) &= \lim_{x \rightarrow c}
  f(x) \cdot \lim_{x \rightarrow c} g(x)
  &\\
%%
\lim_{x \rightarrow c} \frac{f(x)}{g(x)} &=
  \frac{\lim_{x \rightarrow c} f(x)}{\lim_{x \rightarrow c} g(x)}
  \quad\quad(\text{provided }\lim_{x \rightarrow c} g(x) \neq 0)\\
\end{align*}
$$
:::

These rules are verbally described as follows, when the individual limits exist and are finite then:


* Limits involving sums, differences or scalar multiples of functions *exist* *and* can be *computed* by first doing the individual limits and then combining the answers appropriately.

* Limits of products exist and can be found by computing the limits of the individual factors and then combining.

* Limits of ratios *exist* and can be found by computing the limit of the individual terms and then dividing **provided** you don't divide by $0$. The last part is really important, as this rule is no help with the common indeterminate form $0/0$.


In addition

::: {.relationship title="Limit rule for a composition"}
Consider the limit of a composition:


$$
\lim_{x \rightarrow c} f(g(x)).
$$

Suppose that


* The outer limit, $\lim_{x \rightarrow b} f(x) = L$, exists, and

* the inner limit, $\lim_{x \rightarrow c} g(x) = b$, exists, **and**

* for some neighborhood around $c$ (not including $c$) $g(x)$ is not $b$.


Then the limit exists and equals $L$:


$$
\lim_{x \rightarrow c} f(g(x)) = \lim_{u \rightarrow b} f(u) = L.
$$
:::



An alternative, is to assume $f(x)$ is defined at $b$ and equal to $L$ (which is the definition of continuity), but that isn't the assumption above, hence the need to exclude $g$ from taking on a value of $b$ (where $f$ may not be defined) near $c$.


These limit rules, together with the fact that our basic algebraic functions have limits that can be found by simple evaluation, mean that many limits are easy to compute.


##### Example: composition


For example, consider for some non-zero $k$ the following limit:


$$
\lim_{x \rightarrow 0} \frac{\sin(kx)}{x}.
$$

This is clearly related to the function $f(x) = \sin(x)/x$, which has a limit of $1$ as $x \rightarrow 0$. We see $g(x) = k f(kx)$ is the function in the limit of the question. As $kx \rightarrow 0$, though not taking a value of $0$ except when $x=0$, the limit above is $k \lim_{x \rightarrow 0} f(kx) = k \lim_{u \rightarrow 0} f(u) = k$.


Basically when taking a limit as $x$ goes to $0$ we can multiply $x$ by any constant and figure out the limit for that. (It is as though we "go to" $0$ faster or slower, but are still going to $0$.)


Similarly,


$$
\lim_{x \rightarrow 0} \frac{\sin(x^2)}{x^2} = 1,
$$

as this is the limit of $f(g(x))$ with $f$ as above and  $g(x) = x^2$. We need $x \rightarrow 0$, $g$ is only $0$ at $x=0$, which is the case.


##### Example: products


Consider this more complicated limit found on this [Wikipedia](http://en.wikipedia.org/wiki/L%27H%C3%B4pital%27s_rule) page:


$$
\lim_{x \rightarrow 1/2} \frac{\sin(\pi x)}{\pi x} \cdot \frac{\cos(\pi x)}{1 - (2x)^2}.
$$

We know the first factor has a limit found by evaluation: $2/\pi$, so it is really just a constant. The second we can compute:


```{julia}
l(x) = cos(PI*x) / (1 - (2x)^2)
limit(l(x), x => 1//2)
```

Putting together, we would get $1/2$. Which we could have done directly in this case:


```{julia}
limit(sin(PI*x)/(PI*x) * l(x), x => 1//2)
```

##### Example: ratios


Consider again the limit of $\cos(\pi x) / (1 - (2x)^2)$ at $c=1/2$. A graph of both the top and bottom functions shows the indeterminate, $0/0$, form:

::: {#fig-plot-cos-pix-and-1-minus-2x-squared}
```{julia}
#| echo: false
plot(cos(pi*x), 0.4, 0.6)
plot!(1 - (2x)^2)
```

Plot of $f(x) = \cos(\pi x)$ and $g(x) = 1 - (2x)^2$ over $[0.5 - 0.1, 0.5 + 0.1]$. The plot shows at $x=0.5$ the ratio will be indeterminate.
:::

However, following Euler's insight that $\sin(x)/x$ will have a limit at $0$ of $1$ as $\sin(x) \approx x$, and $x/x$ has a limit of $1$ at $c=0$, in @fig-plot-cos-pi-x-tangent-line-and-1-minus-2x-squared-and-tangent-line we can see that $\cos(\pi x)$ looks like $-\pi\cdot (x - 1/2)$ and $(1 - (2x)^2)$ looks like $-4(x-1/2)$ around $x=1/2$. These lines are "tangent lines".


::: {#fig-plot-cos-pi-x-tangent-line-and-1-minus-2x-squared-and-tangent-line}

```{julia}
#| echo: false

p1 = plot(cos(pi*x), 0.4, 0.6; label = "cos(pi*x)")
plot!(p1, -pi*(x - 1/2); label="tangent line")

p2 = plot(1 - (2x)^2, 0.4, 0.6; label="1 - (2x)^2")
plot!(p2, -4(x - 1/2); label="tangent line")

plot(p1, p2)
```

Plot of $\cos(\pi x)$ and its tangent line and a plot of $1 - (2x)^2$ and its tangent line over $[0.5 - 0.1, 0.5 + 0.1]$
:::

So around $c=1/2$ the ratio of the functions should look like the ratio of the lines or $-\pi (x-1/2) / ( -4(x -1/2)) = \pi/4$, which indeed it does, as that is the limit.


This is the basis of L'Hospital's rule, which we will return to once the derivative is discussed so that a tangent line can be mathematically described.


##### Example: sums


If it is known that the following limit exists by some means:


$$
L = 0 = \lim_{x \rightarrow 0} \frac{e^{\csc(x)}}{e^{\cot(x)}} - (1 + \frac{1}{2}x  + \frac{1}{8}x^2)
$$

Then this limit will exist


$$
M = \lim_{x \rightarrow 0} \frac{e^{\csc(x)}}{e^{\cot(x)}}
$$

Why? We can express the function $e^{\csc(x)}/e^{\cot(x)}$ as the above function plus the polynomial $1 + x/2 + x^2/8$. The above is then the sum of two functions whose limits exist and are finite, hence, we can conclude that $M = 0 + 1$.


### The squeeze theorem

Sometimes limits can be found by bounding more complicated functions by easier to reason about functions.

::: {.theorem title="Theorem: squeeze theorem"}


Fix $c$ in $I=(a,b)$. Suppose for all $x$ in $I$, except possibly $c$, there are two functions  $l$ and $u$, satisfying:


* $l(x) \leq f(x) \leq u(x)$.

* These limits exist and are equal:

$$
L = \lim_{x \rightarrow c} l(x) = \lim_{x \rightarrow c} u(x).
$$


Then

$$
\lim_{x\rightarrow c} f(x) = L.
$$

The functions $l$ and $u$ [squeeze](http://en.wikipedia.org/wiki/Squeeze_theorem)) $f$.
:::

The figure  shows a usage of the squeeze theorem to show $\sin(x)/x \rightarrow 1$ as $\cos(x) \leq \sin(x)/x \leq 1$ for $x$ close to $0$.

::: {#fig-animation-for-square-theorem-cos-sinx-over-x}
```{julia}
#| hold: true
#| echo: false
gr()
function squeeze_example(x)
	x₀ = 0.5
	plot(cos, 0, x₀, label="cos")
	plot!(x -> sin(x)/x, label = "sin(x)/x")
	plot!(x -> 1, label = "y=1")
	plot!([x,x], [ cos(x₀), 1], linestyle=:dash, label="")
	scatter!([x,x,x], [cos(x), sin(x)/x, 1], label="")
end

anim = @animate for x ∈ (0.4, 0.3, 0.2, 0.1, 0.05, 0.01)
    squeeze_example(x)
end

imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 1)


caption = ""
plotly()
ImageFile(imgfile, caption)
```

As $x$ goes to $0$, the values of $\sin(x)/x$ are squeezed between the values of $\cos(x)$ and $1$, and both functions  converge to $1$.
:::

## Limits from the definition


The formal definition of a limit involves clarifying what it means for $f(x)$ to be "close to $L$" when $x$ is "close to $c$". These are quantified by the inequalities $0 < \lvert x-c\rvert < \delta$ and the $\lvert f(x) - L\rvert < \epsilon$. The second does not have the restriction that it is greater than $0$, as indeed $f(x)$ can equal $L$. The order is important: it says for any idea of close for $f(x)$ to $L$, an idea of close must be found for $x$ to $c$.



The key is identifying a value for $\delta$  for a given value of $\epsilon$.

::: {#fig-limit-e-d}
```{julia}
#| echo: false
plt = let
    gr()
	f(x) = (x+1)^2 -1
	a, b = -1/4, 2
	c = 1
	L = f(c)
	δ = 0.2
	ϵ = 3sqrt(δ)

	plot(; empty_style...)#, aspect_ratio=:equal)
	plot!(f, a, b; line=(:black, 2))
	plot!([a,b],[0,0]; axis_style...)
	plot!([0,0], [f(a), f(2)]; axis_style...)

	plot!([c, c, 0], [0, f(c), f(c)]; line=(:black, 1, :dash))
	plot!([c-δ, c-δ, 0], [0, f(c-δ), f(c-δ)]; line=(:black, 1, :dashdot))
	plot!([c+δ, c+δ, 0], [0, f(c+δ), f(c+δ)]; line=(:black, 1, :dashdot))

	S = Shape([0,b,b,0],[L-ϵ,L-ϵ,L+ϵ,L+ϵ])
	plot!(S; fill=(:lightblue, 0.25), line=(nothing,))

	domain_color=:red
    range_color=:blue


	S = Shape([c-δ, c+δ, c+δ, c-δ], 0.05*[-1,-1,1,1])
	plot!(S, fill=(domain_color,0.4), line=nothing)
	m,M = f(c-δ), f(c+δ)
	T = Shape(0.015 * [-1,1,1,-1], [m,m,M,M])
	plot!(T, fill=(range_color, 0.4), line=nothing)


    C = Plots.scale(Shape(:circle), 0.02, 0.1)
	plot!(Plots.translate(C, c, L), fill=(:white,1,0), line=(:black, 1))
	plot!(Plots.translate(C, c, 0), fill=(:white,1,0), line=(domain_color, 1))
	plot!(Plots.translate(C, 0, L), fill=(:white,1,0), line=(range_color, 1))

	annotate!([
	(c, 0, text(L"c", :top)),
		(c-δ, 0, text(L"c - \delta", 10, :top)),
		(c+δ, 0, text(L"c + \delta", 10, :top)),
		(0, L, text(L"L", :right)),
		(0, L+ϵ, text(L"L + \epsilon", 10, :right)),
		(0, L-ϵ, text(L"L - \epsilon", 10, :right)),

	])

end
plt
```

```{julia}
#| echo: false
plotly()
nothing
```

Figure illustrating requirements of $\epsilon-\delta$ definition of the limit. The image (shaded red on $y$ axis) of the $x$ within $\delta$ of $c$ (except for $c$ and shaded blue on the $x$ axis) must stay within the bounds of $L-\epsilon$ and $L+ \epsilon$, where $\delta$ may be chosen based on $\epsilon$ but needs to be chosen for every positive $\epsilon$, not just a fixed one.
:::

A simple case is the linear case. Consider the function $f(x) = 3x + 2$. Verify that the limit at $c=1$ is $5$.


We show "numerically" that $\delta = \epsilon/3$.


```{julia}
#| hold: true
f(x) = 3x + 2
c, L = 1, 5
epsilon = rand()                  # some number in (0,1)
delta = epsilon / 3
xs = c .+ delta * rand(100)       # 100 numbers, c < x < c + delta
as = [abs(f(x) - L) < epsilon for x in xs]
all(as)                           # are all the as true?
```

These lines produce a random $\epsilon$, the resulting $\delta$, and then verify for 100 numbers within $(c, c+\delta)$ that the inequality $\lvert f(x) - L \rvert < \epsilon$ holds for each. Running them again and again should always produce `true` if $L$ is the limit and $\delta$ is chosen properly. Of course, we should also verify values to the left of $c$.


(The random numbers are technically in $[0,1)$, so in theory `epsilon` could be `0`. So the above approach would be more solid if some guard, such as  `epsilon = max(eps(), rand())`, was used. As the formal definition is the domain of paper-and-pencil, we don't fuss.)


In this case, $\delta$ is easy to guess, as the function is linear and has slope $3$. This basically says the $y$ scale is 3 times the $x$ scale. For non-linear functions, finding $\delta$ for a given $\epsilon$ can be more of a challenge.


##### Example

We show using the definition that for any fixed $a$ and $n$:
$$
\lim_{x \rightarrow a} x^n = a^n.
$$

This proof uses a bound based on properties of the absolute value.


We look at $f(x) - L = x^n - a^n = (x-a)(x^{n-1} + x^{n-2}a + \cdots + x^1a^{n-1} + a^n)$.

Taking absolute values gives an inequality by the triangle inequality:

$$
\lvert x^n - a^n\rvert \leq \lvert x-a\rvert\cdot
\left(
\lvert x^{n-1}\rvert +
\lvert x^{n-2}\rvert\lvert a\rvert +
\cdots +
\lvert x^1\rvert\lvert a^{n-1}\rvert +
\lvert a^n\rvert
\right).
$$

Now, for a given $\epsilon>0$ we seek a $\delta>0$ satisfying the properties of the limit definition for $f(x) = x^n$ and $L=a^n$. For now, assume $\delta < 1$. Then we can assume $\lvert x-a\rvert < \delta$ and

$$
\lvert x\rvert = \lvert x - a + a\rvert \leq \lvert x-a\rvert + \lvert a\rvert < 1 + \lvert a\rvert
$$

This says then

$$
\begin{align*}
\lvert x^n - a^n\rvert
&\leq
\lvert x-a\rvert\cdot \left(
\lvert x\rvert^{n-1} +
\lvert x\rvert^{n-2}\lvert a\rvert +
\cdots +
\lvert x\rvert^1\lvert a^{n-1}\rvert +
\lvert a^n\rvert
\right)\\
%%
&\leq \lvert x - a\rvert
\cdot \left(
(\lvert a\rvert+1)^{n-1} +
(\lvert a\rvert+1)^{n-2}\lvert a\rvert
+ \cdots +
(\lvert a\rvert+1)^1 \lvert a^{n-1}\rvert +
\lvert a^n \rvert
\right)\\
&\leq \lvert x-a\rvert \cdot C,
\end{align*}
$$

where $C$ is just some constant not depending on $x$, just $a$ and $n$.

Now if $\delta < 1$ and $\delta < \epsilon/C$  and if
$0 < \lvert x - a \rvert < \delta$ then

$$
\lvert f(x) - L \rvert =
\lvert x^n - a^n\rvert \leq \lvert x-a\rvert \cdot C \leq \delta\cdot C < \frac{\epsilon}{C} \cdot C = \epsilon.
$$

With this result, the rules of limits can immediately extend this to any polynomial, $p(x),$ it follows that $\lim_{x \rightarrow c} p(x) = p(a)$. (Because $c_n x^n \rightarrow c_n a^n$ and the sum of two functions with a limit has the limit of the sums.) Based on this, we will say later that any polynomial is *continuous* for all $x$.


## Questions


###### Question


From the graph in @fig-limit-rational-function-x-squared-minus-3x-plus-2-over-x-squared-minus-6x-plua-5, find the limit:


$$
L = \lim_{x\rightarrow 1}  \frac{x^2−3x+2}{x^2−6x+5}
$$

::: {#fig-limit-rational-function-x-squared-minus-3x-plus-2-over-x-squared-minus-6x-plua-5}
```{julia}
#| hold: true
#| echo: false
f(x) = (x^2 - 3x +2) / (x^2 - 6x + 5)
plot(f, 0,2)
```

Plot of $f(x)$ over $[1-1, 1 + 1]$.
:::

```{julia}
#| hold: true
#| echo: false
answer = 1/4
numericq(answer, 1e-1)
```

###### Question


From the graph, find the limit $L$:


$$
L = \lim_{x \rightarrow -2} \frac{x}{x+1} \frac{x^2}{x^2 + 4}
$$

::: {#fig-plot-x-over-x-plus-1-times-x-squared-over-x-squared-plus-4}
```{julia}
#| hold: true
#| echo: false
f(x) = x/(x+1)*x^2/(x^2+4)
plot(f, -3, -1.25)
```

Plot of $f(x)$ over $[-3, -1.25]$
:::

```{julia}
#| hold: true
#| echo: false
f(x) = x/(x+1)*x^2/(x^2+4)
val = f(-2)
numericq(val, 1e-1)
```

###### Question


Graphically investigate the limit of


$$
L = \lim_{x \rightarrow 0} \frac{e^x - 1}{x}.
$$

using @fig-exp-x-minus-1-over-x. What is the value of $L$?

::: {#fig-exp-x-minus-1-over-x}
```{julia}
#| hold: true
#| echo: false
f(x) = (exp(x) - 1)/x
p = plot(f, -1, 1)
```
Plot of $f(x) = (e^x - 1)/x$ over $[-1,1]$
:::

```{julia}
#| hold: true
#| echo: false
val = N(limit((exp(x)-1)/x, x => 0))
numericq(val, 1e-1)
```

###### Question


Graphically investigate the limit


$$
\lim_{x \rightarrow 0} \frac{\cos(x) - 1}{x}.
$$

The limit exists, what is the value?


```{julia}
#| hold: true
#| echo: false
val = 0
numericq(val, 1e-2)
```

###### Question


In @fig-which-graph-has-no-limit-at-a select the graph for which there is no limit at $a$.

::: {#fig-which-graph-has-no-limit-at-a}
```{julia}
#| hold: true
#| echo: false
gr()
let
    p1 = plot(;axis=nothing, legend=false)
    title!(p1, "(a)")
    plot!(p1, x -> x^2, 0, 2, color=:black)
    plot!(p1, zero, linestyle=:dash)
    annotate!(p1,[(1,0,text(L"a",:top))])

    p2 = plot(;axis=nothing, legend=false)
    title!(p2, "(b)")
    plot!(p2, x -> 1/(1-x), 0, .95, color=:black)
    plot!(p2, x-> -1/(1-x), 1.05, 2, color=:black)
    plot!(p2, zero, linestyle=:dash)
    annotate!(p2,[(1,0,text(L"a",:top))])

    p3 = plot(;axis=nothing, legend=false)
    title!(p3, "(c)")
    plot!(p3, sinpi, 0, 2, color=:black)
    plot!(p3, zero, linestyle=:dash)
    annotate!(p3,[(1,0,text("a",:top))])

    p4 = plot(;axis=nothing, legend=false)
    title!(p4, "(d)")
    plot!(p4, x -> x^x, 0, 2, color=:black)
    plot!(p4, zero, linestyle=:dash)
    annotate!(p4,[(1,0,text(L"a",:top))])

    l = @layout[a b; c d]
    p = plot(p1, p2, p3, p4, layout=l)
    imgfile = tempname() * ".png"
    savefig(p, imgfile)
    hotspotq(imgfile, (1/2,1), (1/2,1))
end
```

Select the graph with no limit at $a$
:::

```{julia}
#| echo: false
plotly();
```

###### Question


The following limit is commonly used:


$$
\lim_{h \rightarrow 0} \frac{e^{x + h} - e^x}{h} = L.
$$

Factoring out $e^x$ from the top and using rules of limits this becomes,


$$
L = e^x \lim_{h \rightarrow 0} \frac{e^h - 1}{h}.
$$

What is $L$?


```{julia}
#| hold: true
#| echo: false
choices = ["``0``", "``1``", "``e^x``"]
answer = 3
radioq(choices, answer)
```

###### Question


The following limit is commonly used:


$$
\lim_{h \rightarrow 0} \frac{\sin(x + h) - \sin(x)}{h} = L.
$$

The answer should depend on $x$, though it is possible it is a constant.  Using a double angle formula and the rules of limits, this can be written as:


$$
L = \cos(x) \lim_{h \rightarrow 0}\frac{\sin(h)}{h} + \sin(x) \lim_{h \rightarrow 0}\frac{\cos(h)-1}{h}.
$$

Using the last result, what is the value of $L$?


```{julia}
#| hold: true
#| echo: false
choices = ["``\\cos(x)``", "``\\sin(x)``", "``1``", "``0``", "``\\sin(h)/h``"]
answer = 1
buttonq(choices, answer)
```

###### Question


Find the limit as $x$ goes to $2$ of


$$
f(x) = \frac{3x^2 - x -10}{x^2 - 4}
$$

```{julia}
#| hold: true
#| echo: false
f(x) = (3x^2 - x - 10)/(x^2 - 4);
val = convert(Float64, N(limit(f(x), x => 2)))
numericq(val)
```

###### Question


Find the limit as $x$ goes to $-2$ of


$$
f(x) = \frac{\frac{1}{x} + \frac{1}{2}}{x^3 + 8}
$$

```{julia}
#| hold: true
#| echo: false
f(x) = ((1/x) + (1/2))/(x^3 + 8)
numericq(-1/48, .001)
```

###### Question


Find the limit as $x$ goes to $27$ of


$$
f(x) = \frac{x - 27}{x^{1/3} - 3}
$$

```{julia}
#| hold: true
#| echo: false
f(x) = (x - 27)/(x^(1//3) - 3)
val = N(limit(f(x), x => 27))
numericq(val)
```

###### Question


Find the limit


$$
L = \lim_{x \rightarrow \pi/2} \frac{\tan (2x)}{x - \pi/2}
$$

```{julia}
#| hold: true
#| echo: false
f(x) = tan(2x)/(x-PI/2)
val = N(limit(f(x), x => PI/2))
numericq(val)
```

###### Question


The limit of $\sin(x)/x$ at $0$ has a numeric value. This depends upon the fact that $x$ is measured in radians. Try to find this limit: `limit(sind(x)/x, x => 0)`. What is the value?


```{julia}
#| hold: true
#| echo: false
choices = [q"0", q"1", q"pi/180", q"180/pi"]
answer = 3
radioq(choices, answer)
```

What is the limit `limit(sinpi(x)/x, x => 0)`?


```{julia}
#| hold: true
#| echo: false
choices = [q"0", q"1", q"pi", q"1/pi"]
answer = 3
radioq(choices, answer)
```

###### Question: limit properties


There are several properties of limits that allow one to break down more complicated problems into smaller subproblems. For example,


$$
\lim (f(x) + g(x)) = \lim f(x) + \lim g(x)
$$

is notation to indicate that one can take a limit of the sum of two function or take the limit of each first, then add and the answer will be unchanged, provided all the limits in question exist.


Use one or the either to find the limit of $f(x) = \sin(x) + \tan(x) + \cos(x)$ as $x$ goes to $0$.


```{julia}
#| hold: true
#| echo: false
f(x) = sin(x) + tan(x) + cos(x)
numericq(f(0), 1e-5)
```

###### Question


The key assumption made above in being able to write


$$
\lim_{x\rightarrow c} f(g(x)) = L,
$$

when $\lim_{x\rightarrow b} f(x) = L$ and $\lim_{x\rightarrow c}g(x) = b$ is *continuity*.


This [example](https://en.wikipedia.org/wiki/Limit_of_a_function#Limits_of_compositions_of_functions) shows why it is important.


Take


$$
f(x) = \begin{cases}
0 &~ x \neq 0\\
1 &~ x = 0
\end{cases}
$$

We have $\lim_{x\rightarrow 0}f(x) = 0$, as $0$ is clearly a removable discontinuity. So were the above applicable we would have $\lim_{x \rightarrow 0}f(f(x)) = 0$. But this is not true. What is the limit at $0$ of $f(f(x))$?


```{julia}
#| hold: true
#| echo: false
numericq(1)
```

###### Question


Does this function have a limit as $h$ goes to $0$ from the right (that is, assume $h>0$)?


$$
\frac{h^h - 1}{h}
$$

```{julia}
#| hold: true
#| echo: false
choices = [
"Yes, the value is `-9.2061`",
"Yes, the value is `-11.5123`",
"No, the value heads to negative infinity"
];
answer = 3;
buttonq(choices, answer)
```

###### Question


Compute the limit


$$
\lim_{x \rightarrow 1} \frac{x}{x-1} - \frac{1}{\ln(x)}.
$$

```{julia}
#| hold: true
#| echo: false
f(x) = x/(x-1) - 1/log(x)
val = convert(Float64, N(limit(f(x), x => 1)))
numericq(val)
```

###### Question


Compute the limit


$$
\lim_{x \rightarrow 1/2} \frac{1}{\pi} \frac{\cos(\pi x)}{1 - (2x)^2}.
$$

```{julia}
#| hold: true
#| echo: false
f(x) =  1/PI * cos(PI*x)/(1 - (2x)^2)
val = convert(Float64, N(limit(f(x), x => 1//2)))
numericq(val)
```

###### Question


Compute the limit


$$
\lim_{x \rightarrow 0} \frac{x\sin(\sin(x)) - \sin^2(x)}{x^6}.
$$

```{julia}
#| hold: true
#| echo: false
f(x) =  (x * sin(sin(x))- sin(x)^2)/x^6
val = convert(Float64, N(limit(f(x), x => 0)))
numericq(val)
```


###### Question


Compute the limit


$$
\lim_{x \rightarrow 0} \frac{\tan(x) - 24  \tan(x/2)}{4  \sin(x) - 5  x}.
$$

```{julia}
#| hold: true
#| echo: false
f(x) = (tan(x) - 24 * tan(x/2)) / (4 * sin(x) - 5 * x)
val = N(limit(f(x), x => 0))
numericq(val)
```

###### Question


Some limits involve parameters. For example, suppose we define `ex` as follows:


```{julia}
#| hold: true
@syms m::real k::real
ex = (1 + k*x)^(m/x)
```

What is `limit(ex, x => 0)`?


```{julia}
#| hold: true
#| echo: false
choices = ["``e^{km}``", "``e^{k/m}``", "``k/m``", "``m/k``", "``0``"]
answer = 1
radioq(choices, answer)
```

###### Question


For a given $a$, what is


$$
L = \lim_{x \rightarrow 0+} (1 + a\cdot (e^{-x} -1))^{(1/x)}
$$

```{julia}
#| hold: true
#| echo: false
choices = ["``e^{-a}``", "``e^a``", "``a``", "``L`` does not exist"]
radioq(choices, 1)
```

###### Question


For positive integers $m$ and $n$ what is


$$
\lim_{x \rightarrow 1} \frac{x^{1/m}-1}{x^{1/n}-1}?
$$

```{julia}
#| hold: true
#| echo: false
choices = ["``m/n``", "``n/m``", "``mn``", "The limit does not exist"]
radioq(choices, 2)
```

###### Question


What does `SymPy` find for the limit of `ex` (`limit(ex, x => 0)`), as defined here:


```{julia}
#| hold: true
@syms x a
ex = (a^x - 1)/x
```

```{julia}
#| hold: true
#| echo: false
choices = ["``\\log(a)``", "``a``", "``e^a``", "``e^{-a}``"]
radioq(choices, 1)
```

Should `SymPy` have needed an assumption like


```{julia}
@syms a::positive
```

```{julia}
#| hold: true
#| echo: false
yesnoq("yes")
```

###### Question

The limit

$$
L= \lim_{x \rightarrow 0} \left(\frac{a^x - x \log(a)}{b^x - x\log(b)}\right)^{1/x^2}
$$

For $a=3$ and $b=2$

Can be computed symbolically *two* different ways:

```{julia}
@syms x
a, b = 3, 2
f(x) = ((a^x - x*log(a))/(b^x - x*log(b)))^(1/x^2)
limit(f(x), x=>0)
```

*or*

```{julia}
@syms x a b
f(x) = ((a^x - x*log(a))/(b^x - x*log(b)))^(1/x^2)
L = limit(f(x), x=>0)
L(a => 3, b=>2)
```

Which is correct?

```{julia}
#| echo: false
choices = ["The first one", "The second one"]
explanation = """
The first one is incorrect, as `log(3)` is evaluated numerically, and not symbolically. The difference between a floating point approximation and the symbolic expression is enough to make the first limit infinite, despite the actual limit being finite.
"""
buttonq(choices, 2; explanation=explanation)
```



###### Question: The squeeze theorem


Let's look at the function $f(x) = x \sin(1/x)$. @fig-squeeze-theorem-x-sin-1-over-x-and-abs-x shows the graph of the function near $0$ and also the graph of $\lvert x \rvert$ and $-\lvert x \rvert$.

::: {#fig-squeeze-theorem-x-sin-1-over-x-and-abs-x}
```{julia}
#| hold: true
#| echo: false
f(x) = x == 0 ? NaN : x * sin(1/x)
c, delta = 0, 1/4
plot(f, c - delta, c + delta; legend=false, line=(:blue,))
plot!(abs; line=(:black,))
plot!(x -> -abs(x); line=(:black,))
```

Plot of $f(x) = x \sin(1/x)$ over $[-1/4, 1/4]$ along with $\lvert x\rvert$ and $-\lvert x \rvert$.
:::


This graph clearly oscillates near $0$. To the graph of $f$, we added graphs of both
$g(x) = \lvert x\rvert$ and $h(x) = - \lvert x\rvert$.
From the graph in @fig-squeeze-theorem-x-sin-1-over-x-and-abs-x it is easy to see by the "squeeze theorem" that the limit at $x=0$ is $0$. Why?

```{julia}
#| hold: true
#| echo: false
choices=[L"""The functions $g$ and $h$ both have a limit of $0$ at $x=0$ and the function $f$ is in
between both $g$ and $h$, so must to have a limit of $0$.
""",
	     L"The functions $g$ and $h$ squeeze each other as $g(x) > h(x)$",
         L"The function $f$ has no limit - it oscillates too much near $0$"]
answer = 1
buttonq(choices, answer)
```

(The [Wikipedia](https://en.wikipedia.org/wiki/Squeeze_theorem) entry for the squeeze theorem has this unverified, but colorful detail:


> In many languages (e.g. French, German, Italian, Hungarian and Russian), the squeeze theorem is also known as the two policemen (and a drunk) theorem, or some variation thereof. The story is that if two policemen are escorting a drunk prisoner between them, and both officers go to a cell, then (regardless of the path taken, and the fact that the prisoner may be wobbling about between the policemen) the prisoner must also end up in the cell.



###### Question


Archimedes, in finding bounds on the value of $\pi$ used $n$-gons with sides $12, 24, 48,$ and $96$. This was so the trigonometry involved could be solved exactly for the interior angles (e.g. $n=12$ is an interior angle of $\pi/6$ which has `sin` and `cos` computable by simple geometry. See [Damini and Abhishek](https://arxiv.org/pdf/2008.07995.pdf)) These exact solutions led to subsequent bounds. A more modern approach to bound the circumference of a circle of radius $r$ using a $n$-gon with interior angle $\theta$ would be to use the trigonometric functions. An upper bound would be found with (using the triangle with angle $\theta/2$, opposite side $x$ and adjacent side $r$:


```{julia}
@syms theta::real r::real
```

```{julia}
#| hold: true
x = r * tan(theta/2)
n = 2PI/theta     # using PI to avoid floating point roundoff in 2pi
# C < n * 2x
upper = n*2x
```

A lower bound would use the triangle with angle $\theta/2$, hypotenuse $r$ and opposite side $x$:


```{julia}
#| hold: true
x = r*sin(theta/2)
n = 2PI/theta
# C > n * 2x
lower = n*2x
```

Using the above, find the limit of `upper` and `lower`. Are the two equal and equal to a familiar value?


```{julia}
#| hold: true
#| echo: false
yesnoq("yes")
```

(If so, then the squeeze theorem would say that $\pi$ is the common limit.)
