Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Различные алгоритмы минимизации, реализованные в С++ / all / MDSK
.CPP#include <stdio.h>
#include <math.h>
#include <conio.h>
const double e=.000001;
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);
}
void operator *= (const double &v)
{
x1*=v;
x2*=v;
}
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,vct& p,vct& x2)
{
if(gp(x1,p)>0) p*=-1;
x2=x1+p*a;
while(gp(x1,p)*gp(x2,p)>0)
{
a*=2;
x1=x2;
x2=x1+p*a;
};
}
void main()
{
clrscr();
vct p,x1,x2;
double a=0,b=0,c=0,alp,X;
x1=vct(1,1);
p=vct(2,3);
/* alp=fabs((f(x1)-gp(x1,p))/gp(x1,p));
alp=alp>1?1:alp;
if(gp(x1,p)>0) p*=-1;*/
a=1;
// x2=x1+p*alp;
Swann4(x1,a,p,x2);
c=(a+b)/2;
X=
printf("(%lg,%lg) (%lg,%lg)",x1.x1,x1.x2,x2.x1,x2.x2);
// getch();
}
Соседние файлы в папке all