TheAlgorithms-C-Plus-Plus/computer_oriented_statistical_methods/false_position.cpp
2020-05-26 00:54:03 -04:00

39 lines
762 B
C++

#include <cmath>
#include <cstdlib>
#include <iostream>
static float eq(float i) {
return (pow(i, 3) - (4 * i) - 9); // origial equation
}
int main() {
float a, b, z, c, m, n;
system("clear");
for (int i = 0; i < 100; i++) {
z = eq(i);
if (z >= 0) {
b = i;
a = --i;
break;
}
}
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);
c = ((a * n) - (b * m)) / (n - m);
a = c;
z = eq(c);
if (z > 0 && z < 0.09) { // stoping criteria
break;
}
}
std::cout << "\n\nRoot: " << c;
return 0;
}