Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Различные алгоритмы минимизации, реализованные в С++ / all / LAB-02-1
.CPP#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <process.h>
//#include <string.h>
/* ------------ -------------*//* ------------ -------------*/
double f(double x) // set of the function;
{
// function --> (10*x*x*x+3*x*x+x+5)*(10*x*x*x+3*x*x+x+5);
return ((10*x*x*x+3*x*x+x+5)*(10*x*x*x+3*x*x+x+5));
}
// end
/* ------------ -------------*//* ------------ -------------*/
double df(double x) // proisvodnay ot function;
{
// function --> (10*x*x*x+3*x*x+x+5)*(10*x*x*x+3*x*x+x+5);
return ((30*x*x+6*x+1)*(10*x*x*x+3*x*x+x+5)*2);
}
// end
double fGC2(double x) // set of the function Gold C-2;
{
// function --> (3 * x * x + ( 12 / (x*x*x)) - 5);
return (3 * x * x + ( 12 / (x*x*x)) - 5);
//return (6 * x + ( -36 / (x*x*x*x)));
}
double dfGC2(double x) // set of the function Gold C-2;
{
// function --> (3 * x * x + ( 12 / (x*x*x)) - 5);
//return (3 * x * x + ( 12 / (x*x*x)) - 5);
return (6 * x + ( -36 / (x*x*x*x)));
}
// end
/* ------------ -------------*//* ------------ -------------*/
double fibbo(double n) // function Fibbonashi
{
double Fn,Fn1,F_help;
int i;
Fn1=1;
Fn=1;
i=2;
while(i<n)
{
F_help=Fn;
Fn=Fn+Fn1;
Fn1=F_help;
i++;
}
return Fn;
}
// end
/* ------------ -------------*//* ------------ -------------*/
int main() // main function
{
clrscr();
double a, b, c , v ;
double x1, x2, x0, step;
int k=0, n=0;
double a1, b1;
double aGC, bGC;
/* ----------- Metog Swenn ------------ */
/* ----------- Start ------------ */
step=.01; x1=0.5;
/*printf("Enter argument ");
cin>>x1;
printf("f(x1)= %f\n", f(x1));*/
x2 = x1 + step;
if (fGC2(x2) > fGC2(x1)) {step = -step; x2 = x1 + step;}
k=0; //x3=x1; x1=x2; x2=x3+step;
while (fGC2(x2)<fGC2(x1))
{
k++; x0=x1; x1=x2;
x2 = x2 + step;
step = step * 2;
} //tek:x2; pred:x1; predpred: x0;
if (x2<x0)
{ printf(" Space of Cwenn: [%f, %f]", x2, x0);
a=x2; b=x0; a1=x2; b1=x0; aGC=x2; bGC=x0;
}
else
{ printf("Space of Cwenn [ %f, %f]", x0, x2);
a=x0; b =x2; a1=x0; b1=x2; aGC=x0; bGC=x2;
}
cout<<" , Cshetshik - "<<k;
getch();
/* ----------- Metog Fibbonashi ------------ */
/* ----------- Start ------------ */
/* testing fibbo func
k=3;
while (k!=0) {
cout<<" k=";
cin>>k;
cout<<fibbo(k);
}*/
double L1, Ln, Fn, e;
L1 = fabs(b - a);
Ln = .000001;
Fn = L1/Ln;
n=10;
while (fibbo(n)<Fn) {n++;}
Fn=fibbo(n);
cout<<endl;
cout<<" Fn= "<<Fn<<", n= "<<n<<endl;
//n=k;
e = ( L1 / fibbo(n+1) ) - Ln * 0.1;
if (e<0) {cout<<"Achtung!!!"<<endl; exit;}
cout<<", e="<<e<<endl;
x1 = a + fibbo(n-1) * L1 / Fn + pow(-1,n) * e / Fn;
k=1;
while (n!=k)
{
x2 = a + b - x1;
if ((x1<x2) & (fGC2(x1)<fGC2(x2)))
{
b=x2;
}
if ((x1<x2) & (fGC2(x1)>=fGC2(x2)))
{
a=x1;
x1=x2;
}
if ((x1>x2) & (fGC2(x1)<fGC2(x2)))
{
a=x2;
}
if ((x1>x2) & (fGC2(x1)>=fGC2(x2)))
{
b=x1;
x1=x2;
}
k++;
}
cout<<endl;
cout<<" Min Fibbonashi -> "<<x2<<" , kol-vo iterahiu -> n="<<k<<endl;
getch();
/* ----------- Metog Fibbonashi ------------ */
/* ----------- End ------------ */
/* ----------- Metog Bolshano ------------ */
/* ----------- Start ------------ */
e=.0001; // tohnoct
a=a1;
b=b1;
if ( (fGC2(a) * fGC2(b)) < 0)
{
do
{
c = (a + b) / 2; // c -> middle spase
v = fGC2(c);
if ( v > 0) b=c; // a,b ->
else a=c;
} while ( (fabs(b-a) > e) | (v > e));
cout<<endl;
cout<<" Min Bolshano -> "<<c <<endl;
getch();
}
else
{
cout<<endl;
cout<<" Can not Metog Bolshano use! "<<endl;
getch();
}
/* ----------- Metog Bolshano ------------ */
/* ----------- End ------------ */
/* ----------- Metog Gold C-2 ------------ */
/* ----------- Start ------------ */
// double m ;//, ll;
e=0.000001;
L1 = fabs (bGC - aGC);
x1 = aGC + 0.618 * L1;
// ll = bGC - 0.382 * L1;
k=1;
while ((fabs(bGC - aGC) > e) & (k < 30000) )
{
x2 = aGC + bGC - x1;
if ((x1<x2) & (fGC2(x1)<fGC2(x2)))
{
bGC=x2;
}
if ((x1<x2) & (fGC2(x1)>=fGC2(x2)))
{
aGC=x1;
// x1=x2;
}
if ((x1>x2) & (fGC2(x1)<fGC2(x2)))
{
aGC=x2;
}
if ((x1>x2) & (fGC2(x1)>=fGC2(x2)))
{
bGC=x1;
// x1=x2;
}
L1 = fabs (bGC - aGC);
x1 = aGC + 0.618 * L1;
k++;
}
if (fabs(bGC - aGC) <= e)
{
x2 = (aGC + bGC) / 2;
cout<<endl;
cout<<" Min Gold C-2-> "<<x2<<" , kol-vo iterahiu -> n="<<k<<endl;
getch();
}
else
{
cout<<endl;
cout<<" Can not Min Metog Gold C-2 find!"<<endl;;
getch();
}
/* ----------- Metog Gold C-2 ------------ */
/* ----------- End ------------ */
return 0;
}
// end main
/* ------------ -------------*//* ------------ -------------*/
Соседние файлы в папке all