SHARE
TWEET

Untitled

a guest May 16th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cmath>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5. #include <cstdlib>
  6.  
  7.  
  8. double fx(double x){
  9.     return std::log(std::pow(x,5) + 3.0 * std::pow(x, 2) + x + 9.0);
  10. }
  11.  
  12. double gx(double x){
  13.     return pow(x,6);
  14. }
  15.  
  16.  
  17.  
  18. int main(void) {
  19.  
  20.     double x_dok = -0.1673198;
  21.  
  22.     double xa = -0.5;
  23.     double xb = 1.0;
  24.     double eps = pow(10, -6);
  25.  
  26.     double r = (sqrt(5)-1.0) / 2.0;
  27.  
  28.     double lambda1 = r*r;
  29.     double lambda2 = r;
  30.  
  31.     double lambda1_b = 1.0 / 3.0;
  32.     double lambda2_b = 2.0 / 3.0;
  33.  
  34.  
  35.     double xmin;
  36.  
  37.     unsigned iteration = 0;
  38.  
  39.     std::fstream file;
  40.     file.open("wynik1.txt", std::ios::out);
  41.     file.close();
  42.  
  43.     // zadanie 1 i 2
  44.     do{
  45.         double x1 = xa + lambda1 * (xb-xa);
  46.         double x2 = xa + lambda2 * (xb-xa);
  47.  
  48.        
  49.         if(fx(x2) > fx(x1)){
  50.             xb = x2;
  51.         }
  52.         else if(fx(x2) < fx(x1)){
  53.             xa = x1;
  54.         }
  55.         xmin = (xb+xa)/2.0;
  56.         double diff = std::abs(xmin-x_dok);
  57.         file << iteration << "  " << xmin << "  " << diff << std::endl;
  58.         iteration++;
  59.     }  
  60.     while(std::abs(xa-xb) > std::pow(10, -6));
  61.  
  62.     xmin = (xb+xa)/2.0;
  63.     std::cout << iteration << ", " << xmin << ", dokladne -> " << x_dok << std::endl;
  64.    
  65.     // zadanie 3
  66.    
  67.     xa = -0.5;
  68.     xb = 1.0;
  69.    
  70.     do{
  71.         double x1 = xa + lambda1_b * (xb-xa);
  72.         double x2 = xa + lambda2_b * (xb-xa);
  73.  
  74.        
  75.         if(fx(x2) > fx(x1)){
  76.             xb = x2;
  77.         }
  78.         else if(fx(x2) < fx(x1)){
  79.             xa = x1;
  80.         }
  81.         xmin = (xb+xa)/2.0;
  82.         double diff = std::abs(xmin-x_dok);
  83.         file << iteration << "  " << xmin << "  " << diff << std::endl;
  84.         iteration++;
  85.     }  
  86.     while(std::abs(xa-xb) > std::pow(10, -6));
  87.  
  88.     xmin = (xb+xa)/2.0;
  89.     std::cout << iteration << ", " << xmin << ", dokladne -> " << x_dok << std::endl;
  90.  
  91.  
  92.  
  93.  
  94.     file.open("wynik2.txt", std::ios::out);
  95.    
  96.     file.close();
  97.  
  98.  
  99.     return 0;
  100. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top