2020-05-26 10:56:38 +08:00
|
|
|
#include <cmath>
|
|
|
|
#include <iostream>
|
2016-10-12 02:33:09 +08:00
|
|
|
|
2020-05-26 10:56:38 +08:00
|
|
|
float eq(float i) {
|
|
|
|
return (pow(i, 3) - (4 * i) - 9); // original equation
|
2016-10-12 02:33:09 +08:00
|
|
|
}
|
|
|
|
|
2020-05-26 10:56:38 +08:00
|
|
|
int main() {
|
|
|
|
float a, b, z, c, m, n;
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
z = eq(i);
|
|
|
|
if (z >= 0) {
|
|
|
|
b = i;
|
|
|
|
a = --i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-10-12 02:33:09 +08:00
|
|
|
|
2020-05-26 10:56:38 +08:00
|
|
|
std::cout << "\nFirst initial: " << a;
|
|
|
|
std::cout << "\nSecond initial: " << b;
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
float h, d;
|
|
|
|
m = eq(a);
|
|
|
|
n = eq(b);
|
2016-10-12 02:33:09 +08:00
|
|
|
|
2020-05-26 10:56:38 +08:00
|
|
|
c = ((a * n) - (b * m)) / (n - m);
|
|
|
|
a = b;
|
|
|
|
b = c;
|
2016-10-12 02:33:09 +08:00
|
|
|
|
2020-05-26 10:56:38 +08:00
|
|
|
z = eq(c);
|
|
|
|
if (z > 0 && z < 0.09) // stoping criteria
|
|
|
|
break;
|
|
|
|
}
|
2016-10-12 02:33:09 +08:00
|
|
|
|
2020-05-26 10:56:38 +08:00
|
|
|
std::cout << "\n\nRoot: " << c;
|
|
|
|
return 0;
|
2016-10-12 02:33:09 +08:00
|
|
|
}
|