Добавил:
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);
}
double nrm(vct z)
{
return(sqrt(z.x1*z.x1+z.x2*z.x2));
}
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,xc;
double a=0,b=0,f1,f2,aa,bb,c,xx;
x1=vct(1,1);
p=vct(2,3);
do
{
Swann4(x1,a,b,p);
x2=x1+p*(b-a);
do
{
/* f1=gp(x1,p);
f2=gp(x2,p);
aa=6*(f(x1)-f(x2))+3*(f1+f2);
bb=6*(f(x2)-f(x1))-2*(2*f1-f2);
c=f1;
xx=(-bb+sqrt(bb*bb-4*aa*c))/(2*aa);
xc=x1*(1-xx)+x2*xx;
/**/ xc=(x1+x2)*0.5;
if((gp(xc,p))>0)
{
x2=xc;
}
else
{
x1=xc;
}
}while(nrm(x1-x2)>e*e);
p=vct(-dx1(x1),-dx2(x1));
}while(nrm(vct(dx1(x1),dx2(x1)))>e);
printf("(%lg,%lg) (%lg,%lg)",x1.x1,x1.x2,p.x1,p.x2);
// getch();
}
Соседние файлы в папке all