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

double d,min1,min2,p1=1,p2=0,al,x2=3,x22,x1=0,x12,a1,a2,b1,b2,l1,l2,m1,m2,e=0.0001;

double f (double x1, double x2)
{ return (x1-2)*(x1-2)*(x1-2)*(x1-2) + (x1-2*x2)*(x1-2*x2);}

double proiz (double x1, double x2, double p1, double p2)
{ return (4*(x1-2)*(x1-2)*(x1-2) + 2*(x1-2*x2))*p1 - 4*(x1-2*x2)*p2;}

double grad1 (double x1, double x2)
{ return (4*(x1-2)*(x1-2*(x1-2)) + 2*(x1-2*x2));}

double grad2 (double x1, double x2)
{ return (-4*(x1-2*x2));}

double swann4 (double x1, double x2)
{
 if (fabs((f(x1,x2)-proiz(x1,x2,p1,p2))/proiz(x1,x2,p1,p2)) > 1) {al = 1;}

 else {al = fabs((f(x1,x2)-proiz(x1,x2,p1,p2))/proiz(x1,x2,p1,p2));}

 if (proiz(x1,x2,p1,p2) > 0)
 { p1=-p1;p2=-p2;}

 while (proiz(x12,x22,p1,p2) < 0)
 {
	x12 = x1 + al*p1;
	x22 = x2 + al*p2;
	x1 = x12;
	x2 = x22;
	al = 2*al;
 }

 a1 = x1 - 0.5*al*p1; b1 = x1;
 a2 = x2 - 0.5*al*p2; b2 = x2;
 return (a1,a2,b1,b2,p1,p2,al);
}

double poisk (double x1,double x2,double p1,double p2)
{
//swann4

/* if (fabs((f(x1,x2)-proiz(x1,x2,p1,p2))/proiz(x1,x2,p1,p2)) > 1) {al = 1;}

 else {al = fabs((f(x1,x2)-proiz(x1,x2,p1,p2))/proiz(x1,x2,p1,p2));}

 if (proiz(x1,x2,p1,p2) > 0)
 { p1=-p1;p2=-p2;}

 while (proiz(x12,x22,p1,p2) < 0)
 {
	x12 = x1 + al*p1;
	x22 = x2 + al*p2;
	x1 = x12;
	x2 = x22;
	al = 2*al;
 }

 a1 = x1 - 0.5*al*p1; b1 = x1;
 a2 = x2 - 0.5*al*p2; b2 = x2;*/

 swann4(x1,x2);
//Dihotomija
 d = 0.001*e;
 l1 = p1*(a1+b1)/2 - d*p1;
 m1 = p1*(a1+b1)/2 + d*p1;
 l2 = p2*(a2+b2)/2 - d*p2;
 m2 = p2*(a2+b2)/2 + d*p2;

 while (fabs(b1-a1) >= e)
 {
	l1 = p1*(a1+b1)/2 - 0.5*d*p1;
	m1 = p1*(a1+b1)/2 + 0.5*d*p1;
	l2 = p2*(a2+b2)/2 - 0.5*d*p2;
	m2 = p2*(a2+b2)/2 + 0.5*d*p2;

	if (f(l1,l2) < f(m1,m2))
	{
	 b1 = m1;
	 b2 = m2;
	}
	else
	{
	 a1 = l1;
	 a2 = l2;
	}
 }

 min1 = (a1+b1)/2;
 min2 = (a2+b2)/2;

 return(min1,min2);
}
void main()
{
 clrscr();
 swann4(x1,x2);
 cout<<" a = ("<<a1<<", "<<a2<<")"<<'\n';
 cout<<" b = ("<<b1<<", "<<b2<<")"<<'\n';
 cout<<min1<<"  "<<min2;
 getch();
}
Соседние файлы в папке all