Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. void regraAurea();
  7.  
  8. int main()
  9. {
  10.     regraAurea();
  11.  
  12.     return 0;
  13. }
  14.  
  15. double f(double x){
  16.     return pow(2*x + 1, 2) - 5*cos(10*x);
  17. }
  18.  
  19. void regraAurea(){
  20.     double x1 = -1, x2 = 0, x3 = 0, x4 = 0, B = (sqrt(5) - 1)/2, A = pow(B, 2);
  21.    
  22.     while (abs((x2 - x1)/x2) >= 0.005)
  23.     {
  24.         x3 = x1 + A*(x2 - x1);
  25.         x4 = x1 + B*(x2 - x1);
  26.        
  27.         //minimo
  28.         if (f(x3) < f(x4))
  29.         {
  30.             x1 = x1;
  31.             x2 = x4;
  32.         }
  33.         else
  34.         {
  35.             x1 = x3;
  36.             x2 = x2;
  37.         }
  38.     }
  39.    
  40.     cout << "Minimo:\n";
  41.     cout << "x1 = " << x1 << endl;
  42.     cout << "x2 = " << x2 << endl;
  43.     cout << "x3 = " << x3 << endl;
  44.     cout << "x4 = " << x4 << endl;
  45.    
  46.     x1 = -1;
  47.     x2 = 0;
  48.    
  49.     while (abs((x2 - x1)/x2) >= 0.005)
  50.     {
  51.         x3 = x1 + A*(x2 - x1);
  52.         x4 = x1 + B*(x2 - x1);
  53.        
  54.         //maximo
  55.         if (f(x3) > f(x4))
  56.         {
  57.             x1 = x1;
  58.             x2 = x4;
  59.         }
  60.         else
  61.         {
  62.             x1 = x3;
  63.             x2 = x2;
  64.         }
  65.     }
  66.    
  67.     cout << "Maximo:\n";
  68.     cout << "x1 = " << x1 << endl;
  69.     cout << "x2 = " << x2 << endl;
  70.     cout << "x3 = " << x3 << endl;
  71.     cout << "x4 = " << x4 << endl;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement