Скачиваний:
14
Добавлен:
01.05.2014
Размер:
2.8 Кб
Скачать
#include <conio.h>
#include <iostream.h>
#include <math.h>

struct Vector
{
 double x1,x2;
 Vector()
 {
  x1=0;
  x2=0;
 }
 Vector(double ax1,double ax2)
 {
  x1=ax1;
  x2=ax2;
 }
 const Vector operator * (const double &vec) const
 {
  return Vector(x1*vec,x2*vec);
 }

 const void operator = (const Vector &vec)
 {
  x1=vec.x1;
  x2=vec.x2;
 }

 const double operator * (const Vector &vec) const
 {
  return (x1*vec.x1+x2*vec.x2);
 }

 const Vector operator + (const Vector &vec) const
 {
  return Vector(x1+vec.x1,x2+vec.x2);
 }

 const Vector operator - (const Vector &vec) const
 {
  return Vector(x1-vec.x1,x2-vec.x2);
 }
};

double function(Vector vec)
{
 return(2*vec.x1*vec.x1-vec.x2*vec.x2-2*vec.x1*vec.x2+vec.x1);
}

/*double proizv(Vector vec,Vector p)
{
 Vector dx=vec*0.001;
 Vector vec1,vec2;
 vec1=vec;
 vec2=vec;
 vec1.x1+=dx.x1;
 vec2.x2+=dx.x2;
 grad.x1=(function(vec)-function(vec1))/dx.x1;
 grad.x2=(function(vec)-function(vec2))/dx.x2;
 return(grad*p*(-1));
}*/

double f_dx1(Vector vec)
{
 return(4*vec.x1-2*vec.x2+1);
}

double f_dx2(Vector vec)
{
 return (-2*vec.x2-2*vec.x1);
}

Vector Angrad(Vector vec)
{
 Vector grad;
 grad=Vector(f_dx1(vec),f_dx2(vec));
 return (grad);
}

double proizv(Vector vec,Vector p)
{
 Vector grad;
 grad=Vector(f_dx1(vec),f_dx2(vec));
 return (grad*p);
}

void Swann4(Vector x1,double &a,double &b,Vector &p)
{
 double pr;
 Vector x2;
 pr=proizv(x1,p);
 if(pr>0) p=Vector(0,0)-p;
 b=fabs(function(x1)/pr-1);
 if(b>1) b=1;
 x2=x1+p*b;
 while(proizv(x1,p)*proizv(x2,p)>0)
 {
  a=b;
  b*=2;
  x1=x2;
  x2=x1+p*b;
 }
}

double Alfa(Vector x1,Vector p,double a1,double b1)
{
 double w,z,ar,k;
 Vector r;
 Swann4(x1,a1,b1,p);
 z=proizv(x1+p*a1,p)+proizv(x1+p*b1,p)+3*(function(x1+p*a1)-function(x1+p*b1))/(b1-a1);
 w=sqrt(z*z-proizv(x1+p*a1,p)*proizv(x1+p*b1,p));
 ar=b1*(w-proizv(x1+p*a1,p)+z)/(2*w-proizv(x1+p*a1,p)+proizv(x1+p*b1,p));
 return(ar);
}

Vector AntyGesse(Vector vec,Vector angrad)
{
 double d[2][2]={0};
 d[1][1]=4;
 d[0][1]=2;
 d[1][0]=2;
 d[0][0]=-2;
 double opr=d[1][1]*d[0][0]-d[1][0]*d[0][1];
 for (int i=0;i<2;i++)
  for (int k=0;k<2;k++)
   d[i][k]/=opr;
 Vector p;
 p.x1=-(d[0][0]*angrad.x1+d[0][1]*angrad.x2);
 p.x2=-(d[1][0]*angrad.x1+d[1][1]*angrad.x2);
 return(p);
}

void Newton(Vector x1)
{
 Vector p,angrad,x2;
 double al,e=0.0001,a1,b1;
 int k=0;
 do
 {
  angrad=Angrad(x1);
  p=AntyGesse(x1,angrad);
  al=Alfa(x1,p,a1,b1);
  x2=x1+p*al;
  x1=x2;
  k++;
 }while((sqrt(angrad.x1*angrad.x1+angrad.x2*angrad.x2)>e) && (sqrt(p.x1*p.x1+p.x2*p.x2)));
 cout<<x2.x1<<endl<<x2.x2<<endl<<k;
}

void main()
{
 clrscr();
 Vector x1;
 x1=Vector(0,0);
 Newton(x1);
 getch();
}
Соседние файлы в папке all