2019-08-21 10:10:08 +08:00
|
|
|
#include <iostream.h>
|
|
|
|
#include <conio.h>
|
|
|
|
#include <math.h>
|
2016-10-08 18:10:36 +08:00
|
|
|
|
|
|
|
float eq(float i)
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
return (pow(i, 3) - (4 * i) - 9); // original equation
|
2016-10-08 18:10:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
float a, b, x, z;
|
2016-10-08 18:10:36 +08:00
|
|
|
clrscr();
|
2019-08-21 10:10:08 +08:00
|
|
|
for (int i = 0; i < 100; i++)
|
2016-10-08 18:10:36 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
z = eq(i);
|
|
|
|
if (z >= 0)
|
2016-10-08 18:10:36 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
b = i;
|
|
|
|
a = --i;
|
2016-10-08 18:10:36 +08:00
|
|
|
goto START;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
START:
|
2016-10-08 18:10:36 +08:00
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
cout << "\nFirst initial: " << a;
|
|
|
|
cout << "\nSecond initial: " << b;
|
|
|
|
for (i = 0; i < 100; i++)
|
2016-10-08 18:10:36 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
x = (a + b) / 2;
|
|
|
|
z = eq(x);
|
|
|
|
cout << "\n\nz: " << z << "\t[" << a << " , " << b << " | Bisect: " << x << "]";
|
2016-10-08 18:10:36 +08:00
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
if (z < 0)
|
2016-10-08 18:10:36 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
a = x;
|
2016-10-08 18:10:36 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
b = x;
|
2016-10-08 18:10:36 +08:00
|
|
|
}
|
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
if (z > 0 && z < 0.0009) // stoping criteria
|
2016-10-08 18:10:36 +08:00
|
|
|
{
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
END:
|
|
|
|
cout << "\n\nRoot: " << x;
|
2016-10-08 18:10:36 +08:00
|
|
|
getch();
|
|
|
|
}
|