Скачиваний:
14
Добавлен:
01.05.2014
Размер:
3.01 Кб
Скачать
#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(4*vec.x1*vec.x1+3*vec.x2*vec.x2-4*vec.x1*vec.x2+vec.x1);
return(-12*vec.x2+4*vec.x1*vec.x1+4*vec.x2*vec.x2-4*vec.x1*vec.x2);
//return(3*vec.x1*vec.x1+3*vec.x2*vec.x2-vec.x1*vec.x2+vec.x1);
//return (4*(vec.x1-5)*(vec.x1-5)+(vec.x2-6)*(vec.x2-6));
}

double f_dx1(Vector vec)
{
//return(8*vec.x1-4*vec.x2+1);
return(8*vec.x1-4*vec.x2);
//return(6*vec.x1-vec.x2+1);
//return (8*(vec.x1-5));
}

double f_dx2(Vector vec)
{
//return(6*vec.x2-4*vec.x1);
return(-12+8*vec.x2-4*vec.x1);
//return(6*vec.x2-vec.x1);
//return (2*(vec.x2-6));
}

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

/*double Alfa(Vector vec,Vector p)
{
 double a;
 a=(40*p.x1+12*p.x2-8*vec.x1*p.x1-2*vec.x2*p.x2)/(8*p.x1*p.x1+2*p.x2*p.x2);
 return(a);
} */
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 angrad)
{
 double d[2][2]={0};
 d[1][1]=8;
 d[0][1]=4;
 d[1][0]=4;
 d[0][0]=8;
 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;
 do
 {
  angrad=Angrad(x1);
  p=AntyGesse(angrad);
  al=Alfa(x1,p,a1,b1);
  x2=x1+p*al;
  x1=x2;
 }while((sqrt(angrad.x1*angrad.x1+angrad.x2*angrad.x2)>e) && (sqrt(al*p.x1*al*p.x1+al*p.x2*al*p.x2)));
 cout<<x2.x1<<endl<<x2.x2;
}

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