Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple iteration algorithm
    text
    copied!<p>If we are given with an array of non-linear equation coefficients and some range, how can we find that equation's root within the range given?</p> <p>E.g.: the equation is </p> <p><img src="https://i.stack.imgur.com/kzQ3Y.gif" alt="enter image description here"> </p> <p>So coefficient array will be the array of <em>a</em>'s. Let's say the equation is </p> <p><img src="https://i.stack.imgur.com/wHf1X.gif" alt="enter image description here"> </p> <p>Then the coefficient array is <code>{ 1, -5, -9, 16 }</code>.</p> <p>As Google says, first we need to morph function given (the equation actually) to some other function. E.g. if the given equation is <code>y = f(x)</code>, we should define other function, <code>x = g(x)</code> and then do the algorithm:</p> <pre><code>while (fabs(f(x)) &gt; etha) x = g(x); </code></pre> <p>To find out the root.</p> <p>The question is: how to define that <code>g(x)</code> using coefficient array and the range given only?</p> <p>The problem is: when i define <code>g(x)</code> like this </p> <p><img src="https://i.stack.imgur.com/efmVu.gif" alt="enter image description here"></p> <p>or </p> <p><img src="https://i.stack.imgur.com/tis1c.gif" alt="enter image description here"> </p> <p>for the equation given, any start value for <code>x</code> will lead me to the second equation's root. And no one of 'em would give me the other two (roots are <code>{ -2.5, 1.18, 6.05 }</code> and my code gives <code>1.18</code> only).</p> <p>My code is something like this:</p> <pre><code>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 phi(float x) { return (a[3] * -1.f) / ((a[0] * x * x) + (a[1] * x) + a[2]); } float iterationMethod(float a, float b) { float x = (a + b) / 2.f; while (fabs(f(x)) &gt; etha) { x = phi(x); } return x; } </code></pre> <p>So, calling the <code>iterationMethod()</code> passing ranges <code>{ -3, 0 }</code>, <code>{ 0, 3 }</code> and <code>{ 3, 10 }</code> will provide <code>1.18</code> number three times along.</p> <p>Where am i wrong and how should i act to get it work right?</p> <p><strong>UPD1</strong>: i do not need any third-party libraries.</p> <p><strong>UPD2</strong>: i need "Simple Iteration" algorithm exactly.</p>
 

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