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

double func(double x1, double x2)
	{
	 return (2*x1*x1*x1+4*x1*x2*x2*x2-10*x1*x2+x2*x2);
	}
double df(double x1, double x2, double p1, double p2)
	{
	 return ((6*x1*x1+4*x2*x2*x2-10*x2)*p1+(12*x1*x2*x2-10*x1+2*x2)*p2);
	}

void main()
{
 clrscr();
 double x10,x20,x1,x2,x11,x22,p1,p2,a1,a2,b1,b2,c1,c2,d1,d2,xmin1,xmin2,eps=0.0001,alfa0,alfak;
 int k1=1,k2=0;

 cout<<"‚ўҐ¤ЁвҐ X:\n";
 cin>>x10;
 cin>>x20;

 cout<<"\n‚ўҐ¤ЁвҐ ­ Їа ў«Ґ­ЁҐ P:\n";
 cin>>p1;
 cin>>p2;

 double n=fabs((func(x10,x20)-df(x10,x20,p1,p2))/df(x10,x20,p1,p2));
 if(n>1) {alfa0=1;}
 else {alfa0=n;}

 alfak=alfa0;
 x1=x10+alfak*p1;
 x2=x20+alfak*p2;

 if (func(x1,x2)>func(x10,x20))
	{
	 p1=-p1;
	 p2=-p2;
	}

 x1=x10+alfak*p1;
 x2=x20+alfak*p2;

 while(func(x10,x20)>func(x1,x2))
	{
	 x10=x1;
	 x20=x2;
	 alfak=alfak*2;
	 x1=x10+alfak*p1;
	 x2=x20+alfak*p2;
	 k1++;
	}

 x11=(x1+x10)/2;
 x22=(x2+x20)/2;

 if (func(x10,x20)<func(x11,x22))
	{
	 a1=x10;
	 a2=x20;
	 c1=x11;
	 c2=x22;
	}
 else
	{
	 a1=x11;
	 a2=x22;
	 c1=x10;
	 c2=x20;
	}

 b1=(a1+c1)/2;
 b2=(a2+c2)/2;

 d1=b1+0.5*(b1-a1)*(func(a1,a2)-func(c1,c2))/(func(a1,a2)-2*func(b1,b2)+func(c1,c2));
 d2=b2+0.5*(b2-a2)*(func(a1,a2)-func(c1,c2))/(func(a1,a2)-2*func(b1,b2)+func(c1,c2));

 while ((sqrt(((b1-d1)*(b1-d1)+(b2-d2)*(b2-d2))/(d1*d1+d2*d2))>eps)||((func(b1,b2)-func(d1,d2))/func(d1,d2))>eps)
 {
 alfak=alfa0;
 x1=x10+alfak*p1;
 x2=x20+alfak*p2;

 if (func(x1,x2)>func(x10,x20))
	{
	 p1=-p1;
	 p2=-p2;
	}

 x1=x10+alfak*p1;
 x2=x20+alfak*p2;

 while(func(x10,x20)>func(x1,x2))
	{
	 x10=x1;
	 x20=x2;
	 alfak=alfak*2;
	 x1=x10+alfak*p1;
	 x2=x20+alfak*p2;
	 k1++;
	}

 x11=(x1+x10)/2;
 x22=(x2+x20)/2;

 if (func(x10,x20)<func(x11,x22))
	{
	 a1=x10;
	 a2=x20;
	 c1=x11;
	 c2=x22;
	}
 else
	{
	 a1=x11;
	 a2=x22;
	 c1=x10;
	 c2=x20;
	}

 b1=(a1+c1)/2;
 b2=(a2+c2)/2;

 d1=b1+0.5*(b1-a1)*(func(a1,a2)-func(c1,c2))/(func(a1,a2)-2*func(b1,b2)+func(c1,c2));
 d2=b2+0.5*(b2-a2)*(func(a1,a2)-func(c1,c2))/(func(a1,a2)-2*func(b1,b2)+func(c1,c2));

 alfa0=alfa0/2;
 if (func(b1,b2)>func(d1,d2))
	{
	 x10=b1;
	 x20=b2;
	}
 else
	{
	 x10=d1;
	 x20=d2;
	}
 k2++;
 }
 xmin1=(b1+d1)/2;
 xmin2=(b2+d2)/2;

 cout<<"\nЊЁ­Ё¬г¬  =  "<<xmin1;
 cout<<"\n            "<<xmin2;
 cout<<"\n—Ёб«® ЁвҐа жЁ© = "<<k2;

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