Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
#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,vct& x2,double& alp,vct& p,double& alp0)
{
if(gp(x1,p)>0) p*=-1;
x2=x1+p*alp;
while(gp(x1,p)*gp(x2,p)<0)
{
x1=x2;
x2=x1+p*alp;
alp0=alp;
alp*=2;
}
}
void main()
{
clrscr();
vct p,x1,x2,r;
double alp,alp0,z,w,ar;
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;
x2=x1+p*alp;
Swann4(x1,x2,alp,p,alp0);
z=f(x1)+f(x2)+3*(f(x1)-f(x2))/f(x2);
w=sqrt(z*z-gp(x1,p)*gp(x2,p));
ar=alp*(w-gp(x1,p)+z)/(2*w-gp(x1,p)+gp(x2,p));
r=x1+p*ar;
alp0=alp;
while(gp(r,p)>e);
{
alp=ar;
x1=r;
ar=alp*(w-gp(x1,p)+z)/(2*w-gp(x1,p)+gp(r,p));
r=x1+p*ar;
}
printf("%lf %lf",alp,ar);
// getch();
}
Соседние файлы в папке all