Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <stdlib.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.003; 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 &k, 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;
- }
- /*if(x2-h<x2) { a=x2-h; b=x2; }
- else { a=x2; b=x2-h; }*/
- return k;
- }
- int swann2(double &a, double &b, int &k, int s)
- {
- double x2,t,h;
- h=x1==0?0.01: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++;
- //system("pause");
- }
- if(x2-h<x2) { a=x2-h; b=x2; }
- else { a=x2; b=x2-h; }
- /*if (a>b)
- {
- t=b;
- b=a;
- a=t;
- }*/
- return k;
- }
- double dichotomy(double &a, double &b, int &k, double s)
- {
- double d=e*0.1;
- double a1,b1;
- k=0;
- while(k < 5)
- {
- a1=(a+b-d)/2;
- b1=(a+b+d)/2;
- if(f(a1,s) < f(b1,s)) b=b1;
- else a=a1;
- 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/*k<6*/)
- {
- 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)
- {
- double z=df(a,s)+df(b,s)+3*(f(a,s)-f(b,s))/(b-a);
- double w=pow(z*z-df(a,s)*df(b,s),0.5);
- double Y=(z-df(a,s)+w)/(df(b,s)-df(a,s)+2*w);
- double x=a+Y*(b-a);
- //double x=d;
- k=1;
- while(fabs(df(x,s))>e && x!=a && x!=b)
- {
- if(df(x,s)>0) b=x;
- else a=x;
- 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);
- x=a+Y*(b-a);
- k++;
- }
- return x;//=(a+b)/2.0;
- }
- int main()
- {
- double a,b,x;
- int s=6;
- int k;
- switchvar(s);
- //Свенн
- k=swann(a,b,k,s);
- cout<<endl<<"Swann-dichotomy:"<<endl;
- cout << "func #" << s << ": a=" << a << " " << "b=" << b << " || k=" << k << endl;
- x=dichotomy(a,b,k,s);
- cout << "dichotomy xmin=" << x << " || k=" << k << endl;
- //k=0; a=0; b=0;
- // кубическая интерполяция
- cout << "\nSwann-2 - dichotomy - kub_int:\n";
- k=swann2(a,b,k,s); //Метод Свенна-2
- cout << "Swann-2: a=" << a << " b=" << b << " || k=" << k << endl;
- x=dichotomy(a,b,k,s); // 5 шагов
- //x=bolcano(a,b,k,s);
- cout << "Bolcano: a=" << a << " b=" << b << " || k=" << k << endl;
- x=kub_int(a,b,k,s); // кубическая интерполяция
- cout << "x=" << x << " || k=" << k;
- return 0;
- }
Add Comment
Please, Sign In to add comment