Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Различные алгоритмы минимизации, реализованные в С++ / all / DSKG
.CPP#include <stdio.h>
#include <math.h>
#include <conio.h>
#define e .00001
struct vct
{
double x1,x2;
vct()
{
x1=0;
x2=0;
}
vct(double ax1,double ax2)
{
x1=ax1;
x2=ax2;
}
const vct operator * (const double &v) const
{
return vct(x1*v,x2*v);
}
const void operator = (const vct &v)
{
x1=v.x1;
x2=v.x2;
}
const double operator * (const vct &v) const
{
return (x1*v.x1+x2*v.x2);
}
const vct operator + (const vct &v) const
{
return vct(x1+v.x1,x2+v.x2);
}
const vct operator - (const vct &v) const
{
return vct(x1-v.x1,x2-v.x2);
}
};
double f(vct v)
{
return(v.x1*v.x1+3*v.x2*v.x2+2*v.x1*v.x2);//(2*x*x+16/x);
}
double dx1(vct v)
{
return(2*v.x1+2*v.x2);
}
double dx2(vct v)
{
return(6*v.x2+2*v.x1);
}
double gp(vct v,vct p)
{
vct grad;
grad=vct(dx1(v),dx2(v));
return(grad*p);
}
void Swann4(vct& x1,double& a,double& b,vct& p)
{
double pr;
vct x2;
pr=gp(x1,p);
if(pr>0) p=vct(0,0)-p;
b=fabs(f(x1)/pr-1);
if(b>1) b=1;
x2=x1+p*b;
while(gp(x1,p)*gp(x2,p)>0)
{
a=b;
b*=2;
x1=x2;
x2=x1+p*b;
};
}
void main()
{
clrscr();
vct p,x1,x2;
double a=0,b=0;
x1=vct(1,1);
p=vct(1,0);
Swann4(x1,a,b,p);
printf("(%lg,%lg) a=%lg b=%lg (%lg,%lg)",x1.x1,x1.x2,a,b,p.x1,p.x2);
// getch();
}
Соседние файлы в папке all