Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. // MO_Powell.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <cmath>
  7.  
  8. double f(double p1, double p2, double x, double y, double alpha)
  9. {
  10.     return pow((p1 + x*alpha - 2.0), 2.0) + pow((p2 + y*alpha + 1.0), 2.0) - 0.5*(p1 + x*alpha)*(p2 + y*alpha) + 4.0;
  11.     //return  sin(p1 + x*alpha + 0.8) *sin(p1 + x*alpha + 0.8) + sin(p2 + y*alpha - 0.2)* sin(p2 + y*alpha - 0.2);
  12.  
  13. }
  14.  
  15. int main()
  16. {
  17.    
  18.     double a = -1.0, b = 3.0; //interval
  19.     double e[2] = { 1.0,0 }; //direction
  20.     double x_init[2] = { 0,0 }; //
  21.     double x_second[2] = { 0,0 };
  22.     double accuracy = 0.01;
  23.     double x1, x2;
  24.  
  25.     //25% method
  26.  
  27.     x1 = a + (b - a)*0.25;
  28.     x2 = b - (b - a)*0.25;
  29.  
  30.     std::cout << x1;
  31.     std::cout << x2;
  32.     double min;
  33.  
  34.     while (abs(x2-x1)>accuracy)
  35.     {
  36.         if (f(x_init[0], x_init[1], e[0], e[1], x1) >= f(x_init[0], x_init[1], e[0], e[1], x2))
  37.         {
  38.             min = f(x_init[0], x_init[1], e[0], e[1], x2);
  39.             a = x1;
  40.             x1 = a + (b - a)*0.25;
  41.             x2 = b - (b - a)*0.25;
  42.         }
  43.  
  44.         else if (f(x_init[0], x_init[1], e[0], e[1], x2) > f(x_second[0], x_second[1], e[0], e[1], x1))
  45.         {
  46.             min = f(x_init[0], x_init[1], e[0], e[1], x1);
  47.             b = x2;
  48.             x1 = a + (b - a)*0.25;
  49.             x2 = b - (b - a)*0.25;
  50.         }
  51.         else continue;
  52.     }
  53.  
  54.     std::cout << "local min at" << (x1 + x2) / 2.0 << " with value " <<min<<std::endl;
  55.  
  56.     x_second[0] = (x1 + x2) / 2.0;
  57.     e[0] = 0;
  58.     e[1] = 1;
  59.     a = -1.0;
  60.     b = 3.0;
  61.  
  62.     x1 = a + (b - a)*0.25;
  63.     x2 = b - (b - a)*0.25;
  64.  
  65.     std::cout << "second iteration: "<<std::endl;
  66.  
  67.     while (abs(x2 - x1)>accuracy)
  68.     {
  69.         if (f(x_second[0], x_second[1], e[0], e[1], x1) >= f(x_second[0], x_second[1], e[0], e[1], x2))
  70.         {
  71.             min = f(x_init[0], x_init[1], e[0], e[1], x2);
  72.             b = x2;
  73.             x1 = a + (b - a)*0.25;
  74.             x2 = b - (b - a)*0.25;
  75.         }
  76.         else if (f(x_init[0], x_init[1], e[0], e[1], x2) > f(x_second[0], x_second[1], e[0], e[1], x1))
  77.         {
  78.             min = f(x_second[0], x_second[1], e[0], e[1], x1);
  79.             b = x2;
  80.             x1 = a + (b - a)*0.25;
  81.             x2 = b - (b - a)*0.25;
  82.         }
  83.         else continue;
  84.     }
  85.  
  86.     std::cout << "local min at: " << (x1 + x2) / 2.0 << " with value " << min<<std::endl;
  87.  
  88.     e[0] = x_second[0] - x_init[0];
  89.     e[1] = x_second[1] - x_init[1];
  90.     a = -1.0;
  91.     b = 3.0;
  92.     x1 = a + (b - a)*0.25;
  93.     x2 = b - (b - a)*0.25;
  94.     std::cout << x1 << "," << x2 << "," << e[0] << "," << e[1] << "," << a << "," << b << std::endl;
  95.     std::cout << "third iteration: " << std::endl;
  96.  
  97.     while (abs(x2 - x1)>=accuracy)
  98.     {
  99.        
  100.         if (f(x_second[0], x_second[1], e[0], e[1], x1) >= f(x_second[0], x_second[1], e[0], e[1], x2))
  101.         {
  102.             min = f(x_init[0], x_init[1], e[0], e[1], x2);
  103.             b = x2;
  104.             x1 = a + (b - a)*0.25;
  105.             x2 = b - (b - a)*0.25;
  106.            
  107.         }
  108.         else if (f(x_init[0], x_init[1], e[0], e[1], x2) > f(x_second[0], x_second[1], e[0], e[1], x1))
  109.         {
  110.             min = f(x_second[0], x_second[1], e[0], e[1], x1);
  111.             b = x2;
  112.             x1 = a + (b - a)*0.25;
  113.             x2 = b - (b - a)*0.25;
  114.  
  115.         }
  116.     }
  117.  
  118.     std::cout << "local min at: " << (x1 + x2) / 2.0 << " with value " << min;
  119.  
  120.     system("pause");
  121.  
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement