Test 3 is April 26th and will cover the two projects on derivatives.
We will use functions provided by the MTH229
package:
using MTH229
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 # basically 1/2, but an approximation
Different values of h
give different approximations and there is an art to selecting the right size of h
, but for most purposes, 1e-6
will be reasonable.
h
goes to zero by just using some small value of h
. For most problems, the "automatic derivative" can be found and will be more accurate. The MTH229
package overrides the syntax for the '
to make computing these straightforward:f(x) = x - log(x) c = 2 f'(c)
Second derivatives are found by using 2 "primes":
f''(c)
We can use f'
just like any other function gets used. For example, to find out when $f'(x) = 0$ in the interval $[1/10, 10]$ we have:
fzeros(f', 1/10, 10)
1-element Array{Float64,1}: 1.0
diff
function,as in diff(f)
.At $c=2$, we can see the symbolic derivative is also $1/2$ with diff(f)(2)
.
The project introduced the tangent
function to describe the tangent line to $f$ and $c$. We can plot this as follows:
f(x) = x^5 - x - 1 plot(f, 0, 2) c = 2 plot!(tangent(f, c), 0, 2)
We can ask questions about the tangent line. For example:
When will the tangent line cross the $x$ axis? Clearly it is between 0 and 2:
fzeros(tangent(f, c), 0, 2)
1-element Array{Real,1}: 1.63291
Or where will the tangent line cross the $y$ axis? This is the value at $0$:
tl = tangent(f, c) tl(0)
This rough table describes some relationships we explored in one of the projects:
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 plotif
function was helpful in graphical explorations:
For example, using the second derivative for f
shows that indeed these two points above are inflection points
plotif(f, f'', 0, 10)
The hardest problems here deal with the case where a function is described in terms of its derivative (or second derivative). For example, suppose $f'(x) = x^3 - x + 1$. What can be said about where $f$ is increasing? Concave up?
Well, $f$ is increasing when $f'(x) \geq 0$ (basically), so we identify where $f$ is 0.
fp(x) = x^3 - x + 1 cps = fzeros(fp, -5, 5)
1-element Array{Real,1}: -1.32472
We can graph or check otherwise that to the left of the lone zero $fp < 0$ and to the right $fp > 0$. So $f(x)$ will increase on $( -1.32472, \infty)$.
Now, $f$ will be concave up when $f''(x) \geq 0$. We can investigate this with:
fzeros(fp', -5, 5)
2-element Array{Float64,1}: -0.57735 0.57735
We see two possible inflection points. A graph will show that $f''(x) < 0$ in $(-0.57735, 0.57735)$ and is otherwise positive. So $f(x)$ is concave up except on the interval $(-0.57735, 0.57735)$.