Review for test 2 in MTH 229

[Download this file as an IJulia notebook here.]

Test 2 will cover the 5th, 6th, and 7th projects: limits, derivatives, and the first and second derivatives. This review workbook serves two purposes:

To begin, you should "run all" so that these packages are loaded into your Julia session.

using Plots, Roots, SymPy

Limits

We discussed three ways to investigate the answer (if it exists) to

$$~ \lim_{x \rightarrow c} f(x) ~$$
function lim(f::Function, c::Real; n::Int=6, dir="+")
     hs = [(1/10)^i for i in 1:n] # close to 0
     if dir == "+"
       xs = c + hs 
     else
       xs = c - hs
     end
     ys = map(f, xs)
     [xs ys]
end
lim (generic function with 1 method)

With this, making a table to investigate a limit is as easy as:

f(x) = x^x
lim(f, 0, dir="+") ## going to 0 from right
6x2 Array{Float64,2}:
 0.1     0.794328
 0.01    0.954993
 0.001   0.993116
 0.0001  0.999079
 1.0e-5  0.999885
 1.0e-6  0.999986

Graphically, we see the same by looking at what happens to the $y$ values as we get close to 0 with the $x$ value:

plot(f, 0, 2)
-2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 -2.0 -1.9 -1.8 -1.7 -1.6 -1.5 -1.4 -1.3 -1.2 -1.1 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 -2 0 2 4 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 y1 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0 5.2 5.4 5.6 5.8 6.0 6.2 6.4 6.6 6.8 7.0 7.2 7.4 7.6 7.8 8.0 -5 0 5 10 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0

Finally, symbolic limits can be done with the add-on SymPy package:

limit(f, 0)
$$1$$

Derivatives

We discussed three ways to find derivatives in Julia:

We can wrap this in a function, but perhaps it is best to just compute it. For example, for the function $f(x) = x - \log(x)$, we have the derivative at $2$ is approximately:

f(x) = x - log(x)
c = 2
h = 1e-6
(f(c+h) - f(c)) / h
0.5000001253030462
Base.ctranspose(f::Function) = D(f)
ctranspose (generic function with 40 methods)

With this, the above derivative is found with:

f'(c)
0.5

Second derivative are found by using two primes (watch out for IJulia):

f''(c)
0.25
diff(f)
$$1 - \frac{1}{x}$$

At $c=2$, we can see this will also be $1/2$.

First and second derivatives

We discussed this rough table:

f$+$$\nearrow$$\smile$
f'.$+$$\nearrow$
f''..$+$

It helps us understand two "tests":

The first derivative test states that for a differentiable function $f(x)$ with a critical point at $c$ then if $f′(x)$ changes sign from + to − at c then $f(c)$ is a local maximum and if it changes sign from − to + then $f(c)$ is a local minimum

The second derivative test states that if $c$ is a critical point of $f(x)$ and $f″(c)>0$ then $f(c)$ is a local minimum and if $f″(c)<0$ then $f(c)$ is a local maximum.

Where a critical point is a value in the domain of $f(x)$ where the derivative is $0$ or undefined. In contrast, an inflection point is a point where $f''(x)$ is $0$ or undefined and the second derivative changes sign at this point.

To find these we used fzero and fzeros. The function fzero(f, a, b) will find a zero of $f(x)$ within a bracketing interval $[a,b]$. The function fzeros(f, a, b) will try to find the zeros of $f$ between $[a,b]$. (It may miss zeros that do not cross the $x$ axis!)

For example, if $f(x) = \cos(x) - \log(x)$ on $(0,10)$ we have the critical points found with:

f(x) = cos(x) - log(x)
fzeros(f', 0, 10)
3-element Array{Float64,1}:
 3.43683
 6.11902
 9.5299 

(The function is differentiable on this interval.)

The inflection points are given by

fzeros(f'', 0, 10)
2-element Array{Float64,1}:
 4.7566
 7.8377

(which requires verifying the second derivative changes sign at these two points.)

This function was helpful in graphical explorations:

plotif(f, g, a, b) =  plot([f, x -> g(x) > 0.0 ? f(x) : NaN], a, b, linewidth=5)
plotif (generic function with 1 method)

For example, using the second derivative for f shows that indeed these two points above are inflection points

plotif(f, f'', 0, 10)
-15 -10 -5 0 5 10 15 20 25 -10.0 -9.5 -9.0 -8.5 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0 15.5 16.0 16.5 17.0 17.5 18.0 18.5 19.0 19.5 20.0 -10 0 10 20 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 y1 y2 -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 -14.0 -13.5 -13.0 -12.5 -12.0 -11.5 -11.0 -10.5 -10.0 -9.5 -9.0 -8.5 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0 15.5 16.0 -20 -10 0 10 20 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Helpful functions

These two functions are used to return functions representing lines derived from $f$:

secant(f, a, b) = x -> f(a) + (f(a) - f(b)) / (a - b) * (x - a)
tangent(f, c) = x -> f(c) + f'(c) * (x - c)
tangent (generic function with 1 method)

Okay, start your engines

Use these cells or add some more to answer the questions on the exam.