Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <process.h>
/* ------------ -------------*//* ------------ -------------*/
double f(double x) // set of the function;
{
// function --> (3 * x * x + ( 12 / (x*x*x)) - 5);
return (3 * x * x + ( 12 / (x*x*x)) - 5);
}
// end
/* ------------ -------------*//* ------------ -------------*/
double df(double x) // proisvodnay ot function;
{
// function --> return (6 * x + ( -36 / (x*x*x*x)));
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
/* ----------- Metog Swenn ------------ */
/* ----------- Start ------------ */
void Swenn (double step , double x1 ,double *a,double *b)
{
double x2, x0;
int k=0;
/*printf("Enter argument ");
cin>>x1;
printf("f(x1)= %f\n", f(x1));*/
x2 = x1 + step;
if (f(x2) > f(x1)) {step = -step; x2 = x1 + step;}
k=0;
while (f(x2)<f(x1))
{
k++; x0=x1; x1=x2;
x2 = x2 + step;
step = step * 2;
} //tek:x2; pred:x1; predpred: x0;
if (x0>x2)
{
cout<<endl;cout<<endl;cout<<endl;
printf(" Space of Cwenn: [%f, %f]", x2, x0);
*a=x2; *b=x0; // a1=x2; b1=x0;
}
else
{
cout<<endl;cout<<endl;cout<<endl;
printf(" Space of Cwenn [ %f, %f]", x0, x2);
*a=x0; *b=x1; // a1=x0; b1=x1;
}
cout<<" , Cshetshik - "<<k;
cout<<endl;
}
/* ----------- Metog Swenn ------------ */
/* ----------- End ------------ */
/* ----------- Metog Gold C-2 ------------ */
/* ----------- Start ------------ */
void GoldC2(double aGC, double bGC)
{
double c , v , e , k, L1, x1, x2;
e=0.000001; //tochnoct
L1 = fabs (bGC - aGC);
x1 = aGC + 0.618 * L1;
// ll = bGC - 0.382 * L1;
k=1;
while (fabs(bGC - aGC) > e )
{
x2 = aGC + bGC - x1;
if ((x1<x2) & (f(x1)<f(x2)))
{
bGC=x2;
}
if ((x1<x2) & (f(x1)>=f(x2)))
{
aGC=x1;
}
if ((x1>x2) & (f(x1)<f(x2)))
{
aGC=x2;
}
if ((x1>x2) & (f(x1)>=f(x2)))
{
bGC=x1;
}
L1 = fabs (bGC - aGC);
x1 = aGC + 0.618 * L1;
k++;
}
if (fabs(bGC - aGC) <= e)
{
x2 = (aGC + bGC) / 2;
cout<<endl; cout<<endl; cout<<endl;
cout<<" ----------- Metog Gold C-2 ------------"<<endl;
cout<<endl; cout<<endl;
cout<<" Min Gold C-2-> "<<x2<<" , kol-vo iterahiu -> n="<<k<<endl;
}
else
{
cout<<endl; cout<<endl; cout<<endl;
cout<<" ----------- Metog Gold C-2 ------------"<<endl;
cout<<endl; cout<<endl;
cout<<" Can not Min Metog Gold C-2 find!"<<endl;;
}
}
/* ----------- Metog Gold C-2 ------------ */
/* ----------- End ------------ */
/* ----------- Metog Bolshano ------------ */
/* ----------- Start ------------ */
void Bolshano(double a, double b)
{
double c , v , e , k;
e=.000001; // tohnoct
k=0;
if ( (df(a) * df(b)) < 0)
{
do
{
k++;
c = (a + b) / 2; // c -> middle spase
v = df(c);
if ( v > 0) b=c; // a,b ->
else a=c;
} while ( (fabs(b-a) > e) | (v > e));
cout<<endl; cout<<endl; cout<<endl;
cout<<" ----------- Metog Bolshano ------------"<<endl;
cout<<endl; cout<<endl;
cout<<" Min Bolshano -> "<<c <<" , e = "<< e <<endl;
cout<<endl;
cout<<" kol-vo iterahiu -> n="<<k<<endl;
}
else
{
cout<<endl;
cout<<" Can not Metog Bolshano use! "<<endl;
}
}
/* ----------- Metog Bolshano ------------ */
/* ----------- End ------------ */
/* ----------- Metog Fibbonashi ------------ */
/* ----------- Start ------------ */
void Fibbonashi( double a, double b)
{
double x2, x0 , x1;
int k=0, n=0;
double a1, b1;
double L1, Ln, Fn, e;
/* testing fibbo func
k=3;
while (k!=0) {
cout<<" k=";
cin>>k;
cout<<fibbo(k);
}*/
L1 = fabs(b - a);
Ln = .000001;
Fn = L1/Ln;
n=10;
while (fibbo(n)<Fn) {n++;}
Fn=fibbo(n);
cout<<endl;cout<<endl;cout<<endl;
cout<<" ----------- Metog Fibbonashi ------------"<<endl;
cout<<endl;
cout<<" Fn = "<<Fn<<", n = "<<n;
e = ( L1 / fibbo(n+1) ) - Ln * 0.1;
if (e<0) {cout<<"Achtung!!!"<<endl; exit;}
cout<<" , e = "<<e<<endl;
cout<<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) & (f(x1)<f(x2)))
{
b=x2;
}
if ((x1<x2) & (f(x1)>=f(x2)))
{
a=x1;
x1=x2;
}
if ((x1>x2) & (f(x1)<f(x2)))
{
a=x2;
}
if ((x1>x2) & (f(x1)>=f(x2)))
{
b=x1;
x1=x2;
}
k++;
}
cout<<endl;
cout<<" Min Fibbonashi -> "<<x2<<" , kol-vo iterahiu -> n="<<k<<endl;
}
/* ----------- Metog Fibbonashi ------------ */
/* ----------- End ------------ */
/* ------------ ------------- Start Main ------------ -------------*/
int main() // main function
{
clrscr();
double a, b, x1, step;
char ch;
ch=0; step=.001; x1=0.5;
while (ch!= 27)
{
clrscr();
cout<<endl;cout<<endl;
printf(" Enter nomber ---------- \n"); cout<<endl;
printf(" 1. Min Metog Fibbonashi \n"); cout<<endl;
printf(" 2. Min Metog Bolshano \n"); cout<<endl;
printf(" 3. Min Metog Golg C-2 \n"); cout<<endl;
printf(" 4. Min Metog Fibbonashi & Bolshano & Golg C-2\n"); cout<<endl;
cout<<endl;cout<<endl;
printf(" Exit --> enter <Esc> \n"); cout<<endl;
ch=getch();
cout<<endl; if (ch!= 27) Swenn(step , x1, &a, &b);
switch(ch)
{
case '1':
Bolshano(a,b);
cout<<endl;cout<<endl; cout<<endl;cout<<endl;
printf(" Press any key \n");
getch();
clrscr();
break;
case '2':
Fibbonashi(a,b);
cout<<endl;cout<<endl; cout<<endl;cout<<endl;
printf(" Press any key \n");
getch();
clrscr();
break;
case '3':
GoldC2(a,b);
cout<<endl;cout<<endl; cout<<endl;cout<<endl;
printf(" Press any key \n");
getch();
clrscr();
break;
case '4':
//clrscr();
Bolshano(a,b);
Fibbonashi(a,b);
GoldC2(a,b);
cout<<endl;cout<<endl; cout<<endl;cout<<endl;
printf(" Press any key \n");
getch();
clrscr();
break;
} // switch(ch)
} // while (ch != 27)
return 0;
} // end main
/* ------------ ------------- End main ------------ -------------*/