Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <a href="http://mpec.sc.mahidol.ac.th/numer/STEP9x.HTM" rel="nofollow">link</a> you posted in your comment explains why you can't find all the roots with this algorithm - it only converges to a root if <code>|phi'(x)| &lt; 1</code> around the root. That's not the case with any of the roots of your polynomial; for most starting points, the iteration will end up bouncing around the middle root, and eventually get close to it by accident; it will almost certainly never get close enough to the other roots, wherever it starts.</p> <p>To find all three roots, you need a more stable algorithm such as <a href="http://en.wikipedia.org/wiki/Newton%27s_method" rel="nofollow">Newton's method</a> (which is also described in the <a href="http://mpec.sc.mahidol.ac.th/numer/STEP10.HTM" rel="nofollow">tutorial</a> you linked to). This is also an iterative method; you can find a root of <code>f(x)</code> using the iteration <code>x -&gt; x - f(x)/f'(x)</code>. This is still not guaranteed to converge, but the convergence condition is much more lenient. For your polynomial, it might look a bit like this:</p> <pre><code>#include &lt;iostream&gt; #include &lt;cmath&gt; float a[] = { 1.f, -5.f, -9.f, 16.f }, etha = 0.001f; float f(float x) { return (a[0] * x * x * x) + (a[1] * x * x) + (a[2] * x) + a[3]; } float df(float x) { return (3 * a[0] * x * x) + (2 * a[1] * x) + a[2]; } float newtonMethod(float a, float b) { float x = (a + b) / 2.f; while (fabs(f(x)) &gt; etha) { x -= f(x)/df(x); } return x; } int main() { std::cout &lt;&lt; newtonMethod(-5,0) &lt;&lt; '\n'; // prints -2.2341 std::cout &lt;&lt; newtonMethod(0,5) &lt;&lt; '\n'; // prints 1.18367 std::cout &lt;&lt; newtonMethod(5,10) &lt;&lt; '\n'; // prints 6.05043 } </code></pre> <p>There are many other algorithms for finding roots; <a href="http://en.wikipedia.org/wiki/Root-finding_algorithm" rel="nofollow">here</a> is a good place to start learning.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload