Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdlib>
  2. #include <cmath>
  3. #include <iostream>
  4.  
  5. //QUADRATIC APPROX
  6.  
  7. //accuracy is a parameter that tells us when algorithm will stop.
  8. //If difference between in x after another algorithm step is less than that accuracy algorithm will be stopped
  9. #define ACCURACY 0.01
  10. //a is left border of area where we look for answer and b is right
  11. double a = -1;
  12. double b = 1;
  13.  
  14. //our function in which we are trying to find minimum in range [-1,1]
  15. double function1(double x){
  16.     double s = sin(2*x+1)+2;
  17.     return 1 / (1 + exp(s));
  18. }
  19.  
  20. double function2(double x){
  21.     return -sin(x+1);
  22. }
  23.  
  24. //this function calculates next x (x2) using quadratic approximation (check lab pdf for more info)
  25. //it takes as parameters pointer to function we calculate and previous x (x1)
  26. double qudratApprox(double x1, double(*func)(double)){
  27.     double x2;
  28.     x2 = (func(a)*(pow(b,2) - pow(x1,2)) + func(x1)*(pow(a,2) - pow(b,2)) + func(b)*(pow(x1,2) - pow(a,2)))/
  29.         (func(a)*(b-x1) + func(x1)*(a-b) + func(b)*(x1-a));
  30.     x2 *= 0.5;
  31.  
  32.     //x2 has to be beetween left and right border otherwise more advanced implementation of the algorithm has to be used
  33.     if(a < x2 && b > x2){
  34.        
  35.         return x2;
  36.     }else{
  37.         std::cout<< "ERROR HAS OCCURED\n";
  38.     }
  39.     return x2;
  40. }
  41.  
  42.  
  43. int main(){
  44.  
  45.     //for the beggining we put x in the middle of our range
  46.     double x = 0;
  47.  
  48.     //variable for storing next x
  49.     double x2;
  50.  
  51.     //check internet to find out what is do while loop
  52.     do{
  53.         //first we calculate our next x
  54.         x2 = qudratApprox(x,function1);
  55.         //std::cout << x2 << std::endl;
  56.  
  57.         //now we are comparing which in which point function is bigger
  58.         //(check lab pdf why we do that, that is the essential of this algorithm)
  59.         if(function1(x) > function1(x2)){
  60.             //this and below if statements are for swiping,
  61.             //we will shrink our range from left or right side depending on what side are x and x2
  62.             if(x < x2)  a = x;
  63.             else b = x;
  64.         }else{
  65.             if(x < x2)  a = x2;
  66.             else b = x2;
  67.         }
  68.         //again we put our x in the middle of the new shrinken range
  69.         x = (a+b)/2;
  70.         std::cout << "x = " << x << std::endl;
  71.  
  72.     //we repeat these steps until difference between two steps is smaller than predefined accuracy
  73.     }while(abs(x-x2) >= ACCURACY);
  74.  
  75.     //finally we print out our result
  76.     std::cout << "xmin = " << x << std::endl;
  77.     std::cout << "f(xmin) = " << function1(x) << std::endl;
  78.  
  79.     return 1;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement