Скачиваний:
14
Добавлен:
01.05.2014
Размер:
3.8 Кб
Скачать
#include <iostream.h>
#include <conio.h>
#include <math.h>

double d_left;
double d_right;
double d_startPoint=0;
double d_step;
double d_nextPoint;
double d_previousPoint;
double d_number;
double E=0.00001;
double xmed;

double func(double x)
{
double res=2*x*x+3*exp(-x);
	return res;
}

void Svenn()
{
if(d_startPoint==0)
	d_step=0.01;
else
	d_step=0.01*fabs(d_startPoint);
d_nextPoint=d_startPoint+d_step;
if(func(d_startPoint)<func(d_nextPoint))
 {
 d_step=-d_step;
 d_nextPoint=d_startPoint+d_step;
 }
while(func(d_nextPoint)<func(d_startPoint))
 {
 d_previousPoint=d_startPoint;
 d_startPoint=d_nextPoint;
 d_nextPoint=d_startPoint+d_step;
 d_step=2*d_step;
 }
d_left=d_previousPoint;
d_right=d_nextPoint;
}

void Localization()
{
    int k = 0;
    double d_alfaPoint, d_muPoint;
    while( fabs( d_right - d_left ) > E )
    {
        d_alfaPoint = d_left + ( d_right - d_left) / 4;
        d_muPoint = d_right - ( d_right - d_left) / 4;
        if ( func ( d_alfaPoint ) < func ( d_muPoint ))
            d_right = d_muPoint;
        else
            d_left = d_alfaPoint;
        k++;
    }
    double d_Xmin = ( d_left + d_right ) / 2;
    cout<<"Iteration ="<<k<<endl;
    cout<<"Min ="<<d_Xmin<<endl;
}

double GetFib(double d_number)      //1 1 2 3 5 8 13... - числа Фиб
{
double F0=1,F1=1,F2;

if((d_number==1)||(d_number==2))    //для получения 1-го или 2-го числа Фиб
 return 1;

for(double i=3; i<=d_number; i++)
 {
 F2=F0+F1;
 F0=F1;
 F1=F2;
 }
return F2;
}

double GetFibNumber(double Fn)   // функция, вычисляющая номер заданного числа Фиббоначи
{
double F0=1,F1=1,F2,d_number=2;
do
 {
 F2=F0+F1;
 F0=F1;
 F1=F2;
 d_number++;
 }while(F2<Fn);
return d_number;
}

double Fib()
{
double Ln=0.00001;
double L1=0;
double Fn;
double d_alfaPoint=0;
double d_muPoint=0;
int k;
cout<<d_left<<"----"<<d_right;
L1=fabs(d_right-d_left);
do
{
Fn=GetFib(d_number);
d_number=GetFibNumber(Fn);
d_number++;
}while(Fn<(L1/Ln));
d_alfaPoint=d_left+(GetFib(d_number-2)/Fn)*L1;
d_muPoint=d_left+(GetFib(d_number-1)/Fn)*L1;
k=1;
while(k!=(d_number-1))
	{
	d_muPoint=d_alfaPoint+E;
  if(func(d_startPoint)<func(d_nextPoint))
  	{
    xmed=(d_left+d_muPoint)/2;
    d_left=d_left;
    d_right=d_muPoint;
    d_muPoint=d_alfaPoint;
    d_alfaPoint=d_left+(GetFib(d_number-k-2)/(GetFib(d_number-k)))*L1;
    }
  else
  	{
  	xmed=(d_right+d_alfaPoint)/2;
    d_right=d_right;
    d_left=d_alfaPoint;
    d_alfaPoint=d_muPoint;
    d_muPoint=d_left+(GetFib(d_number-k-1)/(GetFib(d_number-k)))*L1;
    }
  k++;
  }
return xmed;
}



double DevisSvennKempi()
{
double a;
double b;
double c;
double d;
if(d_startPoint==0)
	d_step=0.01;
else
	d_step=0.01*fabs(d_startPoint);
d_nextPoint=d_startPoint+d_step;
while(1)
 {
if(func(d_startPoint)<func(d_nextPoint))
	{
  d_step=-d_step;
  d_nextPoint=d_startPoint+d_step;
  }
do
	{
  d_startPoint=d_nextPoint;
  d_nextPoint=d_startPoint+d_step;
  d_step=2*d_step;
  }
while(func(d_nextPoint)>func(d_startPoint));
d_previousPoint=(d_startPoint+d_nextPoint)/2;
if(d_previousPoint<d_startPoint)
	{
  a=d_startPoint;
	b=d_previousPoint;
  c=d_nextPoint;
  }
else
	{
  a=d_startPoint-0.5*d_step;
	b=d_startPoint;
  c=d_previousPoint;
  }
double if1,if2;
d=b+0.5*((b-a)*(func(a)-func(c)))/(func(a)-2*func(b)+func(c));
if1=fabs((d-b)/b);
if2=fabs((func(d)-func(b))/func(b));
if(if1<=E && if2<=E)
	return (b+d)/2;
else
	{
  if(func(b)<func(d))
  	d_startPoint=b;
  else
    {
  	d_startPoint=d;
  	d_step=d_step/2;
    }
	}
 }
}

void main()
{
double db_startPoint=d_startPoint;
Svenn();
Localization();
Fib();
cout<<endl<<"Minimum="<<DevisSvennKempi();
getch();
}

Соседние файлы в папке prog