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

int func=1;
int dif=1;
int kolit=10;
double eps=0.001;

double x011,x022,x033;
class Vector
{
public:
double x1,x2,x3;
   Vector(double x01, double x02, double x03)
   {
    x1=x01; x2=x02; x3=x03;
   }

};


Vector a(0,0,0);
double fx( Vector a, double l, double p1, double p2, double p3, int func)
  {
	  double x11,x22,x33;

	  x11=a.x1+l*p1;
	  x22=a.x2+l*p2;
	  x33=a.x3+l*p3;
  if (func==1)
	  {
	  return(x11*x11*x11+x22*x22-3*x11-2*x22+2);
	   }
  if (func==2)
	    {
	     return((x11-1)*(x11-1)+(x22-3)*(x22-3)+4*(x33+5)*(x33+5));
	     }

  if (func==3)
	      {
	      return(4*(x11-5)*(x11-5)+(x22-6)*(x22-6));
	      }
	       }


double p11(Vector a, int func)
{
if (func==1)
  return(3*a.x1*a.x1-3);
if (func==2)
  return(2*a.x1-2);
if (func==3)
  return(8*a.x1-40);
}
double p22(Vector a, int func)
{
if (func==1)
return(2*a.x2-2);
if (func==2)
return(2*a.x2-6);
if (func==3)
return(2*a.x1-12);
}

double p33(Vector a, int func)
{
 if (func==1)
 return(0);
if (func==2)
return(8*a.x3+40);
if (func==3)
return(0);
}
double normgrad(Vector a,int func)
{
double g1; double g2; double g3;
g1=p11(a,func);
g2=p22(a,func);
g3=p33(a,func);
return(sqrt(g1*g1+g2*g2+g3*g3));
}
double dfx(Vector a, double l, double p1, double p2,double p3,int func, int dif)
{
	  double g1,g2,g3,x11,x22,x33;


	  x11=a.x1+l*p1;
	  x22=a.x2+l*p2;
	  x33=a.x3+l*p3;
	 if(dif==0&&func==1)
	 {

	   double dlx=0.0001;double dly=0.0001;
	  double a1,a2,a3,g11,g22,g33;
	  a1=a.x1;
	  a.x1=a.x1+dlx;
	  g11=fx(a,l,p1,p2,p3,func);
	  a.x1=a1;
	  g1=(g11-fx(a,l,p1,p2,p3,func))/dlx;
	  a2=a.x2;
	  a.x2=a.x2+dly;
	  g22=fx(a,l,p1,p2,p3,func);
	  a.x2=a2;
	  g2=(g22-fx(a,l,p1,p2,p3,func))/dly;
	  g3=0;
	  }
	  else
	  {

	  if (func==1)
	  {
	  g1=3*x11*x11-3;
	  g2=2*x22-2;
	  g3=0;
	  }
	  if (func==2)
	  {
	  g1=2*x11-2;
	  g2=2*x22-6;
	  g3=8*x33+40;
	  }
	  if (func==3)
	  {
	  g1=8*x11-40;
	  g2=2*x22-12;
	  g3=0;
	  }


	  }

return(g1*p1+g2*p2+g3*p3);
}

double zc(Vector a, double p1, double p2,double p3, int func,int dif)

{
	double x,l,d,x1,x2,x3,x4,dfxa,dfxb,dfx0;

	{

//	 clrscr();
//////////////////// Algoritm Svenna/////////////////
		l = 0.01;
		int kp=0;
		dfx0=dfx(a,0,p1,p2,p3,func,dif);
		if (dfx0>0)
		{
			l = -l;
		}
		do
		{
			l = 2*l;
		dfxa=dfx(a,l,p1,p2,p3,func,dif);
		dfxb=dfx(a,l/2,p1,p2,p3,func,dif);
		kp++;
		}
		while (dfxa * dfxb>0&&kp<30);

	 double b=l/2 ;
	  if(l>b)
	{  b=l;
		l=l/2;
		 }

///////////// Zolotoe se4enie 1/////////////////////////////


double ls=0,m=0;

m=l+0.618*fabs(b-l);
	ls=l+0.382*fabs(b-l);
	int k=0;
	while (fabs(b-l)>0.00001&&k<5)
	{
		if ((fx(a,ls,p1,p2,p3,func)<fx(a,m,p1,p2,p3,func)))
		{
			b=m;
			m=ls;
			ls=l+0.382*fabs(b-l);
		}
		else
		{
			l=ls;
			ls=m;
			m=l+0.618*fabs(b-l);
		}
		k++;
	}


double x;
		x=(l+b)/2;

return(x);


}
}

