lots of cleanup
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
# Roots of a polynomial
|
||||
|
||||
|
||||
{{< include ../_common_code.qmd >}}
|
||||
|
||||
In this section we use the following add on packages:
|
||||
@@ -25,15 +24,18 @@ nothing
|
||||
---
|
||||
|
||||
|
||||
The [roots](http://en.wikipedia.org/wiki/Properties_of_polynomial_roots) of a polynomial are the values of $x$ that when substituted into the expression yield $0$. For example, the polynomial $x^2 - x$ has two roots, $0$ and $1$. A simple graph verifies this:
|
||||
The [roots](http://en.wikipedia.org/wiki/Properties_of_polynomial_roots) of a polynomial are the values of $x$ that when substituted into the polynomial yield $0$. For example, the polynomial $x^2 - x$ has two roots, $0$ and $1$. A simple graph verifies this:
|
||||
|
||||
|
||||
::: {#fig-plot-parabola-highlight-zero-line}
|
||||
```{julia}
|
||||
#| hold: true
|
||||
f(x) = x^2 - x
|
||||
plot(f, -2, 2)
|
||||
plot!(zero, -2, 2)
|
||||
```
|
||||
Plot of $f(x) = x^2 - x$ over $[-2, 2]$ showing the graph crosses the $x$ axis twice
|
||||
:::
|
||||
|
||||
The graph crosses the $x$-axis at both $0$ and $1$.
|
||||
|
||||
@@ -41,9 +43,11 @@ The graph crosses the $x$-axis at both $0$ and $1$.
|
||||
What is known about polynomial roots? Some simple questions might be:
|
||||
|
||||
|
||||
* Will a polynomial always have a root?
|
||||
* How many roots can there be?
|
||||
* How large can the roots be?
|
||||
* Will a polynomial always have a root? A *real* root?
|
||||
|
||||
* How many roots can there be?
|
||||
|
||||
* How large can the roots be?
|
||||
|
||||
|
||||
We look at such questions here.
|
||||
@@ -52,10 +56,12 @@ We look at such questions here.
|
||||
### The factor theorem
|
||||
|
||||
|
||||
We begin with a comment that ties together two concepts related to polynomials. It allows us to speak of roots or factors interchangeably:
|
||||
We begin with a comment that ties together two concepts related to polynomials.
|
||||
|
||||
|
||||
> The [factor theorem](http://en.wikipedia.org/wiki/Factor_theorem) relates the *roots* of a polynomial with its *factors*: $r$ is a root of $p$ if *and* only if $(x-r)$ is a factor of the polynomial $p$.
|
||||
::: {.theorem title="The factor theorem"}
|
||||
The [factor theorem](http://en.wikipedia.org/wiki/Factor_theorem) states that
|
||||
$r$ is a root of the polynomial $p$ if *and* only if $(x-r)$ is a factor of $p$.
|
||||
:::
|
||||
|
||||
|
||||
|
||||
@@ -88,7 +94,7 @@ From this, we see that $f(c) = r$. Hence, when $c$ is a root of $f(x)$, then it
|
||||
---
|
||||
|
||||
|
||||
The division algorithm for the case of linear term, $(x-c)$, can be carried out by the [synthetic division](http://en.wikipedia.org/wiki/Synthetic_division) algorithm. This algorithm produces $q(x)$ and $r$, a.k.a $f(c)$. The Wikipedia page describes the algorithm well.
|
||||
The division algorithm for the case of a linear term, $(x-c)$, can be carried out by the [synthetic division](http://en.wikipedia.org/wiki/Synthetic_division) algorithm. This algorithm produces $q(x)$ and $r$, a.k.a $f(c)$. The Wikipedia page describes the algorithm well.
|
||||
|
||||
|
||||
The following is an example where $f(x) = x^4 + 2x^2 + 5$ and $g(x) = x-2$:
|
||||
@@ -101,7 +107,7 @@ The following is an example where $f(x) = x^4 + 2x^2 + 5$ and $g(x) = x-2$:
|
||||
1 2 6 12 29
|
||||
```
|
||||
|
||||
The polynomial $f(x)$ is coded in terms of its coefficients ($a_n$, $a_{n-1}$, $\dots$, $a_1$, $a_0$) and is written on the top row. The algorithm then proceeds from left to right. The number just produced on the bottom row is multiplied by $c$ and placed under the coefficient of $f(x)$. Then values are then added to produce the next number. The sequence produced above is `1 2 6 12 29`. The last value (`29`) is $r=f(c)$, the others encode the coefficients of `q(x)`, which for this problem is $q(x)=x^3 + 2x^2 + 6x + 12$. That is, we have written:
|
||||
The polynomial $f(x)$ is coded in terms of its coefficients ($a_n$, $a_{n-1}$, $\dots$, $a_1$, $a_0$) and is written on the top row. The algorithm then proceeds from left to right. The number just produced on the bottom row is multiplied by $c$ and placed under the coefficient of $f(x)$. Then values are then added to produce the next number. The sequence produced above is `1 2 6 12 29`. The last value (`29`) is $r=f(c)$, the others encode the coefficients of `q(x)`, which for this problem is $q(x)=1x^3 + 2x^2 + 6x + 12$. That is, we have written:
|
||||
|
||||
|
||||
$$
|
||||
@@ -171,48 +177,38 @@ This naive attempt to divide won't "just work" though:
|
||||
(x^4 + 2x^2 + 5) / (x-2)
|
||||
```
|
||||
|
||||
`SymPy` is fairly conservative in how it simplifies answers, and, as written, there is no compelling reason to change the expressions, though in our example we want it done.
|
||||
`SymPy` is fairly conservative in how it simplifies answers, and, as written, there is no compelling reason to change the expression, though in this example we want it done.
|
||||
|
||||
|
||||
For this task, `divrem` is available:
|
||||
For this task, `divrem` is available:^[
|
||||
For those who have worked with SymPy within Python, `divrem` is the `div` method renamed, as `Julia`'s `div` method has the generic meaning of returning the quotient.]
|
||||
|
||||
|
||||
```{julia}
|
||||
quotient, remainder = divrem(x^4 + 2x^2 + 5, x - 2)
|
||||
```
|
||||
|
||||
The answer is a tuple containing the quotient and remainder. The quotient itself could be found with `div` or `÷` and the remainder with `rem`.
|
||||
|
||||
|
||||
:::{.callout-note}
|
||||
## Note
|
||||
For those who have worked with SymPy within Python, `divrem` is the `div` method renamed, as `Julia`'s `div` method has the generic meaning of returning the quotient.
|
||||
|
||||
:::
|
||||
|
||||
As well, the `apart` function could be used for this task. This function computes the [partial fraction](http://en.wikipedia.org/wiki/Partial_fraction_decomposition) decomposition of a ratio of polynomial functions.
|
||||
|
||||
|
||||
```{julia}
|
||||
apart((x^4 + 2x^2 + 5) / (x-2))
|
||||
```
|
||||
|
||||
The function `together` would combine such terms, as an "inverse" to `apart`. This isn't so much of interest at the moment, but will be when techniques of integration are looked at.
|
||||
The answer is a tuple containing the quotient and remainder. The quotient itself could be found with `div` or `÷` and the remainder with `rem`.^[There is also the `apart` function that could be used for this task. This function computes the [partial fraction](http://en.wikipedia.org/wiki/Partial_fraction_decomposition) decomposition of a ratio of polynomial functions. The function `together` would combine such terms, as an "inverse" to `apart`. This isn't so much of interest at the moment, but will be when techniques of integration are looked at.]
|
||||
|
||||
|
||||
### The rational root theorem
|
||||
|
||||
|
||||
Factoring polynomials to find roots is a task that most all readers here will recognize, and, perhaps, remember not so fondly. One helpful trick to find possible roots *by hand* is the [rational root theorem](http://en.wikipedia.org/wiki/Rational_root_theorem): if a polynomial has integer coefficients with $a_0 \neq 0$, then any rational root, $p/q$, must have $p$ dividing the constant $a_0$ and $q$ dividing the leading term $a_n$.
|
||||
Factoring polynomials to find roots is a task that most all readers here will recognize, and, perhaps, remember not so fondly. One helpful trick to find possible roots *by hand* is the rational-root theorem.
|
||||
|
||||
::: {.theorem title="The rational root theorem"}
|
||||
The [rational root theorem](http://en.wikipedia.org/wiki/Rational_root_theorem) states that
|
||||
if a polynomial has integer coefficients with $a_0 \neq 0$, then any rational root, $p/q$, must have $p$ dividing the constant $a_0$ and $q$ dividing the leading term $a_n$.
|
||||
:::
|
||||
|
||||
|
||||
To glimpse why, suppose we have a polynomial with a rational root and integer coefficients. With this in mind, a polynomial with identical roots may be written as $(qx -p)(a_{n-1}x^{n-1}+\cdots a_1 x + a_0)$, where each coefficient is an integer. Multiplying through, we get that the polynomial is $qa_{n-1}x^n + \cdots + pa_0$. So $q$ is a factor of the leading coefficient and $p$ is a factor of the constant.
|
||||
To glimpse why this is true, suppose we have a polynomial with a rational root and integer coefficients. With this in mind, a polynomial with identical roots may be written as $(qx -p)(a_{n-1}x^{n-1}+\cdots a_1 x + a_0)$, where each coefficient is an integer. Multiplying through, we get that the polynomial is $qa_{n-1}x^n + \cdots + pa_0$. So $q$ is a factor of the leading coefficient and $p$ is a factor of the constant.
|
||||
|
||||
|
||||
An immediate consequence is that if the polynomial with integer coefficients is monic, then any rational root must be an integer.
|
||||
|
||||
|
||||
This gives a finite - though possibly large - set of values that can be checked to exhaust the possibility of a rational root. By hand this process can be tedious, though may be speeded up using synthetic division. This task is one of the mainstays of high school algebra where problems are chosen judiciously to avoid too many possibilities.
|
||||
This gives a finite---though possibly large---set of values that can be checked to exhaust the possibility of a rational root. By hand this process can be tedious, though may be speeded up using synthetic division. This task is one of the mainstays of high school algebra where problems are chosen judiciously to avoid too many possibilities.
|
||||
|
||||
|
||||
However, one of the great triumphs of computer algebra is the ability to factor polynomials with integer (or rational) coefficients over the rational numbers. This is typically done by first factoring over modular numbers (akin to those on a clock face) and has nothing to do with the rational root test.
|
||||
@@ -221,7 +217,7 @@ However, one of the great triumphs of computer algebra is the ability to factor
|
||||
`SymPy` can quickly find such a factorization, even for quite large polynomials with rational or integer coefficients.
|
||||
|
||||
|
||||
For example, factoring $p = 2x^4 + x^3 -19x^2 -9x +9$. This has *possible* rational roots of plus or minus $1$ or $2$ divided by $1$, $3$, or $9$ - $12$ possible answers for this modest question. By hand that can be a bit of work, but `factor` does it without fuss:
|
||||
For example, factoring $p = 2x^4 + x^3 -19x^2 -9x +9$. This has *possible* rational roots of plus or minus $1$ or $2$ divided by $1$, $3$, or $9$---twelve possible answers for this modest question. By hand that can be a bit of work, but `factor` does it without fuss:
|
||||
|
||||
|
||||
```{julia}
|
||||
@@ -235,14 +231,12 @@ factor(p)
|
||||
|
||||
There is a basic fact about the roots of a polynomial of degree $n$. Before formally stating it, we consider the earlier observation that a polynomial of degree $n$ for large values of $x$ has a graph that looks like the leading term. However, except at $0$, monomials do not cross the $x$ axis, the roots must be the result of the interaction of lower order terms. Intuitively, since each term can contribute only one basic shape up or down, there can not be arbitrarily many roots. In fact, a consequence of the [Fundamental Theorem of Algebra](http://en.wikipedia.org/wiki/Fundamental_theorem_of_algebra) (Gauss) is:
|
||||
|
||||
|
||||
> A polynomial of degree $n$ with real or complex coefficients has at most $n$ real roots.
|
||||
|
||||
|
||||
::: {.theorem title="The fundamental theorem of algebra"}
|
||||
A polynomial of degree $n$ with real or complex coefficients has at most $n$ real roots.
|
||||
:::
|
||||
|
||||
This statement can be proved with the factor theorem and the division algorithm.
|
||||
|
||||
|
||||
In fact the fundamental theorem states that there are exactly $n$ roots, though, in general, one must consider multiple roots and possible complex roots to get all $n$. (Consider $x^2$ to see why multiplicity must be accounted for and $x^2 + 1$ to see why complex values may be necessary.)
|
||||
|
||||
|
||||
@@ -265,16 +259,21 @@ $$
|
||||
\frac{-b \pm \sqrt{b^2 - 4ac}}{2a}.
|
||||
$$
|
||||
|
||||
The discriminant is defined as $b^2 - 4ac$. When this is negative, the square root requires the concept of complex numbers to be defined, and the formula shows the two complex roots are conjugates. When the discriminant is $0$, then the root has multiplicity two, e.g., the polynomial will factor as $a_2(x-r)^2$. Finally, when the discriminant is positive, there will be two distinct, real roots. This figure shows the $3$ cases, that are illustrated by $x^2 -1$, $x^2$ and $x^2 + 1$:
|
||||
The discriminant is defined as $b^2 - 4ac$. When this is negative, the square root requires the concept of complex numbers to be defined, and the formula shows the two complex roots are conjugates. When the discriminant is $0$, then the root has multiplicity two, e.g., the polynomial will factor as $a_2(x-r)^2$. Finally, when the discriminant is positive, there will be two distinct, real roots. @fig-three-cases-of-quadratic shows the $3$ cases illustrated by $x^2 -1$, $x^2$ and $x^2 + 1$.
|
||||
|
||||
|
||||
::: {#fig-three-cases-of-quadratic}
|
||||
```{julia}
|
||||
#| echo: false
|
||||
plot(x^2 - 1, -2, 2, legend=false) # two roots
|
||||
plot!(x^2, -2, 2) # one (double) root
|
||||
plot!(x^2 + 1, -2, 2) # no real root
|
||||
plot!(zero, -2, 2)
|
||||
```
|
||||
|
||||
Three simple quadratic functions, one with two real roots; one with one; one with none
|
||||
:::
|
||||
|
||||
There are similar formulas for the [cubic](http://en.wikipedia.org/wiki/Cubic_function#General_formula_for_roots) and [quartic](http://en.wikipedia.org/wiki/Quartic_function#General_formula_for_roots) cases. (The [cubic formula](http://arxiv.org/pdf/math/0005026v1.pdf) was known to Cardano in $1545$, though through Tartagli, and the quartic was solved by Ferrari, Cardano's roommate.)
|
||||
|
||||
|
||||
@@ -283,8 +282,9 @@ In general, there is no such formula using radicals for $5$th degree polynomials
|
||||
|
||||
The `factor` function of `SymPy` only finds factors of polynomials with integer or rational coefficients corresponding to rational roots. There are alternatives.
|
||||
|
||||
### The solve and solveset functions
|
||||
|
||||
Finding roots with `SymPy` can also be done through its `solve` function, a function which also has a more general usage, as it can solve simple expressions or more than one expression. Here we illustrate that `solve` can easily handle quadratic expressions:
|
||||
Finding roots with `SymPy` can also be done through its `solve` function, a function which also has a more general usage, as it can solve simple expressions or more than one expression for unknown variables. Here we illustrate that `solve` can easily handle quadratic expressions:
|
||||
|
||||
|
||||
```{julia}
|
||||
@@ -293,10 +293,9 @@ solve(x^2 + 2x - 3 ~ 0, x)
|
||||
|
||||
The answer is a vector of values that when substituted in for the free variable `x` produce $0.$
|
||||
|
||||
We use the `~` notation to define an equation to pass to `solve`. This convention is not necessary here, as `SymPy` will assume an expression passed to solve is an equation set to `0`, but is pedagogically useful. Equations do not have an equals sign, which is reserved for assignment. To solve a more complicated expression of the type $f(x) = g(x),$ one can solve $f(x) - g(x) = 0,$ use the `Eq` function, or use `f ~ g`.
|
||||
We use the `~` notation to define an equation to pass to `solve`.^[The `solve` function applied to a simple case need not have an equation specified with `~` and need not have a variable to solve for specified. However, we try to be explicit in each example so that exactly what is being asked is as clear as can be.] Equations can not be specified with an equals sign, which is reserved for assignment. To solve a more complicated expression of the type $f(x) = g(x),$ one can solve $f(x) - g(x) = 0$ or use `f ~ g`.
|
||||
|
||||
|
||||
When the expression to solve has more than one free variable, the variable to solve for should be explicitly stated with a second argument. (The specification above is unnecessary.) For example, here we show that `solve` is aware of the quadratic formula:
|
||||
For example, here we show that `solve` is aware of the quadratic formula:
|
||||
|
||||
|
||||
```{julia}
|
||||
@@ -348,9 +347,9 @@ Third- and fourth-degree polynomials can be solved in general, with increasingly
|
||||
```{julia}
|
||||
#| hold: true
|
||||
@syms a[0:3]
|
||||
p = sum(a*x^(i-1) for (i,a) in enumerate(a))
|
||||
p = sum(aᵢ*x^(i-1) for (i,aᵢ) in enumerate(a))
|
||||
rts = solve(p ~ 0, x)
|
||||
rts[1] # there are three roots
|
||||
first(rts) # there are three roots
|
||||
```
|
||||
|
||||
Some fifth degree polynomials are solvable in terms of radicals, however, `solve` will not seem to have luck with this particular fifth degree polynomial:
|
||||
@@ -366,42 +365,39 @@ solve(x^5 - x + 1 ~ 0, x)
|
||||
### The `roots` function
|
||||
|
||||
|
||||
Related to `solve` is the specialized `roots` function for identifying roots, Unlike solve, it will identify multiplicities.
|
||||
|
||||
Related to `solve` is the specialized `roots` function for identifying roots of a polynomial, Unlike solve, it will identify multiplicities, returning a dictionary as its output.
|
||||
|
||||
For a polynomial with only one indeterminate the usage is straight forward:
|
||||
|
||||
|
||||
```{julia}
|
||||
roots((x-1)^2 * (x-2)^2) # solve doesn't identify multiplicities
|
||||
roots((x-1)^2 * (x-2)^3, x) # solve doesn't identify multiplicities
|
||||
```
|
||||
|
||||
For a polynomial with symbolic coefficients, the difference between the symbol and the coefficients must be identified. `SymPy` has a `Poly` type to do so. The following call illustrates:
|
||||
For a polynomial with symbolic coefficients, the difference between the symbol and the coefficients *must* be identified. This can be done with the `Poly` type of `SymPy`,^[The use of `Poly` would look like `q1 = sympy.Poly(p, x); roots(q1)`. The `Poly` function is not exported, so is called by qualifying it with the `sympy` object. This is common when using `SymPy`, as only a small handful of the many functions available are turned into `Julia` functions, the rest are used as would be done in Python. (This is similar, but different than qualifying by a `Julia` module when there are two conflicting names. An example will be the use of the name `roots` in both `SymPy` and `Polynomials` to refer to a function that finds the roots of a polynomial. If both functions were loaded, then the last line in the above example would need to be `SymPy.roots(q)` (note the capitalization.)] or by specifying the variable, as above (which is optional when there is only one free variable):
|
||||
|
||||
|
||||
```{julia}
|
||||
#| hold: true
|
||||
@syms a b c
|
||||
@syms a b c x
|
||||
p = a*x^2 + b*x + c
|
||||
q1 = sympy.Poly(p, x) # identify `x` as indeterminate; alternatively p.as_poly(x)
|
||||
roots(q1)
|
||||
roots(p, x)
|
||||
```
|
||||
|
||||
:::{.callout-note}
|
||||
## Note
|
||||
The sympy `Poly` function must be found within the underlying `sympy` module, a Python object, hence is qualified as `sympy.Poly`. This is common when using `SymPy`, as only a small handful of the many functions available are turned into `Julia` functions, the rest are used as would be done in Python. (This is similar, but different than qualifying by a `Julia` module when there are two conflicting names. An example will be the use of the name `roots` in both `SymPy` and `Polynomials` to refer to a function that finds the roots of a polynomial. If both functions were loaded, then the last line in the above example would need to be `SymPy.roots(q)` (note the capitalization.)
|
||||
|
||||
:::
|
||||
|
||||
### Numerically finding roots
|
||||
|
||||
|
||||
The `solve` function can be used to get numeric approximations to the roots. It is as easy as calling `N` on the solutions:
|
||||
The output of this `solve` call finds $1$ roots, but it is hidden behind a `CRootOf` wrapper:
|
||||
|
||||
```{julia}
|
||||
@syms x::real
|
||||
rts = solve(x^5 - x + 1 ~ 0, x)
|
||||
```
|
||||
|
||||
Such values can be identified with numeric approximations. It is as easy as calling `N` on the solutions, which are held in a vector:
|
||||
|
||||
|
||||
```{julia}
|
||||
#| hold: true
|
||||
rts = solve(x^5 - x + 1 ~ 0, x)
|
||||
N.(rts) # note the `.(` to broadcast over all values in rts
|
||||
```
|
||||
|
||||
@@ -416,13 +412,15 @@ ex = x^7 -3x^6 + 2x^5 -1x^3 + 2x^2 + 1x^1 - 2
|
||||
solve(ex ~ 0, x)
|
||||
```
|
||||
|
||||
This finds two of the seven possible roots, the remainder of the real roots can be found numerically:
|
||||
This identifies exactly two of the up-to-seven possible roots, the remainding real root can be found numerically:
|
||||
|
||||
|
||||
```{julia}
|
||||
N.(solve(ex ~ 0, x))
|
||||
```
|
||||
|
||||
This approach can also be used to find complex answers.
|
||||
|
||||
### The solveset function
|
||||
|
||||
|
||||
@@ -441,14 +439,13 @@ The `p_rts` object, a `Set`, does not allow indexed access to its elements. For
|
||||
collect(p_rts)
|
||||
```
|
||||
|
||||
To get the numeric approximation, we can broadcast:
|
||||
To get the numeric approximation, we can broadcast:^[There is no need to call `collect` before broadcasting, as broadcasting over a set falls back to broadcasting over the iteration of the set and in this case returns a vector.]
|
||||
|
||||
|
||||
```{julia}
|
||||
N.(solveset(p ~ 0, x))
|
||||
```
|
||||
|
||||
(There is no need to call `collect`---though you can---as broadcasting over a set falls back to broadcasting over the iteration of the set and in this case returns a vector.)
|
||||
|
||||
## Do numeric methods matter when you can just graph?
|
||||
|
||||
@@ -456,46 +453,56 @@ N.(solveset(p ~ 0, x))
|
||||
It may seem that certain practices related to roots of polynomials are unnecessary as we could just graph the equation and look for the roots. This feeling is perhaps motivated by the examples given in textbooks to be worked by hand, which necessarily focus on smallish solutions. But, in general, without some sense of where the roots are, an informative graph itself can be hard to produce. That is, technology doesn't displace thinking---it only supplements it.
|
||||
|
||||
|
||||
For another example, consider the polynomial $(x-20)^5 - (x-20) + 1$. In this form we might think the roots are near $20$. However, were we presented with this polynomial in expanded form: $x^5 - 100x^4 + 4000x^3 - 80000x^2 + 799999x - 3199979$, we might be tempted to just graph it to find roots. A naive graph might be to plot over $[-10, 10]$:
|
||||
|
||||
For another example, consider the polynomial $(x-20)^5 - (x-20) + 1$. In this form we might think the roots are near $20$. However, were we presented with this polynomial in expanded form: $x^5 - 100x^4 + 4000x^3 - 80000x^2 + 799999x - 3199979$, we might be tempted to just graph it to find roots.^[Or ask AI for which google gemini gave a wrong answer the first time.] A naive graph might be to plot over $[-10, 10]$:
|
||||
|
||||
::: {#fig-plot-expanded-polynomial-naively}
|
||||
```{julia}
|
||||
p = x^5 - 100x^4 + 4000x^3 - 80000x^2 + 799999x - 3199979
|
||||
plot(p, -10, 10)
|
||||
```
|
||||
Simple plot of expanded polynomial over $[-10,10]$ domain showing this is a poor choice of viewing window
|
||||
:::
|
||||
|
||||
This seems to indicate a root near $10$. But look at the scale of the $y$ axis. The value at $-10$ is around $-25,000,000$ so it is really hard to tell if $f$ is near $0$ when $x=10$, as the range is too large.
|
||||
@fig-plot-expanded-polynomial-naively maybe seems to indicate a root near $10$. But look at the scale of the $y$ axis. The value at $-10$ is around $-25,000,000$ so it is really hard to tell if $f$ is near $0$ when $x=10$, as the range is too large.
|
||||
|
||||
|
||||
A graph over $[10,20]$ is still unclear:
|
||||
|
||||
A graph over $[10,20]$ is still unclear, as seen in @fig-plot-expanded-polynomial-a-bit-less-naively.
|
||||
|
||||
::: {#fig-plot-expanded-polynomial-a-bit-less-naively}
|
||||
```{julia}
|
||||
plot(p, 10,20)
|
||||
```
|
||||
|
||||
Plot of expanded polynomial over $[10, 20]$. Even though there is a zero, it can't be identified from this graph.
|
||||
:::
|
||||
|
||||
We see that what looked like a zero near $10$, was actually a number around $-100,000$.
|
||||
|
||||
|
||||
Continuing, a plot over $[15, 20]$ still isn't that useful. It isn't until we get close to $18$ that the large values of the polynomial allow a clear vision of the values near $0$. That being said, plotting anything bigger than $22$ quickly makes the large values hide those near $0$, and might make us think where the function dips back down there is a second or third zero, when only $1$ is the case. (We know that, as this is the same $x^5 - x + 1$ shifted to the right by $20$ units.)
|
||||
|
||||
|
||||
::: {#fig-plot-expanded-polynomial-a-good-frame}
|
||||
```{julia}
|
||||
plot(p, 18, 22)
|
||||
```
|
||||
|
||||
Plot of expanded polynomial using a good domain
|
||||
:::
|
||||
|
||||
Not that it can't be done, but graphically solving for a root here can require some judicious choice of viewing window. Even worse is the case where something might graphically look like a root, but in fact not be a root. Something like $(x-100)^2 + 0.1$ will demonstrate.
|
||||
|
||||
|
||||
For another example, the following polynomial when plotted over $[-5,7]$ appears to have two real roots:
|
||||
|
||||
For another example, the following polynomial when plotted over $[-5,7]$ appears to have two real roots in @fig-mignotte-poly-2-or-3-roots.
|
||||
|
||||
::: {#fig-mignotte-poly-2-or-3-roots}
|
||||
```{julia}
|
||||
h = x^7 - 16129x^2 + 254x - 1
|
||||
plot(h, -5, 7)
|
||||
```
|
||||
Plot of polynomial that appears to have two real roots
|
||||
:::
|
||||
|
||||
in fact there are three, two are *very* close together:
|
||||
In fact there are three roots of this polynomial, two are *very* close together:
|
||||
|
||||
|
||||
```{julia}
|
||||
@@ -522,9 +529,17 @@ A polynomial with real coefficients may or may not have real roots. The followin
|
||||
|
||||
The study of polynomial roots is an old one. In $1637$ Descartes published a *simple* method to determine an upper bound on the number of *positive* real roots of a polynomial.
|
||||
|
||||
|
||||
> [Descartes' rule of signs](http://en.wikipedia.org/wiki/Descartes%27_rule_of_signs): if $p=a_n x^n + a_{n-1}x^{n-1} + \cdots + a_1x + a_0$ then the number of positive real roots is either equal to the number of sign differences between consecutive nonzero coefficients, or is less than it by an even number. Repeated roots are counted separately.
|
||||
|
||||
::: {.theorem title="Descartes' rule of signs"}
|
||||
[Descartes' rule of signs](http://en.wikipedia.org/wiki/Descartes%27_rule_of_signs) states that if
|
||||
$$
|
||||
p=a_n x^n + a_{n-1}x^{n-1} + \cdots + a_1x + a_0, \quad a_n \neq 0
|
||||
$$
|
||||
then the number of positive real roots is either equal to the number
|
||||
of sign differences between consecutive coefficients
|
||||
(omitting zero coefficients),
|
||||
or is less than this count by an even number.
|
||||
Repeated roots are counted separately.
|
||||
:::
|
||||
|
||||
|
||||
One method of proof (sketched at the end of this section) first shows that in synthetic division by $(x-c)$ with $c > 0$, we must have that any sign change in $q$ is related to a sign change in $p$ and there must be at least one more in $p$. This is then used to show that there can be only as many positive roots as sign changes. That the difference comes in pairs is related to complex roots of real polynomials always coming in pairs.
|
||||
@@ -533,13 +548,13 @@ One method of proof (sketched at the end of this section) first shows that in sy
|
||||
An immediate consequence, is that a polynomial whose coefficients are all non-negative will have no positive real roots.
|
||||
|
||||
|
||||
Applying this to the polynomial $x^5 -x + 1$ we get That the coefficients have signs: `+ 0 0 0 - +` which collapses to the sign pattern `+`, `-`, `+`. This pattern has two changes of sign. The number of *positive* real roots is either $2$ or $0$. In fact there are $0$ for this case.
|
||||
Applying this to the polynomial $x^5 -x + 1$ we get that the coefficients have signs: `+ 0 0 0 - +` which collapses to the sign pattern `+ - +`. This pattern has two changes of sign. The number of *positive* real roots is either $2$ or $0$. In fact there are $0$ for this case.
|
||||
|
||||
|
||||
What about negative roots? Clearly, any negative root of $p$ is a positive root of $q(x) = p(-x)$, as the graph of $q$ is just that of $p$ flipped through the $y$ axis. But the coefficients of $q$ are the same as $p$, except for the odd-indexed coefficients ($a_1, a_3, \dots$) have a changed sign. Continuing with our example, for $q(x) = -x^5 + x + 1$ we get the new sign pattern `-`, `+`, `+` which yields one sign change. That is, there *must* be a negative real root, and indeed there is, $x \approx -1.1673$.
|
||||
What about negative roots? Clearly, any negative root of $p$ is a positive root of $q(x) = p(-x)$, as the graph of $q$ is just that of $p$ flipped through the $y$ axis. But the coefficients of $q$ are the same as $p$, except for the odd-indexed coefficients ($a_1, a_3, \dots$) have a changed sign. Continuing with our example, for $q(x) = -x^5 + x + 1$ we get the new sign pattern `- + +` which yields one sign change. That is, there *must* be a negative real root, and indeed there is, $x \approx -1.1673$.
|
||||
|
||||
|
||||
With this knowledge, we could have known that in an earlier example the graph of `p = x^7 - 16129x^2 + 254x - 1` – which indicated two positive real roots – was misleading, as there must be $1$ or $3$ by a count of the sign changes.
|
||||
With this knowledge, we could have known that in an earlier example the graph of `p = x^7 - 16129x^2 + 254x - 1`---which suggested two positive real roots---was misleading, as there must be $1$ or $3$ by a count of the sign changes.
|
||||
|
||||
|
||||
For another example, if we looked at $f(x) = x^5 - 100x^4 + 4000x^3 - 80000x^2 + 799999x - 3199979$ again, we see that there could be $1$, $3$, or $5$ *positive* roots. However, changing the signs of the odd powers leaves all "-" signs, so there are $0$ negative roots. From the graph, we saw just $1$ real root, not $3$ or $5$. We can verify numerically with:
|
||||
@@ -553,7 +568,7 @@ N.(solve(j ~ 0, x))
|
||||
### Cauchy's bound on the magnitude of the real roots.
|
||||
|
||||
|
||||
Descartes' rule gives a bound on how many real roots there may be. Cauchy provided a bound on how large they can be. Assume our polynomial is monic (if not, divide by $a_n$ to make it so, as this won't effect the roots). Then any real root is no larger in absolute value than $h = 1 + |a_0| + |a_1| + |a_2| + \cdots + |a_{n-1}|$, (this is expressed in different ways.)
|
||||
Descartes' rule gives a bound on how many real roots there may be. Cauchy provided a bound on how large any real roots can be. Assume our polynomial is monic (if not, divide by $a_n$ to make it so, as this won't effect the roots). Then any real root is no larger in absolute value than $h = 1 + |a_0| + |a_1| + |a_2| + \cdots + |a_{n-1}|$, (this is expressed in different ways.)
|
||||
|
||||
|
||||
To see precisely [why](https://captainblack.wordpress.com/2009/03/08/cauchys-upper-bound-for-the-roots-of-a-polynomial/) this bound works, suppose $x$ is a root with $|x| > 1$ and let $h$ be the bound. Then since $x$ is a root, we can solve $a_0 + a_1x + \cdots + 1 \cdot x^n = 0$ for $x^n$ as:
|
||||
@@ -567,20 +582,23 @@ Which after taking absolute values of both sides, yields by the triangle inequal
|
||||
|
||||
|
||||
$$
|
||||
|x^n| \leq |a_0| + |a_1||x| + |a_2||x^2| + \cdots |a_{n-1}| |x^{n-1}| \leq (h-1) (1 + |x| + |x^2| + \cdots |x^{n-1}|).
|
||||
\begin{align*}
|
||||
\lvert x^n\rvert &\leq \lvert a_0\rvert + \lvert a_1\rvert\lvert x\rvert + \lvert a_2\rvert\lvert x^2\rvert + \cdots \lvert a_{n-1}\rvert \lvert x^{n-1}\rvert \\
|
||||
& \leq (h-1) (1 + \lvert x\rvert + \lvert x^2\rvert + \cdots \lvert x^{n-1}\rvert).\\
|
||||
\end{align*}
|
||||
$$
|
||||
|
||||
The last sum can be computed using a formula for geometric sums, $(|x^n| - 1)/(|x|-1)$. Rearranging, gives the inequality:
|
||||
The last sum can be computed using a formula for geometric sums, $(\lvert x^n\rvert - 1)/(\lvert x\rvert-1)$. Rearranging, gives the inequality:
|
||||
|
||||
|
||||
$$
|
||||
|x| - 1 \leq (h-1) \cdot (1 - \frac{1}{|x^n|} ) \leq (h-1)
|
||||
\lvert x\rvert - 1 \leq (h-1) \cdot (1 - \frac{1}{\lvert x^n\rvert} ) \leq (h-1)
|
||||
$$
|
||||
|
||||
from which it follows that $|x| \leq h$, as desired.
|
||||
from which it follows that $\lvert x\rvert \leq h$, as desired.
|
||||
|
||||
|
||||
For our polynomial $x^5 -x + 1$ we have the sum above is $3$. The lone real root is approximately $-1.1673$ which satisfies $|-1.1673| \leq 3$.
|
||||
For our polynomial $x^5 -x + 1$ we have the sum above is $3$. The lone real root is approximately $-1.1673$ which satisfies $\lvert -1.1673\rvert \leq 3$.
|
||||
|
||||
|
||||
|
||||
@@ -605,8 +623,8 @@ choices = [
|
||||
"``6``",
|
||||
"``0``"
|
||||
]
|
||||
answ = 3
|
||||
radioq(choices, answ)
|
||||
answer = 3
|
||||
buttonq(choices, answer)
|
||||
```
|
||||
|
||||
###### Question
|
||||
@@ -623,8 +641,8 @@ choices = [
|
||||
"``x^2 - 2x + 2``",
|
||||
"``2``"
|
||||
]
|
||||
answ = 2
|
||||
radioq(choices, answ)
|
||||
answer = 2
|
||||
buttonq(choices, answer)
|
||||
```
|
||||
|
||||
###### Question
|
||||
@@ -644,8 +662,8 @@ choices = [
|
||||
"``x^3 + x^2 - 1``",
|
||||
"``-2x + 2``"
|
||||
]
|
||||
answ = 3
|
||||
radioq(choices, answ)
|
||||
answer = 3
|
||||
buttonq(choices, answer)
|
||||
```
|
||||
|
||||
###### Question
|
||||
@@ -676,8 +694,8 @@ choices = [
|
||||
"``x^5 + 2x^4 + 4x^3 + 8x^2 + 15x + 31``",
|
||||
"``x^4 +2x^3 + 4x^2 + 8x + 15``",
|
||||
"``31``"]
|
||||
answ = 1
|
||||
radioq(choices, answ)
|
||||
answer = 1
|
||||
buttonq(choices, answer)
|
||||
```
|
||||
|
||||
What is $q(x)$?
|
||||
@@ -692,8 +710,8 @@ choices = [
|
||||
"``x^5 + 2x^4 + 4x^3 + 8x^2 + 15x + 31``",
|
||||
"``x^4 +2x^3 + 4x^2 + 8x + 15``",
|
||||
"``31``"]
|
||||
answ = 4
|
||||
radioq(choices, answ)
|
||||
answer = 4
|
||||
buttonq(choices, answer)
|
||||
```
|
||||
|
||||
What is $r$?
|
||||
@@ -708,8 +726,8 @@ choices = [
|
||||
"``x^5 + 2x^4 + 4x^3 + 8x^2 + 15x + 31``",
|
||||
"``x^4 +2x^3 + 4x^2 + 8x + 15``",
|
||||
"``31``"]
|
||||
answ = 5
|
||||
radioq(choices, answ)
|
||||
answer = 5
|
||||
buttonq(choices, answer)
|
||||
```
|
||||
|
||||
###### Question
|
||||
@@ -728,8 +746,8 @@ choices = [
|
||||
L" $2$ and $3$",
|
||||
L" $(x-2)$ and $(x-3)$",
|
||||
L" $(x+2)$ and $(x+3)$"]
|
||||
answ = 2
|
||||
radioq(choices, answ)
|
||||
answer = 2
|
||||
buttonq(choices, answer)
|
||||
```
|
||||
|
||||
###### Question
|
||||
@@ -785,8 +803,8 @@ q"[-0.434235, -0.434235, 0.188049, 0.188049, 0.578696, 4.91368]",
|
||||
q"[-0.434235, -0.434235, 0.188049, 0.188049]",
|
||||
q"[0.578696, 4.91368]",
|
||||
q"[-0.434235+0.613836im, -0.434235-0.613836im]"]
|
||||
answ = 3
|
||||
radioq(choices, answ)
|
||||
answer = 3
|
||||
buttonq(choices, answer)
|
||||
```
|
||||
|
||||
###### Question
|
||||
@@ -863,8 +881,8 @@ Let $f(x) = x^5 - 4x^4 + x^3 - 2x^2 + x$. What does Cauchy's bound say is the la
|
||||
```{julia}
|
||||
#| hold: true
|
||||
#| echo: false
|
||||
answ = 1 + 4 + 1 + 2 + 1
|
||||
numericq(answ)
|
||||
answer = 1 + 4 + 1 + 2 + 1
|
||||
numericq(answer)
|
||||
```
|
||||
|
||||
What is the largest magnitude of a real root?
|
||||
@@ -875,14 +893,14 @@ What is the largest magnitude of a real root?
|
||||
#| echo: false
|
||||
f(x) = x^5 - 4x^4 + x^3 - 2x^2 + x
|
||||
rts = find_zeros(f, -5..5)
|
||||
answ = maximum(abs.(rts))
|
||||
numericq(answ)
|
||||
answer = maximum(abs.(rts))
|
||||
numericq(answer)
|
||||
```
|
||||
|
||||
###### Question
|
||||
|
||||
|
||||
As $1 + 2 + 3 + 4$ is $10$, Cauchy's bound says that the magnitude of the largest real root of $x^3 - ax^2 + bx - c$ is $10$ where $a,b,c$ is one of $2,3,4$. By considering all 6 such possible polynomials (such as $x^3 - 3x^2 + 2x - 4$) what is the largest magnitude or a root?
|
||||
As $1 + 2 + 3 + 4$ is $10$, Cauchy's bound says that the magnitude of the largest real root of $x^3 - ax^2 + bx - c$ is $10$ where $a,b,c$ is one of $2,3,4$. By considering all 6 such possible polynomials (such as $x^3 - 3x^2 + 2x - 4$) what is the largest magnitude of a root?
|
||||
|
||||
|
||||
```{julia}
|
||||
@@ -921,8 +939,8 @@ choices = [
|
||||
"``2x^2``",
|
||||
"``x``",
|
||||
"``2x``"]
|
||||
answ = 1
|
||||
radioq(choices, answ)
|
||||
answer = 1
|
||||
buttonq(choices, answer)
|
||||
```
|
||||
|
||||
* True or false, the $degree$ of $T_n(x)$ is $n$: (Look at the defining relation and reason this out).
|
||||
@@ -950,19 +968,22 @@ The Chebyshev polynomials have the property that in fact all $n$ roots are real,
|
||||
@syms x
|
||||
p = 16x^5 - 20x^3 + 5x
|
||||
rts = N.(solve(p))
|
||||
answ = maximum(norm.(rts))
|
||||
numericq(answ)
|
||||
answer = maximum(norm.(rts))
|
||||
numericq(answer)
|
||||
```
|
||||
|
||||
* Plotting `p` over the interval $[-2,2]$ does not help graphically identify the roots:
|
||||
|
||||
|
||||
::: {#fig-plot-polynomial-over-minus2-2-not-roots-identifiable}
|
||||
```{julia}
|
||||
#| hold: true
|
||||
plot(16x^5 - 20x^3 + 5x, -2, 2)
|
||||
```
|
||||
Plot of $p = 16x^5 - 20x^3 + 5x$ over $[-2, 2]$
|
||||
:::
|
||||
|
||||
Does graphing over $[-1,1]$ show clearly the $5$ roots?
|
||||
|
||||
Does graphing $p$ over $[-1,1]$ show clearly the $5$ roots?
|
||||
|
||||
|
||||
```{julia}
|
||||
@@ -986,19 +1007,19 @@ Let `var(p)` be the number of sign changes and `pos(p)` the number of positive r
|
||||
First: For a monic $p$ if $p_0 < 0$ then `var(p)` is odd and if $p_0 > 0$ then `var(p)` is even.
|
||||
|
||||
|
||||
This is true for degree $n=1$ the two sign patterns under the assumption are `+-` ($p_0 < 0$) or `++` ($p_0 > 0$). If it is true for degree $n-1$, then the we can consider the sign pattern of such an $n$ degree polynomial having one of these patterns: `+...+-` or `+...--` (if $p_0 < 0$) or `+...++` or `+...-+` if ($p_0>0$). An induction step applied to all but the last sign for these four patterns leads to even, odd, even, odd as the number of sign changes. Incorporating the last sign leads to odd, odd, even, even as the number of sign changes.
|
||||
This is true for degree $n=1$ the two sign patterns under the assumption are `+-` ($p_0 < 0$) or `++` ($p_0 > 0$). If it is true for degree $n-1$, then the we can consider the sign pattern of such an $n$ degree polynomial having one of these patterns: `+...+(0?)-` or `+...-(0?)-` (if $p_0 < 0$) or `+...+(0?)+` or `+...-(0?)+` if ($p_0>0$). The presence of `(0?)` is to show there may be zero, one, or more zeros in that position. An induction step applied to all but the last sign for these four patterns (that is the polynomial after dropping the constant and any of the `0?` terms) leads to even, odd, even, odd as the number of sign changes. Incorporating the last sign leads to odd, odd, even, even as the number of sign changes.
|
||||
|
||||
|
||||
Second: For a monic $p$ if $p_0 < 0$ then `pos(p)` is *odd*, if $p_0 > 0$ then `pos(p)` is even.
|
||||
|
||||
|
||||
This is clearly true for **monic** degree $1$ polynomials: if $c$ is positive $p = x - c$ has one real root (an odd number) and $p = x + c$ has $0$ real roots (an even number). Now, suppose $p$ has degree $n$ and is monic. Then as $x$ goes to $\infty$, it must be $p$ goes to $\infty$.
|
||||
This is clearly true for *monic* degree $1$ polynomials: if $c$ is positive $p = x - c$ has one positive, real root (an odd number) and $p = x + c$ has zero *positive*, real roots (an even number). Now, suppose $p$ has degree $n$ and is monic. Then as $x$ goes to $\infty$, it must be $p$ goes to $\infty$.
|
||||
|
||||
|
||||
If $p_0 < 0$ then there must be a positive real root, say $r$, (Bolzano's intermediate value theorem). Dividing $p$ by $(x-r)$ to produce $q$ requires $q_0$ to be *positive* and of lower degree. By *induction* $q$ will have an even number of roots. Add in the root $r$ to see that $p$ will have an **odd** number of roots.
|
||||
If $p_0 < 0$ then there must be a positive real root, say $r$, (Bolzano's intermediate value theorem). Dividing $p$ by $(x-r)$ to produce $q$ requires $q_0$ to be *positive* and of lower degree. By *induction* $q$ will have an even number of roots. Add in the root $r$ to see that $p$ will have an *odd* number of roots.
|
||||
|
||||
|
||||
Now consider the case $p_0 > 0$. There are two possibilities either `pos(p)` is zero or positive. If `pos(p)` is $0$ then there are an even number of roots. If `pos(p)` is positive, then call $r$ one of the real positive roots. Again divide by $x-r$ to produce $p = (x-r) \cdot q$. Then $q_0$ must be *negative* for $p_0$ to be positive. By *induction* $q$ must have an odd number of roots, meaning $p$ must have an even numbers.
|
||||
Now consider the case $p_0 > 0$. There are two possibilities either `pos(p)` is zero or positive. If `pos(p)` is $0$ then there are an even number of roots. If `pos(p)` is positive, then call $r$ one of the real positive roots. Again divide by $x-r$ to produce $p = (x-r) \cdot q$. Then $q_0$ must be *negative* for $p_0$ to be positive. By *induction* $q$ must have an odd number of roots, meaning $p$ must have an even number.
|
||||
|
||||
|
||||
So there is parity between `var(p)` and `pos(p)`: if $p$ is monic and $p_0 < 0$ then both `var(p)` and `pos(p)` are both odd; and if $p_0 > 0$ both `var(p)` and `pos(p)` are both even.
|
||||
@@ -1017,7 +1038,7 @@ As $p = (x-c)q$ we must have the leading term is $p_nx^n = x \cdot q_{n-1} x^{n-
|
||||
+ - - - + - + + 0
|
||||
```
|
||||
|
||||
But actually, we can fill in more, as the second row is formed by multiplying a positive $c$:
|
||||
The pattern for `q` only has a leading `+` and ending `0`, the pattern shown is just an arbitrary pattern to make a general point. From any pattern for `q`, we can we can fill in more. As the second row is formed by multiplying a positive $c$ the sign in the second row is the same as the sign in the bottom row shifted over 1 to the left.
|
||||
|
||||
|
||||
```{verbatim}
|
||||
@@ -1030,6 +1051,15 @@ But actually, we can fill in more, as the second row is formed by multiplying a
|
||||
What's more, using the fact that to get `0` the two summands must differ in sign and to have a `?` plus `+` yield a `-`, the `?` must be `-` (and reverse), the following must be the case for the signs of `p`:
|
||||
|
||||
|
||||
```{verbatim}
|
||||
+ - ? ? ? ? ? ? -
|
||||
+ + - - - + - + +
|
||||
-----------------
|
||||
+ - - - + - + + 0
|
||||
```
|
||||
|
||||
Going further, to get a `+` in the bottom row with a `-` in the middle row, the top row must be `+` and similarly if `-` and `+`. Filling these in we have this pattern determined.
|
||||
|
||||
```{verbatim}
|
||||
+ - ? ? + - + ? -
|
||||
+ + - - - + - + +
|
||||
|
||||
Reference in New Issue
Block a user