Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Различные алгоритмы минимизации, реализованные в С++ / all / Bolc
.cpp#include <iostream.h>
#include <conio.h>
#include <math.h>
#define eps 0.000001
//Є« бб point
class point
{
public:
double x1;
double x2;
point(double a,double b)
{
x1=a;
x2=b;
}
point()
{
x1=0;
x2=0;
}
point & operator -= (point d)
{
x1-=d.x1;
x2-=d.x2;
return *this;
}
point & operator += (point d)
{
x1+=d.x1;
x2+=d.x2;
return *this;
}
point & operator *= (double d)
{
x1*=d;
x2*=d;
return *this;
}
point & operator /=(double d)
{
x1/=d;
x2/=d;
return *this;
}
point & operator = (point d)
{
x1=d.x1;
x2=d.x2;
return *this;
}
point & operator = (double d)
{
x1=d;
x2=d;
return *this;
}
void prnt()
{
cout<<"x1="<<x1<<" x2="<<x2<<"\n";
}
double norm(point a)
{
return(sqrt(a.x1*a.x1+a.x2*a.x2));
}
};
point operator + (point a,point b)
{
point c=a;
c+=b;
return c;
}
point operator - (point a,point b)
{
point c=a;
c-=b;
return c;
}
double operator * (point a,point b)
{
double c=a.x1*b.x1+a.x2*b.x2;
return c;
}
point operator * (point a,double b)
{
point c=a;
c*=b;
return c;
}
point operator * (double a,point b)
{
point c=b;
c*=a;
return c;
}
point operator / (point a,double b)
{
point c=a;
c/=b;
return c;
}
double f(point); //дгЄжЁп
point gradf(point); //Ја ¤ЁҐв
double df(point x,point p); //Їа®Ё§ў®¤ п ў в®зЄҐ
point Bolcano(point xx,point pp);//Ѓ®«мж ®
double dd(double,double,double,point,point,point);
//дгЄжЁп
double f(point x)
{
return (2*x.x1*x.x1+2*x.x1*x.x2+2*x.x2*x.x2-x.x1+1 );
}
//Ја ¤ЁҐв
point gradf(point x)
{
point g;
g.x1=4*x.x1+2*x.x2-1;
g.x2=4*x.x2+2*x.x1;
return g;
}
//Їа®Ё§ў®¤ п ў в®зЄҐ
double df(point x,point p)
{
return (gradf(x)*p);
}
point Bolcano(point xx,point pp)
{
point x0=xx;
point p=pp;
point a;
point b;
double alfaa=0;
double alfab=0;
double alfax=0;
double alfah=1;
int k=0;
if(df(x0,p)>0)
p*=(-1);
while((df(x0,p)*df(x0+(alfah*p),p)) > 0)
{
alfah*=2;x0+=(alfah*p);
}
a=x0;
b=x0+alfah*p;
alfab=alfah;
k++;
while (1)
{
x0=(a+b)/2;
alfax=(alfaa+alfab)/2;
if( (fabs(df(x0,p))<=eps) && ((alfab-alfaa)<=eps) )
break;
if(df(x0,p)>0)
{
b=x0;
alfab=alfax;
}
else
{
a=x0;
alfab-=alfax;
}
k++;
}
cout<<"\n"<<"Kol-vo iteracii: "<<k<<"\n"
<<"ЋвўҐв: x1="<<x0.x1<<" x2="<<x0.x2<<"\n";
return (x0);
}
void main()
{
clrscr();
point x0(1,1);
point p(1,2);
Bolcano(x0,p);
getch();
}
Соседние файлы в папке all