int menu()
{
int num;
clrscr();
cout<<endl<<"Labaratory work 5"<<endl;
cout<<endl<<endl;
cout<<"1-Input of function"<<endl;
cout<<"2-Input of an index point"<<endl;
cout<<"3-Input of a metod of differentiation"<<endl;
cout<<"4-Input of an error"<<endl;
cout<<"5-Start of the program"<<endl;
cout<<"6-By default"<<endl;
cout<<"7-Exit"<<endl;
cout<<endl<<endl;
cout<<"Your choice:";
cin>>num;
return num;
}

int menu1()
{   int fun;
clrscr();
cout<<endl<<endl;
cout<<"Input of function"<<endl;
cout<<endl<<endl;
cout<<"1-F(x1,x2)=   x1^3+x2^2-3*x1-2*x2+2"<<endl;
cout<<"2-F(x1,x2,x3)=(x1-1)^2+(x2-3)^3+4*(x3+5)^2"<<endl;
cout<<"3-F(x1,x2)=    4*(x1-5)^2+(x2-6)^2"<<endl;
cout<<endl<<endl;
cout<<"Your choice:";
cin>>fun;
return fun;
}
Vector menu2()
{
Vector a(0,0,0);
clrscr();
cout<<endl<<endl;
cout<<"Input of an index point"<<endl;
cout<<endl<<endl;
cout<<"Enter x1:";
cin>>x011;
cout<<"Enter x2:";
cin>>x022;
if (func==2)
{cout<<"Enter x3:";
cin>>x033;
}
a.x1=x011;
a.x2=x022;
a.x3=x033;
}
int menu3()
{   int diff;
clrscr();
cout<<endl<<endl;
cout<<"Input of a metod of differentiation"<<endl;
cout<<endl<<endl;
cout<<"0-Analitical differentiation"<<endl;
cout<<"1-Numerical differentiation"<<endl;
cout<<endl<<endl;
cout<<"Your choice:";
cin>>diff;
return diff;
}

void menu4()
{
clrscr();
cout<<endl<<endl;
cout<<"Input of an error"<<endl;
cout<<endl<<endl;

	int error=1;
	while(error==1)
	{
	cout<<"Enter error from 0.01 to 0.0000001:";
	cin>>eps;
	if((eps>0.01)||(eps<0.0000001))
	{
		cout<<"Error!!!"<<endl;
	}
	else error=0;
	}

}


void menu6()
{
clrscr();
cout<<endl<<endl;
cout<<"By default "<<endl;
cout<<endl<<endl;
cout<<"Function:F(x1,x2)=x1^3+x2^2-3*x1-2*x2+2"<<endl;
cout<<"Index point:(0,0)"<<endl;
cout<<"Analitical differentaition"<<endl;
cout<<"Error:0.001"<<endl;
getch();
}
void menu5()
{
clrscr();

double x;
double p1;double p2;double p3;
double norm=normgrad(a,func);
if (func==3) kolit=2;
int kol=0;
while (norm>eps&&kol<kolit)
{
p1=p11(a,func);
p2=0;
p3=0;
x=zc(a,p1,p2,p3,func,dif);
a.x1 = a.x1+x*p1;
p2=p22(a,func);
p1=0;
p3=0;
x=zc(a,p1,p2,p3,func,dif);
a.x2=a.x2+x*p2;
if (func==2)
{
p3=p33(a,func);
p1=0;
p2=0;
x=zc(a,p1,p2,p3,func,dif);
a.x3=a.x3+x*p3;
}
kol++;
norm=normgrad(a,func);
}

cout<<endl<<endl;
cout<<"Function number "<<func<<endl;
cout<<"Differentaition ";
if (dif==0)
cout<<"analitical";
else cout<<"numerical";
cout<<endl<<"Error:"<<eps<<endl;
cout<<"Quantity of iterations:"<<kol<<endl;
cout<<"Rezult point:("<<a.x1<<","<<a.x2;
if (func==2) cout<<","<<a.x3<<")"<<endl;
else cout<<")";
getch();
}








void main()
{
clrscr();
Vector a(0,0,0);
int pmenu;
pmenu=menu();
while (pmenu!=7)
{
if (pmenu==1)
func=menu1();
if ((func!=1)&&(func!=2)&&(func!=3))
{ func=1;}
if (pmenu==2)
menu2();
if (pmenu==3)
dif=menu3();
if ((dif!=0)&&(dif!=1))
{ dif==1;}
if (pmenu==4)
{menu4();}
if (pmenu==5)
{menu5(); break;}
if (pmenu==6)
{menu6();}
pmenu=menu();
}



}
Соседние файлы в папке all