[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
We discussed three ways to investigate the answer (if it exists) to
$$~ \lim_{x \rightarrow c} f(x) ~$$SymPy package, the command limit(f, c) will _often_ find the limit.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)
Finally, symbolic limits can be done with the add-on SymPy package:
limit(f, 0)
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
D in the Roots package is more accurate. We discussed the following which makes f' use the underlying D function: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 function:diff(f)
At $c=2$, we can see this will also be $1/2$.
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)
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)
Use these cells or add some more to answer the questions on the exam.