Advertisement
rfq

Untitled

rfq
Feb 15th, 2023 (edited)
852
1
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 1 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. double f(double x) {
  7.     return exp(x) - 5 * pow(x, 2);
  8. }
  9.  
  10. int main() {
  11.     double a = 0., b = 1.;
  12.     double c = (a + b) / 2;
  13.     double fa = f(a), fb = f(b), fc = f(c);
  14.    
  15.     double eps = 0., epsT = 0.00001;
  16.    
  17.     int iter = 0;
  18.     while (fabs(b - a) > epsT) {
  19.         c = (a + b) / 2;
  20.         fc = f(c);
  21.  
  22.         eps = (b - a) / 2;
  23.         printf("[%d] a=%.6f | c=%.6f | b=%.6f | fa=%.6f | fc=%.6f | fb=%.6f | epsilon=%.6f\n",
  24.             iter, a, c, b, fa, fc, fb, eps);
  25.        
  26.         if (f(a) * fc < 0) {
  27.             b = c;
  28.         } else {
  29.             a = c;
  30.             fa = c;
  31.         }
  32.        
  33.         iter++;
  34.     }
  35.    
  36.     printf("\nAkar: %.6f | Iterasi berhenti di: %d", c, iter);
  37. }
  38.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement