Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- double f(double x) {
- return exp(x) - 5 * pow(x, 2);
- }
- int main() {
- double a = 0., b = 1.;
- double c = (a + b) / 2;
- double fa = f(a), fb = f(b), fc = f(c);
- double eps = 0., epsT = 0.00001;
- int iter = 0;
- while (fabs(b - a) > epsT) {
- c = (a + b) / 2;
- fc = f(c);
- eps = (b - a) / 2;
- printf("[%d] a=%.6f | c=%.6f | b=%.6f | fa=%.6f | fc=%.6f | fb=%.6f | epsilon=%.6f\n",
- iter, a, c, b, fa, fc, fb, eps);
- if (f(a) * fc < 0) {
- b = c;
- } else {
- a = c;
- fa = c;
- }
- iter++;
- }
- printf("\nAkar: %.6f | Iterasi berhenti di: %d", c, iter);
- }
Advertisement
Advertisement