Guest User

Untitled

a guest
Dec 14th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. double e;
  7. double x1;
  8. double k;
  9. double f(double x,int s)
  10. {
  11.     if (s==1)
  12.         return 2.0*x*x+3.0*exp(-x);
  13.     else if (s==2)
  14.         return -exp(-x)*log(x);
  15.     else if (s==3)
  16.         return 2.0*x*x-exp(x);
  17.     else if (s==4)
  18.         return x*x*x*x-14*x*x*x+60.0*x*x-70.0*x;
  19.     else if (s==5)
  20.     {
  21.         if(x>=0) return 4.0*x*x*x-3.0*x*x*x*x;
  22.         else return 4.0*x*x*x+3.0*x*x*x*x;
  23.     }
  24.     else if (s==6)
  25.         return x*x+2.0*x;
  26.     else if (s==7)
  27.         return 2.0*x*x+16.0/x;
  28.     else if (s==8)
  29.         return (10.0*x*x*x+3.0*x*x+x+5.0)*(10.0*x*x*x+3.0*x*x+x+5.0);
  30.     else if (s==9)
  31.         return 3.0*x*x+12.0/(x*x*x)-5.0;
  32. }
  33.  
  34. double df(double x,int s)
  35. {
  36.     if (s==1)
  37.         return 4*x-3*exp(-x);
  38.     else if (s==2)
  39.         return exp(-x)*(log(x)/x);
  40.     else if (s==3)
  41.         return 4*x-exp(x);
  42.     else if (s==4)
  43.         return 4*pow(x,3)-42*pow(x,2)+120*x-70;
  44.     else if (s==5)
  45.     {
  46.         if(x>=0) return 12*pow(x,2)-12*pow(x,3);
  47.         else return 12*pow(x,2)+12*pow(x,3);
  48.     }
  49.     else if (s==6)
  50.         return 2*x+2;
  51.     else if (s==7)
  52.         return 4*x-(16/(x*x));
  53.     else if (s==8)
  54.         return 2*(10*pow(x,3)+3*x*x+x+5)*(30*x*x+6*x+1);
  55.     else if (s==9)
  56.         return 6*x-(36/pow(x,4));
  57. }
  58.  
  59. void switchvar(int s)
  60. {
  61.     switch(s)
  62.     {
  63.     case 1: x1=1; e=0.001; break;
  64.     case 2: x1=0; e=0.0001; break;
  65.     case 3: x1=1; e=0.001; break;
  66.     case 4: x1=2; e=0.01; break;
  67.     case 5: x1=0.4; e=0.001; break;
  68.     case 6: x1=-4; e=0.01; break;
  69.     case 7: x1=1; e=0.01; break;
  70.     case 8: x1=2; e=0.01; break;
  71.     case 9: x1=0.5; e=0.01; break;
  72.     }
  73. }
  74. int swann(double &a, double &b, int s) //метод Свена
  75. {
  76.     double h=x1==0?0.01:0.01*x1;
  77.     double x2=x1+h;
  78.     int t;
  79.     if(f(x2, s) > f(x2-h, s)) h = -h;
  80.     k = 1;
  81.  
  82.     while(1)
  83.     {
  84.         h=2*h;
  85.         x2=x2+h;
  86.         if(f(x2,s)>f(x2-h,s)) break;
  87.         a=x2-h;
  88.         k++;
  89.     }
  90.     b=x2;
  91.     if (a>b)
  92.     {
  93.         t=b;
  94.         b=a;
  95.         a=t;
  96.     }
  97.     return k;
  98. }
  99. int swann2(double &a, double &b, int s)
  100. {
  101.     double x2,t,h;
  102.     h=x1==0?0.01:fabs(x1*0.01);
  103.     x2=x1+h;
  104.     if(df(x1,s)>0) h=-h;
  105.     k = 1;
  106.     while(1)
  107.     {
  108.         h=2*h;
  109.         x2=x2+h;
  110.         if(df(x2,s)*df(x2-h,s)<0) break;
  111.         a=x2-h;
  112.         k++;
  113.     }
  114.     if(x2-h<x2)
  115.     {
  116.         a=x2-h;
  117.         b=x2;
  118.     }
  119.     else
  120.     {
  121.         a=x2;
  122.         b=x2-h;
  123.     }
  124.     return k;
  125.  
  126. }
  127. double dichotomy(double &a, double &b, int &k, double s, int r)
  128. {
  129.     double d=e*0.1;
  130.     double l,m;
  131.     k=0;
  132.     while(fabs(b-a)>=e)
  133.     {
  134.         if(r && k>=5) return 0; // 5 шагов для метода кубической интерполяции
  135.         l=(a+b-d)/2;
  136.         m=(a+b+d)/2;
  137.         if(f(l,s) < f(m,s))
  138.             b=m;
  139.         else
  140.             a=l;
  141.         k++;
  142.     }
  143.     return((a+b)/2);
  144. }
  145.  
  146. double bolcano(double &a, double &b, int &k, int s)
  147. {
  148.     double x=(a+b)/2;
  149.     k=1;
  150.     while(fabs(df(x,s))>e && fabs(b-a)>e)
  151.     {
  152.         x=(a+b)/2;
  153.         if(df(x,s)>0) b=x;
  154.         else a=x;
  155.         k++;
  156.     }
  157.     return x;
  158. }
  159.  
  160. double kub_int(double &a, double &b, int &k, double s)
  161. {
  162.     k=1;
  163.     double z,w,y,x;
  164.     while(1)
  165.     {
  166.         z=df(a,s)+df(b,s)+(3*(f(a,s)-f(b,s)))/(b-a);
  167.         w=pow(z*z-df(a,s)*df(b,s),0.5);
  168.         y=(z-df(a,s)+w)/(df(b,s)-df(a,s)+2*w);
  169.         if( y >= 0 && y < 1 )
  170.             x = a + y*(b-a);
  171.         else if( y < 0 )
  172.             return a;
  173.         else
  174.             return b;
  175.         if(fabs(df(x,s)) < e) return x;
  176.         if(fabs(df(x,s)) > 0)
  177.             b = x;
  178.         else
  179.             a = x;
  180.         k++;
  181.     }
  182.     return x;
  183. }
  184.  
  185.  
  186. int main()
  187. {
  188.     double a,b,c,x;
  189.     int s=6;
  190.     int k=0;
  191.     switchvar(s);
  192.     //Поиск минимума с помощью дихотомии
  193.     k=swann(a,b,s);
  194.     cout << "a=" << a << "  " << "b=" << b << " || k=" << k << endl;
  195.     x=dichotomy(a,b,k,s,0);
  196.     cout << "dichotomy xmin=" << x << " || k=" << k << endl;
  197.  
  198.     // Поиск минимума с помощью связки методов дихотомия - кубическая интерполяция
  199.     k=swann2(a,b,s); //Метод Свенна-2
  200.     cout << "\n\nSwann-2: a=" << a << " b=" << b << " || k=" << k << endl;
  201.     x=dichotomy(a,b,k,s,1); // 5 шагов
  202.     cout << "Dichtomy: a=" << a << " b=" << b << " || k=" << k << endl;
  203.     x=kub_int(a,b,k,s); // кубическая интерполяция
  204.     cout << "Cubic interpolation: x*=" << x << " || k=" << k << endl << endl;
  205.     return 0;
  206. }
Add Comment
Please, Sign In to add comment