Question: Given a cubic equation where the coefficients are real but can take any extreme conditions (e.g very large or very small number), write a program in Matlab that finds all the roots of this equation. You can't use the built-in functions roots and fzero
Here's my approach: By the Fundamental Theorem of Algebra, f(x) must have at least one real root. If I can find this root, I can factor it out and find the other two roots by using the quadratic formula. But I stuck badly on finding the first real root. I'm thinking about the bisection method, but how can I even find the interval that contains the first root. Can anyone help me at this step?
Thanks!
Let's assume the coeff of $x^3$ is positive; the negative case is similar.
The derivative is a quadratic, whose roots $r_1 < r_2$ you can find. (If it has no roots, see below).
To the right of $x = r_2$, the function is increasing, so if $f(r^2) > 0$, then there's no root there, and if $f(r_2) < 0$, then there's at most one root there. A similar argument applies to $f(r_1)$. So we have three cases:
$f(r_1) < 0, f(r_2) > 0$: there's a root between them.
$f(r_1)$ and $f(r_2)$ are both positive: there's a root to the left of $r_1$.
$f(r_1)$ and $f(r_2)$ are both negative: there's a root to the right of $r_2$.
Here's what you can do in case 3: look at $f(r_2 + 1)$, $f(r_2 + 2)$, $f(r_2 + 4)$, ... $f(r_2 + 2^n)$. Eventually one of these will be positive. Then between $r^2$ and this point, there's a root, which you can find by the intermediate value theorem.
A similar thing works for case 2. Case 1 is easy.
And what if the roots of the derivative are both complex? Then the slope is everywhere positive. Look at the points $x =\pm 1$, $x = \pm 2$, ..., $x = \pm 2^n$, increasing $n$ until the signs differ. Now use bisection to find the root.