Guest User

Untitled

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