Test 2 will cover the 4th and 5th packages: zeros of functions and limits. Some features we used are only available once the MTH229
package is loaded:
using MTH229 using SymPy
The MTH229
package loads the Roots
package which provides functions to return zeros of a function.
For polynomials functions we have the following:
roots(f)
to find all real and (possibly) complex roots of a polynomial function.fzeros(f)
to find the real roots of a polynomial function.For an $n$th degree polynomial, there are exactly $n$ roots when multiplicities are accounted for and complex numbers are used. There are $n-2k$ real roots, for some integer $k$, as complex roots come in pairs.
For example:
f(x) = x^5 - x - 1 roots(f)
5-element Array{Complex{Float64},1}: 1.1673+0.0im 0.181232+1.08395im 0.181232-1.08395im -0.764884+0.352472im -0.764884-0.352472im
(There are 5 complex roots to match the degree of the polynomial). Yet, only one is real, which you can check with fzeros(f)
:
fzeros(f)
1-element Array{Real,1}: 1.1673
The following facts about polynomial zeros (roots in this case) are mentioned in the project:
Polynomials are special enough, that one doesn't need to specify where to look for zeros. This is not so in general. We used two functions to find zeros:
fzero(f, a, b)
will find a zero in $[a,b]$ when $[a,b]$ is a bracketing interval and $f$ is continuous. (That means $f(a)$ and $f(b)$ have different signs, so the intermediate value theorem guarantees some $c$ with $a \leq c \leq b$ and $f(c) = 0$.) This uses the bisection method.fzeros(f, a, b)
– not the extra "s" – will try to use bisection to find all the roots within the interval $[a,b]$. This $[a,b]$ need not be a bracket.For example. Find a solution to $cos(x) = x/2$. We reframe this to be find a zero of $f(x) = \cos(x) - x/2$ and not that $f(0)=1$ and $f(pi/2) = -pi/4$, so $[0, \pi/2]$ is a bracket. Using this we have:
f(x) = cos(x) - x/2 fzero(f, 0, pi/2)
Are there others? We can check. Since $|x/2| > 1$ when $|x| > 2$ and $\cos(x)$ is between $[-1,1]$, any solution must lie in $[-2,2]$. So we ask:
fzeros(f, -2, 2)
1-element Array{Float64,1}: 1.02987
And we see that is it. Not so for a "flatter" line, say $x/10$:
f(x) = cos(x) - x/10 fzeros(f, -10, 10)
7-element Array{Float64,1}: -9.67888 -8.96602 -4.2711 -1.74633 1.42755 5.26712 7.06889
These functions only solve for answers to $f(x) = 0$. You may have to rearrange your problem to fit that. For example, to find out when $\sinh(x) = 1/2$, we can solve for when $\sinh(x) - 1/2 = 0$:
f(x) = sinh(x) - 1/2 fzeros(f, -2, 2) # one answer here
1-element Array{Float64,1}: 0.481212
We discussed three ways to investigate the answer (if it exists) to
$$~ \lim_{x \rightarrow c} f(x) ~$$Can be investigated by creating a graph around $c=4$:
f(t) = (t - sqrt(3t+4)) / (4-t) plot(f, 3, 5)
The answer looks to be about $-0.625$.
lim
in the MTH229
package that makes a tableWith this, making a table to investigate a limit is as easy as:
lim(f, 4, dir="+") ## going to 4 from right
6x2 Array{Float64,2}: 4.1 -0.626742 4.01 -0.625176 4.001 -0.625018 4.0001 -0.625002 4.00001 -0.625 4.0 -0.625
SymPy
package, the command limit(f, c)
will often find the limit. Here we compute the limit above symbolically:limit(f, 4)
In this case, all three give the same answer – the limit is $-5/8 = -0.625$.
Other questions may include finding the distinction between left and right limits, limits of functions with jumps, or whether a limit can be found just by "plugging in".
For example, to find the left limit at $0$ of $\sin(|x|)/x$ we have:
f(x) = sin(abs(x))/x limit(f, 0, dir="-")