Скачиваний:
13
Добавлен:
01.05.2014
Размер:
1.6 Кб
Скачать
class Function
{
	public:

  Matrix GetHk(Matrix X, Function F);
  Matrix Newton(Matrix x1, Function F);
	inline double Val (Matrix X) { double x1 = X.element[0][0], x2 = X.element[1][0]; return (x1+1)*(x1+1)+(x2-1)*(x2-1); }
	inline double df_dx1 (Matrix X) { double x1 = X.element[0][0], x2 = X.element[1][0]; return 2*x1+2; }
	inline double df_dx2 (Matrix X) { double x1 = X.element[0][0], x2 = X.element[1][0]; return 2*x2-2; }
	inline double df_dx1dx1 (Matrix X) { return 2; }
	inline double df_dx1dx2 (Matrix X) { return 0; }
	inline double df_dx2dx1 (Matrix X) { return 0; }
	inline double df_dx2dx2 (Matrix X) { return 2; }

	inline double df_dp (Matrix X, Matrix P)
	{
		Matrix g = this->AntiGrad(X);
		return g.element[0][0]*P.element[0][0] + g.element[1][0]*P.element[1][0];
	}

	inline Matrix AntiGrad (Matrix X)
	{
			Matrix g (2, 1);
		g.element[0][0] = -this->df_dx1(X);
		g.element[1][0] = -this->df_dx2(X);
		return g;
	}

	double GetAlpha (Matrix X, Matrix P)
	{
		double Alpha=0, h=0.00001, a, b, c, d, dc, dd;
		Matrix X1 = X;
		if (this->Val(X) < this->Val(X+P*h))
    	h *= -1;

		while(this->Val(X + P*h) > this->Val(X + P*(2*h)))
			{
			h *= 2;
    	}
		if (h/2 > h*2)
    	{
    	a = h*2;
    	b = h/2;
    	}
		else
    	{
    	a = h/2;
    	b = h*2;
    	}
		while ((b-a) > 0.0001)
			{
			dc = this->Val(X + P*(a+(b-a)/4));
			dd = this->Val(X + P*(b-(b-a)/4));
    	if (dc > dd)
				{
					a = a + (b-a)/4;
				}
				else
				{
					b = b - (b-a)/4;
				}
			}

			Alpha = (b+a) / 2;
			return Alpha;
  }
};
Соседние файлы в папке prog