Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. //---------------------------------------------------------------------------
  2.  
  3. #include <vcl.h>
  4. #pragma hdrstop
  5.  
  6. //---------------------------------------------------------------------------
  7.  
  8. #pragma argsused
  9. #include<iostream.h>
  10. #include<conio.h>
  11. #include<math.h>
  12.  
  13.  
  14. int iter, funct;
  15. double m_x;
  16.  
  17. double func_y(double x)
  18. {
  19.     return pow(M_E,x)-(x*x*x)/3+2*x;
  20. }
  21.  
  22. double general_search(double a, double b, double e)
  23. {
  24.     iter=0;
  25.     funct=0;
  26.     int n=2/e+1;
  27.     double h=fabs(a-b)/n;
  28.     iter++;
  29.  
  30.     double m_y, temp_x, temp_y;
  31.  
  32.    
  33.  
  34.     m_x=a;
  35.     m_y=func_y(m_x);
  36.     funct++;
  37.     temp_x=m_x;
  38.  
  39.     while(temp_x<b)
  40.     {
  41.         temp_x+=h;
  42.         temp_y=func_y(temp_x);
  43.         funct++;
  44.  
  45.         if(temp_y<m_y)
  46.         {
  47.             m_y=temp_y;
  48.             m_x=temp_x;
  49.         }
  50.     }
  51.     return m_y;
  52. }
  53.  
  54.  
  55. double interval_bisection(double a,double b,double e)
  56. {
  57.     iter=0;
  58.     funct=0;
  59.  
  60.     double x1, x2, x3, x4, x5;
  61.     double y1, y2, y3, y4, y5;
  62.     double m_y;
  63.  
  64.  
  65.     x1 = a; x5 = b;
  66.  
  67.     x3 = x1 + (x5 - x1)/2.0;
  68.     y3 = func_y(x3);
  69.     funct++;
  70.  
  71.     while (fabs(x5 - x1) > e)
  72.     {
  73.         iter++;
  74.  
  75.  
  76.         x2 = x1 + (x3 - x1)/2.0;
  77.         x4 = x5 - (x5 - x3)/2.0;
  78.  
  79.         y2 = func_y(x2);
  80.         y4 = func_y(x4);
  81.         funct += 2;
  82.  
  83.  
  84.         if (y2<y3)
  85.         {
  86.             x5 = x3;
  87.             y5 = y3;
  88.             x3=x2;
  89.             y3=y2;
  90.             m_x = x2;
  91.             m_y = y2;
  92.             continue;
  93.         }
  94.  
  95.         if (y4<y3)
  96.         {
  97.             x1 = x3;
  98.             y1 = y3;
  99.             x3=x4;
  100.             y3=y4;
  101.             m_x = x4;
  102.             m_y = y4;
  103.             continue;
  104.         }
  105.  
  106.         else
  107.         {
  108.             x1 = x2;
  109.             y1 = y2;
  110.             x5=x4;
  111.             y5=y4;
  112.             m_x = x3;
  113.             m_y = y3;
  114.             continue;
  115.         }
  116.  
  117.     }
  118.  
  119.     return m_y;
  120. }
  121.  
  122. double gold_section(double a, double b, double e)
  123. {
  124.     double f = (sqrt(5) + 1)/2;
  125.     double x1 = a, x4 = b, x2, x3;
  126.     double y1 =func_y(x1), y4 = func_y(x4), y2, y3;
  127.     double gold, m_y;
  128.  
  129.     funct = 2;
  130.     iter = 0;
  131.  
  132.     gold = (x4 - x1)/f;
  133.     x2 = x4 - gold;
  134.     x3 = x1 + gold;
  135.  
  136.     y2 = func_y(x2);
  137.     y3 = func_y(x3);
  138.     funct+=2;
  139.    
  140.     while (fabs(x4 - x1) > e)
  141.     {
  142.         iter++;
  143.  
  144.  
  145.         if (y2 < y3)
  146.         {
  147.             m_x = x2; m_y = y2;
  148.             x4 = x3; y4 = y3;
  149.             x3 = x2; y3 = y2;
  150.             gold =(x4 - x1)/f;
  151.             x2 = x4 - gold;
  152.             y2 = func_y(x2);
  153.             funct++;
  154.             continue;
  155.         }
  156.  
  157.         if (y3 < y2)
  158.         {
  159.             m_x = x3; m_y = y3;
  160.             x1 = x2; y1 = y2;
  161.             x2 = x3; y2 = y3;
  162.             gold =(x4 - x1)/f;
  163.             x3 = x1 + gold;
  164.             y3 = func_y(x3);
  165.             funct++;
  166.             continue;
  167.         }
  168.     }
  169.  
  170.     return m_y;
  171. }
  172.  
  173.  
  174. void main()
  175. {
  176.     cout.precision(10);
  177.     cout<<"General search:\n\nf(x*)="<<general_search(-2,0,0.00001)<<endl;
  178.     cout<<"x*="<<m_x<<endl;
  179.     cout<<"Function usage counter: "<<funct<<endl;
  180.     cout<<"Iteration counter: "<<iter<<endl;
  181.  
  182.     cout<<"\nInterval Bisection:\n\nf(x*)="<<interval_bisection(-2,0,0.00001)<<endl;
  183.     cout<<"x*="<<m_x<<endl;
  184.     cout<<"Function usage counter: "<<funct<<endl;
  185.     cout<<"Iteration counter: "<<iter<<endl;
  186.  
  187.     cout<<"\nGold Section:\n\nf(x*)="<<gold_section(-2,0,0.00001)<<endl;
  188.     cout<<"x*="<<m_x<<endl;
  189.     cout<<"Function usage counter: "<<funct<<endl;
  190.     cout<<"Iteration counter: "<<iter<<endl;
  191.     getch();
  192. }
  193. //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement