Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- using namespace std;
- double e;
- double x1;
- double k;
- double f(double x,int s)
- {
- if (s==1)
- return 2.0*x*x+3.0*exp(-x);
- else if (s==2)
- return -exp(-x)*log(x);
- else if (s==3)
- return 2.0*x*x-exp(x);
- else if (s==4)
- return x*x*x*x-14*x*x*x+60.0*x*x-70.0*x;
- else if (s==5)
- {
- if(x>=0) return 4.0*x*x*x-3.0*x*x*x*x;
- else return 4.0*x*x*x+3.0*x*x*x*x;
- }
- else if (s==6)
- return x*x+2.0*x;
- else if (s==7)
- return 2.0*x*x+16.0/x;
- else if (s==8)
- 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);
- else if (s==9)
- return 3.0*x*x+12.0/(x*x*x)-5.0;
- }
- double df(double x,int s)
- {
- if (s==1)
- return 4*x-3*exp(-x);
- else if (s==2)
- return exp(-x)*(log(x)/x);
- else if (s==3)
- return 4*x-exp(x);
- else if (s==4)
- return 4*pow(x,3)-42*pow(x,2)+120*x-70;
- else if (s==5)
- {
- if(x>=0) return 12*pow(x,2)-12*pow(x,3);
- else return 12*pow(x,2)+12*pow(x,3);
- }
- else if (s==6)
- return 2*x+2;
- else if (s==7)
- return 4*x-(16/(x*x));
- else if (s==8)
- return 2*(10*pow(x,3)+3*x*x+x+5)*(30*x*x+6*x+1);
- else if (s==9)
- return 6*x-(36/pow(x,4));
- }
- void switchvar(int s)
- {
- switch(s)
- {
- case 1: x1=1; e=0.001; break;
- case 2: x1=0; e=0.0001; break;
- case 3: x1=1; e=0.001; break;
- case 4: x1=2; e=0.01; break;
- case 5: x1=0.4; e=0.001; break;
- case 6: x1=-4; e=0.01; break;
- case 7: x1=1; e=0.01; break;
- case 8: x1=2; e=0.01; break;
- case 9: x1=0.5; e=0.01; break;
- }
- }
- int swann(double &a, double &b, int s) //метод Свена
- {
- double h=x1==0?0.01:0.01*x1;
- double x2=x1+h;
- int t;
- if(f(x2, s) > f(x2-h, s)) h = -h;
- k = 1;
- while(1)
- {
- h=2*h;
- x2=x2+h;
- if(f(x2,s)>f(x2-h,s)) break;
- a=x2-h;
- k++;
- }
- b=x2;
- if (a>b)
- {
- t=b;
- b=a;
- a=t;
- }
- return k;
- }
- int swann2(double &a, double &b, int s)
- {
- double x2,t,h;
- h=x1==0?0.01:fabs(x1*0.01);
- x2=x1+h;
- if(df(x1,s)>0) h=-h;
- k = 1;
- while(1)
- {
- h=2*h;
- x2=x2+h;
- if(df(x2,s)*df(x2-h,s)<0) break;
- a=x2-h;
- k++;
- }
- if(x2-h<x2)
- {
- a=x2-h;
- b=x2;
- }
- else
- {
- a=x2;
- b=x2-h;
- }
- return k;
- }
- double dichotomy(double &a, double &b, int &k, double s, int r)
- {
- double d=e*0.1;
- double l,m;
- k=0;
- while(fabs(b-a)>=e)
- {
- if(r && k>=5) return 0; // 5 шагов для метода кубической интерполяции
- l=(a+b-d)/2;
- m=(a+b+d)/2;
- if(f(l,s) < f(m,s))
- b=m;
- else
- a=l;
- k++;
- }
- return((a+b)/2);
- }
- double bolcano(double &a, double &b, int &k, int s)
- {
- double x=(a+b)/2;
- k=1;
- while(fabs(df(x,s))>e && fabs(b-a)>e)
- {
- x=(a+b)/2;
- if(df(x,s)>0) b=x;
- else a=x;
- k++;
- }
- return x;
- }
- double kub_int(double &a, double &b, int &k, double s)
- {
- k=1;
- double z,w,y,x;
- while(1)
- {
- z=df(a,s)+df(b,s)+(3*(f(a,s)-f(b,s)))/(b-a);
- w=pow(z*z-df(a,s)*df(b,s),0.5);
- y=(z-df(a,s)+w)/(df(b,s)-df(a,s)+2*w);
- if( y >= 0 && y < 1 )
- x = a + y*(b-a);
- else if( y < 0 )
- return a;
- else
- return b;
- if(fabs(df(x,s)) < e) return x;
- if(fabs(df(x,s)) > 0)
- b = x;
- else
- a = x;
- k++;
- }
- return x;
- }
- int main()
- {
- double a,b,c,x;
- int s=6;
- int k=0;
- switchvar(s);
- //Поиск минимума с помощью дихотомии
- k=swann(a,b,s);
- cout << "a=" << a << " " << "b=" << b << " || k=" << k << endl;
- x=dichotomy(a,b,k,s,0);
- cout << "dichotomy xmin=" << x << " || k=" << k << endl;
- // Поиск минимума с помощью связки методов дихотомия - кубическая интерполяция
- k=swann2(a,b,s); //Метод Свенна-2
- cout << "\n\nSwann-2: a=" << a << " b=" << b << " || k=" << k << endl;
- x=dichotomy(a,b,k,s,1); // 5 шагов
- cout << "Dichtomy: a=" << a << " b=" << b << " || k=" << k << endl;
- x=kub_int(a,b,k,s); // кубическая интерполяция
- cout << "Cubic interpolation: x*=" << x << " || k=" << k << endl << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment